SAP ABAP Debugger Scripting is a new tool added in SAP Netweaver 7.0 EHP2. It is a feature available in the new ABAP debugger. However, just like some of the other debugger options, you need to check whether the security team has provided access for the debugger script tool.
Most of ABAP programmers regularly use Break-Points & Watch-Points for debugging. These are necessary in an ABAP programmers role to find the bugs in custom developments, analyze the Standard SAP program or find a BADi for that matter. So where does this Debugger scripting come into play?
Designed To Make Debugging ABAP Code Easy
The debugger scripting is a tool is designed to make debugging easy. Sometimes debugging can be very tiresome, especially when debugging SAP standard code is involved. The debugger scripting tool can sometimes come to the rescue of ABAP programmers, as it helps automate the process of debugging.
Some of the benefits of Debugger Scripting are:
- Automation of repeatedly changing the value of some variable in debugging – You can write a script to do all the changes required.
- Perform any custom trace possible – Even track the call to the ABAP stack. It can be used to trace when new programs are called, as the ABAP stack is updated every time a new program is called.
- Create custom (conditional) watch-points and break-points. script watch-points & break-point are different from the normal debugger watch-points or break-points. Scripts can be implemented for these to work only when required.
- Program Tools for standard tasks like checking data consistency, test error handling, etc.
- It can help make the debugging to be user interactive (pop-up based) where one can enter the value, rather can each time double clicking on the variable to change the value. Since the script can be saved and loaded again, this makes the task for programmer easier to use.
A Debugger script is simply a local Object Oriented ABAP program that is run by the New ABAP Debugger. The script uses the ADI (ABAP Debugger Interface) to automate actions and to add capabilities that otherwise would not be available.
So Let’s Take A Look At The Debugger Scripting Tool.
In the new ABAP debugger there is a separate TAB “Script” after the TAB “Diff”(Diff-Tool).
In this TAB, are various options for creating and executing an ABAP script. Let us explore these options.
There is a standard create button, to create a new script, as per our debugging requirement. Each new script that is created has four methods that can be implemented. These methods are triggered at specific moments and thus it defines their purpose. Also as you can see in Fig 1, all this code is generated automatically whenever a script is created.
A class “LCL_DEBUGGER_SCRIPT” is implemented, which has:
- Method PROLOGUE
After the script is started, the Debugger framework first calls the PROLOGUE method of the script. The default implementation registers the ABAP_SOURCE instance to the DEBUG_STEP event so that the source code information you get from the ABAP_SOURCE instance is always up-to-date.
- Method INIT
INIT method is called only once, directly after the PROLOGUE method. You use this method for initialization. It is also a place to add user interaction, for example, a dialog box, where you ask the user for input, which you need for the script.
- Method SCRIPT
SCRIPT method is where we write the Object Oriented ABAP code that executes. This method is called each time the debugger script executes. It has an importing parameter p_trigger, which holds the trigger type that caused this script to execute (Breakpoint, Watchpoint, Single Step, Single Run).
- Method END
When the script stops, END method is called.
Besides these custom methods, the Script Super class also includes two already implemented methods :
This returns the control to the user. You can stop the script or continue here.
This method is used to exit a script, with respective message displayed to the user.
Like the standard ABAP Editor, in Debugging Script Tool also there are buttons for syntax check, include patterns & pretty printer. Besides these there is a button for the Script Wizard, that can used to generate the code automatically so that the programmer doesn’t have to write the entire code, you can just modify the generated code, based on your specific requirement.
The triggering of debugger script can be done in a number of ways.
- It can be Triggered directly.
- It can be triggered when a Breakpoint is encountered while executing the code.
- It can be triggered when a Watchpoint is encountered while executing the code.
- It can be triggered after each debugger step.
Since the debugger script is a local OOPs program that is executed in the new ABAP debugger, it can be easily saved for later use and loaded whenever required. Which removes the task (and hassle) of doing same things again and again as in a normal debugger.
As tracing is a strong component of this tool, a button is specifically provided on the screen to display a trace. This inherently triggers a Transaction code SAS, used for viewing a debugger trace.
Let’S Have A Practical Example Of The Use Of Debugger Scripting Tool.
Normally watch-points are used to stop the execution of a program whenever the value of some variable changes or is different from the set value.
Suppose there is a scenario where an internal table should have 100 records, however because of some bug in code, some records get deleted. According to the traditional approach you would normally have to put a watch-point over the table and execute the code loop each time until you manually find where the record gets deleted. However using this Debugger script tool you can customize your watch-point, to stop only where a record is removed from the internal table. Let’s see how.
Consider the following code for populating the internal table and displaying the number of records it holds.
However when executed it displays only 99 records as opposed to the 100 records that was expected.
To find out where the issue is, you can write a script to find the bug, instead of executing you programs loop again & again one loop at a time.
- Execute the program in debugging mode and go to Script Tab to create a script.
- Choose a triggering mode as watch-point reached.
- Now select the Edit button to add a watch-point. Choose the internal table. (Note : A script watch-point is different from the normal debugger watch-point)
- Fetch the description of the internal table in a factory description holder.
- Extract the line count from it & compare it with the Saved_Count (holding loop count).
So if both the counts are not equal, the program will stop otherwise it will continue processing our code.
Pretty Damn Easy!
This is how a customized watch-point can be implemented through Debugger scripting.
The debugger scripting tool is quite a new tool for most ABAP programmers and it becomes very handy once you get the hang of it. It requires the knowledge of Object Oriented ABAP as a prerequisite, which in itself is something all ABAP programmers should learn. It can make the life of an ABAP programmer that little bit easier when faced with a difficult bug to solve, so give the tool a go. You might start to like it 🙂