Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 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 from collections import defaultdict | |
| 6 | |
| 7 from common.git_repository import GitRepository | |
| 8 from common.http_client_appengine import HttpClientAppengine | |
| 9 from crash import crash_util | |
| 10 from crash.results import MatchResults | |
| 11 from crash.scorers.aggregator import Aggregator | |
| 12 from crash.scorers.min_distance import MinDistance | |
| 13 from crash.scorers.top_frame_index import TopFrameIndex | |
| 14 | |
| 15 | |
| 16 def GetDepsInCrashStack(crash_stack, crash_deps): | |
| 17 """Gets Dependencies in crash stack.""" | |
| 18 if not crash_stack: | |
| 19 return {} | |
| 20 | |
| 21 stack_deps = {} | |
| 22 for frame in crash_stack: | |
| 23 stack_deps[frame.dep_path] = crash_deps[frame.dep_path] | |
| 24 | |
| 25 return stack_deps | |
| 26 | |
| 27 | |
| 28 def GetFileToChangeLogsForDeps(regression_deps_rolls, stack_deps): | |
| 29 """Gets a dict containing files touched by changelogs for deps in stack_deps. | |
| 30 | |
| 31 Regression ranges for each dep is determined by regression_deps_rolls. | |
| 32 Those changelogs got reverted should be ignored. | |
| 33 | |
| 34 Args: | |
| 35 regression_deps_rolls (dict): Maps dep_path to DependencyRoll in | |
| 36 regression range. | |
| 37 stack_deps (dict): Represents all the dependencies shown in | |
| 38 the crash stack. | |
| 39 | |
| 40 Returns: | |
| 41 A tuple (dep_to_file_to_changelogs, ignore_cls). | |
| 42 | |
| 43 dep_to_file_to_changelogs (dict): Maps dep_path to a dict mapping file path | |
| 44 to ChangeLogs that touched this file. | |
| 45 | |
| 46 For example: | |
| 47 { | |
| 48 'src/': { | |
| 49 'a.cc': [ | |
| 50 ChangeLog.FromDict({ | |
| 51 'author_name': 'test@chromium.org', | |
| 52 'message': 'dummy', | |
| 53 'committer_email': 'example@chromium.org', | |
| 54 'commit_position': 175976, | |
| 55 'author_email': 'example@chromium.org', | |
| 56 'touched_files': [ | |
| 57 { | |
| 58 'change_type': 'add', | |
| 59 'new_path': 'a.cc', | |
| 60 'old_path': 'b/a.cc' | |
| 61 }, | |
| 62 ... | |
| 63 ], | |
| 64 'author_time': 'Thu Mar 31 21:24:43 2016', | |
| 65 'committer_time': 'Thu Mar 31 21:28:39 2016', | |
| 66 'commit_url': | |
| 67 'https://repo.test/+/bcfd', | |
| 68 'code_review_url': 'https://codereview.chromium.org/3281', | |
| 69 'committer_name': 'example@chromium.org', | |
| 70 'revision': 'bcfd', | |
| 71 'reverted_revision': None | |
| 72 }), | |
| 73 ] | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 ignore_cls (set): A set of reverted revisions. | |
| 78 """ | |
| 79 dep_to_file_to_changelogs = defaultdict(lambda: defaultdict(list)) | |
| 80 ignore_cls = set() | |
| 81 | |
| 82 for dep in stack_deps: | |
| 83 dep_roll = regression_deps_rolls[dep] | |
| 84 | |
| 85 git_repository = GitRepository(dep_roll['repo_url'], HttpClientAppengine()) | |
| 86 changelogs = git_repository.GetChangeLogs(dep_roll['old_revision'], | |
| 87 dep_roll['new_revision']) | |
| 88 | |
| 89 for changelog in changelogs: | |
| 90 if changelog.reverted_revision: | |
| 91 # Add reverted revisions to ignore_cls to filter those reverted | |
| 92 # revisions later while matching cls. | |
| 93 ignore_cls.add(changelog.reverted_revision) | |
| 94 continue | |
| 95 | |
| 96 for touched_file in changelog.touched_files: | |
| 97 if (touched_file.change_type == 'delete' or | |
|
stgao
2016/04/20 17:41:26
Can we not hard-code it?
https://chromium.googleso
Sharu Jiang
2016/04/20 18:54:24
Done.
| |
| 98 touched_file.new_path.endswith('.h')): | |
| 99 continue | |
| 100 | |
| 101 dep_to_file_to_changelogs[dep][touched_file.new_path].append(changelog) | |
| 102 | |
| 103 return dep_to_file_to_changelogs, ignore_cls | |
| 104 | |
| 105 | |
| 106 def GetFileToStackInfosForDeps(stacktrace, stack_deps): | |
| 107 """Gets a dict containing all the stack information of files in stacktrace. | |
| 108 | |
| 109 Args: | |
| 110 stacktrace (Stacktrace): Parsed stacktrace object. | |
| 111 stack_deps (dict): Represents all the dependencies show in | |
| 112 the crash stack. | |
| 113 | |
| 114 Returns: | |
| 115 A dict, maps dep path to a dict mapping file path to a list of stack | |
| 116 inforamtion of this file. A file may occur in several frames, one stack info | |
| 117 consist of a StackFrame and the callstack priority of it. | |
| 118 | |
| 119 For example: | |
| 120 { | |
| 121 'src/': { | |
| 122 'a.cc': [ | |
| 123 (StackFrame(0, 'src/', '', 'func', 'a.cc', [1]), 0), | |
| 124 (StackFrame(2, 'src/', '', 'func', 'a.cc', [33]), 0), | |
| 125 ] | |
| 126 } | |
| 127 } | |
| 128 """ | |
| 129 dep_to_file_to_stack_infos = defaultdict(lambda: defaultdict(list)) | |
| 130 | |
| 131 for callstack in stacktrace: | |
| 132 for frame in callstack: | |
| 133 if frame.dep_path not in stack_deps or frame.file_path.endswith('.h'): | |
|
stgao
2016/04/20 17:41:26
Add the reason why we ignore ".h" header file?
Sharu Jiang
2016/04/20 18:54:24
This is logic in findit for clusterfuzz, I just us
| |
| 134 continue | |
| 135 | |
| 136 dep_to_file_to_stack_infos[frame.dep_path][frame.file_path].append(( | |
| 137 frame, callstack.priority)) | |
| 138 | |
| 139 return dep_to_file_to_stack_infos | |
| 140 | |
| 141 | |
| 142 def GetFileToBlameForDeps(stacktrace, stack_deps): | |
|
stgao
2016/04/20 17:41:26
GetBlameForFilesGroupedByDeps?
Sharu Jiang
2016/04/20 18:54:24
Done.
| |
| 143 """Gets Blames for files in stacktrace. | |
| 144 | |
| 145 Args: | |
| 146 stacktrace (Stacktrace): Parsed stacktrace object. | |
| 147 stack_deps (dict of Dependencys): Represents all the dependencies shown in | |
| 148 the crash stack. | |
| 149 | |
| 150 Returns: | |
| 151 A dict, maps dep_path to a dict mapping file path to its Blame. | |
| 152 | |
| 153 For example: | |
| 154 { | |
| 155 'src/': { | |
| 156 'a.cc': Blame(revision, 'a.cc') | |
| 157 'b.cc': Blame(revision, 'b.cc') | |
| 158 } | |
| 159 } | |
| 160 """ | |
| 161 dep_to_file_to_blame = defaultdict(dict) | |
| 162 for callstack in stacktrace: | |
| 163 for frame in callstack: | |
| 164 # We only care about those dependencies in crash stack. | |
| 165 if frame.dep_path not in stack_deps: | |
| 166 continue | |
| 167 | |
| 168 git_repository = GitRepository(stack_deps[frame.dep_path].repo_url, | |
| 169 HttpClientAppengine()) | |
| 170 dep_to_file_to_blame[frame.dep_path][frame.file_path] = ( | |
| 171 git_repository.GetBlame( | |
| 172 frame.file_path, stack_deps[frame.dep_path].revision)) | |
| 173 | |
| 174 return dep_to_file_to_blame | |
| 175 | |
| 176 | |
| 177 def FindMatchResults(dep_to_file_to_changelogs, | |
| 178 dep_to_file_to_stack_infos, | |
| 179 dep_to_file_to_blame, | |
| 180 ignore_cls=None): | |
| 181 """Finds results by matching stacktrace and changelogs in regression range. | |
| 182 | |
| 183 This method only applies to those crashes with regression range. | |
| 184 | |
| 185 Args: | |
| 186 dep_to_file_to_changelogs (dict): Maps dep_path to a dict mapping file path | |
| 187 to ChangeLogs that touched this file. | |
| 188 | |
| 189 dep_to_file_to_stack_infos (dict): Maps dep path to a dict mapping file path | |
| 190 to a list of stack inforamtion of this file. A file may occur in several | |
| 191 frames, one stack info consist of a StackFrame and the callstack priority | |
| 192 of it. | |
| 193 | |
| 194 dep_to_file_to_blame (dict): Maps dep_path to a dict mapping file path to | |
| 195 its Blame. | |
| 196 | |
| 197 ignore_cls (set): Set of reverted revisions. | |
| 198 | |
| 199 Returns: | |
| 200 A list of MatchResult instances with confidence and reason unset. | |
| 201 """ | |
| 202 match_results = MatchResults(ignore_cls) | |
| 203 | |
| 204 for dep, file_to_stack_infos in dep_to_file_to_stack_infos.iteritems(): | |
| 205 | |
| 206 for crashed_file_path, stack_infos in file_to_stack_infos.iteritems(): | |
| 207 file_to_changelogs = dep_to_file_to_changelogs[dep] | |
| 208 | |
| 209 for touched_file_path, changelogs in file_to_changelogs.iteritems(): | |
| 210 if not crash_util.IsSameFilePath(crashed_file_path, touched_file_path): | |
| 211 continue | |
| 212 | |
| 213 match_results.GenerateMatchResults( | |
| 214 crashed_file_path, dep, stack_infos, changelogs, | |
| 215 dep_to_file_to_blame[dep][crashed_file_path]) | |
| 216 | |
| 217 return match_results.values() | |
| 218 | |
| 219 | |
| 220 def FindItForCrash(stacktrace, regression_deps_rolls, crashed_deps): | |
| 221 """Finds culprit results for crash. | |
| 222 | |
| 223 Args: | |
| 224 stacktrace (Stactrace): Parsed Stactrace object. | |
| 225 regression_deps_rolls (dict): Maps dep_path to DependencyRoll in | |
| 226 regression range. | |
| 227 crashed_deps (dict of Dependencys): Represents all the dependencies of | |
| 228 crashed revision. | |
| 229 | |
| 230 Returns: | |
| 231 List of dicts of culprit results, sorted by confidence from highest to | |
| 232 lowest. | |
| 233 """ | |
| 234 if not regression_deps_rolls: | |
| 235 return [] | |
| 236 | |
| 237 # We are only interested in the deps in crash stack (the callstack that | |
| 238 # caused the crash). | |
| 239 stack_deps = GetDepsInCrashStack(stacktrace.GetCrashStack(), crashed_deps) | |
| 240 | |
| 241 # Get dep and file to changelogs, stack_info and blame dicts. | |
| 242 dep_to_file_to_changelogs, ignore_cls = GetFileToChangeLogsForDeps( | |
| 243 regression_deps_rolls, stack_deps) | |
| 244 dep_to_file_to_stack_infos = GetFileToStackInfosForDeps( | |
| 245 stacktrace, stack_deps) | |
| 246 dep_to_file_to_blame = GetFileToBlameForDeps(stacktrace, stack_deps) | |
| 247 | |
| 248 results = FindMatchResults(dep_to_file_to_changelogs, | |
| 249 dep_to_file_to_stack_infos, | |
| 250 dep_to_file_to_blame, | |
| 251 ignore_cls) | |
| 252 | |
| 253 if not results: | |
| 254 return [] | |
| 255 | |
| 256 aggregator = Aggregator([TopFrameIndex(), MinDistance()]) | |
| 257 | |
| 258 map(aggregator.ScoreAndReason, results) | |
| 259 | |
| 260 return sorted([result.ToDict() for result in results], | |
| 261 key=lambda r: -r['confidence']) | |
| OLD | NEW |