This adds the function print_graph_cfg that you can call from a
gdb session and directly pipes a dot representation of the
function to 'dot -Tx11'.  The only change needed to the now very
good dumping code is splitting out the actual worker without
the FILE handling.

Probably not suitable for trunk because I use popen/pclose/fileno
which I don't know whether they are available on all host platforms.
So - any taker to transform this into a gdb python macro for .gdbinit
instead?

Richard.

2012-12-12  Richard Biener  <rguent...@suse.de>

        * graph.c (print_graph_cfg_1): New function split out from
        print_graph_cfg.
        (debug_dot_cfg): New function.

Index: gcc/graph.c
===================================================================
*** gcc/graph.c (revision 194438)
--- gcc/graph.c (working copy)
*************** draw_cfg_node_succ_edges (pretty_printer
*** 165,176 ****
  
  /* Print a graphical representation of the CFG of function FUN.  */
  
! void
! print_graph_cfg (const char *base, struct function *fun)
  {
    const char *funcname = function_name (fun);
    int funcdef_no = fun->funcdef_no;
-   FILE *fp = open_graph_file (base, "a");
    int *rpo = XNEWVEC (int, n_basic_blocks);
    basic_block bb;
    int i, n;
--- 165,175 ----
  
  /* Print a graphical representation of the CFG of function FUN.  */
  
! static void
! print_graph_cfg_1 (FILE *fp, struct function *fun)
  {
    const char *funcname = function_name (fun);
    int funcdef_no = fun->funcdef_no;
    int *rpo = XNEWVEC (int, n_basic_blocks);
    basic_block bb;
    int i, n;
*************** print_graph_cfg (const char *base, struc
*** 202,207 ****
--- 201,215 ----
  
    pp_printf (pp, "\t}\n");
    pp_flush (pp);
+ }
+ 
+ /* Print a graphical representation of the CFG of function FUN.  */
+ 
+ void
+ print_graph_cfg (const char *base, struct function *fun)
+ {
+   FILE *fp = open_graph_file (base, "a");
+   print_graph_cfg_1 (fp, fun);
    fclose (fp);
  }
  
*************** finish_graph_dump_file (const char *base
*** 239,241 ****
--- 247,265 ----
    end_graph_dump (fp);
    fclose (fp);
  }
+ 
+ /* Debug the CFG of the function FN using the dot command.  */
+ void DEBUG_FUNCTION
+ debug_dot_cfg (struct function *fn)
+ {
+   FILE *fp = popen("dot -Tx11", "w");
+   if (!fp)
+     return;
+   fputs ("digraph \"\" { overlap=false;\n", fp);
+   print_graph_cfg_1 (fp, fn);
+   fputs ("}\n", fp);
+   fflush (fp);
+   /* ???  Close the pipe fd, that avoids waiting for the child in pclose.  */
+   close (fileno (fp));
+   pclose (fp);
+ }

Reply via email to