3 * domain.c: MonoDomain functions
6 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2001 Ximian, Inc.
17 #include <mono/os/gc_wrapper.h>
19 #include <mono/metadata/object.h>
20 #include <mono/metadata/domain-internals.h>
21 #include <mono/metadata/class-internals.h>
22 #include <mono/metadata/assembly.h>
23 #include <mono/metadata/exception.h>
24 #include <mono/metadata/cil-coff.h>
25 #include <mono/metadata/rawbuffer.h>
26 #include <mono/metadata/metadata-internals.h>
27 #include <mono/metadata/mono-debug-debugger.h>
29 /* #define DEBUG_DOMAIN_UNLOAD */
31 /* we need to use both the Tls* functions and __thread because
32 * the gc needs to see all the appcontext
34 static guint32 context_thread_id = -1;
35 static guint32 appdomain_thread_id = -1;
38 static __thread MonoDomain * tls_appdomain;
39 static __thread MonoAppContext * tls_appcontext;
40 #define GET_APPDOMAIN() tls_appdomain
41 #define SET_APPDOMAIN(x) do { \
43 TlsSetValue (appdomain_thread_id, x); \
46 #define GET_APPCONTEXT() tls_appcontext
47 #define SET_APPCONTEXT(x) do { \
49 TlsSetValue (context_thread_id, x); \
53 #define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
54 #define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
56 #define GET_APPCONTEXT() ((MonoAppContext *)TlsGetValue (context_thread_id))
57 #define SET_APPCONTEXT(x) TlsSetValue (context_thread_id, x);
60 static gint32 appdomain_id_counter = 0;
62 static MonoGHashTable * appdomains_list = NULL;
64 static CRITICAL_SECTION appdomains_mutex;
66 static MonoDomain *mono_root_domain = NULL;
68 /* RuntimeInfo: Contains information about versions supported by this runtime */
70 const char* runtime_version;
71 const char* framework_version;
74 /* AppConfigInfo: Information about runtime versions supported by an
78 GSList *supported_runtimes;
79 char *required_runtime;
80 int configuration_count;
84 static RuntimeInfo *current_runtime = NULL;
86 /* This is the list of runtime versions supported by this JIT.
88 static RuntimeInfo supported_runtimes[] = {
89 {"v1.0.3705", "1.0"}, {"v1.1.4322", "1.0"}, {"v2.0.40607","2.0"}
92 /* The stable runtime version */
93 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
96 get_runtime_from_exe (const char *exe_file);
99 get_runtime_by_version (const char *version);
101 static MonoJitInfoTable *
102 mono_jit_info_table_new (void)
104 return g_array_new (FALSE, FALSE, sizeof (gpointer));
108 mono_jit_info_table_free (MonoJitInfoTable *table)
110 g_array_free (table, TRUE);
114 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
116 int left = 0, right = table->len;
118 while (left < right) {
119 int pos = (left + right) / 2;
120 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
121 char *start = ji->code_start;
122 char *end = start + ji->code_size;
126 else if (addr >= end)
136 mono_jit_info_table_find (MonoDomain *domain, char *addr)
138 MonoJitInfoTable *table = domain->jit_info_table;
141 mono_domain_lock (domain);
144 while (left < right) {
145 int pos = (left + right) / 2;
146 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
147 char *start = ji->code_start;
148 char *end = start + ji->code_size;
152 else if (addr >= end)
155 mono_domain_unlock (domain);
159 mono_domain_unlock (domain);
161 /* maybe it is shared code, so we also search in the root domain */
162 if (domain != mono_root_domain)
163 return mono_jit_info_table_find (mono_root_domain, addr);
169 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
171 MonoJitInfoTable *table = domain->jit_info_table;
172 gpointer start = ji->code_start;
175 mono_domain_lock (domain);
176 pos = mono_jit_info_table_index (table, start);
178 g_array_insert_val (table, pos, ji);
179 mono_domain_unlock (domain);
183 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
185 MonoJitInfoTable *table = domain->jit_info_table;
186 gpointer start = ji->code_start;
189 mono_domain_lock (domain);
190 pos = mono_jit_info_table_index (table, start);
191 g_assert (g_array_index (table, gpointer, pos) == ji);
193 g_array_remove_index (table, pos);
194 mono_domain_unlock (domain);
198 ldstr_hash (const char* str)
202 len = mono_metadata_decode_blob_size (str, &str) - 1;
204 /* if len == 0 *str will point to the mark byte */
207 * FIXME: The distribution may not be so nice with lots of
208 * null chars in the string.
210 for (str += 1; str < end; str++)
211 h = (h << 5) - h + *str;
216 ldstr_equal (const char *str1, const char *str2) {
220 len = mono_metadata_decode_blob_size (str1, NULL) - 1;
221 len2 = mono_metadata_decode_blob_size (str2, NULL) - 1;
224 return memcmp (str1, str2, len) == 0;
228 mono_string_equal (MonoString *s1, MonoString *s2)
230 int l1 = mono_string_length (s1);
231 int l2 = mono_string_length (s2);
236 return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1) == 0;
240 mono_string_hash (MonoString *s)
242 const guint16 *p = mono_string_chars (s);
243 int i, len = mono_string_length (s);
246 for (i = 0; i < len; i++) {
247 h = (h << 5) - h + *p;
256 domain_finalizer (void *obj, void *data) {
257 g_print ("domain finalized\n");
262 mono_domain_create (void)
267 domain = GC_MALLOC (sizeof (MonoDomain));
268 GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
270 domain = g_new0 (MonoDomain, 1);
272 domain->domain = NULL;
273 domain->setup = NULL;
274 domain->friendly_name = NULL;
275 domain->search_path = NULL;
277 domain->mp = mono_mempool_new ();
278 domain->code_mp = mono_code_manager_new ();
279 domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
280 domain->assemblies_by_name = g_hash_table_new (g_str_hash, g_str_equal);
281 domain->assemblies = NULL;
282 domain->class_vtable_hash = mono_g_hash_table_new (NULL, NULL);
283 domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
284 domain->static_data_hash = mono_g_hash_table_new (NULL, NULL);
285 domain->jit_code_hash = g_hash_table_new (NULL, NULL);
286 domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
287 domain->jit_info_table = mono_jit_info_table_new ();
288 domain->class_init_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
289 domain->jump_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
290 domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
291 domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
293 InitializeCriticalSection (&domain->lock);
295 EnterCriticalSection (&appdomains_mutex);
296 mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
297 LeaveCriticalSection (&appdomains_mutex);
303 * mono_init_internal:
305 * Creates the initial application domain and initializes the mono_defaults
307 * This function is guaranteed to not run any IL code.
308 * If exe_filename is not NULL, the method will determine the required runtime
309 * from the exe configuration file or the version PE field.
310 * If runtime_version is not NULL, that runtime version will be used.
311 * Either exe_filename or runtime_version must be provided.
313 * Returns: the initial domain.
316 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
318 static MonoDomain *domain = NULL;
320 MonoImageOpenStatus status = MONO_IMAGE_OK;
321 MonoAssemblyName corlib_aname;
324 g_assert_not_reached ();
328 appdomain_thread_id = TlsAlloc ();
329 context_thread_id = TlsAlloc ();
331 InitializeCriticalSection (&appdomains_mutex);
333 mono_metadata_init ();
334 mono_raw_buffer_init ();
336 mono_assemblies_init ();
339 /* FIXME: When should we release this memory? */
340 MONO_GC_REGISTER_ROOT (appdomains_list);
341 appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
343 domain = mono_domain_create ();
344 mono_root_domain = domain;
346 SET_APPDOMAIN (domain);
348 if (exe_filename != NULL) {
349 current_runtime = get_runtime_from_exe (exe_filename);
350 } else if (runtime_version != NULL) {
351 current_runtime = get_runtime_by_version (runtime_version);
354 if (current_runtime == NULL) {
355 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
356 current_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
357 g_print ("Using default runtime: %s\n", current_runtime->runtime_version);
360 /* find the corlib */
361 corlib_aname.name = "mscorlib";
362 ass = mono_assembly_load (&corlib_aname, NULL, &status);
363 if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
365 case MONO_IMAGE_ERROR_ERRNO: {
366 char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", mono_get_framework_version (), "mscorlib.dll", NULL);
367 g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
368 g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
369 g_free (corlib_file);
372 case MONO_IMAGE_IMAGE_INVALID:
373 g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
374 mono_assembly_getrootdir ());
376 case MONO_IMAGE_MISSING_ASSEMBLYREF:
377 g_print ("Missing assembly reference in %s/mscorlib.dll\n",
378 mono_assembly_getrootdir ());
381 /* to suppress compiler warning */
387 mono_defaults.corlib = mono_assembly_get_image (ass);
389 mono_defaults.object_class = mono_class_from_name (
390 mono_defaults.corlib, "System", "Object");
391 g_assert (mono_defaults.object_class != 0);
393 mono_defaults.void_class = mono_class_from_name (
394 mono_defaults.corlib, "System", "Void");
395 g_assert (mono_defaults.void_class != 0);
397 mono_defaults.boolean_class = mono_class_from_name (
398 mono_defaults.corlib, "System", "Boolean");
399 g_assert (mono_defaults.boolean_class != 0);
401 mono_defaults.byte_class = mono_class_from_name (
402 mono_defaults.corlib, "System", "Byte");
403 g_assert (mono_defaults.byte_class != 0);
405 mono_defaults.sbyte_class = mono_class_from_name (
406 mono_defaults.corlib, "System", "SByte");
407 g_assert (mono_defaults.sbyte_class != 0);
409 mono_defaults.int16_class = mono_class_from_name (
410 mono_defaults.corlib, "System", "Int16");
411 g_assert (mono_defaults.int16_class != 0);
413 mono_defaults.uint16_class = mono_class_from_name (
414 mono_defaults.corlib, "System", "UInt16");
415 g_assert (mono_defaults.uint16_class != 0);
417 mono_defaults.int32_class = mono_class_from_name (
418 mono_defaults.corlib, "System", "Int32");
419 g_assert (mono_defaults.int32_class != 0);
421 mono_defaults.uint32_class = mono_class_from_name (
422 mono_defaults.corlib, "System", "UInt32");
423 g_assert (mono_defaults.uint32_class != 0);
425 mono_defaults.uint_class = mono_class_from_name (
426 mono_defaults.corlib, "System", "UIntPtr");
427 g_assert (mono_defaults.uint_class != 0);
429 mono_defaults.int_class = mono_class_from_name (
430 mono_defaults.corlib, "System", "IntPtr");
431 g_assert (mono_defaults.int_class != 0);
433 mono_defaults.int64_class = mono_class_from_name (
434 mono_defaults.corlib, "System", "Int64");
435 g_assert (mono_defaults.int64_class != 0);
437 mono_defaults.uint64_class = mono_class_from_name (
438 mono_defaults.corlib, "System", "UInt64");
439 g_assert (mono_defaults.uint64_class != 0);
441 mono_defaults.single_class = mono_class_from_name (
442 mono_defaults.corlib, "System", "Single");
443 g_assert (mono_defaults.single_class != 0);
445 mono_defaults.double_class = mono_class_from_name (
446 mono_defaults.corlib, "System", "Double");
447 g_assert (mono_defaults.double_class != 0);
449 mono_defaults.char_class = mono_class_from_name (
450 mono_defaults.corlib, "System", "Char");
451 g_assert (mono_defaults.char_class != 0);
453 mono_defaults.string_class = mono_class_from_name (
454 mono_defaults.corlib, "System", "String");
455 g_assert (mono_defaults.string_class != 0);
457 mono_defaults.enum_class = mono_class_from_name (
458 mono_defaults.corlib, "System", "Enum");
459 g_assert (mono_defaults.enum_class != 0);
461 mono_defaults.array_class = mono_class_from_name (
462 mono_defaults.corlib, "System", "Array");
463 g_assert (mono_defaults.array_class != 0);
465 mono_defaults.delegate_class = mono_class_from_name (
466 mono_defaults.corlib, "System", "Delegate");
467 g_assert (mono_defaults.delegate_class != 0 );
469 mono_defaults.multicastdelegate_class = mono_class_from_name (
470 mono_defaults.corlib, "System", "MulticastDelegate");
471 g_assert (mono_defaults.multicastdelegate_class != 0 );
473 mono_defaults.asyncresult_class = mono_class_from_name (
474 mono_defaults.corlib, "System.Runtime.Remoting.Messaging",
476 g_assert (mono_defaults.asyncresult_class != 0 );
478 mono_defaults.waithandle_class = mono_class_from_name (
479 mono_defaults.corlib, "System.Threading", "WaitHandle");
480 g_assert (mono_defaults.waithandle_class != 0 );
482 mono_defaults.typehandle_class = mono_class_from_name (
483 mono_defaults.corlib, "System", "RuntimeTypeHandle");
484 g_assert (mono_defaults.typehandle_class != 0);
486 mono_defaults.methodhandle_class = mono_class_from_name (
487 mono_defaults.corlib, "System", "RuntimeMethodHandle");
488 g_assert (mono_defaults.methodhandle_class != 0);
490 mono_defaults.fieldhandle_class = mono_class_from_name (
491 mono_defaults.corlib, "System", "RuntimeFieldHandle");
492 g_assert (mono_defaults.fieldhandle_class != 0);
494 mono_defaults.monotype_class = mono_class_from_name (
495 mono_defaults.corlib, "System", "MonoType");
496 g_assert (mono_defaults.monotype_class != 0);
498 mono_defaults.exception_class = mono_class_from_name (
499 mono_defaults.corlib, "System", "Exception");
500 g_assert (mono_defaults.exception_class != 0);
502 mono_defaults.threadabortexception_class = mono_class_from_name (
503 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
504 g_assert (mono_defaults.threadabortexception_class != 0);
506 mono_defaults.thread_class = mono_class_from_name (
507 mono_defaults.corlib, "System.Threading", "Thread");
508 g_assert (mono_defaults.thread_class != 0);
510 mono_defaults.appdomain_class = mono_class_from_name (
511 mono_defaults.corlib, "System", "AppDomain");
512 g_assert (mono_defaults.appdomain_class != 0);
514 mono_defaults.transparent_proxy_class = mono_class_from_name (
515 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
516 g_assert (mono_defaults.transparent_proxy_class != 0);
518 mono_defaults.real_proxy_class = mono_class_from_name (
519 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
520 g_assert (mono_defaults.real_proxy_class != 0);
522 mono_defaults.mono_method_message_class = mono_class_from_name (
523 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
524 g_assert (mono_defaults.mono_method_message_class != 0);
526 mono_defaults.field_info_class = mono_class_from_name (
527 mono_defaults.corlib, "System.Reflection", "FieldInfo");
528 g_assert (mono_defaults.field_info_class != 0);
530 mono_defaults.method_info_class = mono_class_from_name (
531 mono_defaults.corlib, "System.Reflection", "MethodInfo");
532 g_assert (mono_defaults.method_info_class != 0);
534 mono_defaults.stringbuilder_class = mono_class_from_name (
535 mono_defaults.corlib, "System.Text", "StringBuilder");
536 g_assert (mono_defaults.stringbuilder_class != 0);
538 mono_defaults.math_class = mono_class_from_name (
539 mono_defaults.corlib, "System", "Math");
540 g_assert (mono_defaults.math_class != 0);
542 mono_defaults.stack_frame_class = mono_class_from_name (
543 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
544 g_assert (mono_defaults.stack_frame_class != 0);
546 mono_defaults.stack_trace_class = mono_class_from_name (
547 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
548 g_assert (mono_defaults.stack_trace_class != 0);
550 mono_defaults.marshal_class = mono_class_from_name (
551 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
552 g_assert (mono_defaults.marshal_class != 0);
554 mono_defaults.iserializeable_class = mono_class_from_name (
555 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
556 g_assert (mono_defaults.iserializeable_class != 0);
558 mono_defaults.serializationinfo_class = mono_class_from_name (
559 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
560 g_assert (mono_defaults.serializationinfo_class != 0);
562 mono_defaults.streamingcontext_class = mono_class_from_name (
563 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
564 g_assert (mono_defaults.streamingcontext_class != 0);
566 mono_defaults.typed_reference_class = mono_class_from_name (
567 mono_defaults.corlib, "System", "TypedReference");
568 g_assert (mono_defaults.typed_reference_class != 0);
570 mono_defaults.argumenthandle_class = mono_class_from_name (
571 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
572 g_assert (mono_defaults.argumenthandle_class != 0);
574 mono_defaults.marshalbyrefobject_class = mono_class_from_name (
575 mono_defaults.corlib, "System", "MarshalByRefObject");
576 g_assert (mono_defaults.marshalbyrefobject_class != 0);
578 mono_defaults.monitor_class = mono_class_from_name (
579 mono_defaults.corlib, "System.Threading", "Monitor");
580 g_assert (mono_defaults.monitor_class != 0);
582 mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
583 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
584 g_assert (mono_defaults.iremotingtypeinfo_class != 0);
586 domain->friendly_name = g_path_get_basename (filename);
594 * Creates the initial application domain and initializes the mono_defaults
596 * This function is guaranteed to not run any IL code.
597 * The runtime is initialized using the default runtime version.
599 * Returns: the initial domain.
602 mono_init (const char *domain_name)
604 return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
608 * mono_init_from_assembly:
610 * Creates the initial application domain and initializes the mono_defaults
612 * This function is guaranteed to not run any IL code.
613 * The runtime is initialized using the runtime version required by the
614 * provided executable. The version is determined by looking at the exe
615 * configuration file and the version PE field)
617 * Returns: the initial domain.
620 mono_init_from_assembly (const char *domain_name, const char *filename)
622 return mono_init_internal (domain_name, filename, NULL);
628 * Creates the initial application domain and initializes the mono_defaults
630 * This function is guaranteed to not run any IL code.
631 * The runtime is initialized using the provided rutime version.
633 * Returns: the initial domain.
636 mono_init_version (const char *domain_name, const char *version)
638 return mono_init_internal (domain_name, NULL, version);
642 mono_get_root_domain (void)
644 return mono_root_domain;
650 * Returns: the current domain.
655 return GET_APPDOMAIN ();
659 * mono_domain_set_internal:
660 * @domain: the new domain
662 * Sets the current domain to @domain.
665 mono_domain_set_internal (MonoDomain *domain)
667 SET_APPDOMAIN (domain);
668 SET_APPCONTEXT (domain->default_context);
677 copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
679 MonoGHashTable *dest = (MonoGHashTable*)user_data;
681 mono_g_hash_table_insert (dest, key, data);
685 foreach_domain (gpointer key, gpointer data, gpointer user_data)
687 DomainInfo *dom_info = user_data;
689 dom_info->func ((MonoDomain*)data, dom_info->user_data);
693 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
696 MonoGHashTable *copy;
699 * Create a copy of the hashtable to avoid calling the user callback
700 * inside the lock because that could lead to deadlocks.
701 * We can do this because this function is not perf. critical.
703 copy = mono_g_hash_table_new (NULL, NULL);
704 EnterCriticalSection (&appdomains_mutex);
705 mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
706 LeaveCriticalSection (&appdomains_mutex);
708 dom_info.func = func;
709 dom_info.user_data = user_data;
710 mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
712 mono_g_hash_table_destroy (copy);
716 * mono_domain_assembly_open:
717 * @domain: the application domain
718 * @name: file name of the assembly
720 * fixme: maybe we should integrate this with mono_assembly_open ??
723 mono_domain_assembly_open (MonoDomain *domain, const char *name)
727 mono_domain_lock (domain);
728 if ((ass = g_hash_table_lookup (domain->assemblies_by_name, name))) {
729 mono_domain_unlock (domain);
732 mono_domain_unlock (domain);
734 if (!(ass = mono_assembly_open (name, NULL)))
741 remove_assembly (gpointer key, gpointer value, gpointer user_data)
743 mono_assembly_close ((MonoAssembly *)value);
747 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
749 MonoJitDynamicMethodInfo *di = value;
750 mono_code_manager_destroy (di->code_mp);
755 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
757 g_slist_free (value);
761 mono_domain_free (MonoDomain *domain, gboolean force)
763 if ((domain == mono_root_domain) && !force) {
764 g_warning ("cant unload root domain");
768 EnterCriticalSection (&appdomains_mutex);
769 mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
770 LeaveCriticalSection (&appdomains_mutex);
772 g_free (domain->friendly_name);
773 g_hash_table_foreach (domain->assemblies_by_name, remove_assembly, NULL);
775 mono_g_hash_table_destroy (domain->env);
776 g_hash_table_destroy (domain->assemblies_by_name);
777 g_list_free (domain->assemblies);
778 mono_g_hash_table_destroy (domain->class_vtable_hash);
779 mono_g_hash_table_destroy (domain->proxy_vtable_hash);
780 mono_g_hash_table_destroy (domain->static_data_hash);
781 g_hash_table_destroy (domain->jit_code_hash);
782 if (domain->dynamic_code_hash) {
783 g_hash_table_foreach (domain->dynamic_code_hash, dynamic_method_info_free, NULL);
784 g_hash_table_destroy (domain->dynamic_code_hash);
786 mono_g_hash_table_destroy (domain->ldstr_table);
787 mono_jit_info_table_free (domain->jit_info_table);
788 #ifdef DEBUG_DOMAIN_UNLOAD
789 mono_mempool_invalidate (domain->mp);
790 mono_code_manager_invalidate (domain->code_mp);
792 mono_mempool_destroy (domain->mp);
793 mono_code_manager_destroy (domain->code_mp);
795 if (domain->jump_target_hash) {
796 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
797 g_hash_table_destroy (domain->jump_target_hash);
799 mono_g_hash_table_destroy (domain->class_init_trampoline_hash);
800 mono_g_hash_table_destroy (domain->jump_trampoline_hash);
801 g_hash_table_destroy (domain->finalizable_objects_hash);
802 if (domain->special_static_fields)
803 g_hash_table_destroy (domain->special_static_fields);
804 DeleteCriticalSection (&domain->lock);
805 domain->setup = NULL;
807 /* FIXME: anything else required ? */
814 if ((domain == mono_root_domain))
815 mono_root_domain = NULL;
819 * mono_domain_get_id:
821 * Returns: the a domain for a specific domain id.
824 mono_domain_get_by_id (gint32 domainid)
828 EnterCriticalSection (&appdomains_mutex);
829 domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
830 LeaveCriticalSection (&appdomains_mutex);
836 mono_domain_get_id (MonoDomain *domain)
838 return domain->domain_id;
842 mono_context_set (MonoAppContext * new_context)
844 SET_APPCONTEXT (new_context);
848 mono_context_get (void)
850 return GET_APPCONTEXT ();
854 mono_get_corlib (void)
856 return mono_defaults.corlib;
860 mono_get_object_class (void)
862 return mono_defaults.object_class;
866 mono_get_byte_class (void)
868 return mono_defaults.byte_class;
872 mono_get_void_class (void)
874 return mono_defaults.void_class;
878 mono_get_boolean_class (void)
880 return mono_defaults.boolean_class;
884 mono_get_sbyte_class (void)
886 return mono_defaults.sbyte_class;
890 mono_get_int16_class (void)
892 return mono_defaults.int16_class;
896 mono_get_uint16_class (void)
898 return mono_defaults.uint16_class;
902 mono_get_int32_class (void)
904 return mono_defaults.int32_class;
908 mono_get_uint32_class (void)
910 return mono_defaults.uint32_class;
914 mono_get_intptr_class (void)
916 return mono_defaults.int_class;
920 mono_get_uintptr_class (void)
922 return mono_defaults.uint_class;
926 mono_get_int64_class (void)
928 return mono_defaults.int64_class;
932 mono_get_uint64_class (void)
934 return mono_defaults.uint64_class;
938 mono_get_single_class (void)
940 return mono_defaults.single_class;
944 mono_get_double_class (void)
946 return mono_defaults.double_class;
950 mono_get_char_class (void)
952 return mono_defaults.char_class;
956 mono_get_string_class (void)
958 return mono_defaults.string_class;
962 mono_get_enum_class (void)
964 return mono_defaults.enum_class;
968 mono_get_array_class (void)
970 return mono_defaults.array_class;
974 mono_get_thread_class (void)
976 return mono_defaults.thread_class;
980 mono_get_exception_class (void)
982 return mono_defaults.exception_class;
986 static char* get_attribute_value (const gchar **attribute_names,
987 const gchar **attribute_values,
988 const char *att_name)
991 for (n=0; attribute_names[n] != NULL; n++) {
992 if (strcmp (attribute_names[n], att_name) == 0)
993 return g_strdup (attribute_values[n]);
998 static void start_element (GMarkupParseContext *context,
999 const gchar *element_name,
1000 const gchar **attribute_names,
1001 const gchar **attribute_values,
1005 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1007 if (strcmp (element_name, "configuration") == 0) {
1008 app_config->configuration_count++;
1011 if (strcmp (element_name, "startup") == 0) {
1012 app_config->startup_count++;
1016 if (app_config->configuration_count != 1 || app_config->startup_count != 1)
1019 if (strcmp (element_name, "requiredRuntime") == 0) {
1020 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
1021 } else if (strcmp (element_name, "supportedRuntime") == 0) {
1022 char *version = get_attribute_value (attribute_names, attribute_values, "version");
1023 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
1027 static void end_element (GMarkupParseContext *context,
1028 const gchar *element_name,
1032 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1034 if (strcmp (element_name, "configuration") == 0) {
1035 app_config->configuration_count--;
1036 } else if (strcmp (element_name, "startup") == 0) {
1037 app_config->startup_count--;
1041 static const GMarkupParser
1050 static AppConfigInfo *
1051 app_config_parse (const char *filename)
1053 AppConfigInfo *app_config;
1054 GMarkupParseContext *context;
1059 if (stat (filename, &buf) != 0)
1062 app_config = g_new0 (AppConfigInfo, 1);
1064 if (!g_file_get_contents (filename, &text, &len, NULL))
1067 context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1068 if (g_markup_parse_context_parse (context, text, len, NULL)) {
1069 g_markup_parse_context_end_parse (context, NULL);
1071 g_markup_parse_context_free (context);
1077 app_config_free (AppConfigInfo* app_config)
1080 GSList *list = app_config->supported_runtimes;
1081 while (list != NULL) {
1082 rt = (char*)list->data;
1084 list = g_slist_next (list);
1086 g_slist_free (app_config->supported_runtimes);
1087 g_free (app_config->required_runtime);
1088 g_free (app_config);
1093 get_runtime_by_version (const char *version)
1096 int max = G_N_ELEMENTS (supported_runtimes);
1098 for (n=0; n<max; n++) {
1099 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1100 return &supported_runtimes[n];
1106 get_runtime_from_exe (const char *exe_file)
1108 AppConfigInfo* app_config;
1111 RuntimeInfo* runtime = NULL;
1112 MonoImage *image = NULL;
1114 config_name = g_strconcat (exe_file, ".config", NULL);
1115 app_config = app_config_parse (config_name);
1116 g_free (config_name);
1118 if (app_config != NULL) {
1119 /* Check supportedRuntime elements, if none is supported, fail.
1120 * If there are no such elements, look for a requiredRuntime element.
1122 if (app_config->supported_runtimes != NULL) {
1123 GSList *list = app_config->supported_runtimes;
1124 while (list != NULL && runtime == NULL) {
1125 version = (char*) list->data;
1126 runtime = get_runtime_by_version (version);
1127 list = g_slist_next (list);
1129 app_config_free (app_config);
1133 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1134 if (app_config->required_runtime != NULL) {
1135 runtime = get_runtime_by_version (app_config->required_runtime);
1136 app_config_free (app_config);
1139 app_config_free (app_config);
1142 /* Look for a runtime with the exact version */
1143 image = mono_image_open (exe_file, NULL);
1144 if (image == NULL) {
1145 /* The image is wrong or the file was not found. In this case return
1146 * a default runtime and leave to the initialization method the work of
1147 * reporting the error.
1149 return get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1152 runtime = get_runtime_by_version (image->version);
1158 mono_get_framework_version (void)
1160 return current_runtime->framework_version;
1164 mono_get_runtime_version (void)
1166 return current_runtime->runtime_version;
1170 mono_debugger_check_runtime_version (const char *filename)
1174 rinfo = get_runtime_from_exe (filename);
1176 return g_strdup_printf ("Cannot get runtime version from assembly `%s'", filename);
1178 if (rinfo != current_runtime)
1179 return g_strdup_printf ("The Mono Debugger is currently using the `%s' runtime, but "
1180 "the assembly `%s' requires version `%s'", current_runtime->runtime_version,
1181 filename, rinfo->runtime_version);