1 (* Memory info command 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.
19 Implements 'virt-mem dump' command.
25 module D = Libvirt.Domain
28 open Virt_mem_gettext.Gettext
30 let start_addr = ref None
33 let set_start_addr addr = start_addr := Some (Int64.of_string addr)
34 let set_size n = size := Some (int_of_string n)
36 (* Early argument check. *)
38 (* --start flag must have been specified. *)
39 if !start_addr = None then (
40 prerr_endline (s_"virt-mem dump: '--start' option is required");
44 (* Capture the image. *)
45 let rec run debug image kdata =
46 let start_addr = !start_addr in
51 | None -> assert false (* should have been caught by argcheck *)
52 | Some addr -> addr in
55 | None -> 1 lsl 20 (* large, but not unlimited *)
56 | Some size -> size in
58 (* Load the memory. *)
59 let is_mapped = Virt_mem_mmap.is_mapped_range image.mem start_addr size in
62 load_memory image start_addr size
67 let bytes = Virt_mem_mmap.get_bytes image.mem start_addr size in
69 hexdump stdout start_addr bytes size
71 (* Modified from the version in bitstring library. *)
72 and hexdump chan count data len =
73 let count = ref count in
76 let linelen = ref 0 in
77 let linechars = String.make 16 ' ' in
79 fprintf chan "%08Lx " !count;
82 let bits = min !len 8 in
83 let byte, off', len' =
84 Bitstring.extract_char_unsigned data !off !len bits in
85 off := off'; len := len';
87 let byte = byte lsl (8-bits) in
88 fprintf chan "%02x " byte;
90 count := Int64.succ !count;
91 linechars.[!linelen] <-
92 (let c = Char.chr byte in
93 if isprint c then c else '.');
95 if !linelen = 8 then fprintf chan " ";
96 if !linelen = 16 then (
97 fprintf chan " |%s|\n%08Lx " linechars !count;
99 for i = 0 to 15 do linechars.[i] <- ' ' done
103 if !linelen > 0 then (
104 let skip = (16 - !linelen) * 3 + if !linelen < 8 then 1 else 0 in
105 for i = 0 to skip-1 do fprintf chan " " done;
106 fprintf chan " |%s|\n%!" linechars
111 let c = Char.code c in
114 let summary = s_"dump parts of virtual memory"
115 let description = s_"Dump parts of virtual memory for debugging purposes.
117 Use the --start and --size parameters to specify the start address
118 and size of the region to dump. Hexadecimal numbers can be specified
119 using '0x..' notation.
123 "--start", Arg.String set_start_addr,
124 "addr " ^s_"Set start address to dump";
125 "--size", Arg.String set_size,
126 "size " ^s_"Set size (in bytes) to dump";
130 Virt_mem.register ~run
131 ~external_cmd:false ~extra_args ~argcheck
132 "dump" summary description