Friday, June 22, 2012

Basics of Debugging in SAP



Debugging on SAP:

Debugging is one of the best and wonderful tool used by the developer(In any development Environment) to track down a issue and also to understand code or logic written by any other developer.

As we know, since its being a technical stuff, not all Functional Consultants will have an idea about it, so they have some tough time initially to understand as what it is and how it works, but once they understand it, its like piece of cake to any person.

I have put some basic information regarding debugger and some basic concepts about debugger, going over these might help you in not getting panic when you see a debugger screen :).

Note: In SAP, there are 2 types of debuggers. One is Classic debugger and the other one is New Debugger. Classic debugger is lot easy to use and also to understand but New debugger(available from release levels 6.40) is has more features and also will be used vastly in future, so I am going over New debugger in my blog. 

Some brief differences between these 2 types are, (Courtesy of SAP)

  • The Classic Debugger runs in the same roll area as the application to be analyzed (debuggee). It is therefore displayed in the same window as the application. However, this technology also has some restrictions. For example, some ABAP programs (such as conversion exist) cannot be analyzed in debug mode for technical reasons. However, the most significant restriction is that no ABAP technology can be used for designing the Debugger interface and it is therefore not possible to create a modern user interface.
  • The New Debugger on the other hand, is executed in a separate external session (Debugger), while     the application to be analyzed (debuggee) uses a second external session. With this technology, the user interface of the Debugger can be designed freely by ABAP means.


About New Debugger:

SAP's old debugger which is Classic debugger was user-friendly but has some cons as described above. This New Debugger is a lot different to old debugger, just not in look but also in features.

List of few features available on New Debugger are:

1. Ability to Configure the look of the debugger screen.
2. Multiple Desktops options to go over multiple types of data while debugging.
3. Access to all the Global Variables of the report or function module which we are debugging. 
4. Tab Layouts to easily access multiple information which is necessary while debugging. In Classic debugging, to get to some of the information, we have to navigate through Menu Options.
5. Advance features like Saving a test variants to functions modules while debugging, searching for a particular entry inside a table while in debugging, option to get to the updated source code being in debugging mode and few others.
6. More options while debugging Object Oriented code compared to Classic debugger.

Setting up a break-point or starting a debug session:

Option 1:

Program goes to debug mode when you enter /h in command prompt and then execute the report.

Option 2:

In the program place the cursor on a particular point where you want to debug and click on the stop button on the top. When you execute, program will stop here in Debug mode.

Option 3:

Using BREAK-POINT command inside the code. This is mostly used by the developer, if you are not the developer of that report, you cannot use this option.


Difference between the 2 Stop Buttons is, generally for debugging a report we use the one with no human icon on it. STOP icon with human icon on it will be used only for External Debugging, that is if your application is being called from out of SAP's current session. Example cases are:

1. If we are debugging a Webdynpro application which actually runs on an Internet Explorer browser, we use the External Debugging button to put a break-point.
2. If we want to debug any Portal or PI Application Call, we use External Debugger.

This doesn't mean that you cannot use that for normal report debugging, we can set a break-point using this button too for a normal debugging.

Also, for the below screen shots, I have configured my debug screen to show Code on the left part of screen and the variables data on the right part of the screen, Actually this is the standard layout, but you can change this layout to have 4 screens on a layout..

Once you are in Debug Mode,




On the left screen you will see the code and on the right screen you can see the values of the variables at that point of the program. Like, if you see on the right side p_copy variable has value 001 at this point of the program execution.
Here, to debug step by step, we can use the buttons on the top of debugger screen which are,
If you want to go Step-by-Step either click on F5 or click on first button of above.
If you want to execute a complete step(Like a Function Module called in program without going into the function module, click on second button or F6.
F7 is to come out of step. Like you have gone into a function module, now to come out of it without going through the whole logic, use F7.
F8 can be used to come out of debugger and continue execution normally.

Using the above commands you can navigate through each point of the code and keep checking the main variable which you think has an issue or the main variables which are shown in the output of the report.

Important concepts to know while debugging:

1. One of the most helpful and used property of a debugger is, ability to change values of the variable while in debugging. I can explain that in brief in below screen shots:







In the above screen shot, I am inside a Loop of an internal table, one of the field of that table has a value of '01', as shown below



I have some code to check when that value becomes '02'(LI_CNODID_LINE-NODE_ID), suppose I dont have test case for that, what I can do is click on that Pencil Icon next to that field and you will see that field as input enabled and you can change the value of that field to 02 as shown below,







If you see the above screen, that pencil icon changed to DISPLAY icon and the field is input enabled. Once you change the value of that field you can just hit enter and the value gets changed.

Most important point is, you would need authorizations to do that :), most of the functional people will not be provided authorization to do that.

2. Setting up a break-point at a particular ABAP Command or a Statement. - If we know that the program is failing at a particular command, we can use the below option to set the break-point at that particular statement instead of debugging through the whole code.

When you are in debugger mode and if you want to set a break-point at any of abap command you can do it based on the navigation screen shot below.



 On the right part of screen, if you see Tabs which say Variables1, Variables3, Locals and Globals, in that Globals shows the list of values of all the variables used in that program at that point of time.

We have some notes under help.sap.com too about the Classic and the New Debugger. Below is the link for the same.