Use embedded debug information for C# dll to have proper stack trace information on game scripts exceptions

This commit is contained in:
Wojciech Figat
2023-01-03 18:38:44 +01:00
parent 06b2bf0094
commit 26f8e5aa9e
6 changed files with 47 additions and 80 deletions

View File

@@ -1,27 +1,22 @@
#include "CoreCLR.h" // Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#include "CoreCLR.h"
#include "Engine/Core/Log.h" #include "Engine/Core/Log.h"
#include "Engine/Platform/Platform.h" #include "Engine/Platform/Platform.h"
#include "Engine/Platform/FileSystem.h" #include "Engine/Platform/FileSystem.h"
#include "Engine/Core/Types/DateTime.h" #include "Engine/Core/Types/DateTime.h"
#include "Engine/Debug/DebugLog.h" #include "Engine/Debug/DebugLog.h"
#include "Engine/Core/Collections/Dictionary.h" #include "Engine/Core/Collections/Dictionary.h"
#include <nethost.h> #include <nethost.h>
#include <coreclr_delegates.h> #include <coreclr_delegates.h>
#include <hostfxr.h> #include <hostfxr.h>
#if PLATFORM_WINDOWS #if PLATFORM_WINDOWS
#include <combaseapi.h> // CoTask* #include <combaseapi.h>
#undef SetEnvironmentVariable #undef SetEnvironmentVariable
#undef LoadLibrary #undef LoadLibrary
#endif #endif
#if COMPILE_WITH_PROFILER
#endif
static Dictionary<String, void*> cachedFunctions; static Dictionary<String, void*> cachedFunctions;
static String assemblyName = TEXT("FlaxEngine.CSharp");
#if PLATFORM_WINDOWS #if PLATFORM_WINDOWS
static const char_t* typeName = TEXT("FlaxEngine.NativeInterop, FlaxEngine.CSharp"); static const char_t* typeName = TEXT("FlaxEngine.NativeInterop, FlaxEngine.CSharp");
#else #else
@@ -42,7 +37,6 @@ bool CoreCLR::LoadHostfxr(const String& library_path_)
{ {
const FLAX_CORECLR_STRING& library_path = FLAX_CORECLR_STRING(library_path_); const FLAX_CORECLR_STRING& library_path = FLAX_CORECLR_STRING(library_path_);
char_t hostfxrPath[1024]; char_t hostfxrPath[1024];
size_t hostfxrPathSize = sizeof(hostfxrPath) / sizeof(char_t); size_t hostfxrPathSize = sizeof(hostfxrPath) / sizeof(char_t);

View File

@@ -1,7 +1,6 @@
#pragma once // Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
// FIXME #pragma once
#include <ThirdParty/mono-2.0/mono/metadata/blob.h>
#include "Engine/Core/Types/String.h" #include "Engine/Core/Types/String.h"
#include "Engine/Core/Collections/Array.h" #include "Engine/Core/Collections/Array.h"

View File

@@ -1,11 +1,12 @@
#include <ThirdParty/mono-2.0/mono/metadata/object.h> // Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#include <ThirdParty/mono-2.0/mono/metadata/appdomain.h>
#include "CoreCLR.h" #include "CoreCLR.h"
#include "Engine/Scripting/Types.h" #include "Engine/Scripting/Types.h"
#include "Engine/Core/Collections/Dictionary.h" #include "Engine/Core/Collections/Dictionary.h"
#include "Engine/Graphics/RenderView.h" #include "Engine/Graphics/RenderView.h"
#include "Engine/Core/Types/StringBuilder.h" #include "Engine/Core/Types/StringBuilder.h"
#include <ThirdParty/mono-2.0/mono/metadata/object.h>
#include <ThirdParty/mono-2.0/mono/metadata/appdomain.h>
#pragma warning(disable : 4297) #pragma warning(disable : 4297)
@@ -1066,15 +1067,6 @@ MONO_API void mono_thread_exit(void)
// Ignored // Ignored
} }
/*
* mono-debug.h
*/
MONO_API void mono_debug_open_image_from_memory(MonoImage* image, const mono_byte* raw_contents, int size)
{
// Ignored
}
/* /*
* reflection.h * reflection.h
*/ */

View File

@@ -42,7 +42,7 @@ typedef void MObject;
#define USE_MONO_PROFILER (COMPILE_WITH_PROFILER) #define USE_MONO_PROFILER (COMPILE_WITH_PROFILER)
// Enable/disable mono debugging // Enable/disable mono debugging
#define MONO_DEBUG_ENABLE (!BUILD_RELEASE) #define MONO_DEBUG_ENABLE (!BUILD_RELEASE && !USE_MONO)
#ifndef USE_MONO_AOT #ifndef USE_MONO_AOT
#define USE_MONO_AOT 0 #define USE_MONO_AOT 0

