static void command_set_signal_mask(char * token, Channel * c) { int err = 0; char id[256]; pid_t pid; Context * ctx = NULL; int dont_stop; int dont_pass; json_read_string(&c->inp, id, sizeof(id)); if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX); dont_stop = json_read_long(&c->inp); if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX); dont_pass = json_read_long(&c->inp); if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX); if (read_stream(&c->inp) != MARKER_EOM) exception(ERR_JSON_SYNTAX); pid = id2pid(id, NULL); ctx = context_find_from_pid(pid); if (ctx == NULL) { err = ERR_INV_CONTEXT; } else { ctx->sig_dont_stop = dont_stop; ctx->sig_dont_pass = dont_pass; } write_stringz(&c->out, "R"); write_stringz(&c->out, token); write_errno(&c->out, err); write_stream(&c->out, MARKER_EOM); }
static void command_attach(char * token, Channel * c) { int err = 0; char id[256]; pid_t pid, parent; json_read_string(&c->inp, id, sizeof(id)); if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX); if (read_stream(&c->inp) != MARKER_EOM) exception(ERR_JSON_SYNTAX); pid = id2pid(id, &parent); if (parent != 0) { err = ERR_INV_CONTEXT; } else if (context_find_from_pid(pid) != NULL) { err = ERR_ALREADY_ATTACHED; } else { AttachDoneArgs * data = loc_alloc_zero(sizeof *data); data->c = c; strcpy(data->token, token); if (context_attach(pid, attach_done, data, 0) == 0) { stream_lock(c); return; } err = errno; loc_free(data); } write_stringz(&c->out, "R"); write_stringz(&c->out, token); write_errno(&c->out, err); write_stream(&c->out, MARKER_EOM); }
static void write_context(OutputStream * out, int pid) { Context * ctx = context_find_from_pid(pid); ChildProcess * prs = find_process(pid); write_stream(out, '{'); json_write_string(out, "Name"); write_stream(out, ':'); json_write_string(out, prs ? prs->name : pid2id(pid, 0)); write_stream(out, ','); json_write_string(out, "CanTerminate"); write_stream(out, ':'); json_write_boolean(out, 1); write_stream(out, ','); if (ctx != NULL) { json_write_string(out, "Attached"); write_stream(out, ':'); json_write_boolean(out, 1); write_stream(out, ','); } if (prs != NULL) { if (*prs->inp_id) { json_write_string(out, "StdInID"); write_stream(out, ':'); json_write_string(out, prs->inp_id); write_stream(out, ','); } if (*prs->out_id) { json_write_string(out, "StdOutID"); write_stream(out, ':'); json_write_string(out, prs->out_id); write_stream(out, ','); } if (*prs->err_id) { json_write_string(out, "StdErrID"); write_stream(out, ':'); json_write_string(out, prs->err_id); write_stream(out, ','); } } json_write_string(out, "ID"); write_stream(out, ':'); json_write_string(out, pid2id(pid, 0)); write_stream(out, '}'); }
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 void event_attach_done(void * x) { AttachDoneArgs * args = (AttachDoneArgs *)x; if (context_find_from_pid(args->pid, 0) != NULL) { args->done(ERR_ALREADY_ATTACHED, NULL, args->data); } else { Context * ctx = NULL; if (parent_ctx == NULL) { pid_t pid = taskIdSelf(); parent_ctx = create_context(pid2id(pid, 0)); EXT(parent_ctx)->pid = pid; parent_ctx->mem = parent_ctx; parent_ctx->mem_access |= MEM_ACCESS_INSTRUCTION; parent_ctx->mem_access |= MEM_ACCESS_DATA; parent_ctx->big_endian = big_endian_host(); link_context(parent_ctx); send_context_created_event(parent_ctx); } assert(parent_ctx->ref_count > 0); ctx = create_context(pid2id(args->pid, EXT(parent_ctx)->pid)); EXT(ctx)->pid = args->pid; EXT(ctx)->regs = (REG_SET *)loc_alloc(sizeof(REG_SET)); ctx->mem = parent_ctx; ctx->big_endian = parent_ctx->big_endian; (ctx->parent = parent_ctx)->ref_count++; list_add_last(&ctx->cldl, &parent_ctx->children); link_context(ctx); trace(LOG_CONTEXT, "context: attached: ctx %#lx, id %#x", ctx, EXT(ctx)->pid); send_context_created_event(ctx); args->done(0, ctx, args->data); if (taskIsStopped(args->pid)) { struct event_info * info; ctx->pending_intercept = 1; info = event_info_alloc(EVENT_HOOK_STOP); if (info != NULL) { info->stopped_ctx.ctxId = args->pid; event_info_post(info); } } } loc_free(x); }
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 command_get_children(char * token, Channel * c) { char id[256]; int attached_only; json_read_string(&c->inp, id, sizeof(id)); if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX); attached_only = json_read_boolean(&c->inp); if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX); if (read_stream(&c->inp) != MARKER_EOM) exception(ERR_JSON_SYNTAX); write_stringz(&c->out, "R"); write_stringz(&c->out, token); if (id[0] != 0) { write_errno(&c->out, 0); write_stringz(&c->out, "null"); } else { #if defined(WIN32) DWORD err = 0; HANDLE snapshot; PROCESSENTRY32 pe32; snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (snapshot == INVALID_HANDLE_VALUE) err = set_win32_errno(GetLastError()); memset(&pe32, 0, sizeof(pe32)); pe32.dwSize = sizeof(PROCESSENTRY32); if (!err && !Process32First(snapshot, &pe32)) { err = set_win32_errno(GetLastError()); CloseHandle(snapshot); } write_errno(&c->out, err); if (err) { write_stringz(&c->out, "null"); } else { int cnt = 0; write_stream(&c->out, '['); do { if (!attached_only || context_find_from_pid(pe32.th32ProcessID) != NULL) { if (cnt > 0) write_stream(&c->out, ','); json_write_string(&c->out, pid2id(pe32.th32ProcessID, 0)); cnt++; } } while (Process32Next(snapshot, &pe32)); write_stream(&c->out, ']'); write_stream(&c->out, 0); } if (snapshot != INVALID_HANDLE_VALUE) CloseHandle(snapshot); #elif defined(_WRS_KERNEL) int i = 0; int cnt = 0; int ids_cnt = 0; int ids_max = 500; int * ids = (int *)loc_alloc(ids_max * sizeof(int)); for (;;) { ids_cnt = taskIdListGet(ids, ids_max); if (ids_cnt < ids_max) break; loc_free(ids); ids_max *= 2; ids = (int *)loc_alloc(ids_max * sizeof(int)); } write_errno(&c->out, 0); write_stream(&c->out, '['); for (i = 0; i < ids_cnt; i++) { if (!attached_only || context_find_from_pid(ids[i]) != NULL) { if (cnt > 0) write_stream(&c->out, ','); json_write_string(&c->out, pid2id(ids[i], 0)); cnt++; } } write_stream(&c->out, ']'); write_stream(&c->out, 0); #elif defined(__APPLE__) #else DIR * proc = opendir("/proc"); if (proc == NULL) { write_errno(&c->out, errno); write_stringz(&c->out, "null"); } else { int cnt = 0; write_errno(&c->out, 0); write_stream(&c->out, '['); for (;;) { struct dirent * ent = readdir(proc); if (ent == NULL) break; if (ent->d_name[0] >= '1' && ent->d_name[0] <= '9') { pid_t pid = atol(ent->d_name); if (!attached_only || context_find_from_pid(pid) != NULL) { if (cnt > 0) write_stream(&c->out, ','); json_write_string(&c->out, pid2id(pid, 0)); cnt++; } } } write_stream(&c->out, ']'); write_stream(&c->out, 0); closedir(proc); } #endif } write_stream(&c->out, MARKER_EOM); }
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); } }