It turns out that perf
already has the required features for perf probe
, but only for kernel-space at the moment.
perf
probes can take arguments, which may be virtuals like $retval
, registers like %ax
, or c identifiers and simple expressions for local or global variables.
So, if perf
did support user-space symbolic probes for arguments, you'd create a probe to capture the query_string
argument to exec_simple_query
is invoked with with something like:
perf probe -x /path/to/postgres exec_simple_query debug_query_string:string
The :string
tells perf
it's a C string, so it should deref the pointer and copy the data.
There are multiple places queries can come in - the simple protocol, the v3 parse/bind/execute protocol, the SPI, etc. This is only one of them. You could capture the query from the parser in raw_parse
instead, or grab the debug_query_string
global value from probes on events of interest.
Unfortunately, none of this will work yet, because perf
won't do symbolic lookups on user-space binaries:
$ sudo perf probe -x /path/to/postgres exec_simple_query debug_query_string:string
Debuginfo-analysis is not yet supported with -x/--exec option.
Error: Failed to add events. (-38)
$ perf --version; uname -r
perf version 3.11.6
3.11.6-201.fc19.x86_64
So - if perf
has support for symbolic lookups added you'll be able to do exciting things like capture the query text in the executor by looking up a struct member:
perf probe -x `which postgres` standard_ExecutorStart 'queryDesc->sourceText:string'
... but again, perf
doesn't know how to do the required symbolic look-ups yet, and it can't capture C strings from registers and $retval
. So: wait for a new perf
, unless you're keen on enhancing the tooling yourself. Oh well.