libbpf: Support function name-based attach uprobes
authorAlan Maguire <alan.maguire@oracle.com>
Wed, 30 Mar 2022 15:26:37 +0000 (16:26 +0100)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 4 Apr 2022 01:12:05 +0000 (18:12 -0700)
commit433966e3ae04165811b116af492a684bad7a158c
tree27bd0ca269cbbb97d63b8c96b721874208a8a632
parent1ce3a60e3c287479f15147ffc61c35b2e436a0d5
libbpf: Support function name-based attach uprobes

kprobe attach is name-based, using lookups of kallsyms to translate
a function name to an address.  Currently uprobe attach is done
via an offset value as described in [1].  Extend uprobe opts
for attach to include a function name which can then be converted
into a uprobe-friendly offset.  The calcualation is done in
several steps:

1. First, determine the symbol address using libelf; this gives us
   the offset as reported by objdump
2. If the function is a shared library function - and the binary
   provided is a shared library - no further work is required;
   the address found is the required address
3. Finally, if the function is local, subtract the base address
   associated with the object, retrieved from ELF program headers.

The resultant value is then added to the func_offset value passed
in to specify the uprobe attach address.  So specifying a func_offset
of 0 along with a function name "printf" will attach to printf entry.

The modes of operation supported are then

1. to attach to a local function in a binary; function "foo1" in
   "/usr/bin/foo"
2. to attach to a shared library function in a shared library -
   function "malloc" in libc.

[1] https://www.kernel.org/doc/html/latest/trace/uprobetracer.html

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/1648654000-21758-3-git-send-email-alan.maguire@oracle.com
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.h