ptrace.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (C) 2000-2003, Axis Communications AB.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/sched.h>
  6. #include <linux/mm.h>
  7. #include <linux/smp.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/errno.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/user.h>
  12. #include <linux/signal.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/page.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/system.h>
  17. #include <asm/processor.h>
  18. /*
  19. * Determines which bits in DCCR the user has access to.
  20. * 1 = access, 0 = no access.
  21. */
  22. #define DCCR_MASK 0x0000001f /* XNZVC */
  23. /*
  24. * Get contents of register REGNO in task TASK.
  25. */
  26. inline long get_reg(struct task_struct *task, unsigned int regno)
  27. {
  28. /* USP is a special case, it's not in the pt_regs struct but
  29. * in the tasks thread struct
  30. */
  31. if (regno == PT_USP)
  32. return task->thread.usp;
  33. else if (regno < PT_MAX)
  34. return ((unsigned long *)user_regs(task->thread_info))[regno];
  35. else
  36. return 0;
  37. }
  38. /*
  39. * Write contents of register REGNO in task TASK.
  40. */
  41. inline int put_reg(struct task_struct *task, unsigned int regno,
  42. unsigned long data)
  43. {
  44. if (regno == PT_USP)
  45. task->thread.usp = data;
  46. else if (regno < PT_MAX)
  47. ((unsigned long *)user_regs(task->thread_info))[regno] = data;
  48. else
  49. return -1;
  50. return 0;
  51. }
  52. /*
  53. * Called by kernel/ptrace.c when detaching.
  54. *
  55. * Make sure the single step bit is not set.
  56. */
  57. void
  58. ptrace_disable(struct task_struct *child)
  59. {
  60. /* Todo - pending singlesteps? */
  61. }
  62. /*
  63. * Note that this implementation of ptrace behaves differently from vanilla
  64. * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
  65. * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
  66. * ignored. Instead, the data variable is expected to point at a location
  67. * (in user space) where the result of the ptrace call is written (instead of
  68. * being returned).
  69. */
  70. asmlinkage int
  71. sys_ptrace(long request, long pid, long addr, long data)
  72. {
  73. struct task_struct *child;
  74. int ret;
  75. unsigned long __user *datap = (unsigned long __user *)data;
  76. lock_kernel();
  77. ret = -EPERM;
  78. if (request == PTRACE_TRACEME) {
  79. if (current->ptrace & PT_PTRACED)
  80. goto out;
  81. current->ptrace |= PT_PTRACED;
  82. ret = 0;
  83. goto out;
  84. }
  85. ret = -ESRCH;
  86. read_lock(&tasklist_lock);
  87. child = find_task_by_pid(pid);
  88. if (child)
  89. get_task_struct(child);
  90. read_unlock(&tasklist_lock);
  91. if (!child)
  92. goto out;
  93. ret = -EPERM;
  94. if (pid == 1) /* Leave the init process alone! */
  95. goto out_tsk;
  96. if (request == PTRACE_ATTACH) {
  97. ret = ptrace_attach(child);
  98. goto out_tsk;
  99. }
  100. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  101. if (ret < 0)
  102. goto out_tsk;
  103. switch (request) {
  104. /* Read word at location address. */
  105. case PTRACE_PEEKTEXT:
  106. case PTRACE_PEEKDATA: {
  107. unsigned long tmp;
  108. int copied;
  109. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  110. ret = -EIO;
  111. if (copied != sizeof(tmp))
  112. break;
  113. ret = put_user(tmp,datap);
  114. break;
  115. }
  116. /* Read the word at location address in the USER area. */
  117. case PTRACE_PEEKUSR: {
  118. unsigned long tmp;
  119. ret = -EIO;
  120. if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
  121. break;
  122. tmp = get_reg(child, addr >> 2);
  123. ret = put_user(tmp, datap);
  124. break;
  125. }
  126. /* Write the word at location address. */
  127. case PTRACE_POKETEXT:
  128. case PTRACE_POKEDATA:
  129. ret = 0;
  130. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  131. break;
  132. ret = -EIO;
  133. break;
  134. /* Write the word at location address in the USER area. */
  135. case PTRACE_POKEUSR:
  136. ret = -EIO;
  137. if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
  138. break;
  139. addr >>= 2;
  140. if (addr == PT_DCCR) {
  141. /* don't allow the tracing process to change stuff like
  142. * interrupt enable, kernel/user bit, dma enables etc.
  143. */
  144. data &= DCCR_MASK;
  145. data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
  146. }
  147. if (put_reg(child, addr, data))
  148. break;
  149. ret = 0;
  150. break;
  151. case PTRACE_SYSCALL:
  152. case PTRACE_CONT:
  153. ret = -EIO;
  154. if (!valid_signal(data))
  155. break;
  156. if (request == PTRACE_SYSCALL) {
  157. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  158. }
  159. else {
  160. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  161. }
  162. child->exit_code = data;
  163. /* TODO: make sure any pending breakpoint is killed */
  164. wake_up_process(child);
  165. ret = 0;
  166. break;
  167. /* Make the child exit by sending it a sigkill. */
  168. case PTRACE_KILL:
  169. ret = 0;
  170. if (child->state == TASK_ZOMBIE)
  171. break;
  172. child->exit_code = SIGKILL;
  173. /* TODO: make sure any pending breakpoint is killed */
  174. wake_up_process(child);
  175. break;
  176. /* Set the trap flag. */
  177. case PTRACE_SINGLESTEP:
  178. ret = -EIO;
  179. if (!valid_signal(data))
  180. break;
  181. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  182. /* TODO: set some clever breakpoint mechanism... */
  183. child->exit_code = data;
  184. wake_up_process(child);
  185. ret = 0;
  186. break;
  187. case PTRACE_DETACH:
  188. ret = ptrace_detach(child, data);
  189. break;
  190. /* Get all GP registers from the child. */
  191. case PTRACE_GETREGS: {
  192. int i;
  193. unsigned long tmp;
  194. for (i = 0; i <= PT_MAX; i++) {
  195. tmp = get_reg(child, i);
  196. if (put_user(tmp, datap)) {
  197. ret = -EFAULT;
  198. goto out_tsk;
  199. }
  200. data += sizeof(long);
  201. }
  202. ret = 0;
  203. break;
  204. }
  205. /* Set all GP registers in the child. */
  206. case PTRACE_SETREGS: {
  207. int i;
  208. unsigned long tmp;
  209. for (i = 0; i <= PT_MAX; i++) {
  210. if (get_user(tmp, datap)) {
  211. ret = -EFAULT;
  212. goto out_tsk;
  213. }
  214. if (i == PT_DCCR) {
  215. tmp &= DCCR_MASK;
  216. tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
  217. }
  218. put_reg(child, i, tmp);
  219. data += sizeof(long);
  220. }
  221. ret = 0;
  222. break;
  223. }
  224. default:
  225. ret = ptrace_request(child, request, addr, data);
  226. break;
  227. }
  228. out_tsk:
  229. put_task_struct(child);
  230. out:
  231. unlock_kernel();
  232. return ret;
  233. }
  234. void do_syscall_trace(void)
  235. {
  236. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  237. return;
  238. if (!(current->ptrace & PT_PTRACED))
  239. return;
  240. /* the 0x80 provides a way for the tracing parent to distinguish
  241. between a syscall stop and SIGTRAP delivery */
  242. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  243. ? 0x80 : 0));
  244. /*
  245. * This isn't the same as continuing with a signal, but it will do for
  246. * normal use.
  247. */
  248. if (current->exit_code) {
  249. send_sig(current->exit_code, current, 1);
  250. current->exit_code = 0;
  251. }
  252. }