symtab.hh revision 2632:1bb2f91485ea
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
29#ifndef __SYMTAB_HH__
30#define __SYMTAB_HH__
31
32#include <iosfwd>
33#include <map>
34
35#include "arch/isa_traits.hh"	// for Addr
36
37class Checkpoint;
38class SymbolTable
39{
40  public:
41    typedef std::map<Addr, std::string> ATable;
42    typedef std::map<std::string, Addr> STable;
43
44  private:
45    ATable addrTable;
46    STable symbolTable;
47
48  private:
49    bool
50    upperBound(Addr addr, ATable::const_iterator &iter) const
51    {
52        // find first key *larger* than desired address
53        iter = addrTable.upper_bound(addr);
54
55        // if very first key is larger, we're out of luck
56        if (iter == addrTable.begin())
57            return false;
58
59        return true;
60    }
61
62  public:
63    SymbolTable() {}
64    SymbolTable(const std::string &file) { load(file); }
65    ~SymbolTable() {}
66
67    void clear();
68    bool insert(Addr address, std::string symbol);
69    bool load(const std::string &file);
70
71    const ATable &getAddrTable() const { return addrTable; }
72    const STable &getSymbolTable() const { return symbolTable; }
73
74  public:
75    void serialize(const std::string &base, std::ostream &os);
76    void unserialize(const std::string &base, Checkpoint *cp,
77                     const std::string &section);
78
79  public:
80    bool
81    findSymbol(Addr address, std::string &symbol) const
82    {
83        ATable::const_iterator i = addrTable.find(address);
84        if (i == addrTable.end())
85            return false;
86
87        symbol = (*i).second;
88        return true;
89    }
90
91    bool
92    findAddress(const std::string &symbol, Addr &address) const
93    {
94        STable::const_iterator i = symbolTable.find(symbol);
95        if (i == symbolTable.end())
96            return false;
97
98        address = (*i).second;
99        return true;
100    }
101
102    /// Find the nearest symbol equal to or less than the supplied
103    /// address (e.g., the label for the enclosing function).
104    /// @param address The address to look up.
105    /// @param symbol  Return reference for symbol string.
106    /// @param sym_address Return reference for symbol address.
107    /// @param next_sym_address Address of following symbol (for
108    /// determining valid range of symbol).
109    /// @retval True if a symbol was found.
110    bool
111    findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr,
112                      Addr &nextaddr) const
113    {
114        ATable::const_iterator i;
115        if (!upperBound(addr, i))
116            return false;
117
118        nextaddr = i->first;
119        --i;
120        symaddr = i->first;
121        symbol = i->second;
122        return true;
123    }
124
125    /// Overload for findNearestSymbol() for callers who don't care
126    /// about next_sym_address.
127    bool
128    findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr) const
129    {
130        ATable::const_iterator i;
131        if (!upperBound(addr, i))
132            return false;
133
134        --i;
135        symaddr = i->first;
136        symbol = i->second;
137        return true;
138    }
139
140
141    bool
142    findNearestAddr(Addr addr, Addr &symaddr, Addr &nextaddr) const
143    {
144        ATable::const_iterator i;
145        if (!upperBound(addr, i))
146            return false;
147
148        nextaddr = i->first;
149        --i;
150        symaddr = i->first;
151        return true;
152    }
153
154    bool
155    findNearestAddr(Addr addr, Addr &symaddr) const
156    {
157        ATable::const_iterator i;
158        if (!upperBound(addr, i))
159            return false;
160
161        --i;
162        symaddr = i->first;
163        return true;
164    }
165};
166
167/// Global unified debugging symbol table (for target).  Conceptually
168/// there should be one of these per System object for full system,
169/// and per Process object for non-full-system, but so far one big
170/// global one has worked well enough.
171extern SymbolTable *debugSymbolTable;
172
173#endif // __SYMTAB_HH__
174