2010-01-05 11:09:08 +00:00
|
|
|
/* MSPDebug - debugging tool for the eZ430
|
2010-01-08 08:17:24 +00:00
|
|
|
* Copyright (C) 2009, 2010 Daniel Beer
|
2010-01-05 11:09:08 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-03-27 09:50:02 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2010-05-13 00:57:21 +00:00
|
|
|
#include <stdint.h>
|
2010-03-27 09:50:02 +00:00
|
|
|
#include <regex.h>
|
2010-03-31 06:33:28 +00:00
|
|
|
#include <errno.h>
|
2010-04-21 02:12:12 +00:00
|
|
|
#include <assert.h>
|
2010-03-27 09:50:02 +00:00
|
|
|
#include "btree.h"
|
2010-04-10 02:00:00 +00:00
|
|
|
#include "stab.h"
|
2010-04-21 02:12:12 +00:00
|
|
|
#include "util.h"
|
2010-08-16 23:07:03 +00:00
|
|
|
#include "output.h"
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
stab_t stab_default;
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
/************************************************************************
|
|
|
|
* B+Tree definitions
|
|
|
|
*/
|
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
struct sym_key {
|
|
|
|
char name[64];
|
2010-01-05 11:09:08 +00:00
|
|
|
};
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
static const struct sym_key sym_key_zero = {
|
|
|
|
.name = {0}
|
|
|
|
};
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
static int sym_key_compare(const void *left, const void *right)
|
|
|
|
{
|
|
|
|
return strcmp(((const struct sym_key *)left)->name,
|
|
|
|
((const struct sym_key *)right)->name);
|
|
|
|
}
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
static void sym_key_init(struct sym_key *key, const char *text)
|
2010-01-05 11:09:08 +00:00
|
|
|
{
|
2010-03-27 09:50:02 +00:00
|
|
|
int len = strlen(text);
|
|
|
|
|
|
|
|
if (len >= sizeof(key->name))
|
|
|
|
len = sizeof(key->name) - 1;
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
memcpy(key->name, text, len);
|
|
|
|
key->name[len] = 0;
|
2010-01-05 11:09:08 +00:00
|
|
|
}
|
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
struct addr_key {
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t addr;
|
|
|
|
char name[64];
|
2010-03-27 09:50:02 +00:00
|
|
|
};
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
static const struct addr_key addr_key_zero = {
|
|
|
|
.addr = 0,
|
|
|
|
.name = {0}
|
|
|
|
};
|
2010-03-27 09:50:02 +00:00
|
|
|
|
|
|
|
static int addr_key_compare(const void *left, const void *right)
|
2010-01-05 11:09:08 +00:00
|
|
|
{
|
2010-03-27 09:50:02 +00:00
|
|
|
const struct addr_key *kl = (const struct addr_key *)left;
|
|
|
|
const struct addr_key *kr = (const struct addr_key *)right;
|
|
|
|
|
|
|
|
if (kl->addr < kr->addr)
|
|
|
|
return -1;
|
|
|
|
if (kl->addr > kr->addr)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return strcmp(kl->name, kr->name);
|
2010-01-05 11:09:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 01:59:33 +00:00
|
|
|
static void addr_key_init(struct addr_key *key, address_t addr,
|
2010-03-27 09:50:02 +00:00
|
|
|
const char *text)
|
2010-01-05 11:09:08 +00:00
|
|
|
{
|
2010-03-27 09:50:02 +00:00
|
|
|
int len = strlen(text);
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
if (len >= sizeof(key->name))
|
|
|
|
len = sizeof(key->name) - 1;
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
key->addr = addr;
|
|
|
|
memcpy(key->name, text, len);
|
|
|
|
key->name[len] = 0;
|
|
|
|
}
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
static const struct btree_def sym_table_def = {
|
|
|
|
.compare = sym_key_compare,
|
|
|
|
.zero = &sym_key_zero,
|
|
|
|
.branches = 32,
|
|
|
|
.key_size = sizeof(struct sym_key),
|
2010-08-05 01:59:33 +00:00
|
|
|
.data_size = sizeof(address_t)
|
2010-03-27 09:50:02 +00:00
|
|
|
};
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
static const struct btree_def addr_table_def = {
|
|
|
|
.compare = addr_key_compare,
|
|
|
|
.zero = &addr_key_zero,
|
|
|
|
.branches = 32,
|
|
|
|
.key_size = sizeof(struct addr_key),
|
|
|
|
.data_size = 0
|
|
|
|
};
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
/************************************************************************
|
|
|
|
* Symbol table methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct stab {
|
|
|
|
btree_t sym;
|
|
|
|
btree_t addr;
|
|
|
|
};
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
void stab_clear(stab_t st)
|
2010-03-27 09:50:02 +00:00
|
|
|
{
|
2010-05-01 06:34:43 +00:00
|
|
|
btree_clear(st->sym);
|
|
|
|
btree_clear(st->addr);
|
2010-03-27 09:50:02 +00:00
|
|
|
}
|
2010-01-05 11:09:08 +00:00
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
int stab_set(stab_t st, const char *name, int value)
|
2010-03-27 09:50:02 +00:00
|
|
|
{
|
|
|
|
struct sym_key skey;
|
|
|
|
struct addr_key akey;
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t addr = value;
|
|
|
|
address_t old_addr;
|
2010-03-27 09:50:02 +00:00
|
|
|
|
|
|
|
sym_key_init(&skey, name);
|
|
|
|
|
|
|
|
/* Look for an old address first, and delete the reverse mapping
|
|
|
|
* if it's there.
|
|
|
|
*/
|
2010-05-01 06:34:43 +00:00
|
|
|
if (!btree_get(st->sym, &skey, &old_addr)) {
|
2010-03-27 09:50:02 +00:00
|
|
|
addr_key_init(&akey, old_addr, skey.name);
|
2010-05-01 06:34:43 +00:00
|
|
|
btree_delete(st->addr, &akey);
|
2010-01-05 11:09:08 +00:00
|
|
|
}
|
|
|
|
|
2010-03-27 09:50:02 +00:00
|
|
|
/* Put the new mapping into both tables */
|
|
|
|
addr_key_init(&akey, addr, name);
|
2010-05-01 06:34:43 +00:00
|
|
|
if (btree_put(st->addr, &akey, NULL) < 0 ||
|
|
|
|
btree_put(st->sym, &skey, &addr) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("stab: can't set %s = 0x%04x\n", name, addr);
|
2010-03-27 09:50:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-01-05 11:09:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-05 01:59:33 +00:00
|
|
|
int stab_nearest(stab_t st, address_t addr, char *ret_name, int max_len,
|
|
|
|
address_t *ret_offset)
|
2010-01-05 11:09:08 +00:00
|
|
|
{
|
2010-03-27 09:50:02 +00:00
|
|
|
struct addr_key akey;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
akey.addr = addr;
|
|
|
|
for (i = 0; i < sizeof(akey.name); i++)
|
|
|
|
akey.name[i] = 0xff;
|
|
|
|
akey.name[sizeof(akey.name) - 1] = 0xff;
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
if (!btree_select(st->addr, &akey, BTREE_LE, &akey, NULL)) {
|
2010-03-27 09:50:02 +00:00
|
|
|
strncpy(ret_name, akey.name, max_len);
|
|
|
|
ret_name[max_len - 1] = 0;
|
|
|
|
*ret_offset = addr - akey.addr;
|
|
|
|
return 0;
|
2010-01-05 11:09:08 +00:00
|
|
|
}
|
|
|
|
|
2010-03-28 03:02:04 +00:00
|
|
|
return -1;
|
2010-01-05 11:09:08 +00:00
|
|
|
}
|
2010-03-31 06:33:28 +00:00
|
|
|
|
2010-08-05 01:59:33 +00:00
|
|
|
int stab_get(stab_t st, const char *name, address_t *value)
|
2010-03-31 06:33:28 +00:00
|
|
|
{
|
2010-03-31 07:40:46 +00:00
|
|
|
struct sym_key skey;
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t addr;
|
2010-03-31 07:40:46 +00:00
|
|
|
|
2010-04-10 02:00:00 +00:00
|
|
|
sym_key_init(&skey, name);
|
2010-05-01 06:34:43 +00:00
|
|
|
if (btree_get(st->sym, &skey, &addr))
|
2010-03-31 07:40:46 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-04-10 02:00:00 +00:00
|
|
|
*value = addr;
|
2010-03-31 07:40:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
int stab_del(stab_t st, const char *name)
|
2010-03-31 07:40:46 +00:00
|
|
|
{
|
|
|
|
struct sym_key skey;
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t value;
|
2010-03-31 07:40:46 +00:00
|
|
|
struct addr_key akey;
|
|
|
|
|
|
|
|
sym_key_init(&skey, name);
|
2010-05-01 06:34:43 +00:00
|
|
|
if (btree_get(st->sym, &skey, &value))
|
2010-03-31 07:40:46 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
addr_key_init(&akey, value, name);
|
2010-05-01 06:34:43 +00:00
|
|
|
btree_delete(st->sym, &skey);
|
|
|
|
btree_delete(st->addr, &akey);
|
2010-03-31 07:40:46 +00:00
|
|
|
|
2010-03-31 06:33:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
int stab_enum(stab_t st, stab_callback_t cb, void *user_data)
|
2010-03-31 06:33:28 +00:00
|
|
|
{
|
2010-04-10 02:00:00 +00:00
|
|
|
int ret;
|
|
|
|
struct addr_key akey;
|
2010-03-31 06:33:28 +00:00
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
ret = btree_select(st->addr, NULL, BTREE_FIRST,
|
2010-04-10 02:00:00 +00:00
|
|
|
&akey, NULL);
|
|
|
|
while (!ret) {
|
2010-04-30 10:17:43 +00:00
|
|
|
if (cb(user_data, akey.name, akey.addr) < 0)
|
2010-03-31 06:33:28 +00:00
|
|
|
return -1;
|
2010-05-01 06:34:43 +00:00
|
|
|
ret = btree_select(st->addr, NULL, BTREE_NEXT,
|
2010-04-10 02:00:00 +00:00
|
|
|
&akey, NULL);
|
2010-03-31 06:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
stab_t stab_new(void)
|
2010-03-31 06:33:28 +00:00
|
|
|
{
|
2010-05-01 06:34:43 +00:00
|
|
|
stab_t st = malloc(sizeof(*st));
|
|
|
|
|
|
|
|
if (!st) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("stab: failed to allocate memory\n");
|
2010-05-01 06:34:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
st->sym = btree_alloc(&sym_table_def);
|
|
|
|
if (!st->sym) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("stab: failed to allocate symbol table\n");
|
2010-05-01 06:34:43 +00:00
|
|
|
free(st);
|
|
|
|
return NULL;
|
2010-03-31 06:33:28 +00:00
|
|
|
}
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
st->addr = btree_alloc(&addr_table_def);
|
|
|
|
if (!st->addr) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("stab: failed to allocate address table\n");
|
2010-05-01 06:34:43 +00:00
|
|
|
btree_free(st->sym);
|
|
|
|
free(st);
|
|
|
|
return NULL;
|
2010-03-31 06:33:28 +00:00
|
|
|
}
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
return st;
|
2010-03-31 06:33:28 +00:00
|
|
|
}
|
|
|
|
|
2010-05-01 06:34:43 +00:00
|
|
|
void stab_destroy(stab_t st)
|
2010-03-31 06:33:28 +00:00
|
|
|
{
|
2010-05-01 06:34:43 +00:00
|
|
|
btree_free(st->sym);
|
|
|
|
btree_free(st->addr);
|
|
|
|
free(st);
|
2010-03-31 06:33:28 +00:00
|
|
|
}
|