How to compile gdb in Mountain Lion (updated)

This is an up-to-date version of the old original post about recompiling gdb and other open source packages provided by opensource.apple.com. I’m doing it mostly because code signing is now mandatory for gdb and there’s a stupid old bug that Apple still didn’t fixed since Snow Leopard. I forgot about it on my latest reinstall and lost an afternoon :-). This way you and me will not make the same mistake.

You should have XCode installed. Follow these steps:

1) Download darwinbuild from their SVN repository.
1.1) Since Snow Leopard there is a svn client by default so no need to download.
1.2) Follow the instructions on how to download,compile and install darwinbuild here. Use the guide for Snow Leopard/Lion version, it’s compatible with Mountain Lion.

2) Compile and install darwinbuild:

$ make ; sudo make install

3) Create the DMG file and initialize darwinbuild environment (you should use at least 2 gigabytes):

The plists and build numbers are available at http://svn.macosforge.org/repository/darwinbuild/trunk/plists/. Use build number 12A269 (it’s for 10.8.0 but works ok for all others).

$ hdiutil create -size 2G -type UDIF -fs HFSX -volname Builds -uid 0 -gid 0 -attach Builds.dmg
$ sudo sh
# vsdbutil -a /Volumes/Builds
# cd /Volumes/Builds
# mkdir Build12A269
# cd Build12A269
# darwinbuild -init 12A269 (you need Internet connection)
# darwinxref edit

In darwinxref edit you need to add the gdb package to the configuration. Go to the projects section and add the following:

gdb = {
version = 1822;
};

Default editor is VI. Save and quit. If you have a problem with an invalid property list, use the same tab alignment as the other entries. That should fix it.

4) Clone the gdb-ng repo from github if you want my patches included (you probably do!). Else skip to step 5) (darwinbuild will download the package from Apple opensource repo).

# cd /Volumes/Builds/Build12A269/Sources
# git clone git://github.com/gdbinit/gdb-ng.git
# cd gdb-ng
# bash pack.sh
# mv gdb-1822.tar.gz .. (check version in case it changes)
# cd /Volumes/Builds/Build12A269

5) Compile gdb.

# darwinbuild -nochroot gdb

The -nosource option has been added to recent darwinbuild versions. This option will allow you to patch directly into BuildRoot/SourceCache/.
The first time you shouldn’t use this option so darwinbuild will download gdb package. After that you can use it if you want to patch directly gdb source files (that’s what I do with my gdb patches). It’s much easier and faster than having to patch and compress the whole gdb source. After you patch, you just issue “darwinbuild -nochroot -nosource gdb” and this will not unpack the original source but instead use whatever is at SourceCache.

Wait for the compilation to finish…

Go to Roots/gdb/gdb-1822.root*/usr/libexec/gdb. You should have a gdb-i386-apple-darwin. Backup the original and copy this one over.

# cp /usr/libexec/gdb/gdb-i386-apple-darwin /usr/libexec/gdb/gdb-i386-apple-darwin.orig
# cp gdb-i386-apple-darwin /usr/libexec/gdb/

The latest step is to codesign the binary. This is because taskgated default configuration has changed and it’s not anymore sufficient to have the binary suid to procmod group. It must have entitlements and be codesigned. The process is not just creating a self-signed certificate and codesign the binary with it. There is an old bug since Snow Leopard that complicates it a little bit. Follow this guide from LLDB code signing document. You can either code sign the binary you copied above to /usr/libexec/gdb or sign it at the Roots folder and copy the signed version.

Launch gdb and see if it works. It should ask you for your password the first time (after each reboot). If everything is ok you should be able to attach to or run the target process.

Now you can enjoy your next afternoon in case you want/have to compile gdb. You might also want to download and install gdbinit to improve gdb’s output and available commands.

fG!

