LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Determine a function's stack size. (http://www.linuxquestions.org/questions/programming-9/determine-a-functions-stack-size-559692/)

 koodoo 06-06-2007 02:54 PM

Determine a function's stack size.

Hi,

Once a function is called it's parameters, return address etc, are pushed onto the stack, Can we determine programatically how much stack space a function uses? i.e. by wiring some code in the called function, or the code that calls the function, and not using some profiling tool.

Thanks,
regards,
koodoo.

 ta0kira 06-06-2007 06:14 PM

Maybe use asm to compare the stack pointer at the beginning to where it is at an arbitrary point in the function? It also depends if the function calls itself recursively. I don't really know asm, otherwise I'd give you an example.
ta0kira

 koodoo 06-08-2007 08:56 AM

I myself don't know asm :confused:
However, I searched some forums and then wrote the following code. It compiles/executes without error, but I don't know whether it gives me the correct value or not.
Suppose there's a function "testfunc", and we wish to find how much stackspace this fuction uses. I've written the following code for this.

Code:

```#include <stdio.h> #include <stddef.h> ptrdiff_t testfunc (int arg1, int arg2, char *stackbase); int main() { char stackbase; printf("\nThe amount of stack space used by \"testfunc\" is : %ul bytes\n",testfunc(10, 5, &stackbase)); return 0; } ptrdiff_t testfunc (int arg1, int arg2, char *stackbase) { //. //. //all function processing goes here //. //. //. //. char temp; return stackbase - &temp; }```
First thing is that, will this code give me the desired result?
Secondly, what is the best way to print a ptrdiff_t entity. It is a typedef I think defined in stddef.h as
typedef long ptrdiff_t;
so currently I'm just printing it as an unsigned long integer.

regards,
koodoo

 theNbomr 06-08-2007 09:30 AM

I don't think there is any way to know the stack size. The stack is allocated on a process-wide basis, and is shared by all functions in a process. On x86 architecture, the stack pointer register is decremented as values are pushed on the stack, so the value of the stack pointer at any given instant may reflect the space remaining on the stack. There would be no way to know how much stuff is already on the stack. In Linux, using ld, the amount of space allocated for stack is a linker-defined value, and can be controlled by the --stack argument. Apparently the default size is 2MB. Running a program under a debugger might provide access to the stack pointer value.

man ld

--- rod.

 andrewjj 06-08-2007 12:43 PM

RE: Determine a function's stack size

This will depend on the architecture that you are using. On i386 the parameters are pushed right to left onto the stack then a pointer to the next instruction is pushed onto the stack. On IA64 the parameters are usually placed into registers before stack space is used. You can access the stack register in gdb by asking it to "print \$esp". Probably the easiest way to get an idea of the stack size is with the following code. keep in mind this will only work on when stack space is used to store these values.

Code:

``` int test_fun(int arg1, int arg2, int arg3) { int stack_size = (char *)(&arg3 + 1) - &arg1; }```
it may be difficult to trick gcc to give it to you straight out as it seems to move the stack pointer at the beginning of the function to make space for local variables and variables passed to other functions. It even doesn't do the above code too well with different parameter types. If you are up to it, you can get a look at what assembly is being used by gcc by using the -S option.

andrewjj

 koodoo 06-08-2007 01:53 PM

thank you for all the replies. Any comments on what the code I suggested would do?
I think it should provide a decent approximation of the stack space used by the function "testfunc" but I'm not sure.

 ta0kira 06-08-2007 10:43 PM

Quote:
 Originally Posted by koodoo I myself don't know asm :confused: However, I searched some forums and then wrote the following code. It compiles/executes without error, but I don't know whether it gives me the correct value or not. Suppose there's a function "testfunc", and we wish to find how much stackspace this fuction uses. I've written the following code for this.
This will give you some idea, but that will only tell you the stack size right before stack unwind on return. Any embedded blocks (such as if/for, etc.) will NOT count toward that figure since they will have unwound by the time that calculation is made. Also, that calculation itself uses stack space.
ta0kira

 koodoo 06-09-2007 02:29 AM

Wow, I didn't know that much detail. I didn't know that embedded blocks (such as if/for, etc.) are unwounded earlier. thanks

Moreover here, I'll have to actually call the function inorder to get an idea of stack space usage.
Somebody suggested that, compile the code to the assembly stage (using the gcc -S option), and use the disassembled output, to get an idea of stack space usage. I could obtain the disassembled output but understanding it completely was out of my capacity at this point of time. I'll have to do some study for that :study:

Here's the disassembled output :
Code:

```        .file  "timefunc.c"         .text .globl testfunc         .type  testfunc, @function testfunc:         pushl  %ebp         movl    %esp, %ebp         subl    \$4, %esp         leal    -1(%ebp), %edx         movl    16(%ebp), %eax         subl    %edx, %eax         leave         ret         .size  testfunc, .-testfunc         .section        .note.GNU-stack,"",@progbits         .ident  "GCC: (GNU) 3.4.6"```
regards,
koodoo

 andrewjj 06-09-2007 12:33 PM

If you are intested in learning more about assembly check out http://asm.sourceforge.net/ , they seem to have the most unix related information on the language that I have seen on the net. The best instruction reference that i have found is on the AMD developer website http://developer.amd.com/. Like some languages you probably won't use assembly very often, but knowing it will help you write code better.

andrewjj

Quote:
 Originally Posted by koodoo Wow, I didn't know that much detail. I didn't know that embedded blocks (such as if/for, etc.) are unwounded earlier. thanks Moreover here, I'll have to actually call the function inorder to get an idea of stack space usage. Somebody suggested that, compile the code to the assembly stage (using the gcc -S option), and use the disassembled output, to get an idea of stack space usage. I could obtain the disassembled output but understanding it completely was out of my capacity at this point of time. I'll have to do some study for that :study: Here's the disassembled output : Code: ```        .file  "timefunc.c"         .text .globl testfunc         .type  testfunc, @function testfunc:         pushl  %ebp         movl    %esp, %ebp         subl    \$4, %esp         leal    -1(%ebp), %edx         movl    16(%ebp), %eax         subl    %edx, %eax         leave         ret         .size  testfunc, .-testfunc         .section        .note.GNU-stack,"",@progbits         .ident  "GCC: (GNU) 3.4.6"``` regards, koodoo

 koodoo 06-11-2007 11:40 AM

Thanks for the pointer. Actually I've been delaying learning assembly for a long time now. But I guess, I'll have to do it sometime or the other. The sooner I start the better.!

 All times are GMT -5. The time now is 08:39 PM.