Skip to content

Commit

Permalink
Implement agD for testing dom graph ##analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Apr 2, 2024
1 parent f74d5c5 commit 1f47c28
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 35 deletions.
56 changes: 32 additions & 24 deletions libr/core/agraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1841,8 +1841,10 @@ static int get_edge_number(const RAGraph *g, RANode *src, RANode *dst, bool outg
RANode *v;

if (outgoing && src->is_dummy) {
RANode *in = (RANode *) (((RGraphNode *)r_list_first ((src->gnode)->in_nodes))->data);
cur_nth = get_edge_number (g, in, src, outgoing);
if (src->gnode) {
RANode *in = (RANode *) (((RGraphNode *)r_list_first ((src->gnode)->in_nodes))->data);
cur_nth = get_edge_number (g, in, src, outgoing);
}
} else {
const RList *neighbours = outgoing
? r_graph_get_neighbours (g->graph, src->gnode)
Expand Down Expand Up @@ -3041,7 +3043,7 @@ static void agraph_print_edges(RAGraph *g) {
tm->layer = a->layer;
tm->edgectr = 0;
tm->revedgectr = 0;
if (g->layout == 0) { //vertical layout
if (g->layout == 0) { // vertical layout
tm->minx = a->x;
tm->maxx = a->x + a->w;
} else {
Expand Down Expand Up @@ -3260,7 +3262,8 @@ static void agraph_print_edges(RAGraph *g) {

if (tt) {
int arg = (rightlen < leftlen)? maxx + 1: minx - 1;
r_cons_canvas_line_back_edge (g->can, temp->ax, temp->ay, temp->bx, temp->by, &(temp->style), temp->edgectr, arg, tt->revedgectr, !g->layout);
r_cons_canvas_line_back_edge (g->can, temp->ax, temp->ay, temp->bx, temp->by,
&(temp->style), temp->edgectr, arg, tt->revedgectr, !g->layout);
}

r_list_foreach (lyr, ito, tl) {
Expand Down Expand Up @@ -3468,6 +3471,10 @@ static void agraph_prev_node(RAGraph *g) {

static void agraph_update_title(RCore *core, RAGraph *g, RAnalFunction *fcn) {
RANode *a = get_anode (g->curnode);
if (!a) {
return;
}
eprintf ("GET NODE %p\n", a);
char *sig = r_core_cmd_str (core, "afcf");
char *new_title = r_str_newf (
"%s[0x%08"PFMT64x "]> %s # %s ",
Expand Down Expand Up @@ -4422,16 +4429,17 @@ static void nextword(RCore *core, RAGraph *g, const char *word) {
free (gh->old_word);
gh->old_word = strdup (word);
free (s);
if (!a && count == 0) {
return;
if (a || count > 0) {
nextword (core, g, word);
}
nextword (core, g, word);
}

R_API int r_core_visual_graph(RCore *core, RAGraph *g, RAnalFunction *_fcn, int is_interactive) {
// R2_600 return bool
R_API int r_core_visual_graph(RCore *core, RAGraph *g, RAnalFunction *_fcn, int mode) {
bool is_interactive = (mode != 0);
if (is_interactive && !r_cons_is_interactive ()) {
eprintf ("Interactive graph mode requires scr.interactive=true.\n");
return 0;
R_LOG_ERROR ("Interactive graph mode requires 'e scr.interactive=true'");
return false;
}
r_cons_set_raw (true);
int o_asmqjmps_letter = core->is_asmqjmps_letter;
Expand Down Expand Up @@ -4486,7 +4494,7 @@ R_API int r_core_visual_graph(RCore *core, RAGraph *g, RAnalFunction *_fcn, int
r_config_hold_free (hc);
return false;
}
g->is_tiny = is_interactive == 2;
g->is_tiny = mode == 2;
g->layout = r_config_get_i (core->config, "graph.layout");
g->dummy = r_config_get_i (core->config, "graph.dummy");
g->show_node_titles = r_config_get_i (core->config, "graph.ntitles");
Expand Down Expand Up @@ -4545,21 +4553,21 @@ R_API int r_core_visual_graph(RCore *core, RAGraph *g, RAnalFunction *_fcn, int
core->cons->event_resize = (RConsEvent) agraph_refresh_oneshot;

r_cons_break_push (NULL, NULL);
#if 0
// XXX wrong usage or buggy RGraph.domTree()
// dominance tree here
const RList *l = r_graph_get_nodes (g->graph);
RGraphNode *root = r_list_first (l);
if (root) {
RGraph *dg = r_graph_dom_tree (g->graph, root);
if (dg) {
g->graph = dg;
} else {
R_LOG_WARN ("Cannot compute the dominance tree");
sleep (1);
if (mode == 3) { // XXX wrong usage or buggy RGraph.domTree()
// dominance tree here
const RList *l = r_graph_get_nodes (g->graph);
RGraphNode *root = r_list_first (l);
if (root) {
RGraph *dg = r_graph_dom_tree (g->graph, root);
if (dg) {
// XXX double free - r_graph_free (g->graph);
g->graph = dg;
} else {
R_LOG_WARN ("Cannot compute the dominance tree");
sleep (1);
}
}
}
#endif

while (!exit_graph && !is_error && !r_cons_is_breaked ()) {
w = r_cons_get_size (&h);
Expand Down
6 changes: 5 additions & 1 deletion libr/core/cmd_anal.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ static RCoreHelpMessage help_msg_ag = {
"agc", "[format]", "function callgraph",
"agC", "[format]", "global callgraph",
"agd", "[format] [fcn addr]", "diff graph",
"agD", "[format]", "function dom graph",
"agf", "[format]", "basic blocks function graph",
"agi", "[format]", "imports graph",
"agr", "[format]", "references graph",
Expand Down Expand Up @@ -12114,10 +12115,13 @@ static void cmd_anal_graph(RCore *core, const char *input) {
return;
}
switch (input[0]) {
case 'D': // "agD"-> dom function graph
r_core_visual_graph (core, NULL, NULL, 3); // 3 means dom graph
break;
case 'f': // "agf"
switch (input[1]) {
case 0: // "agf"
r_core_visual_graph (core, NULL, NULL, false);
r_core_visual_graph (core, NULL, NULL, 0);
break;
case 'b': // "agfb" // braile
cmd_agfb (core);
Expand Down
2 changes: 1 addition & 1 deletion libr/include/r_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ R_API int r_core_visual_anal_classes(RCore *core);
R_API int r_core_visual_types(RCore *core);
R_API int r_core_visual(RCore *core, const char *input);
R_API void r_core_visual_find(RCore *core, RAGraph *g);
R_API int r_core_visual_graph(RCore *core, RAGraph *g, RAnalFunction *_fcn, int is_interactive);
R_API int r_core_visual_graph(RCore *core, RAGraph *g, RAnalFunction *_fcn, int mode);
R_API void r_core_visual_browse(RCore *core, const char *arg);
R_API int r_core_visual_cmd(RCore *core, const char *arg);
R_API void r_core_visual_seek_animation(RCore *core, ut64 addr);
Expand Down
15 changes: 6 additions & 9 deletions libr/util/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,19 @@ R_API void r_graph_reset(RGraph *t) {
return;
}
t->nodes->free = (RListFree)r_graph_node_free;
t->n_nodes = 0;
t->n_nodes = 0; // XXX isnt r_list_length enough?
t->n_edges = 0;
t->last_index = 0;
}

R_API RGraphNode *r_graph_add_node(RGraph *t, void *data) {
if (!t) {
return NULL;
}
r_return_val_if_fail (t && data, NULL);
RGraphNode *n = r_graph_node_new (data);
if (!n) {
return NULL;
if (n) {
n->idx = t->last_index++;
r_list_append (t->nodes, n);
t->n_nodes++; /// istn r_list_length enough?
}
n->idx = t->last_index++;
r_list_append (t->nodes, n);
t->n_nodes++;
return n;
}

Expand Down

0 comments on commit 1f47c28

Please sign in to comment.