Get rid of lookup_ksym function, replace with a map.
[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 images before kernel symbol analysis is attempted.
43  * Just save them to the output file(s).
44  *)
45 let rec beforeksyms debug = function
46   | [] ->
47       prerr_endline
48         (s_"virt-mem capture: warning: no kernel images were captured")
49   | [image] ->
50       (* Single image is saved to output_filename. *)
51       save_image image !output_filename
52   | images ->
53       (* Multiple images are saved to output_filename.ID where ID
54        * is the domain ID (if known) or a mangled domain name.
55        *)
56       List.iter (
57         fun ({ dom = dom; domname = domname } as image) ->
58           let filename =
59             !output_filename ^ "." ^
60             match dom with
61             | Some dom -> string_of_int (D.get_id dom)
62             | None ->
63                 let f = function
64                   | ('a'..'z'|'A'..'Z'|'0'..'9'|'_' as c) -> String.make 1 c
65                   | _ -> ""
66                 in
67                 String.replace_chars f domname in
68           save_image image filename
69       ) images
70
71 and save_image { domname = domname } filename =
72   assert false;
73
74   let chan = open_out filename in
75
76   close_out chan;
77
78   printf (f_"virt-mem capture: wrote kernel image from %s to filename %s\n")
79     domname filename
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     ~external_cmd:false ~extra_args
96     ~argcheck ~beforeksyms
97     "capture" summary description