How to debug InvalidProgramException on emited code for DynamicMethod - c#

After searching the stackoverflow plus googling alot, the solutions offered for debugging code that gets emitted for DynamicMethods seems outdated and very unwieldy.
Surely in the intervening 4 years or more since LCG (light-weight code generation) was released, someone must have found a better way.
What do you find is the easiest way to verify the dynamic IL that you write and debug it?
Do you use peverify or ILDasm or something else? Those 2 tools require writing the assembly to disk but DynamicMethod doesn't offer any direct way to do that.
Apparently WinDbg aso offers a way to see the IL but that's very awkward to deal with that.
Something like a plugin to VisualStudio 2010 will be ideal.
Any ideas?

You can use ILGenerator.MarkSequencePoint to allow debugging step by step your emitted code.

Related

Decompiled с# code shows errors

I have decompiled C# dll and now can not compile it. It shows strange errors, for example in some switch blocks some case blocks havent their break statements, or errors like "Can not cast int to bool" are shown. But the amount of errors is not very large for dll of that size, so I think it is not the problem of decompiler.
Is there some derective for compiler (for example, smthing life unsafe) that will solve this problem? Or why is there such strange errors?
P.S. The dll is not broken - the application is using it right now. I'm using dotPeek to decompile and Visual Studio 15 to compile the result code.
For such an error like :
"Can not cast int to bool"
There is absolutely no compiler directive that will allow your compiler to go ahead, I think that your decompiler messed up something decompiling ... You could simply try with another decompiler and see if you get another results, valid alternatives are : ILSpy, JustDecompile and Dotnet IL Editor .
Be aware that some commercial DLL is obfuscated just to try to make life difficult to the decompiler and to who decompile ...
Be careful to avoid breaking some copyright .
It's quite hard to decompile IL to C# and decompiler has probably done something wrong. In my experience, decompiled and compiled code can also behave differently than original assembly, so be aware. And such compile error can be indocation that dotPeek was not sure, so think about it what it should do or look in IL.
If you are willing to make really small edits to the assembly (inject method calls, make something public) it may be safer to do it directly in IL obtained by ildasm.

Running CPython functions from C#

I'm working on a project where I need to be able to run a python function that depends on SciPy/NumPy. Due to this being an add-on to a project already in progress, using IronPython would not be an option.
Additional info:
Python.NET seemed to be a good fit, but I was unable to get the return value from RunString() (it would only return NULL).
Passing arguments and catching the return value (a tuple) is necessary.
The function is in a statistical package that was created by a support group for the team, so modification of that would also not be possible.
I'm at quite a loss for what to do. Any hints in the right direction are appreciated. Thanks for any help you can give!
I understand that this may be quite vague, but I cannot give explicit details to the project. If any clarification is needed please let me know and I'll do my best!
I guess you could write a DLL that uses the CPython API to expose the function, then call it in C#?
It's possible to embed the Python interpreter; although I've never done this personally, I guess it would be useful: http://docs.python.org/extending/embedding.html
Does it need to be portable beyond Windows? If not, perhaps you can embed the CPython interpreter with C++/CLI, wrap that in a nice .Net-ish interface and use the resulting code from C#. Never tried that, so I don't know if it's going to work.
Regardless if you go through this route or the 'write a native DLL' route, it will probably be easier to to embed python using Boost.Python, though I'm not sure if your wrapper code enough is going to be large enough to make all of this (compiling the Boost behemoth, learning Boost.Python, making sure it works with C++/CLR, increasing your target file size) worth it.
IronPython using DLR might be the way to go. Mind you it won't be the fastest way, but it seems like something worth pursuing if you're going to do this a lot. Another useful link
The ironclad project was started to allow using CPython extensions from IronPython, especially SciPy/NumPy it seems. I don't know how usable it is (and how actively it is still being developed)

How to become an MSIL pro?

