Skip to content

Commit 11750e4

Browse files
yunwei37anakryiko
authored andcommitted
examples/c: improve uprobe with function name
The code changes in this commit improve the functionality of the uprobe by using the function name instead of the function offset. This change is made to simplify the code and make it more readable. Signed-off-by: yunwei37 <1067852565@qq.com>
1 parent e15f474 commit 11750e4

File tree

1 file changed

+16
-48
lines changed

1 file changed

+16
-48
lines changed

examples/c/uprobe.c

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,6 @@ static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va
1212
return vfprintf(stderr, format, args);
1313
}
1414

15-
/*
16-
* Taken from https://github.com/torvalds/linux/blob/9b59ec8d50a1f28747ceff9a4f39af5deba9540e/tools/testing/selftests/bpf/trace_helpers.c#L149-L205
17-
*
18-
* See discussion in https://github.com/libbpf/libbpf-bootstrap/pull/90
19-
*/
20-
ssize_t get_uprobe_offset(const void *addr)
21-
{
22-
size_t start, end, base;
23-
char buf[256];
24-
bool found = false;
25-
FILE *f;
26-
27-
f = fopen("/proc/self/maps", "r");
28-
if (!f)
29-
return -errno;
30-
31-
while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) {
32-
if (buf[2] == 'x' && (uintptr_t)addr >= start && (uintptr_t)addr < end) {
33-
found = true;
34-
break;
35-
}
36-
}
37-
38-
fclose(f);
39-
40-
if (!found)
41-
return -ESRCH;
42-
43-
return (uintptr_t)addr - start + base;
44-
}
45-
4615
/* It's a global function to make sure compiler doesn't inline it. */
4716
int uprobed_add(int a, int b)
4817
{
@@ -57,8 +26,8 @@ int uprobed_sub(int a, int b)
5726
int main(int argc, char **argv)
5827
{
5928
struct uprobe_bpf *skel;
60-
long uprobe_offset;
6129
int err, i;
30+
LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
6231

6332
/* Set up libbpf errors and debug info callback */
6433
libbpf_set_print(libbpf_print_fn);
@@ -70,20 +39,18 @@ int main(int argc, char **argv)
7039
return 1;
7140
}
7241

42+
/* Attach tracepoint handler */
43+
uprobe_opts.func_name = "uprobed_add";
44+
uprobe_opts.retprobe = false;
7345
/* uprobe/uretprobe expects relative offset of the function to attach
74-
* to. This offset is relateve to the process's base load address. So
75-
* easy way to do this is to take an absolute address of the desired
76-
* function and substract base load address from it. If we were to
77-
* parse ELF to calculate this function, we'd need to add .text
78-
* section offset and function's offset within .text ELF section.
46+
* to. libbpf will automatically find the offset for us if we provide the
47+
* function name. If the function name is not specified, libbpf will try
48+
* to use the function offset instead.
7949
*/
80-
uprobe_offset = get_uprobe_offset(&uprobed_add);
81-
82-
/* Attach tracepoint handler */
83-
skel->links.uprobe_add =
84-
bpf_program__attach_uprobe(skel->progs.uprobe_add, false /* not uretprobe */,
85-
0 /* self pid */, "/proc/self/exe", uprobe_offset);
86-
50+
skel->links.uprobe_add = bpf_program__attach_uprobe_opts(skel->progs.uprobe_add,
51+
0 /* self pid */, "/proc/self/exe",
52+
0 /* offset for function */,
53+
&uprobe_opts /* opts */);
8754
if (!skel->links.uprobe_add) {
8855
err = -errno;
8956
fprintf(stderr, "Failed to attach uprobe: %d\n", err);
@@ -94,10 +61,11 @@ int main(int argc, char **argv)
9461
* processes that use the same binary executable; to do that we need
9562
* to specify -1 as PID, as we do here
9663
*/
97-
skel->links.uretprobe_add =
98-
bpf_program__attach_uprobe(skel->progs.uretprobe_add, true /* uretprobe */,
99-
-1 /* any pid */, "/proc/self/exe", uprobe_offset);
100-
64+
uprobe_opts.func_name = "uprobed_add";
65+
uprobe_opts.retprobe = true;
66+
skel->links.uretprobe_add = bpf_program__attach_uprobe_opts(
67+
skel->progs.uretprobe_add, -1 /* self pid */, "/proc/self/exe",
68+
0 /* offset for function */, &uprobe_opts /* opts */);
10169
if (!skel->links.uretprobe_add) {
10270
err = -errno;
10371
fprintf(stderr, "Failed to attach uprobe: %d\n", err);

0 commit comments

Comments
 (0)