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