symbol.py revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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.dict = {}
11
12  def Add(self, exec_file_rel_path, offset, symbol):
13    assert(isinstance(symbol, Symbol))
14    self.dict[Symbols._GetKey(exec_file_rel_path, offset)] = symbol
15
16  def Lookup(self, exec_file_rel_path, offset):
17    return self.dict.get(Symbols._GetKey(exec_file_rel_path, offset))
18
19  def Merge(self, other):
20    assert(isinstance(other, Symbols))
21    self.dict.update(other.dict)  # pylint: disable=W0212
22
23  @property
24  def length(self):
25    return len(self.dict)
26
27  @staticmethod
28  def _GetKey(exec_file_rel_path, offset):
29    return '%s+0x%x' % (exec_file_rel_path, offset)
30
31
32class Symbol(object):
33  """Debug information relative to a symbol.
34
35  Note: a symbol can have more than one source line associated to it.
36  """
37
38  def __init__(self, name, source_file_path, line_number):
39    self.name = name
40    self.source_info = []
41    self.AddSourceLineInfo(source_file_path, line_number)
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