forked from mono/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugger-agent.c
7683 lines (6434 loc) · 190 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
/*
* debugger-agent.c: Soft Debugger back-end module
*
* Author:
* Zoltan Varga (vargaz@gmail.com)
*
* Copyright 2009-2010 Novell, Inc.
*/
#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_NETDB_H
#include <netdb.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 HAVE_UCONTEXT_H
#include <ucontext.h>
#endif
#ifdef HOST_WIN32
#ifdef _MSC_VER
#include <winsock2.h>
#endif
#include <ws2tcpip.h>
#ifdef __GNUC__
/* cygwin's headers do not seem to define these */
void WSAAPI freeaddrinfo (struct addrinfo*);
int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
struct addrinfo**);
int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
char*,DWORD,int);
#endif
#endif
#ifdef PLATFORM_ANDROID
#include <linux/in.h>
#include <linux/tcp.h>
#include <sys/endian.h>
#endif
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/mono-debug-debugger.h>
#include <mono/metadata/debug-mono-symfile.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/threads-types.h>
#include <mono/metadata/socket-io.h>
#include <mono/metadata/assembly.h>
#include <mono/utils/mono-semaphore.h>
#include <mono/utils/mono-error-internals.h>
#include <mono/utils/mono-stack-unwinding.h>
#include "debugger-agent.h"
#include "mini.h"
#ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
#define DISABLE_DEBUGGER_AGENT 1
#endif
#ifdef DISABLE_SOFT_DEBUG
#define DISABLE_DEBUGGER_AGENT 1
#endif
#if defined(__MACH__)
#include <mono/utils/mono-threads.h>
#endif
#ifndef DISABLE_DEBUGGER_AGENT
#include <mono/io-layer/mono-mutex.h>
/* Definitions to make backporting to 2.6 easier */
//#define MonoInternalThread MonoThread
//#define mono_thread_internal_current mono_thread_current
#define THREAD_TO_INTERNAL(thread) (thread)->internal_thread
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;
} AgentConfig;
typedef struct
{
int id;
guint32 il_offset, native_offset;
MonoDomain *domain;
MonoMethod *method;
/*
* If method is gshared, this is the actual instance, otherwise this is equal to
* method.
*/
MonoMethod *actual_method;
MonoContext ctx;
MonoDebugMethodJitInfo *jit;
int flags;
mgreg_t *reg_locations [MONO_MAX_IREGS];
/*
* Whenever ctx is set. This is FALSE for the last frame of running threads, since
* the frame can become invalid.
*/
gboolean has_ctx;
} StackFrame;
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;
InvokeData *last_invoke;
};
typedef struct {
MonoThreadUnwindState context;
gpointer resume_event;
/* 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;
/* Number of thread interruptions not yet processed */
gint32 interrupt_count;
/* Whenever to disable breakpoints (used during invokes) */
gboolean disable_breakpoints;
/*
* Number of times this thread has been resumed using resume_thread ().
*/
guint32 resume_count;
MonoInternalThread *thread;
/*
* 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;
/*
* The callee address of the last mono_runtime_invoke call
*/
gpointer invoke_addr;
gboolean abort_requested;
/*
* The current mono_runtime_invoke invocation.
*/
InvokeData *invoke;
/*
* The context where single stepping should resume while the thread is suspended because
* of an EXCEPTION event.
*/
MonoContext catch_ctx;
gboolean has_catch_ctx;
/*
* 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 ().
*/
MonoContext restore_ctx;
} DebuggerTlsData;
/*
* Wire Protocol definitions
*/
#define HEADER_LENGTH 11
#define MAJOR_VERSION 2
#define MINOR_VERSION 4
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_EVENT = 64
} CommandSet;
typedef enum {
EVENT_KIND_VM_START = 0,
EVENT_KIND_VM_DEATH = 1,
EVENT_KIND_THREAD_START = 2,
EVENT_KIND_THREAD_DEATH = 3,
EVENT_KIND_APPDOMAIN_CREATE = 4,
EVENT_KIND_APPDOMAIN_UNLOAD = 5,
EVENT_KIND_METHOD_ENTRY = 6,
EVENT_KIND_METHOD_EXIT = 7,
EVENT_KIND_ASSEMBLY_LOAD = 8,
EVENT_KIND_ASSEMBLY_UNLOAD = 9,
EVENT_KIND_BREAKPOINT = 10,
EVENT_KIND_STEP = 11,
EVENT_KIND_TYPE_LOAD = 12,
EVENT_KIND_EXCEPTION = 13,
EVENT_KIND_KEEPALIVE = 14,
EVENT_KIND_USER_BREAK = 15,
EVENT_KIND_USER_LOG = 16
} EventKind;
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
} ErrorCode;
typedef enum {
MOD_KIND_COUNT = 1,
MOD_KIND_THREAD_ONLY = 3,
MOD_KIND_LOCATION_ONLY = 7,
MOD_KIND_EXCEPTION_ONLY = 8,
MOD_KIND_STEP = 10,
MOD_KIND_ASSEMBLY_ONLY = 11
} ModifierKind;
typedef enum {
STEP_DEPTH_INTO = 0,
STEP_DEPTH_OVER = 1,
STEP_DEPTH_OUT = 2
} StepDepth;
typedef enum {
STEP_SIZE_MIN = 0,
STEP_SIZE_LINE = 1
} StepSize;
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
} ValueTypeId;
typedef enum {
FRAME_FLAG_DEBUGGER_INVOKE = 1
} StackFrameFlags;
typedef enum {
INVOKE_FLAG_DISABLE_BREAKPOINTS = 1,
INVOKE_FLAG_SINGLE_THREADED = 2
} InvokeFlags;
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
} 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
} 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,
} 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
} CmdAssembly;
typedef enum {
CMD_MODULE_GET_INFO = 1,
} CmdModule;
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,
} 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
} CmdType;
typedef enum {
CMD_STACK_FRAME_GET_VALUES = 1,
CMD_STACK_FRAME_GET_THIS = 2,
CMD_STACK_FRAME_SET_VALUES = 3
} 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,
} CmdString;
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
} CmdObject;
typedef struct {
ModifierKind kind;
union {
int count; /* For kind == MOD_KIND_COUNT */
MonoInternalThread *thread; /* For kind == MOD_KIND_THREAD_ONLY */
MonoClass *exc_class; /* For kind == MONO_KIND_EXCEPTION_ONLY */
MonoAssembly **assemblies; /* For kind == MONO_KIND_ASSEMBLY_ONLY */
} data;
gboolean caught, uncaught; /* For kind == MOD_KIND_EXCEPTION_ONLY */
} Modifier;
typedef struct{
int id;
int event_kind;
int suspend_policy;
int nmodifiers;
gpointer info;
Modifier modifiers [MONO_ZERO_LEN_ARRAY];
} EventRequest;
/*
* Describes a single step request.
*/
typedef struct {
EventRequest *req;
MonoInternalThread *thread;
StepDepth depth;
StepSize size;
gpointer last_sp;
gpointer start_sp;
MonoMethod *last_method;
int last_line;
/* Whenever single stepping is performed using start/stop_single_stepping () */
gboolean global;
/* The list of breakpoints used to implement step-over */
GSList *bps;
} SingleStepReq;
/*
* 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;
} EventInfo;
/* Dummy structure used for the profiler callbacks */
typedef struct {
void* dummy;
} DebuggerProfiler;
#define DEBUG(level,s) do { if (G_UNLIKELY ((level) <= log_level)) { s; fflush (log_file); } } while (0)
#ifdef TARGET_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
/*
* Globals
*/
static AgentConfig agent_config;
/*
* 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 inited;
static int conn_fd;
static int listen_fd;
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 */
static MonoGHashTable *thread_to_tls;
/* Maps tid -> MonoInternalThread */
static MonoGHashTable *tid_to_thread;
/* Maps tid -> MonoThread (not MonoInternalThread) */
static MonoGHashTable *tid_to_thread_obj;
static gsize debugger_thread_id;
static HANDLE debugger_thread_handle;
static int log_level;
static gboolean embedding;
static FILE *log_file;
/* Assemblies whose assembly load event has no been sent yet */
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 mono_cond_t debugger_thread_exited_cond;
/* Mutex for the cond var above */
static mono_mutex_t debugger_thread_exited_mutex;
static DebuggerProfiler debugger_profiler;
/* The single step request instance */
static SingleStepReq *ss_req = NULL;
static gpointer ss_invoke_addr = NULL;
#ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
/* Number of single stepping operations in progress */
static int ss_count;
#endif
/* 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;
/* A hash table containing all active domains */
static GHashTable *domains;
static void transport_connect (const char *host, int port);
static guint32 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, int result);
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, int result);
static void assembly_unload (MonoProfiler *prof, MonoAssembly *assembly);
static void emit_assembly_load (gpointer assembly, gpointer user_data);
static void emit_type_load (gpointer key, gpointer type, gpointer user_data);
static void start_runtime_invoke (MonoProfiler *prof, MonoMethod *method);
static void end_runtime_invoke (MonoProfiler *prof, MonoMethod *method);
static void jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result);
static void add_pending_breakpoints (MonoMethod *method, MonoJitInfo *jinfo);
static void start_single_stepping (void);
static void stop_single_stepping (void);
static void suspend_current (void);
static void clear_event_requests_for_assembly (MonoAssembly *assembly);
static void clear_types_for_assembly (MonoAssembly *assembly);
static void clear_breakpoints_for_domain (MonoDomain *domain);
static void process_profiler_event (EventKind event, gpointer arg);
/* Submodule init/cleanup */
static void breakpoints_init (void);
static void breakpoints_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 ss_start (SingleStepReq *ss_req, MonoMethod *method, SeqPoint *sp, MonoSeqPointInfo *info, MonoContext *ctx, DebuggerTlsData *tls, gboolean step_to_catch);
static ErrorCode ss_create (MonoInternalThread *thread, StepSize size, StepDepth depth, EventRequest *req);
static void ss_destroy (SingleStepReq *req);
static void start_debugger_thread (void);
static void stop_debugger_thread (void);
static void finish_agent_init (gboolean on_startup);
static void process_profiler_event (EventKind event, gpointer arg);
static int
parse_address (char *address, char **host, int *port)
{
char *pos = strchr (address, ':');
if (pos == NULL || pos == address)
return 1;
*host = g_malloc (pos - address + 1);
strncpy (*host, address, pos - address);
(*host) [pos - address] = '\0';
*port = atoi (pos + 1);
return 0;
}
static void
print_usage (void)
{
fprintf (stderr, "Usage: mono --debugger-agent=[<option>=<value>,...] ...\n");
fprintf (stderr, "Available options:\n");
fprintf (stderr, " transport=<transport>\t\tTransport to use for connecting to the debugger (mandatory, possible values: 'dt_socket')\n");
fprintf (stderr, " address=<hostname>:<port>\tAddress to connect to (mandatory)\n");
fprintf (stderr, " loglevel=<n>\t\t\tLog level (defaults to 0)\n");
fprintf (stderr, " logfile=<file>\t\tFile to log to (defaults to stdout)\n");
fprintf (stderr, " suspend=y/n\t\t\tWhether to suspend after startup.\n");
fprintf (stderr, " timeout=<n>\t\t\tTimeout for connecting in milliseconds.\n");
fprintf (stderr, " server=y/n\t\t\tWhether to listen for a client connection.\n");
fprintf (stderr, " keepalive=<n>\t\t\tSend keepalive events every n milliseconds.\n");
fprintf (stderr, " 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 {
fprintf (stderr, "debugger-agent: The valid values for the '%s' option are 'y' and 'n'.\n", option);
exit (1);
return FALSE;
}
}
void
mono_debugger_agent_parse_options (char *options)
{
char **args, **ptr;
char *host;
int port;
#ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
fprintf (stderr, "--debugger-agent is not supported on this platform.\n");
exit (1);
#endif
agent_config.enabled = TRUE;
agent_config.suspend = TRUE;
agent_config.server = FALSE;
agent_config.defer = FALSE;
agent_config.address = NULL;
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 {
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 + (GetCurrentProcessId () % 1000));
}
}
if (agent_config.transport == NULL) {
fprintf (stderr, "debugger-agent: The 'transport' option is mandatory.\n");
exit (1);
}
if (strcmp (agent_config.transport, "dt_socket") != 0) {
fprintf (stderr, "debugger-agent: The only supported value for the 'transport' option is 'dt_socket'.\n");
exit (1);
}
if (agent_config.address == NULL && !agent_config.server) {
fprintf (stderr, "debugger-agent: The 'address' option is mandatory.\n");
exit (1);
}
if (agent_config.address && parse_address (agent_config.address, &host, &port)) {
fprintf (stderr, "debugger-agent: The format of the 'address' options is '<host>:<port>'\n");
exit (1);
}
}
void
mono_debugger_agent_init (void)
{
if (!agent_config.enabled)
return;
/* Need to know whenever a thread has acquired the loader mutex */
mono_loader_lock_track_ownership (TRUE);
event_requests = g_ptr_array_new ();
mono_mutex_init (&debugger_thread_exited_mutex, NULL);
mono_cond_init (&debugger_thread_exited_cond, NULL);
mono_profiler_install ((MonoProfiler*)&debugger_profiler, runtime_shutdown);
mono_profiler_set_events (MONO_PROFILE_APPDOMAIN_EVENTS | MONO_PROFILE_THREADS | MONO_PROFILE_ASSEMBLY_EVENTS | MONO_PROFILE_JIT_COMPILATION | MONO_PROFILE_METHOD_EVENTS);
mono_profiler_install_runtime_initialized (runtime_initialized);
mono_profiler_install_appdomain (NULL, appdomain_load, NULL, appdomain_unload);
mono_profiler_install_thread (thread_startup, thread_end);
mono_profiler_install_assembly (NULL, assembly_load, assembly_unload, NULL);
mono_profiler_install_jit_end (jit_end);
mono_profiler_install_method_invoke (start_runtime_invoke, end_runtime_invoke);
mono_native_tls_alloc (&debugger_tls_id, NULL);
thread_to_tls = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_GC);
MONO_GC_REGISTER_ROOT_FIXED (thread_to_tls);
tid_to_thread = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC);
MONO_GC_REGISTER_ROOT_FIXED (tid_to_thread);
tid_to_thread_obj = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC);
MONO_GC_REGISTER_ROOT_FIXED (tid_to_thread_obj);
pending_assembly_loads = g_ptr_array_new ();
domains = g_hash_table_new (mono_aligned_addr_hash, NULL);
log_level = agent_config.log_level;
embedding = agent_config.embedding;
disconnected = TRUE;
if (agent_config.log_file) {
log_file = fopen (agent_config.log_file, "w+");
if (!log_file) {
fprintf (stderr, "Unable to create log file '%s': %s.\n", agent_config.log_file, strerror (errno));
exit (1);
}
} else {
log_file = stdout;
}
ids_init ();
objrefs_init ();
breakpoints_init ();
suspend_init ();
mini_get_debug_options ()->gen_seq_points = TRUE;
/*
* This is needed because currently we don't handle liveness info.
*/
mini_get_debug_options ()->mdb_optimizations = TRUE;
#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
if (!agent_config.onuncaught && !agent_config.onthrow)
finish_agent_init (TRUE);
#if defined(__MACH__)
/*FIXME Under darwin, we need to disable the new interruption code since sdb needs to old signal based one.*/
mono_thread_info_disable_new_interrupt (TRUE);
#endif
}
/*
* finish_agent_init:
*
* Finish the initialization of the agent. This involves connecting the transport
* and starting the agent thread. This is either done at startup, or
* in response to some event like an unhandled exception.
*/
static void
finish_agent_init (gboolean on_startup)
{
char *host;
int port;
int res;
if (InterlockedCompareExchange (&inited, 1, 0) == 1)
return;
if (agent_config.launch) {
char *argv [16];
// FIXME: Generated address
// FIXME: Races with transport_connect ()
argv [0] = agent_config.launch;
argv [1] = agent_config.transport;
argv [2] = agent_config.address;
argv [3] = NULL;
res = g_spawn_async_with_pipes (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
if (!res) {
fprintf (stderr, "Failed to execute '%s'.\n", agent_config.launch);
exit (1);
}
}
if (agent_config.address) {
res = parse_address (agent_config.address, &host, &port);
g_assert (res == 0);
} else {
host = NULL;
port = 0;
}
transport_connect (host, port);
if (!on_startup) {
/* Do some which is usually done after sending the VMStart () event */
vm_start_event_sent = TRUE;
start_debugger_thread ();
}
}
static void
mono_debugger_agent_cleanup (void)
{
if (!inited)
return;
stop_debugger_thread ();
breakpoints_cleanup ();
objrefs_cleanup ();
ids_cleanup ();
mono_mutex_destroy (&debugger_thread_exited_mutex);
mono_cond_destroy (&debugger_thread_exited_cond);
}
/*
* recv_length:
*
* recv() + handle incomplete reads and EINTR
*/
static int
recv_length (int fd, void *buf, int len, int flags)
{
int res;
int total = 0;
do {
again:
res = recv (fd, (char *) buf + total, len - total, flags);
if (res > 0)
total += res;
if (agent_config.keepalive && res == -1 && get_last_sock_error () == MONO_EWOULDBLOCK) {
process_profiler_event (EVENT_KIND_KEEPALIVE, NULL);
goto again;
}
} while ((res > 0 && total < len) || (res == -1 && get_last_sock_error () == MONO_EINTR));
return total;
}
#ifndef TARGET_PS3