我正在学习 JDB 并遇到了一些悖论。启动 JDB(使用“jdb ClassName”)后,大多数教程会告诉我输入
>方法类名
查看可用方法的列表,以便我可以设置断点。如果我这样做,JDB 会回复
在使用“run”命令启动虚拟机之前,命令“methods”无效
当然,如果我在设置任何断点之前说“run”,它会直接运行;不是很有帮助。我唯一能得出的结论是 jdb 希望您盲目地设置断点,但这似乎是一个严重的疏忽,我坚持认为我只是错过了一个命令。
非常感谢!
乔伊斯
I'm learning JDB and running into a bit of a paradox. After starting JDB (with "jdb ClassName") most tutorials will tell me to type
> methods ClassName
to see a list of available methods so that I can set my breakpoints. If I do that, JDB replies
Command 'methods' is not valid until the VM is started with the 'run' command
Of course, if I say "run" before setting any breakpoints, it runs straight through; not very helpful. The only thing I can conclude is that jdb expects you to set your breakpoints blind, but this seems like such a gross oversight, I'm holding out that I'm simply missing a command.
Many thanks!!
Joyce
发布评论
评论(3)
请注意,有两种方法可以创建调试会话(请参阅jdb 文档)。
java -Xdebug -agentlib:jdwp=transport=dt_socket,
地址=8000,服务器=y,挂起=y类名
jdb -attach 8000
jdb ClassName
如果您要附加,则无需使用
run
命令。但是,如果您启动,那么您确实需要使用
run
命令(虚拟机尚未启动)。此行为可以从
man jdb
推断出来:这就是您收到错误消息的原因。您启动了调试器,但没有使用
运行命令
。有些教程可能会错误地告诉您启动 jdb,但忘记告诉您执行
run
命令。下面展示了如何获取方法列表(假设您在名为
ClassName
的类中有main
方法)。正在附加:
jdb -attach 8000
main[1]停在ClassName.main
主要[1]续
main[1] 方法 ClassName
启动:
jdb 类名
<代码>>停在ClassName.main
<代码>>运行
main[1] 方法 ClassName
提示:查看jdb的命令提示符。有时它是
>
,有时它就像main[1]
。如果是>
,则虚拟机尚未启动,并且classes
、methods
等命令将无法工作,直到您使用 <代码>运行命令。如果提示符为main[1]
,则虚拟机已启动并且所需的命令将起作用。单独使用调试器很难设置断点。您需要在其他地方查看源代码。您可能会知道至少一种要中断的方法的名称,从而使用以下命令设置初始断点
停在ClassName.MethodName
。如果您不知道在哪里断点,可以随时使用stop in ClassName.Main
在 main 方法上设置断点。请记住,当调试器运行时,您可以设置更多断点。此外,您可能会发现
list
命令很有用 - 它显示与当前断点命中相对应的源代码。Note that there are two ways to create a debugging session (see the jdb documentation).
java -Xdebug -agentlib:jdwp=transport=dt_socket,
address=8000,server=y,suspend=y ClassName
jdb -attach 8000
jdb ClassName
If you are attaching, then you don't need to use the
run
command.However, if you are launching, then you do need to use the
run
command (the virtual machine hasn't been started yet).This behaviour can be inferred from
man jdb
:This is why you have the error message. You launched the debugger but didn't use the
run command
.Some tutorials may incorrectly tell you to launch jdb, but forget to tell you to execute the
run
command.Below shows how to get the list of methods (assuming you have
main
method in a class calledClassName
).Attaching:
jdb -attach 8000
main[1] stop in ClassName.main
main[1] cont
main[1] methods ClassName
Launching:
jdb ClassName
> stop in ClassName.main
> run
main[1] methods ClassName
Hint: look at jdb's command prompt. Sometimes it's
>
, sometimes it's likemain[1]
. If it's>
, then the VM hasn't started and commands such asclasses
,methods
won't work until you have used therun
command. If the prompt ismain[1]
, the VM has been started and desired commands will work.It is difficult to set breakboints using the debugger alone. You need to be looking at your source code elsewhere. You will likely know the name of at least one method to break at and thus set an initial breakpoint using
stop in ClassName.MethodName
. If you don't know where to break, you can always set a breakpoint on your main method usingstop in ClassName.Main
.Remember that while the debugger is running, you can set more breakpoints. Also, you may find, the
list
command useful - it shows the source code corresponding to the current breakpoint hit.我也有同样的问题。
因此,运行后:
我输入的文本在
jdb
会话中以粗体显示:I had exactly the same question.
So after running:
the text I entered is in bold below in the
jdb
session:如果您正在调试的是您自己的程序,我想您会知道类名!
如果您没有源代码,那么要运行它,您必须知道包含
main()
的类名。如果它位于以java -jar
开头的 jar 中,则该类的名称位于 jar 内的清单中。但实际上您正在运行
jdb ClassName
,因此您知道您将运行方法ClassName.main()
。正确的?如果它是 Web 服务中的 Servlet,则该 Servlet 的类位于
web.xml
中。因此,在任何这些情况下,您至少应该能够获得第一个方法。一旦到达那里,您就可以找到其余的内容。
If it's your own program you're debugging, I'd think you'd know the class names!
If it's a program for which you don't have the source code, then to run it you must know the class name containing
main()
. If it's in a jar started withjava -jar
, the name of that class is in the manifest inside the jar.But in fact you're running
jdb ClassName
, so you know you'll be running methodClassName.main()
. Right?If it's a servlet in a web service, the class of the servlet is in
web.xml
.So in any of those cases you should at least be able to get the very first method. Once there, you can find the rest.