Recent

Author Topic: [Solved] Attach GDB to a TDaemon application  (Read 5987 times)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12302
  • Debugger - SynEdit - and more
    • wiki
Re: Attach GDB to a TDaemon application
« Reply #15 on: February 12, 2022, 06:12:23 pm »
It seems though, that IsDebuggerPresent is part of the Windows unit, so I suspect it won't work with Linux.

To be tested... A more generic approach
Code: Pascal  [Select][+][-]
  1. var WaitForDbg: Ingeger; // Make this a GLOBAL var
  2. begin
  3.   WaitForDbg := 0;
  4.   while WaitForDbg < 1 do
  5.     sleep(10);

Once attached, while the app is paused, go to the debuggers "evaluate / modify" window. Enter WaitForDbg, and the modify it to "1" (need to press enter in the modify input field).

ArminLinder

  • Sr. Member
  • ****
  • Posts: 320
  • Keep it simple.
Re: Attach GDB to a TDaemon application
« Reply #16 on: February 14, 2022, 04:28:09 pm »
I did some more testing with this [new to me] functionality, and I need to correct myself in some respects. All tests done 2/2022 on Windows 11 with Lazarus 2.2.0/FPC 3.2.2.

I could finally debug my daemon regardless of whether I ran it as system or under a specific service user account, and it is not necessary to  use the same account for Lazarus and the daemon. All I needed is to run Lazarus with elevated privilege. The handling of the debugger controls in Lazarus needs a bit of attention and practice when one does it the first time. And FPDebug didn't work, "Run - Attach to program" never got active, unless I changed the debugger in project options to GDB.

PSExec has many options which may be handy in some scenarios, but is not specifically necessary, with one exception: Windows does AFAIK not allow one to "runas" Lazarus under the "system" account, so if anyone, for any reason, wants to run Lazarus under the system account, he needs to do that using PsExec. For my purposes "run as administrator" from the Lazarus.exe context menu did the job nicely.

IsDebuggerPresent is a great aid for Windows, I still wonder whether there is an equivalent for Linux. Sure, there is that "change loop variable using the debugger" workaround mentioned above, but ... :-)

Thanks again to Martin, whose initial post showed me the right way!

Armin.


« Last Edit: February 14, 2022, 04:51:40 pm by Nimral »
Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12302
  • Debugger - SynEdit - and more
    • wiki
Re: Attach GDB to a TDaemon application
« Reply #17 on: February 14, 2022, 05:01:27 pm »
And FPDebug didn't work, "Run - Attach to program" never got active, unless I changed the debugger in project options to GDB.
Have you tried "Reset Debugger", before attaching? Should not be needed, but if the menu entry is grayed out....

The menu entry is enabled for me. I haven't attached to a service, but I did attach to other processes.

However, unlike GDB the process will not be paused (so you don't notice it did attach) => you can pause it with the pause button.
Maybe that should be changed....

ArminLinder

  • Sr. Member
  • ****
  • Posts: 320
  • Keep it simple.
Re: Attach GDB to a TDaemon application
« Reply #18 on: February 14, 2022, 05:06:05 pm »
I tried, but the "Run" - "Attach to program" gets deactivated (grayed), as soon as I switch the debugger from GDB to fpDebug.




« Last Edit: February 14, 2022, 05:14:56 pm by Nimral »
Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12302
  • Debugger - SynEdit - and more
    • wiki
Re: Attach GDB to a TDaemon application
« Reply #19 on: February 14, 2022, 05:15:46 pm »
Sorry, didn't read your footer (in case the 2.0.12 applies). You need 2.2 for "attach" with FpDebug.

ArminLinder

  • Sr. Member
  • ****
  • Posts: 320
  • Keep it simple.
Re: Attach GDB to a TDaemon application
« Reply #20 on: February 14, 2022, 05:19:13 pm »
I am on latest 2.2.0/FPC 3.2.2
Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12302
  • Debugger - SynEdit - and more
    • wiki
Re: Attach GDB to a TDaemon application
« Reply #21 on: February 14, 2022, 05:27:02 pm »
Strange... No idea.

 

TinyPortal © 2005-2018