Java Debugger (JDB)
参考
https://www.geeksforgeeks.org/java-debugger-jdb/
https://docs.oracle.com/en/java/javase/17/docs/specs/man/jdb.html
Java Debugger (JDB) is a command-line tool used by Java developers to debug Java code efficiently.
JDB Architecture
The architecture of Jdb consists of the following components:
- Java Debug Interface (JDI): JDI is a set of APIs that provides a standard interface for interacting with the JVM. Jdb uses JDI to communicate with the JVM and perform debugging tasks.
- Debuggee: Debuggee is the Java program being debugged by Jdb. Jdb attaches itself to the debuggee process and interacts with it through JDI.
- Jdb client: Jdb client is the user interface that allows developers to interact with Jdb. It provides a command-line interface for entering debugging commands.
- Java Virtual Machine (JVM): JVM is the runtime environment in which Java code is executed. Jdb uses the JVM to run the debuggee process and perform debugging tasks.
Options for the jdb command
-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.
Jdb-Basic Commands
Here are some of the most commonly used JDB commands
- run – Starts the execution of the program.
- stop at <class>:<line> – Sets a breakpoint at the specified line in the specified class.
- stop in <class>.<method> – Sets a breakpoint at the beginning of the specified method in the specified class.
- step – Executes the current line of code and stops at the next line. If the current line contains a method call, JDB steps into the method and stops at the first line of the method.
- next – Executes the current line of code and stops at the next line. If the current line contains a method call, JDB executes the method call and stops at the next line after the method call.
- cont – Continues the execution of the program until the next breakpoint or until the program completes.
- list – Lists the source code around the current execution point.
- print <expression> – Prints the value of the specified expression.
- locals – Lists the local variables in the current frame.
- classes – Lists all loaded classes.
- methods <class> – Lists all methods in the specified class.
- thread – Lists all threads and their current status.
- suspend – Suspends all threads.
- resume – Resumes all threads.
- help – Displays a list of available JDB commands.
- exit – Exits JDB.
For example, first start the jar
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar myapp.jar
Then at another terminal
jdb -attach 5005
如果要添加代码目录可以使用-sourcepath
jdb -attach 5005 -sourcepath /mnt/e/app/src/main
之后可以运行如下命令
| 命令示例 | |
| set breakpoint | stop at com.example.app.Scheduler:139 |
| continue | Cont |
| 打印变量 | pint this |
| dump this | |
| next | |
| step |

浙公网安备 33010602011771号