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