$npx -y skills add gadievron/raptor --skill function-tracingInstrument C/C++ with -finstrument-functions for execution tracing and Perfetto visualisation
| 1 | # Function Call Tracing |
| 2 | |
| 3 | ## Purpose |
| 4 | Trace all function calls in C/C++ programs with per-thread logs and Perfetto visualization. |
| 5 | |
| 6 | ## Components |
| 7 | |
| 8 | ### 1. Instrumentation Library (trace_instrument.c) |
| 9 | Captures function entry/exit, writes per-thread logs. |
| 10 | |
| 11 | **Build:** |
| 12 | ```bash |
| 13 | gcc -c -fPIC trace_instrument.c -o trace_instrument.o |
| 14 | gcc -shared trace_instrument.o -o libtrace.so -ldl -lpthread |
| 15 | ``` |
| 16 | |
| 17 | ### 2. Perfetto Converter (trace_to_perfetto.cpp) |
| 18 | Converts logs to Chrome JSON for Perfetto UI. |
| 19 | |
| 20 | **Build:** |
| 21 | ```bash |
| 22 | g++ -O3 -std=c++17 trace_to_perfetto.cpp -o trace_to_perfetto |
| 23 | ``` |
| 24 | |
| 25 | ## Usage |
| 26 | |
| 27 | ### Step 1: Add to Build |
| 28 | ```makefile |
| 29 | CFLAGS += -finstrument-functions -g |
| 30 | LDFLAGS += -L. -ltrace -ldl -lpthread |
| 31 | ``` |
| 32 | |
| 33 | ### Step 2: Build Target |
| 34 | ```bash |
| 35 | make |
| 36 | ``` |
| 37 | |
| 38 | ### Step 3: Run |
| 39 | ```bash |
| 40 | export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH |
| 41 | ./program |
| 42 | # Creates trace_<tid>.log files |
| 43 | ``` |
| 44 | |
| 45 | ### Step 4: Convert to Perfetto |
| 46 | ```bash |
| 47 | ./trace_to_perfetto trace_*.log -o trace.json |
| 48 | # Open trace.json in ui.perfetto.dev |
| 49 | ``` |
| 50 | |
| 51 | ## Log Format |
| 52 | ``` |
| 53 | [seq] [timestamp] [dots] [ENTRY|EXIT!] function_name |
| 54 | [0] [1.000000000] [ENTRY] main |
| 55 | [1] [1.000050000] . [ENTRY] helper |
| 56 | [2] [1.000100000] . [EXIT!] helper |
| 57 | [3] [1.000150000] [EXIT!] main |
| 58 | ``` |
| 59 | |
| 60 | - Dots indicate call depth |
| 61 | - Timestamp in seconds.nanoseconds |
| 62 | - One log file per thread |
| 63 | |
| 64 | ## When User Requests Tracing |
| 65 | |
| 66 | ### Steps |
| 67 | 1. Copy `trace_instrument.c` and `trace_to_perfetto.cpp` to project |
| 68 | 2. Build instrumentation library |
| 69 | 3. Add `-finstrument-functions` to CFLAGS |
| 70 | 4. Add `-L. -ltrace -ldl -lpthread` to LDFLAGS |
| 71 | 5. Build project |
| 72 | 6. Set `LD_LIBRARY_PATH` and run |
| 73 | 7. Convert logs: `./trace_to_perfetto trace_*.log -o trace.json` |
| 74 | 8. Provide link to ui.perfetto.dev |
| 75 | |
| 76 | ### Build System Detection |
| 77 | **Makefile:** Add flags conditionally |
| 78 | ```makefile |
| 79 | ENABLE_TRACE ?= 0 |
| 80 | ifeq ($(ENABLE_TRACE),1) |
| 81 | CFLAGS += -finstrument-functions -g |
| 82 | LDFLAGS += -L. -ltrace -ldl -lpthread |
| 83 | endif |
| 84 | ``` |
| 85 | |
| 86 | **CMake:** Add option |
| 87 | ```cmake |
| 88 | option(ENABLE_TRACE "Enable tracing" OFF) |
| 89 | if(ENABLE_TRACE) |
| 90 | add_compile_options(-finstrument-functions -g) |
| 91 | link_libraries(trace dl pthread) |
| 92 | endif() |
| 93 | ``` |
| 94 | |
| 95 | ## Output |
| 96 | - **trace_<tid>.log**: Per-thread text logs |
| 97 | - **trace.json**: Perfetto Chrome JSON format |
| 98 | - View at https://ui.perfetto.dev |
| 99 | |
| 100 | ## Perfetto JSON Format |
| 101 | Function ENTRY → "B" (begin) event |
| 102 | Function EXIT! → "E" (end) event |
| 103 | All threads aligned by timestamp in single file. |