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