1 /* Base/prototype target for default child (native) targets.
3 Copyright (C) 1988-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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
32 #include "inf-child.h"
33 #include "gdb/fileio.h"
36 #include "filestuff.h"
38 #include <sys/types.h>
42 /* A pointer to what is returned by inf_child_target. Used by
43 inf_child_open to push the most-derived target in reaction to
45 static struct target_ops *inf_child_ops = NULL;
47 /* Helper function for child_wait and the derivatives of child_wait.
48 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
49 translation of that in OURSTATUS. */
51 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
53 if (WIFEXITED (hoststatus))
55 ourstatus->kind = TARGET_WAITKIND_EXITED;
56 ourstatus->value.integer = WEXITSTATUS (hoststatus);
58 else if (!WIFSTOPPED (hoststatus))
60 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
61 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
65 ourstatus->kind = TARGET_WAITKIND_STOPPED;
66 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
70 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
74 inf_child_fetch_inferior_registers (struct target_ops *ops,
75 struct regcache *regcache, int regnum)
80 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
82 regcache_raw_supply (regcache, regnum, NULL);
85 regcache_raw_supply (regcache, regnum, NULL);
88 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
89 this for all registers (including the floating point registers). */
92 inf_child_store_inferior_registers (struct target_ops *ops,
93 struct regcache *regcache, int regnum)
98 inf_child_post_attach (struct target_ops *self, int pid)
100 /* This target doesn't require a meaningful "post attach" operation
104 /* Get ready to modify the registers array. On machines which store
105 individual registers, this doesn't need to do anything. On
106 machines which store all the registers in one fell swoop, this
107 makes sure that registers contains all the registers from the
108 program being debugged. */
111 inf_child_prepare_to_store (struct target_ops *self,
112 struct regcache *regcache)
116 /* True if the user did "target native". In that case, we won't
117 unpush the child target automatically when the last inferior is
119 static int inf_child_explicitly_opened;
121 /* See inf-child.h. */
124 inf_child_open_target (struct target_ops *target, const char *arg,
127 target_preopen (from_tty);
128 push_target (target);
129 inf_child_explicitly_opened = 1;
131 printf_filtered ("Done. Use the \"run\" command to start a process.\n");
135 inf_child_open (const char *arg, int from_tty)
137 inf_child_open_target (inf_child_ops, arg, from_tty);
140 /* Implement the to_disconnect target_ops method. */
143 inf_child_disconnect (struct target_ops *target, const char *args, int from_tty)
146 error (_("Argument given to \"disconnect\"."));
148 /* This offers to detach/kill current inferiors, and then pops all
150 target_preopen (from_tty);
153 /* Implement the to_close target_ops method. */
156 inf_child_close (struct target_ops *target)
158 /* In case we were forcibly closed. */
159 inf_child_explicitly_opened = 0;
163 inf_child_mourn_inferior (struct target_ops *ops)
165 generic_mourn_inferior ();
166 inf_child_maybe_unpush_target (ops);
169 /* See inf-child.h. */
172 inf_child_maybe_unpush_target (struct target_ops *ops)
174 if (!inf_child_explicitly_opened && !have_inferiors ())
179 inf_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
181 /* This target doesn't require a meaningful "post startup inferior"
182 operation by a debugger. */
186 inf_child_follow_fork (struct target_ops *ops, int follow_child,
189 /* This target doesn't support following fork or vfork events. */
194 inf_child_can_run (struct target_ops *self)
200 inf_child_pid_to_exec_file (struct target_ops *self, int pid)
202 /* This target doesn't support translation of a process ID to the
203 filename of the executable file. */
208 /* Target file operations. */
211 inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
215 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
218 if (fileio_open_flags & FILEIO_O_CREAT)
219 open_flags |= O_CREAT;
220 if (fileio_open_flags & FILEIO_O_EXCL)
221 open_flags |= O_EXCL;
222 if (fileio_open_flags & FILEIO_O_TRUNC)
223 open_flags |= O_TRUNC;
224 if (fileio_open_flags & FILEIO_O_APPEND)
225 open_flags |= O_APPEND;
226 if (fileio_open_flags & FILEIO_O_RDONLY)
227 open_flags |= O_RDONLY;
228 if (fileio_open_flags & FILEIO_O_WRONLY)
229 open_flags |= O_WRONLY;
230 if (fileio_open_flags & FILEIO_O_RDWR)
231 open_flags |= O_RDWR;
232 /* On systems supporting binary and text mode, always open files in
235 open_flags |= O_BINARY;
238 *open_flags_p = open_flags;
243 inf_child_errno_to_fileio_error (int errnum)
250 return FILEIO_ENOENT;
258 return FILEIO_EACCES;
260 return FILEIO_EFAULT;
264 return FILEIO_EEXIST;
266 return FILEIO_ENODEV;
268 return FILEIO_ENOTDIR;
270 return FILEIO_EISDIR;
272 return FILEIO_EINVAL;
274 return FILEIO_ENFILE;
276 return FILEIO_EMFILE;
280 return FILEIO_ENOSPC;
282 return FILEIO_ESPIPE;
286 return FILEIO_ENOSYS;
288 return FILEIO_ENAMETOOLONG;
290 return FILEIO_EUNKNOWN;
293 /* Open FILENAME on the target, using FLAGS and MODE. Return a
294 target file descriptor, or -1 if an error occurs (and set
297 inf_child_fileio_open (struct target_ops *self,
298 const char *filename, int flags, int mode,
304 if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
306 *target_errno = FILEIO_EINVAL;
310 /* We do not need to convert MODE, since the fileio protocol uses
311 the standard values. */
312 fd = gdb_open_cloexec (filename, nat_flags, mode);
314 *target_errno = inf_child_errno_to_fileio_error (errno);
319 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
320 Return the number of bytes written, or -1 if an error occurs
321 (and set *TARGET_ERRNO). */
323 inf_child_fileio_pwrite (struct target_ops *self,
324 int fd, const gdb_byte *write_buf, int len,
325 ULONGEST offset, int *target_errno)
330 ret = pwrite (fd, write_buf, len, (long) offset);
334 /* If we have no pwrite or it failed for this file, use lseek/write. */
337 ret = lseek (fd, (long) offset, SEEK_SET);
339 ret = write (fd, write_buf, len);
343 *target_errno = inf_child_errno_to_fileio_error (errno);
348 /* Read up to LEN bytes FD on the target into READ_BUF.
349 Return the number of bytes read, or -1 if an error occurs
350 (and set *TARGET_ERRNO). */
352 inf_child_fileio_pread (struct target_ops *self,
353 int fd, gdb_byte *read_buf, int len,
354 ULONGEST offset, int *target_errno)
359 ret = pread (fd, read_buf, len, (long) offset);
363 /* If we have no pread or it failed for this file, use lseek/read. */
366 ret = lseek (fd, (long) offset, SEEK_SET);
368 ret = read (fd, read_buf, len);
372 *target_errno = inf_child_errno_to_fileio_error (errno);
377 /* Close FD on the target. Return 0, or -1 if an error occurs
378 (and set *TARGET_ERRNO). */
380 inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno)
386 *target_errno = inf_child_errno_to_fileio_error (errno);
391 /* Unlink FILENAME on the target. Return 0, or -1 if an error
392 occurs (and set *TARGET_ERRNO). */
394 inf_child_fileio_unlink (struct target_ops *self,
395 const char *filename, int *target_errno)
399 ret = unlink (filename);
401 *target_errno = inf_child_errno_to_fileio_error (errno);
406 /* Read value of symbolic link FILENAME on the target. Return a
407 null-terminated string allocated via xmalloc, or NULL if an error
408 occurs (and set *TARGET_ERRNO). */
410 inf_child_fileio_readlink (struct target_ops *self,
411 const char *filename, int *target_errno)
413 /* We support readlink only on systems that also provide a compile-time
414 maximum path length (PATH_MAX), at least for now. */
415 #if defined (HAVE_READLINK) && defined (PATH_MAX)
420 len = readlink (filename, buf, sizeof buf);
423 *target_errno = inf_child_errno_to_fileio_error (errno);
427 ret = xmalloc (len + 1);
428 memcpy (ret, buf, len);
432 *target_errno = FILEIO_ENOSYS;
438 inf_child_use_agent (struct target_ops *self, int use)
440 if (agent_loaded_p ())
450 inf_child_can_use_agent (struct target_ops *self)
452 return agent_loaded_p ();
455 /* Default implementation of the to_can_async_p and
456 to_supports_non_stop methods. */
459 return_zero (struct target_ops *ignore)
465 inf_child_target (void)
467 struct target_ops *t = XCNEW (struct target_ops);
469 t->to_shortname = "native";
470 t->to_longname = "Native process";
471 t->to_doc = "Native process (started by the \"run\" command).";
472 t->to_open = inf_child_open;
473 t->to_close = inf_child_close;
474 t->to_disconnect = inf_child_disconnect;
475 t->to_post_attach = inf_child_post_attach;
476 t->to_fetch_registers = inf_child_fetch_inferior_registers;
477 t->to_store_registers = inf_child_store_inferior_registers;
478 t->to_prepare_to_store = inf_child_prepare_to_store;
479 t->to_insert_breakpoint = memory_insert_breakpoint;
480 t->to_remove_breakpoint = memory_remove_breakpoint;
481 t->to_terminal_init = child_terminal_init;
482 t->to_terminal_inferior = child_terminal_inferior;
483 t->to_terminal_ours_for_output = child_terminal_ours_for_output;
484 t->to_terminal_ours = child_terminal_ours;
485 t->to_terminal_info = child_terminal_info;
486 t->to_post_startup_inferior = inf_child_post_startup_inferior;
487 t->to_follow_fork = inf_child_follow_fork;
488 t->to_can_run = inf_child_can_run;
489 /* We must default these because they must be implemented by any
490 target that can run. */
491 t->to_can_async_p = return_zero;
492 t->to_supports_non_stop = return_zero;
493 t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
494 t->to_stratum = process_stratum;
495 t->to_has_all_memory = default_child_has_all_memory;
496 t->to_has_memory = default_child_has_memory;
497 t->to_has_stack = default_child_has_stack;
498 t->to_has_registers = default_child_has_registers;
499 t->to_has_execution = default_child_has_execution;
500 t->to_fileio_open = inf_child_fileio_open;
501 t->to_fileio_pwrite = inf_child_fileio_pwrite;
502 t->to_fileio_pread = inf_child_fileio_pread;
503 t->to_fileio_close = inf_child_fileio_close;
504 t->to_fileio_unlink = inf_child_fileio_unlink;
505 t->to_fileio_readlink = inf_child_fileio_readlink;
506 t->to_magic = OPS_MAGIC;
507 t->to_use_agent = inf_child_use_agent;
508 t->to_can_use_agent = inf_child_can_use_agent;
510 /* Store a pointer so we can push the most-derived target from