static void event_module_unloaded(Context * ctx, void * client_data) { unsigned i; SymbolCacheEntry ** symbol_cache = EXT(ctx)->symbol_cache; if (symbol_cache == NULL) return; for (i = 0; i < SYMBOL_CACHE_SIZE; i++) { while (symbol_cache[i] != NULL) { SymbolCacheEntry * entry = symbol_cache[i]; symbol_cache[i] = entry->next; release_error_report(entry->error); loc_free(entry); } } }
static void waitpid_listener(int pid, int exited, int exit_code, int signal, int event_code, int syscall, void * args) { if (exited) { Context * stopped_ctx = context_find_from_pid(pid, 1); if (stopped_ctx != NULL) { /* TODO: need call back for vxdbgCont() * assert(!stopped_ctx->stopped) can fail if a task is resumed outside TCF agent. */ assert(!stopped_ctx->stopped); assert(!stopped_ctx->exited); trace(LOG_CONTEXT, "context: exited ctx %#lx, id %#x", stopped_ctx, EXT(stopped_ctx)->pid); release_error_report(EXT(stopped_ctx)->regs_error); loc_free(EXT(stopped_ctx)->regs); EXT(stopped_ctx)->regs_error = NULL; EXT(stopped_ctx)->regs = NULL; send_context_exited_event(stopped_ctx); } } }
static int kill_context(Context * ctx) { ContextExtensionVxWorks * ext = EXT(ctx); assert(ctx->stopped); assert(ctx->parent != NULL); if (taskDelete(ext->pid) != OK) { int error = errno; trace(LOG_ALWAYS, "context: can't kill ctx %#lx, id %#x: %s", ctx, ext->pid, errno_to_str(error)); return -1; } send_context_started_event(ctx); trace(LOG_CONTEXT, "context: killed ctx %#lx, id %#x", ctx, ext->pid); release_error_report(ext->regs_error); loc_free(ext->regs); ext->regs_error = NULL; ext->regs = NULL; send_context_exited_event(ctx); return 0; }
static int find_cache_symbol(Context * ctx, int frame, ULONG64 pc, PCSTR name, Symbol * sym) { ContextExtensionWinSym * ext = EXT(ctx->mem); if (ext->symbol_cache != NULL) { int cnt = 0; SymbolCacheEntry * entry = ext->symbol_cache[symbol_hash(pc, name)]; while (entry != NULL) { if (entry->pc == pc && strcmp(entry->name, name) == 0) { if (entry->error == NULL) { if (entry->frame_relative) { assert(frame >= 0); sym->frame = frame - STACK_NO_FRAME; } else { ctx = ctx->mem; } sym->ctx = ctx; sym->sym_class = entry->sym_class; sym->module = entry->module; sym->index = entry->index; } set_error_report_errno(entry->error); return 1; } else if (cnt > 32) { while (entry->next) { SymbolCacheEntry * next = entry->next; entry->next = next->next; release_error_report(next->error); loc_free(next); } return 0; } entry = entry->next; cnt++; } } return 0; }
static void event_module_loaded(Context * ctx, void * client_data) { unsigned i; SymbolCacheEntry ** symbol_cache = EXT(ctx)->symbol_cache; assert(ctx->mem == ctx); if (symbol_cache == NULL) return; for (i = 0; i < SYMBOL_CACHE_SIZE; i++) { SymbolCacheEntry * prev = NULL; SymbolCacheEntry * next = symbol_cache[i]; while (next != NULL) { SymbolCacheEntry * entry = next; next = next->next; if (entry->error) { if (prev) prev->next = next; else symbol_cache[i] = next; release_error_report(entry->error); loc_free(entry); } else { prev = entry; } } } }
static void event_handler(void * arg) { struct event_info * info = (struct event_info *)arg; Context * current_ctx = context_find_from_pid(info->current_ctx.ctxId, 1); Context * stopped_ctx = context_find_from_pid(info->stopped_ctx.ctxId, 1); switch (info->event) { case EVENT_HOOK_BREAKPOINT: if (stopped_ctx == NULL) break; assert(!stopped_ctx->stopped); assert(!EXT(stopped_ctx)->regs_dirty); if (EXT(stopped_ctx)->regs_error) { release_error_report(EXT(stopped_ctx)->regs_error); EXT(stopped_ctx)->regs_error = NULL; } memcpy(EXT(stopped_ctx)->regs, &info->regs, sizeof(REG_SET)); EXT(stopped_ctx)->event = 0; stopped_ctx->signal = SIGTRAP; stopped_ctx->stopped = 1; stopped_ctx->stopped_by_bp = info->bp_info_ok; stopped_ctx->stopped_by_exception = 0; assert(get_regs_PC(stopped_ctx) == info->addr); if (stopped_ctx->stopped_by_bp && !is_breakpoint_address(stopped_ctx, info->addr)) { /* Break instruction that is not planted by us */ stopped_ctx->stopped_by_bp = 0; stopped_ctx->pending_intercept = 1; } EXT(stopped_ctx)->bp_info = info->bp_info; if (current_ctx != NULL) EXT(stopped_ctx)->bp_pid = EXT(current_ctx)->pid; assert(taskIsStopped(EXT(stopped_ctx)->pid)); trace(LOG_CONTEXT, "context: stopped by breakpoint: ctx %#lx, id %#x", stopped_ctx, EXT(stopped_ctx)->pid); send_context_stopped_event(stopped_ctx); break; case EVENT_HOOK_STEP_DONE: if (current_ctx == NULL) break; assert(!current_ctx->stopped); assert(!EXT(current_ctx)->regs_dirty); if (EXT(current_ctx)->regs_error) { release_error_report(EXT(current_ctx)->regs_error); EXT(current_ctx)->regs_error = NULL; } memcpy(EXT(current_ctx)->regs, &info->regs, sizeof(REG_SET)); EXT(current_ctx)->event = TRACE_EVENT_STEP; current_ctx->signal = SIGTRAP; current_ctx->stopped = 1; current_ctx->stopped_by_bp = 0; current_ctx->stopped_by_exception = 0; assert(taskIsStopped(EXT(current_ctx)->pid)); trace(LOG_CONTEXT, "context: stopped by end of step: ctx %#lx, id %#x", current_ctx, EXT(current_ctx)->pid); send_context_stopped_event(current_ctx); break; case EVENT_HOOK_STOP: if (stopped_ctx == NULL) break; assert(!stopped_ctx->exited); if (stopped_ctx->stopped) break; if (EXT(stopped_ctx)->regs_error) { release_error_report(EXT(stopped_ctx)->regs_error); EXT(stopped_ctx)->regs_error = NULL; } if (taskRegsGet(EXT(stopped_ctx)->pid, EXT(stopped_ctx)->regs) != OK) { EXT(stopped_ctx)->regs_error = get_error_report(errno); assert(EXT(stopped_ctx)->regs_error != NULL); } EXT(stopped_ctx)->event = 0; stopped_ctx->signal = SIGSTOP; stopped_ctx->stopped = 1; stopped_ctx->stopped_by_bp = 0; stopped_ctx->stopped_by_exception = 0; assert(taskIsStopped(EXT(stopped_ctx)->pid)); trace(LOG_CONTEXT, "context: stopped by sofware request: ctx %#lx, id %#x", stopped_ctx, EXT(stopped_ctx)->pid); send_context_stopped_event(stopped_ctx); break; case EVENT_HOOK_TASK_ADD: if (current_ctx == NULL) break; assert(stopped_ctx == NULL); stopped_ctx = create_context(pid2id((pid_t)info->stopped_ctx.ctxId, EXT(current_ctx->parent)->pid)); EXT(stopped_ctx)->pid = (pid_t)info->stopped_ctx.ctxId; EXT(stopped_ctx)->regs = (REG_SET *)loc_alloc(sizeof(REG_SET)); stopped_ctx->mem = current_ctx->mem; stopped_ctx->big_endian = current_ctx->mem->big_endian; (stopped_ctx->creator = current_ctx)->ref_count++; (stopped_ctx->parent = current_ctx->parent)->ref_count++; assert(stopped_ctx->mem == stopped_ctx->parent->mem); list_add_last(&stopped_ctx->cldl, &stopped_ctx->parent->children); link_context(stopped_ctx); trace(LOG_CONTEXT, "context: created: ctx %#lx, id %#x", stopped_ctx, EXT(stopped_ctx)->pid); send_context_created_event(stopped_ctx); break; default: assert(0); break; } loc_free(info); SPIN_LOCK_ISR_TAKE(&events_lock); events_cnt--; SPIN_LOCK_ISR_GIVE(&events_lock); }
static void event_pid_stopped(pid_t pid, int signal, int event, int syscall) { int stopped_by_exception = 0; unsigned long msg = 0; Context * ctx = NULL; Context * ctx2 = NULL; trace(LOG_EVENTS, "event: pid %d stopped, signal %d", pid, signal); ctx = context_find_from_pid(pid, 1); if (ctx == NULL) { ctx = find_pending(pid); if (ctx != NULL) { Context * prs = ctx; assert(prs->ref_count == 0); ctx = create_context(pid2id(pid, pid)); EXT(ctx)->pid = pid; EXT(ctx)->regs = (REG_SET *)loc_alloc(sizeof(REG_SET)); ctx->pending_intercept = 1; ctx->mem = prs; ctx->parent = prs; ctx->big_endian = prs->big_endian; prs->ref_count++; list_add_last(&ctx->cldl, &prs->children); link_context(prs); link_context(ctx); send_context_created_event(prs); send_context_created_event(ctx); if (EXT(prs)->attach_callback) { EXT(prs)->attach_callback(0, prs, EXT(prs)->attach_data); EXT(prs)->attach_callback = NULL; EXT(prs)->attach_data = NULL; } } } if (ctx == NULL) return; assert(!ctx->exited); assert(!EXT(ctx)->attach_callback); if (signal != SIGSTOP && signal != SIGTRAP) { sigset_set(&ctx->pending_signals, signal, 1); if (sigset_get(&ctx->sig_dont_stop, signal) == 0) { ctx->pending_intercept = 1; stopped_by_exception = 1; } } if (ctx->stopped) { send_context_changed_event(ctx); } else { thread_state_t state; unsigned int state_count; ContextAddress pc0 = 0; ContextAddress pc1 = 0; assert(!EXT(ctx)->regs_dirty); EXT(ctx)->end_of_step = 0; EXT(ctx)->ptrace_event = event; ctx->signal = signal; ctx->stopped_by_bp = 0; ctx->stopped_by_exception = stopped_by_exception; ctx->stopped = 1; if (EXT(ctx)->regs_error) { release_error_report(EXT(ctx)->regs_error); EXT(ctx)->regs_error = NULL; } else { pc0 = get_regs_PC(ctx); } if (thread_get_state(EXT(ctx)->pid, x86_THREAD_STATE32, EXT(ctx)->regs, &state_count) != KERN_SUCCESS) { assert(errno != 0); EXT(ctx)->regs_error = get_error_report(errno); trace(LOG_ALWAYS, "error: thread_get_state failed; id %s, error %d %s", ctx->id, errno, errno_to_str(errno)); } else { pc1 = get_regs_PC(ctx); } if (!EXT(ctx)->syscall_enter || EXT(ctx)->regs_error || pc0 != pc1) { EXT(ctx)->syscall_enter = 0; EXT(ctx)->syscall_exit = 0; EXT(ctx)->syscall_id = 0; EXT(ctx)->syscall_pc = 0; } trace(LOG_EVENTS, "event: pid %d stopped at PC = %#lx", pid, pc1); if (signal == SIGTRAP && event == 0 && !syscall) { size_t break_size = 0; get_break_instruction(ctx, &break_size); ctx->stopped_by_bp = !EXT(ctx)->regs_error && is_breakpoint_address(ctx, pc1 - break_size); EXT(ctx)->end_of_step = !ctx->stopped_by_bp && EXT(ctx)->pending_step; if (ctx->stopped_by_bp) set_regs_PC(ctx, pc1 - break_size); } EXT(ctx)->pending_step = 0; send_context_stopped_event(ctx); } }
static void event_pid_exited(pid_t pid, int status, int signal) { Context * ctx; ctx = context_find_from_pid(pid, 1); if (ctx == NULL) { ctx = find_pending(pid); if (ctx == NULL) { trace(LOG_EVENTS, "event: ctx not found, pid %d, exit status %d, term signal %d", pid, status, signal); } else { assert(ctx->ref_count == 0); ctx->ref_count = 1; if (EXT(ctx)->attach_callback != NULL) { if (status == 0) status = EINVAL; EXT(ctx)->attach_callback(status, ctx, EXT(ctx)->attach_data); } assert(list_is_empty(&ctx->children)); assert(ctx->parent == NULL); ctx->exited = 1; context_unlock(ctx); } } else { /* Note: ctx->exiting should be 1 here. However, PTRACE_EVENT_EXIT can be lost by PTRACE because of racing * between PTRACE_CONT (or PTRACE_SYSCALL) and SIGTRAP/PTRACE_EVENT_EXIT. So, ctx->exiting can be 0. */ if (EXT(ctx->parent)->pid == pid) ctx = ctx->parent; assert(EXT(ctx)->attach_callback == NULL); if (ctx->exited) { trace(LOG_EVENTS, "event: ctx %#lx, pid %d, exit status %d unexpected, stopped %d, exited %d", ctx, pid, status, ctx->stopped, ctx->exited); } else { trace(LOG_EVENTS, "event: ctx %#lx, pid %d, exit status %d, term signal %d", ctx, pid, status, signal); ctx->exiting = 1; if (ctx->stopped) send_context_started_event(ctx); if (!list_is_empty(&ctx->children)) { LINK * l = ctx->children.next; while (l != &ctx->children) { Context * c = cldl2ctxp(l); l = l->next; assert(c->parent == ctx); if (!c->exited) { c->exiting = 1; if (c->stopped) send_context_started_event(c); release_error_report(EXT(c)->regs_error); loc_free(EXT(c)->regs); EXT(c)->regs_error = NULL; EXT(c)->regs = NULL; send_context_exited_event(c); } } } release_error_report(EXT(ctx)->regs_error); loc_free(EXT(ctx)->regs); EXT(ctx)->regs_error = NULL; EXT(ctx)->regs = NULL; send_context_exited_event(ctx); } } }
static void event_pid_stopped(pid_t pid, int signal, int event, int syscall) { int stopped_by_exception = 0; Context * ctx = NULL; trace(LOG_EVENTS, "event: pid %d stopped, signal %d, event %s", pid, signal, event_name(event)); ctx = context_find_from_pid(pid, 1); if (ctx == NULL) { ctx = find_pending(pid); if (ctx != NULL) { Context * prs = ctx; assert(prs->ref_count == 0); ctx = create_context(pid2id(pid, pid)); EXT(ctx)->pid = pid; EXT(ctx)->regs = (REG_SET *)loc_alloc(sizeof(REG_SET)); ctx->pending_intercept = 1; ctx->mem = prs; ctx->parent = prs; ctx->big_endian = prs->big_endian; prs->ref_count++; list_add_last(&ctx->cldl, &prs->children); link_context(prs); link_context(ctx); send_context_created_event(prs); send_context_created_event(ctx); if (EXT(prs)->attach_callback) { EXT(prs)->attach_callback(0, prs, EXT(prs)->attach_data); EXT(prs)->attach_callback = NULL; EXT(prs)->attach_data = NULL; } } } if (ctx == NULL) return; assert(!ctx->exited); assert(!EXT(ctx)->attach_callback); if (signal != SIGSTOP && signal != SIGTRAP) { assert(signal < 32); ctx->pending_signals |= 1 << signal; if ((ctx->sig_dont_stop & (1 << signal)) == 0) { ctx->pending_intercept = 1; stopped_by_exception = 1; } } if (ctx->stopped) { send_context_changed_event(ctx); } else { ContextAddress pc0 = 0; ContextAddress pc1 = 0; assert(!EXT(ctx)->regs_dirty); EXT(ctx)->end_of_step = 0; EXT(ctx)->ptrace_event = event; ctx->signal = signal; ctx->stopped_by_bp = 0; ctx->stopped_by_exception = stopped_by_exception; ctx->stopped = 1; if (EXT(ctx)->regs_error) { release_error_report(EXT(ctx)->regs_error); EXT(ctx)->regs_error = NULL; } else { pc0 = get_regs_PC(ctx); } if (ptrace(PTRACE_GETREGS, EXT(ctx)->pid, 0, (int)EXT(ctx)->regs) < 0) { assert(errno != 0); if (errno == ESRCH) { /* Racing condition: somebody resumed this context while we are handling stop event. * * One possible cause: main thread has exited forcing children to exit too. * I beleive it is a bug in PTRACE implementation - PTRACE should delay exiting of * a context while it is stopped, but it does not, which causes a nasty racing. * * Workaround: Ignore current event, assume context is running. */ ctx->stopped = 0; return; } EXT(ctx)->regs_error = get_error_report(errno); trace(LOG_ALWAYS, "error: ptrace(PTRACE_GETREGS) failed; id %s, error %d %s", ctx->id, errno, errno_to_str(errno)); } else { pc1 = get_regs_PC(ctx); } trace(LOG_EVENTS, "event: pid %d stopped at PC = %#lx", pid, pc1); if (signal == SIGTRAP && event == 0 && !syscall) { size_t break_size = 0; get_break_instruction(ctx, &break_size); ctx->stopped_by_bp = !EXT(ctx)->regs_error && is_breakpoint_address(ctx, pc1 - break_size); EXT(ctx)->end_of_step = !ctx->stopped_by_bp && EXT(ctx)->pending_step; if (ctx->stopped_by_bp) set_regs_PC(ctx, pc1 - break_size); } EXT(ctx)->pending_step = 0; send_context_stopped_event(ctx); } }