FpDebug: Make the TDbgProcess instance available as soon as the filename is set. E.g. to allow setting properties on the process before calling StartInstance.

This commit is contained in:
Martin 2024-01-27 12:47:07 +01:00
parent b69afbdd4f
commit 5fca66cad9

View File

@ -292,6 +292,7 @@ type
FWorkingDirectory: string; FWorkingDirectory: string;
// This only holds a reference to the LazDebugger instance // This only holds a reference to the LazDebugger instance
FProcessConfig: TDbgProcessConfig; FProcessConfig: TDbgProcessConfig;
function GetCurrentProcess: TDbgProcess;
function GetCurrentThreadId: Integer; function GetCurrentThreadId: Integer;
function GetDefaultContext: TFpDbgLocationContext; function GetDefaultContext: TFpDbgLocationContext;
procedure SetCurrentThreadId(AValue: Integer); procedure SetCurrentThreadId(AValue: Integer);
@ -308,6 +309,7 @@ type
FCurrentThread: TDbgThread; FCurrentThread: TDbgThread;
FCommand, FCommandToBeFreed: TDbgControllerCmd; FCommand, FCommandToBeFreed: TDbgControllerCmd;
function GetProcess(const AProcessIdentifier: THandle; out AProcess: TDbgProcess): Boolean; function GetProcess(const AProcessIdentifier: THandle; out AProcess: TDbgProcess): Boolean;
function CreateDbgProcess: TDbgProcess;
public public
constructor Create(AMemManager: TFpDbgMemManager; AMemModel: TFpDbgMemModel); virtual; constructor Create(AMemManager: TFpDbgMemManager; AMemModel: TFpDbgMemModel); virtual;
destructor Destroy; override; destructor Destroy; override;
@ -338,7 +340,7 @@ type
property ExecutableFilename: string read FExecutableFilename write SetExecutableFilename; property ExecutableFilename: string read FExecutableFilename write SetExecutableFilename;
property AttachToPid: Integer read FAttachToPid write FAttachToPid; property AttachToPid: Integer read FAttachToPid write FAttachToPid;
property CurrentProcess: TDbgProcess read FCurrentProcess; property CurrentProcess: TDbgProcess read GetCurrentProcess;
property CurrentThread: TDbgThread read FCurrentThread; property CurrentThread: TDbgThread read FCurrentThread;
property CurrentThreadId: Integer read GetCurrentThreadId write SetCurrentThreadId; property CurrentThreadId: Integer read GetCurrentThreadId write SetCurrentThreadId;
property MainProcess: TDbgProcess read FMainProcess; property MainProcess: TDbgProcess read FMainProcess;
@ -1520,8 +1522,12 @@ end;
procedure TDbgController.SetExecutableFilename(const AValue: string); procedure TDbgController.SetExecutableFilename(const AValue: string);
begin begin
if assigned(FMainProcess) then
raise Exception.Create('ExecutableFilename can not be changed while running');
if FExecutableFilename=AValue then Exit; if FExecutableFilename=AValue then Exit;
FExecutableFilename:=AValue; FExecutableFilename:=AValue;
FreeAndNil(FCurrentProcess);
end; end;
procedure TDbgController.SetEnvironment(AValue: TStrings); procedure TDbgController.SetEnvironment(AValue: TStrings);
@ -1535,6 +1541,14 @@ begin
Result := FCurrentThread.ID; Result := FCurrentThread.ID;
end; end;
function TDbgController.GetCurrentProcess: TDbgProcess;
begin
if (FCurrentProcess = nil) and (FMainProcess = nil) then
FCurrentProcess := CreateDbgProcess;
Result := FCurrentProcess;
end;
function TDbgController.GetDefaultContext: TFpDbgLocationContext; function TDbgController.GetDefaultContext: TFpDbgLocationContext;
begin begin
Result := FStoredDefaultContext; Result := FStoredDefaultContext;
@ -1631,43 +1645,20 @@ var
begin begin
result := False; result := False;
FLastError := NoError; FLastError := NoError;
if assigned(FMainProcess) then
begin if assigned(FMainProcess) then begin
DebugLn(DBG_WARNINGS, 'The debuggee is already running'); DebugLn(DBG_WARNINGS, 'The debuggee is already running');
Exit; Exit;
end; end;
if FExecutableFilename = '' then if FCurrentProcess = nil then
begin FCurrentProcess := CreateDbgProcess;
DebugLn(DBG_WARNINGS, 'No filename given to execute.'); if not Assigned(FCurrentProcess) then
Exit; Exit;
end;
if not FileExists(FExecutableFilename) then
begin
DebugLn(DBG_WARNINGS, 'File %s does not exist.',[FExecutableFilename]);
Exit;
end;
// Get exe info, load classes
CheckExecutableAndLoadClasses;
if not Assigned(OsDbgClasses) then
begin
result := false;
DebugLn(DBG_WARNINGS, 'Error - No support registered for debug target');
Exit;
end;
Flags := []; Flags := [];
if RedirectConsoleOutput then Include(Flags, siRediretOutput); if RedirectConsoleOutput then Include(Flags, siRediretOutput);
if ForceNewConsoleWin then Include(Flags, siForceNewConsole); if ForceNewConsoleWin then Include(Flags, siForceNewConsole);
FCurrentProcess := OSDbgClasses.DbgProcessClass.Create(FExecutableFilename, OsDbgClasses, MemManager, MemModel, ProcessConfig);
if not Assigned(FCurrentProcess) then
begin
Result := false;
DebugLn(DBG_WARNINGS, 'Error - could not create TDbgProcess');
Exit;
end;
if AttachToPid <> 0 then if AttachToPid <> 0 then
Result := FCurrentProcess.AttachToInstance(AttachToPid, FLastError) Result := FCurrentProcess.AttachToInstance(AttachToPid, FLastError)
@ -2107,6 +2098,36 @@ begin
Result := FProcessMap.GetData(AProcessIdentifier, AProcess) and (AProcess <> nil); Result := FProcessMap.GetData(AProcessIdentifier, AProcess) and (AProcess <> nil);
end; end;
function TDbgController.CreateDbgProcess: TDbgProcess;
begin
Result := nil;
assert(FMainProcess = nil, 'TDbgController.CreateDbgProcess: FMainProcess = nil');
if FExecutableFilename = '' then begin
DebugLn(DBG_WARNINGS, 'No filename given to execute.');
Exit;
end;
if not FileExists(FExecutableFilename) then begin
DebugLn(DBG_WARNINGS, 'File %s does not exist.',[FExecutableFilename]);
Exit;
end;
// Get exe info, load classes
CheckExecutableAndLoadClasses;
if not Assigned(OsDbgClasses) then begin
DebugLn(DBG_WARNINGS, 'Error - No support registered for debug target');
Exit;
end;
Result := OSDbgClasses.DbgProcessClass.Create(
FExecutableFilename, OsDbgClasses, MemManager, MemModel, ProcessConfig);
if not Assigned(Result) then begin
DebugLn(DBG_WARNINGS, 'Error - could not create TDbgProcess');
Exit;
end;
end;
constructor TDbgController.Create(AMemManager: TFpDbgMemManager; constructor TDbgController.Create(AMemManager: TFpDbgMemManager;
AMemModel: TFpDbgMemModel); AMemModel: TFpDbgMemModel);
begin begin