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.

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

目录
相关文章
|
机器学习/深度学习 数据采集 人工智能
探索AI技术在文本生成中的应用与挑战
【9月更文挑战第26天】本文深入探讨了AI技术在文本生成领域的应用,并分析了其面临的挑战。通过介绍AI文本生成的基本原理、应用场景以及未来发展趋势,帮助读者全面了解该技术的潜力和局限性。同时,文章还提供了代码示例,展示了如何使用Python和相关库实现简单的文本生成模型。
750 9
|
数据采集 Java API
初识 DataHub|学习笔记
快速学习初识 DataHub
797 0
初识 DataHub|学习笔记
|
人工智能 搜索推荐 大数据
智能食品生产:自动化与定制化的食品制造
【10月更文挑战第26天】本文探讨了智能食品生产中的自动化与定制化趋势。自动化技术在原料处理、加工制造、包装和质检等环节的应用,显著提高了生产效率和产品质量。智能化技术则通过物联网、大数据、云计算和人工智能等手段,实现了更高效、精准和灵活的生产,并能满足消费者的个性化需求。虽然面临高成本、技术维护和数据安全等挑战,但政府和企业共同努力,将推动食品行业的健康和可持续发展。
|
人工智能 vr&ar Android开发
探索安卓与iOS的无限可能:移动操作系统的技术革新与未来展望
本文旨在探讨安卓和iOS这两大主流移动操作系统在技术上的创新与突破,以及它们如何塑造我们的数字生活。通过深入分析两者的最新进展、面临的挑战以及未来的发展趋势,文章揭示了移动操作系统在推动科技进步和满足用户需求方面的关键作用。我们将从技术角度出发,解读安卓的开放性与iOS的封闭性如何影响应用生态和用户体验,并探讨这些差异背后的设计理念和商业考量。同时,我们还将关注两大系统在安全性、隐私保护、人工智能集成等方面的最新动态,以及它们如何应对日益增长的网络安全威胁和用户对隐私保护的需求。此外,文章还将展望未来移动操作系统的发展,分析5G、物联网等新兴技术如何为安卓和iOS带来新的机遇和挑战,以及这两大
366 6
|
机器学习/深度学习 资源调度 自动驾驶
OFDM:赋能5G通信的基石
OFDM:赋能5G通信的基石
1246 3
|
机器学习/深度学习 搜索推荐 算法
量子计算与药物发现:加速新药研发的新路径
【9月更文挑战第25天】量子计算与药物发现的结合是加速新药研发的重要路径。随着技术的不断进步和应用的不断拓展,量子计算将在药物研发领域发挥越来越重要的作用,推动生命科学领域迈向一个全新的时代。
|
机器学习/深度学习 自然语言处理 NoSQL
智能制造领域智能问答系统
中国积极推动智能制造发展,推出政策支持数字化、网络化和智能化转型。智能问答系统在这一领域扮演关键角色,协助解决复杂问题,提升生产效率。然而,系统需应对跨领域知识融合、精准问题理解和用户隐私保护等挑战。悦数图数据库为智能问答系统提供数据支撑,助力企业优化生产与管理。未来,随着技术进步,两者将在智能制造中发挥更大作用。
|
JavaScript 前端开发
构建工具:配置Webpack打包Vue项目
【4月更文挑战第24天】本文介绍了如何配置Webpack来打包Vue项目。首先,Webpack作为模块打包器处理依赖并打包成可执行文件。接着,通过安装Node.js和npm,创建Vue项目,进入项目目录并配置Webpack的入口、输出、加载器和插件。最后,运行构建命令完成打包。理解Webpack基础并按需配置,能优化前端项目构建和开发体验。
361 0
|
缓存 Shell API
作者推荐 | 一文深度解读 — 彻底认识与理解微服务技术之Rest与Restful架构精髓
作者推荐 | 一文深度解读 — 彻底认识与理解微服务技术之Rest与Restful架构精髓
972 0
|
SpringCloudAlibaba 关系型数据库 MySQL
SpringCloud Alibaba微服务实战 - 基础环境准备
SpringCloud Alibaba微服务实战 - 基础环境准备
461 0