ptrace.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* ptrace.c */
  2. /* By Ross Biro 1/23/92 */
  3. /*
  4.  * Pentium III FXSR, SSE support
  5.  * Gareth Hughes <gareth@valinux.com>, May 2000
  6.  * 
  7.  * x86-64 port 2000-2002 Andi Kleen
  8.  */
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/errno.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/user.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/system.h>
  20. #include <asm/processor.h>
  21. #include <asm/i387.h>
  22. #include <asm/debugreg.h>
  23. /*
  24.  * does not yet catch signals sent when the child dies.
  25.  * in exit.c or in signal.c.
  26.  */
  27. /* determines which flags the user has access to. */
  28. /* 1 = access 0 = no access */
  29. #define FLAG_MASK 0x44dd5UL
  30. /* set's the trap flag. */
  31. #define TRAP_FLAG 0x100UL
  32. /*
  33.  * eflags and offset of eflags on child stack..
  34.  */
  35. #define EFLAGS offsetof(struct pt_regs, eflags)
  36. #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
  37. /*
  38.  * this routine will get a word off of the processes privileged stack. 
  39.  * the offset is how far from the base addr as stored in the TSS.  
  40.  * this routine assumes that all the privileged stacks are in our
  41.  * data space.
  42.  */   
  43. static inline unsigned long get_stack_long(struct task_struct *task, int offset)
  44. {
  45. unsigned char *stack;
  46. stack = (unsigned char *)task->thread.rsp0;
  47. stack += offset;
  48. return (*((unsigned long *)stack));
  49. }
  50. /*
  51.  * this routine will put a word on the processes privileged stack. 
  52.  * the offset is how far from the base addr as stored in the TSS.  
  53.  * this routine assumes that all the privileged stacks are in our
  54.  * data space.
  55.  */
  56. static inline long put_stack_long(struct task_struct *task, int offset,
  57. unsigned long data)
  58. {
  59. unsigned char * stack;
  60. stack = (unsigned char *) task->thread.rsp0;
  61. stack += offset;
  62. *(unsigned long *) stack = data;
  63. return 0;
  64. }
  65. /*
  66.  * Called by kernel/ptrace.c when detaching..
  67.  *
  68.  * Make sure the single step bit is not set.
  69.  */
  70. void ptrace_disable(struct task_struct *child)
  71. long tmp;
  72. tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
  73. put_stack_long(child, EFL_OFFSET, tmp);
  74. }
  75. static int putreg(struct task_struct *child,
  76. unsigned long regno, unsigned long value)
  77. {
  78. unsigned long tmp; 
  79. switch (regno) {
  80. case offsetof(struct user_regs_struct,fs):
  81. if (value && (value & 3) != 3)
  82. return -EIO;
  83. child->thread.fsindex = value & 0xffff; 
  84. return 0; 
  85. case offsetof(struct user_regs_struct,gs):
  86. if (value && (value & 3) != 3)
  87. return -EIO;
  88. child->thread.gsindex = value & 0xffff;
  89. return 0;
  90. case offsetof(struct user_regs_struct,ds):
  91. if (value && (value & 3) != 3)
  92. return -EIO;
  93. child->thread.ds = value & 0xffff;
  94. return 0;
  95. case offsetof(struct user_regs_struct,es): 
  96. if (value && (value & 3) != 3)
  97. return -EIO;
  98. child->thread.es = value & 0xffff;
  99. return 0;
  100. case offsetof(struct user_regs_struct,fs_base):
  101. if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
  102. return -EIO; 
  103. child->thread.fs = value;
  104. return 0;
  105. case offsetof(struct user_regs_struct,gs_base):
  106. if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
  107. return -EIO; 
  108. child->thread.gs = value;
  109. return 0;
  110. case offsetof(struct user_regs_struct, eflags):
  111. value &= FLAG_MASK;
  112. tmp = get_stack_long(child, EFL_OFFSET); 
  113. tmp &= ~FLAG_MASK; 
  114. value |= tmp;
  115. break;
  116. case offsetof(struct user_regs_struct,cs): 
  117. if ((value & 3) != 3)
  118. return -EIO;
  119. value &= 0xffff;
  120. break;
  121. case offsetof(struct user_regs_struct,ss):
  122. if ((value & 3) != 3)
  123. return -EIO;
  124. value &= 0xffff;
  125.             break;
  126. }      
  127. put_stack_long(child, regno - sizeof(struct pt_regs), value);
  128. return 0;
  129. }
  130. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  131. {
  132. switch (regno) {
  133. case offsetof(struct user_regs_struct, fs):
  134. return child->thread.fsindex;
  135. case offsetof(struct user_regs_struct, gs):
  136. return child->thread.gsindex;
  137. case offsetof(struct user_regs_struct, ds):
  138. return child->thread.ds;
  139. case offsetof(struct user_regs_struct, es):
  140. return child->thread.es; 
  141. case offsetof(struct user_regs_struct, fs_base):
  142. return child->thread.fs;
  143. case offsetof(struct user_regs_struct, gs_base):
  144. return child->thread.gs;
  145. default:
  146. regno = regno - sizeof(struct pt_regs);
  147. return get_stack_long(child, regno);
  148. }
  149. }
  150. asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
  151. {
  152. struct task_struct *child;
  153. struct user * dummy = NULL;
  154. long i, ret;
  155. /* This lock_kernel fixes a subtle race with suid exec */
  156. lock_kernel();
  157. ret = -EPERM;
  158. if (request == PTRACE_TRACEME) {
  159. /* are we already being traced? */
  160. if (current->ptrace & PT_PTRACED)
  161. goto out;
  162. /* set the ptrace bit in the process flags. */
  163. current->ptrace |= PT_PTRACED;
  164. ret = 0;
  165. goto out;
  166. }
  167. ret = -ESRCH;
  168. read_lock(&tasklist_lock);
  169. child = find_task_by_pid(pid);
  170. if (child)
  171. get_task_struct(child);
  172. read_unlock(&tasklist_lock);
  173. if (!child)
  174. goto out;
  175. ret = -EPERM;
  176. if (pid == 1) /* you may not mess with init */
  177. goto out_tsk;
  178. if (request == PTRACE_ATTACH) {
  179. ret = ptrace_attach(child);
  180. goto out_tsk;
  181. }
  182. ret = -ESRCH;
  183. if (!(child->ptrace & PT_PTRACED))
  184. goto out_tsk;
  185. if (child->state != TASK_STOPPED) {
  186. if (request != PTRACE_KILL)
  187. goto out_tsk;
  188. }
  189. if (child->p_pptr != current)
  190. goto out_tsk;
  191. switch (request) {
  192. /* when I and D space are separate, these will need to be fixed. */
  193. case PTRACE_PEEKTEXT: /* read word at location addr. */ 
  194. case PTRACE_PEEKDATA: {
  195. unsigned long tmp;
  196. int copied;
  197. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  198. ret = -EIO;
  199. if (copied != sizeof(tmp))
  200. break;
  201. ret = put_user(tmp,(unsigned long *) data);
  202. break;
  203. }
  204. /* read the word at location addr in the USER area. */
  205. case PTRACE_PEEKUSR: {
  206. unsigned long tmp;
  207. ret = -EIO;
  208. if ((addr & 3) || addr < 0 || 
  209.     addr > sizeof(struct user) - 3)
  210. break;
  211. tmp = 0;  /* Default return condition */
  212. if(addr < sizeof(struct user_regs_struct))
  213. tmp = getreg(child, addr);
  214. if(addr >= (long) &dummy->u_debugreg[0] &&
  215.    addr <= (long) &dummy->u_debugreg[7]){
  216. addr -= (long) &dummy->u_debugreg[0];
  217. addr = addr >> 2;
  218. tmp = child->thread.debugreg[addr];
  219. }
  220. ret = put_user(tmp,(unsigned long *) data);
  221. break;
  222. }
  223. /* when I and D space are separate, this will have to be fixed. */
  224. case PTRACE_POKETEXT: /* write the word at location addr. */
  225. case PTRACE_POKEDATA:
  226. ret = 0;
  227. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  228. break;
  229. ret = -EIO;
  230. break;
  231. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  232. ret = -EIO;
  233. if ((addr & 3) || addr < 0 || 
  234.     addr > sizeof(struct user) - 3)
  235. break;
  236. if (addr < sizeof(struct user_regs_struct)) {
  237. ret = putreg(child, addr, data);
  238. break;
  239. }
  240. /* We need to be very careful here.  We implicitly
  241.    want to modify a portion of the task_struct, and we
  242.    have to be selective about what portions we allow someone
  243.    to modify. */
  244.   ret = -EIO;
  245.   if(addr >= (long) &dummy->u_debugreg[0] &&
  246.      addr <= (long) &dummy->u_debugreg[7]){
  247.   if(addr == (long) &dummy->u_debugreg[4]) break;
  248.   if(addr == (long) &dummy->u_debugreg[5]) break;
  249.   if(addr < (long) &dummy->u_debugreg[4] &&
  250.      ((unsigned long) data) >= TASK_SIZE-3) break;
  251.   
  252.   if(addr == (long) &dummy->u_debugreg[7]) {
  253.   data &= ~DR_CONTROL_RESERVED;
  254.   for(i=0; i<4; i++)
  255.   if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  256.   goto out_tsk;
  257.   }
  258.   addr -= (long) &dummy->u_debugreg;
  259.   addr = addr >> 2;
  260.   child->thread.debugreg[addr] = data;
  261.   ret = 0;
  262.   }
  263.   break;
  264. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  265. case PTRACE_CONT: { /* restart after signal. */
  266. long tmp;
  267. ret = -EIO;
  268. if ((unsigned long) data > _NSIG)
  269. break;
  270. if (request == PTRACE_SYSCALL)
  271. child->ptrace |= PT_TRACESYS;
  272. else
  273. child->ptrace &= ~PT_TRACESYS;
  274. child->exit_code = data;
  275. /* make sure the single step bit is not set. */
  276. tmp = get_stack_long(child, EFL_OFFSET);
  277. tmp &= ~TRAP_FLAG;
  278. put_stack_long(child, EFL_OFFSET,tmp);
  279. wake_up_process(child);
  280. ret = 0;
  281. break;
  282. }
  283. /*
  284.  * make the child exit.  Best I can do is send it a sigkill. 
  285.  * perhaps it should be put in the status that it wants to 
  286.  * exit.
  287.  */
  288. case PTRACE_KILL: {
  289. long tmp;
  290. ret = 0;
  291. if (child->state == TASK_ZOMBIE) /* already dead */
  292. break;
  293. child->exit_code = SIGKILL;
  294. /* make sure the single step bit is not set. */
  295. tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
  296. put_stack_long(child, EFL_OFFSET, tmp);
  297. wake_up_process(child);
  298. break;
  299. }
  300. case PTRACE_SINGLESTEP: {  /* set the trap flag. */
  301. long tmp;
  302. ret = -EIO;
  303. if ((unsigned long) data > _NSIG)
  304. break;
  305. child->ptrace &= ~PT_TRACESYS;
  306. if ((child->ptrace & PT_DTRACE) == 0) {
  307. /* Spurious delayed TF traps may occur */
  308. child->ptrace |= PT_DTRACE;
  309. }
  310. tmp = get_stack_long(child, EFL_OFFSET) | TRAP_FLAG;
  311. put_stack_long(child, EFL_OFFSET, tmp);
  312. child->exit_code = data;
  313. /* give it a chance to run. */
  314. wake_up_process(child);
  315. ret = 0;
  316. break;
  317. }
  318. case PTRACE_DETACH:
  319. /* detach a process that was attached. */
  320. ret = ptrace_detach(child, data);
  321. break;
  322. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  323.    if (!access_ok(VERIFY_WRITE, (unsigned *)data, FRAME_SIZE)) {
  324. ret = -EIO;
  325. break;
  326. }
  327. for ( i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long) ) {
  328. __put_user(getreg(child, i),(unsigned long *) data);
  329. data += sizeof(long);
  330. }
  331. ret = 0;
  332. break;
  333. }
  334. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  335. unsigned long tmp;
  336.    if (!access_ok(VERIFY_READ, (unsigned *)data, FRAME_SIZE)) {
  337. ret = -EIO;
  338. break;
  339. }
  340. for ( i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long) ) {
  341. __get_user(tmp, (unsigned long *) data);
  342. putreg(child, i, tmp);
  343. data += sizeof(long);
  344. }
  345. ret = 0;
  346. break;
  347. }
  348. case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
  349. if (!access_ok(VERIFY_WRITE, (unsigned *)data,
  350.        sizeof(struct user_i387_struct))) {
  351. ret = -EIO;
  352. break;
  353. }
  354. ret = get_fpregs((struct user_i387_struct *)data, child);
  355. break;
  356. }
  357. case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
  358. if (!access_ok(VERIFY_READ, (unsigned *)data,
  359.        sizeof(struct user_i387_struct))) {
  360. ret = -EIO;
  361. break;
  362. }
  363. child->used_math = 1;
  364. ret = set_fpregs(child, (struct user_i387_struct *)data);
  365. break;
  366. }
  367. case PTRACE_SETOPTIONS: {
  368. if (data & PTRACE_O_TRACESYSGOOD)
  369. child->ptrace |= PT_TRACESYSGOOD;
  370. else
  371. child->ptrace &= ~PT_TRACESYSGOOD;
  372. ret = 0;
  373. break;
  374. }
  375. default:
  376. ret = -EIO;
  377. break;
  378. }
  379. out_tsk:
  380. free_task_struct(child);
  381. out:
  382. unlock_kernel();
  383. return ret;
  384. }
  385. asmlinkage void syscall_trace(struct pt_regs *regs)
  386. {
  387. if ((current->ptrace & (PT_PTRACED|PT_TRACESYS)) !=
  388. (PT_PTRACED|PT_TRACESYS))
  389. return;
  390. current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  391. ? 0x80 : 0);
  392. current->state = TASK_STOPPED;
  393. notify_parent(current, SIGCHLD);
  394. schedule();
  395. /*
  396.  * this isn't the same as continuing with a signal, but it will do
  397.  * for normal use.  strace only continues with a signal if the
  398.  * stopping signal is not SIGTRAP.  -brl
  399.  */
  400. if (current->exit_code) {
  401. send_sig(current->exit_code, current, 1);
  402. current->exit_code = 0;
  403. }
  404. }