diff --git a/sys/debug/__init__.py b/sys/debug/__init__.py index 545f1b6960..79fdf3e8d6 100644 --- a/sys/debug/__init__.py +++ b/sys/debug/__init__.py @@ -5,7 +5,7 @@ from .kgmon import Kgmon from .klog import Klog from .ktrace import Ktrace -from .proc import Kprocess, Process, CurrentProcess +from .proc import Kprocess, Process, ShowProcess from .struct import BinTime from .sync import CondVar, Mutex from .thread import Kthread, Thread, CurrentThread @@ -35,7 +35,7 @@ def addPrettyPrinters(): # Functions CurrentThread() -CurrentProcess() +ShowProcess() # Events gdb.events.stop.connect(stop_handler) diff --git a/sys/debug/proc.py b/sys/debug/proc.py index 8897903045..96f11bedb3 100644 --- a/sys/debug/proc.py +++ b/sys/debug/proc.py @@ -22,6 +22,15 @@ def from_current(cls): def from_pointer(cls, ptr): return cls(gdb.parse_and_eval('(struct proc *)' + ptr).dereference()) + @classmethod + def from_pid(cls, pid): + alive = list(TailQueue(global_var('proc_list'), 'p_all')) + dead = list(TailQueue(global_var('zombie_list'), 'p_all')) + for p in alive + dead: + if p['p_pid'] == pid: + return cls(p) + return None + @classmethod def list_all(cls): alive = TailQueue(global_var('proc_list'), 'p_all') @@ -31,6 +40,9 @@ def list_all(cls): def __repr__(self): return 'proc{pid=%d}' % self.p_pid + def address(self): + return self._obj.address + class Kprocess(SimpleCommand): """List all processes.""" @@ -47,11 +59,17 @@ def __call__(self, args): print(table) -class CurrentProcess(gdb.Function): - """Return address of currently running process.""" +class ShowProcess(gdb.Function): + """Return address of process of given pid (default current process).""" def __init__(self): - super().__init__('process') - - def invoke(self): - return Process.current() + super().__init__('proc') + + def invoke(self, pid=-1): + if pid == -1: + return Process.current() + p = Process.from_pid(pid) + if p: + return p.address() + print(f"No process of pid={pid}") + return 0