1 /* Default child (native) target interface, for GDB when running under
4 Copyright (C) 1988-1996, 1998-2002, 2004-2005, 2007-2012 Free
5 Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "gdb_string.h"
30 #include "inf-child.h"
31 #include "gdb/fileio.h"
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h> /* for MAXPATHLEN */
38 #include <sys/types.h>
42 /* Helper function for child_wait and the derivatives of child_wait.
43 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
44 translation of that in OURSTATUS. */
46 store_waitstatus (struct target_waitstatus
*ourstatus
, int hoststatus
)
48 if (WIFEXITED (hoststatus
))
50 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
51 ourstatus
->value
.integer
= WEXITSTATUS (hoststatus
);
53 else if (!WIFSTOPPED (hoststatus
))
55 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
56 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (hoststatus
));
60 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
61 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (hoststatus
));
65 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
69 inf_child_fetch_inferior_registers (struct target_ops
*ops
,
70 struct regcache
*regcache
, int regnum
)
75 regnum
< gdbarch_num_regs (get_regcache_arch (regcache
));
77 regcache_raw_supply (regcache
, regnum
, NULL
);
80 regcache_raw_supply (regcache
, regnum
, NULL
);
83 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
84 this for all registers (including the floating point registers). */
87 inf_child_store_inferior_registers (struct target_ops
*ops
,
88 struct regcache
*regcache
, int regnum
)
93 inf_child_post_attach (int pid
)
95 /* This version of Unix doesn't require a meaningful "post attach"
96 operation by a debugger. */
99 /* Get ready to modify the registers array. On machines which store
100 individual registers, this doesn't need to do anything. On
101 machines which store all the registers in one fell swoop, this
102 makes sure that registers contains all the registers from the
103 program being debugged. */
106 inf_child_prepare_to_store (struct regcache
*regcache
)
111 inf_child_open (char *arg
, int from_tty
)
113 error (_("Use the \"run\" command to start a Unix child process."));
117 inf_child_post_startup_inferior (ptid_t ptid
)
119 /* This version of Unix doesn't require a meaningful "post startup
120 inferior" operation by a debugger. */
124 inf_child_follow_fork (struct target_ops
*ops
, int follow_child
)
126 /* This version of Unix doesn't support following fork or vfork
132 inf_child_can_run (void)
138 inf_child_pid_to_exec_file (int pid
)
140 /* This version of Unix doesn't support translation of a process ID
141 to the filename of the executable file. */
146 /* Target file operations. */
149 inf_child_fileio_open_flags_to_host (int fileio_open_flags
, int *open_flags_p
)
153 if (fileio_open_flags
& ~FILEIO_O_SUPPORTED
)
156 if (fileio_open_flags
& FILEIO_O_CREAT
)
157 open_flags
|= O_CREAT
;
158 if (fileio_open_flags
& FILEIO_O_EXCL
)
159 open_flags
|= O_EXCL
;
160 if (fileio_open_flags
& FILEIO_O_TRUNC
)
161 open_flags
|= O_TRUNC
;
162 if (fileio_open_flags
& FILEIO_O_APPEND
)
163 open_flags
|= O_APPEND
;
164 if (fileio_open_flags
& FILEIO_O_RDONLY
)
165 open_flags
|= O_RDONLY
;
166 if (fileio_open_flags
& FILEIO_O_WRONLY
)
167 open_flags
|= O_WRONLY
;
168 if (fileio_open_flags
& FILEIO_O_RDWR
)
169 open_flags
|= O_RDWR
;
170 /* On systems supporting binary and text mode, always open files in
173 open_flags
|= O_BINARY
;
176 *open_flags_p
= open_flags
;
181 inf_child_errno_to_fileio_error (int errnum
)
188 return FILEIO_ENOENT
;
196 return FILEIO_EACCES
;
198 return FILEIO_EFAULT
;
202 return FILEIO_EEXIST
;
204 return FILEIO_ENODEV
;
206 return FILEIO_ENOTDIR
;
208 return FILEIO_EISDIR
;
210 return FILEIO_EINVAL
;
212 return FILEIO_ENFILE
;
214 return FILEIO_EMFILE
;
218 return FILEIO_ENOSPC
;
220 return FILEIO_ESPIPE
;
224 return FILEIO_ENOSYS
;
226 return FILEIO_ENAMETOOLONG
;
228 return FILEIO_EUNKNOWN
;
231 /* Open FILENAME on the target, using FLAGS and MODE. Return a
232 target file descriptor, or -1 if an error occurs (and set
235 inf_child_fileio_open (const char *filename
, int flags
, int mode
,
241 if (inf_child_fileio_open_flags_to_host (flags
, &nat_flags
) == -1)
243 *target_errno
= FILEIO_EINVAL
;
247 /* We do not need to convert MODE, since the fileio protocol uses
248 the standard values. */
249 fd
= open (filename
, nat_flags
, mode
);
251 *target_errno
= inf_child_errno_to_fileio_error (errno
);
256 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
257 Return the number of bytes written, or -1 if an error occurs
258 (and set *TARGET_ERRNO). */
260 inf_child_fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
261 ULONGEST offset
, int *target_errno
)
266 ret
= pwrite (fd
, write_buf
, len
, (long) offset
);
270 /* If we have no pwrite or it failed for this file, use lseek/write. */
273 ret
= lseek (fd
, (long) offset
, SEEK_SET
);
275 ret
= write (fd
, write_buf
, len
);
279 *target_errno
= inf_child_errno_to_fileio_error (errno
);
284 /* Read up to LEN bytes FD on the target into READ_BUF.
285 Return the number of bytes read, or -1 if an error occurs
286 (and set *TARGET_ERRNO). */
288 inf_child_fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
289 ULONGEST offset
, int *target_errno
)
294 ret
= pread (fd
, read_buf
, len
, (long) offset
);
298 /* If we have no pread or it failed for this file, use lseek/read. */
301 ret
= lseek (fd
, (long) offset
, SEEK_SET
);
303 ret
= read (fd
, read_buf
, len
);
307 *target_errno
= inf_child_errno_to_fileio_error (errno
);
312 /* Close FD on the target. Return 0, or -1 if an error occurs
313 (and set *TARGET_ERRNO). */
315 inf_child_fileio_close (int fd
, int *target_errno
)
321 *target_errno
= inf_child_errno_to_fileio_error (errno
);
326 /* Unlink FILENAME on the target. Return 0, or -1 if an error
327 occurs (and set *TARGET_ERRNO). */
329 inf_child_fileio_unlink (const char *filename
, int *target_errno
)
333 ret
= unlink (filename
);
335 *target_errno
= inf_child_errno_to_fileio_error (errno
);
340 /* Read value of symbolic link FILENAME on the target. Return a
341 null-terminated string allocated via xmalloc, or NULL if an error
342 occurs (and set *TARGET_ERRNO). */
344 inf_child_fileio_readlink (const char *filename
, int *target_errno
)
346 /* We support readlink only on systems that also provide a compile-time
347 maximum path length (MAXPATHLEN), at least for now. */
348 #if defined (HAVE_READLINK) && defined (MAXPATHLEN)
349 char buf
[MAXPATHLEN
];
353 len
= readlink (filename
, buf
, sizeof buf
);
356 *target_errno
= inf_child_errno_to_fileio_error (errno
);
360 ret
= xmalloc (len
+ 1);
361 memcpy (ret
, buf
, len
);
365 *target_errno
= FILEIO_ENOSYS
;
371 inf_child_use_agent (int use
)
373 if (agent_loaded_p ())
383 inf_child_can_use_agent (void)
385 return agent_loaded_p ();
389 inf_child_target (void)
391 struct target_ops
*t
= XZALLOC (struct target_ops
);
393 t
->to_shortname
= "child";
394 t
->to_longname
= "Unix child process";
395 t
->to_doc
= "Unix child process (started by the \"run\" command).";
396 t
->to_open
= inf_child_open
;
397 t
->to_post_attach
= inf_child_post_attach
;
398 t
->to_fetch_registers
= inf_child_fetch_inferior_registers
;
399 t
->to_store_registers
= inf_child_store_inferior_registers
;
400 t
->to_prepare_to_store
= inf_child_prepare_to_store
;
401 t
->to_insert_breakpoint
= memory_insert_breakpoint
;
402 t
->to_remove_breakpoint
= memory_remove_breakpoint
;
403 t
->to_terminal_init
= terminal_init_inferior
;
404 t
->to_terminal_inferior
= terminal_inferior
;
405 t
->to_terminal_ours_for_output
= terminal_ours_for_output
;
406 t
->to_terminal_save_ours
= terminal_save_ours
;
407 t
->to_terminal_ours
= terminal_ours
;
408 t
->to_terminal_info
= child_terminal_info
;
409 t
->to_post_startup_inferior
= inf_child_post_startup_inferior
;
410 t
->to_follow_fork
= inf_child_follow_fork
;
411 t
->to_can_run
= inf_child_can_run
;
412 t
->to_pid_to_exec_file
= inf_child_pid_to_exec_file
;
413 t
->to_stratum
= process_stratum
;
414 t
->to_has_all_memory
= default_child_has_all_memory
;
415 t
->to_has_memory
= default_child_has_memory
;
416 t
->to_has_stack
= default_child_has_stack
;
417 t
->to_has_registers
= default_child_has_registers
;
418 t
->to_has_execution
= default_child_has_execution
;
419 t
->to_fileio_open
= inf_child_fileio_open
;
420 t
->to_fileio_pwrite
= inf_child_fileio_pwrite
;
421 t
->to_fileio_pread
= inf_child_fileio_pread
;
422 t
->to_fileio_close
= inf_child_fileio_close
;
423 t
->to_fileio_unlink
= inf_child_fileio_unlink
;
424 t
->to_fileio_readlink
= inf_child_fileio_readlink
;
425 t
->to_magic
= OPS_MAGIC
;
426 t
->to_use_agent
= inf_child_use_agent
;
427 t
->to_can_use_agent
= inf_child_can_use_agent
;