Lazarus

Using the Lazarus IDE => Debugger => Topic started by: calebs on September 02, 2020, 11:09:16 pm

Title: problem executting tprocess with fpdebug
Post by: calebs on September 02, 2020, 11:09:16 pm
Hello all!
I've tried to debug a program that uses tprocess to call another executable and with fpdebug always returns
"failed to execute : 50".
Switched to gdb and worked ok.
Is there an option to solve this or is a missing feature?
Thanks
i'll post the code below

Code: Pascal  [Select][+][-]
  1.     ejecutable:=ruta+ejecutablerece;
  2.     prog:=TProcess.CREATE(nil);
  3.     prog.Executable:=ejecutable;
  4.     prog.Parameters.Add('/ult');
  5.     prog.Parameters.Add(tc);
  6.     prog.Parameters.Add(pdv);
  7.     prog.options:=prog.options+[powaitonexit, poUsePipes];
  8.     prog.ShowWindow:=swoMinimize;
  9.     prog.Execute;                                     >> here gives error
  10.     comprob:='-1';                                        
  11.  
Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on September 03, 2020, 01:02:35 am
I can not reproduce (trunk and 2.0.10).
I used gdb.exe for testing. And that is executed.

But there may be many factors.

That is an "project raised exception....failed to execute"?

Error 50, does not seem to point to it, but could it be that some libraries are missing? or the environment path differs?
Error 50 => "ERROR_NOT_SUPPORTED"

Anything else about the exe, that could depend on environment, permissions or the like?
Title: Re: problem executting tprocess with fpdebug
Post by: bonmario on September 03, 2020, 07:52:39 am
Hello all!
I've tried to debug a program that uses tprocess to call another executable and with fpdebug always returns
"failed to execute : 50".

Same problem for me: Windows 10 64 bit, with Lazarus 2.1.0 r63602 FPC 3.2.0 i386-win32-win32/win64
Same project, with gdb works.

Hi, Mario
Title: Re: problem executting tprocess with fpdebug
Post by: marcov on September 03, 2020, 12:51:50 pm
Sounds like a working dir problem, executing with relative paths, and gdb changing workdir to exe dir, and fpdebug not or so?
Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on September 03, 2020, 02:28:35 pm
FpDebug also set a working dir (as given by the IDE,but that seems to be the correct one / it can be changed in "run params")

But that would affect the app that then calls TProcess. That could lead to an incorrect Path, but then the error would/should be: file not found.

I also just created a new user account on my system, without any admin privileges. But even then, I was able to start a new app with TProcess.

So without any additional info, I cannot do much about this.
Title: Re: problem executting tprocess with fpdebug
Post by: calebs on February 25, 2021, 03:53:20 pm
Hello again, sorry to update this post but i prefer instead open a new one.
I still having problems with spawning process or applications in windows with tprocess.execute.
I 've traced new (useful?) information for debug (i guess).
i have a main program/unit that has on some procedure a tprocess.execute
in the main unit/program the execution worked ok but
i have an external unit that also have a tprocess.execute that calls some other program and there gives the 50 error when using fpdebug.
If i switch to gdb the same application/unit worked as expected with no errors.
it not makes difference if i use dwarf2 or dwarf3.
The problem seems to ocurr when a process.execute is called from other unit.
I home this throw some light at this problem.
Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on February 25, 2021, 04:33:29 pm
Do you have a sample app with source code, which reproduces the issue?

What app do you launch in TProcess? 
Have you tried specifying the full path?
Is the app in question a console app? (then have you tried in Tools > Options > Debugger: "ForceNewConsole" in the property grid?)

Can you use TProcess on   C:\Windows\System32\notepad.exe  ?
Does that work?

Have you tried to run Lazarus as administrator?
(Directly start lazarus.exe / skip startlazarus.exe)
Title: Re: problem executting tprocess with fpdebug
Post by: bonmario on February 25, 2021, 04:57:51 pm
See the attached project.

Works with GDB, it return error 50 with fpdebug

