c2ae54ef90a453e82c4289b8f9a43da490ed331f
[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 max_tasks = 10000
29
30 let find_tasks debug image ksymmap kernel_version =
31   if not (task_struct_known kernel_version) then (
32     eprintf (f_"%s: %s: unknown kernel version
33 Try a newer version of virt-mem, or if the guest is not from a
34 supported Linux distribution, see this page about adding support:
35   http://et.redhat.com/~rjones/virt-mem/faq.html\n")
36       image.domname kernel_version;
37     image, None
38   ) else (
39     let task_struct_size = task_struct_size kernel_version in
40
41     let init_task_addr =
42       try Some (Ksymmap.find "init_task" ksymmap)
43       with Not_found ->
44         eprintf (f_"%s: could not find init_task in kernel image\n")
45           image.domname;
46         None in
47     match init_task_addr with
48     | None -> image, None
49     | Some init_task_addr ->
50         let init_task =
51           get_task_struct kernel_version image.mem init_task_addr in
52
53         (* Starting at init_task, navigate through the linked list of
54          * tasks (through tasks.next).  Map them into memory and load
55          * them into a list.
56          *)
57         let image, tasks =
58           let rec loop i image acc task =
59             if i <= max_tasks then (
60               let next = task.task_struct_tasks'next in
61               if next <> init_task_addr then (
62                 let mapped =
63                   Virt_mem_mmap.is_mapped_range image.mem next task_struct_size in
64                 let image =
65                   if not mapped then
66                     Virt_mem_types.load_memory image next task_struct_size
67                   else
68                     image in
69                 let task = get_task_struct kernel_version image.mem next in
70                 loop (i+1) image (task :: acc) task
71               ) else
72                 image, acc
73             ) else
74               failwith (sprintf (f_"%s: too many tasks") image.domname)
75           in
76           loop 0 image [] init_task in
77
78         (* Convert to the internal format. *)
79         let tasks = List.rev_map (
80           fun task ->
81             { task_state = task.task_struct_state;
82               task_prio = task.task_struct_prio;
83               task_normal_prio = task.task_struct_normal_prio;
84               task_static_prio = task.task_struct_static_prio;
85               task_comm = truncate_c_string task.task_struct_comm;
86               task_pid = task.task_struct_pid }
87         ) tasks in
88
89         image, Some tasks
90   )