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…

5 thoughts on “Apple’s GDB Bug ?

  1. I wish I had a list of ways in which the GDB on OS X doesn’t do what GDB on other platforms does. So far I have run into two ways (it won’t “find” things across a process’s memory, and it won’t “catch” the loading/unloading of libraries). And I’ve only been using it for a few weeks so who knows how many other ways it is broken.

  2. I have mammon’s gdbinit 6.1 working fine here
    (10.5.6 intel, gdb 6.3.50-20050815 ) so i think
    the problem lies somewhere else.

    FYI the following works fine:

    define bugtest
    set $bugtest = 10
    output $bugtest
    end

    no matter if i run gdb without args etc

    1. Sorry for my delay… Problem must be with gdb.
      Check the following:

      $ gdb /bin/ls
      GNU gdb 6.3.50-20050815 (Apple version gdb-768) (Sun Mar 22 01:47:54 UTC 2009)
      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”…Reading symbols for shared libraries …. done

      (gdb) b *0x000023f0
      Breakpoint 1 at 0x23f0
      (gdb) r
      Starting program: /bin/ls
      Reading symbols for shared libraries +++. done

      Breakpoint 1, 0x000023f0 in ?? ()
      $1 = “test”
      $2 = 10
      $3 = void
      (gdb) q
      The program is running. Exit anyway? (y or n) y

      $ gdb
      GNU gdb 6.3.50-20050815 (Apple version gdb-768) (Sun Mar 22 01:47:54 UTC 2009)
      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”.
      (gdb) exec-file /bin/ls
      Reading symbols for shared libraries …. done
      (gdb) b *0x000023f0
      Breakpoint 1 at 0x23f0
      (gdb) r
      Starting program: /bin/ls
      Reading symbols for shared libraries .+++. done

      Breakpoint 1, 0x000023f0 in ?? ()
      $1 = “test”
      $2 = 10
      $3 = 20
      (gdb)

      This is the .gdbinit I used to test:
      $ cat .gdbinit
      set $globalvariable = 20
      define bugtest
      set $bugtest = 10
      print $bugtest
      end

      define hook-stop
      print “test”
      bugtest
      print $globalvariable
      end

      As you can see it doesn’t support global variables if you use a gdb /bin/ls from the command line, only if you load gdb without parameters and then point to the target using exec-file.

  3. This is quite annoying. Here is an expect script to allow you to type “gdb file” and allow global variables to work:

    $ cat gdbx
    #!/usr/bin/expect
    spawn gdb
    expect “(gdb) ”
    send “exec-file [lindex $argv 0]\n”
    interact

    $ ./gdbx /bin/ls
    spawn gdb
    GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
    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”.
    (gdb) exec-file /bin/ls
    Reading symbols for shared libraries …. done
    (gdb) b *0x000023f0
    Breakpoint 1 at 0x23f0
    (gdb) r
    Starting program: /bin/ls
    Reading symbols for shared libraries ….. done

    Breakpoint 1, 0x000023f0 in ?? ()
    $1 = “test”
    $2 = 10
    $3 = 20
    (gdb)

Leave a Reply

Your email address will not be published. Required fields are marked *