forked from HaxeFoundation/hxcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugger.cpp
1520 lines (1206 loc) · 45.5 KB
/
Debugger.cpp
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
#include <hxcpp.h>
#include <list>
#include <map>
#include <vector>
#include <string>
#include <hx/Debug.h>
#include <hx/Thread.h>
#include <hx/OS.h>
#include <hx/QuickVec.h>
// Newer versions of haxe compiler will set these too (or might be null for haxe 3.0)
static const char **__all_files_fullpath = 0;
static const char **__all_classes = 0;
#define HXCPP_DEBUG_HASHES
namespace hx
{
// These are emitted elsewhere by the haxe compiler
extern const char *__hxcpp_all_files[];
// This global boolean is set whenever there are any breakpoints (normal or
// immediate), and can relatively quickly gate debugged threads from making
// more expensive breakpoint check calls when there are no breakpoints set.
// Note that there is no lock to protect this. Volatile is used to ensure
// that within a function call, the value of gShouldCallHandleBreakpoints is
// not cached in a register and thus not properly checked within the function
// call.
volatile bool gShouldCallHandleBreakpoints = false;
// This is the event notification handler, as registered by the debugger
// thread.
// Signature: threadNumber : Int -> status: Int -> Void
static Dynamic g_eventNotificationHandler;
// This is the function to call to create a new Parameter
// Signature: name : String -> value : Dynamic -> Parameter : Dynamic
static Dynamic g_newParameterFunction;
// This is the function to call to create a new StackFrame
// Signature: fileName : String -> lineNumber :Int ->
// className : String -> functionName : String ->
// StackFrame : Dynamic
static Dynamic g_newStackFrameFunction;
// This is the function to call to create a new ThreadInfo
// Signature: number : Int -> statu s: Int -> breakpoint : Int ->
// ThreadInfo : Dynamic
static Dynamic g_newThreadInfoFunction;
// This is the function to call to add a Parameter to a StackFrame.
// Signature: inStackFrame : Dynamic -> inParameter : Dynamic -> Void
static Dynamic g_addParameterToStackFrameFunction;
// This is the function to call to add a StackFrame to a ThreadInfo.
// Signature: inThreadInfo : Dynamic -> inStackFrame : Dynamic -> Void
static Dynamic g_addStackFrameToThreadInfoFunction;
// This is the thread number of the debugger thread, extracted from
// information about the thread that called
// __hxcpp_dbg_setEventNotificationHandler
static unsigned int g_debugThreadNumber = -1;
ExecutionTrace sExecutionTrace = exeTraceOff;
// These should implement write and read memory barrier, but since there are
// no obvious portable implementations, they are currently left unimplemented
static void write_memory_barrier()
{
// currently unimplemented
}
static void read_memory_barrier()
{
// currently unimplemented
}
const char *_hx_dbg_find_scriptable_class_name(String className);
static HxMutex gMutex;
static std::map<int, DebuggerContext *> gMap;
static std::list<DebuggerContext *> gList;
class Breakpoints;
Breakpoints *ReleaseBreakpointsLocked(Breakpoints *inBreakpoints);
class DebuggerContext
{
public:
int mThreadNumber;
StackContext *mStackContext;
bool mCanStop;
int mBreakpoint;
// Always 'const' strings (no GC)
String mCriticalError;
DebugStatus mStatus;
int mStepLevel;
Breakpoints *mBreakpoints;
// Waiting for continue
bool mWaiting;
HxMutex mWaitMutex;
HxSemaphore mWaitSemaphore;
int mContinueCount;
bool mAttached;
DebuggerContext(StackContext *inStack)
{
mStackContext = inStack;
reset();
}
~DebuggerContext()
{
if (mAttached)
detach();
}
void attach(StackContext *inStack)
{
mAttached = true;
mStackContext = inStack;
mThreadNumber = mStackContext->mThreadId;
mStatus = DBG_STATUS_RUNNING;
gMutex.Lock();
gList.push_back(this);
gMap[mThreadNumber] = this;
gMutex.Unlock();
// Note that there is a race condition here. If the debugger is
// "detaching" at this exact moment, it might set the event handler to
// NULL during this call. So latch the handler variable. This means that
// the handler might be called even after the debugger thread has set it
// to NULL, but this should generally be harmless. Doing this correctly
// would require some sophisticated locking that just doesn't seem worth
// it, when the worst that can happen is an extra call to the handler
// function milliseconds after it's set to NULL ...
Dynamic handler = hx::g_eventNotificationHandler;
if (handler != null())
handler(mThreadNumber, hx::THREAD_CREATED);
}
void detach()
{
mAttached = false;
gMutex.Lock();
gList.remove(this);
gMap.erase(mThreadNumber);
mBreakpoints = ReleaseBreakpointsLocked(mBreakpoints);
gMutex.Unlock();
reset();
Dynamic handler = hx::g_eventNotificationHandler;
if (handler != null())
handler(mThreadNumber, hx::THREAD_TERMINATED);
}
void enable(bool inEnable)
{
mCanStop = inEnable;
}
void reset()
{
mCanStop = false;
mBreakpoint = 0;
mBreakpoints = 0;
mCriticalError = null();
mStatus = DBG_STATUS_INVALID;
mStepLevel = 0;
mWaiting = false;
mContinueCount = 0;
mThreadNumber = -1;
}
// Make best effort to wait until all threads are stopped
static void WaitForAllThreadsToStop()
{
// Make a "best effort" in the face of threads that could do arbitrary
// things to make the "break all" not complete successfully. Threads
// can hang in system calls indefinitely, and can spawn new threads
// continuously that themselves do the same. Don't try to be perfect
// and guarantee that all threads have stopped, as that could mean
// waiting a long time in pathological cases or even forever in really
// pathological cases. Just make a best effort and if the break
// doesn't break everything, the user will know when they go to list
// all thread stacks and can try the break again.
// Copy the thread numbers out. This is because we really don't want
// to hold the lock during the entire process as this could block
// threads from actually evaluating breakpoints.
std::vector<int> threadNumbers;
gMutex.Lock();
std::list<DebuggerContext *>::iterator iter = gList.begin();
while (iter != gList.end()) {
DebuggerContext *stack = *iter++;
if (stack->mThreadNumber == g_debugThreadNumber) {
continue;
}
threadNumbers.push_back(stack->mThreadNumber);
}
gMutex.Unlock();
// Now wait no longer than 2 seconds total for all threads to
// be stopped. If any thread times out, then stop immediately.
int size = threadNumbers.size();
// Each time slice is 1/10 of a second. Yeah there's some slop here
// because no time is accounted for the time spent outside of sem
// waiting. If there were good portable time APIs easily available
// within hxcpp I'd use them ...
int timeSlicesLeft = 20;
HxSemaphore timeoutSem;
int i = 0;
while (i < size) {
gMutex.Lock();
DebuggerContext *stack = gMap[threadNumbers[i]];
if (!stack) {
// The thread went away while we were working!
gMutex.Unlock();
i += 1;
continue;
}
if (stack->mWaiting) {
gMutex.Unlock();
i += 1;
continue;
}
gMutex.Unlock();
if (timeSlicesLeft == 0) {
// The 2 seconds have expired, give up
return;
}
// Sleep for 1/10 of a second on a semaphore that will never
// be Set.
timeoutSem.WaitSeconds(0.100);
timeSlicesLeft -= 1;
// Don't increment i, try the same thread again
}
}
// Continue the thread that is waiting, if it is waiting. Only the
// debugger thread should call this.
void Continue(int count)
{
// Paranoia
if (count < 1) {
count = 1;
}
mWaitMutex.Lock();
if (mWaiting) {
mWaiting = false;
mContinueCount = count - 1;
mWaitSemaphore.Set();
}
mWaitMutex.Unlock();
}
void DoBreak(DebugStatus status, int breakpoint, const String *criticalErrorDescription)
{
// Update status
mStatus = status;
mBreakpoint = breakpoint;
if (criticalErrorDescription)
mCriticalError = criticalErrorDescription->makePermanent();
// This thread cannot stop while making the callback
mCanStop = false;
mWaitMutex.Lock();
mWaiting = true;
mWaitMutex.Unlock();
// Call the handler to announce the status.
StackFrame *frame = mStackContext->getCurrentStackFrame();
// Record this before g_eventNotificationHandler is run, since it might change in there
mStackContext->mDebugger->mStepLevel = mStackContext->getDepth();
g_eventNotificationHandler
(mThreadNumber, THREAD_STOPPED, mStackContext->mDebugger->mStepLevel,
String(frame->position->className), String(frame->position->functionName),
String(frame->position->fileName), frame->lineNumber);
if (mWaiting)
{
// Wait until the debugger thread sets mWaiting to false and signals
// the semaphore
mWaitMutex.Lock();
while (mWaiting) {
mWaitMutex.Unlock();
hx::EnterGCFreeZone();
mWaitSemaphore.Wait();
hx::ExitGCFreeZone();
mWaitMutex.Lock();
}
mWaitMutex.Unlock();
}
// Save the breakpoint status in the call stack so that queries for
// thread info will know the current status of the thread
mStatus = DBG_STATUS_RUNNING;
mBreakpoint = -1;
// Announce the new status
Dynamic handler = hx::g_eventNotificationHandler;
if (handler!=null())
handler(mThreadNumber, THREAD_STARTED);
// Can stop again
mCanStop = true;
}
// Wait for someone to call Continue() on this call stack. Really only
// the thread that owns this call stack should call Wait().
void Break(DebugStatus status, int breakpoint,
const String *criticalErrorDescription)
{
// If break status is break immediate, then eliminate any residual
// continue count from the last continue.
if (status == DBG_STATUS_STOPPED_BREAK_IMMEDIATE) {
mContinueCount = 0;
}
// Else break status is break in breakpoint -- but if there is a
// continue count, just decrement the continue count
else if (mContinueCount > 0) {
mContinueCount -= 1;
return;
}
this->DoBreak(status, breakpoint, 0);
}
};
DebuggerContext *dbgCtxCreate(StackContext *inStack) { return new DebuggerContext(inStack); }
void dbgCtxDestroy(DebuggerContext *ctx) { delete ctx; }
void dbgCtxAttach(DebuggerContext *ctx, StackContext *inStack) { ctx->attach(inStack); }
void dbgCtxDetach(DebuggerContext *ctx) { ctx->detach(); }
void dbgCtxEnable(DebuggerContext *ctx, bool inEnable) { ctx->enable(inEnable); }
class Breakpoints
{
public:
static int Hash(int value, const char *inString)
{
while(*inString)
value = value*223 + *inString++;
return value;
}
static int Add(String inFileName, int lineNumber)
{
// Look up the filename constant
const char *fileName = LookupFileName(inFileName);
if (!fileName) {
return -1;
}
gMutex.Lock();
int ret = gNextBreakpointNumber++;
Breakpoints *newBreakpoints = new Breakpoints(gBreakpoints, ret, fileName, lineNumber);
gBreakpoints->RemoveRef();
// Write memory barrier ensures that newBreakpoints values are updated
// before gBreakpoints is assigned to it
write_memory_barrier();
gBreakpoints = newBreakpoints;
// Don't need a write memory barrier here, it's harmless to see
// gShouldCallHandleBreakpoints update before gBreakpoints has updated
gShouldCallHandleBreakpoints = true;
gMutex.Unlock();
return ret;
}
void RemoveRef()
{
if (--mRefCount == 0)
delete this;
}
static int Add(String inClassName, String functionName)
{
// Look up the class name constant
const char *className = LookupClassName(inClassName);
if (!className) {
return -1;
}
gMutex.Lock();
int ret = gNextBreakpointNumber++;
Breakpoints *newBreakpoints = new Breakpoints(gBreakpoints, ret, className, functionName);
gBreakpoints->RemoveRef();
// Write memory barrier ensures that newBreakpoints values are updated
// before gBreakpoints is assigned to it
write_memory_barrier();
gBreakpoints = newBreakpoints;
// Don't need a write memory barrier here, it's harmless to see
// gShouldCallHandleBreakpoints update before gBreakpoints has updated
gShouldCallHandleBreakpoints = true;
gMutex.Unlock();
return ret;
}
static void DeleteAll()
{
gMutex.Lock();
Breakpoints *newBreakpoints = new Breakpoints();
gBreakpoints->RemoveRef();
// Write memory barrier ensures that newBreakpoints values are updated
// before gBreakpoints is assigned to it
write_memory_barrier();
gBreakpoints = newBreakpoints;
// Don't need a write memory barrier here, it's harmless to see
// gShouldCallHandleBreakpoints update before gStepType has updated
gShouldCallHandleBreakpoints = (gStepType != STEP_NONE) || (sExecutionTrace==exeTraceLines);
gMutex.Unlock();
}
static void Delete(int number)
{
gMutex.Lock();
if (gBreakpoints->HasBreakpoint(number)) {
// Replace mBreakpoints with a copy and remove the breakpoint
// from it
Breakpoints *newBreakpoints = new Breakpoints(gBreakpoints, number);
Breakpoints *toRelease = gBreakpoints;
gBreakpoints = newBreakpoints;
// Write memory barrier ensures that newBreakpoints values are
// updated before gBreakpoints is assigned to it
write_memory_barrier();
// Only release after gBreakpoints is set
toRelease->RemoveRef();
if (gBreakpoints->IsEmpty()) {
// Don't need a write memory barrier here, it's harmless to
// see gShouldCallHandleBreakpoints update before gStepType
// has updated
gShouldCallHandleBreakpoints = (gStepType != STEP_NONE) || (sExecutionTrace==exeTraceLines);
}
}
gMutex.Unlock();
}
static void BreakNow(bool wait)
{
gStepType = STEP_INTO;
gStepCount = 0;
gStepThread = -1;
// Won't bother with a write memory barrier here, it's harmless to set
// gShouldCallHandleBreakpoints before the step type and step thread
// are updated xxx should consider making gStepType and gStepThread
// atomic though by putting them into one uint32_t value ...
gShouldCallHandleBreakpoints = true;
// Wait for all threads to be stopped
if (wait) {
DebuggerContext::WaitForAllThreadsToStop();
}
}
static void ContinueThreads(int specialThreadNumber, int continueCount)
{
gStepType = STEP_NONE;
gShouldCallHandleBreakpoints = !gBreakpoints->IsEmpty() || (sExecutionTrace==exeTraceLines);
gMutex.Lock();
// All threads get continued, but specialThreadNumber only for count
std::list<DebuggerContext *>::iterator iter = gList.begin();
while (iter != gList.end()) {
DebuggerContext *stack = *iter++;
if (stack->mThreadNumber == specialThreadNumber) {
stack->Continue(continueCount);
}
else {
stack->Continue(1);
}
}
gMutex.Unlock();
}
static void StepThread(int threadNumber, StepType stepType, int stepCount)
{
// Continue the thread, but set its step first
gStepThread = threadNumber;
gStepType = stepType;
gStepCount = stepCount;
gMutex.Lock();
std::list<DebuggerContext *>::iterator iter = gList.begin();
while (iter != gList.end()) {
DebuggerContext *stack = *iter++;
if (stack->mThreadNumber == threadNumber) {
gStepLevel = stack->mStackContext->mDebugger->mStepLevel;
stack->Continue(1);
break;
}
}
gMutex.Unlock();
}
// Note that HandleBreakpoints is called immediately after a read memory
// barrier by the HX_STACK_LINE macro
static void HandleBreakpoints(hx::StackContext *stack)
{
// This will be set to a valid status if a stop is needed
DebugStatus breakStatus = DBG_STATUS_INVALID;
int breakpointNumber = -1;
// The debug thread never breaks
if (stack->mThreadId == g_debugThreadNumber) {
return;
}
if (sExecutionTrace==exeTraceLines)
stack->tracePosition();
// Handle possible immediate break
if (gStepType == STEP_NONE) {
// No stepping
}
else if (gStepType == STEP_INTO) {
if ((gStepThread == -1) ||
(gStepThread == stack->mThreadId)) {
breakStatus = DBG_STATUS_STOPPED_BREAK_IMMEDIATE;
}
}
else {
if ((gStepThread == -1) ||
(gStepThread == stack->mThreadId)) {
if (gStepType == STEP_OVER) {
if (stack->getDepth() <= gStepLevel) {
breakStatus = DBG_STATUS_STOPPED_BREAK_IMMEDIATE;
}
}
else { // (gStepType == STEP_OUT)
if (stack->getDepth() < gStepLevel) {
breakStatus = DBG_STATUS_STOPPED_BREAK_IMMEDIATE;
}
}
}
}
// If didn't hit any immediate breakpoints, check for set breakpoints
if (breakStatus == DBG_STATUS_INVALID) {
Breakpoints *breakpoints = stack->mDebugger->mBreakpoints;
// If the current thread has never gotten a reference to
// breakpoints, get a reference to the current breakpoints
if (!breakpoints) {
gMutex.Lock();
// Get break points and ref it
breakpoints = gBreakpoints;
// This read memory barrier ensures that old values within
// gBreakpoints are not seen after gBreakpoints has been set
// here
read_memory_barrier();
stack->mDebugger->mBreakpoints = breakpoints;
breakpoints->AddRef();
gMutex.Unlock();
}
// Else if the current thread's breakpoints number is out of date,
// release the reference on that and get the new breakpoints.
// Note that no locking is done on the reference to gBreakpoints.
// A thread calling GetBreakpoints will retain its old breakpoints
// until it "sees" a newer gBreakpoints. Without memory barriers,
// this could theoretically be indefinitely.
else if (breakpoints != gBreakpoints) {
gMutex.Lock();
// Release ref on current break points
breakpoints->RemoveRef();
// Get new break points and ref it
breakpoints = gBreakpoints;
// This read memory barrier ensures that old values within
// gBreakpoints are not seen after gBreakpoints has been set
// here
read_memory_barrier();
stack->mDebugger->mBreakpoints = breakpoints;
breakpoints->AddRef();
gMutex.Unlock();
}
// If there are breakpoints, then may need to break in one
if (!breakpoints->IsEmpty())
{
StackFrame *frame = stack->getCurrentStackFrame();
if (!breakpoints->QuickRejectClassFunc(frame->position->classFuncHash))
{
// Check for class:function breakpoint if this is the
// first line of the stack frame
if (frame->lineNumber == frame->position->firstLineNumber)
breakpointNumber = breakpoints->FindClassFunctionBreakpoint(frame);
}
// If still haven't hit a break point, check for file:line
// breakpoint
if (breakpointNumber == -1 && !breakpoints->QuickRejectFileLine(frame->position->fileHash))
breakpointNumber = breakpoints->FindFileLineBreakpoint(frame);
if (breakpointNumber != -1)
breakStatus = DBG_STATUS_STOPPED_BREAKPOINT;
}
}
// If no breakpoint of any kind was found, then don't break
if (breakStatus == DBG_STATUS_INVALID) {
return;
}
// If the thread has been put into no stop mode, it can't stop
if (!stack->mDebugger->mCanStop) {
return;
}
// If the break was an immediate break, and there was a step count,
// just decrement the step count
if (breakStatus == DBG_STATUS_STOPPED_BREAK_IMMEDIATE) {
if (gStepCount > 1) {
gStepCount -= 1;
return;
}
}
// Now break, which will wait until the debugger thread continues
// the thread
stack->mDebugger->Break(breakStatus, breakpointNumber, 0);
}
static bool shoudBreakOnLine()
{
return gBreakpoints->IsEmpty() || gStepType != hx::STEP_NONE;
}
private:
struct Breakpoint
{
int number;
int lineNumber;
int hash;
bool isFileLine;
std::string fileOrClassName;
std::string functionName;
};
// Creates Breakpoints object with no breakpoints and a zero version
Breakpoints()
: mRefCount(1), mBreakpointCount(0), mBreakpoints(0)
{
#ifdef HXCPP_DEBUG_HASHES
calcCombinedHash();
#endif
}
// Copies breakpoints from toCopy and adds a new file:line breakpoint
Breakpoints(const Breakpoints *toCopy, int number,
const char *fileName, int lineNumber)
: mRefCount(1)
{
mBreakpointCount = toCopy->mBreakpointCount + 1;
mBreakpoints = new Breakpoint[mBreakpointCount];
for (int i = 0; i < toCopy->mBreakpointCount; i++)
mBreakpoints[i] = toCopy->mBreakpoints[i];
mBreakpoints[toCopy->mBreakpointCount].number = number;
mBreakpoints[toCopy->mBreakpointCount].isFileLine = true;
mBreakpoints[toCopy->mBreakpointCount].fileOrClassName = fileName;
mBreakpoints[toCopy->mBreakpointCount].lineNumber = lineNumber;
#ifdef HXCPP_DEBUG_HASHES
mBreakpoints[toCopy->mBreakpointCount].hash = Hash(0, fileName);
calcCombinedHash();
#else
mBreakpoints[toCopy->mBreakpointCount].hash = 0;
#endif
}
// Copies breakpoints from toCopy and adds a new class:function breakpoint
Breakpoints(const Breakpoints *toCopy, int number,
const char *className, String functionName)
: mRefCount(1)
{
mBreakpointCount = toCopy->mBreakpointCount + 1;
mBreakpoints = new Breakpoint[mBreakpointCount];
for (int i = 0; i < toCopy->mBreakpointCount; i++) {
mBreakpoints[i] = toCopy->mBreakpoints[i];
}
mBreakpoints[toCopy->mBreakpointCount].number = number;
mBreakpoints[toCopy->mBreakpointCount].isFileLine = false;
mBreakpoints[toCopy->mBreakpointCount].fileOrClassName = className;
mBreakpoints[toCopy->mBreakpointCount].functionName = functionName.c_str();
#ifdef HXCPP_DEBUG_HASHES
int hash = Hash(0,className);
hash = Hash(hash,".");
hash = Hash(hash,functionName.c_str());
//printf("%s.%s -> %08x\n", className, functionName.c_str(), hash );
mBreakpoints[toCopy->mBreakpointCount].hash = hash;
calcCombinedHash();
#else
mBreakpoints[toCopy->mBreakpointCount].hash = 0;
#endif
}
// Copies breakpoints from toCopy except for number
Breakpoints(const Breakpoints *toCopy, int number)
: mRefCount(1)
{
mBreakpointCount = toCopy->mBreakpointCount - 1;
if (mBreakpointCount == 0)
mBreakpoints = 0;
else
{
mBreakpoints = new Breakpoint[mBreakpointCount];
for(int s = 0, d = 0; s < toCopy->mBreakpointCount; s++)
{
Breakpoint &other = toCopy->mBreakpoints[s];
if (other.number != number)
mBreakpoints[d++] = toCopy->mBreakpoints[s];
}
}
#ifdef HXCPP_DEBUG_HASHES
calcCombinedHash();
#endif
}
#ifdef HXCPP_DEBUG_HASHES
void calcCombinedHash()
{
int allFileLine = 0;
int allClassFunc = 0;
for(int i=0;i<mBreakpointCount;i++)
if (mBreakpoints[i].isFileLine)
allFileLine |= mBreakpoints[i].hash;
else
allClassFunc |= mBreakpoints[i].hash;
mNotInAnyFileLine = ~allFileLine;
mNotInAnyClassFunc = ~allClassFunc;
//printf("Combined mask -> %08x %08x\n", mNotInAnyFileLine, mNotInAnyClassFunc);
}
#endif
~Breakpoints()
{
delete[] mBreakpoints;
}
void AddRef()
{
mRefCount += 1;
}
bool IsEmpty() const
{
return (mBreakpointCount == 0);
}
inline bool QuickRejectClassFunc(int inHash)
{
#ifdef HXCPP_DEBUG_HASHES
return inHash & mNotInAnyClassFunc;
#else
return false;
#endif
}
inline bool QuickRejectFileLine(int inHash)
{
#ifdef HXCPP_DEBUG_HASHES
return inHash & mNotInAnyFileLine;
#else
return false;
#endif
}
bool HasBreakpoint(int number) const
{
for (int i = 0; i < mBreakpointCount; i++) {
if (number == mBreakpoints[i].number) {
return true;
}
}
return false;
}
int FindFileLineBreakpoint(StackFrame *inFrame)
{
for (int i = 0; i < mBreakpointCount; i++)
{
Breakpoint &breakpoint = mBreakpoints[i];
if (breakpoint.isFileLine &&
#ifdef HXCPP_DEBUG_HASHES
breakpoint.hash==inFrame->position->fileHash &&
#endif
(breakpoint.lineNumber == inFrame->lineNumber) &&
!strcmp(breakpoint.fileOrClassName.c_str(),inFrame->position->fileName) )
return breakpoint.number;
}
return -1;
}
int FindClassFunctionBreakpoint(StackFrame *inFrame)
{
for (int i = 0; i < mBreakpointCount; i++)
{
Breakpoint &breakpoint = mBreakpoints[i];
if (!breakpoint.isFileLine &&
#ifdef HXCPP_DEBUG_HASHES
breakpoint.hash==inFrame->position->classFuncHash &&
#endif
!strcmp(breakpoint.fileOrClassName.c_str(), inFrame->position->className) &&
!strcmp(breakpoint.functionName.c_str(), inFrame->position->functionName) )
return breakpoint.number;
}
return -1;
}
// Looks up the "interned" version of the name, for faster compares
// when evaluating breakpoints
static const char *LookupFileName(String fileName)
{
if (fileName.length == 0) return 0;
for (const char **ptr = hx::__hxcpp_all_files; *ptr; ptr++)
{
if (!strcmp(*ptr, fileName))
return *ptr;
}
#ifdef HXCPP_SCRIPTABLE
Array< ::String> ret = Array_obj< ::String>::__new();
__hxcpp_dbg_getScriptableFiles(ret);
for(int i=0;i<ret->length;i++)
if (ret[i]==fileName)
return (ret[i]).makePermanent().utf8_str();
ret = Array_obj< ::String>::__new();
__hxcpp_dbg_getScriptableFilesFullPath(ret);
for(int i=0;i<ret->length;i++)
if (ret[i]==fileName)
return (ret[i]).makePermanent().utf8_str();
#endif
return 0;
}
static const char *LookupClassName(String className)
{
if (__all_classes)
for (const char **ptr = __all_classes; *ptr; ptr++)
{
if (!strcmp(*ptr, className.raw_ptr()))
return *ptr;
}
#ifdef HXCPP_SCRIPTABLE
Array< ::String> ret = Array_obj< ::String>::__new();
__hxcpp_dbg_getScriptableClasses(ret);
for(int i=0;i<ret->length;i++)
if (ret[i]==className)
return ret[i].makePermanent().raw_ptr();
#endif
return 0;
}
private:
int mRefCount;
int mBreakpointCount;
int mNotInAnyClassFunc;
int mNotInAnyFileLine;
Breakpoint *mBreakpoints;
static int gNextBreakpointNumber;
static Breakpoints * volatile gBreakpoints;
static StepType gStepType;
static int gStepLevel;
static int gStepThread; // If -1, all threads are targeted
static int gStepCount;
};
/* static */ int Breakpoints::gNextBreakpointNumber;
/* static */ Breakpoints * volatile Breakpoints::gBreakpoints = new Breakpoints();
/* static */ StepType Breakpoints::gStepType = STEP_NONE;
/* static */ int Breakpoints::gStepLevel;
/* static */ int Breakpoints::gStepThread = -1;
/* static */ int Breakpoints::gStepCount = -1;
Breakpoints *ReleaseBreakpointsLocked(Breakpoints *inBreakpoints)
{
if (inBreakpoints)
inBreakpoints->RemoveRef();
return 0;
}
// Gets a ThreadInfo for a thread
static Dynamic GetThreadInfo(int threadNumber, bool unsafe)
{
if (threadNumber == g_debugThreadNumber)
return null();
DebuggerContext *stack = 0;
gMutex.Lock();
if (gMap.count(threadNumber) == 0)
{
gMutex.Unlock();
return null();
}
else
stack = gMap[threadNumber];
if ((stack->mStatus == DBG_STATUS_RUNNING) && !unsafe)
{
gMutex.Unlock();
return null();
}
// It's safe to release the mutex here, because the stack to be
// converted is either for a thread that is not running (and thus
// the stack cannot be altered while the conversion is in progress),
// or unsafe mode has been invoked
gMutex.Unlock();
Dynamic ret = g_newThreadInfoFunction
(stack->mThreadNumber, stack->mStatus, stack->mBreakpoint,
stack->mCriticalError);
int size = stack->mStackContext->getDepth();
for (int i = 0; i < size; i++)
{
StackFrame *frame = stack->mStackContext->getStackFrame(i);
#ifdef HXCPP_STACK_LINE
Dynamic info = g_newStackFrameFunction