Does detection work under AQTime? Avoid crashing debug code

I am using Profiler Profiler in AQTime. Trying to run it in the IDE (using Embarcadero RAD Studio XE). Detected project failures in this code:

// Setting a Thread Name (Unmanaged):
// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.71).aspx
procedure _NameThreadForDebugging(const ATID: Cardinal; const AThreadName: String);
type
  TThreadNameInfo = record
    FType: LongWord;     // must be 0x1000
    FName: PAnsiChar;    // pointer to name (in user address space)
    FThreadID: LongWord; // thread ID (-1 indicates caller thread)
    FFlags: LongWord;    // reserved for future use, must be zero
  end;
var
  ThreadNameInfo: TThreadNameInfo;
  ThreadName: AnsiString;
begin
  // Applicable only for debugged applications
  if IsDebuggerPresent then
  begin
    FillChar(ThreadNameInfo, SizeOf(ThreadNameInfo), 0);

    ThreadName := AnsiString(AThreadName);
    ThreadNameInfo.FType := $1000;
    ThreadNameInfo.FName := PAnsiChar(ThreadName);
    ThreadNameInfo.FThreadID := ATID;

    try
      RaiseException(cSetThreadNameExcep, 0, SizeOf(ThreadNameInfo) div SizeOf(LongWord), @ThreadNameInfo);
    except
    end;
    Finalize(ThreadName);
  end;
end;

      

It works great when run outside of the IDE (in which case the program will exit without any action) or when run under the normal IDE debugger (in which case the subroutine will throw an exception that will be handled by the IDE debugger).

However, when run under AQTime, the routine will crash immediately when called using the kernel32.RaiseException procedure (APPCRASH C00001A5 somewhere inside kernel32). I confirmed this by putting MessageBoxes around this call (try / except block).

Apparently IsDebuggerPresent is True when run on AQTime, but the exception is not being handled as expected.

The question is, how can I detect and avoid this? How to check if the code is executing in AQTime?

AQTime - 8.22.

+3


source to share


1 answer


You can check the following environment variables, which by default are transmitted profiled process: AQTIME_DEBUGGER_PRESENT

, AQTIME_SESSION_ID

and AQTIME_VERSION

.



Note: the exact variables seem to depend on the profiler used (or the version of AQTime?) - in my case only AQTIME_SESSION_ID

and are present AQTIME_VERSION

.

+5


source







All Articles