Better dtc, demangling support. Handle RABIN2_LANG, and more c++ autodetect

* r_name_filter(len:-1) strlens the string
* Show symbol names in `dtc` (calltracer)
This commit is contained in:
pancake 2015-10-19 13:21:12 +02:00
parent e569417ddf
commit 45c7c923af
10 changed files with 78 additions and 44 deletions

View File

@ -100,6 +100,7 @@ static int rabin_show_help(int v) {
);
if (v) {
printf ("Environment:\n"
" RABIN2_LANG: e bin.lang # assume lang for demangling\n"
" RABIN2_DEMANGLE: e bin.demangle # show symbols demangled\n"
" RABIN2_MAXSTRBUF: e bin.maxstrbuf # specify maximum buffer size\n"
" RABIN2_STRFILTER: e bin.strfilter # r2 -qe bin.strfilter=? -c '' --\n"
@ -405,6 +406,10 @@ int main(int argc, char **argv) {
r_lib_opendir (l, homeplugindir);
r_lib_opendir (l, R2_LIBDIR"/radare2/"R2_VERSION);
if ((tmp = r_sys_getenv ("RABIN2_LANG"))) {
r_config_set (core.config, "bin.lang", tmp);
free (tmp);
}
if ((tmp = r_sys_getenv ("RABIN2_DEMANGLE"))) {
r_config_set (core.config, "bin.demangle", tmp);
free (tmp);

View File

@ -104,17 +104,15 @@ R_API char *r_bin_demangle_java(const char *str) {
return ret;
}
R_API char *r_bin_demangle_msvc(const char *str)
{
R_API char *r_bin_demangle_msvc(const char *str) {
char *out = NULL;
SDemangler *mangler = 0;
create_demangler(&mangler);
if (init_demangler(mangler, (char *)str) == eDemanglerErrOK) {
create_demangler (&mangler);
if (init_demangler (mangler, (char *)str) == eDemanglerErrOK) {
mangler->demangle(mangler, &out/*demangled_name*/);
}
free_demangler(mangler);
free_demangler (mangler);
return out;
}
@ -317,30 +315,44 @@ R_API int r_bin_lang_rust(RBinFile *binfile) {
return haslang;
}
R_API int r_bin_lang_type(RBinFile *binfile, const char *def) {
R_API int r_bin_lang_type(RBinFile *binfile, const char *def, const char *sym) {
int type = 0;
RBinPlugin *plugin;
if (sym && sym[0] == sym[1] && sym[0] == '_') {
type = R_BIN_NM_CXX;
}
if (def && *def) {
type = r_bin_demangle_type (def);
if (type != R_BIN_NM_NONE)
return type;
}
plugin = r_bin_file_cur_plugin (binfile);
if (plugin && plugin->demangle_type)
if (plugin && plugin->demangle_type) {
type = plugin->demangle_type (def);
else {
} else {
if (binfile->o && binfile->o->info) {
type = r_bin_demangle_type (binfile->o->info->lang);
}
}
if (type == R_BIN_NM_NONE)
if (type == R_BIN_NM_NONE) {
type = r_bin_demangle_type (def);
}
return type;
}
R_API char *r_bin_demangle (RBinFile *binfile, const char *def, const char *str) {
int type = -1;
RBin *bin = binfile->rbin;
int type = r_bin_lang_type (binfile, def);
if (!strncmp (str, "imp.", 4)) {
str += 4;
}
if (!strncmp (str, "__", 2)) {
type = R_BIN_NM_CXX;
str++;
}
if (type == -1) {
type = r_bin_lang_type (binfile, def, str);
}
switch (type) {
case R_BIN_NM_JAVA: return r_bin_demangle_java (str);
/* rust uses the same mangling as c++ and appends a uniqueid */

View File

@ -1124,22 +1124,20 @@ static int bin_symbols_internal(RCore *r, int mode, ut64 laddr, int va, ut64 at,
char *name = strdup (symbol->name);
if (bin_demangle) {
const char *symname = name;
char *dname;
if (!strncmp (symname, "imp.", 4))
symname += 4;
dname = r_bin_demangle (r->bin->cur, lang, symname);
char *dname = r_bin_demangle (r->bin->cur, lang, symname);
if (dname) {
free (name);
name = dname;
}
}
r_name_filter (name, 80);
//r_name_filter (name, 80);
r_cons_printf ("0x%08"PFMT64x" %d %s\n",
addr, (int)symbol->size, name);
free (name);
} else if (IS_MODE_RAD (mode)) {
RBinFile *binfile;
RBinPlugin *plugin;
char *name;
if (!strcmp (symbol->type, "NOTYPE")) {
continue;
@ -1149,11 +1147,15 @@ static int bin_symbols_internal(RCore *r, int mode, ut64 laddr, int va, ut64 at,
if (mn) {
r_cons_printf ("s 0x%08"PFMT64x"\n\"CC %s\"\n",
symbol->paddr, mn);
free (mn);
name = mn;
} else {
name = strdup (symbol->name);
}
} else {
name = strdup (symbol->name);
}
r_name_filter (symbol->name, sizeof (symbol->name));
if (!strncmp (symbol->name, "imp.", 4)) {
r_name_filter (name, -1);
if (!strncmp (name, "imp.", 4)) {
if (lastfs != 'i')
r_cons_printf ("fs imports\n");
lastfs = 'i';
@ -1168,7 +1170,7 @@ static int bin_symbols_internal(RCore *r, int mode, ut64 laddr, int va, ut64 at,
lastfs = 's';
}
r_cons_printf ("f sym.%s %u 0x%08"PFMT64x"\n",
symbol->name, symbol->size, addr);
name, symbol->size, addr);
binfile = r_core_bin_cur (r);
plugin = r_bin_file_cur_plugin (binfile);
if (plugin && plugin->name) {

View File

@ -2140,10 +2140,12 @@ R_API int r_core_cmd_file(RCore *core, const char *file) {
R_API int r_core_cmd_command(RCore *core, const char *command) {
int ret, len;
char *buf, *rcmd, *ptr;
rcmd = ptr = buf = r_sys_cmd_str (command, 0, &len);
char *cmd = r_core_sysenv_begin (core, command);
rcmd = ptr = buf = r_sys_cmd_str (cmd, 0, &len);
if (buf == NULL)
return -1;
ret = r_core_cmd (core, rcmd, 0);
r_core_sysenv_end (core, command);
free (buf);
return ret;
}

View File

@ -99,9 +99,7 @@ static RGraphNode *get_graphtrace_node (RGraph *g, Sdb *nodes, struct trace_node
static void dot_trace_create_node (RTreeNode *n, RTreeVisitor *vis) {
struct dot_trace_ght *data = (struct dot_trace_ght *)vis->data;
struct trace_node *tn = n->data;
if (tn)
get_graphtrace_node (data->graph, data->graphnodes, tn);
if (tn) get_graphtrace_node (data->graph, data->graphnodes, tn);
}
static void dot_trace_discover_child (RTreeNode *n, RTreeVisitor *vis) {
@ -1932,18 +1930,21 @@ static RTreeNode *add_trace_tree_child (Sdb *db, RTree *t, RTreeNode *cur, ut64
return node;
}
static RCore *_core = NULL;
static void trace_traverse_pre (RTreeNode *n, RTreeVisitor *vis) {
const char *name = "";
struct trace_node *tn = n->data;
unsigned int i;
if (!tn)
return;
if (!tn) return;
for (i = 0; i < n->depth - 1; ++i)
r_cons_printf (" ");
r_cons_printf (" 0x%08"PFMT64x" refs %d\n",
tn->addr, tn->refs);
r_cons_printf (" ");
if (_core) {
RFlagItem *f = r_flag_get_at (_core->flags, tn->addr);
if (f) name = f->name;
}
r_cons_printf (" 0x%08"PFMT64x" refs %d %s\n",
tn->addr, tn->refs, name);
}
static void trace_traverse (RTree *t) {
@ -2097,7 +2098,7 @@ static void debug_trace_calls (RCore *core, const char *input) {
do_debug_trace_calls (core, from, to, final_addr);
if (bp_final)
r_bp_del (core->dbg->bp, final_addr);
_core = core;
trace_traverse (core->dbg->tree);
core->dbg->trace->enabled = t;
r_cons_break_end();

View File

@ -174,6 +174,8 @@ R_API void r_core_sysenv_help(const RCore* core) {
"=!=", "", "disable remotecmd mode",
"\nEnvironment:", "", "",
"FILE", "", "file name",
"RABIN2_LANG", "", "assume this lang to demangle",
"RABIN2_DEMANGLE", "", "demangle or not",
"SIZE", "","file size",
"OFFSET", "", "10base offset 64bit value",
"XOFFSET", "", "same as above, but in 16 base",
@ -225,7 +227,8 @@ R_API char *r_core_sysenv_begin(RCore *core, const char *cmd) {
r_sys_setenv ("PDB_SERVER", r_config_get (core->config, "pdb.server"));
if (core->file && core->file->desc && core->file->desc->name) {
r_sys_setenv ("FILE", core->file->desc->name);
snprintf (buf, sizeof (buf), "%"PFMT64d, r_io_desc_size (core->io, core->file->desc));
snprintf (buf, sizeof (buf), "%"PFMT64d, r_io_desc_size
(core->io, core->file->desc));
r_sys_setenv ("SIZE", buf);
if (strstr (cmd, "BLOCK")) {
// replace BLOCK in RET string
@ -236,6 +239,8 @@ R_API char *r_core_sysenv_begin(RCore *core, const char *cmd) {
}
}
}
r_sys_setenv ("RABIN2_LANG", r_config_get (core->config, "bin.lang"));
r_sys_setenv ("RABIN2_DEMANGLE", r_config_get (core->config, "bin.demangle"));
snprintf (buf, sizeof (buf), "%"PFMT64d, core->offset);
r_sys_setenv ("OFFSET", buf);
snprintf (buf, sizeof (buf), "0x%08"PFMT64x, core->offset);

View File

@ -424,6 +424,7 @@ R_API char *r_bin_demangle_cxx(const char *str);
R_API char *r_bin_demangle_msvc(const char *str);
R_API char *r_bin_demangle_swift(const char *s);
R_API char *r_bin_demangle_objc(RBinFile *binfile, const char *sym);
R_API int r_bin_lang_type(RBinFile *binfile, const char *def, const char *sym);
R_API int r_bin_lang_objc(RBinFile *binfile);
R_API int r_bin_lang_swift(RBinFile *binfile);
R_API int r_bin_lang_cxx(RBinFile *binfile);

View File

@ -32,6 +32,9 @@ R_API int r_name_check(const char *name) {
R_API int r_name_filter(char *name, int maxlen) {
int i;
char *oname;
if (maxlen<0) {
maxlen = strlen (name);
}
name = oname = r_str_trim_head_tail (name);
for (i=0; *name; name++, i++) {
if (maxlen && i>maxlen) {

View File

@ -572,18 +572,18 @@ R_API void r_sys_perror(const char *fun) {
}
R_API int r_sys_arch_id(const char *arch) {
int i;
for (i=0; arch_bit_array[i].name; i++)
if (!strcmp (arch, arch_bit_array[i].name))
return arch_bit_array[i].bit;
return 0;
int i;
for (i=0; arch_bit_array[i].name; i++)
if (!strcmp (arch, arch_bit_array[i].name))
return arch_bit_array[i].bit;
return 0;
}
R_API const char *r_sys_arch_str(int arch) {
int i;
for (i=0; arch_bit_array[i].name; i++)
if (arch & arch_bit_array[i].bit)
return arch_bit_array[i].name;
int i;
for (i=0; arch_bit_array[i].name; i++)
if (arch & arch_bit_array[i].bit)
return arch_bit_array[i].name;
return "none";
}
@ -618,7 +618,8 @@ R_API int r_sys_run(const ut8 *buf, int len) {
#endif
if (pid<0) {
return cb ();
} else if (!pid) {
}
if (!pid) {
ret = cb ();
exit (ret);
return ret;

View File

@ -102,6 +102,8 @@ Shows strings from raw bins
.El
.Sh ENVIRONMENT
.Pp
RABIN2_LANG same as r2 -e bin.lang for rabin2
.Pp
RABIN2_DEMANGLE demangle symbols
.Pp
RABIN2_MAXSTRBUF same as r2 -e bin.maxstrbuf for rabin2