Graph commands

When analyzing data it is usually handy to have different ways to represent it in order to get new perspectives to allow the analyst to understand how different parts of the program interact.

Representing basic block edges, function calls, string references as graphs show a very clear view of this information.

Radare2 supports various types of graph available through commands starting with ag:

  1. [0x00005000]> ag?
  2. |Usage: ag<graphtype><format> [addr]
  3. | Graph commands:
  4. | aga[format] Data references graph
  5. | agA[format] Global data references graph
  6. | agc[format] Function callgraph
  7. | agC[format] Global callgraph
  8. | agd[format] [fcn addr] Diff graph
  9. | agf[format] Basic blocks function graph
  10. | agi[format] Imports graph
  11. | agr[format] References graph
  12. | agR[format] Global references graph
  13. | agx[format] Cross references graph
  14. | agg[format] Custom graph
  15. | ag- Clear the custom graph
  16. | agn[?] title body Add a node to the custom graph
  17. | age[?] title1 title2 Add an edge to the custom graph
  18. Output formats:
  19. | <blank> Ascii art
  20. | * r2 commands
  21. | d Graphviz dot
  22. | g Graph Modelling Language (gml)
  23. | j json ('J' for formatted disassembly)
  24. | k SDB key-value
  25. | t Tiny ascii art
  26. | v Interactive ascii art
  27. | w [path] Write to path or display graph image (see graph.gv.format and graph.web)

The structure of the commands is as follows: ag <graph type> <output format>.

For example, agid displays the imports graph in dot format, while aggj outputs the custom graph in JSON format.

Here’s a short description for every output format available:

Ascii Art ** (e.g. agf)

Displays the graph directly to stdout using ASCII art to represent blocks and edges.

Warning: displaying large graphs directly to stdout might prove to be computationally expensive and will make r2 not responsive for some time. In case of a doubt, prefer using the interactive view (explained below).

Interactive Ascii Art (e.g. agfv)

Displays the ASCII graph in an interactive view similar to VV which allows to move the screen, zoom in / zoom out, …

Tiny Ascii Art (e.g. agft)

Displays the ASCII graph directly to stdout in tiny mode (which is the same as reaching the maximum zoom out level in the interactive view).

Graphviz dot (e.g. agfd)

Prints the dot source code representing the graph, which can be interpreted by programs such as graphviz or online viewers like this

JSON (e.g. agfj)

Prints a JSON string representing the graph.

  • In case of the f format (basic blocks of function), it will have detailed information about the function and will also contain the disassembly of the function (use J format for the formatted disassembly.

  • In all other cases, it will only have basic information about the nodes of the graph (id, title, body, and edges).

Graph Modelling Language (e.g. agfg)

Prints the GML source code representing the graph, which can be interpreted by programs such as yEd

SDB key-value (e.g. agfk)

Prints key-value strings representing the graph that was stored by sdb (radare2’s string database).

R2 custom graph commands (e.g. agf*)

Prints r2 commands that would recreate the desired graph. The commands to construct the graph are agn [title] [body] to add a node and age [title1] [title2] to add an edge. The [body] field can be expressed in base64 to include special formatting (such as newlines).

To easily execute the printed commands, it is possible to prepend a dot to the command (.agf*).

Web / image (e.g. agfw)

Radare2 will convert the graph to dot format, use the dot program to convert it to a .gif image and then try to find an already installed viewer on your system (xdg-open, open, …) and display the graph there.

The extension of the output image can be set with the graph.extension config variable. Available extensions are png, jpg, gif, pdf, ps.

Note: for particularly large graphs, the most recommended extension is svg as it will produce images of much smaller size

If graph.web config variable is enabled, radare2 will try to display the graph using the browser (this feature is experimental and unfinished, and disabled by default.)