From a011989407845136f4aea12589b020fc1b4185c6 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: [PATCH] Much more similar to real 'ps' command now. --- ps/virt_ps.ml | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/ps/virt_ps.ml b/ps/virt_ps.ml index 100a298..a9bcf1f 100644 --- a/ps/virt_ps.ml +++ b/ps/virt_ps.ml @@ -23,6 +23,7 @@ open Virt_mem_gettext.Gettext open Virt_mem_utils open Virt_mem_types +open Kernel_task_struct let run debug (image, ksymmap, utsname) = try @@ -35,7 +36,7 @@ let run debug (image, ksymmap, utsname) = raise Exit | Some { kernel_release = v } -> v in - if not (Kernel_task_struct.known kernel_version) then ( + if not (task_struct_known kernel_version) then ( eprintf (f_"%s: %s: unknown kernel version Try a newer version of virt-mem, or if the guest is not from a supported Linux distribution, see this page about adding support: @@ -43,7 +44,7 @@ supported Linux distribution, see this page about adding support: raise Exit ); - let task_struct_size = Kernel_task_struct.size kernel_version in + let task_struct_size = task_struct_size kernel_version in let init_task, init_task_addr = let init_task_addr = @@ -51,10 +52,8 @@ supported Linux distribution, see this page about adding support: with Not_found -> eprintf (f_"%s: could not find init_task in kernel image\n") domname; raise Exit in - let addr = - (Virt_mem_mmap.unsafe_typed_addr_of_addr init_task_addr : - [ `task_struct ] Virt_mem_mmap.typed_addr) in - let init_task = Kernel_task_struct.get kernel_version image.mem addr in + let init_task = + get_task_struct kernel_version image.mem init_task_addr in init_task, init_task_addr in (* Starting at init_task, navigate through the linked list of @@ -62,16 +61,18 @@ supported Linux distribution, see this page about adding support: *) let tasks, image = let rec loop image acc task = - let next = task.Kernel_task_struct.tasks'next in - let next_addr = Virt_mem_mmap.unsafe_addr_of_typed_addr next in - if next_addr <> init_task_addr then ( + let next = task.task_struct_tasks'next in + if next <> init_task_addr then ( let mapped = - Virt_mem_mmap.is_mapped_range image.mem - next_addr task_struct_size in + Virt_mem_mmap.is_mapped_range image.mem next task_struct_size in let image = - if not mapped then load_memory image next_addr task_struct_size + if not mapped then load_memory image next task_struct_size else image in - let task = Kernel_task_struct.get kernel_version image.mem next in + let task = get_task_struct kernel_version image.mem next in + let task = { + task with + task_struct_comm = truncate_c_string task.task_struct_comm + } in let acc = task :: acc in loop image acc task ) else @@ -79,13 +80,18 @@ supported Linux distribution, see this page about adding support: in loop image [] init_task in + (* Sort tasks by PID. *) + let cmp { task_struct_pid = p1 } { task_struct_pid = p2 } = compare p1 p2 in + let tasks = List.sort cmp tasks in + + printf " PID STAT COMMAND\n"; + List.iter ( fun task -> - printf "%S\n" task.Kernel_task_struct.comm + printf "%5Ld %s\n" task.task_struct_pid task.task_struct_comm ) tasks - with - | Exit -> () + with Exit -> () let summary = s_"list processes in virtual machine" let description = s_"\ -- 1.8.3.1