2 * debugger-agent.c: Debugger back-end module
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2009 Novell, Inc.
14 #ifdef HAVE_SYS_TYPES_H
15 #include <sys/types.h>
17 #ifdef HAVE_SYS_SOCKET_H
18 #include <sys/socket.h>
20 #ifdef HAVE_NETINET_TCP_H
21 #include <netinet/tcp.h>
23 #ifdef HAVE_NETINET_IN_H
24 #include <netinet/in.h>
39 #ifdef HAVE_UCONTEXT_H
49 /* cygwin's headers do not seem to define these */
50 void WSAAPI freeaddrinfo (struct addrinfo*);
51 int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
53 int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
58 #ifdef PLATFORM_ANDROID
60 #include <linux/tcp.h>
61 #include <sys/endian.h>
64 #include <mono/metadata/mono-debug.h>
65 #include <mono/metadata/mono-debug-debugger.h>
66 #include <mono/metadata/debug-mono-symfile.h>
67 #include <mono/metadata/gc-internal.h>
68 #include <mono/metadata/threads-types.h>
69 #include <mono/metadata/socket-io.h>
70 #include <mono/utils/mono-semaphore.h>
71 #include "debugger-agent.h"
74 #ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
75 #define DISABLE_DEBUGGER_AGENT 1
78 #ifdef DISABLE_SOFT_DEBUG
79 #define DISABLE_DEBUGGER_AGENT 1
82 #ifndef DISABLE_DEBUGGER_AGENT
83 #include <mono/io-layer/mono-mutex.h>
85 /* Definitions to make backporting to 2.6 easier */
86 //#define MonoInternalThread MonoThread
87 //#define mono_thread_internal_current mono_thread_current
88 #define THREAD_TO_INTERNAL(thread) (thread)->internal_thread
111 MonoDebugMethodJitInfo *jit;
114 * Whenever ctx is set. This is FALSE for the last frame of running threads, since
115 * the frame can become invalid.
126 /* This is the context which needs to be restored after the invoke */
130 * If this is set, invoke this method with the arguments given by ARGS.
134 guint32 suspend_count;
141 gboolean has_context;
142 gpointer resume_event;
143 /* This is computed on demand when it is requested using the wire protocol */
144 /* It is freed up when the thread is resumed */
148 * Whenever the frame info is up-to-date. If not, compute_frame_info () will need to
151 gboolean frames_up_to_date;
153 * Points to data about a pending invoke which needs to be executed after the thread
158 * Set to TRUE if this thread is suspended in suspend_current () or it is executing
163 * Signals whenever the thread is in the process of suspending, i.e. it will suspend
164 * within a finite amount of time.
168 * Set to TRUE if this thread is suspended in suspend_current ().
170 gboolean really_suspended;
171 /* Used to pass the context to the breakpoint/single step handler */
172 MonoContext handler_ctx;
173 /* Whenever thread_stop () was called for this thread */
176 /* Number of thread interruptions not yet processed */
177 gint32 interrupt_count;
179 /* Whenever to disable breakpoints (used during invokes) */
180 gboolean disable_breakpoints;
183 * Number of times this thread has been resumed using resume_thread ().
185 guint32 resume_count;
187 MonoInternalThread *thread;
190 * Information about the frame which transitioned to native code for running
193 StackFrameInfo async_last_frame;
196 * The context where the stack walk can be started for running threads.
198 MonoContext async_ctx;
200 gboolean has_async_ctx;
203 * The lmf where the stack walk can be started for running threads.
208 * The callee address of the last mono_runtime_invoke call
210 gpointer invoke_addr;
214 * Wire Protocol definitions
217 #define HEADER_LENGTH 11
219 #define MAJOR_VERSION 2
220 #define MINOR_VERSION 1
224 CMD_SET_OBJECT_REF = 9,
225 CMD_SET_STRING_REF = 10,
227 CMD_SET_ARRAY_REF = 13,
228 CMD_SET_EVENT_REQUEST = 15,
229 CMD_SET_STACK_FRAME = 16,
230 CMD_SET_APPDOMAIN = 20,
231 CMD_SET_ASSEMBLY = 21,
239 EVENT_KIND_VM_START = 0,
240 EVENT_KIND_VM_DEATH = 1,
241 EVENT_KIND_THREAD_START = 2,
242 EVENT_KIND_THREAD_DEATH = 3,
243 EVENT_KIND_APPDOMAIN_CREATE = 4,
244 EVENT_KIND_APPDOMAIN_UNLOAD = 5,
245 EVENT_KIND_METHOD_ENTRY = 6,
246 EVENT_KIND_METHOD_EXIT = 7,
247 EVENT_KIND_ASSEMBLY_LOAD = 8,
248 EVENT_KIND_ASSEMBLY_UNLOAD = 9,
249 EVENT_KIND_BREAKPOINT = 10,
250 EVENT_KIND_STEP = 11,
251 EVENT_KIND_TYPE_LOAD = 12,
252 EVENT_KIND_EXCEPTION = 13
256 SUSPEND_POLICY_NONE = 0,
257 SUSPEND_POLICY_EVENT_THREAD = 1,
258 SUSPEND_POLICY_ALL = 2
263 ERR_INVALID_OBJECT = 20,
264 ERR_INVALID_FIELDID = 25,
265 ERR_INVALID_FRAMEID = 30,
266 ERR_NOT_IMPLEMENTED = 100,
267 ERR_NOT_SUSPENDED = 101,
268 ERR_INVALID_ARGUMENT = 102,
274 MOD_KIND_THREAD_ONLY = 3,
275 MOD_KIND_LOCATION_ONLY = 7,
276 MOD_KIND_EXCEPTION_ONLY = 8,
278 MOD_KIND_ASSEMBLY_ONLY = 11
293 TOKEN_TYPE_STRING = 0,
295 TOKEN_TYPE_FIELD = 2,
296 TOKEN_TYPE_METHOD = 3,
297 TOKEN_TYPE_UNKNOWN = 4
301 VALUE_TYPE_ID_NULL = 0xf0,
302 VALUE_TYPE_ID_TYPE = 0xf1
306 FRAME_FLAG_DEBUGGER_INVOKE = 1
310 INVOKE_FLAG_DISABLE_BREAKPOINTS = 1,
311 INVOKE_FLAG_SINGLE_THREADED = 2
316 CMD_VM_ALL_THREADS = 2,
321 CMD_VM_INVOKE_METHOD = 7,
322 CMD_VM_SET_PROTOCOL_VERSION = 8
326 CMD_THREAD_GET_FRAME_INFO = 1,
327 CMD_THREAD_GET_NAME = 2,
328 CMD_THREAD_GET_STATE = 3,
329 CMD_THREAD_GET_INFO = 4
333 CMD_EVENT_REQUEST_SET = 1,
334 CMD_EVENT_REQUEST_CLEAR = 2,
335 CMD_EVENT_REQUEST_CLEAR_ALL_BREAKPOINTS = 3
343 CMD_APPDOMAIN_GET_ROOT_DOMAIN = 1,
344 CMD_APPDOMAIN_GET_FRIENDLY_NAME = 2,
345 CMD_APPDOMAIN_GET_ASSEMBLIES = 3,
346 CMD_APPDOMAIN_GET_ENTRY_ASSEMBLY = 4,
347 CMD_APPDOMAIN_CREATE_STRING = 5,
348 CMD_APPDOMAIN_GET_CORLIB = 6,
349 CMD_APPDOMAIN_CREATE_BOXED_VALUE = 7,
353 CMD_ASSEMBLY_GET_LOCATION = 1,
354 CMD_ASSEMBLY_GET_ENTRY_POINT = 2,
355 CMD_ASSEMBLY_GET_MANIFEST_MODULE = 3,
356 CMD_ASSEMBLY_GET_OBJECT = 4,
357 CMD_ASSEMBLY_GET_TYPE = 5,
358 CMD_ASSEMBLY_GET_NAME = 6
362 CMD_MODULE_GET_INFO = 1,
366 CMD_METHOD_GET_NAME = 1,
367 CMD_METHOD_GET_DECLARING_TYPE = 2,
368 CMD_METHOD_GET_DEBUG_INFO = 3,
369 CMD_METHOD_GET_PARAM_INFO = 4,
370 CMD_METHOD_GET_LOCALS_INFO = 5,
371 CMD_METHOD_GET_INFO = 6,
372 CMD_METHOD_GET_BODY = 7,
373 CMD_METHOD_RESOLVE_TOKEN = 8,
377 CMD_TYPE_GET_INFO = 1,
378 CMD_TYPE_GET_METHODS = 2,
379 CMD_TYPE_GET_FIELDS = 3,
380 CMD_TYPE_GET_VALUES = 4,
381 CMD_TYPE_GET_OBJECT = 5,
382 CMD_TYPE_GET_SOURCE_FILES = 6,
383 CMD_TYPE_SET_VALUES = 7,
384 CMD_TYPE_IS_ASSIGNABLE_FROM = 8,
385 CMD_TYPE_GET_PROPERTIES = 9,
386 CMD_TYPE_GET_CATTRS = 10,
387 CMD_TYPE_GET_FIELD_CATTRS = 11,
388 CMD_TYPE_GET_PROPERTY_CATTRS = 12
392 CMD_STACK_FRAME_GET_VALUES = 1,
393 CMD_STACK_FRAME_GET_THIS = 2,
394 CMD_STACK_FRAME_SET_VALUES = 3
398 CMD_ARRAY_REF_GET_LENGTH = 1,
399 CMD_ARRAY_REF_GET_VALUES = 2,
400 CMD_ARRAY_REF_SET_VALUES = 3,
404 CMD_STRING_REF_GET_VALUE = 1,
408 CMD_OBJECT_REF_GET_TYPE = 1,
409 CMD_OBJECT_REF_GET_VALUES = 2,
410 CMD_OBJECT_REF_IS_COLLECTED = 3,
411 CMD_OBJECT_REF_GET_ADDRESS = 4,
412 CMD_OBJECT_REF_GET_DOMAIN = 5,
413 CMD_OBJECT_REF_SET_VALUES = 6
419 int count; /* For kind == MOD_KIND_COUNT */
420 MonoInternalThread *thread; /* For kind == MOD_KIND_THREAD_ONLY */
421 MonoClass *exc_class; /* For kind == MONO_KIND_EXCEPTION_ONLY */
422 MonoAssembly **assemblies; /* For kind == MONO_KIND_ASSEMBLY_ONLY */
424 gboolean caught, uncaught; /* For kind == MOD_KIND_EXCEPTION_ONLY */
433 Modifier modifiers [MONO_ZERO_LEN_ARRAY];
437 * Describes a single step request.
441 MonoInternalThread *thread;
446 MonoMethod *last_method;
448 /* Whenever single stepping is performed using start/stop_single_stepping () */
450 /* The list of breakpoints used to implement step-over */
455 * Contains additional information for an event
458 /* For EVENT_KIND_EXCEPTION */
460 MonoContext catch_ctx;
464 /* Dummy structure used for the profiler callbacks */
469 #define DEBUG(level,s) do { if (G_UNLIKELY ((level) <= log_level)) { s; fflush (log_file); } } while (0)
475 static AgentConfig agent_config;
478 * Whenever the agent is fully initialized.
479 * When using the onuncaught or onthrow options, only some parts of the agent are
480 * initialized on startup, and the full initialization which includes connection
481 * establishment and the startup of the agent thread is only done in response to
484 static gint32 inited;
488 static int packet_id = 0;
490 static int objref_id = 0;
492 static int event_request_id = 0;
494 static int frame_id = 0;
496 static GPtrArray *event_requests;
498 static guint32 debugger_tls_id;
500 static gboolean vm_start_event_sent, vm_death_event_sent, disconnected;
502 /* Maps MonoInternalThread -> DebuggerTlsData */
503 static MonoGHashTable *thread_to_tls;
505 /* Maps tid -> MonoInternalThread */
506 static MonoGHashTable *tid_to_thread;
508 /* Maps tid -> MonoThread (not MonoInternalThread) */
509 static MonoGHashTable *tid_to_thread_obj;
511 static gsize debugger_thread_id;
513 static HANDLE debugger_thread_handle;
515 static int log_level;
517 static FILE *log_file;
519 /* Classes whose class load event has been sent */
520 static GHashTable *loaded_classes;
522 /* Assemblies whose assembly load event has no been sent yet */
523 static GPtrArray *pending_assembly_loads;
525 /* Whenever the debugger thread has exited */
526 static gboolean debugger_thread_exited;
528 /* Cond variable used to wait for debugger_thread_exited becoming true */
529 static mono_cond_t debugger_thread_exited_cond;
531 /* Mutex for the cond var above */
532 static mono_mutex_t debugger_thread_exited_mutex;
534 static DebuggerProfiler debugger_profiler;
536 /* The single step request instance */
537 static SingleStepReq *ss_req = NULL;
538 static gpointer ss_invoke_addr = NULL;
540 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
541 /* Number of single stepping operations in progress */
545 /* The protocol version of the client */
546 static int major_version, minor_version;
548 /* Whenever the variables above are set by the client */
549 static gboolean protocol_version_set;
551 static void transport_connect (const char *host, int port);
553 static guint32 WINAPI debugger_thread (void *arg);
555 static void runtime_initialized (MonoProfiler *prof);
557 static void runtime_shutdown (MonoProfiler *prof);
559 static void thread_startup (MonoProfiler *prof, intptr_t tid);
561 static void thread_end (MonoProfiler *prof, intptr_t tid);
563 static void appdomain_load (MonoProfiler *prof, MonoDomain *domain, int result);
565 static void appdomain_unload (MonoProfiler *prof, MonoDomain *domain);
567 static void invalidate_each_thread (gpointer key, gpointer value, gpointer user_data);
569 static void assembly_load (MonoProfiler *prof, MonoAssembly *assembly, int result);
571 static void assembly_unload (MonoProfiler *prof, MonoAssembly *assembly);
573 static void start_runtime_invoke (MonoProfiler *prof, MonoMethod *method);
575 static void end_runtime_invoke (MonoProfiler *prof, MonoMethod *method);
577 static void jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result);
579 static void add_pending_breakpoints (MonoMethod *method, MonoJitInfo *jinfo);
581 static void start_single_stepping (void);
583 static void stop_single_stepping (void);
585 static void suspend_current (void);
587 /* Submodule init/cleanup */
588 static void breakpoints_init (void);
589 static void breakpoints_cleanup (void);
591 static void objrefs_init (void);
592 static void objrefs_cleanup (void);
594 static void ids_init (void);
595 static void ids_cleanup (void);
597 static void suspend_init (void);
599 static void ss_start (SingleStepReq *ss_req, MonoMethod *method, SeqPoint *sp, MonoSeqPointInfo *info, MonoContext *ctx, DebuggerTlsData *tls);
600 static ErrorCode ss_create (MonoInternalThread *thread, StepSize size, StepDepth depth, EventRequest *req);
601 static void ss_destroy (SingleStepReq *req);
603 static void start_debugger_thread (void);
605 static void finish_agent_init (gboolean on_startup);
608 parse_address (char *address, char **host, int *port)
610 char *pos = strchr (address, ':');
612 if (pos == NULL || pos == address)
615 *host = g_malloc (pos - address + 1);
616 strncpy (*host, address, pos - address);
617 (*host) [pos - address] = '\0';
619 *port = atoi (pos + 1);
627 fprintf (stderr, "Usage: mono --debugger-agent=[<option>=<value>,...] ...\n");
628 fprintf (stderr, "Available options:\n");
629 fprintf (stderr, " transport=<transport>\t\tTransport to use for connecting to the debugger (mandatory, possible values: 'dt_socket')\n");
630 fprintf (stderr, " address=<hostname>:<port>\tAddress to connect to (mandatory)\n");
631 fprintf (stderr, " loglevel=<n>\t\t\tLog level (defaults to 0)\n");
632 fprintf (stderr, " logfile=<file>\t\tFile to log to (defaults to stdout)\n");
633 fprintf (stderr, " suspend=y/n\t\t\tWhenever to suspend after startup.\n");
634 fprintf (stderr, " timeout=<n>\t\t\tTimeout for connecting in milliseconds.\n");
635 fprintf (stderr, " help\t\t\t\tPrint this help.\n");
639 parse_flag (const char *option, char *flag)
641 if (!strcmp (flag, "y"))
643 else if (!strcmp (flag, "n"))
646 fprintf (stderr, "debugger-agent: The valid values for the '%s' option are 'y' and 'n'.\n", option);
653 mono_debugger_agent_parse_options (char *options)
659 #ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
660 fprintf (stderr, "--debugger-agent is not supported on this platform.\n");
664 agent_config.enabled = TRUE;
665 agent_config.suspend = TRUE;
666 agent_config.server = FALSE;
668 args = g_strsplit (options, ",", -1);
669 for (ptr = args; ptr && *ptr; ptr ++) {
672 if (strncmp (arg, "transport=", 10) == 0) {
673 agent_config.transport = g_strdup (arg + 10);
674 } else if (strncmp (arg, "address=", 8) == 0) {
675 agent_config.address = g_strdup (arg + 8);
676 } else if (strncmp (arg, "loglevel=", 9) == 0) {
677 agent_config.log_level = atoi (arg + 9);
678 } else if (strncmp (arg, "logfile=", 8) == 0) {
679 agent_config.log_file = g_strdup (arg + 8);
680 } else if (strncmp (arg, "suspend=", 8) == 0) {
681 agent_config.suspend = parse_flag ("suspend", arg + 8);
682 } else if (strncmp (arg, "server=", 7) == 0) {
683 agent_config.server = parse_flag ("server", arg + 7);
684 } else if (strncmp (arg, "onuncaught=", 11) == 0) {
685 agent_config.onuncaught = parse_flag ("onuncaught", arg + 11);
686 } else if (strncmp (arg, "onthrow=", 8) == 0) {
687 /* We support multiple onthrow= options */
688 agent_config.onthrow = g_slist_append (agent_config.onthrow, g_strdup (arg + 8));
689 } else if (strncmp (arg, "onthrow", 7) == 0) {
690 agent_config.onthrow = g_slist_append (agent_config.onthrow, g_strdup (""));
691 } else if (strncmp (arg, "help", 4) == 0) {
694 } else if (strncmp (arg, "timeout=", 8) == 0) {
695 agent_config.timeout = atoi (arg + 8);
696 } else if (strncmp (arg, "launch=", 7) == 0) {
697 agent_config.launch = g_strdup (arg + 7);
704 if (agent_config.transport == NULL) {
705 fprintf (stderr, "debugger-agent: The 'transport' option is mandatory.\n");
708 if (strcmp (agent_config.transport, "dt_socket") != 0) {
709 fprintf (stderr, "debugger-agent: The only supported value for the 'transport' option is 'dt_socket'.\n");
713 if (agent_config.address == NULL && !agent_config.server) {
714 fprintf (stderr, "debugger-agent: The 'address' option is mandatory.\n");
718 if (agent_config.address && parse_address (agent_config.address, &host, &port)) {
719 fprintf (stderr, "debugger-agent: The format of the 'address' options is '<host>:<port>'\n");
725 mono_debugger_agent_init (void)
727 if (!agent_config.enabled)
730 /* Need to know whenever a thread has acquired the loader mutex */
731 mono_loader_lock_track_ownership (TRUE);
733 event_requests = g_ptr_array_new ();
735 mono_mutex_init (&debugger_thread_exited_mutex, NULL);
736 mono_cond_init (&debugger_thread_exited_cond, NULL);
738 mono_profiler_install ((MonoProfiler*)&debugger_profiler, runtime_shutdown);
739 mono_profiler_set_events (MONO_PROFILE_APPDOMAIN_EVENTS | MONO_PROFILE_THREADS | MONO_PROFILE_ASSEMBLY_EVENTS | MONO_PROFILE_JIT_COMPILATION | MONO_PROFILE_METHOD_EVENTS);
740 mono_profiler_install_runtime_initialized (runtime_initialized);
741 mono_profiler_install_appdomain (NULL, appdomain_load, NULL, appdomain_unload);
742 mono_profiler_install_thread (thread_startup, thread_end);
743 mono_profiler_install_assembly (NULL, assembly_load, assembly_unload, NULL);
744 mono_profiler_install_jit_end (jit_end);
745 mono_profiler_install_method_invoke (start_runtime_invoke, end_runtime_invoke);
747 debugger_tls_id = TlsAlloc ();
749 thread_to_tls = mono_g_hash_table_new (NULL, NULL);
750 MONO_GC_REGISTER_ROOT (thread_to_tls);
752 tid_to_thread = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC);
753 MONO_GC_REGISTER_ROOT (tid_to_thread);
755 tid_to_thread_obj = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC);
756 MONO_GC_REGISTER_ROOT (tid_to_thread_obj);
758 loaded_classes = g_hash_table_new (mono_aligned_addr_hash, NULL);
759 pending_assembly_loads = g_ptr_array_new ();
761 log_level = agent_config.log_level;
763 if (agent_config.log_file) {
764 log_file = fopen (agent_config.log_file, "w+");
766 fprintf (stderr, "Unable to create log file '%s': %s.\n", agent_config.log_file, strerror (errno));
778 mini_get_debug_options ()->gen_seq_points = TRUE;
780 * This is needed because currently we don't handle liveness info.
782 mini_get_debug_options ()->mdb_optimizations = TRUE;
784 /* This is needed because we can't set local variables in registers yet */
785 mono_disable_optimizations (MONO_OPT_LINEARS);
787 if (!agent_config.onuncaught && !agent_config.onthrow)
788 finish_agent_init (TRUE);
794 * Finish the initialization of the agent. This involves connecting the transport
795 * and starting the agent thread. This is either done at startup, or
796 * in response to some event like an unhandled exception.
799 finish_agent_init (gboolean on_startup)
805 if (InterlockedCompareExchange (&inited, 1, 0) == 1)
808 if (agent_config.launch) {
811 // FIXME: Generated address
812 // FIXME: Races with transport_connect ()
814 argv [0] = agent_config.launch;
815 argv [1] = agent_config.transport;
816 argv [2] = agent_config.address;
819 res = g_spawn_async_with_pipes (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
821 fprintf (stderr, "Failed to execute '%s'.\n", agent_config.launch);
826 if (agent_config.address) {
827 res = parse_address (agent_config.address, &host, &port);
834 transport_connect (host, port);
837 /* Do some which is usually done after sending the VMStart () event */
838 vm_start_event_sent = TRUE;
839 start_debugger_thread ();
844 mono_debugger_agent_cleanup (void)
849 /* This will interrupt the agent thread */
850 /* Close the read part only so it can still send back replies */
852 shutdown (conn_fd, SD_RECEIVE);
854 shutdown (conn_fd, SHUT_RD);
858 * Wait for the thread to exit.
860 * If we continue with the shutdown without waiting for it, then the client might
861 * not receive an answer to its last command like a resume.
862 * The WaitForSingleObject infrastructure doesn't seem to work during shutdown, so
865 //WaitForSingleObject (debugger_thread_handle, INFINITE);
866 if (GetCurrentThreadId () != debugger_thread_id) {
867 mono_mutex_lock (&debugger_thread_exited_mutex);
868 if (!debugger_thread_exited) {
870 if (WAIT_TIMEOUT == WaitForSingleObject(debugger_thread_exited_cond, 0)) {
871 mono_mutex_unlock (&debugger_thread_exited_mutex);
873 mono_mutex_lock (&debugger_thread_exited_mutex);
876 mono_cond_wait (&debugger_thread_exited_cond, &debugger_thread_exited_mutex);
879 mono_mutex_unlock (&debugger_thread_exited_mutex);
882 breakpoints_cleanup ();
887 shutdown (conn_fd, SD_BOTH);
889 shutdown (conn_fd, SHUT_RDWR);
892 mono_mutex_destroy (&debugger_thread_exited_mutex);
893 mono_cond_destroy (&debugger_thread_exited_cond);
899 * recv() + handle incomplete reads and EINTR
902 recv_length (int fd, void *buf, int len, int flags)
908 res = recv (fd, (char *) buf + total, len - total, flags);
911 } while ((res > 0 && total < len) || (res == -1 && errno == EINTR));
917 * Connect/Listen on HOST:PORT. If HOST is NULL, generate an address and listen on it.
920 transport_connect (const char *host, int port)
922 struct addrinfo hints;
923 struct addrinfo *result, *rp;
925 char port_string [128];
926 char handshake_msg [128];
932 sprintf (port_string, "%d", port);
934 mono_network_init ();
936 /* Obtain address(es) matching host/port */
938 memset (&hints, 0, sizeof (struct addrinfo));
939 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
940 hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
942 hints.ai_protocol = 0; /* Any protocol */
944 s = getaddrinfo (host, port_string, &hints, &result);
946 fprintf (stderr, "debugger-agent: Unable to connect to %s:%d: %s\n", host, port, gai_strerror (s));
951 if (agent_config.server) {
952 /* Wait for a connection */
954 struct sockaddr_in addr;
957 /* No address, generate one */
958 sfd = socket (AF_INET, SOCK_STREAM, 0);
961 /* This will bind the socket to a random port */
962 res = listen (sfd, 16);
964 fprintf (stderr, "debugger-agent: Unable to setup listening socket: %s\n", strerror (errno));
968 addrlen = sizeof (addr);
969 memset (&addr, 0, sizeof (addr));
970 res = getsockname (sfd, &addr, &addrlen);
974 port = ntohs (addr.sin_port);
976 /* Emit the address to stdout */
977 /* FIXME: Should print another interface, not localhost */
978 printf ("%s:%d\n", host, port);
980 /* Listen on the provided address */
981 for (rp = result; rp != NULL; rp = rp->ai_next) {
982 sfd = socket (rp->ai_family, rp->ai_socktype,
987 res = bind (sfd, rp->ai_addr, rp->ai_addrlen);
991 res = listen (sfd, 16);
999 * this function is not present on win2000 which we still support, and the
1000 * workaround described here:
1001 * http://msdn.microsoft.com/en-us/library/ms737931(VS.85).aspx
1002 * only works with MSVC.
1004 freeaddrinfo (result);
1008 DEBUG (1, fprintf (log_file, "Listening on %s:%d (timeout=%d ms)...\n", host, port, agent_config.timeout));
1010 if (agent_config.timeout) {
1015 tv.tv_usec = agent_config.timeout * 1000;
1017 FD_SET (sfd, &readfds);
1018 res = select (sfd + 1, &readfds, NULL, NULL, &tv);
1020 fprintf (stderr, "debugger-agent: Timed out waiting to connect.\n");
1025 conn_fd = accept (sfd, NULL, NULL);
1026 if (conn_fd == -1) {
1027 fprintf (stderr, "debugger-agent: Unable to listen on %s:%d\n", host, port);
1031 DEBUG (1, fprintf (log_file, "Accepted connection from client, socket fd=%d.\n", conn_fd));
1033 /* Connect to the specified address */
1034 /* FIXME: Respect the timeout */
1035 for (rp = result; rp != NULL; rp = rp->ai_next) {
1036 sfd = socket (rp->ai_family, rp->ai_socktype,
1041 if (connect (sfd, rp->ai_addr, rp->ai_addrlen) != -1)
1042 break; /* Success */
1050 /* See the comment above */
1051 freeaddrinfo (result);
1055 fprintf (stderr, "debugger-agent: Unable to connect to %s:%d\n", host, port);
1060 /* Write handshake message */
1061 sprintf (handshake_msg, "DWP-Handshake");
1063 res = send (conn_fd, handshake_msg, strlen (handshake_msg), 0);
1064 } while (res == -1 && errno == EINTR);
1065 g_assert (res != -1);
1068 res = recv_length (conn_fd, buf, strlen (handshake_msg), 0);
1069 if ((res != strlen (handshake_msg)) || (memcmp (buf, handshake_msg, strlen (handshake_msg) != 0))) {
1070 fprintf (stderr, "debugger-agent: DWP handshake failed.\n");
1075 * To support older clients, the client sends its protocol version after connecting
1076 * using a command. Until that is received, default to our protocol version.
1078 major_version = MAJOR_VERSION;
1079 minor_version = MINOR_VERSION;
1080 protocol_version_set = FALSE;
1083 * Set TCP_NODELAY on the socket so the client receives events/command
1084 * results immediately.
1088 int result = setsockopt(conn_fd,
1093 g_assert (result >= 0);
1098 transport_send (guint8 *data, int len)
1103 res = send (conn_fd, data, len, 0);
1104 } while (res == -1 && errno == EINTR);
1112 start_debugger_thread (void)
1116 debugger_thread_handle = mono_create_thread (NULL, 0, debugger_thread, NULL, 0, &tid);
1117 g_assert (debugger_thread_handle);
1121 * Functions to decode protocol data
1125 decode_byte (guint8 *buf, guint8 **endbuf, guint8 *limit)
1128 g_assert (*endbuf <= limit);
1133 decode_int (guint8 *buf, guint8 **endbuf, guint8 *limit)
1136 g_assert (*endbuf <= limit);
1138 return (((int)buf [0]) << 24) | (((int)buf [1]) << 16) | (((int)buf [2]) << 8) | (((int)buf [3]) << 0);
1141 static inline gint64
1142 decode_long (guint8 *buf, guint8 **endbuf, guint8 *limit)
1144 guint32 high = decode_int (buf, &buf, limit);
1145 guint32 low = decode_int (buf, &buf, limit);
1149 return ((((guint64)high) << 32) | ((guint64)low));
1153 decode_id (guint8 *buf, guint8 **endbuf, guint8 *limit)
1155 return decode_int (buf, endbuf, limit);
1159 decode_string (guint8 *buf, guint8 **endbuf, guint8 *limit)
1161 int len = decode_int (buf, &buf, limit);
1164 s = g_malloc (len + 1);
1167 memcpy (s, buf, len);
1176 * Functions to encode protocol data
1180 guint8 *buf, *p, *end;
1184 buffer_init (Buffer *buf, int size)
1186 buf->buf = g_malloc (size);
1188 buf->end = buf->buf + size;
1192 buffer_make_room (Buffer *buf, int size)
1194 if (buf->end - buf->p < size) {
1195 int new_size = buf->end - buf->buf + size + 32;
1196 guint8 *p = g_realloc (buf->buf, new_size);
1197 size = buf->p - buf->buf;
1200 buf->end = buf->buf + new_size;
1205 buffer_add_byte (Buffer *buf, guint8 val)
1207 buffer_make_room (buf, 1);
1213 buffer_add_int (Buffer *buf, guint32 val)
1215 buffer_make_room (buf, 4);
1216 buf->p [0] = (val >> 24) & 0xff;
1217 buf->p [1] = (val >> 16) & 0xff;
1218 buf->p [2] = (val >> 8) & 0xff;
1219 buf->p [3] = (val >> 0) & 0xff;
1224 buffer_add_long (Buffer *buf, guint64 l)
1226 buffer_add_int (buf, (l >> 32) & 0xffffffff);
1227 buffer_add_int (buf, (l >> 0) & 0xffffffff);
1231 buffer_add_id (Buffer *buf, int id)
1233 buffer_add_int (buf, (guint64)id);
1237 buffer_add_data (Buffer *buf, guint8 *data, int len)
1239 buffer_make_room (buf, len);
1240 memcpy (buf->p, data, len);
1245 buffer_add_string (Buffer *buf, const char *str)
1250 buffer_add_int (buf, 0);
1253 buffer_add_int (buf, len);
1254 buffer_add_data (buf, (guint8*)str, len);
1259 buffer_free (Buffer *buf)
1265 send_packet (int command_set, int command, Buffer *data)
1271 id = InterlockedIncrement (&packet_id);
1273 len = data->p - data->buf + 11;
1274 buffer_init (&buf, len);
1275 buffer_add_int (&buf, len);
1276 buffer_add_int (&buf, id);
1277 buffer_add_byte (&buf, 0); /* flags */
1278 buffer_add_byte (&buf, command_set);
1279 buffer_add_byte (&buf, command);
1280 memcpy (buf.buf + 11, data->buf, data->p - data->buf);
1282 res = transport_send (buf.buf, len);
1290 send_reply_packet (int id, int error, Buffer *data)
1296 len = data->p - data->buf + 11;
1297 buffer_init (&buf, len);
1298 buffer_add_int (&buf, len);
1299 buffer_add_int (&buf, id);
1300 buffer_add_byte (&buf, 0x80); /* flags */
1301 buffer_add_byte (&buf, (error >> 8) & 0xff);
1302 buffer_add_byte (&buf, error);
1303 memcpy (buf.buf + 11, data->buf, data->p - data->buf);
1305 res = transport_send (buf.buf, len);
1317 * Represents an object accessible by the debugger client.
1320 /* Unique id used in the wire protocol to refer to objects */
1323 * A weakref gc handle pointing to the object. The gc handle is used to
1324 * detect if the object was garbage collected.
1329 /* Maps objid -> ObjRef */
1330 static GHashTable *objrefs;
1333 free_objref (gpointer value)
1337 mono_gchandle_free (o->handle);
1345 objrefs = g_hash_table_new_full (NULL, NULL, NULL, free_objref);
1349 objrefs_cleanup (void)
1351 g_hash_table_destroy (objrefs);
1355 static GHashTable *obj_to_objref;
1358 * Return an ObjRef for OBJ.
1361 get_objref (MonoObject *obj)
1372 /* Use a hash table with masked pointers to internalize object references */
1373 /* FIXME: This can grow indefinitely */
1374 mono_loader_lock ();
1377 obj_to_objref = g_hash_table_new (NULL, NULL);
1379 ref = g_hash_table_lookup (obj_to_objref, GINT_TO_POINTER (~((gsize)obj)));
1380 /* ref might refer to a different object with the same addr which was GCd */
1381 if (ref && mono_gchandle_get_target (ref->handle) == obj) {
1382 mono_loader_unlock ();
1386 ref = g_new0 (ObjRef, 1);
1387 ref->id = InterlockedIncrement (&objref_id);
1388 ref->handle = mono_gchandle_new_weakref (obj, FALSE);
1390 g_hash_table_insert (objrefs, GINT_TO_POINTER (ref->id), ref);
1391 g_hash_table_insert (obj_to_objref, GINT_TO_POINTER (~((gsize)obj)), ref);
1393 mono_loader_unlock ();
1399 get_objid (MonoObject *obj)
1401 return get_objref (obj)->id;
1405 * Set OBJ to the object identified by OBJID.
1406 * Returns 0 or an error code if OBJID is invalid or the object has been garbage
1410 get_object_allow_null (int objid, MonoObject **obj)
1420 return ERR_INVALID_OBJECT;
1422 mono_loader_lock ();
1424 ref = g_hash_table_lookup (objrefs, GINT_TO_POINTER (objid));
1427 *obj = mono_gchandle_get_target (ref->handle);
1428 mono_loader_unlock ();
1430 return ERR_INVALID_OBJECT;
1433 mono_loader_unlock ();
1434 return ERR_INVALID_OBJECT;
1439 get_object (int objid, MonoObject **obj)
1441 int err = get_object_allow_null (objid, obj);
1446 return ERR_INVALID_OBJECT;
1451 decode_objid (guint8 *buf, guint8 **endbuf, guint8 *limit)
1453 return decode_id (buf, endbuf, limit);
1457 buffer_add_objid (Buffer *buf, MonoObject *o)
1459 buffer_add_id (buf, get_objid (o));
1478 * Represents a runtime structure accessible to the debugger client
1481 /* Unique id used in the wire protocol */
1483 /* Domain of the runtime structure, NULL if the domain was unloaded */
1490 MonoAssembly *assembly;
1491 MonoClassField *field;
1493 MonoProperty *property;
1498 /* Maps runtime structure -> Id */
1499 GHashTable *val_to_id [ID_NUM];
1503 static GPtrArray *ids [ID_NUM];
1510 for (i = 0; i < ID_NUM; ++i)
1511 ids [i] = g_ptr_array_new ();
1519 for (i = 0; i < ID_NUM; ++i) {
1521 for (j = 0; j < ids [i]->len; ++j)
1522 g_free (g_ptr_array_index (ids [i], j));
1523 g_ptr_array_free (ids [i], TRUE);
1530 mono_debugger_agent_free_domain_info (MonoDomain *domain)
1532 AgentDomainInfo *info = domain_jit_info (domain)->agent_info;
1536 for (i = 0; i < ID_NUM; ++i)
1537 if (info->val_to_id [i])
1538 g_hash_table_destroy (info->val_to_id [i]);
1542 domain_jit_info (domain)->agent_info = NULL;
1544 /* Clear ids referencing structures in the domain */
1545 for (i = 0; i < ID_NUM; ++i) {
1547 for (j = 0; j < ids [i]->len; ++j) {
1548 Id *id = g_ptr_array_index (ids [i], j);
1549 if (id->domain == domain)
1557 get_id (MonoDomain *domain, IdType type, gpointer val)
1560 AgentDomainInfo *info;
1565 mono_loader_lock ();
1567 mono_domain_lock (domain);
1569 if (!domain_jit_info (domain)->agent_info)
1570 domain_jit_info (domain)->agent_info = g_new0 (AgentDomainInfo, 1);
1571 info = domain_jit_info (domain)->agent_info;
1572 if (info->val_to_id [type] == NULL)
1573 info->val_to_id [type] = g_hash_table_new (mono_aligned_addr_hash, NULL);
1575 id = g_hash_table_lookup (info->val_to_id [type], val);
1577 mono_domain_unlock (domain);
1578 mono_loader_unlock ();
1582 id = g_new0 (Id, 1);
1584 id->id = ids [type]->len + 1;
1585 id->domain = domain;
1588 g_hash_table_insert (info->val_to_id [type], val, id);
1590 mono_domain_unlock (domain);
1592 g_ptr_array_add (ids [type], id);
1594 mono_loader_unlock ();
1599 static inline gpointer
1600 decode_ptr_id (guint8 *buf, guint8 **endbuf, guint8 *limit, IdType type, MonoDomain **domain, int *err)
1604 int id = decode_id (buf, endbuf, limit);
1613 // FIXME: error handling
1614 mono_loader_lock ();
1615 g_assert (id > 0 && id <= ids [type]->len);
1617 res = g_ptr_array_index (ids [type], GPOINTER_TO_INT (id - 1));
1618 mono_loader_unlock ();
1620 if (res->domain == NULL) {
1621 *err = ERR_UNLOADED;
1626 *domain = res->domain;
1628 return res->data.val;
1632 buffer_add_ptr_id (Buffer *buf, MonoDomain *domain, IdType type, gpointer val)
1634 buffer_add_id (buf, get_id (domain, type, val));
1637 static inline MonoClass*
1638 decode_typeid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1640 return decode_ptr_id (buf, endbuf, limit, ID_TYPE, domain, err);
1643 static inline MonoAssembly*
1644 decode_assemblyid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1646 return decode_ptr_id (buf, endbuf, limit, ID_ASSEMBLY, domain, err);
1649 static inline MonoImage*
1650 decode_moduleid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1652 return decode_ptr_id (buf, endbuf, limit, ID_MODULE, domain, err);
1655 static inline MonoMethod*
1656 decode_methodid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1658 return decode_ptr_id (buf, endbuf, limit, ID_METHOD, domain, err);
1661 static inline MonoClassField*
1662 decode_fieldid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1664 return decode_ptr_id (buf, endbuf, limit, ID_FIELD, domain, err);
1667 static inline MonoDomain*
1668 decode_domainid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1670 return decode_ptr_id (buf, endbuf, limit, ID_DOMAIN, domain, err);
1673 static inline MonoProperty*
1674 decode_propertyid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1676 return decode_ptr_id (buf, endbuf, limit, ID_PROPERTY, domain, err);
1680 buffer_add_typeid (Buffer *buf, MonoDomain *domain, MonoClass *klass)
1682 buffer_add_ptr_id (buf, domain, ID_TYPE, klass);
1686 buffer_add_methodid (Buffer *buf, MonoDomain *domain, MonoMethod *method)
1688 buffer_add_ptr_id (buf, domain, ID_METHOD, method);
1692 buffer_add_assemblyid (Buffer *buf, MonoDomain *domain, MonoAssembly *assembly)
1694 buffer_add_ptr_id (buf, domain, ID_ASSEMBLY, assembly);
1698 buffer_add_moduleid (Buffer *buf, MonoDomain *domain, MonoImage *image)
1700 buffer_add_ptr_id (buf, domain, ID_MODULE, image);
1704 buffer_add_fieldid (Buffer *buf, MonoDomain *domain, MonoClassField *field)
1706 buffer_add_ptr_id (buf, domain, ID_FIELD, field);
1710 buffer_add_propertyid (Buffer *buf, MonoDomain *domain, MonoProperty *property)
1712 buffer_add_ptr_id (buf, domain, ID_PROPERTY, property);
1716 buffer_add_domainid (Buffer *buf, MonoDomain *domain)
1718 buffer_add_ptr_id (buf, domain, ID_DOMAIN, domain);
1721 static void invoke_method (void);
1728 * save_thread_context:
1730 * Set CTX as the current threads context which is used for computing stack traces.
1731 * This function is signal-safe.
1734 save_thread_context (MonoContext *ctx)
1736 DebuggerTlsData *tls;
1738 tls = TlsGetValue (debugger_tls_id);
1742 memcpy (&tls->ctx, ctx, sizeof (MonoContext));
1744 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
1745 MONO_INIT_CONTEXT_FROM_CURRENT (&tls->ctx);
1747 MONO_INIT_CONTEXT_FROM_FUNC (&tls->ctx, save_thread_context);
1751 tls->lmf = mono_get_lmf ();
1752 tls->domain = mono_domain_get ();
1753 tls->has_context = TRUE;
1756 /* The number of times the runtime is suspended */
1757 static gint32 suspend_count;
1759 /* Number of threads suspended */
1761 * If this is equal to the size of thread_to_tls, the runtime is considered
1764 static gint32 threads_suspend_count;
1766 static mono_mutex_t suspend_mutex;
1768 /* Cond variable used to wait for suspend_count becoming 0 */
1769 static mono_cond_t suspend_cond;
1771 /* Semaphore used to wait for a thread becoming suspended */
1772 static MonoSemType suspend_sem;
1777 mono_mutex_init (&suspend_mutex, NULL);
1778 mono_cond_init (&suspend_cond, NULL);
1779 MONO_SEM_INIT (&suspend_sem, 0);
1784 StackFrameInfo last_frame;
1785 gboolean last_frame_set;
1788 } GetLastFrameUserData;
1791 get_last_frame (StackFrameInfo *info, MonoContext *ctx, gpointer user_data)
1793 GetLastFrameUserData *data = user_data;
1795 if (info->type == FRAME_TYPE_MANAGED_TO_NATIVE)
1798 if (!data->last_frame_set) {
1799 /* Store the last frame */
1800 memcpy (&data->last_frame, info, sizeof (StackFrameInfo));
1801 data->last_frame_set = TRUE;
1804 /* Store the context/lmf for the frame above the last frame */
1805 memcpy (&data->ctx, ctx, sizeof (MonoContext));
1806 data->lmf = info->lmf;
1813 * mono_debugger_agent_thread_interrupt:
1815 * Called by the abort signal handler.
1816 * Should be signal safe.
1819 mono_debugger_agent_thread_interrupt (void *sigctx, MonoJitInfo *ji)
1821 DebuggerTlsData *tls;
1826 tls = TlsGetValue (debugger_tls_id);
1831 * OSX can (and will) coalesce signals, so sending multiple pthread_kills does not
1832 * guarantee the signal handler will be called that many times. Instead of tracking
1833 * interrupt_count on osx, we use this as a boolean flag to determine if a interrupt
1834 * has been requested that hasn't been handled yet, otherwise we can have threads
1835 * refuse to die when VM_EXIT is called
1837 #if defined(__APPLE__)
1838 if (InterlockedCompareExchange (&tls->interrupt_count, 0, 1) == 0)
1842 * We use interrupt_count to determine whenever this interrupt should be processed
1843 * by us or the normal interrupt processing code in the signal handler.
1844 * There is no race here with notify_thread (), since the signal is sent after
1845 * incrementing interrupt_count.
1847 if (tls->interrupt_count == 0)
1850 InterlockedDecrement (&tls->interrupt_count);
1853 // FIXME: Races when the thread leaves managed code before hitting a single step
1857 /* Running managed code, will be suspended by the single step code */
1858 DEBUG (1, printf ("[%p] Received interrupt while at %s(%p), continuing.\n", (gpointer)GetCurrentThreadId (), ji->method->name, mono_arch_ip_from_context (sigctx)));
1862 * Running native code, will be suspended when it returns to/enters
1863 * managed code. Treat it as already suspended.
1864 * This might interrupt the code in process_single_step_inner (), we use the
1865 * tls->suspending flag to avoid races when that happens.
1867 if (!tls->suspended && !tls->suspending) {
1869 GetLastFrameUserData data;
1871 // FIXME: printf is not signal safe, but this is only used during
1872 // debugger debugging
1873 DEBUG (1, printf ("[%p] Received interrupt while at %p, treating as suspended.\n", (gpointer)GetCurrentThreadId (), mono_arch_ip_from_context (sigctx)));
1874 //save_thread_context (&ctx);
1877 /* Already terminated */
1881 * We are in a difficult position: we want to be able to provide stack
1882 * traces for this thread, but we can't use the current ctx+lmf, since
1883 * the thread is still running, so it might return to managed code,
1884 * making these invalid.
1885 * So we start a stack walk and save the first frame, along with the
1886 * parent frame's ctx+lmf. This (hopefully) works because the thread will be
1887 * suspended when it returns to managed code, so the parent's ctx should
1890 data.last_frame_set = FALSE;
1892 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
1893 mono_jit_walk_stack_from_ctx_in_thread (get_last_frame, mono_domain_get (), &ctx, FALSE, tls->thread, mono_get_lmf (), &data);
1895 if (data.last_frame_set) {
1896 memcpy (&tls->async_last_frame, &data.last_frame, sizeof (StackFrameInfo));
1897 memcpy (&tls->async_ctx, &data.ctx, sizeof (MonoContext));
1898 tls->async_lmf = data.lmf;
1899 tls->has_async_ctx = TRUE;
1900 tls->domain = mono_domain_get ();
1901 memcpy (&tls->ctx, &ctx, sizeof (MonoContext));
1903 tls->has_async_ctx = FALSE;
1906 mono_memory_barrier ();
1908 tls->suspended = TRUE;
1909 MONO_SEM_POST (&suspend_sem);
1916 static void CALLBACK notify_thread_apc (ULONG_PTR param)
1919 mono_debugger_agent_thread_interrupt (NULL, NULL);
1921 #endif /* HOST_WIN32 */
1924 * reset_native_thread_suspend_state:
1926 * Reset the suspended flag on native threads
1929 reset_native_thread_suspend_state (gpointer key, gpointer value, gpointer user_data)
1931 DebuggerTlsData *tls = value;
1933 if (!tls->really_suspended && tls->suspended)
1934 tls->suspended = FALSE;
1940 * Notify a thread that it needs to suspend.
1943 notify_thread (gpointer key, gpointer value, gpointer user_data)
1945 MonoInternalThread *thread = key;
1946 DebuggerTlsData *tls = value;
1947 gsize tid = thread->tid;
1949 if (GetCurrentThreadId () == tid || tls->terminated)
1952 DEBUG(1, fprintf (log_file, "[%p] Interrupting %p...\n", (gpointer)GetCurrentThreadId (), (gpointer)tid));
1955 * OSX can (and will) coalesce signals, so sending multiple pthread_kills does not
1956 * guarantee the signal handler will be called that many times. Instead of tracking
1957 * interrupt_count on osx, we use this as a boolean flag to determine if a interrupt
1958 * has been requested that hasn't been handled yet, otherwise we can have threads
1959 * refuse to die when VM_EXIT is called
1961 #if defined(__APPLE__)
1962 if (InterlockedCompareExchange (&tls->interrupt_count, 1, 0) == 1)
1966 * Maybe we could use the normal interrupt infrastructure, but that does a lot
1967 * of things like breaking waits etc. which we don't want.
1969 InterlockedIncrement (&tls->interrupt_count);
1972 /* This is _not_ equivalent to ves_icall_System_Threading_Thread_Abort () */
1974 QueueUserAPC (notify_thread_apc, thread->handle, NULL);
1976 pthread_kill ((pthread_t) tid, mono_thread_get_abort_signal ());
1981 process_suspend (DebuggerTlsData *tls, MonoContext *ctx)
1983 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
1986 if (debugger_thread_id == GetCurrentThreadId ())
1989 /* Prevent races with mono_debugger_agent_thread_interrupt () */
1990 if (suspend_count - tls->resume_count > 0)
1991 tls->suspending = TRUE;
1993 DEBUG(1, fprintf (log_file, "[%p] Received single step event for suspending.\n", (gpointer)GetCurrentThreadId ()));
1995 if (suspend_count - tls->resume_count == 0) {
1997 * We are executing a single threaded invoke but the single step for
1998 * suspending is still active.
1999 * FIXME: This slows down single threaded invokes.
2001 DEBUG(1, fprintf (log_file, "[%p] Ignored during single threaded invoke.\n", (gpointer)GetCurrentThreadId ()));
2005 ji = mini_jit_info_table_find (mono_domain_get (), (char*)ip, NULL);
2007 /* Can't suspend in these methods */
2008 if (ji->method->klass == mono_defaults.string_class && (!strcmp (ji->method->name, "memset") || strstr (ji->method->name, "memcpy")))
2011 save_thread_context (ctx);
2019 * Increase the suspend count of the VM. While the suspend count is greater
2020 * than 0, runtime threads are suspended at certain points during execution.
2025 mono_loader_lock ();
2027 mono_mutex_lock (&suspend_mutex);
2031 DEBUG(1, fprintf (log_file, "[%p] Suspending vm...\n", (gpointer)GetCurrentThreadId ()));
2033 if (suspend_count == 1) {
2034 // FIXME: Is it safe to call this inside the lock ?
2035 start_single_stepping ();
2036 mono_g_hash_table_foreach (thread_to_tls, notify_thread, NULL);
2039 mono_mutex_unlock (&suspend_mutex);
2041 mono_loader_unlock ();
2047 * Decrease the suspend count of the VM. If the count reaches 0, runtime threads
2055 g_assert (debugger_thread_id == GetCurrentThreadId ());
2057 mono_loader_lock ();
2059 mono_mutex_lock (&suspend_mutex);
2061 g_assert (suspend_count > 0);
2064 DEBUG(1, fprintf (log_file, "[%p] Resuming vm...\n", (gpointer)GetCurrentThreadId ()));
2066 if (suspend_count == 0) {
2067 // FIXME: Is it safe to call this inside the lock ?
2068 stop_single_stepping ();
2069 mono_g_hash_table_foreach (thread_to_tls, reset_native_thread_suspend_state, NULL);
2072 /* Signal this even when suspend_count > 0, since some threads might have resume_count > 0 */
2073 err = mono_cond_broadcast (&suspend_cond);
2074 g_assert (err == 0);
2076 mono_mutex_unlock (&suspend_mutex);
2077 //g_assert (err == 0);
2079 mono_loader_unlock ();
2085 * Resume just one thread.
2088 resume_thread (MonoInternalThread *thread)
2091 DebuggerTlsData *tls;
2093 g_assert (debugger_thread_id == GetCurrentThreadId ());
2095 mono_loader_lock ();
2097 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
2100 mono_mutex_lock (&suspend_mutex);
2102 g_assert (suspend_count > 0);
2104 DEBUG(1, fprintf (log_file, "[%p] Resuming thread...\n", (gpointer)(gssize)thread->tid));
2106 tls->resume_count += suspend_count;
2109 * Signal suspend_count without decreasing suspend_count, the threads will wake up
2110 * but only the one whose resume_count field is > 0 will be resumed.
2112 err = mono_cond_broadcast (&suspend_cond);
2113 g_assert (err == 0);
2115 mono_mutex_unlock (&suspend_mutex);
2116 //g_assert (err == 0);
2118 mono_loader_unlock ();
2122 invalidate_frames (DebuggerTlsData *tls)
2127 tls = TlsGetValue (debugger_tls_id);
2130 for (i = 0; i < tls->frame_count; ++i) {
2131 if (tls->frames [i]->jit)
2132 mono_debug_free_method_jit_info (tls->frames [i]->jit);
2133 g_free (tls->frames [i]);
2135 g_free (tls->frames);
2136 tls->frame_count = 0;
2143 * Suspend the current thread until the runtime is resumed. If the thread has a
2144 * pending invoke, then the invoke is executed before this function returns.
2147 suspend_current (void)
2150 DebuggerTlsData *tls;
2152 g_assert (debugger_thread_id != GetCurrentThreadId ());
2154 if (mono_loader_lock_is_owned_by_self ()) {
2156 * If we own the loader mutex, can't suspend until we release it, since the
2157 * whole runtime can deadlock otherwise.
2162 tls = TlsGetValue (debugger_tls_id);
2165 mono_mutex_lock (&suspend_mutex);
2167 tls->suspending = FALSE;
2168 tls->really_suspended = TRUE;
2170 if (!tls->suspended) {
2171 tls->suspended = TRUE;
2172 MONO_SEM_POST (&suspend_sem);
2175 DEBUG(1, fprintf (log_file, "[%p] Suspended.\n", (gpointer)GetCurrentThreadId ()));
2177 while (suspend_count - tls->resume_count > 0) {
2179 if (WAIT_TIMEOUT == WaitForSingleObject(suspend_cond, 0))
2181 mono_mutex_unlock (&suspend_mutex);
2183 mono_mutex_lock (&suspend_mutex);
2189 err = mono_cond_wait (&suspend_cond, &suspend_mutex);
2190 g_assert (err == 0);
2194 tls->suspended = FALSE;
2195 tls->really_suspended = FALSE;
2197 threads_suspend_count --;
2199 mono_mutex_unlock (&suspend_mutex);
2201 DEBUG(1, fprintf (log_file, "[%p] Resumed.\n", (gpointer)GetCurrentThreadId ()));
2204 /* Save the original context */
2205 tls->invoke->has_ctx = TRUE;
2206 memcpy (&tls->invoke->ctx, &tls->ctx, sizeof (MonoContext));
2211 /* The frame info becomes invalid after a resume */
2212 tls->has_context = FALSE;
2213 tls->has_async_ctx = FALSE;
2214 invalidate_frames (NULL);
2218 count_thread (gpointer key, gpointer value, gpointer user_data)
2220 DebuggerTlsData *tls = value;
2222 if (!tls->suspended && !tls->terminated)
2223 *(int*)user_data = *(int*)user_data + 1;
2227 count_threads_to_wait_for (void)
2231 mono_loader_lock ();
2232 mono_g_hash_table_foreach (thread_to_tls, count_thread, &count);
2233 mono_loader_unlock ();
2241 * Wait until the runtime is completely suspended.
2244 wait_for_suspend (void)
2246 int nthreads, nwait, err;
2247 gboolean waited = FALSE;
2249 // FIXME: Threads starting/stopping ?
2250 mono_loader_lock ();
2251 nthreads = mono_g_hash_table_size (thread_to_tls);
2252 mono_loader_unlock ();
2255 nwait = count_threads_to_wait_for ();
2257 DEBUG(1, fprintf (log_file, "Waiting for %d(%d) threads to suspend...\n", nwait, nthreads));
2258 err = MONO_SEM_WAIT (&suspend_sem);
2259 g_assert (err == 0);
2267 DEBUG(1, fprintf (log_file, "%d threads suspended.\n", nthreads));
2273 * Return whenever the runtime is suspended.
2278 return count_threads_to_wait_for () == 0;
2282 * find_seq_point_for_native_offset:
2284 * Find the sequence point corresponding to the native offset NATIVE_OFFSET, which
2285 * should be the location of a sequence point.
2288 find_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset, MonoSeqPointInfo **info)
2290 MonoSeqPointInfo *seq_points;
2293 mono_domain_lock (domain);
2294 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
2295 mono_domain_unlock (domain);
2296 g_assert (seq_points);
2300 for (i = 0; i < seq_points->len; ++i) {
2301 if (seq_points->seq_points [i].native_offset == native_offset)
2302 return &seq_points->seq_points [i];
2311 * Find the sequence point corresponding to the IL offset IL_OFFSET, which
2312 * should be the location of a sequence point.
2315 find_seq_point (MonoDomain *domain, MonoMethod *method, gint32 il_offset, MonoSeqPointInfo **info)
2317 MonoSeqPointInfo *seq_points;
2320 mono_domain_lock (domain);
2321 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
2322 mono_domain_unlock (domain);
2323 g_assert (seq_points);
2327 for (i = 0; i < seq_points->len; ++i) {
2328 if (seq_points->seq_points [i].il_offset == il_offset)
2329 return &seq_points->seq_points [i];
2336 * compute_il_offset:
2338 * Compute the IL offset corresponding to NATIVE_OFFSET, which should be
2339 * a location of a sequence point.
2340 * We use this function instead of mono_debug_il_offset_from_address () etc,
2341 * which doesn't seem to work in a lot of cases.
2344 compute_il_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset)
2346 MonoSeqPointInfo *seq_points;
2347 int i, last_il_offset, seq_il_offset, seq_native_offset;
2349 mono_domain_lock (domain);
2350 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
2351 mono_domain_unlock (domain);
2352 g_assert (seq_points);
2354 last_il_offset = -1;
2356 /* Find the sequence point */
2357 for (i = 0; i < seq_points->len; ++i) {
2358 seq_il_offset = seq_points->seq_points [i].il_offset;
2359 seq_native_offset = seq_points->seq_points [i].native_offset;
2361 if (seq_native_offset > native_offset)
2363 last_il_offset = seq_il_offset;
2366 return last_il_offset;
2370 DebuggerTlsData *tls;
2372 } ComputeFramesUserData;
2375 process_frame (StackFrameInfo *info, MonoContext *ctx, gpointer user_data)
2377 ComputeFramesUserData *ud = user_data;
2381 if (info->type != FRAME_TYPE_MANAGED) {
2382 if (info->type == FRAME_TYPE_DEBUGGER_INVOKE) {
2383 /* Mark the last frame as an invoke frame */
2385 ((StackFrame*)ud->frames->data)->flags |= FRAME_FLAG_DEBUGGER_INVOKE;
2391 method = info->ji->method;
2393 method = info->method;
2395 if (!method || (method->wrapper_type && method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD))
2398 if (info->il_offset == -1) {
2399 /* Can't use compute_il_offset () since ip doesn't point precisely at at a seq point */
2400 info->il_offset = mono_debug_il_offset_from_address (method, info->domain, info->native_offset);
2403 DEBUG (1, fprintf (stderr, "\tFrame: %s %d %d %d\n", mono_method_full_name (method, TRUE), info->native_offset, info->il_offset, info->managed));
2405 if (!info->managed && method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD) {
2407 * mono_arch_find_jit_info () returns the context stored in the LMF for
2408 * native frames, but it should unwind once. This is why we have duplicate
2409 * frames on the stack sometimes.
2410 * !managed also seems to be set for dynamic methods.
2415 frame = g_new0 (StackFrame, 1);
2416 frame->method = method;
2417 frame->il_offset = info->il_offset;
2420 frame->has_ctx = TRUE;
2422 frame->domain = info->domain;
2424 ud->frames = g_slist_append (ud->frames, frame);
2430 compute_frame_info (MonoInternalThread *thread, DebuggerTlsData *tls)
2432 ComputeFramesUserData user_data;
2434 int i, findex, new_frame_count;
2435 StackFrame **new_frames, *f;
2437 // FIXME: Locking on tls
2438 if (tls->frames && tls->frames_up_to_date)
2441 DEBUG(1, fprintf (log_file, "Frames for %p(tid=%lx):\n", thread, (glong)thread->tid));
2443 user_data.tls = tls;
2444 user_data.frames = NULL;
2445 if (tls->terminated) {
2446 tls->frame_count = 0;
2448 } if (!tls->really_suspended && tls->has_async_ctx) {
2449 /* Have to use the state saved by the signal handler */
2450 process_frame (&tls->async_last_frame, NULL, &user_data);
2451 mono_jit_walk_stack_from_ctx_in_thread (process_frame, tls->domain, &tls->async_ctx, FALSE, thread, tls->async_lmf, &user_data);
2452 } else if (tls->has_context) {
2453 mono_jit_walk_stack_from_ctx_in_thread (process_frame, tls->domain, &tls->ctx, FALSE, thread, tls->lmf, &user_data);
2456 tls->frame_count = 0;
2460 new_frame_count = g_slist_length (user_data.frames);
2461 new_frames = g_new0 (StackFrame*, new_frame_count);
2463 for (tmp = user_data.frames; tmp; tmp = tmp->next) {
2467 * Reuse the id for already existing stack frames, so invokes don't invalidate
2468 * the still valid stack frames.
2470 for (i = 0; i < tls->frame_count; ++i) {
2471 if (MONO_CONTEXT_GET_SP (&tls->frames [i]->ctx) == MONO_CONTEXT_GET_SP (&f->ctx)) {
2472 f->id = tls->frames [i]->id;
2477 if (i >= tls->frame_count)
2478 f->id = InterlockedIncrement (&frame_id);
2480 new_frames [findex ++] = f;
2483 g_slist_free (user_data.frames);
2485 invalidate_frames (tls);
2487 tls->frames = new_frames;
2488 tls->frame_count = new_frame_count;
2489 tls->frames_up_to_date = TRUE;
2497 * create_event_list:
2499 * Return a list of event request ids matching EVENT, starting from REQS, which
2500 * can be NULL to include all event requests. Set SUSPEND_POLICY to the suspend
2502 * We return request ids, instead of requests, to simplify threading, since
2503 * requests could be deleted anytime when the loader lock is not held.
2504 * LOCKING: Assumes the loader lock is held.
2507 create_event_list (EventKind event, GPtrArray *reqs, MonoJitInfo *ji, EventInfo *ei, int *suspend_policy)
2510 GSList *events = NULL;
2512 *suspend_policy = SUSPEND_POLICY_NONE;
2515 reqs = event_requests;
2520 for (i = 0; i < reqs->len; ++i) {
2521 EventRequest *req = g_ptr_array_index (reqs, i);
2522 if (req->event_kind == event) {
2523 gboolean filtered = FALSE;
2526 for (j = 0; j < req->nmodifiers; ++j) {
2527 Modifier *mod = &req->modifiers [j];
2529 if (mod->kind == MOD_KIND_COUNT) {
2531 if (mod->data.count > 0) {
2532 if (mod->data.count > 0) {
2534 if (mod->data.count == 0)
2538 } else if (mod->kind == MOD_KIND_THREAD_ONLY) {
2539 if (mod->data.thread != mono_thread_internal_current ())
2541 } else if (mod->kind == MOD_KIND_EXCEPTION_ONLY && ei) {
2542 if (mod->data.exc_class && !mono_class_is_assignable_from (mod->data.exc_class, ei->exc->vtable->klass))
2544 if (ei->caught && !mod->caught)
2546 if (!ei->caught && !mod->uncaught)
2548 } else if (mod->kind == MOD_KIND_ASSEMBLY_ONLY && ji) {
2550 gboolean found = FALSE;
2551 MonoAssembly **assemblies = mod->data.assemblies;
2554 for (k = 0; assemblies [k]; ++k)
2555 if (assemblies [k] == ji->method->klass->image->assembly)
2564 *suspend_policy = MAX (*suspend_policy, req->suspend_policy);
2565 events = g_slist_append (events, GINT_TO_POINTER (req->id));
2570 /* Send a VM START/DEATH event by default */
2571 if (event == EVENT_KIND_VM_START)
2572 events = g_slist_append (events, GINT_TO_POINTER (0));
2573 if (event == EVENT_KIND_VM_DEATH)
2574 events = g_slist_append (events, GINT_TO_POINTER (0));
2579 static G_GNUC_UNUSED const char*
2580 event_to_string (EventKind event)
2583 case EVENT_KIND_VM_START: return "VM_START";
2584 case EVENT_KIND_VM_DEATH: return "VM_DEATH";
2585 case EVENT_KIND_THREAD_START: return "THREAD_START";
2586 case EVENT_KIND_THREAD_DEATH: return "THREAD_DEATH";
2587 case EVENT_KIND_APPDOMAIN_CREATE: return "APPDOMAIN_CREATE";
2588 case EVENT_KIND_APPDOMAIN_UNLOAD: return "APPDOMAIN_UNLOAD";
2589 case EVENT_KIND_METHOD_ENTRY: return "METHOD_ENTRY";
2590 case EVENT_KIND_METHOD_EXIT: return "METHOD_EXIT";
2591 case EVENT_KIND_ASSEMBLY_LOAD: return "ASSEMBLY_LOAD";
2592 case EVENT_KIND_ASSEMBLY_UNLOAD: return "ASSEMBLY_UNLOAD";
2593 case EVENT_KIND_BREAKPOINT: return "BREAKPOINT";
2594 case EVENT_KIND_STEP: return "STEP";
2595 case EVENT_KIND_TYPE_LOAD: return "TYPE_LOAD";
2596 case EVENT_KIND_EXCEPTION: return "EXCEPTION";
2598 g_assert_not_reached ();
2605 * Send an event to the client, suspending the vm if needed.
2606 * LOCKING: Since this can suspend the calling thread, no locks should be held
2608 * The EVENTS list is freed by this function.
2611 process_event (EventKind event, gpointer arg, gint32 il_offset, MonoContext *ctx, GSList *events, int suspend_policy)
2615 MonoDomain *domain = mono_domain_get ();
2621 if (!vm_start_event_sent && event != EVENT_KIND_VM_START)
2622 // FIXME: We miss those events
2625 if (vm_death_event_sent)
2628 if (mono_runtime_is_shutting_down () && event != EVENT_KIND_VM_DEATH)
2637 if (debugger_thread_id == GetCurrentThreadId () && event != EVENT_KIND_VM_DEATH)
2638 // FIXME: Send these with a NULL thread, don't suspend the current thread
2641 buffer_init (&buf, 128);
2642 buffer_add_byte (&buf, suspend_policy);
2643 buffer_add_int (&buf, g_slist_length (events)); // n of events
2645 for (l = events; l; l = l->next) {
2646 buffer_add_byte (&buf, event); // event kind
2647 buffer_add_int (&buf, GPOINTER_TO_INT (l->data)); // request id
2649 thread = mono_thread_current ();
2651 if (event == EVENT_KIND_VM_START)
2653 else if (event == EVENT_KIND_THREAD_START)
2654 g_assert (mono_thread_internal_current () == arg);
2656 buffer_add_objid (&buf, (MonoObject*)thread); // thread
2659 case EVENT_KIND_THREAD_START:
2660 case EVENT_KIND_THREAD_DEATH:
2662 case EVENT_KIND_APPDOMAIN_CREATE:
2663 case EVENT_KIND_APPDOMAIN_UNLOAD:
2664 buffer_add_domainid (&buf, arg);
2666 case EVENT_KIND_METHOD_ENTRY:
2667 case EVENT_KIND_METHOD_EXIT:
2668 buffer_add_methodid (&buf, domain, arg);
2670 case EVENT_KIND_ASSEMBLY_LOAD:
2671 case EVENT_KIND_ASSEMBLY_UNLOAD:
2672 buffer_add_assemblyid (&buf, domain, arg);
2674 case EVENT_KIND_TYPE_LOAD:
2675 buffer_add_typeid (&buf, domain, arg);
2677 case EVENT_KIND_BREAKPOINT:
2678 case EVENT_KIND_STEP:
2679 buffer_add_methodid (&buf, domain, arg);
2680 buffer_add_long (&buf, il_offset);
2682 case EVENT_KIND_VM_START:
2683 buffer_add_domainid (&buf, mono_get_root_domain ());
2685 case EVENT_KIND_VM_DEATH:
2687 case EVENT_KIND_EXCEPTION: {
2688 EventInfo *ei = arg;
2689 buffer_add_objid (&buf, ei->exc);
2693 g_assert_not_reached ();
2697 if (event == EVENT_KIND_VM_START) {
2698 suspend_policy = agent_config.suspend ? SUSPEND_POLICY_ALL : SUSPEND_POLICY_NONE;
2699 start_debugger_thread ();
2702 if (event == EVENT_KIND_VM_DEATH) {
2703 vm_death_event_sent = TRUE;
2705 suspend_policy = SUSPEND_POLICY_NONE;
2708 if (mono_runtime_is_shutting_down ())
2709 suspend_policy = SUSPEND_POLICY_NONE;
2711 if (suspend_policy != SUSPEND_POLICY_NONE) {
2713 * Save the thread context and start suspending before sending the packet,
2714 * since we could be receiving the resume request before send_packet ()
2717 save_thread_context (ctx);
2721 send_packet (CMD_SET_EVENT, CMD_COMPOSITE, &buf);
2723 g_slist_free (events);
2726 if (event == EVENT_KIND_VM_START)
2727 vm_start_event_sent = TRUE;
2729 DEBUG (1, fprintf (log_file, "[%p] Sent event %s, suspend=%d.\n", (gpointer)GetCurrentThreadId (), event_to_string (event), suspend_policy));
2733 switch (suspend_policy) {
2734 case SUSPEND_POLICY_NONE:
2736 case SUSPEND_POLICY_ALL:
2739 case SUSPEND_POLICY_EVENT_THREAD:
2743 g_assert_not_reached ();
2748 process_profiler_event (EventKind event, gpointer arg)
2753 mono_loader_lock ();
2754 events = create_event_list (event, NULL, NULL, NULL, &suspend_policy);
2755 mono_loader_unlock ();
2757 process_event (event, arg, 0, NULL, events, suspend_policy);
2761 runtime_initialized (MonoProfiler *prof)
2763 process_profiler_event (EVENT_KIND_VM_START, mono_thread_current ());
2767 runtime_shutdown (MonoProfiler *prof)
2769 process_profiler_event (EVENT_KIND_VM_DEATH, mono_thread_current ());
2771 mono_debugger_agent_cleanup ();
2775 thread_startup (MonoProfiler *prof, intptr_t tid)
2777 MonoInternalThread *thread = mono_thread_internal_current ();
2778 MonoInternalThread *old_thread;
2779 DebuggerTlsData *tls;
2781 if (tid == debugger_thread_id)
2784 g_assert (thread->tid == tid);
2786 mono_loader_lock ();
2787 old_thread = mono_g_hash_table_lookup (tid_to_thread, (gpointer)tid);
2788 mono_loader_unlock ();
2790 if (thread == old_thread) {
2792 * For some reason, thread_startup () might be called for the same thread
2793 * multiple times (attach ?).
2795 DEBUG (1, fprintf (log_file, "[%p] thread_start () called multiple times for %p, ignored.\n", (gpointer)tid, (gpointer)tid));
2799 * thread_end () might not be called for some threads, and the tid could
2802 DEBUG (1, fprintf (log_file, "[%p] Removing stale data for tid %p.\n", (gpointer)tid, (gpointer)tid));
2803 mono_loader_lock ();
2804 mono_g_hash_table_remove (thread_to_tls, old_thread);
2805 mono_g_hash_table_remove (tid_to_thread, (gpointer)tid);
2806 mono_g_hash_table_remove (tid_to_thread_obj, (gpointer)tid);
2807 mono_loader_unlock ();
2811 tls = TlsGetValue (debugger_tls_id);
2813 // FIXME: Free this somewhere
2814 tls = g_new0 (DebuggerTlsData, 1);
2815 tls->resume_event = CreateEvent (NULL, FALSE, FALSE, NULL);
2816 MONO_GC_REGISTER_ROOT (tls->thread);
2817 tls->thread = thread;
2818 TlsSetValue (debugger_tls_id, tls);
2820 DEBUG (1, fprintf (log_file, "[%p] Thread started, obj=%p, tls=%p.\n", (gpointer)tid, thread, tls));
2822 mono_loader_lock ();
2823 mono_g_hash_table_insert (thread_to_tls, thread, tls);
2824 mono_g_hash_table_insert (tid_to_thread, (gpointer)tid, thread);
2825 mono_g_hash_table_insert (tid_to_thread_obj, (gpointer)tid, mono_thread_current ());
2826 mono_loader_unlock ();
2828 process_profiler_event (EVENT_KIND_THREAD_START, thread);
2831 * suspend_vm () could have missed this thread, so wait for a resume.
2837 thread_end (MonoProfiler *prof, intptr_t tid)
2839 MonoInternalThread *thread;
2840 DebuggerTlsData *tls = NULL;
2842 mono_loader_lock ();
2843 thread = mono_g_hash_table_lookup (tid_to_thread, (gpointer)tid);
2845 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
2846 /* FIXME: Maybe we need to free this instead, but some code can't handle that */
2847 tls->terminated = TRUE;
2848 mono_g_hash_table_remove (tid_to_thread_obj, (gpointer)tid);
2849 /* Can't remove from tid_to_thread, as that would defeat the check in thread_start () */
2850 MONO_GC_UNREGISTER_ROOT (tls->thread);
2853 mono_loader_unlock ();
2855 /* We might be called for threads started before we registered the start callback */
2857 DEBUG (1, fprintf (log_file, "[%p] Thread terminated, obj=%p, tls=%p.\n", (gpointer)tid, thread, tls));
2858 process_profiler_event (EVENT_KIND_THREAD_DEATH, thread);
2863 appdomain_load (MonoProfiler *prof, MonoDomain *domain, int result)
2865 process_profiler_event (EVENT_KIND_APPDOMAIN_CREATE, domain);
2869 appdomain_unload (MonoProfiler *prof, MonoDomain *domain)
2871 /* Invalidate each thread's frame stack */
2872 mono_g_hash_table_foreach (thread_to_tls, invalidate_each_thread, NULL);
2873 process_profiler_event (EVENT_KIND_APPDOMAIN_UNLOAD, domain);
2877 * invalidate_each_thread:
2879 * A GHFunc to invalidate frames.
2880 * value must be a DebuggerTlsData*
2883 invalidate_each_thread (gpointer key, gpointer value, gpointer user_data)
2885 invalidate_frames (value);
2889 assembly_load (MonoProfiler *prof, MonoAssembly *assembly, int result)
2891 /* Sent later in jit_end () */
2892 mono_loader_lock ();
2893 g_ptr_array_add (pending_assembly_loads, assembly);
2894 mono_loader_unlock ();
2898 assembly_unload (MonoProfiler *prof, MonoAssembly *assembly)
2900 process_profiler_event (EVENT_KIND_ASSEMBLY_UNLOAD, assembly);
2904 start_runtime_invoke (MonoProfiler *prof, MonoMethod *method)
2906 #if defined(HOST_WIN32) && !defined(__GNUC__)
2907 gpointer stackptr = ((guint64)_AddressOfReturnAddress () - sizeof (void*));
2909 gpointer stackptr = __builtin_frame_address (1);
2911 MonoInternalThread *thread = mono_thread_internal_current ();
2912 DebuggerTlsData *tls;
2914 mono_loader_lock ();
2916 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
2917 /* Could be the debugger thread with assembly/type load hooks */
2919 tls->invoke_addr = stackptr;
2921 mono_loader_unlock ();
2925 end_runtime_invoke (MonoProfiler *prof, MonoMethod *method)
2928 #if defined(HOST_WIN32) && !defined(__GNUC__)
2929 gpointer stackptr = ((guint64)_AddressOfReturnAddress () - sizeof (void*));
2931 gpointer stackptr = __builtin_frame_address (1);
2934 if (ss_req == NULL || stackptr != ss_invoke_addr || ss_req->thread != mono_thread_internal_current ())
2938 * We need to stop single stepping when exiting a runtime invoke, since if it is
2939 * a step out, it may return to native code, and thus never end.
2941 mono_loader_lock ();
2942 ss_invoke_addr = NULL;
2944 for (i = 0; i < event_requests->len; ++i) {
2945 EventRequest *req = g_ptr_array_index (event_requests, i);
2947 if (req->event_kind == EVENT_KIND_STEP) {
2948 ss_destroy (req->info);
2949 g_ptr_array_remove_index_fast (event_requests, i);
2954 mono_loader_unlock ();
2958 jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result)
2961 * We emit type load events when the first method of the type is JITted,
2962 * since the class load profiler callbacks might be called with the
2963 * loader lock held. They could also occur in the debugger thread.
2964 * Same for assembly load events.
2966 gboolean type_load = FALSE;
2969 MonoAssembly *assembly = NULL;
2971 // FIXME: Maybe store this in TLS so the thread of the event is correct ?
2972 mono_loader_lock ();
2973 if (pending_assembly_loads->len > 0) {
2974 assembly = g_ptr_array_index (pending_assembly_loads, 0);
2975 g_ptr_array_remove_index (pending_assembly_loads, 0);
2977 mono_loader_unlock ();
2980 process_profiler_event (EVENT_KIND_ASSEMBLY_LOAD, assembly);
2985 mono_loader_lock ();
2986 if (!g_hash_table_lookup (loaded_classes, method->klass)) {
2988 g_hash_table_insert (loaded_classes, method->klass, method->klass);
2990 mono_loader_unlock ();
2992 process_profiler_event (EVENT_KIND_TYPE_LOAD, method->klass);
2995 add_pending_breakpoints (method, jinfo);
2999 * BREAKPOINTS/SINGLE STEPPING
3003 * Contains information about an inserted breakpoint.
3006 long il_offset, native_offset;
3008 gboolean pending, entry;
3010 } BreakpointInstance;
3013 * Contains generic information about a breakpoint.
3017 * The method where the breakpoint is placed. Can be NULL in which case it
3018 * is inserted into every method. This is used to implement method entry/
3019 * exit events. Can be a generic method definition, in which case the
3020 * breakpoint is inserted into every instance.
3024 gboolean pending, entry;
3027 * A list of BreakpointInstance structures describing where the breakpoint
3028 * was inserted. There could be more than one because of
3029 * generics/appdomains/method entry/exit.
3031 GPtrArray *children;
3034 /* List of breakpoints */
3035 static GPtrArray *breakpoints;
3036 /* Maps breakpoint locations to the number of breakpoints at that location */
3037 static GHashTable *bp_locs;
3040 breakpoints_init (void)
3042 breakpoints = g_ptr_array_new ();
3043 bp_locs = g_hash_table_new (NULL, NULL);
3047 breakpoints_cleanup (void)
3051 mono_loader_lock ();
3053 for (i = 0; i < breakpoints->len; ++i)
3054 g_free (g_ptr_array_index (breakpoints, i));
3056 g_ptr_array_free (breakpoints, TRUE);
3057 g_hash_table_destroy (bp_locs);
3059 mono_loader_unlock ();
3063 * insert_breakpoint:
3065 * Insert the breakpoint described by BP into the method described by
3069 insert_breakpoint (MonoSeqPointInfo *seq_points, MonoJitInfo *ji, MonoBreakpoint *bp)
3072 gint32 il_offset, native_offset;
3073 BreakpointInstance *inst;
3076 for (i = 0; i < seq_points->len; ++i) {
3077 il_offset = seq_points->seq_points [i].il_offset;
3078 native_offset = seq_points->seq_points [i].native_offset;
3080 if (il_offset == bp->il_offset)
3084 if (i == seq_points->len)
3085 /* Have to handle this somehow */
3088 inst = g_new0 (BreakpointInstance, 1);
3089 inst->native_offset = native_offset;
3090 inst->ip = (guint8*)ji->code_start + native_offset;
3093 mono_loader_lock ();
3095 g_ptr_array_add (bp->children, inst);
3097 count = GPOINTER_TO_INT (g_hash_table_lookup (bp_locs, inst->ip));
3098 g_hash_table_insert (bp_locs, inst->ip, GINT_TO_POINTER (count + 1));
3099 mono_loader_unlock ();
3102 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3103 mono_arch_set_breakpoint (ji, inst->ip);
3111 remove_breakpoint (BreakpointInstance *inst)
3113 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3115 MonoJitInfo *ji = inst->ji;
3116 guint8 *ip = inst->ip;
3118 mono_loader_lock ();
3119 count = GPOINTER_TO_INT (g_hash_table_lookup (bp_locs, ip));
3120 g_hash_table_insert (bp_locs, ip, GINT_TO_POINTER (count - 1));
3121 mono_loader_unlock ();
3123 g_assert (count > 0);
3126 mono_arch_clear_breakpoint (ji, ip);
3134 * add_pending_breakpoints:
3136 * Insert pending breakpoints into the newly JITted method METHOD.
3139 add_pending_breakpoints (MonoMethod *method, MonoJitInfo *ji)
3142 MonoSeqPointInfo *seq_points;
3148 domain = mono_domain_get ();
3150 mono_loader_lock ();
3152 for (i = 0; i < breakpoints->len; ++i) {
3153 MonoBreakpoint *bp = g_ptr_array_index (breakpoints, i);
3155 if (bp->pending && (bp->method == method || !bp->method || (method->is_inflated && ((MonoMethodInflated*)method)->declaring == bp->method))) {
3156 mono_domain_lock (domain);
3157 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, ji->method);
3158 mono_domain_unlock (domain);
3160 /* Could be AOT code */
3162 g_assert (seq_points);
3164 insert_breakpoint (seq_points, ji, bp);
3168 mono_loader_unlock ();
3172 set_bp_in_method (MonoDomain *domain, MonoMethod *method, MonoSeqPointInfo *seq_points, MonoBreakpoint *bp)
3177 code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
3179 /* Might be AOTed code */
3180 code = mono_aot_get_method (domain, method);
3182 ji = mono_jit_info_table_find (domain, code);
3187 insert_breakpoint (seq_points, ji, bp);
3191 set_bp_in_method_cb (gpointer key, gpointer value, gpointer user_data)
3193 MonoMethod *method = key;
3194 MonoSeqPointInfo *seq_points = value;
3195 MonoBreakpoint *bp = user_data;
3196 MonoDomain *domain = mono_domain_get ();
3199 if (method->is_inflated && ((MonoMethodInflated*)method)->declaring == bp->method) {
3200 /* Generic instance */
3201 set_bp_in_method (domain, method, seq_points, bp);
3204 /* Method entry/exit */
3205 set_bp_in_method (domain, method, seq_points, bp);
3212 * Set a breakpoint at IL_OFFSET in METHOD.
3213 * METHOD can be NULL, in which case a breakpoint is placed in all methods.
3214 * METHOD can also be a generic method definition, in which case a breakpoint
3215 * is placed in all instances of the method.
3217 static MonoBreakpoint*
3218 set_breakpoint (MonoMethod *method, long il_offset, EventRequest *req)
3220 MonoSeqPointInfo *seq_points;
3225 // - suspend/resume the vm to prevent code patching problems
3227 // - multiple breakpoints on the same location
3228 // - dynamic methods
3231 bp = g_new0 (MonoBreakpoint, 1);
3232 bp->method = method;
3233 bp->il_offset = il_offset;
3235 bp->children = g_ptr_array_new ();
3237 DEBUG(1, fprintf (log_file, "[dbg] Setting %sbreakpoint at %s:0x%x.\n", (req->event_kind == EVENT_KIND_STEP) ? "single step " : "", method ? mono_method_full_name (method, TRUE) : "<all>", (int)il_offset));
3239 domain = mono_domain_get ();
3240 mono_domain_lock (domain);
3242 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
3244 set_bp_in_method (domain, method, seq_points, bp);
3246 if (method->is_generic)
3247 /* There might be already JITted instances */
3248 g_hash_table_foreach (domain_jit_info (domain)->seq_points, set_bp_in_method_cb, bp);
3250 /* Not yet JITted */
3254 g_hash_table_foreach (domain_jit_info (domain)->seq_points, set_bp_in_method_cb, bp);
3257 mono_domain_unlock (domain);
3259 mono_loader_lock ();
3260 g_ptr_array_add (breakpoints, bp);
3261 mono_loader_unlock ();
3267 clear_breakpoint (MonoBreakpoint *bp)
3271 // FIXME: locking, races
3272 for (i = 0; i < bp->children->len; ++i) {
3273 BreakpointInstance *inst = g_ptr_array_index (bp->children, i);
3275 remove_breakpoint (inst);
3280 mono_loader_lock ();
3281 g_ptr_array_remove (breakpoints, bp);
3282 mono_loader_unlock ();
3284 g_ptr_array_free (bp->children, TRUE);
3289 process_breakpoint_inner (DebuggerTlsData *tls, MonoContext *ctx)
3292 guint8 *orig_ip, *ip;
3293 int i, j, suspend_policy;
3294 guint32 native_offset;
3296 BreakpointInstance *inst;
3297 GPtrArray *bp_reqs, *ss_reqs_orig, *ss_reqs;
3298 GSList *bp_events = NULL, *ss_events = NULL, *enter_leave_events = NULL;
3299 EventKind kind = EVENT_KIND_BREAKPOINT;
3301 // FIXME: Speed this up
3303 orig_ip = ip = MONO_CONTEXT_GET_IP (ctx);
3304 ji = mini_jit_info_table_find (mono_domain_get (), (char*)ip, NULL);
3306 g_assert (ji->method);
3308 /* Compute the native offset of the breakpoint from the ip */
3309 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3310 ip = mono_arch_get_ip_for_breakpoint (ji, ctx);
3311 native_offset = ip - (guint8*)ji->code_start;
3317 * Skip the instruction causing the breakpoint signal.
3319 mono_arch_skip_breakpoint (ctx);
3321 if (ji->method->wrapper_type || tls->disable_breakpoints)
3324 bp_reqs = g_ptr_array_new ();
3325 ss_reqs = g_ptr_array_new ();
3326 ss_reqs_orig = g_ptr_array_new ();
3328 DEBUG(1, fprintf (log_file, "[%p] Breakpoint hit, method=%s, offset=0x%x.\n", (gpointer)GetCurrentThreadId (), ji->method->name, native_offset));
3330 mono_loader_lock ();
3333 for (i = 0; i < breakpoints->len; ++i) {
3334 bp = g_ptr_array_index (breakpoints, i);
3339 for (j = 0; j < bp->children->len; ++j) {
3340 inst = g_ptr_array_index (bp->children, j);
3341 if (inst->ji == ji && inst->native_offset == native_offset) {
3342 if (bp->req->event_kind == EVENT_KIND_STEP) {
3343 g_ptr_array_add (ss_reqs_orig, bp->req);
3345 g_ptr_array_add (bp_reqs, bp->req);
3350 if (bp_reqs->len == 0 && ss_reqs_orig->len == 0) {
3351 MonoSeqPointInfo *seq_points;
3352 int seq_il_offset, seq_native_offset;
3353 MonoDomain *domain = mono_domain_get ();
3355 /* Maybe a method entry/exit event */
3356 mono_domain_lock (domain);
3357 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, ji->method);
3358 mono_domain_unlock (domain);
3360 // FIXME: Generic sharing */
3361 mono_loader_unlock ();
3364 g_assert (seq_points);
3366 for (i = 0; i < seq_points->len; ++i) {
3367 seq_il_offset = seq_points->seq_points [i].il_offset;
3368 seq_native_offset = seq_points->seq_points [i].native_offset;
3370 if (native_offset == seq_native_offset) {
3371 if (seq_il_offset == METHOD_ENTRY_IL_OFFSET)
3372 kind = EVENT_KIND_METHOD_ENTRY;
3373 else if (seq_il_offset == METHOD_EXIT_IL_OFFSET)
3374 kind = EVENT_KIND_METHOD_EXIT;
3380 /* Process single step requests */
3381 for (i = 0; i < ss_reqs_orig->len; ++i) {
3382 EventRequest *req = g_ptr_array_index (ss_reqs_orig, i);
3383 SingleStepReq *ss_req = bp->req->info;
3384 gboolean hit = TRUE;
3385 MonoSeqPointInfo *info;
3388 sp = find_seq_point_for_native_offset (mono_domain_get (), ji->method, native_offset, &info);
3391 if (ss_req->size == STEP_SIZE_LINE) {
3392 /* Have to check whenever a different source line was reached */
3393 MonoDebugMethodInfo *minfo;
3394 MonoDebugSourceLocation *loc = NULL;
3396 minfo = mono_debug_lookup_method (ji->method);
3399 loc = mono_debug_symfile_lookup_location (minfo, sp->il_offset);
3401 if (!loc || (loc && ji->method == ss_req->last_method && loc->row == ss_req->last_line))
3402 /* Have to continue single stepping */
3406 ss_req->last_method = ji->method;
3407 ss_req->last_line = loc->row;
3408 mono_debug_free_source_location (loc);
3413 g_ptr_array_add (ss_reqs, req);
3415 /* Start single stepping again from the current sequence point */
3416 ss_start (ss_req, ji->method, sp, info, ctx, NULL);
3419 if (ss_reqs->len > 0)
3420 ss_events = create_event_list (EVENT_KIND_STEP, ss_reqs, ji, NULL, &suspend_policy);
3421 if (bp_reqs->len > 0)
3422 bp_events = create_event_list (EVENT_KIND_BREAKPOINT, bp_reqs, ji, NULL, &suspend_policy);
3423 if (kind != EVENT_KIND_BREAKPOINT)
3424 enter_leave_events = create_event_list (kind, NULL, ji, NULL, &suspend_policy);
3426 mono_loader_unlock ();
3428 g_ptr_array_free (bp_reqs, TRUE);
3429 g_ptr_array_free (ss_reqs, TRUE);
3432 * FIXME: The first event will suspend, so the second will only be sent after the
3436 process_event (EVENT_KIND_STEP, ji->method, 0, ctx, ss_events, suspend_policy);
3438 process_event (kind, ji->method, 0, ctx, bp_events, suspend_policy);
3439 if (enter_leave_events)
3440 process_event (kind, ji->method, 0, ctx, enter_leave_events, suspend_policy);
3444 process_breakpoint (void)
3446 DebuggerTlsData *tls;
3448 static void (*restore_context) (void *);
3450 if (!restore_context)
3451 restore_context = mono_get_restore_context ();
3453 tls = TlsGetValue (debugger_tls_id);
3454 memcpy (&ctx, &tls->handler_ctx, sizeof (MonoContext));
3456 process_breakpoint_inner (tls, &ctx);
3458 /* This is called when resuming from a signal handler, so it shouldn't return */
3459 restore_context (&ctx);
3460 g_assert_not_reached ();
3464 resume_from_signal_handler (void *sigctx, void *func)
3466 DebuggerTlsData *tls;
3469 /* Save the original context in TLS */
3470 // FIXME: This might not work on an altstack ?
3471 tls = TlsGetValue (debugger_tls_id);
3474 // FIXME: MonoContext usually doesn't include the fp registers, so these are
3475 // clobbered by a single step/breakpoint event. If this turns out to be a problem,
3476 // clob:c could be added to op_seq_point.
3478 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
3479 memcpy (&tls->handler_ctx, &ctx, sizeof (MonoContext));
3480 MONO_CONTEXT_SET_IP (&ctx, func);
3481 mono_arch_monoctx_to_sigctx (&ctx, sigctx);
3483 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3484 mono_ppc_set_func_into_sigctx (sigctx, func);
3489 mono_debugger_agent_breakpoint_hit (void *sigctx)
3492 * We are called from a signal handler, and running code there causes all kinds of
3493 * problems, like the original signal is disabled, libgc can't handle altstack, etc.
3494 * So set up the signal context to return to the real breakpoint handler function.
3497 resume_from_signal_handler (sigctx, process_breakpoint);
3501 process_single_step_inner (DebuggerTlsData *tls, MonoContext *ctx)
3506 int il_offset, suspend_policy;
3510 // FIXME: Speed this up
3512 ip = MONO_CONTEXT_GET_IP (ctx);
3514 /* Skip the instruction causing the single step */
3515 mono_arch_skip_single_step (ctx);
3517 if (suspend_count > 0) {
3518 process_suspend (tls, ctx);
3523 // FIXME: A suspend race
3526 if (mono_thread_internal_current () != ss_req->thread)
3529 if (log_level > 0) {
3530 const char *depth = NULL;
3532 ji = mini_jit_info_table_find (mono_domain_get (), (char*)ip, &domain);
3534 switch (ss_req->depth) {
3535 case STEP_DEPTH_OVER:
3538 case STEP_DEPTH_OUT:
3541 case STEP_DEPTH_INTO:
3545 g_assert_not_reached ();
3548 DEBUG (1, fprintf (log_file, "[%p] Single step event (depth=%s) at %s (%p), sp %p, last sp %p\n", (gpointer)GetCurrentThreadId (), ss_req->depth == STEP_DEPTH_OVER ? "over" : "out", mono_method_full_name (ji->method, TRUE), MONO_CONTEXT_GET_IP (ctx), MONO_CONTEXT_GET_SP (ctx), ss_req->last_sp));
3552 * We implement step over/out by single stepping until we reach the same
3553 * frame/parent frame.
3556 * - stack growing upward
3560 if (ss_req->depth != STEP_DEPTH_INTO) {
3561 if (ss_req->depth == STEP_DEPTH_OVER && MONO_CONTEXT_GET_SP (ctx) < ss_req->last_sp)
3563 if (ss_req->depth == STEP_DEPTH_OUT && MONO_CONTEXT_GET_SP (ctx) <= ss_req->last_sp)
3566 ss_req->last_sp = MONO_CONTEXT_GET_SP (ctx);
3569 ji = mini_jit_info_table_find (mono_domain_get (), (char*)ip, &domain);
3571 g_assert (ji->method);
3573 if (ji->method->wrapper_type && ji->method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
3578 * Stopping in memset makes half-initialized vtypes visible.
3579 * Stopping in memcpy makes half-copied vtypes visible.
3581 if (ji->method->klass == mono_defaults.string_class && (!strcmp (ji->method->name, "memset") || strstr (ji->method->name, "memcpy")))
3585 * The ip points to the instruction causing the single step event, convert it
3586 * to the offset stored in seq_points.
3588 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3589 ip = mono_arch_get_ip_for_single_step (ji, ctx);
3591 g_assert_not_reached ();
3595 * mono_debug_lookup_source_location () doesn't work for IL offset 0 for
3596 * example, so do things by hand.
3598 il_offset = compute_il_offset (domain, ji->method, (guint8*)ip - (guint8*)ji->code_start);
3600 if (il_offset == -1)
3603 if (ss_req->size == STEP_SIZE_LINE) {
3604 /* Step until a different source line is reached */
3605 MonoDebugMethodInfo *minfo;
3607 minfo = mono_debug_lookup_method (ji->method);
3610 MonoDebugSourceLocation *loc = mono_debug_symfile_lookup_location (minfo, il_offset);
3612 if (loc && ji->method == ss_req->last_method && loc->row == ss_req->last_line) {
3613 mono_debug_free_source_location (loc);
3618 * Step until we reach a location with line number info,
3619 * otherwise the client can't show a location.
3620 * This can happen for example with statics initialized inline
3621 * outside of a cctor.
3626 ss_req->last_method = ji->method;
3627 ss_req->last_line = loc->row;
3628 mono_debug_free_source_location (loc);
3633 // FIXME: Has to lock earlier
3635 reqs = g_ptr_array_new ();
3637 mono_loader_lock ();
3639 g_ptr_array_add (reqs, ss_req->req);
3641 events = create_event_list (EVENT_KIND_STEP, reqs, ji, NULL, &suspend_policy);
3643 g_ptr_array_free (reqs, TRUE);
3645 mono_loader_unlock ();
3647 process_event (EVENT_KIND_STEP, ji->method, il_offset, ctx, events, suspend_policy);
3651 process_single_step (void)
3653 DebuggerTlsData *tls;
3655 static void (*restore_context) (void *);
3657 if (!restore_context)
3658 restore_context = mono_get_restore_context ();
3660 tls = TlsGetValue (debugger_tls_id);
3661 memcpy (&ctx, &tls->handler_ctx, sizeof (MonoContext));
3663 process_single_step_inner (tls, &ctx);
3665 /* This is called when resuming from a signal handler, so it shouldn't return */
3666 restore_context (&ctx);
3667 g_assert_not_reached ();
3671 * mono_debugger_agent_single_step_event:
3673 * Called from a signal handler to handle a single step event.
3676 mono_debugger_agent_single_step_event (void *sigctx)
3678 /* Resume to process_single_step through the signal context */
3680 // FIXME: Since step out/over is implemented using step in, the step in case should
3681 // be as fast as possible. Move the relevant code from process_single_step_inner ()
3684 if (GetCurrentThreadId () == debugger_thread_id) {
3686 * This could happen despite our best effors when the runtime calls
3687 * assembly/type resolve hooks.
3688 * FIXME: Breakpoints too.
3692 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
3693 mono_arch_skip_single_step (&ctx);
3694 mono_arch_monoctx_to_sigctx (&ctx, sigctx);
3698 resume_from_signal_handler (sigctx, process_single_step);
3702 * start_single_stepping:
3704 * Turn on single stepping. Can be called multiple times, for example,
3705 * by a single step event request + a suspend.
3708 start_single_stepping (void)
3710 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3711 int val = InterlockedIncrement (&ss_count);
3714 mono_arch_start_single_stepping ();
3716 if (ss_req != NULL && ss_invoke_addr == NULL) {
3717 DebuggerTlsData *tls;
3719 mono_loader_lock ();
3721 tls = mono_g_hash_table_lookup (thread_to_tls, ss_req->thread);
3722 ss_invoke_addr = tls->invoke_addr;
3724 mono_loader_unlock ();
3727 g_assert_not_reached ();
3732 stop_single_stepping (void)
3734 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3735 int val = InterlockedDecrement (&ss_count);
3738 mono_arch_stop_single_stepping ();
3740 g_assert_not_reached ();
3747 * Stop the single stepping operation given by SS_REQ.
3750 ss_stop (SingleStepReq *ss_req)
3752 gboolean use_bps = FALSE;
3759 for (l = ss_req->bps; l; l = l->next) {
3760 clear_breakpoint (l->data);
3762 g_slist_free (ss_req->bps);
3766 if (ss_req->global) {
3767 stop_single_stepping ();
3768 ss_req->global = FALSE;
3775 * Start the single stepping operation given by SS_REQ from the sequence point SP.
3778 ss_start (SingleStepReq *ss_req, MonoMethod *method, SeqPoint *sp, MonoSeqPointInfo *info, MonoContext *ctx, DebuggerTlsData *tls)
3780 gboolean use_bp = FALSE;
3785 /* Stop the previous operation */
3789 * Implement single stepping using breakpoints if possible.
3791 if (ss_req->depth == STEP_DEPTH_OVER) {
3794 * Find the first sequence point in the current or in a previous frame which
3795 * is not the last in its method.
3797 while (sp && sp->next_len == 0) {
3799 if (tls && frame_index < tls->frame_count) {
3800 StackFrame *frame = tls->frames [frame_index];
3802 method = frame->method;
3803 if (frame->il_offset != -1) {
3804 sp = find_seq_point (frame->domain, frame->method, frame->il_offset, &info);
3810 if (sp && sp->next_len > 0) {
3812 for (i = 0; i < sp->next_len; ++i) {
3813 next_sp = &info->seq_points [sp->next [i]];
3815 bp = set_breakpoint (method, next_sp->il_offset, ss_req->req);
3816 ss_req->bps = g_slist_append (ss_req->bps, bp);
3822 ss_req->global = TRUE;
3823 start_single_stepping ();
3825 ss_req->global = FALSE;
3830 * Start single stepping of thread THREAD
3833 ss_create (MonoInternalThread *thread, StepSize size, StepDepth depth, EventRequest *req)
3835 DebuggerTlsData *tls;
3836 MonoSeqPointInfo *info;
3837 SeqPoint *sp = NULL;
3838 MonoMethod *method = NULL;
3840 if (suspend_count == 0)
3841 return ERR_NOT_SUSPENDED;
3843 wait_for_suspend ();
3845 // FIXME: Multiple requests
3847 DEBUG (0, printf ("Received a single step request while the previous one was still active.\n"));
3848 return ERR_NOT_IMPLEMENTED;
3851 ss_req = g_new0 (SingleStepReq, 1);
3853 ss_req->thread = thread;
3854 ss_req->size = size;
3855 ss_req->depth = depth;
3858 mono_loader_lock ();
3859 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
3860 mono_loader_unlock ();
3862 g_assert (tls->has_context);
3863 ss_req->start_sp = ss_req->last_sp = MONO_CONTEXT_GET_SP (&tls->ctx);
3865 if (ss_req->size == STEP_SIZE_LINE) {
3867 MonoDebugMethodInfo *minfo;
3869 /* Compute the initial line info */
3870 compute_frame_info (thread, tls);
3872 g_assert (tls->frame_count);
3873 frame = tls->frames [0];
3875 ss_req->last_method = frame->method;
3876 ss_req->last_line = -1;
3878 minfo = mono_debug_lookup_method (frame->method);
3879 if (minfo && frame->il_offset != -1) {
3880 MonoDebugSourceLocation *loc = mono_debug_symfile_lookup_location (minfo, frame->il_offset);
3883 ss_req->last_line = loc->row;
3889 if (ss_req->depth == STEP_DEPTH_OVER) {
3892 compute_frame_info (thread, tls);
3894 g_assert (tls->frame_count);
3895 frame = tls->frames [0];
3897 if (frame->il_offset != -1) {
3898 /* FIXME: Sort the table and use a binary search */
3899 sp = find_seq_point (frame->domain, frame->method, frame->il_offset, &info);
3901 method = frame->method;
3905 ss_start (ss_req, method, sp, info, NULL, tls);
3911 ss_destroy (SingleStepReq *req)
3914 g_assert (ss_req == req);
3923 mono_debugger_agent_handle_exception (MonoException *exc, MonoContext *throw_ctx,
3924 MonoContext *catch_ctx)
3931 memset (&ei, 0, sizeof (EventInfo));
3933 /* Just-In-Time debugging */
3935 if (agent_config.onuncaught && !inited) {
3936 finish_agent_init (FALSE);
3939 * Send an unsolicited EXCEPTION event with a dummy request id.
3941 events = g_slist_append (NULL, GUINT_TO_POINTER (0xffffff));
3942 ei.exc = (MonoObject*)exc;
3943 process_event (EVENT_KIND_EXCEPTION, &ei, 0, throw_ctx, events, SUSPEND_POLICY_ALL);
3946 } else if (agent_config.onthrow && !inited) {
3948 gboolean found = FALSE;
3950 for (l = agent_config.onthrow; l; l = l->next) {
3951 char *ex_type = l->data;
3952 char *f = mono_type_full_name (&exc->object.vtable->klass->byval_arg);
3954 if (!strcmp (ex_type, "") || !strcmp (ex_type, f))
3961 finish_agent_init (FALSE);
3964 * Send an unsolicited EXCEPTION event with a dummy request id.
3966 events = g_slist_append (NULL, GUINT_TO_POINTER (0xffffff));
3967 ei.exc = (MonoObject*)exc;
3968 process_event (EVENT_KIND_EXCEPTION, &ei, 0, throw_ctx, events, SUSPEND_POLICY_ALL);
3976 ji = mini_jit_info_table_find (mono_domain_get (), MONO_CONTEXT_GET_IP (throw_ctx), NULL);
3978 ei.exc = (MonoObject*)exc;
3979 ei.caught = catch_ctx != NULL;
3981 mono_loader_lock ();
3982 events = create_event_list (EVENT_KIND_EXCEPTION, NULL, ji, &ei, &suspend_policy);
3983 mono_loader_unlock ();
3985 process_event (EVENT_KIND_EXCEPTION, &ei, 0, throw_ctx, events, suspend_policy);
3989 * buffer_add_value_full:
3991 * Add the encoding of the value at ADDR described by T to the buffer.
3992 * AS_VTYPE determines whenever to treat primitive types as primitive types or
3996 buffer_add_value_full (Buffer *buf, MonoType *t, void *addr, MonoDomain *domain,
4002 g_assert (*(void**)addr);
4003 addr = *(void**)addr;
4008 case MONO_TYPE_BOOLEAN:
4011 case MONO_TYPE_CHAR:
4031 case MONO_TYPE_VOID:
4032 buffer_add_byte (buf, t->type);
4034 case MONO_TYPE_BOOLEAN:
4037 buffer_add_byte (buf, t->type);
4038 buffer_add_int (buf, *(gint8*)addr);
4040 case MONO_TYPE_CHAR:
4043 buffer_add_byte (buf, t->type);
4044 buffer_add_int (buf, *(gint16*)addr);
4049 buffer_add_byte (buf, t->type);
4050 buffer_add_int (buf, *(gint32*)addr);
4055 buffer_add_byte (buf, t->type);
4056 buffer_add_long (buf, *(gint64*)addr);
4060 /* Treat it as a vtype */
4062 case MONO_TYPE_PTR: {
4063 gssize val = *(gssize*)addr;
4065 buffer_add_byte (buf, t->type);
4066 buffer_add_long (buf, val);
4070 case MONO_TYPE_STRING:
4071 case MONO_TYPE_SZARRAY:
4072 case MONO_TYPE_OBJECT:
4073 case MONO_TYPE_CLASS:
4074 case MONO_TYPE_ARRAY:
4075 obj = *(MonoObject**)addr;
4078 buffer_add_byte (buf, VALUE_TYPE_ID_NULL);
4080 if (obj->vtable->klass->valuetype) {
4081 t = &obj->vtable->klass->byval_arg;
4082 addr = mono_object_unbox (obj);
4084 } else if (obj->vtable->klass->rank) {
4085 buffer_add_byte (buf, obj->vtable->klass->byval_arg.type);
4086 } else if (obj->vtable->klass->byval_arg.type == MONO_TYPE_GENERICINST) {
4087 buffer_add_byte (buf, MONO_TYPE_CLASS);
4089 buffer_add_byte (buf, obj->vtable->klass->byval_arg.type);
4091 buffer_add_objid (buf, obj);
4095 case MONO_TYPE_VALUETYPE: {
4099 MonoClass *klass = mono_class_from_mono_type (t);
4101 buffer_add_byte (buf, MONO_TYPE_VALUETYPE);
4102 buffer_add_byte (buf, klass->enumtype);
4103 buffer_add_typeid (buf, domain, klass);
4107 while ((f = mono_class_get_fields (klass, &iter))) {
4108 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC)
4110 if (mono_field_is_deleted (f))
4114 buffer_add_int (buf, nfields);
4117 while ((f = mono_class_get_fields (klass, &iter))) {
4118 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC)
4120 if (mono_field_is_deleted (f))
4122 buffer_add_value_full (buf, f->type, (guint8*)addr + f->offset - sizeof (MonoObject), domain, FALSE);
4126 case MONO_TYPE_GENERICINST:
4127 if (mono_type_generic_inst_is_valuetype (t)) {
4139 buffer_add_value (Buffer *buf, MonoType *t, void *addr, MonoDomain *domain)
4141 buffer_add_value_full (buf, t, addr, domain, FALSE);
4145 decode_value (MonoType *t, MonoDomain *domain, guint8 *addr, guint8 *buf, guint8 **endbuf, guint8 *limit)
4148 int type = decode_byte (buf, &buf, limit);
4150 if (type != t->type && !MONO_TYPE_IS_REFERENCE (t) &&
4151 !(t->type == MONO_TYPE_I && type == MONO_TYPE_VALUETYPE) &&
4152 !(t->type == MONO_TYPE_U && type == MONO_TYPE_VALUETYPE) &&
4153 !(t->type == MONO_TYPE_PTR && type == MONO_TYPE_I8) &&
4154 !(t->type == MONO_TYPE_GENERICINST && type == MONO_TYPE_VALUETYPE)) {
4155 char *name = mono_type_full_name (t);
4156 DEBUG(1, fprintf (log_file, "[%p] Expected value of type %s, got 0x%0x.\n", (gpointer)GetCurrentThreadId (), name, type));
4158 return ERR_INVALID_ARGUMENT;
4162 case MONO_TYPE_BOOLEAN:
4163 *(guint8*)addr = decode_int (buf, &buf, limit);
4165 case MONO_TYPE_CHAR:
4166 *(gunichar2*)addr = decode_int (buf, &buf, limit);
4169 *(gint8*)addr = decode_int (buf, &buf, limit);
4172 *(guint8*)addr = decode_int (buf, &buf, limit);
4175 *(gint16*)addr = decode_int (buf, &buf, limit);
4178 *(guint16*)addr = decode_int (buf, &buf, limit);
4181 *(gint32*)addr = decode_int (buf, &buf, limit);
4184 *(guint32*)addr = decode_int (buf, &buf, limit);
4187 *(gint64*)addr = decode_long (buf, &buf, limit);
4190 *(guint64*)addr = decode_long (buf, &buf, limit);
4193 *(guint32*)addr = decode_int (buf, &buf, limit);
4196 *(guint64*)addr = decode_long (buf, &buf, limit);
4199 /* We send these as I8, so we get them back as such */
4200 g_assert (type == MONO_TYPE_I8);
4201 *(gssize*)addr = decode_long (buf, &buf, limit);
4203 case MONO_TYPE_GENERICINST:
4204 if (MONO_TYPE_ISSTRUCT (t)) {
4205 /* The client sends these as a valuetype */
4213 /* We send these as vtypes, so we get them back as such */
4214 g_assert (type == MONO_TYPE_VALUETYPE);
4217 case MONO_TYPE_VALUETYPE: {
4218 gboolean is_enum = decode_byte (buf, &buf, limit);
4222 gpointer iter = NULL;
4225 /* Enums are sent as a normal vtype */
4227 return ERR_NOT_IMPLEMENTED;
4228 klass = decode_typeid (buf, &buf, limit, &d, &err);
4232 if (klass != mono_class_from_mono_type (t))
4233 return ERR_INVALID_ARGUMENT;
4235 nfields = decode_int (buf, &buf, limit);
4236 while ((f = mono_class_get_fields (klass, &iter))) {
4237 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC)
4239 if (mono_field_is_deleted (f))
4241 err = decode_value (f->type, domain, (guint8*)addr + f->offset - sizeof (MonoObject), buf, &buf, limit);
4246 g_assert (nfields == 0);
4251 if (MONO_TYPE_IS_REFERENCE (t)) {
4252 if (type == MONO_TYPE_OBJECT) {
4253 int objid = decode_objid (buf, &buf, limit);
4257 err = get_object (objid, (MonoObject**)&obj);
4261 if (obj && !mono_class_is_assignable_from (mono_class_from_mono_type (t), obj->vtable->klass))
4262 return ERR_INVALID_ARGUMENT;
4263 if (obj && obj->vtable->domain != domain)
4264 return ERR_INVALID_ARGUMENT;
4266 mono_gc_wbarrier_generic_store (addr, obj);
4267 } else if (type == VALUE_TYPE_ID_NULL) {
4268 *(MonoObject**)addr = NULL;
4270 return ERR_INVALID_ARGUMENT;
4284 add_var (Buffer *buf, MonoType *t, MonoDebugVarInfo *var, MonoContext *ctx, MonoDomain *domain, gboolean as_vtype)
4291 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
4292 reg = var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
4295 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
4296 reg_val = mono_arch_context_get_int_reg (ctx, reg);
4298 buffer_add_value_full (buf, t, ®_val, domain, as_vtype);
4300 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
4301 addr = mono_arch_context_get_int_reg (ctx, reg);
4302 addr += (gint32)var->offset;
4304 //printf ("[R%d+%d] = %p\n", reg, var->offset, addr);
4306 buffer_add_value_full (buf, t, addr, domain, as_vtype);
4308 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
4312 g_assert_not_reached ();
4317 set_var (MonoType *t, MonoDebugVarInfo *var, MonoContext *ctx, MonoDomain *domain, guint8 *val)
4323 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
4324 reg = var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
4326 if (MONO_TYPE_IS_REFERENCE (t))
4327 size = sizeof (gpointer);
4329 size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
4332 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
4333 // FIXME: Can't set registers, so we disable linears
4336 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
4337 addr = mono_arch_context_get_int_reg (ctx, reg);
4338 addr += (gint32)var->offset;
4340 //printf ("[R%d+%d] = %p\n", reg, var->offset, addr);
4342 // FIXME: Write barriers
4343 memcpy (addr, val, size);
4345 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
4349 g_assert_not_reached ();
4354 clear_event_request (int req_id, int etype)
4358 mono_loader_lock ();
4359 for (i = 0; i < event_requests->len; ++i) {
4360 EventRequest *req = g_ptr_array_index (event_requests, i);
4362 if (req->id == req_id && req->event_kind == etype) {
4363 if (req->event_kind == EVENT_KIND_BREAKPOINT)
4364 clear_breakpoint (req->info);
4365 if (req->event_kind == EVENT_KIND_STEP)
4366 ss_destroy (req->info);
4367 if (req->event_kind == EVENT_KIND_METHOD_ENTRY)
4368 clear_breakpoint (req->info);
4369 if (req->event_kind == EVENT_KIND_METHOD_EXIT)
4370 clear_breakpoint (req->info);
4371 g_ptr_array_remove_index_fast (event_requests, i);
4376 mono_loader_unlock ();
4380 add_thread (gpointer key, gpointer value, gpointer user_data)
4382 MonoInternalThread *thread = value;
4383 Buffer *buf = user_data;
4385 buffer_add_objid (buf, (MonoObject*)thread);
4389 do_invoke_method (DebuggerTlsData *tls, Buffer *buf, InvokeData *invoke)
4391 guint8 *p = invoke->p;
4392 guint8 *end = invoke->endp;
4395 MonoMethodSignature *sig;
4398 MonoObject *this, *res, *exc;
4401 #ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
4405 if (invoke->method) {
4407 * Invoke this method directly, currently only Environment.Exit () is supported.
4410 DEBUG (1, printf ("[%p] Invoking method '%s' on receiver '%s'.\n", (gpointer)GetCurrentThreadId (), mono_method_full_name (invoke->method, TRUE), this ? this->vtable->klass->name : "<null>"));
4411 mono_runtime_invoke (invoke->method, NULL, invoke->args, &exc);
4412 g_assert_not_reached ();
4415 m = decode_methodid (p, &p, end, &domain, &err);
4418 sig = mono_method_signature (m);
4420 if (m->klass->valuetype)
4421 this_buf = g_alloca (mono_class_instance_size (m->klass));
4423 this_buf = g_alloca (sizeof (MonoObject*));
4424 if (m->klass->valuetype && (m->flags & METHOD_ATTRIBUTE_STATIC)) {
4425 /* Should be null */
4426 int type = decode_byte (p, &p, end);
4427 if (type != VALUE_TYPE_ID_NULL)
4428 return ERR_INVALID_ARGUMENT;
4429 memset (this_buf, 0, mono_class_instance_size (m->klass));
4431 err = decode_value (&m->klass->byval_arg, domain, this_buf, p, &p, end);
4436 if (!m->klass->valuetype)
4437 this = *(MonoObject**)this_buf;
4441 DEBUG (1, printf ("[%p] Invoking method '%s' on receiver '%s'.\n", (gpointer)GetCurrentThreadId (), mono_method_full_name (m, TRUE), this ? this->vtable->klass->name : "<null>"));
4443 if (this && this->vtable->domain != domain)
4446 if (!m->klass->valuetype && !(m->flags & METHOD_ATTRIBUTE_STATIC) && !this) {
4447 if (!strcmp (m->name, ".ctor")) {
4448 if (m->klass->flags & TYPE_ATTRIBUTE_ABSTRACT)
4449 return ERR_INVALID_ARGUMENT;
4451 this = mono_object_new (domain, m->klass);
4453 return ERR_INVALID_ARGUMENT;
4457 if (this && !mono_class_is_assignable_from (m->klass, this->vtable->klass))
4458 return ERR_INVALID_ARGUMENT;
4460 nargs = decode_int (p, &p, end);
4461 if (nargs != sig->param_count)
4462 return ERR_INVALID_ARGUMENT;
4463 /* Use alloca to get gc tracking */
4464 arg_buf = g_alloca (nargs * sizeof (gpointer));
4465 memset (arg_buf, 0, nargs * sizeof (gpointer));
4466 args = g_alloca (nargs * sizeof (gpointer));
4467 for (i = 0; i < nargs; ++i) {
4468 if (MONO_TYPE_IS_REFERENCE (sig->params [i])) {
4469 err = decode_value (sig->params [i], domain, (guint8*)&args [i], p, &p, end);
4473 if (args [i] && ((MonoObject*)args [i])->vtable->domain != domain)
4476 arg_buf [i] = g_alloca (mono_class_instance_size (mono_class_from_mono_type (sig->params [i])));
4477 err = decode_value (sig->params [i], domain, arg_buf [i], p, &p, end);
4480 args [i] = arg_buf [i];
4487 if (invoke->flags & INVOKE_FLAG_DISABLE_BREAKPOINTS)
4488 tls->disable_breakpoints = TRUE;
4490 tls->disable_breakpoints = FALSE;
4493 * Add an LMF frame to link the stack frames on the invoke method with our caller.
4495 /* FIXME: Move this to arch specific code */
4496 #ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
4497 if (invoke->has_ctx) {
4500 lmf_addr = mono_get_lmf_addr ();
4503 memset (&ext, 0, sizeof (ext));
4505 ext.lmf.previous_lmf = *(lmf_addr);
4506 /* Mark that this is a MonoLMFExt */
4507 ext.lmf.previous_lmf = (gpointer)(((gssize)ext.lmf.previous_lmf) | 2);
4508 ext.lmf.rsp = (gssize)&ext;
4509 #elif defined(TARGET_X86)
4510 ext.lmf.previous_lmf = (gsize)*(lmf_addr);
4511 /* Mark that this is a MonoLMFExt */
4512 ext.lmf.previous_lmf = (gsize)(gpointer)(((gssize)ext.lmf.previous_lmf) | 2);
4513 ext.lmf.ebp = (gssize)&ext;
4514 #elif defined(TARGET_ARM)
4515 ext.lmf.previous_lmf = *(lmf_addr);
4516 /* Mark that this is a MonoLMFExt */
4517 ext.lmf.previous_lmf = (gpointer)(((gssize)ext.lmf.previous_lmf) | 2);
4518 ext.lmf.ebp = (gssize)&ext;
4519 #elif defined(TARGET_POWERPC)
4520 ext.lmf.previous_lmf = *(lmf_addr);
4521 /* Mark that this is a MonoLMFExt */
4522 ext.lmf.previous_lmf = (gpointer)(((gssize)ext.lmf.previous_lmf) | 2);
4523 ext.lmf.ebp = (gssize)&ext;
4525 g_assert_not_reached ();
4528 ext.debugger_invoke = TRUE;
4529 memcpy (&ext.ctx, &invoke->ctx, sizeof (MonoContext));
4531 mono_set_lmf ((MonoLMF*)&ext);
4535 if (m->klass->valuetype)
4536 res = mono_runtime_invoke (m, this_buf, args, &exc);
4538 res = mono_runtime_invoke (m, this, args, &exc);
4540 buffer_add_byte (buf, 0);
4541 buffer_add_value (buf, &mono_defaults.object_class->byval_arg, &exc, domain);
4543 buffer_add_byte (buf, 1);
4544 if (sig->ret->type == MONO_TYPE_VOID) {
4545 if (!strcmp (m->name, ".ctor") && !m->klass->valuetype) {
4546 buffer_add_value (buf, &mono_defaults.object_class->byval_arg, &this, domain);
4549 buffer_add_value (buf, &mono_defaults.void_class->byval_arg, NULL, domain);
4550 } else if (MONO_TYPE_IS_REFERENCE (sig->ret)) {
4551 buffer_add_value (buf, sig->ret, &res, domain);
4552 } else if (mono_class_from_mono_type (sig->ret)->valuetype) {
4554 buffer_add_value (buf, sig->ret, mono_object_unbox (res), domain);
4560 tls->disable_breakpoints = FALSE;
4562 #ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
4563 if (invoke->has_ctx)
4564 mono_set_lmf ((gpointer)(((gssize)ext.lmf.previous_lmf) & ~3));
4567 // FIXME: byref arguments
4575 * Invoke the method given by tls->invoke in the current thread.
4578 invoke_method (void)
4580 DebuggerTlsData *tls;
4585 static void (*restore_context) (void *);
4586 MonoContext restore_ctx;
4588 if (!restore_context)
4589 restore_context = mono_get_restore_context ();
4591 tls = TlsGetValue (debugger_tls_id);
4594 invoke = tls->invoke;
4598 tls->frames_up_to_date = FALSE;
4602 buffer_init (&buf, 128);
4604 err = do_invoke_method (tls, &buf, invoke);
4606 /* Start suspending before sending the reply */
4607 if (!(invoke->flags & INVOKE_FLAG_SINGLE_THREADED))
4610 send_reply_packet (id, err, &buf);
4614 memcpy (&restore_ctx, &invoke->ctx, sizeof (MonoContext));
4616 if (invoke->has_ctx)
4617 save_thread_context (&restore_ctx);
4619 if (invoke->flags & INVOKE_FLAG_SINGLE_THREADED) {
4620 g_assert (tls->resume_count);
4621 tls->resume_count -= invoke->suspend_count;
4624 DEBUG (1, printf ("[%p] Invoke finished, resume_count = %d.\n", (gpointer)GetCurrentThreadId (), tls->resume_count));
4633 is_really_suspended (gpointer key, gpointer value, gpointer user_data)
4635 MonoThread *thread = value;
4636 DebuggerTlsData *tls;
4639 mono_loader_lock ();
4640 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
4642 res = tls->really_suspended;
4643 mono_loader_unlock ();
4649 vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
4652 case CMD_VM_VERSION: {
4653 char *build_info, *version;
4655 build_info = mono_get_runtime_build_info ();
4656 version = g_strdup_printf ("mono %s", build_info);
4658 buffer_add_string (buf, version); /* vm version */
4659 buffer_add_int (buf, MAJOR_VERSION);
4660 buffer_add_int (buf, MINOR_VERSION);
4661 g_free (build_info);
4665 case CMD_VM_SET_PROTOCOL_VERSION: {
4666 major_version = decode_int (p, &p, end);
4667 minor_version = decode_int (p, &p, end);
4668 protocol_version_set = TRUE;
4669 DEBUG(1, fprintf (log_file, "[dbg] Protocol version %d.%d, client protocol version %d.%d.\n", MAJOR_VERSION, MINOR_VERSION, major_version, minor_version));
4672 case CMD_VM_ALL_THREADS: {
4674 mono_loader_lock ();
4675 buffer_add_int (buf, mono_g_hash_table_size (tid_to_thread_obj));
4676 mono_g_hash_table_foreach (tid_to_thread_obj, add_thread, buf);
4677 mono_loader_unlock ();
4680 case CMD_VM_SUSPEND:
4682 wait_for_suspend ();
4685 if (suspend_count == 0)
4686 return ERR_NOT_SUSPENDED;
4689 case CMD_VM_DISPOSE:
4690 /* Clear all event requests */
4691 mono_loader_lock ();
4692 while (event_requests->len > 0) {
4693 EventRequest *req = g_ptr_array_index (event_requests, 0);
4695 clear_event_request (req->id, req->event_kind);
4697 mono_loader_unlock ();
4699 // FIXME: Count resumes
4701 disconnected = TRUE;
4704 MonoInternalThread *thread;
4705 DebuggerTlsData *tls;
4706 MonoClass *env_class;
4707 MonoMethod *exit_method;
4711 exit_code = decode_int (p, &p, end);
4713 // FIXME: What if there is a VM_DEATH event request with SUSPEND_ALL ?
4715 /* Have to send a reply before exiting */
4716 send_reply_packet (id, 0, buf);
4718 /* Clear all event requests */
4719 mono_loader_lock ();
4720 while (event_requests->len > 0) {
4721 EventRequest *req = g_ptr_array_index (event_requests, 0);
4723 clear_event_request (req->id, req->event_kind);
4725 mono_loader_unlock ();
4728 * The JDWP documentation says that the shutdown is not orderly. It doesn't
4729 * specify whenever a VM_DEATH event is sent. We currently do an orderly
4730 * shutdown by hijacking a thread to execute Environment.Exit (). This is
4731 * better than doing the shutdown ourselves, since it avoids various races.
4735 wait_for_suspend ();
4737 env_class = mono_class_from_name (mono_defaults.corlib, "System", "Environment");
4738 g_assert (env_class);
4739 exit_method = mono_class_get_method_from_name (env_class, "Exit", 1);
4740 g_assert (exit_method);
4742 mono_loader_lock ();
4743 thread = mono_g_hash_table_find (tid_to_thread, is_really_suspended, NULL);
4744 mono_loader_unlock ();
4747 mono_loader_lock ();
4748 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
4749 mono_loader_unlock ();
4751 args = g_new0 (gpointer, 1);
4752 args [0] = g_malloc (sizeof (int));
4753 *(int*)(args [0]) = exit_code;
4755 tls->invoke = g_new0 (InvokeData, 1);
4756 tls->invoke->method = exit_method;
4757 tls->invoke->args = args;
4759 while (suspend_count > 0)
4763 * No thread found, do it ourselves.
4764 * FIXME: This can race with normal shutdown etc.
4766 while (suspend_count > 0)
4769 mono_runtime_set_shutting_down ();
4771 mono_threads_set_shutting_down ();
4773 /* Suspend all managed threads since the runtime is going away */
4774 DEBUG(1, fprintf (log_file, "Suspending all threads...\n"));
4775 mono_thread_suspend_all_other_threads ();
4776 DEBUG(1, fprintf (log_file, "Shutting down the runtime...\n"));
4777 mono_runtime_quit ();
4779 shutdown (conn_fd, SD_BOTH);
4781 shutdown (conn_fd, SHUT_RDWR);
4783 DEBUG(1, fprintf (log_file, "Exiting...\n"));
4789 case CMD_VM_INVOKE_METHOD: {
4790 int objid = decode_objid (p, &p, end);
4792 DebuggerTlsData *tls;
4795 err = get_object (objid, (MonoObject**)&thread);
4799 flags = decode_int (p, &p, end);
4801 // Wait for suspending if it already started
4803 wait_for_suspend ();
4804 if (!is_suspended ())
4805 return ERR_NOT_SUSPENDED;
4807 mono_loader_lock ();
4808 tls = mono_g_hash_table_lookup (thread_to_tls, THREAD_TO_INTERNAL (thread));
4809 mono_loader_unlock ();
4812 if (!tls->really_suspended)
4813 /* The thread is still running native code, can't do invokes */
4814 return ERR_NOT_SUSPENDED;
4817 * Store the invoke data into tls, the thread will execute it after it is
4822 tls->invoke = g_new0 (InvokeData, 1);
4823 tls->invoke->id = id;
4824 tls->invoke->flags = flags;
4825 tls->invoke->p = g_malloc (end - p);
4826 memcpy (tls->invoke->p, p, end - p);
4827 tls->invoke->endp = tls->invoke->p + (end - p);
4828 tls->invoke->suspend_count = suspend_count;
4830 if (flags & INVOKE_FLAG_SINGLE_THREADED)
4831 resume_thread (THREAD_TO_INTERNAL (thread));
4837 return ERR_NOT_IMPLEMENTED;
4844 event_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
4849 case CMD_EVENT_REQUEST_SET: {
4851 int i, event_kind, suspend_policy, nmodifiers, mod;
4854 MonoThread *step_thread;
4855 int size = 0, depth = 0, step_thread_id = 0;
4858 event_kind = decode_byte (p, &p, end);
4859 suspend_policy = decode_byte (p, &p, end);
4860 nmodifiers = decode_byte (p, &p, end);
4862 req = g_malloc0 (sizeof (EventRequest) + (nmodifiers * sizeof (Modifier)));
4863 req->id = InterlockedIncrement (&event_request_id);
4864 req->event_kind = event_kind;
4865 req->suspend_policy = suspend_policy;
4866 req->nmodifiers = nmodifiers;
4869 for (i = 0; i < nmodifiers; ++i) {
4870 mod = decode_byte (p, &p, end);
4872 req->modifiers [i].kind = mod;
4873 if (mod == MOD_KIND_COUNT) {
4874 req->modifiers [i].data.count = decode_int (p, &p, end);
4875 } else if (mod == MOD_KIND_LOCATION_ONLY) {
4876 method = decode_methodid (p, &p, end, &domain, &err);
4879 location = decode_long (p, &p, end);
4880 } else if (mod == MOD_KIND_STEP) {
4881 step_thread_id = decode_id (p, &p, end);
4882 size = decode_int (p, &p, end);
4883 depth = decode_int (p, &p, end);
4884 } else if (mod == MOD_KIND_THREAD_ONLY) {
4885 int id = decode_id (p, &p, end);
4887 err = get_object (id, (MonoObject**)&req->modifiers [i].data.thread);
4892 } else if (mod == MOD_KIND_EXCEPTION_ONLY) {
4893 MonoClass *exc_class = decode_typeid (p, &p, end, &domain, &err);
4897 req->modifiers [i].caught = decode_byte (p, &p, end);
4898 req->modifiers [i].uncaught = decode_byte (p, &p, end);
4900 req->modifiers [i].data.exc_class = exc_class;
4902 if (!mono_class_is_assignable_from (mono_defaults.exception_class, exc_class)) {
4904 return ERR_INVALID_ARGUMENT;
4907 } else if (mod == MOD_KIND_ASSEMBLY_ONLY) {
4908 int n = decode_int (p, &p, end);
4911 req->modifiers [i].data.assemblies = g_new0 (MonoAssembly*, n);
4912 for (j = 0; j < n; ++j) {
4913 req->modifiers [i].data.assemblies [j] = decode_assemblyid (p, &p, end, &domain, &err);
4915 g_free (req->modifiers [i].data.assemblies);
4921 return ERR_NOT_IMPLEMENTED;
4925 if (req->event_kind == EVENT_KIND_BREAKPOINT) {
4928 req->info = set_breakpoint (method, location, req);
4929 } else if (req->event_kind == EVENT_KIND_STEP) {
4930 g_assert (step_thread_id);
4932 err = get_object (step_thread_id, (MonoObject**)&step_thread);
4938 err = ss_create (THREAD_TO_INTERNAL (step_thread), size, depth, req);
4943 } else if (req->event_kind == EVENT_KIND_METHOD_ENTRY) {
4944 req->info = set_breakpoint (NULL, METHOD_ENTRY_IL_OFFSET, req);
4945 } else if (req->event_kind == EVENT_KIND_METHOD_EXIT) {
4946 req->info = set_breakpoint (NULL, METHOD_EXIT_IL_OFFSET, req);
4947 } else if (req->event_kind == EVENT_KIND_EXCEPTION) {
4949 if (req->nmodifiers) {
4951 return ERR_NOT_IMPLEMENTED;
4955 mono_loader_lock ();
4956 g_ptr_array_add (event_requests, req);
4957 mono_loader_unlock ();
4959 buffer_add_int (buf, req->id);
4962 case CMD_EVENT_REQUEST_CLEAR: {
4963 int etype = decode_byte (p, &p, end);
4964 int req_id = decode_int (p, &p, end);
4966 // FIXME: Make a faster mapping from req_id to request
4967 mono_loader_lock ();
4968 clear_event_request (req_id, etype);
4969 mono_loader_unlock ();
4972 case CMD_EVENT_REQUEST_CLEAR_ALL_BREAKPOINTS: {
4975 mono_loader_lock ();
4977 while (i < event_requests->len) {
4978 EventRequest *req = g_ptr_array_index (event_requests, i);
4980 if (req->event_kind == EVENT_KIND_BREAKPOINT) {
4981 clear_breakpoint (req->info);
4983 g_ptr_array_remove_index_fast (event_requests, i);
4989 mono_loader_unlock ();
4993 return ERR_NOT_IMPLEMENTED;
5000 domain_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5006 case CMD_APPDOMAIN_GET_ROOT_DOMAIN: {
5007 buffer_add_domainid (buf, mono_get_root_domain ());
5010 case CMD_APPDOMAIN_GET_FRIENDLY_NAME: {
5011 domain = decode_domainid (p, &p, end, NULL, &err);
5014 buffer_add_string (buf, domain->friendly_name);
5017 case CMD_APPDOMAIN_GET_ASSEMBLIES: {
5022 domain = decode_domainid (p, &p, end, NULL, &err);
5025 mono_loader_lock ();
5027 for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
5030 buffer_add_int (buf, count);
5031 for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
5033 buffer_add_assemblyid (buf, domain, ass);
5035 mono_loader_unlock ();
5038 case CMD_APPDOMAIN_GET_ENTRY_ASSEMBLY: {
5039 domain = decode_domainid (p, &p, end, NULL, &err);
5043 buffer_add_assemblyid (buf, domain, domain->entry_assembly);
5046 case CMD_APPDOMAIN_GET_CORLIB: {
5047 domain = decode_domainid (p, &p, end, NULL, &err);
5051 buffer_add_assemblyid (buf, domain, domain->domain->mbr.obj.vtable->klass->image->assembly);
5054 case CMD_APPDOMAIN_CREATE_STRING: {
5058 domain = decode_domainid (p, &p, end, NULL, &err);
5061 s = decode_string (p, &p, end);
5063 o = mono_string_new (domain, s);
5064 buffer_add_objid (buf, (MonoObject*)o);
5067 case CMD_APPDOMAIN_CREATE_BOXED_VALUE: {
5069 MonoDomain *domain2;
5072 domain = decode_domainid (p, &p, end, NULL, &err);
5075 klass = decode_typeid (p, &p, end, &domain2, &err);
5080 g_assert (domain == domain2);
5082 o = mono_object_new (domain, klass);
5084 err = decode_value (&klass->byval_arg, domain, mono_object_unbox (o), p, &p, end);
5088 buffer_add_objid (buf, o);
5092 return ERR_NOT_IMPLEMENTED;
5099 assembly_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5105 ass = decode_assemblyid (p, &p, end, &domain, &err);
5110 case CMD_ASSEMBLY_GET_LOCATION: {
5111 buffer_add_string (buf, mono_image_get_filename (ass->image));
5114 case CMD_ASSEMBLY_GET_ENTRY_POINT: {
5118 if (ass->image->dynamic) {
5119 buffer_add_id (buf, 0);
5121 token = mono_image_get_entry_point (ass->image);
5123 buffer_add_id (buf, 0);
5125 m = mono_get_method (ass->image, token, NULL);
5126 buffer_add_methodid (buf, domain, m);
5131 case CMD_ASSEMBLY_GET_MANIFEST_MODULE: {
5132 buffer_add_moduleid (buf, domain, ass->image);
5135 case CMD_ASSEMBLY_GET_OBJECT: {
5136 MonoObject *o = (MonoObject*)mono_assembly_get_object (mono_domain_get (), ass);
5137 buffer_add_objid (buf, o);
5140 case CMD_ASSEMBLY_GET_TYPE: {
5141 char *s = decode_string (p, &p, end);
5142 gboolean ignorecase = decode_byte (p, &p, end);
5143 MonoTypeNameParse info;
5145 gboolean type_resolve;
5147 if (!mono_reflection_parse_type (s, &info)) {
5150 if (info.assembly.name)
5152 t = mono_reflection_get_type (ass->image, &info, ignorecase, &type_resolve);
5154 buffer_add_typeid (buf, domain, t ? mono_class_from_mono_type (t) : NULL);
5155 mono_reflection_free_type_info (&info);
5160 case CMD_ASSEMBLY_GET_NAME: {
5162 MonoAssembly *mass = ass;
5164 name = g_strdup_printf (
5165 "%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
5167 mass->aname.major, mass->aname.minor, mass->aname.build, mass->aname.revision,
5168 mass->aname.culture && *mass->aname.culture? mass->aname.culture: "neutral",
5169 mass->aname.public_key_token [0] ? (char *)mass->aname.public_key_token : "null",
5170 (mass->aname.flags & ASSEMBLYREF_RETARGETABLE_FLAG) ? ", Retargetable=Yes" : "");
5172 buffer_add_string (buf, name);
5177 return ERR_NOT_IMPLEMENTED;
5184 module_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5190 case CMD_MODULE_GET_INFO: {
5191 MonoImage *image = decode_moduleid (p, &p, end, &domain, &err);
5194 basename = g_path_get_basename (image->name);
5195 buffer_add_string (buf, basename); // name
5196 buffer_add_string (buf, image->module_name); // scopename
5197 buffer_add_string (buf, image->name); // fqname
5198 buffer_add_string (buf, mono_image_get_guid (image)); // guid
5199 buffer_add_assemblyid (buf, domain, image->assembly); // assembly
5204 return ERR_NOT_IMPLEMENTED;
5211 buffer_add_cattr_arg (Buffer *buf, MonoType *t, MonoDomain *domain, MonoObject *val)
5213 if (val && val->vtable->klass == mono_defaults.monotype_class) {
5214 /* Special case these so the client doesn't have to handle Type objects */
5216 buffer_add_byte (buf, VALUE_TYPE_ID_TYPE);
5217 buffer_add_typeid (buf, domain, mono_class_from_mono_type (((MonoReflectionType*)val)->type));
5218 } else if (MONO_TYPE_IS_REFERENCE (t))
5219 buffer_add_value (buf, t, &val, domain);
5221 buffer_add_value (buf, t, mono_object_unbox (val), domain);
5225 buffer_add_cattrs (Buffer *buf, MonoDomain *domain, MonoImage *image, MonoClass *attr_klass, MonoCustomAttrInfo *cinfo)
5231 buffer_add_int (buf, 0);
5235 for (i = 0; i < cinfo->num_attrs; ++i) {
5236 if (!attr_klass || mono_class_has_parent (cinfo->attrs [i].ctor->klass, attr_klass))
5239 buffer_add_int (buf, nattrs);
5241 for (i = 0; i < cinfo->num_attrs; ++i) {
5242 MonoCustomAttrEntry *attr = &cinfo->attrs [i];
5243 if (!attr_klass || mono_class_has_parent (attr->ctor->klass, attr_klass)) {
5244 MonoArray *typed_args, *named_args;
5246 CattrNamedArg *arginfo;
5248 mono_reflection_create_custom_attr_data_args (image, attr->ctor, attr->data, attr->data_size, &typed_args, &named_args, &arginfo);
5250 buffer_add_methodid (buf, domain, attr->ctor);
5254 buffer_add_int (buf, mono_array_length (typed_args));
5255 for (j = 0; j < mono_array_length (typed_args); ++j) {
5256 MonoObject *val = mono_array_get (typed_args, MonoObject*, j);
5258 t = mono_method_signature (attr->ctor)->params [j];
5260 buffer_add_cattr_arg (buf, t, domain, val);
5263 buffer_add_int (buf, 0);
5268 buffer_add_int (buf, mono_array_length (named_args));
5270 for (j = 0; j < mono_array_length (named_args); ++j) {
5271 MonoObject *val = mono_array_get (named_args, MonoObject*, j);
5273 if (arginfo [j].prop) {
5274 buffer_add_byte (buf, 0x54);
5275 buffer_add_propertyid (buf, domain, arginfo [j].prop);
5276 } else if (arginfo [j].field) {
5277 buffer_add_byte (buf, 0x53);
5279 g_assert_not_reached ();
5282 buffer_add_cattr_arg (buf, arginfo [j].type, domain, val);
5285 buffer_add_int (buf, 0);
5292 type_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5303 klass = decode_typeid (p, &p, end, &domain, &err);
5308 case CMD_TYPE_GET_INFO: {
5309 buffer_add_string (buf, klass->name_space);
5310 buffer_add_string (buf, klass->name);
5312 name = mono_type_get_name_full (&klass->byval_arg, MONO_TYPE_NAME_FORMAT_FULL_NAME);
5313 buffer_add_string (buf, name);
5315 buffer_add_assemblyid (buf, domain, klass->image->assembly);
5316 buffer_add_moduleid (buf, domain, klass->image);
5317 buffer_add_typeid (buf, domain, klass->parent);
5318 if (klass->rank || klass->byval_arg.type == MONO_TYPE_PTR)
5319 buffer_add_typeid (buf, domain, klass->element_class);
5321 buffer_add_id (buf, 0);
5322 buffer_add_int (buf, klass->type_token);
5323 buffer_add_byte (buf, klass->rank);
5324 buffer_add_int (buf, klass->flags);
5326 type = &klass->byval_arg;
5327 // FIXME: Can't decide whenever a class represents a byref type
5330 if (type->type == MONO_TYPE_PTR)
5332 if (!type->byref && (((type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8)) || (type->type == MONO_TYPE_I) || (type->type == MONO_TYPE_U)))
5334 if (type->type == MONO_TYPE_VALUETYPE)
5336 if (klass->enumtype)
5338 buffer_add_byte (buf, b);
5341 while ((nested = mono_class_get_nested_types (klass, &iter)))
5343 buffer_add_int (buf, nnested);
5345 while ((nested = mono_class_get_nested_types (klass, &iter)))
5346 buffer_add_typeid (buf, domain, nested);
5349 case CMD_TYPE_GET_METHODS: {
5352 gpointer iter = NULL;
5355 nmethods = mono_class_num_methods (klass);
5357 buffer_add_int (buf, nmethods);
5359 while ((m = mono_class_get_methods (klass, &iter))) {
5360 buffer_add_methodid (buf, domain, m);
5363 g_assert (i == nmethods);
5366 case CMD_TYPE_GET_FIELDS: {
5369 gpointer iter = NULL;
5372 nfields = mono_class_num_fields (klass);
5374 buffer_add_int (buf, nfields);
5376 while ((f = mono_class_get_fields (klass, &iter))) {
5377 buffer_add_fieldid (buf, domain, f);
5378 buffer_add_string (buf, f->name);
5379 buffer_add_typeid (buf, domain, mono_class_from_mono_type (f->type));
5380 buffer_add_int (buf, f->type->attrs);
5383 g_assert (i == nfields);
5386 case CMD_TYPE_GET_PROPERTIES: {
5389 gpointer iter = NULL;
5392 nprops = mono_class_num_properties (klass);
5394 buffer_add_int (buf, nprops);
5396 while ((p = mono_class_get_properties (klass, &iter))) {
5397 buffer_add_propertyid (buf, domain, p);
5398 buffer_add_string (buf, p->name);
5399 buffer_add_methodid (buf, domain, p->get);
5400 buffer_add_methodid (buf, domain, p->set);
5401 buffer_add_int (buf, p->attrs);
5404 g_assert (i == nprops);
5407 case CMD_TYPE_GET_CATTRS: {
5408 MonoClass *attr_klass = decode_typeid (p, &p, end, NULL, &err);
5409 MonoCustomAttrInfo *cinfo;
5411 cinfo = mono_custom_attrs_from_class (klass);
5413 buffer_add_cattrs (buf, domain, klass->image, attr_klass, cinfo);
5416 case CMD_TYPE_GET_FIELD_CATTRS: {
5417 MonoClass *attr_klass;
5418 MonoCustomAttrInfo *cinfo;
5419 MonoClassField *field;
5421 field = decode_fieldid (p, &p, end, NULL, &err);
5424 attr_klass = decode_typeid (p, &p, end, NULL, &err);
5428 cinfo = mono_custom_attrs_from_field (klass, field);
5430 buffer_add_cattrs (buf, domain, klass->image, attr_klass, cinfo);
5433 case CMD_TYPE_GET_PROPERTY_CATTRS: {
5434 MonoClass *attr_klass;
5435 MonoCustomAttrInfo *cinfo;
5438 prop = decode_propertyid (p, &p, end, NULL, &err);
5441 attr_klass = decode_typeid (p, &p, end, NULL, &err);
5445 cinfo = mono_custom_attrs_from_property (klass, prop);
5447 buffer_add_cattrs (buf, domain, klass->image, attr_klass, cinfo);
5450 case CMD_TYPE_GET_VALUES: {
5458 len = decode_int (p, &p, end);
5459 for (i = 0; i < len; ++i) {
5460 f = decode_fieldid (p, &p, end, NULL, &err);
5464 if (!(f->type->attrs & FIELD_ATTRIBUTE_STATIC))
5465 return ERR_INVALID_FIELDID;
5466 if (mono_class_field_is_special_static (f))
5467 return ERR_INVALID_FIELDID;
5469 /* Check that the field belongs to the object */
5471 for (k = klass; k; k = k->parent) {
5472 if (k == f->parent) {
5478 return ERR_INVALID_FIELDID;
5480 vtable = mono_class_vtable (domain, f->parent);
5481 val = g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
5482 mono_field_static_get_value (vtable, f, val);
5483 buffer_add_value (buf, f->type, val, domain);
5488 case CMD_TYPE_SET_VALUES: {
5496 len = decode_int (p, &p, end);
5497 for (i = 0; i < len; ++i) {
5498 f = decode_fieldid (p, &p, end, NULL, &err);
5502 if (!(f->type->attrs & FIELD_ATTRIBUTE_STATIC))
5503 return ERR_INVALID_FIELDID;
5504 if (mono_class_field_is_special_static (f))
5505 return ERR_INVALID_FIELDID;
5507 /* Check that the field belongs to the object */
5509 for (k = klass; k; k = k->parent) {
5510 if (k == f->parent) {
5516 return ERR_INVALID_FIELDID;
5518 // FIXME: Check for literal/const
5520 vtable = mono_class_vtable (domain, f->parent);
5521 val = g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
5522 err = decode_value (f->type, domain, val, p, &p, end);
5527 if (MONO_TYPE_IS_REFERENCE (f->type))
5528 mono_field_static_set_value (vtable, f, *(gpointer*)val);
5530 mono_field_static_set_value (vtable, f, val);
5535 case CMD_TYPE_GET_OBJECT: {
5536 MonoObject *o = (MonoObject*)mono_type_get_object (mono_domain_get (), &klass->byval_arg);
5537 buffer_add_objid (buf, o);
5540 case CMD_TYPE_GET_SOURCE_FILES: {
5541 gpointer iter = NULL;
5543 char *source_file, *base;
5547 files = g_ptr_array_new ();
5549 while ((method = mono_class_get_methods (klass, &iter))) {
5550 MonoDebugMethodInfo *minfo = mono_debug_lookup_method (method);
5553 mono_debug_symfile_get_line_numbers (minfo, &source_file, NULL, NULL, NULL);
5555 for (i = 0; i < files->len; ++i)
5556 if (!strcmp (g_ptr_array_index (files, i), source_file))
5558 if (i == files->len)
5559 g_ptr_array_add (files, g_strdup (source_file));
5560 g_free (source_file);
5564 buffer_add_int (buf, files->len);
5565 for (i = 0; i < files->len; ++i) {
5566 source_file = g_ptr_array_index (files, i);
5567 base = g_path_get_basename (source_file);
5568 buffer_add_string (buf, base);
5570 g_free (source_file);
5572 g_ptr_array_free (files, TRUE);
5575 case CMD_TYPE_IS_ASSIGNABLE_FROM: {
5576 MonoClass *oklass = decode_typeid (p, &p, end, NULL, &err);
5580 if (mono_class_is_assignable_from (klass, oklass))
5581 buffer_add_byte (buf, 1);
5583 buffer_add_byte (buf, 0);
5587 return ERR_NOT_IMPLEMENTED;
5594 method_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5599 MonoMethodHeader *header;
5601 method = decode_methodid (p, &p, end, &domain, &err);
5606 case CMD_METHOD_GET_NAME: {
5607 buffer_add_string (buf, method->name);
5610 case CMD_METHOD_GET_DECLARING_TYPE: {
5611 buffer_add_typeid (buf, domain, method->klass);
5614 case CMD_METHOD_GET_DEBUG_INFO: {
5615 MonoDebugMethodInfo *minfo;
5617 int i, n_il_offsets;
5621 header = mono_method_get_header (method);
5623 buffer_add_int (buf, 0);
5624 buffer_add_string (buf, "");
5625 buffer_add_int (buf, 0);
5629 minfo = mono_debug_lookup_method (method);
5631 buffer_add_int (buf, header->code_size);
5632 buffer_add_string (buf, "");
5633 buffer_add_int (buf, 0);
5634 mono_metadata_free_mh (header);
5638 mono_debug_symfile_get_line_numbers (minfo, &source_file, &n_il_offsets, &il_offsets, &line_numbers);
5639 buffer_add_int (buf, header->code_size);
5640 buffer_add_string (buf, source_file);
5641 buffer_add_int (buf, n_il_offsets);
5642 //printf ("Line number table for method %s:\n", mono_method_full_name (method, TRUE));
5643 for (i = 0; i < n_il_offsets; ++i) {
5644 //printf ("IL%d -> %d\n", il_offsets [i], line_numbers [i]);
5645 buffer_add_int (buf, il_offsets [i]);
5646 buffer_add_int (buf, line_numbers [i]);
5648 g_free (source_file);
5649 g_free (il_offsets);
5650 g_free (line_numbers);
5651 mono_metadata_free_mh (header);
5654 case CMD_METHOD_GET_PARAM_INFO: {
5655 MonoMethodSignature *sig = mono_method_signature (method);
5659 /* FIXME: mono_class_from_mono_type () and byrefs */
5661 /* FIXME: Use a smaller encoding */
5662 buffer_add_int (buf, sig->call_convention);
5663 buffer_add_int (buf, sig->param_count);
5664 buffer_add_int (buf, sig->generic_param_count);
5665 buffer_add_typeid (buf, domain, mono_class_from_mono_type (sig->ret));
5666 for (i = 0; i < sig->param_count; ++i) {
5668 buffer_add_typeid (buf, domain, mono_class_from_mono_type (sig->params [i]));
5671 /* Emit parameter names */
5672 names = g_new (char *, sig->param_count);
5673 mono_method_get_param_names (method, (const char **) names);
5674 for (i = 0; i < sig->param_count; ++i)
5675 buffer_add_string (buf, names [i]);
5680 case CMD_METHOD_GET_LOCALS_INFO: {
5681 int i, j, num_locals;
5685 header = mono_method_get_header (method);
5688 buffer_add_int (buf, header->num_locals);
5691 for (i = 0; i < header->num_locals; ++i)
5692 buffer_add_typeid (buf, domain, mono_class_from_mono_type (header->locals [i]));
5695 num_locals = mono_debug_lookup_locals (method, &local_names, &local_indexes);
5696 for (i = 0; i < header->num_locals; ++i) {
5697 for (j = 0; j < num_locals; ++j)
5698 if (local_indexes [j] == i)
5701 buffer_add_string (buf, local_names [j]);
5703 buffer_add_string (buf, "");
5705 g_free (local_names);
5706 g_free (local_indexes);
5709 /* FIXME: This works because we set debug_options.mdb_optimizations */
5710 for (i = 0; i < header->num_locals; ++i) {
5711 buffer_add_int (buf, 0);
5712 buffer_add_int (buf, header->code_size);
5714 mono_metadata_free_mh (header);
5718 case CMD_METHOD_GET_INFO:
5719 buffer_add_int (buf, method->flags);
5720 buffer_add_int (buf, method->iflags);
5721 buffer_add_int (buf, method->token);
5723 case CMD_METHOD_GET_BODY: {
5726 header = mono_method_get_header (method);
5728 buffer_add_int (buf, 0);
5730 buffer_add_int (buf, header->code_size);
5731 for (i = 0; i < header->code_size; ++i)
5732 buffer_add_byte (buf, header->code [i]);
5734 mono_metadata_free_mh (header);
5737 case CMD_METHOD_RESOLVE_TOKEN: {
5738 guint32 token = decode_int (p, &p, end);
5741 switch (mono_metadata_token_code (token)) {
5742 case MONO_TOKEN_STRING: {
5746 s = mono_ldstr (domain, method->klass->image, mono_metadata_token_index (token));
5749 s2 = mono_string_to_utf8 (s);
5751 buffer_add_byte (buf, TOKEN_TYPE_STRING);
5752 buffer_add_string (buf, s2);
5758 MonoClass *handle_class;
5760 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
5761 val = mono_method_get_wrapper_data (method, token);
5762 handle_class = mono_method_get_wrapper_data (method, token + 1);
5764 if (handle_class == NULL) {
5765 // Can't figure out the token type
5766 buffer_add_byte (buf, TOKEN_TYPE_UNKNOWN);
5770 val = mono_ldtoken (method->klass->image, token, &handle_class, NULL);
5774 if (handle_class == mono_defaults.typehandle_class) {
5775 buffer_add_byte (buf, TOKEN_TYPE_TYPE);
5776 buffer_add_typeid (buf, domain, mono_class_from_mono_type ((MonoType*)val));
5777 } else if (handle_class == mono_defaults.fieldhandle_class) {
5778 buffer_add_byte (buf, TOKEN_TYPE_FIELD);
5779 buffer_add_fieldid (buf, domain, val);
5780 } else if (handle_class == mono_defaults.methodhandle_class) {
5781 buffer_add_byte (buf, TOKEN_TYPE_METHOD);
5782 buffer_add_methodid (buf, domain, val);
5783 } else if (handle_class == mono_defaults.string_class) {
5786 s = mono_string_to_utf8 (val);
5787 buffer_add_byte (buf, TOKEN_TYPE_STRING);
5788 buffer_add_string (buf, s);
5791 g_assert_not_reached ();
5799 return ERR_NOT_IMPLEMENTED;
5806 thread_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5808 int objid = decode_objid (p, &p, end);
5810 MonoThread *thread_obj;
5811 MonoInternalThread *thread;
5813 err = get_object (objid, (MonoObject**)&thread_obj);
5817 thread = THREAD_TO_INTERNAL (thread_obj);
5820 case CMD_THREAD_GET_NAME: {
5822 gunichar2 *s = mono_thread_get_name (thread, &name_len);
5825 buffer_add_int (buf, 0);
5830 name = g_utf16_to_utf8 (s, name_len, NULL, &len, NULL);
5832 buffer_add_int (buf, len);
5833 buffer_add_data (buf, (guint8*)name, len);
5838 case CMD_THREAD_GET_FRAME_INFO: {
5839 DebuggerTlsData *tls;
5840 int i, start_frame, length;
5842 // Wait for suspending if it already started
5844 wait_for_suspend ();
5845 if (!is_suspended ())
5846 return ERR_NOT_SUSPENDED;
5848 start_frame = decode_int (p, &p, end);
5849 length = decode_int (p, &p, end);
5851 if (start_frame != 0 || length != -1)
5852 return ERR_NOT_IMPLEMENTED;
5854 mono_loader_lock ();
5855 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
5856 mono_loader_unlock ();
5859 compute_frame_info (thread, tls);
5861 buffer_add_int (buf, tls->frame_count);
5862 for (i = 0; i < tls->frame_count; ++i) {
5863 buffer_add_int (buf, tls->frames [i]->id);
5864 buffer_add_methodid (buf, tls->frames [i]->domain, tls->frames [i]->method);
5865 buffer_add_int (buf, tls->frames [i]->il_offset);
5867 * Instead of passing the frame type directly to the client, we associate
5868 * it with the previous frame using a set of flags. This avoids lots of
5869 * conditional code in the client, since a frame whose type isn't
5870 * FRAME_TYPE_MANAGED has no method, location, etc.
5872 buffer_add_byte (buf, tls->frames [i]->flags);
5877 case CMD_THREAD_GET_STATE:
5878 buffer_add_int (buf, thread->state);
5880 case CMD_THREAD_GET_INFO:
5881 buffer_add_byte (buf, thread->threadpool_thread);
5884 return ERR_NOT_IMPLEMENTED;
5891 frame_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5895 MonoThread *thread_obj;
5896 MonoInternalThread *thread;
5898 DebuggerTlsData *tls;
5900 MonoDebugMethodJitInfo *jit;
5901 MonoDebugVarInfo *var;
5902 MonoMethodSignature *sig;
5904 MonoMethodHeader *header;
5906 objid = decode_objid (p, &p, end);
5907 err = get_object (objid, (MonoObject**)&thread_obj);
5911 thread = THREAD_TO_INTERNAL (thread_obj);
5913 id = decode_id (p, &p, end);
5915 mono_loader_lock ();
5916 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
5917 mono_loader_unlock ();
5920 for (i = 0; i < tls->frame_count; ++i) {
5921 if (tls->frames [i]->id == id)
5924 if (i == tls->frame_count)
5925 return ERR_INVALID_FRAMEID;
5927 frame = tls->frames [i];
5929 if (!frame->has_ctx)
5931 return ERR_INVALID_FRAMEID;
5934 frame->jit = mono_debug_find_method (frame->method, frame->domain);
5935 g_assert (frame->jit);
5939 sig = mono_method_signature (frame->method);
5942 case CMD_STACK_FRAME_GET_VALUES: {
5943 len = decode_int (p, &p, end);
5944 header = mono_method_get_header (frame->method);
5946 for (i = 0; i < len; ++i) {
5947 pos = decode_int (p, &p, end);
5952 g_assert (pos >= 0 && pos < jit->num_params);
5954 var = &jit->params [pos];
5956 add_var (buf, sig->params [pos], &jit->params [pos], &frame->ctx, frame->domain, FALSE);
5958 g_assert (pos >= 0 && pos < jit->num_locals);
5960 var = &jit->locals [pos];
5962 add_var (buf, header->locals [pos], &jit->locals [pos], &frame->ctx, frame->domain, FALSE);
5965 mono_metadata_free_mh (header);
5968 case CMD_STACK_FRAME_GET_THIS: {
5969 if (frame->method->klass->valuetype) {
5970 if (!sig->hasthis) {
5971 MonoObject *p = NULL;
5972 buffer_add_value (buf, &mono_defaults.object_class->byval_arg, &p, frame->domain);
5974 add_var (buf, &frame->method->klass->this_arg, jit->this_var, &frame->ctx, frame->domain, TRUE);
5977 if (!sig->hasthis) {
5978 MonoObject *p = NULL;
5979 buffer_add_value (buf, &frame->method->klass->byval_arg, &p, frame->domain);
5981 add_var (buf, &frame->method->klass->byval_arg, jit->this_var, &frame->ctx, frame->domain, TRUE);
5986 case CMD_STACK_FRAME_SET_VALUES: {
5989 MonoDebugVarInfo *var;
5991 len = decode_int (p, &p, end);
5992 header = mono_method_get_header (frame->method);
5994 for (i = 0; i < len; ++i) {
5995 pos = decode_int (p, &p, end);
6000 g_assert (pos >= 0 && pos < jit->num_params);
6002 t = sig->params [pos];
6003 var = &jit->params [pos];
6005 g_assert (pos >= 0 && pos < jit->num_locals);
6007 t = header->locals [pos];
6008 var = &jit->locals [pos];
6011 if (MONO_TYPE_IS_REFERENCE (t))
6012 val_buf = g_alloca (sizeof (MonoObject*));
6014 val_buf = g_alloca (mono_class_instance_size (mono_class_from_mono_type (t)));
6015 err = decode_value (t, frame->domain, val_buf, p, &p, end);
6019 set_var (t, var, &frame->ctx, frame->domain, val_buf);
6021 mono_metadata_free_mh (header);
6025 return ERR_NOT_IMPLEMENTED;
6032 array_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
6035 int objid, err, index, len, i, esize;
6038 objid = decode_objid (p, &p, end);
6039 err = get_object (objid, (MonoObject**)&arr);
6044 case CMD_ARRAY_REF_GET_LENGTH:
6045 buffer_add_int (buf, arr->obj.vtable->klass->rank);
6047 buffer_add_int (buf, arr->max_length);
6048 buffer_add_int (buf, 0);
6050 for (i = 0; i < arr->obj.vtable->klass->rank; ++i) {
6051 buffer_add_int (buf, arr->bounds [i].length);
6052 buffer_add_int (buf, arr->bounds [i].lower_bound);
6056 case CMD_ARRAY_REF_GET_VALUES:
6057 index = decode_int (p, &p, end);
6058 len = decode_int (p, &p, end);
6060 g_assert (index >= 0 && len >= 0);
6061 // Reordered to avoid integer overflow
6062 g_assert (!(index > arr->max_length - len));
6064 esize = mono_array_element_size (arr->obj.vtable->klass);
6065 for (i = index; i < index + len; ++i) {
6066 elem = (gpointer*)((char*)arr->vector + (i * esize));
6067 buffer_add_value (buf, &arr->obj.vtable->klass->element_class->byval_arg, elem, arr->obj.vtable->domain);
6070 case CMD_ARRAY_REF_SET_VALUES:
6071 index = decode_int (p, &p, end);
6072 len = decode_int (p, &p, end);
6074 g_assert (index >= 0 && len >= 0);
6075 // Reordered to avoid integer overflow
6076 g_assert (!(index > arr->max_length - len));
6078 esize = mono_array_element_size (arr->obj.vtable->klass);
6079 for (i = index; i < index + len; ++i) {
6080 elem = (gpointer*)((char*)arr->vector + (i * esize));
6082 decode_value (&arr->obj.vtable->klass->element_class->byval_arg, arr->obj.vtable->domain, elem, p, &p, end);
6086 return ERR_NOT_IMPLEMENTED;
6093 string_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
6099 objid = decode_objid (p, &p, end);
6100 err = get_object (objid, (MonoObject**)&str);
6105 case CMD_STRING_REF_GET_VALUE:
6106 s = mono_string_to_utf8 (str);
6107 buffer_add_string (buf, s);
6111 return ERR_NOT_IMPLEMENTED;
6118 object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
6127 if (command == CMD_OBJECT_REF_IS_COLLECTED) {
6128 objid = decode_objid (p, &p, end);
6129 err = get_object (objid, &obj);
6131 buffer_add_int (buf, 1);
6133 buffer_add_int (buf, 0);
6137 objid = decode_objid (p, &p, end);
6138 err = get_object (objid, &obj);
6143 case CMD_OBJECT_REF_GET_TYPE:
6144 buffer_add_typeid (buf, obj->vtable->domain, obj->vtable->klass);
6146 case CMD_OBJECT_REF_GET_VALUES:
6147 len = decode_int (p, &p, end);
6149 for (i = 0; i < len; ++i) {
6150 MonoClassField *f = decode_fieldid (p, &p, end, NULL, &err);
6154 /* Check that the field belongs to the object */
6156 for (k = obj->vtable->klass; k; k = k->parent) {
6157 if (k == f->parent) {
6163 return ERR_INVALID_FIELDID;
6165 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC) {
6169 if (mono_class_field_is_special_static (f))
6170 return ERR_INVALID_FIELDID;
6172 g_assert (f->type->attrs & FIELD_ATTRIBUTE_STATIC);
6173 vtable = mono_class_vtable (obj->vtable->domain, f->parent);
6174 val = g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
6175 mono_field_static_get_value (vtable, f, val);
6176 buffer_add_value (buf, f->type, val, obj->vtable->domain);
6179 buffer_add_value (buf, f->type, (guint8*)obj + f->offset, obj->vtable->domain);
6183 case CMD_OBJECT_REF_SET_VALUES:
6184 len = decode_int (p, &p, end);
6186 for (i = 0; i < len; ++i) {
6187 f = decode_fieldid (p, &p, end, NULL, &err);
6191 /* Check that the field belongs to the object */
6193 for (k = obj->vtable->klass; k; k = k->parent) {
6194 if (k == f->parent) {
6200 return ERR_INVALID_FIELDID;
6202 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC) {
6206 if (mono_class_field_is_special_static (f))
6207 return ERR_INVALID_FIELDID;
6209 g_assert (f->type->attrs & FIELD_ATTRIBUTE_STATIC);
6210 vtable = mono_class_vtable (obj->vtable->domain, f->parent);
6212 val = g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
6213 err = decode_value (f->type, obj->vtable->domain, val, p, &p, end);
6218 mono_field_static_set_value (vtable, f, val);
6221 err = decode_value (f->type, obj->vtable->domain, (guint8*)obj + f->offset, p, &p, end);
6227 case CMD_OBJECT_REF_GET_ADDRESS:
6228 buffer_add_long (buf, (gssize)obj);
6230 case CMD_OBJECT_REF_GET_DOMAIN:
6231 buffer_add_domainid (buf, obj->vtable->domain);
6234 return ERR_NOT_IMPLEMENTED;
6241 command_set_to_string (CommandSet command_set)
6243 switch (command_set) {
6246 case CMD_SET_OBJECT_REF:
6247 return "OBJECT_REF";
6248 case CMD_SET_STRING_REF:
6249 return "STRING_REF";
6250 case CMD_SET_THREAD:
6252 case CMD_SET_ARRAY_REF:
6254 case CMD_SET_EVENT_REQUEST:
6255 return "EVENT_REQUEST";
6256 case CMD_SET_STACK_FRAME:
6257 return "STACK_FRAME";
6258 case CMD_SET_APPDOMAIN:
6260 case CMD_SET_ASSEMBLY:
6262 case CMD_SET_METHOD:
6266 case CMD_SET_MODULE:
6278 * This thread handles communication with the debugger client using a JDWP
6281 static guint32 WINAPI
6282 debugger_thread (void *arg)
6284 int res, len, id, flags, command_set, command;
6285 guint8 header [HEADER_LENGTH];
6286 guint8 *data, *p, *end;
6291 DEBUG (1, fprintf (log_file, "[dbg] Agent thread started, pid=%p\n", (gpointer)GetCurrentThreadId ()));
6293 debugger_thread_id = GetCurrentThreadId ();
6295 mono_jit_thread_attach (mono_get_root_domain ());
6297 mono_thread_internal_current ()->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
6299 mono_set_is_debugger_attached (TRUE);
6302 res = recv_length (conn_fd, header, HEADER_LENGTH, 0);
6304 /* This will break if the socket is closed during shutdown too */
6305 if (res != HEADER_LENGTH)
6309 end = header + HEADER_LENGTH;
6311 len = decode_int (p, &p, end);
6312 id = decode_int (p, &p, end);
6313 flags = decode_byte (p, &p, end);
6314 command_set = decode_byte (p, &p, end);
6315 command = decode_byte (p, &p, end);
6317 g_assert (flags == 0);
6319 DEBUG (1, fprintf (log_file, "[dbg] Received command %s(%d), id=%d.\n", command_set_to_string (command_set), command, id));
6321 data = g_malloc (len - HEADER_LENGTH);
6322 if (len - HEADER_LENGTH > 0)
6324 res = recv_length (conn_fd, data, len - HEADER_LENGTH, 0);
6325 if (res != len - HEADER_LENGTH)
6330 end = data + (len - HEADER_LENGTH);
6332 buffer_init (&buf, 128);
6337 /* Process the request */
6338 switch (command_set) {
6340 err = vm_commands (command, id, p, end, &buf);
6341 if (!err && command == CMD_VM_INVOKE_METHOD)
6342 /* Sent after the invoke is complete */
6345 case CMD_SET_EVENT_REQUEST:
6346 err = event_commands (command, p, end, &buf);
6348 case CMD_SET_APPDOMAIN:
6349 err = domain_commands (command, p, end, &buf);
6351 case CMD_SET_ASSEMBLY:
6352 err = assembly_commands (command, p, end, &buf);
6354 case CMD_SET_MODULE:
6355 err = module_commands (command, p, end, &buf);
6358 err = type_commands (command, p, end, &buf);
6360 case CMD_SET_METHOD:
6361 err = method_commands (command, p, end, &buf);
6363 case CMD_SET_THREAD:
6364 err = thread_commands (command, p, end, &buf);
6366 case CMD_SET_STACK_FRAME:
6367 err = frame_commands (command, p, end, &buf);
6369 case CMD_SET_ARRAY_REF:
6370 err = array_commands (command, p, end, &buf);
6372 case CMD_SET_STRING_REF:
6373 err = string_commands (command, p, end, &buf);
6375 case CMD_SET_OBJECT_REF:
6376 err = object_commands (command, p, end, &buf);
6379 err = ERR_NOT_IMPLEMENTED;
6383 send_reply_packet (id, err, &buf);
6388 if (command_set == CMD_SET_VM && command == CMD_VM_DISPOSE)
6392 mono_set_is_debugger_attached (FALSE);
6394 mono_mutex_lock (&debugger_thread_exited_mutex);
6395 debugger_thread_exited = TRUE;
6396 mono_cond_signal (&debugger_thread_exited_cond);
6397 mono_mutex_unlock (&debugger_thread_exited_mutex);
6399 DEBUG (1, printf ("[dbg] Debugger thread exited.\n"));
6404 #else /* DISABLE_DEBUGGER_AGENT */
6407 mono_debugger_agent_parse_options (char *options)
6409 g_error ("This runtime is configure with the debugger agent disabled.");
6413 mono_debugger_agent_init (void)
6418 mono_debugger_agent_breakpoint_hit (void *sigctx)
6423 mono_debugger_agent_single_step_event (void *sigctx)
6428 mono_debugger_agent_free_domain_info (MonoDomain *domain)
6433 mono_debugger_agent_thread_interrupt (void *sigctx, MonoJitInfo *ji)
6439 mono_debugger_agent_handle_exception (MonoException *ext, MonoContext *throw_ctx,
6440 MonoContext *catch_ctx)