Commit | Line | Data |
---|---|---|
2c4a536d | 1 | /* Low-level child interface to ptrace. |
5bf970f9 | 2 | |
28e7fd62 | 3 | Copyright (C) 1988-2013 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" |
5bf970f9 | 27 | |
8785ced0 | 28 | #include "gdb_assert.h" |
2c4a536d MK |
29 | #include "gdb_string.h" |
30 | #include "gdb_ptrace.h" | |
34a17005 | 31 | #include "gdb_wait.h" |
5bf970f9 AC |
32 | #include <signal.h> |
33 | ||
2c0b251b | 34 | #include "inf-ptrace.h" |
2c4a536d | 35 | #include "inf-child.h" |
af990527 | 36 | #include "gdbthread.h" |
2c4a536d | 37 | |
c7c14b96 MK |
38 | \f |
39 | ||
735f54b4 MK |
40 | #ifdef PT_GET_PROCESS_STATE |
41 | ||
42 | static int | |
ee057212 | 43 | inf_ptrace_follow_fork (struct target_ops *ops, int follow_child) |
735f54b4 MK |
44 | { |
45 | pid_t pid, fpid; | |
46 | ptrace_state_t pe; | |
47 | ||
e58b0e63 | 48 | pid = ptid_get_pid (inferior_ptid); |
735f54b4 MK |
49 | |
50 | if (ptrace (PT_GET_PROCESS_STATE, pid, | |
51 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
52 | perror_with_name (("ptrace")); | |
53 | ||
54 | gdb_assert (pe.pe_report_event == PTRACE_FORK); | |
55 | fpid = pe.pe_other_pid; | |
56 | ||
57 | if (follow_child) | |
58 | { | |
191c4426 | 59 | struct inferior *parent_inf, *child_inf; |
4e1c45ea PA |
60 | struct thread_info *tp; |
61 | ||
191c4426 PA |
62 | parent_inf = find_inferior_pid (pid); |
63 | ||
64 | /* Add the child. */ | |
65 | child_inf = add_inferior (fpid); | |
66 | child_inf->attach_flag = parent_inf->attach_flag; | |
67 | copy_terminal_info (child_inf, parent_inf); | |
6479260d JB |
68 | child_inf->pspace = parent_inf->pspace; |
69 | child_inf->aspace = parent_inf->aspace; | |
191c4426 | 70 | |
4e1c45ea PA |
71 | /* Before detaching from the parent, remove all breakpoints from |
72 | it. */ | |
77435e4c | 73 | remove_breakpoints (); |
735f54b4 MK |
74 | |
75 | if (ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0) == -1) | |
76 | perror_with_name (("ptrace")); | |
4e1c45ea | 77 | |
7f9f62ba PA |
78 | /* Switch inferior_ptid out of the parent's way. */ |
79 | inferior_ptid = pid_to_ptid (fpid); | |
80 | ||
4e1c45ea | 81 | /* Delete the parent. */ |
7f9f62ba | 82 | detach_inferior (pid); |
4e1c45ea | 83 | |
e58b0e63 | 84 | add_thread_silent (inferior_ptid); |
735f54b4 MK |
85 | } |
86 | else | |
87 | { | |
b242c3c2 PA |
88 | /* Breakpoints have already been detached from the child by |
89 | infrun.c. */ | |
735f54b4 MK |
90 | |
91 | if (ptrace (PT_DETACH, fpid, (PTRACE_TYPE_ARG3)1, 0) == -1) | |
92 | perror_with_name (("ptrace")); | |
93 | } | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
98 | #endif /* PT_GET_PROCESS_STATE */ | |
99 | \f | |
100 | ||
4b8a1a28 | 101 | /* Prepare to be traced. */ |
5bf970f9 AC |
102 | |
103 | static void | |
c7c14b96 | 104 | inf_ptrace_me (void) |
5bf970f9 | 105 | { |
c7c14b96 | 106 | /* "Trace me, Dr. Memory!" */ |
4b8a1a28 | 107 | ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0); |
5bf970f9 AC |
108 | } |
109 | ||
136d6dae VP |
110 | /* Start a new inferior Unix child process. EXEC_FILE is the file to |
111 | run, ALLARGS is a string containing the arguments to the program. | |
112 | ENV is the environment vector to pass. If FROM_TTY is non-zero, be | |
113 | chatty about it. */ | |
5bf970f9 AC |
114 | |
115 | static void | |
136d6dae VP |
116 | inf_ptrace_create_inferior (struct target_ops *ops, |
117 | char *exec_file, char *allargs, char **env, | |
118 | int from_tty) | |
5bf970f9 | 119 | { |
136d6dae VP |
120 | int pid; |
121 | ||
c0edd9ed JK |
122 | /* Do not change either targets above or the same target if already present. |
123 | The reason is the target stack is shared across multiple inferiors. */ | |
124 | int ops_already_pushed = target_is_pushed (ops); | |
25f43500 | 125 | struct cleanup *back_to = make_cleanup (null_cleanup, NULL); |
c0edd9ed JK |
126 | |
127 | if (! ops_already_pushed) | |
128 | { | |
129 | /* Clear possible core file with its process_stratum. */ | |
130 | push_target (ops); | |
25f43500 | 131 | make_cleanup_unpush_target (ops); |
c0edd9ed JK |
132 | } |
133 | ||
136d6dae | 134 | pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL, |
e69860f1 | 135 | NULL, NULL, NULL); |
136d6dae | 136 | |
25f43500 | 137 | discard_cleanups (back_to); |
5bf970f9 | 138 | |
c7c14b96 MK |
139 | /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will |
140 | be 1 or 2 depending on whether we're starting without or with a | |
141 | shell. */ | |
142 | startup_inferior (START_INFERIOR_TRAPS_EXPECTED); | |
143 | ||
144 | /* On some targets, there must be some explicit actions taken after | |
145 | the inferior has been started up. */ | |
146 | target_post_startup_inferior (pid_to_ptid (pid)); | |
5bf970f9 AC |
147 | } |
148 | ||
e4ef629d MK |
149 | #ifdef PT_GET_PROCESS_STATE |
150 | ||
151 | static void | |
152 | inf_ptrace_post_startup_inferior (ptid_t pid) | |
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 | ||
c7c14b96 | 168 | static void |
136d6dae | 169 | inf_ptrace_mourn_inferior (struct target_ops *ops) |
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 | ||
c7c14b96 | 179 | generic_mourn_inferior (); |
d90e17a7 PA |
180 | |
181 | if (!have_inferiors ()) | |
182 | unpush_target (ops); | |
5bf970f9 AC |
183 | } |
184 | ||
4b8a1a28 MK |
185 | /* Attach to the process specified by ARGS. If FROM_TTY is non-zero, |
186 | be chatty about it. */ | |
5bf970f9 AC |
187 | |
188 | static void | |
136d6dae | 189 | inf_ptrace_attach (struct target_ops *ops, char *args, int from_tty) |
5bf970f9 AC |
190 | { |
191 | char *exec_file; | |
4b8a1a28 | 192 | pid_t pid; |
181e7f93 | 193 | struct inferior *inf; |
5bf970f9 | 194 | |
c0edd9ed JK |
195 | /* Do not change either targets above or the same target if already present. |
196 | The reason is the target stack is shared across multiple inferiors. */ | |
197 | int ops_already_pushed = target_is_pushed (ops); | |
25f43500 | 198 | struct cleanup *back_to = make_cleanup (null_cleanup, NULL); |
c0edd9ed | 199 | |
74164c56 | 200 | pid = parse_pid_to_attach (args); |
5bf970f9 | 201 | |
f6ffd89b | 202 | if (pid == getpid ()) /* Trying to masturbate? */ |
8a3fe4f8 | 203 | error (_("I refuse to debug myself!")); |
5bf970f9 | 204 | |
c0edd9ed JK |
205 | if (! ops_already_pushed) |
206 | { | |
207 | /* target_pid_to_str already uses the target. Also clear possible core | |
208 | file with its process_stratum. */ | |
209 | push_target (ops); | |
25f43500 | 210 | make_cleanup_unpush_target (ops); |
c0edd9ed JK |
211 | } |
212 | ||
5bf970f9 AC |
213 | if (from_tty) |
214 | { | |
4b8a1a28 | 215 | exec_file = get_exec_file (0); |
5bf970f9 AC |
216 | |
217 | if (exec_file) | |
a3f17187 | 218 | printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file, |
5bf970f9 AC |
219 | target_pid_to_str (pid_to_ptid (pid))); |
220 | else | |
a3f17187 | 221 | printf_unfiltered (_("Attaching to %s\n"), |
5bf970f9 AC |
222 | target_pid_to_str (pid_to_ptid (pid))); |
223 | ||
224 | gdb_flush (gdb_stdout); | |
225 | } | |
226 | ||
6e1e94ea MK |
227 | #ifdef PT_ATTACH |
228 | errno = 0; | |
4b8a1a28 | 229 | ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0); |
6e1e94ea | 230 | if (errno != 0) |
e2e0b3e5 | 231 | perror_with_name (("ptrace")); |
6e1e94ea | 232 | #else |
8a3fe4f8 | 233 | error (_("This system does not support attaching to a process")); |
6e1e94ea | 234 | #endif |
5bf970f9 | 235 | |
6c95b8df PA |
236 | inf = current_inferior (); |
237 | inferior_appeared (inf, pid); | |
181e7f93 | 238 | inf->attach_flag = 1; |
6c95b8df | 239 | inferior_ptid = pid_to_ptid (pid); |
7f9f62ba | 240 | |
af990527 PA |
241 | /* Always add a main thread. If some target extends the ptrace |
242 | target, it should decorate the ptid later with more info. */ | |
243 | add_thread_silent (inferior_ptid); | |
244 | ||
25f43500 | 245 | discard_cleanups (back_to); |
5bf970f9 AC |
246 | } |
247 | ||
e4ef629d MK |
248 | #ifdef PT_GET_PROCESS_STATE |
249 | ||
460fac3c | 250 | static void |
e4ef629d MK |
251 | inf_ptrace_post_attach (int pid) |
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 | ||
4b8a1a28 | 265 | /* Detach from the inferior, optionally passing it the signal |
f010475d | 266 | specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */ |
5bf970f9 AC |
267 | |
268 | static void | |
136d6dae | 269 | inf_ptrace_detach (struct target_ops *ops, char *args, int from_tty) |
5bf970f9 | 270 | { |
4b8a1a28 | 271 | pid_t pid = ptid_get_pid (inferior_ptid); |
6e1e94ea | 272 | int sig = 0; |
5bf970f9 AC |
273 | |
274 | if (from_tty) | |
275 | { | |
276 | char *exec_file = get_exec_file (0); | |
277 | if (exec_file == 0) | |
278 | exec_file = ""; | |
a3f17187 | 279 | printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file, |
5bf970f9 AC |
280 | target_pid_to_str (pid_to_ptid (pid))); |
281 | gdb_flush (gdb_stdout); | |
282 | } | |
283 | if (args) | |
6e1e94ea | 284 | sig = atoi (args); |
5bf970f9 | 285 | |
6e1e94ea | 286 | #ifdef PT_DETACH |
4b8a1a28 | 287 | /* We'd better not have left any breakpoints in the program or it'll |
f010475d | 288 | die when it hits one. Also note that this may only work if we |
4b8a1a28 MK |
289 | previously attached to the inferior. It *might* work if we |
290 | started the process ourselves. */ | |
6e1e94ea | 291 | errno = 0; |
4b8a1a28 | 292 | ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig); |
6e1e94ea | 293 | if (errno != 0) |
e2e0b3e5 | 294 | perror_with_name (("ptrace")); |
6e1e94ea | 295 | #else |
8a3fe4f8 | 296 | error (_("This system does not support detaching from a process")); |
6e1e94ea | 297 | #endif |
5bf970f9 AC |
298 | |
299 | inferior_ptid = null_ptid; | |
7f9f62ba | 300 | detach_inferior (pid); |
7a7d3353 PA |
301 | |
302 | if (!have_inferiors ()) | |
303 | unpush_target (ops); | |
5bf970f9 AC |
304 | } |
305 | ||
4b8a1a28 MK |
306 | /* Kill the inferior. */ |
307 | ||
5bf970f9 | 308 | static void |
7d85a9c0 | 309 | inf_ptrace_kill (struct target_ops *ops) |
5bf970f9 | 310 | { |
4b8a1a28 | 311 | pid_t pid = ptid_get_pid (inferior_ptid); |
c7c14b96 | 312 | int status; |
c7c14b96 MK |
313 | |
314 | if (pid == 0) | |
315 | return; | |
316 | ||
4b8a1a28 MK |
317 | ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0); |
318 | waitpid (pid, &status, 0); | |
319 | ||
c7c14b96 | 320 | target_mourn_inferior (); |
5bf970f9 AC |
321 | } |
322 | ||
4b8a1a28 | 323 | /* Stop the inferior. */ |
c7c14b96 | 324 | |
5bf970f9 | 325 | static void |
94cc34af | 326 | inf_ptrace_stop (ptid_t ptid) |
5bf970f9 | 327 | { |
4b8a1a28 MK |
328 | /* Send a SIGINT to the process group. This acts just like the user |
329 | typed a ^C on the controlling terminal. Note that using a | |
330 | negative process number in kill() is a System V-ism. The proper | |
331 | BSD interface is killpg(). However, all modern BSDs support the | |
332 | System V interface too. */ | |
7e1789f5 | 333 | kill (-inferior_process_group (), SIGINT); |
5bf970f9 AC |
334 | } |
335 | ||
4b8a1a28 MK |
336 | /* Resume execution of thread PTID, or all threads if PTID is -1. If |
337 | STEP is nonzero, single-step it. If SIGNAL is nonzero, give it | |
338 | that signal. */ | |
5bf970f9 AC |
339 | |
340 | static void | |
28439f5e | 341 | inf_ptrace_resume (struct target_ops *ops, |
2ea28649 | 342 | ptid_t ptid, int step, enum gdb_signal signal) |
5bf970f9 | 343 | { |
4b8a1a28 | 344 | pid_t pid = ptid_get_pid (ptid); |
a96d9b2e | 345 | int request; |
c7c14b96 MK |
346 | |
347 | if (pid == -1) | |
4b8a1a28 MK |
348 | /* Resume all threads. Traditionally ptrace() only supports |
349 | single-threaded processes, so simply resume the inferior. */ | |
350 | pid = ptid_get_pid (inferior_ptid); | |
c7c14b96 | 351 | |
a96d9b2e SDJ |
352 | if (catch_syscall_enabled () > 0) |
353 | request = PT_SYSCALL; | |
354 | else | |
355 | request = PT_CONTINUE; | |
356 | ||
c7c14b96 MK |
357 | if (step) |
358 | { | |
359 | /* If this system does not support PT_STEP, a higher level | |
360 | function will have called single_step() to transmute the step | |
361 | request into a continue request (by setting breakpoints on | |
362 | all possible successor instructions), so we don't have to | |
363 | worry about that here. */ | |
364 | request = PT_STEP; | |
365 | } | |
366 | ||
367 | /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from | |
368 | where it was. If GDB wanted it to start some other way, we have | |
4b8a1a28 | 369 | already written a new program counter value to the child. */ |
c7c14b96 | 370 | errno = 0; |
2ea28649 | 371 | ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal)); |
c7c14b96 MK |
372 | if (errno != 0) |
373 | perror_with_name (("ptrace")); | |
5bf970f9 AC |
374 | } |
375 | ||
4b8a1a28 MK |
376 | /* Wait for the child specified by PTID to do something. Return the |
377 | process ID of the child, or MINUS_ONE_PTID in case of error; store | |
378 | the status in *OURSTATUS. */ | |
5bf970f9 | 379 | |
c7c14b96 | 380 | static ptid_t |
117de6a9 | 381 | inf_ptrace_wait (struct target_ops *ops, |
47608cb1 | 382 | ptid_t ptid, struct target_waitstatus *ourstatus, int options) |
5bf970f9 | 383 | { |
4b8a1a28 MK |
384 | pid_t pid; |
385 | int status, save_errno; | |
5bf970f9 | 386 | |
c7c14b96 MK |
387 | do |
388 | { | |
4b8a1a28 | 389 | set_sigint_trap (); |
5bf970f9 | 390 | |
4b8a1a28 MK |
391 | do |
392 | { | |
393 | pid = waitpid (ptid_get_pid (ptid), &status, 0); | |
394 | save_errno = errno; | |
395 | } | |
396 | while (pid == -1 && errno == EINTR); | |
5bf970f9 | 397 | |
c7c14b96 | 398 | clear_sigint_trap (); |
5bf970f9 | 399 | |
c7c14b96 MK |
400 | if (pid == -1) |
401 | { | |
c7c14b96 | 402 | fprintf_unfiltered (gdb_stderr, |
4b8a1a28 | 403 | _("Child process unexpectedly missing: %s.\n"), |
c7c14b96 MK |
404 | safe_strerror (save_errno)); |
405 | ||
406 | /* Claim it exited with unknown signal. */ | |
407 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
a493e3e2 | 408 | ourstatus->value.sig = GDB_SIGNAL_UNKNOWN; |
fb66883a | 409 | return inferior_ptid; |
c7c14b96 MK |
410 | } |
411 | ||
4b8a1a28 MK |
412 | /* Ignore terminated detached child processes. */ |
413 | if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid)) | |
414 | pid = -1; | |
c7c14b96 | 415 | } |
4b8a1a28 | 416 | while (pid == -1); |
c7c14b96 | 417 | |
735f54b4 MK |
418 | #ifdef PT_GET_PROCESS_STATE |
419 | if (WIFSTOPPED (status)) | |
420 | { | |
421 | ptrace_state_t pe; | |
422 | pid_t fpid; | |
423 | ||
424 | if (ptrace (PT_GET_PROCESS_STATE, pid, | |
425 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
426 | perror_with_name (("ptrace")); | |
427 | ||
428 | switch (pe.pe_report_event) | |
429 | { | |
430 | case PTRACE_FORK: | |
431 | ourstatus->kind = TARGET_WAITKIND_FORKED; | |
3a3e9ee3 | 432 | ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid); |
735f54b4 MK |
433 | |
434 | /* Make sure the other end of the fork is stopped too. */ | |
435 | fpid = waitpid (pe.pe_other_pid, &status, 0); | |
436 | if (fpid == -1) | |
437 | perror_with_name (("waitpid")); | |
438 | ||
439 | if (ptrace (PT_GET_PROCESS_STATE, fpid, | |
440 | (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) | |
441 | perror_with_name (("ptrace")); | |
442 | ||
443 | gdb_assert (pe.pe_report_event == PTRACE_FORK); | |
444 | gdb_assert (pe.pe_other_pid == pid); | |
445 | if (fpid == ptid_get_pid (inferior_ptid)) | |
446 | { | |
3a3e9ee3 | 447 | ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid); |
735f54b4 MK |
448 | return pid_to_ptid (fpid); |
449 | } | |
450 | ||
451 | return pid_to_ptid (pid); | |
452 | } | |
453 | } | |
454 | #endif | |
455 | ||
c7c14b96 MK |
456 | store_waitstatus (ourstatus, status); |
457 | return pid_to_ptid (pid); | |
5bf970f9 AC |
458 | } |
459 | ||
4b8a1a28 MK |
460 | /* Attempt a transfer all LEN bytes starting at OFFSET between the |
461 | inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer. | |
462 | Return the number of bytes actually transferred. */ | |
5bf970f9 AC |
463 | |
464 | static LONGEST | |
465 | inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object, | |
961cb7b5 MK |
466 | const char *annex, gdb_byte *readbuf, |
467 | const gdb_byte *writebuf, | |
468 | ULONGEST offset, LONGEST len) | |
5bf970f9 | 469 | { |
4b8a1a28 MK |
470 | pid_t pid = ptid_get_pid (inferior_ptid); |
471 | ||
5bf970f9 AC |
472 | switch (object) |
473 | { | |
474 | case TARGET_OBJECT_MEMORY: | |
f929a579 AC |
475 | #ifdef PT_IO |
476 | /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO | |
477 | request that promises to be much more efficient in reading | |
478 | and writing data in the traced process's address space. */ | |
479 | { | |
480 | struct ptrace_io_desc piod; | |
4b8a1a28 | 481 | |
f929a579 | 482 | /* NOTE: We assume that there are no distinct address spaces |
b457b3dd MK |
483 | for instruction and data. However, on OpenBSD 3.9 and |
484 | later, PIOD_WRITE_D doesn't allow changing memory that's | |
485 | mapped read-only. Since most code segments will be | |
486 | read-only, using PIOD_WRITE_D will prevent us from | |
487 | inserting breakpoints, so we use PIOD_WRITE_I instead. */ | |
488 | piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D; | |
f929a579 AC |
489 | piod.piod_addr = writebuf ? (void *) writebuf : readbuf; |
490 | piod.piod_offs = (void *) (long) offset; | |
491 | piod.piod_len = len; | |
492 | ||
493 | errno = 0; | |
4b8a1a28 | 494 | if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) |
f929a579 AC |
495 | /* Return the actual number of bytes read or written. */ |
496 | return piod.piod_len; | |
497 | /* If the PT_IO request is somehow not supported, fallback on | |
498 | using PT_WRITE_D/PT_READ_D. Otherwise we will return zero | |
499 | to indicate failure. */ | |
500 | if (errno != EINVAL) | |
501 | return 0; | |
502 | } | |
503 | #endif | |
504 | { | |
505 | union | |
506 | { | |
507 | PTRACE_TYPE_RET word; | |
4b8a1a28 | 508 | gdb_byte byte[sizeof (PTRACE_TYPE_RET)]; |
f929a579 AC |
509 | } buffer; |
510 | ULONGEST rounded_offset; | |
511 | LONGEST partial_len; | |
4b8a1a28 | 512 | |
cb85a953 AC |
513 | /* Round the start offset down to the next long word |
514 | boundary. */ | |
f929a579 | 515 | rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET); |
4b8a1a28 | 516 | |
cb85a953 AC |
517 | /* Since ptrace will transfer a single word starting at that |
518 | rounded_offset the partial_len needs to be adjusted down to | |
519 | that (remember this function only does a single transfer). | |
520 | Should the required length be even less, adjust it down | |
521 | again. */ | |
522 | partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset; | |
523 | if (partial_len > len) | |
f929a579 | 524 | partial_len = len; |
4b8a1a28 | 525 | |
f929a579 AC |
526 | if (writebuf) |
527 | { | |
cb85a953 AC |
528 | /* If OFFSET:PARTIAL_LEN is smaller than |
529 | ROUNDED_OFFSET:WORDSIZE then a read/modify write will | |
530 | be needed. Read in the entire word. */ | |
f929a579 | 531 | if (rounded_offset < offset |
cb85a953 AC |
532 | || (offset + partial_len |
533 | < rounded_offset + sizeof (PTRACE_TYPE_RET))) | |
f929a579 | 534 | /* Need part of initial word -- fetch it. */ |
4b8a1a28 | 535 | buffer.word = ptrace (PT_READ_I, pid, |
f7dd0ed7 UW |
536 | (PTRACE_TYPE_ARG3)(uintptr_t) |
537 | rounded_offset, 0); | |
4b8a1a28 | 538 | |
f929a579 AC |
539 | /* Copy data to be written over corresponding part of |
540 | buffer. */ | |
f6ffd89b MK |
541 | memcpy (buffer.byte + (offset - rounded_offset), |
542 | writebuf, partial_len); | |
4b8a1a28 | 543 | |
f929a579 | 544 | errno = 0; |
4b8a1a28 | 545 | ptrace (PT_WRITE_D, pid, |
f7dd0ed7 UW |
546 | (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, |
547 | buffer.word); | |
f929a579 AC |
548 | if (errno) |
549 | { | |
550 | /* Using the appropriate one (I or D) is necessary for | |
551 | Gould NP1, at least. */ | |
552 | errno = 0; | |
4b8a1a28 | 553 | ptrace (PT_WRITE_I, pid, |
f7dd0ed7 UW |
554 | (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, |
555 | buffer.word); | |
f929a579 AC |
556 | if (errno) |
557 | return 0; | |
558 | } | |
559 | } | |
4b8a1a28 | 560 | |
f929a579 AC |
561 | if (readbuf) |
562 | { | |
563 | errno = 0; | |
4b8a1a28 | 564 | buffer.word = ptrace (PT_READ_I, pid, |
f7dd0ed7 UW |
565 | (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset, |
566 | 0); | |
f929a579 AC |
567 | if (errno) |
568 | return 0; | |
569 | /* Copy appropriate bytes out of the buffer. */ | |
570 | memcpy (readbuf, buffer.byte + (offset - rounded_offset), | |
571 | partial_len); | |
572 | } | |
4b8a1a28 | 573 | |
f929a579 AC |
574 | return partial_len; |
575 | } | |
5bf970f9 AC |
576 | |
577 | case TARGET_OBJECT_UNWIND_TABLE: | |
578 | return -1; | |
579 | ||
580 | case TARGET_OBJECT_AUXV: | |
e8ace1c0 MK |
581 | #if defined (PT_IO) && defined (PIOD_READ_AUXV) |
582 | /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO | |
583 | request that allows us to read the auxilliary vector. Other | |
584 | BSD's may follow if they feel the need to support PIE. */ | |
585 | { | |
586 | struct ptrace_io_desc piod; | |
587 | ||
588 | if (writebuf) | |
a4d0b831 | 589 | return -1; |
e8ace1c0 MK |
590 | piod.piod_op = PIOD_READ_AUXV; |
591 | piod.piod_addr = readbuf; | |
592 | piod.piod_offs = (void *) (long) offset; | |
593 | piod.piod_len = len; | |
594 | ||
595 | errno = 0; | |
596 | if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0) | |
597 | /* Return the actual number of bytes read or written. */ | |
598 | return piod.piod_len; | |
599 | } | |
600 | #endif | |
5bf970f9 AC |
601 | return -1; |
602 | ||
603 | case TARGET_OBJECT_WCOOKIE: | |
604 | return -1; | |
605 | ||
606 | default: | |
607 | return -1; | |
608 | } | |
609 | } | |
610 | ||
4b8a1a28 | 611 | /* Return non-zero if the thread specified by PTID is alive. */ |
c7c14b96 MK |
612 | |
613 | static int | |
28439f5e | 614 | inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid) |
c7c14b96 | 615 | { |
4b8a1a28 MK |
616 | /* ??? Is kill the right way to do this? */ |
617 | return (kill (ptid_get_pid (ptid), 0) != -1); | |
c7c14b96 MK |
618 | } |
619 | ||
620 | /* Print status information about what we're accessing. */ | |
621 | ||
622 | static void | |
623 | inf_ptrace_files_info (struct target_ops *ignore) | |
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 | ||
117de6a9 PA |
632 | static char * |
633 | inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid) | |
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 | ||
645 | static int | |
646 | inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr, | |
647 | gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp) | |
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 | |
672 | ||
8785ced0 MK |
673 | /* Create a prototype ptrace target. The client can override it with |
674 | local methods. */ | |
675 | ||
5bf970f9 AC |
676 | struct target_ops * |
677 | inf_ptrace_target (void) | |
678 | { | |
679 | struct target_ops *t = inf_child_target (); | |
8785ced0 | 680 | |
5bf970f9 | 681 | t->to_attach = inf_ptrace_attach; |
5bf970f9 AC |
682 | t->to_detach = inf_ptrace_detach; |
683 | t->to_resume = inf_ptrace_resume; | |
684 | t->to_wait = inf_ptrace_wait; | |
5bf970f9 | 685 | t->to_files_info = inf_ptrace_files_info; |
4b8a1a28 | 686 | t->to_kill = inf_ptrace_kill; |
5bf970f9 | 687 | t->to_create_inferior = inf_ptrace_create_inferior; |
735f54b4 MK |
688 | #ifdef PT_GET_PROCESS_STATE |
689 | t->to_follow_fork = inf_ptrace_follow_fork; | |
e4ef629d MK |
690 | t->to_post_startup_inferior = inf_ptrace_post_startup_inferior; |
691 | t->to_post_attach = inf_ptrace_post_attach; | |
735f54b4 | 692 | #endif |
5bf970f9 | 693 | t->to_mourn_inferior = inf_ptrace_mourn_inferior; |
5bf970f9 | 694 | t->to_thread_alive = inf_ptrace_thread_alive; |
117de6a9 | 695 | t->to_pid_to_str = inf_ptrace_pid_to_str; |
5bf970f9 | 696 | t->to_stop = inf_ptrace_stop; |
c7c14b96 | 697 | t->to_xfer_partial = inf_ptrace_xfer_partial; |
e8ace1c0 MK |
698 | #if defined (PT_IO) && defined (PIOD_READ_AUXV) |
699 | t->to_auxv_parse = inf_ptrace_auxv_parse; | |
700 | #endif | |
8785ced0 MK |
701 | |
702 | return t; | |
703 | } | |
704 | \f | |
705 | ||
4b8a1a28 | 706 | /* Pointer to a function that returns the offset within the user area |
8785ced0 | 707 | where a particular register is stored. */ |
7714d83a | 708 | static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int); |
8785ced0 MK |
709 | |
710 | /* Fetch register REGNUM from the inferior. */ | |
711 | ||
712 | static void | |
56be3814 | 713 | inf_ptrace_fetch_register (struct regcache *regcache, int regnum) |
8785ced0 | 714 | { |
3b3b1423 | 715 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
8785ced0 MK |
716 | CORE_ADDR addr; |
717 | size_t size; | |
718 | PTRACE_TYPE_RET *buf; | |
719 | int pid, i; | |
720 | ||
7714d83a | 721 | /* This isn't really an address, but ptrace thinks of it as one. */ |
3b3b1423 | 722 | addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0); |
8d4c1ba3 | 723 | if (addr == (CORE_ADDR)-1 |
3b3b1423 | 724 | || gdbarch_cannot_fetch_register (gdbarch, regnum)) |
10d6c8cd | 725 | { |
56be3814 | 726 | regcache_raw_supply (regcache, regnum, NULL); |
10d6c8cd DJ |
727 | return; |
728 | } | |
729 | ||
8785ced0 | 730 | /* Cater for systems like GNU/Linux, that implement threads as |
10d6c8cd | 731 | separate processes. */ |
8785ced0 MK |
732 | pid = ptid_get_lwp (inferior_ptid); |
733 | if (pid == 0) | |
734 | pid = ptid_get_pid (inferior_ptid); | |
735 | ||
3b3b1423 | 736 | size = register_size (gdbarch, regnum); |
8785ced0 MK |
737 | gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0); |
738 | buf = alloca (size); | |
739 | ||
10d6c8cd | 740 | /* Read the register contents from the inferior a chunk at a time. */ |
8785ced0 MK |
741 | for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) |
742 | { | |
743 | errno = 0; | |
f7dd0ed7 | 744 | buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0); |
8785ced0 | 745 | if (errno != 0) |
4b8a1a28 | 746 | error (_("Couldn't read register %s (#%d): %s."), |
3b3b1423 | 747 | gdbarch_register_name (gdbarch, regnum), |
c9f4d572 | 748 | regnum, safe_strerror (errno)); |
8785ced0 MK |
749 | |
750 | addr += sizeof (PTRACE_TYPE_RET); | |
751 | } | |
56be3814 | 752 | regcache_raw_supply (regcache, regnum, buf); |
8785ced0 MK |
753 | } |
754 | ||
755 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this | |
756 | for all registers. */ | |
757 | ||
758 | static void | |
28439f5e PA |
759 | inf_ptrace_fetch_registers (struct target_ops *ops, |
760 | struct regcache *regcache, int regnum) | |
8785ced0 MK |
761 | { |
762 | if (regnum == -1) | |
3b3b1423 UW |
763 | for (regnum = 0; |
764 | regnum < gdbarch_num_regs (get_regcache_arch (regcache)); | |
765 | regnum++) | |
56be3814 | 766 | inf_ptrace_fetch_register (regcache, regnum); |
8785ced0 | 767 | else |
56be3814 | 768 | inf_ptrace_fetch_register (regcache, regnum); |
8785ced0 MK |
769 | } |
770 | ||
771 | /* Store register REGNUM into the inferior. */ | |
772 | ||
773 | static void | |
56be3814 | 774 | inf_ptrace_store_register (const struct regcache *regcache, int regnum) |
8785ced0 | 775 | { |
3b3b1423 | 776 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
8785ced0 MK |
777 | CORE_ADDR addr; |
778 | size_t size; | |
779 | PTRACE_TYPE_RET *buf; | |
780 | int pid, i; | |
781 | ||
7714d83a | 782 | /* This isn't really an address, but ptrace thinks of it as one. */ |
3b3b1423 | 783 | addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1); |
8d4c1ba3 | 784 | if (addr == (CORE_ADDR)-1 |
3b3b1423 | 785 | || gdbarch_cannot_store_register (gdbarch, regnum)) |
10d6c8cd DJ |
786 | return; |
787 | ||
8785ced0 | 788 | /* Cater for systems like GNU/Linux, that implement threads as |
10d6c8cd | 789 | separate processes. */ |
8785ced0 MK |
790 | pid = ptid_get_lwp (inferior_ptid); |
791 | if (pid == 0) | |
792 | pid = ptid_get_pid (inferior_ptid); | |
793 | ||
3b3b1423 | 794 | size = register_size (gdbarch, regnum); |
8785ced0 MK |
795 | gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0); |
796 | buf = alloca (size); | |
797 | ||
10d6c8cd | 798 | /* Write the register contents into the inferior a chunk at a time. */ |
56be3814 | 799 | regcache_raw_collect (regcache, regnum, buf); |
8785ced0 MK |
800 | for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) |
801 | { | |
802 | errno = 0; | |
f7dd0ed7 | 803 | ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]); |
8785ced0 | 804 | if (errno != 0) |
4b8a1a28 | 805 | error (_("Couldn't write register %s (#%d): %s."), |
3b3b1423 | 806 | gdbarch_register_name (gdbarch, regnum), |
c9f4d572 | 807 | regnum, safe_strerror (errno)); |
8785ced0 MK |
808 | |
809 | addr += sizeof (PTRACE_TYPE_RET); | |
810 | } | |
811 | } | |
812 | ||
813 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do | |
814 | this for all registers. */ | |
815 | ||
2c0b251b | 816 | static void |
28439f5e PA |
817 | inf_ptrace_store_registers (struct target_ops *ops, |
818 | struct regcache *regcache, int regnum) | |
8785ced0 MK |
819 | { |
820 | if (regnum == -1) | |
3b3b1423 UW |
821 | for (regnum = 0; |
822 | regnum < gdbarch_num_regs (get_regcache_arch (regcache)); | |
823 | regnum++) | |
56be3814 | 824 | inf_ptrace_store_register (regcache, regnum); |
8785ced0 | 825 | else |
56be3814 | 826 | inf_ptrace_store_register (regcache, regnum); |
8785ced0 MK |
827 | } |
828 | ||
829 | /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be | |
830 | a function returning the offset within the user area where a | |
831 | particular register is stored. */ | |
832 | ||
833 | struct target_ops * | |
7714d83a UW |
834 | inf_ptrace_trad_target (CORE_ADDR (*register_u_offset) |
835 | (struct gdbarch *, int, int)) | |
8785ced0 MK |
836 | { |
837 | struct target_ops *t = inf_ptrace_target(); | |
838 | ||
839 | gdb_assert (register_u_offset); | |
840 | inf_ptrace_register_u_offset = register_u_offset; | |
841 | t->to_fetch_registers = inf_ptrace_fetch_registers; | |
842 | t->to_store_registers = inf_ptrace_store_registers; | |
843 | ||
5bf970f9 AC |
844 | return t; |
845 | } |