3 * Soft Debugger back-end module
6 * Zoltan Varga (vargaz@gmail.com)
8 * Copyright 2009-2010 Novell, Inc.
9 * Copyright 2011 Xamarin Inc.
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
20 #ifdef HAVE_SYS_SELECT_H
21 #include <sys/select.h>
23 #ifdef HAVE_SYS_SOCKET_H
24 #include <sys/socket.h>
26 #ifdef HAVE_NETINET_TCP_H
27 #include <netinet/tcp.h>
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
53 #include <linux/tcp.h>
54 #include <sys/endian.h>
57 #include <mono/metadata/mono-debug.h>
58 #include <mono/metadata/debug-internals.h>
59 #include <mono/metadata/domain-internals.h>
60 #include <mono/metadata/gc-internals.h>
61 #include <mono/metadata/environment.h>
62 #include <mono/metadata/mono-hash-internals.h>
63 #include <mono/metadata/threads-types.h>
64 #include <mono/metadata/threadpool.h>
65 #include <mono/metadata/assembly.h>
66 #include <mono/metadata/assembly-internals.h>
67 #include <mono/metadata/runtime.h>
68 #include <mono/metadata/verify-internals.h>
69 #include <mono/metadata/reflection-internals.h>
70 #include <mono/metadata/w32socket.h>
71 #include <mono/utils/mono-coop-mutex.h>
72 #include <mono/utils/mono-coop-semaphore.h>
73 #include <mono/utils/mono-error-internals.h>
74 #include <mono/utils/mono-stack-unwinding.h>
75 #include <mono/utils/mono-time.h>
76 #include <mono/utils/mono-threads.h>
77 #include <mono/utils/networking.h>
78 #include <mono/utils/mono-proclib.h>
79 #include <mono/utils/w32api.h>
80 #include <mono/utils/mono-logger-internals.h>
81 #include "debugger-state-machine.h"
82 #include "debugger-agent.h"
84 #include "seq-points.h"
85 #include "aot-runtime.h"
86 #include "mini-runtime.h"
87 #include "interp/interp.h"
88 #include "debugger-engine.h"
89 #include "mono/metadata/debug-mono-ppdb.h"
90 #include "mono/metadata/custom-attrs-internals.h"
93 * On iOS we can't use System.Environment.Exit () as it will do the wrong
96 #if !defined (TARGET_IOS)
97 #define TRY_MANAGED_SYSTEM_ENVIRONMENT_EXIT
101 #define DISABLE_SOCKET_TRANSPORT
106 #include <mono/utils/mono-os-mutex.h>
109 #include <sys/stat.h>
112 #define S_IWUSR S_IWRITE
115 #define THREAD_TO_INTERNAL(thread) (thread)->internal_thread
137 //Must be the first field to ensure pointer equivalence
138 DbgEngineStackFrame de
;
142 * If method is gshared, this is the actual instance, otherwise this is equal to
145 MonoMethod
*actual_method
;
147 * This is the method which is visible to debugger clients. Same as method,
148 * except for native-to-managed wrappers.
150 MonoMethod
*api_method
;
152 MonoDebugMethodJitInfo
*jit
;
153 MonoInterpFrameHandle interp_frame
;
156 host_mgreg_t
*reg_locations
[MONO_MAX_IREGS
];
158 * Whenever ctx is set. This is FALSE for the last frame of running threads, since
159 * the frame can become invalid.
164 typedef struct _InvokeData InvokeData
;
172 /* This is the context which needs to be restored after the invoke */
176 * If this is set, invoke this method with the arguments given by ARGS.
180 guint32 suspend_count
;
183 InvokeData
*last_invoke
;
186 struct _DebuggerTlsData
{
187 MonoThreadUnwindState context
;
189 /* This is computed on demand when it is requested using the wire protocol */
190 /* It is freed up when the thread is resumed */
194 * Whenever the frame info is up-to-date. If not, compute_frame_info () will need to
197 gboolean frames_up_to_date
;
199 * Points to data about a pending invoke which needs to be executed after the thread
202 InvokeData
*pending_invoke
;
204 * Set to TRUE if this thread is suspended in suspend_current () or it is executing
209 * Signals whenever the thread is in the process of suspending, i.e. it will suspend
210 * within a finite amount of time.
214 * Set to TRUE if this thread is suspended in suspend_current ().
216 gboolean really_suspended
;
217 /* Used to pass the context to the breakpoint/single step handler */
218 MonoContext handler_ctx
;
219 /* Whenever thread_stop () was called for this thread */
222 /* Whenever to disable breakpoints (used during invokes) */
223 gboolean disable_breakpoints
;
226 * Number of times this thread has been resumed using resume_thread ().
228 guint32 resume_count
;
229 guint32 resume_count_internal
;
230 guint32 suspend_count
;
232 MonoInternalThread
*thread
;
236 * Information about the frame which transitioned to native code for running
239 StackFrameInfo async_last_frame
;
242 * The context where the stack walk can be started for running threads.
244 MonoThreadUnwindState async_state
;
247 * The context used for filter clauses
249 MonoThreadUnwindState filter_state
;
251 gboolean abort_requested
;
254 * The current mono_runtime_invoke_checked invocation.
258 StackFrameInfo catch_frame
;
259 gboolean has_catch_frame
;
262 * The context which needs to be restored after handling a single step/breakpoint
263 * event. This is the same as the ctx at step/breakpoint site, but includes changes
264 * to caller saved registers done by set_var ().
266 MonoThreadUnwindState restore_state
;
267 /* Frames computed from restore_state */
268 int restore_frame_count
;
269 StackFrame
**restore_frames
;
271 /* The currently unloading appdomain */
272 MonoDomain
*domain_unloading
;
274 // The state that the debugger expects the thread to be in
275 MonoDebuggerThreadState thread_state
;
276 MonoStopwatch step_time
;
278 gboolean gc_finalizing
;
283 void (*connect
) (const char *address
);
284 void (*close1
) (void);
285 void (*close2
) (void);
286 gboolean (*send
) (void *buf
, int len
);
287 int (*recv
) (void *buf
, int len
);
291 * Wire Protocol definitions
294 #define HEADER_LENGTH 11
296 #define MAJOR_VERSION 2
297 #define MINOR_VERSION 55
301 CMD_SET_OBJECT_REF
= 9,
302 CMD_SET_STRING_REF
= 10,
304 CMD_SET_ARRAY_REF
= 13,
305 CMD_SET_EVENT_REQUEST
= 15,
306 CMD_SET_STACK_FRAME
= 16,
307 CMD_SET_APPDOMAIN
= 20,
308 CMD_SET_ASSEMBLY
= 21,
318 SUSPEND_POLICY_NONE
= 0,
319 SUSPEND_POLICY_EVENT_THREAD
= 1,
320 SUSPEND_POLICY_ALL
= 2
325 ERR_INVALID_OBJECT
= 20,
326 ERR_INVALID_FIELDID
= 25,
327 ERR_INVALID_FRAMEID
= 30,
328 ERR_NOT_IMPLEMENTED
= 100,
329 ERR_NOT_SUSPENDED
= 101,
330 ERR_INVALID_ARGUMENT
= 102,
332 ERR_NO_INVOCATION
= 104,
333 ERR_ABSENT_INFORMATION
= 105,
334 ERR_NO_SEQ_POINT_AT_IL_OFFSET
= 106,
335 ERR_INVOKE_ABORTED
= 107,
336 ERR_LOADER_ERROR
= 200, /*XXX extend the protocol to pass this information down the pipe */
340 TOKEN_TYPE_STRING
= 0,
342 TOKEN_TYPE_FIELD
= 2,
343 TOKEN_TYPE_METHOD
= 3,
344 TOKEN_TYPE_UNKNOWN
= 4
348 VALUE_TYPE_ID_NULL
= 0xf0,
349 VALUE_TYPE_ID_TYPE
= 0xf1,
350 VALUE_TYPE_ID_PARENT_VTYPE
= 0xf2,
351 VALUE_TYPE_ID_FIXED_ARRAY
= 0xf3
355 FRAME_FLAG_DEBUGGER_INVOKE
= 1,
356 FRAME_FLAG_NATIVE_TRANSITION
= 2
360 INVOKE_FLAG_DISABLE_BREAKPOINTS
= 1,
361 INVOKE_FLAG_SINGLE_THREADED
= 2,
362 INVOKE_FLAG_RETURN_OUT_THIS
= 4,
363 INVOKE_FLAG_RETURN_OUT_ARGS
= 8,
364 INVOKE_FLAG_VIRTUAL
= 16
368 BINDING_FLAGS_IGNORE_CASE
= 0x70000000,
369 } BindingFlagsExtensions
;
373 CMD_VM_ALL_THREADS
= 2,
378 CMD_VM_INVOKE_METHOD
= 7,
379 CMD_VM_SET_PROTOCOL_VERSION
= 8,
380 CMD_VM_ABORT_INVOKE
= 9,
381 CMD_VM_SET_KEEPALIVE
= 10,
382 CMD_VM_GET_TYPES_FOR_SOURCE_FILE
= 11,
383 CMD_VM_GET_TYPES
= 12,
384 CMD_VM_INVOKE_METHODS
= 13,
385 CMD_VM_START_BUFFERING
= 14,
386 CMD_VM_STOP_BUFFERING
= 15
390 CMD_THREAD_GET_FRAME_INFO
= 1,
391 CMD_THREAD_GET_NAME
= 2,
392 CMD_THREAD_GET_STATE
= 3,
393 CMD_THREAD_GET_INFO
= 4,
394 CMD_THREAD_GET_ID
= 5,
395 CMD_THREAD_GET_TID
= 6,
396 CMD_THREAD_SET_IP
= 7,
397 CMD_THREAD_ELAPSED_TIME
= 8
401 CMD_EVENT_REQUEST_SET
= 1,
402 CMD_EVENT_REQUEST_CLEAR
= 2,
403 CMD_EVENT_REQUEST_CLEAR_ALL_BREAKPOINTS
= 3
411 CMD_APPDOMAIN_GET_ROOT_DOMAIN
= 1,
412 CMD_APPDOMAIN_GET_FRIENDLY_NAME
= 2,
413 CMD_APPDOMAIN_GET_ASSEMBLIES
= 3,
414 CMD_APPDOMAIN_GET_ENTRY_ASSEMBLY
= 4,
415 CMD_APPDOMAIN_CREATE_STRING
= 5,
416 CMD_APPDOMAIN_GET_CORLIB
= 6,
417 CMD_APPDOMAIN_CREATE_BOXED_VALUE
= 7,
418 CMD_APPDOMAIN_CREATE_BYTE_ARRAY
= 8,
422 CMD_ASSEMBLY_GET_LOCATION
= 1,
423 CMD_ASSEMBLY_GET_ENTRY_POINT
= 2,
424 CMD_ASSEMBLY_GET_MANIFEST_MODULE
= 3,
425 CMD_ASSEMBLY_GET_OBJECT
= 4,
426 CMD_ASSEMBLY_GET_TYPE
= 5,
427 CMD_ASSEMBLY_GET_NAME
= 6,
428 CMD_ASSEMBLY_GET_DOMAIN
= 7,
429 CMD_ASSEMBLY_GET_METADATA_BLOB
= 8,
430 CMD_ASSEMBLY_GET_IS_DYNAMIC
= 9,
431 CMD_ASSEMBLY_GET_PDB_BLOB
= 10,
432 CMD_ASSEMBLY_GET_TYPE_FROM_TOKEN
= 11,
433 CMD_ASSEMBLY_GET_METHOD_FROM_TOKEN
= 12,
434 CMD_ASSEMBLY_HAS_DEBUG_INFO
= 13
438 CMD_MODULE_GET_INFO
= 1,
442 CMD_FIELD_GET_INFO
= 1,
446 CMD_METHOD_GET_NAME
= 1,
447 CMD_METHOD_GET_DECLARING_TYPE
= 2,
448 CMD_METHOD_GET_DEBUG_INFO
= 3,
449 CMD_METHOD_GET_PARAM_INFO
= 4,
450 CMD_METHOD_GET_LOCALS_INFO
= 5,
451 CMD_METHOD_GET_INFO
= 6,
452 CMD_METHOD_GET_BODY
= 7,
453 CMD_METHOD_RESOLVE_TOKEN
= 8,
454 CMD_METHOD_GET_CATTRS
= 9,
455 CMD_METHOD_MAKE_GENERIC_METHOD
= 10
459 CMD_TYPE_GET_INFO
= 1,
460 CMD_TYPE_GET_METHODS
= 2,
461 CMD_TYPE_GET_FIELDS
= 3,
462 CMD_TYPE_GET_VALUES
= 4,
463 CMD_TYPE_GET_OBJECT
= 5,
464 CMD_TYPE_GET_SOURCE_FILES
= 6,
465 CMD_TYPE_SET_VALUES
= 7,
466 CMD_TYPE_IS_ASSIGNABLE_FROM
= 8,
467 CMD_TYPE_GET_PROPERTIES
= 9,
468 CMD_TYPE_GET_CATTRS
= 10,
469 CMD_TYPE_GET_FIELD_CATTRS
= 11,
470 CMD_TYPE_GET_PROPERTY_CATTRS
= 12,
471 CMD_TYPE_GET_SOURCE_FILES_2
= 13,
472 CMD_TYPE_GET_VALUES_2
= 14,
473 CMD_TYPE_GET_METHODS_BY_NAME_FLAGS
= 15,
474 CMD_TYPE_GET_INTERFACES
= 16,
475 CMD_TYPE_GET_INTERFACE_MAP
= 17,
476 CMD_TYPE_IS_INITIALIZED
= 18,
477 CMD_TYPE_CREATE_INSTANCE
= 19,
478 CMD_TYPE_GET_VALUE_SIZE
= 20
482 CMD_STACK_FRAME_GET_VALUES
= 1,
483 CMD_STACK_FRAME_GET_THIS
= 2,
484 CMD_STACK_FRAME_SET_VALUES
= 3,
485 CMD_STACK_FRAME_GET_DOMAIN
= 4,
486 CMD_STACK_FRAME_SET_THIS
= 5,
490 CMD_ARRAY_REF_GET_LENGTH
= 1,
491 CMD_ARRAY_REF_GET_VALUES
= 2,
492 CMD_ARRAY_REF_SET_VALUES
= 3,
496 CMD_STRING_REF_GET_VALUE
= 1,
497 CMD_STRING_REF_GET_LENGTH
= 2,
498 CMD_STRING_REF_GET_CHARS
= 3
502 CMD_POINTER_GET_VALUE
= 1
506 CMD_OBJECT_REF_GET_TYPE
= 1,
507 CMD_OBJECT_REF_GET_VALUES
= 2,
508 CMD_OBJECT_REF_IS_COLLECTED
= 3,
509 CMD_OBJECT_REF_GET_ADDRESS
= 4,
510 CMD_OBJECT_REF_GET_DOMAIN
= 5,
511 CMD_OBJECT_REF_SET_VALUES
= 6,
512 CMD_OBJECT_REF_GET_INFO
= 7,
516 * Contains additional information for an event
519 /* For EVENT_KIND_EXCEPTION */
521 MonoContext catch_ctx
;
523 /* For EVENT_KIND_USER_LOG */
525 char *category
, *message
;
526 /* For EVENT_KIND_TYPE_LOAD */
528 /* For EVENT_KIND_CRASH */
530 MonoStackHash
*hashes
;
534 guint8
*buf
, *p
, *end
;
537 typedef struct ReplyPacket
{
543 #define DEBUG(level,s) do { if (G_UNLIKELY ((level) <= log_level)) { s; fflush (log_file); } } while (0)
546 #define DEBUG_PRINTF(level, ...) do { if (G_UNLIKELY ((level) <= log_level)) { g_print (__VA_ARGS__); } } while (0)
548 #define DEBUG_PRINTF(level, ...) do { if (G_UNLIKELY ((level) <= log_level)) { fprintf (log_file, __VA_ARGS__); fflush (log_file); } } while (0)
552 #define get_last_sock_error() WSAGetLastError()
553 #define MONO_EWOULDBLOCK WSAEWOULDBLOCK
554 #define MONO_EINTR WSAEINTR
556 #define get_last_sock_error() errno
557 #define MONO_EWOULDBLOCK EWOULDBLOCK
558 #define MONO_EINTR EINTR
561 #define CHECK_PROTOCOL_VERSION(major,minor) \
562 (protocol_version_set && (major_version > (major) || (major_version == (major) && minor_version >= (minor))))
568 static AgentConfig agent_config
;
571 * Whenever the agent is fully initialized.
572 * When using the onuncaught or onthrow options, only some parts of the agent are
573 * initialized on startup, and the full initialization which includes connection
574 * establishment and the startup of the agent thread is only done in response to
577 static gint32 inited
;
579 #ifndef DISABLE_SOCKET_TRANSPORT
581 static int listen_fd
;
584 static int packet_id
= 0;
586 static int objref_id
= 0;
588 static int event_request_id
= 0;
590 static int frame_id
= 0;
592 static GPtrArray
*event_requests
;
594 static MonoNativeTlsKey debugger_tls_id
;
596 static gboolean vm_start_event_sent
, vm_death_event_sent
, disconnected
;
598 /* Maps MonoInternalThread -> DebuggerTlsData */
599 /* Protected by the loader lock */
600 static MonoGHashTable
*thread_to_tls
;
602 /* Maps tid -> MonoInternalThread */
603 /* Protected by the loader lock */
604 static MonoGHashTable
*tid_to_thread
;
606 /* Maps tid -> MonoThread (not MonoInternalThread) */
607 /* Protected by the loader lock */
608 static MonoGHashTable
*tid_to_thread_obj
;
610 static MonoNativeThreadId debugger_thread_id
;
612 static MonoThreadHandle
*debugger_thread_handle
;
614 static int log_level
;
616 static int file_check_valid_memory
= -1;
618 static char* filename_check_valid_memory
;
620 static gboolean embedding
;
622 static FILE *log_file
;
624 /* Assemblies whose assembly load event has no been sent yet */
625 /* Protected by the dbg lock */
626 static GPtrArray
*pending_assembly_loads
;
628 /* Whenever the debugger thread has exited */
629 static gboolean debugger_thread_exited
;
631 /* Cond variable used to wait for debugger_thread_exited becoming true */
632 static MonoCoopCond debugger_thread_exited_cond
;
634 /* Mutex for the cond var above */
635 static MonoCoopMutex debugger_thread_exited_mutex
;
637 /* The protocol version of the client */
638 static int major_version
, minor_version
;
640 /* Whenever the variables above are set by the client */
641 static gboolean protocol_version_set
;
643 /* The number of times the runtime is suspended */
644 static gint32 suspend_count
;
646 /* Whenever to buffer reply messages and send them together */
647 static gboolean buffer_replies
;
649 /* Buffered reply packets */
650 static ReplyPacket reply_packets
[128];
651 static int nreply_packets
;
653 #define dbg_lock mono_de_lock
654 #define dbg_unlock mono_de_unlock
656 static void transport_init (void);
657 static void transport_connect (const char *address
);
658 static gboolean
transport_handshake (void);
659 static void register_transport (DebuggerTransport
*trans
);
661 static gsize WINAPI
debugger_thread (void *arg
);
663 static void runtime_initialized (MonoProfiler
*prof
);
665 static void runtime_shutdown (MonoProfiler
*prof
);
667 static void thread_startup (MonoProfiler
*prof
, uintptr_t tid
);
669 static void thread_end (MonoProfiler
*prof
, uintptr_t tid
);
671 static void appdomain_load (MonoProfiler
*prof
, MonoDomain
*domain
);
673 static void appdomain_start_unload (MonoProfiler
*prof
, MonoDomain
*domain
);
675 static void appdomain_unload (MonoProfiler
*prof
, MonoDomain
*domain
);
677 static void emit_appdomain_load (gpointer key
, gpointer value
, gpointer user_data
);
679 static void emit_thread_start (gpointer key
, gpointer value
, gpointer user_data
);
681 static void invalidate_each_thread (gpointer key
, gpointer value
, gpointer user_data
);
683 static void assembly_load (MonoProfiler
*prof
, MonoAssembly
*assembly
);
685 static void assembly_unload (MonoProfiler
*prof
, MonoAssembly
*assembly
);
687 static void gc_finalizing (MonoProfiler
*prof
);
689 static void gc_finalized (MonoProfiler
*prof
);
691 static void emit_assembly_load (gpointer assembly
, gpointer user_data
);
693 static void emit_type_load (gpointer key
, gpointer type
, gpointer user_data
);
695 static void jit_done (MonoProfiler
*prof
, MonoMethod
*method
, MonoJitInfo
*jinfo
);
697 static void jit_failed (MonoProfiler
*prof
, MonoMethod
*method
);
699 static void jit_end (MonoProfiler
*prof
, MonoMethod
*method
, MonoJitInfo
*jinfo
);
701 static void suspend_current (void);
703 static void clear_event_requests_for_assembly (MonoAssembly
*assembly
);
705 static void clear_types_for_assembly (MonoAssembly
*assembly
);
707 static void process_profiler_event (EventKind event
, gpointer arg
);
709 /* Submodule init/cleanup */
710 static void event_requests_cleanup (void);
712 static void objrefs_init (void);
713 static void objrefs_cleanup (void);
715 static void ids_init (void);
716 static void ids_cleanup (void);
718 static void suspend_init (void);
720 static void start_debugger_thread (MonoError
*error
);
721 static void stop_debugger_thread (void);
723 static void finish_agent_init (gboolean on_startup
);
725 static void process_profiler_event (EventKind event
, gpointer arg
);
727 static void invalidate_frames (DebuggerTlsData
*tls
);
729 /* Callbacks used by debugger-engine */
730 static MonoContext
* tls_get_restore_state (void *the_tls
);
731 static gboolean
try_process_suspend (void *tls
, MonoContext
*ctx
, gboolean from_breakpoint
);
732 static gboolean
begin_breakpoint_processing (void *tls
, MonoContext
*ctx
, MonoJitInfo
*ji
, gboolean from_signal
);
733 static void begin_single_step_processing (MonoContext
*ctx
, gboolean from_signal
);
734 static void ss_discard_frame_context (void *the_tls
);
735 static void ss_calculate_framecount (void *tls
, MonoContext
*ctx
, gboolean force_use_ctx
, DbgEngineStackFrame
***frames
, int *nframes
);
736 static gboolean
ensure_jit (DbgEngineStackFrame
* the_frame
);
737 static int ensure_runtime_is_suspended (void);
738 static int get_this_async_id (DbgEngineStackFrame
*frame
);
739 static gboolean
set_set_notification_for_wait_completion_flag (DbgEngineStackFrame
*frame
);
740 static MonoMethod
* get_notify_debugger_of_wait_completion_method (void);
741 static void* create_breakpoint_events (GPtrArray
*ss_reqs
, GPtrArray
*bp_reqs
, MonoJitInfo
*ji
, EventKind kind
);
742 static void process_breakpoint_events (void *_evts
, MonoMethod
*method
, MonoContext
*ctx
, int il_offset
);
743 static int ss_create_init_args (SingleStepReq
*ss_req
, SingleStepArgs
*args
);
744 static void ss_args_destroy (SingleStepArgs
*ss_args
);
745 static int handle_multiple_ss_requests (void);
747 static GENERATE_TRY_GET_CLASS_WITH_CACHE (fixed_buffer
, "System.Runtime.CompilerServices", "FixedBufferAttribute")
749 #ifndef DISABLE_SOCKET_TRANSPORT
751 register_socket_transport (void);
755 is_debugger_thread (void)
757 MonoInternalThread
*internal
;
759 internal
= mono_thread_internal_current ();
763 return internal
->debugger_thread
;
767 parse_address (char *address
, char **host
, int *port
)
769 char *pos
= strchr (address
, ':');
771 if (pos
== NULL
|| pos
== address
)
774 size_t len
= pos
- address
;
775 *host
= (char *)g_malloc (len
+ 1);
776 memcpy (*host
, address
, len
);
777 (*host
) [len
] = '\0';
779 *port
= atoi (pos
+ 1);
787 g_printerr ("Usage: mono --debugger-agent=[<option>=<value>,...] ...\n");
788 g_printerr ("Available options:\n");
789 g_printerr (" transport=<transport>\t\tTransport to use for connecting to the debugger (mandatory, possible values: 'dt_socket')\n");
790 g_printerr (" address=<hostname>:<port>\tAddress to connect to (mandatory)\n");
791 g_printerr (" loglevel=<n>\t\t\tLog level (defaults to 0)\n");
792 g_printerr (" logfile=<file>\t\tFile to log to (defaults to stdout)\n");
793 g_printerr (" suspend=y/n\t\t\tWhether to suspend after startup.\n");
794 g_printerr (" timeout=<n>\t\t\tTimeout for connecting in milliseconds.\n");
795 g_printerr (" server=y/n\t\t\tWhether to listen for a client connection.\n");
796 g_printerr (" keepalive=<n>\t\t\tSend keepalive events every n milliseconds.\n");
797 g_printerr (" setpgid=y/n\t\t\tWhether to call setpid(0, 0) after startup.\n");
798 g_printerr (" help\t\t\t\tPrint this help.\n");
802 parse_flag (const char *option
, char *flag
)
804 if (!strcmp (flag
, "y"))
806 else if (!strcmp (flag
, "n"))
809 g_printerr ("debugger-agent: The valid values for the '%s' option are 'y' and 'n'.\n", option
);
816 debugger_agent_parse_options (char *options
)
823 #ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
824 g_printerr ("--debugger-agent is not supported on this platform.\n");
828 extra
= g_getenv ("MONO_SDB_ENV_OPTIONS");
830 options
= g_strdup_printf ("%s,%s", options
, extra
);
834 agent_config
.enabled
= TRUE
;
835 agent_config
.suspend
= TRUE
;
836 agent_config
.server
= FALSE
;
837 agent_config
.defer
= FALSE
;
838 agent_config
.address
= NULL
;
840 //agent_config.log_level = 10;
842 args
= g_strsplit (options
, ",", -1);
843 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
846 if (strncmp (arg
, "transport=", 10) == 0) {
847 agent_config
.transport
= g_strdup (arg
+ 10);
848 } else if (strncmp (arg
, "address=", 8) == 0) {
849 agent_config
.address
= g_strdup (arg
+ 8);
850 } else if (strncmp (arg
, "loglevel=", 9) == 0) {
851 agent_config
.log_level
= atoi (arg
+ 9);
852 } else if (strncmp (arg
, "logfile=", 8) == 0) {
853 agent_config
.log_file
= g_strdup (arg
+ 8);
854 } else if (strncmp (arg
, "suspend=", 8) == 0) {
855 agent_config
.suspend
= parse_flag ("suspend", arg
+ 8);
856 } else if (strncmp (arg
, "server=", 7) == 0) {
857 agent_config
.server
= parse_flag ("server", arg
+ 7);
858 } else if (strncmp (arg
, "onuncaught=", 11) == 0) {
859 agent_config
.onuncaught
= parse_flag ("onuncaught", arg
+ 11);
860 } else if (strncmp (arg
, "onthrow=", 8) == 0) {
861 /* We support multiple onthrow= options */
862 agent_config
.onthrow
= g_slist_append (agent_config
.onthrow
, g_strdup (arg
+ 8));
863 } else if (strncmp (arg
, "onthrow", 7) == 0) {
864 agent_config
.onthrow
= g_slist_append (agent_config
.onthrow
, g_strdup (""));
865 } else if (strncmp (arg
, "help", 4) == 0) {
868 } else if (strncmp (arg
, "timeout=", 8) == 0) {
869 agent_config
.timeout
= atoi (arg
+ 8);
870 } else if (strncmp (arg
, "launch=", 7) == 0) {
871 agent_config
.launch
= g_strdup (arg
+ 7);
872 } else if (strncmp (arg
, "embedding=", 10) == 0) {
873 agent_config
.embedding
= atoi (arg
+ 10) == 1;
874 } else if (strncmp (arg
, "keepalive=", 10) == 0) {
875 agent_config
.keepalive
= atoi (arg
+ 10);
876 } else if (strncmp (arg
, "setpgid=", 8) == 0) {
877 agent_config
.setpgid
= parse_flag ("setpgid", arg
+ 8);
884 if (agent_config
.server
&& !agent_config
.suspend
) {
885 /* Waiting for deferred attachment */
886 agent_config
.defer
= TRUE
;
887 if (agent_config
.address
== NULL
) {
888 agent_config
.address
= g_strdup_printf ("0.0.0.0:%u", 56000 + (mono_process_current_pid () % 1000));
892 //agent_config.log_level = 0;
894 if (agent_config
.transport
== NULL
) {
895 g_printerr ("debugger-agent: The 'transport' option is mandatory.\n");
899 if (agent_config
.address
== NULL
&& !agent_config
.server
) {
900 g_printerr ("debugger-agent: The 'address' option is mandatory.\n");
905 if (!strcmp (agent_config
.transport
, "dt_socket")) {
906 if (agent_config
.address
&& parse_address (agent_config
.address
, &host
, &port
)) {
907 g_printerr ("debugger-agent: The format of the 'address' options is '<host>:<port>'\n");
914 mono_debugger_set_thread_state (DebuggerTlsData
*tls
, MonoDebuggerThreadState expected
, MonoDebuggerThreadState set
)
916 g_assertf (tls
, "Cannot get state of null thread", NULL
);
918 g_assert (tls
->thread_state
== expected
);
920 tls
->thread_state
= set
;
923 MonoDebuggerThreadState
924 mono_debugger_get_thread_state (DebuggerTlsData
*tls
)
926 g_assertf (tls
, "Cannot get state of null thread", NULL
);
928 return tls
->thread_state
;
932 mono_debugger_tls_thread_id (DebuggerTlsData
*tls
)
937 return tls
->thread_id
;
940 // Only call this function with the loader lock held
942 mono_debugger_get_thread_states (void)
944 return thread_to_tls
;
948 mono_debugger_is_disconnected (void)
954 debugger_agent_init (void)
956 if (!agent_config
.enabled
)
959 DebuggerEngineCallbacks cbs
;
960 memset (&cbs
, 0, sizeof (cbs
));
961 cbs
.tls_get_restore_state
= tls_get_restore_state
;
962 cbs
.try_process_suspend
= try_process_suspend
;
963 cbs
.begin_breakpoint_processing
= begin_breakpoint_processing
;
964 cbs
.begin_single_step_processing
= begin_single_step_processing
;
965 cbs
.ss_discard_frame_context
= ss_discard_frame_context
;
966 cbs
.ss_calculate_framecount
= ss_calculate_framecount
;
967 cbs
.ensure_jit
= ensure_jit
;
968 cbs
.ensure_runtime_is_suspended
= ensure_runtime_is_suspended
;
969 cbs
.get_this_async_id
= get_this_async_id
;
970 cbs
.set_set_notification_for_wait_completion_flag
= set_set_notification_for_wait_completion_flag
;
971 cbs
.get_notify_debugger_of_wait_completion_method
= get_notify_debugger_of_wait_completion_method
;
972 cbs
.create_breakpoint_events
= create_breakpoint_events
;
973 cbs
.process_breakpoint_events
= process_breakpoint_events
;
974 cbs
.ss_create_init_args
= ss_create_init_args
;
975 cbs
.ss_args_destroy
= ss_args_destroy
;
976 cbs
.handle_multiple_ss_requests
= handle_multiple_ss_requests
;
982 /* Need to know whenever a thread has acquired the loader mutex */
983 mono_loader_lock_track_ownership (TRUE
);
985 event_requests
= g_ptr_array_new ();
987 mono_coop_mutex_init (&debugger_thread_exited_mutex
);
988 mono_coop_cond_init (&debugger_thread_exited_cond
);
990 MonoProfilerHandle prof
= mono_profiler_create (NULL
);
991 mono_profiler_set_runtime_shutdown_end_callback (prof
, runtime_shutdown
);
992 mono_profiler_set_runtime_initialized_callback (prof
, runtime_initialized
);
993 mono_profiler_set_domain_loaded_callback (prof
, appdomain_load
);
994 mono_profiler_set_domain_unloading_callback (prof
, appdomain_start_unload
);
995 mono_profiler_set_domain_unloaded_callback (prof
, appdomain_unload
);
996 mono_profiler_set_thread_started_callback (prof
, thread_startup
);
997 mono_profiler_set_thread_stopped_callback (prof
, thread_end
);
998 mono_profiler_set_assembly_loaded_callback (prof
, assembly_load
);
999 mono_profiler_set_assembly_unloading_callback (prof
, assembly_unload
);
1000 mono_profiler_set_jit_done_callback (prof
, jit_done
);
1001 mono_profiler_set_jit_failed_callback (prof
, jit_failed
);
1002 mono_profiler_set_gc_finalizing_callback (prof
, gc_finalizing
);
1003 mono_profiler_set_gc_finalized_callback (prof
, gc_finalized
);
1005 mono_native_tls_alloc (&debugger_tls_id
, NULL
);
1007 /* Needed by the hash_table_new_type () call below */
1008 mono_gc_base_init ();
1010 thread_to_tls
= mono_g_hash_table_new_type_internal ((GHashFunc
)mono_object_hash_internal
, NULL
, MONO_HASH_KEY_GC
, MONO_ROOT_SOURCE_DEBUGGER
, NULL
, "Debugger TLS Table");
1012 tid_to_thread
= mono_g_hash_table_new_type_internal (NULL
, NULL
, MONO_HASH_VALUE_GC
, MONO_ROOT_SOURCE_DEBUGGER
, NULL
, "Debugger Thread Table");
1014 tid_to_thread_obj
= mono_g_hash_table_new_type_internal (NULL
, NULL
, MONO_HASH_VALUE_GC
, MONO_ROOT_SOURCE_DEBUGGER
, NULL
, "Debugger Thread Object Table");
1016 pending_assembly_loads
= g_ptr_array_new ();
1018 log_level
= agent_config
.log_level
;
1020 embedding
= agent_config
.embedding
;
1021 disconnected
= TRUE
;
1023 if (agent_config
.log_file
) {
1024 log_file
= fopen (agent_config
.log_file
, "w+");
1026 g_printerr ("Unable to create log file '%s': %s.\n", agent_config
.log_file
, strerror (errno
));
1032 mono_de_set_log_level (log_level
, log_file
);
1038 mini_get_debug_options ()->gen_sdb_seq_points
= TRUE
;
1040 * This is needed because currently we don't handle liveness info.
1042 mini_get_debug_options ()->mdb_optimizations
= TRUE
;
1044 #ifndef MONO_ARCH_HAVE_CONTEXT_SET_INT_REG
1045 /* This is needed because we can't set local variables in registers yet */
1046 mono_disable_optimizations (MONO_OPT_LINEARS
);
1050 * The stack walk done from thread_interrupt () needs to be signal safe, but it
1051 * isn't, since it can call into mono_aot_find_jit_info () which is not signal
1052 * safe (#3411). So load AOT info eagerly when the debugger is running as a
1055 mini_get_debug_options ()->load_aot_jit_info_eagerly
= TRUE
;
1058 if (agent_config
.setpgid
)
1062 if (!agent_config
.onuncaught
&& !agent_config
.onthrow
)
1063 finish_agent_init (TRUE
);
1067 * finish_agent_init:
1069 * Finish the initialization of the agent. This involves connecting the transport
1070 * and starting the agent thread. This is either done at startup, or
1071 * in response to some event like an unhandled exception.
1074 finish_agent_init (gboolean on_startup
)
1078 if (mono_atomic_cas_i32 (&inited
, 1, 0) == 1)
1081 if (agent_config
.launch
) {
1084 // FIXME: Generated address
1085 // FIXME: Races with transport_connect ()
1087 argv
[0] = agent_config
.launch
;
1088 argv
[1] = agent_config
.transport
;
1089 argv
[2] = agent_config
.address
;
1092 res
= g_spawn_async_with_pipes (NULL
, argv
, NULL
, (GSpawnFlags
)0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1094 g_printerr ("Failed to execute '%s'.\n", agent_config
.launch
);
1099 transport_connect (agent_config
.address
);
1102 /* Do some which is usually done after sending the VMStart () event */
1103 vm_start_event_sent
= TRUE
;
1105 start_debugger_thread (error
);
1106 mono_error_assert_ok (error
);
1111 mono_debugger_agent_cleanup (void)
1116 stop_debugger_thread ();
1118 event_requests_cleanup ();
1124 if (file_check_valid_memory
!= -1) {
1125 remove (filename_check_valid_memory
);
1126 g_free (filename_check_valid_memory
);
1127 close (file_check_valid_memory
);
1135 #ifndef DISABLE_SOCKET_TRANSPORT
1140 * recv() + handle incomplete reads and EINTR
1143 socket_transport_recv (void *buf
, int len
)
1149 static gint64 last_keepalive
;
1156 res
= recv (fd
, (char *) buf
+ total
, len
- total
, flags
);
1159 if (agent_config
.keepalive
) {
1160 gboolean need_keepalive
= FALSE
;
1161 if (res
== -1 && get_last_sock_error () == MONO_EWOULDBLOCK
) {
1162 need_keepalive
= TRUE
;
1163 } else if (res
== -1) {
1164 /* This could happen if recv () is interrupted repeatedly */
1165 msecs
= mono_msec_ticks ();
1166 if (msecs
- last_keepalive
>= agent_config
.keepalive
) {
1167 need_keepalive
= TRUE
;
1168 last_keepalive
= msecs
;
1171 if (need_keepalive
) {
1172 process_profiler_event (EVENT_KIND_KEEPALIVE
, NULL
);
1176 } while ((res
> 0 && total
< len
) || (res
== -1 && get_last_sock_error () == MONO_EINTR
));
1184 set_keepalive (void)
1189 if (!agent_config
.keepalive
|| !conn_fd
)
1192 tv
.tv_sec
= agent_config
.keepalive
/ 1000;
1193 tv
.tv_usec
= (agent_config
.keepalive
% 1000) * 1000;
1195 result
= setsockopt (conn_fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &tv
, sizeof(struct timeval
));
1196 g_assert (result
>= 0);
1200 socket_transport_accept (int socket_fd
)
1203 conn_fd
= accept (socket_fd
, NULL
, NULL
);
1206 if (conn_fd
== -1) {
1207 g_printerr ("debugger-agent: Unable to listen on %d\n", socket_fd
);
1209 DEBUG_PRINTF (1, "Accepted connection from client, connection fd=%d.\n", conn_fd
);
1216 socket_transport_send (void *data
, int len
)
1223 res
= send (conn_fd
, (const char*)data
, len
, 0);
1224 } while (res
== -1 && get_last_sock_error () == MONO_EINTR
);
1235 * socket_transport_connect:
1237 * Connect/Listen on HOST:PORT. If HOST is NULL, generate an address and listen on it.
1240 socket_transport_connect (const char *address
)
1242 MonoAddressInfo
*result
;
1243 MonoAddressEntry
*rp
;
1244 int sfd
= -1, s
, res
;
1248 if (agent_config
.address
) {
1249 res
= parse_address (agent_config
.address
, &host
, &port
);
1250 g_assert (res
== 0);
1261 MONO_HINT_IPV4
| MONO_HINT_NUMERIC_HOST
,
1262 MONO_HINT_IPV6
| MONO_HINT_NUMERIC_HOST
,
1263 MONO_HINT_UNSPECIFIED
1266 mono_network_init ();
1268 for (int i
= 0; i
< sizeof(hints
) / sizeof(int); i
++) {
1269 /* Obtain address(es) matching host/port */
1270 s
= mono_get_address_info (host
, port
, hints
[i
], &result
);
1275 g_printerr ("debugger-agent: Unable to resolve %s:%d: %d\n", host
, port
, s
); // FIXME add portable error conversion functions
1280 if (agent_config
.server
) {
1281 /* Wait for a connection */
1283 struct sockaddr_in addr
;
1286 /* No address, generate one */
1287 sfd
= socket (AF_INET
, SOCK_STREAM
, 0);
1289 g_printerr ("debugger-agent: Unable to create a socket: %s\n", strerror (get_last_sock_error ()));
1293 /* This will bind the socket to a random port */
1294 res
= listen (sfd
, 16);
1296 g_printerr ("debugger-agent: Unable to setup listening socket: %s\n", strerror (get_last_sock_error ()));
1301 addrlen
= sizeof (addr
);
1302 memset (&addr
, 0, sizeof (addr
));
1303 res
= getsockname (sfd
, (struct sockaddr
*)&addr
, &addrlen
);
1304 g_assert (res
== 0);
1306 host
= (char*)"127.0.0.1";
1307 port
= ntohs (addr
.sin_port
);
1309 /* Emit the address to stdout */
1310 /* FIXME: Should print another interface, not localhost */
1311 printf ("%s:%d\n", host
, port
);
1313 /* Listen on the provided address */
1314 for (rp
= result
->entries
; rp
!= NULL
; rp
= rp
->next
) {
1315 MonoSocketAddress sockaddr
;
1319 mono_socket_address_init (&sockaddr
, &sock_len
, rp
->family
, &rp
->address
, port
);
1321 sfd
= socket (rp
->family
, rp
->socktype
,
1326 if (setsockopt (sfd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&n
, sizeof(n
)) == -1)
1329 res
= bind (sfd
, &sockaddr
.addr
, sock_len
);
1333 res
= listen (sfd
, 16);
1340 mono_free_address_info (result
);
1343 if (agent_config
.defer
)
1346 DEBUG_PRINTF (1, "Listening on %s:%d (timeout=%d ms)...\n", host
, port
, agent_config
.timeout
);
1348 if (agent_config
.timeout
) {
1353 tv
.tv_usec
= agent_config
.timeout
* 1000;
1355 FD_SET (sfd
, &readfds
);
1358 res
= select (sfd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1362 g_printerr ("debugger-agent: Timed out waiting to connect.\n");
1367 conn_fd
= socket_transport_accept (sfd
);
1371 DEBUG_PRINTF (1, "Accepted connection from client, socket fd=%d.\n", conn_fd
);
1373 /* Connect to the specified address */
1374 /* FIXME: Respect the timeout */
1375 for (rp
= result
->entries
; rp
!= NULL
; rp
= rp
->next
) {
1376 MonoSocketAddress sockaddr
;
1379 mono_socket_address_init (&sockaddr
, &sock_len
, rp
->family
, &rp
->address
, port
);
1381 sfd
= socket (rp
->family
, rp
->socktype
,
1387 res
= connect (sfd
, &sockaddr
.addr
, sock_len
);
1391 break; /* Success */
1403 g_printerr ("debugger-agent: Unable to connect to %s:%d\n", host
, port
);
1409 mono_free_address_info (result
);
1412 if (!transport_handshake ())
1417 socket_transport_close1 (void)
1419 /* This will interrupt the agent thread */
1420 /* Close the read part only so it can still send back replies */
1421 /* Also shut down the connection listener so that we can exit normally */
1423 /* SD_RECEIVE doesn't break the recv in the debugger thread */
1424 shutdown (conn_fd
, SD_BOTH
);
1425 shutdown (listen_fd
, SD_BOTH
);
1426 closesocket (listen_fd
);
1428 shutdown (conn_fd
, SHUT_RD
);
1429 shutdown (listen_fd
, SHUT_RDWR
);
1437 socket_transport_close2 (void)
1440 shutdown (conn_fd
, SD_BOTH
);
1442 shutdown (conn_fd
, SHUT_RDWR
);
1447 register_socket_transport (void)
1449 DebuggerTransport trans
;
1451 trans
.name
= "dt_socket";
1452 trans
.connect
= socket_transport_connect
;
1453 trans
.close1
= socket_transport_close1
;
1454 trans
.close2
= socket_transport_close2
;
1455 trans
.send
= socket_transport_send
;
1456 trans
.recv
= socket_transport_recv
;
1458 register_transport (&trans
);
1462 * socket_fd_transport_connect:
1466 socket_fd_transport_connect (const char *address
)
1470 res
= sscanf (address
, "%d", &conn_fd
);
1472 g_printerr ("debugger-agent: socket-fd transport address is invalid: '%s'\n", address
);
1476 if (!transport_handshake ())
1481 register_socket_fd_transport (void)
1483 DebuggerTransport trans
;
1485 /* This is the same as the 'dt_socket' transport, but receives an already connected socket fd */
1486 trans
.name
= "socket-fd";
1487 trans
.connect
= socket_fd_transport_connect
;
1488 trans
.close1
= socket_transport_close1
;
1489 trans
.close2
= socket_transport_close2
;
1490 trans
.send
= socket_transport_send
;
1491 trans
.recv
= socket_transport_recv
;
1493 register_transport (&trans
);
1496 #endif /* DISABLE_SOCKET_TRANSPORT */
1502 #define MAX_TRANSPORTS 16
1504 static DebuggerTransport
*transport
;
1506 static DebuggerTransport transports
[MAX_TRANSPORTS
];
1507 static int ntransports
;
1510 mono_debugger_agent_register_transport (DebuggerTransport
*trans
);
1513 mono_debugger_agent_register_transport (DebuggerTransport
*trans
)
1515 register_transport (trans
);
1519 register_transport (DebuggerTransport
*trans
)
1521 g_assert (ntransports
< MAX_TRANSPORTS
);
1523 memcpy (&transports
[ntransports
], trans
, sizeof (DebuggerTransport
));
1528 transport_init (void)
1532 #ifndef DISABLE_SOCKET_TRANSPORT
1533 register_socket_transport ();
1534 register_socket_fd_transport ();
1537 for (i
= 0; i
< ntransports
; ++i
) {
1538 if (!strcmp (agent_config
.transport
, transports
[i
].name
))
1541 if (i
== ntransports
) {
1542 g_printerr ("debugger-agent: The supported values for the 'transport' option are: ");
1543 for (i
= 0; i
< ntransports
; ++i
)
1544 g_printerr ("%s'%s'", i
> 0 ? ", " : "", transports
[i
].name
);
1548 transport
= &transports
[i
];
1552 transport_connect (const char *address
)
1554 transport
->connect (address
);
1558 transport_close1 (void)
1560 transport
->close1 ();
1564 transport_close2 (void)
1566 transport
->close2 ();
1570 transport_send (void *buf
, int len
)
1572 return transport
->send (buf
, len
);
1576 transport_recv (void *buf
, int len
)
1578 return transport
->recv (buf
, len
);
1582 mono_debugger_agent_transport_handshake (void)
1584 return transport_handshake ();
1588 transport_handshake (void)
1590 char handshake_msg
[128];
1594 disconnected
= TRUE
;
1596 /* Write handshake message */
1597 sprintf (handshake_msg
, "DWP-Handshake");
1600 res
= transport_send (handshake_msg
, strlen (handshake_msg
));
1601 } while (res
== -1 && get_last_sock_error () == MONO_EINTR
);
1603 g_assert (res
!= -1);
1606 res
= transport_recv (buf
, strlen (handshake_msg
));
1607 if ((res
!= strlen (handshake_msg
)) || (memcmp (buf
, handshake_msg
, strlen (handshake_msg
)) != 0)) {
1608 g_printerr ("debugger-agent: DWP handshake failed.\n");
1613 * To support older clients, the client sends its protocol version after connecting
1614 * using a command. Until that is received, default to our protocol version.
1616 major_version
= MAJOR_VERSION
;
1617 minor_version
= MINOR_VERSION
;
1618 protocol_version_set
= FALSE
;
1620 #ifndef DISABLE_SOCKET_TRANSPORT
1621 // FIXME: Move this somewhere else
1623 * Set TCP_NODELAY on the socket so the client receives events/command
1624 * results immediately.
1628 int result
= setsockopt (conn_fd
,
1633 g_assert (result
>= 0);
1639 disconnected
= FALSE
;
1644 stop_debugger_thread (void)
1649 transport_close1 ();
1652 * Wait for the thread to exit.
1654 * If we continue with the shutdown without waiting for it, then the client might
1655 * not receive an answer to its last command like a resume.
1657 if (!is_debugger_thread ()) {
1659 mono_coop_mutex_lock (&debugger_thread_exited_mutex
);
1660 if (!debugger_thread_exited
)
1661 mono_coop_cond_wait (&debugger_thread_exited_cond
, &debugger_thread_exited_mutex
);
1662 mono_coop_mutex_unlock (&debugger_thread_exited_mutex
);
1663 } while (!debugger_thread_exited
);
1665 if (debugger_thread_handle
)
1666 mono_thread_info_wait_one_handle (debugger_thread_handle
, MONO_INFINITE_WAIT
, TRUE
);
1669 transport_close2 ();
1673 start_debugger_thread (MonoError
*error
)
1675 MonoInternalThread
*thread
;
1677 thread
= mono_thread_create_internal (mono_get_root_domain (), (gpointer
)debugger_thread
, NULL
, MONO_THREAD_CREATE_FLAGS_DEBUGGER
, error
);
1678 return_if_nok (error
);
1680 /* Is it possible for the thread to be dead alreay ? */
1681 debugger_thread_handle
= mono_threads_open_thread_handle (thread
->handle
);
1682 g_assert (debugger_thread_handle
);
1687 * Functions to decode protocol data
1691 decode_byte (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1694 g_assert (*endbuf
<= limit
);
1699 decode_int (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1702 g_assert (*endbuf
<= limit
);
1704 return (((int)buf
[0]) << 24) | (((int)buf
[1]) << 16) | (((int)buf
[2]) << 8) | (((int)buf
[3]) << 0);
1708 decode_long (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1710 guint32 high
= decode_int (buf
, &buf
, limit
);
1711 guint32 low
= decode_int (buf
, &buf
, limit
);
1715 return ((((guint64
)high
) << 32) | ((guint64
)low
));
1719 decode_id (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1721 return decode_int (buf
, endbuf
, limit
);
1725 decode_string (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
1727 int len
= decode_int (buf
, &buf
, limit
);
1735 s
= (char *)g_malloc (len
+ 1);
1738 memcpy (s
, buf
, len
);
1747 * Functions to encode protocol data
1751 buffer_init (Buffer
*buf
, int size
)
1753 buf
->buf
= (guint8
*)g_malloc (size
);
1755 buf
->end
= buf
->buf
+ size
;
1759 buffer_len (Buffer
*buf
)
1761 return buf
->p
- buf
->buf
;
1765 buffer_make_room (Buffer
*buf
, int size
)
1767 if (buf
->end
- buf
->p
< size
) {
1768 int new_size
= buf
->end
- buf
->buf
+ size
+ 32;
1769 guint8
*p
= (guint8
*)g_realloc (buf
->buf
, new_size
);
1770 size
= buf
->p
- buf
->buf
;
1773 buf
->end
= buf
->buf
+ new_size
;
1778 buffer_add_byte (Buffer
*buf
, guint8 val
)
1780 buffer_make_room (buf
, 1);
1786 buffer_add_short (Buffer
*buf
, guint32 val
)
1788 buffer_make_room (buf
, 2);
1789 buf
->p
[0] = (val
>> 8) & 0xff;
1790 buf
->p
[1] = (val
>> 0) & 0xff;
1795 buffer_add_int (Buffer
*buf
, guint32 val
)
1797 buffer_make_room (buf
, 4);
1798 buf
->p
[0] = (val
>> 24) & 0xff;
1799 buf
->p
[1] = (val
>> 16) & 0xff;
1800 buf
->p
[2] = (val
>> 8) & 0xff;
1801 buf
->p
[3] = (val
>> 0) & 0xff;
1806 buffer_add_long (Buffer
*buf
, guint64 l
)
1808 buffer_add_int (buf
, (l
>> 32) & 0xffffffff);
1809 buffer_add_int (buf
, (l
>> 0) & 0xffffffff);
1813 buffer_add_id (Buffer
*buf
, int id
)
1815 buffer_add_int (buf
, (guint64
)id
);
1819 buffer_add_data (Buffer
*buf
, guint8
*data
, int len
)
1821 buffer_make_room (buf
, len
);
1822 memcpy (buf
->p
, data
, len
);
1827 buffer_add_string (Buffer
*buf
, const char *str
)
1832 buffer_add_int (buf
, 0);
1835 buffer_add_int (buf
, len
);
1836 buffer_add_data (buf
, (guint8
*)str
, len
);
1841 buffer_add_byte_array (Buffer
*buf
, guint8
*bytes
, guint32 arr_len
)
1843 buffer_add_int (buf
, arr_len
);
1844 buffer_add_data (buf
, bytes
, arr_len
);
1848 buffer_add_buffer (Buffer
*buf
, Buffer
*data
)
1850 buffer_add_data (buf
, data
->buf
, buffer_len (data
));
1854 buffer_free (Buffer
*buf
)
1860 send_packet (int command_set
, int command
, Buffer
*data
)
1866 id
= mono_atomic_inc_i32 (&packet_id
);
1868 len
= data
->p
- data
->buf
+ 11;
1869 buffer_init (&buf
, len
);
1870 buffer_add_int (&buf
, len
);
1871 buffer_add_int (&buf
, id
);
1872 buffer_add_byte (&buf
, 0); /* flags */
1873 buffer_add_byte (&buf
, command_set
);
1874 buffer_add_byte (&buf
, command
);
1875 memcpy (buf
.buf
+ 11, data
->buf
, data
->p
- data
->buf
);
1877 res
= transport_send (buf
.buf
, len
);
1885 send_reply_packets (int npackets
, ReplyPacket
*packets
)
1892 for (i
= 0; i
< npackets
; ++i
)
1893 len
+= buffer_len (packets
[i
].data
) + 11;
1894 buffer_init (&buf
, len
);
1895 for (i
= 0; i
< npackets
; ++i
) {
1896 buffer_add_int (&buf
, buffer_len (packets
[i
].data
) + 11);
1897 buffer_add_int (&buf
, packets
[i
].id
);
1898 buffer_add_byte (&buf
, 0x80); /* flags */
1899 buffer_add_byte (&buf
, (packets
[i
].error
>> 8) & 0xff);
1900 buffer_add_byte (&buf
, packets
[i
].error
);
1901 buffer_add_buffer (&buf
, packets
[i
].data
);
1904 res
= transport_send (buf
.buf
, len
);
1912 send_reply_packet (int id
, int error
, Buffer
*data
)
1916 memset (&packet
, 0, sizeof (packet
));
1918 packet
.error
= error
;
1921 return send_reply_packets (1, &packet
);
1925 send_buffered_reply_packets (void)
1929 send_reply_packets (nreply_packets
, reply_packets
);
1930 for (i
= 0; i
< nreply_packets
; ++i
)
1931 buffer_free (reply_packets
[i
].data
);
1932 DEBUG_PRINTF (1, "[dbg] Sent %d buffered reply packets [at=%lx].\n", nreply_packets
, (long)mono_100ns_ticks () / 10000);
1937 buffer_reply_packet (int id
, int error
, Buffer
*data
)
1941 if (nreply_packets
== 128)
1942 send_buffered_reply_packets ();
1944 p
= &reply_packets
[nreply_packets
];
1947 p
->data
= g_new0 (Buffer
, 1);
1948 buffer_init (p
->data
, buffer_len (data
));
1949 buffer_add_buffer (p
->data
, data
);
1954 /* Maps objid -> ObjRef */
1955 /* Protected by the loader lock */
1956 static GHashTable
*objrefs
;
1957 /* Protected by the loader lock */
1958 static GHashTable
*obj_to_objref
;
1959 /* Protected by the dbg lock */
1960 static MonoGHashTable
*suspended_objs
;
1967 objrefs
= g_hash_table_new_full (NULL
, NULL
, NULL
, mono_debugger_free_objref
);
1968 obj_to_objref
= g_hash_table_new (NULL
, NULL
);
1969 suspended_objs
= mono_g_hash_table_new_type_internal ((GHashFunc
)mono_object_hash_internal
, NULL
, MONO_HASH_KEY_GC
, MONO_ROOT_SOURCE_DEBUGGER
, NULL
, "Debugger Suspended Object Table");
1973 objrefs_cleanup (void)
1975 g_hash_table_destroy (objrefs
);
1980 * Return an ObjRef for OBJ.
1983 get_objref (MonoObject
*obj
)
1986 GSList
*reflist
= NULL
, *l
;
1992 if (suspend_count
) {
1994 * Have to keep object refs created during suspensions alive for the duration of the suspension, so GCs during invokes don't collect them.
1997 mono_g_hash_table_insert_internal (suspended_objs
, obj
, NULL
);
2001 mono_loader_lock ();
2003 /* FIXME: The tables can grow indefinitely */
2005 if (mono_gc_is_moving ()) {
2007 * Objects can move, so use a hash table mapping hash codes to lists of
2008 * ObjRef structures.
2010 hash
= mono_object_hash_internal (obj
);
2012 reflist
= (GSList
*)g_hash_table_lookup (obj_to_objref
, GINT_TO_POINTER (hash
));
2013 for (l
= reflist
; l
; l
= l
->next
) {
2014 ref
= (ObjRef
*)l
->data
;
2015 if (ref
&& mono_gchandle_get_target_internal (ref
->handle
) == obj
) {
2016 mono_loader_unlock ();
2021 /* Use a hash table with masked pointers to internalize object references */
2022 ref
= (ObjRef
*)g_hash_table_lookup (obj_to_objref
, GINT_TO_POINTER (~((gsize
)obj
)));
2023 /* ref might refer to a different object with the same addr which was GCd */
2024 if (ref
&& mono_gchandle_get_target_internal (ref
->handle
) == obj
) {
2025 mono_loader_unlock ();
2030 ref
= g_new0 (ObjRef
, 1);
2031 ref
->id
= mono_atomic_inc_i32 (&objref_id
);
2032 ref
->handle
= mono_gchandle_new_weakref_internal (obj
, FALSE
);
2034 g_hash_table_insert (objrefs
, GINT_TO_POINTER (ref
->id
), ref
);
2036 if (mono_gc_is_moving ()) {
2037 reflist
= g_slist_append (reflist
, ref
);
2038 g_hash_table_insert (obj_to_objref
, GINT_TO_POINTER (hash
), reflist
);
2040 g_hash_table_insert (obj_to_objref
, GINT_TO_POINTER (~((gsize
)obj
)), ref
);
2043 mono_loader_unlock ();
2049 true_pred (gpointer key
, gpointer value
, gpointer user_data
)
2055 clear_suspended_objs (void)
2058 mono_g_hash_table_foreach_remove (suspended_objs
, true_pred
, NULL
);
2063 get_objid (MonoObject
*obj
)
2068 return get_objref (obj
)->id
;
2072 * Set OBJ to the object identified by OBJID.
2073 * Returns 0 or an error code if OBJID is invalid or the object has been garbage
2077 get_object_allow_null (int objid
, MonoObject
**obj
)
2087 return ERR_INVALID_OBJECT
;
2089 mono_loader_lock ();
2091 ref
= (ObjRef
*)g_hash_table_lookup (objrefs
, GINT_TO_POINTER (objid
));
2094 *obj
= mono_gchandle_get_target_internal (ref
->handle
);
2095 mono_loader_unlock ();
2097 return ERR_INVALID_OBJECT
;
2100 mono_loader_unlock ();
2101 return ERR_INVALID_OBJECT
;
2106 get_object (int objid
, MonoObject
**obj
)
2108 ErrorCode err
= get_object_allow_null (objid
, obj
);
2110 if (err
!= ERR_NONE
)
2113 return ERR_INVALID_OBJECT
;
2118 decode_objid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
)
2120 return decode_id (buf
, endbuf
, limit
);
2124 buffer_add_objid (Buffer
*buf
, MonoObject
*o
)
2126 buffer_add_id (buf
, get_objid (o
));
2145 * Represents a runtime structure accessible to the debugger client
2148 /* Unique id used in the wire protocol */
2150 /* Domain of the runtime structure, NULL if the domain was unloaded */
2157 MonoAssembly
*assembly
;
2158 MonoClassField
*field
;
2160 MonoProperty
*property
;
2165 /* Maps runtime structure -> Id */
2166 /* Protected by the dbg lock */
2167 GHashTable
*val_to_id
[ID_NUM
];
2168 /* Classes whose class load event has been sent */
2169 /* Protected by the loader lock */
2170 GHashTable
*loaded_classes
;
2171 /* Maps MonoClass->GPtrArray of file names */
2172 GHashTable
*source_files
;
2173 /* Maps source file basename -> GSList of classes */
2174 GHashTable
*source_file_to_class
;
2175 /* Same with ignore-case */
2176 GHashTable
*source_file_to_class_ignorecase
;
2180 /* Protected by the dbg lock */
2181 static GPtrArray
*ids
[ID_NUM
];
2188 for (i
= 0; i
< ID_NUM
; ++i
)
2189 ids
[i
] = g_ptr_array_new ();
2197 for (i
= 0; i
< ID_NUM
; ++i
) {
2199 for (j
= 0; j
< ids
[i
]->len
; ++j
)
2200 g_free (g_ptr_array_index (ids
[i
], j
));
2201 g_ptr_array_free (ids
[i
], TRUE
);
2208 debugger_agent_free_domain_info (MonoDomain
*domain
)
2210 AgentDomainInfo
*info
= (AgentDomainInfo
*)domain_jit_info (domain
)->agent_info
;
2212 GHashTableIter iter
;
2213 GPtrArray
*file_names
;
2218 for (i
= 0; i
< ID_NUM
; ++i
)
2219 g_hash_table_destroy (info
->val_to_id
[i
]);
2220 g_hash_table_destroy (info
->loaded_classes
);
2222 g_hash_table_iter_init (&iter
, info
->source_files
);
2223 while (g_hash_table_iter_next (&iter
, NULL
, (void**)&file_names
)) {
2224 for (i
= 0; i
< file_names
->len
; ++i
)
2225 g_free (g_ptr_array_index (file_names
, i
));
2226 g_ptr_array_free (file_names
, TRUE
);
2229 g_hash_table_iter_init (&iter
, info
->source_file_to_class
);
2230 while (g_hash_table_iter_next (&iter
, (void**)&basename
, (void**)&l
)) {
2235 g_hash_table_iter_init (&iter
, info
->source_file_to_class_ignorecase
);
2236 while (g_hash_table_iter_next (&iter
, (void**)&basename
, (void**)&l
)) {
2244 domain_jit_info (domain
)->agent_info
= NULL
;
2246 /* Clear ids referencing structures in the domain */
2248 for (i
= 0; i
< ID_NUM
; ++i
) {
2250 for (j
= 0; j
< ids
[i
]->len
; ++j
) {
2251 Id
*id
= (Id
*)g_ptr_array_index (ids
[i
], j
);
2252 if (id
->domain
== domain
)
2259 mono_de_domain_remove (domain
);
2262 static AgentDomainInfo
*
2263 get_agent_domain_info (MonoDomain
*domain
)
2265 AgentDomainInfo
*info
= NULL
;
2266 MonoJitDomainInfo
*jit_info
= domain_jit_info (domain
);
2268 info
= (AgentDomainInfo
*)jit_info
->agent_info
;
2271 mono_memory_read_barrier ();
2275 info
= g_new0 (AgentDomainInfo
, 1);
2276 info
->loaded_classes
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
2277 info
->source_files
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
2278 info
->source_file_to_class
= g_hash_table_new (g_str_hash
, g_str_equal
);
2279 info
->source_file_to_class_ignorecase
= g_hash_table_new (g_str_hash
, g_str_equal
);
2281 mono_memory_write_barrier ();
2283 gpointer other_info
= mono_atomic_cas_ptr (&jit_info
->agent_info
, info
, NULL
);
2285 if (other_info
!= NULL
) {
2286 g_hash_table_destroy (info
->loaded_classes
);
2287 g_hash_table_destroy (info
->source_files
);
2288 g_hash_table_destroy (info
->source_file_to_class
);
2289 g_hash_table_destroy (info
->source_file_to_class_ignorecase
);
2293 return (AgentDomainInfo
*)jit_info
->agent_info
;
2297 get_id (MonoDomain
*domain
, IdType type
, gpointer val
)
2300 AgentDomainInfo
*info
;
2305 info
= get_agent_domain_info (domain
);
2309 if (info
->val_to_id
[type
] == NULL
)
2310 info
->val_to_id
[type
] = g_hash_table_new (mono_aligned_addr_hash
, NULL
);
2312 id
= (Id
*)g_hash_table_lookup (info
->val_to_id
[type
], val
);
2318 id
= g_new0 (Id
, 1);
2320 id
->id
= ids
[type
]->len
+ 1;
2321 id
->domain
= domain
;
2324 g_hash_table_insert (info
->val_to_id
[type
], val
, id
);
2325 g_ptr_array_add (ids
[type
], id
);
2333 decode_ptr_id (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, IdType type
, MonoDomain
**domain
, ErrorCode
*err
)
2337 int id
= decode_id (buf
, endbuf
, limit
);
2346 // FIXME: error handling
2348 g_assert (id
> 0 && id
<= ids
[type
]->len
);
2350 res
= (Id
*)g_ptr_array_index (ids
[type
], GPOINTER_TO_INT (id
- 1));
2353 if (res
->domain
== NULL
|| res
->domain
->state
== MONO_APPDOMAIN_UNLOADED
) {
2354 DEBUG_PRINTF (1, "ERR_UNLOADED, id=%d, type=%d.\n", id
, type
);
2355 *err
= ERR_UNLOADED
;
2360 *domain
= res
->domain
;
2362 return res
->data
.val
;
2366 buffer_add_ptr_id (Buffer
*buf
, MonoDomain
*domain
, IdType type
, gpointer val
)
2368 int id
= get_id (domain
, type
, val
);
2370 buffer_add_id (buf
, id
);
2375 decode_typeid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2379 klass
= (MonoClass
*)decode_ptr_id (buf
, endbuf
, limit
, ID_TYPE
, domain
, err
);
2380 if (G_UNLIKELY (log_level
>= 2) && klass
) {
2383 s
= mono_type_full_name (m_class_get_byval_arg (klass
));
2384 DEBUG_PRINTF (2, "[dbg] recv class [%s]\n", s
);
2390 static MonoAssembly
*
2391 decode_assemblyid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2393 return (MonoAssembly
*)decode_ptr_id (buf
, endbuf
, limit
, ID_ASSEMBLY
, domain
, err
);
2397 decode_moduleid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2399 return (MonoImage
*)decode_ptr_id (buf
, endbuf
, limit
, ID_MODULE
, domain
, err
);
2403 decode_methodid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2407 m
= (MonoMethod
*)decode_ptr_id (buf
, endbuf
, limit
, ID_METHOD
, domain
, err
);
2408 if (G_UNLIKELY (log_level
>= 2) && m
) {
2411 s
= mono_method_full_name (m
, TRUE
);
2412 DEBUG_PRINTF (2, "[dbg] recv method [%s]\n", s
);
2418 static MonoClassField
*
2419 decode_fieldid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2421 return (MonoClassField
*)decode_ptr_id (buf
, endbuf
, limit
, ID_FIELD
, domain
, err
);
2425 decode_domainid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2427 return (MonoDomain
*)decode_ptr_id (buf
, endbuf
, limit
, ID_DOMAIN
, domain
, err
);
2430 static MonoProperty
*
2431 decode_propertyid (guint8
*buf
, guint8
**endbuf
, guint8
*limit
, MonoDomain
**domain
, ErrorCode
*err
)
2433 return (MonoProperty
*)decode_ptr_id (buf
, endbuf
, limit
, ID_PROPERTY
, domain
, err
);
2437 buffer_add_typeid (Buffer
*buf
, MonoDomain
*domain
, MonoClass
*klass
)
2439 buffer_add_ptr_id (buf
, domain
, ID_TYPE
, klass
);
2440 if (G_UNLIKELY (log_level
>= 2) && klass
) {
2443 s
= mono_type_full_name (m_class_get_byval_arg (klass
));
2444 if (is_debugger_thread ())
2445 DEBUG_PRINTF (2, "[dbg] send class [%s]\n", s
);
2447 DEBUG_PRINTF (2, "[%p] send class [%s]\n", (gpointer
) (gsize
) mono_native_thread_id_get (), s
);
2453 buffer_add_methodid (Buffer
*buf
, MonoDomain
*domain
, MonoMethod
*method
)
2455 buffer_add_ptr_id (buf
, domain
, ID_METHOD
, method
);
2456 if (G_UNLIKELY (log_level
>= 2) && method
) {
2459 s
= mono_method_full_name (method
, 1);
2460 if (is_debugger_thread ())
2461 DEBUG_PRINTF (2, "[dbg] send method [%s]\n", s
);
2463 DEBUG_PRINTF (2, "[%p] send method [%s]\n", (gpointer
) (gsize
) mono_native_thread_id_get (), s
);
2469 buffer_add_assemblyid (Buffer
*buf
, MonoDomain
*domain
, MonoAssembly
*assembly
)
2473 id
= buffer_add_ptr_id (buf
, domain
, ID_ASSEMBLY
, assembly
);
2474 if (G_UNLIKELY (log_level
>= 2) && assembly
)
2475 DEBUG_PRINTF (2, "[dbg] send assembly [%s][%s][%d]\n", assembly
->aname
.name
, domain
->friendly_name
, id
);
2479 buffer_add_moduleid (Buffer
*buf
, MonoDomain
*domain
, MonoImage
*image
)
2481 buffer_add_ptr_id (buf
, domain
, ID_MODULE
, image
);
2485 buffer_add_fieldid (Buffer
*buf
, MonoDomain
*domain
, MonoClassField
*field
)
2487 buffer_add_ptr_id (buf
, domain
, ID_FIELD
, field
);
2491 buffer_add_propertyid (Buffer
*buf
, MonoDomain
*domain
, MonoProperty
*property
)
2493 buffer_add_ptr_id (buf
, domain
, ID_PROPERTY
, property
);
2497 buffer_add_domainid (Buffer
*buf
, MonoDomain
*domain
)
2499 buffer_add_ptr_id (buf
, domain
, ID_DOMAIN
, domain
);
2502 static void invoke_method (void);
2509 get_top_method_ji (gpointer ip
, MonoDomain
**domain
, gpointer
*out_ip
)
2516 ji
= mini_jit_info_table_find (mono_domain_get (), (char*)ip
, domain
);
2518 /* Could be an interpreter method */
2520 MonoLMF
*lmf
= mono_get_lmf ();
2521 MonoInterpFrameHandle
*frame
;
2523 g_assert (((gsize
)lmf
->previous_lmf
) & 2);
2524 MonoLMFExt
*ext
= (MonoLMFExt
*)lmf
;
2526 g_assert (ext
->kind
== MONO_LMFEXT_INTERP_EXIT
|| ext
->kind
== MONO_LMFEXT_INTERP_EXIT_WITH_CTX
);
2527 frame
= (MonoInterpFrameHandle
*)ext
->interp_exit_data
;
2528 ji
= mini_get_interp_callbacks ()->frame_get_jit_info (frame
);
2530 *domain
= mono_domain_get ();
2532 *out_ip
= mini_get_interp_callbacks ()->frame_get_ip (frame
);
2538 * save_thread_context:
2540 * Set CTX as the current threads context which is used for computing stack traces.
2541 * This function is signal-safe.
2544 save_thread_context (MonoContext
*ctx
)
2546 DebuggerTlsData
*tls
;
2548 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
2552 mono_thread_state_init_from_monoctx (&tls
->context
, ctx
);
2554 mono_thread_state_init_from_current (&tls
->context
);
2557 static MonoCoopMutex suspend_mutex
;
2559 /* Cond variable used to wait for suspend_count becoming 0 */
2560 static MonoCoopCond suspend_cond
;
2562 /* Semaphore used to wait for a thread becoming suspended */
2563 static MonoCoopSem suspend_sem
;
2568 mono_coop_mutex_init (&suspend_mutex
);
2569 mono_coop_cond_init (&suspend_cond
);
2570 mono_coop_sem_init (&suspend_sem
, 0);
2575 StackFrameInfo last_frame
;
2576 gboolean last_frame_set
;
2580 } GetLastFrameUserData
;
2583 get_last_frame (StackFrameInfo
*info
, MonoContext
*ctx
, gpointer user_data
)
2585 GetLastFrameUserData
*data
= (GetLastFrameUserData
*)user_data
;
2587 if (info
->type
== FRAME_TYPE_MANAGED_TO_NATIVE
|| info
->type
== FRAME_TYPE_TRAMPOLINE
)
2590 if (!data
->last_frame_set
) {
2591 /* Store the last frame */
2592 memcpy (&data
->last_frame
, info
, sizeof (StackFrameInfo
));
2593 data
->last_frame_set
= TRUE
;
2596 /* Store the context/lmf for the frame above the last frame */
2597 memcpy (&data
->ctx
, ctx
, sizeof (MonoContext
));
2598 data
->lmf
= info
->lmf
;
2599 data
->domain
= info
->domain
;
2605 copy_unwind_state_from_frame_data (MonoThreadUnwindState
*to
, GetLastFrameUserData
*data
, gpointer jit_tls
)
2607 memcpy (&to
->ctx
, &data
->ctx
, sizeof (MonoContext
));
2609 to
->unwind_data
[MONO_UNWIND_DATA_DOMAIN
] = data
->domain
;
2610 to
->unwind_data
[MONO_UNWIND_DATA_LMF
] = data
->lmf
;
2611 to
->unwind_data
[MONO_UNWIND_DATA_JIT_TLS
] = jit_tls
;
2618 * Process interruption of a thread. This should be signal safe.
2620 * This always runs in the debugger thread.
2623 thread_interrupt (DebuggerTlsData
*tls
, MonoThreadInfo
*info
, MonoJitInfo
*ji
)
2626 MonoNativeThreadId tid
;
2630 ip
= MONO_CONTEXT_GET_IP (&mono_thread_info_get_suspend_state (info
)->ctx
);
2631 tid
= mono_thread_info_get_tid (info
);
2633 // FIXME: Races when the thread leaves managed code before hitting a single step
2636 if (ji
&& !ji
->is_trampoline
) {
2637 /* Running managed code, will be suspended by the single step code */
2638 DEBUG_PRINTF (1, "[%p] Received interrupt while at %s(%p), continuing.\n", (gpointer
)(gsize
)tid
, jinfo_get_method (ji
)->name
, ip
);
2641 * Running native code, will be suspended when it returns to/enters
2642 * managed code. Treat it as already suspended.
2643 * This might interrupt the code in mono_de_process_single_step (), we use the
2644 * tls->suspending flag to avoid races when that happens.
2646 if (!tls
->suspended
&& !tls
->suspending
) {
2647 GetLastFrameUserData data
;
2649 // FIXME: printf is not signal safe, but this is only used during
2650 // debugger debugging
2652 DEBUG_PRINTF (1, "[%p] Received interrupt while at %p, treating as suspended.\n", (gpointer
)(gsize
)tid
, ip
);
2653 //save_thread_context (&ctx);
2656 /* Already terminated */
2660 * We are in a difficult position: we want to be able to provide stack
2661 * traces for this thread, but we can't use the current ctx+lmf, since
2662 * the thread is still running, so it might return to managed code,
2663 * making these invalid.
2664 * So we start a stack walk and save the first frame, along with the
2665 * parent frame's ctx+lmf. This (hopefully) works because the thread will be
2666 * suspended when it returns to managed code, so the parent's ctx should
2669 MonoThreadUnwindState
*state
= mono_thread_info_get_suspend_state (info
);
2671 data
.last_frame_set
= FALSE
;
2672 mono_get_eh_callbacks ()->mono_walk_stack_with_state (get_last_frame
, state
, MONO_UNWIND_SIGNAL_SAFE
, &data
);
2673 if (data
.last_frame_set
) {
2674 gpointer jit_tls
= tls
->thread
->thread_info
->jit_data
;
2676 memcpy (&tls
->async_last_frame
, &data
.last_frame
, sizeof (StackFrameInfo
));
2678 if (data
.last_frame
.type
== FRAME_TYPE_INTERP_TO_MANAGED
|| data
.last_frame
.type
== FRAME_TYPE_INTERP_TO_MANAGED_WITH_CTX
) {
2680 * Store the current lmf instead of the parent one, since that
2681 * contains the interp exit data.
2683 data
.lmf
= state
->unwind_data
[MONO_UNWIND_DATA_LMF
];
2686 copy_unwind_state_from_frame_data (&tls
->async_state
, &data
, jit_tls
);
2687 /* Don't set tls->context, it could race with the thread processing a breakpoint etc. */
2689 tls
->async_state
.valid
= FALSE
;
2692 mono_memory_barrier ();
2694 tls
->suspended
= TRUE
;
2695 mono_coop_sem_post (&suspend_sem
);
2701 * reset_native_thread_suspend_state:
2703 * Reset the suspended flag and state on native threads
2706 reset_native_thread_suspend_state (gpointer key
, gpointer value
, gpointer user_data
)
2708 DebuggerTlsData
*tls
= (DebuggerTlsData
*)value
;
2710 if (!tls
->really_suspended
&& tls
->suspended
) {
2711 tls
->suspended
= FALSE
;
2713 * The thread might still be running if it was executing native code, so the state won't be invalided by
2714 * suspend_current ().
2716 tls
->context
.valid
= FALSE
;
2717 tls
->async_state
.valid
= FALSE
;
2718 invalidate_frames (tls
);
2720 tls
->resume_count_internal
++;
2725 DebuggerTlsData
*tls
;
2726 gboolean valid_info
;
2729 static SuspendThreadResult
2730 debugger_interrupt_critical (MonoThreadInfo
*info
, gpointer user_data
)
2732 InterruptData
*data
= (InterruptData
*)user_data
;
2735 data
->valid_info
= TRUE
;
2736 MonoDomain
*domain
= (MonoDomain
*) mono_thread_info_get_suspend_state (info
)->unwind_data
[MONO_UNWIND_DATA_DOMAIN
];
2741 ji
= mono_jit_info_table_find_internal ( domain
, MONO_CONTEXT_GET_IP (&mono_thread_info_get_suspend_state (info
)->ctx
), TRUE
, TRUE
);
2744 /* This is signal safe */
2745 thread_interrupt (data
->tls
, info
, ji
);
2746 return MonoResumeThread
;
2752 * Notify a thread that it needs to suspend.
2755 notify_thread (gpointer key
, gpointer value
, gpointer user_data
)
2757 MonoInternalThread
*thread
= (MonoInternalThread
*)key
;
2758 DebuggerTlsData
*tls
= (DebuggerTlsData
*)value
;
2759 MonoNativeThreadId tid
= MONO_UINT_TO_NATIVE_THREAD_ID (thread
->tid
);
2761 if (mono_thread_internal_is_current (thread
) || tls
->terminated
)
2764 DEBUG_PRINTF (1, "[%p] Interrupting %p...\n", (gpointer
) (gsize
) mono_native_thread_id_get (), (gpointer
)tid
);
2766 /* This is _not_ equivalent to mono_thread_internal_abort () */
2767 InterruptData interrupt_data
= { 0 };
2768 interrupt_data
.tls
= tls
;
2770 mono_thread_info_safe_suspend_and_run ((MonoNativeThreadId
)(gsize
)thread
->tid
, FALSE
, debugger_interrupt_critical
, &interrupt_data
);
2771 if (!interrupt_data
.valid_info
) {
2772 DEBUG_PRINTF (1, "[%p] mono_thread_info_suspend_sync () failed for %p...\n", (gpointer
) (gsize
) mono_native_thread_id_get (), (gpointer
)tid
);
2774 * Attached thread which died without detaching.
2776 tls
->terminated
= TRUE
;
2781 process_suspend (DebuggerTlsData
*tls
, MonoContext
*ctx
)
2783 guint8
*ip
= (guint8
*)MONO_CONTEXT_GET_IP (ctx
);
2787 if (mono_loader_lock_is_owned_by_self ()) {
2789 * Shortcut for the check in suspend_current (). This speeds up processing
2790 * when executing long running code inside the loader lock, i.e. assembly load
2796 if (is_debugger_thread ())
2799 /* Prevent races with mono_debugger_agent_thread_interrupt () */
2800 if (suspend_count
- tls
->resume_count
> 0)
2801 tls
->suspending
= TRUE
;
2803 DEBUG_PRINTF (1, "[%p] Received single step event for suspending.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
2805 if (suspend_count
- tls
->resume_count
== 0) {
2807 * We are executing a single threaded invoke but the single step for
2808 * suspending is still active.
2809 * FIXME: This slows down single threaded invokes.
2811 DEBUG_PRINTF (1, "[%p] Ignored during single threaded invoke.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
2815 ji
= get_top_method_ji (ip
, NULL
, NULL
);
2817 /* Can't suspend in these methods */
2818 method
= jinfo_get_method (ji
);
2819 if (method
->klass
== mono_defaults
.string_class
&& (!strcmp (method
->name
, "memset") || strstr (method
->name
, "memcpy")))
2822 save_thread_context (ctx
);
2828 /* Conditionally call process_suspend depending oh the current state */
2830 try_process_suspend (void *the_tls
, MonoContext
*ctx
, gboolean from_breakpoint
)
2832 DebuggerTlsData
*tls
= (DebuggerTlsData
*)the_tls
;
2833 /* if there is a suspend pending that is not executed yes */
2834 if (suspend_count
> 0) {
2835 /* Fastpath during invokes, see in process_suspend () */
2836 /* if there is a suspend pending but this thread is already resumed, we shouldn't suspend it again and the breakpoint/ss can run */
2837 if (suspend_count
- tls
->resume_count
== 0)
2839 /* if there is in a invoke the breakpoint/step should be executed even with the suspend pending */
2842 /* with the multithreaded single step check if there is a suspend_count pending in the current thread and not in the vm */
2843 if (from_breakpoint
&& tls
->suspend_count
<= tls
->resume_count_internal
)
2845 process_suspend (tls
, ctx
);
2847 } /* if there isn't any suspend pending, the breakpoint/ss will be executed and will suspend then vm when the event is sent */
2854 * Increase the suspend count of the VM. While the suspend count is greater
2855 * than 0, runtime threads are suspended at certain points during execution.
2860 gboolean tp_suspend
= FALSE
;
2861 mono_loader_lock ();
2863 mono_coop_mutex_lock (&suspend_mutex
);
2867 DEBUG_PRINTF (1, "[%p] Suspending vm...\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
2869 if (suspend_count
== 1) {
2870 // FIXME: Is it safe to call this inside the lock ?
2871 mono_de_start_single_stepping ();
2872 mono_g_hash_table_foreach (thread_to_tls
, notify_thread
, NULL
);
2875 mono_coop_mutex_unlock (&suspend_mutex
);
2877 if (suspend_count
== 1)
2879 * Suspend creation of new threadpool threads, since they cannot run
2882 mono_loader_unlock ();
2884 #ifndef ENABLE_NETCORE
2886 mono_threadpool_suspend ();
2893 * Decrease the suspend count of the VM. If the count reaches 0, runtime threads
2899 g_assert (is_debugger_thread ());
2900 gboolean tp_resume
= FALSE
;
2902 mono_loader_lock ();
2904 mono_coop_mutex_lock (&suspend_mutex
);
2906 g_assert (suspend_count
> 0);
2909 DEBUG_PRINTF (1, "[%p] Resuming vm, suspend count=%d...\n", (gpointer
) (gsize
) mono_native_thread_id_get (), suspend_count
);
2911 if (suspend_count
== 0) {
2912 // FIXME: Is it safe to call this inside the lock ?
2913 mono_de_stop_single_stepping ();
2914 mono_g_hash_table_foreach (thread_to_tls
, reset_native_thread_suspend_state
, NULL
);
2917 /* Signal this even when suspend_count > 0, since some threads might have resume_count > 0 */
2918 mono_coop_cond_broadcast (&suspend_cond
);
2920 mono_coop_mutex_unlock (&suspend_mutex
);
2921 //g_assert (err == 0);
2923 if (suspend_count
== 0)
2925 mono_loader_unlock ();
2927 #ifndef ENABLE_NETCORE
2929 mono_threadpool_resume ();
2936 * Resume just one thread.
2939 resume_thread (MonoInternalThread
*thread
)
2941 DebuggerTlsData
*tls
;
2943 g_assert (is_debugger_thread ());
2945 mono_loader_lock ();
2947 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
2950 mono_coop_mutex_lock (&suspend_mutex
);
2952 g_assert (suspend_count
> 0);
2954 DEBUG_PRINTF (1, "[sdb] Resuming thread %p...\n", (gpointer
)(gssize
)thread
->tid
);
2956 tls
->resume_count
+= suspend_count
;
2957 tls
->resume_count_internal
+= tls
->suspend_count
;
2958 tls
->suspend_count
= 0;
2961 * Signal suspend_count without decreasing suspend_count, the threads will wake up
2962 * but only the one whose resume_count field is > 0 will be resumed.
2964 mono_coop_cond_broadcast (&suspend_cond
);
2966 mono_coop_mutex_unlock (&suspend_mutex
);
2967 //g_assert (err == 0);
2969 mono_loader_unlock ();
2973 free_frames (StackFrame
**frames
, int nframes
)
2977 for (i
= 0; i
< nframes
; ++i
) {
2978 if (frames
[i
]->jit
)
2979 mono_debug_free_method_jit_info (frames
[i
]->jit
);
2980 g_free (frames
[i
]);
2986 invalidate_frames (DebuggerTlsData
*tls
)
2988 mono_loader_lock ();
2991 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
2994 free_frames (tls
->frames
, tls
->frame_count
);
2995 tls
->frame_count
= 0;
2998 free_frames (tls
->restore_frames
, tls
->restore_frame_count
);
2999 tls
->restore_frame_count
= 0;
3000 tls
->restore_frames
= NULL
;
3002 mono_loader_unlock ();
3008 * Suspend the current thread until the runtime is resumed. If the thread has a
3009 * pending invoke, then the invoke is executed before this function returns.
3012 suspend_current (void)
3014 DebuggerTlsData
*tls
;
3016 g_assert (!is_debugger_thread ());
3018 if (mono_loader_lock_is_owned_by_self ()) {
3020 * If we own the loader mutex, can't suspend until we release it, since the
3021 * whole runtime can deadlock otherwise.
3026 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
3029 gboolean do_resume
= FALSE
;
3030 while (!do_resume
) {
3031 mono_coop_mutex_lock (&suspend_mutex
);
3033 tls
->suspending
= FALSE
;
3034 tls
->really_suspended
= TRUE
;
3036 if (!tls
->suspended
) {
3037 tls
->suspended
= TRUE
;
3038 mono_coop_sem_post (&suspend_sem
);
3041 mono_debugger_log_suspend (tls
);
3042 DEBUG_PRINTF (1, "[%p] Suspended.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
3044 while (suspend_count
- tls
->resume_count
> 0) {
3045 mono_coop_cond_wait (&suspend_cond
, &suspend_mutex
);
3048 tls
->suspended
= FALSE
;
3049 tls
->really_suspended
= FALSE
;
3051 mono_coop_mutex_unlock (&suspend_mutex
);
3053 mono_debugger_log_resume (tls
);
3054 DEBUG_PRINTF (1, "[%p] Resumed.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
3056 if (tls
->pending_invoke
) {
3057 /* Save the original context */
3058 tls
->pending_invoke
->has_ctx
= TRUE
;
3059 tls
->pending_invoke
->ctx
= tls
->context
.ctx
;
3063 /* Have to suspend again */
3069 /* The frame info becomes invalid after a resume */
3070 tls
->context
.valid
= FALSE
;
3071 tls
->async_state
.valid
= FALSE
;
3072 invalidate_frames (tls
);
3073 mono_stopwatch_start (&tls
->step_time
);
3077 count_thread (gpointer key
, gpointer value
, gpointer user_data
)
3079 DebuggerTlsData
*tls
= (DebuggerTlsData
*)value
;
3081 if (!tls
->suspended
&& !tls
->terminated
&& !mono_thread_internal_is_current (tls
->thread
))
3082 *(int*)user_data
= *(int*)user_data
+ 1;
3086 count_threads_to_wait_for (void)
3090 mono_loader_lock ();
3091 mono_g_hash_table_foreach (thread_to_tls
, count_thread
, &count
);
3092 mono_loader_unlock ();
3100 * Wait until the runtime is completely suspended.
3103 wait_for_suspend (void)
3105 int nthreads
, nwait
, err
;
3106 gboolean waited
= FALSE
;
3108 // FIXME: Threads starting/stopping ?
3109 mono_loader_lock ();
3110 nthreads
= mono_g_hash_table_size (thread_to_tls
);
3111 mono_loader_unlock ();
3114 nwait
= count_threads_to_wait_for ();
3116 DEBUG_PRINTF (1, "Waiting for %d(%d) threads to suspend...\n", nwait
, nthreads
);
3117 err
= mono_coop_sem_wait (&suspend_sem
, MONO_SEM_FLAGS_NONE
);
3118 g_assert (err
== 0);
3126 DEBUG_PRINTF (1, "%d threads suspended.\n", nthreads
);
3132 * Return whenever the runtime is suspended.
3137 return count_threads_to_wait_for () == 0;
3141 no_seq_points_found (MonoMethod
*method
, int offset
)
3144 * This can happen in full-aot mode with assemblies AOTed without the 'soft-debug' option to save space.
3146 printf ("Unable to find seq points for method '%s', offset 0x%x.\n", mono_method_full_name (method
, TRUE
), offset
);
3150 calc_il_offset (MonoDomain
*domain
, MonoMethod
*method
, int native_offset
, gboolean is_top_frame
)
3155 /* mono_debug_il_offset_from_address () doesn't seem to be precise enough (#2092) */
3156 if (mono_find_prev_seq_point_for_native_offset (domain
, method
, native_offset
, NULL
, &sp
))
3160 ret
= mono_debug_il_offset_from_address (method
, domain
, native_offset
);
3165 DebuggerTlsData
*tls
;
3167 gboolean set_debugger_flag
;
3168 } ComputeFramesUserData
;
3171 process_frame (StackFrameInfo
*info
, MonoContext
*ctx
, gpointer user_data
)
3173 ComputeFramesUserData
*ud
= (ComputeFramesUserData
*)user_data
;
3175 MonoMethod
*method
, *actual_method
, *api_method
;
3178 mono_loader_lock ();
3179 if (info
->type
!= FRAME_TYPE_MANAGED
&& info
->type
!= FRAME_TYPE_INTERP
&& info
->type
!= FRAME_TYPE_MANAGED_TO_NATIVE
) {
3180 if (info
->type
== FRAME_TYPE_DEBUGGER_INVOKE
) {
3181 /* Mark the last frame as an invoke frame */
3183 ((StackFrame
*)g_slist_last (ud
->frames
)->data
)->flags
|= FRAME_FLAG_DEBUGGER_INVOKE
;
3185 ud
->set_debugger_flag
= TRUE
;
3187 mono_loader_unlock ();
3192 method
= jinfo_get_method (info
->ji
);
3194 method
= info
->method
;
3195 actual_method
= info
->actual_method
;
3196 api_method
= method
;
3199 mono_loader_unlock ();
3203 if (!method
|| (method
->wrapper_type
&& method
->wrapper_type
!= MONO_WRAPPER_DYNAMIC_METHOD
&& method
->wrapper_type
!= MONO_WRAPPER_MANAGED_TO_NATIVE
)) {
3204 mono_loader_unlock ();
3208 if (info
->il_offset
== -1) {
3209 info
->il_offset
= calc_il_offset (info
->domain
, method
, info
->native_offset
, ud
->frames
== NULL
);
3212 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
);
3214 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
) {
3215 if (!CHECK_PROTOCOL_VERSION (2, 17)) {
3216 /* Older clients can't handle this flag */
3217 mono_loader_unlock ();
3220 api_method
= mono_marshal_method_from_wrapper (method
);
3222 mono_loader_unlock ();
3225 actual_method
= api_method
;
3226 flags
|= FRAME_FLAG_NATIVE_TRANSITION
;
3229 if (ud
->set_debugger_flag
) {
3230 g_assert (g_slist_length (ud
->frames
) == 0);
3231 flags
|= FRAME_FLAG_DEBUGGER_INVOKE
;
3232 ud
->set_debugger_flag
= FALSE
;
3235 frame
= g_new0 (StackFrame
, 1);
3236 frame
->de
.ji
= info
->ji
;
3237 frame
->de
.domain
= info
->domain
;
3238 frame
->de
.method
= method
;
3239 frame
->de
.native_offset
= info
->native_offset
;
3241 frame
->actual_method
= actual_method
;
3242 frame
->api_method
= api_method
;
3243 frame
->il_offset
= info
->il_offset
;
3244 frame
->flags
= flags
;
3245 frame
->interp_frame
= info
->interp_frame
;
3246 frame
->frame_addr
= info
->frame_addr
;
3247 if (info
->reg_locations
)
3248 memcpy (frame
->reg_locations
, info
->reg_locations
, MONO_MAX_IREGS
* sizeof (host_mgreg_t
*));
3251 frame
->has_ctx
= TRUE
;
3254 ud
->frames
= g_slist_append (ud
->frames
, frame
);
3256 mono_loader_unlock ();
3260 static gint32
isFixedSizeArray (MonoClassField
*f
)
3263 if (!CHECK_PROTOCOL_VERSION (2, 53) || f
->type
->type
!= MONO_TYPE_VALUETYPE
) {
3266 MonoCustomAttrInfo
*cinfo
;
3267 MonoCustomAttrEntry
*attr
;
3270 cinfo
= mono_custom_attrs_from_field_checked (f
->parent
, f
, error
);
3271 goto_if_nok (error
, leave
);
3274 for (aindex
= 0; aindex
< cinfo
->num_attrs
; ++aindex
) {
3275 MonoClass
*ctor_class
= cinfo
->attrs
[aindex
].ctor
->klass
;
3276 MonoClass
*fixed_size_class
= mono_class_try_get_fixed_buffer_class ();
3277 if (fixed_size_class
!= NULL
&& mono_class_has_parent (ctor_class
, fixed_size_class
)) {
3278 attr
= &cinfo
->attrs
[aindex
];
3279 gpointer
*typed_args
, *named_args
;
3280 CattrNamedArg
*arginfo
;
3283 mono_reflection_create_custom_attr_data_args_noalloc (mono_defaults
.corlib
, attr
->ctor
, attr
->data
, attr
->data_size
,
3284 &typed_args
, &named_args
, &num_named_args
, &arginfo
, error
);
3285 if (!is_ok (error
)) {
3289 ret
= *(gint32
*)typed_args
[1];
3290 g_free (typed_args
[1]);
3291 g_free (typed_args
);
3292 g_free (named_args
);
3299 mono_error_cleanup (error
);
3304 process_filter_frame (StackFrameInfo
*info
, MonoContext
*ctx
, gpointer user_data
)
3306 ComputeFramesUserData
*ud
= (ComputeFramesUserData
*)user_data
;
3309 * 'tls->filter_ctx' is the location of the throw site.
3311 * mono_walk_stack() will never actually hit the throw site, but unwind
3312 * directly from the filter to the call site; we abort stack unwinding here
3313 * once this happens and resume from the throw site.
3315 if (info
->frame_addr
>= MONO_CONTEXT_GET_SP (&ud
->tls
->filter_state
.ctx
))
3318 return process_frame (info
, ctx
, user_data
);
3322 * Return a malloc-ed list of StackFrame structures.
3325 compute_frame_info_from (MonoInternalThread
*thread
, DebuggerTlsData
*tls
, MonoThreadUnwindState
*state
, int *out_nframes
)
3327 ComputeFramesUserData user_data
;
3328 MonoUnwindOptions opts
= (MonoUnwindOptions
)(MONO_UNWIND_DEFAULT
| MONO_UNWIND_REG_LOCATIONS
);
3333 user_data
.tls
= tls
;
3334 user_data
.frames
= NULL
;
3336 mono_walk_stack_with_state (process_frame
, state
, opts
, &user_data
);
3338 nframes
= g_slist_length (user_data
.frames
);
3339 res
= g_new0 (StackFrame
*, nframes
);
3340 l
= user_data
.frames
;
3341 for (i
= 0; i
< nframes
; ++i
) {
3342 res
[i
] = (StackFrame
*)l
->data
;
3345 *out_nframes
= nframes
;
3351 compute_frame_info (MonoInternalThread
*thread
, DebuggerTlsData
*tls
, gboolean force_update
)
3353 ComputeFramesUserData user_data
;
3355 int i
, findex
, new_frame_count
;
3356 StackFrame
**new_frames
, *f
;
3357 MonoUnwindOptions opts
= (MonoUnwindOptions
)(MONO_UNWIND_DEFAULT
| MONO_UNWIND_REG_LOCATIONS
);
3359 // FIXME: Locking on tls
3360 if (tls
->frames
&& tls
->frames_up_to_date
&& !force_update
)
3363 DEBUG_PRINTF (1, "Frames for %p(tid=%lx):\n", thread
, (glong
)thread
->tid
);
3365 if (CHECK_PROTOCOL_VERSION (2, 52)) {
3366 if (tls
->restore_state
.valid
&& MONO_CONTEXT_GET_IP (&tls
->context
.ctx
) != MONO_CONTEXT_GET_IP (&tls
->restore_state
.ctx
)) {
3367 new_frames
= compute_frame_info_from (thread
, tls
, &tls
->restore_state
, &new_frame_count
);
3368 invalidate_frames (tls
);
3370 tls
->frames
= new_frames
;
3371 tls
->frame_count
= new_frame_count
;
3372 tls
->frames_up_to_date
= TRUE
;
3377 user_data
.tls
= tls
;
3378 user_data
.frames
= NULL
;
3379 if (tls
->terminated
) {
3380 tls
->frame_count
= 0;
3382 } if (!tls
->really_suspended
&& tls
->async_state
.valid
) {
3383 /* Have to use the state saved by the signal handler */
3384 process_frame (&tls
->async_last_frame
, NULL
, &user_data
);
3385 mono_walk_stack_with_state (process_frame
, &tls
->async_state
, opts
, &user_data
);
3386 } else if (tls
->filter_state
.valid
) {
3388 * We are inside an exception filter.
3390 * First we add all the frames from inside the filter; 'tls->ctx' has the current context.
3392 if (tls
->context
.valid
) {
3393 mono_walk_stack_with_state (process_filter_frame
, &tls
->context
, opts
, &user_data
);
3394 DEBUG_PRINTF (1, "\tFrame: <call filter>\n");
3397 * After that, we resume unwinding from the location where the exception has been thrown.
3399 mono_walk_stack_with_state (process_frame
, &tls
->filter_state
, opts
, &user_data
);
3400 } else if (tls
->context
.valid
) {
3401 mono_walk_stack_with_state (process_frame
, &tls
->context
, opts
, &user_data
);
3404 tls
->frame_count
= 0;
3408 new_frame_count
= g_slist_length (user_data
.frames
);
3409 new_frames
= g_new0 (StackFrame
*, new_frame_count
);
3411 for (tmp
= user_data
.frames
; tmp
; tmp
= tmp
->next
) {
3412 f
= (StackFrame
*)tmp
->data
;
3415 * Reuse the id for already existing stack frames, so invokes don't invalidate
3416 * the still valid stack frames.
3418 for (i
= 0; i
< tls
->frame_count
; ++i
) {
3419 if (tls
->frames
[i
]->frame_addr
== f
->frame_addr
) {
3420 f
->id
= tls
->frames
[i
]->id
;
3425 if (i
>= tls
->frame_count
)
3426 f
->id
= mono_atomic_inc_i32 (&frame_id
);
3428 new_frames
[findex
++] = f
;
3431 g_slist_free (user_data
.frames
);
3433 invalidate_frames (tls
);
3435 tls
->frames
= new_frames
;
3436 tls
->frame_count
= new_frame_count
;
3437 tls
->frames_up_to_date
= TRUE
;
3439 if (CHECK_PROTOCOL_VERSION (2, 52)) {
3440 MonoJitTlsData
*jit_data
= thread
->thread_info
->jit_data
;
3441 gboolean has_interp_resume_state
= FALSE
;
3442 MonoInterpFrameHandle interp_resume_frame
= NULL
;
3443 gpointer interp_resume_ip
= 0;
3444 mini_get_interp_callbacks ()->get_resume_state (jit_data
, &has_interp_resume_state
, &interp_resume_frame
, &interp_resume_ip
);
3445 if (has_interp_resume_state
&& tls
->frame_count
> 0) {
3446 StackFrame
*top_frame
= tls
->frames
[0];
3447 if (interp_resume_frame
== top_frame
->interp_frame
) {
3448 int native_offset
= (int) ((uintptr_t) interp_resume_ip
- (uintptr_t) top_frame
->de
.ji
->code_start
);
3449 top_frame
->il_offset
= calc_il_offset (top_frame
->de
.domain
, top_frame
->de
.method
, native_offset
, TRUE
);
3456 * GHFunc to emit an appdomain creation event
3457 * @param key Don't care
3458 * @param value A loaded appdomain
3459 * @param user_data Don't care
3462 emit_appdomain_load (gpointer key
, gpointer value
, gpointer user_data
)
3464 process_profiler_event (EVENT_KIND_APPDOMAIN_CREATE
, value
);
3465 g_hash_table_foreach (get_agent_domain_info ((MonoDomain
*)value
)->loaded_classes
, emit_type_load
, NULL
);
3469 * GHFunc to emit a thread start event
3470 * @param key A thread id
3471 * @param value A thread object
3472 * @param user_data Don't care
3475 emit_thread_start (gpointer key
, gpointer value
, gpointer user_data
)
3477 g_assert (!mono_native_thread_id_equals (MONO_UINT_TO_NATIVE_THREAD_ID (GPOINTER_TO_UINT (key
)), debugger_thread_id
));
3478 process_profiler_event (EVENT_KIND_THREAD_START
, value
);
3482 * GFunc to emit an assembly load event
3483 * @param value A loaded assembly
3484 * @param user_data Don't care
3487 emit_assembly_load (gpointer value
, gpointer user_data
)
3489 process_profiler_event (EVENT_KIND_ASSEMBLY_LOAD
, value
);
3493 * GFunc to emit a type load event
3494 * @param value A loaded type
3495 * @param user_data Don't care
3498 emit_type_load (gpointer key
, gpointer value
, gpointer user_data
)
3500 process_profiler_event (EVENT_KIND_TYPE_LOAD
, value
);
3504 static void gc_finalizing (MonoProfiler
*prof
)
3506 DebuggerTlsData
*tls
;
3508 if (is_debugger_thread ())
3511 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
3513 tls
->gc_finalizing
= TRUE
;
3516 static void gc_finalized (MonoProfiler
*prof
)
3518 DebuggerTlsData
*tls
;
3520 if (is_debugger_thread ())
3523 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
3525 tls
->gc_finalizing
= FALSE
;
3530 strdup_tolower (char *s
)
3535 for (p
= s2
; *p
; ++p
)
3541 * Same as g_path_get_basename () but handles windows paths as well,
3542 * which can occur in .mdb files created by pdb2mdb.
3545 dbg_path_get_basename (const char *filename
)
3549 if (!filename
|| strchr (filename
, '/') || !strchr (filename
, '\\'))
3550 return g_path_get_basename (filename
);
3554 /* No separator -> filename */
3555 r
= (char*)strrchr (filename
, '\\');
3557 return g_strdup (filename
);
3559 /* Trailing slash, remove component */
3561 char *copy
= g_strdup (filename
);
3562 copy
[r
-filename
] = 0;
3563 r
= strrchr (copy
, '\\');
3567 return g_strdup ("/");
3569 r
= g_strdup (&r
[1]);
3574 return g_strdup (&r
[1]);
3578 init_jit_info_dbg_attrs (MonoJitInfo
*ji
)
3581 MonoCustomAttrInfo
*ainfo
;
3583 if (ji
->dbg_attrs_inited
)
3586 MONO_STATIC_POINTER_INIT (MonoClass
, hidden_klass
)
3587 hidden_klass
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Diagnostics", "DebuggerHiddenAttribute");
3588 MONO_STATIC_POINTER_INIT_END (MonoClass
, hidden_klass
)
3591 MONO_STATIC_POINTER_INIT (MonoClass
, step_through_klass
)
3592 step_through_klass
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Diagnostics", "DebuggerStepThroughAttribute");
3593 MONO_STATIC_POINTER_INIT_END (MonoClass
, step_through_klass
)
3595 MONO_STATIC_POINTER_INIT (MonoClass
, non_user_klass
)
3596 non_user_klass
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Diagnostics", "DebuggerNonUserCodeAttribute");
3597 MONO_STATIC_POINTER_INIT_END (MonoClass
, non_user_klass
)
3599 ainfo
= mono_custom_attrs_from_method_checked (jinfo_get_method (ji
), error
);
3600 mono_error_cleanup (error
); /* FIXME don't swallow the error? */
3602 if (mono_custom_attrs_has_attr (ainfo
, hidden_klass
))
3603 ji
->dbg_hidden
= TRUE
;
3604 if (mono_custom_attrs_has_attr (ainfo
, step_through_klass
))
3605 ji
->dbg_step_through
= TRUE
;
3606 if (mono_custom_attrs_has_attr (ainfo
, non_user_klass
))
3607 ji
->dbg_non_user_code
= TRUE
;
3608 mono_custom_attrs_free (ainfo
);
3611 ainfo
= mono_custom_attrs_from_class_checked (jinfo_get_method (ji
)->klass
, error
);
3612 mono_error_cleanup (error
); /* FIXME don't swallow the error? */
3614 if (mono_custom_attrs_has_attr (ainfo
, step_through_klass
))
3615 ji
->dbg_step_through
= TRUE
;
3616 if (mono_custom_attrs_has_attr (ainfo
, non_user_klass
))
3617 ji
->dbg_non_user_code
= TRUE
;
3618 mono_custom_attrs_free (ainfo
);
3621 mono_memory_barrier ();
3622 ji
->dbg_attrs_inited
= TRUE
;
3630 * create_event_list:
3632 * Return a list of event request ids matching EVENT, starting from REQS, which
3633 * can be NULL to include all event requests. Set SUSPEND_POLICY to the suspend
3635 * We return request ids, instead of requests, to simplify threading, since
3636 * requests could be deleted anytime when the loader lock is not held.
3637 * LOCKING: Assumes the loader lock is held.
3640 create_event_list (EventKind event
, GPtrArray
*reqs
, MonoJitInfo
*ji
, EventInfo
*ei
, int *suspend_policy
)
3643 GSList
*events
= NULL
;
3645 *suspend_policy
= SUSPEND_POLICY_NONE
;
3648 reqs
= event_requests
;
3652 gboolean has_everything_else
= FALSE
;
3653 gboolean is_new_filtered_exception
= FALSE
;
3654 gboolean filteredException
= TRUE
;
3655 gint filtered_suspend_policy
= 0;
3656 gint filtered_req_id
= 0;
3657 gint everything_else_suspend_policy
= 0;
3658 gint everything_else_req_id
= 0;
3659 gboolean is_already_filtered
= FALSE
;
3660 for (i
= 0; i
< reqs
->len
; ++i
) {
3661 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (reqs
, i
);
3662 if (req
->event_kind
== event
) {
3663 gboolean filtered
= FALSE
;
3666 for (j
= 0; j
< req
->nmodifiers
; ++j
) {
3667 Modifier
*mod
= &req
->modifiers
[j
];
3669 if (mod
->kind
== MOD_KIND_COUNT
) {
3671 if (mod
->data
.count
> 0) {
3672 if (mod
->data
.count
> 0) {
3674 if (mod
->data
.count
== 0)
3678 } else if (mod
->kind
== MOD_KIND_THREAD_ONLY
) {
3679 if (mod
->data
.thread
!= mono_thread_internal_current ())
3681 } else if (mod
->kind
== MOD_KIND_EXCEPTION_ONLY
&& !mod
->not_filtered_feature
&& ei
) {
3682 if (mod
->data
.exc_class
&& mod
->subclasses
&& !mono_class_is_assignable_from_internal (mod
->data
.exc_class
, ei
->exc
->vtable
->klass
))
3684 if (mod
->data
.exc_class
&& !mod
->subclasses
&& mod
->data
.exc_class
!= ei
->exc
->vtable
->klass
)
3686 if (ei
->caught
&& !mod
->caught
)
3688 if (!ei
->caught
&& !mod
->uncaught
)
3690 } else if (mod
->kind
== MOD_KIND_EXCEPTION_ONLY
&& mod
->not_filtered_feature
&& ei
) {
3691 is_new_filtered_exception
= TRUE
;
3692 if ((mod
->data
.exc_class
&& mod
->subclasses
&& mono_class_is_assignable_from_internal (mod
->data
.exc_class
, ei
->exc
->vtable
->klass
)) ||
3693 (mod
->data
.exc_class
&& !mod
->subclasses
&& mod
->data
.exc_class
!= ei
->exc
->vtable
->klass
)) {
3694 is_already_filtered
= TRUE
;
3695 if ((ei
->caught
&& mod
->caught
) || (!ei
->caught
&& mod
->uncaught
)) {
3696 filteredException
= FALSE
;
3697 filtered_suspend_policy
= req
->suspend_policy
;
3698 filtered_req_id
= req
->id
;
3701 if (!mod
->data
.exc_class
&& mod
->everything_else
) {
3702 if ((ei
->caught
&& mod
->caught
) || (!ei
->caught
&& mod
->uncaught
)) {
3703 has_everything_else
= TRUE
;
3704 everything_else_req_id
= req
->id
;
3705 everything_else_suspend_policy
= req
->suspend_policy
;
3708 if (!mod
->data
.exc_class
&& !mod
->everything_else
) {
3709 if ((ei
->caught
&& mod
->caught
) || (!ei
->caught
&& mod
->uncaught
)) {
3710 filteredException
= FALSE
;
3711 filtered_suspend_policy
= req
->suspend_policy
;
3712 filtered_req_id
= req
->id
;
3715 } else if (mod
->kind
== MOD_KIND_ASSEMBLY_ONLY
&& ji
) {
3717 gboolean found
= FALSE
;
3718 MonoAssembly
**assemblies
= mod
->data
.assemblies
;
3721 for (k
= 0; assemblies
[k
]; ++k
)
3722 if (assemblies
[k
] == m_class_get_image (jinfo_get_method (ji
)->klass
)->assembly
)
3727 } else if (mod
->kind
== MOD_KIND_SOURCE_FILE_ONLY
&& ei
&& ei
->klass
) {
3728 gpointer iter
= NULL
;
3730 MonoDebugSourceInfo
*sinfo
;
3732 gboolean found
= FALSE
;
3734 GPtrArray
*source_file_list
;
3736 while ((method
= mono_class_get_methods (ei
->klass
, &iter
))) {
3737 MonoDebugMethodInfo
*minfo
= mono_debug_lookup_method (method
);
3740 mono_debug_get_seq_points (minfo
, NULL
, &source_file_list
, NULL
, NULL
, NULL
);
3741 for (i
= 0; i
< source_file_list
->len
; ++i
) {
3742 sinfo
= (MonoDebugSourceInfo
*)g_ptr_array_index (source_file_list
, i
);
3744 * Do a case-insesitive match by converting the file name to
3747 s
= strdup_tolower (sinfo
->source_file
);
3748 if (g_hash_table_lookup (mod
->data
.source_files
, s
))
3751 char *s2
= dbg_path_get_basename (sinfo
->source_file
);
3752 char *s3
= strdup_tolower (s2
);
3754 if (g_hash_table_lookup (mod
->data
.source_files
, s3
))
3761 g_ptr_array_free (source_file_list
, TRUE
);
3766 } else if (mod
->kind
== MOD_KIND_TYPE_NAME_ONLY
&& ei
&& ei
->klass
) {
3769 s
= mono_type_full_name (m_class_get_byval_arg (ei
->klass
));
3770 if (!g_hash_table_lookup (mod
->data
.type_names
, s
))
3773 } else if (mod
->kind
== MOD_KIND_STEP
) {
3774 if ((mod
->data
.filter
& STEP_FILTER_STATIC_CTOR
) && ji
&&
3775 (jinfo_get_method (ji
)->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) &&
3776 !strcmp (jinfo_get_method (ji
)->name
, ".cctor") &&
3777 (jinfo_get_method (ji
) != ((SingleStepReq
*)req
->info
)->start_method
))
3779 if ((mod
->data
.filter
& STEP_FILTER_DEBUGGER_HIDDEN
) && ji
) {
3780 init_jit_info_dbg_attrs (ji
);
3784 if ((mod
->data
.filter
& STEP_FILTER_DEBUGGER_STEP_THROUGH
) && ji
) {
3785 init_jit_info_dbg_attrs (ji
);
3786 if (ji
->dbg_step_through
)
3789 if ((mod
->data
.filter
& STEP_FILTER_DEBUGGER_NON_USER_CODE
) && ji
) {
3790 init_jit_info_dbg_attrs (ji
);
3791 if (ji
->dbg_non_user_code
)
3797 if (!filtered
&& !is_new_filtered_exception
) {
3798 *suspend_policy
= MAX (*suspend_policy
, req
->suspend_policy
);
3799 events
= g_slist_append (events
, GINT_TO_POINTER (req
->id
));
3804 if (has_everything_else
&& !is_already_filtered
) {
3805 filteredException
= FALSE
;
3806 filtered_suspend_policy
= everything_else_suspend_policy
;
3807 filtered_req_id
= everything_else_req_id
;
3810 if (!filteredException
) {
3811 *suspend_policy
= MAX (*suspend_policy
, filtered_suspend_policy
);
3812 events
= g_slist_append (events
, GINT_TO_POINTER (filtered_req_id
));
3815 /* Send a VM START/DEATH event by default */
3816 if (event
== EVENT_KIND_VM_START
)
3817 events
= g_slist_append (events
, GINT_TO_POINTER (0));
3818 if (event
== EVENT_KIND_VM_DEATH
)
3819 events
= g_slist_append (events
, GINT_TO_POINTER (0));
3824 static G_GNUC_UNUSED
const char*
3825 event_to_string (EventKind event
)
3828 case EVENT_KIND_VM_START
: return "VM_START";
3829 case EVENT_KIND_VM_DEATH
: return "VM_DEATH";
3830 case EVENT_KIND_THREAD_START
: return "THREAD_START";
3831 case EVENT_KIND_THREAD_DEATH
: return "THREAD_DEATH";
3832 case EVENT_KIND_APPDOMAIN_CREATE
: return "APPDOMAIN_CREATE";
3833 case EVENT_KIND_APPDOMAIN_UNLOAD
: return "APPDOMAIN_UNLOAD";
3834 case EVENT_KIND_METHOD_ENTRY
: return "METHOD_ENTRY";
3835 case EVENT_KIND_METHOD_EXIT
: return "METHOD_EXIT";
3836 case EVENT_KIND_ASSEMBLY_LOAD
: return "ASSEMBLY_LOAD";
3837 case EVENT_KIND_ASSEMBLY_UNLOAD
: return "ASSEMBLY_UNLOAD";
3838 case EVENT_KIND_BREAKPOINT
: return "BREAKPOINT";
3839 case EVENT_KIND_STEP
: return "STEP";
3840 case EVENT_KIND_TYPE_LOAD
: return "TYPE_LOAD";
3841 case EVENT_KIND_EXCEPTION
: return "EXCEPTION";
3842 case EVENT_KIND_KEEPALIVE
: return "KEEPALIVE";
3843 case EVENT_KIND_USER_BREAK
: return "USER_BREAK";
3844 case EVENT_KIND_USER_LOG
: return "USER_LOG";
3845 case EVENT_KIND_CRASH
: return "CRASH";
3847 g_assert_not_reached ();
3855 * Send an event to the client, suspending the vm if needed.
3856 * LOCKING: Since this can suspend the calling thread, no locks should be held
3858 * The EVENTS list is freed by this function.
3861 process_event (EventKind event
, gpointer arg
, gint32 il_offset
, MonoContext
*ctx
, GSList
*events
, int suspend_policy
)
3865 MonoDomain
*domain
= mono_domain_get ();
3866 MonoThread
*thread
= NULL
;
3867 MonoObject
*keepalive_obj
= NULL
;
3868 gboolean send_success
= FALSE
;
3873 DEBUG_PRINTF (2, "Debugger agent not initialized yet: dropping %s\n", event_to_string (event
));
3877 if (!vm_start_event_sent
&& event
!= EVENT_KIND_VM_START
) {
3878 // FIXME: We miss those events
3879 DEBUG_PRINTF (2, "VM start event not sent yet: dropping %s\n", event_to_string (event
));
3883 if (vm_death_event_sent
) {
3884 DEBUG_PRINTF (2, "VM death event has been sent: dropping %s\n", event_to_string (event
));
3888 if (mono_runtime_is_shutting_down () && event
!= EVENT_KIND_VM_DEATH
) {
3889 DEBUG_PRINTF (2, "Mono runtime is shutting down: dropping %s\n", event_to_string (event
));
3894 DEBUG_PRINTF (2, "Debugger client is not connected: dropping %s\n", event_to_string (event
));
3898 if (event
== EVENT_KIND_KEEPALIVE
)
3899 suspend_policy
= SUSPEND_POLICY_NONE
;
3904 if (agent_config
.defer
) {
3905 if (is_debugger_thread ()) {
3906 /* Don't suspend on events from the debugger thread */
3907 suspend_policy
= SUSPEND_POLICY_NONE
;
3910 if (is_debugger_thread () && event
!= EVENT_KIND_VM_DEATH
)
3911 // FIXME: Send these with a NULL thread, don't suspend the current thread
3916 if (event
== EVENT_KIND_VM_START
)
3917 suspend_policy
= agent_config
.suspend
? SUSPEND_POLICY_ALL
: SUSPEND_POLICY_NONE
;
3919 nevents
= g_slist_length (events
);
3920 buffer_init (&buf
, 128);
3921 buffer_add_byte (&buf
, suspend_policy
);
3922 buffer_add_int (&buf
, nevents
);
3924 for (l
= events
; l
; l
= l
->next
) {
3925 buffer_add_byte (&buf
, event
); // event kind
3926 buffer_add_int (&buf
, GPOINTER_TO_INT (l
->data
)); // request id
3930 if (event
== EVENT_KIND_VM_DEATH
) {
3934 thread
= is_debugger_thread () ? mono_thread_get_main () : mono_thread_current ();
3936 if (event
== EVENT_KIND_VM_START
&& arg
!= NULL
)
3937 thread
= (MonoThread
*)arg
;
3940 buffer_add_objid (&buf
, (MonoObject
*)thread
); // thread
3943 case EVENT_KIND_THREAD_START
:
3944 case EVENT_KIND_THREAD_DEATH
:
3946 case EVENT_KIND_APPDOMAIN_CREATE
:
3947 case EVENT_KIND_APPDOMAIN_UNLOAD
:
3948 buffer_add_domainid (&buf
, (MonoDomain
*)arg
);
3950 case EVENT_KIND_METHOD_ENTRY
:
3951 case EVENT_KIND_METHOD_EXIT
:
3952 buffer_add_methodid (&buf
, domain
, (MonoMethod
*)arg
);
3954 case EVENT_KIND_ASSEMBLY_LOAD
:
3955 buffer_add_assemblyid (&buf
, domain
, (MonoAssembly
*)arg
);
3957 case EVENT_KIND_ASSEMBLY_UNLOAD
: {
3958 DebuggerTlsData
*tls
;
3960 /* The domain the assembly belonged to is not equal to the current domain */
3961 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
3963 g_assert (tls
->domain_unloading
);
3965 buffer_add_assemblyid (&buf
, tls
->domain_unloading
, (MonoAssembly
*)arg
);
3968 case EVENT_KIND_TYPE_LOAD
:
3969 buffer_add_typeid (&buf
, domain
, (MonoClass
*)arg
);
3971 case EVENT_KIND_BREAKPOINT
:
3972 case EVENT_KIND_STEP
: {
3973 DebuggerTlsData
*tls
;
3974 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
3976 mono_stopwatch_stop (&tls
->step_time
);
3977 MonoMethod
*method
= (MonoMethod
*)arg
;
3979 buffer_add_methodid (&buf
, domain
, method
);
3980 buffer_add_long (&buf
, il_offset
);
3983 case EVENT_KIND_VM_START
:
3984 buffer_add_domainid (&buf
, mono_get_root_domain ());
3986 case EVENT_KIND_VM_DEATH
:
3987 if (CHECK_PROTOCOL_VERSION (2, 27))
3988 buffer_add_int (&buf
, mono_environment_exitcode_get ());
3990 case EVENT_KIND_CRASH
: {
3991 EventInfo
*ei
= (EventInfo
*)arg
;
3992 buffer_add_long (&buf
, ei
->hashes
->offset_free_hash
);
3993 buffer_add_string (&buf
, ei
->dump
);
3996 case EVENT_KIND_EXCEPTION
: {
3997 EventInfo
*ei
= (EventInfo
*)arg
;
3998 buffer_add_objid (&buf
, ei
->exc
);
4000 * We are not yet suspending, so get_objref () will not keep this object alive. So we need to do it
4001 * later after the suspension. (#12494).
4003 keepalive_obj
= ei
->exc
;
4006 case EVENT_KIND_USER_BREAK
: {
4007 DebuggerTlsData
*tls
;
4008 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4010 mono_stopwatch_stop (&tls
->step_time
);
4013 case EVENT_KIND_USER_LOG
: {
4014 EventInfo
*ei
= (EventInfo
*)arg
;
4015 buffer_add_int (&buf
, ei
->level
);
4016 buffer_add_string (&buf
, ei
->category
? ei
->category
: "");
4017 buffer_add_string (&buf
, ei
->message
? ei
->message
: "");
4020 case EVENT_KIND_KEEPALIVE
:
4021 suspend_policy
= SUSPEND_POLICY_NONE
;
4024 g_assert_not_reached ();
4028 if (event
== EVENT_KIND_VM_START
) {
4029 if (!agent_config
.defer
) {
4031 start_debugger_thread (error
);
4032 mono_error_assert_ok (error
);
4036 if (event
== EVENT_KIND_VM_DEATH
) {
4037 vm_death_event_sent
= TRUE
;
4038 suspend_policy
= SUSPEND_POLICY_NONE
;
4041 if (mono_runtime_is_shutting_down ())
4042 suspend_policy
= SUSPEND_POLICY_NONE
;
4044 if (suspend_policy
!= SUSPEND_POLICY_NONE
) {
4046 * Save the thread context and start suspending before sending the packet,
4047 * since we could be receiving the resume request before send_packet ()
4050 save_thread_context (ctx
);
4051 DebuggerTlsData
*tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, mono_thread_internal_current ());
4052 tls
->suspend_count
++;
4056 /* This will keep this object alive */
4057 get_objref (keepalive_obj
);
4060 send_success
= send_packet (CMD_SET_EVENT
, CMD_COMPOSITE
, &buf
);
4063 DebuggerTlsData
*tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4064 mono_debugger_log_event (tls
, event_to_string (event
), buf
.buf
, buffer_len (&buf
));
4069 g_slist_free (events
);
4072 if (!send_success
) {
4073 DEBUG_PRINTF (2, "Sending command %s failed.\n", event_to_string (event
));
4077 if (event
== EVENT_KIND_VM_START
) {
4078 vm_start_event_sent
= TRUE
;
4081 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
);
4083 switch (suspend_policy
) {
4084 case SUSPEND_POLICY_NONE
:
4086 case SUSPEND_POLICY_ALL
:
4089 case SUSPEND_POLICY_EVENT_THREAD
:
4093 g_assert_not_reached ();
4098 process_profiler_event (EventKind event
, gpointer arg
)
4102 EventInfo ei
, *ei_arg
= NULL
;
4104 if (event
== EVENT_KIND_TYPE_LOAD
) {
4105 ei
.klass
= (MonoClass
*)arg
;
4109 mono_loader_lock ();
4110 events
= create_event_list (event
, NULL
, NULL
, ei_arg
, &suspend_policy
);
4111 mono_loader_unlock ();
4113 process_event (event
, arg
, 0, NULL
, events
, suspend_policy
);
4117 runtime_initialized (MonoProfiler
*prof
)
4119 process_profiler_event (EVENT_KIND_VM_START
, mono_thread_current ());
4120 if (agent_config
.defer
) {
4122 start_debugger_thread (error
);
4123 mono_error_assert_ok (error
);
4128 runtime_shutdown (MonoProfiler
*prof
)
4130 process_profiler_event (EVENT_KIND_VM_DEATH
, NULL
);
4132 mono_debugger_agent_cleanup ();
4136 thread_startup (MonoProfiler
*prof
, uintptr_t tid
)
4138 MonoInternalThread
*thread
= mono_thread_internal_current ();
4139 MonoInternalThread
*old_thread
;
4140 DebuggerTlsData
*tls
;
4142 if (is_debugger_thread ())
4145 g_assert (mono_native_thread_id_equals (MONO_UINT_TO_NATIVE_THREAD_ID (tid
), MONO_UINT_TO_NATIVE_THREAD_ID (thread
->tid
)));
4147 mono_loader_lock ();
4148 old_thread
= (MonoInternalThread
*)mono_g_hash_table_lookup (tid_to_thread
, GUINT_TO_POINTER (tid
));
4149 mono_loader_unlock ();
4151 if (thread
== old_thread
) {
4153 * For some reason, thread_startup () might be called for the same thread
4154 * multiple times (attach ?).
4156 DEBUG_PRINTF (1, "[%p] thread_start () called multiple times for %p, ignored.\n", GUINT_TO_POINTER (tid
), GUINT_TO_POINTER (tid
));
4160 * thread_end () might not be called for some threads, and the tid could
4163 DEBUG_PRINTF (1, "[%p] Removing stale data for tid %p.\n", GUINT_TO_POINTER (tid
), GUINT_TO_POINTER (tid
));
4164 mono_loader_lock ();
4165 mono_g_hash_table_remove (thread_to_tls
, old_thread
);
4166 mono_g_hash_table_remove (tid_to_thread
, GUINT_TO_POINTER (tid
));
4167 mono_g_hash_table_remove (tid_to_thread_obj
, GUINT_TO_POINTER (tid
));
4168 mono_loader_unlock ();
4172 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4174 // FIXME: Free this somewhere
4175 tls
= g_new0 (DebuggerTlsData
, 1);
4176 MONO_GC_REGISTER_ROOT_SINGLE (tls
->thread
, MONO_ROOT_SOURCE_DEBUGGER
, NULL
, "Debugger Thread Reference");
4177 tls
->thread
= thread
;
4178 // Do so we have thread id even after termination
4179 tls
->thread_id
= (intptr_t) thread
->tid
;
4180 mono_native_tls_set_value (debugger_tls_id
, tls
);
4182 DEBUG_PRINTF (1, "[%p] Thread started, obj=%p, tls=%p.\n", (gpointer
)tid
, thread
, tls
);
4184 mono_loader_lock ();
4185 mono_g_hash_table_insert_internal (thread_to_tls
, thread
, tls
);
4186 mono_g_hash_table_insert_internal (tid_to_thread
, (gpointer
)tid
, thread
);
4187 mono_g_hash_table_insert_internal (tid_to_thread_obj
, GUINT_TO_POINTER (tid
), mono_thread_current ());
4188 mono_loader_unlock ();
4190 process_profiler_event (EVENT_KIND_THREAD_START
, thread
);
4193 * suspend_vm () could have missed this thread, so wait for a resume.
4200 thread_end (MonoProfiler
*prof
, uintptr_t tid
)
4202 MonoInternalThread
*thread
;
4203 DebuggerTlsData
*tls
= NULL
;
4205 mono_loader_lock ();
4206 thread
= (MonoInternalThread
*)mono_g_hash_table_lookup (tid_to_thread
, GUINT_TO_POINTER (tid
));
4208 mono_g_hash_table_remove (tid_to_thread_obj
, GUINT_TO_POINTER (tid
));
4209 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
4211 /* FIXME: Maybe we need to free this instead, but some code can't handle that */
4212 tls
->terminated
= TRUE
;
4213 /* Can't remove from tid_to_thread, as that would defeat the check in thread_start () */
4214 MONO_GC_UNREGISTER_ROOT (tls
->thread
);
4218 mono_loader_unlock ();
4220 /* We might be called for threads started before we registered the start callback */
4222 DEBUG_PRINTF (1, "[%p] Thread terminated, obj=%p, tls=%p (domain=%p).\n", (gpointer
)tid
, thread
, tls
, (gpointer
)mono_domain_get ());
4224 if (mono_thread_internal_is_current (thread
) &&
4225 (!mono_native_tls_get_value (debugger_tls_id
) ||
4226 !mono_domain_get ())
4229 * This can happen on darwin and android since we
4230 * deregister threads using pthread dtors.
4231 * process_profiler_event () and the code it calls
4232 * cannot handle a null TLS value.
4237 process_profiler_event (EVENT_KIND_THREAD_DEATH
, thread
);
4242 appdomain_load (MonoProfiler
*prof
, MonoDomain
*domain
)
4244 mono_de_domain_add (domain
);
4246 process_profiler_event (EVENT_KIND_APPDOMAIN_CREATE
, domain
);
4250 appdomain_start_unload (MonoProfiler
*prof
, MonoDomain
*domain
)
4252 DebuggerTlsData
*tls
;
4254 /* This might be called during shutdown on the debugger thread from the CMD_VM_EXIT code */
4255 if (is_debugger_thread ())
4259 * Remember the currently unloading appdomain as it is needed to generate
4260 * proper ids for unloading assemblies.
4262 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4264 tls
->domain_unloading
= domain
;
4268 appdomain_unload (MonoProfiler
*prof
, MonoDomain
*domain
)
4270 DebuggerTlsData
*tls
;
4272 if (is_debugger_thread ())
4275 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4277 tls
->domain_unloading
= NULL
;
4279 mono_de_clear_breakpoints_for_domain (domain
);
4281 mono_loader_lock ();
4282 /* Invalidate each thread's frame stack */
4283 mono_g_hash_table_foreach (thread_to_tls
, invalidate_each_thread
, NULL
);
4284 mono_loader_unlock ();
4286 process_profiler_event (EVENT_KIND_APPDOMAIN_UNLOAD
, domain
);
4290 * invalidate_each_thread:
4292 * A GHFunc to invalidate frames.
4293 * value must be a DebuggerTlsData*
4296 invalidate_each_thread (gpointer key
, gpointer value
, gpointer user_data
)
4298 invalidate_frames ((DebuggerTlsData
*)value
);
4302 assembly_load (MonoProfiler
*prof
, MonoAssembly
*assembly
)
4304 /* Sent later in jit_end () */
4306 g_ptr_array_add (pending_assembly_loads
, assembly
);
4311 assembly_unload (MonoProfiler
*prof
, MonoAssembly
*assembly
)
4313 if (is_debugger_thread ())
4316 process_profiler_event (EVENT_KIND_ASSEMBLY_UNLOAD
, assembly
);
4318 clear_event_requests_for_assembly (assembly
);
4319 clear_types_for_assembly (assembly
);
4323 send_type_load (MonoClass
*klass
)
4325 gboolean type_load
= FALSE
;
4326 MonoDomain
*domain
= mono_domain_get ();
4327 AgentDomainInfo
*info
= NULL
;
4329 info
= get_agent_domain_info (domain
);
4331 mono_loader_lock ();
4333 if (!g_hash_table_lookup (info
->loaded_classes
, klass
)) {
4335 g_hash_table_insert (info
->loaded_classes
, klass
, klass
);
4338 mono_loader_unlock ();
4341 emit_type_load (klass
, klass
, NULL
);
4345 * Emit load events for all types currently loaded in the domain.
4346 * Takes the loader and domain locks.
4347 * user_data is unused.
4350 send_types_for_domain (MonoDomain
*domain
, void *user_data
)
4352 MonoDomain
* old_domain
;
4353 AgentDomainInfo
*info
= NULL
;
4355 if (mono_domain_is_unloading (domain
))
4358 info
= get_agent_domain_info (domain
);
4361 old_domain
= mono_domain_get ();
4363 mono_domain_set_fast (domain
, TRUE
);
4365 mono_loader_lock ();
4366 g_hash_table_foreach (info
->loaded_classes
, emit_type_load
, NULL
);
4367 mono_loader_unlock ();
4369 mono_domain_set_fast (old_domain
, TRUE
);
4373 send_assemblies_for_domain (MonoDomain
*domain
, void *user_data
)
4376 MonoDomain
* old_domain
;
4378 if (mono_domain_is_unloading (domain
))
4381 old_domain
= mono_domain_get ();
4383 mono_domain_set_fast (domain
, TRUE
);
4385 mono_domain_assemblies_lock (domain
);
4386 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
4387 MonoAssembly
* ass
= (MonoAssembly
*)tmp
->data
;
4388 emit_assembly_load (ass
, NULL
);
4390 mono_domain_assemblies_unlock (domain
);
4392 mono_domain_set_fast (old_domain
, TRUE
);
4396 jit_done (MonoProfiler
*prof
, MonoMethod
*method
, MonoJitInfo
*jinfo
)
4398 jit_end (prof
, method
, jinfo
);
4402 jit_failed (MonoProfiler
*prof
, MonoMethod
*method
)
4404 jit_end (prof
, method
, NULL
);
4408 jit_end (MonoProfiler
*prof
, MonoMethod
*method
, MonoJitInfo
*jinfo
)
4411 * We emit type load events when the first method of the type is JITted,
4412 * since the class load profiler callbacks might be called with the
4413 * loader lock held. They could also occur in the debugger thread.
4414 * Same for assembly load events.
4417 MonoAssembly
*assembly
= NULL
;
4419 // FIXME: Maybe store this in TLS so the thread of the event is correct ?
4421 if (pending_assembly_loads
->len
> 0) {
4422 assembly
= (MonoAssembly
*)g_ptr_array_index (pending_assembly_loads
, 0);
4423 g_ptr_array_remove_index (pending_assembly_loads
, 0);
4428 process_profiler_event (EVENT_KIND_ASSEMBLY_LOAD
, assembly
);
4434 send_type_load (method
->klass
);
4437 mono_de_add_pending_breakpoints (method
, jinfo
);
4445 event_requests_cleanup (void)
4447 mono_loader_lock ();
4449 while (i
< event_requests
->len
) {
4450 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
4452 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
) {
4453 mono_de_clear_breakpoint ((MonoBreakpoint
*)req
->info
);
4454 g_ptr_array_remove_index_fast (event_requests
, i
);
4460 mono_loader_unlock ();
4464 * ss_calculate_framecount:
4466 * Ensure DebuggerTlsData fields are filled out.
4469 ss_calculate_framecount (void *the_tls
, MonoContext
*ctx
, gboolean force_use_ctx
, DbgEngineStackFrame
***frames
, int *nframes
)
4471 DebuggerTlsData
*tls
= (DebuggerTlsData
*)the_tls
;
4473 if (force_use_ctx
|| !tls
->context
.valid
)
4474 mono_thread_state_init_from_monoctx (&tls
->context
, ctx
);
4475 compute_frame_info (tls
->thread
, tls
, FALSE
);
4477 *frames
= (DbgEngineStackFrame
**)tls
->frames
;
4479 *nframes
= tls
->frame_count
;
4483 * ss_discard_frame_data:
4485 * Discard frame data and invalidate any context
4488 ss_discard_frame_context (void *the_tls
)
4490 DebuggerTlsData
*tls
= (DebuggerTlsData
*)the_tls
;
4491 tls
->context
.valid
= FALSE
;
4492 tls
->async_state
.valid
= FALSE
;
4493 invalidate_frames (tls
);
4497 tls_get_restore_state (void *the_tls
)
4499 DebuggerTlsData
*tls
= (DebuggerTlsData
*)the_tls
;
4501 return &tls
->restore_state
.ctx
;
4505 ensure_jit (DbgEngineStackFrame
* the_frame
)
4507 StackFrame
*frame
= (StackFrame
*)the_frame
;
4509 frame
->jit
= mono_debug_find_method (frame
->api_method
, frame
->de
.domain
);
4510 if (!frame
->jit
&& frame
->api_method
->is_inflated
)
4511 frame
->jit
= mono_debug_find_method(mono_method_get_declaring_generic_method (frame
->api_method
), frame
->de
.domain
);
4515 /* This could happen for aot images with no jit debug info */
4516 s
= mono_method_full_name (frame
->api_method
, TRUE
);
4517 DEBUG_PRINTF(1, "[dbg] No debug information found for '%s'.\n", s
);
4526 breakpoint_matches_assembly (MonoBreakpoint
*bp
, MonoAssembly
*assembly
)
4528 return bp
->method
&& m_class_get_image (bp
->method
->klass
)->assembly
== assembly
;
4532 get_this_addr (DbgEngineStackFrame
*the_frame
)
4534 StackFrame
*frame
= (StackFrame
*)the_frame
;
4535 if (frame
->de
.ji
->is_interp
)
4536 return mini_get_interp_callbacks ()->frame_get_this (frame
->interp_frame
);
4538 MonoDebugVarInfo
*var
= frame
->jit
->this_var
;
4539 if ((var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
) != MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
)
4542 guint8
*addr
= (guint8
*)mono_arch_context_get_int_reg (&frame
->ctx
, var
->index
& ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
);
4543 addr
+= (gint32
)var
->offset
;
4548 get_set_notification_method (MonoClass
* async_builder_class
)
4551 GPtrArray
* array
= mono_class_get_methods_by_name (async_builder_class
, "SetNotificationForWaitCompletion", 0x24, 1, FALSE
, error
);
4552 mono_error_assert_ok (error
);
4553 if (array
->len
== 0) {
4554 g_ptr_array_free (array
, TRUE
);
4557 MonoMethod
* set_notification_method
= (MonoMethod
*)g_ptr_array_index (array
, 0);
4558 g_ptr_array_free (array
, TRUE
);
4559 return set_notification_method
;
4563 get_object_id_for_debugger_method (MonoClass
* async_builder_class
)
4566 GPtrArray
*array
= mono_class_get_methods_by_name (async_builder_class
, "get_ObjectIdForDebugger", 0x24, 1, FALSE
, error
);
4567 mono_error_assert_ok (error
);
4568 g_assert (array
->len
== 1);
4569 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (array
, 0);
4570 g_ptr_array_free (array
, TRUE
);
4575 get_class_to_get_builder_field(DbgEngineStackFrame
*frame
)
4578 gpointer this_addr
= get_this_addr (frame
);
4579 MonoClass
*original_class
= frame
->method
->klass
;
4581 if (!m_class_is_valuetype (original_class
) && mono_class_is_open_constructed_type (m_class_get_byval_arg (original_class
))) {
4582 MonoObject
*this_obj
= *(MonoObject
**)this_addr
;
4583 MonoGenericContext context
;
4584 MonoType
*inflated_type
;
4586 g_assert (this_obj
);
4587 context
= mono_get_generic_context_from_stack_frame (frame
->ji
, this_obj
->vtable
);
4588 inflated_type
= mono_class_inflate_generic_type_checked (m_class_get_byval_arg (original_class
), &context
, error
);
4589 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
4591 ret
= mono_class_from_mono_type_internal (inflated_type
);
4592 mono_metadata_free_type (inflated_type
);
4595 return original_class
;
4599 /* Return the address of the AsyncMethodBuilder struct belonging to the state machine method pointed to by FRAME */
4601 get_async_method_builder (DbgEngineStackFrame
*frame
)
4603 MonoObject
*this_obj
;
4604 MonoClassField
*builder_field
;
4607 MonoClass
* klass
= frame
->method
->klass
;
4609 klass
= get_class_to_get_builder_field(frame
);
4610 builder_field
= mono_class_get_field_from_name_full (klass
, "<>t__builder", NULL
);
4611 g_assert (builder_field
);
4613 this_addr
= get_this_addr (frame
);
4617 if (m_class_is_valuetype (klass
)) {
4618 builder
= mono_vtype_get_field_addr (*(guint8
**)this_addr
, builder_field
);
4620 this_obj
= *(MonoObject
**)this_addr
;
4621 builder
= (char*)this_obj
+ builder_field
->offset
;
4627 //This ID is used to figure out if breakpoint hit on resumeOffset belongs to us or not
4628 //since thread probably changed...
4630 get_this_async_id (DbgEngineStackFrame
*frame
)
4632 MonoClassField
*builder_field
;
4638 gboolean old_disable_breakpoints
= FALSE
;
4639 DebuggerTlsData
*tls
;
4642 * FRAME points to a method in a state machine class/struct.
4643 * Call the ObjectIdForDebugger method of the associated method builder type.
4645 builder
= get_async_method_builder (frame
);
4649 builder_field
= mono_class_get_field_from_name_full (get_class_to_get_builder_field(frame
), "<>t__builder", NULL
);
4650 g_assert (builder_field
);
4652 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4654 old_disable_breakpoints
= tls
->disable_breakpoints
;
4655 tls
->disable_breakpoints
= TRUE
;
4658 method
= get_object_id_for_debugger_method (mono_class_from_mono_type_internal (builder_field
->type
));
4659 obj
= mono_runtime_try_invoke (method
, builder
, NULL
, &ex
, error
);
4660 mono_error_assert_ok (error
);
4663 tls
->disable_breakpoints
= old_disable_breakpoints
;
4665 return get_objid (obj
);
4668 // Returns true if TaskBuilder has NotifyDebuggerOfWaitCompletion method
4669 // false if not(AsyncVoidBuilder)
4671 set_set_notification_for_wait_completion_flag (DbgEngineStackFrame
*frame
)
4673 MonoClassField
*builder_field
= mono_class_get_field_from_name_full (get_class_to_get_builder_field(frame
), "<>t__builder", NULL
);
4674 g_assert (builder_field
);
4675 gpointer builder
= get_async_method_builder (frame
);
4678 MonoMethod
* method
= get_set_notification_method (mono_class_from_mono_type_internal (builder_field
->type
));
4681 gboolean arg
= TRUE
;
4683 void *args
[ ] = { &arg
};
4684 mono_runtime_invoke_checked (method
, builder
, args
, error
);
4685 mono_error_assert_ok (error
);
4689 static MonoMethod
* notify_debugger_of_wait_completion_method_cache
;
4692 get_notify_debugger_of_wait_completion_method (void)
4694 if (notify_debugger_of_wait_completion_method_cache
!= NULL
)
4695 return notify_debugger_of_wait_completion_method_cache
;
4697 MonoClass
* task_class
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Threading.Tasks", "Task");
4698 GPtrArray
* array
= mono_class_get_methods_by_name (task_class
, "NotifyDebuggerOfWaitCompletion", 0x24, 1, FALSE
, error
);
4699 mono_error_assert_ok (error
);
4700 g_assert (array
->len
== 1);
4701 notify_debugger_of_wait_completion_method_cache
= (MonoMethod
*)g_ptr_array_index (array
, 0);
4702 g_ptr_array_free (array
, TRUE
);
4703 return notify_debugger_of_wait_completion_method_cache
;
4707 begin_breakpoint_processing (void *the_tls
, MonoContext
*ctx
, MonoJitInfo
*ji
, gboolean from_signal
)
4709 DebuggerTlsData
*tls
= (DebuggerTlsData
*)the_tls
;
4712 * Skip the instruction causing the breakpoint signal.
4715 mono_arch_skip_breakpoint (ctx
, ji
);
4717 if (tls
->disable_breakpoints
)
4723 GSList
*bp_events
, *ss_events
, *enter_leave_events
;
4729 create_breakpoint_events (GPtrArray
*ss_reqs
, GPtrArray
*bp_reqs
, MonoJitInfo
*ji
, EventKind kind
)
4731 int suspend_policy
= 0;
4732 BreakPointEvents
*evts
= g_new0 (BreakPointEvents
, 1);
4733 if (ss_reqs
&& ss_reqs
->len
> 0)
4734 evts
->ss_events
= create_event_list (EVENT_KIND_STEP
, ss_reqs
, ji
, NULL
, &suspend_policy
);
4735 else if (bp_reqs
&& bp_reqs
->len
> 0)
4736 evts
->bp_events
= create_event_list (EVENT_KIND_BREAKPOINT
, bp_reqs
, ji
, NULL
, &suspend_policy
);
4737 else if (kind
!= EVENT_KIND_BREAKPOINT
)
4738 evts
->enter_leave_events
= create_event_list (kind
, NULL
, ji
, NULL
, &suspend_policy
);
4741 evts
->suspend_policy
= suspend_policy
;
4746 process_breakpoint_events (void *_evts
, MonoMethod
*method
, MonoContext
*ctx
, int il_offset
)
4748 BreakPointEvents
*evts
= (BreakPointEvents
*)_evts
;
4750 * FIXME: The first event will suspend, so the second will only be sent after the
4753 if (evts
->ss_events
)
4754 process_event (EVENT_KIND_STEP
, method
, il_offset
, ctx
, evts
->ss_events
, evts
->suspend_policy
);
4755 if (evts
->bp_events
)
4756 process_event (evts
->kind
, method
, il_offset
, ctx
, evts
->bp_events
, evts
->suspend_policy
);
4757 if (evts
->enter_leave_events
)
4758 process_event (evts
->kind
, method
, il_offset
, ctx
, evts
->enter_leave_events
, evts
->suspend_policy
);
4763 /* Process a breakpoint/single step event after resuming from a signal handler */
4765 process_signal_event (void (*func
) (void*, gboolean
))
4767 DebuggerTlsData
*tls
;
4768 MonoThreadUnwindState orig_restore_state
;
4771 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4772 /* Have to save/restore the restore_ctx as we can be called recursively during invokes etc. */
4773 memcpy (&orig_restore_state
, &tls
->restore_state
, sizeof (MonoThreadUnwindState
));
4774 mono_thread_state_init_from_monoctx (&tls
->restore_state
, &tls
->handler_ctx
);
4778 /* This is called when resuming from a signal handler, so it shouldn't return */
4779 memcpy (&ctx
, &tls
->restore_state
.ctx
, sizeof (MonoContext
));
4780 memcpy (&tls
->restore_state
, &orig_restore_state
, sizeof (MonoThreadUnwindState
));
4781 mono_restore_context (&ctx
);
4782 g_assert_not_reached ();
4786 process_breakpoint_from_signal (void)
4788 process_signal_event (mono_de_process_breakpoint
);
4792 resume_from_signal_handler (void *sigctx
, void *func
)
4794 DebuggerTlsData
*tls
;
4797 /* Save the original context in TLS */
4798 // FIXME: This might not work on an altstack ?
4799 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4801 g_printerr ("Thread %p is not attached to the JIT.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
4804 // FIXME: MonoContext usually doesn't include the fp registers, so these are
4805 // clobbered by a single step/breakpoint event. If this turns out to be a problem,
4806 // clob:c could be added to op_seq_point.
4808 mono_sigctx_to_monoctx (sigctx
, &ctx
);
4809 memcpy (&tls
->handler_ctx
, &ctx
, sizeof (MonoContext
));
4810 #ifdef MONO_ARCH_HAVE_SETUP_RESUME_FROM_SIGNAL_HANDLER_CTX
4811 mono_arch_setup_resume_sighandler_ctx (&ctx
, func
);
4813 MONO_CONTEXT_SET_IP (&ctx
, func
);
4815 mono_monoctx_to_sigctx (&ctx
, sigctx
);
4819 debugger_agent_breakpoint_hit (void *sigctx
)
4822 * We are called from a signal handler, and running code there causes all kinds of
4823 * problems, like the original signal is disabled, libgc can't handle altstack, etc.
4824 * So set up the signal context to return to the real breakpoint handler function.
4826 resume_from_signal_handler (sigctx
, (gpointer
)process_breakpoint_from_signal
);
4835 user_break_cb (StackFrameInfo
*frame
, MonoContext
*ctx
, gpointer user_data
)
4837 UserBreakCbData
*data
= (UserBreakCbData
*)user_data
;
4839 if (frame
->type
== FRAME_TYPE_INTERP_TO_MANAGED
|| frame
->type
== FRAME_TYPE_INTERP_TO_MANAGED_WITH_CTX
) {
4843 if (frame
->managed
) {
4853 * Called by System.Diagnostics.Debugger:Break ().
4856 debugger_agent_user_break (void)
4858 if (agent_config
.enabled
) {
4862 UserBreakCbData data
;
4864 memset (&data
, 0, sizeof (data
));
4867 /* Obtain a context */
4868 MONO_CONTEXT_SET_IP (&ctx
, NULL
);
4869 mono_walk_stack_with_ctx (user_break_cb
, NULL
, (MonoUnwindOptions
)0, &data
);
4870 g_assert (data
.found
);
4872 mono_loader_lock ();
4873 events
= create_event_list (EVENT_KIND_USER_BREAK
, NULL
, NULL
, NULL
, &suspend_policy
);
4874 mono_loader_unlock ();
4876 process_event (EVENT_KIND_USER_BREAK
, NULL
, 0, &ctx
, events
, suspend_policy
);
4877 } else if (mini_debug_options
.native_debugger_break
) {
4883 begin_single_step_processing (MonoContext
*ctx
, gboolean from_signal
)
4886 mono_arch_skip_single_step (ctx
);
4890 process_single_step (void)
4892 process_signal_event (mono_de_process_single_step
);
4896 * debugger_agent_single_step_event:
4898 * Called from a signal handler to handle a single step event.
4901 debugger_agent_single_step_event (void *sigctx
)
4903 /* Resume to process_single_step through the signal context */
4905 // FIXME: Since step out/over is implemented using step in, the step in case should
4906 // be as fast as possible. Move the relevant code from mono_de_process_single_step ()
4909 if (is_debugger_thread ()) {
4911 * This could happen despite our best effors when the runtime calls
4912 * assembly/type resolve hooks.
4913 * FIXME: Breakpoints too.
4917 mono_sigctx_to_monoctx (sigctx
, &ctx
);
4918 mono_arch_skip_single_step (&ctx
);
4919 mono_monoctx_to_sigctx (&ctx
, sigctx
);
4923 resume_from_signal_handler (sigctx
, (gpointer
)process_single_step
);
4927 debugger_agent_single_step_from_context (MonoContext
*ctx
)
4929 DebuggerTlsData
*tls
;
4930 MonoThreadUnwindState orig_restore_state
;
4932 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4933 /* Fastpath during invokes, see in process_suspend () */
4934 if (tls
&& suspend_count
&& suspend_count
- tls
->resume_count
== 0)
4937 if (is_debugger_thread ())
4942 tls
->terminated
= FALSE
;
4944 /* Have to save/restore the restore_ctx as we can be called recursively during invokes etc. */
4945 memcpy (&orig_restore_state
, &tls
->restore_state
, sizeof (MonoThreadUnwindState
));
4946 mono_thread_state_init_from_monoctx (&tls
->restore_state
, ctx
);
4947 memcpy (&tls
->handler_ctx
, ctx
, sizeof (MonoContext
));
4949 mono_de_process_single_step (tls
, FALSE
);
4951 memcpy (ctx
, &tls
->restore_state
.ctx
, sizeof (MonoContext
));
4952 memcpy (&tls
->restore_state
, &orig_restore_state
, sizeof (MonoThreadUnwindState
));
4956 debugger_agent_breakpoint_from_context (MonoContext
*ctx
)
4958 DebuggerTlsData
*tls
;
4959 MonoThreadUnwindState orig_restore_state
;
4962 if (is_debugger_thread ())
4965 orig_ip
= (guint8
*)MONO_CONTEXT_GET_IP (ctx
);
4966 MONO_CONTEXT_SET_IP (ctx
, orig_ip
- 1);
4968 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
4971 //if a thread was suspended and doesn't have any managed stack, it was considered as terminated,
4972 //but it wasn't really terminated because it can execute managed code again, and stop in a breakpoint so here we set terminated as FALSE
4973 tls
->terminated
= FALSE
;
4975 memcpy (&orig_restore_state
, &tls
->restore_state
, sizeof (MonoThreadUnwindState
));
4976 mono_thread_state_init_from_monoctx (&tls
->restore_state
, ctx
);
4977 memcpy (&tls
->handler_ctx
, ctx
, sizeof (MonoContext
));
4979 mono_de_process_breakpoint (tls
, FALSE
);
4981 memcpy (ctx
, &tls
->restore_state
.ctx
, sizeof (MonoContext
));
4982 memcpy (&tls
->restore_state
, &orig_restore_state
, sizeof (MonoThreadUnwindState
));
4983 if (MONO_CONTEXT_GET_IP (ctx
) == orig_ip
- 1)
4984 MONO_CONTEXT_SET_IP (ctx
, orig_ip
);
4988 ss_args_destroy (SingleStepArgs
*ss_args
)
4990 if (ss_args
->frames
)
4991 free_frames ((StackFrame
**)ss_args
->frames
, ss_args
->nframes
);
4995 handle_multiple_ss_requests (void)
5001 ensure_runtime_is_suspended (void)
5003 if (suspend_count
== 0)
5004 return ERR_NOT_SUSPENDED
;
5006 wait_for_suspend ();
5012 ss_create_init_args (SingleStepReq
*ss_req
, SingleStepArgs
*args
)
5014 MonoSeqPointInfo
*info
= NULL
;
5016 MonoMethod
*method
= NULL
;
5017 MonoDebugMethodInfo
*minfo
;
5018 gboolean step_to_catch
= FALSE
;
5019 gboolean set_ip
= FALSE
;
5020 StackFrame
**frames
= NULL
;
5023 mono_loader_lock ();
5024 DebuggerTlsData
*tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, ss_req
->thread
);
5025 mono_loader_unlock ();
5027 if (!tls
->context
.valid
) {
5028 DEBUG_PRINTF (1, "Received a single step request on a thread with no managed frames.");
5029 return ERR_INVALID_ARGUMENT
;
5032 if (tls
->restore_state
.valid
&& MONO_CONTEXT_GET_IP (&tls
->context
.ctx
) != MONO_CONTEXT_GET_IP (&tls
->restore_state
.ctx
)) {
5034 * Need to start single stepping from restore_state and not from the current state
5037 frames
= compute_frame_info_from (ss_req
->thread
, tls
, &tls
->restore_state
, &nframes
);
5040 ss_req
->start_sp
= ss_req
->last_sp
= MONO_CONTEXT_GET_SP (&tls
->context
.ctx
);
5042 if (tls
->has_catch_frame
) {
5043 StackFrameInfo frame
;
5046 * We are stopped at a throw site. Stepping should go to the catch site.
5048 frame
= tls
->catch_frame
;
5049 g_assert (frame
.type
== FRAME_TYPE_MANAGED
|| frame
.type
== FRAME_TYPE_INTERP
);
5052 * Find the seq point corresponding to the landing site ip, which is the first seq
5055 found_sp
= mono_find_next_seq_point_for_native_offset (frame
.domain
, frame
.method
, frame
.native_offset
, &info
, &args
->sp
);
5057 no_seq_points_found (frame
.method
, frame
.native_offset
);
5058 g_assert (found_sp
);
5060 method
= frame
.method
;
5062 step_to_catch
= TRUE
;
5063 /* This make sure the seq point is not skipped by process_single_step () */
5064 ss_req
->last_sp
= NULL
;
5067 if (!step_to_catch
) {
5068 StackFrame
*frame
= NULL
;
5071 if (frames
&& nframes
)
5074 compute_frame_info (ss_req
->thread
, tls
, FALSE
);
5076 if (tls
->frame_count
)
5077 frame
= tls
->frames
[0];
5080 if (ss_req
->size
== STEP_SIZE_LINE
) {
5082 ss_req
->last_method
= frame
->de
.method
;
5083 ss_req
->last_line
= -1;
5085 minfo
= mono_debug_lookup_method (frame
->de
.method
);
5086 if (minfo
&& frame
->il_offset
!= -1) {
5087 MonoDebugSourceLocation
*loc
= mono_debug_method_lookup_location (minfo
, frame
->il_offset
);
5090 ss_req
->last_line
= loc
->row
;
5098 if (!method
&& frame
->il_offset
!= -1) {
5099 /* FIXME: Sort the table and use a binary search */
5100 found_sp
= mono_find_prev_seq_point_for_native_offset (frame
->de
.domain
, frame
->de
.method
, frame
->de
.native_offset
, &info
, &args
->sp
);
5102 no_seq_points_found (frame
->de
.method
, frame
->de
.native_offset
);
5103 g_assert (found_sp
);
5104 method
= frame
->de
.method
;
5109 ss_req
->start_method
= method
;
5111 args
->method
= method
;
5112 args
->ctx
= set_ip
? &tls
->restore_state
.ctx
: &tls
->context
.ctx
;
5114 args
->step_to_catch
= step_to_catch
;
5116 args
->frames
= (DbgEngineStackFrame
**)frames
;
5117 args
->nframes
= nframes
;
5123 ss_clear_for_assembly (SingleStepReq
*req
, MonoAssembly
*assembly
)
5126 gboolean found
= TRUE
;
5130 for (l
= req
->bps
; l
; l
= l
->next
) {
5131 if (breakpoint_matches_assembly ((MonoBreakpoint
*)l
->data
, assembly
)) {
5132 mono_de_clear_breakpoint ((MonoBreakpoint
*)l
->data
);
5133 req
->bps
= g_slist_delete_link (req
->bps
, l
);
5142 * This takes a lot of locks and stuff. Do this at the end, after
5143 * other things have dumped us, so that getting stuck here won't
5144 * prevent seeing other crash information
5147 mono_debugger_agent_send_crash (char *json_dump
, MonoStackHash
*hashes
, int pause
)
5149 #ifndef DISABLE_CRASH_REPORTING
5154 if (!agent_config
.enabled
)
5157 // Don't send the event if the client doesn't expect it
5158 if (!CHECK_PROTOCOL_VERSION (2, 49))
5161 // It doesn't make sense to wait for lldb/gdb to finish if we're not
5162 // actually enabled. Therefore we do the wait here.
5165 // Don't heap allocate when we can avoid it
5166 EventRequest request
;
5167 memset (&request
, 0, sizeof (request
));
5168 request
.event_kind
= EVENT_KIND_CRASH
;
5171 pdata
[0] = &request
;
5173 memset (&array
, 0, sizeof (array
));
5174 array
.pdata
= pdata
;
5177 mono_loader_lock ();
5178 events
= create_event_list (EVENT_KIND_CRASH
, &array
, NULL
, NULL
, &suspend_policy
);
5179 mono_loader_unlock ();
5181 ei
.dump
= json_dump
;
5184 g_assert (events
!= NULL
);
5186 process_event (EVENT_KIND_CRASH
, &ei
, 0, NULL
, events
, suspend_policy
);
5188 // Don't die before it is sent.
5194 * Called from metadata by the icall for System.Diagnostics.Debugger:Log ().
5197 debugger_agent_debug_log (int level
, MonoString
*category
, MonoString
*message
)
5204 if (!agent_config
.enabled
)
5207 memset (&ei
, 0, sizeof (ei
));
5209 mono_loader_lock ();
5210 events
= create_event_list (EVENT_KIND_USER_LOG
, NULL
, NULL
, NULL
, &suspend_policy
);
5211 mono_loader_unlock ();
5215 ei
.category
= mono_string_to_utf8_checked_internal (category
, error
);
5216 mono_error_cleanup (error
);
5220 ei
.message
= mono_string_to_utf8_checked_internal (message
, error
);
5221 mono_error_cleanup (error
);
5224 process_event (EVENT_KIND_USER_LOG
, &ei
, 0, NULL
, events
, suspend_policy
);
5226 g_free (ei
.category
);
5227 g_free (ei
.message
);
5231 debugger_agent_debug_log_is_enabled (void)
5233 /* Treat this as true even if there is no event request for EVENT_KIND_USER_LOG */
5234 return agent_config
.enabled
;
5238 debugger_agent_unhandled_exception (MonoException
*exc
)
5247 memset (&ei
, 0, sizeof (ei
));
5248 ei
.exc
= (MonoObject
*)exc
;
5250 mono_loader_lock ();
5251 events
= create_event_list (EVENT_KIND_EXCEPTION
, NULL
, NULL
, &ei
, &suspend_policy
);
5252 mono_loader_unlock ();
5254 process_event (EVENT_KIND_EXCEPTION
, &ei
, 0, NULL
, events
, suspend_policy
);
5258 debugger_agent_handle_exception (MonoException
*exc
, MonoContext
*throw_ctx
,
5259 MonoContext
*catch_ctx
, StackFrameInfo
*catch_frame
)
5261 if (catch_ctx
== NULL
&& catch_frame
== NULL
&& mini_debug_options
.suspend_on_unhandled
&& mono_object_class (exc
) != mono_defaults
.threadabortexception_class
) {
5262 mono_runtime_printf_err ("Unhandled exception, suspending...");
5267 int i
, j
, suspend_policy
;
5269 MonoJitInfo
*ji
, *catch_ji
;
5271 DebuggerTlsData
*tls
= NULL
;
5273 if (thread_to_tls
!= NULL
) {
5274 MonoInternalThread
*thread
= mono_thread_internal_current ();
5276 mono_loader_lock ();
5277 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
5278 mono_loader_unlock ();
5280 if (tls
&& tls
->abort_requested
)
5282 if (tls
&& tls
->disable_breakpoints
)
5286 memset (&ei
, 0, sizeof (ei
));
5288 /* Just-In-Time debugging */
5290 if (agent_config
.onuncaught
&& !inited
) {
5291 finish_agent_init (FALSE
);
5294 * Send an unsolicited EXCEPTION event with a dummy request id.
5296 events
= g_slist_append (NULL
, GUINT_TO_POINTER (0xffffff));
5297 ei
.exc
= (MonoObject
*)exc
;
5298 process_event (EVENT_KIND_EXCEPTION
, &ei
, 0, throw_ctx
, events
, SUSPEND_POLICY_ALL
);
5301 } else if (agent_config
.onthrow
&& !inited
) {
5303 gboolean found
= FALSE
;
5305 for (l
= agent_config
.onthrow
; l
; l
= l
->next
) {
5306 char *ex_type
= (char *)l
->data
;
5307 char *f
= mono_type_full_name (m_class_get_byval_arg (exc
->object
.vtable
->klass
));
5309 if (!strcmp (ex_type
, "") || !strcmp (ex_type
, f
))
5316 finish_agent_init (FALSE
);
5319 * Send an unsolicited EXCEPTION event with a dummy request id.
5321 events
= g_slist_append (NULL
, GUINT_TO_POINTER (0xffffff));
5322 ei
.exc
= (MonoObject
*)exc
;
5323 process_event (EVENT_KIND_EXCEPTION
, &ei
, 0, throw_ctx
, events
, SUSPEND_POLICY_ALL
);
5331 ji
= mini_jit_info_table_find (mono_domain_get (), (char *)MONO_CONTEXT_GET_IP (throw_ctx
), NULL
);
5333 catch_ji
= catch_frame
->ji
;
5337 ei
.exc
= (MonoObject
*)exc
;
5338 ei
.caught
= catch_ctx
!= NULL
;
5340 mono_loader_lock ();
5342 /* Treat exceptions which are caught in non-user code as unhandled */
5343 for (i
= 0; i
< event_requests
->len
; ++i
) {
5344 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
5345 if (req
->event_kind
!= EVENT_KIND_EXCEPTION
)
5348 for (j
= 0; j
< req
->nmodifiers
; ++j
) {
5349 Modifier
*mod
= &req
->modifiers
[j
];
5351 if (mod
->kind
== MOD_KIND_ASSEMBLY_ONLY
&& catch_ji
) {
5353 gboolean found
= FALSE
;
5354 MonoAssembly
**assemblies
= mod
->data
.assemblies
;
5357 for (k
= 0; assemblies
[k
]; ++k
)
5358 if (assemblies
[k
] == m_class_get_image (jinfo_get_method (catch_ji
)->klass
)->assembly
)
5367 events
= create_event_list (EVENT_KIND_EXCEPTION
, NULL
, ji
, &ei
, &suspend_policy
);
5368 mono_loader_unlock ();
5370 if (tls
&& ei
.caught
&& catch_ctx
) {
5372 tls
->has_catch_frame
= TRUE
;
5373 tls
->catch_frame
= *catch_frame
;
5375 memset (&tls
->catch_frame
, 0, sizeof (tls
->catch_frame
));
5379 process_event (EVENT_KIND_EXCEPTION
, &ei
, 0, throw_ctx
, events
, suspend_policy
);
5382 tls
->has_catch_frame
= FALSE
;
5386 debugger_agent_begin_exception_filter (MonoException
*exc
, MonoContext
*ctx
, MonoContext
*orig_ctx
)
5388 DebuggerTlsData
*tls
;
5393 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
5398 * We're about to invoke an exception filter during the first pass of exception handling.
5400 * 'ctx' is the context that'll get passed to the filter ('call_filter (ctx, ei->data.filter)'),
5401 * 'orig_ctx' is the context where the exception has been thrown.
5404 * See mcs/class/Mono.Debugger.Soft/Tests/dtest-excfilter.il for an example.
5406 * If we're stopped in Filter(), normal stack unwinding would first unwind to
5407 * the call site (line 37) and then continue to Main(), but it would never
5408 * include the throw site (line 32).
5410 * Since exception filters are invoked during the first pass of exception handling,
5411 * the stack frames of the throw site are still intact, so we should include them
5414 * We do this here by saving the context of the throw site in 'tls->filter_state'.
5416 * Exception filters are used by MonoDroid, where we want to stop inside a call filter,
5417 * but report the location of the 'throw' to the user.
5421 g_assert (mono_thread_state_init_from_monoctx (&tls
->filter_state
, orig_ctx
));
5425 debugger_agent_end_exception_filter (MonoException
*exc
, MonoContext
*ctx
, MonoContext
*orig_ctx
)
5427 DebuggerTlsData
*tls
;
5432 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
5436 tls
->filter_state
.valid
= FALSE
;
5440 buffer_add_fixed_array (Buffer
*buf
, MonoType
*t
, void *addr
, MonoDomain
*domain
,
5441 gboolean as_vtype
, GHashTable
*parent_vtypes
, gint32 len_fixed_array
)
5443 buffer_add_byte (buf
, VALUE_TYPE_ID_FIXED_ARRAY
);
5444 buffer_add_byte (buf
, t
->type
);
5445 buffer_add_int (buf
, len_fixed_array
);
5446 for (int i
= 0; i
< len_fixed_array
; i
++) {
5448 case MONO_TYPE_BOOLEAN
:
5451 buffer_add_int (buf
, ((gint8
*)addr
)[i
]);
5453 case MONO_TYPE_CHAR
:
5456 buffer_add_int (buf
, ((gint16
*)addr
)[i
]);
5461 buffer_add_int (buf
, ((gint32
*)addr
)[i
]);
5466 buffer_add_long (buf
, ((gint64
*)addr
)[i
]);
5468 case MONO_TYPE_PTR
: {
5469 gssize val
= *(gssize
*)addr
;
5471 buffer_add_byte (buf
, t
->type
);
5472 buffer_add_long (buf
, val
);
5473 if (CHECK_PROTOCOL_VERSION(2, 46))
5474 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (t
));
5481 * buffer_add_value_full:
5483 * Add the encoding of the value at ADDR described by T to the buffer.
5484 * AS_VTYPE determines whenever to treat primitive types as primitive types or
5488 buffer_add_value_full (Buffer
*buf
, MonoType
*t
, void *addr
, MonoDomain
*domain
,
5489 gboolean as_vtype
, GHashTable
*parent_vtypes
, gint32 len_fixed_array
)
5492 gboolean boxed_vtype
= FALSE
;
5495 if (!(*(void**)addr
)) {
5496 /* This can happen with compiler generated locals */
5497 //printf ("%s\n", mono_type_full_name (t));
5498 buffer_add_byte (buf
, VALUE_TYPE_ID_NULL
);
5501 g_assert (*(void**)addr
);
5502 addr
= *(void**)addr
;
5507 case MONO_TYPE_BOOLEAN
:
5510 case MONO_TYPE_CHAR
:
5529 if (len_fixed_array
> 1 && t
->type
!= MONO_TYPE_VALUETYPE
&& CHECK_PROTOCOL_VERSION (2, 53))
5531 buffer_add_fixed_array(buf
, t
, addr
, domain
, as_vtype
, parent_vtypes
, len_fixed_array
);
5535 case MONO_TYPE_VOID
:
5536 buffer_add_byte (buf
, t
->type
);
5538 case MONO_TYPE_BOOLEAN
:
5541 buffer_add_byte (buf
, t
->type
);
5542 buffer_add_int (buf
, *(gint8
*)addr
);
5544 case MONO_TYPE_CHAR
:
5547 buffer_add_byte (buf
, t
->type
);
5548 buffer_add_int (buf
, *(gint16
*)addr
);
5553 buffer_add_byte (buf
, t
->type
);
5554 buffer_add_int (buf
, *(gint32
*)addr
);
5559 buffer_add_byte (buf
, t
->type
);
5560 buffer_add_long (buf
, *(gint64
*)addr
);
5564 /* Treat it as a vtype */
5566 case MONO_TYPE_PTR
: {
5567 gssize val
= *(gssize
*)addr
;
5569 buffer_add_byte (buf
, t
->type
);
5570 buffer_add_long (buf
, val
);
5571 if (CHECK_PROTOCOL_VERSION(2, 46))
5572 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (t
));
5576 case MONO_TYPE_STRING
:
5577 case MONO_TYPE_SZARRAY
:
5578 case MONO_TYPE_OBJECT
:
5579 case MONO_TYPE_CLASS
:
5580 case MONO_TYPE_ARRAY
:
5581 obj
= *(MonoObject
**)addr
;
5584 buffer_add_byte (buf
, VALUE_TYPE_ID_NULL
);
5586 if (m_class_is_valuetype (obj
->vtable
->klass
)) {
5587 t
= m_class_get_byval_arg (obj
->vtable
->klass
);
5588 addr
= mono_object_unbox_internal (obj
);
5591 } else if (m_class_get_rank (obj
->vtable
->klass
)) {
5592 buffer_add_byte (buf
, m_class_get_byval_arg (obj
->vtable
->klass
)->type
);
5593 } else if (m_class_get_byval_arg (obj
->vtable
->klass
)->type
== MONO_TYPE_GENERICINST
) {
5594 buffer_add_byte (buf
, MONO_TYPE_CLASS
);
5596 buffer_add_byte (buf
, m_class_get_byval_arg (obj
->vtable
->klass
)->type
);
5598 buffer_add_objid (buf
, obj
);
5602 case MONO_TYPE_VALUETYPE
:
5603 case MONO_TYPE_TYPEDBYREF
: {
5607 MonoClass
*klass
= mono_class_from_mono_type_internal (t
);
5612 * Handle boxed vtypes recursively referencing themselves using fields.
5615 parent_vtypes
= g_hash_table_new (NULL
, NULL
);
5616 vtype_index
= GPOINTER_TO_INT (g_hash_table_lookup (parent_vtypes
, addr
));
5618 if (CHECK_PROTOCOL_VERSION (2, 33)) {
5619 buffer_add_byte (buf
, VALUE_TYPE_ID_PARENT_VTYPE
);
5620 buffer_add_int (buf
, vtype_index
- 1);
5622 /* The client can't handle PARENT_VTYPE */
5623 buffer_add_byte (buf
, VALUE_TYPE_ID_NULL
);
5627 g_hash_table_insert (parent_vtypes
, addr
, GINT_TO_POINTER (g_hash_table_size (parent_vtypes
) + 1));
5631 buffer_add_byte (buf
, MONO_TYPE_VALUETYPE
);
5632 buffer_add_byte (buf
, m_class_is_enumtype (klass
));
5633 buffer_add_typeid (buf
, domain
, klass
);
5637 while ((f
= mono_class_get_fields_internal (klass
, &iter
))) {
5638 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
5640 if (mono_field_is_deleted (f
))
5644 buffer_add_int (buf
, nfields
);
5647 while ((f
= mono_class_get_fields_internal (klass
, &iter
))) {
5648 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
5650 if (mono_field_is_deleted (f
))
5652 buffer_add_value_full (buf
, f
->type
, mono_vtype_get_field_addr (addr
, f
), domain
, FALSE
, parent_vtypes
, len_fixed_array
!= 1 ? len_fixed_array
: isFixedSizeArray(f
));
5656 g_hash_table_remove (parent_vtypes
, addr
);
5657 if (g_hash_table_size (parent_vtypes
) == 0) {
5658 g_hash_table_destroy (parent_vtypes
);
5659 parent_vtypes
= NULL
;
5664 case MONO_TYPE_GENERICINST
:
5665 if (mono_type_generic_inst_is_valuetype (t
)) {
5677 buffer_add_value (Buffer
*buf
, MonoType
*t
, void *addr
, MonoDomain
*domain
)
5679 buffer_add_value_full (buf
, t
, addr
, domain
, FALSE
, NULL
, 1);
5683 obj_is_of_type (MonoObject
*obj
, MonoType
*t
)
5685 MonoClass
*klass
= obj
->vtable
->klass
;
5686 if (!mono_class_is_assignable_from_internal (mono_class_from_mono_type_internal (t
), klass
)) {
5687 if (mono_class_is_transparent_proxy (klass
)) {
5688 klass
= ((MonoTransparentProxy
*)obj
)->remote_class
->proxy_class
;
5689 if (mono_class_is_assignable_from_internal (mono_class_from_mono_type_internal (t
), klass
)) {
5699 decode_value (MonoType
*t
, MonoDomain
*domain
, gpointer void_addr
, gpointer void_buf
, guint8
**endbuf
, guint8
*limit
, gboolean check_field_datatype
);
5702 decode_vtype (MonoType
*t
, MonoDomain
*domain
, gpointer void_addr
, gpointer void_buf
, guint8
**endbuf
, guint8
*limit
, gboolean check_field_datatype
)
5704 guint8
*addr
= (guint8
*)void_addr
;
5705 guint8
*buf
= (guint8
*)void_buf
;
5710 gpointer iter
= NULL
;
5714 is_enum
= decode_byte (buf
, &buf
, limit
);
5715 /* Enums are sent as a normal vtype */
5717 return ERR_NOT_IMPLEMENTED
;
5718 klass
= decode_typeid (buf
, &buf
, limit
, &d
, &err
);
5719 if (err
!= ERR_NONE
)
5722 if (t
&& klass
!= mono_class_from_mono_type_internal (t
)) {
5723 char *name
= mono_type_full_name (t
);
5724 char *name2
= mono_type_full_name (m_class_get_byval_arg (klass
));
5725 DEBUG_PRINTF (1, "[%p] Expected value of type %s, got %s.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), name
, name2
);
5728 return ERR_INVALID_ARGUMENT
;
5731 nfields
= decode_int (buf
, &buf
, limit
);
5732 while ((f
= mono_class_get_fields_internal (klass
, &iter
))) {
5733 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
5735 if (mono_field_is_deleted (f
))
5737 err
= decode_value (f
->type
, domain
, mono_vtype_get_field_addr (addr
, f
), buf
, &buf
, limit
, check_field_datatype
);
5738 if (err
!= ERR_NONE
)
5742 g_assert (nfields
== 0);
5748 static ErrorCode
decode_fixed_size_array_internal (MonoType
*t
, int type
, MonoDomain
*domain
, guint8
*addr
, guint8
*buf
, guint8
**endbuf
, guint8
*limit
, gboolean check_field_datatype
)
5750 ErrorCode err
= ERR_NONE
;
5751 int fixedSizeLen
= 1;
5752 int newType
= MONO_TYPE_END
;
5753 if (CHECK_PROTOCOL_VERSION (2, 53)) {
5754 newType
= decode_byte (buf
, &buf
, limit
);
5755 fixedSizeLen
= decode_int (buf
, &buf
, limit
);
5756 //t->type = newType;
5758 for (int i
= 0 ; i
< fixedSizeLen
; i
++) {
5760 case MONO_TYPE_BOOLEAN
:
5761 ((guint8
*)addr
)[i
] = decode_int (buf
, &buf
, limit
);
5763 case MONO_TYPE_CHAR
:
5764 ((gunichar2
*)addr
)[i
] = decode_int (buf
, &buf
, limit
);
5767 ((gint8
*)addr
)[i
] = decode_int (buf
, &buf
, limit
);
5770 ((guint8
*)addr
)[i
] = decode_int (buf
, &buf
, limit
);
5773 ((gint16
*)addr
)[i
] = decode_int (buf
, &buf
, limit
);
5776 ((guint16
*)addr
)[i
] = decode_int (buf
, &buf
, limit
);
5779 ((gint32
*)addr
)[i
] = decode_int (buf
, &buf
, limit
);
5782 ((guint32
*)addr
)[i
] = decode_int (buf
, &buf
, limit
);
5785 ((gint64
*)addr
)[i
] = decode_long (buf
, &buf
, limit
);
5788 ((guint64
*)addr
)[i
] = decode_long (buf
, &buf
, limit
);
5791 ((guint32
*)addr
)[i
] = decode_int (buf
, &buf
, limit
);
5794 ((guint64
*)addr
)[i
] = decode_long (buf
, &buf
, limit
);
5802 decode_value_internal (MonoType
*t
, int type
, MonoDomain
*domain
, guint8
*addr
, guint8
*buf
, guint8
**endbuf
, guint8
*limit
, gboolean check_field_datatype
)
5805 if (type
!= t
->type
&& !MONO_TYPE_IS_REFERENCE (t
) &&
5806 !(t
->type
== MONO_TYPE_I
&& type
== MONO_TYPE_VALUETYPE
) &&
5807 !(type
== VALUE_TYPE_ID_FIXED_ARRAY
) &&
5808 !(t
->type
== MONO_TYPE_U
&& type
== MONO_TYPE_VALUETYPE
) &&
5809 !(t
->type
== MONO_TYPE_PTR
&& type
== MONO_TYPE_I8
) &&
5810 !(t
->type
== MONO_TYPE_GENERICINST
&& type
== MONO_TYPE_VALUETYPE
) &&
5811 !(t
->type
== MONO_TYPE_VALUETYPE
&& type
== MONO_TYPE_OBJECT
)) {
5812 char *name
= mono_type_full_name (t
);
5813 DEBUG_PRINTF (1, "[%p] Expected value of type %s, got 0x%0x.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), name
, type
);
5815 return ERR_INVALID_ARGUMENT
;
5817 if (type
== VALUE_TYPE_ID_FIXED_ARRAY
&& t
->type
!= MONO_TYPE_VALUETYPE
) {
5818 decode_fixed_size_array_internal (t
, type
, domain
, addr
, buf
, endbuf
, limit
, check_field_datatype
);
5823 case MONO_TYPE_BOOLEAN
:
5824 *(guint8
*)addr
= decode_int (buf
, &buf
, limit
);
5826 case MONO_TYPE_CHAR
:
5827 *(gunichar2
*)addr
= decode_int (buf
, &buf
, limit
);
5830 *(gint8
*)addr
= decode_int (buf
, &buf
, limit
);
5833 *(guint8
*)addr
= decode_int (buf
, &buf
, limit
);
5836 *(gint16
*)addr
= decode_int (buf
, &buf
, limit
);
5839 *(guint16
*)addr
= decode_int (buf
, &buf
, limit
);
5842 *(gint32
*)addr
= decode_int (buf
, &buf
, limit
);
5845 *(guint32
*)addr
= decode_int (buf
, &buf
, limit
);
5848 *(gint64
*)addr
= decode_long (buf
, &buf
, limit
);
5851 *(guint64
*)addr
= decode_long (buf
, &buf
, limit
);
5854 *(guint32
*)addr
= decode_int (buf
, &buf
, limit
);
5857 *(guint64
*)addr
= decode_long (buf
, &buf
, limit
);
5860 /* We send these as I8, so we get them back as such */
5861 g_assert (type
== MONO_TYPE_I8
);
5862 *(gssize
*)addr
= decode_long (buf
, &buf
, limit
);
5864 case MONO_TYPE_GENERICINST
:
5865 if (MONO_TYPE_ISSTRUCT (t
)) {
5866 /* The client sends these as a valuetype */
5874 /* We send these as vtypes, so we get them back as such */
5875 g_assert (type
== MONO_TYPE_VALUETYPE
);
5878 case MONO_TYPE_VALUETYPE
:
5879 if (type
== MONO_TYPE_OBJECT
) {
5881 int objid
= decode_objid (buf
, &buf
, limit
);
5885 err
= get_object (objid
, (MonoObject
**)&obj
);
5886 if (err
!= ERR_NONE
)
5889 return ERR_INVALID_ARGUMENT
;
5890 if (obj
->vtable
->klass
!= mono_class_from_mono_type_internal (t
)) {
5891 DEBUG_PRINTF (1, "Expected type '%s', got object '%s'\n", mono_type_full_name (t
), m_class_get_name (obj
->vtable
->klass
));
5892 return ERR_INVALID_ARGUMENT
;
5894 memcpy (addr
, mono_object_unbox_internal (obj
), mono_class_value_size (obj
->vtable
->klass
, NULL
));
5896 err
= decode_vtype (t
, domain
, addr
, buf
, &buf
, limit
, check_field_datatype
);
5897 if (err
!= ERR_NONE
)
5903 if (MONO_TYPE_IS_REFERENCE (t
)) {
5904 if (type
== MONO_TYPE_OBJECT
) {
5905 int objid
= decode_objid (buf
, &buf
, limit
);
5909 err
= get_object (objid
, (MonoObject
**)&obj
);
5910 if (err
!= ERR_NONE
)
5914 if (!obj_is_of_type (obj
, t
)) {
5915 if (check_field_datatype
) { //if it's not executing a invoke method check the datatypes.
5916 DEBUG_PRINTF (1, "Expected type '%s', got '%s'\n", mono_type_full_name (t
), m_class_get_name (obj
->vtable
->klass
));
5917 return ERR_INVALID_ARGUMENT
;
5921 if (obj
&& obj
->vtable
->domain
!= domain
)
5922 return ERR_INVALID_ARGUMENT
;
5924 mono_gc_wbarrier_generic_store_internal (addr
, obj
);
5925 } else if (type
== VALUE_TYPE_ID_NULL
) {
5926 *(MonoObject
**)addr
= NULL
;
5927 } else if (type
== MONO_TYPE_VALUETYPE
) {
5936 /* This can happen when round-tripping boxed vtypes */
5938 * Obtain vtype class.
5939 * Same as the beginning of the handle_vtype case above.
5942 is_enum
= decode_byte (buf
, &buf
, limit
);
5944 return ERR_NOT_IMPLEMENTED
;
5945 klass
= decode_typeid (buf
, &buf
, limit
, &d
, &err
);
5946 if (err
!= ERR_NONE
)
5949 /* Decode the vtype into a temporary buffer, then box it. */
5950 vtype_buf_size
= mono_class_value_size (klass
, NULL
);
5951 vtype_buf
= (guint8
*)g_malloc0 (vtype_buf_size
);
5952 g_assert (vtype_buf
);
5955 err
= decode_vtype (NULL
, domain
, vtype_buf
, buf
, &buf
, limit
, check_field_datatype
);
5956 if (err
!= ERR_NONE
) {
5960 *(MonoObject
**)addr
= mono_value_box_checked (d
, klass
, vtype_buf
, error
);
5961 mono_error_cleanup (error
);
5964 char *name
= mono_type_full_name (t
);
5965 DEBUG_PRINTF (1, "[%p] Expected value of type %s, got 0x%0x.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), name
, type
);
5967 return ERR_INVALID_ARGUMENT
;
5969 } else if ((t
->type
== MONO_TYPE_GENERICINST
) &&
5970 mono_metadata_generic_class_is_valuetype (t
->data
.generic_class
) &&
5971 m_class_is_enumtype (t
->data
.generic_class
->container_class
)){
5972 err
= decode_vtype (t
, domain
, addr
, buf
, &buf
, limit
, check_field_datatype
);
5973 if (err
!= ERR_NONE
)
5988 decode_value (MonoType
*t
, MonoDomain
*domain
, gpointer void_addr
, gpointer void_buf
, guint8
**endbuf
, guint8
*limit
, gboolean check_field_datatype
)
5990 guint8
*addr
= (guint8
*)void_addr
;
5991 guint8
*buf
= (guint8
*)void_buf
;
5995 int type
= decode_byte (buf
, &buf
, limit
);
5997 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type_internal (t
))) {
5998 MonoType
*targ
= t
->data
.generic_class
->context
.class_inst
->type_argv
[0];
5999 guint8
*nullable_buf
;
6002 * First try decoding it as a Nullable`1
6004 err
= decode_value_internal (t
, type
, domain
, addr
, buf
, endbuf
, limit
, check_field_datatype
);
6005 if (err
== ERR_NONE
)
6009 * Then try decoding as a primitive value or null.
6011 if (targ
->type
== type
) {
6012 nullable_buf
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type_internal (targ
)));
6013 err
= decode_value_internal (targ
, type
, domain
, nullable_buf
, buf
, endbuf
, limit
, check_field_datatype
);
6014 if (err
!= ERR_NONE
) {
6015 g_free (nullable_buf
);
6018 MonoObject
*boxed
= mono_value_box_checked (domain
, mono_class_from_mono_type_internal (targ
), nullable_buf
, error
);
6019 if (!is_ok (error
)) {
6020 mono_error_cleanup (error
);
6021 return ERR_INVALID_OBJECT
;
6023 mono_nullable_init (addr
, boxed
, mono_class_from_mono_type_internal (t
));
6024 g_free (nullable_buf
);
6027 } else if (type
== VALUE_TYPE_ID_NULL
) {
6028 mono_nullable_init (addr
, NULL
, mono_class_from_mono_type_internal (t
));
6034 return decode_value_internal (t
, type
, domain
, addr
, buf
, endbuf
, limit
, check_field_datatype
);
6038 add_var (Buffer
*buf
, MonoDebugMethodJitInfo
*jit
, MonoType
*t
, MonoDebugVarInfo
*var
, MonoContext
*ctx
, MonoDomain
*domain
, gboolean as_vtype
)
6042 guint8
*addr
, *gaddr
;
6043 host_mgreg_t reg_val
;
6045 flags
= var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6046 reg
= var
->index
& ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6049 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
:
6050 reg_val
= mono_arch_context_get_int_reg (ctx
, reg
);
6052 buffer_add_value_full (buf
, t
, ®_val
, domain
, as_vtype
, NULL
, 1);
6054 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
:
6055 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6056 addr
+= (gint32
)var
->offset
;
6058 //printf ("[R%d+%d] = %p\n", reg, var->offset, addr);
6060 buffer_add_value_full (buf
, t
, addr
, domain
, as_vtype
, NULL
, 1);
6062 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD
:
6065 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR
:
6066 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR
:
6067 /* Same as regoffset, but with an indirection */
6068 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6069 addr
+= (gint32
)var
->offset
;
6071 gaddr
= (guint8
*)*(gpointer
*)addr
;
6073 buffer_add_value_full (buf
, t
, gaddr
, domain
, as_vtype
, NULL
, 1);
6075 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL
: {
6076 MonoDebugVarInfo
*info_var
= jit
->gsharedvt_info_var
;
6077 MonoDebugVarInfo
*locals_var
= jit
->gsharedvt_locals_var
;
6078 MonoGSharedVtMethodRuntimeInfo
*info
;
6084 g_assert (info_var
);
6085 g_assert (locals_var
);
6087 flags
= info_var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6088 reg
= info_var
->index
& ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6089 if (flags
== MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
) {
6090 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6091 addr
+= (gint32
)info_var
->offset
;
6092 info
= (MonoGSharedVtMethodRuntimeInfo
*)*(gpointer
*)addr
;
6093 } else if (flags
== MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
) {
6094 info
= (MonoGSharedVtMethodRuntimeInfo
*)mono_arch_context_get_int_reg (ctx
, reg
);
6096 g_assert_not_reached ();
6100 flags
= locals_var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6101 reg
= locals_var
->index
& ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6102 if (flags
== MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
) {
6103 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6104 addr
+= (gint32
)locals_var
->offset
;
6105 locals
= (guint8
*)*(gpointer
*)addr
;
6106 } else if (flags
== MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
) {
6107 locals
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6109 g_assert_not_reached ();
6113 addr
= locals
+ GPOINTER_TO_INT (info
->entries
[idx
]);
6115 buffer_add_value_full (buf
, t
, addr
, domain
, as_vtype
, NULL
, 1);
6120 g_assert_not_reached ();
6125 set_var (MonoType
*t
, MonoDebugVarInfo
*var
, MonoContext
*ctx
, MonoDomain
*domain
, guint8
*val
, host_mgreg_t
**reg_locations
, MonoContext
*restore_ctx
)
6129 guint8
*addr
, *gaddr
;
6131 flags
= var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6132 reg
= var
->index
& ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
6134 if (MONO_TYPE_IS_REFERENCE (t
))
6135 size
= sizeof (gpointer
);
6137 size
= mono_class_value_size (mono_class_from_mono_type_internal (t
), NULL
);
6140 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
: {
6141 #ifdef MONO_ARCH_HAVE_CONTEXT_SET_INT_REG
6143 gboolean is_signed
= FALSE
;
6146 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6149 // FIXME: Write barriers
6150 mono_gc_memmove_atomic (addr
, val
, size
);
6155 if (!t
->byref
&& (t
->type
== MONO_TYPE_I1
|| t
->type
== MONO_TYPE_I2
|| t
->type
== MONO_TYPE_I4
|| t
->type
== MONO_TYPE_I8
))
6160 v
= is_signed
? *(gint8
*)val
: *(guint8
*)val
;
6163 v
= is_signed
? *(gint16
*)val
: *(guint16
*)val
;
6166 v
= is_signed
? *(gint32
*)val
: *(guint32
*)val
;
6169 v
= is_signed
? *(gint64
*)val
: *(guint64
*)val
;
6172 g_assert_not_reached ();
6175 /* Set value on the stack or in the return ctx */
6176 if (reg_locations
[reg
]) {
6177 /* Saved on the stack */
6178 DEBUG_PRINTF (1, "[dbg] Setting stack location %p for reg %x to %p.\n", reg_locations
[reg
], reg
, (gpointer
)v
);
6179 *(reg_locations
[reg
]) = v
;
6182 DEBUG_PRINTF (1, "[dbg] Setting context location for reg %x to %p.\n", reg
, (gpointer
)v
);
6183 mono_arch_context_set_int_reg (restore_ctx
, reg
, v
);
6186 // FIXME: Move these to mono-context.h/c.
6187 mono_arch_context_set_int_reg (ctx
, reg
, v
);
6189 // FIXME: Can't set registers, so we disable linears
6194 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
:
6195 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6196 addr
+= (gint32
)var
->offset
;
6198 //printf ("[R%d+%d] = %p\n", reg, var->offset, addr);
6201 addr
= *(guint8
**)addr
;
6207 // FIXME: Write barriers
6208 mono_gc_memmove_atomic (addr
, val
, size
);
6210 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR
:
6211 /* Same as regoffset, but with an indirection */
6212 addr
= (guint8
*)mono_arch_context_get_int_reg (ctx
, reg
);
6213 addr
+= (gint32
)var
->offset
;
6215 gaddr
= (guint8
*)*(gpointer
*)addr
;
6217 // FIXME: Write barriers
6218 mono_gc_memmove_atomic (gaddr
, val
, size
);
6220 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD
:
6224 g_assert_not_reached ();
6229 set_interp_var (MonoType
*t
, gpointer addr
, guint8
*val_buf
)
6234 addr
= *(gpointer
*)addr
;
6238 if (MONO_TYPE_IS_REFERENCE (t
))
6239 size
= sizeof (gpointer
);
6241 size
= mono_class_value_size (mono_class_from_mono_type_internal (t
), NULL
);
6243 memcpy (addr
, val_buf
, size
);
6247 clear_event_request (int req_id
, int etype
)
6251 mono_loader_lock ();
6252 for (i
= 0; i
< event_requests
->len
; ++i
) {
6253 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
6255 if (req
->id
== req_id
&& req
->event_kind
== etype
) {
6256 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
)
6257 mono_de_clear_breakpoint ((MonoBreakpoint
*)req
->info
);
6258 if (req
->event_kind
== EVENT_KIND_STEP
) {
6259 mono_de_cancel_ss ((SingleStepReq
*)req
->info
);
6261 if (req
->event_kind
== EVENT_KIND_METHOD_ENTRY
)
6262 mono_de_clear_breakpoint ((MonoBreakpoint
*)req
->info
);
6263 if (req
->event_kind
== EVENT_KIND_METHOD_EXIT
)
6264 mono_de_clear_breakpoint ((MonoBreakpoint
*)req
->info
);
6265 g_ptr_array_remove_index_fast (event_requests
, i
);
6270 mono_loader_unlock ();
6274 clear_assembly_from_modifier (EventRequest
*req
, Modifier
*m
, MonoAssembly
*assembly
)
6278 if (m
->kind
== MOD_KIND_EXCEPTION_ONLY
&& m
->data
.exc_class
&& m_class_get_image (m
->data
.exc_class
)->assembly
== assembly
)
6279 m
->kind
= MOD_KIND_NONE
;
6280 if (m
->kind
== MOD_KIND_ASSEMBLY_ONLY
&& m
->data
.assemblies
) {
6281 int count
= 0, match_count
= 0, pos
;
6282 MonoAssembly
**newassemblies
;
6284 for (i
= 0; m
->data
.assemblies
[i
]; ++i
) {
6286 if (m
->data
.assemblies
[i
] == assembly
)
6291 // +1 because we don't know length and we use last element to check for end
6292 newassemblies
= g_new0 (MonoAssembly
*, count
- match_count
+ 1);
6295 for (i
= 0; i
< count
; ++i
)
6296 if (m
->data
.assemblies
[i
] != assembly
)
6297 newassemblies
[pos
++] = m
->data
.assemblies
[i
];
6298 g_assert (pos
== count
- match_count
);
6299 g_free (m
->data
.assemblies
);
6300 m
->data
.assemblies
= newassemblies
;
6306 clear_assembly_from_modifiers (EventRequest
*req
, MonoAssembly
*assembly
)
6310 for (i
= 0; i
< req
->nmodifiers
; ++i
) {
6311 Modifier
*m
= &req
->modifiers
[i
];
6313 clear_assembly_from_modifier (req
, m
, assembly
);
6318 * clear_event_requests_for_assembly:
6320 * Clear all events requests which reference ASSEMBLY.
6323 clear_event_requests_for_assembly (MonoAssembly
*assembly
)
6328 mono_loader_lock ();
6332 for (i
= 0; i
< event_requests
->len
; ++i
) {
6333 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
6335 clear_assembly_from_modifiers (req
, assembly
);
6337 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
&& breakpoint_matches_assembly ((MonoBreakpoint
*)req
->info
, assembly
)) {
6338 clear_event_request (req
->id
, req
->event_kind
);
6343 if (req
->event_kind
== EVENT_KIND_STEP
)
6344 ss_clear_for_assembly ((SingleStepReq
*)req
->info
, assembly
);
6347 mono_loader_unlock ();
6351 * type_comes_from_assembly:
6353 * GHRFunc that returns TRUE if klass comes from assembly
6356 type_comes_from_assembly (gpointer klass
, gpointer also_klass
, gpointer assembly
)
6358 return mono_type_in_image (m_class_get_byval_arg ((MonoClass
*)klass
), mono_assembly_get_image_internal ((MonoAssembly
*)assembly
));
6362 * clear_types_for_assembly:
6364 * Clears types from loaded_classes for a given assembly
6367 clear_types_for_assembly (MonoAssembly
*assembly
)
6369 MonoDomain
*domain
= mono_domain_get ();
6370 AgentDomainInfo
*info
= NULL
;
6372 if (!domain
|| !domain_jit_info (domain
))
6373 /* Can happen during shutdown */
6376 info
= get_agent_domain_info (domain
);
6378 mono_loader_lock ();
6379 g_hash_table_foreach_remove (info
->loaded_classes
, type_comes_from_assembly
, assembly
);
6380 mono_loader_unlock ();
6386 /* Clear all event requests */
6387 mono_loader_lock ();
6388 while (event_requests
->len
> 0) {
6389 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, 0);
6391 clear_event_request (req
->id
, req
->event_kind
);
6393 mono_loader_unlock ();
6395 while (suspend_count
> 0)
6397 disconnected
= TRUE
;
6398 vm_start_event_sent
= FALSE
;
6402 count_thread_check_gc_finalizer (gpointer key
, gpointer value
, gpointer user_data
)
6404 MonoThread
*thread
= (MonoThread
*)value
;
6405 gboolean
*ret
= (gboolean
*)user_data
;
6406 if (mono_gc_is_finalizer_internal_thread(thread
->internal_thread
)) {
6407 DebuggerTlsData
*tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
->internal_thread
);
6408 if (!tls
->gc_finalizing
) { //GC Finalizer is not running some finalizer code, so ignore it
6416 add_thread (gpointer key
, gpointer value
, gpointer user_data
)
6418 MonoThread
*thread
= (MonoThread
*)value
;
6419 Buffer
*buf
= (Buffer
*)user_data
;
6420 if (mono_gc_is_finalizer_internal_thread(thread
->internal_thread
)) {
6421 DebuggerTlsData
*tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
->internal_thread
);
6422 if (!tls
->gc_finalizing
) //GC Finalizer is not running some finalizer code, so ignore it
6425 buffer_add_objid (buf
, (MonoObject
*)thread
);
6430 do_invoke_method (DebuggerTlsData
*tls
, Buffer
*buf
, InvokeData
*invoke
, guint8
*p
, guint8
**endp
)
6433 guint8
*end
= invoke
->endp
;
6437 MonoMethodSignature
*sig
;
6440 MonoObject
*this_arg
, *res
, *exc
= NULL
;
6443 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6446 MonoStopwatch watch
;
6448 if (invoke
->method
) {
6450 * Invoke this method directly, currently only Environment.Exit () is supported.
6453 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
? m_class_get_name (this_arg
->vtable
->klass
) : "<null>");
6455 mono_runtime_try_invoke (invoke
->method
, NULL
, invoke
->args
, &exc
, error
);
6456 mono_error_assert_ok (error
);
6457 g_assert_not_reached ();
6460 m
= decode_methodid (p
, &p
, end
, &domain
, &err
);
6461 if (err
!= ERR_NONE
)
6463 sig
= mono_method_signature_internal (m
);
6465 if (m_class_is_valuetype (m
->klass
))
6466 this_buf
= (guint8
*)g_alloca (mono_class_instance_size (m
->klass
));
6468 this_buf
= (guint8
*)g_alloca (sizeof (MonoObject
*));
6470 if (m
->is_generic
) {
6471 DEBUG_PRINTF (1, "[%p] Error: Attempting to invoke uninflated generic method %s.\n", (gpointer
)(gsize
)mono_native_thread_id_get (), mono_method_full_name (m
, TRUE
));
6472 return ERR_INVALID_ARGUMENT
;
6473 } else if (m_class_is_valuetype (m
->klass
) && (m
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
6474 /* Should be null */
6475 int type
= decode_byte (p
, &p
, end
);
6476 if (type
!= VALUE_TYPE_ID_NULL
) {
6477 DEBUG_PRINTF (1, "[%p] Error: Static vtype method invoked with this argument.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
6478 return ERR_INVALID_ARGUMENT
;
6480 memset (this_buf
, 0, mono_class_instance_size (m
->klass
));
6481 } else if (m_class_is_valuetype (m
->klass
) && !strcmp (m
->name
, ".ctor")) {
6485 int type
= decode_byte (p
, &tmp_p
, end
);
6486 if (type
== VALUE_TYPE_ID_NULL
) {
6487 memset (this_buf
, 0, mono_class_instance_size (m
->klass
));
6490 err
= decode_value (m_class_get_byval_arg (m
->klass
), domain
, this_buf
, p
, &p
, end
, FALSE
);
6491 if (err
!= ERR_NONE
)
6495 err
= decode_value (m_class_get_byval_arg (m
->klass
), domain
, this_buf
, p
, &p
, end
, FALSE
);
6496 if (err
!= ERR_NONE
)
6500 if (!m_class_is_valuetype (m
->klass
))
6501 this_arg
= *(MonoObject
**)this_buf
;
6505 if (MONO_CLASS_IS_INTERFACE_INTERNAL (m
->klass
)) {
6507 DEBUG_PRINTF (1, "[%p] Error: Interface method invoked without this argument.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
6508 return ERR_INVALID_ARGUMENT
;
6510 m
= mono_object_get_virtual_method_internal (this_arg
, m
);
6511 /* Transform this to the format the rest of the code expects it to be */
6512 if (m_class_is_valuetype (m
->klass
)) {
6513 this_buf
= (guint8
*)g_alloca (mono_class_instance_size (m
->klass
));
6514 memcpy (this_buf
, mono_object_unbox_internal (this_arg
), mono_class_instance_size (m
->klass
));
6516 } else if ((m
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !m_class_is_valuetype (m
->klass
) && invoke
->flags
& INVOKE_FLAG_VIRTUAL
) {
6518 DEBUG_PRINTF (1, "[%p] Error: invoke with INVOKE_FLAG_VIRTUAL flag set without this argument.\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
6519 return ERR_INVALID_ARGUMENT
;
6521 m
= mono_object_get_virtual_method_internal (this_arg
, m
);
6522 if (m_class_is_valuetype (m
->klass
)) {
6523 this_buf
= (guint8
*)g_alloca (mono_class_instance_size (m
->klass
));
6524 memcpy (this_buf
, mono_object_unbox_internal (this_arg
), mono_class_instance_size (m
->klass
));
6528 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
? m_class_get_name (this_arg
->vtable
->klass
) : "<null>");
6530 if (this_arg
&& this_arg
->vtable
->domain
!= domain
)
6533 if (!m_class_is_valuetype (m
->klass
) && !(m
->flags
& METHOD_ATTRIBUTE_STATIC
) && !this_arg
) {
6534 if (!strcmp (m
->name
, ".ctor")) {
6535 if (mono_class_is_abstract (m
->klass
))
6536 return ERR_INVALID_ARGUMENT
;
6539 this_arg
= mono_object_new_checked (domain
, m
->klass
, error
);
6540 mono_error_assert_ok (error
);
6543 return ERR_INVALID_ARGUMENT
;
6547 if (this_arg
&& !obj_is_of_type (this_arg
, m_class_get_byval_arg (m
->klass
)))
6548 return ERR_INVALID_ARGUMENT
;
6550 nargs
= decode_int (p
, &p
, end
);
6551 if (nargs
!= sig
->param_count
)
6552 return ERR_INVALID_ARGUMENT
;
6553 /* Use alloca to get gc tracking */
6554 arg_buf
= (guint8
**)g_alloca (nargs
* sizeof (gpointer
));
6555 memset (arg_buf
, 0, nargs
* sizeof (gpointer
));
6556 args
= (gpointer
*)g_alloca (nargs
* sizeof (gpointer
));
6557 for (i
= 0; i
< nargs
; ++i
) {
6558 if (MONO_TYPE_IS_REFERENCE (sig
->params
[i
])) {
6559 err
= decode_value (sig
->params
[i
], domain
, (guint8
*)&args
[i
], p
, &p
, end
, TRUE
);
6560 if (err
!= ERR_NONE
)
6562 if (args
[i
] && ((MonoObject
*)args
[i
])->vtable
->domain
!= domain
)
6565 if (sig
->params
[i
]->byref
) {
6566 arg_buf
[i
] = g_newa (guint8
, sizeof (gpointer
));
6567 *(gpointer
*)arg_buf
[i
] = args
[i
];
6568 args
[i
] = arg_buf
[i
];
6571 MonoClass
*arg_class
= mono_class_from_mono_type_internal (sig
->params
[i
]);
6572 arg_buf
[i
] = (guint8
*)g_alloca (mono_class_instance_size (arg_class
));
6573 err
= decode_value (sig
->params
[i
], domain
, arg_buf
[i
], p
, &p
, end
, TRUE
);
6574 if (err
!= ERR_NONE
)
6576 if (mono_class_is_nullable (arg_class
)) {
6577 args
[i
] = mono_nullable_box (arg_buf
[i
], arg_class
, error
);
6578 mono_error_assert_ok (error
);
6580 args
[i
] = arg_buf
[i
];
6588 if (invoke
->flags
& INVOKE_FLAG_DISABLE_BREAKPOINTS
)
6589 tls
->disable_breakpoints
= TRUE
;
6591 tls
->disable_breakpoints
= FALSE
;
6594 * Add an LMF frame to link the stack frames on the invoke method with our caller.
6596 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6597 if (invoke
->has_ctx
) {
6599 memset (&ext
, 0, sizeof (ext
));
6600 ext
.kind
= MONO_LMFEXT_DEBUGGER_INVOKE
;
6601 memcpy (&ext
.ctx
, &invoke
->ctx
, sizeof (MonoContext
));
6603 mono_push_lmf (&ext
);
6607 mono_stopwatch_start (&watch
);
6608 res
= mono_runtime_try_invoke (m
, m_class_is_valuetype (m
->klass
) ? (gpointer
) this_buf
: (gpointer
) this_arg
, args
, &exc
, error
);
6609 if (!is_ok (error
) && exc
== NULL
) {
6610 exc
= (MonoObject
*) mono_error_convert_to_exception (error
);
6612 mono_error_cleanup (error
); /* FIXME report error */
6614 mono_stopwatch_stop (&watch
);
6615 DEBUG_PRINTF (1, "[%p] Invoke result: %p, exc: %s, time: %ld ms.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), res
, exc
? m_class_get_name (exc
->vtable
->klass
) : NULL
, (long)mono_stopwatch_elapsed_ms (&watch
));
6617 buffer_add_byte (buf
, 0);
6618 buffer_add_value (buf
, mono_get_object_type (), &exc
, domain
);
6620 gboolean out_this
= FALSE
;
6621 gboolean out_args
= FALSE
;
6623 if ((invoke
->flags
& INVOKE_FLAG_RETURN_OUT_THIS
) && CHECK_PROTOCOL_VERSION (2, 35))
6625 if ((invoke
->flags
& INVOKE_FLAG_RETURN_OUT_ARGS
) && CHECK_PROTOCOL_VERSION (2, 35))
6627 buffer_add_byte (buf
, 1 + (out_this
? 2 : 0) + (out_args
? 4 : 0));
6628 if (m
->string_ctor
) {
6629 buffer_add_value (buf
, m_class_get_byval_arg (mono_get_string_class ()), &res
, domain
);
6630 } else if (sig
->ret
->type
== MONO_TYPE_VOID
&& !m
->string_ctor
) {
6631 if (!strcmp (m
->name
, ".ctor")) {
6632 if (!m_class_is_valuetype (m
->klass
))
6633 buffer_add_value (buf
, mono_get_object_type (), &this_arg
, domain
);
6635 buffer_add_value (buf
, m_class_get_byval_arg (m
->klass
), this_buf
, domain
);
6637 buffer_add_value (buf
, mono_get_void_type (), NULL
, domain
);
6639 } else if (MONO_TYPE_IS_REFERENCE (sig
->ret
)) {
6640 if (sig
->ret
->byref
) {
6641 MonoType
* ret_byval
= m_class_get_byval_arg (mono_class_from_mono_type_internal (sig
->ret
));
6642 buffer_add_value (buf
, ret_byval
, &res
, domain
);
6644 buffer_add_value (buf
, sig
->ret
, &res
, domain
);
6646 } else if (m_class_is_valuetype (mono_class_from_mono_type_internal (sig
->ret
)) || sig
->ret
->type
== MONO_TYPE_PTR
|| sig
->ret
->type
== MONO_TYPE_FNPTR
) {
6647 if (mono_class_is_nullable (mono_class_from_mono_type_internal (sig
->ret
))) {
6648 MonoClass
*k
= mono_class_from_mono_type_internal (sig
->ret
);
6649 guint8
*nullable_buf
= (guint8
*)g_alloca (mono_class_value_size (k
, NULL
));
6651 g_assert (nullable_buf
);
6652 mono_nullable_init (nullable_buf
, res
, k
);
6653 buffer_add_value (buf
, sig
->ret
, nullable_buf
, domain
);
6657 if (sig
->ret
->byref
) {
6658 MonoType
* ret_byval
= m_class_get_byval_arg (mono_class_from_mono_type_internal (sig
->ret
));
6659 buffer_add_value (buf
, ret_byval
, mono_object_unbox_internal (res
), domain
);
6661 buffer_add_value (buf
, sig
->ret
, mono_object_unbox_internal (res
), domain
);
6668 /* Return the new value of the receiver after the call */
6669 buffer_add_value (buf
, m_class_get_byval_arg (m
->klass
), this_buf
, domain
);
6671 buffer_add_int (buf
, nargs
);
6672 for (i
= 0; i
< nargs
; ++i
) {
6673 if (MONO_TYPE_IS_REFERENCE (sig
->params
[i
]))
6674 buffer_add_value (buf
, sig
->params
[i
], &args
[i
], domain
);
6675 else if (sig
->params
[i
]->byref
)
6676 /* add_value () does an indirection */
6677 buffer_add_value (buf
, sig
->params
[i
], &arg_buf
[i
], domain
);
6679 buffer_add_value (buf
, sig
->params
[i
], arg_buf
[i
], domain
);
6684 tls
->disable_breakpoints
= FALSE
;
6686 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
6687 if (invoke
->has_ctx
)
6688 mono_pop_lmf ((MonoLMF
*)&ext
);
6692 // FIXME: byref arguments
6700 * Invoke the method given by tls->pending_invoke in the current thread.
6703 invoke_method (void)
6705 DebuggerTlsData
*tls
;
6711 MonoContext restore_ctx
;
6714 tls
= (DebuggerTlsData
*)mono_native_tls_get_value (debugger_tls_id
);
6718 * Store the `InvokeData *' in `tls->invoke' until we're done with
6719 * the invocation, so CMD_VM_ABORT_INVOKE can check it.
6722 mono_loader_lock ();
6724 invoke
= tls
->pending_invoke
;
6726 tls
->pending_invoke
= NULL
;
6728 invoke
->last_invoke
= tls
->invoke
;
6729 tls
->invoke
= invoke
;
6731 mono_loader_unlock ();
6733 tls
->frames_up_to_date
= FALSE
;
6739 for (mindex
= 0; mindex
< invoke
->nmethods
; ++mindex
) {
6740 buffer_init (&buf
, 128);
6743 /* Fail the other invokes as well */
6745 err
= do_invoke_method (tls
, &buf
, invoke
, p
, &p
);
6748 if (tls
->abort_requested
) {
6749 if (CHECK_PROTOCOL_VERSION (2, 42))
6750 err
= ERR_INVOKE_ABORTED
;
6753 /* Start suspending before sending the reply */
6754 if (mindex
== invoke
->nmethods
- 1) {
6755 if (!(invoke
->flags
& INVOKE_FLAG_SINGLE_THREADED
)) {
6756 for (i
= 0; i
< invoke
->suspend_count
; ++i
)
6761 send_reply_packet (id
, err
, &buf
);
6766 memcpy (&restore_ctx
, &invoke
->ctx
, sizeof (MonoContext
));
6768 if (invoke
->has_ctx
)
6769 save_thread_context (&restore_ctx
);
6771 if (invoke
->flags
& INVOKE_FLAG_SINGLE_THREADED
) {
6772 g_assert (tls
->resume_count
);
6773 tls
->resume_count
-= invoke
->suspend_count
;
6776 DEBUG_PRINTF (1, "[%p] Invoke finished (%d), resume_count = %d.\n", (gpointer
) (gsize
) mono_native_thread_id_get (), err
, tls
->resume_count
);
6779 * Take the loader lock to avoid race conditions with CMD_VM_ABORT_INVOKE:
6781 * It is possible that mono_thread_internal_abort () was called
6782 * after the mono_runtime_invoke_checked() already returned, but it doesn't matter
6783 * because we reset the abort here.
6786 mono_loader_lock ();
6788 if (tls
->abort_requested
)
6789 mono_thread_internal_reset_abort (tls
->thread
);
6791 tls
->invoke
= tls
->invoke
->last_invoke
;
6792 tls
->abort_requested
= FALSE
;
6794 mono_loader_unlock ();
6801 is_really_suspended (gpointer key
, gpointer value
, gpointer user_data
)
6803 MonoThread
*thread
= (MonoThread
*)value
;
6804 DebuggerTlsData
*tls
;
6807 mono_loader_lock ();
6808 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
6810 res
= tls
->really_suspended
;
6811 mono_loader_unlock ();
6817 get_source_files_for_type (MonoClass
*klass
)
6819 gpointer iter
= NULL
;
6821 MonoDebugSourceInfo
*sinfo
;
6825 files
= g_ptr_array_new ();
6827 while ((method
= mono_class_get_methods (klass
, &iter
))) {
6828 MonoDebugMethodInfo
*minfo
= mono_debug_lookup_method (method
);
6829 GPtrArray
*source_file_list
;
6832 mono_debug_get_seq_points (minfo
, NULL
, &source_file_list
, NULL
, NULL
, NULL
);
6833 for (j
= 0; j
< source_file_list
->len
; ++j
) {
6834 sinfo
= (MonoDebugSourceInfo
*)g_ptr_array_index (source_file_list
, j
);
6835 for (i
= 0; i
< files
->len
; ++i
)
6836 if (!strcmp ((const char*)g_ptr_array_index (files
, i
), (const char*)sinfo
->source_file
))
6838 if (i
== files
->len
)
6839 g_ptr_array_add (files
, g_strdup (sinfo
->source_file
));
6841 g_ptr_array_free (source_file_list
, TRUE
);
6850 MonoTypeNameParse
*info
;
6851 gboolean ignore_case
;
6852 GPtrArray
*res_classes
;
6853 GPtrArray
*res_domains
;
6857 get_types (gpointer key
, gpointer value
, gpointer user_data
)
6860 gboolean type_resolve
;
6863 MonoDomain
*domain
= (MonoDomain
*)key
;
6865 if (mono_domain_is_unloading (domain
))
6868 MonoAssemblyLoadContext
*alc
= mono_domain_default_alc (domain
);
6869 GetTypesArgs
*ud
= (GetTypesArgs
*)user_data
;
6871 mono_domain_assemblies_lock (domain
);
6872 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
6873 ass
= (MonoAssembly
*)tmp
->data
;
6876 ERROR_DECL (probe_type_error
);
6877 /* FIXME really okay to call while holding locks? */
6878 t
= mono_reflection_get_type_checked (alc
, ass
->image
, ass
->image
, ud
->info
, ud
->ignore_case
, TRUE
, &type_resolve
, probe_type_error
);
6879 mono_error_cleanup (probe_type_error
);
6881 g_ptr_array_add (ud
->res_classes
, mono_type_get_class_internal (t
));
6882 g_ptr_array_add (ud
->res_domains
, domain
);
6886 mono_domain_assemblies_unlock (domain
);
6890 gboolean ignore_case
;
6892 GPtrArray
*res_classes
;
6893 GPtrArray
*res_domains
;
6894 } GetTypesForSourceFileArgs
;
6897 get_types_for_source_file (gpointer key
, gpointer value
, gpointer user_data
)
6899 GHashTableIter iter
;
6900 GSList
*class_list
= NULL
;
6901 MonoClass
*klass
= NULL
;
6902 GPtrArray
*files
= NULL
;
6904 GetTypesForSourceFileArgs
*ud
= (GetTypesForSourceFileArgs
*)user_data
;
6905 MonoDomain
*domain
= (MonoDomain
*)key
;
6907 if (mono_domain_is_unloading (domain
))
6910 AgentDomainInfo
*info
= (AgentDomainInfo
*)domain_jit_info (domain
)->agent_info
;
6912 /* Update 'source_file_to_class' cache */
6913 g_hash_table_iter_init (&iter
, info
->loaded_classes
);
6914 while (g_hash_table_iter_next (&iter
, NULL
, (void**)&klass
)) {
6915 if (!g_hash_table_lookup (info
->source_files
, klass
)) {
6916 files
= get_source_files_for_type (klass
);
6917 g_hash_table_insert (info
->source_files
, klass
, files
);
6919 for (int i
= 0; i
< files
->len
; ++i
) {
6920 char *s
= (char *)g_ptr_array_index (files
, i
);
6921 char *s2
= dbg_path_get_basename (s
);
6924 class_list
= (GSList
*)g_hash_table_lookup (info
->source_file_to_class
, s2
);
6926 class_list
= g_slist_prepend (class_list
, klass
);
6927 g_hash_table_insert (info
->source_file_to_class
, g_strdup (s2
), class_list
);
6929 class_list
= g_slist_prepend (class_list
, klass
);
6930 g_hash_table_insert (info
->source_file_to_class
, s2
, class_list
);
6933 /* The _ignorecase hash contains the lowercase path */
6934 s3
= strdup_tolower (s2
);
6935 class_list
= (GSList
*)g_hash_table_lookup (info
->source_file_to_class_ignorecase
, s3
);
6937 class_list
= g_slist_prepend (class_list
, klass
);
6938 g_hash_table_insert (info
->source_file_to_class_ignorecase
, g_strdup (s3
), class_list
);
6940 class_list
= g_slist_prepend (class_list
, klass
);
6941 g_hash_table_insert (info
->source_file_to_class_ignorecase
, s3
, class_list
);
6950 if (ud
->ignore_case
) {
6953 s
= strdup_tolower (ud
->basename
);
6954 class_list
= (GSList
*)g_hash_table_lookup (info
->source_file_to_class_ignorecase
, s
);
6957 class_list
= (GSList
*)g_hash_table_lookup (info
->source_file_to_class
, ud
->basename
);
6960 for (GSList
*l
= class_list
; l
; l
= l
->next
) {
6961 klass
= (MonoClass
*)l
->data
;
6963 g_ptr_array_add (ud
->res_classes
, klass
);
6964 g_ptr_array_add (ud
->res_domains
, domain
);
6969 vm_commands (int command
, int id
, guint8
*p
, guint8
*end
, Buffer
*buf
)
6972 case CMD_VM_VERSION
: {
6973 char *build_info
, *version
;
6975 build_info
= mono_get_runtime_build_info ();
6976 version
= g_strdup_printf ("mono %s", build_info
);
6978 buffer_add_string (buf
, version
); /* vm version */
6979 buffer_add_int (buf
, MAJOR_VERSION
);
6980 buffer_add_int (buf
, MINOR_VERSION
);
6981 g_free (build_info
);
6985 case CMD_VM_SET_PROTOCOL_VERSION
: {
6986 major_version
= decode_int (p
, &p
, end
);
6987 minor_version
= decode_int (p
, &p
, end
);
6988 protocol_version_set
= TRUE
;
6989 DEBUG_PRINTF (1, "[dbg] Protocol version %d.%d, client protocol version %d.%d.\n", MAJOR_VERSION
, MINOR_VERSION
, major_version
, minor_version
);
6992 case CMD_VM_ALL_THREADS
: {
6994 gboolean remove_gc_finalizing
= FALSE
;
6995 mono_loader_lock ();
6996 int count
= mono_g_hash_table_size (tid_to_thread_obj
);
6997 mono_g_hash_table_foreach (tid_to_thread_obj
, count_thread_check_gc_finalizer
, &remove_gc_finalizing
);
6998 if (remove_gc_finalizing
)
7000 buffer_add_int (buf
, count
);
7001 mono_g_hash_table_foreach (tid_to_thread_obj
, add_thread
, buf
);
7003 mono_loader_unlock ();
7006 case CMD_VM_SUSPEND
:
7008 wait_for_suspend ();
7011 if (suspend_count
== 0)
7012 return ERR_NOT_SUSPENDED
;
7014 clear_suspended_objs ();
7016 case CMD_VM_DISPOSE
:
7020 MonoInternalThread
*thread
;
7021 DebuggerTlsData
*tls
;
7022 #ifdef TRY_MANAGED_SYSTEM_ENVIRONMENT_EXIT
7023 MonoClass
*env_class
;
7025 MonoMethod
*exit_method
= NULL
;
7029 exit_code
= decode_int (p
, &p
, end
);
7031 // FIXME: What if there is a VM_DEATH event request with SUSPEND_ALL ?
7033 /* Have to send a reply before exiting */
7034 send_reply_packet (id
, 0, buf
);
7036 /* Clear all event requests */
7037 mono_loader_lock ();
7038 while (event_requests
->len
> 0) {
7039 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, 0);
7041 clear_event_request (req
->id
, req
->event_kind
);
7043 mono_loader_unlock ();
7046 * The JDWP documentation says that the shutdown is not orderly. It doesn't
7047 * specify whenever a VM_DEATH event is sent. We currently do an orderly
7048 * shutdown by hijacking a thread to execute Environment.Exit (). This is
7049 * better than doing the shutdown ourselves, since it avoids various races.
7053 wait_for_suspend ();
7055 #ifdef TRY_MANAGED_SYSTEM_ENVIRONMENT_EXIT
7056 env_class
= mono_class_try_load_from_name (mono_defaults
.corlib
, "System", "Environment");
7059 exit_method
= mono_class_get_method_from_name_checked (env_class
, "Exit", 1, 0, error
);
7060 mono_error_assert_ok (error
);
7064 mono_loader_lock ();
7065 thread
= (MonoInternalThread
*)mono_g_hash_table_find (tid_to_thread
, is_really_suspended
, NULL
);
7066 mono_loader_unlock ();
7068 if (thread
&& exit_method
) {
7069 mono_loader_lock ();
7070 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
7071 mono_loader_unlock ();
7073 args
= g_new0 (gpointer
, 1);
7074 args
[0] = g_malloc (sizeof (int));
7075 *(int*)(args
[0]) = exit_code
;
7077 tls
->pending_invoke
= g_new0 (InvokeData
, 1);
7078 tls
->pending_invoke
->method
= exit_method
;
7079 tls
->pending_invoke
->args
= args
;
7080 tls
->pending_invoke
->nmethods
= 1;
7082 while (suspend_count
> 0)
7086 * No thread found, do it ourselves.
7087 * FIXME: This can race with normal shutdown etc.
7089 while (suspend_count
> 0)
7092 if (!mono_runtime_try_shutdown ())
7095 mono_environment_exitcode_set (exit_code
);
7097 /* Suspend all managed threads since the runtime is going away */
7098 #ifndef ENABLE_NETCORE
7099 DEBUG_PRINTF (1, "Suspending all threads...\n");
7100 mono_thread_suspend_all_other_threads ();
7102 DEBUG_PRINTF (1, "Shutting down the runtime...\n");
7103 mono_runtime_quit_internal ();
7104 transport_close2 ();
7105 DEBUG_PRINTF (1, "Exiting...\n");
7111 case CMD_VM_INVOKE_METHOD
:
7112 case CMD_VM_INVOKE_METHODS
: {
7113 int objid
= decode_objid (p
, &p
, end
);
7115 DebuggerTlsData
*tls
;
7116 int i
, count
, flags
, nmethods
;
7119 err
= get_object (objid
, (MonoObject
**)&thread
);
7120 if (err
!= ERR_NONE
)
7123 flags
= decode_int (p
, &p
, end
);
7125 if (command
== CMD_VM_INVOKE_METHODS
)
7126 nmethods
= decode_int (p
, &p
, end
);
7130 // Wait for suspending if it already started
7132 wait_for_suspend ();
7133 if (!is_suspended ())
7134 return ERR_NOT_SUSPENDED
;
7136 mono_loader_lock ();
7137 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, THREAD_TO_INTERNAL (thread
));
7138 mono_loader_unlock ();
7141 if (!tls
->really_suspended
)
7142 /* The thread is still running native code, can't do invokes */
7143 return ERR_NOT_SUSPENDED
;
7146 * Store the invoke data into tls, the thread will execute it after it is
7149 if (tls
->pending_invoke
)
7150 return ERR_NOT_SUSPENDED
;
7151 tls
->pending_invoke
= g_new0 (InvokeData
, 1);
7152 tls
->pending_invoke
->id
= id
;
7153 tls
->pending_invoke
->flags
= flags
;
7154 tls
->pending_invoke
->p
= (guint8
*)g_malloc (end
- p
);
7155 memcpy (tls
->pending_invoke
->p
, p
, end
- p
);
7156 tls
->pending_invoke
->endp
= tls
->pending_invoke
->p
+ (end
- p
);
7157 tls
->pending_invoke
->suspend_count
= suspend_count
;
7158 tls
->pending_invoke
->nmethods
= nmethods
;
7160 if (flags
& INVOKE_FLAG_SINGLE_THREADED
) {
7161 resume_thread (THREAD_TO_INTERNAL (thread
));
7164 count
= suspend_count
;
7165 for (i
= 0; i
< count
; ++i
)
7170 case CMD_VM_ABORT_INVOKE
: {
7171 int objid
= decode_objid (p
, &p
, end
);
7173 DebuggerTlsData
*tls
;
7177 err
= get_object (objid
, (MonoObject
**)&thread
);
7178 if (err
!= ERR_NONE
)
7181 invoke_id
= decode_int (p
, &p
, end
);
7183 mono_loader_lock ();
7184 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, THREAD_TO_INTERNAL (thread
));
7187 if (tls
->abort_requested
) {
7188 DEBUG_PRINTF (1, "Abort already requested.\n");
7189 mono_loader_unlock ();
7194 * Check whether we're still inside the mono_runtime_invoke_checked() and that it's
7195 * actually the correct invocation.
7197 * Careful, we do not stop the thread that's doing the invocation, so we can't
7198 * inspect its stack. However, invoke_method() also acquires the loader lock
7199 * when it's done, so we're safe here.
7203 if (!tls
->invoke
|| (tls
->invoke
->id
!= invoke_id
)) {
7204 mono_loader_unlock ();
7205 return ERR_NO_INVOCATION
;
7208 tls
->abort_requested
= TRUE
;
7210 mono_thread_internal_abort (THREAD_TO_INTERNAL (thread
), FALSE
);
7211 mono_loader_unlock ();
7215 case CMD_VM_SET_KEEPALIVE
: {
7216 int timeout
= decode_int (p
, &p
, end
);
7217 agent_config
.keepalive
= timeout
;
7219 #ifndef DISABLE_SOCKET_TRANSPORT
7226 case CMD_VM_GET_TYPES_FOR_SOURCE_FILE
: {
7228 char *fname
, *basename
;
7229 gboolean ignore_case
;
7230 GPtrArray
*res_classes
, *res_domains
;
7232 fname
= decode_string (p
, &p
, end
);
7233 ignore_case
= decode_byte (p
, &p
, end
);
7235 basename
= dbg_path_get_basename (fname
);
7237 res_classes
= g_ptr_array_new ();
7238 res_domains
= g_ptr_array_new ();
7240 mono_loader_lock ();
7241 GetTypesForSourceFileArgs args
;
7242 memset (&args
, 0, sizeof (args
));
7243 args
.ignore_case
= ignore_case
;
7244 args
.basename
= basename
;
7245 args
.res_classes
= res_classes
;
7246 args
.res_domains
= res_domains
;
7247 mono_de_foreach_domain (get_types_for_source_file
, &args
);
7248 mono_loader_unlock ();
7253 buffer_add_int (buf
, res_classes
->len
);
7254 for (i
= 0; i
< res_classes
->len
; ++i
)
7255 buffer_add_typeid (buf
, (MonoDomain
*)g_ptr_array_index (res_domains
, i
), (MonoClass
*)g_ptr_array_index (res_classes
, i
));
7256 g_ptr_array_free (res_classes
, TRUE
);
7257 g_ptr_array_free (res_domains
, TRUE
);
7260 case CMD_VM_GET_TYPES
: {
7264 gboolean ignore_case
;
7265 GPtrArray
*res_classes
, *res_domains
;
7266 MonoTypeNameParse info
;
7268 name
= decode_string (p
, &p
, end
);
7269 ignore_case
= decode_byte (p
, &p
, end
);
7271 if (!mono_reflection_parse_type_checked (name
, &info
, error
)) {
7272 mono_error_cleanup (error
);
7274 mono_reflection_free_type_info (&info
);
7275 return ERR_INVALID_ARGUMENT
;
7278 res_classes
= g_ptr_array_new ();
7279 res_domains
= g_ptr_array_new ();
7281 mono_loader_lock ();
7284 memset (&args
, 0, sizeof (args
));
7286 args
.ignore_case
= ignore_case
;
7287 args
.res_classes
= res_classes
;
7288 args
.res_domains
= res_domains
;
7290 mono_de_foreach_domain (get_types
, &args
);
7292 mono_loader_unlock ();
7295 mono_reflection_free_type_info (&info
);
7297 buffer_add_int (buf
, res_classes
->len
);
7298 for (i
= 0; i
< res_classes
->len
; ++i
)
7299 buffer_add_typeid (buf
, (MonoDomain
*)g_ptr_array_index (res_domains
, i
), (MonoClass
*)g_ptr_array_index (res_classes
, i
));
7300 g_ptr_array_free (res_classes
, TRUE
);
7301 g_ptr_array_free (res_domains
, TRUE
);
7304 case CMD_VM_START_BUFFERING
:
7305 case CMD_VM_STOP_BUFFERING
:
7306 /* Handled in the main loop */
7309 return ERR_NOT_IMPLEMENTED
;
7316 event_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7322 case CMD_EVENT_REQUEST_SET
: {
7324 int i
, event_kind
, suspend_policy
, nmodifiers
;
7328 MonoThread
*step_thread
;
7329 int step_thread_id
= 0;
7330 StepDepth depth
= STEP_DEPTH_INTO
;
7331 StepSize size
= STEP_SIZE_MIN
;
7332 StepFilter filter
= STEP_FILTER_NONE
;
7336 event_kind
= decode_byte (p
, &p
, end
);
7337 suspend_policy
= decode_byte (p
, &p
, end
);
7338 nmodifiers
= decode_byte (p
, &p
, end
);
7340 req
= (EventRequest
*)g_malloc0 (sizeof (EventRequest
) + (nmodifiers
* sizeof (Modifier
)));
7341 req
->id
= mono_atomic_inc_i32 (&event_request_id
);
7342 req
->event_kind
= event_kind
;
7343 req
->suspend_policy
= suspend_policy
;
7344 req
->nmodifiers
= nmodifiers
;
7347 for (i
= 0; i
< nmodifiers
; ++i
) {
7348 mod
= (ModifierKind
)decode_byte (p
, &p
, end
);
7350 req
->modifiers
[i
].kind
= mod
;
7351 if (mod
== MOD_KIND_COUNT
) {
7352 req
->modifiers
[i
].data
.count
= decode_int (p
, &p
, end
);
7353 } else if (mod
== MOD_KIND_LOCATION_ONLY
) {
7354 method
= decode_methodid (p
, &p
, end
, &domain
, &err
);
7355 if (err
!= ERR_NONE
)
7357 location
= decode_long (p
, &p
, end
);
7358 } else if (mod
== MOD_KIND_STEP
) {
7359 step_thread_id
= decode_id (p
, &p
, end
);
7360 size
= (StepSize
)decode_int (p
, &p
, end
);
7361 depth
= (StepDepth
)decode_int (p
, &p
, end
);
7362 if (CHECK_PROTOCOL_VERSION (2, 16))
7363 filter
= (StepFilter
)decode_int (p
, &p
, end
);
7364 req
->modifiers
[i
].data
.filter
= filter
;
7365 if (!CHECK_PROTOCOL_VERSION (2, 26) && (req
->modifiers
[i
].data
.filter
& STEP_FILTER_DEBUGGER_HIDDEN
))
7366 /* Treat STEP_THOUGH the same as HIDDEN */
7367 req
->modifiers
[i
].data
.filter
= (StepFilter
)(req
->modifiers
[i
].data
.filter
| STEP_FILTER_DEBUGGER_STEP_THROUGH
);
7368 } else if (mod
== MOD_KIND_THREAD_ONLY
) {
7369 int id
= decode_id (p
, &p
, end
);
7371 err
= get_object (id
, (MonoObject
**)&req
->modifiers
[i
].data
.thread
);
7372 if (err
!= ERR_NONE
) {
7376 } else if (mod
== MOD_KIND_EXCEPTION_ONLY
) {
7377 MonoClass
*exc_class
= decode_typeid (p
, &p
, end
, &domain
, &err
);
7379 if (err
!= ERR_NONE
)
7381 req
->modifiers
[i
].caught
= decode_byte (p
, &p
, end
);
7382 req
->modifiers
[i
].uncaught
= decode_byte (p
, &p
, end
);
7383 if (CHECK_PROTOCOL_VERSION (2, 25))
7384 req
->modifiers
[i
].subclasses
= decode_byte (p
, &p
, end
);
7386 req
->modifiers
[i
].subclasses
= TRUE
;
7388 req
->modifiers
[i
].data
.exc_class
= exc_class
;
7390 if (!mono_class_is_assignable_from_internal (mono_defaults
.exception_class
, exc_class
)) {
7392 return ERR_INVALID_ARGUMENT
;
7395 if (CHECK_PROTOCOL_VERSION (2, 54)) {
7396 req
->modifiers
[i
].not_filtered_feature
= decode_byte (p
, &p
, end
);
7397 req
->modifiers
[i
].everything_else
= decode_byte (p
, &p
, end
);
7398 DEBUG_PRINTF (1, "[dbg] \tEXCEPTION_ONLY 2 filter (%s%s%s%s).\n", exc_class
? m_class_get_name (exc_class
) : (req
->modifiers
[i
].everything_else
? "everything else" : "all"), req
->modifiers
[i
].caught
? ", caught" : "", req
->modifiers
[i
].uncaught
? ", uncaught" : "", req
->modifiers
[i
].subclasses
? ", include-subclasses" : "");
7400 req
->modifiers
[i
].not_filtered_feature
= FALSE
;
7401 req
->modifiers
[i
].everything_else
= FALSE
;
7402 DEBUG_PRINTF (1, "[dbg] \tEXCEPTION_ONLY filter (%s%s%s%s).\n", exc_class
? m_class_get_name (exc_class
) : "all", req
->modifiers
[i
].caught
? ", caught" : "", req
->modifiers
[i
].uncaught
? ", uncaught" : "", req
->modifiers
[i
].subclasses
? ", include-subclasses" : "");
7405 } else if (mod
== MOD_KIND_ASSEMBLY_ONLY
) {
7406 int n
= decode_int (p
, &p
, end
);
7409 // +1 because we don't know length and we use last element to check for end
7410 req
->modifiers
[i
].data
.assemblies
= g_new0 (MonoAssembly
*, n
+ 1);
7411 for (j
= 0; j
< n
; ++j
) {
7412 req
->modifiers
[i
].data
.assemblies
[j
] = decode_assemblyid (p
, &p
, end
, &domain
, &err
);
7413 if (err
!= ERR_NONE
) {
7414 g_free (req
->modifiers
[i
].data
.assemblies
);
7418 } else if (mod
== MOD_KIND_SOURCE_FILE_ONLY
) {
7419 int n
= decode_int (p
, &p
, end
);
7422 modifier
= &req
->modifiers
[i
];
7423 modifier
->data
.source_files
= g_hash_table_new (g_str_hash
, g_str_equal
);
7424 for (j
= 0; j
< n
; ++j
) {
7425 char *s
= decode_string (p
, &p
, end
);
7429 s2
= strdup_tolower (s
);
7430 g_hash_table_insert (modifier
->data
.source_files
, s2
, s2
);
7434 } else if (mod
== MOD_KIND_TYPE_NAME_ONLY
) {
7435 int n
= decode_int (p
, &p
, end
);
7438 modifier
= &req
->modifiers
[i
];
7439 modifier
->data
.type_names
= g_hash_table_new (g_str_hash
, g_str_equal
);
7440 for (j
= 0; j
< n
; ++j
) {
7441 char *s
= decode_string (p
, &p
, end
);
7444 g_hash_table_insert (modifier
->data
.type_names
, s
, s
);
7448 return ERR_NOT_IMPLEMENTED
;
7452 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
) {
7455 req
->info
= mono_de_set_breakpoint (method
, location
, req
, error
);
7456 if (!is_ok (error
)) {
7458 DEBUG_PRINTF (1, "[dbg] Failed to set breakpoint: %s\n", mono_error_get_message (error
));
7459 mono_error_cleanup (error
);
7460 return ERR_NO_SEQ_POINT_AT_IL_OFFSET
;
7462 } else if (req
->event_kind
== EVENT_KIND_STEP
) {
7463 g_assert (step_thread_id
);
7465 err
= get_object (step_thread_id
, (MonoObject
**)&step_thread
);
7466 if (err
!= ERR_NONE
) {
7471 mono_loader_lock ();
7472 DebuggerTlsData
*tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, THREAD_TO_INTERNAL(step_thread
));
7473 mono_loader_unlock ();
7476 if (tls
->terminated
) {
7477 /* if the thread is already terminated ignore the single step */
7478 buffer_add_int (buf
, req
->id
);
7482 err
= (ErrorCode
)mono_de_ss_create (THREAD_TO_INTERNAL (step_thread
), size
, depth
, filter
, req
);
7483 if (err
!= ERR_NONE
) {
7487 } else if (req
->event_kind
== EVENT_KIND_METHOD_ENTRY
) {
7488 req
->info
= mono_de_set_breakpoint (NULL
, METHOD_ENTRY_IL_OFFSET
, req
, NULL
);
7489 } else if (req
->event_kind
== EVENT_KIND_METHOD_EXIT
) {
7490 req
->info
= mono_de_set_breakpoint (NULL
, METHOD_EXIT_IL_OFFSET
, req
, NULL
);
7491 } else if (req
->event_kind
== EVENT_KIND_EXCEPTION
) {
7492 } else if (req
->event_kind
== EVENT_KIND_TYPE_LOAD
) {
7494 if (req
->nmodifiers
) {
7496 return ERR_NOT_IMPLEMENTED
;
7500 mono_loader_lock ();
7501 g_ptr_array_add (event_requests
, req
);
7503 if (agent_config
.defer
) {
7504 /* Transmit cached data to the client on receipt of the event request */
7505 switch (req
->event_kind
) {
7506 case EVENT_KIND_APPDOMAIN_CREATE
:
7507 /* Emit load events for currently loaded domains */
7508 mono_de_foreach_domain (emit_appdomain_load
, NULL
);
7510 case EVENT_KIND_ASSEMBLY_LOAD
:
7511 /* Emit load events for currently loaded assemblies */
7512 mono_domain_foreach (send_assemblies_for_domain
, NULL
);
7514 case EVENT_KIND_THREAD_START
:
7515 /* Emit start events for currently started threads */
7516 mono_g_hash_table_foreach (tid_to_thread
, emit_thread_start
, NULL
);
7518 case EVENT_KIND_TYPE_LOAD
:
7519 /* Emit type load events for currently loaded types */
7520 mono_domain_foreach (send_types_for_domain
, NULL
);
7526 mono_loader_unlock ();
7528 buffer_add_int (buf
, req
->id
);
7531 case CMD_EVENT_REQUEST_CLEAR
: {
7532 int etype
= decode_byte (p
, &p
, end
);
7533 int req_id
= decode_int (p
, &p
, end
);
7535 // FIXME: Make a faster mapping from req_id to request
7536 mono_loader_lock ();
7537 clear_event_request (req_id
, etype
);
7538 mono_loader_unlock ();
7541 case CMD_EVENT_REQUEST_CLEAR_ALL_BREAKPOINTS
: {
7544 mono_loader_lock ();
7546 while (i
< event_requests
->len
) {
7547 EventRequest
*req
= (EventRequest
*)g_ptr_array_index (event_requests
, i
);
7549 if (req
->event_kind
== EVENT_KIND_BREAKPOINT
) {
7550 mono_de_clear_breakpoint ((MonoBreakpoint
*)req
->info
);
7552 g_ptr_array_remove_index_fast (event_requests
, i
);
7558 mono_loader_unlock ();
7562 return ERR_NOT_IMPLEMENTED
;
7569 domain_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7575 case CMD_APPDOMAIN_GET_ROOT_DOMAIN
: {
7576 buffer_add_domainid (buf
, mono_get_root_domain ());
7579 case CMD_APPDOMAIN_GET_FRIENDLY_NAME
: {
7580 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7581 if (err
!= ERR_NONE
)
7583 buffer_add_string (buf
, domain
->friendly_name
);
7586 case CMD_APPDOMAIN_GET_ASSEMBLIES
: {
7591 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7592 if (err
!= ERR_NONE
)
7594 mono_domain_assemblies_lock (domain
);
7596 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
7599 buffer_add_int (buf
, count
);
7600 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
7601 ass
= (MonoAssembly
*)tmp
->data
;
7602 buffer_add_assemblyid (buf
, domain
, ass
);
7604 mono_domain_assemblies_unlock (domain
);
7607 case CMD_APPDOMAIN_GET_ENTRY_ASSEMBLY
: {
7608 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7609 if (err
!= ERR_NONE
)
7612 buffer_add_assemblyid (buf
, domain
, domain
->entry_assembly
);
7615 case CMD_APPDOMAIN_GET_CORLIB
: {
7616 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7617 if (err
!= ERR_NONE
)
7620 buffer_add_assemblyid (buf
, domain
, m_class_get_image (domain
->domain
->mbr
.obj
.vtable
->klass
)->assembly
);
7623 case CMD_APPDOMAIN_CREATE_STRING
: {
7628 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7629 if (err
!= ERR_NONE
)
7631 s
= decode_string (p
, &p
, end
);
7633 o
= mono_string_new_checked (domain
, s
, error
);
7634 if (!is_ok (error
)) {
7635 DEBUG_PRINTF (1, "[dbg] Failed to allocate String object '%s': %s\n", s
, mono_error_get_message (error
));
7636 mono_error_cleanup (error
);
7637 return ERR_INVALID_OBJECT
;
7639 buffer_add_objid (buf
, (MonoObject
*)o
);
7642 case CMD_APPDOMAIN_CREATE_BYTE_ARRAY
: {
7646 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7648 int len
= decode_int (p
, &p
, end
);
7650 arr
= mono_array_new_full_checked (mono_domain_get (), mono_class_create_array (mono_get_byte_class(), 1), &size
, NULL
, error
);
7651 elem
= mono_array_addr_internal (arr
, guint8
, 0);
7652 memcpy (elem
, p
, len
);
7654 buffer_add_objid (buf
, (MonoObject
*) arr
);
7657 case CMD_APPDOMAIN_CREATE_BOXED_VALUE
: {
7660 MonoDomain
*domain2
;
7663 domain
= decode_domainid (p
, &p
, end
, NULL
, &err
);
7664 if (err
!= ERR_NONE
)
7666 klass
= decode_typeid (p
, &p
, end
, &domain2
, &err
);
7667 if (err
!= ERR_NONE
)
7671 g_assert (domain
== domain2
);
7673 o
= mono_object_new_checked (domain
, klass
, error
);
7674 mono_error_assert_ok (error
);
7676 err
= decode_value (m_class_get_byval_arg (klass
), domain
, (guint8
*)mono_object_unbox_internal (o
), p
, &p
, end
, TRUE
);
7677 if (err
!= ERR_NONE
)
7680 buffer_add_objid (buf
, o
);
7684 return ERR_NOT_IMPLEMENTED
;
7691 get_assembly_object_command (MonoDomain
*domain
, MonoAssembly
*ass
, Buffer
*buf
, MonoError
*error
)
7693 HANDLE_FUNCTION_ENTER();
7694 ErrorCode err
= ERR_NONE
;
7696 MonoReflectionAssemblyHandle o
= mono_assembly_get_object_handle (domain
, ass
, error
);
7697 if (MONO_HANDLE_IS_NULL (o
)) {
7698 err
= ERR_INVALID_OBJECT
;
7701 buffer_add_objid (buf
, MONO_HANDLE_RAW (MONO_HANDLE_CAST (MonoObject
, o
)));
7703 HANDLE_FUNCTION_RETURN_VAL (err
);
7708 assembly_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7714 ass
= decode_assemblyid (p
, &p
, end
, &domain
, &err
);
7715 if (err
!= ERR_NONE
)
7719 case CMD_ASSEMBLY_GET_LOCATION
: {
7720 buffer_add_string (buf
, mono_image_get_filename (ass
->image
));
7723 case CMD_ASSEMBLY_GET_ENTRY_POINT
: {
7727 if (ass
->image
->dynamic
) {
7728 buffer_add_id (buf
, 0);
7730 token
= mono_image_get_entry_point (ass
->image
);
7732 buffer_add_id (buf
, 0);
7735 m
= mono_get_method_checked (ass
->image
, token
, NULL
, NULL
, error
);
7737 mono_error_cleanup (error
); /* FIXME don't swallow the error */
7738 buffer_add_methodid (buf
, domain
, m
);
7743 case CMD_ASSEMBLY_GET_MANIFEST_MODULE
: {
7744 buffer_add_moduleid (buf
, domain
, ass
->image
);
7747 case CMD_ASSEMBLY_GET_OBJECT
: {
7749 err
= get_assembly_object_command (domain
, ass
, buf
, error
);
7750 mono_error_cleanup (error
);
7753 case CMD_ASSEMBLY_GET_DOMAIN
: {
7754 buffer_add_domainid (buf
, domain
);
7757 case CMD_ASSEMBLY_GET_TYPE
: {
7759 char *s
= decode_string (p
, &p
, end
);
7760 gboolean ignorecase
= decode_byte (p
, &p
, end
);
7761 MonoTypeNameParse info
;
7763 gboolean type_resolve
, res
;
7764 MonoDomain
*d
= mono_domain_get ();
7765 MonoAssemblyLoadContext
*alc
= mono_domain_default_alc (d
);
7767 /* This is needed to be able to find referenced assemblies */
7768 res
= mono_domain_set_fast (domain
, FALSE
);
7771 if (!mono_reflection_parse_type_checked (s
, &info
, error
)) {
7772 mono_error_cleanup (error
);
7775 if (info
.assembly
.name
)
7777 t
= mono_reflection_get_type_checked (alc
, ass
->image
, ass
->image
, &info
, ignorecase
, TRUE
, &type_resolve
, error
);
7778 if (!is_ok (error
)) {
7779 mono_error_cleanup (error
); /* FIXME don't swallow the error */
7780 mono_reflection_free_type_info (&info
);
7782 return ERR_INVALID_ARGUMENT
;
7785 buffer_add_typeid (buf
, domain
, t
? mono_class_from_mono_type_internal (t
) : NULL
);
7786 mono_reflection_free_type_info (&info
);
7789 mono_domain_set_fast (d
, TRUE
);
7793 case CMD_ASSEMBLY_GET_NAME
: {
7795 MonoAssembly
*mass
= ass
;
7797 name
= g_strdup_printf (
7798 "%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
7800 mass
->aname
.major
, mass
->aname
.minor
, mass
->aname
.build
, mass
->aname
.revision
,
7801 mass
->aname
.culture
&& *mass
->aname
.culture
? mass
->aname
.culture
: "neutral",
7802 mass
->aname
.public_key_token
[0] ? (char *)mass
->aname
.public_key_token
: "null",
7803 (mass
->aname
.flags
& ASSEMBLYREF_RETARGETABLE_FLAG
) ? ", Retargetable=Yes" : "");
7805 buffer_add_string (buf
, name
);
7809 case CMD_ASSEMBLY_GET_METADATA_BLOB
: {
7810 MonoImage
* image
= ass
->image
;
7812 return ERR_NOT_IMPLEMENTED
;
7814 buffer_add_byte_array (buf
, (guint8
*)image
->raw_data
, image
->raw_data_len
);
7817 case CMD_ASSEMBLY_GET_IS_DYNAMIC
: {
7818 buffer_add_byte (buf
, ass
->dynamic
);
7821 case CMD_ASSEMBLY_GET_PDB_BLOB
: {
7822 MonoImage
* image
= ass
->image
;
7823 MonoDebugHandle
* handle
= mono_debug_get_handle (image
);
7825 return ERR_INVALID_ARGUMENT
;
7827 MonoPPDBFile
* ppdb
= handle
->ppdb
;
7829 image
= mono_ppdb_get_image (ppdb
);
7830 buffer_add_byte_array (buf
, (guint8
*)image
->raw_data
, image
->raw_data_len
);
7832 buffer_add_byte_array (buf
, NULL
, 0);
7836 case CMD_ASSEMBLY_GET_TYPE_FROM_TOKEN
: {
7838 return ERR_NOT_IMPLEMENTED
;
7840 guint32 token
= decode_int (p
, &p
, end
);
7843 MonoClass
* mono_class
= mono_class_get_checked (ass
->image
, token
, error
);
7844 if (!is_ok (error
)) {
7845 mono_error_cleanup (error
);
7846 return ERR_INVALID_ARGUMENT
;
7848 buffer_add_typeid (buf
, domain
, mono_class
);
7849 mono_error_cleanup (error
);
7852 case CMD_ASSEMBLY_GET_METHOD_FROM_TOKEN
: {
7854 return ERR_NOT_IMPLEMENTED
;
7856 guint32 token
= decode_int (p
, &p
, end
);
7859 MonoMethod
* mono_method
= mono_get_method_checked (ass
->image
, token
, NULL
, NULL
, error
);
7860 if (!is_ok (error
)) {
7861 mono_error_cleanup (error
);
7862 return ERR_INVALID_ARGUMENT
;
7864 buffer_add_methodid (buf
, domain
, mono_method
);
7865 mono_error_cleanup (error
);
7868 case CMD_ASSEMBLY_HAS_DEBUG_INFO
: {
7869 buffer_add_byte (buf
, !ass
->dynamic
&& mono_debug_image_has_debug_info (ass
->image
));
7873 return ERR_NOT_IMPLEMENTED
;
7880 module_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7886 case CMD_MODULE_GET_INFO
: {
7887 MonoImage
*image
= decode_moduleid (p
, &p
, end
, &domain
, &err
);
7888 char *basename
, *sourcelink
= NULL
;
7890 if (CHECK_PROTOCOL_VERSION (2, 48))
7891 sourcelink
= mono_debug_image_get_sourcelink (image
);
7893 basename
= g_path_get_basename (image
->name
);
7894 buffer_add_string (buf
, basename
); // name
7895 buffer_add_string (buf
, image
->module_name
); // scopename
7896 buffer_add_string (buf
, image
->name
); // fqname
7897 buffer_add_string (buf
, mono_image_get_guid (image
)); // guid
7898 buffer_add_assemblyid (buf
, domain
, image
->assembly
); // assembly
7899 if (CHECK_PROTOCOL_VERSION (2, 48))
7900 buffer_add_string (buf
, sourcelink
);
7902 g_free (sourcelink
);
7906 return ERR_NOT_IMPLEMENTED
;
7913 field_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
7919 case CMD_FIELD_GET_INFO
: {
7920 MonoClassField
*f
= decode_fieldid (p
, &p
, end
, &domain
, &err
);
7922 buffer_add_string (buf
, f
->name
);
7923 buffer_add_typeid (buf
, domain
, f
->parent
);
7924 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (f
->type
));
7925 buffer_add_int (buf
, f
->type
->attrs
);
7929 return ERR_NOT_IMPLEMENTED
;
7936 buffer_add_cattr_arg (Buffer
*buf
, MonoType
*t
, MonoDomain
*domain
, MonoObject
*val
)
7938 if (val
&& val
->vtable
->klass
== mono_defaults
.runtimetype_class
) {
7939 /* Special case these so the client doesn't have to handle Type objects */
7941 buffer_add_byte (buf
, VALUE_TYPE_ID_TYPE
);
7942 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (((MonoReflectionType
*)val
)->type
));
7943 } else if (MONO_TYPE_IS_REFERENCE (t
))
7944 buffer_add_value (buf
, t
, &val
, domain
);
7946 buffer_add_value (buf
, t
, mono_object_unbox_internal (val
), domain
);
7950 buffer_add_cattrs (Buffer
*buf
, MonoDomain
*domain
, MonoImage
*image
, MonoClass
*attr_klass
, MonoCustomAttrInfo
*cinfo
)
7956 buffer_add_int (buf
, 0);
7960 SETUP_ICALL_FUNCTION
;
7962 for (i
= 0; i
< cinfo
->num_attrs
; ++i
) {
7963 if (!attr_klass
|| mono_class_has_parent (cinfo
->attrs
[i
].ctor
->klass
, attr_klass
))
7966 buffer_add_int (buf
, nattrs
);
7968 for (i
= 0; i
< cinfo
->num_attrs
; ++i
) {
7969 MonoCustomAttrEntry
*attr
= &cinfo
->attrs
[i
];
7970 if (!attr_klass
|| mono_class_has_parent (attr
->ctor
->klass
, attr_klass
)) {
7971 MonoArray
*typed_args
, *named_args
;
7972 MonoArrayHandleOut typed_args_h
, named_args_h
;
7973 MonoObjectHandle val_h
;
7975 CattrNamedArg
*arginfo
= NULL
;
7979 typed_args_h
= MONO_HANDLE_NEW (MonoArray
, NULL
);
7980 named_args_h
= MONO_HANDLE_NEW (MonoArray
, NULL
);
7981 val_h
= MONO_HANDLE_NEW (MonoObject
, NULL
);
7983 mono_reflection_create_custom_attr_data_args (image
, attr
->ctor
, attr
->data
, attr
->data_size
, typed_args_h
, named_args_h
, &arginfo
, error
);
7984 if (!is_ok (error
)) {
7985 DEBUG_PRINTF (2, "[dbg] mono_reflection_create_custom_attr_data_args () failed with: '%s'\n", mono_error_get_message (error
));
7986 mono_error_cleanup (error
);
7988 return ERR_LOADER_ERROR
;
7990 typed_args
= MONO_HANDLE_RAW (typed_args_h
);
7991 named_args
= MONO_HANDLE_RAW (named_args_h
);
7993 buffer_add_methodid (buf
, domain
, attr
->ctor
);
7997 buffer_add_int (buf
, mono_array_length_internal (typed_args
));
7998 for (j
= 0; j
< mono_array_length_internal (typed_args
); ++j
) {
7999 MonoObject
*val
= mono_array_get_internal (typed_args
, MonoObject
*, j
);
8000 MONO_HANDLE_ASSIGN_RAW (val_h
, val
);
8002 t
= mono_method_signature_internal (attr
->ctor
)->params
[j
];
8004 buffer_add_cattr_arg (buf
, t
, domain
, val
);
8007 buffer_add_int (buf
, 0);
8012 buffer_add_int (buf
, mono_array_length_internal (named_args
));
8014 for (j
= 0; j
< mono_array_length_internal (named_args
); ++j
) {
8015 MonoObject
*val
= mono_array_get_internal (named_args
, MonoObject
*, j
);
8016 MONO_HANDLE_ASSIGN_RAW (val_h
, val
);
8018 if (arginfo
[j
].prop
) {
8019 buffer_add_byte (buf
, 0x54);
8020 buffer_add_propertyid (buf
, domain
, arginfo
[j
].prop
);
8021 } else if (arginfo
[j
].field
) {
8022 buffer_add_byte (buf
, 0x53);
8023 buffer_add_fieldid (buf
, domain
, arginfo
[j
].field
);
8025 g_assert_not_reached ();
8028 buffer_add_cattr_arg (buf
, arginfo
[j
].type
, domain
, val
);
8031 buffer_add_int (buf
, 0);
8042 /* FIXME: Code duplication with icall.c */
8044 collect_interfaces (MonoClass
*klass
, GHashTable
*ifaces
, MonoError
*error
)
8049 mono_class_setup_interfaces (klass
, error
);
8053 int klass_interface_count
= m_class_get_interface_count (klass
);
8054 MonoClass
**klass_interfaces
= m_class_get_interfaces (klass
);
8055 for (i
= 0; i
< klass_interface_count
; i
++) {
8056 ic
= klass_interfaces
[i
];
8057 g_hash_table_insert (ifaces
, ic
, ic
);
8059 collect_interfaces (ic
, ifaces
, error
);
8066 type_commands_internal (int command
, MonoClass
*klass
, MonoDomain
*domain
, guint8
*p
, guint8
*end
, Buffer
*buf
)
8068 HANDLE_FUNCTION_ENTER ();
8078 MonoStringHandle string_handle
= MONO_HANDLE_NEW (MonoString
, NULL
); // FIXME? Not always needed.
8081 case CMD_TYPE_GET_INFO
: {
8082 buffer_add_string (buf
, m_class_get_name_space (klass
));
8083 buffer_add_string (buf
, m_class_get_name (klass
));
8085 name
= mono_type_get_name_full (m_class_get_byval_arg (klass
), MONO_TYPE_NAME_FORMAT_FULL_NAME
);
8086 buffer_add_string (buf
, name
);
8088 buffer_add_assemblyid (buf
, domain
, m_class_get_image (klass
)->assembly
);
8089 buffer_add_moduleid (buf
, domain
, m_class_get_image (klass
));
8090 buffer_add_typeid (buf
, domain
, m_class_get_parent (klass
));
8091 if (m_class_get_rank (klass
) || m_class_get_byval_arg (klass
)->type
== MONO_TYPE_PTR
)
8092 buffer_add_typeid (buf
, domain
, m_class_get_element_class (klass
));
8094 buffer_add_id (buf
, 0);
8095 buffer_add_int (buf
, m_class_get_type_token (klass
));
8096 buffer_add_byte (buf
, m_class_get_rank (klass
));
8097 buffer_add_int (buf
, mono_class_get_flags (klass
));
8099 type
= m_class_get_byval_arg (klass
);
8100 // FIXME: Can't decide whenever a class represents a byref type
8103 if (type
->type
== MONO_TYPE_PTR
)
8105 if (!type
->byref
&& (((type
->type
>= MONO_TYPE_BOOLEAN
) && (type
->type
<= MONO_TYPE_R8
)) || (type
->type
== MONO_TYPE_I
) || (type
->type
== MONO_TYPE_U
)))
8107 if (type
->type
== MONO_TYPE_VALUETYPE
)
8109 if (m_class_is_enumtype (klass
))
8111 if (mono_class_is_gtd (klass
))
8113 if (mono_class_is_gtd (klass
) || mono_class_is_ginst (klass
))
8115 buffer_add_byte (buf
, b
);
8118 while ((nested
= mono_class_get_nested_types (klass
, &iter
)))
8120 buffer_add_int (buf
, nnested
);
8122 while ((nested
= mono_class_get_nested_types (klass
, &iter
)))
8123 buffer_add_typeid (buf
, domain
, nested
);
8124 if (CHECK_PROTOCOL_VERSION (2, 12)) {
8125 if (mono_class_is_gtd (klass
))
8126 buffer_add_typeid (buf
, domain
, klass
);
8127 else if (mono_class_is_ginst (klass
))
8128 buffer_add_typeid (buf
, domain
, mono_class_get_generic_class (klass
)->container_class
);
8130 buffer_add_id (buf
, 0);
8132 if (CHECK_PROTOCOL_VERSION (2, 15)) {
8135 if (mono_class_is_ginst (klass
)) {
8136 MonoGenericInst
*inst
= mono_class_get_generic_class (klass
)->context
.class_inst
;
8138 count
= inst
->type_argc
;
8139 buffer_add_int (buf
, count
);
8140 for (i
= 0; i
< count
; i
++)
8141 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (inst
->type_argv
[i
]));
8142 } else if (mono_class_is_gtd (klass
)) {
8143 MonoGenericContainer
*container
= mono_class_get_generic_container (klass
);
8146 count
= container
->type_argc
;
8147 buffer_add_int (buf
, count
);
8148 for (i
= 0; i
< count
; i
++) {
8149 pklass
= mono_class_create_generic_parameter (mono_generic_container_get_param (container
, i
));
8150 buffer_add_typeid (buf
, domain
, pklass
);
8153 buffer_add_int (buf
, 0);
8158 case CMD_TYPE_GET_METHODS
: {
8161 gpointer iter
= NULL
;
8164 mono_class_setup_methods (klass
);
8166 nmethods
= mono_class_num_methods (klass
);
8168 buffer_add_int (buf
, nmethods
);
8170 while ((m
= mono_class_get_methods (klass
, &iter
))) {
8171 buffer_add_methodid (buf
, domain
, m
);
8174 g_assert (i
== nmethods
);
8177 case CMD_TYPE_GET_FIELDS
: {
8180 gpointer iter
= NULL
;
8183 nfields
= mono_class_num_fields (klass
);
8185 buffer_add_int (buf
, nfields
);
8187 while ((f
= mono_class_get_fields_internal (klass
, &iter
))) {
8188 buffer_add_fieldid (buf
, domain
, f
);
8189 buffer_add_string (buf
, f
->name
);
8190 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (f
->type
));
8191 buffer_add_int (buf
, f
->type
->attrs
);
8194 g_assert (i
== nfields
);
8197 case CMD_TYPE_GET_PROPERTIES
: {
8200 gpointer iter
= NULL
;
8203 nprops
= mono_class_num_properties (klass
);
8205 buffer_add_int (buf
, nprops
);
8207 while ((p
= mono_class_get_properties (klass
, &iter
))) {
8208 buffer_add_propertyid (buf
, domain
, p
);
8209 buffer_add_string (buf
, p
->name
);
8210 buffer_add_methodid (buf
, domain
, p
->get
);
8211 buffer_add_methodid (buf
, domain
, p
->set
);
8212 buffer_add_int (buf
, p
->attrs
);
8215 g_assert (i
== nprops
);
8218 case CMD_TYPE_GET_CATTRS
: {
8219 MonoClass
*attr_klass
;
8220 MonoCustomAttrInfo
*cinfo
;
8222 attr_klass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8223 /* attr_klass can be NULL */
8224 if (err
!= ERR_NONE
)
8227 cinfo
= mono_custom_attrs_from_class_checked (klass
, error
);
8228 if (!is_ok (error
)) {
8229 mono_error_cleanup (error
); /* FIXME don't swallow the error message */
8233 err
= buffer_add_cattrs (buf
, domain
, m_class_get_image (klass
), attr_klass
, cinfo
);
8234 if (err
!= ERR_NONE
)
8238 case CMD_TYPE_GET_FIELD_CATTRS
: {
8239 MonoClass
*attr_klass
;
8240 MonoCustomAttrInfo
*cinfo
;
8241 MonoClassField
*field
;
8243 field
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
8244 if (err
!= ERR_NONE
)
8246 attr_klass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8247 if (err
!= ERR_NONE
)
8250 cinfo
= mono_custom_attrs_from_field_checked (klass
, field
, error
);
8251 if (!is_ok (error
)) {
8252 mono_error_cleanup (error
); /* FIXME don't swallow the error message */
8256 err
= buffer_add_cattrs (buf
, domain
, m_class_get_image (klass
), attr_klass
, cinfo
);
8257 if (err
!= ERR_NONE
)
8261 case CMD_TYPE_GET_PROPERTY_CATTRS
: {
8262 MonoClass
*attr_klass
;
8263 MonoCustomAttrInfo
*cinfo
;
8266 prop
= decode_propertyid (p
, &p
, end
, NULL
, &err
);
8267 if (err
!= ERR_NONE
)
8269 attr_klass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8270 if (err
!= ERR_NONE
)
8273 cinfo
= mono_custom_attrs_from_property_checked (klass
, prop
, error
);
8274 if (!is_ok (error
)) {
8275 mono_error_cleanup (error
); /* FIXME don't swallow the error message */
8279 err
= buffer_add_cattrs (buf
, domain
, m_class_get_image (klass
), attr_klass
, cinfo
);
8280 if (err
!= ERR_NONE
)
8284 case CMD_TYPE_GET_VALUES
:
8285 case CMD_TYPE_GET_VALUES_2
: {
8292 MonoThread
*thread_obj
;
8293 MonoInternalThread
*thread
= NULL
;
8294 guint32 special_static_type
;
8296 if (command
== CMD_TYPE_GET_VALUES_2
) {
8297 int objid
= decode_objid (p
, &p
, end
);
8299 err
= get_object (objid
, (MonoObject
**)&thread_obj
);
8300 if (err
!= ERR_NONE
)
8303 thread
= THREAD_TO_INTERNAL (thread_obj
);
8306 len
= decode_int (p
, &p
, end
);
8307 for (i
= 0; i
< len
; ++i
) {
8308 f
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
8309 if (err
!= ERR_NONE
)
8312 if (!(f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
8313 goto invalid_fieldid
;
8315 special_static_type
= mono_class_field_get_special_static_type (f
);
8316 if (special_static_type
!= SPECIAL_STATIC_NONE
) {
8317 if (!(thread
&& special_static_type
== SPECIAL_STATIC_THREAD
))
8318 goto invalid_fieldid
;
8321 /* Check that the field belongs to the object */
8323 for (k
= klass
; k
; k
= m_class_get_parent (k
)) {
8324 if (k
== f
->parent
) {
8330 goto invalid_fieldid
;
8332 vtable
= mono_class_vtable_checked (domain
, f
->parent
, error
);
8333 goto_if_nok (error
, invalid_fieldid
);
8335 val
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type_internal (f
->type
)));
8336 mono_field_static_get_value_for_thread (thread
? thread
: mono_thread_internal_current (), vtable
, f
, val
, string_handle
, error
);
8337 goto_if_nok (error
, invalid_fieldid
);
8339 buffer_add_value (buf
, f
->type
, val
, domain
);
8344 case CMD_TYPE_SET_VALUES
: {
8352 len
= decode_int (p
, &p
, end
);
8353 for (i
= 0; i
< len
; ++i
) {
8354 f
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
8355 if (err
!= ERR_NONE
)
8358 if (!(f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
8359 goto invalid_fieldid
;
8361 if (mono_class_field_is_special_static (f
))
8362 goto invalid_fieldid
;
8364 /* Check that the field belongs to the object */
8366 for (k
= klass
; k
; k
= m_class_get_parent (k
)) {
8367 if (k
== f
->parent
) {
8373 goto invalid_fieldid
;
8375 // FIXME: Check for literal/const
8377 vtable
= mono_class_vtable_checked (domain
, f
->parent
, error
);
8378 goto_if_nok (error
, invalid_fieldid
);
8380 val
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type_internal (f
->type
)));
8381 err
= decode_value (f
->type
, domain
, val
, p
, &p
, end
, TRUE
);
8382 if (err
!= ERR_NONE
) {
8386 if (MONO_TYPE_IS_REFERENCE (f
->type
))
8387 mono_field_static_set_value_internal (vtable
, f
, *(gpointer
*)val
);
8389 mono_field_static_set_value_internal (vtable
, f
, val
);
8394 case CMD_TYPE_GET_OBJECT
: {
8395 MonoObject
*o
= (MonoObject
*)mono_type_get_object_checked (domain
, m_class_get_byval_arg (klass
), error
);
8396 if (!is_ok (error
)) {
8397 mono_error_cleanup (error
);
8398 goto invalid_object
;
8400 buffer_add_objid (buf
, o
);
8403 case CMD_TYPE_GET_SOURCE_FILES
:
8404 case CMD_TYPE_GET_SOURCE_FILES_2
: {
8405 char *source_file
, *base
;
8409 files
= get_source_files_for_type (klass
);
8411 buffer_add_int (buf
, files
->len
);
8412 for (i
= 0; i
< files
->len
; ++i
) {
8413 source_file
= (char *)g_ptr_array_index (files
, i
);
8414 if (command
== CMD_TYPE_GET_SOURCE_FILES_2
) {
8415 buffer_add_string (buf
, source_file
);
8417 base
= dbg_path_get_basename (source_file
);
8418 buffer_add_string (buf
, base
);
8421 g_free (source_file
);
8423 g_ptr_array_free (files
, TRUE
);
8426 case CMD_TYPE_IS_ASSIGNABLE_FROM
: {
8427 MonoClass
*oklass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8429 if (err
!= ERR_NONE
)
8431 if (mono_class_is_assignable_from_internal (klass
, oklass
))
8432 buffer_add_byte (buf
, 1);
8434 buffer_add_byte (buf
, 0);
8437 case CMD_TYPE_GET_METHODS_BY_NAME_FLAGS
: {
8438 char *name
= decode_string (p
, &p
, end
);
8439 int i
, flags
= decode_int (p
, &p
, end
);
8441 if (CHECK_PROTOCOL_VERSION (2, 48))
8442 mlisttype
= decode_int (p
, &p
, end
);
8444 mlisttype
= 0; // MLISTTYPE_All
8449 array
= mono_class_get_methods_by_name (klass
, name
, flags
& ~BINDING_FLAGS_IGNORE_CASE
, mlisttype
, TRUE
, error
);
8450 if (!is_ok (error
)) {
8451 mono_error_cleanup (error
);
8454 buffer_add_int (buf
, array
->len
);
8455 for (i
= 0; i
< array
->len
; ++i
) {
8456 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (array
, i
);
8457 buffer_add_methodid (buf
, domain
, method
);
8460 g_ptr_array_free (array
, TRUE
);
8464 case CMD_TYPE_GET_INTERFACES
: {
8466 GHashTable
*iface_hash
= g_hash_table_new (NULL
, NULL
);
8467 MonoClass
*tclass
, *iface
;
8468 GHashTableIter iter
;
8472 for (parent
= tclass
; parent
; parent
= m_class_get_parent (parent
)) {
8473 mono_class_setup_interfaces (parent
, error
);
8474 goto_if_nok (error
, loader_error
);
8476 collect_interfaces (parent
, iface_hash
, error
);
8477 goto_if_nok (error
, loader_error
);
8480 buffer_add_int (buf
, g_hash_table_size (iface_hash
));
8482 g_hash_table_iter_init (&iter
, iface_hash
);
8483 while (g_hash_table_iter_next (&iter
, NULL
, (void**)&iface
))
8484 buffer_add_typeid (buf
, domain
, iface
);
8485 g_hash_table_destroy (iface_hash
);
8488 case CMD_TYPE_GET_INTERFACE_MAP
: {
8489 int tindex
, ioffset
;
8490 gboolean variance_used
;
8492 int len
, nmethods
, i
;
8496 len
= decode_int (p
, &p
, end
);
8497 mono_class_setup_vtable (klass
);
8499 for (tindex
= 0; tindex
< len
; ++tindex
) {
8500 iclass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8501 if (err
!= ERR_NONE
)
8504 ioffset
= mono_class_interface_offset_with_variance (klass
, iclass
, &variance_used
);
8506 goto invalid_argument
;
8508 nmethods
= mono_class_num_methods (iclass
);
8509 buffer_add_int (buf
, nmethods
);
8512 while ((method
= mono_class_get_methods (iclass
, &iter
))) {
8513 buffer_add_methodid (buf
, domain
, method
);
8515 MonoMethod
**klass_vtable
= m_class_get_vtable (klass
);
8516 for (i
= 0; i
< nmethods
; ++i
)
8517 buffer_add_methodid (buf
, domain
, klass_vtable
[i
+ ioffset
]);
8521 case CMD_TYPE_IS_INITIALIZED
: {
8522 MonoVTable
*vtable
= mono_class_vtable_checked (domain
, klass
, error
);
8523 goto_if_nok (error
, loader_error
);
8526 buffer_add_int (buf
, (vtable
->initialized
|| vtable
->init_failed
) ? 1 : 0);
8528 buffer_add_int (buf
, 0);
8531 case CMD_TYPE_CREATE_INSTANCE
: {
8535 obj
= mono_object_new_checked (domain
, klass
, error
);
8536 mono_error_assert_ok (error
);
8537 buffer_add_objid (buf
, obj
);
8540 case CMD_TYPE_GET_VALUE_SIZE
: {
8543 value_size
= mono_class_value_size (klass
, NULL
);
8544 buffer_add_int (buf
, value_size
);
8548 err
= ERR_NOT_IMPLEMENTED
;
8555 err
= ERR_INVALID_ARGUMENT
;
8558 err
= ERR_INVALID_FIELDID
;
8561 err
= ERR_INVALID_OBJECT
;
8564 err
= ERR_LOADER_ERROR
;
8567 HANDLE_FUNCTION_RETURN_VAL (err
);
8571 type_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
8574 MonoDomain
*old_domain
;
8578 klass
= decode_typeid (p
, &p
, end
, &domain
, &err
);
8579 if (err
!= ERR_NONE
)
8582 old_domain
= mono_domain_get ();
8584 mono_domain_set_fast (domain
, TRUE
);
8586 err
= type_commands_internal (command
, klass
, domain
, p
, end
, buf
);
8588 mono_domain_set_fast (old_domain
, TRUE
);
8594 method_commands_internal (int command
, MonoMethod
*method
, MonoDomain
*domain
, guint8
*p
, guint8
*end
, Buffer
*buf
)
8596 MonoMethodHeader
*header
;
8600 case CMD_METHOD_GET_NAME
: {
8601 buffer_add_string (buf
, method
->name
);
8604 case CMD_METHOD_GET_DECLARING_TYPE
: {
8605 buffer_add_typeid (buf
, domain
, method
->klass
);
8608 case CMD_METHOD_GET_DEBUG_INFO
: {
8610 MonoDebugMethodInfo
*minfo
;
8612 int i
, j
, n_il_offsets
;
8614 GPtrArray
*source_file_list
;
8615 MonoSymSeqPoint
*sym_seq_points
;
8617 header
= mono_method_get_header_checked (method
, error
);
8619 mono_error_cleanup (error
); /* FIXME don't swallow the error */
8620 buffer_add_int (buf
, 0);
8621 buffer_add_string (buf
, "");
8622 buffer_add_int (buf
, 0);
8626 minfo
= mono_debug_lookup_method (method
);
8628 buffer_add_int (buf
, header
->code_size
);
8629 buffer_add_string (buf
, "");
8630 buffer_add_int (buf
, 0);
8631 mono_metadata_free_mh (header
);
8635 mono_debug_get_seq_points (minfo
, &source_file
, &source_file_list
, &source_files
, &sym_seq_points
, &n_il_offsets
);
8636 buffer_add_int (buf
, header
->code_size
);
8637 if (CHECK_PROTOCOL_VERSION (2, 13)) {
8638 buffer_add_int (buf
, source_file_list
->len
);
8639 for (i
= 0; i
< source_file_list
->len
; ++i
) {
8640 MonoDebugSourceInfo
*sinfo
= (MonoDebugSourceInfo
*)g_ptr_array_index (source_file_list
, i
);
8641 buffer_add_string (buf
, sinfo
->source_file
);
8642 if (CHECK_PROTOCOL_VERSION (2, 14)) {
8643 for (j
= 0; j
< 16; ++j
)
8644 buffer_add_byte (buf
, sinfo
->hash
[j
]);
8648 buffer_add_string (buf
, source_file
);
8650 buffer_add_int (buf
, n_il_offsets
);
8651 DEBUG_PRINTF (10, "Line number table for method %s:\n", mono_method_full_name (method
, TRUE
));
8652 for (i
= 0; i
< n_il_offsets
; ++i
) {
8653 MonoSymSeqPoint
*sp
= &sym_seq_points
[i
];
8654 const char *srcfile
= "";
8656 if (source_files
[i
] != -1) {
8657 MonoDebugSourceInfo
*sinfo
= (MonoDebugSourceInfo
*)g_ptr_array_index (source_file_list
, source_files
[i
]);
8658 srcfile
= sinfo
->source_file
;
8660 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
);
8661 buffer_add_int (buf
, sp
->il_offset
);
8662 buffer_add_int (buf
, sp
->line
);
8663 if (CHECK_PROTOCOL_VERSION (2, 13))
8664 buffer_add_int (buf
, source_files
[i
]);
8665 if (CHECK_PROTOCOL_VERSION (2, 19))
8666 buffer_add_int (buf
, sp
->column
);
8667 if (CHECK_PROTOCOL_VERSION (2, 32)) {
8668 buffer_add_int (buf
, sp
->end_line
);
8669 buffer_add_int (buf
, sp
->end_column
);
8672 g_free (source_file
);
8673 g_free (source_files
);
8674 g_free (sym_seq_points
);
8675 g_ptr_array_free (source_file_list
, TRUE
);
8676 mono_metadata_free_mh (header
);
8679 case CMD_METHOD_GET_PARAM_INFO
: {
8680 MonoMethodSignature
*sig
= mono_method_signature_internal (method
);
8684 /* FIXME: mono_class_from_mono_type_internal () and byrefs */
8686 /* FIXME: Use a smaller encoding */
8687 buffer_add_int (buf
, sig
->call_convention
);
8688 buffer_add_int (buf
, sig
->param_count
);
8689 buffer_add_int (buf
, sig
->generic_param_count
);
8690 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (sig
->ret
));
8691 for (i
= 0; i
< sig
->param_count
; ++i
) {
8693 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (sig
->params
[i
]));
8696 /* Emit parameter names */
8697 names
= g_new (char *, sig
->param_count
);
8698 mono_method_get_param_names (method
, (const char **) names
);
8699 for (i
= 0; i
< sig
->param_count
; ++i
)
8700 buffer_add_string (buf
, names
[i
]);
8705 case CMD_METHOD_GET_LOCALS_INFO
: {
8708 MonoDebugLocalsInfo
*locals
;
8709 int *locals_map
= NULL
;
8711 header
= mono_method_get_header_checked (method
, error
);
8713 mono_error_cleanup (error
); /* FIXME don't swallow the error */
8714 return ERR_INVALID_ARGUMENT
;
8717 locals
= mono_debug_lookup_locals (method
);
8719 if (CHECK_PROTOCOL_VERSION (2, 43)) {
8721 buffer_add_int (buf
, 1);
8722 buffer_add_int (buf
, 0);
8723 buffer_add_int (buf
, header
->code_size
);
8725 buffer_add_int (buf
, header
->num_locals
);
8727 for (i
= 0; i
< header
->num_locals
; ++i
) {
8728 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (header
->locals
[i
]));
8731 for (i
= 0; i
< header
->num_locals
; ++i
) {
8733 sprintf (lname
, "V_%d", i
);
8734 buffer_add_string (buf
, lname
);
8737 for (i
= 0; i
< header
->num_locals
; ++i
) {
8738 buffer_add_int (buf
, 0);
8739 buffer_add_int (buf
, header
->code_size
);
8742 if (CHECK_PROTOCOL_VERSION (2, 43)) {
8744 buffer_add_int (buf
, locals
->num_blocks
);
8746 for (i
= 0; i
< locals
->num_blocks
; ++i
) {
8747 buffer_add_int (buf
, locals
->code_blocks
[i
].start_offset
- last_start
);
8748 buffer_add_int (buf
, locals
->code_blocks
[i
].end_offset
- locals
->code_blocks
[i
].start_offset
);
8749 last_start
= locals
->code_blocks
[i
].start_offset
;
8753 num_locals
= locals
->num_locals
;
8754 buffer_add_int (buf
, num_locals
);
8757 for (i
= 0; i
< num_locals
; ++i
) {
8758 g_assert (locals
->locals
[i
].index
< header
->num_locals
);
8759 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (header
->locals
[locals
->locals
[i
].index
]));
8762 for (i
= 0; i
< num_locals
; ++i
)
8763 buffer_add_string (buf
, locals
->locals
[i
].name
);
8765 for (i
= 0; i
< num_locals
; ++i
) {
8766 if (locals
->locals
[i
].block
) {
8767 buffer_add_int (buf
, locals
->locals
[i
].block
->start_offset
);
8768 buffer_add_int (buf
, locals
->locals
[i
].block
->end_offset
);
8770 buffer_add_int (buf
, 0);
8771 buffer_add_int (buf
, header
->code_size
);
8775 mono_metadata_free_mh (header
);
8778 mono_debug_free_locals (locals
);
8779 g_free (locals_map
);
8783 case CMD_METHOD_GET_INFO
:
8784 buffer_add_int (buf
, method
->flags
);
8785 buffer_add_int (buf
, method
->iflags
);
8786 buffer_add_int (buf
, method
->token
);
8787 if (CHECK_PROTOCOL_VERSION (2, 12)) {
8789 if (method
->is_generic
)
8791 if (mono_method_signature_internal (method
)->generic_param_count
)
8793 buffer_add_byte (buf
, attrs
);
8794 if (method
->is_generic
|| method
->is_inflated
) {
8797 if (method
->is_generic
) {
8800 MonoMethodInflated
*imethod
= (MonoMethodInflated
*)method
;
8802 result
= imethod
->declaring
;
8803 if (imethod
->context
.class_inst
) {
8804 MonoClass
*klass
= ((MonoMethod
*) imethod
)->klass
;
8805 /*Generic methods gets the context of the GTD.*/
8806 if (mono_class_get_context (klass
)) {
8808 result
= mono_class_inflate_generic_method_full_checked (result
, klass
, mono_class_get_context (klass
), error
);
8809 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
8814 buffer_add_methodid (buf
, domain
, result
);
8816 buffer_add_id (buf
, 0);
8818 if (CHECK_PROTOCOL_VERSION (2, 15)) {
8819 if (mono_method_signature_internal (method
)->generic_param_count
) {
8822 if (method
->is_inflated
) {
8823 MonoGenericInst
*inst
= mono_method_get_context (method
)->method_inst
;
8825 count
= inst
->type_argc
;
8826 buffer_add_int (buf
, count
);
8828 for (i
= 0; i
< count
; i
++)
8829 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal (inst
->type_argv
[i
]));
8831 buffer_add_int (buf
, 0);
8833 } else if (method
->is_generic
) {
8834 MonoGenericContainer
*container
= mono_method_get_generic_container (method
);
8836 count
= mono_method_signature_internal (method
)->generic_param_count
;
8837 buffer_add_int (buf
, count
);
8838 for (i
= 0; i
< count
; i
++) {
8839 MonoGenericParam
*param
= mono_generic_container_get_param (container
, i
);
8840 MonoClass
*pklass
= mono_class_create_generic_parameter (param
);
8841 buffer_add_typeid (buf
, domain
, pklass
);
8844 buffer_add_int (buf
, 0);
8847 buffer_add_int (buf
, 0);
8852 case CMD_METHOD_GET_BODY
: {
8856 header
= mono_method_get_header_checked (method
, error
);
8858 mono_error_cleanup (error
); /* FIXME don't swallow the error */
8859 buffer_add_int (buf
, 0);
8861 if (CHECK_PROTOCOL_VERSION (2, 18))
8862 buffer_add_int (buf
, 0);
8864 buffer_add_int (buf
, header
->code_size
);
8865 for (i
= 0; i
< header
->code_size
; ++i
)
8866 buffer_add_byte (buf
, header
->code
[i
]);
8868 if (CHECK_PROTOCOL_VERSION (2, 18)) {
8869 buffer_add_int (buf
, header
->num_clauses
);
8870 for (i
= 0; i
< header
->num_clauses
; ++i
) {
8871 MonoExceptionClause
*clause
= &header
->clauses
[i
];
8873 buffer_add_int (buf
, clause
->flags
);
8874 buffer_add_int (buf
, clause
->try_offset
);
8875 buffer_add_int (buf
, clause
->try_len
);
8876 buffer_add_int (buf
, clause
->handler_offset
);
8877 buffer_add_int (buf
, clause
->handler_len
);
8878 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_NONE
)
8879 buffer_add_typeid (buf
, domain
, clause
->data
.catch_class
);
8880 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
)
8881 buffer_add_int (buf
, clause
->data
.filter_offset
);
8885 mono_metadata_free_mh (header
);
8890 case CMD_METHOD_RESOLVE_TOKEN
: {
8891 guint32 token
= decode_int (p
, &p
, end
);
8894 switch (mono_metadata_token_code (token
)) {
8895 case MONO_TOKEN_STRING
: {
8900 s
= mono_ldstr_checked (domain
, m_class_get_image (method
->klass
), mono_metadata_token_index (token
), error
);
8901 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
8903 s2
= mono_string_to_utf8_checked_internal (s
, error
);
8904 mono_error_assert_ok (error
);
8906 buffer_add_byte (buf
, TOKEN_TYPE_STRING
);
8907 buffer_add_string (buf
, s2
);
8914 MonoClass
*handle_class
;
8916 if (method
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
) {
8917 val
= mono_method_get_wrapper_data (method
, token
);
8918 handle_class
= (MonoClass
*)mono_method_get_wrapper_data (method
, token
+ 1);
8920 if (handle_class
== NULL
) {
8921 // Can't figure out the token type
8922 buffer_add_byte (buf
, TOKEN_TYPE_UNKNOWN
);
8926 val
= mono_ldtoken_checked (m_class_get_image (method
->klass
), token
, &handle_class
, NULL
, error
);
8928 g_error ("Could not load token due to %s", mono_error_get_message (error
));
8931 if (handle_class
== mono_defaults
.typehandle_class
) {
8932 buffer_add_byte (buf
, TOKEN_TYPE_TYPE
);
8933 if (method
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
)
8934 buffer_add_typeid (buf
, domain
, (MonoClass
*) val
);
8936 buffer_add_typeid (buf
, domain
, mono_class_from_mono_type_internal ((MonoType
*)val
));
8937 } else if (handle_class
== mono_defaults
.fieldhandle_class
) {
8938 buffer_add_byte (buf
, TOKEN_TYPE_FIELD
);
8939 buffer_add_fieldid (buf
, domain
, (MonoClassField
*)val
);
8940 } else if (handle_class
== mono_defaults
.methodhandle_class
) {
8941 buffer_add_byte (buf
, TOKEN_TYPE_METHOD
);
8942 buffer_add_methodid (buf
, domain
, (MonoMethod
*)val
);
8943 } else if (handle_class
== mono_defaults
.string_class
) {
8946 s
= mono_string_to_utf8_checked_internal ((MonoString
*)val
, error
);
8947 mono_error_assert_ok (error
);
8948 buffer_add_byte (buf
, TOKEN_TYPE_STRING
);
8949 buffer_add_string (buf
, s
);
8952 g_assert_not_reached ();
8959 case CMD_METHOD_GET_CATTRS
: {
8961 MonoClass
*attr_klass
;
8962 MonoCustomAttrInfo
*cinfo
;
8964 attr_klass
= decode_typeid (p
, &p
, end
, NULL
, &err
);
8965 /* attr_klass can be NULL */
8966 if (err
!= ERR_NONE
)
8969 cinfo
= mono_custom_attrs_from_method_checked (method
, error
);
8970 if (!is_ok (error
)) {
8971 mono_error_cleanup (error
); /* FIXME don't swallow the error message */
8972 return ERR_LOADER_ERROR
;
8975 err
= buffer_add_cattrs (buf
, domain
, m_class_get_image (method
->klass
), attr_klass
, cinfo
);
8976 if (err
!= ERR_NONE
)
8980 case CMD_METHOD_MAKE_GENERIC_METHOD
: {
8982 MonoType
**type_argv
;
8986 MonoGenericInst
*ginst
;
8987 MonoGenericContext tmp_context
;
8988 MonoMethod
*inflated
;
8990 type_argc
= decode_int (p
, &p
, end
);
8991 type_argv
= g_new0 (MonoType
*, type_argc
);
8992 for (i
= 0; i
< type_argc
; ++i
) {
8993 klass
= decode_typeid (p
, &p
, end
, &d
, &err
);
8994 if (err
!= ERR_NONE
) {
9000 return ERR_INVALID_ARGUMENT
;
9002 type_argv
[i
] = m_class_get_byval_arg (klass
);
9004 ginst
= mono_metadata_get_generic_inst (type_argc
, type_argv
);
9006 tmp_context
.class_inst
= mono_class_is_ginst (method
->klass
) ? mono_class_get_generic_class (method
->klass
)->context
.class_inst
: NULL
;
9007 tmp_context
.method_inst
= ginst
;
9009 inflated
= mono_class_inflate_generic_method_checked (method
, &tmp_context
, error
);
9010 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
9011 if (!mono_verifier_is_method_valid_generic_instantiation (inflated
))
9012 return ERR_INVALID_ARGUMENT
;
9013 buffer_add_methodid (buf
, domain
, inflated
);
9017 return ERR_NOT_IMPLEMENTED
;
9024 method_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9027 MonoDomain
*old_domain
;
9031 method
= decode_methodid (p
, &p
, end
, &domain
, &err
);
9032 if (err
!= ERR_NONE
)
9035 old_domain
= mono_domain_get ();
9037 mono_domain_set_fast (domain
, TRUE
);
9039 err
= method_commands_internal (command
, method
, domain
, p
, end
, buf
);
9041 mono_domain_set_fast (old_domain
, TRUE
);
9047 thread_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9049 int objid
= decode_objid (p
, &p
, end
);
9051 MonoThread
*thread_obj
;
9052 MonoInternalThread
*thread
;
9054 err
= get_object (objid
, (MonoObject
**)&thread_obj
);
9055 if (err
!= ERR_NONE
)
9058 thread
= THREAD_TO_INTERNAL (thread_obj
);
9061 case CMD_THREAD_GET_NAME
: {
9062 char *s
= mono_thread_get_name_utf8 (thread_obj
);
9065 buffer_add_int (buf
, 0);
9067 const size_t len
= strlen (s
);
9068 buffer_add_int (buf
, len
);
9069 buffer_add_data (buf
, (guint8
*)s
, len
);
9074 case CMD_THREAD_GET_FRAME_INFO
: {
9075 DebuggerTlsData
*tls
;
9076 int i
, start_frame
, length
;
9078 // Wait for suspending if it already started
9079 // FIXME: Races with suspend_count
9080 while (!is_suspended ()) {
9082 wait_for_suspend ();
9086 wait_for_suspend ();
9087 if (!is_suspended ())
9088 return ERR_NOT_SUSPENDED;
9091 start_frame
= decode_int (p
, &p
, end
);
9092 length
= decode_int (p
, &p
, end
);
9094 if (start_frame
!= 0 || length
!= -1)
9095 return ERR_NOT_IMPLEMENTED
;
9097 mono_loader_lock ();
9098 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
9099 mono_loader_unlock ();
9102 compute_frame_info (thread
, tls
, TRUE
); //the last parameter is TRUE to force that the frame info that will be send is synchronised with the debugged thread
9104 buffer_add_int (buf
, tls
->frame_count
);
9105 for (i
= 0; i
< tls
->frame_count
; ++i
) {
9106 buffer_add_int (buf
, tls
->frames
[i
]->id
);
9107 buffer_add_methodid (buf
, tls
->frames
[i
]->de
.domain
, tls
->frames
[i
]->actual_method
);
9108 buffer_add_int (buf
, tls
->frames
[i
]->il_offset
);
9110 * Instead of passing the frame type directly to the client, we associate
9111 * it with the previous frame using a set of flags. This avoids lots of
9112 * conditional code in the client, since a frame whose type isn't
9113 * FRAME_TYPE_MANAGED has no method, location, etc.
9115 buffer_add_byte (buf
, tls
->frames
[i
]->flags
);
9120 case CMD_THREAD_GET_STATE
:
9121 buffer_add_int (buf
, thread
->state
);
9123 case CMD_THREAD_GET_INFO
:
9124 buffer_add_byte (buf
, thread
->threadpool_thread
);
9126 case CMD_THREAD_GET_ID
:
9127 buffer_add_long (buf
, (guint64
)(gsize
)thread
);
9129 case CMD_THREAD_GET_TID
:
9130 buffer_add_long (buf
, (guint64
)thread
->tid
);
9132 case CMD_THREAD_SET_IP
: {
9133 DebuggerTlsData
*tls
;
9136 MonoSeqPointInfo
*seq_points
;
9141 method
= decode_methodid (p
, &p
, end
, &domain
, &err
);
9142 if (err
!= ERR_NONE
)
9144 il_offset
= decode_long (p
, &p
, end
);
9146 while (!is_suspended ()) {
9148 wait_for_suspend ();
9151 mono_loader_lock ();
9152 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
9153 mono_loader_unlock ();
9156 compute_frame_info (thread
, tls
, FALSE
);
9157 if (tls
->frame_count
== 0 || tls
->frames
[0]->actual_method
!= method
)
9158 return ERR_INVALID_ARGUMENT
;
9160 found_sp
= mono_find_seq_point (domain
, method
, il_offset
, &seq_points
, &sp
);
9162 g_assert (seq_points
);
9165 return ERR_INVALID_ARGUMENT
;
9167 // FIXME: Check that the ip change is safe
9169 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
);
9171 if (tls
->frames
[0]->de
.ji
->is_interp
) {
9172 MonoJitTlsData
*jit_data
= thread
->thread_info
->jit_data
;
9173 mini_get_interp_callbacks ()->set_resume_state (jit_data
, NULL
, NULL
, tls
->frames
[0]->interp_frame
, (guint8
*)tls
->frames
[0]->de
.ji
->code_start
+ sp
.native_offset
);
9175 MONO_CONTEXT_SET_IP (&tls
->restore_state
.ctx
, (guint8
*)tls
->frames
[0]->de
.ji
->code_start
+ sp
.native_offset
);
9179 case CMD_THREAD_ELAPSED_TIME
: {
9180 DebuggerTlsData
*tls
;
9181 mono_loader_lock ();
9182 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
9183 mono_loader_unlock ();
9185 buffer_add_long (buf
, (long)mono_stopwatch_elapsed_ms (&tls
->step_time
));
9189 return ERR_NOT_IMPLEMENTED
;
9196 frame_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9200 MonoThread
*thread_obj
;
9201 MonoInternalThread
*thread
;
9202 int pos
, i
, len
, frame_idx
;
9203 DebuggerTlsData
*tls
;
9205 MonoDebugMethodJitInfo
*jit
;
9206 MonoMethodSignature
*sig
;
9208 MonoMethodHeader
*header
;
9210 objid
= decode_objid (p
, &p
, end
);
9211 err
= get_object (objid
, (MonoObject
**)&thread_obj
);
9212 if (err
!= ERR_NONE
)
9215 thread
= THREAD_TO_INTERNAL (thread_obj
);
9217 id
= decode_id (p
, &p
, end
);
9219 mono_loader_lock ();
9220 tls
= (DebuggerTlsData
*)mono_g_hash_table_lookup (thread_to_tls
, thread
);
9221 mono_loader_unlock ();
9224 for (i
= 0; i
< tls
->frame_count
; ++i
) {
9225 if (tls
->frames
[i
]->id
== id
)
9228 if (i
== tls
->frame_count
)
9229 return ERR_INVALID_FRAMEID
;
9231 /* The thread is still running native code, can't get frame variables info */
9232 if (!tls
->really_suspended
&& !tls
->async_state
.valid
)
9233 return ERR_NOT_SUSPENDED
;
9235 frame
= tls
->frames
[frame_idx
];
9237 /* This is supported for frames without has_ctx etc. set */
9238 if (command
== CMD_STACK_FRAME_GET_DOMAIN
) {
9239 if (CHECK_PROTOCOL_VERSION (2, 38))
9240 buffer_add_domainid (buf
, frame
->de
.domain
);
9244 if (!frame
->has_ctx
)
9245 return ERR_ABSENT_INFORMATION
;
9247 if (!ensure_jit ((DbgEngineStackFrame
*)frame
))
9248 return ERR_ABSENT_INFORMATION
;
9252 sig
= mono_method_signature_internal (frame
->actual_method
);
9254 if (!(jit
->has_var_info
|| frame
->de
.ji
->is_interp
) || !mono_get_seq_points (frame
->de
.domain
, frame
->actual_method
))
9256 * The method is probably from an aot image compiled without soft-debug, variables might be dead, etc.
9258 return ERR_ABSENT_INFORMATION
;
9261 case CMD_STACK_FRAME_GET_VALUES
: {
9263 len
= decode_int (p
, &p
, end
);
9264 header
= mono_method_get_header_checked (frame
->actual_method
, error
);
9265 mono_error_assert_ok (error
); /* FIXME report error */
9267 for (i
= 0; i
< len
; ++i
) {
9268 pos
= decode_int (p
, &p
, end
);
9273 DEBUG_PRINTF (4, "[dbg] send arg %d.\n", pos
);
9275 if (frame
->de
.ji
->is_interp
) {
9278 addr
= (guint8
*)mini_get_interp_callbacks ()->frame_get_arg (frame
->interp_frame
, pos
);
9280 buffer_add_value_full (buf
, sig
->params
[pos
], addr
, frame
->de
.domain
, FALSE
, NULL
, 1);
9282 g_assert (pos
>= 0 && pos
< jit
->num_params
);
9284 add_var (buf
, jit
, sig
->params
[pos
], &jit
->params
[pos
], &frame
->ctx
, frame
->de
.domain
, FALSE
);
9287 MonoDebugLocalsInfo
*locals
;
9289 locals
= mono_debug_lookup_locals (frame
->de
.method
);
9291 g_assert (pos
< locals
->num_locals
);
9292 pos
= locals
->locals
[pos
].index
;
9293 mono_debug_free_locals (locals
);
9296 DEBUG_PRINTF (4, "[dbg] send local %d.\n", pos
);
9298 if (frame
->de
.ji
->is_interp
) {
9301 addr
= (guint8
*)mini_get_interp_callbacks ()->frame_get_local (frame
->interp_frame
, pos
);
9303 buffer_add_value_full (buf
, header
->locals
[pos
], addr
, frame
->de
.domain
, FALSE
, NULL
, 1);
9305 g_assert (pos
>= 0 && pos
< jit
->num_locals
);
9307 add_var (buf
, jit
, header
->locals
[pos
], &jit
->locals
[pos
], &frame
->ctx
, frame
->de
.domain
, FALSE
);
9311 mono_metadata_free_mh (header
);
9314 case CMD_STACK_FRAME_GET_THIS
: {
9315 if (frame
->de
.method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
)
9316 return ERR_ABSENT_INFORMATION
;
9317 if (m_class_is_valuetype (frame
->api_method
->klass
)) {
9318 if (!sig
->hasthis
) {
9319 MonoObject
*p
= NULL
;
9320 buffer_add_value (buf
, mono_get_object_type (), &p
, frame
->de
.domain
);
9322 if (frame
->de
.ji
->is_interp
) {
9325 addr
= (guint8
*)mini_get_interp_callbacks ()->frame_get_this (frame
->interp_frame
);
9327 buffer_add_value_full (buf
, m_class_get_this_arg (frame
->actual_method
->klass
), addr
, frame
->de
.domain
, FALSE
, NULL
, 1);
9329 add_var (buf
, jit
, m_class_get_this_arg (frame
->actual_method
->klass
), jit
->this_var
, &frame
->ctx
, frame
->de
.domain
, TRUE
);
9333 if (!sig
->hasthis
) {
9334 MonoObject
*p
= NULL
;
9335 buffer_add_value (buf
, m_class_get_byval_arg (frame
->actual_method
->klass
), &p
, frame
->de
.domain
);
9337 if (frame
->de
.ji
->is_interp
) {
9340 addr
= (guint8
*)mini_get_interp_callbacks ()->frame_get_this (frame
->interp_frame
);
9342 buffer_add_value_full (buf
, m_class_get_byval_arg (frame
->api_method
->klass
), addr
, frame
->de
.domain
, FALSE
, NULL
, 1);
9344 add_var (buf
, jit
, m_class_get_byval_arg (frame
->api_method
->klass
), jit
->this_var
, &frame
->ctx
, frame
->de
.domain
, TRUE
);
9350 case CMD_STACK_FRAME_SET_VALUES
: {
9354 MonoDebugVarInfo
*var
= NULL
;
9355 gboolean is_arg
= FALSE
;
9357 len
= decode_int (p
, &p
, end
);
9358 header
= mono_method_get_header_checked (frame
->actual_method
, error
);
9359 mono_error_assert_ok (error
); /* FIXME report error */
9361 for (i
= 0; i
< len
; ++i
) {
9362 pos
= decode_int (p
, &p
, end
);
9367 g_assert (pos
>= 0 && pos
< jit
->num_params
);
9369 t
= sig
->params
[pos
];
9370 var
= &jit
->params
[pos
];
9373 MonoDebugLocalsInfo
*locals
;
9375 locals
= mono_debug_lookup_locals (frame
->de
.method
);
9377 g_assert (pos
< locals
->num_locals
);
9378 pos
= locals
->locals
[pos
].index
;
9379 mono_debug_free_locals (locals
);
9381 g_assert (pos
>= 0 && pos
< jit
->num_locals
);
9383 t
= header
->locals
[pos
];
9384 var
= &jit
->locals
[pos
];
9387 if (MONO_TYPE_IS_REFERENCE (t
))
9388 val_buf
= (guint8
*)g_alloca (sizeof (MonoObject
*));
9390 val_buf
= (guint8
*)g_alloca (mono_class_instance_size (mono_class_from_mono_type_internal (t
)));
9391 err
= decode_value (t
, frame
->de
.domain
, val_buf
, p
, &p
, end
, TRUE
);
9392 if (err
!= ERR_NONE
)
9395 if (frame
->de
.ji
->is_interp
) {
9399 addr
= (guint8
*)mini_get_interp_callbacks ()->frame_get_arg (frame
->interp_frame
, pos
);
9401 addr
= (guint8
*)mini_get_interp_callbacks ()->frame_get_local (frame
->interp_frame
, pos
);
9402 set_interp_var (t
, addr
, val_buf
);
9404 set_var (t
, var
, &frame
->ctx
, frame
->de
.domain
, val_buf
, frame
->reg_locations
, &tls
->restore_state
.ctx
);
9407 mono_metadata_free_mh (header
);
9410 case CMD_STACK_FRAME_GET_DOMAIN
: {
9411 if (CHECK_PROTOCOL_VERSION (2, 38))
9412 buffer_add_domainid (buf
, frame
->de
.domain
);
9415 case CMD_STACK_FRAME_SET_THIS
: {
9418 MonoDebugVarInfo
*var
;
9420 t
= m_class_get_byval_arg (frame
->actual_method
->klass
);
9421 /* Checked by the sender */
9422 g_assert (MONO_TYPE_ISSTRUCT (t
));
9424 val_buf
= (guint8
*)g_alloca (mono_class_instance_size (mono_class_from_mono_type_internal (t
)));
9425 err
= decode_value (t
, frame
->de
.domain
, val_buf
, p
, &p
, end
, TRUE
);
9426 if (err
!= ERR_NONE
)
9429 if (frame
->de
.ji
->is_interp
) {
9432 addr
= (guint8
*)mini_get_interp_callbacks ()->frame_get_this (frame
->interp_frame
);
9433 set_interp_var (m_class_get_this_arg (frame
->actual_method
->klass
), addr
, val_buf
);
9435 var
= jit
->this_var
;
9438 set_var (m_class_get_this_arg (frame
->actual_method
->klass
), var
, &frame
->ctx
, frame
->de
.domain
, val_buf
, frame
->reg_locations
, &tls
->restore_state
.ctx
);
9443 return ERR_NOT_IMPLEMENTED
;
9450 array_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9453 int objid
, index
, len
, i
, esize
;
9457 objid
= decode_objid (p
, &p
, end
);
9458 err
= get_object (objid
, (MonoObject
**)&arr
);
9459 if (err
!= ERR_NONE
)
9463 case CMD_ARRAY_REF_GET_LENGTH
:
9464 buffer_add_int (buf
, m_class_get_rank (arr
->obj
.vtable
->klass
));
9466 buffer_add_int (buf
, arr
->max_length
);
9467 buffer_add_int (buf
, 0);
9469 for (i
= 0; i
< m_class_get_rank (arr
->obj
.vtable
->klass
); ++i
) {
9470 buffer_add_int (buf
, arr
->bounds
[i
].length
);
9471 buffer_add_int (buf
, arr
->bounds
[i
].lower_bound
);
9475 case CMD_ARRAY_REF_GET_VALUES
:
9476 index
= decode_int (p
, &p
, end
);
9477 len
= decode_int (p
, &p
, end
);
9479 g_assert (index
>= 0 && len
>= 0);
9480 // Reordered to avoid integer overflow
9481 g_assert (!(index
> arr
->max_length
- len
));
9483 esize
= mono_array_element_size (arr
->obj
.vtable
->klass
);
9484 for (i
= index
; i
< index
+ len
; ++i
) {
9485 elem
= (gpointer
*)((char*)arr
->vector
+ (i
* esize
));
9486 buffer_add_value (buf
, m_class_get_byval_arg (m_class_get_element_class (arr
->obj
.vtable
->klass
)), elem
, arr
->obj
.vtable
->domain
);
9489 case CMD_ARRAY_REF_SET_VALUES
:
9490 index
= decode_int (p
, &p
, end
);
9491 len
= decode_int (p
, &p
, end
);
9493 g_assert (index
>= 0 && len
>= 0);
9494 // Reordered to avoid integer overflow
9495 g_assert (!(index
> arr
->max_length
- len
));
9497 esize
= mono_array_element_size (arr
->obj
.vtable
->klass
);
9498 for (i
= index
; i
< index
+ len
; ++i
) {
9499 elem
= (gpointer
*)((char*)arr
->vector
+ (i
* esize
));
9501 decode_value (m_class_get_byval_arg (m_class_get_element_class (arr
->obj
.vtable
->klass
)), arr
->obj
.vtable
->domain
, (guint8
*)elem
, p
, &p
, end
, TRUE
);
9505 return ERR_NOT_IMPLEMENTED
;
9512 string_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9518 int i
, index
, length
;
9520 gboolean use_utf16
= FALSE
;
9522 objid
= decode_objid (p
, &p
, end
);
9523 err
= get_object (objid
, (MonoObject
**)&str
);
9524 if (err
!= ERR_NONE
)
9528 case CMD_STRING_REF_GET_VALUE
:
9529 if (CHECK_PROTOCOL_VERSION (2, 41)) {
9530 for (i
= 0; i
< mono_string_length_internal (str
); ++i
)
9531 if (mono_string_chars_internal (str
)[i
] == 0)
9533 buffer_add_byte (buf
, use_utf16
? 1 : 0);
9536 buffer_add_int (buf
, mono_string_length_internal (str
) * 2);
9537 buffer_add_data (buf
, (guint8
*)mono_string_chars_internal (str
), mono_string_length_internal (str
) * 2);
9540 s
= mono_string_to_utf8_checked_internal (str
, error
);
9541 if (!is_ok (error
)) {
9545 return ERR_INVALID_ARGUMENT
;
9547 buffer_add_string (buf
, s
);
9551 case CMD_STRING_REF_GET_LENGTH
:
9552 buffer_add_long (buf
, mono_string_length_internal (str
));
9554 case CMD_STRING_REF_GET_CHARS
:
9555 index
= decode_long (p
, &p
, end
);
9556 length
= decode_long (p
, &p
, end
);
9557 if (index
> mono_string_length_internal (str
) - length
)
9558 return ERR_INVALID_ARGUMENT
;
9559 c
= mono_string_chars_internal (str
) + index
;
9560 for (i
= 0; i
< length
; ++i
)
9561 buffer_add_short (buf
, c
[i
]);
9564 return ERR_NOT_IMPLEMENTED
;
9571 create_file_to_check_memory_address (void)
9573 if (file_check_valid_memory
!= -1)
9575 char *file_name
= g_strdup_printf ("debugger_check_valid_memory.%d", getpid());
9576 filename_check_valid_memory
= g_build_filename (g_get_tmp_dir (), file_name
, (const char*)NULL
);
9577 file_check_valid_memory
= open(filename_check_valid_memory
, O_CREAT
| O_WRONLY
| O_APPEND
, S_IWUSR
);
9582 valid_memory_address (gpointer addr
, gint size
)
9585 gboolean ret
= TRUE
;
9586 create_file_to_check_memory_address ();
9587 if(file_check_valid_memory
< 0) {
9590 write (file_check_valid_memory
, (gpointer
)addr
, 1);
9591 if (errno
== EFAULT
) {
9596 gboolean ret
= FALSE
;
9598 for (i
= 0; i
< size
; i
++)
9599 *((volatile char*)addr
+i
);
9609 pointer_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9614 MonoDomain
* domain
= NULL
;
9615 MonoType
*type
= NULL
;
9620 case CMD_POINTER_GET_VALUE
:
9621 addr
= decode_long (p
, &p
, end
);
9622 klass
= decode_typeid (p
, &p
, end
, &domain
, &err
);
9623 if (err
!= ERR_NONE
)
9626 if (m_class_get_byval_arg (klass
)->type
!= MONO_TYPE_PTR
)
9627 return ERR_INVALID_ARGUMENT
;
9629 type
= m_class_get_byval_arg (m_class_get_element_class (klass
));
9630 size
= mono_type_size (type
, &align
);
9632 if (!valid_memory_address((gpointer
)addr
, size
))
9633 return ERR_INVALID_ARGUMENT
;
9635 buffer_add_value (buf
, type
, (gpointer
)addr
, domain
);
9639 return ERR_NOT_IMPLEMENTED
;
9646 object_commands (int command
, guint8
*p
, guint8
*end
, Buffer
*buf
)
9648 HANDLE_FUNCTION_ENTER ();
9658 gboolean remote_obj
= FALSE
;
9659 MonoStringHandle string_handle
= MONO_HANDLE_NEW (MonoString
, NULL
); // FIXME? Not always needed.
9661 if (command
== CMD_OBJECT_REF_IS_COLLECTED
) {
9662 objid
= decode_objid (p
, &p
, end
);
9663 err
= get_object (objid
, &obj
);
9664 if (err
!= ERR_NONE
)
9665 buffer_add_int (buf
, 1);
9667 buffer_add_int (buf
, 0);
9672 objid
= decode_objid (p
, &p
, end
);
9673 err
= get_object (objid
, &obj
);
9674 if (err
!= ERR_NONE
)
9677 MonoClass
*obj_type
;
9679 obj_type
= obj
->vtable
->klass
;
9680 if (mono_class_is_transparent_proxy (obj_type
)) {
9681 obj_type
= ((MonoTransparentProxy
*)obj
)->remote_class
->proxy_class
;
9685 g_assert (obj_type
);
9688 case CMD_OBJECT_REF_GET_TYPE
:
9689 /* This handles transparent proxies too */
9690 buffer_add_typeid (buf
, obj
->vtable
->domain
, mono_class_from_mono_type_internal (((MonoReflectionType
*)obj
->vtable
->type
)->type
));
9692 case CMD_OBJECT_REF_GET_VALUES
:
9693 len
= decode_int (p
, &p
, end
);
9695 for (i
= 0; i
< len
; ++i
) {
9696 MonoClassField
*f
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
9697 if (err
!= ERR_NONE
)
9700 /* Check that the field belongs to the object */
9702 for (k
= obj_type
; k
; k
= m_class_get_parent (k
)) {
9703 if (k
== f
->parent
) {
9709 goto invalid_fieldid
;
9711 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
) {
9715 if (mono_class_field_is_special_static (f
))
9716 goto invalid_fieldid
;
9718 g_assert (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
);
9719 vtable
= mono_class_vtable_checked (obj
->vtable
->domain
, f
->parent
, error
);
9720 if (!is_ok (error
)) {
9721 mono_error_cleanup (error
);
9722 goto invalid_object
;
9724 val
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type_internal (f
->type
)));
9725 mono_field_static_get_value_checked (vtable
, f
, val
, string_handle
, error
);
9726 if (!is_ok (error
)) {
9727 mono_error_cleanup (error
); /* FIXME report the error */
9728 goto invalid_object
;
9730 buffer_add_value (buf
, f
->type
, val
, obj
->vtable
->domain
);
9733 void *field_value
= NULL
;
9734 #ifndef DISABLE_REMOTING
9735 void *field_storage
= NULL
;
9738 #ifndef DISABLE_REMOTING
9739 field_value
= mono_load_remote_field_checked(obj
, obj_type
, f
, &field_storage
, error
);
9740 if (!is_ok (error
)) {
9741 mono_error_cleanup (error
); /* FIXME report the error */
9742 goto invalid_object
;
9745 g_assert_not_reached ();
9748 field_value
= (guint8
*)obj
+ f
->offset
;
9750 buffer_add_value (buf
, f
->type
, field_value
, obj
->vtable
->domain
);
9754 case CMD_OBJECT_REF_SET_VALUES
:
9755 len
= decode_int (p
, &p
, end
);
9757 for (i
= 0; i
< len
; ++i
) {
9758 f
= decode_fieldid (p
, &p
, end
, NULL
, &err
);
9759 if (err
!= ERR_NONE
)
9762 /* Check that the field belongs to the object */
9764 for (k
= obj_type
; k
; k
= m_class_get_parent (k
)) {
9765 if (k
== f
->parent
) {
9771 goto invalid_fieldid
;
9773 if (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
) {
9777 if (mono_class_field_is_special_static (f
))
9778 goto invalid_fieldid
;
9780 g_assert (f
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
);
9781 vtable
= mono_class_vtable_checked (obj
->vtable
->domain
, f
->parent
, error
);
9782 if (!is_ok (error
)) {
9783 mono_error_cleanup (error
);
9784 goto invalid_fieldid
;
9787 val
= (guint8
*)g_malloc (mono_class_instance_size (mono_class_from_mono_type_internal (f
->type
)));
9788 err
= decode_value (f
->type
, obj
->vtable
->domain
, val
, p
, &p
, end
, TRUE
);
9789 if (err
!= ERR_NONE
) {
9793 mono_field_static_set_value_internal (vtable
, f
, val
);
9796 err
= decode_value (f
->type
, obj
->vtable
->domain
, (guint8
*)obj
+ f
->offset
, p
, &p
, end
, TRUE
);
9797 if (err
!= ERR_NONE
)
9802 case CMD_OBJECT_REF_GET_ADDRESS
:
9803 buffer_add_long (buf
, (gssize
)obj
);
9805 case CMD_OBJECT_REF_GET_DOMAIN
:
9806 buffer_add_domainid (buf
, obj
->vtable
->domain
);
9808 case CMD_OBJECT_REF_GET_INFO
:
9809 buffer_add_typeid (buf
, obj
->vtable
->domain
, mono_class_from_mono_type_internal (((MonoReflectionType
*)obj
->vtable
->type
)->type
));
9810 buffer_add_domainid (buf
, obj
->vtable
->domain
);
9813 err
= ERR_NOT_IMPLEMENTED
;
9820 err
= ERR_INVALID_FIELDID
;
9823 err
= ERR_INVALID_OBJECT
;
9826 HANDLE_FUNCTION_RETURN_VAL (err
);
9830 command_set_to_string (CommandSet command_set
)
9832 switch (command_set
) {
9835 case CMD_SET_OBJECT_REF
:
9836 return "OBJECT_REF";
9837 case CMD_SET_STRING_REF
:
9838 return "STRING_REF";
9839 case CMD_SET_THREAD
:
9841 case CMD_SET_ARRAY_REF
:
9843 case CMD_SET_EVENT_REQUEST
:
9844 return "EVENT_REQUEST";
9845 case CMD_SET_STACK_FRAME
:
9846 return "STACK_FRAME";
9847 case CMD_SET_APPDOMAIN
:
9849 case CMD_SET_ASSEMBLY
:
9851 case CMD_SET_METHOD
:
9855 case CMD_SET_MODULE
:
9861 case CMD_SET_POINTER
:
9868 static const char* vm_cmds_str
[] = {
9876 "SET_PROTOCOL_VERSION",
9879 "GET_TYPES_FOR_SOURCE_FILE",
9884 static const char* thread_cmds_str
[] = {
9894 static const char* event_cmds_str
[] = {
9897 "REQUEST_CLEAR_ALL_BREAKPOINTS"
9900 static const char* appdomain_cmds_str
[] = {
9902 "GET_FRIENDLY_NAME",
9904 "GET_ENTRY_ASSEMBLY",
9907 "CREATE_BOXED_VALUE",
9908 "CREATE_BYTE_ARRAY",
9911 static const char* assembly_cmds_str
[] = {
9914 "GET_MANIFEST_MODULE",
9922 static const char* module_cmds_str
[] = {
9926 static const char* field_cmds_str
[] = {
9930 static const char* method_cmds_str
[] = {
9932 "GET_DECLARING_TYPE",
9940 "MAKE_GENERIC_METHOD"
9943 static const char* type_cmds_str
[] = {
9951 "IS_ASSIGNABLE_FROM",
9955 "GET_PROPERTY_CATTRS",
9956 "GET_SOURCE_FILES_2",
9958 "GET_METHODS_BY_NAME_FLAGS",
9960 "GET_INTERFACE_MAP",
9966 static const char* stack_frame_cmds_str
[] = {
9974 static const char* array_cmds_str
[] = {
9980 static const char* string_cmds_str
[] = {
9986 static const char* pointer_cmds_str
[] = {
9990 static const char* object_cmds_str
[] = {
10001 cmd_to_string (CommandSet set
, int command
)
10008 cmds
= vm_cmds_str
;
10009 cmds_len
= G_N_ELEMENTS (vm_cmds_str
);
10011 case CMD_SET_OBJECT_REF
:
10012 cmds
= object_cmds_str
;
10013 cmds_len
= G_N_ELEMENTS (object_cmds_str
);
10015 case CMD_SET_STRING_REF
:
10016 cmds
= string_cmds_str
;
10017 cmds_len
= G_N_ELEMENTS (string_cmds_str
);
10019 case CMD_SET_THREAD
:
10020 cmds
= thread_cmds_str
;
10021 cmds_len
= G_N_ELEMENTS (thread_cmds_str
);
10023 case CMD_SET_ARRAY_REF
:
10024 cmds
= array_cmds_str
;
10025 cmds_len
= G_N_ELEMENTS (array_cmds_str
);
10027 case CMD_SET_EVENT_REQUEST
:
10028 cmds
= event_cmds_str
;
10029 cmds_len
= G_N_ELEMENTS (event_cmds_str
);
10031 case CMD_SET_STACK_FRAME
:
10032 cmds
= stack_frame_cmds_str
;
10033 cmds_len
= G_N_ELEMENTS (stack_frame_cmds_str
);
10035 case CMD_SET_APPDOMAIN
:
10036 cmds
= appdomain_cmds_str
;
10037 cmds_len
= G_N_ELEMENTS (appdomain_cmds_str
);
10039 case CMD_SET_ASSEMBLY
:
10040 cmds
= assembly_cmds_str
;
10041 cmds_len
= G_N_ELEMENTS (assembly_cmds_str
);
10043 case CMD_SET_METHOD
:
10044 cmds
= method_cmds_str
;
10045 cmds_len
= G_N_ELEMENTS (method_cmds_str
);
10048 cmds
= type_cmds_str
;
10049 cmds_len
= G_N_ELEMENTS (type_cmds_str
);
10051 case CMD_SET_MODULE
:
10052 cmds
= module_cmds_str
;
10053 cmds_len
= G_N_ELEMENTS (module_cmds_str
);
10055 case CMD_SET_FIELD
:
10056 cmds
= field_cmds_str
;
10057 cmds_len
= G_N_ELEMENTS (field_cmds_str
);
10059 case CMD_SET_EVENT
:
10060 cmds
= event_cmds_str
;
10061 cmds_len
= G_N_ELEMENTS (event_cmds_str
);
10063 case CMD_SET_POINTER
:
10064 cmds
= pointer_cmds_str
;
10065 cmds_len
= G_N_ELEMENTS (pointer_cmds_str
);
10070 if (command
> 0 && command
<= cmds_len
)
10071 return cmds
[command
- 1];
10077 wait_for_attach (void)
10079 #ifndef DISABLE_SOCKET_TRANSPORT
10080 if (listen_fd
== -1) {
10081 DEBUG_PRINTF (1, "[dbg] Invalid listening socket\n");
10085 /* Block and wait for client connection */
10086 conn_fd
= socket_transport_accept (listen_fd
);
10088 DEBUG_PRINTF (1, "Accepted connection on %d\n", conn_fd
);
10089 if (conn_fd
== -1) {
10090 DEBUG_PRINTF (1, "[dbg] Bad client connection\n");
10094 g_assert_not_reached ();
10098 disconnected
= !transport_handshake ();
10099 if (disconnected
) {
10100 DEBUG_PRINTF (1, "Transport handshake failed!\n");
10110 * This thread handles communication with the debugger client using a JDWP
10113 static gsize WINAPI
10114 debugger_thread (void *arg
)
10116 int res
, len
, id
, flags
, command
= 0;
10117 CommandSet command_set
= (CommandSet
)0;
10118 guint8 header
[HEADER_LENGTH
];
10119 guint8
*data
, *p
, *end
;
10123 gboolean attach_failed
= FALSE
;
10125 DEBUG_PRINTF (1, "[dbg] Agent thread started, pid=%p\n", (gpointer
) (gsize
) mono_native_thread_id_get ());
10127 gboolean log_each_step
= g_hasenv ("MONO_DEBUGGER_LOG_AFTER_COMMAND");
10129 debugger_thread_id
= mono_native_thread_id_get ();
10131 MonoInternalThread
*internal
= mono_thread_internal_current ();
10132 mono_thread_set_name_constant_ignore_error (internal
, "Debugger agent", MonoSetThreadNameFlag_Permanent
);
10134 internal
->state
|= ThreadState_Background
;
10135 internal
->flags
|= MONO_THREAD_FLAG_DONT_MANAGE
;
10137 if (agent_config
.defer
) {
10138 if (!wait_for_attach ()) {
10139 DEBUG_PRINTF (1, "[dbg] Can't attach, aborting debugger thread.\n");
10140 attach_failed
= TRUE
; // Don't abort process when we can't listen
10142 mono_set_is_debugger_attached (TRUE
);
10143 /* Send start event to client */
10144 process_profiler_event (EVENT_KIND_VM_START
, mono_thread_get_main ());
10147 mono_set_is_debugger_attached (TRUE
);
10150 while (!attach_failed
) {
10151 res
= transport_recv (header
, HEADER_LENGTH
);
10153 /* This will break if the socket is closed during shutdown too */
10154 if (res
!= HEADER_LENGTH
) {
10155 DEBUG_PRINTF (1, "[dbg] transport_recv () returned %d, expected %d.\n", res
, HEADER_LENGTH
);
10156 command_set
= (CommandSet
)0;
10162 end
= header
+ HEADER_LENGTH
;
10164 len
= decode_int (p
, &p
, end
);
10165 id
= decode_int (p
, &p
, end
);
10166 flags
= decode_byte (p
, &p
, end
);
10167 command_set
= (CommandSet
)decode_byte (p
, &p
, end
);
10168 command
= decode_byte (p
, &p
, end
);
10171 g_assert (flags
== 0);
10172 const char *cmd_str
;
10173 char cmd_num
[256];
10175 cmd_str
= cmd_to_string (command_set
, command
);
10177 sprintf (cmd_num
, "%d", command
);
10182 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);
10185 data
= (guint8
*)g_malloc (len
- HEADER_LENGTH
);
10186 if (len
- HEADER_LENGTH
> 0)
10188 res
= transport_recv (data
, len
- HEADER_LENGTH
);
10189 if (res
!= len
- HEADER_LENGTH
) {
10190 DEBUG_PRINTF (1, "[dbg] transport_recv () returned %d, expected %d.\n", res
, len
- HEADER_LENGTH
);
10196 end
= data
+ (len
- HEADER_LENGTH
);
10198 buffer_init (&buf
, 128);
10203 /* Process the request */
10204 switch (command_set
) {
10206 err
= vm_commands (command
, id
, p
, end
, &buf
);
10207 if (err
== ERR_NONE
&& command
== CMD_VM_INVOKE_METHOD
)
10208 /* Sent after the invoke is complete */
10211 case CMD_SET_EVENT_REQUEST
:
10212 err
= event_commands (command
, p
, end
, &buf
);
10214 case CMD_SET_APPDOMAIN
:
10215 err
= domain_commands (command
, p
, end
, &buf
);
10217 case CMD_SET_ASSEMBLY
:
10218 err
= assembly_commands (command
, p
, end
, &buf
);
10220 case CMD_SET_MODULE
:
10221 err
= module_commands (command
, p
, end
, &buf
);
10223 case CMD_SET_FIELD
:
10224 err
= field_commands (command
, p
, end
, &buf
);
10227 err
= type_commands (command
, p
, end
, &buf
);
10229 case CMD_SET_METHOD
:
10230 err
= method_commands (command
, p
, end
, &buf
);
10232 case CMD_SET_THREAD
:
10233 err
= thread_commands (command
, p
, end
, &buf
);
10235 case CMD_SET_STACK_FRAME
:
10236 err
= frame_commands (command
, p
, end
, &buf
);
10238 case CMD_SET_ARRAY_REF
:
10239 err
= array_commands (command
, p
, end
, &buf
);
10241 case CMD_SET_STRING_REF
:
10242 err
= string_commands (command
, p
, end
, &buf
);
10244 case CMD_SET_POINTER
:
10245 err
= pointer_commands (command
, p
, end
, &buf
);
10247 case CMD_SET_OBJECT_REF
:
10248 err
= object_commands (command
, p
, end
, &buf
);
10251 err
= ERR_NOT_IMPLEMENTED
;
10254 if (command_set
== CMD_SET_VM
&& command
== CMD_VM_START_BUFFERING
) {
10255 buffer_replies
= TRUE
;
10259 if (buffer_replies
) {
10260 buffer_reply_packet (id
, err
, &buf
);
10262 send_reply_packet (id
, err
, &buf
);
10263 //DEBUG_PRINTF (1, "[dbg] Sent reply to %d [at=%lx].\n", id, (long)mono_100ns_ticks () / 10000);
10267 mono_debugger_log_command (command_set_to_string (command_set
), cmd_str
, buf
.buf
, buffer_len (&buf
));
10269 if (err
== ERR_NONE
&& command_set
== CMD_SET_VM
&& command
== CMD_VM_STOP_BUFFERING
) {
10270 send_buffered_reply_packets ();
10271 buffer_replies
= FALSE
;
10275 buffer_free (&buf
);
10277 if (log_each_step
) {
10278 char *debugger_log
= mono_debugger_state_str ();
10279 if (debugger_log
) {
10280 fprintf (stderr
, "Debugger state: %s\n", debugger_log
);
10281 g_free (debugger_log
);
10285 if (command_set
== CMD_SET_VM
&& (command
== CMD_VM_DISPOSE
|| command
== CMD_VM_EXIT
))
10289 mono_set_is_debugger_attached (FALSE
);
10291 mono_coop_mutex_lock (&debugger_thread_exited_mutex
);
10292 debugger_thread_exited
= TRUE
;
10293 mono_coop_cond_signal (&debugger_thread_exited_cond
);
10294 mono_coop_mutex_unlock (&debugger_thread_exited_mutex
);
10296 DEBUG_PRINTF (1, "[dbg] Debugger thread exited.\n");
10298 if (!attach_failed
&& command_set
== CMD_SET_VM
&& command
== CMD_VM_DISPOSE
&& !(vm_death_event_sent
|| mono_runtime_is_shutting_down ())) {
10299 DEBUG_PRINTF (2, "[dbg] Detached - restarting clean debugger thread.\n");
10300 ERROR_DECL (error
);
10301 start_debugger_thread (error
);
10302 mono_error_cleanup (error
);
10309 mono_debugger_agent_init (void)
10311 MonoDebuggerCallbacks cbs
;
10313 memset (&cbs
, 0, sizeof (cbs
));
10314 cbs
.version
= MONO_DBG_CALLBACKS_VERSION
;
10315 cbs
.parse_options
= debugger_agent_parse_options
;
10316 cbs
.init
= debugger_agent_init
;
10317 cbs
.breakpoint_hit
= debugger_agent_breakpoint_hit
;
10318 cbs
.single_step_event
= debugger_agent_single_step_event
;
10319 cbs
.single_step_from_context
= debugger_agent_single_step_from_context
;
10320 cbs
.breakpoint_from_context
= debugger_agent_breakpoint_from_context
;
10321 cbs
.free_domain_info
= debugger_agent_free_domain_info
;
10322 cbs
.unhandled_exception
= debugger_agent_unhandled_exception
;
10323 cbs
.handle_exception
= debugger_agent_handle_exception
;
10324 cbs
.begin_exception_filter
= debugger_agent_begin_exception_filter
;
10325 cbs
.end_exception_filter
= debugger_agent_end_exception_filter
;
10326 cbs
.user_break
= debugger_agent_user_break
;
10327 cbs
.debug_log
= debugger_agent_debug_log
;
10328 cbs
.debug_log_is_enabled
= debugger_agent_debug_log_is_enabled
;
10329 cbs
.send_crash
= mono_debugger_agent_send_crash
;
10331 mini_install_dbg_callbacks (&cbs
);
10335 mono_debugger_agent_parse_options (char *options
)
10337 sdb_options
= options
;
10340 #endif /* DISABLE_SDB */