Extracted kernel structures for device addressing in ifconfig.
[virt-mem.git] / lib / virt_mem_kallsyms.ml
index 87035e8..631bf61 100644 (file)
@@ -36,7 +36,7 @@ type kallsyms_compr =
   | Compressed of (string * Virt_mem_mmap.addr) list * Virt_mem_mmap.addr
   | Uncompressed of (string * Virt_mem_mmap.addr) list
 
-let find_kallsyms debug (domid, name, arch, mem, lookup_ksym) =
+let find_kallsyms debug ({ domname = domname; mem = mem } as image) ksymmap =
   let start_t = gettimeofday () in
 
   (* Now try to find the /proc/kallsyms table.  This is in an odd
@@ -50,7 +50,7 @@ let find_kallsyms debug (domid, name, arch, mem, lookup_ksym) =
    * more useful than the basic list of exports.
    *)
   let ksym_addrs = List.filter_map (
-    fun ksym -> try Some (lookup_ksym ksym) with Not_found -> None
+    fun ksym -> try Some (Ksymmap.find ksym ksymmap) with Not_found -> None
   ) Virt_mem_ksyms.common_ksyms in
 
   (* Search for those kernel addresses in the image.  We're looking
@@ -70,7 +70,7 @@ let find_kallsyms debug (domid, name, arch, mem, lookup_ksym) =
        * If found, jump backwards by length and check all addresses.
        *)
       if debug then
-       eprintf "%s: testing candidate kallsyms at %Lx\n" name addr;
+       eprintf "%s: testing candidate kallsyms at %Lx\n" domname addr;
       let rec loop addr =
        let addrp = Virt_mem_mmap.follow_pointer mem addr in
        if Virt_mem_mmap.is_mapped mem addrp then
@@ -100,7 +100,7 @@ let find_kallsyms debug (domid, name, arch, mem, lookup_ksym) =
                  let names_addr = Virt_mem_mmap.succ_long mem end_addr in
                  if debug then
                    eprintf "%s: candidate kallsyms found at %Lx (names_addr at %Lx, num_entries %d)\n"
-                     name start_addr names_addr num_entries;
+                     domname start_addr names_addr num_entries;
                  Some (start_addr, num_entries, names_addr)
              in
              loop2 start_addr
@@ -180,7 +180,7 @@ let find_kallsyms debug (domid, name, arch, mem, lookup_ksym) =
   ) ksym_addrs in
 
   if debug then (
-    eprintf "%s: candidate kallsyms at:\n" name;
+    eprintf "%s: candidate kallsyms at:\n" domname;
     List.iter (
       function
       | (start_addr, num_entries, names_addr, Uncompressed _) ->
@@ -196,23 +196,20 @@ let find_kallsyms debug (domid, name, arch, mem, lookup_ksym) =
   (* Vote for the most popular symbol table candidate and
    * enhance the function for looking up ksyms.
    *)
-  let lookup_ksym =
+  let ksymmap =
     let freqs = frequency kallsymtabs in
     match freqs with
     | [] ->
-       (* Can't find any kallsymtabs, just return the lookup_ksym
-        * function generated previously from the exported symbols.
-        *)
-       lookup_ksym
+       (* Can't find any kallsymtabs. *)
+       None
 
     | (_, (_, _, _, Uncompressed names)) :: _ ->
-       let lookup_ksym name =
-         try (* first look it up in kallsyms table. *)
-           List.assoc name names
-         with Not_found -> (* try the old exports table instead *)
-           lookup_ksym name
+       let rec loop ksymmap = function
+         | (name, value) :: names ->
+             loop (Ksymmap.add name value ksymmap) names
+         | [] -> ksymmap
        in
-       lookup_ksym
+       Some (loop ksymmap names)
 
     | (_, (start_addr, num_entries, names_addr,
           Compressed (compressed_names, markers_addr))) :: _ ->
@@ -258,14 +255,12 @@ let find_kallsyms debug (domid, name, arch, mem, lookup_ksym) =
            Some (name, sym_value)
        ) compressed_names in
 
-       let lookup_ksym name =
-         try (* first look it up in kallsyms table. *)
-           List.assoc name names
-         with Not_found -> (* try the old exports table instead *)
-           lookup_ksym name
+       let rec loop ksymmap = function
+         | (name, value) :: names ->
+             loop (Ksymmap.add name value ksymmap) names
+         | [] -> ksymmap
        in
-
-       lookup_ksym in
+       Some (loop ksymmap names) in
 
   if debug then (
     let end_t = gettimeofday () in
@@ -273,4 +268,4 @@ let find_kallsyms debug (domid, name, arch, mem, lookup_ksym) =
       (end_t -. start_t)
   );
 
-  ((domid, name, arch, mem, lookup_ksym) : image1)
+  (image, ksymmap)