Commit | Line | Data |
---|---|---|
da6d8c04 | 1 | /* Low level interface to ptrace, for the remote server for GDB. |
545587ee | 2 | Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
9b254dd1 | 3 | 2006, 2007, 2008 Free Software Foundation, Inc. |
da6d8c04 DJ |
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 |
da6d8c04 DJ |
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/>. */ |
da6d8c04 DJ |
19 | |
20 | #include "server.h" | |
58caa3dc | 21 | #include "linux-low.h" |
da6d8c04 | 22 | |
58caa3dc | 23 | #include <sys/wait.h> |
da6d8c04 DJ |
24 | #include <stdio.h> |
25 | #include <sys/param.h> | |
26 | #include <sys/dir.h> | |
27 | #include <sys/ptrace.h> | |
28 | #include <sys/user.h> | |
29 | #include <signal.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include <fcntl.h> | |
d07c63e7 | 32 | #include <string.h> |
0a30fbc4 DJ |
33 | #include <stdlib.h> |
34 | #include <unistd.h> | |
fa6a77dc | 35 | #include <errno.h> |
fd500816 | 36 | #include <sys/syscall.h> |
f9387fc3 | 37 | #include <sched.h> |
da6d8c04 | 38 | |
32ca6d61 DJ |
39 | #ifndef PTRACE_GETSIGINFO |
40 | # define PTRACE_GETSIGINFO 0x4202 | |
41 | # define PTRACE_SETSIGINFO 0x4203 | |
42 | #endif | |
43 | ||
fd462a61 DJ |
44 | #ifndef O_LARGEFILE |
45 | #define O_LARGEFILE 0 | |
46 | #endif | |
47 | ||
24a09b5f DJ |
48 | /* If the system headers did not provide the constants, hard-code the normal |
49 | values. */ | |
50 | #ifndef PTRACE_EVENT_FORK | |
51 | ||
52 | #define PTRACE_SETOPTIONS 0x4200 | |
53 | #define PTRACE_GETEVENTMSG 0x4201 | |
54 | ||
55 | /* options set using PTRACE_SETOPTIONS */ | |
56 | #define PTRACE_O_TRACESYSGOOD 0x00000001 | |
57 | #define PTRACE_O_TRACEFORK 0x00000002 | |
58 | #define PTRACE_O_TRACEVFORK 0x00000004 | |
59 | #define PTRACE_O_TRACECLONE 0x00000008 | |
60 | #define PTRACE_O_TRACEEXEC 0x00000010 | |
61 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 | |
62 | #define PTRACE_O_TRACEEXIT 0x00000040 | |
63 | ||
64 | /* Wait extended result codes for the above trace options. */ | |
65 | #define PTRACE_EVENT_FORK 1 | |
66 | #define PTRACE_EVENT_VFORK 2 | |
67 | #define PTRACE_EVENT_CLONE 3 | |
68 | #define PTRACE_EVENT_EXEC 4 | |
69 | #define PTRACE_EVENT_VFORK_DONE 5 | |
70 | #define PTRACE_EVENT_EXIT 6 | |
71 | ||
72 | #endif /* PTRACE_EVENT_FORK */ | |
73 | ||
74 | /* We can't always assume that this flag is available, but all systems | |
75 | with the ptrace event handlers also have __WALL, so it's safe to use | |
76 | in some contexts. */ | |
77 | #ifndef __WALL | |
78 | #define __WALL 0x40000000 /* Wait for any child. */ | |
79 | #endif | |
80 | ||
42c81e2a DJ |
81 | #ifdef __UCLIBC__ |
82 | #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__)) | |
83 | #define HAS_NOMMU | |
84 | #endif | |
85 | #endif | |
86 | ||
24a09b5f DJ |
87 | /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol |
88 | representation of the thread ID. | |
611cb4a5 | 89 | |
0d62e5e8 DJ |
90 | ``all_processes'' is keyed by the process ID - which on Linux is (presently) |
91 | the same as the LWP ID. */ | |
92 | ||
93 | struct inferior_list all_processes; | |
94 | ||
24a09b5f DJ |
95 | /* A list of all unknown processes which receive stop signals. Some other |
96 | process will presumably claim each of these as forked children | |
97 | momentarily. */ | |
98 | ||
99 | struct inferior_list stopped_pids; | |
100 | ||
0d62e5e8 DJ |
101 | /* FIXME this is a bit of a hack, and could be removed. */ |
102 | int stopping_threads; | |
103 | ||
104 | /* FIXME make into a target method? */ | |
24a09b5f DJ |
105 | int using_threads = 1; |
106 | static int thread_db_active; | |
107 | ||
108 | static int must_set_ptrace_flags; | |
0d62e5e8 DJ |
109 | |
110 | static void linux_resume_one_process (struct inferior_list_entry *entry, | |
32ca6d61 | 111 | int step, int signal, siginfo_t *info); |
64386c31 | 112 | static void linux_resume (struct thread_resume *resume_info); |
0d62e5e8 DJ |
113 | static void stop_all_processes (void); |
114 | static int linux_wait_for_event (struct thread_info *child); | |
ae13219e | 115 | static int check_removed_breakpoint (struct process_info *event_child); |
24a09b5f | 116 | static void *add_process (unsigned long pid); |
0d62e5e8 DJ |
117 | |
118 | struct pending_signals | |
119 | { | |
120 | int signal; | |
32ca6d61 | 121 | siginfo_t info; |
0d62e5e8 DJ |
122 | struct pending_signals *prev; |
123 | }; | |
611cb4a5 | 124 | |
d844cde6 | 125 | #define PTRACE_ARG3_TYPE long |
c6ecbae5 | 126 | #define PTRACE_XFER_TYPE long |
da6d8c04 | 127 | |
58caa3dc DJ |
128 | #ifdef HAVE_LINUX_REGSETS |
129 | static int use_regsets_p = 1; | |
130 | #endif | |
131 | ||
0d62e5e8 DJ |
132 | #define pid_of(proc) ((proc)->head.id) |
133 | ||
134 | /* FIXME: Delete eventually. */ | |
135 | #define inferior_pid (pid_of (get_thread_process (current_inferior))) | |
136 | ||
24a09b5f DJ |
137 | static void |
138 | handle_extended_wait (struct process_info *event_child, int wstat) | |
139 | { | |
140 | int event = wstat >> 16; | |
141 | struct process_info *new_process; | |
142 | ||
143 | if (event == PTRACE_EVENT_CLONE) | |
144 | { | |
145 | unsigned long new_pid; | |
146 | int ret, status; | |
147 | ||
148 | ptrace (PTRACE_GETEVENTMSG, inferior_pid, 0, &new_pid); | |
149 | ||
150 | /* If we haven't already seen the new PID stop, wait for it now. */ | |
151 | if (! pull_pid_from_list (&stopped_pids, new_pid)) | |
152 | { | |
153 | /* The new child has a pending SIGSTOP. We can't affect it until it | |
154 | hits the SIGSTOP, but we're already attached. */ | |
155 | ||
156 | do { | |
157 | ret = waitpid (new_pid, &status, __WALL); | |
158 | } while (ret == -1 && errno == EINTR); | |
159 | ||
160 | if (ret == -1) | |
161 | perror_with_name ("waiting for new child"); | |
162 | else if (ret != new_pid) | |
163 | warning ("wait returned unexpected PID %d", ret); | |
da5898ce | 164 | else if (!WIFSTOPPED (status)) |
24a09b5f DJ |
165 | warning ("wait returned unexpected status 0x%x", status); |
166 | } | |
167 | ||
168 | ptrace (PTRACE_SETOPTIONS, new_pid, 0, PTRACE_O_TRACECLONE); | |
169 | ||
170 | new_process = (struct process_info *) add_process (new_pid); | |
171 | add_thread (new_pid, new_process, new_pid); | |
172 | new_thread_notify (thread_id_to_gdb_id (new_process->lwpid)); | |
173 | ||
da5898ce DJ |
174 | /* Normally we will get the pending SIGSTOP. But in some cases |
175 | we might get another signal delivered to the group first. | |
176 | If we do, be sure not to lose it. */ | |
177 | if (WSTOPSIG (status) == SIGSTOP) | |
178 | { | |
179 | if (stopping_threads) | |
180 | new_process->stopped = 1; | |
181 | else | |
182 | ptrace (PTRACE_CONT, new_pid, 0, 0); | |
183 | } | |
24a09b5f | 184 | else |
da5898ce DJ |
185 | { |
186 | new_process->stop_expected = 1; | |
187 | if (stopping_threads) | |
188 | { | |
189 | new_process->stopped = 1; | |
190 | new_process->status_pending_p = 1; | |
191 | new_process->status_pending = status; | |
192 | } | |
193 | else | |
194 | /* Pass the signal on. This is what GDB does - except | |
195 | shouldn't we really report it instead? */ | |
196 | ptrace (PTRACE_CONT, new_pid, 0, WSTOPSIG (status)); | |
197 | } | |
24a09b5f DJ |
198 | |
199 | /* Always resume the current thread. If we are stopping | |
200 | threads, it will have a pending SIGSTOP; we may as well | |
201 | collect it now. */ | |
202 | linux_resume_one_process (&event_child->head, | |
203 | event_child->stepping, 0, NULL); | |
204 | } | |
205 | } | |
206 | ||
0d62e5e8 DJ |
207 | /* This function should only be called if the process got a SIGTRAP. |
208 | The SIGTRAP could mean several things. | |
209 | ||
210 | On i386, where decr_pc_after_break is non-zero: | |
211 | If we were single-stepping this process using PTRACE_SINGLESTEP, | |
212 | we will get only the one SIGTRAP (even if the instruction we | |
213 | stepped over was a breakpoint). The value of $eip will be the | |
214 | next instruction. | |
215 | If we continue the process using PTRACE_CONT, we will get a | |
216 | SIGTRAP when we hit a breakpoint. The value of $eip will be | |
217 | the instruction after the breakpoint (i.e. needs to be | |
218 | decremented). If we report the SIGTRAP to GDB, we must also | |
219 | report the undecremented PC. If we cancel the SIGTRAP, we | |
220 | must resume at the decremented PC. | |
221 | ||
222 | (Presumably, not yet tested) On a non-decr_pc_after_break machine | |
223 | with hardware or kernel single-step: | |
224 | If we single-step over a breakpoint instruction, our PC will | |
225 | point at the following instruction. If we continue and hit a | |
226 | breakpoint instruction, our PC will point at the breakpoint | |
227 | instruction. */ | |
228 | ||
229 | static CORE_ADDR | |
230 | get_stop_pc (void) | |
231 | { | |
232 | CORE_ADDR stop_pc = (*the_low_target.get_pc) (); | |
233 | ||
234 | if (get_thread_process (current_inferior)->stepping) | |
235 | return stop_pc; | |
236 | else | |
237 | return stop_pc - the_low_target.decr_pc_after_break; | |
238 | } | |
ce3a066d | 239 | |
0d62e5e8 | 240 | static void * |
a1928bad | 241 | add_process (unsigned long pid) |
611cb4a5 | 242 | { |
0d62e5e8 DJ |
243 | struct process_info *process; |
244 | ||
245 | process = (struct process_info *) malloc (sizeof (*process)); | |
246 | memset (process, 0, sizeof (*process)); | |
247 | ||
248 | process->head.id = pid; | |
0d62e5e8 DJ |
249 | process->lwpid = pid; |
250 | ||
251 | add_inferior_to_list (&all_processes, &process->head); | |
252 | ||
253 | return process; | |
254 | } | |
611cb4a5 | 255 | |
da6d8c04 DJ |
256 | /* Start an inferior process and returns its pid. |
257 | ALLARGS is a vector of program-name and args. */ | |
258 | ||
ce3a066d DJ |
259 | static int |
260 | linux_create_inferior (char *program, char **allargs) | |
da6d8c04 | 261 | { |
0d62e5e8 | 262 | void *new_process; |
da6d8c04 DJ |
263 | int pid; |
264 | ||
42c81e2a | 265 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) |
52fb6437 NS |
266 | pid = vfork (); |
267 | #else | |
da6d8c04 | 268 | pid = fork (); |
52fb6437 | 269 | #endif |
da6d8c04 DJ |
270 | if (pid < 0) |
271 | perror_with_name ("fork"); | |
272 | ||
273 | if (pid == 0) | |
274 | { | |
275 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
276 | ||
254787d4 | 277 | signal (__SIGRTMIN + 1, SIG_DFL); |
0d62e5e8 | 278 | |
a9fa9f7d DJ |
279 | setpgid (0, 0); |
280 | ||
2b876972 DJ |
281 | execv (program, allargs); |
282 | if (errno == ENOENT) | |
283 | execvp (program, allargs); | |
da6d8c04 DJ |
284 | |
285 | fprintf (stderr, "Cannot exec %s: %s.\n", program, | |
d07c63e7 | 286 | strerror (errno)); |
da6d8c04 DJ |
287 | fflush (stderr); |
288 | _exit (0177); | |
289 | } | |
290 | ||
0d62e5e8 | 291 | new_process = add_process (pid); |
a06660f7 | 292 | add_thread (pid, new_process, pid); |
24a09b5f | 293 | must_set_ptrace_flags = 1; |
611cb4a5 | 294 | |
a9fa9f7d | 295 | return pid; |
da6d8c04 DJ |
296 | } |
297 | ||
298 | /* Attach to an inferior process. */ | |
299 | ||
0d62e5e8 | 300 | void |
24a09b5f | 301 | linux_attach_lwp (unsigned long pid) |
da6d8c04 | 302 | { |
0d62e5e8 | 303 | struct process_info *new_process; |
611cb4a5 | 304 | |
da6d8c04 DJ |
305 | if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0) |
306 | { | |
2d717e4f DJ |
307 | if (all_threads.head != NULL) |
308 | { | |
309 | /* If we fail to attach to an LWP, just warn. */ | |
310 | fprintf (stderr, "Cannot attach to process %ld: %s (%d)\n", pid, | |
311 | strerror (errno), errno); | |
312 | fflush (stderr); | |
313 | return; | |
314 | } | |
315 | else | |
316 | /* If we fail to attach to a process, report an error. */ | |
317 | error ("Cannot attach to process %ld: %s (%d)\n", pid, | |
43d5792c | 318 | strerror (errno), errno); |
da6d8c04 DJ |
319 | } |
320 | ||
24a09b5f DJ |
321 | ptrace (PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACECLONE); |
322 | ||
0d62e5e8 | 323 | new_process = (struct process_info *) add_process (pid); |
24a09b5f DJ |
324 | add_thread (pid, new_process, pid); |
325 | new_thread_notify (thread_id_to_gdb_id (new_process->lwpid)); | |
0d62e5e8 DJ |
326 | |
327 | /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH | |
328 | brings it to a halt. We should ignore that SIGSTOP and resume the process | |
329 | (unless this is the first process, in which case the flag will be cleared | |
330 | in linux_attach). | |
331 | ||
332 | On the other hand, if we are currently trying to stop all threads, we | |
333 | should treat the new thread as if we had sent it a SIGSTOP. This works | |
334 | because we are guaranteed that add_process added us to the end of the | |
335 | list, and so the new thread has not yet reached wait_for_sigstop (but | |
336 | will). */ | |
337 | if (! stopping_threads) | |
338 | new_process->stop_expected = 1; | |
339 | } | |
340 | ||
341 | int | |
a1928bad | 342 | linux_attach (unsigned long pid) |
0d62e5e8 DJ |
343 | { |
344 | struct process_info *process; | |
345 | ||
24a09b5f | 346 | linux_attach_lwp (pid); |
0d62e5e8 | 347 | |
ae13219e DJ |
348 | /* Don't ignore the initial SIGSTOP if we just attached to this process. |
349 | It will be collected by wait shortly. */ | |
0d62e5e8 DJ |
350 | process = (struct process_info *) find_inferior_id (&all_processes, pid); |
351 | process->stop_expected = 0; | |
352 | ||
da6d8c04 DJ |
353 | return 0; |
354 | } | |
355 | ||
356 | /* Kill the inferior process. Make us have no inferior. */ | |
357 | ||
ce3a066d | 358 | static void |
0d62e5e8 | 359 | linux_kill_one_process (struct inferior_list_entry *entry) |
da6d8c04 | 360 | { |
0d62e5e8 DJ |
361 | struct thread_info *thread = (struct thread_info *) entry; |
362 | struct process_info *process = get_thread_process (thread); | |
363 | int wstat; | |
364 | ||
fd500816 DJ |
365 | /* We avoid killing the first thread here, because of a Linux kernel (at |
366 | least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before | |
367 | the children get a chance to be reaped, it will remain a zombie | |
368 | forever. */ | |
369 | if (entry == all_threads.head) | |
370 | return; | |
371 | ||
0d62e5e8 DJ |
372 | do |
373 | { | |
374 | ptrace (PTRACE_KILL, pid_of (process), 0, 0); | |
375 | ||
376 | /* Make sure it died. The loop is most likely unnecessary. */ | |
377 | wstat = linux_wait_for_event (thread); | |
378 | } while (WIFSTOPPED (wstat)); | |
da6d8c04 DJ |
379 | } |
380 | ||
0d62e5e8 DJ |
381 | static void |
382 | linux_kill (void) | |
383 | { | |
fd500816 | 384 | struct thread_info *thread = (struct thread_info *) all_threads.head; |
9d606399 | 385 | struct process_info *process; |
fd500816 DJ |
386 | int wstat; |
387 | ||
9d606399 DJ |
388 | if (thread == NULL) |
389 | return; | |
390 | ||
0d62e5e8 | 391 | for_each_inferior (&all_threads, linux_kill_one_process); |
fd500816 DJ |
392 | |
393 | /* See the comment in linux_kill_one_process. We did not kill the first | |
394 | thread in the list, so do so now. */ | |
9d606399 | 395 | process = get_thread_process (thread); |
fd500816 DJ |
396 | do |
397 | { | |
398 | ptrace (PTRACE_KILL, pid_of (process), 0, 0); | |
399 | ||
400 | /* Make sure it died. The loop is most likely unnecessary. */ | |
401 | wstat = linux_wait_for_event (thread); | |
402 | } while (WIFSTOPPED (wstat)); | |
2d717e4f DJ |
403 | |
404 | clear_inferiors (); | |
405 | free (all_processes.head); | |
406 | all_processes.head = all_processes.tail = NULL; | |
0d62e5e8 DJ |
407 | } |
408 | ||
6ad8ae5c DJ |
409 | static void |
410 | linux_detach_one_process (struct inferior_list_entry *entry) | |
411 | { | |
412 | struct thread_info *thread = (struct thread_info *) entry; | |
413 | struct process_info *process = get_thread_process (thread); | |
414 | ||
ae13219e DJ |
415 | /* Make sure the process isn't stopped at a breakpoint that's |
416 | no longer there. */ | |
417 | check_removed_breakpoint (process); | |
418 | ||
419 | /* If this process is stopped but is expecting a SIGSTOP, then make | |
420 | sure we take care of that now. This isn't absolutely guaranteed | |
421 | to collect the SIGSTOP, but is fairly likely to. */ | |
422 | if (process->stop_expected) | |
423 | { | |
424 | /* Clear stop_expected, so that the SIGSTOP will be reported. */ | |
425 | process->stop_expected = 0; | |
426 | if (process->stopped) | |
427 | linux_resume_one_process (&process->head, 0, 0, NULL); | |
428 | linux_wait_for_event (thread); | |
429 | } | |
430 | ||
431 | /* Flush any pending changes to the process's registers. */ | |
432 | regcache_invalidate_one ((struct inferior_list_entry *) | |
433 | get_process_thread (process)); | |
434 | ||
435 | /* Finally, let it resume. */ | |
6ad8ae5c DJ |
436 | ptrace (PTRACE_DETACH, pid_of (process), 0, 0); |
437 | } | |
438 | ||
dd6953e1 | 439 | static int |
6ad8ae5c DJ |
440 | linux_detach (void) |
441 | { | |
ae13219e | 442 | delete_all_breakpoints (); |
6ad8ae5c | 443 | for_each_inferior (&all_threads, linux_detach_one_process); |
ae13219e | 444 | clear_inferiors (); |
2d717e4f DJ |
445 | free (all_processes.head); |
446 | all_processes.head = all_processes.tail = NULL; | |
dd6953e1 | 447 | return 0; |
6ad8ae5c DJ |
448 | } |
449 | ||
444d6139 PA |
450 | static void |
451 | linux_join (void) | |
452 | { | |
453 | extern unsigned long signal_pid; | |
454 | int status, ret; | |
455 | ||
456 | do { | |
457 | ret = waitpid (signal_pid, &status, 0); | |
458 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
459 | break; | |
460 | } while (ret != -1 || errno != ECHILD); | |
461 | } | |
462 | ||
6ad8ae5c | 463 | /* Return nonzero if the given thread is still alive. */ |
0d62e5e8 | 464 | static int |
24a09b5f | 465 | linux_thread_alive (unsigned long lwpid) |
0d62e5e8 | 466 | { |
24a09b5f | 467 | if (find_inferior_id (&all_threads, lwpid) != NULL) |
0d62e5e8 DJ |
468 | return 1; |
469 | else | |
470 | return 0; | |
471 | } | |
472 | ||
473 | /* Return nonzero if this process stopped at a breakpoint which | |
474 | no longer appears to be inserted. Also adjust the PC | |
475 | appropriately to resume where the breakpoint used to be. */ | |
ce3a066d | 476 | static int |
0d62e5e8 | 477 | check_removed_breakpoint (struct process_info *event_child) |
da6d8c04 | 478 | { |
0d62e5e8 DJ |
479 | CORE_ADDR stop_pc; |
480 | struct thread_info *saved_inferior; | |
481 | ||
482 | if (event_child->pending_is_breakpoint == 0) | |
483 | return 0; | |
484 | ||
485 | if (debug_threads) | |
ae13219e DJ |
486 | fprintf (stderr, "Checking for breakpoint in process %ld.\n", |
487 | event_child->lwpid); | |
0d62e5e8 DJ |
488 | |
489 | saved_inferior = current_inferior; | |
490 | current_inferior = get_process_thread (event_child); | |
491 | ||
492 | stop_pc = get_stop_pc (); | |
493 | ||
494 | /* If the PC has changed since we stopped, then we shouldn't do | |
495 | anything. This happens if, for instance, GDB handled the | |
496 | decr_pc_after_break subtraction itself. */ | |
497 | if (stop_pc != event_child->pending_stop_pc) | |
498 | { | |
499 | if (debug_threads) | |
ae13219e DJ |
500 | fprintf (stderr, "Ignoring, PC was changed. Old PC was 0x%08llx\n", |
501 | event_child->pending_stop_pc); | |
0d62e5e8 DJ |
502 | |
503 | event_child->pending_is_breakpoint = 0; | |
504 | current_inferior = saved_inferior; | |
505 | return 0; | |
506 | } | |
507 | ||
508 | /* If the breakpoint is still there, we will report hitting it. */ | |
509 | if ((*the_low_target.breakpoint_at) (stop_pc)) | |
510 | { | |
511 | if (debug_threads) | |
512 | fprintf (stderr, "Ignoring, breakpoint is still present.\n"); | |
513 | current_inferior = saved_inferior; | |
514 | return 0; | |
515 | } | |
516 | ||
517 | if (debug_threads) | |
518 | fprintf (stderr, "Removed breakpoint.\n"); | |
519 | ||
520 | /* For decr_pc_after_break targets, here is where we perform the | |
521 | decrement. We go immediately from this function to resuming, | |
522 | and can not safely call get_stop_pc () again. */ | |
523 | if (the_low_target.set_pc != NULL) | |
524 | (*the_low_target.set_pc) (stop_pc); | |
525 | ||
526 | /* We consumed the pending SIGTRAP. */ | |
5544ad89 | 527 | event_child->pending_is_breakpoint = 0; |
0d62e5e8 DJ |
528 | event_child->status_pending_p = 0; |
529 | event_child->status_pending = 0; | |
530 | ||
531 | current_inferior = saved_inferior; | |
da6d8c04 DJ |
532 | return 1; |
533 | } | |
534 | ||
0d62e5e8 DJ |
535 | /* Return 1 if this process has an interesting status pending. This function |
536 | may silently resume an inferior process. */ | |
611cb4a5 | 537 | static int |
0d62e5e8 DJ |
538 | status_pending_p (struct inferior_list_entry *entry, void *dummy) |
539 | { | |
540 | struct process_info *process = (struct process_info *) entry; | |
541 | ||
542 | if (process->status_pending_p) | |
543 | if (check_removed_breakpoint (process)) | |
544 | { | |
545 | /* This thread was stopped at a breakpoint, and the breakpoint | |
546 | is now gone. We were told to continue (or step...) all threads, | |
547 | so GDB isn't trying to single-step past this breakpoint. | |
548 | So instead of reporting the old SIGTRAP, pretend we got to | |
549 | the breakpoint just after it was removed instead of just | |
550 | before; resume the process. */ | |
32ca6d61 | 551 | linux_resume_one_process (&process->head, 0, 0, NULL); |
0d62e5e8 DJ |
552 | return 0; |
553 | } | |
554 | ||
555 | return process->status_pending_p; | |
556 | } | |
557 | ||
558 | static void | |
559 | linux_wait_for_process (struct process_info **childp, int *wstatp) | |
611cb4a5 | 560 | { |
0d62e5e8 DJ |
561 | int ret; |
562 | int to_wait_for = -1; | |
563 | ||
564 | if (*childp != NULL) | |
565 | to_wait_for = (*childp)->lwpid; | |
611cb4a5 | 566 | |
24a09b5f | 567 | retry: |
611cb4a5 DJ |
568 | while (1) |
569 | { | |
0d62e5e8 DJ |
570 | ret = waitpid (to_wait_for, wstatp, WNOHANG); |
571 | ||
572 | if (ret == -1) | |
573 | { | |
574 | if (errno != ECHILD) | |
575 | perror_with_name ("waitpid"); | |
576 | } | |
577 | else if (ret > 0) | |
578 | break; | |
579 | ||
580 | ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE); | |
581 | ||
582 | if (ret == -1) | |
583 | { | |
584 | if (errno != ECHILD) | |
585 | perror_with_name ("waitpid (WCLONE)"); | |
586 | } | |
587 | else if (ret > 0) | |
588 | break; | |
589 | ||
590 | usleep (1000); | |
591 | } | |
592 | ||
593 | if (debug_threads | |
594 | && (!WIFSTOPPED (*wstatp) | |
595 | || (WSTOPSIG (*wstatp) != 32 | |
596 | && WSTOPSIG (*wstatp) != 33))) | |
597 | fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp); | |
598 | ||
599 | if (to_wait_for == -1) | |
600 | *childp = (struct process_info *) find_inferior_id (&all_processes, ret); | |
601 | ||
24a09b5f DJ |
602 | /* If we didn't find a process, one of two things presumably happened: |
603 | - A process we started and then detached from has exited. Ignore it. | |
604 | - A process we are controlling has forked and the new child's stop | |
605 | was reported to us by the kernel. Save its PID. */ | |
606 | if (*childp == NULL && WIFSTOPPED (*wstatp)) | |
607 | { | |
608 | add_pid_to_list (&stopped_pids, ret); | |
609 | goto retry; | |
610 | } | |
611 | else if (*childp == NULL) | |
612 | goto retry; | |
613 | ||
0d62e5e8 DJ |
614 | (*childp)->stopped = 1; |
615 | (*childp)->pending_is_breakpoint = 0; | |
616 | ||
32ca6d61 DJ |
617 | (*childp)->last_status = *wstatp; |
618 | ||
0d62e5e8 DJ |
619 | if (debug_threads |
620 | && WIFSTOPPED (*wstatp)) | |
621 | { | |
622 | current_inferior = (struct thread_info *) | |
24a09b5f | 623 | find_inferior_id (&all_threads, (*childp)->lwpid); |
0d62e5e8 DJ |
624 | /* For testing only; i386_stop_pc prints out a diagnostic. */ |
625 | if (the_low_target.get_pc != NULL) | |
626 | get_stop_pc (); | |
627 | } | |
628 | } | |
611cb4a5 | 629 | |
0d62e5e8 DJ |
630 | static int |
631 | linux_wait_for_event (struct thread_info *child) | |
632 | { | |
633 | CORE_ADDR stop_pc; | |
634 | struct process_info *event_child; | |
635 | int wstat; | |
b65d95c5 | 636 | int bp_status; |
0d62e5e8 DJ |
637 | |
638 | /* Check for a process with a pending status. */ | |
639 | /* It is possible that the user changed the pending task's registers since | |
640 | it stopped. We correctly handle the change of PC if we hit a breakpoint | |
e5379b03 | 641 | (in check_removed_breakpoint); signals should be reported anyway. */ |
0d62e5e8 DJ |
642 | if (child == NULL) |
643 | { | |
644 | event_child = (struct process_info *) | |
645 | find_inferior (&all_processes, status_pending_p, NULL); | |
646 | if (debug_threads && event_child) | |
a1928bad | 647 | fprintf (stderr, "Got a pending child %ld\n", event_child->lwpid); |
0d62e5e8 DJ |
648 | } |
649 | else | |
650 | { | |
651 | event_child = get_thread_process (child); | |
652 | if (event_child->status_pending_p | |
653 | && check_removed_breakpoint (event_child)) | |
654 | event_child = NULL; | |
655 | } | |
611cb4a5 | 656 | |
0d62e5e8 DJ |
657 | if (event_child != NULL) |
658 | { | |
659 | if (event_child->status_pending_p) | |
611cb4a5 | 660 | { |
0d62e5e8 | 661 | if (debug_threads) |
a1928bad | 662 | fprintf (stderr, "Got an event from pending child %ld (%04x)\n", |
0d62e5e8 DJ |
663 | event_child->lwpid, event_child->status_pending); |
664 | wstat = event_child->status_pending; | |
665 | event_child->status_pending_p = 0; | |
666 | event_child->status_pending = 0; | |
667 | current_inferior = get_process_thread (event_child); | |
668 | return wstat; | |
669 | } | |
670 | } | |
671 | ||
672 | /* We only enter this loop if no process has a pending wait status. Thus | |
673 | any action taken in response to a wait status inside this loop is | |
674 | responding as soon as we detect the status, not after any pending | |
675 | events. */ | |
676 | while (1) | |
677 | { | |
678 | if (child == NULL) | |
679 | event_child = NULL; | |
680 | else | |
681 | event_child = get_thread_process (child); | |
682 | ||
683 | linux_wait_for_process (&event_child, &wstat); | |
684 | ||
685 | if (event_child == NULL) | |
686 | error ("event from unknown child"); | |
611cb4a5 | 687 | |
0d62e5e8 | 688 | current_inferior = (struct thread_info *) |
24a09b5f | 689 | find_inferior_id (&all_threads, event_child->lwpid); |
0d62e5e8 | 690 | |
89be2091 | 691 | /* Check for thread exit. */ |
24a09b5f | 692 | if (! WIFSTOPPED (wstat)) |
0d62e5e8 | 693 | { |
89be2091 | 694 | if (debug_threads) |
24a09b5f | 695 | fprintf (stderr, "LWP %ld exiting\n", event_child->head.id); |
89be2091 DJ |
696 | |
697 | /* If the last thread is exiting, just return. */ | |
698 | if (all_threads.head == all_threads.tail) | |
699 | return wstat; | |
700 | ||
24a09b5f | 701 | dead_thread_notify (thread_id_to_gdb_id (event_child->lwpid)); |
89be2091 DJ |
702 | |
703 | remove_inferior (&all_processes, &event_child->head); | |
704 | free (event_child); | |
705 | remove_thread (current_inferior); | |
706 | current_inferior = (struct thread_info *) all_threads.head; | |
707 | ||
708 | /* If we were waiting for this particular child to do something... | |
709 | well, it did something. */ | |
710 | if (child != NULL) | |
711 | return wstat; | |
712 | ||
713 | /* Wait for a more interesting event. */ | |
714 | continue; | |
715 | } | |
716 | ||
24a09b5f | 717 | if (WIFSTOPPED (wstat) |
89be2091 DJ |
718 | && WSTOPSIG (wstat) == SIGSTOP |
719 | && event_child->stop_expected) | |
720 | { | |
721 | if (debug_threads) | |
722 | fprintf (stderr, "Expected stop.\n"); | |
723 | event_child->stop_expected = 0; | |
724 | linux_resume_one_process (&event_child->head, | |
725 | event_child->stepping, 0, NULL); | |
726 | continue; | |
727 | } | |
728 | ||
24a09b5f DJ |
729 | if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGTRAP |
730 | && wstat >> 16 != 0) | |
731 | { | |
732 | handle_extended_wait (event_child, wstat); | |
733 | continue; | |
734 | } | |
735 | ||
89be2091 DJ |
736 | /* If GDB is not interested in this signal, don't stop other |
737 | threads, and don't report it to GDB. Just resume the | |
738 | inferior right away. We do this for threading-related | |
69f223ed DJ |
739 | signals as well as any that GDB specifically requested we |
740 | ignore. But never ignore SIGSTOP if we sent it ourselves, | |
741 | and do not ignore signals when stepping - they may require | |
742 | special handling to skip the signal handler. */ | |
89be2091 DJ |
743 | /* FIXME drow/2002-06-09: Get signal numbers from the inferior's |
744 | thread library? */ | |
745 | if (WIFSTOPPED (wstat) | |
69f223ed | 746 | && !event_child->stepping |
24a09b5f DJ |
747 | && ( |
748 | #ifdef USE_THREAD_DB | |
749 | (thread_db_active && (WSTOPSIG (wstat) == __SIGRTMIN | |
750 | || WSTOPSIG (wstat) == __SIGRTMIN + 1)) | |
751 | || | |
752 | #endif | |
753 | (pass_signals[target_signal_from_host (WSTOPSIG (wstat))] | |
754 | && (WSTOPSIG (wstat) != SIGSTOP || !stopping_threads)))) | |
89be2091 DJ |
755 | { |
756 | siginfo_t info, *info_p; | |
757 | ||
758 | if (debug_threads) | |
24a09b5f DJ |
759 | fprintf (stderr, "Ignored signal %d for LWP %ld.\n", |
760 | WSTOPSIG (wstat), event_child->head.id); | |
89be2091 DJ |
761 | |
762 | if (ptrace (PTRACE_GETSIGINFO, event_child->lwpid, 0, &info) == 0) | |
763 | info_p = &info; | |
764 | else | |
765 | info_p = NULL; | |
766 | linux_resume_one_process (&event_child->head, | |
767 | event_child->stepping, | |
768 | WSTOPSIG (wstat), info_p); | |
769 | continue; | |
0d62e5e8 | 770 | } |
611cb4a5 | 771 | |
0d62e5e8 DJ |
772 | /* If this event was not handled above, and is not a SIGTRAP, report |
773 | it. */ | |
774 | if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP) | |
775 | return wstat; | |
611cb4a5 | 776 | |
0d62e5e8 DJ |
777 | /* If this target does not support breakpoints, we simply report the |
778 | SIGTRAP; it's of no concern to us. */ | |
779 | if (the_low_target.get_pc == NULL) | |
780 | return wstat; | |
781 | ||
782 | stop_pc = get_stop_pc (); | |
783 | ||
784 | /* bp_reinsert will only be set if we were single-stepping. | |
785 | Notice that we will resume the process after hitting | |
786 | a gdbserver breakpoint; single-stepping to/over one | |
787 | is not supported (yet). */ | |
788 | if (event_child->bp_reinsert != 0) | |
789 | { | |
790 | if (debug_threads) | |
791 | fprintf (stderr, "Reinserted breakpoint.\n"); | |
792 | reinsert_breakpoint (event_child->bp_reinsert); | |
793 | event_child->bp_reinsert = 0; | |
794 | ||
795 | /* Clear the single-stepping flag and SIGTRAP as we resume. */ | |
32ca6d61 | 796 | linux_resume_one_process (&event_child->head, 0, 0, NULL); |
0d62e5e8 DJ |
797 | continue; |
798 | } | |
799 | ||
b65d95c5 | 800 | bp_status = check_breakpoints (stop_pc); |
0d62e5e8 | 801 | |
b65d95c5 | 802 | if (bp_status != 0) |
0d62e5e8 | 803 | { |
b65d95c5 DJ |
804 | if (debug_threads) |
805 | fprintf (stderr, "Hit a gdbserver breakpoint.\n"); | |
806 | ||
0d62e5e8 | 807 | /* We hit one of our own breakpoints. We mark it as a pending |
e5379b03 | 808 | breakpoint, so that check_removed_breakpoint () will do the PC |
0d62e5e8 DJ |
809 | adjustment for us at the appropriate time. */ |
810 | event_child->pending_is_breakpoint = 1; | |
811 | event_child->pending_stop_pc = stop_pc; | |
812 | ||
b65d95c5 | 813 | /* We may need to put the breakpoint back. We continue in the event |
0d62e5e8 DJ |
814 | loop instead of simply replacing the breakpoint right away, |
815 | in order to not lose signals sent to the thread that hit the | |
816 | breakpoint. Unfortunately this increases the window where another | |
817 | thread could sneak past the removed breakpoint. For the current | |
818 | use of server-side breakpoints (thread creation) this is | |
819 | acceptable; but it needs to be considered before this breakpoint | |
820 | mechanism can be used in more general ways. For some breakpoints | |
821 | it may be necessary to stop all other threads, but that should | |
822 | be avoided where possible. | |
823 | ||
824 | If breakpoint_reinsert_addr is NULL, that means that we can | |
825 | use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint, | |
826 | mark it for reinsertion, and single-step. | |
827 | ||
828 | Otherwise, call the target function to figure out where we need | |
829 | our temporary breakpoint, create it, and continue executing this | |
830 | process. */ | |
b65d95c5 DJ |
831 | if (bp_status == 2) |
832 | /* No need to reinsert. */ | |
833 | linux_resume_one_process (&event_child->head, 0, 0, NULL); | |
834 | else if (the_low_target.breakpoint_reinsert_addr == NULL) | |
0d62e5e8 DJ |
835 | { |
836 | event_child->bp_reinsert = stop_pc; | |
837 | uninsert_breakpoint (stop_pc); | |
32ca6d61 | 838 | linux_resume_one_process (&event_child->head, 1, 0, NULL); |
0d62e5e8 DJ |
839 | } |
840 | else | |
841 | { | |
842 | reinsert_breakpoint_by_bp | |
843 | (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ()); | |
32ca6d61 | 844 | linux_resume_one_process (&event_child->head, 0, 0, NULL); |
611cb4a5 | 845 | } |
0d62e5e8 DJ |
846 | |
847 | continue; | |
848 | } | |
849 | ||
b65d95c5 DJ |
850 | if (debug_threads) |
851 | fprintf (stderr, "Hit a non-gdbserver breakpoint.\n"); | |
852 | ||
0d62e5e8 DJ |
853 | /* If we were single-stepping, we definitely want to report the |
854 | SIGTRAP. The single-step operation has completed, so also | |
aa691b87 | 855 | clear the stepping flag; in general this does not matter, |
0d62e5e8 DJ |
856 | because the SIGTRAP will be reported to the client, which |
857 | will give us a new action for this thread, but clear it for | |
858 | consistency anyway. It's safe to clear the stepping flag | |
859 | because the only consumer of get_stop_pc () after this point | |
e5379b03 | 860 | is check_removed_breakpoint, and pending_is_breakpoint is not |
0d62e5e8 DJ |
861 | set. It might be wiser to use a step_completed flag instead. */ |
862 | if (event_child->stepping) | |
863 | { | |
864 | event_child->stepping = 0; | |
865 | return wstat; | |
866 | } | |
867 | ||
868 | /* A SIGTRAP that we can't explain. It may have been a breakpoint. | |
869 | Check if it is a breakpoint, and if so mark the process information | |
870 | accordingly. This will handle both the necessary fiddling with the | |
871 | PC on decr_pc_after_break targets and suppressing extra threads | |
872 | hitting a breakpoint if two hit it at once and then GDB removes it | |
873 | after the first is reported. Arguably it would be better to report | |
874 | multiple threads hitting breakpoints simultaneously, but the current | |
875 | remote protocol does not allow this. */ | |
876 | if ((*the_low_target.breakpoint_at) (stop_pc)) | |
877 | { | |
878 | event_child->pending_is_breakpoint = 1; | |
879 | event_child->pending_stop_pc = stop_pc; | |
611cb4a5 DJ |
880 | } |
881 | ||
882 | return wstat; | |
883 | } | |
0d62e5e8 | 884 | |
611cb4a5 DJ |
885 | /* NOTREACHED */ |
886 | return 0; | |
887 | } | |
888 | ||
0d62e5e8 | 889 | /* Wait for process, returns status. */ |
da6d8c04 | 890 | |
ce3a066d DJ |
891 | static unsigned char |
892 | linux_wait (char *status) | |
da6d8c04 | 893 | { |
e5f1222d | 894 | int w; |
0d62e5e8 DJ |
895 | struct thread_info *child = NULL; |
896 | ||
897 | retry: | |
898 | /* If we were only supposed to resume one thread, only wait for | |
899 | that thread - if it's still alive. If it died, however - which | |
900 | can happen if we're coming from the thread death case below - | |
901 | then we need to make sure we restart the other threads. We could | |
902 | pick a thread at random or restart all; restarting all is less | |
903 | arbitrary. */ | |
d592fa2f | 904 | if (cont_thread != 0 && cont_thread != -1) |
0d62e5e8 DJ |
905 | { |
906 | child = (struct thread_info *) find_inferior_id (&all_threads, | |
907 | cont_thread); | |
908 | ||
909 | /* No stepping, no signal - unless one is pending already, of course. */ | |
910 | if (child == NULL) | |
64386c31 DJ |
911 | { |
912 | struct thread_resume resume_info; | |
913 | resume_info.thread = -1; | |
914 | resume_info.step = resume_info.sig = resume_info.leave_stopped = 0; | |
915 | linux_resume (&resume_info); | |
916 | } | |
0d62e5e8 | 917 | } |
da6d8c04 | 918 | |
0d62e5e8 DJ |
919 | w = linux_wait_for_event (child); |
920 | stop_all_processes (); | |
da6d8c04 | 921 | |
24a09b5f DJ |
922 | if (must_set_ptrace_flags) |
923 | { | |
924 | ptrace (PTRACE_SETOPTIONS, inferior_pid, 0, PTRACE_O_TRACECLONE); | |
925 | must_set_ptrace_flags = 0; | |
926 | } | |
927 | ||
0d62e5e8 DJ |
928 | /* If we are waiting for a particular child, and it exited, |
929 | linux_wait_for_event will return its exit status. Similarly if | |
930 | the last child exited. If this is not the last child, however, | |
931 | do not report it as exited until there is a 'thread exited' response | |
932 | available in the remote protocol. Instead, just wait for another event. | |
933 | This should be safe, because if the thread crashed we will already | |
934 | have reported the termination signal to GDB; that should stop any | |
935 | in-progress stepping operations, etc. | |
936 | ||
937 | Report the exit status of the last thread to exit. This matches | |
938 | LinuxThreads' behavior. */ | |
939 | ||
940 | if (all_threads.head == all_threads.tail) | |
da6d8c04 | 941 | { |
0d62e5e8 DJ |
942 | if (WIFEXITED (w)) |
943 | { | |
944 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
945 | *status = 'W'; | |
946 | clear_inferiors (); | |
075b3282 DJ |
947 | free (all_processes.head); |
948 | all_processes.head = all_processes.tail = NULL; | |
b80864fb | 949 | return WEXITSTATUS (w); |
0d62e5e8 DJ |
950 | } |
951 | else if (!WIFSTOPPED (w)) | |
952 | { | |
953 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
0d62e5e8 | 954 | *status = 'X'; |
075b3282 DJ |
955 | clear_inferiors (); |
956 | free (all_processes.head); | |
957 | all_processes.head = all_processes.tail = NULL; | |
b80864fb | 958 | return target_signal_from_host (WTERMSIG (w)); |
0d62e5e8 | 959 | } |
da6d8c04 | 960 | } |
0d62e5e8 | 961 | else |
da6d8c04 | 962 | { |
0d62e5e8 DJ |
963 | if (!WIFSTOPPED (w)) |
964 | goto retry; | |
da6d8c04 DJ |
965 | } |
966 | ||
da6d8c04 | 967 | *status = 'T'; |
b80864fb | 968 | return target_signal_from_host (WSTOPSIG (w)); |
da6d8c04 DJ |
969 | } |
970 | ||
fd500816 DJ |
971 | /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if |
972 | thread groups are in use, we need to use tkill. */ | |
973 | ||
974 | static int | |
a1928bad | 975 | kill_lwp (unsigned long lwpid, int signo) |
fd500816 DJ |
976 | { |
977 | static int tkill_failed; | |
978 | ||
979 | errno = 0; | |
980 | ||
981 | #ifdef SYS_tkill | |
982 | if (!tkill_failed) | |
983 | { | |
984 | int ret = syscall (SYS_tkill, lwpid, signo); | |
985 | if (errno != ENOSYS) | |
986 | return ret; | |
987 | errno = 0; | |
988 | tkill_failed = 1; | |
989 | } | |
990 | #endif | |
991 | ||
992 | return kill (lwpid, signo); | |
993 | } | |
994 | ||
0d62e5e8 DJ |
995 | static void |
996 | send_sigstop (struct inferior_list_entry *entry) | |
997 | { | |
998 | struct process_info *process = (struct process_info *) entry; | |
999 | ||
1000 | if (process->stopped) | |
1001 | return; | |
1002 | ||
1003 | /* If we already have a pending stop signal for this process, don't | |
1004 | send another. */ | |
1005 | if (process->stop_expected) | |
1006 | { | |
ae13219e DJ |
1007 | if (debug_threads) |
1008 | fprintf (stderr, "Have pending sigstop for process %ld\n", | |
1009 | process->lwpid); | |
1010 | ||
1011 | /* We clear the stop_expected flag so that wait_for_sigstop | |
1012 | will receive the SIGSTOP event (instead of silently resuming and | |
1013 | waiting again). It'll be reset below. */ | |
0d62e5e8 DJ |
1014 | process->stop_expected = 0; |
1015 | return; | |
1016 | } | |
1017 | ||
1018 | if (debug_threads) | |
a1928bad | 1019 | fprintf (stderr, "Sending sigstop to process %ld\n", process->head.id); |
0d62e5e8 | 1020 | |
fd500816 | 1021 | kill_lwp (process->head.id, SIGSTOP); |
0d62e5e8 DJ |
1022 | } |
1023 | ||
1024 | static void | |
1025 | wait_for_sigstop (struct inferior_list_entry *entry) | |
1026 | { | |
1027 | struct process_info *process = (struct process_info *) entry; | |
1028 | struct thread_info *saved_inferior, *thread; | |
a1928bad DJ |
1029 | int wstat; |
1030 | unsigned long saved_tid; | |
0d62e5e8 DJ |
1031 | |
1032 | if (process->stopped) | |
1033 | return; | |
1034 | ||
1035 | saved_inferior = current_inferior; | |
1036 | saved_tid = ((struct inferior_list_entry *) saved_inferior)->id; | |
1037 | thread = (struct thread_info *) find_inferior_id (&all_threads, | |
24a09b5f | 1038 | process->lwpid); |
0d62e5e8 DJ |
1039 | wstat = linux_wait_for_event (thread); |
1040 | ||
1041 | /* If we stopped with a non-SIGSTOP signal, save it for later | |
1042 | and record the pending SIGSTOP. If the process exited, just | |
1043 | return. */ | |
1044 | if (WIFSTOPPED (wstat) | |
1045 | && WSTOPSIG (wstat) != SIGSTOP) | |
1046 | { | |
1047 | if (debug_threads) | |
24a09b5f DJ |
1048 | fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n", |
1049 | process->lwpid, wstat); | |
0d62e5e8 DJ |
1050 | process->status_pending_p = 1; |
1051 | process->status_pending = wstat; | |
1052 | process->stop_expected = 1; | |
1053 | } | |
1054 | ||
1055 | if (linux_thread_alive (saved_tid)) | |
1056 | current_inferior = saved_inferior; | |
1057 | else | |
1058 | { | |
1059 | if (debug_threads) | |
1060 | fprintf (stderr, "Previously current thread died.\n"); | |
1061 | ||
1062 | /* Set a valid thread as current. */ | |
1063 | set_desired_inferior (0); | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | static void | |
1068 | stop_all_processes (void) | |
1069 | { | |
1070 | stopping_threads = 1; | |
1071 | for_each_inferior (&all_processes, send_sigstop); | |
1072 | for_each_inferior (&all_processes, wait_for_sigstop); | |
1073 | stopping_threads = 0; | |
1074 | } | |
1075 | ||
da6d8c04 DJ |
1076 | /* Resume execution of the inferior process. |
1077 | If STEP is nonzero, single-step it. | |
1078 | If SIGNAL is nonzero, give it that signal. */ | |
1079 | ||
ce3a066d | 1080 | static void |
0d62e5e8 | 1081 | linux_resume_one_process (struct inferior_list_entry *entry, |
32ca6d61 | 1082 | int step, int signal, siginfo_t *info) |
da6d8c04 | 1083 | { |
0d62e5e8 DJ |
1084 | struct process_info *process = (struct process_info *) entry; |
1085 | struct thread_info *saved_inferior; | |
1086 | ||
1087 | if (process->stopped == 0) | |
1088 | return; | |
1089 | ||
1090 | /* If we have pending signals or status, and a new signal, enqueue the | |
1091 | signal. Also enqueue the signal if we are waiting to reinsert a | |
1092 | breakpoint; it will be picked up again below. */ | |
1093 | if (signal != 0 | |
1094 | && (process->status_pending_p || process->pending_signals != NULL | |
1095 | || process->bp_reinsert != 0)) | |
1096 | { | |
1097 | struct pending_signals *p_sig; | |
1098 | p_sig = malloc (sizeof (*p_sig)); | |
1099 | p_sig->prev = process->pending_signals; | |
1100 | p_sig->signal = signal; | |
32ca6d61 DJ |
1101 | if (info == NULL) |
1102 | memset (&p_sig->info, 0, sizeof (siginfo_t)); | |
1103 | else | |
1104 | memcpy (&p_sig->info, info, sizeof (siginfo_t)); | |
0d62e5e8 DJ |
1105 | process->pending_signals = p_sig; |
1106 | } | |
1107 | ||
e5379b03 | 1108 | if (process->status_pending_p && !check_removed_breakpoint (process)) |
0d62e5e8 DJ |
1109 | return; |
1110 | ||
1111 | saved_inferior = current_inferior; | |
1112 | current_inferior = get_process_thread (process); | |
1113 | ||
1114 | if (debug_threads) | |
a1928bad | 1115 | fprintf (stderr, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid, |
0d62e5e8 DJ |
1116 | step ? "step" : "continue", signal, |
1117 | process->stop_expected ? "expected" : "not expected"); | |
1118 | ||
1119 | /* This bit needs some thinking about. If we get a signal that | |
1120 | we must report while a single-step reinsert is still pending, | |
1121 | we often end up resuming the thread. It might be better to | |
1122 | (ew) allow a stack of pending events; then we could be sure that | |
1123 | the reinsert happened right away and not lose any signals. | |
1124 | ||
1125 | Making this stack would also shrink the window in which breakpoints are | |
1126 | uninserted (see comment in linux_wait_for_process) but not enough for | |
1127 | complete correctness, so it won't solve that problem. It may be | |
1128 | worthwhile just to solve this one, however. */ | |
1129 | if (process->bp_reinsert != 0) | |
1130 | { | |
1131 | if (debug_threads) | |
1132 | fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert); | |
1133 | if (step == 0) | |
1134 | fprintf (stderr, "BAD - reinserting but not stepping.\n"); | |
1135 | step = 1; | |
1136 | ||
1137 | /* Postpone any pending signal. It was enqueued above. */ | |
1138 | signal = 0; | |
1139 | } | |
1140 | ||
1141 | check_removed_breakpoint (process); | |
1142 | ||
aa691b87 | 1143 | if (debug_threads && the_low_target.get_pc != NULL) |
0d62e5e8 DJ |
1144 | { |
1145 | fprintf (stderr, " "); | |
52fb6437 | 1146 | (*the_low_target.get_pc) (); |
0d62e5e8 DJ |
1147 | } |
1148 | ||
1149 | /* If we have pending signals, consume one unless we are trying to reinsert | |
1150 | a breakpoint. */ | |
1151 | if (process->pending_signals != NULL && process->bp_reinsert == 0) | |
1152 | { | |
1153 | struct pending_signals **p_sig; | |
1154 | ||
1155 | p_sig = &process->pending_signals; | |
1156 | while ((*p_sig)->prev != NULL) | |
1157 | p_sig = &(*p_sig)->prev; | |
1158 | ||
1159 | signal = (*p_sig)->signal; | |
32ca6d61 DJ |
1160 | if ((*p_sig)->info.si_signo != 0) |
1161 | ptrace (PTRACE_SETSIGINFO, process->lwpid, 0, &(*p_sig)->info); | |
1162 | ||
0d62e5e8 DJ |
1163 | free (*p_sig); |
1164 | *p_sig = NULL; | |
1165 | } | |
1166 | ||
1167 | regcache_invalidate_one ((struct inferior_list_entry *) | |
1168 | get_process_thread (process)); | |
da6d8c04 | 1169 | errno = 0; |
0d62e5e8 DJ |
1170 | process->stopped = 0; |
1171 | process->stepping = step; | |
1172 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal); | |
1173 | ||
1174 | current_inferior = saved_inferior; | |
da6d8c04 DJ |
1175 | if (errno) |
1176 | perror_with_name ("ptrace"); | |
1177 | } | |
1178 | ||
64386c31 DJ |
1179 | static struct thread_resume *resume_ptr; |
1180 | ||
1181 | /* This function is called once per thread. We look up the thread | |
5544ad89 DJ |
1182 | in RESUME_PTR, and mark the thread with a pointer to the appropriate |
1183 | resume request. | |
1184 | ||
1185 | This algorithm is O(threads * resume elements), but resume elements | |
1186 | is small (and will remain small at least until GDB supports thread | |
1187 | suspension). */ | |
0d62e5e8 | 1188 | static void |
5544ad89 | 1189 | linux_set_resume_request (struct inferior_list_entry *entry) |
0d62e5e8 DJ |
1190 | { |
1191 | struct process_info *process; | |
64386c31 | 1192 | struct thread_info *thread; |
5544ad89 | 1193 | int ndx; |
64386c31 DJ |
1194 | |
1195 | thread = (struct thread_info *) entry; | |
1196 | process = get_thread_process (thread); | |
1197 | ||
1198 | ndx = 0; | |
1199 | while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id) | |
1200 | ndx++; | |
1201 | ||
5544ad89 DJ |
1202 | process->resume = &resume_ptr[ndx]; |
1203 | } | |
1204 | ||
1205 | /* This function is called once per thread. We check the thread's resume | |
1206 | request, which will tell us whether to resume, step, or leave the thread | |
1207 | stopped; and what signal, if any, it should be sent. For threads which | |
1208 | we aren't explicitly told otherwise, we preserve the stepping flag; this | |
1209 | is used for stepping over gdbserver-placed breakpoints. */ | |
1210 | ||
1211 | static void | |
1212 | linux_continue_one_thread (struct inferior_list_entry *entry) | |
1213 | { | |
1214 | struct process_info *process; | |
1215 | struct thread_info *thread; | |
1216 | int step; | |
1217 | ||
1218 | thread = (struct thread_info *) entry; | |
1219 | process = get_thread_process (thread); | |
1220 | ||
1221 | if (process->resume->leave_stopped) | |
64386c31 DJ |
1222 | return; |
1223 | ||
5544ad89 DJ |
1224 | if (process->resume->thread == -1) |
1225 | step = process->stepping || process->resume->step; | |
64386c31 | 1226 | else |
5544ad89 DJ |
1227 | step = process->resume->step; |
1228 | ||
32ca6d61 | 1229 | linux_resume_one_process (&process->head, step, process->resume->sig, NULL); |
c6ecbae5 | 1230 | |
5544ad89 DJ |
1231 | process->resume = NULL; |
1232 | } | |
1233 | ||
1234 | /* This function is called once per thread. We check the thread's resume | |
1235 | request, which will tell us whether to resume, step, or leave the thread | |
1236 | stopped; and what signal, if any, it should be sent. We queue any needed | |
1237 | signals, since we won't actually resume. We already have a pending event | |
1238 | to report, so we don't need to preserve any step requests; they should | |
1239 | be re-issued if necessary. */ | |
1240 | ||
1241 | static void | |
1242 | linux_queue_one_thread (struct inferior_list_entry *entry) | |
1243 | { | |
1244 | struct process_info *process; | |
1245 | struct thread_info *thread; | |
1246 | ||
1247 | thread = (struct thread_info *) entry; | |
1248 | process = get_thread_process (thread); | |
1249 | ||
1250 | if (process->resume->leave_stopped) | |
1251 | return; | |
1252 | ||
1253 | /* If we have a new signal, enqueue the signal. */ | |
1254 | if (process->resume->sig != 0) | |
1255 | { | |
1256 | struct pending_signals *p_sig; | |
1257 | p_sig = malloc (sizeof (*p_sig)); | |
1258 | p_sig->prev = process->pending_signals; | |
1259 | p_sig->signal = process->resume->sig; | |
32ca6d61 DJ |
1260 | memset (&p_sig->info, 0, sizeof (siginfo_t)); |
1261 | ||
1262 | /* If this is the same signal we were previously stopped by, | |
1263 | make sure to queue its siginfo. We can ignore the return | |
1264 | value of ptrace; if it fails, we'll skip | |
1265 | PTRACE_SETSIGINFO. */ | |
1266 | if (WIFSTOPPED (process->last_status) | |
1267 | && WSTOPSIG (process->last_status) == process->resume->sig) | |
1268 | ptrace (PTRACE_GETSIGINFO, process->lwpid, 0, &p_sig->info); | |
1269 | ||
5544ad89 DJ |
1270 | process->pending_signals = p_sig; |
1271 | } | |
1272 | ||
1273 | process->resume = NULL; | |
1274 | } | |
1275 | ||
1276 | /* Set DUMMY if this process has an interesting status pending. */ | |
1277 | static int | |
1278 | resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p) | |
1279 | { | |
1280 | struct process_info *process = (struct process_info *) entry; | |
1281 | ||
1282 | /* Processes which will not be resumed are not interesting, because | |
1283 | we might not wait for them next time through linux_wait. */ | |
1284 | if (process->resume->leave_stopped) | |
1285 | return 0; | |
1286 | ||
1287 | /* If this thread has a removed breakpoint, we won't have any | |
1288 | events to report later, so check now. check_removed_breakpoint | |
1289 | may clear status_pending_p. We avoid calling check_removed_breakpoint | |
1290 | for any thread that we are not otherwise going to resume - this | |
1291 | lets us preserve stopped status when two threads hit a breakpoint. | |
1292 | GDB removes the breakpoint to single-step a particular thread | |
1293 | past it, then re-inserts it and resumes all threads. We want | |
1294 | to report the second thread without resuming it in the interim. */ | |
1295 | if (process->status_pending_p) | |
1296 | check_removed_breakpoint (process); | |
1297 | ||
1298 | if (process->status_pending_p) | |
1299 | * (int *) flag_p = 1; | |
1300 | ||
1301 | return 0; | |
0d62e5e8 DJ |
1302 | } |
1303 | ||
1304 | static void | |
64386c31 | 1305 | linux_resume (struct thread_resume *resume_info) |
0d62e5e8 | 1306 | { |
5544ad89 | 1307 | int pending_flag; |
c6ecbae5 | 1308 | |
5544ad89 | 1309 | /* Yes, the use of a global here is rather ugly. */ |
64386c31 | 1310 | resume_ptr = resume_info; |
5544ad89 DJ |
1311 | |
1312 | for_each_inferior (&all_threads, linux_set_resume_request); | |
1313 | ||
1314 | /* If there is a thread which would otherwise be resumed, which | |
1315 | has a pending status, then don't resume any threads - we can just | |
1316 | report the pending status. Make sure to queue any signals | |
1317 | that would otherwise be sent. */ | |
1318 | pending_flag = 0; | |
1319 | find_inferior (&all_processes, resume_status_pending_p, &pending_flag); | |
1320 | ||
1321 | if (debug_threads) | |
1322 | { | |
1323 | if (pending_flag) | |
1324 | fprintf (stderr, "Not resuming, pending status\n"); | |
1325 | else | |
1326 | fprintf (stderr, "Resuming, no pending status\n"); | |
1327 | } | |
1328 | ||
1329 | if (pending_flag) | |
1330 | for_each_inferior (&all_threads, linux_queue_one_thread); | |
1331 | else | |
a20d5e98 | 1332 | for_each_inferior (&all_threads, linux_continue_one_thread); |
0d62e5e8 DJ |
1333 | } |
1334 | ||
1335 | #ifdef HAVE_LINUX_USRREGS | |
da6d8c04 DJ |
1336 | |
1337 | int | |
0a30fbc4 | 1338 | register_addr (int regnum) |
da6d8c04 DJ |
1339 | { |
1340 | int addr; | |
1341 | ||
2ec06d2e | 1342 | if (regnum < 0 || regnum >= the_low_target.num_regs) |
da6d8c04 DJ |
1343 | error ("Invalid register number %d.", regnum); |
1344 | ||
2ec06d2e | 1345 | addr = the_low_target.regmap[regnum]; |
da6d8c04 DJ |
1346 | |
1347 | return addr; | |
1348 | } | |
1349 | ||
58caa3dc | 1350 | /* Fetch one register. */ |
da6d8c04 DJ |
1351 | static void |
1352 | fetch_register (int regno) | |
1353 | { | |
1354 | CORE_ADDR regaddr; | |
48d93c75 | 1355 | int i, size; |
0d62e5e8 | 1356 | char *buf; |
da6d8c04 | 1357 | |
2ec06d2e | 1358 | if (regno >= the_low_target.num_regs) |
0a30fbc4 | 1359 | return; |
2ec06d2e | 1360 | if ((*the_low_target.cannot_fetch_register) (regno)) |
0a30fbc4 | 1361 | return; |
da6d8c04 | 1362 | |
0a30fbc4 DJ |
1363 | regaddr = register_addr (regno); |
1364 | if (regaddr == -1) | |
1365 | return; | |
48d93c75 UW |
1366 | size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1) |
1367 | & - sizeof (PTRACE_XFER_TYPE); | |
1368 | buf = alloca (size); | |
1369 | for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE)) | |
da6d8c04 DJ |
1370 | { |
1371 | errno = 0; | |
0d62e5e8 | 1372 | *(PTRACE_XFER_TYPE *) (buf + i) = |
da6d8c04 DJ |
1373 | ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0); |
1374 | regaddr += sizeof (PTRACE_XFER_TYPE); | |
1375 | if (errno != 0) | |
1376 | { | |
1377 | /* Warning, not error, in case we are attached; sometimes the | |
1378 | kernel doesn't let us at the registers. */ | |
1379 | char *err = strerror (errno); | |
1380 | char *msg = alloca (strlen (err) + 128); | |
1381 | sprintf (msg, "reading register %d: %s", regno, err); | |
1382 | error (msg); | |
1383 | goto error_exit; | |
1384 | } | |
1385 | } | |
5a1f5858 DJ |
1386 | if (the_low_target.left_pad_xfer |
1387 | && register_size (regno) < sizeof (PTRACE_XFER_TYPE)) | |
1388 | supply_register (regno, (buf + sizeof (PTRACE_XFER_TYPE) | |
1389 | - register_size (regno))); | |
1390 | else | |
1391 | supply_register (regno, buf); | |
0d62e5e8 | 1392 | |
da6d8c04 DJ |
1393 | error_exit:; |
1394 | } | |
1395 | ||
1396 | /* Fetch all registers, or just one, from the child process. */ | |
58caa3dc DJ |
1397 | static void |
1398 | usr_fetch_inferior_registers (int regno) | |
da6d8c04 DJ |
1399 | { |
1400 | if (regno == -1 || regno == 0) | |
2ec06d2e | 1401 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
da6d8c04 DJ |
1402 | fetch_register (regno); |
1403 | else | |
1404 | fetch_register (regno); | |
1405 | } | |
1406 | ||
1407 | /* Store our register values back into the inferior. | |
1408 | If REGNO is -1, do this for all registers. | |
1409 | Otherwise, REGNO specifies which register (so we can save time). */ | |
58caa3dc DJ |
1410 | static void |
1411 | usr_store_inferior_registers (int regno) | |
da6d8c04 DJ |
1412 | { |
1413 | CORE_ADDR regaddr; | |
48d93c75 | 1414 | int i, size; |
0d62e5e8 | 1415 | char *buf; |
da6d8c04 DJ |
1416 | |
1417 | if (regno >= 0) | |
1418 | { | |
2ec06d2e | 1419 | if (regno >= the_low_target.num_regs) |
0a30fbc4 DJ |
1420 | return; |
1421 | ||
bc1e36ca | 1422 | if ((*the_low_target.cannot_store_register) (regno) == 1) |
0a30fbc4 DJ |
1423 | return; |
1424 | ||
1425 | regaddr = register_addr (regno); | |
1426 | if (regaddr == -1) | |
da6d8c04 | 1427 | return; |
da6d8c04 | 1428 | errno = 0; |
48d93c75 UW |
1429 | size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1) |
1430 | & - sizeof (PTRACE_XFER_TYPE); | |
1431 | buf = alloca (size); | |
1432 | memset (buf, 0, size); | |
5a1f5858 DJ |
1433 | if (the_low_target.left_pad_xfer |
1434 | && register_size (regno) < sizeof (PTRACE_XFER_TYPE)) | |
1435 | collect_register (regno, (buf + sizeof (PTRACE_XFER_TYPE) | |
1436 | - register_size (regno))); | |
1437 | else | |
1438 | collect_register (regno, buf); | |
48d93c75 | 1439 | for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE)) |
da6d8c04 | 1440 | { |
0a30fbc4 DJ |
1441 | errno = 0; |
1442 | ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
2ff29de4 | 1443 | *(PTRACE_XFER_TYPE *) (buf + i)); |
da6d8c04 DJ |
1444 | if (errno != 0) |
1445 | { | |
bc1e36ca DJ |
1446 | if ((*the_low_target.cannot_store_register) (regno) == 0) |
1447 | { | |
1448 | char *err = strerror (errno); | |
1449 | char *msg = alloca (strlen (err) + 128); | |
1450 | sprintf (msg, "writing register %d: %s", | |
1451 | regno, err); | |
1452 | error (msg); | |
1453 | return; | |
1454 | } | |
da6d8c04 | 1455 | } |
2ff29de4 | 1456 | regaddr += sizeof (PTRACE_XFER_TYPE); |
da6d8c04 | 1457 | } |
da6d8c04 DJ |
1458 | } |
1459 | else | |
2ec06d2e | 1460 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
0d62e5e8 | 1461 | usr_store_inferior_registers (regno); |
da6d8c04 | 1462 | } |
58caa3dc DJ |
1463 | #endif /* HAVE_LINUX_USRREGS */ |
1464 | ||
1465 | ||
1466 | ||
1467 | #ifdef HAVE_LINUX_REGSETS | |
1468 | ||
1469 | static int | |
0d62e5e8 | 1470 | regsets_fetch_inferior_registers () |
58caa3dc DJ |
1471 | { |
1472 | struct regset_info *regset; | |
e9d25b98 | 1473 | int saw_general_regs = 0; |
58caa3dc DJ |
1474 | |
1475 | regset = target_regsets; | |
1476 | ||
1477 | while (regset->size >= 0) | |
1478 | { | |
1479 | void *buf; | |
1480 | int res; | |
1481 | ||
1482 | if (regset->size == 0) | |
1483 | { | |
1484 | regset ++; | |
1485 | continue; | |
1486 | } | |
1487 | ||
1488 | buf = malloc (regset->size); | |
d06f167a | 1489 | res = ptrace (regset->get_request, inferior_pid, 0, buf); |
58caa3dc DJ |
1490 | if (res < 0) |
1491 | { | |
1492 | if (errno == EIO) | |
1493 | { | |
1494 | /* If we get EIO on the first regset, do not try regsets again. | |
1495 | If we get EIO on a later regset, disable that regset. */ | |
1496 | if (regset == target_regsets) | |
1497 | { | |
1498 | use_regsets_p = 0; | |
1499 | return -1; | |
1500 | } | |
1501 | else | |
1502 | { | |
1503 | regset->size = 0; | |
1504 | continue; | |
1505 | } | |
1506 | } | |
1507 | else | |
1508 | { | |
0d62e5e8 | 1509 | char s[256]; |
a1928bad | 1510 | sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%ld", |
0d62e5e8 DJ |
1511 | inferior_pid); |
1512 | perror (s); | |
58caa3dc DJ |
1513 | } |
1514 | } | |
e9d25b98 DJ |
1515 | else if (regset->type == GENERAL_REGS) |
1516 | saw_general_regs = 1; | |
58caa3dc DJ |
1517 | regset->store_function (buf); |
1518 | regset ++; | |
1519 | } | |
e9d25b98 DJ |
1520 | if (saw_general_regs) |
1521 | return 0; | |
1522 | else | |
1523 | return 1; | |
58caa3dc DJ |
1524 | } |
1525 | ||
1526 | static int | |
0d62e5e8 | 1527 | regsets_store_inferior_registers () |
58caa3dc DJ |
1528 | { |
1529 | struct regset_info *regset; | |
e9d25b98 | 1530 | int saw_general_regs = 0; |
58caa3dc DJ |
1531 | |
1532 | regset = target_regsets; | |
1533 | ||
1534 | while (regset->size >= 0) | |
1535 | { | |
1536 | void *buf; | |
1537 | int res; | |
1538 | ||
1539 | if (regset->size == 0) | |
1540 | { | |
1541 | regset ++; | |
1542 | continue; | |
1543 | } | |
1544 | ||
1545 | buf = malloc (regset->size); | |
545587ee DJ |
1546 | |
1547 | /* First fill the buffer with the current register set contents, | |
1548 | in case there are any items in the kernel's regset that are | |
1549 | not in gdbserver's regcache. */ | |
1550 | res = ptrace (regset->get_request, inferior_pid, 0, buf); | |
1551 | ||
1552 | if (res == 0) | |
1553 | { | |
1554 | /* Then overlay our cached registers on that. */ | |
1555 | regset->fill_function (buf); | |
1556 | ||
1557 | /* Only now do we write the register set. */ | |
1558 | res = ptrace (regset->set_request, inferior_pid, 0, buf); | |
1559 | } | |
1560 | ||
58caa3dc DJ |
1561 | if (res < 0) |
1562 | { | |
1563 | if (errno == EIO) | |
1564 | { | |
1565 | /* If we get EIO on the first regset, do not try regsets again. | |
1566 | If we get EIO on a later regset, disable that regset. */ | |
1567 | if (regset == target_regsets) | |
1568 | { | |
1569 | use_regsets_p = 0; | |
1570 | return -1; | |
1571 | } | |
1572 | else | |
1573 | { | |
1574 | regset->size = 0; | |
1575 | continue; | |
1576 | } | |
1577 | } | |
1578 | else | |
1579 | { | |
ce3a066d | 1580 | perror ("Warning: ptrace(regsets_store_inferior_registers)"); |
58caa3dc DJ |
1581 | } |
1582 | } | |
e9d25b98 DJ |
1583 | else if (regset->type == GENERAL_REGS) |
1584 | saw_general_regs = 1; | |
58caa3dc | 1585 | regset ++; |
09ec9b38 | 1586 | free (buf); |
58caa3dc | 1587 | } |
e9d25b98 DJ |
1588 | if (saw_general_regs) |
1589 | return 0; | |
1590 | else | |
1591 | return 1; | |
ce3a066d | 1592 | return 0; |
58caa3dc DJ |
1593 | } |
1594 | ||
1595 | #endif /* HAVE_LINUX_REGSETS */ | |
1596 | ||
1597 | ||
1598 | void | |
ce3a066d | 1599 | linux_fetch_registers (int regno) |
58caa3dc DJ |
1600 | { |
1601 | #ifdef HAVE_LINUX_REGSETS | |
1602 | if (use_regsets_p) | |
1603 | { | |
1604 | if (regsets_fetch_inferior_registers () == 0) | |
1605 | return; | |
1606 | } | |
1607 | #endif | |
1608 | #ifdef HAVE_LINUX_USRREGS | |
1609 | usr_fetch_inferior_registers (regno); | |
1610 | #endif | |
1611 | } | |
1612 | ||
1613 | void | |
ce3a066d | 1614 | linux_store_registers (int regno) |
58caa3dc DJ |
1615 | { |
1616 | #ifdef HAVE_LINUX_REGSETS | |
1617 | if (use_regsets_p) | |
1618 | { | |
1619 | if (regsets_store_inferior_registers () == 0) | |
1620 | return; | |
1621 | } | |
1622 | #endif | |
1623 | #ifdef HAVE_LINUX_USRREGS | |
1624 | usr_store_inferior_registers (regno); | |
1625 | #endif | |
1626 | } | |
1627 | ||
da6d8c04 | 1628 | |
da6d8c04 DJ |
1629 | /* Copy LEN bytes from inferior's memory starting at MEMADDR |
1630 | to debugger memory starting at MYADDR. */ | |
1631 | ||
c3e735a6 | 1632 | static int |
f450004a | 1633 | linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
da6d8c04 DJ |
1634 | { |
1635 | register int i; | |
1636 | /* Round starting address down to longword boundary. */ | |
1637 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
1638 | /* Round ending address up; get number of longwords that makes. */ | |
aa691b87 RM |
1639 | register int count |
1640 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) | |
da6d8c04 DJ |
1641 | / sizeof (PTRACE_XFER_TYPE); |
1642 | /* Allocate buffer of that many longwords. */ | |
aa691b87 | 1643 | register PTRACE_XFER_TYPE *buffer |
da6d8c04 | 1644 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); |
fd462a61 DJ |
1645 | int fd; |
1646 | char filename[64]; | |
1647 | ||
1648 | /* Try using /proc. Don't bother for one word. */ | |
1649 | if (len >= 3 * sizeof (long)) | |
1650 | { | |
1651 | /* We could keep this file open and cache it - possibly one per | |
1652 | thread. That requires some juggling, but is even faster. */ | |
1653 | sprintf (filename, "/proc/%ld/mem", inferior_pid); | |
1654 | fd = open (filename, O_RDONLY | O_LARGEFILE); | |
1655 | if (fd == -1) | |
1656 | goto no_proc; | |
1657 | ||
1658 | /* If pread64 is available, use it. It's faster if the kernel | |
1659 | supports it (only one syscall), and it's 64-bit safe even on | |
1660 | 32-bit platforms (for instance, SPARC debugging a SPARC64 | |
1661 | application). */ | |
1662 | #ifdef HAVE_PREAD64 | |
1663 | if (pread64 (fd, myaddr, len, memaddr) != len) | |
1664 | #else | |
1665 | if (lseek (fd, memaddr, SEEK_SET) == -1 || read (fd, memaddr, len) != len) | |
1666 | #endif | |
1667 | { | |
1668 | close (fd); | |
1669 | goto no_proc; | |
1670 | } | |
1671 | ||
1672 | close (fd); | |
1673 | return 0; | |
1674 | } | |
da6d8c04 | 1675 | |
fd462a61 | 1676 | no_proc: |
da6d8c04 DJ |
1677 | /* Read all the longwords */ |
1678 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
1679 | { | |
c3e735a6 | 1680 | errno = 0; |
d844cde6 | 1681 | buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0); |
c3e735a6 DJ |
1682 | if (errno) |
1683 | return errno; | |
da6d8c04 DJ |
1684 | } |
1685 | ||
1686 | /* Copy appropriate bytes out of the buffer. */ | |
1687 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len); | |
c3e735a6 DJ |
1688 | |
1689 | return 0; | |
da6d8c04 DJ |
1690 | } |
1691 | ||
1692 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
1693 | to inferior's memory at MEMADDR. | |
1694 | On failure (cannot write the inferior) | |
1695 | returns the value of errno. */ | |
1696 | ||
ce3a066d | 1697 | static int |
f450004a | 1698 | linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) |
da6d8c04 DJ |
1699 | { |
1700 | register int i; | |
1701 | /* Round starting address down to longword boundary. */ | |
1702 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
1703 | /* Round ending address up; get number of longwords that makes. */ | |
1704 | register int count | |
1705 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE); | |
1706 | /* Allocate buffer of that many longwords. */ | |
1707 | register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); | |
1708 | extern int errno; | |
1709 | ||
0d62e5e8 DJ |
1710 | if (debug_threads) |
1711 | { | |
1712 | fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr); | |
1713 | } | |
1714 | ||
da6d8c04 DJ |
1715 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
1716 | ||
d844cde6 DJ |
1717 | buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, |
1718 | (PTRACE_ARG3_TYPE) addr, 0); | |
da6d8c04 DJ |
1719 | |
1720 | if (count > 1) | |
1721 | { | |
1722 | buffer[count - 1] | |
1723 | = ptrace (PTRACE_PEEKTEXT, inferior_pid, | |
d844cde6 DJ |
1724 | (PTRACE_ARG3_TYPE) (addr + (count - 1) |
1725 | * sizeof (PTRACE_XFER_TYPE)), | |
1726 | 0); | |
da6d8c04 DJ |
1727 | } |
1728 | ||
1729 | /* Copy data to be written over corresponding part of buffer */ | |
1730 | ||
1731 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len); | |
1732 | ||
1733 | /* Write the entire buffer. */ | |
1734 | ||
1735 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
1736 | { | |
1737 | errno = 0; | |
d844cde6 | 1738 | ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]); |
da6d8c04 DJ |
1739 | if (errno) |
1740 | return errno; | |
1741 | } | |
1742 | ||
1743 | return 0; | |
1744 | } | |
2f2893d9 | 1745 | |
24a09b5f DJ |
1746 | static int linux_supports_tracefork_flag; |
1747 | ||
51c2684e | 1748 | /* Helper functions for linux_test_for_tracefork, called via clone (). */ |
24a09b5f | 1749 | |
51c2684e DJ |
1750 | static int |
1751 | linux_tracefork_grandchild (void *arg) | |
1752 | { | |
1753 | _exit (0); | |
1754 | } | |
1755 | ||
7407e2de AS |
1756 | #define STACK_SIZE 4096 |
1757 | ||
51c2684e DJ |
1758 | static int |
1759 | linux_tracefork_child (void *arg) | |
24a09b5f DJ |
1760 | { |
1761 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
1762 | kill (getpid (), SIGSTOP); | |
7407e2de AS |
1763 | #ifdef __ia64__ |
1764 | __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE, | |
1765 | CLONE_VM | SIGCHLD, NULL); | |
1766 | #else | |
1767 | clone (linux_tracefork_grandchild, arg + STACK_SIZE, | |
1768 | CLONE_VM | SIGCHLD, NULL); | |
1769 | #endif | |
24a09b5f DJ |
1770 | _exit (0); |
1771 | } | |
1772 | ||
1773 | /* Wrapper function for waitpid which handles EINTR. */ | |
1774 | ||
1775 | static int | |
1776 | my_waitpid (int pid, int *status, int flags) | |
1777 | { | |
1778 | int ret; | |
1779 | do | |
1780 | { | |
1781 | ret = waitpid (pid, status, flags); | |
1782 | } | |
1783 | while (ret == -1 && errno == EINTR); | |
1784 | ||
1785 | return ret; | |
1786 | } | |
1787 | ||
1788 | /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make | |
1789 | sure that we can enable the option, and that it had the desired | |
1790 | effect. */ | |
1791 | ||
1792 | static void | |
1793 | linux_test_for_tracefork (void) | |
1794 | { | |
1795 | int child_pid, ret, status; | |
1796 | long second_pid; | |
7407e2de | 1797 | char *stack = malloc (STACK_SIZE * 4); |
24a09b5f DJ |
1798 | |
1799 | linux_supports_tracefork_flag = 0; | |
1800 | ||
51c2684e | 1801 | /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */ |
7407e2de AS |
1802 | #ifdef __ia64__ |
1803 | child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE, | |
1804 | CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2); | |
1805 | #else | |
1806 | child_pid = clone (linux_tracefork_child, stack + STACK_SIZE, | |
1807 | CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2); | |
1808 | #endif | |
24a09b5f | 1809 | if (child_pid == -1) |
51c2684e | 1810 | perror_with_name ("clone"); |
24a09b5f DJ |
1811 | |
1812 | ret = my_waitpid (child_pid, &status, 0); | |
1813 | if (ret == -1) | |
1814 | perror_with_name ("waitpid"); | |
1815 | else if (ret != child_pid) | |
1816 | error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret); | |
1817 | if (! WIFSTOPPED (status)) | |
1818 | error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status); | |
1819 | ||
1820 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK); | |
1821 | if (ret != 0) | |
1822 | { | |
1823 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); | |
1824 | if (ret != 0) | |
1825 | { | |
1826 | warning ("linux_test_for_tracefork: failed to kill child"); | |
1827 | return; | |
1828 | } | |
1829 | ||
1830 | ret = my_waitpid (child_pid, &status, 0); | |
1831 | if (ret != child_pid) | |
1832 | warning ("linux_test_for_tracefork: failed to wait for killed child"); | |
1833 | else if (!WIFSIGNALED (status)) | |
1834 | warning ("linux_test_for_tracefork: unexpected wait status 0x%x from " | |
1835 | "killed child", status); | |
1836 | ||
1837 | return; | |
1838 | } | |
1839 | ||
1840 | ret = ptrace (PTRACE_CONT, child_pid, 0, 0); | |
1841 | if (ret != 0) | |
1842 | warning ("linux_test_for_tracefork: failed to resume child"); | |
1843 | ||
1844 | ret = my_waitpid (child_pid, &status, 0); | |
1845 | ||
1846 | if (ret == child_pid && WIFSTOPPED (status) | |
1847 | && status >> 16 == PTRACE_EVENT_FORK) | |
1848 | { | |
1849 | second_pid = 0; | |
1850 | ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid); | |
1851 | if (ret == 0 && second_pid != 0) | |
1852 | { | |
1853 | int second_status; | |
1854 | ||
1855 | linux_supports_tracefork_flag = 1; | |
1856 | my_waitpid (second_pid, &second_status, 0); | |
1857 | ret = ptrace (PTRACE_KILL, second_pid, 0, 0); | |
1858 | if (ret != 0) | |
1859 | warning ("linux_test_for_tracefork: failed to kill second child"); | |
1860 | my_waitpid (second_pid, &status, 0); | |
1861 | } | |
1862 | } | |
1863 | else | |
1864 | warning ("linux_test_for_tracefork: unexpected result from waitpid " | |
1865 | "(%d, status 0x%x)", ret, status); | |
1866 | ||
1867 | do | |
1868 | { | |
1869 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); | |
1870 | if (ret != 0) | |
1871 | warning ("linux_test_for_tracefork: failed to kill child"); | |
1872 | my_waitpid (child_pid, &status, 0); | |
1873 | } | |
1874 | while (WIFSTOPPED (status)); | |
51c2684e DJ |
1875 | |
1876 | free (stack); | |
24a09b5f DJ |
1877 | } |
1878 | ||
1879 | ||
2f2893d9 DJ |
1880 | static void |
1881 | linux_look_up_symbols (void) | |
1882 | { | |
0d62e5e8 | 1883 | #ifdef USE_THREAD_DB |
24a09b5f | 1884 | if (thread_db_active) |
0d62e5e8 DJ |
1885 | return; |
1886 | ||
24a09b5f | 1887 | thread_db_active = thread_db_init (!linux_supports_tracefork_flag); |
0d62e5e8 DJ |
1888 | #endif |
1889 | } | |
1890 | ||
e5379b03 | 1891 | static void |
ef57601b | 1892 | linux_request_interrupt (void) |
e5379b03 | 1893 | { |
a1928bad | 1894 | extern unsigned long signal_pid; |
e5379b03 | 1895 | |
d592fa2f | 1896 | if (cont_thread != 0 && cont_thread != -1) |
e5379b03 DJ |
1897 | { |
1898 | struct process_info *process; | |
1899 | ||
1900 | process = get_thread_process (current_inferior); | |
ef57601b | 1901 | kill_lwp (process->lwpid, SIGINT); |
e5379b03 DJ |
1902 | } |
1903 | else | |
ef57601b | 1904 | kill_lwp (signal_pid, SIGINT); |
e5379b03 DJ |
1905 | } |
1906 | ||
aa691b87 RM |
1907 | /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET |
1908 | to debugger memory starting at MYADDR. */ | |
1909 | ||
1910 | static int | |
f450004a | 1911 | linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len) |
aa691b87 RM |
1912 | { |
1913 | char filename[PATH_MAX]; | |
1914 | int fd, n; | |
1915 | ||
a1928bad | 1916 | snprintf (filename, sizeof filename, "/proc/%ld/auxv", inferior_pid); |
aa691b87 RM |
1917 | |
1918 | fd = open (filename, O_RDONLY); | |
1919 | if (fd < 0) | |
1920 | return -1; | |
1921 | ||
1922 | if (offset != (CORE_ADDR) 0 | |
1923 | && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset) | |
1924 | n = -1; | |
1925 | else | |
1926 | n = read (fd, myaddr, len); | |
1927 | ||
1928 | close (fd); | |
1929 | ||
1930 | return n; | |
1931 | } | |
1932 | ||
e013ee27 OF |
1933 | /* These watchpoint related wrapper functions simply pass on the function call |
1934 | if the target has registered a corresponding function. */ | |
1935 | ||
1936 | static int | |
1937 | linux_insert_watchpoint (char type, CORE_ADDR addr, int len) | |
1938 | { | |
1939 | if (the_low_target.insert_watchpoint != NULL) | |
1940 | return the_low_target.insert_watchpoint (type, addr, len); | |
1941 | else | |
1942 | /* Unsupported (see target.h). */ | |
1943 | return 1; | |
1944 | } | |
1945 | ||
1946 | static int | |
1947 | linux_remove_watchpoint (char type, CORE_ADDR addr, int len) | |
1948 | { | |
1949 | if (the_low_target.remove_watchpoint != NULL) | |
1950 | return the_low_target.remove_watchpoint (type, addr, len); | |
1951 | else | |
1952 | /* Unsupported (see target.h). */ | |
1953 | return 1; | |
1954 | } | |
1955 | ||
1956 | static int | |
1957 | linux_stopped_by_watchpoint (void) | |
1958 | { | |
1959 | if (the_low_target.stopped_by_watchpoint != NULL) | |
1960 | return the_low_target.stopped_by_watchpoint (); | |
1961 | else | |
1962 | return 0; | |
1963 | } | |
1964 | ||
1965 | static CORE_ADDR | |
1966 | linux_stopped_data_address (void) | |
1967 | { | |
1968 | if (the_low_target.stopped_data_address != NULL) | |
1969 | return the_low_target.stopped_data_address (); | |
1970 | else | |
1971 | return 0; | |
1972 | } | |
1973 | ||
42c81e2a | 1974 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) |
52fb6437 NS |
1975 | #if defined(__mcoldfire__) |
1976 | /* These should really be defined in the kernel's ptrace.h header. */ | |
1977 | #define PT_TEXT_ADDR 49*4 | |
1978 | #define PT_DATA_ADDR 50*4 | |
1979 | #define PT_TEXT_END_ADDR 51*4 | |
1980 | #endif | |
1981 | ||
1982 | /* Under uClinux, programs are loaded at non-zero offsets, which we need | |
1983 | to tell gdb about. */ | |
1984 | ||
1985 | static int | |
1986 | linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p) | |
1987 | { | |
1988 | #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR) | |
1989 | unsigned long text, text_end, data; | |
1990 | int pid = get_thread_process (current_inferior)->head.id; | |
1991 | ||
1992 | errno = 0; | |
1993 | ||
1994 | text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0); | |
1995 | text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0); | |
1996 | data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0); | |
1997 | ||
1998 | if (errno == 0) | |
1999 | { | |
2000 | /* Both text and data offsets produced at compile-time (and so | |
2001 | used by gdb) are relative to the beginning of the program, | |
2002 | with the data segment immediately following the text segment. | |
2003 | However, the actual runtime layout in memory may put the data | |
2004 | somewhere else, so when we send gdb a data base-address, we | |
2005 | use the real data base address and subtract the compile-time | |
2006 | data base-address from it (which is just the length of the | |
2007 | text segment). BSS immediately follows data in both | |
2008 | cases. */ | |
2009 | *text_p = text; | |
2010 | *data_p = data - (text_end - text); | |
2011 | ||
2012 | return 1; | |
2013 | } | |
2014 | #endif | |
2015 | return 0; | |
2016 | } | |
2017 | #endif | |
2018 | ||
23181151 DJ |
2019 | static const char * |
2020 | linux_arch_string (void) | |
2021 | { | |
2022 | return the_low_target.arch_string; | |
2023 | } | |
2024 | ||
ce3a066d DJ |
2025 | static struct target_ops linux_target_ops = { |
2026 | linux_create_inferior, | |
2027 | linux_attach, | |
2028 | linux_kill, | |
6ad8ae5c | 2029 | linux_detach, |
444d6139 | 2030 | linux_join, |
ce3a066d DJ |
2031 | linux_thread_alive, |
2032 | linux_resume, | |
2033 | linux_wait, | |
2034 | linux_fetch_registers, | |
2035 | linux_store_registers, | |
2036 | linux_read_memory, | |
2037 | linux_write_memory, | |
2f2893d9 | 2038 | linux_look_up_symbols, |
ef57601b | 2039 | linux_request_interrupt, |
aa691b87 | 2040 | linux_read_auxv, |
e013ee27 OF |
2041 | linux_insert_watchpoint, |
2042 | linux_remove_watchpoint, | |
2043 | linux_stopped_by_watchpoint, | |
2044 | linux_stopped_data_address, | |
42c81e2a | 2045 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) |
52fb6437 | 2046 | linux_read_offsets, |
dae5f5cf DJ |
2047 | #else |
2048 | NULL, | |
2049 | #endif | |
2050 | #ifdef USE_THREAD_DB | |
2051 | thread_db_get_tls_address, | |
2052 | #else | |
2053 | NULL, | |
52fb6437 | 2054 | #endif |
23181151 | 2055 | linux_arch_string, |
59a016f0 PA |
2056 | NULL, |
2057 | hostio_last_error_from_errno, | |
ce3a066d DJ |
2058 | }; |
2059 | ||
0d62e5e8 DJ |
2060 | static void |
2061 | linux_init_signals () | |
2062 | { | |
2063 | /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads | |
2064 | to find what the cancel signal actually is. */ | |
254787d4 | 2065 | signal (__SIGRTMIN+1, SIG_IGN); |
0d62e5e8 DJ |
2066 | } |
2067 | ||
da6d8c04 DJ |
2068 | void |
2069 | initialize_low (void) | |
2070 | { | |
24a09b5f | 2071 | thread_db_active = 0; |
ce3a066d | 2072 | set_target_ops (&linux_target_ops); |
611cb4a5 DJ |
2073 | set_breakpoint_data (the_low_target.breakpoint, |
2074 | the_low_target.breakpoint_len); | |
0a30fbc4 | 2075 | init_registers (); |
0d62e5e8 | 2076 | linux_init_signals (); |
24a09b5f | 2077 | linux_test_for_tracefork (); |
da6d8c04 | 2078 | } |