Xcode 附加到进程不显示 NSLog
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29106019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode attach to process doesn't display NSLog
提问by Duncan C
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.
我刚刚开始使用 Apple Watch。我从“五分钟手表套件”中找到了有关让 iOS 应用程序和手表套件应用程序都在模拟器中运行并且两个进程都连接到 LLDB 调试器的说明。
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.
我所做的是启动并退出 iOS 应用程序以在 sim 中安装当前版本。然后我切换到 watchKit 方案并启动它,它会在手表模拟器上显示我的手表应用程序 UI。
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.
然后我在模拟器中启动相应的 iOS 应用程序,然后用户在 Xcode 菜单中“附加到进程”将调试器附加到正在运行的 iOS 应用程序。
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.
这有效。我可以在 watch kit InterfaceController 或我的 iOS 应用程序中设置断点,并且调试器会在应该时中断。
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.
但是,我在 iOS 应用程序的调试控制台中没有看到 NSLog() 语句。(我确实看到了 WatchKit 扩展代码中的日志语句。)如果我在我的 iOS 应用程序中设置了一个断点,它确实会在该断点处停止。我认为缺少 NSLog 的控制台输出与附加到 sim 上正在运行的进程有关,而不是从 Xcode 启动它,但我不知道那是什么。
(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?)
(顺便说一句,将操作附加到从断点调用 NSLog 的断点也不会显示,但“日志消息”调试器命令会显示。有人有任何见解吗?)
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:
编辑:iOS 应用程序中的代码似乎无关紧要。就我而言,它是一个简单的 IBAction,它附加到 iOS 应用程序故事板中的一个按钮上:
- (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.
我可以在那个 NSLog 语句上设置一个断点。调试器在该行停止,但我在调试控制台中没有看到日志语句。
采纳答案by Matthias Bauch
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.
我可以用一个简单的测试应用程序来重现它,sans WatchKit。该应用程序由一个 NSTimer 组成,它每秒打印一次“定时器已触发”。(此代码 100% 正确;)。在我手动附加到进程后,日志中没有任何显示。
据我所知 NSLog 输出到 stderr,我猜附加调试器不会将 stderr 重定向到 Xcode 终端。
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.
如果您可以使用控制台应用程序或终端查看日志,则可以这样做。iOS8 将模拟器日志存储在~/Library/Logs/CoreSimulator/<Device-UUID>
. 在此目录中,您将找到一个 system.log,其中包含您的所有NSLog
输出。
You can look at it in terminal (cat
, grep
, tail
), or open it in the Console.app.
您可以在终端 ( cat
, grep
, tail
) 中查看它,或在 Console.app 中打开它。
Apple confirms that (at least for GDB) in Technical Note TN2239: iOS Debugging Magic.
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'.
控制台输出
许多程序,甚至许多系统框架,都将调试消息打印到 stderr。此输出的目的地最终由程序控制:它可以将 stderr 重定向到它选择的任何目的地。但是,在大多数情况下,程序不会重定向 stderr,因此输出会转到程序从其启动环境继承的默认目标。这通常是以下情况之一:
- 如果您像普通用户一样启动 GUI 应用程序,系统会将打印在 stderr 上的任何消息重定向到系统日志。您可以使用前面描述的技术查看这些消息。
- 如果您从 Xcode 中运行程序,您可以在 Xcode 的调试器控制台窗口中看到它的 stderr 输出(从运行菜单中选择控制台菜单项以查看此窗口)。
附加到正在运行的程序(使用 Xcode 的附加到进程菜单,或 GDB 中的附加命令)不会自动将程序的 stderr 连接到 GDB 窗口。您可以使用技术说明 TN2030,'GDB for MacsBug Veterans' 的“Seeing stdout and stderr After Attaching”部分中描述的技巧在 GDB 中执行此操作。
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.
提到的 TN2030 在他们的服务器(镜像)上不再可用。它展示了如何将 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 (gdb) call (int) open("/dev/ttyp1", 2, 0) = 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 ...]
附加后看到 stdout 和 stderr
如果您将 GDB 附加到一个进程(而不是从 GDB 内启动该进程),您将看不到该进程打印到 stdout 或 stderr 的任何内容。Finder 启动的程序通常将 stdout 和 stderr 连接到“/dev/console”,因此它们打印的信息会进入控制台。您可以通过启动 Console 应用程序(在 Utilities 文件夹中)来查看它,但是,必须在单独的窗口中查看很不方便。另一种选择是将进程的 stdout 或 stderr 连接到 GDB 的终端窗口的终端设备。清单 9 展示了如何做到这一点。
清单 9. 将 stdout 和 stderr 连接到 GDB 的终端设备。
(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 (gdb) call (int) open("/dev/ttyp1", 2, 0) = 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 ...]
回答by matt.writes.code
To add onto Filipp Keks answer, here's a visual representation of a much much simpler way to do it than the accepted answer.
要添加到 Filipp Keks 的答案中,这里有一个比公认的答案简单得多的方法的视觉表示。
From Filipp Keks's answer:
来自 Filipp Keks 的回答:
Plug in the device and open Xcode
Choose Window -> Devices from the menu bar
Under the DEVICES section in the left column, choose the device
To see the device console, click the up-triangle at the bottom left of the right hand panel
Click the down arrow on the bottom right to save the console as a file"
插入设备并打开Xcode
从菜单栏中选择窗口 -> 设备
在左列的设备部分下,选择设备
要查看设备控制台,请单击右侧面板左下角的上三角形
单击右下角的向下箭头将控制台另存为文件”
This screenshot was taken in Xcode 7.3.1 of the Devices window.
此屏幕截图是在“设备”窗口的 Xcode 7.3.1 中截取的。
回答by Nik Bhatt
With Xcode Version 7.2 and iOS 9.2 etc, I found the following works:
使用 Xcode 7.2 版和 iOS 9.2 等,我发现以下作品:
- Kill both the phone app and watch apps
- Select the Watch Extension Target and hit Cmd+R(build and run)
- Select the Phone target and hit Ctrl+Cmd+R(Run without building)
- 杀死电话应用程序和手表应用程序
- 选择 Watch Extension Target 并点击Cmd+ R(构建并运行)
- 选择 Phone 目标并点击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.
就我而言,我在他们的模拟器中安装了这两个应用程序,并获得了两者的 NSLog 输出。我不需要单独附加。希望这可以帮助。
回答by Filipp Keks
https://developer.apple.com/library/ios/qa/qa1747/_index.html
https://developer.apple.com/library/ios/qa/qa1747/_index.html
Plug in the device and open Xcode
Choose Window -> Devices from the menu bar
Under the DEVICES section in the left column, choose the device
To see the device console, click the up-triangle at the bottom left of the right hand panel
Click the down arrow on the bottom right to save the console as a file
插入设备并打开Xcode
从菜单栏中选择窗口 -> 设备
在左列的设备部分下,选择设备
要查看设备控制台,请单击右侧面板左下角的上三角形
单击右下角的向下箭头将控制台另存为文件
回答by Spydy
When your Provisioning profile is set to AdHoc or Distribution then Xcode doesnot show log, you need set development to view log
当您的 Provisioning 配置文件设置为 AdHoc 或 Distribution 时,Xcode 不显示日志,您需要设置 development 才能查看日志