Skip to content

Commit bbb8df9

Browse files
authored
Added functionality to detach all uprobes for a binary (#5325)
Added a new method in BCC StatusTuple BPF::detach_all_uprobes_for_binary(const std::string& binary_path) This method detaches all uprobe probes that are associated with the given binary path. It works by matching the sanitized binary path against the event names of all currently attached uprobes, and detaches any that match. Importantly, this function does not check if the binary file still exists on disk; it simply removes all matching uprobes from the internal tracking and from the kernel. This is useful for cleaning up probes that may otherwise remain attached if the binary is deleted or moved, or if you want to forcibly remove all uprobes for a specific binary regardless of its current presence on the filesystem. Use Case Automatic Cleanup When a Binary is Deleted: If a monitored binary is removed from the filesystem, any uprobes attached to it will remain active in the kernel unless explicitly detached. This function allows you to clean up all such probes, preventing resource (fd) leaks and potential errors. Bulk Detachment: When updating or replacing a binary, you may want to remove all associated uprobes before re-attaching new ones. This function provides a simple way to do so without needing to track individual probe details. Example: Suppose you are tracing a user-space binary /proc//exe and that binary is deleted still you can call: bpf.detach_all_uprobes_for_binary("/proc/<program-pid>/exe"); This will remove all uprobes associated with stoped program-pid , even if the file no longer exists, ensuring no stale probes are left behind. With this functionality #4843 will fixed
1 parent 4bc4858 commit bbb8df9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/cc/api/BPF.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,35 @@ StatusTuple BPF::detach_uprobe(const std::string& binary_path,
528528
return StatusTuple::OK();
529529
}
530530

531+
// Detach all uprobes associated with the given binary path.
532+
// This function cleans up all matching uprobes without checking if the binary file exists.
533+
// It simply matches the sanitized binary path in the event names and detaches them.
534+
535+
StatusTuple BPF::detach_all_uprobes_for_binary(const std::string& binary_path) {
536+
bool has_error = false;
537+
std::string error_msg;
538+
std::vector<std::string> to_detach;
539+
540+
// Sanitize the binary path as used in event names
541+
std::string sanitized_path = sanitize_str(binary_path, &BPF::uprobe_path_validator);
542+
// Find all uprobes for this binary
543+
for (auto& it : uprobes_) {
544+
if (it.first.find(sanitized_path) != std::string::npos) {
545+
auto res = detach_uprobe_event(it.first, it.second);
546+
if (!res.ok()) {
547+
error_msg += "Failed to detach uprobe event " + it.first + ": ";
548+
error_msg += res.msg() + "\n";
549+
has_error = true;
550+
}
551+
uprobes_.erase(it.first);
552+
}
553+
}
554+
if (has_error)
555+
return StatusTuple(-1, error_msg);
556+
else
557+
return StatusTuple::OK();
558+
}
559+
531560
StatusTuple BPF::detach_usdt_without_validation(const USDT& u, pid_t pid) {
532561
auto& probe = *static_cast<::USDT::Probe*>(u.probe_.get());
533562
bool failed = false;

src/cc/api/BPF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class BPF {
152152
bpf_probe_attach_type attach_type = BPF_PROBE_ENTRY,
153153
pid_t pid = -1,
154154
uint64_t symbol_offset = 0);
155+
StatusTuple detach_all_uprobes_for_binary(const std::string& binary_path);
155156
StatusTuple attach_usdt(const USDT& usdt, pid_t pid = -1);
156157
StatusTuple attach_usdt_all();
157158
StatusTuple detach_usdt(const USDT& usdt, pid_t pid = -1);

0 commit comments

Comments
 (0)