[PATCH] Add debug_function_to_file

Tom de Vries Tom_deVries@mentor.com
Thu Feb 18 14:29:00 GMT 2016


On 17/02/16 14:42, Richard Biener wrote:
> On Wed, Feb 17, 2016 at 1:41 PM, Tom de Vries <Tom_deVries@mentor.com> wrote:
>> 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?
>
> Bonus for making this a helper in gdbhooks.py instead, using
> fopen/fclose and the existing inferior calls.
>

Using attached demonstrator patch, we can use command 
debug-function-to-file in gdb:
...
(gdb) python import sys; sys.path.append('<path>/gcc'); import gdbhooks
Successfully loaded GDB hooks for GCC
(gdb) b tail_merge_optimize
Breakpoint 1 at 0x11be1cc: file gcc/tree-ssa-tail-merge.c, line 1633.
(gdb) r
Starting program: cc1 -quiet hello.c -O2 -o hello.s
Breakpoint 1, tail_merge_optimize (todo=0) at gcc/tree-ssa-tail-merge.c:1633
1633	  int nr_bbs_removed_total = 0;
(gdb) debug-function-to-file "bla.txt"
$1 = 47180688
$2 = <function_decl 0x7ffff61a22a0 main>
$3 = 0
...

and we find that bla.txt contains the result of dump_function_to_file:
...
$ cat bla.txt
main ()
{
   <bb 2>:
   __builtin_puts (&"hello"[0]);
   return 0;

}
...

I would be nice if we could avoid the ${1,2,3} printouts and value 
history assignments, but I'm not sure how to do that.

And given that we have a command with a variable amount of arguments, we 
should try to handle different number of arguments, say these three 
giving the same result:
- debug-function-to-file "bla.txt"
- debug-function-to-file "bla.txt" cfun->decl
- debug-function-to-file "bla.txt" cfun->decl dump_flags
Or perhaps 0 for flags by default, as in pcfun.

Thanks,
- Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0004-Add-debug-function-to-file-to-gdbhooks.py.patch
Type: text/x-patch
Size: 956 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20160218/99a3f4f9/attachment.bin>


More information about the Gcc-patches mailing list