Skip to Content
Author's profile photo Brian O'Neill

How to Create a Watchpoint for a Field Symbol in the ABAP Debugger

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

Assigned Tags

      25 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Fred Verheul
      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

      Author's profile photo abilash n
      abilash n

      Nice blog and Thanks for your initiative on this topic....

      Author's profile photo Brian O'Neill
      Brian O'Neill
      Blog Post Author

      Thank you! I just posted about creating a custom trace using the debugger script at Tracing a Program with the ABAP Debugger Script

      I'm hoping that is just as useful!

      -Brian

      Author's profile photo Neil Woodruff
      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.

      Author's profile photo Brian O'Neill
      Brian O'Neill
      Blog 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

      Author's profile photo Neil Woodruff
      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

      Author's profile photo Jitendra Soni
      Jitendra Soni

      @Brian O'Neill,

      Thank you Brian for sharing this useful blog. I see that links to some images(e.g. your reply above) are not accessible. Could you please update them ?

      Author's profile photo Brian ONeill
      Brian ONeill

      Oh no, it looks like I don’t have access to the account I originally had and I took down the site I was hosting these images on awhile ago... I will see what I can recover. Is there any specific ones you were looking for?

      Author's profile photo Jitendra Soni
      Jitendra Soni

      Hi Brian ONeill ,

      Thanks for responding.

      Actually I am stuck at

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

      Followed all the steps but still debugger is not stopping in the script where code is written to check difference in the value of local and global data. so I was wondering where to put break point in the script.

      I tried to set break point like we did in abap editor but getting error "Break point can only be set in Active and unchanged code". I see script is activated and no changes are done in that but still not able to set the break point.

      Thanks for your help.

      Author's profile photo Brian O'Neill
      Brian O'Neill
      Blog 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.

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Brian O'Neill
      Brian O'Neill
      Blog Post Author

      I responded to Suhas' question on my other post at Tracing a Program with the ABAP Debugger Script

      Author's profile photo Nabheet Madan
      Nabheet Madan

      Thanks A lot Brian for sharing it...keep them coming:)

      Author's profile photo Brian O'Neill
      Brian O'Neill
      Blog Post Author

      Thank you!

      Author's profile photo Ramesh Babu Srikakollu
      Ramesh Babu Srikakollu

      Thanks Brian for the blog, nice and informative. It is a new learning for me about the debugger script and the watch point creation for the field symbol.

      Author's profile photo Modadugu Hemanth Kumar
      Modadugu Hemanth Kumar

      Good information Brian...

      Author's profile photo Ahmud Shabir Bheekun
      Ahmud Shabir Bheekun

      Great blog. Never thought a script could be use to solve such problem. well done

      Author's profile photo Former Member
      Former Member

      Nice doc Brian 🙂 ,thanks for sharing

      Author's profile photo sriharsha parayatham
      sriharsha parayatham

      I have been told about scripting ,

      but this is the first time tried along reading your document.

      Thanks.

      Author's profile photo Former Member
      Former Member

      Hi Brian ,

      Will this also work for Component version SAP ECC6.0 with system 700, 710, 701, 702, 703, 711, 720, 730 ??

      Thanks,

      Best Regards,

      Praveen Srivastava

      Author's profile photo Former Member
      Former Member

      You should check if the ABAP debugger scripting functionality is not available in the ABAP release.

      Afaik, it is available from 7.0 Ehp2

      Author's profile photo Joachim Rees
      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

      Author's profile photo eliram yakar
      eliram yakar

      How can i create watchpoint for field-symbol on ABAP in Eclipse?

       

      Author's profile photo Brian O'Neill
      Brian O'Neill
      Blog 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/

      Author's profile photo Former Member
      Former Member

      I am late to this golden post. Thanks a bunch Brian for sharing this lovely trick with detailed step by step instructions! I was able to see the magic of watchpoint on fieldsymbols.

      Question: I see that when I save it to database, I am prompted to provide a transport request.

      I can move a transport of copy from DEV to QAS system to take the benefits of my script while testing in QAS. But if I have an issue in PRD, I believe I won't be able to use this approach as I

      can't move a transport to PRD just for debugging unless its a very high priority PRD issue. My QAS

      or PRD systems are not modifiable. So I am not able to use load file option.

      It will be really cool if we had the provision to leverage this trick in PRD system as well.