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