2 * debugger-agent.c: Soft Debugger back-end module
5 * Zoltan Varga (vargaz@gmail.com)
7 * Copyright 2009-2010 Novell, Inc.
8 * Copyright 2011 Xamarin Inc.
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #ifdef HAVE_SYS_TYPES_H
17 #include <sys/types.h>
19 #ifdef HAVE_SYS_SELECT_H
20 #include <sys/select.h>
22 #ifdef HAVE_SYS_SOCKET_H
23 #include <sys/socket.h>
25 #ifdef HAVE_NETINET_TCP_H
26 #include <netinet/tcp.h>
28 #ifdef HAVE_NETINET_IN_H
29 #include <netinet/in.h>
49 #ifdef PLATFORM_ANDROID
51 #include <linux/tcp.h>
52 #include <sys/endian.h>
55 #include <mono/metadata/mono-debug.h>
56 #include <mono/metadata/mono-debug-debugger.h>
57 #include <mono/metadata/debug-mono-symfile.h>
58 #include <mono/metadata/gc-internals.h>
59 #include <mono/metadata/environment.h>
60 #include <mono/metadata/threads-types.h>
61 #include <mono/metadata/threadpool-ms.h>
62 #include <mono/metadata/socket-io.h>
63 #include <mono/metadata/assembly.h>
64 #include <mono/metadata/runtime.h>
65 #include <mono/metadata/verify-internals.h>
66 #include <mono/metadata/reflection-internals.h>
67 #include <mono/utils/mono-coop-mutex.h>
68 #include <mono/utils/mono-coop-semaphore.h>
69 #include <mono/utils/mono-error-internals.h>
70 #include <mono/utils/mono-stack-unwinding.h>
71 #include <mono/utils/mono-time.h>
72 #include <mono/utils/mono-threads.h>
73 #include <mono/utils/networking.h>
74 #include "debugger-agent.h"
76 #include "seq-points.h"
79 * On iOS we can't use System.Environment.Exit () as it will do the wrong
82 #if !defined (TARGET_IOS)
83 #define TRY_MANAGED_SYSTEM_ENVIRONMENT_EXIT
87 #ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
88 #define DISABLE_DEBUGGER_AGENT 1
91 #ifdef DISABLE_SOFT_DEBUG
92 #define DISABLE_DEBUGGER_AGENT 1
95 #ifndef DISABLE_DEBUGGER_AGENT
97 #include <mono/utils/mono-os-mutex.h>
99 #define THREAD_TO_INTERNAL(thread) (thread)->internal_thread
122 guint32 il_offset
, native_offset
;
126 * If method is gshared, this is the actual instance, otherwise this is equal to
129 MonoMethod
*actual_method
;
131 * This is the method which is visible to debugger clients. Same as method,
132 * except for native-to-managed wrappers.
134 MonoMethod
*api_method
;
136 MonoDebugMethodJitInfo
*jit
;
139 mgreg_t
*reg_locations
[MONO_MAX_IREGS
];
141 * Whenever ctx is set. This is FALSE for the last frame of running threads, since
142 * the frame can become invalid.
147 typedef struct _InvokeData InvokeData
;
155 /* This is the context which needs to be restored after the invoke */
159 * If this is set, invoke this method with the arguments given by ARGS.
163 guint32 suspend_count
;
166 InvokeData
*last_invoke
;
170 MonoThreadUnwindState context
;
172 /* This is computed on demand when it is requested using the wire protocol */
173 /* It is freed up when the thread is resumed */
177 * Whenever the frame info is up-to-date. If not, compute_frame_info () will need to
180 gboolean frames_up_to_date
;
182 * Points to data about a pending invoke which needs to be executed after the thread
185 InvokeData
*pending_invoke
;
187 * Set to TRUE if this thread is suspended in suspend_current () or it is executing
192 * Signals whenever the thread is in the process of suspending, i.e. it will suspend
193 * within a finite amount of time.
197 * Set to TRUE if this thread is suspended in suspend_current ().
199 gboolean really_suspended
;
200 /* Used to pass the context to the breakpoint/single step handler */
201 MonoContext handler_ctx
;
202 /* Whenever thread_stop () was called for this thread */
205 /* Whenever to disable breakpoints (used during invokes) */
206 gboolean disable_breakpoints
;
209 * Number of times this thread has been resumed using resume_thread ().
211 guint32 resume_count
;
213 MonoInternalThread
*thread
;
216 * Information about the frame which transitioned to native code for running
219 StackFrameInfo async_last_frame
;
222 * The context where the stack walk can be started for running threads.
224 MonoThreadUnwindState async_state
;
227 * The context used for filter clauses
229 MonoThreadUnwindState filter_state
;
231 gboolean abort_requested
;
234 * The current mono_runtime_invoke_checked invocation.
239 * The context where single stepping should resume while the thread is suspended because
240 * of an EXCEPTION event.
242 MonoThreadUnwindState catch_state
;
245 * The context which needs to be restored after handling a single step/breakpoint
246 * event. This is the same as the ctx at step/breakpoint site, but includes changes
247 * to caller saved registers done by set_var ().
249 MonoThreadUnwindState restore_state
;
250 /* Frames computed from restore_state */
251 int restore_frame_count
;
252 StackFrame
**restore_frames
;
254 /* The currently unloading appdomain */
255 MonoDomain
*domain_unloading
;
260 void (*connect
) (const char *address
);
261 void (*close1
) (void);
262 void (*close2
) (void);
263 gboolean (*send
) (void *buf
, int len
);
264 int (*recv
) (void *buf
, int len
);
268 * Wire Protocol definitions
271 #define HEADER_LENGTH 11
273 #define MAJOR_VERSION 2
274 #define MINOR_VERSION 42
278 CMD_SET_OBJECT_REF
= 9,
279 CMD_SET_STRING_REF
= 10,
281 CMD_SET_ARRAY_REF
= 13,
282 CMD_SET_EVENT_REQUEST
= 15,
283 CMD_SET_STACK_FRAME
= 16,
284 CMD_SET_APPDOMAIN
= 20,
285 CMD_SET_ASSEMBLY
= 21,
294 EVENT_KIND_VM_START
= 0,
295 EVENT_KIND_VM_DEATH
= 1,
296 EVENT_KIND_THREAD_START
= 2,
297 EVENT_KIND_THREAD_DEATH
= 3,
298 EVENT_KIND_APPDOMAIN_CREATE
= 4,
299 EVENT_KIND_APPDOMAIN_UNLOAD
= 5,
300 EVENT_KIND_METHOD_ENTRY
= 6,
301 EVENT_KIND_METHOD_EXIT
= 7,
302 EVENT_KIND_ASSEMBLY_LOAD
= 8,
303 EVENT_KIND_ASSEMBLY_UNLOAD
= 9,
304 EVENT_KIND_BREAKPOINT
= 10,
305 EVENT_KIND_STEP
= 11,
306 EVENT_KIND_TYPE_LOAD
= 12,
307 EVENT_KIND_EXCEPTION
= 13,
308 EVENT_KIND_KEEPALIVE
= 14,
309 EVENT_KIND_USER_BREAK
= 15,
310 EVENT_KIND_USER_LOG
= 16
314 SUSPEND_POLICY_NONE
= 0,
315 SUSPEND_POLICY_EVENT_THREAD
= 1,
316 SUSPEND_POLICY_ALL
= 2
321 ERR_INVALID_OBJECT
= 20,
322 ERR_INVALID_FIELDID
= 25,
323 ERR_INVALID_FRAMEID
= 30,
324 ERR_NOT_IMPLEMENTED
= 100,
325 ERR_NOT_SUSPENDED
= 101,
326 ERR_INVALID_ARGUMENT
= 102,
328 ERR_NO_INVOCATION
= 104,
329 ERR_ABSENT_INFORMATION
= 105,
330 ERR_NO_SEQ_POINT_AT_IL_OFFSET
= 106,
331 ERR_INVOKE_ABORTED
= 107,
332 ERR_LOADER_ERROR
= 200, /*XXX extend the protocol to pass this information down the pipe */
337 MOD_KIND_THREAD_ONLY
= 3,
338 MOD_KIND_LOCATION_ONLY
= 7,
339 MOD_KIND_EXCEPTION_ONLY
= 8,
341 MOD_KIND_ASSEMBLY_ONLY
= 11,
342 MOD_KIND_SOURCE_FILE_ONLY
= 12,
343 MOD_KIND_TYPE_NAME_ONLY
= 13,
359 STEP_FILTER_NONE
= 0,
360 STEP_FILTER_STATIC_CTOR
= 1,
361 STEP_FILTER_DEBUGGER_HIDDEN
= 2,
362 STEP_FILTER_DEBUGGER_STEP_THROUGH
= 4,
363 STEP_FILTER_DEBUGGER_NON_USER_CODE
= 8
367 TOKEN_TYPE_STRING
= 0,
369 TOKEN_TYPE_FIELD
= 2,
370 TOKEN_TYPE_METHOD
= 3,
371 TOKEN_TYPE_UNKNOWN
= 4
375 VALUE_TYPE_ID_NULL
= 0xf0,
376 VALUE_TYPE_ID_TYPE
= 0xf1,
377 VALUE_TYPE_ID_PARENT_VTYPE
= 0xf2
381 FRAME_FLAG_DEBUGGER_INVOKE
= 1,
382 FRAME_FLAG_NATIVE_TRANSITION
= 2
386 INVOKE_FLAG_DISABLE_BREAKPOINTS
= 1,
387 INVOKE_FLAG_SINGLE_THREADED
= 2,
388 INVOKE_FLAG_RETURN_OUT_THIS
= 4,
389 INVOKE_FLAG_RETURN_OUT_ARGS
= 8,
390 INVOKE_FLAG_VIRTUAL
= 16
394 BINDING_FLAGS_IGNORE_CASE
= 0x70000000,
395 } BindingFlagsExtensions
;
399 CMD_VM_ALL_THREADS
= 2,
404 CMD_VM_INVOKE_METHOD
= 7,
405 CMD_VM_SET_PROTOCOL_VERSION
= 8,
406 CMD_VM_ABORT_INVOKE
= 9,
407 CMD_VM_SET_KEEPALIVE
= 10,
408 CMD_VM_GET_TYPES_FOR_SOURCE_FILE
= 11,
409 CMD_VM_GET_TYPES
= 12,
410 CMD_VM_INVOKE_METHODS
= 13,
411 CMD_VM_START_BUFFERING
= 14,
412 CMD_VM_STOP_BUFFERING
= 15
416 CMD_THREAD_GET_FRAME_INFO
= 1,
417 CMD_THREAD_GET_NAME
= 2,
418 CMD_THREAD_GET_STATE
= 3,
419 CMD_THREAD_GET_INFO
= 4,
420 CMD_THREAD_GET_ID
= 5,
421 CMD_THREAD_GET_TID
= 6,
422 CMD_THREAD_SET_IP
= 7
426 CMD_EVENT_REQUEST_SET
= 1,
427 CMD_EVENT_REQUEST_CLEAR
= 2,
428 CMD_EVENT_REQUEST_CLEAR_ALL_BREAKPOINTS
= 3
436 CMD_APPDOMAIN_GET_ROOT_DOMAIN
= 1,
437 CMD_APPDOMAIN_GET_FRIENDLY_NAME
= 2,
438 CMD_APPDOMAIN_GET_ASSEMBLIES
= 3,
439 CMD_APPDOMAIN_GET_ENTRY_ASSEMBLY
= 4,
440 CMD_APPDOMAIN_CREATE_STRING
= 5,
441 CMD_APPDOMAIN_GET_CORLIB
= 6,
442 CMD_APPDOMAIN_CREATE_BOXED_VALUE
= 7
446 CMD_ASSEMBLY_GET_LOCATION
= 1,
447 CMD_ASSEMBLY_GET_ENTRY_POINT
= 2,
448 CMD_ASSEMBLY_GET_MANIFEST_MODULE
= 3,
449 CMD_ASSEMBLY_GET_OBJECT
= 4,
450 CMD_ASSEMBLY_GET_TYPE
= 5,
451 CMD_ASSEMBLY_GET_NAME
= 6
455 CMD_MODULE_GET_INFO
= 1,
459 CMD_FIELD_GET_INFO
= 1,
463 CMD_METHOD_GET_NAME
= 1,
464 CMD_METHOD_GET_DECLARING_TYPE
= 2,
465 CMD_METHOD_GET_DEBUG_INFO
= 3,
466 CMD_METHOD_GET_PARAM_INFO
= 4,
467 CMD_METHOD_GET_LOCALS_INFO
= 5,
468 CMD_METHOD_GET_INFO
= 6,
469 CMD_METHOD_GET_BODY
= 7,
470 CMD_METHOD_RESOLVE_TOKEN
= 8,
471 CMD_METHOD_GET_CATTRS
= 9,
472 CMD_METHOD_MAKE_GENERIC_METHOD
= 10
476 CMD_TYPE_GET_INFO
= 1,
477 CMD_TYPE_GET_METHODS
= 2,
478 CMD_TYPE_GET_FIELDS
= 3,
479 CMD_TYPE_GET_VALUES
= 4,
480 CMD_TYPE_GET_OBJECT
= 5,
481 CMD_TYPE_GET_SOURCE_FILES
= 6,
482 CMD_TYPE_SET_VALUES
= 7,
483 CMD_TYPE_IS_ASSIGNABLE_FROM
= 8,
484 CMD_TYPE_GET_PROPERTIES
= 9,
485 CMD_TYPE_GET_CATTRS
= 10,
486 CMD_TYPE_GET_FIELD_CATTRS
= 11,
487 CMD_TYPE_GET_PROPERTY_CATTRS
= 12,
488 CMD_TYPE_GET_SOURCE_FILES_2
= 13,
489 CMD_TYPE_GET_VALUES_2
= 14,
490 CMD_TYPE_GET_METHODS_BY_NAME_FLAGS
= 15,
491 CMD_TYPE_GET_INTERFACES
= 16,
492 CMD_TYPE_GET_INTERFACE_MAP
= 17,
493 CMD_TYPE_IS_INITIALIZED
= 18,
494 CMD_TYPE_CREATE_INSTANCE
= 19
498 CMD_STACK_FRAME_GET_VALUES
= 1,
499 CMD_STACK_FRAME_GET_THIS
= 2,
500 CMD_STACK_FRAME_SET_VALUES
= 3,
501 CMD_STACK_FRAME_GET_DOMAIN
= 4,
505 CMD_ARRAY_REF_GET_LENGTH
= 1,
506 CMD_ARRAY_REF_GET_VALUES
= 2,
507 CMD_ARRAY_REF_SET_VALUES
= 3,
511 CMD_STRING_REF_GET_VALUE
= 1,
512 CMD_STRING_REF_GET_LENGTH
= 2,
513 CMD_STRING_REF_GET_CHARS
= 3
517 CMD_OBJECT_REF_GET_TYPE
= 1,
518 CMD_OBJECT_REF_GET_VALUES
= 2,
519 CMD_OBJECT_REF_IS_COLLECTED
= 3,
520 CMD_OBJECT_REF_GET_ADDRESS
= 4,
521 CMD_OBJECT_REF_GET_DOMAIN
= 5,
522 CMD_OBJECT_REF_SET_VALUES
= 6,
523 CMD_OBJECT_REF_GET_INFO
= 7,
529 int count
; /* For kind == MOD_KIND_COUNT */
530 MonoInternalThread
*thread
; /* For kind == MOD_KIND_THREAD_ONLY */
531 MonoClass
*exc_class
; /* For kind == MONO_KIND_EXCEPTION_ONLY */
532 MonoAssembly
**assemblies
; /* For kind == MONO_KIND_ASSEMBLY_ONLY */
533 GHashTable
*source_files
; /* For kind == MONO_KIND_SOURCE_FILE_ONLY */
534 GHashTable
*type_names
; /* For kind == MONO_KIND_TYPE_NAME_ONLY */
535 StepFilter filter
; /* For kind == MOD_KIND_STEP */
537 gboolean caught
, uncaught
, subclasses
; /* For kind == MOD_KIND_EXCEPTION_ONLY */
546 Modifier modifiers
[MONO_ZERO_LEN_ARRAY
];
550 * Describes a single step request.
554 MonoInternalThread
*thread
;
560 MonoMethod
*start_method
;
561 MonoMethod
*last_method
;
563 /* Whenever single stepping is performed using start/stop_single_stepping () */
565 /* The list of breakpoints used to implement step-over */
567 /* The number of frames at the start of a step-over */
572 * Contains additional information for an event
575 /* For EVENT_KIND_EXCEPTION */
577 MonoContext catch_ctx
;
579 /* For EVENT_KIND_USER_LOG */
581 char *category
, *message
;
582 /* For EVENT_KIND_TYPE_LOAD */
586 /* Dummy structure used for the profiler callbacks */
592 guint8
*buf
, *p
, *end
;
595 typedef struct ReplyPacket
{
601 #define DEBUG(level,s) do { if (G_UNLIKELY ((level) <= log_level)) { s; fflush (log_file); } } while (0)
603 #ifdef PLATFORM_ANDROID
604 #define DEBUG_PRINTF(level, ...) do { if (G_UNLIKELY ((level) <= log_level)) { g_print (__VA_ARGS__); } } while (0)
606 #define DEBUG_PRINTF(level, ...) do { if (G_UNLIKELY ((level) <= log_level)) { fprintf (log_file, __VA_ARGS__); fflush (log_file); } } while (0)
610 #define get_last_sock_error() WSAGetLastError()
611 #define MONO_EWOULDBLOCK WSAEWOULDBLOCK
612 #define MONO_EINTR WSAEINTR
614 #define get_last_sock_error() errno
615 #define MONO_EWOULDBLOCK EWOULDBLOCK
616 #define MONO_EINTR EINTR
619 #define CHECK_PROTOCOL_VERSION(major,minor) \
620 (protocol_version_set && (major_version > (major) || (major_version == (major) && minor_version >= (minor))))
626 static AgentConfig agent_config
;
629 * Whenever the agent is fully initialized.
630 * When using the onuncaught or onthrow options, only some parts of the agent are
631 * initialized on startup, and the full initialization which includes connection
632 * establishment and the startup of the agent thread is only done in response to
635 static gint32 inited
;
637 #ifndef DISABLE_SOCKET_TRANSPORT
639 static int listen_fd
;
642 static int packet_id
= 0;
644 static int objref_id
= 0;
646 static int event_request_id
= 0;
648 static int frame_id
= 0;
650 static GPtrArray
*event_requests
;
652 static MonoNativeTlsKey debugger_tls_id
;
654 static gboolean vm_start_event_sent
, vm_death_event_sent
, disconnected
;
656 /* Maps MonoInternalThread -> DebuggerTlsData */
657 /* Protected by the loader lock */
658 static MonoGHashTable
*thread_to_tls
;
660 /* Maps tid -> MonoInternalThread */
661 /* Protected by the loader lock */
662 static MonoGHashTable
*tid_to_thread
;
664 /* Maps tid -> MonoThread (not MonoInternalThread) */
665 /* Protected by the loader lock */
666 static MonoGHashTable
*tid_to_thread_obj
;
668 static MonoNativeThreadId debugger_thread_id
;
670 static HANDLE debugger_thread_handle
;
672 static int log_level
;
674 static gboolean embedding
;
676 static FILE *log_file
;
678 /* Assemblies whose assembly load event has no been sent yet */
679 /* Protected by the dbg lock */
680 static GPtrArray
*pending_assembly_loads
;
682 /* Whenever the debugger thread has exited */
683 static gboolean debugger_thread_exited
;
685 /* Cond variable used to wait for debugger_thread_exited becoming true */
686 static MonoCoopCond debugger_thread_exited_cond
;
688 /* Mutex for the cond var above */
689 static MonoCoopMutex debugger_thread_exited_mutex
;
691 static DebuggerProfiler debugger_profiler
;
693 /* The single step request instance */
694 static SingleStepReq
*ss_req
;
696 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
697 /* Number of single stepping operations in progress */
701 /* The protocol version of the client */
702 static int major_version
, minor_version
;
704 /* Whenever the variables above are set by the client */
705 static gboolean protocol_version_set
;
707 /* A hash table containing all active domains */
708 /* Protected by the loader lock */
709 static GHashTable
*domains
;
711 /* The number of times the runtime is suspended */
712 static gint32 suspend_count
;
714 /* Whenever to buffer reply messages and send them together */
715 static gboolean buffer_replies
;
717 /* Buffered reply packets */
718 static ReplyPacket reply_packets
[128];
721 #define dbg_lock() mono_coop_mutex_lock (&debug_mutex)
722 #define dbg_unlock() mono_coop_mutex_unlock (&debug_mutex)
723 static MonoCoopMutex debug_mutex
;
725 static void transport_init (void);
726 static void transport_connect (const char *address
);
727 static gboolean
transport_handshake (void);
728 static void register_transport (DebuggerTransport
*trans
);
730 static guint32 WINAPI
debugger_thread (void *arg
);
732 static void runtime_initialized (MonoProfiler
*prof
);
734 static void runtime_shutdown (MonoProfiler
*prof
);
736 static void thread_startup (MonoProfiler
*prof
, uintptr_t tid
);
738 static void thread_end (MonoProfiler
*prof
, uintptr_t tid
);
740 static void appdomain_load (MonoProfiler
*prof
, MonoDomain
*domain
, int result
);
742 static void appdomain_start_unload (MonoProfiler
*prof
, MonoDomain
*domain
);
744 static void appdomain_unload (MonoProfiler
*prof
, MonoDomain
*domain
);
746 static void emit_appdomain_load (gpointer key
, gpointer value
, gpointer user_data
);
748 static void emit_thread_start (gpointer key
, gpointer value
, gpointer user_data
);
750 static void invalidate_each_thread (gpointer key
, gpointer value
, gpointer user_data
);
752 static void assembly_load (MonoProfiler
*prof
, MonoAssembly
*assembly
, int result
);
754 static void assembly_unload (MonoProfiler
*prof
, MonoAssembly
*assembly
);
756 static void emit_assembly_load (gpointer assembly
, gpointer user_data
);
758 static void emit_type_load (gpointer key
, gpointer type
, gpointer user_data
);
760 static void jit_end (MonoProfiler
*prof
, MonoMethod
*method
, MonoJitInfo
*jinfo
, int result
);
762 static void add_pending_breakpoints (MonoMethod
*method
, MonoJitInfo
*jinfo
);
764 static void start_single_stepping (void);
766 static void stop_single_stepping (void);
768 static void suspend_current (void);
770 static void clear_event_requests_for_assembly (MonoAssembly
*assembly
);
772 static void clear_types_for_assembly (MonoAssembly
*assembly
);
774 static void clear_breakpoints_for_domain (MonoDomain
*domain
);
776 static void process_profiler_event (EventKind event
, gpointer arg
);
778 /* Submodule init/cleanup */
779 static void breakpoints_init (void);
780 static void breakpoints_cleanup (void);
782 static void objrefs_init (void);
783 static void objrefs_cleanup (void);
785 static void ids_init (void);
786 static void ids_cleanup (void);
788 static void suspend_init (void);
790 static void ss_start (SingleStepReq
*ss_req
, MonoMethod
*method
, SeqPoint
*sp
, MonoSeqPointInfo
*info
, MonoContext
*ctx
, DebuggerTlsData
*tls
, gboolean step_to_catch
,
791 StackFrame
**frames
, int nframes
);
792 static ErrorCode
ss_create (MonoInternalThread
*thread
, StepSize size
, StepDepth depth
, StepFilter filter
, EventRequest
*req
);
793 static void ss_destroy (SingleStepReq
*req
);
795 static void start_debugger_thread (void);
796 static void stop_debugger_thread (void);
798 static void finish_agent_init (gboolean on_startup
);
800 static void process_profiler_event (EventKind event
, gpointer arg
);
802 static void invalidate_frames (DebuggerTlsData
*tls
);
804 #ifndef DISABLE_SOCKET_TRANSPORT
806 register_socket_transport (void);
809 static inline gboolean
810 is_debugger_thread (void)
812 return mono_native_thread_id_equals (mono_native_thread_id_get (), debugger_thread_id
);
816 parse_address (char *address
, char **host
, int *port
)
818 char *pos
= strchr (address
, ':');
820 if (pos
== NULL
|| pos
== address
)
823 *host
= (char *)g_malloc (pos
- address
+ 1);
824 strncpy (*host
, address
, pos
- address
);
825 (*host
) [pos
- address
] = '\0';
827 *port
= atoi (pos
+ 1);
835 fprintf (stderr
, "Usage: mono --debugger-agent=[<option>=<value>,...] ...\n");
836 fprintf (stderr
, "Available options:\n");
837 fprintf (stderr
, " transport=<transport>\t\tTransport to use for connecting to the debugger (mandatory, possible values: 'dt_socket')\n");
838 fprintf (stderr
, " address=<hostname>:<port>\tAddress to connect to (mandatory)\n");
839 fprintf (stderr
, " loglevel=<n>\t\t\tLog level (defaults to 0)\n");
840 fprintf (stderr
, " logfile=<file>\t\tFile to log to (defaults to stdout)\n");
841 fprintf (stderr
, " suspend=y/n\t\t\tWhether to suspend after startup.\n");
842 fprintf (stderr
, " timeout=<n>\t\t\tTimeout for connecting in milliseconds.\n");
843 fprintf (stderr
, " server=y/n\t\t\tWhether to listen for a client connection.\n");
844 fprintf (stderr
, " keepalive=<n>\t\t\tSend keepalive events every n milliseconds.\n");
845 fprintf (stderr
, " setpgid=y/n\t\t\tWhether to call setpid(0, 0) after startup.\n");
846 fprintf (stderr
, " help\t\t\t\tPrint this help.\n");
850 parse_flag (const char *option
, char *flag
)
852 if (!strcmp (flag
, "y"))
854 else if (!strcmp (flag
, "n"))
857 fprintf (stderr
, "debugger-agent: The valid values for the '%s' option are 'y' and 'n'.\n", option
);
864 mono_debugger_agent_parse_options (char *options
)
871 #ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
872 fprintf (stderr
, "--debugger-agent is not supported on this platform.\n");
876 extra
= g_getenv ("MONO_SDB_ENV_OPTIONS");
878 options
= g_strdup_printf ("%s,%s", options
, extra
);
880 agent_config
.enabled
= TRUE
;
881 agent_config
.suspend
= TRUE
;
882 agent_config
.server
= FALSE
;
883 agent_config
.defer
= FALSE
;
884 agent_config
.address
= NULL
;
886 //agent_config.log_level = 10;
888 args
= g_strsplit (options
, ",", -1);
889 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
892 if (strncmp (arg
, "transport=", 10) == 0) {
893 agent_config
.transport
= g_strdup (arg
+ 10);
894 } else if (strncmp (arg
, "address=", 8) == 0) {
895 agent_config
.address
= g_strdup (arg
+ 8);
896 } else if (strncmp (arg
, "loglevel=", 9) == 0) {
897 agent_config
.log_level
= atoi (arg
+ 9);
898 } else if (strncmp (arg
, "logfile=", 8) == 0) {
899 agent_config
.log_file
= g_strdup (arg
+ 8);
900 } else if (strncmp (arg
, "suspend=", 8) == 0) {
901 agent_config
.suspend
= parse_flag ("suspend", arg
+ 8);
902 } else if (strncmp (arg
, "server=", 7) == 0) {
903 agent_config
.server
= parse_flag ("server", arg
+ 7);
904 } else if (strncmp (arg
, "onuncaught=", 11) == 0) {
905 agent_config
.onuncaught
= parse_flag ("onuncaught", arg
+ 11);
906 } else if (strncmp (arg
, "onthrow=", 8) == 0) {
907 /* We support multiple onthrow= options */
908 agent_config
.onthrow
= g_slist_append (agent_config
.onthrow
, g_strdup (arg
+ 8));
909 } else if (strncmp (arg
, "onthrow", 7) == 0) {
910 agent_config
.onthrow
= g_slist_append (agent_config
.onthrow
, g_strdup (""));
911 } else if (strncmp (arg
, "help", 4) == 0) {
914 } else if (strncmp (arg
, "timeout=", 8) == 0) {
915 agent_config
.timeout
= atoi (arg
+ 8);
916 } else if (strncmp (arg
, "launch=", 7) == 0) {
917 agent_config
.launch
= g_strdup (arg
+ 7);
918 } else if (strncmp (arg
, "embedding=", 10) == 0) {
919 agent_config
.embedding
= atoi (arg
+ 10) == 1;
920 } else if (strncmp (arg
, "keepalive=", 10) == 0) {
921 agent_config
.keepalive
= atoi (arg
+ 10);
922 } else if (strncmp (arg
, "setpgid=", 8) == 0) {
923 agent_config
.setpgid
= parse_flag ("setpgid", arg
+ 8);
930 if (agent_config
.server
&& !agent_config
.suspend
) {
931 /* Waiting for deferred attachment */
932 agent_config
.defer
= TRUE
;
933 if (agent_config
.address
== NULL
) {
934 agent_config
.address
= g_strdup_printf ("0.0.0.0:%u", 56000 + (getpid () % 1000));
938 //agent_config.log_level = 0;
940 if (agent_config
.transport
== NULL
) {
941 fprintf (stderr
, "debugger-agent: The 'transport' option is mandatory.\n");
945 if (agent_config
.address
== NULL
&& !agent_config
.server
) {
946 fprintf (stderr
, "debugger-agent: The 'address' option is mandatory.\n");
951 if (!strcmp (agent_config
.transport
, "dt_socket")) {
952 if (agent_config
.address
&& parse_address (agent_config
.address
, &host
, &port
)) {
953 fprintf (stderr
, "debugger-agent: The format of the 'address' options is '<host>:<port>'\n");
960 mono_debugger_agent_init (void)
962 mono_coop_mutex_init_recursive (&debug_mutex
);
964 if (!agent_config
.enabled
)
969 /* Need to know whenever a thread has acquired the loader mutex */
970 mono_loader_lock_track_ownership (TRUE
);
972 event_requests
= g_ptr_array_new ();
974 mono_coop_mutex_init (&debugger_thread_exited_mutex
);
975 mono_coop_cond_init (&debugger_thread_exited_cond
);
977 mono_profiler_install ((MonoProfiler
*)&debugger_profiler
, runtime_shutdown
);
978 mono_profiler_set_events ((MonoProfileFlags
)(MONO_PROFILE_APPDOMAIN_EVENTS
| MONO_PROFILE_THREADS
| MONO_PROFILE_ASSEMBLY_EVENTS
| MONO_PROFILE_JIT_COMPILATION
| MONO_PROFILE_METHOD_EVENTS
));
979 mono_profiler_install_runtime_initialized (runtime_initialized
);
980 mono_profiler_install_appdomain (NULL
, appdomain_load
, appdomain_start_unload
, appdomain_unload
);
981 mono_profiler_install_thread (thread_startup
, thread_end
);
982 mono_profiler_install_assembly (NULL
, assembly_load
, assembly_unload
, NULL
);
983 mono_profiler_install_jit_end (jit_end
);
985 mono_native_tls_alloc (&debugger_tls_id
, NULL
);
987 /* Needed by the hash_table_new_type () call below */
988 mono_gc_base_init ();
990 thread_to_tls
= mono_g_hash_table_new_type (NULL
, NULL
, MONO_HASH_KEY_GC
, MONO_ROOT_SOURCE_DEBUGGER
, "thread-to-tls table");
991 MONO_GC_REGISTER_ROOT_FIXED (thread_to_tls
, MONO_ROOT_SOURCE_DEBUGGER
, "thread-to-tls table");
993 tid_to_thread
= mono_g_hash_table_new_type (NULL
, NULL
, MONO_HASH_VALUE_GC
, MONO_ROOT_SOURCE_DEBUGGER
, "tid-to-thread table");
994 MONO_GC_REGISTER_ROOT_FIXED (tid_to_thread
, MONO_ROOT_SOURCE_DEBUGGER
, "tid-to-thread table");
996 tid_to_thread_obj
= mono_g_hash_table_new_type (NULL
, NULL
, MONO_HASH_VALUE_GC
, MONO_ROOT_SOURCE_DEBUGGER
, "tid-to-thread object table");
997 MONO_GC_REGISTER_ROOT_FIXED (tid_to_thread_obj
, MONO_ROOT_SOURCE_DEBUGGER
, "tid-to-thread object table");
999 pending_assembly_loads
= g_ptr_array_new ();
1000 domains
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
1002 log_level
= agent_config
.log_level
;
1004 embedding
= agent_config
.embedding
;
1005 disconnected
= TRUE
;
1007 if (agent_config
.log_file
) {
1008 log_file
= fopen (agent_config
.log_file
, "w+");
1010 fprintf (stderr
, "Unable to create log file '%s': %s.\n", agent_config
.log_file
, strerror (errno
));
1019 breakpoints_init ();
1022 mini_get_debug_options ()->gen_sdb_seq_points
= TRUE
;
1024 * This is needed because currently we don't handle liveness info.
1026 mini_get_debug_options ()->mdb_optimizations
= TRUE
;
1028 #ifndef MONO_ARCH_HAVE_CONTEXT_SET_INT_REG
1029 /* This is needed because we can't set local variables in registers yet */
1030 mono_disable_optimizations (MONO_OPT_LINEARS
);
1034 * The stack walk done from thread_interrupt () needs to be signal safe, but it
1035 * isn't, since it can call into mono_aot_find_jit_info () which is not signal
1036 * safe (#3411). So load AOT info eagerly when the debugger is running as a
1039 mini_get_debug_options ()->load_aot_jit_info_eagerly
= TRUE
;
1042 if (agent_config
.setpgid
)
1046 if (!agent_config
.onuncaught
&& !agent_config
.onthrow
)
1047 finish_agent_init (TRUE
);
1051 * finish_agent_init:
1053 * Finish the initialization of the agent. This involves connecting the transport
1054 * and starting the agent thread. This is either done at startup, or
1055 * in response to some event like an unhandled exception.
1058 finish_agent_init (gboolean on_startup
)
1062 if (InterlockedCompareExchange (&inited
, 1, 0) == 1)
1065 if (agent_config
.launch
) {
1068 // FIXME: Generated address
1069 // FIXME: Races with transport_connect ()
1071 argv
[0] = agent_config
.launch
;
1072 argv
[1] = agent_config
.transport
;
1073 argv
[2] = agent_config
.address
;
1076 res
= g_spawn_async_with_pipes (NULL
, argv
, NULL
, (GSpawnFlags
)0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1078 fprintf (stderr
, "Failed to execute '%s'.\n", agent_config
.launch
);
1083 transport_connect (agent_config
.address
);
1086 /* Do some which is usually done after sending the VMStart () event */
1087 vm_start_event_sent
= TRUE
;
1088 start_debugger_thread ();
1093 mono_debugger_agent_cleanup (void)
1098 stop_debugger_thread ();
1100 breakpoints_cleanup ();
1109 #ifndef DISABLE_SOCKET_TRANSPORT
1114 * recv() + handle incomplete reads and EINTR
1117 socket_transport_recv (void *buf
, int len
)
1123 static gint64 last_keepalive
;
1130 res
= recv (fd
, (char *) buf
+ total
, len
- total
, flags
);
1133 if (agent_config
.keepalive
) {
1134 gboolean need_keepalive
= FALSE
;
1135 if (res
== -1 && get_last_sock_error () == MONO_EWOULDBLOCK
) {
1136 need_keepalive
= TRUE
;
1137 } else if (res
== -1) {
1138 /* This could happen if recv () is interrupted repeatedly */
1139 msecs
= mono_msec_ticks ();
1140 if (msecs
- last_keepalive
>= agent_config
.keepalive
) {
1141 need_keepalive
= TRUE
;
1142 last_keepalive
= msecs
;
1145 if (need_keepalive
) {
1146 process_profiler_event (EVENT_KIND_KEEPALIVE
, NULL
);
1150 } while ((res
> 0 && total
< len
) || (res
== -1 && get_last_sock_error () == MONO_EINTR
));
1158 set_keepalive (void)
1163 if (!agent_config
.keepalive
|| !conn_fd
)
1166 tv
.tv_sec
= agent_config
.keepalive
/ 1000;
1167 tv
.tv_usec
= (agent_config
.keepalive
% 1000) * 1000;
1169 result
= setsockopt (conn_fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &tv
, sizeof(struct timeval
));
1170 g_assert (result
>= 0);
1174 socket_transport_accept (int socket_fd
)
1177 conn_fd
= accept (socket_fd
, NULL
, NULL
);
1180 if (conn_fd
== -1) {
1181 fprintf (stderr
, "debugger-agent: Unable to listen on %d\n", socket_fd
);
1183 DEBUG_PRINTF (1, "Accepted connection from client, connection fd=%d.\n", conn_fd
);
1190 socket_transport_send (void *data
, int len
)
1197 res
= send (conn_fd
, data
, len
, 0);
1198 } while (res
== -1 && get_last_sock_error () == MONO_EINTR
);
1209 * socket_transport_connect:
1211 * Connect/Listen on HOST:PORT. If HOST is NULL, generate an address and listen on it.
1214 socket_transport_connect (const char *address
)
1216 MonoAddressInfo
*result
;
1217 MonoAddressEntry
*rp
;
1218 int sfd
= -1, s
, res
;
1222 if (agent_config
.address
) {
1223 res
= parse_address (agent_config
.address
, &host
, &port
);
1224 g_assert (res
== 0);
1235 mono_network_init ();
1237 /* Obtain address(es) matching host/port */
1238 s
= mono_get_address_info (host
, port
, MONO_HINT_UNSPECIFIED
, &result
);
1240 fprintf (stderr
, "debugger-agent: Unable to resolve %s:%d: %d\n", host
, port
, s
); // FIXME add portable error conversion functions
1245 if (agent_config
.server
) {
1246 /* Wait for a connection */
1248 struct sockaddr_in addr
;
1251 /* No address, generate one */
1252 sfd
= socket (AF_INET
, SOCK_STREAM
, 0);
1255 /* This will bind the socket to a random port */
1256 res
= listen (sfd
, 16);
1258 fprintf (stderr
, "debugger-agent: Unable to setup listening socket: %s\n", strerror (get_last_sock_error ()));
1263 addrlen
= sizeof (addr
);
1264 memset (&addr
, 0, sizeof (addr
));
1265 res
= getsockname (sfd
, (struct sockaddr
*)&addr
, &addrlen
);
1266 g_assert (res
== 0);
1268 host
= (char*)"127.0.0.1";
1269 port
= ntohs (addr
.sin_port
);
1271 /* Emit the address to stdout */
1272 /* FIXME: Should print another interface, not localhost */
1273 printf ("%s:%d\n", host
, port
);
1275 /* Listen on the provided address */
1276 for (rp
= result
->entries
; rp
!= NULL
; rp
= rp
->next
) {
1277 MonoSocketAddress sockaddr
;
1281 mono_socket_address_init (&sockaddr
, &sock_len
, rp
->family
, &rp
->address
, port
);
1283 sfd
= socket (rp
->family
, rp
->socktype
,
1288 if (setsockopt (sfd
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1)
1291 res
= bind (sfd
, &sockaddr
.addr
, sock_len
);
1295 res
= listen (sfd
, 16);
1302 mono_free_address_info (result
);
1305 if (agent_config
.defer
)
1308 DEBUG_PRINTF (1, "Listening on %s:%d (timeout=%d ms)...\n", host
, port
, agent_config
.timeout
);
1310 if (agent_config
.timeout
) {
1315 tv
.tv_usec
= agent_config
.timeout
* 1000;
1317 FD_SET (sfd
, &readfds
);
1320 res
= select (sfd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1324 fprintf (stderr
, "debugger-agent: Timed out waiting to connect.\n");
1329 conn_fd
= socket_transport_accept (sfd
);
1333 DEBUG_PRINTF (1, "Accepted connection from client, socket fd=%d.\n", conn_fd
);
1335 /* Connect to the specified address */
1336 /* FIXME: Respect the timeout */
1337 for (rp
= result
->entries
; rp
!= NULL
; rp
= rp
->next
) {
1338 MonoSocketAddress sockaddr
;
1341 mono_socket_address_init (&sockaddr
, &sock_len
, rp
->family
, &rp
->address
, port
);
1343 sfd
= socket (rp
->family
, rp
->socktype
,
1349 res
= connect (sfd
, &sockaddr
.addr
, sock_len
);
1353 break; /* Success */
1361 fprintf (stderr
, "debugger-agent: Unable to connect to %s:%d\n", host
, port
);
1367 mono_free_address_info (result
);
1370 if (!transport_handshake ())
1375 socket_transport_close1 (void)
1377 /* This will interrupt the agent thread */
1378 /* Close the read part only so it can still send back replies */
1379 /* Also shut down the connection listener so that we can exit normally */
1381 /* SD_RECEIVE doesn't break the recv in the debugger thread */
1382 shutdown (conn_fd
, SD_BOTH
);
1383 shutdown (listen_fd
, SD_BOTH
);
1384 closesocket (listen_fd
);
1386 shutdown (conn_fd
, SHUT_RD
);
1387 shutdown (listen_fd
, SHUT_RDWR
);
1395 socket_transport_close2 (void)
1398 shutdown (conn_fd
, SD_BOTH
);
1400 shutdown (conn_fd
, SHUT_RDWR
);
1405 register_socket_transport (void)
1407 DebuggerTransport trans
;
1409 trans
.name
= "dt_socket";
1410 trans
.connect
= socket_transport_connect
;
1411 trans
.close1
= socket_transport_close1
;
1412 trans
.close2
= socket_transport_close2
;
1413 trans
.send
= socket_transport_send
;
1414 trans
.recv
= socket_transport_recv
;
1416 register_transport (&trans
);
1420 * socket_fd_transport_connect:
1424 socket_fd_transport_connect (const char *address
)
1428 res
= sscanf (address
, "%d", &conn_fd
);
1430 fprintf (stderr
, "debugger-agent: socket-fd transport address is invalid: '%s'\n", address
);
1434 if (!transport_handshake ())
1439 register_socket_fd_transport (void)
1441 DebuggerTransport trans
;
1443 /* This is the same as the 'dt_socket' transport, but receives an already connected socket fd */
1444 trans
.name
= "socket-fd";
1445 trans
.connect
= socket_fd_transport_connect
;
1446 trans
.close1
= socket_transport_close1
;
1447 trans
.close2
= socket_transport_close2
;
1448 trans
.send
= socket_transport_send
;
1449 trans
.recv
= socket_transport_recv
;
1451 register_transport (&trans
);
1454 #endif /* DISABLE_SOCKET_TRANSPORT */
1460 #define MAX_TRANSPORTS 16
1462 static DebuggerTransport
*transport
;
1464 static DebuggerTransport transports
[MAX_TRANSPORTS
];
1465 static int ntransports
;
1468 mono_debugger_agent_register_transport (DebuggerTransport
*trans
);
1471 mono_debugger_agent_register_transport (DebuggerTransport
*trans
)
1473 register_transport (trans
);
1477 register_transport (DebuggerTransport
*trans
)
1479 g_assert (ntransports
< MAX_TRANSPORTS
);
1481 memcpy (&transports
[ntransports
], trans
, sizeof (DebuggerTransport
));
1486 transport_init (void)
1490 #ifndef DISABLE_SOCKET_TRANSPORT
1491 register_socket_transport ();
1492 register_socket_fd_transport ();
1495 for (i
= 0; i
< ntransports
; ++i
) {
1496 if (!strcmp (agent_config
.transport
, transports
[i
].name
))
1499 if (i
== ntransports
) {
1500 fprintf (stderr
, "debugger-agent: The supported values for the 'transport' option are: ");
1501 for (i
= 0; i
< ntransports
; ++i
)
1502 fprintf (stderr
, "%s'%s'", i
> 0 ? ", " : "", transports
[i
].name
);
1503 fprintf (stderr
, "\n");
1506 transport
= &transports
[i
];
1510 transport_connect (const char *address
)
1512 transport
->connect (address
);
1516 transport_close1 (void)
1518 transport
->close1 ();
1522 transport_close2 (void)
1524 transport
->close2 ();
1528 transport_send (void *buf
, int len
)
1530 return transport
->send (buf
, len
);
1534 transport_recv (void *buf
, int len
)
1536 return transport
->recv (buf
, len
);
1540 mono_debugger_agent_transport_handshake (void)
1542 return transport_handshake ();
1546 transport_handshake (void)
1548 char handshake_msg
[128];
1552 disconnected
= TRUE
;
1554 /* Write handshake message */
1555 sprintf (handshake_msg
, "DWP-Handshake");
1558 res
= transport_send (handshake_msg
, strlen (handshake_msg
));
1559 } while (res
== -1 && get_last_sock_error () == MONO_EINTR
);
1561 g_assert (res
!= -1);
1564 res
= transport_recv (buf
, strlen (handshake_msg
));
1565 if ((res
!= strlen (handshake_msg
)) || (memcmp (buf
, handshake_msg
, strlen (handshake_msg
)) != 0)) {
1566 fprintf (stderr
, "debugger-agent: DWP handshake failed.\n");
1571 * To support older clients, the client sends its protocol version after connecting
1572 * using a command. Until that is received, default to our protocol version.
1574 major_version
= MAJOR_VERSION
;
1575 minor_version
= MINOR_VERSION
;
1576 protocol_version_set
= FALSE
;
1578 #ifndef DISABLE_SOCKET_TRANSPORT
1579 // FIXME: Move this somewhere else
1581 * Set TCP_NODELAY on the socket so the client receives events/command
1582 * results immediately.
1586 int result
= setsockopt (conn_fd
,
1591 g_assert (result
>= 0);
1597 disconnected
= FALSE
;
1602 stop_debugger_thread (void)
1607 transport_close1 ();
1610 * Wait for the thread to exit.
1612 * If we continue with the shutdown without waiting for it, then the client might
1613 * not receive an answer to its last command like a resume.
1615 if (!is_debugger_thread ()) {
1617 mono_coop_mutex_lock (&debugger_thread_exited_mutex
);
1618 if (!debugger_thread_exited
)
1619 mono_coop_cond_wait (&debugger_thread_exited_cond
, &debugger_thread_exited_mutex
);
1620 mono_coop_mutex_unlock (&debugger_thread_exited_mutex
);
1621 } while (!debugger_thread_exited
);
1624 transport_close2 ();
1628 start_debugger_thread (void)
1634 tp
.creation_flags
= 0;
1635 debugger_thread_handle
= mono_threads_create_thread (debugger_thread
, NULL
, &tp
, NULL
);
1636 g_assert (debugger_thread_handle
);
1640 * Functions to decode protocol data
1644 decode_byte (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1647 g_assert (*endbuf
<= limit
);
1652 decode_int (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1655 g_assert (*endbuf
<= limit
);
1657 return (((int)buf
[0]) << 24) | (((int)buf
[1]) << 16) | (((int)buf
[2]) << 8) | (((int)buf
[3]) << 0);
1660 static inline gint64
1661 decode_long (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1663 guint32 high
= decode_int (buf
, &buf
, limit
);
1664 guint32 low
= decode_int (buf
, &buf
, limit
);
1668 return ((((guint64
)high
) << 32) | ((guint64
)low
));
1672 decode_id (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1674 return decode_int (buf
, endbuf
, limit
);
1678 decode_string (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1680 int len
= decode_int (buf
, &buf
, limit
);
1688 s
= (char *)g_malloc (len
+ 1);
1691 memcpy (s
, buf
, len
);
1700 * Functions to encode protocol data
1704 buffer_init (Buffer
*buf
, int size
)
1706 buf
->buf
= (guint8
*)g_malloc (size
);
1708 buf
->end
= buf
->buf
+ size
;
1712 buffer_len (Buffer
*buf
)
1714 return buf
->p
- buf
->buf
;
1718 buffer_make_room (Buffer
*buf
, int size
)
1720 if (buf
->end
- buf
->p
< size
) {
1721 int new_size
= buf
->end
- buf
->buf
+ size
+ 32;
1722 guint8
*p
= (guint8
*)g_realloc (buf
->buf
, new_size
);
1723 size
= buf
->p
- buf
->buf
;
1726 buf
->end
= buf
->buf
+ new_size
;
1731 buffer_add_byte (Buffer
*buf
, guint8 val
)
1733 buffer_make_room (buf
, 1);
1739 buffer_add_short (Buffer
*buf
, guint32 val
)
1741 buffer_make_room (buf
, 2);
1742 buf
->p
[0] = (val
>> 8) & 0xff;
1743 buf
->p
[1] = (val
>> 0) & 0xff;
1748 buffer_add_int (Buffer
*buf
, guint32 val
)
1750 buffer_make_room (buf
, 4);
1751 buf
->p
[0] = (val
>> 24) & 0xff;
1752 buf
->p
[1] = (val
>> 16) & 0xff;
1753 buf
->p
[2] = (val
>> 8) & 0xff;
1754 buf
->p
[3] = (val
>> 0) & 0xff;
1759 buffer_add_long (Buffer
*buf
, guint64 l
)
1761 buffer_add_int (buf
, (l
>> 32) & 0xffffffff);
1762 buffer_add_int (buf
, (l
>> 0) & 0xffffffff);
1766 buffer_add_id (Buffer
*buf
, int id
)
1768 buffer_add_int (buf
, (guint64
)id
);
1772 buffer_add_data (Buffer
*buf
, guint8
*data
, int len
)
1774 buffer_make_room (buf
, len
);
1775 memcpy (buf
->p
, data
, len
);
1780 buffer_add_string (Buffer
*buf
, const char *str
)
1785 buffer_add_int (buf
, 0);
1788 buffer_add_int (buf
, len
);
1789 buffer_add_data (buf
, (guint8
*)str
, len
);
1794 buffer_add_buffer (Buffer
*buf
, Buffer
*data
)
1796 buffer_add_data (buf
, data
->buf
, buffer_len (data
));
1800 buffer_free (Buffer
*buf
)
1806 send_packet (int command_set
, int command
, Buffer
*data
)
1812 id
= InterlockedIncrement (&packet_id
);
1814 len
= data
->p
- data
->buf
+ 11;
1815 buffer_init (&buf
, len
);
1816 buffer_add_int (&buf
, len
);
1817 buffer_add_int (&buf
, id
);
1818 buffer_add_byte (&buf
, 0); /* flags */
1819 buffer_add_byte (&buf
, command_set
);
1820 buffer_add_byte (&buf
, command
);
1821 memcpy (buf
.buf
+ 11, data
->buf
, data
->p
- data
->buf
);
1823 res
= transport_send (buf
.buf
, len
);
1831 send_reply_packets (int npackets
, ReplyPacket
*packets
)
1838 for (i
= 0; i
< npackets
; ++i
)
1839 len
+= buffer_len (packets
[i
].data
) + 11;
1840 buffer_init (&buf
, len
);
1841 for (i
= 0; i
< npackets
; ++i
) {
1842 buffer_add_int (&buf
, buffer_len (packets
[i
].data
) + 11);
1843 buffer_add_int (&buf
, packets
[i
].id
);
1844 buffer_add_byte (&buf
, 0x80); /* flags */
1845 buffer_add_byte (&buf
, (packets
[i
].error
>> 8) & 0xff);
1846 buffer_add_byte (&buf
, packets
[i
].error
);
1847 buffer_add_buffer (&buf
, packets
[i
].data
);
1850 res
= transport_send (buf
.buf
, len
);
1858 send_reply_packet (int id
, int error
, Buffer
*data
)
1862 memset (&packet
, 0, sizeof (ReplyPacket
));
1864 packet
.error
= error
;
1867 return send_reply_packets (1, &packet
);
1871 send_buffered_reply_packets (void)
1875 send_reply_packets (nreply_packets
, reply_packets
);
1876 for (i
= 0; i
< nreply_packets
; ++i
)
1877 buffer_free (reply_packets
[i
].data
);
1878 DEBUG_PRINTF (1, "[dbg] Sent %d buffered reply packets [at=%lx].\n", nreply_packets
, (long)mono_100ns_ticks () / 10000);
1883 buffer_reply_packet (int id
, int error
, Buffer
*data
)
1887 if (nreply_packets
== 128)
1888 send_buffered_reply_packets ();
1890 p
= &reply_packets
[nreply_packets
];
1893 p
->data
= g_new0 (Buffer
, 1);
1894 buffer_init (p
->data
, buffer_len (data
));
1895 buffer_add_buffer (p
->data
, data
);
1904 * Represents an object accessible by the debugger client.
1907 /* Unique id used in the wire protocol to refer to objects */
1910 * A weakref gc handle pointing to the object. The gc handle is used to
1911 * detect if the object was garbage collected.
1916 /* Maps objid -> ObjRef */
1917 /* Protected by the loader lock */
1918 static GHashTable
*objrefs
;
1919 /* Protected by the loader lock */
1920 static GHashTable
*obj_to_objref
;
1921 /* Protected by the dbg lock */
1922 static MonoGHashTable
*suspended_objs
;
1925 free_objref (gpointer value
)
1927 ObjRef
*o
= (ObjRef
*)value
;
1929 mono_gchandle_free (o
->handle
);
1937 objrefs
= g_hash_table_new_full (NULL
, NULL
, NULL
, free_objref
);
1938 obj_to_objref
= g_hash_table_new (NULL
, NULL
);
1939 suspended_objs
= mono_g_hash_table_new_type (NULL
, NULL
, MONO_HASH_KEY_GC
, MONO_ROOT_SOURCE_DEBUGGER
, "suspended objects table");
1940 MONO_GC_REGISTER_ROOT_FIXED (suspended_objs
, MONO_ROOT_SOURCE_DEBUGGER
, "suspended objects table");
1944 objrefs_cleanup (void)
1946 g_hash_table_destroy (objrefs
);
1951 * Return an ObjRef for OBJ.
1954 get_objref (MonoObject
*obj
)
1957 GSList
*reflist
= NULL
, *l
;
1963 if (suspend_count
) {
1965 * Have to keep object refs created during suspensions alive for the duration of the suspension, so GCs during invokes don't collect them.
1968 mono_g_hash_table_insert (suspended_objs
, obj
, NULL
);
1972 mono_loader_lock ();
1974 /* FIXME: The tables can grow indefinitely */
1976 if (mono_gc_is_moving ()) {
1978 * Objects can move, so use a hash table mapping hash codes to lists of
1979 * ObjRef structures.
1981 hash
= mono_object_hash (obj
);
1983 reflist
= (GSList
*)g_hash_table_lookup (obj_to_objref
, GINT_TO_POINTER (hash
));
1984 for (l
= reflist
; l
; l
= l
->next
) {
1985 ref
= (ObjRef
*)l
->data
;
1986 if (ref
&& mono_gchandle_get_target (ref
->handle
) == obj
) {
1987 mono_loader_unlock ();
1992 /* Use a hash table with masked pointers to internalize object references */
1993 ref
= (ObjRef
*)g_hash_table_lookup (obj_to_objref
, GINT_TO_POINTER (~((gsize
)obj
)));
1994 /* ref might refer to a different object with the same addr which was GCd */
1995 if (ref
&& mono_gchandle_get_target (ref
->handle
) == obj
) {
1996 mono_loader_unlock ();
2001 ref
= g_new0 (ObjRef
, 1);
2002 ref
->id
= InterlockedIncrement (&objref_id
);
2003 ref
->handle
= mono_gchandle_new_weakref (obj
, FALSE
);
2005 g_hash_table_insert (objrefs
, GINT_TO_POINTER (ref
->id
), ref
);
2007 if (mono_gc_is_moving ()) {
2008 reflist
= g_slist_append (reflist
, ref
);
2009 g_hash_table_insert (obj_to_objref
, GINT_TO_POINTER (hash
), reflist
);
2011 g_hash_table_insert (obj_to_objref
, GINT_TO_POINTER (~((gsize
)obj
)), ref
);
2014 mono_loader_unlock ();
2020 true_pred (gpointer key
, gpointer value
, gpointer user_data
)
2026 clear_suspended_objs (void)
2029 mono_g_hash_table_foreach_remove (suspended_objs
, true_pred
, NULL
);
2034 get_objid (MonoObject
*obj
)
2039 return get_objref (obj
)->id
;
2043 * Set OBJ to the object identified by OBJID.
2044 * Returns 0 or an error code if OBJID is invalid or the object has been garbage
2048 get_object_allow_null (int objid
, MonoObject
**obj
)
2058 return ERR_INVALID_OBJECT
;
2060 mono_loader_lock ();
2062 ref
= (ObjRef
*)g_hash_table_lookup (objrefs
, GINT_TO_POINTER (objid
));
2065 *obj
= mono_gchandle_get_target (ref
->handle
);
2066 mono_loader_unlock ();
2068 return ERR_INVALID_OBJECT
;
2071 mono_loader_unlock ();
2072 return ERR_INVALID_OBJECT
;
2077 get_object (int objid
, MonoObject
**obj
)
2079 ErrorCode err
= get_object_allow_null (objid
, obj
);
2081 if (err
!= ERR_NONE
)
2084 return ERR_INVALID_OBJECT
;
2089 decode_objid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
2091 return decode_id (buf
, endbuf
, limit
);
2095 buffer_add_objid (Buffer
*buf
, MonoObject
*o
)
2097 buffer_add_id (buf
, get_objid (o
));
2116 * Represents a runtime structure accessible to the debugger client
2119 /* Unique id used in the wire protocol */
2121 /* Domain of the runtime structure, NULL if the domain was unloaded */
2128 MonoAssembly
*assembly
;
2129 MonoClassField
*field
;
2131 MonoProperty
*property
;
2136 /* Maps runtime structure -> Id */
2137 /* Protected by the dbg lock */
2138 GHashTable
*val_to_id
[ID_NUM
];
2139 /* Classes whose class load event has been sent */
2140 /* Protected by the loader lock */
2141 GHashTable
*loaded_classes
;
2142 /* Maps MonoClass->GPtrArray of file names */
2143 GHashTable
*source_files
;
2144 /* Maps source file basename -> GSList of classes */
2145 GHashTable
*source_file_to_class
;
2146 /* Same with ignore-case */
2147 GHashTable
*source_file_to_class_ignorecase
;
2151 /* Protected by the dbg lock */
2152 static GPtrArray
*ids
[ID_NUM
];
2159 for (i
= 0; i
< ID_NUM
; ++i
)
2160 ids
[i
] = g_ptr_array_new ();
2168 for (i
= 0; i
< ID_NUM
; ++i
) {
2170 for (j
= 0; j
< ids
[i
]->len
; ++j
)
2171 g_free (g_ptr_array_index (ids
[i
], j
));
2172 g_ptr_array_free (ids
[i
], TRUE
);
2179 mono_debugger_agent_free_domain_info (MonoDomain
*domain
)
2181 AgentDomainInfo
*info
= (AgentDomainInfo
*)domain_jit_info (domain
)->agent_info
;
2183 GHashTableIter iter
;
2184 GPtrArray
*file_names
;
2189 for (i
= 0; i
< ID_NUM
; ++i
)
2190 if (info
->val_to_id
[i
])
2191 g_hash_table_destroy (info
->val_to_id
[i
]);
2192 g_hash_table_destroy (info
->loaded_classes
);
2194 g_hash_table_iter_init (&iter
, info
->source_files
);
2195 while (g_hash_table_iter_next (&iter
, NULL
, (void**)&file_names
)) {
2196 for (i
= 0; i
< file_names
->len
; ++i
)
2197 g_free (g_ptr_array_index (file_names
, i
));
2198 g_ptr_array_free (file_names
, TRUE
);
2201 g_hash_table_iter_init (&iter
, info
->source_file_to_class
);
2202 while (g_hash_table_iter_next (&iter
, (void**)&basename
, (void**)&l
)) {
2207 g_hash_table_iter_init (&iter
, info
->source_file_to_class_ignorecase
);
2208 while (g_hash_table_iter_next (&iter
, (void**)&basename
, (void**)&l
)) {
2216 domain_jit_info (domain
)->agent_info
= NULL
;
2218 /* Clear ids referencing structures in the domain */
2220 for (i
= 0; i
< ID_NUM
; ++i
) {
2222 for (j
= 0; j
< ids
[i
]->len
; ++j
) {
2223 Id
*id
= (Id
*)g_ptr_array_index (ids
[i
], j
);
2224 if (id
->domain
== domain
)
2231 mono_loader_lock ();
2232 g_hash_table_remove (domains
, domain
);
2233 mono_loader_unlock ();
2236 static AgentDomainInfo
*
2237 get_agent_domain_info (MonoDomain
*domain
)
2239 AgentDomainInfo
*info
= NULL
;
2241 mono_domain_lock (domain
);
2243 info
= (AgentDomainInfo
*)domain_jit_info (domain
)->agent_info
;
2245 info
= g_new0 (AgentDomainInfo
, 1);
2246 domain_jit_info (domain
)->agent_info
= info
;
2247 info
->loaded_classes
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
2248 info
->source_files
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
2249 info
->source_file_to_class
= g_hash_table_new (g_str_hash
, g_str_equal
);
2250 info
->source_file_to_class_ignorecase
= g_hash_table_new (g_str_hash
, g_str_equal
);
2253 mono_domain_unlock (domain
);
2259 get_id (MonoDomain
*domain
, IdType type
, gpointer val
)
2262 AgentDomainInfo
*info
;
2267 info
= get_agent_domain_info (domain
);
2271 if (info
->val_to_id
[type
] == NULL
)
2272 info
->val_to_id
[type
] = g_hash_table_new (mono_aligned_addr_hash
, NULL
);
2274 id
= (Id
*)g_hash_table_lookup (info
->val_to_id
[type
], val
);
2280 id
= g_new0 (Id
, 1);
2282 id
->id
= ids
[type
]->len
+ 1;
2283 id
->domain
= domain
;
2286 g_hash_table_insert (info
->val_to_id
[type
], val
, id
);
2287 g_ptr_array_add (ids
[type
], id
);
2294 static inline gpointer
2295 decode_ptr_id (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, IdType type
, MonoDomain
**domain
, ErrorCode
*err
)
2299 int id
= decode_id (buf
, endbuf
, limit
);
2308 // FIXME: error handling
2310 g_assert (id
> 0 && id
<= ids
[type
]->len
);
2312 res
= (Id
*)g_ptr_array_index (ids
[type
], GPOINTER_TO_INT (id
- 1));
2315 if (res
->domain
== NULL
) {
2316 DEBUG_PRINTF (1, "ERR_UNLOADED, id=%d, type=%d.\n", id
, type
);
2317 *err
= ERR_UNLOADED
;
2322 *domain
= res
->domain
;
2324 return res
->data
.val
;
2328 buffer_add_ptr_id (Buffer
*buf
, MonoDomain
*domain
, IdType type
, gpointer val
)
2330 int id
= get_id (domain
, type
, val
);
2332 buffer_add_id (buf
, id
);
2336 static inline MonoClass
*
2337 decode_typeid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2341 klass
= (MonoClass
*)decode_ptr_id (buf
, endbuf
, limit
, ID_TYPE
, domain
, err
);
2342 if (G_UNLIKELY (log_level
>= 2) && klass
) {
2345 s
= mono_type_full_name (&klass
->byval_arg
);
2346 DEBUG_PRINTF (2, "[dbg] recv class [%s]\n", s
);
2352 static inline MonoAssembly
*
2353 decode_assemblyid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2355 return (MonoAssembly
*)decode_ptr_id (buf
, endbuf
, limit
, ID_ASSEMBLY
, domain
, err
);
2358 static inline MonoImage
*
2359 decode_moduleid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2361 return (MonoImage
*)decode_ptr_id (buf
, endbuf
, limit
, ID_MODULE
, domain
, err
);
2364 static inline MonoMethod
*
2365 decode_methodid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2369 m
= (MonoMethod
*)decode_ptr_id (buf
, endbuf
, limit
, ID_METHOD
, domain
, err
);
2370 if (G_UNLIKELY (log_level
>= 2) && m
) {
2373 s
= mono_method_full_name (m
, TRUE
);
2374 DEBUG_PRINTF (2, "[dbg] recv method [%s]\n", s
);
2380 static inline MonoClassField
*
2381 decode_fieldid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2383 return (MonoClassField
*)decode_ptr_id (buf
, endbuf
, limit
, ID_FIELD
, domain
, err
);
2386 static inline MonoDomain
*
2387 decode_domainid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2389 return (MonoDomain
*)decode_ptr_id (buf
, endbuf
, limit
, ID_DOMAIN
, domain
, err
);
2392 static inline MonoProperty
*
2393 decode_propertyid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2395 return (MonoProperty
*)decode_ptr_id (buf
, endbuf
, limit
, ID_PROPERTY
, domain
, err
);
2399 buffer_add_typeid (Buffer
*buf
, MonoDomain
*domain
, MonoClass
*klass
)
2401 buffer_add_ptr_id (buf
, domain
, ID_TYPE
, klass
);
2402 if (G_UNLIKELY (log_level
>= 2) && klass
) {
2405 s
= mono_type_full_name (&klass
->byval_arg
);
2406 if (is_debugger_thread ())
2407 DEBUG_PRINTF (2, "[dbg] send class [%s]\n", s
);
2409 DEBUG_PRINTF (2, "[%p] send class [%s]\n", (gpointer
) (gsize
) mono_native_thread_id_get (), s
);
2415 buffer_add_methodid (Buffer
*buf
, MonoDomain
*domain
, MonoMethod
*method
)
2417 buffer_add_ptr_id (buf
, domain
, ID_METHOD
, method
);
2418 if (G_UNLIKELY (log_level
>= 2) && method
) {
2421 s
= mono_method_full_name (method
, 1);
2422 DEBUG_PRINTF (2, "[dbg] send method [%s]\n", s
);
2428 buffer_add_assemblyid (Buffer
*buf
, MonoDomain
*domain
, MonoAssembly
*assembly
)
2432 id
= buffer_add_ptr_id (buf
, domain
, ID_ASSEMBLY
, assembly
);
2433 if (G_UNLIKELY (log_level
>= 2) && assembly
)
2434 DEBUG_PRINTF (2, "[dbg] send assembly [%s][%s][%d]\n", assembly
->aname
.name
, domain
->friendly_name
, id
);
2438 buffer_add_moduleid (Buffer
*buf
, MonoDomain
*domain
, MonoImage
*image
)
2440 buffer_add_ptr_id (buf
, domain
, ID_MODULE
, image
);
2444 buffer_add_fieldid (Buffer
*buf
, MonoDomain
*domain
, MonoClassField
*field
)
2446 buffer_add_ptr_id (buf
, domain
, ID_FIELD
, field
);
2450 buffer_add_propertyid (Buffer
*buf
, MonoDomain
*domain
, MonoProperty
*property
)
2452 buffer_add_ptr_id (buf
, domain
, ID_PROPERTY
, property
);
2456 buffer_add_domainid (Buffer
*buf
, MonoDomain
*domain
)
2458 buffer_add_ptr_id (buf
, domain
, ID_DOMAIN
, domain
);
2461 static void invoke_method (void);
2468 * save_thread_context:
2470 * Set CTX as the current threads context which is used for computing stack traces.
2471 * This function is signal-safe.
2474 save_thread_context (MonoContext
*ctx
)
2476 DebuggerTlsData
*tls
;
2478 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
2482 mono_thread_state_init_from_monoctx (&tls
->context
, ctx
);
2484 mono_thread_state_init_from_current (&tls
->context
);
2487 /* Number of threads suspended */
2489 * If this is equal to the size of thread_to_tls, the runtime is considered
2492 static gint32 threads_suspend_count
;
2494 static MonoCoopMutex suspend_mutex
;
2496 /* Cond variable used to wait for suspend_count becoming 0 */
2497 static MonoCoopCond suspend_cond
;
2499 /* Semaphore used to wait for a thread becoming suspended */
2500 static MonoCoopSem suspend_sem
;
2505 mono_coop_mutex_init (&suspend_mutex
);
2506 mono_coop_cond_init (&suspend_cond
);
2507 mono_coop_sem_init (&suspend_sem
, 0);
2512 StackFrameInfo last_frame
;
2513 gboolean last_frame_set
;
2517 } GetLastFrameUserData
;
2520 get_last_frame (StackFrameInfo
*info
, MonoContext
*ctx
, gpointer user_data
)
2522 GetLastFrameUserData
*data
= (GetLastFrameUserData
*)user_data
;
2524 if (info
->type
== FRAME_TYPE_MANAGED_TO_NATIVE
|| info
->type
== FRAME_TYPE_TRAMPOLINE
)
2527 if (!data
->last_frame_set
) {
2528 /* Store the last frame */
2529 memcpy (&data
->last_frame
, info
, sizeof (StackFrameInfo
));
2530 data
->last_frame_set
= TRUE
;
2533 /* Store the context/lmf for the frame above the last frame */
2534 memcpy (&data
->ctx
, ctx
, sizeof (MonoContext
));
2535 data
->lmf
= info
->lmf
;
2536 data
->domain
= info
->domain
;
2542 copy_unwind_state_from_frame_data (MonoThreadUnwindState
*to
, GetLastFrameUserData
*data
, gpointer jit_tls
)
2544 memcpy (&to
->ctx
, &data
->ctx
, sizeof (MonoContext
));
2546 to
->unwind_data
[MONO_UNWIND_DATA_DOMAIN
] = data
->domain
;
2547 to
->unwind_data
[MONO_UNWIND_DATA_LMF
] = data
->lmf
;
2548 to
->unwind_data
[MONO_UNWIND_DATA_JIT_TLS
] = jit_tls
;
2555 * Process interruption of a thread. This should be signal safe.
2557 * This always runs in the debugger thread.
2560 thread_interrupt (DebuggerTlsData
*tls
, MonoThreadInfo
*info
, MonoJitInfo
*ji
)
2563 MonoNativeThreadId tid
;
2567 ip
= MONO_CONTEXT_GET_IP (&mono_thread_info_get_suspend_state (info
)->ctx
);
2568 tid
= mono_thread_info_get_tid (info
);
2570 // FIXME: Races when the thread leaves managed code before hitting a single step
2573 if (ji
&& !ji
->is_trampoline
) {
2574 /* Running managed code, will be suspended by the single step code */
2575 DEBUG_PRINTF (1, "[%p] Received interrupt while at %s(%p), continuing.\n", (gpointer
)(gsize
)tid
, jinfo_get_method (ji
)->name
, ip
);
2578 * Running native code, will be suspended when it returns to/enters
2579 * managed code. Treat it as already suspended.
2580 * This might interrupt the code in process_single_step_inner (), we use the
2581 * tls->suspending flag to avoid races when that happens.
2583 if (!tls
->suspended
&& !tls
->suspending
) {
2584 GetLastFrameUserData data
;
2586 // FIXME: printf is not signal safe, but this is only used during
2587 // debugger debugging
2589 DEBUG_PRINTF (1, "[%p] Received interrupt while at %p, treating as suspended.\n", (gpointer
)(gsize
)tid
, ip
);
2590 //save_thread_context (&ctx);
2593 /* Already terminated */
2597 * We are in a difficult position: we want to be able to provide stack
2598 * traces for this thread, but we can't use the current ctx+lmf, since
2599 * the thread is still running, so it might return to managed code,
2600 * making these invalid.
2601 * So we start a stack walk and save the first frame, along with the
2602 * parent frame's ctx+lmf. This (hopefully) works because the thread will be
2603 * suspended when it returns to managed code, so the parent's ctx should
2606 data
.last_frame_set
= FALSE
;
2607 mono_get_eh_callbacks ()->mono_walk_stack_with_state (get_last_frame
, mono_thread_info_get_suspend_state (info
), MONO_UNWIND_SIGNAL_SAFE
, &data
);
2608 if (data
.last_frame_set
) {
2609 gpointer jit_tls
= ((MonoThreadInfo
*)tls
->thread
->thread_info
)->jit_data
;
2611 memcpy (&tls
->async_last_frame
, &data
.last_frame
, sizeof (StackFrameInfo
));
2613 copy_unwind_state_from_frame_data (&tls
->async_state
, &data
, jit_tls
);
2614 copy_unwind_state_from_frame_data (&tls
->context
, &data
, jit_tls
);
2616 tls
->async_state
.valid
= FALSE
;
2619 mono_memory_barrier ();
2621 tls
->suspended
= TRUE
;
2622 mono_coop_sem_post (&suspend_sem
);
2628 * reset_native_thread_suspend_state:
2630 * Reset the suspended flag and state on native threads
2633 reset_native_thread_suspend_state (gpointer key
, gpointer value
, gpointer user_data
)
2635 DebuggerTlsData
*tls
= (DebuggerTlsData
*)value
;
2637 if (!tls
->really_suspended
&& tls
->suspended
) {
2638 tls
->suspended
= FALSE
;
2640 * The thread might still be running if it was executing native code, so the state won't be invalided by
2641 * suspend_current ().
2643 tls
->context
.valid
= FALSE
;
2644 tls
->async_state
.valid
= FALSE
;
2645 invalidate_frames (tls
);
2650 DebuggerTlsData
*tls
;
2651 gboolean valid_info
;
2654 static SuspendThreadResult
2655 debugger_interrupt_critical (MonoThreadInfo
*info
, gpointer user_data
)
2657 InterruptData
*data
= (InterruptData
*)user_data
;
2660 data
->valid_info
= TRUE
;
2661 ji
= mono_jit_info_table_find_internal (
2662 (MonoDomain
*)mono_thread_info_get_suspend_state (info
)->unwind_data
[MONO_UNWIND_DATA_DOMAIN
],
2663 (char *)MONO_CONTEXT_GET_IP (&mono_thread_info_get_suspend_state (info
)->ctx
),
2667 /* This is signal safe */
2668 thread_interrupt (data
->tls
, info
, ji
);
2669 return MonoResumeThread
;
2675 * Notify a thread that it needs to suspend.
2678 notify_thread (gpointer key
, gpointer value
, gpointer user_data
)
2680 MonoInternalThread
*thread
= (MonoInternalThread
*)key
;
2681 DebuggerTlsData
*tls
= (DebuggerTlsData
*)value
;
2682 MonoNativeThreadId tid
= MONO_UINT_TO_NATIVE_THREAD_ID (thread
->tid
);
2684 if (mono_native_thread_id_equals (mono_native_thread_id_get (), tid
) || tls
->terminated
)
2687 DEBUG_PRINTF (1, "[%p] Interrupting %p...\n", (gpointer
) (gsize
) mono_native_thread_id_get (), (gpointer
)tid
);
2689 /* This is _not_ equivalent to mono_thread_internal_abort () */
2690 InterruptData interrupt_data
= { 0 };
2691 interrupt_data
.tls
= tls
;
2693 mono_thread_info_safe_suspend_and_run ((MonoNativeThreadId
)(gpointer
)(gsize
)thread
->tid
, FALSE
, debugger_interrupt_critical
, &interrupt_data
);
2694 if (!interrupt_data
.valid_info
) {
2695 DEBUG_PRINTF (1, "[%p] mono_thread_info_suspend_sync () failed for %p...\n", (gpointer
) (gsize
) mono_native_thread_id_get (), (gpointer
)tid
);
2697 * Attached thread which died without detaching.
2699 tls
->terminated
= TRUE
;
2704 process_suspend (DebuggerTlsData
*tls
, MonoContext
*ctx
)
2706 guint8
*ip
= (guint8
*)MONO_CONTEXT_GET_IP (ctx
);
2710 if (mono_loader_lock_is_owned_by_self ()) {
2712 * Shortcut for the check in suspend_current (). This speeds up processing
2713 * when executing long running code inside the loader lock, i.e. assembly load
2719 if (is_debugger_thread ())
2722 /* Prevent races with mono_debugger_agent_thread_interrupt () */
2723 if (suspend_count
- tls
->resume_count
> 0)
2724 tls
->suspending
= TRUE
;
2726 DEBUG_PRINTF (1, "[%p] Received single step event for suspending.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
2728 if (suspend_count
- tls
->resume_count
== 0) {
2730 * We are executing a single threaded invoke but the single step for
2731 * suspending is still active.
2732 * FIXME: This slows down single threaded invokes.
2734 DEBUG_PRINTF (1, "[%p] Ignored during single threaded invoke.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
2738 ji
= mini_jit_info_table_find (mono_domain_get (), (char*)ip
, NULL
);
2740 /* Can't suspend in these methods */
2741 method
= jinfo_get_method (ji
);
2742 if (method
->klass
== mono_defaults
.string_class
&& (!strcmp (method
->name
, "memset") || strstr (method
->name
, "memcpy")))
2745 save_thread_context (ctx
);
2753 * Increase the suspend count of the VM. While the suspend count is greater
2754 * than 0, runtime threads are suspended at certain points during execution.
2759 mono_loader_lock ();
2761 mono_coop_mutex_lock (&suspend_mutex
);
2765 DEBUG_PRINTF (1, "[%p] Suspending vm...\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
2767 if (suspend_count
== 1) {
2768 // FIXME: Is it safe to call this inside the lock ?
2769 start_single_stepping ();
2770 mono_g_hash_table_foreach (thread_to_tls
, notify_thread
, NULL
);
2773 mono_coop_mutex_unlock (&suspend_mutex
);
2775 if (suspend_count
== 1)
2777 * Suspend creation of new threadpool threads, since they cannot run
2779 mono_threadpool_ms_suspend ();
2781 mono_loader_unlock ();
2787 * Decrease the suspend count of the VM. If the count reaches 0, runtime threads
2795 g_assert (is_debugger_thread ());
2797 mono_loader_lock ();
2799 mono_coop_mutex_lock (&suspend_mutex
);
2801 g_assert (suspend_count
> 0);
2804 DEBUG_PRINTF (1, "[%p] Resuming vm, suspend count=%d...\n", (gpointer
) (gsize
) mono_native_thread_id_get (), suspend_count
);
2806 if (suspend_count
== 0) {
2807 // FIXME: Is it safe to call this inside the lock ?
2808 stop_single_stepping ();
2809 mono_g_hash_table_foreach (thread_to_tls
, reset_native_thread_suspend_state
, NULL
);
2812 /* Signal this even when suspend_count > 0, since some threads might have resume_count > 0 */
2813 err
= mono_coop_cond_broadcast (&suspend_cond
);
2814 g_assert (err
== 0);
2816 mono_coop_mutex_unlock (&suspend_mutex
);
2817 //g_assert (err == 0);
2819 if (suspend_count
== 0)
2820 mono_threadpool_ms_resume ();
2822 mono_loader_unlock ();
2828 * Resume just one thread.
2831 resume_thread (MonoInternalThread
*thread
)
2834 DebuggerTlsData
*tls
;
2836 g_assert (is_debugger_thread ());
2838 mono_loader_lock ();
2840 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
2843 mono_coop_mutex_lock (&suspend_mutex
);
2845 g_assert (suspend_count
> 0);
2847 DEBUG_PRINTF (1, "[sdb] Resuming thread %p...\n", (gpointer
)(gssize
)thread
->tid
);
2849 tls
->resume_count
+= suspend_count
;
2852 * Signal suspend_count without decreasing suspend_count, the threads will wake up
2853 * but only the one whose resume_count field is > 0 will be resumed.
2855 err
= mono_coop_cond_broadcast (&suspend_cond
);
2856 g_assert (err
== 0);
2858 mono_coop_mutex_unlock (&suspend_mutex
);
2859 //g_assert (err == 0);
2861 mono_loader_unlock ();
2865 free_frames (StackFrame
**frames
, int nframes
)
2869 for (i
= 0; i
< nframes
; ++i
) {
2870 if (frames
[i
]->jit
)
2871 mono_debug_free_method_jit_info (frames
[i
]->jit
);
2872 g_free (frames
[i
]);
2878 invalidate_frames (DebuggerTlsData
*tls
)
2881 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
2884 free_frames (tls
->frames
, tls
->frame_count
);
2885 tls
->frame_count
= 0;
2888 free_frames (tls
->restore_frames
, tls
->restore_frame_count
);
2889 tls
->restore_frame_count
= 0;
2890 tls
->restore_frames
= NULL
;
2896 * Suspend the current thread until the runtime is resumed. If the thread has a
2897 * pending invoke, then the invoke is executed before this function returns.
2900 suspend_current (void)
2902 DebuggerTlsData
*tls
;
2905 g_assert (!is_debugger_thread ());
2907 if (mono_loader_lock_is_owned_by_self ()) {
2909 * If we own the loader mutex, can't suspend until we release it, since the
2910 * whole runtime can deadlock otherwise.
2915 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
2918 mono_coop_mutex_lock (&suspend_mutex
);
2920 tls
->suspending
= FALSE
;
2921 tls
->really_suspended
= TRUE
;
2923 if (!tls
->suspended
) {
2924 tls
->suspended
= TRUE
;
2925 mono_coop_sem_post (&suspend_sem
);
2928 DEBUG_PRINTF (1, "[%p] Suspended.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
2930 while (suspend_count
- tls
->resume_count
> 0) {
2931 err
= mono_coop_cond_wait (&suspend_cond
, &suspend_mutex
);
2932 g_assert (err
== 0);
2935 tls
->suspended
= FALSE
;
2936 tls
->really_suspended
= FALSE
;
2938 threads_suspend_count
--;
2940 mono_coop_mutex_unlock (&suspend_mutex
);
2942 DEBUG_PRINTF (1, "[%p] Resumed.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
2944 if (tls
->pending_invoke
) {
2945 /* Save the original context */
2946 tls
->pending_invoke
->has_ctx
= TRUE
;
2947 tls
->pending_invoke
->ctx
= tls
->context
.ctx
;
2952 /* The frame info becomes invalid after a resume */
2953 tls
->context
.valid
= FALSE
;
2954 tls
->async_state
.valid
= FALSE
;
2955 invalidate_frames (tls
);
2959 count_thread (gpointer key
, gpointer value
, gpointer user_data
)
2961 DebuggerTlsData
*tls
= (DebuggerTlsData
*)value
;
2963 if (!tls
->suspended
&& !tls
->terminated
)
2964 *(int*)user_data
= *(int*)user_data
+ 1;
2968 count_threads_to_wait_for (void)
2972 mono_loader_lock ();
2973 mono_g_hash_table_foreach (thread_to_tls
, count_thread
, &count
);
2974 mono_loader_unlock ();
2982 * Wait until the runtime is completely suspended.
2985 wait_for_suspend (void)
2987 int nthreads
, nwait
, err
;
2988 gboolean waited
= FALSE
;
2990 // FIXME: Threads starting/stopping ?
2991 mono_loader_lock ();
2992 nthreads
= mono_g_hash_table_size (thread_to_tls
);
2993 mono_loader_unlock ();
2996 nwait
= count_threads_to_wait_for ();
2998 DEBUG_PRINTF (1, "Waiting for %d(%d) threads to suspend...\n", nwait
, nthreads
);
2999 err
= mono_coop_sem_wait (&suspend_sem
, MONO_SEM_FLAGS_NONE
);
3000 g_assert (err
== 0);
3008 DEBUG_PRINTF (1, "%d threads suspended.\n", nthreads
);
3014 * Return whenever the runtime is suspended.
3019 return count_threads_to_wait_for () == 0;
3023 no_seq_points_found (MonoMethod
*method
, int offset
)
3026 * This can happen in full-aot mode with assemblies AOTed without the 'soft-debug' option to save space.
3028 printf ("Unable to find seq points for method '%s', offset 0x%x.\n", mono_method_full_name (method
, TRUE
), offset
);
3032 DebuggerTlsData
*tls
;
3034 } ComputeFramesUserData
;
3037 process_frame (StackFrameInfo
*info
, MonoContext
*ctx
, gpointer user_data
)
3039 ComputeFramesUserData
*ud
= (ComputeFramesUserData
*)user_data
;
3041 MonoMethod
*method
, *actual_method
, *api_method
;
3045 if (info
->type
!= FRAME_TYPE_MANAGED
) {
3046 if (info
->type
== FRAME_TYPE_DEBUGGER_INVOKE
) {
3047 /* Mark the last frame as an invoke frame */
3049 ((StackFrame
*)g_slist_last (ud
->frames
)->data
)->flags
|= FRAME_FLAG_DEBUGGER_INVOKE
;
3055 method
= jinfo_get_method (info
->ji
);
3057 method
= info
->method
;
3058 actual_method
= info
->actual_method
;
3059 api_method
= method
;
3064 if (!method
|| (method
->wrapper_type
&& method
->wrapper_type
!= MONO_WRAPPER_DYNAMIC_METHOD
&& method
->wrapper_type
!= MONO_WRAPPER_MANAGED_TO_NATIVE
))
3067 if (info
->il_offset
== -1) {
3068 /* mono_debug_il_offset_from_address () doesn't seem to be precise enough (#2092) */
3069 if (ud
->frames
== NULL
) {
3070 if (mono_find_prev_seq_point_for_native_offset (info
->domain
, method
, info
->native_offset
, NULL
, &sp
))
3071 info
->il_offset
= sp
.il_offset
;
3073 if (info
->il_offset
== -1)
3074 info
->il_offset
= mono_debug_il_offset_from_address (method
, info
->domain
, info
->native_offset
);
3077 DEBUG_PRINTF (1, "\tFrame: %s:[il=0x%x, native=0x%x] %d\n", mono_method_full_name (method
, TRUE
), info
->il_offset
, info
->native_offset
, info
->managed
);
3079 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) {
3080 if (!CHECK_PROTOCOL_VERSION (2, 17))
3081 /* Older clients can't handle this flag */
3083 api_method
= mono_marshal_method_from_wrapper (method
);
3086 actual_method
= api_method
;
3087 flags
|= FRAME_FLAG_NATIVE_TRANSITION
;
3090 frame
= g_new0 (StackFrame
, 1);
3091 frame
->method
= method
;
3092 frame
->actual_method
= actual_method
;
3093 frame
->api_method
= api_method
;
3094 frame
->il_offset
= info
->il_offset
;
3095 frame
->native_offset
= info
->native_offset
;
3096 frame
->flags
= flags
;
3097 frame
->ji
= info
->ji
;
3098 if (info
->reg_locations
)
3099 memcpy (frame
->reg_locations
, info
->reg_locations
, MONO_MAX_IREGS
* sizeof (mgreg_t
*));
3102 frame
->has_ctx
= TRUE
;
3104 frame
->domain
= info
->domain
;
3106 ud
->frames
= g_slist_append (ud
->frames
, frame
);
3112 process_filter_frame (StackFrameInfo
*info
, MonoContext
*ctx
, gpointer user_data
)
3114 ComputeFramesUserData
*ud
= (ComputeFramesUserData
*)user_data
;
3117 * 'tls->filter_ctx' is the location of the throw site.
3119 * mono_walk_stack() will never actually hit the throw site, but unwind
3120 * directly from the filter to the call site; we abort stack unwinding here
3121 * once this happens and resume from the throw site.
3124 if (MONO_CONTEXT_GET_SP (ctx
) >= MONO_CONTEXT_GET_SP (&ud
->tls
->filter_state
.ctx
))
3127 return process_frame (info
, ctx
, user_data
);
3131 * Return a malloc-ed list of StackFrame structures.
3134 compute_frame_info_from (MonoInternalThread
*thread
, DebuggerTlsData
*tls
, MonoThreadUnwindState
*state
, int *out_nframes
)
3136 ComputeFramesUserData user_data
;
3137 MonoUnwindOptions opts
= (MonoUnwindOptions
)(MONO_UNWIND_DEFAULT
| MONO_UNWIND_REG_LOCATIONS
);
3142 user_data
.tls
= tls
;
3143 user_data
.frames
= NULL
;
3145 mono_walk_stack_with_state (process_frame
, state
, opts
, &user_data
);
3147 nframes
= g_slist_length (user_data
.frames
);
3148 res
= g_new0 (StackFrame
*, nframes
);
3149 l
= user_data
.frames
;
3150 for (i
= 0; i
< nframes
; ++i
) {
3151 res
[i
] = (StackFrame
*)l
->data
;
3154 *out_nframes
= nframes
;
3160 compute_frame_info (MonoInternalThread
*thread
, DebuggerTlsData
*tls
)
3162 ComputeFramesUserData user_data
;
3164 int i
, findex
, new_frame_count
;
3165 StackFrame
**new_frames
, *f
;
3166 MonoUnwindOptions opts
= (MonoUnwindOptions
)(MONO_UNWIND_DEFAULT
| MONO_UNWIND_REG_LOCATIONS
);
3168 // FIXME: Locking on tls
3169 if (tls
->frames
&& tls
->frames_up_to_date
)
3172 DEBUG_PRINTF (1, "Frames for %p(tid=%lx):\n", thread
, (glong
)thread
->tid
);
3174 user_data
.tls
= tls
;
3175 user_data
.frames
= NULL
;
3176 if (tls
->terminated
) {
3177 tls
->frame_count
= 0;
3179 } if (!tls
->really_suspended
&& tls
->async_state
.valid
) {
3180 /* Have to use the state saved by the signal handler */
3181 process_frame (&tls
->async_last_frame
, NULL
, &user_data
);
3182 mono_walk_stack_with_state (process_frame
, &tls
->async_state
, opts
, &user_data
);
3183 } else if (tls
->filter_state
.valid
) {
3185 * We are inside an exception filter.
3187 * First we add all the frames from inside the filter; 'tls->ctx' has the current context.
3189 if (tls
->context
.valid
)
3190 mono_walk_stack_with_state (process_filter_frame
, &tls
->context
, opts
, &user_data
);
3192 * After that, we resume unwinding from the location where the exception has been thrown.
3194 mono_walk_stack_with_state (process_frame
, &tls
->filter_state
, opts
, &user_data
);
3195 } else if (tls
->context
.valid
) {
3196 mono_walk_stack_with_state (process_frame
, &tls
->context
, opts
, &user_data
);
3199 tls
->frame_count
= 0;
3203 new_frame_count
= g_slist_length (user_data
.frames
);
3204 new_frames
= g_new0 (StackFrame
*, new_frame_count
);
3206 for (tmp
= user_data
.frames
; tmp
; tmp
= tmp
->next
) {
3207 f
= (StackFrame
*)tmp
->data
;
3210 * Reuse the id for already existing stack frames, so invokes don't invalidate
3211 * the still valid stack frames.
3213 for (i
= 0; i
< tls
->frame_count
; ++i
) {
3214 if (MONO_CONTEXT_GET_SP (&tls
->frames
[i
]->ctx
) == MONO_CONTEXT_GET_SP (&f
->ctx
)) {
3215 f
->id
= tls
->frames
[i
]->id
;
3220 if (i
>= tls
->frame_count
)
3221 f
->id
= InterlockedIncrement (&frame_id
);
3223 new_frames
[findex
++] = f
;
3226 g_slist_free (user_data
.frames
);
3228 invalidate_frames (tls
);
3230 tls
->frames
= new_frames
;
3231 tls
->frame_count
= new_frame_count
;
3232 tls
->frames_up_to_date
= TRUE
;
3236 * GHFunc to emit an appdomain creation event
3237 * @param key Don't care
3238 * @param value A loaded appdomain
3239 * @param user_data Don't care
3242 emit_appdomain_load (gpointer key
, gpointer value
, gpointer user_data
)
3244 process_profiler_event (EVENT_KIND_APPDOMAIN_CREATE
, value
);
3245 g_hash_table_foreach (get_agent_domain_info ((MonoDomain
*)value
)->loaded_classes
, emit_type_load
, NULL
);
3249 * GHFunc to emit a thread start event
3250 * @param key A thread id
3251 * @param value A thread object
3252 * @param user_data Don't care
3255 emit_thread_start (gpointer key
, gpointer value
, gpointer user_data
)
3257 if (!mono_native_thread_id_equals (MONO_UINT_TO_NATIVE_THREAD_ID (GPOINTER_TO_UINT (key
)), debugger_thread_id
))
3258 process_profiler_event (EVENT_KIND_THREAD_START
, value
);
3262 * GFunc to emit an assembly load event
3263 * @param value A loaded assembly
3264 * @param user_data Don't care
3267 emit_assembly_load (gpointer value
, gpointer user_data
)
3269 process_profiler_event (EVENT_KIND_ASSEMBLY_LOAD
, value
);
3273 * GFunc to emit a type load event
3274 * @param value A loaded type
3275 * @param user_data Don't care
3278 emit_type_load (gpointer key
, gpointer value
, gpointer user_data
)
3280 process_profiler_event (EVENT_KIND_TYPE_LOAD
, value
);
3284 strdup_tolower (char *s
)
3289 for (p
= s2
; *p
; ++p
)
3295 * Same as g_path_get_basename () but handles windows paths as well,
3296 * which can occur in .mdb files created by pdb2mdb.
3299 dbg_path_get_basename (const char *filename
)
3303 if (!filename
|| strchr (filename
, '/') || !strchr (filename
, '\\'))
3304 return g_path_get_basename (filename
);
3308 /* No separator -> filename */
3309 r
= strrchr (filename
, '\\');
3311 return g_strdup (filename
);
3313 /* Trailing slash, remove component */
3315 char *copy
= g_strdup (filename
);
3316 copy
[r
-filename
] = 0;
3317 r
= strrchr (copy
, '\\');
3321 return g_strdup ("/");
3323 r
= g_strdup (&r
[1]);
3328 return g_strdup (&r
[1]);
3332 init_jit_info_dbg_attrs (MonoJitInfo
*ji
)
3334 static MonoClass
*hidden_klass
, *step_through_klass
, *non_user_klass
;
3336 MonoCustomAttrInfo
*ainfo
;
3338 if (ji
->dbg_attrs_inited
)
3342 hidden_klass
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Diagnostics", "DebuggerHiddenAttribute");
3344 if (!step_through_klass
)
3345 step_through_klass
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Diagnostics", "DebuggerStepThroughAttribute");
3347 if (!non_user_klass
)
3348 non_user_klass
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Diagnostics", "DebuggerNonUserCodeAttribute");
3350 ainfo
= mono_custom_attrs_from_method_checked (jinfo_get_method (ji
), &error
);
3351 mono_error_cleanup (&error
); /* FIXME don't swallow the error? */
3353 if (mono_custom_attrs_has_attr (ainfo
, hidden_klass
))
3354 ji
->dbg_hidden
= TRUE
;
3355 if (mono_custom_attrs_has_attr (ainfo
, step_through_klass
))
3356 ji
->dbg_step_through
= TRUE
;
3357 if (mono_custom_attrs_has_attr (ainfo
, non_user_klass
))
3358 ji
->dbg_non_user_code
= TRUE
;
3359 mono_custom_attrs_free (ainfo
);
3362 ainfo
= mono_custom_attrs_from_class_checked (jinfo_get_method (ji
)->klass
, &error
);
3363 mono_error_cleanup (&error
); /* FIXME don't swallow the error? */
3365 if (mono_custom_attrs_has_attr (ainfo
, step_through_klass
))
3366 ji
->dbg_step_through
= TRUE
;
3367 if (mono_custom_attrs_has_attr (ainfo
, non_user_klass
))
3368 ji
->dbg_non_user_code
= TRUE
;
3369 mono_custom_attrs_free (ainfo
);
3372 mono_memory_barrier ();
3373 ji
->dbg_attrs_inited
= TRUE
;
3381 * create_event_list:
3383 * Return a list of event request ids matching EVENT, starting from REQS, which
3384 * can be NULL to include all event requests. Set SUSPEND_POLICY to the suspend
3386 * We return request ids, instead of requests, to simplify threading, since
3387 * requests could be deleted anytime when the loader lock is not held.
3388 * LOCKING: Assumes the loader lock is held.
3391 create_event_list (EventKind event
, GPtrArray
*reqs
, MonoJitInfo
*ji
, EventInfo
*ei
, int *suspend_policy
)
3394 GSList
*events
= NULL
;
3396 *suspend_policy
= SUSPEND_POLICY_NONE
;
3399 reqs
= event_requests
;
3404 for (i
= 0; i
< reqs
->len
; ++i
) {
3405 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (reqs
, i
);
3406 if (req
->event_kind
== event
) {
3407 gboolean filtered
= FALSE
;
3410 for (j
= 0; j
< req
->nmodifiers
; ++j
) {
3411 Modifier
*mod
= &req
->modifiers
[j
];
3413 if (mod
->kind
== MOD_KIND_COUNT
) {
3415 if (mod
->data
.count
> 0) {
3416 if (mod
->data
.count
> 0) {
3418 if (mod
->data
.count
== 0)
3422 } else if (mod
->kind
== MOD_KIND_THREAD_ONLY
) {
3423 if (mod
->data
.thread
!= mono_thread_internal_current ())
3425 } else if (mod
->kind
== MOD_KIND_EXCEPTION_ONLY
&& ei
) {
3426 if (mod
->data
.exc_class
&& mod
->subclasses
&& !mono_class_is_assignable_from (mod
->data
.exc_class
, ei
->exc
->vtable
->klass
))
3428 if (mod
->data
.exc_class
&& !mod
->subclasses
&& mod
->data
.exc_class
!= ei
->exc
->vtable
->klass
)
3430 if (ei
->caught
&& !mod
->caught
)
3432 if (!ei
->caught
&& !mod
->uncaught
)
3434 } else if (mod
->kind
== MOD_KIND_ASSEMBLY_ONLY
&& ji
) {
3436 gboolean found
= FALSE
;
3437 MonoAssembly
**assemblies
= mod
->data
.assemblies
;
3440 for (k
= 0; assemblies
[k
]; ++k
)
3441 if (assemblies
[k
] == jinfo_get_method (ji
)->klass
->image
->assembly
)
3446 } else if (mod
->kind
== MOD_KIND_SOURCE_FILE_ONLY
&& ei
&& ei
->klass
) {
3447 gpointer iter
= NULL
;
3449 MonoDebugSourceInfo
*sinfo
;
3450 char *source_file
, *s
;
3451 gboolean found
= FALSE
;
3453 GPtrArray
*source_file_list
;
3455 while ((method
= mono_class_get_methods (ei
->klass
, &iter
))) {
3456 MonoDebugMethodInfo
*minfo
= mono_debug_lookup_method (method
);
3459 mono_debug_get_seq_points (minfo
, &source_file
, &source_file_list
, NULL
, NULL
, NULL
);
3460 for (i
= 0; i
< source_file_list
->len
; ++i
) {
3461 sinfo
= (MonoDebugSourceInfo
*)g_ptr_array_index (source_file_list
, i
);
3463 * Do a case-insesitive match by converting the file name to
3466 s
= strdup_tolower (sinfo
->source_file
);
3467 if (g_hash_table_lookup (mod
->data
.source_files
, s
))
3470 char *s2
= dbg_path_get_basename (sinfo
->source_file
);
3471 char *s3
= strdup_tolower (s2
);
3473 if (g_hash_table_lookup (mod
->data
.source_files
, s3
))
3480 g_ptr_array_free (source_file_list
, TRUE
);
3485 } else if (mod
->kind
== MOD_KIND_TYPE_NAME_ONLY
&& ei
&& ei
->klass
) {
3488 s
= mono_type_full_name (&ei
->klass
->byval_arg
);
3489 if (!g_hash_table_lookup (mod
->data
.type_names
, s
))
3492 } else if (mod
->kind
== MOD_KIND_STEP
) {
3493 if ((mod
->data
.filter
& STEP_FILTER_STATIC_CTOR
) && ji
&&
3494 (jinfo_get_method (ji
)->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) &&
3495 !strcmp (jinfo_get_method (ji
)->name
, ".cctor") &&
3496 (jinfo_get_method (ji
) != ((SingleStepReq
*)req
->info
)->start_method
))
3498 if ((mod
->data
.filter
& STEP_FILTER_DEBUGGER_HIDDEN
) && ji
) {
3499 init_jit_info_dbg_attrs (ji
);
3503 if ((mod
->data
.filter
& STEP_FILTER_DEBUGGER_STEP_THROUGH
) && ji
) {
3504 init_jit_info_dbg_attrs (ji
);
3505 if (ji
->dbg_step_through
)
3508 if ((mod
->data
.filter
& STEP_FILTER_DEBUGGER_NON_USER_CODE
) && ji
) {
3509 init_jit_info_dbg_attrs (ji
);
3510 if (ji
->dbg_non_user_code
)
3517 *suspend_policy
= MAX (*suspend_policy
, req
->suspend_policy
);
3518 events
= g_slist_append (events
, GINT_TO_POINTER (req
->id
));
3523 /* Send a VM START/DEATH event by default */
3524 if (event
== EVENT_KIND_VM_START
)
3525 events
= g_slist_append (events
, GINT_TO_POINTER (0));
3526 if (event
== EVENT_KIND_VM_DEATH
)
3527 events
= g_slist_append (events
, GINT_TO_POINTER (0));
3532 static G_GNUC_UNUSED
const char*
3533 event_to_string (EventKind event
)
3536 case EVENT_KIND_VM_START
: return "VM_START";
3537 case EVENT_KIND_VM_DEATH
: return "VM_DEATH";
3538 case EVENT_KIND_THREAD_START
: return "THREAD_START";
3539 case EVENT_KIND_THREAD_DEATH
: return "THREAD_DEATH";
3540 case EVENT_KIND_APPDOMAIN_CREATE
: return "APPDOMAIN_CREATE";
3541 case EVENT_KIND_APPDOMAIN_UNLOAD
: return "APPDOMAIN_UNLOAD";
3542 case EVENT_KIND_METHOD_ENTRY
: return "METHOD_ENTRY";
3543 case EVENT_KIND_METHOD_EXIT
: return "METHOD_EXIT";
3544 case EVENT_KIND_ASSEMBLY_LOAD
: return "ASSEMBLY_LOAD";
3545 case EVENT_KIND_ASSEMBLY_UNLOAD
: return "ASSEMBLY_UNLOAD";
3546 case EVENT_KIND_BREAKPOINT
: return "BREAKPOINT";
3547 case EVENT_KIND_STEP
: return "STEP";
3548 case EVENT_KIND_TYPE_LOAD
: return "TYPE_LOAD";
3549 case EVENT_KIND_EXCEPTION
: return "EXCEPTION";
3550 case EVENT_KIND_KEEPALIVE
: return "KEEPALIVE";
3551 case EVENT_KIND_USER_BREAK
: return "USER_BREAK";
3552 case EVENT_KIND_USER_LOG
: return "USER_LOG";
3554 g_assert_not_reached ();
3562 * Send an event to the client, suspending the vm if needed.
3563 * LOCKING: Since this can suspend the calling thread, no locks should be held
3565 * The EVENTS list is freed by this function.
3568 process_event (EventKind event
, gpointer arg
, gint32 il_offset
, MonoContext
*ctx
, GSList
*events
, int suspend_policy
)
3572 MonoDomain
*domain
= mono_domain_get ();
3573 MonoThread
*thread
= NULL
;
3574 MonoObject
*keepalive_obj
= NULL
;
3575 gboolean send_success
= FALSE
;
3580 DEBUG_PRINTF (2, "Debugger agent not initialized yet: dropping %s\n", event_to_string (event
));
3584 if (!vm_start_event_sent
&& event
!= EVENT_KIND_VM_START
) {
3585 // FIXME: We miss those events
3586 DEBUG_PRINTF (2, "VM start event not sent yet: dropping %s\n", event_to_string (event
));
3590 if (vm_death_event_sent
) {
3591 DEBUG_PRINTF (2, "VM death event has been sent: dropping %s\n", event_to_string (event
));
3595 if (mono_runtime_is_shutting_down () && event
!= EVENT_KIND_VM_DEATH
) {
3596 DEBUG_PRINTF (2, "Mono runtime is shutting down: dropping %s\n", event_to_string (event
));
3601 DEBUG_PRINTF (2, "Debugger client is not connected: dropping %s\n", event_to_string (event
));
3605 if (event
== EVENT_KIND_KEEPALIVE
)
3606 suspend_policy
= SUSPEND_POLICY_NONE
;
3611 if (agent_config
.defer
) {
3612 /* Make sure the thread id is always set when doing deferred debugging */
3613 if (is_debugger_thread ()) {
3614 /* Don't suspend on events from the debugger thread */
3615 suspend_policy
= SUSPEND_POLICY_NONE
;
3616 thread
= mono_thread_get_main ();
3618 else thread
= mono_thread_current ();
3620 if (is_debugger_thread () && event
!= EVENT_KIND_VM_DEATH
)
3621 // FIXME: Send these with a NULL thread, don't suspend the current thread
3626 nevents
= g_slist_length (events
);
3627 buffer_init (&buf
, 128);
3628 buffer_add_byte (&buf
, suspend_policy
);
3629 buffer_add_int (&buf
, nevents
);
3631 for (l
= events
; l
; l
= l
->next
) {
3632 buffer_add_byte (&buf
, event
); // event kind
3633 buffer_add_int (&buf
, GPOINTER_TO_INT (l
->data
)); // request id
3637 if (event
== EVENT_KIND_VM_DEATH
) {
3641 thread
= mono_thread_current ();
3643 if (event
== EVENT_KIND_VM_START
&& arg
!= NULL
)
3644 thread
= (MonoThread
*)arg
;
3647 buffer_add_objid (&buf
, (MonoObject
*)thread
); // thread
3650 case EVENT_KIND_THREAD_START
:
3651 case EVENT_KIND_THREAD_DEATH
:
3653 case EVENT_KIND_APPDOMAIN_CREATE
:
3654 case EVENT_KIND_APPDOMAIN_UNLOAD
:
3655 buffer_add_domainid (&buf
, (MonoDomain
*)arg
);
3657 case EVENT_KIND_METHOD_ENTRY
:
3658 case EVENT_KIND_METHOD_EXIT
:
3659 buffer_add_methodid (&buf
, domain
, (MonoMethod
*)arg
);
3661 case EVENT_KIND_ASSEMBLY_LOAD
:
3662 buffer_add_assemblyid (&buf
, domain
, (MonoAssembly
*)arg
);
3664 case EVENT_KIND_ASSEMBLY_UNLOAD
: {
3665 DebuggerTlsData
*tls
;
3667 /* The domain the assembly belonged to is not equal to the current domain */
3668 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
3670 g_assert (tls
->domain_unloading
);
3672 buffer_add_assemblyid (&buf
, tls
->domain_unloading
, (MonoAssembly
*)arg
);
3675 case EVENT_KIND_TYPE_LOAD
:
3676 buffer_add_typeid (&buf
, domain
, (MonoClass
*)arg
);
3678 case EVENT_KIND_BREAKPOINT
:
3679 case EVENT_KIND_STEP
:
3680 buffer_add_methodid (&buf
, domain
, (MonoMethod
*)arg
);
3681 buffer_add_long (&buf
, il_offset
);
3683 case EVENT_KIND_VM_START
:
3684 buffer_add_domainid (&buf
, mono_get_root_domain ());
3686 case EVENT_KIND_VM_DEATH
:
3687 if (CHECK_PROTOCOL_VERSION (2, 27))
3688 buffer_add_int (&buf
, mono_environment_exitcode_get ());
3690 case EVENT_KIND_EXCEPTION
: {
3691 EventInfo
*ei
= (EventInfo
*)arg
;
3692 buffer_add_objid (&buf
, ei
->exc
);
3694 * We are not yet suspending, so get_objref () will not keep this object alive. So we need to do it
3695 * later after the suspension. (#12494).
3697 keepalive_obj
= ei
->exc
;
3700 case EVENT_KIND_USER_BREAK
:
3702 case EVENT_KIND_USER_LOG
: {
3703 EventInfo
*ei
= (EventInfo
*)arg
;
3704 buffer_add_int (&buf
, ei
->level
);
3705 buffer_add_string (&buf
, ei
->category
? ei
->category
: "");
3706 buffer_add_string (&buf
, ei
->message
? ei
->message
: "");
3709 case EVENT_KIND_KEEPALIVE
:
3710 suspend_policy
= SUSPEND_POLICY_NONE
;
3713 g_assert_not_reached ();
3717 if (event
== EVENT_KIND_VM_START
) {
3718 suspend_policy
= agent_config
.suspend
? SUSPEND_POLICY_ALL
: SUSPEND_POLICY_NONE
;
3719 if (!agent_config
.defer
)
3720 start_debugger_thread ();
3723 if (event
== EVENT_KIND_VM_DEATH
) {
3724 vm_death_event_sent
= TRUE
;
3725 suspend_policy
= SUSPEND_POLICY_NONE
;
3728 if (mono_runtime_is_shutting_down ())
3729 suspend_policy
= SUSPEND_POLICY_NONE
;
3731 if (suspend_policy
!= SUSPEND_POLICY_NONE
) {
3733 * Save the thread context and start suspending before sending the packet,
3734 * since we could be receiving the resume request before send_packet ()
3737 save_thread_context (ctx
);
3741 /* This will keep this object alive */
3742 get_objref (keepalive_obj
);
3745 send_success
= send_packet (CMD_SET_EVENT
, CMD_COMPOSITE
, &buf
);
3749 g_slist_free (events
);
3752 if (!send_success
) {
3753 DEBUG_PRINTF (2, "Sending command %s failed.\n", event_to_string (event
));
3757 if (event
== EVENT_KIND_VM_START
) {
3758 vm_start_event_sent
= TRUE
;
3761 DEBUG_PRINTF (1, "[%p] Sent %d events %s(%d), suspend=%d.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), nevents
, event_to_string (event
), ecount
, suspend_policy
);
3763 switch (suspend_policy
) {
3764 case SUSPEND_POLICY_NONE
:
3766 case SUSPEND_POLICY_ALL
:
3769 case SUSPEND_POLICY_EVENT_THREAD
:
3773 g_assert_not_reached ();
3778 process_profiler_event (EventKind event
, gpointer arg
)
3782 EventInfo ei
, *ei_arg
= NULL
;
3784 if (event
== EVENT_KIND_TYPE_LOAD
) {
3785 ei
.klass
= (MonoClass
*)arg
;
3789 mono_loader_lock ();
3790 events
= create_event_list (event
, NULL
, NULL
, ei_arg
, &suspend_policy
);
3791 mono_loader_unlock ();
3793 process_event (event
, arg
, 0, NULL
, events
, suspend_policy
);
3797 runtime_initialized (MonoProfiler
*prof
)
3799 process_profiler_event (EVENT_KIND_VM_START
, mono_thread_current ());
3800 if (agent_config
.defer
)
3801 start_debugger_thread ();
3805 runtime_shutdown (MonoProfiler
*prof
)
3807 process_profiler_event (EVENT_KIND_VM_DEATH
, NULL
);
3809 mono_debugger_agent_cleanup ();
3813 thread_startup (MonoProfiler
*prof
, uintptr_t tid
)
3815 MonoInternalThread
*thread
= mono_thread_internal_current ();
3816 MonoInternalThread
*old_thread
;
3817 DebuggerTlsData
*tls
;
3819 if (mono_native_thread_id_equals (MONO_UINT_TO_NATIVE_THREAD_ID (tid
), debugger_thread_id
))
3822 g_assert (mono_native_thread_id_equals (MONO_UINT_TO_NATIVE_THREAD_ID (tid
), MONO_UINT_TO_NATIVE_THREAD_ID (thread
->tid
)));
3824 mono_loader_lock ();
3825 old_thread
= (MonoInternalThread
*)mono_g_hash_table_lookup (tid_to_thread
, GUINT_TO_POINTER (tid
));
3826 mono_loader_unlock ();
3828 if (thread
== old_thread
) {
3830 * For some reason, thread_startup () might be called for the same thread
3831 * multiple times (attach ?).
3833 DEBUG_PRINTF (1, "[%p] thread_start () called multiple times for %p, ignored.\n", GUINT_TO_POINTER (tid
), GUINT_TO_POINTER (tid
));
3837 * thread_end () might not be called for some threads, and the tid could
3840 DEBUG_PRINTF (1, "[%p] Removing stale data for tid %p.\n", GUINT_TO_POINTER (tid
), GUINT_TO_POINTER (tid
));
3841 mono_loader_lock ();
3842 mono_g_hash_table_remove (thread_to_tls
, old_thread
);
3843 mono_g_hash_table_remove (tid_to_thread
, GUINT_TO_POINTER (tid
));
3844 mono_g_hash_table_remove (tid_to_thread_obj
, GUINT_TO_POINTER (tid
));
3845 mono_loader_unlock ();
3849 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
3851 // FIXME: Free this somewhere
3852 tls
= g_new0 (DebuggerTlsData
, 1);
3853 MONO_GC_REGISTER_ROOT_SINGLE (tls
->thread
, MONO_ROOT_SOURCE_DEBUGGER
, "debugger thread reference");
3854 tls
->thread
= thread
;
3855 mono_native_tls_set_value (debugger_tls_id
, tls
);
3857 DEBUG_PRINTF (1, "[%p] Thread started, obj=%p, tls=%p.\n", (gpointer
)tid
, thread
, tls
);
3859 mono_loader_lock ();
3860 mono_g_hash_table_insert (thread_to_tls
, thread
, tls
);
3861 mono_g_hash_table_insert (tid_to_thread
, (gpointer
)tid
, thread
);
3862 mono_g_hash_table_insert (tid_to_thread_obj
, GUINT_TO_POINTER (tid
), mono_thread_current ());
3863 mono_loader_unlock ();
3865 process_profiler_event (EVENT_KIND_THREAD_START
, thread
);
3868 * suspend_vm () could have missed this thread, so wait for a resume.
3874 thread_end (MonoProfiler
*prof
, uintptr_t tid
)
3876 MonoInternalThread
*thread
;
3877 DebuggerTlsData
*tls
= NULL
;
3879 mono_loader_lock ();
3880 thread
= (MonoInternalThread
*)mono_g_hash_table_lookup (tid_to_thread
, GUINT_TO_POINTER (tid
));
3882 mono_g_hash_table_remove (tid_to_thread_obj
, GUINT_TO_POINTER (tid
));
3883 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
3885 /* FIXME: Maybe we need to free this instead, but some code can't handle that */
3886 tls
->terminated
= TRUE
;
3887 /* Can't remove from tid_to_thread, as that would defeat the check in thread_start () */
3888 MONO_GC_UNREGISTER_ROOT (tls
->thread
);
3892 mono_loader_unlock ();
3894 /* We might be called for threads started before we registered the start callback */
3896 DEBUG_PRINTF (1, "[%p] Thread terminated, obj=%p, tls=%p.\n", (gpointer
)tid
, thread
, tls
);
3898 if (mono_native_thread_id_equals (mono_native_thread_id_get (), MONO_UINT_TO_NATIVE_THREAD_ID (tid
))
3899 && !mono_native_tls_get_value (debugger_tls_id
)
3902 * This can happen on darwin since we deregister threads using pthread dtors.
3903 * process_profiler_event () and the code it calls cannot handle a null TLS value.
3908 process_profiler_event (EVENT_KIND_THREAD_DEATH
, thread
);
3913 appdomain_load (MonoProfiler
*prof
, MonoDomain
*domain
, int result
)
3915 mono_loader_lock ();
3916 g_hash_table_insert (domains
, domain
, domain
);
3917 mono_loader_unlock ();
3919 process_profiler_event (EVENT_KIND_APPDOMAIN_CREATE
, domain
);
3923 appdomain_start_unload (MonoProfiler
*prof
, MonoDomain
*domain
)
3925 DebuggerTlsData
*tls
;
3927 /* This might be called during shutdown on the debugger thread from the CMD_VM_EXIT code */
3928 if (is_debugger_thread ())
3932 * Remember the currently unloading appdomain as it is needed to generate
3933 * proper ids for unloading assemblies.
3935 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
3937 tls
->domain_unloading
= domain
;
3941 appdomain_unload (MonoProfiler
*prof
, MonoDomain
*domain
)
3943 DebuggerTlsData
*tls
;
3945 if (is_debugger_thread ())
3948 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
3950 tls
->domain_unloading
= NULL
;
3952 clear_breakpoints_for_domain (domain
);
3954 mono_loader_lock ();
3955 /* Invalidate each thread's frame stack */
3956 mono_g_hash_table_foreach (thread_to_tls
, invalidate_each_thread
, NULL
);
3957 mono_loader_unlock ();
3959 process_profiler_event (EVENT_KIND_APPDOMAIN_UNLOAD
, domain
);
3963 * invalidate_each_thread:
3965 * A GHFunc to invalidate frames.
3966 * value must be a DebuggerTlsData*
3969 invalidate_each_thread (gpointer key
, gpointer value
, gpointer user_data
)
3971 invalidate_frames ((DebuggerTlsData
*)value
);
3975 assembly_load (MonoProfiler
*prof
, MonoAssembly
*assembly
, int result
)
3977 /* Sent later in jit_end () */
3979 g_ptr_array_add (pending_assembly_loads
, assembly
);
3984 assembly_unload (MonoProfiler
*prof
, MonoAssembly
*assembly
)
3986 if (is_debugger_thread ())
3989 process_profiler_event (EVENT_KIND_ASSEMBLY_UNLOAD
, assembly
);
3991 clear_event_requests_for_assembly (assembly
);
3992 clear_types_for_assembly (assembly
);
3996 send_type_load (MonoClass
*klass
)
3998 gboolean type_load
= FALSE
;
3999 MonoDomain
*domain
= mono_domain_get ();
4000 AgentDomainInfo
*info
= NULL
;
4002 info
= get_agent_domain_info (domain
);
4004 mono_loader_lock ();
4006 if (!g_hash_table_lookup (info
->loaded_classes
, klass
)) {
4008 g_hash_table_insert (info
->loaded_classes
, klass
, klass
);
4011 mono_loader_unlock ();
4014 emit_type_load (klass
, klass
, NULL
);
4018 * Emit load events for all types currently loaded in the domain.
4019 * Takes the loader and domain locks.
4020 * user_data is unused.
4023 send_types_for_domain (MonoDomain
*domain
, void *user_data
)
4025 AgentDomainInfo
*info
= NULL
;
4027 info
= get_agent_domain_info (domain
);
4030 mono_loader_lock ();
4031 g_hash_table_foreach (info
->loaded_classes
, emit_type_load
, NULL
);
4032 mono_loader_unlock ();
4036 jit_end (MonoProfiler
*prof
, MonoMethod
*method
, MonoJitInfo
*jinfo
, int result
)
4039 * We emit type load events when the first method of the type is JITted,
4040 * since the class load profiler callbacks might be called with the
4041 * loader lock held. They could also occur in the debugger thread.
4042 * Same for assembly load events.
4045 MonoAssembly
*assembly
= NULL
;
4047 // FIXME: Maybe store this in TLS so the thread of the event is correct ?
4049 if (pending_assembly_loads
->len
> 0) {
4050 assembly
= (MonoAssembly
*)g_ptr_array_index (pending_assembly_loads
, 0);
4051 g_ptr_array_remove_index (pending_assembly_loads
, 0);
4056 process_profiler_event (EVENT_KIND_ASSEMBLY_LOAD
, assembly
);
4062 send_type_load (method
->klass
);
4065 add_pending_breakpoints (method
, jinfo
);
4069 * BREAKPOINTS/SINGLE STEPPING
4073 * Contains information about an inserted breakpoint.
4076 long il_offset
, native_offset
;
4080 } BreakpointInstance
;
4083 * Contains generic information about a breakpoint.
4087 * The method where the breakpoint is placed. Can be NULL in which case it
4088 * is inserted into every method. This is used to implement method entry/
4089 * exit events. Can be a generic method definition, in which case the
4090 * breakpoint is inserted into every instance.
4096 * A list of BreakpointInstance structures describing where the breakpoint
4097 * was inserted. There could be more than one because of
4098 * generics/appdomains/method entry/exit.
4100 GPtrArray
*children
;
4103 /* List of breakpoints */
4104 /* Protected by the loader lock */
4105 static GPtrArray
*breakpoints
;
4106 /* Maps breakpoint locations to the number of breakpoints at that location */
4107 static GHashTable
*bp_locs
;
4110 breakpoints_init (void)
4112 breakpoints
= g_ptr_array_new ();
4113 bp_locs
= g_hash_table_new (NULL
, NULL
);
4117 * insert_breakpoint:
4119 * Insert the breakpoint described by BP into the method described by
4123 insert_breakpoint (MonoSeqPointInfo
*seq_points
, MonoDomain
*domain
, MonoJitInfo
*ji
, MonoBreakpoint
*bp
, MonoError
*error
)
4126 BreakpointInstance
*inst
;
4127 SeqPointIterator it
;
4128 gboolean it_has_sp
= FALSE
;
4131 mono_error_init (error
);
4133 mono_seq_point_iterator_init (&it
, seq_points
);
4134 while (mono_seq_point_iterator_next (&it
)) {
4135 if (it
.seq_point
.il_offset
== bp
->il_offset
) {
4143 * The set of IL offsets with seq points doesn't completely match the
4144 * info returned by CMD_METHOD_GET_DEBUG_INFO (#407).
4146 mono_seq_point_iterator_init (&it
, seq_points
);
4147 while (mono_seq_point_iterator_next (&it
)) {
4148 if (it
.seq_point
.il_offset
!= METHOD_ENTRY_IL_OFFSET
&&
4149 it
.seq_point
.il_offset
!= METHOD_EXIT_IL_OFFSET
&&
4150 it
.seq_point
.il_offset
+ 1 == bp
->il_offset
) {
4158 char *s
= g_strdup_printf ("Unable to insert breakpoint at %s:%d", mono_method_full_name (jinfo_get_method (ji
), TRUE
), bp
->il_offset
);
4160 mono_seq_point_iterator_init (&it
, seq_points
);
4161 while (mono_seq_point_iterator_next (&it
))
4162 DEBUG_PRINTF (1, "%d\n", it
.seq_point
.il_offset
);
4165 mono_error_set_error (error
, MONO_ERROR_GENERIC
, "%s", s
);
4166 g_warning ("%s", s
);
4170 g_warning ("%s", s
);
4176 inst
= g_new0 (BreakpointInstance
, 1);
4177 inst
->il_offset
= it
.seq_point
.il_offset
;
4178 inst
->native_offset
= it
.seq_point
.native_offset
;
4179 inst
->ip
= (guint8
*)ji
->code_start
+ it
.seq_point
.native_offset
;
4181 inst
->domain
= domain
;
4183 mono_loader_lock ();
4185 g_ptr_array_add (bp
->children
, inst
);
4187 mono_loader_unlock ();
4190 count
= GPOINTER_TO_INT (g_hash_table_lookup (bp_locs
, inst
->ip
));
4191 g_hash_table_insert (bp_locs
, inst
->ip
, GINT_TO_POINTER (count
+ 1));
4194 if (it
.seq_point
.native_offset
== SEQ_POINT_NATIVE_OFFSET_DEAD_CODE
) {
4195 DEBUG_PRINTF (1, "[dbg] Attempting to insert seq point at dead IL offset %d, ignoring.\n", (int)bp
->il_offset
);
4196 } else if (count
== 0) {
4197 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
4198 mono_arch_set_breakpoint (ji
, inst
->ip
);
4204 DEBUG_PRINTF (1, "[dbg] Inserted breakpoint at %s:[il=0x%x,native=0x%x] [%p](%d).\n", mono_method_full_name (jinfo_get_method (ji
), TRUE
), (int)it
.seq_point
.il_offset
, (int)it
.seq_point
.native_offset
, inst
->ip
, count
);
4208 remove_breakpoint (BreakpointInstance
*inst
)
4210 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
4212 MonoJitInfo
*ji
= inst
->ji
;
4213 guint8
*ip
= inst
->ip
;
4216 count
= GPOINTER_TO_INT (g_hash_table_lookup (bp_locs
, ip
));
4217 g_hash_table_insert (bp_locs
, ip
, GINT_TO_POINTER (count
- 1));
4220 g_assert (count
> 0);
4222 if (count
== 1 && inst
->native_offset
!= SEQ_POINT_NATIVE_OFFSET_DEAD_CODE
) {
4223 mono_arch_clear_breakpoint (ji
, ip
);
4224 DEBUG_PRINTF (1, "[dbg] Clear breakpoint at %s [%p].\n", mono_method_full_name (jinfo_get_method (ji
), TRUE
), ip
);
4232 * This doesn't take any locks.
4234 static inline gboolean
4235 bp_matches_method (MonoBreakpoint
*bp
, MonoMethod
*method
)
4241 if (method
== bp
->method
)
4243 if (method
->is_inflated
&& ((MonoMethodInflated
*)method
)->declaring
== bp
->method
)
4246 if (bp
->method
->is_inflated
&& method
->is_inflated
) {
4247 MonoMethodInflated
*bpimethod
= (MonoMethodInflated
*)bp
->method
;
4248 MonoMethodInflated
*imethod
= (MonoMethodInflated
*)method
;
4250 /* Open generic methods should match closed generic methods of the same class */
4251 if (bpimethod
->declaring
== imethod
->declaring
&& bpimethod
->context
.class_inst
== imethod
->context
.class_inst
&& bpimethod
->context
.method_inst
&& bpimethod
->context
.method_inst
->is_open
) {
4252 for (i
= 0; i
< bpimethod
->context
.method_inst
->type_argc
; ++i
) {
4253 MonoType
*t1
= bpimethod
->context
.method_inst
->type_argv
[i
];
4255 /* FIXME: Handle !mvar */
4256 if (t1
->type
!= MONO_TYPE_MVAR
)
4267 * add_pending_breakpoints:
4269 * Insert pending breakpoints into the newly JITted method METHOD.
4272 add_pending_breakpoints (MonoMethod
*method
, MonoJitInfo
*ji
)
4275 MonoSeqPointInfo
*seq_points
;
4277 MonoMethod
*jmethod
;
4282 domain
= mono_domain_get ();
4284 mono_loader_lock ();
4286 for (i
= 0; i
< breakpoints
->len
; ++i
) {
4287 MonoBreakpoint
*bp
= (MonoBreakpoint
*)g_ptr_array_index (breakpoints
, i
);
4288 gboolean found
= FALSE
;
4290 if (!bp_matches_method (bp
, method
))
4293 for (j
= 0; j
< bp
->children
->len
; ++j
) {
4294 BreakpointInstance
*inst
= (BreakpointInstance
*)g_ptr_array_index (bp
->children
, j
);
4301 MonoMethod
*declaring
= NULL
;
4303 jmethod
= jinfo_get_method (ji
);
4304 if (jmethod
->is_inflated
)
4305 declaring
= mono_method_get_declaring_generic_method (jmethod
);
4307 mono_domain_lock (domain
);
4308 seq_points
= (MonoSeqPointInfo
*)g_hash_table_lookup (domain_jit_info (domain
)->seq_points
, jmethod
);
4309 if (!seq_points
&& declaring
)
4310 seq_points
= (MonoSeqPointInfo
*)g_hash_table_lookup (domain_jit_info (domain
)->seq_points
, declaring
);
4311 mono_domain_unlock (domain
);
4313 /* Could be AOT code */
4315 g_assert (seq_points
);
4317 insert_breakpoint (seq_points
, domain
, ji
, bp
, NULL
);
4321 mono_loader_unlock ();
4325 set_bp_in_method (MonoDomain
*domain
, MonoMethod
*method
, MonoSeqPointInfo
*seq_points
, MonoBreakpoint
*bp
, MonoError
*error
)
4331 mono_error_init (error
);
4333 code
= mono_jit_find_compiled_method_with_jit_info (domain
, method
, &ji
);
4337 /* Might be AOTed code */
4338 code
= mono_aot_get_method_checked (domain
, method
, &oerror
);
4340 mono_error_assert_ok (&oerror
);
4341 ji
= mono_jit_info_table_find (domain
, (char *)code
);
4346 insert_breakpoint (seq_points
, domain
, ji
, bp
, error
);
4350 clear_breakpoint (MonoBreakpoint
*bp
);
4355 * Set a breakpoint at IL_OFFSET in METHOD.
4356 * METHOD can be NULL, in which case a breakpoint is placed in all methods.
4357 * METHOD can also be a generic method definition, in which case a breakpoint
4358 * is placed in all instances of the method.
4359 * If ERROR is non-NULL, then it is set and NULL is returnd if some breakpoints couldn't be
4362 static MonoBreakpoint
*
4363 set_breakpoint (MonoMethod
*method
, long il_offset
, EventRequest
*req
, MonoError
*error
)
4366 GHashTableIter iter
, iter2
;
4369 MonoSeqPointInfo
*seq_points
;
4371 GPtrArray
*method_domains
;
4372 GPtrArray
*method_seq_points
;
4376 mono_error_init (error
);
4379 // - suspend/resume the vm to prevent code patching problems
4380 // - multiple breakpoints on the same location
4381 // - dynamic methods
4384 bp
= g_new0 (MonoBreakpoint
, 1);
4385 bp
->method
= method
;
4386 bp
->il_offset
= il_offset
;
4388 bp
->children
= g_ptr_array_new ();
4390 DEBUG_PRINTF (1, "[dbg] Setting %sbreakpoint at %s:0x%x.\n", (req
->event_kind
== EVENT_KIND_STEP
) ? "single step " : "", method
? mono_method_full_name (method
, TRUE
) : "<all>", (int)il_offset
);
4392 methods
= g_ptr_array_new ();
4393 method_domains
= g_ptr_array_new ();
4394 method_seq_points
= g_ptr_array_new ();
4396 mono_loader_lock ();
4397 g_hash_table_iter_init (&iter
, domains
);
4398 while (g_hash_table_iter_next (&iter
, (void**)&domain
, NULL
)) {
4399 mono_domain_lock (domain
);
4400 g_hash_table_iter_init (&iter2
, domain_jit_info (domain
)->seq_points
);
4401 while (g_hash_table_iter_next (&iter2
, (void**)&m
, (void**)&seq_points
)) {
4402 if (bp_matches_method (bp
, m
)) {
4403 /* Save the info locally to simplify the code inside the domain lock */
4404 g_ptr_array_add (methods
, m
);
4405 g_ptr_array_add (method_domains
, domain
);
4406 g_ptr_array_add (method_seq_points
, seq_points
);
4409 mono_domain_unlock (domain
);
4412 for (i
= 0; i
< methods
->len
; ++i
) {
4413 m
= (MonoMethod
*)g_ptr_array_index (methods
, i
);
4414 domain
= (MonoDomain
*)g_ptr_array_index (method_domains
, i
);
4415 seq_points
= (MonoSeqPointInfo
*)g_ptr_array_index (method_seq_points
, i
);
4416 set_bp_in_method (domain
, m
, seq_points
, bp
, error
);
4419 g_ptr_array_add (breakpoints
, bp
);
4420 mono_loader_unlock ();
4422 g_ptr_array_free (methods
, TRUE
);
4423 g_ptr_array_free (method_domains
, TRUE
);
4424 g_ptr_array_free (method_seq_points
, TRUE
);
4426 if (error
&& !mono_error_ok (error
)) {
4427 clear_breakpoint (bp
);
4435 clear_breakpoint (MonoBreakpoint
*bp
)
4439 // FIXME: locking, races
4440 for (i
= 0; i
< bp
->children
->len
; ++i
) {
4441 BreakpointInstance
*inst
= (BreakpointInstance
*)g_ptr_array_index (bp
->children
, i
);
4443 remove_breakpoint (inst
);
4448 mono_loader_lock ();
4449 g_ptr_array_remove (breakpoints
, bp
);
4450 mono_loader_unlock ();
4452 g_ptr_array_free (bp
->children
, TRUE
);
4457 breakpoints_cleanup (void)
4461 mono_loader_lock ();
4463 while (i
< event_requests
->len
) {
4464 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
4466 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
) {
4467 clear_breakpoint ((MonoBreakpoint
*)req
->info
);
4468 g_ptr_array_remove_index_fast (event_requests
, i
);
4475 for (i
= 0; i
< breakpoints
->len
; ++i
)
4476 g_free (g_ptr_array_index (breakpoints
, i
));
4478 g_ptr_array_free (breakpoints
, TRUE
);
4479 g_hash_table_destroy (bp_locs
);
4484 mono_loader_unlock ();
4488 * clear_breakpoints_for_domain:
4490 * Clear breakpoint instances which reference DOMAIN.
4493 clear_breakpoints_for_domain (MonoDomain
*domain
)
4497 /* This could be called after shutdown */
4501 mono_loader_lock ();
4502 for (i
= 0; i
< breakpoints
->len
; ++i
) {
4503 MonoBreakpoint
*bp
= (MonoBreakpoint
*)g_ptr_array_index (breakpoints
, i
);
4506 while (j
< bp
->children
->len
) {
4507 BreakpointInstance
*inst
= (BreakpointInstance
*)g_ptr_array_index (bp
->children
, j
);
4509 if (inst
->domain
== domain
) {
4510 remove_breakpoint (inst
);
4514 g_ptr_array_remove_index_fast (bp
->children
, j
);
4520 mono_loader_unlock ();
4524 * ss_calculate_framecount:
4526 * Ensure DebuggerTlsData fields are filled out.
4528 static void ss_calculate_framecount (DebuggerTlsData
*tls
, MonoContext
*ctx
)
4530 if (!tls
->context
.valid
)
4531 mono_thread_state_init_from_monoctx (&tls
->context
, ctx
);
4532 compute_frame_info (tls
->thread
, tls
);
4538 * Return FALSE if single stepping needs to continue.
4541 ss_update (SingleStepReq
*req
, MonoJitInfo
*ji
, SeqPoint
*sp
, DebuggerTlsData
*tls
, MonoContext
*ctx
)
4543 MonoDebugMethodInfo
*minfo
;
4544 MonoDebugSourceLocation
*loc
= NULL
;
4545 gboolean hit
= TRUE
;
4548 if (req
->depth
== STEP_DEPTH_OVER
&& (sp
->flags
& MONO_SEQ_POINT_FLAG_NONEMPTY_STACK
)) {
4550 * These seq points are inserted by the JIT after calls, step over needs to skip them.
4552 DEBUG_PRINTF (1, "[%p] Seq point at nonempty stack %x while stepping over, continuing single stepping.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), sp
->il_offset
);
4556 if ((req
->depth
== STEP_DEPTH_OVER
|| req
->depth
== STEP_DEPTH_OUT
) && hit
) {
4557 gboolean is_step_out
= req
->depth
== STEP_DEPTH_OUT
;
4559 ss_calculate_framecount (tls
, ctx
);
4561 // Because functions can call themselves recursively, we need to make sure we're stopping at the right stack depth.
4562 // In case of step out, the target is the frame *enclosing* the one where the request was made.
4563 int target_frames
= req
->nframes
+ (is_step_out
? -1 : 0);
4564 if (req
->nframes
> 0 && tls
->frame_count
> 0 && tls
->frame_count
> target_frames
) {
4565 /* Hit the breakpoint in a recursive call, don't halt */
4566 DEBUG_PRINTF (1, "[%p] Breakpoint at lower frame while stepping %s, continuing single stepping.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), is_step_out
? "out" : "over");
4571 if (req
->depth
== STEP_DEPTH_INTO
&& req
->size
== STEP_SIZE_MIN
&& (sp
->flags
& MONO_SEQ_POINT_FLAG_NONEMPTY_STACK
) && ss_req
->start_method
){
4572 method
= jinfo_get_method (ji
);
4573 ss_calculate_framecount (tls
, ctx
);
4574 if (ss_req
->start_method
== method
&& req
->nframes
&& tls
->frame_count
== req
->nframes
) {//Check also frame count(could be recursion)
4575 DEBUG_PRINTF (1, "[%p] Seq point at nonempty stack %x while stepping in, continuing single stepping.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), sp
->il_offset
);
4580 if (req
->size
!= STEP_SIZE_LINE
)
4583 /* Have to check whenever a different source line was reached */
4584 method
= jinfo_get_method (ji
);
4585 minfo
= mono_debug_lookup_method (method
);
4588 loc
= mono_debug_method_lookup_location (minfo
, sp
->il_offset
);
4591 DEBUG_PRINTF (1, "[%p] No line number info for il offset %x, continuing single stepping.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), sp
->il_offset
);
4592 ss_req
->last_method
= method
;
4594 } else if (loc
&& method
== ss_req
->last_method
&& loc
->row
== ss_req
->last_line
) {
4595 ss_calculate_framecount (tls
, ctx
);
4596 if (tls
->frame_count
== req
->nframes
) { // If the frame has changed we're clearly not on the same source line.
4597 DEBUG_PRINTF (1, "[%p] Same source line (%d), continuing single stepping.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), loc
->row
);
4603 ss_req
->last_method
= method
;
4604 ss_req
->last_line
= loc
->row
;
4605 mono_debug_free_source_location (loc
);
4612 breakpoint_matches_assembly (MonoBreakpoint
*bp
, MonoAssembly
*assembly
)
4614 return bp
->method
&& bp
->method
->klass
->image
->assembly
== assembly
;
4618 process_breakpoint_inner (DebuggerTlsData
*tls
, gboolean from_signal
)
4622 int i
, j
, suspend_policy
;
4623 guint32 native_offset
;
4625 BreakpointInstance
*inst
;
4626 GPtrArray
*bp_reqs
, *ss_reqs_orig
, *ss_reqs
;
4627 GSList
*bp_events
= NULL
, *ss_events
= NULL
, *enter_leave_events
= NULL
;
4628 EventKind kind
= EVENT_KIND_BREAKPOINT
;
4629 MonoContext
*ctx
= &tls
->restore_state
.ctx
;
4631 MonoSeqPointInfo
*info
;
4635 // FIXME: Speed this up
4637 ip
= (guint8
*)MONO_CONTEXT_GET_IP (ctx
);
4638 ji
= mini_jit_info_table_find (mono_domain_get (), (char*)ip
, NULL
);
4639 g_assert (ji
&& !ji
->is_trampoline
);
4640 method
= jinfo_get_method (ji
);
4642 /* Compute the native offset of the breakpoint from the ip */
4643 native_offset
= ip
- (guint8
*)ji
->code_start
;
4646 * Skip the instruction causing the breakpoint signal.
4649 mono_arch_skip_breakpoint (ctx
, ji
);
4651 if (method
->wrapper_type
|| tls
->disable_breakpoints
)
4654 bp_reqs
= g_ptr_array_new ();
4655 ss_reqs
= g_ptr_array_new ();
4656 ss_reqs_orig
= g_ptr_array_new ();
4658 mono_loader_lock ();
4661 * The ip points to the instruction causing the breakpoint event, which is after
4662 * the offset recorded in the seq point map, so find the prev seq point before ip.
4664 found_sp
= mono_find_prev_seq_point_for_native_offset (mono_domain_get (), method
, native_offset
, &info
, &sp
);
4667 no_seq_points_found (method
, native_offset
);
4669 g_assert (found_sp
);
4671 DEBUG_PRINTF (1, "[%p] Breakpoint hit, method=%s, ip=%p, [il=0x%x,native=0x%x].\n", (gpointer
) (gsize
) mono_native_thread_id_get (), method
->name
, ip
, sp
.il_offset
, native_offset
);
4674 for (i
= 0; i
< breakpoints
->len
; ++i
) {
4675 bp
= (MonoBreakpoint
*)g_ptr_array_index (breakpoints
, i
);
4680 for (j
= 0; j
< bp
->children
->len
; ++j
) {
4681 inst
= (BreakpointInstance
*)g_ptr_array_index (bp
->children
, j
);
4682 if (inst
->ji
== ji
&& inst
->il_offset
== sp
.il_offset
&& inst
->native_offset
== sp
.native_offset
) {
4683 if (bp
->req
->event_kind
== EVENT_KIND_STEP
) {
4684 g_ptr_array_add (ss_reqs_orig
, bp
->req
);
4686 g_ptr_array_add (bp_reqs
, bp
->req
);
4691 if (bp_reqs
->len
== 0 && ss_reqs_orig
->len
== 0) {
4692 /* Maybe a method entry/exit event */
4693 if (sp
.il_offset
== METHOD_ENTRY_IL_OFFSET
)
4694 kind
= EVENT_KIND_METHOD_ENTRY
;
4695 else if (sp
.il_offset
== METHOD_EXIT_IL_OFFSET
)
4696 kind
= EVENT_KIND_METHOD_EXIT
;
4699 /* Process single step requests */
4700 for (i
= 0; i
< ss_reqs_orig
->len
; ++i
) {
4701 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (ss_reqs_orig
, i
);
4702 SingleStepReq
*ss_req
= (SingleStepReq
*)req
->info
;
4705 if (mono_thread_internal_current () != ss_req
->thread
)
4708 hit
= ss_update (ss_req
, ji
, &sp
, tls
, ctx
);
4710 g_ptr_array_add (ss_reqs
, req
);
4712 /* Start single stepping again from the current sequence point */
4713 ss_start (ss_req
, method
, &sp
, info
, ctx
, tls
, FALSE
, NULL
, 0);
4716 if (ss_reqs
->len
> 0)
4717 ss_events
= create_event_list (EVENT_KIND_STEP
, ss_reqs
, ji
, NULL
, &suspend_policy
);
4718 if (bp_reqs
->len
> 0)
4719 bp_events
= create_event_list (EVENT_KIND_BREAKPOINT
, bp_reqs
, ji
, NULL
, &suspend_policy
);
4720 if (kind
!= EVENT_KIND_BREAKPOINT
)
4721 enter_leave_events
= create_event_list (kind
, NULL
, ji
, NULL
, &suspend_policy
);
4723 mono_loader_unlock ();
4725 g_ptr_array_free (bp_reqs
, TRUE
);
4726 g_ptr_array_free (ss_reqs
, TRUE
);
4729 * FIXME: The first event will suspend, so the second will only be sent after the
4733 process_event (EVENT_KIND_STEP
, method
, 0, ctx
, ss_events
, suspend_policy
);
4735 process_event (kind
, method
, 0, ctx
, bp_events
, suspend_policy
);
4736 if (enter_leave_events
)
4737 process_event (kind
, method
, 0, ctx
, enter_leave_events
, suspend_policy
);
4740 /* Process a breakpoint/single step event after resuming from a signal handler */
4742 process_signal_event (void (*func
) (DebuggerTlsData
*, gboolean
))
4744 DebuggerTlsData
*tls
;
4745 MonoThreadUnwindState orig_restore_state
;
4748 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4749 /* Have to save/restore the restore_ctx as we can be called recursively during invokes etc. */
4750 memcpy (&orig_restore_state
, &tls
->restore_state
, sizeof (MonoThreadUnwindState
));
4751 mono_thread_state_init_from_monoctx (&tls
->restore_state
, &tls
->handler_ctx
);
4755 /* This is called when resuming from a signal handler, so it shouldn't return */
4756 memcpy (&ctx
, &tls
->restore_state
.ctx
, sizeof (MonoContext
));
4757 memcpy (&tls
->restore_state
, &orig_restore_state
, sizeof (MonoThreadUnwindState
));
4758 mono_restore_context (&ctx
);
4759 g_assert_not_reached ();
4763 process_breakpoint (void)
4765 process_signal_event (process_breakpoint_inner
);
4769 resume_from_signal_handler (void *sigctx
, void *func
)
4771 DebuggerTlsData
*tls
;
4774 /* Save the original context in TLS */
4775 // FIXME: This might not work on an altstack ?
4776 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4778 fprintf (stderr
, "Thread %p is not attached to the JIT.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
4781 // FIXME: MonoContext usually doesn't include the fp registers, so these are
4782 // clobbered by a single step/breakpoint event. If this turns out to be a problem,
4783 // clob:c could be added to op_seq_point.
4785 mono_sigctx_to_monoctx (sigctx
, &ctx
);
4786 memcpy (&tls
->handler_ctx
, &ctx
, sizeof (MonoContext
));
4787 #ifdef MONO_ARCH_HAVE_SETUP_RESUME_FROM_SIGNAL_HANDLER_CTX
4788 mono_arch_setup_resume_sighandler_ctx (&ctx
, func
);
4790 MONO_CONTEXT_SET_IP (&ctx
, func
);
4792 mono_monoctx_to_sigctx (&ctx
, sigctx
);
4794 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4795 mono_ppc_set_func_into_sigctx (sigctx
, func
);
4800 mono_debugger_agent_breakpoint_hit (void *sigctx
)
4803 * We are called from a signal handler, and running code there causes all kinds of
4804 * problems, like the original signal is disabled, libgc can't handle altstack, etc.
4805 * So set up the signal context to return to the real breakpoint handler function.
4807 resume_from_signal_handler (sigctx
, process_breakpoint
);
4811 user_break_cb (StackFrameInfo
*frame
, MonoContext
*ctx
, gpointer data
)
4813 if (frame
->managed
) {
4814 *(MonoContext
*)data
= *ctx
;
4823 * Called by System.Diagnostics.Debugger:Break ().
4826 mono_debugger_agent_user_break (void)
4828 if (agent_config
.enabled
) {
4833 /* Obtain a context */
4834 MONO_CONTEXT_SET_IP (&ctx
, NULL
);
4835 mono_walk_stack_with_ctx (user_break_cb
, NULL
, (MonoUnwindOptions
)0, &ctx
);
4836 g_assert (MONO_CONTEXT_GET_IP (&ctx
) != NULL
);
4838 mono_loader_lock ();
4839 events
= create_event_list (EVENT_KIND_USER_BREAK
, NULL
, NULL
, NULL
, &suspend_policy
);
4840 mono_loader_unlock ();
4842 process_event (EVENT_KIND_USER_BREAK
, NULL
, 0, &ctx
, events
, suspend_policy
);
4843 } else if (debug_options
.native_debugger_break
) {
4849 ss_depth_to_string (StepDepth depth
)
4852 case STEP_DEPTH_OVER
:
4854 case STEP_DEPTH_OUT
:
4856 case STEP_DEPTH_INTO
:
4859 g_assert_not_reached ();
4865 process_single_step_inner (DebuggerTlsData
*tls
, gboolean from_signal
)
4870 int il_offset
, suspend_policy
;
4873 MonoContext
*ctx
= &tls
->restore_state
.ctx
;
4876 MonoSeqPointInfo
*info
;
4878 ip
= (guint8
*)MONO_CONTEXT_GET_IP (ctx
);
4880 /* Skip the instruction causing the single step */
4882 mono_arch_skip_single_step (ctx
);
4884 if (suspend_count
> 0) {
4885 /* Fastpath during invokes, see in process_suspend () */
4886 if (suspend_count
- tls
->resume_count
== 0)
4888 process_suspend (tls
, ctx
);
4893 // FIXME: A suspend race
4896 if (mono_thread_internal_current () != ss_req
->thread
)
4899 if (log_level
> 0) {
4900 ji
= mini_jit_info_table_find (mono_domain_get (), (char*)ip
, &domain
);
4902 DEBUG_PRINTF (1, "[%p] Single step event (depth=%s) at %s (%p)[0x%x], sp %p, last sp %p\n", (gpointer
) (gsize
) mono_native_thread_id_get (), ss_depth_to_string (ss_req
->depth
), mono_method_full_name (jinfo_get_method (ji
), TRUE
), MONO_CONTEXT_GET_IP (ctx
), (int)((guint8
*)MONO_CONTEXT_GET_IP (ctx
) - (guint8
*)ji
->code_start
), MONO_CONTEXT_GET_SP (ctx
), ss_req
->last_sp
);
4905 ji
= mini_jit_info_table_find (mono_domain_get (), (char*)ip
, &domain
);
4906 g_assert (ji
&& !ji
->is_trampoline
);
4907 method
= jinfo_get_method (ji
);
4910 if (method
->wrapper_type
&& method
->wrapper_type
!= MONO_WRAPPER_DYNAMIC_METHOD
)
4915 * Stopping in memset makes half-initialized vtypes visible.
4916 * Stopping in memcpy makes half-copied vtypes visible.
4918 if (method
->klass
== mono_defaults
.string_class
&& (!strcmp (method
->name
, "memset") || strstr (method
->name
, "memcpy")))
4922 * The ip points to the instruction causing the single step event, which is before
4923 * the offset recorded in the seq point map, so find the next seq point after ip.
4925 if (!mono_find_next_seq_point_for_native_offset (domain
, method
, (guint8
*)ip
- (guint8
*)ji
->code_start
, &info
, &sp
))
4928 il_offset
= sp
.il_offset
;
4930 if (!ss_update (ss_req
, ji
, &sp
, tls
, ctx
))
4933 /* Start single stepping again from the current sequence point */
4934 ss_start (ss_req
, method
, &sp
, info
, ctx
, tls
, FALSE
, NULL
, 0);
4936 if ((ss_req
->filter
& STEP_FILTER_STATIC_CTOR
) &&
4937 (method
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) &&
4938 !strcmp (method
->name
, ".cctor"))
4941 // FIXME: Has to lock earlier
4943 reqs
= g_ptr_array_new ();
4945 mono_loader_lock ();
4947 g_ptr_array_add (reqs
, ss_req
->req
);
4949 events
= create_event_list (EVENT_KIND_STEP
, reqs
, ji
, NULL
, &suspend_policy
);
4951 g_ptr_array_free (reqs
, TRUE
);
4953 mono_loader_unlock ();
4955 process_event (EVENT_KIND_STEP
, jinfo_get_method (ji
), il_offset
, ctx
, events
, suspend_policy
);
4959 process_single_step (void)
4961 process_signal_event (process_single_step_inner
);
4965 * mono_debugger_agent_single_step_event:
4967 * Called from a signal handler to handle a single step event.
4970 mono_debugger_agent_single_step_event (void *sigctx
)
4972 /* Resume to process_single_step through the signal context */
4974 // FIXME: Since step out/over is implemented using step in, the step in case should
4975 // be as fast as possible. Move the relevant code from process_single_step_inner ()
4978 if (is_debugger_thread ()) {
4980 * This could happen despite our best effors when the runtime calls
4981 * assembly/type resolve hooks.
4982 * FIXME: Breakpoints too.
4986 mono_sigctx_to_monoctx (sigctx
, &ctx
);
4987 mono_arch_skip_single_step (&ctx
);
4988 mono_monoctx_to_sigctx (&ctx
, sigctx
);
4992 resume_from_signal_handler (sigctx
, process_single_step
);
4996 debugger_agent_single_step_from_context (MonoContext
*ctx
)
4998 DebuggerTlsData
*tls
;
4999 MonoThreadUnwindState orig_restore_state
;
5001 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
5002 /* Fastpath during invokes, see in process_suspend () */
5003 if (tls
&& suspend_count
&& suspend_count
- tls
->resume_count
== 0)
5006 if (is_debugger_thread ())
5011 /* Have to save/restore the restore_ctx as we can be called recursively during invokes etc. */
5012 memcpy (&orig_restore_state
, &tls
->restore_state
, sizeof (MonoThreadUnwindState
));
5013 mono_thread_state_init_from_monoctx (&tls
->restore_state
, ctx
);
5014 memcpy (&tls
->handler_ctx
, ctx
, sizeof (MonoContext
));
5016 process_single_step_inner (tls
, FALSE
);
5018 memcpy (ctx
, &tls
->restore_state
.ctx
, sizeof (MonoContext
));
5019 memcpy (&tls
->restore_state
, &orig_restore_state
, sizeof (MonoThreadUnwindState
));
5023 debugger_agent_breakpoint_from_context (MonoContext
*ctx
)
5025 DebuggerTlsData
*tls
;
5026 MonoThreadUnwindState orig_restore_state
;
5029 if (is_debugger_thread ())
5032 orig_ip
= (guint8
*)MONO_CONTEXT_GET_IP (ctx
);
5033 MONO_CONTEXT_SET_IP (ctx
, orig_ip
- 1);
5035 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
5037 memcpy (&orig_restore_state
, &tls
->restore_state
, sizeof (MonoThreadUnwindState
));
5038 mono_thread_state_init_from_monoctx (&tls
->restore_state
, ctx
);
5039 memcpy (&tls
->handler_ctx
, ctx
, sizeof (MonoContext
));
5041 process_breakpoint_inner (tls
, FALSE
);
5043 memcpy (ctx
, &tls
->restore_state
.ctx
, sizeof (MonoContext
));
5044 memcpy (&tls
->restore_state
, &orig_restore_state
, sizeof (MonoThreadUnwindState
));
5045 if (MONO_CONTEXT_GET_IP (ctx
) == orig_ip
- 1)
5046 MONO_CONTEXT_SET_IP (ctx
, orig_ip
);
5050 * start_single_stepping:
5052 * Turn on single stepping. Can be called multiple times, for example,
5053 * by a single step event request + a suspend.
5056 start_single_stepping (void)
5058 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
5059 int val
= InterlockedIncrement (&ss_count
);
5062 mono_arch_start_single_stepping ();
5064 g_assert_not_reached ();
5069 stop_single_stepping (void)
5071 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
5072 int val
= InterlockedDecrement (&ss_count
);
5075 mono_arch_stop_single_stepping ();
5077 g_assert_not_reached ();
5084 * Stop the single stepping operation given by SS_REQ.
5087 ss_stop (SingleStepReq
*ss_req
)
5092 for (l
= ss_req
->bps
; l
; l
= l
->next
) {
5093 clear_breakpoint ((MonoBreakpoint
*)l
->data
);
5095 g_slist_free (ss_req
->bps
);
5099 if (ss_req
->global
) {
5100 stop_single_stepping ();
5101 ss_req
->global
= FALSE
;
5108 * Reject breakpoint if it is a duplicate of one already in list or hash table.
5111 ss_bp_is_unique (GSList
*bps
, GHashTable
*ss_req_bp_cache
, MonoMethod
*method
, guint32 il_offset
)
5113 if (ss_req_bp_cache
) {
5114 MonoBreakpoint dummy
= {method
, il_offset
, NULL
, NULL
};
5115 return !g_hash_table_lookup (ss_req_bp_cache
, &dummy
);
5117 for (GSList
*l
= bps
; l
; l
= l
->next
) {
5118 MonoBreakpoint
*bp
= (MonoBreakpoint
*)l
->data
;
5119 if (bp
->method
== method
&& bp
->il_offset
== il_offset
)
5128 * GHashTable equality for a MonoBreakpoint (only care about method and il_offset fields)
5131 ss_bp_eq (gconstpointer ka
, gconstpointer kb
)
5133 const MonoBreakpoint
*s1
= (const MonoBreakpoint
*)ka
;
5134 const MonoBreakpoint
*s2
= (const MonoBreakpoint
*)kb
;
5135 return (s1
->method
== s2
->method
&& s1
->il_offset
== s2
->il_offset
) ? 1 : 0;
5141 * GHashTable hash for a MonoBreakpoint (only care about method and il_offset fields)
5144 ss_bp_hash (gconstpointer data
)
5146 const MonoBreakpoint
*s
= (const MonoBreakpoint
*)data
;
5147 guint hash
= (guint
) (uintptr_t) s
->method
;
5148 hash
^= ((guint
)s
->il_offset
) << 16; // Assume low bits are more interesting
5149 hash
^= ((guint
)s
->il_offset
) >> 16;
5153 #define MAX_LINEAR_SCAN_BPS 7
5158 * Create a new breakpoint and add it to a step request.
5159 * Will adjust the bp count and cache used by ss_start.
5162 ss_bp_add_one (SingleStepReq
*ss_req
, int *ss_req_bp_count
, GHashTable
**ss_req_bp_cache
,
5163 MonoMethod
*method
, guint32 il_offset
)
5165 // This list is getting too long, switch to using the hash table
5166 if (!*ss_req_bp_cache
&& *ss_req_bp_count
> MAX_LINEAR_SCAN_BPS
) {
5167 *ss_req_bp_cache
= g_hash_table_new (ss_bp_hash
, ss_bp_eq
);
5168 for (GSList
*l
= ss_req
->bps
; l
; l
= l
->next
)
5169 g_hash_table_insert (*ss_req_bp_cache
, l
->data
, l
->data
);
5172 if (ss_bp_is_unique (ss_req
->bps
, *ss_req_bp_cache
, method
, il_offset
)) {
5173 // Create and add breakpoint
5174 MonoBreakpoint
*bp
= set_breakpoint (method
, il_offset
, ss_req
->req
, NULL
);
5175 ss_req
->bps
= g_slist_append (ss_req
->bps
, bp
);
5176 if (*ss_req_bp_cache
)
5177 g_hash_table_insert (*ss_req_bp_cache
, bp
, bp
);
5178 (*ss_req_bp_count
)++;
5180 DEBUG_PRINTF (1, "[dbg] Candidate breakpoint at %s:[il=0x%x] is a duplicate for this step request, will not add.\n", mono_method_full_name (method
, TRUE
), (int)il_offset
);
5187 * Start the single stepping operation given by SS_REQ from the sequence point SP.
5188 * If CTX is not set, then this can target any thread. If CTX is set, then TLS should
5189 * belong to the same thread as CTX.
5190 * If FRAMES is not-null, use that instead of tls->frames for placing breakpoints etc.
5193 ss_start (SingleStepReq
*ss_req
, MonoMethod
*method
, SeqPoint
* sp
, MonoSeqPointInfo
*info
, MonoContext
*ctx
, DebuggerTlsData
*tls
,
5194 gboolean step_to_catch
, StackFrame
**frames
, int nframes
)
5196 int i
, j
, frame_index
;
5197 SeqPoint
*next_sp
, *parent_sp
= NULL
;
5198 SeqPoint local_sp
, local_parent_sp
;
5200 MonoSeqPointInfo
*parent_info
;
5201 MonoMethod
*parent_sp_method
= NULL
;
5202 gboolean enable_global
= FALSE
;
5204 // When 8 or more entries are in bps, we build a hash table to serve as a set of breakpoints.
5205 // Recreating this on each pass is a little wasteful but at least keeps behavior linear.
5206 int ss_req_bp_count
= g_slist_length (ss_req
->bps
);
5207 GHashTable
*ss_req_bp_cache
= NULL
;
5209 /* Stop the previous operation */
5213 * Implement single stepping using breakpoints if possible.
5215 if (step_to_catch
) {
5216 ss_bp_add_one (ss_req
, &ss_req_bp_count
, &ss_req_bp_cache
, method
, sp
->il_offset
);
5220 if (ctx
&& !frames
) {
5221 /* Need parent frames */
5222 if (!tls
->context
.valid
)
5223 mono_thread_state_init_from_monoctx (&tls
->context
, ctx
);
5224 compute_frame_info (tls
->thread
, tls
);
5225 frames
= tls
->frames
;
5226 nframes
= tls
->frame_count
;
5230 * Find the first sequence point in the current or in a previous frame which
5231 * is not the last in its method.
5233 if (ss_req
->depth
== STEP_DEPTH_OUT
) {
5234 /* Ignore seq points in current method */
5235 while (frame_index
< nframes
) {
5236 StackFrame
*frame
= frames
[frame_index
];
5238 method
= frame
->method
;
5239 found_sp
= mono_find_prev_seq_point_for_native_offset (frame
->domain
, frame
->method
, frame
->native_offset
, &info
, &local_sp
);
5240 sp
= (found_sp
)? &local_sp
: NULL
;
5242 if (sp
&& sp
->next_len
!= 0)
5245 // There could be method calls before the next seq point in the caller when using nested calls
5246 //enable_global = TRUE;
5248 if (sp
&& sp
->next_len
== 0) {
5250 while (frame_index
< nframes
) {
5251 StackFrame
*frame
= frames
[frame_index
];
5253 method
= frame
->method
;
5254 found_sp
= mono_find_prev_seq_point_for_native_offset (frame
->domain
, frame
->method
, frame
->native_offset
, &info
, &local_sp
);
5255 sp
= (found_sp
)? &local_sp
: NULL
;
5256 if (sp
&& sp
->next_len
!= 0)
5262 /* Have to put a breakpoint into a parent frame since the seq points might not cover all control flow out of the method */
5263 while (frame_index
< nframes
) {
5264 StackFrame
*frame
= frames
[frame_index
];
5266 parent_sp_method
= frame
->method
;
5267 found_sp
= mono_find_prev_seq_point_for_native_offset (frame
->domain
, frame
->method
, frame
->native_offset
, &parent_info
, &local_parent_sp
);
5268 parent_sp
= found_sp
? &local_parent_sp
: NULL
;
5269 if (found_sp
&& parent_sp
->next_len
!= 0)
5277 if (sp
&& sp
->next_len
> 0) {
5278 SeqPoint
* next
= g_new(SeqPoint
, sp
->next_len
);
5280 mono_seq_point_init_next (info
, *sp
, next
);
5281 for (i
= 0; i
< sp
->next_len
; i
++) {
5284 ss_bp_add_one (ss_req
, &ss_req_bp_count
, &ss_req_bp_cache
, method
, next_sp
->il_offset
);
5290 SeqPoint
* next
= g_new(SeqPoint
, parent_sp
->next_len
);
5292 mono_seq_point_init_next (parent_info
, *parent_sp
, next
);
5293 for (i
= 0; i
< parent_sp
->next_len
; i
++) {
5296 ss_bp_add_one (ss_req
, &ss_req_bp_count
, &ss_req_bp_cache
, parent_sp_method
, next_sp
->il_offset
);
5301 if (ss_req
->nframes
== 0)
5302 ss_req
->nframes
= nframes
;
5304 if ((ss_req
->depth
== STEP_DEPTH_OVER
) && (!sp
&& !parent_sp
)) {
5305 DEBUG_PRINTF (1, "[dbg] No parent frame for step over, transition to step into.\n");
5307 * This is needed since if we leave managed code, and later return to it, step over
5308 * is not going to stop.
5309 * This approach is a bit ugly, since we change the step depth, but it only affects
5310 * clients who reuse the same step request, and only in this special case.
5312 ss_req
->depth
= STEP_DEPTH_INTO
;
5315 if (ss_req
->depth
== STEP_DEPTH_OVER
) {
5316 /* Need to stop in catch clauses as well */
5317 for (i
= 0; i
< nframes
; ++i
) {
5318 StackFrame
*frame
= frames
[i
];
5321 MonoJitInfo
*jinfo
= frame
->ji
;
5322 for (j
= 0; j
< jinfo
->num_clauses
; ++j
) {
5323 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[j
];
5325 found_sp
= mono_find_next_seq_point_for_native_offset (frame
->domain
, frame
->method
, (char*)ei
->handler_start
- (char*)jinfo
->code_start
, NULL
, &local_sp
);
5326 sp
= (found_sp
)? &local_sp
: NULL
;
5328 ss_bp_add_one (ss_req
, &ss_req_bp_count
, &ss_req_bp_cache
, frame
->method
, sp
->il_offset
);
5334 if (ss_req
->depth
== STEP_DEPTH_INTO
) {
5335 /* Enable global stepping so we stop at method entry too */
5336 enable_global
= TRUE
;
5340 * The ctx/frame info computed above will become invalid when we continue.
5342 tls
->context
.valid
= FALSE
;
5343 tls
->async_state
.valid
= FALSE
;
5344 invalidate_frames (tls
);
5347 if (enable_global
) {
5348 DEBUG_PRINTF (1, "[dbg] Turning on global single stepping.\n");
5349 ss_req
->global
= TRUE
;
5350 start_single_stepping ();
5351 } else if (!ss_req
->bps
) {
5352 DEBUG_PRINTF (1, "[dbg] Turning on global single stepping.\n");
5353 ss_req
->global
= TRUE
;
5354 start_single_stepping ();
5356 ss_req
->global
= FALSE
;
5359 if (ss_req_bp_cache
)
5360 g_hash_table_destroy (ss_req_bp_cache
);
5364 * Start single stepping of thread THREAD
5367 ss_create (MonoInternalThread
*thread
, StepSize size
, StepDepth depth
, StepFilter filter
, EventRequest
*req
)
5369 DebuggerTlsData
*tls
;
5370 MonoSeqPointInfo
*info
= NULL
;
5371 SeqPoint
*sp
= NULL
;
5374 MonoMethod
*method
= NULL
;
5375 MonoDebugMethodInfo
*minfo
;
5376 gboolean step_to_catch
= FALSE
;
5377 gboolean set_ip
= FALSE
;
5378 StackFrame
**frames
= NULL
;
5381 if (suspend_count
== 0)
5382 return ERR_NOT_SUSPENDED
;
5384 wait_for_suspend ();
5386 // FIXME: Multiple requests
5388 DEBUG_PRINTF (0, "Received a single step request while the previous one was still active.\n");
5389 return ERR_NOT_IMPLEMENTED
;
5392 DEBUG_PRINTF (1, "[dbg] Starting single step of thread %p (depth=%s).\n", thread
, ss_depth_to_string (depth
));
5394 ss_req
= g_new0 (SingleStepReq
, 1);
5396 ss_req
->thread
= thread
;
5397 ss_req
->size
= size
;
5398 ss_req
->depth
= depth
;
5399 ss_req
->filter
= filter
;
5402 mono_loader_lock ();
5403 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
5404 mono_loader_unlock ();
5406 g_assert (tls
->context
.valid
);
5408 if (tls
->restore_state
.valid
&& MONO_CONTEXT_GET_IP (&tls
->context
.ctx
) != MONO_CONTEXT_GET_IP (&tls
->restore_state
.ctx
)) {
5410 * Need to start single stepping from restore_state and not from the current state
5413 frames
= compute_frame_info_from (thread
, tls
, &tls
->restore_state
, &nframes
);
5416 ss_req
->start_sp
= ss_req
->last_sp
= MONO_CONTEXT_GET_SP (&tls
->context
.ctx
);
5418 if (tls
->catch_state
.valid
) {
5420 StackFrameInfo frame
;
5421 MonoContext new_ctx
;
5422 MonoLMF
*lmf
= NULL
;
5425 * We are stopped at a throw site. Stepping should go to the catch site.
5428 /* Find the the jit info for the catch context */
5429 res
= mono_find_jit_info_ext (
5430 (MonoDomain
*)tls
->catch_state
.unwind_data
[MONO_UNWIND_DATA_DOMAIN
],
5431 (MonoJitTlsData
*)((MonoThreadInfo
*)thread
->thread_info
)->jit_data
,
5432 NULL
, &tls
->catch_state
.ctx
, &new_ctx
, NULL
, &lmf
, NULL
, &frame
);
5434 g_assert (frame
.type
== FRAME_TYPE_MANAGED
);
5437 * Find the seq point corresponding to the landing site ip, which is the first seq
5440 found_sp
= mono_find_next_seq_point_for_native_offset (frame
.domain
, frame
.method
, frame
.native_offset
, &info
, &local_sp
);
5441 sp
= (found_sp
)? &local_sp
: NULL
;
5443 no_seq_points_found (frame
.method
, frame
.native_offset
);
5446 method
= frame
.method
;
5448 step_to_catch
= TRUE
;
5449 /* This make sure the seq point is not skipped by process_single_step () */
5450 ss_req
->last_sp
= NULL
;
5453 if (!step_to_catch
) {
5454 StackFrame
*frame
= NULL
;
5457 if (frames
&& nframes
)
5460 compute_frame_info (thread
, tls
);
5462 if (tls
->frame_count
)
5463 frame
= tls
->frames
[0];
5466 if (ss_req
->size
== STEP_SIZE_LINE
) {
5468 ss_req
->last_method
= frame
->method
;
5469 ss_req
->last_line
= -1;
5471 minfo
= mono_debug_lookup_method (frame
->method
);
5472 if (minfo
&& frame
->il_offset
!= -1) {
5473 MonoDebugSourceLocation
*loc
= mono_debug_method_lookup_location (minfo
, frame
->il_offset
);
5476 ss_req
->last_line
= loc
->row
;
5484 if (!method
&& frame
->il_offset
!= -1) {
5485 /* FIXME: Sort the table and use a binary search */
5486 found_sp
= mono_find_prev_seq_point_for_native_offset (frame
->domain
, frame
->method
, frame
->native_offset
, &info
, &local_sp
);
5487 sp
= (found_sp
)? &local_sp
: NULL
;
5489 no_seq_points_found (frame
->method
, frame
->native_offset
);
5491 method
= frame
->method
;
5496 ss_req
->start_method
= method
;
5498 ss_start (ss_req
, method
, sp
, info
, set_ip
? &tls
->restore_state
.ctx
: &tls
->context
.ctx
, tls
, step_to_catch
, frames
, nframes
);
5501 free_frames (frames
, nframes
);
5507 ss_destroy (SingleStepReq
*req
)
5510 g_assert (ss_req
== req
);
5519 ss_clear_for_assembly (SingleStepReq
*req
, MonoAssembly
*assembly
)
5522 gboolean found
= TRUE
;
5526 for (l
= ss_req
->bps
; l
; l
= l
->next
) {
5527 if (breakpoint_matches_assembly ((MonoBreakpoint
*)l
->data
, assembly
)) {
5528 clear_breakpoint ((MonoBreakpoint
*)l
->data
);
5529 ss_req
->bps
= g_slist_delete_link (ss_req
->bps
, l
);
5538 * Called from metadata by the icall for System.Diagnostics.Debugger:Log ().
5541 mono_debugger_agent_debug_log (int level
, MonoString
*category
, MonoString
*message
)
5548 if (!agent_config
.enabled
)
5551 mono_loader_lock ();
5552 events
= create_event_list (EVENT_KIND_USER_LOG
, NULL
, NULL
, NULL
, &suspend_policy
);
5553 mono_loader_unlock ();
5558 ei
.category
= mono_string_to_utf8_checked (category
, &error
);
5559 mono_error_cleanup (&error
);
5563 ei
.message
= mono_string_to_utf8_checked (message
, &error
);
5564 mono_error_cleanup (&error
);
5567 process_event (EVENT_KIND_USER_LOG
, &ei
, 0, NULL
, events
, suspend_policy
);
5569 g_free (ei
.category
);
5570 g_free (ei
.message
);
5574 mono_debugger_agent_debug_log_is_enabled (void)
5576 /* Treat this as true even if there is no event request for EVENT_KIND_USER_LOG */
5577 return agent_config
.enabled
;
5580 #if defined(PLATFORM_ANDROID) || defined(TARGET_ANDROID)
5582 mono_debugger_agent_unhandled_exception (MonoException
*exc
)
5591 memset (&ei
, 0, sizeof (EventInfo
));
5592 ei
.exc
= (MonoObject
*)exc
;
5594 mono_loader_lock ();
5595 events
= create_event_list (EVENT_KIND_EXCEPTION
, NULL
, NULL
, &ei
, &suspend_policy
);
5596 mono_loader_unlock ();
5598 process_event (EVENT_KIND_EXCEPTION
, &ei
, 0, NULL
, events
, suspend_policy
);
5603 mono_debugger_agent_handle_exception (MonoException
*exc
, MonoContext
*throw_ctx
,
5604 MonoContext
*catch_ctx
)
5606 int i
, j
, suspend_policy
;
5608 MonoJitInfo
*ji
, *catch_ji
;
5610 DebuggerTlsData
*tls
= NULL
;
5612 if (thread_to_tls
!= NULL
) {
5613 MonoInternalThread
*thread
= mono_thread_internal_current ();
5615 mono_loader_lock ();
5616 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
5617 mono_loader_unlock ();
5619 if (tls
&& tls
->abort_requested
)
5621 if (tls
&& tls
->disable_breakpoints
)
5625 memset (&ei
, 0, sizeof (EventInfo
));
5627 /* Just-In-Time debugging */
5629 if (agent_config
.onuncaught
&& !inited
) {
5630 finish_agent_init (FALSE
);
5633 * Send an unsolicited EXCEPTION event with a dummy request id.
5635 events
= g_slist_append (NULL
, GUINT_TO_POINTER (0xffffff));
5636 ei
.exc
= (MonoObject
*)exc
;
5637 process_event (EVENT_KIND_EXCEPTION
, &ei
, 0, throw_ctx
, events
, SUSPEND_POLICY_ALL
);
5640 } else if (agent_config
.onthrow
&& !inited
) {
5642 gboolean found
= FALSE
;
5644 for (l
= agent_config
.onthrow
; l
; l
= l
->next
) {
5645 char *ex_type
= (char *)l
->data
;
5646 char *f
= mono_type_full_name (&exc
->object
.vtable
->klass
->byval_arg
);
5648 if (!strcmp (ex_type
, "") || !strcmp (ex_type
, f
))
5655 finish_agent_init (FALSE
);
5658 * Send an unsolicited EXCEPTION event with a dummy request id.
5660 events
= g_slist_append (NULL
, GUINT_TO_POINTER (0xffffff));
5661 ei
.exc
= (MonoObject
*)exc
;
5662 process_event (EVENT_KIND_EXCEPTION
, &ei
, 0, throw_ctx
, events
, SUSPEND_POLICY_ALL
);
5670 ji
= mini_jit_info_table_find (mono_domain_get (), (char *)MONO_CONTEXT_GET_IP (throw_ctx
), NULL
);
5672 catch_ji
= mini_jit_info_table_find (mono_domain_get (), (char *)MONO_CONTEXT_GET_IP (catch_ctx
), NULL
);
5676 ei
.exc
= (MonoObject
*)exc
;
5677 ei
.caught
= catch_ctx
!= NULL
;
5679 mono_loader_lock ();
5681 /* Treat exceptions which are caught in non-user code as unhandled */
5682 for (i
= 0; i
< event_requests
->len
; ++i
) {
5683 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
5684 if (req
->event_kind
!= EVENT_KIND_EXCEPTION
)
5687 for (j
= 0; j
< req
->nmodifiers
; ++j
) {
5688 Modifier
*mod
= &req
->modifiers
[j
];
5690 if (mod
->kind
== MOD_KIND_ASSEMBLY_ONLY
&& catch_ji
) {
5692 gboolean found
= FALSE
;
5693 MonoAssembly
**assemblies
= mod
->data
.assemblies
;
5696 for (k
= 0; assemblies
[k
]; ++k
)
5697 if (assemblies
[k
] == jinfo_get_method (catch_ji
)->klass
->image
->assembly
)
5706 events
= create_event_list (EVENT_KIND_EXCEPTION
, NULL
, ji
, &ei
, &suspend_policy
);
5707 mono_loader_unlock ();
5709 if (tls
&& ei
.caught
&& catch_ctx
) {
5710 memset (&tls
->catch_state
, 0, sizeof (tls
->catch_state
));
5711 tls
->catch_state
.ctx
= *catch_ctx
;
5712 tls
->catch_state
.unwind_data
[MONO_UNWIND_DATA_DOMAIN
] = mono_domain_get ();
5713 tls
->catch_state
.valid
= TRUE
;
5716 process_event (EVENT_KIND_EXCEPTION
, &ei
, 0, throw_ctx
, events
, suspend_policy
);
5719 tls
->catch_state
.valid
= FALSE
;
5723 mono_debugger_agent_begin_exception_filter (MonoException
*exc
, MonoContext
*ctx
, MonoContext
*orig_ctx
)
5725 DebuggerTlsData
*tls
;
5730 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
5735 * We're about to invoke an exception filter during the first pass of exception handling.
5737 * 'ctx' is the context that'll get passed to the filter ('call_filter (ctx, ei->data.filter)'),
5738 * 'orig_ctx' is the context where the exception has been thrown.
5741 * See mcs/class/Mono.Debugger.Soft/Tests/dtest-excfilter.il for an example.
5743 * If we're stopped in Filter(), normal stack unwinding would first unwind to
5744 * the call site (line 37) and then continue to Main(), but it would never
5745 * include the throw site (line 32).
5747 * Since exception filters are invoked during the first pass of exception handling,
5748 * the stack frames of the throw site are still intact, so we should include them
5751 * We do this here by saving the context of the throw site in 'tls->filter_state'.
5753 * Exception filters are used by MonoDroid, where we want to stop inside a call filter,
5754 * but report the location of the 'throw' to the user.
5758 g_assert (mono_thread_state_init_from_monoctx (&tls
->filter_state
, orig_ctx
));
5762 mono_debugger_agent_end_exception_filter (MonoException
*exc
, MonoContext
*ctx
, MonoContext
*orig_ctx
)
5764 DebuggerTlsData
*tls
;
5769 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
5773 tls
->filter_state
.valid
= FALSE
;
5777 * buffer_add_value_full:
5779 * Add the encoding of the value at ADDR described by T to the buffer.
5780 * AS_VTYPE determines whenever to treat primitive types as primitive types or
5784 buffer_add_value_full (Buffer
*buf
, MonoType
*t
, void *addr
, MonoDomain
*domain
,
5785 gboolean as_vtype
, GHashTable
*parent_vtypes
)
5788 gboolean boxed_vtype
= FALSE
;
5791 if (!(*(void**)addr
)) {
5792 /* This can happen with compiler generated locals */
5793 //printf ("%s\n", mono_type_full_name (t));
5794 buffer_add_byte (buf
, VALUE_TYPE_ID_NULL
);
5797 g_assert (*(void**)addr
);
5798 addr
= *(void**)addr
;
5803 case MONO_TYPE_BOOLEAN
:
5806 case MONO_TYPE_CHAR
:
5826 case MONO_TYPE_VOID
:
5827 buffer_add_byte (buf
, t
->type
);
5829 case MONO_TYPE_BOOLEAN
:
5832 buffer_add_byte (buf
, t
->type
);
5833 buffer_add_int (buf
, *(gint8
*)addr
);
5835 case MONO_TYPE_CHAR
:
5838 buffer_add_byte (buf
, t
->type
);
5839 buffer_add_int (buf
, *(gint16
*)addr
);
5844 buffer_add_byte (buf
, t
->type
);
5845 buffer_add_int (buf
, *(gint32
*)addr
);
5850 buffer_add_byte (buf
, t
->type
);
5851 buffer_add_long (buf
, *(gint64
*)addr
);
5855 /* Treat it as a vtype */
5857 case MONO_TYPE_PTR
: {
5858 gssize val
= *(gssize
*)addr
;
5860 buffer_add_byte (buf
, t
->type
);
5861 buffer_add_long (buf
, val
);
5865 case MONO_TYPE_STRING
:
5866 case MONO_TYPE_SZARRAY
:
5867 case MONO_TYPE_OBJECT
:
5868 case MONO_TYPE_CLASS
:
5869 case MONO_TYPE_ARRAY
:
5870 obj
= *(MonoObject
**)addr
;
5873 buffer_add_byte (buf
, VALUE_TYPE_ID_NULL
);
5875 if (obj
->vtable
->klass
->valuetype
) {
5876 t
= &obj
->vtable
->klass
->byval_arg
;
5877 addr
= mono_object_unbox (obj
);
5880 } else if (obj
->vtable
->klass
->rank
) {
5881 buffer_add_byte (buf
, obj
->vtable
->klass
->byval_arg
.type
);
5882 } else if (obj
->vtable
->klass
->byval_arg
.type
== MONO_TYPE_GENERICINST
) {
5883 buffer_add_byte (buf
, MONO_TYPE_CLASS
);
5885 buffer_add_byte (buf
, obj
->vtable
->klass
->byval_arg
.type
);
5887 buffer_add_objid (buf
, obj
);
5891 case MONO_TYPE_VALUETYPE
:
5892 case MONO_TYPE_TYPEDBYREF
: {
5896 MonoClass
*klass
= mono_class_from_mono_type (t
);
5901 * Handle boxed vtypes recursively referencing themselves using fields.
5904 parent_vtypes
= g_hash_table_new (NULL
, NULL
);
5905 vtype_index
= GPOINTER_TO_INT (g_hash_table_lookup (parent_vtypes
, addr
));
5907 if (CHECK_PROTOCOL_VERSION (2, 33)) {
5908 buffer_add_byte (buf
, VALUE_TYPE_ID_PARENT_VTYPE
);
5909 buffer_add_int (buf
, vtype_index
- 1);
5911 /* The client can't handle PARENT_VTYPE */
5912 buffer_add_byte (buf
, VALUE_TYPE_ID_NULL
);
5916 g_hash_table_insert (parent_vtypes
, addr
, GINT_TO_POINTER (g_hash_table_size (parent_vtypes
) + 1));
5920 buffer_add_byte (buf
, MONO_TYPE_VALUETYPE
);
5921 buffer_add_byte (buf
, klass
->enumtype
);
5922 buffer_add_typeid (buf
, domain
, klass
);
5926 while ((f
= mono_class_get_fields (klass
, &iter
))) {
5927 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
5929 if (mono_field_is_deleted (f
))
5933 buffer_add_int (buf
, nfields
);
5936 while ((f
= mono_class_get_fields (klass
, &iter
))) {
5937 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
5939 if (mono_field_is_deleted (f
))
5941 buffer_add_value_full (buf
, f
->type
, (guint8
*)addr
+ f
->offset
- sizeof (MonoObject
), domain
, FALSE
, parent_vtypes
);
5945 g_hash_table_remove (parent_vtypes
, addr
);
5946 if (g_hash_table_size (parent_vtypes
) == 0) {
5947 g_hash_table_destroy (parent_vtypes
);
5948 parent_vtypes
= NULL
;
5953 case MONO_TYPE_GENERICINST
:
5954 if (mono_type_generic_inst_is_valuetype (t
)) {
5966 buffer_add_value (Buffer
*buf
, MonoType
*t
, void *addr
, MonoDomain
*domain
)
5968 buffer_add_value_full (buf
, t
, addr
, domain
, FALSE
, NULL
);
5972 obj_is_of_type (MonoObject
*obj
, MonoType
*t
)
5974 MonoClass
*klass
= obj
->vtable
->klass
;
5975 if (!mono_class_is_assignable_from (mono_class_from_mono_type (t
), klass
)) {
5976 if (mono_class_is_transparent_proxy (klass
)) {
5977 klass
= ((MonoTransparentProxy
*)obj
)->remote_class
->proxy_class
;
5978 if (mono_class_is_assignable_from (mono_class_from_mono_type (t
), klass
)) {
5988 decode_value (MonoType
*t
, MonoDomain
*domain
, guint8
*addr
, guint8
*buf
, guint8
**endbuf
, guint8
*limit
);
5991 decode_vtype (MonoType
*t
, MonoDomain
*domain
, guint8
*addr
, guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
5997 gpointer iter
= NULL
;
6001 is_enum
= decode_byte (buf
, &buf
, limit
);
6002 /* Enums are sent as a normal vtype */
6004 return ERR_NOT_IMPLEMENTED
;
6005 klass
= decode_typeid (buf
, &buf
, limit
, &d
, &err
);
6006 if (err
!= ERR_NONE
)
6009 if (t
&& klass
!= mono_class_from_mono_type (t
)) {
6010 char *name
= mono_type_full_name (t
);
6011 char *name2
= mono_type_full_name (&klass
->byval_arg
);
6012 DEBUG_PRINTF (1, "[%p] Expected value of type %s, got %s.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), name
, name2
);
6015 return ERR_INVALID_ARGUMENT
;
6018 nfields
= decode_int (buf
, &buf
, limit
);
6019 while ((f
= mono_class_get_fields (klass
, &iter
))) {
6020 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
6022 if (mono_field_is_deleted (f
))
6024 err
= decode_value (f
->type
, domain
, (guint8
*)addr
+ f
->offset
- sizeof (MonoObject
), buf
, &buf
, limit
);
6025 if (err
!= ERR_NONE
)
6029 g_assert (nfields
== 0);
6037 decode_value_internal (MonoType
*t
, int type
, MonoDomain
*domain
, guint8
*addr
, guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
6041 if (type
!= t
->type
&& !MONO_TYPE_IS_REFERENCE (t
) &&
6042 !(t
->type
== MONO_TYPE_I
&& type
== MONO_TYPE_VALUETYPE
) &&
6043 !(t
->type
== MONO_TYPE_U
&& type
== MONO_TYPE_VALUETYPE
) &&
6044 !(t
->type
== MONO_TYPE_PTR
&& type
== MONO_TYPE_I8
) &&
6045 !(t
->type
== MONO_TYPE_GENERICINST
&& type
== MONO_TYPE_VALUETYPE
) &&
6046 !(t
->type
== MONO_TYPE_VALUETYPE
&& type
== MONO_TYPE_OBJECT
)) {
6047 char *name
= mono_type_full_name (t
);
6048 DEBUG_PRINTF (1, "[%p] Expected value of type %s, got 0x%0x.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), name
, type
);
6050 return ERR_INVALID_ARGUMENT
;
6054 case MONO_TYPE_BOOLEAN
:
6055 *(guint8
*)addr
= decode_int (buf
, &buf
, limit
);
6057 case MONO_TYPE_CHAR
:
6058 *(gunichar2
*)addr
= decode_int (buf
, &buf
, limit
);
6061 *(gint8
*)addr
= decode_int (buf
, &buf
, limit
);
6064 *(guint8
*)addr
= decode_int (buf
, &buf
, limit
);
6067 *(gint16
*)addr
= decode_int (buf
, &buf
, limit
);
6070 *(guint16
*)addr
= decode_int (buf
, &buf
, limit
);
6073 *(gint32
*)addr
= decode_int (buf
, &buf
, limit
);
6076 *(guint32
*)addr
= decode_int (buf
, &buf
, limit
);
6079 *(gint64
*)addr
= decode_long (buf
, &buf
, limit
);
6082 *(guint64
*)addr
= decode_long (buf
, &buf
, limit
);
6085 *(guint32
*)addr
= decode_int (buf
, &buf
, limit
);
6088 *(guint64
*)addr
= decode_long (buf
, &buf
, limit
);
6091 /* We send these as I8, so we get them back as such */
6092 g_assert (type
== MONO_TYPE_I8
);
6093 *(gssize
*)addr
= decode_long (buf
, &buf
, limit
);
6095 case MONO_TYPE_GENERICINST
:
6096 if (MONO_TYPE_ISSTRUCT (t
)) {
6097 /* The client sends these as a valuetype */
6105 /* We send these as vtypes, so we get them back as such */
6106 g_assert (type
== MONO_TYPE_VALUETYPE
);
6109 case MONO_TYPE_VALUETYPE
:
6110 if (type
== MONO_TYPE_OBJECT
) {
6112 int objid
= decode_objid (buf
, &buf
, limit
);
6116 err
= get_object (objid
, (MonoObject
**)&obj
);
6117 if (err
!= ERR_NONE
)
6120 return ERR_INVALID_ARGUMENT
;
6121 if (obj
->vtable
->klass
!= mono_class_from_mono_type (t
)) {
6122 DEBUG_PRINTF (1, "Expected type '%s', got object '%s'\n", mono_type_full_name (t
), obj
->vtable
->klass
->name
);
6123 return ERR_INVALID_ARGUMENT
;
6125 memcpy (addr
, mono_object_unbox (obj
), mono_class_value_size (obj
->vtable
->klass
, NULL
));
6127 err
= decode_vtype (t
, domain
, addr
, buf
, &buf
, limit
);
6128 if (err
!= ERR_NONE
)
6134 if (MONO_TYPE_IS_REFERENCE (t
)) {
6135 if (type
== MONO_TYPE_OBJECT
) {
6136 int objid
= decode_objid (buf
, &buf
, limit
);
6140 err
= get_object (objid
, (MonoObject
**)&obj
);
6141 if (err
!= ERR_NONE
)
6145 if (!obj_is_of_type (obj
, t
)) {
6146 DEBUG_PRINTF (1, "Expected type '%s', got '%s'\n", mono_type_full_name (t
), obj
->vtable
->klass
->name
);
6147 return ERR_INVALID_ARGUMENT
;
6150 if (obj
&& obj
->vtable
->domain
!= domain
)
6151 return ERR_INVALID_ARGUMENT
;
6153 mono_gc_wbarrier_generic_store (addr
, obj
);
6154 } else if (type
== VALUE_TYPE_ID_NULL
) {
6155 *(MonoObject
**)addr
= NULL
;
6156 } else if (type
== MONO_TYPE_VALUETYPE
) {
6165 /* This can happen when round-tripping boxed vtypes */
6167 * Obtain vtype class.
6168 * Same as the beginning of the handle_vtype case above.
6171 is_enum
= decode_byte (buf
, &buf
, limit
);
6173 return ERR_NOT_IMPLEMENTED
;
6174 klass
= decode_typeid (buf
, &buf
, limit
, &d
, &err
);
6175 if (err
!= ERR_NONE
)
6178 /* Decode the vtype into a temporary buffer, then box it. */
6179 vtype_buf_size
= mono_class_value_size (klass
, NULL
);
6180 vtype_buf
= (guint8
*)g_malloc0 (vtype_buf_size
);
6181 g_assert (vtype_buf
);
6184 err
= decode_vtype (NULL
, domain
, vtype_buf
, buf
, &buf
, limit
);
6185 if (err
!= ERR_NONE
) {
6189 *(MonoObject
**)addr
= mono_value_box_checked (d
, klass
, vtype_buf
, &error
);
6190 mono_error_cleanup (&error
);
6193 char *name
= mono_type_full_name (t
);
6194 DEBUG_PRINTF (1, "[%p] Expected value of type %s, got 0x%0x.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), name
, type
);
6196 return ERR_INVALID_ARGUMENT
;
6210 decode_value (MonoType
*t
, MonoDomain
*domain
, guint8
*addr
, guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
6214 int type
= decode_byte (buf
, &buf
, limit
);
6216 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
))) {
6217 MonoType
*targ
= t
->data
.generic_class
->context
.class_inst
->type_argv
[0];
6218 guint8
*nullable_buf
;
6221 * First try decoding it as a Nullable`1
6223 err
= decode_value_internal (t
, type
, domain
, addr
, buf
, endbuf
, limit
);
6224 if (err
== ERR_NONE
)
6228 * Then try decoding as a primitive value or null.
6230 if (targ
->type
== type
) {
6231 nullable_buf
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type (targ
)));
6232 err
= decode_value_internal (targ
, type
, domain
, nullable_buf
, buf
, endbuf
, limit
);
6233 if (err
!= ERR_NONE
) {
6234 g_free (nullable_buf
);
6237 MonoObject
*boxed
= mono_value_box_checked (domain
, mono_class_from_mono_type (targ
), nullable_buf
, &error
);
6238 if (!is_ok (&error
)) {
6239 mono_error_cleanup (&error
);
6240 return ERR_INVALID_OBJECT
;
6242 mono_nullable_init (addr
, boxed
, mono_class_from_mono_type (t
));
6243 g_free (nullable_buf
);
6246 } else if (type
== VALUE_TYPE_ID_NULL
) {
6247 mono_nullable_init (addr
, NULL
, mono_class_from_mono_type (t
));
6253 return decode_value_internal (t
, type
, domain
, addr
, buf
, endbuf
, limit
);
6257 add_var (Buffer
*buf
, MonoDebugMethodJitInfo
*jit
, MonoType
*t
, MonoDebugVarInfo
*var
, MonoContext
*ctx
, MonoDomain
*domain
, gboolean as_vtype
)
6261 guint8
*addr
, *gaddr
;
6264 flags
= var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6265 reg
= var
->index
& ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6268 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
:
6269 reg_val
= mono_arch_context_get_int_reg (ctx
, reg
);
6271 buffer_add_value_full (buf
, t
, ®_val
, domain
, as_vtype
, NULL
);
6273 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
:
6274 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6275 addr
+= (gint32
)var
->offset
;
6277 //printf ("[R%d+%d] = %p\n", reg, var->offset, addr);
6279 buffer_add_value_full (buf
, t
, addr
, domain
, as_vtype
, NULL
);
6281 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD
:
6284 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR
:
6285 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR
:
6286 /* Same as regoffset, but with an indirection */
6287 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6288 addr
+= (gint32
)var
->offset
;
6290 gaddr
= (guint8
*)*(gpointer
*)addr
;
6292 buffer_add_value_full (buf
, t
, gaddr
, domain
, as_vtype
, NULL
);
6294 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL
: {
6295 MonoDebugVarInfo
*info_var
= jit
->gsharedvt_info_var
;
6296 MonoDebugVarInfo
*locals_var
= jit
->gsharedvt_locals_var
;
6297 MonoGSharedVtMethodRuntimeInfo
*info
;
6303 g_assert (info_var
);
6304 g_assert (locals_var
);
6306 flags
= info_var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6307 reg
= info_var
->index
& ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6308 if (flags
== MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
) {
6309 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6310 addr
+= (gint32
)info_var
->offset
;
6311 info
= (MonoGSharedVtMethodRuntimeInfo
*)*(gpointer
*)addr
;
6312 } else if (flags
== MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
) {
6313 info
= (MonoGSharedVtMethodRuntimeInfo
*)mono_arch_context_get_int_reg (ctx
, reg
);
6315 g_assert_not_reached ();
6319 flags
= locals_var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6320 reg
= locals_var
->index
& ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6321 if (flags
== MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
) {
6322 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6323 addr
+= (gint32
)locals_var
->offset
;
6324 locals
= (guint8
*)*(gpointer
*)addr
;
6325 } else if (flags
== MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
) {
6326 locals
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6328 g_assert_not_reached ();
6332 addr
= locals
+ GPOINTER_TO_INT (info
->entries
[idx
]);
6334 buffer_add_value_full (buf
, t
, addr
, domain
, as_vtype
, NULL
);
6339 g_assert_not_reached ();
6344 set_var (MonoType
*t
, MonoDebugVarInfo
*var
, MonoContext
*ctx
, MonoDomain
*domain
, guint8
*val
, mgreg_t
**reg_locations
, MonoContext
*restore_ctx
)
6348 guint8
*addr
, *gaddr
;
6350 flags
= var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6351 reg
= var
->index
& ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6353 if (MONO_TYPE_IS_REFERENCE (t
))
6354 size
= sizeof (gpointer
);
6356 size
= mono_class_value_size (mono_class_from_mono_type (t
), NULL
);
6359 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
: {
6360 #ifdef MONO_ARCH_HAVE_CONTEXT_SET_INT_REG
6362 gboolean is_signed
= FALSE
;
6365 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6368 // FIXME: Write barriers
6369 mono_gc_memmove_atomic (addr
, val
, size
);
6374 if (!t
->byref
&& (t
->type
== MONO_TYPE_I1
|| t
->type
== MONO_TYPE_I2
|| t
->type
== MONO_TYPE_I4
|| t
->type
== MONO_TYPE_I8
))
6379 v
= is_signed
? *(gint8
*)val
: *(guint8
*)val
;
6382 v
= is_signed
? *(gint16
*)val
: *(guint16
*)val
;
6385 v
= is_signed
? *(gint32
*)val
: *(guint32
*)val
;
6388 v
= is_signed
? *(gint64
*)val
: *(guint64
*)val
;
6391 g_assert_not_reached ();
6394 /* Set value on the stack or in the return ctx */
6395 if (reg_locations
[reg
]) {
6396 /* Saved on the stack */
6397 DEBUG_PRINTF (1, "[dbg] Setting stack location %p for reg %x to %p.\n", reg_locations
[reg
], reg
, (gpointer
)v
);
6398 *(reg_locations
[reg
]) = v
;
6401 DEBUG_PRINTF (1, "[dbg] Setting context location for reg %x to %p.\n", reg
, (gpointer
)v
);
6402 mono_arch_context_set_int_reg (restore_ctx
, reg
, v
);
6405 // FIXME: Move these to mono-context.h/c.
6406 mono_arch_context_set_int_reg (ctx
, reg
, v
);
6408 // FIXME: Can't set registers, so we disable linears
6413 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
:
6414 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6415 addr
+= (gint32
)var
->offset
;
6417 //printf ("[R%d+%d] = %p\n", reg, var->offset, addr);
6420 addr
= *(guint8
**)addr
;
6426 // FIXME: Write barriers
6427 mono_gc_memmove_atomic (addr
, val
, size
);
6429 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR
:
6430 /* Same as regoffset, but with an indirection */
6431 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6432 addr
+= (gint32
)var
->offset
;
6434 gaddr
= (guint8
*)*(gpointer
*)addr
;
6436 // FIXME: Write barriers
6437 mono_gc_memmove_atomic (gaddr
, val
, size
);
6439 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD
:
6443 g_assert_not_reached ();
6448 clear_event_request (int req_id
, int etype
)
6452 mono_loader_lock ();
6453 for (i
= 0; i
< event_requests
->len
; ++i
) {
6454 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
6456 if (req
->id
== req_id
&& req
->event_kind
== etype
) {
6457 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
)
6458 clear_breakpoint ((MonoBreakpoint
*)req
->info
);
6459 if (req
->event_kind
== EVENT_KIND_STEP
)
6460 ss_destroy ((SingleStepReq
*)req
->info
);
6461 if (req
->event_kind
== EVENT_KIND_METHOD_ENTRY
)
6462 clear_breakpoint ((MonoBreakpoint
*)req
->info
);
6463 if (req
->event_kind
== EVENT_KIND_METHOD_EXIT
)
6464 clear_breakpoint ((MonoBreakpoint
*)req
->info
);
6465 g_ptr_array_remove_index_fast (event_requests
, i
);
6470 mono_loader_unlock ();
6474 clear_assembly_from_modifier (EventRequest
*req
, Modifier
*m
, MonoAssembly
*assembly
)
6478 if (m
->kind
== MOD_KIND_EXCEPTION_ONLY
&& m
->data
.exc_class
&& m
->data
.exc_class
->image
->assembly
== assembly
)
6479 m
->kind
= MOD_KIND_NONE
;
6480 if (m
->kind
== MOD_KIND_ASSEMBLY_ONLY
&& m
->data
.assemblies
) {
6481 int count
= 0, match_count
= 0, pos
;
6482 MonoAssembly
**newassemblies
;
6484 for (i
= 0; m
->data
.assemblies
[i
]; ++i
) {
6486 if (m
->data
.assemblies
[i
] == assembly
)
6491 newassemblies
= g_new0 (MonoAssembly
*, count
- match_count
);
6494 for (i
= 0; i
< count
; ++i
)
6495 if (m
->data
.assemblies
[i
] != assembly
)
6496 newassemblies
[pos
++] = m
->data
.assemblies
[i
];
6497 g_assert (pos
== count
- match_count
);
6498 g_free (m
->data
.assemblies
);
6499 m
->data
.assemblies
= newassemblies
;
6505 clear_assembly_from_modifiers (EventRequest
*req
, MonoAssembly
*assembly
)
6509 for (i
= 0; i
< req
->nmodifiers
; ++i
) {
6510 Modifier
*m
= &req
->modifiers
[i
];
6512 clear_assembly_from_modifier (req
, m
, assembly
);
6517 * clear_event_requests_for_assembly:
6519 * Clear all events requests which reference ASSEMBLY.
6522 clear_event_requests_for_assembly (MonoAssembly
*assembly
)
6527 mono_loader_lock ();
6531 for (i
= 0; i
< event_requests
->len
; ++i
) {
6532 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
6534 clear_assembly_from_modifiers (req
, assembly
);
6536 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
&& breakpoint_matches_assembly ((MonoBreakpoint
*)req
->info
, assembly
)) {
6537 clear_event_request (req
->id
, req
->event_kind
);
6542 if (req
->event_kind
== EVENT_KIND_STEP
)
6543 ss_clear_for_assembly ((SingleStepReq
*)req
->info
, assembly
);
6546 mono_loader_unlock ();
6550 * type_comes_from_assembly:
6552 * GHRFunc that returns TRUE if klass comes from assembly
6555 type_comes_from_assembly (gpointer klass
, gpointer also_klass
, gpointer assembly
)
6557 return (mono_class_get_image ((MonoClass
*)klass
) == mono_assembly_get_image ((MonoAssembly
*)assembly
));
6561 * clear_types_for_assembly:
6563 * Clears types from loaded_classes for a given assembly
6566 clear_types_for_assembly (MonoAssembly
*assembly
)
6568 MonoDomain
*domain
= mono_domain_get ();
6569 AgentDomainInfo
*info
= NULL
;
6571 if (!domain
|| !domain_jit_info (domain
))
6572 /* Can happen during shutdown */
6575 info
= get_agent_domain_info (domain
);
6577 mono_loader_lock ();
6578 g_hash_table_foreach_remove (info
->loaded_classes
, type_comes_from_assembly
, assembly
);
6579 mono_loader_unlock ();
6583 add_thread (gpointer key
, gpointer value
, gpointer user_data
)
6585 MonoInternalThread
*thread
= (MonoInternalThread
*)value
;
6586 Buffer
*buf
= (Buffer
*)user_data
;
6588 buffer_add_objid (buf
, (MonoObject
*)thread
);
6592 do_invoke_method (DebuggerTlsData
*tls
, Buffer
*buf
, InvokeData
*invoke
, guint8
*p
, guint8
**endp
)
6595 guint8
*end
= invoke
->endp
;
6599 MonoMethodSignature
*sig
;
6602 MonoObject
*this_arg
, *res
, *exc
;
6605 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6608 MonoStopwatch watch
;
6610 if (invoke
->method
) {
6612 * Invoke this method directly, currently only Environment.Exit () is supported.
6615 DEBUG_PRINTF (1, "[%p] Invoking method '%s' on receiver '%s'.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), mono_method_full_name (invoke
->method
, TRUE
), this_arg
? this_arg
->vtable
->klass
->name
: "<null>");
6617 mono_runtime_try_invoke (invoke
->method
, NULL
, invoke
->args
, &exc
, &error
);
6618 mono_error_assert_ok (&error
);
6620 g_assert_not_reached ();
6623 m
= decode_methodid (p
, &p
, end
, &domain
, &err
);
6624 if (err
!= ERR_NONE
)
6626 sig
= mono_method_signature (m
);
6628 if (m
->klass
->valuetype
)
6629 this_buf
= (guint8
*)g_alloca (mono_class_instance_size (m
->klass
));
6631 this_buf
= (guint8
*)g_alloca (sizeof (MonoObject
*));
6632 if (m
->klass
->valuetype
&& (m
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
6633 /* Should be null */
6634 int type
= decode_byte (p
, &p
, end
);
6635 if (type
!= VALUE_TYPE_ID_NULL
) {
6636 DEBUG_PRINTF (1, "[%p] Error: Static vtype method invoked with this argument.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
6637 return ERR_INVALID_ARGUMENT
;
6639 memset (this_buf
, 0, mono_class_instance_size (m
->klass
));
6640 } else if (m
->klass
->valuetype
&& !strcmp (m
->name
, ".ctor")) {
6644 int type
= decode_byte (p
, &tmp_p
, end
);
6645 if (type
== VALUE_TYPE_ID_NULL
) {
6646 memset (this_buf
, 0, mono_class_instance_size (m
->klass
));
6649 err
= decode_value (&m
->klass
->byval_arg
, domain
, this_buf
, p
, &p
, end
);
6650 if (err
!= ERR_NONE
)
6654 err
= decode_value (&m
->klass
->byval_arg
, domain
, this_buf
, p
, &p
, end
);
6655 if (err
!= ERR_NONE
)
6659 if (!m
->klass
->valuetype
)
6660 this_arg
= *(MonoObject
**)this_buf
;
6664 if (MONO_CLASS_IS_INTERFACE (m
->klass
)) {
6666 DEBUG_PRINTF (1, "[%p] Error: Interface method invoked without this argument.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
6667 return ERR_INVALID_ARGUMENT
;
6669 m
= mono_object_get_virtual_method (this_arg
, m
);
6670 /* Transform this to the format the rest of the code expects it to be */
6671 if (m
->klass
->valuetype
) {
6672 this_buf
= (guint8
*)g_alloca (mono_class_instance_size (m
->klass
));
6673 memcpy (this_buf
, mono_object_unbox (this_arg
), mono_class_instance_size (m
->klass
));
6675 } else if ((m
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !m
->klass
->valuetype
&& invoke
->flags
& INVOKE_FLAG_VIRTUAL
) {
6677 DEBUG_PRINTF (1, "[%p] Error: invoke with INVOKE_FLAG_VIRTUAL flag set without this argument.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
6678 return ERR_INVALID_ARGUMENT
;
6680 m
= mono_object_get_virtual_method (this_arg
, m
);
6681 if (m
->klass
->valuetype
) {
6682 this_buf
= (guint8
*)g_alloca (mono_class_instance_size (m
->klass
));
6683 memcpy (this_buf
, mono_object_unbox (this_arg
), mono_class_instance_size (m
->klass
));
6687 DEBUG_PRINTF (1, "[%p] Invoking method '%s' on receiver '%s'.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), mono_method_full_name (m
, TRUE
), this_arg
? this_arg
->vtable
->klass
->name
: "<null>");
6689 if (this_arg
&& this_arg
->vtable
->domain
!= domain
)
6692 if (!m
->klass
->valuetype
&& !(m
->flags
& METHOD_ATTRIBUTE_STATIC
) && !this_arg
) {
6693 if (!strcmp (m
->name
, ".ctor")) {
6694 if (m
->klass
->flags
& TYPE_ATTRIBUTE_ABSTRACT
)
6695 return ERR_INVALID_ARGUMENT
;
6698 this_arg
= mono_object_new_checked (domain
, m
->klass
, &error
);
6699 mono_error_assert_ok (&error
);
6702 return ERR_INVALID_ARGUMENT
;
6706 if (this_arg
&& !obj_is_of_type (this_arg
, &m
->klass
->byval_arg
))
6707 return ERR_INVALID_ARGUMENT
;
6709 nargs
= decode_int (p
, &p
, end
);
6710 if (nargs
!= sig
->param_count
)
6711 return ERR_INVALID_ARGUMENT
;
6712 /* Use alloca to get gc tracking */
6713 arg_buf
= (guint8
**)g_alloca (nargs
* sizeof (gpointer
));
6714 memset (arg_buf
, 0, nargs
* sizeof (gpointer
));
6715 args
= (gpointer
*)g_alloca (nargs
* sizeof (gpointer
));
6716 for (i
= 0; i
< nargs
; ++i
) {
6717 if (MONO_TYPE_IS_REFERENCE (sig
->params
[i
])) {
6718 err
= decode_value (sig
->params
[i
], domain
, (guint8
*)&args
[i
], p
, &p
, end
);
6719 if (err
!= ERR_NONE
)
6721 if (args
[i
] && ((MonoObject
*)args
[i
])->vtable
->domain
!= domain
)
6724 if (sig
->params
[i
]->byref
) {
6725 arg_buf
[i
] = (guint8
*)g_alloca (sizeof (mgreg_t
));
6726 *(gpointer
*)arg_buf
[i
] = args
[i
];
6727 args
[i
] = arg_buf
[i
];
6730 arg_buf
[i
] = (guint8
*)g_alloca (mono_class_instance_size (mono_class_from_mono_type (sig
->params
[i
])));
6731 err
= decode_value (sig
->params
[i
], domain
, arg_buf
[i
], p
, &p
, end
);
6732 if (err
!= ERR_NONE
)
6734 args
[i
] = arg_buf
[i
];
6741 if (invoke
->flags
& INVOKE_FLAG_DISABLE_BREAKPOINTS
)
6742 tls
->disable_breakpoints
= TRUE
;
6744 tls
->disable_breakpoints
= FALSE
;
6747 * Add an LMF frame to link the stack frames on the invoke method with our caller.
6749 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6750 if (invoke
->has_ctx
) {
6753 lmf_addr
= mono_get_lmf_addr ();
6756 memset (&ext
, 0, sizeof (ext
));
6757 mono_arch_init_lmf_ext (&ext
, *lmf_addr
);
6759 ext
.debugger_invoke
= TRUE
;
6760 memcpy (&ext
.ctx
, &invoke
->ctx
, sizeof (MonoContext
));
6762 mono_set_lmf ((MonoLMF
*)&ext
);
6766 mono_stopwatch_start (&watch
);
6767 res
= mono_runtime_try_invoke (m
, m
->klass
->valuetype
? (gpointer
) this_buf
: (gpointer
) this_arg
, args
, &exc
, &error
);
6768 if (exc
== NULL
&& !mono_error_ok (&error
)) {
6769 exc
= (MonoObject
*) mono_error_convert_to_exception (&error
);
6771 mono_error_cleanup (&error
); /* FIXME report error */
6773 mono_stopwatch_stop (&watch
);
6774 DEBUG_PRINTF (1, "[%p] Invoke result: %p, exc: %s, time: %ld ms.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), res
, exc
? exc
->vtable
->klass
->name
: NULL
, (long)mono_stopwatch_elapsed_ms (&watch
));
6776 buffer_add_byte (buf
, 0);
6777 buffer_add_value (buf
, &mono_defaults
.object_class
->byval_arg
, &exc
, domain
);
6779 gboolean out_this
= FALSE
;
6780 gboolean out_args
= FALSE
;
6782 if ((invoke
->flags
& INVOKE_FLAG_RETURN_OUT_THIS
) && CHECK_PROTOCOL_VERSION (2, 35))
6784 if ((invoke
->flags
& INVOKE_FLAG_RETURN_OUT_ARGS
) && CHECK_PROTOCOL_VERSION (2, 35))
6786 buffer_add_byte (buf
, 1 + (out_this
? 2 : 0) + (out_args
? 4 : 0));
6787 if (sig
->ret
->type
== MONO_TYPE_VOID
) {
6788 if (!strcmp (m
->name
, ".ctor")) {
6789 if (!m
->klass
->valuetype
)
6790 buffer_add_value (buf
, &mono_defaults
.object_class
->byval_arg
, &this_arg
, domain
);
6792 buffer_add_value (buf
, &m
->klass
->byval_arg
, this_buf
, domain
);
6794 buffer_add_value (buf
, &mono_defaults
.void_class
->byval_arg
, NULL
, domain
);
6796 } else if (MONO_TYPE_IS_REFERENCE (sig
->ret
)) {
6797 buffer_add_value (buf
, sig
->ret
, &res
, domain
);
6798 } else if (mono_class_from_mono_type (sig
->ret
)->valuetype
|| sig
->ret
->type
== MONO_TYPE_PTR
|| sig
->ret
->type
== MONO_TYPE_FNPTR
) {
6799 if (mono_class_is_nullable (mono_class_from_mono_type (sig
->ret
))) {
6800 MonoClass
*k
= mono_class_from_mono_type (sig
->ret
);
6801 guint8
*nullable_buf
= (guint8
*)g_alloca (mono_class_value_size (k
, NULL
));
6803 g_assert (nullable_buf
);
6804 mono_nullable_init (nullable_buf
, res
, k
);
6805 buffer_add_value (buf
, sig
->ret
, nullable_buf
, domain
);
6808 buffer_add_value (buf
, sig
->ret
, mono_object_unbox (res
), domain
);
6814 /* Return the new value of the receiver after the call */
6815 buffer_add_value (buf
, &m
->klass
->byval_arg
, this_buf
, domain
);
6817 buffer_add_int (buf
, nargs
);
6818 for (i
= 0; i
< nargs
; ++i
) {
6819 if (MONO_TYPE_IS_REFERENCE (sig
->params
[i
]))
6820 buffer_add_value (buf
, sig
->params
[i
], &args
[i
], domain
);
6821 else if (sig
->params
[i
]->byref
)
6822 /* add_value () does an indirection */
6823 buffer_add_value (buf
, sig
->params
[i
], &arg_buf
[i
], domain
);
6825 buffer_add_value (buf
, sig
->params
[i
], arg_buf
[i
], domain
);
6830 tls
->disable_breakpoints
= FALSE
;
6832 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6833 if (invoke
->has_ctx
)
6834 mono_set_lmf ((MonoLMF
*)(((gssize
)ext
.lmf
.previous_lmf
) & ~3));
6838 // FIXME: byref arguments
6846 * Invoke the method given by tls->pending_invoke in the current thread.
6849 invoke_method (void)
6851 DebuggerTlsData
*tls
;
6857 MonoContext restore_ctx
;
6860 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
6864 * Store the `InvokeData *' in `tls->invoke' until we're done with
6865 * the invocation, so CMD_VM_ABORT_INVOKE can check it.
6868 mono_loader_lock ();
6870 invoke
= tls
->pending_invoke
;
6872 tls
->pending_invoke
= NULL
;
6874 invoke
->last_invoke
= tls
->invoke
;
6875 tls
->invoke
= invoke
;
6877 mono_loader_unlock ();
6879 tls
->frames_up_to_date
= FALSE
;
6885 for (mindex
= 0; mindex
< invoke
->nmethods
; ++mindex
) {
6886 buffer_init (&buf
, 128);
6889 /* Fail the other invokes as well */
6891 err
= do_invoke_method (tls
, &buf
, invoke
, p
, &p
);
6894 if (tls
->abort_requested
) {
6895 if (CHECK_PROTOCOL_VERSION (2, 42))
6896 err
= ERR_INVOKE_ABORTED
;
6899 /* Start suspending before sending the reply */
6900 if (mindex
== invoke
->nmethods
- 1) {
6901 if (!(invoke
->flags
& INVOKE_FLAG_SINGLE_THREADED
)) {
6902 for (i
= 0; i
< invoke
->suspend_count
; ++i
)
6907 send_reply_packet (id
, err
, &buf
);
6912 memcpy (&restore_ctx
, &invoke
->ctx
, sizeof (MonoContext
));
6914 if (invoke
->has_ctx
)
6915 save_thread_context (&restore_ctx
);
6917 if (invoke
->flags
& INVOKE_FLAG_SINGLE_THREADED
) {
6918 g_assert (tls
->resume_count
);
6919 tls
->resume_count
-= invoke
->suspend_count
;
6922 DEBUG_PRINTF (1, "[%p] Invoke finished (%d), resume_count = %d.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), err
, tls
->resume_count
);
6925 * Take the loader lock to avoid race conditions with CMD_VM_ABORT_INVOKE:
6927 * It is possible that mono_thread_internal_abort () was called
6928 * after the mono_runtime_invoke_checked() already returned, but it doesn't matter
6929 * because we reset the abort here.
6932 mono_loader_lock ();
6934 if (tls
->abort_requested
)
6935 mono_thread_internal_reset_abort (tls
->thread
);
6937 tls
->invoke
= tls
->invoke
->last_invoke
;
6938 tls
->abort_requested
= FALSE
;
6940 mono_loader_unlock ();
6949 is_really_suspended (gpointer key
, gpointer value
, gpointer user_data
)
6951 MonoThread
*thread
= (MonoThread
*)value
;
6952 DebuggerTlsData
*tls
;
6955 mono_loader_lock ();
6956 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
6958 res
= tls
->really_suspended
;
6959 mono_loader_unlock ();
6965 get_source_files_for_type (MonoClass
*klass
)
6967 gpointer iter
= NULL
;
6969 MonoDebugSourceInfo
*sinfo
;
6973 files
= g_ptr_array_new ();
6975 while ((method
= mono_class_get_methods (klass
, &iter
))) {
6976 MonoDebugMethodInfo
*minfo
= mono_debug_lookup_method (method
);
6977 GPtrArray
*source_file_list
;
6980 mono_debug_get_seq_points (minfo
, NULL
, &source_file_list
, NULL
, NULL
, NULL
);
6981 for (j
= 0; j
< source_file_list
->len
; ++j
) {
6982 sinfo
= (MonoDebugSourceInfo
*)g_ptr_array_index (source_file_list
, j
);
6983 for (i
= 0; i
< files
->len
; ++i
)
6984 if (!strcmp (g_ptr_array_index (files
, i
), sinfo
->source_file
))
6986 if (i
== files
->len
)
6987 g_ptr_array_add (files
, g_strdup (sinfo
->source_file
));
6989 g_ptr_array_free (source_file_list
, TRUE
);
6997 vm_commands (int command
, int id
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7000 case CMD_VM_VERSION
: {
7001 char *build_info
, *version
;
7003 build_info
= mono_get_runtime_build_info ();
7004 version
= g_strdup_printf ("mono %s", build_info
);
7006 buffer_add_string (buf
, version
); /* vm version */
7007 buffer_add_int (buf
, MAJOR_VERSION
);
7008 buffer_add_int (buf
, MINOR_VERSION
);
7009 g_free (build_info
);
7013 case CMD_VM_SET_PROTOCOL_VERSION
: {
7014 major_version
= decode_int (p
, &p
, end
);
7015 minor_version
= decode_int (p
, &p
, end
);
7016 protocol_version_set
= TRUE
;
7017 DEBUG_PRINTF (1, "[dbg] Protocol version %d.%d, client protocol version %d.%d.\n", MAJOR_VERSION
, MINOR_VERSION
, major_version
, minor_version
);
7020 case CMD_VM_ALL_THREADS
: {
7022 mono_loader_lock ();
7023 buffer_add_int (buf
, mono_g_hash_table_size (tid_to_thread_obj
));
7024 mono_g_hash_table_foreach (tid_to_thread_obj
, add_thread
, buf
);
7025 mono_loader_unlock ();
7028 case CMD_VM_SUSPEND
:
7030 wait_for_suspend ();
7033 if (suspend_count
== 0)
7034 return ERR_NOT_SUSPENDED
;
7036 clear_suspended_objs ();
7038 case CMD_VM_DISPOSE
:
7039 /* Clear all event requests */
7040 mono_loader_lock ();
7041 while (event_requests
->len
> 0) {
7042 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, 0);
7044 clear_event_request (req
->id
, req
->event_kind
);
7046 mono_loader_unlock ();
7048 while (suspend_count
> 0)
7050 disconnected
= TRUE
;
7051 vm_start_event_sent
= FALSE
;
7054 MonoInternalThread
*thread
;
7055 DebuggerTlsData
*tls
;
7056 #ifdef TRY_MANAGED_SYSTEM_ENVIRONMENT_EXIT
7057 MonoClass
*env_class
;
7059 MonoMethod
*exit_method
= NULL
;
7063 exit_code
= decode_int (p
, &p
, end
);
7065 // FIXME: What if there is a VM_DEATH event request with SUSPEND_ALL ?
7067 /* Have to send a reply before exiting */
7068 send_reply_packet (id
, 0, buf
);
7070 /* Clear all event requests */
7071 mono_loader_lock ();
7072 while (event_requests
->len
> 0) {
7073 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, 0);
7075 clear_event_request (req
->id
, req
->event_kind
);
7077 mono_loader_unlock ();
7080 * The JDWP documentation says that the shutdown is not orderly. It doesn't
7081 * specify whenever a VM_DEATH event is sent. We currently do an orderly
7082 * shutdown by hijacking a thread to execute Environment.Exit (). This is
7083 * better than doing the shutdown ourselves, since it avoids various races.
7087 wait_for_suspend ();
7089 #ifdef TRY_MANAGED_SYSTEM_ENVIRONMENT_EXIT
7090 env_class
= mono_class_try_load_from_name (mono_defaults
.corlib
, "System", "Environment");
7092 exit_method
= mono_class_get_method_from_name (env_class
, "Exit", 1);
7095 mono_loader_lock ();
7096 thread
= (MonoInternalThread
*)mono_g_hash_table_find (tid_to_thread
, is_really_suspended
, NULL
);
7097 mono_loader_unlock ();
7099 if (thread
&& exit_method
) {
7100 mono_loader_lock ();
7101 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
7102 mono_loader_unlock ();
7104 args
= g_new0 (gpointer
, 1);
7105 args
[0] = g_malloc (sizeof (int));
7106 *(int*)(args
[0]) = exit_code
;
7108 tls
->pending_invoke
= g_new0 (InvokeData
, 1);
7109 tls
->pending_invoke
->method
= exit_method
;
7110 tls
->pending_invoke
->args
= args
;
7111 tls
->pending_invoke
->nmethods
= 1;
7113 while (suspend_count
> 0)
7117 * No thread found, do it ourselves.
7118 * FIXME: This can race with normal shutdown etc.
7120 while (suspend_count
> 0)
7123 if (!mono_runtime_try_shutdown ())
7126 mono_environment_exitcode_set (exit_code
);
7128 /* Suspend all managed threads since the runtime is going away */
7129 DEBUG_PRINTF (1, "Suspending all threads...\n");
7130 mono_thread_suspend_all_other_threads ();
7131 DEBUG_PRINTF (1, "Shutting down the runtime...\n");
7132 mono_runtime_quit ();
7133 transport_close2 ();
7134 DEBUG_PRINTF (1, "Exiting...\n");
7140 case CMD_VM_INVOKE_METHOD
:
7141 case CMD_VM_INVOKE_METHODS
: {
7142 int objid
= decode_objid (p
, &p
, end
);
7144 DebuggerTlsData
*tls
;
7145 int i
, count
, flags
, nmethods
;
7148 err
= get_object (objid
, (MonoObject
**)&thread
);
7149 if (err
!= ERR_NONE
)
7152 flags
= decode_int (p
, &p
, end
);
7154 if (command
== CMD_VM_INVOKE_METHODS
)
7155 nmethods
= decode_int (p
, &p
, end
);
7159 // Wait for suspending if it already started
7161 wait_for_suspend ();
7162 if (!is_suspended ())
7163 return ERR_NOT_SUSPENDED
;
7165 mono_loader_lock ();
7166 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, THREAD_TO_INTERNAL (thread
));
7167 mono_loader_unlock ();
7170 if (!tls
->really_suspended
)
7171 /* The thread is still running native code, can't do invokes */
7172 return ERR_NOT_SUSPENDED
;
7175 * Store the invoke data into tls, the thread will execute it after it is
7178 if (tls
->pending_invoke
)
7179 return ERR_NOT_SUSPENDED
;
7180 tls
->pending_invoke
= g_new0 (InvokeData
, 1);
7181 tls
->pending_invoke
->id
= id
;
7182 tls
->pending_invoke
->flags
= flags
;
7183 tls
->pending_invoke
->p
= (guint8
*)g_malloc (end
- p
);
7184 memcpy (tls
->pending_invoke
->p
, p
, end
- p
);
7185 tls
->pending_invoke
->endp
= tls
->pending_invoke
->p
+ (end
- p
);
7186 tls
->pending_invoke
->suspend_count
= suspend_count
;
7187 tls
->pending_invoke
->nmethods
= nmethods
;
7189 if (flags
& INVOKE_FLAG_SINGLE_THREADED
) {
7190 resume_thread (THREAD_TO_INTERNAL (thread
));
7193 count
= suspend_count
;
7194 for (i
= 0; i
< count
; ++i
)
7199 case CMD_VM_ABORT_INVOKE
: {
7200 int objid
= decode_objid (p
, &p
, end
);
7202 DebuggerTlsData
*tls
;
7206 err
= get_object (objid
, (MonoObject
**)&thread
);
7207 if (err
!= ERR_NONE
)
7210 invoke_id
= decode_int (p
, &p
, end
);
7212 mono_loader_lock ();
7213 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, THREAD_TO_INTERNAL (thread
));
7216 if (tls
->abort_requested
) {
7217 DEBUG_PRINTF (1, "Abort already requested.\n");
7218 mono_loader_unlock ();
7223 * Check whether we're still inside the mono_runtime_invoke_checked() and that it's
7224 * actually the correct invocation.
7226 * Careful, we do not stop the thread that's doing the invocation, so we can't
7227 * inspect its stack. However, invoke_method() also acquires the loader lock
7228 * when it's done, so we're safe here.
7232 if (!tls
->invoke
|| (tls
->invoke
->id
!= invoke_id
)) {
7233 mono_loader_unlock ();
7234 return ERR_NO_INVOCATION
;
7237 tls
->abort_requested
= TRUE
;
7239 mono_thread_internal_abort (THREAD_TO_INTERNAL (thread
));
7240 mono_loader_unlock ();
7244 case CMD_VM_SET_KEEPALIVE
: {
7245 int timeout
= decode_int (p
, &p
, end
);
7246 agent_config
.keepalive
= timeout
;
7248 #ifndef DISABLE_SOCKET_TRANSPORT
7255 case CMD_VM_GET_TYPES_FOR_SOURCE_FILE
: {
7256 GHashTableIter iter
, kiter
;
7261 char *fname
, *basename
;
7262 gboolean ignore_case
;
7263 GSList
*class_list
, *l
;
7264 GPtrArray
*res_classes
, *res_domains
;
7266 fname
= decode_string (p
, &p
, end
);
7267 ignore_case
= decode_byte (p
, &p
, end
);
7269 basename
= dbg_path_get_basename (fname
);
7271 res_classes
= g_ptr_array_new ();
7272 res_domains
= g_ptr_array_new ();
7274 mono_loader_lock ();
7275 g_hash_table_iter_init (&iter
, domains
);
7276 while (g_hash_table_iter_next (&iter
, NULL
, (void**)&domain
)) {
7277 AgentDomainInfo
*info
= (AgentDomainInfo
*)domain_jit_info (domain
)->agent_info
;
7279 /* Update 'source_file_to_class' cache */
7280 g_hash_table_iter_init (&kiter
, info
->loaded_classes
);
7281 while (g_hash_table_iter_next (&kiter
, NULL
, (void**)&klass
)) {
7282 if (!g_hash_table_lookup (info
->source_files
, klass
)) {
7283 files
= get_source_files_for_type (klass
);
7284 g_hash_table_insert (info
->source_files
, klass
, files
);
7286 for (i
= 0; i
< files
->len
; ++i
) {
7287 char *s
= (char *)g_ptr_array_index (files
, i
);
7288 char *s2
= dbg_path_get_basename (s
);
7291 class_list
= (GSList
*)g_hash_table_lookup (info
->source_file_to_class
, s2
);
7293 class_list
= g_slist_prepend (class_list
, klass
);
7294 g_hash_table_insert (info
->source_file_to_class
, g_strdup (s2
), class_list
);
7296 class_list
= g_slist_prepend (class_list
, klass
);
7297 g_hash_table_insert (info
->source_file_to_class
, s2
, class_list
);
7300 /* The _ignorecase hash contains the lowercase path */
7301 s3
= strdup_tolower (s2
);
7302 class_list
= (GSList
*)g_hash_table_lookup (info
->source_file_to_class_ignorecase
, s3
);
7304 class_list
= g_slist_prepend (class_list
, klass
);
7305 g_hash_table_insert (info
->source_file_to_class_ignorecase
, g_strdup (s3
), class_list
);
7307 class_list
= g_slist_prepend (class_list
, klass
);
7308 g_hash_table_insert (info
->source_file_to_class_ignorecase
, s3
, class_list
);
7320 s
= strdup_tolower (basename
);
7321 class_list
= (GSList
*)g_hash_table_lookup (info
->source_file_to_class_ignorecase
, s
);
7324 class_list
= (GSList
*)g_hash_table_lookup (info
->source_file_to_class
, basename
);
7327 for (l
= class_list
; l
; l
= l
->next
) {
7328 klass
= (MonoClass
*)l
->data
;
7330 g_ptr_array_add (res_classes
, klass
);
7331 g_ptr_array_add (res_domains
, domain
);
7334 mono_loader_unlock ();
7339 buffer_add_int (buf
, res_classes
->len
);
7340 for (i
= 0; i
< res_classes
->len
; ++i
)
7341 buffer_add_typeid (buf
, (MonoDomain
*)g_ptr_array_index (res_domains
, i
), (MonoClass
*)g_ptr_array_index (res_classes
, i
));
7342 g_ptr_array_free (res_classes
, TRUE
);
7343 g_ptr_array_free (res_domains
, TRUE
);
7346 case CMD_VM_GET_TYPES
: {
7347 GHashTableIter iter
;
7351 gboolean ignore_case
;
7352 GPtrArray
*res_classes
, *res_domains
;
7353 MonoTypeNameParse info
;
7355 name
= decode_string (p
, &p
, end
);
7356 ignore_case
= decode_byte (p
, &p
, end
);
7358 if (!mono_reflection_parse_type (name
, &info
)) {
7360 mono_reflection_free_type_info (&info
);
7361 return ERR_INVALID_ARGUMENT
;
7364 res_classes
= g_ptr_array_new ();
7365 res_domains
= g_ptr_array_new ();
7367 mono_loader_lock ();
7368 g_hash_table_iter_init (&iter
, domains
);
7369 while (g_hash_table_iter_next (&iter
, NULL
, (void**)&domain
)) {
7371 gboolean type_resolve
;
7375 mono_domain_assemblies_lock (domain
);
7376 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
7377 ass
= (MonoAssembly
*)tmp
->data
;
7381 type_resolve
= TRUE
;
7382 /* FIXME really okay to call while holding locks? */
7383 t
= mono_reflection_get_type_checked (ass
->image
, ass
->image
, &info
, ignore_case
, &type_resolve
, &error
);
7384 mono_error_cleanup (&error
);
7386 g_ptr_array_add (res_classes
, mono_type_get_class (t
));
7387 g_ptr_array_add (res_domains
, domain
);
7391 mono_domain_assemblies_unlock (domain
);
7393 mono_loader_unlock ();
7396 mono_reflection_free_type_info (&info
);
7398 buffer_add_int (buf
, res_classes
->len
);
7399 for (i
= 0; i
< res_classes
->len
; ++i
)
7400 buffer_add_typeid (buf
, (MonoDomain
*)g_ptr_array_index (res_domains
, i
), (MonoClass
*)g_ptr_array_index (res_classes
, i
));
7401 g_ptr_array_free (res_classes
, TRUE
);
7402 g_ptr_array_free (res_domains
, TRUE
);
7405 case CMD_VM_START_BUFFERING
:
7406 case CMD_VM_STOP_BUFFERING
:
7407 /* Handled in the main loop */
7410 return ERR_NOT_IMPLEMENTED
;
7417 event_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7423 case CMD_EVENT_REQUEST_SET
: {
7425 int i
, event_kind
, suspend_policy
, nmodifiers
;
7429 MonoThread
*step_thread
;
7430 int step_thread_id
= 0;
7431 StepDepth depth
= STEP_DEPTH_INTO
;
7432 StepSize size
= STEP_SIZE_MIN
;
7433 StepFilter filter
= STEP_FILTER_NONE
;
7437 event_kind
= decode_byte (p
, &p
, end
);
7438 suspend_policy
= decode_byte (p
, &p
, end
);
7439 nmodifiers
= decode_byte (p
, &p
, end
);
7441 req
= (EventRequest
*)g_malloc0 (sizeof (EventRequest
) + (nmodifiers
* sizeof (Modifier
)));
7442 req
->id
= InterlockedIncrement (&event_request_id
);
7443 req
->event_kind
= event_kind
;
7444 req
->suspend_policy
= suspend_policy
;
7445 req
->nmodifiers
= nmodifiers
;
7448 for (i
= 0; i
< nmodifiers
; ++i
) {
7449 mod
= (ModifierKind
)decode_byte (p
, &p
, end
);
7451 req
->modifiers
[i
].kind
= mod
;
7452 if (mod
== MOD_KIND_COUNT
) {
7453 req
->modifiers
[i
].data
.count
= decode_int (p
, &p
, end
);
7454 } else if (mod
== MOD_KIND_LOCATION_ONLY
) {
7455 method
= decode_methodid (p
, &p
, end
, &domain
, &err
);
7456 if (err
!= ERR_NONE
)
7458 location
= decode_long (p
, &p
, end
);
7459 } else if (mod
== MOD_KIND_STEP
) {
7460 step_thread_id
= decode_id (p
, &p
, end
);
7461 size
= (StepSize
)decode_int (p
, &p
, end
);
7462 depth
= (StepDepth
)decode_int (p
, &p
, end
);
7463 if (CHECK_PROTOCOL_VERSION (2, 16))
7464 filter
= (StepFilter
)decode_int (p
, &p
, end
);
7465 req
->modifiers
[i
].data
.filter
= filter
;
7466 if (!CHECK_PROTOCOL_VERSION (2, 26) && (req
->modifiers
[i
].data
.filter
& STEP_FILTER_DEBUGGER_HIDDEN
))
7467 /* Treat STEP_THOUGH the same as HIDDEN */
7468 req
->modifiers
[i
].data
.filter
= (StepFilter
)(req
->modifiers
[i
].data
.filter
| STEP_FILTER_DEBUGGER_STEP_THROUGH
);
7469 } else if (mod
== MOD_KIND_THREAD_ONLY
) {
7470 int id
= decode_id (p
, &p
, end
);
7472 err
= get_object (id
, (MonoObject
**)&req
->modifiers
[i
].data
.thread
);
7473 if (err
!= ERR_NONE
) {
7477 } else if (mod
== MOD_KIND_EXCEPTION_ONLY
) {
7478 MonoClass
*exc_class
= decode_typeid (p
, &p
, end
, &domain
, &err
);
7480 if (err
!= ERR_NONE
)
7482 req
->modifiers
[i
].caught
= decode_byte (p
, &p
, end
);
7483 req
->modifiers
[i
].uncaught
= decode_byte (p
, &p
, end
);
7484 if (CHECK_PROTOCOL_VERSION (2, 25))
7485 req
->modifiers
[i
].subclasses
= decode_byte (p
, &p
, end
);
7487 req
->modifiers
[i
].subclasses
= TRUE
;
7488 DEBUG_PRINTF (1, "[dbg] \tEXCEPTION_ONLY filter (%s%s%s%s).\n", exc_class
? exc_class
->name
: "all", req
->modifiers
[i
].caught
? ", caught" : "", req
->modifiers
[i
].uncaught
? ", uncaught" : "", req
->modifiers
[i
].subclasses
? ", include-subclasses" : "");
7490 req
->modifiers
[i
].data
.exc_class
= exc_class
;
7492 if (!mono_class_is_assignable_from (mono_defaults
.exception_class
, exc_class
)) {
7494 return ERR_INVALID_ARGUMENT
;
7497 } else if (mod
== MOD_KIND_ASSEMBLY_ONLY
) {
7498 int n
= decode_int (p
, &p
, end
);
7501 req
->modifiers
[i
].data
.assemblies
= g_new0 (MonoAssembly
*, n
);
7502 for (j
= 0; j
< n
; ++j
) {
7503 req
->modifiers
[i
].data
.assemblies
[j
] = decode_assemblyid (p
, &p
, end
, &domain
, &err
);
7504 if (err
!= ERR_NONE
) {
7505 g_free (req
->modifiers
[i
].data
.assemblies
);
7509 } else if (mod
== MOD_KIND_SOURCE_FILE_ONLY
) {
7510 int n
= decode_int (p
, &p
, end
);
7513 modifier
= &req
->modifiers
[i
];
7514 modifier
->data
.source_files
= g_hash_table_new (g_str_hash
, g_str_equal
);
7515 for (j
= 0; j
< n
; ++j
) {
7516 char *s
= decode_string (p
, &p
, end
);
7520 s2
= strdup_tolower (s
);
7521 g_hash_table_insert (modifier
->data
.source_files
, s2
, s2
);
7525 } else if (mod
== MOD_KIND_TYPE_NAME_ONLY
) {
7526 int n
= decode_int (p
, &p
, end
);
7529 modifier
= &req
->modifiers
[i
];
7530 modifier
->data
.type_names
= g_hash_table_new (g_str_hash
, g_str_equal
);
7531 for (j
= 0; j
< n
; ++j
) {
7532 char *s
= decode_string (p
, &p
, end
);
7535 g_hash_table_insert (modifier
->data
.type_names
, s
, s
);
7539 return ERR_NOT_IMPLEMENTED
;
7543 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
) {
7546 req
->info
= set_breakpoint (method
, location
, req
, &error
);
7547 if (!mono_error_ok (&error
)) {
7549 DEBUG_PRINTF (1, "[dbg] Failed to set breakpoint: %s\n", mono_error_get_message (&error
));
7550 mono_error_cleanup (&error
);
7551 return ERR_NO_SEQ_POINT_AT_IL_OFFSET
;
7553 } else if (req
->event_kind
== EVENT_KIND_STEP
) {
7554 g_assert (step_thread_id
);
7556 err
= get_object (step_thread_id
, (MonoObject
**)&step_thread
);
7557 if (err
!= ERR_NONE
) {
7562 err
= ss_create (THREAD_TO_INTERNAL (step_thread
), size
, depth
, filter
, req
);
7563 if (err
!= ERR_NONE
) {
7567 } else if (req
->event_kind
== EVENT_KIND_METHOD_ENTRY
) {
7568 req
->info
= set_breakpoint (NULL
, METHOD_ENTRY_IL_OFFSET
, req
, NULL
);
7569 } else if (req
->event_kind
== EVENT_KIND_METHOD_EXIT
) {
7570 req
->info
= set_breakpoint (NULL
, METHOD_EXIT_IL_OFFSET
, req
, NULL
);
7571 } else if (req
->event_kind
== EVENT_KIND_EXCEPTION
) {
7572 } else if (req
->event_kind
== EVENT_KIND_TYPE_LOAD
) {
7574 if (req
->nmodifiers
) {
7576 return ERR_NOT_IMPLEMENTED
;
7580 mono_loader_lock ();
7581 g_ptr_array_add (event_requests
, req
);
7583 if (agent_config
.defer
) {
7584 /* Transmit cached data to the client on receipt of the event request */
7585 switch (req
->event_kind
) {
7586 case EVENT_KIND_APPDOMAIN_CREATE
:
7587 /* Emit load events for currently loaded domains */
7588 g_hash_table_foreach (domains
, emit_appdomain_load
, NULL
);
7590 case EVENT_KIND_ASSEMBLY_LOAD
:
7591 /* Emit load events for currently loaded assemblies */
7592 mono_assembly_foreach (emit_assembly_load
, NULL
);
7594 case EVENT_KIND_THREAD_START
:
7595 /* Emit start events for currently started threads */
7596 mono_g_hash_table_foreach (tid_to_thread
, emit_thread_start
, NULL
);
7598 case EVENT_KIND_TYPE_LOAD
:
7599 /* Emit type load events for currently loaded types */
7600 mono_domain_foreach (send_types_for_domain
, NULL
);
7606 mono_loader_unlock ();
7608 buffer_add_int (buf
, req
->id
);
7611 case CMD_EVENT_REQUEST_CLEAR
: {
7612 int etype
= decode_byte (p
, &p
, end
);
7613 int req_id
= decode_int (p
, &p
, end
);
7615 // FIXME: Make a faster mapping from req_id to request
7616 mono_loader_lock ();
7617 clear_event_request (req_id
, etype
);
7618 mono_loader_unlock ();
7621 case CMD_EVENT_REQUEST_CLEAR_ALL_BREAKPOINTS
: {
7624 mono_loader_lock ();
7626 while (i
< event_requests
->len
) {
7627 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
7629 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
) {
7630 clear_breakpoint ((MonoBreakpoint
*)req
->info
);
7632 g_ptr_array_remove_index_fast (event_requests
, i
);
7638 mono_loader_unlock ();
7642 return ERR_NOT_IMPLEMENTED
;
7649 domain_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7655 case CMD_APPDOMAIN_GET_ROOT_DOMAIN
: {
7656 buffer_add_domainid (buf
, mono_get_root_domain ());
7659 case CMD_APPDOMAIN_GET_FRIENDLY_NAME
: {
7660 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7661 if (err
!= ERR_NONE
)
7663 buffer_add_string (buf
, domain
->friendly_name
);
7666 case CMD_APPDOMAIN_GET_ASSEMBLIES
: {
7671 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7672 if (err
!= ERR_NONE
)
7674 mono_loader_lock ();
7676 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
7679 buffer_add_int (buf
, count
);
7680 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
7681 ass
= (MonoAssembly
*)tmp
->data
;
7682 buffer_add_assemblyid (buf
, domain
, ass
);
7684 mono_loader_unlock ();
7687 case CMD_APPDOMAIN_GET_ENTRY_ASSEMBLY
: {
7688 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7689 if (err
!= ERR_NONE
)
7692 buffer_add_assemblyid (buf
, domain
, domain
->entry_assembly
);
7695 case CMD_APPDOMAIN_GET_CORLIB
: {
7696 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7697 if (err
!= ERR_NONE
)
7700 buffer_add_assemblyid (buf
, domain
, domain
->domain
->mbr
.obj
.vtable
->klass
->image
->assembly
);
7703 case CMD_APPDOMAIN_CREATE_STRING
: {
7707 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7708 if (err
!= ERR_NONE
)
7710 s
= decode_string (p
, &p
, end
);
7712 o
= mono_string_new (domain
, s
);
7713 buffer_add_objid (buf
, (MonoObject
*)o
);
7716 case CMD_APPDOMAIN_CREATE_BOXED_VALUE
: {
7719 MonoDomain
*domain2
;
7722 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7723 if (err
!= ERR_NONE
)
7725 klass
= decode_typeid (p
, &p
, end
, &domain2
, &err
);
7726 if (err
!= ERR_NONE
)
7730 g_assert (domain
== domain2
);
7732 o
= mono_object_new_checked (domain
, klass
, &error
);
7733 mono_error_assert_ok (&error
);
7735 err
= decode_value (&klass
->byval_arg
, domain
, (guint8
*)mono_object_unbox (o
), p
, &p
, end
);
7736 if (err
!= ERR_NONE
)
7739 buffer_add_objid (buf
, o
);
7743 return ERR_NOT_IMPLEMENTED
;
7750 assembly_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7756 ass
= decode_assemblyid (p
, &p
, end
, &domain
, &err
);
7757 if (err
!= ERR_NONE
)
7761 case CMD_ASSEMBLY_GET_LOCATION
: {
7762 buffer_add_string (buf
, mono_image_get_filename (ass
->image
));
7765 case CMD_ASSEMBLY_GET_ENTRY_POINT
: {
7769 if (ass
->image
->dynamic
) {
7770 buffer_add_id (buf
, 0);
7772 token
= mono_image_get_entry_point (ass
->image
);
7774 buffer_add_id (buf
, 0);
7777 m
= mono_get_method_checked (ass
->image
, token
, NULL
, NULL
, &error
);
7779 mono_error_cleanup (&error
); /* FIXME don't swallow the error */
7780 buffer_add_methodid (buf
, domain
, m
);
7785 case CMD_ASSEMBLY_GET_MANIFEST_MODULE
: {
7786 buffer_add_moduleid (buf
, domain
, ass
->image
);
7789 case CMD_ASSEMBLY_GET_OBJECT
: {
7791 MonoObject
*o
= (MonoObject
*)mono_assembly_get_object_checked (domain
, ass
, &error
);
7793 mono_error_cleanup (&error
); /* FIXME don't swallow the error */
7794 return ERR_INVALID_OBJECT
;
7796 buffer_add_objid (buf
, o
);
7799 case CMD_ASSEMBLY_GET_TYPE
: {
7801 char *s
= decode_string (p
, &p
, end
);
7802 gboolean ignorecase
= decode_byte (p
, &p
, end
);
7803 MonoTypeNameParse info
;
7805 gboolean type_resolve
, res
;
7806 MonoDomain
*d
= mono_domain_get ();
7808 /* This is needed to be able to find referenced assemblies */
7809 res
= mono_domain_set (domain
, FALSE
);
7812 if (!mono_reflection_parse_type (s
, &info
)) {
7815 if (info
.assembly
.name
)
7817 t
= mono_reflection_get_type_checked (ass
->image
, ass
->image
, &info
, ignorecase
, &type_resolve
, &error
);
7818 if (!is_ok (&error
)) {
7819 mono_error_cleanup (&error
); /* FIXME don't swallow the error */
7820 mono_reflection_free_type_info (&info
);
7822 return ERR_INVALID_ARGUMENT
;
7825 buffer_add_typeid (buf
, domain
, t
? mono_class_from_mono_type (t
) : NULL
);
7826 mono_reflection_free_type_info (&info
);
7829 mono_domain_set (d
, TRUE
);
7833 case CMD_ASSEMBLY_GET_NAME
: {
7835 MonoAssembly
*mass
= ass
;
7837 name
= g_strdup_printf (
7838 "%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
7840 mass
->aname
.major
, mass
->aname
.minor
, mass
->aname
.build
, mass
->aname
.revision
,
7841 mass
->aname
.culture
&& *mass
->aname
.culture
? mass
->aname
.culture
: "neutral",
7842 mass
->aname
.public_key_token
[0] ? (char *)mass
->aname
.public_key_token
: "null",
7843 (mass
->aname
.flags
& ASSEMBLYREF_RETARGETABLE_FLAG
) ? ", Retargetable=Yes" : "");
7845 buffer_add_string (buf
, name
);
7850 return ERR_NOT_IMPLEMENTED
;
7857 module_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7863 case CMD_MODULE_GET_INFO
: {
7864 MonoImage
*image
= decode_moduleid (p
, &p
, end
, &domain
, &err
);
7867 basename
= g_path_get_basename (image
->name
);
7868 buffer_add_string (buf
, basename
); // name
7869 buffer_add_string (buf
, image
->module_name
); // scopename
7870 buffer_add_string (buf
, image
->name
); // fqname
7871 buffer_add_string (buf
, mono_image_get_guid (image
)); // guid
7872 buffer_add_assemblyid (buf
, domain
, image
->assembly
); // assembly
7877 return ERR_NOT_IMPLEMENTED
;
7884 field_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7890 case CMD_FIELD_GET_INFO
: {
7891 MonoClassField
*f
= decode_fieldid (p
, &p
, end
, &domain
, &err
);
7893 buffer_add_string (buf
, f
->name
);
7894 buffer_add_typeid (buf
, domain
, f
->parent
);
7895 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type (f
->type
));
7896 buffer_add_int (buf
, f
->type
->attrs
);
7900 return ERR_NOT_IMPLEMENTED
;
7907 buffer_add_cattr_arg (Buffer
*buf
, MonoType
*t
, MonoDomain
*domain
, MonoObject
*val
)
7909 if (val
&& val
->vtable
->klass
== mono_defaults
.runtimetype_class
) {
7910 /* Special case these so the client doesn't have to handle Type objects */
7912 buffer_add_byte (buf
, VALUE_TYPE_ID_TYPE
);
7913 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type (((MonoReflectionType
*)val
)->type
));
7914 } else if (MONO_TYPE_IS_REFERENCE (t
))
7915 buffer_add_value (buf
, t
, &val
, domain
);
7917 buffer_add_value (buf
, t
, mono_object_unbox (val
), domain
);
7921 buffer_add_cattrs (Buffer
*buf
, MonoDomain
*domain
, MonoImage
*image
, MonoClass
*attr_klass
, MonoCustomAttrInfo
*cinfo
)
7927 buffer_add_int (buf
, 0);
7931 for (i
= 0; i
< cinfo
->num_attrs
; ++i
) {
7932 if (!attr_klass
|| mono_class_has_parent (cinfo
->attrs
[i
].ctor
->klass
, attr_klass
))
7935 buffer_add_int (buf
, nattrs
);
7937 for (i
= 0; i
< cinfo
->num_attrs
; ++i
) {
7938 MonoCustomAttrEntry
*attr
= &cinfo
->attrs
[i
];
7939 if (!attr_klass
|| mono_class_has_parent (attr
->ctor
->klass
, attr_klass
)) {
7940 MonoArray
*typed_args
, *named_args
;
7942 CattrNamedArg
*arginfo
= NULL
;
7945 mono_reflection_create_custom_attr_data_args (image
, attr
->ctor
, attr
->data
, attr
->data_size
, &typed_args
, &named_args
, &arginfo
, &error
);
7946 if (!mono_error_ok (&error
)) {
7947 DEBUG_PRINTF (2, "[dbg] mono_reflection_create_custom_attr_data_args () failed with: '%s'\n", mono_error_get_message (&error
));
7948 mono_error_cleanup (&error
);
7949 return ERR_LOADER_ERROR
;
7952 buffer_add_methodid (buf
, domain
, attr
->ctor
);
7956 buffer_add_int (buf
, mono_array_length (typed_args
));
7957 for (j
= 0; j
< mono_array_length (typed_args
); ++j
) {
7958 MonoObject
*val
= mono_array_get (typed_args
, MonoObject
*, j
);
7960 t
= mono_method_signature (attr
->ctor
)->params
[j
];
7962 buffer_add_cattr_arg (buf
, t
, domain
, val
);
7965 buffer_add_int (buf
, 0);
7970 buffer_add_int (buf
, mono_array_length (named_args
));
7972 for (j
= 0; j
< mono_array_length (named_args
); ++j
) {
7973 MonoObject
*val
= mono_array_get (named_args
, MonoObject
*, j
);
7975 if (arginfo
[j
].prop
) {
7976 buffer_add_byte (buf
, 0x54);
7977 buffer_add_propertyid (buf
, domain
, arginfo
[j
].prop
);
7978 } else if (arginfo
[j
].field
) {
7979 buffer_add_byte (buf
, 0x53);
7980 buffer_add_fieldid (buf
, domain
, arginfo
[j
].field
);
7982 g_assert_not_reached ();
7985 buffer_add_cattr_arg (buf
, arginfo
[j
].type
, domain
, val
);
7988 buffer_add_int (buf
, 0);
7997 /* FIXME: Code duplication with icall.c */
7999 collect_interfaces (MonoClass
*klass
, GHashTable
*ifaces
, MonoError
*error
)
8004 mono_class_setup_interfaces (klass
, error
);
8005 if (!mono_error_ok (error
))
8008 for (i
= 0; i
< klass
->interface_count
; i
++) {
8009 ic
= klass
->interfaces
[i
];
8010 g_hash_table_insert (ifaces
, ic
, ic
);
8012 collect_interfaces (ic
, ifaces
, error
);
8013 if (!mono_error_ok (error
))
8019 type_commands_internal (int command
, MonoClass
*klass
, MonoDomain
*domain
, guint8
*p
, guint8
*end
, Buffer
*buf
)
8031 case CMD_TYPE_GET_INFO
: {
8032 buffer_add_string (buf
, klass
->name_space
);
8033 buffer_add_string (buf
, klass
->name
);
8035 name
= mono_type_get_name_full (&klass
->byval_arg
, MONO_TYPE_NAME_FORMAT_FULL_NAME
);
8036 buffer_add_string (buf
, name
);
8038 buffer_add_assemblyid (buf
, domain
, klass
->image
->assembly
);
8039 buffer_add_moduleid (buf
, domain
, klass
->image
);
8040 buffer_add_typeid (buf
, domain
, klass
->parent
);
8041 if (klass
->rank
|| klass
->byval_arg
.type
== MONO_TYPE_PTR
)
8042 buffer_add_typeid (buf
, domain
, klass
->element_class
);
8044 buffer_add_id (buf
, 0);
8045 buffer_add_int (buf
, klass
->type_token
);
8046 buffer_add_byte (buf
, klass
->rank
);
8047 buffer_add_int (buf
, klass
->flags
);
8049 type
= &klass
->byval_arg
;
8050 // FIXME: Can't decide whenever a class represents a byref type
8053 if (type
->type
== MONO_TYPE_PTR
)
8055 if (!type
->byref
&& (((type
->type
>= MONO_TYPE_BOOLEAN
) && (type
->type
<= MONO_TYPE_R8
)) || (type
->type
== MONO_TYPE_I
) || (type
->type
== MONO_TYPE_U
)))
8057 if (type
->type
== MONO_TYPE_VALUETYPE
)
8059 if (klass
->enumtype
)
8061 if (klass
->generic_container
)
8063 if (klass
->generic_container
|| klass
->generic_class
)
8065 buffer_add_byte (buf
, b
);
8068 while ((nested
= mono_class_get_nested_types (klass
, &iter
)))
8070 buffer_add_int (buf
, nnested
);
8072 while ((nested
= mono_class_get_nested_types (klass
, &iter
)))
8073 buffer_add_typeid (buf
, domain
, nested
);
8074 if (CHECK_PROTOCOL_VERSION (2, 12)) {
8075 if (klass
->generic_container
)
8076 buffer_add_typeid (buf
, domain
, klass
);
8077 else if (klass
->generic_class
)
8078 buffer_add_typeid (buf
, domain
, klass
->generic_class
->container_class
);
8080 buffer_add_id (buf
, 0);
8082 if (CHECK_PROTOCOL_VERSION (2, 15)) {
8085 if (klass
->generic_class
) {
8086 MonoGenericInst
*inst
= klass
->generic_class
->context
.class_inst
;
8088 count
= inst
->type_argc
;
8089 buffer_add_int (buf
, count
);
8090 for (i
= 0; i
< count
; i
++)
8091 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type (inst
->type_argv
[i
]));
8092 } else if (klass
->generic_container
) {
8093 MonoGenericContainer
*container
= klass
->generic_container
;
8096 count
= container
->type_argc
;
8097 buffer_add_int (buf
, count
);
8098 for (i
= 0; i
< count
; i
++) {
8099 pklass
= mono_class_from_generic_parameter_internal (mono_generic_container_get_param (container
, i
));
8100 buffer_add_typeid (buf
, domain
, pklass
);
8103 buffer_add_int (buf
, 0);
8108 case CMD_TYPE_GET_METHODS
: {
8111 gpointer iter
= NULL
;
8114 mono_class_setup_methods (klass
);
8116 nmethods
= mono_class_num_methods (klass
);
8118 buffer_add_int (buf
, nmethods
);
8120 while ((m
= mono_class_get_methods (klass
, &iter
))) {
8121 buffer_add_methodid (buf
, domain
, m
);
8124 g_assert (i
== nmethods
);
8127 case CMD_TYPE_GET_FIELDS
: {
8130 gpointer iter
= NULL
;
8133 nfields
= mono_class_num_fields (klass
);
8135 buffer_add_int (buf
, nfields
);
8137 while ((f
= mono_class_get_fields (klass
, &iter
))) {
8138 buffer_add_fieldid (buf
, domain
, f
);
8139 buffer_add_string (buf
, f
->name
);
8140 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type (f
->type
));
8141 buffer_add_int (buf
, f
->type
->attrs
);
8144 g_assert (i
== nfields
);
8147 case CMD_TYPE_GET_PROPERTIES
: {
8150 gpointer iter
= NULL
;
8153 nprops
= mono_class_num_properties (klass
);
8155 buffer_add_int (buf
, nprops
);
8157 while ((p
= mono_class_get_properties (klass
, &iter
))) {
8158 buffer_add_propertyid (buf
, domain
, p
);
8159 buffer_add_string (buf
, p
->name
);
8160 buffer_add_methodid (buf
, domain
, p
->get
);
8161 buffer_add_methodid (buf
, domain
, p
->set
);
8162 buffer_add_int (buf
, p
->attrs
);
8165 g_assert (i
== nprops
);
8168 case CMD_TYPE_GET_CATTRS
: {
8169 MonoClass
*attr_klass
;
8170 MonoCustomAttrInfo
*cinfo
;
8172 attr_klass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8173 /* attr_klass can be NULL */
8174 if (err
!= ERR_NONE
)
8177 cinfo
= mono_custom_attrs_from_class_checked (klass
, &error
);
8178 if (!is_ok (&error
)) {
8179 mono_error_cleanup (&error
); /* FIXME don't swallow the error message */
8180 return ERR_LOADER_ERROR
;
8183 err
= buffer_add_cattrs (buf
, domain
, klass
->image
, attr_klass
, cinfo
);
8184 if (err
!= ERR_NONE
)
8188 case CMD_TYPE_GET_FIELD_CATTRS
: {
8189 MonoClass
*attr_klass
;
8190 MonoCustomAttrInfo
*cinfo
;
8191 MonoClassField
*field
;
8193 field
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
8194 if (err
!= ERR_NONE
)
8196 attr_klass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8197 if (err
!= ERR_NONE
)
8200 cinfo
= mono_custom_attrs_from_field_checked (klass
, field
, &error
);
8201 if (!is_ok (&error
)) {
8202 mono_error_cleanup (&error
); /* FIXME don't swallow the error message */
8203 return ERR_LOADER_ERROR
;
8206 err
= buffer_add_cattrs (buf
, domain
, klass
->image
, attr_klass
, cinfo
);
8207 if (err
!= ERR_NONE
)
8211 case CMD_TYPE_GET_PROPERTY_CATTRS
: {
8212 MonoClass
*attr_klass
;
8213 MonoCustomAttrInfo
*cinfo
;
8216 prop
= decode_propertyid (p
, &p
, end
, NULL
, &err
);
8217 if (err
!= ERR_NONE
)
8219 attr_klass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8220 if (err
!= ERR_NONE
)
8223 cinfo
= mono_custom_attrs_from_property_checked (klass
, prop
, &error
);
8224 if (!is_ok (&error
)) {
8225 mono_error_cleanup (&error
); /* FIXME don't swallow the error message */
8226 return ERR_LOADER_ERROR
;
8229 err
= buffer_add_cattrs (buf
, domain
, klass
->image
, attr_klass
, cinfo
);
8230 if (err
!= ERR_NONE
)
8234 case CMD_TYPE_GET_VALUES
:
8235 case CMD_TYPE_GET_VALUES_2
: {
8242 MonoThread
*thread_obj
;
8243 MonoInternalThread
*thread
= NULL
;
8244 guint32 special_static_type
;
8246 if (command
== CMD_TYPE_GET_VALUES_2
) {
8247 int objid
= decode_objid (p
, &p
, end
);
8250 err
= get_object (objid
, (MonoObject
**)&thread_obj
);
8251 if (err
!= ERR_NONE
)
8254 thread
= THREAD_TO_INTERNAL (thread_obj
);
8257 len
= decode_int (p
, &p
, end
);
8258 for (i
= 0; i
< len
; ++i
) {
8259 f
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
8260 if (err
!= ERR_NONE
)
8263 if (!(f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
8264 return ERR_INVALID_FIELDID
;
8265 special_static_type
= mono_class_field_get_special_static_type (f
);
8266 if (special_static_type
!= SPECIAL_STATIC_NONE
) {
8267 if (!(thread
&& special_static_type
== SPECIAL_STATIC_THREAD
))
8268 return ERR_INVALID_FIELDID
;
8271 /* Check that the field belongs to the object */
8273 for (k
= klass
; k
; k
= k
->parent
) {
8274 if (k
== f
->parent
) {
8280 return ERR_INVALID_FIELDID
;
8282 vtable
= mono_class_vtable (domain
, f
->parent
);
8283 val
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type (f
->type
)));
8284 mono_field_static_get_value_for_thread (thread
? thread
: mono_thread_internal_current (), vtable
, f
, val
, &error
);
8285 if (!is_ok (&error
))
8286 return ERR_INVALID_FIELDID
;
8287 buffer_add_value (buf
, f
->type
, val
, domain
);
8292 case CMD_TYPE_SET_VALUES
: {
8300 len
= decode_int (p
, &p
, end
);
8301 for (i
= 0; i
< len
; ++i
) {
8302 f
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
8303 if (err
!= ERR_NONE
)
8306 if (!(f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
8307 return ERR_INVALID_FIELDID
;
8308 if (mono_class_field_is_special_static (f
))
8309 return ERR_INVALID_FIELDID
;
8311 /* Check that the field belongs to the object */
8313 for (k
= klass
; k
; k
= k
->parent
) {
8314 if (k
== f
->parent
) {
8320 return ERR_INVALID_FIELDID
;
8322 // FIXME: Check for literal/const
8324 vtable
= mono_class_vtable (domain
, f
->parent
);
8325 val
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type (f
->type
)));
8326 err
= decode_value (f
->type
, domain
, val
, p
, &p
, end
);
8327 if (err
!= ERR_NONE
) {
8331 if (MONO_TYPE_IS_REFERENCE (f
->type
))
8332 mono_field_static_set_value (vtable
, f
, *(gpointer
*)val
);
8334 mono_field_static_set_value (vtable
, f
, val
);
8339 case CMD_TYPE_GET_OBJECT
: {
8340 MonoObject
*o
= (MonoObject
*)mono_type_get_object_checked (domain
, &klass
->byval_arg
, &error
);
8341 if (!mono_error_ok (&error
)) {
8342 mono_error_cleanup (&error
);
8343 return ERR_INVALID_OBJECT
;
8345 buffer_add_objid (buf
, o
);
8348 case CMD_TYPE_GET_SOURCE_FILES
:
8349 case CMD_TYPE_GET_SOURCE_FILES_2
: {
8350 char *source_file
, *base
;
8354 files
= get_source_files_for_type (klass
);
8356 buffer_add_int (buf
, files
->len
);
8357 for (i
= 0; i
< files
->len
; ++i
) {
8358 source_file
= (char *)g_ptr_array_index (files
, i
);
8359 if (command
== CMD_TYPE_GET_SOURCE_FILES_2
) {
8360 buffer_add_string (buf
, source_file
);
8362 base
= dbg_path_get_basename (source_file
);
8363 buffer_add_string (buf
, base
);
8366 g_free (source_file
);
8368 g_ptr_array_free (files
, TRUE
);
8371 case CMD_TYPE_IS_ASSIGNABLE_FROM
: {
8372 MonoClass
*oklass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8374 if (err
!= ERR_NONE
)
8376 if (mono_class_is_assignable_from (klass
, oklass
))
8377 buffer_add_byte (buf
, 1);
8379 buffer_add_byte (buf
, 0);
8382 case CMD_TYPE_GET_METHODS_BY_NAME_FLAGS
: {
8383 char *name
= decode_string (p
, &p
, end
);
8384 int i
, flags
= decode_int (p
, &p
, end
);
8385 MonoException
*ex
= NULL
;
8386 GPtrArray
*array
= mono_class_get_methods_by_name (klass
, name
, flags
& ~BINDING_FLAGS_IGNORE_CASE
, (flags
& BINDING_FLAGS_IGNORE_CASE
) != 0, TRUE
, &ex
);
8389 return ERR_LOADER_ERROR
;
8390 buffer_add_int (buf
, array
->len
);
8391 for (i
= 0; i
< array
->len
; ++i
) {
8392 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (array
, i
);
8393 buffer_add_methodid (buf
, domain
, method
);
8396 g_ptr_array_free (array
, TRUE
);
8400 case CMD_TYPE_GET_INTERFACES
: {
8402 GHashTable
*iface_hash
= g_hash_table_new (NULL
, NULL
);
8403 MonoClass
*tclass
, *iface
;
8404 GHashTableIter iter
;
8408 for (parent
= tclass
; parent
; parent
= parent
->parent
) {
8409 mono_class_setup_interfaces (parent
, &error
);
8410 if (!mono_error_ok (&error
))
8411 return ERR_LOADER_ERROR
;
8412 collect_interfaces (parent
, iface_hash
, &error
);
8413 if (!mono_error_ok (&error
))
8414 return ERR_LOADER_ERROR
;
8417 buffer_add_int (buf
, g_hash_table_size (iface_hash
));
8419 g_hash_table_iter_init (&iter
, iface_hash
);
8420 while (g_hash_table_iter_next (&iter
, NULL
, (void**)&iface
))
8421 buffer_add_typeid (buf
, domain
, iface
);
8422 g_hash_table_destroy (iface_hash
);
8425 case CMD_TYPE_GET_INTERFACE_MAP
: {
8426 int tindex
, ioffset
;
8427 gboolean variance_used
;
8429 int len
, nmethods
, i
;
8433 len
= decode_int (p
, &p
, end
);
8434 mono_class_setup_vtable (klass
);
8436 for (tindex
= 0; tindex
< len
; ++tindex
) {
8437 iclass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8438 if (err
!= ERR_NONE
)
8441 ioffset
= mono_class_interface_offset_with_variance (klass
, iclass
, &variance_used
);
8443 return ERR_INVALID_ARGUMENT
;
8445 nmethods
= mono_class_num_methods (iclass
);
8446 buffer_add_int (buf
, nmethods
);
8449 while ((method
= mono_class_get_methods (iclass
, &iter
))) {
8450 buffer_add_methodid (buf
, domain
, method
);
8452 for (i
= 0; i
< nmethods
; ++i
)
8453 buffer_add_methodid (buf
, domain
, klass
->vtable
[i
+ ioffset
]);
8457 case CMD_TYPE_IS_INITIALIZED
: {
8458 MonoVTable
*vtable
= mono_class_vtable (domain
, klass
);
8461 buffer_add_int (buf
, (vtable
->initialized
|| vtable
->init_failed
) ? 1 : 0);
8463 buffer_add_int (buf
, 0);
8466 case CMD_TYPE_CREATE_INSTANCE
: {
8470 obj
= mono_object_new_checked (domain
, klass
, &error
);
8471 mono_error_assert_ok (&error
);
8472 buffer_add_objid (buf
, obj
);
8476 return ERR_NOT_IMPLEMENTED
;
8483 type_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
8486 MonoDomain
*old_domain
;
8490 klass
= decode_typeid (p
, &p
, end
, &domain
, &err
);
8491 if (err
!= ERR_NONE
)
8494 old_domain
= mono_domain_get ();
8496 mono_domain_set (domain
, TRUE
);
8498 err
= type_commands_internal (command
, klass
, domain
, p
, end
, buf
);
8500 mono_domain_set (old_domain
, TRUE
);
8506 method_commands_internal (int command
, MonoMethod
*method
, MonoDomain
*domain
, guint8
*p
, guint8
*end
, Buffer
*buf
)
8508 MonoMethodHeader
*header
;
8512 case CMD_METHOD_GET_NAME
: {
8513 buffer_add_string (buf
, method
->name
);
8516 case CMD_METHOD_GET_DECLARING_TYPE
: {
8517 buffer_add_typeid (buf
, domain
, method
->klass
);
8520 case CMD_METHOD_GET_DEBUG_INFO
: {
8522 MonoDebugMethodInfo
*minfo
;
8524 int i
, j
, n_il_offsets
;
8526 GPtrArray
*source_file_list
;
8527 MonoSymSeqPoint
*sym_seq_points
;
8529 header
= mono_method_get_header_checked (method
, &error
);
8531 mono_error_cleanup (&error
); /* FIXME don't swallow the error */
8532 buffer_add_int (buf
, 0);
8533 buffer_add_string (buf
, "");
8534 buffer_add_int (buf
, 0);
8538 minfo
= mono_debug_lookup_method (method
);
8540 buffer_add_int (buf
, header
->code_size
);
8541 buffer_add_string (buf
, "");
8542 buffer_add_int (buf
, 0);
8543 mono_metadata_free_mh (header
);
8547 mono_debug_get_seq_points (minfo
, &source_file
, &source_file_list
, &source_files
, &sym_seq_points
, &n_il_offsets
);
8548 buffer_add_int (buf
, header
->code_size
);
8549 if (CHECK_PROTOCOL_VERSION (2, 13)) {
8550 buffer_add_int (buf
, source_file_list
->len
);
8551 for (i
= 0; i
< source_file_list
->len
; ++i
) {
8552 MonoDebugSourceInfo
*sinfo
= (MonoDebugSourceInfo
*)g_ptr_array_index (source_file_list
, i
);
8553 buffer_add_string (buf
, sinfo
->source_file
);
8554 if (CHECK_PROTOCOL_VERSION (2, 14)) {
8555 for (j
= 0; j
< 16; ++j
)
8556 buffer_add_byte (buf
, sinfo
->hash
[j
]);
8560 buffer_add_string (buf
, source_file
);
8562 buffer_add_int (buf
, n_il_offsets
);
8563 DEBUG_PRINTF (10, "Line number table for method %s:\n", mono_method_full_name (method
, TRUE
));
8564 for (i
= 0; i
< n_il_offsets
; ++i
) {
8565 MonoSymSeqPoint
*sp
= &sym_seq_points
[i
];
8566 const char *srcfile
= "";
8568 if (source_files
[i
] != -1) {
8569 MonoDebugSourceInfo
*sinfo
= (MonoDebugSourceInfo
*)g_ptr_array_index (source_file_list
, source_files
[i
]);
8570 srcfile
= sinfo
->source_file
;
8572 DEBUG_PRINTF (10, "IL%x -> %s:%d %d %d %d\n", sp
->il_offset
, srcfile
, sp
->line
, sp
->column
, sp
->end_line
, sp
->end_column
);
8573 buffer_add_int (buf
, sp
->il_offset
);
8574 buffer_add_int (buf
, sp
->line
);
8575 if (CHECK_PROTOCOL_VERSION (2, 13))
8576 buffer_add_int (buf
, source_files
[i
]);
8577 if (CHECK_PROTOCOL_VERSION (2, 19))
8578 buffer_add_int (buf
, sp
->column
);
8579 if (CHECK_PROTOCOL_VERSION (2, 32)) {
8580 buffer_add_int (buf
, sp
->end_line
);
8581 buffer_add_int (buf
, sp
->end_column
);
8584 g_free (source_file
);
8585 g_free (source_files
);
8586 g_free (sym_seq_points
);
8587 g_ptr_array_free (source_file_list
, TRUE
);
8588 mono_metadata_free_mh (header
);
8591 case CMD_METHOD_GET_PARAM_INFO
: {
8592 MonoMethodSignature
*sig
= mono_method_signature (method
);
8596 /* FIXME: mono_class_from_mono_type () and byrefs */
8598 /* FIXME: Use a smaller encoding */
8599 buffer_add_int (buf
, sig
->call_convention
);
8600 buffer_add_int (buf
, sig
->param_count
);
8601 buffer_add_int (buf
, sig
->generic_param_count
);
8602 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type (sig
->ret
));
8603 for (i
= 0; i
< sig
->param_count
; ++i
) {
8605 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type (sig
->params
[i
]));
8608 /* Emit parameter names */
8609 names
= g_new (char *, sig
->param_count
);
8610 mono_method_get_param_names (method
, (const char **) names
);
8611 for (i
= 0; i
< sig
->param_count
; ++i
)
8612 buffer_add_string (buf
, names
[i
]);
8617 case CMD_METHOD_GET_LOCALS_INFO
: {
8620 MonoDebugLocalsInfo
*locals
;
8621 int *locals_map
= NULL
;
8623 header
= mono_method_get_header_checked (method
, &error
);
8625 mono_error_cleanup (&error
); /* FIXME don't swallow the error */
8626 return ERR_INVALID_ARGUMENT
;
8629 locals
= mono_debug_lookup_locals (method
);
8631 buffer_add_int (buf
, header
->num_locals
);
8633 for (i
= 0; i
< header
->num_locals
; ++i
) {
8634 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type (header
->locals
[i
]));
8637 for (i
= 0; i
< header
->num_locals
; ++i
) {
8639 sprintf (lname
, "V_%d", i
);
8640 buffer_add_string (buf
, lname
);
8643 for (i
= 0; i
< header
->num_locals
; ++i
) {
8644 buffer_add_int (buf
, 0);
8645 buffer_add_int (buf
, header
->code_size
);
8648 num_locals
= locals
->num_locals
;
8649 buffer_add_int (buf
, num_locals
);
8652 for (i
= 0; i
< num_locals
; ++i
) {
8653 g_assert (locals
->locals
[i
].index
< header
->num_locals
);
8654 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type (header
->locals
[locals
->locals
[i
].index
]));
8657 for (i
= 0; i
< num_locals
; ++i
)
8658 buffer_add_string (buf
, locals
->locals
[i
].name
);
8660 for (i
= 0; i
< num_locals
; ++i
) {
8661 if (locals
->locals
[i
].block
) {
8662 buffer_add_int (buf
, locals
->locals
[i
].block
->start_offset
);
8663 buffer_add_int (buf
, locals
->locals
[i
].block
->end_offset
);
8665 buffer_add_int (buf
, 0);
8666 buffer_add_int (buf
, header
->code_size
);
8670 mono_metadata_free_mh (header
);
8673 mono_debug_free_locals (locals
);
8674 g_free (locals_map
);
8678 case CMD_METHOD_GET_INFO
:
8679 buffer_add_int (buf
, method
->flags
);
8680 buffer_add_int (buf
, method
->iflags
);
8681 buffer_add_int (buf
, method
->token
);
8682 if (CHECK_PROTOCOL_VERSION (2, 12)) {
8684 if (method
->is_generic
)
8686 if (mono_method_signature (method
)->generic_param_count
)
8688 buffer_add_byte (buf
, attrs
);
8689 if (method
->is_generic
|| method
->is_inflated
) {
8692 if (method
->is_generic
) {
8695 MonoMethodInflated
*imethod
= (MonoMethodInflated
*)method
;
8697 result
= imethod
->declaring
;
8698 if (imethod
->context
.class_inst
) {
8699 MonoClass
*klass
= ((MonoMethod
*) imethod
)->klass
;
8700 /*Generic methods gets the context of the GTD.*/
8701 if (mono_class_get_context (klass
)) {
8703 result
= mono_class_inflate_generic_method_full_checked (result
, klass
, mono_class_get_context (klass
), &error
);
8704 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
8709 buffer_add_methodid (buf
, domain
, result
);
8711 buffer_add_id (buf
, 0);
8713 if (CHECK_PROTOCOL_VERSION (2, 15)) {
8714 if (mono_method_signature (method
)->generic_param_count
) {
8717 if (method
->is_inflated
) {
8718 MonoGenericInst
*inst
= mono_method_get_context (method
)->method_inst
;
8720 count
= inst
->type_argc
;
8721 buffer_add_int (buf
, count
);
8723 for (i
= 0; i
< count
; i
++)
8724 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type (inst
->type_argv
[i
]));
8726 buffer_add_int (buf
, 0);
8728 } else if (method
->is_generic
) {
8729 MonoGenericContainer
*container
= mono_method_get_generic_container (method
);
8731 count
= mono_method_signature (method
)->generic_param_count
;
8732 buffer_add_int (buf
, count
);
8733 for (i
= 0; i
< count
; i
++) {
8734 MonoGenericParam
*param
= mono_generic_container_get_param (container
, i
);
8735 MonoClass
*pklass
= mono_class_from_generic_parameter_internal (param
);
8736 buffer_add_typeid (buf
, domain
, pklass
);
8739 buffer_add_int (buf
, 0);
8742 buffer_add_int (buf
, 0);
8747 case CMD_METHOD_GET_BODY
: {
8751 header
= mono_method_get_header_checked (method
, &error
);
8753 mono_error_cleanup (&error
); /* FIXME don't swallow the error */
8754 buffer_add_int (buf
, 0);
8756 if (CHECK_PROTOCOL_VERSION (2, 18))
8757 buffer_add_int (buf
, 0);
8759 buffer_add_int (buf
, header
->code_size
);
8760 for (i
= 0; i
< header
->code_size
; ++i
)
8761 buffer_add_byte (buf
, header
->code
[i
]);
8763 if (CHECK_PROTOCOL_VERSION (2, 18)) {
8764 buffer_add_int (buf
, header
->num_clauses
);
8765 for (i
= 0; i
< header
->num_clauses
; ++i
) {
8766 MonoExceptionClause
*clause
= &header
->clauses
[i
];
8768 buffer_add_int (buf
, clause
->flags
);
8769 buffer_add_int (buf
, clause
->try_offset
);
8770 buffer_add_int (buf
, clause
->try_len
);
8771 buffer_add_int (buf
, clause
->handler_offset
);
8772 buffer_add_int (buf
, clause
->handler_len
);
8773 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_NONE
)
8774 buffer_add_typeid (buf
, domain
, clause
->data
.catch_class
);
8775 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
)
8776 buffer_add_int (buf
, clause
->data
.filter_offset
);
8780 mono_metadata_free_mh (header
);
8785 case CMD_METHOD_RESOLVE_TOKEN
: {
8786 guint32 token
= decode_int (p
, &p
, end
);
8789 switch (mono_metadata_token_code (token
)) {
8790 case MONO_TOKEN_STRING
: {
8795 s
= mono_ldstr_checked (domain
, method
->klass
->image
, mono_metadata_token_index (token
), &error
);
8796 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
8798 s2
= mono_string_to_utf8_checked (s
, &error
);
8799 mono_error_assert_ok (&error
);
8801 buffer_add_byte (buf
, TOKEN_TYPE_STRING
);
8802 buffer_add_string (buf
, s2
);
8809 MonoClass
*handle_class
;
8811 if (method
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
) {
8812 val
= mono_method_get_wrapper_data (method
, token
);
8813 handle_class
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
+ 1);
8815 if (handle_class
== NULL
) {
8816 // Can't figure out the token type
8817 buffer_add_byte (buf
, TOKEN_TYPE_UNKNOWN
);
8821 val
= mono_ldtoken_checked (method
->klass
->image
, token
, &handle_class
, NULL
, &error
);
8823 g_error ("Could not load token due to %s", mono_error_get_message (&error
));
8826 if (handle_class
== mono_defaults
.typehandle_class
) {
8827 buffer_add_byte (buf
, TOKEN_TYPE_TYPE
);
8828 if (method
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
)
8829 buffer_add_typeid (buf
, domain
, (MonoClass
*) val
);
8831 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type ((MonoType
*)val
));
8832 } else if (handle_class
== mono_defaults
.fieldhandle_class
) {
8833 buffer_add_byte (buf
, TOKEN_TYPE_FIELD
);
8834 buffer_add_fieldid (buf
, domain
, (MonoClassField
*)val
);
8835 } else if (handle_class
== mono_defaults
.methodhandle_class
) {
8836 buffer_add_byte (buf
, TOKEN_TYPE_METHOD
);
8837 buffer_add_methodid (buf
, domain
, (MonoMethod
*)val
);
8838 } else if (handle_class
== mono_defaults
.string_class
) {
8841 s
= mono_string_to_utf8_checked ((MonoString
*)val
, &error
);
8842 mono_error_assert_ok (&error
);
8843 buffer_add_byte (buf
, TOKEN_TYPE_STRING
);
8844 buffer_add_string (buf
, s
);
8847 g_assert_not_reached ();
8854 case CMD_METHOD_GET_CATTRS
: {
8856 MonoClass
*attr_klass
;
8857 MonoCustomAttrInfo
*cinfo
;
8859 attr_klass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8860 /* attr_klass can be NULL */
8861 if (err
!= ERR_NONE
)
8864 cinfo
= mono_custom_attrs_from_method_checked (method
, &error
);
8865 if (!is_ok (&error
)) {
8866 mono_error_cleanup (&error
); /* FIXME don't swallow the error message */
8867 return ERR_LOADER_ERROR
;
8870 err
= buffer_add_cattrs (buf
, domain
, method
->klass
->image
, attr_klass
, cinfo
);
8871 if (err
!= ERR_NONE
)
8875 case CMD_METHOD_MAKE_GENERIC_METHOD
: {
8877 MonoType
**type_argv
;
8881 MonoGenericInst
*ginst
;
8882 MonoGenericContext tmp_context
;
8883 MonoMethod
*inflated
;
8885 type_argc
= decode_int (p
, &p
, end
);
8886 type_argv
= g_new0 (MonoType
*, type_argc
);
8887 for (i
= 0; i
< type_argc
; ++i
) {
8888 klass
= decode_typeid (p
, &p
, end
, &d
, &err
);
8889 if (err
!= ERR_NONE
) {
8895 return ERR_INVALID_ARGUMENT
;
8897 type_argv
[i
] = &klass
->byval_arg
;
8899 ginst
= mono_metadata_get_generic_inst (type_argc
, type_argv
);
8901 tmp_context
.class_inst
= method
->klass
->generic_class
? method
->klass
->generic_class
->context
.class_inst
: NULL
;
8902 tmp_context
.method_inst
= ginst
;
8904 inflated
= mono_class_inflate_generic_method_checked (method
, &tmp_context
, &error
);
8905 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
8906 if (!mono_verifier_is_method_valid_generic_instantiation (inflated
))
8907 return ERR_INVALID_ARGUMENT
;
8908 buffer_add_methodid (buf
, domain
, inflated
);
8912 return ERR_NOT_IMPLEMENTED
;
8919 method_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
8922 MonoDomain
*old_domain
;
8926 method
= decode_methodid (p
, &p
, end
, &domain
, &err
);
8927 if (err
!= ERR_NONE
)
8930 old_domain
= mono_domain_get ();
8932 mono_domain_set (domain
, TRUE
);
8934 err
= method_commands_internal (command
, method
, domain
, p
, end
, buf
);
8936 mono_domain_set (old_domain
, TRUE
);
8942 thread_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
8944 int objid
= decode_objid (p
, &p
, end
);
8946 MonoThread
*thread_obj
;
8947 MonoInternalThread
*thread
;
8949 err
= get_object (objid
, (MonoObject
**)&thread_obj
);
8950 if (err
!= ERR_NONE
)
8953 thread
= THREAD_TO_INTERNAL (thread_obj
);
8956 case CMD_THREAD_GET_NAME
: {
8958 gunichar2
*s
= mono_thread_get_name (thread
, &name_len
);
8961 buffer_add_int (buf
, 0);
8966 name
= g_utf16_to_utf8 (s
, name_len
, NULL
, &len
, NULL
);
8968 buffer_add_int (buf
, len
);
8969 buffer_add_data (buf
, (guint8
*)name
, len
);
8974 case CMD_THREAD_GET_FRAME_INFO
: {
8975 DebuggerTlsData
*tls
;
8976 int i
, start_frame
, length
;
8978 // Wait for suspending if it already started
8979 // FIXME: Races with suspend_count
8980 while (!is_suspended ()) {
8982 wait_for_suspend ();
8986 wait_for_suspend ();
8987 if (!is_suspended ())
8988 return ERR_NOT_SUSPENDED;
8991 start_frame
= decode_int (p
, &p
, end
);
8992 length
= decode_int (p
, &p
, end
);
8994 if (start_frame
!= 0 || length
!= -1)
8995 return ERR_NOT_IMPLEMENTED
;
8997 mono_loader_lock ();
8998 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
8999 mono_loader_unlock ();
9002 compute_frame_info (thread
, tls
);
9004 buffer_add_int (buf
, tls
->frame_count
);
9005 for (i
= 0; i
< tls
->frame_count
; ++i
) {
9006 buffer_add_int (buf
, tls
->frames
[i
]->id
);
9007 buffer_add_methodid (buf
, tls
->frames
[i
]->domain
, tls
->frames
[i
]->actual_method
);
9008 buffer_add_int (buf
, tls
->frames
[i
]->il_offset
);
9010 * Instead of passing the frame type directly to the client, we associate
9011 * it with the previous frame using a set of flags. This avoids lots of
9012 * conditional code in the client, since a frame whose type isn't
9013 * FRAME_TYPE_MANAGED has no method, location, etc.
9015 buffer_add_byte (buf
, tls
->frames
[i
]->flags
);
9020 case CMD_THREAD_GET_STATE
:
9021 buffer_add_int (buf
, thread
->state
);
9023 case CMD_THREAD_GET_INFO
:
9024 buffer_add_byte (buf
, thread
->threadpool_thread
);
9026 case CMD_THREAD_GET_ID
:
9027 buffer_add_long (buf
, (guint64
)(gsize
)thread
);
9029 case CMD_THREAD_GET_TID
:
9030 buffer_add_long (buf
, (guint64
)thread
->tid
);
9032 case CMD_THREAD_SET_IP
: {
9033 DebuggerTlsData
*tls
;
9036 MonoSeqPointInfo
*seq_points
;
9041 method
= decode_methodid (p
, &p
, end
, &domain
, &err
);
9042 if (err
!= ERR_NONE
)
9044 il_offset
= decode_long (p
, &p
, end
);
9046 while (!is_suspended ()) {
9048 wait_for_suspend ();
9051 mono_loader_lock ();
9052 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
9053 mono_loader_unlock ();
9056 compute_frame_info (thread
, tls
);
9057 if (tls
->frame_count
== 0 || tls
->frames
[0]->actual_method
!= method
)
9058 return ERR_INVALID_ARGUMENT
;
9060 found_sp
= mono_find_seq_point (domain
, method
, il_offset
, &seq_points
, &sp
);
9062 g_assert (seq_points
);
9065 return ERR_INVALID_ARGUMENT
;
9067 // FIXME: Check that the ip change is safe
9069 DEBUG_PRINTF (1, "[dbg] Setting IP to %s:0x%0x(0x%0x)\n", tls
->frames
[0]->actual_method
->name
, (int)sp
.il_offset
, (int)sp
.native_offset
);
9070 MONO_CONTEXT_SET_IP (&tls
->restore_state
.ctx
, (guint8
*)tls
->frames
[0]->ji
->code_start
+ sp
.native_offset
);
9074 return ERR_NOT_IMPLEMENTED
;
9081 frame_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9085 MonoThread
*thread_obj
;
9086 MonoInternalThread
*thread
;
9087 int pos
, i
, len
, frame_idx
;
9088 DebuggerTlsData
*tls
;
9090 MonoDebugMethodJitInfo
*jit
;
9091 MonoMethodSignature
*sig
;
9093 MonoMethodHeader
*header
;
9095 objid
= decode_objid (p
, &p
, end
);
9096 err
= get_object (objid
, (MonoObject
**)&thread_obj
);
9097 if (err
!= ERR_NONE
)
9100 thread
= THREAD_TO_INTERNAL (thread_obj
);
9102 id
= decode_id (p
, &p
, end
);
9104 mono_loader_lock ();
9105 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
9106 mono_loader_unlock ();
9109 for (i
= 0; i
< tls
->frame_count
; ++i
) {
9110 if (tls
->frames
[i
]->id
== id
)
9113 if (i
== tls
->frame_count
)
9114 return ERR_INVALID_FRAMEID
;
9117 frame
= tls
->frames
[frame_idx
];
9119 /* This is supported for frames without has_ctx etc. set */
9120 if (command
== CMD_STACK_FRAME_GET_DOMAIN
) {
9121 if (CHECK_PROTOCOL_VERSION (2, 38))
9122 buffer_add_domainid (buf
, frame
->domain
);
9126 if (!frame
->has_ctx
)
9127 return ERR_ABSENT_INFORMATION
;
9130 frame
->jit
= mono_debug_find_method (frame
->api_method
, frame
->domain
);
9131 if (!frame
->jit
&& frame
->api_method
->is_inflated
)
9132 frame
->jit
= mono_debug_find_method (mono_method_get_declaring_generic_method (frame
->api_method
), frame
->domain
);
9136 /* This could happen for aot images with no jit debug info */
9137 s
= mono_method_full_name (frame
->api_method
, TRUE
);
9138 DEBUG_PRINTF (1, "[dbg] No debug information found for '%s'.\n", s
);
9140 return ERR_ABSENT_INFORMATION
;
9145 sig
= mono_method_signature (frame
->actual_method
);
9147 if (!mono_get_seq_points (frame
->domain
, frame
->actual_method
))
9149 * The method is probably from an aot image compiled without soft-debug, variables might be dead, etc.
9151 return ERR_ABSENT_INFORMATION
;
9154 case CMD_STACK_FRAME_GET_VALUES
: {
9156 len
= decode_int (p
, &p
, end
);
9157 header
= mono_method_get_header_checked (frame
->actual_method
, &error
);
9158 mono_error_assert_ok (&error
); /* FIXME report error */
9160 for (i
= 0; i
< len
; ++i
) {
9161 pos
= decode_int (p
, &p
, end
);
9166 DEBUG_PRINTF (4, "[dbg] send arg %d.\n", pos
);
9168 g_assert (pos
>= 0 && pos
< jit
->num_params
);
9170 add_var (buf
, jit
, sig
->params
[pos
], &jit
->params
[pos
], &frame
->ctx
, frame
->domain
, FALSE
);
9172 MonoDebugLocalsInfo
*locals
;
9174 locals
= mono_debug_lookup_locals (frame
->method
);
9176 g_assert (pos
< locals
->num_locals
);
9177 pos
= locals
->locals
[pos
].index
;
9178 mono_debug_free_locals (locals
);
9180 g_assert (pos
>= 0 && pos
< jit
->num_locals
);
9182 DEBUG_PRINTF (4, "[dbg] send local %d.\n", pos
);
9184 add_var (buf
, jit
, header
->locals
[pos
], &jit
->locals
[pos
], &frame
->ctx
, frame
->domain
, FALSE
);
9187 mono_metadata_free_mh (header
);
9190 case CMD_STACK_FRAME_GET_THIS
: {
9191 if (frame
->api_method
->klass
->valuetype
) {
9192 if (!sig
->hasthis
) {
9193 MonoObject
*p
= NULL
;
9194 buffer_add_value (buf
, &mono_defaults
.object_class
->byval_arg
, &p
, frame
->domain
);
9196 add_var (buf
, jit
, &frame
->actual_method
->klass
->this_arg
, jit
->this_var
, &frame
->ctx
, frame
->domain
, TRUE
);
9199 if (!sig
->hasthis
) {
9200 MonoObject
*p
= NULL
;
9201 buffer_add_value (buf
, &frame
->actual_method
->klass
->byval_arg
, &p
, frame
->domain
);
9203 add_var (buf
, jit
, &frame
->api_method
->klass
->byval_arg
, jit
->this_var
, &frame
->ctx
, frame
->domain
, TRUE
);
9208 case CMD_STACK_FRAME_SET_VALUES
: {
9212 MonoDebugVarInfo
*var
;
9214 len
= decode_int (p
, &p
, end
);
9215 header
= mono_method_get_header_checked (frame
->actual_method
, &error
);
9216 mono_error_assert_ok (&error
); /* FIXME report error */
9218 for (i
= 0; i
< len
; ++i
) {
9219 pos
= decode_int (p
, &p
, end
);
9224 g_assert (pos
>= 0 && pos
< jit
->num_params
);
9226 t
= sig
->params
[pos
];
9227 var
= &jit
->params
[pos
];
9229 MonoDebugLocalsInfo
*locals
;
9231 locals
= mono_debug_lookup_locals (frame
->method
);
9233 g_assert (pos
< locals
->num_locals
);
9234 pos
= locals
->locals
[pos
].index
;
9235 mono_debug_free_locals (locals
);
9237 g_assert (pos
>= 0 && pos
< jit
->num_locals
);
9239 t
= header
->locals
[pos
];
9240 var
= &jit
->locals
[pos
];
9243 if (MONO_TYPE_IS_REFERENCE (t
))
9244 val_buf
= (guint8
*)g_alloca (sizeof (MonoObject
*));
9246 val_buf
= (guint8
*)g_alloca (mono_class_instance_size (mono_class_from_mono_type (t
)));
9247 err
= decode_value (t
, frame
->domain
, val_buf
, p
, &p
, end
);
9248 if (err
!= ERR_NONE
)
9251 set_var (t
, var
, &frame
->ctx
, frame
->domain
, val_buf
, frame
->reg_locations
, &tls
->restore_state
.ctx
);
9253 mono_metadata_free_mh (header
);
9256 case CMD_STACK_FRAME_GET_DOMAIN
: {
9257 if (CHECK_PROTOCOL_VERSION (2, 38))
9258 buffer_add_domainid (buf
, frame
->domain
);
9262 return ERR_NOT_IMPLEMENTED
;
9269 array_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9272 int objid
, index
, len
, i
, esize
;
9276 objid
= decode_objid (p
, &p
, end
);
9277 err
= get_object (objid
, (MonoObject
**)&arr
);
9278 if (err
!= ERR_NONE
)
9282 case CMD_ARRAY_REF_GET_LENGTH
:
9283 buffer_add_int (buf
, arr
->obj
.vtable
->klass
->rank
);
9285 buffer_add_int (buf
, arr
->max_length
);
9286 buffer_add_int (buf
, 0);
9288 for (i
= 0; i
< arr
->obj
.vtable
->klass
->rank
; ++i
) {
9289 buffer_add_int (buf
, arr
->bounds
[i
].length
);
9290 buffer_add_int (buf
, arr
->bounds
[i
].lower_bound
);
9294 case CMD_ARRAY_REF_GET_VALUES
:
9295 index
= decode_int (p
, &p
, end
);
9296 len
= decode_int (p
, &p
, end
);
9298 g_assert (index
>= 0 && len
>= 0);
9299 // Reordered to avoid integer overflow
9300 g_assert (!(index
> arr
->max_length
- len
));
9302 esize
= mono_array_element_size (arr
->obj
.vtable
->klass
);
9303 for (i
= index
; i
< index
+ len
; ++i
) {
9304 elem
= (gpointer
*)((char*)arr
->vector
+ (i
* esize
));
9305 buffer_add_value (buf
, &arr
->obj
.vtable
->klass
->element_class
->byval_arg
, elem
, arr
->obj
.vtable
->domain
);
9308 case CMD_ARRAY_REF_SET_VALUES
:
9309 index
= decode_int (p
, &p
, end
);
9310 len
= decode_int (p
, &p
, end
);
9312 g_assert (index
>= 0 && len
>= 0);
9313 // Reordered to avoid integer overflow
9314 g_assert (!(index
> arr
->max_length
- len
));
9316 esize
= mono_array_element_size (arr
->obj
.vtable
->klass
);
9317 for (i
= index
; i
< index
+ len
; ++i
) {
9318 elem
= (gpointer
*)((char*)arr
->vector
+ (i
* esize
));
9320 decode_value (&arr
->obj
.vtable
->klass
->element_class
->byval_arg
, arr
->obj
.vtable
->domain
, (guint8
*)elem
, p
, &p
, end
);
9324 return ERR_NOT_IMPLEMENTED
;
9331 string_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9337 int i
, index
, length
;
9339 gboolean use_utf16
= FALSE
;
9341 objid
= decode_objid (p
, &p
, end
);
9342 err
= get_object (objid
, (MonoObject
**)&str
);
9343 if (err
!= ERR_NONE
)
9347 case CMD_STRING_REF_GET_VALUE
:
9348 if (CHECK_PROTOCOL_VERSION (2, 41)) {
9349 for (i
= 0; i
< mono_string_length (str
); ++i
)
9350 if (mono_string_chars (str
)[i
] == 0)
9352 buffer_add_byte (buf
, use_utf16
? 1 : 0);
9355 buffer_add_int (buf
, mono_string_length (str
) * 2);
9356 buffer_add_data (buf
, (guint8
*)mono_string_chars (str
), mono_string_length (str
) * 2);
9359 s
= mono_string_to_utf8_checked (str
, &error
);
9360 mono_error_assert_ok (&error
);
9361 buffer_add_string (buf
, s
);
9365 case CMD_STRING_REF_GET_LENGTH
:
9366 buffer_add_long (buf
, mono_string_length (str
));
9368 case CMD_STRING_REF_GET_CHARS
:
9369 index
= decode_long (p
, &p
, end
);
9370 length
= decode_long (p
, &p
, end
);
9371 if (index
> mono_string_length (str
) - length
)
9372 return ERR_INVALID_ARGUMENT
;
9373 c
= mono_string_chars (str
) + index
;
9374 for (i
= 0; i
< length
; ++i
)
9375 buffer_add_short (buf
, c
[i
]);
9378 return ERR_NOT_IMPLEMENTED
;
9385 object_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9396 if (command
== CMD_OBJECT_REF_IS_COLLECTED
) {
9397 objid
= decode_objid (p
, &p
, end
);
9398 err
= get_object (objid
, &obj
);
9399 if (err
!= ERR_NONE
)
9400 buffer_add_int (buf
, 1);
9402 buffer_add_int (buf
, 0);
9406 objid
= decode_objid (p
, &p
, end
);
9407 err
= get_object (objid
, &obj
);
9408 if (err
!= ERR_NONE
)
9411 MonoClass
*obj_type
;
9412 gboolean remote_obj
= FALSE
;
9414 obj_type
= obj
->vtable
->klass
;
9415 if (mono_class_is_transparent_proxy (obj_type
)) {
9416 obj_type
= ((MonoTransparentProxy
*)obj
)->remote_class
->proxy_class
;
9420 g_assert (obj_type
);
9423 case CMD_OBJECT_REF_GET_TYPE
:
9424 /* This handles transparent proxies too */
9425 buffer_add_typeid (buf
, obj
->vtable
->domain
, mono_class_from_mono_type (((MonoReflectionType
*)obj
->vtable
->type
)->type
));
9427 case CMD_OBJECT_REF_GET_VALUES
:
9428 len
= decode_int (p
, &p
, end
);
9430 for (i
= 0; i
< len
; ++i
) {
9431 MonoClassField
*f
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
9432 if (err
!= ERR_NONE
)
9435 /* Check that the field belongs to the object */
9437 for (k
= obj_type
; k
; k
= k
->parent
) {
9438 if (k
== f
->parent
) {
9444 return ERR_INVALID_FIELDID
;
9446 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
) {
9450 if (mono_class_field_is_special_static (f
))
9451 return ERR_INVALID_FIELDID
;
9453 g_assert (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
);
9454 vtable
= mono_class_vtable (obj
->vtable
->domain
, f
->parent
);
9455 val
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type (f
->type
)));
9456 mono_field_static_get_value_checked (vtable
, f
, val
, &error
);
9457 if (!is_ok (&error
)) {
9458 mono_error_cleanup (&error
); /* FIXME report the error */
9459 return ERR_INVALID_OBJECT
;
9461 buffer_add_value (buf
, f
->type
, val
, obj
->vtable
->domain
);
9464 guint8
*field_value
= NULL
;
9465 void *field_storage
= NULL
;
9468 #ifndef DISABLE_REMOTING
9469 field_value
= mono_load_remote_field_checked(obj
, obj_type
, f
, &field_storage
, &error
);
9470 if (!is_ok (&error
)) {
9471 mono_error_cleanup (&error
); /* FIXME report the error */
9472 return ERR_INVALID_OBJECT
;
9475 g_assert_not_reached ();
9478 field_value
= (guint8
*)obj
+ f
->offset
;
9480 buffer_add_value (buf
, f
->type
, field_value
, obj
->vtable
->domain
);
9484 case CMD_OBJECT_REF_SET_VALUES
:
9485 len
= decode_int (p
, &p
, end
);
9487 for (i
= 0; i
< len
; ++i
) {
9488 f
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
9489 if (err
!= ERR_NONE
)
9492 /* Check that the field belongs to the object */
9494 for (k
= obj_type
; k
; k
= k
->parent
) {
9495 if (k
== f
->parent
) {
9501 return ERR_INVALID_FIELDID
;
9503 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
) {
9507 if (mono_class_field_is_special_static (f
))
9508 return ERR_INVALID_FIELDID
;
9510 g_assert (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
);
9511 vtable
= mono_class_vtable (obj
->vtable
->domain
, f
->parent
);
9513 val
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type (f
->type
)));
9514 err
= decode_value (f
->type
, obj
->vtable
->domain
, val
, p
, &p
, end
);
9515 if (err
!= ERR_NONE
) {
9519 mono_field_static_set_value (vtable
, f
, val
);
9522 err
= decode_value (f
->type
, obj
->vtable
->domain
, (guint8
*)obj
+ f
->offset
, p
, &p
, end
);
9523 if (err
!= ERR_NONE
)
9528 case CMD_OBJECT_REF_GET_ADDRESS
:
9529 buffer_add_long (buf
, (gssize
)obj
);
9531 case CMD_OBJECT_REF_GET_DOMAIN
:
9532 buffer_add_domainid (buf
, obj
->vtable
->domain
);
9534 case CMD_OBJECT_REF_GET_INFO
:
9535 buffer_add_typeid (buf
, obj
->vtable
->domain
, mono_class_from_mono_type (((MonoReflectionType
*)obj
->vtable
->type
)->type
));
9536 buffer_add_domainid (buf
, obj
->vtable
->domain
);
9539 return ERR_NOT_IMPLEMENTED
;
9546 command_set_to_string (CommandSet command_set
)
9548 switch (command_set
) {
9551 case CMD_SET_OBJECT_REF
:
9552 return "OBJECT_REF";
9553 case CMD_SET_STRING_REF
:
9554 return "STRING_REF";
9555 case CMD_SET_THREAD
:
9557 case CMD_SET_ARRAY_REF
:
9559 case CMD_SET_EVENT_REQUEST
:
9560 return "EVENT_REQUEST";
9561 case CMD_SET_STACK_FRAME
:
9562 return "STACK_FRAME";
9563 case CMD_SET_APPDOMAIN
:
9565 case CMD_SET_ASSEMBLY
:
9567 case CMD_SET_METHOD
:
9571 case CMD_SET_MODULE
:
9582 static const char* vm_cmds_str
[] = {
9590 "SET_PROTOCOL_VERSION",
9593 "GET_TYPES_FOR_SOURCE_FILE",
9598 static const char* thread_cmds_str
[] = {
9608 static const char* event_cmds_str
[] = {
9611 "REQUEST_CLEAR_ALL_BREAKPOINTS"
9614 static const char* appdomain_cmds_str
[] = {
9616 "GET_FRIENDLY_NAME",
9618 "GET_ENTRY_ASSEMBLY",
9621 "CREATE_BOXED_VALUE"
9624 static const char* assembly_cmds_str
[] = {
9627 "GET_MANIFEST_MODULE",
9633 static const char* module_cmds_str
[] = {
9637 static const char* field_cmds_str
[] = {
9641 static const char* method_cmds_str
[] = {
9643 "GET_DECLARING_TYPE",
9651 "MAKE_GENERIC_METHOD"
9654 static const char* type_cmds_str
[] = {
9662 "IS_ASSIGNABLE_FROM",
9666 "GET_PROPERTY_CATTRS",
9667 "GET_SOURCE_FILES_2",
9669 "GET_METHODS_BY_NAME_FLAGS",
9671 "GET_INTERFACE_MAP",
9675 static const char* stack_frame_cmds_str
[] = {
9682 static const char* array_cmds_str
[] = {
9688 static const char* string_cmds_str
[] = {
9694 static const char* object_cmds_str
[] = {
9705 cmd_to_string (CommandSet set
, int command
)
9713 cmds_len
= G_N_ELEMENTS (vm_cmds_str
);
9715 case CMD_SET_OBJECT_REF
:
9716 cmds
= object_cmds_str
;
9717 cmds_len
= G_N_ELEMENTS (object_cmds_str
);
9719 case CMD_SET_STRING_REF
:
9720 cmds
= string_cmds_str
;
9721 cmds_len
= G_N_ELEMENTS (string_cmds_str
);
9723 case CMD_SET_THREAD
:
9724 cmds
= thread_cmds_str
;
9725 cmds_len
= G_N_ELEMENTS (thread_cmds_str
);
9727 case CMD_SET_ARRAY_REF
:
9728 cmds
= array_cmds_str
;
9729 cmds_len
= G_N_ELEMENTS (array_cmds_str
);
9731 case CMD_SET_EVENT_REQUEST
:
9732 cmds
= event_cmds_str
;
9733 cmds_len
= G_N_ELEMENTS (event_cmds_str
);
9735 case CMD_SET_STACK_FRAME
:
9736 cmds
= stack_frame_cmds_str
;
9737 cmds_len
= G_N_ELEMENTS (stack_frame_cmds_str
);
9739 case CMD_SET_APPDOMAIN
:
9740 cmds
= appdomain_cmds_str
;
9741 cmds_len
= G_N_ELEMENTS (appdomain_cmds_str
);
9743 case CMD_SET_ASSEMBLY
:
9744 cmds
= assembly_cmds_str
;
9745 cmds_len
= G_N_ELEMENTS (assembly_cmds_str
);
9747 case CMD_SET_METHOD
:
9748 cmds
= method_cmds_str
;
9749 cmds_len
= G_N_ELEMENTS (method_cmds_str
);
9752 cmds
= type_cmds_str
;
9753 cmds_len
= G_N_ELEMENTS (type_cmds_str
);
9755 case CMD_SET_MODULE
:
9756 cmds
= module_cmds_str
;
9757 cmds_len
= G_N_ELEMENTS (module_cmds_str
);
9760 cmds
= field_cmds_str
;
9761 cmds_len
= G_N_ELEMENTS (field_cmds_str
);
9764 cmds
= event_cmds_str
;
9765 cmds_len
= G_N_ELEMENTS (event_cmds_str
);
9770 if (command
> 0 && command
<= cmds_len
)
9771 return cmds
[command
- 1];
9777 wait_for_attach (void)
9779 #ifndef DISABLE_SOCKET_TRANSPORT
9780 if (listen_fd
== -1) {
9781 DEBUG_PRINTF (1, "[dbg] Invalid listening socket\n");
9785 /* Block and wait for client connection */
9786 conn_fd
= socket_transport_accept (listen_fd
);
9788 DEBUG_PRINTF (1, "Accepted connection on %d\n", conn_fd
);
9789 if (conn_fd
== -1) {
9790 DEBUG_PRINTF (1, "[dbg] Bad client connection\n");
9794 g_assert_not_reached ();
9798 disconnected
= !transport_handshake ();
9800 DEBUG_PRINTF (1, "Transport handshake failed!\n");
9810 * This thread handles communication with the debugger client using a JDWP
9813 static guint32 WINAPI
9814 debugger_thread (void *arg
)
9817 int res
, len
, id
, flags
, command
= 0;
9818 CommandSet command_set
= (CommandSet
)0;
9819 guint8 header
[HEADER_LENGTH
];
9820 guint8
*data
, *p
, *end
;
9824 gboolean attach_failed
= FALSE
;
9826 DEBUG_PRINTF (1, "[dbg] Agent thread started, pid=%p\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
9828 debugger_thread_id
= mono_native_thread_id_get ();
9830 MonoThread
*thread
= mono_thread_attach (mono_get_root_domain ());
9831 mono_thread_set_name_internal (thread
->internal_thread
, mono_string_new (mono_get_root_domain (), "Debugger agent"), TRUE
, &error
);
9832 mono_error_assert_ok (&error
);
9834 thread
->internal_thread
->state
|= ThreadState_Background
;
9835 thread
->internal_thread
->flags
|= MONO_THREAD_FLAG_DONT_MANAGE
;
9837 mono_set_is_debugger_attached (TRUE
);
9839 if (agent_config
.defer
) {
9840 if (!wait_for_attach ()) {
9841 DEBUG_PRINTF (1, "[dbg] Can't attach, aborting debugger thread.\n");
9842 attach_failed
= TRUE
; // Don't abort process when we can't listen
9844 /* Send start event to client */
9845 process_profiler_event (EVENT_KIND_VM_START
, mono_thread_get_main ());
9849 while (!attach_failed
) {
9850 res
= transport_recv (header
, HEADER_LENGTH
);
9852 /* This will break if the socket is closed during shutdown too */
9853 if (res
!= HEADER_LENGTH
) {
9854 DEBUG_PRINTF (1, "[dbg] transport_recv () returned %d, expected %d.\n", res
, HEADER_LENGTH
);
9859 end
= header
+ HEADER_LENGTH
;
9861 len
= decode_int (p
, &p
, end
);
9862 id
= decode_int (p
, &p
, end
);
9863 flags
= decode_byte (p
, &p
, end
);
9864 command_set
= (CommandSet
)decode_byte (p
, &p
, end
);
9865 command
= decode_byte (p
, &p
, end
);
9867 g_assert (flags
== 0);
9870 const char *cmd_str
;
9873 cmd_str
= cmd_to_string (command_set
, command
);
9875 sprintf (cmd_num
, "%d", command
);
9879 DEBUG_PRINTF (1, "[dbg] Command %s(%s) [%d][at=%lx].\n", command_set_to_string (command_set
), cmd_str
, id
, (long)mono_100ns_ticks () / 10000);
9882 data
= (guint8
*)g_malloc (len
- HEADER_LENGTH
);
9883 if (len
- HEADER_LENGTH
> 0)
9885 res
= transport_recv (data
, len
- HEADER_LENGTH
);
9886 if (res
!= len
- HEADER_LENGTH
) {
9887 DEBUG_PRINTF (1, "[dbg] transport_recv () returned %d, expected %d.\n", res
, len
- HEADER_LENGTH
);
9893 end
= data
+ (len
- HEADER_LENGTH
);
9895 buffer_init (&buf
, 128);
9900 /* Process the request */
9901 switch (command_set
) {
9903 err
= vm_commands (command
, id
, p
, end
, &buf
);
9904 if (err
== ERR_NONE
&& command
== CMD_VM_INVOKE_METHOD
)
9905 /* Sent after the invoke is complete */
9908 case CMD_SET_EVENT_REQUEST
:
9909 err
= event_commands (command
, p
, end
, &buf
);
9911 case CMD_SET_APPDOMAIN
:
9912 err
= domain_commands (command
, p
, end
, &buf
);
9914 case CMD_SET_ASSEMBLY
:
9915 err
= assembly_commands (command
, p
, end
, &buf
);
9917 case CMD_SET_MODULE
:
9918 err
= module_commands (command
, p
, end
, &buf
);
9921 err
= field_commands (command
, p
, end
, &buf
);
9924 err
= type_commands (command
, p
, end
, &buf
);
9926 case CMD_SET_METHOD
:
9927 err
= method_commands (command
, p
, end
, &buf
);
9929 case CMD_SET_THREAD
:
9930 err
= thread_commands (command
, p
, end
, &buf
);
9932 case CMD_SET_STACK_FRAME
:
9933 err
= frame_commands (command
, p
, end
, &buf
);
9935 case CMD_SET_ARRAY_REF
:
9936 err
= array_commands (command
, p
, end
, &buf
);
9938 case CMD_SET_STRING_REF
:
9939 err
= string_commands (command
, p
, end
, &buf
);
9941 case CMD_SET_OBJECT_REF
:
9942 err
= object_commands (command
, p
, end
, &buf
);
9945 err
= ERR_NOT_IMPLEMENTED
;
9948 if (command_set
== CMD_SET_VM
&& command
== CMD_VM_START_BUFFERING
) {
9949 buffer_replies
= TRUE
;
9953 if (buffer_replies
) {
9954 buffer_reply_packet (id
, err
, &buf
);
9956 send_reply_packet (id
, err
, &buf
);
9957 //DEBUG_PRINTF (1, "[dbg] Sent reply to %d [at=%lx].\n", id, (long)mono_100ns_ticks () / 10000);
9961 if (err
== ERR_NONE
&& command_set
== CMD_SET_VM
&& command
== CMD_VM_STOP_BUFFERING
) {
9962 send_buffered_reply_packets ();
9963 buffer_replies
= FALSE
;
9969 if (command_set
== CMD_SET_VM
&& (command
== CMD_VM_DISPOSE
|| command
== CMD_VM_EXIT
))
9973 mono_set_is_debugger_attached (FALSE
);
9975 mono_coop_mutex_lock (&debugger_thread_exited_mutex
);
9976 debugger_thread_exited
= TRUE
;
9977 mono_coop_cond_signal (&debugger_thread_exited_cond
);
9978 mono_coop_mutex_unlock (&debugger_thread_exited_mutex
);
9980 DEBUG_PRINTF (1, "[dbg] Debugger thread exited.\n");
9982 if (!attach_failed
&& command_set
== CMD_SET_VM
&& command
== CMD_VM_DISPOSE
&& !(vm_death_event_sent
|| mono_runtime_is_shutting_down ())) {
9983 DEBUG_PRINTF (2, "[dbg] Detached - restarting clean debugger thread.\n");
9984 start_debugger_thread ();
9990 #else /* DISABLE_DEBUGGER_AGENT */
9993 mono_debugger_agent_parse_options (char *options
)
9995 g_error ("This runtime is configured with the debugger agent disabled.");
9999 mono_debugger_agent_init (void)
10004 mono_debugger_agent_breakpoint_hit (void *sigctx
)
10009 mono_debugger_agent_single_step_event (void *sigctx
)
10014 mono_debugger_agent_free_domain_info (MonoDomain
*domain
)
10019 mono_debugger_agent_handle_exception (MonoException
*ext
, MonoContext
*throw_ctx
,
10020 MonoContext
*catch_ctx
)
10025 mono_debugger_agent_begin_exception_filter (MonoException
*exc
, MonoContext
*ctx
, MonoContext
*orig_ctx
)
10030 mono_debugger_agent_end_exception_filter (MonoException
*exc
, MonoContext
*ctx
, MonoContext
*orig_ctx
)
10035 mono_debugger_agent_user_break (void)
10041 mono_debugger_agent_debug_log (int level
, MonoString
*category
, MonoString
*message
)
10046 mono_debugger_agent_debug_log_is_enabled (void)
10052 mono_debugger_agent_unhandled_exception (MonoException
*exc
)
10054 g_assert_not_reached ();
10058 debugger_agent_single_step_from_context (MonoContext
*ctx
)
10060 g_assert_not_reached ();
10064 debugger_agent_breakpoint_from_context (MonoContext
*ctx
)
10066 g_assert_not_reached ();