Debug with jdb

原文地址: http://www.javaworld.com/article/2077445/testing-debugging/debug-with-jdb.html

Q: How do you use jdb (included in the JDK 1.2 package)
effectively to debug Java programs?

I‘ve tried many times, but I am successful only in loading a class file
to jdb; I can‘t debug it.
The help command isn‘t much use.

A: You ask an interesting question. To be honest,
I‘ve neverused jdb. I have always used the
debugger provided by my IDE environment. So to answer your question I had to do
a little research of my own.

It turns out that Sun considers jdb a proof of concept
for the Java Debugger API. The Java Debugger API allows us to actually peek into
the runtime and debug our code. The jdb is just one
implementation of a debugger that uses the API. Compared to the visual debuggers
with which I‘m familiar (yes, I guess I‘m a wimp), it‘s not the easiest debugger
to use -- though it is similar to other command-line debuggers, such
asgdb.

Anyhow, on to your question. Before attempting to debug your code, be sure to
use the -g option while compiling your classes. This
option tells the compiler to include debugging information in your class
file.

Let‘s define a contrived class for testing:

publicclassTestMe{privateint int_value;privateString string_value;publicstaticvoid main(String[] args){TestMe testMe =newTestMe();
testMe.setInt_value(1);
testMe.setString_value("test");int integer = testMe.getInt_value();Stringstring= testMe.getString_value();String toString = testMe.toString();}publicTestMe(){}publicint getInt_value(){return int_value;}publicString getString_value(){return string_value;}publicvoid setInt_value(int value){
int_value = value;}publicvoid setString_value(String value){
string_value = value;}publicString toString(){return"String value: "+ string_value +" int value: "+ int_value;}}

POPULAR
RESOURCES

SEE ALL

Go

Start the debugger:

> jdb TestMe

You should see:

>Initializing jdb...>0xaa:class

Let‘s take a look at some basic commands. In order to set breakpoints, we
need to know the line numbers or the method names of the places where we would
like to break. To obtain a list of methods, simply use
the methods command:

> methods TestMevoid main(java.lang.String[])void()int getInt_value()
java.lang.String getString_value()void setInt_value(int)void setString_value(java.lang.String)
java.lang.String toString()

Setting a breakpoint is simple. Use the following syntax:

stop in.[<argument_type,...>]

Or:

stop at :

We should start debugging at the beginning of the main method:

> stop inTestMe.main
Breakpointsetin javaworld.TestMe.main

Now that we have a breakpoint, we can begin execution. To run up to the
breakpoint, simply use the run command:

> run
run javaworld.TestMe
running ...
main[1]Breakpoint hit: javaworld.TestMe.main (TestMe:10)

At this point, the debugger halts execution at the first line of the main
method. Notice that the cursor has changed to reflect the method that we are
currently in.

The list command will display the code at the
breakpoint. An arrow indicates the spot where the debugger has halted
execution.

