Update French translation of ld strings.
[deliverable/binutils-gdb.git] / gdb / inf-child.c
CommitLineData
f1aea813 1/* Base/prototype target for default child (native) targets.
5bf970f9 2
32d0add0 3 Copyright (C) 1988-2015 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"
b88bb450 33#include "common-remote-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
6a3cb8e8
PA
42/* A pointer to what is returned by inf_child_target. Used by
43 inf_child_open to push the most-derived target in reaction to
44 "target native". */
45static struct target_ops *inf_child_ops = NULL;
46
dab06dbe
PA
47/* Helper function for child_wait and the derivatives of child_wait.
48 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
49 translation of that in OURSTATUS. */
50void
51store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
52{
53 if (WIFEXITED (hoststatus))
54 {
55 ourstatus->kind = TARGET_WAITKIND_EXITED;
56 ourstatus->value.integer = WEXITSTATUS (hoststatus);
57 }
58 else if (!WIFSTOPPED (hoststatus))
59 {
60 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2ea28649 61 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
dab06dbe
PA
62 }
63 else
64 {
65 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2ea28649 66 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
dab06dbe
PA
67 }
68}
69
5bf970f9
AC
70/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
71 for all registers. */
72
73static void
28439f5e
PA
74inf_child_fetch_inferior_registers (struct target_ops *ops,
75 struct regcache *regcache, int regnum)
5bf970f9
AC
76{
77 if (regnum == -1)
78 {
b1a653ae
UW
79 for (regnum = 0;
80 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
81 regnum++)
56be3814 82 regcache_raw_supply (regcache, regnum, NULL);
5bf970f9
AC
83 }
84 else
56be3814 85 regcache_raw_supply (regcache, regnum, NULL);
5bf970f9
AC
86}
87
88/* Store register REGNUM back into the inferior. If REGNUM is -1, do
89 this for all registers (including the floating point registers). */
90
91static void
28439f5e
PA
92inf_child_store_inferior_registers (struct target_ops *ops,
93 struct regcache *regcache, int regnum)
5bf970f9
AC
94{
95}
96
5bf970f9 97static void
f045800c 98inf_child_post_attach (struct target_ops *self, int pid)
5bf970f9 99{
f1aea813
PA
100 /* This target doesn't require a meaningful "post attach" operation
101 by a debugger. */
5bf970f9
AC
102}
103
104/* Get ready to modify the registers array. On machines which store
105 individual registers, this doesn't need to do anything. On
106 machines which store all the registers in one fell swoop, this
107 makes sure that registers contains all the registers from the
108 program being debugged. */
109
110static void
f32dbf8c
MM
111inf_child_prepare_to_store (struct target_ops *self,
112 struct regcache *regcache)
5bf970f9
AC
113{
114}
115
6a3cb8e8
PA
116/* True if the user did "target native". In that case, we won't
117 unpush the child target automatically when the last inferior is
118 gone. */
119static int inf_child_explicitly_opened;
120
121/* See inf-child.h. */
122
123void
0d5f0dbe
TT
124inf_child_open_target (struct target_ops *target, const char *arg,
125 int from_tty)
6a3cb8e8
PA
126{
127 target_preopen (from_tty);
128 push_target (target);
129 inf_child_explicitly_opened = 1;
130 if (from_tty)
131 printf_filtered ("Done. Use the \"run\" command to start a process.\n");
132}
133
5bf970f9 134static void
014f9477 135inf_child_open (const char *arg, int from_tty)
5bf970f9 136{
6a3cb8e8
PA
137 inf_child_open_target (inf_child_ops, arg, from_tty);
138}
139
140/* Implement the to_disconnect target_ops method. */
141
142static void
fee354ee 143inf_child_disconnect (struct target_ops *target, const char *args, int from_tty)
6a3cb8e8
PA
144{
145 if (args != NULL)
146 error (_("Argument given to \"disconnect\"."));
147
148 /* This offers to detach/kill current inferiors, and then pops all
149 targets. */
150 target_preopen (from_tty);
151}
152
153/* Implement the to_close target_ops method. */
154
155static void
156inf_child_close (struct target_ops *target)
157{
158 /* In case we were forcibly closed. */
159 inf_child_explicitly_opened = 0;
160}
161
c1ee2fb3
PA
162void
163inf_child_mourn_inferior (struct target_ops *ops)
164{
165 generic_mourn_inferior ();
166 inf_child_maybe_unpush_target (ops);
167}
168
6a3cb8e8
PA
169/* See inf-child.h. */
170
171void
172inf_child_maybe_unpush_target (struct target_ops *ops)
173{
174 if (!inf_child_explicitly_opened && !have_inferiors ())
175 unpush_target (ops);
5bf970f9
AC
176}
177
178static void
2e97a79e 179inf_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
5bf970f9 180{
f1aea813
PA
181 /* This target doesn't require a meaningful "post startup inferior"
182 operation by a debugger. */
5bf970f9
AC
183}
184
5bf970f9 185static int
07107ca6
LM
186inf_child_follow_fork (struct target_ops *ops, int follow_child,
187 int detach_fork)
5bf970f9 188{
f1aea813 189 /* This target doesn't support following fork or vfork events. */
5bf970f9
AC
190 return 0;
191}
192
5bf970f9 193static int
da82bd6b 194inf_child_can_run (struct target_ops *self)
5bf970f9
AC
195{
196 return 1;
197}
198
5bf970f9 199static char *
8dd27370 200inf_child_pid_to_exec_file (struct target_ops *self, int pid)
5bf970f9 201{
f1aea813
PA
202 /* This target doesn't support translation of a process ID to the
203 filename of the executable file. */
5bf970f9
AC
204 return NULL;
205}
206
7313baad
UW
207
208/* Target file operations. */
209
210static int
211inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
212{
213 int open_flags = 0;
214
215 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
216 return -1;
217
218 if (fileio_open_flags & FILEIO_O_CREAT)
219 open_flags |= O_CREAT;
220 if (fileio_open_flags & FILEIO_O_EXCL)
221 open_flags |= O_EXCL;
222 if (fileio_open_flags & FILEIO_O_TRUNC)
223 open_flags |= O_TRUNC;
224 if (fileio_open_flags & FILEIO_O_APPEND)
225 open_flags |= O_APPEND;
226 if (fileio_open_flags & FILEIO_O_RDONLY)
227 open_flags |= O_RDONLY;
228 if (fileio_open_flags & FILEIO_O_WRONLY)
229 open_flags |= O_WRONLY;
230 if (fileio_open_flags & FILEIO_O_RDWR)
231 open_flags |= O_RDWR;
232/* On systems supporting binary and text mode, always open files in
233 binary mode. */
234#ifdef O_BINARY
235 open_flags |= O_BINARY;
236#endif
237
238 *open_flags_p = open_flags;
239 return 0;
240}
241
7313baad
UW
242/* Open FILENAME on the target, using FLAGS and MODE. Return a
243 target file descriptor, or -1 if an error occurs (and set
244 *TARGET_ERRNO). */
245static int
cd897586
TT
246inf_child_fileio_open (struct target_ops *self,
247 const char *filename, int flags, int mode,
7313baad
UW
248 int *target_errno)
249{
250 int nat_flags;
251 int fd;
252
253 if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
254 {
255 *target_errno = FILEIO_EINVAL;
256 return -1;
257 }
258
259 /* We do not need to convert MODE, since the fileio protocol uses
260 the standard values. */
614c279d 261 fd = gdb_open_cloexec (filename, nat_flags, mode);
7313baad 262 if (fd == -1)
b88bb450 263 *target_errno = remote_fileio_to_fio_error (errno);
7313baad
UW
264
265 return fd;
266}
267
268/* Write up to LEN bytes from WRITE_BUF to FD on the target.
269 Return the number of bytes written, or -1 if an error occurs
270 (and set *TARGET_ERRNO). */
271static int
0d866f62
TT
272inf_child_fileio_pwrite (struct target_ops *self,
273 int fd, const gdb_byte *write_buf, int len,
7313baad
UW
274 ULONGEST offset, int *target_errno)
275{
276 int ret;
277
278#ifdef HAVE_PWRITE
279 ret = pwrite (fd, write_buf, len, (long) offset);
280#else
7c3270ae 281 ret = -1;
7313baad 282#endif
7c3270ae
UW
283 /* If we have no pwrite or it failed for this file, use lseek/write. */
284 if (ret == -1)
285 {
286 ret = lseek (fd, (long) offset, SEEK_SET);
287 if (ret != -1)
288 ret = write (fd, write_buf, len);
289 }
7313baad
UW
290
291 if (ret == -1)
b88bb450 292 *target_errno = remote_fileio_to_fio_error (errno);
7313baad
UW
293
294 return ret;
295}
296
297/* Read up to LEN bytes FD on the target into READ_BUF.
298 Return the number of bytes read, or -1 if an error occurs
299 (and set *TARGET_ERRNO). */
300static int
a3be983c
TT
301inf_child_fileio_pread (struct target_ops *self,
302 int fd, gdb_byte *read_buf, int len,
7313baad
UW
303 ULONGEST offset, int *target_errno)
304{
305 int ret;
306
307#ifdef HAVE_PREAD
308 ret = pread (fd, read_buf, len, (long) offset);
309#else
7c3270ae 310 ret = -1;
7313baad 311#endif
7c3270ae
UW
312 /* If we have no pread or it failed for this file, use lseek/read. */
313 if (ret == -1)
314 {
315 ret = lseek (fd, (long) offset, SEEK_SET);
316 if (ret != -1)
317 ret = read (fd, read_buf, len);
318 }
7313baad
UW
319
320 if (ret == -1)
b88bb450 321 *target_errno = remote_fileio_to_fio_error (errno);
7313baad
UW
322
323 return ret;
324}
325
9b15c1f0
GB
326/* Implementation of to_fileio_fstat. */
327static int
328inf_child_fileio_fstat (struct target_ops *self, int fd,
329 struct stat *sb, int *target_errno)
330{
331 int ret;
332
333 ret = fstat (fd, sb);
334 if (ret == -1)
b88bb450 335 *target_errno = remote_fileio_to_fio_error (errno);
9b15c1f0
GB
336
337 return ret;
338}
339
7313baad
UW
340/* Close FD on the target. Return 0, or -1 if an error occurs
341 (and set *TARGET_ERRNO). */
342static int
df39ea25 343inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno)
7313baad
UW
344{
345 int ret;
346
347 ret = close (fd);
348 if (ret == -1)
b88bb450 349 *target_errno = remote_fileio_to_fio_error (errno);
7313baad
UW
350
351 return ret;
352}
353
354/* Unlink FILENAME on the target. Return 0, or -1 if an error
355 occurs (and set *TARGET_ERRNO). */
356static int
dbbca37d
TT
357inf_child_fileio_unlink (struct target_ops *self,
358 const char *filename, int *target_errno)
7313baad
UW
359{
360 int ret;
361
362 ret = unlink (filename);
363 if (ret == -1)
b88bb450 364 *target_errno = remote_fileio_to_fio_error (errno);
7313baad
UW
365
366 return ret;
367}
368
b9e7b9c3
UW
369/* Read value of symbolic link FILENAME on the target. Return a
370 null-terminated string allocated via xmalloc, or NULL if an error
371 occurs (and set *TARGET_ERRNO). */
372static char *
fab5aa7c
TT
373inf_child_fileio_readlink (struct target_ops *self,
374 const char *filename, int *target_errno)
b9e7b9c3
UW
375{
376 /* We support readlink only on systems that also provide a compile-time
d8d2a3ee 377 maximum path length (PATH_MAX), at least for now. */
bdca27a2 378#if defined (PATH_MAX)
d8d2a3ee 379 char buf[PATH_MAX];
b9e7b9c3
UW
380 int len;
381 char *ret;
382
383 len = readlink (filename, buf, sizeof buf);
384 if (len < 0)
385 {
b88bb450 386 *target_errno = remote_fileio_to_fio_error (errno);
b9e7b9c3
UW
387 return NULL;
388 }
389
390 ret = xmalloc (len + 1);
391 memcpy (ret, buf, len);
392 ret[len] = '\0';
393 return ret;
394#else
395 *target_errno = FILEIO_ENOSYS;
396 return NULL;
397#endif
398}
399
5808517f 400static int
2c152180 401inf_child_use_agent (struct target_ops *self, int use)
5808517f
YQ
402{
403 if (agent_loaded_p ())
404 {
405 use_agent = use;
406 return 1;
407 }
408 else
409 return 0;
410}
411
412static int
fe38f897 413inf_child_can_use_agent (struct target_ops *self)
5808517f
YQ
414{
415 return agent_loaded_p ();
416}
7313baad 417
b3ccfe11
TT
418/* Default implementation of the to_can_async_p and
419 to_supports_non_stop methods. */
420
421static int
422return_zero (struct target_ops *ignore)
423{
424 return 0;
425}
426
5bf970f9
AC
427struct target_ops *
428inf_child_target (void)
429{
41bf6aca 430 struct target_ops *t = XCNEW (struct target_ops);
abbb1732 431
4ebfc96e
PA
432 t->to_shortname = "native";
433 t->to_longname = "Native process";
434 t->to_doc = "Native process (started by the \"run\" command).";
5bf970f9 435 t->to_open = inf_child_open;
6a3cb8e8
PA
436 t->to_close = inf_child_close;
437 t->to_disconnect = inf_child_disconnect;
5bf970f9 438 t->to_post_attach = inf_child_post_attach;
7681f339
AC
439 t->to_fetch_registers = inf_child_fetch_inferior_registers;
440 t->to_store_registers = inf_child_store_inferior_registers;
5bf970f9
AC
441 t->to_prepare_to_store = inf_child_prepare_to_store;
442 t->to_insert_breakpoint = memory_insert_breakpoint;
443 t->to_remove_breakpoint = memory_remove_breakpoint;
d6b64346
PA
444 t->to_terminal_init = child_terminal_init;
445 t->to_terminal_inferior = child_terminal_inferior;
446 t->to_terminal_ours_for_output = child_terminal_ours_for_output;
d6b64346 447 t->to_terminal_ours = child_terminal_ours;
5bf970f9
AC
448 t->to_terminal_info = child_terminal_info;
449 t->to_post_startup_inferior = inf_child_post_startup_inferior;
5bf970f9 450 t->to_follow_fork = inf_child_follow_fork;
5bf970f9 451 t->to_can_run = inf_child_can_run;
b3ccfe11
TT
452 /* We must default these because they must be implemented by any
453 target that can run. */
454 t->to_can_async_p = return_zero;
455 t->to_supports_non_stop = return_zero;
5bf970f9
AC
456 t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
457 t->to_stratum = process_stratum;
c35b1492
PA
458 t->to_has_all_memory = default_child_has_all_memory;
459 t->to_has_memory = default_child_has_memory;
460 t->to_has_stack = default_child_has_stack;
461 t->to_has_registers = default_child_has_registers;
462 t->to_has_execution = default_child_has_execution;
7313baad
UW
463 t->to_fileio_open = inf_child_fileio_open;
464 t->to_fileio_pwrite = inf_child_fileio_pwrite;
465 t->to_fileio_pread = inf_child_fileio_pread;
9b15c1f0 466 t->to_fileio_fstat = inf_child_fileio_fstat;
7313baad
UW
467 t->to_fileio_close = inf_child_fileio_close;
468 t->to_fileio_unlink = inf_child_fileio_unlink;
b9e7b9c3 469 t->to_fileio_readlink = inf_child_fileio_readlink;
5bf970f9 470 t->to_magic = OPS_MAGIC;
5808517f
YQ
471 t->to_use_agent = inf_child_use_agent;
472 t->to_can_use_agent = inf_child_can_use_agent;
6a3cb8e8
PA
473
474 /* Store a pointer so we can push the most-derived target from
475 inf_child_open. */
476 inf_child_ops = t;
477
5bf970f9
AC
478 return t;
479}
This page took 0.880227 seconds and 4 git commands to generate.