Commit | Line | Data |
---|---|---|
2c4a536d | 1 | /* Low-level child interface to ptrace. |
5bf970f9 | 2 | |
61baf725 | 3 | Copyright (C) 1988-2017 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 AC |
19 | |
20 | #include "defs.h" | |
5bf970f9 | 21 | #include "command.h" |
2c4a536d MK |
22 | #include "inferior.h" |
23 | #include "inflow.h" | |
191c4426 | 24 | #include "terminal.h" |
5bf970f9 | 25 | #include "gdbcore.h" |
8785ced0 | 26 | #include "regcache.h" |
e3790375 | 27 | #include "nat/gdb_ptrace.h" |
34a17005 | 28 | #include "gdb_wait.h" |
5bf970f9 AC |
29 | #include <signal.h> |
30 | ||
2c0b251b | 31 | #include "inf-ptrace.h" |
2c4a536d | 32 | #include "inf-child.h" |
af990527 | 33 | #include "gdbthread.h" |
2c4a536d | 34 | |
c7c14b96 MK |
35 | \f |
36 | ||
735f54b4 MK |
37 | #ifdef PT_GET_PROCESS_STATE |
38 | ||
d83ad864 DB |
39 | /* Target hook for follow_fork. On entry and at return inferior_ptid is |
40 | the ptid of the followed inferior. */ | |
41 | ||
735f54b4 | 42 | static int |
07107ca6 LM |
43 | inf_ptrace_follow_fork (struct target_ops *ops, int follow_child, |
44 | int detach_fork) | |
735f54b4 | 45 | { |
d83ad864 | 46 | if (!follow_child) |
735f54b4 | 47 | { |
ebf3aa72 MK |
48 | struct thread_info *tp = inferior_thread (); |
49 | pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid); | |
191c4426 | 50 | |
b242c3c2 PA |
51 | /* Breakpoints have already been detached from the child by |
52 | infrun.c. */ | |
735f54b4 | 53 | |
d83ad864 | 54 | if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1) |
735f54b4 MK |
55 | perror_with_name (("ptrace")); |
56 | } | |
57 | ||
58 | return 0; | |
59 | } | |
60 | ||
e85e8e5e MK |
61 | static int |
62 | inf_ptrace_insert_fork_catchpoint (struct target_ops *self, int pid) | |
63 | { | |
64 | return 0; | |
65 | } | |
66 | ||
67 | static int | |
68 | inf_ptrace_remove_fork_catchpoint (struct target_ops *self, int pid) | |
69 | { | |
70 | return 0; | |
71 | } | |
72 | ||
735f54b4 MK |
73 | #endif /* PT_GET_PROCESS_STATE */ |
74 | \f | |
75 | ||
4b8a1a28 | 76 | /* Prepare to be traced. */ |
5bf970f9 AC |
77 | |
78 | static void | |
c7c14b96 | 79 | inf_ptrace_me (void) |
5bf970f9 | 80 | { |
c7c14b96 | 81 | /* "Trace me, Dr. Memory!" */ |
4b8a1a28 | 82 | ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0); |
5bf970f9 AC |
83 | } |
84 | ||
136d6dae VP |
85 | /* Start a new inferior Unix child process. EXEC_FILE is the file to |
86 | run, ALLARGS is a string containing the arguments to the program. | |
87 | ENV is the environment vector to pass. If FROM_TTY is non-zero, be | |
88 | chatty about it. */ | |
5bf970f9 AC |
89 | |
90 | static void | |
136d6dae VP |
91 | inf_ptrace_create_inferior (struct target_ops *ops, |
92 | char *exec_file, char *allargs, char **env, | |
93 | int from_tty) | |
5bf970f9 | 94 | { |
136d6dae VP |
95 | int pid; |
96 | ||
c0edd9ed JK |
97 | /* Do not change either targets above or the same target if already present. |
98 | The reason is the target stack is shared across multiple inferiors. */ | |
99 | int ops_already_pushed = target_is_pushed (ops); | |
25f43500 | 100 | struct cleanup *back_to = make_cleanup (null_cleanup, NULL); |
c0edd9ed JK |
101 | |
102 | if (! ops_already_pushed) | |
103 | { | |
104 | /* Clear possible core file with its process_stratum. */ | |
105 | push_target (ops); | |
25f43500 | 106 | make_cleanup_unpush_target (ops); |
c0edd9ed JK |
107 | } |
108 | ||
136d6dae | 109 | pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL, |
e69860f1 | 110 | NULL, NULL, NULL); |
136d6dae | 111 | |
25f43500 | 112 | discard_cleanups (back_to); |
5bf970f9 | 113 | |
c7c14b96 MK |
114 | startup_inferior (START_INFERIOR_TRAPS_EXPECTED); |
115 | ||
116 | /* On some targets, there must be some explicit actions taken after | |
117 | the inferior has been started up. */ | |
118 | target_post_startup_inferior (pid_to_ptid (pid)); | |
5bf970f9 AC |
119 | } |
120 | ||
e4ef629d MK |
121 | #ifdef PT_GET_PROCESS_STATE |
122 | ||
123 | static void | |
2e97a79e | 124 | inf_ptrace_post_startup_inferior (struct target_ops *self, ptid_t pid) |
e4ef629d MK |
125 | { |
126 | ptrace_event_t pe; | |
127 | ||
128 | /* Set the initial event mask. */ | |
129 | memset (&pe, 0, sizeof pe); | |
130 | pe.pe_set_event |= PTRACE_FORK; | |
131 | if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid), | |
132 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
133 | perror_with_name (("ptrace")); | |
134 | } | |
135 | ||
136 | #endif | |
137 | ||
4b8a1a28 MK |
138 | /* Clean up a rotting corpse of an inferior after it died. */ |
139 | ||
c7c14b96 | 140 | static void |
136d6dae | 141 | inf_ptrace_mourn_inferior (struct target_ops *ops) |
5bf970f9 | 142 | { |
4b8a1a28 MK |
143 | int status; |
144 | ||
145 | /* Wait just one more time to collect the inferior's exit status. | |
f010475d | 146 | Do not check whether this succeeds though, since we may be |
4b8a1a28 | 147 | dealing with a process that we attached to. Such a process will |
3d450bdd | 148 | only report its exit status to its original parent. */ |
4b8a1a28 MK |
149 | waitpid (ptid_get_pid (inferior_ptid), &status, 0); |
150 | ||
c1ee2fb3 | 151 | inf_child_mourn_inferior (ops); |
5bf970f9 AC |
152 | } |
153 | ||
4b8a1a28 MK |
154 | /* Attach to the process specified by ARGS. If FROM_TTY is non-zero, |
155 | be chatty about it. */ | |
5bf970f9 AC |
156 | |
157 | static void | |
c0939df1 | 158 | inf_ptrace_attach (struct target_ops *ops, const char *args, int from_tty) |
5bf970f9 AC |
159 | { |
160 | char *exec_file; | |
4b8a1a28 | 161 | pid_t pid; |
181e7f93 | 162 | struct inferior *inf; |
5bf970f9 | 163 | |
c0edd9ed JK |
164 | /* Do not change either targets above or the same target if already present. |
165 | The reason is the target stack is shared across multiple inferiors. */ | |
166 | int ops_already_pushed = target_is_pushed (ops); | |
25f43500 | 167 | struct cleanup *back_to = make_cleanup (null_cleanup, NULL); |
c0edd9ed | 168 | |
74164c56 | 169 | pid = parse_pid_to_attach (args); |
5bf970f9 | 170 | |
f6ffd89b | 171 | if (pid == getpid ()) /* Trying to masturbate? */ |
8a3fe4f8 | 172 | error (_("I refuse to debug myself!")); |
5bf970f9 | 173 | |
c0edd9ed JK |
174 | if (! ops_already_pushed) |
175 | { | |
176 | /* target_pid_to_str already uses the target. Also clear possible core | |
177 | file with its process_stratum. */ | |
178 | push_target (ops); | |
25f43500 | 179 | make_cleanup_unpush_target (ops); |
c0edd9ed JK |
180 | } |
181 | ||
5bf970f9 AC |
182 | if (from_tty) |
183 | { | |
4b8a1a28 | 184 | exec_file = get_exec_file (0); |
5bf970f9 AC |
185 | |
186 | if (exec_file) | |
a3f17187 | 187 | printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file, |
5bf970f9 AC |
188 | target_pid_to_str (pid_to_ptid (pid))); |
189 | else | |
a3f17187 | 190 | printf_unfiltered (_("Attaching to %s\n"), |
5bf970f9 AC |
191 | target_pid_to_str (pid_to_ptid (pid))); |
192 | ||
193 | gdb_flush (gdb_stdout); | |
194 | } | |
195 | ||
6e1e94ea MK |
196 | #ifdef PT_ATTACH |
197 | errno = 0; | |
4b8a1a28 | 198 | ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0); |
6e1e94ea | 199 | if (errno != 0) |
e2e0b3e5 | 200 | perror_with_name (("ptrace")); |
6e1e94ea | 201 | #else |
8a3fe4f8 | 202 | error (_("This system does not support attaching to a process")); |
6e1e94ea | 203 | #endif |
5bf970f9 | 204 | |
6c95b8df PA |
205 | inf = current_inferior (); |
206 | inferior_appeared (inf, pid); | |
181e7f93 | 207 | inf->attach_flag = 1; |
6c95b8df | 208 | inferior_ptid = pid_to_ptid (pid); |
7f9f62ba | 209 | |
af990527 PA |
210 | /* Always add a main thread. If some target extends the ptrace |
211 | target, it should decorate the ptid later with more info. */ | |
212 | add_thread_silent (inferior_ptid); | |
213 | ||
25f43500 | 214 | discard_cleanups (back_to); |
5bf970f9 AC |
215 | } |
216 | ||
e4ef629d MK |
217 | #ifdef PT_GET_PROCESS_STATE |
218 | ||
460fac3c | 219 | static void |
f045800c | 220 | inf_ptrace_post_attach (struct target_ops *self, int pid) |
e4ef629d MK |
221 | { |
222 | ptrace_event_t pe; | |
223 | ||
224 | /* Set the initial event mask. */ | |
225 | memset (&pe, 0, sizeof pe); | |
226 | pe.pe_set_event |= PTRACE_FORK; | |
227 | if (ptrace (PT_SET_EVENT_MASK, pid, | |
228 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
229 | perror_with_name (("ptrace")); | |
230 | } | |
231 | ||
232 | #endif | |
233 | ||
4b8a1a28 | 234 | /* Detach from the inferior, optionally passing it the signal |
f010475d | 235 | specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */ |
5bf970f9 AC |
236 | |
237 | static void | |
52554a0e | 238 | inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty) |
5bf970f9 | 239 | { |
4b8a1a28 | 240 | pid_t pid = ptid_get_pid (inferior_ptid); |
6e1e94ea | 241 | int sig = 0; |
5bf970f9 | 242 | |
0f48b757 | 243 | target_announce_detach (from_tty); |
5bf970f9 | 244 | if (args) |
6e1e94ea | 245 | sig = atoi (args); |
5bf970f9 | 246 | |
6e1e94ea | 247 | #ifdef PT_DETACH |
4b8a1a28 | 248 | /* We'd better not have left any breakpoints in the program or it'll |
f010475d | 249 | die when it hits one. Also note that this may only work if we |
4b8a1a28 MK |
250 | previously attached to the inferior. It *might* work if we |
251 | started the process ourselves. */ | |
6e1e94ea | 252 | errno = 0; |
4b8a1a28 | 253 | ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig); |
6e1e94ea | 254 | if (errno != 0) |
e2e0b3e5 | 255 | perror_with_name (("ptrace")); |
6e1e94ea | 256 | #else |
8a3fe4f8 | 257 | error (_("This system does not support detaching from a process")); |
6e1e94ea | 258 | #endif |
5bf970f9 | 259 | |
ced2dffb PA |
260 | inf_ptrace_detach_success (ops); |
261 | } | |
262 | ||
263 | /* See inf-ptrace.h. */ | |
264 | ||
265 | void | |
266 | inf_ptrace_detach_success (struct target_ops *ops) | |
267 | { | |
268 | pid_t pid = ptid_get_pid (inferior_ptid); | |
269 | ||
5bf970f9 | 270 | inferior_ptid = null_ptid; |
7f9f62ba | 271 | detach_inferior (pid); |
7a7d3353 | 272 | |
6a3cb8e8 | 273 | inf_child_maybe_unpush_target (ops); |
5bf970f9 AC |
274 | } |
275 | ||
4b8a1a28 MK |
276 | /* Kill the inferior. */ |
277 | ||
5bf970f9 | 278 | static void |
7d85a9c0 | 279 | inf_ptrace_kill (struct target_ops *ops) |
5bf970f9 | 280 | { |
4b8a1a28 | 281 | pid_t pid = ptid_get_pid (inferior_ptid); |
c7c14b96 | 282 | int status; |
c7c14b96 MK |
283 | |
284 | if (pid == 0) | |
285 | return; | |
286 | ||
4b8a1a28 MK |
287 | ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0); |
288 | waitpid (pid, &status, 0); | |
289 | ||
bc1e6c81 | 290 | target_mourn_inferior (inferior_ptid); |
5bf970f9 AC |
291 | } |
292 | ||
bfedc46a | 293 | /* Interrupt the inferior. */ |
c7c14b96 | 294 | |
5bf970f9 | 295 | static void |
bfedc46a | 296 | inf_ptrace_interrupt (struct target_ops *self, ptid_t ptid) |
5bf970f9 | 297 | { |
4b8a1a28 MK |
298 | /* Send a SIGINT to the process group. This acts just like the user |
299 | typed a ^C on the controlling terminal. Note that using a | |
300 | negative process number in kill() is a System V-ism. The proper | |
301 | BSD interface is killpg(). However, all modern BSDs support the | |
302 | System V interface too. */ | |
7e1789f5 | 303 | kill (-inferior_process_group (), SIGINT); |
5bf970f9 AC |
304 | } |
305 | ||
90ad5e1d PA |
306 | /* Return which PID to pass to ptrace in order to observe/control the |
307 | tracee identified by PTID. */ | |
308 | ||
94309df7 | 309 | pid_t |
90ad5e1d PA |
310 | get_ptrace_pid (ptid_t ptid) |
311 | { | |
312 | pid_t pid; | |
313 | ||
314 | /* If we have an LWPID to work with, use it. Otherwise, we're | |
315 | dealing with a non-threaded program/target. */ | |
316 | pid = ptid_get_lwp (ptid); | |
317 | if (pid == 0) | |
318 | pid = ptid_get_pid (ptid); | |
319 | return pid; | |
320 | } | |
321 | ||
4b8a1a28 MK |
322 | /* Resume execution of thread PTID, or all threads if PTID is -1. If |
323 | STEP is nonzero, single-step it. If SIGNAL is nonzero, give it | |
324 | that signal. */ | |
5bf970f9 AC |
325 | |
326 | static void | |
28439f5e | 327 | inf_ptrace_resume (struct target_ops *ops, |
2ea28649 | 328 | ptid_t ptid, int step, enum gdb_signal signal) |
5bf970f9 | 329 | { |
90ad5e1d | 330 | pid_t pid; |
a96d9b2e | 331 | int request; |
c7c14b96 | 332 | |
90ad5e1d | 333 | if (ptid_equal (minus_one_ptid, ptid)) |
4b8a1a28 MK |
334 | /* Resume all threads. Traditionally ptrace() only supports |
335 | single-threaded processes, so simply resume the inferior. */ | |
c1593e4f | 336 | pid = ptid_get_pid (inferior_ptid); |
90ad5e1d PA |
337 | else |
338 | pid = get_ptrace_pid (ptid); | |
c7c14b96 | 339 | |
a96d9b2e SDJ |
340 | if (catch_syscall_enabled () > 0) |
341 | request = PT_SYSCALL; | |
342 | else | |
343 | request = PT_CONTINUE; | |
344 | ||
c7c14b96 MK |
345 | if (step) |
346 | { | |
347 | /* If this system does not support PT_STEP, a higher level | |
348 | function will have called single_step() to transmute the step | |
349 | request into a continue request (by setting breakpoints on | |
350 | all possible successor instructions), so we don't have to | |
351 | worry about that here. */ | |
352 | request = PT_STEP; | |
353 | } | |
354 | ||
355 | /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from | |
356 | where it was. If GDB wanted it to start some other way, we have | |
4b8a1a28 | 357 | already written a new program counter value to the child. */ |
c7c14b96 | 358 | errno = 0; |
2ea28649 | 359 | ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal)); |
c7c14b96 MK |
360 | if (errno != 0) |
361 | perror_with_name (("ptrace")); | |
5bf970f9 AC |
362 | } |
363 | ||
4b8a1a28 MK |
364 | /* Wait for the child specified by PTID to do something. Return the |
365 | process ID of the child, or MINUS_ONE_PTID in case of error; store | |
366 | the status in *OURSTATUS. */ | |
5bf970f9 | 367 | |
c7c14b96 | 368 | static ptid_t |
117de6a9 | 369 | inf_ptrace_wait (struct target_ops *ops, |
47608cb1 | 370 | ptid_t ptid, struct target_waitstatus *ourstatus, int options) |
5bf970f9 | 371 | { |
4b8a1a28 MK |
372 | pid_t pid; |
373 | int status, save_errno; | |
5bf970f9 | 374 | |
c7c14b96 MK |
375 | do |
376 | { | |
4b8a1a28 | 377 | set_sigint_trap (); |
5bf970f9 | 378 | |
4b8a1a28 MK |
379 | do |
380 | { | |
381 | pid = waitpid (ptid_get_pid (ptid), &status, 0); | |
382 | save_errno = errno; | |
383 | } | |
384 | while (pid == -1 && errno == EINTR); | |
5bf970f9 | 385 | |
c7c14b96 | 386 | clear_sigint_trap (); |
5bf970f9 | 387 | |
c7c14b96 MK |
388 | if (pid == -1) |
389 | { | |
c7c14b96 | 390 | fprintf_unfiltered (gdb_stderr, |
4b8a1a28 | 391 | _("Child process unexpectedly missing: %s.\n"), |
c7c14b96 MK |
392 | safe_strerror (save_errno)); |
393 | ||
394 | /* Claim it exited with unknown signal. */ | |
395 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
a493e3e2 | 396 | ourstatus->value.sig = GDB_SIGNAL_UNKNOWN; |
fb66883a | 397 | return inferior_ptid; |
c7c14b96 MK |
398 | } |
399 | ||
4b8a1a28 MK |
400 | /* Ignore terminated detached child processes. */ |
401 | if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid)) | |
402 | pid = -1; | |
c7c14b96 | 403 | } |
4b8a1a28 | 404 | while (pid == -1); |
c7c14b96 | 405 | |
735f54b4 MK |
406 | #ifdef PT_GET_PROCESS_STATE |
407 | if (WIFSTOPPED (status)) | |
408 | { | |
409 | ptrace_state_t pe; | |
410 | pid_t fpid; | |
411 | ||
412 | if (ptrace (PT_GET_PROCESS_STATE, pid, | |
413 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
414 | perror_with_name (("ptrace")); | |
415 | ||
416 | switch (pe.pe_report_event) | |
417 | { | |
418 | case PTRACE_FORK: | |
419 | ourstatus->kind = TARGET_WAITKIND_FORKED; | |
3a3e9ee3 | 420 | ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid); |
735f54b4 MK |
421 | |
422 | /* Make sure the other end of the fork is stopped too. */ | |
423 | fpid = waitpid (pe.pe_other_pid, &status, 0); | |
424 | if (fpid == -1) | |
425 | perror_with_name (("waitpid")); | |
426 | ||
427 | if (ptrace (PT_GET_PROCESS_STATE, fpid, | |
428 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
429 | perror_with_name (("ptrace")); | |
430 | ||
431 | gdb_assert (pe.pe_report_event == PTRACE_FORK); | |
432 | gdb_assert (pe.pe_other_pid == pid); | |
433 | if (fpid == ptid_get_pid (inferior_ptid)) | |
434 | { | |
3a3e9ee3 | 435 | ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid); |
735f54b4 MK |
436 | return pid_to_ptid (fpid); |
437 | } | |
438 | ||
439 | return pid_to_ptid (pid); | |
440 | } | |
441 | } | |
442 | #endif | |
443 | ||
c7c14b96 MK |
444 | store_waitstatus (ourstatus, status); |
445 | return pid_to_ptid (pid); | |
5bf970f9 AC |
446 | } |
447 | ||
edcc890f | 448 | /* Implement the to_xfer_partial target_ops method. */ |
5bf970f9 | 449 | |
9b409511 | 450 | static enum target_xfer_status |
5bf970f9 | 451 | inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object, |
961cb7b5 MK |
452 | const char *annex, gdb_byte *readbuf, |
453 | const gdb_byte *writebuf, | |
9b409511 | 454 | ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) |
5bf970f9 | 455 | { |
4b8a1a28 MK |
456 | pid_t pid = ptid_get_pid (inferior_ptid); |
457 | ||
5bf970f9 AC |
458 | switch (object) |
459 | { | |
460 | case TARGET_OBJECT_MEMORY: | |
f929a579 AC |
461 | #ifdef PT_IO |
462 | /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO | |
463 | request that promises to be much more efficient in reading | |
464 | and writing data in the traced process's address space. */ | |
465 | { | |
466 | struct ptrace_io_desc piod; | |
4b8a1a28 | 467 | |
f929a579 | 468 | /* NOTE: We assume that there are no distinct address spaces |
b457b3dd MK |
469 | for instruction and data. However, on OpenBSD 3.9 and |
470 | later, PIOD_WRITE_D doesn't allow changing memory that's | |
471 | mapped read-only. Since most code segments will be | |
472 | read-only, using PIOD_WRITE_D will prevent us from | |
473 | inserting breakpoints, so we use PIOD_WRITE_I instead. */ | |
474 | piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D; | |
f929a579 AC |
475 | piod.piod_addr = writebuf ? (void *) writebuf : readbuf; |
476 | piod.piod_offs = (void *) (long) offset; | |
477 | piod.piod_len = len; | |
478 | ||
479 | errno = 0; | |
4b8a1a28 | 480 | if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) |
9b409511 | 481 | { |
9b409511 | 482 | /* Return the actual number of bytes read or written. */ |
493443a4 MK |
483 | *xfered_len = piod.piod_len; |
484 | return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; | |
9b409511 | 485 | } |
f929a579 AC |
486 | /* If the PT_IO request is somehow not supported, fallback on |
487 | using PT_WRITE_D/PT_READ_D. Otherwise we will return zero | |
488 | to indicate failure. */ | |
489 | if (errno != EINVAL) | |
9b409511 | 490 | return TARGET_XFER_EOF; |
f929a579 AC |
491 | } |
492 | #endif | |
493 | { | |
494 | union | |
495 | { | |
496 | PTRACE_TYPE_RET word; | |
4b8a1a28 | 497 | gdb_byte byte[sizeof (PTRACE_TYPE_RET)]; |
f929a579 AC |
498 | } buffer; |
499 | ULONGEST rounded_offset; | |
9b409511 | 500 | ULONGEST partial_len; |
4b8a1a28 | 501 | |
cb85a953 AC |
502 | /* Round the start offset down to the next long word |
503 | boundary. */ | |
f929a579 | 504 | rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET); |
4b8a1a28 | 505 | |
cb85a953 AC |
506 | /* Since ptrace will transfer a single word starting at that |
507 | rounded_offset the partial_len needs to be adjusted down to | |
508 | that (remember this function only does a single transfer). | |
509 | Should the required length be even less, adjust it down | |
510 | again. */ | |
511 | partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset; | |
512 | if (partial_len > len) | |
f929a579 | 513 | partial_len = len; |
4b8a1a28 | 514 | |
f929a579 AC |
515 | if (writebuf) |
516 | { | |
cb85a953 AC |
517 | /* If OFFSET:PARTIAL_LEN is smaller than |
518 | ROUNDED_OFFSET:WORDSIZE then a read/modify write will | |
519 | be needed. Read in the entire word. */ | |
f929a579 | 520 | if (rounded_offset < offset |
cb85a953 AC |
521 | || (offset + partial_len |
522 | < rounded_offset + sizeof (PTRACE_TYPE_RET))) | |
f929a579 | 523 | /* Need part of initial word -- fetch it. */ |
4b8a1a28 | 524 | buffer.word = ptrace (PT_READ_I, pid, |
f7dd0ed7 UW |
525 | (PTRACE_TYPE_ARG3)(uintptr_t) |
526 | rounded_offset, 0); | |
4b8a1a28 | 527 | |
f929a579 AC |
528 | /* Copy data to be written over corresponding part of |
529 | buffer. */ | |
f6ffd89b MK |
530 | memcpy (buffer.byte + (offset - rounded_offset), |
531 | writebuf, partial_len); | |
4b8a1a28 | 532 | |
f929a579 | 533 | errno = 0; |
4b8a1a28 | 534 | ptrace (PT_WRITE_D, pid, |
f7dd0ed7 UW |
535 | (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, |
536 | buffer.word); | |
f929a579 AC |
537 | if (errno) |
538 | { | |
539 | /* Using the appropriate one (I or D) is necessary for | |
540 | Gould NP1, at least. */ | |
541 | errno = 0; | |
4b8a1a28 | 542 | ptrace (PT_WRITE_I, pid, |
f7dd0ed7 UW |
543 | (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, |
544 | buffer.word); | |
f929a579 | 545 | if (errno) |
9b409511 | 546 | return TARGET_XFER_EOF; |
f929a579 AC |
547 | } |
548 | } | |
4b8a1a28 | 549 | |
f929a579 AC |
550 | if (readbuf) |
551 | { | |
552 | errno = 0; | |
4b8a1a28 | 553 | buffer.word = ptrace (PT_READ_I, pid, |
f7dd0ed7 UW |
554 | (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, |
555 | 0); | |
f929a579 | 556 | if (errno) |
9b409511 | 557 | return TARGET_XFER_EOF; |
f929a579 AC |
558 | /* Copy appropriate bytes out of the buffer. */ |
559 | memcpy (readbuf, buffer.byte + (offset - rounded_offset), | |
560 | partial_len); | |
561 | } | |
4b8a1a28 | 562 | |
9b409511 YQ |
563 | *xfered_len = partial_len; |
564 | return TARGET_XFER_OK; | |
f929a579 | 565 | } |
5bf970f9 AC |
566 | |
567 | case TARGET_OBJECT_UNWIND_TABLE: | |
2ed4b548 | 568 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
569 | |
570 | case TARGET_OBJECT_AUXV: | |
e8ace1c0 MK |
571 | #if defined (PT_IO) && defined (PIOD_READ_AUXV) |
572 | /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO | |
573 | request that allows us to read the auxilliary vector. Other | |
574 | BSD's may follow if they feel the need to support PIE. */ | |
575 | { | |
576 | struct ptrace_io_desc piod; | |
577 | ||
578 | if (writebuf) | |
2ed4b548 | 579 | return TARGET_XFER_E_IO; |
e8ace1c0 MK |
580 | piod.piod_op = PIOD_READ_AUXV; |
581 | piod.piod_addr = readbuf; | |
582 | piod.piod_offs = (void *) (long) offset; | |
583 | piod.piod_len = len; | |
584 | ||
585 | errno = 0; | |
586 | if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) | |
9b409511 | 587 | { |
9b409511 | 588 | /* Return the actual number of bytes read or written. */ |
493443a4 MK |
589 | *xfered_len = piod.piod_len; |
590 | return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; | |
9b409511 | 591 | } |
e8ace1c0 MK |
592 | } |
593 | #endif | |
2ed4b548 | 594 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
595 | |
596 | case TARGET_OBJECT_WCOOKIE: | |
2ed4b548 | 597 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
598 | |
599 | default: | |
2ed4b548 | 600 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
601 | } |
602 | } | |
603 | ||
4b8a1a28 | 604 | /* Return non-zero if the thread specified by PTID is alive. */ |
c7c14b96 MK |
605 | |
606 | static int | |
28439f5e | 607 | inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid) |
c7c14b96 | 608 | { |
4b8a1a28 MK |
609 | /* ??? Is kill the right way to do this? */ |
610 | return (kill (ptid_get_pid (ptid), 0) != -1); | |
c7c14b96 MK |
611 | } |
612 | ||
613 | /* Print status information about what we're accessing. */ | |
614 | ||
615 | static void | |
616 | inf_ptrace_files_info (struct target_ops *ignore) | |
617 | { | |
181e7f93 PA |
618 | struct inferior *inf = current_inferior (); |
619 | ||
4b8a1a28 | 620 | printf_filtered (_("\tUsing the running image of %s %s.\n"), |
181e7f93 | 621 | inf->attach_flag ? "attached" : "child", |
4b8a1a28 | 622 | target_pid_to_str (inferior_ptid)); |
5bf970f9 AC |
623 | } |
624 | ||
117de6a9 PA |
625 | static char * |
626 | inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid) | |
627 | { | |
628 | return normal_pid_to_str (ptid); | |
629 | } | |
630 | ||
e8ace1c0 MK |
631 | #if defined (PT_IO) && defined (PIOD_READ_AUXV) |
632 | ||
633 | /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR. | |
634 | Return 0 if *READPTR is already at the end of the buffer. | |
635 | Return -1 if there is insufficient buffer for a whole entry. | |
636 | Return 1 if an entry was read into *TYPEP and *VALP. */ | |
637 | ||
638 | static int | |
639 | inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr, | |
640 | gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp) | |
641 | { | |
f5656ead TT |
642 | struct type *int_type = builtin_type (target_gdbarch ())->builtin_int; |
643 | struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr; | |
e8ace1c0 MK |
644 | const int sizeof_auxv_type = TYPE_LENGTH (int_type); |
645 | const int sizeof_auxv_val = TYPE_LENGTH (ptr_type); | |
f5656ead | 646 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); |
e8ace1c0 MK |
647 | gdb_byte *ptr = *readptr; |
648 | ||
649 | if (endptr == ptr) | |
650 | return 0; | |
651 | ||
652 | if (endptr - ptr < 2 * sizeof_auxv_val) | |
653 | return -1; | |
654 | ||
655 | *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order); | |
656 | ptr += sizeof_auxv_val; /* Alignment. */ | |
657 | *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order); | |
658 | ptr += sizeof_auxv_val; | |
659 | ||
660 | *readptr = ptr; | |
661 | return 1; | |
662 | } | |
663 | ||
664 | #endif | |
665 | ||
8785ced0 MK |
666 | /* Create a prototype ptrace target. The client can override it with |
667 | local methods. */ | |
668 | ||
5bf970f9 AC |
669 | struct target_ops * |
670 | inf_ptrace_target (void) | |
671 | { | |
672 | struct target_ops *t = inf_child_target (); | |
8785ced0 | 673 | |
5bf970f9 | 674 | t->to_attach = inf_ptrace_attach; |
5bf970f9 AC |
675 | t->to_detach = inf_ptrace_detach; |
676 | t->to_resume = inf_ptrace_resume; | |
677 | t->to_wait = inf_ptrace_wait; | |
5bf970f9 | 678 | t->to_files_info = inf_ptrace_files_info; |
4b8a1a28 | 679 | t->to_kill = inf_ptrace_kill; |
5bf970f9 | 680 | t->to_create_inferior = inf_ptrace_create_inferior; |
735f54b4 MK |
681 | #ifdef PT_GET_PROCESS_STATE |
682 | t->to_follow_fork = inf_ptrace_follow_fork; | |
e85e8e5e MK |
683 | t->to_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint; |
684 | t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint; | |
e4ef629d MK |
685 | t->to_post_startup_inferior = inf_ptrace_post_startup_inferior; |
686 | t->to_post_attach = inf_ptrace_post_attach; | |
735f54b4 | 687 | #endif |
5bf970f9 | 688 | t->to_mourn_inferior = inf_ptrace_mourn_inferior; |
5bf970f9 | 689 | t->to_thread_alive = inf_ptrace_thread_alive; |
117de6a9 | 690 | t->to_pid_to_str = inf_ptrace_pid_to_str; |
bfedc46a | 691 | t->to_interrupt = inf_ptrace_interrupt; |
c7c14b96 | 692 | t->to_xfer_partial = inf_ptrace_xfer_partial; |
e8ace1c0 MK |
693 | #if defined (PT_IO) && defined (PIOD_READ_AUXV) |
694 | t->to_auxv_parse = inf_ptrace_auxv_parse; | |
695 | #endif | |
8785ced0 MK |
696 | |
697 | return t; | |
698 | } | |
699 | \f | |
700 | ||
4b8a1a28 | 701 | /* Pointer to a function that returns the offset within the user area |
8785ced0 | 702 | where a particular register is stored. */ |
7714d83a | 703 | static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int); |
8785ced0 MK |
704 | |
705 | /* Fetch register REGNUM from the inferior. */ | |
706 | ||
707 | static void | |
56be3814 | 708 | inf_ptrace_fetch_register (struct regcache *regcache, int regnum) |
8785ced0 | 709 | { |
3b3b1423 | 710 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
8785ced0 MK |
711 | CORE_ADDR addr; |
712 | size_t size; | |
713 | PTRACE_TYPE_RET *buf; | |
714 | int pid, i; | |
715 | ||
7714d83a | 716 | /* This isn't really an address, but ptrace thinks of it as one. */ |
3b3b1423 | 717 | addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0); |
8d4c1ba3 | 718 | if (addr == (CORE_ADDR)-1 |
3b3b1423 | 719 | || gdbarch_cannot_fetch_register (gdbarch, regnum)) |
10d6c8cd | 720 | { |
56be3814 | 721 | regcache_raw_supply (regcache, regnum, NULL); |
10d6c8cd DJ |
722 | return; |
723 | } | |
724 | ||
8785ced0 | 725 | /* Cater for systems like GNU/Linux, that implement threads as |
10d6c8cd | 726 | separate processes. */ |
8785ced0 MK |
727 | pid = ptid_get_lwp (inferior_ptid); |
728 | if (pid == 0) | |
729 | pid = ptid_get_pid (inferior_ptid); | |
730 | ||
3b3b1423 | 731 | size = register_size (gdbarch, regnum); |
8785ced0 | 732 | gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0); |
4397c913 | 733 | buf = (PTRACE_TYPE_RET *) alloca (size); |
8785ced0 | 734 | |
10d6c8cd | 735 | /* Read the register contents from the inferior a chunk at a time. */ |
8785ced0 MK |
736 | for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) |
737 | { | |
738 | errno = 0; | |
f7dd0ed7 | 739 | buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0); |
8785ced0 | 740 | if (errno != 0) |
4b8a1a28 | 741 | error (_("Couldn't read register %s (#%d): %s."), |
3b3b1423 | 742 | gdbarch_register_name (gdbarch, regnum), |
c9f4d572 | 743 | regnum, safe_strerror (errno)); |
8785ced0 MK |
744 | |
745 | addr += sizeof (PTRACE_TYPE_RET); | |
746 | } | |
56be3814 | 747 | regcache_raw_supply (regcache, regnum, buf); |
8785ced0 MK |
748 | } |
749 | ||
750 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this | |
751 | for all registers. */ | |
752 | ||
753 | static void | |
28439f5e PA |
754 | inf_ptrace_fetch_registers (struct target_ops *ops, |
755 | struct regcache *regcache, int regnum) | |
8785ced0 MK |
756 | { |
757 | if (regnum == -1) | |
3b3b1423 UW |
758 | for (regnum = 0; |
759 | regnum < gdbarch_num_regs (get_regcache_arch (regcache)); | |
760 | regnum++) | |
56be3814 | 761 | inf_ptrace_fetch_register (regcache, regnum); |
8785ced0 | 762 | else |
56be3814 | 763 | inf_ptrace_fetch_register (regcache, regnum); |
8785ced0 MK |
764 | } |
765 | ||
766 | /* Store register REGNUM into the inferior. */ | |
767 | ||
768 | static void | |
56be3814 | 769 | inf_ptrace_store_register (const struct regcache *regcache, int regnum) |
8785ced0 | 770 | { |
3b3b1423 | 771 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
8785ced0 MK |
772 | CORE_ADDR addr; |
773 | size_t size; | |
774 | PTRACE_TYPE_RET *buf; | |
775 | int pid, i; | |
776 | ||
7714d83a | 777 | /* This isn't really an address, but ptrace thinks of it as one. */ |
3b3b1423 | 778 | addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1); |
8d4c1ba3 | 779 | if (addr == (CORE_ADDR)-1 |
3b3b1423 | 780 | || gdbarch_cannot_store_register (gdbarch, regnum)) |
10d6c8cd DJ |
781 | return; |
782 | ||
8785ced0 | 783 | /* Cater for systems like GNU/Linux, that implement threads as |
10d6c8cd | 784 | separate processes. */ |
8785ced0 MK |
785 | pid = ptid_get_lwp (inferior_ptid); |
786 | if (pid == 0) | |
787 | pid = ptid_get_pid (inferior_ptid); | |
788 | ||
3b3b1423 | 789 | size = register_size (gdbarch, regnum); |
8785ced0 | 790 | gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0); |
4397c913 | 791 | buf = (PTRACE_TYPE_RET *) alloca (size); |
8785ced0 | 792 | |
10d6c8cd | 793 | /* Write the register contents into the inferior a chunk at a time. */ |
56be3814 | 794 | regcache_raw_collect (regcache, regnum, buf); |
8785ced0 MK |
795 | for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) |
796 | { | |
797 | errno = 0; | |
f7dd0ed7 | 798 | ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]); |
8785ced0 | 799 | if (errno != 0) |
4b8a1a28 | 800 | error (_("Couldn't write register %s (#%d): %s."), |
3b3b1423 | 801 | gdbarch_register_name (gdbarch, regnum), |
c9f4d572 | 802 | regnum, safe_strerror (errno)); |
8785ced0 MK |
803 | |
804 | addr += sizeof (PTRACE_TYPE_RET); | |
805 | } | |
806 | } | |
807 | ||
808 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do | |
809 | this for all registers. */ | |
810 | ||
2c0b251b | 811 | static void |
28439f5e PA |
812 | inf_ptrace_store_registers (struct target_ops *ops, |
813 | struct regcache *regcache, int regnum) | |
8785ced0 MK |
814 | { |
815 | if (regnum == -1) | |
3b3b1423 UW |
816 | for (regnum = 0; |
817 | regnum < gdbarch_num_regs (get_regcache_arch (regcache)); | |
818 | regnum++) | |
56be3814 | 819 | inf_ptrace_store_register (regcache, regnum); |
8785ced0 | 820 | else |
56be3814 | 821 | inf_ptrace_store_register (regcache, regnum); |
8785ced0 MK |
822 | } |
823 | ||
824 | /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be | |
825 | a function returning the offset within the user area where a | |
826 | particular register is stored. */ | |
827 | ||
828 | struct target_ops * | |
7714d83a UW |
829 | inf_ptrace_trad_target (CORE_ADDR (*register_u_offset) |
830 | (struct gdbarch *, int, int)) | |
8785ced0 MK |
831 | { |
832 | struct target_ops *t = inf_ptrace_target(); | |
833 | ||
834 | gdb_assert (register_u_offset); | |
835 | inf_ptrace_register_u_offset = register_u_offset; | |
836 | t->to_fetch_registers = inf_ptrace_fetch_registers; | |
837 | t->to_store_registers = inf_ptrace_store_registers; | |
838 | ||
5bf970f9 AC |
839 | return t; |
840 | } |