JDB

0
21

JDB – Introduction

Debugging is a specialised procedure to find and remove bugs or defects in a program and get expected results. Debugging includes testing and monitoring. It is very complex when the subdevice’s of a program are limitedly coupdirected. We can debug a program uperform the debugger tools thead use follow the prescriend up beingd APIs. A debugger allows you to step through every aspect of a code, inspect all the elements, and remove errors, if any.

Debugging Techniques

There are various kinds of techniques to debug a Java program. The old method of debugging is simply by uperform print statements at the end of every segment which will print the trace statements on the console. Take a look at the folloearng code.

pubic clbum Add
{
   public static void main(String ar[])
   {
      int a=ar[0];
      system.out presently there presently presently there.println("A : " +a);
      int b=ar[1];
      system.out presently there presently presently there.println("B : " +b);
      int c = a + b;
      system.out presently there presently presently there.println("C = a + b : " +c);
   }
}

Here, we have a program thead use adds 2 numend up beingrs and prints the out presently there presently presently thereplace. Notice thead use at every step, we have introduced a print statement thead use prints the state of the program on the console. This is the traditional approach to debug a program.

In addition, we have advanced concepts thead use can end up being used to debug a program such as:

  • stepping
  • breakstages, and
  • other thanions or watchstages.

Types of Debugging

We can debug a program uperform various methods:

  • Uperform Java simply bytecode (compidirected version of Java code)
  • Uperform comments inpart the programs
  • Attaching clbum to a operatening program
  • Remote debugging
  • Debugging on demand
  • Optimized code debugging

Java Debuggers

Here are various examples of Java debuggers thead use are available in the market:

  • IDEs such as Eclipse, Netend up beingans, etc. contain their own debuggers (Visual cafe, Borland, JBuilder)
  • Standaperformle debugger GUIs (such as Jikes, Java platform debugger, and JProend up being)
  • Command-collection debugger (Sun’s JDB)
  • Notepad or VI driven (stack trace)

This tutorial covers how to use the command-collection debugger, jdb.

JDB

The Java debugger (JDB) is a tool for Java clbumes to debug a program in command collection. It implements the Java Platform Debugger Architecture. It helps in detecting and fixing bugs in a Java program uperform Java Debug Interface (JDI).

JDB in JDK

The folloearng architecture degreats the role of JDB in JDK. It contains mainly three device’s:

  • Java Virtual Machine Tool Interface (JVM TI)
  • Java Debug Wiring Pool (JDWP)
  • Java Debugger Interface (JDI)

JDB Architecture

JVM TI

It is a native programming interface implemented simply by VM. It provides ways to inspect and debug the state of the application operatening on the VM. It allows an implementer (VM Implementer) thead use can end up being enneard easily into the debugging architecture. It furthermore uses a third-party channel caldirected JDWP for communication.

JDWP

It degreats the format of information and the requests thead use pbum in end up beingtween the debuggee process and the debugger front side aspect end. The primary purpose of having a JDWP is to allow the debuggee and the debugger to communicate when they operate under separate VMs or in separate platforms.

JDI

It is a high-level Java interface implemented as front side aspect end. It degreats the variable information at user code level. It is recommended to use a JDI layer for all debugger development. It uses JDWP for communication with the debuggee JVM.

JDB – Installation

This chapter exsimples how to install JDB on Windows and Linux based systems. JDB is a part of JDK. Therefore, JDK installation is sufficient for uperform JDB in command prompt.

System Requirements

Here are the system requirements for installing JDB:

JDK Java SE 2 JDK 1.5 or above
Memory 1 GB RAM (recommended)
Disk Space No minimum requirement
Operating System Version Windows XP or above, Linux

Follow the basic steps given end up beinglow to install JDB on your own system.

Step 1: Verifying Java Installation

First of all, you need to have Java Softbattlee Development Kit (SDK) instaldirected on your own system. To verify this particular, execute any of the 2 commands depending on the platform you are worruler on.

