FpDebug question(s) lazarus / 12-Debugger ---------------------------------------------------------------------- 440bx · 2024-09-30 14:20 Hello, For the time being, these questions are mostly a matter of curiousity. First one is, can FpDebug dynamically load an external file (.dbg) when execution goes into dll code ?  e.g, a Windows program goes into user32, ntdll or some other system dll. In MS Visual Studio, when execution goes into a system dll, VS uses the debug information stored in the dll to download the appropriate symbols (or load them from the local cache), how much work and how complicated would it be to enable Lazarus to load external .dbg files on demand based on the dll's stored debug information ? presume that there is a .dbg file whose name is the guid found in the dll's debug descriptor (to enable unambiguous identification.) Thank you for your help. ---------------------------------------------------------------------- Martin_fr · 2024-09-30 20:59 First of all => that is talking about dbg files with DWARF info. Since FpDebug only loads dwarf. (Happy to accept merge request for other formats, but currently not on my list). If they are on the disk, they should be found. Though I haven't tested that. I know FpDebug loads the dwarf if it is inside the library itself. It uses the same loading code, and that code supports dbg files (on the disk). If they point elsewhere, I would probably deal with that via a callback hook to the IDE, and let the IDE download them (if the user enabled that option). Shouldn't be to complicated. The relevant code in is the FpImageReader[Elf|Pe]. (IIRC). System dll on windows usually don't have dwarf info. They use a different format. (afaik) ---------------------------------------------------------------------- 440bx · 2024-09-30 22:56 Quote from: Martin_fr on September 30, 2024, 10:59:03 pmFirst of all => that is talking about dbg files with DWARF info. Since FpDebug only loads dwarf. (Happy to accept merge request for other formats, but currently not on my list). Yes.  I am also definitely limiting the considerations to DWARF info.  Implementing PDB processing requires too many dependencies on proprietary MS technologies which, I believe would not be beneficial to Lazarus. Quote from: Martin_fr on September 30, 2024, 10:59:03 pmIf they are on the disk, they should be found. Though I haven't tested that. I know FpDebug loads the dwarf if it is inside the library itself. It uses the same loading code, and that code supports dbg files (on the disk). I'd like to test FpDebug's ability to load an external disk based .dbg file (I don't care about other possible locations.)  Do you have any suggestions as to how  I should go about doing that ?  thank you in advance.  Quote from: Martin_fr on September 30, 2024, 10:59:03 pmThe relevant code in is the FpImageReader[Elf|Pe]. (IIRC). Good to know.  I'll have look at it.  Thank you. Quote from: Martin_fr on September 30, 2024, 10:59:03 pmSystem dll on windows usually don't have dwarf info. They use a different format. (afaik) Yes, they use the MS proprietary PDB format but, there is nothing that prevents writing a program to create DWARF symbols for them ---------------------------------------------------------------------- Martin_fr · 2024-10-01 05:53 Well FpDebug expects a "gnu_debuglink" section that has the link to the filename. TDbgImageReader.LoadGnuDebugLink Fpc created that if you compile something with external debug info. (it has a crc to check that it is up to date) FpDebug looks in the path of the exe, and in /.debug/ I haven't investigated what .so files off the linux kernel contain. I know gdb has some download ability for them. But I know no details about that. **If** the are the same format, and if you have them TElfDbgSource.Create  calls the LoadGnuDebugLink => just hardcode name and location ---------------------------------------------------------------------- 440bx · 2024-10-01 14:52 Quote from: Martin_fr on October 01, 2024, 07:53:00 amWell FpDebug expects a "gnu_debuglink" section that has the link to the filename. TDbgImageReader.LoadGnuDebugLink Fpc created that if you compile something with external debug info. (it has a crc to check that it is up to date) FpDebug looks in the path of the exe, and in /.debug/ Very useful information.  Thank you Martin. ---------------------------------------------------------------------- Martin_fr · 2024-10-03 18:30 You may find this of interest. https://gitlab.com/freepascal.org/lazar us/lazarus/-/issues/41033#note_2141894188 ---------------------------------------------------------------------- 440bx · 2024-10-03 18:42 Thank you Martin. Yes, I'm definitely interested in anything that is related to FpDebug's abilities and behavior. ---------------------------------------------------------------------- Red_prig · 2024-10-23 16:53 Martin_fr, I am wondering if it is possible to add a modified fpdebug, for example via an installable package? I need some advanced features on the debugger side, can I write a plugin/package for this? ---------------------------------------------------------------------- Martin_fr · 2024-10-23 16:59 You can register new backends via a package. (That would be a modified LazDebuggerFp, which could then use a modified FpDebug). The existing frontend will use the Intf to communicate with them (so the actions/data) is fixed there. And keep in mind, that intf will change) But you can register additional stuff via IDE intf, and talk directly to your package. ---------------------------------------------------------------------- PascalDragon · 2024-10-24 19:37 Quote from: 440bx on October 01, 2024, 12:56:45 amQuote from: Martin_fr on September 30, 2024, 10:59:03 pmFirst of all => that is talking about dbg files with DWARF info. Since FpDebug only loads dwarf. (Happy to accept merge request for other formats, but currently not on my list). Yes.  I am also definitely limiting the considerations to DWARF info.  Implementing PDB processing requires too many dependencies on proprietary MS technologies which, I believe would not be beneficial to Lazarus. LLDB supports the PDB format (see here). Also Microsoft published information about the format to help LLVM further. LLVM documents it also here. So it isn't that proprietary anymore. ---------------------------------------------------------------------- Martin_fr · 2024-10-24 20:38 Quote from: 440bx on October 01, 2024, 12:56:45 amQuote from: Martin_fr on September 30, 2024, 10:59:03 pmSystem dll on windows usually don't have dwarf info. They use a different format. (afaik) Yes, they use the MS proprietary PDB format but, there is nothing that prevents writing a program to create DWARF symbols for them I wouldn't mind FpDebug to be extended to read it directly. If someone wants to do that. Assuming that there are no licensing issues, but if lldb does then.... ---------------------------------------------------------------------- 440bx · 2024-10-24 21:39 @PascalDragon, I knew about the LLVM folks having fully reversed engineered the PDB format as well as MS' contribution (which doesn't compile!), OTH, the last time I looked at their stuff was a few years ago (more than 4) and, at least at the time, there was no _published_ documentation from LLVM about the PDB format (and following their code was  no picnic!.) I see now that the documentation is much better and, from the link you posted, there is code that seems reasonably usable to understand the format without incurring a very steep learning curve. Thank you for the update.  It's good to know there have been improvements in that area. Quote from: Martin_fr on October 24, 2024, 10:38:32 pmI wouldn't mind FpDebug to be extended to read it directly. If someone wants to do that. Assuming that there are no licensing issues, but if lldb does then.... Layman's opinion:  given LLVM's ability to use the format (which they reversed engineered with only some/partial help from MS), it seems MS is "ok" with third parties using the format (but, it is still suspicious they don't document it.) I've been "looking" at the PDB format for a long time, since Undocumented Windows 2000 by Sven Schrieber.  Since MS doesn't document it, any changes, which there have been a great many through the years, could be problematic for any program directly reading the format. Personally, I'm leaning towards using the "cv2pdb" approach which uses MS provided facilities (API's) to read the PDB and convert the information to DWARF.  I believe that is a safer and more stable approach in the long run.  The downside is: an external ".dbg" must be created and Lazarus be given the ability to find it so it can use it. That said, I certainly would not have anything against someone creating a direct PDB reader in Pascal with could potentially be included into Lazarus but, as previously stated, I think it is risky in the long run.  I can't help feeling uneasy about it. Somewhat related observation about the PDB symbols.  I routinely dis- assemble MS executables (mostly Windows system dlls) and, one of the things that is noticeable is that the debug information they publish has been "fiddled" with to some extent.  This lowers the value of the PDB information they make available.  It would be _very_ nice to augment/correct the PDB information.  I think that, when it comes to creating debugging symbols, the DWARF specification is a more reasonable/safer avenue.  That's the objective that has captured my attention for the time being. ---------------------------------------------------------------------- Alexander (Rouse_) Bagel · 2024-10-26 04:12 Quote from: 440bx on October 24, 2024, 11:39:15 pmThat said, I certainly would not have anything against someone creating a direct PDB reader in Pascal with could potentially be included into Lazarus but, as previously stated, I think it is risky in the long run.  I can't help feeling uneasy about it. I have plans to develop a PDB-Reader in RAW format for my ProcessMemoryMap utility. https://github.com/AlexanderBagel/ProcessMemoryMap Now it works only with debug symbols provided by WinAPI (SymInitialize, SymGetSymFromAddr, etc...), with Delphi MAP files and DWARF(1-4, partially 5). (Sorry, the documentation for the utility is still in Russian, but there is nothing important there) But now work on this utility is on hold as I need to finish work on CPU-View and assembler/disassembler framework (it is needed for my own debugging kernel). As soon as I finish these projects, I will tackle the PDB immediately with the possibility of connecting to Lazarus in mind. ---------------------------------------------------------------------- 440bx · 2024-10-26 04:49 Quote from: Alexander (Rouse_) Bagel on October 26, 2024, 06:12:12 amNow it works only with debug symbols provided by WinAPI (SymInitialize, SymGetSymFromAddr, etc...), with Delphi MAP files and DWARF(1-4, partially 5). I've "played" with the dbghelp functions used to extract information from a PDB.  Mostly proof of concept stuff, nothing hard core. Because of your comment about developing a PDB reader, I have a question for you:  do you know if there is information a direct PDB reader can obtain that is not available using the dbghelp functions ?  (I ask because, if no additional info can be had reading the PDB directly, it seems there is no net advantage to implementing one.) That said, if you developed one, I'd be among the firsts to have a good look at it. BTW, I had already seen your ProcessMemoryMap and one thing that caught my attention was your use of distorm to disassemble code.  I've used distorm and like it but, I've switched to Zydis because it supports practically all intel instructions and it is well tested (and used everyday as it is the disassembler used by the open source x32/64dbg) ---------------------------------------------------------------------- Alexander (Rouse_) Bagel · 2024-10-26 05:23 Quote from: 440bx on October 26, 2024, 06:49:10 amdo you know if there is information a direct PDB reader can obtain that is not available using the dbghelp functions ?  (I ask because, if no additional info can be had reading the PDB directly, it seems there is no net advantage to implementing one.) I haven't really delved into the PDB issue yet, so I can't answer what exactly might be  not available. But I have superficially studied this project, on the basis of which I want to make my own implementation of PDB format reading. https://github.com/MolecularMatters/raw_pdb/tree/main/src As for distorm - its use is a forced measure, in the future it will be replaced by its own disassembler engine (its previous implementation was limited in capabilities), as I need to perform assembly and then modify the code in a remote process. ---------------------------------------------------------------------- 440bx · 2024-10-26 05:59 Quote from: Alexander (Rouse_) Bagel on October 26, 2024, 07:23:33 amBut I have superficially studied this project, on the basis of which I want to make my own implementation of PDB format reading. https://github.com/MolecularMatters/raw_pdb/tree/main/src That project looks exceptionally interesting.  Thank you for that link Alexander, much appreciated. ---------------------------------------------------------------------- Alexander (Rouse_) Bagel · 2026-09-26 15:19 Quote from: 440bx on October 26, 2024, 07:59:52 amQuote from: Alexander (Rouse_) Bagel on October 26, 2024, 07:23:33 amBut I have superficially studied this project, on the basis of which I want to make my own implementation of PDB format reading. https://github.com/MolecularMatters/raw_pdb/tree/main/src That project looks exceptionally interesting.  Thank you for that link Alexander, much appreciated. I added PDB support to Cpu-View ---------------------------------------------------------------------- 440bx · 2026-09-26 15:37 Quote from: Alexander (Rouse_) Bagel on September 26, 2026, 05:19:38 pmI added PDB support to Cpu-View That's great news.   Thank you Alexander. Before anything though, what version of FPC and Lazarus are required to use the version of Cpu-View that supports PDB ? ---------------------------------------------------------------------- Alexander (Rouse_) Bagel · 2026-09-26 15:40 Quote from: 440bx on September 26, 2026, 05:37:28 pmQuote from: Alexander (Rouse_) Bagel on September 26, 2026, 05:19:38 pmI added PDB support to Cpu-View That's great news.   Thank you Alexander. Before anything though, what version of FPC and Lazarus are required to use the version of Cpu-View that supports PDB ? I tested it on the current Stable release, on the fixes-4.0 branch, and on a trunk build from about two weeks ago. ---------------------------------------------------------------------- 440bx · 2026-09-26 15:44 Quote from: Alexander (Rouse_) Bagel on September 26, 2026, 05:40:20 pmI tested it on the current Stable release, on the fixes-4.0 branch, and on a trunk build from about two weeks ago. That most likely means, it won't compile with FPC v3.2.2. As far as a Lazarus version, what version would you recommend using ? (basically, what version(s) have you used/tested Cpu-View in ?) Thanks you again. ---------------------------------------------------------------------- Alexander (Rouse_) Bagel · 2026-09-26 15:47 Quote from: 440bx on September 26, 2026, 05:44:41 pmQuote from: Alexander (Rouse_) Bagel on September 26, 2026, 05:40:20 pmI tested it on the current Stable release, on the fixes-4.0 branch, and on a trunk build from about two weeks ago. That most likely means, it won't compile with FPC v3.2.2. As far as a Lazarus version, what version would you recommend using ? (basically, what version(s) have you used/tested Cpu-View in ?) Thanks you again. Stable runs on this version of FPC Verbose: Free Pascal Compiler version 3.2.2-r0d122c49 [09/07/2026] for x86_64 Other than that, personally, I work strictly on Trunk (FPC and Lazarus)