Skip to content

Commit 48c09a7

Browse files
committed
[lldb] Run python formatter on crashlog & scripted process (nfc)
Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent bcd6a35 commit 48c09a7

File tree

4 files changed

+796
-187
lines changed

4 files changed

+796
-187
lines changed

lldb/examples/python/crashlog.py

Lines changed: 116 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -681,22 +681,46 @@ def parse_asi_backtrace(self, thread, bt):
681681
print("error: can't parse application specific backtrace.")
682682
return False
683683

684-
frame_id = frame_img_name = frame_addr = frame_symbol = frame_offset = frame_file = frame_line = frame_column = None
684+
frame_id = (
685+
frame_img_name
686+
) = (
687+
frame_addr
688+
) = (
689+
frame_symbol
690+
) = frame_offset = frame_file = frame_line = frame_column = None
685691

686692
if len(frame_match.groups()) == 3:
687693
# Get the image UUID from the frame image name.
688694
(frame_id, frame_img_name, frame_addr) = frame_match.groups()
689695
elif len(frame_match.groups()) == 5:
690-
(frame_id, frame_img_name, frame_addr,
691-
frame_symbol, frame_offset) = frame_match.groups()
696+
(
697+
frame_id,
698+
frame_img_name,
699+
frame_addr,
700+
frame_symbol,
701+
frame_offset,
702+
) = frame_match.groups()
692703
elif len(frame_match.groups()) == 7:
693-
(frame_id, frame_img_name, frame_addr,
694-
frame_symbol, frame_offset,
695-
frame_file, frame_line) = frame_match.groups()
704+
(
705+
frame_id,
706+
frame_img_name,
707+
frame_addr,
708+
frame_symbol,
709+
frame_offset,
710+
frame_file,
711+
frame_line,
712+
) = frame_match.groups()
696713
elif len(frame_match.groups()) == 8:
697-
(frame_id, frame_img_name, frame_addr,
698-
frame_symbol, frame_offset,
699-
frame_file, frame_line, frame_column) = frame_match.groups()
714+
(
715+
frame_id,
716+
frame_img_name,
717+
frame_addr,
718+
frame_symbol,
719+
frame_offset,
720+
frame_file,
721+
frame_line,
722+
frame_column,
723+
) = frame_match.groups()
700724

701725
thread.add_ident(frame_img_name)
702726
if frame_img_name not in self.crashlog.idents:
@@ -769,24 +793,24 @@ class CrashLogParseMode:
769793

770794

771795
class TextCrashLogParser(CrashLogParser):
772-
parent_process_regex = re.compile(r'^Parent Process:\s*(.*)\[(\d+)\]')
773-
thread_state_regex = re.compile(r'^Thread \d+ crashed with')
774-
thread_instrs_regex = re.compile(r'^Thread \d+ instruction stream')
775-
thread_regex = re.compile(r'^Thread (\d+).*:')
776-
app_backtrace_regex = re.compile(r'^Application Specific Backtrace (\d+).*:')
796+
parent_process_regex = re.compile(r"^Parent Process:\s*(.*)\[(\d+)\]")
797+
thread_state_regex = re.compile(r"^Thread \d+ crashed with")
798+
thread_instrs_regex = re.compile(r"^Thread \d+ instruction stream")
799+
thread_regex = re.compile(r"^Thread (\d+).*:")
800+
app_backtrace_regex = re.compile(r"^Application Specific Backtrace (\d+).*:")
777801

778802
class VersionRegex:
779-
version = r'\(.+\)|(?:arm|x86_)[0-9a-z]+'
803+
version = r"\(.+\)|(?:arm|x86_)[0-9a-z]+"
780804

781805
class FrameRegex(VersionRegex):
782806
@classmethod
783807
def get(cls):
784-
index = r'^(\d+)\s+'
785-
img_name = r'(.+?)\s+'
786-
version = r'(?:' + super().version + r'\s+)?'
787-
address = r'(0x[0-9a-fA-F]{4,})' # 4 digits or more
808+
index = r"^(\d+)\s+"
809+
img_name = r"(.+?)\s+"
810+
version = r"(?:" + super().version + r"\s+)?"
811+
address = r"(0x[0-9a-fA-F]{4,})" # 4 digits or more
788812

