From 6718047aff504b3261229dd648b1a4c00ca9ab21 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: [PATCH] Simple support for new libvirt virDomainMemoryPeek --- dmesg/Makefile.in | 4 +- lib/Makefile.in | 4 +- lib/virt_mem.ml | 145 ++++++++++++++++++++++++++++++++++++++++++-------- lib/virt_mem_mmap.ml | 19 +++++++ lib/virt_mem_mmap.mli | 7 +++ uname/Makefile.in | 4 +- 6 files changed, 155 insertions(+), 28 deletions(-) diff --git a/dmesg/Makefile.in b/dmesg/Makefile.in index 388b1a8..a7ea7a5 100644 --- a/dmesg/Makefile.in +++ b/dmesg/Makefile.in @@ -23,8 +23,8 @@ INSTALL = @INSTALL@ SYNTAX = -pp "camlp4o -I`ocamlc -where`/bitmatch bitmatch.cma pa_bitmatch.cmo" -#OCAMLCPACKAGES = -package unix,bigarray,extlib,bitmatch -OCAMLCPACKAGES = -package unix,bigarray,extlib -I +bitmatch -I ../lib +#OCAMLCPACKAGES = -package unix,bigarray,extlib,libvirt,xml-light,bitmatch +OCAMLCPACKAGES = -package unix,bigarray,extlib,libvirt,xml-light -I +bitmatch -I ../lib ifneq ($(pkg_gettext),no) OCAMLCPACKAGES += -package gettext-stub diff --git a/lib/Makefile.in b/lib/Makefile.in index 0af4ae7..1a20d23 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -23,8 +23,8 @@ INSTALL = @INSTALL@ SYNTAX = -pp "camlp4o -I$$(ocamlc -where)/bitmatch bitmatch.cma pa_bitmatch.cmo" -#OCAMLCPACKAGES = -package unix,bigarray,extlib,bitmatch -OCAMLCPACKAGES = -package unix,bigarray,extlib -I +bitmatch +#OCAMLCPACKAGES = -package unix,bigarray,extlib,libvirt,xml-light,bitmatch +OCAMLCPACKAGES = -package unix,bigarray,extlib,libvirt,xml-light -I +bitmatch ifneq ($(pkg_gettext),no) OCAMLCPACKAGES += -package gettext-stub diff --git a/lib/virt_mem.ml b/lib/virt_mem.ml index 4859840..3a42448 100644 --- a/lib/virt_mem.ml +++ b/lib/virt_mem.ml @@ -22,6 +22,9 @@ open Printf open ExtList open ExtString +module C = Libvirt.Connect +module D = Libvirt.Domain + open Virt_mem_gettext.Gettext open Virt_mem_utils module MMap = Virt_mem_mmap @@ -29,6 +32,9 @@ module MMap = Virt_mem_mmap let min_kallsyms_tabsize = 1_000L let max_kallsyms_tabsize = 250_000L +let kernel_size = 0x100_0000 +let max_memory_peek = 0x1_000 + type ksym = string type image = @@ -42,8 +48,8 @@ type kallsyms_compr = | Uncompressed of (string * MMap.addr) list let start usage_msg = - (* Verbose messages. *) - let verbose = ref false in + (* Debug messages. *) + let debug = ref false in (* Default wordsize. *) let def_wordsize = ref None in @@ -87,6 +93,7 @@ let start usage_msg = (* List of kernel images. *) let images = ref [] in + let uri = ref "" in let memory_image filename = images := @@ -96,17 +103,21 @@ let start usage_msg = let argspec = Arg.align [ "-A", Arg.String set_architecture, - "arch " ^ s_"Set kernel architecture, endianness and word size"; + "arch " ^ s_"Set kernel architecture, endianness and word size"; "-E", Arg.String set_endian, - "endian " ^ s_"Set kernel endianness"; + "endian " ^ s_"Set kernel endianness"; "-T", Arg.String set_text_addr, - "addr " ^ s_"Set kernel text address"; + "addr " ^ s_"Set kernel text address"; "-W", Arg.String set_wordsize, - "addr " ^ s_"Set kernel word size"; + "addr " ^ s_"Set kernel word size"; + "-c", Arg.Set_string uri, + "uri " ^ s_ "Connect to URI"; + "--connect", Arg.Set_string uri, + "uri " ^ s_ "Connect to URI"; + "--debug", Arg.Set debug, + " " ^ s_"Debug mode (default: false)"; "-t", Arg.String memory_image, - "image " ^ s_"Use saved kernel memory image"; - "-verbose", Arg.Set verbose, - " " ^ s_"Verbose messages"; + "image " ^ s_"Use saved kernel memory image"; ] in let anon_fun str = @@ -115,14 +126,104 @@ let start usage_msg = Arg.parse argspec anon_fun usage_msg; let images = !images in - let verbose = !verbose in + let debug = !debug in + let uri = if !uri = "" then None else Some !uri in (* Get the kernel images. *) let images = - if images = [] then - (* XXX use libvirt to get images *) - failwith "libvirt: not yet implemented" - else + if images = [] then ( + let conn = + let name = uri in + try C.connect_readonly ?name () + with Libvirt.Virterror err -> + prerr_endline (Libvirt.Virterror.to_string err); + (* If non-root and no explicit connection URI, print a warning. *) + if Unix.geteuid () <> 0 && name = None then ( + print_endline (s_ "NB: If you want to monitor a local Xen hypervisor, you usually need to be root"); + ); + exit 1 in + + (* List of active domains. *) + let doms = + let nr_active_doms = C.num_of_domains conn in + let active_doms = + Array.to_list (C.list_domains conn nr_active_doms) in + let active_doms = + List.map (D.lookup_by_id conn) active_doms in + active_doms in + + (* Get their XML. *) + let xmls = List.map (fun dom -> dom, D.get_xml_desc dom) doms in + + (* Parse the XML. *) + let xmls = List.map (fun (dom, xml) -> + dom, Xml.parse_string xml) xmls in + + (* XXX Do something with the XML XXX + * such as detecting arch, wordsize, endianness. + * XXXXXXXXXXXXXX + * + * + * + *) + + + List.map ( + fun (dom, _) -> + let name = D.get_name dom in + + let wordsize = + match !def_wordsize with + | None -> + failwith + (sprintf (f_"%s: use -W to define word size for this image") + name); + | Some ws -> ws in + let endian = + match !def_endian with + | None -> + failwith + (sprintf (f_"%s: use -E to define endianness for this image") + name); + | Some e -> e in + + let arch = + match !def_architecture with + | Some I386 -> I386 | Some X86_64 -> X86_64 + | _ -> + failwith + (sprintf (f_"%s: use -A to define architecture (i386/x86-64 only) for this image") name) in + + if !def_text_addr = 0L then + failwith + (sprintf (f_"%s: use -T to define kernel load address for this image") + name); + + (* Read the kernel memory. + * Maximum 64K can be read over remote connections. + *) + let str = String.create kernel_size in + let rec loop i = + let remaining = kernel_size - i in + if remaining > 0 then ( + let size = min remaining max_memory_peek in + D.memory_peek dom [D.Virtual] + (!def_text_addr +^ Int64.of_int i) size str i; + loop (i + size) + ) + in + loop 0; + + (* Map the virtual memory. *) + let mem = MMap.of_string str !def_text_addr in + + (* Force the wordsize and endianness. *) + let mem = MMap.set_wordsize mem wordsize in + let mem = MMap.set_endian mem endian in + + (name, arch, mem) + ) xmls + ) else List.map ( fun (wordsize, endian, arch, text_addr, filename) -> (* Quite a lot of limitations on the kernel images we can @@ -257,7 +358,7 @@ let start usage_msg = (* Simply ignore any symbol table candidates which are too small. *) let ksymtabs = List.filter (fun (_, size) -> size > 64L) ksymtabs in - if verbose then ( + if debug then ( printf "%s: candidate symbol tables at:\n" name; List.iter ( fun (addr, size) -> @@ -280,7 +381,7 @@ let start usage_msg = (fun _ -> raise Not_found) | (_, (ksymtab_addr, ksymtab_size)) :: _ -> - if verbose then + if debug then printf "%s: Kernel symbol table found at %Lx, size %Lx bytes\n%!" name ksymtab_addr ksymtab_size; @@ -342,7 +443,7 @@ let start usage_msg = (* Search upwards from address until we find the length field. * If found, jump backwards by length and check all addresses. *) - if verbose then + if debug then printf "%s: testing candidate kallsyms at %Lx\n" name addr; let rec loop addr = let addrp = MMap.follow_pointer mem addr in @@ -369,7 +470,7 @@ let start usage_msg = ) else (* ok! *) let names_addr = MMap.succ_long mem end_addr in - if verbose then + if debug then printf "%s: candidate kallsyms found at %Lx (names_addr at %Lx, num_entries %d)\n" name start_addr names_addr num_entries; Some (start_addr, num_entries, names_addr) @@ -444,7 +545,7 @@ let start usage_msg = Invalid_argument _ -> None (* bad names list *) ) ksym_addrs in - if verbose then ( + if debug then ( printf "%s: candidate kallsyms at:\n" name; List.iter ( function @@ -532,10 +633,10 @@ let start usage_msg = lookup_ksym in (* Just wrap the lookup_ksym call in something which prints - * the query when verbose is set. + * the query when debug is set. *) let lookup_ksym = - if verbose then + if debug then let lookup_ksym sym = try let value = lookup_ksym sym in @@ -553,4 +654,4 @@ let start usage_msg = ((name, arch, mem, lookup_ksym) : image) ) images in - verbose, images + debug, images diff --git a/lib/virt_mem_mmap.ml b/lib/virt_mem_mmap.ml index 87741e4..98dc84a 100644 --- a/lib/virt_mem_mmap.ml +++ b/lib/virt_mem_mmap.ml @@ -78,6 +78,25 @@ let of_file fd addr = let t = create () in add_file t fd addr +let add_string ({ mappings = mappings } as t) str addr = + if addr &^ 7L <> 0L then + invalid_arg "add_file: mapping address must be aligned to 8 bytes"; + let size = String.length str in + (* Use Bigarray.Array1. XXX We should just use the string. *) + let arr = Array1.create char c_layout size in + for i = 0 to size-1 do + Array1.set arr i (String.unsafe_get str i) + done; + (* Create the mapping entry and keep the mappings sorted by start addr. *) + let mappings = + { start = addr; size = Int64.of_int size; arr = arr } :: mappings in + let mappings = sort_mappings mappings in + { t with mappings = mappings } + +let of_string str addr = + let t = create () in + add_string t str addr + (* Find in mappings and return first predicate match. *) let _find_map { mappings = mappings } pred = let rec loop = function diff --git a/lib/virt_mem_mmap.mli b/lib/virt_mem_mmap.mli index de052a2..0ed9176 100644 --- a/lib/virt_mem_mmap.mli +++ b/lib/virt_mem_mmap.mli @@ -52,6 +52,13 @@ val add_file : ('a, 'b) t -> Unix.file_descr -> addr -> ('a, 'b) t (** Add file [fd] at address [addr] to an existing memory map. Behaviour is undefined if memory mappings overlap. *) +val of_string : string -> addr -> ([`NoWordsize], [`NoEndian]) t +(** Create a new memory map, mapping string at address [addr]. *) + +val add_string : ('a, 'b) t -> string -> addr -> ('a, 'b) t +(** Add string at address [addr] to an existing memory map. + Behaviour is undefined if memory mappings overlap. *) + val find : ('a, 'b) t -> ?start:addr -> string -> addr option (** Find string in a memory map and return its address (if found). You can pass an optional starting address. Any holes in diff --git a/uname/Makefile.in b/uname/Makefile.in index 6a57ad2..672aaeb 100644 --- a/uname/Makefile.in +++ b/uname/Makefile.in @@ -23,8 +23,8 @@ INSTALL = @INSTALL@ SYNTAX = -pp "camlp4o -I`ocamlc -where`/bitmatch bitmatch.cma pa_bitmatch.cmo" -#OCAMLCPACKAGES = -package unix,bigarray,extlib,bitmatch -OCAMLCPACKAGES = -package unix,bigarray,extlib -I +bitmatch -I ../lib +#OCAMLCPACKAGES = -package unix,bigarray,extlib,libvirt,xml-light,bitmatch +OCAMLCPACKAGES = -package unix,bigarray,extlib,libvirt,xml-light -I +bitmatch -I ../lib ifneq ($(pkg_gettext),no) OCAMLCPACKAGES += -package gettext-stub -- 1.8.3.1