Recent

Author Topic: Debugger doesn't display range check errors  (Read 1247 times)

dg4fac

  • New Member
  • *
  • Posts: 21
Debugger doesn't display range check errors
« on: May 11, 2022, 04:26:14 pm »
Hi.

I am working with Lazarus 2.2.0 under Win10. While always working very fine, I now realize that the debugger does not work. I am getting range check errors, and the debugger doesn't provide any hints on the reasons. And I have all debug options activated!

I provoked a range check error by accessing an array out of its bounds. The range-check-error-message appears. But if I abort the program execution the debugger does not allow to locate the line where it occurred. It is simply missing the option to locate the last executed line.

What can be wrong here? With that the debugger is not useful.

brgs, gsc

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8438
  • Debugger - SynEdit - and more
    • wiki
Re: Debugger doesn't display range check errors
« Reply #1 on: May 11, 2022, 08:27:59 pm »
Works fine for me. Could you share more details please?

See my screen captures. (the order is wrong, the lower picture comes first)

1) Debugger Exception Notification
The line is already there.

2) After pressing "Break"
Line is indicated / Stack has info.

- I used "Dwarf with sets", but it should be the same when using just Dwarf, or Dwarf-3.
- I tested with -O1 => but other settings should work. Though for debugging "no opt" is the best.

It also works (I tested) if I do not use "sysutils".
The difference should be, that if you "continue" on the "Run Error", then with SysUtils you additionally get an Exception (the exception can also be caught by your code using "try except end". The run-error does not trigger this)


Please check under Tools > Debugger > Backend: Are you using FpDebug?

If you have "inlined" code, you may want to try and disable this for the unit in question. "{$INLINE off}"

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8438
  • Debugger - SynEdit - and more
    • wiki
Re: Debugger doesn't display range check errors
« Reply #2 on: May 11, 2022, 08:33:52 pm »
Just adding one other note:

It is possible, depending on how FPC was compiled, that the "FPC_BREAK" procedure "hides" the caller.

That is the stack window will be missing one entry. in case of my example this would be the line "Foo" => then you only see the caller of code that triggers the error.

This is a known issue, but there is currently no fix.

dg4fac

  • New Member
  • *
  • Posts: 21
Re: Debugger doesn't display range check errors
« Reply #3 on: May 12, 2022, 09:56:32 am »
Hi.

Works fine for me. Could you share more details please?

Here are my debugger entries.

I dont get a message like yours. The range check error window occurs. If I press Abort, the progam is killed and is completely removed, no error message appears any more. If I press continue, the program continues, again without any debugger infos.

It also works (I tested) if I do not use "sysutils".
The difference should be, that if you "continue" on the "Run Error", then with SysUtils you additionally get an Exception (the exception can also be caught by your
code using "try except end". The run-error does not trigger this)

All my units use sysutils. No exeption occurs.

Please check under Tools > Debugger > Backend: Are you using FpDebug?

Yes.

Quote
If you have "inlined" code, you may want to try and disable this for the unit in question. "{$INLINE off}"

No inline code used.

brgs, gsc

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8438
  • Debugger - SynEdit - and more
    • wiki
Re: Debugger doesn't display range check errors
« Reply #4 on: May 12, 2022, 11:04:42 am »
"avr sim" => you have Win10 on AVR? or you are debugging an embedded target?

Also your 3rd picture shows "Debugger > General".
But what is set in "Debugger > Backend" ?


If you are using avr that is maintained by Christo. So I would have to notify him.

dg4fac

  • New Member
  • *
  • Posts: 21
Re: Debugger doesn't display range check errors
« Reply #5 on: May 12, 2022, 11:59:07 am »
"avr sim" => you have Win10 on AVR? or you are debugging an embedded target?
If you are using avr that is maintained by Christo. So I would have to notify him.

No, avr_sim is not a simulator for Lazarus code using Pascal language, but a simulator for AVR assembler language files only. avr_sim is a stand-alone simulator, written in Lazarus Pascal and distributed as Win and Lin 64-bit executable, that eats assembler source code files and simulates them on a PC or laptop. It produces Intel-Hex-Code only from the assembler source code, not from Pascal source code.
No need to notify Christo.
(Sidekick: I hate high-level-languages on AVRs, for me high-level languages on AVRs are like applying a bicycle instead of a Formula 1 racer car - or at least a Fiat 500 - to get forward. Highlevel lamguages produce more than 90% scrap code when converting it to assembler, I hate that, it is completely inappropriate.)

Also your 3rd picture shows "Debugger > General".
But what is set in "Debugger > Backend" ?

The backend is set to FpDebug. Do you need a screenshot as well?

In the meantime I have tested debugging in a new project. The properties and settings of the new project look exactly like in avr_sim. A forced exception works perfect, the debugger shows its correct message.  But not so in avr_sim: if I provoke an exception in avr_sim, the debugger window does not appear. It is like no exception is created.

brgs, gsc


dg4fac

  • New Member
  • *
  • Posts: 21
Re: Debugger doesn't display range check errors
« Reply #6 on: May 12, 2022, 12:27:38 pm »
Hi.

Now it is getting even crazier. If I set debugging to Release (and remove all range checks), I get the added error message. Unfortunately I can't find out the source code line where this access exception occurred.

brgs, gsc

ccrause

  • Hero Member
  • *****
  • Posts: 664
Re: Debugger doesn't display range check errors
« Reply #7 on: May 12, 2022, 01:09:20 pm »
No, avr_sim is not a simulator for Lazarus code using Pascal language, but a simulator for AVR assembler language files only.

I assume avr_sim refers to Gerd's AVR simulator?  If so, this is indeed a regular PC type application.  Since the source is available for download, can the OP please give exact steps on how to produce this specific range check error (for example open unit X, change value Y on line Z, compile, run then click on button A and observe crash...)?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8438
  • Debugger - SynEdit - and more
    • wiki
Re: Debugger doesn't display range check errors
« Reply #8 on: May 12, 2022, 01:09:43 pm »
Ok...

Quote
The backend is set to FpDebug. Do you need a screenshot as well?
Nope, no screenshot.

But, ....

1) please also check in your "project options" on the page "Debugger" (below "Verschiedenes") => It should say "Use IDE default debugger".

2) In your "project options" > "language exceptions" ("Sprach Ausnahmen") => What entries do you have listed?
  Are any "run error ..." or "range check" listed?

3) When you debug, are there "little blue dots" in the editor's gutter?

4) Your code will stop at a breakpoint?

5a) 64 or 32 Bit Lazarus IDE installed?
5b) 64 or 32 Bit project (i.e. cross compiled to 32 bit)?

6) Do you have libraries (dll) written in Pascal? And is the failing code in such a library?

7) Does the debugger inform you if you run this?
Code: Pascal  [Select][+][-]
  1. type TFooExcept = class(Exception) a: byte; end;
  2. begin
  3.   raise TFooExcept.Create('123');
  4.  


---- EDIT

8) Is some of your code in (a) package(s)?
  If so, open the package and ensure it is also compiled with dwarf debug info.
« Last Edit: May 12, 2022, 01:14:37 pm by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8438
  • Debugger - SynEdit - and more
    • wiki
Re: Debugger doesn't display range check errors
« Reply #9 on: May 12, 2022, 01:13:17 pm »
Now it is getting even crazier. If I set debugging to Release (and remove all range checks), I get the added error message. Unfortunately I can't find out the source code line where this access exception occurred.

That is the normal. If the range check is not stopped, then it often causes other errors.
An unfixed range-check can cause anything from
- nothing
- weird behaviour of the app (wrong (re-)action of any unrelated code in the app)
- access violation

dg4fac

  • New Member
  • *
  • Posts: 21
Re: Debugger doesn't display range check errors
« Reply #10 on: May 13, 2022, 11:53:06 am »
No, avr_sim is not a simulator for Lazarus code using Pascal language, but a simulator for AVR assembler language files only.

I assume avr_sim refers to Gerd's AVR simulator?  If so, this is indeed a regular PC type application.  Since the source is available for download, can the OP please give exact steps on how to produce this specific range check error (for example open unit X, change value Y on line Z, compile, run then click on button A and observe crash...)?

That is all correct. As compared to the published Lazarus source code (version 2.5), I have made a lot of changes towards version 2.6. So I would have to pack the complete source files as they are now. Probably I will put it on the webpage as preliminary version 2.6. Would that help?

The debug error is only occurring when I start the Timer TC0  in an ATmega16 device. And the most crazy thing is: when I start the simulation for the first time the code runs normal (without range check error) and the timer TC0 runs. If the timer reaches its overflow condition, the interrupt serrvice routine switches a pin high/low that provokes an INT1 condition. If that happens, the range check error happens. And isn't correctly displayed.