I have spent hours on a debugging problem only to have a more experienced guy look at the IL (something like 00400089 mov dword ptr [ebp-8],edx ) and point out the problem. Honestly, this looks like Hebrew to me - I have no idea what the heck it's saying.
Where can I learn more about this stuff and impress everyone around me? My goal is to read stuff like the following and make a comment like: Yeh, you are having a race condition.
.maxstack 2
.entrypoint
.locals init (valuetype [MathLib]HangamaHouse.MathClass mclass)
ldloca mclass
ldc.i4 5
That is not MSIL, it is assembly langauge 80x86.
To get great at IL, start with this fantastic article: Introduction to IL Assembly Language. Although it says "introduction", it's everything you need to start getting comfortable.
The other part of what you need is practice and lots of it. Use .NET Reflector and start looking at code disassembled into IL. (Tip: when you go to download it, you don't have to provide a real email.) Also, play with the Reflexil plugin in Reflector. Here's a good starting point for that: Assembly Manipulation and C# / VB.NET Code Injection.
Not necessary but a bonus: Reflexil is open source. You can get the source here.
I can give you an answer that runs both ways.
On the one hand, there's nothing like good assembly language skills to teach you how a computer really operates. MSIL is, to some extent, an assembly-like language. On the down side, there are very few opportunities to do this kind of development any more.
On the other hand, resorting to looking at the MSIL to fix a problem is not necessarily the most direct or educational way to understand a problem. In five years of .NET programming, I've never felt the need to go there. Only once has a co-worker (who had worked at Microsoft on compiler testing) gone there with a problem that I was trying to solve, and in the end, his answer was misleading, because the real issue was based in CLR design and constraints. Better knowledge of the CLR and C# would have led to a better understanding and a real solution.
(In case you're wondering, the problem was that I wanted to use "as" for safe casting with a generic. "as" didn't work, but "is" did. My co-worker noted that "is" and "as" use the same MSIL. The real problem is that "as" only works for casting classes, and without the proper constraint on the generic declaration, C# doesn't know whether your generic type will be a class. In fact, the types I was using with generics were value types; "as" couldn't work for those at all.)
Rather than going for MSIL skills, I highly recommend Jeffrey Richter's book CLR via C#. Even after years of digging hard at into C#, this book is still full of revelations - I learn something from every page.
Can’t say I’m an IL “pro”, but I managed to teach myself pretty much all of IL by doing the following:
Write a very short (two or three line) C# program that you are curious about how to write in IL.
Compile the program.
Open the compiled EXE in .NET Reflector.
Look at the IL code for the method in Reflector.
Hover your mouse over an IL opcode (e.g. “ldloc”). There is a tooltip describing each IL instruction.

If I turn off ReSharper 5's IntelliSense, what am I missing?

During my trial of ReSharper 5, I noticed its version of IntelliSense falls behind Visual Studio 2010's in three ways that were key to me:
ReSharper doesn't support IntelliSense in the "QuickWatch..." debugger utility.
ReSharper's IntelliSense seems to break down for me in .aspx files between the <%= %> tags.
I couldn't find a way to get a listing of properties within an object initializer block. (VS does this if you hit the space key.)
Thankfully ReSharper lets you use Visual Studio IntelliSense alongside its other great features. Am I missing out on anything great by not using ReSharper's IntelliSense?
Well you're losing quite a bit. Here are some quick facts about ReSharper code completion: http://www.jetbrains.com/resharper/webhelp/Coding_Assistance__Code_Completion.html
Smart Completion (Ctrl+Shift+Space in IntelliJ IDEA keymap) is especially useful because in common scenarios it gives you a narrow selection of symbols that you most likely want to complete, and in some cases acts as a shortcut to code generation features
By the way, Smart Completion is the kind of completion that you should use with object initializers: www.jetbrains.com/resharper/webhelp/Coding_Assistance__Code_Completion__Smart.html#object_initializers
As for completion within the <%= %> pair, this should work fine. Please let us know what exactly went wrong by submitting an issue to youtrack.jetbrains.net/issues/RSRP Thanks!
P.S. I work at JetBrains
A five minute comparison I'd say that ReSharper's gives you a bit of help in the typing.
So if you have a method that's A(int a, int b) and you hit ctrl+space when you select A it will add (), will place you in the middle of the brackets and will show you the information about the method. Other than that you're probably not missing much.
For me that is quite helpful, but if you're having trouble doing ASP.NET and those are known bugs / limitations its always best to pick the best tool for the job.
Personally I turn it off. It is annoying and slows me down. Here is a prime example:
No you're not missing much apart from a test runner that supports NUnit. I've had the same problem and also (rather worryingly), hideous performance. I tend to use the productivity power tools extension and the native refactor stuff instead. Info here:
http://visualstudiogallery.msdn.microsoft.com/en-us/d0d33361-18e2-46c0-8ff2-4adea1e34fef
R# also does stuff which I plainly do not want to do and my code ends up littered with commented resharper hints.
I find the CompleteCodeSmart functionality something I use frequently and save me alot of typing.

How to compare execution paths in .NET?

I'm debugging into the .NET Framework source code to look for a bug in my application. I have two similar inputs for the code where one exhibits the bug and the other doesn't. However to follow the code path into the .NET source is quite complex.
What I'd like is a tool that can be executed for both inputs and compare the results to see what code paths are taken, how the internal values differ, etc...
Is this available for .NET?
You could try nCover. It can show you code coverage information which might help you work out which branches are taken when the bug occurs.
You could try JetBrains DotTrace and just ignore the timings. They offer a free 30 day trial download.
As Mitch suggests, check out a profiler, perhaps ANTS. It will give you all the method calls so you can see everything that is going on.
The SD C# Test Coverage Tool will show you what code is executed if you run your test cases separately.
It will also compute the difference between the covered code for each case if you ask it to do so. This difference will be the code that one tests executes, that the other does not. Likely that's where your problem is.
It won't help you obtain source for the .NET framework.
Try using a profiler:
EqaTec (free)
ANTS Performance Profiler (14-day Trial)
dotTrace (10-day Trial)
If you have the Enterprise version of Visual Studio 2008 (or other version)
Visual Studio Profiler
Try the free EQATEC Tracer - it does pretty much exactly what you're looking for.
It injects "tracing-code" into your application which will at runtime tell you exactly what methods are executed and what the parameters are. There are sophisticated ways of fine-tuning what methods to trace, since "all" is usually too much, but you can turn on tracing for all methods with just one single click if you really want to.
Take a look at the key features here: http://www.eqatec.com/tools/tracer/features
-and get it here: http://www.eqatec.com/tools/tracer
Edit: Sorry, I missed the part about having to dig into the actual .NET framework code. The tracer can't do that in an easy manner.

Categories

Resources