Normalize on PATH_MAX instead of MAXPATHLEN throughout.
[deliverable/binutils-gdb.git] / gdb / inf-child.c
CommitLineData
5bf970f9
AC
1/* Default child (native) target interface, for GDB when running under
2 Unix.
3
28e7fd62 4 Copyright (C) 1988-2013 Free Software Foundation, Inc.
5bf970f9
AC
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
5bf970f9
AC
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5bf970f9
AC
20
21#include "defs.h"
22#include "regcache.h"
23#include "memattr.h"
24#include "symtab.h"
25#include "target.h"
26#include "inferior.h"
7681f339 27#include "gdb_string.h"
a71b5a38 28#include "gdb_stat.h"
2c0b251b 29#include "inf-child.h"
7313baad 30#include "gdb/fileio.h"
5808517f 31#include "agent.h"
dab06dbe 32#include "gdb_wait.h"
614c279d 33#include "filestuff.h"
7313baad
UW
34
35#include <sys/types.h>
7313baad
UW
36#include <fcntl.h>
37#include <unistd.h>
5bf970f9 38
dab06dbe
PA
39/* Helper function for child_wait and the derivatives of child_wait.
40 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
41 translation of that in OURSTATUS. */
42void
43store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
44{
45 if (WIFEXITED (hoststatus))
46 {
47 ourstatus->kind = TARGET_WAITKIND_EXITED;
48 ourstatus->value.integer = WEXITSTATUS (hoststatus);
49 }
50 else if (!WIFSTOPPED (hoststatus))
51 {
52 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2ea28649 53 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
dab06dbe
PA
54 }
55 else
56 {
57 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2ea28649 58 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
dab06dbe
PA
59 }
60}
61
5bf970f9
AC
62/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
63 for all registers. */
64
65static void
28439f5e
PA
66inf_child_fetch_inferior_registers (struct target_ops *ops,
67 struct regcache *regcache, int regnum)
5bf970f9
AC
68{
69 if (regnum == -1)
70 {
b1a653ae
UW
71 for (regnum = 0;
72 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
73 regnum++)
56be3814 74 regcache_raw_supply (regcache, regnum, NULL);
5bf970f9
AC
75 }
76 else
56be3814 77 regcache_raw_supply (regcache, regnum, NULL);
5bf970f9
AC
78}
79
80/* Store register REGNUM back into the inferior. If REGNUM is -1, do
81 this for all registers (including the floating point registers). */
82
83static void
28439f5e
PA
84inf_child_store_inferior_registers (struct target_ops *ops,
85 struct regcache *regcache, int regnum)
5bf970f9
AC
86{
87}
88
5bf970f9
AC
89static void
90inf_child_post_attach (int pid)
91{
92 /* This version of Unix doesn't require a meaningful "post attach"
93 operation by a debugger. */
94}
95
96/* Get ready to modify the registers array. On machines which store
97 individual registers, this doesn't need to do anything. On
98 machines which store all the registers in one fell swoop, this
99 makes sure that registers contains all the registers from the
100 program being debugged. */
101
102static void
316f2060 103inf_child_prepare_to_store (struct regcache *regcache)
5bf970f9
AC
104{
105}
106
107static void
108inf_child_open (char *arg, int from_tty)
109{
8a3fe4f8 110 error (_("Use the \"run\" command to start a Unix child process."));
5bf970f9
AC
111}
112
113static void
114inf_child_post_startup_inferior (ptid_t ptid)
115{
116 /* This version of Unix doesn't require a meaningful "post startup
117 inferior" operation by a debugger. */
118}
119
5bf970f9 120static int
ee057212 121inf_child_follow_fork (struct target_ops *ops, int follow_child)
5bf970f9
AC
122{
123 /* This version of Unix doesn't support following fork or vfork
124 events. */
125 return 0;
126}
127
5bf970f9
AC
128static int
129inf_child_can_run (void)
130{
131 return 1;
132}
133
5bf970f9
AC
134static char *
135inf_child_pid_to_exec_file (int pid)
136{
137 /* This version of Unix doesn't support translation of a process ID
138 to the filename of the executable file. */
139 return NULL;
140}
141
7313baad
UW
142
143/* Target file operations. */
144
145static int
146inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
147{
148 int open_flags = 0;
149
150 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
151 return -1;
152
153 if (fileio_open_flags & FILEIO_O_CREAT)
154 open_flags |= O_CREAT;
155 if (fileio_open_flags & FILEIO_O_EXCL)
156 open_flags |= O_EXCL;
157 if (fileio_open_flags & FILEIO_O_TRUNC)
158 open_flags |= O_TRUNC;
159 if (fileio_open_flags & FILEIO_O_APPEND)
160 open_flags |= O_APPEND;
161 if (fileio_open_flags & FILEIO_O_RDONLY)
162 open_flags |= O_RDONLY;
163 if (fileio_open_flags & FILEIO_O_WRONLY)
164 open_flags |= O_WRONLY;
165 if (fileio_open_flags & FILEIO_O_RDWR)
166 open_flags |= O_RDWR;
167/* On systems supporting binary and text mode, always open files in
168 binary mode. */
169#ifdef O_BINARY
170 open_flags |= O_BINARY;
171#endif
172
173 *open_flags_p = open_flags;
174 return 0;
175}
176
177static int
178inf_child_errno_to_fileio_error (int errnum)
179{
180 switch (errnum)
181 {
182 case EPERM:
183 return FILEIO_EPERM;
184 case ENOENT:
185 return FILEIO_ENOENT;
186 case EINTR:
187 return FILEIO_EINTR;
188 case EIO:
189 return FILEIO_EIO;
190 case EBADF:
191 return FILEIO_EBADF;
192 case EACCES:
193 return FILEIO_EACCES;
194 case EFAULT:
195 return FILEIO_EFAULT;
196 case EBUSY:
197 return FILEIO_EBUSY;
198 case EEXIST:
199 return FILEIO_EEXIST;
200 case ENODEV:
201 return FILEIO_ENODEV;
202 case ENOTDIR:
203 return FILEIO_ENOTDIR;
204 case EISDIR:
205 return FILEIO_EISDIR;
206 case EINVAL:
207 return FILEIO_EINVAL;
208 case ENFILE:
209 return FILEIO_ENFILE;
210 case EMFILE:
211 return FILEIO_EMFILE;
212 case EFBIG:
213 return FILEIO_EFBIG;
214 case ENOSPC:
215 return FILEIO_ENOSPC;
216 case ESPIPE:
217 return FILEIO_ESPIPE;
218 case EROFS:
219 return FILEIO_EROFS;
220 case ENOSYS:
221 return FILEIO_ENOSYS;
222 case ENAMETOOLONG:
223 return FILEIO_ENAMETOOLONG;
224 }
225 return FILEIO_EUNKNOWN;
226}
227
228/* Open FILENAME on the target, using FLAGS and MODE. Return a
229 target file descriptor, or -1 if an error occurs (and set
230 *TARGET_ERRNO). */
231static int
232inf_child_fileio_open (const char *filename, int flags, int mode,
233 int *target_errno)
234{
235 int nat_flags;
236 int fd;
237
238 if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
239 {
240 *target_errno = FILEIO_EINVAL;
241 return -1;
242 }
243
244 /* We do not need to convert MODE, since the fileio protocol uses
245 the standard values. */
614c279d 246 fd = gdb_open_cloexec (filename, nat_flags, mode);
7313baad
UW
247 if (fd == -1)
248 *target_errno = inf_child_errno_to_fileio_error (errno);
249
250 return fd;
251}
252
253/* Write up to LEN bytes from WRITE_BUF to FD on the target.
254 Return the number of bytes written, or -1 if an error occurs
255 (and set *TARGET_ERRNO). */
256static int
257inf_child_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
258 ULONGEST offset, int *target_errno)
259{
260 int ret;
261
262#ifdef HAVE_PWRITE
263 ret = pwrite (fd, write_buf, len, (long) offset);
264#else
7c3270ae 265 ret = -1;
7313baad 266#endif
7c3270ae
UW
267 /* If we have no pwrite or it failed for this file, use lseek/write. */
268 if (ret == -1)
269 {
270 ret = lseek (fd, (long) offset, SEEK_SET);
271 if (ret != -1)
272 ret = write (fd, write_buf, len);
273 }
7313baad
UW
274
275 if (ret == -1)
276 *target_errno = inf_child_errno_to_fileio_error (errno);
277
278 return ret;
279}
280
281/* Read up to LEN bytes FD on the target into READ_BUF.
282 Return the number of bytes read, or -1 if an error occurs
283 (and set *TARGET_ERRNO). */
284static int
285inf_child_fileio_pread (int fd, gdb_byte *read_buf, int len,
286 ULONGEST offset, int *target_errno)
287{
288 int ret;
289
290#ifdef HAVE_PREAD
291 ret = pread (fd, read_buf, len, (long) offset);
292#else
7c3270ae 293 ret = -1;
7313baad 294#endif
7c3270ae
UW
295 /* If we have no pread or it failed for this file, use lseek/read. */
296 if (ret == -1)
297 {
298 ret = lseek (fd, (long) offset, SEEK_SET);
299 if (ret != -1)
300 ret = read (fd, read_buf, len);
301 }
7313baad
UW
302
303 if (ret == -1)
304 *target_errno = inf_child_errno_to_fileio_error (errno);
305
306 return ret;
307}
308
309/* Close FD on the target. Return 0, or -1 if an error occurs
310 (and set *TARGET_ERRNO). */
311static int
312inf_child_fileio_close (int fd, int *target_errno)
313{
314 int ret;
315
316 ret = close (fd);
317 if (ret == -1)
318 *target_errno = inf_child_errno_to_fileio_error (errno);
319
320 return ret;
321}
322
323/* Unlink FILENAME on the target. Return 0, or -1 if an error
324 occurs (and set *TARGET_ERRNO). */
325static int
326inf_child_fileio_unlink (const char *filename, int *target_errno)
327{
328 int ret;
329
330 ret = unlink (filename);
331 if (ret == -1)
332 *target_errno = inf_child_errno_to_fileio_error (errno);
333
334 return ret;
335}
336
b9e7b9c3
UW
337/* Read value of symbolic link FILENAME on the target. Return a
338 null-terminated string allocated via xmalloc, or NULL if an error
339 occurs (and set *TARGET_ERRNO). */
340static char *
341inf_child_fileio_readlink (const char *filename, int *target_errno)
342{
343 /* We support readlink only on systems that also provide a compile-time
d8d2a3ee
PA
344 maximum path length (PATH_MAX), at least for now. */
345#if defined (HAVE_READLINK) && defined (PATH_MAX)
346 char buf[PATH_MAX];
b9e7b9c3
UW
347 int len;
348 char *ret;
349
350 len = readlink (filename, buf, sizeof buf);
351 if (len < 0)
352 {
353 *target_errno = inf_child_errno_to_fileio_error (errno);
354 return NULL;
355 }
356
357 ret = xmalloc (len + 1);
358 memcpy (ret, buf, len);
359 ret[len] = '\0';
360 return ret;
361#else
362 *target_errno = FILEIO_ENOSYS;
363 return NULL;
364#endif
365}
366
5808517f
YQ
367static int
368inf_child_use_agent (int use)
369{
370 if (agent_loaded_p ())
371 {
372 use_agent = use;
373 return 1;
374 }
375 else
376 return 0;
377}
378
379static int
380inf_child_can_use_agent (void)
381{
382 return agent_loaded_p ();
383}
7313baad 384
5bf970f9
AC
385struct target_ops *
386inf_child_target (void)
387{
388 struct target_ops *t = XZALLOC (struct target_ops);
abbb1732 389
5bf970f9
AC
390 t->to_shortname = "child";
391 t->to_longname = "Unix child process";
392 t->to_doc = "Unix child process (started by the \"run\" command).";
393 t->to_open = inf_child_open;
394 t->to_post_attach = inf_child_post_attach;
7681f339
AC
395 t->to_fetch_registers = inf_child_fetch_inferior_registers;
396 t->to_store_registers = inf_child_store_inferior_registers;
5bf970f9
AC
397 t->to_prepare_to_store = inf_child_prepare_to_store;
398 t->to_insert_breakpoint = memory_insert_breakpoint;
399 t->to_remove_breakpoint = memory_remove_breakpoint;
400 t->to_terminal_init = terminal_init_inferior;
401 t->to_terminal_inferior = terminal_inferior;
402 t->to_terminal_ours_for_output = terminal_ours_for_output;
403 t->to_terminal_save_ours = terminal_save_ours;
404 t->to_terminal_ours = terminal_ours;
405 t->to_terminal_info = child_terminal_info;
406 t->to_post_startup_inferior = inf_child_post_startup_inferior;
5bf970f9 407 t->to_follow_fork = inf_child_follow_fork;
5bf970f9 408 t->to_can_run = inf_child_can_run;
5bf970f9
AC
409 t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
410 t->to_stratum = process_stratum;
c35b1492
PA
411 t->to_has_all_memory = default_child_has_all_memory;
412 t->to_has_memory = default_child_has_memory;
413 t->to_has_stack = default_child_has_stack;
414 t->to_has_registers = default_child_has_registers;
415 t->to_has_execution = default_child_has_execution;
7313baad
UW
416 t->to_fileio_open = inf_child_fileio_open;
417 t->to_fileio_pwrite = inf_child_fileio_pwrite;
418 t->to_fileio_pread = inf_child_fileio_pread;
419 t->to_fileio_close = inf_child_fileio_close;
420 t->to_fileio_unlink = inf_child_fileio_unlink;
b9e7b9c3 421 t->to_fileio_readlink = inf_child_fileio_readlink;
5bf970f9 422 t->to_magic = OPS_MAGIC;
5808517f
YQ
423 t->to_use_agent = inf_child_use_agent;
424 t->to_can_use_agent = inf_child_can_use_agent;
5bf970f9
AC
425 return t;
426}
This page took 0.676888 seconds and 4 git commands to generate.