Recent

Author Topic: line number when error happens  (Read 275 times)

rcmz

  • Full Member
  • ***
  • Posts: 156
line number when error happens
« on: November 13, 2025, 08:48:00 pm »
hi,

how can i get the line number when an error happens at runtime.

Aruna

  • Hero Member
  • *****
  • Posts: 764
Re: line number when error happens
« Reply #1 on: November 13, 2025, 10:26:16 pm »
hi,

how can i get the line number when an error happens at runtime.
Hi rcmz, When you run your program inside Lazarus (Run → Run or F9), the IDE will automatically catch exceptions and highlight the exact line that caused the problem.

But outside the IDE, you’ll need the correct Compiler Flag(s) for Line Info.
Compile the code given below using: fpc testerror.pas. Then run using ./testerror.
The first screenshot shows the output. Has some information but no line number yet.

Now recompile using the correct flag for line numbers: fpc -gl testerror.pas. The second screenshot now shows you the line number where the exception happened.

Code: Pascal  [Select][+][-]
  1. program TestError;
  2. {$mode objfpc}{$H+}
  3.  
  4. uses
  5.   SysUtils;
  6.  
  7. var
  8.   x, y, z: Integer;
  9. begin
  10.   x := 10;
  11.   y := 0;
  12.   z := x div y;  // This will raise a division by zero error
  13.   Writeln('Result: ', z);
  14. end.
  15.  

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11923
  • Debugger - SynEdit - and more
    • wiki
Re: line number when error happens
« Reply #2 on: November 13, 2025, 10:37:19 pm »
Depends what exactly you need.

In all and any cases (well one exception) you must have the exe with debug info (that will make the exe rather large)

If you want the line number inside the exception handler of your own code, then search the fpc code for
dump_stack and BackTraceStrFunc

You will most likely have dwarf debug info (except maybe on some embedded targets), so you must include the unit LNfoDwarf. (or use -gl).

----

If you just want to debug, run in the debugger.

----

If you want to ship a release, but get meaningful error reports from your users (here is the exception / kind of)

build your app with debug info (dwarf), but WITHOUT  -gl

     EDIT: Build it with -gl


- Then you have the big exe (with debug info).
- Make a copy
- Use strip to remove debug info from the copy
- ship that

When you get a trace, it will be addresses only.

In the IDE menu: View > Leaks And traces
- paste the trace with the addresses
- press resolve
- select the big exe with debug info (the one before the copy).
  - That must be the exact one, before the copy
  - This will NOT work if you later build it again

And the IDE computes the line numbers.

Test it, before you use it





EDIT:

Originally I wrote without -gl.

But that is wrong (well it can be done, but would be an unnecessary bother)

The important part is to make the copy, and strip the copy. Then -gl on the stripped copy only prints addresses when it prints the trace for an exception.
« Last Edit: November 13, 2025, 11:33:13 pm by Martin_fr »

 

TinyPortal © 2005-2018