From c52a98dba20b37236fbf6c20ca07271fa442cada Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 22 Jun 2025 15:31:05 +0200 Subject: [PATCH] FpDebug: Fix attaching to a process, if the project does not provide the filename of the (host) executable. Issue #41201 (cherry picked from commit 7f0a0f7ea64ec1589f7e8a452fc6b3230c041543) --- components/fpdebug/fpdbgcontroller.pas | 37 +++++++++++++------ .../lazdebuggerfp/fpdebugdebugger.pas | 21 ++++++----- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/components/fpdebug/fpdbgcontroller.pas b/components/fpdebug/fpdbgcontroller.pas index 35b4215372..137b685a1f 100644 --- a/components/fpdebug/fpdbgcontroller.pas +++ b/components/fpdebug/fpdbgcontroller.pas @@ -305,6 +305,7 @@ type procedure SetParams(AValue: TStringList); procedure CheckExecutableAndLoadClasses(out ATargetInfo: TTargetDescriptor); + procedure InitForDefaultTargetAndLoadClasses(out ATargetInfo: TTargetDescriptor); protected FMainProcess: TDbgProcess; FCurrentProcess: TDbgProcess; @@ -1554,6 +1555,13 @@ begin FOsDbgClasses := FpDbgClasses.GetDbgProcessClass(ATargetInfo); end; +procedure TDbgController.InitForDefaultTargetAndLoadClasses(out ATargetInfo: TTargetDescriptor); +begin + ATargetInfo := hostDescriptor; + + FOsDbgClasses := FpDbgClasses.GetDbgProcessClass(ATargetInfo); +end; + procedure TDbgController.SetExecutableFilename(const AValue: string); begin if assigned(FMainProcess) then @@ -2163,19 +2171,26 @@ begin Result := nil; assert(FMainProcess = nil, 'TDbgController.CreateDbgProcess: FMainProcess = nil'); - if FExecutableFilename = '' then begin - DebugLn(DBG_WARNINGS, 'No filename given to execute.'); - FLastError := CreateError(fpInternalErr, ['No filename given to execute.']); - Exit; - end; - if not FileExists(FExecutableFilename) then begin - DebugLn(DBG_WARNINGS, 'File %s does not exist.',[FExecutableFilename]); - FLastError := CreateError(fpInternalErr, ['File does not exist: ' + FExecutableFilename]); - Exit; + if AttachToPid = 0 then begin + if FExecutableFilename = '' then begin + DebugLn(DBG_WARNINGS, 'No filename given to execute.'); + FLastError := CreateError(fpInternalErr, ['No filename given to execute.']); + Exit; + end; + if not FileExists(FExecutableFilename) then begin + DebugLn(DBG_WARNINGS, 'File %s does not exist.',[FExecutableFilename]); + FLastError := CreateError(fpInternalErr, ['File does not exist: ' + FExecutableFilename]); + Exit; + end; + + // Get exe info, load classes + CheckExecutableAndLoadClasses(TargetDescriptor); + end + else begin + // Attach, get debug classes for the host system + InitForDefaultTargetAndLoadClasses(TargetDescriptor); end; - // Get exe info, load classes - CheckExecutableAndLoadClasses(TargetDescriptor); if not Assigned(OsDbgClasses) then begin DebugLn(DBG_WARNINGS, 'Error - No support registered for debug target'); FLastError := CreateError(fpInternalErr, ['Unsupported target for file: ' + FExecutableFilename+'.'#13#10 + diff --git a/components/lazdebuggers/lazdebuggerfp/fpdebugdebugger.pas b/components/lazdebuggers/lazdebuggerfp/fpdebugdebugger.pas index 6438129e9e..f8305bb6fb 100644 --- a/components/lazdebuggers/lazdebuggerfp/fpdebugdebugger.pas +++ b/components/lazdebuggers/lazdebuggerfp/fpdebugdebugger.pas @@ -4157,7 +4157,19 @@ begin {$ifdef windows} FDbgController.ForceNewConsoleWin:=TFpDebugDebuggerProperties(GetProperties).ForceNewConsole; {$endif windows} + + FDbgController.AttachToPid := 0; + if ACommand = dcAttach then begin + FDbgController.AttachToPid := StrToIntDef(String(AParams[0].VAnsiString), 0); + Result := FDbgController.AttachToPid <> 0; + if not Result then begin + FileName := ''; + Exit; + end; + end; + // Check if CreateDbgProcess returns a valid TDbgProcess +// if ACommand <> dcAttach then begin if Assigned(FDbgController.CurrentProcess) then begin FDbgController.CurrentProcess.Config.UseConsoleWinPos := FUseConsoleWinPos; FDbgController.CurrentProcess.Config.UseConsoleWinSize := FUseConsoleWinSize; @@ -4175,15 +4187,6 @@ begin FDbgController.CurrentProcess.Config.BreakpointSearchMaxLines := TFpDebugDebuggerProperties(GetProperties).BreakpointSearchMaxLines; - FDbgController.AttachToPid := 0; - if ACommand = dcAttach then begin - FDbgController.AttachToPid := StrToIntDef(String(AParams[0].VAnsiString), 0); - Result := FDbgController.AttachToPid <> 0; - if not Result then begin - FileName := ''; - Exit; - end; - end; FWorkQueue.Clear; FWorkQueue.ThreadCount := 1; {$IFDEF FPDEBUG_THREAD_CHECK} CurrentFpDebugThreadIdForAssert := FWorkQueue.Threads[0].ThreadID;{$ENDIF}