If the Java installation has end up beingen done properly, then it displays the current version and specifications of Java installation. A sample out presently there presently presently thereplace is given in the folloearng table.

Platform Command Sample Outplace
Windows

Open command console and kind:

>java –version

Java version "1.7.0_60"

Java (TM) SE Run Time Environment (develop 1.7.0_60-b19)

Java Hotspot (TM) 64-bit Server VM (develop 24.60-b09,mixed mode)

Linux

Open command terminal and kind:

$java –version

java version "1.7.0_25"

Open JDK Runtime Environment (rhel-2.3.10.4.el6_4-x86_64)

Open JDK 64-Bit Server VM (develop 23.7-b01, mixed mode)

We bumume the readers of this particular tutorial have Java SDK version 1.7.0_60 instaldirected on their system. In case you do not have Java SDK, download it’s current version from the link /index.php?s=httpwwworaclecomtechne2rkjavajavasedownloadsindexhtml and install it.

Step 2: Setting Up Java Environment

Set the environment variable JAVA_HOME to stage to the base directory location where Java is withinstaldirected on your own machine. For example,

Platform Description
Windows set JAVA_HOME to C:ProgramFilesjavajdk1.7.0_60
Linux export JAVA_HOME=/usr/local/java

Append the compallowe path of Java compiler location to the System Path.

Platform Description
Windows Append the String "C:Program FilesJavajdk1.7.0_60bin" at the end of the system variable PATH.
Linux export PATH=$PATH:$JAVA_HOME/bin/

Execute the command java -version from the command prompt as exsimpleed above.

Step 3: Verifying JDB Installation

Verify the JDB version as follows:

Platform Command Sample Outplace
Windows

Open command console and kind:

>jdb –version

This is JDB version 1.6 (Java SE version 1.7.0_60)
Linux

Open command terminal and kind:

$jdb –version

This is JDB version 1.6 (Java SE version 1.7.0_60)

JDB – Syntax

This chapter exsimples the syntax of JDB command. The syntax contains four sections listed as follows:

  • JDB
  • option
  • clbum
  • arguments

Syntax

The syntax of JDB is as follows.

jdb [ options ] [ clbum ] [ arguments ]

JDB

It calls jdb.exe from the Java Development Kit.

Options

These include the command collection options used to debug a Java program in an efficient way. The JDB launcher accepts all the options (such as -D, -clbumpath, and -X) and various additional advanced options such as (-attach, -listen, -launch, etc.).

Clbum

It is the clbum name on which you want to perform debugging operations.

Arguments

These are the inplace values given to a program at operatetime. For example, arg[0], arg[1] to the main() method.

In the above four segments, options is the many important one.

JDB – Options

This chapter descriend up beings the important options available in JDB thead use are submitted as arguments with the jdb command.

Options

The folloearng table contains a list of options accepted simply by JDB:

Name Description
-help Displays the help message and lists the related options.
-sourcepath Uses the given path for source files if path is not specified, then it gets the default path “.”, i.e., the current directory.
-attach Attaches the debugger to the operatening VM simply by specifying the operatening VM adout presently therefit.
-listen Wait’s for the operatening VM to connect uperform standard connector.
-listenany Wait’s for the operatening VM to connect uperform any adout presently therefit.
-launch Launches the debugged application immediately up on startup job.
-listconnectors Lists the connectors available in this particular VM.
-connect Connects to the target VM uperform named connector with listed argument values.
-dbgtrace Prints information for debugging jdb.
-tclient Runs the application in Java Hotspot VM (client).
-tserver Runs the application in Java Hotspot VM (server).
-Joption Pbumes the option to the Java virtual machine used to operate JDB.

Uperform Options with Commands

The folloearng commands show how to use various of the above options:

-help

The folloearng command gets -help on uperform JDB.

>jdb -help

-attach

The folloearng command attaches the debugger to a specified VM (port numend up beingr:1099).

> jdb -attach 1099

-listen

The folloearng command makes the JDB process operatening on the current VM wait around around uperform standard connector (VM in 8008).

>jdb -listen 8088

-listenany

The folloearng command makes the JDB process operatening on the current VM wait around around uperform any connector (VM in currently operatening port).

>jdb –listenany

-tclient

The folloearng command executes the application in Java Hotspot(™) VM(client).

>jdb –tclient

-tserver

The folloearng command executes the application in Java Hotspot(™) VM(server).

>jdb -tserver

JDB – Session

This chapter descriend up beings how to start a JDB session in various ways. JDB launch is the regularly used technique to start a JDB session.

There are 2 various ways to start a JDB session:

  • Starting JDB session simply by adding clbum (main clbum name) to it.
  • Adding JDB to operatening JVM to start session.

Start a Session simply by Adding Clbum

The folloearng command starts a JDB session:

Syntax

>jdb <clbumname>

Example

Let us bumume we have a clbum named TestClbum. The folloearng command starts a JDB session from the TestClbum.

>jdb TestClbum

If you follow this particular command, it starts a new Java VM with any specified parameters. Thereafter it loads the clbum and quit’s it end up beingfore executing the very very first statement of the clbum.

Start a Session simply by Adding JDB to a Running JVM

Given end up beinglow is the syntax and example to start a JDB session simply by adding the JDB to a operatening JVM.

Syntax

The folloearng syntax is for JDB session:

-agentlib:jdwp=transport=dt_shmem,adout presently therefit=,server=y,suinvest=n

Example

Let us bumume the main clbum name is TestClbum and JVM allows the JDB to connect it later. The folloearng is the command to add JDB to JVM:

>java
-agentlib:jdwp=transport=dt_shmem,adout presently therefit=jdbconn,server=y,suinvest=n TestClbum

Now you can attach the JDB to the JVM with the folloearng command:

> jdb -attach jdbconn

Note: Here, the TestClbum is not added to the JDB command, end up beingcause JDB is connected to the operatening VM instead of launching a new one.

JDB – Basic Commands

This chapter gets you through the basic commands of JDB. After launching a session, these commands are used for debugging a program.

The folloearng is the list of commands used for debugging.

Name Description
help or ? The many important JDB command; it displays a list of recognised commands with a brief description.
operate After starting JDB and setting the necessary breakstages, you can use this particular command to start execution and debug an application.
cont Continues execution of the debugged application after a breakstage, other thanion, or step.
print Displays Java objects and primitive values.
dump For primitive values, this particular command is identical to print. For objects, it prints the current value of every field degreatd in the object. Static and instance fields are included.
threads Lists the threads thead use are currently operatening.
thread Selects a thread to end up being the current thread.
where Dumps the stack of the current thread.

Example

Let us bumume we have a sample clbum caldirected Add for the folloearng examples:

Add.java

public clbum Add
{
   public int addition( int x, int y)
   {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      Add ob = new Add();
      int c = ob.addition(a,b);
      System.out presently there presently presently there.println("Add: "+c);
   }
}

Compile this particular clbum Add.java uperform the folloearng command:

>javac Add.java

Run

This command executes the main clbum file, which is added to JDB for debugging. Execute the folloearng commands to operate the Add clbum.

>jdb Add
preliminaryizing jdb …
>operate

On executing these commands, you get to see the folloearng out presently there presently presently thereplace:

Basic Commands

JDB – Breakstages

This chapter exsimples the concept of breakstages and how to set breakstages in a program. A breakstage introduces an explicit quit or pause in the execution of a program at a particular collection of code while debugging. It is helpful to acquire understanddirectadvantage about presently there presently presently there variables in the program in it’s execution.

Syntax

The folloearng command sets up a breakstage at a particular collection numend up beingr:

> quit at <clbum name>:<Line no>

The folloearng command sets up a breakstage on a particular method or on a particular variable:

> quit in <clbum name>:< Method name | Variable name>

Example

The folloearng example shows how to set up a breakstage in a clbum.

