X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fvirt_mem.ml;h=14456336e636dba69fec7315ba3d076ae529e342;hb=c6362265f3247c19a9791e2bae65e2c6747ecdf7;hp=3a42448d70e38ddc9db758297da8d7cbbf4c1b3d;hpb=6718047aff504b3261229dd648b1a4c00ca9ab21;p=virt-mem.git diff --git a/lib/virt_mem.ml b/lib/virt_mem.ml index 3a42448..1445633 100644 --- a/lib/virt_mem.ml +++ b/lib/virt_mem.ml @@ -32,8 +32,13 @@ 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 +(* Make the kernel size around 16 MB, but just a bit smaller than + * maximum string length so we can still run this on a 32 bit platform. + *) +let kernel_size = + if Sys.word_size = 32 then Sys.max_string_length + else 0x100_0000 +let max_memory_peek = 0x1000 type ksym = string @@ -94,6 +99,7 @@ let start usage_msg = (* List of kernel images. *) let images = ref [] in let uri = ref "" in + let anon_args = ref [] in let memory_image filename = images := @@ -101,6 +107,16 @@ let start usage_msg = :: !images in + let version () = + printf "virt-mem %s\n" Virt_mem_version.version; + + let major, minor, release = + let v, _ = Libvirt.get_version () in + v / 1_000_000, (v / 1_000) mod 1_000, v mod 1_000 in + printf "libvirt %d.%d.%d\n" major minor release; + exit 0 + in + let argspec = Arg.align [ "-A", Arg.String set_architecture, "arch " ^ s_"Set kernel architecture, endianness and word size"; @@ -118,16 +134,18 @@ let start usage_msg = " " ^ s_"Debug mode (default: false)"; "-t", Arg.String memory_image, "image " ^ s_"Use saved kernel memory image"; + "--version", Arg.Unit version, + " " ^ s_"Display version and exit"; ] in - let anon_fun str = - raise (Arg.Bad (sprintf (f_"%s: unknown parameter") str)) in + let anon_arg str = anon_args := str :: !anon_args in let usage_msg = usage_msg ^ s_"\n\nOPTIONS" in - Arg.parse argspec anon_fun usage_msg; + Arg.parse argspec anon_arg usage_msg; let images = !images in let debug = !debug in let uri = if !uri = "" then None else Some !uri in + let anon_args = List.rev !anon_args in (* Get the kernel images. *) let images = @@ -143,14 +161,36 @@ let start usage_msg = ); exit 1 in - (* List of active domains. *) + (* If we have a list of parameters, then it is the domain names / UUIDs / + * IDs ONLY that we wish to display. Otherwise, display all active. + *) 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 + if anon_args = [] then ( + (* List of active domains. *) + let nr_active_doms = C.num_of_domains conn in + let active_doms = + Array.to_list (C.list_domains conn nr_active_doms) in + List.map (D.lookup_by_id conn) active_doms + ) else ( + List.map ( + fun arg -> + let dom = + try D.lookup_by_uuid_string conn arg + with _ -> + try D.lookup_by_name conn arg + with _ -> + try D.lookup_by_id conn (int_of_string arg) + with _ -> + failwith (sprintf (f_"%s: unknown domain (not a UUID, name or ID of any active domain)") arg) in + + (* XXX Primitive test to see if the domain is active. *) + let is_active = try D.get_id dom >= 0 with _ -> false in + if not is_active then + failwith (sprintf (f_"%s: domain is not running") arg); + + dom + ) anon_args + ) in (* Get their XML. *) let xmls = List.map (fun dom -> dom, D.get_xml_desc dom) doms in @@ -196,8 +236,9 @@ let start usage_msg = if !def_text_addr = 0L then failwith - (sprintf (f_"%s: use -T to define kernel load address for this image") - name); + (sprintf (f_"%s: use -T to define kernel load address for this image") name); + + let start_t = gettimeofday () in (* Read the kernel memory. * Maximum 64K can be read over remote connections. @@ -214,6 +255,12 @@ let start usage_msg = in loop 0; + if debug then ( + let end_t = gettimeofday () in + eprintf "timing: downloading kernel took %f seconds\n%!" + (end_t -. start_t) + ); + (* Map the virtual memory. *) let mem = MMap.of_string str !def_text_addr in @@ -223,7 +270,11 @@ let start usage_msg = (name, arch, mem) ) xmls - ) else + ) else ( + (* One or more -t options passed. *) + if anon_args <> [] then + failwith (s_"virt-mem: if -t given on command line, then no domain arguments should be listed"); + List.map ( fun (wordsize, endian, arch, text_addr, filename) -> (* Quite a lot of limitations on the kernel images we can @@ -266,7 +317,8 @@ let start usage_msg = let mem = MMap.set_endian mem endian in (filename, arch, mem) - ) images in + ) images + ) in let images = List.map ( @@ -292,12 +344,22 @@ let start usage_msg = (* Searching for string *) let common_ksyms_nul = List.map (sprintf "\000%s\000") common_ksyms in + let start_t = gettimeofday () in + (* Search for these strings in the memory image. *) let ksym_strings = List.map (MMap.find_all mem) common_ksyms_nul in let ksym_strings = List.concat ksym_strings in (* Adjust found addresses to start of the string (skip ). *) let ksym_strings = List.map Int64.succ ksym_strings in + if debug then ( + let end_t = gettimeofday () in + eprintf "timing: searching for common_ksyms took %f seconds\n%!" + (end_t -. start_t) + ); + + let start_t = gettimeofday () in + (* For any we found, try to look up the symbol table * base addr and size. *) @@ -359,11 +421,11 @@ let start usage_msg = let ksymtabs = List.filter (fun (_, size) -> size > 64L) ksymtabs in if debug then ( - printf "%s: candidate symbol tables at:\n" name; + eprintf "%s: candidate symbol tables at:\n" name; List.iter ( fun (addr, size) -> - printf "\t%Lx\t%Lx\t%!" addr size; - printf "first symbol: %s\n%!" + eprintf "\t%Lx\t%Lx\t%!" addr size; + eprintf "first symbol: %s\n%!" (MMap.get_string mem (MMap.follow_pointer mem (MMap.succ_long mem addr))) @@ -382,7 +444,7 @@ let start usage_msg = | (_, (ksymtab_addr, ksymtab_size)) :: _ -> if debug then - printf + eprintf "%s: Kernel symbol table found at %Lx, size %Lx bytes\n%!" name ksymtab_addr ksymtab_size; @@ -414,6 +476,14 @@ let start usage_msg = lookup_ksym in + if debug then ( + let end_t = gettimeofday () in + eprintf "timing: searching for ordinary ksyms took %f seconds\n%!" + (end_t -. start_t) + ); + + let start_t = gettimeofday () in + (* Now try to find the /proc/kallsyms table. This is in an odd * compressed format (but not a very successful compression * format). However if it exists we know that it will contain @@ -444,7 +514,7 @@ let start usage_msg = * If found, jump backwards by length and check all addresses. *) if debug then - printf "%s: testing candidate kallsyms at %Lx\n" name addr; + eprintf "%s: testing candidate kallsyms at %Lx\n" name addr; let rec loop addr = let addrp = MMap.follow_pointer mem addr in if MMap.is_mapped mem addrp then @@ -471,7 +541,7 @@ let start usage_msg = (* ok! *) let names_addr = MMap.succ_long mem end_addr in if debug then - printf "%s: candidate kallsyms found at %Lx (names_addr at %Lx, num_entries %d)\n" + eprintf "%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) in @@ -508,7 +578,7 @@ let start usage_msg = let names_addr = names_addr +^ Int64.of_int len +^ 2L in let sym_value = MMap.follow_pointer mem start_addr in let start_addr = MMap.succ_long mem start_addr in - (*printf "%S -> %Lx\n" name sym_value;*) + (*eprintf "%S -> %Lx\n" name sym_value;*) names := (name, sym_value) :: !names; loop names_addr start_addr (num-1) ) @@ -546,15 +616,15 @@ let start usage_msg = ) ksym_addrs in if debug then ( - printf "%s: candidate kallsyms at:\n" name; + eprintf "%s: candidate kallsyms at:\n" name; List.iter ( function | (start_addr, num_entries, names_addr, Uncompressed _) -> - printf "\t%Lx %d entries names_addr=%Lx old-style\n%!" + eprintf "\t%Lx %d entries names_addr=%Lx old-style\n%!" start_addr num_entries names_addr | (start_addr, num_entries, names_addr, Compressed (_, markers_addr)) -> - printf "\t%Lx %d entries names_addr=%Lx markers_addr=%Lx\n%!" + eprintf "\t%Lx %d entries names_addr=%Lx markers_addr=%Lx\n%!" start_addr num_entries names_addr markers_addr ) kallsymtabs ); @@ -619,7 +689,7 @@ let start usage_msg = *) (*let typ = name.[0] in*) let name = String.sub name 1 (String.length name - 1) in - (*printf "%S -> %Lx\n" name sym_value;*) + (*eprintf "%S -> %Lx\n" name sym_value;*) Some (name, sym_value) ) compressed_names in @@ -632,6 +702,12 @@ let start usage_msg = lookup_ksym in + if debug then ( + let end_t = gettimeofday () in + eprintf "timing: searching for kallsyms took %f seconds\n%!" + (end_t -. start_t) + ); + (* Just wrap the lookup_ksym call in something which prints * the query when debug is set. *) @@ -640,10 +716,10 @@ let start usage_msg = let lookup_ksym sym = try let value = lookup_ksym sym in - printf "lookup_ksym %S = %Lx\n%!" sym value; + eprintf "lookup_ksym %S = %Lx\n%!" sym value; value with Not_found -> - printf "lookup_ksym %S failed\n%!" sym; + eprintf "lookup_ksym %S failed\n%!" sym; raise Not_found in lookup_ksym