原文地址: 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;}}

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 a 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

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

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 a 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的更多相关文章

  1. jdb

    http://herongyang.com/jtool/jdb.html http://www.rhcedan.com/2010/06/22/killing-a-java-thread/ 用处:上去杀 ...

  2. 使用JDB调试Java程序

    Java程序中有逻辑错误,就需要使用JDB来进行调试了.调试程序在IDE中很方便了,比如这篇博客介绍了在Intellj IDEA中调试Java程序的方法. 我们课程内容推荐在Linux环境下学习,有同 ...

  3. 014-通过JDB调试,通过HSDB来查看HotSpot VM的运行时数据

    一.JDB调试        在预发环境下进行debug时,时常因为工具和环境的限制,导致debug体验非常差,那么有什么方法能够简化我们进行debug的体验吗?JDB就是一种.        JDB ...

  4. jmeter sampler maven项目排错记

    eclipse 创建的maven项目,引入jar包之后出现红色叹号,一直找不到原因,连main方法都无法运行,提示找不到类: 错误: 找不到或无法加载主类 soapsampler.SoapSample ...

  5. 远程debug调试java代码

    远程debug调试java代码 日常环境和预发环境遇到问题时,可以用远程调试的方法本地打断点,在本地调试.生产环境由于网络隔离和系统稳定性考虑,不能进行远程代码调试. 整体过程是通过修改远程服务JAV ...

  6. ndk-gdb of NDK r9d modified to *always* debug the ":remote"-process of your app

    https://gist.github.com/TomTasche/9690186 ndk-gdb of NDK r9d modified to *always* debug the ":r ...

  7. Java Tomcat Glassfish Weblogic远程debug(remote debug)

    tomcat ./catalina.sh jpda start 这条命令启动tomcat,它就会监听8000端口,等待调试器的连接. 默认监听8000端口,通过设置环境变量JPDA_ADDRESS指定 ...

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

    JDK内置工具使用 一.javah命令(C Header and Stub File Generator) 二.jps命令(Java Virtual Machine Process Status To ...

  9. Android Debug Bridge

    Android Debug Bridge Introduction     Android Debug Bridge (adb) is a versatile command line tool th ...

随机推荐

  1. ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

    这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...

  2. Android 超仿Path时间轴和扇形菜单的效果

    网上看到的  ,仅此记录一下,用到的时候便于查找 效果如下: 源码下载地址 :  http://download.csdn.net/detail/abc13939746593/7251933

  3. 如何在Android开发中让你的代码更有效率

    最近看了Google IO 2012年的一个视频,名字叫做Doing More With Less: Being a Good Android Citizen,主要是讲如何用少少的几句代码来改善And ...

  4. Android中FragmentPagerAdapter对Fragment的缓存(二)

    上一篇我们谈到了,当应用程序恢复时,由于FragmentPagerAdapter对Fragment进行了缓存的读取,导致其并未使用在Activity中新创建的Fragment实例.今天我们来看如何解决 ...

  5. eclipse设置自定义快捷键

    eclipse有很多强大且人性化的功能,而各项功能有时又隐藏得比较深(需要点击数次菜单才能找到),而系统提供的快捷键有时比较难记住甚至根本没有提供快捷键时,就需要自己手动设置快捷键了.设置方法有两种, ...

  6. WebForm+Web.config: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。

    ylbtech-Error-WebForm+Web.config: 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 1.A,错误代码 ...

  7. 《C++ primer》--第9章

    习题9.2 创建和初始化一个vector对象有4种方式,为每种方式提供一个例子. 解答: 分配指定数目的元素,并对这些元素进行值初始化: vector<int> ivec(10);     ...

  8. 在Ubuntu下安装Apache

    在Ubuntu下安装软件其实非常方便,Ubuntu提供了apt-get工具,可以使用该工具直接下载安装软件. 在Linux里,系统最高权限账户为root账户,而默认登录的账户并非root账户,例如不具 ...

  9. IOS 本地通知UILocalNotification

    //发送通知    UILocalNotification *notification=[[UILocalNotification alloc] init];       if (notificati ...

  10. xcode 怎么样在发布release版本的时候 不输出log

    我们平时在开发应用的时候,经常会用到 NSLog 来调试我们的程序,而随着项目越来越大,这些用于调试的日志输出就会变得很难管理. 发布正式版的时候一定要屏蔽掉所有后台输出,因为这些输出还是比较消耗系统 ...