Bochs/bochs/bx_debug/symbols.cc

420 lines
9.7 KiB
C++
Raw Normal View History

2006-01-15 20:57:48 +03:00
/////////////////////////////////////////////////////////////////////////
// $Id$
2006-01-15 20:57:48 +03:00
/////////////////////////////////////////////////////////////////////////
//
2009-12-04 19:53:12 +03:00
// Copyright (C) 2001-2009 The Bochs Project
2006-01-15 20:57:48 +03:00
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2006-01-15 20:57:48 +03:00
/////////////////////////////////////////////////////////////////////////
#include "bochs.h"
#include "cpu/cpu.h"
2006-01-15 20:57:48 +03:00
#if BX_DEBUGGER
#if !((BX_HAVE_MAP || BX_HAVE_MAP_H) && (BX_HAVE_SET || BX_HAVE_SET_H))
2006-01-15 20:57:48 +03:00
static const char BX_HAVE_MAP_ERR[] = "context not implemented because BX_HAVE_MAP=0\n";
2006-01-15 20:57:48 +03:00
2009-05-03 23:21:38 +04:00
const char* bx_dbg_symbolic_address(Bit32u context, Bit32u eip, Bit32u base)
2006-01-15 20:57:48 +03:00
{
static bx_bool first = true;
if (first) {
dbg_printf(BX_HAVE_MAP_ERR);
2006-01-15 20:57:48 +03:00
first = false;
}
return "unk. ctxt";
}
2009-05-03 23:21:38 +04:00
int bx_dbg_symbol_command(const char* filename, bx_bool global, Bit32u offset)
2006-01-15 20:57:48 +03:00
{
dbg_printf(BX_HAVE_MAP_ERR);
return -1;
2006-01-15 20:57:48 +03:00
}
2013-06-26 22:07:01 +04:00
void bx_dbg_info_symbols_command(const char *symbol)
2006-01-15 20:57:48 +03:00
{
dbg_printf(BX_HAVE_MAP_ERR);
2006-01-15 20:57:48 +03:00
}
2013-06-26 22:07:01 +04:00
int bx_dbg_lbreakpoint_symbol_command(const char *symbol)
2006-01-15 20:57:48 +03:00
{
dbg_printf(BX_HAVE_MAP_ERR);
2006-01-15 20:57:48 +03:00
return -1;
}
2013-06-26 22:07:01 +04:00
Bit32u bx_dbg_get_symbol_value(const char *symbol)
2006-01-15 20:57:48 +03:00
{
return 0;
}
2009-05-03 23:21:38 +04:00
const char* bx_dbg_disasm_symbolic_address(Bit32u eip, Bit32u base)
2006-01-15 20:57:48 +03:00
{
return 0;
}
#else /* if BX_HAVE_MAP == 1 */
2006-01-15 20:57:48 +03:00
#if BX_HAVE_MAP
#include <map>
#elif BX_HAVE_MAP_H
#include <map.h>
2006-01-15 20:57:48 +03:00
#endif
#if BX_HAVE_SET
#include <set>
#elif BX_HAVE_SET_H
#include <set.h>
#endif
struct symbol_entry_t
{
symbol_entry_t (Bit32u _start = 0, const char* _name = 0)
2006-01-15 20:57:48 +03:00
{
start = _start;
name = _name ? strdup(_name) : 0;
}
~symbol_entry_t()
{
if (name)
free(name);
2006-01-15 20:57:48 +03:00
}
void trim_quotes(void);
2006-01-15 20:57:48 +03:00
char* name;
Bit32u start;
private:
symbol_entry_t(const symbol_entry_t&); // No definition
void operator=(const symbol_entry_t &); // No definition
2006-01-15 20:57:48 +03:00
};
struct lt_symbol_entry_t
{
bool operator()(const symbol_entry_t* s1, const symbol_entry_t* s2) const
{
return s1->start < s2->start;
}
};
struct lt_rsymbol_entry_t
{
bool operator()(const symbol_entry_t* s1, const symbol_entry_t* s2) const
{
return strcoll(s1->name, s2->name) < 0;
}
};
struct context_t
{
typedef std::set<symbol_entry_t*,lt_symbol_entry_t> sym_set_t;
typedef std::set<symbol_entry_t*,lt_rsymbol_entry_t> rsym_set_t;
typedef std::map<Bit32u,context_t*> map_t;
2006-01-15 20:57:48 +03:00
context_t (Bit32u);
~context_t();
2006-01-15 20:57:48 +03:00
static context_t* get_context(Bit32u);
symbol_entry_t* get_symbol_entry(Bit32u);
2013-06-26 22:07:01 +04:00
symbol_entry_t* get_symbol_entry(const char *symbol) const;
2006-01-15 20:57:48 +03:00
void add_symbol(symbol_entry_t*);
const sym_set_t* get_all_symbols() const {return &m_syms;}
const rsym_set_t* get_all_rsymbols() const {return &m_rsyms;}
friend class context_dtor_t;
protected:
static map_t s_map;
2006-01-15 20:57:48 +03:00
// Forvard references (find name by address)
sym_set_t m_syms;
2006-01-15 20:57:48 +03:00
// Reverse references (find address by name)
rsym_set_t m_rsyms;
Bit32u m_id;
2006-01-15 20:57:48 +03:00
};
context_t::map_t context_t::s_map;
2006-01-15 20:57:48 +03:00
context_t::context_t (Bit32u id)
2006-01-15 20:57:48 +03:00
{
m_id = id;
s_map[m_id] = this;
2006-01-15 20:57:48 +03:00
}
context_t::~context_t()
{
while (!m_syms.empty())
{
sym_set_t::iterator it = m_syms.begin();
symbol_entry_t *sym = *it;
m_syms.erase(it);
delete sym;
}
2006-01-15 20:57:48 +03:00
}
context_t* context_t::get_context(Bit32u i)
{
return s_map[i];
2006-01-15 20:57:48 +03:00
}
symbol_entry_t* context_t::get_symbol_entry(Bit32u ip)
{
symbol_entry_t probe(ip, 0);
2006-01-15 20:57:48 +03:00
// find the first symbol whose address is greater than ip.
if (m_syms.empty ())
return 0;
sym_set_t::iterator iter = m_syms.upper_bound(&probe);
2006-01-15 20:57:48 +03:00
if (iter == m_syms.end()) { // No symbol found
2006-01-15 20:57:48 +03:00
return 0;
}
--iter;
if(iter == m_syms.end())
return 0;
return *iter;
2006-01-15 20:57:48 +03:00
}
2013-06-26 22:07:01 +04:00
symbol_entry_t* context_t::get_symbol_entry(const char *symbol) const
2006-01-15 20:57:48 +03:00
{
if (m_rsyms.empty())
return 0;
2006-01-15 20:57:48 +03:00
2013-06-26 22:07:01 +04:00
symbol_entry_t probe(0, symbol);
rsym_set_t::const_iterator iter;
iter=m_rsyms.find(&probe);
if(iter==m_rsyms.end()) // No symbol found
return 0;
2006-01-15 20:57:48 +03:00
return *iter;
}
void context_t::add_symbol(symbol_entry_t* sym)
{
m_syms.insert(sym);
m_rsyms.insert(sym);
}
void symbol_entry_t::trim_quotes(void)
{
size_t len = strlen(name);
if (name[0] == '\"' && name[len-1] == '\"')
{
memmove(name, name+1, len);
name[len-2] = 0;
}
2006-01-15 20:57:48 +03:00
}
2013-06-26 22:07:01 +04:00
Bit32u bx_dbg_get_symbol_value(const char *symbol)
2006-01-15 20:57:48 +03:00
{
context_t* cntx = context_t::get_context(0);
if(!cntx) // Context not found
return 0;
2006-01-15 20:57:48 +03:00
2013-06-26 22:07:01 +04:00
symbol_entry_t s(0, symbol);
s.trim_quotes();
2006-01-15 20:57:48 +03:00
symbol_entry_t* sym=cntx->get_symbol_entry(s.name);
2013-06-26 22:07:01 +04:00
if(!sym) // symbol not found
return 0;
2006-01-15 20:57:48 +03:00
return sym->start;
2006-01-15 20:57:48 +03:00
}
2009-05-03 23:21:38 +04:00
const char* bx_dbg_symbolic_address(Bit32u context, Bit32u eip, Bit32u base)
2006-01-15 20:57:48 +03:00
{
static char buf[80];
2012-05-05 22:40:37 +04:00
2006-01-15 20:57:48 +03:00
// Look up this context
context_t* cntx = context_t::get_context(context);
if (!cntx) {
// Try global context
cntx = context_t::get_context(0);
if (!cntx) {
snprintf (buf, 80, "unk. ctxt");
return buf;
}
}
// full linear address not only eip (for nonzero based segments)
symbol_entry_t* entr = cntx->get_symbol_entry(base+eip);
2006-01-15 20:57:48 +03:00
if (!entr) {
snprintf (buf, 80, "no symbol");
return buf;
}
snprintf (buf, 80, "%s+%x", entr->name, (base+eip) - entr->start);
return buf;
}
2009-05-03 23:21:38 +04:00
const char* bx_dbg_disasm_symbolic_address(Bit32u eip, Bit32u base)
2006-01-15 20:57:48 +03:00
{
static char buf[80];
// Try global context
context_t* cntx = context_t::get_context(0);
if (!cntx) {
return 0;
}
// full linear address not only eip (for nonzero based segments)
symbol_entry_t* entr = cntx->get_symbol_entry(base+eip);
2006-01-15 20:57:48 +03:00
if (!entr) {
return 0;
}
snprintf (buf, 80, "%s+%x", entr->name, (base+eip) - entr->start);
return buf;
}
2009-05-03 23:21:38 +04:00
int bx_dbg_symbol_command(const char* filename, bx_bool global, Bit32u offset)
2006-01-15 20:57:48 +03:00
{
2009-05-03 23:21:38 +04:00
symbol_entry_t file(0, filename);
file.trim_quotes();
2006-01-15 20:57:48 +03:00
// Install symbols in correct context (page table)
// The file format should be
// address symbol (example '00002afe _StartLoseNT')
Bit32u context_id = (global) ? 0 : (BX_CPU(dbg_cpu)->cr3) >> 12;
context_t* cntx = context_t::get_context(context_id);
2006-01-15 20:57:48 +03:00
if (!cntx) {
cntx = new context_t(context_id);
2006-01-15 20:57:48 +03:00
}
FILE* fp = fopen(file.name, "rt"); // 't' is need for win32, unixes simply ignore it
2006-01-15 20:57:48 +03:00
if (!fp) {
dbg_printf ("Could not open symbol file '%s'\n", file.name);
return -1;
2006-01-15 20:57:48 +03:00
}
// C++/C# symbols can be long
char buf[512];
int line_num = 1;
while (fgets(buf, sizeof(buf), fp)) {
// handle end of line (before error messages)
int len = strlen(buf);
bool whole_line = (buf[len - 1] == '\n');
if (whole_line)
buf[len - 1] = 0;
// parse
char* sym_name;
Bit32u addr = strtoul(buf, &sym_name, 16);
if (!isspace(*sym_name)) {
if (*sym_name == 0)
dbg_printf("%s:%d: missing symbol name\n", file.name, line_num);
else
dbg_printf("%s:%d: syntax error near '%s'\n", file.name, line_num, sym_name);
return -1;
2006-01-15 20:57:48 +03:00
}
++sym_name;
symbol_entry_t* sym = new symbol_entry_t(addr + offset, sym_name);
2006-01-15 20:57:48 +03:00
cntx->add_symbol(sym);
// skip the rest of long line
while (!whole_line) {
if (!fgets(buf, sizeof(buf), fp))
break;
// actually, last line can end without newline, but then
// we'll just break at the next iteration because of EOF
whole_line = (buf[strlen(buf)-1] == '\n');
}
++line_num;
2006-01-15 20:57:48 +03:00
}
return 0;
2006-01-15 20:57:48 +03:00
}
// check if s1 is prefix of s2
2006-01-15 20:57:48 +03:00
static bool bx_dbg_strprefix(const char *s1, const char *s2)
{
if(!s1 || !s2)
return false;
2006-01-15 20:57:48 +03:00
size_t len=strlen(s1);
if(len>strlen(s2))
return false;
2006-01-15 20:57:48 +03:00
return strncmp(s1, s2, len)==0;
}
2013-06-26 22:07:01 +04:00
void bx_dbg_info_symbols_command(const char *symbol)
2006-01-15 20:57:48 +03:00
{
context_t* cntx = context_t::get_context(0);
if(!cntx) {
dbg_printf ("Global context not available\n");
return;
2006-01-15 20:57:48 +03:00
}
2013-06-26 22:07:01 +04:00
if(symbol) {
const context_t::rsym_set_t* rsyms;
2006-01-15 20:57:48 +03:00
rsyms=cntx->get_all_rsymbols();
if (rsyms->empty ()) {
2013-06-26 22:07:01 +04:00
dbg_printf ("symbols not loaded\n");
return;
}
2013-06-26 22:07:01 +04:00
symbol_entry_t probe(0, symbol);
// remove leading and trailing quotas
probe.trim_quotes();
context_t::rsym_set_t::const_iterator iter;
iter=rsyms->lower_bound(&probe);
if(iter==rsyms->end() || !bx_dbg_strprefix(probe.name, (*iter)->name))
dbg_printf ("No symbols found\n");
else {
for(;iter!=rsyms->end() && bx_dbg_strprefix(probe.name, (*iter)->name);++iter) {
dbg_printf ("%08x: %s\n", (*iter)->start, (*iter)->name);
}
}
2006-01-15 20:57:48 +03:00
}
else {
const context_t::sym_set_t* syms;
2006-01-15 20:57:48 +03:00
syms=cntx->get_all_symbols();
2013-06-26 22:07:01 +04:00
if (syms->empty()) {
dbg_printf ("symbols not loaded\n");
return;
}
2006-01-15 20:57:48 +03:00
context_t::sym_set_t::const_iterator iter;
for(iter = syms->begin();iter!=syms->end();++iter) {
dbg_printf ("%08x: %s\n", (*iter)->start, (*iter)->name);
}
2006-01-15 20:57:48 +03:00
}
}
2009-05-03 23:21:38 +04:00
int bx_dbg_lbreakpoint_symbol_command(const char *symbol)
2006-01-15 20:57:48 +03:00
{
context_t* cntx = context_t::get_context(0);
if(!cntx) {
dbg_printf ("Global context not available\n");
return -1;
}
symbol_entry_t probe(0, symbol);
// remove leading and trailing quotes
probe.trim_quotes();
const symbol_entry_t* sym=cntx->get_symbol_entry(probe.name);
if(sym)
return bx_dbg_lbreakpoint_command(bkRegular, sym->start);
2013-06-26 22:07:01 +04:00
dbg_printf ("symbol not found\n");
2006-01-15 20:57:48 +03:00
return -1;
}
2006-01-15 20:57:48 +03:00
#endif
#endif