1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
unit FpDebugDebuggerBase;
{$mode objfpc}{$H+}
{$IF FPC_Fullversion=30202}{$Optimization NOPEEPHOLE}{$ENDIF}
interface
uses
Classes, SysUtils, fgl, Math, FPDbgController, FpdMemoryTools, FpDbgClasses,
FpDbgUtil, FpDbgInfo, FpDbgCallContextInfo, DbgIntfDebuggerBase,
DbgIntfBaseTypes, {$ifdef FORCE_LAZLOGGER_DUMMY} LazLoggerDummy {$else} LazLoggerBase {$endif}, FpDebugDebuggerUtils,
LazDebuggerIntfBaseTypes;
type
{ TFpDebugDebuggerBase }
TFpDebugDebuggerBase = class(TDebuggerIntf)
private type
TCachedDbgPtrMap = specialize TFPGMap<Pointer, TDbgPtr>;
protected
FDbgController: TDbgController;
FMemManager: TFpDbgMemManager;
FMemModel: TFpDbgMemModel;
FMemReader: TDbgMemReader;
FMemConverter: TFpDbgMemConvertor;
FLockList: TFpDbgLockList;
FWorkQueue: TFpThreadPriorityWorkerQueue;
FCached_FPC_ANSISTR_DECR_REF: TDbgPtr;
FCached_FPC_WIDESTR_DECR_REF: TDbgPtr;
FCached_FPC_ANSISTR_SETLENGTH: TDbgPtr;
FCached_FPC_WIDESTR_SETLENGTH: TDbgPtr;
FCached_Data: TCachedDbgPtrMap;
function GetCached_FPC_Func_Addr(var ACacheVar: TDbgPtr; const AName: String): TDbgPtr;
public
destructor Destroy; override;
// All caches must only be accessed it the debug-thread
function GetCached_FPC_ANSISTR_DECR_REF: TDBGPtr; inline;
function GetCached_FPC_WIDESTR_DECR_REF: TDBGPtr; inline;
function GetCached_FPC_ANSISTR_SETLENGTH: TDBGPtr; inline;
function GetCached_FPC_WIDESTR_SETLENGTH: TDBGPtr; inline;
procedure SetCachedData(AKey: Pointer; AValue: TDBGPtr);
function GetCachedData(AKey: Pointer): TDBGPtr;
procedure ClearCachedData;
procedure CallTargetFuncStringDecRef(const AProcAddr: TDbgPtr;
const AStringDataAddr: TDBGPtr;
const ABaseContext: TFpDbgLocationContext; AMemReader: TFpDbgMemReaderBase = nil; AMemConverter: TFpDbgMemConvertor = nil);
function CallTargetFuncStringSetLength(const ASetLengthProcAddr: TDbgPtr;
out AStringDataAddr: TDBGPtr;
const AStringLen: cardinal;
const ABaseContext: TFpDbgLocationContext; AMemReader: TFpDbgMemReaderBase = nil; AMemConverter: TFpDbgMemConvertor = nil
): Boolean;
function CreateAnsiStringInTarget(const ASetLengthProcAddr: TDbgPtr;
out AStringDataAddr: TDBGPtr;
const AStringContent: String;
const ABaseContext: TFpDbgLocationContext; AMemReader: TFpDbgMemReaderBase = nil; AMemConverter: TFpDbgMemConvertor = nil
): Boolean;
function CreateWideStringInTarget(const ASetLengthProcAddr: TDbgPtr;
out AStringDataAddr: TDBGPtr;
const AStringContent: WideString;
const ABaseContext: TFpDbgLocationContext; AMemReader: TFpDbgMemReaderBase = nil; AMemConverter: TFpDbgMemConvertor = nil
): Boolean;
function ReadAnsiStringFromTarget(AStringAddr: TDBGPtr; out AString: String): boolean;
procedure BeforeWatchEval(ACallContext: TFpDbgInfoCallContext); virtual; abstract;
procedure RunProcessLoop(OnlyCurrentThread: Boolean); virtual; abstract;
property DbgController: TDbgController read FDbgController;
property MemManager: TFpDbgMemManager read FMemManager;
property MemReader: TDbgMemReader read FMemReader;
property MemConverter: TFpDbgMemConvertor read FMemConverter;
property LockList: TFpDbgLockList read FLockList;
property WorkQueue: TFpThreadPriorityWorkerQueue read FWorkQueue;
end;
implementation
{ TFpDebugDebuggerBase }
function TFpDebugDebuggerBase.GetCached_FPC_Func_Addr(var ACacheVar: TDbgPtr;
const AName: String): TDbgPtr;
var
FunctSymbol: TFpSymbol;
begin
Result := ACacheVar;
if Result <> 0 then
exit;
FunctSymbol := DbgController.CurrentProcess.FindProcSymbol(AName);
if (FunctSymbol <> nil) and (IsTargetNotNil(FunctSymbol.Address)) then
ACacheVar := FunctSymbol.Address.Address;
FunctSymbol.ReleaseReference;
Result := ACacheVar;
end;
destructor TFpDebugDebuggerBase.Destroy;
begin
inherited Destroy;
FreeAndNil(FCached_Data);
end;
function TFpDebugDebuggerBase.GetCached_FPC_ANSISTR_DECR_REF: TDBGPtr;
begin
Result := GetCached_FPC_Func_Addr(FCached_FPC_ANSISTR_DECR_REF, 'FPC_ANSISTR_DECR_REF');
end;
function TFpDebugDebuggerBase.GetCached_FPC_WIDESTR_DECR_REF: TDBGPtr;
begin
Result := GetCached_FPC_Func_Addr(FCached_FPC_WIDESTR_DECR_REF, 'FPC_WIDESTR_DECR_REF');
end;
function TFpDebugDebuggerBase.GetCached_FPC_ANSISTR_SETLENGTH: TDBGPtr;
begin
Result := GetCached_FPC_Func_Addr(FCached_FPC_ANSISTR_SETLENGTH, 'FPC_ANSISTR_SETLENGTH');
end;
function TFpDebugDebuggerBase.GetCached_FPC_WIDESTR_SETLENGTH: TDBGPtr;
begin
Result := GetCached_FPC_Func_Addr(FCached_FPC_WIDESTR_SETLENGTH, 'FPC_WIDESTR_SETLENGTH');
end;
procedure TFpDebugDebuggerBase.SetCachedData(AKey: Pointer; AValue: TDBGPtr);
begin
if FCached_Data = nil then
FCached_Data := TCachedDbgPtrMap.Create;
FCached_Data[AKey] := AValue;
end;
function TFpDebugDebuggerBase.GetCachedData(AKey: Pointer): TDBGPtr;
var
i: Integer;
begin
Result := 0;
if FCached_Data <> nil then begin
i := FCached_Data.IndexOf(AKey);
if i >= 0 then
Result := FCached_Data.Data[i];
end;
end;
procedure TFpDebugDebuggerBase.ClearCachedData;
begin
FCached_FPC_ANSISTR_DECR_REF := 0;
FCached_FPC_WIDESTR_DECR_REF := 0;
FCached_FPC_ANSISTR_SETLENGTH := 0;
if FCached_Data <> nil then
FCached_Data.Clear;
end;
procedure TFpDebugDebuggerBase.CallTargetFuncStringDecRef(
const AProcAddr: TDbgPtr; const AStringDataAddr: TDBGPtr;
const ABaseContext: TFpDbgLocationContext; AMemReader: TFpDbgMemReaderBase;
AMemConverter: TFpDbgMemConvertor);
var
CallContext: TFpDbgInfoCallContext;
begin
if AStringDataAddr = 0 then
exit;
if AMemReader = nil then
AMemReader := MemReader;
if AMemConverter = nil then
AMemConverter := MemConverter;
CallContext := DbgController.Call(TargetLoc(AProcAddr), ABaseContext, AMemReader, AMemConverter);
if CallContext = nil then
exit;
try
CallContext.AddOrdinalViaRefAsParam(AStringDataAddr);
CallContext.FinalizeParams;
BeforeWatchEval(CallContext);
RunProcessLoop(True);
finally
DbgController.AbortCurrentCommand(True);
CallContext.ReleaseReference;
end;
end;
function TFpDebugDebuggerBase.CallTargetFuncStringSetLength(
const ASetLengthProcAddr: TDbgPtr; out AStringDataAddr: TDBGPtr;
const AStringLen: cardinal; const ABaseContext: TFpDbgLocationContext;
AMemReader: TFpDbgMemReaderBase; AMemConverter: TFpDbgMemConvertor): Boolean;
var
CallContext: TFpDbgInfoCallContext;
AParamAddr: TDBGPtr;
begin
if AMemReader = nil then
AMemReader := MemReader;
if AMemConverter = nil then
AMemConverter := MemConverter;
Result := False;
CallContext := DbgController.Call(TargetLoc(ASetLengthProcAddr), ABaseContext, AMemReader, AMemConverter);
if CallContext = nil then
exit;
try
if not CallContext.AddOrdinalViaRefAsParam(0, AParamAddr) then
exit;
if not CallContext.AddOrdinalParam(AStringLen) then
exit;
// Some versions may take a TCodePage // Lazarus defaults to utf-8 // CP_UTF8 = 65001;
// This value is ignored if not needed
if not CallContext.AddOrdinalParam(65001) then
exit;
CallContext.FinalizeParams;
BeforeWatchEval(CallContext);
RunProcessLoop(True);
if not CallContext.IsValid then
exit;
Result := DbgController.CurrentProcess.ReadAddress(AParamAddr, AStringDataAddr);
finally
DbgController.AbortCurrentCommand(True);
CallContext.ReleaseReference;
end;
end;
function TFpDebugDebuggerBase.CreateAnsiStringInTarget(
const ASetLengthProcAddr: TDbgPtr; out AStringDataAddr: TDBGPtr;
const AStringContent: String; const ABaseContext: TFpDbgLocationContext;
AMemReader: TFpDbgMemReaderBase; AMemConverter: TFpDbgMemConvertor): Boolean;
begin
if AStringContent = '' then begin
AStringDataAddr := 0;
Result := True;
exit;
end;
Result := CallTargetFuncStringSetLength(ASetLengthProcAddr, AStringDataAddr, length(AStringContent),
ABaseContext, AMemReader, AMemConverter);
if not Result then
exit;
Result := AStringDataAddr <> 0;
if not Result then
exit;
DbgController.CurrentProcess.WriteData(AStringDataAddr, Length(AStringContent), AStringContent[1]);
end;
function TFpDebugDebuggerBase.CreateWideStringInTarget(
const ASetLengthProcAddr: TDbgPtr; out AStringDataAddr: TDBGPtr;
const AStringContent: WideString; const ABaseContext: TFpDbgLocationContext;
AMemReader: TFpDbgMemReaderBase; AMemConverter: TFpDbgMemConvertor): Boolean;
begin
if AStringContent = '' then begin
AStringDataAddr := 0;
Result := True;
exit;
end;
Result := CallTargetFuncStringSetLength(ASetLengthProcAddr, AStringDataAddr, length(AStringContent),
ABaseContext, AMemReader, AMemConverter);
if not Result then
exit;
Result := AStringDataAddr <> 0;
if not Result then
exit;
DbgController.CurrentProcess.WriteData(AStringDataAddr, Length(AStringContent) * 2, AStringContent[1]);
end;
function TFpDebugDebuggerBase.ReadAnsiStringFromTarget(AStringAddr: TDBGPtr;
out AString: String): boolean;
var
CurProcess: TDbgProcess;
l: TDBGPtr;
r: Cardinal;
begin
AString := '';
Result := AStringAddr = 0;
if Result then
exit;
CurProcess := DbgController.CurrentProcess;
Result := CurProcess <> nil;
if not Result then
exit;
{$PUSH}{$Q-}{$R-}
Result := CurProcess.ReadAddress(AStringAddr - CurProcess.PointerSize, l);
if not Result then
exit;
l := Min(l, MemManager.MemLimits.MaxStringLen);
if (not MemManager.CheckDataSize(l)) then
exit;
SetLength(AString, l);
if l > 0 then begin
Result := CurProcess.ReadData(AStringAddr, l, AString[1], r);
SetLength(AString,r);
end;
{$POP}
end;
end.
|