ptrace.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * linux/arch/h8300/kernel/ptrace.c
  3. *
  4. * Yoshinori Sato <ysato@users.sourceforge.jp>
  5. *
  6. * Based on:
  7. * linux/arch/m68k/kernel/ptrace.c
  8. *
  9. * Copyright (C) 1994 by Hamish Macdonald
  10. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  11. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  12. *
  13. * This file is subject to the terms and conditions of the GNU General
  14. * Public License. See the file COPYING in the main directory of
  15. * this archive for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/mm.h>
  20. #include <linux/smp.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/errno.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/user.h>
  25. #include <linux/signal.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/page.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/system.h>
  30. #include <asm/processor.h>
  31. #include <asm/signal.h>
  32. /* cpu depend functions */
  33. extern long h8300_get_reg(struct task_struct *task, int regno);
  34. extern int h8300_put_reg(struct task_struct *task, int regno, unsigned long data);
  35. extern void h8300_disable_trace(struct task_struct *child);
  36. extern void h8300_enable_trace(struct task_struct *child);
  37. /*
  38. * does not yet catch signals sent when the child dies.
  39. * in exit.c or in signal.c.
  40. */
  41. inline
  42. static int read_long(struct task_struct * tsk, unsigned long addr,
  43. unsigned long * result)
  44. {
  45. *result = *(unsigned long *)addr;
  46. return 0;
  47. }
  48. void ptrace_disable(struct task_struct *child)
  49. {
  50. h8300_disable_trace(child);
  51. }
  52. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  53. {
  54. int ret;
  55. switch (request) {
  56. case PTRACE_PEEKTEXT: /* read word at location addr. */
  57. case PTRACE_PEEKDATA: {
  58. unsigned long tmp;
  59. ret = read_long(child, addr, &tmp);
  60. if (ret < 0)
  61. break ;
  62. ret = put_user(tmp, (unsigned long *) data);
  63. break ;
  64. }
  65. /* read the word at location addr in the USER area. */
  66. case PTRACE_PEEKUSR: {
  67. unsigned long tmp = 0;
  68. if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
  69. ret = -EIO;
  70. break ;
  71. }
  72. ret = 0; /* Default return condition */
  73. addr = addr >> 2; /* temporary hack. */
  74. if (addr < H8300_REGS_NO)
  75. tmp = h8300_get_reg(child, addr);
  76. else {
  77. switch(addr) {
  78. case 49:
  79. tmp = child->mm->start_code;
  80. break ;
  81. case 50:
  82. tmp = child->mm->start_data;
  83. break ;
  84. case 51:
  85. tmp = child->mm->end_code;
  86. break ;
  87. case 52:
  88. tmp = child->mm->end_data;
  89. break ;
  90. default:
  91. ret = -EIO;
  92. }
  93. }
  94. if (!ret)
  95. ret = put_user(tmp,(unsigned long *) data);
  96. break ;
  97. }
  98. /* when I and D space are separate, this will have to be fixed. */
  99. case PTRACE_POKETEXT: /* write the word at location addr. */
  100. case PTRACE_POKEDATA:
  101. ret = 0;
  102. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  103. break;
  104. ret = -EIO;
  105. break;
  106. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  107. if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
  108. ret = -EIO;
  109. break ;
  110. }
  111. addr = addr >> 2; /* temporary hack. */
  112. if (addr == PT_ORIG_ER0) {
  113. ret = -EIO;
  114. break ;
  115. }
  116. if (addr < H8300_REGS_NO) {
  117. ret = h8300_put_reg(child, addr, data);
  118. break ;
  119. }
  120. ret = -EIO;
  121. break ;
  122. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  123. case PTRACE_CONT: { /* restart after signal. */
  124. ret = -EIO;
  125. if (!valid_signal(data))
  126. break ;
  127. if (request == PTRACE_SYSCALL)
  128. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  129. else
  130. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  131. child->exit_code = data;
  132. wake_up_process(child);
  133. /* make sure the single step bit is not set. */
  134. h8300_disable_trace(child);
  135. ret = 0;
  136. }
  137. /*
  138. * make the child exit. Best I can do is send it a sigkill.
  139. * perhaps it should be put in the status that it wants to
  140. * exit.
  141. */
  142. case PTRACE_KILL: {
  143. ret = 0;
  144. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  145. break;
  146. child->exit_code = SIGKILL;
  147. h8300_disable_trace(child);
  148. wake_up_process(child);
  149. break;
  150. }
  151. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  152. ret = -EIO;
  153. if (!valid_signal(data))
  154. break;
  155. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  156. child->exit_code = data;
  157. h8300_enable_trace(child);
  158. wake_up_process(child);
  159. ret = 0;
  160. break;
  161. }
  162. case PTRACE_DETACH: /* detach a process that was attached. */
  163. ret = ptrace_detach(child, data);
  164. break;
  165. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  166. int i;
  167. unsigned long tmp;
  168. for (i = 0; i < H8300_REGS_NO; i++) {
  169. tmp = h8300_get_reg(child, i);
  170. if (put_user(tmp, (unsigned long *) data)) {
  171. ret = -EFAULT;
  172. break;
  173. }
  174. data += sizeof(long);
  175. }
  176. ret = 0;
  177. break;
  178. }
  179. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  180. int i;
  181. unsigned long tmp;
  182. for (i = 0; i < H8300_REGS_NO; i++) {
  183. if (get_user(tmp, (unsigned long *) data)) {
  184. ret = -EFAULT;
  185. break;
  186. }
  187. h8300_put_reg(child, i, tmp);
  188. data += sizeof(long);
  189. }
  190. ret = 0;
  191. break;
  192. }
  193. default:
  194. ret = -EIO;
  195. break;
  196. }
  197. return ret;
  198. }
  199. asmlinkage void syscall_trace(void)
  200. {
  201. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  202. return;
  203. if (!(current->ptrace & PT_PTRACED))
  204. return;
  205. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  206. ? 0x80 : 0));
  207. /*
  208. * this isn't the same as continuing with a signal, but it will do
  209. * for normal use. strace only continues with a signal if the
  210. * stopping signal is not SIGTRAP. -brl
  211. */
  212. if (current->exit_code) {
  213. send_sig(current->exit_code, current, 1);
  214. current->exit_code = 0;
  215. }
  216. }