mirror of
https://github.com/JakeHillion/drgn.git
synced 2024-12-25 18:23:07 +00:00
724d15563d
New output: PID PPID CPU ST COMM 1 0 4 S systemd 2 0 13 S [kthreadd] 3 2 0 I [rcu_gp] 4 2 0 I [rcu_par_gp] 5 2 0 I [slub_flushwq] 6 2 0 I [netns] 8 2 0 I [kworker/0:0H] 10 2 0 I [mm_percpu_wq] 11 2 0 I [rcu_tasks_kthre] ... Signed-off-by: Martin Liska <mliska@suse.cz> Co-authored-by: Omar Sandoval <osandov@osandov.com>
33 lines
827 B
Python
Executable File
33 lines
827 B
Python
Executable File
#!/usr/bin/env drgn
|
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
"""A simplified implementation of ps(1) using drgn"""
|
|
|
|
from drgn.helpers.linux.pid import for_each_task
|
|
from drgn.helpers.linux.sched import task_cpu, task_state_to_char
|
|
|
|
|
|
def is_kthread(task):
|
|
"""
|
|
Make a guess if task_struct is a kernel thread.
|
|
"""
|
|
|
|
return not task.mm
|
|
|
|
|
|
print("PID PPID CPU ST COMM")
|
|
for task in for_each_task(prog):
|
|
pid = task.pid.value_()
|
|
ppid = task.parent.pid.value_() if task.parent else 0
|
|
|
|
comm = task.comm.string_().decode()
|
|
# Distinguish kernel and user-space threads
|
|
if is_kthread(task):
|
|
comm = f"[{comm}]"
|
|
|
|
cpu = task_cpu(task)
|
|
state = task_state_to_char(task)
|
|
|
|
print(f"{pid:<7} {ppid:<7} {cpu:<4} {state} {comm}")
|