]>
Commit | Line | Data |
---|---|---|
da6d8c04 | 1 | /* Low level interface to ptrace, for the remote server for GDB. |
6f0f660e | 2 | Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 |
da6d8c04 DJ |
3 | Free Software Foundation, Inc. |
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 | |
9 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
18 | along with this program; if not, write to the Free Software | |
6f0f660e EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
da6d8c04 DJ |
21 | |
22 | #include "server.h" | |
58caa3dc | 23 | #include "linux-low.h" |
da6d8c04 | 24 | |
58caa3dc | 25 | #include <sys/wait.h> |
da6d8c04 DJ |
26 | #include <stdio.h> |
27 | #include <sys/param.h> | |
28 | #include <sys/dir.h> | |
29 | #include <sys/ptrace.h> | |
30 | #include <sys/user.h> | |
31 | #include <signal.h> | |
32 | #include <sys/ioctl.h> | |
33 | #include <fcntl.h> | |
d07c63e7 | 34 | #include <string.h> |
0a30fbc4 DJ |
35 | #include <stdlib.h> |
36 | #include <unistd.h> | |
fa6a77dc | 37 | #include <errno.h> |
fd500816 | 38 | #include <sys/syscall.h> |
da6d8c04 | 39 | |
0d62e5e8 DJ |
40 | /* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead, |
41 | however. This requires changing the ID in place when we go from !using_threads | |
42 | to using_threads, immediately. | |
611cb4a5 | 43 | |
0d62e5e8 DJ |
44 | ``all_processes'' is keyed by the process ID - which on Linux is (presently) |
45 | the same as the LWP ID. */ | |
46 | ||
47 | struct inferior_list all_processes; | |
48 | ||
49 | /* FIXME this is a bit of a hack, and could be removed. */ | |
50 | int stopping_threads; | |
51 | ||
52 | /* FIXME make into a target method? */ | |
53 | int using_threads; | |
54 | ||
55 | static void linux_resume_one_process (struct inferior_list_entry *entry, | |
56 | int step, int signal); | |
64386c31 | 57 | static void linux_resume (struct thread_resume *resume_info); |
0d62e5e8 DJ |
58 | static void stop_all_processes (void); |
59 | static int linux_wait_for_event (struct thread_info *child); | |
60 | ||
61 | struct pending_signals | |
62 | { | |
63 | int signal; | |
64 | struct pending_signals *prev; | |
65 | }; | |
611cb4a5 | 66 | |
d844cde6 | 67 | #define PTRACE_ARG3_TYPE long |
c6ecbae5 | 68 | #define PTRACE_XFER_TYPE long |
da6d8c04 | 69 | |
58caa3dc DJ |
70 | #ifdef HAVE_LINUX_REGSETS |
71 | static int use_regsets_p = 1; | |
72 | #endif | |
73 | ||
0d62e5e8 DJ |
74 | int debug_threads = 0; |
75 | ||
76 | #define pid_of(proc) ((proc)->head.id) | |
77 | ||
78 | /* FIXME: Delete eventually. */ | |
79 | #define inferior_pid (pid_of (get_thread_process (current_inferior))) | |
80 | ||
81 | /* This function should only be called if the process got a SIGTRAP. | |
82 | The SIGTRAP could mean several things. | |
83 | ||
84 | On i386, where decr_pc_after_break is non-zero: | |
85 | If we were single-stepping this process using PTRACE_SINGLESTEP, | |
86 | we will get only the one SIGTRAP (even if the instruction we | |
87 | stepped over was a breakpoint). The value of $eip will be the | |
88 | next instruction. | |
89 | If we continue the process using PTRACE_CONT, we will get a | |
90 | SIGTRAP when we hit a breakpoint. The value of $eip will be | |
91 | the instruction after the breakpoint (i.e. needs to be | |
92 | decremented). If we report the SIGTRAP to GDB, we must also | |
93 | report the undecremented PC. If we cancel the SIGTRAP, we | |
94 | must resume at the decremented PC. | |
95 | ||
96 | (Presumably, not yet tested) On a non-decr_pc_after_break machine | |
97 | with hardware or kernel single-step: | |
98 | If we single-step over a breakpoint instruction, our PC will | |
99 | point at the following instruction. If we continue and hit a | |
100 | breakpoint instruction, our PC will point at the breakpoint | |
101 | instruction. */ | |
102 | ||
103 | static CORE_ADDR | |
104 | get_stop_pc (void) | |
105 | { | |
106 | CORE_ADDR stop_pc = (*the_low_target.get_pc) (); | |
107 | ||
108 | if (get_thread_process (current_inferior)->stepping) | |
109 | return stop_pc; | |
110 | else | |
111 | return stop_pc - the_low_target.decr_pc_after_break; | |
112 | } | |
ce3a066d | 113 | |
0d62e5e8 | 114 | static void * |
a1928bad | 115 | add_process (unsigned long pid) |
611cb4a5 | 116 | { |
0d62e5e8 DJ |
117 | struct process_info *process; |
118 | ||
119 | process = (struct process_info *) malloc (sizeof (*process)); | |
120 | memset (process, 0, sizeof (*process)); | |
121 | ||
122 | process->head.id = pid; | |
123 | ||
124 | /* Default to tid == lwpid == pid. */ | |
125 | process->tid = pid; | |
126 | process->lwpid = pid; | |
127 | ||
128 | add_inferior_to_list (&all_processes, &process->head); | |
129 | ||
130 | return process; | |
131 | } | |
611cb4a5 | 132 | |
da6d8c04 DJ |
133 | /* Start an inferior process and returns its pid. |
134 | ALLARGS is a vector of program-name and args. */ | |
135 | ||
ce3a066d DJ |
136 | static int |
137 | linux_create_inferior (char *program, char **allargs) | |
da6d8c04 | 138 | { |
0d62e5e8 | 139 | void *new_process; |
da6d8c04 DJ |
140 | int pid; |
141 | ||
142 | pid = fork (); | |
143 | if (pid < 0) | |
144 | perror_with_name ("fork"); | |
145 | ||
146 | if (pid == 0) | |
147 | { | |
148 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
149 | ||
254787d4 | 150 | signal (__SIGRTMIN + 1, SIG_DFL); |
0d62e5e8 | 151 | |
a9fa9f7d DJ |
152 | setpgid (0, 0); |
153 | ||
da6d8c04 DJ |
154 | execv (program, allargs); |
155 | ||
156 | fprintf (stderr, "Cannot exec %s: %s.\n", program, | |
d07c63e7 | 157 | strerror (errno)); |
da6d8c04 DJ |
158 | fflush (stderr); |
159 | _exit (0177); | |
160 | } | |
161 | ||
0d62e5e8 | 162 | new_process = add_process (pid); |
a06660f7 | 163 | add_thread (pid, new_process, pid); |
611cb4a5 | 164 | |
a9fa9f7d | 165 | return pid; |
da6d8c04 DJ |
166 | } |
167 | ||
168 | /* Attach to an inferior process. */ | |
169 | ||
0d62e5e8 | 170 | void |
a1928bad | 171 | linux_attach_lwp (unsigned long pid, unsigned long tid) |
da6d8c04 | 172 | { |
0d62e5e8 | 173 | struct process_info *new_process; |
611cb4a5 | 174 | |
da6d8c04 DJ |
175 | if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0) |
176 | { | |
a1928bad | 177 | fprintf (stderr, "Cannot attach to process %ld: %s (%d)\n", pid, |
43d5792c | 178 | strerror (errno), errno); |
da6d8c04 | 179 | fflush (stderr); |
0d62e5e8 DJ |
180 | |
181 | /* If we fail to attach to an LWP, just return. */ | |
182 | if (!using_threads) | |
183 | _exit (0177); | |
184 | return; | |
da6d8c04 DJ |
185 | } |
186 | ||
0d62e5e8 | 187 | new_process = (struct process_info *) add_process (pid); |
a06660f7 | 188 | add_thread (tid, new_process, pid); |
0d62e5e8 DJ |
189 | |
190 | /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH | |
191 | brings it to a halt. We should ignore that SIGSTOP and resume the process | |
192 | (unless this is the first process, in which case the flag will be cleared | |
193 | in linux_attach). | |
194 | ||
195 | On the other hand, if we are currently trying to stop all threads, we | |
196 | should treat the new thread as if we had sent it a SIGSTOP. This works | |
197 | because we are guaranteed that add_process added us to the end of the | |
198 | list, and so the new thread has not yet reached wait_for_sigstop (but | |
199 | will). */ | |
200 | if (! stopping_threads) | |
201 | new_process->stop_expected = 1; | |
202 | } | |
203 | ||
204 | int | |
a1928bad | 205 | linux_attach (unsigned long pid) |
0d62e5e8 DJ |
206 | { |
207 | struct process_info *process; | |
208 | ||
209 | linux_attach_lwp (pid, pid); | |
210 | ||
211 | /* Don't ignore the initial SIGSTOP if we just attached to this process. */ | |
212 | process = (struct process_info *) find_inferior_id (&all_processes, pid); | |
213 | process->stop_expected = 0; | |
214 | ||
da6d8c04 DJ |
215 | return 0; |
216 | } | |
217 | ||
218 | /* Kill the inferior process. Make us have no inferior. */ | |
219 | ||
ce3a066d | 220 | static void |
0d62e5e8 | 221 | linux_kill_one_process (struct inferior_list_entry *entry) |
da6d8c04 | 222 | { |
0d62e5e8 DJ |
223 | struct thread_info *thread = (struct thread_info *) entry; |
224 | struct process_info *process = get_thread_process (thread); | |
225 | int wstat; | |
226 | ||
fd500816 DJ |
227 | /* We avoid killing the first thread here, because of a Linux kernel (at |
228 | least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before | |
229 | the children get a chance to be reaped, it will remain a zombie | |
230 | forever. */ | |
231 | if (entry == all_threads.head) | |
232 | return; | |
233 | ||
0d62e5e8 DJ |
234 | do |
235 | { | |
236 | ptrace (PTRACE_KILL, pid_of (process), 0, 0); | |
237 | ||
238 | /* Make sure it died. The loop is most likely unnecessary. */ | |
239 | wstat = linux_wait_for_event (thread); | |
240 | } while (WIFSTOPPED (wstat)); | |
da6d8c04 DJ |
241 | } |
242 | ||
0d62e5e8 DJ |
243 | static void |
244 | linux_kill (void) | |
245 | { | |
fd500816 DJ |
246 | struct thread_info *thread = (struct thread_info *) all_threads.head; |
247 | struct process_info *process = get_thread_process (thread); | |
248 | int wstat; | |
249 | ||
0d62e5e8 | 250 | for_each_inferior (&all_threads, linux_kill_one_process); |
fd500816 DJ |
251 | |
252 | /* See the comment in linux_kill_one_process. We did not kill the first | |
253 | thread in the list, so do so now. */ | |
254 | do | |
255 | { | |
256 | ptrace (PTRACE_KILL, pid_of (process), 0, 0); | |
257 | ||
258 | /* Make sure it died. The loop is most likely unnecessary. */ | |
259 | wstat = linux_wait_for_event (thread); | |
260 | } while (WIFSTOPPED (wstat)); | |
0d62e5e8 DJ |
261 | } |
262 | ||
6ad8ae5c DJ |
263 | static void |
264 | linux_detach_one_process (struct inferior_list_entry *entry) | |
265 | { | |
266 | struct thread_info *thread = (struct thread_info *) entry; | |
267 | struct process_info *process = get_thread_process (thread); | |
268 | ||
269 | ptrace (PTRACE_DETACH, pid_of (process), 0, 0); | |
270 | } | |
271 | ||
272 | static void | |
273 | linux_detach (void) | |
274 | { | |
275 | for_each_inferior (&all_threads, linux_detach_one_process); | |
276 | } | |
277 | ||
278 | /* Return nonzero if the given thread is still alive. */ | |
0d62e5e8 | 279 | static int |
a1928bad | 280 | linux_thread_alive (unsigned long tid) |
0d62e5e8 DJ |
281 | { |
282 | if (find_inferior_id (&all_threads, tid) != NULL) | |
283 | return 1; | |
284 | else | |
285 | return 0; | |
286 | } | |
287 | ||
288 | /* Return nonzero if this process stopped at a breakpoint which | |
289 | no longer appears to be inserted. Also adjust the PC | |
290 | appropriately to resume where the breakpoint used to be. */ | |
ce3a066d | 291 | static int |
0d62e5e8 | 292 | check_removed_breakpoint (struct process_info *event_child) |
da6d8c04 | 293 | { |
0d62e5e8 DJ |
294 | CORE_ADDR stop_pc; |
295 | struct thread_info *saved_inferior; | |
296 | ||
297 | if (event_child->pending_is_breakpoint == 0) | |
298 | return 0; | |
299 | ||
300 | if (debug_threads) | |
301 | fprintf (stderr, "Checking for breakpoint.\n"); | |
302 | ||
303 | saved_inferior = current_inferior; | |
304 | current_inferior = get_process_thread (event_child); | |
305 | ||
306 | stop_pc = get_stop_pc (); | |
307 | ||
308 | /* If the PC has changed since we stopped, then we shouldn't do | |
309 | anything. This happens if, for instance, GDB handled the | |
310 | decr_pc_after_break subtraction itself. */ | |
311 | if (stop_pc != event_child->pending_stop_pc) | |
312 | { | |
313 | if (debug_threads) | |
314 | fprintf (stderr, "Ignoring, PC was changed.\n"); | |
315 | ||
316 | event_child->pending_is_breakpoint = 0; | |
317 | current_inferior = saved_inferior; | |
318 | return 0; | |
319 | } | |
320 | ||
321 | /* If the breakpoint is still there, we will report hitting it. */ | |
322 | if ((*the_low_target.breakpoint_at) (stop_pc)) | |
323 | { | |
324 | if (debug_threads) | |
325 | fprintf (stderr, "Ignoring, breakpoint is still present.\n"); | |
326 | current_inferior = saved_inferior; | |
327 | return 0; | |
328 | } | |
329 | ||
330 | if (debug_threads) | |
331 | fprintf (stderr, "Removed breakpoint.\n"); | |
332 | ||
333 | /* For decr_pc_after_break targets, here is where we perform the | |
334 | decrement. We go immediately from this function to resuming, | |
335 | and can not safely call get_stop_pc () again. */ | |
336 | if (the_low_target.set_pc != NULL) | |
337 | (*the_low_target.set_pc) (stop_pc); | |
338 | ||
339 | /* We consumed the pending SIGTRAP. */ | |
5544ad89 | 340 | event_child->pending_is_breakpoint = 0; |
0d62e5e8 DJ |
341 | event_child->status_pending_p = 0; |
342 | event_child->status_pending = 0; | |
343 | ||
344 | current_inferior = saved_inferior; | |
da6d8c04 DJ |
345 | return 1; |
346 | } | |
347 | ||
0d62e5e8 DJ |
348 | /* Return 1 if this process has an interesting status pending. This function |
349 | may silently resume an inferior process. */ | |
611cb4a5 | 350 | static int |
0d62e5e8 DJ |
351 | status_pending_p (struct inferior_list_entry *entry, void *dummy) |
352 | { | |
353 | struct process_info *process = (struct process_info *) entry; | |
354 | ||
355 | if (process->status_pending_p) | |
356 | if (check_removed_breakpoint (process)) | |
357 | { | |
358 | /* This thread was stopped at a breakpoint, and the breakpoint | |
359 | is now gone. We were told to continue (or step...) all threads, | |
360 | so GDB isn't trying to single-step past this breakpoint. | |
361 | So instead of reporting the old SIGTRAP, pretend we got to | |
362 | the breakpoint just after it was removed instead of just | |
363 | before; resume the process. */ | |
364 | linux_resume_one_process (&process->head, 0, 0); | |
365 | return 0; | |
366 | } | |
367 | ||
368 | return process->status_pending_p; | |
369 | } | |
370 | ||
371 | static void | |
372 | linux_wait_for_process (struct process_info **childp, int *wstatp) | |
611cb4a5 | 373 | { |
0d62e5e8 DJ |
374 | int ret; |
375 | int to_wait_for = -1; | |
376 | ||
377 | if (*childp != NULL) | |
378 | to_wait_for = (*childp)->lwpid; | |
611cb4a5 DJ |
379 | |
380 | while (1) | |
381 | { | |
0d62e5e8 DJ |
382 | ret = waitpid (to_wait_for, wstatp, WNOHANG); |
383 | ||
384 | if (ret == -1) | |
385 | { | |
386 | if (errno != ECHILD) | |
387 | perror_with_name ("waitpid"); | |
388 | } | |
389 | else if (ret > 0) | |
390 | break; | |
391 | ||
392 | ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE); | |
393 | ||
394 | if (ret == -1) | |
395 | { | |
396 | if (errno != ECHILD) | |
397 | perror_with_name ("waitpid (WCLONE)"); | |
398 | } | |
399 | else if (ret > 0) | |
400 | break; | |
401 | ||
402 | usleep (1000); | |
403 | } | |
404 | ||
405 | if (debug_threads | |
406 | && (!WIFSTOPPED (*wstatp) | |
407 | || (WSTOPSIG (*wstatp) != 32 | |
408 | && WSTOPSIG (*wstatp) != 33))) | |
409 | fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp); | |
410 | ||
411 | if (to_wait_for == -1) | |
412 | *childp = (struct process_info *) find_inferior_id (&all_processes, ret); | |
413 | ||
414 | (*childp)->stopped = 1; | |
415 | (*childp)->pending_is_breakpoint = 0; | |
416 | ||
417 | if (debug_threads | |
418 | && WIFSTOPPED (*wstatp)) | |
419 | { | |
420 | current_inferior = (struct thread_info *) | |
421 | find_inferior_id (&all_threads, (*childp)->tid); | |
422 | /* For testing only; i386_stop_pc prints out a diagnostic. */ | |
423 | if (the_low_target.get_pc != NULL) | |
424 | get_stop_pc (); | |
425 | } | |
426 | } | |
611cb4a5 | 427 | |
0d62e5e8 DJ |
428 | static int |
429 | linux_wait_for_event (struct thread_info *child) | |
430 | { | |
431 | CORE_ADDR stop_pc; | |
432 | struct process_info *event_child; | |
433 | int wstat; | |
434 | ||
435 | /* Check for a process with a pending status. */ | |
436 | /* It is possible that the user changed the pending task's registers since | |
437 | it stopped. We correctly handle the change of PC if we hit a breakpoint | |
e5379b03 | 438 | (in check_removed_breakpoint); signals should be reported anyway. */ |
0d62e5e8 DJ |
439 | if (child == NULL) |
440 | { | |
441 | event_child = (struct process_info *) | |
442 | find_inferior (&all_processes, status_pending_p, NULL); | |
443 | if (debug_threads && event_child) | |
a1928bad | 444 | fprintf (stderr, "Got a pending child %ld\n", event_child->lwpid); |
0d62e5e8 DJ |
445 | } |
446 | else | |
447 | { | |
448 | event_child = get_thread_process (child); | |
449 | if (event_child->status_pending_p | |
450 | && check_removed_breakpoint (event_child)) | |
451 | event_child = NULL; | |
452 | } | |
611cb4a5 | 453 | |
0d62e5e8 DJ |
454 | if (event_child != NULL) |
455 | { | |
456 | if (event_child->status_pending_p) | |
611cb4a5 | 457 | { |
0d62e5e8 | 458 | if (debug_threads) |
a1928bad | 459 | fprintf (stderr, "Got an event from pending child %ld (%04x)\n", |
0d62e5e8 DJ |
460 | event_child->lwpid, event_child->status_pending); |
461 | wstat = event_child->status_pending; | |
462 | event_child->status_pending_p = 0; | |
463 | event_child->status_pending = 0; | |
464 | current_inferior = get_process_thread (event_child); | |
465 | return wstat; | |
466 | } | |
467 | } | |
468 | ||
469 | /* We only enter this loop if no process has a pending wait status. Thus | |
470 | any action taken in response to a wait status inside this loop is | |
471 | responding as soon as we detect the status, not after any pending | |
472 | events. */ | |
473 | while (1) | |
474 | { | |
475 | if (child == NULL) | |
476 | event_child = NULL; | |
477 | else | |
478 | event_child = get_thread_process (child); | |
479 | ||
480 | linux_wait_for_process (&event_child, &wstat); | |
481 | ||
482 | if (event_child == NULL) | |
483 | error ("event from unknown child"); | |
611cb4a5 | 484 | |
0d62e5e8 DJ |
485 | current_inferior = (struct thread_info *) |
486 | find_inferior_id (&all_threads, event_child->tid); | |
487 | ||
488 | if (using_threads) | |
489 | { | |
490 | /* Check for thread exit. */ | |
491 | if (! WIFSTOPPED (wstat)) | |
611cb4a5 | 492 | { |
0d62e5e8 | 493 | if (debug_threads) |
a1928bad | 494 | fprintf (stderr, "Thread %ld (LWP %ld) exiting\n", |
0d62e5e8 DJ |
495 | event_child->tid, event_child->head.id); |
496 | ||
497 | /* If the last thread is exiting, just return. */ | |
498 | if (all_threads.head == all_threads.tail) | |
499 | return wstat; | |
500 | ||
501 | dead_thread_notify (event_child->tid); | |
502 | ||
503 | remove_inferior (&all_processes, &event_child->head); | |
504 | free (event_child); | |
505 | remove_thread (current_inferior); | |
506 | current_inferior = (struct thread_info *) all_threads.head; | |
507 | ||
508 | /* If we were waiting for this particular child to do something... | |
509 | well, it did something. */ | |
510 | if (child != NULL) | |
511 | return wstat; | |
512 | ||
513 | /* Wait for a more interesting event. */ | |
611cb4a5 DJ |
514 | continue; |
515 | } | |
516 | ||
0d62e5e8 DJ |
517 | if (WIFSTOPPED (wstat) |
518 | && WSTOPSIG (wstat) == SIGSTOP | |
519 | && event_child->stop_expected) | |
520 | { | |
521 | if (debug_threads) | |
522 | fprintf (stderr, "Expected stop.\n"); | |
523 | event_child->stop_expected = 0; | |
524 | linux_resume_one_process (&event_child->head, | |
525 | event_child->stepping, 0); | |
526 | continue; | |
527 | } | |
611cb4a5 | 528 | |
0d62e5e8 DJ |
529 | /* FIXME drow/2002-06-09: Get signal numbers from the inferior's |
530 | thread library? */ | |
531 | if (WIFSTOPPED (wstat) | |
254787d4 DJ |
532 | && (WSTOPSIG (wstat) == __SIGRTMIN |
533 | || WSTOPSIG (wstat) == __SIGRTMIN + 1)) | |
611cb4a5 | 534 | { |
0d62e5e8 | 535 | if (debug_threads) |
a1928bad | 536 | fprintf (stderr, "Ignored signal %d for %ld (LWP %ld).\n", |
0d62e5e8 DJ |
537 | WSTOPSIG (wstat), event_child->tid, |
538 | event_child->head.id); | |
539 | linux_resume_one_process (&event_child->head, | |
540 | event_child->stepping, | |
541 | WSTOPSIG (wstat)); | |
542 | continue; | |
543 | } | |
544 | } | |
611cb4a5 | 545 | |
0d62e5e8 DJ |
546 | /* If this event was not handled above, and is not a SIGTRAP, report |
547 | it. */ | |
548 | if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP) | |
549 | return wstat; | |
611cb4a5 | 550 | |
0d62e5e8 DJ |
551 | /* If this target does not support breakpoints, we simply report the |
552 | SIGTRAP; it's of no concern to us. */ | |
553 | if (the_low_target.get_pc == NULL) | |
554 | return wstat; | |
555 | ||
556 | stop_pc = get_stop_pc (); | |
557 | ||
558 | /* bp_reinsert will only be set if we were single-stepping. | |
559 | Notice that we will resume the process after hitting | |
560 | a gdbserver breakpoint; single-stepping to/over one | |
561 | is not supported (yet). */ | |
562 | if (event_child->bp_reinsert != 0) | |
563 | { | |
564 | if (debug_threads) | |
565 | fprintf (stderr, "Reinserted breakpoint.\n"); | |
566 | reinsert_breakpoint (event_child->bp_reinsert); | |
567 | event_child->bp_reinsert = 0; | |
568 | ||
569 | /* Clear the single-stepping flag and SIGTRAP as we resume. */ | |
570 | linux_resume_one_process (&event_child->head, 0, 0); | |
571 | continue; | |
572 | } | |
573 | ||
574 | if (debug_threads) | |
575 | fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n"); | |
576 | ||
577 | if (check_breakpoints (stop_pc) != 0) | |
578 | { | |
579 | /* We hit one of our own breakpoints. We mark it as a pending | |
e5379b03 | 580 | breakpoint, so that check_removed_breakpoint () will do the PC |
0d62e5e8 DJ |
581 | adjustment for us at the appropriate time. */ |
582 | event_child->pending_is_breakpoint = 1; | |
583 | event_child->pending_stop_pc = stop_pc; | |
584 | ||
585 | /* Now we need to put the breakpoint back. We continue in the event | |
586 | loop instead of simply replacing the breakpoint right away, | |
587 | in order to not lose signals sent to the thread that hit the | |
588 | breakpoint. Unfortunately this increases the window where another | |
589 | thread could sneak past the removed breakpoint. For the current | |
590 | use of server-side breakpoints (thread creation) this is | |
591 | acceptable; but it needs to be considered before this breakpoint | |
592 | mechanism can be used in more general ways. For some breakpoints | |
593 | it may be necessary to stop all other threads, but that should | |
594 | be avoided where possible. | |
595 | ||
596 | If breakpoint_reinsert_addr is NULL, that means that we can | |
597 | use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint, | |
598 | mark it for reinsertion, and single-step. | |
599 | ||
600 | Otherwise, call the target function to figure out where we need | |
601 | our temporary breakpoint, create it, and continue executing this | |
602 | process. */ | |
603 | if (the_low_target.breakpoint_reinsert_addr == NULL) | |
604 | { | |
605 | event_child->bp_reinsert = stop_pc; | |
606 | uninsert_breakpoint (stop_pc); | |
607 | linux_resume_one_process (&event_child->head, 1, 0); | |
608 | } | |
609 | else | |
610 | { | |
611 | reinsert_breakpoint_by_bp | |
612 | (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ()); | |
613 | linux_resume_one_process (&event_child->head, 0, 0); | |
611cb4a5 | 614 | } |
0d62e5e8 DJ |
615 | |
616 | continue; | |
617 | } | |
618 | ||
619 | /* If we were single-stepping, we definitely want to report the | |
620 | SIGTRAP. The single-step operation has completed, so also | |
aa691b87 | 621 | clear the stepping flag; in general this does not matter, |
0d62e5e8 DJ |
622 | because the SIGTRAP will be reported to the client, which |
623 | will give us a new action for this thread, but clear it for | |
624 | consistency anyway. It's safe to clear the stepping flag | |
625 | because the only consumer of get_stop_pc () after this point | |
e5379b03 | 626 | is check_removed_breakpoint, and pending_is_breakpoint is not |
0d62e5e8 DJ |
627 | set. It might be wiser to use a step_completed flag instead. */ |
628 | if (event_child->stepping) | |
629 | { | |
630 | event_child->stepping = 0; | |
631 | return wstat; | |
632 | } | |
633 | ||
634 | /* A SIGTRAP that we can't explain. It may have been a breakpoint. | |
635 | Check if it is a breakpoint, and if so mark the process information | |
636 | accordingly. This will handle both the necessary fiddling with the | |
637 | PC on decr_pc_after_break targets and suppressing extra threads | |
638 | hitting a breakpoint if two hit it at once and then GDB removes it | |
639 | after the first is reported. Arguably it would be better to report | |
640 | multiple threads hitting breakpoints simultaneously, but the current | |
641 | remote protocol does not allow this. */ | |
642 | if ((*the_low_target.breakpoint_at) (stop_pc)) | |
643 | { | |
644 | event_child->pending_is_breakpoint = 1; | |
645 | event_child->pending_stop_pc = stop_pc; | |
611cb4a5 DJ |
646 | } |
647 | ||
648 | return wstat; | |
649 | } | |
0d62e5e8 | 650 | |
611cb4a5 DJ |
651 | /* NOTREACHED */ |
652 | return 0; | |
653 | } | |
654 | ||
0d62e5e8 | 655 | /* Wait for process, returns status. */ |
da6d8c04 | 656 | |
ce3a066d DJ |
657 | static unsigned char |
658 | linux_wait (char *status) | |
da6d8c04 | 659 | { |
e5f1222d | 660 | int w; |
0d62e5e8 DJ |
661 | struct thread_info *child = NULL; |
662 | ||
663 | retry: | |
664 | /* If we were only supposed to resume one thread, only wait for | |
665 | that thread - if it's still alive. If it died, however - which | |
666 | can happen if we're coming from the thread death case below - | |
667 | then we need to make sure we restart the other threads. We could | |
668 | pick a thread at random or restart all; restarting all is less | |
669 | arbitrary. */ | |
d592fa2f | 670 | if (cont_thread != 0 && cont_thread != -1) |
0d62e5e8 DJ |
671 | { |
672 | child = (struct thread_info *) find_inferior_id (&all_threads, | |
673 | cont_thread); | |
674 | ||
675 | /* No stepping, no signal - unless one is pending already, of course. */ | |
676 | if (child == NULL) | |
64386c31 DJ |
677 | { |
678 | struct thread_resume resume_info; | |
679 | resume_info.thread = -1; | |
680 | resume_info.step = resume_info.sig = resume_info.leave_stopped = 0; | |
681 | linux_resume (&resume_info); | |
682 | } | |
0d62e5e8 | 683 | } |
da6d8c04 DJ |
684 | |
685 | enable_async_io (); | |
62ea82f5 | 686 | unblock_async_io (); |
0d62e5e8 DJ |
687 | w = linux_wait_for_event (child); |
688 | stop_all_processes (); | |
da6d8c04 | 689 | disable_async_io (); |
da6d8c04 | 690 | |
0d62e5e8 DJ |
691 | /* If we are waiting for a particular child, and it exited, |
692 | linux_wait_for_event will return its exit status. Similarly if | |
693 | the last child exited. If this is not the last child, however, | |
694 | do not report it as exited until there is a 'thread exited' response | |
695 | available in the remote protocol. Instead, just wait for another event. | |
696 | This should be safe, because if the thread crashed we will already | |
697 | have reported the termination signal to GDB; that should stop any | |
698 | in-progress stepping operations, etc. | |
699 | ||
700 | Report the exit status of the last thread to exit. This matches | |
701 | LinuxThreads' behavior. */ | |
702 | ||
703 | if (all_threads.head == all_threads.tail) | |
da6d8c04 | 704 | { |
0d62e5e8 DJ |
705 | if (WIFEXITED (w)) |
706 | { | |
707 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
708 | *status = 'W'; | |
709 | clear_inferiors (); | |
075b3282 DJ |
710 | free (all_processes.head); |
711 | all_processes.head = all_processes.tail = NULL; | |
0d62e5e8 DJ |
712 | return ((unsigned char) WEXITSTATUS (w)); |
713 | } | |
714 | else if (!WIFSTOPPED (w)) | |
715 | { | |
716 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
0d62e5e8 | 717 | *status = 'X'; |
075b3282 DJ |
718 | clear_inferiors (); |
719 | free (all_processes.head); | |
720 | all_processes.head = all_processes.tail = NULL; | |
0d62e5e8 DJ |
721 | return ((unsigned char) WTERMSIG (w)); |
722 | } | |
da6d8c04 | 723 | } |
0d62e5e8 | 724 | else |
da6d8c04 | 725 | { |
0d62e5e8 DJ |
726 | if (!WIFSTOPPED (w)) |
727 | goto retry; | |
da6d8c04 DJ |
728 | } |
729 | ||
da6d8c04 DJ |
730 | *status = 'T'; |
731 | return ((unsigned char) WSTOPSIG (w)); | |
732 | } | |
733 | ||
fd500816 DJ |
734 | /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if |
735 | thread groups are in use, we need to use tkill. */ | |
736 | ||
737 | static int | |
a1928bad | 738 | kill_lwp (unsigned long lwpid, int signo) |
fd500816 DJ |
739 | { |
740 | static int tkill_failed; | |
741 | ||
742 | errno = 0; | |
743 | ||
744 | #ifdef SYS_tkill | |
745 | if (!tkill_failed) | |
746 | { | |
747 | int ret = syscall (SYS_tkill, lwpid, signo); | |
748 | if (errno != ENOSYS) | |
749 | return ret; | |
750 | errno = 0; | |
751 | tkill_failed = 1; | |
752 | } | |
753 | #endif | |
754 | ||
755 | return kill (lwpid, signo); | |
756 | } | |
757 | ||
0d62e5e8 DJ |
758 | static void |
759 | send_sigstop (struct inferior_list_entry *entry) | |
760 | { | |
761 | struct process_info *process = (struct process_info *) entry; | |
762 | ||
763 | if (process->stopped) | |
764 | return; | |
765 | ||
766 | /* If we already have a pending stop signal for this process, don't | |
767 | send another. */ | |
768 | if (process->stop_expected) | |
769 | { | |
770 | process->stop_expected = 0; | |
771 | return; | |
772 | } | |
773 | ||
774 | if (debug_threads) | |
a1928bad | 775 | fprintf (stderr, "Sending sigstop to process %ld\n", process->head.id); |
0d62e5e8 | 776 | |
fd500816 | 777 | kill_lwp (process->head.id, SIGSTOP); |
0d62e5e8 DJ |
778 | process->sigstop_sent = 1; |
779 | } | |
780 | ||
781 | static void | |
782 | wait_for_sigstop (struct inferior_list_entry *entry) | |
783 | { | |
784 | struct process_info *process = (struct process_info *) entry; | |
785 | struct thread_info *saved_inferior, *thread; | |
a1928bad DJ |
786 | int wstat; |
787 | unsigned long saved_tid; | |
0d62e5e8 DJ |
788 | |
789 | if (process->stopped) | |
790 | return; | |
791 | ||
792 | saved_inferior = current_inferior; | |
793 | saved_tid = ((struct inferior_list_entry *) saved_inferior)->id; | |
794 | thread = (struct thread_info *) find_inferior_id (&all_threads, | |
795 | process->tid); | |
796 | wstat = linux_wait_for_event (thread); | |
797 | ||
798 | /* If we stopped with a non-SIGSTOP signal, save it for later | |
799 | and record the pending SIGSTOP. If the process exited, just | |
800 | return. */ | |
801 | if (WIFSTOPPED (wstat) | |
802 | && WSTOPSIG (wstat) != SIGSTOP) | |
803 | { | |
804 | if (debug_threads) | |
805 | fprintf (stderr, "Stopped with non-sigstop signal\n"); | |
806 | process->status_pending_p = 1; | |
807 | process->status_pending = wstat; | |
808 | process->stop_expected = 1; | |
809 | } | |
810 | ||
811 | if (linux_thread_alive (saved_tid)) | |
812 | current_inferior = saved_inferior; | |
813 | else | |
814 | { | |
815 | if (debug_threads) | |
816 | fprintf (stderr, "Previously current thread died.\n"); | |
817 | ||
818 | /* Set a valid thread as current. */ | |
819 | set_desired_inferior (0); | |
820 | } | |
821 | } | |
822 | ||
823 | static void | |
824 | stop_all_processes (void) | |
825 | { | |
826 | stopping_threads = 1; | |
827 | for_each_inferior (&all_processes, send_sigstop); | |
828 | for_each_inferior (&all_processes, wait_for_sigstop); | |
829 | stopping_threads = 0; | |
830 | } | |
831 | ||
da6d8c04 DJ |
832 | /* Resume execution of the inferior process. |
833 | If STEP is nonzero, single-step it. | |
834 | If SIGNAL is nonzero, give it that signal. */ | |
835 | ||
ce3a066d | 836 | static void |
0d62e5e8 DJ |
837 | linux_resume_one_process (struct inferior_list_entry *entry, |
838 | int step, int signal) | |
da6d8c04 | 839 | { |
0d62e5e8 DJ |
840 | struct process_info *process = (struct process_info *) entry; |
841 | struct thread_info *saved_inferior; | |
842 | ||
843 | if (process->stopped == 0) | |
844 | return; | |
845 | ||
846 | /* If we have pending signals or status, and a new signal, enqueue the | |
847 | signal. Also enqueue the signal if we are waiting to reinsert a | |
848 | breakpoint; it will be picked up again below. */ | |
849 | if (signal != 0 | |
850 | && (process->status_pending_p || process->pending_signals != NULL | |
851 | || process->bp_reinsert != 0)) | |
852 | { | |
853 | struct pending_signals *p_sig; | |
854 | p_sig = malloc (sizeof (*p_sig)); | |
855 | p_sig->prev = process->pending_signals; | |
856 | p_sig->signal = signal; | |
857 | process->pending_signals = p_sig; | |
858 | } | |
859 | ||
e5379b03 | 860 | if (process->status_pending_p && !check_removed_breakpoint (process)) |
0d62e5e8 DJ |
861 | return; |
862 | ||
863 | saved_inferior = current_inferior; | |
864 | current_inferior = get_process_thread (process); | |
865 | ||
866 | if (debug_threads) | |
a1928bad | 867 | fprintf (stderr, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid, |
0d62e5e8 DJ |
868 | step ? "step" : "continue", signal, |
869 | process->stop_expected ? "expected" : "not expected"); | |
870 | ||
871 | /* This bit needs some thinking about. If we get a signal that | |
872 | we must report while a single-step reinsert is still pending, | |
873 | we often end up resuming the thread. It might be better to | |
874 | (ew) allow a stack of pending events; then we could be sure that | |
875 | the reinsert happened right away and not lose any signals. | |
876 | ||
877 | Making this stack would also shrink the window in which breakpoints are | |
878 | uninserted (see comment in linux_wait_for_process) but not enough for | |
879 | complete correctness, so it won't solve that problem. It may be | |
880 | worthwhile just to solve this one, however. */ | |
881 | if (process->bp_reinsert != 0) | |
882 | { | |
883 | if (debug_threads) | |
884 | fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert); | |
885 | if (step == 0) | |
886 | fprintf (stderr, "BAD - reinserting but not stepping.\n"); | |
887 | step = 1; | |
888 | ||
889 | /* Postpone any pending signal. It was enqueued above. */ | |
890 | signal = 0; | |
891 | } | |
892 | ||
893 | check_removed_breakpoint (process); | |
894 | ||
aa691b87 | 895 | if (debug_threads && the_low_target.get_pc != NULL) |
0d62e5e8 DJ |
896 | { |
897 | fprintf (stderr, " "); | |
898 | (long) (*the_low_target.get_pc) (); | |
899 | } | |
900 | ||
901 | /* If we have pending signals, consume one unless we are trying to reinsert | |
902 | a breakpoint. */ | |
903 | if (process->pending_signals != NULL && process->bp_reinsert == 0) | |
904 | { | |
905 | struct pending_signals **p_sig; | |
906 | ||
907 | p_sig = &process->pending_signals; | |
908 | while ((*p_sig)->prev != NULL) | |
909 | p_sig = &(*p_sig)->prev; | |
910 | ||
911 | signal = (*p_sig)->signal; | |
912 | free (*p_sig); | |
913 | *p_sig = NULL; | |
914 | } | |
915 | ||
916 | regcache_invalidate_one ((struct inferior_list_entry *) | |
917 | get_process_thread (process)); | |
da6d8c04 | 918 | errno = 0; |
0d62e5e8 DJ |
919 | process->stopped = 0; |
920 | process->stepping = step; | |
921 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal); | |
922 | ||
923 | current_inferior = saved_inferior; | |
da6d8c04 DJ |
924 | if (errno) |
925 | perror_with_name ("ptrace"); | |
926 | } | |
927 | ||
64386c31 DJ |
928 | static struct thread_resume *resume_ptr; |
929 | ||
930 | /* This function is called once per thread. We look up the thread | |
5544ad89 DJ |
931 | in RESUME_PTR, and mark the thread with a pointer to the appropriate |
932 | resume request. | |
933 | ||
934 | This algorithm is O(threads * resume elements), but resume elements | |
935 | is small (and will remain small at least until GDB supports thread | |
936 | suspension). */ | |
0d62e5e8 | 937 | static void |
5544ad89 | 938 | linux_set_resume_request (struct inferior_list_entry *entry) |
0d62e5e8 DJ |
939 | { |
940 | struct process_info *process; | |
64386c31 | 941 | struct thread_info *thread; |
5544ad89 | 942 | int ndx; |
64386c31 DJ |
943 | |
944 | thread = (struct thread_info *) entry; | |
945 | process = get_thread_process (thread); | |
946 | ||
947 | ndx = 0; | |
948 | while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id) | |
949 | ndx++; | |
950 | ||
5544ad89 DJ |
951 | process->resume = &resume_ptr[ndx]; |
952 | } | |
953 | ||
954 | /* This function is called once per thread. We check the thread's resume | |
955 | request, which will tell us whether to resume, step, or leave the thread | |
956 | stopped; and what signal, if any, it should be sent. For threads which | |
957 | we aren't explicitly told otherwise, we preserve the stepping flag; this | |
958 | is used for stepping over gdbserver-placed breakpoints. */ | |
959 | ||
960 | static void | |
961 | linux_continue_one_thread (struct inferior_list_entry *entry) | |
962 | { | |
963 | struct process_info *process; | |
964 | struct thread_info *thread; | |
965 | int step; | |
966 | ||
967 | thread = (struct thread_info *) entry; | |
968 | process = get_thread_process (thread); | |
969 | ||
970 | if (process->resume->leave_stopped) | |
64386c31 DJ |
971 | return; |
972 | ||
5544ad89 DJ |
973 | if (process->resume->thread == -1) |
974 | step = process->stepping || process->resume->step; | |
64386c31 | 975 | else |
5544ad89 DJ |
976 | step = process->resume->step; |
977 | ||
978 | linux_resume_one_process (&process->head, step, process->resume->sig); | |
c6ecbae5 | 979 | |
5544ad89 DJ |
980 | process->resume = NULL; |
981 | } | |
982 | ||
983 | /* This function is called once per thread. We check the thread's resume | |
984 | request, which will tell us whether to resume, step, or leave the thread | |
985 | stopped; and what signal, if any, it should be sent. We queue any needed | |
986 | signals, since we won't actually resume. We already have a pending event | |
987 | to report, so we don't need to preserve any step requests; they should | |
988 | be re-issued if necessary. */ | |
989 | ||
990 | static void | |
991 | linux_queue_one_thread (struct inferior_list_entry *entry) | |
992 | { | |
993 | struct process_info *process; | |
994 | struct thread_info *thread; | |
995 | ||
996 | thread = (struct thread_info *) entry; | |
997 | process = get_thread_process (thread); | |
998 | ||
999 | if (process->resume->leave_stopped) | |
1000 | return; | |
1001 | ||
1002 | /* If we have a new signal, enqueue the signal. */ | |
1003 | if (process->resume->sig != 0) | |
1004 | { | |
1005 | struct pending_signals *p_sig; | |
1006 | p_sig = malloc (sizeof (*p_sig)); | |
1007 | p_sig->prev = process->pending_signals; | |
1008 | p_sig->signal = process->resume->sig; | |
1009 | process->pending_signals = p_sig; | |
1010 | } | |
1011 | ||
1012 | process->resume = NULL; | |
1013 | } | |
1014 | ||
1015 | /* Set DUMMY if this process has an interesting status pending. */ | |
1016 | static int | |
1017 | resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p) | |
1018 | { | |
1019 | struct process_info *process = (struct process_info *) entry; | |
1020 | ||
1021 | /* Processes which will not be resumed are not interesting, because | |
1022 | we might not wait for them next time through linux_wait. */ | |
1023 | if (process->resume->leave_stopped) | |
1024 | return 0; | |
1025 | ||
1026 | /* If this thread has a removed breakpoint, we won't have any | |
1027 | events to report later, so check now. check_removed_breakpoint | |
1028 | may clear status_pending_p. We avoid calling check_removed_breakpoint | |
1029 | for any thread that we are not otherwise going to resume - this | |
1030 | lets us preserve stopped status when two threads hit a breakpoint. | |
1031 | GDB removes the breakpoint to single-step a particular thread | |
1032 | past it, then re-inserts it and resumes all threads. We want | |
1033 | to report the second thread without resuming it in the interim. */ | |
1034 | if (process->status_pending_p) | |
1035 | check_removed_breakpoint (process); | |
1036 | ||
1037 | if (process->status_pending_p) | |
1038 | * (int *) flag_p = 1; | |
1039 | ||
1040 | return 0; | |
0d62e5e8 DJ |
1041 | } |
1042 | ||
1043 | static void | |
64386c31 | 1044 | linux_resume (struct thread_resume *resume_info) |
0d62e5e8 | 1045 | { |
5544ad89 | 1046 | int pending_flag; |
c6ecbae5 | 1047 | |
5544ad89 | 1048 | /* Yes, the use of a global here is rather ugly. */ |
64386c31 | 1049 | resume_ptr = resume_info; |
5544ad89 DJ |
1050 | |
1051 | for_each_inferior (&all_threads, linux_set_resume_request); | |
1052 | ||
1053 | /* If there is a thread which would otherwise be resumed, which | |
1054 | has a pending status, then don't resume any threads - we can just | |
1055 | report the pending status. Make sure to queue any signals | |
1056 | that would otherwise be sent. */ | |
1057 | pending_flag = 0; | |
1058 | find_inferior (&all_processes, resume_status_pending_p, &pending_flag); | |
1059 | ||
1060 | if (debug_threads) | |
1061 | { | |
1062 | if (pending_flag) | |
1063 | fprintf (stderr, "Not resuming, pending status\n"); | |
1064 | else | |
1065 | fprintf (stderr, "Resuming, no pending status\n"); | |
1066 | } | |
1067 | ||
1068 | if (pending_flag) | |
1069 | for_each_inferior (&all_threads, linux_queue_one_thread); | |
1070 | else | |
62ea82f5 DJ |
1071 | { |
1072 | block_async_io (); | |
1073 | enable_async_io (); | |
1074 | for_each_inferior (&all_threads, linux_continue_one_thread); | |
1075 | } | |
0d62e5e8 DJ |
1076 | } |
1077 | ||
1078 | #ifdef HAVE_LINUX_USRREGS | |
da6d8c04 DJ |
1079 | |
1080 | int | |
0a30fbc4 | 1081 | register_addr (int regnum) |
da6d8c04 DJ |
1082 | { |
1083 | int addr; | |
1084 | ||
2ec06d2e | 1085 | if (regnum < 0 || regnum >= the_low_target.num_regs) |
da6d8c04 DJ |
1086 | error ("Invalid register number %d.", regnum); |
1087 | ||
2ec06d2e | 1088 | addr = the_low_target.regmap[regnum]; |
da6d8c04 DJ |
1089 | |
1090 | return addr; | |
1091 | } | |
1092 | ||
58caa3dc | 1093 | /* Fetch one register. */ |
da6d8c04 DJ |
1094 | static void |
1095 | fetch_register (int regno) | |
1096 | { | |
1097 | CORE_ADDR regaddr; | |
48d93c75 | 1098 | int i, size; |
0d62e5e8 | 1099 | char *buf; |
da6d8c04 | 1100 | |
2ec06d2e | 1101 | if (regno >= the_low_target.num_regs) |
0a30fbc4 | 1102 | return; |
2ec06d2e | 1103 | if ((*the_low_target.cannot_fetch_register) (regno)) |
0a30fbc4 | 1104 | return; |
da6d8c04 | 1105 | |
0a30fbc4 DJ |
1106 | regaddr = register_addr (regno); |
1107 | if (regaddr == -1) | |
1108 | return; | |
48d93c75 UW |
1109 | size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1) |
1110 | & - sizeof (PTRACE_XFER_TYPE); | |
1111 | buf = alloca (size); | |
1112 | for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE)) | |
da6d8c04 DJ |
1113 | { |
1114 | errno = 0; | |
0d62e5e8 | 1115 | *(PTRACE_XFER_TYPE *) (buf + i) = |
da6d8c04 DJ |
1116 | ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0); |
1117 | regaddr += sizeof (PTRACE_XFER_TYPE); | |
1118 | if (errno != 0) | |
1119 | { | |
1120 | /* Warning, not error, in case we are attached; sometimes the | |
1121 | kernel doesn't let us at the registers. */ | |
1122 | char *err = strerror (errno); | |
1123 | char *msg = alloca (strlen (err) + 128); | |
1124 | sprintf (msg, "reading register %d: %s", regno, err); | |
1125 | error (msg); | |
1126 | goto error_exit; | |
1127 | } | |
1128 | } | |
5a1f5858 DJ |
1129 | if (the_low_target.left_pad_xfer |
1130 | && register_size (regno) < sizeof (PTRACE_XFER_TYPE)) | |
1131 | supply_register (regno, (buf + sizeof (PTRACE_XFER_TYPE) | |
1132 | - register_size (regno))); | |
1133 | else | |
1134 | supply_register (regno, buf); | |
0d62e5e8 | 1135 | |
da6d8c04 DJ |
1136 | error_exit:; |
1137 | } | |
1138 | ||
1139 | /* Fetch all registers, or just one, from the child process. */ | |
58caa3dc DJ |
1140 | static void |
1141 | usr_fetch_inferior_registers (int regno) | |
da6d8c04 DJ |
1142 | { |
1143 | if (regno == -1 || regno == 0) | |
2ec06d2e | 1144 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
da6d8c04 DJ |
1145 | fetch_register (regno); |
1146 | else | |
1147 | fetch_register (regno); | |
1148 | } | |
1149 | ||
1150 | /* Store our register values back into the inferior. | |
1151 | If REGNO is -1, do this for all registers. | |
1152 | Otherwise, REGNO specifies which register (so we can save time). */ | |
58caa3dc DJ |
1153 | static void |
1154 | usr_store_inferior_registers (int regno) | |
da6d8c04 DJ |
1155 | { |
1156 | CORE_ADDR regaddr; | |
48d93c75 | 1157 | int i, size; |
0d62e5e8 | 1158 | char *buf; |
da6d8c04 DJ |
1159 | |
1160 | if (regno >= 0) | |
1161 | { | |
2ec06d2e | 1162 | if (regno >= the_low_target.num_regs) |
0a30fbc4 DJ |
1163 | return; |
1164 | ||
bc1e36ca | 1165 | if ((*the_low_target.cannot_store_register) (regno) == 1) |
0a30fbc4 DJ |
1166 | return; |
1167 | ||
1168 | regaddr = register_addr (regno); | |
1169 | if (regaddr == -1) | |
da6d8c04 | 1170 | return; |
da6d8c04 | 1171 | errno = 0; |
48d93c75 UW |
1172 | size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1) |
1173 | & - sizeof (PTRACE_XFER_TYPE); | |
1174 | buf = alloca (size); | |
1175 | memset (buf, 0, size); | |
5a1f5858 DJ |
1176 | if (the_low_target.left_pad_xfer |
1177 | && register_size (regno) < sizeof (PTRACE_XFER_TYPE)) | |
1178 | collect_register (regno, (buf + sizeof (PTRACE_XFER_TYPE) | |
1179 | - register_size (regno))); | |
1180 | else | |
1181 | collect_register (regno, buf); | |
48d93c75 | 1182 | for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE)) |
da6d8c04 | 1183 | { |
0a30fbc4 DJ |
1184 | errno = 0; |
1185 | ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
2ff29de4 | 1186 | *(PTRACE_XFER_TYPE *) (buf + i)); |
da6d8c04 DJ |
1187 | if (errno != 0) |
1188 | { | |
bc1e36ca DJ |
1189 | if ((*the_low_target.cannot_store_register) (regno) == 0) |
1190 | { | |
1191 | char *err = strerror (errno); | |
1192 | char *msg = alloca (strlen (err) + 128); | |
1193 | sprintf (msg, "writing register %d: %s", | |
1194 | regno, err); | |
1195 | error (msg); | |
1196 | return; | |
1197 | } | |
da6d8c04 | 1198 | } |
2ff29de4 | 1199 | regaddr += sizeof (PTRACE_XFER_TYPE); |
da6d8c04 | 1200 | } |
da6d8c04 DJ |
1201 | } |
1202 | else | |
2ec06d2e | 1203 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
0d62e5e8 | 1204 | usr_store_inferior_registers (regno); |
da6d8c04 | 1205 | } |
58caa3dc DJ |
1206 | #endif /* HAVE_LINUX_USRREGS */ |
1207 | ||
1208 | ||
1209 | ||
1210 | #ifdef HAVE_LINUX_REGSETS | |
1211 | ||
1212 | static int | |
0d62e5e8 | 1213 | regsets_fetch_inferior_registers () |
58caa3dc DJ |
1214 | { |
1215 | struct regset_info *regset; | |
e9d25b98 | 1216 | int saw_general_regs = 0; |
58caa3dc DJ |
1217 | |
1218 | regset = target_regsets; | |
1219 | ||
1220 | while (regset->size >= 0) | |
1221 | { | |
1222 | void *buf; | |
1223 | int res; | |
1224 | ||
1225 | if (regset->size == 0) | |
1226 | { | |
1227 | regset ++; | |
1228 | continue; | |
1229 | } | |
1230 | ||
1231 | buf = malloc (regset->size); | |
d06f167a | 1232 | res = ptrace (regset->get_request, inferior_pid, 0, buf); |
58caa3dc DJ |
1233 | if (res < 0) |
1234 | { | |
1235 | if (errno == EIO) | |
1236 | { | |
1237 | /* If we get EIO on the first regset, do not try regsets again. | |
1238 | If we get EIO on a later regset, disable that regset. */ | |
1239 | if (regset == target_regsets) | |
1240 | { | |
1241 | use_regsets_p = 0; | |
1242 | return -1; | |
1243 | } | |
1244 | else | |
1245 | { | |
1246 | regset->size = 0; | |
1247 | continue; | |
1248 | } | |
1249 | } | |
1250 | else | |
1251 | { | |
0d62e5e8 | 1252 | char s[256]; |
a1928bad | 1253 | sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%ld", |
0d62e5e8 DJ |
1254 | inferior_pid); |
1255 | perror (s); | |
58caa3dc DJ |
1256 | } |
1257 | } | |
e9d25b98 DJ |
1258 | else if (regset->type == GENERAL_REGS) |
1259 | saw_general_regs = 1; | |
58caa3dc DJ |
1260 | regset->store_function (buf); |
1261 | regset ++; | |
1262 | } | |
e9d25b98 DJ |
1263 | if (saw_general_regs) |
1264 | return 0; | |
1265 | else | |
1266 | return 1; | |
58caa3dc DJ |
1267 | } |
1268 | ||
1269 | static int | |
0d62e5e8 | 1270 | regsets_store_inferior_registers () |
58caa3dc DJ |
1271 | { |
1272 | struct regset_info *regset; | |
e9d25b98 | 1273 | int saw_general_regs = 0; |
58caa3dc DJ |
1274 | |
1275 | regset = target_regsets; | |
1276 | ||
1277 | while (regset->size >= 0) | |
1278 | { | |
1279 | void *buf; | |
1280 | int res; | |
1281 | ||
1282 | if (regset->size == 0) | |
1283 | { | |
1284 | regset ++; | |
1285 | continue; | |
1286 | } | |
1287 | ||
1288 | buf = malloc (regset->size); | |
1289 | regset->fill_function (buf); | |
d06f167a | 1290 | res = ptrace (regset->set_request, inferior_pid, 0, buf); |
58caa3dc DJ |
1291 | if (res < 0) |
1292 | { | |
1293 | if (errno == EIO) | |
1294 | { | |
1295 | /* If we get EIO on the first regset, do not try regsets again. | |
1296 | If we get EIO on a later regset, disable that regset. */ | |
1297 | if (regset == target_regsets) | |
1298 | { | |
1299 | use_regsets_p = 0; | |
1300 | return -1; | |
1301 | } | |
1302 | else | |
1303 | { | |
1304 | regset->size = 0; | |
1305 | continue; | |
1306 | } | |
1307 | } | |
1308 | else | |
1309 | { | |
ce3a066d | 1310 | perror ("Warning: ptrace(regsets_store_inferior_registers)"); |
58caa3dc DJ |
1311 | } |
1312 | } | |
e9d25b98 DJ |
1313 | else if (regset->type == GENERAL_REGS) |
1314 | saw_general_regs = 1; | |
58caa3dc | 1315 | regset ++; |
09ec9b38 | 1316 | free (buf); |
58caa3dc | 1317 | } |
e9d25b98 DJ |
1318 | if (saw_general_regs) |
1319 | return 0; | |
1320 | else | |
1321 | return 1; | |
ce3a066d | 1322 | return 0; |
58caa3dc DJ |
1323 | } |
1324 | ||
1325 | #endif /* HAVE_LINUX_REGSETS */ | |
1326 | ||
1327 | ||
1328 | void | |
ce3a066d | 1329 | linux_fetch_registers (int regno) |
58caa3dc DJ |
1330 | { |
1331 | #ifdef HAVE_LINUX_REGSETS | |
1332 | if (use_regsets_p) | |
1333 | { | |
1334 | if (regsets_fetch_inferior_registers () == 0) | |
1335 | return; | |
1336 | } | |
1337 | #endif | |
1338 | #ifdef HAVE_LINUX_USRREGS | |
1339 | usr_fetch_inferior_registers (regno); | |
1340 | #endif | |
1341 | } | |
1342 | ||
1343 | void | |
ce3a066d | 1344 | linux_store_registers (int regno) |
58caa3dc DJ |
1345 | { |
1346 | #ifdef HAVE_LINUX_REGSETS | |
1347 | if (use_regsets_p) | |
1348 | { | |
1349 | if (regsets_store_inferior_registers () == 0) | |
1350 | return; | |
1351 | } | |
1352 | #endif | |
1353 | #ifdef HAVE_LINUX_USRREGS | |
1354 | usr_store_inferior_registers (regno); | |
1355 | #endif | |
1356 | } | |
1357 | ||
da6d8c04 | 1358 | |
da6d8c04 DJ |
1359 | /* Copy LEN bytes from inferior's memory starting at MEMADDR |
1360 | to debugger memory starting at MYADDR. */ | |
1361 | ||
c3e735a6 | 1362 | static int |
f450004a | 1363 | linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
da6d8c04 DJ |
1364 | { |
1365 | register int i; | |
1366 | /* Round starting address down to longword boundary. */ | |
1367 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
1368 | /* Round ending address up; get number of longwords that makes. */ | |
aa691b87 RM |
1369 | register int count |
1370 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) | |
da6d8c04 DJ |
1371 | / sizeof (PTRACE_XFER_TYPE); |
1372 | /* Allocate buffer of that many longwords. */ | |
aa691b87 | 1373 | register PTRACE_XFER_TYPE *buffer |
da6d8c04 DJ |
1374 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); |
1375 | ||
1376 | /* Read all the longwords */ | |
1377 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
1378 | { | |
c3e735a6 | 1379 | errno = 0; |
d844cde6 | 1380 | buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0); |
c3e735a6 DJ |
1381 | if (errno) |
1382 | return errno; | |
da6d8c04 DJ |
1383 | } |
1384 | ||
1385 | /* Copy appropriate bytes out of the buffer. */ | |
1386 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len); | |
c3e735a6 DJ |
1387 | |
1388 | return 0; | |
da6d8c04 DJ |
1389 | } |
1390 | ||
1391 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
1392 | to inferior's memory at MEMADDR. | |
1393 | On failure (cannot write the inferior) | |
1394 | returns the value of errno. */ | |
1395 | ||
ce3a066d | 1396 | static int |
f450004a | 1397 | linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) |
da6d8c04 DJ |
1398 | { |
1399 | register int i; | |
1400 | /* Round starting address down to longword boundary. */ | |
1401 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
1402 | /* Round ending address up; get number of longwords that makes. */ | |
1403 | register int count | |
1404 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE); | |
1405 | /* Allocate buffer of that many longwords. */ | |
1406 | register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); | |
1407 | extern int errno; | |
1408 | ||
0d62e5e8 DJ |
1409 | if (debug_threads) |
1410 | { | |
1411 | fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr); | |
1412 | } | |
1413 | ||
da6d8c04 DJ |
1414 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
1415 | ||
d844cde6 DJ |
1416 | buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, |
1417 | (PTRACE_ARG3_TYPE) addr, 0); | |
da6d8c04 DJ |
1418 | |
1419 | if (count > 1) | |
1420 | { | |
1421 | buffer[count - 1] | |
1422 | = ptrace (PTRACE_PEEKTEXT, inferior_pid, | |
d844cde6 DJ |
1423 | (PTRACE_ARG3_TYPE) (addr + (count - 1) |
1424 | * sizeof (PTRACE_XFER_TYPE)), | |
1425 | 0); | |
da6d8c04 DJ |
1426 | } |
1427 | ||
1428 | /* Copy data to be written over corresponding part of buffer */ | |
1429 | ||
1430 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len); | |
1431 | ||
1432 | /* Write the entire buffer. */ | |
1433 | ||
1434 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
1435 | { | |
1436 | errno = 0; | |
d844cde6 | 1437 | ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]); |
da6d8c04 DJ |
1438 | if (errno) |
1439 | return errno; | |
1440 | } | |
1441 | ||
1442 | return 0; | |
1443 | } | |
2f2893d9 DJ |
1444 | |
1445 | static void | |
1446 | linux_look_up_symbols (void) | |
1447 | { | |
0d62e5e8 DJ |
1448 | #ifdef USE_THREAD_DB |
1449 | if (using_threads) | |
1450 | return; | |
1451 | ||
1452 | using_threads = thread_db_init (); | |
1453 | #endif | |
1454 | } | |
1455 | ||
e5379b03 DJ |
1456 | static void |
1457 | linux_send_signal (int signum) | |
1458 | { | |
a1928bad | 1459 | extern unsigned long signal_pid; |
e5379b03 | 1460 | |
d592fa2f | 1461 | if (cont_thread != 0 && cont_thread != -1) |
e5379b03 DJ |
1462 | { |
1463 | struct process_info *process; | |
1464 | ||
1465 | process = get_thread_process (current_inferior); | |
fd500816 | 1466 | kill_lwp (process->lwpid, signum); |
e5379b03 DJ |
1467 | } |
1468 | else | |
fd500816 | 1469 | kill_lwp (signal_pid, signum); |
e5379b03 DJ |
1470 | } |
1471 | ||
aa691b87 RM |
1472 | /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET |
1473 | to debugger memory starting at MYADDR. */ | |
1474 | ||
1475 | static int | |
f450004a | 1476 | linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len) |
aa691b87 RM |
1477 | { |
1478 | char filename[PATH_MAX]; | |
1479 | int fd, n; | |
1480 | ||
a1928bad | 1481 | snprintf (filename, sizeof filename, "/proc/%ld/auxv", inferior_pid); |
aa691b87 RM |
1482 | |
1483 | fd = open (filename, O_RDONLY); | |
1484 | if (fd < 0) | |
1485 | return -1; | |
1486 | ||
1487 | if (offset != (CORE_ADDR) 0 | |
1488 | && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset) | |
1489 | n = -1; | |
1490 | else | |
1491 | n = read (fd, myaddr, len); | |
1492 | ||
1493 | close (fd); | |
1494 | ||
1495 | return n; | |
1496 | } | |
1497 | ||
e013ee27 OF |
1498 | /* These watchpoint related wrapper functions simply pass on the function call |
1499 | if the target has registered a corresponding function. */ | |
1500 | ||
1501 | static int | |
1502 | linux_insert_watchpoint (char type, CORE_ADDR addr, int len) | |
1503 | { | |
1504 | if (the_low_target.insert_watchpoint != NULL) | |
1505 | return the_low_target.insert_watchpoint (type, addr, len); | |
1506 | else | |
1507 | /* Unsupported (see target.h). */ | |
1508 | return 1; | |
1509 | } | |
1510 | ||
1511 | static int | |
1512 | linux_remove_watchpoint (char type, CORE_ADDR addr, int len) | |
1513 | { | |
1514 | if (the_low_target.remove_watchpoint != NULL) | |
1515 | return the_low_target.remove_watchpoint (type, addr, len); | |
1516 | else | |
1517 | /* Unsupported (see target.h). */ | |
1518 | return 1; | |
1519 | } | |
1520 | ||
1521 | static int | |
1522 | linux_stopped_by_watchpoint (void) | |
1523 | { | |
1524 | if (the_low_target.stopped_by_watchpoint != NULL) | |
1525 | return the_low_target.stopped_by_watchpoint (); | |
1526 | else | |
1527 | return 0; | |
1528 | } | |
1529 | ||
1530 | static CORE_ADDR | |
1531 | linux_stopped_data_address (void) | |
1532 | { | |
1533 | if (the_low_target.stopped_data_address != NULL) | |
1534 | return the_low_target.stopped_data_address (); | |
1535 | else | |
1536 | return 0; | |
1537 | } | |
1538 | ||
ce3a066d DJ |
1539 | static struct target_ops linux_target_ops = { |
1540 | linux_create_inferior, | |
1541 | linux_attach, | |
1542 | linux_kill, | |
6ad8ae5c | 1543 | linux_detach, |
ce3a066d DJ |
1544 | linux_thread_alive, |
1545 | linux_resume, | |
1546 | linux_wait, | |
1547 | linux_fetch_registers, | |
1548 | linux_store_registers, | |
1549 | linux_read_memory, | |
1550 | linux_write_memory, | |
2f2893d9 | 1551 | linux_look_up_symbols, |
e5379b03 | 1552 | linux_send_signal, |
aa691b87 | 1553 | linux_read_auxv, |
e013ee27 OF |
1554 | linux_insert_watchpoint, |
1555 | linux_remove_watchpoint, | |
1556 | linux_stopped_by_watchpoint, | |
1557 | linux_stopped_data_address, | |
ce3a066d DJ |
1558 | }; |
1559 | ||
0d62e5e8 DJ |
1560 | static void |
1561 | linux_init_signals () | |
1562 | { | |
1563 | /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads | |
1564 | to find what the cancel signal actually is. */ | |
254787d4 | 1565 | signal (__SIGRTMIN+1, SIG_IGN); |
0d62e5e8 DJ |
1566 | } |
1567 | ||
da6d8c04 DJ |
1568 | void |
1569 | initialize_low (void) | |
1570 | { | |
0d62e5e8 | 1571 | using_threads = 0; |
ce3a066d | 1572 | set_target_ops (&linux_target_ops); |
611cb4a5 DJ |
1573 | set_breakpoint_data (the_low_target.breakpoint, |
1574 | the_low_target.breakpoint_len); | |
0a30fbc4 | 1575 | init_registers (); |
0d62e5e8 | 1576 | linux_init_signals (); |
da6d8c04 | 1577 | } |