GetThreadStackTrace returns nothing - Delphi XE3/XE5 64bit

delphi package - automated exception handling

GetThreadStackTrace returns nothing - Delphi XE3/XE5 64bit

Postby PavKa » Thu Oct 31, 2013 5:38 pm

Hi,
I have madCollection 4.0.8.1 installed.
Delphi XE3 or XE5 - one form VCL forms application.
madExcept.GetThreadStackTrace(); returns empty string if compiler target platform is Windows 64 bit
and returns proper stack trace if compiler target platform is Windows 32 bit

procedure TForm1.Button1Click(Sender: TObject);
begin
Vcl.Dialogs.ShowMessage(madExcept.GetThreadStackTrace());
end;

Please advise on the issue.

Thank you,
Pavel
PavKa
 
Posts: 12
Joined: Wed May 05, 2004 5:46 pm

Re: GetThreadStackTrace returns nothing - Delphi XE3/XE5 64b

Postby madshi » Thu Oct 31, 2013 7:49 pm

I've double and triple checked my code, changed a few things, but the underlying API (StackWalk64) doesn't seem to work right. I'm not sure why. From what I can see, I'm calling it correctly.

A google search seems to recommend raising a dummy exception to do a stack trace for the current thread. That seems to work nicely:

Code: Select all
function GetThreadStackTrace_Hacked() : UnicodeString;
begin
  try
    integer(nil^) := 0;
  except
    result := GetCrashStackTrace();
  end;
end;

Ugly, but I don't have a better solution at the moment.
madshi
Site Admin
 
Posts: 9546
Joined: Sun Mar 21, 2004 5:25 pm

Re: GetThreadStackTrace returns nothing - Delphi XE3/XE5 64b

Postby PavKa » Thu Oct 31, 2013 9:10 pm

Thank you for the quick answer - we will use recommended hack for now.

Also I am not sure, but should Wow64GetThreadContext be used for win64 instead of GetThreadContext in GetThreadInfos (madexcept.pas)?

Thank you,
Pavel
PavKa
 
Posts: 12
Joined: Wed May 05, 2004 5:46 pm

Re: GetThreadStackTrace returns nothing - Delphi XE3/XE5 64b

Postby madshi » Thu Oct 31, 2013 9:16 pm

The information returned by GetThreadContext() is not actually used for the x64 stack trace. Actually for the stack trace I'm using RtlCaptureContext(), which is the recommended method to initialize the context for x64 stack walking.
madshi
Site Admin
 
Posts: 9546
Joined: Sun Mar 21, 2004 5:25 pm

Re: GetThreadStackTrace returns nothing - Delphi XE3/XE5 64b

Postby jmarder » Fri Dec 29, 2017 12:55 pm

> but I don't have a better solution at the moment.

I wonder if anything has improved in the past 4 years regarding this issue?
Otherwise it could make sense to at least add the workaround to MadExcept.
jmarder
 
Posts: 12
Joined: Tue Sep 12, 2017 8:00 am

Re: GetThreadStackTrace returns nothing - Delphi XE3/XE5 64b

Postby madshi » Fri Dec 29, 2017 1:09 pm

I haven't tested if the problem is gone in newer OSs or not. If not, the workaround should still work. I'm not eager to add the workaround to madExcept because if I do, even if the underlying problem gets fixed in a future OS version, the dummy exception would always stay in madExcept.
madshi
Site Admin
 
Posts: 9546
Joined: Sun Mar 21, 2004 5:25 pm

Re: GetThreadStackTrace returns nothing - Delphi XE3/XE5 64b

Postby jmarder » Fri Dec 29, 2017 1:16 pm

> I'm not eager to add the workaround to madExcept

So you think it is better if many of your customers run into that issue, spend time in analyzing the issue and seeking a workaround, and then all copy this workaround to their projects?


> the dummy exception would always stay in madExcept.

It should be fairly easy to use it only below a certain Windows build number.
jmarder
 
Posts: 12
Joined: Tue Sep 12, 2017 8:00 am

Re: GetThreadStackTrace returns nothing - Delphi XE3/XE5 64b

Postby PavKa » Fri Dec 29, 2017 4:37 pm

I've just retested it with Tokyo 10.2 Update 1 - it does not work (still returns empty stack trace).

for years we use workaround:

Code: Select all
function GetStackTrace: string;

  // Re: GetThreadStackTrace returns nothing - Delphi XE3/XE5 64b
  // http://forum.madshi.net/viewtopic.php?f=4&t=27637&p=46729#p46729
  // Unread postby madshi » Thu Oct 31, 2013 7:49 pm
  // I've double and triple checked my code, changed a few things, but the underlying API (StackWalk64) doesn't seem to work right. I'm not sure why. From what I can see, I'm calling it correctly.
  // A google search seems to recommend raising a dummy exception to do a stack trace for the current thread. That seems to work nicely:
  function _GetThreadStackTrace_Hacked: UnicodeString;
  var
    vPos: integer;
  begin
    try
      Abort;
    except
      Result := madExcept.GetCrashStackTrace;
      // remove part of the stack generated by this call
      vPos := pos('GetStackTrace', Result);
      if vPos <> -1  then
        Result := copy(Result, vPos + 14);
    end;
  end;

begin
  // in case of exception need a crash stack - otherwise just current stack
  if ExceptAddr <> nil then
    Result := madExcept.GetCrashStackTrace
  else
    Result := _GetThreadStackTrace_Hacked;
end;


Thank you,
Pavel
PavKa
 
Posts: 12
Joined: Wed May 05, 2004 5:46 pm


Return to madExcept

Who is online

Users browsing this forum: No registered users and 3 guests