Using optional fields, navigate net_device list in its various incarnations.
[virt-mem.git] / lib / virt_mem.ml
index ca89e32..7bd30dc 100644 (file)
@@ -44,7 +44,8 @@ let tools = ref []
 (* Registration function used by the tools. *)
 let register
     ?(needs_ksyms = false) ?(needs_utsname = false)
-    ?(needs_tasks = false) ?(needs_everything = false)
+    ?(needs_tasks = false) ?(needs_net_devices = false)
+    ?(needs_everything = false)
     ~run
     ?(external_cmd = true)
     ?(extra_args = [])
@@ -52,7 +53,8 @@ let register
     name summary description =
   tools :=
     (name, (name, summary, description,
-           needs_ksyms, needs_utsname, needs_tasks, needs_everything,
+           needs_ksyms, needs_utsname, needs_tasks, needs_net_devices,
+           needs_everything,
            run, external_cmd, extra_args, argcheck))
   :: !tools
 
@@ -111,7 +113,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
@@ -138,7 +140,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
 
@@ -254,7 +256,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";
@@ -308,7 +310,8 @@ Options:") cmd summary description in
    * Detect that final case now and give an error.
    *)
   let name, _, _,
-    needs_ksyms, needs_utsname, needs_tasks, needs_everything,
+    needs_ksyms, needs_utsname, needs_tasks, needs_net_devices,
+    needs_everything,
     run, external_cmd, extra_args, argcheck =
     match tool with
     | Some t -> t
@@ -500,7 +503,8 @@ Possibly the '-T' command line parameter was used inconsistently.");
   let images =
     List.map (
       fun image ->
-       let kdata = { ksyms = None; utsname = None; tasks = None } in
+       let kdata = { ksyms = None; utsname = None; tasks = None;
+                     net_devices = None } in
        image, kdata
     ) images in
   (* Certain needs are dependent on others ... *)
@@ -513,6 +517,9 @@ Possibly the '-T' command line parameter was used inconsistently.");
   let needs_ksyms, needs_utsname, needs_tasks =
     if needs_everything then true, true, true
     else needs_ksyms, needs_utsname, needs_tasks in
+  let needs_ksyms, needs_utsname, needs_net_devices =
+    if needs_everything then true, true, true
+    else needs_ksyms, needs_utsname, needs_net_devices in
 
   (* Do the kernel symbol analysis. *)
   let images =
@@ -569,6 +576,22 @@ Possibly the '-T' command line parameter was used inconsistently.");
          | _, _ -> image, kdata
       ) images in
 
+  (* Get the net devices. *)
+  let images =
+    if not needs_net_devices then images
+    else
+      List.map (
+       fun (image, ({ ksyms = ksyms; utsname = utsname } as kdata)) ->
+         match ksyms, utsname with
+         | Some ksyms, Some { kernel_release = kversion } ->
+             let image, net_devices =
+               Virt_mem_net_devices.find_net_devices debug
+                 image ksyms kversion in
+             let kdata = { kdata with net_devices = net_devices } in
+             image, kdata
+         | _, _ -> image, kdata
+      ) images in
+
   (* Run the tool's main function. *)
   let errors = ref 0 in
   List.iter (
@@ -581,6 +604,8 @@ Possibly the '-T' command line parameter was used inconsistently.");
            failwith (s_"could not read kernel version")
          else if needs_tasks && kdata.tasks = None then
            failwith (s_"could not read process table")
+         else if needs_net_devices && kdata.net_devices = None then
+           failwith (s_"could not read net device table")
        );
        run debug image kdata
       with exn ->