constify inf_child_open_target
[platform/upstream/binutils.git] / gdb / inf-child.c
1 /* Base/prototype target for default child (native) targets.
2
3    Copyright (C) 1988-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* This file provides a common base class/target that all native
21    target implementations extend, by calling inf_child_target to get a
22    new prototype target and then overriding target methods as
23    necessary.  */
24
25 #include "defs.h"
26 #include "regcache.h"
27 #include "memattr.h"
28 #include "symtab.h"
29 #include "target.h"
30 #include "inferior.h"
31 #include <string.h>
32 #include <sys/stat.h>
33 #include "inf-child.h"
34 #include "gdb/fileio.h"
35 #include "agent.h"
36 #include "gdb_wait.h"
37 #include "filestuff.h"
38
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42
43 /* A pointer to what is returned by inf_child_target.  Used by
44    inf_child_open to push the most-derived target in reaction to
45    "target native".  */
46 static struct target_ops *inf_child_ops = NULL;
47
48 /* Helper function for child_wait and the derivatives of child_wait.
49    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
50    translation of that in OURSTATUS.  */
51 void
52 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
53 {
54   if (WIFEXITED (hoststatus))
55     {
56       ourstatus->kind = TARGET_WAITKIND_EXITED;
57       ourstatus->value.integer = WEXITSTATUS (hoststatus);
58     }
59   else if (!WIFSTOPPED (hoststatus))
60     {
61       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
62       ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
63     }
64   else
65     {
66       ourstatus->kind = TARGET_WAITKIND_STOPPED;
67       ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
68     }
69 }
70
71 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
72    for all registers.  */
73
74 static void
75 inf_child_fetch_inferior_registers (struct target_ops *ops,
76                                     struct regcache *regcache, int regnum)
77 {
78   if (regnum == -1)
79     {
80       for (regnum = 0;
81            regnum < gdbarch_num_regs (get_regcache_arch (regcache));
82            regnum++)
83         regcache_raw_supply (regcache, regnum, NULL);
84     }
85   else
86     regcache_raw_supply (regcache, regnum, NULL);
87 }
88
89 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
90    this for all registers (including the floating point registers).  */
91
92 static void
93 inf_child_store_inferior_registers (struct target_ops *ops,
94                                     struct regcache *regcache, int regnum)
95 {
96 }
97
98 static void
99 inf_child_post_attach (struct target_ops *self, int pid)
100 {
101   /* This target doesn't require a meaningful "post attach" operation
102      by a debugger.  */
103 }
104
105 /* Get ready to modify the registers array.  On machines which store
106    individual registers, this doesn't need to do anything.  On
107    machines which store all the registers in one fell swoop, this
108    makes sure that registers contains all the registers from the
109    program being debugged.  */
110
111 static void
112 inf_child_prepare_to_store (struct target_ops *self,
113                             struct regcache *regcache)
114 {
115 }
116
117 /* True if the user did "target native".  In that case, we won't
118    unpush the child target automatically when the last inferior is
119    gone.  */
120 static int inf_child_explicitly_opened;
121
122 /* See inf-child.h.  */
123
124 void
125 inf_child_open_target (struct target_ops *target, const char *arg,
126                        int from_tty)
127 {
128   target_preopen (from_tty);
129   push_target (target);
130   inf_child_explicitly_opened = 1;
131   if (from_tty)
132     printf_filtered ("Done.  Use the \"run\" command to start a process.\n");
133 }
134
135 static void
136 inf_child_open (char *arg, int from_tty)
137 {
138   inf_child_open_target (inf_child_ops, arg, from_tty);
139 }
140
141 /* Implement the to_disconnect target_ops method.  */
142
143 static void
144 inf_child_disconnect (struct target_ops *target, const char *args, int from_tty)
145 {
146   if (args != NULL)
147     error (_("Argument given to \"disconnect\"."));
148
149   /* This offers to detach/kill current inferiors, and then pops all
150      targets.  */
151   target_preopen (from_tty);
152 }
153
154 /* Implement the to_close target_ops method.  */
155
156 static void
157 inf_child_close (struct target_ops *target)
158 {
159   /* In case we were forcibly closed.  */
160   inf_child_explicitly_opened = 0;
161 }
162
163 void
164 inf_child_mourn_inferior (struct target_ops *ops)
165 {
166   generic_mourn_inferior ();
167   inf_child_maybe_unpush_target (ops);
168 }
169
170 /* See inf-child.h.  */
171
172 void
173 inf_child_maybe_unpush_target (struct target_ops *ops)
174 {
175   if (!inf_child_explicitly_opened && !have_inferiors ())
176     unpush_target (ops);
177 }
178
179 static void
180 inf_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
181 {
182   /* This target doesn't require a meaningful "post startup inferior"
183      operation by a debugger.  */
184 }
185
186 static int
187 inf_child_follow_fork (struct target_ops *ops, int follow_child,
188                        int detach_fork)
189 {
190   /* This target doesn't support following fork or vfork events.  */
191   return 0;
192 }
193
194 static int
195 inf_child_can_run (struct target_ops *self)
196 {
197   return 1;
198 }
199
200 static char *
201 inf_child_pid_to_exec_file (struct target_ops *self, int pid)
202 {
203   /* This target doesn't support translation of a process ID to the
204      filename of the executable file.  */
205   return NULL;
206 }
207
208
209 /* Target file operations.  */
210
211 static int
212 inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
213 {
214   int open_flags = 0;
215
216   if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
217     return -1;
218
219   if (fileio_open_flags & FILEIO_O_CREAT)
220     open_flags |= O_CREAT;
221   if (fileio_open_flags & FILEIO_O_EXCL)
222     open_flags |= O_EXCL;
223   if (fileio_open_flags & FILEIO_O_TRUNC)
224     open_flags |= O_TRUNC;
225   if (fileio_open_flags & FILEIO_O_APPEND)
226     open_flags |= O_APPEND;
227   if (fileio_open_flags & FILEIO_O_RDONLY)
228     open_flags |= O_RDONLY;
229   if (fileio_open_flags & FILEIO_O_WRONLY)
230     open_flags |= O_WRONLY;
231   if (fileio_open_flags & FILEIO_O_RDWR)
232     open_flags |= O_RDWR;
233 /* On systems supporting binary and text mode, always open files in
234    binary mode. */
235 #ifdef O_BINARY
236   open_flags |= O_BINARY;
237 #endif
238
239   *open_flags_p = open_flags;
240   return 0;
241 }
242
243 static int
244 inf_child_errno_to_fileio_error (int errnum)
245 {
246   switch (errnum)
247     {
248       case EPERM:
249         return FILEIO_EPERM;
250       case ENOENT:
251         return FILEIO_ENOENT;
252       case EINTR:
253         return FILEIO_EINTR;
254       case EIO:
255         return FILEIO_EIO;
256       case EBADF:
257         return FILEIO_EBADF;
258       case EACCES:
259         return FILEIO_EACCES;
260       case EFAULT:
261         return FILEIO_EFAULT;
262       case EBUSY:
263         return FILEIO_EBUSY;
264       case EEXIST:
265         return FILEIO_EEXIST;
266       case ENODEV:
267         return FILEIO_ENODEV;
268       case ENOTDIR:
269         return FILEIO_ENOTDIR;
270       case EISDIR:
271         return FILEIO_EISDIR;
272       case EINVAL:
273         return FILEIO_EINVAL;
274       case ENFILE:
275         return FILEIO_ENFILE;
276       case EMFILE:
277         return FILEIO_EMFILE;
278       case EFBIG:
279         return FILEIO_EFBIG;
280       case ENOSPC:
281         return FILEIO_ENOSPC;
282       case ESPIPE:
283         return FILEIO_ESPIPE;
284       case EROFS:
285         return FILEIO_EROFS;
286       case ENOSYS:
287         return FILEIO_ENOSYS;
288       case ENAMETOOLONG:
289         return FILEIO_ENAMETOOLONG;
290     }
291   return FILEIO_EUNKNOWN;
292 }
293
294 /* Open FILENAME on the target, using FLAGS and MODE.  Return a
295    target file descriptor, or -1 if an error occurs (and set
296    *TARGET_ERRNO).  */
297 static int
298 inf_child_fileio_open (struct target_ops *self,
299                        const char *filename, int flags, int mode,
300                        int *target_errno)
301 {
302   int nat_flags;
303   int fd;
304
305   if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
306     {
307       *target_errno = FILEIO_EINVAL;
308       return -1;
309     }
310
311   /* We do not need to convert MODE, since the fileio protocol uses
312      the standard values.  */
313   fd = gdb_open_cloexec (filename, nat_flags, mode);
314   if (fd == -1)
315     *target_errno = inf_child_errno_to_fileio_error (errno);
316
317   return fd;
318 }
319
320 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
321    Return the number of bytes written, or -1 if an error occurs
322    (and set *TARGET_ERRNO).  */
323 static int
324 inf_child_fileio_pwrite (struct target_ops *self,
325                          int fd, const gdb_byte *write_buf, int len,
326                          ULONGEST offset, int *target_errno)
327 {
328   int ret;
329
330 #ifdef HAVE_PWRITE
331   ret = pwrite (fd, write_buf, len, (long) offset);
332 #else
333   ret = -1;
334 #endif
335   /* If we have no pwrite or it failed for this file, use lseek/write.  */
336   if (ret == -1)
337     {
338       ret = lseek (fd, (long) offset, SEEK_SET);
339       if (ret != -1)
340         ret = write (fd, write_buf, len);
341     }
342
343   if (ret == -1)
344     *target_errno = inf_child_errno_to_fileio_error (errno);
345
346   return ret;
347 }
348
349 /* Read up to LEN bytes FD on the target into READ_BUF.
350    Return the number of bytes read, or -1 if an error occurs
351    (and set *TARGET_ERRNO).  */
352 static int
353 inf_child_fileio_pread (struct target_ops *self,
354                         int fd, gdb_byte *read_buf, int len,
355                         ULONGEST offset, int *target_errno)
356 {
357   int ret;
358
359 #ifdef HAVE_PREAD
360   ret = pread (fd, read_buf, len, (long) offset);
361 #else
362   ret = -1;
363 #endif
364   /* If we have no pread or it failed for this file, use lseek/read.  */
365   if (ret == -1)
366     {
367       ret = lseek (fd, (long) offset, SEEK_SET);
368       if (ret != -1)
369         ret = read (fd, read_buf, len);
370     }
371
372   if (ret == -1)
373     *target_errno = inf_child_errno_to_fileio_error (errno);
374
375   return ret;
376 }
377
378 /* Close FD on the target.  Return 0, or -1 if an error occurs
379    (and set *TARGET_ERRNO).  */
380 static int
381 inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno)
382 {
383   int ret;
384
385   ret = close (fd);
386   if (ret == -1)
387     *target_errno = inf_child_errno_to_fileio_error (errno);
388
389   return ret;
390 }
391
392 /* Unlink FILENAME on the target.  Return 0, or -1 if an error
393    occurs (and set *TARGET_ERRNO).  */
394 static int
395 inf_child_fileio_unlink (struct target_ops *self,
396                          const char *filename, int *target_errno)
397 {
398   int ret;
399
400   ret = unlink (filename);
401   if (ret == -1)
402     *target_errno = inf_child_errno_to_fileio_error (errno);
403
404   return ret;
405 }
406
407 /* Read value of symbolic link FILENAME on the target.  Return a
408    null-terminated string allocated via xmalloc, or NULL if an error
409    occurs (and set *TARGET_ERRNO).  */
410 static char *
411 inf_child_fileio_readlink (struct target_ops *self,
412                            const char *filename, int *target_errno)
413 {
414   /* We support readlink only on systems that also provide a compile-time
415      maximum path length (PATH_MAX), at least for now.  */
416 #if defined (HAVE_READLINK) && defined (PATH_MAX)
417   char buf[PATH_MAX];
418   int len;
419   char *ret;
420
421   len = readlink (filename, buf, sizeof buf);
422   if (len < 0)
423     {
424       *target_errno = inf_child_errno_to_fileio_error (errno);
425       return NULL;
426     }
427
428   ret = xmalloc (len + 1);
429   memcpy (ret, buf, len);
430   ret[len] = '\0';
431   return ret;
432 #else
433   *target_errno = FILEIO_ENOSYS;
434   return NULL;
435 #endif
436 }
437
438 static int
439 inf_child_use_agent (struct target_ops *self, int use)
440 {
441   if (agent_loaded_p ())
442     {
443       use_agent = use;
444       return 1;
445     }
446   else
447     return 0;
448 }
449
450 static int
451 inf_child_can_use_agent (struct target_ops *self)
452 {
453   return agent_loaded_p ();
454 }
455
456 /* Default implementation of the to_can_async_p and
457    to_supports_non_stop methods.  */
458
459 static int
460 return_zero (struct target_ops *ignore)
461 {
462   return 0;
463 }
464
465 struct target_ops *
466 inf_child_target (void)
467 {
468   struct target_ops *t = XCNEW (struct target_ops);
469
470   t->to_shortname = "native";
471   t->to_longname = "Native process";
472   t->to_doc = "Native process (started by the \"run\" command).";
473   t->to_open = inf_child_open;
474   t->to_close = inf_child_close;
475   t->to_disconnect = inf_child_disconnect;
476   t->to_post_attach = inf_child_post_attach;
477   t->to_fetch_registers = inf_child_fetch_inferior_registers;
478   t->to_store_registers = inf_child_store_inferior_registers;
479   t->to_prepare_to_store = inf_child_prepare_to_store;
480   t->to_insert_breakpoint = memory_insert_breakpoint;
481   t->to_remove_breakpoint = memory_remove_breakpoint;
482   t->to_terminal_init = child_terminal_init;
483   t->to_terminal_inferior = child_terminal_inferior;
484   t->to_terminal_ours_for_output = child_terminal_ours_for_output;
485   t->to_terminal_save_ours = child_terminal_save_ours;
486   t->to_terminal_ours = child_terminal_ours;
487   t->to_terminal_info = child_terminal_info;
488   t->to_post_startup_inferior = inf_child_post_startup_inferior;
489   t->to_follow_fork = inf_child_follow_fork;
490   t->to_can_run = inf_child_can_run;
491   /* We must default these because they must be implemented by any
492      target that can run.  */
493   t->to_can_async_p = return_zero;
494   t->to_supports_non_stop = return_zero;
495   t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
496   t->to_stratum = process_stratum;
497   t->to_has_all_memory = default_child_has_all_memory;
498   t->to_has_memory = default_child_has_memory;
499   t->to_has_stack = default_child_has_stack;
500   t->to_has_registers = default_child_has_registers;
501   t->to_has_execution = default_child_has_execution;
502   t->to_fileio_open = inf_child_fileio_open;
503   t->to_fileio_pwrite = inf_child_fileio_pwrite;
504   t->to_fileio_pread = inf_child_fileio_pread;
505   t->to_fileio_close = inf_child_fileio_close;
506   t->to_fileio_unlink = inf_child_fileio_unlink;
507   t->to_fileio_readlink = inf_child_fileio_readlink;
508   t->to_magic = OPS_MAGIC;
509   t->to_use_agent = inf_child_use_agent;
510   t->to_can_use_agent = inf_child_can_use_agent;
511
512   /* Store a pointer so we can push the most-derived target from
513      inf_child_open.  */
514   inf_child_ops = t;
515
516   return t;
517 }