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
33 #include "inf-child.h"
34 #include "gdb/fileio.h"
37 #include "filestuff.h"
39 #include <sys/types.h>
43 /* Helper function for child_wait and the derivatives of child_wait.
44 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
45 translation of that in OURSTATUS. */
47 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
49 if (WIFEXITED (hoststatus))
51 ourstatus->kind = TARGET_WAITKIND_EXITED;
52 ourstatus->value.integer = WEXITSTATUS (hoststatus);
54 else if (!WIFSTOPPED (hoststatus))
56 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
57 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
61 ourstatus->kind = TARGET_WAITKIND_STOPPED;
62 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
66 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
70 inf_child_fetch_inferior_registers (struct target_ops *ops,
71 struct regcache *regcache, int regnum)
76 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
78 regcache_raw_supply (regcache, regnum, NULL);
81 regcache_raw_supply (regcache, regnum, NULL);
84 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
85 this for all registers (including the floating point registers). */
88 inf_child_store_inferior_registers (struct target_ops *ops,
89 struct regcache *regcache, int regnum)
94 inf_child_post_attach (struct target_ops *self, int pid)
96 /* This target doesn't require a meaningful "post attach" operation
100 /* Get ready to modify the registers array. On machines which store
101 individual registers, this doesn't need to do anything. On
102 machines which store all the registers in one fell swoop, this
103 makes sure that registers contains all the registers from the
104 program being debugged. */
107 inf_child_prepare_to_store (struct target_ops *self,
108 struct regcache *regcache)
113 inf_child_open (char *arg, int from_tty)
115 error (_("Use the \"run\" command to start a Unix child process."));
119 inf_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
121 /* This target doesn't require a meaningful "post startup inferior"
122 operation by a debugger. */
126 inf_child_follow_fork (struct target_ops *ops, int follow_child,
129 /* This target doesn't support following fork or vfork events. */
134 inf_child_can_run (struct target_ops *self)
140 inf_child_pid_to_exec_file (struct target_ops *self, int pid)
142 /* This target doesn't support translation of a process ID to the
143 filename of the executable file. */
148 /* Target file operations. */
151 inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
155 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
158 if (fileio_open_flags & FILEIO_O_CREAT)
159 open_flags |= O_CREAT;
160 if (fileio_open_flags & FILEIO_O_EXCL)
161 open_flags |= O_EXCL;
162 if (fileio_open_flags & FILEIO_O_TRUNC)
163 open_flags |= O_TRUNC;
164 if (fileio_open_flags & FILEIO_O_APPEND)
165 open_flags |= O_APPEND;
166 if (fileio_open_flags & FILEIO_O_RDONLY)
167 open_flags |= O_RDONLY;
168 if (fileio_open_flags & FILEIO_O_WRONLY)
169 open_flags |= O_WRONLY;
170 if (fileio_open_flags & FILEIO_O_RDWR)
171 open_flags |= O_RDWR;
172 /* On systems supporting binary and text mode, always open files in
175 open_flags |= O_BINARY;
178 *open_flags_p = open_flags;
183 inf_child_errno_to_fileio_error (int errnum)
190 return FILEIO_ENOENT;
198 return FILEIO_EACCES;
200 return FILEIO_EFAULT;
204 return FILEIO_EEXIST;
206 return FILEIO_ENODEV;
208 return FILEIO_ENOTDIR;
210 return FILEIO_EISDIR;
212 return FILEIO_EINVAL;
214 return FILEIO_ENFILE;
216 return FILEIO_EMFILE;
220 return FILEIO_ENOSPC;
222 return FILEIO_ESPIPE;
226 return FILEIO_ENOSYS;
228 return FILEIO_ENAMETOOLONG;
230 return FILEIO_EUNKNOWN;
233 /* Open FILENAME on the target, using FLAGS and MODE. Return a
234 target file descriptor, or -1 if an error occurs (and set
237 inf_child_fileio_open (struct target_ops *self,
238 const char *filename, int flags, int mode,
244 if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
246 *target_errno = FILEIO_EINVAL;
250 /* We do not need to convert MODE, since the fileio protocol uses
251 the standard values. */
252 fd = gdb_open_cloexec (filename, nat_flags, mode);
254 *target_errno = inf_child_errno_to_fileio_error (errno);
259 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
260 Return the number of bytes written, or -1 if an error occurs
261 (and set *TARGET_ERRNO). */
263 inf_child_fileio_pwrite (struct target_ops *self,
264 int fd, const gdb_byte *write_buf, int len,
265 ULONGEST offset, int *target_errno)
270 ret = pwrite (fd, write_buf, len, (long) offset);
274 /* If we have no pwrite or it failed for this file, use lseek/write. */
277 ret = lseek (fd, (long) offset, SEEK_SET);
279 ret = write (fd, write_buf, len);
283 *target_errno = inf_child_errno_to_fileio_error (errno);
288 /* Read up to LEN bytes FD on the target into READ_BUF.
289 Return the number of bytes read, or -1 if an error occurs
290 (and set *TARGET_ERRNO). */
292 inf_child_fileio_pread (struct target_ops *self,
293 int fd, gdb_byte *read_buf, int len,
294 ULONGEST offset, int *target_errno)
299 ret = pread (fd, read_buf, len, (long) offset);
303 /* If we have no pread or it failed for this file, use lseek/read. */
306 ret = lseek (fd, (long) offset, SEEK_SET);
308 ret = read (fd, read_buf, len);
312 *target_errno = inf_child_errno_to_fileio_error (errno);
317 /* Close FD on the target. Return 0, or -1 if an error occurs
318 (and set *TARGET_ERRNO). */
320 inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno)
326 *target_errno = inf_child_errno_to_fileio_error (errno);
331 /* Unlink FILENAME on the target. Return 0, or -1 if an error
332 occurs (and set *TARGET_ERRNO). */
334 inf_child_fileio_unlink (struct target_ops *self,
335 const char *filename, int *target_errno)
339 ret = unlink (filename);
341 *target_errno = inf_child_errno_to_fileio_error (errno);
346 /* Read value of symbolic link FILENAME on the target. Return a
347 null-terminated string allocated via xmalloc, or NULL if an error
348 occurs (and set *TARGET_ERRNO). */
350 inf_child_fileio_readlink (struct target_ops *self,
351 const char *filename, int *target_errno)
353 /* We support readlink only on systems that also provide a compile-time
354 maximum path length (PATH_MAX), at least for now. */
355 #if defined (HAVE_READLINK) && defined (PATH_MAX)
360 len = readlink (filename, buf, sizeof buf);
363 *target_errno = inf_child_errno_to_fileio_error (errno);
367 ret = xmalloc (len + 1);
368 memcpy (ret, buf, len);
372 *target_errno = FILEIO_ENOSYS;
378 inf_child_use_agent (struct target_ops *self, int use)
380 if (agent_loaded_p ())
390 inf_child_can_use_agent (struct target_ops *self)
392 return agent_loaded_p ();
395 /* Default implementation of the to_can_async_p and
396 to_supports_non_stop methods. */
399 return_zero (struct target_ops *ignore)
405 inf_child_target (void)
407 struct target_ops *t = XCNEW (struct target_ops);
409 t->to_shortname = "child";
410 t->to_longname = "Unix child process";
411 t->to_doc = "Unix child process (started by the \"run\" command).";
412 t->to_open = inf_child_open;
413 t->to_post_attach = inf_child_post_attach;
414 t->to_fetch_registers = inf_child_fetch_inferior_registers;
415 t->to_store_registers = inf_child_store_inferior_registers;
416 t->to_prepare_to_store = inf_child_prepare_to_store;
417 t->to_insert_breakpoint = memory_insert_breakpoint;
418 t->to_remove_breakpoint = memory_remove_breakpoint;
419 t->to_terminal_init = terminal_init_inferior;
420 t->to_terminal_inferior = terminal_inferior;
421 t->to_terminal_ours_for_output = terminal_ours_for_output;
422 t->to_terminal_save_ours = terminal_save_ours;
423 t->to_terminal_ours = terminal_ours;
424 t->to_terminal_info = child_terminal_info;
425 t->to_post_startup_inferior = inf_child_post_startup_inferior;
426 t->to_follow_fork = inf_child_follow_fork;
427 t->to_can_run = inf_child_can_run;
428 /* We must default these because they must be implemented by any
429 target that can run. */
430 t->to_can_async_p = return_zero;
431 t->to_supports_non_stop = return_zero;
432 t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
433 t->to_stratum = process_stratum;
434 t->to_has_all_memory = default_child_has_all_memory;
435 t->to_has_memory = default_child_has_memory;
436 t->to_has_stack = default_child_has_stack;
437 t->to_has_registers = default_child_has_registers;
438 t->to_has_execution = default_child_has_execution;
439 t->to_fileio_open = inf_child_fileio_open;
440 t->to_fileio_pwrite = inf_child_fileio_pwrite;
441 t->to_fileio_pread = inf_child_fileio_pread;
442 t->to_fileio_close = inf_child_fileio_close;
443 t->to_fileio_unlink = inf_child_fileio_unlink;
444 t->to_fileio_readlink = inf_child_fileio_readlink;
445 t->to_magic = OPS_MAGIC;
446 t->to_use_agent = inf_child_use_agent;
447 t->to_can_use_agent = inf_child_can_use_agent;