@@ -12,37 +12,6 @@ static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va
12
12
return vfprintf (stderr , format , args );
13
13
}
14
14
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
-
46
15
/* It's a global function to make sure compiler doesn't inline it. */
47
16
int uprobed_add (int a , int b )
48
17
{
@@ -57,8 +26,8 @@ int uprobed_sub(int a, int b)
57
26
int main (int argc , char * * argv )
58
27
{
59
28
struct uprobe_bpf * skel ;
60
- long uprobe_offset ;
61
29
int err , i ;
30
+ LIBBPF_OPTS (bpf_uprobe_opts , uprobe_opts );
62
31
63
32
/* Set up libbpf errors and debug info callback */
64
33
libbpf_set_print (libbpf_print_fn );
@@ -70,20 +39,18 @@ int main(int argc, char **argv)
70
39
return 1 ;
71
40
}
72
41
42
+ /* Attach tracepoint handler */
43
+ uprobe_opts .func_name = "uprobed_add" ;
44
+ uprobe_opts .retprobe = false;
73
45
/* 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.
79
49
*/
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 */ );
87
54
if (!skel -> links .uprobe_add ) {
88
55
err = - errno ;
89
56
fprintf (stderr , "Failed to attach uprobe: %d\n" , err );
@@ -94,10 +61,11 @@ int main(int argc, char **argv)
94
61
* processes that use the same binary executable; to do that we need
95
62
* to specify -1 as PID, as we do here
96
63
*/
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 */ );
101
69
if (!skel -> links .uretprobe_add ) {
102
70
err = - errno ;
103
71
fprintf (stderr , "Failed to attach uprobe: %d\n" , err );
0 commit comments