ptrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* ptrace.c */
  2. /* By Ross Biro 1/23/92 */
  3. /* edited by Linus Torvalds */
  4. /* mangled further by Bob Manson (manson@santafe.edu) */
  5. /* more mutilation by David Mosberger (davidm@azstarnet.com) */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/smp.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/errno.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/user.h>
  14. #include <linux/slab.h>
  15. #include <linux/security.h>
  16. #include <linux/signal.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/system.h>
  20. #include <asm/fpu.h>
  21. #include "proto.h"
  22. #define DEBUG DBG_MEM
  23. #undef DEBUG
  24. #ifdef DEBUG
  25. enum {
  26. DBG_MEM = (1<<0),
  27. DBG_BPT = (1<<1),
  28. DBG_MEM_ALL = (1<<2)
  29. };
  30. #define DBG(fac,args) {if ((fac) & DEBUG) printk args;}
  31. #else
  32. #define DBG(fac,args)
  33. #endif
  34. #define BREAKINST 0x00000080 /* call_pal bpt */
  35. /*
  36. * does not yet catch signals sent when the child dies.
  37. * in exit.c or in signal.c.
  38. */
  39. /*
  40. * Processes always block with the following stack-layout:
  41. *
  42. * +================================+ <---- task + 2*PAGE_SIZE
  43. * | PALcode saved frame (ps, pc, | ^
  44. * | gp, a0, a1, a2) | |
  45. * +================================+ | struct pt_regs
  46. * | | |
  47. * | frame generated by SAVE_ALL | |
  48. * | | v
  49. * +================================+
  50. * | | ^
  51. * | frame saved by do_switch_stack | | struct switch_stack
  52. * | | v
  53. * +================================+
  54. */
  55. /*
  56. * The following table maps a register index into the stack offset at
  57. * which the register is saved. Register indices are 0-31 for integer
  58. * regs, 32-63 for fp regs, and 64 for the pc. Notice that sp and
  59. * zero have no stack-slot and need to be treated specially (see
  60. * get_reg/put_reg below).
  61. */
  62. enum {
  63. REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
  64. };
  65. static int regoff[] = {
  66. PT_REG( r0), PT_REG( r1), PT_REG( r2), PT_REG( r3),
  67. PT_REG( r4), PT_REG( r5), PT_REG( r6), PT_REG( r7),
  68. PT_REG( r8), SW_REG( r9), SW_REG( r10), SW_REG( r11),
  69. SW_REG( r12), SW_REG( r13), SW_REG( r14), SW_REG( r15),
  70. PT_REG( r16), PT_REG( r17), PT_REG( r18), PT_REG( r19),
  71. PT_REG( r20), PT_REG( r21), PT_REG( r22), PT_REG( r23),
  72. PT_REG( r24), PT_REG( r25), PT_REG( r26), PT_REG( r27),
  73. PT_REG( r28), PT_REG( gp), -1, -1,
  74. SW_REG(fp[ 0]), SW_REG(fp[ 1]), SW_REG(fp[ 2]), SW_REG(fp[ 3]),
  75. SW_REG(fp[ 4]), SW_REG(fp[ 5]), SW_REG(fp[ 6]), SW_REG(fp[ 7]),
  76. SW_REG(fp[ 8]), SW_REG(fp[ 9]), SW_REG(fp[10]), SW_REG(fp[11]),
  77. SW_REG(fp[12]), SW_REG(fp[13]), SW_REG(fp[14]), SW_REG(fp[15]),
  78. SW_REG(fp[16]), SW_REG(fp[17]), SW_REG(fp[18]), SW_REG(fp[19]),
  79. SW_REG(fp[20]), SW_REG(fp[21]), SW_REG(fp[22]), SW_REG(fp[23]),
  80. SW_REG(fp[24]), SW_REG(fp[25]), SW_REG(fp[26]), SW_REG(fp[27]),
  81. SW_REG(fp[28]), SW_REG(fp[29]), SW_REG(fp[30]), SW_REG(fp[31]),
  82. PT_REG( pc)
  83. };
  84. static unsigned long zero;
  85. /*
  86. * Get address of register REGNO in task TASK.
  87. */
  88. static unsigned long *
  89. get_reg_addr(struct task_struct * task, unsigned long regno)
  90. {
  91. unsigned long *addr;
  92. if (regno == 30) {
  93. addr = &task->thread_info->pcb.usp;
  94. } else if (regno == 65) {
  95. addr = &task->thread_info->pcb.unique;
  96. } else if (regno == 31 || regno > 65) {
  97. zero = 0;
  98. addr = &zero;
  99. } else {
  100. addr = (void *)task->thread_info + regoff[regno];
  101. }
  102. return addr;
  103. }
  104. /*
  105. * Get contents of register REGNO in task TASK.
  106. */
  107. static unsigned long
  108. get_reg(struct task_struct * task, unsigned long regno)
  109. {
  110. /* Special hack for fpcr -- combine hardware and software bits. */
  111. if (regno == 63) {
  112. unsigned long fpcr = *get_reg_addr(task, regno);
  113. unsigned long swcr
  114. = task->thread_info->ieee_state & IEEE_SW_MASK;
  115. swcr = swcr_update_status(swcr, fpcr);
  116. return fpcr | swcr;
  117. }
  118. return *get_reg_addr(task, regno);
  119. }
  120. /*
  121. * Write contents of register REGNO in task TASK.
  122. */
  123. static int
  124. put_reg(struct task_struct *task, unsigned long regno, unsigned long data)
  125. {
  126. if (regno == 63) {
  127. task->thread_info->ieee_state
  128. = ((task->thread_info->ieee_state & ~IEEE_SW_MASK)
  129. | (data & IEEE_SW_MASK));
  130. data = (data & FPCR_DYN_MASK) | ieee_swcr_to_fpcr(data);
  131. }
  132. *get_reg_addr(task, regno) = data;
  133. return 0;
  134. }
  135. static inline int
  136. read_int(struct task_struct *task, unsigned long addr, int * data)
  137. {
  138. int copied = access_process_vm(task, addr, data, sizeof(int), 0);
  139. return (copied == sizeof(int)) ? 0 : -EIO;
  140. }
  141. static inline int
  142. write_int(struct task_struct *task, unsigned long addr, int data)
  143. {
  144. int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
  145. return (copied == sizeof(int)) ? 0 : -EIO;
  146. }
  147. /*
  148. * Set breakpoint.
  149. */
  150. int
  151. ptrace_set_bpt(struct task_struct * child)
  152. {
  153. int displ, i, res, reg_b, nsaved = 0;
  154. unsigned int insn, op_code;
  155. unsigned long pc;
  156. pc = get_reg(child, REG_PC);
  157. res = read_int(child, pc, (int *) &insn);
  158. if (res < 0)
  159. return res;
  160. op_code = insn >> 26;
  161. if (op_code >= 0x30) {
  162. /*
  163. * It's a branch: instead of trying to figure out
  164. * whether the branch will be taken or not, we'll put
  165. * a breakpoint at either location. This is simpler,
  166. * more reliable, and probably not a whole lot slower
  167. * than the alternative approach of emulating the
  168. * branch (emulation can be tricky for fp branches).
  169. */
  170. displ = ((s32)(insn << 11)) >> 9;
  171. child->thread_info->bpt_addr[nsaved++] = pc + 4;
  172. if (displ) /* guard against unoptimized code */
  173. child->thread_info->bpt_addr[nsaved++]
  174. = pc + 4 + displ;
  175. DBG(DBG_BPT, ("execing branch\n"));
  176. } else if (op_code == 0x1a) {
  177. reg_b = (insn >> 16) & 0x1f;
  178. child->thread_info->bpt_addr[nsaved++] = get_reg(child, reg_b);
  179. DBG(DBG_BPT, ("execing jump\n"));
  180. } else {
  181. child->thread_info->bpt_addr[nsaved++] = pc + 4;
  182. DBG(DBG_BPT, ("execing normal insn\n"));
  183. }
  184. /* install breakpoints: */
  185. for (i = 0; i < nsaved; ++i) {
  186. res = read_int(child, child->thread_info->bpt_addr[i],
  187. (int *) &insn);
  188. if (res < 0)
  189. return res;
  190. child->thread_info->bpt_insn[i] = insn;
  191. DBG(DBG_BPT, (" -> next_pc=%lx\n",
  192. child->thread_info->bpt_addr[i]));
  193. res = write_int(child, child->thread_info->bpt_addr[i],
  194. BREAKINST);
  195. if (res < 0)
  196. return res;
  197. }
  198. child->thread_info->bpt_nsaved = nsaved;
  199. return 0;
  200. }
  201. /*
  202. * Ensure no single-step breakpoint is pending. Returns non-zero
  203. * value if child was being single-stepped.
  204. */
  205. int
  206. ptrace_cancel_bpt(struct task_struct * child)
  207. {
  208. int i, nsaved = child->thread_info->bpt_nsaved;
  209. child->thread_info->bpt_nsaved = 0;
  210. if (nsaved > 2) {
  211. printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
  212. nsaved = 2;
  213. }
  214. for (i = 0; i < nsaved; ++i) {
  215. write_int(child, child->thread_info->bpt_addr[i],
  216. child->thread_info->bpt_insn[i]);
  217. }
  218. return (nsaved != 0);
  219. }
  220. /*
  221. * Called by kernel/ptrace.c when detaching..
  222. *
  223. * Make sure the single step bit is not set.
  224. */
  225. void ptrace_disable(struct task_struct *child)
  226. {
  227. ptrace_cancel_bpt(child);
  228. }
  229. asmlinkage long
  230. do_sys_ptrace(long request, long pid, long addr, long data,
  231. struct pt_regs *regs)
  232. {
  233. struct task_struct *child;
  234. unsigned long tmp;
  235. size_t copied;
  236. long ret;
  237. lock_kernel();
  238. DBG(DBG_MEM, ("request=%ld pid=%ld addr=0x%lx data=0x%lx\n",
  239. request, pid, addr, data));
  240. ret = -EPERM;
  241. if (request == PTRACE_TRACEME) {
  242. /* are we already being traced? */
  243. if (current->ptrace & PT_PTRACED)
  244. goto out_notsk;
  245. ret = security_ptrace(current->parent, current);
  246. if (ret)
  247. goto out_notsk;
  248. /* set the ptrace bit in the process ptrace flags. */
  249. current->ptrace |= PT_PTRACED;
  250. ret = 0;
  251. goto out_notsk;
  252. }
  253. if (pid == 1) /* you may not mess with init */
  254. goto out_notsk;
  255. ret = -ESRCH;
  256. read_lock(&tasklist_lock);
  257. child = find_task_by_pid(pid);
  258. if (child)
  259. get_task_struct(child);
  260. read_unlock(&tasklist_lock);
  261. if (!child)
  262. goto out_notsk;
  263. if (request == PTRACE_ATTACH) {
  264. ret = ptrace_attach(child);
  265. goto out;
  266. }
  267. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  268. if (ret < 0)
  269. goto out;
  270. switch (request) {
  271. /* When I and D space are separate, these will need to be fixed. */
  272. case PTRACE_PEEKTEXT: /* read word at location addr. */
  273. case PTRACE_PEEKDATA:
  274. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  275. ret = -EIO;
  276. if (copied != sizeof(tmp))
  277. break;
  278. regs->r0 = 0; /* special return: no errors */
  279. ret = tmp;
  280. break;
  281. /* Read register number ADDR. */
  282. case PTRACE_PEEKUSR:
  283. regs->r0 = 0; /* special return: no errors */
  284. ret = get_reg(child, addr);
  285. DBG(DBG_MEM, ("peek $%ld->%#lx\n", addr, ret));
  286. break;
  287. /* When I and D space are separate, this will have to be fixed. */
  288. case PTRACE_POKETEXT: /* write the word at location addr. */
  289. case PTRACE_POKEDATA:
  290. tmp = data;
  291. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
  292. ret = (copied == sizeof(tmp)) ? 0 : -EIO;
  293. break;
  294. case PTRACE_POKEUSR: /* write the specified register */
  295. DBG(DBG_MEM, ("poke $%ld<-%#lx\n", addr, data));
  296. ret = put_reg(child, addr, data);
  297. break;
  298. case PTRACE_SYSCALL:
  299. /* continue and stop at next (return from) syscall */
  300. case PTRACE_CONT: /* restart after signal. */
  301. ret = -EIO;
  302. if (!valid_signal(data))
  303. break;
  304. if (request == PTRACE_SYSCALL)
  305. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  306. else
  307. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  308. child->exit_code = data;
  309. /* make sure single-step breakpoint is gone. */
  310. ptrace_cancel_bpt(child);
  311. wake_up_process(child);
  312. ret = 0;
  313. break;
  314. /*
  315. * Make the child exit. Best I can do is send it a sigkill.
  316. * perhaps it should be put in the status that it wants to
  317. * exit.
  318. */
  319. case PTRACE_KILL:
  320. ret = 0;
  321. if (child->exit_state == EXIT_ZOMBIE)
  322. break;
  323. child->exit_code = SIGKILL;
  324. /* make sure single-step breakpoint is gone. */
  325. ptrace_cancel_bpt(child);
  326. wake_up_process(child);
  327. goto out;
  328. case PTRACE_SINGLESTEP: /* execute single instruction. */
  329. ret = -EIO;
  330. if (!valid_signal(data))
  331. break;
  332. /* Mark single stepping. */
  333. child->thread_info->bpt_nsaved = -1;
  334. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  335. child->exit_code = data;
  336. wake_up_process(child);
  337. /* give it a chance to run. */
  338. ret = 0;
  339. goto out;
  340. case PTRACE_DETACH: /* detach a process that was attached. */
  341. ret = ptrace_detach(child, data);
  342. goto out;
  343. default:
  344. ret = ptrace_request(child, request, addr, data);
  345. goto out;
  346. }
  347. out:
  348. put_task_struct(child);
  349. out_notsk:
  350. unlock_kernel();
  351. return ret;
  352. }
  353. asmlinkage void
  354. syscall_trace(void)
  355. {
  356. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  357. return;
  358. if (!(current->ptrace & PT_PTRACED))
  359. return;
  360. /* The 0x80 provides a way for the tracing parent to distinguish
  361. between a syscall stop and SIGTRAP delivery */
  362. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  363. ? 0x80 : 0));
  364. /*
  365. * This isn't the same as continuing with a signal, but it will do
  366. * for normal use. strace only continues with a signal if the
  367. * stopping signal is not SIGTRAP. -brl
  368. */
  369. if (current->exit_code) {
  370. send_sig(current->exit_code, current, 1);
  371. current->exit_code = 0;
  372. }
  373. }