From 26f8e5aa9e384ff5a784989a6499af8499bd981d Mon Sep 17 00:00:00 2001 From: Wojciech Figat Date: Tue, 3 Jan 2023 18:38:44 +0100 Subject: [PATCH] Use `embedded` debug information for C# dll to have proper stack trace information on game scripts exceptions --- Source/Engine/Scripting/DotNet/CoreCLR.cpp | 12 +-- Source/Engine/Scripting/DotNet/CoreCLR.h | 5 +- Source/Engine/Scripting/DotNet/MonoApi.cpp | 14 +-- Source/Engine/Scripting/Types.h | 2 +- .../Flax.Build/Build/DotNet/Builder.DotNet.cs | 6 +- .../VisualStudio/CSSDKProjectGenerator.cs | 88 +++++++------------ 6 files changed, 47 insertions(+), 80 deletions(-) diff --git a/Source/Engine/Scripting/DotNet/CoreCLR.cpp b/Source/Engine/Scripting/DotNet/CoreCLR.cpp index 9235443cb..cf427d3c1 100644 --- a/Source/Engine/Scripting/DotNet/CoreCLR.cpp +++ b/Source/Engine/Scripting/DotNet/CoreCLR.cpp @@ -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/Platform/Platform.h" #include "Engine/Platform/FileSystem.h" #include "Engine/Core/Types/DateTime.h" #include "Engine/Debug/DebugLog.h" #include "Engine/Core/Collections/Dictionary.h" - #include #include #include - #if PLATFORM_WINDOWS -#include // CoTask* +#include #undef SetEnvironmentVariable #undef LoadLibrary #endif -#if COMPILE_WITH_PROFILER -#endif - static Dictionary cachedFunctions; -static String assemblyName = TEXT("FlaxEngine.CSharp"); #if PLATFORM_WINDOWS static const char_t* typeName = TEXT("FlaxEngine.NativeInterop, FlaxEngine.CSharp"); #else @@ -42,7 +37,6 @@ bool CoreCLR::LoadHostfxr(const String& library_path_) { const FLAX_CORECLR_STRING& library_path = FLAX_CORECLR_STRING(library_path_); - char_t hostfxrPath[1024]; size_t hostfxrPathSize = sizeof(hostfxrPath) / sizeof(char_t); diff --git a/Source/Engine/Scripting/DotNet/CoreCLR.h b/Source/Engine/Scripting/DotNet/CoreCLR.h index 1d1bec932..3a8d3e480 100644 --- a/Source/Engine/Scripting/DotNet/CoreCLR.h +++ b/Source/Engine/Scripting/DotNet/CoreCLR.h @@ -1,7 +1,6 @@ -#pragma once +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. -// FIXME -#include +#pragma once #include "Engine/Core/Types/String.h" #include "Engine/Core/Collections/Array.h" diff --git a/Source/Engine/Scripting/DotNet/MonoApi.cpp b/Source/Engine/Scripting/DotNet/MonoApi.cpp index 8facba15e..8f90bfa32 100644 --- a/Source/Engine/Scripting/DotNet/MonoApi.cpp +++ b/Source/Engine/Scripting/DotNet/MonoApi.cpp @@ -1,11 +1,12 @@ -#include -#include +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. #include "CoreCLR.h" #include "Engine/Scripting/Types.h" #include "Engine/Core/Collections/Dictionary.h" #include "Engine/Graphics/RenderView.h" #include "Engine/Core/Types/StringBuilder.h" +#include +#include #pragma warning(disable : 4297) @@ -1066,15 +1067,6 @@ MONO_API void mono_thread_exit(void) // 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 */ diff --git a/Source/Engine/Scripting/Types.h b/Source/Engine/Scripting/Types.h index f6251d184..d59bb33e2 100644 --- a/Source/Engine/Scripting/Types.h +++ b/Source/Engine/Scripting/Types.h @@ -42,7 +42,7 @@ typedef void MObject; #define USE_MONO_PROFILER (COMPILE_WITH_PROFILER) // Enable/disable mono debugging -#define MONO_DEBUG_ENABLE (!BUILD_RELEASE) +#define MONO_DEBUG_ENABLE (!BUILD_RELEASE && !USE_MONO) #ifndef USE_MONO_AOT #define USE_MONO_AOT 0 diff --git a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs index 1ddc390f4..f0530ba49 100644 --- a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs +++ b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs @@ -225,7 +225,6 @@ namespace Flax.Build args.Add("/target:library"); args.Add("/platform:AnyCPU"); args.Add("/debug+"); - args.Add("/debug:portable"); args.Add("/errorreport:prompt"); args.Add("/preferreduilang:en-US"); args.Add("/highentropyva+"); @@ -240,8 +239,13 @@ namespace Flax.Build #if USE_NETCORE args.Add("/langversion:11.0"); 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 args.Add("/langversion:7.3"); + args.Add("/debug:portable"); #endif if (buildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings) args.Add("-nowarn:1591"); diff --git a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs index 69a509d53..57717d16e 100644 --- a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs +++ b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs @@ -115,66 +115,13 @@ namespace Flax.Build.Projects.VisualStudio // Default configuration { - var configuration = 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(" ", configuration.Name)); - csProjectFileContent.AppendLine(" true"); - csProjectFileContent.AppendLine(" portable"); - csProjectFileContent.AppendLine(string.Format(" {0}", configuration.Configuration == TargetConfiguration.Debug ? "false" : "true")); - csProjectFileContent.AppendLine(string.Format(" {0}\\", outputPath)); - csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath)); - csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath)); - csProjectFileContent.AppendLine(string.Format(" {0}", defines)); - csProjectFileContent.AppendLine(" prompt"); - csProjectFileContent.AppendLine(" 4"); - csProjectFileContent.AppendLine(" true"); - if (configuration.TargetBuildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings) - csProjectFileContent.AppendLine(" 1591"); - csProjectFileContent.AppendLine(string.Format(" {0}\\{1}.CSharp.xml", outputPath, project.BaseName)); - csProjectFileContent.AppendLine(" true"); - csProjectFileContent.AppendLine(" "); - csProjectFileContent.AppendLine(""); + WriteConfiguration(project, csProjectFileContent, projectDirectory, defaultConfiguration); } // Configurations foreach (var configuration in project.Configurations) { - 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(" ", configuration.Name)); - csProjectFileContent.AppendLine(" true"); - csProjectFileContent.AppendLine(" portable"); - csProjectFileContent.AppendLine(string.Format(" {0}", configuration.Configuration == TargetConfiguration.Release ? "true" : "false")); - csProjectFileContent.AppendLine(string.Format(" {0}\\", outputPath)); - csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath)); - csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath)); - csProjectFileContent.AppendLine(string.Format(" {0}", defines)); - csProjectFileContent.AppendLine(" prompt"); - csProjectFileContent.AppendLine(" 4"); - csProjectFileContent.AppendLine(" true"); - if (configuration.TargetBuildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings) - csProjectFileContent.AppendLine(" 1591"); - csProjectFileContent.AppendLine(string.Format(" {0}\\{1}.CSharp.xml", outputPath, project.BaseName)); - csProjectFileContent.AppendLine(" true"); - csProjectFileContent.AppendLine(" "); - csProjectFileContent.AppendLine(""); + WriteConfiguration(project, csProjectFileContent, projectDirectory, configuration); } // References @@ -258,5 +205,36 @@ namespace Flax.Build.Projects.VisualStudio 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(" ", configuration.Name)); + csProjectFileContent.AppendLine(" true"); + csProjectFileContent.AppendLine(string.Format(" {0}", configuration.Configuration == TargetConfiguration.Release ? "portable" : "embedded")); + csProjectFileContent.AppendLine(string.Format(" {0}", configuration.Configuration == TargetConfiguration.Release ? "true" : "false")); + csProjectFileContent.AppendLine(string.Format(" {0}\\", outputPath)); + csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath)); + csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath)); + csProjectFileContent.AppendLine(string.Format(" {0}", defines)); + csProjectFileContent.AppendLine(" prompt"); + csProjectFileContent.AppendLine(" 4"); + csProjectFileContent.AppendLine(" true"); + if (configuration.TargetBuildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings) + csProjectFileContent.AppendLine(" 1591"); + csProjectFileContent.AppendLine(string.Format(" {0}\\{1}.CSharp.xml", outputPath, project.BaseName)); + csProjectFileContent.AppendLine(" true"); + csProjectFileContent.AppendLine(" "); + csProjectFileContent.AppendLine(""); + } } }