forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduk_debugger.c
1780 lines (1522 loc) · 46.7 KB
/
duk_debugger.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
/*
* Duktape debugger
*/
#include "duk_internal.h"
#if defined(DUK_USE_DEBUGGER_SUPPORT)
/*
* Helper structs
*/
typedef union {
void *p;
duk_uint_t b[1];
/* Use b[] to access the size of the union, which is strictly not
* correct. Can't use fixed size unless there's feature detection
* for pointer byte size.
*/
} duk__ptr_union;
/*
* Detach handling
*/
#define DUK__SET_CONN_BROKEN(thr) do { \
/* For now shared handler is fine. */ \
duk_debug_do_detach((thr)->heap); \
} while (0)
DUK_INTERNAL void duk_debug_do_detach(duk_heap *heap) {
/* Can be called muliple times with no harm. */
heap->dbg_read_cb = NULL;
heap->dbg_write_cb = NULL;
heap->dbg_peek_cb = NULL;
heap->dbg_read_flush_cb = NULL;
heap->dbg_write_flush_cb = NULL;
if (heap->dbg_detached_cb) {
heap->dbg_detached_cb(heap->dbg_udata);
}
heap->dbg_detached_cb = NULL;
heap->dbg_udata = NULL;
heap->dbg_processing = 0;
heap->dbg_paused = 0;
heap->dbg_state_dirty = 0;
heap->dbg_step_type = 0;
heap->dbg_step_thread = NULL;
heap->dbg_step_csindex = 0;
heap->dbg_step_startline = 0;
/* Ensure there are no stale active breakpoint pointers.
* Breakpoint list is currently kept - we could empty it
* here but we'd need to handle refcounts correctly, and
* we'd need a 'thr' reference for that.
*
* XXX: clear breakpoint on either attach or detach?
*/
heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
}
/*
* Debug connection peek and flush primitives
*/
DUK_INTERNAL duk_bool_t duk_debug_read_peek(duk_hthread *thr) {
duk_heap *heap;
DUK_ASSERT(thr != NULL);
heap = thr->heap;
DUK_ASSERT(heap != NULL);
if (heap->dbg_read_cb == NULL) {
DUK_D(DUK_DPRINT("attempt to peek in detached state, return zero (= no data)"));
return 0;
}
if (heap->dbg_peek_cb == NULL) {
DUK_DD(DUK_DDPRINT("no peek callback, return zero (= no data)"));
return 0;
}
return (duk_bool_t) (heap->dbg_peek_cb(heap->dbg_udata) > 0);
}
DUK_INTERNAL void duk_debug_read_flush(duk_hthread *thr) {
duk_heap *heap;
DUK_ASSERT(thr != NULL);
heap = thr->heap;
DUK_ASSERT(heap != NULL);
if (heap->dbg_read_cb == NULL) {
DUK_D(DUK_DPRINT("attempt to read flush in detached state, ignore"));
return;
}
if (heap->dbg_read_flush_cb == NULL) {
DUK_DD(DUK_DDPRINT("no read flush callback, ignore"));
return;
}
heap->dbg_read_flush_cb(heap->dbg_udata);
}
DUK_INTERNAL void duk_debug_write_flush(duk_hthread *thr) {
duk_heap *heap;
DUK_ASSERT(thr != NULL);
heap = thr->heap;
DUK_ASSERT(heap != NULL);
if (heap->dbg_read_cb == NULL) {
DUK_D(DUK_DPRINT("attempt to write flush in detached state, ignore"));
return;
}
if (heap->dbg_write_flush_cb == NULL) {
DUK_DD(DUK_DDPRINT("no write flush callback, ignore"));
return;
}
heap->dbg_write_flush_cb(heap->dbg_udata);
}
/*
* Debug connection skip primitives
*/
/* Skip fully. */
DUK_INTERNAL void duk_debug_skip_bytes(duk_hthread *thr, duk_size_t length) {
duk_uint8_t dummy[64];
duk_size_t now;
DUK_ASSERT(thr != NULL);
while (length > 0) {
now = (length > sizeof(dummy) ? sizeof(dummy) : length);
duk_debug_read_bytes(thr, dummy, now);
length -= now;
}
}
DUK_INTERNAL void duk_debug_skip_byte(duk_hthread *thr) {
DUK_ASSERT(thr != NULL);
(void) duk_debug_read_byte(thr);
}
/*
* Debug connection read primitives
*/
/* Read fully. */
DUK_INTERNAL void duk_debug_read_bytes(duk_hthread *thr, duk_uint8_t *data, duk_size_t length) {
duk_heap *heap;
duk_uint8_t *p;
duk_size_t left;
duk_size_t got;
DUK_ASSERT(thr != NULL);
heap = thr->heap;
DUK_ASSERT(heap != NULL);
if (heap->dbg_read_cb == NULL) {
DUK_D(DUK_DPRINT("attempt to read %ld bytes in detached state, return zero data", (long) length));
goto fail;
}
p = data;
for (;;) {
left = (duk_size_t) ((data + length) - p);
if (left == 0) {
break;
}
DUK_ASSERT(heap->dbg_read_cb != NULL);
DUK_ASSERT(left >= 1);
#if defined(DUK_USE_DEBUGGER_TRANSPORT_TORTURE)
left = 1;
#endif
got = heap->dbg_read_cb(heap->dbg_udata, (char *) p, left);
if (got == 0 || got > left) {
DUK_D(DUK_DPRINT("connection error during read, return zero data"));
DUK__SET_CONN_BROKEN(thr);
goto fail;
}
p += got;
}
return;
fail:
DUK_MEMZERO((void *) data, (size_t) length);
}
DUK_INTERNAL duk_uint8_t duk_debug_read_byte(duk_hthread *thr) {
duk_heap *heap;
duk_size_t got;
duk_uint8_t x;
DUK_ASSERT(thr != NULL);
heap = thr->heap;
DUK_ASSERT(heap != NULL);
if (heap->dbg_read_cb == NULL) {
DUK_D(DUK_DPRINT("attempt to read 1 bytes in detached state, return zero data"));
return 0;
}
x = 0; /* just in case callback is broken and won't write 'x' */
DUK_ASSERT(heap->dbg_read_cb != NULL);
got = heap->dbg_read_cb(heap->dbg_udata, (char *) (&x), 1);
if (got != 1) {
DUK_D(DUK_DPRINT("connection error during read, return zero data"));
DUK__SET_CONN_BROKEN(thr);
return 0;
}
return x;
}
DUK_LOCAL duk_uint32_t duk__debug_read_uint32_raw(duk_hthread *thr) {
duk_uint8_t buf[4];
DUK_ASSERT(thr != NULL);
duk_debug_read_bytes(thr, buf, 4);
return ((duk_uint32_t) buf[0] << 24) |
((duk_uint32_t) buf[1] << 16) |
((duk_uint32_t) buf[2] << 8) |
(duk_uint32_t) buf[3];
}
DUK_LOCAL duk_uint32_t duk__debug_read_int32_raw(duk_hthread *thr) {
return (duk_int32_t) duk__debug_read_uint32_raw(thr);
}
DUK_LOCAL duk_uint16_t duk__debug_read_uint16_raw(duk_hthread *thr) {
duk_uint8_t buf[2];
DUK_ASSERT(thr != NULL);
duk_debug_read_bytes(thr, buf, 2);
return ((duk_uint16_t) buf[0] << 8) |
(duk_uint16_t) buf[1];
}
DUK_INTERNAL duk_int32_t duk_debug_read_int(duk_hthread *thr) {
duk_small_uint_t x;
duk_small_uint_t t;
DUK_ASSERT(thr != NULL);
x = duk_debug_read_byte(thr);
if (x >= 0xc0) {
t = duk_debug_read_byte(thr);
return (duk_int32_t) (((x - 0xc0) << 8) + t);
} else if (x >= 0x80) {
return (duk_int32_t) (x - 0x80);
} else if (x == 0x10) {
return (duk_int32_t) duk__debug_read_uint32_raw(thr);
}
DUK_D(DUK_DPRINT("debug connection error: failed to decode int"));
DUK__SET_CONN_BROKEN(thr);
return 0;
}
DUK_LOCAL duk_hstring *duk__debug_read_hstring_raw(duk_hthread *thr, duk_uint32_t len) {
duk_context *ctx = (duk_context *) thr;
duk_uint8_t buf[31];
duk_uint8_t *p;
if (len <= sizeof(buf)) {
duk_debug_read_bytes(thr, buf, (duk_size_t) len);
duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len);
} else {
p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
DUK_ASSERT(p != NULL);
duk_debug_read_bytes(thr, p, (duk_size_t) len);
duk_to_string(ctx, -1);
}
return duk_require_hstring(ctx, -1);
}
DUK_INTERNAL duk_hstring *duk_debug_read_hstring(duk_hthread *thr) {
duk_context *ctx = (duk_context *) thr;
duk_small_uint_t x;
duk_uint32_t len;
DUK_ASSERT(thr != NULL);
x = duk_debug_read_byte(thr);
if (x >= 0x60 && x <= 0x7f) {
/* For short strings, use a fixed temp buffer. */
len = (duk_uint32_t) (x - 0x60);
} else if (x == 0x12) {
len = (duk_uint32_t) duk__debug_read_uint16_raw(thr);
} else if (x == 0x11) {
len = (duk_uint32_t) duk__debug_read_uint32_raw(thr);
} else {
goto fail;
}
return duk__debug_read_hstring_raw(thr, len);
fail:
DUK_D(DUK_DPRINT("debug connection error: failed to decode int"));
DUK__SET_CONN_BROKEN(thr);
duk_push_hstring_stridx(thr, DUK_STRIDX_EMPTY_STRING); /* always push some string */
return duk_require_hstring(ctx, -1);
}
DUK_LOCAL duk_hbuffer *duk__debug_read_hbuffer_raw(duk_hthread *thr, duk_uint32_t len) {
duk_context *ctx = (duk_context *) thr;
duk_uint8_t *p;
p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
DUK_ASSERT(p != NULL);
duk_debug_read_bytes(thr, p, (duk_size_t) len);
return duk_require_hbuffer(ctx, -1);
}
DUK_LOCAL const void *duk__debug_read_pointer_raw(duk_hthread *thr) {
duk_small_uint_t x;
volatile duk__ptr_union pu;
DUK_ASSERT(thr != NULL);
x = duk_debug_read_byte(thr);
if (x != sizeof(pu)) {
goto fail;
}
duk_debug_read_bytes(thr, (duk_uint8_t *) &pu.p, sizeof(pu));
#if defined(DUK_USE_INTEGER_LE)
duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
#endif
return (const void *) pu.p;
fail:
DUK_D(DUK_DPRINT("debug connection error: failed to decode pointer"));
DUK__SET_CONN_BROKEN(thr);
return (const void *) NULL;
}
DUK_LOCAL duk_double_t duk__debug_read_double_raw(duk_hthread *thr) {
duk_double_union du;
DUK_ASSERT(sizeof(du.uc) == 8);
duk_debug_read_bytes(thr, (duk_uint8_t *) du.uc, sizeof(du.uc));
DUK_DBLUNION_DOUBLE_NTOH(&du);
return du.d;
}
DUK_INTERNAL void duk_debug_read_tval(duk_hthread *thr) {
duk_context *ctx = (duk_context *) thr;
duk_uint8_t x;
duk_uint_t t;
duk_uint32_t len;
DUK_ASSERT(thr != NULL);
x = duk_debug_read_byte(thr);
if (x >= 0xc0) {
t = (duk_uint_t) (x - 0xc0);
t = (t << 8) + duk_debug_read_byte(thr);
duk_push_uint(ctx, (duk_uint_t) t);
return;
}
if (x >= 0x80) {
duk_push_uint(ctx, (duk_uint_t) (x - 0x80));
return;
}
if (x >= 0x60) {
len = (duk_uint32_t) (x - 0x60);
duk__debug_read_hstring_raw(thr, len);
return;
}
switch (x) {
case 0x10: {
duk_int32_t i = duk__debug_read_int32_raw(thr);
duk_push_i32(ctx, i);
break;
}
case 0x11:
len = duk__debug_read_uint32_raw(thr);
duk__debug_read_hstring_raw(thr, len);
break;
case 0x12:
len = duk__debug_read_uint16_raw(thr);
duk__debug_read_hstring_raw(thr, len);
break;
case 0x13:
len = duk__debug_read_uint32_raw(thr);
duk__debug_read_hbuffer_raw(thr, len);
break;
case 0x14:
len = duk__debug_read_uint16_raw(thr);
duk__debug_read_hbuffer_raw(thr, len);
break;
case 0x15:
duk_push_unused(ctx);
break;
case 0x16:
duk_push_undefined(ctx);
break;
case 0x17:
duk_push_null(ctx);
break;
case 0x18:
duk_push_true(ctx);
break;
case 0x19:
duk_push_false(ctx);
break;
case 0x1a: {
duk_double_t d;
d = duk__debug_read_double_raw(thr);
duk_push_number(ctx, d);
break;
}
case 0x1b:
/* XXX: not needed for now, so not implemented */
DUK_D(DUK_DPRINT("reading object values unimplemented"));
goto fail;
case 0x1c: {
const void *ptr;
ptr = duk__debug_read_pointer_raw(thr);
duk_push_pointer(thr, (void *) ptr);
break;
}
case 0x1d:
/* XXX: not needed for now, so not implemented */
DUK_D(DUK_DPRINT("reading lightfunc values unimplemented"));
goto fail;
case 0x1e: {
duk_heaphdr *h;
h = (duk_heaphdr *) duk__debug_read_pointer_raw(thr);
duk_push_heapptr(thr, (void *) h);
break;
}
default:
goto fail;
}
return;
fail:
DUK_D(DUK_DPRINT("debug connection error: failed to decode tval"));
DUK__SET_CONN_BROKEN(thr);
}
/*
* Debug connection write primitives
*/
/* Write fully. */
DUK_INTERNAL void duk_debug_write_bytes(duk_hthread *thr, const duk_uint8_t *data, duk_size_t length) {
duk_heap *heap;
const duk_uint8_t *p;
duk_size_t left;
duk_size_t got;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(length == 0 || data != NULL);
heap = thr->heap;
DUK_ASSERT(heap != NULL);
if (heap->dbg_write_cb == NULL) {
DUK_D(DUK_DPRINT("attempt to write %ld bytes in detached state, ignore", (long) length));
return;
}
if (length == 0) {
/* Avoid doing an actual write callback with length == 0,
* because that's reserved for a write flush.
*/
return;
}
DUK_ASSERT(data != NULL);
p = data;
for (;;) {
left = (duk_size_t) ((data + length) - p);
if (left == 0) {
break;
}
DUK_ASSERT(heap->dbg_write_cb != NULL);
DUK_ASSERT(left >= 1);
#if defined(DUK_USE_DEBUGGER_TRANSPORT_TORTURE)
left = 1;
#endif
got = heap->dbg_write_cb(heap->dbg_udata, (const char *) p, left);
if (got == 0 || got > left) {
DUK_D(DUK_DPRINT("connection error during write"));
DUK__SET_CONN_BROKEN(thr);
return;
}
p += got;
}
}
DUK_INTERNAL void duk_debug_write_byte(duk_hthread *thr, duk_uint8_t x) {
duk_heap *heap;
duk_size_t got;
DUK_ASSERT(thr != NULL);
heap = thr->heap;
DUK_ASSERT(heap != NULL);
if (heap->dbg_write_cb == NULL) {
DUK_D(DUK_DPRINT("attempt to write 1 bytes in detached state, ignore"));
return;
}
DUK_ASSERT(heap->dbg_write_cb != NULL);
got = heap->dbg_write_cb(heap->dbg_udata, (const char *) (&x), 1);
if (got != 1) {
DUK_D(DUK_DPRINT("connection error during write"));
DUK__SET_CONN_BROKEN(thr);
}
}
DUK_INTERNAL void duk_debug_write_unused(duk_hthread *thr) {
duk_debug_write_byte(thr, 0x15);
}
DUK_INTERNAL void duk_debug_write_undefined(duk_hthread *thr) {
duk_debug_write_byte(thr, 0x16);
}
/* Write signed 32-bit integer. */
DUK_INTERNAL void duk_debug_write_int(duk_hthread *thr, duk_int32_t x) {
duk_uint8_t buf[5];
duk_size_t len;
DUK_ASSERT(thr != NULL);
if (x >= 0 && x <= 0x3fL) {
buf[0] = (duk_uint8_t) (0x80 + x);
len = 1;
} else if (x >= 0 && x <= 0x3fffL) {
buf[0] = (duk_uint8_t) (0xc0 + (x >> 8));
buf[1] = (duk_uint8_t) (x & 0xff);
len = 2;
} else {
/* Signed integers always map to 4 bytes now. */
buf[0] = (duk_uint8_t) 0x10;
buf[1] = (duk_uint8_t) ((x >> 24) & 0xff);
buf[2] = (duk_uint8_t) ((x >> 16) & 0xff);
buf[3] = (duk_uint8_t) ((x >> 8) & 0xff);
buf[4] = (duk_uint8_t) (x & 0xff);
len = 5;
}
duk_debug_write_bytes(thr, buf, len);
}
/* Write unsigned 32-bit integer. */
DUK_INTERNAL void duk_debug_write_uint(duk_hthread *thr, duk_uint32_t x) {
/* XXX: there's currently no need to support full 32-bit unsigned
* integer range in practice. If that becomes necessary, add a new
* dvalue type or encode as an IEEE double.
*/
duk_debug_write_int(thr, (duk_int32_t) x);
}
DUK_INTERNAL void duk_debug_write_strbuf(duk_hthread *thr, const char *data, duk_size_t length, duk_uint8_t marker_base) {
duk_uint8_t buf[5];
duk_size_t buflen;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(length == 0 || data != NULL);
if (length <= 0x1fUL && marker_base == 0x11) {
/* For strings, special form for short lengths. */
buf[0] = (duk_uint8_t) (0x60 + length);
buflen = 1;
} else if (length <= 0xffffUL) {
buf[0] = (duk_uint8_t) (marker_base + 1);
buf[1] = (duk_uint8_t) (length >> 8);
buf[2] = (duk_uint8_t) (length & 0xff);
buflen = 3;
} else {
buf[0] = (duk_uint8_t) marker_base;
buf[1] = (duk_uint8_t) (length >> 24);
buf[2] = (duk_uint8_t) ((length >> 16) & 0xff);
buf[3] = (duk_uint8_t) ((length >> 8) & 0xff);
buf[4] = (duk_uint8_t) (length & 0xff);
buflen = 5;
}
duk_debug_write_bytes(thr, (const duk_uint8_t *) buf, buflen);
duk_debug_write_bytes(thr, (const duk_uint8_t *) data, length);
}
DUK_INTERNAL void duk_debug_write_string(duk_hthread *thr, const char *data, duk_size_t length) {
duk_debug_write_strbuf(thr, data, length, 0x11);
}
DUK_INTERNAL void duk_debug_write_cstring(duk_hthread *thr, const char *data) {
DUK_ASSERT(thr != NULL);
duk_debug_write_string(thr,
data,
data ? DUK_STRLEN(data) : 0);
}
DUK_INTERNAL void duk_debug_write_hstring(duk_hthread *thr, duk_hstring *h) {
DUK_ASSERT(thr != NULL);
/* XXX: differentiate null pointer from empty string? */
duk_debug_write_string(thr,
(h != NULL ? (const char *) DUK_HSTRING_GET_DATA(h) : NULL),
(h != NULL ? (duk_size_t) DUK_HSTRING_GET_BYTELEN(h) : 0));
}
DUK_INTERNAL void duk_debug_write_buffer(duk_hthread *thr, const char *data, duk_size_t length) {
duk_debug_write_strbuf(thr, data, length, 0x13);
}
DUK_INTERNAL void duk_debug_write_hbuffer(duk_hthread *thr, duk_hbuffer *h) {
DUK_ASSERT(thr != NULL);
duk_debug_write_buffer(thr,
(h != NULL ? (const char *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h) : NULL),
(h != NULL ? (duk_size_t) DUK_HBUFFER_GET_SIZE(h) : 0));
}
DUK_LOCAL void duk__debug_write_pointer_raw(duk_hthread *thr, const void *ptr, duk_uint8_t ibyte) {
duk_uint8_t buf[2];
volatile duk__ptr_union pu;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(sizeof(ptr) >= 1 && sizeof(ptr) <= 16);
/* ptr may be NULL */
buf[0] = ibyte;
buf[1] = sizeof(pu);
duk_debug_write_bytes(thr, buf, 2);
pu.p = (void *) ptr;
#if defined(DUK_USE_INTEGER_LE)
duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
#endif
duk_debug_write_bytes(thr, (const duk_uint8_t *) &pu.p, (duk_size_t) sizeof(pu));
}
DUK_INTERNAL void duk_debug_write_pointer(duk_hthread *thr, const void *ptr) {
duk__debug_write_pointer_raw(thr, ptr, 0x1c);
}
#if defined(DUK_USE_DEBUGGER_DUMPHEAP)
DUK_INTERNAL void duk_debug_write_heapptr(duk_hthread *thr, duk_heaphdr *h) {
duk__debug_write_pointer_raw(thr, (const void *) h, 0x1e);
}
#endif /* DUK_USE_DEBUGGER_DUMPHEAP */
DUK_INTERNAL void duk_debug_write_hobject(duk_hthread *thr, duk_hobject *obj) {
duk_uint8_t buf[3];
volatile duk__ptr_union pu;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(sizeof(obj) >= 1 && sizeof(obj) <= 16);
DUK_ASSERT(obj != NULL);
buf[0] = 0x1b;
buf[1] = (duk_uint8_t) DUK_HOBJECT_GET_CLASS_NUMBER(obj);
buf[2] = sizeof(pu);
duk_debug_write_bytes(thr, buf, 3);
pu.p = (void *) obj;
#if defined(DUK_USE_INTEGER_LE)
duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
#endif
duk_debug_write_bytes(thr, (const duk_uint8_t *) &pu.p, (duk_size_t) sizeof(pu));
}
DUK_INTERNAL void duk_debug_write_tval(duk_hthread *thr, duk_tval *tv) {
duk_c_function lf_func;
duk_small_uint_t lf_flags;
duk_uint8_t buf[4];
duk_double_union du;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(tv != NULL);
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_UNDEFINED:
duk_debug_write_byte(thr,
DUK_TVAL_IS_UNDEFINED_UNUSED(tv) ? 0x15 : 0x16);
break;
case DUK_TAG_NULL:
duk_debug_write_byte(thr, 0x17);
break;
case DUK_TAG_BOOLEAN:
DUK_ASSERT(DUK_TVAL_GET_BOOLEAN(tv) == 0 ||
DUK_TVAL_GET_BOOLEAN(tv) == 1);
duk_debug_write_byte(thr, DUK_TVAL_GET_BOOLEAN(tv) ? 0x18 : 0x19);
break;
case DUK_TAG_POINTER:
duk_debug_write_pointer(thr, (const void *) DUK_TVAL_GET_POINTER(tv));
break;
case DUK_TAG_LIGHTFUNC:
DUK_TVAL_GET_LIGHTFUNC(tv, lf_func, lf_flags);
buf[0] = 0x1d;
buf[1] = (duk_uint8_t) (lf_flags >> 8);
buf[2] = (duk_uint8_t) (lf_flags & 0xff);
buf[3] = sizeof(lf_func);
duk_debug_write_bytes(thr, buf, 4);
duk_debug_write_bytes(thr, (const duk_uint8_t *) &lf_func, sizeof(lf_func));
break;
case DUK_TAG_STRING:
duk_debug_write_hstring(thr, DUK_TVAL_GET_STRING(tv));
break;
case DUK_TAG_OBJECT:
duk_debug_write_hobject(thr, DUK_TVAL_GET_OBJECT(tv));
break;
case DUK_TAG_BUFFER:
duk_debug_write_hbuffer(thr, DUK_TVAL_GET_BUFFER(tv));
break;
#if defined(DUK_USE_FASTINT)
case DUK_TAG_FASTINT:
#endif
default:
/* Numbers are normalized to big (network) endian. */
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
du.d = DUK_TVAL_GET_NUMBER(tv);
DUK_DBLUNION_DOUBLE_HTON(&du);
duk_debug_write_byte(thr, 0x1a);
duk_debug_write_bytes(thr, (const duk_uint8_t *) du.uc, sizeof(du.uc));
}
}
#if defined(DUK_USE_DEBUGGER_DUMPHEAP)
/* Variant for writing duk_tvals so that any heap allocated values are
* written out as tagged heap pointers.
*/
DUK_LOCAL void duk__debug_write_tval_heapptr(duk_hthread *thr, duk_tval *tv) {
if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
duk_debug_write_heapptr(thr, h);
} else {
duk_debug_write_tval(thr, tv);
}
}
#endif /* DUK_USE_DEBUGGER_DUMPHEAP */
/*
* Debug connection message write helpers
*/
#if 0 /* unused */
DUK_INTERNAL void duk_debug_write_request(duk_hthread *thr, duk_small_uint_t command) {
duk_debug_write_byte(thr, DUK_DBG_MARKER_REQUEST);
duk_debug_write_int(thr, command);
}
#endif
DUK_INTERNAL void duk_debug_write_reply(duk_hthread *thr) {
duk_debug_write_byte(thr, DUK_DBG_MARKER_REPLY);
}
DUK_INTERNAL void duk_debug_write_error_eom(duk_hthread *thr, duk_small_uint_t err_code, const char *msg) {
/* Allow NULL 'msg' */
duk_debug_write_byte(thr, DUK_DBG_MARKER_ERROR);
duk_debug_write_int(thr, (duk_int32_t) err_code);
duk_debug_write_cstring(thr, msg);
duk_debug_write_eom(thr);
}
DUK_INTERNAL void duk_debug_write_notify(duk_hthread *thr, duk_small_uint_t command) {
duk_debug_write_byte(thr, DUK_DBG_MARKER_NOTIFY);
duk_debug_write_int(thr, command);
}
DUK_INTERNAL void duk_debug_write_eom(duk_hthread *thr) {
duk_debug_write_byte(thr, DUK_DBG_MARKER_EOM);
/* As an initial implementation, write flush after every EOM (and the
* version identifier). A better implementation would flush only when
* Duktape is finished processing messages so that a flush only happens
* after all outbound messages are finished on that occasion.
*/
duk_debug_write_flush(thr);
}
/*
* Status message and helpers
*/
DUK_INTERNAL duk_uint_fast32_t duk_debug_curr_line(duk_hthread *thr) {
duk_context *ctx = (duk_context *) thr;
duk_activation *act;
duk_uint_fast32_t line;
duk_uint_fast32_t pc;
if (thr->callstack_top == 0) {
return 0;
}
act = thr->callstack + thr->callstack_top - 1;
/* We're conceptually between two opcodes; act->pc indicates the next
* instruction to be executed. This is usually the correct pc/line to
* indicate in Status. (For the 'debugger' statement this now reports
* the pc/line after the debugger statement because the debugger opcode
* has already been executed.)
*/
pc = duk_hthread_get_act_curr_pc(thr, act);
/* XXX: this should be optimized to be a raw query and avoid valstack
* operations if possible.
*/
duk_push_hobject(ctx, act->func);
line = duk_hobject_pc2line_query(ctx, -1, pc);
duk_pop(ctx);
return line;
}
DUK_INTERNAL void duk_debug_send_status(duk_hthread *thr) {
duk_context *ctx = (duk_context *) thr;
duk_activation *act;
duk_debug_write_notify(thr, DUK_DBG_CMD_STATUS);
duk_debug_write_int(thr, thr->heap->dbg_paused);
DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* unsigned */
if (thr->callstack_top == 0) {
duk_debug_write_undefined(thr);
duk_debug_write_undefined(thr);
duk_debug_write_int(thr, 0);
duk_debug_write_int(thr, 0);
} else {
act = thr->callstack + thr->callstack_top - 1;
duk_push_hobject(ctx, act->func);
duk_get_prop_string(ctx, -1, "fileName");
duk_safe_to_string(ctx, -1);
duk_debug_write_hstring(thr, duk_require_hstring(ctx, -1));
duk_get_prop_string(ctx, -2, "name");
duk_safe_to_string(ctx, -1);
duk_debug_write_hstring(thr, duk_require_hstring(ctx, -1));
duk_pop_3(ctx);
/* Report next pc/line to be executed. */
duk_debug_write_uint(thr, (duk_uint32_t) duk_debug_curr_line(thr));
duk_debug_write_uint(thr, (duk_uint32_t) duk_hthread_get_act_curr_pc(thr, act));
}
duk_debug_write_eom(thr);
}
/*
* Debug message processing
*/
/* Skip dvalue. */
DUK_LOCAL duk_bool_t duk__debug_skip_dvalue(duk_hthread *thr) {
duk_uint8_t x;
duk_uint32_t len;
x = duk_debug_read_byte(thr);
if (x >= 0xc0) {
duk_debug_skip_byte(thr);
return 0;
}
if (x >= 0x80) {
return 0;
}
if (x >= 0x60) {
duk_debug_skip_bytes(thr, x - 0x60);
return 0;
}
switch(x) {
case 0x00:
return 1; /* Return 1: got EOM */
case 0x01:
case 0x02:
case 0x03:
case 0x04:
break;
case 0x10:
(void) duk__debug_read_uint32_raw(thr);
break;
case 0x11:
case 0x13:
len = duk__debug_read_uint32_raw(thr);
duk_debug_skip_bytes(thr, len);
break;
case 0x12:
case 0x14:
len = duk__debug_read_uint16_raw(thr);
duk_debug_skip_bytes(thr, len);
break;
case 0x15:
case 0x16:
case 0x17:
case 0x18:
case 0x19:
break;
case 0x1a:
duk_debug_skip_bytes(thr, 8);
break;
case 0x1b:
duk_debug_skip_byte(thr);
len = duk_debug_read_byte(thr);
duk_debug_skip_bytes(thr, len);
break;
case 0x1c:
len = duk_debug_read_byte(thr);
duk_debug_skip_bytes(thr, len);
break;
case 0x1d:
duk_debug_skip_bytes(thr, 2);
len = duk_debug_read_byte(thr);
duk_debug_skip_bytes(thr, len);
break;
default:
goto fail;
}
return 0;
fail:
DUK__SET_CONN_BROKEN(thr);
return 1; /* Pretend like we got EOM */
}
/* Skip dvalues to EOM. */
DUK_LOCAL void duk__debug_skip_to_eom(duk_hthread *thr) {
for (;;) {
if (duk__debug_skip_dvalue(thr)) {
break;
}
}
}
/*
* Process incoming debug requests
*/
DUK_LOCAL void duk__debug_handle_basic_info(duk_hthread *thr, duk_heap *heap) {
DUK_UNREF(heap);
DUK_D(DUK_DPRINT("debug command version"));
duk_debug_write_reply(thr);
duk_debug_write_int(thr, DUK_VERSION);
duk_debug_write_cstring(thr, DUK_GIT_DESCRIBE);
duk_debug_write_cstring(thr, DUK_USE_TARGET_INFO);
#if defined(DUK_USE_DOUBLE_LE)
duk_debug_write_int(thr, 1);
#elif defined(DUK_USE_DOUBLE_ME)
duk_debug_write_int(thr, 2);
#elif defined(DUK_USE_DOUBLE_BE)
duk_debug_write_int(thr, 3);
#else
duk_debug_write_int(thr, 0);
#endif
duk_debug_write_eom(thr);
}
DUK_LOCAL void duk__debug_handle_trigger_status(duk_hthread *thr, duk_heap *heap) {
DUK_UNREF(heap);
DUK_D(DUK_DPRINT("debug command triggerstatus"));
duk_debug_write_reply(thr);
duk_debug_write_eom(thr);
heap->dbg_state_dirty = 1;
}
DUK_LOCAL void duk__debug_handle_pause(duk_hthread *thr, duk_heap *heap) {
DUK_D(DUK_DPRINT("debug command pause"));
DUK_HEAP_SET_PAUSED(heap);
duk_debug_write_reply(thr);
duk_debug_write_eom(thr);
}
DUK_LOCAL void duk__debug_handle_resume(duk_hthread *thr, duk_heap *heap) {
DUK_D(DUK_DPRINT("debug command resume"));
DUK_HEAP_CLEAR_PAUSED(heap);
duk_debug_write_reply(thr);
duk_debug_write_eom(thr);
}
DUK_LOCAL void duk__debug_handle_step(duk_hthread *thr, duk_heap *heap, duk_int32_t cmd) {
duk_small_uint_t step_type;
duk_uint_fast32_t line;
if (cmd == DUK_DBG_CMD_STEPINTO) {
step_type = DUK_STEP_TYPE_INTO;
} else if (cmd == DUK_DBG_CMD_STEPOVER) {
step_type = DUK_STEP_TYPE_OVER;
} else {
DUK_ASSERT(cmd == DUK_DBG_CMD_STEPOUT);
step_type = DUK_STEP_TYPE_OUT;
}
DUK_D(DUK_DPRINT("debug command stepinto/stepover/stepout: %d", (int) cmd));
line = duk_debug_curr_line(thr);
if (line > 0) {
heap->dbg_paused = 0;