View File

@@ -225,7 +225,6 @@ namespace Flax.Build
args.Add("/target:library"); args.Add("/target:library");
args.Add("/platform:AnyCPU"); args.Add("/platform:AnyCPU");
args.Add("/debug+"); args.Add("/debug+");
args.Add("/debug:portable");
args.Add("/errorreport:prompt"); args.Add("/errorreport:prompt");
args.Add("/preferreduilang:en-US"); args.Add("/preferreduilang:en-US");
args.Add("/highentropyva+"); args.Add("/highentropyva+");
@@ -240,8 +239,13 @@ namespace Flax.Build
#if USE_NETCORE #if USE_NETCORE
args.Add("/langversion:11.0"); args.Add("/langversion:11.0");
args.Add("-nowarn:8632"); // Nullable args.Add("-nowarn:8632"); // Nullable
if (buildData.Configuration == TargetConfiguration.Release)
args.Add("/debug:portable");
else
args.Add("/debug:embedded"); // Embed pdb information into dll for proper stack trace information on C# exception in game code
#else #else
args.Add("/langversion:7.3"); args.Add("/langversion:7.3");
args.Add("/debug:portable");
#endif #endif
if (buildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings) if (buildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings)
args.Add("-nowarn:1591"); args.Add("-nowarn:1591");

View File

