ChangeLog:
[deliverable/binutils-gdb.git] / gdb / inf-child.c
1 /* Default child (native) target interface, for GDB when running under
2 Unix.
3
4 Copyright (C) 1988-1996, 1998-2002, 2004-2005, 2007-2012 Free
5 Software Foundation, Inc.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22 #include "defs.h"
23 #include "regcache.h"
24 #include "memattr.h"
25 #include "symtab.h"
26 #include "target.h"
27 #include "inferior.h"
28 #include "gdb_string.h"
29 #include "inf-child.h"
30 #include "gdb/fileio.h"
31
32 #ifdef HAVE_SYS_PARAM_H
33 #include <sys/param.h> /* for MAXPATHLEN */
34 #endif
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39
40 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
41 for all registers. */
42
43 static void
44 inf_child_fetch_inferior_registers (struct target_ops *ops,
45 struct regcache *regcache, int regnum)
46 {
47 if (regnum == -1)
48 {
49 for (regnum = 0;
50 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
51 regnum++)
52 regcache_raw_supply (regcache, regnum, NULL);
53 }
54 else
55 regcache_raw_supply (regcache, regnum, NULL);
56 }
57
58 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
59 this for all registers (including the floating point registers). */
60
61 static void
62 inf_child_store_inferior_registers (struct target_ops *ops,
63 struct regcache *regcache, int regnum)
64 {
65 }
66
67 static void
68 inf_child_post_attach (int pid)
69 {
70 /* This version of Unix doesn't require a meaningful "post attach"
71 operation by a debugger. */
72 }
73
74 /* Get ready to modify the registers array. On machines which store
75 individual registers, this doesn't need to do anything. On
76 machines which store all the registers in one fell swoop, this
77 makes sure that registers contains all the registers from the
78 program being debugged. */
79
80 static void
81 inf_child_prepare_to_store (struct regcache *regcache)
82 {
83 }
84
85 static void
86 inf_child_open (char *arg, int from_tty)
87 {
88 error (_("Use the \"run\" command to start a Unix child process."));
89 }
90
91 static void
92 inf_child_post_startup_inferior (ptid_t ptid)
93 {
94 /* This version of Unix doesn't require a meaningful "post startup
95 inferior" operation by a debugger. */
96 }
97
98 static int
99 inf_child_follow_fork (struct target_ops *ops, int follow_child)
100 {
101 /* This version of Unix doesn't support following fork or vfork
102 events. */
103 return 0;
104 }
105
106 static int
107 inf_child_can_run (void)
108 {
109 return 1;
110 }
111
112 static char *
113 inf_child_pid_to_exec_file (int pid)
114 {
115 /* This version of Unix doesn't support translation of a process ID
116 to the filename of the executable file. */
117 return NULL;
118 }
119
120
121 /* Target file operations. */
122
123 static int
124 inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
125 {
126 int open_flags = 0;
127
128 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
129 return -1;
130
131 if (fileio_open_flags & FILEIO_O_CREAT)
132 open_flags |= O_CREAT;
133 if (fileio_open_flags & FILEIO_O_EXCL)
134 open_flags |= O_EXCL;
135 if (fileio_open_flags & FILEIO_O_TRUNC)
136 open_flags |= O_TRUNC;
137 if (fileio_open_flags & FILEIO_O_APPEND)
138 open_flags |= O_APPEND;
139 if (fileio_open_flags & FILEIO_O_RDONLY)
140 open_flags |= O_RDONLY;
141 if (fileio_open_flags & FILEIO_O_WRONLY)
142 open_flags |= O_WRONLY;
143 if (fileio_open_flags & FILEIO_O_RDWR)
144 open_flags |= O_RDWR;
145 /* On systems supporting binary and text mode, always open files in
146 binary mode. */
147 #ifdef O_BINARY
148 open_flags |= O_BINARY;
149 #endif
150
151 *open_flags_p = open_flags;
152 return 0;
153 }
154
155 static int
156 inf_child_errno_to_fileio_error (int errnum)
157 {
158 switch (errnum)
159 {
160 case EPERM:
161 return FILEIO_EPERM;
162 case ENOENT:
163 return FILEIO_ENOENT;
164 case EINTR:
165 return FILEIO_EINTR;
166 case EIO:
167 return FILEIO_EIO;
168 case EBADF:
169 return FILEIO_EBADF;
170 case EACCES:
171 return FILEIO_EACCES;
172 case EFAULT:
173 return FILEIO_EFAULT;
174 case EBUSY:
175 return FILEIO_EBUSY;
176 case EEXIST:
177 return FILEIO_EEXIST;
178 case ENODEV:
179 return FILEIO_ENODEV;
180 case ENOTDIR:
181 return FILEIO_ENOTDIR;
182 case EISDIR:
183 return FILEIO_EISDIR;
184 case EINVAL:
185 return FILEIO_EINVAL;
186 case ENFILE:
187 return FILEIO_ENFILE;
188 case EMFILE:
189 return FILEIO_EMFILE;
190 case EFBIG:
191 return FILEIO_EFBIG;
192 case ENOSPC:
193 return FILEIO_ENOSPC;
194 case ESPIPE:
195 return FILEIO_ESPIPE;
196 case EROFS:
197 return FILEIO_EROFS;
198 case ENOSYS:
199 return FILEIO_ENOSYS;
200 case ENAMETOOLONG:
201 return FILEIO_ENAMETOOLONG;
202 }
203 return FILEIO_EUNKNOWN;
204 }
205
206 /* Open FILENAME on the target, using FLAGS and MODE. Return a
207 target file descriptor, or -1 if an error occurs (and set
208 *TARGET_ERRNO). */
209 static int
210 inf_child_fileio_open (const char *filename, int flags, int mode,
211 int *target_errno)
212 {
213 int nat_flags;
214 int fd;
215
216 if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
217 {
218 *target_errno = FILEIO_EINVAL;
219 return -1;
220 }
221
222 /* We do not need to convert MODE, since the fileio protocol uses
223 the standard values. */
224 fd = open (filename, nat_flags, mode);
225 if (fd == -1)
226 *target_errno = inf_child_errno_to_fileio_error (errno);
227
228 return fd;
229 }
230
231 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
232 Return the number of bytes written, or -1 if an error occurs
233 (and set *TARGET_ERRNO). */
234 static int
235 inf_child_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
236 ULONGEST offset, int *target_errno)
237 {
238 int ret;
239
240 #ifdef HAVE_PWRITE
241 ret = pwrite (fd, write_buf, len, (long) offset);
242 #else
243 ret = lseek (fd, (long) offset, SEEK_SET);
244 if (ret != -1)
245 ret = write (fd, write_buf, len);
246 #endif
247
248 if (ret == -1)
249 *target_errno = inf_child_errno_to_fileio_error (errno);
250
251 return ret;
252 }
253
254 /* Read up to LEN bytes FD on the target into READ_BUF.
255 Return the number of bytes read, or -1 if an error occurs
256 (and set *TARGET_ERRNO). */
257 static int
258 inf_child_fileio_pread (int fd, gdb_byte *read_buf, int len,
259 ULONGEST offset, int *target_errno)
260 {
261 int ret;
262
263 #ifdef HAVE_PREAD
264 ret = pread (fd, read_buf, len, (long) offset);
265 #else
266 ret = lseek (fd, (long) offset, SEEK_SET);
267 if (ret != -1)
268 ret = read (fd, read_buf, len);
269 #endif
270
271 if (ret == -1)
272 *target_errno = inf_child_errno_to_fileio_error (errno);
273
274 return ret;
275 }
276
277 /* Close FD on the target. Return 0, or -1 if an error occurs
278 (and set *TARGET_ERRNO). */
279 static int
280 inf_child_fileio_close (int fd, int *target_errno)
281 {
282 int ret;
283
284 ret = close (fd);
285 if (ret == -1)
286 *target_errno = inf_child_errno_to_fileio_error (errno);
287
288 return ret;
289 }
290
291 /* Unlink FILENAME on the target. Return 0, or -1 if an error
292 occurs (and set *TARGET_ERRNO). */
293 static int
294 inf_child_fileio_unlink (const char *filename, int *target_errno)
295 {
296 int ret;
297
298 ret = unlink (filename);
299 if (ret == -1)
300 *target_errno = inf_child_errno_to_fileio_error (errno);
301
302 return ret;
303 }
304
305 /* Read value of symbolic link FILENAME on the target. Return a
306 null-terminated string allocated via xmalloc, or NULL if an error
307 occurs (and set *TARGET_ERRNO). */
308 static char *
309 inf_child_fileio_readlink (const char *filename, int *target_errno)
310 {
311 /* We support readlink only on systems that also provide a compile-time
312 maximum path length (MAXPATHLEN), at least for now. */
313 #if defined (HAVE_READLINK) && defined (MAXPATHLEN)
314 char buf[MAXPATHLEN];
315 int len;
316 char *ret;
317
318 len = readlink (filename, buf, sizeof buf);
319 if (len < 0)
320 {
321 *target_errno = inf_child_errno_to_fileio_error (errno);
322 return NULL;
323 }
324
325 ret = xmalloc (len + 1);
326 memcpy (ret, buf, len);
327 ret[len] = '\0';
328 return ret;
329 #else
330 *target_errno = FILEIO_ENOSYS;
331 return NULL;
332 #endif
333 }
334
335
336 struct target_ops *
337 inf_child_target (void)
338 {
339 struct target_ops *t = XZALLOC (struct target_ops);
340
341 t->to_shortname = "child";
342 t->to_longname = "Unix child process";
343 t->to_doc = "Unix child process (started by the \"run\" command).";
344 t->to_open = inf_child_open;
345 t->to_post_attach = inf_child_post_attach;
346 t->to_fetch_registers = inf_child_fetch_inferior_registers;
347 t->to_store_registers = inf_child_store_inferior_registers;
348 t->to_prepare_to_store = inf_child_prepare_to_store;
349 t->to_insert_breakpoint = memory_insert_breakpoint;
350 t->to_remove_breakpoint = memory_remove_breakpoint;
351 t->to_terminal_init = terminal_init_inferior;
352 t->to_terminal_inferior = terminal_inferior;
353 t->to_terminal_ours_for_output = terminal_ours_for_output;
354 t->to_terminal_save_ours = terminal_save_ours;
355 t->to_terminal_ours = terminal_ours;
356 t->to_terminal_info = child_terminal_info;
357 t->to_post_startup_inferior = inf_child_post_startup_inferior;
358 t->to_follow_fork = inf_child_follow_fork;
359 t->to_can_run = inf_child_can_run;
360 t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
361 t->to_stratum = process_stratum;
362 t->to_has_all_memory = default_child_has_all_memory;
363 t->to_has_memory = default_child_has_memory;
364 t->to_has_stack = default_child_has_stack;
365 t->to_has_registers = default_child_has_registers;
366 t->to_has_execution = default_child_has_execution;
367 t->to_fileio_open = inf_child_fileio_open;
368 t->to_fileio_pwrite = inf_child_fileio_pwrite;
369 t->to_fileio_pread = inf_child_fileio_pread;
370 t->to_fileio_close = inf_child_fileio_close;
371 t->to_fileio_unlink = inf_child_fileio_unlink;
372 t->to_fileio_readlink = inf_child_fileio_readlink;
373 t->to_magic = OPS_MAGIC;
374 return t;
375 }
This page took 0.038426 seconds and 5 git commands to generate.