kniEngine / kni

KNI is a cross-platform C# game framework.
Other
139 stars 9 forks source link

How can I see a callstack on exceptions in C# when debugging a BlazorGL project #1847

Closed vchelaru closed 1 month ago

vchelaru commented 1 month ago

I'm not sure if this is a Kni issue but any advise would be much appreciated. If an exception is thrown, I do not see a callstack, either in Visual Studio or in the web console.

To reproduce, add the following code to a new kni project.

Microsoft.Xna.Framework.Input.GamePad.SetVibration(0, 1f, 1f);

I realize that this is not currently implemented in Kni, that's why I'm intentionally using this function. When this occurs, I get the following:

The game screen turns purple instead of CornflowerBlue image

Visual Studio does not hit a breakpoint - it shows the controls for a running app as if no exception has occurred image

The browser console (CTRL+SHIFT+i) shows an exception but does not show a callstack image

Visual Studio output window shows exceptions but does not show a callstack

Uncaught ManagedError ManagedError: The method or operation is not implemented.
    at Jn (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\marshal-to-js.ts:349:18)
    at kr (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\invoke-cs.ts:277:19)
    at <anonymous> (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\invoke-cs.ts:247:13)
    at invokeDotNetFromJS (localhost꞉5259/_framework/blazor.webassembly.js:1:45370)
    at invokeDotNetMethod (localhost꞉5259/_framework/blazor.webassembly.js:1:3485)
    at invokeMethod (localhost꞉5259/_framework/blazor.webassembly.js:1:5372)
    at tickJS (C:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\index.html:75:32)
    --- requestAnimationFrame ---
    at window.initRenderJS (C:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\index.html:92:20)
    at <anonymous> (localhost꞉5259/_framework/blazor.webassembly.js:1:2878)
    at beginInvokeJSFromDotNet (localhost꞉5259/_framework/blazor.webassembly.js:1:2835)
    at vn (localhost꞉5259/_framework/blazor.webassembly.js:1:58849)
    at <anonymous> (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\invoke-js.ts:233:31)
    at Tl (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\invoke-js.ts:276:5)
    at $func349 (wasm/00b21cf6:47411:1)
    at $func245 (wasm/00b21cf6:40189:1)
    at $func238 (wasm/00b21cf6:15307:1)
    at $func272 (wasm/00b21cf6:42415:1)
    at $func3185 (wasm/00b21cf6:441589:1)
    at $func2505 (wasm/00b21cf6:359552:1)
    at $func2511 (wasm/00b21cf6:360550:1)
    at $func2535 (wasm/00b21cf6:365402:1)
    at $mono_wasm_invoke_method_bound (wasm/00b21cf6:6628:1)
    at Module._mono_wasm_invoke_method_bound (localhost꞉5259/_framework/dotnet.native.8.0.7.cfqr3xztjy.js:8:123345)
    at kr (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\invoke-cs.ts:273:29)
    at l.javaScriptExports.complete_task (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\managed-exports.ts:142:13)
    at <anonymous> (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\marshal-to-cs.ts:335:46)
    --- Promise.then ---
    at mo (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\marshal-to-cs.ts:329:11)
    at <anonymous> (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\marshal-to-cs.ts:83:9)
    at <anonymous> (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\invoke-js.ts:177:13)
    at Tl (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\invoke-js.ts:276:5)
    at $func349 (wasm/00b21cf6:47411:1)
    at $func245 (wasm/00b21cf6:40189:1)
    at $func238 (wasm/00b21cf6:15307:1)
    at $func272 (wasm/00b21cf6:42415:1)
    at $func3185 (wasm/00b21cf6:441589:1)
    at $func2505 (wasm/00b21cf6:359552:1)
    at $func2504 (wasm/00b21cf6:359503:1)
    at $func1874 (wasm/00b21cf6:287551:1)
    at $func349 (wasm/00b21cf6:47468:1)
    at $func245 (wasm/00b21cf6:40189:1)
    at $func238 (wasm/00b21cf6:15307:1)
    at $func272 (wasm/00b21cf6:42415:1)
    at $func3185 (wasm/00b21cf6:441589:1)
    at $func2505 (wasm/00b21cf6:359552:1)
    at $func2511 (wasm/00b21cf6:360550:1)
    at $func2535 (wasm/00b21cf6:365402:1)
    at $mono_wasm_invoke_method_bound (wasm/00b21cf6:6628:1)
    at Module._mono_wasm_invoke_method_bound (localhost꞉5259/_framework/dotnet.native.8.0.7.cfqr3xztjy.js:8:123345)
    at kr (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\invoke-cs.ts:273:29)
    at l.javaScriptExports.call_entry_point (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\managed-exports.ts:60:13)
    at Oc (c:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\_framework\https:\raw.githubusercontent.com\dotnet\runtime\2aade6beb02ea367fd97c4070a4198802fe61c03\src\mono\wasm\runtime\run.ts:44:45)
    at callEntryPoint (localhost꞉5259/_framework/blazor.webassembly.js:1:45490)
    at mn (localhost꞉5259/_framework/blazor.webassembly.js:1:57708)
    --- await ---
    at fn (localhost꞉5259/_framework/blazor.webassembly.js:1:54176)
    at An (localhost꞉5259/_framework/blazor.webassembly.js:1:60103)
    at <anonymous> (C:\Users\Owner\source\repos\XboxGamepadTest\XboxGamepadTest\wwwroot\index.html:36:16)
Uncaught TypeError TypeError: Cannot read properties of null (reading 'getAttribute')
    at initialize (localhost꞉5259/_vs/browserLink:65:1411)
    at <anonymous> (localhost꞉5259/_vs/browserLink:65:3427)
    at <anonymous> (localhost꞉5259/_vs/browserLink:65:3522)

This makes debugging very difficult. Is there a way to get a callstack? I'm not sure if I'm missing some setting in Visual Studio or in my project to break on exceptions or get callstacks.

What version of KNI does the bug occur on:

What operating system are you using:

What KNI platform are you using:

vchelaru commented 1 month ago

I found a workaround, not sure if this is the proper way to do it. I can wrap the Update method:

        protected override void Update(GameTime gameTime)
        {
            try
            {
                GamePadState gamePadState = default;
                try { gamePadState = GamePad.GetState(PlayerIndex.One); }
                catch (NotImplementedException) { /* ignore gamePadState */ }

                Microsoft.Xna.Framework.Input.GamePad.SetVibration(0, 1f, 1f);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e);
                throw;
            }
            base.Update(gameTime);
        }

This results in the callstack showing in the output window, which is a big improvement. Is this the best we can do?

vchelaru commented 1 month ago

I'm going to close this issue since there is a workaround here. Also, Visual Studio 17.11 seems to also have improved debugging of blazor wasm builds - I'm now getting callstacks and code breaking when I wasn't before with 17.10.