symtab.hh revision 2650
12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272SN/A */
282SN/A
292SN/A#ifndef __SYMTAB_HH__
302SN/A#define __SYMTAB_HH__
312SN/A
321984SN/A#include <iosfwd>
33857SN/A#include <map>
341984SN/A
352650Ssaidi@eecs.umich.edu#include "sim/host.hh"	// for Addr
362SN/A
371984SN/Aclass Checkpoint;
382SN/Aclass SymbolTable
392SN/A{
401912SN/A  public:
412130SN/A    typedef std::map<Addr, std::string> ATable;
42857SN/A    typedef std::map<std::string, Addr> STable;
432SN/A
441912SN/A  private:
452SN/A    ATable addrTable;
462SN/A    STable symbolTable;
472SN/A
481912SN/A  private:
491912SN/A    bool
501912SN/A    upperBound(Addr addr, ATable::const_iterator &iter) const
511912SN/A    {
521912SN/A        // find first key *larger* than desired address
531912SN/A        iter = addrTable.upper_bound(addr);
541912SN/A
551912SN/A        // if very first key is larger, we're out of luck
561912SN/A        if (iter == addrTable.begin())
571912SN/A            return false;
581912SN/A
591912SN/A        return true;
601912SN/A    }
611912SN/A
622SN/A  public:
632SN/A    SymbolTable() {}
642SN/A    SymbolTable(const std::string &file) { load(file); }
652SN/A    ~SymbolTable() {}
662SN/A
671984SN/A    void clear();
682SN/A    bool insert(Addr address, std::string symbol);
692SN/A    bool load(const std::string &file);
702SN/A
711912SN/A    const ATable &getAddrTable() const { return addrTable; }
721912SN/A    const STable &getSymbolTable() const { return symbolTable; }
731912SN/A
741912SN/A  public:
751984SN/A    void serialize(const std::string &base, std::ostream &os);
761984SN/A    void unserialize(const std::string &base, Checkpoint *cp,
771984SN/A                     const std::string &section);
781984SN/A
791984SN/A  public:
801912SN/A    bool
811912SN/A    findSymbol(Addr address, std::string &symbol) const
821912SN/A    {
831912SN/A        ATable::const_iterator i = addrTable.find(address);
841912SN/A        if (i == addrTable.end())
851912SN/A            return false;
861912SN/A
871912SN/A        symbol = (*i).second;
881912SN/A        return true;
891912SN/A    }
901912SN/A
911912SN/A    bool
921912SN/A    findAddress(const std::string &symbol, Addr &address) const
931912SN/A    {
941912SN/A        STable::const_iterator i = symbolTable.find(symbol);
951912SN/A        if (i == symbolTable.end())
961912SN/A            return false;
971912SN/A
981912SN/A        address = (*i).second;
991912SN/A        return true;
1001912SN/A    }
1011912SN/A
1021158SN/A    /// Find the nearest symbol equal to or less than the supplied
1031158SN/A    /// address (e.g., the label for the enclosing function).
1041158SN/A    /// @param address The address to look up.
1051158SN/A    /// @param symbol  Return reference for symbol string.
1061158SN/A    /// @param sym_address Return reference for symbol address.
1071158SN/A    /// @param next_sym_address Address of following symbol (for
1081158SN/A    /// determining valid range of symbol).
1091158SN/A    /// @retval True if a symbol was found.
1101912SN/A    bool
1111912SN/A    findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr,
1121912SN/A                      Addr &nextaddr) const
1131912SN/A    {
1141912SN/A        ATable::const_iterator i;
1151912SN/A        if (!upperBound(addr, i))
1161912SN/A            return false;
1171912SN/A
1181912SN/A        nextaddr = i->first;
1191912SN/A        --i;
1201912SN/A        symaddr = i->first;
1211912SN/A        symbol = i->second;
1221912SN/A        return true;
1231912SN/A    }
1241158SN/A
1251158SN/A    /// Overload for findNearestSymbol() for callers who don't care
1261158SN/A    /// about next_sym_address.
1271912SN/A    bool
1281912SN/A    findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr) const
1291158SN/A    {
1301912SN/A        ATable::const_iterator i;
1311912SN/A        if (!upperBound(addr, i))
1321912SN/A            return false;
1331912SN/A
1341912SN/A        --i;
1351912SN/A        symaddr = i->first;
1361912SN/A        symbol = i->second;
1371912SN/A        return true;
1381158SN/A    }
1391158SN/A
1401158SN/A
1411912SN/A    bool
1421912SN/A    findNearestAddr(Addr addr, Addr &symaddr, Addr &nextaddr) const
1431912SN/A    {
1441912SN/A        ATable::const_iterator i;
1451912SN/A        if (!upperBound(addr, i))
1461912SN/A            return false;
1471912SN/A
1481912SN/A        nextaddr = i->first;
1491912SN/A        --i;
1501912SN/A        symaddr = i->first;
1511912SN/A        return true;
1521912SN/A    }
1531912SN/A
1541912SN/A    bool
1551912SN/A    findNearestAddr(Addr addr, Addr &symaddr) const
1561912SN/A    {
1571912SN/A        ATable::const_iterator i;
1581912SN/A        if (!upperBound(addr, i))
1591912SN/A            return false;
1601912SN/A
1611912SN/A        --i;
1621912SN/A        symaddr = i->first;
1631912SN/A        return true;
1641912SN/A    }
1652SN/A};
1662SN/A
1671158SN/A/// Global unified debugging symbol table (for target).  Conceptually
1681158SN/A/// there should be one of these per System object for full system,
1691158SN/A/// and per Process object for non-full-system, but so far one big
1701158SN/A/// global one has worked well enough.
1711158SN/Aextern SymbolTable *debugSymbolTable;
1721158SN/A
1732SN/A#endif // __SYMTAB_HH__
174