Not able to attach Xcode debugger to System Extension by name on macOS

I am developing an NE based System Extension on macOS Catalina 10.15.7 using Xcode 12.4. I have everything in place and my extension is running fine. However, I am not able to attach debugger to my extension from Xcode. I can go to terminal and as root user, I am able to attach lldb to the running system extension. But I am not able to attach the debugger from Xcode using Debug -> Attach to Process by PID or Name -> Debug Process As -> root. Xcode just keeps on Waiting to Attach. If the extension is already running, then I am able to attach by PID but never by name. I would like to be able to start debugger using process name and launch the extension and be able to debug from first line of code in extension.

Also, I really like Xcode's contextual debugging where I can see most of the variables and information without any extra effort. With terminal the debugging is comparatively hectic.

So, is there any way I can attach to System Extensions by name in Xcode? I have tried using target name as well as extension bundle identifier and several other combinations. From terminal I can attach using bundle identifier of the extension itself.
Answered by DTS Engineer in 674520022

I would like to be able to start debugger using process name and
launch the extension and be able to debug from first line of code in
extension.

Due to the way that sysexes are loaded — they are very different from apexes! — I would not expect to be able to attach before the process is running. However, you wrote:

If the extension is already running, then I am able to attach by PID
but never by name.

and I would expect name and PID to work equally well here, that is, they both work or they both don’t. That fact that’s not the case is weird.

I have tried using target name as well as extension bundle identifier
and several other combinations.

Try this…

Run lldb using sudo.

Code Block
% sudo lldb


Type in this command:

Code Block
(lldb) process attach -n ***


but don’t press Return. In this context *** is the name that you successfully attached with in the past.

Press Tab.

LLDB should print a list of candidates. Copy the full expansion of the sysex name.

Go to Xcode and enter that into Debug > Attach to Process by PID or Name.

Does that work?



Oh, and btw, if you want to debug from the start of your sysex, add a call to pause at the beginning of main. That’ll pause waiting for you to attach with the debugger (attaching delivers a signal which breaks you out of pause).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Accepted Answer

I would like to be able to start debugger using process name and
launch the extension and be able to debug from first line of code in
extension.

Due to the way that sysexes are loaded — they are very different from apexes! — I would not expect to be able to attach before the process is running. However, you wrote:

If the extension is already running, then I am able to attach by PID
but never by name.

and I would expect name and PID to work equally well here, that is, they both work or they both don’t. That fact that’s not the case is weird.

I have tried using target name as well as extension bundle identifier
and several other combinations.

Try this…

Run lldb using sudo.

Code Block
% sudo lldb


Type in this command:

Code Block
(lldb) process attach -n ***


but don’t press Return. In this context *** is the name that you successfully attached with in the past.

Press Tab.

LLDB should print a list of candidates. Copy the full expansion of the sysex name.

Go to Xcode and enter that into Debug > Attach to Process by PID or Name.

Does that work?



Oh, and btw, if you want to debug from the start of your sysex, add a call to pause at the beginning of main. That’ll pause waiting for you to attach with the debugger (attaching delivers a signal which breaks you out of pause).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I would expect name and PID to work equally well here, that is, they both work or they both don’t. That fact that’s not the case is weird.

Yep. Thats how it appears right now. If I go to Terminal or Activity Monitor and fetch the process ID of the running sysex, then Xcode attaches fine with PID. But if I use the name of the sysex, it just keeps on waiting to attach.


Try this…

Tried your suggestion and lldb provides the bundle identifier of the sysex as the candidate process. Copied and used the same to attach in Xcode and it still keeps on waiting to attach. Verified in Terminal (ps -ax | grep ***) as well as in Activity Monitor and they both provide bundle identifier itself as process name. lldb is able to attach with that name but Xcode is not. PID works fine in both cases.


Oh, and btw, if you want to debug from the start of your sysex, add a call to pause at the beginning of main. That’ll pause waiting for you to attach with the debugger (attaching delivers a signal which breaks you out of pause).

Thanks for this awesome suggestion. With this, I am able to get pid of the process in Terminal and use it to attach debugger in Xcode before any code is executed.


lldb is able to attach with that name but Xcode is not.

OK, I’m going to label that as a bug and I encourage you to file it as such. Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Thanks. Here is the feedback reported: FB9108985

Accepting you first reply as answer since it provides a very good solution.

I ran that command and it says

error: '{\rtf1\ansi\ansicpg1252\cocoartf2709' is not a valid command.

error: '\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fnil\fcharset0' is not a valid command.

error: '{\colortbl;\red255\green255\blue255;\red38\green38\blue38;\red239\green239\blue239;}' is not a valid command.

error: '{*\expandedcolortbl;;\cssrgb\c20000\c20000\c20000;\cssrgb\c94902\c94902\c94902;}' is not a valid command.

error: '\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0' is not a valid command.

error: '\deftab720' is not a valid command.

error: '\pard\pardeftab720\partightenfactor0' is not a valid command.

error: '\f0\fs32' is not a valid command.

error: '\outl0\strokewidth0' is not a valid command.

What does these errors mean, how to solve this ?

Which command? Because the error message you posted suggests that you’re trying to run some RTF as an LLDB command O-:

If you’re trying to bring up a Network Extension, check out Debugging a Network Extension Provider.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Not able to attach Xcode debugger to System Extension by name on macOS
 
 
Q