00a0fa2d7d
Extend urandom_read helper binary to include USDTs of 4 combinations: semaphore/semaphoreless (refcounted and non-refcounted) and based in executable or shared library. We also extend urandom_read with ability to report it's own PID to parent process and wait for parent process to ready itself up for tracing urandom_read. We utilize popen() and underlying pipe properties for proper signaling. Once urandom_read is ready, we add few tests to validate that libbpf's USDT attachment handles all the above combinations of semaphore (or lack of it) and static or shared library USDTs. Also, we validate that libbpf handles shared libraries both with PID filter and without one (i.e., -1 for PID argument). Having the shared library case tested with and without PID is important because internal logic differs on kernels that don't support BPF cookies. On such older kernels, attaching to USDTs in shared libraries without specifying concrete PID doesn't work in principle, because it's impossible to determine shared library's load address to derive absolute IPs for uprobe attachments. Without absolute IPs, it's impossible to perform correct look up of USDT spec based on uprobe's absolute IP (the only kind available from BPF at runtime). This is not the problem on newer kernels with BPF cookie as we don't need IP-to-ID lookup because BPF cookie value *is* spec ID. So having those two situations as separate subtests is good because libbpf CI is able to test latest selftests against old kernels (e.g., 4.9 and 5.5), so we'll be able to disable PID-less shared lib attachment for old kernels, but will still leave PID-specific one enabled to validate this legacy logic is working correctly. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Dave Marchevsky <davemarchevsky@fb.com> Link: https://lore.kernel.org/bpf/20220404234202.331384-8-andrii@kernel.org
89 lines
1.9 KiB
C
89 lines
1.9 KiB
C
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <stdlib.h>
|
|
#include <signal.h>
|
|
|
|
#define _SDT_HAS_SEMAPHORES 1
|
|
#include "sdt.h"
|
|
|
|
#define SEC(name) __attribute__((section(name), used))
|
|
|
|
#define BUF_SIZE 256
|
|
|
|
/* defined in urandom_read_aux.c */
|
|
void urand_read_without_sema(int iter_num, int iter_cnt, int read_sz);
|
|
/* these are coming from urandom_read_lib{1,2}.c */
|
|
void urandlib_read_with_sema(int iter_num, int iter_cnt, int read_sz);
|
|
void urandlib_read_without_sema(int iter_num, int iter_cnt, int read_sz);
|
|
|
|
unsigned short urand_read_with_sema_semaphore SEC(".probes");
|
|
|
|
static __attribute__((noinline))
|
|
void urandom_read(int fd, int count)
|
|
{
|
|
char buf[BUF_SIZE];
|
|
int i;
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
read(fd, buf, BUF_SIZE);
|
|
|
|
/* trigger USDTs defined in executable itself */
|
|
urand_read_without_sema(i, count, BUF_SIZE);
|
|
STAP_PROBE3(urand, read_with_sema, i, count, BUF_SIZE);
|
|
|
|
/* trigger USDTs defined in shared lib */
|
|
urandlib_read_without_sema(i, count, BUF_SIZE);
|
|
urandlib_read_with_sema(i, count, BUF_SIZE);
|
|
}
|
|
}
|
|
|
|
static volatile bool parent_ready;
|
|
|
|
static void handle_sigpipe(int sig)
|
|
{
|
|
parent_ready = true;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
int count = 4;
|
|
bool report_pid = false;
|
|
|
|
if (fd < 0)
|
|
return 1;
|
|
|
|
if (argc >= 2)
|
|
count = atoi(argv[1]);
|
|
if (argc >= 3) {
|
|
report_pid = true;
|
|
/* install SIGPIPE handler to catch when parent closes their
|
|
* end of the pipe (on the other side of our stdout)
|
|
*/
|
|
signal(SIGPIPE, handle_sigpipe);
|
|
}
|
|
|
|
/* report PID and wait for parent process to send us "signal" by
|
|
* closing stdout
|
|
*/
|
|
if (report_pid) {
|
|
while (!parent_ready) {
|
|
fprintf(stdout, "%d\n", getpid());
|
|
fflush(stdout);
|
|
}
|
|
/* at this point stdout is closed, parent process knows our
|
|
* PID and is ready to trace us
|
|
*/
|
|
}
|
|
|
|
urandom_read(fd, count);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|