Automate debugging using ABAP debugger scripts

I would like to share a very detailed and useful document guiding how to use Script in Debugging from SAPTechnical.

Introduction:-

There must be many situations in which you have to debug very complex SAP standard or custom code. It might be possible that you have to check all the RFC calls, database updates, authority checks etc.

But as you might very well know, that complex applications can have very large number of these statements. Checking the result of each statement in debugger can be very cumbersome. Using debugger scripts, you can automate the above process and pin point the particular statement in the code.

Example: Here, I have created a demo program which contains some ‘Select’ statements. We wish to check the statement, which is not returning SY-SUBRC = 0. Note that this is a test program and you might not appreciate the use of scripts here. But applying the same technique in large programs can increase your productivity significantly.

There are some select statements in the above program. We wish to know which statement is giving SY-SUBRC = 4.

Steps:-

1. Enter in debug mode in the code in which you want to check. Open the script tab.

2. Now, change the trigger of the script. Tick the checkbox ‘Breakpoint reached’.

3. Note that, the breakpoint for the script trigger is not the same one as we create in ABAP. For triggering in script, you would have to create your breakpoint through script.

Click on the ‘Change’ button in front of ‘Breakpoint Reached’ checkbox.

Create a breakpoint for ABAP statement ‘Select’.

4. Now that the trigger is created. We have to write the code to analyze the statement at which the script is triggered. All the code can be easily generated using ‘Script wizard’ and has to be written in method ‘Script’. Following are the steps which we need to perform using script.

- The script will trigger at the ‘Select’ statement. Execute this statement

- Get the value of the program variable ‘SY-SUBRC’.

- Analyze the sy-subrc value. If sy-subrc NE 0, we will trigger the breakpoint.

a. So, we will use script wizard to step over (execute) the current statement (‘Select’). Remember to pass the parameter for ‘F5’ from the flower box that we get after selecting the Debug step. (Refer to attached code)

b. Get the value of the system variable ‘SY-SUBRC’ using script wizard.

c. Check the value of the variable obtained and if it is not 0, then trigger the breakpoint. The attached code is the final code, which would be generated using script wizard and slight modifications.

  METHOD script.
*** insert your script code here
*1. Step over the current statement (Select)
*************************************************
* debugger commands (p_command):
* Step into(F5)   -> CL_TPDA_SCRIPT_DEBUGGER_CTRL=>DEBUG_STEP_INTO
* Execute(F6)     -> CL_TPDA_SCRIPT_DEBUGGER_CTRL=>DEBUG_STEP_OVER
* Return(F7)      -> CL_TPDA_SCRIPT_DEBUGGER_CTRL=>DEBUG_STEP_OUT
* Continue(F8)    -> CL_TPDA_SCRIPT_DEBUGGER_CTRL=>DEBUG_CONTINUE
*************************************************
****************************************************************
*Interface (CLASS = CL_TPDA_SCRIPT_DEBUGGER_CTRL / METHOD = DEBUG_STEP )
*Importing
*        REFERENCE( P_COMMAND ) TYPE I
****************************************************************
    TRY.
        CALL METHOD debugger_controller->debug_step
          EXPORTING
            p_command = cl_tpda_script_debugger_ctrl=>debug_step_into.
      CATCH cx_tpda_scr_rtctrl_status .
      CATCH cx_tpda_scr_rtctrl .
    ENDTRY.
*2. Get the value of the system variable 'sy-subrc'
****************************************************************
*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
****************************************************************
    DATA lv_subrc TYPE sy-subrc.
    TRY.
        CALL METHOD cl_tpda_script_data_descr=>get_simple_value
          EXPORTING
            p_var_name  = 'SY-SUBRC'
          RECEIVING
            p_var_value = lv_subrc.
      CATCH cx_tpda_varname .
      CATCH cx_tpda_script_no_simple_type .
    ENDTRY.
*3.Check the value of sy-subrc. Trigger point, if condition reached
    IF lv_subrc NE 0.
      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  

5. Check the syntax of the code and start the script (using the button on the left of Pattern’.

6. It will stop at the point where the select statement doesn’t return sy-subrc as 0.

So, the debugger has stopped at the position where SY-SUBRC is not 0.

7. Now, we have the option to either continue the script (to find more such select statements), or exit the script and manually debug from that point onwards. We will exit the script and go to debugger to check the SY-SUBRC value.

So, the SY-SUBRC is 4.

Note: If after starting the script, it goes to the output screen directly, you can press back and exit the script.

Summary:

The above technique can be used for other statements as well like RFC calls, Authority checks, call method etc.

Also, you can explore the script wizard and there are a lot of things which you can do. There are a lot of standard scripts available. You can either use them directly or modify them according to your requirement.

We have written the script on the fly. We can also save our script for reusability

We can also see the trace of our debugger script, which can be very useful if we are going for layer aware debugging.

Raymond Zhang
If you want to discuss with me about any idea, please contact me at raymond.zhang@sap.com

原文地址:https://www.cnblogs.com/rabbitzhang/p/2995229.html