Commit | Line | Data |
---|---|---|
2c4a536d | 1 | /* Low-level child interface to ptrace. |
5bf970f9 | 2 | |
42a4f53d | 3 | Copyright (C) 1988-2019 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" |
268a13a5 | 28 | #include "gdbsupport/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" | |
0d12e84c | 36 | #include "gdbarch.h" |
2c4a536d | 37 | |
c7c14b96 MK |
38 | \f |
39 | ||
9ae79dac TT |
40 | /* A unique_ptr helper to unpush a target. */ |
41 | ||
42 | struct target_unpusher | |
43 | { | |
44 | void operator() (struct target_ops *ops) const | |
45 | { | |
46 | unpush_target (ops); | |
47 | } | |
48 | }; | |
49 | ||
50 | /* A unique_ptr that unpushes a target on destruction. */ | |
51 | ||
52 | typedef std::unique_ptr<struct target_ops, target_unpusher> target_unpush_up; | |
53 | ||
54 | \f | |
55 | ||
f6ac5f3d PA |
56 | inf_ptrace_target::~inf_ptrace_target () |
57 | {} | |
58 | ||
735f54b4 MK |
59 | #ifdef PT_GET_PROCESS_STATE |
60 | ||
d83ad864 DB |
61 | /* Target hook for follow_fork. On entry and at return inferior_ptid is |
62 | the ptid of the followed inferior. */ | |
63 | ||
f6ac5f3d PA |
64 | int |
65 | inf_ptrace_target::follow_fork (int follow_child, int detach_fork) | |
735f54b4 | 66 | { |
d83ad864 | 67 | if (!follow_child) |
735f54b4 | 68 | { |
ebf3aa72 | 69 | struct thread_info *tp = inferior_thread (); |
e99b03dc | 70 | pid_t child_pid = tp->pending_follow.value.related_pid.pid (); |
191c4426 | 71 | |
b242c3c2 PA |
72 | /* Breakpoints have already been detached from the child by |
73 | infrun.c. */ | |
735f54b4 | 74 | |
d83ad864 | 75 | if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1) |
735f54b4 MK |
76 | perror_with_name (("ptrace")); |
77 | } | |
78 | ||
79 | return 0; | |
80 | } | |
81 | ||
f6ac5f3d PA |
82 | int |
83 | inf_ptrace_target::insert_fork_catchpoint (int pid) | |
e85e8e5e MK |
84 | { |
85 | return 0; | |
86 | } | |
87 | ||
f6ac5f3d PA |
88 | int |
89 | inf_ptrace_target::remove_fork_catchpoint (int pid) | |
e85e8e5e MK |
90 | { |
91 | return 0; | |
92 | } | |
93 | ||
735f54b4 MK |
94 | #endif /* PT_GET_PROCESS_STATE */ |
95 | \f | |
96 | ||
4b8a1a28 | 97 | /* Prepare to be traced. */ |
5bf970f9 AC |
98 | |
99 | static void | |
c7c14b96 | 100 | inf_ptrace_me (void) |
5bf970f9 | 101 | { |
c7c14b96 | 102 | /* "Trace me, Dr. Memory!" */ |
0db8980c SDJ |
103 | if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0) |
104 | trace_start_error_with_name ("ptrace"); | |
5bf970f9 AC |
105 | } |
106 | ||
136d6dae VP |
107 | /* Start a new inferior Unix child process. EXEC_FILE is the file to |
108 | run, ALLARGS is a string containing the arguments to the program. | |
109 | ENV is the environment vector to pass. If FROM_TTY is non-zero, be | |
110 | chatty about it. */ | |
5bf970f9 | 111 | |
f6ac5f3d PA |
112 | void |
113 | inf_ptrace_target::create_inferior (const char *exec_file, | |
114 | const std::string &allargs, | |
115 | char **env, int from_tty) | |
5bf970f9 | 116 | { |
2090129c SDJ |
117 | pid_t pid; |
118 | ptid_t ptid; | |
136d6dae | 119 | |
c0edd9ed JK |
120 | /* Do not change either targets above or the same target if already present. |
121 | The reason is the target stack is shared across multiple inferiors. */ | |
f6ac5f3d | 122 | int ops_already_pushed = target_is_pushed (this); |
c0edd9ed | 123 | |
9ae79dac | 124 | target_unpush_up unpusher; |
c0edd9ed JK |
125 | if (! ops_already_pushed) |
126 | { | |
127 | /* Clear possible core file with its process_stratum. */ | |
f6ac5f3d PA |
128 | push_target (this); |
129 | unpusher.reset (this); | |
c0edd9ed JK |
130 | } |
131 | ||
136d6dae | 132 | pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL, |
e69860f1 | 133 | NULL, NULL, NULL); |
136d6dae | 134 | |
f2907e49 | 135 | ptid = ptid_t (pid); |
2090129c SDJ |
136 | /* We have something that executes now. We'll be running through |
137 | the shell at this point (if startup-with-shell is true), but the | |
138 | pid shouldn't change. */ | |
139 | add_thread_silent (ptid); | |
140 | ||
9ae79dac | 141 | unpusher.release (); |
5bf970f9 | 142 | |
2090129c | 143 | gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED); |
c7c14b96 MK |
144 | |
145 | /* On some targets, there must be some explicit actions taken after | |
146 | the inferior has been started up. */ | |
2090129c | 147 | target_post_startup_inferior (ptid); |
5bf970f9 AC |
148 | } |
149 | ||
e4ef629d MK |
150 | #ifdef PT_GET_PROCESS_STATE |
151 | ||
f6ac5f3d PA |
152 | void |
153 | inf_ptrace_target::post_startup_inferior (ptid_t pid) | |
e4ef629d MK |
154 | { |
155 | ptrace_event_t pe; | |
156 | ||
157 | /* Set the initial event mask. */ | |
158 | memset (&pe, 0, sizeof pe); | |
159 | pe.pe_set_event |= PTRACE_FORK; | |
e99b03dc | 160 | if (ptrace (PT_SET_EVENT_MASK, pid.pid (), |
e4ef629d MK |
161 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) |
162 | perror_with_name (("ptrace")); | |
163 | } | |
164 | ||
165 | #endif | |
166 | ||
4b8a1a28 MK |
167 | /* Clean up a rotting corpse of an inferior after it died. */ |
168 | ||
f6ac5f3d PA |
169 | void |
170 | inf_ptrace_target::mourn_inferior () | |
5bf970f9 | 171 | { |
4b8a1a28 MK |
172 | int status; |
173 | ||
174 | /* Wait just one more time to collect the inferior's exit status. | |
f010475d | 175 | Do not check whether this succeeds though, since we may be |
4b8a1a28 | 176 | dealing with a process that we attached to. Such a process will |
3d450bdd | 177 | only report its exit status to its original parent. */ |
e99b03dc | 178 | waitpid (inferior_ptid.pid (), &status, 0); |
4b8a1a28 | 179 | |
f6ac5f3d | 180 | inf_child_target::mourn_inferior (); |
5bf970f9 AC |
181 | } |
182 | ||
4b8a1a28 MK |
183 | /* Attach to the process specified by ARGS. If FROM_TTY is non-zero, |
184 | be chatty about it. */ | |
5bf970f9 | 185 | |
f6ac5f3d PA |
186 | void |
187 | inf_ptrace_target::attach (const char *args, int from_tty) | |
5bf970f9 AC |
188 | { |
189 | char *exec_file; | |
4b8a1a28 | 190 | pid_t pid; |
181e7f93 | 191 | struct inferior *inf; |
5bf970f9 | 192 | |
c0edd9ed JK |
193 | /* Do not change either targets above or the same target if already present. |
194 | The reason is the target stack is shared across multiple inferiors. */ | |
f6ac5f3d | 195 | int ops_already_pushed = target_is_pushed (this); |
c0edd9ed | 196 | |
74164c56 | 197 | pid = parse_pid_to_attach (args); |
5bf970f9 | 198 | |
f6ffd89b | 199 | if (pid == getpid ()) /* Trying to masturbate? */ |
8a3fe4f8 | 200 | error (_("I refuse to debug myself!")); |
5bf970f9 | 201 | |
9ae79dac | 202 | target_unpush_up unpusher; |
c0edd9ed JK |
203 | if (! ops_already_pushed) |
204 | { | |
205 | /* target_pid_to_str already uses the target. Also clear possible core | |
206 | file with its process_stratum. */ | |
f6ac5f3d PA |
207 | push_target (this); |
208 | unpusher.reset (this); | |
c0edd9ed JK |
209 | } |
210 | ||
5bf970f9 AC |
211 | if (from_tty) |
212 | { | |
4b8a1a28 | 213 | exec_file = get_exec_file (0); |
5bf970f9 AC |
214 | |
215 | if (exec_file) | |
a3f17187 | 216 | printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file, |
a068643d | 217 | target_pid_to_str (ptid_t (pid)).c_str ()); |
5bf970f9 | 218 | else |
a3f17187 | 219 | printf_unfiltered (_("Attaching to %s\n"), |
a068643d | 220 | target_pid_to_str (ptid_t (pid)).c_str ()); |
5bf970f9 AC |
221 | } |
222 | ||
6e1e94ea MK |
223 | #ifdef PT_ATTACH |
224 | errno = 0; | |
4b8a1a28 | 225 | ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0); |
6e1e94ea | 226 | if (errno != 0) |
e2e0b3e5 | 227 | perror_with_name (("ptrace")); |
6e1e94ea | 228 | #else |
8a3fe4f8 | 229 | error (_("This system does not support attaching to a process")); |
6e1e94ea | 230 | #endif |
5bf970f9 | 231 | |
6c95b8df PA |
232 | inf = current_inferior (); |
233 | inferior_appeared (inf, pid); | |
181e7f93 | 234 | inf->attach_flag = 1; |
f2907e49 | 235 | inferior_ptid = ptid_t (pid); |
7f9f62ba | 236 | |
af990527 PA |
237 | /* Always add a main thread. If some target extends the ptrace |
238 | target, it should decorate the ptid later with more info. */ | |
00aecdcf PA |
239 | thread_info *thr = add_thread_silent (inferior_ptid); |
240 | /* Don't consider the thread stopped until we've processed its | |
241 | initial SIGSTOP stop. */ | |
242 | set_executing (thr->ptid, true); | |
af990527 | 243 | |
9ae79dac | 244 | unpusher.release (); |
5bf970f9 AC |
245 | } |
246 | ||
e4ef629d MK |
247 | #ifdef PT_GET_PROCESS_STATE |
248 | ||
f6ac5f3d PA |
249 | void |
250 | inf_ptrace_target::post_attach (int pid) | |
e4ef629d MK |
251 | { |
252 | ptrace_event_t pe; | |
253 | ||
254 | /* Set the initial event mask. */ | |
255 | memset (&pe, 0, sizeof pe); | |
256 | pe.pe_set_event |= PTRACE_FORK; | |
257 | if (ptrace (PT_SET_EVENT_MASK, pid, | |
258 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
259 | perror_with_name (("ptrace")); | |
260 | } | |
261 | ||
262 | #endif | |
263 | ||
6bd6f3b6 | 264 | /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */ |
5bf970f9 | 265 | |
f6ac5f3d PA |
266 | void |
267 | inf_ptrace_target::detach (inferior *inf, int from_tty) | |
5bf970f9 | 268 | { |
e99b03dc | 269 | pid_t pid = inferior_ptid.pid (); |
5bf970f9 | 270 | |
0f48b757 | 271 | target_announce_detach (from_tty); |
5bf970f9 | 272 | |
6e1e94ea | 273 | #ifdef PT_DETACH |
4b8a1a28 | 274 | /* We'd better not have left any breakpoints in the program or it'll |
f010475d | 275 | die when it hits one. Also note that this may only work if we |
4b8a1a28 MK |
276 | previously attached to the inferior. It *might* work if we |
277 | started the process ourselves. */ | |
6e1e94ea | 278 | errno = 0; |
6bd6f3b6 | 279 | ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0); |
6e1e94ea | 280 | if (errno != 0) |
e2e0b3e5 | 281 | perror_with_name (("ptrace")); |
6e1e94ea | 282 | #else |
8a3fe4f8 | 283 | error (_("This system does not support detaching from a process")); |
6e1e94ea | 284 | #endif |
5bf970f9 | 285 | |
f6ac5f3d | 286 | detach_success (inf); |
ced2dffb PA |
287 | } |
288 | ||
289 | /* See inf-ptrace.h. */ | |
290 | ||
291 | void | |
f6ac5f3d | 292 | inf_ptrace_target::detach_success (inferior *inf) |
ced2dffb | 293 | { |
5bf970f9 | 294 | inferior_ptid = null_ptid; |
bc09b0c1 | 295 | detach_inferior (inf); |
7a7d3353 | 296 | |
f6ac5f3d | 297 | maybe_unpush_target (); |
5bf970f9 AC |
298 | } |
299 | ||
4b8a1a28 MK |
300 | /* Kill the inferior. */ |
301 | ||
f6ac5f3d PA |
302 | void |
303 | inf_ptrace_target::kill () | |
5bf970f9 | 304 | { |
e99b03dc | 305 | pid_t pid = inferior_ptid.pid (); |
c7c14b96 | 306 | int status; |
c7c14b96 MK |
307 | |
308 | if (pid == 0) | |
309 | return; | |
310 | ||
4b8a1a28 MK |
311 | ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0); |
312 | waitpid (pid, &status, 0); | |
313 | ||
bc1e6c81 | 314 | target_mourn_inferior (inferior_ptid); |
5bf970f9 AC |
315 | } |
316 | ||
90ad5e1d PA |
317 | /* Return which PID to pass to ptrace in order to observe/control the |
318 | tracee identified by PTID. */ | |
319 | ||
94309df7 | 320 | pid_t |
90ad5e1d PA |
321 | get_ptrace_pid (ptid_t ptid) |
322 | { | |
323 | pid_t pid; | |
324 | ||
325 | /* If we have an LWPID to work with, use it. Otherwise, we're | |
326 | dealing with a non-threaded program/target. */ | |
e38504b3 | 327 | pid = ptid.lwp (); |
90ad5e1d | 328 | if (pid == 0) |
e99b03dc | 329 | pid = ptid.pid (); |
90ad5e1d PA |
330 | return pid; |
331 | } | |
332 | ||
4b8a1a28 MK |
333 | /* Resume execution of thread PTID, or all threads if PTID is -1. If |
334 | STEP is nonzero, single-step it. If SIGNAL is nonzero, give it | |
335 | that signal. */ | |
5bf970f9 | 336 | |
f6ac5f3d PA |
337 | void |
338 | inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal) | |
5bf970f9 | 339 | { |
90ad5e1d | 340 | pid_t pid; |
a96d9b2e | 341 | int request; |
c7c14b96 | 342 | |
d7e15655 | 343 | if (minus_one_ptid == ptid) |
4b8a1a28 MK |
344 | /* Resume all threads. Traditionally ptrace() only supports |
345 | single-threaded processes, so simply resume the inferior. */ | |
e99b03dc | 346 | pid = inferior_ptid.pid (); |
90ad5e1d PA |
347 | else |
348 | pid = get_ptrace_pid (ptid); | |
c7c14b96 | 349 | |
a96d9b2e SDJ |
350 | if (catch_syscall_enabled () > 0) |
351 | request = PT_SYSCALL; | |
352 | else | |
353 | request = PT_CONTINUE; | |
354 | ||
c7c14b96 MK |
355 | if (step) |
356 | { | |
357 | /* If this system does not support PT_STEP, a higher level | |
358 | function will have called single_step() to transmute the step | |
359 | request into a continue request (by setting breakpoints on | |
360 | all possible successor instructions), so we don't have to | |
361 | worry about that here. */ | |
362 | request = PT_STEP; | |
363 | } | |
364 | ||
365 | /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from | |
366 | where it was. If GDB wanted it to start some other way, we have | |
4b8a1a28 | 367 | already written a new program counter value to the child. */ |
c7c14b96 | 368 | errno = 0; |
2ea28649 | 369 | ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal)); |
c7c14b96 MK |
370 | if (errno != 0) |
371 | perror_with_name (("ptrace")); | |
5bf970f9 AC |
372 | } |
373 | ||
4b8a1a28 MK |
374 | /* Wait for the child specified by PTID to do something. Return the |
375 | process ID of the child, or MINUS_ONE_PTID in case of error; store | |
376 | the status in *OURSTATUS. */ | |
5bf970f9 | 377 | |
f6ac5f3d PA |
378 | ptid_t |
379 | inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, | |
380 | int options) | |
5bf970f9 | 381 | { |
4b8a1a28 MK |
382 | pid_t pid; |
383 | int status, save_errno; | |
5bf970f9 | 384 | |
c7c14b96 MK |
385 | do |
386 | { | |
4b8a1a28 | 387 | set_sigint_trap (); |
5bf970f9 | 388 | |
4b8a1a28 MK |
389 | do |
390 | { | |
e99b03dc | 391 | pid = waitpid (ptid.pid (), &status, 0); |
4b8a1a28 MK |
392 | save_errno = errno; |
393 | } | |
394 | while (pid == -1 && errno == EINTR); | |
5bf970f9 | 395 | |
c7c14b96 | 396 | clear_sigint_trap (); |
5bf970f9 | 397 | |
c7c14b96 MK |
398 | if (pid == -1) |
399 | { | |
c7c14b96 | 400 | fprintf_unfiltered (gdb_stderr, |
4b8a1a28 | 401 | _("Child process unexpectedly missing: %s.\n"), |
c7c14b96 MK |
402 | safe_strerror (save_errno)); |
403 | ||
404 | /* Claim it exited with unknown signal. */ | |
405 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
a493e3e2 | 406 | ourstatus->value.sig = GDB_SIGNAL_UNKNOWN; |
fb66883a | 407 | return inferior_ptid; |
c7c14b96 MK |
408 | } |
409 | ||
4b8a1a28 | 410 | /* Ignore terminated detached child processes. */ |
e99b03dc | 411 | if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ()) |
4b8a1a28 | 412 | pid = -1; |
c7c14b96 | 413 | } |
4b8a1a28 | 414 | while (pid == -1); |
c7c14b96 | 415 | |
735f54b4 MK |
416 | #ifdef PT_GET_PROCESS_STATE |
417 | if (WIFSTOPPED (status)) | |
418 | { | |
419 | ptrace_state_t pe; | |
420 | pid_t fpid; | |
421 | ||
422 | if (ptrace (PT_GET_PROCESS_STATE, pid, | |
423 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
424 | perror_with_name (("ptrace")); | |
425 | ||
426 | switch (pe.pe_report_event) | |
427 | { | |
428 | case PTRACE_FORK: | |
429 | ourstatus->kind = TARGET_WAITKIND_FORKED; | |
f2907e49 | 430 | ourstatus->value.related_pid = ptid_t (pe.pe_other_pid); |
735f54b4 MK |
431 | |
432 | /* Make sure the other end of the fork is stopped too. */ | |
433 | fpid = waitpid (pe.pe_other_pid, &status, 0); | |
434 | if (fpid == -1) | |
435 | perror_with_name (("waitpid")); | |
436 | ||
437 | if (ptrace (PT_GET_PROCESS_STATE, fpid, | |
438 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
439 | perror_with_name (("ptrace")); | |
440 | ||
441 | gdb_assert (pe.pe_report_event == PTRACE_FORK); | |
442 | gdb_assert (pe.pe_other_pid == pid); | |
e99b03dc | 443 | if (fpid == inferior_ptid.pid ()) |
735f54b4 | 444 | { |
f2907e49 TT |
445 | ourstatus->value.related_pid = ptid_t (pe.pe_other_pid); |
446 | return ptid_t (fpid); | |
735f54b4 MK |
447 | } |
448 | ||
f2907e49 | 449 | return ptid_t (pid); |
735f54b4 MK |
450 | } |
451 | } | |
452 | #endif | |
453 | ||
c7c14b96 | 454 | store_waitstatus (ourstatus, status); |
f2907e49 | 455 | return ptid_t (pid); |
5bf970f9 AC |
456 | } |
457 | ||
87c336f6 AA |
458 | /* Transfer data via ptrace into process PID's memory from WRITEBUF, or |
459 | from process PID's memory into READBUF. Start at target address ADDR | |
460 | and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must | |
461 | be non-null. Return the number of transferred bytes. */ | |
462 | ||
463 | static ULONGEST | |
464 | inf_ptrace_peek_poke (pid_t pid, gdb_byte *readbuf, | |
465 | const gdb_byte *writebuf, | |
466 | ULONGEST addr, ULONGEST len) | |
467 | { | |
468 | ULONGEST n; | |
469 | unsigned int chunk; | |
470 | ||
471 | /* We transfer aligned words. Thus align ADDR down to a word | |
472 | boundary and determine how many bytes to skip at the | |
473 | beginning. */ | |
28f1c605 | 474 | ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1); |
87c336f6 AA |
475 | addr -= skip; |
476 | ||
477 | for (n = 0; | |
478 | n < len; | |
479 | n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0) | |
480 | { | |
481 | /* Restrict to a chunk that fits in the current word. */ | |
482 | chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n); | |
483 | ||
484 | /* Use a union for type punning. */ | |
485 | union | |
486 | { | |
487 | PTRACE_TYPE_RET word; | |
488 | gdb_byte byte[sizeof (PTRACE_TYPE_RET)]; | |
489 | } buf; | |
490 | ||
491 | /* Read the word, also when doing a partial word write. */ | |
492 | if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET)) | |
493 | { | |
494 | errno = 0; | |
495 | buf.word = ptrace (PT_READ_I, pid, | |
496 | (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0); | |
497 | if (errno != 0) | |
498 | break; | |
499 | if (readbuf != NULL) | |
500 | memcpy (readbuf + n, buf.byte + skip, chunk); | |
501 | } | |
502 | if (writebuf != NULL) | |
503 | { | |
504 | memcpy (buf.byte + skip, writebuf + n, chunk); | |
505 | errno = 0; | |
506 | ptrace (PT_WRITE_D, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr, | |
507 | buf.word); | |
508 | if (errno != 0) | |
509 | { | |
510 | /* Using the appropriate one (I or D) is necessary for | |
511 | Gould NP1, at least. */ | |
512 | errno = 0; | |
513 | ptrace (PT_WRITE_I, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr, | |
514 | buf.word); | |
515 | if (errno != 0) | |
516 | break; | |
517 | } | |
518 | } | |
519 | } | |
520 | ||
521 | return n; | |
522 | } | |
523 | ||
edcc890f | 524 | /* Implement the to_xfer_partial target_ops method. */ |
5bf970f9 | 525 | |
f6ac5f3d PA |
526 | enum target_xfer_status |
527 | inf_ptrace_target::xfer_partial (enum target_object object, | |
528 | const char *annex, gdb_byte *readbuf, | |
529 | const gdb_byte *writebuf, | |
530 | ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) | |
5bf970f9 | 531 | { |
b67aeab0 | 532 | pid_t pid = get_ptrace_pid (inferior_ptid); |
4b8a1a28 | 533 | |
5bf970f9 AC |
534 | switch (object) |
535 | { | |
536 | case TARGET_OBJECT_MEMORY: | |
f929a579 AC |
537 | #ifdef PT_IO |
538 | /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO | |
539 | request that promises to be much more efficient in reading | |
540 | and writing data in the traced process's address space. */ | |
541 | { | |
542 | struct ptrace_io_desc piod; | |
4b8a1a28 | 543 | |
f929a579 | 544 | /* NOTE: We assume that there are no distinct address spaces |
b457b3dd MK |
545 | for instruction and data. However, on OpenBSD 3.9 and |
546 | later, PIOD_WRITE_D doesn't allow changing memory that's | |
547 | mapped read-only. Since most code segments will be | |
548 | read-only, using PIOD_WRITE_D will prevent us from | |
549 | inserting breakpoints, so we use PIOD_WRITE_I instead. */ | |
550 | piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D; | |
f929a579 AC |
551 | piod.piod_addr = writebuf ? (void *) writebuf : readbuf; |
552 | piod.piod_offs = (void *) (long) offset; | |
553 | piod.piod_len = len; | |
554 | ||
555 | errno = 0; | |
4b8a1a28 | 556 | if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) |
9b409511 | 557 | { |
9b409511 | 558 | /* Return the actual number of bytes read or written. */ |
493443a4 MK |
559 | *xfered_len = piod.piod_len; |
560 | return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; | |
9b409511 | 561 | } |
f929a579 AC |
562 | /* If the PT_IO request is somehow not supported, fallback on |
563 | using PT_WRITE_D/PT_READ_D. Otherwise we will return zero | |
564 | to indicate failure. */ | |
565 | if (errno != EINVAL) | |
9b409511 | 566 | return TARGET_XFER_EOF; |
f929a579 AC |
567 | } |
568 | #endif | |
87c336f6 AA |
569 | *xfered_len = inf_ptrace_peek_poke (pid, readbuf, writebuf, |
570 | offset, len); | |
571 | return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF; | |
5bf970f9 AC |
572 | |
573 | case TARGET_OBJECT_UNWIND_TABLE: | |
2ed4b548 | 574 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
575 | |
576 | case TARGET_OBJECT_AUXV: | |
e8ace1c0 MK |
577 | #if defined (PT_IO) && defined (PIOD_READ_AUXV) |
578 | /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO | |
579 | request that allows us to read the auxilliary vector. Other | |
580 | BSD's may follow if they feel the need to support PIE. */ | |
581 | { | |
582 | struct ptrace_io_desc piod; | |
583 | ||
584 | if (writebuf) | |
2ed4b548 | 585 | return TARGET_XFER_E_IO; |
e8ace1c0 MK |
586 | piod.piod_op = PIOD_READ_AUXV; |
587 | piod.piod_addr = readbuf; | |
588 | piod.piod_offs = (void *) (long) offset; | |
589 | piod.piod_len = len; | |
590 | ||
591 | errno = 0; | |
592 | if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) | |
9b409511 | 593 | { |
9b409511 | 594 | /* Return the actual number of bytes read or written. */ |
493443a4 MK |
595 | *xfered_len = piod.piod_len; |
596 | return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; | |
9b409511 | 597 | } |
e8ace1c0 MK |
598 | } |
599 | #endif | |
2ed4b548 | 600 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
601 | |
602 | case TARGET_OBJECT_WCOOKIE: | |
2ed4b548 | 603 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
604 | |
605 | default: | |
2ed4b548 | 606 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
607 | } |
608 | } | |
609 | ||
4b8a1a28 | 610 | /* Return non-zero if the thread specified by PTID is alive. */ |
c7c14b96 | 611 | |
57810aa7 | 612 | bool |
f6ac5f3d | 613 | inf_ptrace_target::thread_alive (ptid_t ptid) |
c7c14b96 | 614 | { |
4b8a1a28 | 615 | /* ??? Is kill the right way to do this? */ |
e99b03dc | 616 | return (::kill (ptid.pid (), 0) != -1); |
c7c14b96 MK |
617 | } |
618 | ||
619 | /* Print status information about what we're accessing. */ | |
620 | ||
f6ac5f3d PA |
621 | void |
622 | inf_ptrace_target::files_info () | |
c7c14b96 | 623 | { |
181e7f93 PA |
624 | struct inferior *inf = current_inferior (); |
625 | ||
4b8a1a28 | 626 | printf_filtered (_("\tUsing the running image of %s %s.\n"), |
181e7f93 | 627 | inf->attach_flag ? "attached" : "child", |
a068643d | 628 | target_pid_to_str (inferior_ptid).c_str ()); |
5bf970f9 AC |
629 | } |
630 | ||
a068643d | 631 | std::string |
f6ac5f3d | 632 | inf_ptrace_target::pid_to_str (ptid_t ptid) |
117de6a9 PA |
633 | { |
634 | return normal_pid_to_str (ptid); | |
635 | } | |
636 | ||
e8ace1c0 MK |
637 | #if defined (PT_IO) && defined (PIOD_READ_AUXV) |
638 | ||
639 | /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR. | |
640 | Return 0 if *READPTR is already at the end of the buffer. | |
641 | Return -1 if there is insufficient buffer for a whole entry. | |
642 | Return 1 if an entry was read into *TYPEP and *VALP. */ | |
643 | ||
f6ac5f3d PA |
644 | int |
645 | inf_ptrace_target::auxv_parse (gdb_byte **readptr, gdb_byte *endptr, | |
646 | CORE_ADDR *typep, CORE_ADDR *valp) | |
e8ace1c0 | 647 | { |
f5656ead TT |
648 | struct type *int_type = builtin_type (target_gdbarch ())->builtin_int; |
649 | struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr; | |
e8ace1c0 MK |
650 | const int sizeof_auxv_type = TYPE_LENGTH (int_type); |
651 | const int sizeof_auxv_val = TYPE_LENGTH (ptr_type); | |
f5656ead | 652 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); |
e8ace1c0 MK |
653 | gdb_byte *ptr = *readptr; |
654 | ||
655 | if (endptr == ptr) | |
656 | return 0; | |
657 | ||
658 | if (endptr - ptr < 2 * sizeof_auxv_val) | |
659 | return -1; | |
660 | ||
661 | *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order); | |
662 | ptr += sizeof_auxv_val; /* Alignment. */ | |
663 | *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order); | |
664 | ptr += sizeof_auxv_val; | |
665 | ||
666 | *readptr = ptr; | |
667 | return 1; | |
668 | } | |
669 | ||
670 | #endif | |
8785ced0 | 671 | \f |