1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/* MSPDebug - debugging tool for the eZ430
* Copyright (C) 2009, 2010 Daniel Beer
*
* 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>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <regex.h>
#include <assert.h>
#include "btree.h"
#include "stab.h"
#include "util.h"
#include "output.h"
/************************************************************************
* B+Tree definitions
*/
struct sym_key {
char name[MAX_SYMBOL_LENGTH];
};
static const struct sym_key sym_key_zero = {
.name = {0}
};
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);
}
static void sym_key_init(struct sym_key *key, const char *text)
{
int len = strlen(text);
if (len >= sizeof(key->name))
len = sizeof(key->name) - 1;
memcpy(key->name, text, len);
key->name[len] = 0;
}
struct addr_key {
address_t addr;
char name[MAX_SYMBOL_LENGTH];
};
static const struct addr_key addr_key_zero = {
.addr = 0,
.name = {0}
};
static int addr_key_compare(const void *left, const void *right)
{
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);
}
static void addr_key_init(struct addr_key *key, address_t addr,
const char *text)
{
int len = strlen(text);
if (len >= sizeof(key->name))
len = sizeof(key->name) - 1;
key->addr = addr;
memcpy(key->name, text, len);
key->name[len] = 0;
}
static const struct btree_def sym_table_def = {
.compare = sym_key_compare,
.zero = &sym_key_zero,
.branches = 32,
.key_size = sizeof(struct sym_key),
.data_size = sizeof(address_t)
};
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
};
/************************************************************************
* Symbol table methods
*/
static btree_t stab_sym;
static btree_t stab_addr;
void stab_clear(void)
{
btree_clear(stab_sym);
btree_clear(stab_addr);
}
int stab_set(const char *name, int value)
{
struct sym_key skey;
struct addr_key akey;
address_t addr = value;
address_t old_addr;
sym_key_init(&skey, name);
/* Look for an old address first, and delete the reverse mapping
* if it's there.
*/
if (!btree_get(stab_sym, &skey, &old_addr)) {
addr_key_init(&akey, old_addr, skey.name);
btree_delete(stab_addr, &akey);
}
/* Put the new mapping into both tables */
addr_key_init(&akey, addr, name);
if (btree_put(stab_addr, &akey, NULL) < 0 ||
btree_put(stab_sym, &skey, &addr) < 0) {
printc_err("stab: can't set %s = 0x%04x\n", name, addr);
return -1;
}
return 0;
}
int stab_nearest(address_t addr, char *ret_name, int max_len,
address_t *ret_offset)
{
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;
if (!btree_select(stab_addr, &akey, BTREE_LE, &akey, NULL)) {
strncpy(ret_name, akey.name, max_len);
ret_name[max_len - 1] = 0;
*ret_offset = addr - akey.addr;
return 0;
}
return -1;
}
int stab_get(const char *name, address_t *value)
{
struct sym_key skey;
address_t addr;
sym_key_init(&skey, name);
if (btree_get(stab_sym, &skey, &addr))
return -1;
*value = addr;
return 0;
}
int stab_del(const char *name)
{
struct sym_key skey;
address_t value;
struct addr_key akey;
sym_key_init(&skey, name);
if (btree_get(stab_sym, &skey, &value))
return -1;
addr_key_init(&akey, value, name);
btree_delete(stab_sym, &skey);
btree_delete(stab_addr, &akey);
return 0;
}
int stab_enum(stab_callback_t cb, void *user_data)
{
int ret;
struct addr_key akey;
ret = btree_select(stab_addr, NULL, BTREE_FIRST,
&akey, NULL);
while (!ret) {
if (cb(user_data, akey.name, akey.addr) < 0)
return -1;
ret = btree_select(stab_addr, NULL, BTREE_NEXT,
&akey, NULL);
}
return 0;
}
int stab_init(void)
{
stab_sym = btree_alloc(&sym_table_def);
if (!stab_sym) {
printc_err("stab: failed to allocate symbol table\n");
return -1;
}
stab_addr = btree_alloc(&addr_table_def);
if (!stab_addr) {
printc_err("stab: failed to allocate address table\n");
btree_free(stab_sym);
return -1;
}
return 0;
}
void stab_exit(void)
{
btree_free(stab_sym);
btree_free(stab_addr);
}
|