Commit | Line | Data |
---|---|---|
5bf970f9 AC |
1 | /* Default child (native) target interface, for GDB when running under |
2 | Unix. | |
3 | ||
28e7fd62 | 4 | Copyright (C) 1988-2013 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" | |
7681f339 | 27 | #include "gdb_string.h" |
a71b5a38 | 28 | #include "gdb_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" |
7313baad | 33 | |
b9e7b9c3 UW |
34 | #ifdef HAVE_SYS_PARAM_H |
35 | #include <sys/param.h> /* for MAXPATHLEN */ | |
36 | #endif | |
7313baad | 37 | #include <sys/types.h> |
7313baad UW |
38 | #include <fcntl.h> |
39 | #include <unistd.h> | |
5bf970f9 | 40 | |
dab06dbe PA |
41 | /* Helper function for child_wait and the derivatives of child_wait. |
42 | HOSTSTATUS is the waitstatus from wait() or the equivalent; store our | |
43 | translation of that in OURSTATUS. */ | |
44 | void | |
45 | store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus) | |
46 | { | |
47 | if (WIFEXITED (hoststatus)) | |
48 | { | |
49 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
50 | ourstatus->value.integer = WEXITSTATUS (hoststatus); | |
51 | } | |
52 | else if (!WIFSTOPPED (hoststatus)) | |
53 | { | |
54 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
2ea28649 | 55 | ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus)); |
dab06dbe PA |
56 | } |
57 | else | |
58 | { | |
59 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
2ea28649 | 60 | ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus)); |
dab06dbe PA |
61 | } |
62 | } | |
63 | ||
5bf970f9 AC |
64 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this |
65 | for all registers. */ | |
66 | ||
67 | static void | |
28439f5e PA |
68 | inf_child_fetch_inferior_registers (struct target_ops *ops, |
69 | struct regcache *regcache, int regnum) | |
5bf970f9 AC |
70 | { |
71 | if (regnum == -1) | |
72 | { | |
b1a653ae UW |
73 | for (regnum = 0; |
74 | regnum < gdbarch_num_regs (get_regcache_arch (regcache)); | |
75 | regnum++) | |
56be3814 | 76 | regcache_raw_supply (regcache, regnum, NULL); |
5bf970f9 AC |
77 | } |
78 | else | |
56be3814 | 79 | regcache_raw_supply (regcache, regnum, NULL); |
5bf970f9 AC |
80 | } |
81 | ||
82 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do | |
83 | this for all registers (including the floating point registers). */ | |
84 | ||
85 | static void | |
28439f5e PA |
86 | inf_child_store_inferior_registers (struct target_ops *ops, |
87 | struct regcache *regcache, int regnum) | |
5bf970f9 AC |
88 | { |
89 | } | |
90 | ||
5bf970f9 AC |
91 | static void |
92 | inf_child_post_attach (int pid) | |
93 | { | |
94 | /* This version of Unix doesn't require a meaningful "post attach" | |
95 | operation by a debugger. */ | |
96 | } | |
97 | ||
98 | /* Get ready to modify the registers array. On machines which store | |
99 | individual registers, this doesn't need to do anything. On | |
100 | machines which store all the registers in one fell swoop, this | |
101 | makes sure that registers contains all the registers from the | |
102 | program being debugged. */ | |
103 | ||
104 | static void | |
316f2060 | 105 | inf_child_prepare_to_store (struct regcache *regcache) |
5bf970f9 AC |
106 | { |
107 | } | |
108 | ||
109 | static void | |
110 | inf_child_open (char *arg, int from_tty) | |
111 | { | |
8a3fe4f8 | 112 | error (_("Use the \"run\" command to start a Unix child process.")); |
5bf970f9 AC |
113 | } |
114 | ||
115 | static void | |
116 | inf_child_post_startup_inferior (ptid_t ptid) | |
117 | { | |
118 | /* This version of Unix doesn't require a meaningful "post startup | |
119 | inferior" operation by a debugger. */ | |
120 | } | |
121 | ||
5bf970f9 | 122 | static int |
ee057212 | 123 | inf_child_follow_fork (struct target_ops *ops, int follow_child) |
5bf970f9 AC |
124 | { |
125 | /* This version of Unix doesn't support following fork or vfork | |
126 | events. */ | |
127 | return 0; | |
128 | } | |
129 | ||
5bf970f9 AC |
130 | static int |
131 | inf_child_can_run (void) | |
132 | { | |
133 | return 1; | |
134 | } | |
135 | ||
5bf970f9 AC |
136 | static char * |
137 | inf_child_pid_to_exec_file (int pid) | |
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 | |
234 | inf_child_fileio_open (const char *filename, int flags, int mode, | |
235 | int *target_errno) | |
236 | { | |
237 | int nat_flags; | |
238 | int fd; | |
239 | ||
240 | if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1) | |
241 | { | |
242 | *target_errno = FILEIO_EINVAL; | |
243 | return -1; | |
244 | } | |
245 | ||
246 | /* We do not need to convert MODE, since the fileio protocol uses | |
247 | the standard values. */ | |
248 | fd = open (filename, nat_flags, mode); | |
249 | if (fd == -1) | |
250 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
251 | ||
252 | return fd; | |
253 | } | |
254 | ||
255 | /* Write up to LEN bytes from WRITE_BUF to FD on the target. | |
256 | Return the number of bytes written, or -1 if an error occurs | |
257 | (and set *TARGET_ERRNO). */ | |
258 | static int | |
259 | inf_child_fileio_pwrite (int fd, const gdb_byte *write_buf, int len, | |
260 | ULONGEST offset, int *target_errno) | |
261 | { | |
262 | int ret; | |
263 | ||
264 | #ifdef HAVE_PWRITE | |
265 | ret = pwrite (fd, write_buf, len, (long) offset); | |
266 | #else | |
7c3270ae | 267 | ret = -1; |
7313baad | 268 | #endif |
7c3270ae UW |
269 | /* If we have no pwrite or it failed for this file, use lseek/write. */ |
270 | if (ret == -1) | |
271 | { | |
272 | ret = lseek (fd, (long) offset, SEEK_SET); | |
273 | if (ret != -1) | |
274 | ret = write (fd, write_buf, len); | |
275 | } | |
7313baad UW |
276 | |
277 | if (ret == -1) | |
278 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
279 | ||
280 | return ret; | |
281 | } | |
282 | ||
283 | /* Read up to LEN bytes FD on the target into READ_BUF. | |
284 | Return the number of bytes read, or -1 if an error occurs | |
285 | (and set *TARGET_ERRNO). */ | |
286 | static int | |
287 | inf_child_fileio_pread (int fd, gdb_byte *read_buf, int len, | |
288 | ULONGEST offset, int *target_errno) | |
289 | { | |
290 | int ret; | |
291 | ||
292 | #ifdef HAVE_PREAD | |
293 | ret = pread (fd, read_buf, len, (long) offset); | |
294 | #else | |
7c3270ae | 295 | ret = -1; |
7313baad | 296 | #endif |
7c3270ae UW |
297 | /* If we have no pread or it failed for this file, use lseek/read. */ |
298 | if (ret == -1) | |
299 | { | |
300 | ret = lseek (fd, (long) offset, SEEK_SET); | |
301 | if (ret != -1) | |
302 | ret = read (fd, read_buf, len); | |
303 | } | |
7313baad UW |
304 | |
305 | if (ret == -1) | |
306 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
307 | ||
308 | return ret; | |
309 | } | |
310 | ||
311 | /* Close FD on the target. Return 0, or -1 if an error occurs | |
312 | (and set *TARGET_ERRNO). */ | |
313 | static int | |
314 | inf_child_fileio_close (int fd, int *target_errno) | |
315 | { | |
316 | int ret; | |
317 | ||
318 | ret = close (fd); | |
319 | if (ret == -1) | |
320 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
321 | ||
322 | return ret; | |
323 | } | |
324 | ||
325 | /* Unlink FILENAME on the target. Return 0, or -1 if an error | |
326 | occurs (and set *TARGET_ERRNO). */ | |
327 | static int | |
328 | inf_child_fileio_unlink (const char *filename, int *target_errno) | |
329 | { | |
330 | int ret; | |
331 | ||
332 | ret = unlink (filename); | |
333 | if (ret == -1) | |
334 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
335 | ||
336 | return ret; | |
337 | } | |
338 | ||
b9e7b9c3 UW |
339 | /* Read value of symbolic link FILENAME on the target. Return a |
340 | null-terminated string allocated via xmalloc, or NULL if an error | |
341 | occurs (and set *TARGET_ERRNO). */ | |
342 | static char * | |
343 | inf_child_fileio_readlink (const char *filename, int *target_errno) | |
344 | { | |
345 | /* We support readlink only on systems that also provide a compile-time | |
346 | maximum path length (MAXPATHLEN), at least for now. */ | |
347 | #if defined (HAVE_READLINK) && defined (MAXPATHLEN) | |
0270a750 | 348 | char buf[MAXPATHLEN - 1]; |
b9e7b9c3 UW |
349 | int len; |
350 | char *ret; | |
351 | ||
352 | len = readlink (filename, buf, sizeof buf); | |
353 | if (len < 0) | |
354 | { | |
355 | *target_errno = inf_child_errno_to_fileio_error (errno); | |
356 | return NULL; | |
357 | } | |
358 | ||
359 | ret = xmalloc (len + 1); | |
360 | memcpy (ret, buf, len); | |
361 | ret[len] = '\0'; | |
362 | return ret; | |
363 | #else | |
364 | *target_errno = FILEIO_ENOSYS; | |
365 | return NULL; | |
366 | #endif | |
367 | } | |
368 | ||
5808517f YQ |
369 | static int |
370 | inf_child_use_agent (int use) | |
371 | { | |
372 | if (agent_loaded_p ()) | |
373 | { | |
374 | use_agent = use; | |
375 | return 1; | |
376 | } | |
377 | else | |
378 | return 0; | |
379 | } | |
380 | ||
381 | static int | |
382 | inf_child_can_use_agent (void) | |
383 | { | |
384 | return agent_loaded_p (); | |
385 | } | |
7313baad | 386 | |
5bf970f9 AC |
387 | struct target_ops * |
388 | inf_child_target (void) | |
389 | { | |
390 | struct target_ops *t = XZALLOC (struct target_ops); | |
abbb1732 | 391 | |
5bf970f9 AC |
392 | t->to_shortname = "child"; |
393 | t->to_longname = "Unix child process"; | |
394 | t->to_doc = "Unix child process (started by the \"run\" command)."; | |
395 | t->to_open = inf_child_open; | |
396 | t->to_post_attach = inf_child_post_attach; | |
7681f339 AC |
397 | t->to_fetch_registers = inf_child_fetch_inferior_registers; |
398 | t->to_store_registers = inf_child_store_inferior_registers; | |
5bf970f9 AC |
399 | t->to_prepare_to_store = inf_child_prepare_to_store; |
400 | t->to_insert_breakpoint = memory_insert_breakpoint; | |
401 | t->to_remove_breakpoint = memory_remove_breakpoint; | |
402 | t->to_terminal_init = terminal_init_inferior; | |
403 | t->to_terminal_inferior = terminal_inferior; | |
404 | t->to_terminal_ours_for_output = terminal_ours_for_output; | |
405 | t->to_terminal_save_ours = terminal_save_ours; | |
406 | t->to_terminal_ours = terminal_ours; | |
407 | t->to_terminal_info = child_terminal_info; | |
408 | t->to_post_startup_inferior = inf_child_post_startup_inferior; | |
5bf970f9 | 409 | t->to_follow_fork = inf_child_follow_fork; |
5bf970f9 | 410 | t->to_can_run = inf_child_can_run; |
5bf970f9 AC |
411 | t->to_pid_to_exec_file = inf_child_pid_to_exec_file; |
412 | t->to_stratum = process_stratum; | |
c35b1492 PA |
413 | t->to_has_all_memory = default_child_has_all_memory; |
414 | t->to_has_memory = default_child_has_memory; | |
415 | t->to_has_stack = default_child_has_stack; | |
416 | t->to_has_registers = default_child_has_registers; | |
417 | t->to_has_execution = default_child_has_execution; | |
7313baad UW |
418 | t->to_fileio_open = inf_child_fileio_open; |
419 | t->to_fileio_pwrite = inf_child_fileio_pwrite; | |
420 | t->to_fileio_pread = inf_child_fileio_pread; | |
421 | t->to_fileio_close = inf_child_fileio_close; | |
422 | t->to_fileio_unlink = inf_child_fileio_unlink; | |
b9e7b9c3 | 423 | t->to_fileio_readlink = inf_child_fileio_readlink; |
5bf970f9 | 424 | t->to_magic = OPS_MAGIC; |
5808517f YQ |
425 | t->to_use_agent = inf_child_use_agent; |
426 | t->to_can_use_agent = inf_child_can_use_agent; | |
5bf970f9 AC |
427 | return t; |
428 | } |