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 | |
dab06dbe PA |
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; | |
2ea28649 | 57 | ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus)); |
dab06dbe PA |
58 | } |
59 | else | |
60 | { | |
61 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
2ea28649 | 62 | ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus)); |
dab06dbe PA |
63 | } |
64 | } | |
65 | ||
5bf970f9 AC |
66 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this |
67 | for all registers. */ | |
68 | ||
69 | static void | |
28439f5e PA |
70 | inf_child_fetch_inferior_registers (struct target_ops *ops, |
71 | struct regcache *regcache, int regnum) | |
5bf970f9 AC |
72 | { |
73 | if (regnum == -1) | |
74 | { | |
b1a653ae UW |
75 | for (regnum = 0; |
76 | regnum < gdbarch_num_regs (get_regcache_arch (regcache)); | |
77 | regnum++) | |
56be3814 | 78 | regcache_raw_supply (regcache, regnum, NULL); |
5bf970f9 AC |
79 | } |
80 | else | |
56be3814 | 81 | regcache_raw_supply (regcache, regnum, NULL); |
5bf970f9 AC |
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 | |
28439f5e PA |
88 | inf_child_store_inferior_registers (struct target_ops *ops, |
89 | struct regcache *regcache, int regnum) | |
5bf970f9 AC |
90 | { |
91 | } | |
92 | ||
5bf970f9 | 93 | static void |
f045800c | 94 | inf_child_post_attach (struct target_ops *self, int pid) |
5bf970f9 | 95 | { |
f1aea813 PA |
96 | /* This target doesn't require a meaningful "post attach" operation |
97 | by a debugger. */ | |
5bf970f9 AC |
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 | |
f32dbf8c MM |
107 | inf_child_prepare_to_store (struct target_ops *self, |
108 | struct regcache *regcache) | |
5bf970f9 AC |
109 | { |
110 | } | |
111 | ||
112 | static void | |
113 | inf_child_open (char *arg, int from_tty) | |
114 | { | |
5db9f0bd | 115 | error (_("Use the \"run\" command to start a child process.")); |
5bf970f9 AC |
116 | } |
117 | ||
118 | static void | |
2e97a79e | 119 | inf_child_post_startup_inferior (struct target_ops *self, ptid_t ptid) |
5bf970f9 | 120 | { |
f1aea813 PA |
121 | /* This target doesn't require a meaningful "post startup inferior" |
122 | operation by a debugger. */ | |
5bf970f9 AC |
123 | } |
124 | ||
5bf970f9 | 125 | static int |
07107ca6 LM |
126 | inf_child_follow_fork (struct target_ops *ops, int follow_child, |
127 | int detach_fork) | |
5bf970f9 | 128 | { |
f1aea813 | 129 | /* This target doesn't support following fork or vfork events. */ |
5bf970f9 AC |
130 | return 0; |
131 | } | |
132 | ||
5bf970f9 | 133 | static int |
da82bd6b | 134 | inf_child_can_run (struct target_ops *self) |
5bf970f9 AC |
135 | { |
136 | return 1; | |
137 | } | |
138 | ||
5bf970f9 | 139 | static char * |
8dd27370 | 140 | inf_child_pid_to_exec_file (struct target_ops *self, int pid) |
5bf970f9 | 141 | { |
f1aea813 PA |
142 | /* This target doesn't support translation of a process ID to the |
143 | filename of the executable file. */ | |
5bf970f9 AC |
144 | return NULL; |
145 | } | |
146 | ||
7313baad UW |
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 | |
cd897586 TT |
237 | inf_child_fileio_open (struct target_ops *self, |
238 | const char *filename, int flags, int mode, | |
7313baad UW |
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. */ | |
614c279d | 252 | fd = gdb_open_cloexec (filename, nat_flags, mode); |
7313baad UW |
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 | |
0d866f62 TT |
263 | inf_child_fileio_pwrite (struct target_ops *self, |
264 | int fd, const gdb_byte *write_buf, int len, | |
7313baad UW |
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 | |
7c3270ae | 272 | ret = -1; |
7313baad | 273 | #endif |
7c3270ae UW |
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 | } | |
7313baad UW |
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 | |
a3be983c TT |
292 | inf_child_fileio_pread (struct target_ops *self, |
293 | int fd, gdb_byte *read_buf, int len, | |
7313baad UW |
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 | |
7c3270ae | 301 | ret = -1; |
7313baad | 302 | #endif |
7c3270ae UW |
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 | } | |
7313baad UW |
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 | |
df39ea25 | 320 | inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno) |
7313baad UW |
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 | |
dbbca37d TT |
334 | inf_child_fileio_unlink (struct target_ops *self, |
335 | const char *filename, int *target_errno) | |
7313baad UW |
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 | ||
b9e7b9c3 UW |
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 * | |
fab5aa7c TT |
350 | inf_child_fileio_readlink (struct target_ops *self, |
351 | const char *filename, int *target_errno) | |
b9e7b9c3 UW |
352 | { |
353 | /* We support readlink only on systems that also provide a compile-time | |
d8d2a3ee PA |
354 | maximum path length (PATH_MAX), at least for now. */ |
355 | #if defined (HAVE_READLINK) && defined (PATH_MAX) | |
356 | char buf[PATH_MAX]; | |
b9e7b9c3 UW |
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 | ||
5808517f | 377 | static int |
2c152180 | 378 | inf_child_use_agent (struct target_ops *self, int use) |
5808517f YQ |
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 | |
fe38f897 | 390 | inf_child_can_use_agent (struct target_ops *self) |
5808517f YQ |
391 | { |
392 | return agent_loaded_p (); | |
393 | } | |
7313baad | 394 | |
b3ccfe11 TT |
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 | ||
5bf970f9 AC |
404 | struct target_ops * |
405 | inf_child_target (void) | |
406 | { | |
41bf6aca | 407 | struct target_ops *t = XCNEW (struct target_ops); |
abbb1732 | 408 | |
5bf970f9 | 409 | t->to_shortname = "child"; |
5db9f0bd PA |
410 | t->to_longname = "Child process"; |
411 | t->to_doc = "Child process (started by the \"run\" command)."; | |
5bf970f9 AC |
412 | t->to_open = inf_child_open; |
413 | t->to_post_attach = inf_child_post_attach; | |
7681f339 AC |
414 | t->to_fetch_registers = inf_child_fetch_inferior_registers; |
415 | t->to_store_registers = inf_child_store_inferior_registers; | |
5bf970f9 AC |
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; | |
d6b64346 PA |
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; | |
5bf970f9 AC |
424 | t->to_terminal_info = child_terminal_info; |
425 | t->to_post_startup_inferior = inf_child_post_startup_inferior; | |
5bf970f9 | 426 | t->to_follow_fork = inf_child_follow_fork; |
5bf970f9 | 427 | t->to_can_run = inf_child_can_run; |
b3ccfe11 TT |
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; | |
5bf970f9 AC |
432 | t->to_pid_to_exec_file = inf_child_pid_to_exec_file; |
433 | t->to_stratum = process_stratum; | |
c35b1492 PA |
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; | |
7313baad UW |
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; | |
b9e7b9c3 | 444 | t->to_fileio_readlink = inf_child_fileio_readlink; |
5bf970f9 | 445 | t->to_magic = OPS_MAGIC; |
5808517f YQ |
446 | t->to_use_agent = inf_child_use_agent; |
447 | t->to_can_use_agent = inf_child_can_use_agent; | |
5bf970f9 AC |
448 | return t; |
449 | } |