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