0

I am doing mixed language simulation with modelsim, part of the code is written in SystemC (C++), then I got stack overflow when I use the SystemC code. I am not sure how to trace this issue. Just want to check if it is possible to report the stack usage during compile the C++ code?

Enze Chi
  • 1,733
  • 17
  • 28

2 Answers2

1

the compiler can't tell exactly how much the max stack size will be since it depends of many things. if you have a recursion, the compiler can't foretell what will be the inputs, if you have threads there will be more then a single stack, and so on.

how to trace? for each code block, this is the stack frame: from the address of the first variable in the current function, note that the first variable may be a by value argument(not by reference), first or last, depending the calling convention, to the last declared variable in the current block, plus the sizeof the last variable. before the frame there is the return value(type size) and the return address (pointer size), so you can tell how many bytes each function takes on the stack, the current frame can be compared to the first main (or thread entry) variable address to alert you when you getting close to the limit. note that thread stack usually have different stack size than the main thread.

SHR
  • 7,940
  • 9
  • 38
  • 57
  • The stack used by a method has nothing to do with the size of a thread stack, or their number, except that it obviously has to be smaller. – user207421 Aug 10 '15 at 23:33
  • @EJP All I said is threads have stacks of their own, methods are took place in their thread stack, not in the main stack, I don't know what you understood. – SHR Aug 10 '15 at 23:39
  • After add `-Wall` flag, it reports a `frame size too large` on a function (1.4K+ lines). But it dose not seem have a lot of local variables. But it really have a lot of nested `if`, `case`. Will that affect the stack size? – Enze Chi Aug 11 '15 at 01:45
  • maybe there is some large array? or maybe some data type with large size? the code doesn't run on the stack, only local variables and function calls. – SHR Aug 11 '15 at 07:15
0

In non-recursive functions, yes: just add up the sizes of the parameters and local variables and add a couple or words for the return address and base pointer.

In recursive functions it isn't possible in general, although if the recursion is based on conditions that can be evaluated at compile time it might be feasible in limited cases.

I'm not aware of any compilers that do any of this.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • The suggested duplicate says gcc can report stack usage per function. – nobody Aug 10 '15 at 23:25
  • Simple reasoning based on number and size of parameters, stack pointers etc. could be incorrect, as nothing holds compiler implementers from implementing stack in a way you never expect. Manual counting will surely be incorrect in optimized builds. GCC has `-fstack-usage` flag. – Ivan Aksamentov - Drop Aug 10 '15 at 23:25
  • The g++ I am using is 4.7.4. It seams nothing happened after add `-fstack-usage` flag. – Enze Chi Aug 11 '15 at 01:38