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 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
|
unit DebugThreadCommand;
{$mode objfpc}{$H+}
{$ifndef VER2}
{$define disassemblernestedproc}
{$endif VER2}
{$ifdef disassemblernestedproc}
{$modeswitch nestedprocvars}
{$endif disassemblernestedproc}
interface
uses
Classes,
FPDbgController,
FpDbgClasses,
FpDbgUtil,
FpDbgInfo,
FpPascalParser,
FpPascalBuilder,
FpErrorMessages,
DbgIntfDebuggerBase,
DbgIntfBaseTypes,
strutils,
debugthread,
CustApp,
Maps,
SysUtils;
type
{ TFpDebugThreadCommandList }
TFpDebugThreadCommandList = class(TFPList)
public
class function instance: TFpDebugThreadCommandList;
function GetCommandByName(ATextName: string): TFpDebugThreadCommandClass;
end;
{ TFpDebugThreadQuitDebugServerCommand }
TFpDebugThreadQuitDebugServerCommand = class(TFpDebugThreadCommand)
public
function PreExecute(AController: TFpServerDbgController; out DoQueueCommand: boolean): boolean; override;
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
end;
{ TFpDebugThreadSetFilenameCommand }
TFpDebugThreadSetFilenameCommand = class(TFpDebugThreadCommand)
private
FFileName: string;
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
published
property Filename: string read FFileName write FFileName;
end;
{ TFpDebugThreadSetRedirectConsoleOutputCommand }
TFpDebugThreadSetConsoleTtyCommand = class(TFpDebugThreadCommand)
private
FConsoleTty: String;
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
published
property ConsoleTty: String read FConsoleTty write FConsoleTty;
end;
{ TFpDebugThreadRunCommand }
TFpDebugThreadRunCommand = class(TFpDebugThreadCommand)
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
end;
{ TFpDebugThreadContinueCommand }
TFpDebugThreadContinueCommand = class(TFpDebugThreadCommand)
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
end;
{ TFpDebugThreadNextCommand }
TFpDebugThreadNextCommand = class(TFpDebugThreadCommand)
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
end;
{ TFpDebugThreadStepCommand }
TFpDebugThreadStepCommand = class(TFpDebugThreadCommand)
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
end;
{ TFpDebugThreadStepOutCommand }
TFpDebugThreadStepOutCommand = class(TFpDebugThreadCommand)
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
end;
{ TFpDebugThreadStepIntoInstrCommand }
TFpDebugThreadStepIntoInstrCommand = class(TFpDebugThreadCommand)
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
end;
{ TFpDebugThreadStepOverInstrCommand }
TFpDebugThreadStepOverInstrCommand = class(TFpDebugThreadCommand)
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
end;
{ TFpDebugThreadStopCommand }
TFpDebugThreadStopCommand = class(TFpDebugThreadCommand)
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
end;
{ TFpDebugThreadAddBreakpointCommand }
TFpDebugThreadAddBreakpointCommand = class(TFpDebugThreadCommand)
private
FFileName: string;
FLine: integer;
FBreakPoint: TFpInternalBreakpoint;
FBreakServerId: Integer;
public
procedure ComposeSuccessEvent(var AnEvent: TFpDebugEvent); override;
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
published
property Filename: string read FFileName write FFileName;
property Line: integer read FLine write FLine;
end;
{ TFpDebugThreadRemoveBreakpointCommand }
TFpDebugThreadRemoveBreakpointCommand = class(TFpDebugThreadCommand)
private
FBreakpointServerIdr: Integer;
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
published
property BreakpointServerIdr: Integer read FBreakpointServerIdr write FBreakpointServerIdr;
end;
{ TFpDebugThreadGetLocationInfoCommand }
TFpDebugThreadGetLocationInfoCommand = class(TFpDebugThreadCommand)
private
FLocationRec: TDBGLocationRec;
FAddressValue: TDBGPtr;
function GetAddress: string;
procedure SetAddress(AValue: string);
protected
procedure ComposeSuccessEvent(var AnEvent: TFpDebugEvent); override;
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
published
property Address: string read GetAddress write SetAddress;
end;
{ TFpDebugThreadEvaluateCommand }
TFpDebugThreadEvaluateCommand = class(TFpDebugThreadCommand)
private
FExpression: string;
FResText: string;
FValidity: TDebuggerDataState;
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
procedure ComposeSuccessEvent(var AnEvent: TFpDebugEvent); override;
published
property Expression: string read FExpression write FExpression;
end;
{ TFpDebugThreadStackTraceCommand }
TFpDebugThreadStackTraceCommand = class(TFpDebugThreadCommand)
private
FStackEntryArray: TFpDebugEventCallStackEntryArray;
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
procedure ComposeSuccessEvent(var AnEvent: TFpDebugEvent); override;
end;
{ TFpDebugThreadDisassembleCommand }
TFpDebugThreadDisassembleCommand = class(TFpDebugThreadCommand)
private
FAddressValue: TDBGPtr;
FLinesAfter: integer;
FLinesBefore: integer;
FDisassemblerEntryArray: TFpDebugEventDisassemblerEntryArray;
FStartAddr: TDBGPtr;
FEndAddr: TDBGPtr;
FLastEntryEndAddr: TDBGPtr;
function GetAddress: string;
procedure SetAddress(AValue: string);
{$ifndef disassemblernestedproc}
private
FController: TFpServerDbgController;
function OnAdjustToKnowFunctionStart(var AStartAddr: TDisassemblerAddress): Boolean;
function OnDoDisassembleRange(AnEntryRanges: TDBGDisassemblerEntryMap; AFirstAddr, ALastAddr: TDisassemblerAddress; AStopAfterAddress: TDBGPtr; AStopAfterNumLines: Integer): Boolean;
{$endif}
public
constructor Create(AListenerIdentifier: integer; AnUID: variant; AOnLog: TOnLog); override;
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
procedure ComposeSuccessEvent(var AnEvent: TFpDebugEvent); override;
published
property Address: string read GetAddress write SetAddress;
property LinesAfter: integer read FLinesAfter write FLinesAfter;
property LinesBefore: integer read FLinesBefore write FLinesBefore;
end;
{ TFpDebugLocalsCommand }
TFpDebugLocalsCommand = class(TFpDebugThreadCommand)
private
FWatchEntryArray: TFpDebugEventWatchEntryArray;
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
procedure ComposeSuccessEvent(var AnEvent: TFpDebugEvent); override;
end;
{ TFpDebugRegistersCommand }
TFpDebugRegistersCommand = class(TFpDebugThreadCommand)
private
FWatchEntryArray: TFpDebugEventWatchEntryArray;
public
function Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean; override;
class function TextName: string; override;
procedure ComposeSuccessEvent(var AnEvent: TFpDebugEvent); override;
end;
implementation
uses
FpDbgDisasX86;
{ TFpDebugRegistersCommand }
function TFpDebugRegistersCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
var
ARegisterList: TDbgRegisterValueList;
i: Integer;
begin
result := false;
if (AController = nil) or (AController.CurrentProcess = nil) or
(AController.CurrentProcess.DbgInfo = nil) then
exit;
ARegisterList := AController.CurrentProcess.MainThread.RegisterValueList;
SetLength(FWatchEntryArray, ARegisterList.Count);
for i := 0 to ARegisterList.Count-1 do
begin
FWatchEntryArray[i].Expression := ARegisterList[i].Name;
FWatchEntryArray[i].TextValue := ARegisterList[i].StrValue;
FWatchEntryArray[i].NumValue := ARegisterList[i].NumValue;
FWatchEntryArray[i].Size := ARegisterList[i].Size;
end;
result := true;
DoProcessLoop := false;
end;
class function TFpDebugRegistersCommand.TextName: string;
begin
result := 'registers';
end;
procedure TFpDebugRegistersCommand.ComposeSuccessEvent(var AnEvent: TFpDebugEvent);
begin
inherited ComposeSuccessEvent(AnEvent);
AnEvent.WatchEntryArray := FWatchEntryArray;
end;
{ TFpDebugLocalsCommand }
function TFpDebugLocalsCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
var
AContext: TFpDbgInfoContext;
ProcVal: TFpDbgValue;
i: Integer;
m: TFpDbgValue;
n, v: String;
Reg: TDBGPtr;
PrettyPrinter: TFpPascalPrettyPrinter;
begin
result := false;
if (AController = nil) or (AController.CurrentProcess = nil) or
(AController.CurrentProcess.DbgInfo = nil) then
exit;
Reg := AController.CurrentThread.GetInstructionPointerRegisterValue;
AContext := AController.CurrentProcess.DbgInfo.FindContext(AController.CurrentThread.ID, 0, Reg);
if (AContext = nil) or (AContext.SymbolAtAddress = nil) then
exit;
ProcVal := AContext.ProcedureAtAddress;
if (ProcVal = nil) then
exit;
PrettyPrinter := TFpPascalPrettyPrinter.Create(sizeof(pointer));
try
PrettyPrinter.AddressSize := AContext.SizeOfAddress;
SetLength(FWatchEntryArray, ProcVal.MemberCount);
for i := 0 to ProcVal.MemberCount - 1 do
begin
m := ProcVal.Member[i];
if m <> nil then
begin
if m.DbgSymbol <> nil then
n := m.DbgSymbol.Name
else
n := '';
PrettyPrinter.PrintValue(v, m);
FWatchEntryArray[i].TextValue := v;
FWatchEntryArray[i].Expression := n;
end;
end;
finally
PrettyPrinter.Free;
end;
AContext.ReleaseReference;
DoProcessLoop:=false;
result := true;
end;
class function TFpDebugLocalsCommand.TextName: string;
begin
result := 'locals';
end;
procedure TFpDebugLocalsCommand.ComposeSuccessEvent(var AnEvent: TFpDebugEvent);
begin
inherited ComposeSuccessEvent(AnEvent);
AnEvent.WatchEntryArray := FWatchEntryArray;
end;
{ TFpDebugThreadDisassembleCommand }
function TFpDebugThreadDisassembleCommand.GetAddress: string;
begin
result := FormatAddress(FAddressValue);
end;
procedure TFpDebugThreadDisassembleCommand.SetAddress(AValue: string);
begin
FAddressValue := Hex2Dec(AValue);
end;
constructor TFpDebugThreadDisassembleCommand.Create(AListenerIdentifier: integer; AnUID: variant; AOnLog: TOnLog);
begin
inherited Create(AListenerIdentifier, AnUID, AOnLog);
FLinesAfter:=10;
FLinesBefore:=5;
end;
{$ifdef disassemblernestedproc}
function TFpDebugThreadDisassembleCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
{$endif}
function {$ifndef disassemblernestedproc}TFpDebugThreadDisassembleCommand.{$endif}OnAdjustToKnowFunctionStart(var AStartAddr: TDisassemblerAddress): Boolean;
var
Sym: TFpDbgSymbol;
begin
Sym := {$ifndef disassemblernestedproc}FController{$else}AController{$endif}.CurrentProcess.FindSymbol(AStartAddr.GuessedValue);
if assigned(Sym) and (Sym.Kind in [skProcedure, skFunction]) then
begin
AStartAddr.Value:=Sym.Address.Address;
AStartAddr.Offset:=0;
AStartAddr.Validity:=avFoundFunction;
result := true;
end
else
result := false;
end;
function {$ifndef disassemblernestedproc}TFpDebugThreadDisassembleCommand.{$endif}OnDoDisassembleRange(AnEntryRanges: TDBGDisassemblerEntryMap; AFirstAddr, ALastAddr: TDisassemblerAddress; AStopAfterAddress: TDBGPtr; AStopAfterNumLines: Integer): Boolean;
var
AnAddr: TDBGPtr;
CodeBin: array[0..20] of byte;
AnEntry: TDisassemblerEntry;
p: pointer;
ADump,
AStatement,
ASrcFileName: string;
ASrcFileLine: cardinal;
i,j: Integer;
Sym: TFpDbgSymbol;
StatIndex: integer;
FirstIndex: integer;
AResultList: TDBGDisassemblerEntryRange;
begin
result := false;
AResultList := TDBGDisassemblerEntryRange.Create;
AResultList.RangeStartAddr := AFirstAddr.Value;
Sym:=nil;
ASrcFileLine:=0;
ASrcFileName:='';
StatIndex:=0;
FirstIndex:=0;
AnEntry.Offset:=-1;
AnAddr:=AFirstAddr.Value;
i := 0;
while ((AStopAfterAddress=0) or (AStopAfterNumLines > -1)) and (AnAddr <= ALastAddr.Value) do
begin
AnEntry.Addr:=AnAddr;
if not {$ifndef disassemblernestedproc}FController{$else}AController{$endif}.CurrentProcess.ReadData(AnAddr, sizeof(CodeBin),CodeBin) then
begin
Log(Format('Disassemble: Failed to read memory at %s.', [FormatAddress(AnAddr)]), dllDebug);
AnEntry.Statement := 'Failed to read memory';
inc(AnAddr);
end
else
begin
p := @CodeBin;
FpDbgDisasX86.Disassemble(p, {$ifndef disassemblernestedproc}FController{$else}AController{$endif}.CurrentProcess.Mode=dm64, ADump, AStatement);
Sym := {$ifndef disassemblernestedproc}FController{$else}AController{$endif}.CurrentProcess.FindSymbol(AnAddr);
// If this is the last statement for this source-code-line, fill the
// SrcStatementCount from the prior statements.
if (assigned(sym) and ((ASrcFileName<>sym.FileName) or (ASrcFileLine<>sym.Line))) or
(not assigned(sym) and ((ASrcFileLine<>0) or (ASrcFileName<>''))) then
begin
for j := 0 to StatIndex-1 do
AResultList.EntriesPtr[FirstIndex+j]^.SrcStatementCount:=StatIndex;
StatIndex:=0;
FirstIndex:=i;
end;
if assigned(sym) then
begin
ASrcFileName:=sym.FileName;
ASrcFileLine:=sym.Line;
end
else
begin
ASrcFileName:='';
ASrcFileLine:=0;
end;
AnEntry.Dump := ADump;
AnEntry.Statement := AStatement;
AnEntry.SrcFileLine:=ASrcFileLine;
AnEntry.SrcFileName:=ASrcFileName;
AnEntry.SrcStatementIndex:=StatIndex;
inc(StatIndex);
AResultList.RangeEndAddr:=AnAddr;
Inc(AnAddr, {%H-}PtrUInt(p) - {%H-}PtrUInt(@CodeBin));
end;
AResultList.Append(@AnEntry);
if (AnAddr>AStopAfterAddress) then
dec(AStopAfterNumLines);
inc(i);
end;
AResultList.LastEntryEndAddr:=AnAddr;
if AResultList.Count>0 then
AnEntryRanges.AddRange(AResultList)
else
AResultList.Free;
result := true;
end;
{$ifndef disassemblernestedproc}
function TFpDebugThreadDisassembleCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
{$endif disassemblernestedproc}
var
i: Integer;
DisassembleRangeExtender: TDBGDisassemblerRangeExtender;
DisassemblerEntryRange: TDBGDisassemblerEntryRange;
DisassemblerEntryRangeMap: TDBGDisassemblerEntryMap;
RangeIterator: TDBGDisassemblerEntryMapIterator;
ARange: TDBGDisassemblerEntryRange;
begin
{$ifndef disassemblernestedproc}
FController := AController;
{$endif}
result := false;
DoProcessLoop:=false;
if not assigned(AController.CurrentProcess) then
begin
log('Failed to dissasemble: No process', dllInfo);
exit;
end;
if FAddressValue=0 then
FStartAddr:=AController.CurrentThread.GetInstructionPointerRegisterValue
else
FStartAddr:=FAddressValue;
DisassemblerEntryRangeMap := TDBGDisassemblerEntryMap.Create(itu8, SizeOf(TDBGDisassemblerEntryRange));
try
DisassembleRangeExtender := TDBGDisassemblerRangeExtender.Create(DisassemblerEntryRangeMap);
try
DisassembleRangeExtender.OnDoDisassembleRange:=@OnDoDisassembleRange;
DisassembleRangeExtender.OnAdjustToKnowFunctionStart:=@OnAdjustToKnowFunctionStart;
DisassembleRangeExtender.DisassembleRange(FLinesBefore, FLinesAfter, FStartAddr, FStartAddr);
finally
DisassembleRangeExtender.Free;
end;
// Convert the DisassemblerEntryRangeMap to the FDisassemblerEntryArray
DisassemblerEntryRange := TDBGDisassemblerEntryRange.Create;
try
RangeIterator := TDBGDisassemblerEntryMapIterator.Create(DisassemblerEntryRangeMap);
try
RangeIterator.First;
RangeIterator.GetData(ARange);
repeat
DisassemblerEntryRange.Merge(ARange);
ARange := RangeIterator.NextRange;
until RangeIterator.EOM;
setlength(FDisassemblerEntryArray, DisassemblerEntryRange.Count);
for i := 0 to DisassemblerEntryRange.Count-1 do
begin
FDisassemblerEntryArray[i] := DisassemblerEntryRange.Entries[i];
end;
FStartAddr:=DisassemblerEntryRange.RangeStartAddr;
FEndAddr:=DisassemblerEntryRange.RangeEndAddr;
FLastEntryEndAddr:=DisassemblerEntryRange.LastEntryEndAddr;
finally
RangeIterator.Free;
end;
finally
DisassemblerEntryRange.Free;;
end;
finally
DisassemblerEntryRangeMap.Free;
end;
result := true;
end;
class function TFpDebugThreadDisassembleCommand.TextName: string;
begin
result := 'disassemble';
end;
procedure TFpDebugThreadDisassembleCommand.ComposeSuccessEvent(var AnEvent: TFpDebugEvent);
begin
inherited ComposeSuccessEvent(AnEvent);
AnEvent.DisassemblerEntryArray := FDisassemblerEntryArray;
AnEvent.Addr1:=FStartAddr;
AnEvent.Addr2:=FEndAddr;
AnEvent.Addr3:=FLastEntryEndAddr;
end;
{ TFpDebugThreadSetConsoleTtyCommand }
function TFpDebugThreadSetConsoleTtyCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
begin
AController.ConsoleTty:=FConsoleTty;
AController.RedirectConsoleOutput:=(AController.ConsoleTty='');
DoProcessLoop:=false;
result:=true;
end;
class function TFpDebugThreadSetConsoleTtyCommand.TextName: string;
begin
result := 'setconsoletty';
end;
{ TFpDebugThreadStackTraceCommand }
function TFpDebugThreadStackTraceCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
var
ThreadCallStack: TDbgCallstackEntryList;
i: integer;
begin
result := false;
DoProcessLoop:=false;
if not assigned(AController.CurrentProcess) then
begin
log('Failed to get call stack: No process', dllInfo);
exit;
end;
AController.CurrentProcess.MainThread.PrepareCallStackEntryList;
ThreadCallStack := AController.CurrentProcess.MainThread.CallStackEntryList;
SetLength(FStackEntryArray, ThreadCallStack.Count);
for i := 0 to ThreadCallStack.Count-1 do
begin
FStackEntryArray[i].AnAddress:=ThreadCallStack[i].AnAddress;
FStackEntryArray[i].FrameAdress:=ThreadCallStack[i].FrameAdress;
FStackEntryArray[i].FunctionName:=ThreadCallStack[i].FunctionName+ThreadCallStack[i].GetParamsAsString;
FStackEntryArray[i].Line:=ThreadCallStack[i].Line;
FStackEntryArray[i].SourceFile:=ThreadCallStack[i].SourceFile;
end;
// Clear the callstack immediately. Doing this each time the process continous is
// cumbersome. And the chances that this command is called twice, so that
// caching the result is usefull, are slim.
AController.CurrentProcess.MainThread.ClearCallStack;
result := true;
end;
class function TFpDebugThreadStackTraceCommand.TextName: string;
begin
result := 'stacktrace';
end;
procedure TFpDebugThreadStackTraceCommand.ComposeSuccessEvent(var AnEvent: TFpDebugEvent);
begin
inherited ComposeSuccessEvent(AnEvent);
AnEvent.StackEntryArray:=FStackEntryArray;
end;
{ TFpDebugThreadEvaluateCommand }
procedure TFpDebugThreadEvaluateCommand.ComposeSuccessEvent(var AnEvent: TFpDebugEvent);
begin
inherited ComposeSuccessEvent(AnEvent);
AnEvent.Message:=FResText;
AnEvent.Validity:=FValidity;
end;
function TFpDebugThreadEvaluateCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
var
AContext: TFpDbgInfoContext;
APasExpr: TFpPascalExpression;
ADbgInfo: TDbgInfo;
Res: Boolean;
APrettyPrinter: TFpPascalPrettyPrinter;
ATypeInfo: TDBGType;
begin
Result := False;
DoProcessLoop:=false;
if not assigned(AController.CurrentProcess) then
begin
log('Failed to evaluate expression: No process', dllInfo);
exit;
end;
ADbgInfo := AController.CurrentProcess.DbgInfo;
AContext := ADbgInfo.FindContext(AController.CurrentThread.ID, 0, AController.CurrentThread.GetInstructionPointerRegisterValue);
if AContext = nil then
begin
FValidity:=ddsInvalid;
exit;
end;
Result := True;
AContext.MemManager.DefaultContext := AContext;
APasExpr := TFpPascalExpression.Create(FExpression, AContext);
try
APasExpr.ResultValue; // trigger full validation
if not APasExpr.Valid then
begin
FResText := ErrorHandler.ErrorAsString(APasExpr.Error);
FValidity := ddsError;
end
else
begin
APrettyPrinter := TFpPascalPrettyPrinter.Create(sizeof(pointer));
try
APrettyPrinter.AddressSize:=AContext.SizeOfAddress;
APrettyPrinter.MemManager := AContext.MemManager;
Res := APrettyPrinter.PrintValue(FResText, ATypeInfo, APasExpr.ResultValue);
if Res then
begin
FValidity:=ddsValid;
end
else
begin
FResText := 'Error';
FValidity:=ddsValid;
end;
finally
APrettyPrinter.Free;
end;
end;
finally
APasExpr.Free;
AContext.ReleaseReference;
end;
end;
class function TFpDebugThreadEvaluateCommand.TextName: string;
begin
result := 'evaluate';
end;
{ TFpDebugThreadQuitDebugServerCommand }
function TFpDebugThreadQuitDebugServerCommand.PreExecute(AController: TFpServerDbgController; out DoQueueCommand: boolean): boolean;
begin
DoQueueCommand:=false;
CustomApplication.Terminate;
result := true;
end;
function TFpDebugThreadQuitDebugServerCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
begin
result := true;
DoProcessLoop := false;
end;
class function TFpDebugThreadQuitDebugServerCommand.TextName: string;
begin
result := 'quitdebugserver';
end;
{ TFpDebugThreadRemoveBreakpointCommand }
function TFpDebugThreadRemoveBreakpointCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
var
Brk: TFpInternalBreakpoint;
begin
result := false;
DoProcessLoop:=false;
if not assigned(AController.CurrentProcess) then
begin
log('Failed to remove breakpoint: No process', dllInfo);
exit;
end;
if (FBreakpointServerIdr<>0) then begin
Brk := AController.GetInternalBreakPointFromId(FBreakpointServerIdr);
result := AController.CurrentProcess.RemoveBreak(Brk);
Brk.Free; // actually removes it from target process
AController.RemoveInternalBreakPoint(FBreakpointServerIdr);
end
else
log('Failed to remove breakpoint: No location given', dllInfo);
end;
class function TFpDebugThreadRemoveBreakpointCommand.TextName: string;
begin
result := 'removebreakpoint';
end;
{ TFpDebugThreadStopCommand }
function TFpDebugThreadStopCommand.Execute(AController: TFpServerDbgController; out
DoProcessLoop: boolean): boolean;
begin
AController.Stop;
DoProcessLoop:=true;
result := true;
end;
class function TFpDebugThreadStopCommand.TextName: string;
begin
result := 'stop';
end;
{ TFpDebugThreadStepOutCommand }
function TFpDebugThreadStepOutCommand.Execute(AController: TFpServerDbgController; out
DoProcessLoop: boolean): boolean;
begin
AController.StepOut;
DoProcessLoop:=true;
result := true;
end;
class function TFpDebugThreadStepOutCommand.TextName: string;
begin
result := 'stepout';
end;
{ TFpDebugThreadStepOverInstrCommand }
function TFpDebugThreadStepOverInstrCommand.Execute(
AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
begin
AController.StepOverInstr;
DoProcessLoop:=true;
result := true;
end;
class function TFpDebugThreadStepOverInstrCommand.TextName: string;
begin
result := 'stepoverinstr';
end;
{ TFpDebugThreadStepIntoInstrCommand }
function TFpDebugThreadStepIntoInstrCommand.Execute(
AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
begin
AController.StepIntoInstr;
DoProcessLoop:=true;
result := true;
end;
class function TFpDebugThreadStepIntoInstrCommand.TextName: string;
begin
result := 'stepintoinstr';
end;
{ TFpDebugThreadStepCommand }
function TFpDebugThreadStepCommand.Execute(AController: TFpServerDbgController; out
DoProcessLoop: boolean): boolean;
begin
AController.Step;
DoProcessLoop:=true;
result := true;
end;
class function TFpDebugThreadStepCommand.TextName: string;
begin
result := 'step';
end;
{ TFpDebugThreadNextCommand }
function TFpDebugThreadNextCommand.Execute(AController: TFpServerDbgController; out
DoProcessLoop: boolean): boolean;
begin
AController.Next;
DoProcessLoop:=true;
result := true;
end;
class function TFpDebugThreadNextCommand.TextName: string;
begin
result := 'next';
end;
{ TFpDebugThreadGetLocationInfoCommand }
function TFpDebugThreadGetLocationInfoCommand.GetAddress: string;
begin
result := FormatAddress(FAddressValue);
end;
procedure TFpDebugThreadGetLocationInfoCommand.SetAddress(AValue: string);
begin
FAddressValue := Hex2Dec(AValue);
end;
procedure TFpDebugThreadGetLocationInfoCommand.ComposeSuccessEvent(var AnEvent: TFpDebugEvent);
begin
inherited ComposeSuccessEvent(AnEvent);
AnEvent.LocationRec:=FLocationRec;
end;
function TFpDebugThreadGetLocationInfoCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
var
sym, symproc: TFpDbgSymbol;
begin
DoProcessLoop:=false;
result := false;
if not assigned(AController.CurrentProcess) then
begin
log('Failed to get location info: No process', dllInfo);
exit;
end
else
begin
FLocationRec.FuncName:='';
FLocationRec.SrcFile:='';
FLocationRec.SrcFullName:='';
FLocationRec.SrcLine:=0;
if FAddressValue=0 then
FLocationRec.Address := AController.CurrentThread.GetInstructionPointerRegisterValue
else
FLocationRec.Address := FAddressValue;
sym := AController.CurrentProcess.FindSymbol(FLocationRec.Address);
if sym = nil then
Exit;
FLocationRec.SrcFile := ExtractFileName(sym.FileName);
FLocationRec.SrcLine := sym.Line;
FLocationRec.SrcFullName := sym.FileName;
symproc := sym;
while not (symproc.kind in [skProcedure, skFunction]) do
symproc := symproc.Parent;
if assigned(symproc) then
FLocationRec.FuncName:=symproc.Name;
sym.free;
result := true;
end;
end;
class function TFpDebugThreadGetLocationInfoCommand.TextName: string;
begin
result := 'getlocationinfo'
end;
{ TFpDebugThreadAddBreakpointCommand }
procedure TFpDebugThreadAddBreakpointCommand.ComposeSuccessEvent(var AnEvent: TFpDebugEvent);
begin
inherited ComposeSuccessEvent(AnEvent);
AnEvent.BreakpointServerIdr:=FBreakServerId;
end;
function TFpDebugThreadAddBreakpointCommand.Execute(AController: TFpServerDbgController; out DoProcessLoop: boolean): boolean;
begin
result := false;
FBreakServerId := 0;
DoProcessLoop:=false;
if not assigned(AController.CurrentProcess) then
begin
log('Failed to add breakpoint: No process', dllInfo);
exit;
end;
if (Filename<>'') and (line>-1) then
begin
FBreakPoint := AController.CurrentProcess.AddBreak(FileName, Line);
result := assigned(FBreakPoint);
if Result then
FBreakServerId := AController.AddInternalBreakPointToId(FBreakPoint);
end
else
log('Failed to add breakpoint: No filename and line-number given', dllInfo);
end;
class function TFpDebugThreadAddBreakpointCommand.TextName: string;
begin
result := 'addbreakpoint';
end;
{ TFpDebugThreadCommandList }
var
GFpDebugThreadCommandList: TFpDebugThreadCommandList = nil;
class function TFpDebugThreadCommandList.instance: TFpDebugThreadCommandList;
begin
if not assigned(GFpDebugThreadCommandList) then
GFpDebugThreadCommandList := TFpDebugThreadCommandList.Create;
result := GFpDebugThreadCommandList;
end;
function TFpDebugThreadCommandList.GetCommandByName(ATextName: string): TFpDebugThreadCommandClass;
var
i: Integer;
begin
result := nil;
for i := 0 to count -1 do
begin
if TFpDebugThreadCommandClass(Items[i]).TextName=ATextName then
result := TFpDebugThreadCommandClass(Items[i]);
end;
end;
{ TFpDebugThreadContinueCommand }
function TFpDebugThreadContinueCommand.Execute(AController: TFpServerDbgController; out
DoProcessLoop: boolean): boolean;
begin
DoProcessLoop:=true;
result := true;
end;
class function TFpDebugThreadContinueCommand.TextName: string;
begin
result := 'continue';
end;
{ TFpDebugThreadRunCommand }
function TFpDebugThreadRunCommand.Execute(AController: TFpServerDbgController; out
DoProcessLoop: boolean): boolean;
begin
DoProcessLoop := AController.Run;
result := DoProcessLoop;
end;
class function TFpDebugThreadRunCommand.TextName: string;
begin
result := 'run';
end;
{ TFpDebugThreadSetFilenameCommand }
function TFpDebugThreadSetFilenameCommand.Execute(AController: TFpServerDbgController;
out DoProcessLoop: boolean): boolean;
begin
AController.ExecutableFilename:=FFileName;
DoProcessLoop:=false;
result:=true;
end;
class function TFpDebugThreadSetFilenameCommand.TextName: string;
begin
result := 'filename'
end;
initialization
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadQuitDebugServerCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadSetFilenameCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadSetConsoleTtyCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadRunCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadContinueCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadStepOverInstrCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadStepIntoInstrCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadNextCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadStepCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadStepOutCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadStopCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadAddBreakpointCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadRemoveBreakpointCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadGetLocationInfoCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadEvaluateCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadStackTraceCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugThreadDisassembleCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugLocalsCommand);
TFpDebugThreadCommandList.instance.Add(TFpDebugRegistersCommand);
finalization
GFpDebugThreadCommandList.Free;
end.
|