ptrace.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <linux/tracehook.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/ptrace-abi.h>
  11. void user_enable_single_step(struct task_struct *child)
  12. {
  13. child->ptrace |= PT_DTRACE;
  14. child->thread.singlestep_syscall = 0;
  15. #ifdef SUBARCH_SET_SINGLESTEPPING
  16. SUBARCH_SET_SINGLESTEPPING(child, 1);
  17. #endif
  18. }
  19. void user_disable_single_step(struct task_struct *child)
  20. {
  21. child->ptrace &= ~PT_DTRACE;
  22. child->thread.singlestep_syscall = 0;
  23. #ifdef SUBARCH_SET_SINGLESTEPPING
  24. SUBARCH_SET_SINGLESTEPPING(child, 0);
  25. #endif
  26. }
  27. /*
  28. * Called by kernel/ptrace.c when detaching..
  29. */
  30. void ptrace_disable(struct task_struct *child)
  31. {
  32. user_disable_single_step(child);
  33. }
  34. extern int peek_user(struct task_struct * child, long addr, long data);
  35. extern int poke_user(struct task_struct * child, long addr, long data);
  36. long arch_ptrace(struct task_struct *child, long request,
  37. unsigned long addr, unsigned long data)
  38. {
  39. int i, ret;
  40. unsigned long __user *p = (void __user *)data;
  41. void __user *vp = p;
  42. switch (request) {
  43. /* read the word at location addr in the USER area. */
  44. case PTRACE_PEEKUSR:
  45. ret = peek_user(child, addr, data);
  46. break;
  47. /* write the word at location addr in the USER area */
  48. case PTRACE_POKEUSR:
  49. ret = poke_user(child, addr, data);
  50. break;
  51. case PTRACE_SYSEMU:
  52. case PTRACE_SYSEMU_SINGLESTEP:
  53. ret = -EIO;
  54. break;
  55. #ifdef PTRACE_GETREGS
  56. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  57. if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
  58. ret = -EIO;
  59. break;
  60. }
  61. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  62. __put_user(getreg(child, i), p);
  63. p++;
  64. }
  65. ret = 0;
  66. break;
  67. }
  68. #endif
  69. #ifdef PTRACE_SETREGS
  70. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  71. unsigned long tmp = 0;
  72. if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
  73. ret = -EIO;
  74. break;
  75. }
  76. for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
  77. __get_user(tmp, p);
  78. putreg(child, i, tmp);
  79. p++;
  80. }
  81. ret = 0;
  82. break;
  83. }
  84. #endif
  85. case PTRACE_GET_THREAD_AREA:
  86. ret = ptrace_get_thread_area(child, addr, vp);
  87. break;
  88. case PTRACE_SET_THREAD_AREA:
  89. ret = ptrace_set_thread_area(child, addr, vp);
  90. break;
  91. default:
  92. ret = ptrace_request(child, request, addr, data);
  93. if (ret == -EIO)
  94. ret = subarch_ptrace(child, request, addr, data);
  95. break;
  96. }
  97. return ret;
  98. }
  99. static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
  100. int error_code)
  101. {
  102. struct siginfo info;
  103. memset(&info, 0, sizeof(info));
  104. info.si_signo = SIGTRAP;
  105. info.si_code = TRAP_BRKPT;
  106. /* User-mode eip? */
  107. info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
  108. /* Send us the fake SIGTRAP */
  109. force_sig_info(SIGTRAP, &info, tsk);
  110. }
  111. /*
  112. * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
  113. * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
  114. */
  115. int syscall_trace_enter(struct pt_regs *regs)
  116. {
  117. audit_syscall_entry(UPT_SYSCALL_NR(&regs->regs),
  118. UPT_SYSCALL_ARG1(&regs->regs),
  119. UPT_SYSCALL_ARG2(&regs->regs),
  120. UPT_SYSCALL_ARG3(&regs->regs),
  121. UPT_SYSCALL_ARG4(&regs->regs));
  122. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  123. return 0;
  124. return tracehook_report_syscall_entry(regs);
  125. }
  126. void syscall_trace_leave(struct pt_regs *regs)
  127. {
  128. int ptraced = current->ptrace;
  129. audit_syscall_exit(regs);
  130. /* Fake a debug trap */
  131. if (ptraced & PT_DTRACE)
  132. send_sigtrap(current, &regs->regs, 0);
  133. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  134. return;
  135. tracehook_report_syscall_exit(regs, 0);
  136. /* force do_signal() --> is_syscall() */
  137. if (ptraced & PT_PTRACED)
  138. set_thread_flag(TIF_SIGPENDING);
  139. }