jdb - The Java Debugger

In this article we are going to elaborate the jdb command tool which is a part of JAVA commands (comes with jdk package). This article is the continuation of this previous article.

As we know the standard format of commands in java, the jdb command also have the format(in command prompt)
jdb [options] [class] [arguments]

JDB helps to find and fix bugs of JAVA code. Mainly it is a command line debugger of java code. So, if we find any error when compiling , we can use this tool to get the problem and solve that. We can start jdb in many ways.
-Start jdb to a new JVM with main class of the application needed debugging. In this way, jdb invokes a new JVM with any specific parameters(we select), loads the specified class, and stops the VM before executing that class's first instruction.

-Start jdb with a already running JVM(attaching). The JVM must have started in a debug supporting mode( Loads in process debug libraries and connection type to connect). Example of debug-able JVM start command
java -agentlib:jdwp=transport=dt_shmem,address=jdbconn,server=y,suspend=n MyClass
Now, attaching jdb to the VM
jdb -attach jdbconn

-We can directly attach JDB with current VM and start debugging session. In this way we have to run every command step by step. To start jdb session, just type
It will initiate jdb.

JDB options : These are command line options. 
-sourcepath [directories separated by ";"] : Directories to search source files
-launch: Launch VM immediately
-tclient : Runs the application in client Hotspot(tm) Performance Engine
-tserver : Run the application in server Hotspot(tm) Performance Engine
-listconnectors :List the connectors available in this VM
-connect [connector-name]:[name1]=[value1].. : Connect to target VM using named connector with listed argument values
-attach [address] : Attach to a running VM at the specified address using connector.
-listen [address] : Wait for a running VM to connect at the specified address using connector
-listenany : Wait for a running VM to connect at any available address using connector
-dbgtrace [flags] : print info for debugging jdb
-J[option] : Pass option to the Java virtual machine by jdb.

-v -verbose[:class|gc|jni] : Turn on verbose mode
-D[name]=[value] : Sets a system property
-classpath [directories separated by ";"] : list directories in which to look for classes
-X[option] :Non-standard target VM option. See java commands for more details.

JDB command line commands : (Inside a JDB Session)

connectors : list available connectors and transports in this VM
run [class [args]]: Start execution of application's main class

threads [thread group] : Lists threads
thread [thread id] : Sets default thread
suspend [thread id(s)] : Suspend threads (default: all)
resume [thread id(s)] : Resumes threads (default: all)
where [[thread id] | all] : Dumps a thread's stack
wherei [[thread id] | all] : Dumps a thread's stack, with PC info
up [n frames]: Moves up a thread's stack
down [n frames]: Moves down a thread's stack
kill [thread id] [expr] : Kills a thread with the given exception object
interrupt [thread id]: Interrupts a thread
threadgroups: Liststhread groups
threadgroup [name]: Sets current thread group
threadlocks [thread id]   : Prints lock info for a thread

lock [expr]: Prints lock info for an object
print [expr]: Prints value of expression
dump [expr]: Prints all object information
eval [expr]: Evaluates expression (same as print)
set [lvalue] = [expr]: Assigns new value to field/variable/array element
locals : Prints all local variables in current stack frame
classes : Lists currently known classes
class [class id]: Shows details of named class
methods [class id]: Lists a class's methods
fields [class id]: Lists a class's fields

stop in [class id].[method] [(argument_type,...)]: Sets a break point in a method
stop at [class id]:[line] : Sets a breakpoint at a line
clear : list breakpoints
clear [class id].[method][(argument_type,...)] : Clears a break point in a method
clear [class id]:[line] : Clears a break point at a line

step : Executes current line
step up : Executes until the current method returns to its caller
stepi : Executes current instruction
next : step one line (step OVER calls)
cont : Continue execution from break point
catch [uncaught|caught|all] [class id]|[class pattern]: Breaks when specified exception occurs
ignore [uncaught|caught|all] [class id]|[class pattern]: Cancels 'catch' for the specified exception

redefine [class id] [class file name] : Redefines the code for a class

watch [access|all] [class id].[field name] : Watches access/modifications to a field
unwatch [access|all] [class id].[field name] : Discontinue watching access/modifications to a field

trace [go] methods [thread] : Traces method entries and exits. All threads are suspended unless 'go' is specified
trace [go] method exit | exits [thread] : Traces the current method's exit, or all methods' exits. All threads are suspended unless 'go' is specified
untrace [methods]: Stops tracing method entries and/or exits

list [line number|method] : print source code
use [source file path]: Displays /changes the source path
sourcepath [source file path]: Displays /changes the source path
exclude [[class pattern], ... | "none"] : Does not report step or method events for specified classes
classpath : print classpath info from target VM

monitor : Lists monitors
monitor [command]: Executes command each time the program stops
unmonitor [monitor#] : Deletes a monitor

read [filename]: Reads and executes a command file
!! : Repeats last command[n] [command] : Repeat command n times
# [command] : Discard (no-op)
exit (or quit) : Exit debugger

pop : Pops the stack through and including the current frame
reenter  : Same as pop, but current frame is reentered

disablegc [expr]: Prevents garbage collection(GC) of an object
enablegc [expr]: Permits garbage collection(GC) of an object

Though all IDEs have build in debugger, but jdb is very useful while debugging remotely to server. As well as in maintenance of running java applications for supporting.


No comments:

Post a Comment