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