1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * 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 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 */
31
32#ifndef __SYMTAB_HH__
33#define __SYMTAB_HH__
34
35#include <iosfwd>
36#include <map>
37#include <string>
38
39#include "base/types.hh"
40#include "sim/serialize.hh"
41
41class Checkpoint;
42class SymbolTable
43{
44 public:
45 typedef std::map<Addr, std::string> ATable;
46 typedef std::map<std::string, Addr> STable;
47
48 private:
49 ATable addrTable;
50 STable symbolTable;
51
52 private:
53 bool
54 upperBound(Addr addr, ATable::const_iterator &iter) const
55 {
56 // find first key *larger* than desired address
57 iter = addrTable.upper_bound(addr);
58
59 // if very first key is larger, we're out of luck
60 if (iter == addrTable.begin())
61 return false;
62
63 return true;
64 }
65
66 public:
67 SymbolTable() {}
68 SymbolTable(const std::string &file) { load(file); }
69 ~SymbolTable() {}
70
71 void clear();
72 bool insert(Addr address, std::string symbol);
73 bool load(const std::string &file);
74
75 const ATable &getAddrTable() const { return addrTable; }
76 const STable &getSymbolTable() const { return symbolTable; }
77
78 public:
79 void serialize(const std::string &base, std::ostream &os);
80 void unserialize(const std::string &base, Checkpoint *cp,
81 const std::string &section);
79 void serialize(const std::string &base, CheckpointOut &cp) const;
80 void unserialize(const std::string &base, CheckpointIn &cp);
81
82 public:
83 bool
84 findSymbol(Addr address, std::string &symbol) const
85 {
86 ATable::const_iterator i = addrTable.find(address);
87 if (i == addrTable.end())
88 return false;
89
90 symbol = (*i).second;
91 return true;
92 }
93
94 bool
95 findAddress(const std::string &symbol, Addr &address) const
96 {
97 STable::const_iterator i = symbolTable.find(symbol);
98 if (i == symbolTable.end())
99 return false;
100
101 address = (*i).second;
102 return true;
103 }
104
105 /// Find the nearest symbol equal to or less than the supplied
106 /// address (e.g., the label for the enclosing function).
107 /// @param addr The address to look up.
108 /// @param symbol Return reference for symbol string.
109 /// @param symaddr Return reference for symbol address.
110 /// @param nextaddr Address of following symbol (for
111 /// determining valid range of symbol).
112 /// @retval True if a symbol was found.
113 bool
114 findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr,
115 Addr &nextaddr) const
116 {
117 ATable::const_iterator i;
118 if (!upperBound(addr, i))
119 return false;
120
121 nextaddr = i->first;
122 --i;
123 symaddr = i->first;
124 symbol = i->second;
125 return true;
126 }
127
128 /// Overload for findNearestSymbol() for callers who don't care
129 /// about nextaddr.
130 bool
131 findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr) const
132 {
133 ATable::const_iterator i;
134 if (!upperBound(addr, i))
135 return false;
136
137 --i;
138 symaddr = i->first;
139 symbol = i->second;
140 return true;
141 }
142
143
144 bool
145 findNearestAddr(Addr addr, Addr &symaddr, Addr &nextaddr) const
146 {
147 ATable::const_iterator i;
148 if (!upperBound(addr, i))
149 return false;
150
151 nextaddr = i->first;
152 --i;
153 symaddr = i->first;
154 return true;
155 }
156
157 bool
158 findNearestAddr(Addr addr, Addr &symaddr) const
159 {
160 ATable::const_iterator i;
161 if (!upperBound(addr, i))
162 return false;
163
164 --i;
165 symaddr = i->first;
166 return true;
167 }
168};
169
170/// Global unified debugging symbol table (for target). Conceptually
171/// there should be one of these per System object for full system,
172/// and per Process object for non-full-system, but so far one big
173/// global one has worked well enough.
174extern SymbolTable *debugSymbolTable;
175
176#endif // __SYMTAB_HH__