📄 ptrace.c
字号:
(!cap_issubset(child->cap_permitted, current->cap_permitted)) || (current->gid != child->gid)) && !capable(CAP_SYS_PTRACE)) { pt_error_return(regs, EPERM); goto out; } /* the same process cannot be attached many times */ if (child->ptrace & PT_PTRACED) { pt_error_return(regs, EPERM); goto out; } child->ptrace |= PT_PTRACED; write_lock_irqsave(&tasklist_lock, flags); if(child->p_pptr != current) { REMOVE_LINKS(child); child->p_pptr = current; SET_LINKS(child); } write_unlock_irqrestore(&tasklist_lock, flags); send_sig(SIGSTOP, child, 1); pt_succ_return(regs, 0); goto out; } if (!(child->ptrace & PT_PTRACED)) { pt_error_return(regs, ESRCH); goto out; } if(child->state != TASK_STOPPED) { if(request != PTRACE_KILL) { pt_error_return(regs, ESRCH); goto out; } } if(child->p_pptr != current) { pt_error_return(regs, ESRCH); goto out; } switch(request) { case PTRACE_PEEKTEXT: /* read word at location addr. */ case PTRACE_PEEKDATA: { unsigned long tmp; if (access_process_vm(child, addr, &tmp, sizeof(tmp), 0) == sizeof(tmp)) pt_os_succ_return(regs, tmp, (long *)data); else pt_error_return(regs, EIO); goto out; } case PTRACE_PEEKUSR: read_sunos_user(regs, addr, child, (long *) data); goto out; case PTRACE_POKEUSR: write_sunos_user(regs, addr, child); goto out; case PTRACE_POKETEXT: /* write the word at location addr. */ case PTRACE_POKEDATA: { if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data)) pt_succ_return(regs, 0); else pt_error_return(regs, EIO); goto out; } case PTRACE_GETREGS: { struct pt_regs *pregs = (struct pt_regs *) addr; struct pt_regs *cregs = child->thread.kregs; int rval; rval = verify_area(VERIFY_WRITE, pregs, sizeof(struct pt_regs)); if(rval) { pt_error_return(regs, -rval); goto out; } __put_user(cregs->psr, (&pregs->psr)); __put_user(cregs->pc, (&pregs->pc)); __put_user(cregs->npc, (&pregs->npc)); __put_user(cregs->y, (&pregs->y)); for(rval = 1; rval < 16; rval++) __put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1])); pt_succ_return(regs, 0);#ifdef DEBUG_PTRACE printk ("PC=%x nPC=%x o7=%x\n", cregs->pc, cregs->npc, cregs->u_regs [15]);#endif goto out; } case PTRACE_SETREGS: { struct pt_regs *pregs = (struct pt_regs *) addr; struct pt_regs *cregs = child->thread.kregs; unsigned long psr, pc, npc, y; int i; /* Must be careful, tracing process can only set certain * bits in the psr. */ i = verify_area(VERIFY_READ, pregs, sizeof(struct pt_regs)); if(i) { pt_error_return(regs, -i); goto out; } __get_user(psr, (&pregs->psr)); __get_user(pc, (&pregs->pc)); __get_user(npc, (&pregs->npc)); __get_user(y, (&pregs->y)); psr &= PSR_ICC; cregs->psr &= ~PSR_ICC; cregs->psr |= psr; if(!((pc | npc) & 3)) { cregs->pc = pc; cregs->npc =npc; } cregs->y = y; for(i = 1; i < 16; i++) __get_user(cregs->u_regs[i], (&pregs->u_regs[i-1])); pt_succ_return(regs, 0); goto out; } case PTRACE_GETFPREGS: { struct fps { unsigned long regs[32]; unsigned long fsr; unsigned long flags; unsigned long extra; unsigned long fpqd; struct fq { unsigned long *insnaddr; unsigned long insn; } fpq[16]; } *fps = (struct fps *) addr; int i; i = verify_area(VERIFY_WRITE, fps, sizeof(struct fps)); if(i) { pt_error_return(regs, -i); goto out; } for(i = 0; i < 32; i++) __put_user(child->thread.float_regs[i], (&fps->regs[i])); __put_user(child->thread.fsr, (&fps->fsr)); __put_user(child->thread.fpqdepth, (&fps->fpqd)); __put_user(0, (&fps->flags)); __put_user(0, (&fps->extra)); for(i = 0; i < 16; i++) { __put_user(child->thread.fpqueue[i].insn_addr, (&fps->fpq[i].insnaddr)); __put_user(child->thread.fpqueue[i].insn, (&fps->fpq[i].insn)); } pt_succ_return(regs, 0); goto out; } case PTRACE_SETFPREGS: { struct fps { unsigned long regs[32]; unsigned long fsr; unsigned long flags; unsigned long extra; unsigned long fpqd; struct fq { unsigned long *insnaddr; unsigned long insn; } fpq[16]; } *fps = (struct fps *) addr; int i; i = verify_area(VERIFY_READ, fps, sizeof(struct fps)); if(i) { pt_error_return(regs, -i); goto out; } copy_from_user(&child->thread.float_regs[0], &fps->regs[0], (32 * sizeof(unsigned long))); __get_user(child->thread.fsr, (&fps->fsr)); __get_user(child->thread.fpqdepth, (&fps->fpqd)); for(i = 0; i < 16; i++) { __get_user(child->thread.fpqueue[i].insn_addr, (&fps->fpq[i].insnaddr)); __get_user(child->thread.fpqueue[i].insn, (&fps->fpq[i].insn)); } pt_succ_return(regs, 0); goto out; } case PTRACE_READTEXT: case PTRACE_READDATA: { int res = ptrace_readdata(child, addr, (void *) addr2, data); if (res == data) { pt_succ_return(regs, 0); goto out; } /* Partial read is an IO failure */ if (res >= 0) res = -EIO; pt_error_return(regs, -res); goto out; } case PTRACE_WRITETEXT: case PTRACE_WRITEDATA: { int res = ptrace_writedata(child, (void *) addr2, addr, data); if (res == data) { pt_succ_return(regs, 0); goto out; } /* Partial write is an IO failure */ if (res >= 0) res = -EIO; pt_error_return(regs, -res); goto out; } case PTRACE_SYSCALL: /* continue and stop at (return from) syscall */ addr = 1; case PTRACE_CONT: { /* restart after signal. */ if ((unsigned long) data > _NSIG) { pt_error_return(regs, EIO); goto out; } if (addr != 1) { if (addr & 3) { pt_error_return(regs, EINVAL); goto out; }#ifdef DEBUG_PTRACE printk ("Original: %08lx %08lx\n", child->thread.kregs->pc, child->thread.kregs->npc); printk ("Continuing with %08lx %08lx\n", addr, addr+4);#endif child->thread.kregs->pc = addr; child->thread.kregs->npc = addr + 4; } if (request == PTRACE_SYSCALL) child->ptrace |= PT_TRACESYS; else child->ptrace &= ~PT_TRACESYS; child->exit_code = data;#ifdef DEBUG_PTRACE printk("CONT: %s [%d]: set exit_code = %x %x %x\n", child->comm, child->pid, child->exit_code, child->thread.kregs->pc, child->thread.kregs->npc); #endif wake_up_process(child); pt_succ_return(regs, 0); goto out; }/* * make the child exit. Best I can do is send it a sigkill. * perhaps it should be put in the status that it wants to * exit. */ case PTRACE_KILL: { if (child->state == TASK_ZOMBIE) { /* already dead */ pt_succ_return(regs, 0); goto out; } wake_up_process(child); child->exit_code = SIGKILL; pt_succ_return(regs, 0); goto out; } case PTRACE_SUNDETACH: { /* detach a process that was attached. */ unsigned long flags; if ((unsigned long) data > _NSIG) { pt_error_return(regs, EIO); goto out; } child->ptrace &= ~(PT_PTRACED|PT_TRACESYS); wake_up_process(child); child->exit_code = data; write_lock_irqsave(&tasklist_lock, flags); REMOVE_LINKS(child); child->p_pptr = child->p_opptr; SET_LINKS(child); write_unlock_irqrestore(&tasklist_lock, flags); pt_succ_return(regs, 0); goto out; } /* PTRACE_DUMPCORE unsupported... */ default: pt_error_return(regs, EIO); goto out; }out: unlock_kernel();}asmlinkage void syscall_trace(void){#ifdef DEBUG_PTRACE printk("%s [%d]: syscall_trace\n", current->comm, current->pid);#endif if ((current->ptrace & (PT_PTRACED|PT_TRACESYS)) != (PT_PTRACED|PT_TRACESYS)) return; current->exit_code = SIGTRAP; current->state = TASK_STOPPED; current->thread.flags ^= MAGIC_CONSTANT; notify_parent(current, SIGCHLD); schedule(); /* * this isn't the same as continuing with a signal, but it will do * for normal use. strace only continues with a signal if the * stopping signal is not SIGTRAP. -brl */#ifdef DEBUG_PTRACE printk("%s [%d]: syscall_trace exit= %x\n", current->comm, current->pid, current->exit_code);#endif if (current->exit_code) { send_sig (current->exit_code, current, 1); current->exit_code = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -