Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
Showing results for 
Search instead for 
Did you mean: 
Former Member

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.
  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'.
    WRITE: d_i.

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.
        CALL METHOD abap_source->program
            p_prg = ld_program.
      CATCH cx_tpda_src_info .
      CATCH cx_tpda_src_descr_invalidated .
    IF ld_program = 'Z_FIZZBUZZ'.
      ld_found = abap_false.
          CALL METHOD abap_source->line
              p_line = ld_line.
        CATCH cx_tpda_src_info .
        CATCH cx_tpda_src_descr_invalidated .
      CASE ld_line.
        WHEN 6.
          ld_action = 'Increment Number'.
          ld_found = abap_true.
          "Step debugger so we get updated number
              CALL METHOD debugger_controller->debug_step
                  p_command = cl_tpda_script_debugger_ctrl=>debug_step_into.
            CATCH cx_tpda_scr_rtctrl_status .
            CATCH cx_tpda_scr_rtctrl .
        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( ).
      IF ld_found = abap_true.
            CALL METHOD cl_tpda_script_data_descr=>get_simple_value
                p_var_name  = 'd_i'
                p_var_value = ld_value.
          CATCH cx_tpda_varname .
          CATCH cx_tpda_script_no_simple_type .
        ld_trace_string = |Action: { ld_action }|.
        ld_trace-value = ld_trace_string.
        ld_trace-id = ld_value.
        CALL METHOD trace->add_custom_info
            p_trace_entry = ld_trace.
  ENDMETHOD.                    "script

Now, before running the program, type "/h" in to the command line to enter debug mode. Click the button to load your script and click to 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.

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

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!