Commit | Line | Data |
---|---|---|
2c4a536d | 1 | /* Low-level child interface to ptrace. |
5bf970f9 | 2 | |
b811d2c2 | 3 | Copyright (C) 1988-2020 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 | 103 | if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0) |
50fa3001 | 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. */ | |
5b6d1e4f | 139 | add_thread_silent (this, ptid); |
2090129c | 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 | 188 | { |
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 | { | |
d9fa87f4 | 212 | const char *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, |
a068643d | 216 | target_pid_to_str (ptid_t (pid)).c_str ()); |
5bf970f9 | 217 | else |
a3f17187 | 218 | printf_unfiltered (_("Attaching to %s\n"), |
a068643d | 219 | target_pid_to_str (ptid_t (pid)).c_str ()); |
5bf970f9 AC |
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; |
f2907e49 | 234 | inferior_ptid = ptid_t (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. */ | |
5b6d1e4f | 238 | thread_info *thr = add_thread_silent (this, inferior_ptid); |
00aecdcf PA |
239 | /* Don't consider the thread stopped until we've processed its |
240 | initial SIGSTOP stop. */ | |
5b6d1e4f | 241 | set_executing (this, thr->ptid, true); |
af990527 | 242 | |
9ae79dac | 243 | unpusher.release (); |
5bf970f9 AC |
244 | } |
245 | ||
e4ef629d MK |
246 | #ifdef PT_GET_PROCESS_STATE |
247 | ||
f6ac5f3d PA |
248 | void |
249 | inf_ptrace_target::post_attach (int pid) | |
e4ef629d MK |
250 | { |
251 | ptrace_event_t pe; | |
252 | ||
253 | /* Set the initial event mask. */ | |
254 | memset (&pe, 0, sizeof pe); | |
255 | pe.pe_set_event |= PTRACE_FORK; | |
256 | if (ptrace (PT_SET_EVENT_MASK, pid, | |
257 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
258 | perror_with_name (("ptrace")); | |
259 | } | |
260 | ||
261 | #endif | |
262 | ||
6bd6f3b6 | 263 | /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */ |
5bf970f9 | 264 | |
f6ac5f3d PA |
265 | void |
266 | inf_ptrace_target::detach (inferior *inf, int from_tty) | |
5bf970f9 | 267 | { |
e99b03dc | 268 | pid_t pid = inferior_ptid.pid (); |
5bf970f9 | 269 | |
0f48b757 | 270 | target_announce_detach (from_tty); |
5bf970f9 | 271 | |
6e1e94ea | 272 | #ifdef PT_DETACH |
4b8a1a28 | 273 | /* We'd better not have left any breakpoints in the program or it'll |
f010475d | 274 | die when it hits one. Also note that this may only work if we |
4b8a1a28 MK |
275 | previously attached to the inferior. It *might* work if we |
276 | started the process ourselves. */ | |
6e1e94ea | 277 | errno = 0; |
6bd6f3b6 | 278 | ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0); |
6e1e94ea | 279 | if (errno != 0) |
e2e0b3e5 | 280 | perror_with_name (("ptrace")); |
6e1e94ea | 281 | #else |
8a3fe4f8 | 282 | error (_("This system does not support detaching from a process")); |
6e1e94ea | 283 | #endif |
5bf970f9 | 284 | |
f6ac5f3d | 285 | detach_success (inf); |
ced2dffb PA |
286 | } |
287 | ||
288 | /* See inf-ptrace.h. */ | |
289 | ||
290 | void | |
f6ac5f3d | 291 | inf_ptrace_target::detach_success (inferior *inf) |
ced2dffb | 292 | { |
5bf970f9 | 293 | inferior_ptid = null_ptid; |
bc09b0c1 | 294 | detach_inferior (inf); |
7a7d3353 | 295 | |
f6ac5f3d | 296 | maybe_unpush_target (); |
5bf970f9 AC |
297 | } |
298 | ||
4b8a1a28 MK |
299 | /* Kill the inferior. */ |
300 | ||
f6ac5f3d PA |
301 | void |
302 | inf_ptrace_target::kill () | |
5bf970f9 | 303 | { |
e99b03dc | 304 | pid_t pid = inferior_ptid.pid (); |
c7c14b96 | 305 | int status; |
c7c14b96 MK |
306 | |
307 | if (pid == 0) | |
308 | return; | |
309 | ||
4b8a1a28 MK |
310 | ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0); |
311 | waitpid (pid, &status, 0); | |
312 | ||
bc1e6c81 | 313 | target_mourn_inferior (inferior_ptid); |
5bf970f9 AC |
314 | } |
315 | ||
90ad5e1d PA |
316 | /* Return which PID to pass to ptrace in order to observe/control the |
317 | tracee identified by PTID. */ | |
318 | ||
94309df7 | 319 | pid_t |
90ad5e1d PA |
320 | get_ptrace_pid (ptid_t ptid) |
321 | { | |
322 | pid_t pid; | |
323 | ||
324 | /* If we have an LWPID to work with, use it. Otherwise, we're | |
325 | dealing with a non-threaded program/target. */ | |
e38504b3 | 326 | pid = ptid.lwp (); |
90ad5e1d | 327 | if (pid == 0) |
e99b03dc | 328 | pid = ptid.pid (); |
90ad5e1d PA |
329 | return pid; |
330 | } | |
331 | ||
4b8a1a28 MK |
332 | /* Resume execution of thread PTID, or all threads if PTID is -1. If |
333 | STEP is nonzero, single-step it. If SIGNAL is nonzero, give it | |
334 | that signal. */ | |
5bf970f9 | 335 | |
f6ac5f3d PA |
336 | void |
337 | inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal) | |
5bf970f9 | 338 | { |
90ad5e1d | 339 | pid_t pid; |
a96d9b2e | 340 | int request; |
c7c14b96 | 341 | |
d7e15655 | 342 | if (minus_one_ptid == ptid) |
4b8a1a28 MK |
343 | /* Resume all threads. Traditionally ptrace() only supports |
344 | single-threaded processes, so simply resume the inferior. */ | |
e99b03dc | 345 | pid = inferior_ptid.pid (); |
90ad5e1d PA |
346 | else |
347 | pid = get_ptrace_pid (ptid); | |
c7c14b96 | 348 | |
a96d9b2e SDJ |
349 | if (catch_syscall_enabled () > 0) |
350 | request = PT_SYSCALL; | |
351 | else | |
352 | request = PT_CONTINUE; | |
353 | ||
c7c14b96 MK |
354 | if (step) |
355 | { | |
356 | /* If this system does not support PT_STEP, a higher level | |
7da6a5b9 LM |
357 | function will have called the appropriate functions to transmute the |
358 | step request into a continue request (by setting breakpoints on | |
359 | all possible successor instructions), so we don't have to | |
360 | worry about that here. */ | |
c7c14b96 MK |
361 | request = PT_STEP; |
362 | } | |
363 | ||
364 | /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from | |
365 | where it was. If GDB wanted it to start some other way, we have | |
4b8a1a28 | 366 | already written a new program counter value to the child. */ |
c7c14b96 | 367 | errno = 0; |
2ea28649 | 368 | ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal)); |
c7c14b96 MK |
369 | if (errno != 0) |
370 | perror_with_name (("ptrace")); | |
5bf970f9 AC |
371 | } |
372 | ||
4b8a1a28 MK |
373 | /* Wait for the child specified by PTID to do something. Return the |
374 | process ID of the child, or MINUS_ONE_PTID in case of error; store | |
375 | the status in *OURSTATUS. */ | |
5bf970f9 | 376 | |
f6ac5f3d PA |
377 | ptid_t |
378 | inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, | |
379 | int options) | |
5bf970f9 | 380 | { |
4b8a1a28 MK |
381 | pid_t pid; |
382 | int status, save_errno; | |
5bf970f9 | 383 | |
c7c14b96 MK |
384 | do |
385 | { | |
4b8a1a28 | 386 | set_sigint_trap (); |
5bf970f9 | 387 | |
4b8a1a28 MK |
388 | do |
389 | { | |
e99b03dc | 390 | pid = waitpid (ptid.pid (), &status, 0); |
4b8a1a28 MK |
391 | save_errno = errno; |
392 | } | |
393 | while (pid == -1 && errno == EINTR); | |
5bf970f9 | 394 | |
c7c14b96 | 395 | clear_sigint_trap (); |
5bf970f9 | 396 | |
c7c14b96 MK |
397 | if (pid == -1) |
398 | { | |
c7c14b96 | 399 | fprintf_unfiltered (gdb_stderr, |
4b8a1a28 | 400 | _("Child process unexpectedly missing: %s.\n"), |
c7c14b96 MK |
401 | safe_strerror (save_errno)); |
402 | ||
403 | /* Claim it exited with unknown signal. */ | |
404 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
a493e3e2 | 405 | ourstatus->value.sig = GDB_SIGNAL_UNKNOWN; |
fb66883a | 406 | return inferior_ptid; |
c7c14b96 MK |
407 | } |
408 | ||
4b8a1a28 | 409 | /* Ignore terminated detached child processes. */ |
e99b03dc | 410 | if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ()) |
4b8a1a28 | 411 | pid = -1; |
c7c14b96 | 412 | } |
4b8a1a28 | 413 | while (pid == -1); |
c7c14b96 | 414 | |
735f54b4 MK |
415 | #ifdef PT_GET_PROCESS_STATE |
416 | if (WIFSTOPPED (status)) | |
417 | { | |
418 | ptrace_state_t pe; | |
419 | pid_t fpid; | |
420 | ||
421 | if (ptrace (PT_GET_PROCESS_STATE, pid, | |
422 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
423 | perror_with_name (("ptrace")); | |
424 | ||
425 | switch (pe.pe_report_event) | |
426 | { | |
427 | case PTRACE_FORK: | |
428 | ourstatus->kind = TARGET_WAITKIND_FORKED; | |
f2907e49 | 429 | ourstatus->value.related_pid = ptid_t (pe.pe_other_pid); |
735f54b4 MK |
430 | |
431 | /* Make sure the other end of the fork is stopped too. */ | |
432 | fpid = waitpid (pe.pe_other_pid, &status, 0); | |
433 | if (fpid == -1) | |
434 | perror_with_name (("waitpid")); | |
435 | ||
436 | if (ptrace (PT_GET_PROCESS_STATE, fpid, | |
437 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
438 | perror_with_name (("ptrace")); | |
439 | ||
440 | gdb_assert (pe.pe_report_event == PTRACE_FORK); | |
441 | gdb_assert (pe.pe_other_pid == pid); | |
e99b03dc | 442 | if (fpid == inferior_ptid.pid ()) |
735f54b4 | 443 | { |
f2907e49 TT |
444 | ourstatus->value.related_pid = ptid_t (pe.pe_other_pid); |
445 | return ptid_t (fpid); | |
735f54b4 MK |
446 | } |
447 | ||
f2907e49 | 448 | return ptid_t (pid); |
735f54b4 MK |
449 | } |
450 | } | |
451 | #endif | |
452 | ||
c7c14b96 | 453 | store_waitstatus (ourstatus, status); |
f2907e49 | 454 | return ptid_t (pid); |
5bf970f9 AC |
455 | } |
456 | ||
87c336f6 AA |
457 | /* Transfer data via ptrace into process PID's memory from WRITEBUF, or |
458 | from process PID's memory into READBUF. Start at target address ADDR | |
459 | and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must | |
460 | be non-null. Return the number of transferred bytes. */ | |
461 | ||
462 | static ULONGEST | |
463 | inf_ptrace_peek_poke (pid_t pid, gdb_byte *readbuf, | |
464 | const gdb_byte *writebuf, | |
465 | ULONGEST addr, ULONGEST len) | |
466 | { | |
467 | ULONGEST n; | |
468 | unsigned int chunk; | |
469 | ||
470 | /* We transfer aligned words. Thus align ADDR down to a word | |
471 | boundary and determine how many bytes to skip at the | |
472 | beginning. */ | |
28f1c605 | 473 | ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1); |
87c336f6 AA |
474 | addr -= skip; |
475 | ||
476 | for (n = 0; | |
477 | n < len; | |
478 | n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0) | |
479 | { | |
480 | /* Restrict to a chunk that fits in the current word. */ | |
481 | chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n); | |
482 | ||
483 | /* Use a union for type punning. */ | |
484 | union | |
485 | { | |
486 | PTRACE_TYPE_RET word; | |
487 | gdb_byte byte[sizeof (PTRACE_TYPE_RET)]; | |
488 | } buf; | |
489 | ||
490 | /* Read the word, also when doing a partial word write. */ | |
491 | if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET)) | |
492 | { | |
493 | errno = 0; | |
494 | buf.word = ptrace (PT_READ_I, pid, | |
495 | (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0); | |
496 | if (errno != 0) | |
497 | break; | |
498 | if (readbuf != NULL) | |
499 | memcpy (readbuf + n, buf.byte + skip, chunk); | |
500 | } | |
501 | if (writebuf != NULL) | |
502 | { | |
503 | memcpy (buf.byte + skip, writebuf + n, chunk); | |
504 | errno = 0; | |
505 | ptrace (PT_WRITE_D, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr, | |
506 | buf.word); | |
507 | if (errno != 0) | |
508 | { | |
509 | /* Using the appropriate one (I or D) is necessary for | |
510 | Gould NP1, at least. */ | |
511 | errno = 0; | |
512 | ptrace (PT_WRITE_I, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr, | |
513 | buf.word); | |
514 | if (errno != 0) | |
515 | break; | |
516 | } | |
517 | } | |
518 | } | |
519 | ||
520 | return n; | |
521 | } | |
522 | ||
edcc890f | 523 | /* Implement the to_xfer_partial target_ops method. */ |
5bf970f9 | 524 | |
f6ac5f3d PA |
525 | enum target_xfer_status |
526 | inf_ptrace_target::xfer_partial (enum target_object object, | |
527 | const char *annex, gdb_byte *readbuf, | |
528 | const gdb_byte *writebuf, | |
529 | ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) | |
5bf970f9 | 530 | { |
b67aeab0 | 531 | pid_t pid = get_ptrace_pid (inferior_ptid); |
4b8a1a28 | 532 | |
5bf970f9 AC |
533 | switch (object) |
534 | { | |
535 | case TARGET_OBJECT_MEMORY: | |
f929a579 AC |
536 | #ifdef PT_IO |
537 | /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO | |
538 | request that promises to be much more efficient in reading | |
539 | and writing data in the traced process's address space. */ | |
540 | { | |
541 | struct ptrace_io_desc piod; | |
4b8a1a28 | 542 | |
f929a579 | 543 | /* NOTE: We assume that there are no distinct address spaces |
b457b3dd MK |
544 | for instruction and data. However, on OpenBSD 3.9 and |
545 | later, PIOD_WRITE_D doesn't allow changing memory that's | |
546 | mapped read-only. Since most code segments will be | |
547 | read-only, using PIOD_WRITE_D will prevent us from | |
548 | inserting breakpoints, so we use PIOD_WRITE_I instead. */ | |
549 | piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D; | |
f929a579 AC |
550 | piod.piod_addr = writebuf ? (void *) writebuf : readbuf; |
551 | piod.piod_offs = (void *) (long) offset; | |
552 | piod.piod_len = len; | |
553 | ||
554 | errno = 0; | |
4b8a1a28 | 555 | if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) |
9b409511 | 556 | { |
9b409511 | 557 | /* Return the actual number of bytes read or written. */ |
493443a4 MK |
558 | *xfered_len = piod.piod_len; |
559 | return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; | |
9b409511 | 560 | } |
f929a579 AC |
561 | /* If the PT_IO request is somehow not supported, fallback on |
562 | using PT_WRITE_D/PT_READ_D. Otherwise we will return zero | |
563 | to indicate failure. */ | |
564 | if (errno != EINVAL) | |
9b409511 | 565 | return TARGET_XFER_EOF; |
f929a579 AC |
566 | } |
567 | #endif | |
87c336f6 AA |
568 | *xfered_len = inf_ptrace_peek_poke (pid, readbuf, writebuf, |
569 | offset, len); | |
570 | return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF; | |
5bf970f9 AC |
571 | |
572 | case TARGET_OBJECT_UNWIND_TABLE: | |
2ed4b548 | 573 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
574 | |
575 | case TARGET_OBJECT_AUXV: | |
e8ace1c0 MK |
576 | #if defined (PT_IO) && defined (PIOD_READ_AUXV) |
577 | /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO | |
578 | request that allows us to read the auxilliary vector. Other | |
579 | BSD's may follow if they feel the need to support PIE. */ | |
580 | { | |
581 | struct ptrace_io_desc piod; | |
582 | ||
583 | if (writebuf) | |
2ed4b548 | 584 | return TARGET_XFER_E_IO; |
e8ace1c0 MK |
585 | piod.piod_op = PIOD_READ_AUXV; |
586 | piod.piod_addr = readbuf; | |
587 | piod.piod_offs = (void *) (long) offset; | |
588 | piod.piod_len = len; | |
589 | ||
590 | errno = 0; | |
591 | if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) | |
9b409511 | 592 | { |
9b409511 | 593 | /* Return the actual number of bytes read or written. */ |
493443a4 MK |
594 | *xfered_len = piod.piod_len; |
595 | return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; | |
9b409511 | 596 | } |
e8ace1c0 MK |
597 | } |
598 | #endif | |
2ed4b548 | 599 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
600 | |
601 | case TARGET_OBJECT_WCOOKIE: | |
2ed4b548 | 602 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
603 | |
604 | default: | |
2ed4b548 | 605 | return TARGET_XFER_E_IO; |
5bf970f9 AC |
606 | } |
607 | } | |
608 | ||
4b8a1a28 | 609 | /* Return non-zero if the thread specified by PTID is alive. */ |
c7c14b96 | 610 | |
57810aa7 | 611 | bool |
f6ac5f3d | 612 | inf_ptrace_target::thread_alive (ptid_t ptid) |
c7c14b96 | 613 | { |
4b8a1a28 | 614 | /* ??? Is kill the right way to do this? */ |
e99b03dc | 615 | return (::kill (ptid.pid (), 0) != -1); |
c7c14b96 MK |
616 | } |
617 | ||
618 | /* Print status information about what we're accessing. */ | |
619 | ||
f6ac5f3d PA |
620 | void |
621 | inf_ptrace_target::files_info () | |
c7c14b96 | 622 | { |
181e7f93 PA |
623 | struct inferior *inf = current_inferior (); |
624 | ||
4b8a1a28 | 625 | printf_filtered (_("\tUsing the running image of %s %s.\n"), |
181e7f93 | 626 | inf->attach_flag ? "attached" : "child", |
a068643d | 627 | target_pid_to_str (inferior_ptid).c_str ()); |
5bf970f9 AC |
628 | } |
629 | ||
a068643d | 630 | std::string |
f6ac5f3d | 631 | inf_ptrace_target::pid_to_str (ptid_t ptid) |
117de6a9 PA |
632 | { |
633 | return normal_pid_to_str (ptid); | |
634 | } | |
635 | ||
e8ace1c0 MK |
636 | #if defined (PT_IO) && defined (PIOD_READ_AUXV) |
637 | ||
638 | /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR. | |
639 | Return 0 if *READPTR is already at the end of the buffer. | |
640 | Return -1 if there is insufficient buffer for a whole entry. | |
641 | Return 1 if an entry was read into *TYPEP and *VALP. */ | |
642 | ||
f6ac5f3d PA |
643 | int |
644 | inf_ptrace_target::auxv_parse (gdb_byte **readptr, gdb_byte *endptr, | |
645 | CORE_ADDR *typep, CORE_ADDR *valp) | |
e8ace1c0 | 646 | { |
f5656ead TT |
647 | struct type *int_type = builtin_type (target_gdbarch ())->builtin_int; |
648 | struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr; | |
e8ace1c0 MK |
649 | const int sizeof_auxv_type = TYPE_LENGTH (int_type); |
650 | const int sizeof_auxv_val = TYPE_LENGTH (ptr_type); | |
f5656ead | 651 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); |
e8ace1c0 MK |
652 | gdb_byte *ptr = *readptr; |
653 | ||
654 | if (endptr == ptr) | |
655 | return 0; | |
656 | ||
657 | if (endptr - ptr < 2 * sizeof_auxv_val) | |
658 | return -1; | |
659 | ||
660 | *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order); | |
661 | ptr += sizeof_auxv_val; /* Alignment. */ | |
662 | *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order); | |
663 | ptr += sizeof_auxv_val; | |
664 | ||
665 | *readptr = ptr; | |
666 | return 1; | |
667 | } | |
668 | ||
669 | #endif | |
8785ced0 | 670 | \f |