drgn/contrib/ps.py
Martin Liska 724d15563d enhance ps
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>
2023-01-27 08:19:50 -08:00

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}")