Bring kernel version checking (utsname) into the central process.
[virt-mem.git] / lib / virt_mem.ml
index 0a956f1..03b0d1e 100644 (file)
@@ -44,11 +44,11 @@ let tools = ref []
 
 (* Registration function used by the tools. *)
 let register ?(external_cmd = true) ?(extra_args = [])
-    ?argcheck ?beforeksyms ?run
+    ?argcheck ?beforeksyms ?beforeutsname ?run
     name summary description =
   tools :=
     (name, (name, summary, description, external_cmd, extra_args,
-           argcheck, beforeksyms, run))
+           argcheck, beforeksyms, beforeutsname, run))
   :: !tools
 
 (* Main program, called from mem/virt_mem_main.ml when all the
@@ -106,7 +106,7 @@ let main () =
     match tool with
     | None ->                          (* Generic usage message. *)
        let tools = List.map (
-         fun (name, (_, summary, _, external_cmd, _, _, _, _)) ->
+         fun (name, (_, summary, _, external_cmd, _, _, _, _, _)) ->
            if external_cmd then "virt-"^name, summary
            else                 "virt-mem "^name, summary
        ) tools in
@@ -133,7 +133,7 @@ To display extra help for a single tool, do:
 Options:") tools
 
                                         (* Tool-specific usage message. *)
-    | Some (name, summary, description, external_cmd, _, _, _, _) ->
+    | Some (name, summary, description, external_cmd, _, _, _, _, _) ->
        let cmd =
          if external_cmd then "virt-" ^ name else "virt-mem " ^ name in
 
@@ -219,7 +219,7 @@ Options:") cmd summary description in
   let argspec =
     let extra_args = match tool with
       | None -> []
-      | Some (_, _, _, _, extra_args, _, _, _) -> extra_args in
+      | Some (_, _, _, _, extra_args, _, _, _, _) -> extra_args in
     let argspec = [
       "-A", Arg.String set_architecture,
         "arch " ^ s_"Set kernel architecture, endianness and word size";
@@ -270,7 +270,7 @@ Options:") cmd summary description in
    * 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, _, _, _, _, argcheck, beforeksyms, run =
+  let name, _, _, _, _, argcheck, beforeksyms,  beforeutsname, run =
     match tool with
     | Some t -> t
     | None ->
@@ -469,16 +469,15 @@ Use 'virt-mem --help' for more help or read the manual page virt-mem(1)");
    | Some beforeksyms -> beforeksyms debug images
   );
 
-  (* If there is no run function, then there is no point continuing
-   * with the rest of the program (kernel symbol analysis) ...
+  (* If there are no more callback functions, then there is no point
+   * continuing with the rest of the program (kernel symbol analysis) ...
    *)
-  if run = None then exit 0;
+  if beforeutsname = None && run = None then exit 0;
 
   (* Do the kernel symbol analysis. *)
   let images =
     List.map (
       fun image ->
-
        (* Look for ordinary kernel symbols: *)
        let image = Virt_mem_ksyms.find_kernel_symbols debug image in
        (* Look for kallsyms: *)
@@ -506,9 +505,22 @@ Use 'virt-mem --help' for more help or read the manual page virt-mem(1)");
        image
     ) images in
 
+  (* Before utsname analysis. *)
+  (match beforeutsname with
+   | None -> ()
+   | Some beforeutsname -> List.iter (beforeutsname debug) images
+  );
+
+  (* If there are no more callback functions, then there is no point
+   * continuing with the rest of the program (kernel version analysis) ...
+   *)
+  if run = None then exit 0;
+
+  (* Get the kernel version (utsname analysis). *)
+  let images = List.map (Virt_mem_utsname.find_utsname debug) images in
+
   (* Run the tool's main function. *)
   (match run with
    | None -> ()
-   | Some run ->
-       run debug images
+   | Some run -> List.iter (run debug) images
   )