import strstr and strerror modules
[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-2013 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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"
27 #include "gdb_string.h"
28 #include "gdb_stat.h"
29 #include "inf-child.h"
30 #include "gdb/fileio.h"
31 #include "agent.h"
32 #include "gdb_wait.h"
33 #include "filestuff.h"
34
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38
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. */
42 void
43 store_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;
53 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
54 }
55 else
56 {
57 ourstatus->kind = TARGET_WAITKIND_STOPPED;
58 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
59 }
60 }
61
62 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
63 for all registers. */
64
65 static void
66 inf_child_fetch_inferior_registers (struct target_ops *ops,
67 struct regcache *regcache, int regnum)
68 {
69 if (regnum == -1)
70 {
71 for (regnum = 0;
72 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
73 regnum++)
74 regcache_raw_supply (regcache, regnum, NULL);
75 }
76 else
77 regcache_raw_supply (regcache, regnum, NULL);
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
83 static void
84 inf_child_store_inferior_registers (struct target_ops *ops,
85 struct regcache *regcache, int regnum)
86 {
87 }
88
89 static void
90 inf_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
102 static void
103 inf_child_prepare_to_store (struct regcache *regcache)
104 {
105 }
106
107 static void
108 inf_child_open (char *arg, int from_tty)
109 {
110 error (_("Use the \"run\" command to start a Unix child process."));
111 }
112
113 static void
114 inf_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
120 static int
121 inf_child_follow_fork (struct target_ops *ops, int follow_child,
122 int detach_fork)
123 {
124 /* This version of Unix doesn't support following fork or vfork
125 events. */
126 return 0;
127 }
128
129 static int
130 inf_child_can_run (void)
131 {
132 return 1;
133 }
134
135 static char *
136 inf_child_pid_to_exec_file (int pid)
137 {
138 /* This version of Unix doesn't support translation of a process ID
139 to the filename of the executable file. */
140 return NULL;
141 }
142
143
144 /* Target file operations. */
145
146 static int
147 inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
148 {
149 int open_flags = 0;
150
151 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
152 return -1;
153
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
169 binary mode. */
170 #ifdef O_BINARY
171 open_flags |= O_BINARY;
172 #endif
173
174 *open_flags_p = open_flags;
175 return 0;
176 }
177
178 static int
179 inf_child_errno_to_fileio_error (int errnum)
180 {
181 switch (errnum)
182 {
183 case EPERM:
184 return FILEIO_EPERM;
185 case ENOENT:
186 return FILEIO_ENOENT;
187 case EINTR:
188 return FILEIO_EINTR;
189 case EIO:
190 return FILEIO_EIO;
191 case EBADF:
192 return FILEIO_EBADF;
193 case EACCES:
194 return FILEIO_EACCES;
195 case EFAULT:
196 return FILEIO_EFAULT;
197 case EBUSY:
198 return FILEIO_EBUSY;
199 case EEXIST:
200 return FILEIO_EEXIST;
201 case ENODEV:
202 return FILEIO_ENODEV;
203 case ENOTDIR:
204 return FILEIO_ENOTDIR;
205 case EISDIR:
206 return FILEIO_EISDIR;
207 case EINVAL:
208 return FILEIO_EINVAL;
209 case ENFILE:
210 return FILEIO_ENFILE;
211 case EMFILE:
212 return FILEIO_EMFILE;
213 case EFBIG:
214 return FILEIO_EFBIG;
215 case ENOSPC:
216 return FILEIO_ENOSPC;
217 case ESPIPE:
218 return FILEIO_ESPIPE;
219 case EROFS:
220 return FILEIO_EROFS;
221 case ENOSYS:
222 return FILEIO_ENOSYS;
223 case ENAMETOOLONG:
224 return FILEIO_ENAMETOOLONG;
225 }
226 return FILEIO_EUNKNOWN;
227 }
228
229 /* Open FILENAME on the target, using FLAGS and MODE. Return a
230 target file descriptor, or -1 if an error occurs (and set
231 *TARGET_ERRNO). */
232 static int
233 inf_child_fileio_open (const char *filename, int flags, int mode,
234 int *target_errno)
235 {
236 int nat_flags;
237 int fd;
238
239 if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
240 {
241 *target_errno = FILEIO_EINVAL;
242 return -1;
243 }
244
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);
248 if (fd == -1)
249 *target_errno = inf_child_errno_to_fileio_error (errno);
250
251 return fd;
252 }
253
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). */
257 static int
258 inf_child_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
259 ULONGEST offset, int *target_errno)
260 {
261 int ret;
262
263 #ifdef HAVE_PWRITE
264 ret = pwrite (fd, write_buf, len, (long) offset);
265 #else
266 ret = -1;
267 #endif
268 /* If we have no pwrite or it failed for this file, use lseek/write. */
269 if (ret == -1)
270 {
271 ret = lseek (fd, (long) offset, SEEK_SET);
272 if (ret != -1)
273 ret = write (fd, write_buf, len);
274 }
275
276 if (ret == -1)
277 *target_errno = inf_child_errno_to_fileio_error (errno);
278
279 return ret;
280 }
281
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). */
285 static int
286 inf_child_fileio_pread (int fd, gdb_byte *read_buf, int len,
287 ULONGEST offset, int *target_errno)
288 {
289 int ret;
290
291 #ifdef HAVE_PREAD
292 ret = pread (fd, read_buf, len, (long) offset);
293 #else
294 ret = -1;
295 #endif
296 /* If we have no pread or it failed for this file, use lseek/read. */
297 if (ret == -1)
298 {
299 ret = lseek (fd, (long) offset, SEEK_SET);
300 if (ret != -1)
301 ret = read (fd, read_buf, len);
302 }
303
304 if (ret == -1)
305 *target_errno = inf_child_errno_to_fileio_error (errno);
306
307 return ret;
308 }
309
310 /* Close FD on the target. Return 0, or -1 if an error occurs
311 (and set *TARGET_ERRNO). */
312 static int
313 inf_child_fileio_close (int fd, int *target_errno)
314 {
315 int ret;
316
317 ret = close (fd);
318 if (ret == -1)
319 *target_errno = inf_child_errno_to_fileio_error (errno);
320
321 return ret;
322 }
323
324 /* Unlink FILENAME on the target. Return 0, or -1 if an error
325 occurs (and set *TARGET_ERRNO). */
326 static int
327 inf_child_fileio_unlink (const char *filename, int *target_errno)
328 {
329 int ret;
330
331 ret = unlink (filename);
332 if (ret == -1)
333 *target_errno = inf_child_errno_to_fileio_error (errno);
334
335 return ret;
336 }
337
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). */
341 static char *
342 inf_child_fileio_readlink (const char *filename, int *target_errno)
343 {
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)
347 char buf[PATH_MAX];
348 int len;
349 char *ret;
350
351 len = readlink (filename, buf, sizeof buf);
352 if (len < 0)
353 {
354 *target_errno = inf_child_errno_to_fileio_error (errno);
355 return NULL;
356 }
357
358 ret = xmalloc (len + 1);
359 memcpy (ret, buf, len);
360 ret[len] = '\0';
361 return ret;
362 #else
363 *target_errno = FILEIO_ENOSYS;
364 return NULL;
365 #endif
366 }
367
368 static int
369 inf_child_use_agent (int use)
370 {
371 if (agent_loaded_p ())
372 {
373 use_agent = use;
374 return 1;
375 }
376 else
377 return 0;
378 }
379
380 static int
381 inf_child_can_use_agent (void)
382 {
383 return agent_loaded_p ();
384 }
385
386 struct target_ops *
387 inf_child_target (void)
388 {
389 struct target_ops *t = XZALLOC (struct target_ops);
390
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;
426 return t;
427 }
This page took 0.038074 seconds and 4 git commands to generate.