Android 使用 waitForDebugger 与 jdb 混淆
我想使用命令行在我的设备(Nexus One - 不是模拟器)上调试我的 Android 应用程序。
我对如何使用 jdb 与 android.os.Debug.waitForDebugger 结合使用设置断点感到困惑。
假设我将以下代码放入我的主要活动 onCreate
中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.os.Debug.waitForDebugger();
int j = 10;
int r = j;
}
使用 ddms
我可以看到我的应用程序在启动时正在等待调试器(红色错误图标)。
但是,我不明白如何在 waitForDebugger()
调用之后设置断点,以便我可以开始单步执行。
显然,只需附加 jdb 就会立即继续运行应用程序而不会停止。
例如,
jdb -attach localhost:8700
有没有办法在运行jdb
之前预设断点,或者启动jdb
设置断点然后附加?
I'd like to debug my Android application on my device (Nexus One - not the emulator) using the command line.
I'm confused at how to set a breakpoint using jdb in combination with android.os.Debug.waitForDebugger
.
Say I put the following code in my main activity onCreate
:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.os.Debug.waitForDebugger();
int j = 10;
int r = j;
}
Using ddms
I can see that my application is waiting for the debugger (red bug icon) when I start it.
However, I don't understand how to set a breakpoint after the waitForDebugger()
call so that I can start stepping.
Obviously just attaching jdb
is going to immediately continue running the app without stopping.
e.g.
jdb -attach localhost:8700
Is there way to preset breakpoints prior to running jdb
or a way to start jdb
set breakpoints and then attach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jdb 在 Linux 中有一个配置文件:~/.jdrbc。例如,在我的设备中,我有:
在您的设备模拟器开发设置中,您可以为相关应用程序类设置“等待调试器”。运行应用程序,启动调试器,它就会中断。
jdb has a config file : ~/.jdrbc in Linux. e.g in mine I have:
The in your device emulator dev settings you can set "wait for debugger" for the application class in question. Run the application, start the debugger and it will break.
不幸的是,虚拟机无法告诉调试器它已经挂起。如果初始状态不是“正在运行”,我尝试过的各种调试器都会感到困惑。因此,在调试器连接后,VM 必须恢复所有线程。 (上述情况的唯一例外是虚拟机刚刚启动,在这种情况下,它可以发送一条特殊的“我刚刚启动”消息,指示它是要开始运行还是保持挂起状态。这在这里没有帮助。
) waitForDebugger() 所做的就是等待调试器的初始突发活动安静下来。调试器连接后,该方法将休眠,直到 1.5 秒内调试器没有任何活动。这使得调试器有机会在虚拟机恢复之前设置任何断点。
对于 Eclipse,这个 hack 效果非常好,因为您可以在附加之前配置断点。对于jdb,我无法在启动后告诉它附加,所以你必须快速敲击键盘或使用某种配置文件(我也没有看到)。
您可以用不同的方式解决这个问题:在 waitForDebugger 调用下方,添加一个循环,例如:
然后,在按照您想要的方式配置 jdb 之后,使用类似的内容:
如果您速度很快,您可以跳过循环,只使用 Thread .sleep(5000) 给自己一点额外的时间来设置断点。
Unfortunately, there is no way for the VM to tell the debugger that it's already suspended. The various debuggers I've tried get confused if the initial state is anything other than "running". As a result, after the debugger connects, the VM has to resume all threads. (The only exception to the above is if the VM was just started, in which case it can send a special "I just started" message indicating whether it's going to come up running or stay suspended. This doesn't help here.)
What waitForDebugger() does do is wait until the initial burst of activity from the debugger quiets down. After the debugger connects, the method will sleep until there has been no activity from the debugger for 1.5 seconds. This allows the debugger a chance to set any breakpoints before the VM resumes.
For Eclipse, this hack works out pretty well, because you can configure your breakpoints before you attach. For jdb, there's no way I can see to tell it to attach after it has started up, so you have to be quick on the keyboard or use some sort of config file (which I also don't see).
You could solve this a different way: below the waitForDebugger call, add a loop like:
Then, after you've got jdb configured the way you want, use something like:
If you're quick, you could skip the loop and just Thread.sleep(5000) to give yourself a little extra time to slam breakpoints in.
我尝试过两种解决方案(来自 fadden 的解决方案和来自 Richard Riley 的解决方案),但都不起作用。一旦 jdb 连接到调试端口(通过 Dalvik 为 8700),在模拟器中启动的应用程序就会解锁,但随后它会自行运行,根本不与 jdb 同步。引入等待循环不会改变任何事情,并且 .jdbrc 中的断点命令不起作用。 jdb 的答案总是“推迟断点等。它将在类加载后设置。”,因为应用程序没有运行;但是,如果您输入“运行”,答案是“没有任何暂停。”,因为应用程序已经在运行......
I've tried with both solutions (the one from fadden and the one from Richard Riley), but none of them worked. The app launched in the emulator get unblocked as soon as a jdb is attached to the debug port (8700 via Dalvik) but then it runs on its own, without synchronizing with jdb at all. Introducing a waiting loop does not change things, and breakpoint commands in .jdbrc have no effect. Invariably, the jdb answer is "Deferring breakpoint etc. etc. It will be set after the class is loaded.", as the app was not running; but, if you type "run" the answer is "Nothing suspended.", as the app was already running...