This question is somewhat related to this one I asked earlier this week. I'm still dealing with C++'s inability to properly reflect over its own code. Now, while I've come across several libraries that somehow try to add this functionality, I'd like to ask if what I need to do can't be achieved using more readily available and widely used (ie simpler to implement) methods.
What I need is basically a snapshot of object instances at a certain point in time. They can have sub-objects (ie some structure where an object "has" one or more other objects), but that's basically it. Now, while reflection seemed like an obvious choice, this has been pretty much ruled out due to the projects always being coded in C++. However, a debugger seems to do just what I need (stop the program at a certain point and tell me about the instances currently in memory), so I feel like I should be able to achieve some kind of information extraction using simple debugging techniques as the code of the given projects can be assumed to be available. The workflow within my program could look like
- compile c++ code in debug mode
- create and attach debugger
- set breakpoint (as I know the method call which indicates that all needed instances have been created)
- run and check for instances (and sub-instances) of a certain class (and its inherited types), extract this information for further processing
While this is basically wh开发者_开发知识库at I usually do while debugging, I have been unable to find any information about how to do this automatically from within another program. This would have the advantage of achieving kind of a limited reflection without any further libraries or code changes. So, is there any way - preferrably in Java, C# or C++, as I'm most familiar with these - to just load and compile a VS solution or execute a makefile and perform the steps given above? I keep finding a lot of pages that tell me how to use a debugger, but that's really not useful here...
Thanks for any help.
If Visual Studio is not a must, you could have a look at GDB. This debugger can be controlled programmatically, it is actually used by some debugger GUIs, so you should be able to perform the steps 2-4 with it. For the first step, the obvious choice in this context is gcc.
Well since it seems from your other question that your plattform is Windows you could do this in principle with windbg and related (command line only) debuggers available in the Debugging Tools for Windows or Windows SDK package.
With it you can easily perform tasks 2 to 4 in an automated way - although 4 (checking automatically for instances) would I think not be possible to do automatically for native (C++) code since, as you already mentioned, C++ lacks something like the reflection mechanismus in .net code.
Now I'm not sure if all this effort will be really worth it. If all you are trying to accomplish is to debug/tune your application you can use conditional compiling for debug/release code and add extra code for tracing purposes.
Couldn't you simply convert the code to a .NET application and use .NET reflection?
If what you are trying to do is to see what has been allocated at a certain point in the execution of your program, a code profiler might help. A code profiler can show you snapshots of what your code is doing at particular points in time. On Linux, valgrind is a popular choice. Visual Studio is supposed to have a profiler, although I've never used it.
精彩评论