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 “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.
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 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 button and load the script that you created. 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!
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
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
Nice blog and Thanks for your initiative on this topic....
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
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.
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.
Please let me know if that cleared up your issue! Thanks!
-Brian
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
@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 ?
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?
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.
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.
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
I responded to Suhas' question on my other post at Tracing a Program with the ABAP Debugger Script
Thanks A lot Brian for sharing it...keep them coming:)
Thank you!
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.
Good information Brian...
Great blog. Never thought a script could be use to solve such problem. well done
Nice doc Brian 🙂 ,thanks for sharing
I have been told about scripting ,
but this is the first time tried along reading your document.
Thanks.
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
You should check if the ABAP debugger scripting functionality is not available in the ABAP release.
Afaik, it is available from 7.0 Ehp2
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
How can i create watchpoint for field-symbol on ABAP in Eclipse?
Â
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/
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.