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