---
#include <execinfo.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdio.h>
#include <unistd.h>
 
void* callstack[256];
int stack_size;

#define STARTED 1
#define DONE 0
_Atomic int state;

void dump_backtrace(int signum) {
    stack_size = backtrace(callstack, 256);
    atomic_store(&state, DONE);
}

int collect_stack(pthread_t thread) {
    atomic_store(&state, STARTED);
    signal(SIGUSR2, dump_backtrace);

    int result = pthread_kill(thread, SIGUSR2);
    if (result != 0) {
        printf("Fail to signal: %d\n", result);
        atomic_store(&state, DONE);
        return -1;
    }

    while (atomic_load(&state) != DONE) {}

    return stack_size;
}

void set_signal_handler() {
}
