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