Instrument C/C++ with -finstrument-functions for execution tracing and Perfetto visualization
The Function Call Tracing skill is designed to trace function calls within C/C++ programs, providing valuable insights into the execution flow of the code. It addresses the challenge of tracking function calls and visualizing their performance in a structured manner, which is particularly useful for debugging, profiling, and performance analysis. By leveraging function instrumentation and per-thread logging, the skill ensures that developers can track each function’s entry and exit, providing a detailed execution trace for deeper analysis and optimization. With integration into the Perfetto visualization tool, users can access high-fidelity performance data, making it easier to identify bottlenecks and optimize their code for better performance and resource utilization.
Main features of the skill include instrumentation through the `-finstrument-functions` GCC flag, capturing function calls with per-thread logs, and the ability to convert logs into a format suitable for viewing in the Perfetto Chrome UI. The logs capture the timestamp, call depth, and function name, allowing users to gain a clear understanding of how their program functions. Additionally, the solution offers an easy setup through a Makefile or CMake configuration to enable tracing conditionally. The final output consists of per-thread log files and a unified Perfetto JSON file, allowing seamless visualization and analysis of the collected data.
This skill is ideal for developers working with C/C++ codebases who need to monitor and analyze function execution in detail. It's particularly useful for performance engineers, software debuggers, and anyone aiming to improve the performance of their applications by understanding the execution flow at a granular level. By integrating this skill, users can gain deeper insights into their programs' behavior, enabling them to make data-driven decisions for optimization and troubleshooting.
To enable function tracing, copy `trace_instrument.c` and `trace_to_perfetto.cpp` to your project, add `-finstrument-functions` to the CFLAGS, and link with the instrumentation library by adding `-L. -ltrace -ldl -lpthread` to LDFLAGS. Then build and run your program as usual.
The trace data is output as per-thread log files in the format `trace_<tid>.log`, which contain the function entry and exit events. These logs are then converted into a Perfetto JSON file (`trace.json`) for visualization in the Perfetto UI.
Yes, you can use this tool with CMake. You can enable tracing by setting the `ENABLE_TRACE` option to `ON` in your CMake configuration. This will add the necessary compiler options and link libraries to enable tracing functionality.
This skill requires a C/C++ development environment with support for GCC or Clang for compilation. The system must also support Perfetto for visualization, and you need access to `libdl` and `libpthread` for linking the instrumentation library.
Yes, there will be some performance overhead due to the logging and function instrumentation. However, the impact is typically minimal during the trace collection phase and is meant for profiling purposes rather than regular production use.
Quick Setup:
.claude/skills/Repository
gadievron/raptor