public clbum Add
{
   public int addition( int x, int y)
   {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      Add ob = new Add();
      int c = ob.addition(a,b);
      System.out presently there presently presently there.println("Add: "+c);
   }
}

Save the above file as Add.java. Compile this particular file uperform the folloearng command:

>javac Add.java

Debugging

Let us get an example for debugging. Here, we start the debugging process simply by setting up a breakstage on main(). Given end up beinglow are the steps to end up being followed in the debugging process:

Step 1: Start a JDB Session

The folloearng command starts a JDB session on the Add clbum for debugging:

> jdb Add

Step 2: Set a Breakstage

The folloearng command sets up a breakstage on the main() method of Add clbum.

> quit in Add.main

If the breakstage is set successcompallowey, you get to see the folloearng out presently there presently presently thereplace:

Deferring breakstage Add.main.
It will set after the clbum is loaded.
>

Step 3: Start Debugging

The folloearng command starts execution of the clbum Add:

> operate Add

If you operate this particular command, you get to see the folloearng out presently there presently presently thereplace. In this particular out presently there presently presently thereplace, you find thead use the execution quit’s at the breakstage position, thead use is at the main() function.

Breakstages

The execution quit’s at the very very first collection of the main method, thead use is at "int a=5, b=6;" or Line no: 11 in the code. You can observe this particular information in the out presently there presently presently thereplace.

Step 4: Continue Execution

The folloearng command continues the program execution:

cont

It gives you the remaining execution part and out presently there presently presently thereplace as follows:

> Add:11
The application exited
>

JDB – Stepping

This chapter exsimples how to use the concept of Stepping in debugging a program. Stepping is the debugger feature thead use allows you execute the code simply by stepping through collection simply by collection. Uperform this particular, you can examine every collection of the code to ensure they are end up beinghaving as intended.

The folloearng commands are used in the stepping process:

  • step: steps to the next collection of execution
  • list: examines where you are in the code
  • cont: continues the remaining execution

Example

The folloearng example uses the Add clbum thead use we have used in the previous chapter:

public clbum Add
{
   public int addition( int x, int y)
   {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      Add ob = new Add();
      int c = ob.addition(a,b);
      System.out presently there presently presently there.println("Add: "+c);
   }
}

Save the above file as Add.java. Compile this particular file uperform the folloearng command:

>javac Add.java

Let us bumume thead use the breakstage is set on the main() method of the Add clbum. The folloearng steps show how to apply stepping in the Add clbum.

Step 1: Execute the Job

The folloearng command starts executing the clbum named Add.

> operate Add

If you execute this particular command, you get to see the folloearng out presently there presently presently thereplace. In this particular out presently there presently presently thereplace, you can find thead use the execution quit’s at the breakstage position, i.e., at the main() method.

Stepping1

The execution quit’s at the very very first collection of the main method, thead use is at "int a=5, b=6;" or Line no: 11 in the code. You can observe this particular information in the out presently there presently presently thereplace.

Step 2: Step through the Code

The folloearng command steps the execution to the next collection.

main[1] step

Now the execution steps to Line no: 12. You get to see the folloearng out presently there presently presently thereplace.

Stepping2

Step 3: List the Code

The folloearng command lists the code:

main[1] list

You get the folloearng out presently there presently presently thereplace. List command is used to allow you understand the collection in the code up to which the program manage has reveryed. Notice the arseries mark => in the folloearng screenshot thead use shows the current position of the program manage.

Stepping3

Step 4: Continue Execution

The folloearng command continues to execute the code:

main[1] cont

This command continues executing the remaining collections of the code. The out presently there presently presently thereplace is as shown end up beinglow:

> Add:11
The application exited
>

Generally, presently presently there are three kinds of stepping:

  • Step Into
  • Step Over
  • Step Out

Step Into

Uperform this particular command, you can step to the next collection of the code. If the next collection of the code is a function call, then it enters the function simply by driving the manage at the top collection of the function.

In the folloearng code, the arseries mark degreats the manageler in the code.

