Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import graph (agi) + new ag? help #10132

Merged
merged 4 commits into from May 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions libr/core/canal.c
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions libr/core/cbin.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
118 changes: 55 additions & 63 deletions libr/core/cmd_anal.c
Expand Up @@ -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 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",
Expand Down Expand Up @@ -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<graphtype><format> [addr]", "",
"Graph types:", "", "",
"c", " [fcn addr]", "Function callgraph",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shuold put the whole command in here, otherwise it will break ?* to enumerate all recursive commands

"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:", "", "",
"<blank>", "", "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
};

Expand All @@ -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:", "", "",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -5792,17 +5786,13 @@ 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");
}
}

static void cmd_anal_graph(RCore *core, const char *input) {
RList *list;
const char *arg;
switch (input[0]) {
case 'f': // "agf"
switch (input[1]) {
Expand Down Expand Up @@ -5992,6 +5982,28 @@ static void cmd_anal_graph(RCore *core, const char *input) {
break;
}
break;
case 'i': // agi "import graph"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put quotes around "agi", we use this to find where the commands are implemented by greeping for this expresssion which have very few false positives

switch (input[1]) {
case 'v':
case 't':
case 'd':
case 'J':
case 'j':
case 'g':
case 'k':
case 'w':
case ' ':
case 0:
r_core_cmdf (core, "ag-; .agi*; agg%c;", input[1]);
break;
case '*':
r_core_anal_importxrefs (core);
break;
default:
eprintf ("Usage: see ag?\n");
break;
}
break;
case 'c': // "agc"
switch (input[1]) {
case 'v':
Expand All @@ -6013,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 + 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;
}
Expand Down Expand Up @@ -6114,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);
Expand All @@ -6139,19 +6142,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;
}
}
Expand Down
5 changes: 4 additions & 1 deletion libr/core/gdiff.c
Expand Up @@ -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) {
Expand All @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion libr/core/graph.c
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions libr/include/r_core.h
Expand Up @@ -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);
Expand Down Expand Up @@ -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?
Expand Down