ios - Xcode附加到进程不显示NSLog

标签 ios objective-c xcode apple-watch

我刚刚开始使用 Apple Watch。我从“Five Minute Watchkit”中找到了关于让 iOS 应用程序和 watch kit 应用程序都在模拟器中运行并且两个进程都附加到 LLDB 调试器的说明。

我要做的是启动并退出 iOS 应用程序以在 sim 中安装当前版本。然后我切换到 watchKit 方案并启动它,它会在 watch 模拟器上显示我的 watch 应用程序 UI。

然后我在模拟器中启动相应的 iOS 应用程序,然后用户在 Xcode 菜单中“附加到进程”以将调试器附加到正在运行的 iOS 应用程序。

这行得通。我可以在 watch 套件 InterfaceController 或我的 iOS 应用程序中设置断点,并且调试器会在应该的时候中断。

但是,我没有在我的 iOS 应用程序的调试控制台中看到 NSLog() 语句。 (我确实看到了来自 WatchKit 扩展代码的日志语句。)如果我在我的 iOS 应用程序中设置了一个断点,它会在应该的时候停止在那个断点处。我认为 NSLog 缺少控制台输出与附加到 sim 上正在运行的进程有关,而不是从 Xcode 启动它,但我不知道那是什么。

(顺便说一句,将操作附加到从断点调用 NSLog 的断点也不会显示,但会显示“日志消息”调试器命令。
有人有什么见解吗?)

编辑:
iOS 应用程序中的代码似乎无关紧要。在我的例子中,这是一个非常简单的 IBAction,它附加到 iOS 应用 Storyboard中的一个按钮上:

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

我可以在该 NSLog 语句上设置断点。调试器在该行停止,但我在调试控制台中看不到日志语句。

最佳答案

我可以用一个简单的测试应用程序重现它,没有 WatchKit。该应用程序由一个 NSTimer 组成,它每秒打印“定时器触发”。 (此代码 100% 正确;)。手动附加到进程后,日志中没有显示任何内容。
据我所知,NSLog 输出到 stderr,我猜想附加调试器不会将 stderr 重定向到 Xcode 终端。

如果您可以使用控制台应用程序或终端查看日志,您可以这样做。 iOS8将模拟器日志存储在~/Library/Logs/CoreSimulator/<Device-UUID> .在此目录中,您将找到一个 system.log,其中包含您所有的 NSLog输出。

您可以在终端(catgreptail)中查看它,或者在 Console.app 中打开它。

enter image description here

Apple 在 Technical Note TN2239: iOS Debugging Magic 中确认(至少对于 GDB) .

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'.



提到的 TN2030 在他们的服务器上不再可用( mirror )。它展示了如何将 stdout 和 stderr 重定向到 Xcode 控制台。但是,由于 shell tty不是 LLDB 的有效命令,它不会有太大帮助。但也许有一种不同的方式来访问 tty Xcodes 控制台使用,所以我附上了那个 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 ...]

关于ios - Xcode附加到进程不显示NSLog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30321690/

相关文章:

ios - SpriteKit : SKShapeNodes overlapping

ios - 从 NSUserDefaults 获取值时出错

objective-c - 如何用UIImage填充CGContext?

iphone - 如何仅在 DEBUG 和 AdHoc 模式下执行特定功能

ios - 如何从 iOS 应用程序中的音频文件访问声音样本?

objective-c - 在编写使用 NSDictionary 将变量转换为数字的方法时遇到问题

ios - 如何使用 CoreGrahpics 和 Core 动画绘制文本

objective-c - 是否可以使用 CGContextScaleCTM 创建视网膜图像?

ios - Xcode 错误 "Profile doesn' t include the beta-reports-active entitlement"for adhoc profile

ios - Xcode 5 无法识别 Storyboard中的自定义 UIViewController 子类