forked from SkyLined/BugId
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbInstallAsJITDebugger.py
164 lines (148 loc) · 7.38 KB
/
fbInstallAsJITDebugger.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os;
import mRegistry;
from foConsoleLoader import foConsoleLoader;
from fsCreateBugIdCommandLine import fsCreateBugIdCommandLine;
from mColorsAndChars import *;
import mJITDebuggerRegistry;
oConsole = foConsoleLoader();
def fbInstallAsJITDebugger(asAdditionalArguments):
# To prevent the user from accidentally providing these arguments themselves, we scan the arguments provided by the
# user for these and report an error and return false if we find them. We will also check if the user has provided
# a report folder path.
bBugIdReportsFolderArgumentPresent = False;
asPauseArguments = [];
asFilteredAdditionalArguments = [];
for sArgument in asAdditionalArguments:
if sArgument.startswith("--"):
tsNameAndValue = sArgument[2:].split("=");
sName = tsNameAndValue[0];
if sName in ["pid", "pids", "handle-jit-event"]:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " You cannot use ",
COLOR_INFO, sArgument,
COLOR_NORMAL, " in combination with ",
COLOR_INFO, "-I",
COLOR_NORMAL, " in the arguments.",
);
return False;
if sName in ["p", "pause"]:
# We want to set the pause flag as early as possible to catch any exceptions.
asPauseArguments.append(sArgument);
# So we will remove
continue;
if sName in ["report", "reports", "report-folder", "reports-folder", "report-folder-path", "reports-folder-path", "sReportFolderPath"]:
bBugIdReportsFolderArgumentPresent = True;
asFilteredAdditionalArguments.append(sArgument);
# Two different registry paths must be adjusted individually on x64 systems - one for 64-bit applications, and a different one for 32-bit
# applications, as outlined in https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/enabling-postmortem-debugging
sBugIdCommandLine = fsCreateBugIdCommandLine(
(
# We want to set the pause flag as early as possible to catch any exceptions.
asPauseArguments or ["--pause"] # If no pause argument is provided, default to pausing
) + [
# When BugId gets started as a JIT debugger, we can use the string "%ld" in the arguments twice, which will get
# replaced by the process id and the JIT event number, in order. We will add arguments to that effect at the start
# of the arument list provided by the user (later).
# '%' gets escaped to avoid environment variable expansion. However, here we do want a '%' in the command line.
# We can use '%%' to do so '%':
"--pid=%%ld",
"--handle-jit-event=%%ld",
"--isa=x64",
] + (
# By default the python process hosting BugId will be run in the Windows System32 folder. We cannot save bug
# reports there. To make sure we will save bug reports somewhere we can write and where the user will likely find
# them, we will add an argument
["--reports-folder-path=%s\\BugId reports" % os.getenv("USERPROFILE")]
if not bBugIdReportsFolderArgumentPresent else []
) + (
asFilteredAdditionalArguments
)
);
# specifically adding --isa=x86 for the 32-bit version
sBugIdCommandLinex86 = fsCreateBugIdCommandLine(
(
# We want to set the pause flag as early as possible to catch any exceptions.
asPauseArguments or ["--pause"] # If no pause argument is provided, default to pausing
) + [
# When BugId gets started as a JIT debugger, we can use the string "%ld" in the arguments twice, which will get
# replaced by the process id and the JIT event number, in order. We will add arguments to that effect at the start
# of the arument list provided by the user (later).
# '%' gets escaped to avoid environment variable expansion. However, here we do want a '%' in the command line.
# We can use '%%' to do so '%':
"--pid=%%ld",
"--handle-jit-event=%%ld",
"--isa=x86",
] + (
# By default the python process hosting BugId will be run in the Windows System32 folder. We cannot save bug
# reports there. To make sure we will save bug reports somewhere we can write and where the user will likely find
# them, we will add an argument
["--reports-folder-path=%s\\BugId reports" % os.getenv("USERPROFILE")]
if not bBugIdReportsFolderArgumentPresent else []
) + (
asFilteredAdditionalArguments
)
);
oRegistryHiveKey = mRegistry.cRegistryHiveKey(
sHiveName = mJITDebuggerRegistry.sComandLineHiveName,
sKeyPath = mJITDebuggerRegistry.sComandLineKeyPath,
);
# adding JIT entry for 32-bit
oRegistryHiveKeyx86 = mRegistry.cRegistryHiveKey(
sHiveName = mJITDebuggerRegistry.sComandLineHiveName,
sKeyPath = mJITDebuggerRegistry.sComandLineKeyPathx86,
);
oConsole.fStatus("* Installing as JIT debugger 64-bit...");
bSettingsChanged = False;
for (sName, sValue) in {
"Auto": "1",
"Debugger": sBugIdCommandLine
}.items():
# Check if the value is already set correctly:
o0RegistryValue = oRegistryHiveKey.fo0GetValueForName(sValueName = "Debugger");
if o0RegistryValue and o0RegistryValue.sTypeName == "REG_SZ" and o0RegistryValue.xValue == sValue:
continue; # Yes; no need to modify it.
try:
oRegistryHiveKey.foSetValueForName(sValueName = sName, sTypeName = "SZ", xValue = sValue);
except WindowsError as oException:
if oException.winerror == 5:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " BugId cannot be installed as the default JIT debugger for 64-bit binaries..");
oConsole.fOutput(" Access to the relevant registry keys is denied.");
oConsole.fOutput(" Please try again with ", COLOR_INFO, "elevated priviledges", COLOR_NORMAL, ".");
return False;
raise;
bSettingsChanged = True;
oConsole.fOutput(
COLOR_OK, CHAR_OK,
COLOR_NORMAL, " BugId is ", "already" if bSettingsChanged else "now", " installed as the default JIT debugger for 64-bit binaries.");
oConsole.fOutput(" Command line: ", COLOR_INFO, sBugIdCommandLine);
# do the same for the 32-bit version
oConsole.fStatus("* Installing as JIT debugger 32-bit...");
bSettingsChanged = False;
for (sName, sValue) in {
"Auto": "1",
"Debugger": sBugIdCommandLinex86
}.items():
# Check if the value is already set correctly for 32-bit:
o0RegistryValuex86 = oRegistryHiveKey.fo0GetValueForName(sValueName = "Debugger");
if o0RegistryValuex86 and o0RegistryValuex86.sTypeName == "REG_SZ" and o0RegistryValuex86.xValue == sValue:
continue; # Yes; no need to modify it.
try:
oRegistryHiveKeyx86.foSetValueForName(sValueName = sName, sTypeName = "SZ", xValue = sValue);
except WindowsError as oException:
if oException.winerror == 5:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " BugId cannot be installed as the default JIT debugger for 32-bit binaries..");
oConsole.fOutput(" Access to the relevant registry keys is denied.");
oConsole.fOutput(" Please try again with ", COLOR_INFO, "elevated priviledges", COLOR_NORMAL, ".");
return False;
raise;
bSettingsChanged = True;
oConsole.fOutput(
COLOR_OK, CHAR_OK,
COLOR_NORMAL, " BugId is ", "already" if bSettingsChanged else "now", " installed as the default JIT debugger for 32-bit binaries.");
oConsole.fOutput(" Command line: ", COLOR_INFO, sBugIdCommandLinex86);
return True;