Commit | Line | Data |
---|---|---|
f1aea813 | 1 | /* Base/prototype target for default child (native) targets. |
5bf970f9 | 2 | |
ecd75fc8 | 3 | Copyright (C) 1988-2014 Free Software Foundation, Inc. |
5bf970f9 AC |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5bf970f9 AC |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
5bf970f9 | 19 | |
f1aea813 PA |
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 | ||
5bf970f9 AC |
25 | #include "defs.h" |
26 | #include "regcache.h" | |
27 | #include "memattr.h" | |
28 | #include "symtab.h" | |
29 | #include "target.h" | |
30 | #include "inferior.h" | |
0e9f083f | 31 | #include <string.h> |
53ce3c39 | 32 | #include <sys/stat.h> |
2c0b251b | 33 | #include "inf-child.h" |
7313baad | 34 | #include "gdb/fileio.h" |
5808517f | 35 | #include "agent.h" |
dab06dbe | 36 | #include "gdb_wait.h" |
614c279d | 37 | #include "filestuff.h" |
7313baad UW |
38 | |
39 | #include <sys/types.h> | |
7313baad UW |
40 | #include <fcntl.h> |
41 | #include <unistd.h> | |
5bf970f9 | 42 | |
6a3cb8e8 PA |
43 | /* A pointer to what is returned by inf_child_target. Used by |
44 | inf_child_open to push the most-derived target in reaction to | |
45 | "target native". */ | |
46 | static struct target_ops *inf_child_ops = NULL; | |
47 | ||
dab06dbe PA |
48 | /* Helper function for child_wait and the derivatives of child_wait. |
49 | HOSTSTATUS is the waitstatus from wait() or the equivalent; store our | |
50 | translation of that in OURSTATUS. */ | |
51 | void | |
52 | store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus) | |
53 | { | |
54 | if (WIFEXITED (hoststatus)) | |
55 | { | |
56 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
57 | ourstatus->value.integer = WEXITSTATUS (hoststatus); | |
58 | } | |
59 | else if (!WIFSTOPPED (hoststatus)) | |
60 | { | |
61 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
2ea28649 | 62 | ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus)); |
dab06dbe PA |
63 | } |
64 | else | |
65 | { | |
66 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
2ea28649 | 67 | ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus)); |
dab06dbe PA |
68 | } |
69 | } | |
70 | ||
5bf970f9 AC |
71 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this |
72 | for all registers. */ | |
73 | ||
74 | static void | |
28439f5e PA |
75 | inf_child_fetch_inferior_registers (struct target_ops *ops, |
76 | struct regcache *regcache, int regnum) | |
5bf970f9 AC |
77 | { |
78 | if (regnum == -1) | |
79 | { | |
b1a653ae UW |
80 | for (regnum = 0; |
81 | regnum < gdbarch_num_regs (get_regcache_arch (regcache)); | |
82 | regnum++) | |
56be3814 | 83 | regcache_raw_supply (regcache, regnum, NULL); |
5bf970f9 AC |
84 | } |
85 | else | |
56be3814 | 86 | regcache_raw_supply (regcache, regnum, NULL); |
5bf970f9 AC |
87 | } |
88 | ||
89 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do | |
90 | this for all registers (including the floating point registers). */ | |
91 | ||
92 | static void | |
28439f5e PA |
93 | inf_child_store_inferior_registers (struct target_ops *ops, |
94 | struct regcache *regcache, int regnum) | |
5bf970f9 AC |
95 | { |
96 | } | |
97 | ||
5bf970f9 | 98 | static void |
f045800c | 99 | inf_child_post_attach (struct target_ops *self, int pid) |
5bf970f9 | 100 | { |
f1aea813 PA |
101 | /* This target doesn't require a meaningful "post attach" operation |
102 | by a debugger. */ | |
5bf970f9 AC |
103 | } |
104 | ||
105 | /* Get ready to modify the registers array. On machines which store | |
106 | individual registers, this doesn't need to do anything. On | |
107 | machines which store all the registers in one fell swoop, this | |
108 | makes sure that registers contains all the registers from the | |
109 | program being debugged. */ | |
110 | ||
111 | static void | |
f32dbf8c MM |
112 | inf_child_prepare_to_store (struct target_ops *self, |
113 | struct regcache *regcache) | |
5bf970f9 AC |
114 | { |
115 | } | |
116 | ||
6a3cb8e8 PA |
117 | /* True if the user did "target native". In that case, we won't |
118 | unpush the child target automatically when the last inferior is | |
119 | gone. */ | |
120 | static int inf_child_explicitly_opened; | |
121 | ||
122 | /* See inf-child.h. */ | |
123 | ||
124 | void | |
125 | inf_child_open_target (struct target_ops *target, char *arg, int from_tty) | |
126 | { | |
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"); | |
132 | } | |
133 | ||
5bf970f9 AC |
134 | static void |
135 | inf_child_open (char *arg, int from_tty) | |
136 | { | |
6a3cb8e8 PA |
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, 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. */ | |
159 | inf_child_explicitly_opened = 0; | |
160 | } | |
161 | ||
162 | /* See inf-child.h. */ | |
163 | ||
164 | void | |
165 | inf_child_maybe_unpush_target (struct target_ops *ops) | |
166 | { | |
167 | if (!inf_child_explicitly_opened && !have_inferiors ()) | |
168 | unpush_target (ops); | |
5bf970f9 AC |
169 | } |
170 | ||
171 | static void | |
2e97a79e | 172 | inf_child_post_startup_inferior (struct target_ops *self, ptid_t ptid) |
5bf970f9 | 173 | { |
f1aea813 PA |
174 | /* This target doesn't require a meaningful "post startup inferior" |
175 | operation by a debugger. */ | |
5bf970f9 AC |
176 | } |
177 | ||
5bf970f9 | 178 | static int |
07107ca6 LM |
179 | inf_child_follow_fork (struct target_ops *ops, int follow_child, |
180 | int detach_fork) | |
5bf970f9 | 181 | { |
f1aea813 | 182 | /* This target doesn't support following fork or vfork events. */ |
5bf970f9 AC |
183 | return 0; |
184 | } | |
185 | ||
5bf970f9 | 186 | static int |
da82bd6b | 187 | inf_child_can_run (struct target_ops *self) |
5bf970f9 AC |
188 | { |
189 | return 1; | |
190 | } | |
191 | ||
5bf970f9 | 192 | static char * |
8dd27370 | 193 | inf_child_pid_to_exec_file (struct target_ops *self, int pid) |
5bf970f9 | 194 | { |
f1aea813 PA |
195 | /* This target doesn't support translation of a process ID to the |
196 | filename of the executable file. */ | |
5bf970f9 AC |
197 | return NULL; |
198 | } | |
199 | ||
7313baad UW |
200 | |
201 | /* Target file operations. */ | |
202 | ||
203 | static int | |
204 | inf_child_fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p) | |
205 | { | |
206 | int open_flags = 0; | |
207 | ||
208 | if (fileio_open_flags & ~FILEIO_O_SUPPORTED) | |
209 | return -1; | |
210 | ||
211 | if (fileio_open_flags & FILEIO_O_CREAT) | |
212 | open_flags |= O_CREAT; | |
213 | if (fileio_open_flags & FILEIO_O_EXCL) | |
214 | open_flags |= O_EXCL; | |
215 | if (fileio_open_flags & FILEIO_O_TRUNC) | |
216 | open_flags |= O_TRUNC; | |
217 | if (fileio_open_flags & FILEIO_O_APPEND) | |
218 | open_flags |= O_APPEND; | |
219 | if (fileio_open_flags & FILEIO_O_RDONLY) | |
220 | open_flags |= O_RDONLY; | |
221 | if (fileio_open_flags & FILEIO_O_WRONLY) | |
222 | open_flags |= O_WRONLY; | |
223 | if (fileio_open_flags & FILEIO_O_RDWR) | |
224 | open_flags |= O_RDWR; | |
225 | /* On systems supporting binary and text mode, always open files in | |
226 | binary mode. */ | |
227 | #ifdef O_BINARY | |
228 | open_flags |= O_BINARY; | |
229 | #endif | |
230 | ||
231 | *open_flags_p = open_flags; | |
232 | return 0; | |
233 | } | |
234 | ||
235 | static int | |
236 | inf_child_errno_to_fileio_error (int errnum) | |
237 | { | |
238 | switch (errnum) | |
239 | { | |
240 | case EPERM: | |
241 | return FILEIO_EPERM; | |
242 | case ENOENT: | |
243 | return FILEIO_ENOENT; | |
244 | case EINTR: | |
245 | return FILEIO_EINTR; | |
246 | case EIO: | |
247 | return FILEIO_EIO; | |
248 | case EBADF: | |
249 | return FILEIO_EBADF; | |
250 | case EACCES: | |
251 | return FILEIO_EACCES; | |
252 | case EFAULT: | |
253 | return FILEIO_EFAULT; | |
254 | case EBUSY: | |
255 | return FILEIO_EBUSY; | |
256 | case EEXIST: | |
257 | return FILEIO_EEXIST; | |
258 | case ENODEV: | |
259 | return FILEIO_ENODEV; | |
260 | case ENOTDIR: | |
261 | return FILEIO_ENOTDIR; | |
262 | case EISDIR: | |
263 | return FILEIO_EISDIR; | |
264 | case EINVAL: | |
265 | return FILEIO_EINVAL; | |
266 | case ENFILE: | |
267 | return FILEIO_ENFILE; | |
268 | case EMFILE: | |
269 | return FILEIO_EMFILE; | |
270 | case EFBIG: | |
271 | return FILEIO_EFBIG; | |
272 | case ENOSPC: | |
273 | return FILEIO_ENOSPC; | |
274 | case ESPIPE: | |
275 | return FILEIO_ESPIPE; | |
276 | case EROFS: | |
277 | return FILEIO_EROFS; | |
278 | case ENOSYS: | |
279 | return FILEIO_ENOSYS; | |
280 | case ENAMETOOLONG: | |
281 | return FILEIO_ENAMETOOLONG; | |
282 | } | |
283 | return FILEIO_EUNKNOWN; | |
284 | } | |
285 | ||
286 | /* Open FILENAME on the target, using FLAGS and MODE. Return a | |
287 | target file descriptor, or -1 if an error occurs (and set | |
288 | *TARGET_ERRNO). */ | |
289 | static int | |
cd897586 TT |
290 | inf_child_fileio_open (struct target_ops *self, |
291 | const char *filename, int flags, int mode, | |
7313baad UW |
292 | int *target_errno) |
293 | { | |
294 | int nat_flags; | |
295 | int fd; | |
296 | ||
297 | if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1) | |
298 | { | |
299 | *target_errno = FILEIO_EINVAL; | |
300 | return -1; | |
301 | } | |
302 | ||
303 | /* We do not need to convert MODE, since the fileio protocol uses | |
304 | the standard values. */ | |
614c279d | 305 | fd = gdb_open_cloexec (filename, nat_flags, mode); |
7313baad UW |
306 | if (fd == -1) |
307 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
308 | ||
309 | return fd; | |
310 | } | |
311 | ||
312 | /* Write up to LEN bytes from WRITE_BUF to FD on the target. | |
313 | Return the number of bytes written, or -1 if an error occurs | |
314 | (and set *TARGET_ERRNO). */ | |
315 | static int | |
0d866f62 TT |
316 | inf_child_fileio_pwrite (struct target_ops *self, |
317 | int fd, const gdb_byte *write_buf, int len, | |
7313baad UW |
318 | ULONGEST offset, int *target_errno) |
319 | { | |
320 | int ret; | |
321 | ||
322 | #ifdef HAVE_PWRITE | |
323 | ret = pwrite (fd, write_buf, len, (long) offset); | |
324 | #else | |
7c3270ae | 325 | ret = -1; |
7313baad | 326 | #endif |
7c3270ae UW |
327 | /* If we have no pwrite or it failed for this file, use lseek/write. */ |
328 | if (ret == -1) | |
329 | { | |
330 | ret = lseek (fd, (long) offset, SEEK_SET); | |
331 | if (ret != -1) | |
332 | ret = write (fd, write_buf, len); | |
333 | } | |
7313baad UW |
334 | |
335 | if (ret == -1) | |
336 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
337 | ||
338 | return ret; | |
339 | } | |
340 | ||
341 | /* Read up to LEN bytes FD on the target into READ_BUF. | |
342 | Return the number of bytes read, or -1 if an error occurs | |
343 | (and set *TARGET_ERRNO). */ | |
344 | static int | |
a3be983c TT |
345 | inf_child_fileio_pread (struct target_ops *self, |
346 | int fd, gdb_byte *read_buf, int len, | |
7313baad UW |
347 | ULONGEST offset, int *target_errno) |
348 | { | |
349 | int ret; | |
350 | ||
351 | #ifdef HAVE_PREAD | |
352 | ret = pread (fd, read_buf, len, (long) offset); | |
353 | #else | |
7c3270ae | 354 | ret = -1; |
7313baad | 355 | #endif |
7c3270ae UW |
356 | /* If we have no pread or it failed for this file, use lseek/read. */ |
357 | if (ret == -1) | |
358 | { | |
359 | ret = lseek (fd, (long) offset, SEEK_SET); | |
360 | if (ret != -1) | |
361 | ret = read (fd, read_buf, len); | |
362 | } | |
7313baad UW |
363 | |
364 | if (ret == -1) | |
365 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
366 | ||
367 | return ret; | |
368 | } | |
369 | ||
370 | /* Close FD on the target. Return 0, or -1 if an error occurs | |
371 | (and set *TARGET_ERRNO). */ | |
372 | static int | |
df39ea25 | 373 | inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno) |
7313baad UW |
374 | { |
375 | int ret; | |
376 | ||
377 | ret = close (fd); | |
378 | if (ret == -1) | |
379 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
380 | ||
381 | return ret; | |
382 | } | |
383 | ||
384 | /* Unlink FILENAME on the target. Return 0, or -1 if an error | |
385 | occurs (and set *TARGET_ERRNO). */ | |
386 | static int | |
dbbca37d TT |
387 | inf_child_fileio_unlink (struct target_ops *self, |
388 | const char *filename, int *target_errno) | |
7313baad UW |
389 | { |
390 | int ret; | |
391 | ||
392 | ret = unlink (filename); | |
393 | if (ret == -1) | |
394 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
395 | ||
396 | return ret; | |
397 | } | |
398 | ||
b9e7b9c3 UW |
399 | /* Read value of symbolic link FILENAME on the target. Return a |
400 | null-terminated string allocated via xmalloc, or NULL if an error | |
401 | occurs (and set *TARGET_ERRNO). */ | |
402 | static char * | |
fab5aa7c TT |
403 | inf_child_fileio_readlink (struct target_ops *self, |
404 | const char *filename, int *target_errno) | |
b9e7b9c3 UW |
405 | { |
406 | /* We support readlink only on systems that also provide a compile-time | |
d8d2a3ee PA |
407 | maximum path length (PATH_MAX), at least for now. */ |
408 | #if defined (HAVE_READLINK) && defined (PATH_MAX) | |
409 | char buf[PATH_MAX]; | |
b9e7b9c3 UW |
410 | int len; |
411 | char *ret; | |
412 | ||
413 | len = readlink (filename, buf, sizeof buf); | |
414 | if (len < 0) | |
415 | { | |
416 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
417 | return NULL; | |
418 | } | |
419 | ||
420 | ret = xmalloc (len + 1); | |
421 | memcpy (ret, buf, len); | |
422 | ret[len] = '\0'; | |
423 | return ret; | |
424 | #else | |
425 | *target_errno = FILEIO_ENOSYS; | |
426 | return NULL; | |
427 | #endif | |
428 | } | |
429 | ||
5808517f | 430 | static int |
2c152180 | 431 | inf_child_use_agent (struct target_ops *self, int use) |
5808517f YQ |
432 | { |
433 | if (agent_loaded_p ()) | |
434 | { | |
435 | use_agent = use; | |
436 | return 1; | |
437 | } | |
438 | else | |
439 | return 0; | |
440 | } | |
441 | ||
442 | static int | |
fe38f897 | 443 | inf_child_can_use_agent (struct target_ops *self) |
5808517f YQ |
444 | { |
445 | return agent_loaded_p (); | |
446 | } | |
7313baad | 447 | |
b3ccfe11 TT |
448 | /* Default implementation of the to_can_async_p and |
449 | to_supports_non_stop methods. */ | |
450 | ||
451 | static int | |
452 | return_zero (struct target_ops *ignore) | |
453 | { | |
454 | return 0; | |
455 | } | |
456 | ||
5bf970f9 AC |
457 | struct target_ops * |
458 | inf_child_target (void) | |
459 | { | |
41bf6aca | 460 | struct target_ops *t = XCNEW (struct target_ops); |
abbb1732 | 461 | |
4ebfc96e PA |
462 | t->to_shortname = "native"; |
463 | t->to_longname = "Native process"; | |
464 | t->to_doc = "Native process (started by the \"run\" command)."; | |
5bf970f9 | 465 | t->to_open = inf_child_open; |
6a3cb8e8 PA |
466 | t->to_close = inf_child_close; |
467 | t->to_disconnect = inf_child_disconnect; | |
5bf970f9 | 468 | t->to_post_attach = inf_child_post_attach; |
7681f339 AC |
469 | t->to_fetch_registers = inf_child_fetch_inferior_registers; |
470 | t->to_store_registers = inf_child_store_inferior_registers; | |
5bf970f9 AC |
471 | t->to_prepare_to_store = inf_child_prepare_to_store; |
472 | t->to_insert_breakpoint = memory_insert_breakpoint; | |
473 | t->to_remove_breakpoint = memory_remove_breakpoint; | |
d6b64346 PA |
474 | t->to_terminal_init = child_terminal_init; |
475 | t->to_terminal_inferior = child_terminal_inferior; | |
476 | t->to_terminal_ours_for_output = child_terminal_ours_for_output; | |
477 | t->to_terminal_save_ours = child_terminal_save_ours; | |
478 | t->to_terminal_ours = child_terminal_ours; | |
5bf970f9 AC |
479 | t->to_terminal_info = child_terminal_info; |
480 | t->to_post_startup_inferior = inf_child_post_startup_inferior; | |
5bf970f9 | 481 | t->to_follow_fork = inf_child_follow_fork; |
5bf970f9 | 482 | t->to_can_run = inf_child_can_run; |
b3ccfe11 TT |
483 | /* We must default these because they must be implemented by any |
484 | target that can run. */ | |
485 | t->to_can_async_p = return_zero; | |
486 | t->to_supports_non_stop = return_zero; | |
5bf970f9 AC |
487 | t->to_pid_to_exec_file = inf_child_pid_to_exec_file; |
488 | t->to_stratum = process_stratum; | |
c35b1492 PA |
489 | t->to_has_all_memory = default_child_has_all_memory; |
490 | t->to_has_memory = default_child_has_memory; | |
491 | t->to_has_stack = default_child_has_stack; | |
492 | t->to_has_registers = default_child_has_registers; | |
493 | t->to_has_execution = default_child_has_execution; | |
7313baad UW |
494 | t->to_fileio_open = inf_child_fileio_open; |
495 | t->to_fileio_pwrite = inf_child_fileio_pwrite; | |
496 | t->to_fileio_pread = inf_child_fileio_pread; | |
497 | t->to_fileio_close = inf_child_fileio_close; | |
498 | t->to_fileio_unlink = inf_child_fileio_unlink; | |
b9e7b9c3 | 499 | t->to_fileio_readlink = inf_child_fileio_readlink; |
5bf970f9 | 500 | t->to_magic = OPS_MAGIC; |
5808517f YQ |
501 | t->to_use_agent = inf_child_use_agent; |
502 | t->to_can_use_agent = inf_child_can_use_agent; | |
6a3cb8e8 PA |
503 | |
504 | /* Store a pointer so we can push the most-derived target from | |
505 | inf_child_open. */ | |
506 | inf_child_ops = t; | |
507 | ||
5bf970f9 AC |
508 | return t; |
509 | } |