3

I usePerformance Profiler in AQTime. Trying to run it under IDE (using Embarcadero RAD Studio XE). Inspected project crashes on such 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 fine when run outside of IDE (in which case the routine will exit without doing anything), or when run under usual IDE debugger (in which case the routine will raise exception, which will be handled by IDE's debugger).

However, when run under AQTime - the routine will crash right at call to kernel32.RaiseException routine (APPCRASH C00001A5 somewhere inside kernel32). I have confirmed this by putting MessageBoxes around this call (try/except block).

Apparently, IsDebuggerPresent is True when run under AQTime, but exception is not properly handled.

Question: how can I detect and avoid this? How can I check if code is executed under AQTime?

AQTime is 8.22.

Alex
  • 5,477
  • 2
  • 36
  • 56
  • 1
    AQTime uses binary (executable is instrumented when it's loaded into memory), or sampling (*watching* the application from outside) profiler. So, your application is *under* the debugger only when the binary profiler is used (performance profiler). And there remains the only way, I'd say; find a function that will recognize the debugger that is attached to your app. process, because I highly doubt that AQTime would modify the executed code in a way that you could detect back in your code. Good question, with negative answer, I'm afraid (looking at the MSDN debugging functions). – TLama May 21 '15 at 22:03
  • 1
    Have you tried TThread.NameThreadForDebugging? It should do exactly what your code does, but is an inbuilt function. Probably no different behaviour in AQTime of course, it just might be useful to you to have to write less code. – David May 22 '15 at 14:21

1 Answers1

6

You can check the following environment variables that are passed to the profiled process by default: AQTIME_DEBUGGER_PRESENT, AQTIME_SESSION_ID and AQTIME_VERSION.

Note: exact variables seems to depend on profiler used (or AQTime version?) - in my case only AQTIME_SESSION_ID and AQTIME_VERSION are present.

David
  • 13,360
  • 7
  • 66
  • 130
Alex
  • 5,477
  • 2
  • 36
  • 56