Using ABAP Debugger

Objective

After completing this lesson, you will be able to Debug an ABAP program.

Start Using the ABAP Debugger in ADT

There is no way around the fact that errors occur in programs. However, they manifest themselves in different ways. When a user starts a faulty application, it may crash, something unexpected may happen, or nothing at all may happen. From the user's point of view, at the user interface level, it is impossible to say just how and why this error occurred.

As a developer, you need to examine the program more closely, line-by-line, to establish just what statements and combinations of values in the different program variables caused the error. This is where the Debugger comes in.

The Debugger in the ADT is an important diagnostic tool that you can use to analyze an ABAP application.

With the Debugger, you can determine why a program is not working correctly by stepping inside the program at runtime. This allows you to see the statements being executed and the changing value of variable values as the program proceeds.

To use the Debugger, you first decide where to start the Debug process. One way is to set breakpoints in the source code, run your program, and stop at that set breakpoint.

Some of the features in the Debugger that are available are as follows:

Features of the ABAP Development Tools (ADT) Debugger

FeatureADT Debugger
Setting BreakpointsYes
WatchpointsYes (Depending on release)
Conditional breakpointsYes
Stepping through codeYes
Displaying variable valuesYes

Debug an ABAP Program using ABAP Development Tools (ADT)

When you debug an ABAP Program using ADT, you use the Debug perspective. This is a customized version of the standard Eclipse Debug perspective, and it contains views and functions that are particularly important for debugging.

Screenshot of the Debug perspective with debug specific views

Some important elements of the debugger perspective are as follows:

Source Code View

The Source Code view is the central part of the debugger perspective. It displays the source code and highlights the current position in the program.

Variables View

The Variables view is also very important. You use this view to display the current values of variables of variables.

Breakpoints View

The Breakpoints view is displayed next to the Variables view (not selected in the preceding figure, Debug Perspective). You use this view to display, delete, or create breakpoints. Breakpoints are points in the program at which normal processing is interrupted, and the system shows you the Debugger so that you can analyze the program's state at exactly that moment.

Navigation Functions

While debugging a program, you use the navigation functions to control the execution of the code.

Debug View

The Debug view on the upper left shows the debugging session and the call hierarchy. You will need this later when you debug calls of modularization units, for example, methods.

Perspective Selector

You can switch back to the ABAP perspective with the Perspective Selector buttons in the upper right corner.

Controlling the Execution of the Code

Some Navigation Functions

When you start debugging, use the navigation functions to control the execution of the code.

Screenshot of the “Step Into” button
Screenshot of the “Resume” button

Some important navigation functions follow:

Step Into (F5)

Choose Step Into, or select F5 on your keyboard to execute a single step. Use this function for a step-by-step analysis. If, for example, you want to see which code block of a control structure is actually executed.

Resume (F8)

Choose Resume or select F8 on your keyboard to execute the program up to the next breakpoint.  If the debugger does not hit any more breakpoints, the program is executed to the end and the debugging session terminates.

Run to Line (Shift+F8)

Choose Run to Line, or select Shift+F8 on your keyboard to execute the program up to the current cursor position. Choosing a code line and choosing this function is a convenient alternative to setting a breakpoint, choosing Resume, and removing the breakpoint again.

Jump to Line (Shift+F12)

Choose Jump to Line or select Shift+F12 on your keyboard to skip some lines of code or to jump backwards to some already executed code. This function can be helpful to simulate what would happen if a certain piece of code was removed or to repeat debugging a bit of code you missed analyzing the first time. Keep in mind that this is jumping, not executing the code. When you jump backward, changes to the data objects are not reverted.

Terminate

Choose Terminate (red box icon) if you are done with debugging and you do not want to execute the remaining program. The debug session terminates immediately.

Display Content of Data Objects

One way to analyze the content of data objects in the debugger is the mouse-over functionality of the ABAP Editor. While in debugging mode, place the cursor on the name of a data object and wait a moment. A dialog box opens with the content of the data object.

Another way to analyze the content of data objects in the debugger is the Variables view. This view displays a list of data objects and their current values. The main list, the so-called top-level variables, contains some built-in data objects, by default. In this example SY-SUBRC and ME. Expand node Locals to see a list of all variable data objects defined in the current processing block.

There are three ways to add data objects to the main list on the Variables view:

  • In the editor, double-click on the name of a data object
  • In the variables list, left-click on placeholder <Enter variable> and enter the name of the data object
  • Right-click on a variable in Locals node and choose Show as Top Level Variable

One way to analyze the content of data objects in the debugger is the mouse-over functionality of the ABAP Editor. While in debugging mode, place the cursor on the name of a data object and wait a moment. A dialog box opens with the content of the data object.

Another way to analyze the content of data objects in the debugger is the Variables view. This view displays a list of data objects and their current values. The main list, the so-called top-level variables, contains some built-in data objects, by default. In the example, these are SY-SUBRC and ME. Expand node Locals to see a list of all variable data objects defined in the current processing block.

There are three ways to add data objects to the main list on the Variables view:

  • In the editor, double-click on the name of a data object
  • In the variables list, left-click on placeholder <Enter variable> and enter the name of the data object
  • Right-click on a variable in Locals node and choose Show as Top Level Variable

Here's a tip. To remove a data object from the list, right-click on it and choose Remove.

Watchpoints

A simple watchpoint on a variable causes the program to stop in the debugger whenever the value of this variable changes. By adding a condition, the program only stops when the value of the variable changes and the condition is fulfilled.

To set a watchpoint on a variable, double-click the variable in the source-code display, then right-click it and choose Set Watchpoint. This creates a watchpoint on this variable, which you can then see in the Breakpoints view.

To add a condition to a watchpoint, choose it in the list of breakpoints and enter the condition in the Condition field. Press Enter to save the watchpoint with the condition.

Screenshot of a watchpoint defined in the Breakpoints view

If an unexpected value of a variable is causing you problems, you can track its value during the course of the program execution using a Watchpoint.

A simple Watchpoint on a variable causes the program to stop in the debugger whenever the value of this variable changes. By adding a condition, the program does not stop at every value change of the variable, but only in those cases where also the condition is fulfilled.

To set a Watchpoint on a variable, only while in the debugger, highlight the variable in the source code, then right-click on it and choose Set Watchpoint. This creates a Watchpoint on the variable, which you can see in the Breakpoints view.

Select the Watchpoint variable to add a condition to a Watchpoint, choose it in the list of Breakpoints and enter the condition in the Condition field. Select Enter on your keyboard to save the Watchpoint with the condition.

To remove the Watchpoint, go to the Breakpoints view, right-click on the Watchpoint variable, and choose Remove.

Debug an ABAP Application and Set a Watchpoint

Business Exercise

We will now look at how to set a breakpoint and create a watchpoint.

Note

In this exercise, XX refers to your number.

Steps

  1. Open the local class LHC_GROCERY of class ZBP_R_XX_GROCERY. Add a breakpoint when field lv_expiration is assigned a value.

    1. Double-click on class ZBP_R_XX_GROCERY in the Project Explorer.

    2. Select the Local Types tab at the bottom of the editor window.

    3. To set the breakpoint, double-click in the line's margin:

      lv_expiration = ls_grocery-Expirationdate.
  2. Execute the application program.

    1. Double-lick on the Service Binding ZUI_XX_GROCERY_04 in the project explorer to open the service binding.

    2. Under Service Version Details, select Grocery below Entity Set and Association.

    3. Press Preview... to execute the application.

  3. Step through the code and see the variables.

    1. Press Go to view the grocery list. If the list is empty, press Create and create at least one grocery item.

    2. Select the checkbox to the left of a grocery item and press Check for expiration.

    3. There will be a prompt in Eclipse to switch to the debug perspective. Press Switch. The debug perspective is now displayed.

    4. To display the value of lv_Expiration, hover the mouse over lv_expiration on line:

      lv_expiration = ls_grocery-Expirationdate.
    5. Double-click on lv_expriation to add to the variable list on the right side of the window.

    6. Press Step Into (F5) (single step). Look at the changed value for lv_expiration in the variable list.

    7. Step through more code and look at any variable values you are interested in.

    8. To finish, press Resume (F8).

      Screenshot of the Resume button
  4. Create a watchpoint for a variable.

    Note

    In order for the watchpoint to be reached the grocery item selected must have an Expiration Date in the past. If this is not the case, click on a grocery item from the list, and on the detail screen, choose Edit and set the Expiration Date to a past date.
    1. To create a watchpoint on a variable, repeat the previous steps until you enter the debug perspective.

    2. With your mouse, right-click on lv_expired. Choose Set Watchpoint.

    3. On the top right of the screen, switch from the tab Variables to the tab Breakpoint.

    4. Double-click on lv_expired and add this condition at the bottom of the view.

      = abap.true
    5. Press Save Save (Ctrl-S).

    6. Press Resume (F8).

  5. View the old and new values of the watchpoint variable.

    1. Press the Variables tab.

    2. Expand Watchpoint LV_EXIPIRED Values. View Recent Value and Current Value.

  6. Resume the application and switch back to the ABAP Perspective.

    1. Press Resume (F8).

    2. Press the ABAP Perspective button on the top right of the window.

    Practice

Log in to track your progress & complete quizzes