symbol.py revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1# Copyright 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
6class Symbols(object):
7  """A dictionary of symbols indexed by the key 'exec_path+0xoffset'."""
8
9  def __init__(self):
10    self.symbols = {}  # 'foo.so+0x1234' -> |Symbol|
11
12  def Add(self, exec_file_rel_path, offset, symbol):
13    assert(isinstance(symbol, Symbol))
14    self.symbols[Symbols._GetKey(exec_file_rel_path, offset)] = symbol
15
16  def Lookup(self, exec_file_rel_path, offset):
17    return self.symbols.get(Symbols._GetKey(exec_file_rel_path, offset))
18
19  def Merge(self, other):
20    assert(isinstance(other, Symbols))
21    self.symbols.update(other.symbols)  # pylint: disable=W0212
22
23  def __len__(self):
24    return len(self.symbols)
25
26  @staticmethod
27  def _GetKey(exec_file_rel_path, offset):
28    return '%s+0x%x' % (exec_file_rel_path, offset)
29
30
31class Symbol(object):
32  """Debug information relative to a symbol.
33
34  Note: a symbol can have more than one source line associated to it.
35  """
36
37  def __init__(self, name, source_file_path=None, line_number=None):
38    self.name = name
39    self.source_info = []
40    if source_file_path:
41      self.AddSourceLineInfo(source_file_path, line_number or 0)
42
43  def AddSourceLineInfo(self, source_file_path, line_number):
44    self.source_info += [SourceInfo(source_file_path, line_number)]
45
46  def __str__(self):
47    return '%s %s' % (
48      self.name,
49      self.source_info[0] if len(self.source_info) else '')
50
51
52class SourceInfo(object):
53  """Source file + line information for a given |Symbol|."""
54
55  def __init__(self, source_file_path, line_number):
56    assert(isinstance(line_number, int))
57    self.source_file_path = source_file_path
58    self.line_number = line_number
59
60  def __str__(self):
61    return '%s:%d' % (self.source_file_path, self.line_number)
62