| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2017 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 import collections |
| 6 import copy |
| 7 import re |
| 8 |
| 9 |
| 10 SECTION_TO_SECTION_NAME = { |
| 11 'b': '.bss', |
| 12 'd': '.data', |
| 13 'r': '.rodata', |
| 14 't': '.text', |
| 15 } |
| 16 |
| 17 |
| 18 class SizeInfo(object): |
| 19 __slots__ = ( |
| 20 'symbols', |
| 21 'section_sizes', |
| 22 ) |
| 23 |
| 24 """Root size information.""" |
| 25 def __init__(self, symbols, section_sizes): |
| 26 self.symbols = symbols |
| 27 self.section_sizes = section_sizes # E.g. {'.text': 0} |
| 28 |
| 29 |
| 30 class BaseSymbol(object): |
| 31 """Base class for Symbol and SymbolGroup.""" |
| 32 __slots__ = () |
| 33 |
| 34 @property |
| 35 def section(self): |
| 36 return self.section_name[1] |
| 37 |
| 38 @property |
| 39 def size_without_padding(self): |
| 40 return self.size - self.padding |
| 41 |
| 42 @property |
| 43 def end_address(self): |
| 44 return self.address + self.size_without_padding |
| 45 |
| 46 def IsBss(self): |
| 47 return self.section_name == '.bss' |
| 48 |
| 49 def IsGroup(self): |
| 50 return False |
| 51 |
| 52 def IsGenerated(self): |
| 53 # TODO(agrieve): Also match generated functions such as: |
| 54 # startup._GLOBAL__sub_I_page_allocator.cc |
| 55 return self.name.endswith(']') and not self.name.endswith('[]') |
| 56 |
| 57 def _Key(self): |
| 58 return (self.section_name, self.function_signature or self.name) |
| 59 |
| 60 |
| 61 class Symbol(BaseSymbol): |
| 62 """Represents a single symbol within a binary.""" |
| 63 |
| 64 __slots__ = ( |
| 65 'section_name', |
| 66 'address', |
| 67 'size', |
| 68 'padding', |
| 69 'name', |
| 70 'function_signature', |
| 71 'path', |
| 72 ) |
| 73 |
| 74 def __init__(self, section_name, address, size_without_padding, name, path): |
| 75 self.section_name = section_name |
| 76 self.address = address |
| 77 self.name = name or '' |
| 78 self.function_signature = '' |
| 79 self.path = path or '' |
| 80 self.size = size_without_padding |
| 81 self.padding = 0 |
| 82 |
| 83 def __repr__(self): |
| 84 return '%s@%x(size=%d,padding=%d,name=%s,path=%s)' % ( |
| 85 self.section_name, self.address, self.size_without_padding, |
| 86 self.padding, self.name, self.path) |
| 87 |
| 88 |
| 89 class SymbolGroup(BaseSymbol): |
| 90 """Represents a group of symbols using the same interface as Symbol.""" |
| 91 |
| 92 __slots__ = ( |
| 93 'symbols', |
| 94 'filtered_symbols', |
| 95 'name', |
| 96 'section_name', |
| 97 ) |
| 98 |
| 99 def __init__(self, symbols, filtered_symbols=None, name=None, |
| 100 section_name=None): |
| 101 self.symbols = symbols |
| 102 self.filtered_symbols = filtered_symbols or [] |
| 103 self.name = name or '' |
| 104 self.section_name = section_name or '.*' |
| 105 |
| 106 def __repr__(self): |
| 107 return 'Group(name=%s,count=%d,size=%d)' % ( |
| 108 self.name, len(self), self.size) |
| 109 |
| 110 def __iter__(self): |
| 111 return iter(self.symbols) |
| 112 |
| 113 def __len__(self): |
| 114 return len(self.symbols) |
| 115 |
| 116 def __getitem__(self, index): |
| 117 return self.symbols[index] |
| 118 |
| 119 def __sub__(self, other): |
| 120 other_ids = set(id(s) for s in other) |
| 121 new_symbols = [s for s in self if id(s) not in other_ids] |
| 122 return self._CreateTransformed(new_symbols, section_name=self.section_name) |
| 123 |
| 124 def __add__(self, other): |
| 125 self_ids = set(id(s) for s in self) |
| 126 new_symbols = self.symbols + [s for s in other if id(s) not in self_ids] |
| 127 return self._CreateTransformed(new_symbols, section_name=self.section_name) |
| 128 |
| 129 @property |
| 130 def address(self): |
| 131 return 0 |
| 132 |
| 133 @property |
| 134 def function_signature(self): |
| 135 return None |
| 136 |
| 137 @property |
| 138 def path(self): |
| 139 return None |
| 140 |
| 141 @property |
| 142 def size(self): |
| 143 if self.IsBss(): |
| 144 return sum(s.size for s in self) |
| 145 return sum(s.size for s in self if not s.IsBss()) |
| 146 |
| 147 @property |
| 148 def padding(self): |
| 149 return sum(s.padding for s in self) |
| 150 |
| 151 def IsGroup(self): |
| 152 return True |
| 153 |
| 154 def _CreateTransformed(self, symbols, filtered_symbols=None, name=None, |
| 155 section_name=None): |
| 156 return SymbolGroup(symbols, filtered_symbols=filtered_symbols, name=name, |
| 157 section_name=section_name) |
| 158 |
| 159 def Sorted(self, cmp_func=None, key=None, reverse=False): |
| 160 # Default to sorting by abs(size) then name. |
| 161 if cmp_func is None and key is None: |
| 162 cmp_func = lambda a, b: cmp((a.IsBss(), abs(b.size), a.name), |
| 163 (b.IsBss(), abs(a.size), b.name)) |
| 164 |
| 165 new_symbols = sorted(self.symbols, cmp_func, key, reverse) |
| 166 return self._CreateTransformed(new_symbols, |
| 167 filtered_symbols=self.filtered_symbols, |
| 168 section_name=self.section_name) |
| 169 |
| 170 def Filter(self, func): |
| 171 filtered_and_kept = ([], []) |
| 172 for symbol in self: |
| 173 filtered_and_kept[int(bool(func(symbol)))].append(symbol) |
| 174 return self._CreateTransformed(filtered_and_kept[1], |
| 175 filtered_symbols=filtered_and_kept[0], |
| 176 section_name=self.section_name) |
| 177 |
| 178 def WhereBiggerThan(self, min_size): |
| 179 return self.Filter(lambda s: s.size >= min_size) |
| 180 |
| 181 def WhereInSection(self, section): |
| 182 if len(section) == 1: |
| 183 ret = self.Filter(lambda s: s.section == section) |
| 184 ret.section_name = SECTION_TO_SECTION_NAME[section] |
| 185 else: |
| 186 ret = self.Filter(lambda s: s.section_name == section) |
| 187 ret.section_name = section |
| 188 return ret |
| 189 |
| 190 def WhereIsGenerated(self): |
| 191 return self.Filter(lambda s: s.IsGenerated()) |
| 192 |
| 193 def WhereNameMatches(self, pattern): |
| 194 regex = re.compile(pattern) |
| 195 return self.Filter(lambda s: regex.search(s.name)) |
| 196 |
| 197 def WherePathMatches(self, pattern): |
| 198 regex = re.compile(pattern) |
| 199 return self.Filter(lambda s: s.path and regex.search(s.path)) |
| 200 |
| 201 def WhereAddressInRange(self, start, end): |
| 202 return self.Filter(lambda s: s.address >= start and s.address <= end) |
| 203 |
| 204 def WhereHasAnyAttribution(self): |
| 205 return self.Filter(lambda s: s.name or s.path) |
| 206 |
| 207 def Inverted(self): |
| 208 return self._CreateTransformed(self.filtered_symbols, |
| 209 filtered_symbols=self.symbols) |
| 210 |
| 211 def GroupBy(self, func): |
| 212 new_syms = [] |
| 213 filtered_symbols = [] |
| 214 symbols_by_token = collections.defaultdict(list) |
| 215 for symbol in self: |
| 216 token = func(symbol) |
| 217 if not token: |
| 218 filtered_symbols.append(symbol) |
| 219 continue |
| 220 symbols_by_token[token].append(symbol) |
| 221 for token, symbols in symbols_by_token.iteritems(): |
| 222 new_syms.append(self._CreateTransformed(symbols, name=token, |
| 223 section_name=self.section_name)) |
| 224 return self._CreateTransformed(new_syms, filtered_symbols=filtered_symbols, |
| 225 section_name=self.section_name) |
| 226 |
| 227 def GroupByNamespace(self, depth=1): |
| 228 def extract_namespace(symbol): |
| 229 # Does not distinguish between classes and namespaces. |
| 230 idx = -2 |
| 231 for _ in xrange(depth): |
| 232 idx = symbol.name.find('::', idx + 2) |
| 233 if idx != -1: |
| 234 ret = symbol.name[:idx] |
| 235 if '<' not in ret: |
| 236 return ret |
| 237 return '{global}' |
| 238 return self.GroupBy(extract_namespace) |
| 239 |
| 240 def GroupByPath(self, depth=1): |
| 241 def extract_path(symbol): |
| 242 idx = -1 |
| 243 for _ in xrange(depth): |
| 244 idx = symbol.path.find('/', idx + 1) |
| 245 if idx != -1: |
| 246 return symbol.path[:idx] |
| 247 return '{path unknown}' |
| 248 return self.GroupBy(extract_path) |
| 249 |
| 250 |
| 251 class SymbolDiff(SymbolGroup): |
| 252 __slots__ = ( |
| 253 '_added_ids', |
| 254 '_removed_ids', |
| 255 ) |
| 256 |
| 257 def __init__(self, added, removed, similar): |
| 258 self._added_ids = set(id(s) for s in added) |
| 259 self._removed_ids = set(id(s) for s in removed) |
| 260 symbols = [] |
| 261 symbols.extend(added) |
| 262 symbols.extend(removed) |
| 263 symbols.extend(similar) |
| 264 super(SymbolDiff, self).__init__(symbols) |
| 265 |
| 266 def __repr__(self): |
| 267 return '%s(%d added, %d removed, %d changed, %d unchanged, size=%d)' % ( |
| 268 'SymbolGroup', self.added_count, self.removed_count, self.changed_count, |
| 269 self.unchanged_count, self.size) |
| 270 |
| 271 def _CreateTransformed(self, symbols, filtered_symbols=None, name=None, |
| 272 section_name=None): |
| 273 ret = SymbolDiff.__new__(SymbolDiff) |
| 274 # Printing sorts, so fast-path the same symbols case. |
| 275 if len(symbols) == len(self.symbols): |
| 276 ret._added_ids = self._added_ids |
| 277 ret._removed_ids = self._removed_ids |
| 278 else: |
| 279 ret._added_ids = set(id(s) for s in symbols if self.IsAdded(s)) |
| 280 ret._removed_ids = set(id(s) for s in symbols if self.IsRemoved(s)) |
| 281 super(SymbolDiff, ret).__init__(symbols, filtered_symbols=filtered_symbols, |
| 282 name=name, section_name=section_name) |
| 283 |
| 284 return ret |
| 285 |
| 286 @property |
| 287 def added_count(self): |
| 288 return len(self._added_ids) |
| 289 |
| 290 @property |
| 291 def removed_count(self): |
| 292 return len(self._removed_ids) |
| 293 |
| 294 @property |
| 295 def changed_count(self): |
| 296 not_changed = self.unchanged_count + self.added_count + self.removed_count |
| 297 return len(self) - not_changed |
| 298 |
| 299 @property |
| 300 def unchanged_count(self): |
| 301 return sum(1 for s in self if self.IsSimilar(s) and s.size == 0) |
| 302 |
| 303 def IsAdded(self, sym): |
| 304 return id(sym) in self._added_ids |
| 305 |
| 306 def IsSimilar(self, sym): |
| 307 key = id(sym) |
| 308 return key not in self._added_ids and key not in self._removed_ids |
| 309 |
| 310 def IsRemoved(self, sym): |
| 311 return id(sym) in self._removed_ids |
| 312 |
| 313 def WhereNotUnchanged(self): |
| 314 return self.Filter(lambda s: not self.IsSimilar(s) or s.size) |
| 315 |
| 316 |
| 317 def Diff(new, old): |
| 318 """Diffs two SizeInfo or SymbolGroup objects. |
| 319 |
| 320 When diffing SizeInfos, ret.section_sizes are the result of |new| - |old|, and |
| 321 ret.symbols will be a SymbolDiff. |
| 322 |
| 323 When diffing SymbolGroups, a SymbolDiff is returned. |
| 324 |
| 325 Returns: |
| 326 Returns a SizeInfo when args are of type SizeInfo. |
| 327 Returns a SymbolDiff when args are of type SymbolGroup. |
| 328 """ |
| 329 if isinstance(new, SizeInfo): |
| 330 assert isinstance(old, SizeInfo) |
| 331 section_sizes = { |
| 332 k:new.section_sizes[k] - v for k, v in old.section_sizes.iteritems()} |
| 333 symbol_diff = Diff(new.symbols, old.symbols) |
| 334 return SizeInfo(symbol_diff, section_sizes) |
| 335 |
| 336 assert isinstance(new, SymbolGroup) and isinstance(old, SymbolGroup) |
| 337 symbols_by_key = collections.defaultdict(list) |
| 338 for s in old: |
| 339 symbols_by_key[s._Key()].append(s) |
| 340 |
| 341 added = [] |
| 342 removed = [] |
| 343 similar = [] |
| 344 # For similar symbols, padding is zeroed out. In order to not lose the |
| 345 # information entirely, store it in aggregate. |
| 346 padding_by_section_name = collections.defaultdict(int) |
| 347 for new_sym in new: |
| 348 matching_syms = symbols_by_key.get(new_sym._Key()) |
| 349 if matching_syms: |
| 350 old_sym = matching_syms.pop(0) |
| 351 # More stable/useful to compare size without padding. |
| 352 size_diff = (new_sym.size_without_padding - |
| 353 old_sym.size_without_padding) |
| 354 merged_sym = Symbol(old_sym.section_name, old_sym.address, |
| 355 size_diff, old_sym.name, old_sym.path) |
| 356 merged_sym.function_signature = old_sym.function_signature |
| 357 similar.append(merged_sym) |
| 358 padding_by_section_name[new_sym.section_name] += ( |
| 359 new_sym.padding - old_sym.padding) |
| 360 else: |
| 361 added.append(new_sym) |
| 362 |
| 363 for remaining_syms in symbols_by_key.itervalues(): |
| 364 for old_sym in remaining_syms: |
| 365 duped = copy.copy(old_sym) |
| 366 duped.size = -duped.size |
| 367 duped.padding = -duped.padding |
| 368 removed.append(duped) |
| 369 |
| 370 for section_name, padding in padding_by_section_name.iteritems(): |
| 371 similar.append(Symbol(section_name, 0, padding, |
| 372 '** aggregate padding of delta symbols', None)) |
| 373 return SymbolDiff(added, removed, similar) |
| OLD | NEW |