-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathruby_debug.c
2705 lines (2353 loc) · 74.4 KB
/
ruby_debug.c
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 <ruby.h>
#include <stdio.h>
#include <ctype.h>
#include <vm_core.h>
#include <iseq.h>
#include <version.h>
#include <eval_intern.h>
#include <insns.inc>
#include <insns_info.inc>
#include "ruby_debug.h"
#define DEBUG_VERSION "0.11.30.pre10"
#define FRAME_N(n) (&debug_context->frames[debug_context->stack_size-(n)-1])
#define GET_FRAME (FRAME_N(check_frame_number(debug_context, frame)))
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif
#define STACK_SIZE_INCREMENT 128
int rb_vm_get_sourceline(const rb_control_frame_t *cfp); /* from vm.c */
/* from iseq.c */
#ifdef RB_ISEQ_COMPILE_5ARGS
VALUE rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE filepath, VALUE line, VALUE opt);
#else
VALUE rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE line, VALUE opt);
#endif
typedef struct {
st_table *tbl;
} threads_table_t;
static VALUE tracing = Qfalse;
static VALUE locker = Qnil;
static VALUE post_mortem = Qfalse;
static VALUE keep_frame_binding = Qfalse;
static VALUE resolve_symlinks = Qtrue;
static VALUE debug = Qfalse;
static VALUE track_frame_args = Qfalse;
static VALUE last_context = Qnil;
static VALUE last_thread = Qnil;
static debug_context_t *last_debug_context = NULL;
VALUE rdebug_threads_tbl = Qnil; /* Context for each of the threads */
VALUE mDebugger; /* Ruby Debugger Module object */
static VALUE opt_call_c_function;
static VALUE cThreadsTable;
static VALUE cContext;
static VALUE cDebugThread;
static VALUE rb_mObjectSpace;
static ID idAtBreakpoint;
static ID idAtCatchpoint;
static ID idAtLine;
static ID idAtReturn;
static ID idAtTracing;
static ID idList;
static int start_count = 0;
static int thnum_max = 0;
static int bkp_count = 0;
static int last_debugged_thnum = -1;
static unsigned long last_check = 0;
static unsigned long hook_count = 0;
static VALUE create_binding(VALUE);
static VALUE debug_stop(VALUE);
static void save_current_position(debug_context_t *);
static VALUE context_copy_args(debug_frame_t *);
static VALUE context_copy_locals(debug_context_t *,debug_frame_t *, VALUE);
static void context_suspend_0(debug_context_t *);
static void context_resume_0(debug_context_t *);
static void copy_scalar_args(debug_frame_t *);
typedef struct locked_thread_t {
VALUE thread_id;
struct locked_thread_t *next;
} locked_thread_t;
static locked_thread_t *locked_head = NULL;
static locked_thread_t *locked_tail = NULL;
/* "Step", "Next" and "Finish" do their work by saving information
about where to stop next. reset_stopping_points removes/resets this
information. */
inline static const char *
get_event_name(rb_event_flag_t _event)
{
switch (_event) {
case RUBY_EVENT_LINE:
return "line";
case RUBY_EVENT_CLASS:
return "class";
case RUBY_EVENT_END:
return "end";
case RUBY_EVENT_CALL:
return "call";
case RUBY_EVENT_RETURN:
return "return";
case RUBY_EVENT_C_CALL:
return "c-call";
case RUBY_EVENT_C_RETURN:
return "c-return";
case RUBY_EVENT_RAISE:
return "raise";
default:
return "unknown";
}
}
inline static void
reset_stepping_stop_points(debug_context_t *debug_context)
{
debug_context->dest_frame = -1;
debug_context->stop_line = -1;
debug_context->stop_next = -1;
}
inline static VALUE
real_class(VALUE klass)
{
if (klass) {
if (TYPE(klass) == T_ICLASS) {
return RBASIC(klass)->klass;
}
else if (FL_TEST(klass, FL_SINGLETON)) {
return rb_iv_get(klass, "__attached__");
}
}
return klass;
}
inline static VALUE
ref2id(VALUE obj)
{
return obj;
}
inline static VALUE
id2ref(VALUE id)
{
return id;
}
inline static VALUE
context_thread_0(debug_context_t *debug_context)
{
return id2ref(debug_context->thread_id);
}
static inline const rb_data_type_t *
threadptr_data_type(void)
{
static const rb_data_type_t *thread_data_type;
if (!thread_data_type) {
VALUE current_thread = rb_thread_current();
thread_data_type = RTYPEDDATA_TYPE(current_thread);
}
return thread_data_type;
}
#define ruby_threadptr_data_type *threadptr_data_type()
#define ruby_current_thread ((rb_thread_t *)RTYPEDDATA_DATA(rb_thread_current()))
static int
is_in_locked(VALUE thread_id)
{
locked_thread_t *node;
if(!locked_head)
return 0;
for(node = locked_head; node != locked_tail; node = node->next)
{
if(node->thread_id == thread_id) return 1;
}
return 0;
}
static void
add_to_locked(VALUE thread)
{
locked_thread_t *node;
VALUE thread_id = ref2id(thread);
if(is_in_locked(thread_id))
return;
node = ALLOC(locked_thread_t);
node->thread_id = thread_id;
node->next = NULL;
if(locked_tail)
locked_tail->next = node;
locked_tail = node;
if(!locked_head)
locked_head = node;
}
static VALUE
remove_from_locked()
{
VALUE thread;
locked_thread_t *node;
if(locked_head == NULL)
return Qnil;
node = locked_head;
locked_head = locked_head->next;
if(locked_tail == node)
locked_tail = NULL;
thread = id2ref(node->thread_id);
xfree(node);
return thread;
}
static int is_living_thread(VALUE thread);
static int
threads_table_mark_keyvalue(st_data_t key, st_data_t value, st_data_t tbl)
{
VALUE thread = id2ref((VALUE)key);
if (!value) {
return ST_CONTINUE;
}
rb_gc_mark((VALUE)value);
if (is_living_thread(thread)) {
rb_gc_mark(thread);
}
else {
st_insert((st_table *)tbl, key, 0);
}
return ST_CONTINUE;
}
static void
threads_table_mark(void* data)
{
threads_table_t *threads_table = (threads_table_t*)data;
st_table *tbl = threads_table->tbl;
st_foreach(tbl, threads_table_mark_keyvalue, (st_data_t)tbl);
}
static void
threads_table_free(void* data)
{
threads_table_t *threads_table = (threads_table_t*)data;
st_free_table(threads_table->tbl);
xfree(threads_table);
}
static VALUE
threads_table_create(void)
{
threads_table_t *threads_table;
threads_table = ALLOC(threads_table_t);
threads_table->tbl = st_init_numtable();
return Data_Wrap_Struct(cThreadsTable, threads_table_mark, threads_table_free, threads_table);
}
static void
threads_table_clear(VALUE table)
{
threads_table_t *threads_table;
Data_Get_Struct(table, threads_table_t, threads_table);
st_clear(threads_table->tbl);
}
static int
is_thread_alive(VALUE thread)
{
rb_thread_t *th;
GetThreadPtr(thread, th);
return th->status != THREAD_KILLED;
}
static int
is_living_thread(VALUE thread)
{
return rb_obj_is_kind_of(thread, rb_cThread) && is_thread_alive(thread);
}
static int
threads_table_check_i(st_data_t key, st_data_t value, st_data_t dummy)
{
VALUE thread;
if(!value)
{
return ST_DELETE;
}
thread = id2ref((VALUE)key);
if(!is_living_thread(thread))
{
return ST_DELETE;
}
return ST_CONTINUE;
}
static void
check_thread_contexts(void)
{
threads_table_t *threads_table;
Data_Get_Struct(rdebug_threads_tbl, threads_table_t, threads_table);
st_foreach(threads_table->tbl, threads_table_check_i, 0);
}
/*
* call-seq:
* Debugger.started? -> bool
*
* Returns +true+ the debugger is started.
*/
static VALUE
debug_is_started(VALUE self)
{
return IS_STARTED ? Qtrue : Qfalse;
}
static void
debug_context_mark(void *data)
{
debug_frame_t *frame;
int i;
debug_context_t *debug_context = (debug_context_t *)data;
for(i = 0; i < debug_context->stack_size; i++)
{
frame = &(debug_context->frames[i]);
rb_gc_mark(frame->binding);
rb_gc_mark(frame->self);
rb_gc_mark(frame->arg_ary);
if(frame->dead)
{
rb_gc_mark(frame->info.copy.locals);
rb_gc_mark(frame->info.copy.args);
}
}
rb_gc_mark(debug_context->breakpoint);
}
static void
debug_context_free(void *data)
{
debug_context_t *debug_context = (debug_context_t *)data;
xfree(debug_context->frames);
}
static VALUE
debug_context_create(VALUE thread)
{
debug_context_t *debug_context;
debug_context = ALLOC(debug_context_t);
debug_context-> thnum = ++thnum_max;
debug_context->last_file = NULL;
debug_context->last_line = 0;
debug_context->flags = 0;
debug_context->stop_next = -1;
debug_context->dest_frame = -1;
debug_context->stop_line = -1;
debug_context->stop_frame = -1;
debug_context->stop_reason = CTX_STOP_NONE;
debug_context->stack_len = STACK_SIZE_INCREMENT;
debug_context->frames = ALLOC_N(debug_frame_t, STACK_SIZE_INCREMENT);
debug_context->stack_size = 0;
debug_context->thread_id = ref2id(thread);
debug_context->breakpoint = Qnil;
debug_context->jump_pc = NULL;
debug_context->jump_cfp = NULL;
debug_context->old_iseq_catch = NULL;
debug_context->thread_pause = 0;
if(rb_obj_class(thread) == cDebugThread)
CTX_FL_SET(debug_context, CTX_FL_IGNORE);
return Data_Wrap_Struct(cContext, debug_context_mark, debug_context_free, debug_context);
}
static VALUE
debug_context_dup(debug_context_t *debug_context, VALUE self)
{
debug_context_t *new_debug_context;
debug_frame_t *new_frame, *old_frame;
int i;
new_debug_context = ALLOC(debug_context_t);
memcpy(new_debug_context, debug_context, sizeof(debug_context_t));
new_debug_context->stop_next = -1;
new_debug_context->dest_frame = -1;
new_debug_context->stop_line = -1;
new_debug_context->stop_frame = -1;
new_debug_context->breakpoint = Qnil;
CTX_FL_SET(new_debug_context, CTX_FL_DEAD);
new_debug_context->frames = ALLOC_N(debug_frame_t, debug_context->stack_size);
new_debug_context->stack_len = debug_context->stack_size;
memcpy(new_debug_context->frames, debug_context->frames, sizeof(debug_frame_t) * debug_context->stack_size);
for(i = 0; i < debug_context->stack_size; i++)
{
new_frame = &(new_debug_context->frames[i]);
old_frame = &(debug_context->frames[i]);
new_frame->dead = 1;
new_frame->info.copy.args = context_copy_args(old_frame);
new_frame->info.copy.locals = context_copy_locals(debug_context, old_frame, self);
}
return Data_Wrap_Struct(cContext, debug_context_mark, debug_context_free, new_debug_context);
}
static void
thread_context_lookup(VALUE thread, VALUE *context, debug_context_t **debug_context, int create)
{
threads_table_t *threads_table;
VALUE thread_id;
debug_context_t *l_debug_context;
debug_check_started();
if(last_thread == thread && last_context != Qnil)
{
*context = last_context;
if(debug_context)
*debug_context = last_debug_context;
return;
}
thread_id = ref2id(thread);
Data_Get_Struct(rdebug_threads_tbl, threads_table_t, threads_table);
if(!st_lookup(threads_table->tbl, thread_id, context) || !*context)
{
if (create)
{
*context = debug_context_create(thread);
st_insert(threads_table->tbl, thread_id, *context);
}
else
{
*context = 0;
if (debug_context)
*debug_context = NULL;
return;
}
}
Data_Get_Struct(*context, debug_context_t, l_debug_context);
if(debug_context)
*debug_context = l_debug_context;
last_thread = thread;
last_context = *context;
last_debug_context = l_debug_context;
}
static VALUE
call_at_line_unprotected(VALUE args)
{
VALUE context;
context = *RARRAY_PTR(args);
return rb_funcall2(context, idAtLine, (int)RARRAY_LEN(args) - 1, RARRAY_PTR(args) + 1);
}
static VALUE
call_at_line(VALUE context, debug_context_t *debug_context, VALUE file, VALUE line)
{
VALUE args;
last_debugged_thnum = debug_context->thnum;
save_current_position(debug_context);
args = rb_ary_new3(3, context, file, line);
return rb_protect(call_at_line_unprotected, args, 0);
}
static void
save_call_frame(rb_event_flag_t _event, debug_context_t *debug_context, VALUE self, char *file, int line, ID mid)
{
VALUE binding;
debug_frame_t *debug_frame;
int frame_n;
binding = self && RTEST(keep_frame_binding)? create_binding(self) : Qnil;
frame_n = debug_context->stack_size++;
if(frame_n >= debug_context->stack_len)
{
debug_context->stack_len += STACK_SIZE_INCREMENT;
debug_context->frames = REALLOC_N(debug_context->frames, debug_frame_t, debug_context->stack_len);
}
debug_frame = &debug_context->frames[frame_n];
debug_frame->file = file;
debug_frame->line = line;
debug_frame->binding = binding;
debug_frame->id = mid;
debug_frame->orig_id = mid;
debug_frame->dead = 0;
debug_frame->self = self;
debug_frame->arg_ary = Qnil;
debug_frame->argc = GET_THREAD()->cfp->iseq->argc;
debug_frame->info.runtime.cfp = GET_THREAD()->cfp;
debug_frame->info.runtime.bp = GET_THREAD()->cfp->bp;
debug_frame->info.runtime.block_iseq = GET_THREAD()->cfp->block_iseq;
debug_frame->info.runtime.block_pc = NULL;
debug_frame->info.runtime.last_pc = GET_THREAD()->cfp->pc;
if (RTEST(track_frame_args))
copy_scalar_args(debug_frame);
}
#if defined DOSISH
#define isdirsep(x) ((x) == '/' || (x) == '\\')
#else
#define isdirsep(x) ((x) == '/')
#endif
int
filename_cmp_impl(VALUE source, char *file);
int
filename_cmp(VALUE source, char *file) {
#ifdef _WIN32
return filename_cmp_impl(source, file);
#else
if (!RTEST(resolve_symlinks)) {
return filename_cmp_impl(source, file);
}
#ifdef PATH_MAX
{
char path[PATH_MAX + 1];
path[PATH_MAX] = 0;
if (realpath(file, path) != NULL)
return filename_cmp_impl(source, path);
else
return filename_cmp_impl(source, file);
}
#else
{
char *path = realpath(file, NULL);
result = filename_cmp_impl(source, path == NULL ? file : path);
free(path);
return result;
}
#endif
#endif
}
int
filename_cmp_impl(VALUE source, char *file)
{
char *source_ptr, *file_ptr;
long s_len, f_len, min_len;
long s,f;
int dirsep_flag = 0;
s_len = RSTRING_LEN(source);
f_len = strlen(file);
min_len = min(s_len, f_len);
source_ptr = RSTRING_PTR(source);
file_ptr = file;
for( s = s_len - 1, f = f_len - 1; s >= s_len - min_len && f >= f_len - min_len; s--, f-- )
{
if((source_ptr[s] == '.' || file_ptr[f] == '.') && dirsep_flag)
return 1;
if(isdirsep(source_ptr[s]) && isdirsep(file_ptr[f]))
dirsep_flag = 1;
#ifdef DOSISH_DRIVE_LETTER
else if (s == 0)
return(toupper(source_ptr[s]) == toupper(file_ptr[f]));
#endif
else if(source_ptr[s] != file_ptr[f])
return 0;
}
return 1;
}
static VALUE
create_binding(VALUE self)
{
return(rb_binding_new());
}
inline static debug_frame_t *
get_top_frame(debug_context_t *debug_context)
{
if(debug_context->stack_size == 0)
return NULL;
else
return &(debug_context->frames[debug_context->stack_size-1]);
}
inline static void
save_top_binding(debug_context_t *debug_context, VALUE binding)
{
debug_frame_t *debug_frame;
debug_frame = get_top_frame(debug_context);
if(debug_frame)
debug_frame->binding = binding;
}
inline static void
set_frame_source(rb_event_flag_t event, debug_context_t *debug_context, VALUE self, char *file, int line, ID mid)
{
debug_frame_t *top_frame;
top_frame = get_top_frame(debug_context);
if(top_frame)
{
top_frame->info.runtime.cfp = GET_THREAD()->cfp;
if (top_frame->info.runtime.block_iseq == GET_THREAD()->cfp->iseq)
{
top_frame->info.runtime.block_pc = GET_THREAD()->cfp->pc;
top_frame->binding = create_binding(self); /* block entered; need to rebind */
}
else if ((top_frame->info.runtime.block_pc != NULL) && (GET_THREAD()->cfp->pc == top_frame->info.runtime.block_pc))
{
top_frame->binding = create_binding(self); /* block re-entered; need to rebind */
}
top_frame->info.runtime.block_iseq = GET_THREAD()->cfp->block_iseq;
if (event == RUBY_EVENT_LINE)
top_frame->info.runtime.last_pc = GET_THREAD()->cfp->pc;
top_frame->self = self;
top_frame->file = file;
top_frame->line = line;
top_frame->id = mid;
}
}
inline static void
reset_frame_mid(debug_context_t *debug_context)
{
debug_frame_t *top_frame;
top_frame = get_top_frame(debug_context);
if(top_frame)
{
top_frame->id = 0;
}
}
static void
save_current_position(debug_context_t *debug_context)
{
debug_frame_t *debug_frame;
debug_frame = get_top_frame(debug_context);
if(!debug_frame) return;
debug_context->last_file = debug_frame->file;
debug_context->last_line = debug_frame->line;
CTX_FL_UNSET(debug_context, CTX_FL_ENABLE_BKPT);
CTX_FL_UNSET(debug_context, CTX_FL_STEPPED);
CTX_FL_UNSET(debug_context, CTX_FL_FORCE_MOVE);
}
inline static int
c_call_new_frame_p(VALUE klass, ID mid)
{
klass = real_class(klass);
// if ((klass == rb_mKernel) && (strcmp(rb_id2name(mid), "raise") == 0)) return 0;
if(rb_block_given_p()) return 1;
if(klass == rb_cProc || klass == rb_mKernel || klass == rb_cModule /*|| klass == rb_cFixnum*/) return 1;
return 0;
}
static void
call_at_line_check(VALUE self, debug_context_t *debug_context, VALUE breakpoint, VALUE context, char *file, int line)
{
VALUE binding = self? create_binding(self) : Qnil;
save_top_binding(debug_context, binding);
debug_context->stop_reason = CTX_STOP_STEP;
/* check breakpoint expression */
if(breakpoint != Qnil)
{
if(!check_breakpoint_expression(breakpoint, binding))
return;// TODO
if(!check_breakpoint_hit_condition(breakpoint))
return;// TODO
if(breakpoint != debug_context->breakpoint)
{
debug_context->stop_reason = CTX_STOP_BREAKPOINT;
rb_funcall(context, idAtBreakpoint, 1, breakpoint);
}
else
debug_context->breakpoint = Qnil;
}
reset_stepping_stop_points(debug_context);
call_at_line(context, debug_context, rb_str_new2(file), INT2FIX(line));
}
static struct iseq_catch_table_entry *
create_catch_table(debug_context_t *debug_context, unsigned long cont)
{
struct iseq_catch_table_entry *catch_table = &debug_context->catch_table.tmp_catch_table;
GET_THREAD()->parse_in_eval++;
GET_THREAD()->mild_compile_error++;
/* compiling with option Qfalse (no options) prevents debug hook calls during this catch routine */
#ifdef RB_ISEQ_COMPILE_5ARGS
catch_table->iseq = rb_iseq_compile_with_option(
rb_str_new_cstr("begin\nend"), rb_str_new_cstr("(exception catcher)"), Qnil, INT2FIX(1), Qfalse);
#else
catch_table->iseq = rb_iseq_compile_with_option(
rb_str_new_cstr("begin\nend"), rb_str_new_cstr("(exception catcher)"), INT2FIX(1), Qfalse);
#endif
GET_THREAD()->mild_compile_error--;
GET_THREAD()->parse_in_eval--;
catch_table->type = CATCH_TYPE_RESCUE;
catch_table->start = 0;
catch_table->end = ULONG_MAX;
catch_table->cont = cont;
catch_table->sp = 0;
return(catch_table);
}
static int
set_thread_event_flag_i(st_data_t key, st_data_t val, st_data_t flag)
{
VALUE thval = key;
rb_thread_t *th;
GetThreadPtr(thval, th);
th->event_flags |= RUBY_EVENT_VM;
return(ST_CONTINUE);
}
static void
debug_event_hook(rb_event_flag_t event, VALUE data, VALUE self, ID mid, VALUE klass)
{
VALUE context;
VALUE breakpoint = Qnil, binding = Qnil;
debug_context_t *debug_context;
char *file = (char*)rb_sourcefile();
int line = rb_sourceline();
int moved = 0;
rb_thread_t *thread = GET_THREAD();
struct rb_iseq_struct *iseq = thread->cfp->iseq;
hook_count++;
if (((iseq == NULL) || (file == NULL)) && (event != RUBY_EVENT_RAISE))
return;
thread_context_lookup(thread->self, &context, &debug_context, 1);
if ((event == RUBY_EVENT_LINE) || (event == RUBY_EVENT_CALL))
{
mid = iseq->defined_method_id;
klass = iseq->klass;
}
if (mid == ID_ALLOCATOR) return;
/* return if thread is marked as 'ignored'.
debugger's threads are marked this way
*/
if(CTX_FL_TEST(debug_context, CTX_FL_IGNORE)) return;
while(1)
{
/* halt execution of the current thread if the debugger
is activated in another
*/
while(locker != Qnil && locker != thread->self)
{
add_to_locked(thread->self);
rb_thread_stop();
}
/* stop the current thread if it's marked as suspended */
if(CTX_FL_TEST(debug_context, CTX_FL_SUSPEND) && locker != thread->self)
{
CTX_FL_SET(debug_context, CTX_FL_WAS_RUNNING);
rb_thread_stop();
}
else break;
}
/* return if the current thread is the locker */
if (locker != Qnil) return;
/* only the current thread can proceed */
locker = thread->self;
/* restore catch tables removed for jump */
if (debug_context->old_iseq_catch != NULL)
{
int i = 0;
while (debug_context->old_iseq_catch[i].iseq != NULL)
{
debug_context->old_iseq_catch[i].iseq->catch_table = debug_context->old_iseq_catch[i].catch_table;
debug_context->old_iseq_catch[i].iseq->catch_table_size = debug_context->old_iseq_catch[i].catch_table_size;
i++;
}
free(debug_context->old_iseq_catch);
debug_context->old_iseq_catch = NULL;
}
/* make sure all threads have event flag set so we'll get its events */
st_foreach(thread->vm->living_threads, set_thread_event_flag_i, 0);
/* remove any frames that are now out of scope */
while(debug_context->stack_size > 0)
{
if (debug_context->frames[debug_context->stack_size - 1].info.runtime.bp <= thread->cfp->bp)
break;
debug_context->stack_size--;
}
if (debug_context->thread_pause)
{
debug_context->thread_pause = 0;
debug_context->stop_next = 1;
debug_context->dest_frame = -1;
moved = 1;
}
else
{
/* ignore a skipped section of code */
if (CTX_FL_TEST(debug_context, CTX_FL_SKIPPED))
goto cleanup;
if ((event == RUBY_EVENT_LINE) && (debug_context->stack_size > 0) &&
(get_top_frame(debug_context)->line == line) && (get_top_frame(debug_context)->info.runtime.cfp->iseq == iseq) &&
!CTX_FL_TEST(debug_context, CTX_FL_CATCHING))
{
/* Sometimes duplicate RUBY_EVENT_LINE messages get generated by the compiler.
* Ignore them. */
goto cleanup;
}
}
if(debug == Qtrue)
fprintf(stderr, "%s:%d [%s] %s\n", file, line, get_event_name(event), rb_id2name(mid));
/* There can be many event calls per line, but we only want
*one* breakpoint per line. */
if(!CTX_FL_TEST(debug_context, CTX_FL_FORCE_MOVE) ||
debug_context->last_line != line || debug_context->last_file == NULL ||
strcmp(debug_context->last_file, file) != 0)
{
CTX_FL_SET(debug_context, CTX_FL_ENABLE_BKPT);
moved = 1;
}
if(event != RUBY_EVENT_LINE)
CTX_FL_SET(debug_context, CTX_FL_STEPPED);
switch(event)
{
case RUBY_EVENT_LINE:
{
if (CTX_FL_TEST(debug_context, CTX_FL_CATCHING))
{
debug_frame_t *top_frame = get_top_frame(debug_context);
if (top_frame != NULL)
{
rb_control_frame_t *cfp = top_frame->info.runtime.cfp;
VALUE hit_count;
int c_hit_count;
rb_iseq_t *iseq = cfp->iseq;
if (iseq != NULL) {
/* restore the proper catch table */
iseq->catch_table_size = debug_context->catch_table.old_catch_table_size;
iseq->catch_table = debug_context->catch_table.old_catch_table;
}
/* send catchpoint notification */
c_hit_count = FIX2INT(rb_hash_aref(rdebug_catchpoints, debug_context->catch_table.mod_name)) + 1;
hit_count = INT2FIX(c_hit_count);
rb_hash_aset(rdebug_catchpoints, debug_context->catch_table.mod_name, hit_count);
debug_context->stop_reason = CTX_STOP_CATCHPOINT;
rb_funcall(context, idAtCatchpoint, 1, debug_context->catch_table.errinfo);
if(self && binding == Qnil)
binding = create_binding(self);
save_top_binding(debug_context, binding);
call_at_line(context, debug_context, rb_str_new2(top_frame->file), INT2FIX(top_frame->line));
}
break;
}
if(debug_context->stack_size == 0 ||
get_top_frame(debug_context)->info.runtime.block_iseq != thread->cfp->block_iseq ||
get_top_frame(debug_context)->info.runtime.cfp->iseq != thread->cfp->iseq)
save_call_frame(event, debug_context, self, file, line, mid);
else
set_frame_source(event, debug_context, self, file, line, mid);
if(RTEST(tracing) || CTX_FL_TEST(debug_context, CTX_FL_TRACING))
rb_funcall(context, idAtTracing, 2, rb_str_new2(file), INT2FIX(line));
if(debug_context->dest_frame == -1 ||
debug_context->stack_size == debug_context->dest_frame)
{
if(moved || !CTX_FL_TEST(debug_context, CTX_FL_FORCE_MOVE))
debug_context->stop_next--;
if(debug_context->stop_next < 0)
debug_context->stop_next = -1;
if(moved || (CTX_FL_TEST(debug_context, CTX_FL_STEPPED) &&
!CTX_FL_TEST(debug_context, CTX_FL_FORCE_MOVE)))
{
debug_context->stop_line--;
CTX_FL_UNSET(debug_context, CTX_FL_STEPPED);
}
}
else if(debug_context->stack_size < debug_context->dest_frame)
{
debug_context->stop_next = 0;
}
if(debug_context->stop_next == 0 || debug_context->stop_line == 0 ||
(breakpoint = check_breakpoints_by_pos(debug_context, file, line)) != Qnil)
{
call_at_line_check(self, debug_context, breakpoint, context, file, line);
}
break;
}
case RUBY_EVENT_CALL:
{
save_call_frame(event, debug_context, self, file, line, mid);
breakpoint = check_breakpoints_by_method(debug_context, klass, mid, self);
if(breakpoint != Qnil)
{
debug_frame_t *debug_frame;
debug_frame = get_top_frame(debug_context);
if(debug_frame)
binding = debug_frame->binding;
if(NIL_P(binding) && self)
binding = create_binding(self);
save_top_binding(debug_context, binding);
if(!check_breakpoint_expression(breakpoint, binding))
break;
if(!check_breakpoint_hit_condition(breakpoint))
break;
if(breakpoint != debug_context->breakpoint)
{
debug_context->stop_reason = CTX_STOP_BREAKPOINT;
rb_funcall(context, idAtBreakpoint, 1, breakpoint);
}
else
debug_context->breakpoint = Qnil;
call_at_line(context, debug_context, rb_str_new2(file), INT2FIX(line));
break;
}
breakpoint = check_breakpoints_by_pos(debug_context, file, line);
if (breakpoint != Qnil)
call_at_line_check(self, debug_context, breakpoint, context, file, line);
break;
}
case RUBY_EVENT_C_CALL:
{
if(c_call_new_frame_p(klass, mid))
save_call_frame(event, debug_context, self, file, line, mid);
else
set_frame_source(event, debug_context, self, file, line, mid);
break;
}
case RUBY_EVENT_C_RETURN:
{
/* note if a block is given we fall through! */
if(!rb_method_boundp(klass, mid, 0) || !c_call_new_frame_p(klass, mid))
break;
}
case RUBY_EVENT_RETURN:
case RUBY_EVENT_END:
{
if(debug_context->stack_size == debug_context->stop_frame)
{
debug_context->stop_next = 1;
debug_context->stop_frame = 0;
/* NOTE: can't use call_at_line function here to trigger a debugger event.
this can lead to segfault. We should only unroll the stack on this event.
*/
}
while(debug_context->stack_size > 0)
{
debug_context->stack_size--;
if (debug_context->frames[debug_context->stack_size].info.runtime.bp <= GET_THREAD()->cfp->bp)
break;
}
CTX_FL_SET(debug_context, CTX_FL_ENABLE_BKPT);
break;
}
case RUBY_EVENT_CLASS:
{
reset_frame_mid(debug_context);