-
Notifications
You must be signed in to change notification settings - Fork 171
/
debugserver.c
2692 lines (2252 loc) · 99.5 KB
/
debugserver.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 "moar.h"
#include "platform/threads.h"
#define bool int
#define true TRUE
#define false FALSE
#include "cmp.h"
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
typedef SOCKET Socket;
#define sa_family_t unsigned int
#else
#include "unistd.h"
#include <sys/socket.h>
#include <sys/un.h>
typedef int Socket;
#define closesocket close
#endif
typedef enum {
MT_MessageTypeNotUnderstood,
MT_ErrorProcessingMessage,
MT_OperationSuccessful,
MT_IsExecutionSuspendedRequest,
MT_IsExecutionSuspendedResponse,
MT_SuspendAll,
MT_ResumeAll,
MT_SuspendOne,
MT_ResumeOne,
MT_ThreadStarted,
MT_ThreadEnded,
MT_ThreadListRequest,
MT_ThreadListResponse,
MT_ThreadStackTraceRequest,
MT_ThreadStackTraceResponse,
MT_SetBreakpointRequest,
MT_SetBreakpointConfirmation,
MT_BreakpointNotification,
MT_ClearBreakpoint,
MT_ClearAllBreakpoints,
MT_StepInto,
MT_StepOver,
MT_StepOut,
MT_StepCompleted,
MT_ReleaseHandles,
MT_HandleResult,
MT_ContextHandle,
MT_ContextLexicalsRequest,
MT_ContextLexicalsResponse,
MT_OuterContextRequest,
MT_CallerContextRequest,
MT_CodeObjectHandle,
MT_ObjectAttributesRequest,
MT_ObjectAttributesResponse,
MT_DecontainerizeHandle,
MT_FindMethod,
MT_Invoke,
MT_InvokeResult,
MT_UnhandledException,
MT_OperationUnsuccessful,
MT_ObjectMetadataRequest,
MT_ObjectMetadataResponse,
MT_ObjectPositionalsRequest,
MT_ObjectPositionalsResponse,
MT_ObjectAssociativesRequest,
MT_ObjectAssociativesResponse,
MT_HandleEquivalenceRequest,
MT_HandleEquivalenceResponse,
} message_type;
typedef enum {
ArgKind_Handle,
ArgKind_Integer,
ArgKind_Num,
ArgKind_String,
} argument_kind;
typedef struct {
MVMuint8 arg_kind;
union {
MVMint64 i;
MVMnum64 n;
char *s;
MVMint64 o;
} arg_u;
} argument_data;
typedef enum {
FS_type = 1,
FS_id = 2,
FS_thread_id = 4,
FS_file = 8,
FS_line = 16,
FS_suspend = 32,
FS_stacktrace = 64,
/* handle_count is just bookkeeping */
FS_handles = 128,
FS_handle_id = 256,
FS_frame_number = 512,
FS_arguments = 1024,
} fields_set;
typedef struct {
MVMuint16 type;
MVMuint64 id;
MVMuint32 thread_id;
char *file;
MVMuint32 line;
MVMuint8 suspend;
MVMuint8 stacktrace;
MVMuint16 handle_count;
MVMuint64 *handles;
MVMuint64 handle_id;
MVMuint32 frame_number;
MVMuint32 argument_count;
argument_data *arguments;
MVMuint8 parse_fail;
const char *parse_fail_message;
fields_set fields_set;
} request_data;
static MVMint32 write_stacktrace_frames(MVMThreadContext *dtc, cmp_ctx_t *ctx, MVMThread *thread);
static MVMint32 request_all_threads_suspend(MVMThreadContext *dtc, cmp_ctx_t *ctx, request_data *argument);
static MVMuint64 allocate_handle(MVMThreadContext *dtc, MVMObject *target);
/* Breakpoint stuff */
MVM_PUBLIC void MVM_debugserver_register_line(MVMThreadContext *tc, char *filename, MVMuint32 filename_len, MVMuint32 line_no, MVMuint32 *file_idx) {
MVMDebugServerData *debugserver = tc->instance->debugserver;
MVMDebugServerBreakpointTable *table = debugserver->breakpoints;
MVMDebugServerBreakpointFileTable *found = NULL;
MVMuint32 index = 0;
char *open_paren_pos = (char *)memchr(filename, '(', filename_len);
if (open_paren_pos) {
if (open_paren_pos[-1] == ' ') {
filename_len = open_paren_pos - filename - 1;
}
}
uv_mutex_lock(&debugserver->mutex_breakpoints);
if (*file_idx < table->files_used) {
MVMDebugServerBreakpointFileTable *file = &table->files[*file_idx];
if (file->filename_length == filename_len && memcmp(file->filename, filename, filename_len) == 0)
found = file;
}
if (!found) {
for (index = 0; index < table->files_used; index++) {
MVMDebugServerBreakpointFileTable *file = &table->files[index];
if (file->filename_length != filename_len)
continue;
if (memcmp(file->filename, filename, filename_len) != 0)
continue;
found = file;
*file_idx = index;
break;
}
}
if (!found) {
if (table->files_used++ >= table->files_alloc) {
MVMuint32 old_alloc = table->files_alloc;
table->files_alloc *= 2;
table->files = MVM_fixed_size_realloc_at_safepoint(tc, tc->instance->fsa, table->files,
old_alloc * sizeof(MVMDebugServerBreakpointFileTable),
table->files_alloc * sizeof(MVMDebugServerBreakpointFileTable));
memset((char *)(table->files + old_alloc), 0, (table->files_alloc - old_alloc) * sizeof(MVMDebugServerBreakpointFileTable) - 1);
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "table for files increased to %u slots\n", table->files_alloc);
}
found = &table->files[table->files_used - 1];
found->filename = MVM_calloc(filename_len + 1, sizeof(char));
strncpy(found->filename, filename, filename_len);
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "created new file entry at %u for %s\n", table->files_used - 1, found->filename);
found->filename_length = filename_len;
found->lines_active_alloc = line_no + 32;
found->lines_active = MVM_fixed_size_alloc_zeroed(tc, tc->instance->fsa, found->lines_active_alloc * sizeof(MVMuint8));
*file_idx = table->files_used - 1;
found->breakpoints = NULL;
found->breakpoints_alloc = 0;
found->breakpoints_used = 0;
}
if (found->lines_active_alloc < line_no + 1) {
MVMuint32 old_size = found->lines_active_alloc;
found->lines_active_alloc *= 2;
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "increasing line number table for %s from %u to %u slots\n", found->filename, old_size, found->lines_active_alloc);
found->lines_active = MVM_fixed_size_realloc_at_safepoint(tc, tc->instance->fsa,
found->lines_active, old_size, found->lines_active_alloc);
memset((char *)found->lines_active + old_size, 0, found->lines_active_alloc - old_size - 1);
}
uv_mutex_unlock(&debugserver->mutex_breakpoints);
}
static void stop_point_hit(MVMThreadContext *tc) {
while (1) {
/* We're in total regular boring execution. Set ourselves to
* interrupted for suspend reasons */
if (MVM_cas(&tc->gc_status, MVMGCStatus_NONE, MVMGCStatus_INTERRUPT | MVMSuspendState_SUSPEND_REQUEST)
== MVMGCStatus_NONE) {
break;
}
/* Looks like another thread just interrupted us; join in on GC and
* then this loop will store the suspend request flag when we're back
* to MVMGCStatus_NONE. */
if (MVM_load(&tc->gc_status) == MVMGCStatus_INTERRUPT) {
MVM_gc_enter_from_interrupt(tc);
}
/* Perhaps the debugserver just asked us to suspend, too. It's not
* important for our suspend request flag to survive or something. */
if (MVM_load(&tc->gc_status) == (MVMGCStatus_INTERRUPT | MVMSuspendState_SUSPEND_REQUEST)) {
break;
}
}
MVM_gc_enter_from_interrupt(tc);
}
static MVMuint8 breakpoint_hit(MVMThreadContext *tc, MVMDebugServerBreakpointFileTable *file, MVMuint32 line_no) {
cmp_ctx_t *ctx = NULL;
MVMDebugServerBreakpointInfo *info;
MVMuint32 index;
MVMuint8 must_suspend = 0;
if (tc->instance->debugserver && tc->instance->debugserver->messagepack_data) {
ctx = (cmp_ctx_t*)tc->instance->debugserver->messagepack_data;
}
for (index = 0; index < file->breakpoints_used; index++) {
info = &file->breakpoints[index];
if (info->line_no == line_no) {
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "hit a breakpoint\n");
if (ctx) {
uv_mutex_lock(&tc->instance->debugserver->mutex_network_send);
cmp_write_map(ctx, 4);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, info->breakpoint_id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_BreakpointNotification);
cmp_write_str(ctx, "thread", 6);
cmp_write_integer(ctx, tc->thread_id);
cmp_write_str(ctx, "frames", 6);
if (info->send_backtrace) {
write_stacktrace_frames(tc, ctx, tc->thread_obj);
} else {
cmp_write_nil(ctx);
}
uv_mutex_unlock(&tc->instance->debugserver->mutex_network_send);
}
if (info->shall_suspend) {
must_suspend = 1;
}
}
}
return must_suspend;
}
static void step_point_hit(MVMThreadContext *tc) {
cmp_ctx_t *ctx = (cmp_ctx_t*)tc->instance->debugserver->messagepack_data;
uv_mutex_lock(&tc->instance->debugserver->mutex_network_send);
cmp_write_map(ctx, 4);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, tc->step_message_id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_StepCompleted);
cmp_write_str(ctx, "thread", 6);
cmp_write_integer(ctx, tc->thread_id);
cmp_write_str(ctx, "frames", 6);
write_stacktrace_frames(tc, ctx, tc->thread_obj);
uv_mutex_unlock(&tc->instance->debugserver->mutex_network_send);
tc->step_mode = MVMDebugSteppingMode_NONE;
tc->step_mode_frame = NULL;
}
MVM_PUBLIC void MVM_debugserver_breakpoint_check(MVMThreadContext *tc, MVMuint32 file_idx, MVMuint32 line_no) {
MVMDebugServerData *debugserver = tc->instance->debugserver;
MVMuint8 shall_suspend = 0;
tc->cur_line_no = line_no;
tc->cur_file_idx = file_idx;
if (debugserver->any_breakpoints_at_all) {
MVMDebugServerBreakpointTable *table = debugserver->breakpoints;
MVMDebugServerBreakpointFileTable *found = &table->files[file_idx];
if (debugserver->any_breakpoints_at_all && found->breakpoints_used && found->lines_active[line_no]) {
shall_suspend |= breakpoint_hit(tc, found, line_no);
}
}
if (tc->step_mode) {
if (tc->step_mode == MVMDebugSteppingMode_STEP_OVER) {
if (line_no != tc->step_mode_line_no && tc->step_mode_frame == tc->cur_frame) {
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "hit a stepping point: step over; %u != %u, %p == %p\n", line_no, tc->step_mode_line_no, tc->step_mode_frame, tc->cur_frame);
step_point_hit(tc);
shall_suspend = 1;
}
}
else if (tc->step_mode == MVMDebugSteppingMode_STEP_INTO) {
if (line_no != tc->step_mode_line_no && tc->step_mode_frame == tc->cur_frame
|| tc->step_mode_frame != tc->cur_frame) {
if (tc->instance->debugserver->debugspam_protocol)
if (line_no != tc->step_mode_line_no && tc->step_mode_frame == tc->cur_frame)
fprintf(stderr, "hit a stepping point: step into; %u != %u, %p == %p\n",
line_no, tc->step_mode_line_no, tc->step_mode_frame, tc->cur_frame);
else
fprintf(stderr, "hit a stepping point: step into; %u, %u, %p != %p\n",
line_no, tc->step_mode_line_no, tc->step_mode_frame, tc->cur_frame);
step_point_hit(tc);
shall_suspend = 1;
}
}
/* Nothing to do for STEP_OUT. */
/* else if (tc->step_mode == MVMDebugSteppingMode_STEP_OUT) { } */
}
if (shall_suspend)
stop_point_hit(tc);
}
#define REQUIRE(field, message) do { if(!(data->fields_set & (field))) { data->parse_fail = 1; data->parse_fail_message = (message); return 0; }; accepted = accepted | (field); } while (0)
MVMuint8 check_requirements(request_data *data) {
fields_set accepted = FS_id | FS_type;
REQUIRE(FS_id, "An id field is required");
REQUIRE(FS_type, "A type field is required");
switch (data->type) {
case MT_IsExecutionSuspendedRequest:
case MT_SuspendAll:
case MT_ResumeAll:
case MT_ThreadListRequest:
case MT_ClearAllBreakpoints:
/* All of these messages only take id and type */
break;
case MT_SuspendOne:
case MT_ResumeOne:
case MT_ThreadStackTraceRequest:
case MT_StepInto:
case MT_StepOver:
case MT_StepOut:
REQUIRE(FS_thread_id, "A thread field is required");
break;
case MT_SetBreakpointRequest:
REQUIRE(FS_suspend, "A suspend field is required");
REQUIRE(FS_stacktrace, "A stacktrace field is required");
/* Fall-Through */
case MT_ClearBreakpoint:
REQUIRE(FS_file, "A file field is required");
REQUIRE(FS_line, "A line field is required");
break;
case MT_HandleEquivalenceRequest:
case MT_ReleaseHandles:
REQUIRE(FS_handles, "A handles field is required");
break;
case MT_FindMethod:
/* TODO we've got to have some name field or something */
/* Fall-Through */
case MT_DecontainerizeHandle:
REQUIRE(FS_thread_id, "A thread field is required");
/* Fall-Through */
case MT_ContextLexicalsRequest:
case MT_OuterContextRequest:
case MT_CallerContextRequest:
case MT_ObjectAttributesRequest:
case MT_ObjectMetadataRequest:
case MT_ObjectPositionalsRequest:
case MT_ObjectAssociativesRequest:
REQUIRE(FS_handle_id, "A handle field is required");
break;
case MT_ContextHandle:
case MT_CodeObjectHandle:
REQUIRE(FS_thread_id, "A thread field is required");
REQUIRE(FS_frame_number, "A frame field is required");
break;
case MT_Invoke:
REQUIRE(FS_handle_id, "A handle field is required");
REQUIRE(FS_thread_id, "A thread field is required");
REQUIRE(FS_arguments, "An arguments field is required");
break;
default:
break;
}
if (data->fields_set != accepted) {
data->parse_fail = 1;
data->parse_fail_message = "Too many keys in message";
}
}
static MVMuint16 big_endian_16(MVMuint16 number) {
#ifdef MVM_BIGENDIAN
return number;
#else
char *bytes = (char *)&number;
char tmp;
tmp = bytes[1];
bytes[1] = bytes[0];
bytes[0] = tmp;
return *((MVMuint16 *)bytes);
#endif
}
static void send_greeting(Socket *sock) {
char buffer[24] = "MOARVM-REMOTE-DEBUG\0";
MVMuint16 version = big_endian_16(1);
MVMuint16 *verptr = (MVMuint16 *)(&buffer[strlen("MOARVM-REMOTE-DEBUG") + 1]);
*verptr = version;
verptr++;
version = big_endian_16(0);
*verptr = version;
send(*sock, buffer, 24, 0);
}
static int receive_greeting(Socket *sock) {
const char *expected = "MOARVM-REMOTE-CLIENT-OK";
char buffer[24];
int received = 0;
memset(buffer, 0, sizeof(buffer));
received = recv(*sock, buffer, sizeof(buffer), 0);
if (received != sizeof(buffer)) {
return 0;
}
if (memcmp(buffer, expected, sizeof(buffer)) == 0) {
return 1;
}
return 0;
}
static void communicate_error(MVMThreadContext *tc, cmp_ctx_t *ctx, request_data *argument) {
if (argument) {
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "communicating an error\n");
cmp_write_map(ctx, 2);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, argument->id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_ErrorProcessingMessage);
}
}
static void communicate_success(MVMThreadContext *tc, cmp_ctx_t *ctx, request_data *argument) {
if (argument) {
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "communicating success\n");
cmp_write_map(ctx, 2);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, argument->id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_OperationSuccessful);
}
}
/* Send spontaneous events */
MVM_PUBLIC void MVM_debugserver_notify_thread_creation(MVMThreadContext *tc) {
if (tc->instance->debugserver && tc->instance->debugserver->messagepack_data) {
cmp_ctx_t *ctx = (cmp_ctx_t*)tc->instance->debugserver->messagepack_data;
MVMuint64 event_id;
uv_mutex_lock(&tc->instance->debugserver->mutex_network_send);
event_id = tc->instance->debugserver->event_id;
tc->instance->debugserver->event_id += 2;
cmp_write_map(ctx, 5);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, event_id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_ThreadStarted);
cmp_write_str(ctx, "thread", 6);
cmp_write_integer(ctx, tc->thread_obj->body.thread_id);
cmp_write_str(ctx, "native_id", 9);
cmp_write_integer(ctx, tc->thread_obj->body.native_thread_id);
cmp_write_str(ctx, "app_lifetime", 12);
cmp_write_integer(ctx, tc->thread_obj->body.app_lifetime);
uv_mutex_unlock(&tc->instance->debugserver->mutex_network_send);
}
}
MVM_PUBLIC void MVM_debugserver_notify_thread_destruction(MVMThreadContext *tc) {
if (tc->instance->debugserver && tc->instance->debugserver->messagepack_data) {
cmp_ctx_t *ctx = (cmp_ctx_t*)tc->instance->debugserver->messagepack_data;
MVMuint64 event_id;
uv_mutex_lock(&tc->instance->debugserver->mutex_network_send);
event_id = tc->instance->debugserver->event_id;
tc->instance->debugserver->event_id += 2;
cmp_write_map(ctx, 3);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, event_id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_ThreadEnded);
cmp_write_str(ctx, "thread", 6);
cmp_write_integer(ctx, tc->thread_obj->body.thread_id);
uv_mutex_unlock(&tc->instance->debugserver->mutex_network_send);
}
}
MVM_PUBLIC void MVM_debugserver_notify_unhandled_exception(MVMThreadContext *tc, MVMException *ex) {
if (tc->instance->debugserver && tc->instance->debugserver->messagepack_data) {
cmp_ctx_t *ctx = (cmp_ctx_t*)tc->instance->debugserver->messagepack_data;
MVMuint64 event_id;
uv_mutex_lock(&tc->instance->debugserver->mutex_network_send);
request_all_threads_suspend(tc, ctx, NULL);
event_id = tc->instance->debugserver->event_id;
tc->instance->debugserver->event_id += 2;
cmp_write_map(ctx, 5);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, event_id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_UnhandledException);
cmp_write_str(ctx, "handle", 6);
cmp_write_integer(ctx, allocate_handle(tc, (MVMObject *)ex));
cmp_write_str(ctx, "thread", 6);
cmp_write_integer(ctx, tc->thread_obj->body.thread_id);
cmp_write_str(ctx, "frames", 6);
write_stacktrace_frames(tc, ctx, tc->thread_obj);
uv_mutex_unlock(&tc->instance->debugserver->mutex_network_send);
MVM_gc_enter_from_interrupt(tc);
}
}
static MVMuint8 is_thread_id_eligible(MVMInstance *vm, MVMuint32 id) {
if (id == vm->debugserver->thread_id || id == vm->speshworker_thread_id) {
return 0;
}
return 1;
}
/* Send replies to requests send by the client */
static MVMThread *find_thread_by_id(MVMInstance *vm, MVMint32 id) {
MVMThread *cur_thread = 0;
if (!is_thread_id_eligible(vm, id)) {
return NULL;
}
uv_mutex_lock(&vm->mutex_threads);
cur_thread = vm->threads;
while (cur_thread) {
if (cur_thread->body.thread_id == id) {
break;
}
cur_thread = cur_thread->body.next;
}
uv_mutex_unlock(&vm->mutex_threads);
return cur_thread;
}
static MVMint32 request_thread_suspends(MVMThreadContext *dtc, cmp_ctx_t *ctx, request_data *argument, MVMThread *thread) {
MVMThread *to_do = thread ? thread : find_thread_by_id(dtc->instance, argument->thread_id);
MVMThreadContext *tc = to_do ? to_do->body.tc : NULL;
if (!tc)
return 1;
MVM_gc_mark_thread_blocked(dtc);
while (1) {
/* Is the thread currently doing completely ordinary code execution? */
if (MVM_cas(&tc->gc_status, MVMGCStatus_NONE, MVMGCStatus_INTERRUPT | MVMSuspendState_SUSPEND_REQUEST)
== MVMGCStatus_NONE) {
break;
}
/* Is the thread in question currently blocked, i.e. spending time in
* some long-running piece of C code, waiting for I/O, etc.?
* If so, just store the suspend request bit so when it unblocks itself
* it'll suspend execution. */
if (MVM_cas(&tc->gc_status, MVMGCStatus_UNABLE, MVMGCStatus_UNABLE | MVMSuspendState_SUSPEND_REQUEST)
== MVMGCStatus_UNABLE) {
break;
}
/* Was the thread faster than us? For example by running into
* a breakpoint, completing a step, or encountering an
* unhandled exception? If so, we're done here. */
if ((MVM_load(&tc->gc_status) & MVMSUSPENDSTATUS_MASK) == MVMSuspendState_SUSPEND_REQUEST) {
break;
}
MVM_platform_thread_yield();
}
if (argument && argument->type == MT_SuspendOne)
communicate_success(tc, ctx, argument);
MVM_gc_mark_thread_unblocked(dtc);
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "thread %u successfully suspended\n", tc->thread_id);
return 0;
}
static MVMint32 request_all_threads_suspend(MVMThreadContext *dtc, cmp_ctx_t *ctx, request_data *argument) {
MVMInstance *vm = dtc->instance;
MVMThread *cur_thread = 0;
MVMuint32 success = 1;
uv_mutex_lock(&vm->mutex_threads);
/* TODO track which threads we successfully suspended so we can wake them
* up again if an error occured */
cur_thread = vm->threads;
while (cur_thread) {
if (is_thread_id_eligible(vm, cur_thread->body.thread_id)) {
AO_t current = MVM_load(&cur_thread->body.tc->gc_status);
if (current == MVMGCStatus_NONE || current == MVMGCStatus_UNABLE) {
MVMint32 result = request_thread_suspends(dtc, ctx, argument, cur_thread);
if (result == 1) {
success = 0;
break;
}
}
}
cur_thread = cur_thread->body.next;
}
if (success)
communicate_success(dtc, ctx, argument);
else
communicate_error(dtc, ctx, argument);
uv_mutex_unlock(&vm->mutex_threads);
}
static MVMint32 request_thread_resumes(MVMThreadContext *dtc, cmp_ctx_t *ctx, request_data *argument, MVMThread *thread) {
MVMInstance *vm = dtc->instance;
MVMThread *to_do = thread ? thread : find_thread_by_id(vm, argument->thread_id);
MVMThreadContext *tc = to_do ? to_do->body.tc : NULL;
AO_t current;
if (!tc) {
return 1;
}
current = MVM_load(&tc->gc_status);
if (current != (MVMGCStatus_UNABLE | MVMSuspendState_SUSPENDED)
&& (current & MVMSUSPENDSTATUS_MASK) != MVMSuspendState_SUSPEND_REQUEST) {
fprintf(stderr, "wrong state to resume from: %lu\n", MVM_load(&tc->gc_status));
return 1;
}
MVM_gc_mark_thread_blocked(dtc);
while(1) {
current = MVM_cas(&tc->gc_status, MVMGCStatus_UNABLE | MVMSuspendState_SUSPENDED, MVMGCStatus_UNABLE);
if (current == (MVMGCStatus_UNABLE | MVMSuspendState_SUSPENDED)) {
/* Success! We signalled the thread and can now tell it to
* mark itself unblocked, which takes care of any looming GC
* and related business. */
uv_cond_broadcast(&vm->debugserver->tell_threads);
break;
} else if ((current & MVMGCSTATUS_MASK) == MVMGCStatus_STOLEN) {
uv_mutex_lock(&tc->instance->mutex_gc_orchestrate);
if (tc->instance->in_gc) {
uv_cond_wait(&tc->instance->cond_blocked_can_continue,
&tc->instance->mutex_gc_orchestrate);
}
uv_mutex_unlock(&tc->instance->mutex_gc_orchestrate);
} else {
if (current == (MVMGCStatus_UNABLE | MVMSuspendState_SUSPEND_REQUEST)) {
if (MVM_cas(&tc->gc_status, current, MVMGCStatus_UNABLE) == current) {
break;
}
}
}
}
MVM_gc_mark_thread_unblocked(dtc);
if (argument && argument->type == MT_ResumeOne)
communicate_success(tc, ctx, argument);
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "success resuming thread; its status is now %lu\n", MVM_load(&tc->gc_status));
return 0;
}
static MVMint32 request_all_threads_resume(MVMThreadContext *dtc, cmp_ctx_t *ctx, request_data *argument) {
MVMInstance *vm = dtc->instance;
MVMThread *cur_thread = 0;
MVMuint8 success = 1;
uv_mutex_lock(&vm->mutex_threads);
cur_thread = vm->threads;
while (cur_thread) {
if (cur_thread != dtc->thread_obj) {
AO_t current = MVM_load(&cur_thread->body.tc->gc_status);
if (current == (MVMGCStatus_UNABLE | MVMSuspendState_SUSPENDED) ||
current == (MVMGCStatus_INTERRUPT | MVMSuspendState_SUSPEND_REQUEST) ||
current == (MVMGCStatus_STOLEN | MVMSuspendState_SUSPEND_REQUEST)) {
if (request_thread_resumes(dtc, ctx, argument, cur_thread)) {
if (vm->debugserver->debugspam_protocol)
fprintf(stderr, "failure to resume thread %u\n", cur_thread->body.thread_id);
success = 0;
break;
}
}
}
cur_thread = cur_thread->body.next;
}
if (success)
communicate_success(dtc, ctx, argument);
else
communicate_error(dtc, ctx, argument);
uv_mutex_unlock(&vm->mutex_threads);
return !success;
}
static MVMint32 write_stacktrace_frames(MVMThreadContext *dtc, cmp_ctx_t *ctx, MVMThread *thread) {
MVMThreadContext *tc = thread->body.tc;
MVMuint64 stack_size = 0;
MVMFrame *cur_frame = tc->cur_frame;
while (cur_frame != NULL) {
stack_size++;
cur_frame = cur_frame->caller;
}
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "dumping a stack trace of %lu frames\n", stack_size);
cmp_write_array(ctx, stack_size);
cur_frame = tc->cur_frame;
stack_size = 0; /* To check if we've got the topmost frame or not */
while (cur_frame != NULL) {
MVMString *bc_filename = cur_frame->static_info->body.cu->body.filename;
MVMString *name = cur_frame->static_info->body.name;
MVMuint8 *cur_op = stack_size != 0 ? cur_frame->return_address : *(tc->interp_cur_op);
MVMuint32 offset = cur_op - MVM_frame_effective_bytecode(cur_frame);
MVMBytecodeAnnotation *annot = MVM_bytecode_resolve_annotation(tc, &cur_frame->static_info->body,
offset > 0 ? offset - 1 : 0);
MVMint32 line_number = annot ? annot->line_number : 1;
MVMint16 string_heap_index = annot ? annot->filename_string_heap_index : 1;
char *tmp1 = annot && string_heap_index < cur_frame->static_info->body.cu->body.num_strings
? MVM_string_utf8_encode_C_string(tc, MVM_cu_string(tc,
cur_frame->static_info->body.cu, string_heap_index))
: NULL;
char *filename_c = bc_filename
? MVM_string_utf8_encode_C_string(tc, bc_filename)
: NULL;
char *name_c = name
? MVM_string_utf8_encode_C_string(tc, name)
: NULL;
MVMObject *code_ref = cur_frame->code_ref;
MVMCode *code_obj = code_ref && REPR(code_ref)->ID == MVM_REPR_ID_MVMCode ? (MVMCode*)code_ref : NULL;
char *debugname = code_obj && code_obj->body.code_object ? MVM_6model_get_debug_name(tc, code_obj->body.code_object) : "";
cmp_write_map(ctx, 5);
cmp_write_str(ctx, "file", 4);
cmp_write_str(ctx, tmp1, tmp1 ? strlen(tmp1) : 0);
cmp_write_str(ctx, "line", 4);
cmp_write_integer(ctx, line_number);
cmp_write_str(ctx, "bytecode_file", 13);
if (bc_filename)
cmp_write_str(ctx, filename_c, strlen(filename_c));
else
cmp_write_nil(ctx);
cmp_write_str(ctx, "name", 4);
cmp_write_str(ctx, name_c, name_c ? strlen(name_c) : 0);
cmp_write_str(ctx, "type", 4);
cmp_write_str(ctx, debugname, strlen(debugname));
if (bc_filename)
MVM_free(filename_c);
if (name)
MVM_free(name_c);
if (tmp1)
MVM_free(tmp1);
cur_frame = cur_frame->caller;
stack_size++;
}
}
static MVMint32 request_thread_stacktrace(MVMThreadContext *dtc, cmp_ctx_t *ctx, request_data *argument, MVMThread *thread) {
MVMThread *to_do = thread ? thread : find_thread_by_id(dtc->instance, argument->thread_id);
if (!to_do)
return 1;
if ((to_do->body.tc->gc_status & MVMGCSTATUS_MASK) != MVMGCStatus_UNABLE) {
return 1;
}
cmp_write_map(ctx, 3);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, argument->id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_ThreadStackTraceResponse);
cmp_write_str(ctx, "frames", 6);
write_stacktrace_frames(dtc, ctx, to_do);
return 0;
}
static void send_thread_info(MVMThreadContext *dtc, cmp_ctx_t *ctx, request_data *argument) {
MVMInstance *vm = dtc->instance;
MVMint32 threadcount = 0;
MVMThread *cur_thread;
char infobuf[32] = "THL";
uv_mutex_lock(&vm->mutex_threads);
cur_thread = vm->threads;
while (cur_thread) {
threadcount++;
cur_thread = cur_thread->body.next;
}
cmp_write_map(ctx, 3);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, argument->id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_ThreadListResponse);
cmp_write_str(ctx, "threads", 7);
cmp_write_array(ctx, threadcount);
cur_thread = vm->threads;
while (cur_thread) {
cmp_write_map(ctx, 5);
cmp_write_str(ctx, "thread", 6);
cmp_write_integer(ctx, cur_thread->body.thread_id);
cmp_write_str(ctx, "native_id", 9);
cmp_write_integer(ctx, cur_thread->body.native_thread_id);
cmp_write_str(ctx, "app_lifetime", 12);
cmp_write_bool(ctx, cur_thread->body.app_lifetime);
cmp_write_str(ctx, "suspended", 9);
cmp_write_bool(ctx, (MVM_load(&cur_thread->body.tc->gc_status) & MVMSUSPENDSTATUS_MASK) != MVMSuspendState_NONE);
cmp_write_str(ctx, "num_locks", 9);
cmp_write_integer(ctx, cur_thread->body.tc->num_locks);
cur_thread = cur_thread->body.next;
}
uv_mutex_unlock(&vm->mutex_threads);
}
static MVMuint64 send_is_execution_suspended_info(MVMThreadContext *dtc, cmp_ctx_t *ctx, request_data *argument) {
MVMInstance *vm = dtc->instance;
MVMuint8 result = 1;
MVMThread *cur_thread;
uv_mutex_lock(&vm->mutex_threads);
cur_thread = vm->threads;
while (cur_thread) {
if ((MVM_load(&cur_thread->body.tc->gc_status) & MVMSUSPENDSTATUS_MASK) != MVMSuspendState_SUSPENDED
&& cur_thread->body.thread_id != vm->debugserver->thread_id
&& cur_thread->body.thread_id != vm->speshworker_thread_id) {
result = 0;
break;
}
cur_thread = cur_thread->body.next;
}
uv_mutex_unlock(&vm->mutex_threads);
cmp_write_map(ctx, 3);
cmp_write_str(ctx, "id", 2);
cmp_write_integer(ctx, argument->id);
cmp_write_str(ctx, "type", 4);
cmp_write_integer(ctx, MT_IsExecutionSuspendedResponse);
cmp_write_str(ctx, "suspended", 9);
cmp_write_bool(ctx, result);
}
MVMuint8 setup_step(MVMThreadContext *dtc, cmp_ctx_t *ctx, request_data *argument, MVMDebugSteppingMode mode, MVMThread *thread) {
MVMThread *to_do = thread ? thread : find_thread_by_id(dtc->instance, argument->thread_id);
MVMThreadContext *tc;
if (!to_do)
return 1;
if ((to_do->body.tc->gc_status & MVMGCSTATUS_MASK) != MVMGCStatus_UNABLE) {
return 1;
}
tc = to_do->body.tc;
tc->step_mode_frame = tc->cur_frame;
tc->step_message_id = argument->id;
tc->step_mode_line_no = tc->cur_line_no;
tc->step_mode_file_idx = tc->cur_file_idx;
tc->step_mode = mode;
request_thread_resumes(dtc, ctx, NULL, to_do);
return 0;
}
void MVM_debugserver_add_breakpoint(MVMThreadContext *tc, cmp_ctx_t *ctx, request_data *argument) {
MVMDebugServerData *debugserver = tc->instance->debugserver;
MVMDebugServerBreakpointTable *table = debugserver->breakpoints;
MVMDebugServerBreakpointFileTable *found = NULL;
MVMDebugServerBreakpointInfo *bp_info = NULL;
MVMuint32 index = 0;
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "asked to set a breakpoint for file %s line %u to send id %lu\n", argument->file, argument->line, argument->id);
MVM_debugserver_register_line(tc, argument->file, strlen(argument->file), argument->line, &index);
uv_mutex_lock(&debugserver->mutex_breakpoints);
found = &table->files[index];
/* Create breakpoint first so that if a thread breaks on the activated line
* the breakpoint information already exists */
if (found->breakpoints_alloc == 0) {
found->breakpoints_alloc = 4;
found->breakpoints = MVM_fixed_size_alloc_zeroed(tc, tc->instance->fsa,
found->breakpoints_alloc * sizeof(MVMDebugServerBreakpointInfo));
}
if (found->breakpoints_used++ >= found->breakpoints_alloc) {
MVMuint32 old_alloc = found->breakpoints_alloc;
found->breakpoints_alloc *= 2;
found->breakpoints = MVM_fixed_size_realloc_at_safepoint(tc, tc->instance->fsa, found->breakpoints,
old_alloc * sizeof(MVMDebugServerBreakpointInfo),
found->breakpoints_alloc * sizeof(MVMDebugServerBreakpointInfo));
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "table for breakpoints increased to %u slots\n", found->breakpoints_alloc);
}