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.

Description

The Java Debugger (JDB) is a simple command-line debugger for Java classes. The jdb command and its options call the JDB. The jdb command demonstrates the Java Platform Debugger Architecture (JDBA) and provides inspection and debugging of a local or remote Java Virtual Machine (JVM). See Java Platform Debugger Architecture (JDBA) at
http://docs.oracle.com/javase/8/docs/technotes/guides/jpda/index.html

Start a JDB Session

There are many ways to start a JDB session. The most frequently used way is to have JDB launch a new JVM with the main class of the application to be debugged. Do this by substituting the jdb command for the java command in the command line. For example, if your application‘s main class is MyClass, then use the following command to debug it under JDB:

jdb MyClass

When started this way, the jdb command calls a second JVM with the specified parameters, loads the specified class, and stops the JVM before executing that class‘s first instruction.

Another way to use the jdb command is by attaching it to a JVM that is already running. Syntax for starting a JVM to which the jdb command attaches when the JVM is running is as follows. This loads in-process debugging libraries and specifies the kind of connection to be made.

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n MyClass

You can then attach the jdb command to the JVM with the following command:

jdb -attach 8000

The MyClass argument is not specified in the jdb command line in this case because the jdb command is connecting to an existing JVM instead of launching a new JVM.

There are many other ways to connect the debugger to a JVM, and all of them are supported by the jdb command. The Java Platform Debugger Architecture has additional documentation on these connection options.

Basic jdb Commands

The following is a list of the basic jdb commands. The JDB supports other commands that you can list with the -help option.

help or ?

The help or ? commands display the list of recognized commands with a brief description.

run

After you start JDB and set breakpoints, you can use the run command to execute the debugged application. The run command is available only when the jdb command starts the debugged application as opposed to attaching to an existing JVM.

cont

Continues execution of the debugged application after a breakpoint, exception, or step.

print

Displays Java objects and primitive values. For variables or fields of primitive types, the actual value is printed. For objects, a short description is printed. See the dump command to find out how to get more information about an object.

Note: To display local variables, the containing class must have been compiled with the javac -g option.

The print command supports many simple Java expressions including those with method invocations, for example:

print MyClass.myStaticField
print myObj.myInstanceField
print i + j + k (i, j, k are primities and either fields or local variables)
print myObj.myMethod() (if myMethod returns a non-null)
print new java.lang.String("Hello").length()

dump

For primitive values, the dump command is identical to the print command. For objects, the dump command prints the current value of each field defined in the object. Static and instance fields are included. The dump command supports the same set of expressions as the print command.

threads

List the threads that are currently running. For each thread, its name and current status are printed and an index that can be used in other commands. In this example, the thread index is 4, the thread is an instance of java.lang.Thread, the thread name is main, and it is currently running.

4. (java.lang.Thread)0x1 main      running

thread

Select a thread to be the current thread. Many jdb commands are based on the setting of the current thread. The thread is specified with the thread index described in the threads command.

where

The where command with no arguments dumps the stack of the current thread. The where all command dumps the stack of all threads in the current thread group. The where threadindex command dumps the stack of the specified thread.

If the current thread is suspended either through an event such as a breakpoint or through the suspend command, then local variables and fields can be displayed with the print and dump commands. The up and down commands select which stack frame is the current stack frame.

Breakpoints

Breakpoints can be set in JDB at line numbers or at the first instruction of a method, for example:

  • The command stop at MyClass:22 sets a breakpoint at the first instruction for line 22 of the source file containing MyClass.
  • The command stop in java.lang.String.length sets a breakpoint at the beginning of the method java.lang.String.length.
  • The command stop in MyClass.<clinit> uses <clinit> to identify the static initialization code for MyClass.

When a method is overloaded, you must also specify its argument types so that the proper method can be selected for a breakpoint. For example, MyClass.myMethod(int,java.lang.String) or MyClass.myMethod().

The clear command removes breakpoints using the following syntax: clear MyClass:45. Using the clear or stop command with no argument displays a list of all breakpoints currently set. The cont command continues execution.

Stepping

The step command advances execution to the next line whether it is in the current stack frame or a called method. The next command advances execution to the next line in the current stack frame.

Exceptions

When an exception occurs for which there is not a catch statement anywhere in the throwing thread‘s call stack, the JVM typically prints an exception trace and exits. When running under JDB, however, control returns to JDB at the offending throw. You can then use the jdb command to diagnose the cause of the exception.

Use the catch command to cause the debugged application to stop at other thrown exceptions, for example: catch java.io.FileNotFoundException or catch mypackage.BigTroubleException. Any exception that is an instance of the specified class or subclass stops the application at the point where it is thrown.

The ignore command negates the effect of an earlier catch command. The ignore command does not cause the debugged JVM to ignore specific exceptions, but only to ignore the debugger.

Options

When you use the jdb command instead of the java command on the command line, the jdb command accepts many of the same options as the java command, including -D-classpath, and -X options. The following list contains additional options that are accepted by the jdb command.

Other options are supported to provide alternate mechanisms for connecting the debugger to the JVM it is to debug. For additional documentation about these connection alternatives, see Java Platform Debugger Architecture (JPDA) at
http://docs.oracle.com/javase/8/docs/technotes/guides/jpda/index.html

