diff --git a/binr/rabin2/rabin2.c b/binr/rabin2/rabin2.c index 2a20059728..a0d0a88e7f 100644 --- a/binr/rabin2/rabin2.c +++ b/binr/rabin2/rabin2.c @@ -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); diff --git a/libr/bin/demangle.c b/libr/bin/demangle.c index 336e88382f..12165a4429 100644 --- a/libr/bin/demangle.c +++ b/libr/bin/demangle.c @@ -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 */ diff --git a/libr/core/bin.c b/libr/core/bin.c index 8716c95ef8..dddde0072b 100644 --- a/libr/core/bin.c +++ b/libr/core/bin.c @@ -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) { diff --git a/libr/core/cmd.c b/libr/core/cmd.c index 1fc092a29e..fd44c9d5e7 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -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; } diff --git a/libr/core/cmd_debug.c b/libr/core/cmd_debug.c index 6536ae65f4..5db3d4c55b 100644 --- a/libr/core/cmd_debug.c +++ b/libr/core/cmd_debug.c @@ -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(); diff --git a/libr/core/file.c b/libr/core/file.c index df0eeec3be..c93315fa93 100644 --- a/libr/core/file.c +++ b/libr/core/file.c @@ -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); diff --git a/libr/include/r_bin.h b/libr/include/r_bin.h index 6e1d69509d..ec461da923 100644 --- a/libr/include/r_bin.h +++ b/libr/include/r_bin.h @@ -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); diff --git a/libr/util/name.c b/libr/util/name.c index a8032d92ef..fc7bed823b 100644 --- a/libr/util/name.c +++ b/libr/util/name.c @@ -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) { diff --git a/libr/util/sys.c b/libr/util/sys.c index 945e375e32..35b912ccdd 100644 --- a/libr/util/sys.c +++ b/libr/util/sys.c @@ -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; diff --git a/man/rabin2.1 b/man/rabin2.1 index aa69694f3b..e4803c24a1 100644 --- a/man/rabin2.1 +++ b/man/rabin2.1 @@ -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