CS255/455 Spring 2009 How to Debug your compiler
- Build a debuggable compiler
Usually if we want to debug a program, we need to turn off all compiler optimizations and add '-g' flag when building the program. To build a debuggable GCC, just configure it normally and then make STAGE1_CFLAGS='-g -O0' all-stage1
. However, in our project, at most time we only need to debug our own code, which is supposed to be in a single file gcc-4.2.2/gcc/cs255.c
. So you can just build and install the compiler normally. Then each time after you modify cs255.c, build the compiler with make BOOT_CFLAGS='-O0 -g3'
. This will go through the bootstrap steps. To further save compile time, you can go to obj/gcc
directory and run make CFLAGS='-O0 -g3'
. And don't forget to run make install
.
- Underneath
gcc
Actually, gcc
you called when you do compilation is just a driver for real compiler cc1
, assembler as
and linker collect2
. Use option -v
you can see the exact command sequence. For direct debugging you should run gdb cc1
.
- Get familiar with gdb commands
Refer to gdb documentation
on how to set break points, print the current call stack, examine variable values, and so on.
-
break [file:]func_name
to set a break point; break [filename:]linenum
to set a break point at a line; break ... if cond
to stop only when cond
evaluates to true; info break
to print all breakpoints.
-
backtrace
or bt
to print the current call stack; frame n
or f n
to select a stack frame.
-
print variable
to view variable value.
-
watch
to set watchpoints and disable, enble or delete these points.
- You can modify variable values and call program functions within the debugger, making it a good test environment for trying your coding ideas.
- Some other reference: Debugging with GDB by Stallman, Pesch, Shebs, et al.
- Run gdb in Emacs
A nice feature of running gdb in Emacs is it can link a gdb window and source code window together. Stepping in gdb will move the cursor on the source code window as well. A useful trick is to create a soft link to your cc1
on your test case directory, and then you don't need to change your working directory in gdb back and forth.
-
M-x gdb
-
gdb --annotate=3 cc1
.
-
gdb
has been started, so you can use it normally.
- Dump gimple tree node in gdb
Definitions of dump functions can be found in tree-dump.c
and tree-pretty-print.c
. Let node
be a gimple tree node,
-
p debug_tree(node)
can print some basic information about node
;
-
p print_generic_stmt(stdout, node, 2)
can print the generic form of node
if it is a statement node, where 2
is the value of the macro TDF_SLIM
;
-
p dump_node(node, 2, stdout)
can dump details about node
and its children.
* Set ALLOWTOPICCHANGE =
ChenDing,
BinBao
This topic: Main
> TWikiUsers >
ChenDing >
CS255Spring09 >
CS255Spring09ProjectHome > CS255Spring09DebugCC1
Topic revision: r7 - 2009-01-27 - BinBao