From 28699f4d5f9242b9f2bd37b659d14119f5e1c89b Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Sat, 19 May 2018 17:16:13 +0200 Subject: [PATCH 1/4] Added import graph; updated ag? help Removed agg? help Removed unused variable warning --- libr/core/canal.c | 21 ++++++-- libr/core/cbin.c | 4 +- libr/core/cmd_anal.c | 108 ++++++++++++++++++++++-------------------- libr/core/gdiff.c | 5 +- libr/core/graph.c | 2 +- libr/include/r_core.h | 2 + 6 files changed, 84 insertions(+), 58 deletions(-) diff --git a/libr/core/canal.c b/libr/core/canal.c index 127f275de7a67..f9934c4cf3b39 100644 --- a/libr/core/canal.c +++ b/libr/core/canal.c @@ -1721,7 +1721,6 @@ R_API void r_core_anal_datarefs(RCore *core, ut64 addr) { RBinSection *binsec = r_bin_get_section_at (obj, ref->addr, true); if (binsec->is_data) { if (!found) { - r_cons_printf ("ag-\n"); r_cons_printf ("agn %s\n", me); found = true; } @@ -1747,7 +1746,6 @@ R_API void r_core_anal_coderefs(RCore *core, ut64 addr) { RListIter *iter; RAnalRef *ref; RList *refs = r_anal_fcn_get_refs (core->anal, fcn); - r_cons_printf ("ag-\n"); r_cons_printf ("agn %s\n", me); r_list_foreach (refs, iter, ref) { RFlagItem *item = r_flag_get_i (core->flags, ref->addr); @@ -1761,11 +1759,28 @@ R_API void r_core_anal_coderefs(RCore *core, ut64 addr) { } } +R_API void r_core_anal_importxrefs(RCore *core) { + RBinInfo *info = r_bin_get_info (core->bin); + RBinObject *obj = r_bin_cur_object (core->bin); + bool lit = info ? info->has_lit: false; + int va = core->io->va || core->io->debug; + + RListIter *iter; + RBinImport *imp; + r_list_foreach (obj->imports, iter, imp) { + ut64 addr = lit ? r_core_bin_impaddr (core->bin, va, imp->name): 0; + if (addr) { + r_core_anal_codexrefs (core, addr); + } else { + r_cons_printf ("agn %s\n", imp->name); + } + } +} + R_API void r_core_anal_codexrefs(RCore *core, ut64 addr) { RFlagItem *f = r_flag_get_at (core->flags, addr, false); char *me = (f && f->offset == addr) ? r_str_new (f->name) : r_str_newf ("0x%"PFMT64x, addr); - r_cons_printf ("ag-\n"); r_cons_printf ("agn %s\n", me); RListIter *iter; RAnalRef *ref; diff --git a/libr/core/cbin.c b/libr/core/cbin.c index d14f3a0ba8936..b35cd138a0084 100644 --- a/libr/core/cbin.c +++ b/libr/core/cbin.c @@ -1545,7 +1545,7 @@ static RBinSymbol *get_symbol(RBin *bin, RList *symbols, const char *name, ut64 /* XXX: This is a hack to get PLT references in rabin2 -i */ /* imp. is a prefix that can be rewritten by the symbol table */ -static ut64 impaddr(RBin *bin, int va, const char *name) { +R_API ut64 r_core_bin_impaddr(RBin *bin, int va, const char *name) { RList *symbols; if (!name || !*name) { @@ -1585,7 +1585,7 @@ static int bin_imports(RCore *r, int mode, int va, const char *name) { continue; } char *symname = strdup (import->name); - ut64 addr = lit ? impaddr (r->bin, va, symname): 0; + ut64 addr = lit ? r_core_bin_impaddr (r->bin, va, symname): 0; if (bin_demangle) { char *dname = r_bin_demangle (r->bin->cur, NULL, symname, addr); if (dname) { diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index 3a8a5c1b09c33..6210973101a4d 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -15,7 +15,7 @@ static const char *help_msg_a[] = { "ae", "[?] [expr]", "analyze opcode eval expression (see ao)", "af", "[?]", "analyze Functions", "aF", "", "same as above, but using anal.depth=1", - "ag", "[?] [options]", "output Graphviz code", + "ag", "[?] [options]", "draw graphs", "ah", "[?]", "analysis hints (force opcode size, ...)", "ai", " [addr]", "address information (show perms, stack, heap, ...)", "an"," [name] [@addr]","show/rename/create whatever flag/function is used at addr", @@ -379,26 +379,32 @@ static const char *help_msg_afvs[] = { }; static const char *help_msg_ag[] = { - "Usage:", "ag[?f]", " Graphviz/graph code", - "ag", " [addr]", "output graphviz code (bb at addr and children)", - "ag-", "", "Reset the current ASCII art graph (see agn, age, agg?)", - "aga", " [addr]", "idem, but only addresses", - "agr", "[j] [addr]", "output graphviz call graph of function", - "agg", "", "display current graph created with agn and age (see also ag-)", - "agc", "[*j] [addr]", "output graphviz call graph of function", - "agC", "[j]", "Same as agc -1. full program callgraph", - "agd", " [fcn name]", "output graphviz code of diffed function", - "age", "[?] title1 title2", "Add an edge to the current graph", - "agf", " [addr]", "Show ASCII art graph of given function", - "agg", "[?] [kdi*]", "Print graph in ASCII-Art, graphviz, k=v, r2 or visual", - "agj", " [addr]", "idem, but in JSON format", - "agJ", " [addr]", "idem, but in JSON format with formatted disassembly (like pdJ)", - "agk", " [addr]", "idem, but in SDB key-value format", - "agl", " [fcn name]", "output graphviz code using meta-data", - "agn", "[?] title body", "Add a node to the current graph", - "ags", " [addr]", "output simple graphviz call graph of function (only bb offset)", - "agt", " [addr]", "find paths from current offset to given address", - "agv", "", "Show function graph in web/png (see graph.web and cmd.graph) or agf for asciiart", + "Usage:", "ag [addr]", "", + "Graph types:", "", "", + "c", " [fcn addr]", "Function callgraph", + "f", " [fcn addr]", "Basic blocks function graph", + "x", " [addr]", "Cross references graph", + "r", " [fcn addr]", "References graph", + "a", " [fcn addr]", "Data references graph", + "d", " [fcn addr]", "Diff graph", + "i", "", "Imports graph", + "g", "", "Custom graph", + "","","", + "Output formats:", "", "", + "", "", "Ascii art", + "v", "", "Interactive ascii art", + "t", "", "Tiny ascii art", + "d", "", "Graphviz dot", + "j", "", "json ('J' for formatted disassembly)", + "g", "", "Graph Modelling Language (gml)", + "k", "", "SDB key-value", + "*", "", "r2 commands", + "w", "", "Web/image (see graph.extension and graph.web)", + "","","", + "Custom graph commands:", "", "", + "ag-", "", "Clear the custom graph", + "agn", "[?] title body", "Add a node to the custom graph", + "age", "[?] title1 title2", "Add an edge to the custom graph", NULL }; @@ -412,17 +418,6 @@ static const char *help_msg_age[] = { NULL }; -static const char *help_msg_agg[] = { - "Usage:", "agg[kid?*]", "print graph", - "agg", "", "show current graph in ascii art", - "aggk", "", "show graph in key=value form", - "aggi", "", "enter interactive mode for the current graph", - "aggd", "", "print the current graph in GRAPHVIZ dot format", - "aggv", "", "run graphviz + viewer (see 'e cmd.graph')", - "agg*", "", "in r2 commands, to save in projects, etc", - NULL -}; - static const char *help_msg_agn[] = { "Usage:", "agn [title] [body]", "", "Examples:", "", "", @@ -595,7 +590,6 @@ static void cmd_anal_init(RCore *core) { DEFINE_CMD_DESCRIPTOR (core, afvs); DEFINE_CMD_DESCRIPTOR (core, ag); DEFINE_CMD_DESCRIPTOR (core, age); - DEFINE_CMD_DESCRIPTOR (core, agg); DEFINE_CMD_DESCRIPTOR (core, agn); DEFINE_CMD_DESCRIPTOR (core, ah); DEFINE_CMD_DESCRIPTOR (core, ahi); @@ -5792,9 +5786,6 @@ static void cmd_agraph_print(RCore *core, const char *input) { } break; } - case '?': - r_core_cmd_help (core, help_msg_agg); - break; default: eprintf ("Usage: see ag?\n"); } @@ -5802,7 +5793,6 @@ static void cmd_agraph_print(RCore *core, const char *input) { static void cmd_anal_graph(RCore *core, const char *input) { RList *list; - const char *arg; switch (input[0]) { case 'f': // "agf" switch (input[1]) { @@ -5992,6 +5982,33 @@ static void cmd_anal_graph(RCore *core, const char *input) { break; } break; + case 'i': // agi "import graph" + switch (input[1]) { + case 'v': + case 't': + case 'd': + case 'J': + case 'j': + case 'g': + case 'k': + case 'w': + case ' ': + case 0: { + char *cmd = r_str_newf ("ag-; .agi*; agg%c;", input[1]); + if (cmd && *cmd) { + r_core_cmd0 (core, cmd); + } + free (cmd); + break; + } + case '*': + r_core_anal_importxrefs (core); + break; + default: + eprintf ("Usage: see ag?\n"); + break; + } + break; case 'c': // "agc" switch (input[1]) { case 'v': @@ -6013,12 +6030,12 @@ static void cmd_anal_graph(RCore *core, const char *input) { r_core_cmd0 (core, "ag-; .agc* $$; agg;"); break; case 'g': { - ut64 addr = input[2]? r_num_math (core->num, input + 1): core->offset; + ut64 addr = input[2]? r_num_math (core->num, input + 2): core->offset; r_core_anal_callgraph (core, addr, R_GRAPH_FORMAT_GMLFCN); break; } case 'd': { - ut64 addr = input[2]? r_num_math (core->num, input + 1): core->offset; + ut64 addr = input[2]? r_num_math (core->num, input + 2): core->offset; r_core_anal_callgraph (core, addr, R_GRAPH_FORMAT_DOT); break; } @@ -6139,19 +6156,8 @@ static void cmd_anal_graph(RCore *core, const char *input) { free (cmdargs); } break; - case '?': // "ag?" - r_core_cmd_help (core, help_msg_ag); - break; - case ' ': // "ag" - arg = strchr (input, ' '); - r_core_anal_graph (core, r_num_math (core->num, arg? arg + 1: NULL), - R_CORE_ANAL_GRAPHBODY); - break; - case 0: - eprintf ("|ERROR| Usage: ag [addr]\n"); - break; default: - eprintf ("See ag?\n"); + r_core_cmd_help (core, help_msg_ag); break; } } diff --git a/libr/core/gdiff.c b/libr/core/gdiff.c index ccd1b23197c2a..e30ff2ceb6753 100644 --- a/libr/core/gdiff.c +++ b/libr/core/gdiff.c @@ -11,6 +11,9 @@ R_API int r_core_gdiff_fcn(RCore *c, ut64 addr, ut64 addr2) { RList *la, *lb; RAnalFunction *fa = r_anal_get_fcn_at (c->anal, addr, 0); RAnalFunction *fb = r_anal_get_fcn_at (c->anal, addr2, 0); + if (!fa || !fb) { + return false; + } RAnalBlock *bb; RListIter *iter; r_list_foreach (fa->bbs, iter, bb) { @@ -26,7 +29,7 @@ R_API int r_core_gdiff_fcn(RCore *c, ut64 addr, ut64 addr2) { r_anal_diff_fcn (c->anal, la, lb); r_list_free (la); r_list_free (lb); - return false; + return true; } /* Fingerprint functions and blocks, then diff. */ diff --git a/libr/core/graph.c b/libr/core/graph.c index 7dd92d16678fe..517d6eb870ae6 100644 --- a/libr/core/graph.c +++ b/libr/core/graph.c @@ -3643,7 +3643,7 @@ R_API int r_core_visual_graph(RCore *core, RAGraph *g, RAnalFunction *_fcn, int int o_vmode = core->vmode; int exit_graph = false, is_error = false; struct agraph_refresh_data *grd; - int okey, key, wheel; + int okey, key; RAnalFunction *fcn = NULL; const char *key_s; RConsCanvas *can, *o_can = NULL; diff --git a/libr/include/r_core.h b/libr/include/r_core.h index 79ca63c1670e1..05c13db85ea76 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -415,6 +415,7 @@ R_API int r_core_anal_data (RCore *core, ut64 addr, int count, int depth, int wo R_API void r_core_anal_datarefs(RCore *core, ut64 addr); R_API void r_core_anal_coderefs(RCore *core, ut64 addr); R_API void r_core_anal_codexrefs(RCore *core, ut64 addr); +R_API void r_core_anal_importxrefs(RCore *core); R_API void r_core_anal_callgraph(RCore *core, ut64 addr, int fmt); R_API int r_core_anal_refs(RCore *core, const char *input); R_API int r_core_esil_step(RCore *core, ut64 until_addr, const char *until_expr, ut64 *prev_addr); @@ -481,6 +482,7 @@ R_API int r_core_hash_load(RCore *core, const char *file); R_API int r_core_bin_list(RCore *core, int mode); R_API int r_core_bin_raise (RCore *core, ut32 binfile_idx, ut32 obj_idx); R_API bool r_core_bin_delete (RCore *core, ut32 binfile_idx, ut32 binobj_idx); +R_API ut64 r_core_bin_impaddr(RBin *bin, int va, const char *name); // XXX - this is kinda hacky, maybe there should be a way to // refresh the bin environment without specific calls? From 52f5c89da41e52d0bebceffcf762d5bd186e2651 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Sat, 19 May 2018 17:42:25 +0200 Subject: [PATCH 2/4] Change agv command to alias of agfv Fix of string arguments in ag* commands Fix whitespace --- libr/core/cmd_anal.c | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index 6210973101a4d..f1646af8349c7 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -15,7 +15,7 @@ static const char *help_msg_a[] = { "ae", "[?] [expr]", "analyze opcode eval expression (see ao)", "af", "[?]", "analyze Functions", "aF", "", "same as above, but using anal.depth=1", - "ag", "[?] [options]", "draw graphs", + "ag", "[?] [options]", "draw graphs in various formats", "ah", "[?]", "analysis hints (force opcode size, ...)", "ai", " [addr]", "address information (show perms, stack, heap, ...)", "an"," [name] [@addr]","show/rename/create whatever flag/function is used at addr", @@ -380,7 +380,7 @@ static const char *help_msg_afvs[] = { static const char *help_msg_ag[] = { "Usage:", "ag [addr]", "", - "Graph types:", "", "", + "Graph types:", "", "", "c", " [fcn addr]", "Function callgraph", "f", " [fcn addr]", "Basic blocks function graph", "x", " [addr]", "Cross references graph", @@ -5993,14 +5993,9 @@ static void cmd_anal_graph(RCore *core, const char *input) { case 'k': case 'w': case ' ': - case 0: { - char *cmd = r_str_newf ("ag-; .agi*; agg%c;", input[1]); - if (cmd && *cmd) { - r_core_cmd0 (core, cmd); - } - free (cmd); + case 0: + r_core_cmdf (core, "ag-; .agi*; agg%c;", input[1]); break; - } case '*': r_core_anal_importxrefs (core); break; @@ -6030,12 +6025,12 @@ static void cmd_anal_graph(RCore *core, const char *input) { r_core_cmd0 (core, "ag-; .agc* $$; agg;"); break; case 'g': { - ut64 addr = input[2]? r_num_math (core->num, input + 2): core->offset; + ut64 addr = input[2] ? r_num_math (core->num, input + 2): core->offset; r_core_anal_callgraph (core, addr, R_GRAPH_FORMAT_GMLFCN); break; } case 'd': { - ut64 addr = input[2]? r_num_math (core->num, input + 2): core->offset; + ut64 addr = input[2] ? r_num_math (core->num, input + 2): core->offset; r_core_anal_callgraph (core, addr, R_GRAPH_FORMAT_DOT); break; } @@ -6131,23 +6126,14 @@ static void cmd_anal_graph(RCore *core, const char *input) { } } break; - case 'v': // "agv" - if (r_config_get_i (core->config, "graph.web")) { - r_core_cmd0 (core, "=H /graph/"); - } else { - const char *cmd = r_config_get (core->config, "cmd.graph"); - if (cmd && *cmd) { - r_core_cmd0 (core, cmd); - } else { - r_core_cmd0 (core, "agf"); - } - } + case 'v': // "agv" alias for "agfv" + r_core_cmdf (core, "agfv%s", input + 1); break; case 'w':// "agw" if (r_config_get_i (core->config, "graph.web")) { r_core_cmd0 (core, "=H /graph/"); } else { - char *cmdargs = r_str_newf ("agfd %lld", r_num_math (core->num, input + 2)); + char *cmdargs = r_str_newf ("agfd %lld", r_num_math (core->num, input + 1)); char *cmd = r_core_graph_cmd (core, cmdargs); if (cmd && *cmd) { r_core_cmd0 (core, cmd); From 3618796dee7fbf87282bc2a1e3622eb804745045 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Sun, 20 May 2018 17:00:53 +0200 Subject: [PATCH 3/4] Fix agi -> 'agi' comment --- libr/core/cmd_anal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index f1646af8349c7..0f984c2fa58b3 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -5982,7 +5982,7 @@ static void cmd_anal_graph(RCore *core, const char *input) { break; } break; - case 'i': // agi "import graph" + case 'i': // "agi" import graph switch (input[1]) { case 'v': case 't': From c6fd25a8ee4db677cb97363a2a1e2ca2dcf350f1 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Sun, 20 May 2018 17:39:57 +0200 Subject: [PATCH 4/4] Changed ag? help --- libr/core/cmd_anal.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index 0f984c2fa58b3..1dbde30840070 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -380,15 +380,18 @@ static const char *help_msg_afvs[] = { static const char *help_msg_ag[] = { "Usage:", "ag [addr]", "", - "Graph types:", "", "", - "c", " [fcn addr]", "Function callgraph", - "f", " [fcn addr]", "Basic blocks function graph", - "x", " [addr]", "Cross references graph", - "r", " [fcn addr]", "References graph", - "a", " [fcn addr]", "Data references graph", - "d", " [fcn addr]", "Diff graph", - "i", "", "Imports graph", - "g", "", "Custom graph", + "Graph commands:", "", "", + "agc", "[format] [fcn addr]", "Function callgraph", + "agf", "[format] [fcn addr]", "Basic blocks function graph", + "agx", "[format] [addr]", "Cross references graph", + "agr", "[format] [fcn addr]", "References graph", + "aga", "[format] [fcn addr]", "Data references graph", + "agd", "[format] [fcn addr]", "Diff graph", + "agi", "[format]", "Imports graph", + "agg", "[format]", "Custom graph", + "ag-", "", "Clear the custom graph", + "agn", "[?] title body", "Add a node to the custom graph", + "age", "[?] title1 title2", "Add an edge to the custom graph", "","","", "Output formats:", "", "", "", "", "Ascii art", @@ -400,11 +403,6 @@ static const char *help_msg_ag[] = { "k", "", "SDB key-value", "*", "", "r2 commands", "w", "", "Web/image (see graph.extension and graph.web)", - "","","", - "Custom graph commands:", "", "", - "ag-", "", "Clear the custom graph", - "agn", "[?] title body", "Add a node to the custom graph", - "age", "[?] title1 title2", "Add an edge to the custom graph", NULL };