I have known about the debugger script for sometime and have recently stumbled on what I think are some practical uses for it. The first example that I want to share is how to create a watchpoint for a field symbol using the ABAP debugger script. A simple search brought me to a discussion post titled "I do not like being told what I can't do in SAP, so I decided to find a way!
" where the responding parties all concluded that it is not possible to create a watchpoint for a field symbol. When I tried for myself, I received the error pictured below. Even though I received the error,
Transaction SAS
Transaction SAS is used to create the debugger scripts and view trace files. I will be talking more about trace files in my next practical use of the debugger script post. To create your script, click on the tab. Editor area of the screen should look familiar since it is nothing more than an ABAP editor. In the script method, remove the comment line "*** insert your script code here" and create a local data variable that matches the data type that you want to use for your watchpoint and then place the cursor on a new line. Next, click the button and double click the item titled "Variable Value (for Simple Variables)" under the Variable Information folder. Change the exporting variable P_VAR_NAME to match the variable that you want to set as a watchpoint and change the importing variable P_VAR_VALUE to store the results in your local data variable. In the end, the code should look like below.
METHOD script. DATA: ld_vbeln TYPE likp-vbeln. **************************************************************** *Interface (CLASS = CL_TPDA_SCRIPT_DATA_DESCR / METHOD = GET_SIMPLE_VALUE ) *Importing * REFERENCE( P_VAR_NAME ) TYPE TPDA_VAR_NAME *Returning * VALUE( P_VAR_VALUE ) TYPE TPDA_VAR_VALUE **************************************************************** TRY. CALL METHOD cl_tpda_script_data_descr=>get_simple_value EXPORTING p_var_name = '<ls_delivery>-vbeln' RECEIVING p_var_value = ld_vbeln. CATCH cx_tpda_varname . CATCH cx_tpda_script_no_simple_type . ENDTRY. me->break( ). ENDMETHOD. "script
Next, add a global data variable that matches the data type that you want to create a watchpoint for in the public section of the script class. This variable will be used to compare the variable when it changes. Then go back to the script method and add an if statement so that if the local data variable is different from the global data variable, then save the local variable in to the global variable and call the break( ) method. An example of this is given below. If you want to only stop at a certain condition, change the if statement to check for that condition. Click Save As to save your script to the database. You can also save scripts as a local file, but you can only load that file into modifiable systems.
CLASS lcl_debugger_script DEFINITION INHERITING FROM cl_tpda_script_class_super . PUBLIC SECTION. METHODS: prologue REDEFINITION, init REDEFINITION, script REDEFINITION, end REDEFINITION. DATA: d_vbeln TYPE likp-vbeln. ENDCLASS. "lcl_debugger_script DEFINITION CLASS lcl_debugger_script IMPLEMENTATION. METHOD prologue. *** generate abap_source (source handler for ABAP) super->prologue( ). ENDMETHOD. "prolog METHOD init. *** insert your initialization code here ENDMETHOD. "init METHOD script. DATA: ld_vbeln TYPE likp-vbeln. **************************************************************** *Interface (CLASS = CL_TPDA_SCRIPT_DATA_DESCR / METHOD = GET_SIMPLE_VALUE ) *Importing * REFERENCE( P_VAR_NAME ) TYPE TPDA_VAR_NAME *Returning * VALUE( P_VAR_VALUE ) TYPE TPDA_VAR_VALUE **************************************************************** TRY. CALL METHOD cl_tpda_script_data_descr=>get_simple_value EXPORTING p_var_name = '<ls_delivery>-vbeln' RECEIVING p_var_value = ld_vbeln. CATCH cx_tpda_varname . CATCH cx_tpda_script_no_simple_type . ENDTRY. IF ld_vbeln <> d_vbeln. d_vbeln = ld_vbeln. me->break( ). ENDIF. ENDMETHOD. "script METHOD end. *** insert your code which shall be executed at the end of the scripting (before trace is saved) *** here ENDMETHOD. "end ENDCLASS. "lcl_debugger_script IMPLEMENTATION
Execute the Script
To execute the script, enter debug mode while running the program you want to debug. Next click on the last tab titled "Script" and click the button and load the script that you created. If you leave the trigger set to debugger single step, the script will run for every executable line of code. Instead, select breakpoint reached and set an appropriate breakpoint. Once you are ready, click the button. You will notice that everytime the variable changes, the breakpoint will hit. You have sucessfully created a watchpoint for a field symbol using the debugger script.
I'm going to be looking for more practical uses of the ABAP debugger script. Follow me on twitter @boneill3 to get notified on my upcoming blog posts!