1 /* Default child (native) target interface, for GDB when running under
4 Copyright (C) 1988-2013 Free Software Foundation, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "gdb_string.h"
29 #include "inf-child.h"
30 #include "gdb/fileio.h"
33 #include "filestuff.h"
35 #include <sys/types.h>
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. */
43 store_waitstatus (struct target_waitstatus
*ourstatus
, int hoststatus
)
45 if (WIFEXITED (hoststatus
))
47 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
48 ourstatus
->value
.integer
= WEXITSTATUS (hoststatus
);
50 else if (!WIFSTOPPED (hoststatus
))
52 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
53 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (hoststatus
));
57 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
58 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (hoststatus
));
62 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
66 inf_child_fetch_inferior_registers (struct target_ops
*ops
,
67 struct regcache
*regcache
, int regnum
)
72 regnum
< gdbarch_num_regs (get_regcache_arch (regcache
));
74 regcache_raw_supply (regcache
, regnum
, NULL
);
77 regcache_raw_supply (regcache
, regnum
, NULL
);
80 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
81 this for all registers (including the floating point registers). */
84 inf_child_store_inferior_registers (struct target_ops
*ops
,
85 struct regcache
*regcache
, int regnum
)
90 inf_child_post_attach (int pid
)
92 /* This version of Unix doesn't require a meaningful "post attach"
93 operation by a debugger. */
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. */
103 inf_child_prepare_to_store (struct regcache
*regcache
)
108 inf_child_open (char *arg
, int from_tty
)
110 error (_("Use the \"run\" command to start a Unix child process."));
114 inf_child_post_startup_inferior (ptid_t ptid
)
116 /* This version of Unix doesn't require a meaningful "post startup
117 inferior" operation by a debugger. */
121 inf_child_follow_fork (struct target_ops
*ops
, int follow_child
,
124 /* This version of Unix doesn't support following fork or vfork
130 inf_child_can_run (void)
136 inf_child_pid_to_exec_file (int pid
)
138 /* This version of Unix doesn't support translation of a process ID
139 to the filename of the executable file. */
144 /* Target file operations. */
147 inf_child_fileio_open_flags_to_host (int fileio_open_flags
, int *open_flags_p
)
151 if (fileio_open_flags
& ~FILEIO_O_SUPPORTED
)
154 if (fileio_open_flags
& FILEIO_O_CREAT
)
155 open_flags
|= O_CREAT
;
156 if (fileio_open_flags
& FILEIO_O_EXCL
)
157 open_flags
|= O_EXCL
;
158 if (fileio_open_flags
& FILEIO_O_TRUNC
)
159 open_flags
|= O_TRUNC
;
160 if (fileio_open_flags
& FILEIO_O_APPEND
)
161 open_flags
|= O_APPEND
;
162 if (fileio_open_flags
& FILEIO_O_RDONLY
)
163 open_flags
|= O_RDONLY
;
164 if (fileio_open_flags
& FILEIO_O_WRONLY
)
165 open_flags
|= O_WRONLY
;
166 if (fileio_open_flags
& FILEIO_O_RDWR
)
167 open_flags
|= O_RDWR
;
168 /* On systems supporting binary and text mode, always open files in
171 open_flags
|= O_BINARY
;
174 *open_flags_p
= open_flags
;
179 inf_child_errno_to_fileio_error (int errnum
)
186 return FILEIO_ENOENT
;
194 return FILEIO_EACCES
;
196 return FILEIO_EFAULT
;
200 return FILEIO_EEXIST
;
202 return FILEIO_ENODEV
;
204 return FILEIO_ENOTDIR
;
206 return FILEIO_EISDIR
;
208 return FILEIO_EINVAL
;
210 return FILEIO_ENFILE
;
212 return FILEIO_EMFILE
;
216 return FILEIO_ENOSPC
;
218 return FILEIO_ESPIPE
;
222 return FILEIO_ENOSYS
;
224 return FILEIO_ENAMETOOLONG
;
226 return FILEIO_EUNKNOWN
;
229 /* Open FILENAME on the target, using FLAGS and MODE. Return a
230 target file descriptor, or -1 if an error occurs (and set
233 inf_child_fileio_open (const char *filename
, int flags
, int mode
,
239 if (inf_child_fileio_open_flags_to_host (flags
, &nat_flags
) == -1)
241 *target_errno
= FILEIO_EINVAL
;
245 /* We do not need to convert MODE, since the fileio protocol uses
246 the standard values. */
247 fd
= gdb_open_cloexec (filename
, nat_flags
, mode
);
249 *target_errno
= inf_child_errno_to_fileio_error (errno
);
254 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
255 Return the number of bytes written, or -1 if an error occurs
256 (and set *TARGET_ERRNO). */
258 inf_child_fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
259 ULONGEST offset
, int *target_errno
)
264 ret
= pwrite (fd
, write_buf
, len
, (long) offset
);
268 /* If we have no pwrite or it failed for this file, use lseek/write. */
271 ret
= lseek (fd
, (long) offset
, SEEK_SET
);
273 ret
= write (fd
, write_buf
, len
);
277 *target_errno
= inf_child_errno_to_fileio_error (errno
);
282 /* Read up to LEN bytes FD on the target into READ_BUF.
283 Return the number of bytes read, or -1 if an error occurs
284 (and set *TARGET_ERRNO). */
286 inf_child_fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
287 ULONGEST offset
, int *target_errno
)
292 ret
= pread (fd
, read_buf
, len
, (long) offset
);
296 /* If we have no pread or it failed for this file, use lseek/read. */
299 ret
= lseek (fd
, (long) offset
, SEEK_SET
);
301 ret
= read (fd
, read_buf
, len
);
305 *target_errno
= inf_child_errno_to_fileio_error (errno
);
310 /* Close FD on the target. Return 0, or -1 if an error occurs
311 (and set *TARGET_ERRNO). */
313 inf_child_fileio_close (int fd
, int *target_errno
)
319 *target_errno
= inf_child_errno_to_fileio_error (errno
);
324 /* Unlink FILENAME on the target. Return 0, or -1 if an error
325 occurs (and set *TARGET_ERRNO). */
327 inf_child_fileio_unlink (const char *filename
, int *target_errno
)
331 ret
= unlink (filename
);
333 *target_errno
= inf_child_errno_to_fileio_error (errno
);
338 /* Read value of symbolic link FILENAME on the target. Return a
339 null-terminated string allocated via xmalloc, or NULL if an error
340 occurs (and set *TARGET_ERRNO). */
342 inf_child_fileio_readlink (const char *filename
, int *target_errno
)
344 /* We support readlink only on systems that also provide a compile-time
345 maximum path length (PATH_MAX), at least for now. */
346 #if defined (HAVE_READLINK) && defined (PATH_MAX)
351 len
= readlink (filename
, buf
, sizeof buf
);
354 *target_errno
= inf_child_errno_to_fileio_error (errno
);
358 ret
= xmalloc (len
+ 1);
359 memcpy (ret
, buf
, len
);
363 *target_errno
= FILEIO_ENOSYS
;
369 inf_child_use_agent (int use
)
371 if (agent_loaded_p ())
381 inf_child_can_use_agent (void)
383 return agent_loaded_p ();
387 inf_child_target (void)
389 struct target_ops
*t
= XZALLOC (struct target_ops
);
391 t
->to_shortname
= "child";
392 t
->to_longname
= "Unix child process";
393 t
->to_doc
= "Unix child process (started by the \"run\" command).";
394 t
->to_open
= inf_child_open
;
395 t
->to_post_attach
= inf_child_post_attach
;
396 t
->to_fetch_registers
= inf_child_fetch_inferior_registers
;
397 t
->to_store_registers
= inf_child_store_inferior_registers
;
398 t
->to_prepare_to_store
= inf_child_prepare_to_store
;
399 t
->to_insert_breakpoint
= memory_insert_breakpoint
;
400 t
->to_remove_breakpoint
= memory_remove_breakpoint
;
401 t
->to_terminal_init
= terminal_init_inferior
;
402 t
->to_terminal_inferior
= terminal_inferior
;
403 t
->to_terminal_ours_for_output
= terminal_ours_for_output
;
404 t
->to_terminal_save_ours
= terminal_save_ours
;
405 t
->to_terminal_ours
= terminal_ours
;
406 t
->to_terminal_info
= child_terminal_info
;
407 t
->to_post_startup_inferior
= inf_child_post_startup_inferior
;
408 t
->to_follow_fork
= inf_child_follow_fork
;
409 t
->to_can_run
= inf_child_can_run
;
410 t
->to_pid_to_exec_file
= inf_child_pid_to_exec_file
;
411 t
->to_stratum
= process_stratum
;
412 t
->to_has_all_memory
= default_child_has_all_memory
;
413 t
->to_has_memory
= default_child_has_memory
;
414 t
->to_has_stack
= default_child_has_stack
;
415 t
->to_has_registers
= default_child_has_registers
;
416 t
->to_has_execution
= default_child_has_execution
;
417 t
->to_fileio_open
= inf_child_fileio_open
;
418 t
->to_fileio_pwrite
= inf_child_fileio_pwrite
;
419 t
->to_fileio_pread
= inf_child_fileio_pread
;
420 t
->to_fileio_close
= inf_child_fileio_close
;
421 t
->to_fileio_unlink
= inf_child_fileio_unlink
;
422 t
->to_fileio_readlink
= inf_child_fileio_readlink
;
423 t
->to_magic
= OPS_MAGIC
;
424 t
->to_use_agent
= inf_child_use_agent
;
425 t
->to_can_use_agent
= inf_child_can_use_agent
;