Forgive me If this is a dumb question. Can one programmatically "observe" the contents of stack and heap while an application (say a console app) is running? Are there any APIs which would do this?
views:
49answers:
2
+1
A:
Well, you could try using the CLR Debugger API - although I'm not sure whether you can use that within the same process. However, I think it's likely that there's a better solution... what are you actually trying to do? What's the bigger picture here?
Jon Skeet
2010-09-25 08:15:41
@Jon, purely from the learning perspective. I thought It would be fascinating to see a counter(on my UI) increasing on the "heap" each time the application (a different one may be, as you mentioned) new up an object. Update :- I think I should look into the following passage from the link you mentioned above :- "A thread can be examined to inspect its call stack. A thread's call stack is decomposed at two levels: at the chain level and at the stack frame level. The call stack is first decomposed into chains. A chain is a contiguous logical call stack segment..."
ydobonmai
2010-09-25 08:34:06
@ydobonmai: I would hook that up as an event, within a single application. I wouldn't start trying to get into the debugger API unless you really need to.
Jon Skeet
2010-09-25 08:36:37
@Jon, "I would hook that up as an event, within a single application". Which "event" are you talking about? Do you mean you would increment a counter inside of a constructor of each object I new up or I get it completely wrong. Could you please provide an example?
ydobonmai
2010-09-25 08:40:20
@ydobonmai: If you wanted to track *every* object created, then yes, you'd need to call a method from every constructor. Of course, that wouldn't help you track construction of objects not under your control. This isn't something I would try to do without a great deal of time to spare - I wouldn't expect the debugger API to be particularly simple.
Jon Skeet
2010-09-25 09:02:08
A:
I do not know although some unmanaged code can easily track the stack while tracking heap requires considering GC since pointers can move after each GC.
Now, since the only application that can change variable is your console application (!) so why would you wanna do that? Surely you will be only tracking what you are doing yourself in the application. Is it for bug-fixing? In that case I recommend you do code review instead of black magic.
Aliostad
2010-09-25 08:17:01