inf-ptrace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* Low-level child interface to ptrace.
  2. Copyright (C) 1988-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "command.h"
  16. #include "inferior.h"
  17. #include "terminal.h"
  18. #include "gdbcore.h"
  19. #include "regcache.h"
  20. #include "nat/gdb_ptrace.h"
  21. #include "gdbsupport/gdb_wait.h"
  22. #include <signal.h>
  23. #include "inf-ptrace.h"
  24. #include "inf-child.h"
  25. #include "gdbthread.h"
  26. #include "nat/fork-inferior.h"
  27. #include "utils.h"
  28. #include "gdbarch.h"
  29. static PTRACE_TYPE_RET
  30. gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
  31. PTRACE_TYPE_ARG4 data)
  32. {
  33. #ifdef __NetBSD__
  34. return ptrace (request, ptid.pid (), addr, data);
  35. #else
  36. pid_t pid = get_ptrace_pid (ptid);
  37. return ptrace (request, pid, addr, data);
  38. #endif
  39. }
  40. /* The event pipe registered as a waitable file in the event loop. */
  41. event_pipe inf_ptrace_target::m_event_pipe;
  42. inf_ptrace_target::~inf_ptrace_target ()
  43. {}
  44. /* Prepare to be traced. */
  45. static void
  46. inf_ptrace_me (void)
  47. {
  48. /* "Trace me, Dr. Memory!" */
  49. if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
  50. trace_start_error_with_name ("ptrace");
  51. }
  52. /* Start a new inferior Unix child process. EXEC_FILE is the file to
  53. run, ALLARGS is a string containing the arguments to the program.
  54. ENV is the environment vector to pass. If FROM_TTY is non-zero, be
  55. chatty about it. */
  56. void
  57. inf_ptrace_target::create_inferior (const char *exec_file,
  58. const std::string &allargs,
  59. char **env, int from_tty)
  60. {
  61. inferior *inf = current_inferior ();
  62. /* Do not change either targets above or the same target if already present.
  63. The reason is the target stack is shared across multiple inferiors. */
  64. int ops_already_pushed = inf->target_is_pushed (this);
  65. target_unpush_up unpusher;
  66. if (! ops_already_pushed)
  67. {
  68. /* Clear possible core file with its process_stratum. */
  69. inf->push_target (this);
  70. unpusher.reset (this);
  71. }
  72. pid_t pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
  73. NULL, NULL, NULL);
  74. ptid_t ptid (pid);
  75. /* We have something that executes now. We'll be running through
  76. the shell at this point (if startup-with-shell is true), but the
  77. pid shouldn't change. */
  78. thread_info *thr = add_thread_silent (this, ptid);
  79. switch_to_thread (thr);
  80. unpusher.release ();
  81. gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
  82. /* On some targets, there must be some explicit actions taken after
  83. the inferior has been started up. */
  84. post_startup_inferior (ptid);
  85. }
  86. /* Clean up a rotting corpse of an inferior after it died. */
  87. void
  88. inf_ptrace_target::mourn_inferior ()
  89. {
  90. int status;
  91. /* Wait just one more time to collect the inferior's exit status.
  92. Do not check whether this succeeds though, since we may be
  93. dealing with a process that we attached to. Such a process will
  94. only report its exit status to its original parent. */
  95. waitpid (inferior_ptid.pid (), &status, 0);
  96. inf_child_target::mourn_inferior ();
  97. }
  98. /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
  99. be chatty about it. */
  100. void
  101. inf_ptrace_target::attach (const char *args, int from_tty)
  102. {
  103. inferior *inf = current_inferior ();
  104. /* Do not change either targets above or the same target if already present.
  105. The reason is the target stack is shared across multiple inferiors. */
  106. int ops_already_pushed = inf->target_is_pushed (this);
  107. pid_t pid = parse_pid_to_attach (args);
  108. if (pid == getpid ()) /* Trying to masturbate? */
  109. error (_("I refuse to debug myself!"));
  110. target_unpush_up unpusher;
  111. if (! ops_already_pushed)
  112. {
  113. /* target_pid_to_str already uses the target. Also clear possible core
  114. file with its process_stratum. */
  115. inf->push_target (this);
  116. unpusher.reset (this);
  117. }
  118. target_announce_attach (from_tty, pid);
  119. #ifdef PT_ATTACH
  120. errno = 0;
  121. ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
  122. if (errno != 0)
  123. perror_with_name (("ptrace"));
  124. #else
  125. error (_("This system does not support attaching to a process"));
  126. #endif
  127. inferior_appeared (inf, pid);
  128. inf->attach_flag = 1;
  129. /* Always add a main thread. If some target extends the ptrace
  130. target, it should decorate the ptid later with more info. */
  131. thread_info *thr = add_thread_silent (this, ptid_t (pid));
  132. switch_to_thread (thr);
  133. /* Don't consider the thread stopped until we've processed its
  134. initial SIGSTOP stop. */
  135. set_executing (this, thr->ptid, true);
  136. unpusher.release ();
  137. }
  138. /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
  139. void
  140. inf_ptrace_target::detach (inferior *inf, int from_tty)
  141. {
  142. pid_t pid = inferior_ptid.pid ();
  143. target_announce_detach (from_tty);
  144. #ifdef PT_DETACH
  145. /* We'd better not have left any breakpoints in the program or it'll
  146. die when it hits one. Also note that this may only work if we
  147. previously attached to the inferior. It *might* work if we
  148. started the process ourselves. */
  149. errno = 0;
  150. ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
  151. if (errno != 0)
  152. perror_with_name (("ptrace"));
  153. #else
  154. error (_("This system does not support detaching from a process"));
  155. #endif
  156. detach_success (inf);
  157. }
  158. /* See inf-ptrace.h. */
  159. void
  160. inf_ptrace_target::detach_success (inferior *inf)
  161. {
  162. switch_to_no_thread ();
  163. detach_inferior (inf);
  164. maybe_unpush_target ();
  165. }
  166. /* Kill the inferior. */
  167. void
  168. inf_ptrace_target::kill ()
  169. {
  170. pid_t pid = inferior_ptid.pid ();
  171. int status;
  172. if (pid == 0)
  173. return;
  174. ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
  175. waitpid (pid, &status, 0);
  176. target_mourn_inferior (inferior_ptid);
  177. }
  178. #ifndef __NetBSD__
  179. /* See inf-ptrace.h. */
  180. pid_t
  181. get_ptrace_pid (ptid_t ptid)
  182. {
  183. pid_t pid;
  184. /* If we have an LWPID to work with, use it. Otherwise, we're
  185. dealing with a non-threaded program/target. */
  186. pid = ptid.lwp ();
  187. if (pid == 0)
  188. pid = ptid.pid ();
  189. return pid;
  190. }
  191. #endif
  192. /* Resume execution of thread PTID, or all threads if PTID is -1. If
  193. STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
  194. that signal. */
  195. void
  196. inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
  197. {
  198. PTRACE_TYPE_ARG1 request;
  199. if (minus_one_ptid == ptid)
  200. /* Resume all threads. Traditionally ptrace() only supports
  201. single-threaded processes, so simply resume the inferior. */
  202. ptid = ptid_t (inferior_ptid.pid ());
  203. if (catch_syscall_enabled () > 0)
  204. request = PT_SYSCALL;
  205. else
  206. request = PT_CONTINUE;
  207. if (step)
  208. {
  209. /* If this system does not support PT_STEP, a higher level
  210. function will have called the appropriate functions to transmute the
  211. step request into a continue request (by setting breakpoints on
  212. all possible successor instructions), so we don't have to
  213. worry about that here. */
  214. request = PT_STEP;
  215. }
  216. /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
  217. where it was. If GDB wanted it to start some other way, we have
  218. already written a new program counter value to the child. */
  219. errno = 0;
  220. gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
  221. if (errno != 0)
  222. perror_with_name (("ptrace"));
  223. }
  224. /* Wait for the child specified by PTID to do something. Return the
  225. process ID of the child, or MINUS_ONE_PTID in case of error; store
  226. the status in *OURSTATUS. */
  227. ptid_t
  228. inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
  229. target_wait_flags target_options)
  230. {
  231. pid_t pid;
  232. int options, status, save_errno;
  233. options = 0;
  234. if (target_options & TARGET_WNOHANG)
  235. options |= WNOHANG;
  236. do
  237. {
  238. set_sigint_trap ();
  239. do
  240. {
  241. pid = waitpid (ptid.pid (), &status, options);
  242. save_errno = errno;
  243. }
  244. while (pid == -1 && errno == EINTR);
  245. clear_sigint_trap ();
  246. if (pid == 0)
  247. {
  248. gdb_assert (target_options & TARGET_WNOHANG);
  249. ourstatus->set_ignore ();
  250. return minus_one_ptid;
  251. }
  252. if (pid == -1)
  253. {
  254. /* In async mode the SIGCHLD might have raced and triggered
  255. a check for an event that had already been reported. If
  256. the event was the exit of the only remaining child,
  257. waitpid() will fail with ECHILD. */
  258. if (ptid == minus_one_ptid && save_errno == ECHILD)
  259. {
  260. ourstatus->set_no_resumed ();
  261. return minus_one_ptid;
  262. }
  263. gdb_printf (gdb_stderr,
  264. _("Child process unexpectedly missing: %s.\n"),
  265. safe_strerror (save_errno));
  266. ourstatus->set_ignore ();
  267. return minus_one_ptid;
  268. }
  269. /* Ignore terminated detached child processes. */
  270. if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
  271. pid = -1;
  272. }
  273. while (pid == -1);
  274. *ourstatus = host_status_to_waitstatus (status);
  275. return ptid_t (pid);
  276. }
  277. /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
  278. from process PID's memory into READBUF. Start at target address ADDR
  279. and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
  280. be non-null. Return the number of transferred bytes. */
  281. static ULONGEST
  282. inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
  283. const gdb_byte *writebuf,
  284. ULONGEST addr, ULONGEST len)
  285. {
  286. ULONGEST n;
  287. unsigned int chunk;
  288. /* We transfer aligned words. Thus align ADDR down to a word
  289. boundary and determine how many bytes to skip at the
  290. beginning. */
  291. ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
  292. addr -= skip;
  293. for (n = 0;
  294. n < len;
  295. n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
  296. {
  297. /* Restrict to a chunk that fits in the current word. */
  298. chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
  299. /* Use a union for type punning. */
  300. union
  301. {
  302. PTRACE_TYPE_RET word;
  303. gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
  304. } buf;
  305. /* Read the word, also when doing a partial word write. */
  306. if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
  307. {
  308. errno = 0;
  309. buf.word = gdb_ptrace (PT_READ_I, ptid,
  310. (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
  311. if (errno != 0)
  312. break;
  313. if (readbuf != NULL)
  314. memcpy (readbuf + n, buf.byte + skip, chunk);
  315. }
  316. if (writebuf != NULL)
  317. {
  318. memcpy (buf.byte + skip, writebuf + n, chunk);
  319. errno = 0;
  320. gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
  321. buf.word);
  322. if (errno != 0)
  323. {
  324. /* Using the appropriate one (I or D) is necessary for
  325. Gould NP1, at least. */
  326. errno = 0;
  327. gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
  328. buf.word);
  329. if (errno != 0)
  330. break;
  331. }
  332. }
  333. }
  334. return n;
  335. }
  336. /* Implement the to_xfer_partial target_ops method. */
  337. enum target_xfer_status
  338. inf_ptrace_target::xfer_partial (enum target_object object,
  339. const char *annex, gdb_byte *readbuf,
  340. const gdb_byte *writebuf,
  341. ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
  342. {
  343. ptid_t ptid = inferior_ptid;
  344. switch (object)
  345. {
  346. case TARGET_OBJECT_MEMORY:
  347. #ifdef PT_IO
  348. /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
  349. request that promises to be much more efficient in reading
  350. and writing data in the traced process's address space. */
  351. {
  352. struct ptrace_io_desc piod;
  353. /* NOTE: We assume that there are no distinct address spaces
  354. for instruction and data. However, on OpenBSD 3.9 and
  355. later, PIOD_WRITE_D doesn't allow changing memory that's
  356. mapped read-only. Since most code segments will be
  357. read-only, using PIOD_WRITE_D will prevent us from
  358. inserting breakpoints, so we use PIOD_WRITE_I instead. */
  359. piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
  360. piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
  361. piod.piod_offs = (void *) (long) offset;
  362. piod.piod_len = len;
  363. errno = 0;
  364. if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
  365. {
  366. /* Return the actual number of bytes read or written. */
  367. *xfered_len = piod.piod_len;
  368. return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
  369. }
  370. /* If the PT_IO request is somehow not supported, fallback on
  371. using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
  372. to indicate failure. */
  373. if (errno != EINVAL)
  374. return TARGET_XFER_EOF;
  375. }
  376. #endif
  377. *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
  378. offset, len);
  379. return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
  380. case TARGET_OBJECT_UNWIND_TABLE:
  381. return TARGET_XFER_E_IO;
  382. case TARGET_OBJECT_AUXV:
  383. #if defined (PT_IO) && defined (PIOD_READ_AUXV)
  384. /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
  385. request that allows us to read the auxilliary vector. Other
  386. BSD's may follow if they feel the need to support PIE. */
  387. {
  388. struct ptrace_io_desc piod;
  389. if (writebuf)
  390. return TARGET_XFER_E_IO;
  391. piod.piod_op = PIOD_READ_AUXV;
  392. piod.piod_addr = readbuf;
  393. piod.piod_offs = (void *) (long) offset;
  394. piod.piod_len = len;
  395. errno = 0;
  396. if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
  397. {
  398. /* Return the actual number of bytes read or written. */
  399. *xfered_len = piod.piod_len;
  400. return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
  401. }
  402. }
  403. #endif
  404. return TARGET_XFER_E_IO;
  405. case TARGET_OBJECT_WCOOKIE:
  406. return TARGET_XFER_E_IO;
  407. default:
  408. return TARGET_XFER_E_IO;
  409. }
  410. }
  411. /* Return non-zero if the thread specified by PTID is alive. */
  412. bool
  413. inf_ptrace_target::thread_alive (ptid_t ptid)
  414. {
  415. /* ??? Is kill the right way to do this? */
  416. return (::kill (ptid.pid (), 0) != -1);
  417. }
  418. /* Print status information about what we're accessing. */
  419. void
  420. inf_ptrace_target::files_info ()
  421. {
  422. struct inferior *inf = current_inferior ();
  423. gdb_printf (_("\tUsing the running image of %s %s.\n"),
  424. inf->attach_flag ? "attached" : "child",
  425. target_pid_to_str (inferior_ptid).c_str ());
  426. }
  427. std::string
  428. inf_ptrace_target::pid_to_str (ptid_t ptid)
  429. {
  430. return normal_pid_to_str (ptid);
  431. }
  432. /* Implement the "close" target method. */
  433. void
  434. inf_ptrace_target::close ()
  435. {
  436. /* Unregister from the event loop. */
  437. if (is_async_p ())
  438. async (0);
  439. inf_child_target::close ();
  440. }