// Ctrl+C handling for Windows console apps static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) { if (is_tracing && fdwCtrlType == CTRL_C_EVENT) { printf("Ctrl-C detected! Flushing trace and shutting down.\n\n"); mtr_flush(); mtr_shutdown(); } ExitProcess(1); }
static void termination_handler(int signum) { (void) signum; if (is_tracing) { printf("Ctrl-C detected! Flushing trace and shutting down.\n\n"); mtr_flush(); fwrite("\n]}\n", 1, 4, f); fclose(f); } exit(1); }
int main() { int i; mtr_init("trace.json"); mtr_register_sigint_handler(); MTR_META_PROCESS_NAME("minitrace_test"); MTR_META_THREAD_NAME("main thread"); int long_running_thing_1; int long_running_thing_2; MTR_START("background", "long_running", &long_running_thing_1); MTR_START("background", "long_running", &long_running_thing_2); MTR_COUNTER("main", "greebles", 3); MTR_BEGIN("main", "outer"); usleep(80000); for (i = 0; i < 3; i++) { MTR_BEGIN("main", "inner"); usleep(40000); MTR_END("main", "inner"); usleep(10000); MTR_COUNTER("main", "greebles", 3 * i + 10); } MTR_STEP("background", "long_running", &long_running_thing_1, "middle step"); usleep(80000); MTR_END("main", "outer"); MTR_COUNTER("main", "greebles", 0); usleep(10000); a(); usleep(50000); MTR_INSTANT("main", "the end"); usleep(10000); MTR_FINISH("background", "long_running", &long_running_thing_1); MTR_FINISH("background", "long_running", &long_running_thing_2); mtr_flush(); mtr_shutdown(); return 0; }
void mtr_shutdown() { int i; #ifndef MTR_ENABLED return; #endif is_tracing = 0; mtr_flush(); fwrite("\n]}\n", 1, 4, f); fclose(f); pthread_mutex_destroy(&mutex); f = 0; free(buffer); buffer = 0; for (i = 0; i < STRING_POOL_SIZE; i++) { if (str_pool[i]) { free(str_pool[i]); str_pool[i] = 0; } } }