Sunday, November 8, 2015

Detecting debugger in ABAP program

With relation to my recent post “How to determine simulation mode of DTP” where I was blogging about detecting debugger within BW’s transformation’s routines I was curious how to achieve the same with classic ABAP environment. Within the BW’s transformation it was simple there is a clear variable which simple evaluation get the answer weather the transformation is running in the debugger or not.

In case of pure ABAP environment it is not that simple. There are ABAP system field like SY-DEBUG and SY-BATCH which I immediately thought of. However at least in newer versions of NetWeaver (7.x) they seems not be functioning in case of evaluating whether the ABAP program runs in the debugger or not.

As next I found FM TH_GET_DEBUG_INFO which looked from description that it does the job: get debugging info… I tried to put following code into my report:

DATA: lv_dbg_cnt TYPE i.
DEBUGGING_COUNT= lv_dbg_cnt.
IF  lv_dbg_cnt EQ 2.
   write: / 'dbg is ON'.
  IF  lv_dbg_cnt EQ 1.
   write: / 'dbg is OFF'.

However this didn’t work out either.

As third thing I found FM SYSTEM_DEBUG_BREAKPOINTS. This FM calls C function called 'DEBUG_CNTL' which finally does the job. Actually the breakpoints seem to be stored somewhere within app server memory and the C function provides them. The above mentioned FM needs to be called up with input parameter MAIN_PROGRAM which’s values is the program that we want to evaluate whether it run in debugger.

Down below is simple call of the FM. In case TABLE parameter’s BREAKPOINTS has at least one row then the debugger is set in source code if input’s parameter MAIN_PROGRAM.

This solution will work in cases someone puts breakpoints manually in source code. Therefore I think it should cover all switches to debugger in case the breakpoints are set. However it won’t cover the case if someone just hits /h and continues to debug the code.

DATA: lt_breakpoints TYPE TABLE OF breakpoint.
   main_program                = 'ZMM_TEST'
   breakpoints                 = lt_breakpoints.

No comments: