Xcode attach to process doesn't display NSLog

21,713

Solution 1

I can reproduce that with a simple test app, sans WatchKit. The app consists of a NSTimer that prints "Timer fired" every second. (This code is 100% correct ;). Nothing shows in the log after I have manually attached to the process.
As far as I know NSLog outputs to stderr, I guess attaching the debugger does not redirect stderr to the Xcode terminal.

If you are okay with using the console app or the terminal to look at your logs you can do that. iOS8 stores simulator logs in ~/Library/Logs/CoreSimulator/<Device-UUID>. In this directory you will find a system.log, which contains all your NSLog output.

You can look at it in terminal (cat, grep, tail), or open it in the Console.app.

enter image description here


Apple confirms that (at least for GDB) in Technical Note TN2239: iOS Debugging Magic.

Console Output

Many programs, and indeed many system frameworks, print debugging messages to stderr. The destination for this output is ultimately controlled by the program: it can redirect stderr to whatever destination it chooses. However, in most cases a program does not redirect stderr, so the output goes to the default destination inherited by the program from its launch environment. This is typically one of the following:

  • If you launch a GUI application as it would be launched by a normal user, the system redirects any messages printed on stderr to the system log. You can view these messages using the techniques described earlier.
  • If you run a program from within Xcode, you can see its stderr output in Xcode's debugger Console window (choose the Console menu item from the Run menu to see this window).

Attaching to a running program (using Xcode's Attach to Process menu, or the attach command in GDB) does not automatically connect the program's stderr to your GDB window. You can do this from within GDB using the trick described in the "Seeing stdout and stderr After Attaching" section of Technical Note TN2030, 'GDB for MacsBug Veterans'.

The mentioned TN2030 is no longer available on their server (mirror). It showed how you can redirect stdout and stderr to the Xcode console. However, since shell tty isn't a valid command for LLDB it won't help much. But maybe there is a different way to access the tty Xcodes console uses, so I attach the important part of that TN.

Seeing stdout and stderr After Attaching

If you attach GDB to a process (as opposed to starting the process from within GDB), you won't be able to see anything that the process prints to stdout or stderr. Programs launched by the Finder typically have stdout and stderr connected to "/dev/console", so the information they print goes to the console. You can view this by launching the Console application (in the Utilities folder), however, it's inconvenient to have to look in a separate window. Another alternative is to connect the process's stdout or stderr to the terminal device for GDB's Terminal window. Listing 9 shows how to do this.

Listing 9. Connecting stdout and stderr to GDB's terminal device.

(gdb) attach 795
[... output omitted ...]
(gdb) call (void) DebugPrintMenuList()
 No output )-:

 Close the stdout and stderr file descriptors.
(gdb) call (void) close(1)
(gdb) call (void) close(2)

 Determine the name of the terminal device for GDB itself.
(gdb) shell tty
/dev/ttyp1

 Reopen stdout and stderr, but connected to GDB's terminal.
 The function results should be 1 and 2; if not, something
 is horribly wrong.
(gdb) call (int) open("/dev/ttyp1", 2, 0)
$1 = 1
(gdb) call (int) open("/dev/ttyp1", 2, 0)
$2 = 2

 Try the DebugPrintMenuList again.
(gdb) call (void) DebugPrintMenuList()
 Yay output!
Index MenuRef     ID  Title
----- ---------- ---- -----
<regular menus>
00001 0x767725D3 -21629 Ed
00002 0x76772627 1128 <Apple>
00003 0x767726CF 1129 File
00004 0x76772567 1130 Edit
[... remaining output omitted ...]

Solution 2

To add onto Filipp Keks answer, here's a visual representation of a much much simpler way to do it than the accepted answer.

From Filipp Keks's answer:

  1. Plug in the device and open Xcode

  2. Choose Window -> Devices from the menu bar

  3. Under the DEVICES section in the left column, choose the device

  4. To see the device console, click the up-triangle at the bottom left of the right hand panel

  5. Click the down arrow on the bottom right to save the console as a file"

This screenshot was taken in Xcode 7.3.1 of the Devices window.

enter image description here

Solution 3

With Xcode Version 7.2 and iOS 9.2 etc, I found the following works:

  1. Kill both the phone app and watch apps
  2. Select the Watch Extension Target and hit Cmd+R (build and run)
  3. Select the Phone target and hit Ctrl+Cmd+R (Run without building)

In my case, I have both apps up in their simulators and get NSLog output for both. I don't need to attach separately. Hope this helps.

Solution 4

https://developer.apple.com/library/ios/qa/qa1747/_index.html

  1. Plug in the device and open Xcode

  2. Choose Window -> Devices from the menu bar

  3. Under the DEVICES section in the left column, choose the device

  4. To see the device console, click the up-triangle at the bottom left of the right hand panel

  5. Click the down arrow on the bottom right to save the console as a file

Share:
21,713
Duncan C
Author by

Duncan C

I am a tech lead at Capital One in Tysons, Virginia, managing cross-platform iOS and Android development. I am also a personnel manager. I am available on CodeMentor.com if you need help with iOS development: Enter the following link: https://www.codementor.io/duncanc

Updated on July 09, 2022

Comments

  • Duncan C
    Duncan C almost 2 years

    I'm just getting started with Apple Watch. I found instructions from "Five Minute Watchkit", on getting the iOS app and the watch kit app both running in the simulator and both processes attached to the LLDB debugger.

    What I do is launch and quit the iOS app to install a current version in the sim. Then I switch to the watchKit scheme and launch that, which displays my watch app UI on the watch simulator.

    I then launch the corresponding iOS app in the simulator, then user "attach to process" in the Xcode menu to attach the debugger to the running iOS app.

    This works. I can set breakpoints in either the watch kit InterfaceController or in my iOS app and the debugger breaks there when it should.

    However, I'm not seeing NSLog() statements in the debug console from my iOS app. (I do see log statements from the WatchKit extension code.) If I set a breakpoint in my iOS app, it does stop at that breakpoint when it should. I assume the lack of console output from NSLog has SOMETHING to do with attaching to a running process on the sim rather than launching it from Xcode, but I don't know what that something is.

    (BTW, attaching an action to a breakpoint that invokes NSLog from the breakpoint also doesn't display, but the "log message" debugger command DOES display. Does anybody have any insights?)

    EDIT: The code in the iOS app doesn't seem to matter. In my case, it was a dirt simple IBAction that was attached to a button in the iOS app storyboard:

    - (IBAction)buttonAction:(UIButton *)sender;
    {
      NSLog(@"Button clicked on iPhone");
    }
    

    I can set a breakpoint on that NSLog statement. The debugger stops at that line, but I don't see the log statement in the debug console.