Compare commits

...

1 Commits

Author SHA1 Message Date
75184d9a6e add c# debugging support 2025-10-26 16:08:38 +08:00
2 changed files with 45 additions and 5 deletions

View File

@ -320,7 +320,7 @@ namespace Hazel
static float fpsRefreshTime = 0.0f; static float fpsRefreshTime = 0.0f;
static float displayedFPS = 0; static float displayedFPS = 0;
float currentTime = ImGui::GetTime(); float currentTime = static_cast<float>(ImGui::GetTime());
// 每秒更新一次要显示的FPS // 每秒更新一次要显示的FPS
if (currentTime - fpsRefreshTime >= 1.0f) if (currentTime - fpsRefreshTime >= 1.0f)

View File

@ -13,6 +13,8 @@
#include "mono/jit/jit.h" #include "mono/jit/jit.h"
#include "mono/metadata/assembly.h" #include "mono/metadata/assembly.h"
#include "mono/metadata/attrdefs.h" #include "mono/metadata/attrdefs.h"
#include "mono/metadata/mono-debug.h"
#include "mono/metadata/threads.h"
namespace Hazel namespace Hazel
{ {
@ -72,7 +74,7 @@ namespace Hazel
return buffer; return buffer;
} }
MonoAssembly* LoadMonoAssembly(const std::filesystem::path& assemblyPath) MonoAssembly* LoadMonoAssembly(const std::filesystem::path& assemblyPath, bool loadPDB = false)
{ {
HZ_CORE_DEBUG("Load Assemble: {0}", assemblyPath.string().c_str()); HZ_CORE_DEBUG("Load Assemble: {0}", assemblyPath.string().c_str());
uint32_t fileSize = 0; uint32_t fileSize = 0;
@ -88,6 +90,24 @@ namespace Hazel
return nullptr; return nullptr;
} }
if (loadPDB)
{
HZ_CORE_INFO("loading pdb file...");
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, fileSize);
HZ_CORE_INFO("loaded pdb file: {0}", pdbPath.string());
HZ_CORE_INFO(" size: {0}", pdbFileSize);
delete[] pdbFileData;
}
}
std::string string = assemblyPath.string(); std::string string = assemblyPath.string();
MonoAssembly* assembly = mono_assembly_load_from_full(image, string.c_str(), &status, 0); MonoAssembly* assembly = mono_assembly_load_from_full(image, string.c_str(), &status, 0);
mono_image_close(image); mono_image_close(image);
@ -152,6 +172,8 @@ namespace Hazel
Scope<filewatch::FileWatch<std::string>> AppAssemblyFileWatcher; Scope<filewatch::FileWatch<std::string>> AppAssemblyFileWatcher;
bool AssemblyReloading = false; bool AssemblyReloading = false;
bool EnableDebugging = false;
// runtime // runtime
Scene* SceneContext = nullptr; Scene* SceneContext = nullptr;
}; };
@ -235,7 +257,7 @@ namespace Hazel
// TODO: move this later // TODO: move this later
s_Data->CoreAssemblyFilePath = assemblePath; s_Data->CoreAssemblyFilePath = assemblePath;
s_Data->CoreAssembly = Utils::LoadMonoAssembly(assemblePath); s_Data->CoreAssembly = Utils::LoadMonoAssembly(assemblePath, s_Data->EnableDebugging);
s_Data->CoreAssemblyImage = mono_assembly_get_image(s_Data->CoreAssembly); s_Data->CoreAssemblyImage = mono_assembly_get_image(s_Data->CoreAssembly);
// Utils::PrintAssemblyTypes(s_Data->CoreAssembly); // Utils::PrintAssemblyTypes(s_Data->CoreAssembly);
@ -245,7 +267,7 @@ namespace Hazel
{ {
// TODO: move this later // TODO: move this later
s_Data->AppAssemblyFilePath = assemblePath; s_Data->AppAssemblyFilePath = assemblePath;
s_Data->AppAssembly = Utils::LoadMonoAssembly(assemblePath); s_Data->AppAssembly = Utils::LoadMonoAssembly(assemblePath, s_Data->EnableDebugging);
s_Data->AppAssemblyImage = mono_assembly_get_image(s_Data->AppAssembly); s_Data->AppAssemblyImage = mono_assembly_get_image(s_Data->AppAssembly);
s_Data->AppAssemblyFileWatcher = CreateScope<filewatch::FileWatch<std::string>>(assemblePath.string(), OnAssemblyFileSystemEvent); s_Data->AppAssemblyFileWatcher = CreateScope<filewatch::FileWatch<std::string>>(assemblePath.string(), OnAssemblyFileSystemEvent);
@ -408,6 +430,18 @@ namespace Hazel
mono_set_dirs("Resources", "Resources"); mono_set_dirs("Resources", "Resources");
mono_set_assemblies_path("Resources/mono"); mono_set_assemblies_path("Resources/mono");
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"); MonoDomain* rootDomain = mono_jit_init("HazelJITRuntime");
const char* monoPath = mono_assembly_getrootdir(); const char* monoPath = mono_assembly_getrootdir();
@ -419,6 +453,11 @@ namespace Hazel
} }
// Storage the root Domain ptr // Storage the root Domain ptr
s_Data->RootDomain = rootDomain; s_Data->RootDomain = rootDomain;
if (s_Data->EnableDebugging)
mono_debug_domain_create(s_Data->RootDomain);
mono_thread_set_main(mono_thread_current());
} }
void ScriptEngine::MonoShutdown() void ScriptEngine::MonoShutdown()
@ -468,7 +507,8 @@ namespace Hazel
MonoObject* ScriptClass::InvokeMethod(MonoObject* instance, MonoMethod* method, void** param) MonoObject* ScriptClass::InvokeMethod(MonoObject* instance, MonoMethod* method, void** param)
{ {
return mono_runtime_invoke(method, instance, param, nullptr); MonoObject* expection = nullptr;
return mono_runtime_invoke(method, instance, param, &expection);
} }