Completed reimplementation of faster Virt_mem_mmap.
[virt-mem.git] / lib / virt_mem.ml
1 (* Memory info for virtual domains.
2    (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *)
19
20 open Unix
21 open Printf
22 open ExtList
23 open ExtString
24
25 module C = Libvirt.Connect
26 module D = Libvirt.Domain
27
28 open Virt_mem_gettext.Gettext
29 open Virt_mem_utils
30 module MMap = Virt_mem_mmap
31
32 let min_kallsyms_tabsize = 1_000L
33 let max_kallsyms_tabsize = 250_000L
34
35 (* Make the kernel size around 16 MB, but just a bit smaller than
36  * maximum string length so we can still run this on a 32 bit platform.
37  *)
38 let kernel_size =
39   if Sys.word_size = 32 then Sys.max_string_length
40   else 0x100_0000
41 let max_memory_peek = 65536 (* XXX Use D.max_peek function *)
42
43 type ksym = string
44
45 type image =
46     int option
47     * string
48     * Virt_mem_utils.architecture
49     * ([`Wordsize], [`Endian], [`HasMapping]) Virt_mem_mmap.t
50
51 type image_with_ksyms =
52     int option
53     * string
54     * Virt_mem_utils.architecture
55     * ([`Wordsize], [`Endian], [`HasMapping]) Virt_mem_mmap.t
56     * (ksym -> MMap.addr)
57
58 type kallsyms_compr =
59   | Compressed of (string * MMap.addr) list * MMap.addr
60   | Uncompressed of (string * MMap.addr) list
61
62 (* When tools register themselves, they are added to this list.
63  * Later, we will alphabetize the list.
64  *)
65 let tools = ref []
66
67 (* Registration function used by the tools. *)
68 let register ?(external_cmd = true) ?(extra_args = [])
69     ?argcheck ?beforeksyms ?run
70     name summary description =
71   tools :=
72     (name, (name, summary, description, external_cmd, extra_args,
73             argcheck, beforeksyms, run))
74   :: !tools
75
76 (* Main program, called from mem/virt_mem_main.ml when all the
77  * tools have had a chance to register themselves.
78  *)
79 let main () =
80   (* Get the registered tools, alphabetically. *)
81   let tools = !tools in
82   let tools = List.sort ~cmp:(fun (a,_) (b,_) -> compare a b) tools in
83
84   (* Which tool did the user want to run?  Look at the executable
85    * name (eg. 'virt-dmesg' => tool == dmesg).  If we don't recognise
86    * the executable name then we must look for the first parameter
87    * which doesn't begin with a '-' character.
88    *
89    * Note that we must do all of this before using the OCaml Arg
90    * module to properly parse the command line (below), so that
91    * we can have a usage message ready.
92    *)
93   let tool, ignore_first_anon_arg =
94     let prog = Sys.executable_name in   (* eg. "/usr/bin/virt-dmesg.opt" *)
95     let prog = Filename.basename prog in(* eg. "virt-dmesg.opt" *)
96     let prog =                          (* eg. "virt-dmesg" *)
97       try Filename.chop_extension prog with Invalid_argument _ -> prog in
98     let prog =                          (* eg. "dmesg" *)
99       if String.starts_with prog "virt-" then
100         String.sub prog 5 (String.length prog - 5)
101       else prog in
102     try Some (List.assoc prog tools), false
103     with Not_found ->
104       let arg1 =                        (* First non-option argument. *)
105         match Array.to_list Sys.argv with
106         | [] -> None
107         | _::args ->
108             let rec loop = function
109               | [] -> None
110               | a::args when String.length a > 0 && a.[0] = '-' -> loop args
111               | a::_ -> Some a
112             in
113             loop args in
114       match arg1 with
115       | None -> None, false
116       | Some prog ->                    (* Recognisable first argument? *)
117           let prog =
118             try Filename.chop_extension prog with Invalid_argument _ -> prog in
119           let prog =
120             if String.starts_with prog "virt-" then
121               String.sub prog 5 (String.length prog - 5)
122             else prog in
123           (try Some (List.assoc prog tools), true
124            with Not_found -> None, false) in
125
126   (* Make a usage message. *)
127   let usage_msg =
128     match tool with
129     | None ->                           (* Generic usage message. *)
130         let tools = List.map (
131           fun (name, (_, summary, _, external_cmd, _, _, _, _)) ->
132             if external_cmd then "virt-"^name, summary
133             else                 "virt-mem "^name, summary
134         ) tools in
135         (* Maximum width of field in the left hand column. *)
136         let max_width =
137           List.fold_left max 0 (List.map String.length (List.map fst tools)) in
138         let tools = List.map (fun (l,r) -> pad max_width l, r) tools in
139         let tools = List.map (fun (l,r) -> "  " ^ l ^ " - " ^ r) tools in
140         let tools = String.concat "\n" tools in
141
142         sprintf (f_"\
143
144 virt-mem: Tools for providing information about virtual machines
145
146 Currently available tools include:
147 %s
148
149 General usage is:
150   <tool> [-options] [domains...]
151
152 To display extra help for a single tool, do:
153   virt-mem help <tool>
154
155 Options:") tools
156
157                                         (* Tool-specific usage message. *)
158     | Some (name, summary, description, external_cmd, _, _, _, _) ->
159         let cmd =
160           if external_cmd then "virt-" ^ name else "virt-mem " ^ name in
161
162         sprintf (f_"\
163
164 %s: %s
165
166 Description:
167 %s
168
169 Options:") cmd summary description in
170
171   (* Now begin proper parsing of the command line arguments. *)
172   let debug = ref false in
173   let images = ref [] in
174   let uri = ref "" in
175   let anon_args = ref [] in
176
177   (* Default wordsize. *)
178   let def_wordsize = ref None in
179   let set_wordsize = function
180     | "32" -> def_wordsize := Some W32
181     | "64" -> def_wordsize := Some W64
182     | "auto" -> def_wordsize := None
183     | str -> failwith (sprintf (f_"set_wordsize: %s: unknown wordsize") str)
184   in
185
186   (* Default endianness. *)
187   let def_endian = ref None in
188   let set_endian = function
189     | "auto" -> def_endian := None
190     | "le" | "little" | "littleendian" | "intel" ->
191         def_endian := Some Bitstring.LittleEndian
192     | "be" | "big" | "bigendian" | "motorola" ->
193         def_endian := Some Bitstring.BigEndian
194     | str -> failwith (sprintf (f_"set_endian: %s: unknown endianness") str)
195   in
196
197   (* Default architecture. *)
198   let def_architecture = ref None in
199   let set_architecture = function
200     | "auto" -> def_architecture := None
201     | arch ->
202         let arch = architecture_of_string arch in
203         def_architecture := Some arch;
204         def_endian := Some (endian_of_architecture arch);
205         def_wordsize := Some (wordsize_of_architecture arch)
206   in
207
208   (* Default text address. *)
209   let def_text_addr = ref 0L (* 0 = auto-detect *) in
210   let set_text_addr = function
211     | "auto" -> def_text_addr := 0L
212     | "i386" -> def_text_addr := 0xc010_0000_L (* common for x86 *)
213     | "x86-64"|"x86_64" -> def_text_addr := 0xffffffff_81000000_L (* x86-64? *)
214     | str -> def_text_addr := Int64.of_string str
215   in
216
217   (* Handle -t option. *)
218   let memory_image filename =
219     images :=
220       (!def_wordsize, !def_endian, !def_architecture, !def_text_addr, filename)
221     :: !images
222   in
223
224   (* Handle --version option. *)
225   let version () =
226     printf "virt-mem %s\n" Virt_mem_version.version;
227
228     let major, minor, release =
229       let v, _ = Libvirt.get_version () in
230       v / 1_000_000, (v / 1_000) mod 1_000, v mod 1_000 in
231     printf "libvirt %d.%d.%d\n" major minor release;
232     exit 0
233   in
234
235   (* Function to collect up any anonymous args (domain names/IDs). *)
236   let anon_arg str = anon_args := str :: !anon_args in
237
238   (* Construct the argspec.
239    * May include extra arguments specified by the tool.
240    *)
241   let argspec =
242     let extra_args = match tool with
243       | None -> []
244       | Some (_, _, _, _, extra_args, _, _, _) -> extra_args in
245     let argspec = [
246       "-A", Arg.String set_architecture,
247         "arch " ^ s_"Set kernel architecture, endianness and word size";
248       "-E", Arg.String set_endian,
249         "endian " ^ s_"Set kernel endianness";
250       "-T", Arg.String set_text_addr,
251         "addr " ^ s_"Set kernel text address";
252       "-W", Arg.String set_wordsize,
253         "addr " ^ s_"Set kernel word size";
254       "-c", Arg.Set_string uri,
255         "uri " ^ s_ "Connect to URI";
256       "--connect", Arg.Set_string uri,
257         "uri " ^ s_ "Connect to URI";
258       "--debug", Arg.Set debug,
259         " " ^ s_"Debug mode (default: false)";
260       "-t", Arg.String memory_image,
261         "image " ^ s_"Use saved kernel memory image";
262       "--version", Arg.Unit version,
263         " " ^ s_"Display version and exit";
264     ] @ extra_args in
265
266     (* Sort options alphabetically on first alpha character. *)
267     let cmp (a,_,_) (b,_,_) =
268       let chars = "-" in
269       let a = String.strip ~chars a and b = String.strip ~chars b in
270       compare a b
271     in
272     let argspec = List.sort ~cmp argspec in
273     (* Make the options line up nicely. *)
274     Arg.align argspec in
275
276   (* Parse the command line.  This will exit if --version or --help found. *)
277   Arg.parse argspec anon_arg usage_msg;
278
279   let images = !images in
280   let debug = !debug in
281   let uri = if !uri = "" then None else Some !uri in
282
283   (* Discard the first anonymous argument if, above, we previously
284    * found it contained the tool name.
285    *)
286   let anon_args = List.rev !anon_args in
287   let anon_args =
288     if ignore_first_anon_arg then List.tl anon_args else anon_args in
289
290   (* At this point, either --help was specified on the command line
291    * (and so the program has exited) or we must have determined tool,
292    * or the user didn't give us a valid tool (eg. "virt-mem foobar").
293    * Detect that final case now and give an error.
294    *)
295   let name, _, _, _, _, argcheck, beforeksyms, run =
296     match tool with
297     | Some t -> t
298     | None ->
299         prerr_endline (s_"\
300 virt-mem: I could not work out which tool you are trying to run.
301 Use 'virt-mem --help' for more help or read the manual page virt-mem(1)");
302         exit 1
303   in
304   if debug then eprintf "tool = %s\n%!" name;
305
306   (* Optional argument checking in the tool. *)
307   (match argcheck with
308    | None -> ()
309    | Some argcheck -> argcheck debug
310   );
311
312   (* Get the kernel images. *)
313   let images =
314     if images = [] then (
315       let conn =
316         let name = uri in
317         try C.connect_readonly ?name ()
318         with Libvirt.Virterror err ->
319           prerr_endline (Libvirt.Virterror.to_string err);
320           (* If non-root and no explicit connection URI, print a warning. *)
321           if Unix.geteuid () <> 0 && name = None then (
322             print_endline (s_ "NB: If you want to monitor a local Xen hypervisor, you usually need to be root");
323           );
324           exit 1 in
325
326       (* If we have a list of parameters, then it is the domain names / UUIDs /
327        * IDs ONLY that we wish to display.  Otherwise, display all active.
328        *)
329       let doms =
330         if anon_args = [] then (
331           (* List of active domains. *)
332           let nr_active_doms = C.num_of_domains conn in
333           let active_doms =
334             Array.to_list (C.list_domains conn nr_active_doms) in
335           List.map (D.lookup_by_id conn) active_doms
336         ) else (
337           List.map (
338             fun arg ->
339               let dom =
340                 try D.lookup_by_uuid_string conn arg
341                 with _ ->
342                   try D.lookup_by_name conn arg
343                   with _ ->
344                     try D.lookup_by_id conn (int_of_string arg)
345                     with _ ->
346                       failwith (sprintf (f_"%s: unknown domain (not a UUID, name or ID of any active domain)") arg) in
347
348               (* XXX Primitive test to see if the domain is active. *)
349               let is_active = try D.get_id dom >= 0 with _ -> false in
350               if not is_active then
351                 failwith (sprintf (f_"%s: domain is not running") arg);
352
353               dom
354           ) anon_args
355         ) in
356
357       (* Get their XML. *)
358       let xmls = List.map (fun dom -> dom, D.get_xml_desc dom) doms in
359
360       (* Parse the XML. *)
361       let xmls = List.map (fun (dom, xml) ->
362                              dom, Xml.parse_string xml) xmls in
363
364       (* XXX Do something with the XML XXX
365        * such as detecting arch, wordsize, endianness.
366        * XXXXXXXXXXXXXX
367        *
368        *
369        *
370        *)
371
372
373       List.map (
374         fun (dom, _) ->
375           let id = D.get_id dom in
376           let name = D.get_name dom in
377
378           let wordsize =
379             match !def_wordsize with
380             | None ->
381                 failwith
382                   (sprintf (f_"%s: use -W to define word size for this image")
383                      name);
384             | Some ws -> ws in
385           let endian =
386             match !def_endian with
387             | None ->
388                 failwith
389                   (sprintf (f_"%s: use -E to define endianness for this image")
390                      name);
391             | Some e -> e in
392
393           let arch =
394             match !def_architecture with
395             | Some I386 -> I386 | Some X86_64 -> X86_64
396             | _ ->
397                 failwith
398                   (sprintf (f_"%s: use -A to define architecture (i386/x86-64 only) for this image") name) in
399
400           if !def_text_addr = 0L then
401             failwith
402               (sprintf (f_"%s: use -T to define kernel load address for this image") name);
403
404           let start_t = gettimeofday () in
405
406           (* Read the kernel memory.
407            * Maximum 64K can be read over remote connections.
408            *)
409           let str = String.create kernel_size in
410           let rec loop i =
411             let remaining = kernel_size - i in
412             if remaining > 0 then (
413               let size = min remaining max_memory_peek in
414               D.memory_peek dom [D.Virtual]
415                 (!def_text_addr +^ Int64.of_int i) size str i;
416               loop (i + size)
417             )
418           in
419           loop 0;
420
421           if debug then (
422             let end_t = gettimeofday () in
423             eprintf "timing: downloading kernel took %f seconds\n%!"
424               (end_t -. start_t)
425           );
426
427           (* Map the virtual memory. *)
428           let mem = MMap.of_string str !def_text_addr in
429
430           (* Force the wordsize and endianness. *)
431           let mem = MMap.set_wordsize mem wordsize in
432           let mem = MMap.set_endian mem endian in
433
434           ((Some id, name, arch, mem) : image)
435       ) xmls
436     ) else (
437       (* One or more -t options passed. *)
438       if anon_args <> [] then
439         failwith (s_"virt-mem: if -t given on command line, then no domain arguments should be listed");
440
441       List.map (
442         fun (wordsize, endian, arch, text_addr, filename) ->
443           (* Quite a lot of limitations on the kernel images we can
444            * handle at the moment ...
445            *)
446           (* XXX We could auto-detect wordsize easily. *)
447           let wordsize =
448             match wordsize with
449             | None ->
450                 failwith
451                   (sprintf (f_"%s: use -W to define word size for this image")
452                      filename);
453             | Some ws -> ws in
454           let endian =
455             match endian with
456             | None ->
457                 failwith
458                   (sprintf (f_"%s: use -E to define endianness for this image")
459                      filename);
460             | Some e -> e in
461
462           let arch =
463             match arch with
464             | Some I386 -> I386 | Some X86_64 -> X86_64
465             | _ ->
466                 failwith
467                   (sprintf (f_"%s: use -A to define architecture (i386/x86-64 only) for this image") filename) in
468
469           if text_addr = 0L then
470             failwith
471               (sprintf (f_"%s: use -T to define kernel load address for this image")
472                  filename);
473
474           (* Map the virtual memory. *)
475           let fd = openfile filename [O_RDONLY] 0 in
476           let mem = MMap.of_file fd text_addr in
477
478           (* Force the wordsize and endianness. *)
479           let mem = MMap.set_wordsize mem wordsize in
480           let mem = MMap.set_endian mem endian in
481
482           ((None, filename, arch, mem) : image)
483       ) images
484     ) in
485
486   (* Optional callback into the tool before we start looking for
487    * kernel symbols.
488    *)
489   (match beforeksyms with
490    | None -> ()
491    | Some beforeksyms -> beforeksyms debug images
492   );
493
494   (* If there is no run function, then there is no point continuing
495    * with the rest of the program (kernel symbol analysis) ...
496    *)
497   if run = None then exit 0;
498
499   (* Now kernel symbol analysis starts ... *)
500   let images =
501     List.map (
502       fun (domid, name, arch, mem) ->
503         (* Look for some common entries in the exported symbol table and
504          * from that find the symbol table itself.  These are just
505          * supposed to be symbols which are very likely to be present
506          * in any Linux kernel, although we only need one of them to be
507          * present to find the symbol table.
508          *
509          * NB. Must not be __initdata, must be in EXPORT_SYMBOL.
510          *)
511         let common_ksyms = [
512           "init_task";                  (* first task_struct *)
513           "root_mountflags";            (* flags for mounting root fs *)
514           "init_uts_ns";                (* uname strings *)
515           "sys_open";                   (* open(2) entry point *)
516           "sys_chdir";                  (* chdir(2) entry point *)
517           "sys_chroot";                 (* chroot(2) entry point *)
518           "sys_umask";                  (* umask(2) entry point *)
519           "schedule";                   (* scheduler entry point *)
520         ] in
521         (* Searching for <NUL>string<NUL> *)
522         let common_ksyms_nul = List.map (sprintf "\000%s\000") common_ksyms in
523
524         let start_t = gettimeofday () in
525
526         (* Search for these strings in the memory image. *)
527         let ksym_strings = List.map (MMap.find_all mem) common_ksyms_nul in
528         let ksym_strings = List.concat ksym_strings in
529         (* Adjust found addresses to start of the string (skip <NUL>). *)
530         let ksym_strings = List.map Int64.succ ksym_strings in
531
532         if debug then (
533           let end_t = gettimeofday () in
534           eprintf "timing: searching for common_ksyms took %f seconds\n%!"
535             (end_t -. start_t)
536         );
537
538         let start_t = gettimeofday () in
539
540         (* For any we found, try to look up the symbol table
541          * base addr and size.
542          *)
543         let ksymtabs = List.map (
544           fun addr ->
545             (* Search for 'addr' appearing in the image. *)
546             let addrs = MMap.find_pointer_all mem addr in
547
548             (* Now consider each of these addresses and search back
549              * until we reach the beginning of the (possible) symbol
550              * table.
551              *
552              * Kernel symbol table struct is:
553              * struct kernel_symbol {
554              *   unsigned long value;
555              *   const char *name;    <-- initial pointer
556              * } symbols[];
557              *)
558             let pred_long2 addr =
559               MMap.pred_long mem (MMap.pred_long mem addr)
560             in
561             let base_addrs = List.map (
562               fun addr ->
563                 let rec loop addr =
564                   (* '*addr' should point to a C identifier.  If it does,
565                    * step backwards to the previous symbol table entry.
566                    *)
567                   let addrp = MMap.follow_pointer mem addr in
568                   if MMap.is_C_identifier mem addrp then
569                     loop (pred_long2 addr)
570                   else
571                     MMap.succ_long mem addr
572                 in
573                 loop addr
574             ) addrs in
575
576             (* Also look for the end of the symbol table and
577              * calculate its size.
578              *)
579             let base_addrs_sizes = List.map (
580               fun base_addr ->
581                 let rec loop addr =
582                   let addr2 = MMap.succ_long mem addr in
583                   let addr2p = MMap.follow_pointer mem addr2 in
584                   if MMap.is_C_identifier mem addr2p then
585                     loop (MMap.succ_long mem addr2)
586                   else
587                     addr
588                 in
589                 let end_addr = loop base_addr in
590                 base_addr, end_addr -^ base_addr
591             ) base_addrs in
592
593             base_addrs_sizes
594         ) ksym_strings in
595         let ksymtabs = List.concat ksymtabs in
596
597         (* Simply ignore any symbol table candidates which are too small. *)
598         let ksymtabs = List.filter (fun (_, size) -> size > 64L) ksymtabs in
599
600         if debug then (
601           eprintf "%s: candidate symbol tables at:\n" name;
602           List.iter (
603             fun (addr, size) ->
604               eprintf "\t%Lx\t%Lx\t%!" addr size;
605               eprintf "first symbol: %s\n%!"
606                 (MMap.get_string mem
607                    (MMap.follow_pointer mem
608                       (MMap.succ_long mem addr)))
609           ) ksymtabs
610         );
611
612         (* Vote for the most popular symbol table candidate and from this
613          * generate a function to look up ksyms.
614          *)
615         let lookup_ksym =
616           let freqs = frequency ksymtabs in
617           match freqs with
618           | [] ->
619               eprintf (f_"%s: cannot find start of kernel symbol table\n") name;
620               (fun _ -> raise Not_found)
621
622           | (_, (ksymtab_addr, ksymtab_size)) :: _ ->
623               if debug then
624                 eprintf
625                   "%s: Kernel symbol table found at %Lx, size %Lx bytes\n%!"
626                   name ksymtab_addr ksymtab_size;
627
628               (* Load the whole symbol table as a bitstring. *)
629               let ksymtab =
630                 Bitstring.bitstring_of_string
631                   (MMap.get_bytes mem ksymtab_addr
632                      (Int64.to_int ksymtab_size)) in
633
634               (* Function to look up an address in the symbol table. *)
635               let lookup_ksym sym =
636                 let bits = bits_of_wordsize (MMap.get_wordsize mem) in
637                 let e = MMap.get_endian mem in
638                 let rec loop bs =
639                   bitmatch bs with
640                   | { value : bits : endian(e);
641                       name_ptr : bits : endian(e) }
642                       when MMap.get_string mem name_ptr = sym ->
643                       value
644                   | { _ : bits : endian(e);
645                       _ : bits : endian(e);
646                       bs : -1 : bitstring } ->
647                       loop bs
648                   | { _ } -> raise Not_found
649                 in
650                 loop ksymtab
651               in
652
653               lookup_ksym
654         in
655
656         if debug then (
657           let end_t = gettimeofday () in
658           eprintf "timing: searching for ordinary ksyms took %f seconds\n%!"
659             (end_t -. start_t)
660         );
661
662         let start_t = gettimeofday () in
663
664         (* Now try to find the /proc/kallsyms table.  This is in an odd
665          * compressed format (but not a very successful compression
666          * format).  However if it exists we know that it will contain
667          * addresses of the common ksyms above, and it has some
668          * characteristics which make it easy to detect in the
669          * memory.
670          *
671          * kallsyms contains a complete list of symbols so is much
672          * more useful than the basic list of exports.
673          *)
674         let ksym_addrs = List.filter_map (
675           fun ksym -> try Some (lookup_ksym ksym) with Not_found -> None
676         ) common_ksyms in
677
678         (* Search for those kernel addresses in the image.  We're looking
679          * for the table kallsyms_addresses followed by kallsyms_num_syms
680          * (number of symbols in the table).
681          *)
682         let ksym_addrs = List.map (MMap.find_pointer_all mem) ksym_addrs in
683         let ksym_addrs = List.concat ksym_addrs in
684
685         (* Test each one to see if it's a candidate list of kernel
686          * addresses followed by length of list.
687          *)
688         let kallsymtabs = List.filter_map (
689           fun addr ->
690             (* Search upwards from address until we find the length field.
691              * If found, jump backwards by length and check all addresses.
692              *)
693             if debug then
694               eprintf "%s: testing candidate kallsyms at %Lx\n" name addr;
695             let rec loop addr =
696               let addrp = MMap.follow_pointer mem addr in
697               if MMap.is_mapped mem addrp then
698                 loop (MMap.succ_long mem addr) (* continue up the table *)
699               else
700                 if addrp >= min_kallsyms_tabsize &&
701                   addrp <= max_kallsyms_tabsize then (
702                   (* addrp might be the symbol count.  Count backwards and
703                    * check the full table.
704                    *)
705                   let num_entries = Int64.to_int addrp in
706                   let entry_size = bytes_of_wordsize (MMap.get_wordsize mem) in
707                   let start_addr =
708                     addr -^ Int64.of_int (entry_size * num_entries) in
709                   let end_addr = addr in
710                   let rec loop2 addr =
711                     if addr < end_addr then (
712                       let addrp = MMap.follow_pointer mem addr in
713                       if MMap.is_mapped mem addrp then
714                         loop2 (MMap.succ_long mem addr)
715                       else
716                         None (* can't verify the full address table *)
717                     ) else
718                       (* ok! *)
719                       let names_addr = MMap.succ_long mem end_addr in
720                       if debug then
721                         eprintf "%s: candidate kallsyms found at %Lx (names_addr at %Lx, num_entries %d)\n"
722                           name start_addr names_addr num_entries;
723                       Some (start_addr, num_entries, names_addr)
724                   in
725                   loop2 start_addr
726                 )
727                 else
728                   None (* forget it *)
729             in
730             match loop addr with
731             | None -> None
732             | Some (start_addr, num_entries, names_addr) ->
733                 (* As an additional verification, check the list of
734                  * kallsyms_names.
735                  *)
736                 try
737                   (* If the first byte is '\000' and is followed by a
738                    * C identifier, then this is old-school list of
739                    * symbols with prefix compression as in 2.6.9.
740                    * Otherwise Huffman-compressed kallsyms as in
741                    * 2.6.25.
742                    *)
743                   if MMap.get_byte mem names_addr = 0 &&
744                     MMap.is_C_identifier mem (names_addr+^1L) then (
745                     let names = ref [] in
746                     let prev = ref "" in
747                     let rec loop names_addr start_addr num =
748                       if num > 0 then (
749                         let prefix = MMap.get_byte mem names_addr in
750                         let prefix = String.sub !prev 0 prefix in
751                         let name = MMap.get_string mem (names_addr+^1L) in
752                         let len = String.length name in
753                         let name = prefix ^ name in
754                         prev := name;
755                         let names_addr = names_addr +^ Int64.of_int len +^ 2L in
756                         let sym_value = MMap.follow_pointer mem start_addr in
757                         let start_addr = MMap.succ_long mem start_addr in
758                         (*eprintf "%S -> %Lx\n" name sym_value;*)
759                         names := (name, sym_value) :: !names;
760                         loop names_addr start_addr (num-1)
761                       )
762                     in
763                     loop names_addr start_addr num_entries;
764                     let names = List.rev !names in
765
766                     Some (start_addr, num_entries, names_addr,
767                           Uncompressed names)
768                     )
769                   else ( (* new-style "compressed" names. *)
770                     let compressed_names = ref [] in
771                     let rec loop names_addr start_addr num =
772                       if num > 0 then (
773                         let len = MMap.get_byte mem names_addr in
774                         let name = MMap.get_bytes mem (names_addr+^1L) len in
775                         let names_addr = names_addr +^ Int64.of_int len +^ 1L in
776                         let sym_value = MMap.follow_pointer mem start_addr in
777                         let start_addr = MMap.succ_long mem start_addr in
778                         compressed_names :=
779                           (name, sym_value) :: !compressed_names;
780                         loop names_addr start_addr (num-1)
781                       ) else
782                         names_addr
783                     in
784                     let markers_addr = loop names_addr start_addr num_entries in
785                     let markers_addr = MMap.align mem markers_addr in
786                     let compressed_names = List.rev !compressed_names in
787
788                     Some (start_addr, num_entries, names_addr,
789                           Compressed (compressed_names, markers_addr))
790                   )
791                 with
792                   Invalid_argument _ -> None (* bad names list *)
793         ) ksym_addrs in
794
795         if debug then (
796           eprintf "%s: candidate kallsyms at:\n" name;
797           List.iter (
798             function
799             | (start_addr, num_entries, names_addr, Uncompressed _) ->
800                 eprintf "\t%Lx %d entries names_addr=%Lx old-style\n%!"
801                   start_addr num_entries names_addr
802             | (start_addr, num_entries, names_addr,
803                Compressed (_, markers_addr)) ->
804                 eprintf "\t%Lx %d entries names_addr=%Lx markers_addr=%Lx\n%!"
805                   start_addr num_entries names_addr markers_addr
806           ) kallsymtabs
807         );
808
809         (* Vote for the most popular symbol table candidate and
810          * enhance the function for looking up ksyms.
811          *)
812         let lookup_ksym =
813           let freqs = frequency kallsymtabs in
814           match freqs with
815           | [] ->
816               (* Can't find any kallsymtabs, just return the lookup_ksym
817                * function generated previously from the exported symbols.
818                *)
819               lookup_ksym
820
821           | (_, (_, _, _, Uncompressed names)) :: _ ->
822               let lookup_ksym name =
823                 try (* first look it up in kallsyms table. *)
824                   List.assoc name names
825                 with Not_found -> (* try the old exports table instead *)
826                   lookup_ksym name
827               in
828               lookup_ksym
829
830           | (_, (start_addr, num_entries, names_addr,
831                  Compressed (compressed_names, markers_addr))) :: _ ->
832               (* Skip the markers and look for the token table. *)
833               let num_markers = Int64.of_int ((num_entries + 255) / 256) in
834               let marker_size =
835                 Int64.of_int (bytes_of_wordsize (MMap.get_wordsize mem)) in
836               let tokens_addr = markers_addr +^ marker_size *^ num_markers in
837
838               (* Now read out the compression tokens, which are just
839                * 256 ASCIIZ strings that map bytes in the compression
840                * names to substrings.
841                *)
842               let tokens = Array.make 256 "" in
843               let rec loop i addr =
844                 if i < 256 then (
845                   let str = MMap.get_string mem addr in
846                   let len = String.length str in
847                   let addr = addr +^ Int64.of_int (len+1) in
848                   tokens.(i) <- str;
849                   loop (i+1) addr
850                 )
851               in
852               loop 0 tokens_addr;
853
854               (* Expand the compressed names using the tokens. *)
855               let names = List.filter_map (
856                 fun (name, sym_value) ->
857                   let f c = tokens.(Char.code c) in
858                   let name = String.replace_chars f name in
859                   (* First character in uncompressed output is the symbol
860                    * type, eg. 'T'/'t' for text etc.
861                    *)
862                   (* NOTE: Symbol names are NOT unique
863                    * (eg. 'con_start' is both a function and data in
864                    * some kernels).  XXX We need to handle this situation
865                    * better.
866                    *)
867                   (*let typ = name.[0] in*)
868                   let name = String.sub name 1 (String.length name - 1) in
869                   (*eprintf "%S -> %Lx\n" name sym_value;*)
870                   Some (name, sym_value)
871               ) compressed_names in
872
873               let lookup_ksym name =
874                 try (* first look it up in kallsyms table. *)
875                   List.assoc name names
876                 with Not_found -> (* try the old exports table instead *)
877                   lookup_ksym name
878               in
879
880               lookup_ksym in
881
882         if debug then (
883           let end_t = gettimeofday () in
884           eprintf "timing: searching for kallsyms took %f seconds\n%!"
885             (end_t -. start_t)
886         );
887
888         (* Just wrap the lookup_ksym call in something which prints
889          * the query when debug is set.
890          *)
891         let lookup_ksym =
892           if debug then
893             let lookup_ksym sym =
894               try
895                 let value = lookup_ksym sym in
896                 eprintf "lookup_ksym %S = %Lx\n%!" sym value;
897                 value
898               with Not_found ->
899                 eprintf "lookup_ksym %S failed\n%!" sym;
900                 raise Not_found
901             in
902             lookup_ksym
903           else
904             lookup_ksym
905         in
906
907         ((domid, name, arch, mem, lookup_ksym) : image_with_ksyms)
908     ) images in
909
910   (* Run the tool's main function. *)
911   (match run with
912    | None -> ()
913    | Some run ->
914        run debug images
915   )