Skip to contentSkip to conversation

libbpf: Allow attach USDT BPF program without specifying binary path

PATCH In review Hengqi Chen 3 participants 4 messages Jun 30, 2022 – Jul 1, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: Allow attach USDT BPF program without specifying binary path

Currently, libbpf requires specifying binary path when attach USDT BPF program manually. This is not necessary because we can infer that from /proc/$PID/exe. This also avoids coredump when user do not provide binary path.

---
 tools/lib/bpf/libbpf.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +13 -1
index 8a45a84eb9b2..4ee9b6a0944e 100644
@@ -10686,7 +10686,19 @@ struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
 		return libbpf_err_ptr(-EINVAL);
 	}
 
-	if (!strchr(binary_path, '/')) {
+	if (!binary_path) {
+		if (pid < 0) {
+			pr_warn("prog '%s': missing attach target, pid or binary path required\n",
+				prog->name);
+			return libbpf_err_ptr(-EINVAL);
+		}
+		if (!pid)
+			binary_path = "/proc/self/exe";
+		else {
+			snprintf(resolved_path, sizeof(resolved_path), "/proc/%d/exe", pid);
+			binary_path = resolved_path;
+		}
+	} else if (!strchr(binary_path, '/')) {
 		err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
 		if (err) {
 			pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",

--
2.30.2

#1 lore raw

Hi Hengqi,

Quoted 21 lines · Hengqi Chen

Currently, libbpf requires specifying binary path when attach USDT BPF program manually. This is not necessary because we can infer that from /proc/$PID/exe. This also avoids coredump when user do not provide binary path.

---
 tools/lib/bpf/libbpf.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +2 -1
index 8a45a84eb9b2..4ee9b6a0944e 100644
@@ -10686,7 +10686,19 @@ struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
                return libbpf_err_ptr(-EINVAL);
        }
 
-       if (!strchr(binary_path, '/')) {
+       if (!binary_path) {
+               if (pid < 0) {

tools/lib/bpf/libbpf.c struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog, · line 10700

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+                       pr_warn("prog '%s': missing attach target, pid or binary path required\n",
+                               prog->name);
+                       return libbpf_err_ptr(-EINVAL);
+               }
+               if (!pid)
+                       binary_path = "/proc/self/exe";
+               else {
+                       snprintf(resolved_path, sizeof(resolved_path), "/proc/%d/exe", pid);
+                       binary_path = resolved_path;
+               }

Please add matching brackets for the 'then' clause.

Besides, do you need to call readlink() to extract the real path of the binary? Reading /proc/$pid/exe may fail due to not being root. Detecting such failures early and giving a warning would be great.

Quoted 7 lines
tools/lib/bpf/libbpf.c +1 -1
+       } else if (!strchr(binary_path, '/')) {
                err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
                if (err) {
                        pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
--

2.30.2

#2 lore raw
In reply to Hengqi Chen
Quoted 7 lines · Hengqi Chen

Currently, libbpf requires specifying binary path when attach USDT BPF program manually. This is not necessary because we can infer that from /proc/$PID/exe. This also avoids coredump when user do not provide binary path.

---

Hm... just because I specify PID, doesn't mean I mean main binary of that process, it could be some other shared library within that process.

I don't really like this change because it doesn't feel 100% obvious and natural. User can easily specify "/proc/self/exe" or "/proc/<pid>/exe" if that's what they are targeting.

Documentation for bpf_program__attach_usdt() states

@param binary_path Path to binary that contains provided USDT probe

it doesn't say it is optional and can be NULL, so there is no valid reason to pass NULL and expect things to work.

Quoted 31 lines
 tools/lib/bpf/libbpf.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +13 -2
index 8a45a84eb9b2..4ee9b6a0944e 100644
@@ -10686,7 +10686,19 @@ struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
                return libbpf_err_ptr(-EINVAL);
        }
 
-       if (!strchr(binary_path, '/')) {
+       if (!binary_path) {
+               if (pid < 0) {
+                       pr_warn("prog '%s': missing attach target, pid or binary path required\n",
+                               prog->name);
+                       return libbpf_err_ptr(-EINVAL);
+               }
+               if (!pid)
+                       binary_path = "/proc/self/exe";
+               else {
+                       snprintf(resolved_path, sizeof(resolved_path), "/proc/%d/exe", pid);
+                       binary_path = resolved_path;
+               }
+       } else if (!strchr(binary_path, '/')) {
                err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
                if (err) {
                        pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
--

2.30.2

#3 lore raw
Quoted 9 lines · Andrii Nakryiko
Quoted 7 lines · Hengqi Chen

Currently, libbpf requires specifying binary path when attach USDT BPF program manually. This is not necessary because we can infer that from /proc/$PID/exe. This also avoids coredump when user do not provide binary path.

---

Quoted 8 lines

Hm... just because I specify PID, doesn't mean I mean main binary of that process, it could be some other shared library within that process.

I don't really like this change because it doesn't feel 100% obvious and natural. User can easily specify "/proc/self/exe" or "/proc/<pid>/exe" if that's what they are targeting.

Actually, this is an implication and practice set by BCC. If path is not set, BCC assumes that it is targeting the process.

Quoted 8 lines

Documentation for bpf_program__attach_usdt() states

@param binary_path Path to binary that contains provided USDT probe

it doesn't say it is optional and can be NULL, so there is no valid reason to pass NULL and expect things to work.

OK, then we should at least check against NULL and emit a warning.

Quoted 31 lines
Quoted 31 lines
 tools/lib/bpf/libbpf.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +13 -2
index 8a45a84eb9b2..4ee9b6a0944e 100644
@@ -10686,7 +10686,19 @@ struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
                return libbpf_err_ptr(-EINVAL);
        }
 
-       if (!strchr(binary_path, '/')) {
+       if (!binary_path) {
+               if (pid < 0) {
+                       pr_warn("prog '%s': missing attach target, pid or binary path required\n",
+                               prog->name);
+                       return libbpf_err_ptr(-EINVAL);
+               }
+               if (!pid)
+                       binary_path = "/proc/self/exe";
+               else {
+                       snprintf(resolved_path, sizeof(resolved_path), "/proc/%d/exe", pid);
+                       binary_path = resolved_path;
+               }
+       } else if (!strchr(binary_path, '/')) {
                err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
                if (err) {
                        pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
--

2.30.2