Sunday, May 15, 2016

Using Reflexil with ILSpy to add Debug lines in a deployed DLL/EXE. (without recompiling/redeploying)



Using Reflexil with ILSpy to add Debug lines in a deployed DLL/EXE. (without recompiling/redeploying)






This can be done by directly editing the IL.
For example – In the beginning of method “func1” we want to record the parameters using Debug.WriteLine to see in DbgView or by adding some other listeners.
Do the following steps –
         1)      Unzip IlSpy and Reflexil  (both latest version, so that they are compatible) in the same folder.
         2)      Open ILSpy, open the dll/exe and click on the ‘gear’ icon on the top right to open reflexil.
         3)      Navigate to the required method, in this case “func1”.
         4)      Go to “Instructions” tab.
         5)      Right-click to add instructions. (Ignore the offset, focus on inserting the instruction at the right            place).
         6)      Most of the instruction to add Debug lines are done using ldstr (load string to stack), ldarg.0              (load the 0th (first) argument to stack), box (if it is value type/integer), call to Concat and                    WriteLine.
         7)      To load nth argument, use ldarg.s (as shown above in the above diagram).
         8)      nop is no instruction (probably ‘enter’).
         9)      If you are unclear of any instruction, just write the code and see it in Ilspy+Reflexil.
         10)   To load any property say “LastName” of any object say “Person”, use ‘callvirt’ (shown above).