Debugging, but it doesn't work for some reason

This commit is contained in:
Simon Lübeß
2023-07-21 16:16:24 +02:00
parent cc7fb7311f
commit 457b43352a
2 changed files with 96 additions and 11 deletions
+64 -10
View File
@@ -89,6 +89,9 @@ static class ScriptEngine
private static FileSystemWatcher _userAssemblyWatcher ~ delete _;
// TODO: This should be a global setting somewhere
private static bool _debuggingEnabled = true;
internal static class Attributes
{
internal static MonoClass* s_ShowInEditorAttribute;
@@ -96,11 +99,7 @@ static class ScriptEngine
public static void Init()
{
Mono.mono_set_assemblies_path("mono/lib");
s_RootDomain = Mono.mono_jit_init("GlitchyEngineJITRuntime");
Log.EngineLogger.Assert(s_RootDomain != null, "Failed to initialize mono root domain");
InitMono();
ScriptGlue.Init();
LoadScriptAssemblies();
@@ -108,6 +107,32 @@ static class ScriptEngine
InitAssemblyWatcher();
}
static void InitMono()
{
Mono.mono_set_assemblies_path("mono/lib");
if (_debuggingEnabled)
{
char8*[2] options = .(
"--debugger-agent=transport=dt_socket,address=127.0.0.1:2550,server=y,suspend=n,loglevel=3,logfile=MonoDebugger.log",
"--soft-breakpoints"
);
Mono.mono_jit_parse_options(options.Count, &options);
Mono.mono_debug_init(.Mono);
}
s_RootDomain = Mono.mono_jit_init("GlitchyEngineJITRuntime");
Log.EngineLogger.Assert(s_RootDomain != null, "Failed to initialize mono root domain");
if (_debuggingEnabled)
{
Mono.mono_debug_domain_create(s_RootDomain);
}
Mono.mono_thread_set_main(Mono.mono_thread_current());
}
static void InitAssemblyWatcher()
{
if (_userAssemblyWatcher == null)
@@ -143,8 +168,8 @@ static class ScriptEngine
static void LoadScriptAssemblies()
{
CreateAppDomain("GlitchyEngineScriptRuntime");
(s_CoreAssembly, s_CoreAssemblyImage) = LoadAssembly("resources/scripts/ScriptCore.dll");
(s_AppAssembly, s_AppAssemblyImage) = LoadAssembly("SandboxProject/Assets/Scripts/bin/Sandbox.dll");
(s_CoreAssembly, s_CoreAssemblyImage) = LoadAssembly("resources/scripts/ScriptCore.dll", _debuggingEnabled);
(s_AppAssembly, s_AppAssemblyImage) = LoadAssembly("SandboxProject/Assets/Scripts/bin/Sandbox.dll", _debuggingEnabled);
GetEntitiesFromAssemblies();
@@ -202,7 +227,7 @@ static class ScriptEngine
}
}
private static MonoAssembly* LoadCSharpAssembly(StringView assemblyPath)
private static MonoAssembly* LoadCSharpAssembly(StringView assemblyPath, bool loadPDB = false)
{
List<uint8> data = new List<uint8>(1024);
@@ -222,8 +247,37 @@ static class ScriptEngine
return null;
}
if (loadPDB)
{
String pdbPath = scope .();
Path.ChangeExtension(assemblyPath, ".pdb", pdbPath);
if (File.Exists(pdbPath))
{
Log.EngineLogger.Trace($"Loading PDB \"{pdbPath}\"...");
List<uint8> pdbData = new List<uint8>(1024);
let result = File.ReadAll(pdbPath, pdbData);
if (result case .Err(let error))
{
Log.EngineLogger.Error("Failed to load PDB file ({error}).");
}
Mono.mono_debug_open_image_from_memory(image, pdbData.Ptr, (int32)pdbData.Count);
delete pdbData;
}
else
{
Log.EngineLogger.Warning($"Debugging enabled but no PDB-File found \"{assemblyPath}\".");
}
}
MonoAssembly* assembly = Mono.mono_assembly_load_from_full(image, assemblyPath.ToScopeCStr!(), &status, 0);
Mono.mono_image_close(image);
return assembly;
@@ -235,9 +289,9 @@ static class ScriptEngine
Mono.mono_domain_set(s_AppDomain, true);
}
static (MonoAssembly* assembly, MonoImage* image) LoadAssembly(StringView filepath)
static (MonoAssembly* assembly, MonoImage* image) LoadAssembly(StringView filepath, bool loadPDB = false)
{
MonoAssembly* assembly = LoadCSharpAssembly(filepath);
MonoAssembly* assembly = LoadCSharpAssembly(filepath, loadPDB);
MonoImage* image = Mono.mono_assembly_get_image(assembly);
return (assembly, image);
+32 -1
View File
@@ -11,7 +11,10 @@ static class Mono
[LinkName(.C)]
public static extern MonoDomain* mono_jit_init(char8* file);
[LinkName(.C)]
public static extern void mono_jit_parse_options(int32 argc, char8** argv);
[LinkName(.C)]
public static extern void mono_jit_cleanup(MonoDomain* domain);
@@ -208,6 +211,24 @@ static class Mono
[LinkName(.C)]
public static extern char8* mono_error_get_message(MonoError* error);
[LinkName(.C)]
public static extern void mono_debug_init(DebugFormat debugFormat);
[LinkName(.C)]
public static extern void mono_debug_domain_create(MonoDomain* domain);
[LinkName(.C)]
public static extern void mono_debug_domain_unload(MonoDomain* domain);
[LinkName(.C)]
public static extern void mono_debug_open_image_from_memory(MonoImage *image, uint8* raw_contents, int32 size);
[LinkName(.C)]
public static extern void mono_thread_set_main(MonoThread* thread);
[LinkName(.C)]
public static extern MonoThread* mono_thread_current();
}
struct MonoDomain;
@@ -226,6 +247,8 @@ struct MonoObject;
struct MonoMethod;
struct MonoThread;
struct MonoException
{
void* _bla;
@@ -339,6 +362,14 @@ enum MonoMetaTableEnum : int32
}
enum DebugFormat : uint32
{
None,
Mono,
/** Deprecated, the mdb debugger is not longer supported. */
Debugger
}
enum FieldAttribute
{
FieldAccessMask = 0x0007,