1 # Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
10 SYZYASAN_STACK_FRAME_PATTERN
= re
.compile(
11 r
'(CF: )?(.*?)( \(FPO: .*\) )?( \(CONV: .*\) )?\[(.*) @ (\d+)\]')
12 FILE_PATH_AND_LINE_PATTERN
= re
.compile(r
'(.*?):(\d+)(:\d+)?')
15 class StackFrame(object):
16 """Represents a frame in stacktrace.
19 index: An index of the stack frame.
20 component_path: The path of the component this frame represents.
21 component_name: The name of the component this frame represents.
22 file_name: The name of the file that crashed.
23 function: The function that caused the crash.
24 file_path: The path of the crashed file.
25 crashed_line_range: The line of the file that caused the crash.
28 def __init__(self
, stack_frame_index
, component_path
, component_name
,
29 file_name
, function
, file_path
, crashed_line_range
):
30 self
.index
= stack_frame_index
31 self
.component_path
= component_path
32 self
.component_name
= component_name
33 self
.file_name
= file_name
34 self
.function
= function
35 self
.file_path
= file_path
36 self
.crashed_line_range
= crashed_line_range
39 class CallStack(object):
40 """Represents a call stack within a stacktrace.
42 It is a list of StackFrame object, and the object keeps track of whether
43 the stack is crash stack, freed or previously-allocated.
46 def __init__(self
, stack_priority
):
48 self
.priority
= stack_priority
50 def Add(self
, stacktrace_line
):
51 self
.frame_list
.append(stacktrace_line
)
53 def GetTopNFrames(self
, n
):
54 return self
.frame_list
[:n
]
57 class Stacktrace(object):
58 """Represents Stacktrace object.
60 Contains a list of callstacks, because one stacktrace might have more than
64 def __init__(self
, stacktrace
, build_type
, parsed_deps
):
65 self
.stack_list
= None
66 self
.ParseStacktrace(stacktrace
, build_type
, parsed_deps
)
68 def ParseStacktrace(self
, stacktrace
, build_type
, parsed_deps
):
69 """Parses stacktrace and normalizes it.
71 If there are multiple callstacks within the stacktrace,
72 it will parse each of them separately, and store them in the stack_list
76 stacktrace: A string containing stacktrace.
77 build_type: A string containing the build type of the crash.
78 parsed_deps: A parsed DEPS file to normalize path with.
80 # If the passed in string is empty, the object does not represent anything.
83 # Reset the stack list.
85 reached_new_callstack
= False
86 # Note that we do not need exact stack frame index, we only need relative
87 # position of a frame within a callstack. The reason for not extracting
88 # index from a line is that some stack frames do not have index.
90 current_stack
= CallStack(-1)
92 for line
in stacktrace
:
94 (is_new_callstack
, stack_priority
) = self
.__IsStartOfNewCallStack
(
97 # If this callstack is crash stack, update the boolean.
98 if not reached_new_callstack
:
99 reached_new_callstack
= True
100 current_stack
= CallStack(stack_priority
)
102 # If this is from freed or allocation, add the callstack we have
103 # to the list of callstacks, and increment the stack priority.
105 stack_frame_index
= 0
106 if current_stack
and current_stack
.frame_list
:
107 self
.stack_list
.append(current_stack
)
108 current_stack
= CallStack(stack_priority
)
110 # Generate stack frame object from the line.
111 parsed_stack_frame
= self
.__GenerateStackFrame
(
112 stack_frame_index
, line
, build_type
, parsed_deps
)
114 # If the line does not represent the stack frame, ignore this line.
115 if not parsed_stack_frame
:
118 # Add the parsed stack frame object to the current stack.
119 current_stack
.Add(parsed_stack_frame
)
120 stack_frame_index
+= 1
122 # Add the current callstack only if there are frames in it.
123 if current_stack
and current_stack
.frame_list
:
124 self
.stack_list
.append(current_stack
)
126 def __IsStartOfNewCallStack(self
, line
, build_type
):
127 """Check if this line is the start of the new callstack.
129 Since each builds have different format of stacktrace, the logic for
130 checking the line for all builds is handled in here.
133 line: Line to check for.
134 build_type: The name of the build.
137 True if the line is the start of new callstack, False otherwise. If True,
138 it also returns the priority of the line.
140 if 'syzyasan' in build_type
:
141 # In syzyasan build, new stack starts with 'crash stack:',
142 # 'freed stack:', etc.
143 callstack_start_pattern
= re
.compile(r
'^(.*) stack:$')
144 match
= callstack_start_pattern
.match(line
)
146 # If the line matches the callstack start pattern.
148 # Check the type of the new match.
149 stack_type
= match
.group(1)
151 # Crash stack gets priority 0.
152 if stack_type
== 'Crash':
155 # Other callstacks all get priority 1.
159 elif 'tsan' in build_type
:
160 # Create patterns for each callstack type.
161 crash_callstack_start_pattern1
= re
.compile(
162 r
'^(Read|Write) of size \d+')
164 crash_callstack_start_pattern2
= re
.compile(
165 r
'^[A-Z]+: ThreadSanitizer')
167 allocation_callstack_start_pattern
= re
.compile(
168 r
'^Previous (write|read) of size \d+')
170 location_callstack_start_pattern
= re
.compile(
171 r
'^Location is heap block of size \d+')
173 # Crash stack gets priority 0.
174 if (crash_callstack_start_pattern1
.match(line
) or
175 crash_callstack_start_pattern2
.match(line
)):
178 # All other stacks get priority 1.
179 if allocation_callstack_start_pattern
.match(line
):
182 if location_callstack_start_pattern
.match(line
):
186 # In asan and other build types, crash stack can start
187 # in two different ways.
188 crash_callstack_start_pattern1
= re
.compile(r
'^==\d+== ?[A-Z]+:')
189 crash_callstack_start_pattern2
= re
.compile(
190 r
'^(READ|WRITE) of size \d+ at')
191 crash_callstack_start_pattern3
= re
.compile(r
'^backtrace:')
193 freed_callstack_start_pattern
= re
.compile(
194 r
'^freed by thread T\d+ (.* )?here:')
196 allocation_callstack_start_pattern
= re
.compile(
197 r
'^previously allocated by thread T\d+ (.* )?here:')
199 other_callstack_start_pattern
= re
.compile(
200 r
'^Thread T\d+ (.* )?created by')
202 # Crash stack gets priority 0.
203 if (crash_callstack_start_pattern1
.match(line
) or
204 crash_callstack_start_pattern2
.match(line
) or
205 crash_callstack_start_pattern3
.match(line
)):
208 # All other callstack gets priority 1.
209 if freed_callstack_start_pattern
.match(line
):
212 if allocation_callstack_start_pattern
.match(line
):
215 if other_callstack_start_pattern
.match(line
):
218 # If the line does not match any pattern, return false and a dummy for
222 def __GenerateStackFrame(self
, stack_frame_index
, line
, build_type
,
224 """Extracts information from a line in stacktrace.
227 stack_frame_index: A stack frame index of this line.
228 line: A stacktrace string to extract data from.
229 build_type: A string containing the build type
230 of this crash (e.g. linux_asan_chrome_mp).
231 parsed_deps: A parsed DEPS file to normalize path with.
234 A triple containing the name of the function, the path of the file and
235 the crashed line number.
237 line_parts
= line
.split()
240 if 'syzyasan' in build_type
:
241 stack_frame_match
= SYZYASAN_STACK_FRAME_PATTERN
.match(line
)
243 if not stack_frame_match
:
245 file_path
= stack_frame_match
.group(5)
246 crashed_line_range
= [int(stack_frame_match
.group(6))]
247 function
= stack_frame_match
.group(2)
250 if not line_parts
[0].startswith('#'):
253 if 'tsan' in build_type
:
254 file_path_and_line
= line_parts
[-2]
255 function
= ' '.join(line_parts
[1:-2])
257 file_path_and_line
= line_parts
[-1]
258 function
= ' '.join(line_parts
[3:-1])
260 # Get file path and line info from the line.
261 file_path_and_line_match
= FILE_PATH_AND_LINE_PATTERN
.match(
264 # Return None if the file path information is not available
265 if not file_path_and_line_match
:
268 file_path
= file_path_and_line_match
.group(1)
270 # Get the crashed line range. For example, file_path:line_number:range.
271 crashed_line_range_num
= file_path_and_line_match
.group(3)
273 if crashed_line_range_num
:
275 crashed_line_range_num
= int(crashed_line_range_num
[1:])
277 crashed_line_range_num
= 0
279 crashed_line_number
= int(file_path_and_line_match
.group(2))
280 # For example, 655:1 has crashed lines 655 and 656.
281 crashed_line_range
= \
282 range(crashed_line_number
,
283 crashed_line_number
+ crashed_line_range_num
+ 1)
285 # Return None if the line is malformed.
291 # Normalize the file path so that it can be compared to repository path.
292 (component_path
, component_name
, file_path
) = (
293 crash_utils
.NormalizePath(file_path
, parsed_deps
))
295 # Return a new stack frame object with the parsed information.
296 file_name
= file_path
.split('/')[-1]
298 # If we have the common stack frame index pattern, then use it
299 # since it is more reliable.
300 index_match
= re
.match('\s*#(\d+)\s.*', line
)
302 stack_frame_index
= int(index_match
.group(1))
304 return StackFrame(stack_frame_index
, component_path
, component_name
,
305 file_name
, function
, file_path
, crashed_line_range
)
307 def __getitem__(self
, index
):
308 return self
.stack_list
[index
]
310 def GetCrashStack(self
):
311 """Returns the callstack with the highest priority.
313 Crash stack has priority 0, and allocation/freed/other thread stacks
317 The highest priority callstack in the stacktrace.
319 sorted_stacklist
= sorted(self
.stack_list
,
320 key
=lambda callstack
: callstack
.priority
)
321 return sorted_stacklist
[0]