(Ada) problem printing renaming which references a subprogram parameter
[binutils-gdb.git] / gdb / inf-child.c
blob69a149a144a8020cd2d40918a032a081ef843b35
1 /* Base/prototype target for default child (native) targets.
3 Copyright (C) 1988-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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. */
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 <sys/stat.h>
32 #include "inf-child.h"
33 #include "fileio.h"
34 #include "agent.h"
35 #include "gdb_wait.h"
36 #include "filestuff.h"
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <unistd.h>
42 /* A pointer to what is returned by inf_child_target. Used by
43 inf_child_open to push the most-derived target in reaction to
44 "target native". */
45 static struct target_ops *inf_child_ops = NULL;
47 /* Helper function for child_wait and the derivatives of child_wait.
48 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
49 translation of that in OURSTATUS. */
50 void
51 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
53 if (WIFEXITED (hoststatus))
55 ourstatus->kind = TARGET_WAITKIND_EXITED;
56 ourstatus->value.integer = WEXITSTATUS (hoststatus);
58 else if (!WIFSTOPPED (hoststatus))
60 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
61 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
63 else
65 ourstatus->kind = TARGET_WAITKIND_STOPPED;
66 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
70 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
71 for all registers. */
73 static void
74 inf_child_fetch_inferior_registers (struct target_ops *ops,
75 struct regcache *regcache, int regnum)
77 if (regnum == -1)
79 for (regnum = 0;
80 regnum < gdbarch_num_regs (regcache->arch ());
81 regnum++)
82 regcache_raw_supply (regcache, regnum, NULL);
84 else
85 regcache_raw_supply (regcache, regnum, NULL);
88 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
89 this for all registers (including the floating point registers). */
91 static void
92 inf_child_store_inferior_registers (struct target_ops *ops,
93 struct regcache *regcache, int regnum)
97 static void
98 inf_child_post_attach (struct target_ops *self, int pid)
100 /* This target doesn't require a meaningful "post attach" operation
101 by a debugger. */
104 /* Get ready to modify the registers array. On machines which store
105 individual registers, this doesn't need to do anything. On
106 machines which store all the registers in one fell swoop, this
107 makes sure that registers contains all the registers from the
108 program being debugged. */
110 static void
111 inf_child_prepare_to_store (struct target_ops *self,
112 struct regcache *regcache)
116 /* True if the user did "target native". In that case, we won't
117 unpush the child target automatically when the last inferior is
118 gone. */
119 static int inf_child_explicitly_opened;
121 /* See inf-child.h. */
123 void
124 inf_child_open_target (struct target_ops *target, const char *arg,
125 int from_tty)
127 target_preopen (from_tty);
128 push_target (target);
129 inf_child_explicitly_opened = 1;
130 if (from_tty)
131 printf_filtered ("Done. Use the \"run\" command to start a process.\n");
134 static void
135 inf_child_open (const char *arg, int from_tty)
137 inf_child_open_target (inf_child_ops, arg, from_tty);
140 /* Implement the to_disconnect target_ops method. */
142 static void
143 inf_child_disconnect (struct target_ops *target, const char *args, int from_tty)
145 if (args != NULL)
146 error (_("Argument given to \"disconnect\"."));
148 /* This offers to detach/kill current inferiors, and then pops all
149 targets. */
150 target_preopen (from_tty);
153 /* Implement the to_close target_ops method. */
155 static void
156 inf_child_close (struct target_ops *target)
158 /* In case we were forcibly closed. */
159 inf_child_explicitly_opened = 0;
162 void
163 inf_child_mourn_inferior (struct target_ops *ops)
165 generic_mourn_inferior ();
166 inf_child_maybe_unpush_target (ops);
169 /* See inf-child.h. */
171 void
172 inf_child_maybe_unpush_target (struct target_ops *ops)
174 if (!inf_child_explicitly_opened && !have_inferiors ())
175 unpush_target (ops);
178 static void
179 inf_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
181 /* This target doesn't require a meaningful "post startup inferior"
182 operation by a debugger. */
185 static int
186 inf_child_follow_fork (struct target_ops *ops, int follow_child,
187 int detach_fork)
189 /* This target doesn't support following fork or vfork events. */
190 return 0;
193 static int
194 inf_child_can_run (struct target_ops *self)
196 return 1;
199 static char *
200 inf_child_pid_to_exec_file (struct target_ops *self, int pid)
202 /* This target doesn't support translation of a process ID to the
203 filename of the executable file. */
204 return NULL;
207 /* Implementation of to_fileio_open. */
209 static int
210 inf_child_fileio_open (struct target_ops *self,
211 struct inferior *inf, const char *filename,
212 int flags, int mode, int warn_if_slow,
213 int *target_errno)
215 int nat_flags;
216 mode_t nat_mode;
217 int fd;
219 if (fileio_to_host_openflags (flags, &nat_flags) == -1
220 || fileio_to_host_mode (mode, &nat_mode) == -1)
222 *target_errno = FILEIO_EINVAL;
223 return -1;
226 fd = gdb_open_cloexec (filename, nat_flags, nat_mode);
227 if (fd == -1)
228 *target_errno = host_to_fileio_error (errno);
230 return fd;
233 /* Implementation of to_fileio_pwrite. */
235 static int
236 inf_child_fileio_pwrite (struct target_ops *self,
237 int fd, const gdb_byte *write_buf, int len,
238 ULONGEST offset, int *target_errno)
240 int ret;
242 #ifdef HAVE_PWRITE
243 ret = pwrite (fd, write_buf, len, (long) offset);
244 #else
245 ret = -1;
246 #endif
247 /* If we have no pwrite or it failed for this file, use lseek/write. */
248 if (ret == -1)
250 ret = lseek (fd, (long) offset, SEEK_SET);
251 if (ret != -1)
252 ret = write (fd, write_buf, len);
255 if (ret == -1)
256 *target_errno = host_to_fileio_error (errno);
258 return ret;
261 /* Implementation of to_fileio_pread. */
263 static int
264 inf_child_fileio_pread (struct target_ops *self,
265 int fd, gdb_byte *read_buf, int len,
266 ULONGEST offset, int *target_errno)
268 int ret;
270 #ifdef HAVE_PREAD
271 ret = pread (fd, read_buf, len, (long) offset);
272 #else
273 ret = -1;
274 #endif
275 /* If we have no pread or it failed for this file, use lseek/read. */
276 if (ret == -1)
278 ret = lseek (fd, (long) offset, SEEK_SET);
279 if (ret != -1)
280 ret = read (fd, read_buf, len);
283 if (ret == -1)
284 *target_errno = host_to_fileio_error (errno);
286 return ret;
289 /* Implementation of to_fileio_fstat. */
291 static int
292 inf_child_fileio_fstat (struct target_ops *self, int fd,
293 struct stat *sb, int *target_errno)
295 int ret;
297 ret = fstat (fd, sb);
298 if (ret == -1)
299 *target_errno = host_to_fileio_error (errno);
301 return ret;
304 /* Implementation of to_fileio_close. */
306 static int
307 inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno)
309 int ret;
311 ret = close (fd);
312 if (ret == -1)
313 *target_errno = host_to_fileio_error (errno);
315 return ret;
318 /* Implementation of to_fileio_unlink. */
320 static int
321 inf_child_fileio_unlink (struct target_ops *self,
322 struct inferior *inf, const char *filename,
323 int *target_errno)
325 int ret;
327 ret = unlink (filename);
328 if (ret == -1)
329 *target_errno = host_to_fileio_error (errno);
331 return ret;
334 /* Implementation of to_fileio_readlink. */
336 static char *
337 inf_child_fileio_readlink (struct target_ops *self,
338 struct inferior *inf, const char *filename,
339 int *target_errno)
341 /* We support readlink only on systems that also provide a compile-time
342 maximum path length (PATH_MAX), at least for now. */
343 #if defined (PATH_MAX)
344 char buf[PATH_MAX];
345 int len;
346 char *ret;
348 len = readlink (filename, buf, sizeof buf);
349 if (len < 0)
351 *target_errno = host_to_fileio_error (errno);
352 return NULL;
355 ret = (char *) xmalloc (len + 1);
356 memcpy (ret, buf, len);
357 ret[len] = '\0';
358 return ret;
359 #else
360 *target_errno = FILEIO_ENOSYS;
361 return NULL;
362 #endif
365 static int
366 inf_child_use_agent (struct target_ops *self, int use)
368 if (agent_loaded_p ())
370 use_agent = use;
371 return 1;
373 else
374 return 0;
377 static int
378 inf_child_can_use_agent (struct target_ops *self)
380 return agent_loaded_p ();
383 /* Default implementation of the to_can_async_p and
384 to_supports_non_stop methods. */
386 static int
387 return_zero (struct target_ops *ignore)
389 return 0;
392 struct target_ops *
393 inf_child_target (void)
395 struct target_ops *t = XCNEW (struct target_ops);
397 t->to_shortname = "native";
398 t->to_longname = "Native process";
399 t->to_doc = "Native process (started by the \"run\" command).";
400 t->to_open = inf_child_open;
401 t->to_close = inf_child_close;
402 t->to_disconnect = inf_child_disconnect;
403 t->to_post_attach = inf_child_post_attach;
404 t->to_fetch_registers = inf_child_fetch_inferior_registers;
405 t->to_store_registers = inf_child_store_inferior_registers;
406 t->to_prepare_to_store = inf_child_prepare_to_store;
407 t->to_insert_breakpoint = memory_insert_breakpoint;
408 t->to_remove_breakpoint = memory_remove_breakpoint;
409 t->to_terminal_init = child_terminal_init;
410 t->to_terminal_inferior = child_terminal_inferior;
411 t->to_terminal_ours_for_output = child_terminal_ours_for_output;
412 t->to_terminal_ours = child_terminal_ours;
413 t->to_terminal_info = child_terminal_info;
414 t->to_post_startup_inferior = inf_child_post_startup_inferior;
415 t->to_follow_fork = inf_child_follow_fork;
416 t->to_can_run = inf_child_can_run;
417 /* We must default these because they must be implemented by any
418 target that can run. */
419 t->to_can_async_p = return_zero;
420 t->to_supports_non_stop = return_zero;
421 t->to_pid_to_exec_file = inf_child_pid_to_exec_file;
422 t->to_stratum = process_stratum;
423 t->to_has_all_memory = default_child_has_all_memory;
424 t->to_has_memory = default_child_has_memory;
425 t->to_has_stack = default_child_has_stack;
426 t->to_has_registers = default_child_has_registers;
427 t->to_has_execution = default_child_has_execution;
428 t->to_fileio_open = inf_child_fileio_open;
429 t->to_fileio_pwrite = inf_child_fileio_pwrite;
430 t->to_fileio_pread = inf_child_fileio_pread;
431 t->to_fileio_fstat = inf_child_fileio_fstat;
432 t->to_fileio_close = inf_child_fileio_close;
433 t->to_fileio_unlink = inf_child_fileio_unlink;
434 t->to_fileio_readlink = inf_child_fileio_readlink;
435 t->to_magic = OPS_MAGIC;
436 t->to_use_agent = inf_child_use_agent;
437 t->to_can_use_agent = inf_child_can_use_agent;
439 /* Store a pointer so we can push the most-derived target from
440 inf_child_open. */
441 inf_child_ops = t;
443 return t;