main[1] list
6privateString string_value;78publicstaticvoid main(String[] args)9{10=>TestMe testMe =newTestMe();11 testMe.setInt_value(1);12 testMe.setString_value("test");1314int integer = testMe.getInt_value();
main[1]

Next, we‘ll want to step through a few lines of code
and see what‘s changed:

main[1] step
main[1]Breakpoint hit: javaworld.TestMe.(TestMe:20)
main[1] locals
Method arguments:Local variables:this=String value:nullint value:0
main[1] list
1617String toString = testMe.toString();18}1920=>publicTestMe()21{22}2324publicint getInt_value()
main[1] step
main[1]Breakpoint hit: java.lang.Object.(Object:27)
main[1] list
Unable to find Object.java
main[1] step
main[1]Breakpoint hit: javaworld.TestMe.(TestMe:22)
main[1] list
18}1920publicTestMe()21{22=>}2324publicint getInt_value()25{26return int_value;
main[1] step
main[1]Breakpoint hit: javaworld.TestMe.main (TestMe:10)
main[1] list
6privateString string_value;78publicstaticvoid main(String[] args)9{10=>TestMe testMe =newTestMe();11 testMe.setInt_value(1);12 testMe.setString_value("test");1314int integer = testMe.getInt_value();
main[1] step
main[1]Breakpoint hit: javaworld.TestMe.main (TestMe:11)
main[1] list
78publicstaticvoid main(String[] args)9{10TestMe testMe =newTestMe();11=> testMe.setInt_value(1);12 testMe.setString_value("test");1314int integer = testMe.getInt_value();15Stringstring= testMe.getString_value();
main[1] locals
Method arguments:Local variables:
args =
testMe =String value:nullint value:0

After each step, I called
the list command to see where I was in the code. The
return value from the command listed the line number, but somehow that didn‘t
really help me very much.

As we step, we see that the main method is constructing
TestMe instance. Each step takes us through the
constructor and finally back into the main method.
The localscommand lists all of the local variables visible in
the current stack. We see that at this point in the main method there are only
two local
variables: args and testMe.

By using step, we can get inside any of the methods to see
what is going on. When we combine step with
the locals command we can see our variables:

main[1] step
main[1]Breakpoint hit: javaworld.TestMe.setInt_value (TestMe:36)
main[1] list
32}3334publicvoid setInt_value(int value)35{36=> int_value = value;37}3839publicvoid setString_value(String value)40{
main[1] locals
Method arguments:Local variables:
value =1this=String value:nullint value:0

YOU MIGHT ALSO LIKE

If we step one more time, we end up in
the setInt_value()method. If
we step two more times, the method will set
the int_value member to 1 and
return. (To check to see that the method set the value, use
the locals command.)

Of course, when we step, we won‘t always want to trace into
each method we encounter. Some method calls can nest very deeply. If we were
forced to trace through an entire hierarchy, we might never finish.
Luckily, jdb has a way to execute a
method without tracing into that method:
the next command.

jdb also provides a few
other step commands.
The stepi command executes the current instruction. In
other words, the code at the => will execute but the
current line will not advance to the next instruction. You can
call stepi a million times, but
the => displayed from
the list command will not move.

jdb also provides the step
up
 command. The step up call executes until
the current method returns to its caller. Simply put, this stepper executes a
method and nothing else. Take the following code segment as an example:

int integer = testMe.getInt_value();

If this is our current line and we run step up,
the getInt_value() method will execute. However, that‘s
all that will happen. The return value will not get set
to integer.

jdb also allows us to set multiple breakpoints. To go from
one breakpoint directly to the next, jdb provides
the cont command.

Finally, there are times when we want to look at all the members of an
instance or class. Luckily, jdb provides
the dump and print commands:

main[1]dumpTestMeTestMe=0xa9:class(javaworld.TestMe){
superclass =0x2:class(java.lang.Object)
loader =(sun.misc.Launcher$AppClassLoader)0xaa}
main[1]printTestMeTestMe=0xa9:class(javaworld.TestMe)
main[1]dump testMe
testMe =(javaworld.TestMe)0xec{private java.lang.String string_value = test
privateint int_value =1}
main[1]print testMe
testMe =String value: test int value:1

FEATURED RESOURCE

Presented by Dell Software

10
key findings illustrate the need for a new breed of APM solutions

This paper highlights ten key takeaways from the most recent survey on the
impact of Cloud on

LEARN
MORE

When you run dump or print on a
class, you get class information, which includes superclass and loader
information. When you
run dump and print on an instance,
you get instance information, such as data members and their current values.

jdb also provides commands for getting down and dirty in
the threads and stacks. However, these commands are really beyond the scope of
jdb intro.

One final point: you may ask, "How do you effectively usejdb?"
The effectiveness of use will depend on your comfort level
with jdb. When you first use jdb, the most
important command is help.
The help command lists each command and provides some
basic information to help you get started. Once you have
the help command mastered, you‘ll find yourself using the
commands that set breakpoints, along
with step and list. Any combination of
those commands will allow you to get started
using jdbstepliststeplist...
should help you quickly locate code that is bombing out on you.

Learn more about this topic


Debug with jdb,码迷,mamicode.com

时间: 2024-10-13 21:44:32

Debug with jdb的相关文章

jdb

http://herongyang.com/jtool/jdb.html http://www.rhcedan.com/2010/06/22/killing-a-java-thread/ 用处:上去杀死一个线程,! "jdb" Command "jdb": A command line tool that allows you to debug a Java application interactively with in a command line mode.

Android jdb debug

在ubuntu上android的debug的方式有很多种,可以用eclipse,android studio.但是有的时候,为了方便也可以用命令行去debug.下面列出命令行debug的过程 xxx:~$ cd source/xxx:~/source$ cd packages/apps/Settings/xx:~/source/packages/apps/Settings$ adb shell ps | grep com.android.settingssystem 3119 202 55043

Java自带的性能监测工具用法简介——jstack、jconsole、jinfo、jmap、jdb、jsta、jvisualvm

JDK内置工具使用 一.javah命令(C Header and Stub File Generator) 二.jps命令(Java Virtual Machine Process Status Tool) 三.jstack命令(Java Stack Trace) 四.jstat命令(Java Virtual Machine Statistics Monitoring Tool) 五.jmap命令(Java Memory Map) 六.jinfo命令(Java Configuration Inf

java调试工具jdb

Finds and fixes bugs in Java platform programs. Synopsis jdb [options] [classname] [arguments] options Command-line options. See Options. classname Name of the main class to debug. arguments Arguments passed to the main() method of the class. Descrip

远程debug调试java代码

远程debug调试java代码 日常环境和预发环境遇到问题时,可以用远程调试的方法本地打断点,在本地调试.生产环境由于网络隔离和系统稳定性考虑,不能进行远程代码调试. 整体过程是通过修改远程服务JAVA_OPTS参数,然后本地通过Eclipse或IDEA等工具调试. 下面简单介绍下理论. 理论 JPDA(Java Platform Debugger Architecture)是Java平台调试体系结构的缩写.由3个规范组成,分别是JVMTI(JVM Tool Interface),JDWP(Ja

百度加固后检测反jdb调试apk的小记录

百度加固后的apk检测反jdb调试apk的简单分析,就一个很简单的记录而已,方便以后查阅. 按照正常IDA调试,在开始用jdb来连接,出错了 毕竟是加壳的东西,所以反编译看一下,竟然找到了一点内容,如下: static { if(!Debug.isDebuggerConnected()) { String v0 = Build.CPU_ABI; if(v0 != null && (v0.startsWith("x86"))) { StubApplication.load

Android Debug

我们今天将讨论的是8大你不得不知的Android调试工具,这些工具部分属于系统自带,也是一种方式方法,希望对大家有所帮助. ? ? 1. 查看当前堆栈 1) 功能:在程序中加入代码,使可以在logcat中看到打印出的当前函数调用关系 2) 方法:?new Exception("print trace").printStackTrace(); 2. MethodTracing 1) 功能:用于热点分析和性能优化,分析每个函数占用的CPU时间,调用次数,函数调用关系等 2) 方法: a)

使用jdb调试apk

jdb是一个支持java代码级调试的工具,它是由java jdk提供的,存在于xxx\Java\jdk1.6.0_21\bin之下 使用ddms调试时,主机会打开另外一个网络端口,在DDMS里查看,一般是8700. 启动DDMS,这时程序前面应该有个红色小虫,点上面的开始调试按钮.这步不是必须的,这步的工作其实相当于手动敲: $ adb -d forward tcp:8700 jdwp:$PID 其中$PID为要调程序的进程号. 通过attach方式进行调试步骤: 显示所有可供调试的用户进程:

idea下远程debug配置

一. 背景: 在测试工作中,为方便发现代码中的逻辑问题,尝试使用远程debug模式,在测试过程中走查代码,不仅可以辅助测试减少与开发的沟通成本,更便于了解业务提升测试深度. 二. 配置方式: 1. 调试的配置方式主要为设置JVM的参数,使之工作在debug模式下,常用参数为: -Xdebug -Xrunjdwp:transport=dt_socket,address=8012,server=y,suspend=n 2. 服务器端配置: 若项目为web项目,可在tomcat的启动程序如catali