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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
|
unit FpDebugValueConvertors;
{$mode objfpc}{$H+}
{$ModeSwitch typehelpers}
interface
uses
Classes, SysUtils, FpDbgInfo, FpdMemoryTools, FpDbgCallContextInfo,
FpPascalBuilder, FpErrorMessages, FpDbgClasses, FpDbgUtil, DbgIntfBaseTypes,
LazClasses, Forms, StdCtrls, Controls, StrUtils, FpDebugDebuggerBase,
FpDebugStringConstants, LazDebuggerValueConverter, LazDebuggerIntfBaseTypes,
LazDebuggerIntf;
type
(* TFpDbgValueConverter and descendants
- A TFpDbgValueConverter should be immutable, once in the list.
To change settings a new instance can be set to TDbgBackendConverterConfig
This allows for TFpDbgValueConverter to be used outside the lock (reduces lock time)
- Any setting that the IDE may need to store, should be published
*)
TFpDbgValueConverter = class(TRefCountedObject, ILazDbgValueConverterIntf)
private
FLastErrror: TFpError;
protected
function GetObject: TObject;
function GetSettingsFrame: ILazDbgValueConverterSettingsFrameIntf; virtual;
procedure Init; virtual;
public
class function GetName: String; virtual; abstract;
function GetRegistryEntry: TLazDbgValueConvertRegistryEntryClass; virtual;
constructor Create; virtual;
procedure Assign(ASource: TFpDbgValueConverter); virtual;
function CreateCopy: ILazDbgValueConverterIntf; virtual;
function NeedConversionLimit: Boolean; virtual;
(* CanHandleValue must return the SAME RESULT if called repeatedly (e.g. in an array)
CanHandleValue must NOT depend on DATA, or anything that can change
CanHandleValue must ONLY check type info (or other data that will not change)
- For any other/uncertain case the converter should create an error (empty text) in
ConvertValue, changing AnResData
*)
function CanHandleValue(ASourceValue: TFpValue; AnFpDebugger: TFpDebugDebuggerBase): Boolean; virtual;
(* ConvertValue
* AnResData = nil
=> Do nothing => ConvertValue must have done AnResData.Create....
* Result <> nil / AnResData <> nil
=> Use Result (instead of ASourceValue) to build watch-data
=> use the new AnResData (in case it changed)
If the new AnResData = nil, then do nothing
* Result = nil
- AnResData NOT-changed
=> Create an error (IDE will show ASourceValue)
- AnResData CHANGED
=> Do nothing => ConvertValue must have done AnResData.Create....
*)
function ConvertValue(ASourceValue: TFpValue;
AnFpDebugger: TFpDebugDebuggerBase;
AnExpressionScope: TFpDbgSymbolScope;
var AnResData: IDbgWatchDataIntf // if changed, then the converter has done its job, and should return nil
): TFpValue; virtual; abstract;
procedure SetError(AnError: TFpError);
property LastErrror: TFpError read FLastErrror;
end;
TFpDbgValueConverterClass = class of TFpDbgValueConverter;
{ TConverterSettingsFrameBase }
TConverterSettingsFrameBase = class(TFrame, ILazDbgValueConverterSettingsFrameIntf)
protected
function GetFrame: TObject; virtual;
public
procedure ReadFrom(AConvertor: ILazDbgValueConverterIntf); virtual;
function WriteTo(AConvertor: ILazDbgValueConverterIntf): Boolean; virtual;
end;
{ TConverterWithFuncCallSettingsFrame }
TConverterWithFuncCallSettingsFrame = class(TConverterSettingsFrameBase)
chkRunAll: TCheckBox;
public
constructor Create(TheOwner: TComponent); override;
procedure ReadFrom(AConvertor: ILazDbgValueConverterIntf); override;
function WriteTo(AConvertor: ILazDbgValueConverterIntf): Boolean; override;
end;
{ TFpDbgValueConverterWithFuncCall }
TFpDbgValueConverterWithFuncCall = class(TFpDbgValueConverter)
private
FFuncCallRunAllThreads: Boolean;
public
procedure Assign(ASource: TFpDbgValueConverter); override;
published
property FuncCallRunAllThreads: Boolean read FFuncCallRunAllThreads write FFuncCallRunAllThreads;
end;
{ TFpDbgValueConvertSelectorIntfHelper }
TFpDbgValueConvertSelectorIntfHelper = type helper for ILazDbgValueConvertSelectorIntf
function CheckMatch(AValue: TFpValue; IgnoreInstanceClass: boolean = False): Boolean;
function CheckTypeMatch(AValue: TFpValue; IgnoreInstanceClass: boolean = False): Boolean;
end;
{ TFpDbgValueConverterRegistryEntry }
TFpDbgValueConverterRegistryEntry = class(TLazDbgValueConvertRegistryEntry)
public
class function CreateValueConvertorIntf: ILazDbgValueConverterIntf; override;
class function GetName: String; override;
class function GetDebuggerClass: TClass; override;
end;
(**** Call SysVarToLStr on variant ****)
{ TFpDbgValueConverterVariantToLStr }
TFpDbgValueConverterVariantToLStr = class(TFpDbgValueConverterWithFuncCall)
private
function GetProcAddrFromMgr(AnFpDebugger: TFpDebugDebuggerBase; AnExpressionScope: TFpDbgSymbolScope): TDbgPtr;
protected
function GetSettingsFrame: ILazDbgValueConverterSettingsFrameIntf; override;
public
class function GetName: String; override;
function GetRegistryEntry: TLazDbgValueConvertRegistryEntryClass; override;
function ConvertValue(ASourceValue: TFpValue;
AnFpDebugger: TFpDebugDebuggerBase;
AnExpressionScope: TFpDbgSymbolScope;
var AnResData: IDbgWatchDataIntf
): TFpValue; override;
end;
{ TFpDbgValueConverterVariantToLStrRegistryEntry }
TFpDbgValueConverterVariantToLStrRegistryEntry = class(TFpDbgValueConverterRegistryEntry)
public
class function GetConvertorClass: TClass; override;
end;
implementation
{$R *.lfm}
{ TFpDbgValueConverterWithFuncCall }
procedure TFpDbgValueConverterWithFuncCall.Assign(ASource: TFpDbgValueConverter);
begin
inherited Assign(ASource);
if ASource is TFpDbgValueConverterWithFuncCall then begin
FFuncCallRunAllThreads := TFpDbgValueConverterWithFuncCall(ASource).FFuncCallRunAllThreads;
end;
end;
{ TFpDbgValueConverter }
function TFpDbgValueConverter.CreateCopy: ILazDbgValueConverterIntf;
var
c: TFpDbgValueConverter;
begin
c := TFpDbgValueConverterClass(ClassType).Create;
c.Assign(Self);
Result := c;
end;
function TFpDbgValueConverter.NeedConversionLimit: Boolean;
begin
Result := True;
end;
function TFpDbgValueConverter.CanHandleValue(ASourceValue: TFpValue;
AnFpDebugger: TFpDebugDebuggerBase): Boolean;
begin
Result := True;
end;
procedure TFpDbgValueConverter.SetError(AnError: TFpError);
begin
FLastErrror := AnError;
end;
function TFpDbgValueConverter.GetObject: TObject;
begin
Result := Self;
end;
function TFpDbgValueConverter.GetSettingsFrame: ILazDbgValueConverterSettingsFrameIntf;
begin
Result := nil;
end;
procedure TFpDbgValueConverter.Init;
begin
//
end;
function TFpDbgValueConverter.GetRegistryEntry: TLazDbgValueConvertRegistryEntryClass;
begin
Result := nil;
end;
constructor TFpDbgValueConverter.Create;
begin
inherited Create;
Init;
end;
procedure TFpDbgValueConverter.Assign(ASource: TFpDbgValueConverter);
begin
//
end;
{ TConverterSettingsFrameBase }
function TConverterSettingsFrameBase.GetFrame: TObject;
begin
Result := Self;
end;
procedure TConverterSettingsFrameBase.ReadFrom(
AConvertor: ILazDbgValueConverterIntf);
begin
//
end;
function TConverterSettingsFrameBase.WriteTo(
AConvertor: ILazDbgValueConverterIntf): Boolean;
begin
Result := False; // nothing changed
end;
{ TConverterWithFuncCallSettingsFrame }
procedure TConverterWithFuncCallSettingsFrame.ReadFrom(
AConvertor: ILazDbgValueConverterIntf);
var
c: TFpDbgValueConverterWithFuncCall;
begin
if not (AConvertor.GetObject is TFpDbgValueConverterWithFuncCall) then
exit;
c := TFpDbgValueConverterWithFuncCall(AConvertor.GetObject);
chkRunAll.Checked := c.FuncCallRunAllThreads;
end;
function TConverterWithFuncCallSettingsFrame.WriteTo(
AConvertor: ILazDbgValueConverterIntf): Boolean;
var
c: TFpDbgValueConverterWithFuncCall;
begin
Result := False;
if not (AConvertor.GetObject is TFpDbgValueConverterWithFuncCall) then
exit;
c := TFpDbgValueConverterWithFuncCall(AConvertor.GetObject);
Result := chkRunAll.Checked <> c.FuncCallRunAllThreads;
c.FuncCallRunAllThreads := chkRunAll.Checked;
end;
constructor TConverterWithFuncCallSettingsFrame.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
chkRunAll.Caption := drsRunAllThreadsWhileEval;
end;
{ TFpDbgValueConvertSelectorIntfHelper }
function TFpDbgValueConvertSelectorIntfHelper.CheckMatch(AValue: TFpValue;
IgnoreInstanceClass: boolean): Boolean;
begin
Result := //(AValue.Kind in (MatchKinds * GetConverter.GetSupportedKinds)) and
CheckTypeMatch(AValue, IgnoreInstanceClass);
end;
function TFpDbgValueConvertSelectorIntfHelper.CheckTypeMatch(AValue: TFpValue;
IgnoreInstanceClass: boolean): Boolean;
function MatchPattern(const AName, APattern: String): Boolean;
var
NamePos, PatternPos, p: Integer;
begin
Result := False;
if APattern = '' then
exit;
NamePos := 1;
PatternPos := 1;
while PatternPos <= Length(APattern) do begin
if APattern[PatternPos] = '*' then begin
inc(PatternPos);
end
else begin
p := PatternPos;
PatternPos := PosEx('*', APattern, p);
if PatternPos < 1 then
PatternPos := Length(APattern)+1;
if PatternPos-p > Length(AName)+1 - NamePos then
break;
NamePos := PosEx(Copy(APattern, p, PatternPos-p), AName, NamePos);
if (NamePos < 1) or
( (p = 1) and (NamePos <> 1) ) // APattern does not start with *
then
break;
inc(NamePos, PatternPos-p);
end;
end;
Result := (PatternPos = Length(APattern)+1) and
( (NamePos = Length(AName)+1) or
( (APattern[Length(APattern)] = '*') and
(NamePos <= Length(AName)+1)
)
);
end;
var
i, CnIdx: Integer;
TpName, Pattern, ValClassName, ValUnitName: String;
t: TFpSymbol;
HasMaybeUnitDot: Boolean;
MatchTypeNames: TStrings;
begin
t := AValue.TypeInfo;
Result := (t <> nil) and GetTypeName(TpName, t, [tnfNoSubstitute]);
if not Result then
exit;
TpName := LowerCase(TpName);
MatchTypeNames := AllowedTypeNames;
i := MatchTypeNames.Count;
while i > 0 do begin
dec(i);
Pattern := LowerCase(trim(MatchTypeNames[i]));
HasMaybeUnitDot := (pos('.', Pattern) > 1) and
(AValue.Kind in [skClass]); // only class supports unitnames (via rtti)
if AnsiStrLIComp('is:', @Pattern[1], 3) = 0 then begin
Delete(Pattern, 1, 3);
Pattern := trim(Pattern);
if (AValue.Kind in [skRecord, skClass, skObject, skInterface]) then begin
ValClassName := TpName;
while t <> nil do begin
Result := MatchPattern(ValClassName, Pattern);
if Result then
exit;
t := t.TypeInfo;
if (t = nil) or not GetTypeName(ValClassName, t, [tnfNoSubstitute]) then
break;
ValClassName := LowerCase(ValClassName);
end;
if not IgnoreInstanceClass then begin
CnIdx := 0;
while AValue.GetInstanceClassName(@ValClassName, @ValUnitName, CnIdx) and
(ValClassName <> '')
do begin
ValClassName := LowerCase(ValClassName);
if (ValClassName = TpName) and (not HasMaybeUnitDot) then
Break;
Result := MatchPattern(ValClassName, Pattern);
if Result then
exit;
if HasMaybeUnitDot and (ValUnitName <> '') then begin
ValUnitName := LowerCase(ValUnitName);
Result := MatchPattern(ValUnitName+'.'+ValClassName, Pattern);
if Result then
exit;
end;
inc(CnIdx);
end;
end;
AValue.ResetError;
Continue;
end;
end;
Result := MatchPattern(TpName, Pattern);
if Result then
exit;
if HasMaybeUnitDot then begin
if AValue.GetInstanceClassName(@ValClassName, @ValUnitName) and
(ValUnitName <> '') and (ValClassName <> '')
then begin
Result := MatchPattern(ValUnitName+'.'+ValClassName, Pattern);
if Result then
exit;
end;
AValue.ResetError;
end;
end;
end;
{ TFpDbgValueConverterRegistryEntry }
class function TFpDbgValueConverterRegistryEntry.CreateValueConvertorIntf: ILazDbgValueConverterIntf;
begin
Result := TFpDbgValueConverterClass(GetConvertorClass).Create;
end;
class function TFpDbgValueConverterRegistryEntry.GetName: String;
begin
Result := TFpDbgValueConverterClass(GetConvertorClass).GetName;
end;
class function TFpDbgValueConverterRegistryEntry.GetDebuggerClass: TClass;
begin
Result := TFpDebugDebuggerBase;
end;
{ TFpDbgValueConverterVariantToLStr }
function TFpDbgValueConverterVariantToLStr.GetProcAddrFromMgr(
AnFpDebugger: TFpDebugDebuggerBase; AnExpressionScope: TFpDbgSymbolScope
): TDbgPtr;
var
ProcSym: TFpSymbol;
MgrAddr, Fnd: TDBGPtr;
CallContext: TFpDbgInfoCallContext;
CurProc: TDbgProcess;
begin
Result := 0;
CurProc := AnFpDebugger.DbgController.CurrentProcess;
if CurProc = nil then
exit;
ProcSym := nil;
ProcSym := CurProc.FindProcSymbol('SYSTEM_$$_GETVARIANTMANAGER$TVARIANTMANAGER');
try
if (ProcSym = nil) or (not (ProcSym.Kind = skProcedure)) or
(not IsTargetAddr(ProcSym.Address))
then
exit;
CallContext := nil;
MgrAddr := AnFpDebugger.DbgController.CurrentThread.AllocStackMem(1024); // enough space for the record
CallContext := AnFpDebugger.DbgController.Call(ProcSym.Address, AnExpressionScope.LocationContext,
AnFpDebugger.MemReader, AnFpDebugger.MemConverter);
if CallContext = nil then
exit;
try
CallContext.AddOrdinalParam(nil, MgrAddr);
CallContext.FinalizeParams;
AnFpDebugger.BeforeWatchEval(CallContext);
AnFpDebugger.RunProcessLoop(True);
if not CallContext.IsValid then
exit;
if CurProc.ReadAddress(MgrAddr + 8 * CurProc.PointerSize, Fnd) then
Result := Fnd;
finally
AnFpDebugger.DbgController.AbortCurrentCommand(True);
CallContext.ReleaseReference;
AnFpDebugger.DbgController.CurrentThread.RestoreStackMem;
end;
finally
ProcSym.ReleaseReference;
end;
end;
function TFpDbgValueConverterVariantToLStr.GetSettingsFrame: ILazDbgValueConverterSettingsFrameIntf;
begin
Result := TConverterWithFuncCallSettingsFrame.Create(nil);
end;
class function TFpDbgValueConverterVariantToLStr.GetName: String;
begin
Result := drsCallSysVarToLStr;
end;
function TFpDbgValueConverterVariantToLStr.GetRegistryEntry: TLazDbgValueConvertRegistryEntryClass;
begin
Result := TFpDbgValueConverterVariantToLStrRegistryEntry;
end;
function TFpDbgValueConverterVariantToLStr.ConvertValue(ASourceValue: TFpValue;
AnFpDebugger: TFpDebugDebuggerBase; AnExpressionScope: TFpDbgSymbolScope;
var AnResData: IDbgWatchDataIntf): TFpValue;
var
NewResult, ProcVal, m: TFpValue;
ProcSym: TFpSymbol;
CallContext: TFpDbgInfoCallContext;
StringAddr, ProcAddr, StringDecRefAddress: TDbgPtr;
ProcLoc: TFpDbgMemLocation;
r: Boolean;
begin
Result := nil;
if (ASourceValue.Kind <> skRecord) or
(AnFpDebugger.DbgController.CurrentProcess = nil) or
( (AnFpDebugger.DbgController.CurrentProcess.Mode = dm32) and
(ASourceValue.DataSize.Size <> 16)
) or
( (AnFpDebugger.DbgController.CurrentProcess.Mode = dm64) and
(ASourceValue.DataSize.Size <> 24)
)
then begin
SetError(CreateError(fpErrAnyError, ['Value not a variant']));
exit;
end;
m := ASourceValue.MemberByName['vtype'];
r := (m = nil) or (SizeToFullBytes(m.DataSize) <> 2);
m.ReleaseReference;
if r then begin
SetError(CreateError(fpErrAnyError, ['Value not a variant']));
exit;
end;
ProcVal := nil;
ProcSym := nil;
try
(*
//VARIANTS_$$_SYSVARTOLSTR$ANSISTRING$VARIANT
//U_$SYSTEM_$$_VARIANTMANAGER
//SYSTEM_$$_GETVARIANTMANAGER$TVARIANTMANAGER
ProcVal := AnExpressionScope.FindSymbol('sysvartolstr', 'variants', [fsfIgnoreEnumVals]);
if ProcVal <> nil then begin
ProcSym := ProcVal.DbgSymbol;
if ProcSym <> nil then
ProcSym.AddReference;
end;
if (ProcSym = nil) or (not (ProcSym.Kind = skProcedure)) or
(not IsTargetAddr(ProcSym.Address))
then
ProcSym := AnFpDebugger.DbgController.CurrentProcess.FindProcSymbol('sysvartolstr');
if (ProcSym = nil) or (not IsTargetAddr(ProcSym.Address)) then
exit;
ProcLoc := ProcSym.Address
*)
if not IsTargetAddr(ASourceValue.Address) then begin
SetError(CreateError(fpErrAnyError, ['Value not in memory']));
exit;
end;
ProcAddr := AnFpDebugger.GetCachedData(pointer(TFpDbgValueConverterVariantToLStr));
if ProcAddr = 0 then begin
ProcAddr := GetProcAddrFromMgr(AnFpDebugger, AnExpressionScope);
if ProcAddr = 0 then begin
SetError(CreateError(fpErrAnyError, ['SysVarToLStr not found']));
exit;
end;
AnFpDebugger.SetCachedData(pointer(TFpDbgValueConverterVariantToLStr), ProcAddr);
end;
ProcLoc := TargetLoc(ProcAddr);
StringDecRefAddress := AnFpDebugger.GetCached_FPC_ANSISTR_DECR_REF;
if (StringDecRefAddress = 0) then begin
SetError(CreateError(fpErrAnyError, ['STRING_DEC_REF not found']));
exit;
end;
StringAddr := 0;
CallContext := AnFpDebugger.DbgController.Call(ProcLoc, AnExpressionScope.LocationContext,
AnFpDebugger.MemReader, AnFpDebugger.MemConverter);
if CallContext = nil then begin
SetError(CreateError(fpErrAnyError, ['function call not possible']));
exit;
end;
try
CallContext.AddStringResult;
CallContext.FinalizeParams; // force the string as first param (32bit) // TODO
CallContext.AddOrdinalParam(nil, ASourceValue.DataAddress.Address);
AnFpDebugger.BeforeWatchEval(CallContext);
AnFpDebugger.RunProcessLoop(not FuncCallRunAllThreads);
if not CallContext.IsValid then begin
if (IsError(CallContext.LastError)) then
SetError(CallContext.LastError)
else
if (CallContext.Message <> '') then
SetError(CreateError(fpErrAnyError, [CallContext.Message]));
exit;
end;
if not CallContext.GetStringResultAsPointer(StringAddr) then begin
SetError(CallContext.LastError);
exit;
end;
if not CallContext.GetStringResult(NewResult) then begin
SetError(CallContext.LastError);
exit;
end;
Result := NewResult;
finally
AnFpDebugger.DbgController.AbortCurrentCommand(True);
CallContext.ReleaseReference;
AnFpDebugger.CallTargetFuncStringDecRef(StringDecRefAddress, StringAddr, AnExpressionScope.LocationContext);
end;
finally
ProcVal.ReleaseReference;
ProcSym.ReleaseReference;
end;
end;
{ TFpDbgValueConverterVariantToLStrRegistryEntry }
class function TFpDbgValueConverterVariantToLStrRegistryEntry.GetConvertorClass: TClass;
begin
Result := TFpDbgValueConverterVariantToLStr;
end;
initialization
ValueConverterRegistry.Add(TFpDbgValueConverterVariantToLStrRegistryEntry);
end.
|