/usr/lib/lazarus/0.9.30.4/debugger/assemblerdlg.pp is in lazarus-src-0.9.30.4 0.9.30.4-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | unit AssemblerDlg;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
ComCtrls, StdCtrls, Grids, ExtCtrls, LclType, LCLIntf, DebuggerDlg, Debugger,
BaseDebugManager, EditorOptions, Maps, Math, LCLProc, Menus, Clipbrd, ActnList,
IDECommands, IDEImagesIntf, CodeToolManager, CodeCache, SourceEditor;
type
{ TAssemblerDlg }
TAsmDlgLineMapState = (
lmsUnknown,
lmsInvalid, // debugger couldn't disassemble this address
lmsStatement, // display line as assembler
lmsSource, // display line as source
lmsFuncName // Name of function
);
TAsmDlgLineEntry = record
State: TAsmDlgLineMapState;
Addr: TDbgPtr;
Dump: String;
Statement: String;
PasCode: String;
FileName: String;
SourceLine: Integer;
end;
TAsmDlgLineEntries = Array of TAsmDlgLineEntry;
TAssemblerDlg = class(TDebuggerDlg)
actCurrentInstr: TAction;
actGotoAddr: TAction;
actCopy: TAction;
actStepOverInstr: TAction;
actStepIntoInstr: TAction;
ActionList1: TActionList;
CopyToClipboard: TMenuItem;
EditGotoAddr: TEdit;
ImageList1: TImageList;
pnlToolAddr: TPanel;
pbAsm: TPaintBox;
PopupMenu1: TPopupMenu;
sbHorizontal: TScrollBar;
sbVertical: TScrollBar;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButtonCopy: TToolButton;
ToolButtonGoto: TToolButton;
ToolButtonGotoCurrent: TToolButton;
ToolButtonStepOverInstr: TToolButton;
ToolButtonStepIntoInstr: TToolButton;
ToolButton4: TToolButton;
ToolButtonPower: TToolButton;
ToolButton2: TToolButton;
procedure actCurrentInstrExecute(Sender: TObject);
procedure actGotoAddrExecute(Sender: TObject);
procedure actStepIntoInstrExecute(Sender: TObject);
procedure actStepOverInstrExecute(Sender: TObject);
procedure CopyToClipboardClick(Sender: TObject);
procedure EditGotoAddrChange(Sender: TObject);
procedure EditGotoAddrKeyPress(Sender: TObject; var Key: char);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormResize(Sender: TObject);
procedure pbAsmMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure pbAsmMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure pbAsmMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
procedure pbAsmMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure pbAsmPaint(Sender: TObject);
procedure sbHorizontalChange(Sender: TObject);
procedure sbVerticalChange(Sender: TObject);
procedure sbVerticalScroll(Sender: TObject; ScrollCode: TScrollCode; var ScrollPos: Integer);
procedure ToolButtonPowerClick(Sender: TObject);
private
FDebugger: TDebugger;
FDebugManager: TBaseDebugManager;
FDisassembler: TIDEDisassembler;
FDisassemblerNotification: TIDEDisassemblerNotification;
FCurrentLocation, FLocation: TDBGPtr;
FMouseIsDown: Boolean;
FTopLine: Integer;
FLastTopLine: Integer;
FLastTopLineIdx: Integer;
FLastTopLineIsSrc: Boolean; // The Source In Fron of Idx
FLastTopLineValid: Boolean;
FSelectLine: Integer;
FSelectionEndLine: Integer;
FLineCount: Integer;
FLineMap: TAsmDlgLineEntries;
FLineHeight: Integer;
FCharWidth: Integer;
FGutterWidth: Integer;
FUpdating: Boolean;
FUpdateNeeded: Boolean;
FPowerImgIdx, FPowerImgIdxGrey: Integer;
FCurLineImgIdx: Integer;
procedure DoDebuggerDestroyed(Sender: TObject);
procedure ClearLineMap(AState: TAsmDlgLineMapState = lmsUnknown);
procedure DisassemblerChanged(Sender: TObject);
procedure SetDisassembler(const AValue: TIDEDisassembler);
procedure SetDebugger(const AValue: TDebugger);
function FormatLine(ALine: TAsmDlgLineEntry; W: Integer): String;
procedure UpdateView;
procedure UpdateActionEnabled;
procedure UpdateLineData;
procedure UpdateLineDataEx(ALineMap: TAsmDlgLineEntries;
AFirstLine, ALineCount: Integer;
var ACachedLine, ACachedIdx: Integer;
var ACachedIsSrc, ACachedValid: Boolean;
ACachedUpdate: Boolean;
ANoExtraHeader: Boolean = False
);
procedure SetSelection(ALine: Integer; AMakeVisible: Boolean; AKeepSelEnd: Boolean = False);
procedure SetLineCount(ALineCount: Integer);
procedure SetTopLine(ALine: Integer);
function IndexOfAddr(const AAddr: TDBGPtr): Integer;
procedure UpdateLocation(const AAddr: TDBGPtr);
procedure DoEditorOptsChanged(Sender: TObject; Restore: boolean);
protected
function GetSourceCodeLine(SrcFileName: string; SrcLineNumber: Integer): string;
procedure InitializeWnd; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetLocation(ADebugger: TDebugger; const AAddr: TDBGPtr);
property Disassembler: TIDEDisassembler read FDisassembler write SetDisassembler;
property DebugManager: TBaseDebugManager read FDebugManager write FDebugManager;
end;
implementation
{$R *.lfm}
uses
LazarusIDEStrConsts;
{ TAssemblerDlg }
procedure TAssemblerDlg.ClearLineMap(AState: TAsmDlgLineMapState = lmsUnknown);
var
n: Integer;
begin
FLastTopLineValid := False;
for n := Low(FLineMap) to High(FLineMap) do
begin
FLineMap[n].State := AState;
FLineMap[n].Dump := '';
FLineMap[n].Statement := '';
if AState = lmsUnknown
then FLineMap[n].Addr := 0;
end;
end;
procedure TAssemblerDlg.SetDisassembler(const AValue: TIDEDisassembler);
begin
if FDisassembler = AValue then exit;
BeginUpdate;
try
if FDisassembler <> nil
then begin
FDisassembler.RemoveNotification(FDisassemblerNotification);
end;
FDisassembler := AValue;
if FDisassembler <> nil
then begin
FDisassembler.AddNotification(FDisassemblerNotification);
end;
DisassemblerChanged(FDisassembler);
finally
EndUpdate;
end;
UpdateActionEnabled;
end;
procedure TAssemblerDlg.SetDebugger(const AValue: TDebugger);
begin
if FDebugger = AValue
then exit;
if FDebugger <> nil
then FDebugger.RemoveNotifyEvent(dnrDestroy, @DoDebuggerDestroyed);
FDebugger := AValue;
if FDebugger <> nil
then FDebugger.AddNotifyEvent(dnrDestroy, @DoDebuggerDestroyed);
UpdateActionEnabled;
end;
constructor TAssemblerDlg.Create(AOwner: TComponent);
begin
FCurrentLocation := 0;
FLocation := 0;
FLineCount := 0;
SetLength(FLineMap, FLineCount + 1);
FGutterWidth := 32;
FDisassemblerNotification := TIDEDisassemblerNotification.Create;
FDisassemblerNotification.AddReference;
FDisassemblerNotification.OnChange := @DisassemblerChanged;
inherited Create(AOwner);
// DoubleBuffered := True;
Caption := lisDisAssAssembler;
EditorOpts.AddHandlerAfterWrite(@DoEditorOptsChanged);
pbAsm.Font.Height := EditorOpts.EditorFontHeight;
pbAsm.Font.Name := EditorOpts.EditorFont;
Caption := lisMenuViewAssembler;
CopyToClipboard.Caption := lisDbgAsmCopyToClipboard;
ToolBar1.Images := IDEImages.Images_16;
PopupMenu1.Images := IDEImages.Images_16;
actStepOverInstr.Caption := lisMenuStepOverInstr;
actStepOverInstr.Hint := lisMenuStepOverInstrHint;
actStepOverInstr.ImageIndex := IDEImages.LoadImage(16, 'menu_stepover_instr');
actStepIntoInstr.Caption := lisMenuStepIntoInstr;
actStepIntoInstr.Hint := lisMenuStepIntoInstrHint;
actStepIntoInstr.ImageIndex := IDEImages.LoadImage(16, 'menu_stepinto_instr');
actCurrentInstr.Caption := lisDisAssGotoCurrentAddress;
actCurrentInstr.Hint := lisDisAssGotoCurrentAddressHint;
actCurrentInstr.ImageIndex := IDEImages.LoadImage(16, 'debugger_current_line');
actGotoAddr.Caption := lisDisAssGotoAddress;
actGotoAddr.Hint := lisDisAssGotoAddressHint;
actGotoAddr.ImageIndex := IDEImages.LoadImage(16, 'callstack_show');
actCopy.Caption := lisMenuCopy;
actCopy.Hint := lisMenuCopy;
actCopy.ImageIndex := IDEImages.LoadImage(16, 'laz_copy');
FPowerImgIdx := IDEImages.LoadImage(16, 'debugger_power');
FPowerImgIdxGrey := IDEImages.LoadImage(16, 'debugger_power_grey');
ToolButtonPower.ImageIndex := FPowerImgIdx;
FCurLineImgIdx := IDEImages.LoadImage(16, 'debugger_current_line');
end;
destructor TAssemblerDlg.Destroy;
begin
EditorOpts.RemoveHandlerAfterWrite(@DoEditorOptsChanged);
SetDisassembler(nil);
SetDebugger(nil);
FDisassemblerNotification.OnChange := nil;
FDisassemblerNotification.ReleaseReference;
inherited Destroy;
end;
procedure TAssemblerDlg.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
i: LongInt;
begin
if (Shift - [ssShift] <> []) then begin
inherited;
Exit;
end;
case Key of
VK_UP: begin
ToolButtonPower.Down := True;
ToolButtonPowerClick(nil);
SetSelection(FSelectLine - 1, True, ssShift in Shift);
Key := 0;
end;
VK_DOWN: begin
ToolButtonPower.Down := True;
ToolButtonPowerClick(nil);
SetSelection(FSelectLine + 1, True, ssShift in Shift);
Key := 0;
end;
VK_PRIOR: begin
ToolButtonPower.Down := True;
ToolButtonPowerClick(nil);
i := FTopLine;
SetSelection(FSelectLine - FLineCount, False, ssShift in Shift);
SetTopline(i - FLineCount);
Key := 0;
end;
VK_NEXT: begin
ToolButtonPower.Down := True;
ToolButtonPowerClick(nil);
i := FTopLine;
SetSelection(FSelectLine + FLineCount, False, ssShift in Shift);
SetTopline(i + FLineCount);
Key := 0;
end;
VK_LEFT: begin
if not EditGotoAddr.Focused then begin
sbHorizontal.Position := sbHorizontal.Position - sbHorizontal.SmallChange;
Key := 0;
end;
end;
VK_RIGHT: begin
if not EditGotoAddr.Focused then begin
sbHorizontal.Position := sbHorizontal.Position + sbHorizontal.SmallChange;
Key := 0;
end;
end;
VK_HOME: begin
if not EditGotoAddr.Focused then begin
sbHorizontal.Position := 0;
Key := 0;
end;
end;
else
inherited;
end;
pbAsm.Invalidate;
end;
procedure TAssemblerDlg.CopyToClipboardClick(Sender: TObject);
var
ALineMap: TAsmDlgLineEntries;
i, w: Integer;
s: String;
begin
SetLength(ALineMap, abs(FSelectionEndLine - FSelectLine)+1);
UpdateLineDataEx(ALineMap, Min(FSelectionEndLine, FSelectLine),
abs(FSelectionEndLine - FSelectLine)+1,
FLastTopLine, FLastTopLineIdx, FLastTopLineIsSrc, FLastTopLineValid, False, True);
if FDebugger = nil
then W := 16
else W := FDebugger.TargetWidth div 4;
s := '';
for i := 0 to length(ALineMap)-1 do
begin
s := s + FormatLine(ALineMap[i], W) + LineEnding;
end;
Clipboard.AsText := s;
end;
procedure TAssemblerDlg.EditGotoAddrChange(Sender: TObject);
var
HasDisassembler: Boolean;
begin
HasDisassembler := (FDebugger <> nil) and (FDisassembler <> nil);
actGotoAddr.Enabled := HasDisassembler and (StrToQWordDef(EditGotoAddr.Text, 0) <> 0);
end;
procedure TAssemblerDlg.EditGotoAddrKeyPress(Sender: TObject; var Key: char);
begin
if (key = #13) and (StrToQWordDef(EditGotoAddr.Text, 0) <> 0)
then actGotoAddr.Execute;
end;
procedure TAssemblerDlg.actStepOverInstrExecute(Sender: TObject);
var
Handled: Boolean;
begin
if Assigned(OnProcessCommand)
then OnProcessCommand(Self, ecStepOverInstr, Handled);
end;
procedure TAssemblerDlg.actStepIntoInstrExecute(Sender: TObject);
var
Handled: Boolean;
begin
if Assigned(OnProcessCommand)
then OnProcessCommand(Self, ecStepIntoInstr, Handled);
end;
procedure TAssemblerDlg.actCurrentInstrExecute(Sender: TObject);
begin
if FDisassembler.BaseAddr <> FLocation
then begin
ToolButtonPower.Down := True;
ToolButtonPowerClick(nil);
end;
UpdateLocation(FLocation);
end;
procedure TAssemblerDlg.actGotoAddrExecute(Sender: TObject);
var
Addr: TDBGPtr;
begin
ToolButtonPower.Down := True;
ToolButtonPowerClick(nil);
Addr := StrToQWordDef(EditGotoAddr.Text, 0);
if Addr <> 0
then UpdateLocation(Addr);
end;
procedure TAssemblerDlg.DisassemblerChanged(Sender: TObject);
begin
if (FDisassembler = nil) or (FCurrentLocation = 0) or (FLineCount = 0)
then exit;
if (FDebugger <> nil) and (FDebugger.State <> dsPause)
then begin
// only for F9, not for F8,F7 single stepping with assembler is no good, if it clears all the time
//ClearLineMap;
FCurrentLocation := 0;
FLocation := 0;
end
else begin
UpdateView;
end;
pbAsm.Invalidate;
end;
procedure TAssemblerDlg.FormResize(Sender: TObject);
begin
sbHorizontal.PageSize := pbAsm.Width;
sbHorizontal.LargeChange := pbAsm.Width div 3;
if FLineHeight <> 0
then SetLineCount(pbAsm.Height div FLineHeight);
end;
procedure TAssemblerDlg.InitializeWnd;
begin
inherited InitializeWnd;
DoEditorOptsChanged(nil, False);
end;
procedure TAssemblerDlg.pbAsmMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button <> mbLeft then exit;
SetSelection(FTopLine + Y div FLineHeight, False, ssShift in Shift);
FMouseIsDown := True;
end;
procedure TAssemblerDlg.pbAsmMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
y := Y div FLineHeight;
if FMouseIsDown and (y >= 0) and (y < FLineCount)
then SetSelection(FTopLine + Y, False, True);
end;
procedure TAssemblerDlg.pbAsmMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FMouseIsDown := False;
end;
procedure TAssemblerDlg.pbAsmMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
i, j: LongInt;
begin
if not ToolButtonPower.Down then exit;
Handled := True;
j := WheelDelta div 120;
i := FTopLine ;
if FSelectLine <> MaxInt
then SetSelection(FSelectLine - j, False, ssShift in Shift);
SetTopline(i - j);
end;
procedure TAssemblerDlg.pbAsmPaint(Sender: TObject);
var
R: TRect;
n, X, Y, Line, W: Integer;
S: String;
begin
R := pbAsm.ClientRect;
// pbAsm.Canvas.Brush.Color := clWindow;
pbAsm.Canvas.FillRect(R);
Inc(R.Left, FGutterWidth);
X := FGutterWidth - sbHorizontal.Position;
Y := 0;
Line := FTopLine;
if FDebugger = nil
then W := 16
else W := FDebugger.TargetWidth div 4;
for n := 0 to FLineCount do
begin
if Line = FSelectLine
then begin
pbAsm.Canvas.Brush.Color := clHighlight;
pbAsm.Canvas.Font.Color := clHighlightText;
pbAsm.Canvas.FillRect(R.Left, n * FLineHeight, R.Right, (n + 1) * FLineHeight);
if (FSelectionEndLine <> FSelectLine)
then begin
pbAsm.Canvas.Brush.Color := clHotLight;
pbAsm.Canvas.Brush.Style := bsClear;
pbAsm.Canvas.Rectangle(R.Left, n * FLineHeight, R.Right, (n + 1) * FLineHeight);
pbAsm.Canvas.Brush.Style := bsSolid;
pbAsm.Canvas.Brush.Color := clHighlight;
end;
end
else if (FSelectionEndLine <> FSelectLine)
and (line >= Min(FSelectLine, FSelectionEndLine))
and (line <= Max(FSelectLine, FSelectionEndLine))
then begin
pbAsm.Canvas.Brush.Color := clHighlight;
pbAsm.Canvas.Font.Color := clHighlightText;
pbAsm.Canvas.FillRect(R.Left, n * FLineHeight, R.Right, (n + 1) * FLineHeight);
end
else begin
pbAsm.Canvas.Brush.Color := pbAsm.Color;
pbAsm.Canvas.Font.Color := pbAsm.Font.Color;
end;
pbAsm.Canvas.Font.Bold := (FLineMap[n].State in [lmsSource, lmsFuncName]);
if (FLineMap[n].State = lmsStatement) and (FLineMap[n].Addr = FLocation)
then IDEImages.Images_16.Draw(pbAsm.Canvas, FGutterWidth - 16, Y, FCurLineImgIdx, True);
S := FormatLine(FLineMap[n], W);
pbAsm.Canvas.TextRect(R, X, Y, S);
Inc(Y, FLineHeight);
Inc(Line);
end;
end;
function TAssemblerDlg.FormatLine(ALine: TAsmDlgLineEntry; W: Integer) : String;
begin
Result := '';
//Result := Format('[a:%8.8u l:%8.8d i:%3.3u] ', [Cardinal(ALine.Addr), Line, n]);
Result := Result + HexStr(ALine.Addr, W) + ' ';
case ALine.State of
lmsUnknown: Result := Result + '??????';
lmsInvalid: Result := Result + '......';
lmsStatement: Result := Result + Copy(ALine.Dump + ' ', 1, 24) + ' ' + ALine.Statement;
lmsSource: begin
if ALine.SourceLine = 0
then Result := '---'
else Result := Format('%-'+IntToStr(W+25)+'s %s',
[Format('%s:%u %s', [ALine.FileName, ALine.SourceLine, ALine.Statement]),
ALine.PasCode]);
end;
lmsFuncName: Result:= ALine.FileName + ' ' + ALine.Statement;
end;
end;
procedure TAssemblerDlg.UpdateView;
begin
if not ToolButtonPower.Down
then exit;
if (FDisassembler <> nil) and (FCurrentLocation <> 0)
then begin
FDisassembler.PrepareRange(FCurrentLocation, Max(0, -(FTopLine - 5)), Max(0, FTopLine + FLineCount + 1 + 5));
UpdateLineData;
end
else ClearLineMap;
pbAsm.Invalidate;
end;
procedure TAssemblerDlg.UpdateActionEnabled;
var
HasDisassembler: Boolean;
begin
HasDisassembler := (FDebugger <> nil) and (FDisassembler <> nil);
actCurrentInstr.Enabled := HasDisassembler and (FLocation <> 0);
actGotoAddr.Enabled := HasDisassembler and (StrToQWordDef(EditGotoAddr.Text, 0) <> 0);
actCopy.Enabled := HasDisassembler;
actStepOverInstr.Enabled := HasDisassembler;
actStepIntoInstr.Enabled := HasDisassembler;
end;
procedure TAssemblerDlg.sbHorizontalChange(Sender: TObject);
begin
pbAsm.Invalidate;
end;
procedure TAssemblerDlg.sbVerticalChange(Sender: TObject);
begin
ToolButtonPower.Down := True;
ToolButtonPowerClick(nil);
sbVertical.Position := 475;
pbAsm.Invalidate;
end;
procedure TAssemblerDlg.sbVerticalScroll(Sender: TObject; ScrollCode: TScrollCode; var ScrollPos: Integer);
begin
case ScrollCode of
scLineUp: begin
SetTopline(FTopLine - 1);
end;
scLineDown: begin
SetTopline(FTopLine + 1);
end;
scPageUp: begin
SetTopline(FTopLine - FLineCount);
end;
scPageDown: begin
SetTopline(FTopLine + FLineCount);
end;
scPosition: begin
// doesn't work on gtk
end;
scTrack: begin
// --- remove when scPosition works
ScrollPos := 475;
Exit;
// ---
if ScrollPos = 475 then Exit;
if ScrollPos < 475
then SetTopline(FTopLine - 1)
else SetTopline(FTopLine + 1);
end;
// scTop, // = SB_TOP
// scBottom, // = SB_BOTTOM
// scEndScroll // = SB_ENDSCROLL
end;
end;
procedure TAssemblerDlg.ToolButtonPowerClick(Sender: TObject);
begin
if ToolButtonPower.Down
then begin
ToolButtonPower.ImageIndex := FPowerImgIdx;
UpdateView;
end
else ToolButtonPower.ImageIndex := FPowerImgIdxGrey;
end;
procedure TAssemblerDlg.DoDebuggerDestroyed(Sender: TObject);
begin
FDebugger := nil;
FDisassembler := nil;
UpdateView;
end;
function TAssemblerDlg.IndexOfAddr(const AAddr: TDBGPtr): Integer;
begin
Result := length(FLineMap) - 1;
while Result >= 0 do begin
if (FLineMap[Result].State = lmsStatement) and (FLineMap[Result].Addr = FCurrentLocation)
then exit;
dec(Result);
end;
end;
procedure TAssemblerDlg.UpdateLocation(const AAddr: TDBGPtr);
var
i: Integer;
begin
if FCurrentLocation <> AAddr
then begin
FCurrentLocation := AAddr;
FLastTopLineValid := False;
end;
i := IndexOfAddr(FCurrentLocation);
if (i >= 0) and (i < FLineCount - 1)
then begin
FSelectLine := FTopLine + i;
end
else begin
FTopLine := -(FLineCount div 2);
FSelectLine := 0;
end;
FSelectionEndLine := FSelectLine;
UpdateActionEnabled;
UpdateView;
end;
procedure TAssemblerDlg.DoEditorOptsChanged(Sender: TObject; Restore: boolean);
var
TM: TTextMetric;
begin
pbAsm.Font.Height := EditorOpts.EditorFontHeight;
pbAsm.Font.Name := EditorOpts.EditorFont;
if EditorOpts.DisableAntialiasing then
pbAsm.Font.Quality := fqNonAntialiased
else
pbAsm.Font.Quality := fqDefault;
GetTextMetrics(pbAsm.Canvas.Handle, TM);
FCharWidth := TM.tmMaxCharWidth; // EditorOpts.ExtraCharSpacing +
sbHorizontal.SmallChange := FCHarWidth;
FLineHeight := EditorOpts.ExtraLineSpacing + TM.tmHeight;
SetLineCount(pbAsm.Height div FLineHeight);
end;
procedure TAssemblerDlg.SetLocation(ADebugger: TDebugger; const AAddr: TDBGPtr);
var
i: Integer;
begin
SetDebugger(ADebugger);
FCurrentLocation := AAddr;
FLocation := AAddr;
FLastTopLineValid := False;
if not ToolButtonPower.Down
then begin
i := IndexOfAddr(FCurrentLocation);
if (i >= 0)
then FSelectLine := FTopLine + i
else FSelectLine := MaxInt;
FSelectionEndLine := FSelectLine;
pbAsm.Invalidate;
exit;
end;
FTopLine := -(FLineCount div 2);
FSelectLine := 0;
FSelectionEndLine := 0;
UpdateActionEnabled;
if Visible then // otherwhise in resize
UpdateView
else
ClearLineMap;
end;
procedure TAssemblerDlg.SetSelection(ALine: Integer; AMakeVisible: Boolean;
AKeepSelEnd: Boolean = False);
var
OldLine: Integer;
begin
if Aline = FSelectLine then Exit;
// UpdateLineData may cause eventhandling, so we enter here again
// set variable first
OldLine := FSelectLine;
FSelectLine := Aline;
if not AKeepSelEnd
then FSelectionEndLine := FSelectLine;
if AMakeVisible
then begin
if FSelectLine < OldLine
then begin
if FTopLine > FSelectLine
then SetTopLine(FSelectLine);
end
else begin
if FTopLine + FLineCount <= FSelectLine
then SetTopLine(FSelectLine - FLineCount + 1);
end;
end;
pbAsm.Invalidate;
end;
procedure TAssemblerDlg.SetLineCount(ALineCount: Integer);
begin
if FLineCount = ALineCount
then exit;
FLineCount := ALineCount;
SetLength(FLineMap, FLineCount + 1);
UpdateView;
end;
procedure TAssemblerDlg.SetTopLine(ALine: Integer);
var
PadFront, PadEnd: Integer;
begin
if not ToolButtonPower.Down
then exit;
if FTopLine = ALine then Exit;
// scrolled by user, get more padding lines
PadFront := 5;
PadEnd := 5;
if ALine < FTopLine
then PadFront := 20
else PadEnd := 20;
FTopLine := ALine;
if (FDisassembler <> nil)
and ( (FDisassembler.CountBefore < Max(0, -(FTopLine - 1)))
or (FDisassembler.CountAfter < Max(0, FTopLine + FLineCount + 2)) )
then FDisassembler.PrepareRange(FCurrentLocation, Max(0, -(FTopLine - PadFront)), Max(0, FTopLine + FLineCount + 1 + PadEnd));
UpdateLineData;
end;
function TAssemblerDlg.GetSourceCodeLine(SrcFileName: string; SrcLineNumber: Integer): string;
var
PasSource: TCodeBuffer;
Editor: TSourceEditor;
begin
Result := '';
if SrcLineNumber < 1 then exit;
if not FDebugManager.GetFullFilename(SrcFileName, False) // TODO: maybe ask user?
then exit;
PasSource := CodeToolBoss.LoadFile(SrcFileName, true, false);
if PasSource = nil
then exit;
Editor := SourceEditorManager.SourceEditorIntfWithFilename(SrcFileName);
if Editor <> nil then SrcLineNumber := Editor.DebugToSourceLine(SrcLineNumber);
Result := Trim(PasSource.GetLine(SrcLineNumber - 1));
end;
procedure TAssemblerDlg.UpdateLineData;
begin
UpdateLineDataEx(FLineMap, FTopLine, FLineCount + 1,
FLastTopLine, FLastTopLineIdx, FLastTopLineIsSrc, FLastTopLineValid, True);
end;
procedure TAssemblerDlg.UpdateLineDataEx(ALineMap: TAsmDlgLineEntries; AFirstLine,
ALineCount: Integer; var ACachedLine, ACachedIdx: Integer;
var ACachedIsSrc, ACachedValid: Boolean; ACachedUpdate: Boolean;
ANoExtraHeader: Boolean = False);
function GetItem(AIdx: Integer): PDisassemblerEntry;
begin
Result := nil;
if (AIdx >= -FDisassembler.CountBefore) and (AIdx < FDisassembler.CountAfter)
then Result := FDisassembler.EntriesPtr[AIdx];
end;
function IsSourceBeforeItem(AItm: PDisassemblerEntry;
APrvItm: PDisassemblerEntry): Boolean;
begin
if AItm = nil
then exit(False);
if AItm^.SrcFileName <> '' then begin
Result := AItm^.SrcStatementIndex = 0;
if (not Result) and (APrvItm <> nil)
then Result := (AItm^.SrcFileName <> APrvItm^.SrcFileName)
or (AItm^.SrcFileLine <> APrvItm^.SrcFileLine);
end
else begin
Result := (AItm^.FuncName <> '');
if Result
then Result := (AItm^.Offset = 0)
or ( (APrvItm <> nil) and (AItm^.FuncName <> APrvItm^.FuncName) );
end;
end;
var
DoneLocation: TDBGPtr;
DoneTopLine, DoneLineCount: Integer;
DoneCountBefore, DoneCountAfter: Integer;
Line, Idx: Integer;
Itm, NextItm, PrevItm: PDisassemblerEntry;
LineIsSrc, HasLineOutOfRange: Boolean;
begin
if (FDebugger = nil) or (FDisassembler = nil) or (FDebugger.State <> dsPause)
then begin
ClearLineMap; // set all to lmsUnknown;
exit;
end;
if FDisassembler.BaseAddr <> FCurrentLocation
then begin
ClearLineMap(lmsInvalid);
exit;
end;
if FUpdating
then begin
FUpdateNeeded := True;
Exit;
end;
FUpdating := True;
try
FUpdateNeeded := False;
DoneLocation := FCurrentLocation;
DoneTopLine := AFirstLine;
DoneLineCount := ALineCount;
DoneCountBefore := FDisassembler.CountBefore;
DoneCountAfter := FDisassembler.CountAfter;
// Find Idx for topline
Line := 0;
Idx := 0;
LineIsSrc := False;
if ACachedValid
and (abs(AFirstLine - ACachedLine) < AFirstLine)
then begin
Line := ACachedLine;
Idx := ACachedIdx;
LineIsSrc := ACachedIsSrc;
end;
Itm := GetItem(Idx);
NextItm := GetItem(Idx + 1);
while AFirstLine > Line
do begin
NextItm := GetItem(Idx+1);
if LineIsSrc
then begin
LineIsSrc := False;
end
else if IsSourceBeforeItem(NextItm, Itm)
then begin
inc(Idx);
Itm := NextItm;
NextItm := GetItem(Idx + 1);
LineIsSrc := True;
end
else begin
inc(Idx);
Itm := NextItm;
NextItm := GetItem(Idx + 1);
end;
inc(Line);
end;
Itm := GetItem(Idx);
PrevItm := GetItem(Idx - 1);
while AFirstLine < line
do begin
if LineIsSrc
then begin
dec(Idx);
Itm := PrevItm;
PrevItm := GetItem(Idx - 1);
LineIsSrc := False;
end
else if IsSourceBeforeItem(Itm, PrevItm)
then begin
LineIsSrc := True;
end
else begin
dec(Idx);
Itm := PrevItm;
PrevItm := GetItem(Idx - 1);
end;
Dec(Line);
end;
if ACachedUpdate
then begin
ACachedLine := AFirstLine;
ACachedIdx := Idx;
ACachedIsSrc := LineIsSrc;
ACachedValid := True;
end;
// Fill LineMap
HasLineOutOfRange := False;
Line := 0;
PrevItm := GetItem(Idx - 1);
NextItm := GetItem(Idx);
while Line < ALineCount do begin
PrevItm := Itm;
Itm := NextItm;
NextItm := GetItem(Idx+1);
if Itm = nil
then begin
ALineMap[Line].State := lmsInvalid;
HasLineOutOfRange := True;
inc(Line);
inc(idx);
continue;
end;
if ( (Line = 0) and LineIsSrc )
or ( (Line <> 0) and IsSourceBeforeItem(Itm, PrevItm) )
then begin
ALineMap[Line].Dump := '';
ALineMap[Line].Statement := '';
if Itm^.SrcFileName <> ''
then begin
ALineMap[Line].State := lmsSource;
ALineMap[Line].SourceLine := Itm^.SrcFileLine;
ALineMap[Line].FileName := Itm^.SrcFileName;
ALineMap[Line].PasCode := GetSourceCodeLine(Itm^.SrcFileName, Itm^.SrcFileLine);
end
else begin
ALineMap[Line].State := lmsFuncName;
ALineMap[Line].SourceLine := Itm^.Offset;
ALineMap[Line].FileName := Itm^.FuncName;
end;
inc(Line);
end
else
if (Line = 0) and (not ANoExtraHeader) // but it's not LineIsSrc
and ( ( (Itm^.SrcFileName <> '') and (Itm^.SrcStatementIndex <> Itm^.SrcStatementCount-1) )
or ( (Itm^.SrcFileName = '') and (Itm^.FuncName <> '') and (NextItm <> nil) and (Itm^.Offset < NextItm^.Offset) )
)
then begin
ALineMap[Line].Dump := '';
ALineMap[Line].Statement := '';
if Itm^.SrcFileName <> ''
then begin
ALineMap[Line].State := lmsSource;
ALineMap[Line].SourceLine := Itm^.SrcFileLine;
ALineMap[Line].FileName := Itm^.SrcFileName;
if NextItm <> nil
then ALineMap[Line].Statement := Format('(%d of %d)', [NextItm^.SrcStatementIndex, NextItm^.SrcStatementCount])
else ALineMap[Line].Statement := Format('(??? of %d)', [Itm^.SrcStatementCount]);
ALineMap[Line].PasCode := GetSourceCodeLine(Itm^.SrcFileName, Itm^.SrcFileLine);
end
else begin
ALineMap[Line].State := lmsFuncName;
ALineMap[Line].SourceLine := 0;
if NextItm <> nil
then ALineMap[Line].SourceLine := NextItm^.Offset;
ALineMap[Line].FileName := Itm^.FuncName;
if NextItm <> nil
then ALineMap[Line].Statement := Format('(%d)', [NextItm^.Offset])
else ALineMap[Line].Statement := '(???)';
end;
inc(Line);
inc(idx); // displayed source-info, instead of asm (topline substituted)
LineIsSrc := False;
continue;
end;
LineIsSrc := False; // only for topline
if Line >= ALineCount
then break;
ALineMap[Line].Addr := Itm^.Addr;
ALineMap[Line].State := lmsStatement;
ALineMap[Line].Dump := Itm^.Dump;
ALineMap[Line].Statement := Itm^.Statement;
ALineMap[Line].SourceLine := Itm^.SrcFileLine;
inc(Line);
inc(idx);
end;
finally
FUpdating := False;
if FUpdateNeeded
and ( (DoneLocation <> FCurrentLocation)
or (DoneTopLine <> AFirstLine)
or (DoneLineCount <> ALineCount)
or (HasLineOutOfRange
and ( (DoneCountBefore <> FDisassembler.CountBefore)
or (DoneCountAfter <> FDisassembler.CountAfter) ) )
)
then UpdateLineData;
end;
end;
end.
|