1

I need to fill all available stack space with specific character for profiling purposes.

First of all, is there a safe way to do this on Android(some compiler specific functions maybe) in C++? If not, may I assume the address of first variable declared in a function is the "start" of "unused" stack? If not is there a way to get current stack pointer, something like ESP value on x86?

Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
  • Actually, after looking I thin that [How to determine Stack size of a Program in linux?](http://stackoverflow.com/questions/1678803/how-to-determine-stack-size-of-a-program-in-linux) has an answer, by [Steven Schlansker](http://stackoverflow.com/users/171061/steven-schlansker), to your exact question. – artless noise Dec 03 '13 at 15:16
  • You need to clarify what you mean by all available stack space? Per some specific functions, all functions? all program space? Multi-threaded? more generally http://meta.stackexchange.com/q/66377/195751 – auselen Dec 03 '13 at 15:45
  • @auselen When creating a new thread in the entry point function I want to fill all stack with special value to be able to detect later how much of the stack was used. – Mircea Ispas Dec 04 '13 at 10:18
  • Did you check stack-protector stuff? http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html – auselen Dec 04 '13 at 21:00
  • @auselen I need to know stack usage to be able to set stack size as low as possible to reduce memory usage. – Mircea Ispas Dec 05 '13 at 09:03
  • 1
    GCC has an option for that too :) `-fstack-usage` however I hope you can imagine it is hard to calculate total stack usage on a runtime. – auselen Dec 05 '13 at 09:24
  • @auselen I need just some statistics/estimations. The statistics have to be made on many platforms. Using compiler specific features is not an option. Thanks for help! – Mircea Ispas Dec 05 '13 at 10:07

1 Answers1

1

With ARM, you need to think about calling convention since a function with more than four arguments would have them spilled to stack. I find it much safer to use GCC's __builtin_frame_address.

Built-in Function: void * __builtin_frame_address (unsigned int level)
auselen
  • 27,577
  • 7
  • 73
  • 114