Apple’s GDB Bug?

I was trying to add some features to gdbinit and I needed global variables. I already knew that feature wasn’t working on Mac OS X GDB and I was puzzled why it didn’t work. Some quick tests on a Linux box couldn’t reproduce the same behaviour so something is wrong with Apple’s GDB version. I finally found how it happens ! A very simple .gdbinit to test things would be:

set $bugtest = 10
define bugtest
output $bugtest
end

Replacing our beloved .gdbinit with this simple version and let’s see what happens:

$ gdb
GNU gdb 6.3.50-20050815 (Apple version gdb-696) (Sat Oct 20 18:16:54 GMT 2007)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".
warning: --arch option not supported in this gdb.

(gdb) bugtest
10(gdb)

Now another test:

$ gdb antidebug
GNU gdb 6.3.50-20050815 (Apple version gdb-696) (Sat Oct 20 18:16:54 GMT 2007)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin"...
warning: --arch option not supported in this gdb.
Reading symbols for shared libraries .. done

(gdb) bugtest
void(gdb)

Can you spot the difference? This should help…

$ gdb
GNU gdb 6.3.50-20050815 (Apple version gdb-696) (Sat Oct 20 18:16:54 GMT 2007)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".
warning: --arch option not supported in this gdb.

(gdb) exec-file antidebug
Reading symbols for shared libraries .. done
(gdb) bugtest
10(gdb)

So for some reason (bug ?!?!?) the .gdbinit global variables are lost if we start GDB with a program as argument and they are kept if we start gdb without any argument. Attaching to an already running process has no problems. Using the same trick with one of those unmodified .gdbinit (7.0 or 7.1) and everything goes smooth, no errors 😄.

GDB source code is huge and the changelog might not be helpful to track this problem 😦. I was trying to backport the memory search feature implemented in latest GDB versions but I gave up ! At least I have a workaround…