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