SymbolRegistry.cc Source File

Back to the index.

SymbolRegistry.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009-2010 Anders Gavare. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "SymbolRegistry.h"
29 
30 
32 {
33 }
34 
35 
37 {
38  m_map.clear();
39 }
40 
41 
42 void SymbolRegistry::AddSymbol(const string& symbol, uint64_t vaddr)
43 {
44  m_map[vaddr] = symbol;
45 }
46 
47 
48 string SymbolRegistry::LookupAddress(uint64_t vaddr, bool allowOffset) const
49 {
50  // Must be an exact match?
51  if (!allowOffset) {
52  SymbolMap::const_iterator a = m_map.find(vaddr);
53  return a == m_map.end()? "" : a->second;
54  }
55 
56  // Try to find the symbol given the address, or at least some
57  // symbol close to it:
58  SymbolMap::const_iterator a = m_map.lower_bound(vaddr);
59  if (a == m_map.end()) {
60  if (m_map.empty())
61  return "";
62 
63  a = m_map.end();
64  }
65 
66  // Exact match? Then just return the symbol.
67  if (a != m_map.end() && vaddr == a->first)
68  return a->second;
69 
70  if (a == m_map.begin())
71  return "";
72 
73  // Return the symbol _before_ the address, plus an offset.
74  --a;
75 
76  stringstream ss;
77  ss.flags(std::ios::hex);
78  ss << a->second << "+0x" << (vaddr - a->first);
79  return ss.str();
80 }
81 
82 
83 /*****************************************************************************/
84 
85 
86 #ifdef WITHUNITTESTS
87 
88 static void Test_SymbolRegistry_Basic()
89 {
90  SymbolRegistry registry;
91 
92  registry.AddSymbol("symA", 0x1000);
93  registry.AddSymbol("symB", 0x1020);
94  registry.AddSymbol("symC", 0x1060);
95 
96  UnitTest::Assert("lookup failed 1?",
97  registry.LookupAddress(0x1020, true), "symB");
98 
99  UnitTest::Assert("lookup failed 2?",
100  registry.LookupAddress(0x1020, false), "symB");
101 
102  registry.Clear();
103 
104  UnitTest::Assert("clear failed?",
105  registry.LookupAddress(0x1020, true), "");
106 }
107 
108 static void Test_SymbolRegistry_BeforeFirst()
109 {
110  SymbolRegistry registry;
111 
112  registry.AddSymbol("symA", 0x1000);
113  registry.AddSymbol("symB", 0x1020);
114 
115  UnitTest::Assert("lookup should have failed 1",
116  registry.LookupAddress(0xffc, true), "");
117 
118  UnitTest::Assert("lookup should have failed 2",
119  registry.LookupAddress(0xffc, false), "");
120 }
121 
122 static void Test_SymbolRegistry_WithOffset()
123 {
124  SymbolRegistry registry;
125 
126  registry.AddSymbol("symA", 0x1000);
127  registry.AddSymbol("symB", 0x1020);
128 
129  UnitTest::Assert("lookup failed?",
130  registry.LookupAddress(0x1006, true), "symA+0x6");
131 
132  UnitTest::Assert("lookup should have failed",
133  registry.LookupAddress(0x1006, false), "");
134 }
135 
136 static void Test_SymbolRegistry_AfterLast()
137 {
138  SymbolRegistry registry;
139 
140  registry.AddSymbol("symA", 0x1000);
141  registry.AddSymbol("symB", 0x1020);
142 
143  UnitTest::Assert("lookup failed?",
144  registry.LookupAddress(0x1090, true), "symB+0x70");
145 
146  UnitTest::Assert("lookup should have failed",
147  registry.LookupAddress(0x1090, false), "");
148 }
149 
151 {
152  UNITTEST(Test_SymbolRegistry_Basic);
153  UNITTEST(Test_SymbolRegistry_BeforeFirst);
154  UNITTEST(Test_SymbolRegistry_WithOffset);
155  UNITTEST(Test_SymbolRegistry_AfterLast);
156 }
157 
158 #endif
SymbolRegistry::Clear
void Clear()
Clears the registry.
Definition: SymbolRegistry.cc:36
UNITTESTS
#define UNITTESTS(class)
Helper for unit test case execution.
Definition: UnitTest.h:184
SymbolRegistry.h
UNITTEST
#define UNITTEST(functionname)
Helper for unit test case execution.
Definition: UnitTest.h:217
SymbolRegistry::AddSymbol
void AddSymbol(const string &symbol, uint64_t vaddr)
Adds a symbol to the registry.
Definition: SymbolRegistry.cc:42
UnitTest::Assert
static void Assert(const string &strFailMessage, bool condition)
Asserts that a boolean condition is correct.
Definition: UnitTest.cc:40
SymbolRegistry
A registry for loaded symbols.
Definition: SymbolRegistry.h:41
SymbolRegistry::SymbolRegistry
SymbolRegistry()
Constructs a SymbolRegistry.
Definition: SymbolRegistry.cc:31
SymbolRegistry::LookupAddress
string LookupAddress(uint64_t vaddr, bool allowOffset) const
Looks up an address.
Definition: SymbolRegistry.cc:48
symbol
Definition: symbol.h:37

Generated on Tue Aug 25 2020 19:25:06 for GXemul by doxygen 1.8.18