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