Debugging NetBeans with the NetBeans debugger
by Marián Petráš
Introduction
To debug Netbeans using the NetBeans debugger, two instances of NetBeans must be run.
One instance of NetBeans will serve as a debugger, the second instance will be
debugged. Once the first instance of NetBeans is running, you use it to
attach to the JVM running the second instance of NetBeans. Debugging
starts as soon as the connection is established. The two instances of NetBeans
need not to be the same versions. E.g. you can use NetBeans 4.1
for debugging NetBeans 5.0 and vice versa.
This document describes debugging using JPDA Debugger.
Prerequesites
- module JPDA Debugger must be installed in NetBeans serving
as a debugger
- NetBeans being debugged must be built with debugging information generated, line breakpoints
will not work otherwise
(assure that parameter
-Dbuild.compiler.debug=true is passed to ant when building)
Step-by-step instructions
Summary
- Start NetBeans which will be used as a debugger.
- Start the second instance of NetBeans in a debugging mode.
- Attach to the second instance of NetBeans.
Starting NetBeans in a debugging mode
The JVM running the second instance of NetBeans must be run in a
debugging
mode, otherwise a connection cannot be established.
To run NetBeans in a debugging mode, you have to pass extra parameters to the
NetBeans launcher:
netbeans -J-Xdebug -J-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8765
Text displayed in blue may be replaced with an alternative text if needed:
- netbeans
- You may also use an alternative launcher, such as
nb.exe.
- transport=dt_socket
- This argument specifies way of communication between the debugger
and the JVM being debugged. On MS Windows, you can use transport=dt_shmem instead. A shared memory
will be then used as a communication medium instead of sockets.
Communication via shared memory is slightly quicker than
communication via sockets.
- suspend=n
- This argument specifies that the JVM will start NetBeans
immediately and will not wait until some debugger attaches to
it. Change this option to suspend=y if you want to debug
NetBeans startup. The JVM will be suspended until you attach to
it.
- address=8765
- This argument specifies a port number used for socket
communication between debugger and this JVM. You can use also
another port number.
If a shared memory is used for communication, this argument
specifies a name of a shared memory to be used. The name
can consist of any combination of characters, excluding the
backslash. Example: address=NetBeans.
Alternatively when using post 3.5 release you can use main
nbbuild/build.xml to pass the correct parameters to the launcher:
cd nbbuild
ant tryme -Ddebug.port=8765
Attaching to the second instance of NetBeans
In NetBeans, select menu item
Debug | Attach.... A dialog
appears:
(The dialog may appear slightly differently - see notes
below.)
Enter proper parameters...
- Make sure that you have selected "JPDA Debugger"
in the Debugger Type combo-box.
- Make sure that the selected transport type
matches the transport type specified in the command-line for
starting NetBeans in debugging mode.
- (socket connection only) Enter port number. The port number must match
the port number specified in the command-line for starting NetBeans in
debugging mode.
- (shared memory connection only) Enter the shared memory name. It must
match the name specified in the command-line for starting NetBeans in
debugging mode.
... and press
OK.
If you used parameter suspend=y in the command-line arguments, the
second instance of NetBeans starts loading now.
Test
To check that you have done everything well, try the following:
- Start NetBeans which will be used as a debugger.
- Create a method breakpoint on method
org.netbeans.Main.main.
- Start the second instance of NetBeans in debugging mode, using switch
"suspend=y" (the launcher will write out information
about a user directory and wait for a connection).
- Attach to the second instance of NetBeans.
The breakpoint should be hit immediately after a connection is established.
Notes
- If you want to see values of local variables (such as method
parameters), you need to assure that switch
"-Dbuild.compiler.debug=true" is passed to
ant when building NetBeans or the plugin you are going to debug.
Troubleshooting
- I cannot start NetBeans in debugging mode.
Make sure all parameters of the NetBeans launcher are correct. There must be no
space between "-J-Xrundjwp:" and
"transport=...". The port number should be higher than 1024.
- (Unix/Linux)
I cannot start NetBeans in debugging mode. JVM cannot find library
libjdwp.so.
Add path to libjdwp.so to an environment variable
LD_LIBRARY_PATH before starting NetBeans in debugging mode.
E. g. if you use Linux, your JDK is installed
in directory /usr/java and you use bash,
use the following commands:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/java/jdk1.3.1/lib/i386
runide.sh ...