Search code examples
objective-cswiftxcodelldbxcode11

Force write to Xcode 'Debugger Output' in console?


The Xcode console has a 'Debugger output' filter. I understand this is for use with lldb, and that you can get messages to print to this output by using breakpoints. My question is not how to do that.

My question is: what is the underlying mechanism Xcode itself uses to write lldb messages to Debugger Output (not Target Output)? Is there a variable similar to stdout or stderr that writes here? Is it possible, from Xcode target code (Swift/Obj-C/C), to write to this output?


Solution

  • Looks like Xcode uses a tty to communicate with lldb, and you can interface with the Debugger Output using that:

    echo "Wheeeeeeee" > $(lsof -p $(ps -A | grep -m1 MacOS/Xcode | awk '{print $1}') | grep -m2 dev/ttys | tail -1 | awk '{print $9}')

    Breaking the above down:

    $ ps -A | grep -m1 MacOS/Xcode | awk '{print $1}' 
    21280
    

    This gives the process ID of Xcode (21280). Using this, we can find the files it has open:

    $ lsof -p 21280 | grep /dev/ttys
    Xcode   21280 tres   47u      CHR               16,3       0t0                3569 /dev/ttys003
    Xcode   21280 tres   58u      CHR               16,5       0t0                3575 /dev/ttys005
    

    The one with the highest number (/dev/ttys005 in this case) is the one we want, so let's extract it. tail -1 will give us the last line of output, and awk '{print $9}' will give us the 9th item on the line, which is what we want!

    $ lsof -p 21280 | grep /dev/ttys | tail -1 | awk '{print $9}'
    /dev/ttys005
    

    Now we can use this to write whatever we want:

    enter image description here