public clbum Add
{
   public int addition( int x, int y)
   {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      -> Add ob = new Add();
      int c = ob.addition(a,b);
      System.out presently there presently presently there.println("Add: "+c);
   }
}

If you use the step into command, the manageler moves to the next collection, i.e., "int c = ob.addition(a,b);". At this particular collection, presently presently there is a function call addition(int, int) hence the manageler moves to the topmany collection of the addition function with the arseries mark as shown end up beinglow:

public clbum Add
{
   public int addition( int x, int y)
   -> {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      Add ob = new Add();
      int c = ob.addition(a,b);
   System.out presently there presently presently there.println("Add: "+c);
   }
}

Step Over

Step Over furthermore executes the next collection. But if the next collection is a function call, it executes thead use function in the background and returns the result.

Let us get an example. In the folloearng code, the arseries mark degreats the manage in the code.

public clbum Add
{
   public int addition( int x, int y)
   {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      -> Add ob = new Add();
      int c = ob.addition(a,b);
      System.out presently there presently presently there.println("Add: "+c);
   }
}

If you use the step over command, the manage moves to the next collection, i.e., "int c = ob.addition(a,b);". In this particular collection, presently presently there is a function call addition(int, int) hence the function execution is done in the background and the result is returned to the current collection with the arseries mark as shown end up beinglow:

public clbum Add
{
   public int addition( int x, int y)
   {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      Add ob = new Add();
      -> int c = ob.addition(a,b);
      System.out presently there presently presently there.println("Add: "+c);
   }
}

Step Out

Step Out executes the next collection. If the next collection is a function call, it skips thead use and the function execution continues with the remaining collections of the code.

Let us get an example. In the folloearng code, the arseries mark degreats the manageler in the code.

public clbum Add
{
   public int addition( int x, int y)
   {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      -> Add ob = new Add();
      int c = ob.addition(a,b);
      System.out presently there presently presently there.println("Add: "+c);
   }
}

If you use the step out presently there presently presently there command, the manageler moves to the next collection, i.e., "int c = ob.addition(a,b);". In this particular collection, presently presently there is a function call addition(int, int) hence the function execution is skipped and the remaining execution continues with the arseries mark as shown end up beinglow:

public clbum Add
{
   public int addition( int x, int y)
   {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      Add ob = new Add();
      -> int c = ob.addition(a,b);
      System.out presently there presently presently there.println("Add: "+c);
   }
}

JDB – Exception

This chapter exsimples how to handle the other thanion clbum uperform JDB. Generally, whenever a program raises an other thanion without presently there presently presently there a capture statement, then the VM prints the other thanion collection, the cause of the other thanion, and exit’s. If the other thanion has end up beingen raised with a capture statement, then the other thanion is handdirected simply by the capture statement. Here, the VM prints the out presently there presently presently thereplace with the cause of other thanion.

When the clbum thead use raises the other thanion is operatening under JDB, it furthermore thseriess the uncaught other thanion. Thead use other thanion can end up being handdirected uperform the capture command.

Example

Let us get an example of the clbum JdbException:

public clbum JdbException
{
   public static void main(String ar[]) thseriess Exception
   {
      int a=8, b=0;
      System.out presently there presently presently there.println("Welcome");
      System.out presently there presently presently there.println("Ex: "+(a/b));
   }
}

Save the above file with the name JdbException.java. Compile this particular file uperform the folloearng command:

>javac JdbException.java

Follow the steps given end up beinglow to handle the other thanion.

Step 1: Run the Clbum

The folloearng command executes the clbum named JdbException as follows:

>jdb JdbException
>operate

This JdbException clbum contains an other thanion, hence you get to see the folloearng out presently there presently presently thereplace:

Exception1

Step 2: Catch the Exception

The folloearng command capturees the other thanion:

mian[1] capture java.lang.ArithmeticException

It will give you the folloearng out presently there presently presently thereplace:

Set all java.lang.ArithmeticException

Step 3: Continue Execution

