3 ms·
> The other threads need to continue running while the thread waiting on open(2) is paused, but ptrace pauses the whole process. This definitely doesn't sound
by Hello71 6y ago
> The other threads need to continue running while the thread waiting on open(2) is paused, but ptrace pauses the whole process.
This definitely doesn't sound right. Running strace, you need to specify -f in order to trace threads, otherwise only the main thread is traced. gdb does stop all threads on interrupts, but only by default for convenience, and I don't think it's atomic.
Because of this, LD_PRELOAD is not the easiest way to delay system calls, strace -e inject:delay is.
$ cat > a.c << EOF
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
void *start_thread(void *arg) {
while (1) {
puts("thread");
sleep(1);
}
}
int main() {
pthread_t thr;
pthread_create(&thr, NULL, start_thread, NULL);
while (1) {
puts("main");
close(open("/dev/null", O_RDONLY));
sleep(1);
}
}
EOF
$ gcc a.c
$ strace -o /dev/null -P /dev/null -e inject=openat:delay_enter=3s ./a.out
main
thread
thread
thread
thread
thread
main
thread
thread
thread
thread
thread
[ and so on. ]