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