Ask Your Question

How does Linux-ftrace utilize the mcount function (with the gcc `-pg` option) to accomplish code profiling?

asked 2021-12-30 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-09-14 01:00:00 +0000

plato gravatar image

When the -pg option is passed to gcc, it generates code that includes a call to the mcount function at the entry point of every function. The mcount function records the current program counter (PC) address and the address of the function entry point.

In Linux, ftrace utilizes the mcount records to accomplish code profiling. It hooks onto the mcount records and records the entry and exit time of each function call. With this information, ftrace can build a timeline of function calls, calculate the time spent in each function, and create a stack trace of function calls.

The ftrace infrastructure in Linux provides a set of tracepoints that are used to instrument the kernel code using mcount. The function_graph tracer is an example of a tracer that utilizes mcount to profile the kernel code. When enabled, this tracer records the entry and exit times of each function and generates a graph that shows the call flow and execution time of each function.

In summary, the mcount function, along with the -pg option in gcc, is used to instrument the code and record function entry and exit times. The ftrace infrastructure in Linux hooks onto these records to perform code profiling, which includes analyzing the execution time of each function call, creating a function call stack trace, and generating a graph of function call flows.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2021-12-30 11:00:00 +0000

Seen: 1 times

Last updated: Sep 14 '22