Rizin
unix-like reverse engineering framework and cli tools
cagraph.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2020 pancake <pancake@nopcode.org>
2 // SPDX-FileCopyrightText: 2014-2021 ret2libc <sirmy15@gmail.com>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_core.h>
6 #include "core_private.h"
7 
9  rz_agraph_reset(core->graph);
10 }
11 
12 RZ_IPI void rz_core_agraph_add_node(RzCore *core, const char *title, const char *body, int color) {
13  char *b = strdup(body);
14  if (rz_str_startswith(b, "base64:")) {
15  char *newbody = strdup(b);
16  if (!newbody) {
17  free(b);
18  return;
19  }
20  b = rz_str_replace(newbody, "\\n", "", true);
21  newbody = (char *)rz_base64_decode_dyn(b + strlen("base64:"), -1);
22  free(b);
23  if (!newbody) {
24  return;
25  }
26  b = newbody;
27  }
28  if (!RZ_STR_ISEMPTY(b)) {
29  b = rz_str_append(b, "\n");
30  }
32  free(b);
33 }
34 
35 RZ_IPI void rz_core_agraph_del_node(RzCore *core, const char *title) {
36  rz_agraph_del_node(core->graph, title);
37 }
38 
39 RZ_IPI void rz_core_agraph_add_edge(RzCore *core, const char *un, const char *vn) {
40  RzANode *u = rz_agraph_get_node(core->graph, un);
41  RzANode *v = rz_agraph_get_node(core->graph, vn);
42  if (!u) {
43  rz_cons_printf("Node %s not found!\n", un);
44  return;
45  } else if (!v) {
46  rz_cons_printf("Node %s not found!\n", vn);
47  return;
48  }
49  rz_agraph_add_edge(core->graph, u, v);
50 }
51 
52 RZ_IPI void rz_core_agraph_del_edge(RzCore *core, const char *un, const char *vn) {
53  RzANode *u = rz_agraph_get_node(core->graph, un);
54  RzANode *v = rz_agraph_get_node(core->graph, vn);
55  if (!u) {
56  rz_cons_printf("Node %s not found!\n", un);
57  return;
58  } else if (!v) {
59  rz_cons_printf("Node %s not found!\n", vn);
60  return;
61  }
62  rz_agraph_del_edge(core->graph, u, v);
63 }
64 
66  core->graph->can->linemode = rz_config_get_i(core->config, "graph.linemode");
67  core->graph->can->color = rz_config_get_i(core->config, "scr.color");
68  rz_agraph_set_title(core->graph, rz_config_get(core->config, "graph.title"));
69  rz_agraph_print(core->graph);
70 }
71 
73  core->graph->is_tiny = true;
74  int e = rz_config_get_i(core->config, "graph.edges");
75  rz_config_set_i(core->config, "graph.edges", 0);
76  rz_core_visual_graph(core, core->graph, NULL, false);
77  rz_config_set_i(core->config, "graph.edges", e);
78  core->graph->is_tiny = false;
79 }
80 
82  Sdb *db = rz_agraph_get_sdb(core->graph);
83  char *o = sdb_querys(db, "null", 0, "*");
84  rz_cons_print(o);
85  free(o);
86 }
87 
90  if (!ran) {
91  eprintf("This graph contains no nodes\n");
92  return;
93  }
94 
95  ut64 oseek = core->offset;
96  rz_agraph_set_title(core->graph, rz_config_get(core->config, "graph.title"));
97  rz_agraph_set_curnode(core->graph, ran);
98  core->graph->force_update_seek = true;
99  core->graph->need_set_layout = true;
100  core->graph->layout = rz_config_get_i(core->config, "graph.layout");
101  bool ov = rz_cons_is_interactive();
102  core->graph->need_update_dim = true;
103  int update_seek = rz_core_visual_graph(core, core->graph, NULL, true);
104  rz_config_set_i(core->config, "scr.interactive", ov);
105  rz_cons_show_cursor(true);
106  rz_cons_enable_mouse(false);
107  if (update_seek != -1) {
108  rz_core_seek(core, oseek, false);
109  }
110 }
111 
112 static void agraph_print_node_dot(RzANode *n, void *user) {
113  char *label = strdup(n->body);
114  // label = rz_str_replace (label, "\n", "\\l", 1);
115  if (!label || !*label) {
116  rz_cons_printf("\"%s\" [URL=\"%s\", color=\"lightgray\", label=\"%s\"]\n",
117  n->title, n->title, n->title);
118  } else {
119  rz_cons_printf("\"%s\" [URL=\"%s\", color=\"lightgray\", label=\"%s\\n%s\"]\n",
120  n->title, n->title, n->title, label);
121  }
122  free(label);
123 }
124 
125 static void agraph_print_edge_dot(RzANode *from, RzANode *to, void *user) {
126  rz_cons_printf("\"%s\" -> \"%s\"\n", from->title, to->title);
127 }
128 
129 static void agraph_print_edge(RzANode *from, RzANode *to, void *user) {
130  rz_cons_printf("age \"%s\" \"%s\"\n", from->title, to->title);
131 }
132 
133 static void agraph_print_node(RzANode *n, void *user) {
134  char *encbody, *cmd;
135  int len = strlen(n->body);
136 
137  if (len > 0 && n->body[len - 1] == '\n') {
138  len--;
139  }
140  encbody = rz_base64_encode_dyn((const ut8 *)n->body, len);
141  cmd = rz_str_newf("agn \"%s\" base64:%s\n", n->title, encbody);
142  rz_cons_print(cmd);
143  free(cmd);
144  free(encbody);
145 }
146 
148  const char *font = rz_config_get(core->config, "graph.font");
149  rz_cons_printf("digraph code {\nrankdir=LR;\noutputorder=edgesfirst\ngraph [bgcolor=azure];\n"
150  "edge [arrowhead=normal, color=\"#3030c0\" style=bold weight=2];\n"
151  "node [fillcolor=white, style=filled shape=box "
152  "fontname=\"%s\" fontsize=\"8\"];\n",
153  font);
156  rz_cons_printf("}\n");
157 }
158 
162 }
163 
165  PJ *pj = pj_new();
166  if (!pj) {
167  return;
168  }
169  pj_o(pj);
170  pj_k(pj, "nodes");
171  pj_a(pj);
172  rz_agraph_print_json(core->graph, pj);
173  pj_end(pj);
174  pj_end(pj);
176  pj_free(pj);
177 }
178 
179 static void agraph_print_node_gml(RzANode *n, void *user) {
180  rz_cons_printf(" node [\n"
181  " id %d\n"
182  " label \"%s\"\n"
183  " ]\n",
184  n->gnode->idx, n->title);
185 }
186 
187 static void agraph_print_edge_gml(RzANode *from, RzANode *to, void *user) {
188  rz_cons_printf(" edge [\n"
189  " source %d\n"
190  " target %d\n"
191  " ]\n",
192  from->gnode->idx, to->gnode->idx);
193 }
194 
196  rz_cons_printf("graph\n[\n"
197  "hierarchic 1\n"
198  "label \"\"\n"
199  "directed 1\n");
202  rz_cons_print("]\n");
203 }
size_t len
Definition: 6502dis.c:15
RZ_API int rz_core_visual_graph(RzCore *core, RzAGraph *g, RzAnalysisFunction *_fcn, int is_interactive)
Definition: agraph.c:4114
RZ_API RzANode * rz_agraph_add_node_with_color(const RzAGraph *g, const char *title, const char *body, int color)
Definition: agraph.c:3726
RZ_API bool rz_agraph_del_node(const RzAGraph *g, const char *title)
Definition: agraph.c:3769
RZ_API void rz_agraph_set_title(RzAGraph *g, const char *title)
Definition: agraph.c:3720
RZ_API RzANode * rz_agraph_get_first_node(const RzAGraph *g)
Definition: agraph.c:3846
RZ_API void rz_agraph_del_edge(const RzAGraph *g, RzANode *a, RzANode *b)
Definition: agraph.c:3880
RZ_API Sdb * rz_agraph_get_sdb(RzAGraph *g)
Definition: agraph.c:3679
RZ_API void rz_agraph_set_curnode(RzAGraph *g, RzANode *a)
Definition: agraph.c:2696
RZ_API void rz_agraph_reset(RzAGraph *g)
Definition: agraph.c:3889
RZ_API void rz_agraph_foreach_edge(RzAGraph *g, RAEdgeCallback cb, void *user)
Definition: agraph.c:3837
RZ_API RzANode * rz_agraph_get_node(const RzAGraph *g, const char *title)
Definition: agraph.c:3852
RZ_API void rz_agraph_print(RzAGraph *g)
Definition: agraph.c:3687
static void update_seek(RzConsCanvas *can, RzANode *n, int force)
Definition: agraph.c:2511
RZ_API void rz_agraph_foreach(RzAGraph *g, RzANodeCallback cb, void *user)
Definition: agraph.c:3829
RZ_API void rz_agraph_print_json(RzAGraph *g, PJ *pj)
Definition: agraph.c:3694
RZ_API void rz_agraph_add_edge(const RzAGraph *g, RzANode *a, RzANode *b)
Definition: agraph.c:3862
#define e(frag)
#define RZ_IPI
Definition: analysis_wasm.c:11
static void agraph_print_node(RzANode *n, void *user)
Definition: cagraph.c:133
static void agraph_print_edge(RzANode *from, RzANode *to, void *user)
Definition: cagraph.c:129
RZ_IPI void rz_core_agraph_add_edge(RzCore *core, const char *un, const char *vn)
Definition: cagraph.c:39
RZ_IPI void rz_core_agraph_print_interactive(RzCore *core)
Definition: cagraph.c:88
static void agraph_print_edge_gml(RzANode *from, RzANode *to, void *user)
Definition: cagraph.c:187
static void agraph_print_node_gml(RzANode *n, void *user)
Definition: cagraph.c:179
RZ_IPI void rz_core_agraph_print_tiny(RzCore *core)
Definition: cagraph.c:72
static void agraph_print_edge_dot(RzANode *from, RzANode *to, void *user)
Definition: cagraph.c:125
static void agraph_print_node_dot(RzANode *n, void *user)
Definition: cagraph.c:112
RZ_IPI void rz_core_agraph_reset(RzCore *core)
Definition: cagraph.c:8
RZ_IPI void rz_core_agraph_print_json(RzCore *core)
Definition: cagraph.c:164
RZ_IPI void rz_core_agraph_print_dot(RzCore *core)
Definition: cagraph.c:147
RZ_IPI void rz_core_agraph_del_edge(RzCore *core, const char *un, const char *vn)
Definition: cagraph.c:52
RZ_IPI void rz_core_agraph_print_rizin(RzCore *core)
Definition: cagraph.c:159
RZ_IPI void rz_core_agraph_print_sdb(RzCore *core)
Definition: cagraph.c:81
RZ_IPI void rz_core_agraph_del_node(RzCore *core, const char *title)
Definition: cagraph.c:35
RZ_IPI void rz_core_agraph_print_gml(RzCore *core)
Definition: cagraph.c:195
RZ_IPI void rz_core_agraph_print_ascii(RzCore *core)
Definition: cagraph.c:65
RZ_IPI void rz_core_agraph_add_node(RzCore *core, const char *title, const char *body, int color)
Definition: cagraph.c:12
RZ_API ut64 rz_config_get_i(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:119
RZ_API RzConfigNode * rz_config_set_i(RzConfig *cfg, RZ_NONNULL const char *name, const ut64 i)
Definition: config.c:419
RZ_API RZ_BORROW const char * rz_config_get(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:75
RZ_API bool rz_cons_enable_mouse(const bool enable)
Definition: cons.c:500
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
RZ_API bool rz_cons_is_interactive(void)
Definition: cons.c:365
RZ_API void rz_cons_show_cursor(int cursor)
Definition: cons.c:1581
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
const char * v
Definition: dsignal.c:12
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
RZ_API char * sdb_querys(Sdb *r, char *buf, size_t len, const char *_cmd)
Definition: query.c:164
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
int n
Definition: mipsasm.c:19
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API char * rz_base64_encode_dyn(const ut8 *bin, size_t sz)
Definition: ubase64.c:92
RZ_API ut8 * rz_base64_decode_dyn(const char *in, int len)
Definition: ubase64.c:65
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_k(PJ *j, const char *k)
Definition: pj.c:104
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_str_append(char *ptr, const char *string)
Definition: str.c:1063
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API char * rz_str_replace(char *str, const char *key, const char *val, int g)
Definition: str.c:1110
RZ_API bool rz_str_startswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string starts with a specifc sequence of characters (case sensitive)
Definition: str.c:3286
RZ_API bool rz_core_seek(RzCore *core, ut64 addr, bool rb)
Seek to addr.
Definition: seek.c:116
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
#define b(i)
Definition: sha256.c:42
Definition: dis.h:35
Definition: rz_pj.h:12
int force_update_seek
Definition: rz_agraph.h:71
bool need_set_layout
Definition: rz_agraph.h:69
RzConsCanvas * can
Definition: rz_agraph.h:47
ut64 offset
Definition: rz_core.h:301
RzAGraph * graph
Definition: rz_core.h:333
RzConfig * config
Definition: rz_core.h:300
Definition: sdb.h:63
static int color
Definition: visual.c:20
ut64(WINAPI *w32_GetEnabledXStateFeatures)()