Sun, 16 Feb 2020 19:36:46 +0100
Started implementing the patching of the os module.
7422
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
3 | # Copyright (c) 2002 - 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7 | Module implementing a function to patch the process creation functions to |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | support multiprocess debugging. |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
10 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
11 | import sys |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
12 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
13 | from DebugUtilities import isWindowsPlatform, patchArguments, isPythonProgram |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
14 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
15 | _debugClient = None |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
16 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
17 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
18 | def patchModule(module, functionName, createFunction): |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
19 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
20 | Function to replace a function of a module with a modified one. |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
21 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | @param module reference to the module |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23 | @type types.ModuleType |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
24 | @param functionName name of the function to be replaced |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25 | @type str |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | @param createFunction function creating the replacement |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27 | @type types.FunctionType |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | if hasattr(module, functionName): |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | originalName = 'original_' + functionName |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | if not hasattr(module, originalName): |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | setattr(module, originalName, getattr(module, functionName)) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33 | setattr(module, functionName, createFunction(originalName)) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
34 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | def createExecl(originalName): |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | Function to patch the 'execl' process creation functions. |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | <ul> |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | <li>os.execl(path, arg0, arg1, ...)</li> |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | <li>os.execle(path, arg0, arg1, ..., env)</li> |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | <li>os.execlp(file, arg0, arg1, ...)</li> |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | <li>os.execlpe(file, arg0, arg1, ..., env)</li> |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | </ul> |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | def newExecl(path, *args): |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
49 | Function replacing the 'execl' functions of the os module. |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
50 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51 | print(args) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
52 | import os |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
53 | if ( |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
54 | _debugClient.debugging and |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
55 | _debugClient.multiprocessSupport |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
56 | ): |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57 | args = patchArguments(_debugClient, args) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
58 | if isPythonProgram(args[0]): |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
59 | path = args[0] |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
60 | print(args) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
61 | return getattr(os, originalName)(path, *args) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
62 | return newExecl |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
63 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
64 | def patchNewProcessFunctions(multiprocessEnabled, debugClient): |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
65 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
66 | Function to patch the process creation functions to support multiprocess |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
67 | debugging. |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
68 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
69 | @param multiprocessEnabled flag indicating multiprocess support |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
70 | @type bool |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
71 | @param debugClient reference to the debug client object |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | @type DebugClient |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
73 | """ |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
74 | global _debugClient |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
75 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
76 | if not multiprocessEnabled: |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
77 | # return without patching |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
78 | return |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
79 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
80 | import os |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
81 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82 | # patch 'os.exec...()' functions |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
83 | patchModule(os, "execl", createExecl) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
84 | patchModule(os, "execle", createExecl) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
85 | patchModule(os, "execlp", createExecl) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
86 | patchModule(os, "execlpe", createExecl) |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
87 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
88 | # TODO: implement patching of the various functions of the os module |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
89 | |
9a008ab4811b
Started implementing the patching of the os module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
90 | _debugClient = debugClient |