ptrace.c 5.5 KB

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