Detect socket closed or other error and set status to SIGHUP. Do not access FConnection methods that read or write to socket when status = SIGHUP.

Patch/Contributed by ccrause

git-svn-id: trunk@62758 -
This commit is contained in:
martin 2020-03-12 14:21:18 +00:00
parent 62facbb44d
commit 93ee5eb352
2 changed files with 23 additions and 23 deletions

View File

@ -228,7 +228,7 @@ end;
function TDbgAvrThread.ReadDebugReg(ind: byte; out AVal: PtrUInt): boolean;
begin
if TDbgAvrProcess(Process).FIsTerminating then
if TDbgAvrProcess(Process).FIsTerminating or (TDbgAvrProcess(Process).FStatus = SIGHUP) then
begin
DebugLn(DBG_WARNINGS, 'TDbgRspThread.GetDebugReg called while FIsTerminating is set.');
Result := false;
@ -252,7 +252,7 @@ end;
function TDbgAvrThread.WriteDebugReg(ind: byte; AVal: PtrUInt): boolean;
begin
if TDbgAvrProcess(Process).FIsTerminating then
if TDbgAvrProcess(Process).FIsTerminating or (TDbgAvrProcess(Process).FStatus = SIGHUP) then
begin
DebugLn(DBG_WARNINGS, 'TDbgRspThread.WriteDebugReg called while FIsTerminating is set.');
Result := false;
@ -296,7 +296,7 @@ begin
DebugLn(DBG_WARNINGS, 'TDbgRspThread.RequestInternalPause called while FIsTerminating is set.');
Result := False;
if FInternalPauseRequested or FIsPaused then
if FInternalPauseRequested or FIsPaused or (TDbgAvrProcess(Process).FStatus = SIGHUP) then
exit;
DebugLn(DBG_VERBOSE, 'TDbgRspThread.RequestInternalPause requesting Ctrl-C.');
@ -404,7 +404,7 @@ var
i: integer;
regs: TBytes;
begin
if TDbgAvrProcess(Process).FIsTerminating then
if TDbgAvrProcess(Process).FIsTerminating or (TDbgAvrProcess(Process).FStatus = SIGHUP) then
begin
DebugLn(DBG_WARNINGS, 'TDbgRspThread.LoadRegisterValues called while FIsTerminating is set.');
exit;
@ -583,7 +583,7 @@ end;
function TDbgAvrProcess.ReadData(const AAdress: TDbgPtr;
const ASize: Cardinal; out AData): Boolean;
begin
if FIsTerminating then
if FIsTerminating or (TDbgAvrProcess(Process).FStatus = SIGHUP) then
begin
DebugLn(DBG_WARNINGS, 'TDbgRspProcess.ReadData called while FIsTerminating is set.');
Result := false;
@ -597,7 +597,7 @@ end;
function TDbgAvrProcess.WriteData(const AAdress: TDbgPtr;
const ASize: Cardinal; const AData): Boolean;
begin
if FIsTerminating then
if FIsTerminating or (TDbgAvrProcess(Process).FStatus = SIGHUP) then
begin
DebugLn(DBG_WARNINGS, 'TDbgRspProcess.WriteData called while FIsTerminating is set.');
Result := false;
@ -610,7 +610,7 @@ end;
procedure TDbgAvrProcess.TerminateProcess;
begin
// Try to prevent access to the RSP socket after it has been closed
if not FIsTerminating then
if not (FIsTerminating or (TDbgAvrProcess(Process).FStatus = SIGHUP)) then
begin
DebugLn(DBG_VERBOSE, 'Removing all break points');
RemoveAllBreakPoints;
@ -622,7 +622,7 @@ end;
function TDbgAvrProcess.Pause: boolean;
begin
if FIsTerminating then
if FIsTerminating or (TDbgAvrProcess(Process).FStatus = SIGHUP) then
begin
DebugLn(DBG_WARNINGS, 'TDbgRspProcess.Pause called while FIsTerminating is set.');
Result := false;
@ -660,7 +660,7 @@ var
initRegs: TInitializedRegisters;
begin
// Terminating process and all threads
if FIsTerminating then
if FIsTerminating or (FStatus = SIGHUP) then
begin
AThread.BeforeContinue;
TDbgAvrThread(AThread).InvalidateRegisters;
@ -791,7 +791,7 @@ end;
function TDbgAvrProcess.InsertBreakInstructionCode(const ALocation: TDBGPtr;
out OrigValue: Byte): Boolean;
begin
if FIsTerminating then
if FIsTerminating or (FStatus = SIGHUP) then
DebugLn(DBG_WARNINGS, 'TDbgRspProcess.InsertBreakInstruction called while FIsTerminating is set.');
result := ReadData(ALocation, SizeOf(OrigValue), OrigValue);
@ -809,7 +809,7 @@ end;
function TDbgAvrProcess.RemoveBreakInstructionCode(const ALocation: TDBGPtr;
const OrigValue: Byte): Boolean;
begin
if FIsTerminating then
if FIsTerminating or (FStatus = SIGHUP) then
begin
DebugLn(DBG_WARNINGS, 'TDbgRspProcess.RemoveBreakInstructionCode called while FIsTerminating is set');
result := false;

View File

@ -122,7 +122,7 @@ implementation
uses
LazLoggerBase, StrUtils,
{$IFNDEF WINDOWS}BaseUnix;
{$IFNDEF WINDOWS}BaseUnix, termio;
{$ELSE}winsock2, windows;
{$ENDIF}
@ -157,26 +157,25 @@ function TRspConnection.FWaitForData({timeout: integer}): boolean;
{$if defined(unix) or defined(windows)}
var
FDS: TFDSet;
//TimeV: TTimeVal;
r: integer;
{$endif}
begin
Result:=False;
//{$if defined(unix) or defined(windows)}
// TimeV.tv_usec := timeout * 1000; // 1 msec
// TimeV.tv_sec := 0;
//{$endif}
{$ifdef unix}
{$if defined(unix)}
FDS := Default(TFDSet);
fpFD_Zero(FDS);
fpFD_Set(self.Handle, FDS);
Result := fpSelect(self.Handle + 1, @FDS, nil, nil, nil{@TimeV}) > 0;
{$else}
{$ifdef windows}
fpSelect(self.Handle + 1, @FDS, nil, nil, nil);
// FDS is set even if the socket has been closed.
// Read available data and if 0 data is available then socket must be closed/ or error
r := 0;
FpIOCtl(self.Handle, FIONREAD, @r);
Result := r > 0;
{$elseif defined(windows)}
FDS := Default(TFDSet);
FD_Zero(FDS);
FD_Set(self.Handle, FDS);
Result := Select(self.Handle + 1, @FDS, nil, nil, nil{@TimeV}) > 0;
{$endif}
Result := Select(self.Handle + 1, @FDS, nil, nil, nil) > SOCKET_ERROR;
{$endif}
end;
@ -439,6 +438,7 @@ begin
if not FWaitForData() then
begin
msg := '';
result := SIGHUP;
exit;
end;