Hi,

once in a while I'm in a gdb debug session debugging cc1, and want to print the current function graph to file (see also https://gcc.gnu.org/ml/gcc-patches/2016-02/msg01160.html for the non-graph variant).

That is currently possible by doing:
...
(gdb) call clean_graph_dump_file ("foo.1")
(gdb) call print_graph_cfg ("foo.1", cfun)
(gdb) call finish_graph_dump_file ("foo.1")
...
which will generate a file "foo.1.dot".

But I think a short-hand is easier.

This patch adds a function debug_function_graph_to_file. It can f.i. be called as:
...
(gdb) call debug_function_graph_to_file (cfun.decl, "foo.1.dot")
...

[ I'll post a follow-up WIP patch that adds the flags parameter. ]

OK for stage1 trunk if bootstrap and reg-test succeeds?

Thanks,
- Tom


Add debug_function_graph_to_file

2016-02-17  Tom de Vries  <t...@codesourcery.com>

	* graph.c (print_graph_cfg_fp): New function, factor out of ...
	(print_graph_cfg): ... here.
	(debug_function_graph_to_file): New debug function.
	* graph.h (debug_function_graph_to_file): Declare.

---
 gcc/graph.c | 37 +++++++++++++++++++++++++++++++++----
 gcc/graph.h |  1 +
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/gcc/graph.c b/gcc/graph.c
index 1b28c67..6a06c03 100644
--- a/gcc/graph.c
+++ b/gcc/graph.c
@@ -29,6 +29,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "cfganal.h"
 #include "cfgloop.h"
 #include "graph.h"
+#include "tree.h"
 
 /* DOT files with the .dot extension are recognized as document templates
    by a well-known piece of word processing software out of Redmond, WA.
@@ -267,16 +268,15 @@ draw_cfg_edges (pretty_printer *pp, struct function *fun)
   pp_flush (pp);
 }
 
-/* Print a graphical representation of the CFG of function FUN.
+/* Print a graphical representation of the CFG of function FUN to file FP.
    First print all basic blocks.  Draw all edges at the end to get
    subgraphs right for GraphViz, which requires nodes to be defined
    before edges to cluster nodes properly.  */
 
-void
-print_graph_cfg (const char *base, struct function *fun)
+static void
+print_graph_cfg_fp (FILE *fp, struct function *fun)
 {
   const char *funcname = function_name (fun);
-  FILE *fp = open_graph_file (base, "a");
   pretty_printer graph_slim_pp;
   graph_slim_pp.buffer->stream = fp;
   pretty_printer *const pp = &graph_slim_pp;
@@ -289,6 +289,16 @@ print_graph_cfg (const char *base, struct function *fun)
   draw_cfg_edges (pp, fun);
   pp_printf (pp, "}\n");
   pp_flush (pp);
+}
+
+/* Print a graphical representation of the CFG of function FUN to a file
+   BASE.dot.  */
+
+void
+print_graph_cfg (const char *base, struct function *fun)
+{
+  FILE *fp = open_graph_file (base, "a");
+  print_graph_cfg_fp (fp, fun);
   fclose (fp);
 }
 
@@ -333,3 +343,22 @@ finish_graph_dump_file (const char *base)
   end_graph_dump (fp);
   fclose (fp);
 }
+
+/* Dump FUNCTION_DECL FN to FILENAME.  */
+
+DEBUG_FUNCTION void
+debug_function_graph_to_file (tree fn, const char *filename)
+{
+  FILE *fp = fopen (filename, "w");
+  if (fp == NULL)
+    {
+      fprintf (stderr, "can't open %s for writing", filename);
+      return;
+    }
+
+  start_graph_dump (fp, filename);
+  print_graph_cfg_fp (fp, DECL_STRUCT_FUNCTION (fn));
+  end_graph_dump (fp);
+
+  fclose (fp);
+}
diff --git a/gcc/graph.h b/gcc/graph.h
index fadd7c5..a770f77 100644
--- a/gcc/graph.h
+++ b/gcc/graph.h
@@ -23,5 +23,6 @@ along with GCC; see the file COPYING3.  If not see
 extern void print_graph_cfg (const char *, struct function *);
 extern void clean_graph_dump_file (const char *);
 extern void finish_graph_dump_file (const char *);
+extern void debug_function_graph_to_file (tree, const char *);
 
 #endif /* ! GCC_GRAPH_H */

Reply via email to