Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnSpy starts the application without attaching a debugger #297

Open
murolem opened this issue Feb 13, 2024 · 13 comments
Open

dnSpy starts the application without attaching a debugger #297

murolem opened this issue Feb 13, 2024 · 13 comments
Labels
bug Something isn't working

Comments

@murolem
Copy link

murolem commented Feb 13, 2024

dnSpyEx version

6.5.0

Describe the Bug

I'm trying to debug a .NET 7.0 dll that the game loads, but dnSpy seems to not do anything. It attaches just for a mere moment, then stops doing anything, launching the application.

The game in question is Cosmoteer.

I have these settings (I didn't find the option to switch languages):
image

From I've read from the docs, for debugging unity games you need patched dlls, but there's no mention of non-unity games? How to debug those? I didn't find any info searching either, like there's something obvious that I'm missing.

If the game does indeed need some dll patched (I guess for invoking dnSpy methods or something?), I happen to have a C++ injector ready (that I use for modding the game by injecting C# dlls).

How To Reproduce

  1. Open cosmoteer.dll inside bin dir of Cosmoteer game with dnSpy.
  2. Set a break point, try to launch the debugger with settings that I provided above.

Expected Behavior

  1. Debugger works.

Actual Behavior

  1. Debugger doesn't do anything except launch the game.

Additional Context

No response

@murolem murolem added the bug Something isn't working label Feb 13, 2024
@ElektroKill
Copy link
Member

Hi, could you perhaps provide me with the DLL and any .json files located next to it via email (I assume the game is paid software, and redistributing the component publicly would not be the wisest idea)?

[email protected]

Thanks in advance!

From I've read from the docs, for debugging unity games you need patched dlls, but there's no mention of non-unity games? How to debug those? I didn't find any info searching either, like there's something obvious that I'm missing.

If the game does indeed need some dll patched (I guess for invoking dnSpy methods or something?), I happen to have a C++ injector ready (that I use for modding the game by injecting C# dlls).

The information about Unity games does not apply to non-Unity games. No patching should be necessary to get your .NET 7 application/game working under the debugger. If you can provide the file alongside the .json files next to it, I will be able to investigate the cause of the error further!

@murolem
Copy link
Author

murolem commented Feb 18, 2024

@ElektroKill hi!

I've sent you the DLL file and the .json file. Look for subject dnSpy/issues/297

@ElektroKill
Copy link
Member

Hi, I took a look at the files you provided. Looking at the contents of .runtimeconfig.json file it's evident that the file comes with an included copy of the .NET runtime. This means the game was published in self-contained mode and does not contain the necessary runtime components for debugging. There are two ways to tackle this problem:

  1. Install the exact .NET runtime the game requires on your computer. In this case, this would be .NET 7.0.14. After the installation, try to debug the file again with dnSpy. The latest dnSpy versions can detect the lack of these debug components and load them in from runtimes installed on the system rather than only relying on the files included in the runtime the file is using. This should allow you to debug the game as usual.
  2. It is also possible to convert the self-contained application into a normal application that uses the .NET installed on the system rather than the installation it ships with. This requires modifying the .runtimeconfig.json and .deps.json files. AFter applying th necessary changes dnSpy should be able to debug the file without issues. Instructions on how to convert a published file into a normal file can be found here (begin from step 7 as in this case the game does not use a single file bundle and thus does not need to be unpacked): Can't attach to .NET 5 application #48 (comment)

The first method is preferable as it does not require modification of the game files and I suggest you try that first. If it doesn't work, try the second method.

Let me know of the results and if the issue can be marked as closed!

@murolem
Copy link
Author

murolem commented Feb 19, 2024

@ElektroKill First one didn't work, sadly. Installed this version of .net, tried running in both .net and .net framework modes. Same result.

Second one didn't work either. Got the same error when trying to run with host:
image

But without it got this:
image
Idk why it's complaining. Clicking OK opened the download for 7.0.16 version, which I've also installed. Same error.

Then I tried installing 7.0.14 .net binaries (previously only installed the SDK) - same error.
Installed the Windows Desktop Runtime - same error.

P.S. I haven’t seen anyone respond so quickly, but at the same time so thoroughly - cheers.

@ElektroKill
Copy link
Member

Can you make sure that you installed the x64 version of the 7.0.14 runtime? You can do this by running the dotnet --info command in the terminal and posting the output here!

If you could post the contents of C:\Program Files\dotnet\shared\Microsoft.NETCore.App and C:\Program Files (x86)\dotnet\shared\Microsoft.NETCore.App it would also be helpful.

@murolem
Copy link
Author

murolem commented Feb 19, 2024

@ElektroKill
Copy link
Member

ElektroKill commented Feb 19, 2024

Huh, it looks like you do have the appropriate runtimes installed. Can you check please if inside the directory where cosmoteer.dll resides the following files are present:

  • mscordaccore.dll
  • mscordbi.dll

I managed to obtain a copy of the game and for me those files are present and I am able to attach using dnSpy.exe when using the 64 bit version.

I used these options to start debugging:
image

@murolem
Copy link
Author

murolem commented Feb 20, 2024

The files are indeed present.

The debugger still attaches only for a fraction of a second.
image

@ElektroKill
Copy link
Member

Okay, that's very strange. I'm not sure what could be going on here. Can you please inform me of the version of the dnSpy build you are using? (.NET vs .Net Framework and the architecture?). Information is found in the title bar. I'm using the .NET Framework 64bit version and I can debug the build of the game I obtained successfully.
image

@murolem
Copy link
Author

murolem commented Feb 21, 2024

Same build, but I was using .net version. Downloaded framework version, same result sadly.

@murolem
Copy link
Author

murolem commented Feb 21, 2024

I've even reinstalled the whole game - it attached for a little bit longer, but still, only momentarily 😭

@ElektroKill
Copy link
Member

That is very strange. At this point, I honestly have no idea what could be causing dnSpy to attach. All the necessary files for debugging appear to be present but for some reason when dnSpy awaits the .NET debugger startup event it times out after the 5 seconds.

uint WAIT_MS = (uint)info.ConnectionTimeout.TotalMilliseconds;
for (;;) {
uint res = NativeMethods.WaitForSingleObject(startupEvent, WAIT_MS);
if (res == 0)
break;
if (res == NativeMethods.WAIT_FAILED)
throw new Exception($"Error waiting for startup event: 0x{Marshal.GetLastWin32Error():X8}");
if (res == NativeMethods.WAIT_TIMEOUT) {
if (keepWaiting())
continue;
throw new TimeoutException("Waiting for CoreCLR timed out. Debug 32-bit .NET apps with 32-bit dnSpy (dnSpy-x86.exe), and 64-bit .NET apps with 64-bit dnSpy (dnSpy.exe).");
}
Debug.Fail($"Unknown result from WaitForMultipleObjects: 0x{res:X8}");
throw new Exception("Error waiting for startup event");
}

Can you try one more thing and that is let the game fully load and then try to attach dnSpy through the Attach to Process option under the Debug menu? Let me know of the results.

@murolem
Copy link
Author

murolem commented Feb 23, 2024

Hmm, it attaches...but doesn't seem to be able to break on breakpoints, displaying there a little error symbol.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants