Rizin
unix-like reverse engineering framework and cli tools
graph_drawable.c File Reference
#include <rz_core.h>
#include <rz_util/rz_graph_drawable.h>

Go to the source code of this file.

Functions

RZ_API void rz_graph_free_node_info (void *ptr)
 
RZ_API RzGraphNodeInforz_graph_create_node_info (const char *title, const char *body, ut64 offset)
 
RZ_API RzGraphNoderz_graph_add_node_info (RzGraph *graph, const char *title, const char *body, ut64 offset)
 
RZ_API char * rz_graph_drawable_to_dot (RzGraph *graph, const char *node_properties, const char *edge_properties)
 Convert graph to Graphviz dot format. More...
 
RZ_API void rz_graph_drawable_to_json (RzGraph *graph, PJ *pj, bool use_offset)
 Convert graph to JSON. More...
 

Function Documentation

◆ rz_graph_add_node_info()

RZ_API RzGraphNode* rz_graph_add_node_info ( RzGraph graph,
const char *  title,
const char *  body,
ut64  offset 
)

Definition at line 28 of file graph_drawable.c.

28  {
30  RzGraphNodeInfo *data = rz_graph_create_node_info(title, body, offset);
31  if (!data) {
32  return NULL;
33  }
35  if (!node) {
37  }
38  return node;
39 }
#define NULL
Definition: cris-opc.c:27
RZ_API RzGraphNodeInfo * rz_graph_create_node_info(const char *title, const char *body, ut64 offset)
RZ_API void rz_graph_free_node_info(void *ptr)
Definition: graph_drawable.c:8
voidpf uLong offset
Definition: ioapi.h:144
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RzGraphNode * rz_graph_add_nodef(RzGraph *g, void *data, RzListFree user_free)
Definition: graph.c:167
Generic drawable graph node.

References NULL, rz_graph_add_nodef(), rz_graph_create_node_info(), rz_graph_free_node_info(), and rz_return_val_if_fail.

Referenced by add_single_addr_xrefs(), rz_analysis_class_get_inheritance_graph(), and rz_core_analysis_importxrefs().

◆ rz_graph_create_node_info()

RZ_API RzGraphNodeInfo* rz_graph_create_node_info ( const char *  title,
const char *  body,
ut64  offset 
)

Definition at line 18 of file graph_drawable.c.

18  {
20  if (data) {
21  data->title = RZ_STR_DUP(title);
22  data->body = RZ_STR_DUP(body);
23  data->offset = offset;
24  }
25  return data;
26 }
#define RZ_STR_DUP(x)
Definition: rz_str.h:69
#define RZ_NEW0(x)
Definition: rz_types.h:284
ut64 offset
Optional offset for the object corresponding to node.

References rz_analysis_graph_node_info_t::body, rz_analysis_graph_node_info_t::offset, RZ_NEW0, RZ_STR_DUP, and rz_analysis_graph_node_info_t::title.

Referenced by rz_graph_add_node_info().

◆ rz_graph_drawable_to_dot()

RZ_API char* rz_graph_drawable_to_dot ( RzGraph graph,
const char *  node_properties,
const char *  edge_properties 
)

Convert graph to Graphviz dot format.

Parameters
graphGraph with RzGraphNodeInfo used as node user data
node_propertiesList node styling attributes. Can be set to NULL.
edge_propertiesList edge styling attributes. Can be set to NULL.

Definition at line 41 of file graph_drawable.c.

41  {
42  RzList *nodes = graph->nodes;
43  RzListIter *it, *itt;
44  RzGraphNode *node = NULL, *target = NULL;
45  RzStrBuf buf;
48  "digraph code {\nrankdir=LR;\noutputorder=edgesfirst\ngraph [bgcolor=azure];\n"
49  "edge [arrowhead=normal, color=\"#3030c0\" style=bold weight=2 %s];\n"
50  "node [fillcolor=white, style=filled shape=box "
51  "fontsize=\"8\" %s];\n",
52  edge_properties ? edge_properties : "",
53  node_properties ? node_properties : "");
54 
55  rz_list_foreach (nodes, it, node) {
56  RzGraphNodeInfo *print_node = (RzGraphNodeInfo *)node->data;
57  const char *body = print_node->body;
58  if (!body || !*body) {
59  rz_strbuf_appendf(&buf, "%d [URL=\"%s\", color=\"lightgray\", label=\"%s\"]\n",
60  node->idx, print_node->title, print_node->title);
61  } else {
62  rz_strbuf_appendf(&buf, "%d [URL=\"%s\", color=\"lightgray\", label=\"%s\\n%s\"]\n",
63  node->idx, print_node->title, print_node->title, body);
64  }
65  rz_list_foreach (node->out_nodes, itt, target) {
66  rz_strbuf_appendf(&buf, "%d -> %d\n", node->idx, target->idx);
67  }
68  }
69  rz_strbuf_append(&buf, "}\n");
70  return rz_strbuf_drain_nofree(&buf);
71 }
voidpf void * buf
Definition: ioapi.h:138
RZ_API RZ_OWN char * rz_strbuf_drain_nofree(RzStrBuf *sb)
Definition: strbuf.c:349
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
void * data
Definition: rz_graph.h:12
unsigned int idx
Definition: rz_graph.h:11
RzList * out_nodes
Definition: rz_graph.h:13
RzList * nodes
Definition: rz_graph.h:29
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References rz_analysis_graph_node_info_t::body, rz_graph_node_t::data, rz_graph_node_t::idx, if(), rz_graph_t::nodes, NULL, rz_graph_node_t::out_nodes, rz_strbuf_append(), rz_strbuf_appendf(), rz_strbuf_drain_nofree(), rz_strbuf_init(), and rz_analysis_graph_node_info_t::title.

Referenced by print_graph_dot().

◆ rz_graph_drawable_to_json()

RZ_API void rz_graph_drawable_to_json ( RzGraph graph,
PJ pj,
bool  use_offset 
)

Convert graph to JSON.

Parameters
[in]graphGraph to convert
[out]pjJson output structure. Can be used to include the resulting JSON value inside bigger JSON.
[in]use_offsetSet this to true if graph uses RzGraphNodeInfo::offset offset field.

Definition at line 73 of file graph_drawable.c.

73  {
74  RzList *nodes = graph->nodes, *neighbours = NULL;
75  RzListIter *it, *itt;
76  RzGraphNode *node = NULL, *neighbour = NULL;
77  if (!pj) {
78  return;
79  }
80  pj_o(pj);
81  pj_k(pj, "nodes");
82  pj_a(pj);
83 
84  rz_list_foreach (nodes, it, node) {
85  RzGraphNodeInfo *print_node = (RzGraphNodeInfo *)node->data;
86  pj_o(pj);
87  pj_ki(pj, "id", node->idx);
88  if (print_node->title) {
89  pj_ks(pj, "title", print_node->title);
90  }
91  if (print_node->body) {
92  pj_ks(pj, "body", print_node->body);
93  }
94  if (use_offset) {
95  pj_kn(pj, "offset", print_node->offset);
96  }
97  pj_k(pj, "out_nodes");
98  pj_a(pj);
99  neighbours = node->out_nodes;
100  rz_list_foreach (neighbours, itt, neighbour) {
101  pj_i(pj, neighbour->idx);
102  }
103  pj_end(pj);
104  pj_end(pj);
105  }
106  pj_end(pj);
107  pj_end(pj);
108 }
RZ_API PJ * pj_ki(PJ *j, const char *k, int d)
Definition: pj.c:149
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 PJ * pj_i(PJ *j, int d)
Definition: pj.c:284
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
RZ_API PJ * pj_kn(PJ *j, const char *k, ut64 n)
Definition: pj.c:121
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81

References rz_analysis_graph_node_info_t::body, rz_graph_node_t::data, rz_graph_node_t::idx, rz_graph_t::nodes, NULL, rz_analysis_graph_node_info_t::offset, rz_graph_node_t::out_nodes, pj_a(), pj_end(), pj_i(), pj_k(), pj_ki(), pj_kn(), pj_ks(), pj_o(), and rz_analysis_graph_node_info_t::title.

Referenced by rz_core_graph_print().

◆ rz_graph_free_node_info()

RZ_API void rz_graph_free_node_info ( void *  ptr)

Definition at line 8 of file graph_drawable.c.

8  {
9  if (!ptr) {
10  return;
11  }
12  RzGraphNodeInfo *info = ptr;
13  free(info->body);
14  free(info->title);
15  free(info);
16 }
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free(), and info().

Referenced by rz_graph_add_node_info().