If I reset the simulator (which sets all variables to their default values), the error appears already when I write the control word to the timer TC0's TCCR0. So probably I have not resetted all variables properly in my Restart procedure.

The whole mess started because I changed the port display window completely. It wasn't working correct before, so I had to improve it. And it now works correct with other AVR device types, but not with an ATmega16's INT1.

In my previous work with Lazarus it would have been easy to find the bug. E.g. when accessing arrays out of their bounds. The debugger would have detected this and would have stopped this. He would have provided information from which it has been easy to find the bug's reason. But not now any more: it dosn't stop such an error any more, and allows that such an access violation with a false address can occur.

I want my old debugger back!

Shall I publish the Lazarus source code with the current code as well as the error-producing assembler source code for the ATmega16?

But, ....

1) please also check in your "project options" on the page "Debugger" (below "Verschiedenes") => It should say "Use IDE default debugger".

That is correct.

2) In your "project options" > "language exceptions" ("Sprach Ausnahmen") => What entries do you have listed?
  Are any "run error ..." or "range check" listed?

All those entries are enabled.

3) When you debug, are there "little blue dots" in the editor's gutter?

All executable code lines have such a blue dot, not the begin and end lines.

4) Your code will stop at a breakpoint?

Breakpoints work correct, if I set them.

5a) 64 or 32 Bit Lazarus IDE installed?

64 bit.

5b) 64 or 32 Bit project (i.e. cross compiled to 32 bit)?

Compiling for 64 bit, no cross compilation.

6) Do you have libraries (dll) written in Pascal? And is the failing code in such a library?

Nope, I don't use libraries (as far as I know. I do not know how to produce a library. And, as an assembler freak, I hate libraries: they are completely intransparent and witchy code).

7) Does the debugger inform you if you run this?
Code: Pascal  [Select][+][-]
  1. type TFooExcept = class(Exception) a: byte; end;
  2. begin
  3.   raise TFooExcept.Create('123');
  4.  

Works correct as expected. See attached window.

8) Is some of your code in (a) package(s)?
  If so, open the package and ensure it is also compiled with dwarf debug info.

No, the code only uses standard components. No additional packages.

brgs, gsc

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8438
  • Debugger - SynEdit - and more
    • wiki
Re: Debugger doesn't display range check errors
« Reply #11 on: May 13, 2022, 01:10:22 pm »
OK, one more idea....

Maybe that particular "range check" is not your app at all...

But, that would be weird, because when you compile without range-check => you do get bad behaviour....
So we do know that your app does have some bug. Maybe even a range check. But maybe not the one displayed....

Ok, that sounds very confusing....
So here is the explanation:

Maybe there is a "range check" error in the IDE itself (possible triggered when your app hits its own bug).
You can test that => if it is the IDE, and if you press "Cancel" => the IDE should close.

If the error is in the IDE then of course the debugger does not show any info. (In fact it might be that part of the debugger crashed).

As for the title of the pop up window, which clearly has the name of your project "avr-sim" => What is the title of your IDE main window (the IDE menu bar window?) => because you can set that to start with the project name / though I would still expect it to end in "Lazarus" => so this does not match your error window....

I did fix an overflow error in FpDebug: https://gitlab.com/freepascal.org/lazarus/lazarus/-/commit/4ad24ee8371d1fa7743fe1d0eaced1e36a5b2e24


Anyway, even if in some weird way the above applies. => We know that there is an issue in your code too.

Btw, did you try, if you can catch it using the gdb debugger?




You could also run the IDE with
Code: Text  [Select][+][-]
  1. lazarus.exe --debug-log=c:\laz-log.txt --debug-enable=DBG_STATE,DBG_DATA_MONITORS,DBGMI_QUEUE_DEBUG,FPDBG_DWARF_ERRORS,FPDBG_DWARF_WARNINGS,DBG_VERBOSE,DBG_WARNINGS,DBG_STATE,DBG_EVENTS,DBG_THREAD_AND_FRAME,DBG_ERRORS    

Maybe the log catches something ...

ccrause

  • Hero Member
  • *****
  • Posts: 664
Re: Debugger doesn't display range check errors
« Reply #12 on: May 13, 2022, 02:19:34 pm »
Shall I publish the Lazarus source code with the current code as well as the error-producing assembler source code for the ATmega16?
I think just publish the latest source with the error producing example - also with steps on how to trigger the error.  With this one can start testing the code and debugger.  My expectation is not to spot the error in your code, but perhaps using different debugger options may help in trapping the error.

