Debugging Compiler

From Free Pascal wiki
Revision as of 12:00, 12 April 2021 by Bi0T1N (talk | contribs) (Add simple step-by-step tutorial + extend provided information)
Jump to navigationJump to search

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, ...).