Combined binary.
[virt-mem.git] / lib / virt_mem.ml
index 384a089..1f21a66 100644 (file)
@@ -38,7 +38,7 @@ let max_kallsyms_tabsize = 250_000L
 let kernel_size =
   if Sys.word_size = 32 then Sys.max_string_length
   else 0x100_0000
-let max_memory_peek = 0x1000
+let max_memory_peek = 65536 (* XXX Use D.max_peek function *)
 
 type ksym = string
 
@@ -52,7 +52,122 @@ type kallsyms_compr =
   | Compressed of (string * MMap.addr) list * MMap.addr
   | Uncompressed of (string * MMap.addr) list
 
-let start usage_msg =
+(* When tools register themselves, they are added to this list.
+ * Later, we will alphabetize the list.
+ *)
+let tools = ref [
+  "capture", (
+    "capture",
+    s_"capture memory image for post-mortem analysis",
+    s_"Capture a memory image to a file for later post-mortem
+analysis.  Use the '-o memoryimage' option to specify the
+output file.
+
+Other tools can load the memory image using the '-t' option.",
+    false,
+    (fun _ _ -> ())
+  );
+]
+
+(* Registration function used by the tools. *)
+let register name summary description is_cmd run_fn =
+  tools := (name, (name, summary, description, is_cmd, run_fn)) :: !tools
+
+(* Main program, called from mem/virt_mem_main.ml when all the
+ * tools have had a chance to register themselves.
+ *)
+let main () =
+  (* Get the registered tools, alphabetically. *)
+  let tools = !tools in
+  let tools = List.sort ~cmp:(fun (a,_) (b,_) -> compare a b) tools in
+
+  (* Which tool did the user want to run?  Look at the executable
+   * name (eg. 'virt-dmesg' => tool == dmesg).  If we don't recognise
+   * the executable name then we must look for the first parameter
+   * which doesn't begin with a '-' character.
+   *
+   * Note that we must do all of this before using the OCaml Arg
+   * module to properly parse the command line (below), so that
+   * we can have a usage message ready.
+   *)
+  let tool =
+    let prog = Sys.executable_name in  (* eg. "/usr/bin/virt-dmesg.opt" *)
+    let prog = Filename.basename prog in(* eg. "virt-dmesg.opt" *)
+    let prog =                         (* eg. "virt-dmesg" *)
+      try Filename.chop_extension prog with Invalid_argument _ -> prog in
+    let prog =                         (* eg. "dmesg" *)
+      if String.starts_with prog "virt-" then
+       String.sub prog 5 (String.length prog - 5)
+      else prog in
+    try Some (List.assoc prog tools)
+    with Not_found ->
+      let arg1 =                       (* First non-option argument. *)
+       match Array.to_list Sys.argv with
+       | [] -> None
+       | _::args ->
+           let rec loop = function
+             | [] -> None
+             | a::args when String.length a > 0 && a.[0] = '-' -> loop args
+             | a::_ -> Some a
+           in
+           loop args in
+      match arg1 with
+      | None -> None
+      | Some prog ->                   (* Recognisable first argument? *)
+         let prog =
+           try Filename.chop_extension prog with Invalid_argument _ -> prog in
+         let prog =
+           if String.starts_with prog "virt-" then
+             String.sub prog 5 (String.length prog - 5)
+           else prog in
+         (try Some (List.assoc prog tools) with Not_found -> None) in
+
+  (* Make a usage message. *)
+  let usage_msg =
+    match tool with
+    | None ->                          (* Generic usage message. *)
+       let tools = List.map (
+         fun (name, (_, summary, _, is_cmd, _)) ->
+           if is_cmd then "virt-"^name, summary
+           else           "virt-mem "^name, summary
+       ) tools in
+       (* Maximum width of field in the left hand column. *)
+       let max_width =
+         List.fold_left max 0 (List.map String.length (List.map fst tools)) in
+       let tools = List.map (fun (l,r) -> pad max_width l, r) tools in
+       let tools = List.map (fun (l,r) -> "  " ^ l ^ " - " ^ r) tools in
+       let tools = String.concat "\n" tools in
+
+       sprintf (f_"\
+
+virt-mem: Tools for providing information about virtual machines
+
+Currently available tools include:
+%s
+
+General usage is:
+  <tool> [-options] [domains...]
+
+To display extra help for a single tool, do:
+  virt-mem help <tool>
+
+Options:") tools
+
+                                        (* Tool-specific usage message. *)
+    | Some (name, summary, description, is_cmd, _) ->
+       let cmd = if is_cmd then "virt-" ^ name else "virt-mem " ^ name in
+
+       sprintf (f_"\
+
+%s: %s
+
+Description:
+%s
+
+Options:") cmd summary description in
+
+  (* Now begin proper parsing of the command line arguments. *)
+
   (* Debug messages. *)
   let debug = ref false in
 
@@ -139,7 +254,6 @@ let start usage_msg =
   ] in
 
   let anon_arg str = anon_args := str :: !anon_args in
-  let usage_msg = usage_msg ^ s_"\n\nOPTIONS" in
   Arg.parse argspec anon_arg usage_msg;
 
   let images = !images in
@@ -147,6 +261,22 @@ let start usage_msg =
   let uri = if !uri = "" then None else Some !uri in
   let anon_args = List.rev !anon_args in
 
+  (* At this point, either --help was specified on the command line
+   * (and so the program has exited) or we must have determined tool,
+   * or the user didn't give us a valid tool (eg. "virt-mem foobar").
+   * Detect that final case now and give an error.
+   *)
+  let name, _, _, _, run_fn =
+    match tool with
+    | Some t -> t
+    | None ->
+       prerr_endline (s_"\
+virt-mem: I could not work out which tool you are trying to run.
+Use 'virt-mem --help' for more help or read the manual page virt-mem(1)");
+       exit 1
+  in
+  if debug then eprintf "tool = %s\n%!" name;
+
   (* Get the kernel images. *)
   let images =
     if images = [] then (
@@ -236,8 +366,9 @@ let start usage_msg =
 
          if !def_text_addr = 0L then
            failwith
-             (sprintf (f_"%s: use -T to define kernel load address for this image")
-                name);
+             (sprintf (f_"%s: use -T to define kernel load address for this image") name);
+
+         let start_t = gettimeofday () in
 
          (* Read the kernel memory.
           * Maximum 64K can be read over remote connections.
@@ -254,6 +385,12 @@ let start usage_msg =
          in
          loop 0;
 
+         if debug then (
+           let end_t = gettimeofday () in
+           eprintf "timing: downloading kernel took %f seconds\n%!"
+             (end_t -. start_t)
+         );
+
          (* Map the virtual memory. *)
          let mem = MMap.of_string str !def_text_addr in
 
@@ -337,12 +474,22 @@ let start usage_msg =
        (* Searching for <NUL>string<NUL> *)
        let common_ksyms_nul = List.map (sprintf "\000%s\000") common_ksyms in
 
+       let start_t = gettimeofday () in
+
        (* Search for these strings in the memory image. *)
        let ksym_strings = List.map (MMap.find_all mem) common_ksyms_nul in
        let ksym_strings = List.concat ksym_strings in
        (* Adjust found addresses to start of the string (skip <NUL>). *)
        let ksym_strings = List.map Int64.succ ksym_strings in
 
+       if debug then (
+         let end_t = gettimeofday () in
+         eprintf "timing: searching for common_ksyms took %f seconds\n%!"
+           (end_t -. start_t)
+       );
+
+       let start_t = gettimeofday () in
+
        (* For any we found, try to look up the symbol table
         * base addr and size.
         *)
@@ -404,11 +551,11 @@ let start usage_msg =
        let ksymtabs = List.filter (fun (_, size) -> size > 64L) ksymtabs in
 
        if debug then (
-         printf "%s: candidate symbol tables at:\n" name;
+         eprintf "%s: candidate symbol tables at:\n" name;
          List.iter (
            fun (addr, size) ->
-             printf "\t%Lx\t%Lx\t%!" addr size;
-             printf "first symbol: %s\n%!"
+             eprintf "\t%Lx\t%Lx\t%!" addr size;
+             eprintf "first symbol: %s\n%!"
                (MMap.get_string mem
                   (MMap.follow_pointer mem
                      (MMap.succ_long mem addr)))
@@ -427,7 +574,7 @@ let start usage_msg =
 
          | (_, (ksymtab_addr, ksymtab_size)) :: _ ->
              if debug then
-               printf
+               eprintf
                  "%s: Kernel symbol table found at %Lx, size %Lx bytes\n%!"
                  name ksymtab_addr ksymtab_size;
 
@@ -459,6 +606,14 @@ let start usage_msg =
              lookup_ksym
        in
 
+       if debug then (
+         let end_t = gettimeofday () in
+         eprintf "timing: searching for ordinary ksyms took %f seconds\n%!"
+           (end_t -. start_t)
+       );
+
+       let start_t = gettimeofday () in
+
        (* Now try to find the /proc/kallsyms table.  This is in an odd
         * compressed format (but not a very successful compression
         * format).  However if it exists we know that it will contain
@@ -489,7 +644,7 @@ let start usage_msg =
             * If found, jump backwards by length and check all addresses.
             *)
            if debug then
-             printf "%s: testing candidate kallsyms at %Lx\n" name addr;
+             eprintf "%s: testing candidate kallsyms at %Lx\n" name addr;
            let rec loop addr =
              let addrp = MMap.follow_pointer mem addr in
              if MMap.is_mapped mem addrp then
@@ -516,7 +671,7 @@ let start usage_msg =
                      (* ok! *)
                      let names_addr = MMap.succ_long mem end_addr in
                      if debug then
-                       printf "%s: candidate kallsyms found at %Lx (names_addr at %Lx, num_entries %d)\n"
+                       eprintf "%s: candidate kallsyms found at %Lx (names_addr at %Lx, num_entries %d)\n"
                          name start_addr names_addr num_entries;
                      Some (start_addr, num_entries, names_addr)
                  in
@@ -553,7 +708,7 @@ let start usage_msg =
                        let names_addr = names_addr +^ Int64.of_int len +^ 2L in
                        let sym_value = MMap.follow_pointer mem start_addr in
                        let start_addr = MMap.succ_long mem start_addr in
-                       (*printf "%S -> %Lx\n" name sym_value;*)
+                       (*eprintf "%S -> %Lx\n" name sym_value;*)
                        names := (name, sym_value) :: !names;
                        loop names_addr start_addr (num-1)
                      )
@@ -591,15 +746,15 @@ let start usage_msg =
        ) ksym_addrs in
 
        if debug then (
-         printf "%s: candidate kallsyms at:\n" name;
+         eprintf "%s: candidate kallsyms at:\n" name;
          List.iter (
            function
            | (start_addr, num_entries, names_addr, Uncompressed _) ->
-               printf "\t%Lx %d entries names_addr=%Lx old-style\n%!"
+               eprintf "\t%Lx %d entries names_addr=%Lx old-style\n%!"
                  start_addr num_entries names_addr
            | (start_addr, num_entries, names_addr,
               Compressed (_, markers_addr)) ->
-               printf "\t%Lx %d entries names_addr=%Lx markers_addr=%Lx\n%!"
+               eprintf "\t%Lx %d entries names_addr=%Lx markers_addr=%Lx\n%!"
                  start_addr num_entries names_addr markers_addr
          ) kallsymtabs
        );
@@ -664,7 +819,7 @@ let start usage_msg =
                   *)
                  (*let typ = name.[0] in*)
                  let name = String.sub name 1 (String.length name - 1) in
-                 (*printf "%S -> %Lx\n" name sym_value;*)
+                 (*eprintf "%S -> %Lx\n" name sym_value;*)
                  Some (name, sym_value)
              ) compressed_names in
 
@@ -677,6 +832,12 @@ let start usage_msg =
 
              lookup_ksym in
 
+       if debug then (
+         let end_t = gettimeofday () in
+         eprintf "timing: searching for kallsyms took %f seconds\n%!"
+           (end_t -. start_t)
+       );
+
        (* Just wrap the lookup_ksym call in something which prints
         * the query when debug is set.
         *)
@@ -685,10 +846,10 @@ let start usage_msg =
            let lookup_ksym sym =
              try
                let value = lookup_ksym sym in
-               printf "lookup_ksym %S = %Lx\n%!" sym value;
+               eprintf "lookup_ksym %S = %Lx\n%!" sym value;
                value
              with Not_found ->
-               printf "lookup_ksym %S failed\n%!" sym;
+               eprintf "lookup_ksym %S failed\n%!" sym;
                raise Not_found
            in
            lookup_ksym
@@ -699,4 +860,5 @@ let start usage_msg =
        ((name, arch, mem, lookup_ksym) : image)
     ) images in
 
-  debug, images
+  (* Run the actual tool. *)
+  run_fn debug images