Adding Runtime Intelligence Application Analytics for a library and not an application - visual-studio-2010

I want to add usage statistics for a .NET 4.0 library I write on CodePlex.
I try to follow the step described here but my problem lies with the fact that what I write is a library and not an application.
One of the steps is put the Setup and Teardown attributes. I thought about adding the Setup attribute on a static constructor or a different place that will run once per usage of the library. My problem lies with the Teardown attribute that should be placed on code that ends the usage. I don't know where to put this attribute.
Is it possible to get usage statistics on a library?
Maybe I can register on an event that will fire when the application unloads the dll?

This looks like a typical honeypot giveaway, designed to commit you to the retail edition of their obfuscator. It's a tough business, few play this game better than Preemptive. Yes, using attributes is not going work for a library. The only possible candidate would be a finalizer. And you do not want your code to contact some website while the finalizer thread is running.
Take a look at the retail edition of their product. I bet it has a way to invoke the methods that are normally injected by their obfuscator directly. The class constructor is an obvious candidate for "Setup". An event handler for the AppDomain.ProcessExit event could be a possible location for the "Teardown" call. This also might avoid having to run the obfuscator at all, not undesirable in an open source project.

Related

Is there a tool for Visual Studio which can show the list of all called functions?

When debug application, it may be usefull to see in what sequence methods are called. It is possible to write Debug.WriteLine("Called MethodName") in earch method but there are big number of methods in my project.
Is there tool that logging method calls?
Runtime Flow (developed by me) shows all function calls in a .NET application.
As mentioned in a comment, the Visual Studio Call Stack window already implements this.
However, if you really wanted more information you can always look in to adding Tracing information.
You could use an AOP (aspect oriented programming) framework to make your life a bit easier.
There is a worked example of how to use PostSharp to log method calls here: http://www.codeproject.com/Articles/337564/Aspect-Oriented-Programming-Using-Csharp-and-PostS

Quickly testing a function that is a part of a big DLL project

I use VS2010 for C++ development, and I often end up doing work in some dll project and after everything compiles nicely I would like to try to run dummy data on some classes, but ofc the fact that it is a dll and not an exe with main makes that a no go. So is there a simple way to do what I want, or Im cursed till eternity to c/p parts of a big project into small testing one?
Ofc changing the type of the project also works, but I would like to have some almost like iteractive shell way of testing functions.
I know this isn't a library or anything, but if you want to run the dll on windows simply without framing it into anything, or writing a script, you can use rundll32.exe within windows. It allows you to run any of the exported functions in the dll. The syntax should be similiar to:
rundll32.exe PathAndNameofDll,exportedFunctionName [ArgsToTheExportedFunction]
http://best-windows.vlaurie.com/rundll32.html -- is a good simple still relevant tutorial on how to use this binary. Its got some cool tricks in there that may surprise you.
If you are wondering about a 64-bit version, it has the same name (seriously microsoft?) check it out here:
rundll32.exe equivalent for 64-bit DLLs
Furthermore, if you wanted to go low level, you could in theory utilize OllyDbg which comes with a DLL loader for running DLL's you want to debug (in assembly), which you can do the same type of stuff in (call exported functions and pass args) but the debugger is more for reverse engineering than code debugging.
I think you have basically two options.
First, is to use some sort of unit tests on the function. For C++ you can find a variety of implementations, for one take a look at CppUnit
The second option is to open the DLL, get the function via the Win32API and call it that way (this would still qualify as unit testing on some level). You could generalize this approach somewhat by creating an executable that does the above parametrized with the required information (e.g. dll path, function name) to achieve the "interactive shell" you mentioned -- if you decide to take this path, you can check out this CodeProject article on loading DLLs from C++
Besides using unit tests as provided by CppUnit, you can still write your own
small testing framework. That way you can setup your Dll projects as needed,
load it, link it, whatever you want and prove it with some simple data as
you like.
This is valueable if you have many Dlls that depend on each other to do a certain job.
(legacy Dlls projects in C++ tend to be hardly testable in my experience).
Having done some frame application, you can also inspect the possibilities that
CppUnit will give you and combine it with your test frame.
That way you will end up with a good set of automated test, which still are
valueable unit tests. It is somewhat hard starting to make unit tests if
a project already has a certain size. Having your own framework will let you
write tests whenever you make some change to a dll. Just insert it into your
framework, test what you expect it to do and enhance your frame more and more.
The basic idea is to separate the test, the testrunner, the testdata and the asserts
to be made.
I’m using python + ctypes to build quick testing routines for my DLL applications.
If you are using the extended attribute syntax, will be easy for you.
Google for Python + ctypes + test unit and you will find several examples.
I would recommend Window Powershell commandlets.
If you look at the article here - http://msdn.microsoft.com/en-us/magazine/cc163430.aspx you can see how easy it is to set up. Of course this article is mostly about testing C# code, but you can see how they talk about also being able to load any COM enabled DLL in the same way.
Here you can see how to load a COM assembly - http://blogs.technet.com/b/heyscriptingguy/archive/2009/01/26/how-do-i-use-windows-powershell-to-work-with-junk-e-mail-in-office-outlook.aspx
EDIT: I know a very successful storage virtualization software company that uses Powershell extensively to test both it's managaged and unmanaged (drivers) code.

