How to compile GDB and other Apple open source packages in Mac OS X

I wanted to recompile so I can modify it’s source and add some custom patches to enhance it’s output… Easier said than done !!!!
There’s no much information around about this and my first attempt was by downloading gdb source package from Apple and trying to compile it. Didn’t compiled out of the box so I had to fix here and there and finally it compiled, but it didn’t work. Searching the web for more ideas and finally understood I had to use darwinbuild environment for this task. Install darwinbuild, follow instructions (crappy ones I might add!) and bang, doesn’t compile due to huge dependencies from include files… Fix here and there and still no luck. Searching the web again and finally the misterious parameter to darwinbuild, -nochroot ! Compile and voila, it works šŸ™‚

And now it’s very easy to do. You should have XCode installed. Follow these steps:

1) Download darwinbuild from their SVN repository (Mac SVN client available here http://homepage.mac.com/martinott/ )
1.1) Snow Leopard already has svn client by default so no need to download. Instructions on how to download,compile and install darwinbuild are here. Macports can too be used to install.
2) Compile and install darwinbuild:

$ make ; sudo make install

3) Create the DMG file and initialize darwinbuild environment (you should use 2 gigabytes for Snow Leopard because of the 64bit version):

The plists and build numbers are available at http://svn.macosforge.org/repository/darwinbuild/trunk/plists/

$ hdiutil create -size 1G -type UDIF -fs HFSX -volname Builds -uid 0 -gid 0 -attach Builds.dmg
$ sudo sh
# vsdbutil -a /Volumes/Builds
# cd /Volumes/Builds
# mkdir Build9G55 (this is for Leopard 10.5.6) (Snow Leopard 10.6.2 is Build10C540)
# cd Build9G55
# darwinbuild -init 9G55 (you need Internet connection)
# darwinxref edit

Insert the following after darwin tag (this will make it compile only for i386):
environment = {
INSTALLED_PRODUCT_ASIDES = YES;
MACOSX_DEPLOYMENT_TARGET = 10.5;
NEXT_ROOT = “”;
RC_ARCHS = i386;
RC_JASPER = YES;
RC_NONARCH_CFLAGS = “-pipe -no-cpp-precomp”;
RC_OS = macos;
RC_PRIVATE = /private;
RC_RELEASE = Leopard;
RC_XBS = YES;
SEPARATE_STRIP = YES;
UNAME_RELEASE = 9.6;
UNAME_SYSNAME = Darwin;
};

For Snow Leopard use this (it will build 32 and 64 bit binaries):

environment = {
INSTALLED_PRODUCT_ASIDES = YES;
MACOSX_DEPLOYMENT_TARGET = 10.6;
NEXT_ROOT = “”;
RC_ARCHS = “i386 x86_64”;
RC_JASPER = YES;
RC_NONARCH_CFLAGS = “-pipe”;
RC_OS = macos;
RC_PRIVATE = /private;
RC_RELEASE = SnowLeopard;
RC_TARGET_CONFIG = MacOSX;
RC_XBS = YES;
SEPARATE_STRIP = YES;
UNAME_RELEASE = 10.0;
UNAME_SYSNAME = Darwin;
};

Editor used is VI. Save and quit. Update: If you have a problem with an invalid property list, you need to replace the quotes in the block you just pasted (copy&paste problems). That should fix the problem.

# darwinbuild -nochroot gdb

Update:
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.

There are problems with libiconv in Snow Leopard. Configure picks the lib available at /usr/local/lib and this generates undefined symbols when compiling. The solution is to link that to /usr/lib/libiconv.dylib or to edit the Makefile. To edit the Makefile you either need to edit the original tar.gz available at Sources dir (and repackage it), or you can issue “darwinbuild -nochroot gdb”, wait for error, then edit BuildRoot/SourceCache/gdb/gdb-1344/src/gdb/Makefile.in , search for LIBICONV and replace with “LIBICONV = /usr/lib/libiconv.dylib”. I have tried to modify the Makefile to pass the correct path to configure but it’s not working… Bah !!! I don’t feel like exploring this (darwinbuild documentation is CRAP) so MacGyver tactics will have to do the job šŸ™‚

If you have some problems compiling for x86_64 then remove that architecture from RC_ARCHS. It worked without any problem for me. The final binary will be a fat binary having i386 and x86_64 versions.

Update End.

Wait for the compilation to finish…
Go to Roots/gdb/gdb-768.root*/usr/libexec/gdb (in Snow Leopard it should be gdb-1344.root*). 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/

Launch gdb and see if it works. It should šŸ™‚ It’s easy after you find how šŸ˜‰

References:

Now I just need to finish the patches… And tha tha that that’s all folks!

fG!

