Recent

Author Topic: Tutorial for debugging compiler?  (Read 2740 times)

prabhav

  • Newbie
  • Posts: 2
Tutorial for debugging compiler?
« on: April 11, 2021, 03:45:42 pm »
Hello,
I'm new to FPC but would like to help by working on the compiler but I'm unable to find any documentation on how to debug it through Lazarus to get a better understanding of the flow, its function etc. I've saw that there are defines that enable different writeln but that's not my favourite way of debugging things especially if its something complicated :-X
Is there any howto for that for beginners? %)

Thanks :)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9526
  • FPC developer.
Re: Tutorial for debugging compiler?
« Reply #1 on: April 11, 2021, 03:56:51 pm »
- Place a breakpoint in lazarus? A .lpi is in the compiler/ directory
- like gcc there are some verbosity options to show internal representations like -vp -vv,

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 899
Re: Tutorial for debugging compiler?
« Reply #2 on: April 11, 2021, 08:06:33 pm »
Also, a useful procedure to place a breakpoint in is GenerateError. It's called for any kind of compilation error (syntax error, type error, assembler reader error, internal error, ...).

Bi0T1N

  • Jr. Member
  • **
  • Posts: 65
Re: Tutorial for debugging compiler?
« Reply #3 on: April 12, 2021, 12:11:17 pm »
Someone started a wiki page and I've just added a simple step-by-step tutorial. Hope that will help :)
However, I don't think it will work like this once you added something new to the compiler itself (e.g. add new intrinsics) as you need to recompile the compiler and then recompile the provided units. Maybe someone who knows how to do that from within Lazarus could add the needed information? I would also appreciate that ;)

ccrause

  • Sr. Member
  • ****
  • Posts: 434
Re: Tutorial for debugging compiler?
« Reply #4 on: April 12, 2021, 01:02:06 pm »
In theory, debugging of the compiler is exactly the same as any other program (just like marcov already said).  My experience is that in practice you need to know what behaviour you want to debug (e.g. procedure overload selection) and where to find it in the source to put a break point in approximately the right place.  Just stepping through the compiler from the start hoping to find something specific requires an enormous amount of time and patience.

An easier situation is when you want to investigate what goes wrong when the compiler spits out an internal error code - in this case search for the error code in the source folder (I use rgrep a lot on Linux), then place a break point on the internalerror call and look at the stack trace to see the call history leading up to this point.

Another situation could be when you want to analyze the compiler code that leads to an error/warning.  In this case search for the error/warning code (or a short text snippet of the message) in the compiler/msg folder.  The English messages will be in the errore.msg file.  Locate the constant name of message (first part of message before "=") and search for this constant in the compiler source.

When using one of the existing ppcX.lpi projects in Lazarus, an executable called pp will be created in the target specific folder (X= target CPU).  This executable can be used just like a normal compiler to perform simple testing with. You can also recompile the RTL using this compiler, it saves the time to bootstrap the new compiler (if you are working on the 64 bit version of the compiler):
Code: Text  [Select][+][-]
  1. make rtl PP=compiler/x86_64/pp

Just some random thoughts from my side.  Please ask if there is anything more specific you would like to investigate.

prabhav

  • Newbie
  • Posts: 2
Re: Tutorial for debugging compiler?
« Reply #5 on: April 12, 2021, 10:40:46 pm »
Thanks for all the comment and also for the wiki tutorial!!
Is there something similar for debug the RTL since it was mention here how to compile it?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9526
  • FPC developer.
Re: Tutorial for debugging compiler?
« Reply #6 on: April 12, 2021, 11:18:26 pm »
Thanks for all the comment and also for the wiki tutorial!!
Is there something similar for debug the RTL since it was mention here how to compile it?

In the past I maintained a small tutorial here. It mostly still applies.

ccrause

  • Sr. Member
  • ****
  • Posts: 434
Re: Tutorial for debugging compiler?
« Reply #7 on: April 13, 2021, 09:53:55 am »
Thanks for all the comment and also for the wiki tutorial!!
Is there something similar for debug the RTL since it was mention here how to compile it?
The RTL is in some respects like a library, so if you have a test program that calls the code of interest in the RTL, you can debug the test program and step into the RTL code - if the RTL has been compiled with the necessary debug information.  I've also noticed that the optimization level should not be too high, else tools like gdb struggle with stepping, but -O1 seems to work.  Here is an example command to build the RTL for debugging:
Code: Text  [Select][+][-]
  1. make rtl OPT="-O1 -g -dDEBUG"
The -dDEBUG define is included because the default fpc.cfg file has some predefined extra options for debugging. But this is optional, the key is to include debug information (-g).  You can also be more specific with the debug information and specify dwarf (-gw2 or -gw3) or stabs (-gs) if the automatic option (-g) doesn't work properly for debugging.

 

TinyPortal © 2005-2018