Search code examples
linux-kernellibcperf

Perf probe event for C variable assignment


I've been using perf probe with malloc, but can't seem to find a suitable perf event for when variable assignment happens. Is there such an event?

Ideally when something like int var = 17; occurs there is some corresponding event I'm missing? Outside of the instantiation of the variable, but the actual assignment of the value and on each successive change.


Solution

  • Yes this is possible with hardware breakpoint events. perf record supports this if you know the address:

    a hardware breakpoint event in the form of \mem:addr[/len][:access] where addr is the address in memory you want to break in. Access is the memory access type (read, write, execute) it can be passed as follows: \mem:addr[:[r][w][x]]. len is the range, number of bytes from specified addr, which the breakpoint will cover. If you want to profile read-write accesses in 0x1000, just set mem:0x1000:rw. If you want to profile write accesses in [0x1000~1008), just set mem:0x1000/8:w.

    It may be difficult to get the memory address before hand. You can also use perf_event_open inside your program, but then you need to parse the perf sample records in your program.