Recent

Author Topic: How to get GDB to transfer control to exception handler ?  (Read 1189 times)

440bx

  • Hero Member
  • *****
  • Posts: 826
How to get GDB to transfer control to exception handler ?
« on: March 05, 2019, 11:48:28 am »
Hello,

I have a simple exception handler to work around an occasional problem.  The code is as follows:
Code: Pascal  [Select]
  1.     {$ifdef UseWordAccessWorkaround}
  2.       try
  3.         { FPC v3.0.4 generates a word access for the expression below.  This  }
  4.         { causes an access violation if the last byte in the block is also    }
  5.         { last accessible byte.  The try/except is to work around that bug.   }
  6.  
  7.         HexPtr^ := HexDigits[p^ and $F];           { second nibble            }
  8.       except
  9.         b := p^;                                   { these instructions do    }
  10.         b := b and $F;                             { not generate a word      }
  11.         HexPtr^ := HexDigits[b];                   { access.                  }
  12.       end;
  13.     {$endif}
when the program is executed from the command line, the exception handler handles the exception and execution resumes normally.

When running the program under GDB, GDB gets "stuck" at
Code: Pascal  [Select]
  1.        HexPtr^ := HexDigits[p^ and $F];           { second
instead of transferring control to the exception handler.

the question is: how do I tell GDB to transfer control to the exception handler so execution resumes normally ?  F7/F8/F9 only seem to cause the exception again.

Attached is a complete sample program which executes as it should when invoked on the command line but gets stuck on the offending line as described above when run under GDB.

Thank you for your help.

PS: FPC v3.0.4, Windows 7 64bit. 
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Cyrax

  • Hero Member
  • *****
  • Posts: 727
Re: How to get GDB to transfer control to exception handler ?
« Reply #1 on: March 05, 2019, 12:05:49 pm »
Are you building a 32-bit or 64-bit binary?

440bx

  • Hero Member
  • *****
  • Posts: 826
Re: How to get GDB to transfer control to exception handler ?
« Reply #2 on: March 05, 2019, 12:12:47 pm »
Are you building a 32-bit or 64-bit binary?
Both.  Can't get past the exception in either bitness.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5389
    • wiki
Re: How to get GDB to transfer control to exception handler ?
« Reply #3 on: March 05, 2019, 01:20:47 pm »
It works on Windows 10 64 bit. (continue running)

IIRC, there are issues with either gdb, or rumour has it windows itself. That is I remember on my older system (Vista) I was not able to continue after such an exception.
I do not know what makes the difference.

You can try the following.
1) get gdb 8.2 from our sourceforge page.
2) use fpdebug (might need Lazarus trunk, might work on 2.0... not sure)

440bx

  • Hero Member
  • *****
  • Posts: 826
Re: How to get GDB to transfer control to exception handler ?
« Reply #4 on: March 05, 2019, 01:40:06 pm »
You can try the following.
1) get gdb 8.2 from our sourceforge page.
2) use fpdebug (might need Lazarus trunk, might work on 2.0... not sure)
Thank you Martin, I will try your suggestions. 
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5389
    • wiki
Re: How to get GDB to transfer control to exception handler ?
« Reply #5 on: March 05, 2019, 01:59:04 pm »
Btw, you can avoid the issue
Code: Pascal  [Select]
  1. HexPtr^ := HexDigits[p^ and byte($F)];

At least for me that seems to fix it.

Cyrax

  • Hero Member
  • *****
  • Posts: 727
Re: How to get GDB to transfer control to exception handler ?
« Reply #6 on: March 05, 2019, 02:03:14 pm »
Btw, you can avoid the issue
Code: Pascal  [Select]
  1. HexPtr^ := HexDigits[p^ and byte($F)];

At least for me that seems to fix it.

I can confirm this. The test project in this bug report https://bugs.freepascal.org/view.php?id=35187 works after changing the problematic line to above quoted one.

440bx

  • Hero Member
  • *****
  • Posts: 826
Re: How to get GDB to transfer control to exception handler ?
« Reply #7 on: March 05, 2019, 02:54:16 pm »
Btw, you can avoid the issue
Code: Pascal  [Select]
  1. HexPtr^ := HexDigits[p^ and byte($F)];

At least for me that seems to fix it.
Very nice and simple solution.  Both Delphi (v2) and FPC versions work as expected with it.  Thank you!.

I can confirm this. The test project in this bug report https://bugs.freepascal.org/view.php?id=35187 works after changing the problematic line to above quoted one.
I can confirm that as well.  It solves the problem in both, 32bit and 64bit. 
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.