How to Search Multiple Projects to Find Where a Specific Class's Method is being Invoked

We currently have multiple .NET projects that we have developed using Visual Studio. Each project has a reference to a shared dll that contains some helper classes.
Let's say I have an instance class named 'Helper1' with a public method named 'Save()'. Does anyone know of a way for me to search a group of projects to determine which projects/line the Save() method belonging to the Helper1 class is being invoked?
Basically, I want a list of where the Helper1 class' Save() method is being used among multiple projects.
This is not as simple as doing a text search. There could be many other classes in these projects that also have a Save() method which is being invoked, but I don't care about them. I only want to know about the Save() method belonging to the Helper1 class.
This means, the tool performing the search needs to be smart enough to understand the current namespace it is searching in. When a Save() method is found, the search tool needs to determine if the Save() method belongs to the Helper1 class or to some other class.
Note: There is no dynamic dependency injection happening in these projects, so we know at compile time which classes are being used.
Load all the dlls into "reflector" (the free version is fine)
navigate to the method you are interested in
bring up the analyser (ctrl+r I believe)
job done
Red Gate's .NET Reflector can do this, even in the free version.
The "Analyze" feature will tell you where specific types are used, exposed, and instantiated:
If you find yourself needing something a little bit more high powered, you might look into Tom Carter's Dependency Structure Matrix Plugin. This works with Reflector to give you a more powerful way of tracking inter-module dependencies. You can read an article about it here.

Is there a tool that will track/log managed code?

I'm working on a bug where we have a use case that works, and a subtly different use case that doesn't. The code base has next to no logging, and I don't want to spend time now sprinkling logging throught the code base, though I do have time budgeted to do that at a later date.
Is there a tool that logs a program's actions ie, logs each function call?
Apparently Appsight does do this but costs 100,000's.
You could try using our logging tool SmartInspect together with the aspect oriented programming (AOP) framework PostSharp. By using our aspect library, you can automatically instrument and log all method calls, exceptions and even field/property changes.
A solution that doesn't require any code change / recompile would be to use a tool that's based on the .NET Profiling API, which injects its hooks at runtime.
The only free-open-source project i know that traces not just method entry/exit but also the return values/method parameters is SAE (Simple Assembly Explorer). When you open it up, just navigate to your exe, right click on it, and choose Profile.
There are some limits to this tool - specifically, it won't print out the values of generic types, and the text output is not indented/prettified.
Runtime Flow (developed by me) can log each function call with parameters for .NET 2.0 - 4.0 applications without any additional instrumentation.

Is it possible to get Code Coverage Analysis on an Interop Assembly?

I've asked this question over on the MSDN forums also and haven't found a resolution:
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3686852&SiteID=1
The basic problem here as I see it is that an interop assembly doesn't actually contain any IL that can be instrumented (except for maybe a few delegates). So, although I can put together a test project that exercises the interop layer, I can't get a sense for how many of those methods and properties I'm actually calling.
Plan B is to go and write a code generator that creates a library of RCWWs (Runtime Callable Wrapper Wrappers), and instrument that for the purposes of code coverage.
Edit: #Franci Penov,
Yes that's exactly what I want to do. The COM components delivered to us constitute a library of some dozen DLLs containing approx. 3000 types. We consume that library in our application and are charged with testing that Interop layer, since the group delivering the libraries to us does minimal testing. Code coverage would allow us to ensure that all interfaces and coclasses are exercised. That's all I'm attempting to do. We have separate test projects that exercise our own managed code.
Yes, ideally the COM server team should be testing and analyzing their own code, but we don't live in an ideal world and I have to deliver a quality product based on their work. If can produce a test report indicating that I've tested 80% of their code interfaces and 50% of those don't work as advertised, I can get fixes done where fixes need to be done, and not workaround problems.
The mock layer you mentioned would be useful, but wouldn't ultimately be achieving the goal of testing the Interop layer itself, and I certainly would not want to be maintaining it by hand -- we are at the mercy of the COM guys in terms of changes to the interfaces.
Like I mentioned above -- the next step is to generate wrappers for the wrappers and instrument those for testing purposes.
To answer your question - it's not possible to instrument interop assemblies for code coverage. They contain only metadata, and no executable code as you mention yourself.
Besides, I don't see much point in trying to code coverage the interop assembly. You should be measuring the code coverage of code you write.
From the MDN forums thread you mention, it seems to me you actually want to measure how your code uses the COM component. Unless your code's goal is to enumerate and explicitly call all methods and properties of the COM object, you don't need to measure code coverage. You need unit/scenario testing to ensure that your code is calling the right methods/properties at the right time.
Imho, the right way to do this would be to write a mock layer for the COM object and test that you are calling all the methods/properties as expected.
Plan C:
use something like Mono.Cecil to weave simple execution counters into the interop assembly. For example, check out this one section in the Faq: "I would like to add some tracing functionality to an assembly I can’t debug, is it possible using Cecil?"

Resources