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 /* Helper function for child_wait and the derivatives of child_wait.
43 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
44 translation of that in OURSTATUS. */
46 store_waitstatus (struct target_waitstatus
*ourstatus
, int hoststatus
)
48 if (WIFEXITED (hoststatus
))
50 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
51 ourstatus
->value
.integer
= WEXITSTATUS (hoststatus
);
53 else if (!WIFSTOPPED (hoststatus
))
55 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
56 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (hoststatus
));
60 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
61 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (hoststatus
));
65 inf_child_target::~inf_child_target ()
69 inf_child_target::post_attach (int pid
)
71 /* This target doesn't require a meaningful "post attach" operation
75 /* Get ready to modify the registers array. On machines which store
76 individual registers, this doesn't need to do anything. On
77 machines which store all the registers in one fell swoop, this
78 makes sure that registers contains all the registers from the
79 program being debugged. */
82 inf_child_target::prepare_to_store (struct regcache
*regcache
)
87 inf_child_target::supports_terminal_ours ()
93 inf_child_target::terminal_init ()
95 child_terminal_init (this);
99 inf_child_target::terminal_inferior ()
101 child_terminal_inferior (this);
105 inf_child_target::terminal_ours_for_output ()
107 child_terminal_ours_for_output (this);
111 inf_child_target::terminal_ours ()
113 child_terminal_ours (this);
117 inf_child_target::interrupt ()
119 child_interrupt (this);
123 inf_child_target::pass_ctrlc ()
125 child_pass_ctrlc (this);
129 inf_child_target::terminal_info (const char *args
, int from_tty
)
131 child_terminal_info (this, args
, from_tty
);
134 /* True if the user did "target native". In that case, we won't
135 unpush the child target automatically when the last inferior is
137 static int inf_child_explicitly_opened
;
139 /* See inf-child.h. */
142 inf_child_open_target (struct target_ops
*target
, const char *arg
,
145 target_preopen (from_tty
);
146 push_target (target
);
147 inf_child_explicitly_opened
= 1;
149 printf_filtered ("Done. Use the \"run\" command to start a process.\n");
153 inf_child_target::open (const char *arg
, int from_tty
)
155 inf_child_open_target (this, arg
, from_tty
);
158 /* Implement the to_disconnect target_ops method. */
161 inf_child_target::disconnect (const char *args
, int from_tty
)
164 error (_("Argument given to \"disconnect\"."));
166 /* This offers to detach/kill current inferiors, and then pops all
168 target_preopen (from_tty
);
171 /* Implement the to_close target_ops method. */
174 inf_child_target::close ()
176 /* In case we were forcibly closed. */
177 inf_child_explicitly_opened
= 0;
181 inf_child_target::mourn_inferior ()
183 generic_mourn_inferior ();
184 maybe_unpush_target ();
187 /* See inf-child.h. */
190 inf_child_target::maybe_unpush_target ()
192 if (!inf_child_explicitly_opened
&& !have_inferiors ())
193 unpush_target (this);
197 inf_child_target::post_startup_inferior (ptid_t ptid
)
199 /* This target doesn't require a meaningful "post startup inferior"
200 operation by a debugger. */
204 inf_child_target::can_run ()
210 inf_child_target::can_create_inferior ()
216 inf_child_target::can_attach ()
222 inf_child_target::pid_to_exec_file (int pid
)
224 /* This target doesn't support translation of a process ID to the
225 filename of the executable file. */
230 inf_child_target::has_all_memory ()
232 return default_child_has_all_memory ();
236 inf_child_target::has_memory ()
238 return default_child_has_memory ();
242 inf_child_target::has_stack ()
244 return default_child_has_stack ();
248 inf_child_target::has_registers ()
250 return default_child_has_registers ();
254 inf_child_target::has_execution (ptid_t ptid
)
256 return default_child_has_execution (ptid
);
259 /* Implementation of to_fileio_open. */
262 inf_child_target::fileio_open (struct inferior
*inf
, const char *filename
,
263 int flags
, int mode
, int warn_if_slow
,
270 if (fileio_to_host_openflags (flags
, &nat_flags
) == -1
271 || fileio_to_host_mode (mode
, &nat_mode
) == -1)
273 *target_errno
= FILEIO_EINVAL
;
277 fd
= gdb_open_cloexec (filename
, nat_flags
, nat_mode
);
279 *target_errno
= host_to_fileio_error (errno
);
284 /* Implementation of to_fileio_pwrite. */
287 inf_child_target::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
288 ULONGEST offset
, int *target_errno
)
293 ret
= pwrite (fd
, write_buf
, len
, (long) offset
);
297 /* If we have no pwrite or it failed for this file, use lseek/write. */
300 ret
= lseek (fd
, (long) offset
, SEEK_SET
);
302 ret
= write (fd
, write_buf
, len
);
306 *target_errno
= host_to_fileio_error (errno
);
311 /* Implementation of to_fileio_pread. */
314 inf_child_target::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
315 ULONGEST offset
, int *target_errno
)
320 ret
= pread (fd
, read_buf
, len
, (long) offset
);
324 /* If we have no pread or it failed for this file, use lseek/read. */
327 ret
= lseek (fd
, (long) offset
, SEEK_SET
);
329 ret
= read (fd
, read_buf
, len
);
333 *target_errno
= host_to_fileio_error (errno
);
338 /* Implementation of to_fileio_fstat. */
341 inf_child_target::fileio_fstat (int fd
, struct stat
*sb
, int *target_errno
)
345 ret
= fstat (fd
, sb
);
347 *target_errno
= host_to_fileio_error (errno
);
352 /* Implementation of to_fileio_close. */
355 inf_child_target::fileio_close (int fd
, int *target_errno
)
361 *target_errno
= host_to_fileio_error (errno
);
366 /* Implementation of to_fileio_unlink. */
369 inf_child_target::fileio_unlink (struct inferior
*inf
, const char *filename
,
374 ret
= unlink (filename
);
376 *target_errno
= host_to_fileio_error (errno
);
381 /* Implementation of to_fileio_readlink. */
383 gdb::optional
<std::string
>
384 inf_child_target::fileio_readlink (struct inferior
*inf
, const char *filename
,
387 /* We support readlink only on systems that also provide a compile-time
388 maximum path length (PATH_MAX), at least for now. */
389 #if defined (PATH_MAX)
393 len
= readlink (filename
, buf
, sizeof buf
);
396 *target_errno
= host_to_fileio_error (errno
);
400 return std::string (buf
, len
);
402 *target_errno
= FILEIO_ENOSYS
;
408 inf_child_target::use_agent (int use
)
410 if (agent_loaded_p ())
420 inf_child_target::can_use_agent ()
422 return agent_loaded_p ();
425 inf_child_target::inf_child_target ()
427 this->to_stratum
= process_stratum
;