aboutsummaryrefslogtreecommitdiff
path: root/python/weenix/proc.py
diff options
context:
space:
mode:
authornthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
committernthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
commitc63f340d90800895f007de64b7d2d14624263331 (patch)
tree2c0849fa597dd6da831c8707b6f2603403778d7b /python/weenix/proc.py
Created student weenix repository
Diffstat (limited to 'python/weenix/proc.py')
-rw-r--r--python/weenix/proc.py90
1 files changed, 90 insertions, 0 deletions
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