@@ -115,66 +115,13 @@ namespace Flax.Build.Projects.VisualStudio
// Default configuration // Default configuration
{ {
var configuration = defaultConfiguration; WriteConfiguration(project, csProjectFileContent, projectDirectory, defaultConfiguration);
var defines = string.Join(";", project.Defines);
if (configuration.TargetBuildOptions.ScriptingAPI.Defines.Count != 0)
{
if (defines.Length != 0)
defines += ";";
defines += string.Join(";", configuration.TargetBuildOptions.ScriptingAPI.Defines);
}
var outputPath = Utilities.MakePathRelativeTo(project.CSharp.OutputPath ?? configuration.TargetBuildOptions.OutputFolder, projectDirectory);
var intermediateOutputPath = Utilities.MakePathRelativeTo(project.CSharp.IntermediateOutputPath ?? Path.Combine(configuration.TargetBuildOptions.IntermediateFolder, "CSharp"), projectDirectory);
csProjectFileContent.AppendLine(string.Format(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == '{0}' \">", configuration.Name));
csProjectFileContent.AppendLine(" <DebugSymbols>true</DebugSymbols>");
csProjectFileContent.AppendLine(" <DebugType>portable</DebugType>");
csProjectFileContent.AppendLine(string.Format(" <Optimize>{0}</Optimize>", configuration.Configuration == TargetConfiguration.Debug ? "false" : "true"));
csProjectFileContent.AppendLine(string.Format(" <OutputPath>{0}\\</OutputPath>", outputPath));
csProjectFileContent.AppendLine(string.Format(" <BaseIntermediateOutputPath>{0}\\</BaseIntermediateOutputPath>", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" <IntermediateOutputPath>{0}\\</IntermediateOutputPath>", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" <DefineConstants>{0}</DefineConstants>", defines));
csProjectFileContent.AppendLine(" <ErrorReport>prompt</ErrorReport>");
csProjectFileContent.AppendLine(" <WarningLevel>4</WarningLevel>");
csProjectFileContent.AppendLine(" <AllowUnsafeBlocks>true</AllowUnsafeBlocks>");
if (configuration.TargetBuildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings)
csProjectFileContent.AppendLine(" <NoWarn>1591</NoWarn>");
csProjectFileContent.AppendLine(string.Format(" <DocumentationFile>{0}\\{1}.CSharp.xml</DocumentationFile>", outputPath, project.BaseName));
csProjectFileContent.AppendLine(" <UseVSHostingProcess>true</UseVSHostingProcess>");
csProjectFileContent.AppendLine(" </PropertyGroup>");
csProjectFileContent.AppendLine("");
} }
// Configurations // Configurations
foreach (var configuration in project.Configurations) foreach (var configuration in project.Configurations)
{ {
var defines = string.Join(";", project.Defines); WriteConfiguration(project, csProjectFileContent, projectDirectory, configuration);
if (configuration.TargetBuildOptions.ScriptingAPI.Defines.Count != 0)
{
if (defines.Length != 0)
defines += ";";
defines += string.Join(";", configuration.TargetBuildOptions.ScriptingAPI.Defines);
}
var outputPath = Utilities.MakePathRelativeTo(project.CSharp.OutputPath ?? configuration.TargetBuildOptions.OutputFolder, projectDirectory);
var intermediateOutputPath = Utilities.MakePathRelativeTo(project.CSharp.IntermediateOutputPath ?? Path.Combine(configuration.TargetBuildOptions.IntermediateFolder, "CSharp"), projectDirectory);
csProjectFileContent.AppendLine(string.Format(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == '{0}' \">", configuration.Name));
csProjectFileContent.AppendLine(" <DebugSymbols>true</DebugSymbols>");
csProjectFileContent.AppendLine(" <DebugType>portable</DebugType>");
csProjectFileContent.AppendLine(string.Format(" <Optimize>{0}</Optimize>", configuration.Configuration == TargetConfiguration.Release ? "true" : "false"));
csProjectFileContent.AppendLine(string.Format(" <OutputPath>{0}\\</OutputPath>", outputPath));
csProjectFileContent.AppendLine(string.Format(" <BaseIntermediateOutputPath>{0}\\</BaseIntermediateOutputPath>", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" <IntermediateOutputPath>{0}\\</IntermediateOutputPath>", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" <DefineConstants>{0}</DefineConstants>", defines));
csProjectFileContent.AppendLine(" <ErrorReport>prompt</ErrorReport>");
csProjectFileContent.AppendLine(" <WarningLevel>4</WarningLevel>");
csProjectFileContent.AppendLine(" <AllowUnsafeBlocks>true</AllowUnsafeBlocks>");
if (configuration.TargetBuildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings)
csProjectFileContent.AppendLine(" <NoWarn>1591</NoWarn>");
csProjectFileContent.AppendLine(string.Format(" <DocumentationFile>{0}\\{1}.CSharp.xml</DocumentationFile>", outputPath, project.BaseName));
csProjectFileContent.AppendLine(" <UseVSHostingProcess>true</UseVSHostingProcess>");
csProjectFileContent.AppendLine(" </PropertyGroup>");
csProjectFileContent.AppendLine("");
} }
// References // References
@@ -258,5 +205,36 @@ namespace Flax.Build.Projects.VisualStudio
Utilities.WriteFileIfChanged(project.Path, csProjectFileContent.ToString()); Utilities.WriteFileIfChanged(project.Path, csProjectFileContent.ToString());
} }
} }
private void WriteConfiguration(Project project, StringBuilder csProjectFileContent, string projectDirectory, Project.ConfigurationData configuration)
{
var defines = string.Join(";", project.Defines);
if (configuration.TargetBuildOptions.ScriptingAPI.Defines.Count != 0)
{
if (defines.Length != 0)
defines += ";";
defines += string.Join(";", configuration.TargetBuildOptions.ScriptingAPI.Defines);
}
var outputPath = Utilities.MakePathRelativeTo(project.CSharp.OutputPath ?? configuration.TargetBuildOptions.OutputFolder, projectDirectory);
var intermediateOutputPath = Utilities.MakePathRelativeTo(project.CSharp.IntermediateOutputPath ?? Path.Combine(configuration.TargetBuildOptions.IntermediateFolder, "CSharp"), projectDirectory);
csProjectFileContent.AppendLine(string.Format(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == '{0}' \">", configuration.Name));
csProjectFileContent.AppendLine(" <DebugSymbols>true</DebugSymbols>");
csProjectFileContent.AppendLine(string.Format(" <DebugType>{0}</DebugType>", configuration.Configuration == TargetConfiguration.Release ? "portable" : "embedded"));
csProjectFileContent.AppendLine(string.Format(" <Optimize>{0}</Optimize>", configuration.Configuration == TargetConfiguration.Release ? "true" : "false"));
csProjectFileContent.AppendLine(string.Format(" <OutputPath>{0}\\</OutputPath>", outputPath));
csProjectFileContent.AppendLine(string.Format(" <BaseIntermediateOutputPath>{0}\\</BaseIntermediateOutputPath>", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" <IntermediateOutputPath>{0}\\</IntermediateOutputPath>", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" <DefineConstants>{0}</DefineConstants>", defines));
csProjectFileContent.AppendLine(" <ErrorReport>prompt</ErrorReport>");
csProjectFileContent.AppendLine(" <WarningLevel>4</WarningLevel>");
csProjectFileContent.AppendLine(" <AllowUnsafeBlocks>true</AllowUnsafeBlocks>");
if (configuration.TargetBuildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings)
csProjectFileContent.AppendLine(" <NoWarn>1591</NoWarn>");
csProjectFileContent.AppendLine(string.Format(" <DocumentationFile>{0}\\{1}.CSharp.xml</DocumentationFile>", outputPath, project.BaseName));
csProjectFileContent.AppendLine(" <UseVSHostingProcess>true</UseVSHostingProcess>");
csProjectFileContent.AppendLine(" </PropertyGroup>");
csProjectFileContent.AppendLine("");
}
} }
} }