Fix some nits from the two previous plugin power saver CLs.
[chromium-blink-merge.git] / tools / findit / component_dictionary.py
blobd1a8967338e9afc615ff30a74208e0ce8c2405e4
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.
6 class FileDictionary(object):
7 """Maps file in a stacktrace to its crash information.
9 It maps file to another dictionary, which maps the file's path to crashed
10 lines, stack frame indices and crashed functions.
11 """
13 def __init__(self):
14 """Initializes the file dictionary."""
15 self.file_dict = {}
17 def AddFile(self, file_path, crashed_line_range, stack_frame_index,
18 function):
19 """Adds file and its crash information to the map.
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'] = []
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)
41 def GetCrashedLineNumbers(self, file_path):
42 """Returns crashed line numbers given a file path."""
43 return self.file_dict[file_path]['line_numbers']
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']
49 def GetCrashFunctions(self, file_path):
50 """Returns list of crashed functions given a file path."""
51 return self.file_dict[file_path]['function']
53 def __iter__(self):
54 return iter(self.file_dict)
57 class ComponentDictionary(object):
58 """Represents a file dictionary.
60 It maps each component path to a file dictionary.
61 """
63 def __init__(self, callstack, components):
64 """Initializes the dictionary with given components."""
65 self.component_dict = {}
67 # Create file dictionary for all the components.
68 for component in components:
69 self.component_dict[component] = FileDictionary()
71 # Create file dict from callstack.
72 self.__CreateFileDictFromCallstack(callstack)
74 def GetFileDict(self, component):
75 """Returns a file dictionary for a given component."""
76 return self.component_dict.get(component)
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
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
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)
99 def __CreateFileDictFromCallstack(self, callstack, top_n_frames=10):
100 """Creates a file dict that maps a file to the occurrence in the stack.
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.
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.
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)
121 def __iter__(self):
122 return iter(self.component_dict)