I am doing a project in linux kernel and I wanted to know what does this checkstack.pl do? I have never studied perl so cant understand the program. It will be great if I co
It creates a listing of the size of the stack frame used by every function in the kernel (i.e. the total amount of local scratch space used by each function for local variables and whatnot).
The way it does this is by going through the disassembly of the kernel and looking for 2 things: function names and instructions which adjust the stack. It looks for function names by looking for lines that match $funcre
(qr/^$x* <(.*)>:$/
), and it looks for stack adjustment instructions that match $re
or $dre
; the latter two depend highly on what architecture the kernel was compiled for, which is what the first big block if if/else statements is checking for. $re
searches for functions which adjust the stack by a fixed amount (the vast majority of functions), and $dre
searches for functions which adjust the stack by a variable amount (rare).
objdump
is part of binutils; objdump -d
is the command to disassemble an object file. The usage of this script is to disassemble the kernel (objdump -d vmlinux
) and pipe the output into the script. The output of the script is a listing of all of the functions in the kernel, sorted by the largest stack frame size. I assume the purpose of the script is for the kernel maintainers to be able to avoid stack overflows by painfully making sure that the stack frames of everything is as small as possible, and this script allows them to verify this.
As already explained above that Perl script is used to find out the stack usage of kernel code, I think that Perl is used due to the fact that parsing the output of objdump -d
won't be so easy if done through C code.
You can find the stack usage at the runtime by taking the address of the first argument and the address of last local variable, then subtract them, something like:
int stack_usage_func(char i)
{
int j,k,l;
char buf[256];
char m;
unsigned long stack_use = &i - &m;
//do all processing
return stack_use
}
The return of the function should give you the runtime stack usage, I have not compiled the code, so please ignore if it gives compilation errors, but the logic should work.