Make '--version' flag work.
[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 let kernel_size = 0x100_0000
36 let max_memory_peek = 0x1_000
37
38 type ksym = string
39
40 type image =
41     string
42     * Virt_mem_utils.architecture
43     * ([`Wordsize], [`Endian]) Virt_mem_mmap.t
44     * (ksym -> MMap.addr)
45
46 type kallsyms_compr =
47   | Compressed of (string * MMap.addr) list * MMap.addr
48   | Uncompressed of (string * MMap.addr) list
49
50 let start usage_msg =
51   (* Debug messages. *)
52   let debug = ref false in
53
54   (* Default wordsize. *)
55   let def_wordsize = ref None in
56   let set_wordsize = function
57     | "32" -> def_wordsize := Some W32
58     | "64" -> def_wordsize := Some W64
59     | "auto" -> def_wordsize := None
60     | str -> failwith (sprintf (f_"set_wordsize: %s: unknown wordsize") str)
61   in
62
63   (* Default endianness. *)
64   let def_endian = ref None in
65   let set_endian = function
66     | "auto" -> def_endian := None
67     | "le" | "little" | "littleendian" | "intel" ->
68         def_endian := Some Bitmatch.LittleEndian
69     | "be" | "big" | "bigendian" | "motorola" ->
70         def_endian := Some Bitmatch.BigEndian
71     | str -> failwith (sprintf (f_"set_endian: %s: unknown endianness") str)
72   in
73
74   (* Default architecture. *)
75   let def_architecture = ref None in
76   let set_architecture = function
77     | "auto" -> def_architecture := None
78     | arch ->
79         let arch = architecture_of_string arch in
80         def_architecture := Some arch;
81         def_endian := Some (endian_of_architecture arch);
82         def_wordsize := Some (wordsize_of_architecture arch)
83   in
84
85   (* Default text address. *)
86   let def_text_addr = ref 0L (* 0 = auto-detect *) in
87   let set_text_addr = function
88     | "auto" -> def_text_addr := 0L
89     | "i386" -> def_text_addr := 0xc010_0000_L (* common for x86 *)
90     | "x86-64"|"x86_64" -> def_text_addr := 0xffffffff_81000000_L (* x86-64? *)
91     | str -> def_text_addr := Int64.of_string str
92   in
93
94   (* List of kernel images. *)
95   let images = ref [] in
96   let uri = ref "" in
97
98   let memory_image filename =
99     images :=
100       (!def_wordsize, !def_endian, !def_architecture, !def_text_addr, filename)
101     :: !images
102   in
103
104   let version () =
105     printf "virt-mem %s\n" Virt_mem_version.version;
106
107     let major, minor, release =
108       let v, _ = Libvirt.get_version () in
109       v / 1_000_000, (v / 1_000) mod 1_000, v mod 1_000 in
110     printf "libvirt %d.%d.%d\n" major minor release;
111     exit 0
112   in
113
114   let argspec = Arg.align [
115     "-A", Arg.String set_architecture,
116       "arch " ^ s_"Set kernel architecture, endianness and word size";
117     "-E", Arg.String set_endian,
118       "endian " ^ s_"Set kernel endianness";
119     "-T", Arg.String set_text_addr,
120       "addr " ^ s_"Set kernel text address";
121     "-W", Arg.String set_wordsize,
122       "addr " ^ s_"Set kernel word size";
123     "-c", Arg.Set_string uri,
124       "uri " ^ s_ "Connect to URI";
125     "--connect", Arg.Set_string uri,
126       "uri " ^ s_ "Connect to URI";
127     "--debug", Arg.Set debug,
128       " " ^ s_"Debug mode (default: false)";
129     "-t", Arg.String memory_image,
130       "image " ^ s_"Use saved kernel memory image";
131     "--version", Arg.Unit version,
132       " " ^ s_"Display version and exit";
133   ] in
134
135   let anon_fun str =
136     raise (Arg.Bad (sprintf (f_"%s: unknown parameter") str)) in
137   let usage_msg = usage_msg ^ s_"\n\nOPTIONS" in
138   Arg.parse argspec anon_fun usage_msg;
139
140   let images = !images in
141   let debug = !debug in
142   let uri = if !uri = "" then None else Some !uri in
143
144   (* Get the kernel images. *)
145   let images =
146     if images = [] then (
147       let conn =
148         let name = uri in
149         try C.connect_readonly ?name ()
150         with Libvirt.Virterror err ->
151           prerr_endline (Libvirt.Virterror.to_string err);
152           (* If non-root and no explicit connection URI, print a warning. *)
153           if Unix.geteuid () <> 0 && name = None then (
154             print_endline (s_ "NB: If you want to monitor a local Xen hypervisor, you usually need to be root");
155           );
156           exit 1 in
157
158       (* List of active domains. *)
159       let doms =
160         let nr_active_doms = C.num_of_domains conn in
161         let active_doms =
162           Array.to_list (C.list_domains conn nr_active_doms) in
163         let active_doms =
164           List.map (D.lookup_by_id conn) active_doms in
165         active_doms in
166
167       (* Get their XML. *)
168       let xmls = List.map (fun dom -> dom, D.get_xml_desc dom) doms in
169
170       (* Parse the XML. *)
171       let xmls = List.map (fun (dom, xml) ->
172                              dom, Xml.parse_string xml) xmls in
173
174       (* XXX Do something with the XML XXX
175        * such as detecting arch, wordsize, endianness.
176        * XXXXXXXXXXXXXX
177        *
178        *
179        *
180        *)
181
182
183       List.map (
184         fun (dom, _) ->
185           let name = D.get_name dom in
186
187           let wordsize =
188             match !def_wordsize with
189             | None ->
190                 failwith
191                   (sprintf (f_"%s: use -W to define word size for this image")
192                      name);
193             | Some ws -> ws in
194           let endian =
195             match !def_endian with
196             | None ->
197                 failwith
198                   (sprintf (f_"%s: use -E to define endianness for this image")
199                      name);
200             | Some e -> e in
201
202           let arch =
203             match !def_architecture with
204             | Some I386 -> I386 | Some X86_64 -> X86_64
205             | _ ->
206                 failwith
207                   (sprintf (f_"%s: use -A to define architecture (i386/x86-64 only) for this image") name) in
208
209           if !def_text_addr = 0L then
210             failwith
211               (sprintf (f_"%s: use -T to define kernel load address for this image")
212                  name);
213
214           (* Read the kernel memory.
215            * Maximum 64K can be read over remote connections.
216            *)
217           let str = String.create kernel_size in
218           let rec loop i =
219             let remaining = kernel_size - i in
220             if remaining > 0 then (
221               let size = min remaining max_memory_peek in
222               D.memory_peek dom [D.Virtual]
223                 (!def_text_addr +^ Int64.of_int i) size str i;
224               loop (i + size)
225             )
226           in
227           loop 0;
228
229           (* Map the virtual memory. *)
230           let mem = MMap.of_string str !def_text_addr in
231
232           (* Force the wordsize and endianness. *)
233           let mem = MMap.set_wordsize mem wordsize in
234           let mem = MMap.set_endian mem endian in
235
236           (name, arch, mem)
237       ) xmls
238     ) else
239       List.map (
240         fun (wordsize, endian, arch, text_addr, filename) ->
241           (* Quite a lot of limitations on the kernel images we can
242            * handle at the moment ...
243            *)
244           (* XXX We could auto-detect wordsize easily. *)
245           let wordsize =
246             match wordsize with
247             | None ->
248                 failwith
249                   (sprintf (f_"%s: use -W to define word size for this image")
250                      filename);
251             | Some ws -> ws in
252           let endian =
253             match endian with
254             | None ->
255                 failwith
256                   (sprintf (f_"%s: use -E to define endianness for this image")
257                      filename);
258             | Some e -> e in
259
260           let arch =
261             match arch with
262             | Some I386 -> I386 | Some X86_64 -> X86_64
263             | _ ->
264                 failwith
265                   (sprintf (f_"%s: use -A to define architecture (i386/x86-64 only) for this image") filename) in
266
267           if text_addr = 0L then
268             failwith
269               (sprintf (f_"%s: use -T to define kernel load address for this image")
270                  filename);
271
272           (* Map the virtual memory. *)
273           let fd = openfile filename [O_RDONLY] 0 in
274           let mem = MMap.of_file fd text_addr in
275
276           (* Force the wordsize and endianness. *)
277           let mem = MMap.set_wordsize mem wordsize in
278           let mem = MMap.set_endian mem endian in
279
280           (filename, arch, mem)
281       ) images in
282
283   let images =
284     List.map (
285       fun (name, arch, mem) ->
286         (* Look for some common entries in the exported symbol table and
287          * from that find the symbol table itself.  These are just
288          * supposed to be symbols which are very likely to be present
289          * in any Linux kernel, although we only need one of them to be
290          * present to find the symbol table.
291          *
292          * NB. Must not be __initdata, must be in EXPORT_SYMBOL.
293          *)
294         let common_ksyms = [
295           "init_task";                  (* first task_struct *)
296           "root_mountflags";            (* flags for mounting root fs *)
297           "init_uts_ns";                (* uname strings *)
298           "sys_open";                   (* open(2) entry point *)
299           "sys_chdir";                  (* chdir(2) entry point *)
300           "sys_chroot";                 (* chroot(2) entry point *)
301           "sys_umask";                  (* umask(2) entry point *)
302           "schedule";                   (* scheduler entry point *)
303         ] in
304         (* Searching for <NUL>string<NUL> *)
305         let common_ksyms_nul = List.map (sprintf "\000%s\000") common_ksyms in
306
307         (* Search for these strings in the memory image. *)
308         let ksym_strings = List.map (MMap.find_all mem) common_ksyms_nul in
309         let ksym_strings = List.concat ksym_strings in
310         (* Adjust found addresses to start of the string (skip <NUL>). *)
311         let ksym_strings = List.map Int64.succ ksym_strings in
312
313         (* For any we found, try to look up the symbol table
314          * base addr and size.
315          *)
316         let ksymtabs = List.map (
317           fun addr ->
318             (* Search for 'addr' appearing in the image. *)
319             let addrs = MMap.find_pointer_all mem addr in
320
321             (* Now consider each of these addresses and search back
322              * until we reach the beginning of the (possible) symbol
323              * table.
324              *
325              * Kernel symbol table struct is:
326              * struct kernel_symbol {
327              *   unsigned long value;
328              *   const char *name;    <-- initial pointer
329              * } symbols[];
330              *)
331             let pred_long2 addr =
332               MMap.pred_long mem (MMap.pred_long mem addr)
333             in
334             let base_addrs = List.map (
335               fun addr ->
336                 let rec loop addr =
337                   (* '*addr' should point to a C identifier.  If it does,
338                    * step backwards to the previous symbol table entry.
339                    *)
340                   let addrp = MMap.follow_pointer mem addr in
341                   if MMap.is_C_identifier mem addrp then
342                     loop (pred_long2 addr)
343                   else
344                     MMap.succ_long mem addr
345                 in
346                 loop addr
347             ) addrs in
348
349             (* Also look for the end of the symbol table and
350              * calculate its size.
351              *)
352             let base_addrs_sizes = List.map (
353               fun base_addr ->
354                 let rec loop addr =
355                   let addr2 = MMap.succ_long mem addr in
356                   let addr2p = MMap.follow_pointer mem addr2 in
357                   if MMap.is_C_identifier mem addr2p then
358                     loop (MMap.succ_long mem addr2)
359                   else
360                     addr
361                 in
362                 let end_addr = loop base_addr in
363                 base_addr, end_addr -^ base_addr
364             ) base_addrs in
365
366             base_addrs_sizes
367         ) ksym_strings in
368         let ksymtabs = List.concat ksymtabs in
369
370         (* Simply ignore any symbol table candidates which are too small. *)
371         let ksymtabs = List.filter (fun (_, size) -> size > 64L) ksymtabs in
372
373         if debug then (
374           printf "%s: candidate symbol tables at:\n" name;
375           List.iter (
376             fun (addr, size) ->
377               printf "\t%Lx\t%Lx\t%!" addr size;
378               printf "first symbol: %s\n%!"
379                 (MMap.get_string mem
380                    (MMap.follow_pointer mem
381                       (MMap.succ_long mem addr)))
382           ) ksymtabs
383         );
384
385         (* Vote for the most popular symbol table candidate and from this
386          * generate a function to look up ksyms.
387          *)
388         let lookup_ksym =
389           let freqs = frequency ksymtabs in
390           match freqs with
391           | [] ->
392               eprintf (f_"%s: cannot find start of kernel symbol table\n") name;
393               (fun _ -> raise Not_found)
394
395           | (_, (ksymtab_addr, ksymtab_size)) :: _ ->
396               if debug then
397                 printf
398                   "%s: Kernel symbol table found at %Lx, size %Lx bytes\n%!"
399                   name ksymtab_addr ksymtab_size;
400
401               (* Load the whole symbol table as a bitstring. *)
402               let ksymtab =
403                 Bitmatch.bitstring_of_string
404                   (MMap.get_bytes mem ksymtab_addr
405                      (Int64.to_int ksymtab_size)) in
406
407               (* Function to look up an address in the symbol table. *)
408               let lookup_ksym sym =
409                 let bits = bits_of_wordsize (MMap.get_wordsize mem) in
410                 let e = MMap.get_endian mem in
411                 let rec loop bs =
412                   bitmatch bs with
413                   | { value : bits : endian(e);
414                       name_ptr : bits : endian(e) }
415                       when MMap.get_string mem name_ptr = sym ->
416                       value
417                   | { _ : bits : endian(e);
418                       _ : bits : endian(e);
419                       bs : -1 : bitstring } ->
420                       loop bs
421                   | { _ } -> raise Not_found
422                 in
423                 loop ksymtab
424               in
425
426               lookup_ksym
427         in
428
429         (* Now try to find the /proc/kallsyms table.  This is in an odd
430          * compressed format (but not a very successful compression
431          * format).  However if it exists we know that it will contain
432          * addresses of the common ksyms above, and it has some
433          * characteristics which make it easy to detect in the
434          * memory.
435          *
436          * kallsyms contains a complete list of symbols so is much
437          * more useful than the basic list of exports.
438          *)
439         let ksym_addrs = List.filter_map (
440           fun ksym -> try Some (lookup_ksym ksym) with Not_found -> None
441         ) common_ksyms in
442
443         (* Search for those kernel addresses in the image.  We're looking
444          * for the table kallsyms_addresses followed by kallsyms_num_syms
445          * (number of symbols in the table).
446          *)
447         let ksym_addrs = List.map (MMap.find_pointer_all mem) ksym_addrs in
448         let ksym_addrs = List.concat ksym_addrs in
449
450         (* Test each one to see if it's a candidate list of kernel
451          * addresses followed by length of list.
452          *)
453         let kallsymtabs = List.filter_map (
454           fun addr ->
455             (* Search upwards from address until we find the length field.
456              * If found, jump backwards by length and check all addresses.
457              *)
458             if debug then
459               printf "%s: testing candidate kallsyms at %Lx\n" name addr;
460             let rec loop addr =
461               let addrp = MMap.follow_pointer mem addr in
462               if MMap.is_mapped mem addrp then
463                 loop (MMap.succ_long mem addr) (* continue up the table *)
464               else
465                 if addrp >= min_kallsyms_tabsize &&
466                   addrp <= max_kallsyms_tabsize then (
467                   (* addrp might be the symbol count.  Count backwards and
468                    * check the full table.
469                    *)
470                   let num_entries = Int64.to_int addrp in
471                   let entry_size = bytes_of_wordsize (MMap.get_wordsize mem) in
472                   let start_addr =
473                     addr -^ Int64.of_int (entry_size * num_entries) in
474                   let end_addr = addr in
475                   let rec loop2 addr =
476                     if addr < end_addr then (
477                       let addrp = MMap.follow_pointer mem addr in
478                       if MMap.is_mapped mem addrp then
479                         loop2 (MMap.succ_long mem addr)
480                       else
481                         None (* can't verify the full address table *)
482                     ) else
483                       (* ok! *)
484                       let names_addr = MMap.succ_long mem end_addr in
485                       if debug then
486                         printf "%s: candidate kallsyms found at %Lx (names_addr at %Lx, num_entries %d)\n"
487                           name start_addr names_addr num_entries;
488                       Some (start_addr, num_entries, names_addr)
489                   in
490                   loop2 start_addr
491                 )
492                 else
493                   None (* forget it *)
494             in
495             match loop addr with
496             | None -> None
497             | Some (start_addr, num_entries, names_addr) ->
498                 (* As an additional verification, check the list of
499                  * kallsyms_names.
500                  *)
501                 try
502                   (* If the first byte is '\000' and is followed by a
503                    * C identifier, then this is old-school list of
504                    * symbols with prefix compression as in 2.6.9.
505                    * Otherwise Huffman-compressed kallsyms as in
506                    * 2.6.25.
507                    *)
508                   if MMap.get_byte mem names_addr = 0 &&
509                     MMap.is_C_identifier mem (names_addr+^1L) then (
510                     let names = ref [] in
511                     let prev = ref "" in
512                     let rec loop names_addr start_addr num =
513                       if num > 0 then (
514                         let prefix = MMap.get_byte mem names_addr in
515                         let prefix = String.sub !prev 0 prefix in
516                         let name = MMap.get_string mem (names_addr+^1L) in
517                         let len = String.length name in
518                         let name = prefix ^ name in
519                         prev := name;
520                         let names_addr = names_addr +^ Int64.of_int len +^ 2L in
521                         let sym_value = MMap.follow_pointer mem start_addr in
522                         let start_addr = MMap.succ_long mem start_addr in
523                         (*printf "%S -> %Lx\n" name sym_value;*)
524                         names := (name, sym_value) :: !names;
525                         loop names_addr start_addr (num-1)
526                       )
527                     in
528                     loop names_addr start_addr num_entries;
529                     let names = List.rev !names in
530
531                     Some (start_addr, num_entries, names_addr,
532                           Uncompressed names)
533                     )
534                   else ( (* new-style "compressed" names. *)
535                     let compressed_names = ref [] in
536                     let rec loop names_addr start_addr num =
537                       if num > 0 then (
538                         let len = MMap.get_byte mem names_addr in
539                         let name = MMap.get_bytes mem (names_addr+^1L) len in
540                         let names_addr = names_addr +^ Int64.of_int len +^ 1L in
541                         let sym_value = MMap.follow_pointer mem start_addr in
542                         let start_addr = MMap.succ_long mem start_addr in
543                         compressed_names :=
544                           (name, sym_value) :: !compressed_names;
545                         loop names_addr start_addr (num-1)
546                       ) else
547                         names_addr
548                     in
549                     let markers_addr = loop names_addr start_addr num_entries in
550                     let markers_addr = MMap.align mem markers_addr in
551                     let compressed_names = List.rev !compressed_names in
552
553                     Some (start_addr, num_entries, names_addr,
554                           Compressed (compressed_names, markers_addr))
555                   )
556                 with
557                   Invalid_argument _ -> None (* bad names list *)
558         ) ksym_addrs in
559
560         if debug then (
561           printf "%s: candidate kallsyms at:\n" name;
562           List.iter (
563             function
564             | (start_addr, num_entries, names_addr, Uncompressed _) ->
565                 printf "\t%Lx %d entries names_addr=%Lx old-style\n%!"
566                   start_addr num_entries names_addr
567             | (start_addr, num_entries, names_addr,
568                Compressed (_, markers_addr)) ->
569                 printf "\t%Lx %d entries names_addr=%Lx markers_addr=%Lx\n%!"
570                   start_addr num_entries names_addr markers_addr
571           ) kallsymtabs
572         );
573
574         (* Vote for the most popular symbol table candidate and
575          * enhance the function for looking up ksyms.
576          *)
577         let lookup_ksym =
578           let freqs = frequency kallsymtabs in
579           match freqs with
580           | [] ->
581               (* Can't find any kallsymtabs, just return the lookup_ksym
582                * function generated previously from the exported symbols.
583                *)
584               lookup_ksym
585
586           | (_, (_, _, _, Uncompressed names)) :: _ ->
587               let lookup_ksym name =
588                 try (* first look it up in kallsyms table. *)
589                   List.assoc name names
590                 with Not_found -> (* try the old exports table instead *)
591                   lookup_ksym name
592               in
593               lookup_ksym
594
595           | (_, (start_addr, num_entries, names_addr,
596                  Compressed (compressed_names, markers_addr))) :: _ ->
597               (* Skip the markers and look for the token table. *)
598               let num_markers = Int64.of_int ((num_entries + 255) / 256) in
599               let marker_size =
600                 Int64.of_int (bytes_of_wordsize (MMap.get_wordsize mem)) in
601               let tokens_addr = markers_addr +^ marker_size *^ num_markers in
602
603               (* Now read out the compression tokens, which are just
604                * 256 ASCIIZ strings that map bytes in the compression
605                * names to substrings.
606                *)
607               let tokens = Array.make 256 "" in
608               let rec loop i addr =
609                 if i < 256 then (
610                   let str = MMap.get_string mem addr in
611                   let len = String.length str in
612                   let addr = addr +^ Int64.of_int (len+1) in
613                   tokens.(i) <- str;
614                   loop (i+1) addr
615                 )
616               in
617               loop 0 tokens_addr;
618
619               (* Expand the compressed names using the tokens. *)
620               let names = List.filter_map (
621                 fun (name, sym_value) ->
622                   let f c = tokens.(Char.code c) in
623                   let name = String.replace_chars f name in
624                   (* First character in uncompressed output is the symbol
625                    * type, eg. 'T'/'t' for text etc.
626                    *)
627                   (* NOTE: Symbol names are NOT unique
628                    * (eg. 'con_start' is both a function and data in
629                    * some kernels).  XXX We need to handle this situation
630                    * better.
631                    *)
632                   (*let typ = name.[0] in*)
633                   let name = String.sub name 1 (String.length name - 1) in
634                   (*printf "%S -> %Lx\n" name sym_value;*)
635                   Some (name, sym_value)
636               ) compressed_names in
637
638               let lookup_ksym name =
639                 try (* first look it up in kallsyms table. *)
640                   List.assoc name names
641                 with Not_found -> (* try the old exports table instead *)
642                   lookup_ksym name
643               in
644
645               lookup_ksym in
646
647         (* Just wrap the lookup_ksym call in something which prints
648          * the query when debug is set.
649          *)
650         let lookup_ksym =
651           if debug then
652             let lookup_ksym sym =
653               try
654                 let value = lookup_ksym sym in
655                 printf "lookup_ksym %S = %Lx\n%!" sym value;
656                 value
657               with Not_found ->
658                 printf "lookup_ksym %S failed\n%!" sym;
659                 raise Not_found
660             in
661             lookup_ksym
662           else
663             lookup_ksym
664         in
665
666         ((name, arch, mem, lookup_ksym) : image)
667     ) images in
668
669   debug, images