This blog describes anything about @mrkn's thoughts, which includes computer programming (mostly Ruby, Haskell, and C++), mathematics, physics, and so on. Each article is written in English for learning it.

2009-12-18

To use preprocessor macros on gdb

When debugging ruby on gdb, we often want to expand some preprocessor macros, such as RSTRING_LEN, RARRAY_PTR. To make preprocessor macros available to gdb, we should call gcc with these debugging options: -gdwarf-2 -g3.

To configure ruby with these options, we do,

./configure debugflags="-gdwarf-2 -g3"

Then build ruby,

make

This allows us to expand preprocessor macros in gdb like this:

$ gdb --quiet /opt/ruby/trunk/bin/ruby
Reading symbols for shared libraries .... done
(gdb) b ruby_yyparse 
Breakpoint 1 at 0x20c49ba5cd1c8b: file parse.c, line 4583.
(gdb) r
Starting program: /opt/ruby/trunk/bin/ruby 
Reading symbols for shared libraries +++... done
Reading symbols for shared libraries . done

Breakpoint 1, ruby_yyparse (parser=0x10060c310) at parse.c:4583
4583    {
(gdb) macro expand Qfalse
expands to: ((VALUE)RUBY_Qfalse)
(gdb) info macro RSTRING_PTR
Defined at ../include/ruby/ruby.h:619
  included at /Users/muraken/src/ruby.git/build-osx/parse.c:18
#define RSTRING_PTR(str) (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? RSTRING(str)->as.ary : RSTRING(str)->as.heap.ptr)
(gdb) 

Note that we must be in a frame where the target preprocessor macros are visible.

I thank hayeah and ujihisa for helpful discussion about this topic and proofreading my English.

See also:

Followers