Skip to Content

This is my second post on practical uses of the ABAP debugger script. My first post is on how to create a watchpoint for a field symbol.

You can also find my third post at Skip the Authority Check with the ABAP Debugger Script

I remember working on an undergraduate college programming project in an IDE where we did not have a debugger. When something was not working right, we would add write line commands to try to find where things were going wrong causing the program to act incorrectly. In most cases, interactive debugging can be a much faster way to figure out what is working or not working. Recently I was working on an issue with a program which had pretty complicated logic, so I wanted a print out of what was happening in specific scenarios, so that I could discuss it with a business analyst. Previously, I would step through the program for each scenario in debug mode and write the results of the logic on a piece of paper.

Instead of using this paper based process, I decided to try to find a way to accomplish this using the ABAP debugger script. Within the debugger script, you can write out custom trace records, which can then be viewed by other users on the system in transaction SAS. I will explain how I did this, but I am assuming that you already know the ABAP debugger script basics that I covered in my earlier post. For this example, I will us a simple FizzBuzz program, which will print the word “Fizz” for ever number divisible by 3 and “buzz” for every number divisible by 5 up to 100. The code is displayed below.

REPORT  z_fizzbuzz.
DATA: d_i TYPE i.
DO 100 TIMES.
  d_i = d_i + 1.
  IF d_i MOD 15 = 0.
    WRITE: 'FizzBuzz'.
  ELSEIF d_i MOD 3 = 0.
    WRITE: 'Fizz'.
  ELSEIF d_i MOD 5 = 0.
    WRITE: 'Buzz'.
  ELSE.
    WRITE: d_i.
  ENDIF.
  NEW-LINE.
ENDDO.

WRITE: 'Done'.

Create a script using the script editor in transaction SAS using the below code for the script method. Leave the default “Debugger Single Step” checkbox checked. Note that all of the added method calls were found using the script wizard.

METHOD script.
    DATA: ld_program TYPE sy-repid,
          ld_found TYPE boolean,
          ld_line TYPE i,
          ld_action TYPE string,
          ld_value TYPE i,
          ld_trace_string TYPE string,
          ld_trace TYPE tpda_trace_custom.
    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 = 'Z_FIZZBUZZ'.
      ld_found = abap_false.
      TRY.
          CALL METHOD abap_source->line
            RECEIVING
              p_line = ld_line.
        CATCH cx_tpda_src_info .
        CATCH cx_tpda_src_descr_invalidated .
      ENDTRY.
      CASE ld_line.
        WHEN 6.
          ld_action = 'Increment Number'.
          ld_found = abap_true.
          "Step debugger so we get updated number
          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.
        WHEN 9.
          ld_action = 'Display FizzBuzz'.
          ld_found = abap_true.
        WHEN 11.
          ld_action = 'Display Fizz'.
          ld_found = abap_true.
        WHEN 13.
          ld_action = 'Display Buzz'.
          ld_found = abap_true.
        WHEN 15.
          ld_action = 'Display Number'.
          ld_found = abap_true.
        WHEN 21.
          me->break( ).
      ENDCASE.
      IF ld_found = abap_true.
        TRY.
            CALL METHOD cl_tpda_script_data_descr=>get_simple_value
              EXPORTING
                p_var_name  = 'd_i'
              RECEIVING
                p_var_value = ld_value.
          CATCH cx_tpda_varname .
          CATCH cx_tpda_script_no_simple_type .
        ENDTRY.
        ld_trace_string = |Action: { ld_action }|.
        ld_trace-value = ld_trace_string.
        ld_trace-id = ld_value.
        CALL METHOD trace->add_custom_info
          EXPORTING
            p_trace_entry = ld_trace.
      ENDIF.
    ENDIF.
  ENDMETHOD.                    "script

Now, before running the program, type “/h” in to the command line to enter debug mode. Click the Load-Script-Button.png button to load your script and click Start-Script-Button.pngto start it. Once it gets to the end of the program, you will see the trace results in the debugger as pictured below. Click  Exit, to stop the script and quit the debugger. You must quit the debugger in order for the trace file to be saved.


Custom Trace Results1.png

You can now view your trace file from transaction SAS. Other users can also access your trace files from this transaction. Since these files are saved on the server, make sure that you delete them once they are no longer needed. To view your trace file, double click the file that you created located in the Trace files tab of transaction SAS. Then click the User Specific Trace Button.png button to see your custom trace results and you will now see your trace as displayed below. From here, you can export the results as a file, filter on a column (notice that I set the custom value column) or search for a particular record.

Custom Trace Results2.png

This use of the ABAP debugger script saved me a ton of time while working with someone on a recent project. We were able to run it and quickly find out why certain items were displaying on a report or why they were being hidden. Have you done something similar with the debugger script? Do you have any suggestions on ways to improve this method of creating a log? Please leave a comment below!

To report this post you need to login first.

11 Comments

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

  1. Suhas Saha

    Hello Brian,

    I think you must be aware of the 999 line item issue with accounting documents. And we have a logic to split the records in case of > 999 line items.

    I am trying to perform unit test but unfortunately i can’t due to lack of data in DEV. I was thinking if we can use debugger scripts to add items to the table?

    BR,

    Suhas

    (0) 
    1. Brian O'Neill Post author

      Hi Suhas,

      You definently can! Click on the script wizard and go to Variable Information->Change Variable Value->Table->Append Table Line. There are also methods available for Insert Table Row (by index or key).

      Hopefully that works for you! Let me know if it does not!

      -Brian

      (0) 
  2. Helder da Costa

    Interesting.

    I haven’t used the debugger script yet. When i first heard about it, i thought “What a cool feature”. Then i went and checked the videos about it, i played around a bit with it, but i struggled to find a use for it during a project.

    This is a good example indeed.

    Cheers,

    Helder

    (0) 
  3. zongjie zhang

    Hi Brian,

         This blog is very impressive! I have some question, Can I trace the dialog with the ABAP Debugger Script ? I want to know how the Diff tool in the Debugger work, I want to trace this process of this function. As I know, this Diff tool in the Debugger is a dialog, So I wonder if you know how to trace one dialog ?

    Thanks

    zhang

    (0) 
  4. Airwave Ertas

    Hi Brian, thank you for this helpful blog.

    Is there a possibility to enhance this requirement via debugger scripting.

    Break/Stop when you find this string ‘9999 in any variable (local/global/nested objects etc.)

    I am missing the option to work more generic/dynamicly.

    Mostly the methods of cl_tpda_script_xxxx are expecting fix variable names etc.

    Best Regards

    Hakan

    (0) 
    1. Brian O'Neill Post author

      Hi Hakan,

      sorry, but I can’t think of a way to check all variables. I would recommend stepping through the program in debug to find some suspects and get a better understanding of the program logic. Good luck!

      -Brian

      (0) 

Leave a Reply