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
43 #ifdef PLATFORM_ANDROID
45 #include <linux/tcp.h>
46 #include <sys/endian.h>
49 #include <mono/metadata/mono-debug.h>
50 #include <mono/metadata/mono-debug-debugger.h>
51 #include <mono/metadata/debug-mono-symfile.h>
52 #include <mono/metadata/gc-internal.h>
53 #include <mono/metadata/threads-types.h>
54 #include <mono/metadata/socket-io.h>
55 #include <mono/utils/mono-semaphore.h>
56 #include "debugger-agent.h"
59 #ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
60 #define DISABLE_DEBUGGER_AGENT 1
63 #ifdef DISABLE_SOFT_DEBUG
64 #define DISABLE_DEBUGGER_AGENT 1
67 #ifndef DISABLE_DEBUGGER_AGENT
68 #include <mono/io-layer/mono-mutex.h>
70 /* Definitions to make backporting to 2.6 easier */
71 //#define MonoInternalThread MonoThread
72 //#define mono_thread_internal_current mono_thread_current
73 #define THREAD_TO_INTERNAL(thread) (thread)->internal_thread
96 MonoDebugMethodJitInfo *jit;
106 /* This is the context which needs to be restored after the invoke */
115 gboolean has_context;
116 gpointer resume_event;
117 /* This is computed on demand when it is requested using the wire protocol */
118 /* It is freed up when the thread is resumed */
122 * Whenever the frame info is up-to-date. If not, compute_frame_info () will need to
125 gboolean frames_up_to_date;
127 * Points to data about a pending invoke which needs to be executed after the thread
132 * Set to TRUE if this thread is suspended in suspend_current () or it is executing
137 * Set to TRUE if this thread is suspended in suspend_current ().
139 gboolean really_suspended;
140 /* Used to pass the context to the breakpoint/single step handler */
141 MonoContext handler_ctx;
142 /* Whenever thread_stop () was called for this thread */
145 /* Number of thread interruptions not yet processed */
146 gint32 interrupt_count;
148 /* Whenever to disable breakpoints (used during invokes) */
149 gboolean disable_breakpoints;
153 * Wire Protocol definitions
156 #define HEADER_LENGTH 11
158 #define MAJOR_VERSION 2
159 #define MINOR_VERSION 0
163 CMD_SET_OBJECT_REF = 9,
164 CMD_SET_STRING_REF = 10,
166 CMD_SET_ARRAY_REF = 13,
167 CMD_SET_EVENT_REQUEST = 15,
168 CMD_SET_STACK_FRAME = 16,
169 CMD_SET_APPDOMAIN = 20,
170 CMD_SET_ASSEMBLY = 21,
178 EVENT_KIND_VM_START = 0,
179 EVENT_KIND_VM_DEATH = 1,
180 EVENT_KIND_THREAD_START = 2,
181 EVENT_KIND_THREAD_DEATH = 3,
182 EVENT_KIND_APPDOMAIN_CREATE = 4,
183 EVENT_KIND_APPDOMAIN_UNLOAD = 5,
184 EVENT_KIND_METHOD_ENTRY = 6,
185 EVENT_KIND_METHOD_EXIT = 7,
186 EVENT_KIND_ASSEMBLY_LOAD = 8,
187 EVENT_KIND_ASSEMBLY_UNLOAD = 9,
188 EVENT_KIND_BREAKPOINT = 10,
189 EVENT_KIND_STEP = 11,
190 EVENT_KIND_TYPE_LOAD = 12,
191 EVENT_KIND_EXCEPTION = 13
195 SUSPEND_POLICY_NONE = 0,
196 SUSPEND_POLICY_EVENT_THREAD = 1,
197 SUSPEND_POLICY_ALL = 2
202 ERR_INVALID_OBJECT = 20,
203 ERR_INVALID_FIELDID = 25,
204 ERR_INVALID_FRAMEID = 30,
205 ERR_NOT_IMPLEMENTED = 100,
206 ERR_NOT_SUSPENDED = 101,
207 ERR_INVALID_ARGUMENT = 102,
213 MOD_KIND_THREAD_ONLY = 3,
214 MOD_KIND_LOCATION_ONLY = 7,
215 MOD_KIND_EXCEPTION_ONLY = 8,
217 MOD_KIND_ASSEMBLY_ONLY = 11
232 TOKEN_TYPE_STRING = 0,
234 TOKEN_TYPE_FIELD = 2,
235 TOKEN_TYPE_METHOD = 3,
236 TOKEN_TYPE_UNKNOWN = 4
240 VALUE_TYPE_ID_NULL = 0xf0,
241 VALUE_TYPE_ID_TYPE = 0xf1
245 FRAME_FLAG_DEBUGGER_INVOKE = 1
249 INVOKE_FLAG_DISABLE_BPS = 1
254 CMD_VM_ALL_THREADS = 2,
259 CMD_VM_INVOKE_METHOD = 7
263 CMD_THREAD_GET_FRAME_INFO = 1,
264 CMD_THREAD_GET_NAME = 2,
265 CMD_THREAD_GET_STATE = 3,
266 CMD_THREAD_GET_INFO = 4
270 CMD_EVENT_REQUEST_SET = 1,
271 CMD_EVENT_REQUEST_CLEAR = 2,
272 CMD_EVENT_REQUEST_CLEAR_ALL_BREAKPOINTS = 3
280 CMD_APPDOMAIN_GET_ROOT_DOMAIN = 1,
281 CMD_APPDOMAIN_GET_FRIENDLY_NAME = 2,
282 CMD_APPDOMAIN_GET_ASSEMBLIES = 3,
283 CMD_APPDOMAIN_GET_ENTRY_ASSEMBLY = 4,
284 CMD_APPDOMAIN_CREATE_STRING = 5,
285 CMD_APPDOMAIN_GET_CORLIB = 6
289 CMD_ASSEMBLY_GET_LOCATION = 1,
290 CMD_ASSEMBLY_GET_ENTRY_POINT = 2,
291 CMD_ASSEMBLY_GET_MANIFEST_MODULE = 3,
292 CMD_ASSEMBLY_GET_OBJECT = 4,
293 CMD_ASSEMBLY_GET_TYPE = 5,
294 CMD_ASSEMBLY_GET_NAME = 6
298 CMD_MODULE_GET_INFO = 1,
302 CMD_METHOD_GET_NAME = 1,
303 CMD_METHOD_GET_DECLARING_TYPE = 2,
304 CMD_METHOD_GET_DEBUG_INFO = 3,
305 CMD_METHOD_GET_PARAM_INFO = 4,
306 CMD_METHOD_GET_LOCALS_INFO = 5,
307 CMD_METHOD_GET_INFO = 6,
308 CMD_METHOD_GET_BODY = 7,
309 CMD_METHOD_RESOLVE_TOKEN = 8,
313 CMD_TYPE_GET_INFO = 1,
314 CMD_TYPE_GET_METHODS = 2,
315 CMD_TYPE_GET_FIELDS = 3,
316 CMD_TYPE_GET_VALUES = 4,
317 CMD_TYPE_GET_OBJECT = 5,
318 CMD_TYPE_GET_SOURCE_FILES = 6,
319 CMD_TYPE_SET_VALUES = 7,
320 CMD_TYPE_IS_ASSIGNABLE_FROM = 8,
321 CMD_TYPE_GET_PROPERTIES = 9,
322 CMD_TYPE_GET_CATTRS = 10,
323 CMD_TYPE_GET_FIELD_CATTRS = 11,
324 CMD_TYPE_GET_PROPERTY_CATTRS = 12
328 CMD_STACK_FRAME_GET_VALUES = 1,
329 CMD_STACK_FRAME_GET_THIS = 2,
330 CMD_STACK_FRAME_SET_VALUES = 3
334 CMD_ARRAY_REF_GET_LENGTH = 1,
335 CMD_ARRAY_REF_GET_VALUES = 2,
336 CMD_ARRAY_REF_SET_VALUES = 3,
340 CMD_STRING_REF_GET_VALUE = 1,
344 CMD_OBJECT_REF_GET_TYPE = 1,
345 CMD_OBJECT_REF_GET_VALUES = 2,
346 CMD_OBJECT_REF_IS_COLLECTED = 3,
347 CMD_OBJECT_REF_GET_ADDRESS = 4,
348 CMD_OBJECT_REF_GET_DOMAIN = 5,
349 CMD_OBJECT_REF_SET_VALUES = 6
355 int count; /* For kind == MOD_KIND_COUNT */
356 MonoInternalThread *thread; /* For kind == MOD_KIND_THREAD_ONLY */
357 MonoClass *exc_class; /* For kind == MONO_KIND_EXCEPTION_ONLY */
358 MonoAssembly **assemblies; /* For kind == MONO_KIND_ASSEMBLY_ONLY */
368 Modifier modifiers [MONO_ZERO_LEN_ARRAY];
372 * Describes a single step request.
376 MonoInternalThread *thread;
381 MonoMethod *last_method;
385 /* Dummy structure used for the profiler callbacks */
390 #define DEBUG(level,s) do { if (G_UNLIKELY ((level) <= log_level)) { s; fflush (log_file); } } while (0)
396 static AgentConfig agent_config;
399 * Whenever the agent is fully initialized.
400 * When using the onuncaught or onthrow options, only some parts of the agent are
401 * initialized on startup, and the full initialization which includes connection
402 * establishment and the startup of the agent thread is only done in response to
405 static gint32 inited;
409 static int packet_id = 0;
411 static int objref_id = 0;
413 static int event_request_id = 0;
415 static int frame_id = 0;
417 static GPtrArray *event_requests;
419 static guint32 debugger_tls_id;
421 static gboolean vm_start_event_sent, vm_death_event_sent, disconnected;
423 /* Maps MonoInternalThread -> DebuggerTlsData */
424 static MonoGHashTable *thread_to_tls;
426 /* Maps tid -> MonoInternalThread */
427 static MonoGHashTable *tid_to_thread;
429 /* Maps tid -> MonoThread (not MonoInternalThread) */
430 static MonoGHashTable *tid_to_thread_obj;
432 static gsize debugger_thread_id;
434 static HANDLE debugger_thread_handle;
436 static int log_level;
438 static FILE *log_file;
440 /* Classes whose class load event has been sent */
441 static GHashTable *loaded_classes;
443 /* Assemblies whose assembly load event has no been sent yet */
444 static GPtrArray *pending_assembly_loads;
446 /* Whenever the debugger thread has exited */
447 static gboolean debugger_thread_exited;
449 /* Cond variable used to wait for debugger_thread_exited becoming true */
450 static mono_cond_t debugger_thread_exited_cond;
452 /* Mutex for the cond var above */
453 static mono_mutex_t debugger_thread_exited_mutex;
455 static DebuggerProfiler debugger_profiler;
457 /* The single step request instance */
458 static MonoSingleStepReq *ss_req = NULL;
460 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
461 /* Number of single stepping operations in progress */
465 static void transport_connect (const char *host, int port);
467 static guint32 WINAPI debugger_thread (void *arg);
469 static void runtime_initialized (MonoProfiler *prof);
471 static void runtime_shutdown (MonoProfiler *prof);
473 static void thread_startup (MonoProfiler *prof, gsize tid);
475 static void thread_end (MonoProfiler *prof, gsize tid);
477 static void appdomain_load (MonoProfiler *prof, MonoDomain *domain, int result);
479 static void appdomain_unload (MonoProfiler *prof, MonoDomain *domain);
481 static void assembly_load (MonoProfiler *prof, MonoAssembly *assembly, int result);
483 static void assembly_unload (MonoProfiler *prof, MonoAssembly *assembly);
485 static void start_runtime_invoke (MonoProfiler *prof, MonoMethod *method);
487 static void end_runtime_invoke (MonoProfiler *prof, MonoMethod *method);
489 static void jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result);
491 static void add_pending_breakpoints (MonoMethod *method, MonoJitInfo *jinfo);
493 static void start_single_stepping (void);
495 static void stop_single_stepping (void);
497 static void suspend_current (void);
499 /* Submodule init/cleanup */
500 static void breakpoints_init (void);
501 static void breakpoints_cleanup (void);
503 static void objrefs_init (void);
504 static void objrefs_cleanup (void);
506 static void ids_init (void);
507 static void ids_cleanup (void);
509 static void suspend_init (void);
511 static ErrorCode ss_start (MonoInternalThread *thread, StepSize size, StepDepth depth, EventRequest *req);
512 static void ss_stop (EventRequest *req);
514 static void start_debugger_thread (void);
516 static void finish_agent_init (gboolean on_startup);
519 parse_address (char *address, char **host, int *port)
521 char *pos = strchr (address, ':');
523 if (pos == NULL || pos == address)
526 *host = g_malloc (pos - address + 1);
527 strncpy (*host, address, pos - address);
528 (*host) [pos - address] = '\0';
530 *port = atoi (pos + 1);
538 fprintf (stderr, "Usage: mono --debugger-agent=[<option>=<value>,...] ...\n");
539 fprintf (stderr, "Available options:\n");
540 fprintf (stderr, " transport=<transport>\t\tTransport to use for connecting to the debugger (mandatory, possible values: 'dt_socket')\n");
541 fprintf (stderr, " address=<hostname>:<port>\tAddress to connect to (mandatory)\n");
542 fprintf (stderr, " loglevel=<n>\t\t\tLog level (defaults to 0)\n");
543 fprintf (stderr, " logfile=<file>\t\tFile to log to (defaults to stdout)\n");
544 fprintf (stderr, " suspend=y/n\t\t\tWhenever to suspend after startup.\n");
545 fprintf (stderr, " timeout=<n>\t\t\tTimeout for connecting in milliseconds.\n");
546 fprintf (stderr, " help\t\t\t\tPrint this help.\n");
550 parse_flag (const char *option, char *flag)
552 if (!strcmp (flag, "y"))
554 else if (!strcmp (flag, "n"))
557 fprintf (stderr, "debugger-agent: The valid values for the '%s' option are 'y' and 'n'.\n", option);
564 mono_debugger_agent_parse_options (char *options)
570 #ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
571 fprintf (stderr, "--debugger-agent is not supported on this platform.\n");
575 agent_config.enabled = TRUE;
576 agent_config.suspend = TRUE;
577 agent_config.server = FALSE;
579 args = g_strsplit (options, ",", -1);
580 for (ptr = args; ptr && *ptr; ptr ++) {
583 if (strncmp (arg, "transport=", 10) == 0) {
584 agent_config.transport = g_strdup (arg + 10);
585 } else if (strncmp (arg, "address=", 8) == 0) {
586 agent_config.address = g_strdup (arg + 8);
587 } else if (strncmp (arg, "loglevel=", 9) == 0) {
588 agent_config.log_level = atoi (arg + 9);
589 } else if (strncmp (arg, "logfile=", 8) == 0) {
590 agent_config.log_file = g_strdup (arg + 8);
591 } else if (strncmp (arg, "suspend=", 8) == 0) {
592 agent_config.suspend = parse_flag ("suspend", arg + 8);
593 } else if (strncmp (arg, "server=", 7) == 0) {
594 agent_config.server = parse_flag ("server", arg + 7);
595 } else if (strncmp (arg, "onuncaught=", 11) == 0) {
596 agent_config.onuncaught = parse_flag ("onuncaught", arg + 11);
597 } else if (strncmp (arg, "onthrow=", 8) == 0) {
598 /* We support multiple onthrow= options */
599 agent_config.onthrow = g_slist_append (agent_config.onthrow, g_strdup (arg + 8));
600 } else if (strncmp (arg, "help", 4) == 0) {
603 } else if (strncmp (arg, "timeout=", 8) == 0) {
604 agent_config.timeout = atoi (arg + 8);
605 } else if (strncmp (arg, "launch=", 7) == 0) {
606 agent_config.launch = g_strdup (arg + 7);
613 if (agent_config.transport == NULL) {
614 fprintf (stderr, "debugger-agent: The 'transport' option is mandatory.\n");
617 if (strcmp (agent_config.transport, "dt_socket") != 0) {
618 fprintf (stderr, "debugger-agent: The only supported value for the 'transport' option is 'dt_socket'.\n");
622 if (agent_config.address == NULL && !agent_config.server) {
623 fprintf (stderr, "debugger-agent: The 'address' option is mandatory.\n");
627 if (agent_config.address && parse_address (agent_config.address, &host, &port)) {
628 fprintf (stderr, "debugger-agent: The format of the 'address' options is '<host>:<port>'\n");
634 mono_debugger_agent_init (void)
636 if (!agent_config.enabled)
639 /* Need to know whenever a thread has acquired the loader mutex */
640 mono_loader_lock_track_ownership (TRUE);
642 event_requests = g_ptr_array_new ();
644 mono_mutex_init (&debugger_thread_exited_mutex, NULL);
645 mono_cond_init (&debugger_thread_exited_cond, NULL);
647 mono_profiler_install ((MonoProfiler*)&debugger_profiler, runtime_shutdown);
648 mono_profiler_set_events (MONO_PROFILE_APPDOMAIN_EVENTS | MONO_PROFILE_THREADS | MONO_PROFILE_ASSEMBLY_EVENTS | MONO_PROFILE_JIT_COMPILATION | MONO_PROFILE_METHOD_EVENTS);
649 mono_profiler_install_runtime_initialized (runtime_initialized);
650 mono_profiler_install_appdomain (NULL, appdomain_load, appdomain_unload, NULL);
651 mono_profiler_install_thread (thread_startup, thread_end);
652 mono_profiler_install_assembly (NULL, assembly_load, assembly_unload, NULL);
653 mono_profiler_install_jit_end (jit_end);
654 mono_profiler_install_method_invoke (start_runtime_invoke, end_runtime_invoke);
656 debugger_tls_id = TlsAlloc ();
658 thread_to_tls = mono_g_hash_table_new (NULL, NULL);
659 MONO_GC_REGISTER_ROOT (thread_to_tls);
661 tid_to_thread = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC);
662 MONO_GC_REGISTER_ROOT (tid_to_thread);
664 tid_to_thread_obj = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC);
665 MONO_GC_REGISTER_ROOT (tid_to_thread_obj);
667 loaded_classes = g_hash_table_new (mono_aligned_addr_hash, NULL);
668 pending_assembly_loads = g_ptr_array_new ();
670 log_level = agent_config.log_level;
672 if (agent_config.log_file) {
673 log_file = fopen (agent_config.log_file, "w+");
675 fprintf (stderr, "Unable to create log file '%s': %s.\n", agent_config.log_file, strerror (errno));
687 mini_get_debug_options ()->gen_seq_points = TRUE;
689 * This is needed because currently we don't handle liveness info.
691 mini_get_debug_options ()->mdb_optimizations = TRUE;
693 /* This is needed because we can't set local variables in registers yet */
694 mono_disable_optimizations (MONO_OPT_LINEARS);
696 if (!agent_config.onuncaught && !agent_config.onthrow)
697 finish_agent_init (TRUE);
703 * Finish the initialization of the agent. This involves connecting the transport
704 * and starting the agent thread. This is either done at startup, or
705 * in response to some event like an unhandled exception.
708 finish_agent_init (gboolean on_startup)
714 if (InterlockedCompareExchange (&inited, 1, 0) == 1)
717 if (agent_config.launch) {
720 // FIXME: Generated address
721 // FIXME: Races with transport_connect ()
723 argv [0] = agent_config.launch;
724 argv [1] = agent_config.transport;
725 argv [2] = agent_config.address;
728 res = g_spawn_async_with_pipes (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
730 fprintf (stderr, "Failed to execute '%s'.\n", agent_config.launch);
735 if (agent_config.address) {
736 res = parse_address (agent_config.address, &host, &port);
743 transport_connect (host, port);
746 /* Do some which is usually done after sending the VMStart () event */
747 vm_start_event_sent = TRUE;
748 start_debugger_thread ();
753 mono_debugger_agent_cleanup (void)
758 /* This will interrupt the agent thread */
759 /* Close the read part only so it can still send back replies */
761 shutdown (conn_fd, SD_RECEIVE);
763 shutdown (conn_fd, SHUT_RD);
767 * Wait for the thread to exit.
769 * If we continue with the shutdown without waiting for it, then the client might
770 * not receive an answer to its last command like a resume.
771 * The WaitForSingleObject infrastructure doesn't seem to work during shutdown, so
774 //WaitForSingleObject (debugger_thread_handle, INFINITE);
775 if (GetCurrentThreadId () != debugger_thread_id) {
776 mono_mutex_lock (&debugger_thread_exited_mutex);
777 if (!debugger_thread_exited)
778 mono_cond_wait (&debugger_thread_exited_cond, &debugger_thread_exited_mutex);
779 mono_mutex_unlock (&debugger_thread_exited_mutex);
782 breakpoints_cleanup ();
787 mono_mutex_destroy (&debugger_thread_exited_mutex);
788 mono_cond_destroy (&debugger_thread_exited_cond);
794 * Connect/Listen on HOST:PORT. If HOST is NULL, generate an address and listen on it.
797 transport_connect (const char *host, int port)
799 struct addrinfo hints;
800 struct addrinfo *result, *rp;
802 char port_string [128];
803 char handshake_msg [128];
809 sprintf (port_string, "%d", port);
811 mono_network_init ();
813 /* Obtain address(es) matching host/port */
815 memset (&hints, 0, sizeof (struct addrinfo));
816 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
817 hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
819 hints.ai_protocol = 0; /* Any protocol */
821 s = getaddrinfo (host, port_string, &hints, &result);
823 fprintf (stderr, "debugger-agent: Unable to connect to %s:%d: %s\n", host, port, gai_strerror (s));
828 if (agent_config.server) {
829 /* Wait for a connection */
831 struct sockaddr_in addr;
834 /* No address, generate one */
835 sfd = socket (AF_INET, SOCK_STREAM, 0);
838 /* This will bind the socket to a random port */
839 res = listen (sfd, 16);
841 fprintf (stderr, "debugger-agent: Unable to setup listening socket: %s\n", strerror (errno));
845 addrlen = sizeof (addr);
846 memset (&addr, 0, sizeof (addr));
847 res = getsockname (sfd, &addr, &addrlen);
851 port = ntohs (addr.sin_port);
853 /* Emit the address to stdout */
854 /* FIXME: Should print another interface, not localhost */
855 printf ("%s:%d\n", host, port);
857 /* Listen on the provided address */
858 for (rp = result; rp != NULL; rp = rp->ai_next) {
859 sfd = socket (rp->ai_family, rp->ai_socktype,
864 res = bind (sfd, rp->ai_addr, rp->ai_addrlen);
868 res = listen (sfd, 16);
874 freeaddrinfo (result);
877 DEBUG (1, fprintf (log_file, "Listening on %s:%d (timeout=%d ms)...\n", host, port, agent_config.timeout));
879 if (agent_config.timeout) {
884 tv.tv_usec = agent_config.timeout * 1000;
886 FD_SET (sfd, &readfds);
887 res = select (sfd + 1, &readfds, NULL, NULL, &tv);
889 fprintf (stderr, "debugger-agent: Timed out waiting to connect.\n");
894 conn_fd = accept (sfd, NULL, NULL);
896 fprintf (stderr, "debugger-agent: Unable to listen on %s:%d\n", host, port);
900 DEBUG (1, fprintf (log_file, "Accepted connection from client, socket fd=%d.\n", conn_fd));
902 /* Connect to the specified address */
903 /* FIXME: Respect the timeout */
904 for (rp = result; rp != NULL; rp = rp->ai_next) {
905 sfd = socket (rp->ai_family, rp->ai_socktype,
910 if (connect (sfd, rp->ai_addr, rp->ai_addrlen) != -1)
918 freeaddrinfo (result);
921 fprintf (stderr, "debugger-agent: Unable to connect to %s:%d\n", host, port);
926 /* Write handshake message */
927 sprintf (handshake_msg, "DWP-Handshake");
928 res = send (conn_fd, handshake_msg, strlen (handshake_msg), 0);
929 g_assert (res != -1);
932 res = recv (conn_fd, buf, strlen (handshake_msg), 0);
933 if ((res != strlen (handshake_msg)) || (memcmp (buf, handshake_msg, strlen (handshake_msg) != 0))) {
934 fprintf (stderr, "debugger-agent: DWP handshake failed.\n");
939 * Set TCP_NODELAY on the socket so the client receives events/command
940 * results immediately.
944 int result = setsockopt(conn_fd,
949 g_assert (result >= 0);
954 transport_send (guint8 *data, int len)
958 res = send (conn_fd, data, len, 0);
966 start_debugger_thread (void)
970 debugger_thread_handle = mono_create_thread (NULL, 0, debugger_thread, NULL, 0, &tid);
971 g_assert (debugger_thread_handle);
975 * Functions to decode protocol data
979 decode_byte (guint8 *buf, guint8 **endbuf, guint8 *limit)
982 g_assert (*endbuf <= limit);
987 decode_int (guint8 *buf, guint8 **endbuf, guint8 *limit)
990 g_assert (*endbuf <= limit);
992 return (((int)buf [0]) << 24) | (((int)buf [1]) << 16) | (((int)buf [2]) << 8) | (((int)buf [3]) << 0);
996 decode_long (guint8 *buf, guint8 **endbuf, guint8 *limit)
998 guint32 high = decode_int (buf, &buf, limit);
999 guint32 low = decode_int (buf, &buf, limit);
1003 return ((((guint64)high) << 32) | ((guint64)low));
1007 decode_id (guint8 *buf, guint8 **endbuf, guint8 *limit)
1009 return decode_int (buf, endbuf, limit);
1013 decode_string (guint8 *buf, guint8 **endbuf, guint8 *limit)
1015 int len = decode_int (buf, &buf, limit);
1018 s = g_malloc (len + 1);
1021 memcpy (s, buf, len);
1030 * Functions to encode protocol data
1034 guint8 *buf, *p, *end;
1038 buffer_init (Buffer *buf, int size)
1040 buf->buf = g_malloc (size);
1042 buf->end = buf->buf + size;
1046 buffer_make_room (Buffer *buf, int size)
1048 if (buf->end - buf->p < size) {
1049 int new_size = buf->end - buf->buf + size + 32;
1050 guint8 *p = g_realloc (buf->buf, new_size);
1051 size = buf->p - buf->buf;
1054 buf->end = buf->buf + new_size;
1059 buffer_add_byte (Buffer *buf, guint8 val)
1061 buffer_make_room (buf, 1);
1067 buffer_add_int (Buffer *buf, guint32 val)
1069 buffer_make_room (buf, 4);
1070 buf->p [0] = (val >> 24) & 0xff;
1071 buf->p [1] = (val >> 16) & 0xff;
1072 buf->p [2] = (val >> 8) & 0xff;
1073 buf->p [3] = (val >> 0) & 0xff;
1078 buffer_add_long (Buffer *buf, guint64 l)
1080 buffer_add_int (buf, (l >> 32) & 0xffffffff);
1081 buffer_add_int (buf, (l >> 0) & 0xffffffff);
1085 buffer_add_id (Buffer *buf, int id)
1087 buffer_add_int (buf, (guint64)id);
1091 buffer_add_data (Buffer *buf, guint8 *data, int len)
1093 buffer_make_room (buf, len);
1094 memcpy (buf->p, data, len);
1099 buffer_add_string (Buffer *buf, const char *str)
1104 buffer_add_int (buf, 0);
1107 buffer_add_int (buf, len);
1108 buffer_add_data (buf, (guint8*)str, len);
1113 buffer_free (Buffer *buf)
1119 send_packet (int command_set, int command, Buffer *data)
1125 id = InterlockedIncrement (&packet_id);
1127 len = data->p - data->buf + 11;
1128 buffer_init (&buf, len);
1129 buffer_add_int (&buf, len);
1130 buffer_add_int (&buf, id);
1131 buffer_add_byte (&buf, 0); /* flags */
1132 buffer_add_byte (&buf, command_set);
1133 buffer_add_byte (&buf, command);
1134 memcpy (buf.buf + 11, data->buf, data->p - data->buf);
1136 res = transport_send (buf.buf, len);
1144 send_reply_packet (int id, int error, Buffer *data)
1150 len = data->p - data->buf + 11;
1151 buffer_init (&buf, len);
1152 buffer_add_int (&buf, len);
1153 buffer_add_int (&buf, id);
1154 buffer_add_byte (&buf, 0x80); /* flags */
1155 buffer_add_byte (&buf, (error >> 8) & 0xff);
1156 buffer_add_byte (&buf, error);
1157 memcpy (buf.buf + 11, data->buf, data->p - data->buf);
1159 res = transport_send (buf.buf, len);
1171 * Represents an object accessible by the debugger client.
1174 /* Unique id used in the wire protocol to refer to objects */
1177 * A weakref gc handle pointing to the object. The gc handle is used to
1178 * detect if the object was garbage collected.
1183 /* Maps objid -> ObjRef */
1184 static GHashTable *objrefs;
1187 free_objref (gpointer value)
1191 mono_gchandle_free (o->handle);
1199 objrefs = g_hash_table_new_full (NULL, NULL, NULL, free_objref);
1203 objrefs_cleanup (void)
1205 g_hash_table_destroy (objrefs);
1209 static GHashTable *obj_to_objref;
1212 * Return an ObjRef for OBJ.
1215 get_objref (MonoObject *obj)
1226 /* Use a hash table with masked pointers to internalize object references */
1227 /* FIXME: This can grow indefinitely */
1228 mono_loader_lock ();
1231 obj_to_objref = g_hash_table_new (NULL, NULL);
1233 ref = g_hash_table_lookup (obj_to_objref, GINT_TO_POINTER (~((gsize)obj)));
1234 /* ref might refer to a different object with the same addr which was GCd */
1235 if (ref && mono_gchandle_get_target (ref->handle) == obj) {
1236 mono_loader_unlock ();
1240 ref = g_new0 (ObjRef, 1);
1241 ref->id = InterlockedIncrement (&objref_id);
1242 ref->handle = mono_gchandle_new_weakref (obj, FALSE);
1244 g_hash_table_insert (objrefs, GINT_TO_POINTER (ref->id), ref);
1245 g_hash_table_insert (obj_to_objref, GINT_TO_POINTER (~((gsize)obj)), ref);
1247 mono_loader_unlock ();
1253 get_objid (MonoObject *obj)
1255 return get_objref (obj)->id;
1259 * Set OBJ to the object identified by OBJID.
1260 * Returns 0 or an error code if OBJID is invalid or the object has been garbage
1264 get_object_allow_null (int objid, MonoObject **obj)
1274 return ERR_INVALID_OBJECT;
1276 mono_loader_lock ();
1278 ref = g_hash_table_lookup (objrefs, GINT_TO_POINTER (objid));
1281 *obj = mono_gchandle_get_target (ref->handle);
1282 mono_loader_unlock ();
1284 return ERR_INVALID_OBJECT;
1287 mono_loader_unlock ();
1288 return ERR_INVALID_OBJECT;
1293 get_object (int objid, MonoObject **obj)
1295 int err = get_object_allow_null (objid, obj);
1300 return ERR_INVALID_OBJECT;
1305 decode_objid (guint8 *buf, guint8 **endbuf, guint8 *limit)
1307 return decode_id (buf, endbuf, limit);
1311 buffer_add_objid (Buffer *buf, MonoObject *o)
1313 buffer_add_id (buf, get_objid (o));
1332 * Represents a runtime structure accessible to the debugger client
1335 /* Unique id used in the wire protocol */
1337 /* Domain of the runtime structure, NULL if the domain was unloaded */
1344 MonoAssembly *assembly;
1345 MonoClassField *field;
1347 MonoProperty *property;
1352 /* Maps runtime structure -> Id */
1353 GHashTable *val_to_id [ID_NUM];
1357 static GPtrArray *ids [ID_NUM];
1364 for (i = 0; i < ID_NUM; ++i)
1365 ids [i] = g_ptr_array_new ();
1373 for (i = 0; i < ID_NUM; ++i) {
1375 for (j = 0; j < ids [i]->len; ++j)
1376 g_free (g_ptr_array_index (ids [i], j));
1377 g_ptr_array_free (ids [i], TRUE);
1384 mono_debugger_agent_free_domain_info (MonoDomain *domain)
1386 AgentDomainInfo *info = domain_jit_info (domain)->agent_info;
1390 for (i = 0; i < ID_NUM; ++i)
1391 if (info->val_to_id [i])
1392 g_hash_table_destroy (info->val_to_id [i]);
1396 domain_jit_info (domain)->agent_info = NULL;
1398 /* Clear ids referencing structures in the domain */
1399 for (i = 0; i < ID_NUM; ++i) {
1401 for (j = 0; j < ids [i]->len; ++j) {
1402 Id *id = g_ptr_array_index (ids [i], j);
1403 if (id->domain == domain)
1411 get_id (MonoDomain *domain, IdType type, gpointer val)
1414 AgentDomainInfo *info;
1419 mono_loader_lock ();
1421 mono_domain_lock (domain);
1423 if (!domain_jit_info (domain)->agent_info)
1424 domain_jit_info (domain)->agent_info = g_new0 (AgentDomainInfo, 1);
1425 info = domain_jit_info (domain)->agent_info;
1426 if (info->val_to_id [type] == NULL)
1427 info->val_to_id [type] = g_hash_table_new (mono_aligned_addr_hash, NULL);
1429 id = g_hash_table_lookup (info->val_to_id [type], val);
1431 mono_domain_unlock (domain);
1432 mono_loader_unlock ();
1436 id = g_new0 (Id, 1);
1438 id->id = ids [type]->len + 1;
1439 id->domain = domain;
1442 g_hash_table_insert (info->val_to_id [type], val, id);
1444 mono_domain_unlock (domain);
1446 g_ptr_array_add (ids [type], id);
1448 mono_loader_unlock ();
1453 static inline gpointer
1454 decode_ptr_id (guint8 *buf, guint8 **endbuf, guint8 *limit, IdType type, MonoDomain **domain, int *err)
1458 int id = decode_id (buf, endbuf, limit);
1467 // FIXME: error handling
1468 mono_loader_lock ();
1469 g_assert (id > 0 && id <= ids [type]->len);
1471 res = g_ptr_array_index (ids [type], GPOINTER_TO_INT (id - 1));
1472 mono_loader_unlock ();
1474 if (res->domain == NULL) {
1475 *err = ERR_UNLOADED;
1480 *domain = res->domain;
1482 return res->data.val;
1486 buffer_add_ptr_id (Buffer *buf, MonoDomain *domain, IdType type, gpointer val)
1488 buffer_add_id (buf, get_id (domain, type, val));
1491 static inline MonoClass*
1492 decode_typeid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1494 return decode_ptr_id (buf, endbuf, limit, ID_TYPE, domain, err);
1497 static inline MonoAssembly*
1498 decode_assemblyid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1500 return decode_ptr_id (buf, endbuf, limit, ID_ASSEMBLY, domain, err);
1503 static inline MonoImage*
1504 decode_moduleid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1506 return decode_ptr_id (buf, endbuf, limit, ID_MODULE, domain, err);
1509 static inline MonoMethod*
1510 decode_methodid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1512 return decode_ptr_id (buf, endbuf, limit, ID_METHOD, domain, err);
1515 static inline MonoClassField*
1516 decode_fieldid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1518 return decode_ptr_id (buf, endbuf, limit, ID_FIELD, domain, err);
1521 static inline MonoDomain*
1522 decode_domainid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1524 return decode_ptr_id (buf, endbuf, limit, ID_DOMAIN, domain, err);
1527 static inline MonoProperty*
1528 decode_propertyid (guint8 *buf, guint8 **endbuf, guint8 *limit, MonoDomain **domain, int *err)
1530 return decode_ptr_id (buf, endbuf, limit, ID_PROPERTY, domain, err);
1534 buffer_add_typeid (Buffer *buf, MonoDomain *domain, MonoClass *klass)
1536 buffer_add_ptr_id (buf, domain, ID_TYPE, klass);
1540 buffer_add_methodid (Buffer *buf, MonoDomain *domain, MonoMethod *method)
1542 buffer_add_ptr_id (buf, domain, ID_METHOD, method);
1546 buffer_add_assemblyid (Buffer *buf, MonoDomain *domain, MonoAssembly *assembly)
1548 buffer_add_ptr_id (buf, domain, ID_ASSEMBLY, assembly);
1552 buffer_add_moduleid (Buffer *buf, MonoDomain *domain, MonoImage *image)
1554 buffer_add_ptr_id (buf, domain, ID_MODULE, image);
1558 buffer_add_fieldid (Buffer *buf, MonoDomain *domain, MonoClassField *field)
1560 buffer_add_ptr_id (buf, domain, ID_FIELD, field);
1564 buffer_add_propertyid (Buffer *buf, MonoDomain *domain, MonoProperty *property)
1566 buffer_add_ptr_id (buf, domain, ID_PROPERTY, property);
1570 buffer_add_domainid (Buffer *buf, MonoDomain *domain)
1572 buffer_add_ptr_id (buf, domain, ID_DOMAIN, domain);
1575 static void invoke_method (void);
1582 * save_thread_context:
1584 * Set CTX as the current threads context which is used for computing stack traces.
1585 * This function is signal-safe.
1588 save_thread_context (MonoContext *ctx)
1590 DebuggerTlsData *tls;
1592 tls = TlsGetValue (debugger_tls_id);
1596 memcpy (&tls->ctx, ctx, sizeof (MonoContext));
1598 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
1599 MONO_INIT_CONTEXT_FROM_CURRENT (&tls->ctx);
1601 MONO_INIT_CONTEXT_FROM_FUNC (&tls->ctx, save_thread_context);
1605 tls->lmf = mono_get_lmf ();
1606 tls->domain = mono_domain_get ();
1607 tls->has_context = TRUE;
1610 /* The number of times the runtime is suspended */
1611 static gint32 suspend_count;
1613 /* Number of threads suspended */
1615 * If this is equal to the size of thread_to_tls, the runtime is considered
1618 static gint32 threads_suspend_count;
1620 static mono_mutex_t suspend_mutex;
1622 /* Cond variable used to wait for suspend_count becoming 0 */
1623 static mono_cond_t suspend_cond;
1625 /* Semaphore used to wait for a thread becoming suspended */
1626 static MonoSemType suspend_sem;
1631 mono_mutex_init (&suspend_mutex, NULL);
1632 mono_cond_init (&suspend_cond, NULL);
1633 MONO_SEM_INIT (&suspend_sem, 0);
1637 * mono_debugger_agent_thread_interrupt:
1639 * Called by the abort signal handler.
1642 mono_debugger_agent_thread_interrupt (void *sigctx, MonoJitInfo *ji)
1644 DebuggerTlsData *tls;
1649 tls = TlsGetValue (debugger_tls_id);
1654 * We use interrupt_count to determine whenever this interrupt should be processed
1655 * by us or the normal interrupt processing code in the signal handler.
1656 * There is no race here with notify_thread (), since the signal is sent after
1657 * incrementing interrupt_count.
1659 if (tls->interrupt_count == 0)
1662 InterlockedDecrement (&tls->interrupt_count);
1664 // FIXME: Races when the thread leaves managed code before hitting a single step
1668 /* Running managed code, will be suspended by the single step code */
1669 //printf ("S1: %p\n", GetCurrentThreadId ());
1673 * Running native code, will be suspended when it returns to/enters
1674 * managed code. Treat it as already suspended.
1676 //printf ("S2: %p\n", GetCurrentThreadId ());
1677 if (!tls->suspended) {
1681 * FIXME: This is dangerous as the thread is not really suspended, but
1682 * without this, we can't print stack traces for threads executing
1684 * Maybe do a stack walk now, and save its result ?
1686 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
1687 save_thread_context (&ctx);
1689 tls->suspended = TRUE;
1690 MONO_SEM_POST (&suspend_sem);
1697 static void CALLBACK notify_thread_apc (ULONG_PTR param)
1700 mono_debugger_agent_thread_interrupt (NULL, NULL);
1702 #endif /* HOST_WIN32 */
1707 * Notify a thread that it needs to suspend.
1710 notify_thread (gpointer key, gpointer value, gpointer user_data)
1712 MonoInternalThread *thread = key;
1713 DebuggerTlsData *tls = value;
1714 gsize tid = thread->tid;
1716 if (GetCurrentThreadId () != tid) {
1717 DEBUG(1, fprintf (log_file, "[%p] Interrupting %p...\n", (gpointer)GetCurrentThreadId (), (gpointer)tid));
1719 * Maybe we could use the normal interrupt infrastructure, but that does a lot
1720 * of things like breaking waits etc. which we don't want.
1722 InterlockedIncrement (&tls->interrupt_count);
1723 ves_icall_System_Threading_Thread_Abort (thread, NULL);
1730 * Increase the suspend count of the VM. While the suspend count is greater
1731 * than 0, runtime threads are suspended at certain points during execution.
1736 mono_loader_lock ();
1738 mono_mutex_lock (&suspend_mutex);
1742 DEBUG(1, fprintf (log_file, "[%p] Suspending vm...\n", (gpointer)GetCurrentThreadId ()));
1744 if (suspend_count == 1) {
1745 // FIXME: Is it safe to call this inside the lock ?
1746 start_single_stepping ();
1747 mono_g_hash_table_foreach (thread_to_tls, notify_thread, NULL);
1750 mono_mutex_unlock (&suspend_mutex);
1752 mono_loader_unlock ();
1758 * Decrease the suspend count of the VM. If the count reaches 0, runtime threads
1766 g_assert (debugger_thread_id == GetCurrentThreadId ());
1768 mono_loader_lock ();
1770 mono_mutex_lock (&suspend_mutex);
1772 g_assert (suspend_count > 0);
1775 DEBUG(1, fprintf (log_file, "[%p] Resuming vm...\n", (gpointer)GetCurrentThreadId ()));
1777 if (suspend_count == 0) {
1778 // FIXME: Is it safe to call this inside the lock ?
1779 stop_single_stepping ();
1780 err = mono_cond_broadcast (&suspend_cond);
1781 g_assert (err == 0);
1784 mono_mutex_unlock (&suspend_mutex);
1785 //g_assert (err == 0);
1787 mono_loader_unlock ();
1791 invalidate_frames (DebuggerTlsData *tls)
1796 tls = TlsGetValue (debugger_tls_id);
1799 for (i = 0; i < tls->frame_count; ++i) {
1800 if (tls->frames [i]->jit)
1801 mono_debug_free_method_jit_info (tls->frames [i]->jit);
1802 g_free (tls->frames [i]);
1804 g_free (tls->frames);
1805 tls->frame_count = 0;
1812 * Suspend the current thread until the runtime is resumed. If the thread has a
1813 * pending invoke, then the invoke is executed before this function returns.
1816 suspend_current (void)
1819 DebuggerTlsData *tls;
1821 g_assert (debugger_thread_id != GetCurrentThreadId ());
1823 if (mono_loader_lock_is_owned_by_self ()) {
1825 * If we own the loader mutex, can't suspend until we release it, since the
1826 * whole runtime can deadlock otherwise.
1831 tls = TlsGetValue (debugger_tls_id);
1834 mono_mutex_lock (&suspend_mutex);
1836 if (!tls->suspended) {
1837 tls->suspended = TRUE;
1838 tls->really_suspended = TRUE;
1839 MONO_SEM_POST (&suspend_sem);
1842 DEBUG(1, fprintf (log_file, "[%p] Suspended.\n", (gpointer)GetCurrentThreadId ()));
1844 while (suspend_count > 0) {
1846 if (WAIT_TIMEOUT == WaitForSingleObject(suspend_cond, 0))
1848 mono_mutex_unlock (&suspend_mutex);
1850 mono_mutex_lock (&suspend_mutex);
1856 err = mono_cond_wait (&suspend_cond, &suspend_mutex);
1857 g_assert (err == 0);
1861 tls->suspended = FALSE;
1862 tls->really_suspended = FALSE;
1864 threads_suspend_count --;
1866 mono_mutex_unlock (&suspend_mutex);
1868 DEBUG(1, fprintf (log_file, "[%p] Resumed.\n", (gpointer)GetCurrentThreadId ()));
1871 /* Save the original context */
1872 tls->invoke->has_ctx = TRUE;
1873 memcpy (&tls->invoke->ctx, &tls->ctx, sizeof (MonoContext));
1878 /* The frame info becomes invalid after a resume */
1879 tls->has_context = FALSE;
1880 invalidate_frames (NULL);
1884 count_thread (gpointer key, gpointer value, gpointer user_data)
1886 DebuggerTlsData *tls = value;
1888 if (!tls->suspended && !tls->terminated)
1889 *(int*)user_data = *(int*)user_data + 1;
1893 count_threads_to_wait_for (void)
1897 mono_loader_lock ();
1898 mono_g_hash_table_foreach (thread_to_tls, count_thread, &count);
1899 mono_loader_unlock ();
1907 * Wait until the runtime is completely suspended.
1910 wait_for_suspend (void)
1912 int nthreads, nwait, err;
1913 gboolean waited = FALSE;
1915 // FIXME: Threads starting/stopping ?
1916 mono_loader_lock ();
1917 nthreads = mono_g_hash_table_size (thread_to_tls);
1918 mono_loader_unlock ();
1921 nwait = count_threads_to_wait_for ();
1923 DEBUG(1, fprintf (log_file, "Waiting for %d(%d) threads to suspend...\n", nwait, nthreads));
1924 err = MONO_SEM_WAIT (&suspend_sem);
1925 g_assert (err == 0);
1933 DEBUG(1, fprintf (log_file, "%d threads suspended.\n", nthreads));
1939 * Return whenever the runtime is suspended.
1944 return count_threads_to_wait_for () == 0;
1948 * compute_il_offset:
1950 * Compute the IL offset corresponding to NATIVE_OFFSET, which should be
1951 * a location of a sequence point.
1952 * We use this function instead of mono_debug_il_offset_from_address () etc,
1953 * which doesn't seem to work in a lot of cases.
1956 compute_il_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset)
1958 GPtrArray *seq_points;
1959 int i, last_il_offset, seq_il_offset, seq_native_offset;
1961 mono_domain_lock (domain);
1962 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
1963 mono_domain_unlock (domain);
1964 g_assert (seq_points);
1966 last_il_offset = -1;
1968 /* Find the sequence point */
1969 for (i = 0; i < seq_points->len; i += 2) {
1970 seq_il_offset = GPOINTER_TO_UINT (g_ptr_array_index (seq_points, i));
1971 seq_native_offset = GPOINTER_TO_UINT (g_ptr_array_index (seq_points, i + 1));
1973 if (seq_native_offset > native_offset)
1975 last_il_offset = seq_il_offset;
1978 return last_il_offset;
1982 DebuggerTlsData *tls;
1984 } ComputeFramesUserData;
1987 process_frame (StackFrameInfo *info, MonoContext *ctx, gpointer user_data)
1989 ComputeFramesUserData *ud = user_data;
1993 if (info->type != FRAME_TYPE_MANAGED) {
1994 if (info->type == FRAME_TYPE_DEBUGGER_INVOKE) {
1995 /* Mark the last frame as an invoke frame */
1997 ((StackFrame*)ud->frames->data)->flags |= FRAME_FLAG_DEBUGGER_INVOKE;
2003 method = info->ji->method;
2005 method = info->method;
2007 if (!method || (method->wrapper_type && method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD))
2010 if (info->il_offset == -1) {
2011 /* Can't use compute_il_offset () since ip doesn't point precisely at at a seq point */
2012 info->il_offset = mono_debug_il_offset_from_address (method, info->domain, info->native_offset);
2015 DEBUG (1, fprintf (stderr, "\tFrame: %s %d %d %d\n", mono_method_full_name (method, TRUE), info->native_offset, info->il_offset, info->managed));
2017 if (!info->managed && method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD) {
2019 * mono_arch_find_jit_info () returns the context stored in the LMF for
2020 * native frames, but it should unwind once. This is why we have duplicate
2021 * frames on the stack sometimes.
2022 * !managed also seems to be set for dynamic methods.
2027 frame = g_new0 (StackFrame, 1);
2028 frame->method = method;
2029 frame->il_offset = info->il_offset;
2031 frame->domain = info->domain;
2033 ud->frames = g_slist_append (ud->frames, frame);
2039 compute_frame_info (MonoInternalThread *thread, DebuggerTlsData *tls)
2041 ComputeFramesUserData user_data;
2043 int i, findex, new_frame_count;
2044 StackFrame **new_frames, *f;
2046 // FIXME: Locking on tls
2047 if (tls->frames && tls->frames_up_to_date)
2050 DEBUG(1, fprintf (log_file, "Frames for %p(tid=%lx):\n", thread, (glong)thread->tid));
2052 user_data.tls = tls;
2053 user_data.frames = NULL;
2054 if (tls->has_context) {
2055 mono_jit_walk_stack_from_ctx_in_thread (process_frame, tls->domain, &tls->ctx, FALSE, thread, tls->lmf, &user_data);
2058 tls->frame_count = 0;
2062 new_frame_count = g_slist_length (user_data.frames);
2063 new_frames = g_new0 (StackFrame*, new_frame_count);
2065 for (tmp = user_data.frames; tmp; tmp = tmp->next) {
2069 * Reuse the id for already existing stack frames, so invokes don't invalidate
2070 * the still valid stack frames.
2072 for (i = 0; i < tls->frame_count; ++i) {
2073 if (MONO_CONTEXT_GET_SP (&tls->frames [i]->ctx) == MONO_CONTEXT_GET_SP (&f->ctx)) {
2074 f->id = tls->frames [i]->id;
2079 if (i >= tls->frame_count)
2080 f->id = InterlockedIncrement (&frame_id);
2082 new_frames [findex ++] = f;
2085 g_slist_free (user_data.frames);
2087 invalidate_frames (tls);
2089 tls->frames = new_frames;
2090 tls->frame_count = new_frame_count;
2091 tls->frames_up_to_date = TRUE;
2099 * create_event_list:
2101 * Return a list of event request ids matching EVENT, starting from REQS, which
2102 * can be NULL to include all event requests. Set SUSPEND_POLICY to the suspend
2104 * We return request ids, instead of requests, to simplify threading, since
2105 * requests could be deleted anytime when the loader lock is not held.
2106 * LOCKING: Assumes the loader lock is held.
2109 create_event_list (EventKind event, GPtrArray *reqs, MonoJitInfo *ji, MonoException *exc, int *suspend_policy)
2112 GSList *events = NULL;
2114 *suspend_policy = SUSPEND_POLICY_NONE;
2117 reqs = event_requests;
2122 for (i = 0; i < reqs->len; ++i) {
2123 EventRequest *req = g_ptr_array_index (reqs, i);
2124 if (req->event_kind == event) {
2125 gboolean filtered = FALSE;
2128 for (j = 0; j < req->nmodifiers; ++j) {
2129 Modifier *mod = &req->modifiers [j];
2131 if (mod->kind == MOD_KIND_COUNT) {
2133 if (mod->data.count > 0) {
2134 if (mod->data.count > 0) {
2136 if (mod->data.count == 0)
2140 } else if (mod->kind == MOD_KIND_THREAD_ONLY) {
2141 if (mod->data.thread != mono_thread_internal_current ())
2143 } else if (mod->kind == MOD_KIND_EXCEPTION_ONLY && exc) {
2144 if (mod->data.exc_class && !mono_class_is_assignable_from (mod->data.exc_class, exc->object.vtable->klass))
2146 } else if (mod->kind == MOD_KIND_ASSEMBLY_ONLY && ji) {
2148 gboolean found = FALSE;
2149 MonoAssembly **assemblies = mod->data.assemblies;
2152 for (k = 0; assemblies [k]; ++k)
2153 if (assemblies [k] == ji->method->klass->image->assembly)
2162 *suspend_policy = MAX (*suspend_policy, req->suspend_policy);
2163 events = g_slist_append (events, GINT_TO_POINTER (req->id));
2168 /* Send a VM START/DEATH event by default */
2169 if (event == EVENT_KIND_VM_START)
2170 events = g_slist_append (events, GINT_TO_POINTER (0));
2171 if (event == EVENT_KIND_VM_DEATH)
2172 events = g_slist_append (events, GINT_TO_POINTER (0));
2177 static G_GNUC_UNUSED const char*
2178 event_to_string (EventKind event)
2181 case EVENT_KIND_VM_START: return "VM_START";
2182 case EVENT_KIND_VM_DEATH: return "VM_DEATH";
2183 case EVENT_KIND_THREAD_START: return "THREAD_START";
2184 case EVENT_KIND_THREAD_DEATH: return "THREAD_DEATH";
2185 case EVENT_KIND_APPDOMAIN_CREATE: return "APPDOMAIN_CREATE";
2186 case EVENT_KIND_APPDOMAIN_UNLOAD: return "APPDOMAIN_UNLOAD";
2187 case EVENT_KIND_METHOD_ENTRY: return "METHOD_ENTRY";
2188 case EVENT_KIND_METHOD_EXIT: return "METHOD_EXIT";
2189 case EVENT_KIND_ASSEMBLY_LOAD: return "ASSEMBLY_LOAD";
2190 case EVENT_KIND_ASSEMBLY_UNLOAD: return "ASSEMBLY_UNLOAD";
2191 case EVENT_KIND_BREAKPOINT: return "BREAKPOINT";
2192 case EVENT_KIND_STEP: return "STEP";
2193 case EVENT_KIND_TYPE_LOAD: return "TYPE_LOAD";
2194 case EVENT_KIND_EXCEPTION: return "EXCEPTION";
2196 g_assert_not_reached ();
2203 * Send an event to the client, suspending the vm if needed.
2204 * LOCKING: Since this can suspend the calling thread, no locks should be held
2206 * The EVENTS list is freed by this function.
2209 process_event (EventKind event, gpointer arg, gint32 il_offset, MonoContext *ctx, GSList *events, int suspend_policy)
2213 MonoDomain *domain = mono_domain_get ();
2219 if (!vm_start_event_sent && event != EVENT_KIND_VM_START)
2220 // FIXME: We miss those events
2223 if (vm_death_event_sent)
2226 if (mono_runtime_is_shutting_down () && event != EVENT_KIND_VM_DEATH)
2235 if (debugger_thread_id == GetCurrentThreadId () && event != EVENT_KIND_VM_DEATH)
2236 // FIXME: Send these with a NULL thread, don't suspend the current thread
2239 buffer_init (&buf, 128);
2240 buffer_add_byte (&buf, suspend_policy);
2241 buffer_add_int (&buf, g_slist_length (events)); // n of events
2243 for (l = events; l; l = l->next) {
2244 buffer_add_byte (&buf, event); // event kind
2245 buffer_add_int (&buf, GPOINTER_TO_INT (l->data)); // request id
2247 thread = mono_thread_current ();
2249 if (event == EVENT_KIND_VM_START)
2251 else if (event == EVENT_KIND_THREAD_START)
2252 g_assert (mono_thread_internal_current () == arg);
2254 buffer_add_objid (&buf, (MonoObject*)thread); // thread
2257 case EVENT_KIND_THREAD_START:
2258 case EVENT_KIND_THREAD_DEATH:
2260 case EVENT_KIND_APPDOMAIN_CREATE:
2261 case EVENT_KIND_APPDOMAIN_UNLOAD:
2262 buffer_add_domainid (&buf, arg);
2264 case EVENT_KIND_METHOD_ENTRY:
2265 case EVENT_KIND_METHOD_EXIT:
2266 buffer_add_methodid (&buf, domain, arg);
2268 case EVENT_KIND_ASSEMBLY_LOAD:
2269 case EVENT_KIND_ASSEMBLY_UNLOAD:
2270 buffer_add_assemblyid (&buf, domain, arg);
2272 case EVENT_KIND_TYPE_LOAD:
2273 buffer_add_typeid (&buf, domain, arg);
2275 case EVENT_KIND_BREAKPOINT:
2276 case EVENT_KIND_STEP:
2277 buffer_add_methodid (&buf, domain, arg);
2278 buffer_add_long (&buf, il_offset);
2280 case EVENT_KIND_VM_START:
2281 buffer_add_domainid (&buf, mono_get_root_domain ());
2283 case EVENT_KIND_VM_DEATH:
2285 case EVENT_KIND_EXCEPTION:
2286 buffer_add_objid (&buf, (MonoObject*)arg);
2289 g_assert_not_reached ();
2293 if (event == EVENT_KIND_VM_START) {
2294 suspend_policy = agent_config.suspend ? SUSPEND_POLICY_ALL : SUSPEND_POLICY_NONE;
2295 start_debugger_thread ();
2298 if (event == EVENT_KIND_VM_DEATH) {
2299 vm_death_event_sent = TRUE;
2301 suspend_policy = SUSPEND_POLICY_NONE;
2304 if (mono_runtime_is_shutting_down ())
2305 suspend_policy = SUSPEND_POLICY_NONE;
2307 if (suspend_policy != SUSPEND_POLICY_NONE) {
2309 * Save the thread context and start suspending before sending the packet,
2310 * since we could be receiving the resume request before send_packet ()
2313 save_thread_context (ctx);
2317 send_packet (CMD_SET_EVENT, CMD_COMPOSITE, &buf);
2319 g_slist_free (events);
2322 if (event == EVENT_KIND_VM_START)
2323 vm_start_event_sent = TRUE;
2325 DEBUG (1, fprintf (log_file, "[%p] Sent event %s, suspend=%d.\n", (gpointer)GetCurrentThreadId (), event_to_string (event), suspend_policy));
2329 switch (suspend_policy) {
2330 case SUSPEND_POLICY_NONE:
2332 case SUSPEND_POLICY_ALL:
2335 case SUSPEND_POLICY_EVENT_THREAD:
2339 g_assert_not_reached ();
2344 process_profiler_event (EventKind event, gpointer arg)
2349 mono_loader_lock ();
2350 events = create_event_list (event, NULL, NULL, NULL, &suspend_policy);
2351 mono_loader_unlock ();
2353 process_event (event, arg, 0, NULL, events, suspend_policy);
2357 runtime_initialized (MonoProfiler *prof)
2359 process_profiler_event (EVENT_KIND_VM_START, mono_thread_current ());
2363 runtime_shutdown (MonoProfiler *prof)
2365 process_profiler_event (EVENT_KIND_VM_DEATH, mono_thread_current ());
2367 mono_debugger_agent_cleanup ();
2371 thread_startup (MonoProfiler *prof, gsize tid)
2373 MonoInternalThread *thread = mono_thread_internal_current ();
2374 MonoInternalThread *old_thread;
2375 DebuggerTlsData *tls;
2377 if (tid == debugger_thread_id)
2380 g_assert (thread->tid == tid);
2382 mono_loader_lock ();
2383 old_thread = mono_g_hash_table_lookup (tid_to_thread, (gpointer)tid);
2384 mono_loader_unlock ();
2386 if (thread == old_thread) {
2388 * For some reason, thread_startup () might be called for the same thread
2389 * multiple times (attach ?).
2391 DEBUG (1, fprintf (log_file, "[%p] thread_start () called multiple times for %p, ignored.\n", (gpointer)tid, (gpointer)tid));
2395 * thread_end () might not be called for some threads, and the tid could
2398 DEBUG (1, fprintf (log_file, "[%p] Removing stale data for tid %p.\n", (gpointer)tid, (gpointer)tid));
2399 mono_loader_lock ();
2400 mono_g_hash_table_remove (thread_to_tls, old_thread);
2401 mono_g_hash_table_remove (tid_to_thread, (gpointer)tid);
2402 mono_g_hash_table_remove (tid_to_thread_obj, (gpointer)tid);
2403 mono_loader_unlock ();
2407 tls = TlsGetValue (debugger_tls_id);
2409 // FIXME: Free this somewhere
2410 tls = g_new0 (DebuggerTlsData, 1);
2411 tls->resume_event = CreateEvent (NULL, FALSE, FALSE, NULL);
2412 TlsSetValue (debugger_tls_id, tls);
2414 DEBUG (1, fprintf (log_file, "[%p] Thread started, obj=%p, tls=%p.\n", (gpointer)tid, thread, tls));
2416 mono_loader_lock ();
2417 mono_g_hash_table_insert (thread_to_tls, thread, tls);
2418 mono_g_hash_table_insert (tid_to_thread, (gpointer)tid, thread);
2419 mono_g_hash_table_insert (tid_to_thread_obj, (gpointer)tid, mono_thread_current ());
2420 mono_loader_unlock ();
2422 process_profiler_event (EVENT_KIND_THREAD_START, thread);
2425 * suspend_vm () could have missed this thread, so wait for a resume.
2431 thread_end (MonoProfiler *prof, gsize tid)
2433 MonoInternalThread *thread;
2434 DebuggerTlsData *tls = NULL;
2436 mono_loader_lock ();
2437 thread = mono_g_hash_table_lookup (tid_to_thread, (gpointer)tid);
2439 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
2440 /* FIXME: Maybe we need to free this instead, but some code can't handle that */
2441 tls->terminated = TRUE;
2442 mono_g_hash_table_remove (tid_to_thread_obj, (gpointer)tid);
2443 /* Can't remove from tid_to_thread, as that would defeat the check in thread_start () */
2445 mono_loader_unlock ();
2447 /* We might be called for threads started before we registered the start callback */
2449 DEBUG (1, fprintf (log_file, "[%p] Thread terminated, obj=%p, tls=%p.\n", (gpointer)tid, thread, tls));
2450 process_profiler_event (EVENT_KIND_THREAD_DEATH, thread);
2455 appdomain_load (MonoProfiler *prof, MonoDomain *domain, int result)
2457 process_profiler_event (EVENT_KIND_APPDOMAIN_CREATE, domain);
2461 appdomain_unload (MonoProfiler *prof, MonoDomain *domain)
2463 process_profiler_event (EVENT_KIND_APPDOMAIN_UNLOAD, domain);
2467 assembly_load (MonoProfiler *prof, MonoAssembly *assembly, int result)
2469 /* Sent later in jit_end () */
2470 mono_loader_lock ();
2471 g_ptr_array_add (pending_assembly_loads, assembly);
2472 mono_loader_unlock ();
2476 assembly_unload (MonoProfiler *prof, MonoAssembly *assembly)
2478 process_profiler_event (EVENT_KIND_ASSEMBLY_UNLOAD, assembly);
2482 start_runtime_invoke (MonoProfiler *prof, MonoMethod *method)
2487 end_runtime_invoke (MonoProfiler *prof, MonoMethod *method)
2491 gpointer stackptr = ((guint64)_AddressOfReturnAddress () - sizeof (void*));
2493 gpointer stackptr = __builtin_frame_address (0);
2496 if (ss_req == NULL || ss_req->start_sp > stackptr || ss_req->thread != mono_thread_internal_current ())
2500 * We need to stop single stepping when exiting a runtime invoke, since if it is
2501 * a step out, it may return to native code, and thus never end.
2503 mono_loader_lock ();
2504 for (i = 0; i < event_requests->len; ++i) {
2505 EventRequest *req = g_ptr_array_index (event_requests, i);
2507 if (req->event_kind == EVENT_KIND_STEP) {
2509 g_ptr_array_remove_index_fast (event_requests, i);
2514 mono_loader_unlock ();
2519 jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result)
2522 * We emit type load events when the first method of the type is JITted,
2523 * since the class load profiler callbacks might be called with the
2524 * loader lock held. They could also occur in the debugger thread.
2525 * Same for assembly load events.
2527 gboolean type_load = FALSE;
2530 MonoAssembly *assembly = NULL;
2532 // FIXME: Maybe store this in TLS so the thread of the event is correct ?
2533 mono_loader_lock ();
2534 if (pending_assembly_loads->len > 0) {
2535 assembly = g_ptr_array_index (pending_assembly_loads, 0);
2536 g_ptr_array_remove_index (pending_assembly_loads, 0);
2538 mono_loader_unlock ();
2541 process_profiler_event (EVENT_KIND_ASSEMBLY_LOAD, assembly);
2546 mono_loader_lock ();
2547 if (!g_hash_table_lookup (loaded_classes, method->klass)) {
2549 g_hash_table_insert (loaded_classes, method->klass, method->klass);
2551 mono_loader_unlock ();
2553 process_profiler_event (EVENT_KIND_TYPE_LOAD, method->klass);
2556 add_pending_breakpoints (method, jinfo);
2560 * BREAKPOINTS/SINGLE STEPPING
2564 * Contains information about an inserted breakpoint.
2567 long il_offset, native_offset;
2569 gboolean pending, entry;
2571 } BreakpointInstance;
2574 * Contains generic information about a breakpoint.
2578 * The method where the breakpoint is placed. Can be NULL in which case it
2579 * is inserted into every method. This is used to implement method entry/
2580 * exit events. Can be a generic method definition, in which case the
2581 * breakpoint is inserted into every instance.
2585 gboolean pending, entry;
2588 * A list of BreakpointInstance structures describing where the breakpoint
2589 * was inserted. There could be more than one because of
2590 * generics/appdomains/method entry/exit.
2592 GPtrArray *children;
2595 /* List of breakpoints */
2596 static GPtrArray *breakpoints;
2597 /* Maps breakpoint locations to the number of breakpoints at that location */
2598 static GHashTable *bp_locs;
2601 breakpoints_init (void)
2603 breakpoints = g_ptr_array_new ();
2604 bp_locs = g_hash_table_new (NULL, NULL);
2608 breakpoints_cleanup (void)
2612 mono_loader_lock ();
2614 for (i = 0; i < breakpoints->len; ++i)
2615 g_free (g_ptr_array_index (breakpoints, i));
2617 g_ptr_array_free (breakpoints, TRUE);
2618 g_hash_table_destroy (bp_locs);
2620 mono_loader_unlock ();
2624 * insert_breakpoint:
2626 * Insert the breakpoint described by BP into the method described by
2630 insert_breakpoint (GPtrArray *seq_points, MonoJitInfo *ji, MonoBreakpoint *bp)
2633 gint32 il_offset, native_offset;
2634 BreakpointInstance *inst;
2637 for (i = 0; i < seq_points->len; i += 2) {
2638 il_offset = GPOINTER_TO_INT (g_ptr_array_index (seq_points, i));
2639 native_offset = GPOINTER_TO_INT (g_ptr_array_index (seq_points, i + 1));
2641 if (il_offset == bp->il_offset)
2645 if (i == seq_points->len)
2646 /* Have to handle this somehow */
2649 inst = g_new0 (BreakpointInstance, 1);
2650 inst->native_offset = native_offset;
2651 inst->ip = (guint8*)ji->code_start + native_offset;
2654 mono_loader_lock ();
2656 g_ptr_array_add (bp->children, inst);
2658 count = GPOINTER_TO_INT (g_hash_table_lookup (bp_locs, inst->ip));
2659 g_hash_table_insert (bp_locs, inst->ip, GINT_TO_POINTER (count + 1));
2660 mono_loader_unlock ();
2663 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
2664 mono_arch_set_breakpoint (ji, inst->ip);
2672 remove_breakpoint (BreakpointInstance *inst)
2674 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
2676 MonoJitInfo *ji = inst->ji;
2677 guint8 *ip = inst->ip;
2679 mono_loader_lock ();
2680 count = GPOINTER_TO_INT (g_hash_table_lookup (bp_locs, ip));
2681 g_hash_table_insert (bp_locs, ip, GINT_TO_POINTER (count - 1));
2682 mono_loader_unlock ();
2684 g_assert (count > 0);
2687 mono_arch_clear_breakpoint (ji, ip);
2695 * add_pending_breakpoints:
2697 * Insert pending breakpoints into the newly JITted method METHOD.
2700 add_pending_breakpoints (MonoMethod *method, MonoJitInfo *ji)
2703 GPtrArray *seq_points;
2709 domain = mono_domain_get ();
2711 mono_loader_lock ();
2713 for (i = 0; i < breakpoints->len; ++i) {
2714 MonoBreakpoint *bp = g_ptr_array_index (breakpoints, i);
2716 if (bp->pending && (bp->method == method || !bp->method || (method->is_inflated && ((MonoMethodInflated*)method)->declaring == bp->method))) {
2717 mono_domain_lock (domain);
2718 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, ji->method);
2719 mono_domain_unlock (domain);
2721 /* Could be AOT code */
2723 g_assert (seq_points);
2725 insert_breakpoint (seq_points, ji, bp);
2729 mono_loader_unlock ();
2733 set_bp_in_method (MonoDomain *domain, MonoMethod *method, GPtrArray *seq_points, MonoBreakpoint *bp)
2738 code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
2740 /* Might be AOTed code */
2741 code = mono_aot_get_method (domain, method);
2743 ji = mono_jit_info_table_find (domain, code);
2748 insert_breakpoint (seq_points, ji, bp);
2752 set_bp_in_method_cb (gpointer key, gpointer value, gpointer user_data)
2754 MonoMethod *method = key;
2755 GPtrArray *seq_points = value;
2756 MonoBreakpoint *bp = user_data;
2757 MonoDomain *domain = mono_domain_get ();
2760 if (method->is_inflated && ((MonoMethodInflated*)method)->declaring == bp->method) {
2761 /* Generic instance */
2762 set_bp_in_method (domain, method, seq_points, bp);
2765 /* Method entry/exit */
2766 set_bp_in_method (domain, method, seq_points, bp);
2773 * Set a breakpoint at IL_OFFSET in METHOD.
2774 * METHOD can be NULL, in which case a breakpoint is placed in all methods.
2775 * METHOD can also be a generic method definition, in which case a breakpoint
2776 * is placed in all instances of the method.
2778 static MonoBreakpoint*
2779 set_breakpoint (MonoMethod *method, long il_offset, EventRequest *req)
2781 GPtrArray *seq_points;
2786 // - suspend/resume the vm to prevent code patching problems
2788 // - multiple breakpoints on the same location
2789 // - dynamic methods
2792 bp = g_new0 (MonoBreakpoint, 1);
2793 bp->method = method;
2794 bp->il_offset = il_offset;
2796 bp->children = g_ptr_array_new ();
2798 DEBUG(1, fprintf (log_file, "[dbg] Setting breakpoint at %s:0x%x.\n", mono_method_full_name (method, TRUE), (int)il_offset));
2800 domain = mono_domain_get ();
2801 mono_domain_lock (domain);
2803 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
2805 set_bp_in_method (domain, method, seq_points, bp);
2807 if (method->is_generic)
2808 /* There might be already JITted instances */
2809 g_hash_table_foreach (domain_jit_info (domain)->seq_points, set_bp_in_method_cb, bp);
2811 /* Not yet JITted */
2815 g_hash_table_foreach (domain_jit_info (domain)->seq_points, set_bp_in_method_cb, bp);
2818 mono_domain_unlock (domain);
2820 mono_loader_lock ();
2821 g_ptr_array_add (breakpoints, bp);
2822 mono_loader_unlock ();
2828 clear_breakpoint (MonoBreakpoint *bp)
2832 // FIXME: locking, races
2833 for (i = 0; i < bp->children->len; ++i) {
2834 BreakpointInstance *inst = g_ptr_array_index (bp->children, i);
2836 remove_breakpoint (inst);
2841 mono_loader_lock ();
2842 g_ptr_array_remove (breakpoints, bp);
2843 mono_loader_unlock ();
2845 g_ptr_array_free (bp->children, TRUE);
2850 process_breakpoint_inner (DebuggerTlsData *tls, MonoContext *ctx)
2853 guint8 *orig_ip, *ip;
2854 int i, j, suspend_policy;
2855 guint32 native_offset;
2857 BreakpointInstance *inst;
2859 GSList *events = NULL;
2860 EventKind kind = EVENT_KIND_BREAKPOINT;
2862 // FIXME: Speed this up
2864 orig_ip = ip = MONO_CONTEXT_GET_IP (ctx);
2865 ji = mono_jit_info_table_find (mono_domain_get (), (char*)ip);
2867 g_assert (ji->method);
2869 /* Compute the native offset of the breakpoint from the ip */
2870 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
2871 ip = mono_arch_get_ip_for_breakpoint (ji, ctx);
2872 native_offset = ip - (guint8*)ji->code_start;
2878 * Skip the instruction causing the breakpoint signal.
2880 mono_arch_skip_breakpoint (ctx);
2882 if (ji->method->wrapper_type || tls->disable_breakpoints)
2885 reqs = g_ptr_array_new ();
2887 DEBUG(1, fprintf (log_file, "[%p] Breakpoint hit, method=%s, offset=0x%x.\n", (gpointer)GetCurrentThreadId (), ji->method->name, native_offset));
2889 mono_loader_lock ();
2892 for (i = 0; i < breakpoints->len; ++i) {
2893 bp = g_ptr_array_index (breakpoints, i);
2898 for (j = 0; j < bp->children->len; ++j) {
2899 inst = g_ptr_array_index (bp->children, j);
2900 if (inst->ji == ji && inst->native_offset == native_offset)
2901 g_ptr_array_add (reqs, bp->req);
2904 if (reqs->len == 0) {
2905 GPtrArray *seq_points;
2906 int seq_il_offset, seq_native_offset;
2907 MonoDomain *domain = mono_domain_get ();
2909 /* Maybe a method entry/exit event */
2910 mono_domain_lock (domain);
2911 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, ji->method);
2912 mono_domain_unlock (domain);
2914 // FIXME: Generic sharing */
2915 mono_loader_unlock ();
2918 g_assert (seq_points);
2920 for (i = 0; i < seq_points->len; i += 2) {
2921 seq_il_offset = GPOINTER_TO_INT (g_ptr_array_index (seq_points, i));
2922 seq_native_offset = GPOINTER_TO_INT (g_ptr_array_index (seq_points, i + 1));
2924 if (native_offset == seq_native_offset) {
2925 if (seq_il_offset == METHOD_ENTRY_IL_OFFSET)
2926 kind = EVENT_KIND_METHOD_ENTRY;
2927 else if (seq_il_offset == METHOD_EXIT_IL_OFFSET)
2928 kind = EVENT_KIND_METHOD_EXIT;
2935 events = create_event_list (EVENT_KIND_BREAKPOINT, reqs, ji, NULL, &suspend_policy);
2936 else if (kind != EVENT_KIND_BREAKPOINT)
2937 events = create_event_list (kind, NULL, ji, NULL, &suspend_policy);
2939 g_ptr_array_free (reqs, TRUE);
2941 mono_loader_unlock ();
2944 process_event (kind, ji->method, 0, ctx, events, suspend_policy);
2948 process_breakpoint (void)
2950 DebuggerTlsData *tls;
2952 static void (*restore_context) (void *);
2954 if (!restore_context)
2955 restore_context = mono_get_restore_context ();
2957 tls = TlsGetValue (debugger_tls_id);
2958 memcpy (&ctx, &tls->handler_ctx, sizeof (MonoContext));
2960 process_breakpoint_inner (tls, &ctx);
2962 /* This is called when resuming from a signal handler, so it shouldn't return */
2963 restore_context (&ctx);
2964 g_assert_not_reached ();
2968 resume_from_signal_handler (void *sigctx, void *func)
2970 DebuggerTlsData *tls;
2973 /* Save the original context in TLS */
2974 // FIXME: This might not work on an altstack ?
2975 tls = TlsGetValue (debugger_tls_id);
2978 // FIXME: MonoContext usually doesn't include the fp registers, so these are
2979 // clobbered by a single step/breakpoint event. If this turns out to be a problem,
2980 // clob:c could be added to op_seq_point.
2982 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
2983 memcpy (&tls->handler_ctx, &ctx, sizeof (MonoContext));
2984 MONO_CONTEXT_SET_IP (&ctx, func);
2986 mono_arch_monoctx_to_sigctx (&ctx, sigctx);
2990 mono_debugger_agent_breakpoint_hit (void *sigctx)
2993 * We are called from a signal handler, and running code there causes all kinds of
2994 * problems, like the original signal is disabled, libgc can't handle altstack, etc.
2995 * So set up the signal context to return to the real breakpoint handler function.
2998 resume_from_signal_handler (sigctx, process_breakpoint);
3002 process_single_step_inner (MonoContext *ctx)
3007 int il_offset, suspend_policy;
3008 MonoDomain *domain = mono_domain_get ();
3011 // FIXME: Speed this up
3013 ip = MONO_CONTEXT_GET_IP (ctx);
3015 /* Skip the instruction causing the single step */
3016 mono_arch_skip_single_step (ctx);
3018 if (suspend_count > 0) {
3019 if (debugger_thread_id == GetCurrentThreadId ())
3022 DEBUG(1, fprintf (log_file, "[%p] Received single step event for suspending.\n", (gpointer)GetCurrentThreadId ()));
3024 ji = mono_jit_info_table_find (mono_domain_get (), (char*)ip);
3026 /* See the comment below */
3027 if (ji->method->klass == mono_defaults.string_class && (!strcmp (ji->method->name, "memset") || strstr (ji->method->name, "memcpy")))
3030 save_thread_context (ctx);
3036 // FIXME: A suspend race
3039 if (mono_thread_internal_current () != ss_req->thread)
3042 if (log_level > 0) {
3043 const char *depth = NULL;
3045 ji = mono_jit_info_table_find (mono_domain_get (), (char*)ip);
3047 switch (ss_req->depth) {
3048 case STEP_DEPTH_OVER:
3051 case STEP_DEPTH_OUT:
3054 case STEP_DEPTH_INTO:
3058 g_assert_not_reached ();
3061 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));
3065 * We implement step over/out by single stepping until we reach the same
3066 * frame/parent frame.
3069 * - stack growing upward
3073 if (ss_req->depth != STEP_DEPTH_INTO) {
3074 if (ss_req->depth == STEP_DEPTH_OVER && MONO_CONTEXT_GET_SP (ctx) < ss_req->last_sp)
3076 if (ss_req->depth == STEP_DEPTH_OUT && MONO_CONTEXT_GET_SP (ctx) <= ss_req->last_sp)
3079 ss_req->last_sp = MONO_CONTEXT_GET_SP (ctx);
3082 ji = mono_jit_info_table_find (mono_domain_get (), (char*)ip);
3084 g_assert (ji->method);
3086 if (ji->method->wrapper_type && ji->method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
3091 * Stopping in memset makes half-initialized vtypes visible.
3092 * Stopping in memcpy makes half-copied vtypes visible.
3094 if (ji->method->klass == mono_defaults.string_class && (!strcmp (ji->method->name, "memset") || strstr (ji->method->name, "memcpy")))
3098 * The ip points to the instruction causing the single step event, convert it
3099 * to the offset stored in seq_points.
3101 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3102 ip = mono_arch_get_ip_for_single_step (ji, ctx);
3104 g_assert_not_reached ();
3108 * mono_debug_lookup_source_location () doesn't work for IL offset 0 for
3109 * example, so do things by hand.
3111 il_offset = compute_il_offset (domain, ji->method, (guint8*)ip - (guint8*)ji->code_start);
3113 if (il_offset == -1)
3116 if (ss_req->size == STEP_SIZE_LINE) {
3117 /* Step until a different source line is reached */
3118 MonoDebugMethodInfo *minfo;
3120 minfo = mono_debug_lookup_method (ji->method);
3123 MonoDebugSourceLocation *loc = mono_debug_symfile_lookup_location (minfo, il_offset);
3125 if (loc && ji->method == ss_req->last_method && loc->row == ss_req->last_line) {
3126 mono_debug_free_source_location (loc);
3131 * Step until we reach a location with line number info,
3132 * otherwise the client can't show a location.
3133 * This can happen for example with statics initialized inline
3134 * outside of a cctor.
3139 ss_req->last_method = ji->method;
3140 ss_req->last_line = loc->row;
3141 mono_debug_free_source_location (loc);
3146 // FIXME: Has to lock earlier
3148 reqs = g_ptr_array_new ();
3150 mono_loader_lock ();
3152 g_ptr_array_add (reqs, ss_req->req);
3154 events = create_event_list (EVENT_KIND_STEP, reqs, ji, NULL, &suspend_policy);
3156 g_ptr_array_free (reqs, TRUE);
3158 mono_loader_unlock ();
3160 process_event (EVENT_KIND_STEP, ji->method, il_offset, ctx, events, suspend_policy);
3164 process_single_step (void)
3166 DebuggerTlsData *tls;
3168 static void (*restore_context) (void *);
3170 if (!restore_context)
3171 restore_context = mono_get_restore_context ();
3173 tls = TlsGetValue (debugger_tls_id);
3174 memcpy (&ctx, &tls->handler_ctx, sizeof (MonoContext));
3176 process_single_step_inner (&ctx);
3178 /* This is called when resuming from a signal handler, so it shouldn't return */
3179 restore_context (&ctx);
3180 g_assert_not_reached ();
3184 * mono_debugger_agent_single_step_event:
3186 * Called from a signal handler to handle a single step event.
3189 mono_debugger_agent_single_step_event (void *sigctx)
3191 /* Resume to process_single_step through the signal context */
3193 // FIXME: Since step out/over is implemented using step in, the step in case should
3194 // be as fast as possible. Move the relevant code from process_single_step_inner ()
3197 if (GetCurrentThreadId () == debugger_thread_id) {
3199 * This could happen despite our best effors when the runtime calls
3200 * assembly/type resolve hooks.
3201 * FIXME: Breakpoints too.
3205 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
3206 mono_arch_skip_single_step (&ctx);
3207 mono_arch_monoctx_to_sigctx (&ctx, sigctx);
3211 resume_from_signal_handler (sigctx, process_single_step);
3215 * start_single_stepping:
3217 * Turn on single stepping. Can be called multiple times, for example,
3218 * by a single step event request + a suspend.
3221 start_single_stepping (void)
3223 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3224 int val = InterlockedIncrement (&ss_count);
3227 mono_arch_start_single_stepping ();
3229 g_assert_not_reached ();
3234 stop_single_stepping (void)
3236 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
3237 int val = InterlockedDecrement (&ss_count);
3240 mono_arch_stop_single_stepping ();
3242 g_assert_not_reached ();
3247 * Start single stepping of thread THREAD
3250 ss_start (MonoInternalThread *thread, StepSize size, StepDepth depth, EventRequest *req)
3252 DebuggerTlsData *tls;
3254 if (suspend_count == 0)
3255 return ERR_NOT_SUSPENDED;
3257 wait_for_suspend ();
3259 // FIXME: Multiple requests
3261 return ERR_NOT_IMPLEMENTED;
3263 ss_req = g_new0 (MonoSingleStepReq, 1);
3265 ss_req->thread = thread;
3266 ss_req->size = size;
3267 ss_req->depth = depth;
3270 mono_loader_lock ();
3271 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
3272 mono_loader_unlock ();
3274 g_assert (tls->has_context);
3275 ss_req->start_sp = ss_req->last_sp = MONO_CONTEXT_GET_SP (&tls->ctx);
3277 if (ss_req->size == STEP_SIZE_LINE) {
3279 MonoDebugMethodInfo *minfo;
3281 /* Compute the initial line info */
3282 compute_frame_info (thread, tls);
3284 g_assert (tls->frame_count);
3285 frame = tls->frames [0];
3287 ss_req->last_method = frame->method;
3288 ss_req->last_line = -1;
3290 minfo = mono_debug_lookup_method (frame->method);
3291 if (minfo && frame->il_offset != -1) {
3292 MonoDebugSourceLocation *loc = mono_debug_symfile_lookup_location (minfo, frame->il_offset);
3295 ss_req->last_line = loc->row;
3301 start_single_stepping ();
3307 ss_stop (EventRequest *req)
3311 g_assert (ss_req->req == req);
3316 stop_single_stepping ();
3320 mono_debugger_agent_handle_exception (MonoException *exc, MonoContext *ctx)
3326 /* Just-In-Time debugging */
3327 if (agent_config.onthrow && !inited) {
3329 gboolean found = FALSE;
3331 for (l = agent_config.onthrow; l; l = l->next) {
3332 char *ex_type = l->data;
3333 char *f = mono_type_full_name (&exc->object.vtable->klass->byval_arg);
3335 if (!strcmp (ex_type, f))
3342 finish_agent_init (FALSE);
3345 * Send an unsolicited EXCEPTION event with a dummy request id.
3347 events = g_slist_append (NULL, GUINT_TO_POINTER (0xffffff));
3348 process_event (EVENT_KIND_EXCEPTION, exc, 0, ctx, events, SUSPEND_POLICY_ALL);
3356 ji = mini_jit_info_table_find (mono_domain_get (), MONO_CONTEXT_GET_IP (ctx), NULL);
3358 mono_loader_lock ();
3359 events = create_event_list (EVENT_KIND_EXCEPTION, NULL, ji, exc, &suspend_policy);
3360 mono_loader_unlock ();
3362 process_event (EVENT_KIND_EXCEPTION, exc, 0, ctx, events, suspend_policy);
3366 mono_debugger_agent_handle_unhandled_exception (MonoException *exc, MonoContext *ctx)
3370 if (!agent_config.onuncaught)
3373 finish_agent_init (FALSE);
3376 * Send an unsolicited EXCEPTION event with a dummy request id.
3378 events = g_slist_append (NULL, GUINT_TO_POINTER (0xffffff));
3379 process_event (EVENT_KIND_EXCEPTION, exc, 0, ctx, events, SUSPEND_POLICY_ALL);
3385 * Add the encoding of the value at ADDR described by T to the buffer.
3388 buffer_add_value (Buffer *buf, MonoType *t, void *addr, MonoDomain *domain)
3393 g_assert (*(void**)addr);
3394 addr = *(void**)addr;
3398 case MONO_TYPE_VOID:
3399 buffer_add_byte (buf, t->type);
3401 case MONO_TYPE_BOOLEAN:
3404 buffer_add_byte (buf, t->type);
3405 buffer_add_int (buf, *(gint8*)addr);
3407 case MONO_TYPE_CHAR:
3410 buffer_add_byte (buf, t->type);
3411 buffer_add_int (buf, *(gint16*)addr);
3416 buffer_add_byte (buf, t->type);
3417 buffer_add_int (buf, *(gint32*)addr);
3422 buffer_add_byte (buf, t->type);
3423 buffer_add_long (buf, *(gint64*)addr);
3427 case MONO_TYPE_PTR: {
3428 gssize val = *(gssize*)addr;
3430 buffer_add_byte (buf, t->type);
3431 buffer_add_long (buf, val);
3435 case MONO_TYPE_STRING:
3436 case MONO_TYPE_SZARRAY:
3437 case MONO_TYPE_OBJECT:
3438 case MONO_TYPE_CLASS:
3439 case MONO_TYPE_ARRAY:
3440 obj = *(MonoObject**)addr;
3443 buffer_add_byte (buf, VALUE_TYPE_ID_NULL);
3445 if (obj->vtable->klass->valuetype) {
3446 t = &obj->vtable->klass->byval_arg;
3447 addr = mono_object_unbox (obj);
3449 } else if (obj->vtable->klass->rank) {
3450 buffer_add_byte (buf, obj->vtable->klass->byval_arg.type);
3451 } else if (obj->vtable->klass->byval_arg.type == MONO_TYPE_GENERICINST) {
3452 buffer_add_byte (buf, MONO_TYPE_CLASS);
3454 buffer_add_byte (buf, obj->vtable->klass->byval_arg.type);
3456 buffer_add_objid (buf, obj);
3460 case MONO_TYPE_VALUETYPE: {
3464 MonoClass *klass = mono_class_from_mono_type (t);
3466 buffer_add_byte (buf, MONO_TYPE_VALUETYPE);
3467 buffer_add_byte (buf, klass->enumtype);
3468 buffer_add_typeid (buf, domain, klass);
3472 while ((f = mono_class_get_fields (klass, &iter))) {
3473 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC)
3475 if (mono_field_is_deleted (f))
3479 buffer_add_int (buf, nfields);
3482 while ((f = mono_class_get_fields (klass, &iter))) {
3483 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC)
3485 if (mono_field_is_deleted (f))
3487 buffer_add_value (buf, f->type, (guint8*)addr + f->offset - sizeof (MonoObject), domain);
3491 case MONO_TYPE_GENERICINST:
3492 if (mono_type_generic_inst_is_valuetype (t)) {
3504 decode_value (MonoType *t, MonoDomain *domain, guint8 *addr, guint8 *buf, guint8 **endbuf, guint8 *limit)
3507 int type = decode_byte (buf, &buf, limit);
3509 if (type != t->type && !MONO_TYPE_IS_REFERENCE (t)) {
3510 DEBUG(1, fprintf (log_file, "Expected value of type %d, got %d.\n", t->type, type));
3511 return ERR_INVALID_ARGUMENT;
3515 case MONO_TYPE_BOOLEAN:
3516 *(guint8*)addr = decode_int (buf, &buf, limit);
3518 case MONO_TYPE_CHAR:
3519 *(gunichar2*)addr = decode_int (buf, &buf, limit);
3522 *(gint8*)addr = decode_int (buf, &buf, limit);
3525 *(guint8*)addr = decode_int (buf, &buf, limit);
3528 *(gint16*)addr = decode_int (buf, &buf, limit);
3531 *(guint16*)addr = decode_int (buf, &buf, limit);
3534 *(gint32*)addr = decode_int (buf, &buf, limit);
3537 *(guint32*)addr = decode_int (buf, &buf, limit);
3540 *(gint64*)addr = decode_long (buf, &buf, limit);
3543 *(guint64*)addr = decode_long (buf, &buf, limit);
3546 *(guint32*)addr = decode_int (buf, &buf, limit);
3549 *(guint64*)addr = decode_long (buf, &buf, limit);
3551 case MONO_TYPE_VALUETYPE: {
3552 gboolean is_enum = decode_byte (buf, &buf, limit);
3556 gpointer iter = NULL;
3559 /* Enums are sent as a normal vtype */
3561 return ERR_NOT_IMPLEMENTED;
3562 klass = decode_typeid (buf, &buf, limit, &d, &err);
3566 if (klass != mono_class_from_mono_type (t))
3567 return ERR_INVALID_ARGUMENT;
3569 nfields = decode_int (buf, &buf, limit);
3570 while ((f = mono_class_get_fields (klass, &iter))) {
3571 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC)
3573 if (mono_field_is_deleted (f))
3575 err = decode_value (f->type, domain, (guint8*)addr + f->offset - sizeof (MonoObject), buf, &buf, limit);
3580 g_assert (nfields == 0);
3584 if (MONO_TYPE_IS_REFERENCE (t)) {
3585 if (type == MONO_TYPE_OBJECT) {
3586 int objid = decode_objid (buf, &buf, limit);
3590 err = get_object (objid, (MonoObject**)&obj);
3594 if (obj && !mono_class_is_assignable_from (mono_class_from_mono_type (t), obj->vtable->klass))
3595 return ERR_INVALID_ARGUMENT;
3596 if (obj && obj->vtable->domain != domain)
3597 return ERR_INVALID_ARGUMENT;
3599 mono_gc_wbarrier_generic_store (addr, obj);
3600 } else if (type == VALUE_TYPE_ID_NULL) {
3601 *(MonoObject**)addr = NULL;
3603 return ERR_INVALID_ARGUMENT;
3617 add_var (Buffer *buf, MonoType *t, MonoDebugVarInfo *var, MonoContext *ctx, MonoDomain *domain)
3624 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
3625 reg = var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
3628 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
3629 reg_val = mono_arch_context_get_int_reg (ctx, reg);
3631 buffer_add_value (buf, t, ®_val, domain);
3633 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
3634 addr = mono_arch_context_get_int_reg (ctx, reg);
3635 addr += (gint32)var->offset;
3637 //printf ("[R%d+%d] = %p\n", reg, var->offset, addr);
3639 buffer_add_value (buf, t, addr, domain);
3641 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
3645 g_assert_not_reached ();
3650 set_var (MonoType *t, MonoDebugVarInfo *var, MonoContext *ctx, MonoDomain *domain, guint8 *val)
3656 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
3657 reg = var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
3659 if (MONO_TYPE_IS_REFERENCE (t))
3660 size = sizeof (gpointer);
3662 size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
3665 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
3666 // FIXME: Can't set registers, so we disable linears
3669 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
3670 addr = mono_arch_context_get_int_reg (ctx, reg);
3671 addr += (gint32)var->offset;
3673 //printf ("[R%d+%d] = %p\n", reg, var->offset, addr);
3675 // FIXME: Write barriers
3676 memcpy (addr, val, size);
3678 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
3682 g_assert_not_reached ();
3687 clear_event_request (int req_id, int etype)
3691 mono_loader_lock ();
3692 for (i = 0; i < event_requests->len; ++i) {
3693 EventRequest *req = g_ptr_array_index (event_requests, i);
3695 if (req->id == req_id && req->event_kind == etype) {
3696 if (req->event_kind == EVENT_KIND_BREAKPOINT)
3697 clear_breakpoint (req->info);
3698 if (req->event_kind == EVENT_KIND_STEP)
3700 g_ptr_array_remove_index_fast (event_requests, i);
3705 mono_loader_unlock ();
3709 add_thread (gpointer key, gpointer value, gpointer user_data)
3711 MonoInternalThread *thread = value;
3712 Buffer *buf = user_data;
3714 buffer_add_objid (buf, (MonoObject*)thread);
3718 do_invoke_method (DebuggerTlsData *tls, Buffer *buf, InvokeData *invoke)
3720 guint8 *p = invoke->p;
3721 guint8 *end = invoke->endp;
3724 MonoMethodSignature *sig;
3727 MonoObject *this, *res, *exc;
3730 #ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
3734 m = decode_methodid (p, &p, end, &domain, &err);
3737 sig = mono_method_signature (m);
3739 if (m->klass->valuetype)
3740 this_buf = g_alloca (mono_class_instance_size (m->klass));
3742 this_buf = g_alloca (sizeof (MonoObject*));
3743 err = decode_value (&m->klass->byval_arg, domain, this_buf, p, &p, end);
3747 if (!m->klass->valuetype)
3748 this = *(MonoObject**)this_buf;
3752 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>"));
3754 if (this && this->vtable->domain != domain)
3757 if (!m->klass->valuetype && !(m->flags & METHOD_ATTRIBUTE_STATIC) && !this) {
3758 if (!strcmp (m->name, ".ctor")) {
3759 if (m->klass->flags & TYPE_ATTRIBUTE_ABSTRACT)
3760 return ERR_INVALID_ARGUMENT;
3762 this = mono_object_new (domain, m->klass);
3764 return ERR_INVALID_ARGUMENT;
3768 if (this && !mono_class_is_assignable_from (m->klass, this->vtable->klass))
3769 return ERR_INVALID_ARGUMENT;
3771 nargs = decode_int (p, &p, end);
3772 if (nargs != sig->param_count)
3773 return ERR_INVALID_ARGUMENT;
3774 /* Use alloca to get gc tracking */
3775 arg_buf = g_alloca (nargs * sizeof (gpointer));
3776 memset (arg_buf, 0, nargs * sizeof (gpointer));
3777 args = g_alloca (nargs * sizeof (gpointer));
3778 for (i = 0; i < nargs; ++i) {
3779 if (MONO_TYPE_IS_REFERENCE (sig->params [i])) {
3780 err = decode_value (sig->params [i], domain, (guint8*)&args [i], p, &p, end);
3784 if (args [i] && ((MonoObject*)args [i])->vtable->domain != domain)
3787 arg_buf [i] = g_alloca (mono_class_instance_size (mono_class_from_mono_type (sig->params [i])));
3788 err = decode_value (sig->params [i], domain, arg_buf [i], p, &p, end);
3791 args [i] = arg_buf [i];
3798 if (invoke->flags & INVOKE_FLAG_DISABLE_BPS)
3799 tls->disable_breakpoints = TRUE;
3801 tls->disable_breakpoints = FALSE;
3804 * Add an LMF frame to link the stack frames on the invoke method with our caller.
3806 /* FIXME: Move this to arch specific code */
3807 #ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
3808 if (invoke->has_ctx) {
3811 lmf_addr = mono_get_lmf_addr ();
3814 memset (&ext, 0, sizeof (ext));
3816 ext.lmf.previous_lmf = *(lmf_addr);
3817 /* Mark that this is a MonoLMFExt */
3818 ext.lmf.previous_lmf = (gpointer)(((gssize)ext.lmf.previous_lmf) | 2);
3819 ext.lmf.rsp = (gssize)&ext;
3820 #elif defined(TARGET_X86)
3821 ext.lmf.previous_lmf = (gsize)*(lmf_addr);
3822 /* Mark that this is a MonoLMFExt */
3823 ext.lmf.previous_lmf = (gsize)(gpointer)(((gssize)ext.lmf.previous_lmf) | 2);
3824 ext.lmf.ebp = (gssize)&ext;
3825 #elif defined(TARGET_ARM)
3826 ext.lmf.previous_lmf = *(lmf_addr);
3827 /* Mark that this is a MonoLMFExt */
3828 ext.lmf.previous_lmf = (gpointer)(((gssize)ext.lmf.previous_lmf) | 2);
3829 ext.lmf.ebp = (gssize)&ext;
3831 g_assert_not_reached ();
3834 ext.debugger_invoke = TRUE;
3835 memcpy (&ext.ctx, &invoke->ctx, sizeof (MonoContext));
3837 mono_set_lmf ((MonoLMF*)&ext);
3841 if (m->klass->valuetype)
3842 res = mono_runtime_invoke (m, this_buf, args, &exc);
3844 res = mono_runtime_invoke (m, this, args, &exc);
3846 buffer_add_byte (buf, 0);
3847 buffer_add_value (buf, &mono_defaults.object_class->byval_arg, &exc, domain);
3849 buffer_add_byte (buf, 1);
3850 if (sig->ret->type == MONO_TYPE_VOID) {
3851 if (!strcmp (m->name, ".ctor") && !m->klass->valuetype) {
3852 buffer_add_value (buf, &mono_defaults.object_class->byval_arg, &this, domain);
3855 buffer_add_value (buf, &mono_defaults.void_class->byval_arg, NULL, domain);
3856 } else if (MONO_TYPE_IS_REFERENCE (sig->ret)) {
3857 buffer_add_value (buf, sig->ret, &res, domain);
3858 } else if (mono_class_from_mono_type (sig->ret)->valuetype) {
3860 buffer_add_value (buf, sig->ret, mono_object_unbox (res), domain);
3866 tls->disable_breakpoints = FALSE;
3868 #ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
3869 if (invoke->has_ctx)
3870 mono_set_lmf ((gpointer)(((gssize)ext.lmf.previous_lmf) & ~3));
3873 // FIXME: byref arguments
3881 * Invoke the method given by tls->invoke in the current thread.
3884 invoke_method (void)
3886 DebuggerTlsData *tls;
3891 static void (*restore_context) (void *);
3892 MonoContext restore_ctx;
3894 if (!restore_context)
3895 restore_context = mono_get_restore_context ();
3897 tls = TlsGetValue (debugger_tls_id);
3900 invoke = tls->invoke;
3904 tls->frames_up_to_date = FALSE;
3908 buffer_init (&buf, 128);
3910 err = do_invoke_method (tls, &buf, invoke);
3912 /* Start suspending before sending the reply */
3915 send_reply_packet (id, err, &buf);
3919 memcpy (&restore_ctx, &invoke->ctx, sizeof (MonoContext));
3921 if (invoke->has_ctx)
3922 save_thread_context (&restore_ctx);
3931 vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
3934 case CMD_VM_VERSION: {
3935 char *build_info, *version;
3937 build_info = mono_get_runtime_build_info ();
3938 version = g_strdup_printf ("mono %s", build_info);
3940 buffer_add_string (buf, version); /* vm version */
3941 buffer_add_int (buf, MAJOR_VERSION);
3942 buffer_add_int (buf, MINOR_VERSION);
3943 g_free (build_info);
3947 case CMD_VM_ALL_THREADS: {
3949 mono_loader_lock ();
3950 buffer_add_int (buf, mono_g_hash_table_size (tid_to_thread_obj));
3951 mono_g_hash_table_foreach (tid_to_thread_obj, add_thread, buf);
3952 mono_loader_unlock ();
3955 case CMD_VM_SUSPEND:
3957 wait_for_suspend ();
3960 if (suspend_count == 0)
3961 return ERR_NOT_SUSPENDED;
3964 case CMD_VM_DISPOSE:
3965 /* Clear all event requests */
3966 mono_loader_lock ();
3967 while (event_requests->len > 0) {
3968 EventRequest *req = g_ptr_array_index (event_requests, 0);
3970 clear_event_request (req->id, req->event_kind);
3972 mono_loader_unlock ();
3974 // FIXME: Count resumes
3976 disconnected = TRUE;
3979 int exit_code = decode_int (p, &p, end);
3981 // FIXME: What if there is a VM_DEATH event request with SUSPEND_ALL ?
3983 /* Have to send a reply before exiting */
3984 send_reply_packet (id, 0, buf);
3986 /* Clear all event requests */
3987 mono_loader_lock ();
3988 while (event_requests->len > 0) {
3989 EventRequest *req = g_ptr_array_index (event_requests, 0);
3991 clear_event_request (req->id, req->event_kind);
3993 mono_loader_unlock ();
3995 /* FIXME: Races with normal shutdown */
3996 while (suspend_count > 0)
4000 * The JDWP documentation says that the shutdown is not orderly. It doesn't
4001 * specify whenever a VM_DEATH event is sent. We currently do an orderly
4002 * shutdown similar to Environment.Exit ().
4004 mono_runtime_set_shutting_down ();
4006 mono_threads_set_shutting_down ();
4008 /* Suspend all managed threads since the runtime is going away */
4009 DEBUG(1, fprintf (log_file, "Suspending all threads...\n"));
4010 mono_thread_suspend_all_other_threads ();
4011 DEBUG(1, fprintf (log_file, "Shutting down the runtime...\n"));
4012 mono_runtime_quit ();
4014 shutdown (conn_fd, SD_BOTH);
4016 shutdown (conn_fd, SHUT_RDWR);
4018 DEBUG(1, fprintf (log_file, "Exiting...\n"));
4022 case CMD_VM_INVOKE_METHOD: {
4023 int objid = decode_objid (p, &p, end);
4025 DebuggerTlsData *tls;
4028 err = get_object (objid, (MonoObject**)&thread);
4032 flags = decode_int (p, &p, end);
4034 // Wait for suspending if it already started
4036 wait_for_suspend ();
4037 if (!is_suspended ())
4038 return ERR_NOT_SUSPENDED;
4040 mono_loader_lock ();
4041 tls = mono_g_hash_table_lookup (thread_to_tls, THREAD_TO_INTERNAL (thread));
4042 mono_loader_unlock ();
4046 * Store the invoke data into tls, the thread will execute it after it is
4051 tls->invoke = g_new0 (InvokeData, 1);
4052 tls->invoke->id = id;
4053 tls->invoke->flags = flags;
4054 tls->invoke->p = g_malloc (end - p);
4055 memcpy (tls->invoke->p, p, end - p);
4056 tls->invoke->endp = tls->invoke->p + (end - p);
4062 return ERR_NOT_IMPLEMENTED;
4069 event_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
4074 case CMD_EVENT_REQUEST_SET: {
4076 int i, event_kind, suspend_policy, nmodifiers, mod;
4079 MonoThread *step_thread;
4080 int size = 0, depth = 0, step_thread_id = 0;
4083 event_kind = decode_byte (p, &p, end);
4084 suspend_policy = decode_byte (p, &p, end);
4085 nmodifiers = decode_byte (p, &p, end);
4087 req = g_malloc0 (sizeof (EventRequest) + (nmodifiers * sizeof (Modifier)));
4088 req->id = InterlockedIncrement (&event_request_id);
4089 req->event_kind = event_kind;
4090 req->suspend_policy = suspend_policy;
4091 req->nmodifiers = nmodifiers;
4094 for (i = 0; i < nmodifiers; ++i) {
4095 mod = decode_byte (p, &p, end);
4097 req->modifiers [i].kind = mod;
4098 if (mod == MOD_KIND_COUNT) {
4099 req->modifiers [i].data.count = decode_int (p, &p, end);
4100 } else if (mod == MOD_KIND_LOCATION_ONLY) {
4101 method = decode_methodid (p, &p, end, &domain, &err);
4104 location = decode_long (p, &p, end);
4105 } else if (mod == MOD_KIND_STEP) {
4106 step_thread_id = decode_id (p, &p, end);
4107 size = decode_int (p, &p, end);
4108 depth = decode_int (p, &p, end);
4109 } else if (mod == MOD_KIND_THREAD_ONLY) {
4110 int id = decode_id (p, &p, end);
4112 err = get_object (id, (MonoObject**)&req->modifiers [i].data.thread);
4117 } else if (mod == MOD_KIND_EXCEPTION_ONLY) {
4118 MonoClass *exc_class = decode_typeid (p, &p, end, &domain, &err);
4123 req->modifiers [i].data.exc_class = exc_class;
4125 if (!mono_class_is_assignable_from (mono_defaults.exception_class, exc_class)) {
4127 return ERR_INVALID_ARGUMENT;
4130 } else if (mod == MOD_KIND_ASSEMBLY_ONLY) {
4131 int n = decode_int (p, &p, end);
4134 req->modifiers [i].data.assemblies = g_new0 (MonoAssembly*, n);
4135 for (j = 0; j < n; ++j) {
4136 req->modifiers [i].data.assemblies [j] = decode_assemblyid (p, &p, end, &domain, &err);
4138 g_free (req->modifiers [i].data.assemblies);
4144 return ERR_NOT_IMPLEMENTED;
4148 if (req->event_kind == EVENT_KIND_BREAKPOINT) {
4151 req->info = set_breakpoint (method, location, req);
4152 } else if (req->event_kind == EVENT_KIND_STEP) {
4153 g_assert (step_thread_id);
4155 err = get_object (step_thread_id, (MonoObject**)&step_thread);
4161 err = ss_start (THREAD_TO_INTERNAL (step_thread), size, depth, req);
4166 } else if (req->event_kind == EVENT_KIND_METHOD_ENTRY) {
4167 req->info = set_breakpoint (NULL, METHOD_ENTRY_IL_OFFSET, req);
4168 } else if (req->event_kind == EVENT_KIND_METHOD_EXIT) {
4169 req->info = set_breakpoint (NULL, METHOD_EXIT_IL_OFFSET, req);
4170 } else if (req->event_kind == EVENT_KIND_EXCEPTION) {
4172 if (req->nmodifiers) {
4174 return ERR_NOT_IMPLEMENTED;
4178 mono_loader_lock ();
4179 g_ptr_array_add (event_requests, req);
4180 mono_loader_unlock ();
4182 buffer_add_int (buf, req->id);
4185 case CMD_EVENT_REQUEST_CLEAR: {
4186 int etype = decode_byte (p, &p, end);
4187 int req_id = decode_int (p, &p, end);
4189 // FIXME: Make a faster mapping from req_id to request
4190 mono_loader_lock ();
4191 clear_event_request (req_id, etype);
4192 mono_loader_unlock ();
4195 case CMD_EVENT_REQUEST_CLEAR_ALL_BREAKPOINTS: {
4198 mono_loader_lock ();
4200 while (i < event_requests->len) {
4201 EventRequest *req = g_ptr_array_index (event_requests, i);
4203 if (req->event_kind == EVENT_KIND_BREAKPOINT) {
4204 clear_breakpoint (req->info);
4206 g_ptr_array_remove_index_fast (event_requests, i);
4212 mono_loader_unlock ();
4216 return ERR_NOT_IMPLEMENTED;
4223 domain_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
4229 case CMD_APPDOMAIN_GET_ROOT_DOMAIN: {
4230 buffer_add_domainid (buf, mono_get_root_domain ());
4233 case CMD_APPDOMAIN_GET_FRIENDLY_NAME: {
4234 domain = decode_domainid (p, &p, end, NULL, &err);
4237 buffer_add_string (buf, domain->friendly_name);
4240 case CMD_APPDOMAIN_GET_ASSEMBLIES: {
4245 domain = decode_domainid (p, &p, end, NULL, &err);
4248 mono_loader_lock ();
4250 for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
4253 buffer_add_int (buf, count);
4254 for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
4256 buffer_add_assemblyid (buf, domain, ass);
4258 mono_loader_unlock ();
4261 case CMD_APPDOMAIN_GET_ENTRY_ASSEMBLY: {
4262 domain = decode_domainid (p, &p, end, NULL, &err);
4266 buffer_add_assemblyid (buf, domain, domain->entry_assembly);
4269 case CMD_APPDOMAIN_GET_CORLIB: {
4270 domain = decode_domainid (p, &p, end, NULL, &err);
4274 buffer_add_assemblyid (buf, domain, domain->domain->mbr.obj.vtable->klass->image->assembly);
4277 case CMD_APPDOMAIN_CREATE_STRING: {
4281 domain = decode_domainid (p, &p, end, NULL, &err);
4284 s = decode_string (p, &p, end);
4286 o = mono_string_new (domain, s);
4287 buffer_add_objid (buf, (MonoObject*)o);
4291 return ERR_NOT_IMPLEMENTED;
4298 assembly_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
4304 ass = decode_assemblyid (p, &p, end, &domain, &err);
4309 case CMD_ASSEMBLY_GET_LOCATION: {
4310 buffer_add_string (buf, mono_image_get_filename (ass->image));
4313 case CMD_ASSEMBLY_GET_ENTRY_POINT: {
4317 if (ass->image->dynamic) {
4318 buffer_add_id (buf, 0);
4320 token = mono_image_get_entry_point (ass->image);
4322 buffer_add_id (buf, 0);
4324 m = mono_get_method (ass->image, token, NULL);
4325 buffer_add_methodid (buf, domain, m);
4330 case CMD_ASSEMBLY_GET_MANIFEST_MODULE: {
4331 buffer_add_moduleid (buf, domain, ass->image);
4334 case CMD_ASSEMBLY_GET_OBJECT: {
4335 MonoObject *o = (MonoObject*)mono_assembly_get_object (mono_domain_get (), ass);
4336 buffer_add_objid (buf, o);
4339 case CMD_ASSEMBLY_GET_TYPE: {
4340 char *s = decode_string (p, &p, end);
4341 gboolean ignorecase = decode_byte (p, &p, end);
4342 MonoTypeNameParse info;
4344 gboolean type_resolve;
4346 if (!mono_reflection_parse_type (s, &info)) {
4349 if (info.assembly.name)
4351 t = mono_reflection_get_type (ass->image, &info, ignorecase, &type_resolve);
4353 buffer_add_typeid (buf, domain, t ? mono_class_from_mono_type (t) : NULL);
4354 mono_reflection_free_type_info (&info);
4359 case CMD_ASSEMBLY_GET_NAME: {
4361 MonoAssembly *mass = ass;
4363 name = g_strdup_printf (
4364 "%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
4366 mass->aname.major, mass->aname.minor, mass->aname.build, mass->aname.revision,
4367 mass->aname.culture && *mass->aname.culture? mass->aname.culture: "neutral",
4368 mass->aname.public_key_token [0] ? (char *)mass->aname.public_key_token : "null",
4369 (mass->aname.flags & ASSEMBLYREF_RETARGETABLE_FLAG) ? ", Retargetable=Yes" : "");
4371 buffer_add_string (buf, name);
4376 return ERR_NOT_IMPLEMENTED;
4383 module_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
4389 case CMD_MODULE_GET_INFO: {
4390 MonoImage *image = decode_moduleid (p, &p, end, &domain, &err);
4393 basename = g_path_get_basename (image->name);
4394 buffer_add_string (buf, basename); // name
4395 buffer_add_string (buf, image->module_name); // scopename
4396 buffer_add_string (buf, image->name); // fqname
4397 buffer_add_string (buf, mono_image_get_guid (image)); // guid
4398 buffer_add_assemblyid (buf, domain, image->assembly); // assembly
4403 return ERR_NOT_IMPLEMENTED;
4410 buffer_add_cattr_arg (Buffer *buf, MonoType *t, MonoDomain *domain, MonoObject *val)
4412 if (val && val->vtable->klass == mono_defaults.monotype_class) {
4413 /* Special case these so the client doesn't have to handle Type objects */
4415 buffer_add_byte (buf, VALUE_TYPE_ID_TYPE);
4416 buffer_add_typeid (buf, domain, mono_class_from_mono_type (((MonoReflectionType*)val)->type));
4417 } else if (MONO_TYPE_IS_REFERENCE (t))
4418 buffer_add_value (buf, t, &val, domain);
4420 buffer_add_value (buf, t, mono_object_unbox (val), domain);
4424 buffer_add_cattrs (Buffer *buf, MonoDomain *domain, MonoImage *image, MonoClass *attr_klass, MonoCustomAttrInfo *cinfo)
4430 buffer_add_int (buf, 0);
4434 for (i = 0; i < cinfo->num_attrs; ++i) {
4435 if (!attr_klass || mono_class_has_parent (cinfo->attrs [i].ctor->klass, attr_klass))
4438 buffer_add_int (buf, nattrs);
4440 for (i = 0; i < cinfo->num_attrs; ++i) {
4441 MonoCustomAttrEntry *attr = &cinfo->attrs [i];
4442 if (!attr_klass || mono_class_has_parent (attr->ctor->klass, attr_klass)) {
4443 MonoArray *typed_args, *named_args;
4445 CattrNamedArg *arginfo;
4447 mono_reflection_create_custom_attr_data_args (image, attr->ctor, attr->data, attr->data_size, &typed_args, &named_args, &arginfo);
4449 buffer_add_methodid (buf, domain, attr->ctor);
4453 buffer_add_int (buf, mono_array_length (typed_args));
4454 for (j = 0; j < mono_array_length (typed_args); ++j) {
4455 MonoObject *val = mono_array_get (typed_args, MonoObject*, j);
4457 t = mono_method_signature (attr->ctor)->params [j];
4459 buffer_add_cattr_arg (buf, t, domain, val);
4462 buffer_add_int (buf, 0);
4467 buffer_add_int (buf, mono_array_length (named_args));
4469 for (j = 0; j < mono_array_length (named_args); ++j) {
4470 MonoObject *val = mono_array_get (named_args, MonoObject*, j);
4472 if (arginfo [j].prop) {
4473 buffer_add_byte (buf, 0x54);
4474 buffer_add_propertyid (buf, domain, arginfo [j].prop);
4475 } else if (arginfo [j].field) {
4476 buffer_add_byte (buf, 0x53);
4478 g_assert_not_reached ();
4481 buffer_add_cattr_arg (buf, arginfo [j].type, domain, val);
4484 buffer_add_int (buf, 0);
4491 type_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
4502 klass = decode_typeid (p, &p, end, &domain, &err);
4507 case CMD_TYPE_GET_INFO: {
4508 buffer_add_string (buf, klass->name_space);
4509 buffer_add_string (buf, klass->name);
4511 name = mono_type_get_name_full (&klass->byval_arg, MONO_TYPE_NAME_FORMAT_FULL_NAME);
4512 buffer_add_string (buf, name);
4514 buffer_add_assemblyid (buf, domain, klass->image->assembly);
4515 buffer_add_moduleid (buf, domain, klass->image);
4516 buffer_add_typeid (buf, domain, klass->parent);
4517 if (klass->rank || klass->byval_arg.type == MONO_TYPE_PTR)
4518 buffer_add_typeid (buf, domain, klass->element_class);
4520 buffer_add_id (buf, 0);
4521 buffer_add_int (buf, klass->type_token);
4522 buffer_add_byte (buf, klass->rank);
4523 buffer_add_int (buf, klass->flags);
4525 type = &klass->byval_arg;
4526 // FIXME: Can't decide whenever a class represents a byref type
4529 if (type->type == MONO_TYPE_PTR)
4531 if (!type->byref && (((type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8)) || (type->type == MONO_TYPE_I) || (type->type == MONO_TYPE_U)))
4533 if (type->type == MONO_TYPE_VALUETYPE)
4535 buffer_add_byte (buf, b);
4538 while ((nested = mono_class_get_nested_types (klass, &iter)))
4540 buffer_add_int (buf, nnested);
4542 while ((nested = mono_class_get_nested_types (klass, &iter)))
4543 buffer_add_typeid (buf, domain, nested);
4546 case CMD_TYPE_GET_METHODS: {
4549 gpointer iter = NULL;
4552 nmethods = mono_class_num_methods (klass);
4554 buffer_add_int (buf, nmethods);
4556 while ((m = mono_class_get_methods (klass, &iter))) {
4557 buffer_add_methodid (buf, domain, m);
4560 g_assert (i == nmethods);
4563 case CMD_TYPE_GET_FIELDS: {
4566 gpointer iter = NULL;
4569 nfields = mono_class_num_fields (klass);
4571 buffer_add_int (buf, nfields);
4573 while ((f = mono_class_get_fields (klass, &iter))) {
4574 buffer_add_fieldid (buf, domain, f);
4575 buffer_add_string (buf, f->name);
4576 buffer_add_typeid (buf, domain, mono_class_from_mono_type (f->type));
4577 buffer_add_int (buf, f->type->attrs);
4580 g_assert (i == nfields);
4583 case CMD_TYPE_GET_PROPERTIES: {
4586 gpointer iter = NULL;
4589 nprops = mono_class_num_properties (klass);
4591 buffer_add_int (buf, nprops);
4593 while ((p = mono_class_get_properties (klass, &iter))) {
4594 buffer_add_propertyid (buf, domain, p);
4595 buffer_add_string (buf, p->name);
4596 buffer_add_methodid (buf, domain, p->get);
4597 buffer_add_methodid (buf, domain, p->set);
4598 buffer_add_int (buf, p->attrs);
4601 g_assert (i == nprops);
4604 case CMD_TYPE_GET_CATTRS: {
4605 MonoClass *attr_klass = decode_typeid (p, &p, end, NULL, &err);
4606 MonoCustomAttrInfo *cinfo;
4608 cinfo = mono_custom_attrs_from_class (klass);
4610 buffer_add_cattrs (buf, domain, klass->image, attr_klass, cinfo);
4613 case CMD_TYPE_GET_FIELD_CATTRS: {
4614 MonoClass *attr_klass;
4615 MonoCustomAttrInfo *cinfo;
4616 MonoClassField *field;
4618 field = decode_fieldid (p, &p, end, NULL, &err);
4621 attr_klass = decode_typeid (p, &p, end, NULL, &err);
4625 cinfo = mono_custom_attrs_from_field (klass, field);
4627 buffer_add_cattrs (buf, domain, klass->image, attr_klass, cinfo);
4630 case CMD_TYPE_GET_PROPERTY_CATTRS: {
4631 MonoClass *attr_klass;
4632 MonoCustomAttrInfo *cinfo;
4635 prop = decode_propertyid (p, &p, end, NULL, &err);
4638 attr_klass = decode_typeid (p, &p, end, NULL, &err);
4642 cinfo = mono_custom_attrs_from_property (klass, prop);
4644 buffer_add_cattrs (buf, domain, klass->image, attr_klass, cinfo);
4647 case CMD_TYPE_GET_VALUES: {
4655 len = decode_int (p, &p, end);
4656 for (i = 0; i < len; ++i) {
4657 f = decode_fieldid (p, &p, end, NULL, &err);
4661 if (!(f->type->attrs & FIELD_ATTRIBUTE_STATIC))
4662 return ERR_INVALID_FIELDID;
4663 if (mono_class_field_is_special_static (f))
4664 return ERR_INVALID_FIELDID;
4666 /* Check that the field belongs to the object */
4668 for (k = klass; k; k = k->parent) {
4669 if (k == f->parent) {
4675 return ERR_INVALID_FIELDID;
4677 vtable = mono_class_vtable (domain, f->parent);
4678 val = g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
4679 mono_field_static_get_value (vtable, f, val);
4680 buffer_add_value (buf, f->type, val, domain);
4685 case CMD_TYPE_SET_VALUES: {
4693 len = decode_int (p, &p, end);
4694 for (i = 0; i < len; ++i) {
4695 f = decode_fieldid (p, &p, end, NULL, &err);
4699 if (!(f->type->attrs & FIELD_ATTRIBUTE_STATIC))
4700 return ERR_INVALID_FIELDID;
4701 if (mono_class_field_is_special_static (f))
4702 return ERR_INVALID_FIELDID;
4704 /* Check that the field belongs to the object */
4706 for (k = klass; k; k = k->parent) {
4707 if (k == f->parent) {
4713 return ERR_INVALID_FIELDID;
4715 // FIXME: Check for literal/const
4717 vtable = mono_class_vtable (domain, f->parent);
4718 val = g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
4719 err = decode_value (f->type, domain, val, p, &p, end);
4724 mono_field_static_set_value (vtable, f, val);
4729 case CMD_TYPE_GET_OBJECT: {
4730 MonoObject *o = (MonoObject*)mono_type_get_object (mono_domain_get (), &klass->byval_arg);
4731 buffer_add_objid (buf, o);
4734 case CMD_TYPE_GET_SOURCE_FILES: {
4735 gpointer iter = NULL;
4737 char *source_file, *base;
4741 files = g_ptr_array_new ();
4743 while ((method = mono_class_get_methods (klass, &iter))) {
4744 MonoDebugMethodInfo *minfo = mono_debug_lookup_method (method);
4747 mono_debug_symfile_get_line_numbers (minfo, &source_file, NULL, NULL, NULL);
4749 for (i = 0; i < files->len; ++i)
4750 if (!strcmp (g_ptr_array_index (files, i), source_file))
4752 if (i == files->len)
4753 g_ptr_array_add (files, g_strdup (source_file));
4754 g_free (source_file);
4758 buffer_add_int (buf, files->len);
4759 for (i = 0; i < files->len; ++i) {
4760 source_file = g_ptr_array_index (files, i);
4761 base = g_path_get_basename (source_file);
4762 buffer_add_string (buf, base);
4764 g_free (source_file);
4766 g_ptr_array_free (files, TRUE);
4769 case CMD_TYPE_IS_ASSIGNABLE_FROM: {
4770 MonoClass *oklass = decode_typeid (p, &p, end, NULL, &err);
4774 if (mono_class_is_assignable_from (klass, oklass))
4775 buffer_add_byte (buf, 1);
4777 buffer_add_byte (buf, 0);
4781 return ERR_NOT_IMPLEMENTED;
4788 method_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
4793 MonoMethodHeader *header;
4795 method = decode_methodid (p, &p, end, &domain, &err);
4800 case CMD_METHOD_GET_NAME: {
4801 buffer_add_string (buf, method->name);
4804 case CMD_METHOD_GET_DECLARING_TYPE: {
4805 buffer_add_typeid (buf, domain, method->klass);
4808 case CMD_METHOD_GET_DEBUG_INFO: {
4809 MonoDebugMethodInfo *minfo;
4811 int i, n_il_offsets;
4815 header = mono_method_get_header (method);
4817 buffer_add_int (buf, 0);
4818 buffer_add_string (buf, "");
4819 buffer_add_int (buf, 0);
4823 minfo = mono_debug_lookup_method (method);
4825 buffer_add_int (buf, header->code_size);
4826 buffer_add_string (buf, "");
4827 buffer_add_int (buf, 0);
4831 mono_debug_symfile_get_line_numbers (minfo, &source_file, &n_il_offsets, &il_offsets, &line_numbers);
4832 buffer_add_int (buf, header->code_size);
4833 buffer_add_string (buf, source_file);
4834 buffer_add_int (buf, n_il_offsets);
4835 //printf ("Line number table for method %s:\n", mono_method_full_name (method, TRUE));
4836 for (i = 0; i < n_il_offsets; ++i) {
4837 //printf ("IL%d -> %d\n", il_offsets [i], line_numbers [i]);
4838 buffer_add_int (buf, il_offsets [i]);
4839 buffer_add_int (buf, line_numbers [i]);
4841 g_free (source_file);
4842 g_free (il_offsets);
4843 g_free (line_numbers);
4846 case CMD_METHOD_GET_PARAM_INFO: {
4847 MonoMethodSignature *sig = mono_method_signature (method);
4851 /* FIXME: mono_class_from_mono_type () and byrefs */
4853 /* FIXME: Use a smaller encoding */
4854 buffer_add_int (buf, sig->call_convention);
4855 buffer_add_int (buf, sig->param_count);
4856 buffer_add_int (buf, sig->generic_param_count);
4857 buffer_add_typeid (buf, domain, mono_class_from_mono_type (sig->ret));
4858 for (i = 0; i < sig->param_count; ++i) {
4860 buffer_add_typeid (buf, domain, mono_class_from_mono_type (sig->params [i]));
4863 /* Emit parameter names */
4864 names = g_new (char *, sig->param_count);
4865 mono_method_get_param_names (method, (const char **) names);
4866 for (i = 0; i < sig->param_count; ++i)
4867 buffer_add_string (buf, names [i]);
4872 case CMD_METHOD_GET_LOCALS_INFO: {
4873 int i, j, num_locals;
4877 header = mono_method_get_header (method);
4880 buffer_add_int (buf, header->num_locals);
4883 for (i = 0; i < header->num_locals; ++i)
4884 buffer_add_typeid (buf, domain, mono_class_from_mono_type (header->locals [i]));
4887 num_locals = mono_debug_lookup_locals (method, &local_names, &local_indexes);
4888 for (i = 0; i < header->num_locals; ++i) {
4889 for (j = 0; j < num_locals; ++j)
4890 if (local_indexes [j] == i)
4893 buffer_add_string (buf, local_names [j]);
4895 buffer_add_string (buf, "");
4897 g_free (local_names);
4898 g_free (local_indexes);
4901 /* FIXME: This works because we set debug_options.mdb_optimizations */
4902 for (i = 0; i < header->num_locals; ++i) {
4903 buffer_add_int (buf, 0);
4904 buffer_add_int (buf, header->code_size);
4909 case CMD_METHOD_GET_INFO:
4910 buffer_add_int (buf, method->flags);
4911 buffer_add_int (buf, method->iflags);
4912 buffer_add_int (buf, method->token);
4914 case CMD_METHOD_GET_BODY: {
4917 header = mono_method_get_header (method);
4919 buffer_add_int (buf, 0);
4921 buffer_add_int (buf, header->code_size);
4922 for (i = 0; i < header->code_size; ++i)
4923 buffer_add_byte (buf, header->code [i]);
4927 case CMD_METHOD_RESOLVE_TOKEN: {
4928 guint32 token = decode_int (p, &p, end);
4931 switch (mono_metadata_token_code (token)) {
4932 case MONO_TOKEN_STRING: {
4936 s = mono_ldstr (domain, method->klass->image, mono_metadata_token_index (token));
4939 s2 = mono_string_to_utf8 (s);
4941 buffer_add_byte (buf, TOKEN_TYPE_STRING);
4942 buffer_add_string (buf, s2);
4948 MonoClass *handle_class;
4950 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4951 val = mono_method_get_wrapper_data (method, token);
4952 handle_class = mono_method_get_wrapper_data (method, token + 1);
4954 if (handle_class == NULL) {
4955 // Can't figure out the token type
4956 buffer_add_byte (buf, TOKEN_TYPE_UNKNOWN);
4960 val = mono_ldtoken (method->klass->image, token, &handle_class, NULL);
4964 if (handle_class == mono_defaults.typehandle_class) {
4965 buffer_add_byte (buf, TOKEN_TYPE_TYPE);
4966 buffer_add_typeid (buf, domain, mono_class_from_mono_type ((MonoType*)val));
4967 } else if (handle_class == mono_defaults.fieldhandle_class) {
4968 buffer_add_byte (buf, TOKEN_TYPE_FIELD);
4969 buffer_add_fieldid (buf, domain, val);
4970 } else if (handle_class == mono_defaults.methodhandle_class) {
4971 buffer_add_byte (buf, TOKEN_TYPE_METHOD);
4972 buffer_add_methodid (buf, domain, val);
4973 } else if (handle_class == mono_defaults.string_class) {
4976 s = mono_string_to_utf8 (val);
4977 buffer_add_byte (buf, TOKEN_TYPE_STRING);
4978 buffer_add_string (buf, s);
4981 g_assert_not_reached ();
4989 return ERR_NOT_IMPLEMENTED;
4996 thread_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
4998 int objid = decode_objid (p, &p, end);
5000 MonoThread *thread_obj;
5001 MonoInternalThread *thread;
5003 err = get_object (objid, (MonoObject**)&thread_obj);
5007 thread = THREAD_TO_INTERNAL (thread_obj);
5010 case CMD_THREAD_GET_NAME: {
5012 gunichar2 *s = mono_thread_get_name (thread, &name_len);
5015 buffer_add_int (buf, 0);
5020 name = g_utf16_to_utf8 (s, name_len, NULL, &len, NULL);
5022 buffer_add_int (buf, len);
5023 buffer_add_data (buf, (guint8*)name, len);
5028 case CMD_THREAD_GET_FRAME_INFO: {
5029 DebuggerTlsData *tls;
5030 int i, start_frame, length;
5032 // Wait for suspending if it already started
5034 wait_for_suspend ();
5035 if (!is_suspended ())
5036 return ERR_NOT_SUSPENDED;
5038 start_frame = decode_int (p, &p, end);
5039 length = decode_int (p, &p, end);
5041 if (start_frame != 0 || length != -1)
5042 return ERR_NOT_IMPLEMENTED;
5044 mono_loader_lock ();
5045 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
5046 mono_loader_unlock ();
5049 compute_frame_info (thread, tls);
5051 buffer_add_int (buf, tls->frame_count);
5052 for (i = 0; i < tls->frame_count; ++i) {
5053 buffer_add_int (buf, tls->frames [i]->id);
5054 buffer_add_methodid (buf, tls->frames [i]->domain, tls->frames [i]->method);
5055 buffer_add_int (buf, tls->frames [i]->il_offset);
5057 * Instead of passing the frame type directly to the client, we associate
5058 * it with the previous frame using a set of flags. This avoids lots of
5059 * conditional code in the client, since a frame whose type isn't
5060 * FRAME_TYPE_MANAGED has no method, location, etc.
5062 buffer_add_byte (buf, tls->frames [i]->flags);
5067 case CMD_THREAD_GET_STATE:
5068 buffer_add_int (buf, thread->state);
5070 case CMD_THREAD_GET_INFO:
5071 buffer_add_byte (buf, thread->threadpool_thread);
5074 return ERR_NOT_IMPLEMENTED;
5081 frame_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5085 MonoThread *thread_obj;
5086 MonoInternalThread *thread;
5088 DebuggerTlsData *tls;
5090 MonoDebugMethodJitInfo *jit;
5091 MonoDebugVarInfo *var;
5092 MonoMethodSignature *sig;
5094 MonoMethodHeader *header;
5096 objid = decode_objid (p, &p, end);
5097 err = get_object (objid, (MonoObject**)&thread_obj);
5101 thread = THREAD_TO_INTERNAL (thread_obj);
5103 id = decode_id (p, &p, end);
5105 mono_loader_lock ();
5106 tls = mono_g_hash_table_lookup (thread_to_tls, thread);
5107 mono_loader_unlock ();
5110 for (i = 0; i < tls->frame_count; ++i) {
5111 if (tls->frames [i]->id == id)
5114 if (i == tls->frame_count)
5115 return ERR_INVALID_FRAMEID;
5117 frame = tls->frames [i];
5120 frame->jit = mono_debug_find_method (frame->method, frame->domain);
5121 g_assert (frame->jit);
5125 sig = mono_method_signature (frame->method);
5128 case CMD_STACK_FRAME_GET_VALUES: {
5129 len = decode_int (p, &p, end);
5130 header = mono_method_get_header (frame->method);
5132 for (i = 0; i < len; ++i) {
5133 pos = decode_int (p, &p, end);
5138 g_assert (pos >= 0 && pos < jit->num_params);
5140 var = &jit->params [pos];
5142 add_var (buf, sig->params [pos], &jit->params [pos], &frame->ctx, frame->domain);
5144 g_assert (pos >= 0 && pos < jit->num_locals);
5146 var = &jit->locals [pos];
5148 add_var (buf, header->locals [pos], &jit->locals [pos], &frame->ctx, frame->domain);
5153 case CMD_STACK_FRAME_GET_THIS: {
5154 if (frame->method->klass->valuetype) {
5155 if (!sig->hasthis) {
5156 MonoObject *p = NULL;
5157 buffer_add_value (buf, &mono_defaults.object_class->byval_arg, &p, frame->domain);
5159 add_var (buf, &frame->method->klass->this_arg, jit->this_var, &frame->ctx, frame->domain);
5162 if (!sig->hasthis) {
5163 MonoObject *p = NULL;
5164 buffer_add_value (buf, &frame->method->klass->byval_arg, &p, frame->domain);
5166 add_var (buf, &frame->method->klass->byval_arg, jit->this_var, &frame->ctx, frame->domain);
5171 case CMD_STACK_FRAME_SET_VALUES: {
5174 MonoDebugVarInfo *var;
5176 len = decode_int (p, &p, end);
5177 header = mono_method_get_header (frame->method);
5179 for (i = 0; i < len; ++i) {
5180 pos = decode_int (p, &p, end);
5185 g_assert (pos >= 0 && pos < jit->num_params);
5187 t = sig->params [pos];
5188 var = &jit->params [pos];
5190 g_assert (pos >= 0 && pos < jit->num_locals);
5192 t = header->locals [pos];
5193 var = &jit->locals [pos];
5196 if (MONO_TYPE_IS_REFERENCE (t))
5197 val_buf = g_alloca (sizeof (MonoObject*));
5199 val_buf = g_alloca (mono_class_instance_size (mono_class_from_mono_type (t)));
5200 err = decode_value (t, frame->domain, val_buf, p, &p, end);
5204 set_var (t, var, &frame->ctx, frame->domain, val_buf);
5209 return ERR_NOT_IMPLEMENTED;
5216 array_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5219 int objid, err, index, len, i, esize;
5222 objid = decode_objid (p, &p, end);
5223 err = get_object (objid, (MonoObject**)&arr);
5228 case CMD_ARRAY_REF_GET_LENGTH:
5229 buffer_add_int (buf, arr->obj.vtable->klass->rank);
5231 buffer_add_int (buf, arr->max_length);
5232 buffer_add_int (buf, 0);
5234 for (i = 0; i < arr->obj.vtable->klass->rank; ++i) {
5235 buffer_add_int (buf, arr->bounds [i].length);
5236 buffer_add_int (buf, arr->bounds [i].lower_bound);
5240 case CMD_ARRAY_REF_GET_VALUES:
5241 index = decode_int (p, &p, end);
5242 len = decode_int (p, &p, end);
5244 g_assert (index >= 0 && len >= 0);
5245 // Reordered to avoid integer overflow
5246 g_assert (!(index > arr->max_length - len));
5248 esize = mono_array_element_size (arr->obj.vtable->klass);
5249 for (i = index; i < index + len; ++i) {
5250 elem = (gpointer*)((char*)arr->vector + (i * esize));
5251 buffer_add_value (buf, &arr->obj.vtable->klass->element_class->byval_arg, elem, arr->obj.vtable->domain);
5254 case CMD_ARRAY_REF_SET_VALUES:
5255 index = decode_int (p, &p, end);
5256 len = decode_int (p, &p, end);
5258 g_assert (index >= 0 && len >= 0);
5259 // Reordered to avoid integer overflow
5260 g_assert (!(index > arr->max_length - len));
5262 esize = mono_array_element_size (arr->obj.vtable->klass);
5263 for (i = index; i < index + len; ++i) {
5264 elem = (gpointer*)((char*)arr->vector + (i * esize));
5266 decode_value (&arr->obj.vtable->klass->element_class->byval_arg, arr->obj.vtable->domain, elem, p, &p, end);
5270 return ERR_NOT_IMPLEMENTED;
5277 string_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5283 objid = decode_objid (p, &p, end);
5284 err = get_object (objid, (MonoObject**)&str);
5289 case CMD_STRING_REF_GET_VALUE:
5290 s = mono_string_to_utf8 (str);
5291 buffer_add_string (buf, s);
5295 return ERR_NOT_IMPLEMENTED;
5302 object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
5311 if (command == CMD_OBJECT_REF_IS_COLLECTED) {
5312 objid = decode_objid (p, &p, end);
5313 err = get_object (objid, &obj);
5315 buffer_add_int (buf, 1);
5317 buffer_add_int (buf, 0);
5321 objid = decode_objid (p, &p, end);
5322 err = get_object (objid, &obj);
5327 case CMD_OBJECT_REF_GET_TYPE:
5328 buffer_add_typeid (buf, obj->vtable->domain, obj->vtable->klass);
5330 case CMD_OBJECT_REF_GET_VALUES:
5331 len = decode_int (p, &p, end);
5333 for (i = 0; i < len; ++i) {
5334 MonoClassField *f = decode_fieldid (p, &p, end, NULL, &err);
5338 /* Check that the field belongs to the object */
5340 for (k = obj->vtable->klass; k; k = k->parent) {
5341 if (k == f->parent) {
5347 return ERR_INVALID_FIELDID;
5349 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC) {
5353 if (mono_class_field_is_special_static (f))
5354 return ERR_INVALID_FIELDID;
5356 g_assert (f->type->attrs & FIELD_ATTRIBUTE_STATIC);
5357 vtable = mono_class_vtable (obj->vtable->domain, f->parent);
5358 val = g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
5359 mono_field_static_get_value (vtable, f, val);
5360 buffer_add_value (buf, f->type, val, obj->vtable->domain);
5363 buffer_add_value (buf, f->type, (guint8*)obj + f->offset, obj->vtable->domain);
5367 case CMD_OBJECT_REF_SET_VALUES:
5368 len = decode_int (p, &p, end);
5370 for (i = 0; i < len; ++i) {
5371 f = decode_fieldid (p, &p, end, NULL, &err);
5375 /* Check that the field belongs to the object */
5377 for (k = obj->vtable->klass; k; k = k->parent) {
5378 if (k == f->parent) {
5384 return ERR_INVALID_FIELDID;
5386 if (f->type->attrs & FIELD_ATTRIBUTE_STATIC) {
5390 if (mono_class_field_is_special_static (f))
5391 return ERR_INVALID_FIELDID;
5393 g_assert (f->type->attrs & FIELD_ATTRIBUTE_STATIC);
5394 vtable = mono_class_vtable (obj->vtable->domain, f->parent);
5396 val = g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
5397 err = decode_value (f->type, obj->vtable->domain, val, p, &p, end);
5402 mono_field_static_set_value (vtable, f, val);
5405 err = decode_value (f->type, obj->vtable->domain, (guint8*)obj + f->offset, p, &p, end);
5411 case CMD_OBJECT_REF_GET_ADDRESS:
5412 buffer_add_long (buf, (gssize)obj);
5414 case CMD_OBJECT_REF_GET_DOMAIN:
5415 buffer_add_domainid (buf, obj->vtable->domain);
5418 return ERR_NOT_IMPLEMENTED;
5425 command_set_to_string (CommandSet command_set)
5427 switch (command_set) {
5430 case CMD_SET_OBJECT_REF:
5431 return "OBJECT_REF";
5432 case CMD_SET_STRING_REF:
5433 return "STRING_REF";
5434 case CMD_SET_THREAD:
5436 case CMD_SET_ARRAY_REF:
5438 case CMD_SET_EVENT_REQUEST:
5439 return "EVENT_REQUEST";
5440 case CMD_SET_STACK_FRAME:
5441 return "STACK_FRAME";
5442 case CMD_SET_APPDOMAIN:
5444 case CMD_SET_ASSEMBLY:
5446 case CMD_SET_METHOD:
5450 case CMD_SET_MODULE:
5462 * This thread handles communication with the debugger client using a JDWP
5465 static guint32 WINAPI
5466 debugger_thread (void *arg)
5468 int res, len, id, flags, command_set, command;
5469 guint8 header [HEADER_LENGTH];
5470 guint8 *data, *p, *end;
5475 DEBUG (1, fprintf (log_file, "[dbg] Agent thread started, pid=%p\n", (gpointer)GetCurrentThreadId ()));
5477 debugger_thread_id = GetCurrentThreadId ();
5479 mono_jit_thread_attach (mono_get_root_domain ());
5481 mono_thread_internal_current ()->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
5483 mono_set_is_debugger_attached (TRUE);
5486 res = recv (conn_fd, header, HEADER_LENGTH, 0);
5488 /* This will break if the socket is closed during shutdown too */
5489 if (res != HEADER_LENGTH)
5493 end = header + HEADER_LENGTH;
5495 len = decode_int (p, &p, end);
5496 id = decode_int (p, &p, end);
5497 flags = decode_byte (p, &p, end);
5498 command_set = decode_byte (p, &p, end);
5499 command = decode_byte (p, &p, end);
5501 g_assert (flags == 0);
5503 DEBUG (1, fprintf (log_file, "[dbg] Received command %s(%d), id=%d.\n", command_set_to_string (command_set), command, id));
5505 data = g_malloc (len - HEADER_LENGTH);
5506 if (len - HEADER_LENGTH > 0)
5508 res = recv (conn_fd, data, len - HEADER_LENGTH, 0);
5509 if (res != len - HEADER_LENGTH)
5514 end = data + (len - HEADER_LENGTH);
5516 buffer_init (&buf, 128);
5521 /* Process the request */
5522 switch (command_set) {
5524 err = vm_commands (command, id, p, end, &buf);
5525 if (!err && command == CMD_VM_INVOKE_METHOD)
5526 /* Sent after the invoke is complete */
5529 case CMD_SET_EVENT_REQUEST:
5530 err = event_commands (command, p, end, &buf);
5532 case CMD_SET_APPDOMAIN:
5533 err = domain_commands (command, p, end, &buf);
5535 case CMD_SET_ASSEMBLY:
5536 err = assembly_commands (command, p, end, &buf);
5538 case CMD_SET_MODULE:
5539 err = module_commands (command, p, end, &buf);
5542 err = type_commands (command, p, end, &buf);
5544 case CMD_SET_METHOD:
5545 err = method_commands (command, p, end, &buf);
5547 case CMD_SET_THREAD:
5548 err = thread_commands (command, p, end, &buf);
5550 case CMD_SET_STACK_FRAME:
5551 err = frame_commands (command, p, end, &buf);
5553 case CMD_SET_ARRAY_REF:
5554 err = array_commands (command, p, end, &buf);
5556 case CMD_SET_STRING_REF:
5557 err = string_commands (command, p, end, &buf);
5559 case CMD_SET_OBJECT_REF:
5560 err = object_commands (command, p, end, &buf);
5563 err = ERR_NOT_IMPLEMENTED;
5567 send_reply_packet (id, err, &buf);
5572 if (command_set == CMD_SET_VM && command == CMD_VM_DISPOSE)
5576 mono_set_is_debugger_attached (FALSE);
5578 mono_mutex_lock (&debugger_thread_exited_mutex);
5579 debugger_thread_exited = TRUE;
5580 mono_cond_signal (&debugger_thread_exited_cond);
5581 mono_mutex_unlock (&debugger_thread_exited_mutex);
5584 shutdown (conn_fd, SD_BOTH);
5586 shutdown (conn_fd, SHUT_RDWR);
5592 #else /* DISABLE_DEBUGGER_AGENT */
5595 mono_debugger_agent_parse_options (char *options)
5597 g_error ("This runtime is configure with the debugger agent disabled.");
5601 mono_debugger_agent_init (void)
5606 mono_debugger_agent_breakpoint_hit (void *sigctx)
5611 mono_debugger_agent_single_step_event (void *sigctx)
5616 mono_debugger_agent_free_domain_info (MonoDomain *domain)
5621 mono_debugger_agent_thread_interrupt (void *sigctx, MonoJitInfo *ji)
5627 mono_debugger_agent_handle_exception (MonoException *ext, MonoContext *ctx)
5632 mono_debugger_agent_handle_unhandled_exception (MonoException *exc, MonoContext *ctx)