Hi,

once in a while I'm in a gdb debug session debugging cc1, and want to print the current function to file.

There's a debug function debug_function that prints a function to stderr, and there are methods to redirect output of a command to a file ( https://sourceware.org/gdb/onlinedocs/gdb/Logging-Output.html ).

And there's a function dump_function_to_file that takes a FILE* parameter, which could be combined with open/close calls in gdb.

But I think a short-hand is easier.

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

Hmm, now I wonder if the order 'cfun.decl, 0, "foo.1.txt"' would make more sense (first two parameters the same as in debug_function).

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

Thanks,
- Tom
Add debug_function_to_file

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

	* tree-cfg.c (debug_function_to_file): New debug function.
	* tree-cfg.h (debug_function_to_file): Declare.

---
 gcc/tree-cfg.c | 14 ++++++++++++++
 gcc/tree-cfg.h |  1 +
 2 files changed, 15 insertions(+)

diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c
index b54545d..8b6ae86 100644
--- a/gcc/tree-cfg.c
+++ b/gcc/tree-cfg.c
@@ -7600,6 +7600,20 @@ debug_function (tree fn, int flags)
   dump_function_to_file (fn, stderr, flags);
 }
 
+/* Dump FUNCTION_DECL FN to FILENAME using FLAGS (see TDF_* in tree.h).  */
+
+DEBUG_FUNCTION void
+debug_function_to_file (tree fn, const char *filename, int flags)
+{
+  FILE *fp = fopen (filename, "w");
+  if (fp == NULL)
+    {
+      fprintf (stderr, "can%'t open %s for writing: %m", filename);
+      return;
+    }
+  dump_function_to_file (fn, fp, flags);
+  fclose (fp);
+}
 
 /* Print on FILE the indexes for the predecessors of basic_block BB.  */
 
diff --git a/gcc/tree-cfg.h b/gcc/tree-cfg.h
index 802e292..6c573e4 100644
--- a/gcc/tree-cfg.h
+++ b/gcc/tree-cfg.h
@@ -80,6 +80,7 @@ extern basic_block move_sese_region_to_fn (struct function *, basic_block,
 				           basic_block, tree);
 extern void dump_function_to_file (tree, FILE *, int);
 extern void debug_function (tree, int) ;
+extern void debug_function_to_file (tree, const char *, int);
 extern void print_loops_bb (FILE *, basic_block, int, int);
 extern void print_loops (FILE *, int);
 extern void debug (struct loop &ref);

Reply via email to