aboutsummaryrefslogtreecommitdiff
path: root/python/weenix/proc.py
blob: 14ffa1d0c1f46093b90dc06195c603b85c53e252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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