Experimental automated 'follower' code.
[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 (*
27 open Kernel_task_struct
28
29 let max_tasks = 10000
30
31 let find_tasks debug image ksymmap kernel_version =
32   if not (task_struct_known kernel_version) then (
33     eprintf (f_"%s: %s: unknown kernel version
34 Try a newer version of virt-mem, or if the guest is not from a
35 supported Linux distribution, see this page about adding support:
36   http://et.redhat.com/~rjones/virt-mem/faq.html\n")
37       image.domname kernel_version;
38     image, None
39   ) else (
40     let size = task_struct_size kernel_version in
41
42     let init_task_addr =
43       try Some (Ksymmap.find "init_task" ksymmap)
44       with Not_found ->
45         eprintf (f_"%s: could not find init_task in kernel image\n")
46           image.domname;
47         None in
48     match init_task_addr with
49     | None -> image, None
50     | Some init_task_addr ->
51         let { field_offset = offset } =
52           field_signature_of_task_struct_tasks'next kernel_version in
53
54         let lh = Virt_mem_list_head.create_base image init_task_addr offset in
55         let image, lh = Virt_mem_list_head.load_all lh size in
56
57         let tasks, _ =
58           Virt_mem_list_head.fold lh ([], 0) (
59             fun (tasks, i) addr ->
60               if i > max_tasks then
61                 failwith (sprintf (f_"%s: too many tasks") image.domname);
62
63               let task = get_task_struct kernel_version image.mem addr in
64               let tasks = task :: tasks in
65               (tasks, i+1)
66           ) in
67
68         (* Convert to the internal format. *)
69         let tasks = List.rev_map (
70           fun task ->
71             { task_state = task.task_struct_state;
72               task_prio = task.task_struct_prio;
73               task_normal_prio = task.task_struct_normal_prio;
74               task_static_prio = task.task_struct_static_prio;
75               task_comm = truncate_c_string task.task_struct_comm;
76               task_pid = task.task_struct_pid }
77         ) tasks in
78
79         image, Some tasks
80   )
81 *)