GDB (xrefs)
/tmp/gdb-7.10/gdb/inf-child.c
Go to the documentation of this file.
1 /* Base/prototype target for default child (native) targets.
2 
3  Copyright (C) 1988-2015 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 <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"
37 
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 
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;
46 
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)
52 {
53  if (WIFEXITED (hoststatus))
54  {
55  ourstatus->kind = TARGET_WAITKIND_EXITED;
56  ourstatus->value.integer = WEXITSTATUS (hoststatus);
57  }
58  else if (!WIFSTOPPED (hoststatus))
59  {
60  ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
61  ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
62  }
63  else
64  {
65  ourstatus->kind = TARGET_WAITKIND_STOPPED;
66  ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
67  }
68 }
69 
70 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
71  for all registers. */
72 
73 static void
75  struct regcache *regcache, int regnum)
76 {
77  if (regnum == -1)
78  {
79  for (regnum = 0;
80  regnum < gdbarch_num_regs (get_regcache_arch (regcache));
81  regnum++)
82  regcache_raw_supply (regcache, regnum, NULL);
83  }
84  else
85  regcache_raw_supply (regcache, regnum, NULL);
86 }
87 
88 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
89  this for all registers (including the floating point registers). */
90 
91 static void
93  struct regcache *regcache, int regnum)
94 {
95 }
96 
97 static void
99 {
100  /* This target doesn't require a meaningful "post attach" operation
101  by a debugger. */
102 }
103 
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. */
109 
110 static void
112  struct regcache *regcache)
113 {
114 }
115 
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. */
120 
121 /* See inf-child.h. */
122 
123 void
124 inf_child_open_target (struct target_ops *target, const char *arg,
125  int from_tty)
126 {
127  target_preopen (from_tty);
128  push_target (target);
130  if (from_tty)
131  printf_filtered ("Done. Use the \"run\" command to start a process.\n");
132 }
133 
134 static void
135 inf_child_open (const char *arg, int from_tty)
136 {
137  inf_child_open_target (inf_child_ops, arg, from_tty);
138 }
139 
140 /* Implement the to_disconnect target_ops method. */
141 
142 static void
143 inf_child_disconnect (struct target_ops *target, const char *args, int from_tty)
144 {
145  if (args != NULL)
146  error (_("Argument given to \"disconnect\"."));
147 
148  /* This offers to detach/kill current inferiors, and then pops all
149  targets. */
150  target_preopen (from_tty);
151 }
152 
153 /* Implement the to_close target_ops method. */
154 
155 static void
156 inf_child_close (struct target_ops *target)
157 {
158  /* In case we were forcibly closed. */
160 }
161 
162 void
164 {
167 }
168 
169 /* See inf-child.h. */
170 
171 void
173 {
175  unpush_target (ops);
176 }
177 
178 static void
180 {
181  /* This target doesn't require a meaningful "post startup inferior"
182  operation by a debugger. */
183 }
184 
185 static int
186 inf_child_follow_fork (struct target_ops *ops, int follow_child,
187  int detach_fork)
188 {
189  /* This target doesn't support following fork or vfork events. */
190  return 0;
191 }
192 
193 static int
195 {
196  return 1;
197 }
198 
199 static char *
201 {
202  /* This target doesn't support translation of a process ID to the
203  filename of the executable file. */
204  return NULL;
205 }
206 
207 /* Implementation of to_fileio_open. */
208 
209 static int
211  struct inferior *inf, const char *filename,
212  int flags, int mode, int warn_if_slow,
213  int *target_errno)
214 {
215  int nat_flags;
216  mode_t nat_mode;
217  int fd;
218 
219  if (fileio_to_host_openflags (flags, &nat_flags) == -1
220  || fileio_to_host_mode (mode, &nat_mode) == -1)
221  {
222  *target_errno = FILEIO_EINVAL;
223  return -1;
224  }
225 
226  fd = gdb_open_cloexec (filename, nat_flags, nat_mode);
227  if (fd == -1)
228  *target_errno = host_to_fileio_error (errno);
229 
230  return fd;
231 }
232 
233 /* Implementation of to_fileio_pwrite. */
234 
235 static int
237  int fd, const gdb_byte *write_buf, int len,
238  ULONGEST offset, int *target_errno)
239 {
240  int ret;
241 
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)
249  {
250  ret = lseek (fd, (long) offset, SEEK_SET);
251  if (ret != -1)
252  ret = write (fd, write_buf, len);
253  }
254 
255  if (ret == -1)
256  *target_errno = host_to_fileio_error (errno);
257 
258  return ret;
259 }
260 
261 /* Implementation of to_fileio_pread. */
262 
263 static int
265  int fd, gdb_byte *read_buf, int len,
266  ULONGEST offset, int *target_errno)
267 {
268  int ret;
269 
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)
277  {
278  ret = lseek (fd, (long) offset, SEEK_SET);
279  if (ret != -1)
280  ret = read (fd, read_buf, len);
281  }
282 
283  if (ret == -1)
284  *target_errno = host_to_fileio_error (errno);
285 
286  return ret;
287 }
288 
289 /* Implementation of to_fileio_fstat. */
290 
291 static int
292 inf_child_fileio_fstat (struct target_ops *self, int fd,
293  struct stat *sb, int *target_errno)
294 {
295  int ret;
296 
297  ret = fstat (fd, sb);
298  if (ret == -1)
299  *target_errno = host_to_fileio_error (errno);
300 
301  return ret;
302 }
303 
304 /* Implementation of to_fileio_close. */
305 
306 static int
307 inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno)
308 {
309  int ret;
310 
311  ret = close (fd);
312  if (ret == -1)
313  *target_errno = host_to_fileio_error (errno);
314 
315  return ret;
316 }
317 
318 /* Implementation of to_fileio_unlink. */
319 
320 static int
322  struct inferior *inf, const char *filename,
323  int *target_errno)
324 {
325  int ret;
326 
327  ret = unlink (filename);
328  if (ret == -1)
329  *target_errno = host_to_fileio_error (errno);
330 
331  return ret;
332 }
333 
334 /* Implementation of to_fileio_readlink. */
335 
336 static char *
338  struct inferior *inf, const char *filename,
339  int *target_errno)
340 {
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;
347 
348  len = readlink (filename, buf, sizeof buf);
349  if (len < 0)
350  {
351  *target_errno = host_to_fileio_error (errno);
352  return NULL;
353  }
354 
355  ret = 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
363 }
364 
365 static int
366 inf_child_use_agent (struct target_ops *self, int use)
367 {
368  if (agent_loaded_p ())
369  {
370  use_agent = use;
371  return 1;
372  }
373  else
374  return 0;
375 }
376 
377 static int
379 {
380  return agent_loaded_p ();
381 }
382 
383 /* Default implementation of the to_can_async_p and
384  to_supports_non_stop methods. */
385 
386 static int
388 {
389  return 0;
390 }
391 
392 struct target_ops *
394 {
395  struct target_ops *t = XCNEW (struct target_ops);
396 
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;
417  /* We must default these because they must be implemented by any
418  target that can run. */
435  t->to_magic = OPS_MAGIC;
438 
439  /* Store a pointer so we can push the most-derived target from
440  inf_child_open. */
441  inf_child_ops = t;
442 
443  return t;
444 }
int(* to_fileio_pread)(struct target_ops *, int fd, gdb_byte *read_buf, int len, ULONGEST offset, int *target_errno)
Definition: target.h:887
static int inf_child_explicitly_opened
Definition: inf-child.c:119
int default_child_has_stack(struct target_ops *ops)
Definition: target.c:216
ssize_t read(int fd, void *buf, size_t count)
Definition: expect-read1.c:26
const char * to_longname
Definition: target.h:433
int have_inferiors(void)
Definition: inferior.c:460
void child_terminal_info(struct target_ops *self, const char *, int)
Definition: inflow.c:584
int(* to_fileio_close)(struct target_ops *, int fd, int *target_errno)
Definition: target.h:899
static void inf_child_post_startup_inferior(struct target_ops *self, ptid_t ptid)
Definition: inf-child.c:179
struct gdbarch * get_regcache_arch(const struct regcache *regcache)
Definition: regcache.c:297
int(* to_has_memory)(struct target_ops *)
Definition: target.h:652
int fileio_to_host_mode(int fileio_mode, mode_t *mode_p)
Definition: fileio.c:115
int(* to_has_stack)(struct target_ops *)
Definition: target.h:653
void(* to_terminal_init)(struct target_ops *) TARGET_DEFAULT_IGNORE()
Definition: target.h:565
void push_target(struct target_ops *t)
Definition: target.c:664
int(* to_insert_breakpoint)(struct target_ops *, struct gdbarch *, struct bp_target_info *) TARGET_DEFAULT_FUNC(memory_insert_breakpoint)
Definition: target.h:481
static int inf_child_fileio_pwrite(struct target_ops *self, int fd, const gdb_byte *write_buf, int len, ULONGEST offset, int *target_errno)
Definition: inf-child.c:236
int unpush_target(struct target_ops *t)
Definition: target.c:711
int(* to_follow_fork)(struct target_ops *, int, int) TARGET_DEFAULT_FUNC(default_follow_fork)
Definition: target.h:596
void(* to_terminal_ours_for_output)(struct target_ops *) TARGET_DEFAULT_IGNORE()
Definition: target.h:569
void generic_mourn_inferior(void)
Definition: target.c:3171
void(* to_close)(struct target_ops *)
Definition: target.h:448
void child_terminal_inferior(struct target_ops *self)
Definition: inflow.c:287
void child_terminal_ours_for_output(struct target_ops *self)
Definition: inflow.c:371
static int inf_child_fileio_unlink(struct target_ops *self, struct inferior *inf, const char *filename, int *target_errno)
Definition: inf-child.c:321
int(* to_remove_breakpoint)(struct target_ops *, struct gdbarch *, struct bp_target_info *) TARGET_DEFAULT_FUNC(memory_remove_breakpoint)
Definition: target.h:484
static int inf_child_follow_fork(struct target_ops *ops, int follow_child, int detach_fork)
Definition: inf-child.c:186
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:1898
#define _(String)
Definition: gdb_locale.h:40
int default_child_has_execution(struct target_ops *ops, ptid_t the_ptid)
Definition: target.c:236
void inf_child_maybe_unpush_target(struct target_ops *ops)
Definition: inf-child.c:172
void child_terminal_ours(struct target_ops *self)
Definition: inflow.c:385
static int inf_child_use_agent(struct target_ops *self, int use)
Definition: inf-child.c:366
void printf_filtered(const char *format,...)
Definition: utils.c:2388
Definition: ptid.h:35
int fileio_to_host_openflags(int fileio_open_flags, int *open_flags_p)
Definition: fileio.c:81
mach_port_t kern_return_t mach_port_t msgports mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition: gnu-nat.c:1885
int default_child_has_memory(struct target_ops *ops)
Definition: target.c:206
#define WSTOPSIG
Definition: gdb_wait.h:75
#define OPS_MAGIC
Definition: target.h:1233
static int inf_child_fileio_fstat(struct target_ops *self, int fd, struct stat *sb, int *target_errno)
Definition: inf-child.c:292
#define WTERMSIG(w)
Definition: gdb_wait.h:71
static void inf_child_store_inferior_registers(struct target_ops *ops, struct regcache *regcache, int regnum)
Definition: inf-child.c:92
static void inf_child_fetch_inferior_registers(struct target_ops *ops, struct regcache *regcache, int regnum)
Definition: inf-child.c:74
enum gdb_signal sig
Definition: waitstatus.h:108
#define WIFEXITED(w)
Definition: gdb_wait.h:44
static void inf_child_open(const char *arg, int from_tty)
Definition: inf-child.c:135
int(* to_can_run)(struct target_ops *) TARGET_DEFAULT_RETURN(0)
Definition: target.h:612
int host_to_fileio_error(int error)
Definition: fileio.c:28
int default_child_has_all_memory(struct target_ops *ops)
Definition: target.c:196
void store_waitstatus(struct target_waitstatus *ourstatus, int hoststatus)
Definition: inf-child.c:51
union target_waitstatus::@161 value
static int inf_child_fileio_open(struct target_ops *self, struct inferior *inf, const char *filename, int flags, int mode, int warn_if_slow, int *target_errno)
Definition: inf-child.c:210
int use_agent
Definition: agent.c:46
int(* to_has_execution)(struct target_ops *, ptid_t)
Definition: target.h:655
void inf_child_open_target(struct target_ops *target, const char *arg, int from_tty)
Definition: inf-child.c:124
enum gdb_signal gdb_signal_from_host(int)
Definition: signals.c:116
int(* to_fileio_fstat)(struct target_ops *, int fd, struct stat *sb, int *target_errno)
Definition: target.h:894
Definition: gnu-nat.c:163
static void inf_child_post_attach(struct target_ops *self, int pid)
Definition: inf-child.c:98
const char * to_doc
Definition: target.h:434
static void inf_child_disconnect(struct target_ops *target, const char *args, int from_tty)
Definition: inf-child.c:143
int regnum
Definition: aarch64-tdep.c:69
#define WEXITSTATUS(w)
Definition: gdb_wait.h:67
void inf_child_mourn_inferior(struct target_ops *ops)
Definition: inf-child.c:163
void * xmalloc(YYSIZE_T)
char *(* to_pid_to_exec_file)(struct target_ops *, int pid) TARGET_DEFAULT_RETURN(NULL)
Definition: target.h:644
static int inf_child_fileio_pread(struct target_ops *self, int fd, gdb_byte *read_buf, int len, ULONGEST offset, int *target_errno)
Definition: inf-child.c:264
static int return_zero(struct target_ops *ignore)
Definition: inf-child.c:387
void(* to_disconnect)(struct target_ops *, const char *, int) TARGET_DEFAULT_NORETURN(tcomplain())
Definition: target.h:462
#define WIFSTOPPED(w)
Definition: gdb_wait.h:62
int(* to_fileio_open)(struct target_ops *, struct inferior *inf, const char *filename, int flags, int mode, int warn_if_slow, int *target_errno)
Definition: target.h:872
bfd_byte gdb_byte
Definition: common-types.h:38
static void inf_child_close(struct target_ops *target)
Definition: inf-child.c:156
struct target_ops * inf_child_target(void)
Definition: inf-child.c:393
static int inf_child_fileio_close(struct target_ops *self, int fd, int *target_errno)
Definition: inf-child.c:307
void(* to_fetch_registers)(struct target_ops *, struct regcache *, int) TARGET_DEFAULT_IGNORE()
Definition: target.h:472
int default_child_has_registers(struct target_ops *ops)
Definition: target.c:226
#define SEEK_SET
Definition: defs.h:87
int(* to_fileio_pwrite)(struct target_ops *, int fd, const gdb_byte *write_buf, int len, ULONGEST offset, int *target_errno)
Definition: target.h:880
enum target_waitkind kind
Definition: waitstatus.h:100
static char * inf_child_pid_to_exec_file(struct target_ops *self, int pid)
Definition: inf-child.c:200
enum strata to_stratum
Definition: target.h:650
void(* to_open)(const char *, int)
Definition: target.h:443
int offset
Definition: agent.c:65
void(* to_post_attach)(struct target_ops *, int) TARGET_DEFAULT_IGNORE()
Definition: target.h:458
int(* to_can_async_p)(struct target_ops *) TARGET_DEFAULT_RETURN(0)
Definition: target.h:660
static int inf_child_can_run(struct target_ops *self)
Definition: inf-child.c:194
void(* to_terminal_ours)(struct target_ops *) TARGET_DEFAULT_IGNORE()
Definition: target.h:571
int gdb_open_cloexec(const char *filename, int flags, unsigned long mode)
Definition: filestuff.c:291
int agent_loaded_p(void)
Definition: agent.c:78
void regcache_raw_supply(struct regcache *regcache, int regnum, const void *buf)
Definition: regcache.c:1041
static void inf_child_prepare_to_store(struct target_ops *self, struct regcache *regcache)
Definition: inf-child.c:111
void(* to_terminal_inferior)(struct target_ops *) TARGET_DEFAULT_IGNORE()
Definition: target.h:567
const char * to_shortname
Definition: target.h:432
unsigned long long ULONGEST
Definition: common-types.h:53
static int ignore(struct target_ops *ops, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: corelow.c:917
int to_magic
Definition: target.h:1224
static char * inf_child_fileio_readlink(struct target_ops *self, struct inferior *inf, const char *filename, int *target_errno)
Definition: inf-child.c:337
int(* to_supports_non_stop)(struct target_ops *) TARGET_DEFAULT_RETURN(0)
Definition: target.h:668
int memory_insert_breakpoint(struct target_ops *ops, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: mem-break.c:88
int(* to_has_all_memory)(struct target_ops *)
Definition: target.h:651
int memory_remove_breakpoint(struct target_ops *ops, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: mem-break.c:95
int(* to_fileio_unlink)(struct target_ops *, struct inferior *inf, const char *filename, int *target_errno)
Definition: target.h:905
void child_terminal_init(struct target_ops *self)
Definition: inflow.c:266
void target_preopen(int from_tty)
Definition: target.c:2171
int(* to_use_agent)(struct target_ops *, int use) TARGET_DEFAULT_NORETURN(tcomplain())
Definition: target.h:1083
static int detach_fork
Definition: infrun.c:130
void(* to_prepare_to_store)(struct target_ops *, struct regcache *) TARGET_DEFAULT_NORETURN(noprocess())
Definition: target.h:476
void(* to_store_registers)(struct target_ops *, struct regcache *, int) TARGET_DEFAULT_NORETURN(noprocess())
Definition: target.h:474
int(* to_has_registers)(struct target_ops *)
Definition: target.h:654
static int inf_child_can_use_agent(struct target_ops *self)
Definition: inf-child.c:378
void error(const char *fmt,...)
Definition: errors.c:38
char *(* to_fileio_readlink)(struct target_ops *, struct inferior *inf, const char *filename, int *target_errno)
Definition: target.h:915
mach_port_t mach_port_t name mach_port_t mach_port_t name error_t int int rusage_t pid_t pid
Definition: gnu-nat.c:1818
void(* to_terminal_info)(struct target_ops *, const char *, int) TARGET_DEFAULT_FUNC(default_terminal_info)
Definition: target.h:573
void(* to_post_startup_inferior)(struct target_ops *, ptid_t) TARGET_DEFAULT_IGNORE()
Definition: target.h:586
int(* to_can_use_agent)(struct target_ops *) TARGET_DEFAULT_RETURN(0)
Definition: target.h:1087
const ULONGEST const LONGEST len
Definition: target.h:309