* * *

Author Topic: Is there a way to tell the debugger (GDB) to ignore some exceptions ?  (Read 823 times)

440bx

  • Sr. Member
  • ****
  • Posts: 292
Hello,

I'd like to tell the debugger (GDB in this case) to ignore some exceptions, particularly those that I am actively testing against.   To show what I mean, here is a short example:
Code: Pascal  [Select]
  1. {$IFDEF FPC                      }
  2.   {$MODE OBJFPC                  }
  3.  
  4.   {$MODESWITCH ADVANCEDRECORDS   }
  5.   {$MODESWITCH AUTODEREF-        }
  6.   {$MODESWITCH ALLOWINLINE       }
  7. {$ENDIF                          }
  8.  
  9. {$MODESWITCH     ANSISTRINGS-    }
  10. {$LONGSTRINGS    OFF             }
  11. {$WRITEABLECONST ON              }
  12.  
  13. {$APPTYPE CONSOLE                }
  14.  
  15.  
  16. program PropsExcep;
  17.  
  18. uses windows;
  19.  
  20. type
  21.   PPropertiesRecord4 = ^PropertiesRecord4;
  22.   PropertiesRecord4 = record
  23.     private
  24.       TheField   : integer;
  25.  
  26.       procedure init;
  27.       procedure SetTheField(value : integer);
  28.       function  GetTheField : integer;
  29.  
  30.     public
  31.       property Field : integer read GetTheField write SetTheField;
  32.   end;
  33.  
  34. procedure PropertiesRecord4.init;
  35. begin
  36.   TheField := 4;
  37. end;
  38.  
  39. procedure PropertiesRecord4.SetTheField(value : integer);
  40. begin
  41.   TheField := value;
  42. end;
  43.  
  44. function PropertiesRecord4.GetTheField : integer;
  45. begin
  46.   if IsBadReadPtr(@Self, sizeof(Self)) then exit($AAAA);
  47.  
  48.   Result := TheField;
  49. end;
  50.  
  51.  
  52. var
  53.   Record4ptr : PPropertiesRecord4;
  54.  
  55. begin
  56.   // see how the getter handles the exception
  57.  
  58.   Record4ptr := PPropertiesRecord4($44);  // load a bad address
  59.  
  60.   // this should cause an exception in kernel32 and as a result the value
  61.   // returned should be $AAAA
  62.  
  63.   writeln('The field : ', Record4Ptr^.Field);
  64. end.
  65.  

The writeln should display the value of $AAAA in decimal but, when debugging, GDB traps the exception that occurred in kernel32 which gets in the way of testing how the rest of the code behaves (unlike in this example, there is more code in the "real" program.)

Is there a way I can tell GDB to ignore some exceptions ? like that one for instance.

(NOTE: I am aware that MS recommends against using the IsBad... family of functions because it can potentially mess up the stack's guard page.  I mention it because stating not to use those functions isn't the answer I'm looking for.)

Thank you for your help.


howardpc

  • Hero Member
  • *****
  • Posts: 2780
The IDE Options dialog has a Language Exceptions page under Debugger.
The [ + Add ] button opens a little dialog where you type the name of the exception to ignore.
Alternatively, when debugging, and an exception occurs, the debugger shows a dialog giving the exception class, message, and source code location. It includes an "Ignore this exception type" checkbox. Checking this does the same thing as manually adding the exception to above debugger Language Exceptions list.

taazz

  • Hero Member
  • *****
  • Posts: 5337
In the debugger exception dialog there is a "ignore this exception" check box when checked the debugger will ignore all exception of that type until you remove it from the list in tools\Options select the debugger\exception list on the left remove excpetion on the list on the right.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

440bx

  • Sr. Member
  • ****
  • Posts: 292
The IDE Options dialog has a Language Exceptions page under Debugger.
The [ + Add ] button opens a little dialog where you type the name of the exception to ignore.

My concern with that option is that the exception reported doesn't seem to be a language exception.  That's my impression, given my current knowledge of Lazarus, I could be wrong about that.

Alternatively, when debugging, and an exception occurs, the debugger shows a dialog giving the exception class, message, and source code location. It includes an "Ignore this exception type" checkbox. Checking this does the same thing as manually adding the exception to above debugger Language Exceptions list.

In the debugger exception dialog there is a "ignore this exception" check box when checked the debugger will ignore all exception of that type until you remove it from the list in tools\Options select the debugger\exception list on the left remove excpetion on the list on the right.

I am not getting a dialog box with an option of ignoring the exception.  I attached a screenshot of what I'm getting.  It pretty much says that I am SOL.

Under Tools/Options/Debugger/OS exceptions,  the "add" button is grayed.  Can't add anything there and...

Under Tools/Options/Debugger/Language Options, I'm concerned that if I disable EAccessViolation, I may be disabling other access violations that I may need to know about.  For that reason, blanket disabling EAccessViolation is not really desirable. 

I'd like to disable the access violation when it happens at that address (inside kernel32 in the IsBad...Ptr routine), other exceptions, I want to know about so I can deal with them.

If there is a way to do that, it'd be great.  thanks!



Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 4810
    • wiki
You can only suppress exception that are thrown by "raise" (and therefore are of class "Exception" or inherited)

The exception you get, is (also) called a "Signal".
The IDE has no option to tell gdb to recover from that.

And if you are under windows, then even outside the IDE it is not possible (gdb itself can not do it). At least last time I checked (a while back).

If you are on linux (or if (miraculously) it was fixed for gdb on win), you may check the gdb docs. There should be a way to tell gdb not to trap any signal at all. The IDE allows you to give startup arguments to gdb, so you may be able to pass that in... (It's a long shot though).

Note, you need to double check, so you still trap breakpoints, and SigInt (or pause and related stuff will not work)

440bx

  • Sr. Member
  • ****
  • Posts: 292
You can only suppress exception that are thrown by "raise" (and therefore are of class "Exception" or inherited)

The exception you get, is (also) called a "Signal".
The IDE has no option to tell gdb to recover from that.

And if you are under windows, then even outside the IDE it is not possible (gdb itself can not do it). At least last time I checked (a while back).

If you are on linux (or if (miraculously) it was fixed for gdb on win), you may check the gdb docs. There should be a way to tell gdb not to trap any signal at all. The IDE allows you to give startup arguments to gdb, so you may be able to pass that in... (It's a long shot though).

Note, you need to double check, so you still trap breakpoints, and SigInt (or pause and related stuff will not work)

Thank you Martin.  I suspected that was the case.  Nice to have confirmation of it. 

Playing around with the code, I ran into something interesting.  In the original code (as posted above) I purposely initialized the variable to a bad address to ensure an access violation was going to happen and that cause the message I got which there does not seem to be a simple way to have GDB ignore.

Just to see what happens, I commented out the line that initializes the pointer to a bad address, leaving the pointer simply uninitialized.   An access violation still took place but GDB didn't complain about it.  The function executed and returned without being bothered by the debugger.  I cannot explain that.  In both cases there was an access violation but, GDB only complained about it in the first case and not the second.    The code works, that's what counts.

Thank you.  I appreciate your input.
« Last Edit: July 25, 2018, 09:58:27 pm by 440bx »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus