Recent

Author Topic: Stopping the debugger under a certain condition  (Read 1497 times)

zoltanleo

  • Sr. Member
  • ****
  • Posts: 488
Stopping the debugger under a certain condition
« on: September 24, 2019, 01:49:24 pm »
Hi guys. I need your help.

In my application, in each iteration loop, I parse the string and remove the "extra" characters, e.g.:

Code: Pascal  [Select][+][-]
  1.           //get position #10 in the line
  2.           LineEndingPos_10:= Pos(#10,Buffer);
  3.  
  4.           repeat {until LineEndingPos_10 = 0;}
  5.             case LineEndingPos_10 of
  6.               0:
  7.                 begin
  8.                   {I need to stop the debugger here...}
  9.                   BufferStg:= BufferStg + Buffer;
  10.                 end;
  11.               1: //#10 at the beginning of the line
  12.                 begin
  13.                   Inc(StrCounter);
  14.                   if Length(BufferStg) > 0 then
  15.                   begin
  16.                     //dump the buffer storage to memo ...
  17.                     memoOutput.Lines.Add(Format(ConstFormatStr,[StrCounter,'B',BufferStg]));{B}
  18.                     memoOutput.SelStart:= UTF8Length(memoOutput.Text);
  19.                   end;
  20.                   Application.ProcessMessages;
  21.  
  22.                   //clear the storage buffer
  23.                   BufferStg:='';
  24.  
  25.                   //clip the first character #10(LF) from the buffer
  26.                   Delete(Buffer,1,1);
  27.  
  28.                   //... and continue the cycle further
  29.                 end
  30.               else //#10(LF) in the middle or at the end of the buffer  
  31.               ...
  32.  

The total number of iteration cycles is more than 4400. An error can occur on a cycle with the number 2531, e.g.:

Code: Pascal  [Select][+][-]
  1. 2528. {C}~ gbak:0 records written
  2. 2529. {C}~ gbak:    writing index PK_TBL_LS_INFECTION_HIV
  3. 2530. {C}~ gbak:    writing data for table TBL_LS_INFECTION_HIV
  4. gbak:0 records written
  5. 2531. {C}~ gbak:    writing index PK_TBL_LS_INFECTION_HERPES6
  6. 2532. {C}~ gbak:    writing data for table TBL_LS_INFECTION_HERPES6

It will take a very long time if I press F8 2530 times.

Can I stop debugging under a certain condition, e.g. when the iteration counter reaches the number 2528?
Win10 LTSC x64/Deb 11 amd64(gtk2/qt5)/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.10; IBX by TonyW

Sorry for my bad English, I'm using translator ;)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Stopping the debugger under a certain condition
« Reply #1 on: September 24, 2019, 02:12:01 pm »
If you have any kind of counter (say, you¡'re using a for loop or increasing a "total" counter with each iteration) you can set a conditional breakpoint.

You can either use "Run -> Add breakpoint" and set the breakpoint properties or set a breakpoint with F5 and then edit its properties by using "View -> Debug Windows -> Breakpoints"
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

RayoGlauco

  • Full Member
  • ***
  • Posts: 179
  • Beers: 1567
Re: Stopping the debugger under a certain condition
« Reply #2 on: September 24, 2019, 02:15:40 pm »
Surely there is a more elegant way to do it, but I usually add temporarily a counter and an extra instruction inside the loop:

Code: Pascal  [Select][+][-]
  1. var
  2.   loopcount: integer;
  3. {...}
  4. repeat
  5.   ...
  6.   if loopcount = 2531 then
  7.     sleep(10);
  8.   ...
  9.  

Then, I set a breakpoint on sleep(10);
To err is human, but to really mess things up, you need a computer.

zoltanleo

  • Sr. Member
  • ****
  • Posts: 488
Re: Stopping the debugger under a certain condition
« Reply #3 on: September 24, 2019, 02:32:40 pm »
@lucamar
thanks for the advice

Then, I set a breakpoint on sleep(10);
This is exactly what I wanted to hear. Thank you very much.
Win10 LTSC x64/Deb 11 amd64(gtk2/qt5)/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.10; IBX by TonyW

Sorry for my bad English, I'm using translator ;)

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Stopping the debugger under a certain condition
« Reply #4 on: September 24, 2019, 02:33:44 pm »
Hi guys. I need your help.

In my application, in each iteration loop, I parse the string and remove the "extra" characters, e.g.:

Code: Pascal  [Select][+][-]
  1.           //get position #10 in the line
  2.           LineEndingPos_10:= Pos(#10,Buffer);
  3.  
  4.           repeat {until LineEndingPos_10 = 0;}
  5.             case LineEndingPos_10 of
  6.               0:
  7.                 begin
  8.                   {I need to stop the debugger here...}
  9.                   BufferStg:= BufferStg + Buffer;
  10.                 end;
  11.               1: //#10 at the beginning of the line
  12.                 begin
  13.                   Inc(StrCounter);
  14.                   if Length(BufferStg) > 0 then
  15.                   begin
  16.                     //dump the buffer storage to memo ...
  17.                     memoOutput.Lines.Add(Format(ConstFormatStr,[StrCounter,'B',BufferStg]));{B}
  18.                     memoOutput.SelStart:= UTF8Length(memoOutput.Text);
  19.                   end;
  20.                   Application.ProcessMessages;
  21.  
  22.                   //clear the storage buffer
  23.                   BufferStg:='';
  24.  
  25.                   //clip the first character #10(LF) from the buffer
  26.                   Delete(Buffer,1,1);
  27.  
  28.                   //... and continue the cycle further
  29.                 end
  30.               else //#10(LF) in the middle or at the end of the buffer  
  31.               ...

The total number of iteration cycles is more than 4400. An error can occur on a cycle with the number 2531, e.g.:

Code: Pascal  [Select][+][-]
  1. 2528. {C}~ gbak:0 records written
  2. 2529. {C}~ gbak:    writing index PK_TBL_LS_INFECTION_HIV
  3. 2530. {C}~ gbak:    writing data for table TBL_LS_INFECTION_HIV
  4. gbak:0 records written
  5. 2531. {C}~ gbak:    writing index PK_TBL_LS_INFECTION_HERPES6
  6. 2532. {C}~ gbak:    writing data for table TBL_LS_INFECTION_HERPES6

It will take a very long time if I press F8 2530 times.

Can I stop debugging under a certain condition, e.g. when the iteration counter reaches the number 2528?
It's not beautiful, but it might be helpful sometimes:
Code: Pascal  [Select][+][-]
  1.           //get position #10 in the line
  2.           LineEndingPos_10:= Pos(#10,Buffer);
  3.           Counter := 0;
  4.           repeat {until LineEndingPos_10 = 0;}
  5.             Counter := Counter + 1;
  6.             case LineEndingPos_10 of
  7.               0:
  8.                 begin
  9.                   {I need to stop the debugger here...}
  10.                   if (Counter = 2530) then
  11.                     ShowMessage('Counter is now 2530 or any number you want.');
  12.                   BufferStg:= BufferStg + Buffer;
  13.                 end;

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9867
  • Debugger - SynEdit - and more
    • wiki
Re: Stopping the debugger under a certain condition
« Reply #5 on: September 24, 2019, 02:35:54 pm »
Breakpoints do have conditions.

If you right click a breakpoint you can go to "breakpoint properties". Or you can do so via the "breakpoint window" from menu view>debugger-win.

https://wiki.lazarus.freepascal.org/IDE_Window:Breakpoints#Breakpoint_properties

If you set condition to:
  i=2000
It will only stop if that evals to true.

The only issue: gdb does not understand strings. So
  s='abc'
will NOT work.
I personally had some success with the following (but others said it did not work for them). Note the double quotes:
   $_streq(^char(s), "abc")
https://sourceware.org/gdb/current/onlinedocs/gdb/Convenience-Funs.html#Convenience-Funs



Alternatively you can use the hitcount property.

zoltanleo

  • Sr. Member
  • ****
  • Posts: 488
Re: Stopping the debugger under a certain condition
« Reply #6 on: September 24, 2019, 07:55:14 pm »
@valdir.marcos
@Martin_fr

Thanks so much for the valuable advice.
Win10 LTSC x64/Deb 11 amd64(gtk2/qt5)/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.10; IBX by TonyW

Sorry for my bad English, I'm using translator ;)

 

TinyPortal © 2005-2018