Hi, Mario
Title: Re: problem executting tprocess with fpdebug
Post by: bonmario on February 25, 2021, 05:00:07 pm
If i change executable with 'C:\Windows\System32\notepad.exe' it works also with fpdebug
Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on February 26, 2021, 12:18:36 am
The path from your sample does not exist on my system.

If I change it, then it works (under fpdebug / 2.0.11 and trunk)
I did compile your code to 64 and 32 bit (but tested both in a 64bit IDE)
=> So if your IDE is 32bit that may still need testing.

Your path: C:\Programmi\Notepad++\notepad++.exe
mine:  C:\Program Files (x86)\Notepad++\notepad++.exe'

Is it possible that you are running into some UAC stuff?
(I may still test with a more restricted User account, but not now, got to many stuff open in my session)



Btw, what happens, if in the IDE, you choose "run without debugging" ?



You could play with
File:   components\fpdebug\fpdbgwinclasses.pas
Line:  650    in TDbgWinProcess.StartInstance

Code: Pascal  [Select][+][-]
  1.     AProcess.Options:=[poDebugProcess, poNewProcessGroup];

"poDebugProcess" has to be kept. Actually you can try to replace it with poDebugOnlyThisProcess


But you can try removing "poNewProcessGroup", or add others...
Not sure if that makes much of a difference.
Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on February 26, 2021, 12:22:05 am
Same problem for me: Windows 10 64 bit, with Lazarus 2.1.0 r63602 FPC 3.2.0 i386-win32-win32/win64

If I read correct:
=> Your IDE is 64 bit?
=> You have a 32bit cross compiler
=> so your project app maybe is 32bit?
Title: Re: problem executting tprocess with fpdebug
Post by: bonmario on February 26, 2021, 08:21:35 am
Hi,
sorry, you're right, in the Italian version of Windows, "C:\ Programmi" is a link to "C: \ Program Files".
I still tried to put in the path "C:\ Program Files", but I get the same error.

As you suggested, I tried this:
Code: Pascal  [Select][+][-]
  1.     //AProcess.Options:=[poDebugProcess, poNewProcessGroup];
  2.     AProcess.Options:=[poDebugOnlyThisProcess, poNewProcessGroup];
  3.  
After rebuilding the IDE it works !!!

My situation is:
- Windows 64 bit
- Lazarus 2.1.0 r64457 FPC 3.2.0 i386-win32-win32/win64
- Notepad++ 64 bit

Thanks, Mario
Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on February 26, 2021, 03:10:31 pm
Ok, this is a valid solution.  And I will probably change it in the IDE for now. That is, as long as the debugger does not debug processes launched by the debuggee (no plans for that yet).

At the moment, with poDebugProcess, fpdebug is notified on forked processes, and fpdebug releases them into the wild.

Still curious why you run into it.

It would make sense, if your IDE was 32 bits (I have not tested on my end, if it actually triggers it).
With a 32bit IDE, the included fpdebug is of course 32bit too. And a 32bit debugger can not debug a 64bit app. (So the OS would not allow a 64bit process being forked).

But if the IDE is 64 bit.....

Just to be sure that you did not accidentally rebuild the IDE. Please open the Task Manager (ctrl-shift escape on win 10), and when the IDE is running find it in the list (looking at the first tab "processes", not "details"). Make sure neither the IDE (nor its sub entries) have an appendix "(32 bit)"

Btw, by writing those steps down, I noted: I have a 32bit version of notepad++ installed. So that may be a difference there.
Maybe windows already can not tell a 64 bit debugger, that a 32 bit process launches a 64bit exe. After all, to debug 32 bits the 64bit debugger must use a special WOW64 api, which deals with 32 bits only......

Well anyway, we know how to fix it....


https://bugs.freepascal.org/view.php?id=38546
Title: Re: problem executting tprocess with fpdebug
Post by: bonmario on February 26, 2021, 03:27:47 pm
Sorry, I probably expressed myself wrong: my IDE is 32-bit.
Title: Re: problem executting tprocess with fpdebug
Post by: calebs on February 26, 2021, 11:57:12 pm
Hi,
sorry, you're right, in the Italian version of Windows, "C:\ Programmi" is a link to "C: \ Program Files".
I still tried to put in the path "C:\ Program Files", but I get the same error.