23 thoughts on “How to compile gdb in Mountain Lion (updated)

      1. thanks for your answer… I’ve installed your version but it seems that I can’t break on symbol…why?

        iPad-4-White:~ root# gdb -p 990
        warning: unrecognized host cpusubtype 11, defaulting to host==armv7.
        GNU gdb 6.3.50-20050815 (Apple version gdb-1708 + reverse.put.as patches v0.4) (Mon Apr 16 00:53:47 UTC 2012)
        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 “arm-apple-darwin”.
        /private/var/root/990: No such file or directory
        Attaching to process 990.
        Reading symbols for shared libraries . done
        Reading symbols for shared libraries …………………………………………………………………………………………………………………………………………………………………… done
        Reading symbols for shared libraries + done
        0x3951ae30 in mach_msg_trap ()
        (gdb) break -[NewsPadViewController addFeeds]
        Function “-[NewsController addFeeds]” not defined.
        Make breakpoint pending on future shared library load? (y or [n]) y
        Breakpoint 1 (-[NewsController addFeeds]) pending.
        (gdb) c
        Continuing.

        1. Hummm I never break on symbols.
          Not sure if it’s a problem with that version or iOS.
          I will try to compile a more recent version and test it out.

    1. Hummmm I think the gdbserver is not built by this Apple package.
      Need to give it a try, but I think I remember someone telling it didn’t work.

  1. Hey,

    For some reason darwinxref edit isn’t working for me. Everytime I :wq or 😡 after the edit, the command line gives me “darwinxref [edit cancelled]: cancelled by user.” When I rerun darwinxref edit, my gdb line isn’t there….

    Any idea what gives?

    Thanks.

  2. Hi fG!

    When issuing darwinbuild -nochroot gdb I’m getting this error. Any idea on how to fix it?

    3 warnings and 2 errors generated.
    make[4]: *** [cplus-dem.o] Error 1
    make[3]: *** [/Volumes/Builds/Build12A269/BuildRoot/private/var/tmp/gdb/gdb-1822.obj/i386-apple-darwin–i386-apple-darwin/stamp-build-core] Error 2
    make[2]: *** [build-core] Error 2
    make[1]: *** [build] Error 2
    make: *** [install] Error 2
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    BUILD TIME: 0h 1m 43s
    EXIT STATUS: 2

    1. Yeah I have the same error following this guide… any clues?

      /Volumes/Builds/Build12A269/BuildRoot/SourceCache/gdb/gdb-1822/src/libiberty/cplus-dem.c:3177:20: error: implicit declaration
      of function ‘atoi’ is invalid in C99 [-Werror,-Wimplicit-function-declaration]
      qualifiers = atoi (num);
      ^
      3 warnings and 2 errors generated.
      make[4]: *** [cplus-dem.o] Error 1
      make[3]: *** [/Volumes/Builds/Build12A269/BuildRoot/private/var/tmp/gdb/gdb-1822.obj/i386-apple-darwin–i386-apple-darwin/sta
      mp-build-core] Error 2
      make[2]: *** [build-core] Error 2
      make[1]: *** [build] Error 2
      make: *** [install] Error 2
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      BUILD TIME: 0h 0m 53s
      EXIT STATUS: 2

  3. hi, I got up to step 5!
    I think doing everything correctly, but at the end of the compile,
    use the string : sudo darwinbuild -nochroot gdb

    I get this response, where am I wrong?

    sudo darwinbuild -nochroot gdb
    ERROR: project not found: gdb

    thanks in advance!

    1. Hi,

      Are you located in the build number folder? You must be there else I think you will run into problems.
      Also, did you add the gdb project to the configuration?

      fG!

  4. Hello there,

    These instructions do not work. The last revision that can be set up correctly, 11C74, fails due to the lack of CoreOSMakefiles. Is there any way to obtain this package?

  5. Any ideas on how to get gdb running on Yosemite? I was able to install using brew and code sing the binary but whenever I try to attach an application I get the following error (copying the entire output in case there are any useful clues):

    gdb /Applications/0xED.app/Contents/MacOS/0xED
    GNU gdb (GDB) 7.10.1
    Copyright (C) 2015 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law. Type “show copying”
    and “show warranty” for details.
    This GDB was configured as “x86_64-apple-darwin14.5.0”.
    Type “show configuration” for configuration details.
    For bug reporting instructions, please see:
    .
    Find the GDB manual and other documentation resources online at:
    .
    For help, type “help”.
    Type “apropos word” to search for commands related to “word”…
    “/Applications/0xED.app/Contents/MacOS/0xED”: not in executable format: File format not recognized

Leave a Reply

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