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