gdb/inf-child.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Base/prototype target for default child (native) targets.

  2.    Copyright (C) 1988-2015 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. /* This file provides a common base class/target that all native
  15.    target implementations extend, by calling inf_child_target to get a
  16.    new prototype target and then overriding target methods as
  17.    necessary.  */

  18. #include "defs.h"
  19. #include "regcache.h"
  20. #include "memattr.h"
  21. #include "symtab.h"
  22. #include "target.h"
  23. #include "inferior.h"
  24. #include <sys/stat.h>
  25. #include "inf-child.h"
  26. #include "gdb/fileio.h"
  27. #include "agent.h"
  28. #include "gdb_wait.h"
  29. #include "filestuff.h"

  30. #include <sys/types.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>

  33. /* A pointer to what is returned by inf_child_target.  Used by
  34.    inf_child_open to push the most-derived target in reaction to
  35.    "target native".  */
  36. static struct target_ops *inf_child_ops = NULL;

  37. /* Helper function for child_wait and the derivatives of child_wait.
  38.    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
  39.    translation of that in OURSTATUS.  */
  40. void
  41. store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
  42. {
  43.   if (WIFEXITED (hoststatus))
  44.     {
  45.       ourstatus->kind = TARGET_WAITKIND_EXITED;
  46.       ourstatus->value.integer = WEXITSTATUS (hoststatus);
  47.     }
  48.   else if (!WIFSTOPPED (hoststatus))
  49.     {
  50.       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
  51.       ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
  52.     }
  53.   else
  54.     {
  55.       ourstatus->kind = TARGET_WAITKIND_STOPPED;
  56.       ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
  57.     }
  58. }

  59. /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
  60.    for all registers.  */

  61. static void
  62. inf_child_fetch_inferior_registers (struct target_ops *ops,
  63.                                     struct regcache *regcache, int regnum)
  64. {
  65.   if (regnum == -1)
  66.     {
  67.       for (regnum = 0;
  68.            regnum < gdbarch_num_regs (get_regcache_arch (regcache));
  69.            regnum++)
  70.         regcache_raw_supply (regcache, regnum, NULL);
  71.     }
  72.   else
  73.     regcache_raw_supply (regcache, regnum, NULL);
  74. }

  75. /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
  76.    this for all registers (including the floating point registers).  */

  77. static void
  78. inf_child_store_inferior_registers (struct target_ops *ops,
  79.                                     struct regcache *regcache, int regnum)
  80. {
  81. }

  82. static void
  83. inf_child_post_attach (struct target_ops *self, int pid)
  84. {
  85.   /* This target doesn't require a meaningful "post attach" operation
  86.      by a debugger.  */
  87. }

  88. /* Get ready to modify the registers array.  On machines which store
  89.    individual registers, this doesn't need to do anything.  On
  90.    machines which store all the registers in one fell swoop, this
  91.    makes sure that registers contains all the registers from the
  92.    program being debugged.  */

  93. static void
  94. inf_child_prepare_to_store (struct target_ops *self,
  95.                             struct regcache *regcache)
  96. {
  97. }

  98. /* True if the user did "target native".  In that case, we won't
  99.    unpush the child target automatically when the last inferior is
  100.    gone.  */
  101. static int inf_child_explicitly_opened;

  102. /* See inf-child.h.  */

  103. void
  104. inf_child_open_target (struct target_ops *target, const char *arg,
  105.                        int from_tty)
  106. {
  107.   target_preopen (from_tty);
  108.   push_target (target);
  109.   inf_child_explicitly_opened = 1;
  110.   if (from_tty)
  111.     printf_filtered ("Done.  Use the \"run\" command to start a process.\n");
  112. }

  113. static void
  114. inf_child_open (const char *arg, int from_tty)
  115. {
  116.   inf_child_open_target (inf_child_ops, arg, from_tty);
  117. }

  118. /* Implement the to_disconnect target_ops method.  */

  119. static void
  120. inf_child_disconnect (struct target_ops *target, const char *args, int from_tty)
  121. {
  122.   if (args != NULL)
  123.     error (_("Argument given to \"disconnect\"."));

  124.   /* This offers to detach/kill current inferiors, and then pops all
  125.      targets.  */
  126.   target_preopen (from_tty);
  127. }

  128. /* Implement the to_close target_ops method.  */

  129. static void
  130. inf_child_close (struct target_ops *target)
  131. {
  132.   /* In case we were forcibly closed.  */
  133.   inf_child_explicitly_opened = 0;
  134. }

  135. void
  136. inf_child_mourn_inferior (struct target_ops *ops)
  137. {
  138.   generic_mourn_inferior ();
  139.   inf_child_maybe_unpush_target (ops);
  140. }

  141. /* See inf-child.h.  */

  142. void
  143. inf_child_maybe_unpush_target (struct target_ops *ops)
  144. {
  145.   if (!inf_child_explicitly_opened && !have_inferiors ())
  146.     unpush_target (ops);
  147. }

  148. static void
  149. inf_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
  150. {
  151.   /* This target doesn't require a meaningful "post startup inferior"
  152.      operation by a debugger.  */
  153. }

  154. static int
  155. inf_child_follow_fork (struct target_ops *ops, int follow_child,
  156.                        int detach_fork)
  157. {
  158.   /* This target doesn't support following fork or vfork events.  */
  159.   return 0;
  160. }

  161. static int
  162. inf_child_can_run (struct target_ops *self)
  163. {
  164.   return 1;
  165. }

  166. static char *
  167. inf_child_pid_to_exec_file (struct target_ops *self, int pid)
  168. {
  169.   /* This target doesn't support translation of a process ID to the
  170.      filename of the executable file.  */
  171.   return NULL;
  172. }


  173. /* Target file operations.  */

  174. static int
  175. inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
  176. {
  177.   int open_flags = 0;

  178.   if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
  179.     return -1;

  180.   if (fileio_open_flags & FILEIO_O_CREAT)
  181.     open_flags |= O_CREAT;
  182.   if (fileio_open_flags & FILEIO_O_EXCL)
  183.     open_flags |= O_EXCL;
  184.   if (fileio_open_flags & FILEIO_O_TRUNC)
  185.     open_flags |= O_TRUNC;
  186.   if (fileio_open_flags & FILEIO_O_APPEND)
  187.     open_flags |= O_APPEND;
  188.   if (fileio_open_flags & FILEIO_O_RDONLY)
  189.     open_flags |= O_RDONLY;
  190.   if (fileio_open_flags & FILEIO_O_WRONLY)
  191.     open_flags |= O_WRONLY;
  192.   if (fileio_open_flags & FILEIO_O_RDWR)
  193.     open_flags |= O_RDWR;
  194. /* On systems supporting binary and text mode, always open files in
  195.    binary mode. */
  196. #ifdef O_BINARY
  197.   open_flags |= O_BINARY;
  198. #endif

  199.   *open_flags_p = open_flags;
  200.   return 0;
  201. }

  202. static int
  203. inf_child_errno_to_fileio_error (int errnum)
  204. {
  205.   switch (errnum)
  206.     {
  207.       case EPERM:
  208.         return FILEIO_EPERM;
  209.       case ENOENT:
  210.         return FILEIO_ENOENT;
  211.       case EINTR:
  212.         return FILEIO_EINTR;
  213.       case EIO:
  214.         return FILEIO_EIO;
  215.       case EBADF:
  216.         return FILEIO_EBADF;
  217.       case EACCES:
  218.         return FILEIO_EACCES;
  219.       case EFAULT:
  220.         return FILEIO_EFAULT;
  221.       case EBUSY:
  222.         return FILEIO_EBUSY;
  223.       case EEXIST:
  224.         return FILEIO_EEXIST;
  225.       case ENODEV:
  226.         return FILEIO_ENODEV;
  227.       case ENOTDIR:
  228.         return FILEIO_ENOTDIR;
  229.       case EISDIR:
  230.         return FILEIO_EISDIR;
  231.       case EINVAL:
  232.         return FILEIO_EINVAL;
  233.       case ENFILE:
  234.         return FILEIO_ENFILE;
  235.       case EMFILE:
  236.         return FILEIO_EMFILE;
  237.       case EFBIG:
  238.         return FILEIO_EFBIG;
  239.       case ENOSPC:
  240.         return FILEIO_ENOSPC;
  241.       case ESPIPE:
  242.         return FILEIO_ESPIPE;
  243.       case EROFS:
  244.         return FILEIO_EROFS;
  245.       case ENOSYS:
  246.         return FILEIO_ENOSYS;
  247.       case ENAMETOOLONG:
  248.         return FILEIO_ENAMETOOLONG;
  249.     }
  250.   return FILEIO_EUNKNOWN;
  251. }

  252. /* Open FILENAME on the target, using FLAGS and MODE.  Return a
  253.    target file descriptor, or -1 if an error occurs (and set
  254.    *TARGET_ERRNO).  */
  255. static int
  256. inf_child_fileio_open (struct target_ops *self,
  257.                        const char *filename, int flags, int mode,
  258.                        int *target_errno)
  259. {
  260.   int nat_flags;
  261.   int fd;

  262.   if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
  263.     {
  264.       *target_errno = FILEIO_EINVAL;
  265.       return -1;
  266.     }

  267.   /* We do not need to convert MODE, since the fileio protocol uses
  268.      the standard values.  */
  269.   fd = gdb_open_cloexec (filename, nat_flags, mode);
  270.   if (fd == -1)
  271.     *target_errno = inf_child_errno_to_fileio_error (errno);

  272.   return fd;
  273. }

  274. /* Write up to LEN bytes from WRITE_BUF to FD on the target.
  275.    Return the number of bytes written, or -1 if an error occurs
  276.    (and set *TARGET_ERRNO).  */
  277. static int
  278. inf_child_fileio_pwrite (struct target_ops *self,
  279.                          int fd, const gdb_byte *write_buf, int len,
  280.                          ULONGEST offset, int *target_errno)
  281. {
  282.   int ret;

  283. #ifdef HAVE_PWRITE
  284.   ret = pwrite (fd, write_buf, len, (long) offset);
  285. #else
  286.   ret = -1;
  287. #endif
  288.   /* If we have no pwrite or it failed for this file, use lseek/write.  */
  289.   if (ret == -1)
  290.     {
  291.       ret = lseek (fd, (long) offset, SEEK_SET);
  292.       if (ret != -1)
  293.         ret = write (fd, write_buf, len);
  294.     }

  295.   if (ret == -1)
  296.     *target_errno = inf_child_errno_to_fileio_error (errno);

  297.   return ret;
  298. }

  299. /* Read up to LEN bytes FD on the target into READ_BUF.
  300.    Return the number of bytes read, or -1 if an error occurs
  301.    (and set *TARGET_ERRNO).  */
  302. static int
  303. inf_child_fileio_pread (struct target_ops *self,
  304.                         int fd, gdb_byte *read_buf, int len,
  305.                         ULONGEST offset, int *target_errno)
  306. {
  307.   int ret;

  308. #ifdef HAVE_PREAD
  309.   ret = pread (fd, read_buf, len, (long) offset);
  310. #else
  311.   ret = -1;
  312. #endif
  313.   /* If we have no pread or it failed for this file, use lseek/read.  */
  314.   if (ret == -1)
  315.     {
  316.       ret = lseek (fd, (long) offset, SEEK_SET);
  317.       if (ret != -1)
  318.         ret = read (fd, read_buf, len);
  319.     }

  320.   if (ret == -1)
  321.     *target_errno = inf_child_errno_to_fileio_error (errno);

  322.   return ret;
  323. }

  324. /* Close FD on the target.  Return 0, or -1 if an error occurs
  325.    (and set *TARGET_ERRNO).  */
  326. static int
  327. inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno)
  328. {
  329.   int ret;

  330.   ret = close (fd);
  331.   if (ret == -1)
  332.     *target_errno = inf_child_errno_to_fileio_error (errno);

  333.   return ret;
  334. }

  335. /* Unlink FILENAME on the target.  Return 0, or -1 if an error
  336.    occurs (and set *TARGET_ERRNO).  */
  337. static int
  338. inf_child_fileio_unlink (struct target_ops *self,
  339.                          const char *filename, int *target_errno)
  340. {
  341.   int ret;

  342.   ret = unlink (filename);
  343.   if (ret == -1)
  344.     *target_errno = inf_child_errno_to_fileio_error (errno);

  345.   return ret;
  346. }

  347. /* Read value of symbolic link FILENAME on the target.  Return a
  348.    null-terminated string allocated via xmalloc, or NULL if an error
  349.    occurs (and set *TARGET_ERRNO).  */
  350. static char *
  351. inf_child_fileio_readlink (struct target_ops *self,
  352.                            const char *filename, int *target_errno)
  353. {
  354.   /* We support readlink only on systems that also provide a compile-time
  355.      maximum path length (PATH_MAX), at least for now.  */
  356. #if defined (PATH_MAX)
  357.   char buf[PATH_MAX];
  358.   int len;
  359.   char *ret;

  360.   len = readlink (filename, buf, sizeof buf);
  361.   if (len < 0)
  362.     {
  363.       *target_errno = inf_child_errno_to_fileio_error (errno);
  364.       return NULL;
  365.     }

  366.   ret = xmalloc (len + 1);
  367.   memcpy (ret, buf, len);
  368.   ret[len] = '\0';
  369.   return ret;
  370. #else
  371.   *target_errno = FILEIO_ENOSYS;
  372.   return NULL;
  373. #endif
  374. }

  375. static int
  376. inf_child_use_agent (struct target_ops *self, int use)
  377. {
  378.   if (agent_loaded_p ())
  379.     {
  380.       use_agent = use;
  381.       return 1;
  382.     }
  383.   else
  384.     return 0;
  385. }

  386. static int
  387. inf_child_can_use_agent (struct target_ops *self)
  388. {
  389.   return agent_loaded_p ();
  390. }

  391. /* Default implementation of the to_can_async_p and
  392.    to_supports_non_stop methods.  */

  393. static int
  394. return_zero (struct target_ops *ignore)
  395. {
  396.   return 0;
  397. }

  398. struct target_ops *
  399. inf_child_target (void)
  400. {
  401.   struct target_ops *t = XCNEW (struct target_ops);

  402.   t->to_shortname = "native";
  403.   t->to_longname = "Native process";
  404.   t->to_doc = "Native process (started by the \"run\" command).";
  405.   t->to_open = inf_child_open;
  406.   t->to_close = inf_child_close;
  407.   t->to_disconnect = inf_child_disconnect;
  408.   t->to_post_attach = inf_child_post_attach;
  409.   t->to_fetch_registers = inf_child_fetch_inferior_registers;
  410.   t->to_store_registers = inf_child_store_inferior_registers;
  411.   t->to_prepare_to_store = inf_child_prepare_to_store;
  412.   t->to_insert_breakpoint = memory_insert_breakpoint;
  413.   t->to_remove_breakpoint = memory_remove_breakpoint;
  414.   t->to_terminal_init = child_terminal_init;
  415.   t->to_terminal_inferior = child_terminal_inferior;
  416.   t->to_terminal_ours_for_output = child_terminal_ours_for_output;
  417.   t->to_terminal_ours = child_terminal_ours;
  418.   t->to_terminal_info = child_terminal_info;
  419.   t->to_post_startup_inferior = inf_child_post_startup_inferior;
  420.   t->to_follow_fork = inf_child_follow_fork;
  421.   t->to_can_run = inf_child_can_run;
  422.   /* We must default these because they must be implemented by any
  423.      target that can run.  */
  424.   t->to_can_async_p = return_zero;
  425.   t->to_supports_non_stop = return_zero;
  426.   t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
  427.   t->to_stratum = process_stratum;
  428.   t->to_has_all_memory = default_child_has_all_memory;
  429.   t->to_has_memory = default_child_has_memory;
  430.   t->to_has_stack = default_child_has_stack;
  431.   t->to_has_registers = default_child_has_registers;
  432.   t->to_has_execution = default_child_has_execution;
  433.   t->to_fileio_open = inf_child_fileio_open;
  434.   t->to_fileio_pwrite = inf_child_fileio_pwrite;
  435.   t->to_fileio_pread = inf_child_fileio_pread;
  436.   t->to_fileio_close = inf_child_fileio_close;
  437.   t->to_fileio_unlink = inf_child_fileio_unlink;
  438.   t->to_fileio_readlink = inf_child_fileio_readlink;
  439.   t->to_magic = OPS_MAGIC;
  440.   t->to_use_agent = inf_child_use_agent;
  441.   t->to_can_use_agent = inf_child_can_use_agent;

  442.   /* Store a pointer so we can push the most-derived target from
  443.      inf_child_open.  */
  444.   inf_child_ops = t;

  445.   return t;
  446. }