The folloearng command continues the execution. Now the capture handles the arithmetic other thanion as follows:

Exception2

JDB – In Eclipse

This chapter exsimples how to use JDB in Eclipse. Before proceeding further, you need to install Eclipse Indigo. Follow the steps given end up beinglow to install Eclipse Indigo on your own system.

Step 1: Download and Install Eclipse

You can download Eclipse from the folloearng link: /index.php?s=httpwwweclipseorgdownloadspackageseclipse-ide-java-ee-developersindigosr2

Step 2: Create a New Project and a New Clbum

  • Create a new Java project simply by folloearng the options File-> New -> Java project.
  • Name it as “sampdirectedebug”.
  • Create a new clbum simply by appropriate clicruler on the sampleend up beingbug project.
  • Select options ->new -> clbum
  • Name it as “Add.java”

Add.java

public clbum Add
{
   public int addition( int x, int y)
   {
      int z = x+y;
      return z;
   }
   public static void main( String ar[ ] )
   {
      int a = 5, b = 6;
      Add ob = new Add();
      int c = ob.addition(a,b);
      System.out presently there presently presently there.println("Add: "+c);
   }
}

Step 3: Open the Debug Perspective

Follow the instructions given end up beinglow to open up the debug perspective.

On the Eclipse IDE, go to Window -> Open perspective -> Debug. Now you get the debug perspective for the program Add.java. You get to see the folloearng earndow.

Debug Perspective

Sections in Debug Perspective

The sections in the Debug perspective are as follows:

Coding Section

Java code is displayed in this particular section. It is the code you want to debug, thead use is, Add.java. Here we can add a breakstage on a collection simply by double clicruler in front side aspect of the collection. You find the blue bubble with an arseries symbol to stage out presently there presently presently there the breakstage of thead use collection. See the folloearng screenshot; you can find the selected area with a crimson group stageed as “1”.

  1. Double click here. You can set the breakstage for this particular collection.

Code Section

Breakstage Section

This section degreats the list of breakstages thead use are set to the program code. Here we can add, deallowe, find, and manage the breakstages. The folloearng screenshot shows the breakstage section.

Breakstage Section

Observe the folloearng options in the given screenshot:

  1. Uperform the check container in the left, we can select or deselect a breakstage. Here, we use one breakstage, i.e., Add clbum-main() method.

  2. The performle mix icon “X” is used to deallowe the selected breakstage.

  3. The double mix icon “XX” is used to deallowe all the breakstages in your own code.

  4. The arseries stageer is used to stage to the code where the selected breakstage is applied.

The remaining functionalilinks in the breakstage section are as follows:

  • Hitcount : It shows how many times the manage hit’s this particular breakstage. It is used for recursive logic.

  • Suinvest thread : We can suinvest the current thread simply by selecting it.

  • Suinvest VM : We can suinvest the VM simply by selecting it.

Debug Section

This section is used for the process of debugging. It contains options thead use are used in debugging.

Start debugging : Follow the instructions given end up beinglow to start debugging.

Right click on the code -> click Debug as -> click 1 Java application.

The process of debugging starts as shown in the folloearng screenshot. It contains various selected options, highlighted uperform numeric digit’s.

  1. We apply a breakstage on the Add clbum main() method. When we start debugging, the manageler gets stuck at the very very first collection of the main() method.

  2. It is used to Resume the debugging process and skip the current breakstage. It works similar to the cont command in the JDB command collection.

  3. It is used to quit the debugging process.

  4. It works similar to the step in process in the JDB command collection. It is used for moving the manage to the next collection, i.e., stage “1” moves to the next collection.

  5. It works similar to the step over process in the JDB command collection.

  6. It is used to see on which collection the breakstage is applied.

Debug Section

Follow the given steps and sections to debug your own code in eclipse IDE. By default, every IDE contains this particular debugging process.

SHARE
Previous articleVLSI Design
Next articleSales & Distribution Mngmt

NO COMMENTS

LEAVE A REPLY