inflow.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include "defs.h"
  18. #include "initialize.h"
  19. #include "param.h"
  20. #include "frame.h"
  21. #include "inferior.h"
  22. #include <sys/param.h>
  23. #include <sys/dir.h>
  24. #include <sys/user.h>
  25. #include <signal.h>
  26. #include <sys/ioctl.h>
  27. #include <sgtty.h>
  28. #include <fcntl.h>
  29. #ifdef NEW_SUN_PTRACE
  30. #include <sys/ptrace.h>
  31. #include <machine/reg.h>
  32. #endif
  33. START_FILE
  34. /* Record terminal status separately for debugger and inferior. */
  35. static struct sgttyb sg_inferior;
  36. static struct tchars tc_inferior;
  37. static struct ltchars ltc_inferior;
  38. static int lmode_inferior;
  39. static int tflags_inferior;
  40. static int pgrp_inferior;
  41. static struct sgttyb sg_ours;
  42. static struct tchars tc_ours;
  43. static struct ltchars ltc_ours;
  44. static int lmode_ours;
  45. static int tflags_ours;
  46. static int pgrp_ours;
  47. static void terminal_ours_1 ();
  48. /* Nonzero if our terminal settings are in effect.
  49. Zero if the inferior's settings are in effect. */
  50. static int terminal_is_ours;
  51. /* Initialize the terminal settings we record for the inferior,
  52. before we actually run the inferior. */
  53. void
  54. terminal_init_inferior ()
  55. {
  56. sg_inferior = sg_ours;
  57. tc_inferior = tc_ours;
  58. ltc_inferior = ltc_ours;
  59. lmode_inferior = lmode_ours;
  60. tflags_inferior = tflags_ours;
  61. pgrp_inferior = inferior_pid;
  62. terminal_is_ours = 1;
  63. }
  64. /* Put the inferior's terminal settings into effect.
  65. This is preparation for starting or resuming the inferior. */
  66. void
  67. terminal_inferior ()
  68. {
  69. if (terminal_is_ours)
  70. {
  71. fcntl (0, F_SETFL, tflags_inferior);
  72. fcntl (0, F_SETFL, tflags_inferior);
  73. ioctl (0, TIOCSETN, &sg_inferior);
  74. ioctl (0, TIOCSETC, &tc_inferior);
  75. ioctl (0, TIOCSLTC, &ltc_inferior);
  76. ioctl (0, TIOCLSET, &lmode_inferior);
  77. ioctl (0, TIOCSPGRP, &pgrp_inferior);
  78. }
  79. terminal_is_ours = 0;
  80. }
  81. /* Put some of our terminal settings into effect,
  82. enough to get proper results from our output,
  83. but do not change into or out of RAW mode
  84. so that no input is discarded.
  85. After doing this, either terminal_ours or terminal_inferior
  86. should be called to get back to a normal state of affairs. */
  87. void
  88. terminal_ours_for_output ()
  89. {
  90. terminal_ours_1 (1);
  91. }
  92. /* Put our terminal settings into effect.
  93. First record the inferior's terminal settings
  94. so they can be restored properly later. */
  95. void
  96. terminal_ours ()
  97. {
  98. terminal_ours_1 (0);
  99. }
  100. static void
  101. terminal_ours_1 (output_only)
  102. int output_only;
  103. {
  104. /* Ignore this signal since it will happen when we try to set the pgrp. */
  105. int (*osigttou) ();
  106. if (!terminal_is_ours)
  107. {
  108. terminal_is_ours = 1;
  109. osigttou = signal (SIGTTOU, SIG_IGN);
  110. ioctl (0, TIOCGPGRP, &pgrp_inferior);
  111. ioctl (0, TIOCSPGRP, &pgrp_ours);
  112. signal (SIGTTOU, osigttou);
  113. fcntl (0, F_GETFL, tflags_inferior);
  114. ioctl (0, TIOCGETP, &sg_inferior);
  115. ioctl (0, TIOCGETC, &tc_inferior);
  116. ioctl (0, TIOCGLTC, &ltc_inferior);
  117. ioctl (0, TIOCLGET, &lmode_inferior);
  118. }
  119. sg_ours.sg_flags &= ~RAW & ~CBREAK;
  120. if (output_only)
  121. sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
  122. fcntl (0, F_SETFL, tflags_ours);
  123. fcntl (0, F_SETFL, tflags_ours);
  124. ioctl (0, TIOCSETN, &sg_ours);
  125. ioctl (0, TIOCSETC, &tc_ours);
  126. ioctl (0, TIOCSLTC, &ltc_ours);
  127. ioctl (0, TIOCLSET, &lmode_ours);
  128. sg_ours.sg_flags &= ~RAW & ~CBREAK;
  129. }
  130. static void
  131. term_status_command ()
  132. {
  133. printf ("Inferior's terminal status (currently saved by GDB):\n");
  134. printf ("fcntl flags = 0x%x, lmode = 0x%x,\nsgttyb.sg_flags = 0x%x, owner pid = %d.\n",
  135. tflags_inferior, lmode_inferior,
  136. sg_inferior.sg_flags, pgrp_inferior);
  137. }
  138. /* Kill the inferior process. Make us have no inferior. */
  139. static void
  140. kill_command ()
  141. {
  142. if (inferior_pid == 0)
  143. error ("The program is not being run.");
  144. if (!query ("Kill the inferior process? "))
  145. error ("Not confirmed.");
  146. kill_inferior ();
  147. }
  148. kill_inferior ()
  149. {
  150. if (inferior_pid == 0)
  151. return;
  152. ptrace (8, inferior_pid, 0, 0);
  153. wait (0);
  154. inferior_pid = 0;
  155. mark_breakpoints_out ();
  156. if (have_core_file_p ())
  157. set_current_frame (read_register (FP_REGNUM));
  158. }
  159. /* Resume execution of the inferior process.
  160. If STEP is nonzero, single-step it.
  161. If SIGNAL is nonzero, give it that signal. */
  162. void
  163. resume (step, signal)
  164. int step;
  165. int signal;
  166. {
  167. extern int errno;
  168. errno = 0;
  169. ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  170. if (errno)
  171. perror_with_name ("ptrace");
  172. }
  173. #ifdef NEW_SUN_PTRACE
  174. void
  175. fetch_inferior_registers ()
  176. {
  177. struct regs inferior_registers;
  178. struct fp_status inferior_fp_registers;
  179. extern char registers[];
  180. ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
  181. ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
  182. bcopy (&inferior_registers, registers, 16 * 4);
  183. bcopy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
  184. sizeof inferior_fp_registers.fps_regs);
  185. *(int *)&registers[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
  186. *(int *)&registers[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
  187. bcopy (&inferior_fp_registers.fps_control,
  188. &registers[REGISTER_BYTE (FPC_REGNUM)],
  189. sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  190. }
  191. /* Store our register values back into the inferior.
  192. If REGNO is -1, do this for all registers.
  193. Otherwise, REGNO specifies which register (so we can save time). */
  194. store_inferior_registers (regno)
  195. int regno;
  196. {
  197. struct regs inferior_registers;
  198. struct fp_status inferior_fp_registers;
  199. extern char registers[];
  200. bcopy (registers, &inferior_registers, 16 * 4);
  201. bcopy (&registers[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
  202. sizeof inferior_fp_registers.fps_regs);
  203. inferior_registers.r_ps = *(int *)&registers[REGISTER_BYTE (PS_REGNUM)];
  204. inferior_registers.r_pc = *(int *)&registers[REGISTER_BYTE (PC_REGNUM)];
  205. bcopy (&registers[REGISTER_BYTE (FPC_REGNUM)],
  206. &inferior_fp_registers.fps_control,
  207. sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  208. ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
  209. ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
  210. }
  211. #else
  212. void
  213. fetch_inferior_registers ()
  214. {
  215. struct user u;
  216. register unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  217. register int regno;
  218. register unsigned int regaddr;
  219. offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  220. for (regno = 0; regno < NUM_REGS; regno++)
  221. {
  222. regaddr = register_addr (regno, offset);
  223. supply_register (regno, ptrace (3, inferior_pid, regaddr, 0));
  224. }
  225. }
  226. /* Store our register values back into the inferior.
  227. If REGNO is -1, do this for all registers.
  228. Otherwise, REGNO specifies which register (so we can save time). */
  229. store_inferior_registers (regno)
  230. int regno;
  231. {
  232. struct user u;
  233. register unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  234. register unsigned int regaddr;
  235. char buf[80];
  236. offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  237. if (regno >= 0)
  238. {
  239. regaddr = register_addr (regno, offset);
  240. errno = 0;
  241. ptrace (6, inferior_pid, regaddr, read_register (regno));
  242. if (errno != 0)
  243. {
  244. sprintf (buf, "writing register number %d", regno);
  245. perror_with_name (buf);
  246. }
  247. }
  248. else for (regno = 0; regno < NUM_REGS; regno++)
  249. {
  250. regaddr = register_addr (regno, offset);
  251. errno = 0;
  252. ptrace (6, inferior_pid, regaddr, read_register (regno));
  253. if (errno != 0)
  254. {
  255. sprintf (buf, "writing register number %d", regno);
  256. perror_with_name (buf);
  257. }
  258. }
  259. }
  260. #endif
  261. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  262. in the NEW_SUN_PTRACE case.
  263. It ought to be straightforward. But it appears that writing did
  264. not write the data that I specified. I cannot understand where
  265. it got the data that it actually did write. */
  266. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  267. to debugger memory starting at MYADDR. */
  268. read_inferior_memory (memaddr, myaddr, len)
  269. CORE_ADDR memaddr;
  270. char *myaddr;
  271. int len;
  272. {
  273. register int i;
  274. /* Round starting address down to longword boundary. */
  275. register CORE_ADDR addr = memaddr & - sizeof (int);
  276. /* Round ending address up; get number of longwords that makes. */
  277. register int count
  278. = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  279. /* Allocate buffer of that many longwords. */
  280. register int *buffer = (int *) alloca (count * sizeof (int));
  281. /* Read all the longwords */
  282. for (i = 0; i < count; i++, addr += sizeof (int))
  283. buffer[i] = ptrace (1, inferior_pid, addr, 0);
  284. /* Copy appropriate bytes out of the buffer. */
  285. bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  286. }
  287. /* Copy LEN bytes of data from debugger memnory at MYADDR
  288. to inferior's memory at MEMADDR.
  289. Returns nonzero on failure (cannot write the inferior) */
  290. int
  291. write_inferior_memory (memaddr, myaddr, len)
  292. CORE_ADDR memaddr;
  293. char *myaddr;
  294. int len;
  295. {
  296. register int i;
  297. /* Round starting address down to longword boundary. */
  298. register CORE_ADDR addr = memaddr & - sizeof (int);
  299. /* Round ending address up; get number of longwords that makes. */
  300. register int count
  301. = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  302. /* Allocate buffer of that many longwords. */
  303. register int *buffer = (int *) alloca (count * sizeof (int));
  304. extern int errno;
  305. /* Fill start and end extra bytes of buffer with existing memory data. */
  306. buffer[0] = ptrace (1, inferior_pid, addr, 0);
  307. if (count > 1)
  308. buffer[count - 1]
  309. = ptrace (1, inferior_pid,
  310. addr + (count - 1) * sizeof (int), 0);
  311. /* Copy data to be written over corresponding part of buffer */
  312. bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  313. /* Write the entire buffer. */
  314. for (i = 0; i < count; i++, addr += sizeof (int))
  315. {
  316. errno = 0;
  317. ptrace (4, inferior_pid, addr, buffer[i]);
  318. if (errno)
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. static void
  324. try_writing_regs_command ()
  325. {
  326. register int i;
  327. register int value;
  328. extern int errno;
  329. if (inferior_pid == 0)
  330. error ("The program is not being run.");
  331. for (i = 0; ; i += 2)
  332. {
  333. QUIT;
  334. errno = 0;
  335. value = ptrace (3, inferior_pid, i, 0);
  336. ptrace (6, inferior_pid, i, value);
  337. if (errno == 0)
  338. {
  339. printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
  340. i, value, value);
  341. }
  342. else if ((i & 0377) == 0)
  343. printf (" Failed at 0x%x.\n", i);
  344. }
  345. }
  346. static
  347. initialize ()
  348. {
  349. add_com ("term-status", class_obscure, term_status_command,
  350. "Print info on inferior's saved terminal status.");
  351. add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
  352. "Try writing all locations in inferior's system block.\n\
  353. Report which ones can be written.");
  354. add_com ("kill", class_run, kill_command,
  355. "Kill execution of program being debugged.");
  356. inferior_pid = 0;
  357. ioctl (0, TIOCGETP, &sg_ours);
  358. ioctl (0, TIOCGETC, &tc_ours);
  359. ioctl (0, TIOCGLTC, &ltc_ours);
  360. ioctl (0, TIOCLGET, &lmode_ours);
  361. fcntl (0, F_GETFL, tflags_ours);
  362. ioctl (0, TIOCGPGRP, &pgrp_ours);
  363. terminal_is_ours = 1;
  364. }
  365. END_FILE