Menu

[327ada]: / Source / DebugReader.pas  Maximize  Restore  History

Download this file

972 lines (804 with data), 26.9 kB

  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
{
This file is part of Dev-C++
Copyright (c) 2004 Bloodshed Software
Dev-C++ is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Dev-C++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dev-C++; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
unit DebugReader;
interface
uses
Sysutils, Classes, Windows, StdCtrls,
Dialogs, editor, ComCtrls, StrUtils, Forms;
type
TAnnotateType = (
TPrePrompt, TPrompt, TPostPrompt,
TSource,
TDisplayBegin, TDisplayEnd,
TDisplayExpression,
TFrameSourceFile, TFrameSourceBegin, TFrameSourceLine, TFrameFunctionName, TFrameWhere,
TFrameArgs,
TFrameBegin, TFrameEnd,
TErrorBegin, TErrorEnd,
TArrayBegin, TArrayEnd,
TElt, TEltRep, TEltRepEnd,
TExit,
TSignal, TSignalName, TSignalNameEnd, TSignalString, TSignalStringEnd,
TValueHistoryValue, TValueHistoryBegin, TValueHistoryEnd,
TArgBegin, TArgEnd, TArgValue, TArgNameEnd,
TFieldBegin, TFieldEnd, TFieldValue, TFieldNameEnd,
TInfoReg, TInfoAsm,
TUnknown, TEOF);
PWatchVar = ^TWatchVar;
TWatchVar = record
Name: String;
gdbindex: integer;
Node: TTreeNode;
end;
PBreakPoint = ^TBreakPoint;
TBreakPoint = record
line: integer;
editor: TEditor;
end;
PTrace = ^TTrace;
TTrace = record
funcname: String;
filename: String;
line: String;
end;
PRegister = ^TRegister;
TRegister = record
name: String;
valuehex: String;
valuedec: String;
end;
TDebugReader = class(TThread)
private
fPipeRead: THandle;
fRegisters: TList;
fDisassembly: TStringList; // convert to TList with proper data formatting?
fBacktrace: TList;
fBreakpointList: TList;
fWatchVarList: TList; // contains all parents
fDebugView: TTreeView;
fIndex: integer;
fBreakPointLine: integer;
fBreakPointFile: String;
fOutput: String;
fEvalValue: String;
fSignal: String;
// attempt to cut down on Synchronize calls
dobacktraceready: boolean;
dodisassemblerready: boolean;
doregistersready: boolean;
dorescanwatches: boolean;
doevalready: boolean;
doprocessexited: boolean;
doupdatecpuwindow: boolean;
doupdateexecution: boolean;
doreceivedsignal: boolean;
doreceivedsfwarning: boolean;
// Signal handlers
procedure HandleValueHistoryValue;
procedure HandleSignal;
procedure HandleExit;
procedure HandleFrames;
procedure HandleDisassembly;
procedure HandleRegisters;
procedure HandleError;
procedure HandleDisplay;
procedure HandleSource;
// Evaluation tree output handlers
procedure ProcessWatchOutput(WatchVar: PWatchVar);
function ProcessEvalOutput: String;
procedure ProcessDebugOutput;
// synching with GUI
procedure SyncFinishedParsing;
// parsing
procedure SkipSpaces; // skips space and tab
procedure SkipToAnnotation; // skips until it finds #26#26 (GDB annotation for interfaces)
function FindAnnotation(Annotation: TAnnotateType): boolean; // Finds the given annotation, returns false on EOF
function GetNextAnnotation: TAnnotateType; // Returns the next annotation
function GetLastAnnotation(const text: TBytes; curpos, len: integer): TAnnotateType;
// Returns the last annotation in given string
function PeekNextAnnotation: TAnnotateType;
// Returns the next annotation, but does not modify current scanning positions
function GetNextWord: String; // copies the next word, stops when it finds chars 0..32
function GetNextLine: String;
// skips until enter sequence, skips ONE enter sequence, copies until next enter sequence
function GetNextFilledLine: String;
// skips until enter sequence, skips enter sequences, copies until next enter sequence
function GetRemainingLine: String; // copies until enter sequence
function GetAnnotation(const s: String): TAnnotateType; // converts string to TAnnotateType
protected
procedure Execute; override;
public
property Registers: TList read fRegisters write fRegisters;
property Disassembly: TStringList read fDisassembly write fDisassembly;
property Backtrace: TList read fBacktrace write fBacktrace;
property PipeRead: THandle read fPipeRead write fPipeRead;
property BreakPointList: TList read fBreakPointList write fBreakPointList;
property WatchVarList: TList read fWatchVarList write fWatchVarList;
property DebugView: TTreeView read fDebugView write fDebugView;
property BreakPointFile: String read fBreakPointFile;
end;
implementation
uses
System.UItypes, main, devcfg, CPUFrm, multilangsupport, debugger, utils, Controls, Math;
// macro for all the things that need to be done when we are finished parsing the current block
procedure TDebugReader.SyncFinishedParsing;
var
SignalDialog: TForm;
SignalCheck: TCheckBox;
spawnedcpuform: boolean;
begin
spawnedcpuform := false;
// GDB determined that the source code is more recent than the executable. Ask the user if he wants to rebuild.
if doreceivedsfwarning then begin
if MessageDlg(Lang[ID_MSG_SOURCEMORERECENT], mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
MainForm.Debugger.Stop;
MainForm.actCompileExecute(nil);
Exit;
end;
end;
// The program to debug has stopped. Stop the debugger
if doprocessexited then begin
MainForm.Debugger.Stop;
Exit;
end;
// An evaluation variable has been processed. Forward the results
if doevalready and Assigned(MainForm.Debugger.OnEvalReady) then
MainForm.Debugger.OnEvalReady(fEvalValue);
// Delete unimportant stuff to reduce clutter
fOutput := StringReplace(fOutput, #26, '->', [rfReplaceAll]);
MainForm.DebugOutput.Lines.Add(fOutput);
// Some part of the CPU form has been updated
if Assigned(CPUForm) and not doreceivedsignal then begin
if doregistersready then
CPUForm.OnRegistersReady;
if dodisassemblerready then
CPUForm.OnAssemblerReady;
if dobacktraceready then
CPUForm.OnBacktraceReady;
end;
if doupdateexecution then begin
MainForm.GotoBreakpoint(fBreakPointFile, fBreakPointLine); // set active line
MainForm.Debugger.RefreshWatchVars; // update variable information
end;
if doreceivedsignal then begin
SignalDialog := CreateMessageDialog(fSignal, mtError, [mbOk]);
SignalCheck := TCheckBox.Create(SignalDialog);
// Display it on top of everything
SignalDialog.FormStyle := fsStayOnTop;
SignalDialog.Height := 150;
with SignalCheck do begin
Parent := SignalDialog;
Caption := 'Show CPU window';
Top := Parent.ClientHeight - 22;
Left := 8;
Width := Parent.ClientWidth - 16;
Checked := devData.ShowCPUSignal;
end;
MessageBeep(MB_ICONERROR);
if SignalDialog.ShowModal = ID_OK then begin
devData.ShowCPUSignal := SignalCheck.Checked;
if SignalCheck.Checked and not Assigned(CPUForm) then begin
MainForm.ViewCPUItemClick(nil);
spawnedcpuform := true;
end;
end;
SignalDialog.Free;
end;
// CPU form updates itself when spawned, don't update twice!
if (doupdatecpuwindow and not spawnedcpuform) and Assigned(CPUForm) then begin
MainForm.Debugger.SendCommand('disas', '');
MainForm.Debugger.SendCommand('info registers', '');
MainForm.Debugger.SendCommand('backtrace', '');
end;
end;
procedure TDebugReader.SkipSpaces;
begin
while CharInSet(fOutput[fIndex], [#9, #32]) do
Inc(fIndex);
end;
procedure TDebugReader.SkipToAnnotation;
begin
// Walk up to the next annotation
while not CharInSet(fOutput[fIndex], [#26, #0]) do
Inc(fIndex);
// Crawl through the remaining ->'s
while CharInSet(fOutput[fIndex], [#26]) do
Inc(fIndex);
end;
function TDebugReader.FindAnnotation(Annotation: TAnnotateType): boolean;
var
NextAnnotation: TAnnotateType;
begin
result := False;
repeat
NextAnnotation := GetNextAnnotation;
if NextAnnotation = TEOF then
Exit; // return false
until NextAnnotation = Annotation;
result := True;
end;
function TDebugReader.GetNextWord: String;
begin
Result := '';
// Called when at a space? Skip over
SkipSpaces;
// Skip until a space
while not CharInSet(fOutput[fIndex], [#0..#32]) do begin
Result := Result + fOutput[fIndex];
Inc(fIndex);
end;
end;
function TDebugReader.GetRemainingLine: String;
begin
Result := '';
// Return part of line still ahead of us
while not CharInSet(fOutput[fIndex], [#13, #10, #0]) do begin
Result := Result + fOutput[fIndex];
Inc(fIndex);
end;
end;
function TDebugReader.GetNextLine: String;
begin
Result := '';
// Walk up to an enter sequence
while not CharInSet(fOutput[fIndex], [#13, #10, #0]) do
Inc(fIndex);
// End of output. Exit
if fOutput[fIndex] = #0 then
Exit;
// Skip ONE enter sequence (CRLF, CR, LF, etc.)
if (fOutput[fIndex] = #13) and (fOutput[fIndex + 1] = #10) then // DOS
Inc(fIndex, 2)
else if fOutput[fIndex] = #13 then // UNIX
Inc(fIndex)
else if fOutput[fIndex] = #10 then // MAC
Inc(fIndex);
// Return next line
Result := GetRemainingLine;
end;
function TDebugReader.GetNextFilledLine: String;
begin
Result := '';
// Walk up to an enter sequence
while not CharInSet(fOutput[fIndex], [#13, #10, #0]) do
Inc(fIndex);
// Skip enter sequences (CRLF, CR, LF, etc.)
while CharInSet(fOutput[fIndex], [#13, #10, #0]) do
Inc(fIndex);
// Return next line
Result := GetRemainingLine;
end;
function TDebugReader.PeekNextAnnotation: TAnnotateType;
var
IndexBackup: integer;
begin
IndexBackup := fIndex; // do NOT modifiy curpos
Result := GetNextAnnotation;
fIndex := IndexBackup;
end;
function TDebugReader.GetLastAnnotation(const text: TBytes; curpos, len: integer): TAnnotateType;
var
s: String;
begin
// Walk back until end of #26's
while (curpos > 0) and (text[curpos] <> 26) do
Dec(curpos);
Inc(curpos);
// Tiny rewrite of GetNextWord for special purposes
s := '';
while (curpos <= len) and not (text[curpos] in [0..32]) do begin
s := s + Chr(text[curpos]);
Inc(curpos);
end;
Result := GetAnnotation(s);
end;
function TDebugReader.GetNextAnnotation: TAnnotateType;
begin
// Skip until end of #26's, i.e. GDB formatted output
SkipToAnnotation;
// Get part this line, after #26#26
Result := GetAnnotation(GetNextWord);
end;
function TDebugReader.GetAnnotation(const s: String): TAnnotateType;
var
IndexBackup: integer;
t: String;
begin
if SameStr(s, 'pre-prompt') then
result := TPrePrompt
else if SameStr(s, 'prompt') then
result := TPrompt
else if SameStr(s, 'post-prompt') then begin
result := TPostPrompt;
IndexBackup := fIndex;
t := GetNextFilledLine;
fIndex := IndexBackup;
// Hack fix to catch register dump
if Assigned(fRegisters) then
if StartsStr('rax ', t) or StartsStr('eax ', t) then
result := TInfoReg;
// Another hack to catch assembler
if Assigned(fDisassembly) then
if StartsStr('Dump of assembler code for function ', t) then
result := TInfoAsm;
end else if SameStr(s, 'error-begin') then
result := TErrorBegin
else if SameStr(s, 'error-end') then
result := TErrorEnd
else if SameStr(s, 'display-begin') then
result := TDisplayBegin
else if SameStr(s, 'display-expression') then
result := TDisplayExpression
else if SameStr(s, 'display-end') then
result := TDisplayEnd
else if SameStr(s, 'frame-source-begin') then
result := TFrameSourceBegin
else if SameStr(s, 'frame-source-file') then
result := TFrameSourceFile
else if SameStr(s, 'frame-source-line') then
result := TFrameSourceLine
else if SameStr(s, 'frame-function-name') then
result := TFrameFunctionName
else if SameStr(s, 'frame-args') then
result := TFrameArgs
else if SameStr(s, 'frame-begin') then
result := TFrameBegin
else if SameStr(s, 'frame-end') then
result := TFrameEnd
else if SameStr(s, 'frame-where') then
result := TFrameWhere
else if SameStr(s, 'source') then
result := TSource
else if SameStr(s, 'exited') then
result := TExit
else if SameStr(s, 'arg-begin') then
result := TArgBegin
else if SameStr(s, 'arg-name-end') then
result := TArgNameEnd
else if SameStr(s, 'arg-value') then
result := TArgValue
else if SameStr(s, 'arg-end') then
result := TArgEnd
else if SameStr(s, 'array-section-begin') then
result := TArrayBegin
else if SameStr(s, 'array-section-end') then
result := TArrayEnd
else if SameStr(s, 'elt') then
result := TElt
else if SameStr(s, 'elt-rep') then
result := TEltRep
else if SameStr(s, 'elt-rep-end') then
result := TEltRepEnd
else if SameStr(s, 'field-begin') then
result := TFieldBegin
else if SameStr(s, 'field-name-end') then
result := TFieldNameEnd
else if SameStr(s, 'field-value') then
result := TFieldValue
else if SameStr(s, 'field-end') then
result := TFieldEnd
else if SameStr(s, 'value-history-value') then
result := TValueHistoryValue
else if SameStr(s, 'value-history-begin') then
result := TValueHistoryBegin
else if SameStr(s, 'value-history-end') then
result := TValueHistoryEnd
else if SameStr(s, 'signal') then
result := TSignal
else if SameStr(s, 'signal-name') then
result := TSignalName
else if SameStr(s, 'signal-name-end') then
result := TSignalNameEnd
else if SameStr(s, 'signal-string') then
result := TSignalString
else if SameStr(s, 'signal-string-end') then
result := TSignalStringEnd
else if (fOutput[fIndex] = #0) then
result := TEOF
else
result := TUnknown;
end;
procedure TDebugReader.ProcessWatchOutput(WatchVar: PWatchVar);
var
S, NodeText: String;
ParentNode: TTreeNode;
ParentWasExpanded: boolean;
I: integer;
sl: TStringList;
begin
// Expand if it was expanded or if it didn't have any children
ParentWasExpanded := WatchVar^.Node.Expanded or not WatchVar^.Node.HasChildren;
// Do not remove root node of watch variable
WatchVar^.Node.DeleteChildren;
ParentNode := WatchVar^.Node;
ParentNode.Text := '';
// Process output parsed by ProcessEvalStruct
S := WatchVar^.Name + ' = ' + ProcessEvalOutput;
// add placeholder name for variable name so we can format structs using one rule
// Add children based on indent
sl := TStringList.Create;
try
sl.Text := S;
for I := 0 to sl.Count - 1 do begin
// Format node text. Remove trailing comma
NodeText := Trim(sl[i]);
if EndsStr(',', NodeText) then
Delete(NodeText, Length(NodeText), 1);
if EndsStr('{', NodeText) then begin // new member struct
if ParentNode.Text = '' then // root node, replace text only
ParentNode.Text := NodeText
else
ParentNode := DebugView.Items.AddChild(ParentNode, NodeText);
end else if StartsStr('}', NodeText) then begin // end of struct, change parent
ParentNode := ParentNode.Parent;
end else begin // next parent member/child
if ParentNode.Text = '' then // root node, replace text only
ParentNode.Text := NodeText
else
DebugView.Items.AddChild(ParentNode, NodeText);
end;
end;
finally
sl.Free;
end;
// TODO: remember expansion state
if ParentWasExpanded then
WatchVar^.Node.Expand(false);
end;
function TDebugReader.ProcessEvalOutput: String;
var
indent: integer;
NextLine: String;
NextAnnotation: TAnnotateType;
begin
indent := 0;
// First line gets special treatment
Result := GetNextLine;
if StartsStr('{', Result) then
Inc(indent, 4);
// Collect all data, add formatting in between
repeat
NextAnnotation := GetNextAnnotation;
NextLine := GetNextLine;
case NextAnnotation of
// Change indent if { or } is found
TFieldBegin: begin
Result := Result + #13#10 + StringOfChar(' ', indent);
end;
TFieldValue: begin
if StartsStr('{', NextLine) and (PeekNextAnnotation <> TArrayBegin) then
Inc(indent, 4);
end;
TFieldEnd: begin
if EndsStr('}', NextLine) then begin
Dec(indent, 4);
Result := Result + #13#10 + StringOfChar(' ', indent);
end;
end;
end;
Result := Result + NextLine;
until NextAnnotation in [TEOF, TValueHistoryEnd, TDisplayEnd];
end;
procedure TDebugReader.HandleValueHistoryValue;
begin
fEvalValue := ProcessEvalOutput;
doevalready := true;
end;
procedure TDebugReader.HandleSignal;
begin
fSignal := GetNextFilledLine; // Program received signal
if not FindAnnotation(TSignalName) then
Exit;
fSignal := fSignal + GetNextFilledLine; // signal code
if not FindAnnotation(TSignalNameEnd) then
Exit;
fSignal := fSignal + GetNextFilledLine; // comma
if not FindAnnotation(TSignalString) then
Exit;
fSignal := fSignal + GetNextFilledLine; // user friendly description
if not FindAnnotation(TSignalStringEnd) then
Exit;
fSignal := fSignal + GetNextFilledLine; // period
doreceivedsignal := true;
end;
procedure TDebugReader.HandleExit;
begin
doprocessexited := true;
end;
procedure TDebugReader.HandleFrames;
var
s: String;
trace: PTrace;
begin
s := GetNextLine;
// Is this a backtrace dump?
if Assigned(fBacktrace) and StartsStr('#', s) then begin
trace := new(PTrace);
// Find function name
if not FindAnnotation(TFrameFunctionName) then begin
Dispose(PTrace(trace));
Exit;
end;
trace^.funcname := GetNextLine;
// Find argument list start
if not FindAnnotation(TFrameArgs) then begin
Dispose(PTrace(trace));
Exit;
end;
// Arguments are either () or detailed list
s := GetNextLine;
while (PeekNextAnnotation = TArgBegin) do begin
// argument name
if not FindAnnotation(TArgBegin) then begin
Dispose(PTrace(trace));
Exit;
end;
s := s + GetNextLine;
// =
if not FindAnnotation(TArgNameEnd) then begin
Dispose(PTrace(trace));
Exit;
end;
s := s + ' ' + GetNextLine + ' '; // should be =
// argument value
if not FindAnnotation(TArgValue) then begin
Dispose(PTrace(trace));
Exit;
end;
s := s + GetNextLine;
// argument end
if not FindAnnotation(TArgEnd) then begin
Dispose(PTrace(trace));
Exit;
end;
s := s + GetNextLine;
end;
trace^.funcname := trace^.funcname + Trim(s);
// If source info can't be found, skip
if PeekNextAnnotation = TFrameSourceBegin then begin
// Find filename
if not FindAnnotation(TFrameSourceFile) then begin
Dispose(PTrace(trace));
Exit;
end;
trace^.filename := GetNextLine;
// find line
if not FindAnnotation(TFrameSourceLine) then begin
Dispose(PTrace(trace));
Exit;
end;
trace^.line := GetNextLine;
end else begin
trace^.filename := '';
trace^.line := '';
end;
fBacktrace.Add(trace);
// Skip over the remaining frame part...
if not FindAnnotation(TFrameEnd) then
Exit;
// Not another one coming? Done!
if PeekNextAnnotation <> TFrameBegin then begin
// End of stack trace dump!
dobacktraceready := true;
end;
end else
doupdatecpuwindow := true;
end;
procedure TDebugReader.HandleDisassembly;
var
s: String;
begin
if not Assigned(fDisassembly) then
Exit;
// Get info message
s := GetNextLine;
// the full function name will be saved at index 0
fDisassembly.Add(Copy(s, 37, Length(s) - 37));
s := GetNextLine;
// Add lines of disassembly
while not SameStr('End of assembler dump.', s) and not SameStr(s, '') do begin
fDisassembly.Add(s);
s := GetNextLine;
end;
dodisassemblerready := true;
end;
procedure TDebugReader.HandleRegisters;
var
s: String;
reg: PRegister;
x: integer;
begin
if not Assigned(fRegisters) then
Exit;
// name(spaces)hexvalue(tab)decimalvalue
s := GetNextFilledLine;
repeat
reg := new(PRegister);
// Cut name from 1 to first space
x := Pos(' ', s);
reg^.name := Copy(s, 1, x - 1);
Delete(s, 1, x - 1);
// Remove spaces
s := TrimLeft(s);
// Cut hex value from 1 to first tab
x := Pos(' ', s); // was #9
reg^.valuehex := Copy(s, 1, x - 1);
Delete(s, 1, x); // delete tab too // space not tab, but still ok
s := TrimLeft(s);
// Remaining part contains decimal value
reg^.valuedec := s;
fRegisters.Add(reg);
s := GetNextLine;
until SameStr('', s);
doregistersready := true;
end;
procedure TDebugReader.HandleError;
var
s, WatchName: String;
Tail, Head, I: integer;
WatchVar: PWatchVar;
begin
s := GetNextLine; // error text
if StartsStr('No symbol "', s) then begin
Tail := Pos('"', s);
Head := RPos('"', s);
WatchName := Copy(s, Tail + 1, Head - Tail - 1);
// Update current...
for I := 0 to WatchVarList.Count - 1 do begin
WatchVar := PWatchVar(WatchVarList.Items[I]);
if SameStr(WatchVar^.name, WatchName) then begin
WatchVar^.Node.Text := WatchVar^.Name + ' = Not found in current context';
// Delete now invalid children
WatchVar^.Node.DeleteChildren;
dorescanwatches := true;
break;
end;
end;
end;
end;
procedure TDebugReader.HandleDisplay;
var
s, WatchName: String;
I: integer;
WatchVar: PWatchVar;
begin
s := GetNextLine; // watch index
if not FindAnnotation(TDisplayExpression) then
Exit;
WatchName := GetNextLine; // watch name
// Find parent we're talking about
for I := 0 to WatchVarList.Count - 1 do begin
WatchVar := PWatchVar(WatchVarList.Items[I]);
if SameStr(WatchVar^.name, WatchName) then begin
// Advance up to the value
if not FindAnnotation(TDisplayExpression) then
Exit;
// Refresh GDB index so we can undisplay this by index
WatchVar^.gdbindex := StrToInt(s);
// Refresh members...
ProcessWatchOutput(WatchVar);
break;
end;
end;
end;
procedure TDebugReader.HandleSource;
var
s: String;
DelimPos, I: integer;
begin
// source filename:line:offset:beg/middle/end:addr
s := TrimLeft(GetRemainingLine);
// remove offset, beg/middle/end, address
for I := 1 to 3 do begin
DelimPos := RPos(':', s);
if DelimPos > 0 then
Delete(s, DelimPos, MaxInt)
else
Exit; // Wrong format. Don't bother to continue
end;
// get line
DelimPos := RPos(':', s);
if DelimPos > 0 then begin
fBreakPointLine := StrToIntDef(Copy(s, DelimPos + 1, MaxInt), 1);
Delete(s, DelimPos, MaxInt);
end;
// get file
fBreakPointFile := s;
doupdateexecution := true;
doupdatecpuwindow := true;
end;
procedure TDebugReader.ProcessDebugOutput;
var
NextAnnotation: TAnnotateType;
begin
// Only update once per update at most
DebugView.Items.BeginUpdate;
try
dobacktraceready := false;
dodisassemblerready := false;
doregistersready := false;
dorescanwatches := false;
doevalready := false;
doprocessexited := false;
doupdateexecution := false;
doreceivedsignal := false;
doupdatecpuwindow := false;
doreceivedsfwarning := false;
// Global checks
if Pos('warning: Source file is more recent than executable.', fOutput) > 0 then
doreceivedsfwarning := true;
fIndex := 1;
repeat
NextAnnotation := GetNextAnnotation;
case NextAnnotation of
TValueHistoryValue:
HandleValueHistoryValue;
TSignal:
HandleSignal;
TExit:
HandleExit;
TFrameBegin:
HandleFrames;
TInfoAsm:
HandleDisassembly;
TInfoReg:
HandleRegisters;
TErrorBegin:
HandleError;
TDisplayBegin:
HandleDisplay;
TSource:
HandleSource
//else
// break;
end;
until NextAnnotation = TEOF;
// Only update once per update at most
finally
DebugView.Items.EndUpdate;
end;
Synchronize(SyncFinishedParsing);
end;
procedure TDebugReader.Execute;
var
tmp: TBytes;
bytesread, totalbytesread: DWORD;
const
chunklen = 1000; // GDB usually sends 4K blocks, disassembly easily takes up to 20K
begin
bytesread := 0;
totalbytesread := 0;
while not Terminated do begin
// Add chunklen bytes to length, and set chunklen extra bytes to zero
SetLength(tmp, totalbytesread + chunklen);
FillChar(tmp[totalbytesread], chunklen, 0);
// ReadFile returns when there's something to read
if not ReadFile(fPipeRead, (@tmp[totalbytesread])^, chunklen, bytesread, nil) or (bytesread = 0) then
break;
Inc(totalbytesread, bytesread);
if not Terminated then begin
// Assume fragments don't end nicely with TErrorBegin or TPrompt
fOutput := TEncoding.Default.GetString(tmp);
if GetLastAnnotation(Tmp, totalbytesread, totalbytesread + chunklen) in [TErrorBegin, TPrompt] then begin
fOutput := TEncoding.Default.GetString(tmp);
ProcessDebugOutput;
// Reset storage
totalbytesread := 0;
end;
end;
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.