ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
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.
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.
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
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
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
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.!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.