forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDEDebugger.pb
1733 lines (1401 loc) · 62.2 KB
/
IDEDebugger.pb
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
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
; shortcuts that are added to every debugger window
;
Enumeration #DEBUGGER_MENU_LAST ; add after the debuggers own shortcuts
#MENU_Debugger_Stop
#MENU_Debugger_Run
#MENU_Debugger_Step
#MENU_Debugger_StepX
#MENU_Debugger_StepOut
#MENU_Debugger_StepOver
#MENU_Debugger_Kill
#MENU_Debugger_DebugOutput
#MENU_Debugger_Watchlist
#MENU_Debugger_VariableList
#MENU_Debugger_History
#MENU_Debugger_Memory
#MENU_Debugger_LibraryViewer
#MENU_Debugger_DebugAsm
#MENU_Debugger_DataBreakpoints
;#MENU_Debugger_CPUMonitor
EndEnumeration
Procedure FindDebuggerFromID(ID)
If ID ; 0 is the "invalid ID"
ForEach RunningDebuggers()
If ID = RunningDebuggers()\ID
ProcedureReturn @RunningDebuggers()
EndIf
Next RunningDebuggers()
EndIf
ProcedureReturn 0
EndProcedure
; find out if the given file is active (included) in a debug session
; For Project files which are not included in the debugged source, this returns nothing
;
Procedure IsDebuggedFile(*Source.SourceFile)
; no source passed (not loaded yet maybe?)
;
If *Source = 0
ProcedureReturn 0
EndIf
; Get the debugger ID (if any)
;
If *Source\DebuggerID
*Debugger = FindDebuggerFromID(*Source\DebuggerID)
If *Debugger
ProcedureReturn *Debugger
EndIf
EndIf
; The source is not directly linked to a debugger, so check all debugger file lists
;
ForEach RunningDebuggers()
If IsEqualFile(*Source\FileName$, RunningDebuggers()\FileName$)
ProcedureReturn @RunningDebuggers() ; return the debugger structure
EndIf
If RunningDebuggers()\IncludedFiles ; is filename buffer initialized
*Cursor = RunningDebuggers()\IncludedFiles
*Cursor + MemoryAsciiLength(*Cursor) + 1 ; skip the source path string
*Cursor + MemoryAsciiLength(*Cursor) + 1 ; skip the main source name (checked above)
For i = 1 To RunningDebuggers()\NbIncludedFiles ; check all included files
; the included filenames may include "../" so use ResolveRelativePath() on them, which resolves that to get a unique filename
FileName$ = UniqueFilename(PeekAscii(*Cursor))
If IsEqualFile(*Source\FileName$, FileName$)
ProcedureReturn @RunningDebuggers() ; return the debugger structure
EndIf
*Cursor + MemoryAsciiLength(*Cursor) + 1
Next i
EndIf
Next RunningDebuggers()
; No debugger found
;
ProcedureReturn 0
EndProcedure
; Returns the debugger that is associated with this sourcefile
; For Projects, this returns the "Project Debugger" for all project files
;
Procedure GetDebuggerForFile(*Source.SourceFile)
; This function is called during IDE startup when no files are loaded, so handle this case
If *Source = 0
ProcedureReturn 0
EndIf
If (*Source = *ProjectInfo Or *Source\ProjectFile) And ProjectDebuggerID
*Debugger = FindDebuggerFromID(ProjectDebuggerID)
If *Debugger
ProcedureReturn *Debugger
EndIf
EndIf
; The file is not in a project with debugger, so do a normal search
ProcedureReturn IsDebuggedFile(*Source)
EndProcedure
Procedure GetDebuggerFileNumber(*Debugger.DebuggerData, *Source.SourceFile)
If *Source\FileName$ = "" ; can only be the main source
If IsDebuggedFile(*Source) = *Debugger
ProcedureReturn 0
EndIf
Else
If IsEqualFile(*Source\FileName$, *Debugger\FileName$)
ProcedureReturn 0
EndIf
If *Debugger\IncludedFiles ; is filename buffer initialized
*Cursor = *Debugger\IncludedFiles
*Cursor + MemoryAsciiLength(*Cursor) + 1 ; skip the source path string
*Cursor + MemoryAsciiLength(*Cursor) + 1 ; skip the main source name (checked above)
For i = 1 To *Debugger\NbIncludedFiles ; check all included files
If IsEqualFile(*Source\FileName$, PeekAscii(*Cursor))
ProcedureReturn i ; return the file number
EndIf
*Cursor + MemoryAsciiLength(*Cursor) + 1
Next i
EndIf
EndIf
ProcedureReturn -1 ; file not in this debugger!
EndProcedure
Procedure UpdateErrorLogMenuState()
If *ActiveSource And AlwaysHideLog = 0
DisableMenuItem(#MENU, #MENU_ShowLog, 0) ; the log is always viewable, as it displays compiler errors too
If *ActiveSource = *ProjectInfo Or *ActiveSource\ProjectFile
State = ProjectShowLog
Size = ListSize(ProjectLog())
Else
State = *ActiveSource\ErrorLog
Size = *ActiveSource\LogSize
EndIf
If State And Size > 0
DisableMenuAndToolbarItem(#MENU_ClearLog, 0)
DisableMenuAndToolbarItem(#MENU_CopyLog, 0)
Else
DisableMenuAndToolbarItem(#MENU_ClearLog, 1)
DisableMenuAndToolbarItem(#MENU_CopyLog, 1)
EndIf
Else
DisableMenuItem(#MENU, #MENU_ShowLog, 1)
DisableMenuAndToolbarItem(#MENU_ClearLog, 1)
DisableMenuAndToolbarItem(#MENU_CopyLog, 1)
EndIf
EndProcedure
Procedure SetDebuggerMenuStates()
IsEnabled = 0
*Target.CompileTarget = GetActiveCompileTarget()
If *Target
IsEnabled = *Target\Debugger
EndIf
If *ActiveSource And (*ActiveSource = *ProjectInfo Or *ActiveSource\ProjectFile Or *ActiveSource\IsCode)
NonPBFile = 0
Else
NonPBFile = 1
EndIf
If IsToolBar(#TOOLBAR)
SetToolBarButtonState(#TOOLBAR, #MENU_Debugger, IsEnabled)
EndIf
SetMenuItemState(#MENU, #MENU_Debugger, IsEnabled)
*Debugger.DebuggerData = GetDebuggerForFile(*ActiveSource)
If *Debugger Or IsEnabled
If *Debugger And *Debugger\ProgramState <> -1
Select *Debugger\ProgramState
Case -2 ; means running in step mode
DisableMenuAndToolbarItem(#MENU_Stop, 0)
DisableMenuAndToolbarItem(#MENU_Run, 1)
DisableMenuAndToolbarItem(#MENU_Step, 1)
DisableMenuAndToolbarItem(#MENU_StepX, 1)
DisableMenuAndToolbarItem(#MENU_StepOver, 1)
DisableMenuAndToolbarItem(#MENU_StepOut, 1)
Case 0 ; running
DisableMenuAndToolbarItem(#MENU_Stop, 0)
DisableMenuAndToolbarItem(#MENU_Run, 1)
DisableMenuAndToolbarItem(#MENU_Step, 1)
DisableMenuAndToolbarItem(#MENU_StepX, 1)
DisableMenuAndToolbarItem(#MENU_StepOver, 1)
DisableMenuAndToolbarItem(#MENU_StepOut, 1)
Case 6 ; fatal error (cannot continue)
DisableMenuAndToolbarItem(#MENU_Stop, 1)
DisableMenuAndToolbarItem(#MENU_Run, 1)
DisableMenuAndToolbarItem(#MENU_Step, 1)
DisableMenuAndToolbarItem(#MENU_StepX, 1)
DisableMenuAndToolbarItem(#MENU_StepOver, 1)
DisableMenuAndToolbarItem(#MENU_StepOut, 1)
Case 5 ; program ended
DisableMenuAndToolbarItem(#MENU_Stop, 1)
DisableMenuAndToolbarItem(#MENU_Run, 1)
DisableMenuAndToolbarItem(#MENU_Step, 1)
DisableMenuAndToolbarItem(#MENU_StepX, 1)
DisableMenuAndToolbarItem(#MENU_StepOver, 1)
DisableMenuAndToolbarItem(#MENU_StepOut, 1)
Default ; stopped for some other reason (can continue)
DisableMenuAndToolbarItem(#MENU_Stop, 1)
DisableMenuAndToolbarItem(#MENU_Run, 0)
DisableMenuAndToolbarItem(#MENU_Step, 0)
DisableMenuAndToolbarItem(#MENU_StepX, 0)
DisableMenuAndToolbarItem(#MENU_StepOver, 0)
DisableMenuAndToolbarItem(#MENU_StepOut, 0)
EndSelect
DisableMenuAndToolbarItem(#MENU_Debugger, 1) ; cannot enable/disable the debugger wile it is running
; not available if not compiled in purifier mode
If *Debugger\IsPurifier
DisableMenuAndToolbarItem(#MENU_Purifier, 0)
Else
DisableMenuAndToolbarItem(#MENU_Purifier, 1)
EndIf
DisableMenuAndToolbarItem(#MENU_Kill, 0)
DisableMenuAndToolbarItem(#MENU_DebugOutput, 0)
DisableMenuAndToolbarItem(#MENU_Watchlist, 0)
DisableMenuAndToolbarItem(#MENU_VariableList, 0)
DisableMenuAndToolbarItem(#MENU_Profiler, 0)
DisableMenuAndToolbarItem(#MENU_Memory, 0)
DisableMenuAndToolbarItem(#MENU_DebugAsm, 0)
DisableMenuAndToolbarItem(#MENU_History, 0)
DisableMenuAndToolbarItem(#MENU_LibraryViewer, 0)
DisableMenuAndToolbarItem(#MENU_DataBreakPoints, 0)
; ElseIf *Debugger
; DisableMenuAndToolbarItem(#MENU_Debugger, 0)
;
; DisableMenuAndToolbarItem(#MENU_Stop, 1)
; DisableMenuAndToolbarItem(#MENU_Run, 1)
; DisableMenuAndToolbarItem(#MENU_Step, 1)
; DisableMenuAndToolbarItem(#MENU_StepX, 1)
; DisableMenuAndToolbarItem(#MENU_Kill, 0) ; enable only the kill, to be enable to do a "cleanup" when the exe does not even start.
; DisableMenuAndToolbarItem(#MENU_DebugOutput, 1)
; DisableMenuAndToolbarItem(#MENU_Watchlist, 1)
; DisableMenuAndToolbarItem(#MENU_VariableList, 1)
; DisableMenuAndToolbarItem(#MENU_Memory, 1)
; DisableMenuAndToolbarItem(#MENU_DebugAsm, 1)
; DisableMenuAndToolbarItem(#MENU_History, 1)
Else
DisableMenuAndToolbarItem(#MENU_Debugger, 0)
DisableMenuAndToolbarItem(#MENU_Stop, 1)
DisableMenuAndToolbarItem(#MENU_Run, 1)
DisableMenuAndToolbarItem(#MENU_Step, 1)
DisableMenuAndToolbarItem(#MENU_StepX, 1)
DisableMenuAndToolbarItem(#MENU_StepOver, 1)
DisableMenuAndToolbarItem(#MENU_StepOut, 1)
DisableMenuAndToolbarItem(#MENU_Kill, 1)
DisableMenuAndToolbarItem(#MENU_DebugOutput, 1)
DisableMenuAndToolbarItem(#MENU_Watchlist, 1)
DisableMenuAndToolbarItem(#MENU_VariableList, 1)
DisableMenuAndToolbarItem(#MENU_Memory, 1)
DisableMenuAndToolbarItem(#MENU_DebugAsm, 1)
DisableMenuAndToolbarItem(#MENU_History, 1)
DisableMenuAndToolbarItem(#MENU_LibraryViewer, 1)
DisableMenuAndToolbarItem(#MENU_Profiler, 1)
DisableMenuAndToolbarItem(#MENU_DataBreakPoints, 1)
DisableMenuAndToolbarItem(#MENU_Purifier, 1)
EndIf
ElseIf IsEnabled And NonPBFile = 0
DisableMenuAndToolbarItem(#MENU_Debugger, 0)
DisableMenuAndToolbarItem(#MENU_Stop, 1)
DisableMenuAndToolbarItem(#MENU_Run, 1)
DisableMenuAndToolbarItem(#MENU_Step, 1)
DisableMenuAndToolbarItem(#MENU_StepX, 1)
DisableMenuAndToolbarItem(#MENU_StepOver, 1)
DisableMenuAndToolbarItem(#MENU_StepOut, 1)
DisableMenuAndToolbarItem(#MENU_Kill, 1)
DisableMenuAndToolbarItem(#MENU_DebugOutput, 1)
DisableMenuAndToolbarItem(#MENU_Watchlist, 1)
DisableMenuAndToolbarItem(#MENU_VariableList, 1)
DisableMenuAndToolbarItem(#MENU_DebugAsm, 1)
DisableMenuAndToolbarItem(#MENU_History, 1)
DisableMenuAndToolbarItem(#MENU_Memory, 1)
DisableMenuAndToolbarItem(#MENU_LibraryViewer, 1)
DisableMenuAndToolbarItem(#MENU_Profiler, 1)
DisableMenuAndToolbarItem(#MENU_DataBreakPoints, 1)
DisableMenuAndToolbarItem(#MENU_Purifier, 1)
; ; If *StartSource And *StartSource\RunDebuggerMode = 3 ; console debugger
; ; DisableMenuAndToolbarItem(#MENU_BreakPoint, 1)
; ; DisableMenuAndToolbarItem(#MENU_BreakClear, 1)
; ; Else
; DisableMenuAndToolbarItem(#MENU_BreakPoint, 0)
; DisableMenuAndToolbarItem(#MENU_BreakClear, 0)
; ; EndIf
Else
DisableMenuAndToolbarItem(#MENU_Debugger, NonPBFile)
DisableMenuAndToolbarItem(#MENU_Stop, 1)
DisableMenuAndToolbarItem(#MENU_Run, 1)
DisableMenuAndToolbarItem(#MENU_Step, 1)
DisableMenuAndToolbarItem(#MENU_StepX, 1)
DisableMenuAndToolbarItem(#MENU_StepOver, 1)
DisableMenuAndToolbarItem(#MENU_StepOut, 1)
DisableMenuAndToolbarItem(#MENU_Kill, 1)
DisableMenuAndToolbarItem(#MENU_BreakPoint, NonPBFile)
DisableMenuAndToolbarItem(#MENU_BreakClear, NonPBFile)
DisableMenuAndToolbarItem(#MENU_DataBreakPoints, 1)
DisableMenuAndToolbarItem(#MENU_ShowLog, 1)
; DisableMenuAndToolbarItem(#MENU_ClearLog, 1)
; DisableMenuAndToolbarItem(#MENU_CopyLog, 1)
DisableMenuAndToolbarItem(#MENU_DebugOutput, 1)
DisableMenuAndToolbarItem(#MENU_Watchlist, 1)
DisableMenuAndToolbarItem(#MENU_VariableList, 1)
DisableMenuAndToolbarItem(#MENU_DebugAsm, 1)
DisableMenuAndToolbarItem(#MENU_History, 1)
DisableMenuAndToolbarItem(#MENU_Memory, 1)
DisableMenuAndToolbarItem(#MENU_LibraryViewer, 1)
DisableMenuAndToolbarItem(#MENU_Profiler, 1)
DisableMenuAndToolbarItem(#MENU_Purifier, 1)
EndIf
If *ActiveSource And *ActiveSource <> *ProjectInfo
DisableMenuAndToolbarItem(#MENU_BreakPoint, 0)
DisableMenuAndToolbarItem(#MENU_BreakClear, 0)
DisableMenuAndToolbarItem(#MENU_ClearErrorMarks, 0)
Else
DisableMenuAndToolbarItem(#MENU_BreakPoint, 1)
DisableMenuAndToolbarItem(#MENU_BreakClear, 1)
DisableMenuAndToolbarItem(#MENU_ClearErrorMarks, 1)
EndIf
CompilerIf #CompilePPC
DisableMenuAndToolbarItem(#MENU_DebugAsm, 1) ; TODO: ASM registers support not supported on OS X
CompilerEndIf
UpdateErrorLogMenuState()
EndProcedure
Procedure Debugger_AddLog_BySource(*Source.SourceFile, Message$, TimeStamp)
If LogTimeStamp
Message$ = "[" + FormatDate(Language("Debugger","TimeStamp"), TimeStamp) + "] " + Message$
EndIf
If *Source = *ProjectInfo Or *Source\ProjectFile ; project mode
; limit the project log size too (like normal files), so the list stays readable
;
If ListSize(ProjectLog()) = #MAX_ErrorLog*10
FirstElement(ProjectLog())
DeleteElement(ProjectLog())
EndIf
LastElement(ProjectLog())
AddElement(ProjectLog())
ProjectLog() = Message$
Else
; add the messages to the source structure
;
If *Source\LogSize = #MAX_ErrorLog ; log buffer is full (remove first line)
For i = 0 To #MAX_ErrorLog-2
*Source\LogLines$[i] = *Source\LogLines$[i+1]
Next i
*Source\LogSize - 1
EndIf
*Source\LogLines$[*Source\LogSize] = Message$
*Source\LogSize + 1
EndIf
; the *Source is not necessarily the active source, so do not just add the
; message to the log, but update the full list now
;
ErrorLog_Refresh()
UpdateErrorLogMenuState()
EndProcedure
Procedure Debugger_AddLog(*Debugger.DebuggerData, Message$, TimeStamp)
If LogTimeStamp And TimeStamp <> 0
Message$ = "[" + FormatDate(Language("Debugger","TimeStamp"), TimeStamp) + "] " + Message$
EndIf
; add message to the gadget
;
If ((*ActiveSource = *ProjectInfo Or *ActiveSource\ProjectFile) And *Debugger\ID = ProjectDebuggerID) Or (*Debugger = IsDebuggedFile(*ActiveSource))
AddGadgetItem(#GADGET_ErrorLog, -1, Message$)
SetGadgetState(#GADGET_ErrorLog, CountGadgetItems(#GADGET_ErrorLog)-1)
EndIf
If *Debugger\ID = ProjectDebuggerID
; limit the project log size too (like normal files), so the list stays readable
;
If ListSize(ProjectLog()) = #MAX_ErrorLog*10
FirstElement(ProjectLog())
DeleteElement(ProjectLog())
EndIf
LastElement(ProjectLog())
AddElement(ProjectLog())
ProjectLog() = Message$
Else
; add the messages to all source files associated with this debugger
;
ForEach FileList()
If @FileList() <> *ProjectInfo And *Debugger = IsDebuggedFile(@FileList())
If FileList()\LogSize = #MAX_ErrorLog ; log buffer is full (remove first line)
For i = 0 To #MAX_ErrorLog-2
FileList()\LogLines$[i] = FileList()\LogLines$[i+1]
Next i
FileList()\LogSize - 1
EndIf
FileList()\LogLines$[FileList()\LogSize] = Message$
FileList()\LogSize + 1
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource)
EndIf
; update menu state
UpdateErrorLogMenuState()
EndProcedure
; called before the exe is terminated in any way to save the watchlist
; in the source file
;
Procedure Debugger_SaveWatchlist(*Debugger.DebuggerData)
; Find the target that triggered the compile
;
*Target.CompileTarget = FindTargetFromID(*Debugger\TriggerTargetID)
If *Target
; Save the purifier options
If *Debugger\IsPurifier
*Target\PurifierGranularity$ = GetPurifierOptions(*Debugger)
EndIf
; save the debug window history (if any)
;
If *Debugger\Windows[#DEBUGGER_WINDOW_Debug]
Gadget = *Debugger\Gadgets[#DEBUGGER_GADGET_Debug_Entry]
*Target\ExpressionHistorySize = CountGadgetItems(Gadget)
If *Target\ExpressionHistorySize > #MAX_EpressionHistory
*Target\ExpressionHistorySize = #MAX_EpressionHistory
EndIf
For i = 0 To *Target\ExpressionHistorySize - 1
*Target\ExpressionHistory$[i] = GetGadgetItemText(Gadget, i)
Next i
EndIf
; There is no reading from the program below
; this kills the watchlist save feature!
;If *Debugger\ProgramState = -1 ; after a fatal error, this can happen.
; ProcedureReturn ; cannot read from unloaded/unconnected exe
;EndIf
*Target\Watchlist$ = ""
;
; NOTE: we access the ListIconGadget of the VariableGadget directly here
; This only works as long as there are no structures in it!
; (which are currently not supported in the watchlist anyway)
; so if there are ever structures supported here, this MUST be changed!
;
Gadget = *Debugger\Gadgets[#DEBUGGER_GADGET_WatchList_List]
size = CountGadgetItems(Gadget)
If size > 0
For i = 1 To size
ProcName$ = GetGadgetItemText(Gadget, i-1, 1)
If ProcName$ <> ""
*Target\Watchlist$ + ProcName$ + ">"
EndIf
*Target\Watchlist$ + GetGadgetItemText(Gadget, i-1, 2) + ";"
Next i
; cut the last ","
*Target\Watchlist$ = Left(*Target\Watchlist$, Len(*Target\Watchlist$)-1)
EndIf
; remove all spaces to save space
; (to array indexes, there are automatically spaces added)
*Target\Watchlist$ = RemoveString(*Target\Watchlist$, " ")
EndIf
EndProcedure
Procedure Debugger_Started(*Debugger.DebuggerData)
; Output warnings for the various internal debugging features
;
CompilerIf #NOTHREAD
Debugger_AddLog(*Debugger, "WARNING! #NOTHREAD communication is used. This is for IDE debugging only! (see DebuggerCommon.pb)", 0)
CompilerEndIf
CompilerIf #PRINT_DEBUGGER_COMMANDS
Debugger_AddLog(*Debugger, "WARNING! #PRINT_DEBUGGER_COMMANDS is set. This is for IDE debugging only! (see DebuggerCommon.pb)", 0)
CompilerEndIf
CompilerIf #LOG_DEBUGGER_COMMANDS
Debugger_AddLog(*Debugger, "WARNING! #LOG_DEBUGGER_COMMANDS is set. The output file is: "+#LOG_DEBUGGER_FILE, 0)
CompilerEndIf
; transmit the set breakpoints to the exe
;
ForEach FileList()
If @FileList() <> *ProjectInfo And IsDebuggedFile(@FileList()) = *Debugger ; get all open files included in this debugger
ClearErrorLines(@FileList()) ; clear the errors in all lines
SetReadOnly(FileList()\EditorGadget, 1) ; make all included sources readonly
; enumerate all lines and transmit breakpoint messages for each breakpoint
File = GetDebuggerFileNumber(*Debugger, @FileList())
Line = -1
Repeat
Line = GetBreakPoint(@FileList(), Line+1)
If Line <> -1
Command.CommandInfo\Command = #COMMAND_BreakPoint
Command\Value1 = 1 ; set breakpoint
Command\Value2 = Line | (File << 24)
Command\DataSize = 0
SendDebuggerCommand(*Debugger, @Command)
EndIf
Until Line = -1
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource)
; transmit watchlist to exe
;
*Target.CompileTarget = FindTargetFromID(*Debugger\TriggerTargetID)
If *Target
; load DebutOutput expression history
;
Gadget = *Debugger\Gadgets[#DEBUGGER_GADGET_Debug_Entry]
For i = 0 To *Target\ExpressionHistorySize - 1
AddGadgetItem(Gadget, -1, *Target\ExpressionHistory$[i])
Next i
If *Target\Watchlist$ <> ""
index = 1
Entry$ = StringField(*Target\Watchlist$, index, ";")
While Entry$ <> ""
If FindString(Entry$, ">", 1) = 0
ProcIndex = -1
Variable$ = Entry$
Else
ProcName$ = UCase(RemoveString(StringField(Entry$, 1, ">"), "()"))
Variable$ = StringField(Entry$, 2, ">")
ProcIndex = -1 ; get the index of this procedure in the list
If *Debugger\Procedures
*pointer = *Debugger\Procedures
For i = 0 To *Debugger\NbProcedures-1
Proc$ = UCase(PeekAscii(*pointer))
*pointer + MemoryAsciiLength(*pointer) + 1
ModName$ = UCase(PeekAscii(*pointer))
*pointer + MemoryAsciiLength(*pointer) + 1
If ModuleName(Proc$, ModName$) = ProcName$
ProcIndex = i
Break
EndIf
Next i
EndIf
EndIf
; Convert to UTF8 to correctly preserve Map keys (which may contain unicode chars)
UTF8Length = StringByteLength(Variable$) + 1
VariableUTF8$ = Space(UTF8Length)
PokeS(@VariableUTF8$, Variable$, -1, #PB_UTF8)
Command.CommandInfo\Command = #COMMAND_WatchlistAdd
Command\Value1 = ProcIndex
Command\Value2 = 0 ; report no errors!
Command\DataSize = UTF8Length
SendDebuggerCommandWithData(*Debugger, @Command, @VariableUTF8$)
index + 1
Entry$ = StringField(*Target\Watchlist$, index, ";")
Wend
; update complete list:
;
Command.CommandInfo\Command = #COMMAND_GetWatchlist
SendDebuggerCommand(*Debugger, @Command)
EndIf
; Apply any saved purifier options (ignored if purifier is off)
If *Target\PurifierGranularity$ <> ""
ApplyDefaultPurifierOptions(*Debugger, *Target\PurifierGranularity$)
EndIf
EndIf
; Do this to update the Filename for all "automatic open" windows (else it remains empty!)
Debugger_UpdateWindowPreferences()
; tell the executable to start executing
;
Command.CommandInfo\Command = #COMMAND_Run
Command\Value1 = 0; do not respond with COMMAND_Continued
SendDebuggerCommand(*Debugger, @Command)
SetDebuggerMenuStates()
EndProcedure
Procedure Debugger_Ended(*Debugger.DebuggerData)
Debugger_SaveWatchlist(*Debugger)
; remove any old current line mark
; enable all included sources for editing again
;
; Note: Check if a source isn't in another debug session as well!
ForEach FileList()
If @FileList() <> *ProjectInfo
ThisDebugger = #False
OtherDebugger = #False
; Check the direct link to a debugger
;
If FileList()\DebuggerID
*FileDebugger.DebuggerData = FindDebuggerFromID(FileList()\DebuggerID)
If *FileDebugger = *Debugger
ThisDebugger = #True
ElseIf *FileDebugger And *FileDebugger\CanDestroy = 0
OtherDebugger = #True
EndIf
EndIf
; Check the indirect link as included file
;
If OtherDebugger = #False
ForEach RunningDebuggers()
If RunningDebuggers()\CanDestroy = 0
; debugger main file
If IsEqualFile(FileList()\FileName$, RunningDebuggers()\FileName$)
If @RunningDebuggers() = *Debugger
ThisDebugger = #True
Else
OtherDebugger = #True
Break ; no need to look further in this case
EndIf
EndIf
; included files
If RunningDebuggers()\IncludedFiles ; is filename buffer initialized
*Cursor = RunningDebuggers()\IncludedFiles
*Cursor + MemoryAsciiLength(*Cursor) + 1 ; skip the source path string
*Cursor + MemoryAsciiLength(*Cursor) + 1 ; skip the main source name (checked above)
For i = 1 To RunningDebuggers()\NbIncludedFiles ; check all included files
; the included filenames may include "../" so use ResolveRelativePath() on them, which resolves that to get a unique filename
FileName$ = UniqueFilename(PeekAscii(*Cursor))
If IsEqualFile(FileList()\FileName$, FileName$)
If @RunningDebuggers() = *Debugger
ThisDebugger = #True
Else
OtherDebugger = #True
Break ; no need to look further in this case
EndIf
EndIf
*Cursor + MemoryAsciiLength(*Cursor) + 1
Next i
EndIf
EndIf
Next RunningDebuggers()
EndIf
; Only clean up when no longer in any debugger's files
;
If ThisDebugger And OtherDebugger = #False
If DebuggerKeepErrorMarks = 0
ClearErrorLines(@FileList())
EndIf
ClearCurrentLine(@FileList())
SetReadOnly(FileList()\EditorGadget, 0)
EndIf
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource)
SetDebuggerMenuStates()
ActivateMainWindow() ; re-enable the focus on editor gadget (https://www.purebasic.fr/english/viewtopic.php?f=4&t=46375&p=352864#p352864)
EndProcedure
; returns true/false
Procedure Debugger_SwitchToFile(*Debugger.DebuggerData, Line)
FileName$ = GetDebuggerFile(*Debugger, Line)
If FileName$ = "" ; main source, and not saved yet
*Source.SourceFile = FindTargetFromID(*Debugger\SourceID)
If *Source And *Source\IsProject = 0 ; sanity check
If *Source <> *ActiveSource ; check to reduce flickering (especially for step)
ChangeCurrentElement(FileList(), *Source)
ChangeActiveSourceCode()
EndIf
result = 1
Else
result = 0
EndIf
ElseIf IsEqualFile(FileName$, *ActiveSource\FileName$)
result = 1
Else
result = LoadSourceFile(FileName$)
EndIf
ProcedureReturn result
EndProcedure
Procedure DebuggerCallback(*Debugger.DebuggerData)
CompilerIf #PB_Compiler_Debugger
InDebuggerCallback = #True
CompilerEndIf
Select *Debugger\Command\Command
Case #COMMAND_FatalError ; fatal communication error
Select *Debugger\Command\Value1
Case #ERROR_Memory
Text$ = Language("Debugger", "MemoryError")
Case #ERROR_Pipe
Text$ = Language("Debugger", "PipeError")
Case #ERROR_ExeQuit
Text$ = Language("Debugger", "ExeQuitError")
Case #ERROR_Timeout
Text$ = LanguagePattern("Debugger", "TimeoutError", "%timeout%", StrF(DebuggerTimeout / 1000.0, 0))
Case #ERROR_Version
Text$ = Language("Debugger", "VersionError")
Case #ERROR_NetworkFail
Text$ = Language("Debugger", "NetworkError")
EndSelect
Text$ = StringField(Text$, 1, Left(#NewLine, 1)) ; first line only
Debugger_AddLog(*Debugger, Text$, *Debugger\Command\TimeStamp)
Debugger_Ended(*Debugger) ; re-enable editing the sources
ChangeStatus(Text$, 3000)
;Case #COMMAND_Init
; NOTE: we do nothing on the init command.. we first wait for the requested
; COMMAND_GetProcedures to return, so we can init the watchlist and stuff
Case #COMMAND_Procedures
; set the warning mode
Command.CommandInfo\Command = #COMMAND_WarningMode
*Target.CompileTarget = FindTargetFromID(*Debugger\TriggerTargetID)
If *Target And *Target\CustomWarning
Command\Value1 = *Target\WarningMode
Else
Command\Value1 = WarningMode ; global setting
EndIf
SendDebuggerCommand(*Debugger, @Command)
; here the program really starts
Debugger_AddLog(*Debugger, Language("Debugger","ExeStarted"), *Debugger\Command\TimeStamp)
Debugger_Started(*Debugger)
ChangeStatus(Language("Debugger","ExeStarted"), -1)
Case #COMMAND_End
Debugger_AddLog(*Debugger, Language("Debugger","ExeEnded"), *Debugger\Command\TimeStamp)
Debugger_Ended(*Debugger)
ChangeStatus(Language("Debugger","ExeEnded"), 3000)
Case #COMMAND_ExeMode
Text$ = Language("Debugger","ExecutableType") + ": "
Select (*Debugger\Command\Value2 >> 16) & $FFFF
Case 1: Text$ + "Windows"
Case 2: Text$ + "Linux"
Case 3: Text$ + "MacOSX"
EndSelect
Select *Debugger\Command\Value2 & $FFFF
Case 1: Text$ + " - x86"
Case 2: Text$ + " - x64"
Case 3: Text$ + " - ppc"
EndSelect
If *Debugger\Is64bit
Text$ + " (64bit"
Else
Text$ + " (32bit"
EndIf
If *Debugger\IsUnicode: Text$ + ", Unicode": EndIf
If *Debugger\IsThread : Text$ + ", Thread" : EndIf
If *Debugger\IsPurifier: Text$ + ", Purifier": EndIf
Text$ + ")"
Debugger_AddLog(*Debugger, Text$, *Debugger\Command\TimeStamp)
Case #COMMAND_Error
FileName$ = GetDebuggerFile(*Debugger, *Debugger\Command\Value1)
LineNumber = *Debugger\Command\Value1 & $FFFFFF + 1
If Debugger_SwitchToFile(*Debugger, *Debugger\Command\Value1)
; the compiled file has the IDE settings appended to it, so the reported error line
; might be bigger than what the user sees. correct this here
If LineNumber > GetLinesCount(*ActiveSource)
LineNumber = GetLinesCount(*ActiveSource)
EndIf
; remove any old current line mark
ForEach FileList()
If @FileList() <> *ProjectInfo And IsDebuggedFile(@FileList()) = *Debugger ; get all open files belonging to this debugger
ClearCurrentLine(@FileList())
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource)
MarkErrorLine(LineNumber)
MarkCurrentLine(LineNumber)
EndIf
If FileName$ <> ""
Debugger_AddLog(*Debugger, Language("Debugger", "LogError") + " " + GetFilePart(FileName$) + " ("+Language("Misc","Line")+": " + Str(LineNumber)+")", *Debugger\Command\TimeStamp)
Else
Debugger_AddLog(*Debugger, Language("Debugger", "LogError") +" "+Language("Misc","Line")+": " + Str(LineNumber), *Debugger\Command\TimeStamp)
EndIf
Debugger_AddLog(*Debugger, Language("Debugger", "LogError") +" " + PeekAscii(*Debugger\CommandData), *Debugger\Command\TimeStamp)
ChangeStatus(Language("Misc","Line")+": " + Str(LineNumber) +" - " + PeekAscii(*Debugger\CommandData), -1)
If DebuggerKillOnError
Debugger_Ended(*Debugger)
Debugger_ForceDestroy(*Debugger)
EndIf
SetDebuggerMenuStates()
SetWindowForeGround(#WINDOW_Main)
Case #COMMAND_Warning
FileName$ = GetDebuggerFile(*Debugger, *Debugger\Command\Value1)
LineNumber = *Debugger\Command\Value1 & $FFFFFF + 1
If Debugger_SwitchToFile(*Debugger, *Debugger\Command\Value1)
; the compiled file has the IDE settings appended to it, so the reported error line
; might be bigger than what the user sees. correct this here
If LineNumber > GetLinesCount(*ActiveSource)
LineNumber = GetLinesCount(*ActiveSource)
EndIf
; just mark as warning, no stop or currentline change
MarkWarningLine(LineNumber)
EndIf
If FileName$ <> ""
Debugger_AddLog(*Debugger, Language("Debugger", "LogWarning") + " " + GetFilePart(FileName$) + " ("+Language("Misc","Line")+": " + Str(LineNumber)+")", *Debugger\Command\TimeStamp)
Else
Debugger_AddLog(*Debugger, Language("Debugger", "LogWarning") +" "+Language("Misc","Line")+": " + Str(LineNumber), *Debugger\Command\TimeStamp)
EndIf
Debugger_AddLog(*Debugger, Language("Debugger", "LogWarning") +" " + PeekAscii(*Debugger\CommandData), *Debugger\Command\TimeStamp)
ChangeStatus(Language("Misc","Line")+": " + Str(LineNumber) +" - " + PeekAscii(*Debugger\CommandData), -1)
Case #COMMAND_Stopped
Text$ = Language("Debugger","Stopped")
Select *Debugger\Command\Value2
Case 3: Text$ + " (CallDebugger)": SetWindowForeGround(#WINDOW_Main)
Case 5: Text$ + " ("+Language("Debugger","BeforeEnd")+")": SetWindowForeGround(#WINDOW_Main)
Case 7: Text$ + " ("+Language("Debugger","BreakPoint")+")": SetWindowForeGround(#WINDOW_Main)
Case 8: Text$ + " ("+Language("Debugger","UserRequest")+")"
Case 9
; The DataBreakpoint functions have processed the eariler #COMMAND_DataBreakPoint,
; so the matching Condition is marked in the breakpoint list
Text$ + " (" + Language("Debugger","DataBreakpoint")
*Point.DataBreakPoint = *Debugger\FirstDataBreakPoint
While *Point
If *Point\ConditionTrue
Text$ + ": " + *Point\Condition$
Break
Else
*Point = *Point\Next
EndIf
Wend
Text$ + ")"
SetWindowForeGround(#WINDOW_Main)
If *Debugger\DataBreakpointsVisible
SetWindowForeGround(*Debugger\Windows[#DEBUGGER_WINDOW_DataBreakpoints])
EndIf
EndSelect
If *Debugger\LastProgramState <> -2 ; don't log a stop after a Step command
Debugger_AddLog(*Debugger, Text$, *Debugger\Command\TimeStamp)
EndIf
ChangeStatus(Text$, -1)
; Debug "---------- Stopped ----------------------"
; Debug "Line: "+Str(*Debugger\Command\Value1)
; Debug "FilePart: "+Str((*Debugger\Command\Value1 >> 24) & $FF)
; Debug GetDebuggerFile(*Debugger, *Debugger\Command\Value1)
If *Debugger\Command\Value1 <> -1 ; check to ensure correct currentline value
FileName$ = GetDebuggerFile(*Debugger, *Debugger\Command\Value1)
LineNumber = *Debugger\Command\Value1 & $FFFFFF + 1
If Debugger_SwitchToFile(*Debugger, *Debugger\Command\Value1)
; the compiled file has the IDE settings appended to it, so the reported error line
; might be bigger than what the user sees. correct this here
If LineNumber > GetLinesCount(*ActiveSource)
LineNumber = GetLinesCount(*ActiveSource)
EndIf
; remove any old current line mark
ForEach FileList()
If @FileList() <> *ProjectInfo And IsDebuggedFile(@FileList()) = *Debugger ; get all open files belonging to this debugger
ClearCurrentLine(@FileList())
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource)
MarkCurrentLine(LineNumber)
EndIf
EndIf
SetDebuggerMenuStates()
Case #COMMAND_Continued
Debugger_AddLog(*Debugger, Language("Debugger","Continued"), *Debugger\Command\TimeStamp)
ChangeStatus(Language("Debugger","Continued"), -1)
SetDebuggerMenuStates()
; remove any old current line mark
ForEach FileList()
If @FileList() <> *ProjectInfo And IsDebuggedFile(@FileList()) = *Debugger ; get all open files belonging to this debugger
ClearCurrentLine(@FileList())
EndIf
Next FileList()
ChangeCurrentElement(FileList(), *ActiveSource)
Case #COMMAND_Debug
If DebugOutputToErrorLog ; if set, this procedure is responsible for the debug output
Message$ = "[Debug] "
If *Debugger\Command\Value1 = 5 ; long
If DebugIsHex
Message$ + Hex(*Debugger\Command\Value2, #PB_Long)