CPA: Add new object for gathering critical path annotations.
[gem5.git] / src / base / loader / symtab.hh
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 "sim/host.hh" // for Addr
40
41 class Checkpoint;
42 class 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);
82
83 public:
84 bool
85 findSymbol(Addr address, std::string &symbol) const
86 {
87 ATable::const_iterator i = addrTable.find(address);
88 if (i == addrTable.end())
89 return false;
90
91 symbol = (*i).second;
92 return true;
93 }
94
95 bool
96 findAddress(const std::string &symbol, Addr &address) const
97 {
98 STable::const_iterator i = symbolTable.find(symbol);
99 if (i == symbolTable.end())
100 return false;
101
102 address = (*i).second;
103 return true;
104 }
105
106 /// Find the nearest symbol equal to or less than the supplied
107 /// address (e.g., the label for the enclosing function).
108 /// @param addr The address to look up.
109 /// @param symbol Return reference for symbol string.
110 /// @param symaddr Return reference for symbol address.
111 /// @param nextaddr Address of following symbol (for
112 /// determining valid range of symbol).
113 /// @retval True if a symbol was found.
114 bool
115 findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr,
116 Addr &nextaddr) const
117 {
118 ATable::const_iterator i;
119 if (!upperBound(addr, i))
120 return false;
121
122 nextaddr = i->first;
123 --i;
124 symaddr = i->first;
125 symbol = i->second;
126 return true;
127 }
128
129 /// Overload for findNearestSymbol() for callers who don't care
130 /// about nextaddr.
131 bool
132 findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr) const
133 {
134 ATable::const_iterator i;
135 if (!upperBound(addr, i))
136 return false;
137
138 --i;
139 symaddr = i->first;
140 symbol = i->second;
141 return true;
142 }
143
144
145 bool
146 findNearestAddr(Addr addr, Addr &symaddr, Addr &nextaddr) const
147 {
148 ATable::const_iterator i;
149 if (!upperBound(addr, i))
150 return false;
151
152 nextaddr = i->first;
153 --i;
154 symaddr = i->first;
155 return true;
156 }
157
158 bool
159 findNearestAddr(Addr addr, Addr &symaddr) const
160 {
161 ATable::const_iterator i;
162 if (!upperBound(addr, i))
163 return false;
164
165 --i;
166 symaddr = i->first;
167 return true;
168 }
169 };
170
171 /// Global unified debugging symbol table (for target). Conceptually
172 /// there should be one of these per System object for full system,
173 /// and per Process object for non-full-system, but so far one big
174 /// global one has worked well enough.
175 extern SymbolTable *debugSymbolTable;
176
177 #endif // __SYMTAB_HH__