]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/inf-child.c
target factories, target open and multiple instances of targets
[thirdparty/binutils-gdb.git] / gdb / inf-child.c
CommitLineData
f1aea813 1/* Base/prototype target for default child (native) targets.
5bf970f9 2
e2882c85 3 Copyright (C) 1988-2018 Free Software Foundation, Inc.
5bf970f9
AC
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
5bf970f9
AC
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5bf970f9 19
f1aea813
PA
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
5bf970f9
AC
25#include "defs.h"
26#include "regcache.h"
27#include "memattr.h"
28#include "symtab.h"
29#include "target.h"
30#include "inferior.h"
53ce3c39 31#include <sys/stat.h>
2c0b251b 32#include "inf-child.h"
7823a941 33#include "fileio.h"
5808517f 34#include "agent.h"
dab06dbe 35#include "gdb_wait.h"
614c279d 36#include "filestuff.h"
7313baad
UW
37
38#include <sys/types.h>
7313baad
UW
39#include <fcntl.h>
40#include <unistd.h>
5bf970f9 41
d9f719f1
PA
42static const target_info inf_child_target_info = {
43 "native",
44 N_("Native process"),
45 N_("Native process (started by the \"run\" command).")
46};
47
48const target_info &
49inf_child_target::info () const
50{
51 return inf_child_target_info;
52}
53
dab06dbe
PA
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. */
57void
58store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
59{
60 if (WIFEXITED (hoststatus))
61 {
62 ourstatus->kind = TARGET_WAITKIND_EXITED;
63 ourstatus->value.integer = WEXITSTATUS (hoststatus);
64 }
65 else if (!WIFSTOPPED (hoststatus))
66 {
67 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2ea28649 68 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
dab06dbe
PA
69 }
70 else
71 {
72 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2ea28649 73 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
dab06dbe
PA
74 }
75}
76
f6ac5f3d
PA
77inf_child_target::~inf_child_target ()
78{}
5bf970f9 79
f6ac5f3d
PA
80void
81inf_child_target::post_attach (int pid)
5bf970f9 82{
f1aea813
PA
83 /* This target doesn't require a meaningful "post attach" operation
84 by a debugger. */
5bf970f9
AC
85}
86
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. */
92
f6ac5f3d
PA
93void
94inf_child_target::prepare_to_store (struct regcache *regcache)
5bf970f9
AC
95{
96}
97
f6ac5f3d
PA
98bool
99inf_child_target::supports_terminal_ours ()
100{
101 return true;
102}
103
104void
105inf_child_target::terminal_init ()
106{
107 child_terminal_init (this);
108}
109
110void
111inf_child_target::terminal_inferior ()
112{
113 child_terminal_inferior (this);
114}
115
116void
117inf_child_target::terminal_ours_for_output ()
118{
119 child_terminal_ours_for_output (this);
120}
121
122void
123inf_child_target::terminal_ours ()
124{
125 child_terminal_ours (this);
126}
127
128void
129inf_child_target::interrupt ()
130{
131 child_interrupt (this);
132}
133
134void
135inf_child_target::pass_ctrlc ()
136{
137 child_pass_ctrlc (this);
138}
139
140void
141inf_child_target::terminal_info (const char *args, int from_tty)
142{
143 child_terminal_info (this, args, from_tty);
144}
145
6a3cb8e8
PA
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
148 gone. */
149static int inf_child_explicitly_opened;
150
151/* See inf-child.h. */
152
153void
d9f719f1 154inf_child_open_target (const char *arg, int from_tty)
6a3cb8e8 155{
d9f719f1
PA
156 target_ops *target = get_native_target ();
157
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);
161
6a3cb8e8
PA
162 target_preopen (from_tty);
163 push_target (target);
164 inf_child_explicitly_opened = 1;
165 if (from_tty)
166 printf_filtered ("Done. Use the \"run\" command to start a process.\n");
167}
168
6a3cb8e8
PA
169/* Implement the to_disconnect target_ops method. */
170
f6ac5f3d
PA
171void
172inf_child_target::disconnect (const char *args, int from_tty)
6a3cb8e8
PA
173{
174 if (args != NULL)
175 error (_("Argument given to \"disconnect\"."));
176
177 /* This offers to detach/kill current inferiors, and then pops all
178 targets. */
179 target_preopen (from_tty);
180}
181
182/* Implement the to_close target_ops method. */
183
f6ac5f3d
PA
184void
185inf_child_target::close ()
6a3cb8e8
PA
186{
187 /* In case we were forcibly closed. */
188 inf_child_explicitly_opened = 0;
189}
190
c1ee2fb3 191void
f6ac5f3d 192inf_child_target::mourn_inferior ()
c1ee2fb3
PA
193{
194 generic_mourn_inferior ();
f6ac5f3d 195 maybe_unpush_target ();
c1ee2fb3
PA
196}
197
6a3cb8e8
PA
198/* See inf-child.h. */
199
200void
f6ac5f3d 201inf_child_target::maybe_unpush_target ()
6a3cb8e8
PA
202{
203 if (!inf_child_explicitly_opened && !have_inferiors ())
f6ac5f3d 204 unpush_target (this);
5bf970f9
AC
205}
206
f6ac5f3d
PA
207void
208inf_child_target::post_startup_inferior (ptid_t ptid)
5bf970f9 209{
f1aea813
PA
210 /* This target doesn't require a meaningful "post startup inferior"
211 operation by a debugger. */
5bf970f9
AC
212}
213
57810aa7 214bool
f6ac5f3d 215inf_child_target::can_run ()
5bf970f9 216{
57810aa7 217 return true;
5bf970f9
AC
218}
219
f6ac5f3d
PA
220bool
221inf_child_target::can_create_inferior ()
5bf970f9 222{
f6ac5f3d
PA
223 return true;
224}
225
226bool
227inf_child_target::can_attach ()
228{
229 return true;
5bf970f9
AC
230}
231
f6ac5f3d
PA
232char *
233inf_child_target::pid_to_exec_file (int pid)
5bf970f9 234{
f1aea813
PA
235 /* This target doesn't support translation of a process ID to the
236 filename of the executable file. */
5bf970f9
AC
237 return NULL;
238}
239
57810aa7 240bool
f6ac5f3d
PA
241inf_child_target::has_all_memory ()
242{
243 return default_child_has_all_memory ();
244}
245
57810aa7 246bool
f6ac5f3d
PA
247inf_child_target::has_memory ()
248{
249 return default_child_has_memory ();
250}
251
57810aa7 252bool
f6ac5f3d
PA
253inf_child_target::has_stack ()
254{
255 return default_child_has_stack ();
256}
257
57810aa7 258bool
f6ac5f3d
PA
259inf_child_target::has_registers ()
260{
261 return default_child_has_registers ();
262}
263
57810aa7 264bool
f6ac5f3d
PA
265inf_child_target::has_execution (ptid_t ptid)
266{
267 return default_child_has_execution (ptid);
268}
269
12e2a5fd
GB
270/* Implementation of to_fileio_open. */
271
f6ac5f3d
PA
272int
273inf_child_target::fileio_open (struct inferior *inf, const char *filename,
274 int flags, int mode, int warn_if_slow,
275 int *target_errno)
7313baad
UW
276{
277 int nat_flags;
3ac2e371 278 mode_t nat_mode;
7313baad
UW
279 int fd;
280
3ac2e371
GB
281 if (fileio_to_host_openflags (flags, &nat_flags) == -1
282 || fileio_to_host_mode (mode, &nat_mode) == -1)
7313baad
UW
283 {
284 *target_errno = FILEIO_EINVAL;
285 return -1;
286 }
287
3ac2e371 288 fd = gdb_open_cloexec (filename, nat_flags, nat_mode);
7313baad 289 if (fd == -1)
7823a941 290 *target_errno = host_to_fileio_error (errno);
7313baad
UW
291
292 return fd;
293}
294
12e2a5fd
GB
295/* Implementation of to_fileio_pwrite. */
296
f6ac5f3d
PA
297int
298inf_child_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
299 ULONGEST offset, int *target_errno)
7313baad
UW
300{
301 int ret;
302
303#ifdef HAVE_PWRITE
304 ret = pwrite (fd, write_buf, len, (long) offset);
305#else
7c3270ae 306 ret = -1;
7313baad 307#endif
7c3270ae
UW
308 /* If we have no pwrite or it failed for this file, use lseek/write. */
309 if (ret == -1)
310 {
311 ret = lseek (fd, (long) offset, SEEK_SET);
312 if (ret != -1)
313 ret = write (fd, write_buf, len);
314 }
7313baad
UW
315
316 if (ret == -1)
7823a941 317 *target_errno = host_to_fileio_error (errno);
7313baad
UW
318
319 return ret;
320}
321
12e2a5fd
GB
322/* Implementation of to_fileio_pread. */
323
f6ac5f3d
PA
324int
325inf_child_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
326 ULONGEST offset, int *target_errno)
7313baad
UW
327{
328 int ret;
329
330#ifdef HAVE_PREAD
331 ret = pread (fd, read_buf, len, (long) offset);
332#else
7c3270ae 333 ret = -1;
7313baad 334#endif
7c3270ae
UW
335 /* If we have no pread or it failed for this file, use lseek/read. */
336 if (ret == -1)
337 {
338 ret = lseek (fd, (long) offset, SEEK_SET);
339 if (ret != -1)
340 ret = read (fd, read_buf, len);
341 }
7313baad
UW
342
343 if (ret == -1)
7823a941 344 *target_errno = host_to_fileio_error (errno);
7313baad
UW
345
346 return ret;
347}
348
9b15c1f0 349/* Implementation of to_fileio_fstat. */
12e2a5fd 350
f6ac5f3d
PA
351int
352inf_child_target::fileio_fstat (int fd, struct stat *sb, int *target_errno)
9b15c1f0
GB
353{
354 int ret;
355
356 ret = fstat (fd, sb);
357 if (ret == -1)
7823a941 358 *target_errno = host_to_fileio_error (errno);
9b15c1f0
GB
359
360 return ret;
361}
362
12e2a5fd
GB
363/* Implementation of to_fileio_close. */
364
f6ac5f3d
PA
365int
366inf_child_target::fileio_close (int fd, int *target_errno)
7313baad
UW
367{
368 int ret;
369
f6ac5f3d 370 ret = ::close (fd);
7313baad 371 if (ret == -1)
7823a941 372 *target_errno = host_to_fileio_error (errno);
7313baad
UW
373
374 return ret;
375}
376
12e2a5fd
GB
377/* Implementation of to_fileio_unlink. */
378
f6ac5f3d
PA
379int
380inf_child_target::fileio_unlink (struct inferior *inf, const char *filename,
381 int *target_errno)
7313baad
UW
382{
383 int ret;
384
385 ret = unlink (filename);
386 if (ret == -1)
7823a941 387 *target_errno = host_to_fileio_error (errno);
7313baad
UW
388
389 return ret;
390}
391
12e2a5fd
GB
392/* Implementation of to_fileio_readlink. */
393
f6ac5f3d
PA
394gdb::optional<std::string>
395inf_child_target::fileio_readlink (struct inferior *inf, const char *filename,
396 int *target_errno)
b9e7b9c3
UW
397{
398 /* We support readlink only on systems that also provide a compile-time
d8d2a3ee 399 maximum path length (PATH_MAX), at least for now. */
bdca27a2 400#if defined (PATH_MAX)
d8d2a3ee 401 char buf[PATH_MAX];
b9e7b9c3 402 int len;
b9e7b9c3
UW
403
404 len = readlink (filename, buf, sizeof buf);
405 if (len < 0)
406 {
7823a941 407 *target_errno = host_to_fileio_error (errno);
e0d3522b 408 return {};
b9e7b9c3
UW
409 }
410
e0d3522b 411 return std::string (buf, len);
b9e7b9c3
UW
412#else
413 *target_errno = FILEIO_ENOSYS;
e0d3522b 414 return {};
b9e7b9c3
UW
415#endif
416}
417
57810aa7
PA
418bool
419inf_child_target::use_agent (bool use)
5808517f
YQ
420{
421 if (agent_loaded_p ())
422 {
f6ac5f3d 423 ::use_agent = use;
57810aa7 424 return true;
5808517f
YQ
425 }
426 else
57810aa7 427 return false;
5808517f
YQ
428}
429
57810aa7 430bool
f6ac5f3d 431inf_child_target::can_use_agent ()
5808517f
YQ
432{
433 return agent_loaded_p ();
434}
7313baad 435
f6ac5f3d
PA
436inf_child_target::inf_child_target ()
437{
438 this->to_stratum = process_stratum;
5bf970f9 439}
d9f719f1
PA
440
441/* See inf-child.h. */
442
443void
444add_inf_child_target (inf_child_target *target)
445{
446 set_native_target (target);
447 add_target (inf_child_target_info, inf_child_open_target);
448}