ptrace.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/audit.h"
  6. #include "linux/ptrace.h"
  7. #include "linux/sched.h"
  8. #include "asm/uaccess.h"
  9. #include "skas_ptrace.h"
  10. void user_enable_single_step(struct task_struct *child)
  11. {
  12. child->ptrace |= PT_DTRACE;
  13. child->thread.singlestep_syscall = 0;
  14. #ifdef SUBARCH_SET_SINGLESTEPPING
  15. SUBARCH_SET_SINGLESTEPPING(child, 1);
  16. #endif
  17. }
  18. void user_disable_single_step(struct task_struct *child)
  19. {
  20. child->ptrace &= ~PT_DTRACE;
  21. child->thread.singlestep_syscall = 0;
  22. #ifdef SUBARCH_SET_SINGLESTEPPING
  23. SUBARCH_SET_SINGLESTEPPING(child, 0);
  24. #endif
  25. }
  26. /*
  27. * Called by kernel/ptrace.c when detaching..
  28. */
  29. void ptrace_disable(struct task_struct *child)
  30. {
  31. user_disable_single_step(child);
  32. }
  33. extern int peek_user(struct task_struct * child, long addr, long data);
  34. extern int poke_user(struct task_struct * child, long addr, long data);
  35. long arch_ptrace(struct task_struct *child, long request,
  36. unsigned long addr, unsigned long data)
  37. {
  38. int i, ret;
  39. unsigned long __user *p = (void __user *)data;
  40. void __user *vp = p;
  41. switch (request) {
  42. /* read the word at location addr in the USER area. */
  43. case PTRACE_PEEKUSR:
  44. ret = peek_user(child, addr, data);
  45. break;
  46. /* write the word at location addr in the USER area */
  47. case PTRACE_POKEUSR:
  48. ret = poke_user(child, addr, data);
  49. break;
  50. case PTRACE_SYSEMU:
  51. case PTRACE_SYSEMU_SINGLESTEP:
  52. ret = -EIO;
  53. break;
  54. #ifdef PTRACE_GETREGS
  55. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  56. if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
  57. ret = -EIO;
  58. break;
  59. }
  60. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  61. __put_user(getreg(child, i), p);
  62. p++;
  63. }
  64. ret = 0;
  65. break;
  66. }
  67. #endif
  68. #ifdef PTRACE_SETREGS
  69. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  70. unsigned long tmp = 0;
  71. if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
  72. ret = -EIO;
  73. break;
  74. }
  75. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  76. __get_user(tmp, p);
  77. putreg(child, i, tmp);
  78. p++;
  79. }
  80. ret = 0;
  81. break;
  82. }
  83. #endif
  84. case PTRACE_GET_THREAD_AREA:
  85. ret = ptrace_get_thread_area(child, addr, vp);
  86. break;
  87. case PTRACE_SET_THREAD_AREA:
  88. ret = ptrace_set_thread_area(child, addr, vp);
  89. break;
  90. case PTRACE_FAULTINFO: {
  91. /*
  92. * Take the info from thread->arch->faultinfo,
  93. * but transfer max. sizeof(struct ptrace_faultinfo).
  94. * On i386, ptrace_faultinfo is smaller!
  95. */
  96. ret = copy_to_user(p, &child->thread.arch.faultinfo,
  97. sizeof(struct ptrace_faultinfo)) ?
  98. -EIO : 0;
  99. break;
  100. }
  101. #ifdef PTRACE_LDT
  102. case PTRACE_LDT: {
  103. struct ptrace_ldt ldt;
  104. if (copy_from_user(&ldt, p, sizeof(ldt))) {
  105. ret = -EIO;
  106. break;
  107. }
  108. /*
  109. * This one is confusing, so just punt and return -EIO for
  110. * now
  111. */
  112. ret = -EIO;
  113. break;
  114. }
  115. #endif
  116. default:
  117. ret = ptrace_request(child, request, addr, data);
  118. if (ret == -EIO)
  119. ret = subarch_ptrace(child, request, addr, data);
  120. break;
  121. }
  122. return ret;
  123. }
  124. static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
  125. int error_code)
  126. {
  127. struct siginfo info;
  128. memset(&info, 0, sizeof(info));
  129. info.si_signo = SIGTRAP;
  130. info.si_code = TRAP_BRKPT;
  131. /* User-mode eip? */
  132. info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
  133. /* Send us the fake SIGTRAP */
  134. force_sig_info(SIGTRAP, &info, tsk);
  135. }
  136. /*
  137. * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
  138. * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
  139. */
  140. void syscall_trace(struct uml_pt_regs *regs, int entryexit)
  141. {
  142. int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
  143. int tracesysgood;
  144. if (!entryexit)
  145. audit_syscall_entry(HOST_AUDIT_ARCH,
  146. UPT_SYSCALL_NR(regs),
  147. UPT_SYSCALL_ARG1(regs),
  148. UPT_SYSCALL_ARG2(regs),
  149. UPT_SYSCALL_ARG3(regs),
  150. UPT_SYSCALL_ARG4(regs));
  151. else
  152. audit_syscall_exit(regs);
  153. /* Fake a debug trap */
  154. if (is_singlestep)
  155. send_sigtrap(current, regs, 0);
  156. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  157. return;
  158. if (!(current->ptrace & PT_PTRACED))
  159. return;
  160. /*
  161. * the 0x80 provides a way for the tracing parent to distinguish
  162. * between a syscall stop and SIGTRAP delivery
  163. */
  164. tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
  165. ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
  166. if (entryexit) /* force do_signal() --> is_syscall() */
  167. set_thread_flag(TIF_SIGPENDING);
  168. /*
  169. * this isn't the same as continuing with a signal, but it will do
  170. * for normal use. strace only continues with a signal if the
  171. * stopping signal is not SIGTRAP. -brl
  172. */
  173. if (current->exit_code) {
  174. send_sig(current->exit_code, current, 1);
  175. current->exit_code = 0;
  176. }
  177. }