Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does DUnit2's CallerAddr function do, and how do I convert it to 64 bits?

I am trying to get DUnit2 working under 64 bits, but I am stumped to what this method does, let alone how to convert it to 64 bits. Pure Pascal would better, but since it refers to the stack (ebp), it might not be possible.

function CallerAddr: Pointer; assembler;
const
  CallerIP = $4;
asm
   mov   eax, ebp
   call  IsBadPointer
   test  eax,eax
   jne   @@Error

   mov   eax, [ebp].CallerIP
   sub   eax, 5   // 5 bytes for call

   push  eax
   call  IsBadPointer
   test  eax,eax
   pop   eax
   je    @@Finish

@@Error:
   xor eax, eax
@@Finish:
end;
like image 842
Nicholas Ring Avatar asked Aug 18 '12 22:08

Nicholas Ring


1 Answers

function RtlCaptureStackBackTrace(FramesToSkip: ULONG; FramesToCapture: ULONG; 
  out BackTrace: Pointer; BackTraceHash: PULONG): USHORT; stdcall; 
  external 'kernel32.dll' name 'RtlCaptureStackBackTrace' delayed;

function CallerAddr: Pointer;
begin
  // Skip 2 Frames, one for the return of CallerAddr and one for the
  // return of RtlCaptureStackBackTrace
  if RtlCaptureStackBackTrace(2, 1, Result, nil) > 0 then
  begin
    if not IsBadPointer(Result) then
      Result := Pointer(NativeInt(Result) - 5)
    else
      Result := nil;
  end
  else
  begin
    Result := nil;
  end;
end;
like image 140
pani Avatar answered Nov 17 '22 00:11

pani