symtab.hh revision 5952
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 * Authors: Nathan Binkert 292665Ssaidi@eecs.umich.edu * Steve Reinhardt 302SN/A */ 312SN/A 322SN/A#ifndef __SYMTAB_HH__ 332SN/A#define __SYMTAB_HH__ 342SN/A 351984SN/A#include <iosfwd> 36857SN/A#include <map> 375952Ssaidi@eecs.umich.edu#include <string> 381984SN/A 395543Ssaidi@eecs.umich.edu#include "sim/host.hh" // for Addr 402SN/A 411984SN/Aclass Checkpoint; 422SN/Aclass SymbolTable 432SN/A{ 441912SN/A public: 452130SN/A typedef std::map<Addr, std::string> ATable; 46857SN/A typedef std::map<std::string, Addr> STable; 472SN/A 481912SN/A private: 492SN/A ATable addrTable; 502SN/A STable symbolTable; 512SN/A 521912SN/A private: 531912SN/A bool 541912SN/A upperBound(Addr addr, ATable::const_iterator &iter) const 551912SN/A { 561912SN/A // find first key *larger* than desired address 571912SN/A iter = addrTable.upper_bound(addr); 581912SN/A 591912SN/A // if very first key is larger, we're out of luck 601912SN/A if (iter == addrTable.begin()) 611912SN/A return false; 621912SN/A 631912SN/A return true; 641912SN/A } 651912SN/A 662SN/A public: 672SN/A SymbolTable() {} 682SN/A SymbolTable(const std::string &file) { load(file); } 692SN/A ~SymbolTable() {} 702SN/A 711984SN/A void clear(); 722SN/A bool insert(Addr address, std::string symbol); 732SN/A bool load(const std::string &file); 742SN/A 751912SN/A const ATable &getAddrTable() const { return addrTable; } 761912SN/A const STable &getSymbolTable() const { return symbolTable; } 771912SN/A 781912SN/A public: 791984SN/A void serialize(const std::string &base, std::ostream &os); 801984SN/A void unserialize(const std::string &base, Checkpoint *cp, 811984SN/A const std::string §ion); 821984SN/A 831984SN/A public: 841912SN/A bool 851912SN/A findSymbol(Addr address, std::string &symbol) const 861912SN/A { 871912SN/A ATable::const_iterator i = addrTable.find(address); 881912SN/A if (i == addrTable.end()) 891912SN/A return false; 901912SN/A 911912SN/A symbol = (*i).second; 921912SN/A return true; 931912SN/A } 941912SN/A 951912SN/A bool 961912SN/A findAddress(const std::string &symbol, Addr &address) const 971912SN/A { 981912SN/A STable::const_iterator i = symbolTable.find(symbol); 991912SN/A if (i == symbolTable.end()) 1001912SN/A return false; 1011912SN/A 1021912SN/A address = (*i).second; 1031912SN/A return true; 1041912SN/A } 1051912SN/A 1061158SN/A /// Find the nearest symbol equal to or less than the supplied 1071158SN/A /// address (e.g., the label for the enclosing function). 1082982Sstever@eecs.umich.edu /// @param addr The address to look up. 1092982Sstever@eecs.umich.edu /// @param symbol Return reference for symbol string. 1102982Sstever@eecs.umich.edu /// @param symaddr Return reference for symbol address. 1112982Sstever@eecs.umich.edu /// @param nextaddr Address of following symbol (for 1122982Sstever@eecs.umich.edu /// determining valid range of symbol). 1131158SN/A /// @retval True if a symbol was found. 1141912SN/A bool 1151912SN/A findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr, 1161912SN/A Addr &nextaddr) const 1171912SN/A { 1181912SN/A ATable::const_iterator i; 1191912SN/A if (!upperBound(addr, i)) 1201912SN/A return false; 1211912SN/A 1221912SN/A nextaddr = i->first; 1231912SN/A --i; 1241912SN/A symaddr = i->first; 1251912SN/A symbol = i->second; 1261912SN/A return true; 1271912SN/A } 1281158SN/A 1291158SN/A /// Overload for findNearestSymbol() for callers who don't care 1302982Sstever@eecs.umich.edu /// about nextaddr. 1311912SN/A bool 1321912SN/A findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr) const 1331158SN/A { 1341912SN/A ATable::const_iterator i; 1351912SN/A if (!upperBound(addr, i)) 1361912SN/A return false; 1371912SN/A 1381912SN/A --i; 1391912SN/A symaddr = i->first; 1401912SN/A symbol = i->second; 1411912SN/A return true; 1421158SN/A } 1431158SN/A 1441158SN/A 1451912SN/A bool 1461912SN/A findNearestAddr(Addr addr, Addr &symaddr, Addr &nextaddr) const 1471912SN/A { 1481912SN/A ATable::const_iterator i; 1491912SN/A if (!upperBound(addr, i)) 1501912SN/A return false; 1511912SN/A 1521912SN/A nextaddr = i->first; 1531912SN/A --i; 1541912SN/A symaddr = i->first; 1551912SN/A return true; 1561912SN/A } 1571912SN/A 1581912SN/A bool 1591912SN/A findNearestAddr(Addr addr, Addr &symaddr) const 1601912SN/A { 1611912SN/A ATable::const_iterator i; 1621912SN/A if (!upperBound(addr, i)) 1631912SN/A return false; 1641912SN/A 1651912SN/A --i; 1661912SN/A symaddr = i->first; 1671912SN/A return true; 1681912SN/A } 1692SN/A}; 1702SN/A 1711158SN/A/// Global unified debugging symbol table (for target). Conceptually 1721158SN/A/// there should be one of these per System object for full system, 1731158SN/A/// and per Process object for non-full-system, but so far one big 1741158SN/A/// global one has worked well enough. 1751158SN/Aextern SymbolTable *debugSymbolTable; 1761158SN/A 1772SN/A#endif // __SYMTAB_HH__ 178