Debugging an ABAP Program

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Identify and correct problems in an ABAP application using the Debug perspective

Start Using ABAP Debugger

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 values of variable 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 you program and stop at that set breakpoint.

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

Features of the ADT Debugger

FeatureADT Debugger
Setting BreakpointsYes
WatchpointsYes (Depending on release)
Conditional breakpointsYes
Stepping through codeYes
Displaying variable valuesYes
Analyzing internal tablesInternal table rows can be displayed and edited in the ABAP Internal Table view, but the view can be customized.
Displaying structuresStructures can be displayed by field, but the view cannot be customized.
Displaying objectsStructured display of object contents is supported, but the capabilities for customizing the data display of the Objects tool are not offered.
Comparing data objectsnot for complex data objects
Analyzing memory useNo
Debugger scriptingNo
Change debugger settingsYes
Direct editing of code in the debuggerYes

Debug an ABAP Program using 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.

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 state of the program at exactly that moment.

Navigation Functions

While you are 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.

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 Shft+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 backwards, 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.

Watchpoints

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.

Create an ABAP Application

Business Example

We will now look at how to create an ABAP application. As a prerequisite, we need a package and a class. If you do not have either, you need to create them.

Steps

  1. Create a new class.

    1. Right-click on your package.

    2. Choose NewABAP Class.

  2. Name a program and add a description.

    1. In the Name field, enter ZCL_S4D100_DEBUG.

    2. In the Description field, enter Debug Program.

    3. Choose Next.

    4. Select the Transport request, and choose Finish

  3. Define the interface in the public section.

    1. Define the interface in the PUBLIC SECTION: IF_OO_ADT_CLASSRUN.

  4. Add code to the program.

    1. Add the following Loop code to the program using a D0-ENDDO loop and a WRITE statement.

    2. You can use the DO-ENDDO loop-to-loop through the LV_NUMBER1 and output the result with the statement OUT->WRITE( lv_number1 ). When you are writing the code, the following should be taken into consideration:

      • There is no space between the write and opening parenthesis
      • There is at least one space after the opening parenthesis
      • There is at least one space before the closing parenthesis
      • There is a period at the end of the line
  5. Activate the program and clear the console.

    1. Activate your program.

    2. Run the application by pressing F9.

    3. Select the Clear Console button in the top-right row to clear the console contents.

      Practice

Debug a Program and Set a Watchpoint

Business Exercise

We will now look at how to set a breakpoint and create a watchpoint. There are a couple of different ways to set a breakpoint in an application program.

Steps

  1. Double-click on the program, ZCL_S4D100_DEBUG, and change the code to look like what is shown in the following figure and activate the program.

    1. Set a breakpoint at Line 16. To set a breakpoint at Line 16, double-click to the left of the column number on the line of code or right-click to the left of the column number and choose Toggle Breakpoint to set a breakpoint at Line 16.

    Result

    Result:

  2. Execute the application program.

    1. Execute the application program by selecting F9 (on your keyboard).

      The blue arrow and the highlighted line (at Line 16) indicate the statement.

    2. Double-click on the variable, LV_NUMBER1 (expand the Locals Node).

  3. Execute the code.

    1. Press F5 on your keyboard (single step) to take a look at the changed value for LV_NUMBER2 from 0 to 3.

    2. Press the F8 (Resume) button.

  4. Clear the contents.

    1. Notice the output is displayed in the ABAP console. You can clear the contents by choosing the Clear Console button that is located on the toolbar in the console.

  5. Create a watchpoint for a variable.

    1. To create a watchpoint on a variable, you must be in the debugger. Set a breakpoint at Line 16.

    2. Execute the program by selecting F9 (on your keyboard).

    3. While in the Debugger, highlight the variable LV_NUMBER1 and right-click on it, and choose Set Watchpoint.

  6. Select the variable in the breakpoints list.

    1. Double-click on the variable, LV_NUMBER1.

    2. Select the Breakpoints tab.

      If the Breakpoints tab does not automatically display itself, choose the Breakpoints button.

    3. At this point, you can add a condition.

    4. Double-click on the variable, LV_NUMBER1.

    5. Add the condition (= 6) and save the condition by selecting Save.

      Note
      Ensure that you have a space between = and 6
  7. Stop the Debugger at the watchpoint.

    1. Double-click on the variable, LV_NUMBER1, in the ABAP editor.

    2. Press F5 (Step Into) on your keyboard.

    3. Select the Variables tab.

    4. Expand the watchpoint LV_NUMBER1 and note the variable LV_NUMBER2 has changed to 3.

    5. Press the F8 Resume button on your keyboard.

      Result

      Result: The watchpoint stopped when the LV_NUMBER1 was equal to 6.

    6. Explore the practice.

      Practice

Log in to track your progress & complete quizzes