From c63f340d90800895f007de64b7d2d14624263331 Mon Sep 17 00:00:00 2001 From: nthnluu Date: Sun, 28 Jan 2024 21:20:27 -0500 Subject: Created student weenix repository --- python/weenix/proc.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 python/weenix/proc.py (limited to 'python/weenix/proc.py') diff --git a/python/weenix/proc.py b/python/weenix/proc.py new file mode 100644 index 0000000..14ffa1d --- /dev/null +++ b/python/weenix/proc.py @@ -0,0 +1,90 @@ +import gdb +import weenix +import weenix.list + +_proc_states = { + 0 : "RUNNING", + 1 : "EXITED" +} + +class Proc: + + def __init__(self, val): + self._val = val + + def name(self): + return self._val["p_name"].string() + + def pid(self): + return int(self._val["p_pid"]) + + def state(self): + return _proc_states[int(self._val["p_state"])] + + def status(self): + return int(self._val["p_status"]) + + def parent(self): + proc = self._val["p_pproc"] + if (proc == 0): + return None + else: + return Proc(proc.dereference()) + + def children(self): + for child in weenix.list.load(self._val["p_children"], "struct proc", "p_child_link"): + yield Proc(child.item()) + + def str_short(self): + res = "{0:>5} ({1}) {2}".format(self.pid(), self.name(), self.state()) + if (self.state() == "EXITED"): + res = "{0} ({1})".format(res, self.status()) + if (self == curproc()): + res = "\033[1m{0}\033[22m".format(res) + return res + + def __eq__(self, other): + if (not isinstance(other, Proc)): + return False + else: + return self.pid() == other.pid() + + def __ne__(self, other): + return not self.__eq__(other) + + def __str__(self): + res = "PID: {0} ({1})\n".format(self.pid(), self.name()) + if (self == curproc()): + res = "\033[1m{0}\033[22m".format(res) + if (self.state() == "EXITED"): + res += "{0} ({1})\n".format(self.state(), self.status()) + else: + res += "{0}\n".format(self.state()) + if (self.parent() != None): + res += "Parent:\n" + res += "{0}\n".format(self.parent().str_short()) + if (len(list(self.children())) > 0): + res += "Children:\n" + for child in self.children(): + res += "{0}\n".format(child.str_short()) + return res + +def iter(): + for link in weenix.list.load("proc_list", "struct proc", "p_list_link"): + yield Proc(link.item()) + +def lookup(pid): + return Proc(weenix.eval_func("proc_lookup", pid).dereference()) + +def curproc(): + return Proc(gdb.parse_and_eval("curproc")) + +def str_proc_tree(proc=None, indent=""): + if (proc == None): + proc = lookup(0) + + res = "{0}| {1}\n".format(indent, proc.str_short()) + + for child in proc.children(): + res += str_proc_tree(child, indent+" ") + return res -- cgit v1.2.3-70-g09d2