I will test this on Linux, perhaps there the bug is easier to trap with the debugger...

dg4fac

  • New Member
  • *
  • Posts: 21
Re: Debugger doesn't display range check errors
« Reply #13 on: May 13, 2022, 05:14:14 pm »
Hi.

Shall I publish the Lazarus source code with the current code as well as the error-producing assembler source code for the ATmega16?
I think just publish the latest source with the error producing example - also with steps on how to trigger the error.  With this one can start testing the code and debugger.  My expectation is not to spot the error in your code, but perhaps using different debugger options may help in trapping the error.

I will test this on Linux, perhaps there the bug is easier to trap with the debugger...

I finally found the bug:
1. Switching all range checks off.
2. Provoking the access error, get the line number and its content from there.
3. Corrected the bug in this line.
4. Switched all range checks on again.

The errors are now all fixed, so I can publish the code for version 2.6 in the next days (after I prepared the Linux executable). It is really improved, compared to version 2.5.  And I myself am surprised that anything works now as desired and planned.

I'd like to say Thank You to all Lazarus programmers. When I started with this avr_sim software in 2016 I was expecting that this would end in a catastrophe, like my first approach in 2005 with Kylix did. Thanks to Lazarus more and more components of the final started to work. And now I am only fixing minor errors and defective appearances. Without Lazarus I would have given up in 2017. 

@Martin_fr:
I never would have expected that switching the range checks on would mask these kind of memory access errors. So, in the future, in case of an error, I will have to check the software with all range checks off, additionally. Can you probably find a way to let the other error message through with the range checks switched on, too? Those provide more info in those cases.

Thank you all for your help.

brgs, gsc

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8438
  • Debugger - SynEdit - and more
    • wiki
Re: Debugger doesn't display range check errors
« Reply #14 on: May 13, 2022, 11:19:18 pm »
I never would have expected that switching the range checks on would mask these kind of memory access errors.
Well....

Note: in the below I say "on the heap" => for locals that is "on the stack" => but the same rules apply...

Code: Pascal  [Select][+][-]
  1. var a: array of bytes;
  2. begin
  3.   setlength(a,2); // allocates 2 bytes of mem on the heap => the heap itself is bigger though, depends on OS
  4.   a[50 * 1000 * 1000] := 1;

Now you write to memory 50 MB after your allocated area => most likely outside the total heap area. You get an access violation.

With range-checking, fpc compiles the above as
Code: Pascal  [Select][+][-]
  1. var a: array of bytes;
  2. begin
  3.   setlength(a,2); // allocates 2 bytes of mem on the heap => the heap itself is bigger though, depends on OS
  4.   if 50 * 1000 * 1000 > length(a) then CallRangeCheckError;
  5.   a[50 * 1000 * 1000] := 1;
CallRangeCheckError is a runtime error or exception. It either aborts your program, or raises on exception which will go to the next "except end" (if there is).
In either case: The assignment is not executed => no access violation.

The range checking has nothing to do with the debugger. It is compiled into your exe. If you ship this, it will do range checking at your clients PC too. (there are valid reasons to do that).

Quote

@Martin_fr:
 So, in the future, in case of an error, I will have to check the software with all range checks off, additionally. Can you probably find a way to let the other error message through with the range checks switched on, too? Those provide more info in those cases.

As above: the debugger does not create the range check error. It merely catches it. (internally it just sets a breakpoint an "raise" and at "run_error".).

And normally that works. I still have no idea why it does not work in your case.

You could still produce the log as I described (by putting the error back in your code). It may or may not help in identifying why it's not caught by the debugger.




Btw, take the example above:

Code: Pascal  [Select][+][-]
  1. var
  2.   i: integer;
  3.   a: array [0..1] Integer;
  4.   b: boolean;
  5. begin
  6.   b := false;
  7.   i := 2;
  8.   a[i] := 1; // this changes "b" // Depend on compiler setting / optimization ...
  9.  
So without range check => at some point later when you happen to do "if b then ..." => your app will behave unexpected. Because b is not what you wanted.

If "b" wasn't boolean, but "b: TObject" => then you changed the internal pointer of b (the reference to the object date). => "b" will not be nil, but "b" will point to a random address.
If that address is readable you get some strange values.
If that address is not readable you get the access violation when you access "b" (which may be long after the mistake had happen).


 

TinyPortal © 2005-2018