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