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