static void event_thread_init(void *drcontext) { per_thread_t *data = dr_thread_alloc(drcontext, sizeof(per_thread_t)); DR_ASSERT(data != NULL); drmgr_set_tls_field(drcontext, tls_idx, data); /* Keep seg_base in a per-thread data structure so we can get the TLS * slot and find where the pointer points to in the buffer. */ data->seg_base = dr_get_dr_segment_base(tls_seg); data->buf_base = dr_raw_mem_alloc(MEM_BUF_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE, NULL); DR_ASSERT(data->seg_base != NULL && data->buf_base != NULL); /* put buf_base to TLS as starting buf_ptr */ BUF_PTR(data->seg_base) = data->buf_base; data->num_refs = 0; /* We're going to dump our data to a per-thread file. * On Windows we need an absolute path so we place it in * the same directory as our library. We could also pass * in a path as a client argument. */ data->log = log_file_open(client_id, drcontext, NULL /* using client lib path */, "memtrace", #ifndef WINDOWS DR_FILE_CLOSE_ON_FORK | #endif DR_FILE_ALLOW_LARGE); data->logf = log_stream_from_file(data->log); fprintf(data->logf, "Format: <data address>: <data size>, <(r)ead/(w)rite/opcode>\n"); }
static void event_thread_init(void *drcontext) { per_thread_t *data = dr_thread_alloc(drcontext, sizeof(*data)); DR_ASSERT(data != NULL); dr_set_tls_field(drcontext, data); /* Keep seg_base in a per-thread data structure so we can get the TLS * slot and find where the pointer points to in the buffer. * It is mainly for users using a debugger to get the execution history. */ data->seg_base = dr_get_dr_segment_base(tls_seg); /* We allocate a 128KB buffer to make sure we have a 64KB buffer with * 64KB-aligned starting address, so that we can fill the buffer * cyclically by incrementing the bottom 16 bits of the pointer. */ data->buf_base = dr_raw_mem_alloc(TLS_BUF_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE, NULL); DR_ASSERT(data->seg_base != NULL && data->buf_base != NULL); memset(data->buf_base, 0, TLS_BUF_SIZE); /* put the 64KB-aligned address into TLS slot as the pointer pointing * to the 64KB cyclic buffer */ *(void **)((byte *)(data->seg_base) + tls_offs) = (void *) ALIGN_FORWARD(data->buf_base, BUF_64K_BYTE); }