Support for demangling of C++ symbol names.

This commit is contained in:
Ingo van Lil 2012-07-12 14:13:20 +02:00 committed by Daniel Beer
parent 69f3a15187
commit 3cc5f47304
9 changed files with 189 additions and 20 deletions

View File

@ -65,3 +65,6 @@ James Nuss <jamesnuss@nanometrics.ca>:
Tamas Tevesz <ice@extreme.hu>: Tamas Tevesz <ice@extreme.hu>:
* Improvements to interactive interface. * Improvements to interactive interface.
* Support for MSP430AFE253. * Support for MSP430AFE253.
Ingo van Lil <inguin@gmx.de>:
* Support for demangling of C++ function names.

View File

@ -113,6 +113,7 @@ OBJ=\
util/dis.o \ util/dis.o \
util/gdb_proto.o \ util/gdb_proto.o \
util/dynload.o \ util/dynload.o \
util/demangle.o \
drivers/device.o \ drivers/device.o \
drivers/bsl.o \ drivers/bsl.o \
drivers/fet.o \ drivers/fet.o \

View File

@ -32,11 +32,12 @@
#include "vector.h" #include "vector.h"
#include "sym.h" #include "sym.h"
#include "reader.h" #include "reader.h"
#include "demangle.h"
int cmd_eval(char **arg) int cmd_eval(char **arg)
{ {
address_t addr; address_t addr;
char name[64]; char name[MAX_SYMBOL_LENGTH];
if (expr_eval(*arg, &addr) < 0) { if (expr_eval(*arg, &addr) < 0) {
printc_err("=: can't parse: %s\n", *arg); printc_err("=: can't parse: %s\n", *arg);
@ -132,7 +133,12 @@ static int print_sym(void *user_data, const char *name, address_t value)
{ {
(void)user_data; (void)user_data;
char demangled[MAX_SYMBOL_LENGTH];
if (demangle(name, demangled, sizeof(demangled)) > 0)
printc("0x%04x: %s (%s)\n", value, name, demangled);
else
printc("0x%04x: %s\n", value, name); printc("0x%04x: %s\n", value, name);
return 0; return 0;
} }
@ -140,8 +146,16 @@ static int find_sym(void *user_data, const char *name, address_t value)
{ {
regex_t *find_preg = (regex_t *)user_data; regex_t *find_preg = (regex_t *)user_data;
if (!regexec(find_preg, name, 0, NULL, 0)) char demangled[MAX_SYMBOL_LENGTH];
int len = demangle(name, demangled, sizeof(demangled));
if (!regexec(find_preg, name, 0, NULL, 0) ||
(len > 0 && !regexec(find_preg, demangled, 0, NULL, 0))) {
if (len > 0)
printc("0x%04x: %s (%s)\n", value, name, demangled);
else
printc("0x%04x: %s\n", value, name); printc("0x%04x: %s\n", value, name);
}
return 0; return 0;
} }
@ -167,7 +181,7 @@ static int cmd_sym_find(char **arg)
} }
struct rename_record { struct rename_record {
char old_name[64]; char old_name[MAX_SYMBOL_LENGTH];
int start, end; int start, end;
}; };
@ -184,7 +198,7 @@ static int renames_do(struct rename_data *rename, const char *replace)
for (i = 0; i < rename->list.size; i++) { for (i = 0; i < rename->list.size; i++) {
struct rename_record *r = struct rename_record *r =
VECTOR_PTR(rename->list, i, struct rename_record); VECTOR_PTR(rename->list, i, struct rename_record);
char new_name[128]; char new_name[MAX_SYMBOL_LENGTH];
int len = r->start; int len = r->start;
address_t value; address_t value;

117
util/demangle.c Normal file
View File

@ -0,0 +1,117 @@
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009-2012 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 "opdb.h"
#include "stab.h"
/* Buffer in which the demangler result will be constructed. */
struct dmbuf {
char *out;
int max_len;
int len;
};
/* Add a chunk of text to the buffer */
static int dm_append(struct dmbuf *d, const char *text, int len)
{
int i;
if (d->len + len + 1 > d->max_len)
return -1;
for (i = 0; i < len; i++) {
if (!text[i])
return -1;
d->out[d->len++] = text[i];
}
d->out[d->len] = 0;
return 0;
}
/* Demangle a single component (possibly part of a nested name). */
static int dm_component(struct dmbuf *d, const char *text, const char **out)
{
char *next;
unsigned int len = strtoul(text, &next, 10);
if (next == text)
return -1;
if (dm_append(d, next, len) < 0)
return -1;
if (out)
*out = next + len;
return 0;
}
/* Demangler interface */
int demangle(const char *raw, char *out, int max_len)
{
struct dmbuf d;
d.out = out;
d.max_len = max_len;
d.len = 0;
if (*raw != '_' || raw[1] != 'Z')
return -1;
raw += 2;
if (*raw == 'N') {
const char *next;
/* Skip CV qualifiers */
raw++;
while (*raw == 'v' || *raw == 'V' || *raw == 'K')
raw++;
next = raw;
while (*next != 'C' && *next != 'D' && *next != 'E') {
const char *comp = next;
if (d.len > 0 && dm_append(&d, "::", 2) < 0)
return -1;
if (dm_component(&d, comp, &next) < 0)
return -1;
if (*next == 'C' || *next == 'D') {
/* Constructor/Destructor */
if (dm_append(&d, "::", 2) < 0)
return -1;
if (*next == 'D' && dm_append(&d, "~", 1) < 0)
return -1;
if (dm_component(&d, comp, NULL) < 0)
return -1;
}
}
} else {
if (dm_component(&d, raw, NULL) < 0)
return -1;
}
return d.len;
}

25
util/demangle.h Normal file
View File

@ -0,0 +1,25 @@
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009-2012 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
*/
#ifndef DEMANGLE_H_
#define DEMANGLE_H_
/* Demangle a function name into the buffer "out". */
int demangle(const char *raw, char *out, size_t max_len);
#endif

View File

@ -28,6 +28,7 @@
#include "util.h" #include "util.h"
#include "output.h" #include "output.h"
#include "opdb.h" #include "opdb.h"
#include "demangle.h"
/************************************************************************ /************************************************************************
* Address expression parsing. * Address expression parsing.
@ -233,7 +234,7 @@ static int addr_exp_finish(struct addr_exp_state *s, address_t *ret)
int expr_eval(const char *text, address_t *addr) int expr_eval(const char *text, address_t *addr)
{ {
const char *text_save = text; const char *text_save = text;
char token_buf[64]; char token_buf[MAX_SYMBOL_LENGTH];
int token_len = 0; int token_len = 0;
struct addr_exp_state s = {0}; struct addr_exp_state s = {0};

View File

@ -23,10 +23,11 @@
#include "output_util.h" #include "output_util.h"
#include "stab.h" #include "stab.h"
#include "util.h" #include "util.h"
#include "demangle.h"
static int format_addr(msp430_amode_t amode, address_t addr) static int format_addr(msp430_amode_t amode, address_t addr)
{ {
char name[64]; char name[MAX_SYMBOL_LENGTH];
const char *prefix = ""; const char *prefix = "";
switch (amode) { switch (amode) {
@ -166,14 +167,14 @@ void disassemble(address_t offset, const uint8_t *data, int length)
int count; int count;
int i; int i;
address_t oboff; address_t oboff;
char obname[64]; char obname[MAX_SYMBOL_LENGTH];
if (!stab_nearest(offset, obname, sizeof(obname), &oboff) && if (first_line ||
!oboff) { (!stab_nearest(offset, obname, sizeof(obname), &oboff) &&
printc("\x1b[m%s\x1b[0m:\n", obname); !oboff)) {
} else if (first_line) { char buffer[MAX_SYMBOL_LENGTH];
print_address(offset, obname, sizeof(obname)); print_address(offset, buffer, sizeof(buffer));
printc("\x1b[m%s\x1b[0m:\n", obname); printc("\x1b[m%s\x1b[0m:\n", buffer);
} }
first_line = 0; first_line = 0;
@ -254,15 +255,20 @@ void show_regs(const address_t *regs)
int print_address(address_t addr, char *out, int max_len) int print_address(address_t addr, char *out, int max_len)
{ {
char name[128]; char name[MAX_SYMBOL_LENGTH];
address_t offset; address_t offset;
if (!stab_nearest(addr, name, sizeof(name), &offset)) { if (!stab_nearest(addr, name, sizeof(name), &offset)) {
int len;
if (offset) if (offset)
snprintf(out, max_len, "%s+0x%x", name, offset); len = snprintf(out, max_len, "%s+0x%x", name, offset);
else else
snprintf(out, max_len, "%s", name); len = snprintf(out, max_len, "%s", name);
char demangled[MAX_SYMBOL_LENGTH];
if (demangle(name, demangled, sizeof(demangled)) > 0) {
snprintf(out + len, max_len - len, " (%s)", demangled);
}
return 1; return 1;
} }

View File

@ -33,7 +33,7 @@
*/ */
struct sym_key { struct sym_key {
char name[64]; char name[MAX_SYMBOL_LENGTH];
}; };
static const struct sym_key sym_key_zero = { static const struct sym_key sym_key_zero = {
@ -59,7 +59,7 @@ static void sym_key_init(struct sym_key *key, const char *text)
struct addr_key { struct addr_key {
address_t addr; address_t addr;
char name[64]; char name[MAX_SYMBOL_LENGTH];
}; };
static const struct addr_key addr_key_zero = { static const struct addr_key addr_key_zero = {

View File

@ -22,6 +22,8 @@
#include <stdint.h> #include <stdint.h>
#include "util.h" #include "util.h"
#define MAX_SYMBOL_LENGTH 512
/* Create/destroy a symbol table. The constructor returns NULL if it /* Create/destroy a symbol table. The constructor returns NULL if it
* was unable to allocate memory for the table. * was unable to allocate memory for the table.
*/ */