#!/bin/sh

sysctl -w security.pax.mprotect.enabled=0 || true
sysctl -w security.pax.mprotect.ptrace=0 || true
 
cat >test.c <<EOF
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define N 30

void g();

void *f(void *arg) {
    sleep(rand() % 5);
    g();
    return 0;
}

void g() {
    printf(".");
    fflush(stdout);
}

int main(int argc, char **argv) {
    int i;
    pthread_t threads[N];

    for (i = 0; i < N; i++) {
        int r = pthread_create(&threads[i], NULL, f, NULL);
    }
    for (i = 0; i < N; i++) {
        int r = pthread_join(threads[i], NULL);
    }
}
EOF

cat >test.gdb <<EOF
exec-file test
symbol-file test
break g
run
cont 100
EOF

cc test.c -lpthread -o test

gdb -batch -x test.gdb | tee log

! grep SIGSEGV log
