1 (* Memory info for virtual domains.
2 (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
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.
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.
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.
25 module C = Libvirt.Connect
26 module D = Libvirt.Domain
28 open Virt_mem_gettext.Gettext
30 module MMap = Virt_mem_mmap
32 let min_kallsyms_tabsize = 1_000L
33 let max_kallsyms_tabsize = 250_000L
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.
39 if Sys.word_size = 32 then Sys.max_string_length
41 let max_memory_peek = 0x1000
47 * Virt_mem_utils.architecture
48 * ([`Wordsize], [`Endian]) Virt_mem_mmap.t
52 | Compressed of (string * MMap.addr) list * MMap.addr
53 | Uncompressed of (string * MMap.addr) list
57 let debug = ref false in
59 (* Default wordsize. *)
60 let def_wordsize = ref None in
61 let set_wordsize = function
62 | "32" -> def_wordsize := Some W32
63 | "64" -> def_wordsize := Some W64
64 | "auto" -> def_wordsize := None
65 | str -> failwith (sprintf (f_"set_wordsize: %s: unknown wordsize") str)
68 (* Default endianness. *)
69 let def_endian = ref None in
70 let set_endian = function
71 | "auto" -> def_endian := None
72 | "le" | "little" | "littleendian" | "intel" ->
73 def_endian := Some Bitmatch.LittleEndian
74 | "be" | "big" | "bigendian" | "motorola" ->
75 def_endian := Some Bitmatch.BigEndian
76 | str -> failwith (sprintf (f_"set_endian: %s: unknown endianness") str)
79 (* Default architecture. *)
80 let def_architecture = ref None in
81 let set_architecture = function
82 | "auto" -> def_architecture := None
84 let arch = architecture_of_string arch in
85 def_architecture := Some arch;
86 def_endian := Some (endian_of_architecture arch);
87 def_wordsize := Some (wordsize_of_architecture arch)
90 (* Default text address. *)
91 let def_text_addr = ref 0L (* 0 = auto-detect *) in
92 let set_text_addr = function
93 | "auto" -> def_text_addr := 0L
94 | "i386" -> def_text_addr := 0xc010_0000_L (* common for x86 *)
95 | "x86-64"|"x86_64" -> def_text_addr := 0xffffffff_81000000_L (* x86-64? *)
96 | str -> def_text_addr := Int64.of_string str
99 (* List of kernel images. *)
100 let images = ref [] in
102 let anon_args = ref [] in
104 let memory_image filename =
106 (!def_wordsize, !def_endian, !def_architecture, !def_text_addr, filename)
111 printf "virt-mem %s\n" Virt_mem_version.version;
113 let major, minor, release =
114 let v, _ = Libvirt.get_version () in
115 v / 1_000_000, (v / 1_000) mod 1_000, v mod 1_000 in
116 printf "libvirt %d.%d.%d\n" major minor release;
120 let argspec = Arg.align [
121 "-A", Arg.String set_architecture,
122 "arch " ^ s_"Set kernel architecture, endianness and word size";
123 "-E", Arg.String set_endian,
124 "endian " ^ s_"Set kernel endianness";
125 "-T", Arg.String set_text_addr,
126 "addr " ^ s_"Set kernel text address";
127 "-W", Arg.String set_wordsize,
128 "addr " ^ s_"Set kernel word size";
129 "-c", Arg.Set_string uri,
130 "uri " ^ s_ "Connect to URI";
131 "--connect", Arg.Set_string uri,
132 "uri " ^ s_ "Connect to URI";
133 "--debug", Arg.Set debug,
134 " " ^ s_"Debug mode (default: false)";
135 "-t", Arg.String memory_image,
136 "image " ^ s_"Use saved kernel memory image";
137 "--version", Arg.Unit version,
138 " " ^ s_"Display version and exit";
141 let anon_arg str = anon_args := str :: !anon_args in
142 let usage_msg = usage_msg ^ s_"\n\nOPTIONS" in
143 Arg.parse argspec anon_arg usage_msg;
145 let images = !images in
146 let debug = !debug in
147 let uri = if !uri = "" then None else Some !uri in
148 let anon_args = List.rev !anon_args in
150 (* Get the kernel images. *)
152 if images = [] then (
155 try C.connect_readonly ?name ()
156 with Libvirt.Virterror err ->
157 prerr_endline (Libvirt.Virterror.to_string err);
158 (* If non-root and no explicit connection URI, print a warning. *)
159 if Unix.geteuid () <> 0 && name = None then (
160 print_endline (s_ "NB: If you want to monitor a local Xen hypervisor, you usually need to be root");
164 (* If we have a list of parameters, then it is the domain names / UUIDs /
165 * IDs ONLY that we wish to display. Otherwise, display all active.
168 if anon_args = [] then (
169 (* List of active domains. *)
170 let nr_active_doms = C.num_of_domains conn in
172 Array.to_list (C.list_domains conn nr_active_doms) in
173 List.map (D.lookup_by_id conn) active_doms
178 try D.lookup_by_uuid_string conn arg
180 try D.lookup_by_name conn arg
182 try D.lookup_by_id conn (int_of_string arg)
184 failwith (sprintf (f_"%s: unknown domain (not a UUID, name or ID of any active domain)") arg) in
186 (* XXX Primitive test to see if the domain is active. *)
187 let is_active = try D.get_id dom >= 0 with _ -> false in
188 if not is_active then
189 failwith (sprintf (f_"%s: domain is not running") arg);
196 let xmls = List.map (fun dom -> dom, D.get_xml_desc dom) doms in
199 let xmls = List.map (fun (dom, xml) ->
200 dom, Xml.parse_string xml) xmls in
202 (* XXX Do something with the XML XXX
203 * such as detecting arch, wordsize, endianness.
213 let name = D.get_name dom in
216 match !def_wordsize with
219 (sprintf (f_"%s: use -W to define word size for this image")
223 match !def_endian with
226 (sprintf (f_"%s: use -E to define endianness for this image")
231 match !def_architecture with
232 | Some I386 -> I386 | Some X86_64 -> X86_64
235 (sprintf (f_"%s: use -A to define architecture (i386/x86-64 only) for this image") name) in
237 if !def_text_addr = 0L then
239 (sprintf (f_"%s: use -T to define kernel load address for this image")
242 (* Read the kernel memory.
243 * Maximum 64K can be read over remote connections.
245 let str = String.create kernel_size in
247 let remaining = kernel_size - i in
248 if remaining > 0 then (
249 let size = min remaining max_memory_peek in
250 D.memory_peek dom [D.Virtual]
251 (!def_text_addr +^ Int64.of_int i) size str i;
257 (* Map the virtual memory. *)
258 let mem = MMap.of_string str !def_text_addr in
260 (* Force the wordsize and endianness. *)
261 let mem = MMap.set_wordsize mem wordsize in
262 let mem = MMap.set_endian mem endian in
267 (* One or more -t options passed. *)
268 if anon_args <> [] then
269 failwith (s_"virt-mem: if -t given on command line, then no domain arguments should be listed");
272 fun (wordsize, endian, arch, text_addr, filename) ->
273 (* Quite a lot of limitations on the kernel images we can
274 * handle at the moment ...
276 (* XXX We could auto-detect wordsize easily. *)
281 (sprintf (f_"%s: use -W to define word size for this image")
288 (sprintf (f_"%s: use -E to define endianness for this image")
294 | Some I386 -> I386 | Some X86_64 -> X86_64
297 (sprintf (f_"%s: use -A to define architecture (i386/x86-64 only) for this image") filename) in
299 if text_addr = 0L then
301 (sprintf (f_"%s: use -T to define kernel load address for this image")
304 (* Map the virtual memory. *)
305 let fd = openfile filename [O_RDONLY] 0 in
306 let mem = MMap.of_file fd text_addr in
308 (* Force the wordsize and endianness. *)
309 let mem = MMap.set_wordsize mem wordsize in
310 let mem = MMap.set_endian mem endian in
312 (filename, arch, mem)
318 fun (name, arch, mem) ->
319 (* Look for some common entries in the exported symbol table and
320 * from that find the symbol table itself. These are just
321 * supposed to be symbols which are very likely to be present
322 * in any Linux kernel, although we only need one of them to be
323 * present to find the symbol table.
325 * NB. Must not be __initdata, must be in EXPORT_SYMBOL.
328 "init_task"; (* first task_struct *)
329 "root_mountflags"; (* flags for mounting root fs *)
330 "init_uts_ns"; (* uname strings *)
331 "sys_open"; (* open(2) entry point *)
332 "sys_chdir"; (* chdir(2) entry point *)
333 "sys_chroot"; (* chroot(2) entry point *)
334 "sys_umask"; (* umask(2) entry point *)
335 "schedule"; (* scheduler entry point *)
337 (* Searching for <NUL>string<NUL> *)
338 let common_ksyms_nul = List.map (sprintf "\000%s\000") common_ksyms in
340 (* Search for these strings in the memory image. *)
341 let ksym_strings = List.map (MMap.find_all mem) common_ksyms_nul in
342 let ksym_strings = List.concat ksym_strings in
343 (* Adjust found addresses to start of the string (skip <NUL>). *)
344 let ksym_strings = List.map Int64.succ ksym_strings in
346 (* For any we found, try to look up the symbol table
347 * base addr and size.
349 let ksymtabs = List.map (
351 (* Search for 'addr' appearing in the image. *)
352 let addrs = MMap.find_pointer_all mem addr in
354 (* Now consider each of these addresses and search back
355 * until we reach the beginning of the (possible) symbol
358 * Kernel symbol table struct is:
359 * struct kernel_symbol {
360 * unsigned long value;
361 * const char *name; <-- initial pointer
364 let pred_long2 addr =
365 MMap.pred_long mem (MMap.pred_long mem addr)
367 let base_addrs = List.map (
370 (* '*addr' should point to a C identifier. If it does,
371 * step backwards to the previous symbol table entry.
373 let addrp = MMap.follow_pointer mem addr in
374 if MMap.is_C_identifier mem addrp then
375 loop (pred_long2 addr)
377 MMap.succ_long mem addr
382 (* Also look for the end of the symbol table and
383 * calculate its size.
385 let base_addrs_sizes = List.map (
388 let addr2 = MMap.succ_long mem addr in
389 let addr2p = MMap.follow_pointer mem addr2 in
390 if MMap.is_C_identifier mem addr2p then
391 loop (MMap.succ_long mem addr2)
395 let end_addr = loop base_addr in
396 base_addr, end_addr -^ base_addr
401 let ksymtabs = List.concat ksymtabs in
403 (* Simply ignore any symbol table candidates which are too small. *)
404 let ksymtabs = List.filter (fun (_, size) -> size > 64L) ksymtabs in
407 printf "%s: candidate symbol tables at:\n" name;
410 printf "\t%Lx\t%Lx\t%!" addr size;
411 printf "first symbol: %s\n%!"
413 (MMap.follow_pointer mem
414 (MMap.succ_long mem addr)))
418 (* Vote for the most popular symbol table candidate and from this
419 * generate a function to look up ksyms.
422 let freqs = frequency ksymtabs in
425 eprintf (f_"%s: cannot find start of kernel symbol table\n") name;
426 (fun _ -> raise Not_found)
428 | (_, (ksymtab_addr, ksymtab_size)) :: _ ->
431 "%s: Kernel symbol table found at %Lx, size %Lx bytes\n%!"
432 name ksymtab_addr ksymtab_size;
434 (* Load the whole symbol table as a bitstring. *)
436 Bitmatch.bitstring_of_string
437 (MMap.get_bytes mem ksymtab_addr
438 (Int64.to_int ksymtab_size)) in
440 (* Function to look up an address in the symbol table. *)
441 let lookup_ksym sym =
442 let bits = bits_of_wordsize (MMap.get_wordsize mem) in
443 let e = MMap.get_endian mem in
446 | { value : bits : endian(e);
447 name_ptr : bits : endian(e) }
448 when MMap.get_string mem name_ptr = sym ->
450 | { _ : bits : endian(e);
451 _ : bits : endian(e);
452 bs : -1 : bitstring } ->
454 | { _ } -> raise Not_found
462 (* Now try to find the /proc/kallsyms table. This is in an odd
463 * compressed format (but not a very successful compression
464 * format). However if it exists we know that it will contain
465 * addresses of the common ksyms above, and it has some
466 * characteristics which make it easy to detect in the
469 * kallsyms contains a complete list of symbols so is much
470 * more useful than the basic list of exports.
472 let ksym_addrs = List.filter_map (
473 fun ksym -> try Some (lookup_ksym ksym) with Not_found -> None
476 (* Search for those kernel addresses in the image. We're looking
477 * for the table kallsyms_addresses followed by kallsyms_num_syms
478 * (number of symbols in the table).
480 let ksym_addrs = List.map (MMap.find_pointer_all mem) ksym_addrs in
481 let ksym_addrs = List.concat ksym_addrs in
483 (* Test each one to see if it's a candidate list of kernel
484 * addresses followed by length of list.
486 let kallsymtabs = List.filter_map (
488 (* Search upwards from address until we find the length field.
489 * If found, jump backwards by length and check all addresses.
492 printf "%s: testing candidate kallsyms at %Lx\n" name addr;
494 let addrp = MMap.follow_pointer mem addr in
495 if MMap.is_mapped mem addrp then
496 loop (MMap.succ_long mem addr) (* continue up the table *)
498 if addrp >= min_kallsyms_tabsize &&
499 addrp <= max_kallsyms_tabsize then (
500 (* addrp might be the symbol count. Count backwards and
501 * check the full table.
503 let num_entries = Int64.to_int addrp in
504 let entry_size = bytes_of_wordsize (MMap.get_wordsize mem) in
506 addr -^ Int64.of_int (entry_size * num_entries) in
507 let end_addr = addr in
509 if addr < end_addr then (
510 let addrp = MMap.follow_pointer mem addr in
511 if MMap.is_mapped mem addrp then
512 loop2 (MMap.succ_long mem addr)
514 None (* can't verify the full address table *)
517 let names_addr = MMap.succ_long mem end_addr in
519 printf "%s: candidate kallsyms found at %Lx (names_addr at %Lx, num_entries %d)\n"
520 name start_addr names_addr num_entries;
521 Some (start_addr, num_entries, names_addr)
530 | Some (start_addr, num_entries, names_addr) ->
531 (* As an additional verification, check the list of
535 (* If the first byte is '\000' and is followed by a
536 * C identifier, then this is old-school list of
537 * symbols with prefix compression as in 2.6.9.
538 * Otherwise Huffman-compressed kallsyms as in
541 if MMap.get_byte mem names_addr = 0 &&
542 MMap.is_C_identifier mem (names_addr+^1L) then (
543 let names = ref [] in
545 let rec loop names_addr start_addr num =
547 let prefix = MMap.get_byte mem names_addr in
548 let prefix = String.sub !prev 0 prefix in
549 let name = MMap.get_string mem (names_addr+^1L) in
550 let len = String.length name in
551 let name = prefix ^ name in
553 let names_addr = names_addr +^ Int64.of_int len +^ 2L in
554 let sym_value = MMap.follow_pointer mem start_addr in
555 let start_addr = MMap.succ_long mem start_addr in
556 (*printf "%S -> %Lx\n" name sym_value;*)
557 names := (name, sym_value) :: !names;
558 loop names_addr start_addr (num-1)
561 loop names_addr start_addr num_entries;
562 let names = List.rev !names in
564 Some (start_addr, num_entries, names_addr,
567 else ( (* new-style "compressed" names. *)
568 let compressed_names = ref [] in
569 let rec loop names_addr start_addr num =
571 let len = MMap.get_byte mem names_addr in
572 let name = MMap.get_bytes mem (names_addr+^1L) len in
573 let names_addr = names_addr +^ Int64.of_int len +^ 1L in
574 let sym_value = MMap.follow_pointer mem start_addr in
575 let start_addr = MMap.succ_long mem start_addr in
577 (name, sym_value) :: !compressed_names;
578 loop names_addr start_addr (num-1)
582 let markers_addr = loop names_addr start_addr num_entries in
583 let markers_addr = MMap.align mem markers_addr in
584 let compressed_names = List.rev !compressed_names in
586 Some (start_addr, num_entries, names_addr,
587 Compressed (compressed_names, markers_addr))
590 Invalid_argument _ -> None (* bad names list *)
594 printf "%s: candidate kallsyms at:\n" name;
597 | (start_addr, num_entries, names_addr, Uncompressed _) ->
598 printf "\t%Lx %d entries names_addr=%Lx old-style\n%!"
599 start_addr num_entries names_addr
600 | (start_addr, num_entries, names_addr,
601 Compressed (_, markers_addr)) ->
602 printf "\t%Lx %d entries names_addr=%Lx markers_addr=%Lx\n%!"
603 start_addr num_entries names_addr markers_addr
607 (* Vote for the most popular symbol table candidate and
608 * enhance the function for looking up ksyms.
611 let freqs = frequency kallsymtabs in
614 (* Can't find any kallsymtabs, just return the lookup_ksym
615 * function generated previously from the exported symbols.
619 | (_, (_, _, _, Uncompressed names)) :: _ ->
620 let lookup_ksym name =
621 try (* first look it up in kallsyms table. *)
622 List.assoc name names
623 with Not_found -> (* try the old exports table instead *)
628 | (_, (start_addr, num_entries, names_addr,
629 Compressed (compressed_names, markers_addr))) :: _ ->
630 (* Skip the markers and look for the token table. *)
631 let num_markers = Int64.of_int ((num_entries + 255) / 256) in
633 Int64.of_int (bytes_of_wordsize (MMap.get_wordsize mem)) in
634 let tokens_addr = markers_addr +^ marker_size *^ num_markers in
636 (* Now read out the compression tokens, which are just
637 * 256 ASCIIZ strings that map bytes in the compression
638 * names to substrings.
640 let tokens = Array.make 256 "" in
641 let rec loop i addr =
643 let str = MMap.get_string mem addr in
644 let len = String.length str in
645 let addr = addr +^ Int64.of_int (len+1) in
652 (* Expand the compressed names using the tokens. *)
653 let names = List.filter_map (
654 fun (name, sym_value) ->
655 let f c = tokens.(Char.code c) in
656 let name = String.replace_chars f name in
657 (* First character in uncompressed output is the symbol
658 * type, eg. 'T'/'t' for text etc.
660 (* NOTE: Symbol names are NOT unique
661 * (eg. 'con_start' is both a function and data in
662 * some kernels). XXX We need to handle this situation
665 (*let typ = name.[0] in*)
666 let name = String.sub name 1 (String.length name - 1) in
667 (*printf "%S -> %Lx\n" name sym_value;*)
668 Some (name, sym_value)
669 ) compressed_names in
671 let lookup_ksym name =
672 try (* first look it up in kallsyms table. *)
673 List.assoc name names
674 with Not_found -> (* try the old exports table instead *)
680 (* Just wrap the lookup_ksym call in something which prints
681 * the query when debug is set.
685 let lookup_ksym sym =
687 let value = lookup_ksym sym in
688 printf "lookup_ksym %S = %Lx\n%!" sym value;
691 printf "lookup_ksym %S failed\n%!" sym;
699 ((name, arch, mem, lookup_ksym) : image)