Updated kernel structures & kerneldb.
[virt-mem.git] / lib / virt_mem_capture.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 capture' 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 (* This will contain what is passed by the user as '-o' option. *)
31 let output_filename = ref ""
32
33 (* Early argument check. *)
34 let argcheck debug =
35   (* -o flag must have been specified. *)
36   let output_filename = !output_filename in
37   if output_filename = "" then (
38     prerr_endline (s_"virt-mem capture: '-o memoryimage' option is required");
39     exit 1
40   )
41
42 (* Capture the image. *)
43 let rec run debug image kdata = ()
44 (*
45   | [] ->
46       prerr_endline
47         (s_"virt-mem capture: warning: no kernel images were captured")
48   | [image] ->
49       (* Single image is saved to output_filename. *)
50       save_image image !output_filename
51   | images ->
52       (* Multiple images are saved to output_filename.ID where ID
53        * is the domain ID (if known) or a mangled domain name.
54        *)
55       List.iter (
56         fun ({ dom = dom; domname = domname } as image) ->
57           let filename =
58             !output_filename ^ "." ^
59             match dom with
60             | Some dom -> string_of_int (D.get_id dom)
61             | None ->
62                 let f = function
63                   | ('a'..'z'|'A'..'Z'|'0'..'9'|'_' as c) -> String.make 1 c
64                   | _ -> ""
65                 in
66                 String.replace_chars f domname in
67           save_image image filename
68       ) images
69
70 and save_image { domname = domname } filename =
71   assert false;
72
73   let chan = open_out filename in
74
75   close_out chan;
76
77   printf (f_"virt-mem capture: wrote kernel image from %s to filename %s\n")
78     domname filename
79 *)
80
81 let summary = s_"capture memory image for post-mortem analysis"
82 let description = s_"Capture a memory image to a file for later post-mortem
83 analysis.  Use the '-o memoryimage' option to specify the
84 output file.
85
86 Other tools can load the memory image using the '-t' option."
87
88 let extra_args = [
89   "-o", Arg.Set_string output_filename,
90     "memoryimage " ^s_"Set output filename"
91 ]
92
93 let () =
94   Virt_mem.register
95     ~needs_everything:true ~run
96     ~external_cmd:false ~extra_args ~argcheck
97     "capture" summary description