Recent

Author Topic: Problem with "Attach to program..."  (Read 1915 times)

korba812

  • Sr. Member
  • ****
  • Posts: 447
Problem with "Attach to program..."
« on: October 21, 2024, 09:39:32 pm »
I am trying to debug shared library running with an external, non-Pascal program using "Attach to program...". I can attach to program however I cannot use the "Step..." (key F7, F8, etc.), tool button and menu items "Run", "Step..." remain inactive, however, I can resume the program with the F9 key and breakpoints works. The GDB backend and FpDebug behave the same way. I'm using lazarus/main and am wondering if this is a problem with my configuration or just a bug?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10686
  • Debugger - SynEdit - and more
    • wiki
Re: Problem with "Attach to program..."
« Reply #1 on: October 21, 2024, 10:04:01 pm »
Need a bit more detail...

When you do "attach" and choose the program. Then the program would still be  running => so the pause button should be active, but not the run or step buttons.
Though it may be dependent on Lazarus version, maybe the app is paused. In that case run it, and later pause it.

Then when you hit pause => you should be getting the asm view, and should be able to single step asm instructions.




Now if your main app has debug info (in a version readable to the debugger) then you should be able to set breakpoints, and once you reach a breakpoint you should be able to single step.

What I don't know... You have the debug info in a library. And there may be a problem (may or may not be / I am not sure). Because on attach the debugger (at least fpdebug, I don't know the internals of gdb) reads debug info of the main app. But I am not sure if it reads debug info of the library.

At least fpdebug usually reads debug info for libraries when the lib is loaded. But when attaching that may have happened when the debugger was not looking...



So the real question here is, when you pause the app => and you have files for the library open in the IDE editor => do they show little blue dots in the gutter? If not then debug info wasn't loaded, or the filename is not detected correctly.

You can try to set the external app as launch application in "run" > "Run Parameters" => and then it runs in the debugger, and the debugger should see when the lib is loaded.

korba812

  • Sr. Member
  • ****
  • Posts: 447
Re: Problem with "Attach to program..."
« Reply #2 on: October 21, 2024, 10:33:36 pm »
After attaching, program stops, blue dots appear on the gutter but menu items and buttons remain inactive. After setting a breakpoint and resuming the program with the F9 key, debugger stops at the breakpoint, I can preview contents of variables in the hint window (although the "Evaluate/modify" window is unavailable) but still the buttons and menu items are inactive and "Step..." (F7/F8)  does not work.

But running through external applications from "Run parameters" works fine!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10686
  • Debugger - SynEdit - and more
    • wiki
Re: Problem with "Attach to program..."
« Reply #3 on: October 21, 2024, 10:49:31 pm »
ok, in this case the step buttons should become active.

I will have to see if I can reproduce this somehow...

I noted in your image, the run button looks different (looks like the symbol for "run without debug". Maybe something to do with that. (I have to figure out how to set it to that mode).

Another thing (even I could not get it to fail): How did you start the exe to which you attach?
Did you start it with "Run without debug" from the IDE?
As, I said, it did not fail in my tests right now, but that could IIRC cause issues. Because the IDE is watching for the process to terminate (even without debugger), and when you attach it, the IDE monitors the same process twice => and the the debugger (at least FpDebug) can be hindered by that.

korba812

  • Sr. Member
  • ****
  • Posts: 447
Re: Problem with "Attach to program..."
« Reply #4 on: October 21, 2024, 11:16:51 pm »
Program I attach to is run outside of Lazarus - it is cscript.exe - windows script host run from the command line. Shared library contains COM objects and type library, if that matters.

edit

I attach a sample library and wsh script for testing.
« Last Edit: October 22, 2024, 12:30:32 am by korba812 »

Thaddy

  • Hero Member
  • *****
  • Posts: 16408
  • Censorship about opinions does not belong here.
Re: Problem with "Attach to program..."
« Reply #5 on: October 22, 2024, 10:19:37 am »
It is better to simply integrate it, not use an external. This has been the case for over 25 years and there are many examples.
(one of which I wrote for KOL 20+ years ago: kolscript.)
« Last Edit: October 22, 2024, 03:01:52 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10686
  • Debugger - SynEdit - and more
    • wiki
Re: Problem with "Attach to program..."
« Reply #6 on: October 22, 2024, 01:13:54 pm »
It will probably be some time till I can test that in detail.

I did some quick tests with my own test-apps (On Windows 10)

- FpDebug currently fails with an error, if you try to attach while a library project is open in the IDE.

- GDB does attach
=> GDB indeed pauses the app on attach
=> GDB loads the library info and sets the breakpoints and stops there.

For me, in my test apps the step button were active whenever the app was paused.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10686
  • Debugger - SynEdit - and more
    • wiki
Re: Problem with "Attach to program..."
« Reply #7 on: October 22, 2024, 01:14:47 pm »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10686
  • Debugger - SynEdit - and more
    • wiki
Re: Problem with "Attach to program..."
« Reply #8 on: October 22, 2024, 05:25:12 pm »
Ok, I can reproduce.

The IDE considers a library project as not run-able (except if it has a host app).
Yet the attach will set the debugger into running mode. But the IDE remains convinced that running is not possible. So it wont let you have the run/step buttons.

As a workaround:
If you want to attach to any app, while having a library project open => put the exe into the "host app" of the run parameters.
You can still start it outside the IDE, and then attach to it.
You just need something in that setting field. (mind that the debugger will be given that exe as fallback for debug info, if it can't get the info from the attach operation / or depending on gdb internals / so either use the correct exe, or something without debug info / or - not tested - a non existing app)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10686
  • Debugger - SynEdit - and more
    • wiki
Re: Problem with "Attach to program..."
« Reply #9 on: October 22, 2024, 05:42:25 pm »

korba812

  • Sr. Member
  • ****
  • Posts: 447
Re: Problem with "Attach to program..."
« Reply #10 on: October 22, 2024, 10:40:44 pm »
Thanks for the patch but unfortunately it didn't help. Interestingly, I tried attaching to a program written in c and using a regular (with c interface, no COM) library written in fpc and it works fine. The problem is only with the COM library.

Thaddy

  • Hero Member
  • *****
  • Posts: 16408
  • Censorship about opinions does not belong here.
Re: Problem with "Attach to program..."
« Reply #11 on: October 23, 2024, 11:32:57 am »
That is a bit strange, because kolscript still works (also on fpc) but I wrote it decades ago and I do not fully understand my own code. Will have a look.
There is nothing wrong with being blunt. At a minimum it is also honest.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10686
  • Debugger - SynEdit - and more
    • wiki
Re: Problem with "Attach to program..."
« Reply #12 on: October 26, 2024, 05:35:45 pm »
Once I compiled your demo, what exact commandline do I need to start it? From cmd.exe ?

korba812

  • Sr. Member
  • ****
  • Posts: 447
Re: Problem with "Attach to program..."
« Reply #13 on: October 26, 2024, 09:33:25 pm »
I run “cscript.exe test.js” from cmd. I am attaching simple lazarus project which you can also use.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10686
  • Debugger - SynEdit - and more
    • wiki
Re: Problem with "Attach to program..."
« Reply #14 on: October 27, 2024, 12:41:15 am »
I compiled the dll, and the exe.

The exe crashes "with invalid class string".

I don't use OLE myself, so I am doing that part blind, if there are any steps I need to do, then you need to tell me.


I don't have the "test.js"

 

TinyPortal © 2005-2018