LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-06-2007, 03:54 PM   #1
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 32
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.
 
Old 06-06-2007, 07:14 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 06-08-2007, 09:56 AM   #3
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 32
I myself don't know asm
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

Last edited by koodoo; 06-08-2007 at 02:50 PM.
 
Old 06-08-2007, 10:30 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.

Last edited by theNbomr; 06-08-2007 at 10:31 AM.
 
Old 06-08-2007, 01:43 PM   #5
andrewjj
LQ Newbie
 
Registered: Jun 2007
Posts: 2

Rep: Reputation: 0
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
 
Old 06-08-2007, 02:53 PM   #6
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 32
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.
 
Old 06-08-2007, 11:43 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by koodoo
I myself don't know asm
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
 
Old 06-09-2007, 03:29 AM   #8
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 32
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

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
 
Old 06-09-2007, 01:33 PM   #9
andrewjj
LQ Newbie
 
Registered: Jun 2007
Posts: 2

Rep: Reputation: 0
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

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
 
Old 06-11-2007, 12:40 PM   #10
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 32
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.!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
size of call stack? vkmgeek Programming 11 11-06-2006 10:42 AM
how to disable 4K stack size wahaha Linux - Wireless Networking 1 08-26-2006 07:33 AM
16K stack size in 2.6..xx quietguy47 Linux - Kernel 3 04-24-2006 02:21 PM
Stack size decision atul_mehrotra Programming 2 10-08-2004 05:32 AM
Stack Size Decision atul_mehrotra Linux - General 0 10-07-2004 02:01 AM


All times are GMT -5. The time now is 08:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration