symtab.hh revision 2130
19288Sandreas.hansson@arm.com/*
29288Sandreas.hansson@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
39288Sandreas.hansson@arm.com * All rights reserved.
49288Sandreas.hansson@arm.com *
59288Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
69288Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
79288Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
89288Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
99288Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
109288Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
119288Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
129288Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
139288Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
149288Sandreas.hansson@arm.com * this software without specific prior written permission.
159288Sandreas.hansson@arm.com *
169288Sandreas.hansson@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179288Sandreas.hansson@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
189288Sandreas.hansson@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
199288Sandreas.hansson@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
209288Sandreas.hansson@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
219288Sandreas.hansson@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
229288Sandreas.hansson@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239288Sandreas.hansson@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249288Sandreas.hansson@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259288Sandreas.hansson@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
269288Sandreas.hansson@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279288Sandreas.hansson@arm.com */
289288Sandreas.hansson@arm.com
299288Sandreas.hansson@arm.com#ifndef __SYMTAB_HH__
309288Sandreas.hansson@arm.com#define __SYMTAB_HH__
319288Sandreas.hansson@arm.com
329288Sandreas.hansson@arm.com#include <iosfwd>
339288Sandreas.hansson@arm.com#include <map>
349288Sandreas.hansson@arm.com
359288Sandreas.hansson@arm.com#include "arch/isa_traits.hh"	// for Addr
369288Sandreas.hansson@arm.com
379288Sandreas.hansson@arm.comclass Checkpoint;
389288Sandreas.hansson@arm.comclass SymbolTable
399288Sandreas.hansson@arm.com{
409288Sandreas.hansson@arm.com  public:
419288Sandreas.hansson@arm.com    typedef std::map<Addr, std::string> ATable;
428831Smrinmoy.ghosh@arm.com    typedef std::map<std::string, Addr> STable;
438832SAli.Saidi@ARM.com
448832SAli.Saidi@ARM.com  private:
459288Sandreas.hansson@arm.com    ATable addrTable;
468831Smrinmoy.ghosh@arm.com    STable symbolTable;
478831Smrinmoy.ghosh@arm.com
489338SAndreas.Sandberg@arm.com  private:
498831Smrinmoy.ghosh@arm.com    bool
508831Smrinmoy.ghosh@arm.com    upperBound(Addr addr, ATable::const_iterator &iter) const
518831Smrinmoy.ghosh@arm.com    {
528831Smrinmoy.ghosh@arm.com        // find first key *larger* than desired address
538831Smrinmoy.ghosh@arm.com        iter = addrTable.upper_bound(addr);
548831Smrinmoy.ghosh@arm.com
558831Smrinmoy.ghosh@arm.com        // if very first key is larger, we're out of luck
568831Smrinmoy.ghosh@arm.com        if (iter == addrTable.begin())
579288Sandreas.hansson@arm.com            return false;
588832SAli.Saidi@ARM.com
598832SAli.Saidi@ARM.com        return true;
608831Smrinmoy.ghosh@arm.com    }
618831Smrinmoy.ghosh@arm.com
6210052Smitch.hayenga+gem5@gmail.com  public:
6310052Smitch.hayenga+gem5@gmail.com    SymbolTable() {}
6410052Smitch.hayenga+gem5@gmail.com    SymbolTable(const std::string &file) { load(file); }
6510052Smitch.hayenga+gem5@gmail.com    ~SymbolTable() {}
6610052Smitch.hayenga+gem5@gmail.com
6710052Smitch.hayenga+gem5@gmail.com    void clear();
6810053Smitch.hayenga+gem5@gmail.com    bool insert(Addr address, std::string symbol);
6910053Smitch.hayenga+gem5@gmail.com    bool load(const std::string &file);
708832SAli.Saidi@ARM.com
718831Smrinmoy.ghosh@arm.com    const ATable &getAddrTable() const { return addrTable; }
728831Smrinmoy.ghosh@arm.com    const STable &getSymbolTable() const { return symbolTable; }
738831Smrinmoy.ghosh@arm.com
748831Smrinmoy.ghosh@arm.com  public:
759338SAndreas.Sandberg@arm.com    void serialize(const std::string &base, std::ostream &os);
768831Smrinmoy.ghosh@arm.com    void unserialize(const std::string &base, Checkpoint *cp,
778831Smrinmoy.ghosh@arm.com                     const std::string &section);
788831Smrinmoy.ghosh@arm.com
798831Smrinmoy.ghosh@arm.com  public:
809338SAndreas.Sandberg@arm.com    bool
818831Smrinmoy.ghosh@arm.com    findSymbol(Addr address, std::string &symbol) const
828831Smrinmoy.ghosh@arm.com    {
838831Smrinmoy.ghosh@arm.com        ATable::const_iterator i = addrTable.find(address);
848831Smrinmoy.ghosh@arm.com        if (i == addrTable.end())
859338SAndreas.Sandberg@arm.com            return false;
868831Smrinmoy.ghosh@arm.com
878831Smrinmoy.ghosh@arm.com        symbol = (*i).second;
888831Smrinmoy.ghosh@arm.com        return true;
898831Smrinmoy.ghosh@arm.com    }
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