Skip to Content

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).

debugger trigger.png

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

To report this post you need to login first.

21 Comments

You must be Logged on to comment or reply to a post.

  1. Fred Verheul

    Hi Brian,

    Great blog! I’ve never given much consideration to this debugger scripting thing since I had no clue what it could be used for, and you have now solved this 🙂 .

    I’m gonna try it out today!

    Thanks for sharing, and I’m looking forward to your next blog posts.

    Cheers, Fred

    (0) 
  2. Neil Woodruff

    Hi Brian,

    this was very interesting  but I’m not sure it worked the way I expected. The key was:Instead, select breakpoint reached and set an appropriate breakpoint. How would you expect it to work for the ‘appropraite breakpoint’? Only stop there when the value changed? For me it always stopped there which it would have done anyway…and the script was running. maybe there was an error in my script code but I could not pick it.

    (0) 
    1. Brian O'Neill Post author

      Hi Neil,

      Make sure that you are setting the breakpoint in the debugger script tab (pictured below), not the standard breakpoint section. Sounds like I may have went through this part too fast, so I will update the post ASAP.



      Also, make sure that your script method is set to break only when the value changes like below. There is a me->break() command by default in the script method, so make sure that has been removed.

      •      IF ld_vbeln <> d_vbeln. 
      •        d_vbeln = ld_vbeln. 
      •        me->break( ). 
      •      ENDIF. 

      Please let me know if that cleared up your issue! Thanks!

      -Brian

      (0) 
      1. Neil Woodruff

        Thanks Brian, I did have the ‘trigger’ parameters set as you describe…the script method did have the value change logic in it…..T think I tried setting breakpoints within and without the script….I will try and revisit this next week if I get the time. Cheers, Neil

        (0) 
  3. Brian O'Neill Post author

    Also, I think I believe you can run this on every step, just add a check in the beginning so it only runs in your program to speed things up. I updated the post to show this method.

    (0) 
  4. Suhas Saha

    Thanks Brian … There are a few articles on debugger scripts on SDN & thanks sharing your experience with us.

    I have to replicate the 999 line item issue with accounting documents & i am planning to use scripts to replicate this scenario. Do you think this is possible?

    BR,

    Suhas

    (0) 
  5. Joachim Rees

    Brian, thank you very much for that introduction!

    I have the impression, that “debugger scripting” is not yet widely used or known…

    Here’s my idea for a use-case for debugger scripting:

    in SE16n

    1. set gd-edit to ‘X’

    2. create a watchpoint on gd-edit

    3. if that watchpoint is reached (gd-edit is no longer ‘X’) set it to ‘X’ again.

    Best

    Joachim

    (1) 
    1. Brian O'Neill Post author

      Hi Eliram,

      The debugger script is only available in the gui at this time. However, maybe what you are trying to do is stop at specific record in at LOOP AT…ASSIGNING statement. If so (and it is not a hash table), you could set a conditional breakpoint right after your statement that should stop if sy-tabix is equal to the row you are looking for.

      Here is a blog on conditional breakpoints: https://blogs.sap.com/2015/10/22/conditional-breakpoints/

      (1) 

Leave a Reply