Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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 "Wacth points on Field symbols" 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, I do not like being told what I can't do in SAP, so I decided to find a way!

watchpoint 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  Transaction SAS script editor tab 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 Script Wizard Button 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.

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

Lastly, we only want to check if the field symbol has changed within the scope of our zprogram. In order to do this, place the cursor in your script method, but before the get_simple_value method call and click on the Script Wizard Button button again and expand the folder titled "Source Code Information" and then expand the "Fast Access to Source Code Information" folder below that and finally select the "Program" script. Create a variable of type sy-repid to store the retrieved program name and use it as the P_PRG receiving parameter.  Add an IF statement to confirm that your program variable matches the program name that you want to set as the scope to do this test. The code mentioned earlier should be contained within this IF statement. An example of this is given below.

****************************************************************
*Interface (CLASS = CL_TPDA_SCRIPT_ABAPDESCR / METHOD = PROGRAM )
*Returning
*        VALUE( P_PRG ) TYPE SY-REPID
****************************************************************
     TRY.
         CALL METHOD abap_source->program
           RECEIVING
             p_prg = ld_program.
       CATCH cx_tpda_src_info .
       CATCH cx_tpda_src_descr_invalidated .
     ENDTRY.
     IF ld_program = 'ZProgram'.
       "Insert above script here
ENDIF.


Make sure that the radio button "After Debugger Events" is selected and the checkbox for "Debugger Single Step" is checked (pictured below).

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.


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 Load Script Button button and load the script that you created. Once you are ready, click the Start Script Button 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!

Related posts I wrote on practical uses of the ABAP debugger script: Tracing a Program with the ABAP Debugger Script, Skip the Authority Check with the ABAP Debugger Script

----

Update 3/20/13

Thank you to neil.woodruff for noticing a small issue in my post! I originally was using this technique to test for a specific value and still needed to update it to make it a fully functioning watchpoint.

Added - Set scope for script method to only run on specified zprogram and set trigger to be based on debugger single step

25 Comments