symtab.hh revision 1984
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.
272665Ssaidi@eecs.umich.edu */
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu#ifndef __SYMTAB_HH__
302SN/A#define __SYMTAB_HH__
312SN/A
324997Sgblack@eecs.umich.edu#include <iosfwd>
331110SN/A#include <map>
344997Sgblack@eecs.umich.edu
352680Sktlim@umich.edu#include "targetarch/isa_traits.hh"	// for Addr
362196SN/A
372196SN/Aclass Checkpoint;
387676Snate@binkert.orgclass SymbolTable
394997Sgblack@eecs.umich.edu{
402800Ssaidi@eecs.umich.edu  public:
412800Ssaidi@eecs.umich.edu    typedef std::map<Addr, std::string> ATable;
422289SN/A    typedef std::map<std::string, Addr> STable;
432SN/A
445569Snate@binkert.org  private:
452167SN/A    ATable addrTable;
462203SN/A    STable symbolTable;
472203SN/A
482222SN/A  private:
492166SN/A    bool
502203SN/A    upperBound(Addr addr, ATable::const_iterator &iter) const
512203SN/A    {
522222SN/A        // find first key *larger* than desired address
532166SN/A        iter = addrTable.upper_bound(addr);
542147SN/A
552147SN/A        // if very first key is larger, we're out of luck
562222SN/A        if (iter == addrTable.begin())
572147SN/A            return false;
582147SN/A
592147SN/A        return true;
602222SN/A    }
612147SN/A
622147SN/A  public:
632147SN/A    SymbolTable() {}
642222SN/A    SymbolTable(const std::string &file) { load(file); }
652147SN/A    ~SymbolTable() {}
662147SN/A
672147SN/A    void clear();
682222SN/A    bool insert(Addr address, std::string symbol);
692147SN/A    bool load(const std::string &file);
702147SN/A
712147SN/A    const ATable &getAddrTable() const { return addrTable; }
722222SN/A    const STable &getSymbolTable() const { return symbolTable; }
732147SN/A
742147SN/A  public:
752147SN/A    void serialize(const std::string &base, std::ostream &os);
762222SN/A    void unserialize(const std::string &base, Checkpoint *cp,
772147SN/A                     const std::string &section);
782147SN/A
792147SN/A  public:
802222SN/A    bool
812147SN/A    findSymbol(Addr address, std::string &symbol) const
822289SN/A    {
832289SN/A        ATable::const_iterator i = addrTable.find(address);
842289SN/A        if (i == addrTable.end())
852289SN/A            return false;
862147SN/A
872147SN/A        symbol = (*i).second;
882222SN/A        return true;
892147SN/A    }
902147SN/A
912147SN/A    bool
922222SN/A    findAddress(const std::string &symbol, Addr &address) const
932147SN/A    {
942147SN/A        STable::const_iterator i = symbolTable.find(symbol);
952147SN/A        if (i == symbolTable.end())
962222SN/A            return false;
972147SN/A
982147SN/A        address = (*i).second;
992147SN/A        return true;
1002222SN/A    }
1012147SN/A
1022147SN/A    /// Find the nearest symbol equal to or less than the supplied
1032147SN/A    /// address (e.g., the label for the enclosing function).
1042222SN/A    /// @param address The address to look up.
1052147SN/A    /// @param symbol  Return reference for symbol string.
1062147SN/A    /// @param sym_address Return reference for symbol address.
1072147SN/A    /// @param next_sym_address Address of following symbol (for
1082222SN/A    /// determining valid range of symbol).
1092147SN/A    /// @retval True if a symbol was found.
1102174SN/A    bool
1112174SN/A    findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr,
1125569Snate@binkert.org                      Addr &nextaddr) const
1135569Snate@binkert.org    {
1142174SN/A        ATable::const_iterator i;
1152680Sktlim@umich.edu        if (!upperBound(addr, i))
1162222SN/A            return false;
1172174SN/A
1182196SN/A        nextaddr = i->first;
1193521Sgblack@eecs.umich.edu        --i;
1205568Snate@binkert.org        symaddr = i->first;
1212196SN/A        symbol = i->second;
1222201SN/A        return true;
1232196SN/A    }
1245568Snate@binkert.org
1255568Snate@binkert.org    /// Overload for findNearestSymbol() for callers who don't care
1262196SN/A    /// about next_sym_address.
1272196SN/A    bool
1285568Snate@binkert.org    findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr) const
1292680Sktlim@umich.edu    {
1302174SN/A        ATable::const_iterator i;
1312174SN/A        if (!upperBound(addr, i))
1325569Snate@binkert.org            return false;
1335569Snate@binkert.org
1342201SN/A        --i;
1352680Sktlim@umich.edu        symaddr = i->first;
1362201SN/A        symbol = i->second;
1372201SN/A        return true;
1382201SN/A    }
1395569Snate@binkert.org
1405569Snate@binkert.org
1412289SN/A    bool
1422289SN/A    findNearestAddr(Addr addr, Addr &symaddr, Addr &nextaddr) const
1432289SN/A    {
1442289SN/A        ATable::const_iterator i;
1452289SN/A        if (!upperBound(addr, i))
1462289SN/A            return false;
1475569Snate@binkert.org
1486739Sgblack@eecs.umich.edu        nextaddr = i->first;
1492289SN/A        --i;
1505568Snate@binkert.org        symaddr = i->first;
1512289SN/A        return true;
1522289SN/A    }
1535568Snate@binkert.org
1545569Snate@binkert.org    bool
1555569Snate@binkert.org    findNearestAddr(Addr addr, Addr &symaddr) const
1565569Snate@binkert.org    {
1572289SN/A        ATable::const_iterator i;
1582289SN/A        if (!upperBound(addr, i))
1595568Snate@binkert.org            return false;
1605568Snate@binkert.org
1612289SN/A        --i;
1622289SN/A        symaddr = i->first;
1632680Sktlim@umich.edu        return true;
1642289SN/A    }
1652289SN/A};
1665569Snate@binkert.org
1675569Snate@binkert.org/// Global unified debugging symbol table (for target).  Conceptually
1682289SN/A/// there should be one of these per System object for full system,
1692680Sktlim@umich.edu/// and per Process object for non-full-system, but so far one big
1705568Snate@binkert.org/// global one has worked well enough.
1715568Snate@binkert.orgextern SymbolTable *debugSymbolTable;
1725569Snate@binkert.org
1732289SN/A#endif // __SYMTAB_HH__
1742289SN/A