Skip to content

Commit

Permalink
Added C# debugging support
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCherno committed Oct 31, 2022
1 parent 44b3535 commit a611994
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions Hazel/src/Hazel/Scripting/ScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "mono/metadata/assembly.h"
#include "mono/metadata/object.h"
#include "mono/metadata/tabledefs.h"
#include "mono/metadata/mono-debug.h"
#include "mono/metadata/threads.h"

#include "FileWatch.h"

Expand Down Expand Up @@ -67,7 +69,7 @@ namespace Hazel {
return buffer;
}

static MonoAssembly* LoadMonoAssembly(const std::filesystem::path& assemblyPath)
static MonoAssembly* LoadMonoAssembly(const std::filesystem::path& assemblyPath, bool loadPDB = false)
{
uint32_t fileSize = 0;
char* fileData = ReadBytes(assemblyPath, &fileSize);
Expand All @@ -83,6 +85,21 @@ namespace Hazel {
return nullptr;
}

if (loadPDB)
{
std::filesystem::path pdbPath = assemblyPath;
pdbPath.replace_extension(".pdb");

if (std::filesystem::exists(pdbPath))
{
uint32_t pdbFileSize = 0;
char* pdbFileData = ReadBytes(pdbPath, &pdbFileSize);
mono_debug_open_image_from_memory(image, (const mono_byte*)pdbFileData, pdbFileSize);
HZ_CORE_INFO("Loaded PDB {}", pdbPath);
delete[] pdbFileData;
}
}

std::string pathString = assemblyPath.string();
MonoAssembly* assembly = mono_assembly_load_from_full(image, pathString.c_str(), &status, 0);
mono_image_close(image);
Expand Down Expand Up @@ -149,6 +166,8 @@ namespace Hazel {
Scope<filewatch::FileWatch<std::string>> AppAssemblyFileWatcher;
bool AssemblyReloadPending = false;

bool EnableDebugging = true;

// Runtime

Scene* SceneContext = nullptr;
Expand Down Expand Up @@ -229,11 +248,27 @@ namespace Hazel {
{
mono_set_assemblies_path("mono/lib");

if (s_Data->EnableDebugging)
{
const char* argv[2] = {
"--debugger-agent=transport=dt_socket,address=127.0.0.1:2550,server=y,suspend=n,loglevel=3,logfile=MonoDebugger.log",
"--soft-breakpoints"
};

mono_jit_parse_options(2, (char**)argv);
mono_debug_init(MONO_DEBUG_FORMAT_MONO);
}

MonoDomain* rootDomain = mono_jit_init("HazelJITRuntime");
HZ_CORE_ASSERT(rootDomain);

// Store the root domain pointer
s_Data->RootDomain = rootDomain;

if (s_Data->EnableDebugging)
mono_debug_domain_create(s_Data->RootDomain);

mono_thread_set_main(mono_thread_current());
}

void ScriptEngine::ShutdownMono()
Expand All @@ -255,7 +290,7 @@ namespace Hazel {

// Move this maybe
s_Data->CoreAssemblyFilepath = filepath;
s_Data->CoreAssembly = Utils::LoadMonoAssembly(filepath);
s_Data->CoreAssembly = Utils::LoadMonoAssembly(filepath, s_Data->EnableDebugging);
s_Data->CoreAssemblyImage = mono_assembly_get_image(s_Data->CoreAssembly);
// Utils::PrintAssemblyTypes(s_Data->CoreAssembly);
}
Expand All @@ -264,7 +299,7 @@ namespace Hazel {
{
// Move this maybe
s_Data->AppAssemblyFilepath = filepath;
s_Data->AppAssembly = Utils::LoadMonoAssembly(filepath);
s_Data->AppAssembly = Utils::LoadMonoAssembly(filepath, s_Data->EnableDebugging);
auto assemb = s_Data->AppAssembly;
s_Data->AppAssemblyImage = mono_assembly_get_image(s_Data->AppAssembly);
auto assembi = s_Data->AppAssemblyImage;
Expand Down Expand Up @@ -474,7 +509,8 @@ namespace Hazel {

MonoObject* ScriptClass::InvokeMethod(MonoObject* instance, MonoMethod* method, void** params)
{
return mono_runtime_invoke(method, instance, params, nullptr);
MonoObject* exception = nullptr;
return mono_runtime_invoke(method, instance, params, &exception);
}

ScriptInstance::ScriptInstance(Ref<ScriptClass> scriptClass, Entity entity)
Expand Down

0 comments on commit a611994

Please sign in to comment.