789-
symbol = """
813+
symbol = """
790814
(?:
791815
[ ]+
792816
(?P<symbol>.+)
@@ -804,24 +828,28 @@ def get(cls):
804828
)?
805829
"""
806830

807-
return re.compile(index + img_name + version + address + symbol,
808-
flags=re.VERBOSE)
831+
return re.compile(
832+
index + img_name + version + address + symbol, flags=re.VERBOSE
833+
)
809834

810835
frame_regex = FrameRegex.get()
811-
null_frame_regex = re.compile(r'^\d+\s+\?\?\?\s+0{4,} +')
812-
image_regex_uuid = re.compile(r'(0x[0-9a-fA-F]+)' # img_lo
813-
r'\s+-\s+' # -
814-
r'(0x[0-9a-fA-F]+)\s+' # img_hi
815-
r'[+]?(.+?)\s+' # img_name
816-
r'(?:(' +
817-
VersionRegex.version + # img_version
818-
r')\s+)?'
819-
r'(?:<([-0-9a-fA-F]+)>\s+)?' # img_uuid
820-
r'(\?+|/.*)' # img_path
821-
)
822-
exception_type_regex = re.compile(r'^Exception Type:\s+(EXC_[A-Z_]+)(?:\s+\((.*)\))?')
823-
exception_codes_regex = re.compile(r'^Exception Codes:\s+(0x[0-9a-fA-F]+),\s*(0x[0-9a-fA-F]+)')
824-
exception_extra_regex = re.compile(r'^Exception\s+.*:\s+(.*)')
836+
null_frame_regex = re.compile(r"^\d+\s+\?\?\?\s+0{4,} +")
837+
image_regex_uuid = re.compile(
838+
r"(0x[0-9a-fA-F]+)" # img_lo
839+
r"\s+-\s+" # -
840+
r"(0x[0-9a-fA-F]+)\s+" # img_hi
841+
r"[+]?(.+?)\s+" # img_name
842+
r"(?:(" + VersionRegex.version + r")\s+)?" # img_version
843+
r"(?:<([-0-9a-fA-F]+)>\s+)?" # img_uuid
844+
r"(\?+|/.*)" # img_path
845+
)
846+
exception_type_regex = re.compile(
847+
r"^Exception Type:\s+(EXC_[A-Z_]+)(?:\s+\((.*)\))?"
848+
)
849+
exception_codes_regex = re.compile(
850+
r"^Exception Codes:\s+(0x[0-9a-fA-F]+),\s*(0x[0-9a-fA-F]+)"
851+
)
852+
exception_extra_regex = re.compile(r"^Exception\s+.*:\s+(.*)")
825853

826854
def __init__(self, debugger, path, verbose):
827855
super().__init__(debugger, path, verbose)
@@ -1011,22 +1039,44 @@ def parse_thread(self, line):
10111039
print('error: frame regex failed for line: "%s"' % line)
10121040
return
10131041

1014-
frame_id = frame_img_name = frame_addr = frame_symbol = frame_offset = frame_file = frame_line = frame_column = None
1042+
frame_id = (
1043+
frame_img_name
1044+
) = (
1045+
frame_addr
1046+
) = frame_symbol = frame_offset = frame_file = frame_line = frame_column = None
10151047

10161048
if len(frame_match.groups()) == 3:
10171049
# Get the image UUID from the frame image name.
10181050
(frame_id, frame_img_name, frame_addr) = frame_match.groups()
10191051
elif len(frame_match.groups()) == 5:
1020-
(frame_id, frame_img_name, frame_addr,
1021-
frame_symbol, frame_offset) = frame_match.groups()
1052+
(
1053+
frame_id,
1054+
frame_img_name,
1055+
frame_addr,
1056+
frame_symbol,
1057+
frame_offset,
1058+
) = frame_match.groups()
10221059
elif len(frame_match.groups()) == 7:
1023-
(frame_id, frame_img_name, frame_addr,
1024-
frame_symbol, frame_offset,
1025-
frame_file, frame_line) = frame_match.groups()
1060+
(
1061+
frame_id,
1062+
frame_img_name,
1063+
frame_addr,
1064+
frame_symbol,
1065+
frame_offset,
1066+
frame_file,
1067+
frame_line,
1068+
) = frame_match.groups()
10261069
elif len(frame_match.groups()) == 8:
1027-
(frame_id, frame_img_name, frame_addr,
1028-
frame_symbol, frame_offset,
1029-
frame_file, frame_line, frame_column) = frame_match.groups()
1070+
(
1071+
frame_id,
1072+
frame_img_name,
1073+
frame_addr,
1074+
frame_symbol,
1075+
frame_offset,
1076+
frame_file,
1077+
frame_line,
1078+
frame_column,
1079+
) = frame_match.groups()
10301080

10311081
self.thread.add_ident(frame_img_name)
10321082
if frame_img_name not in self.crashlog.idents:
@@ -1057,15 +1107,24 @@ def parse_thread(self, line):
10571107
def parse_images(self, line):
10581108
image_match = self.image_regex_uuid.search(line)
10591109
if image_match:
1060-
(img_lo, img_hi, img_name, img_version,
1061-
img_uuid, img_path) = image_match.groups()
1062-
1063-
image = self.crashlog.DarwinImage(int(img_lo, 0), int(img_hi, 0),
1064-
img_name.strip(),
1065-
img_version.strip()
1066-
if img_version else "",
1067-
uuid.UUID(img_uuid), img_path,
1068-
self.verbose)
1110+
(
1111+
img_lo,
1112+
img_hi,
1113+
img_name,
1114+
img_version,
1115+
img_uuid,
1116+
img_path,
1117+
) = image_match.groups()
1118+
1119+
image = self.crashlog.DarwinImage(
1120+
int(img_lo, 0),
1121+
int(img_hi, 0),
1122+
img_name.strip(),
1123+
img_version.strip() if img_version else "",
1124+
uuid.UUID(img_uuid),
1125+
img_path,
1126+
self.verbose,
1127+
)
10691128
unqualified_img_name = os.path.basename(img_path)
10701129
if unqualified_img_name in self.symbols:
10711130
for symbol in self.symbols[unqualified_img_name]:
@@ -1326,7 +1385,9 @@ def load_crashlog_in_scripted_process(debugger, crash_log_file, options, result)
13261385
if target is None or not target.IsValid():
13271386
arch = crashlog.process_arch
13281387
if not arch:
1329-
raise InteractiveCrashLogException("couldn't create find the architecture to create the target")
1388+
raise InteractiveCrashLogException(
1389+
"couldn't create find the architecture to create the target"
1390+
)
13301391
target = debugger.CreateTargetWithFileAndArch(None, arch)
13311392
# 4. Fail
13321393
if target is None or not target.IsValid():

lldb/examples/python/scripted_process/crashlog_scripted_process.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from lldb.plugins.scripted_process import ScriptedProcess
77
from lldb.plugins.scripted_process import ScriptedThread
88

9-
from lldb.macosx.crashlog import CrashLog,CrashLogParser
9+
from lldb.macosx.crashlog import CrashLog, CrashLogParser
10+
1011

1112
class CrashLogScriptedProcess(ScriptedProcess):
1213
def set_crashlog(self, crashlog):
@@ -23,9 +24,9 @@ def set_crashlog(self, crashlog):
2324
self.loaded_images = []
2425
self.exception = self.crashlog.exception
2526
self.app_specific_thread = None
26-
if hasattr(self.crashlog, 'asi'):
27-
self.metadata['asi'] = self.crashlog.asi
28-
if hasattr(self.crashlog, 'asb'):
27+
if hasattr(self.crashlog, "asi"):
28+
self.metadata["asi"] = self.crashlog.asi
29+
if hasattr(self.crashlog, "asb"):
2930
self.extended_thread_info = self.crashlog.asb
3031

3132
if self.load_all_images:
@@ -51,7 +52,10 @@ def set_crashlog(self, crashlog):
5152
self.loaded_images.append(image)
5253

5354
for thread in self.crashlog.threads:
54-
if hasattr(thread, 'app_specific_backtrace') and thread.app_specific_backtrace:
55+
if (
56+
hasattr(thread, "app_specific_backtrace")
57+
and thread.app_specific_backtrace
58+
):
5559
# We don't want to include the Application Specific Backtrace
5660
# Thread into the Scripted Process' Thread list.
5761
# Instead, we will try to extract the stackframe pcs from the
@@ -61,14 +65,12 @@ def set_crashlog(self, crashlog):
6165

6266
self.threads[thread.index] = CrashLogScriptedThread(self, None, thread)
6367

64-
6568
if self.app_specific_thread:
66-
self.extended_thread_info = \
67-
CrashLogScriptedThread.resolve_stackframes(self.app_specific_thread,
68-
self.addr_mask,
69-
self.target)
69+
self.extended_thread_info = CrashLogScriptedThread.resolve_stackframes(
70+
self.app_specific_thread, self.addr_mask, self.target
71+
)
7072

71-
def __init__(self, exe_ctx: lldb.SBExecutionContext, args : lldb.SBStructuredData):
73+
def __init__(self, exe_ctx: lldb.SBExecutionContext, args: lldb.SBStructuredData):
7274
super().__init__(exe_ctx, args)
7375

7476
if not self.target or not self.target.IsValid():
@@ -99,7 +101,9 @@ def __init__(self, exe_ctx: lldb.SBExecutionContext, args : lldb.SBStructuredDat
99101
self.exception = None
100102
self.extended_thread_info = None
101103

102-
def read_memory_at_address(self, addr: int, size: int, error: lldb.SBError) -> lldb.SBData:
104+
def read_memory_at_address(
105+
self, addr: int, size: int, error: lldb.SBError
106+
) -> lldb.SBData:
103107
# NOTE: CrashLogs don't contain any memory.
104108
return lldb.SBData()
105109

@@ -120,16 +124,21 @@ def get_scripted_thread_plugin(self):
120124
def get_process_metadata(self):
121125
return self.metadata
122126

127+
123128
class CrashLogScriptedThread(ScriptedThread):
124129
def create_register_ctx(self):
125130
if not self.has_crashed:
126-
return dict.fromkeys([*map(lambda reg: reg['name'], self.register_info['registers'])] , 0)
131+
return dict.fromkeys(
132+
[*map(lambda reg: reg["name"], self.register_info["registers"])], 0
133+
)
127134

128135
if not self.backing_thread or not len(self.backing_thread.registers):
129-
return dict.fromkeys([*map(lambda reg: reg['name'], self.register_info['registers'])] , 0)
136+
return dict.fromkeys(
137+
[*map(lambda reg: reg["name"], self.register_info["registers"])], 0
138+
)
130139

131-
for reg in self.register_info['registers']:
132-
reg_name = reg['name']
140+
for reg in self.register_info["registers"]:
141+
reg_name = reg["name"]
133142
if reg_name in self.backing_thread.registers:
134143
self.register_ctx[reg_name] = self.backing_thread.registers[reg_name]
135144
else:
@@ -141,25 +150,24 @@ def resolve_stackframes(thread, addr_mask, target):
141150
frames = []
142151
for frame in thread.frames:
143152
frame_pc = frame.pc & addr_mask
144-
pc = frame_pc if frame.index == 0 or frame_pc == 0 else frame_pc - 1
153+
pc = frame_pc if frame.index == 0 or frame_pc == 0 else frame_pc - 1
145154
sym_addr = lldb.SBAddress()
146155
sym_addr.SetLoadAddress(pc, target)
147156
if not sym_addr.IsValid():
148157
continue
149158
frames.append({"idx": frame.index, "pc": pc})
150159
return frames
151160

152-
153161
def create_stackframes(self):
154162
if not (self.scripted_process.load_all_images or self.has_crashed):
155163
return None
156164

157165
if not self.backing_thread or not len(self.backing_thread.frames):
158166
return None
159167

160-
self.frames = CrashLogScriptedThread.resolve_stackframes(self.backing_thread,
161-
self.scripted_process.addr_mask,
162-
self.target)
168+
self.frames = CrashLogScriptedThread.resolve_stackframes(
169+
self.backing_thread, self.scripted_process.addr_mask, self.target
170+
)
163171

164172
return self.frames
165173

@@ -174,7 +182,7 @@ def __init__(self, process, args, crashlog_thread):
174182
else:
175183
self.name = self.backing_thread.name
176184
self.queue = self.backing_thread.queue
177-
self.has_crashed = (self.scripted_process.crashed_thread_idx == self.idx)
185+
self.has_crashed = self.scripted_process.crashed_thread_idx == self.idx
178186
self.create_stackframes()
179187

180188
def get_state(self):
@@ -184,21 +192,22 @@ def get_state(self):
184192

185193
def get_stop_reason(self) -> Dict[str, Any]:
186194
if not self.has_crashed:
187-
return { "type": lldb.eStopReasonNone }
195+
return {"type": lldb.eStopReasonNone}
188196
# TODO: Investigate what stop reason should be reported when crashed
189-
stop_reason = { "type": lldb.eStopReasonException, "data": { }}
197+
stop_reason = {"type": lldb.eStopReasonException, "data": {}}
190198
if self.scripted_process.exception:
191-
stop_reason['data']['mach_exception'] = self.scripted_process.exception
199+
stop_reason["data"]["mach_exception"] = self.scripted_process.exception
192200
return stop_reason
193201

194202
def get_register_context(self) -> str:
195203
if not self.register_ctx:
196204
self.register_ctx = self.create_register_ctx()
197205

198-
return struct.pack("{}Q".format(len(self.register_ctx)), *self.register_ctx.values())
206+
return struct.pack(
207+
"{}Q".format(len(self.register_ctx)), *self.register_ctx.values()
208+
)
199209

200210
def get_extended_info(self):
201-
if (self.has_crashed):
211+
if self.has_crashed:
202212
self.extended_info = self.scripted_process.extended_thread_info
203213
return self.extended_info
204-

0 commit comments

Comments
 (0)