Java Debugging Tools Jdb

Source: Internet
Author: User

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 command and its options call the jdb JDB. The jdb command demonstrates the Java Platform Debugger Architecture (JDBA) and provides inspection and debugging of a L ocal 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 is many ways to start a JDB session. The most frequently used-on-the-JDB launch a new JVM with the main class of the application-be debugged. The command for the command is substituting the command line jdb java . For example, if your application ' s main class MyClass was, then use the following command to debug it under JDB:

Jdb MyClass

When started this to, 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.

The another jdb -the-use-command is-attaching it to a JVM, which is already running. Syntax for starting a JVMs to which the command attaches when the JVM was running is as follows jdb . 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 command to the JVM with the jdb following command:

Jdb-attach 8000

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

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

Basic Jdb Commands

The following is a list of the basic jdb commands. The JDB supports other commands, the 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 if the jdb command starts the debugged application as opposed to attaching to an E Xisting 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" to "get more information" about a object.

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

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

Print Myclass.mystaticfieldprint Myobj.myinstancefieldprint i + j + k (I, J, K is primities and either fields or local VA Riables) Print Myobj.mymethod () (if MyMethod returns a non-null) print new java.lang.String ("Hello"). Length ()
Dump

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

Threads

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

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

Select a thread to is the current thread. Many commands is based on the setting of the current jdb 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 command dumps the stack of all threads in the current where all thread group. The where threadindex command dumps the stack of the specified thread.

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

Breakpoints

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

    • The command sets a breakpoint at the first instruction to line in the stop at MyClass:22 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 to <clinit> identify the static initialization code for MyClass .

When a method was overloaded, you must also specify it argument types so, the proper method can be selected for a Brea Kpoint. 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 was in the current stack frame or a called method. The next command advances execution to the next on the current stack frame.

Exceptions

When an exception occurs for which there are not a catch statement anywhere in the throwing thread's call stack, the JVM ty Pically 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 catch the command to cause the debugged application to stop at other thrown exceptions, for example: catch java.io.FileNotFoundException or catch . Any exception a 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. ignorethe command does not cause the debugged JVM to ignore specific exceptions, but only to ignore the debugger.

Options

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

Other options were supported to provide alternate mechanisms for connecting the debugger to the JVM it was to debug. For additional documentation on 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 isn't 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 this 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.

-j Option

Passes option to the JVM, where option was one of the options described on the reference page for the Java application Launc Her. For example, sets the ' Startup memory ' to ' -J-Xms48m MB '. See java (1).

Options forwarded to the Debugger Process
-v-verbose[: class|gc|jni]

Turns on verbose mode.

-D Name= value

Sets a system property.

-classpath dir

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

-X option

nonstandard target JVM option.

Java Debugging Tools Jdb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.