symtab.cc revision 1984
16657Snate@binkert.org/* 26657Snate@binkert.org * Copyright (c) 2002-2005 The Regents of The University of Michigan 36657Snate@binkert.org * All rights reserved. 46657Snate@binkert.org * 56657Snate@binkert.org * Redistribution and use in source and binary forms, with or without 66657Snate@binkert.org * modification, are permitted provided that the following conditions are 76657Snate@binkert.org * met: redistributions of source code must retain the above copyright 86657Snate@binkert.org * notice, this list of conditions and the following disclaimer; 96657Snate@binkert.org * redistributions in binary form must reproduce the above copyright 106657Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 116657Snate@binkert.org * documentation and/or other materials provided with the distribution; 126657Snate@binkert.org * neither the name of the copyright holders nor the names of its 136657Snate@binkert.org * contributors may be used to endorse or promote products derived from 146657Snate@binkert.org * this software without specific prior written permission. 156657Snate@binkert.org * 166657Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 176657Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 186657Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 196657Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 206657Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 216657Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 226657Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236657Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246657Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256657Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266657Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276657Snate@binkert.org */ 286657Snate@binkert.org 296657Snate@binkert.org#include <iostream> 306657Snate@binkert.org#include <fstream> 316657Snate@binkert.org#include <string> 326657Snate@binkert.org#include <vector> 336999Snate@binkert.org 346657Snate@binkert.org#include "base/loader/symtab.hh" 356657Snate@binkert.org#include "base/misc.hh" 366657Snate@binkert.org#include "base/str.hh" 376657Snate@binkert.org#include "sim/host.hh" 386657Snate@binkert.org#include "sim/serialize.hh" 396657Snate@binkert.org 406657Snate@binkert.orgusing namespace std; 416657Snate@binkert.org 426657Snate@binkert.orgSymbolTable *debugSymbolTable = NULL; 436657Snate@binkert.org 446657Snate@binkert.orgvoid 456657Snate@binkert.orgSymbolTable::clear() 466657Snate@binkert.org{ 476657Snate@binkert.org addrTable.clear(); 486657Snate@binkert.org symbolTable.clear(); 496657Snate@binkert.org} 506657Snate@binkert.org 516657Snate@binkert.orgbool 526657Snate@binkert.orgSymbolTable::insert(Addr address, string symbol) 536657Snate@binkert.org{ 546999Snate@binkert.org if (symbol.empty()) 556657Snate@binkert.org return false; 566657Snate@binkert.org 576657Snate@binkert.org if (!addrTable.insert(make_pair(address, symbol)).second) 586999Snate@binkert.org return false; 596999Snate@binkert.org 606999Snate@binkert.org if (!symbolTable.insert(make_pair(symbol, address)).second) 616999Snate@binkert.org return false; 626999Snate@binkert.org 636999Snate@binkert.org return true; 646999Snate@binkert.org} 656657Snate@binkert.org 666657Snate@binkert.org 676657Snate@binkert.orgbool 686657Snate@binkert.orgSymbolTable::load(const string &filename) 696657Snate@binkert.org{ 706657Snate@binkert.org string buffer; 716657Snate@binkert.org ifstream file(filename.c_str()); 726657Snate@binkert.org 736657Snate@binkert.org if (!file) 746657Snate@binkert.org fatal("file error: Can't open symbol table file %s\n", filename); 756657Snate@binkert.org 766657Snate@binkert.org while (!file.eof()) { 776657Snate@binkert.org getline(file, buffer); 786657Snate@binkert.org if (buffer.empty()) 796657Snate@binkert.org continue; 806657Snate@binkert.org 816657Snate@binkert.org int idx = buffer.find(','); 826657Snate@binkert.org if (idx == string::npos) 836657Snate@binkert.org return false; 846657Snate@binkert.org 856657Snate@binkert.org string address = buffer.substr(0, idx); 866657Snate@binkert.org eat_white(address); 876657Snate@binkert.org if (address.empty()) 886657Snate@binkert.org return false; 896657Snate@binkert.org 906657Snate@binkert.org string symbol = buffer.substr(idx + 1); 916657Snate@binkert.org eat_white(symbol); 926657Snate@binkert.org if (symbol.empty()) 936657Snate@binkert.org return false; 946657Snate@binkert.org 956657Snate@binkert.org Addr addr; 966657Snate@binkert.org if (!to_number(address, addr)) 976657Snate@binkert.org return false; 986657Snate@binkert.org 996657Snate@binkert.org if (!insert(addr, symbol)) 1006657Snate@binkert.org return false; 1016657Snate@binkert.org } 1026657Snate@binkert.org 1036657Snate@binkert.org file.close(); 1046657Snate@binkert.org 1056657Snate@binkert.org return true; 1066657Snate@binkert.org} 1076657Snate@binkert.org 1086657Snate@binkert.orgvoid 1096657Snate@binkert.orgSymbolTable::serialize(const string &base, ostream &os) 1106657Snate@binkert.org{ 1116657Snate@binkert.org paramOut(os, base + ".size", addrTable.size()); 1126657Snate@binkert.org 1136657Snate@binkert.org int i = 0; 1146657Snate@binkert.org ATable::const_iterator p, end = addrTable.end(); 1156657Snate@binkert.org for (p = addrTable.begin(); p != end; ++p) { 1166657Snate@binkert.org paramOut(os, csprintf("%s.addr_%d", base, i), p->first); 1176657Snate@binkert.org paramOut(os, csprintf("%s.symbol_%d", base, i), p->second); 1186657Snate@binkert.org ++i; 1196657Snate@binkert.org } 1206657Snate@binkert.org} 1216657Snate@binkert.org 1226657Snate@binkert.orgvoid 1236657Snate@binkert.orgSymbolTable::unserialize(const string &base, Checkpoint *cp, 1246714Ssteve.reinhardt@amd.com const string §ion) 1256714Ssteve.reinhardt@amd.com{ 1266657Snate@binkert.org clear(); 1276657Snate@binkert.org int size; 1286657Snate@binkert.org paramIn(cp, section, base + ".size", size); 1296657Snate@binkert.org for (int i = 0; i < size; ++i) { 1306714Ssteve.reinhardt@amd.com Addr addr; 1316657Snate@binkert.org std::string symbol; 1326714Ssteve.reinhardt@amd.com 1336657Snate@binkert.org paramIn(cp, section, csprintf("%s.addr_%d", base, i), addr); 1346657Snate@binkert.org paramIn(cp, section, csprintf("%s.symbol_%d", base, i), symbol); 1356657Snate@binkert.org insert(addr, symbol); 1366657Snate@binkert.org } 1376657Snate@binkert.org} 1386657Snate@binkert.org