how to collect information regarding function calls in run time?
linux.bhar at gmail.com
Wed Apr 3 16:15:06 EDT 2019
On Wed, Apr 03, 2019 at 04:25:02PM -0300, Pedro Terra Delboni wrote:
> I have a question about profiling, please, let me know if this is not
> the right mailing list to post these kind of questions.
> Is there a way for me to record how many times each specific direct
> call to a function happened?
> I've seen ways of profiling the kernel that calculate how much time
> the system spent in each function, but having a bit more information
> related to calls would be really good.
> I would like to know, for every function call that happens in run
> time, who called it (it's for a project in my University).
> I've also seen (I may be mistaken here) that by compiling the kernel
> with perf, each function will start with a stub call which can be used
> for profiling purposes.
> I was thinking in using this stub to plug a function to dump
> (somewhere) the return address before it's own (so I can collect the
> info about where the call came from).
> I wonder if changing every stub calls in all functions to dump its
> return address wouldn't create too much of a latency impact to the
> point of skewing the control flow of the execution,
> or even making it nonviable.
> Thanks in advance, any help would be great!
> If this is not the right place to post this question, I would
> appreciate if anyone could point me to the right place.
I am assuming your talking about profiling functions in the linux kernel.
You can the kernel tracing infrastructure called kprobes to record how
many times a function is called by a particular process or for a time
interval. Kprobes will dynamically put a 'tracepoint'(if you use
debuggers it is similar to 'breakpoints') at the function call.
You can then trace that particular function to see how many times its called and stuff like that.
Luckily perf comes with kprobes! yay!
perf probe --add <function_name>
This will add a probe to the function. You can then trace it by:
perf record -e <function_name> <options>
The perf.data file will give you all the info you need.
Also for better output from perf, enable CONFIG_FRAME_POINTER in your
kernel build(assuming it is a custom build).
Check this out for more info:
There is similar functionality in user space. It is called uprobes(user
probes) similar to kprobes(kernel probes).
Now before you click on the above link, use kprobes only for functions
which do not have a static tracepoint. Certain interesting functions like system
calls, scheduler specific functions, mm functions like kmalloc etc. You
can find the static tracepoints in /sys/kernel/debug/tracing/events. You
do not need kprobes. For example if your tracing the write system call,
you can just do:
perf record -e syscalls:sys_enter_write -ag 1 (For system wide
Again check out the above link for more info!
Hope I was helpful!
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
More information about the Kernelnewbies