-help

Displays a help message.

-sourcepath dir1:dir2: . . .

Uses the specified path to search for source files in the specified path. If this option is not specified, then use the default path of dot (.).

-attach address

Attaches the debugger to a running JVM with the default connection mechanism.

-listen address

Waits for a running JVM to connect to the specified address with a standard connector.

-launch

Starts the debugged application immediately upon startup of JDB. The -launch option removes the need for the run command. The debugged application is launched and then stopped just before the initial application class is loaded. At that point, you can set any necessary breakpoints and use the cont command to continue execution.

-listconnectors

List the connectors available in this JVM.

-connect connector-name:name1=value1

Connects to the target JVM with the named connector and listed argument values.

-dbgtrace [flags]

Prints information for debugging the jdb command.

-tclient

Runs the application in the Java HotSpot VM client.

-tserver

Runs the application in the Java HotSpot VM server.

-Joption

Passes option to the JVM, where option is one of the options described on the reference page for the Java application launcher. For example, -J-Xms48m sets the startup memory to 48 MB. See java(1).

Options Forwarded to the Debugger Process

-v -verbose[:class|gc|jni]

Turns on verbose mode.

-Dname=value

Sets a system property.

-classpath dir

Lists directories separated by colons in which to look for classes.

-Xoption

Nonstandard target JVM option.

时间: 2024-10-14 14:08:52

java调试工具jdb的相关文章

远程debug调试java代码

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

Java千百问_08JDK详解(012)_JPDA是什么

点击进入_更多_Java千百问 1.JPDA是什么 我们可以通过常见的Java IDE提供的图形界面调试工具进行代码调试,甚至可以使用JDK自带的jdb工具,以文本命令的形式来调试Java 程序,这些形形色色的调试工具都是通过JPDA开发的. JPDA(Java Platform Debugger Architecture)是 Java平台调试体系结构的缩写,通过JPDA提供的API,开发人员可以方便灵活的搭建Java调试工具. JPDA主要由三个部分组成:Java虚拟机工具接口(JVMTI,J

Android 调试工具集【转】

1.TraceView1)功能:用于热点分析和性能优化,分析每个函数占用的CPU时间,调用次数,函数调用关系等 2)方法: a)在程序代码中加入追踪开关 import android.os.Debug; …… android.os.Debug.startMethodTracing(“/data/tmp/test”);// 先建/data/tmp目录 ……// 被追踪的程序段 android.os.Debug.stopMethodTracing(); b)编译,运行后,设备端生成/data/tmp

Java千百问_08JDK详解(002)_jdk工具集都有什么

点击进入_更多_Java千百问 1.jdk工具集都有什么工具 了解java核心框架看这里:java核心框架是什么样的 jdk工具集包含了java开发.编译和运行过程中所使用的工具,主要有以下这些: 调试体系(Debugger Architecture) 具备完善的体系结构和使用规格的调试体系,允许在开发环境中可以用来进行代码调试(调试即逐行执行代码,并且可以监控变量和运行情况).Java平台调试器体系结构也叫JPDA,它提供了一整套用于调试Java程序的API,以及用于开发Java调试工具的接口

Android开发——android调试工具集【转】

转:http://www.cnblogs.com/halzhang/archive/2010/08/11/1797097.html 1. 查看当前堆栈 1) 功能:在程序中加入代码,使可以在logcat中看到打印出的当前函数调用关系 2) 方法: new Exception(“print trace”).printStackTrace(); 2. MethodTracing 1) 功能:用于热点分析和性能优化,分析每个函数占用的CPU时间,调用次数,函数调用关系等 2) 方法: a) 在程序代码

Java学习之路(二) Java命令

1.java.exe:2.javac.exe:编译的Java程序编写的后缀为.java的文件,利用javac编译后会生成执行文件.class文件3.javaw.exe:运行Javac编译后生成的可执行文件.class文件,在执行图形界面的java程序运行java命令时,会出现一个console窗口并一直保持,并通过System.out将程序中的信息在console窗口内输出,avaw 大多用来运行GUI程序,运行javaw,JVM首先会调用控制台程序启动主程序,调用完主程序后就会隐藏控制台:4.

Java的bin等目录说明

javac:Java编译器,将Java源代码换成字节代 java:Java解释器,直接从类文件执行Java应用程序代码 appletviewer(小程序浏览器):一种执行HTML文件上的Java小程序类的Java浏览器 javadoc:根据Java源代码及其说明语句生成的HTML文档 jdb:Java调试器,可以逐行地执行程序.设置断点和检查变量 javah:产生可以调用Java过程的C过程,或建立能被Java程序调用的C过程的头文件 Javap:Java反汇编器,显示编译类文件中的可访问功能和

Android开发:调试工具集

1. 查看当前堆栈1) 功能:在程序中加入代码,使可以在logcat中看到打印出的当前函数调用关系2) 方法: new Exception("print trace").printStackTrace(); 2. MethodTracing1) 功能:用于热点分析和性能优化,分析每个函数占用的CPU时间,调用次数,函数调用关系等2) 方法:a) 在程序代码中加入追踪开关   1: import android.os.Debug;   2: --   3: android.os.Debu

使用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方式进行调试步骤: 显示所有可供调试的用户进程: