JDB(1)
π JDB(1) β JDK Commands π
π NAME π
jdb β find and fix bugs in Java platform programs. Thatβs the whole assignment, bestie. πβ¨
π¦ SYNOPSIS π¦
jdb [options] [classname] [arguments]
- options β this represents the
jdbcommand-line options. See Options for thejdbcommand. π - classname β this represents the name of the main class to debug. π·οΈ
- arguments β this represents the arguments that are passed to the
main()method of the class. π«Ά
πΈ DESCRIPTION πΈ
Okay so 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 and provides inspection and debugging of a local or remote JVM. Itβs giving βtiny but mighty debugger energyβ fr fr. π₯
β¨ START A JDB SESSION β¨
There are many ways to start a JDB session. The most frequently used way is to have the 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 the 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. Iconic behavior, honestly. π
Another way to use the jdb command is by attaching it to a JVM thatβs 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
8000 is the address of the running JVM. π
The MyClass argument isnβt 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. No duplicate main character energy here. π
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. β¨
π₯ BREAKPOINTS π₯
Breakpoints can be set in the JDB at line numbers or at the first instruction of a method, for example:
- The command
stop at MyClass:22sets a breakpoint at the first instruction for line 22 of the source file containingMyClass. - The command
stop in java.lang.String.lengthsets a breakpoint at the beginning of the methodjava.lang.String.length. - The command
stop in MyClass.<clinit>uses<clinit>to identify the static initialization code forMyClass.
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. Slay, continue. πββοΈπ¨
π STEPPING π
The step command advances execution to the next line whether itβs in the current stack frame or a called method. The next command advances execution to the next line in the current stack frame. One is βgo anywhere,β one is βstay in your lane,β and both are valid. π¦
π EXCEPTIONS π
When an exception occurs for which there isnβt a catch statement anywhere in the throwing threadβs call stack, the JVM typically prints an exception trace and exits. When running under the JDB, however, control returns to the JDB at the offending throw. You can then use the jdb command to diagnose the cause of the exception. Not me being summoned directly to the scene of the crime. π
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βs an instance of the specified class or subclass stops the application at the point where the exception is thrown. π¨
The ignore command negates the effect of an earlier catch command. The ignore command doesnβt cause the debugged JVM to ignore specific exceptions, but only to ignore the debugger. Important distinction, bestie β read it twice. ππ
π OPTIONS FOR THE JDB COMMAND π
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. Reusing the fit, we love to see it. π«Ά
The following options are accepted by the jdb command:
-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.-listenanyβ waits for a running JVM to connect at any available address using a standard connector.-launchβ starts the debugged application immediately upon startup of thejdbcommand. The-launchoption removes the need for theruncommand. 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 thecontcommand to continue execution.-listconnectorsβ lists 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 thejdbcommand.-tclientβ runs the application in the Java HotSpot VM client.-trackallthreadsβ track all threads as they are created, including virtual threads. See Working With Virtual Threads below.-tserverβ runs the application in the Java HotSpot VM server.-Joptionβ passes option to the JDB JVM, where option is one of the options described on the reference page for the Java application launcher. For example,-J-Xms48msets the startup memory to 48 MB. See Overview of Java Options injava.
The following options are forwarded to the debuggee process:
-Roptionβ passes option to the debuggee JVM, where option is one of the options described on the reference page for the Java application launcher. For example,-R-Xms48msets the startup memory to 48 MB. See Overview of Java Options injava.-vor-verbose[:class|gc|jni]β turns on the verbose mode.-Dname=valueβ sets a system property.-classpath dirβ lists directories separated by colons in which to look for classes.-Xoption β a nonstandard target JVM option.
Other options are supported to provide alternate mechanisms for connecting the debugger to the JVM that itβs to debug. Options on options, the moment. π β¨
π§ WORKING WITH VIRTUAL THREADS π§
Often virtual threads are created in such large numbers and frequency that they can overwhelm a debugger. For this reason by default JDB does not keep track of virtual threads as they are created. It will only keep track of virtual threads that an event has arrived on, such as a breakpoint event. The -trackallthreads option can be used to make JDB track all virtual threads as they are created. π§΅β¨
When JDB first connects, it requests a list of all known threads from the Debug Agent. By default the debug agent does not return any virtual threads in this list, once again because the list could be so large that it overwhelms the debugger. The Debug Agent has an includevirtualthreads option that can be enabled to change this behavior so all known virtual threads will be included in the list. The JDB -trackallthreads option will cause JDB to automatically enable the Debug Agentβs includevirtualthreads option when JDB launches an application to debug. However, keep in mind that the Debug Agent may not know about any virtual threads that were created before JDB attached to the debugged application. Big βwe can only track what we can seeβ energy. ππ
Thatβs the whole page, bestie β JDK 23, dated 2024, and JDB(1) is signing off with a little debugger kiss. ππ
β¨
π¦ where this page comes from
- opensuse-leap 16.0: java-23-openjdk-devel 23.0.2.0-160000.1.1 Β· Apache-1.1 AND Apache-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND SUSE-Public-Domain AND W3C
- opensuse-leap 16.0: java-23-openjdk-devel 23.0.2.0-160000.1.1 Β· Apache-1.1 AND Apache-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND SUSE-Public-Domain AND W3C
- opensuse-leap 16.0: java-23-openjdk-devel 23.0.2.0-160000.1.1 Β· Apache-1.1 AND Apache-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND SUSE-Public-Domain AND W3C
- opensuse-leap 16.0: java-23-openjdk-devel 23.0.2.0-160000.1.1 Β· Apache-1.1 AND Apache-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND SUSE-Public-Domain AND W3C
- Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms