2017-10-02 23:24:00 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
|
/* gd_mono_utils.cpp */
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
/* This file is part of: */
|
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
|
/* https://godotengine.org */
|
|
|
|
|
/*************************************************************************/
|
C#: Move marshaling logic and generated glue to C#
We will be progressively moving most code to C#.
The plan is to only use Mono's embedding APIs to set things at launch.
This will make it much easier to later support CoreCLR too which
doesn't have rich embedding APIs.
Additionally the code in C# is more maintainable and makes it easier
to implement new features, e.g.: runtime codegen which we could use to
avoid using reflection for marshaling everytime a field, property or
method is accessed.
SOME NOTES ON INTEROP
We make the same assumptions as GDNative about the size of the Godot
structures we use. We take it a bit further by also assuming the layout
of fields in some cases, which is riskier but let's us squeeze out some
performance by avoiding unnecessary managed to native calls.
Code that deals with native structs is less safe than before as there's
no RAII and copy constructors in C#. It's like using the GDNative C API
directly. One has to take special care to free values they own.
Perhaps we could use roslyn analyzers to check this, but I don't know
any that uses attributes to determine what's owned or borrowed.
As to why we maily use pointers for native structs instead of ref/out:
- AFAIK (and confirmed with a benchmark) ref/out are pinned
during P/Invoke calls and that has a cost.
- Native struct fields can't be ref/out in the first place.
- A `using` local can't be passed as ref/out, only `in`. Calling a
method or property on an `in` value makes a silent copy, so we want
to avoid `in`.
REGARDING THE BUILD SYSTEM
There's no longer a `mono_glue=yes/no` SCons options. We no longer
need to build with `mono_glue=no`, generate the glue and then build
again with `mono_glue=yes`. We build only once and generate the glue
(which is in C# now).
However, SCons no longer builds the C# projects for us. Instead one
must run `build_assemblies.py`, e.g.:
```sh
%godot_src_root%/modules/mono/build_scripts/build_assemblies.py \
--godot-output-dir=%godot_src_root%/bin \
--godot-target=release_debug`
```
We could turn this into a custom build target, but I don't know how
to do that with SCons (it's possible with Meson).
OTHER NOTES
Most of the moved code doesn't follow the C# naming convention and
still has the word Mono in the names despite no longer dealing with
Mono's embedding APIs. This is just temporary while transitioning,
to make it easier to understand what was moved where.
2021-05-03 15:21:06 +02:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-10-02 23:24:00 +02:00
|
|
|
/* */
|
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
|
/* the following conditions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
#include "gd_mono_utils.h"
|
|
|
|
|
|
2020-04-23 02:19:32 +02:00
|
|
|
#include <mono/metadata/debug-helpers.h>
|
2018-06-26 21:03:42 +02:00
|
|
|
#include <mono/metadata/exception.h>
|
|
|
|
|
|
2020-02-27 03:30:20 +01:00
|
|
|
#include "core/debugger/engine_debugger.h"
|
|
|
|
|
#include "core/debugger/script_debugger.h"
|
2021-06-11 14:51:48 +02:00
|
|
|
#include "core/io/dir_access.h"
|
2021-06-04 18:03:15 +02:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-02-26 11:28:13 +01:00
|
|
|
#include "core/os/mutex.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/os.h"
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2019-07-24 23:13:24 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2020-02-24 06:21:15 +01:00
|
|
|
#include "editor/debugger/editor_debugger_node.h"
|
2019-07-24 23:13:24 +02:00
|
|
|
#endif
|
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
#include "../csharp_script.h"
|
2018-06-26 21:03:42 +02:00
|
|
|
#include "../utils/macros.h"
|
2017-10-02 23:24:00 +02:00
|
|
|
#include "gd_mono.h"
|
2019-11-10 17:10:38 +01:00
|
|
|
#include "gd_mono_cache.h"
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
|
namespace GDMonoUtils {
|
|
|
|
|
|
|
|
|
|
void set_main_thread(MonoThread *p_thread) {
|
|
|
|
|
mono_thread_set_main(p_thread);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-16 17:11:13 +01:00
|
|
|
MonoThread *attach_current_thread() {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(!GDMono::get_singleton()->is_runtime_initialized(), nullptr);
|
2020-01-16 17:11:13 +01:00
|
|
|
MonoDomain *scripts_domain = GDMono::get_singleton()->get_scripts_domain();
|
2020-03-18 17:40:04 +01:00
|
|
|
#ifndef GD_MONO_SINGLE_APPDOMAIN
|
2020-01-16 17:11:13 +01:00
|
|
|
MonoThread *mono_thread = mono_thread_attach(scripts_domain ? scripts_domain : mono_get_root_domain());
|
2020-03-18 17:40:04 +01:00
|
|
|
#else
|
|
|
|
|
// The scripts domain is the root domain
|
|
|
|
|
MonoThread *mono_thread = mono_thread_attach(scripts_domain);
|
|
|
|
|
#endif
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_NULL_V(mono_thread, nullptr);
|
2020-01-16 17:11:13 +01:00
|
|
|
return mono_thread;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void detach_current_thread() {
|
|
|
|
|
ERR_FAIL_COND(!GDMono::get_singleton()->is_runtime_initialized());
|
|
|
|
|
MonoThread *mono_thread = mono_thread_current();
|
|
|
|
|
ERR_FAIL_NULL(mono_thread);
|
|
|
|
|
mono_thread_detach(mono_thread);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-16 17:11:13 +01:00
|
|
|
void detach_current_thread(MonoThread *p_mono_thread) {
|
|
|
|
|
ERR_FAIL_COND(!GDMono::get_singleton()->is_runtime_initialized());
|
|
|
|
|
ERR_FAIL_NULL(p_mono_thread);
|
|
|
|
|
mono_thread_detach(p_mono_thread);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
MonoThread *get_current_thread() {
|
|
|
|
|
return mono_thread_current();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-16 17:11:13 +01:00
|
|
|
bool is_thread_attached() {
|
2020-04-02 01:20:12 +02:00
|
|
|
return mono_domain_get() != nullptr;
|
2020-01-16 17:11:13 +01:00
|
|
|
}
|
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
MonoDomain *create_domain(const String &p_friendly_name) {
|
2019-10-11 01:23:35 +02:00
|
|
|
print_verbose("Mono: Creating domain '" + p_friendly_name + "'...");
|
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoDomain *domain = mono_domain_create_appdomain((char *)p_friendly_name.utf8().get_data(), nullptr);
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
|
if (domain) {
|
|
|
|
|
// Workaround to avoid this exception:
|
|
|
|
|
// System.Configuration.ConfigurationErrorsException: Error Initializing the configuration system.
|
|
|
|
|
// ---> System.ArgumentException: The 'ExeConfigFilename' argument cannot be null.
|
|
|
|
|
mono_domain_set_config(domain, ".", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return domain;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
// TODO:
|
|
|
|
|
// Implement all of the disabled exception logging below. Once we move to .NET 6.
|
|
|
|
|
// It will have to be done from C# as UnmanagedCallersOnly doesn't allow throwing.
|
2020-04-23 02:19:32 +02:00
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
#warning TODO
|
|
|
|
|
#if 0
|
2018-07-18 23:07:57 +02:00
|
|
|
String get_exception_name_and_message(MonoException *p_exc) {
|
2017-10-02 23:24:00 +02:00
|
|
|
String res;
|
|
|
|
|
|
2018-07-18 23:07:57 +02:00
|
|
|
MonoClass *klass = mono_object_get_class((MonoObject *)p_exc);
|
2017-10-02 23:24:00 +02:00
|
|
|
MonoType *type = mono_class_get_type(klass);
|
|
|
|
|
|
|
|
|
|
char *full_name = mono_type_full_name(type);
|
|
|
|
|
res += full_name;
|
|
|
|
|
mono_free(full_name);
|
|
|
|
|
|
|
|
|
|
res += ": ";
|
|
|
|
|
|
|
|
|
|
MonoProperty *prop = mono_class_get_property_from_name(klass, "Message");
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoString *msg = (MonoString *)property_get_value(prop, (MonoObject *)p_exc, nullptr, nullptr);
|
2017-10-02 23:24:00 +02:00
|
|
|
res += GDMonoMarshal::mono_string_to_godot(msg);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2021-09-12 20:21:15 +02:00
|
|
|
#endif
|
2018-01-04 21:05:46 +01:00
|
|
|
|
2018-06-26 21:03:42 +02:00
|
|
|
void debug_print_unhandled_exception(MonoException *p_exc) {
|
|
|
|
|
print_unhandled_exception(p_exc);
|
|
|
|
|
debug_send_unhandled_exception_error(p_exc);
|
2018-01-09 17:19:03 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-26 21:03:42 +02:00
|
|
|
void debug_send_unhandled_exception_error(MonoException *p_exc) {
|
2018-01-09 17:19:03 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-02-27 03:30:20 +01:00
|
|
|
if (!EngineDebugger::is_active()) {
|
2019-07-24 23:13:24 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2021-09-12 20:21:15 +02:00
|
|
|
#warning TODO
|
|
|
|
|
#if 0
|
2019-11-06 17:03:04 +01:00
|
|
|
ERR_PRINT(GDMonoUtils::get_exception_name_and_message(p_exc));
|
2021-09-12 20:21:15 +02:00
|
|
|
#endif
|
2019-07-24 23:13:24 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
2018-01-12 19:23:11 +01:00
|
|
|
return;
|
2019-07-24 23:13:24 +02:00
|
|
|
}
|
2018-01-12 19:23:11 +01:00
|
|
|
|
2020-03-14 19:20:17 +01:00
|
|
|
static thread_local bool _recursion_flag_ = false;
|
2020-07-05 19:19:36 +02:00
|
|
|
if (_recursion_flag_) {
|
2020-03-14 19:20:17 +01:00
|
|
|
return;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2020-03-14 19:20:17 +01:00
|
|
|
_recursion_flag_ = true;
|
|
|
|
|
SCOPE_EXIT { _recursion_flag_ = false; };
|
2018-06-26 21:03:42 +02:00
|
|
|
|
2018-02-24 16:36:48 +01:00
|
|
|
ScriptLanguage::StackInfo separator;
|
2018-06-26 21:03:42 +02:00
|
|
|
separator.file = String();
|
2018-02-27 10:36:58 +01:00
|
|
|
separator.func = "--- " + RTR("End of inner exception stack trace") + " ---";
|
2018-02-24 16:36:48 +01:00
|
|
|
separator.line = 0;
|
2018-01-09 17:19:03 +01:00
|
|
|
|
2018-02-24 16:36:48 +01:00
|
|
|
Vector<ScriptLanguage::StackInfo> si;
|
2018-06-26 21:03:42 +02:00
|
|
|
String exc_msg;
|
2018-02-24 16:36:48 +01:00
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
#warning TODO
|
|
|
|
|
#if 0
|
2020-04-02 01:20:12 +02:00
|
|
|
while (p_exc != nullptr) {
|
2018-02-24 16:36:48 +01:00
|
|
|
GDMonoClass *st_klass = CACHED_CLASS(System_Diagnostics_StackTrace);
|
|
|
|
|
MonoObject *stack_trace = mono_object_new(mono_domain_get(), st_klass->get_mono_ptr());
|
|
|
|
|
|
|
|
|
|
MonoBoolean need_file_info = true;
|
|
|
|
|
void *ctor_args[2] = { p_exc, &need_file_info };
|
2018-01-09 17:19:03 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *unexpected_exc = nullptr;
|
2018-02-24 16:36:48 +01:00
|
|
|
CACHED_METHOD(System_Diagnostics_StackTrace, ctor_Exception_bool)->invoke_raw(stack_trace, ctor_args, &unexpected_exc);
|
2018-01-09 17:19:03 +01:00
|
|
|
|
2018-06-26 21:03:42 +02:00
|
|
|
if (unexpected_exc) {
|
|
|
|
|
GDMonoInternals::unhandled_exception(unexpected_exc);
|
2019-07-24 23:13:24 +02:00
|
|
|
return;
|
2018-01-09 17:19:03 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-24 16:36:48 +01:00
|
|
|
Vector<ScriptLanguage::StackInfo> _si;
|
2020-04-02 01:20:12 +02:00
|
|
|
if (stack_trace != nullptr) {
|
2018-02-24 16:36:48 +01:00
|
|
|
_si = CSharpLanguage::get_singleton()->stack_trace_get_info(stack_trace);
|
2020-07-05 19:19:36 +02:00
|
|
|
for (int i = _si.size() - 1; i >= 0; i--) {
|
2018-02-24 16:36:48 +01:00
|
|
|
si.insert(0, _si[i]);
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-02-24 16:36:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exc_msg += (exc_msg.length() > 0 ? " ---> " : "") + GDMonoUtils::get_exception_name_and_message(p_exc);
|
|
|
|
|
|
2018-06-26 21:03:42 +02:00
|
|
|
GDMonoClass *exc_class = GDMono::get_singleton()->get_class(mono_get_exception_class());
|
|
|
|
|
GDMonoProperty *inner_exc_prop = exc_class->get_property("InnerException");
|
2020-04-02 01:20:12 +02:00
|
|
|
CRASH_COND(inner_exc_prop == nullptr);
|
2018-06-26 21:03:42 +02:00
|
|
|
|
|
|
|
|
MonoObject *inner_exc = inner_exc_prop->get_value((MonoObject *)p_exc);
|
2020-07-05 19:19:36 +02:00
|
|
|
if (inner_exc != nullptr) {
|
2018-02-24 16:36:48 +01:00
|
|
|
si.insert(0, separator);
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-06-26 21:03:42 +02:00
|
|
|
|
|
|
|
|
p_exc = (MonoException *)inner_exc;
|
2018-02-24 16:36:48 +01:00
|
|
|
}
|
2021-09-12 20:21:15 +02:00
|
|
|
#endif
|
2018-01-09 17:19:03 +01:00
|
|
|
|
|
|
|
|
String file = si.size() ? si[0].file : __FILE__;
|
|
|
|
|
String func = si.size() ? si[0].func : FUNCTION_STR;
|
|
|
|
|
int line = si.size() ? si[0].line : __LINE__;
|
|
|
|
|
String error_msg = "Unhandled exception";
|
|
|
|
|
|
2021-10-20 11:07:20 +02:00
|
|
|
EngineDebugger::get_script_debugger()->send_error(func, file, line, error_msg, exc_msg, true, ERR_HANDLER_ERROR, si);
|
2018-01-09 17:19:03 +01:00
|
|
|
#endif
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-26 21:03:42 +02:00
|
|
|
void debug_unhandled_exception(MonoException *p_exc) {
|
|
|
|
|
GDMonoInternals::unhandled_exception(p_exc); // prints the exception as well
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void print_unhandled_exception(MonoException *p_exc) {
|
|
|
|
|
mono_print_unhandled_exception((MonoObject *)p_exc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_pending_exception(MonoException *p_exc) {
|
2019-07-03 09:44:53 +02:00
|
|
|
#ifdef NO_PENDING_EXCEPTIONS
|
|
|
|
|
debug_unhandled_exception(p_exc);
|
|
|
|
|
#else
|
2018-06-26 21:03:42 +02:00
|
|
|
if (get_runtime_invoke_count() == 0) {
|
|
|
|
|
debug_unhandled_exception(p_exc);
|
2020-12-05 00:38:24 +01:00
|
|
|
return;
|
2018-06-26 21:03:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!mono_runtime_set_pending_exception(p_exc, false)) {
|
2019-11-06 17:03:04 +01:00
|
|
|
ERR_PRINT("Exception thrown from managed code, but it could not be set as pending:");
|
2018-06-26 21:03:42 +02:00
|
|
|
GDMonoUtils::debug_print_unhandled_exception(p_exc);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-14 19:20:17 +01:00
|
|
|
thread_local int current_invoke_count = 0;
|
2018-06-26 21:03:42 +02:00
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
ScopeThreadAttach::ScopeThreadAttach() {
|
2020-01-16 17:11:13 +01:00
|
|
|
if (likely(GDMono::get_singleton()->is_runtime_initialized()) && unlikely(!mono_domain_get())) {
|
|
|
|
|
mono_thread = GDMonoUtils::attach_current_thread();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScopeThreadAttach::~ScopeThreadAttach() {
|
|
|
|
|
if (unlikely(mono_thread)) {
|
|
|
|
|
GDMonoUtils::detach_current_thread(mono_thread);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-16 21:09:00 -05:00
|
|
|
} // namespace GDMonoUtils
|