forked from mono/mono
-
Notifications
You must be signed in to change notification settings - Fork 520
/
Copy pathdebugger-agent.c
10677 lines (9123 loc) · 288 KB
/
debugger-agent.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
/**
* \file
* Soft Debugger back-end module
*
* Author:
* Zoltan Varga ([email protected])
*
* Copyright 2009-2010 Novell, Inc.
* Copyright 2011 Xamarin Inc.
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <glib.h>
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#ifdef HOST_WIN32
#ifdef _MSC_VER
#include <winsock2.h>
#include <process.h>
#endif
#include <ws2tcpip.h>
#include <windows.h>
#endif
#ifdef HOST_ANDROID
#include <linux/in.h>
#include <linux/tcp.h>
#include <sys/endian.h>
#endif
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/debug-internals.h>
#include <mono/metadata/domain-internals.h>
#include <mono/metadata/gc-internals.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/mono-hash-internals.h>
#include <mono/metadata/threads-types.h>
#include <mono/metadata/threadpool.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/assembly-internals.h>
#include <mono/metadata/runtime.h>
#include <mono/metadata/verify-internals.h>
#include <mono/metadata/reflection-internals.h>
#include <mono/metadata/w32socket.h>
#include <mono/metadata/w32socket-internals.h>
#include <mono/utils/mono-coop-mutex.h>
#include <mono/utils/mono-coop-semaphore.h>
#include <mono/utils/mono-error-internals.h>
#include <mono/utils/mono-stack-unwinding.h>
#include <mono/utils/mono-time.h>
#include <mono/utils/mono-threads.h>
#include <mono/utils/networking.h>
#include <mono/utils/mono-proclib.h>
#include <mono/utils/w32api.h>
#include <mono/utils/mono-logger-internals.h>
#include "debugger-state-machine.h"
#include "debugger-agent.h"
#include "mini.h"
#include "seq-points.h"
#include "aot-runtime.h"
#include "mini-runtime.h"
#include "interp/interp.h"
#include "debugger-engine.h"
#include "mono/metadata/debug-mono-ppdb.h"
#include "mono/metadata/custom-attrs-internals.h"
#include "mono/metadata/unity-utils.h"
/*
* On iOS we can't use System.Environment.Exit () as it will do the wrong
* shutdown sequence.
*/
#if !defined (TARGET_IOS)
#define TRY_MANAGED_SYSTEM_ENVIRONMENT_EXIT
#endif
#if DISABLE_SOCKETS
#define DISABLE_SOCKET_TRANSPORT
#endif
#ifndef DISABLE_SDB
#include <mono/utils/mono-os-mutex.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifndef S_IWUSR
#define S_IWUSR S_IWRITE
#endif
#define THREAD_TO_INTERNAL(thread) (thread)->internal_thread
#if _MSC_VER
#pragma warning(disable:4312) // FIXME pointer cast to different size
#endif
typedef struct {
gboolean enabled;
char *transport;
char *address;
int log_level;
char *log_file;
gboolean suspend;
gboolean server;
gboolean onuncaught;
GSList *onthrow;
int timeout;
char *launch;
gboolean embedding;
gboolean defer;
int keepalive;
gboolean setpgid;
} AgentConfig;
typedef struct _InvokeData InvokeData;
struct _InvokeData
{
int id;
int flags;
guint8 *p;
guint8 *endp;
/* This is the context which needs to be restored after the invoke */
MonoContext ctx;
gboolean has_ctx;
/*
* If this is set, invoke this method with the arguments given by ARGS.
*/
MonoMethod *method;
gpointer *args;
guint32 suspend_count;
int nmethods;
InvokeData *last_invoke;
};
struct _DebuggerTlsData {
MonoThreadUnwindState context;
/* This is computed on demand when it is requested using the wire protocol */
/* It is freed up when the thread is resumed */
int frame_count;
StackFrame **frames;
/*
* Whenever the frame info is up-to-date. If not, compute_frame_info () will need to
* re-compute it.
*/
gboolean frames_up_to_date;
/*
* Points to data about a pending invoke which needs to be executed after the thread
* resumes.
*/
InvokeData *pending_invoke;
/*
* Set to TRUE if this thread is suspended in suspend_current () or it is executing
* native code.
*/
gboolean suspended;
/*
* Signals whenever the thread is in the process of suspending, i.e. it will suspend
* within a finite amount of time.
*/
gboolean suspending;
/*
* Set to TRUE if this thread is suspended in suspend_current ().
*/
gboolean really_suspended;
/* Used to pass the context to the breakpoint/single step handler */
MonoContext handler_ctx;
/* Whenever thread_stop () was called for this thread */
gboolean terminated;
/* Whenever to disable breakpoints (used during invokes) */
gboolean disable_breakpoints;
/*
* Number of times this thread has been resumed using resume_thread ().
*/
guint32 resume_count;
guint32 resume_count_internal;
guint32 suspend_count;
MonoInternalThread *thread;
intptr_t thread_id;
/*
* Information about the frame which transitioned to native code for running
* threads.
*/
StackFrameInfo async_last_frame;
/*
* The context where the stack walk can be started for running threads.
*/
MonoThreadUnwindState async_state;
/*
* The context used for filter clauses
*/
MonoThreadUnwindState filter_state;
gboolean abort_requested;
/*
* The current mono_runtime_invoke_checked invocation.
*/
InvokeData *invoke;
StackFrameInfo catch_frame;
gboolean has_catch_frame;
/*
* The context which needs to be restored after handling a single step/breakpoint
* event. This is the same as the ctx at step/breakpoint site, but includes changes
* to caller saved registers done by set_var ().
*/
MonoThreadUnwindState restore_state;
/* Frames computed from restore_state */
int restore_frame_count;
StackFrame **restore_frames;
/* The currently unloading appdomain */
MonoDomain *domain_unloading;
// The state that the debugger expects the thread to be in
MonoDebuggerThreadState thread_state;
MonoStopwatch step_time;
gboolean gc_finalizing;
};
typedef struct {
const char *name;
void (*connect) (const char *address);
void (*close1) (void);
void (*close2) (void);
gboolean (*send) (void *buf, int len);
int (*recv) (void *buf, int len);
} DebuggerTransport;
/*
* Wire Protocol definitions
*/
#define HEADER_LENGTH 11
#define MAJOR_VERSION 2
#define MINOR_VERSION 58
typedef enum {
CMD_SET_VM = 1,
CMD_SET_OBJECT_REF = 9,
CMD_SET_STRING_REF = 10,
CMD_SET_THREAD = 11,
CMD_SET_ARRAY_REF = 13,
CMD_SET_EVENT_REQUEST = 15,
CMD_SET_STACK_FRAME = 16,
CMD_SET_APPDOMAIN = 20,
CMD_SET_ASSEMBLY = 21,
CMD_SET_METHOD = 22,
CMD_SET_TYPE = 23,
CMD_SET_MODULE = 24,
CMD_SET_FIELD = 25,
CMD_SET_EVENT = 64,
CMD_SET_POINTER = 65
} CommandSet;
typedef enum {
SUSPEND_POLICY_NONE = 0,
SUSPEND_POLICY_EVENT_THREAD = 1,
SUSPEND_POLICY_ALL = 2
} SuspendPolicy;
typedef enum {
ERR_NONE = 0,
ERR_INVALID_OBJECT = 20,
ERR_INVALID_FIELDID = 25,
ERR_INVALID_FRAMEID = 30,
ERR_NOT_IMPLEMENTED = 100,
ERR_NOT_SUSPENDED = 101,
ERR_INVALID_ARGUMENT = 102,
ERR_UNLOADED = 103,
ERR_NO_INVOCATION = 104,
ERR_ABSENT_INFORMATION = 105,
ERR_NO_SEQ_POINT_AT_IL_OFFSET = 106,
ERR_INVOKE_ABORTED = 107,
ERR_LOADER_ERROR = 200, /*XXX extend the protocol to pass this information down the pipe */
} ErrorCode;
typedef enum {
TOKEN_TYPE_STRING = 0,
TOKEN_TYPE_TYPE = 1,
TOKEN_TYPE_FIELD = 2,
TOKEN_TYPE_METHOD = 3,
TOKEN_TYPE_UNKNOWN = 4
} DebuggerTokenType;
typedef enum {
VALUE_TYPE_ID_NULL = 0xf0,
VALUE_TYPE_ID_TYPE = 0xf1,
VALUE_TYPE_ID_PARENT_VTYPE = 0xf2,
VALUE_TYPE_ID_FIXED_ARRAY = 0xf3
} ValueTypeId;
typedef enum {
FRAME_FLAG_DEBUGGER_INVOKE = 1,
FRAME_FLAG_NATIVE_TRANSITION = 2
} StackFrameFlags;
typedef enum {
INVOKE_FLAG_DISABLE_BREAKPOINTS = 1,
INVOKE_FLAG_SINGLE_THREADED = 2,
INVOKE_FLAG_RETURN_OUT_THIS = 4,
INVOKE_FLAG_RETURN_OUT_ARGS = 8,
INVOKE_FLAG_VIRTUAL = 16
} InvokeFlags;
typedef enum {
BINDING_FLAGS_IGNORE_CASE = 0x70000000,
} BindingFlagsExtensions;
typedef enum {
CMD_VM_VERSION = 1,
CMD_VM_ALL_THREADS = 2,
CMD_VM_SUSPEND = 3,
CMD_VM_RESUME = 4,
CMD_VM_EXIT = 5,
CMD_VM_DISPOSE = 6,
CMD_VM_INVOKE_METHOD = 7,
CMD_VM_SET_PROTOCOL_VERSION = 8,
CMD_VM_ABORT_INVOKE = 9,
CMD_VM_SET_KEEPALIVE = 10,
CMD_VM_GET_TYPES_FOR_SOURCE_FILE = 11,
CMD_VM_GET_TYPES = 12,
CMD_VM_INVOKE_METHODS = 13,
CMD_VM_START_BUFFERING = 14,
CMD_VM_STOP_BUFFERING = 15
} CmdVM;
typedef enum {
CMD_THREAD_GET_FRAME_INFO = 1,
CMD_THREAD_GET_NAME = 2,
CMD_THREAD_GET_STATE = 3,
CMD_THREAD_GET_INFO = 4,
CMD_THREAD_GET_ID = 5,
CMD_THREAD_GET_TID = 6,
CMD_THREAD_SET_IP = 7,
CMD_THREAD_ELAPSED_TIME = 8
} CmdThread;
typedef enum {
CMD_EVENT_REQUEST_SET = 1,
CMD_EVENT_REQUEST_CLEAR = 2,
CMD_EVENT_REQUEST_CLEAR_ALL_BREAKPOINTS = 3
} CmdEvent;
typedef enum {
CMD_COMPOSITE = 100
} CmdComposite;
typedef enum {
CMD_APPDOMAIN_GET_ROOT_DOMAIN = 1,
CMD_APPDOMAIN_GET_FRIENDLY_NAME = 2,
CMD_APPDOMAIN_GET_ASSEMBLIES = 3,
CMD_APPDOMAIN_GET_ENTRY_ASSEMBLY = 4,
CMD_APPDOMAIN_CREATE_STRING = 5,
CMD_APPDOMAIN_GET_CORLIB = 6,
CMD_APPDOMAIN_CREATE_BOXED_VALUE = 7,
CMD_APPDOMAIN_CREATE_BYTE_ARRAY = 8,
} CmdAppDomain;
typedef enum {
CMD_ASSEMBLY_GET_LOCATION = 1,
CMD_ASSEMBLY_GET_ENTRY_POINT = 2,
CMD_ASSEMBLY_GET_MANIFEST_MODULE = 3,
CMD_ASSEMBLY_GET_OBJECT = 4,
CMD_ASSEMBLY_GET_TYPE = 5,
CMD_ASSEMBLY_GET_NAME = 6,
CMD_ASSEMBLY_GET_DOMAIN = 7,
CMD_ASSEMBLY_GET_METADATA_BLOB = 8,
CMD_ASSEMBLY_GET_IS_DYNAMIC = 9,
CMD_ASSEMBLY_GET_PDB_BLOB = 10,
CMD_ASSEMBLY_GET_TYPE_FROM_TOKEN = 11,
CMD_ASSEMBLY_GET_METHOD_FROM_TOKEN = 12,
CMD_ASSEMBLY_HAS_DEBUG_INFO = 13,
CMD_ASSEMBLY_GET_CATTRS = 14,
} CmdAssembly;
typedef enum {
CMD_MODULE_GET_INFO = 1,
CMD_MODULE_APPLY_CHANGES = 2, /* unused, reserved */
} CmdModule;
typedef enum {
CMD_FIELD_GET_INFO = 1,
} CmdField;
typedef enum {
CMD_METHOD_GET_NAME = 1,
CMD_METHOD_GET_DECLARING_TYPE = 2,
CMD_METHOD_GET_DEBUG_INFO = 3,
CMD_METHOD_GET_PARAM_INFO = 4,
CMD_METHOD_GET_LOCALS_INFO = 5,
CMD_METHOD_GET_INFO = 6,
CMD_METHOD_GET_BODY = 7,
CMD_METHOD_RESOLVE_TOKEN = 8,
CMD_METHOD_GET_CATTRS = 9,
CMD_METHOD_MAKE_GENERIC_METHOD = 10
} CmdMethod;
typedef enum {
CMD_TYPE_GET_INFO = 1,
CMD_TYPE_GET_METHODS = 2,
CMD_TYPE_GET_FIELDS = 3,
CMD_TYPE_GET_VALUES = 4,
CMD_TYPE_GET_OBJECT = 5,
CMD_TYPE_GET_SOURCE_FILES = 6,
CMD_TYPE_SET_VALUES = 7,
CMD_TYPE_IS_ASSIGNABLE_FROM = 8,
CMD_TYPE_GET_PROPERTIES = 9,
CMD_TYPE_GET_CATTRS = 10,
CMD_TYPE_GET_FIELD_CATTRS = 11,
CMD_TYPE_GET_PROPERTY_CATTRS = 12,
CMD_TYPE_GET_SOURCE_FILES_2 = 13,
CMD_TYPE_GET_VALUES_2 = 14,
CMD_TYPE_GET_METHODS_BY_NAME_FLAGS = 15,
CMD_TYPE_GET_INTERFACES = 16,
CMD_TYPE_GET_INTERFACE_MAP = 17,
CMD_TYPE_IS_INITIALIZED = 18,
CMD_TYPE_CREATE_INSTANCE = 19,
CMD_TYPE_GET_VALUE_SIZE = 20
} CmdType;
typedef enum {
CMD_STACK_FRAME_GET_VALUES = 1,
CMD_STACK_FRAME_GET_THIS = 2,
CMD_STACK_FRAME_SET_VALUES = 3,
CMD_STACK_FRAME_GET_DOMAIN = 4,
CMD_STACK_FRAME_SET_THIS = 5,
} CmdStackFrame;
typedef enum {
CMD_ARRAY_REF_GET_LENGTH = 1,
CMD_ARRAY_REF_GET_VALUES = 2,
CMD_ARRAY_REF_SET_VALUES = 3,
} CmdArray;
typedef enum {
CMD_STRING_REF_GET_VALUE = 1,
CMD_STRING_REF_GET_LENGTH = 2,
CMD_STRING_REF_GET_CHARS = 3
} CmdString;
typedef enum {
CMD_POINTER_GET_VALUE = 1
} CmdPointer;
typedef enum {
CMD_OBJECT_REF_GET_TYPE = 1,
CMD_OBJECT_REF_GET_VALUES = 2,
CMD_OBJECT_REF_IS_COLLECTED = 3,
CMD_OBJECT_REF_GET_ADDRESS = 4,
CMD_OBJECT_REF_GET_DOMAIN = 5,
CMD_OBJECT_REF_SET_VALUES = 6,
CMD_OBJECT_REF_GET_INFO = 7,
} CmdObject;
/*
* Contains additional information for an event
*/
typedef struct {
/* For EVENT_KIND_EXCEPTION */
MonoObject *exc;
MonoContext catch_ctx;
gboolean caught;
/* For EVENT_KIND_USER_LOG */
int level;
char *category, *message;
/* For EVENT_KIND_TYPE_LOAD */
MonoClass *klass;
/* For EVENT_KIND_CRASH */
char *dump;
MonoStackHash *hashes;
} EventInfo;
typedef struct {
guint8 *buf, *p, *end;
} Buffer;
typedef struct ReplyPacket {
int id;
int error;
Buffer *data;
} ReplyPacket;
#ifdef HOST_WIN32
#define get_last_sock_error() WSAGetLastError()
#define MONO_EWOULDBLOCK WSAEWOULDBLOCK
#define MONO_EINTR WSAEINTR
#else
#define get_last_sock_error() errno
#define MONO_EWOULDBLOCK EWOULDBLOCK
#define MONO_EINTR EINTR
#endif
#define CHECK_PROTOCOL_VERSION(major,minor) \
(protocol_version_set && (major_version > (major) || (major_version == (major) && minor_version >= (minor))))
/*
* Globals
*/
static AgentConfig agent_config;
static MonoDomain* g_BurstDebugDomain = NULL;
static BurstMonoDebuggerCallbacks g_BurstDebugCallbacks = { 0 };
static MonoClass* g_BurstKlass = NULL;
static MonoAssembly* g_BurstAssembly = NULL;
static MonoCoopMutex g_BurstDebugMutex;
#define burst_lock() do {mono_coop_mutex_lock (&g_BurstDebugMutex);}while(0)
#define burst_unlock() do {mono_coop_mutex_unlock (&g_BurstDebugMutex);}while(0)
/*
* Whenever the agent is fully initialized.
* When using the onuncaught or onthrow options, only some parts of the agent are
* initialized on startup, and the full initialization which includes connection
* establishment and the startup of the agent thread is only done in response to
* an event.
*/
static gint32 agent_inited;
#ifndef DISABLE_SOCKET_TRANSPORT
static int conn_fd;
static int listen_fd;
#endif
static int packet_id = 0;
static int objref_id = 0;
static int event_request_id = 0;
static int frame_id = 0;
static GPtrArray *event_requests;
static MonoNativeTlsKey debugger_tls_id;
static gboolean vm_start_event_sent, vm_death_event_sent, disconnected;
/* Maps MonoInternalThread -> DebuggerTlsData */
/* Protected by the loader lock */
static MonoGHashTable *thread_to_tls;
/* Maps tid -> MonoInternalThread */
/* Protected by the loader lock */
static MonoGHashTable *tid_to_thread;
/* Maps tid -> MonoThread (not MonoInternalThread) */
/* Protected by the loader lock */
static MonoGHashTable *tid_to_thread_obj;
static MonoNativeThreadId debugger_thread_id;
static MonoThreadHandle *debugger_thread_handle;
static int log_level;
static int file_check_valid_memory = -1;
static char* filename_check_valid_memory;
static gboolean embedding;
static FILE *log_file;
/* Assemblies whose assembly load event has no been sent yet */
/* Protected by the dbg lock */
static GPtrArray *pending_assembly_loads;
/* Whenever the debugger thread has exited */
static gboolean debugger_thread_exited;
/* Cond variable used to wait for debugger_thread_exited becoming true */
static MonoCoopCond debugger_thread_exited_cond;
/* Mutex for the cond var above */
static MonoCoopMutex debugger_thread_exited_mutex;
/* The protocol version of the client */
static int major_version, minor_version;
/* Whenever the variables above are set by the client */
static gboolean protocol_version_set;
/* The number of times the runtime is suspended */
static gint32 suspend_count;
/* Whenever to buffer reply messages and send them together */
static gboolean buffer_replies;
/* Buffered reply packets */
static ReplyPacket reply_packets [128];
static int nreply_packets;
#define dbg_lock mono_de_lock
#define dbg_unlock mono_de_unlock
static void transport_init (void);
static void transport_connect (const char *address);
static gboolean transport_handshake (void);
static void register_transport (DebuggerTransport *trans);
static gsize WINAPI debugger_thread (void *arg);
static void runtime_initialized (MonoProfiler *prof);
static void runtime_shutdown (MonoProfiler *prof);
static void thread_startup (MonoProfiler *prof, uintptr_t tid);
static void thread_end (MonoProfiler *prof, uintptr_t tid);
static void appdomain_load (MonoProfiler *prof, MonoDomain *domain);
static void appdomain_start_unload (MonoProfiler *prof, MonoDomain *domain);
static void appdomain_unload (MonoProfiler *prof, MonoDomain *domain);
static void emit_appdomain_load (gpointer key, gpointer value, gpointer user_data);
static void emit_thread_start (gpointer key, gpointer value, gpointer user_data);
static void invalidate_each_thread (gpointer key, gpointer value, gpointer user_data);
static void assembly_load (MonoProfiler *prof, MonoAssembly *assembly);
static void assembly_unload (MonoProfiler *prof, MonoAssembly *assembly);
static void gc_finalizing (MonoProfiler *prof);
static void gc_finalized (MonoProfiler *prof);
static void emit_assembly_load (gpointer assembly, gpointer user_data);
static void emit_type_load (gpointer key, gpointer type, gpointer user_data);
static void burst_check_source (gpointer key, gpointer type, gpointer user_data);
static void jit_done (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo);
static void jit_failed (MonoProfiler *prof, MonoMethod *method);
static void jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo);
static void suspend_current (void);
static void clear_event_requests_for_assembly (MonoAssembly *assembly);
static void clear_types_for_assembly (MonoAssembly *assembly);
static void process_profiler_event (EventKind event, gpointer arg);
/* Submodule init/cleanup */
static void event_requests_cleanup (void);
static void objrefs_init (void);
static void objrefs_cleanup (void);
static void ids_init (void);
static void ids_cleanup (void);
static void suspend_init (void);
static void start_debugger_thread (MonoError *error);
static void stop_debugger_thread (void);
static void finish_agent_init (gboolean on_startup);
static void process_profiler_event (EventKind event, gpointer arg);
static void invalidate_frames (DebuggerTlsData *tls);
/* Callbacks used by debugger-engine */
static MonoContext* tls_get_restore_state (void *the_tls);
static gboolean try_process_suspend (void *tls, MonoContext *ctx, gboolean from_breakpoint);
static gboolean begin_breakpoint_processing (void *tls, MonoContext *ctx, MonoJitInfo *ji, gboolean from_signal);
static void begin_single_step_processing (MonoContext *ctx, gboolean from_signal);
static void ss_discard_frame_context (void *the_tls);
static void ss_calculate_framecount (void *tls, MonoContext *ctx, gboolean force_use_ctx, DbgEngineStackFrame ***frames, int *nframes);
static gboolean ensure_jit (DbgEngineStackFrame* the_frame);
static int ensure_runtime_is_suspended (void);
static int get_this_async_id (DbgEngineStackFrame *frame);
static void* create_breakpoint_events (GPtrArray *ss_reqs, GPtrArray *bp_reqs, MonoJitInfo *ji, EventKind kind);
static void process_breakpoint_events (void *_evts, MonoMethod *method, MonoContext *ctx, int il_offset);
static int ss_create_init_args (SingleStepReq *ss_req, SingleStepArgs *args);
static void ss_args_destroy (SingleStepArgs *ss_args);
static int handle_multiple_ss_requests (void);
static GENERATE_TRY_GET_CLASS_WITH_CACHE (fixed_buffer, "System.Runtime.CompilerServices", "FixedBufferAttribute")
#ifndef DISABLE_SOCKET_TRANSPORT
static void
register_socket_transport (void);
#endif
static gboolean
is_debugger_thread (void)
{
MonoInternalThread *internal;
internal = mono_thread_internal_current ();
if (!internal)
return FALSE;
return internal->debugger_thread;
}
static int
parse_address (char *address, char **host, int *port)
{
char *pos = strchr (address, ':');
if (pos == NULL || pos == address)
return 1;
size_t len = pos - address;
*host = (char *)g_malloc (len + 1);
memcpy (*host, address, len);
(*host) [len] = '\0';
*port = atoi (pos + 1);
return 0;
}
static void
print_usage (void)
{
PRINT_ERROR_MSG ("Usage: mono --debugger-agent=[<option>=<value>,...] ...\n");
PRINT_ERROR_MSG ("Available options:\n");
PRINT_ERROR_MSG (" transport=<transport>\t\tTransport to use for connecting to the debugger (mandatory, possible values: 'dt_socket')\n");
PRINT_ERROR_MSG (" address=<hostname>:<port>\tAddress to connect to (mandatory)\n");
PRINT_ERROR_MSG (" loglevel=<n>\t\t\tLog level (defaults to 0)\n");
PRINT_ERROR_MSG (" logfile=<file>\t\tFile to log to (defaults to stdout)\n");
PRINT_ERROR_MSG (" suspend=y/n\t\t\tWhether to suspend after startup.\n");
PRINT_ERROR_MSG (" timeout=<n>\t\t\tTimeout for connecting in milliseconds.\n");
PRINT_ERROR_MSG (" server=y/n\t\t\tWhether to listen for a client connection.\n");
PRINT_ERROR_MSG (" keepalive=<n>\t\t\tSend keepalive events every n milliseconds.\n");
PRINT_ERROR_MSG (" setpgid=y/n\t\t\tWhether to call setpid(0, 0) after startup.\n");
PRINT_ERROR_MSG (" help\t\t\t\tPrint this help.\n");
}
static gboolean
parse_flag (const char *option, char *flag)
{
if (!strcmp (flag, "y"))
return TRUE;
else if (!strcmp (flag, "n"))
return FALSE;
else {
PRINT_ERROR_MSG ("debugger-agent: The valid values for the '%s' option are 'y' and 'n'.\n", option);
exit (1);
return FALSE;
}
}
static void
debugger_agent_parse_options (char *options)
{
char **args, **ptr;
char *host;
int port;
char *extra;
#ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
PRINT_ERROR_MSG ("--debugger-agent is not supported on this platform.\n");
exit (1);
#endif
extra = g_getenv ("MONO_SDB_ENV_OPTIONS");
if (extra) {
options = g_strdup_printf ("%s,%s", options, extra);
g_free (extra);
}
agent_config.enabled = TRUE;
agent_config.suspend = TRUE;
agent_config.server = FALSE;
agent_config.defer = FALSE;
agent_config.address = NULL;
//agent_config.log_level = 10;
args = g_strsplit (options, ",", -1);
for (ptr = args; ptr && *ptr; ptr ++) {
char *arg = *ptr;
if (strncmp (arg, "transport=", 10) == 0) {
agent_config.transport = g_strdup (arg + 10);
} else if (strncmp (arg, "address=", 8) == 0) {
agent_config.address = g_strdup (arg + 8);
} else if (strncmp (arg, "loglevel=", 9) == 0) {
agent_config.log_level = atoi (arg + 9);
} else if (strncmp (arg, "logfile=", 8) == 0) {
agent_config.log_file = g_strdup (arg + 8);
} else if (strncmp (arg, "suspend=", 8) == 0) {
agent_config.suspend = parse_flag ("suspend", arg + 8);
} else if (strncmp (arg, "server=", 7) == 0) {
agent_config.server = parse_flag ("server", arg + 7);
} else if (strncmp (arg, "onuncaught=", 11) == 0) {
agent_config.onuncaught = parse_flag ("onuncaught", arg + 11);
} else if (strncmp (arg, "onthrow=", 8) == 0) {
/* We support multiple onthrow= options */
agent_config.onthrow = g_slist_append (agent_config.onthrow, g_strdup (arg + 8));
} else if (strncmp (arg, "onthrow", 7) == 0) {
agent_config.onthrow = g_slist_append (agent_config.onthrow, g_strdup (""));
} else if (strncmp (arg, "help", 4) == 0) {
print_usage ();
exit (0);
} else if (strncmp (arg, "timeout=", 8) == 0) {
agent_config.timeout = atoi (arg + 8);
} else if (strncmp (arg, "launch=", 7) == 0) {
agent_config.launch = g_strdup (arg + 7);
} else if (strncmp (arg, "embedding=", 10) == 0) {
agent_config.embedding = atoi (arg + 10) == 1;
} else if (strncmp (arg, "keepalive=", 10) == 0) {
agent_config.keepalive = atoi (arg + 10);
} else if (strncmp (arg, "setpgid=", 8) == 0) {
agent_config.setpgid = parse_flag ("setpgid", arg + 8);
} else {
print_usage ();
exit (1);
}
}
if (agent_config.server && !agent_config.suspend) {
/* Waiting for deferred attachment */
agent_config.defer = TRUE;
if (agent_config.address == NULL) {
agent_config.address = g_strdup_printf ("0.0.0.0:%u", 56000 + (mono_process_current_pid () % 1000));
}
}
//agent_config.log_level = 0;
if (agent_config.transport == NULL) {
PRINT_ERROR_MSG ("debugger-agent: The 'transport' option is mandatory.\n");
exit (1);
}
if (agent_config.address == NULL && !agent_config.server) {
PRINT_ERROR_MSG ("debugger-agent: The 'address' option is mandatory.\n");
exit (1);
}
// FIXME:
if (!strcmp (agent_config.transport, "dt_socket")) {
if (agent_config.address && parse_address (agent_config.address, &host, &port)) {
PRINT_ERROR_MSG ("debugger-agent: The format of the 'address' options is '<host>:<port>'\n");
exit (1);
}
}
}
static gboolean disable_optimizations = TRUE;
static void
update_mdb_optimizations ()
{
gboolean enable = disable_optimizations;
#ifndef RUNTIME_IL2CPP
mini_get_debug_options ()->gen_sdb_seq_points = enable;
/*
* This is needed because currently we don't handle liveness info.
*/
mini_get_debug_options ()->mdb_optimizations = enable;
#ifndef MONO_ARCH_HAVE_CONTEXT_SET_INT_REG
/* This is needed because we can't set local variables in registers yet */
mono_disable_optimizations (MONO_OPT_LINEARS);
#endif
/*
* The stack walk done from thread_interrupt () needs to be signal safe, but it
* isn't, since it can call into mono_aot_find_jit_info () which is not signal
* safe (#3411). So load AOT info eagerly when the debugger is running as a
* workaround.
*/
mini_get_debug_options ()->load_aot_jit_info_eagerly = enable;
#endif // !RUNTIME_IL2CPP
}
MONO_API void
mono_debugger_set_generate_debug_info (gboolean enable)
{
disable_optimizations = enable;
update_mdb_optimizations ();
}
MONO_API gboolean
mono_debugger_get_generate_debug_info ()
{
return disable_optimizations;
}
MONO_API void
mono_debugger_disconnect ()
{
stop_debugger_thread ();
//restart debugger thread now that we've forcefully disconnected any clients
mono_atomic_cas_i32(&agent_inited, 0, 1);
finish_agent_init(FALSE);
}
typedef void (*MonoDebuggerAttachFunc)(gboolean attached);
static MonoDebuggerAttachFunc attach_func;
MONO_API void
mono_debugger_install_attach_detach_callback (MonoDebuggerAttachFunc func)
{
attach_func = func;
}
void
mono_debugger_set_thread_state (DebuggerTlsData *tls, MonoDebuggerThreadState expected, MonoDebuggerThreadState set)
{
g_assertf (tls, "Cannot get state of null thread", NULL);
g_assert (tls->thread_state == expected);
tls->thread_state = set;
}
MonoDebuggerThreadState
mono_debugger_get_thread_state (DebuggerTlsData *tls)
{
g_assertf (tls, "Cannot get state of null thread", NULL);
return tls->thread_state;
}
gsize
mono_debugger_tls_thread_id (DebuggerTlsData *tls)
{
if (!tls)
return 0;
return tls->thread_id;
}
// Only call this function with the loader lock held
MonoGHashTable *
mono_debugger_get_thread_states (void)
{
return thread_to_tls;
}
gboolean
mono_debugger_is_disconnected (void)
{
return disconnected;
}
static void
debugger_agent_init (void)
{
if (!agent_config.enabled)
return;
DebuggerEngineCallbacks cbs;
memset (&cbs, 0, sizeof (cbs));
cbs.tls_get_restore_state = tls_get_restore_state;
cbs.try_process_suspend = try_process_suspend;