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