Difference between revisions of "Debugging Compiler"

From Lazarus wiki
Jump to navigationJump to search
m (→‎Debugging FPC Hints: Fix typos; add debugging category)
(Add simple step-by-step tutorial + extend provided information)
Line 1: Line 1:
Here are some hints on debugging FPC compiler itself
+
This page is about debugging the FPC compiler and thus it provides a tutorial and some hints that might be useful.
 +
 
 +
==Step-by-step tutorial==
 +
# There is a FPC installation in <code>D:\Fcpupdeluxe\fpc</code>
 +
# The FPC repository is checked out (same revision as the FPC installation) in another folder and used to work on the compiler, thus the .lpi file is opened from this folder.
 +
# There are multiple .lpi files for different architectures in the compiler directory to be used with the Lazarus IDE. Open the one which fits your architecture. In this tutorial we're using the <code>ppcx64.lpi</code>.
 +
# In the menu bar go to ''Run'' and then click ''Run parameters ...'' to specify the ''Command line parameters''.
 +
#: An example of command line parameters could look like this:
 +
#: <code>D:\Delphi\tests\test.pas -FuD:\Fcpupdeluxe\fpc\units\x86_64-win64\*</code>
 +
#: Explanation of the command line parameters:
 +
## <code>D:\Delphi\tests\test.pas</code> <- program or unit that should be compiled
 +
## <code>-FuD:\Fcpupdeluxe\fpc\units\x86_64-win64\*</code> <- path to the units provided by FPC, otherwise the error ''Fatal: Can't find unit system used by test'' will be shown and an ''ECompilerAbort'' exception in verbose.pas is raised
 +
# Now you can set breakpoints in Lazarus, e.g. in the <code>parser.compile</code> function and follow the flow of compiling something by using the functionality of Step Over (F8) or Step Into (F7).
 +
 
 
==Debugging FPC Hints==
 
==Debugging FPC Hints==
  
* A useful procedure in which to place a breakpoint is [code]GenerateError[/code]. It's called for any kind of compilation error (syntax error, type error, assembler reader error, internal error, ...).
+
* There are some verbosity options (like gcc) to be specified for the command line to show internal representations like
 +
** -vp: Write tree.log with parse tree
 +
** -vv: Write fpcdebug.txt with lots of debugging info
  
* Like gcc there are some verbosity options to show internal representations like -vp -vv
+
* Place a breakpoint at the <code>parser.compile</code> function in Lazarus as this is the entry point for parsing.
  
* Place a breakpoint in Lazarus. An .lpi is in the compiler/directory (using Lazarus IDE).
+
* Another useful procedure in which to place a breakpoint is <code>GenerateError</code> in verbose.pas. It's called for any kind of compilation error (syntax error, type error, assembler reader error, internal error, ...).
  
 
[[Category:FPC]]
 
[[Category:FPC]]

Revision as of 11:00, 12 April 2021

This page is about debugging the FPC compiler and thus it provides a tutorial and some hints that might be useful.

Step-by-step tutorial

  1. There is a FPC installation in D:\Fcpupdeluxe\fpc
  2. The FPC repository is checked out (same revision as the FPC installation) in another folder and used to work on the compiler, thus the .lpi file is opened from this folder.
  3. There are multiple .lpi files for different architectures in the compiler directory to be used with the Lazarus IDE. Open the one which fits your architecture. In this tutorial we're using the ppcx64.lpi.
  4. In the menu bar go to Run and then click Run parameters ... to specify the Command line parameters.
    An example of command line parameters could look like this:
    D:\Delphi\tests\test.pas -FuD:\Fcpupdeluxe\fpc\units\x86_64-win64\*
    Explanation of the command line parameters:
    1. D:\Delphi\tests\test.pas <- program or unit that should be compiled
    2. -FuD:\Fcpupdeluxe\fpc\units\x86_64-win64\* <- path to the units provided by FPC, otherwise the error Fatal: Can't find unit system used by test will be shown and an ECompilerAbort exception in verbose.pas is raised
  5. Now you can set breakpoints in Lazarus, e.g. in the parser.compile function and follow the flow of compiling something by using the functionality of Step Over (F8) or Step Into (F7).

Debugging FPC Hints

  • There are some verbosity options (like gcc) to be specified for the command line to show internal representations like
    • -vp: Write tree.log with parse tree
    • -vv: Write fpcdebug.txt with lots of debugging info
  • Place a breakpoint at the parser.compile function in Lazarus as this is the entry point for parsing.
  • Another useful procedure in which to place a breakpoint is GenerateError in verbose.pas. It's called for any kind of compilation error (syntax error, type error, assembler reader error, internal error, ...).