Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 import os | |
|
stgao
2014/07/31 19:48:58
Usually we leave a blank line between code and the
| |
| 5 | |
| 6 | |
| 7 class FileDictionary(object): | |
| 8 """Maps file in a stacktrace to its crash information. | |
| 9 | |
| 10 It maps file to another dictionary, which maps the file's path to crashed | |
| 11 lines, stack frame indices and crashed functions. | |
| 12 """ | |
| 13 | |
| 14 def __init__(self): | |
| 15 """Initializes the file dictionary.""" | |
| 16 self.file_dic = {} | |
| 17 | |
| 18 def AddFile(self, file_name, file_path, crashed_line_number, | |
| 19 stack_frame_index, function): | |
| 20 """Adds file and its crash information to the map. | |
| 21 | |
| 22 Args: | |
| 23 file_name: name of the crashed file | |
| 24 file_path: path of the crashed file | |
| 25 crashed_line_number: crashed line of the file | |
| 26 stack_frame_index: the file's position in the callstack | |
| 27 function: name of the crashed function | |
| 28 """ | |
| 29 # Populate the dictionary if this file/path has not been added before. | |
| 30 if file_name not in self.file_dic: | |
| 31 self.file_dic[file_name] = {} | |
| 32 | |
| 33 if file_path not in self.file_dic[file_name]: | |
| 34 self.file_dic[file_name][file_path] = {} | |
| 35 | |
| 36 if 'lines' not in self.file_dic[file_name][file_path]: | |
| 37 self.file_dic[file_name][file_path]['lines'] = [] | |
| 38 | |
| 39 if 'stack_frame_index' not in self.file_dic[file_name][file_path]: | |
| 40 self.file_dic[file_name][file_path]['stack_frame_index'] = [] | |
| 41 | |
| 42 if 'function' not in self.file_dic[file_name][file_path]: | |
| 43 self.file_dic[file_name][file_path]['function'] = [] | |
| 44 | |
| 45 # Add the crashed line, frame index and function name. | |
| 46 self.file_dic[file_name][file_path]['lines'].append(crashed_line_number) | |
| 47 self.file_dic[file_name][file_path]['stack_frame_index'].append( | |
| 48 stack_frame_index) | |
| 49 self.file_dic[file_name][file_path]['function'].append(function) | |
| 50 | |
| 51 def GetPathDic(self, file_name): | |
| 52 """Returns file's path and crash information.""" | |
| 53 return self.file_dic[file_name] | |
| 54 | |
| 55 def GetCrashedLines(self, file_path): | |
| 56 """Returns crashed lines given a file name and path.""" | |
| 57 file_name = os.path.basename(file_path) | |
| 58 return self.file_dic[file_name][file_path]['lines'] | |
| 59 | |
| 60 def GetCrashStackFrameindex(self, file_path): | |
| 61 """Returns stack frame indices given a file name and path.""" | |
| 62 file_name = os.path.basename(file_path) | |
| 63 return self.file_dic[file_name][file_path]['stack_frame_index'] | |
| 64 | |
| 65 def GetCrashFunction(self, file_path): | |
| 66 """Returns list of crashed functions given a file name and path.""" | |
| 67 file_name = os.path.basename(file_path) | |
| 68 return self.file_dic[file_name][file_path]['function'] | |
| 69 | |
| 70 def __iter__(self): | |
| 71 return iter(self.file_dic) | |
| 72 | |
| 73 | |
| 74 class ComponentDictionary(object): | |
| 75 """Represents a file dictionary. | |
| 76 | |
| 77 It maps each component (blink, chrome, etc) to a file dictionary. | |
| 78 """ | |
| 79 | |
| 80 def __init__(self, components): | |
| 81 """Initializes the dictionary with given components.""" | |
| 82 self.component_dic = {} | |
| 83 | |
| 84 # Create file dictionary for all the components. | |
| 85 for component in components: | |
| 86 self.component_dic[component] = FileDictionary() | |
| 87 | |
| 88 def __iter__(self): | |
| 89 return iter(self.component_dic) | |
| 90 | |
| 91 def GetFileDic(self, component): | |
| 92 """Returns a file dictionary for a given component.""" | |
| 93 return self.component_dic[component] | |
| 94 | |
| 95 def FromStacktrace(self, stack_frame_list): | |
| 96 """Parses stacktrace, given an instance of StackFrame list.""" | |
| 97 # Iterate through the list of stackframe objects. | |
| 98 for stackframe in stack_frame_list: | |
| 99 # If the component of this line is not in the list of components to | |
| 100 # look for, ignore this line. | |
| 101 component = stackframe.component | |
| 102 if component not in self.component_dic: | |
| 103 continue | |
| 104 | |
| 105 # Get values of the variables | |
| 106 file_name = stackframe.file_name | |
| 107 file_path = stackframe.file_path | |
| 108 crashed_line_number = stackframe.crashed_line_number | |
| 109 stack_frame_index = stackframe.index | |
| 110 function = stackframe.function | |
| 111 | |
| 112 # Add the file to this component's dictionary of files. | |
| 113 file_dic = self.component_dic[component] | |
| 114 file_dic.AddFile(file_name, file_path, crashed_line_number, | |
| 115 stack_frame_index, function) | |
| OLD | NEW |