50 lines · cpp
1#include "attach.h"2#include "pseudo_barrier.h"3#include <chrono>4#include <cstdio>5#include <fcntl.h>6#include <fstream>7#include <thread>8#include <vector>9 10pseudo_barrier_t barrier;11 12constexpr size_t nthreads = 5;13volatile bool wait_for_debugger_flag = true;14 15void break_here() {}16 17void tfunc() {18 pseudo_barrier_wait(barrier);19 20 break_here();21}22 23int main(int argc, char const *argv[]) {24 lldb_enable_attach();25 26 if (argc < 3)27 return 1;28 29 // Create a file to signal that this process has started up.30 std::ofstream(argv[1]).close();31 32 // And wait for it to attach.33 for (int i = 0; i < 100 && wait_for_debugger_flag; ++i)34 std::this_thread::sleep_for(std::chrono::seconds(1));35 36 // Fire up the threads and have them call break_here() simultaneously.37 pseudo_barrier_init(barrier, nthreads);38 std::vector<std::thread> threads;39 for (size_t i = 0; i < nthreads; ++i)40 threads.emplace_back(tfunc);41 42 for (std::thread &t : threads)43 t.join();44 45 // Create the file to let the debugger know we're running.46 std::ofstream(argv[2]).close();47 48 return 0;49}50