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, |
4c38e0a4 | 3 | 2006, 2007, 2008, 2009, 2010 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> | |
efcbbd14 UW |
40 | #include <sys/stat.h> |
41 | #include <sys/vfs.h> | |
957f3f49 DE |
42 | #ifndef ELFMAG0 |
43 | /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h | |
44 | then ELFMAG0 will have been defined. If it didn't get included by | |
45 | gdb_proc_service.h then including it will likely introduce a duplicate | |
46 | definition of elf_fpregset_t. */ | |
47 | #include <elf.h> | |
48 | #endif | |
efcbbd14 UW |
49 | |
50 | #ifndef SPUFS_MAGIC | |
51 | #define SPUFS_MAGIC 0x23c9b64e | |
52 | #endif | |
da6d8c04 | 53 | |
32ca6d61 DJ |
54 | #ifndef PTRACE_GETSIGINFO |
55 | # define PTRACE_GETSIGINFO 0x4202 | |
56 | # define PTRACE_SETSIGINFO 0x4203 | |
57 | #endif | |
58 | ||
fd462a61 DJ |
59 | #ifndef O_LARGEFILE |
60 | #define O_LARGEFILE 0 | |
61 | #endif | |
62 | ||
24a09b5f DJ |
63 | /* If the system headers did not provide the constants, hard-code the normal |
64 | values. */ | |
65 | #ifndef PTRACE_EVENT_FORK | |
66 | ||
67 | #define PTRACE_SETOPTIONS 0x4200 | |
68 | #define PTRACE_GETEVENTMSG 0x4201 | |
69 | ||
70 | /* options set using PTRACE_SETOPTIONS */ | |
71 | #define PTRACE_O_TRACESYSGOOD 0x00000001 | |
72 | #define PTRACE_O_TRACEFORK 0x00000002 | |
73 | #define PTRACE_O_TRACEVFORK 0x00000004 | |
74 | #define PTRACE_O_TRACECLONE 0x00000008 | |
75 | #define PTRACE_O_TRACEEXEC 0x00000010 | |
76 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 | |
77 | #define PTRACE_O_TRACEEXIT 0x00000040 | |
78 | ||
79 | /* Wait extended result codes for the above trace options. */ | |
80 | #define PTRACE_EVENT_FORK 1 | |
81 | #define PTRACE_EVENT_VFORK 2 | |
82 | #define PTRACE_EVENT_CLONE 3 | |
83 | #define PTRACE_EVENT_EXEC 4 | |
84 | #define PTRACE_EVENT_VFORK_DONE 5 | |
85 | #define PTRACE_EVENT_EXIT 6 | |
86 | ||
87 | #endif /* PTRACE_EVENT_FORK */ | |
88 | ||
89 | /* We can't always assume that this flag is available, but all systems | |
90 | with the ptrace event handlers also have __WALL, so it's safe to use | |
91 | in some contexts. */ | |
92 | #ifndef __WALL | |
93 | #define __WALL 0x40000000 /* Wait for any child. */ | |
94 | #endif | |
95 | ||
ec8ebe72 DE |
96 | #ifndef W_STOPCODE |
97 | #define W_STOPCODE(sig) ((sig) << 8 | 0x7f) | |
98 | #endif | |
99 | ||
42c81e2a DJ |
100 | #ifdef __UCLIBC__ |
101 | #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__)) | |
102 | #define HAS_NOMMU | |
103 | #endif | |
104 | #endif | |
105 | ||
24a09b5f DJ |
106 | /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol |
107 | representation of the thread ID. | |
611cb4a5 | 108 | |
54a0b537 | 109 | ``all_lwps'' is keyed by the process ID - which on Linux is (presently) |
95954743 PA |
110 | the same as the LWP ID. |
111 | ||
112 | ``all_processes'' is keyed by the "overall process ID", which | |
113 | GNU/Linux calls tgid, "thread group ID". */ | |
0d62e5e8 | 114 | |
54a0b537 | 115 | struct inferior_list all_lwps; |
0d62e5e8 | 116 | |
24a09b5f DJ |
117 | /* A list of all unknown processes which receive stop signals. Some other |
118 | process will presumably claim each of these as forked children | |
119 | momentarily. */ | |
120 | ||
121 | struct inferior_list stopped_pids; | |
122 | ||
0d62e5e8 DJ |
123 | /* FIXME this is a bit of a hack, and could be removed. */ |
124 | int stopping_threads; | |
125 | ||
126 | /* FIXME make into a target method? */ | |
24a09b5f | 127 | int using_threads = 1; |
24a09b5f | 128 | |
95954743 PA |
129 | /* This flag is true iff we've just created or attached to our first |
130 | inferior but it has not stopped yet. As soon as it does, we need | |
131 | to call the low target's arch_setup callback. Doing this only on | |
132 | the first inferior avoids reinializing the architecture on every | |
133 | inferior, and avoids messing with the register caches of the | |
134 | already running inferiors. NOTE: this assumes all inferiors under | |
135 | control of gdbserver have the same architecture. */ | |
d61ddec4 UW |
136 | static int new_inferior; |
137 | ||
2acc282a | 138 | static void linux_resume_one_lwp (struct lwp_info *lwp, |
54a0b537 | 139 | int step, int signal, siginfo_t *info); |
2bd7c093 | 140 | static void linux_resume (struct thread_resume *resume_info, size_t n); |
54a0b537 | 141 | static void stop_all_lwps (void); |
95954743 | 142 | static int linux_wait_for_event (ptid_t ptid, int *wstat, int options); |
54a0b537 | 143 | static int check_removed_breakpoint (struct lwp_info *event_child); |
95954743 | 144 | static void *add_lwp (ptid_t ptid); |
c35fafde | 145 | static int linux_stopped_by_watchpoint (void); |
95954743 | 146 | static void mark_lwp_dead (struct lwp_info *lwp, int wstat); |
dc146f7c | 147 | static int linux_core_of_thread (ptid_t ptid); |
0d62e5e8 DJ |
148 | |
149 | struct pending_signals | |
150 | { | |
151 | int signal; | |
32ca6d61 | 152 | siginfo_t info; |
0d62e5e8 DJ |
153 | struct pending_signals *prev; |
154 | }; | |
611cb4a5 | 155 | |
d844cde6 | 156 | #define PTRACE_ARG3_TYPE long |
c6ecbae5 | 157 | #define PTRACE_XFER_TYPE long |
da6d8c04 | 158 | |
58caa3dc | 159 | #ifdef HAVE_LINUX_REGSETS |
52fa2412 UW |
160 | static char *disabled_regsets; |
161 | static int num_regsets; | |
58caa3dc DJ |
162 | #endif |
163 | ||
bd99dc85 PA |
164 | /* The read/write ends of the pipe registered as waitable file in the |
165 | event loop. */ | |
166 | static int linux_event_pipe[2] = { -1, -1 }; | |
167 | ||
168 | /* True if we're currently in async mode. */ | |
169 | #define target_is_async_p() (linux_event_pipe[0] != -1) | |
170 | ||
171 | static void send_sigstop (struct inferior_list_entry *entry); | |
172 | static void wait_for_sigstop (struct inferior_list_entry *entry); | |
173 | ||
d0722149 DE |
174 | /* Accepts an integer PID; Returns a string representing a file that |
175 | can be opened to get info for the child process. | |
176 | Space for the result is malloc'd, caller must free. */ | |
177 | ||
178 | char * | |
179 | linux_child_pid_to_exec_file (int pid) | |
180 | { | |
181 | char *name1, *name2; | |
182 | ||
183 | name1 = xmalloc (MAXPATHLEN); | |
184 | name2 = xmalloc (MAXPATHLEN); | |
185 | memset (name2, 0, MAXPATHLEN); | |
186 | ||
187 | sprintf (name1, "/proc/%d/exe", pid); | |
188 | if (readlink (name1, name2, MAXPATHLEN) > 0) | |
189 | { | |
190 | free (name1); | |
191 | return name2; | |
192 | } | |
193 | else | |
194 | { | |
195 | free (name2); | |
196 | return name1; | |
197 | } | |
198 | } | |
199 | ||
200 | /* Return non-zero if HEADER is a 64-bit ELF file. */ | |
201 | ||
202 | static int | |
957f3f49 | 203 | elf_64_header_p (const Elf64_Ehdr *header) |
d0722149 DE |
204 | { |
205 | return (header->e_ident[EI_MAG0] == ELFMAG0 | |
206 | && header->e_ident[EI_MAG1] == ELFMAG1 | |
207 | && header->e_ident[EI_MAG2] == ELFMAG2 | |
208 | && header->e_ident[EI_MAG3] == ELFMAG3 | |
209 | && header->e_ident[EI_CLASS] == ELFCLASS64); | |
210 | } | |
211 | ||
212 | /* Return non-zero if FILE is a 64-bit ELF file, | |
213 | zero if the file is not a 64-bit ELF file, | |
214 | and -1 if the file is not accessible or doesn't exist. */ | |
215 | ||
216 | int | |
217 | elf_64_file_p (const char *file) | |
218 | { | |
957f3f49 | 219 | Elf64_Ehdr header; |
d0722149 DE |
220 | int fd; |
221 | ||
222 | fd = open (file, O_RDONLY); | |
223 | if (fd < 0) | |
224 | return -1; | |
225 | ||
226 | if (read (fd, &header, sizeof (header)) != sizeof (header)) | |
227 | { | |
228 | close (fd); | |
229 | return 0; | |
230 | } | |
231 | close (fd); | |
232 | ||
233 | return elf_64_header_p (&header); | |
234 | } | |
235 | ||
bd99dc85 PA |
236 | static void |
237 | delete_lwp (struct lwp_info *lwp) | |
238 | { | |
239 | remove_thread (get_lwp_thread (lwp)); | |
240 | remove_inferior (&all_lwps, &lwp->head); | |
aa5ca48f | 241 | free (lwp->arch_private); |
bd99dc85 PA |
242 | free (lwp); |
243 | } | |
244 | ||
95954743 PA |
245 | /* Add a process to the common process list, and set its private |
246 | data. */ | |
247 | ||
248 | static struct process_info * | |
249 | linux_add_process (int pid, int attached) | |
250 | { | |
251 | struct process_info *proc; | |
252 | ||
253 | /* Is this the first process? If so, then set the arch. */ | |
254 | if (all_processes.head == NULL) | |
255 | new_inferior = 1; | |
256 | ||
257 | proc = add_process (pid, attached); | |
258 | proc->private = xcalloc (1, sizeof (*proc->private)); | |
259 | ||
aa5ca48f DE |
260 | if (the_low_target.new_process != NULL) |
261 | proc->private->arch_private = the_low_target.new_process (); | |
262 | ||
95954743 PA |
263 | return proc; |
264 | } | |
265 | ||
5091eb23 DE |
266 | /* Remove a process from the common process list, |
267 | also freeing all private data. */ | |
268 | ||
269 | static void | |
ca5c370d | 270 | linux_remove_process (struct process_info *process) |
5091eb23 | 271 | { |
cdbfd419 PP |
272 | struct process_info_private *priv = process->private; |
273 | ||
cdbfd419 PP |
274 | free (priv->arch_private); |
275 | free (priv); | |
5091eb23 DE |
276 | remove_process (process); |
277 | } | |
278 | ||
07d4f67e DE |
279 | /* Wrapper function for waitpid which handles EINTR, and emulates |
280 | __WALL for systems where that is not available. */ | |
281 | ||
282 | static int | |
283 | my_waitpid (int pid, int *status, int flags) | |
284 | { | |
285 | int ret, out_errno; | |
286 | ||
287 | if (debug_threads) | |
288 | fprintf (stderr, "my_waitpid (%d, 0x%x)\n", pid, flags); | |
289 | ||
290 | if (flags & __WALL) | |
291 | { | |
292 | sigset_t block_mask, org_mask, wake_mask; | |
293 | int wnohang; | |
294 | ||
295 | wnohang = (flags & WNOHANG) != 0; | |
296 | flags &= ~(__WALL | __WCLONE); | |
297 | flags |= WNOHANG; | |
298 | ||
299 | /* Block all signals while here. This avoids knowing about | |
300 | LinuxThread's signals. */ | |
301 | sigfillset (&block_mask); | |
302 | sigprocmask (SIG_BLOCK, &block_mask, &org_mask); | |
303 | ||
304 | /* ... except during the sigsuspend below. */ | |
305 | sigemptyset (&wake_mask); | |
306 | ||
307 | while (1) | |
308 | { | |
309 | /* Since all signals are blocked, there's no need to check | |
310 | for EINTR here. */ | |
311 | ret = waitpid (pid, status, flags); | |
312 | out_errno = errno; | |
313 | ||
314 | if (ret == -1 && out_errno != ECHILD) | |
315 | break; | |
316 | else if (ret > 0) | |
317 | break; | |
318 | ||
319 | if (flags & __WCLONE) | |
320 | { | |
321 | /* We've tried both flavors now. If WNOHANG is set, | |
322 | there's nothing else to do, just bail out. */ | |
323 | if (wnohang) | |
324 | break; | |
325 | ||
326 | if (debug_threads) | |
327 | fprintf (stderr, "blocking\n"); | |
328 | ||
329 | /* Block waiting for signals. */ | |
330 | sigsuspend (&wake_mask); | |
331 | } | |
332 | ||
333 | flags ^= __WCLONE; | |
334 | } | |
335 | ||
336 | sigprocmask (SIG_SETMASK, &org_mask, NULL); | |
337 | } | |
338 | else | |
339 | { | |
340 | do | |
341 | ret = waitpid (pid, status, flags); | |
342 | while (ret == -1 && errno == EINTR); | |
343 | out_errno = errno; | |
344 | } | |
345 | ||
346 | if (debug_threads) | |
347 | fprintf (stderr, "my_waitpid (%d, 0x%x): status(%x), %d\n", | |
348 | pid, flags, status ? *status : -1, ret); | |
349 | ||
350 | errno = out_errno; | |
351 | return ret; | |
352 | } | |
353 | ||
bd99dc85 PA |
354 | /* Handle a GNU/Linux extended wait response. If we see a clone |
355 | event, we need to add the new LWP to our list (and not report the | |
356 | trap to higher layers). */ | |
0d62e5e8 | 357 | |
24a09b5f | 358 | static void |
54a0b537 | 359 | handle_extended_wait (struct lwp_info *event_child, int wstat) |
24a09b5f DJ |
360 | { |
361 | int event = wstat >> 16; | |
54a0b537 | 362 | struct lwp_info *new_lwp; |
24a09b5f DJ |
363 | |
364 | if (event == PTRACE_EVENT_CLONE) | |
365 | { | |
95954743 | 366 | ptid_t ptid; |
24a09b5f | 367 | unsigned long new_pid; |
836acd6d | 368 | int ret, status = W_STOPCODE (SIGSTOP); |
24a09b5f | 369 | |
bd99dc85 | 370 | ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_child), 0, &new_pid); |
24a09b5f DJ |
371 | |
372 | /* If we haven't already seen the new PID stop, wait for it now. */ | |
373 | if (! pull_pid_from_list (&stopped_pids, new_pid)) | |
374 | { | |
375 | /* The new child has a pending SIGSTOP. We can't affect it until it | |
376 | hits the SIGSTOP, but we're already attached. */ | |
377 | ||
97438e3f | 378 | ret = my_waitpid (new_pid, &status, __WALL); |
24a09b5f DJ |
379 | |
380 | if (ret == -1) | |
381 | perror_with_name ("waiting for new child"); | |
382 | else if (ret != new_pid) | |
383 | warning ("wait returned unexpected PID %d", ret); | |
da5898ce | 384 | else if (!WIFSTOPPED (status)) |
24a09b5f DJ |
385 | warning ("wait returned unexpected status 0x%x", status); |
386 | } | |
387 | ||
388 | ptrace (PTRACE_SETOPTIONS, new_pid, 0, PTRACE_O_TRACECLONE); | |
389 | ||
95954743 PA |
390 | ptid = ptid_build (pid_of (event_child), new_pid, 0); |
391 | new_lwp = (struct lwp_info *) add_lwp (ptid); | |
392 | add_thread (ptid, new_lwp); | |
24a09b5f | 393 | |
e27d73f6 DE |
394 | /* Either we're going to immediately resume the new thread |
395 | or leave it stopped. linux_resume_one_lwp is a nop if it | |
396 | thinks the thread is currently running, so set this first | |
397 | before calling linux_resume_one_lwp. */ | |
398 | new_lwp->stopped = 1; | |
399 | ||
da5898ce DJ |
400 | /* Normally we will get the pending SIGSTOP. But in some cases |
401 | we might get another signal delivered to the group first. | |
f21cc1a2 | 402 | If we do get another signal, be sure not to lose it. */ |
da5898ce DJ |
403 | if (WSTOPSIG (status) == SIGSTOP) |
404 | { | |
e27d73f6 DE |
405 | if (! stopping_threads) |
406 | linux_resume_one_lwp (new_lwp, 0, 0, NULL); | |
da5898ce | 407 | } |
24a09b5f | 408 | else |
da5898ce | 409 | { |
54a0b537 | 410 | new_lwp->stop_expected = 1; |
da5898ce DJ |
411 | if (stopping_threads) |
412 | { | |
54a0b537 PA |
413 | new_lwp->status_pending_p = 1; |
414 | new_lwp->status_pending = status; | |
da5898ce DJ |
415 | } |
416 | else | |
417 | /* Pass the signal on. This is what GDB does - except | |
418 | shouldn't we really report it instead? */ | |
e27d73f6 | 419 | linux_resume_one_lwp (new_lwp, 0, WSTOPSIG (status), NULL); |
da5898ce | 420 | } |
24a09b5f DJ |
421 | |
422 | /* Always resume the current thread. If we are stopping | |
423 | threads, it will have a pending SIGSTOP; we may as well | |
424 | collect it now. */ | |
2acc282a | 425 | linux_resume_one_lwp (event_child, event_child->stepping, 0, NULL); |
24a09b5f DJ |
426 | } |
427 | } | |
428 | ||
0d62e5e8 DJ |
429 | /* This function should only be called if the process got a SIGTRAP. |
430 | The SIGTRAP could mean several things. | |
431 | ||
432 | On i386, where decr_pc_after_break is non-zero: | |
433 | If we were single-stepping this process using PTRACE_SINGLESTEP, | |
434 | we will get only the one SIGTRAP (even if the instruction we | |
435 | stepped over was a breakpoint). The value of $eip will be the | |
436 | next instruction. | |
437 | If we continue the process using PTRACE_CONT, we will get a | |
438 | SIGTRAP when we hit a breakpoint. The value of $eip will be | |
439 | the instruction after the breakpoint (i.e. needs to be | |
440 | decremented). If we report the SIGTRAP to GDB, we must also | |
441 | report the undecremented PC. If we cancel the SIGTRAP, we | |
442 | must resume at the decremented PC. | |
443 | ||
444 | (Presumably, not yet tested) On a non-decr_pc_after_break machine | |
445 | with hardware or kernel single-step: | |
446 | If we single-step over a breakpoint instruction, our PC will | |
447 | point at the following instruction. If we continue and hit a | |
448 | breakpoint instruction, our PC will point at the breakpoint | |
449 | instruction. */ | |
450 | ||
451 | static CORE_ADDR | |
452 | get_stop_pc (void) | |
453 | { | |
442ea881 PA |
454 | struct regcache *regcache = get_thread_regcache (current_inferior, 1); |
455 | CORE_ADDR stop_pc = (*the_low_target.get_pc) (regcache); | |
0d62e5e8 | 456 | |
47c0c975 DE |
457 | if (! get_thread_lwp (current_inferior)->stepping) |
458 | stop_pc -= the_low_target.decr_pc_after_break; | |
459 | ||
460 | if (debug_threads) | |
461 | fprintf (stderr, "stop pc is 0x%lx\n", (long) stop_pc); | |
462 | ||
463 | return stop_pc; | |
0d62e5e8 | 464 | } |
ce3a066d | 465 | |
0d62e5e8 | 466 | static void * |
95954743 | 467 | add_lwp (ptid_t ptid) |
611cb4a5 | 468 | { |
54a0b537 | 469 | struct lwp_info *lwp; |
0d62e5e8 | 470 | |
54a0b537 PA |
471 | lwp = (struct lwp_info *) xmalloc (sizeof (*lwp)); |
472 | memset (lwp, 0, sizeof (*lwp)); | |
0d62e5e8 | 473 | |
95954743 | 474 | lwp->head.id = ptid; |
0d62e5e8 | 475 | |
aa5ca48f DE |
476 | if (the_low_target.new_thread != NULL) |
477 | lwp->arch_private = the_low_target.new_thread (); | |
478 | ||
54a0b537 | 479 | add_inferior_to_list (&all_lwps, &lwp->head); |
0d62e5e8 | 480 | |
54a0b537 | 481 | return lwp; |
0d62e5e8 | 482 | } |
611cb4a5 | 483 | |
da6d8c04 DJ |
484 | /* Start an inferior process and returns its pid. |
485 | ALLARGS is a vector of program-name and args. */ | |
486 | ||
ce3a066d DJ |
487 | static int |
488 | linux_create_inferior (char *program, char **allargs) | |
da6d8c04 | 489 | { |
a6dbe5df | 490 | struct lwp_info *new_lwp; |
da6d8c04 | 491 | int pid; |
95954743 | 492 | ptid_t ptid; |
da6d8c04 | 493 | |
42c81e2a | 494 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) |
52fb6437 NS |
495 | pid = vfork (); |
496 | #else | |
da6d8c04 | 497 | pid = fork (); |
52fb6437 | 498 | #endif |
da6d8c04 DJ |
499 | if (pid < 0) |
500 | perror_with_name ("fork"); | |
501 | ||
502 | if (pid == 0) | |
503 | { | |
504 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
505 | ||
60c3d7b0 | 506 | #ifdef __SIGRTMIN /* Bionic doesn't use SIGRTMIN the way glibc does. */ |
254787d4 | 507 | signal (__SIGRTMIN + 1, SIG_DFL); |
60c3d7b0 | 508 | #endif |
0d62e5e8 | 509 | |
a9fa9f7d DJ |
510 | setpgid (0, 0); |
511 | ||
2b876972 DJ |
512 | execv (program, allargs); |
513 | if (errno == ENOENT) | |
514 | execvp (program, allargs); | |
da6d8c04 DJ |
515 | |
516 | fprintf (stderr, "Cannot exec %s: %s.\n", program, | |
d07c63e7 | 517 | strerror (errno)); |
da6d8c04 DJ |
518 | fflush (stderr); |
519 | _exit (0177); | |
520 | } | |
521 | ||
95954743 PA |
522 | linux_add_process (pid, 0); |
523 | ||
524 | ptid = ptid_build (pid, pid, 0); | |
525 | new_lwp = add_lwp (ptid); | |
526 | add_thread (ptid, new_lwp); | |
a6dbe5df | 527 | new_lwp->must_set_ptrace_flags = 1; |
611cb4a5 | 528 | |
a9fa9f7d | 529 | return pid; |
da6d8c04 DJ |
530 | } |
531 | ||
532 | /* Attach to an inferior process. */ | |
533 | ||
95954743 PA |
534 | static void |
535 | linux_attach_lwp_1 (unsigned long lwpid, int initial) | |
da6d8c04 | 536 | { |
95954743 | 537 | ptid_t ptid; |
54a0b537 | 538 | struct lwp_info *new_lwp; |
611cb4a5 | 539 | |
95954743 | 540 | if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) != 0) |
da6d8c04 | 541 | { |
95954743 | 542 | if (!initial) |
2d717e4f DJ |
543 | { |
544 | /* If we fail to attach to an LWP, just warn. */ | |
95954743 | 545 | fprintf (stderr, "Cannot attach to lwp %ld: %s (%d)\n", lwpid, |
2d717e4f DJ |
546 | strerror (errno), errno); |
547 | fflush (stderr); | |
548 | return; | |
549 | } | |
550 | else | |
551 | /* If we fail to attach to a process, report an error. */ | |
95954743 | 552 | error ("Cannot attach to lwp %ld: %s (%d)\n", lwpid, |
43d5792c | 553 | strerror (errno), errno); |
da6d8c04 DJ |
554 | } |
555 | ||
95954743 PA |
556 | if (initial) |
557 | /* NOTE/FIXME: This lwp might have not been the tgid. */ | |
558 | ptid = ptid_build (lwpid, lwpid, 0); | |
559 | else | |
560 | { | |
561 | /* Note that extracting the pid from the current inferior is | |
562 | safe, since we're always called in the context of the same | |
563 | process as this new thread. */ | |
564 | int pid = pid_of (get_thread_lwp (current_inferior)); | |
565 | ptid = ptid_build (pid, lwpid, 0); | |
566 | } | |
24a09b5f | 567 | |
95954743 PA |
568 | new_lwp = (struct lwp_info *) add_lwp (ptid); |
569 | add_thread (ptid, new_lwp); | |
0d62e5e8 | 570 | |
a6dbe5df PA |
571 | /* We need to wait for SIGSTOP before being able to make the next |
572 | ptrace call on this LWP. */ | |
573 | new_lwp->must_set_ptrace_flags = 1; | |
574 | ||
0d62e5e8 | 575 | /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH |
0e21c1ec DE |
576 | brings it to a halt. |
577 | ||
578 | There are several cases to consider here: | |
579 | ||
580 | 1) gdbserver has already attached to the process and is being notified | |
1b3f6016 PA |
581 | of a new thread that is being created. |
582 | In this case we should ignore that SIGSTOP and resume the process. | |
583 | This is handled below by setting stop_expected = 1. | |
0e21c1ec DE |
584 | |
585 | 2) This is the first thread (the process thread), and we're attaching | |
1b3f6016 PA |
586 | to it via attach_inferior. |
587 | In this case we want the process thread to stop. | |
588 | This is handled by having linux_attach clear stop_expected after | |
589 | we return. | |
590 | ??? If the process already has several threads we leave the other | |
591 | threads running. | |
0e21c1ec DE |
592 | |
593 | 3) GDB is connecting to gdbserver and is requesting an enumeration of all | |
1b3f6016 PA |
594 | existing threads. |
595 | In this case we want the thread to stop. | |
596 | FIXME: This case is currently not properly handled. | |
597 | We should wait for the SIGSTOP but don't. Things work apparently | |
598 | because enough time passes between when we ptrace (ATTACH) and when | |
599 | gdb makes the next ptrace call on the thread. | |
0d62e5e8 DJ |
600 | |
601 | On the other hand, if we are currently trying to stop all threads, we | |
602 | should treat the new thread as if we had sent it a SIGSTOP. This works | |
54a0b537 | 603 | because we are guaranteed that the add_lwp call above added us to the |
0e21c1ec DE |
604 | end of the list, and so the new thread has not yet reached |
605 | wait_for_sigstop (but will). */ | |
0d62e5e8 | 606 | if (! stopping_threads) |
54a0b537 | 607 | new_lwp->stop_expected = 1; |
0d62e5e8 DJ |
608 | } |
609 | ||
95954743 PA |
610 | void |
611 | linux_attach_lwp (unsigned long lwpid) | |
612 | { | |
613 | linux_attach_lwp_1 (lwpid, 0); | |
614 | } | |
615 | ||
0d62e5e8 | 616 | int |
a1928bad | 617 | linux_attach (unsigned long pid) |
0d62e5e8 | 618 | { |
54a0b537 | 619 | struct lwp_info *lwp; |
0d62e5e8 | 620 | |
95954743 PA |
621 | linux_attach_lwp_1 (pid, 1); |
622 | ||
623 | linux_add_process (pid, 1); | |
0d62e5e8 | 624 | |
bd99dc85 PA |
625 | if (!non_stop) |
626 | { | |
627 | /* Don't ignore the initial SIGSTOP if we just attached to this | |
628 | process. It will be collected by wait shortly. */ | |
95954743 PA |
629 | lwp = (struct lwp_info *) find_inferior_id (&all_lwps, |
630 | ptid_build (pid, pid, 0)); | |
bd99dc85 PA |
631 | lwp->stop_expected = 0; |
632 | } | |
0d62e5e8 | 633 | |
95954743 PA |
634 | return 0; |
635 | } | |
636 | ||
637 | struct counter | |
638 | { | |
639 | int pid; | |
640 | int count; | |
641 | }; | |
642 | ||
643 | static int | |
644 | second_thread_of_pid_p (struct inferior_list_entry *entry, void *args) | |
645 | { | |
646 | struct counter *counter = args; | |
647 | ||
648 | if (ptid_get_pid (entry->id) == counter->pid) | |
649 | { | |
650 | if (++counter->count > 1) | |
651 | return 1; | |
652 | } | |
d61ddec4 | 653 | |
da6d8c04 DJ |
654 | return 0; |
655 | } | |
656 | ||
95954743 PA |
657 | static int |
658 | last_thread_of_process_p (struct thread_info *thread) | |
659 | { | |
660 | ptid_t ptid = ((struct inferior_list_entry *)thread)->id; | |
661 | int pid = ptid_get_pid (ptid); | |
662 | struct counter counter = { pid , 0 }; | |
da6d8c04 | 663 | |
95954743 PA |
664 | return (find_inferior (&all_threads, |
665 | second_thread_of_pid_p, &counter) == NULL); | |
666 | } | |
667 | ||
668 | /* Kill the inferior lwp. */ | |
669 | ||
670 | static int | |
671 | linux_kill_one_lwp (struct inferior_list_entry *entry, void *args) | |
da6d8c04 | 672 | { |
0d62e5e8 | 673 | struct thread_info *thread = (struct thread_info *) entry; |
54a0b537 | 674 | struct lwp_info *lwp = get_thread_lwp (thread); |
0d62e5e8 | 675 | int wstat; |
95954743 PA |
676 | int pid = * (int *) args; |
677 | ||
678 | if (ptid_get_pid (entry->id) != pid) | |
679 | return 0; | |
0d62e5e8 | 680 | |
fd500816 DJ |
681 | /* We avoid killing the first thread here, because of a Linux kernel (at |
682 | least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before | |
683 | the children get a chance to be reaped, it will remain a zombie | |
684 | forever. */ | |
95954743 | 685 | |
12b42a12 | 686 | if (lwpid_of (lwp) == pid) |
95954743 PA |
687 | { |
688 | if (debug_threads) | |
689 | fprintf (stderr, "lkop: is last of process %s\n", | |
690 | target_pid_to_str (entry->id)); | |
691 | return 0; | |
692 | } | |
fd500816 | 693 | |
bd99dc85 PA |
694 | /* If we're killing a running inferior, make sure it is stopped |
695 | first, as PTRACE_KILL will not work otherwise. */ | |
696 | if (!lwp->stopped) | |
697 | send_sigstop (&lwp->head); | |
698 | ||
0d62e5e8 DJ |
699 | do |
700 | { | |
bd99dc85 | 701 | ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0); |
0d62e5e8 DJ |
702 | |
703 | /* Make sure it died. The loop is most likely unnecessary. */ | |
95954743 | 704 | pid = linux_wait_for_event (lwp->head.id, &wstat, __WALL); |
bd99dc85 | 705 | } while (pid > 0 && WIFSTOPPED (wstat)); |
95954743 PA |
706 | |
707 | return 0; | |
da6d8c04 DJ |
708 | } |
709 | ||
95954743 PA |
710 | static int |
711 | linux_kill (int pid) | |
0d62e5e8 | 712 | { |
95954743 | 713 | struct process_info *process; |
54a0b537 | 714 | struct lwp_info *lwp; |
95954743 | 715 | struct thread_info *thread; |
fd500816 | 716 | int wstat; |
95954743 | 717 | int lwpid; |
fd500816 | 718 | |
95954743 PA |
719 | process = find_process_pid (pid); |
720 | if (process == NULL) | |
721 | return -1; | |
9d606399 | 722 | |
95954743 | 723 | find_inferior (&all_threads, linux_kill_one_lwp, &pid); |
fd500816 | 724 | |
54a0b537 | 725 | /* See the comment in linux_kill_one_lwp. We did not kill the first |
fd500816 | 726 | thread in the list, so do so now. */ |
95954743 PA |
727 | lwp = find_lwp_pid (pid_to_ptid (pid)); |
728 | thread = get_lwp_thread (lwp); | |
bd99dc85 PA |
729 | |
730 | if (debug_threads) | |
95954743 PA |
731 | fprintf (stderr, "lk_1: killing lwp %ld, for pid: %d\n", |
732 | lwpid_of (lwp), pid); | |
bd99dc85 PA |
733 | |
734 | /* If we're killing a running inferior, make sure it is stopped | |
735 | first, as PTRACE_KILL will not work otherwise. */ | |
736 | if (!lwp->stopped) | |
737 | send_sigstop (&lwp->head); | |
738 | ||
fd500816 DJ |
739 | do |
740 | { | |
bd99dc85 | 741 | ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0); |
fd500816 DJ |
742 | |
743 | /* Make sure it died. The loop is most likely unnecessary. */ | |
95954743 PA |
744 | lwpid = linux_wait_for_event (lwp->head.id, &wstat, __WALL); |
745 | } while (lwpid > 0 && WIFSTOPPED (wstat)); | |
2d717e4f | 746 | |
ca5c370d PA |
747 | #ifdef USE_THREAD_DB |
748 | thread_db_free (process, 0); | |
749 | #endif | |
bd99dc85 | 750 | delete_lwp (lwp); |
ca5c370d | 751 | linux_remove_process (process); |
95954743 | 752 | return 0; |
0d62e5e8 DJ |
753 | } |
754 | ||
95954743 PA |
755 | static int |
756 | linux_detach_one_lwp (struct inferior_list_entry *entry, void *args) | |
6ad8ae5c DJ |
757 | { |
758 | struct thread_info *thread = (struct thread_info *) entry; | |
54a0b537 | 759 | struct lwp_info *lwp = get_thread_lwp (thread); |
95954743 PA |
760 | int pid = * (int *) args; |
761 | ||
762 | if (ptid_get_pid (entry->id) != pid) | |
763 | return 0; | |
6ad8ae5c | 764 | |
bd99dc85 PA |
765 | /* If we're detaching from a running inferior, make sure it is |
766 | stopped first, as PTRACE_DETACH will not work otherwise. */ | |
767 | if (!lwp->stopped) | |
768 | { | |
95954743 | 769 | int lwpid = lwpid_of (lwp); |
bd99dc85 PA |
770 | |
771 | stopping_threads = 1; | |
772 | send_sigstop (&lwp->head); | |
773 | ||
774 | /* If this detects a new thread through a clone event, the new | |
775 | thread is appended to the end of the lwp list, so we'll | |
776 | eventually detach from it. */ | |
777 | wait_for_sigstop (&lwp->head); | |
778 | stopping_threads = 0; | |
779 | ||
780 | /* If LWP exits while we're trying to stop it, there's nothing | |
781 | left to do. */ | |
95954743 | 782 | lwp = find_lwp_pid (pid_to_ptid (lwpid)); |
bd99dc85 | 783 | if (lwp == NULL) |
95954743 | 784 | return 0; |
bd99dc85 PA |
785 | } |
786 | ||
ae13219e DJ |
787 | /* Make sure the process isn't stopped at a breakpoint that's |
788 | no longer there. */ | |
54a0b537 | 789 | check_removed_breakpoint (lwp); |
ae13219e DJ |
790 | |
791 | /* If this process is stopped but is expecting a SIGSTOP, then make | |
792 | sure we take care of that now. This isn't absolutely guaranteed | |
793 | to collect the SIGSTOP, but is fairly likely to. */ | |
54a0b537 | 794 | if (lwp->stop_expected) |
ae13219e | 795 | { |
bd99dc85 | 796 | int wstat; |
ae13219e | 797 | /* Clear stop_expected, so that the SIGSTOP will be reported. */ |
54a0b537 PA |
798 | lwp->stop_expected = 0; |
799 | if (lwp->stopped) | |
2acc282a | 800 | linux_resume_one_lwp (lwp, 0, 0, NULL); |
95954743 | 801 | linux_wait_for_event (lwp->head.id, &wstat, __WALL); |
ae13219e DJ |
802 | } |
803 | ||
804 | /* Flush any pending changes to the process's registers. */ | |
805 | regcache_invalidate_one ((struct inferior_list_entry *) | |
54a0b537 | 806 | get_lwp_thread (lwp)); |
ae13219e DJ |
807 | |
808 | /* Finally, let it resume. */ | |
bd99dc85 PA |
809 | ptrace (PTRACE_DETACH, lwpid_of (lwp), 0, 0); |
810 | ||
811 | delete_lwp (lwp); | |
95954743 | 812 | return 0; |
6ad8ae5c DJ |
813 | } |
814 | ||
dd6953e1 | 815 | static int |
95954743 | 816 | any_thread_of (struct inferior_list_entry *entry, void *args) |
6ad8ae5c | 817 | { |
95954743 PA |
818 | int *pid_p = args; |
819 | ||
820 | if (ptid_get_pid (entry->id) == *pid_p) | |
821 | return 1; | |
822 | ||
823 | return 0; | |
824 | } | |
825 | ||
826 | static int | |
827 | linux_detach (int pid) | |
828 | { | |
829 | struct process_info *process; | |
830 | ||
831 | process = find_process_pid (pid); | |
832 | if (process == NULL) | |
833 | return -1; | |
834 | ||
ca5c370d PA |
835 | #ifdef USE_THREAD_DB |
836 | thread_db_free (process, 1); | |
837 | #endif | |
838 | ||
95954743 PA |
839 | current_inferior = |
840 | (struct thread_info *) find_inferior (&all_threads, any_thread_of, &pid); | |
841 | ||
ae13219e | 842 | delete_all_breakpoints (); |
95954743 | 843 | find_inferior (&all_threads, linux_detach_one_lwp, &pid); |
ca5c370d | 844 | linux_remove_process (process); |
dd6953e1 | 845 | return 0; |
6ad8ae5c DJ |
846 | } |
847 | ||
444d6139 | 848 | static void |
95954743 | 849 | linux_join (int pid) |
444d6139 | 850 | { |
444d6139 | 851 | int status, ret; |
95954743 | 852 | struct process_info *process; |
bd99dc85 | 853 | |
95954743 PA |
854 | process = find_process_pid (pid); |
855 | if (process == NULL) | |
856 | return; | |
444d6139 PA |
857 | |
858 | do { | |
95954743 | 859 | ret = my_waitpid (pid, &status, 0); |
444d6139 PA |
860 | if (WIFEXITED (status) || WIFSIGNALED (status)) |
861 | break; | |
862 | } while (ret != -1 || errno != ECHILD); | |
863 | } | |
864 | ||
6ad8ae5c | 865 | /* Return nonzero if the given thread is still alive. */ |
0d62e5e8 | 866 | static int |
95954743 | 867 | linux_thread_alive (ptid_t ptid) |
0d62e5e8 | 868 | { |
95954743 PA |
869 | struct lwp_info *lwp = find_lwp_pid (ptid); |
870 | ||
871 | /* We assume we always know if a thread exits. If a whole process | |
872 | exited but we still haven't been able to report it to GDB, we'll | |
873 | hold on to the last lwp of the dead process. */ | |
874 | if (lwp != NULL) | |
875 | return !lwp->dead; | |
0d62e5e8 DJ |
876 | else |
877 | return 0; | |
878 | } | |
879 | ||
880 | /* Return nonzero if this process stopped at a breakpoint which | |
881 | no longer appears to be inserted. Also adjust the PC | |
882 | appropriately to resume where the breakpoint used to be. */ | |
ce3a066d | 883 | static int |
54a0b537 | 884 | check_removed_breakpoint (struct lwp_info *event_child) |
da6d8c04 | 885 | { |
0d62e5e8 DJ |
886 | CORE_ADDR stop_pc; |
887 | struct thread_info *saved_inferior; | |
442ea881 | 888 | struct regcache *regcache; |
0d62e5e8 DJ |
889 | |
890 | if (event_child->pending_is_breakpoint == 0) | |
891 | return 0; | |
892 | ||
893 | if (debug_threads) | |
54a0b537 | 894 | fprintf (stderr, "Checking for breakpoint in lwp %ld.\n", |
bd99dc85 | 895 | lwpid_of (event_child)); |
0d62e5e8 DJ |
896 | |
897 | saved_inferior = current_inferior; | |
54a0b537 | 898 | current_inferior = get_lwp_thread (event_child); |
442ea881 | 899 | regcache = get_thread_regcache (current_inferior, 1); |
0d62e5e8 DJ |
900 | stop_pc = get_stop_pc (); |
901 | ||
902 | /* If the PC has changed since we stopped, then we shouldn't do | |
903 | anything. This happens if, for instance, GDB handled the | |
904 | decr_pc_after_break subtraction itself. */ | |
905 | if (stop_pc != event_child->pending_stop_pc) | |
906 | { | |
907 | if (debug_threads) | |
ae13219e DJ |
908 | fprintf (stderr, "Ignoring, PC was changed. Old PC was 0x%08llx\n", |
909 | event_child->pending_stop_pc); | |
0d62e5e8 DJ |
910 | |
911 | event_child->pending_is_breakpoint = 0; | |
912 | current_inferior = saved_inferior; | |
913 | return 0; | |
914 | } | |
915 | ||
916 | /* If the breakpoint is still there, we will report hitting it. */ | |
917 | if ((*the_low_target.breakpoint_at) (stop_pc)) | |
918 | { | |
919 | if (debug_threads) | |
920 | fprintf (stderr, "Ignoring, breakpoint is still present.\n"); | |
921 | current_inferior = saved_inferior; | |
922 | return 0; | |
923 | } | |
924 | ||
925 | if (debug_threads) | |
926 | fprintf (stderr, "Removed breakpoint.\n"); | |
927 | ||
928 | /* For decr_pc_after_break targets, here is where we perform the | |
929 | decrement. We go immediately from this function to resuming, | |
930 | and can not safely call get_stop_pc () again. */ | |
931 | if (the_low_target.set_pc != NULL) | |
47c0c975 DE |
932 | { |
933 | if (debug_threads) | |
934 | fprintf (stderr, "Set pc to 0x%lx\n", (long) stop_pc); | |
442ea881 | 935 | (*the_low_target.set_pc) (regcache, stop_pc); |
47c0c975 | 936 | } |
0d62e5e8 DJ |
937 | |
938 | /* We consumed the pending SIGTRAP. */ | |
5544ad89 | 939 | event_child->pending_is_breakpoint = 0; |
0d62e5e8 DJ |
940 | event_child->status_pending_p = 0; |
941 | event_child->status_pending = 0; | |
942 | ||
943 | current_inferior = saved_inferior; | |
da6d8c04 DJ |
944 | return 1; |
945 | } | |
946 | ||
54a0b537 PA |
947 | /* Return 1 if this lwp has an interesting status pending. This |
948 | function may silently resume an inferior lwp. */ | |
611cb4a5 | 949 | static int |
95954743 | 950 | status_pending_p (struct inferior_list_entry *entry, void *arg) |
0d62e5e8 | 951 | { |
54a0b537 | 952 | struct lwp_info *lwp = (struct lwp_info *) entry; |
95954743 PA |
953 | ptid_t ptid = * (ptid_t *) arg; |
954 | ||
955 | /* Check if we're only interested in events from a specific process | |
956 | or its lwps. */ | |
957 | if (!ptid_equal (minus_one_ptid, ptid) | |
958 | && ptid_get_pid (ptid) != ptid_get_pid (lwp->head.id)) | |
959 | return 0; | |
0d62e5e8 | 960 | |
bd99dc85 | 961 | if (lwp->status_pending_p && !lwp->suspended) |
54a0b537 | 962 | if (check_removed_breakpoint (lwp)) |
0d62e5e8 DJ |
963 | { |
964 | /* This thread was stopped at a breakpoint, and the breakpoint | |
965 | is now gone. We were told to continue (or step...) all threads, | |
966 | so GDB isn't trying to single-step past this breakpoint. | |
967 | So instead of reporting the old SIGTRAP, pretend we got to | |
968 | the breakpoint just after it was removed instead of just | |
969 | before; resume the process. */ | |
2acc282a | 970 | linux_resume_one_lwp (lwp, 0, 0, NULL); |
0d62e5e8 DJ |
971 | return 0; |
972 | } | |
973 | ||
bd99dc85 | 974 | return (lwp->status_pending_p && !lwp->suspended); |
0d62e5e8 DJ |
975 | } |
976 | ||
95954743 PA |
977 | static int |
978 | same_lwp (struct inferior_list_entry *entry, void *data) | |
979 | { | |
980 | ptid_t ptid = *(ptid_t *) data; | |
981 | int lwp; | |
982 | ||
983 | if (ptid_get_lwp (ptid) != 0) | |
984 | lwp = ptid_get_lwp (ptid); | |
985 | else | |
986 | lwp = ptid_get_pid (ptid); | |
987 | ||
988 | if (ptid_get_lwp (entry->id) == lwp) | |
989 | return 1; | |
990 | ||
991 | return 0; | |
992 | } | |
993 | ||
994 | struct lwp_info * | |
995 | find_lwp_pid (ptid_t ptid) | |
996 | { | |
997 | return (struct lwp_info*) find_inferior (&all_lwps, same_lwp, &ptid); | |
998 | } | |
999 | ||
bd99dc85 | 1000 | static struct lwp_info * |
95954743 | 1001 | linux_wait_for_lwp (ptid_t ptid, int *wstatp, int options) |
611cb4a5 | 1002 | { |
0d62e5e8 | 1003 | int ret; |
95954743 | 1004 | int to_wait_for = -1; |
bd99dc85 | 1005 | struct lwp_info *child = NULL; |
0d62e5e8 | 1006 | |
bd99dc85 | 1007 | if (debug_threads) |
95954743 PA |
1008 | fprintf (stderr, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid)); |
1009 | ||
1010 | if (ptid_equal (ptid, minus_one_ptid)) | |
1011 | to_wait_for = -1; /* any child */ | |
1012 | else | |
1013 | to_wait_for = ptid_get_lwp (ptid); /* this lwp only */ | |
0d62e5e8 | 1014 | |
bd99dc85 | 1015 | options |= __WALL; |
0d62e5e8 | 1016 | |
bd99dc85 | 1017 | retry: |
0d62e5e8 | 1018 | |
bd99dc85 PA |
1019 | ret = my_waitpid (to_wait_for, wstatp, options); |
1020 | if (ret == 0 || (ret == -1 && errno == ECHILD && (options & WNOHANG))) | |
1021 | return NULL; | |
1022 | else if (ret == -1) | |
1023 | perror_with_name ("waitpid"); | |
0d62e5e8 DJ |
1024 | |
1025 | if (debug_threads | |
1026 | && (!WIFSTOPPED (*wstatp) | |
1027 | || (WSTOPSIG (*wstatp) != 32 | |
1028 | && WSTOPSIG (*wstatp) != 33))) | |
1029 | fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp); | |
1030 | ||
95954743 | 1031 | child = find_lwp_pid (pid_to_ptid (ret)); |
0d62e5e8 | 1032 | |
24a09b5f DJ |
1033 | /* If we didn't find a process, one of two things presumably happened: |
1034 | - A process we started and then detached from has exited. Ignore it. | |
1035 | - A process we are controlling has forked and the new child's stop | |
1036 | was reported to us by the kernel. Save its PID. */ | |
bd99dc85 | 1037 | if (child == NULL && WIFSTOPPED (*wstatp)) |
24a09b5f DJ |
1038 | { |
1039 | add_pid_to_list (&stopped_pids, ret); | |
1040 | goto retry; | |
1041 | } | |
bd99dc85 | 1042 | else if (child == NULL) |
24a09b5f DJ |
1043 | goto retry; |
1044 | ||
bd99dc85 PA |
1045 | child->stopped = 1; |
1046 | child->pending_is_breakpoint = 0; | |
0d62e5e8 | 1047 | |
bd99dc85 | 1048 | child->last_status = *wstatp; |
32ca6d61 | 1049 | |
d61ddec4 UW |
1050 | /* Architecture-specific setup after inferior is running. |
1051 | This needs to happen after we have attached to the inferior | |
1052 | and it is stopped for the first time, but before we access | |
1053 | any inferior registers. */ | |
1054 | if (new_inferior) | |
1055 | { | |
1056 | the_low_target.arch_setup (); | |
52fa2412 UW |
1057 | #ifdef HAVE_LINUX_REGSETS |
1058 | memset (disabled_regsets, 0, num_regsets); | |
1059 | #endif | |
d61ddec4 UW |
1060 | new_inferior = 0; |
1061 | } | |
1062 | ||
0d62e5e8 | 1063 | if (debug_threads |
47c0c975 DE |
1064 | && WIFSTOPPED (*wstatp) |
1065 | && the_low_target.get_pc != NULL) | |
0d62e5e8 | 1066 | { |
896c7fbb | 1067 | struct thread_info *saved_inferior = current_inferior; |
442ea881 | 1068 | struct regcache *regcache = get_thread_regcache (current_inferior, 1); |
47c0c975 DE |
1069 | CORE_ADDR pc; |
1070 | ||
0d62e5e8 | 1071 | current_inferior = (struct thread_info *) |
95954743 | 1072 | find_inferior_id (&all_threads, child->head.id); |
442ea881 | 1073 | pc = (*the_low_target.get_pc) (regcache); |
47c0c975 | 1074 | fprintf (stderr, "linux_wait_for_lwp: pc is 0x%lx\n", (long) pc); |
896c7fbb | 1075 | current_inferior = saved_inferior; |
0d62e5e8 | 1076 | } |
bd99dc85 PA |
1077 | |
1078 | return child; | |
0d62e5e8 | 1079 | } |
611cb4a5 | 1080 | |
bd99dc85 PA |
1081 | /* Wait for an event from child PID. If PID is -1, wait for any |
1082 | child. Store the stop status through the status pointer WSTAT. | |
1083 | OPTIONS is passed to the waitpid call. Return 0 if no child stop | |
1084 | event was found and OPTIONS contains WNOHANG. Return the PID of | |
1085 | the stopped child otherwise. */ | |
1086 | ||
0d62e5e8 | 1087 | static int |
95954743 | 1088 | linux_wait_for_event_1 (ptid_t ptid, int *wstat, int options) |
0d62e5e8 DJ |
1089 | { |
1090 | CORE_ADDR stop_pc; | |
bd99dc85 | 1091 | struct lwp_info *event_child = NULL; |
b65d95c5 | 1092 | int bp_status; |
bd99dc85 | 1093 | struct lwp_info *requested_child = NULL; |
0d62e5e8 | 1094 | |
95954743 | 1095 | /* Check for a lwp with a pending status. */ |
0d62e5e8 DJ |
1096 | /* It is possible that the user changed the pending task's registers since |
1097 | it stopped. We correctly handle the change of PC if we hit a breakpoint | |
e5379b03 | 1098 | (in check_removed_breakpoint); signals should be reported anyway. */ |
bd99dc85 | 1099 | |
95954743 PA |
1100 | if (ptid_equal (ptid, minus_one_ptid) |
1101 | || ptid_equal (pid_to_ptid (ptid_get_pid (ptid)), ptid)) | |
0d62e5e8 | 1102 | { |
54a0b537 | 1103 | event_child = (struct lwp_info *) |
95954743 | 1104 | find_inferior (&all_lwps, status_pending_p, &ptid); |
0d62e5e8 | 1105 | if (debug_threads && event_child) |
bd99dc85 | 1106 | fprintf (stderr, "Got a pending child %ld\n", lwpid_of (event_child)); |
0d62e5e8 DJ |
1107 | } |
1108 | else | |
1109 | { | |
95954743 | 1110 | requested_child = find_lwp_pid (ptid); |
bd99dc85 PA |
1111 | if (requested_child->status_pending_p |
1112 | && !check_removed_breakpoint (requested_child)) | |
1113 | event_child = requested_child; | |
0d62e5e8 | 1114 | } |
611cb4a5 | 1115 | |
0d62e5e8 DJ |
1116 | if (event_child != NULL) |
1117 | { | |
bd99dc85 PA |
1118 | if (debug_threads) |
1119 | fprintf (stderr, "Got an event from pending child %ld (%04x)\n", | |
1120 | lwpid_of (event_child), event_child->status_pending); | |
1121 | *wstat = event_child->status_pending; | |
1122 | event_child->status_pending_p = 0; | |
1123 | event_child->status_pending = 0; | |
1124 | current_inferior = get_lwp_thread (event_child); | |
1125 | return lwpid_of (event_child); | |
0d62e5e8 DJ |
1126 | } |
1127 | ||
1128 | /* We only enter this loop if no process has a pending wait status. Thus | |
1129 | any action taken in response to a wait status inside this loop is | |
1130 | responding as soon as we detect the status, not after any pending | |
1131 | events. */ | |
1132 | while (1) | |
1133 | { | |
95954743 | 1134 | event_child = linux_wait_for_lwp (ptid, wstat, options); |
0d62e5e8 | 1135 | |
bd99dc85 PA |
1136 | if ((options & WNOHANG) && event_child == NULL) |
1137 | return 0; | |
0d62e5e8 DJ |
1138 | |
1139 | if (event_child == NULL) | |
1140 | error ("event from unknown child"); | |
611cb4a5 | 1141 | |
bd99dc85 | 1142 | current_inferior = get_lwp_thread (event_child); |
0d62e5e8 | 1143 | |
89be2091 | 1144 | /* Check for thread exit. */ |
bd99dc85 | 1145 | if (! WIFSTOPPED (*wstat)) |
0d62e5e8 | 1146 | { |
89be2091 | 1147 | if (debug_threads) |
95954743 | 1148 | fprintf (stderr, "LWP %ld exiting\n", lwpid_of (event_child)); |
89be2091 DJ |
1149 | |
1150 | /* If the last thread is exiting, just return. */ | |
95954743 | 1151 | if (last_thread_of_process_p (current_inferior)) |
bd99dc85 PA |
1152 | { |
1153 | if (debug_threads) | |
95954743 PA |
1154 | fprintf (stderr, "LWP %ld is last lwp of process\n", |
1155 | lwpid_of (event_child)); | |
bd99dc85 PA |
1156 | return lwpid_of (event_child); |
1157 | } | |
89be2091 | 1158 | |
bd99dc85 | 1159 | delete_lwp (event_child); |
89be2091 | 1160 | |
bd99dc85 PA |
1161 | if (!non_stop) |
1162 | { | |
1163 | current_inferior = (struct thread_info *) all_threads.head; | |
1164 | if (debug_threads) | |
1165 | fprintf (stderr, "Current inferior is now %ld\n", | |
1166 | lwpid_of (get_thread_lwp (current_inferior))); | |
1167 | } | |
1168 | else | |
1169 | { | |
1170 | current_inferior = NULL; | |
1171 | if (debug_threads) | |
1172 | fprintf (stderr, "Current inferior is now <NULL>\n"); | |
1173 | } | |
89be2091 DJ |
1174 | |
1175 | /* If we were waiting for this particular child to do something... | |
1176 | well, it did something. */ | |
bd99dc85 | 1177 | if (requested_child != NULL) |
95954743 | 1178 | return lwpid_of (event_child); |
89be2091 DJ |
1179 | |
1180 | /* Wait for a more interesting event. */ | |
1181 | continue; | |
1182 | } | |
1183 | ||
a6dbe5df PA |
1184 | if (event_child->must_set_ptrace_flags) |
1185 | { | |
1186 | ptrace (PTRACE_SETOPTIONS, lwpid_of (event_child), | |
1187 | 0, PTRACE_O_TRACECLONE); | |
1188 | event_child->must_set_ptrace_flags = 0; | |
1189 | } | |
1190 | ||
bd99dc85 PA |
1191 | if (WIFSTOPPED (*wstat) |
1192 | && WSTOPSIG (*wstat) == SIGSTOP | |
89be2091 DJ |
1193 | && event_child->stop_expected) |
1194 | { | |
1195 | if (debug_threads) | |
1196 | fprintf (stderr, "Expected stop.\n"); | |
1197 | event_child->stop_expected = 0; | |
2acc282a | 1198 | linux_resume_one_lwp (event_child, event_child->stepping, 0, NULL); |
89be2091 DJ |
1199 | continue; |
1200 | } | |
1201 | ||
bd99dc85 PA |
1202 | if (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) == SIGTRAP |
1203 | && *wstat >> 16 != 0) | |
24a09b5f | 1204 | { |
bd99dc85 | 1205 | handle_extended_wait (event_child, *wstat); |
24a09b5f DJ |
1206 | continue; |
1207 | } | |
1208 | ||
89be2091 DJ |
1209 | /* If GDB is not interested in this signal, don't stop other |
1210 | threads, and don't report it to GDB. Just resume the | |
1211 | inferior right away. We do this for threading-related | |
69f223ed DJ |
1212 | signals as well as any that GDB specifically requested we |
1213 | ignore. But never ignore SIGSTOP if we sent it ourselves, | |
1214 | and do not ignore signals when stepping - they may require | |
1215 | special handling to skip the signal handler. */ | |
89be2091 DJ |
1216 | /* FIXME drow/2002-06-09: Get signal numbers from the inferior's |
1217 | thread library? */ | |
bd99dc85 | 1218 | if (WIFSTOPPED (*wstat) |
69f223ed | 1219 | && !event_child->stepping |
24a09b5f | 1220 | && ( |
60c3d7b0 | 1221 | #if defined (USE_THREAD_DB) && defined (__SIGRTMIN) |
cdbfd419 | 1222 | (current_process ()->private->thread_db != NULL |
bd99dc85 PA |
1223 | && (WSTOPSIG (*wstat) == __SIGRTMIN |
1224 | || WSTOPSIG (*wstat) == __SIGRTMIN + 1)) | |
24a09b5f DJ |
1225 | || |
1226 | #endif | |
bd99dc85 PA |
1227 | (pass_signals[target_signal_from_host (WSTOPSIG (*wstat))] |
1228 | && (WSTOPSIG (*wstat) != SIGSTOP || !stopping_threads)))) | |
89be2091 DJ |
1229 | { |
1230 | siginfo_t info, *info_p; | |
1231 | ||
1232 | if (debug_threads) | |
24a09b5f | 1233 | fprintf (stderr, "Ignored signal %d for LWP %ld.\n", |
bd99dc85 | 1234 | WSTOPSIG (*wstat), lwpid_of (event_child)); |
89be2091 | 1235 | |
bd99dc85 | 1236 | if (ptrace (PTRACE_GETSIGINFO, lwpid_of (event_child), 0, &info) == 0) |
89be2091 DJ |
1237 | info_p = &info; |
1238 | else | |
1239 | info_p = NULL; | |
2acc282a | 1240 | linux_resume_one_lwp (event_child, |
54a0b537 | 1241 | event_child->stepping, |
bd99dc85 | 1242 | WSTOPSIG (*wstat), info_p); |
89be2091 | 1243 | continue; |
0d62e5e8 | 1244 | } |
611cb4a5 | 1245 | |
0d62e5e8 DJ |
1246 | /* If this event was not handled above, and is not a SIGTRAP, report |
1247 | it. */ | |
bd99dc85 PA |
1248 | if (!WIFSTOPPED (*wstat) || WSTOPSIG (*wstat) != SIGTRAP) |
1249 | return lwpid_of (event_child); | |
611cb4a5 | 1250 | |
0d62e5e8 DJ |
1251 | /* If this target does not support breakpoints, we simply report the |
1252 | SIGTRAP; it's of no concern to us. */ | |
1253 | if (the_low_target.get_pc == NULL) | |
bd99dc85 | 1254 | return lwpid_of (event_child); |
0d62e5e8 DJ |
1255 | |
1256 | stop_pc = get_stop_pc (); | |
1257 | ||
1258 | /* bp_reinsert will only be set if we were single-stepping. | |
1259 | Notice that we will resume the process after hitting | |
1260 | a gdbserver breakpoint; single-stepping to/over one | |
1261 | is not supported (yet). */ | |
1262 | if (event_child->bp_reinsert != 0) | |
1263 | { | |
1264 | if (debug_threads) | |
1265 | fprintf (stderr, "Reinserted breakpoint.\n"); | |
1266 | reinsert_breakpoint (event_child->bp_reinsert); | |
1267 | event_child->bp_reinsert = 0; | |
1268 | ||
1269 | /* Clear the single-stepping flag and SIGTRAP as we resume. */ | |
2acc282a | 1270 | linux_resume_one_lwp (event_child, 0, 0, NULL); |
0d62e5e8 DJ |
1271 | continue; |
1272 | } | |
1273 | ||
b65d95c5 | 1274 | bp_status = check_breakpoints (stop_pc); |
0d62e5e8 | 1275 | |
b65d95c5 | 1276 | if (bp_status != 0) |
0d62e5e8 | 1277 | { |
b65d95c5 DJ |
1278 | if (debug_threads) |
1279 | fprintf (stderr, "Hit a gdbserver breakpoint.\n"); | |
1280 | ||
0d62e5e8 | 1281 | /* We hit one of our own breakpoints. We mark it as a pending |
e5379b03 | 1282 | breakpoint, so that check_removed_breakpoint () will do the PC |
0d62e5e8 DJ |
1283 | adjustment for us at the appropriate time. */ |
1284 | event_child->pending_is_breakpoint = 1; | |
1285 | event_child->pending_stop_pc = stop_pc; | |
1286 | ||
b65d95c5 | 1287 | /* We may need to put the breakpoint back. We continue in the event |
0d62e5e8 DJ |
1288 | loop instead of simply replacing the breakpoint right away, |
1289 | in order to not lose signals sent to the thread that hit the | |
1290 | breakpoint. Unfortunately this increases the window where another | |
1291 | thread could sneak past the removed breakpoint. For the current | |
1292 | use of server-side breakpoints (thread creation) this is | |
1293 | acceptable; but it needs to be considered before this breakpoint | |
1294 | mechanism can be used in more general ways. For some breakpoints | |
1295 | it may be necessary to stop all other threads, but that should | |
1296 | be avoided where possible. | |
1297 | ||
1298 | If breakpoint_reinsert_addr is NULL, that means that we can | |
1299 | use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint, | |
1300 | mark it for reinsertion, and single-step. | |
1301 | ||
1302 | Otherwise, call the target function to figure out where we need | |
1303 | our temporary breakpoint, create it, and continue executing this | |
1304 | process. */ | |
bd99dc85 PA |
1305 | |
1306 | /* NOTE: we're lifting breakpoints in non-stop mode. This | |
1307 | is currently only used for thread event breakpoints, so | |
1308 | it isn't that bad as long as we have PTRACE_EVENT_CLONE | |
1309 | events. */ | |
b65d95c5 DJ |
1310 | if (bp_status == 2) |
1311 | /* No need to reinsert. */ | |
2acc282a | 1312 | linux_resume_one_lwp (event_child, 0, 0, NULL); |
b65d95c5 | 1313 | else if (the_low_target.breakpoint_reinsert_addr == NULL) |
0d62e5e8 DJ |
1314 | { |
1315 | event_child->bp_reinsert = stop_pc; | |
1316 | uninsert_breakpoint (stop_pc); | |
2acc282a | 1317 | linux_resume_one_lwp (event_child, 1, 0, NULL); |
0d62e5e8 DJ |
1318 | } |
1319 | else | |
1320 | { | |
1321 | reinsert_breakpoint_by_bp | |
1322 | (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ()); | |
2acc282a | 1323 | linux_resume_one_lwp (event_child, 0, 0, NULL); |
611cb4a5 | 1324 | } |
0d62e5e8 DJ |
1325 | |
1326 | continue; | |
1327 | } | |
1328 | ||
b65d95c5 DJ |
1329 | if (debug_threads) |
1330 | fprintf (stderr, "Hit a non-gdbserver breakpoint.\n"); | |
1331 | ||
0d62e5e8 | 1332 | /* If we were single-stepping, we definitely want to report the |
c35fafde PA |
1333 | SIGTRAP. Although the single-step operation has completed, |
1334 | do not clear clear the stepping flag yet; we need to check it | |
1335 | in wait_for_sigstop. */ | |
0d62e5e8 | 1336 | if (event_child->stepping) |
bd99dc85 | 1337 | return lwpid_of (event_child); |
0d62e5e8 DJ |
1338 | |
1339 | /* A SIGTRAP that we can't explain. It may have been a breakpoint. | |
1340 | Check if it is a breakpoint, and if so mark the process information | |
1341 | accordingly. This will handle both the necessary fiddling with the | |
1342 | PC on decr_pc_after_break targets and suppressing extra threads | |
1343 | hitting a breakpoint if two hit it at once and then GDB removes it | |
1344 | after the first is reported. Arguably it would be better to report | |
1345 | multiple threads hitting breakpoints simultaneously, but the current | |
1346 | remote protocol does not allow this. */ | |
1347 | if ((*the_low_target.breakpoint_at) (stop_pc)) | |
1348 | { | |
1349 | event_child->pending_is_breakpoint = 1; | |
1350 | event_child->pending_stop_pc = stop_pc; | |
611cb4a5 DJ |
1351 | } |
1352 | ||
bd99dc85 | 1353 | return lwpid_of (event_child); |
611cb4a5 | 1354 | } |
0d62e5e8 | 1355 | |
611cb4a5 DJ |
1356 | /* NOTREACHED */ |
1357 | return 0; | |
1358 | } | |
1359 | ||
95954743 PA |
1360 | static int |
1361 | linux_wait_for_event (ptid_t ptid, int *wstat, int options) | |
1362 | { | |
1363 | ptid_t wait_ptid; | |
1364 | ||
1365 | if (ptid_is_pid (ptid)) | |
1366 | { | |
1367 | /* A request to wait for a specific tgid. This is not possible | |
1368 | with waitpid, so instead, we wait for any child, and leave | |
1369 | children we're not interested in right now with a pending | |
1370 | status to report later. */ | |
1371 | wait_ptid = minus_one_ptid; | |
1372 | } | |
1373 | else | |
1374 | wait_ptid = ptid; | |
1375 | ||
1376 | while (1) | |
1377 | { | |
1378 | int event_pid; | |
1379 | ||
1380 | event_pid = linux_wait_for_event_1 (wait_ptid, wstat, options); | |
1381 | ||
1382 | if (event_pid > 0 | |
1383 | && ptid_is_pid (ptid) && ptid_get_pid (ptid) != event_pid) | |
1384 | { | |
1385 | struct lwp_info *event_child = find_lwp_pid (pid_to_ptid (event_pid)); | |
1386 | ||
1387 | if (! WIFSTOPPED (*wstat)) | |
1388 | mark_lwp_dead (event_child, *wstat); | |
1389 | else | |
1390 | { | |
1391 | event_child->status_pending_p = 1; | |
1392 | event_child->status_pending = *wstat; | |
1393 | } | |
1394 | } | |
1395 | else | |
1396 | return event_pid; | |
1397 | } | |
1398 | } | |
1399 | ||
0d62e5e8 | 1400 | /* Wait for process, returns status. */ |
da6d8c04 | 1401 | |
95954743 PA |
1402 | static ptid_t |
1403 | linux_wait_1 (ptid_t ptid, | |
1404 | struct target_waitstatus *ourstatus, int target_options) | |
da6d8c04 | 1405 | { |
e5f1222d | 1406 | int w; |
bd99dc85 PA |
1407 | struct thread_info *thread = NULL; |
1408 | struct lwp_info *lwp = NULL; | |
1409 | int options; | |
bd99dc85 PA |
1410 | int pid; |
1411 | ||
1412 | /* Translate generic target options into linux options. */ | |
1413 | options = __WALL; | |
1414 | if (target_options & TARGET_WNOHANG) | |
1415 | options |= WNOHANG; | |
0d62e5e8 DJ |
1416 | |
1417 | retry: | |
bd99dc85 PA |
1418 | ourstatus->kind = TARGET_WAITKIND_IGNORE; |
1419 | ||
0d62e5e8 DJ |
1420 | /* If we were only supposed to resume one thread, only wait for |
1421 | that thread - if it's still alive. If it died, however - which | |
1422 | can happen if we're coming from the thread death case below - | |
1423 | then we need to make sure we restart the other threads. We could | |
1424 | pick a thread at random or restart all; restarting all is less | |
1425 | arbitrary. */ | |
95954743 PA |
1426 | if (!non_stop |
1427 | && !ptid_equal (cont_thread, null_ptid) | |
1428 | && !ptid_equal (cont_thread, minus_one_ptid)) | |
0d62e5e8 | 1429 | { |
bd99dc85 PA |
1430 | thread = (struct thread_info *) find_inferior_id (&all_threads, |
1431 | cont_thread); | |
0d62e5e8 DJ |
1432 | |
1433 | /* No stepping, no signal - unless one is pending already, of course. */ | |
bd99dc85 | 1434 | if (thread == NULL) |
64386c31 DJ |
1435 | { |
1436 | struct thread_resume resume_info; | |
95954743 | 1437 | resume_info.thread = minus_one_ptid; |
bd99dc85 PA |
1438 | resume_info.kind = resume_continue; |
1439 | resume_info.sig = 0; | |
2bd7c093 | 1440 | linux_resume (&resume_info, 1); |
64386c31 | 1441 | } |
bd99dc85 | 1442 | else |
95954743 | 1443 | ptid = cont_thread; |
0d62e5e8 | 1444 | } |
da6d8c04 | 1445 | |
95954743 | 1446 | pid = linux_wait_for_event (ptid, &w, options); |
bd99dc85 | 1447 | if (pid == 0) /* only if TARGET_WNOHANG */ |
95954743 | 1448 | return null_ptid; |
bd99dc85 PA |
1449 | |
1450 | lwp = get_thread_lwp (current_inferior); | |
da6d8c04 | 1451 | |
0d62e5e8 DJ |
1452 | /* If we are waiting for a particular child, and it exited, |
1453 | linux_wait_for_event will return its exit status. Similarly if | |
1454 | the last child exited. If this is not the last child, however, | |
1455 | do not report it as exited until there is a 'thread exited' response | |
1456 | available in the remote protocol. Instead, just wait for another event. | |
1457 | This should be safe, because if the thread crashed we will already | |
1458 | have reported the termination signal to GDB; that should stop any | |
1459 | in-progress stepping operations, etc. | |
1460 | ||
1461 | Report the exit status of the last thread to exit. This matches | |
1462 | LinuxThreads' behavior. */ | |
1463 | ||
95954743 | 1464 | if (last_thread_of_process_p (current_inferior)) |
da6d8c04 | 1465 | { |
bd99dc85 | 1466 | if (WIFEXITED (w) || WIFSIGNALED (w)) |
0d62e5e8 | 1467 | { |
95954743 PA |
1468 | int pid = pid_of (lwp); |
1469 | struct process_info *process = find_process_pid (pid); | |
5b1c542e | 1470 | |
ca5c370d PA |
1471 | #ifdef USE_THREAD_DB |
1472 | thread_db_free (process, 0); | |
1473 | #endif | |
bd99dc85 | 1474 | delete_lwp (lwp); |
ca5c370d | 1475 | linux_remove_process (process); |
5b1c542e | 1476 | |
bd99dc85 | 1477 | current_inferior = NULL; |
5b1c542e | 1478 | |
bd99dc85 PA |
1479 | if (WIFEXITED (w)) |
1480 | { | |
1481 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
1482 | ourstatus->value.integer = WEXITSTATUS (w); | |
1483 | ||
1484 | if (debug_threads) | |
1485 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
1486 | } | |
1487 | else | |
1488 | { | |
1489 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
1490 | ourstatus->value.sig = target_signal_from_host (WTERMSIG (w)); | |
1491 | ||
1492 | if (debug_threads) | |
1493 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
1494 | ||
1495 | } | |
5b1c542e | 1496 | |
95954743 | 1497 | return pid_to_ptid (pid); |
0d62e5e8 | 1498 | } |
da6d8c04 | 1499 | } |
0d62e5e8 | 1500 | else |
da6d8c04 | 1501 | { |
0d62e5e8 DJ |
1502 | if (!WIFSTOPPED (w)) |
1503 | goto retry; | |
da6d8c04 DJ |
1504 | } |
1505 | ||
bd99dc85 PA |
1506 | /* In all-stop, stop all threads. Be careful to only do this if |
1507 | we're about to report an event to GDB. */ | |
1508 | if (!non_stop) | |
1509 | stop_all_lwps (); | |
1510 | ||
5b1c542e | 1511 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
5b1c542e | 1512 | |
bd99dc85 PA |
1513 | if (lwp->suspended && WSTOPSIG (w) == SIGSTOP) |
1514 | { | |
1515 | /* A thread that has been requested to stop by GDB with vCont;t, | |
1516 | and it stopped cleanly, so report as SIG0. The use of | |
1517 | SIGSTOP is an implementation detail. */ | |
1518 | ourstatus->value.sig = TARGET_SIGNAL_0; | |
1519 | } | |
1520 | else if (lwp->suspended && WSTOPSIG (w) != SIGSTOP) | |
1521 | { | |
1522 | /* A thread that has been requested to stop by GDB with vCont;t, | |
1523 | but, it stopped for other reasons. Set stop_expected so the | |
1524 | pending SIGSTOP is ignored and the LWP is resumed. */ | |
1525 | lwp->stop_expected = 1; | |
1526 | ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w)); | |
1527 | } | |
1528 | else | |
1529 | { | |
1530 | ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w)); | |
1531 | } | |
1532 | ||
1533 | if (debug_threads) | |
95954743 PA |
1534 | fprintf (stderr, "linux_wait ret = %s, %d, %d\n", |
1535 | target_pid_to_str (lwp->head.id), | |
bd99dc85 PA |
1536 | ourstatus->kind, |
1537 | ourstatus->value.sig); | |
1538 | ||
95954743 | 1539 | return lwp->head.id; |
bd99dc85 PA |
1540 | } |
1541 | ||
1542 | /* Get rid of any pending event in the pipe. */ | |
1543 | static void | |
1544 | async_file_flush (void) | |
1545 | { | |
1546 | int ret; | |
1547 | char buf; | |
1548 | ||
1549 | do | |
1550 | ret = read (linux_event_pipe[0], &buf, 1); | |
1551 | while (ret >= 0 || (ret == -1 && errno == EINTR)); | |
1552 | } | |
1553 | ||
1554 | /* Put something in the pipe, so the event loop wakes up. */ | |
1555 | static void | |
1556 | async_file_mark (void) | |
1557 | { | |
1558 | int ret; | |
1559 | ||
1560 | async_file_flush (); | |
1561 | ||
1562 | do | |
1563 | ret = write (linux_event_pipe[1], "+", 1); | |
1564 | while (ret == 0 || (ret == -1 && errno == EINTR)); | |
1565 | ||
1566 | /* Ignore EAGAIN. If the pipe is full, the event loop will already | |
1567 | be awakened anyway. */ | |
1568 | } | |
1569 | ||
95954743 PA |
1570 | static ptid_t |
1571 | linux_wait (ptid_t ptid, | |
1572 | struct target_waitstatus *ourstatus, int target_options) | |
bd99dc85 | 1573 | { |
95954743 | 1574 | ptid_t event_ptid; |
bd99dc85 PA |
1575 | |
1576 | if (debug_threads) | |
95954743 | 1577 | fprintf (stderr, "linux_wait: [%s]\n", target_pid_to_str (ptid)); |
bd99dc85 PA |
1578 | |
1579 | /* Flush the async file first. */ | |
1580 | if (target_is_async_p ()) | |
1581 | async_file_flush (); | |
1582 | ||
95954743 | 1583 | event_ptid = linux_wait_1 (ptid, ourstatus, target_options); |
bd99dc85 PA |
1584 | |
1585 | /* If at least one stop was reported, there may be more. A single | |
1586 | SIGCHLD can signal more than one child stop. */ | |
1587 | if (target_is_async_p () | |
1588 | && (target_options & TARGET_WNOHANG) != 0 | |
95954743 | 1589 | && !ptid_equal (event_ptid, null_ptid)) |
bd99dc85 PA |
1590 | async_file_mark (); |
1591 | ||
1592 | return event_ptid; | |
da6d8c04 DJ |
1593 | } |
1594 | ||
c5f62d5f | 1595 | /* Send a signal to an LWP. */ |
fd500816 DJ |
1596 | |
1597 | static int | |
a1928bad | 1598 | kill_lwp (unsigned long lwpid, int signo) |
fd500816 | 1599 | { |
c5f62d5f DE |
1600 | /* Use tkill, if possible, in case we are using nptl threads. If tkill |
1601 | fails, then we are not using nptl threads and we should be using kill. */ | |
fd500816 | 1602 | |
c5f62d5f DE |
1603 | #ifdef __NR_tkill |
1604 | { | |
1605 | static int tkill_failed; | |
fd500816 | 1606 | |
c5f62d5f DE |
1607 | if (!tkill_failed) |
1608 | { | |
1609 | int ret; | |
1610 | ||
1611 | errno = 0; | |
1612 | ret = syscall (__NR_tkill, lwpid, signo); | |
1613 | if (errno != ENOSYS) | |
1614 | return ret; | |
1615 | tkill_failed = 1; | |
1616 | } | |
1617 | } | |
fd500816 DJ |
1618 | #endif |
1619 | ||
1620 | return kill (lwpid, signo); | |
1621 | } | |
1622 | ||
0d62e5e8 DJ |
1623 | static void |
1624 | send_sigstop (struct inferior_list_entry *entry) | |
1625 | { | |
54a0b537 | 1626 | struct lwp_info *lwp = (struct lwp_info *) entry; |
bd99dc85 | 1627 | int pid; |
0d62e5e8 | 1628 | |
54a0b537 | 1629 | if (lwp->stopped) |
0d62e5e8 DJ |
1630 | return; |
1631 | ||
bd99dc85 PA |
1632 | pid = lwpid_of (lwp); |
1633 | ||
0d62e5e8 DJ |
1634 | /* If we already have a pending stop signal for this process, don't |
1635 | send another. */ | |
54a0b537 | 1636 | if (lwp->stop_expected) |
0d62e5e8 | 1637 | { |
ae13219e | 1638 | if (debug_threads) |
bd99dc85 | 1639 | fprintf (stderr, "Have pending sigstop for lwp %d\n", pid); |
ae13219e DJ |
1640 | |
1641 | /* We clear the stop_expected flag so that wait_for_sigstop | |
1642 | will receive the SIGSTOP event (instead of silently resuming and | |
1643 | waiting again). It'll be reset below. */ | |
54a0b537 | 1644 | lwp->stop_expected = 0; |
0d62e5e8 DJ |
1645 | return; |
1646 | } | |
1647 | ||
1648 | if (debug_threads) | |
bd99dc85 | 1649 | fprintf (stderr, "Sending sigstop to lwp %d\n", pid); |
0d62e5e8 | 1650 | |
bd99dc85 | 1651 | kill_lwp (pid, SIGSTOP); |
0d62e5e8 DJ |
1652 | } |
1653 | ||
95954743 PA |
1654 | static void |
1655 | mark_lwp_dead (struct lwp_info *lwp, int wstat) | |
1656 | { | |
1657 | /* It's dead, really. */ | |
1658 | lwp->dead = 1; | |
1659 | ||
1660 | /* Store the exit status for later. */ | |
1661 | lwp->status_pending_p = 1; | |
1662 | lwp->status_pending = wstat; | |
1663 | ||
1664 | /* So that check_removed_breakpoint doesn't try to figure out if | |
1665 | this is stopped at a breakpoint. */ | |
1666 | lwp->pending_is_breakpoint = 0; | |
1667 | ||
1668 | /* Prevent trying to stop it. */ | |
1669 | lwp->stopped = 1; | |
1670 | ||
1671 | /* No further stops are expected from a dead lwp. */ | |
1672 | lwp->stop_expected = 0; | |
1673 | } | |
1674 | ||
0d62e5e8 DJ |
1675 | static void |
1676 | wait_for_sigstop (struct inferior_list_entry *entry) | |
1677 | { | |
54a0b537 | 1678 | struct lwp_info *lwp = (struct lwp_info *) entry; |
bd99dc85 | 1679 | struct thread_info *saved_inferior; |
a1928bad | 1680 | int wstat; |
95954743 PA |
1681 | ptid_t saved_tid; |
1682 | ptid_t ptid; | |
0d62e5e8 | 1683 | |
54a0b537 | 1684 | if (lwp->stopped) |
0d62e5e8 DJ |
1685 | return; |
1686 | ||
1687 | saved_inferior = current_inferior; | |
bd99dc85 PA |
1688 | if (saved_inferior != NULL) |
1689 | saved_tid = ((struct inferior_list_entry *) saved_inferior)->id; | |
1690 | else | |
95954743 | 1691 | saved_tid = null_ptid; /* avoid bogus unused warning */ |
bd99dc85 | 1692 | |
95954743 | 1693 | ptid = lwp->head.id; |
bd99dc85 PA |
1694 | |
1695 | linux_wait_for_event (ptid, &wstat, __WALL); | |
0d62e5e8 DJ |
1696 | |
1697 | /* If we stopped with a non-SIGSTOP signal, save it for later | |
1698 | and record the pending SIGSTOP. If the process exited, just | |
1699 | return. */ | |
1700 | if (WIFSTOPPED (wstat) | |
1701 | && WSTOPSIG (wstat) != SIGSTOP) | |
1702 | { | |
1703 | if (debug_threads) | |
24a09b5f | 1704 | fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n", |
bd99dc85 | 1705 | lwpid_of (lwp), wstat); |
c35fafde PA |
1706 | |
1707 | /* Do not leave a pending single-step finish to be reported to | |
1708 | the client. The client will give us a new action for this | |
1709 | thread, possibly a continue request --- otherwise, the client | |
1710 | would consider this pending SIGTRAP reported later a spurious | |
1711 | signal. */ | |
1712 | if (WSTOPSIG (wstat) == SIGTRAP | |
1713 | && lwp->stepping | |
1714 | && !linux_stopped_by_watchpoint ()) | |
1715 | { | |
1716 | if (debug_threads) | |
1717 | fprintf (stderr, " single-step SIGTRAP ignored\n"); | |
1718 | } | |
1719 | else | |
1720 | { | |
1721 | lwp->status_pending_p = 1; | |
1722 | lwp->status_pending = wstat; | |
1723 | } | |
54a0b537 | 1724 | lwp->stop_expected = 1; |
0d62e5e8 | 1725 | } |
95954743 PA |
1726 | else if (!WIFSTOPPED (wstat)) |
1727 | { | |
1728 | if (debug_threads) | |
1729 | fprintf (stderr, "Process %ld exited while stopping LWPs\n", | |
1730 | lwpid_of (lwp)); | |
1731 | ||
1732 | /* Leave this status pending for the next time we're able to | |
1733 | report it. In the mean time, we'll report this lwp as dead | |
1734 | to GDB, so GDB doesn't try to read registers and memory from | |
1735 | it. */ | |
1736 | mark_lwp_dead (lwp, wstat); | |
1737 | } | |
0d62e5e8 | 1738 | |
bd99dc85 | 1739 | if (saved_inferior == NULL || linux_thread_alive (saved_tid)) |
0d62e5e8 DJ |
1740 | current_inferior = saved_inferior; |
1741 | else | |
1742 | { | |
1743 | if (debug_threads) | |
1744 | fprintf (stderr, "Previously current thread died.\n"); | |
1745 | ||
bd99dc85 PA |
1746 | if (non_stop) |
1747 | { | |
1748 | /* We can't change the current inferior behind GDB's back, | |
1749 | otherwise, a subsequent command may apply to the wrong | |
1750 | process. */ | |
1751 | current_inferior = NULL; | |
1752 | } | |
1753 | else | |
1754 | { | |
1755 | /* Set a valid thread as current. */ | |
1756 | set_desired_inferior (0); | |
1757 | } | |
0d62e5e8 DJ |
1758 | } |
1759 | } | |
1760 | ||
1761 | static void | |
54a0b537 | 1762 | stop_all_lwps (void) |
0d62e5e8 DJ |
1763 | { |
1764 | stopping_threads = 1; | |
54a0b537 PA |
1765 | for_each_inferior (&all_lwps, send_sigstop); |
1766 | for_each_inferior (&all_lwps, wait_for_sigstop); | |
0d62e5e8 DJ |
1767 | stopping_threads = 0; |
1768 | } | |
1769 | ||
da6d8c04 DJ |
1770 | /* Resume execution of the inferior process. |
1771 | If STEP is nonzero, single-step it. | |
1772 | If SIGNAL is nonzero, give it that signal. */ | |
1773 | ||
ce3a066d | 1774 | static void |
2acc282a | 1775 | linux_resume_one_lwp (struct lwp_info *lwp, |
54a0b537 | 1776 | int step, int signal, siginfo_t *info) |
da6d8c04 | 1777 | { |
0d62e5e8 DJ |
1778 | struct thread_info *saved_inferior; |
1779 | ||
54a0b537 | 1780 | if (lwp->stopped == 0) |
0d62e5e8 DJ |
1781 | return; |
1782 | ||
1783 | /* If we have pending signals or status, and a new signal, enqueue the | |
1784 | signal. Also enqueue the signal if we are waiting to reinsert a | |
1785 | breakpoint; it will be picked up again below. */ | |
1786 | if (signal != 0 | |
54a0b537 PA |
1787 | && (lwp->status_pending_p || lwp->pending_signals != NULL |
1788 | || lwp->bp_reinsert != 0)) | |
0d62e5e8 DJ |
1789 | { |
1790 | struct pending_signals *p_sig; | |
bca929d3 | 1791 | p_sig = xmalloc (sizeof (*p_sig)); |
54a0b537 | 1792 | p_sig->prev = lwp->pending_signals; |
0d62e5e8 | 1793 | p_sig->signal = signal; |
32ca6d61 DJ |
1794 | if (info == NULL) |
1795 | memset (&p_sig->info, 0, sizeof (siginfo_t)); | |
1796 | else | |
1797 | memcpy (&p_sig->info, info, sizeof (siginfo_t)); | |
54a0b537 | 1798 | lwp->pending_signals = p_sig; |
0d62e5e8 DJ |
1799 | } |
1800 | ||
54a0b537 | 1801 | if (lwp->status_pending_p && !check_removed_breakpoint (lwp)) |
0d62e5e8 DJ |
1802 | return; |
1803 | ||
1804 | saved_inferior = current_inferior; | |
54a0b537 | 1805 | current_inferior = get_lwp_thread (lwp); |
0d62e5e8 DJ |
1806 | |
1807 | if (debug_threads) | |
1b3f6016 | 1808 | fprintf (stderr, "Resuming lwp %ld (%s, signal %d, stop %s)\n", |
bd99dc85 | 1809 | lwpid_of (lwp), step ? "step" : "continue", signal, |
54a0b537 | 1810 | lwp->stop_expected ? "expected" : "not expected"); |
0d62e5e8 DJ |
1811 | |
1812 | /* This bit needs some thinking about. If we get a signal that | |
1813 | we must report while a single-step reinsert is still pending, | |
1814 | we often end up resuming the thread. It might be better to | |
1815 | (ew) allow a stack of pending events; then we could be sure that | |
1816 | the reinsert happened right away and not lose any signals. | |
1817 | ||
1818 | Making this stack would also shrink the window in which breakpoints are | |
54a0b537 | 1819 | uninserted (see comment in linux_wait_for_lwp) but not enough for |
0d62e5e8 DJ |
1820 | complete correctness, so it won't solve that problem. It may be |
1821 | worthwhile just to solve this one, however. */ | |
54a0b537 | 1822 | if (lwp->bp_reinsert != 0) |
0d62e5e8 DJ |
1823 | { |
1824 | if (debug_threads) | |
54a0b537 | 1825 | fprintf (stderr, " pending reinsert at %08lx", (long)lwp->bp_reinsert); |
0d62e5e8 DJ |
1826 | if (step == 0) |
1827 | fprintf (stderr, "BAD - reinserting but not stepping.\n"); | |
1828 | step = 1; | |
1829 | ||
1830 | /* Postpone any pending signal. It was enqueued above. */ | |
1831 | signal = 0; | |
1832 | } | |
1833 | ||
54a0b537 | 1834 | check_removed_breakpoint (lwp); |
0d62e5e8 | 1835 | |
aa691b87 | 1836 | if (debug_threads && the_low_target.get_pc != NULL) |
0d62e5e8 | 1837 | { |
442ea881 PA |
1838 | struct regcache *regcache = get_thread_regcache (current_inferior, 1); |
1839 | CORE_ADDR pc = (*the_low_target.get_pc) (regcache); | |
47c0c975 | 1840 | fprintf (stderr, " resuming from pc 0x%lx\n", (long) pc); |
0d62e5e8 DJ |
1841 | } |
1842 | ||
1843 | /* If we have pending signals, consume one unless we are trying to reinsert | |
1844 | a breakpoint. */ | |
54a0b537 | 1845 | if (lwp->pending_signals != NULL && lwp->bp_reinsert == 0) |
0d62e5e8 DJ |
1846 | { |
1847 | struct pending_signals **p_sig; | |
1848 | ||
54a0b537 | 1849 | p_sig = &lwp->pending_signals; |
0d62e5e8 DJ |
1850 | while ((*p_sig)->prev != NULL) |
1851 | p_sig = &(*p_sig)->prev; | |
1852 | ||
1853 | signal = (*p_sig)->signal; | |
32ca6d61 | 1854 | if ((*p_sig)->info.si_signo != 0) |
bd99dc85 | 1855 | ptrace (PTRACE_SETSIGINFO, lwpid_of (lwp), 0, &(*p_sig)->info); |
32ca6d61 | 1856 | |
0d62e5e8 DJ |
1857 | free (*p_sig); |
1858 | *p_sig = NULL; | |
1859 | } | |
1860 | ||
aa5ca48f DE |
1861 | if (the_low_target.prepare_to_resume != NULL) |
1862 | the_low_target.prepare_to_resume (lwp); | |
1863 | ||
0d62e5e8 | 1864 | regcache_invalidate_one ((struct inferior_list_entry *) |
54a0b537 | 1865 | get_lwp_thread (lwp)); |
da6d8c04 | 1866 | errno = 0; |
54a0b537 PA |
1867 | lwp->stopped = 0; |
1868 | lwp->stepping = step; | |
bd99dc85 | 1869 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, lwpid_of (lwp), 0, signal); |
0d62e5e8 DJ |
1870 | |
1871 | current_inferior = saved_inferior; | |
da6d8c04 | 1872 | if (errno) |
3221518c UW |
1873 | { |
1874 | /* ESRCH from ptrace either means that the thread was already | |
1875 | running (an error) or that it is gone (a race condition). If | |
1876 | it's gone, we will get a notification the next time we wait, | |
1877 | so we can ignore the error. We could differentiate these | |
1878 | two, but it's tricky without waiting; the thread still exists | |
1879 | as a zombie, so sending it signal 0 would succeed. So just | |
1880 | ignore ESRCH. */ | |
1881 | if (errno == ESRCH) | |
1882 | return; | |
1883 | ||
1884 | perror_with_name ("ptrace"); | |
1885 | } | |
da6d8c04 DJ |
1886 | } |
1887 | ||
2bd7c093 PA |
1888 | struct thread_resume_array |
1889 | { | |
1890 | struct thread_resume *resume; | |
1891 | size_t n; | |
1892 | }; | |
64386c31 DJ |
1893 | |
1894 | /* This function is called once per thread. We look up the thread | |
5544ad89 DJ |
1895 | in RESUME_PTR, and mark the thread with a pointer to the appropriate |
1896 | resume request. | |
1897 | ||
1898 | This algorithm is O(threads * resume elements), but resume elements | |
1899 | is small (and will remain small at least until GDB supports thread | |
1900 | suspension). */ | |
2bd7c093 PA |
1901 | static int |
1902 | linux_set_resume_request (struct inferior_list_entry *entry, void *arg) | |
0d62e5e8 | 1903 | { |
54a0b537 | 1904 | struct lwp_info *lwp; |
64386c31 | 1905 | struct thread_info *thread; |
5544ad89 | 1906 | int ndx; |
2bd7c093 | 1907 | struct thread_resume_array *r; |
64386c31 DJ |
1908 | |
1909 | thread = (struct thread_info *) entry; | |
54a0b537 | 1910 | lwp = get_thread_lwp (thread); |
2bd7c093 | 1911 | r = arg; |
64386c31 | 1912 | |
2bd7c093 | 1913 | for (ndx = 0; ndx < r->n; ndx++) |
95954743 PA |
1914 | { |
1915 | ptid_t ptid = r->resume[ndx].thread; | |
1916 | if (ptid_equal (ptid, minus_one_ptid) | |
1917 | || ptid_equal (ptid, entry->id) | |
1918 | || (ptid_is_pid (ptid) | |
1919 | && (ptid_get_pid (ptid) == pid_of (lwp))) | |
1920 | || (ptid_get_lwp (ptid) == -1 | |
1921 | && (ptid_get_pid (ptid) == pid_of (lwp)))) | |
1922 | { | |
1923 | lwp->resume = &r->resume[ndx]; | |
1924 | return 0; | |
1925 | } | |
1926 | } | |
2bd7c093 PA |
1927 | |
1928 | /* No resume action for this thread. */ | |
1929 | lwp->resume = NULL; | |
64386c31 | 1930 | |
2bd7c093 | 1931 | return 0; |
5544ad89 DJ |
1932 | } |
1933 | ||
5544ad89 | 1934 | |
bd99dc85 PA |
1935 | /* Set *FLAG_P if this lwp has an interesting status pending. */ |
1936 | static int | |
1937 | resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p) | |
5544ad89 | 1938 | { |
bd99dc85 | 1939 | struct lwp_info *lwp = (struct lwp_info *) entry; |
5544ad89 | 1940 | |
bd99dc85 PA |
1941 | /* LWPs which will not be resumed are not interesting, because |
1942 | we might not wait for them next time through linux_wait. */ | |
2bd7c093 | 1943 | if (lwp->resume == NULL) |
bd99dc85 | 1944 | return 0; |
64386c31 | 1945 | |
bd99dc85 PA |
1946 | /* If this thread has a removed breakpoint, we won't have any |
1947 | events to report later, so check now. check_removed_breakpoint | |
1948 | may clear status_pending_p. We avoid calling check_removed_breakpoint | |
1949 | for any thread that we are not otherwise going to resume - this | |
1950 | lets us preserve stopped status when two threads hit a breakpoint. | |
1951 | GDB removes the breakpoint to single-step a particular thread | |
1952 | past it, then re-inserts it and resumes all threads. We want | |
1953 | to report the second thread without resuming it in the interim. */ | |
1954 | if (lwp->status_pending_p) | |
1955 | check_removed_breakpoint (lwp); | |
5544ad89 | 1956 | |
bd99dc85 PA |
1957 | if (lwp->status_pending_p) |
1958 | * (int *) flag_p = 1; | |
c6ecbae5 | 1959 | |
bd99dc85 | 1960 | return 0; |
5544ad89 DJ |
1961 | } |
1962 | ||
1963 | /* This function is called once per thread. We check the thread's resume | |
1964 | request, which will tell us whether to resume, step, or leave the thread | |
bd99dc85 | 1965 | stopped; and what signal, if any, it should be sent. |
5544ad89 | 1966 | |
bd99dc85 PA |
1967 | For threads which we aren't explicitly told otherwise, we preserve |
1968 | the stepping flag; this is used for stepping over gdbserver-placed | |
1969 | breakpoints. | |
1970 | ||
1971 | If pending_flags was set in any thread, we queue any needed | |
1972 | signals, since we won't actually resume. We already have a pending | |
1973 | event to report, so we don't need to preserve any step requests; | |
1974 | they should be re-issued if necessary. */ | |
1975 | ||
1976 | static int | |
1977 | linux_resume_one_thread (struct inferior_list_entry *entry, void *arg) | |
5544ad89 | 1978 | { |
54a0b537 | 1979 | struct lwp_info *lwp; |
5544ad89 | 1980 | struct thread_info *thread; |
bd99dc85 PA |
1981 | int step; |
1982 | int pending_flag = * (int *) arg; | |
5544ad89 DJ |
1983 | |
1984 | thread = (struct thread_info *) entry; | |
54a0b537 | 1985 | lwp = get_thread_lwp (thread); |
5544ad89 | 1986 | |
2bd7c093 | 1987 | if (lwp->resume == NULL) |
bd99dc85 | 1988 | return 0; |
5544ad89 | 1989 | |
bd99dc85 | 1990 | if (lwp->resume->kind == resume_stop) |
5544ad89 | 1991 | { |
bd99dc85 PA |
1992 | if (debug_threads) |
1993 | fprintf (stderr, "suspending LWP %ld\n", lwpid_of (lwp)); | |
1994 | ||
1995 | if (!lwp->stopped) | |
1996 | { | |
1997 | if (debug_threads) | |
95954743 | 1998 | fprintf (stderr, "running -> suspending LWP %ld\n", lwpid_of (lwp)); |
bd99dc85 PA |
1999 | |
2000 | lwp->suspended = 1; | |
2001 | send_sigstop (&lwp->head); | |
2002 | } | |
2003 | else | |
2004 | { | |
2005 | if (debug_threads) | |
2006 | { | |
2007 | if (lwp->suspended) | |
2008 | fprintf (stderr, "already stopped/suspended LWP %ld\n", | |
2009 | lwpid_of (lwp)); | |
2010 | else | |
2011 | fprintf (stderr, "already stopped/not suspended LWP %ld\n", | |
2012 | lwpid_of (lwp)); | |
2013 | } | |
32ca6d61 | 2014 | |
bd99dc85 PA |
2015 | /* Make sure we leave the LWP suspended, so we don't try to |
2016 | resume it without GDB telling us to. FIXME: The LWP may | |
2017 | have been stopped in an internal event that was not meant | |
2018 | to be notified back to GDB (e.g., gdbserver breakpoint), | |
2019 | so we should be reporting a stop event in that case | |
2020 | too. */ | |
2021 | lwp->suspended = 1; | |
2022 | } | |
32ca6d61 | 2023 | |
bd99dc85 PA |
2024 | /* For stop requests, we're done. */ |
2025 | lwp->resume = NULL; | |
2026 | return 0; | |
5544ad89 | 2027 | } |
bd99dc85 PA |
2028 | else |
2029 | lwp->suspended = 0; | |
5544ad89 | 2030 | |
bd99dc85 PA |
2031 | /* If this thread which is about to be resumed has a pending status, |
2032 | then don't resume any threads - we can just report the pending | |
2033 | status. Make sure to queue any signals that would otherwise be | |
2034 | sent. In all-stop mode, we do this decision based on if *any* | |
2035 | thread has a pending status. */ | |
2036 | if (non_stop) | |
2037 | resume_status_pending_p (&lwp->head, &pending_flag); | |
5544ad89 | 2038 | |
bd99dc85 PA |
2039 | if (!pending_flag) |
2040 | { | |
2041 | if (debug_threads) | |
2042 | fprintf (stderr, "resuming LWP %ld\n", lwpid_of (lwp)); | |
5544ad89 | 2043 | |
95954743 | 2044 | if (ptid_equal (lwp->resume->thread, minus_one_ptid) |
bd99dc85 PA |
2045 | && lwp->stepping |
2046 | && lwp->pending_is_breakpoint) | |
2047 | step = 1; | |
2048 | else | |
2049 | step = (lwp->resume->kind == resume_step); | |
5544ad89 | 2050 | |
2acc282a | 2051 | linux_resume_one_lwp (lwp, step, lwp->resume->sig, NULL); |
bd99dc85 PA |
2052 | } |
2053 | else | |
2054 | { | |
2055 | if (debug_threads) | |
2056 | fprintf (stderr, "leaving LWP %ld stopped\n", lwpid_of (lwp)); | |
5544ad89 | 2057 | |
bd99dc85 PA |
2058 | /* If we have a new signal, enqueue the signal. */ |
2059 | if (lwp->resume->sig != 0) | |
2060 | { | |
2061 | struct pending_signals *p_sig; | |
2062 | p_sig = xmalloc (sizeof (*p_sig)); | |
2063 | p_sig->prev = lwp->pending_signals; | |
2064 | p_sig->signal = lwp->resume->sig; | |
2065 | memset (&p_sig->info, 0, sizeof (siginfo_t)); | |
2066 | ||
2067 | /* If this is the same signal we were previously stopped by, | |
2068 | make sure to queue its siginfo. We can ignore the return | |
2069 | value of ptrace; if it fails, we'll skip | |
2070 | PTRACE_SETSIGINFO. */ | |
2071 | if (WIFSTOPPED (lwp->last_status) | |
2072 | && WSTOPSIG (lwp->last_status) == lwp->resume->sig) | |
2073 | ptrace (PTRACE_GETSIGINFO, lwpid_of (lwp), 0, &p_sig->info); | |
2074 | ||
2075 | lwp->pending_signals = p_sig; | |
2076 | } | |
2077 | } | |
5544ad89 | 2078 | |
bd99dc85 | 2079 | lwp->resume = NULL; |
5544ad89 | 2080 | return 0; |
0d62e5e8 DJ |
2081 | } |
2082 | ||
2083 | static void | |
2bd7c093 | 2084 | linux_resume (struct thread_resume *resume_info, size_t n) |
0d62e5e8 | 2085 | { |
5544ad89 | 2086 | int pending_flag; |
2bd7c093 | 2087 | struct thread_resume_array array = { resume_info, n }; |
c6ecbae5 | 2088 | |
2bd7c093 | 2089 | find_inferior (&all_threads, linux_set_resume_request, &array); |
5544ad89 DJ |
2090 | |
2091 | /* If there is a thread which would otherwise be resumed, which | |
2092 | has a pending status, then don't resume any threads - we can just | |
2093 | report the pending status. Make sure to queue any signals | |
bd99dc85 PA |
2094 | that would otherwise be sent. In non-stop mode, we'll apply this |
2095 | logic to each thread individually. */ | |
5544ad89 | 2096 | pending_flag = 0; |
bd99dc85 PA |
2097 | if (!non_stop) |
2098 | find_inferior (&all_lwps, resume_status_pending_p, &pending_flag); | |
5544ad89 DJ |
2099 | |
2100 | if (debug_threads) | |
2101 | { | |
2102 | if (pending_flag) | |
2103 | fprintf (stderr, "Not resuming, pending status\n"); | |
2104 | else | |
2105 | fprintf (stderr, "Resuming, no pending status\n"); | |
2106 | } | |
2107 | ||
bd99dc85 | 2108 | find_inferior (&all_threads, linux_resume_one_thread, &pending_flag); |
0d62e5e8 DJ |
2109 | } |
2110 | ||
2111 | #ifdef HAVE_LINUX_USRREGS | |
da6d8c04 DJ |
2112 | |
2113 | int | |
0a30fbc4 | 2114 | register_addr (int regnum) |
da6d8c04 DJ |
2115 | { |
2116 | int addr; | |
2117 | ||
2ec06d2e | 2118 | if (regnum < 0 || regnum >= the_low_target.num_regs) |
da6d8c04 DJ |
2119 | error ("Invalid register number %d.", regnum); |
2120 | ||
2ec06d2e | 2121 | addr = the_low_target.regmap[regnum]; |
da6d8c04 DJ |
2122 | |
2123 | return addr; | |
2124 | } | |
2125 | ||
58caa3dc | 2126 | /* Fetch one register. */ |
da6d8c04 | 2127 | static void |
442ea881 | 2128 | fetch_register (struct regcache *regcache, int regno) |
da6d8c04 DJ |
2129 | { |
2130 | CORE_ADDR regaddr; | |
48d93c75 | 2131 | int i, size; |
0d62e5e8 | 2132 | char *buf; |
95954743 | 2133 | int pid; |
da6d8c04 | 2134 | |
2ec06d2e | 2135 | if (regno >= the_low_target.num_regs) |
0a30fbc4 | 2136 | return; |
2ec06d2e | 2137 | if ((*the_low_target.cannot_fetch_register) (regno)) |
0a30fbc4 | 2138 | return; |
da6d8c04 | 2139 | |
0a30fbc4 DJ |
2140 | regaddr = register_addr (regno); |
2141 | if (regaddr == -1) | |
2142 | return; | |
95954743 PA |
2143 | |
2144 | pid = lwpid_of (get_thread_lwp (current_inferior)); | |
1b3f6016 PA |
2145 | size = ((register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1) |
2146 | & - sizeof (PTRACE_XFER_TYPE)); | |
48d93c75 UW |
2147 | buf = alloca (size); |
2148 | for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE)) | |
da6d8c04 DJ |
2149 | { |
2150 | errno = 0; | |
0d62e5e8 | 2151 | *(PTRACE_XFER_TYPE *) (buf + i) = |
95954743 | 2152 | ptrace (PTRACE_PEEKUSER, pid, (PTRACE_ARG3_TYPE) regaddr, 0); |
da6d8c04 DJ |
2153 | regaddr += sizeof (PTRACE_XFER_TYPE); |
2154 | if (errno != 0) | |
2155 | { | |
2156 | /* Warning, not error, in case we are attached; sometimes the | |
2157 | kernel doesn't let us at the registers. */ | |
2158 | char *err = strerror (errno); | |
2159 | char *msg = alloca (strlen (err) + 128); | |
2160 | sprintf (msg, "reading register %d: %s", regno, err); | |
2161 | error (msg); | |
2162 | goto error_exit; | |
2163 | } | |
2164 | } | |
ee1a7ae4 UW |
2165 | |
2166 | if (the_low_target.supply_ptrace_register) | |
442ea881 | 2167 | the_low_target.supply_ptrace_register (regcache, regno, buf); |
5a1f5858 | 2168 | else |
442ea881 | 2169 | supply_register (regcache, regno, buf); |
0d62e5e8 | 2170 | |
da6d8c04 DJ |
2171 | error_exit:; |
2172 | } | |
2173 | ||
2174 | /* Fetch all registers, or just one, from the child process. */ | |
58caa3dc | 2175 | static void |
442ea881 | 2176 | usr_fetch_inferior_registers (struct regcache *regcache, int regno) |
da6d8c04 | 2177 | { |
4463ce24 | 2178 | if (regno == -1) |
2ec06d2e | 2179 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
442ea881 | 2180 | fetch_register (regcache, regno); |
da6d8c04 | 2181 | else |
442ea881 | 2182 | fetch_register (regcache, regno); |
da6d8c04 DJ |
2183 | } |
2184 | ||
2185 | /* Store our register values back into the inferior. | |
2186 | If REGNO is -1, do this for all registers. | |
2187 | Otherwise, REGNO specifies which register (so we can save time). */ | |
58caa3dc | 2188 | static void |
442ea881 | 2189 | usr_store_inferior_registers (struct regcache *regcache, int regno) |
da6d8c04 DJ |
2190 | { |
2191 | CORE_ADDR regaddr; | |
48d93c75 | 2192 | int i, size; |
0d62e5e8 | 2193 | char *buf; |
55ac2b99 | 2194 | int pid; |
da6d8c04 DJ |
2195 | |
2196 | if (regno >= 0) | |
2197 | { | |
2ec06d2e | 2198 | if (regno >= the_low_target.num_regs) |
0a30fbc4 DJ |
2199 | return; |
2200 | ||
bc1e36ca | 2201 | if ((*the_low_target.cannot_store_register) (regno) == 1) |
0a30fbc4 DJ |
2202 | return; |
2203 | ||
2204 | regaddr = register_addr (regno); | |
2205 | if (regaddr == -1) | |
da6d8c04 | 2206 | return; |
da6d8c04 | 2207 | errno = 0; |
48d93c75 UW |
2208 | size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1) |
2209 | & - sizeof (PTRACE_XFER_TYPE); | |
2210 | buf = alloca (size); | |
2211 | memset (buf, 0, size); | |
ee1a7ae4 UW |
2212 | |
2213 | if (the_low_target.collect_ptrace_register) | |
442ea881 | 2214 | the_low_target.collect_ptrace_register (regcache, regno, buf); |
5a1f5858 | 2215 | else |
442ea881 | 2216 | collect_register (regcache, regno, buf); |
ee1a7ae4 | 2217 | |
95954743 | 2218 | pid = lwpid_of (get_thread_lwp (current_inferior)); |
48d93c75 | 2219 | for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE)) |
da6d8c04 | 2220 | { |
0a30fbc4 | 2221 | errno = 0; |
95954743 | 2222 | ptrace (PTRACE_POKEUSER, pid, (PTRACE_ARG3_TYPE) regaddr, |
2ff29de4 | 2223 | *(PTRACE_XFER_TYPE *) (buf + i)); |
da6d8c04 DJ |
2224 | if (errno != 0) |
2225 | { | |
1b3f6016 PA |
2226 | /* At this point, ESRCH should mean the process is |
2227 | already gone, in which case we simply ignore attempts | |
2228 | to change its registers. See also the related | |
2229 | comment in linux_resume_one_lwp. */ | |
3221518c UW |
2230 | if (errno == ESRCH) |
2231 | return; | |
2232 | ||
bc1e36ca DJ |
2233 | if ((*the_low_target.cannot_store_register) (regno) == 0) |
2234 | { | |
2235 | char *err = strerror (errno); | |
2236 | char *msg = alloca (strlen (err) + 128); | |
2237 | sprintf (msg, "writing register %d: %s", | |
2238 | regno, err); | |
2239 | error (msg); | |
2240 | return; | |
2241 | } | |
da6d8c04 | 2242 | } |
2ff29de4 | 2243 | regaddr += sizeof (PTRACE_XFER_TYPE); |
da6d8c04 | 2244 | } |
da6d8c04 DJ |
2245 | } |
2246 | else | |
2ec06d2e | 2247 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
442ea881 | 2248 | usr_store_inferior_registers (regcache, regno); |
da6d8c04 | 2249 | } |
58caa3dc DJ |
2250 | #endif /* HAVE_LINUX_USRREGS */ |
2251 | ||
2252 | ||
2253 | ||
2254 | #ifdef HAVE_LINUX_REGSETS | |
2255 | ||
2256 | static int | |
442ea881 | 2257 | regsets_fetch_inferior_registers (struct regcache *regcache) |
58caa3dc DJ |
2258 | { |
2259 | struct regset_info *regset; | |
e9d25b98 | 2260 | int saw_general_regs = 0; |
95954743 | 2261 | int pid; |
58caa3dc DJ |
2262 | |
2263 | regset = target_regsets; | |
2264 | ||
95954743 | 2265 | pid = lwpid_of (get_thread_lwp (current_inferior)); |
58caa3dc DJ |
2266 | while (regset->size >= 0) |
2267 | { | |
2268 | void *buf; | |
2269 | int res; | |
2270 | ||
52fa2412 | 2271 | if (regset->size == 0 || disabled_regsets[regset - target_regsets]) |
58caa3dc DJ |
2272 | { |
2273 | regset ++; | |
2274 | continue; | |
2275 | } | |
2276 | ||
bca929d3 | 2277 | buf = xmalloc (regset->size); |
dfb64f85 | 2278 | #ifndef __sparc__ |
95954743 | 2279 | res = ptrace (regset->get_request, pid, 0, buf); |
dfb64f85 | 2280 | #else |
95954743 | 2281 | res = ptrace (regset->get_request, pid, buf, 0); |
dfb64f85 | 2282 | #endif |
58caa3dc DJ |
2283 | if (res < 0) |
2284 | { | |
2285 | if (errno == EIO) | |
2286 | { | |
52fa2412 UW |
2287 | /* If we get EIO on a regset, do not try it again for |
2288 | this process. */ | |
2289 | disabled_regsets[regset - target_regsets] = 1; | |
fdeb2a12 | 2290 | free (buf); |
52fa2412 | 2291 | continue; |
58caa3dc DJ |
2292 | } |
2293 | else | |
2294 | { | |
0d62e5e8 | 2295 | char s[256]; |
95954743 PA |
2296 | sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d", |
2297 | pid); | |
0d62e5e8 | 2298 | perror (s); |
58caa3dc DJ |
2299 | } |
2300 | } | |
e9d25b98 DJ |
2301 | else if (regset->type == GENERAL_REGS) |
2302 | saw_general_regs = 1; | |
442ea881 | 2303 | regset->store_function (regcache, buf); |
58caa3dc | 2304 | regset ++; |
fdeb2a12 | 2305 | free (buf); |
58caa3dc | 2306 | } |
e9d25b98 DJ |
2307 | if (saw_general_regs) |
2308 | return 0; | |
2309 | else | |
2310 | return 1; | |
58caa3dc DJ |
2311 | } |
2312 | ||
2313 | static int | |
442ea881 | 2314 | regsets_store_inferior_registers (struct regcache *regcache) |
58caa3dc DJ |
2315 | { |
2316 | struct regset_info *regset; | |
e9d25b98 | 2317 | int saw_general_regs = 0; |
95954743 | 2318 | int pid; |
58caa3dc DJ |
2319 | |
2320 | regset = target_regsets; | |
2321 | ||
95954743 | 2322 | pid = lwpid_of (get_thread_lwp (current_inferior)); |
58caa3dc DJ |
2323 | while (regset->size >= 0) |
2324 | { | |
2325 | void *buf; | |
2326 | int res; | |
2327 | ||
52fa2412 | 2328 | if (regset->size == 0 || disabled_regsets[regset - target_regsets]) |
58caa3dc DJ |
2329 | { |
2330 | regset ++; | |
2331 | continue; | |
2332 | } | |
2333 | ||
bca929d3 | 2334 | buf = xmalloc (regset->size); |
545587ee DJ |
2335 | |
2336 | /* First fill the buffer with the current register set contents, | |
2337 | in case there are any items in the kernel's regset that are | |
2338 | not in gdbserver's regcache. */ | |
dfb64f85 | 2339 | #ifndef __sparc__ |
95954743 | 2340 | res = ptrace (regset->get_request, pid, 0, buf); |
dfb64f85 | 2341 | #else |
95954743 | 2342 | res = ptrace (regset->get_request, pid, buf, 0); |
dfb64f85 | 2343 | #endif |
545587ee DJ |
2344 | |
2345 | if (res == 0) | |
2346 | { | |
2347 | /* Then overlay our cached registers on that. */ | |
442ea881 | 2348 | regset->fill_function (regcache, buf); |
545587ee DJ |
2349 | |
2350 | /* Only now do we write the register set. */ | |
dfb64f85 | 2351 | #ifndef __sparc__ |
95954743 | 2352 | res = ptrace (regset->set_request, pid, 0, buf); |
dfb64f85 | 2353 | #else |
95954743 | 2354 | res = ptrace (regset->set_request, pid, buf, 0); |
dfb64f85 | 2355 | #endif |
545587ee DJ |
2356 | } |
2357 | ||
58caa3dc DJ |
2358 | if (res < 0) |
2359 | { | |
2360 | if (errno == EIO) | |
2361 | { | |
52fa2412 UW |
2362 | /* If we get EIO on a regset, do not try it again for |
2363 | this process. */ | |
2364 | disabled_regsets[regset - target_regsets] = 1; | |
fdeb2a12 | 2365 | free (buf); |
52fa2412 | 2366 | continue; |
58caa3dc | 2367 | } |
3221518c UW |
2368 | else if (errno == ESRCH) |
2369 | { | |
1b3f6016 PA |
2370 | /* At this point, ESRCH should mean the process is |
2371 | already gone, in which case we simply ignore attempts | |
2372 | to change its registers. See also the related | |
2373 | comment in linux_resume_one_lwp. */ | |
fdeb2a12 | 2374 | free (buf); |
3221518c UW |
2375 | return 0; |
2376 | } | |
58caa3dc DJ |
2377 | else |
2378 | { | |
ce3a066d | 2379 | perror ("Warning: ptrace(regsets_store_inferior_registers)"); |
58caa3dc DJ |
2380 | } |
2381 | } | |
e9d25b98 DJ |
2382 | else if (regset->type == GENERAL_REGS) |
2383 | saw_general_regs = 1; | |
58caa3dc | 2384 | regset ++; |
09ec9b38 | 2385 | free (buf); |
58caa3dc | 2386 | } |
e9d25b98 DJ |
2387 | if (saw_general_regs) |
2388 | return 0; | |
2389 | else | |
2390 | return 1; | |
ce3a066d | 2391 | return 0; |
58caa3dc DJ |
2392 | } |
2393 | ||
2394 | #endif /* HAVE_LINUX_REGSETS */ | |
2395 | ||
2396 | ||
2397 | void | |
442ea881 | 2398 | linux_fetch_registers (struct regcache *regcache, int regno) |
58caa3dc DJ |
2399 | { |
2400 | #ifdef HAVE_LINUX_REGSETS | |
442ea881 | 2401 | if (regsets_fetch_inferior_registers (regcache) == 0) |
52fa2412 | 2402 | return; |
58caa3dc DJ |
2403 | #endif |
2404 | #ifdef HAVE_LINUX_USRREGS | |
442ea881 | 2405 | usr_fetch_inferior_registers (regcache, regno); |
58caa3dc DJ |
2406 | #endif |
2407 | } | |
2408 | ||
2409 | void | |
442ea881 | 2410 | linux_store_registers (struct regcache *regcache, int regno) |
58caa3dc DJ |
2411 | { |
2412 | #ifdef HAVE_LINUX_REGSETS | |
442ea881 | 2413 | if (regsets_store_inferior_registers (regcache) == 0) |
52fa2412 | 2414 | return; |
58caa3dc DJ |
2415 | #endif |
2416 | #ifdef HAVE_LINUX_USRREGS | |
442ea881 | 2417 | usr_store_inferior_registers (regcache, regno); |
58caa3dc DJ |
2418 | #endif |
2419 | } | |
2420 | ||
da6d8c04 | 2421 | |
da6d8c04 DJ |
2422 | /* Copy LEN bytes from inferior's memory starting at MEMADDR |
2423 | to debugger memory starting at MYADDR. */ | |
2424 | ||
c3e735a6 | 2425 | static int |
f450004a | 2426 | linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
da6d8c04 DJ |
2427 | { |
2428 | register int i; | |
2429 | /* Round starting address down to longword boundary. */ | |
2430 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
2431 | /* Round ending address up; get number of longwords that makes. */ | |
aa691b87 RM |
2432 | register int count |
2433 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) | |
da6d8c04 DJ |
2434 | / sizeof (PTRACE_XFER_TYPE); |
2435 | /* Allocate buffer of that many longwords. */ | |
aa691b87 | 2436 | register PTRACE_XFER_TYPE *buffer |
da6d8c04 | 2437 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); |
fd462a61 DJ |
2438 | int fd; |
2439 | char filename[64]; | |
95954743 | 2440 | int pid = lwpid_of (get_thread_lwp (current_inferior)); |
fd462a61 DJ |
2441 | |
2442 | /* Try using /proc. Don't bother for one word. */ | |
2443 | if (len >= 3 * sizeof (long)) | |
2444 | { | |
2445 | /* We could keep this file open and cache it - possibly one per | |
2446 | thread. That requires some juggling, but is even faster. */ | |
95954743 | 2447 | sprintf (filename, "/proc/%d/mem", pid); |
fd462a61 DJ |
2448 | fd = open (filename, O_RDONLY | O_LARGEFILE); |
2449 | if (fd == -1) | |
2450 | goto no_proc; | |
2451 | ||
2452 | /* If pread64 is available, use it. It's faster if the kernel | |
2453 | supports it (only one syscall), and it's 64-bit safe even on | |
2454 | 32-bit platforms (for instance, SPARC debugging a SPARC64 | |
2455 | application). */ | |
2456 | #ifdef HAVE_PREAD64 | |
2457 | if (pread64 (fd, myaddr, len, memaddr) != len) | |
2458 | #else | |
1de1badb | 2459 | if (lseek (fd, memaddr, SEEK_SET) == -1 || read (fd, myaddr, len) != len) |
fd462a61 DJ |
2460 | #endif |
2461 | { | |
2462 | close (fd); | |
2463 | goto no_proc; | |
2464 | } | |
2465 | ||
2466 | close (fd); | |
2467 | return 0; | |
2468 | } | |
da6d8c04 | 2469 | |
fd462a61 | 2470 | no_proc: |
da6d8c04 DJ |
2471 | /* Read all the longwords */ |
2472 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
2473 | { | |
c3e735a6 | 2474 | errno = 0; |
95954743 | 2475 | buffer[i] = ptrace (PTRACE_PEEKTEXT, pid, (PTRACE_ARG3_TYPE) addr, 0); |
c3e735a6 DJ |
2476 | if (errno) |
2477 | return errno; | |
da6d8c04 DJ |
2478 | } |
2479 | ||
2480 | /* Copy appropriate bytes out of the buffer. */ | |
1b3f6016 PA |
2481 | memcpy (myaddr, |
2482 | (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), | |
2483 | len); | |
c3e735a6 DJ |
2484 | |
2485 | return 0; | |
da6d8c04 DJ |
2486 | } |
2487 | ||
2488 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
2489 | to inferior's memory at MEMADDR. | |
2490 | On failure (cannot write the inferior) | |
2491 | returns the value of errno. */ | |
2492 | ||
ce3a066d | 2493 | static int |
f450004a | 2494 | linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) |
da6d8c04 DJ |
2495 | { |
2496 | register int i; | |
2497 | /* Round starting address down to longword boundary. */ | |
2498 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
2499 | /* Round ending address up; get number of longwords that makes. */ | |
2500 | register int count | |
2501 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE); | |
2502 | /* Allocate buffer of that many longwords. */ | |
2503 | register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); | |
95954743 | 2504 | int pid = lwpid_of (get_thread_lwp (current_inferior)); |
da6d8c04 | 2505 | |
0d62e5e8 DJ |
2506 | if (debug_threads) |
2507 | { | |
58d6951d DJ |
2508 | /* Dump up to four bytes. */ |
2509 | unsigned int val = * (unsigned int *) myaddr; | |
2510 | if (len == 1) | |
2511 | val = val & 0xff; | |
2512 | else if (len == 2) | |
2513 | val = val & 0xffff; | |
2514 | else if (len == 3) | |
2515 | val = val & 0xffffff; | |
2516 | fprintf (stderr, "Writing %0*x to 0x%08lx\n", 2 * ((len < 4) ? len : 4), | |
2517 | val, (long)memaddr); | |
0d62e5e8 DJ |
2518 | } |
2519 | ||
da6d8c04 DJ |
2520 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
2521 | ||
95954743 | 2522 | buffer[0] = ptrace (PTRACE_PEEKTEXT, pid, (PTRACE_ARG3_TYPE) addr, 0); |
da6d8c04 DJ |
2523 | |
2524 | if (count > 1) | |
2525 | { | |
2526 | buffer[count - 1] | |
95954743 | 2527 | = ptrace (PTRACE_PEEKTEXT, pid, |
d844cde6 DJ |
2528 | (PTRACE_ARG3_TYPE) (addr + (count - 1) |
2529 | * sizeof (PTRACE_XFER_TYPE)), | |
2530 | 0); | |
da6d8c04 DJ |
2531 | } |
2532 | ||
2533 | /* Copy data to be written over corresponding part of buffer */ | |
2534 | ||
2535 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len); | |
2536 | ||
2537 | /* Write the entire buffer. */ | |
2538 | ||
2539 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
2540 | { | |
2541 | errno = 0; | |
95954743 | 2542 | ptrace (PTRACE_POKETEXT, pid, (PTRACE_ARG3_TYPE) addr, buffer[i]); |
da6d8c04 DJ |
2543 | if (errno) |
2544 | return errno; | |
2545 | } | |
2546 | ||
2547 | return 0; | |
2548 | } | |
2f2893d9 | 2549 | |
24a09b5f DJ |
2550 | static int linux_supports_tracefork_flag; |
2551 | ||
51c2684e | 2552 | /* Helper functions for linux_test_for_tracefork, called via clone (). */ |
24a09b5f | 2553 | |
51c2684e DJ |
2554 | static int |
2555 | linux_tracefork_grandchild (void *arg) | |
2556 | { | |
2557 | _exit (0); | |
2558 | } | |
2559 | ||
7407e2de AS |
2560 | #define STACK_SIZE 4096 |
2561 | ||
51c2684e DJ |
2562 | static int |
2563 | linux_tracefork_child (void *arg) | |
24a09b5f DJ |
2564 | { |
2565 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
2566 | kill (getpid (), SIGSTOP); | |
7407e2de AS |
2567 | #ifdef __ia64__ |
2568 | __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE, | |
2569 | CLONE_VM | SIGCHLD, NULL); | |
2570 | #else | |
2571 | clone (linux_tracefork_grandchild, arg + STACK_SIZE, | |
2572 | CLONE_VM | SIGCHLD, NULL); | |
2573 | #endif | |
24a09b5f DJ |
2574 | _exit (0); |
2575 | } | |
2576 | ||
24a09b5f DJ |
2577 | /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make |
2578 | sure that we can enable the option, and that it had the desired | |
2579 | effect. */ | |
2580 | ||
2581 | static void | |
2582 | linux_test_for_tracefork (void) | |
2583 | { | |
2584 | int child_pid, ret, status; | |
2585 | long second_pid; | |
bca929d3 | 2586 | char *stack = xmalloc (STACK_SIZE * 4); |
24a09b5f DJ |
2587 | |
2588 | linux_supports_tracefork_flag = 0; | |
2589 | ||
51c2684e | 2590 | /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */ |
7407e2de AS |
2591 | #ifdef __ia64__ |
2592 | child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE, | |
2593 | CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2); | |
2594 | #else | |
2595 | child_pid = clone (linux_tracefork_child, stack + STACK_SIZE, | |
2596 | CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2); | |
2597 | #endif | |
24a09b5f | 2598 | if (child_pid == -1) |
51c2684e | 2599 | perror_with_name ("clone"); |
24a09b5f DJ |
2600 | |
2601 | ret = my_waitpid (child_pid, &status, 0); | |
2602 | if (ret == -1) | |
2603 | perror_with_name ("waitpid"); | |
2604 | else if (ret != child_pid) | |
2605 | error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret); | |
2606 | if (! WIFSTOPPED (status)) | |
2607 | error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status); | |
2608 | ||
2609 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK); | |
2610 | if (ret != 0) | |
2611 | { | |
2612 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); | |
2613 | if (ret != 0) | |
2614 | { | |
2615 | warning ("linux_test_for_tracefork: failed to kill child"); | |
2616 | return; | |
2617 | } | |
2618 | ||
2619 | ret = my_waitpid (child_pid, &status, 0); | |
2620 | if (ret != child_pid) | |
2621 | warning ("linux_test_for_tracefork: failed to wait for killed child"); | |
2622 | else if (!WIFSIGNALED (status)) | |
2623 | warning ("linux_test_for_tracefork: unexpected wait status 0x%x from " | |
2624 | "killed child", status); | |
2625 | ||
2626 | return; | |
2627 | } | |
2628 | ||
2629 | ret = ptrace (PTRACE_CONT, child_pid, 0, 0); | |
2630 | if (ret != 0) | |
2631 | warning ("linux_test_for_tracefork: failed to resume child"); | |
2632 | ||
2633 | ret = my_waitpid (child_pid, &status, 0); | |
2634 | ||
2635 | if (ret == child_pid && WIFSTOPPED (status) | |
2636 | && status >> 16 == PTRACE_EVENT_FORK) | |
2637 | { | |
2638 | second_pid = 0; | |
2639 | ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid); | |
2640 | if (ret == 0 && second_pid != 0) | |
2641 | { | |
2642 | int second_status; | |
2643 | ||
2644 | linux_supports_tracefork_flag = 1; | |
2645 | my_waitpid (second_pid, &second_status, 0); | |
2646 | ret = ptrace (PTRACE_KILL, second_pid, 0, 0); | |
2647 | if (ret != 0) | |
2648 | warning ("linux_test_for_tracefork: failed to kill second child"); | |
2649 | my_waitpid (second_pid, &status, 0); | |
2650 | } | |
2651 | } | |
2652 | else | |
2653 | warning ("linux_test_for_tracefork: unexpected result from waitpid " | |
2654 | "(%d, status 0x%x)", ret, status); | |
2655 | ||
2656 | do | |
2657 | { | |
2658 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); | |
2659 | if (ret != 0) | |
2660 | warning ("linux_test_for_tracefork: failed to kill child"); | |
2661 | my_waitpid (child_pid, &status, 0); | |
2662 | } | |
2663 | while (WIFSTOPPED (status)); | |
51c2684e DJ |
2664 | |
2665 | free (stack); | |
24a09b5f DJ |
2666 | } |
2667 | ||
2668 | ||
2f2893d9 DJ |
2669 | static void |
2670 | linux_look_up_symbols (void) | |
2671 | { | |
0d62e5e8 | 2672 | #ifdef USE_THREAD_DB |
95954743 PA |
2673 | struct process_info *proc = current_process (); |
2674 | ||
cdbfd419 | 2675 | if (proc->private->thread_db != NULL) |
0d62e5e8 DJ |
2676 | return; |
2677 | ||
cdbfd419 | 2678 | thread_db_init (!linux_supports_tracefork_flag); |
0d62e5e8 DJ |
2679 | #endif |
2680 | } | |
2681 | ||
e5379b03 | 2682 | static void |
ef57601b | 2683 | linux_request_interrupt (void) |
e5379b03 | 2684 | { |
a1928bad | 2685 | extern unsigned long signal_pid; |
e5379b03 | 2686 | |
95954743 PA |
2687 | if (!ptid_equal (cont_thread, null_ptid) |
2688 | && !ptid_equal (cont_thread, minus_one_ptid)) | |
e5379b03 | 2689 | { |
54a0b537 | 2690 | struct lwp_info *lwp; |
bd99dc85 | 2691 | int lwpid; |
e5379b03 | 2692 | |
54a0b537 | 2693 | lwp = get_thread_lwp (current_inferior); |
bd99dc85 PA |
2694 | lwpid = lwpid_of (lwp); |
2695 | kill_lwp (lwpid, SIGINT); | |
e5379b03 DJ |
2696 | } |
2697 | else | |
ef57601b | 2698 | kill_lwp (signal_pid, SIGINT); |
e5379b03 DJ |
2699 | } |
2700 | ||
aa691b87 RM |
2701 | /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET |
2702 | to debugger memory starting at MYADDR. */ | |
2703 | ||
2704 | static int | |
f450004a | 2705 | linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len) |
aa691b87 RM |
2706 | { |
2707 | char filename[PATH_MAX]; | |
2708 | int fd, n; | |
95954743 | 2709 | int pid = lwpid_of (get_thread_lwp (current_inferior)); |
aa691b87 | 2710 | |
95954743 | 2711 | snprintf (filename, sizeof filename, "/proc/%d/auxv", pid); |
aa691b87 RM |
2712 | |
2713 | fd = open (filename, O_RDONLY); | |
2714 | if (fd < 0) | |
2715 | return -1; | |
2716 | ||
2717 | if (offset != (CORE_ADDR) 0 | |
2718 | && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset) | |
2719 | n = -1; | |
2720 | else | |
2721 | n = read (fd, myaddr, len); | |
2722 | ||
2723 | close (fd); | |
2724 | ||
2725 | return n; | |
2726 | } | |
2727 | ||
d993e290 PA |
2728 | /* These breakpoint and watchpoint related wrapper functions simply |
2729 | pass on the function call if the target has registered a | |
2730 | corresponding function. */ | |
e013ee27 OF |
2731 | |
2732 | static int | |
d993e290 | 2733 | linux_insert_point (char type, CORE_ADDR addr, int len) |
e013ee27 | 2734 | { |
d993e290 PA |
2735 | if (the_low_target.insert_point != NULL) |
2736 | return the_low_target.insert_point (type, addr, len); | |
e013ee27 OF |
2737 | else |
2738 | /* Unsupported (see target.h). */ | |
2739 | return 1; | |
2740 | } | |
2741 | ||
2742 | static int | |
d993e290 | 2743 | linux_remove_point (char type, CORE_ADDR addr, int len) |
e013ee27 | 2744 | { |
d993e290 PA |
2745 | if (the_low_target.remove_point != NULL) |
2746 | return the_low_target.remove_point (type, addr, len); | |
e013ee27 OF |
2747 | else |
2748 | /* Unsupported (see target.h). */ | |
2749 | return 1; | |
2750 | } | |
2751 | ||
2752 | static int | |
2753 | linux_stopped_by_watchpoint (void) | |
2754 | { | |
2755 | if (the_low_target.stopped_by_watchpoint != NULL) | |
2756 | return the_low_target.stopped_by_watchpoint (); | |
2757 | else | |
2758 | return 0; | |
2759 | } | |
2760 | ||
2761 | static CORE_ADDR | |
2762 | linux_stopped_data_address (void) | |
2763 | { | |
2764 | if (the_low_target.stopped_data_address != NULL) | |
2765 | return the_low_target.stopped_data_address (); | |
2766 | else | |
2767 | return 0; | |
2768 | } | |
2769 | ||
42c81e2a | 2770 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) |
52fb6437 NS |
2771 | #if defined(__mcoldfire__) |
2772 | /* These should really be defined in the kernel's ptrace.h header. */ | |
2773 | #define PT_TEXT_ADDR 49*4 | |
2774 | #define PT_DATA_ADDR 50*4 | |
2775 | #define PT_TEXT_END_ADDR 51*4 | |
2776 | #endif | |
2777 | ||
2778 | /* Under uClinux, programs are loaded at non-zero offsets, which we need | |
2779 | to tell gdb about. */ | |
2780 | ||
2781 | static int | |
2782 | linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p) | |
2783 | { | |
2784 | #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR) | |
2785 | unsigned long text, text_end, data; | |
bd99dc85 | 2786 | int pid = lwpid_of (get_thread_lwp (current_inferior)); |
52fb6437 NS |
2787 | |
2788 | errno = 0; | |
2789 | ||
2790 | text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0); | |
2791 | text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0); | |
2792 | data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0); | |
2793 | ||
2794 | if (errno == 0) | |
2795 | { | |
2796 | /* Both text and data offsets produced at compile-time (and so | |
1b3f6016 PA |
2797 | used by gdb) are relative to the beginning of the program, |
2798 | with the data segment immediately following the text segment. | |
2799 | However, the actual runtime layout in memory may put the data | |
2800 | somewhere else, so when we send gdb a data base-address, we | |
2801 | use the real data base address and subtract the compile-time | |
2802 | data base-address from it (which is just the length of the | |
2803 | text segment). BSS immediately follows data in both | |
2804 | cases. */ | |
52fb6437 NS |
2805 | *text_p = text; |
2806 | *data_p = data - (text_end - text); | |
1b3f6016 | 2807 | |
52fb6437 NS |
2808 | return 1; |
2809 | } | |
2810 | #endif | |
2811 | return 0; | |
2812 | } | |
2813 | #endif | |
2814 | ||
dc146f7c VP |
2815 | static int |
2816 | compare_ints (const void *xa, const void *xb) | |
2817 | { | |
2818 | int a = *(const int *)xa; | |
2819 | int b = *(const int *)xb; | |
2820 | ||
2821 | return a - b; | |
2822 | } | |
2823 | ||
2824 | static int * | |
2825 | unique (int *b, int *e) | |
2826 | { | |
2827 | int *d = b; | |
2828 | while (++b != e) | |
2829 | if (*d != *b) | |
2830 | *++d = *b; | |
2831 | return ++d; | |
2832 | } | |
2833 | ||
2834 | /* Given PID, iterates over all threads in that process. | |
2835 | ||
2836 | Information about each thread, in a format suitable for qXfer:osdata:thread | |
2837 | is printed to BUFFER, if it's not NULL. BUFFER is assumed to be already | |
2838 | initialized, and the caller is responsible for finishing and appending '\0' | |
2839 | to it. | |
2840 | ||
2841 | The list of cores that threads are running on is assigned to *CORES, if it | |
2842 | is not NULL. If no cores are found, *CORES will be set to NULL. Caller | |
2843 | should free *CORES. */ | |
2844 | ||
2845 | static void | |
2846 | list_threads (int pid, struct buffer *buffer, char **cores) | |
2847 | { | |
2848 | int count = 0; | |
2849 | int allocated = 10; | |
2850 | int *core_numbers = xmalloc (sizeof (int) * allocated); | |
2851 | char pathname[128]; | |
2852 | DIR *dir; | |
2853 | struct dirent *dp; | |
2854 | struct stat statbuf; | |
2855 | ||
2856 | sprintf (pathname, "/proc/%d/task", pid); | |
2857 | if (stat (pathname, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)) | |
2858 | { | |
2859 | dir = opendir (pathname); | |
2860 | if (!dir) | |
2861 | { | |
2862 | free (core_numbers); | |
2863 | return; | |
2864 | } | |
2865 | ||
2866 | while ((dp = readdir (dir)) != NULL) | |
2867 | { | |
2868 | unsigned long lwp = strtoul (dp->d_name, NULL, 10); | |
2869 | ||
2870 | if (lwp != 0) | |
2871 | { | |
2872 | unsigned core = linux_core_of_thread (ptid_build (pid, lwp, 0)); | |
2873 | ||
2874 | if (core != -1) | |
2875 | { | |
2876 | char s[sizeof ("4294967295")]; | |
2877 | sprintf (s, "%u", core); | |
2878 | ||
2879 | if (count == allocated) | |
2880 | { | |
2881 | allocated *= 2; | |
2882 | core_numbers = realloc (core_numbers, | |
2883 | sizeof (int) * allocated); | |
2884 | } | |
2885 | core_numbers[count++] = core; | |
2886 | if (buffer) | |
2887 | buffer_xml_printf (buffer, | |
2888 | "<item>" | |
2889 | "<column name=\"pid\">%d</column>" | |
2890 | "<column name=\"tid\">%s</column>" | |
2891 | "<column name=\"core\">%s</column>" | |
2892 | "</item>", pid, dp->d_name, s); | |
2893 | } | |
2894 | else | |
2895 | { | |
2896 | if (buffer) | |
2897 | buffer_xml_printf (buffer, | |
2898 | "<item>" | |
2899 | "<column name=\"pid\">%d</column>" | |
2900 | "<column name=\"tid\">%s</column>" | |
2901 | "</item>", pid, dp->d_name); | |
2902 | } | |
2903 | } | |
2904 | } | |
2905 | } | |
2906 | ||
2907 | if (cores) | |
2908 | { | |
2909 | *cores = NULL; | |
2910 | if (count > 0) | |
2911 | { | |
2912 | struct buffer buffer2; | |
2913 | int *b; | |
2914 | int *e; | |
2915 | qsort (core_numbers, count, sizeof (int), compare_ints); | |
2916 | ||
2917 | /* Remove duplicates. */ | |
2918 | b = core_numbers; | |
2919 | e = unique (b, core_numbers + count); | |
2920 | ||
2921 | buffer_init (&buffer2); | |
2922 | ||
2923 | for (b = core_numbers; b != e; ++b) | |
2924 | { | |
2925 | char number[sizeof ("4294967295")]; | |
2926 | sprintf (number, "%u", *b); | |
2927 | buffer_xml_printf (&buffer2, "%s%s", | |
2928 | (b == core_numbers) ? "" : ",", number); | |
2929 | } | |
2930 | buffer_grow_str0 (&buffer2, ""); | |
2931 | ||
2932 | *cores = buffer_finish (&buffer2); | |
2933 | } | |
2934 | } | |
2935 | free (core_numbers); | |
2936 | } | |
2937 | ||
2938 | static void | |
2939 | show_process (int pid, const char *username, struct buffer *buffer) | |
2940 | { | |
2941 | char pathname[128]; | |
2942 | FILE *f; | |
2943 | char cmd[MAXPATHLEN + 1]; | |
2944 | ||
2945 | sprintf (pathname, "/proc/%d/cmdline", pid); | |
2946 | ||
2947 | if ((f = fopen (pathname, "r")) != NULL) | |
2948 | { | |
2949 | size_t len = fread (cmd, 1, sizeof (cmd) - 1, f); | |
2950 | if (len > 0) | |
2951 | { | |
2952 | char *cores = 0; | |
2953 | int i; | |
2954 | for (i = 0; i < len; i++) | |
2955 | if (cmd[i] == '\0') | |
2956 | cmd[i] = ' '; | |
2957 | cmd[len] = '\0'; | |
2958 | ||
2959 | buffer_xml_printf (buffer, | |
2960 | "<item>" | |
2961 | "<column name=\"pid\">%d</column>" | |
2962 | "<column name=\"user\">%s</column>" | |
2963 | "<column name=\"command\">%s</column>", | |
2964 | pid, | |
2965 | username, | |
2966 | cmd); | |
2967 | ||
2968 | /* This only collects core numbers, and does not print threads. */ | |
2969 | list_threads (pid, NULL, &cores); | |
2970 | ||
2971 | if (cores) | |
2972 | { | |
2973 | buffer_xml_printf (buffer, | |
2974 | "<column name=\"cores\">%s</column>", cores); | |
2975 | free (cores); | |
2976 | } | |
2977 | ||
2978 | buffer_xml_printf (buffer, "</item>"); | |
2979 | } | |
2980 | fclose (f); | |
2981 | } | |
2982 | } | |
2983 | ||
07e059b5 VP |
2984 | static int |
2985 | linux_qxfer_osdata (const char *annex, | |
1b3f6016 PA |
2986 | unsigned char *readbuf, unsigned const char *writebuf, |
2987 | CORE_ADDR offset, int len) | |
07e059b5 VP |
2988 | { |
2989 | /* We make the process list snapshot when the object starts to be | |
2990 | read. */ | |
2991 | static const char *buf; | |
2992 | static long len_avail = -1; | |
2993 | static struct buffer buffer; | |
dc146f7c VP |
2994 | int processes = 0; |
2995 | int threads = 0; | |
07e059b5 VP |
2996 | |
2997 | DIR *dirp; | |
2998 | ||
dc146f7c VP |
2999 | if (strcmp (annex, "processes") == 0) |
3000 | processes = 1; | |
3001 | else if (strcmp (annex, "threads") == 0) | |
3002 | threads = 1; | |
3003 | else | |
07e059b5 VP |
3004 | return 0; |
3005 | ||
3006 | if (!readbuf || writebuf) | |
3007 | return 0; | |
3008 | ||
3009 | if (offset == 0) | |
3010 | { | |
3011 | if (len_avail != -1 && len_avail != 0) | |
3012 | buffer_free (&buffer); | |
3013 | len_avail = 0; | |
3014 | buf = NULL; | |
3015 | buffer_init (&buffer); | |
dc146f7c VP |
3016 | if (processes) |
3017 | buffer_grow_str (&buffer, "<osdata type=\"processes\">"); | |
3018 | else if (threads) | |
3019 | buffer_grow_str (&buffer, "<osdata type=\"threads\">"); | |
07e059b5 VP |
3020 | |
3021 | dirp = opendir ("/proc"); | |
3022 | if (dirp) | |
3023 | { | |
1b3f6016 PA |
3024 | struct dirent *dp; |
3025 | while ((dp = readdir (dirp)) != NULL) | |
3026 | { | |
3027 | struct stat statbuf; | |
3028 | char procentry[sizeof ("/proc/4294967295")]; | |
3029 | ||
3030 | if (!isdigit (dp->d_name[0]) | |
3031 | || strlen (dp->d_name) > sizeof ("4294967295") - 1) | |
3032 | continue; | |
3033 | ||
3034 | sprintf (procentry, "/proc/%s", dp->d_name); | |
3035 | if (stat (procentry, &statbuf) == 0 | |
3036 | && S_ISDIR (statbuf.st_mode)) | |
3037 | { | |
dc146f7c | 3038 | int pid = (int) strtoul (dp->d_name, NULL, 10); |
1b3f6016 | 3039 | |
dc146f7c | 3040 | if (processes) |
1b3f6016 | 3041 | { |
dc146f7c VP |
3042 | struct passwd *entry = getpwuid (statbuf.st_uid); |
3043 | show_process (pid, entry ? entry->pw_name : "?", &buffer); | |
3044 | } | |
3045 | else if (threads) | |
3046 | { | |
3047 | list_threads (pid, &buffer, NULL); | |
1b3f6016 PA |
3048 | } |
3049 | } | |
3050 | } | |
07e059b5 | 3051 | |
1b3f6016 | 3052 | closedir (dirp); |
07e059b5 VP |
3053 | } |
3054 | buffer_grow_str0 (&buffer, "</osdata>\n"); | |
3055 | buf = buffer_finish (&buffer); | |
3056 | len_avail = strlen (buf); | |
3057 | } | |
3058 | ||
3059 | if (offset >= len_avail) | |
3060 | { | |
3061 | /* Done. Get rid of the data. */ | |
3062 | buffer_free (&buffer); | |
3063 | buf = NULL; | |
3064 | len_avail = 0; | |
3065 | return 0; | |
3066 | } | |
3067 | ||
3068 | if (len > len_avail - offset) | |
3069 | len = len_avail - offset; | |
3070 | memcpy (readbuf, buf + offset, len); | |
3071 | ||
3072 | return len; | |
3073 | } | |
3074 | ||
d0722149 DE |
3075 | /* Convert a native/host siginfo object, into/from the siginfo in the |
3076 | layout of the inferiors' architecture. */ | |
3077 | ||
3078 | static void | |
3079 | siginfo_fixup (struct siginfo *siginfo, void *inf_siginfo, int direction) | |
3080 | { | |
3081 | int done = 0; | |
3082 | ||
3083 | if (the_low_target.siginfo_fixup != NULL) | |
3084 | done = the_low_target.siginfo_fixup (siginfo, inf_siginfo, direction); | |
3085 | ||
3086 | /* If there was no callback, or the callback didn't do anything, | |
3087 | then just do a straight memcpy. */ | |
3088 | if (!done) | |
3089 | { | |
3090 | if (direction == 1) | |
3091 | memcpy (siginfo, inf_siginfo, sizeof (struct siginfo)); | |
3092 | else | |
3093 | memcpy (inf_siginfo, siginfo, sizeof (struct siginfo)); | |
3094 | } | |
3095 | } | |
3096 | ||
4aa995e1 PA |
3097 | static int |
3098 | linux_xfer_siginfo (const char *annex, unsigned char *readbuf, | |
3099 | unsigned const char *writebuf, CORE_ADDR offset, int len) | |
3100 | { | |
d0722149 | 3101 | int pid; |
4aa995e1 | 3102 | struct siginfo siginfo; |
d0722149 | 3103 | char inf_siginfo[sizeof (struct siginfo)]; |
4aa995e1 PA |
3104 | |
3105 | if (current_inferior == NULL) | |
3106 | return -1; | |
3107 | ||
bd99dc85 | 3108 | pid = lwpid_of (get_thread_lwp (current_inferior)); |
4aa995e1 PA |
3109 | |
3110 | if (debug_threads) | |
d0722149 | 3111 | fprintf (stderr, "%s siginfo for lwp %d.\n", |
4aa995e1 PA |
3112 | readbuf != NULL ? "Reading" : "Writing", |
3113 | pid); | |
3114 | ||
3115 | if (offset > sizeof (siginfo)) | |
3116 | return -1; | |
3117 | ||
3118 | if (ptrace (PTRACE_GETSIGINFO, pid, 0, &siginfo) != 0) | |
3119 | return -1; | |
3120 | ||
d0722149 DE |
3121 | /* When GDBSERVER is built as a 64-bit application, ptrace writes into |
3122 | SIGINFO an object with 64-bit layout. Since debugging a 32-bit | |
3123 | inferior with a 64-bit GDBSERVER should look the same as debugging it | |
3124 | with a 32-bit GDBSERVER, we need to convert it. */ | |
3125 | siginfo_fixup (&siginfo, inf_siginfo, 0); | |
3126 | ||
4aa995e1 PA |
3127 | if (offset + len > sizeof (siginfo)) |
3128 | len = sizeof (siginfo) - offset; | |
3129 | ||
3130 | if (readbuf != NULL) | |
d0722149 | 3131 | memcpy (readbuf, inf_siginfo + offset, len); |
4aa995e1 PA |
3132 | else |
3133 | { | |
d0722149 DE |
3134 | memcpy (inf_siginfo + offset, writebuf, len); |
3135 | ||
3136 | /* Convert back to ptrace layout before flushing it out. */ | |
3137 | siginfo_fixup (&siginfo, inf_siginfo, 1); | |
3138 | ||
4aa995e1 PA |
3139 | if (ptrace (PTRACE_SETSIGINFO, pid, 0, &siginfo) != 0) |
3140 | return -1; | |
3141 | } | |
3142 | ||
3143 | return len; | |
3144 | } | |
3145 | ||
bd99dc85 PA |
3146 | /* SIGCHLD handler that serves two purposes: In non-stop/async mode, |
3147 | so we notice when children change state; as the handler for the | |
3148 | sigsuspend in my_waitpid. */ | |
3149 | ||
3150 | static void | |
3151 | sigchld_handler (int signo) | |
3152 | { | |
3153 | int old_errno = errno; | |
3154 | ||
3155 | if (debug_threads) | |
3156 | /* fprintf is not async-signal-safe, so call write directly. */ | |
3157 | write (2, "sigchld_handler\n", sizeof ("sigchld_handler\n") - 1); | |
3158 | ||
3159 | if (target_is_async_p ()) | |
3160 | async_file_mark (); /* trigger a linux_wait */ | |
3161 | ||
3162 | errno = old_errno; | |
3163 | } | |
3164 | ||
3165 | static int | |
3166 | linux_supports_non_stop (void) | |
3167 | { | |
3168 | return 1; | |
3169 | } | |
3170 | ||
3171 | static int | |
3172 | linux_async (int enable) | |
3173 | { | |
3174 | int previous = (linux_event_pipe[0] != -1); | |
3175 | ||
3176 | if (previous != enable) | |
3177 | { | |
3178 | sigset_t mask; | |
3179 | sigemptyset (&mask); | |
3180 | sigaddset (&mask, SIGCHLD); | |
3181 | ||
3182 | sigprocmask (SIG_BLOCK, &mask, NULL); | |
3183 | ||
3184 | if (enable) | |
3185 | { | |
3186 | if (pipe (linux_event_pipe) == -1) | |
3187 | fatal ("creating event pipe failed."); | |
3188 | ||
3189 | fcntl (linux_event_pipe[0], F_SETFL, O_NONBLOCK); | |
3190 | fcntl (linux_event_pipe[1], F_SETFL, O_NONBLOCK); | |
3191 | ||
3192 | /* Register the event loop handler. */ | |
3193 | add_file_handler (linux_event_pipe[0], | |
3194 | handle_target_event, NULL); | |
3195 | ||
3196 | /* Always trigger a linux_wait. */ | |
3197 | async_file_mark (); | |
3198 | } | |
3199 | else | |
3200 | { | |
3201 | delete_file_handler (linux_event_pipe[0]); | |
3202 | ||
3203 | close (linux_event_pipe[0]); | |
3204 | close (linux_event_pipe[1]); | |
3205 | linux_event_pipe[0] = -1; | |
3206 | linux_event_pipe[1] = -1; | |
3207 | } | |
3208 | ||
3209 | sigprocmask (SIG_UNBLOCK, &mask, NULL); | |
3210 | } | |
3211 | ||
3212 | return previous; | |
3213 | } | |
3214 | ||
3215 | static int | |
3216 | linux_start_non_stop (int nonstop) | |
3217 | { | |
3218 | /* Register or unregister from event-loop accordingly. */ | |
3219 | linux_async (nonstop); | |
3220 | return 0; | |
3221 | } | |
3222 | ||
cf8fd78b PA |
3223 | static int |
3224 | linux_supports_multi_process (void) | |
3225 | { | |
3226 | return 1; | |
3227 | } | |
3228 | ||
efcbbd14 UW |
3229 | |
3230 | /* Enumerate spufs IDs for process PID. */ | |
3231 | static int | |
3232 | spu_enumerate_spu_ids (long pid, unsigned char *buf, CORE_ADDR offset, int len) | |
3233 | { | |
3234 | int pos = 0; | |
3235 | int written = 0; | |
3236 | char path[128]; | |
3237 | DIR *dir; | |
3238 | struct dirent *entry; | |
3239 | ||
3240 | sprintf (path, "/proc/%ld/fd", pid); | |
3241 | dir = opendir (path); | |
3242 | if (!dir) | |
3243 | return -1; | |
3244 | ||
3245 | rewinddir (dir); | |
3246 | while ((entry = readdir (dir)) != NULL) | |
3247 | { | |
3248 | struct stat st; | |
3249 | struct statfs stfs; | |
3250 | int fd; | |
3251 | ||
3252 | fd = atoi (entry->d_name); | |
3253 | if (!fd) | |
3254 | continue; | |
3255 | ||
3256 | sprintf (path, "/proc/%ld/fd/%d", pid, fd); | |
3257 | if (stat (path, &st) != 0) | |
3258 | continue; | |
3259 | if (!S_ISDIR (st.st_mode)) | |
3260 | continue; | |
3261 | ||
3262 | if (statfs (path, &stfs) != 0) | |
3263 | continue; | |
3264 | if (stfs.f_type != SPUFS_MAGIC) | |
3265 | continue; | |
3266 | ||
3267 | if (pos >= offset && pos + 4 <= offset + len) | |
3268 | { | |
3269 | *(unsigned int *)(buf + pos - offset) = fd; | |
3270 | written += 4; | |
3271 | } | |
3272 | pos += 4; | |
3273 | } | |
3274 | ||
3275 | closedir (dir); | |
3276 | return written; | |
3277 | } | |
3278 | ||
3279 | /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU | |
3280 | object type, using the /proc file system. */ | |
3281 | static int | |
3282 | linux_qxfer_spu (const char *annex, unsigned char *readbuf, | |
3283 | unsigned const char *writebuf, | |
3284 | CORE_ADDR offset, int len) | |
3285 | { | |
3286 | long pid = lwpid_of (get_thread_lwp (current_inferior)); | |
3287 | char buf[128]; | |
3288 | int fd = 0; | |
3289 | int ret = 0; | |
3290 | ||
3291 | if (!writebuf && !readbuf) | |
3292 | return -1; | |
3293 | ||
3294 | if (!*annex) | |
3295 | { | |
3296 | if (!readbuf) | |
3297 | return -1; | |
3298 | else | |
3299 | return spu_enumerate_spu_ids (pid, readbuf, offset, len); | |
3300 | } | |
3301 | ||
3302 | sprintf (buf, "/proc/%ld/fd/%s", pid, annex); | |
3303 | fd = open (buf, writebuf? O_WRONLY : O_RDONLY); | |
3304 | if (fd <= 0) | |
3305 | return -1; | |
3306 | ||
3307 | if (offset != 0 | |
3308 | && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset) | |
3309 | { | |
3310 | close (fd); | |
3311 | return 0; | |
3312 | } | |
3313 | ||
3314 | if (writebuf) | |
3315 | ret = write (fd, writebuf, (size_t) len); | |
3316 | else | |
3317 | ret = read (fd, readbuf, (size_t) len); | |
3318 | ||
3319 | close (fd); | |
3320 | return ret; | |
3321 | } | |
3322 | ||
dc146f7c VP |
3323 | static int |
3324 | linux_core_of_thread (ptid_t ptid) | |
3325 | { | |
3326 | char filename[sizeof ("/proc//task//stat") | |
3327 | + 2 * 20 /* decimal digits for 2 numbers, max 2^64 bit each */ | |
3328 | + 1]; | |
3329 | FILE *f; | |
3330 | char *content = NULL; | |
3331 | char *p; | |
3332 | char *ts = 0; | |
3333 | int content_read = 0; | |
3334 | int i; | |
3335 | int core; | |
3336 | ||
3337 | sprintf (filename, "/proc/%d/task/%ld/stat", | |
3338 | ptid_get_pid (ptid), ptid_get_lwp (ptid)); | |
3339 | f = fopen (filename, "r"); | |
3340 | if (!f) | |
3341 | return -1; | |
3342 | ||
3343 | for (;;) | |
3344 | { | |
3345 | int n; | |
3346 | content = realloc (content, content_read + 1024); | |
3347 | n = fread (content + content_read, 1, 1024, f); | |
3348 | content_read += n; | |
3349 | if (n < 1024) | |
3350 | { | |
3351 | content[content_read] = '\0'; | |
3352 | break; | |
3353 | } | |
3354 | } | |
3355 | ||
3356 | p = strchr (content, '('); | |
3357 | p = strchr (p, ')') + 2; /* skip ")" and a whitespace. */ | |
3358 | ||
3359 | p = strtok_r (p, " ", &ts); | |
3360 | for (i = 0; i != 36; ++i) | |
3361 | p = strtok_r (NULL, " ", &ts); | |
3362 | ||
3363 | if (sscanf (p, "%d", &core) == 0) | |
3364 | core = -1; | |
3365 | ||
3366 | free (content); | |
3367 | fclose (f); | |
3368 | ||
3369 | return core; | |
3370 | } | |
3371 | ||
ce3a066d DJ |
3372 | static struct target_ops linux_target_ops = { |
3373 | linux_create_inferior, | |
3374 | linux_attach, | |
3375 | linux_kill, | |
6ad8ae5c | 3376 | linux_detach, |
444d6139 | 3377 | linux_join, |
ce3a066d DJ |
3378 | linux_thread_alive, |
3379 | linux_resume, | |
3380 | linux_wait, | |
3381 | linux_fetch_registers, | |
3382 | linux_store_registers, | |
3383 | linux_read_memory, | |
3384 | linux_write_memory, | |
2f2893d9 | 3385 | linux_look_up_symbols, |
ef57601b | 3386 | linux_request_interrupt, |
aa691b87 | 3387 | linux_read_auxv, |
d993e290 PA |
3388 | linux_insert_point, |
3389 | linux_remove_point, | |
e013ee27 OF |
3390 | linux_stopped_by_watchpoint, |
3391 | linux_stopped_data_address, | |
42c81e2a | 3392 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) |
52fb6437 | 3393 | linux_read_offsets, |
dae5f5cf DJ |
3394 | #else |
3395 | NULL, | |
3396 | #endif | |
3397 | #ifdef USE_THREAD_DB | |
3398 | thread_db_get_tls_address, | |
3399 | #else | |
3400 | NULL, | |
52fb6437 | 3401 | #endif |
efcbbd14 | 3402 | linux_qxfer_spu, |
59a016f0 | 3403 | hostio_last_error_from_errno, |
07e059b5 | 3404 | linux_qxfer_osdata, |
4aa995e1 | 3405 | linux_xfer_siginfo, |
bd99dc85 PA |
3406 | linux_supports_non_stop, |
3407 | linux_async, | |
3408 | linux_start_non_stop, | |
cdbfd419 PP |
3409 | linux_supports_multi_process, |
3410 | #ifdef USE_THREAD_DB | |
dc146f7c | 3411 | thread_db_handle_monitor_command, |
cdbfd419 | 3412 | #else |
dc146f7c | 3413 | NULL, |
cdbfd419 | 3414 | #endif |
dc146f7c | 3415 | linux_core_of_thread |
ce3a066d DJ |
3416 | }; |
3417 | ||
0d62e5e8 DJ |
3418 | static void |
3419 | linux_init_signals () | |
3420 | { | |
3421 | /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads | |
3422 | to find what the cancel signal actually is. */ | |
60c3d7b0 | 3423 | #ifdef __SIGRTMIN /* Bionic doesn't use SIGRTMIN the way glibc does. */ |
254787d4 | 3424 | signal (__SIGRTMIN+1, SIG_IGN); |
60c3d7b0 | 3425 | #endif |
0d62e5e8 DJ |
3426 | } |
3427 | ||
da6d8c04 DJ |
3428 | void |
3429 | initialize_low (void) | |
3430 | { | |
bd99dc85 PA |
3431 | struct sigaction sigchld_action; |
3432 | memset (&sigchld_action, 0, sizeof (sigchld_action)); | |
ce3a066d | 3433 | set_target_ops (&linux_target_ops); |
611cb4a5 DJ |
3434 | set_breakpoint_data (the_low_target.breakpoint, |
3435 | the_low_target.breakpoint_len); | |
0d62e5e8 | 3436 | linux_init_signals (); |
24a09b5f | 3437 | linux_test_for_tracefork (); |
52fa2412 UW |
3438 | #ifdef HAVE_LINUX_REGSETS |
3439 | for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++) | |
3440 | ; | |
bca929d3 | 3441 | disabled_regsets = xmalloc (num_regsets); |
52fa2412 | 3442 | #endif |
bd99dc85 PA |
3443 | |
3444 | sigchld_action.sa_handler = sigchld_handler; | |
3445 | sigemptyset (&sigchld_action.sa_mask); | |
3446 | sigchld_action.sa_flags = SA_RESTART; | |
3447 | sigaction (SIGCHLD, &sigchld_action, NULL); | |
da6d8c04 | 3448 | } |