Problem with "Attach to program..." lazarus / 12-Debugger ---------------------------------------------------------------------- korba812 · 2024-10-21 19:39 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 · 2024-10-21 20:04 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 · 2024-10-21 20:33 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 · 2024-10-21 20:49 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 · 2024-10-21 21:16 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. ---------------------------------------------------------------------- Thaddy · 2024-10-22 08:19 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.) ---------------------------------------------------------------------- Martin_fr · 2024-10-22 11:13 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 · 2024-10-22 11:14 For the FpDebug part: https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41201 ---------------------------------------------------------------------- Martin_fr · 2024-10-22 15:25 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 · 2024-10-22 15:42 You can try this patch (for the button issue): https://gitlab.com/free pascal.org/lazarus/lazarus/- /commit/3fa54762f2ea84846397a218b43a22a32f75805d.patch ---------------------------------------------------------------------- korba812 · 2024-10-22 20:40 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 · 2024-10-23 09:32 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. ---------------------------------------------------------------------- Martin_fr · 2024-10-26 15:35 Once I compiled your demo, what exact commandline do I need to start it? From cmd.exe ? ---------------------------------------------------------------------- korba812 · 2024-10-26 19:33 I run “cscript.exe test.js” from cmd. I am attaching simple lazarus project which you can also use. ---------------------------------------------------------------------- Martin_fr · 2024-10-26 22:41 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" ---------------------------------------------------------------------- korba812 · 2024-10-26 22:49 After compiling library, you should register it with "regsvr32 comdemolib.dl" command. You will find "test.js" script in "wsh" directory in archive with the library. ---------------------------------------------------------------------- Martin_fr · 2024-10-27 11:19 I managed to register it (needed to restart the test system to get rid of it again...).  But I still got errors. Lets go a different way. Please start you Lazarus (from a command line) with Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lazarus.exe  --debug-log=LOG_FILE  --debug-enable=DBGMI_TYPE_INFO,DBG_CMD_ECHO,DBG _STATE,DBG_DATA_MONITORS,DBGMI_QUEUE_DEBUG,DBG_ERRORS,DBG_VERBOSE,DBG_ WARNINGS,DBG_THREAD_AND_FRAME   And attach the resulting log file. Thanks. ---------------------------------------------------------------------- korba812 · 2024-10-27 12:03 It's really strange. I run lazarus with indicated parameters and the problem does not occur. Then I ran normally through startlazarus and I can no longer repeat the problem either. I did not update or rebuild lazarus at that time, that night I tried and the problem occurred.... What happened? Thanks for your help and sorry for the lost time. If the problem returns I will attach a log file. P.S. Thanks to this problem, I had the opportunity to switch from GDB to FpDebug and will stay that way - great job!