Debug C and Objective-C of Emacs with LLDB on macOS Big Sur


Summary

This post is a note about debugging C and Objective-C code of Emacs with LLDB.
If you want info about Emacs and dap-mode, see this post:

As an example, I wrote the procedure for “step executing” that the EmacsApp does not have a mainWindow in applicationDidFinishLaunching in nsterm.m.

By the way, I used GDB at the beginning of debugging, but I couldn’t check the values ​​of Objective-C variables. Therefore, I switched to LLDB from GDB.

Prerequisites

Steps

Build Emacs (C/Objective-C) with debug symbol.

./configure CFLAGS="-g3 -O0"\
            --with-ns\
            --with-modules\
            --without-x\
            --without-selinux\
            --without-mail-unlink\
            --without-mailhost\
            --without-pop\
            --without-mailutils\
            --without-compress-install
make -j$(sysctl -n machdep.cpu.thread_count) bootstrap
make install

Launch lldb.

$ lldb nextstep/Emacs.app/Contents/MacOS/Emacs
(lldb) target create "nextstep/Emacs.app/Contents/MacOS/Emacs"
Current executable set to '/path/to/your/emacs/nextstep/Emacs.app/Contents/MacOS/Emacs' (x86_64).
(lldb)

Set breakpoint.

(lldb) b nsterm.m:5626

Run Emacs via lldb.

# `-Q` is init option for Emacs
# More detail is https://www.gnu.org/software/emacs/manual/html_node/emacs/Initial-Options.html
(lldb) run -Q
Process 60822 launched: '/path/to/your/emacs/nextstep/Emacs.app/Contents/MacOS/Emacs' (x86_64)
2022-01-01 22:07:24.890807+0900 Emacs[60822:1208222] (
)
2022-01-01 22:07:24.925930+0900 Emacs[60822:1208222] SecTaskLoadEntitlements failed error=22 cs_flags=20, pid=60822
2022-01-01 22:07:24.926053+0900 Emacs[60822:1208222] SecTaskCopyDebugDescription: Emacs[60822]/0#-1 LF=0
Process 60822 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100348d3b Emacs`-[EmacsApp applicationDidFinishLaunching:](self=0x0000000101318500, _cmd="applicationDidFinishLaunching:", notification=@"NSApplicationDidFinishLaunchingNotification") at nsterm.m:5626:4
   5623	#ifdef NS_IMPL_GNUSTEP
   5624	  ((EmacsApp *)self)->applicationDidFinishLaunchingCalled = YES;
   5625	#endif
-> 5626	  [NSApp setServicesProvider: NSApp];
   5627
   5628	  [self antialiasThresholdDidChange:nil];
   5629	#ifdef NS_IMPL_COCOA
Target 0: (Emacs) stopped.

Print value.

(lldb) po NSApp
<EmacsApp: 0x101318500>

(lldb) po [EmacsApp superclass]
NSApplication

(lldb) po [[EmacsApp sharedApplication] mainWindow]
 nil

Step-over

(lldb) n
Process 60822 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
    frame #0: 0x0000000100348d52 Emacs`-[EmacsApp applicationDidFinishLaunching:](self=0x0000000101318500, _cmd="applicationDidFinishLaunching:", notification=@"NSApplicationDidFinishLaunchingNotification") at nsterm.m:5628:4
   5625	#endif
   5626	  [NSApp setServicesProvider: NSApp];
   5627
-> 5628	  [self antialiasThresholdDidChange:nil];
   5629	#ifdef NS_IMPL_COCOA
   5630	  [[NSNotificationCenter defaultCenter]
   5631	    addObserver:self
Target 0: (Emacs) stopped.

Continue Emacs.

(lldb) c
Process 60822 resuming
2022-01-01 22:17:28.303972+0900 Emacs[60822:1208222] SecTaskLoadEntitlements failed error=22 cs_flags=20, pid=60822
2022-01-01 22:17:28.304077+0900 Emacs[60822:1208222] SecTaskCopyDebugDescription: Emacs[60822]/0#-1 LF=0
2022-01-01 22:17:28.462339+0900 Emacs[60822:1208222] SecTaskLoadEntitlements failed error=22 cs_flags=20, pid=60822
2022-01-01 22:17:28.462428+0900 Emacs[60822:1208222] SecTaskCopyDebugDescription: Emacs[60822]/0#-1 LF=0
2022-01-01 22:17:28.670099+0900 Emacs[60822:1208222] MTLIOAccelDevice bad MetalPluginClassName property (null)
2022-01-01 22:17:28.720344+0900 Emacs[60822:1208222] +[MTLIOAccelDevice registerDevices]: Zero Metal services found

Quit lldb.

(lldb) q

References