GDB (API)
/home/stan/gdb/src/gdb/inf-child.c
Go to the documentation of this file.
00001 /* Default child (native) target interface, for GDB when running under
00002    Unix.
00003 
00004    Copyright (C) 1988-2013 Free Software Foundation, Inc.
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #include "defs.h"
00022 #include "regcache.h"
00023 #include "memattr.h"
00024 #include "symtab.h"
00025 #include "target.h"
00026 #include "inferior.h"
00027 #include "gdb_string.h"
00028 #include "gdb_stat.h"
00029 #include "inf-child.h"
00030 #include "gdb/fileio.h"
00031 #include "agent.h"
00032 #include "gdb_wait.h"
00033 #include "filestuff.h"
00034 
00035 #include <sys/types.h>
00036 #include <fcntl.h>
00037 #include <unistd.h>
00038 
00039 /* Helper function for child_wait and the derivatives of child_wait.
00040    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
00041    translation of that in OURSTATUS.  */
00042 void
00043 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
00044 {
00045   if (WIFEXITED (hoststatus))
00046     {
00047       ourstatus->kind = TARGET_WAITKIND_EXITED;
00048       ourstatus->value.integer = WEXITSTATUS (hoststatus);
00049     }
00050   else if (!WIFSTOPPED (hoststatus))
00051     {
00052       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
00053       ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
00054     }
00055   else
00056     {
00057       ourstatus->kind = TARGET_WAITKIND_STOPPED;
00058       ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
00059     }
00060 }
00061 
00062 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
00063    for all registers.  */
00064 
00065 static void
00066 inf_child_fetch_inferior_registers (struct target_ops *ops,
00067                                     struct regcache *regcache, int regnum)
00068 {
00069   if (regnum == -1)
00070     {
00071       for (regnum = 0;
00072            regnum < gdbarch_num_regs (get_regcache_arch (regcache));
00073            regnum++)
00074         regcache_raw_supply (regcache, regnum, NULL);
00075     }
00076   else
00077     regcache_raw_supply (regcache, regnum, NULL);
00078 }
00079 
00080 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
00081    this for all registers (including the floating point registers).  */
00082 
00083 static void
00084 inf_child_store_inferior_registers (struct target_ops *ops,
00085                                     struct regcache *regcache, int regnum)
00086 {
00087 }
00088 
00089 static void
00090 inf_child_post_attach (int pid)
00091 {
00092   /* This version of Unix doesn't require a meaningful "post attach"
00093      operation by a debugger.  */
00094 }
00095 
00096 /* Get ready to modify the registers array.  On machines which store
00097    individual registers, this doesn't need to do anything.  On
00098    machines which store all the registers in one fell swoop, this
00099    makes sure that registers contains all the registers from the
00100    program being debugged.  */
00101 
00102 static void
00103 inf_child_prepare_to_store (struct regcache *regcache)
00104 {
00105 }
00106 
00107 static void
00108 inf_child_open (char *arg, int from_tty)
00109 {
00110   error (_("Use the \"run\" command to start a Unix child process."));
00111 }
00112 
00113 static void
00114 inf_child_post_startup_inferior (ptid_t ptid)
00115 {
00116   /* This version of Unix doesn't require a meaningful "post startup
00117      inferior" operation by a debugger.  */
00118 }
00119 
00120 static int
00121 inf_child_follow_fork (struct target_ops *ops, int follow_child,
00122                        int detach_fork)
00123 {
00124   /* This version of Unix doesn't support following fork or vfork
00125      events.  */
00126   return 0;
00127 }
00128 
00129 static int
00130 inf_child_can_run (void)
00131 {
00132   return 1;
00133 }
00134 
00135 static char *
00136 inf_child_pid_to_exec_file (int pid)
00137 {
00138   /* This version of Unix doesn't support translation of a process ID
00139      to the filename of the executable file.  */
00140   return NULL;
00141 }
00142 
00143 
00144 /* Target file operations.  */
00145 
00146 static int
00147 inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
00148 {
00149   int open_flags = 0;
00150 
00151   if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
00152     return -1;
00153 
00154   if (fileio_open_flags & FILEIO_O_CREAT)
00155     open_flags |= O_CREAT;
00156   if (fileio_open_flags & FILEIO_O_EXCL)
00157     open_flags |= O_EXCL;
00158   if (fileio_open_flags & FILEIO_O_TRUNC)
00159     open_flags |= O_TRUNC;
00160   if (fileio_open_flags & FILEIO_O_APPEND)
00161     open_flags |= O_APPEND;
00162   if (fileio_open_flags & FILEIO_O_RDONLY)
00163     open_flags |= O_RDONLY;
00164   if (fileio_open_flags & FILEIO_O_WRONLY)
00165     open_flags |= O_WRONLY;
00166   if (fileio_open_flags & FILEIO_O_RDWR)
00167     open_flags |= O_RDWR;
00168 /* On systems supporting binary and text mode, always open files in
00169    binary mode. */
00170 #ifdef O_BINARY
00171   open_flags |= O_BINARY;
00172 #endif
00173 
00174   *open_flags_p = open_flags;
00175   return 0;
00176 }
00177 
00178 static int
00179 inf_child_errno_to_fileio_error (int errnum)
00180 {
00181   switch (errnum)
00182     {
00183       case EPERM:
00184         return FILEIO_EPERM;
00185       case ENOENT:
00186         return FILEIO_ENOENT;
00187       case EINTR:
00188         return FILEIO_EINTR;
00189       case EIO:
00190         return FILEIO_EIO;
00191       case EBADF:
00192         return FILEIO_EBADF;
00193       case EACCES:
00194         return FILEIO_EACCES;
00195       case EFAULT:
00196         return FILEIO_EFAULT;
00197       case EBUSY:
00198         return FILEIO_EBUSY;
00199       case EEXIST:
00200         return FILEIO_EEXIST;
00201       case ENODEV:
00202         return FILEIO_ENODEV;
00203       case ENOTDIR:
00204         return FILEIO_ENOTDIR;
00205       case EISDIR:
00206         return FILEIO_EISDIR;
00207       case EINVAL:
00208         return FILEIO_EINVAL;
00209       case ENFILE:
00210         return FILEIO_ENFILE;
00211       case EMFILE:
00212         return FILEIO_EMFILE;
00213       case EFBIG:
00214         return FILEIO_EFBIG;
00215       case ENOSPC:
00216         return FILEIO_ENOSPC;
00217       case ESPIPE:
00218         return FILEIO_ESPIPE;
00219       case EROFS:
00220         return FILEIO_EROFS;
00221       case ENOSYS:
00222         return FILEIO_ENOSYS;
00223       case ENAMETOOLONG:
00224         return FILEIO_ENAMETOOLONG;
00225     }
00226   return FILEIO_EUNKNOWN;
00227 }
00228 
00229 /* Open FILENAME on the target, using FLAGS and MODE.  Return a
00230    target file descriptor, or -1 if an error occurs (and set
00231    *TARGET_ERRNO).  */
00232 static int
00233 inf_child_fileio_open (const char *filename, int flags, int mode,
00234                        int *target_errno)
00235 {
00236   int nat_flags;
00237   int fd;
00238 
00239   if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
00240     {
00241       *target_errno = FILEIO_EINVAL;
00242       return -1;
00243     }
00244 
00245   /* We do not need to convert MODE, since the fileio protocol uses
00246      the standard values.  */
00247   fd = gdb_open_cloexec (filename, nat_flags, mode);
00248   if (fd == -1)
00249     *target_errno = inf_child_errno_to_fileio_error (errno);
00250 
00251   return fd;
00252 }
00253 
00254 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
00255    Return the number of bytes written, or -1 if an error occurs
00256    (and set *TARGET_ERRNO).  */
00257 static int
00258 inf_child_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
00259                          ULONGEST offset, int *target_errno)
00260 {
00261   int ret;
00262 
00263 #ifdef HAVE_PWRITE
00264   ret = pwrite (fd, write_buf, len, (long) offset);
00265 #else
00266   ret = -1;
00267 #endif
00268   /* If we have no pwrite or it failed for this file, use lseek/write.  */
00269   if (ret == -1)
00270     {
00271       ret = lseek (fd, (long) offset, SEEK_SET);
00272       if (ret != -1)
00273         ret = write (fd, write_buf, len);
00274     }
00275 
00276   if (ret == -1)
00277     *target_errno = inf_child_errno_to_fileio_error (errno);
00278 
00279   return ret;
00280 }
00281 
00282 /* Read up to LEN bytes FD on the target into READ_BUF.
00283    Return the number of bytes read, or -1 if an error occurs
00284    (and set *TARGET_ERRNO).  */
00285 static int
00286 inf_child_fileio_pread (int fd, gdb_byte *read_buf, int len,
00287                         ULONGEST offset, int *target_errno)
00288 {
00289   int ret;
00290 
00291 #ifdef HAVE_PREAD
00292   ret = pread (fd, read_buf, len, (long) offset);
00293 #else
00294   ret = -1;
00295 #endif
00296   /* If we have no pread or it failed for this file, use lseek/read.  */
00297   if (ret == -1)
00298     {
00299       ret = lseek (fd, (long) offset, SEEK_SET);
00300       if (ret != -1)
00301         ret = read (fd, read_buf, len);
00302     }
00303 
00304   if (ret == -1)
00305     *target_errno = inf_child_errno_to_fileio_error (errno);
00306 
00307   return ret;
00308 }
00309 
00310 /* Close FD on the target.  Return 0, or -1 if an error occurs
00311    (and set *TARGET_ERRNO).  */
00312 static int
00313 inf_child_fileio_close (int fd, int *target_errno)
00314 {
00315   int ret;
00316 
00317   ret = close (fd);
00318   if (ret == -1)
00319     *target_errno = inf_child_errno_to_fileio_error (errno);
00320 
00321   return ret;
00322 }
00323 
00324 /* Unlink FILENAME on the target.  Return 0, or -1 if an error
00325    occurs (and set *TARGET_ERRNO).  */
00326 static int
00327 inf_child_fileio_unlink (const char *filename, int *target_errno)
00328 {
00329   int ret;
00330 
00331   ret = unlink (filename);
00332   if (ret == -1)
00333     *target_errno = inf_child_errno_to_fileio_error (errno);
00334 
00335   return ret;
00336 }
00337 
00338 /* Read value of symbolic link FILENAME on the target.  Return a
00339    null-terminated string allocated via xmalloc, or NULL if an error
00340    occurs (and set *TARGET_ERRNO).  */
00341 static char *
00342 inf_child_fileio_readlink (const char *filename, int *target_errno)
00343 {
00344   /* We support readlink only on systems that also provide a compile-time
00345      maximum path length (PATH_MAX), at least for now.  */
00346 #if defined (HAVE_READLINK) && defined (PATH_MAX)
00347   char buf[PATH_MAX];
00348   int len;
00349   char *ret;
00350 
00351   len = readlink (filename, buf, sizeof buf);
00352   if (len < 0)
00353     {
00354       *target_errno = inf_child_errno_to_fileio_error (errno);
00355       return NULL;
00356     }
00357 
00358   ret = xmalloc (len + 1);
00359   memcpy (ret, buf, len);
00360   ret[len] = '\0';
00361   return ret;
00362 #else
00363   *target_errno = FILEIO_ENOSYS;
00364   return NULL;
00365 #endif
00366 }
00367 
00368 static int
00369 inf_child_use_agent (int use)
00370 {
00371   if (agent_loaded_p ())
00372     {
00373       use_agent = use;
00374       return 1;
00375     }
00376   else
00377     return 0;
00378 }
00379 
00380 static int
00381 inf_child_can_use_agent (void)
00382 {
00383   return agent_loaded_p ();
00384 }
00385 
00386 struct target_ops *
00387 inf_child_target (void)
00388 {
00389   struct target_ops *t = XZALLOC (struct target_ops);
00390 
00391   t->to_shortname = "child";
00392   t->to_longname = "Unix child process";
00393   t->to_doc = "Unix child process (started by the \"run\" command).";
00394   t->to_open = inf_child_open;
00395   t->to_post_attach = inf_child_post_attach;
00396   t->to_fetch_registers = inf_child_fetch_inferior_registers;
00397   t->to_store_registers = inf_child_store_inferior_registers;
00398   t->to_prepare_to_store = inf_child_prepare_to_store;
00399   t->to_insert_breakpoint = memory_insert_breakpoint;
00400   t->to_remove_breakpoint = memory_remove_breakpoint;
00401   t->to_terminal_init = terminal_init_inferior;
00402   t->to_terminal_inferior = terminal_inferior;
00403   t->to_terminal_ours_for_output = terminal_ours_for_output;
00404   t->to_terminal_save_ours = terminal_save_ours;
00405   t->to_terminal_ours = terminal_ours;
00406   t->to_terminal_info = child_terminal_info;
00407   t->to_post_startup_inferior = inf_child_post_startup_inferior;
00408   t->to_follow_fork = inf_child_follow_fork;
00409   t->to_can_run = inf_child_can_run;
00410   t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
00411   t->to_stratum = process_stratum;
00412   t->to_has_all_memory = default_child_has_all_memory;
00413   t->to_has_memory = default_child_has_memory;
00414   t->to_has_stack = default_child_has_stack;
00415   t->to_has_registers = default_child_has_registers;
00416   t->to_has_execution = default_child_has_execution;
00417   t->to_fileio_open = inf_child_fileio_open;
00418   t->to_fileio_pwrite = inf_child_fileio_pwrite;
00419   t->to_fileio_pread = inf_child_fileio_pread;
00420   t->to_fileio_close = inf_child_fileio_close;
00421   t->to_fileio_unlink = inf_child_fileio_unlink;
00422   t->to_fileio_readlink = inf_child_fileio_readlink;
00423   t->to_magic = OPS_MAGIC;
00424   t->to_use_agent = inf_child_use_agent;
00425   t->to_can_use_agent = inf_child_can_use_agent;
00426   return t;
00427 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines