2012-01-20 Pedro Alves <palves@redhat.com>
[platform/upstream/binutils.git] / gdb / inf-child.c
1 /* Default child (native) target interface, for GDB when running under
2    Unix.
3
4    Copyright (C) 1988-1996, 1998-2002, 2004-2005, 2007-2012 Free
5    Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "regcache.h"
24 #include "memattr.h"
25 #include "symtab.h"
26 #include "target.h"
27 #include "inferior.h"
28 #include "gdb_string.h"
29 #include "inf-child.h"
30 #include "gdb/fileio.h"
31
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
38    for all registers.  */
39
40 static void
41 inf_child_fetch_inferior_registers (struct target_ops *ops,
42                                     struct regcache *regcache, int regnum)
43 {
44   if (regnum == -1)
45     {
46       for (regnum = 0;
47            regnum < gdbarch_num_regs (get_regcache_arch (regcache));
48            regnum++)
49         regcache_raw_supply (regcache, regnum, NULL);
50     }
51   else
52     regcache_raw_supply (regcache, regnum, NULL);
53 }
54
55 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
56    this for all registers (including the floating point registers).  */
57
58 static void
59 inf_child_store_inferior_registers (struct target_ops *ops,
60                                     struct regcache *regcache, int regnum)
61 {
62 }
63
64 static void
65 inf_child_post_attach (int pid)
66 {
67   /* This version of Unix doesn't require a meaningful "post attach"
68      operation by a debugger.  */
69 }
70
71 /* Get ready to modify the registers array.  On machines which store
72    individual registers, this doesn't need to do anything.  On
73    machines which store all the registers in one fell swoop, this
74    makes sure that registers contains all the registers from the
75    program being debugged.  */
76
77 static void
78 inf_child_prepare_to_store (struct regcache *regcache)
79 {
80 }
81
82 static void
83 inf_child_open (char *arg, int from_tty)
84 {
85   error (_("Use the \"run\" command to start a Unix child process."));
86 }
87
88 static void
89 inf_child_post_startup_inferior (ptid_t ptid)
90 {
91   /* This version of Unix doesn't require a meaningful "post startup
92      inferior" operation by a debugger.  */
93 }
94
95 static int
96 inf_child_follow_fork (struct target_ops *ops, int follow_child)
97 {
98   /* This version of Unix doesn't support following fork or vfork
99      events.  */
100   return 0;
101 }
102
103 static int
104 inf_child_can_run (void)
105 {
106   return 1;
107 }
108
109 static char *
110 inf_child_pid_to_exec_file (int pid)
111 {
112   /* This version of Unix doesn't support translation of a process ID
113      to the filename of the executable file.  */
114   return NULL;
115 }
116
117
118 /* Target file operations.  */
119
120 static int
121 inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
122 {
123   int open_flags = 0;
124
125   if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
126     return -1;
127
128   if (fileio_open_flags & FILEIO_O_CREAT)
129     open_flags |= O_CREAT;
130   if (fileio_open_flags & FILEIO_O_EXCL)
131     open_flags |= O_EXCL;
132   if (fileio_open_flags & FILEIO_O_TRUNC)
133     open_flags |= O_TRUNC;
134   if (fileio_open_flags & FILEIO_O_APPEND)
135     open_flags |= O_APPEND;
136   if (fileio_open_flags & FILEIO_O_RDONLY)
137     open_flags |= O_RDONLY;
138   if (fileio_open_flags & FILEIO_O_WRONLY)
139     open_flags |= O_WRONLY;
140   if (fileio_open_flags & FILEIO_O_RDWR)
141     open_flags |= O_RDWR;
142 /* On systems supporting binary and text mode, always open files in
143    binary mode. */
144 #ifdef O_BINARY
145   open_flags |= O_BINARY;
146 #endif
147
148   *open_flags_p = open_flags;
149   return 0;
150 }
151
152 static int
153 inf_child_errno_to_fileio_error (int errnum)
154 {
155   switch (errnum)
156     {
157       case EPERM:
158         return FILEIO_EPERM;
159       case ENOENT:
160         return FILEIO_ENOENT;
161       case EINTR:
162         return FILEIO_EINTR;
163       case EIO:
164         return FILEIO_EIO;
165       case EBADF:
166         return FILEIO_EBADF;
167       case EACCES:
168         return FILEIO_EACCES;
169       case EFAULT:
170         return FILEIO_EFAULT;
171       case EBUSY:
172         return FILEIO_EBUSY;
173       case EEXIST:
174         return FILEIO_EEXIST;
175       case ENODEV:
176         return FILEIO_ENODEV;
177       case ENOTDIR:
178         return FILEIO_ENOTDIR;
179       case EISDIR:
180         return FILEIO_EISDIR;
181       case EINVAL:
182         return FILEIO_EINVAL;
183       case ENFILE:
184         return FILEIO_ENFILE;
185       case EMFILE:
186         return FILEIO_EMFILE;
187       case EFBIG:
188         return FILEIO_EFBIG;
189       case ENOSPC:
190         return FILEIO_ENOSPC;
191       case ESPIPE:
192         return FILEIO_ESPIPE;
193       case EROFS:
194         return FILEIO_EROFS;
195       case ENOSYS:
196         return FILEIO_ENOSYS;
197       case ENAMETOOLONG:
198         return FILEIO_ENAMETOOLONG;
199     }
200   return FILEIO_EUNKNOWN;
201 }
202
203 /* Open FILENAME on the target, using FLAGS and MODE.  Return a
204    target file descriptor, or -1 if an error occurs (and set
205    *TARGET_ERRNO).  */
206 static int
207 inf_child_fileio_open (const char *filename, int flags, int mode,
208                        int *target_errno)
209 {
210   int nat_flags;
211   int fd;
212
213   if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
214     {
215       *target_errno = FILEIO_EINVAL;
216       return -1;
217     }
218
219   /* We do not need to convert MODE, since the fileio protocol uses
220      the standard values.  */
221   fd = open (filename, nat_flags, mode);
222   if (fd == -1)
223     *target_errno = inf_child_errno_to_fileio_error (errno);
224
225   return fd;
226 }
227
228 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
229    Return the number of bytes written, or -1 if an error occurs
230    (and set *TARGET_ERRNO).  */
231 static int
232 inf_child_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
233                          ULONGEST offset, int *target_errno)
234 {
235   int ret;
236
237 #ifdef HAVE_PWRITE
238   ret = pwrite (fd, write_buf, len, (long) offset);
239 #else
240   ret = lseek (fd, (long) offset, SEEK_SET);
241   if (ret != -1)
242     ret = write (fd, write_buf, len);
243 #endif
244
245   if (ret == -1)
246     *target_errno = inf_child_errno_to_fileio_error (errno);
247
248   return ret;
249 }
250
251 /* Read up to LEN bytes FD on the target into READ_BUF.
252    Return the number of bytes read, or -1 if an error occurs
253    (and set *TARGET_ERRNO).  */
254 static int
255 inf_child_fileio_pread (int fd, gdb_byte *read_buf, int len,
256                         ULONGEST offset, int *target_errno)
257 {
258   int ret;
259
260 #ifdef HAVE_PREAD
261   ret = pread (fd, read_buf, len, (long) offset);
262 #else
263   ret = lseek (fd, (long) offset, SEEK_SET);
264   if (ret != -1)
265     ret = read (fd, read_buf, len);
266 #endif
267
268   if (ret == -1)
269     *target_errno = inf_child_errno_to_fileio_error (errno);
270
271   return ret;
272 }
273
274 /* Close FD on the target.  Return 0, or -1 if an error occurs
275    (and set *TARGET_ERRNO).  */
276 static int
277 inf_child_fileio_close (int fd, int *target_errno)
278 {
279   int ret;
280
281   ret = close (fd);
282   if (ret == -1)
283     *target_errno = inf_child_errno_to_fileio_error (errno);
284
285   return ret;
286 }
287
288 /* Unlink FILENAME on the target.  Return 0, or -1 if an error
289    occurs (and set *TARGET_ERRNO).  */
290 static int
291 inf_child_fileio_unlink (const char *filename, int *target_errno)
292 {
293   int ret;
294
295   ret = unlink (filename);
296   if (ret == -1)
297     *target_errno = inf_child_errno_to_fileio_error (errno);
298
299   return ret;
300 }
301
302
303 struct target_ops *
304 inf_child_target (void)
305 {
306   struct target_ops *t = XZALLOC (struct target_ops);
307
308   t->to_shortname = "child";
309   t->to_longname = "Unix child process";
310   t->to_doc = "Unix child process (started by the \"run\" command).";
311   t->to_open = inf_child_open;
312   t->to_post_attach = inf_child_post_attach;
313   t->to_fetch_registers = inf_child_fetch_inferior_registers;
314   t->to_store_registers = inf_child_store_inferior_registers;
315   t->to_prepare_to_store = inf_child_prepare_to_store;
316   t->to_insert_breakpoint = memory_insert_breakpoint;
317   t->to_remove_breakpoint = memory_remove_breakpoint;
318   t->to_terminal_init = terminal_init_inferior;
319   t->to_terminal_inferior = terminal_inferior;
320   t->to_terminal_ours_for_output = terminal_ours_for_output;
321   t->to_terminal_save_ours = terminal_save_ours;
322   t->to_terminal_ours = terminal_ours;
323   t->to_terminal_info = child_terminal_info;
324   t->to_post_startup_inferior = inf_child_post_startup_inferior;
325   t->to_follow_fork = inf_child_follow_fork;
326   t->to_can_run = inf_child_can_run;
327   t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
328   t->to_stratum = process_stratum;
329   t->to_has_all_memory = default_child_has_all_memory;
330   t->to_has_memory = default_child_has_memory;
331   t->to_has_stack = default_child_has_stack;
332   t->to_has_registers = default_child_has_registers;
333   t->to_has_execution = default_child_has_execution;
334   t->to_fileio_open = inf_child_fileio_open;
335   t->to_fileio_pwrite = inf_child_fileio_pwrite;
336   t->to_fileio_pread = inf_child_fileio_pread;
337   t->to_fileio_close = inf_child_fileio_close;
338   t->to_fileio_unlink = inf_child_fileio_unlink;
339   t->to_magic = OPS_MAGIC;
340   return t;
341 }