OLD | NEW |
1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | |
6 | |
7 | 5 |
8 class FileDictionary(object): | 6 class FileDictionary(object): |
9 """Maps file in a stacktrace to its crash information. | 7 """Maps file in a stacktrace to its crash information. |
10 | 8 |
11 It maps file to another dictionary, which maps the file's path to crashed | 9 It maps file to another dictionary, which maps the file's path to crashed |
12 lines, stack frame indices and crashed functions. | 10 lines, stack frame indices and crashed functions. |
13 """ | 11 """ |
14 | 12 |
15 def __init__(self): | 13 def __init__(self): |
16 """Initializes the file dictionary.""" | 14 """Initializes the file dictionary.""" |
17 self.file_dict = {} | 15 self.file_dict = {} |
18 | 16 |
19 def AddFile(self, file_name, file_path, crashed_line_number, | 17 def AddFile(self, file_path, crashed_line_range, stack_frame_index, |
20 stack_frame_index, function): | 18 function): |
21 """Adds file and its crash information to the map. | 19 """Adds file and its crash information to the map. |
22 | 20 |
23 Args: | 21 Args: |
24 file_name: The name of the crashed file. | |
25 file_path: The path of the crashed file. | 22 file_path: The path of the crashed file. |
26 crashed_line_number: The crashed line of the file. | 23 crashed_line_range: The crashed line of the file. |
27 stack_frame_index: The file's position in the callstack. | 24 stack_frame_index: The file's position in the callstack. |
28 function: The name of the crashed function. | 25 function: The name of the crashed function. |
29 """ | 26 """ |
30 # Populate the dictionary if this file/path has not been added before. | 27 # Populate the dictionary if this file path has not been added before. |
31 if file_name not in self.file_dict: | 28 if file_path not in self.file_dict: |
32 self.file_dict[file_name] = {} | 29 self.file_dict[file_path] = {} |
33 | 30 self.file_dict[file_path]['line_numbers'] = [] |
34 if file_path not in self.file_dict[file_name]: | 31 self.file_dict[file_path]['stack_frame_indices'] = [] |
35 self.file_dict[file_name][file_path] = {} | 32 self.file_dict[file_path]['function'] = [] |
36 self.file_dict[file_name][file_path]['line_numbers'] = [] | |
37 self.file_dict[file_name][file_path]['stack_frame_indices'] = [] | |
38 self.file_dict[file_name][file_path]['function'] = [] | |
39 | 33 |
40 # Add the crashed line, frame index and function name. | 34 # Add the crashed line, frame index and function name. |
41 self.file_dict[file_name][file_path]['line_numbers'].append( | 35 self.file_dict[file_path]['line_numbers'].append( |
42 crashed_line_number) | 36 crashed_line_range) |
43 self.file_dict[file_name][file_path]['stack_frame_indices'].append( | 37 self.file_dict[file_path]['stack_frame_indices'].append( |
44 stack_frame_index) | 38 stack_frame_index) |
45 self.file_dict[file_name][file_path]['function'].append(function) | 39 self.file_dict[file_path]['function'].append(function) |
46 | |
47 def GetPathDic(self, file_name): | |
48 """Returns file's path and crash information.""" | |
49 return self.file_dict[file_name] | |
50 | 40 |
51 def GetCrashedLineNumbers(self, file_path): | 41 def GetCrashedLineNumbers(self, file_path): |
52 """Returns crashed line numbers given a file path.""" | 42 """Returns crashed line numbers given a file path.""" |
53 file_name = os.path.basename(file_path) | 43 return self.file_dict[file_path]['line_numbers'] |
54 return self.file_dict[file_name][file_path]['line_numbers'] | |
55 | 44 |
56 def GetCrashStackFrameindex(self, file_path): | 45 def GetCrashStackFrameIndices(self, file_path): |
57 """Returns stack frame indices given a file path.""" | 46 """Returns stack frame indices given a file path.""" |
58 file_name = os.path.basename(file_path) | 47 return self.file_dict[file_path]['stack_frame_indices'] |
59 return self.file_dict[file_name][file_path]['stack_frame_indices'] | |
60 | 48 |
61 def GetCrashFunction(self, file_path): | 49 def GetCrashFunctions(self, file_path): |
62 """Returns list of crashed functions given a file path.""" | 50 """Returns list of crashed functions given a file path.""" |
63 file_name = os.path.basename(file_path) | 51 return self.file_dict[file_path]['function'] |
64 return self.file_dict[file_name][file_path]['function'] | |
65 | 52 |
66 def __iter__(self): | 53 def __iter__(self): |
67 return iter(self.file_dict) | 54 return iter(self.file_dict) |
68 | 55 |
69 | 56 |
70 class ComponentDictionary(object): | 57 class ComponentDictionary(object): |
71 """Represents a file dictionary. | 58 """Represents a file dictionary. |
72 | 59 |
73 It maps each component path to a file dictionary. | 60 It maps each component path to a file dictionary. |
74 """ | 61 """ |
(...skipping 17 matching lines...) Expand all Loading... |
92 """Generates file dictionary, given an instance of StackFrame list.""" | 79 """Generates file dictionary, given an instance of StackFrame list.""" |
93 # Iterate through the list of stackframe objects. | 80 # Iterate through the list of stackframe objects. |
94 for stack_frame in stack_frame_list: | 81 for stack_frame in stack_frame_list: |
95 # If the component of this line is not in the list of components to | 82 # If the component of this line is not in the list of components to |
96 # look for, ignore this line. | 83 # look for, ignore this line. |
97 component_path = stack_frame.component_path | 84 component_path = stack_frame.component_path |
98 if component_path not in self.component_dict: | 85 if component_path not in self.component_dict: |
99 continue | 86 continue |
100 | 87 |
101 # Get values of the variables | 88 # Get values of the variables |
102 file_name = stack_frame.file_name | |
103 file_path = stack_frame.file_path | 89 file_path = stack_frame.file_path |
104 crashed_line_number = stack_frame.crashed_line_number | 90 crashed_line_range = stack_frame.crashed_line_range |
105 stack_frame_index = stack_frame.index | 91 stack_frame_index = stack_frame.index |
106 function = stack_frame.function | 92 function = stack_frame.function |
107 | 93 |
108 # Add the file to this component's dictionary of files. | 94 # Add the file to this component's dictionary of files. |
109 file_dict = self.component_dict[component_path] | 95 file_dict = self.component_dict[component_path] |
110 file_dict.AddFile(file_name, file_path, crashed_line_number, | 96 file_dict.AddFile(file_path, crashed_line_range, stack_frame_index, |
111 stack_frame_index, function) | 97 function) |
112 | 98 |
113 def __CreateFileDictFromCallstack(self, callstack, top_n_frames=15): | 99 def __CreateFileDictFromCallstack(self, callstack, top_n_frames=10): |
114 """Creates a file dict that maps a file to the occurrence in the stack. | 100 """Creates a file dict that maps a file to the occurrence in the stack. |
115 | 101 |
116 Args: | 102 Args: |
117 callstack: A list containing parsed result from a single stack | 103 callstack: A list containing parsed result from a single stack |
118 within a stacktrace. For example, a stacktrace from | 104 within a stacktrace. For example, a stacktrace from |
119 previously-allocated thread in release build stacktrace. | 105 previously-allocated thread in release build stacktrace. |
120 top_n_frames: The number of frames to look for. | 106 top_n_frames: The number of frames to look for. |
121 | 107 |
122 Returns: | 108 Returns: |
123 Component_dict, a dictionary with key as a file name and value as another | 109 Component_dict, a dictionary with key as a file name and value as another |
124 dictionary, which maps the file's path (because there can be multiple | 110 dictionary, which maps the file's path (because there can be multiple |
125 files with same name but in different directory) to the list of this | 111 files with same name but in different directory) to the list of this |
126 file's place in stack, lines that this file caused a crash, and the name | 112 file's place in stack, lines that this file caused a crash, and the name |
127 of the function. | 113 of the function. |
128 """ | 114 """ |
129 | 115 |
130 # Only look at first top_n_frames of the stacktrace, below those are likely | 116 # Only look at first top_n_frames of the stacktrace, below those are likely |
131 # to be noisy. Parse the stacktrace into the component dictionary. | 117 # to be noisy. Parse the stacktrace into the component dictionary. |
132 stack_list = callstack.GetTopNFrames(top_n_frames) | 118 stack_list = callstack.GetTopNFrames(top_n_frames) |
133 self.__GenerateFileDict(stack_list) | 119 self.__GenerateFileDict(stack_list) |
134 | 120 |
135 def __iter__(self): | 121 def __iter__(self): |
136 return iter(self.component_dict) | 122 return iter(self.component_dict) |
OLD | NEW |