16 thoughts on “How to compile GDB and other Apple open source packages in Mac OS X

  1. Thanks for the HOWTO! My eventual goal is to cross-compile GDB for older versions of OS X, but I cannot get past “darwinxref edit” as I enter :wq in VI to save and quit I get:

    XML parser error:
    Unexpected character / at line 1
    Old-style plist parser error:
    Unexpected character ‘0x201c’ at line 12
    Invalid property list
    e)dit, q)uit
    Action: (edit)

    Have you seen this problem before, and if so, how did you solve it?

    Again, thanks for putting this together!

  2. It is weird. I didn’t change anything, and I’ve had this problem on two separate Leopard machines. Not sure if this is a bug with the XML parser or what! Heck, I’m not even sure how to go about identifying the nature of the actual problem. Alas, as your HOWTO was exactly what I was looking for!

    1. Hummmm could be due to some change in SVN repository that is breaking things. I could reproduce your problem by adding a bogus line with a string like CRAP.
      Most probable is that darwinbuild -init 9G55 failed somewhere. Have you tried to clean and do a new init ?

  3. I just tried blowing things away and rebuilding. I get initialization complete, but adding the line in the XML file reproduces the error. This is on a 10.5.6 machine (9G55) so that pairs up. I’m totally stumped.

  4. Hrm. Sometimes I guess less is more. I just went ahead and compiled without using editing the xref db, so it may be that the step is optional. Not that the error should exist, but GDB compiled just fine without the “environment” options.

    1. Now that’s weird ! I remember I couldn’t compile without removing the PPC part and adding other stuff. I had no time to test it but if you it working then it’s good news šŸ™‚

  5. [quote]
    XML parser error:
    Unexpected character / at line 1
    Old-style plist parser error:
    Unexpected character ā€˜0Ɨ201cā€™ at line 12
    Invalid property list
    e)dit, q)uit
    Action: (edit)
    [quote]
    This error is coming because the code is copy-pasted directly from the site; thereby pasting ā€œā€ characters instead of usual double quotes characters found on the keyboard (“)

  6. Hey fG!,

    i was trying to compile gdb from the sources with darwinbuild because I wanted to apply the patches for the gdbinit bug but I don’t know why when I try to start gdb and run a program inside it i get this error message :

    Unable to find Mach task port for process-id 280: (os/kern) failure (0x5).

    (the process-id obviously is not always the same)

    But if I run gdb as root this won’t happen!
    What could be the problem?
    Thank you in advance !

    1. Hello,

      Most probably you are missing permissions on the new binary. The gdb should be group suid for procmod, like this:
      -rwxr-sr-x 1 root procmod 7318144 Aug 20 12:50 gdb-i386-apple-darwin

      Fixing those permissions should fix the problem.
      fG!

  7. I have a problem with this output:

    ld: symbol(s) not found for architecture i386
    collect2: ld returned 1 exit status
    make[4]: *** [gdb] Error 1
    make[3]: *** [/Volumes/Builds/Build10A432/BuildRoot/private/var/tmp/gdb/gdb-1344.obj/i386-apple-darwin–i386-apple-darwin/stamp-build-gdb] Error 2
    make[2]: *** [build-gdb] Error 2
    make[1]: *** [build] Error 2
    make: *** [install] Error 2

    How to solve this?

  8. hi,
    I tried following your guide (my system is OSX 10.7.2). after building, I copied the new gdb-i386-apple-darwin to /usr/libexec/gdb
    but I notice some new error messages “unable to read unknown load command…” (moreover, it didn’t fix the clear symbols bug)

    bash-3.2$ gdb test
    GNU gdb 6.3.50-20050815 (Apple version gdb-1344) (Fri Dec 23 20:42:42 UTC 2011)
    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 “x86_64-apple-darwin”…unable to read unknown load command 0x24
    unable to read unknown load command 0x26
    unable to read unknown load command 0x24
    unable to read unknown load command 0x26
    unable to read unknown load command 0x24
    unable to read unknown load command 0x26
    Reading symbols for shared libraries .. done
    unable to read unknown load command 0x24
    unable to read unknown load command 0x26

    (gdb) bugtest
    void(gdb) q

    here is the original gdb:

    bash-3.2$ gdb test
    GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov 3 21:59:02 UTC 2011)
    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 “x86_64-apple-darwin”…Reading symbols for shared libraries .. done

    (gdb) bugtest
    void(gdb) q

    1. Hello,

      You need to use the latest version for Lion, which should be 1708 (or at least 1705). You can do it manually at opensource.apple.com, on 10.7.2 section.
      Lion introduced new load commands and gdb is complaining about that. You might want to download my latest patch version (0.3).

      fG!

Leave a Reply

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