[crash] Crash Reporter V2 (#10532)
[mono-project.git] / mono / mini / mini-runtime.h
blobce49a570ee67594e4f5c5d3def5921ac3df45690
1 /**
2 * \file
4 * Runtime declarations for the JIT.
6 * Copyright 2002-2003 Ximian Inc
7 * Copyright 2003-2011 Novell Inc
8 * Copyright 2011 Xamarin Inc
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12 #ifndef __MONO_MINI_RUNTIME_H__
13 #define __MONO_MINI_RUNTIME_H__
15 #include "mini.h"
16 #include "ee.h"
18 /* Per-domain information maintained by the JIT */
19 typedef struct
21 /* Maps MonoMethod's to a GSList of GOT slot addresses pointing to its code */
22 GHashTable *jump_target_got_slot_hash;
23 GHashTable *jump_target_hash;
24 /* Maps methods/klasses to the address of the given type of trampoline */
25 GHashTable *class_init_trampoline_hash;
26 GHashTable *jump_trampoline_hash;
27 GHashTable *jit_trampoline_hash;
28 GHashTable *delegate_trampoline_hash;
29 /* Maps ClassMethodPair -> MonoDelegateTrampInfo */
30 GHashTable *static_rgctx_trampoline_hash;
31 GHashTable *llvm_vcall_trampoline_hash;
32 /* maps MonoMethod -> MonoJitDynamicMethodInfo */
33 GHashTable *dynamic_code_hash;
34 GHashTable *method_code_hash;
35 /* Maps methods to a RuntimeInvokeInfo structure, protected by the associated MonoDomain lock */
36 MonoConcurrentHashTable *runtime_invoke_hash;
37 /* Maps MonoMethod to a GPtrArray containing sequence point locations */
38 /* Protected by the domain lock */
39 GHashTable *seq_points;
40 /* Debugger agent data */
41 gpointer agent_info;
42 /* Maps MonoMethod to an arch-specific structure */
43 GHashTable *arch_seq_points;
44 /* Maps a GSharedVtTrampInfo structure to a trampoline address */
45 GHashTable *gsharedvt_arg_tramp_hash;
46 /* memcpy/bzero methods specialized for small constant sizes */
47 gpointer *memcpy_addr [17];
48 gpointer *bzero_addr [17];
49 gpointer llvm_module;
50 /* Maps MonoMethod -> GSlist of addresses */
51 GHashTable *llvm_jit_callees;
52 /* Maps MonoMethod -> RuntimeMethod */
53 MonoInternalHashTable interp_code_hash;
54 /* Maps MonoMethod -> MonoMethodRuntimeGenericContext */
55 GHashTable *mrgctx_hash;
56 GHashTable *method_rgctx_hash;
57 /* Maps gpointer -> InterpMethod */
58 GHashTable *interp_method_pointer_hash;
59 } MonoJitDomainInfo;
61 #define domain_jit_info(domain) ((MonoJitDomainInfo*)((domain)->runtime_info))
64 * Stores state need to resume exception handling when using LLVM
66 typedef struct {
67 MonoJitInfo *ji;
68 int clause_index;
69 MonoContext ctx, new_ctx;
70 /* FIXME: GC */
71 gpointer ex_obj;
72 MonoLMF *lmf;
73 int first_filter_idx, filter_idx;
74 } ResumeState;
76 typedef void (*MonoAbortFunction)(MonoObject*);
78 struct MonoJitTlsData {
79 gpointer end_of_stack;
80 guint32 stack_size;
81 MonoLMF *lmf;
82 MonoLMF *first_lmf;
83 guint handling_stack_ovf : 1;
84 gpointer signal_stack;
85 guint32 signal_stack_size;
86 gpointer stack_ovf_guard_base;
87 guint32 stack_ovf_guard_size;
88 guint stack_ovf_valloced : 1;
89 guint stack_ovf_pending : 1;
90 MonoAbortFunction abort_func;
91 /* Used to implement --debug=casts */
92 MonoClass *class_cast_from, *class_cast_to;
94 /* Stores state needed by handler block with a guard */
95 MonoContext ex_ctx;
96 ResumeState resume_state;
98 /* handler block been guarded. It's safe to store this even for dynamic methods since there
99 is an activation on stack making sure it will remain alive.*/
100 MonoJitExceptionInfo *handler_block;
102 /* context to be used by the guard trampoline when resuming interruption.*/
103 MonoContext handler_block_context;
105 * Stores the state at the exception throw site to be used by mono_stack_walk ()
106 * when it is called from profiler functions during exception handling.
108 MonoContext orig_ex_ctx;
109 gboolean orig_ex_ctx_set;
112 * Stores if we need to run a chained exception in Windows.
114 gboolean mono_win_chained_exception_needs_run;
117 * The current exception in flight
119 guint32 thrown_exc;
121 * If the current exception is not a subclass of Exception,
122 * the original exception.
124 guint32 thrown_non_exc;
127 * The calling assembly in llvmonly mode.
129 MonoImage *calling_image;
132 * The stack frame "high water mark" for ThreadAbortExceptions.
133 * We will rethrow the exception upon exiting a catch clause that's
134 * in a function stack frame above the water mark(isn't being called by
135 * the catch block that caught the ThreadAbortException).
137 gpointer abort_exc_stack_threshold;
140 * List of methods being JIT'd in the current thread.
142 int active_jit_methods;
144 gpointer interp_context;
146 #if defined(TARGET_WIN32)
147 MonoContext stack_restore_ctx;
148 #endif
151 #define MONO_LMFEXT_DEBUGGER_INVOKE 1
152 #define MONO_LMFEXT_INTERP_EXIT 2
153 #define MONO_LMFEXT_INTERP_EXIT_WITH_CTX 3
156 * This structure is an extension of MonoLMF and contains extra information.
158 typedef struct {
159 struct MonoLMF lmf;
160 int kind;
161 MonoContext ctx; /* valid if kind == DEBUGGER_INVOKE || kind == INTERP_EXIT_WITH_CTX */
162 gpointer interp_exit_data; /* valid if kind == INTERP_EXIT || kind == INTERP_EXIT_WITH_CTX */
163 } MonoLMFExt;
165 typedef void (*MonoFtnPtrEHCallback) (guint32 gchandle);
167 typedef struct MonoDebugOptions {
168 gboolean handle_sigint;
169 gboolean keep_delegates;
170 gboolean reverse_pinvoke_exceptions;
171 gboolean collect_pagefault_stats;
172 gboolean break_on_unverified;
173 gboolean better_cast_details;
174 gboolean mdb_optimizations;
175 gboolean no_gdb_backtrace;
176 gboolean suspend_on_native_crash;
177 gboolean suspend_on_exception;
178 gboolean suspend_on_unhandled;
179 gboolean dyn_runtime_invoke;
180 gboolean gdb;
181 gboolean lldb;
182 gboolean use_fallback_tls;
184 * Whenever data such as next sequence points and flags is required.
185 * Next sequence points and flags are required by the debugger agent.
187 gboolean gen_sdb_seq_points;
188 gboolean no_seq_points_compact_data;
190 * Setting single_imm_size should guarantee that each time managed code is compiled
191 * the same instructions and registers are used, regardless of the size of used values.
193 gboolean single_imm_size;
194 gboolean explicit_null_checks;
196 * Fill stack frames with 0x2a in method prologs. This helps with the
197 * debugging of the stack marking code in the GC.
199 gboolean init_stacks;
202 * Whenever to implement single stepping and breakpoints without signals in the
203 * soft debugger. This is useful on platforms without signals, like the ps3, or during
204 * runtime debugging, since it avoids SIGSEGVs when a single step location or breakpoint
205 * is hit.
207 gboolean soft_breakpoints;
209 * Whenever to break in the debugger using G_BREAKPOINT on unhandled exceptions.
211 gboolean break_on_exc;
213 * Load AOT JIT info eagerly.
215 gboolean load_aot_jit_info_eagerly;
217 * Check for pinvoke calling convention mismatches.
219 gboolean check_pinvoke_callconv;
221 * Translate Debugger.Break () into a native breakpoint signal
223 gboolean native_debugger_break;
225 * Disabling the frame pointer emit optimization can allow debuggers to more easily
226 * identify the stack on some platforms
228 gboolean disable_omit_fp;
230 * Make gdb output on native crashes more verbose.
232 gboolean verbose_gdb;
234 // Internal testing feature.
235 gboolean test_tailcall_require;
238 * Internal testing feature
239 * Testing feature, skip loading the Nth aot loadable method.
241 gboolean aot_skip_set;
242 int aot_skip;
243 } MonoDebugOptions;
247 * We need to store the image which the token refers to along with the token,
248 * since the image might not be the same as the image of the method which
249 * contains the relocation, because of inlining.
251 typedef struct MonoJumpInfoToken {
252 MonoImage *image;
253 guint32 token;
254 gboolean has_context;
255 MonoGenericContext context;
256 } MonoJumpInfoToken;
258 typedef struct MonoJumpInfoBBTable {
259 MonoBasicBlock **table;
260 int table_size;
261 } MonoJumpInfoBBTable;
263 /* Contains information describing an LLVM IMT trampoline */
264 typedef struct MonoJumpInfoImtTramp {
265 MonoMethod *method;
266 int vt_offset;
267 } MonoJumpInfoImtTramp;
270 * Contains information for computing the
271 * property given by INFO_TYPE of the runtime
272 * object described by DATA.
274 struct MonoJumpInfoRgctxEntry {
275 MonoMethod *method;
276 gboolean in_mrgctx;
277 MonoJumpInfo *data; /* describes the data to be loaded */
278 MonoRgctxInfoType info_type;
281 /* Contains information about a gsharedvt call */
282 struct MonoJumpInfoGSharedVtCall {
283 /* The original signature of the call */
284 MonoMethodSignature *sig;
285 /* The method which is called */
286 MonoMethod *method;
290 * Represents the method which is called when a virtual call is made to METHOD
291 * on a receiver of type KLASS.
293 typedef struct {
294 /* Receiver class */
295 MonoClass *klass;
296 /* Virtual method */
297 MonoMethod *method;
298 } MonoJumpInfoVirtMethod;
300 struct MonoJumpInfo {
301 MonoJumpInfo *next;
302 /* Relocation type for patching */
303 int relocation;
304 union {
305 int i;
306 guint8 *p;
307 MonoInst *label;
308 } ip;
310 MonoJumpInfoType type;
311 union {
312 gconstpointer target;
313 #if TARGET_SIZEOF_VOID_P == 8
314 gint64 offset;
315 #else
316 int offset;
317 #endif
318 int index;
319 MonoBasicBlock *bb;
320 MonoInst *inst;
321 MonoMethod *method;
322 MonoClass *klass;
323 MonoClassField *field;
324 MonoImage *image;
325 MonoVTable *vtable;
326 const char *name;
327 MonoJumpInfoToken *token;
328 MonoJumpInfoBBTable *table;
329 MonoJumpInfoRgctxEntry *rgctx_entry;
330 MonoJumpInfoImtTramp *imt_tramp;
331 MonoJumpInfoGSharedVtCall *gsharedvt;
332 MonoGSharedVtMethodInfo *gsharedvt_method;
333 MonoMethodSignature *sig;
334 MonoDelegateClassMethodPair *del_tramp;
335 /* MONO_PATCH_INFO_VIRT_METHOD */
336 MonoJumpInfoVirtMethod *virt_method;
337 } data;
340 extern gboolean mono_break_on_exc;
341 extern gboolean mono_compile_aot;
342 extern gboolean mono_aot_only;
343 extern gboolean mono_llvm_only;
344 extern MonoAotMode mono_aot_mode;
345 MONO_API_DATA const char *mono_build_date;
346 extern gboolean mono_do_signal_chaining;
347 extern gboolean mono_do_crash_chaining;
348 MONO_API_DATA gboolean mono_use_llvm;
349 MONO_API_DATA gboolean mono_use_interpreter;
350 extern const char* mono_interp_opts_string;
351 extern gboolean mono_do_single_method_regression;
352 extern guint32 mono_single_method_regression_opt;
353 extern MonoMethod *mono_current_single_method;
354 extern GSList *mono_single_method_list;
355 extern GHashTable *mono_single_method_hash;
356 extern GList* mono_aot_paths;
357 extern MonoDebugOptions mini_debug_options;
358 extern GSList *mono_interp_only_classes;
359 extern char *sdb_options;
362 This struct describes what execution engine feature to use.
363 This subsume, and will eventually sunset, mono_aot_only / mono_llvm_only and friends.
364 The goal is to transition us to a place were we can more easily compose/describe what features we need for a given execution mode.
366 A good feature flag is checked alone, a bad one described many things and keeps breaking some of the modes
368 typedef struct {
370 * If true, trampolines are to be fetched from the AOT runtime instead of JIT compiled
372 gboolean use_aot_trampolines;
375 * If true, the runtime will try to use the interpreter before looking for compiled code.
377 gboolean force_use_interpreter;
378 } MonoEEFeatures;
380 extern MonoEEFeatures mono_ee_features;
382 //XXX this enum *MUST extend MonoAotMode as they are consumed together.
383 typedef enum {
384 /* Always execute with interp, will use JIT to produce trampolines */
385 MONO_EE_MODE_INTERP = MONO_AOT_MODE_LAST,
386 } MonoEEMode;
389 static inline MonoMethod*
390 jinfo_get_method (MonoJitInfo *ji)
392 return mono_jit_info_get_method (ji);
395 /* main function */
396 MONO_API int mono_main (int argc, char* argv[]);
397 MONO_API void mono_set_defaults (int verbose_level, guint32 opts);
398 MONO_API void mono_parse_env_options (int *ref_argc, char **ref_argv []);
399 MONO_API char *mono_parse_options_from (const char *options, int *ref_argc, char **ref_argv []);
400 MONO_API int mono_regression_test_step (int verbose_level, const char *image, const char *method_name);
403 void mono_interp_stub_init (void);
404 void mini_install_interp_callbacks (MonoEECallbacks *cbs);
405 MonoEECallbacks* mini_get_interp_callbacks (void);
407 typedef struct _MonoDebuggerCallbacks MonoDebuggerCallbacks;
409 void mini_install_dbg_callbacks (MonoDebuggerCallbacks *cbs);
410 MonoDebuggerCallbacks *mini_get_dbg_callbacks (void);
412 MonoDomain* mini_init (const char *filename, const char *runtime_version);
413 void mini_cleanup (MonoDomain *domain);
414 MONO_API MonoDebugOptions *mini_get_debug_options (void);
415 MONO_API gboolean mini_parse_debug_option (const char *option);
417 MONO_API void
418 mono_install_ftnptr_eh_callback (MonoFtnPtrEHCallback callback);
420 void mini_jit_init (void);
421 void mini_jit_cleanup (void);
422 void mono_disable_optimizations (guint32 opts);
423 void mono_set_optimizations (guint32 opts);
424 void mono_precompile_assemblies (void);
425 MONO_API int mono_parse_default_optimizations (const char* p);
426 gboolean mono_running_on_valgrind (void);
428 MonoLMF * mono_get_lmf (void);
429 #define mono_get_lmf_addr mono_tls_get_lmf_addr
430 MonoLMF** mono_get_lmf_addr (void);
431 void mono_set_lmf (MonoLMF *lmf);
432 void mono_push_lmf (MonoLMFExt *ext);
433 void mono_pop_lmf (MonoLMF *lmf);
434 #define mono_get_jit_tls mono_tls_get_jit_tls
435 MonoJitTlsData* mono_get_jit_tls (void);
436 MONO_API MONO_RT_EXTERNAL_ONLY
437 MonoDomain* mono_jit_thread_attach (MonoDomain *domain);
438 MONO_API void mono_jit_set_domain (MonoDomain *domain);
440 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee);
441 gpointer mono_create_ftnptr (MonoDomain *domain, gpointer addr);
442 gconstpointer mono_icall_get_wrapper (MonoJitICallInfo* callinfo) MONO_LLVM_INTERNAL;
443 gconstpointer mono_icall_get_wrapper_full (MonoJitICallInfo* callinfo, gboolean do_compile);
445 MonoJumpInfo* mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info);
446 guint mono_patch_info_hash (gconstpointer data);
447 gint mono_patch_info_equal (gconstpointer ka, gconstpointer kb);
448 MonoJumpInfo *mono_patch_info_list_prepend (MonoJumpInfo *list, int ip, MonoJumpInfoType type, gconstpointer target);
449 MonoJumpInfoToken* mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token);
450 MonoJumpInfoToken* mono_jump_info_token_new2 (MonoMemPool *mp, MonoImage *image, guint32 token, MonoGenericContext *context);
451 gpointer mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors, MonoError *error) MONO_LLVM_INTERNAL;
452 void mini_register_jump_site (MonoDomain *domain, MonoMethod *method, gpointer ip);
453 void mini_patch_jump_sites (MonoDomain *domain, MonoMethod *method, gpointer addr);
454 gpointer mono_jit_search_all_backends_for_jit_info (MonoDomain *domain, MonoMethod *method, MonoJitInfo **ji);
455 gpointer mono_jit_find_compiled_method_with_jit_info (MonoDomain *domain, MonoMethod *method, MonoJitInfo **ji);
456 gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
457 gpointer mono_jit_compile_method (MonoMethod *method, MonoError *error);
458 gpointer mono_jit_compile_method_jit_only (MonoMethod *method, MonoError *error);
460 void mono_set_bisect_methods (guint32 opt, const char *method_list_filename);
461 guint32 mono_get_optimizations_for_method (MonoMethod *method, guint32 default_opt);
462 char* mono_opt_descr (guint32 flags);
463 void mono_set_verbose_level (guint32 level);
464 const char*mono_ji_type_to_string (MonoJumpInfoType type) MONO_LLVM_INTERNAL;
465 void mono_print_ji (const MonoJumpInfo *ji);
466 MONO_API void mono_print_method_from_ip (void *ip);
467 MONO_API char *mono_pmip (void *ip);
468 MONO_API int mono_ee_api_version (void);
469 gboolean mono_debug_count (void);
471 #ifdef __linux__
472 #define XDEBUG_ENABLED 1
473 #endif
475 #ifdef __linux__
476 /* maybe enable also for other systems? */
477 #define ENABLE_JIT_MAP 1
478 void mono_enable_jit_map (void);
479 void mono_emit_jit_map (MonoJitInfo *jinfo);
480 void mono_emit_jit_tramp (void *start, int size, const char *desc);
481 gboolean mono_jit_map_is_enabled (void);
482 #else
483 #define mono_enable_jit_map()
484 #define mono_emit_jit_map(ji)
485 #define mono_emit_jit_tramp(s,z,d)
486 #define mono_jit_map_is_enabled() (0)
487 #endif
490 * Per-OS implementation functions.
492 void
493 mono_runtime_install_handlers (void);
495 gboolean
496 mono_runtime_install_custom_handlers (const char *handlers);
498 void
499 mono_runtime_install_custom_handlers_usage (void);
501 void
502 mono_runtime_cleanup_handlers (void);
504 void
505 mono_runtime_setup_stat_profiler (void);
507 void
508 mono_runtime_shutdown_stat_profiler (void);
510 void
511 mono_runtime_posix_install_handlers (void);
513 void
514 mono_gdb_render_native_backtraces (pid_t crashed_pid);
516 void
517 mono_cross_helpers_run (void);
519 void
520 mono_dump_native_crash_info (const char *signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info);
522 void
523 mono_post_native_crash_handler (const char *signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info, gboolean crash_chaining);
526 * Signal handling
529 #if defined(DISABLE_HW_TRAPS) || defined(MONO_ARCH_DISABLE_HW_TRAPS)
530 // Signal handlers not available
531 #define MONO_ARCH_NEED_DIV_CHECK 1
532 #endif
534 void MONO_SIG_HANDLER_SIGNATURE (mono_sigfpe_signal_handler) ;
535 void MONO_SIG_HANDLER_SIGNATURE (mono_sigill_signal_handler) ;
536 void MONO_SIG_HANDLER_SIGNATURE (mono_sigsegv_signal_handler);
537 void MONO_SIG_HANDLER_SIGNATURE (mono_sigint_signal_handler) ;
538 gboolean MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal);
540 #ifdef MONO_ARCH_VARARG_ICALLS
541 #define ARCH_VARARG_ICALLS 1
542 #else
543 #define ARCH_VARARG_ICALLS 0
544 #endif
546 #if defined (HOST_WASM)
548 #define MONO_RETURN_ADDRESS_N(N) NULL
549 #define MONO_RETURN_ADDRESS() MONO_RETURN_ADDRESS_N(0)
552 #elif defined (__GNUC__)
554 #define MONO_RETURN_ADDRESS_N(N) (__builtin_extract_return_addr (__builtin_return_address (N)))
555 #define MONO_RETURN_ADDRESS() MONO_RETURN_ADDRESS_N(0)
557 #elif defined(_MSC_VER)
559 #include <intrin.h>
560 #pragma intrinsic(_ReturnAddress)
562 #define MONO_RETURN_ADDRESS() _ReturnAddress()
563 #define MONO_RETURN_ADDRESS_N(N) NULL
565 #else
567 #error "Missing return address intrinsics implementation"
569 #endif
571 //have a global view of sdb disable
572 #if !defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) || defined (DISABLE_DEBUGGER_AGENT)
573 #define DISABLE_SDB 1
574 #endif
576 void mini_register_sigterm_handler (void);
578 #endif /* __MONO_MINI_RUNTIME_H__ */