1 /* Base/prototype target for default child (native) targets.
3 Copyright (C) 1988-2018 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"
36 #include "filestuff.h"
38 #include <sys/types.h>
42 static const target_info inf_child_target_info = {
45 N_("Native process (started by the \"run\" command).")
49 inf_child_target::info () const
51 return inf_child_target_info;
54 /* Helper function for child_wait and the derivatives of child_wait.
55 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
56 translation of that in OURSTATUS. */
58 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
60 if (WIFEXITED (hoststatus))
62 ourstatus->kind = TARGET_WAITKIND_EXITED;
63 ourstatus->value.integer = WEXITSTATUS (hoststatus);
65 else if (!WIFSTOPPED (hoststatus))
67 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
68 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
72 ourstatus->kind = TARGET_WAITKIND_STOPPED;
73 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
77 inf_child_target::~inf_child_target ()
81 inf_child_target::post_attach (int pid)
83 /* This target doesn't require a meaningful "post attach" operation
87 /* Get ready to modify the registers array. On machines which store
88 individual registers, this doesn't need to do anything. On
89 machines which store all the registers in one fell swoop, this
90 makes sure that registers contains all the registers from the
91 program being debugged. */
94 inf_child_target::prepare_to_store (struct regcache *regcache)
99 inf_child_target::supports_terminal_ours ()
105 inf_child_target::terminal_init ()
107 child_terminal_init (this);
111 inf_child_target::terminal_inferior ()
113 child_terminal_inferior (this);
117 inf_child_target::terminal_ours_for_output ()
119 child_terminal_ours_for_output (this);
123 inf_child_target::terminal_ours ()
125 child_terminal_ours (this);
129 inf_child_target::interrupt ()
131 child_interrupt (this);
135 inf_child_target::pass_ctrlc ()
137 child_pass_ctrlc (this);
141 inf_child_target::terminal_info (const char *args, int from_tty)
143 child_terminal_info (this, args, from_tty);
146 /* True if the user did "target native". In that case, we won't
147 unpush the child target automatically when the last inferior is
149 static int inf_child_explicitly_opened;
151 /* See inf-child.h. */
154 inf_child_open_target (const char *arg, int from_tty)
156 target_ops *target = get_native_target ();
158 /* There's always only ever one native target, and if we get here,
159 it better be an inf-child target. */
160 gdb_assert (dynamic_cast<inf_child_target *> (target) != NULL);
162 target_preopen (from_tty);
163 push_target (target);
164 inf_child_explicitly_opened = 1;
166 printf_filtered ("Done. Use the \"run\" command to start a process.\n");
169 /* Implement the to_disconnect target_ops method. */
172 inf_child_target::disconnect (const char *args, int from_tty)
175 error (_("Argument given to \"disconnect\"."));
177 /* This offers to detach/kill current inferiors, and then pops all
179 target_preopen (from_tty);
182 /* Implement the to_close target_ops method. */
185 inf_child_target::close ()
187 /* In case we were forcibly closed. */
188 inf_child_explicitly_opened = 0;
192 inf_child_target::mourn_inferior ()
194 generic_mourn_inferior ();
195 maybe_unpush_target ();
198 /* See inf-child.h. */
201 inf_child_target::maybe_unpush_target ()
203 if (!inf_child_explicitly_opened && !have_inferiors ())
204 unpush_target (this);
208 inf_child_target::post_startup_inferior (ptid_t ptid)
210 /* This target doesn't require a meaningful "post startup inferior"
211 operation by a debugger. */
215 inf_child_target::can_run ()
221 inf_child_target::can_create_inferior ()
227 inf_child_target::can_attach ()
233 inf_child_target::pid_to_exec_file (int pid)
235 /* This target doesn't support translation of a process ID to the
236 filename of the executable file. */
241 inf_child_target::has_all_memory ()
243 return default_child_has_all_memory ();
247 inf_child_target::has_memory ()
249 return default_child_has_memory ();
253 inf_child_target::has_stack ()
255 return default_child_has_stack ();
259 inf_child_target::has_registers ()
261 return default_child_has_registers ();
265 inf_child_target::has_execution (ptid_t ptid)
267 return default_child_has_execution (ptid);
270 /* Implementation of to_fileio_open. */
273 inf_child_target::fileio_open (struct inferior *inf, const char *filename,
274 int flags, int mode, int warn_if_slow,
281 if (fileio_to_host_openflags (flags, &nat_flags) == -1
282 || fileio_to_host_mode (mode, &nat_mode) == -1)
284 *target_errno = FILEIO_EINVAL;
288 fd = gdb_open_cloexec (filename, nat_flags, nat_mode);
290 *target_errno = host_to_fileio_error (errno);
295 /* Implementation of to_fileio_pwrite. */
298 inf_child_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
299 ULONGEST offset, int *target_errno)
304 ret = pwrite (fd, write_buf, len, (long) offset);
308 /* If we have no pwrite or it failed for this file, use lseek/write. */
311 ret = lseek (fd, (long) offset, SEEK_SET);
313 ret = write (fd, write_buf, len);
317 *target_errno = host_to_fileio_error (errno);
322 /* Implementation of to_fileio_pread. */
325 inf_child_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
326 ULONGEST offset, int *target_errno)
331 ret = pread (fd, read_buf, len, (long) offset);
335 /* If we have no pread or it failed for this file, use lseek/read. */
338 ret = lseek (fd, (long) offset, SEEK_SET);
340 ret = read (fd, read_buf, len);
344 *target_errno = host_to_fileio_error (errno);
349 /* Implementation of to_fileio_fstat. */
352 inf_child_target::fileio_fstat (int fd, struct stat *sb, int *target_errno)
356 ret = fstat (fd, sb);
358 *target_errno = host_to_fileio_error (errno);
363 /* Implementation of to_fileio_close. */
366 inf_child_target::fileio_close (int fd, int *target_errno)
372 *target_errno = host_to_fileio_error (errno);
377 /* Implementation of to_fileio_unlink. */
380 inf_child_target::fileio_unlink (struct inferior *inf, const char *filename,
385 ret = unlink (filename);
387 *target_errno = host_to_fileio_error (errno);
392 /* Implementation of to_fileio_readlink. */
394 gdb::optional<std::string>
395 inf_child_target::fileio_readlink (struct inferior *inf, const char *filename,
398 /* We support readlink only on systems that also provide a compile-time
399 maximum path length (PATH_MAX), at least for now. */
400 #if defined (PATH_MAX)
404 len = readlink (filename, buf, sizeof buf);
407 *target_errno = host_to_fileio_error (errno);
411 return std::string (buf, len);
413 *target_errno = FILEIO_ENOSYS;
419 inf_child_target::use_agent (bool use)
421 if (agent_loaded_p ())
431 inf_child_target::can_use_agent ()
433 return agent_loaded_p ();
436 inf_child_target::inf_child_target ()
438 this->to_stratum = process_stratum;
441 /* See inf-child.h. */
444 add_inf_child_target (inf_child_target *target)
446 set_native_target (target);
447 add_target (inf_child_target_info, inf_child_open_target);