As you suggested, I tried this:
Code: Pascal  [Select][+][-]
  1.     //AProcess.Options:=[poDebugProcess, poNewProcessGroup];
  2.     AProcess.Options:=[poDebugOnlyThisProcess, poNewProcessGroup];
  3.  
After rebuilding the IDE it works !!!

My situation is:
- Windows 64 bit
- Lazarus 2.1.0 r64457 FPC 3.2.0 i386-win32-win32/win64
- Notepad++ 64 bit

Thanks, Mario

Hello. I've modified the file you suggest, noadaways the line was 470
Code: Pascal  [Select][+][-]
  1. ...
  2. class function TDbgWinProcess.StartInstance(AFileName: string; AParams, AnEnvironment: TStrings; AWorkingDirectory, AConsoleTty: string;
  3.   AOnLog: TOnLog; ReDirectOutput: boolean): TDbgProcess;
  4. var
  5.   AProcess: TProcessUTF8;
  6. begin
  7.   result := nil;
  8.   AProcess := TProcessUTF8.Create(nil);
  9.   try
  10.     //AProcess.Options:=[poDebugProcess, poNewProcessGroup];    
  11.     AProcess.Options:=[poDebugOnlyThisProcess, poNewProcessGroup];
  12. ...

but it gives me anyway error 50 when i run it.
i've modified that file and went to tools>configure build lazarus>clean & build everything and checked clean all
but it gives me error 50 anyway when process.execute is run from other unit than the main unit.
btw im using lazarus 32 bits on windows 64 bits. All the executables are 32 bits
Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on February 27, 2021, 08:14:04 pm
Ok, so I build a 32bit IDE for testing.

If the 32bit debuggee attempts to launch a 64bit app, I do get the error.
If I change to poDebugOnlyThisProcess => I no longer get the error.

I was not able to reproduce any other cases.

I will test poDebugOnlyThisProcess for a while, and change FpDebug accordingly at some time (not sure if it will hit the next release).


One more note, I have not tested this in relation to any debugger, but remember having had issues (not related to the IDE nor any debugger)
If notepad++ was "run as admin" and is running, then if you try to open another file in notepad++ (and that starts as normal user) there will be an error.


Out of interest...
As for works in gdb. Can you verify it is a 32bit gdb? There are after all 64bit versions of gdb too....
Though gdb may well set up things differently, but I am not going to dive into gdb sources at this time...


The easiest way for you to use fpdebug and (hopefully) not have the troubles is to instal Lazarus 64bit. And a cross compiler.

You can still develop your own apps as 32 bit apps. (You just need to change the target settings in project options).

But with the 64bit IDE comes 64bit fpdebug, and in my tests, with that there were no permission issues.
Title: Re: problem executting tprocess with fpdebug
Post by: calebs on February 28, 2021, 03:57:32 pm
Hello and thanks for taking the time to test all these.
I've had mixed experiencies with lazarus 64 bits, i develop much for low end machines and always work in 32 bits, even in linux with (the little) distros in 32bits... i've had a traumatical experience switching to 64bits and some currency functions that don't work aynmore so i stay away as long as i can from 64 bits lazarus because i have to modify a lot of the set of programs. I will take a closer look to the compiler target settings .
All the executables that i call in tprocess are 32 bits, i think. I have one that is an compiled python script that i have my doubts and i'll double check if it is 64 bit or 32.
Can you tell me how to change poDebugOnlyThisProcess ? i've changed the code in components\fpdebug\fpdbgwinclasses.pas and recompiled all lazarus but it keeps giving error 50.
Thx!
Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on February 28, 2021, 04:22:33 pm
Can you tell me how to change poDebugOnlyThisProcess ? i've changed the code in components\fpdebug\fpdbgwinclasses.pas and recompiled all lazarus but it keeps giving error 50.

That is exactly what I did. This is the only occurrence of poDebugProcess. And that I changed. Rebuild and done.

And searching gdb sources shows they use that flag too. Under some condition they use both together? But not sure when.
They also use CreateNewProcessGroup.

I have only searched the keywords in gdb, I have not studied the code around them.



If you cross compile to 32 bit, then you should get the exact same results, as if you compile in a 32 bit IDE. Its just the IDE that differs.

At least if you download the cross compiler add on from the lazarus download. => Because the lazarus-2.0.12-fpc-3.2.0-cross-i386-win32-win64.exe is really a native 32 bit fpc in disguise. It has just been renamed to be found as cross compiler.

But you can even go one step further. Install (or keep installed) the 32 bit compiler. Tools > Option: point to the 32 bit compiler (no cross compiling).
Your 64 bit IDE will compile all apps to 32 bit (you do NOT even have to change the targed in Project-Options, you keep it empty/default).
Only to rebuild the IDE, you need to then change back to the 64 bit fpc.
And of course any package in the IDE will be 64 bit, but in your app that package will be 32 bit. So that only matters if you write your own packages for the IDE.

Not saying I want to force you to a 64bit IDE. I would be happy to fix that do whatever extend is possible.
But currently I am not sure where to look....



On my tests, when the 64 bit app failed to launch, there was no event that Windows sent to FpDebug. So there was no answer from fpdebug that could have affected it.
It was simply that a debugger was attached in WOW64 mode. (32 bit on 64 bit OS)
You can add
  -dDebuglnWinDebugEvents
and run the ide with
  --debug-log=c:\file.log
it and I can see if I can spot something.

(If it compiles also uncomment all calls to "//DumpEvent" in fpdbgwinclasses.pas . BUt only if it compiles)


You can try to start without debugger, and attach (before the external process is launched). Just to see if it makes any diff....
But even if...


Again: You checked your gdb (with witch you said it worked) is actually 32 bit ?
While debugging, in the taskmanager it has the "(32bit)" suffix?

Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on March 02, 2021, 02:05:23 am
I don't know if will make a diff, but have you tried to specify both: poDebugProcess and poDebugOnlyThisProcess?

https://docs.microsoft.com/en-us/windows/win32/procthread/process-creation-flags
Quote
DEBUG_PROCESS.
If this flag is combined with DEBUG_ONLY_THIS_PROCESS,

It seems each one on its own can be given, but also both together.
Title: Re: problem executting tprocess with fpdebug
Post by: calebs on March 02, 2021, 11:02:06 pm
Hello all.
After research a little find that the compiled python script was 64 bits (reading it with an hex editor and microsoft pe guides) so i proceed to delete lazarus x86 and install lazarus x64 with the addon to compile in 32bits.
Have to modify the programs (i still have some bugs for sure, i assume a lot for rounding with decimal types) and after hit my head in the wall several times with random hangs in debugging with fpdebug and gdb, i found that i had to remove all the precompiled units (ppu's and alike) and it finally worked out without the 50 errror message when i call the other executables with tprocess.
I guess the problem was indeed calling a 64 bit executable within a 32bit ide debugger.
I don't know if it fail also if i call a 32bit program in a 64bit ide but i guess soon i will find out.
I'll take a note of your suggestions to modify fpdebug source if that error appear.
it will be a fix for this? i mean in a future version of lazarus would this work?
Thanks for the support.
Title: Re: problem executting tprocess with fpdebug
Post by: ASBzone on March 03, 2021, 12:53:03 am
it will be a fix for this? i mean in a future version of lazarus would this work?
Thanks for the support.

Thanks for following up here.

If what you are asking is:  "In the future, will a 32-bit process ever be allowed to call a 64-bit one" then the answer is "No, not directly."  This is not a Lazarus or FPC issue...
Title: Re: problem executting tprocess with fpdebug
Post by: Martin_fr on March 03, 2021, 01:11:16 am
The issue is about the DebugThisProcessOnly flag. And yes I have plans to add that.

A 32 bit IDE can always only debug 32bit processes. (or must use an external 64bit debugger).

But that 32bit debuggee can start 64bit processes. That is, if the debugger has declared that it does not need/want to know about them. In my tests that flag has worked. So it will at least fix some of the issues reported here.
TinyPortal © 2005-2018