static void _stp_stack_user_print(struct context *c, int sym_flags) { struct pt_regs *regs = NULL; int uregs_valid = 0; struct uretprobe_instance *ri = NULL; if (c->probe_type == _STP_PROBE_HANDLER_URETPROBE) ri = c->ips.ri; #ifdef STAPCONF_UPROBE_GET_PC else if (c->probe_type == _STP_PROBE_HANDLER_UPROBE) ri = GET_PC_URETPROBE_NONE; #endif regs = _stp_get_uregs(c); uregs_valid = c->probe_flags & _STP_PROBE_STATE_FULL_UREGS; if (! current->mm || ! regs) { if (sym_flags & _STP_SYM_SYMBOL) _stp_printf("<no user backtrace at %s>\n", c->probe_point); else _stp_print("\n"); return; } /* print the current address */ #ifdef STAPCONF_UPROBE_GET_PC if (c->probe_type == _STP_PROBE_HANDLER_URETPROBE && ri) { if ((sym_flags & _STP_SYM_FULL) == _STP_SYM_FULL) { _stp_print("Returning from: "); /* ... otherwise this dereference fails */ _stp_print_addr(ri->rp->u.vaddr, sym_flags, current); _stp_print("Returning to : "); _stp_print_addr(ri->ret_addr, sym_flags, current); } else _stp_print_addr(ri->ret_addr, sym_flags, current); } else { _stp_print_addr(REG_IP(regs), sym_flags, current); } #else _stp_print_addr(REG_IP(regs), sym_flags, current); #endif /* print rest of stack... */ #ifdef STP_USE_DWARF_UNWINDER if (c->uregs == &c->uwcontext.info.regs) { /* Unwinder needs the reg state, clear uregs ref. */ c->uregs = NULL; c->probe_flags &= ~_STP_PROBE_STATE_FULL_UREGS; } __stp_dwarf_stack_user_print(regs, sym_flags, MAXBACKTRACE, &c->uwcontext, ri, uregs_valid); #else /* User stack traces only supported for arches with dwarf unwinder. */ if (sym_flags & _STP_SYM_SYMBOL) _stp_printf("<no user backtrace support on arch>\n"); else _stp_print("\n"); #endif }
struct parasite_ctl *parasite_prep_ctl(pid_t pid, struct vm_area_list *vma_area_list) { struct parasite_ctl *ctl = NULL; struct vma_area *vma_area; if (!arch_can_dump_task(pid)) goto err; /* * Control block early setup. */ ctl = xzalloc(sizeof(*ctl)); if (!ctl) { pr_err("Parasite control block allocation failed (pid: %d)\n", pid); goto err; } ctl->tsock = -1; if (ptrace(PTRACE_GETREGS, pid, NULL, &ctl->regs_orig)) { pr_err("Can't obtain registers (pid: %d)\n", pid); goto err; } vma_area = get_vma_by_ip(&vma_area_list->h, REG_IP(ctl->regs_orig)); if (!vma_area) { pr_err("No suitable VMA found to run parasite " "bootstrap code (pid: %d)\n", pid); goto err; } ctl->pid.real = pid; ctl->pid.virt = 0; ctl->syscall_ip = vma_area->vma.start; /* * Inject syscall instruction and remember original code, * we will need it to restore original program content. */ memcpy(ctl->code_orig, code_syscall, sizeof(ctl->code_orig)); if (ptrace_swap_area(pid, (void *)ctl->syscall_ip, (void *)ctl->code_orig, sizeof(ctl->code_orig))) { pr_err("Can't inject syscall blob (pid: %d)\n", pid); goto err; } return ctl; err: xfree(ctl); return NULL; }
static bool task_in_parasite(struct parasite_ctl *ctl, user_regs_struct_t *regs) { void *addr = (void *) REG_IP(*regs); return addr >= ctl->remote_map && addr < ctl->remote_map + ctl->map_length; }
static unsigned long _stp_stack_unwind_one_user(struct context *c, unsigned depth) { struct pt_regs *regs = NULL; int uregs_valid = 0; struct uretprobe_instance *ri = NULL; struct unwind_frame_info *info = NULL; int ret; #ifdef STAPCONF_UPROBE_GET_PC unsigned long maybe_pc; #endif if (c->probe_type == stp_probe_type_uretprobe) ri = c->ips.ri; #ifdef STAPCONF_UPROBE_GET_PC else if (c->probe_type == stp_probe_type_uprobe) ri = GET_PC_URETPROBE_NONE; #endif /* XXX: The computation that gives this is cached, so calling * _stp_get_uregs multiple times is okay... probably. */ regs = _stp_get_uregs(c); uregs_valid = c->full_uregs_p; if (! current->mm || ! regs) return 0; // no user backtrace at this probe point if (depth == 0) { /* Start by fetching the current PC. */ dbug_unwind(1, "STARTING user unwind\n"); #ifdef STAPCONF_UPROBE_GET_PC if (c->probe_type == stp_probe_type_uretprobe && ri) { return ri->ret_addr; } else { return REG_IP(regs); } #else return REG_IP(regs); #endif } #ifdef STP_USE_DWARF_UNWINDER info = &c->uwcontext_user.info; dbug_unwind(1, "CONTINUING user unwind to depth %d\n", depth); if (depth == 1) { /* need to clear uregs & set up uwcontext->info */ if (c->uregs == &c->uwcontext_user.info.regs) { dbug_unwind(1, "clearing uregs\n"); /* Unwinder needs the reg state, clear uregs ref. */ c->uregs = NULL; c->full_uregs_p = 0; } arch_unw_init_frame_info(info, regs, 0); } ret = unwind(&c->uwcontext_user, 1); #ifdef STAPCONF_UPROBE_GET_PC maybe_pc = 0; if (ri) { maybe_pc = uprobe_get_pc(ri, UNW_PC(info), UNW_SP(info)); if (!maybe_pc) printk("SYSTEMTAP ERROR: uprobe_get_return returned 0\n"); else UNW_PC(info) = maybe_pc; } #endif dbug_unwind(1, "ret=%d PC=%llx SP=%llx\n", ret, (unsigned long long) UNW_PC(info), (unsigned long long) UNW_SP(info)); /* check if unwind hit an error */ if (ret || ! _stp_valid_pc_addr(UNW_PC(info), current)) { return 0; } return UNW_PC(info); #else /* User stack traces only supported for arches with dwarf unwinder. */ return 0; #endif }
static unsigned long _stp_stack_unwind_one_kernel(struct context *c, unsigned depth) { struct pt_regs *regs = NULL; struct unwind_frame_info *info = NULL; int ret; if (depth == 0) { /* Start by fetching the current PC. */ dbug_unwind(1, "STARTING kernel unwind\n"); if (! c->kregs) { /* Even the current PC is unknown; so we have * absolutely no data at any depth. * * Note that unlike _stp_stack_kernel_print(), * we can't fall back to calling dump_trace() * to obtain the backtrace -- since that * returns a string, which we would have to * tokenize. Callers that want to use the * dump_trace() fallback should call * _stp_stack_kernel_print() and do their own * tokenization of the result. */ #if defined (__i386__) || defined (__x86_64__) arch_unw_init_frame_info(&c->uwcontext_kernel.info, NULL, 0); return UNW_PC(&c->uwcontext_kernel.info); #else return 0; #endif } else if (c->probe_type == stp_probe_type_kretprobe && c->ips.krp.pi) { return (unsigned long)_stp_ret_addr_r(c->ips.krp.pi); } else { return REG_IP(c->kregs); } } #ifdef STP_USE_DWARF_UNWINDER /* Otherwise, use the DWARF unwinder to unwind one step. */ regs = c->kregs; info = &c->uwcontext_kernel.info; dbug_unwind(1, "CONTINUING kernel unwind to depth %d\n", depth); if (depth == 1) { /* First step of actual DWARF unwind; need to clear uregs& set up uwcontext->info. */ if (c->uregs == &c->uwcontext_kernel.info.regs) { dbug_unwind(1, "clearing uregs\n"); /* Unwinder needs the reg state, clear uregs ref. */ c->uregs = NULL; c->full_uregs_p = 0; } arch_unw_init_frame_info(info, regs, 0); } ret = unwind(&c->uwcontext_kernel, 0); dbug_unwind(1, "ret=%d PC=%llx SP=%llx\n", ret, (unsigned long long) UNW_PC(info), (unsigned long long) UNW_SP(info)); /* check if unwind hit an error */ if (ret || ! _stp_valid_pc_addr(UNW_PC(info), NULL)) { return 0; } return UNW_PC(info); #else return 0; #endif }
/** Gets user space registers when available, also sets context * full_uregs_p if appropriate. Should be used instead of accessing * context uregs field directly when (full) uregs are needed from * kernel context. */ static struct pt_regs *_stp_get_uregs(struct context *c) { /* When the probe occurred in user context uregs are always complete. */ if (c->uregs && c->user_mode_p) c->full_uregs_p = 1; else if (c->uregs == NULL) { dbug_unwind(1, "computing uregs\n"); /* First try simple recovery through task_pt_regs, on some platforms that already provides complete uregs. */ c->uregs = _stp_current_pt_regs(); if (c->uregs && _stp_task_pt_regs_valid(current, c->uregs)) c->full_uregs_p = 1; /* Sadly powerpc does support the dwarf unwinder, but doesn't have enough CFI in the kernel to recover fully to user space. */ #if defined(STP_USE_DWARF_UNWINDER) && !defined (__powerpc__) else if (c->uregs != NULL && c->kregs != NULL && !c->user_mode_p) { struct unwind_frame_info *info = &c->uwcontext_kernel.info; int ret = 0; int levels; /* We might be lucky and this probe already ran the kernel unwind to end up in the user regs. */ if (UNW_PC(info) == REG_IP(c->uregs)) { levels = 0; dbug_unwind(1, "feeling lucky, info pc == uregs pc\n"); } else { /* Try to recover the uregs by unwinding from the the kernel probe location. */ levels = MAXBACKTRACE; arch_unw_init_frame_info(info, c->kregs, 0); dbug_unwind(1, "Trying to recover... searching for 0x%llx\n", (unsigned long long) REG_IP(c->uregs)); /* Mark the kernel unwind cache as invalid (uwcache_kernel.depth is no longer consistent with the actual current depth of the unwind). We don't save PCs in the cache at this point because this kernel unwind procedure does not fetch the top level PC, so uwcache_kernel.pc[0] would be left unpopulated. We would have to either fetch the current PC here, or specially represent this state of the cache, something we don't bother with at this stage. XXX: this can create (tolerable amounts of) inefficiency if the probe intersperses user and kernel unwind calls, since the other unwind code can clear uregs, triggering a redundant unwind the next time we need them. */ dbug_unwind(1, "clearing kernel unwind cache\n"); c->uwcache_kernel.state = uwcache_uninitialized; } while (levels > 0 && ret == 0 && UNW_PC(info) != REG_IP(c->uregs)) { levels--; ret = unwind(&c->uwcontext_kernel, 0); dbug_unwind(1, "unwind levels: %d, ret: %d, pc=0x%llx\n", levels, ret, (unsigned long long) UNW_PC(info)); } /* Have we arrived where we think user space currently is? */ if (ret == 0 && UNW_PC(info) == REG_IP(c->uregs)) { /* Note we need to clear this state again when the unwinder has been rerun. See __stp_stack_print invocation below. */ UNW_SP(info) = REG_SP(c->uregs); /* Fix up user stack */ c->uregs = &info->regs; c->full_uregs_p = 1; dbug_unwind(1, "recovered with pc=0x%llx sp=0x%llx\n", (unsigned long long) UNW_PC(info), (unsigned long long) UNW_SP(info)); } else dbug_unwind(1, "failed to recover user reg state\n"); } #endif } return c->uregs; }
static void _stp_stack_kernel_print(struct context *c, int sym_flags) { struct pt_regs *regs = NULL; if (! c->kregs) { /* For the kernel we can use an inexact fallback. When compiled with frame pointers we can do a pretty good guess at the stack value, otherwise let dump_stack guess it (and skip some framework frames). */ #if defined(STAPCONF_KERNEL_STACKTRACE) || defined(STAPCONF_KERNEL_STACKTRACE_NO_BP) unsigned long sp; int skip; #ifdef CONFIG_FRAME_POINTER sp = *(unsigned long *) __builtin_frame_address (0); skip = 1; /* Skip just this frame. */ #else sp = 0; skip = 5; /* yes, that many framework frames. */ #endif _stp_stack_print_fallback(sp, sym_flags, MAXBACKTRACE, skip); #else if (sym_flags & _STP_SYM_SYMBOL) _stp_printf("<no kernel backtrace at %s>\n", c->probe_point); else _stp_print("\n"); #endif return; } else { regs = c->kregs; } /* print the current address */ if (c->probe_type == _STP_PROBE_HANDLER_KRETPROBE && c->ips.krp.pi) { if ((sym_flags & _STP_SYM_FULL) == _STP_SYM_FULL) { _stp_print("Returning from: "); _stp_print_addr((unsigned long)_stp_probe_addr_r(c->ips.krp.pi), sym_flags, NULL); _stp_print("Returning to : "); } _stp_print_addr((unsigned long)_stp_ret_addr_r(c->ips.krp.pi), sym_flags, NULL); } else { _stp_print_addr(REG_IP(regs), sym_flags, NULL); } /* print rest of stack... */ #ifdef STP_USE_DWARF_UNWINDER if (c->uregs == &c->uwcontext.info.regs) { /* Unwinder needs the reg state, clear uregs ref. */ c->uregs = NULL; c->probe_flags &= ~_STP_PROBE_STATE_FULL_UREGS; } __stp_dwarf_stack_kernel_print(regs, sym_flags, MAXBACKTRACE, &c->uwcontext); #else /* Arch specific fallback for kernel backtraces. */ __stp_stack_print(regs, sym_flags, MAXBACKTRACE); #endif }
/** Gets user space registers when available, also sets context probe_flags * _STP_PROBE_STATE_FULL_UREGS if appropriate. Should be used instead of * accessing context uregs field directly when (full) uregs are needed * from kernel context. */ static struct pt_regs *_stp_get_uregs(struct context *c) { /* When the probe occurred in user context uregs are always complete. */ if (c->uregs && c->probe_flags & _STP_PROBE_STATE_USER_MODE) c->probe_flags |= _STP_PROBE_STATE_FULL_UREGS; else if (c->uregs == NULL) { /* First try simple recovery through task_pt_regs, on some platforms that already provides complete uregs. */ c->uregs = _stp_current_pt_regs(); if (c->uregs && _stp_task_pt_regs_valid(current, c->uregs)) c->probe_flags |= _STP_PROBE_STATE_FULL_UREGS; /* Sadly powerpc does support the dwarf unwinder, but doesn't have enough CFI in the kernel to recover fully to user space. */ #if defined(STP_USE_DWARF_UNWINDER) && !defined (__powerpc__) else if (c->uregs != NULL && c->kregs != NULL && ! (c->probe_flags & _STP_PROBE_STATE_USER_MODE)) { struct unwind_frame_info *info = &c->uwcontext.info; int ret = 0; int levels; /* We might be lucky and this probe already ran the kernel unwind to end up in the user regs. */ if (UNW_PC(info) == REG_IP(c->uregs)) { levels = 0; dbug_unwind(1, "feeling lucky, info pc == uregs pc\n"); } else { /* Try to recover the uregs by unwinding from the the kernel probe location. */ levels = MAXBACKTRACE; arch_unw_init_frame_info(info, c->kregs, 0); dbug_unwind(1, "Trying to recover... searching for 0x%lx\n", REG_IP(c->uregs)); } while (levels > 0 && ret == 0 && UNW_PC(info) != REG_IP(c->uregs)) { levels--; ret = unwind(&c->uwcontext, 0); dbug_unwind(1, "unwind levels: %d, ret: %d, pc=0x%lx\n", levels, ret, UNW_PC(info)); } /* Have we arrived where we think user space currently is? */ if (ret == 0 && UNW_PC(info) == REG_IP(c->uregs)) { /* Note we need to clear this state again when the unwinder has been rerun. See __stp_stack_print invocation below. */ UNW_SP(info) = REG_SP(c->uregs); /* Fix up user stack */ c->uregs = &info->regs; c->probe_flags |= _STP_PROBE_STATE_FULL_UREGS; dbug_unwind(1, "recovered with pc=0x%lx sp=0x%lx\n", UNW_PC(info), UNW_SP(info)); } else dbug_unwind(1, "failed to recover user reg state\n"); } #endif } return c->uregs; }