Skip to content

Commit 660c182

Browse files
committed
chore(libscap/engine/bpf): improve error logging
Right now if perf_event_mmap() fails, it will buffer some diagnostic info using scap_errprintf(), spelling out which of the two mmap() calls failed. However, upon detecting a failure, the calling function will also call scap_errprintf() and therefore overwrite the previous log line. This change: a) adds the actual values passed to mmap() to the log line, so get a bit more context b) suppresses the subsequent scap_errprintf() invocation so to surface the orignal log line instead. Signed-off-by: Gerlando Falauto <[email protected]>
1 parent e074796 commit 660c182

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

userspace/libscap/engine/bpf/scap_bpf.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,9 @@ static void *perf_event_mmap(struct bpf_engine *handle,
813813
if(tmp == MAP_FAILED) {
814814
scap_errprintf(handle->m_lasterr,
815815
errno,
816-
"mmap (1) failed (If you get memory allocation errors try to reduce the "
817-
"buffer dimension)");
816+
"mmap (1) size %lu failed (If you get memory allocation errors try "
817+
"to reduce the buffer dimension)",
818+
total_size);
818819
return MAP_FAILED;
819820
}
820821

@@ -827,8 +828,10 @@ static void *perf_event_mmap(struct bpf_engine *handle,
827828
if(p1 == MAP_FAILED) {
828829
scap_errprintf(handle->m_lasterr,
829830
errno,
830-
"mmap (2) failed (If you get memory allocation errors try to reduce the "
831-
"buffer dimension)");
831+
"mmap (2) addr %p size %lu failed (If you get memory allocation errors try "
832+
"to reduce the buffer dimension)",
833+
tmp,
834+
ring_size + header_size);
832835
munmap(tmp, total_size);
833836
return MAP_FAILED;
834837
}
@@ -1538,10 +1541,8 @@ int32_t scap_bpf_load(struct bpf_engine *handle, unsigned long buffer_bytes_dim)
15381541
dev->m_buffer = perf_event_mmap(handle, pmu_fd, &dev->m_mmap_size, buffer_bytes_dim);
15391542
dev->m_buffer_size = buffer_bytes_dim;
15401543
if(dev->m_buffer == MAP_FAILED) {
1541-
return scap_errprintf(handle->m_lasterr,
1542-
errno,
1543-
"unable to mmap the perf-buffer for cpu '%d'",
1544-
cpu_idx);
1544+
// No need to overwrite the error already prepared by perf_event_mmap
1545+
return SCAP_FAILURE;
15451546
}
15461547
online_idx++;
15471548
}

0 commit comments

Comments
 (0)