Skip to content

Commit

Permalink
Get any process in gdb by pid
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscozdo committed May 22, 2021
1 parent 8ae9726 commit 4adc5b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions sys/debug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,7 +35,7 @@ def addPrettyPrinters():

# Functions
CurrentThread()
CurrentProcess()
ShowProcess()

# Events
gdb.events.stop.connect(stop_handler)
30 changes: 24 additions & 6 deletions sys/debug/proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand 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."""
Expand All @@ -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

0 comments on commit 4adc5b6

Please sign in to comment.