connect parent/child processes
Stefan Schuermans

Stefan Schuermans commited on 2020-05-23 15:41:03
Showing 1 changed files, with 26 additions and 7 deletions.

... ...
@@ -17,19 +17,33 @@ class Process():
17 17
         self._pid = pid
18 18
         self._begin = None
19 19
         self._end = None
20
+        self._parent = None
21
+        self._children = list()
22
+
23
+    def addChild(self, child: Process):
24
+        """
25
+        Add a child process.
26
+        """
27
+        self._children.append(child)
20 28
 
21 29
     def setBegin(self, proc_begin: uproctrace.parse.ProcBegin):
22 30
         """
23
-        set begin event of process
31
+        Set begin event of process.
24 32
         """
25 33
         self._begin = proc_begin
26 34
 
27 35
     def setEnd(self, proc_end: uproctrace.parse.ProcEnd):
28 36
         """
29
-        set end event of process
37
+        Set end event of process.
30 38
         """
31 39
         self._end = proc_end
32 40
 
41
+    def setParent(self, parent: Process):
42
+        """
43
+        Set parent process.
44
+        """
45
+        self._parent = parent
46
+
33 47
 
34 48
 class Processes(uproctrace.parse.Visitor):
35 49
     """
... ...
@@ -41,7 +55,7 @@ class Processes(uproctrace.parse.Visitor):
41 55
         Initialize processes from a trace file (f).
42 56
         """
43 57
         super().__init__()
44
-        self._processes = dict()  # pid -> Process
58
+        self._current_processes = dict()  # pid -> Process
45 59
         self._readTrace(proto_file)
46 60
 
47 61
     def _readTrace(self, proto_file):
... ...
@@ -58,20 +72,25 @@ class Processes(uproctrace.parse.Visitor):
58 72
         # new process
59 73
         proc = Process(proc_begin.pid)
60 74
         # add process to dict of current processes
61
-        self._processes[proc_begin.pid] = proc
75
+        self._current_processes[proc_begin.pid] = proc
62 76
         # set begin event of process
63 77
         proc.setBegin(proc_begin)
78
+        # connect to parent
79
+        if proc_begin.ppid in self._current_processes:
80
+            parent = self._current_processes[proc_begin.ppid]
81
+            proc.setParent(parent)
82
+            parent.addChild(proc)
64 83
 
65 84
     def visitProcEnd(self, proc_end: uproctrace.parse.ProcEnd):
66 85
         """
67 86
         Process a process end event.
68 87
         """
69 88
         # get process (or create it it is not known)
70
-        if proc_end.pid in self._processes:
71
-            proc = self._processes[proc_end.pid]
89
+        if proc_end.pid in self._current_processes:
90
+            proc = self._current_processes[proc_end.pid]
72 91
         else:
73 92
             proc = Process(proc_end.pid)
74 93
         # set end event of process
75 94
         proc.setEnd(proc_end)
76 95
         # remove process from dict of current processes (it ended)
77
-        self._processes[proc_end.pid] = None
96
+        self._current_processes[proc_end.pid] = None
78 97