symtab.hh revision 2665
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> 371984SN/A 382650Ssaidi@eecs.umich.edu#include "sim/host.hh" // for Addr 392SN/A 401984SN/Aclass Checkpoint; 412SN/Aclass SymbolTable 422SN/A{ 431912SN/A public: 442130SN/A typedef std::map<Addr, std::string> ATable; 45857SN/A typedef std::map<std::string, Addr> STable; 462SN/A 471912SN/A private: 482SN/A ATable addrTable; 492SN/A STable symbolTable; 502SN/A 511912SN/A private: 521912SN/A bool 531912SN/A upperBound(Addr addr, ATable::const_iterator &iter) const 541912SN/A { 551912SN/A // find first key *larger* than desired address 561912SN/A iter = addrTable.upper_bound(addr); 571912SN/A 581912SN/A // if very first key is larger, we're out of luck 591912SN/A if (iter == addrTable.begin()) 601912SN/A return false; 611912SN/A 621912SN/A return true; 631912SN/A } 641912SN/A 652SN/A public: 662SN/A SymbolTable() {} 672SN/A SymbolTable(const std::string &file) { load(file); } 682SN/A ~SymbolTable() {} 692SN/A 701984SN/A void clear(); 712SN/A bool insert(Addr address, std::string symbol); 722SN/A bool load(const std::string &file); 732SN/A 741912SN/A const ATable &getAddrTable() const { return addrTable; } 751912SN/A const STable &getSymbolTable() const { return symbolTable; } 761912SN/A 771912SN/A public: 781984SN/A void serialize(const std::string &base, std::ostream &os); 791984SN/A void unserialize(const std::string &base, Checkpoint *cp, 801984SN/A const std::string §ion); 811984SN/A 821984SN/A public: 831912SN/A bool 841912SN/A findSymbol(Addr address, std::string &symbol) const 851912SN/A { 861912SN/A ATable::const_iterator i = addrTable.find(address); 871912SN/A if (i == addrTable.end()) 881912SN/A return false; 891912SN/A 901912SN/A symbol = (*i).second; 911912SN/A return true; 921912SN/A } 931912SN/A 941912SN/A bool 951912SN/A findAddress(const std::string &symbol, Addr &address) const 961912SN/A { 971912SN/A STable::const_iterator i = symbolTable.find(symbol); 981912SN/A if (i == symbolTable.end()) 991912SN/A return false; 1001912SN/A 1011912SN/A address = (*i).second; 1021912SN/A return true; 1031912SN/A } 1041912SN/A 1051158SN/A /// Find the nearest symbol equal to or less than the supplied 1061158SN/A /// address (e.g., the label for the enclosing function). 1071158SN/A /// @param address The address to look up. 1081158SN/A /// @param symbol Return reference for symbol string. 1091158SN/A /// @param sym_address Return reference for symbol address. 1101158SN/A /// @param next_sym_address Address of following symbol (for 1111158SN/A /// determining valid range of symbol). 1121158SN/A /// @retval True if a symbol was found. 1131912SN/A bool 1141912SN/A findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr, 1151912SN/A Addr &nextaddr) const 1161912SN/A { 1171912SN/A ATable::const_iterator i; 1181912SN/A if (!upperBound(addr, i)) 1191912SN/A return false; 1201912SN/A 1211912SN/A nextaddr = i->first; 1221912SN/A --i; 1231912SN/A symaddr = i->first; 1241912SN/A symbol = i->second; 1251912SN/A return true; 1261912SN/A } 1271158SN/A 1281158SN/A /// Overload for findNearestSymbol() for callers who don't care 1291158SN/A /// about next_sym_address. 1301912SN/A bool 1311912SN/A findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr) const 1321158SN/A { 1331912SN/A ATable::const_iterator i; 1341912SN/A if (!upperBound(addr, i)) 1351912SN/A return false; 1361912SN/A 1371912SN/A --i; 1381912SN/A symaddr = i->first; 1391912SN/A symbol = i->second; 1401912SN/A return true; 1411158SN/A } 1421158SN/A 1431158SN/A 1441912SN/A bool 1451912SN/A findNearestAddr(Addr addr, Addr &symaddr, Addr &nextaddr) const 1461912SN/A { 1471912SN/A ATable::const_iterator i; 1481912SN/A if (!upperBound(addr, i)) 1491912SN/A return false; 1501912SN/A 1511912SN/A nextaddr = i->first; 1521912SN/A --i; 1531912SN/A symaddr = i->first; 1541912SN/A return true; 1551912SN/A } 1561912SN/A 1571912SN/A bool 1581912SN/A findNearestAddr(Addr addr, Addr &symaddr) const 1591912SN/A { 1601912SN/A ATable::const_iterator i; 1611912SN/A if (!upperBound(addr, i)) 1621912SN/A return false; 1631912SN/A 1641912SN/A --i; 1651912SN/A symaddr = i->first; 1661912SN/A return true; 1671912SN/A } 1682SN/A}; 1692SN/A 1701158SN/A/// Global unified debugging symbol table (for target). Conceptually 1711158SN/A/// there should be one of these per System object for full system, 1721158SN/A/// and per Process object for non-full-system, but so far one big 1731158SN/A/// global one has worked well enough. 1741158SN/Aextern SymbolTable *debugSymbolTable; 1751158SN/A 1762SN/A#endif // __SYMTAB_HH__ 177