HarmonyX icon indicating copy to clipboard operation
HarmonyX copied to clipboard

Missing information on how to debug with dnSpy using Harmony

Open Muchaszewski opened this issue 4 years ago • 2 comments

I have been following information from this wiki site on how to debug with dnSpy with DumpAssemblies. However, this looks like it doesn't apply to Harmony as there is no mention and testing is confirms my worries.

I have created a simple Transpiler patch using Harmony and I want to debug it further.

        private Harmony _harmonyInstance;
        public void Awake()
        {
            HarmonyFileLog.Enabled = true;
            _harmonyInstance = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());
        }

[HarmonyPatch(typeof(PlayerAction_Build), nameof(PlayerAction_Build.CreatePrebuilds))]
        private class Transpiler_PlayerAction_Build
        {
            static FieldInfo searchedVFInpudOnDown = AccessTools.Field(typeof(VFInput.InputValue), nameof(VFInput.InputValue.onDown));
            static FieldInfo requestedVFInpudOnDown = AccessTools.Field(typeof(VFInput.InputValue), nameof(VFInput.InputValue.pressing));

            private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
            {
                return new CodeMatcher(instructions).MatchForward(false,
                        new CodeMatch(OpCodes.Ldfld, searchedVFInpudOnDown))
                    .SetOperandAndAdvance(requestedVFInpudOnDown)
                    .InstructionEnumeration();
            }
        }

I have enabled as instructed

LoadDumpedAssemblies = true
BreakBeforeLoadAssemblies = true

and the results are not promising image

The only dumped assembly is UnityEngine.CoreModule.dll and setting BreakBeforeLoadAssemblies to true, crashes the game while loading.

Assembly-CSharp is not reacting to my breakpoints anymore for the modified method!

Callstack from this method is marked as ??? in dnSpy image

Is there any way to debug using HarmonyX or I need to go to pre-patch assembly route?

Muchaszewski avatar Jan 27 '21 21:01 Muchaszewski

Assuming you use bepinex, to debug harmony patched methods you have to change harmony backend to cecil in bepinex.cfg. You still can't easily set a breakpoint inside the method but you can step into it and see it in call stack.

For debugging preloader patched assemblies check https://bepinex.github.io/bepinex_docs/master/articles/advanced/debug/assemblies_dnSpy.html

ManlyMarco avatar Jan 27 '21 23:01 ManlyMarco

Thank you, I have found this option in the settings, I missed it for the first time.

I suggest then that you should add this information to the wiki for either HarmonyX or to the articles mentioned above, on how to debug those methods that were patched.

As for this point, and I think this should be mentioned in the wiki for how to debug this stuff for less informed users

You still can't easily set a breakpoint inside the method

You can set a breakpoint into the method, once you know which in the memory address of the assembly you are looking for (and there is many) image image

Another question: Is it possible to name those assemblies, to have a somewhat human-readable name?

Muchaszewski avatar Jan 28 '21 09:01 Muchaszewski