views:

296

answers:

4

I have a large inherited C/C++ project. Are there any good tools or techniques to produce a report on the "sizeof" of all the datatypes, and a breakdown of the stack footprints of each function in such a project.

A: 

I'm not aware of any tools, but if you're working under MSVC you can use DIA SDK to extract size information from .PDB files. Sadly, this wont work for stack footprints IIRC.

yrp
A: 

I'm not sure if the concept of the stack footprint actually exists with modern compilers. That is to say, I think that determining the amount of stack space used depends on the branches taken, which in turn depends on input parameters, and in general requires solving the halting problem.

MSalters
it still ought to be possible to get the max footprint of each function as a discrete entity, not the footprint of each possible code path followable from that function.
idij
+1  A: 

I'm curious to know why you want to do this, but that's merely a curiosity.

Determining the sizeof for every class used should be simple, unless they've been templated, in which case you'd have to check every instantiation, also.

Likewise, determining the per call sizeof on a function is simple: it's a sizeof on each passed parameter plus some function overhead.

To determine the full memory usage of the whole program, if it's not all statically defined, couldn't be done without a runtime profiler.

Writing a shell scrip that would collect all the class names into a file would be pretty simple. That file could be constructed as a .cpp file that was a series of calls to sizeof on each class. If the file also #included each header file, it could be compiled and run to get an output of the memory footprint of just the classes.

Likewise, culling all of the function definitions to see when they're not using reference or pointer arguments (ie copying the entire class instance onto the stack) should be pretty straight-forward.

All this goes to say that I know of no existing tool, but writing one shouldn't be difficult.

warren
Hi warren, the reason to ask is that I port a program (gnugo) to a platform, Symbian/S60, which has a limited stack size (and indeed limited heap size, but the stack is particularly critical). I have it mostly working now by putting some stuff fon the heap, but it still runs into stack overflows.
idij
Yes, writing a script to do it is of course the fallback, but I was hoping to avoid that. In particular, the "some fucntion overhead" part is actually the most critical part because of the recursion in the program. I was hoping there was a way to avoid writing most of a C language parser :)
idij
A: 

I am looking for the same information about stack footprint for functions, and I dont believe what warren said is true. Yes, part of what impacts the stack in a function is the parameters, but I've also found that every local variable in a function, regardless of the scoping of said variable, is used to determine the amount of stack space to reserve for the function.

In the particular poor code example I am working with, there are >200 local class instances, each guarded by if (blah-blah) clauses, but the stack space reserved is modified by these guarded local variables.

I know what I need is to be able to read the function prologue for each method to determine the amount of space being reserved for the function, now how would I do that....?

SteveV
I didn't get any good answer, but the closes I got which was ultimately good enough for my purposes was to just wrote a function to measure the stack depth. Then you need to call it from every function, but that was good enough for me as I had some places I could hook it in. See the next comment for the code...
idij
// // not sure if this is 100% accurate, but it's close enough // inline void stack_trace () { size_t stack = reinterpret_cast<size_t>( VirtualQuery (reinterpret_cast<void*>(stack), VirtualQuery (mbi_current.BaseAddress, size_t stack_size = reinterpret_cast<size_t>(mbi_current.BaseAddress) + mbi_base.RegionSize - stack; if (stack_size > max_stack) max_stack = stack_size; //printf ("stack_size = %d\n", stack_size); }
idij
idij