Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Building lldb with GDB JIT support on Mac OSX.

Building lldb on Mac

Create Codesign Certificate

First we need to create a certificate. The llvm provided a way to do that, but I found this way to work slightly better for me. Just substitute lldb_codesign for the certificate name, instead of gdb-cert.

Install swig dependency

brew install swig

Pulling Code and Building

Note

Currently GDB JIT support is disabled for MAC. To turn it on apply the following patch before running the build.

enable-jit.diff

Index: source/Plugins/JITLoader/GDB/JITLoaderGDB.cpp
===================================================================
--- source/Plugins/JITLoader/GDB/JITLoaderGDB.cpp	(revision 213962)
+++ source/Plugins/JITLoader/GDB/JITLoaderGDB.cpp	(working copy)
@@ -363,9 +363,9 @@
 JITLoaderGDB::CreateInstance(Process *process, bool force)
 {
     JITLoaderSP jit_loader_sp;
-    ArchSpec arch (process->GetTarget().GetArchitecture());
-    if (arch.GetTriple().getVendor() != llvm::Triple::Apple)
-        jit_loader_sp.reset(new JITLoaderGDB(process));
+    /*ArchSpec arch (process->GetTarget().GetArchitecture());
+    if (arch.GetTriple().getVendor() != llvm::Triple::Apple)*/
+    jit_loader_sp.reset(new JITLoaderGDB(process));
     return jit_loader_sp;
 }
 

Apply it via patch -p0 -i ~/enable-jit.diff.

Steps

svn co http://llvm.org/svn/llvm-project/lldb/trunk lldb
cd lldb
xcodebuild -configuration Release

Running the build will automatically check out extra sources it needs, like llvm. The build will prompt you twice for your username and password in order to perform the code signing.

Replacing default lldb

Assunming /usr/local/bin is in your path before /usr/bin - homebrew users will be setup that way already, do

ln -s `pwd`/build/Release/lldb /usr/local/bin/lldb
lldb --version

This should print lldb-320.99.0 or higher if all is setup correctly.

Opening Xcode Project

In case you are interested at looking at lldbs code you may open the Xcode project provided.

open lldb.xcodeproj

Alternate more complex ways to build

There are other ways to build, but I've found them to be harder to get right, especially the codesigning that is performed in these cases doesn't work out of the box.

Additionally I could never get lldb to find the debugserver even after it was codesigned directly, so only try the below if you are ready for a bit of pain.

Get Build Tools and Dependencies

brew install ninja
brew install swig
brew install cmake

Clone Projects

Clone llvm and then lldb and clang into it.

git clone https://github.com/llvm-mirror/llvm.git
cd llvm/tools
git clone https://github.com/llvm-mirror/lldb
git clone https://github.com/llvm-mirror/clang
cd ..

Fixing signing script

When using --entitlements, signing succeeds, but launching debugserver localhost:<port> --attach <pid> fails with Killed 9.

Remove the --entitlements sections and change to:

CODESIGN_ALLOCATE=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate codesign --force --sign lldb_codesign debugserver

Build with Ninja

First we generate the ninja build file with cmake and then tell ninja to build llvm entirely which builds all we need including lldb and debugserver.

mkdir build
cd build
cmake .. -G Ninja
ninja all

You may see the following error as I did (seems like not too many folks try to build this on Mac?)

../tools/lldb/source/Host/common/Host.cpp:371:68: error: use of undeclared identifier 'CPU_SUBTYPE_X86_64_H'
                if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == CPU_SUBTYPE_X86_64_H)

you'll have to fix the Host.cpp file a bit basically to include Mach0.h and to namespace CPU_SUBTYPE_X86_64_H:

diff --git a/source/Host/common/Host.cpp b/source/Host/common/Host.cpp
index 275b446..9cba9fe 100644
--- a/source/Host/common/Host.cpp
+++ b/source/Host/common/Host.cpp
@@ -33,6 +33,7 @@
 #endif
 
 #if defined (__APPLE__)
+#include "llvm/Support/MachO.h"
 #include <mach/mach_port.h>
 #include <mach/mach_init.h>
 #include <mach-o/dyld.h>
@@ -364,11 +365,11 @@ Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
                     // cpusubtype will be correct as if it were for a 64 bit architecture
                     g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype | CPU_ARCH_ABI64, cpusubtype);
                 }
-                
+
                 // Now we need modify the cpusubtype for the 32 bit slices.
                 uint32_t cpusubtype32 = cpusubtype;
 #if defined (__i386__) || defined (__x86_64__)
-                if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == CPU_SUBTYPE_X86_64_H)
+                if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == llvm::MachO::CPU_SUBTYPE_X86_64_H)
                     cpusubtype32 = CPU_SUBTYPE_I386_ALL;
 #elif defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
                 if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64)

At this point you should have a bin directory added that looks similar to the below:

bin
├── clang-tblgen
├── lldb -> lldb-3.5.0
├── lldb-3.5.0
├── llvm-lit
└── llvm-tblgen
➝  cd bin
➝  lldb --version
lldb-320.99.0

However we aren't completely done yet:

➝  lldb -- top
Current executable set to 'top' (x86_64).
(lldb) r
error: process launch failed: unable to locate debugserver
(lldb)

If lldb cannot find debugserver

We are going to use the debugserver separately and connect lldb to it. It is very similar to getting this up and running for debugging ios apps

Launch the debugserver that is in the same folder as lldb.

debugserver localhost:<port> --attach <pid-of-running-program>

In different terminal launch the lldb you built and connect it to the running debug server

➝  lldb -o 'process connect connect://localhost:3000'

Generating an Xcode project for LLVM

In case you want to generate an Xcode project to look at the entire llvm project you can do that with cmake as well after you followed along the above Get Build Tools and Dependencies and Clone Projects sections.

cmake .. -G Xcode
open LLDB.xcodeproj

You can then build the entire llvm project in Xcode.

@burblebee
Copy link

burblebee commented Oct 2, 2014

debugserver codesigning issue with cmake+ninja build on OSX was fixed in lldb svn rev.218890, so no more "error: process launch failed: unable to locate debugserver", although you will need to point LLDB_DEBUGSERVER_PATH at your built debugserver before debugging with lldb (e.g. export LLDB_DEBUGSERVER_PATH=$BUILDDIR/bin/debugserver). No need to launch debugserver separately anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment