Using optional fields, navigate net_device list in its various incarnations.
[virt-mem.git] / lib / virt_mem_tasks.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
20 open Printf
21
22 open Virt_mem_gettext.Gettext
23 open Virt_mem_utils
24 open Virt_mem_types
25
26 open Kernel_task_struct
27
28 let find_tasks debug image ksymmap kernel_version =
29   if not (task_struct_known kernel_version) then (
30     eprintf (f_"%s: %s: unknown kernel version
31 Try a newer version of virt-mem, or if the guest is not from a
32 supported Linux distribution, see this page about adding support:
33   http://et.redhat.com/~rjones/virt-mem/faq.html\n")
34       image.domname kernel_version;
35     image, None
36   ) else (
37     let task_struct_size = task_struct_size kernel_version in
38
39     let init_task_addr =
40       try Some (Ksymmap.find "init_task" ksymmap)
41       with Not_found ->
42         eprintf (f_"%s: could not find init_task in kernel image\n")
43           image.domname;
44         None in
45     match init_task_addr with
46     | None -> image, None
47     | Some init_task_addr ->
48         let init_task =
49           get_task_struct kernel_version image.mem init_task_addr in
50
51         (* Starting at init_task, navigate through the linked list of
52          * tasks (through tasks.next).  Just make sure they are mapped
53          * into memory.
54          *)
55         let image =
56           let rec loop image task =
57             let next = task.task_struct_tasks'next in
58             if next <> init_task_addr then (
59               let mapped =
60                 Virt_mem_mmap.is_mapped_range image.mem next task_struct_size in
61               let image =
62                 if not mapped then
63                   Virt_mem_types.load_memory image next task_struct_size
64                 else
65                   image in
66               let task = get_task_struct kernel_version image.mem next in
67               loop image task
68             ) else
69               image
70           in
71           loop image init_task in
72
73         image, Some init_task_addr
74   )