Updated kernel structures & kerneldb.
[virt-mem.git] / lib / virt_mem_dump.ml
1 (* Memory info command 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    Implements 'virt-mem dump' command.
20  *)
21
22 open Printf
23 open ExtString
24
25 module D = Libvirt.Domain
26
27 open Virt_mem_types
28 open Virt_mem_gettext.Gettext
29
30 let start_addr = ref None
31 let size = ref None
32
33 let set_start_addr addr = start_addr := Some (Int64.of_string addr)
34 let set_size n = size := Some (int_of_string n)
35
36 (* Early argument check. *)
37 let argcheck debug =
38   (* --start flag must have been specified. *)
39   if !start_addr = None then (
40     prerr_endline (s_"virt-mem dump: '--start' option is required");
41     exit 1
42   )
43
44 (* Capture the image. *)
45 let rec run debug image kdata =
46   let start_addr = !start_addr in
47   let size = !size in
48
49   let start_addr =
50     match start_addr with
51     | None -> assert false   (* should have been caught by argcheck *)
52     | Some addr -> addr in
53   let size =
54     match size with
55     | None -> 1 lsl 20       (* large, but not unlimited *)
56     | Some size -> size in
57
58   (* Load the memory. *)
59   let is_mapped = Virt_mem_mmap.is_mapped_range image.mem start_addr size in
60   let image =
61     if not is_mapped then
62       load_memory image start_addr size
63     else
64       image in
65
66   (* Get the bytes. *)
67   let bytes = Virt_mem_mmap.get_bytes image.mem start_addr size in
68
69   hexdump stdout start_addr bytes size
70
71 (* Modified from the version in bitstring library. *)
72 and hexdump chan count data len =
73   let count = ref count in
74   let off = ref 0 in
75   let len = ref len in
76   let linelen = ref 0 in
77   let linechars = String.make 16 ' ' in
78
79   fprintf chan "%08Lx  " !count;
80
81   while !len > 0 do
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';
86
87     let byte = byte lsl (8-bits) in
88     fprintf chan "%02x " byte;
89
90     count := Int64.succ !count;
91     linechars.[!linelen] <-
92       (let c = Char.chr byte in
93        if isprint c then c else '.');
94     incr linelen;
95     if !linelen = 8 then fprintf chan " ";
96     if !linelen = 16 then (
97       fprintf chan " |%s|\n%08Lx  " linechars !count;
98       linelen := 0;
99       for i = 0 to 15 do linechars.[i] <- ' ' done
100     )
101   done;
102
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
107   ) else
108     fprintf chan "\n%!"
109
110 and isprint c =
111   let c = Char.code c in
112   c >= 32 && c < 127
113
114 let summary = s_"dump parts of virtual memory"
115 let description = s_"Dump parts of virtual memory for debugging purposes.
116
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.
120 "
121
122 let extra_args = [
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";
127 ]
128
129 let () =
130   Virt_mem.register ~run
131     ~external_cmd:false ~extra_args ~argcheck
132     "dump" summary description