Rename native-only terminal related functions.
[deliverable/binutils-gdb.git] / gdb / inf-child.c
1 /* Base/prototype target for default child (native) targets.
2
3 Copyright (C) 1988-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* This file provides a common base class/target that all native
21 target implementations extend, by calling inf_child_target to get a
22 new prototype target and then overriding target methods as
23 necessary. */
24
25 #include "defs.h"
26 #include "regcache.h"
27 #include "memattr.h"
28 #include "symtab.h"
29 #include "target.h"
30 #include "inferior.h"
31 #include <string.h>
32 #include <sys/stat.h>
33 #include "inf-child.h"
34 #include "gdb/fileio.h"
35 #include "agent.h"
36 #include "gdb_wait.h"
37 #include "filestuff.h"
38
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42
43 /* Helper function for child_wait and the derivatives of child_wait.
44 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
45 translation of that in OURSTATUS. */
46 void
47 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
48 {
49 if (WIFEXITED (hoststatus))
50 {
51 ourstatus->kind = TARGET_WAITKIND_EXITED;
52 ourstatus->value.integer = WEXITSTATUS (hoststatus);
53 }
54 else if (!WIFSTOPPED (hoststatus))
55 {
56 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
57 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
58 }
59 else
60 {
61 ourstatus->kind = TARGET_WAITKIND_STOPPED;
62 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
63 }
64 }
65
66 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
67 for all registers. */
68
69 static void
70 inf_child_fetch_inferior_registers (struct target_ops *ops,
71 struct regcache *regcache, int regnum)
72 {
73 if (regnum == -1)
74 {
75 for (regnum = 0;
76 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
77 regnum++)
78 regcache_raw_supply (regcache, regnum, NULL);
79 }
80 else
81 regcache_raw_supply (regcache, regnum, NULL);
82 }
83
84 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
85 this for all registers (including the floating point registers). */
86
87 static void
88 inf_child_store_inferior_registers (struct target_ops *ops,
89 struct regcache *regcache, int regnum)
90 {
91 }
92
93 static void
94 inf_child_post_attach (struct target_ops *self, int pid)
95 {
96 /* This target doesn't require a meaningful "post attach" operation
97 by a debugger. */
98 }
99
100 /* Get ready to modify the registers array. On machines which store
101 individual registers, this doesn't need to do anything. On
102 machines which store all the registers in one fell swoop, this
103 makes sure that registers contains all the registers from the
104 program being debugged. */
105
106 static void
107 inf_child_prepare_to_store (struct target_ops *self,
108 struct regcache *regcache)
109 {
110 }
111
112 static void
113 inf_child_open (char *arg, int from_tty)
114 {
115 error (_("Use the \"run\" command to start a child process."));
116 }
117
118 static void
119 inf_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
120 {
121 /* This target doesn't require a meaningful "post startup inferior"
122 operation by a debugger. */
123 }
124
125 static int
126 inf_child_follow_fork (struct target_ops *ops, int follow_child,
127 int detach_fork)
128 {
129 /* This target doesn't support following fork or vfork events. */
130 return 0;
131 }
132
133 static int
134 inf_child_can_run (struct target_ops *self)
135 {
136 return 1;
137 }
138
139 static char *
140 inf_child_pid_to_exec_file (struct target_ops *self, int pid)
141 {
142 /* This target doesn't support translation of a process ID to the
143 filename of the executable file. */
144 return NULL;
145 }
146
147
148 /* Target file operations. */
149
150 static int
151 inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
152 {
153 int open_flags = 0;
154
155 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
156 return -1;
157
158 if (fileio_open_flags & FILEIO_O_CREAT)
159 open_flags |= O_CREAT;
160 if (fileio_open_flags & FILEIO_O_EXCL)
161 open_flags |= O_EXCL;
162 if (fileio_open_flags & FILEIO_O_TRUNC)
163 open_flags |= O_TRUNC;
164 if (fileio_open_flags & FILEIO_O_APPEND)
165 open_flags |= O_APPEND;
166 if (fileio_open_flags & FILEIO_O_RDONLY)
167 open_flags |= O_RDONLY;
168 if (fileio_open_flags & FILEIO_O_WRONLY)
169 open_flags |= O_WRONLY;
170 if (fileio_open_flags & FILEIO_O_RDWR)
171 open_flags |= O_RDWR;
172 /* On systems supporting binary and text mode, always open files in
173 binary mode. */
174 #ifdef O_BINARY
175 open_flags |= O_BINARY;
176 #endif
177
178 *open_flags_p = open_flags;
179 return 0;
180 }
181
182 static int
183 inf_child_errno_to_fileio_error (int errnum)
184 {
185 switch (errnum)
186 {
187 case EPERM:
188 return FILEIO_EPERM;
189 case ENOENT:
190 return FILEIO_ENOENT;
191 case EINTR:
192 return FILEIO_EINTR;
193 case EIO:
194 return FILEIO_EIO;
195 case EBADF:
196 return FILEIO_EBADF;
197 case EACCES:
198 return FILEIO_EACCES;
199 case EFAULT:
200 return FILEIO_EFAULT;
201 case EBUSY:
202 return FILEIO_EBUSY;
203 case EEXIST:
204 return FILEIO_EEXIST;
205 case ENODEV:
206 return FILEIO_ENODEV;
207 case ENOTDIR:
208 return FILEIO_ENOTDIR;
209 case EISDIR:
210 return FILEIO_EISDIR;
211 case EINVAL:
212 return FILEIO_EINVAL;
213 case ENFILE:
214 return FILEIO_ENFILE;
215 case EMFILE:
216 return FILEIO_EMFILE;
217 case EFBIG:
218 return FILEIO_EFBIG;
219 case ENOSPC:
220 return FILEIO_ENOSPC;
221 case ESPIPE:
222 return FILEIO_ESPIPE;
223 case EROFS:
224 return FILEIO_EROFS;
225 case ENOSYS:
226 return FILEIO_ENOSYS;
227 case ENAMETOOLONG:
228 return FILEIO_ENAMETOOLONG;
229 }
230 return FILEIO_EUNKNOWN;
231 }
232
233 /* Open FILENAME on the target, using FLAGS and MODE. Return a
234 target file descriptor, or -1 if an error occurs (and set
235 *TARGET_ERRNO). */
236 static int
237 inf_child_fileio_open (struct target_ops *self,
238 const char *filename, int flags, int mode,
239 int *target_errno)
240 {
241 int nat_flags;
242 int fd;
243
244 if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
245 {
246 *target_errno = FILEIO_EINVAL;
247 return -1;
248 }
249
250 /* We do not need to convert MODE, since the fileio protocol uses
251 the standard values. */
252 fd = gdb_open_cloexec (filename, nat_flags, mode);
253 if (fd == -1)
254 *target_errno = inf_child_errno_to_fileio_error (errno);
255
256 return fd;
257 }
258
259 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
260 Return the number of bytes written, or -1 if an error occurs
261 (and set *TARGET_ERRNO). */
262 static int
263 inf_child_fileio_pwrite (struct target_ops *self,
264 int fd, const gdb_byte *write_buf, int len,
265 ULONGEST offset, int *target_errno)
266 {
267 int ret;
268
269 #ifdef HAVE_PWRITE
270 ret = pwrite (fd, write_buf, len, (long) offset);
271 #else
272 ret = -1;
273 #endif
274 /* If we have no pwrite or it failed for this file, use lseek/write. */
275 if (ret == -1)
276 {
277 ret = lseek (fd, (long) offset, SEEK_SET);
278 if (ret != -1)
279 ret = write (fd, write_buf, len);
280 }
281
282 if (ret == -1)
283 *target_errno = inf_child_errno_to_fileio_error (errno);
284
285 return ret;
286 }
287
288 /* Read up to LEN bytes FD on the target into READ_BUF.
289 Return the number of bytes read, or -1 if an error occurs
290 (and set *TARGET_ERRNO). */
291 static int
292 inf_child_fileio_pread (struct target_ops *self,
293 int fd, gdb_byte *read_buf, int len,
294 ULONGEST offset, int *target_errno)
295 {
296 int ret;
297
298 #ifdef HAVE_PREAD
299 ret = pread (fd, read_buf, len, (long) offset);
300 #else
301 ret = -1;
302 #endif
303 /* If we have no pread or it failed for this file, use lseek/read. */
304 if (ret == -1)
305 {
306 ret = lseek (fd, (long) offset, SEEK_SET);
307 if (ret != -1)
308 ret = read (fd, read_buf, len);
309 }
310
311 if (ret == -1)
312 *target_errno = inf_child_errno_to_fileio_error (errno);
313
314 return ret;
315 }
316
317 /* Close FD on the target. Return 0, or -1 if an error occurs
318 (and set *TARGET_ERRNO). */
319 static int
320 inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno)
321 {
322 int ret;
323
324 ret = close (fd);
325 if (ret == -1)
326 *target_errno = inf_child_errno_to_fileio_error (errno);
327
328 return ret;
329 }
330
331 /* Unlink FILENAME on the target. Return 0, or -1 if an error
332 occurs (and set *TARGET_ERRNO). */
333 static int
334 inf_child_fileio_unlink (struct target_ops *self,
335 const char *filename, int *target_errno)
336 {
337 int ret;
338
339 ret = unlink (filename);
340 if (ret == -1)
341 *target_errno = inf_child_errno_to_fileio_error (errno);
342
343 return ret;
344 }
345
346 /* Read value of symbolic link FILENAME on the target. Return a
347 null-terminated string allocated via xmalloc, or NULL if an error
348 occurs (and set *TARGET_ERRNO). */
349 static char *
350 inf_child_fileio_readlink (struct target_ops *self,
351 const char *filename, int *target_errno)
352 {
353 /* We support readlink only on systems that also provide a compile-time
354 maximum path length (PATH_MAX), at least for now. */
355 #if defined (HAVE_READLINK) && defined (PATH_MAX)
356 char buf[PATH_MAX];
357 int len;
358 char *ret;
359
360 len = readlink (filename, buf, sizeof buf);
361 if (len < 0)
362 {
363 *target_errno = inf_child_errno_to_fileio_error (errno);
364 return NULL;
365 }
366
367 ret = xmalloc (len + 1);
368 memcpy (ret, buf, len);
369 ret[len] = '\0';
370 return ret;
371 #else
372 *target_errno = FILEIO_ENOSYS;
373 return NULL;
374 #endif
375 }
376
377 static int
378 inf_child_use_agent (struct target_ops *self, int use)
379 {
380 if (agent_loaded_p ())
381 {
382 use_agent = use;
383 return 1;
384 }
385 else
386 return 0;
387 }
388
389 static int
390 inf_child_can_use_agent (struct target_ops *self)
391 {
392 return agent_loaded_p ();
393 }
394
395 /* Default implementation of the to_can_async_p and
396 to_supports_non_stop methods. */
397
398 static int
399 return_zero (struct target_ops *ignore)
400 {
401 return 0;
402 }
403
404 struct target_ops *
405 inf_child_target (void)
406 {
407 struct target_ops *t = XCNEW (struct target_ops);
408
409 t->to_shortname = "child";
410 t->to_longname = "Child process";
411 t->to_doc = "Child process (started by the \"run\" command).";
412 t->to_open = inf_child_open;
413 t->to_post_attach = inf_child_post_attach;
414 t->to_fetch_registers = inf_child_fetch_inferior_registers;
415 t->to_store_registers = inf_child_store_inferior_registers;
416 t->to_prepare_to_store = inf_child_prepare_to_store;
417 t->to_insert_breakpoint = memory_insert_breakpoint;
418 t->to_remove_breakpoint = memory_remove_breakpoint;
419 t->to_terminal_init = child_terminal_init;
420 t->to_terminal_inferior = child_terminal_inferior;
421 t->to_terminal_ours_for_output = child_terminal_ours_for_output;
422 t->to_terminal_save_ours = child_terminal_save_ours;
423 t->to_terminal_ours = child_terminal_ours;
424 t->to_terminal_info = child_terminal_info;
425 t->to_post_startup_inferior = inf_child_post_startup_inferior;
426 t->to_follow_fork = inf_child_follow_fork;
427 t->to_can_run = inf_child_can_run;
428 /* We must default these because they must be implemented by any
429 target that can run. */
430 t->to_can_async_p = return_zero;
431 t->to_supports_non_stop = return_zero;
432 t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
433 t->to_stratum = process_stratum;
434 t->to_has_all_memory = default_child_has_all_memory;
435 t->to_has_memory = default_child_has_memory;
436 t->to_has_stack = default_child_has_stack;
437 t->to_has_registers = default_child_has_registers;
438 t->to_has_execution = default_child_has_execution;
439 t->to_fileio_open = inf_child_fileio_open;
440 t->to_fileio_pwrite = inf_child_fileio_pwrite;
441 t->to_fileio_pread = inf_child_fileio_pread;
442 t->to_fileio_close = inf_child_fileio_close;
443 t->to_fileio_unlink = inf_child_fileio_unlink;
444 t->to_fileio_readlink = inf_child_fileio_readlink;
445 t->to_magic = OPS_MAGIC;
446 t->to_use_agent = inf_child_use_agent;
447 t->to_can_use_agent = inf_child_can_use_agent;
448 return t;
449 }
This page took 0.040032 seconds and 5 git commands to generate.