* linux-low.c (linux_wait_1): Avoid setting need_step_over is
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-low.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "server.h"
21 #include "linux-low.h"
22
23 #include <sys/wait.h>
24 #include <stdio.h>
25 #include <sys/param.h>
26 #include <sys/ptrace.h>
27 #include <signal.h>
28 #include <sys/ioctl.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <sys/syscall.h>
35 #include <sched.h>
36 #include <ctype.h>
37 #include <pwd.h>
38 #include <sys/types.h>
39 #include <dirent.h>
40 #include <sys/stat.h>
41 #include <sys/vfs.h>
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
49
50 #ifndef SPUFS_MAGIC
51 #define SPUFS_MAGIC 0x23c9b64e
52 #endif
53
54 #ifndef PTRACE_GETSIGINFO
55 # define PTRACE_GETSIGINFO 0x4202
56 # define PTRACE_SETSIGINFO 0x4203
57 #endif
58
59 #ifndef O_LARGEFILE
60 #define O_LARGEFILE 0
61 #endif
62
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
96 #ifndef W_STOPCODE
97 #define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
98 #endif
99
100 #ifdef __UCLIBC__
101 #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
102 #define HAS_NOMMU
103 #endif
104 #endif
105
106 /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
107 representation of the thread ID.
108
109 ``all_lwps'' is keyed by the process ID - which on Linux is (presently)
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". */
114
115 struct inferior_list all_lwps;
116
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
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? */
127 int using_threads = 1;
128
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. */
136 static int new_inferior;
137
138 static void linux_resume_one_lwp (struct lwp_info *lwp,
139 int step, int signal, siginfo_t *info);
140 static void linux_resume (struct thread_resume *resume_info, size_t n);
141 static void stop_all_lwps (void);
142 static int linux_wait_for_event (ptid_t ptid, int *wstat, int options);
143 static void *add_lwp (ptid_t ptid);
144 static int linux_stopped_by_watchpoint (void);
145 static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
146 static int linux_core_of_thread (ptid_t ptid);
147 static void proceed_all_lwps (void);
148 static void unstop_all_lwps (struct lwp_info *except);
149 static int finish_step_over (struct lwp_info *lwp);
150 static CORE_ADDR get_stop_pc (struct lwp_info *lwp);
151 static int kill_lwp (unsigned long lwpid, int signo);
152
153 /* True if the low target can hardware single-step. Such targets
154 don't need a BREAKPOINT_REINSERT_ADDR callback. */
155
156 static int
157 can_hardware_single_step (void)
158 {
159 return (the_low_target.breakpoint_reinsert_addr == NULL);
160 }
161
162 /* True if the low target supports memory breakpoints. If so, we'll
163 have a GET_PC implementation. */
164
165 static int
166 supports_breakpoints (void)
167 {
168 return (the_low_target.get_pc != NULL);
169 }
170
171 struct pending_signals
172 {
173 int signal;
174 siginfo_t info;
175 struct pending_signals *prev;
176 };
177
178 #define PTRACE_ARG3_TYPE void *
179 #define PTRACE_ARG4_TYPE void *
180 #define PTRACE_XFER_TYPE long
181
182 #ifdef HAVE_LINUX_REGSETS
183 static char *disabled_regsets;
184 static int num_regsets;
185 #endif
186
187 /* The read/write ends of the pipe registered as waitable file in the
188 event loop. */
189 static int linux_event_pipe[2] = { -1, -1 };
190
191 /* True if we're currently in async mode. */
192 #define target_is_async_p() (linux_event_pipe[0] != -1)
193
194 static void send_sigstop (struct inferior_list_entry *entry);
195 static void wait_for_sigstop (struct inferior_list_entry *entry);
196
197 /* Accepts an integer PID; Returns a string representing a file that
198 can be opened to get info for the child process.
199 Space for the result is malloc'd, caller must free. */
200
201 char *
202 linux_child_pid_to_exec_file (int pid)
203 {
204 char *name1, *name2;
205
206 name1 = xmalloc (MAXPATHLEN);
207 name2 = xmalloc (MAXPATHLEN);
208 memset (name2, 0, MAXPATHLEN);
209
210 sprintf (name1, "/proc/%d/exe", pid);
211 if (readlink (name1, name2, MAXPATHLEN) > 0)
212 {
213 free (name1);
214 return name2;
215 }
216 else
217 {
218 free (name2);
219 return name1;
220 }
221 }
222
223 /* Return non-zero if HEADER is a 64-bit ELF file. */
224
225 static int
226 elf_64_header_p (const Elf64_Ehdr *header)
227 {
228 return (header->e_ident[EI_MAG0] == ELFMAG0
229 && header->e_ident[EI_MAG1] == ELFMAG1
230 && header->e_ident[EI_MAG2] == ELFMAG2
231 && header->e_ident[EI_MAG3] == ELFMAG3
232 && header->e_ident[EI_CLASS] == ELFCLASS64);
233 }
234
235 /* Return non-zero if FILE is a 64-bit ELF file,
236 zero if the file is not a 64-bit ELF file,
237 and -1 if the file is not accessible or doesn't exist. */
238
239 int
240 elf_64_file_p (const char *file)
241 {
242 Elf64_Ehdr header;
243 int fd;
244
245 fd = open (file, O_RDONLY);
246 if (fd < 0)
247 return -1;
248
249 if (read (fd, &header, sizeof (header)) != sizeof (header))
250 {
251 close (fd);
252 return 0;
253 }
254 close (fd);
255
256 return elf_64_header_p (&header);
257 }
258
259 static void
260 delete_lwp (struct lwp_info *lwp)
261 {
262 remove_thread (get_lwp_thread (lwp));
263 remove_inferior (&all_lwps, &lwp->head);
264 free (lwp->arch_private);
265 free (lwp);
266 }
267
268 /* Add a process to the common process list, and set its private
269 data. */
270
271 static struct process_info *
272 linux_add_process (int pid, int attached)
273 {
274 struct process_info *proc;
275
276 /* Is this the first process? If so, then set the arch. */
277 if (all_processes.head == NULL)
278 new_inferior = 1;
279
280 proc = add_process (pid, attached);
281 proc->private = xcalloc (1, sizeof (*proc->private));
282
283 if (the_low_target.new_process != NULL)
284 proc->private->arch_private = the_low_target.new_process ();
285
286 return proc;
287 }
288
289 /* Remove a process from the common process list,
290 also freeing all private data. */
291
292 static void
293 linux_remove_process (struct process_info *process)
294 {
295 struct process_info_private *priv = process->private;
296
297 free (priv->arch_private);
298 free (priv);
299 remove_process (process);
300 }
301
302 /* Wrapper function for waitpid which handles EINTR, and emulates
303 __WALL for systems where that is not available. */
304
305 static int
306 my_waitpid (int pid, int *status, int flags)
307 {
308 int ret, out_errno;
309
310 if (debug_threads)
311 fprintf (stderr, "my_waitpid (%d, 0x%x)\n", pid, flags);
312
313 if (flags & __WALL)
314 {
315 sigset_t block_mask, org_mask, wake_mask;
316 int wnohang;
317
318 wnohang = (flags & WNOHANG) != 0;
319 flags &= ~(__WALL | __WCLONE);
320 flags |= WNOHANG;
321
322 /* Block all signals while here. This avoids knowing about
323 LinuxThread's signals. */
324 sigfillset (&block_mask);
325 sigprocmask (SIG_BLOCK, &block_mask, &org_mask);
326
327 /* ... except during the sigsuspend below. */
328 sigemptyset (&wake_mask);
329
330 while (1)
331 {
332 /* Since all signals are blocked, there's no need to check
333 for EINTR here. */
334 ret = waitpid (pid, status, flags);
335 out_errno = errno;
336
337 if (ret == -1 && out_errno != ECHILD)
338 break;
339 else if (ret > 0)
340 break;
341
342 if (flags & __WCLONE)
343 {
344 /* We've tried both flavors now. If WNOHANG is set,
345 there's nothing else to do, just bail out. */
346 if (wnohang)
347 break;
348
349 if (debug_threads)
350 fprintf (stderr, "blocking\n");
351
352 /* Block waiting for signals. */
353 sigsuspend (&wake_mask);
354 }
355
356 flags ^= __WCLONE;
357 }
358
359 sigprocmask (SIG_SETMASK, &org_mask, NULL);
360 }
361 else
362 {
363 do
364 ret = waitpid (pid, status, flags);
365 while (ret == -1 && errno == EINTR);
366 out_errno = errno;
367 }
368
369 if (debug_threads)
370 fprintf (stderr, "my_waitpid (%d, 0x%x): status(%x), %d\n",
371 pid, flags, status ? *status : -1, ret);
372
373 errno = out_errno;
374 return ret;
375 }
376
377 /* Handle a GNU/Linux extended wait response. If we see a clone
378 event, we need to add the new LWP to our list (and not report the
379 trap to higher layers). */
380
381 static void
382 handle_extended_wait (struct lwp_info *event_child, int wstat)
383 {
384 int event = wstat >> 16;
385 struct lwp_info *new_lwp;
386
387 if (event == PTRACE_EVENT_CLONE)
388 {
389 ptid_t ptid;
390 unsigned long new_pid;
391 int ret, status = W_STOPCODE (SIGSTOP);
392
393 ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_child), 0, &new_pid);
394
395 /* If we haven't already seen the new PID stop, wait for it now. */
396 if (! pull_pid_from_list (&stopped_pids, new_pid))
397 {
398 /* The new child has a pending SIGSTOP. We can't affect it until it
399 hits the SIGSTOP, but we're already attached. */
400
401 ret = my_waitpid (new_pid, &status, __WALL);
402
403 if (ret == -1)
404 perror_with_name ("waiting for new child");
405 else if (ret != new_pid)
406 warning ("wait returned unexpected PID %d", ret);
407 else if (!WIFSTOPPED (status))
408 warning ("wait returned unexpected status 0x%x", status);
409 }
410
411 ptrace (PTRACE_SETOPTIONS, new_pid, 0, (PTRACE_ARG4_TYPE) PTRACE_O_TRACECLONE);
412
413 ptid = ptid_build (pid_of (event_child), new_pid, 0);
414 new_lwp = (struct lwp_info *) add_lwp (ptid);
415 add_thread (ptid, new_lwp);
416
417 /* Either we're going to immediately resume the new thread
418 or leave it stopped. linux_resume_one_lwp is a nop if it
419 thinks the thread is currently running, so set this first
420 before calling linux_resume_one_lwp. */
421 new_lwp->stopped = 1;
422
423 /* Normally we will get the pending SIGSTOP. But in some cases
424 we might get another signal delivered to the group first.
425 If we do get another signal, be sure not to lose it. */
426 if (WSTOPSIG (status) == SIGSTOP)
427 {
428 if (stopping_threads)
429 new_lwp->stop_pc = get_stop_pc (new_lwp);
430 else
431 linux_resume_one_lwp (new_lwp, 0, 0, NULL);
432 }
433 else
434 {
435 new_lwp->stop_expected = 1;
436
437 if (stopping_threads)
438 {
439 new_lwp->stop_pc = get_stop_pc (new_lwp);
440 new_lwp->status_pending_p = 1;
441 new_lwp->status_pending = status;
442 }
443 else
444 /* Pass the signal on. This is what GDB does - except
445 shouldn't we really report it instead? */
446 linux_resume_one_lwp (new_lwp, 0, WSTOPSIG (status), NULL);
447 }
448
449 /* Always resume the current thread. If we are stopping
450 threads, it will have a pending SIGSTOP; we may as well
451 collect it now. */
452 linux_resume_one_lwp (event_child, event_child->stepping, 0, NULL);
453 }
454 }
455
456 /* Return the PC as read from the regcache of LWP, without any
457 adjustment. */
458
459 static CORE_ADDR
460 get_pc (struct lwp_info *lwp)
461 {
462 struct thread_info *saved_inferior;
463 struct regcache *regcache;
464 CORE_ADDR pc;
465
466 if (the_low_target.get_pc == NULL)
467 return 0;
468
469 saved_inferior = current_inferior;
470 current_inferior = get_lwp_thread (lwp);
471
472 regcache = get_thread_regcache (current_inferior, 1);
473 pc = (*the_low_target.get_pc) (regcache);
474
475 if (debug_threads)
476 fprintf (stderr, "pc is 0x%lx\n", (long) pc);
477
478 current_inferior = saved_inferior;
479 return pc;
480 }
481
482 /* This function should only be called if LWP got a SIGTRAP.
483 The SIGTRAP could mean several things.
484
485 On i386, where decr_pc_after_break is non-zero:
486 If we were single-stepping this process using PTRACE_SINGLESTEP,
487 we will get only the one SIGTRAP (even if the instruction we
488 stepped over was a breakpoint). The value of $eip will be the
489 next instruction.
490 If we continue the process using PTRACE_CONT, we will get a
491 SIGTRAP when we hit a breakpoint. The value of $eip will be
492 the instruction after the breakpoint (i.e. needs to be
493 decremented). If we report the SIGTRAP to GDB, we must also
494 report the undecremented PC. If we cancel the SIGTRAP, we
495 must resume at the decremented PC.
496
497 (Presumably, not yet tested) On a non-decr_pc_after_break machine
498 with hardware or kernel single-step:
499 If we single-step over a breakpoint instruction, our PC will
500 point at the following instruction. If we continue and hit a
501 breakpoint instruction, our PC will point at the breakpoint
502 instruction. */
503
504 static CORE_ADDR
505 get_stop_pc (struct lwp_info *lwp)
506 {
507 CORE_ADDR stop_pc;
508
509 if (the_low_target.get_pc == NULL)
510 return 0;
511
512 stop_pc = get_pc (lwp);
513
514 if (WSTOPSIG (lwp->last_status) == SIGTRAP && !lwp->stepping)
515 stop_pc -= the_low_target.decr_pc_after_break;
516
517 if (debug_threads)
518 fprintf (stderr, "stop pc is 0x%lx\n", (long) stop_pc);
519
520 return stop_pc;
521 }
522
523 static void *
524 add_lwp (ptid_t ptid)
525 {
526 struct lwp_info *lwp;
527
528 lwp = (struct lwp_info *) xmalloc (sizeof (*lwp));
529 memset (lwp, 0, sizeof (*lwp));
530
531 lwp->head.id = ptid;
532
533 lwp->last_resume_kind = resume_continue;
534
535 if (the_low_target.new_thread != NULL)
536 lwp->arch_private = the_low_target.new_thread ();
537
538 add_inferior_to_list (&all_lwps, &lwp->head);
539
540 return lwp;
541 }
542
543 /* Start an inferior process and returns its pid.
544 ALLARGS is a vector of program-name and args. */
545
546 static int
547 linux_create_inferior (char *program, char **allargs)
548 {
549 struct lwp_info *new_lwp;
550 int pid;
551 ptid_t ptid;
552
553 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
554 pid = vfork ();
555 #else
556 pid = fork ();
557 #endif
558 if (pid < 0)
559 perror_with_name ("fork");
560
561 if (pid == 0)
562 {
563 ptrace (PTRACE_TRACEME, 0, 0, 0);
564
565 #ifdef __SIGRTMIN /* Bionic doesn't use SIGRTMIN the way glibc does. */
566 signal (__SIGRTMIN + 1, SIG_DFL);
567 #endif
568
569 setpgid (0, 0);
570
571 execv (program, allargs);
572 if (errno == ENOENT)
573 execvp (program, allargs);
574
575 fprintf (stderr, "Cannot exec %s: %s.\n", program,
576 strerror (errno));
577 fflush (stderr);
578 _exit (0177);
579 }
580
581 linux_add_process (pid, 0);
582
583 ptid = ptid_build (pid, pid, 0);
584 new_lwp = add_lwp (ptid);
585 add_thread (ptid, new_lwp);
586 new_lwp->must_set_ptrace_flags = 1;
587
588 return pid;
589 }
590
591 /* Attach to an inferior process. */
592
593 static void
594 linux_attach_lwp_1 (unsigned long lwpid, int initial)
595 {
596 ptid_t ptid;
597 struct lwp_info *new_lwp;
598
599 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) != 0)
600 {
601 if (!initial)
602 {
603 /* If we fail to attach to an LWP, just warn. */
604 fprintf (stderr, "Cannot attach to lwp %ld: %s (%d)\n", lwpid,
605 strerror (errno), errno);
606 fflush (stderr);
607 return;
608 }
609 else
610 /* If we fail to attach to a process, report an error. */
611 error ("Cannot attach to lwp %ld: %s (%d)\n", lwpid,
612 strerror (errno), errno);
613 }
614
615 if (initial)
616 /* NOTE/FIXME: This lwp might have not been the tgid. */
617 ptid = ptid_build (lwpid, lwpid, 0);
618 else
619 {
620 /* Note that extracting the pid from the current inferior is
621 safe, since we're always called in the context of the same
622 process as this new thread. */
623 int pid = pid_of (get_thread_lwp (current_inferior));
624 ptid = ptid_build (pid, lwpid, 0);
625 }
626
627 new_lwp = (struct lwp_info *) add_lwp (ptid);
628 add_thread (ptid, new_lwp);
629
630 /* We need to wait for SIGSTOP before being able to make the next
631 ptrace call on this LWP. */
632 new_lwp->must_set_ptrace_flags = 1;
633
634 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
635 brings it to a halt.
636
637 There are several cases to consider here:
638
639 1) gdbserver has already attached to the process and is being notified
640 of a new thread that is being created.
641 In this case we should ignore that SIGSTOP and resume the
642 process. This is handled below by setting stop_expected = 1,
643 and the fact that add_lwp sets last_resume_kind ==
644 resume_continue.
645
646 2) This is the first thread (the process thread), and we're attaching
647 to it via attach_inferior.
648 In this case we want the process thread to stop.
649 This is handled by having linux_attach set last_resume_kind ==
650 resume_stop after we return.
651 ??? If the process already has several threads we leave the other
652 threads running.
653
654 3) GDB is connecting to gdbserver and is requesting an enumeration of all
655 existing threads.
656 In this case we want the thread to stop.
657 FIXME: This case is currently not properly handled.
658 We should wait for the SIGSTOP but don't. Things work apparently
659 because enough time passes between when we ptrace (ATTACH) and when
660 gdb makes the next ptrace call on the thread.
661
662 On the other hand, if we are currently trying to stop all threads, we
663 should treat the new thread as if we had sent it a SIGSTOP. This works
664 because we are guaranteed that the add_lwp call above added us to the
665 end of the list, and so the new thread has not yet reached
666 wait_for_sigstop (but will). */
667 new_lwp->stop_expected = 1;
668 }
669
670 void
671 linux_attach_lwp (unsigned long lwpid)
672 {
673 linux_attach_lwp_1 (lwpid, 0);
674 }
675
676 int
677 linux_attach (unsigned long pid)
678 {
679 struct lwp_info *lwp;
680
681 linux_attach_lwp_1 (pid, 1);
682
683 linux_add_process (pid, 1);
684
685 if (!non_stop)
686 {
687 /* Don't ignore the initial SIGSTOP if we just attached to this
688 process. It will be collected by wait shortly. */
689 lwp = (struct lwp_info *) find_inferior_id (&all_lwps,
690 ptid_build (pid, pid, 0));
691 lwp->last_resume_kind = resume_stop;
692 }
693
694 return 0;
695 }
696
697 struct counter
698 {
699 int pid;
700 int count;
701 };
702
703 static int
704 second_thread_of_pid_p (struct inferior_list_entry *entry, void *args)
705 {
706 struct counter *counter = args;
707
708 if (ptid_get_pid (entry->id) == counter->pid)
709 {
710 if (++counter->count > 1)
711 return 1;
712 }
713
714 return 0;
715 }
716
717 static int
718 last_thread_of_process_p (struct thread_info *thread)
719 {
720 ptid_t ptid = ((struct inferior_list_entry *)thread)->id;
721 int pid = ptid_get_pid (ptid);
722 struct counter counter = { pid , 0 };
723
724 return (find_inferior (&all_threads,
725 second_thread_of_pid_p, &counter) == NULL);
726 }
727
728 /* Kill the inferior lwp. */
729
730 static int
731 linux_kill_one_lwp (struct inferior_list_entry *entry, void *args)
732 {
733 struct thread_info *thread = (struct thread_info *) entry;
734 struct lwp_info *lwp = get_thread_lwp (thread);
735 int wstat;
736 int pid = * (int *) args;
737
738 if (ptid_get_pid (entry->id) != pid)
739 return 0;
740
741 /* We avoid killing the first thread here, because of a Linux kernel (at
742 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
743 the children get a chance to be reaped, it will remain a zombie
744 forever. */
745
746 if (lwpid_of (lwp) == pid)
747 {
748 if (debug_threads)
749 fprintf (stderr, "lkop: is last of process %s\n",
750 target_pid_to_str (entry->id));
751 return 0;
752 }
753
754 /* If we're killing a running inferior, make sure it is stopped
755 first, as PTRACE_KILL will not work otherwise. */
756 if (!lwp->stopped)
757 send_sigstop (&lwp->head);
758
759 do
760 {
761 ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0);
762
763 /* Make sure it died. The loop is most likely unnecessary. */
764 pid = linux_wait_for_event (lwp->head.id, &wstat, __WALL);
765 } while (pid > 0 && WIFSTOPPED (wstat));
766
767 return 0;
768 }
769
770 static int
771 linux_kill (int pid)
772 {
773 struct process_info *process;
774 struct lwp_info *lwp;
775 struct thread_info *thread;
776 int wstat;
777 int lwpid;
778
779 process = find_process_pid (pid);
780 if (process == NULL)
781 return -1;
782
783 find_inferior (&all_threads, linux_kill_one_lwp, &pid);
784
785 /* See the comment in linux_kill_one_lwp. We did not kill the first
786 thread in the list, so do so now. */
787 lwp = find_lwp_pid (pid_to_ptid (pid));
788 thread = get_lwp_thread (lwp);
789
790 if (debug_threads)
791 fprintf (stderr, "lk_1: killing lwp %ld, for pid: %d\n",
792 lwpid_of (lwp), pid);
793
794 /* If we're killing a running inferior, make sure it is stopped
795 first, as PTRACE_KILL will not work otherwise. */
796 if (!lwp->stopped)
797 send_sigstop (&lwp->head);
798
799 do
800 {
801 ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0);
802
803 /* Make sure it died. The loop is most likely unnecessary. */
804 lwpid = linux_wait_for_event (lwp->head.id, &wstat, __WALL);
805 } while (lwpid > 0 && WIFSTOPPED (wstat));
806
807 #ifdef USE_THREAD_DB
808 thread_db_free (process, 0);
809 #endif
810 delete_lwp (lwp);
811 linux_remove_process (process);
812 return 0;
813 }
814
815 static int
816 linux_detach_one_lwp (struct inferior_list_entry *entry, void *args)
817 {
818 struct thread_info *thread = (struct thread_info *) entry;
819 struct lwp_info *lwp = get_thread_lwp (thread);
820 int pid = * (int *) args;
821
822 if (ptid_get_pid (entry->id) != pid)
823 return 0;
824
825 /* If we're detaching from a running inferior, make sure it is
826 stopped first, as PTRACE_DETACH will not work otherwise. */
827 if (!lwp->stopped)
828 {
829 int lwpid = lwpid_of (lwp);
830
831 stopping_threads = 1;
832 send_sigstop (&lwp->head);
833
834 /* If this detects a new thread through a clone event, the new
835 thread is appended to the end of the lwp list, so we'll
836 eventually detach from it. */
837 wait_for_sigstop (&lwp->head);
838 stopping_threads = 0;
839
840 /* If LWP exits while we're trying to stop it, there's nothing
841 left to do. */
842 lwp = find_lwp_pid (pid_to_ptid (lwpid));
843 if (lwp == NULL)
844 return 0;
845 }
846
847 /* If this process is stopped but is expecting a SIGSTOP, then make
848 sure we take care of that now. This isn't absolutely guaranteed
849 to collect the SIGSTOP, but is fairly likely to. */
850 if (lwp->stop_expected)
851 {
852 int wstat;
853 /* Clear stop_expected, so that the SIGSTOP will be reported. */
854 lwp->stop_expected = 0;
855 if (lwp->stopped)
856 linux_resume_one_lwp (lwp, 0, 0, NULL);
857 linux_wait_for_event (lwp->head.id, &wstat, __WALL);
858 }
859
860 /* Flush any pending changes to the process's registers. */
861 regcache_invalidate_one ((struct inferior_list_entry *)
862 get_lwp_thread (lwp));
863
864 /* Finally, let it resume. */
865 ptrace (PTRACE_DETACH, lwpid_of (lwp), 0, 0);
866
867 delete_lwp (lwp);
868 return 0;
869 }
870
871 static int
872 any_thread_of (struct inferior_list_entry *entry, void *args)
873 {
874 int *pid_p = args;
875
876 if (ptid_get_pid (entry->id) == *pid_p)
877 return 1;
878
879 return 0;
880 }
881
882 static int
883 linux_detach (int pid)
884 {
885 struct process_info *process;
886
887 process = find_process_pid (pid);
888 if (process == NULL)
889 return -1;
890
891 #ifdef USE_THREAD_DB
892 thread_db_free (process, 1);
893 #endif
894
895 current_inferior =
896 (struct thread_info *) find_inferior (&all_threads, any_thread_of, &pid);
897
898 delete_all_breakpoints ();
899 find_inferior (&all_threads, linux_detach_one_lwp, &pid);
900 linux_remove_process (process);
901 return 0;
902 }
903
904 static void
905 linux_join (int pid)
906 {
907 int status, ret;
908 struct process_info *process;
909
910 process = find_process_pid (pid);
911 if (process == NULL)
912 return;
913
914 do {
915 ret = my_waitpid (pid, &status, 0);
916 if (WIFEXITED (status) || WIFSIGNALED (status))
917 break;
918 } while (ret != -1 || errno != ECHILD);
919 }
920
921 /* Return nonzero if the given thread is still alive. */
922 static int
923 linux_thread_alive (ptid_t ptid)
924 {
925 struct lwp_info *lwp = find_lwp_pid (ptid);
926
927 /* We assume we always know if a thread exits. If a whole process
928 exited but we still haven't been able to report it to GDB, we'll
929 hold on to the last lwp of the dead process. */
930 if (lwp != NULL)
931 return !lwp->dead;
932 else
933 return 0;
934 }
935
936 /* Return 1 if this lwp has an interesting status pending. */
937 static int
938 status_pending_p_callback (struct inferior_list_entry *entry, void *arg)
939 {
940 struct lwp_info *lwp = (struct lwp_info *) entry;
941 ptid_t ptid = * (ptid_t *) arg;
942 struct thread_info *thread = get_lwp_thread (lwp);
943
944 /* Check if we're only interested in events from a specific process
945 or its lwps. */
946 if (!ptid_equal (minus_one_ptid, ptid)
947 && ptid_get_pid (ptid) != ptid_get_pid (lwp->head.id))
948 return 0;
949
950 thread = get_lwp_thread (lwp);
951
952 /* If we got a `vCont;t', but we haven't reported a stop yet, do
953 report any status pending the LWP may have. */
954 if (lwp->last_resume_kind == resume_stop
955 && thread->last_status.kind == TARGET_WAITKIND_STOPPED)
956 return 0;
957
958 return lwp->status_pending_p;
959 }
960
961 static int
962 same_lwp (struct inferior_list_entry *entry, void *data)
963 {
964 ptid_t ptid = *(ptid_t *) data;
965 int lwp;
966
967 if (ptid_get_lwp (ptid) != 0)
968 lwp = ptid_get_lwp (ptid);
969 else
970 lwp = ptid_get_pid (ptid);
971
972 if (ptid_get_lwp (entry->id) == lwp)
973 return 1;
974
975 return 0;
976 }
977
978 struct lwp_info *
979 find_lwp_pid (ptid_t ptid)
980 {
981 return (struct lwp_info*) find_inferior (&all_lwps, same_lwp, &ptid);
982 }
983
984 static struct lwp_info *
985 linux_wait_for_lwp (ptid_t ptid, int *wstatp, int options)
986 {
987 int ret;
988 int to_wait_for = -1;
989 struct lwp_info *child = NULL;
990
991 if (debug_threads)
992 fprintf (stderr, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid));
993
994 if (ptid_equal (ptid, minus_one_ptid))
995 to_wait_for = -1; /* any child */
996 else
997 to_wait_for = ptid_get_lwp (ptid); /* this lwp only */
998
999 options |= __WALL;
1000
1001 retry:
1002
1003 ret = my_waitpid (to_wait_for, wstatp, options);
1004 if (ret == 0 || (ret == -1 && errno == ECHILD && (options & WNOHANG)))
1005 return NULL;
1006 else if (ret == -1)
1007 perror_with_name ("waitpid");
1008
1009 if (debug_threads
1010 && (!WIFSTOPPED (*wstatp)
1011 || (WSTOPSIG (*wstatp) != 32
1012 && WSTOPSIG (*wstatp) != 33)))
1013 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
1014
1015 child = find_lwp_pid (pid_to_ptid (ret));
1016
1017 /* If we didn't find a process, one of two things presumably happened:
1018 - A process we started and then detached from has exited. Ignore it.
1019 - A process we are controlling has forked and the new child's stop
1020 was reported to us by the kernel. Save its PID. */
1021 if (child == NULL && WIFSTOPPED (*wstatp))
1022 {
1023 add_pid_to_list (&stopped_pids, ret);
1024 goto retry;
1025 }
1026 else if (child == NULL)
1027 goto retry;
1028
1029 child->stopped = 1;
1030
1031 child->last_status = *wstatp;
1032
1033 /* Architecture-specific setup after inferior is running.
1034 This needs to happen after we have attached to the inferior
1035 and it is stopped for the first time, but before we access
1036 any inferior registers. */
1037 if (new_inferior)
1038 {
1039 the_low_target.arch_setup ();
1040 #ifdef HAVE_LINUX_REGSETS
1041 memset (disabled_regsets, 0, num_regsets);
1042 #endif
1043 new_inferior = 0;
1044 }
1045
1046 /* Fetch the possibly triggered data watchpoint info and store it in
1047 CHILD.
1048
1049 On some archs, like x86, that use debug registers to set
1050 watchpoints, it's possible that the way to know which watched
1051 address trapped, is to check the register that is used to select
1052 which address to watch. Problem is, between setting the
1053 watchpoint and reading back which data address trapped, the user
1054 may change the set of watchpoints, and, as a consequence, GDB
1055 changes the debug registers in the inferior. To avoid reading
1056 back a stale stopped-data-address when that happens, we cache in
1057 LP the fact that a watchpoint trapped, and the corresponding data
1058 address, as soon as we see CHILD stop with a SIGTRAP. If GDB
1059 changes the debug registers meanwhile, we have the cached data we
1060 can rely on. */
1061
1062 if (WIFSTOPPED (*wstatp) && WSTOPSIG (*wstatp) == SIGTRAP)
1063 {
1064 if (the_low_target.stopped_by_watchpoint == NULL)
1065 {
1066 child->stopped_by_watchpoint = 0;
1067 }
1068 else
1069 {
1070 struct thread_info *saved_inferior;
1071
1072 saved_inferior = current_inferior;
1073 current_inferior = get_lwp_thread (child);
1074
1075 child->stopped_by_watchpoint
1076 = the_low_target.stopped_by_watchpoint ();
1077
1078 if (child->stopped_by_watchpoint)
1079 {
1080 if (the_low_target.stopped_data_address != NULL)
1081 child->stopped_data_address
1082 = the_low_target.stopped_data_address ();
1083 else
1084 child->stopped_data_address = 0;
1085 }
1086
1087 current_inferior = saved_inferior;
1088 }
1089 }
1090
1091 /* Store the STOP_PC, with adjustment applied. This depends on the
1092 architecture being defined already (so that CHILD has a valid
1093 regcache), and on LAST_STATUS being set (to check for SIGTRAP or
1094 not). */
1095 if (WIFSTOPPED (*wstatp))
1096 child->stop_pc = get_stop_pc (child);
1097
1098 if (debug_threads
1099 && WIFSTOPPED (*wstatp)
1100 && the_low_target.get_pc != NULL)
1101 {
1102 struct thread_info *saved_inferior = current_inferior;
1103 struct regcache *regcache;
1104 CORE_ADDR pc;
1105
1106 current_inferior = get_lwp_thread (child);
1107 regcache = get_thread_regcache (current_inferior, 1);
1108 pc = (*the_low_target.get_pc) (regcache);
1109 fprintf (stderr, "linux_wait_for_lwp: pc is 0x%lx\n", (long) pc);
1110 current_inferior = saved_inferior;
1111 }
1112
1113 return child;
1114 }
1115
1116 /* Arrange for a breakpoint to be hit again later. We don't keep the
1117 SIGTRAP status and don't forward the SIGTRAP signal to the LWP. We
1118 will handle the current event, eventually we will resume this LWP,
1119 and this breakpoint will trap again. */
1120
1121 static int
1122 cancel_breakpoint (struct lwp_info *lwp)
1123 {
1124 struct thread_info *saved_inferior;
1125 struct regcache *regcache;
1126
1127 /* There's nothing to do if we don't support breakpoints. */
1128 if (!supports_breakpoints ())
1129 return 0;
1130
1131 if (lwp->stepping)
1132 {
1133 if (debug_threads)
1134 fprintf (stderr,
1135 "CB: [%s] is stepping\n",
1136 target_pid_to_str (lwp->head.id));
1137 return 0;
1138 }
1139
1140 regcache = get_thread_regcache (get_lwp_thread (lwp), 1);
1141
1142 /* breakpoint_at reads from current inferior. */
1143 saved_inferior = current_inferior;
1144 current_inferior = get_lwp_thread (lwp);
1145
1146 if ((*the_low_target.breakpoint_at) (lwp->stop_pc))
1147 {
1148 if (debug_threads)
1149 fprintf (stderr,
1150 "CB: Push back breakpoint for %s\n",
1151 target_pid_to_str (lwp->head.id));
1152
1153 /* Back up the PC if necessary. */
1154 if (the_low_target.decr_pc_after_break)
1155 {
1156 struct regcache *regcache
1157 = get_thread_regcache (get_lwp_thread (lwp), 1);
1158 (*the_low_target.set_pc) (regcache, lwp->stop_pc);
1159 }
1160
1161 current_inferior = saved_inferior;
1162 return 1;
1163 }
1164 else
1165 {
1166 if (debug_threads)
1167 fprintf (stderr,
1168 "CB: No breakpoint found at %s for [%s]\n",
1169 paddress (lwp->stop_pc),
1170 target_pid_to_str (lwp->head.id));
1171 }
1172
1173 current_inferior = saved_inferior;
1174 return 0;
1175 }
1176
1177 /* When the event-loop is doing a step-over, this points at the thread
1178 being stepped. */
1179 ptid_t step_over_bkpt;
1180
1181 /* Wait for an event from child PID. If PID is -1, wait for any
1182 child. Store the stop status through the status pointer WSTAT.
1183 OPTIONS is passed to the waitpid call. Return 0 if no child stop
1184 event was found and OPTIONS contains WNOHANG. Return the PID of
1185 the stopped child otherwise. */
1186
1187 static int
1188 linux_wait_for_event_1 (ptid_t ptid, int *wstat, int options)
1189 {
1190 struct lwp_info *event_child, *requested_child;
1191
1192 event_child = NULL;
1193 requested_child = NULL;
1194
1195 /* Check for a lwp with a pending status. */
1196
1197 if (ptid_equal (ptid, minus_one_ptid)
1198 || ptid_equal (pid_to_ptid (ptid_get_pid (ptid)), ptid))
1199 {
1200 event_child = (struct lwp_info *)
1201 find_inferior (&all_lwps, status_pending_p_callback, &ptid);
1202 if (debug_threads && event_child)
1203 fprintf (stderr, "Got a pending child %ld\n", lwpid_of (event_child));
1204 }
1205 else
1206 {
1207 requested_child = find_lwp_pid (ptid);
1208
1209 if (requested_child->status_pending_p)
1210 event_child = requested_child;
1211 }
1212
1213 if (event_child != NULL)
1214 {
1215 if (debug_threads)
1216 fprintf (stderr, "Got an event from pending child %ld (%04x)\n",
1217 lwpid_of (event_child), event_child->status_pending);
1218 *wstat = event_child->status_pending;
1219 event_child->status_pending_p = 0;
1220 event_child->status_pending = 0;
1221 current_inferior = get_lwp_thread (event_child);
1222 return lwpid_of (event_child);
1223 }
1224
1225 /* We only enter this loop if no process has a pending wait status. Thus
1226 any action taken in response to a wait status inside this loop is
1227 responding as soon as we detect the status, not after any pending
1228 events. */
1229 while (1)
1230 {
1231 event_child = linux_wait_for_lwp (ptid, wstat, options);
1232
1233 if ((options & WNOHANG) && event_child == NULL)
1234 {
1235 if (debug_threads)
1236 fprintf (stderr, "WNOHANG set, no event found\n");
1237 return 0;
1238 }
1239
1240 if (event_child == NULL)
1241 error ("event from unknown child");
1242
1243 current_inferior = get_lwp_thread (event_child);
1244
1245 /* Check for thread exit. */
1246 if (! WIFSTOPPED (*wstat))
1247 {
1248 if (debug_threads)
1249 fprintf (stderr, "LWP %ld exiting\n", lwpid_of (event_child));
1250
1251 /* If the last thread is exiting, just return. */
1252 if (last_thread_of_process_p (current_inferior))
1253 {
1254 if (debug_threads)
1255 fprintf (stderr, "LWP %ld is last lwp of process\n",
1256 lwpid_of (event_child));
1257 return lwpid_of (event_child);
1258 }
1259
1260 if (!non_stop)
1261 {
1262 current_inferior = (struct thread_info *) all_threads.head;
1263 if (debug_threads)
1264 fprintf (stderr, "Current inferior is now %ld\n",
1265 lwpid_of (get_thread_lwp (current_inferior)));
1266 }
1267 else
1268 {
1269 current_inferior = NULL;
1270 if (debug_threads)
1271 fprintf (stderr, "Current inferior is now <NULL>\n");
1272 }
1273
1274 /* If we were waiting for this particular child to do something...
1275 well, it did something. */
1276 if (requested_child != NULL)
1277 {
1278 int lwpid = lwpid_of (event_child);
1279
1280 /* Cancel the step-over operation --- the thread that
1281 started it is gone. */
1282 if (finish_step_over (event_child))
1283 unstop_all_lwps (event_child);
1284 delete_lwp (event_child);
1285 return lwpid;
1286 }
1287
1288 delete_lwp (event_child);
1289
1290 /* Wait for a more interesting event. */
1291 continue;
1292 }
1293
1294 if (event_child->must_set_ptrace_flags)
1295 {
1296 ptrace (PTRACE_SETOPTIONS, lwpid_of (event_child),
1297 0, (PTRACE_ARG4_TYPE) PTRACE_O_TRACECLONE);
1298 event_child->must_set_ptrace_flags = 0;
1299 }
1300
1301 if (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) == SIGTRAP
1302 && *wstat >> 16 != 0)
1303 {
1304 handle_extended_wait (event_child, *wstat);
1305 continue;
1306 }
1307
1308 /* If GDB is not interested in this signal, don't stop other
1309 threads, and don't report it to GDB. Just resume the
1310 inferior right away. We do this for threading-related
1311 signals as well as any that GDB specifically requested we
1312 ignore. But never ignore SIGSTOP if we sent it ourselves,
1313 and do not ignore signals when stepping - they may require
1314 special handling to skip the signal handler. */
1315 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
1316 thread library? */
1317 if (WIFSTOPPED (*wstat)
1318 && !event_child->stepping
1319 && (
1320 #if defined (USE_THREAD_DB) && defined (__SIGRTMIN)
1321 (current_process ()->private->thread_db != NULL
1322 && (WSTOPSIG (*wstat) == __SIGRTMIN
1323 || WSTOPSIG (*wstat) == __SIGRTMIN + 1))
1324 ||
1325 #endif
1326 (pass_signals[target_signal_from_host (WSTOPSIG (*wstat))]
1327 && !(WSTOPSIG (*wstat) == SIGSTOP
1328 && event_child->stop_expected))))
1329 {
1330 siginfo_t info, *info_p;
1331
1332 if (debug_threads)
1333 fprintf (stderr, "Ignored signal %d for LWP %ld.\n",
1334 WSTOPSIG (*wstat), lwpid_of (event_child));
1335
1336 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (event_child), 0, &info) == 0)
1337 info_p = &info;
1338 else
1339 info_p = NULL;
1340 linux_resume_one_lwp (event_child, event_child->stepping,
1341 WSTOPSIG (*wstat), info_p);
1342 continue;
1343 }
1344
1345 if (WIFSTOPPED (*wstat)
1346 && WSTOPSIG (*wstat) == SIGSTOP
1347 && event_child->stop_expected)
1348 {
1349 int should_stop;
1350
1351 if (debug_threads)
1352 fprintf (stderr, "Expected stop.\n");
1353 event_child->stop_expected = 0;
1354
1355 should_stop = (event_child->last_resume_kind == resume_stop
1356 || stopping_threads);
1357
1358 if (!should_stop)
1359 {
1360 linux_resume_one_lwp (event_child,
1361 event_child->stepping, 0, NULL);
1362 continue;
1363 }
1364 }
1365
1366 return lwpid_of (event_child);
1367 }
1368
1369 /* NOTREACHED */
1370 return 0;
1371 }
1372
1373 static int
1374 linux_wait_for_event (ptid_t ptid, int *wstat, int options)
1375 {
1376 ptid_t wait_ptid;
1377
1378 if (ptid_is_pid (ptid))
1379 {
1380 /* A request to wait for a specific tgid. This is not possible
1381 with waitpid, so instead, we wait for any child, and leave
1382 children we're not interested in right now with a pending
1383 status to report later. */
1384 wait_ptid = minus_one_ptid;
1385 }
1386 else
1387 wait_ptid = ptid;
1388
1389 while (1)
1390 {
1391 int event_pid;
1392
1393 event_pid = linux_wait_for_event_1 (wait_ptid, wstat, options);
1394
1395 if (event_pid > 0
1396 && ptid_is_pid (ptid) && ptid_get_pid (ptid) != event_pid)
1397 {
1398 struct lwp_info *event_child = find_lwp_pid (pid_to_ptid (event_pid));
1399
1400 if (! WIFSTOPPED (*wstat))
1401 mark_lwp_dead (event_child, *wstat);
1402 else
1403 {
1404 event_child->status_pending_p = 1;
1405 event_child->status_pending = *wstat;
1406 }
1407 }
1408 else
1409 return event_pid;
1410 }
1411 }
1412
1413
1414 /* Count the LWP's that have had events. */
1415
1416 static int
1417 count_events_callback (struct inferior_list_entry *entry, void *data)
1418 {
1419 struct lwp_info *lp = (struct lwp_info *) entry;
1420 int *count = data;
1421
1422 gdb_assert (count != NULL);
1423
1424 /* Count only resumed LWPs that have a SIGTRAP event pending that
1425 should be reported to GDB. */
1426 if (get_lwp_thread (lp)->last_status.kind == TARGET_WAITKIND_IGNORE
1427 && lp->last_resume_kind != resume_stop
1428 && lp->status_pending_p
1429 && WIFSTOPPED (lp->status_pending)
1430 && WSTOPSIG (lp->status_pending) == SIGTRAP
1431 && !breakpoint_inserted_here (lp->stop_pc))
1432 (*count)++;
1433
1434 return 0;
1435 }
1436
1437 /* Select the LWP (if any) that is currently being single-stepped. */
1438
1439 static int
1440 select_singlestep_lwp_callback (struct inferior_list_entry *entry, void *data)
1441 {
1442 struct lwp_info *lp = (struct lwp_info *) entry;
1443
1444 if (get_lwp_thread (lp)->last_status.kind == TARGET_WAITKIND_IGNORE
1445 && lp->last_resume_kind == resume_step
1446 && lp->status_pending_p)
1447 return 1;
1448 else
1449 return 0;
1450 }
1451
1452 /* Select the Nth LWP that has had a SIGTRAP event that should be
1453 reported to GDB. */
1454
1455 static int
1456 select_event_lwp_callback (struct inferior_list_entry *entry, void *data)
1457 {
1458 struct lwp_info *lp = (struct lwp_info *) entry;
1459 int *selector = data;
1460
1461 gdb_assert (selector != NULL);
1462
1463 /* Select only resumed LWPs that have a SIGTRAP event pending. */
1464 if (lp->last_resume_kind != resume_stop
1465 && get_lwp_thread (lp)->last_status.kind == TARGET_WAITKIND_IGNORE
1466 && lp->status_pending_p
1467 && WIFSTOPPED (lp->status_pending)
1468 && WSTOPSIG (lp->status_pending) == SIGTRAP
1469 && !breakpoint_inserted_here (lp->stop_pc))
1470 if ((*selector)-- == 0)
1471 return 1;
1472
1473 return 0;
1474 }
1475
1476 static int
1477 cancel_breakpoints_callback (struct inferior_list_entry *entry, void *data)
1478 {
1479 struct lwp_info *lp = (struct lwp_info *) entry;
1480 struct lwp_info *event_lp = data;
1481
1482 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1483 if (lp == event_lp)
1484 return 0;
1485
1486 /* If a LWP other than the LWP that we're reporting an event for has
1487 hit a GDB breakpoint (as opposed to some random trap signal),
1488 then just arrange for it to hit it again later. We don't keep
1489 the SIGTRAP status and don't forward the SIGTRAP signal to the
1490 LWP. We will handle the current event, eventually we will resume
1491 all LWPs, and this one will get its breakpoint trap again.
1492
1493 If we do not do this, then we run the risk that the user will
1494 delete or disable the breakpoint, but the LWP will have already
1495 tripped on it. */
1496
1497 if (lp->last_resume_kind != resume_stop
1498 && get_lwp_thread (lp)->last_status.kind == TARGET_WAITKIND_IGNORE
1499 && lp->status_pending_p
1500 && WIFSTOPPED (lp->status_pending)
1501 && WSTOPSIG (lp->status_pending) == SIGTRAP
1502 && cancel_breakpoint (lp))
1503 /* Throw away the SIGTRAP. */
1504 lp->status_pending_p = 0;
1505
1506 return 0;
1507 }
1508
1509 /* Select one LWP out of those that have events pending. */
1510
1511 static void
1512 select_event_lwp (struct lwp_info **orig_lp)
1513 {
1514 int num_events = 0;
1515 int random_selector;
1516 struct lwp_info *event_lp;
1517
1518 /* Give preference to any LWP that is being single-stepped. */
1519 event_lp
1520 = (struct lwp_info *) find_inferior (&all_lwps,
1521 select_singlestep_lwp_callback, NULL);
1522 if (event_lp != NULL)
1523 {
1524 if (debug_threads)
1525 fprintf (stderr,
1526 "SEL: Select single-step %s\n",
1527 target_pid_to_str (ptid_of (event_lp)));
1528 }
1529 else
1530 {
1531 /* No single-stepping LWP. Select one at random, out of those
1532 which have had SIGTRAP events. */
1533
1534 /* First see how many SIGTRAP events we have. */
1535 find_inferior (&all_lwps, count_events_callback, &num_events);
1536
1537 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1538 random_selector = (int)
1539 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
1540
1541 if (debug_threads && num_events > 1)
1542 fprintf (stderr,
1543 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1544 num_events, random_selector);
1545
1546 event_lp = (struct lwp_info *) find_inferior (&all_lwps,
1547 select_event_lwp_callback,
1548 &random_selector);
1549 }
1550
1551 if (event_lp != NULL)
1552 {
1553 /* Switch the event LWP. */
1554 *orig_lp = event_lp;
1555 }
1556 }
1557
1558 /* Set this inferior LWP's state as "want-stopped". We won't resume
1559 this LWP until the client gives us another action for it. */
1560
1561 static void
1562 gdb_wants_lwp_stopped (struct inferior_list_entry *entry)
1563 {
1564 struct lwp_info *lwp = (struct lwp_info *) entry;
1565 struct thread_info *thread = get_lwp_thread (lwp);
1566
1567 /* Most threads are stopped implicitly (all-stop); tag that with
1568 signal 0. The thread being explicitly reported stopped to the
1569 client, gets it's status fixed up afterwards. */
1570 thread->last_status.kind = TARGET_WAITKIND_STOPPED;
1571 thread->last_status.value.sig = TARGET_SIGNAL_0;
1572
1573 lwp->last_resume_kind = resume_stop;
1574 }
1575
1576 /* Set all LWP's states as "want-stopped". */
1577
1578 static void
1579 gdb_wants_all_stopped (void)
1580 {
1581 for_each_inferior (&all_lwps, gdb_wants_lwp_stopped);
1582 }
1583
1584 /* Wait for process, returns status. */
1585
1586 static ptid_t
1587 linux_wait_1 (ptid_t ptid,
1588 struct target_waitstatus *ourstatus, int target_options)
1589 {
1590 int w;
1591 struct thread_info *thread = NULL;
1592 struct lwp_info *event_child = NULL;
1593 int options;
1594 int pid;
1595 int step_over_finished;
1596 int bp_explains_trap;
1597 int maybe_internal_trap;
1598 int report_to_gdb;
1599
1600 /* Translate generic target options into linux options. */
1601 options = __WALL;
1602 if (target_options & TARGET_WNOHANG)
1603 options |= WNOHANG;
1604
1605 retry:
1606 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1607
1608 /* If we were only supposed to resume one thread, only wait for
1609 that thread - if it's still alive. If it died, however - which
1610 can happen if we're coming from the thread death case below -
1611 then we need to make sure we restart the other threads. We could
1612 pick a thread at random or restart all; restarting all is less
1613 arbitrary. */
1614 if (!non_stop
1615 && !ptid_equal (cont_thread, null_ptid)
1616 && !ptid_equal (cont_thread, minus_one_ptid))
1617 {
1618 thread = (struct thread_info *) find_inferior_id (&all_threads,
1619 cont_thread);
1620
1621 /* No stepping, no signal - unless one is pending already, of course. */
1622 if (thread == NULL)
1623 {
1624 struct thread_resume resume_info;
1625 resume_info.thread = minus_one_ptid;
1626 resume_info.kind = resume_continue;
1627 resume_info.sig = 0;
1628 linux_resume (&resume_info, 1);
1629 }
1630 else
1631 ptid = cont_thread;
1632 }
1633
1634 if (ptid_equal (step_over_bkpt, null_ptid))
1635 pid = linux_wait_for_event (ptid, &w, options);
1636 else
1637 {
1638 if (debug_threads)
1639 fprintf (stderr, "step_over_bkpt set [%s], doing a blocking wait\n",
1640 target_pid_to_str (step_over_bkpt));
1641 pid = linux_wait_for_event (step_over_bkpt, &w, options & ~WNOHANG);
1642 }
1643
1644 if (pid == 0) /* only if TARGET_WNOHANG */
1645 return null_ptid;
1646
1647 event_child = get_thread_lwp (current_inferior);
1648
1649 /* If we are waiting for a particular child, and it exited,
1650 linux_wait_for_event will return its exit status. Similarly if
1651 the last child exited. If this is not the last child, however,
1652 do not report it as exited until there is a 'thread exited' response
1653 available in the remote protocol. Instead, just wait for another event.
1654 This should be safe, because if the thread crashed we will already
1655 have reported the termination signal to GDB; that should stop any
1656 in-progress stepping operations, etc.
1657
1658 Report the exit status of the last thread to exit. This matches
1659 LinuxThreads' behavior. */
1660
1661 if (last_thread_of_process_p (current_inferior))
1662 {
1663 if (WIFEXITED (w) || WIFSIGNALED (w))
1664 {
1665 int pid = pid_of (event_child);
1666 struct process_info *process = find_process_pid (pid);
1667
1668 #ifdef USE_THREAD_DB
1669 thread_db_free (process, 0);
1670 #endif
1671 delete_lwp (event_child);
1672 linux_remove_process (process);
1673
1674 current_inferior = NULL;
1675
1676 if (WIFEXITED (w))
1677 {
1678 ourstatus->kind = TARGET_WAITKIND_EXITED;
1679 ourstatus->value.integer = WEXITSTATUS (w);
1680
1681 if (debug_threads)
1682 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
1683 }
1684 else
1685 {
1686 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1687 ourstatus->value.sig = target_signal_from_host (WTERMSIG (w));
1688
1689 if (debug_threads)
1690 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
1691
1692 }
1693
1694 return pid_to_ptid (pid);
1695 }
1696 }
1697 else
1698 {
1699 if (!WIFSTOPPED (w))
1700 goto retry;
1701 }
1702
1703 /* If this event was not handled before, and is not a SIGTRAP, we
1704 report it. SIGILL and SIGSEGV are also treated as traps in case
1705 a breakpoint is inserted at the current PC. If this target does
1706 not support internal breakpoints at all, we also report the
1707 SIGTRAP without further processing; it's of no concern to us. */
1708 maybe_internal_trap
1709 = (supports_breakpoints ()
1710 && (WSTOPSIG (w) == SIGTRAP
1711 || ((WSTOPSIG (w) == SIGILL
1712 || WSTOPSIG (w) == SIGSEGV)
1713 && (*the_low_target.breakpoint_at) (event_child->stop_pc))));
1714
1715 if (maybe_internal_trap)
1716 {
1717 /* Handle anything that requires bookkeeping before deciding to
1718 report the event or continue waiting. */
1719
1720 /* First check if we can explain the SIGTRAP with an internal
1721 breakpoint, or if we should possibly report the event to GDB.
1722 Do this before anything that may remove or insert a
1723 breakpoint. */
1724 bp_explains_trap = breakpoint_inserted_here (event_child->stop_pc);
1725
1726 /* We have a SIGTRAP, possibly a step-over dance has just
1727 finished. If so, tweak the state machine accordingly,
1728 reinsert breakpoints and delete any reinsert (software
1729 single-step) breakpoints. */
1730 step_over_finished = finish_step_over (event_child);
1731
1732 /* Now invoke the callbacks of any internal breakpoints there. */
1733 check_breakpoints (event_child->stop_pc);
1734
1735 if (bp_explains_trap)
1736 {
1737 /* If we stepped or ran into an internal breakpoint, we've
1738 already handled it. So next time we resume (from this
1739 PC), we should step over it. */
1740 if (debug_threads)
1741 fprintf (stderr, "Hit a gdbserver breakpoint.\n");
1742
1743 if (breakpoint_here (event_child->stop_pc))
1744 event_child->need_step_over = 1;
1745 }
1746 }
1747 else
1748 {
1749 /* We have some other signal, possibly a step-over dance was in
1750 progress, and it should be cancelled too. */
1751 step_over_finished = finish_step_over (event_child);
1752 }
1753
1754 /* We have all the data we need. Either report the event to GDB, or
1755 resume threads and keep waiting for more. */
1756
1757 /* Check If GDB would be interested in this event. If GDB wanted
1758 this thread to single step, we always want to report the SIGTRAP,
1759 and let GDB handle it. Watchpoints should always be reported.
1760 So should signals we can't explain. A SIGTRAP we can't explain
1761 could be a GDB breakpoint --- we may or not support Z0
1762 breakpoints. If we do, we're be able to handle GDB breakpoints
1763 on top of internal breakpoints, by handling the internal
1764 breakpoint and still reporting the event to GDB. If we don't,
1765 we're out of luck, GDB won't see the breakpoint hit. */
1766 report_to_gdb = (!maybe_internal_trap
1767 || event_child->last_resume_kind == resume_step
1768 || event_child->stopped_by_watchpoint
1769 || (!step_over_finished && !bp_explains_trap)
1770 || gdb_breakpoint_here (event_child->stop_pc));
1771
1772 /* We found no reason GDB would want us to stop. We either hit one
1773 of our own breakpoints, or finished an internal step GDB
1774 shouldn't know about. */
1775 if (!report_to_gdb)
1776 {
1777 if (debug_threads)
1778 {
1779 if (bp_explains_trap)
1780 fprintf (stderr, "Hit a gdbserver breakpoint.\n");
1781 if (step_over_finished)
1782 fprintf (stderr, "Step-over finished.\n");
1783 }
1784
1785 /* We're not reporting this breakpoint to GDB, so apply the
1786 decr_pc_after_break adjustment to the inferior's regcache
1787 ourselves. */
1788
1789 if (the_low_target.set_pc != NULL)
1790 {
1791 struct regcache *regcache
1792 = get_thread_regcache (get_lwp_thread (event_child), 1);
1793 (*the_low_target.set_pc) (regcache, event_child->stop_pc);
1794 }
1795
1796 /* We've finished stepping over a breakpoint. We've stopped all
1797 LWPs momentarily except the stepping one. This is where we
1798 resume them all again. We're going to keep waiting, so use
1799 proceed, which handles stepping over the next breakpoint. */
1800 if (debug_threads)
1801 fprintf (stderr, "proceeding all threads.\n");
1802 proceed_all_lwps ();
1803 goto retry;
1804 }
1805
1806 if (debug_threads)
1807 {
1808 if (event_child->last_resume_kind == resume_step)
1809 fprintf (stderr, "GDB wanted to single-step, reporting event.\n");
1810 if (event_child->stopped_by_watchpoint)
1811 fprintf (stderr, "Stopped by watchpoint.\n");
1812 if (gdb_breakpoint_here (event_child->stop_pc))
1813 fprintf (stderr, "Stopped by GDB breakpoint.\n");
1814 if (debug_threads)
1815 fprintf (stderr, "Hit a non-gdbserver trap event.\n");
1816 }
1817
1818 /* Alright, we're going to report a stop. */
1819
1820 if (!non_stop)
1821 {
1822 /* In all-stop, stop all threads. */
1823 stop_all_lwps ();
1824
1825 /* If we're not waiting for a specific LWP, choose an event LWP
1826 from among those that have had events. Giving equal priority
1827 to all LWPs that have had events helps prevent
1828 starvation. */
1829 if (ptid_equal (ptid, minus_one_ptid))
1830 {
1831 event_child->status_pending_p = 1;
1832 event_child->status_pending = w;
1833
1834 select_event_lwp (&event_child);
1835
1836 event_child->status_pending_p = 0;
1837 w = event_child->status_pending;
1838 }
1839
1840 /* Now that we've selected our final event LWP, cancel any
1841 breakpoints in other LWPs that have hit a GDB breakpoint.
1842 See the comment in cancel_breakpoints_callback to find out
1843 why. */
1844 find_inferior (&all_lwps, cancel_breakpoints_callback, event_child);
1845 }
1846 else
1847 {
1848 /* If we just finished a step-over, then all threads had been
1849 momentarily paused. In all-stop, that's fine, we want
1850 threads stopped by now anyway. In non-stop, we need to
1851 re-resume threads that GDB wanted to be running. */
1852 if (step_over_finished)
1853 unstop_all_lwps (event_child);
1854 }
1855
1856 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1857
1858 /* Do this before the gdb_wants_all_stopped calls below, since they
1859 always set last_resume_kind to resume_stop. */
1860 if (event_child->last_resume_kind == resume_stop && WSTOPSIG (w) == SIGSTOP)
1861 {
1862 /* A thread that has been requested to stop by GDB with vCont;t,
1863 and it stopped cleanly, so report as SIG0. The use of
1864 SIGSTOP is an implementation detail. */
1865 ourstatus->value.sig = TARGET_SIGNAL_0;
1866 }
1867 else if (event_child->last_resume_kind == resume_stop && WSTOPSIG (w) != SIGSTOP)
1868 {
1869 /* A thread that has been requested to stop by GDB with vCont;t,
1870 but, it stopped for other reasons. */
1871 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w));
1872 }
1873 else
1874 {
1875 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w));
1876 }
1877
1878 gdb_assert (ptid_equal (step_over_bkpt, null_ptid));
1879
1880 if (!non_stop)
1881 {
1882 /* From GDB's perspective, all-stop mode always stops all
1883 threads implicitly. Tag all threads as "want-stopped". */
1884 gdb_wants_all_stopped ();
1885 }
1886 else
1887 {
1888 /* We're reporting this LWP as stopped. Update it's
1889 "want-stopped" state to what the client wants, until it gets
1890 a new resume action. */
1891 gdb_wants_lwp_stopped (&event_child->head);
1892 }
1893
1894 if (debug_threads)
1895 fprintf (stderr, "linux_wait ret = %s, %d, %d\n",
1896 target_pid_to_str (ptid_of (event_child)),
1897 ourstatus->kind,
1898 ourstatus->value.sig);
1899
1900 get_lwp_thread (event_child)->last_status = *ourstatus;
1901 return ptid_of (event_child);
1902 }
1903
1904 /* Get rid of any pending event in the pipe. */
1905 static void
1906 async_file_flush (void)
1907 {
1908 int ret;
1909 char buf;
1910
1911 do
1912 ret = read (linux_event_pipe[0], &buf, 1);
1913 while (ret >= 0 || (ret == -1 && errno == EINTR));
1914 }
1915
1916 /* Put something in the pipe, so the event loop wakes up. */
1917 static void
1918 async_file_mark (void)
1919 {
1920 int ret;
1921
1922 async_file_flush ();
1923
1924 do
1925 ret = write (linux_event_pipe[1], "+", 1);
1926 while (ret == 0 || (ret == -1 && errno == EINTR));
1927
1928 /* Ignore EAGAIN. If the pipe is full, the event loop will already
1929 be awakened anyway. */
1930 }
1931
1932 static ptid_t
1933 linux_wait (ptid_t ptid,
1934 struct target_waitstatus *ourstatus, int target_options)
1935 {
1936 ptid_t event_ptid;
1937
1938 if (debug_threads)
1939 fprintf (stderr, "linux_wait: [%s]\n", target_pid_to_str (ptid));
1940
1941 /* Flush the async file first. */
1942 if (target_is_async_p ())
1943 async_file_flush ();
1944
1945 event_ptid = linux_wait_1 (ptid, ourstatus, target_options);
1946
1947 /* If at least one stop was reported, there may be more. A single
1948 SIGCHLD can signal more than one child stop. */
1949 if (target_is_async_p ()
1950 && (target_options & TARGET_WNOHANG) != 0
1951 && !ptid_equal (event_ptid, null_ptid))
1952 async_file_mark ();
1953
1954 return event_ptid;
1955 }
1956
1957 /* Send a signal to an LWP. */
1958
1959 static int
1960 kill_lwp (unsigned long lwpid, int signo)
1961 {
1962 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1963 fails, then we are not using nptl threads and we should be using kill. */
1964
1965 #ifdef __NR_tkill
1966 {
1967 static int tkill_failed;
1968
1969 if (!tkill_failed)
1970 {
1971 int ret;
1972
1973 errno = 0;
1974 ret = syscall (__NR_tkill, lwpid, signo);
1975 if (errno != ENOSYS)
1976 return ret;
1977 tkill_failed = 1;
1978 }
1979 }
1980 #endif
1981
1982 return kill (lwpid, signo);
1983 }
1984
1985 static void
1986 send_sigstop (struct inferior_list_entry *entry)
1987 {
1988 struct lwp_info *lwp = (struct lwp_info *) entry;
1989 int pid;
1990
1991 if (lwp->stopped)
1992 return;
1993
1994 pid = lwpid_of (lwp);
1995
1996 /* If we already have a pending stop signal for this process, don't
1997 send another. */
1998 if (lwp->stop_expected)
1999 {
2000 if (debug_threads)
2001 fprintf (stderr, "Have pending sigstop for lwp %d\n", pid);
2002
2003 return;
2004 }
2005
2006 if (debug_threads)
2007 fprintf (stderr, "Sending sigstop to lwp %d\n", pid);
2008
2009 lwp->stop_expected = 1;
2010 kill_lwp (pid, SIGSTOP);
2011 }
2012
2013 static void
2014 mark_lwp_dead (struct lwp_info *lwp, int wstat)
2015 {
2016 /* It's dead, really. */
2017 lwp->dead = 1;
2018
2019 /* Store the exit status for later. */
2020 lwp->status_pending_p = 1;
2021 lwp->status_pending = wstat;
2022
2023 /* Prevent trying to stop it. */
2024 lwp->stopped = 1;
2025
2026 /* No further stops are expected from a dead lwp. */
2027 lwp->stop_expected = 0;
2028 }
2029
2030 static void
2031 wait_for_sigstop (struct inferior_list_entry *entry)
2032 {
2033 struct lwp_info *lwp = (struct lwp_info *) entry;
2034 struct thread_info *saved_inferior;
2035 int wstat;
2036 ptid_t saved_tid;
2037 ptid_t ptid;
2038 int pid;
2039
2040 if (lwp->stopped)
2041 {
2042 if (debug_threads)
2043 fprintf (stderr, "wait_for_sigstop: LWP %ld already stopped\n",
2044 lwpid_of (lwp));
2045 return;
2046 }
2047
2048 saved_inferior = current_inferior;
2049 if (saved_inferior != NULL)
2050 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
2051 else
2052 saved_tid = null_ptid; /* avoid bogus unused warning */
2053
2054 ptid = lwp->head.id;
2055
2056 if (debug_threads)
2057 fprintf (stderr, "wait_for_sigstop: pulling one event\n");
2058
2059 pid = linux_wait_for_event (ptid, &wstat, __WALL);
2060
2061 /* If we stopped with a non-SIGSTOP signal, save it for later
2062 and record the pending SIGSTOP. If the process exited, just
2063 return. */
2064 if (WIFSTOPPED (wstat))
2065 {
2066 if (debug_threads)
2067 fprintf (stderr, "LWP %ld stopped with signal %d\n",
2068 lwpid_of (lwp), WSTOPSIG (wstat));
2069
2070 if (WSTOPSIG (wstat) != SIGSTOP)
2071 {
2072 if (debug_threads)
2073 fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n",
2074 lwpid_of (lwp), wstat);
2075
2076 lwp->status_pending_p = 1;
2077 lwp->status_pending = wstat;
2078 }
2079 }
2080 else
2081 {
2082 if (debug_threads)
2083 fprintf (stderr, "Process %d exited while stopping LWPs\n", pid);
2084
2085 lwp = find_lwp_pid (pid_to_ptid (pid));
2086 if (lwp)
2087 {
2088 /* Leave this status pending for the next time we're able to
2089 report it. In the mean time, we'll report this lwp as
2090 dead to GDB, so GDB doesn't try to read registers and
2091 memory from it. This can only happen if this was the
2092 last thread of the process; otherwise, PID is removed
2093 from the thread tables before linux_wait_for_event
2094 returns. */
2095 mark_lwp_dead (lwp, wstat);
2096 }
2097 }
2098
2099 if (saved_inferior == NULL || linux_thread_alive (saved_tid))
2100 current_inferior = saved_inferior;
2101 else
2102 {
2103 if (debug_threads)
2104 fprintf (stderr, "Previously current thread died.\n");
2105
2106 if (non_stop)
2107 {
2108 /* We can't change the current inferior behind GDB's back,
2109 otherwise, a subsequent command may apply to the wrong
2110 process. */
2111 current_inferior = NULL;
2112 }
2113 else
2114 {
2115 /* Set a valid thread as current. */
2116 set_desired_inferior (0);
2117 }
2118 }
2119 }
2120
2121 static void
2122 stop_all_lwps (void)
2123 {
2124 stopping_threads = 1;
2125 for_each_inferior (&all_lwps, send_sigstop);
2126 for_each_inferior (&all_lwps, wait_for_sigstop);
2127 stopping_threads = 0;
2128 }
2129
2130 /* Resume execution of the inferior process.
2131 If STEP is nonzero, single-step it.
2132 If SIGNAL is nonzero, give it that signal. */
2133
2134 static void
2135 linux_resume_one_lwp (struct lwp_info *lwp,
2136 int step, int signal, siginfo_t *info)
2137 {
2138 struct thread_info *saved_inferior;
2139
2140 if (lwp->stopped == 0)
2141 return;
2142
2143 /* If we have pending signals or status, and a new signal, enqueue the
2144 signal. Also enqueue the signal if we are waiting to reinsert a
2145 breakpoint; it will be picked up again below. */
2146 if (signal != 0
2147 && (lwp->status_pending_p || lwp->pending_signals != NULL
2148 || lwp->bp_reinsert != 0))
2149 {
2150 struct pending_signals *p_sig;
2151 p_sig = xmalloc (sizeof (*p_sig));
2152 p_sig->prev = lwp->pending_signals;
2153 p_sig->signal = signal;
2154 if (info == NULL)
2155 memset (&p_sig->info, 0, sizeof (siginfo_t));
2156 else
2157 memcpy (&p_sig->info, info, sizeof (siginfo_t));
2158 lwp->pending_signals = p_sig;
2159 }
2160
2161 if (lwp->status_pending_p)
2162 {
2163 if (debug_threads)
2164 fprintf (stderr, "Not resuming lwp %ld (%s, signal %d, stop %s);"
2165 " has pending status\n",
2166 lwpid_of (lwp), step ? "step" : "continue", signal,
2167 lwp->stop_expected ? "expected" : "not expected");
2168 return;
2169 }
2170
2171 saved_inferior = current_inferior;
2172 current_inferior = get_lwp_thread (lwp);
2173
2174 if (debug_threads)
2175 fprintf (stderr, "Resuming lwp %ld (%s, signal %d, stop %s)\n",
2176 lwpid_of (lwp), step ? "step" : "continue", signal,
2177 lwp->stop_expected ? "expected" : "not expected");
2178
2179 /* This bit needs some thinking about. If we get a signal that
2180 we must report while a single-step reinsert is still pending,
2181 we often end up resuming the thread. It might be better to
2182 (ew) allow a stack of pending events; then we could be sure that
2183 the reinsert happened right away and not lose any signals.
2184
2185 Making this stack would also shrink the window in which breakpoints are
2186 uninserted (see comment in linux_wait_for_lwp) but not enough for
2187 complete correctness, so it won't solve that problem. It may be
2188 worthwhile just to solve this one, however. */
2189 if (lwp->bp_reinsert != 0)
2190 {
2191 if (debug_threads)
2192 fprintf (stderr, " pending reinsert at 0x%s\n",
2193 paddress (lwp->bp_reinsert));
2194
2195 if (lwp->bp_reinsert != 0 && can_hardware_single_step ())
2196 {
2197 if (step == 0)
2198 fprintf (stderr, "BAD - reinserting but not stepping.\n");
2199
2200 step = 1;
2201 }
2202
2203 /* Postpone any pending signal. It was enqueued above. */
2204 signal = 0;
2205 }
2206
2207 if (debug_threads && the_low_target.get_pc != NULL)
2208 {
2209 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
2210 CORE_ADDR pc = (*the_low_target.get_pc) (regcache);
2211 fprintf (stderr, " resuming from pc 0x%lx\n", (long) pc);
2212 }
2213
2214 /* If we have pending signals, consume one unless we are trying to reinsert
2215 a breakpoint. */
2216 if (lwp->pending_signals != NULL && lwp->bp_reinsert == 0)
2217 {
2218 struct pending_signals **p_sig;
2219
2220 p_sig = &lwp->pending_signals;
2221 while ((*p_sig)->prev != NULL)
2222 p_sig = &(*p_sig)->prev;
2223
2224 signal = (*p_sig)->signal;
2225 if ((*p_sig)->info.si_signo != 0)
2226 ptrace (PTRACE_SETSIGINFO, lwpid_of (lwp), 0, &(*p_sig)->info);
2227
2228 free (*p_sig);
2229 *p_sig = NULL;
2230 }
2231
2232 if (the_low_target.prepare_to_resume != NULL)
2233 the_low_target.prepare_to_resume (lwp);
2234
2235 regcache_invalidate_one ((struct inferior_list_entry *)
2236 get_lwp_thread (lwp));
2237 errno = 0;
2238 lwp->stopped = 0;
2239 lwp->stopped_by_watchpoint = 0;
2240 lwp->stepping = step;
2241 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, lwpid_of (lwp), 0,
2242 /* Coerce to a uintptr_t first to avoid potential gcc warning
2243 of coercing an 8 byte integer to a 4 byte pointer. */
2244 (PTRACE_ARG4_TYPE) (uintptr_t) signal);
2245
2246 current_inferior = saved_inferior;
2247 if (errno)
2248 {
2249 /* ESRCH from ptrace either means that the thread was already
2250 running (an error) or that it is gone (a race condition). If
2251 it's gone, we will get a notification the next time we wait,
2252 so we can ignore the error. We could differentiate these
2253 two, but it's tricky without waiting; the thread still exists
2254 as a zombie, so sending it signal 0 would succeed. So just
2255 ignore ESRCH. */
2256 if (errno == ESRCH)
2257 return;
2258
2259 perror_with_name ("ptrace");
2260 }
2261 }
2262
2263 struct thread_resume_array
2264 {
2265 struct thread_resume *resume;
2266 size_t n;
2267 };
2268
2269 /* This function is called once per thread. We look up the thread
2270 in RESUME_PTR, and mark the thread with a pointer to the appropriate
2271 resume request.
2272
2273 This algorithm is O(threads * resume elements), but resume elements
2274 is small (and will remain small at least until GDB supports thread
2275 suspension). */
2276 static int
2277 linux_set_resume_request (struct inferior_list_entry *entry, void *arg)
2278 {
2279 struct lwp_info *lwp;
2280 struct thread_info *thread;
2281 int ndx;
2282 struct thread_resume_array *r;
2283
2284 thread = (struct thread_info *) entry;
2285 lwp = get_thread_lwp (thread);
2286 r = arg;
2287
2288 for (ndx = 0; ndx < r->n; ndx++)
2289 {
2290 ptid_t ptid = r->resume[ndx].thread;
2291 if (ptid_equal (ptid, minus_one_ptid)
2292 || ptid_equal (ptid, entry->id)
2293 || (ptid_is_pid (ptid)
2294 && (ptid_get_pid (ptid) == pid_of (lwp)))
2295 || (ptid_get_lwp (ptid) == -1
2296 && (ptid_get_pid (ptid) == pid_of (lwp))))
2297 {
2298 if (r->resume[ndx].kind == resume_stop
2299 && lwp->last_resume_kind == resume_stop)
2300 {
2301 if (debug_threads)
2302 fprintf (stderr, "already %s LWP %ld at GDB's request\n",
2303 thread->last_status.kind == TARGET_WAITKIND_STOPPED
2304 ? "stopped"
2305 : "stopping",
2306 lwpid_of (lwp));
2307
2308 continue;
2309 }
2310
2311 lwp->resume = &r->resume[ndx];
2312 lwp->last_resume_kind = lwp->resume->kind;
2313 return 0;
2314 }
2315 }
2316
2317 /* No resume action for this thread. */
2318 lwp->resume = NULL;
2319
2320 return 0;
2321 }
2322
2323
2324 /* Set *FLAG_P if this lwp has an interesting status pending. */
2325 static int
2326 resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
2327 {
2328 struct lwp_info *lwp = (struct lwp_info *) entry;
2329
2330 /* LWPs which will not be resumed are not interesting, because
2331 we might not wait for them next time through linux_wait. */
2332 if (lwp->resume == NULL)
2333 return 0;
2334
2335 if (lwp->status_pending_p)
2336 * (int *) flag_p = 1;
2337
2338 return 0;
2339 }
2340
2341 /* Return 1 if this lwp that GDB wants running is stopped at an
2342 internal breakpoint that we need to step over. It assumes that any
2343 required STOP_PC adjustment has already been propagated to the
2344 inferior's regcache. */
2345
2346 static int
2347 need_step_over_p (struct inferior_list_entry *entry, void *dummy)
2348 {
2349 struct lwp_info *lwp = (struct lwp_info *) entry;
2350 struct thread_info *saved_inferior;
2351 CORE_ADDR pc;
2352
2353 /* LWPs which will not be resumed are not interesting, because we
2354 might not wait for them next time through linux_wait. */
2355
2356 if (!lwp->stopped)
2357 {
2358 if (debug_threads)
2359 fprintf (stderr,
2360 "Need step over [LWP %ld]? Ignoring, not stopped\n",
2361 lwpid_of (lwp));
2362 return 0;
2363 }
2364
2365 if (lwp->last_resume_kind == resume_stop)
2366 {
2367 if (debug_threads)
2368 fprintf (stderr,
2369 "Need step over [LWP %ld]? Ignoring, should remain stopped\n",
2370 lwpid_of (lwp));
2371 return 0;
2372 }
2373
2374 if (!lwp->need_step_over)
2375 {
2376 if (debug_threads)
2377 fprintf (stderr,
2378 "Need step over [LWP %ld]? No\n", lwpid_of (lwp));
2379 }
2380
2381 if (lwp->status_pending_p)
2382 {
2383 if (debug_threads)
2384 fprintf (stderr,
2385 "Need step over [LWP %ld]? Ignoring, has pending status.\n",
2386 lwpid_of (lwp));
2387 return 0;
2388 }
2389
2390 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
2391 or we have. */
2392 pc = get_pc (lwp);
2393
2394 /* If the PC has changed since we stopped, then don't do anything,
2395 and let the breakpoint/tracepoint be hit. This happens if, for
2396 instance, GDB handled the decr_pc_after_break subtraction itself,
2397 GDB is OOL stepping this thread, or the user has issued a "jump"
2398 command, or poked thread's registers herself. */
2399 if (pc != lwp->stop_pc)
2400 {
2401 if (debug_threads)
2402 fprintf (stderr,
2403 "Need step over [LWP %ld]? Cancelling, PC was changed. "
2404 "Old stop_pc was 0x%s, PC is now 0x%s\n",
2405 lwpid_of (lwp), paddress (lwp->stop_pc), paddress (pc));
2406
2407 lwp->need_step_over = 0;
2408 return 0;
2409 }
2410
2411 saved_inferior = current_inferior;
2412 current_inferior = get_lwp_thread (lwp);
2413
2414 /* We can only step over breakpoints we know about. */
2415 if (breakpoint_here (pc))
2416 {
2417 /* Don't step over a breakpoint that GDB expects to hit
2418 though. */
2419 if (gdb_breakpoint_here (pc))
2420 {
2421 if (debug_threads)
2422 fprintf (stderr,
2423 "Need step over [LWP %ld]? yes, but found"
2424 " GDB breakpoint at 0x%s; skipping step over\n",
2425 lwpid_of (lwp), paddress (pc));
2426
2427 current_inferior = saved_inferior;
2428 return 0;
2429 }
2430 else
2431 {
2432 if (debug_threads)
2433 fprintf (stderr,
2434 "Need step over [LWP %ld]? yes, found breakpoint at 0x%s\n",
2435 lwpid_of (lwp), paddress (pc));
2436
2437 /* We've found an lwp that needs stepping over --- return 1 so
2438 that find_inferior stops looking. */
2439 current_inferior = saved_inferior;
2440
2441 /* If the step over is cancelled, this is set again. */
2442 lwp->need_step_over = 0;
2443 return 1;
2444 }
2445 }
2446
2447 current_inferior = saved_inferior;
2448
2449 if (debug_threads)
2450 fprintf (stderr,
2451 "Need step over [LWP %ld]? No, no breakpoint found at 0x%s\n",
2452 lwpid_of (lwp), paddress (pc));
2453
2454 return 0;
2455 }
2456
2457 /* Start a step-over operation on LWP. When LWP stopped at a
2458 breakpoint, to make progress, we need to remove the breakpoint out
2459 of the way. If we let other threads run while we do that, they may
2460 pass by the breakpoint location and miss hitting it. To avoid
2461 that, a step-over momentarily stops all threads while LWP is
2462 single-stepped while the breakpoint is temporarily uninserted from
2463 the inferior. When the single-step finishes, we reinsert the
2464 breakpoint, and let all threads that are supposed to be running,
2465 run again.
2466
2467 On targets that don't support hardware single-step, we don't
2468 currently support full software single-stepping. Instead, we only
2469 support stepping over the thread event breakpoint, by asking the
2470 low target where to place a reinsert breakpoint. Since this
2471 routine assumes the breakpoint being stepped over is a thread event
2472 breakpoint, it usually assumes the return address of the current
2473 function is a good enough place to set the reinsert breakpoint. */
2474
2475 static int
2476 start_step_over (struct lwp_info *lwp)
2477 {
2478 struct thread_info *saved_inferior;
2479 CORE_ADDR pc;
2480 int step;
2481
2482 if (debug_threads)
2483 fprintf (stderr,
2484 "Starting step-over on LWP %ld. Stopping all threads\n",
2485 lwpid_of (lwp));
2486
2487 stop_all_lwps ();
2488
2489 if (debug_threads)
2490 fprintf (stderr, "Done stopping all threads for step-over.\n");
2491
2492 /* Note, we should always reach here with an already adjusted PC,
2493 either by GDB (if we're resuming due to GDB's request), or by our
2494 caller, if we just finished handling an internal breakpoint GDB
2495 shouldn't care about. */
2496 pc = get_pc (lwp);
2497
2498 saved_inferior = current_inferior;
2499 current_inferior = get_lwp_thread (lwp);
2500
2501 lwp->bp_reinsert = pc;
2502 uninsert_breakpoints_at (pc);
2503
2504 if (can_hardware_single_step ())
2505 {
2506 step = 1;
2507 }
2508 else
2509 {
2510 CORE_ADDR raddr = (*the_low_target.breakpoint_reinsert_addr) ();
2511 set_reinsert_breakpoint (raddr);
2512 step = 0;
2513 }
2514
2515 current_inferior = saved_inferior;
2516
2517 linux_resume_one_lwp (lwp, step, 0, NULL);
2518
2519 /* Require next event from this LWP. */
2520 step_over_bkpt = lwp->head.id;
2521 return 1;
2522 }
2523
2524 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
2525 start_step_over, if still there, and delete any reinsert
2526 breakpoints we've set, on non hardware single-step targets. */
2527
2528 static int
2529 finish_step_over (struct lwp_info *lwp)
2530 {
2531 if (lwp->bp_reinsert != 0)
2532 {
2533 if (debug_threads)
2534 fprintf (stderr, "Finished step over.\n");
2535
2536 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
2537 may be no breakpoint to reinsert there by now. */
2538 reinsert_breakpoints_at (lwp->bp_reinsert);
2539
2540 lwp->bp_reinsert = 0;
2541
2542 /* Delete any software-single-step reinsert breakpoints. No
2543 longer needed. We don't have to worry about other threads
2544 hitting this trap, and later not being able to explain it,
2545 because we were stepping over a breakpoint, and we hold all
2546 threads but LWP stopped while doing that. */
2547 if (!can_hardware_single_step ())
2548 delete_reinsert_breakpoints ();
2549
2550 step_over_bkpt = null_ptid;
2551 return 1;
2552 }
2553 else
2554 return 0;
2555 }
2556
2557 /* This function is called once per thread. We check the thread's resume
2558 request, which will tell us whether to resume, step, or leave the thread
2559 stopped; and what signal, if any, it should be sent.
2560
2561 For threads which we aren't explicitly told otherwise, we preserve
2562 the stepping flag; this is used for stepping over gdbserver-placed
2563 breakpoints.
2564
2565 If pending_flags was set in any thread, we queue any needed
2566 signals, since we won't actually resume. We already have a pending
2567 event to report, so we don't need to preserve any step requests;
2568 they should be re-issued if necessary. */
2569
2570 static int
2571 linux_resume_one_thread (struct inferior_list_entry *entry, void *arg)
2572 {
2573 struct lwp_info *lwp;
2574 struct thread_info *thread;
2575 int step;
2576 int leave_all_stopped = * (int *) arg;
2577 int leave_pending;
2578
2579 thread = (struct thread_info *) entry;
2580 lwp = get_thread_lwp (thread);
2581
2582 if (lwp->resume == NULL)
2583 return 0;
2584
2585 if (lwp->resume->kind == resume_stop)
2586 {
2587 if (debug_threads)
2588 fprintf (stderr, "resume_stop request for LWP %ld\n", lwpid_of (lwp));
2589
2590 if (!lwp->stopped)
2591 {
2592 if (debug_threads)
2593 fprintf (stderr, "stopping LWP %ld\n", lwpid_of (lwp));
2594
2595 /* Stop the thread, and wait for the event asynchronously,
2596 through the event loop. */
2597 send_sigstop (&lwp->head);
2598 }
2599 else
2600 {
2601 if (debug_threads)
2602 fprintf (stderr, "already stopped LWP %ld\n",
2603 lwpid_of (lwp));
2604
2605 /* The LWP may have been stopped in an internal event that
2606 was not meant to be notified back to GDB (e.g., gdbserver
2607 breakpoint), so we should be reporting a stop event in
2608 this case too. */
2609
2610 /* If the thread already has a pending SIGSTOP, this is a
2611 no-op. Otherwise, something later will presumably resume
2612 the thread and this will cause it to cancel any pending
2613 operation, due to last_resume_kind == resume_stop. If
2614 the thread already has a pending status to report, we
2615 will still report it the next time we wait - see
2616 status_pending_p_callback. */
2617 send_sigstop (&lwp->head);
2618 }
2619
2620 /* For stop requests, we're done. */
2621 lwp->resume = NULL;
2622 get_lwp_thread (lwp)->last_status.kind = TARGET_WAITKIND_IGNORE;
2623 return 0;
2624 }
2625
2626 /* If this thread which is about to be resumed has a pending status,
2627 then don't resume any threads - we can just report the pending
2628 status. Make sure to queue any signals that would otherwise be
2629 sent. In all-stop mode, we do this decision based on if *any*
2630 thread has a pending status. If there's a thread that needs the
2631 step-over-breakpoint dance, then don't resume any other thread
2632 but that particular one. */
2633 leave_pending = (lwp->status_pending_p || leave_all_stopped);
2634
2635 if (!leave_pending)
2636 {
2637 if (debug_threads)
2638 fprintf (stderr, "resuming LWP %ld\n", lwpid_of (lwp));
2639
2640 step = (lwp->resume->kind == resume_step);
2641 linux_resume_one_lwp (lwp, step, lwp->resume->sig, NULL);
2642 get_lwp_thread (lwp)->last_status.kind = TARGET_WAITKIND_IGNORE;
2643 }
2644 else
2645 {
2646 if (debug_threads)
2647 fprintf (stderr, "leaving LWP %ld stopped\n", lwpid_of (lwp));
2648
2649 /* If we have a new signal, enqueue the signal. */
2650 if (lwp->resume->sig != 0)
2651 {
2652 struct pending_signals *p_sig;
2653 p_sig = xmalloc (sizeof (*p_sig));
2654 p_sig->prev = lwp->pending_signals;
2655 p_sig->signal = lwp->resume->sig;
2656 memset (&p_sig->info, 0, sizeof (siginfo_t));
2657
2658 /* If this is the same signal we were previously stopped by,
2659 make sure to queue its siginfo. We can ignore the return
2660 value of ptrace; if it fails, we'll skip
2661 PTRACE_SETSIGINFO. */
2662 if (WIFSTOPPED (lwp->last_status)
2663 && WSTOPSIG (lwp->last_status) == lwp->resume->sig)
2664 ptrace (PTRACE_GETSIGINFO, lwpid_of (lwp), 0, &p_sig->info);
2665
2666 lwp->pending_signals = p_sig;
2667 }
2668 }
2669
2670 lwp->resume = NULL;
2671 return 0;
2672 }
2673
2674 static void
2675 linux_resume (struct thread_resume *resume_info, size_t n)
2676 {
2677 struct thread_resume_array array = { resume_info, n };
2678 struct lwp_info *need_step_over = NULL;
2679 int any_pending;
2680 int leave_all_stopped;
2681
2682 find_inferior (&all_threads, linux_set_resume_request, &array);
2683
2684 /* If there is a thread which would otherwise be resumed, which has
2685 a pending status, then don't resume any threads - we can just
2686 report the pending status. Make sure to queue any signals that
2687 would otherwise be sent. In non-stop mode, we'll apply this
2688 logic to each thread individually. We consume all pending events
2689 before considering to start a step-over (in all-stop). */
2690 any_pending = 0;
2691 if (!non_stop)
2692 find_inferior (&all_lwps, resume_status_pending_p, &any_pending);
2693
2694 /* If there is a thread which would otherwise be resumed, which is
2695 stopped at a breakpoint that needs stepping over, then don't
2696 resume any threads - have it step over the breakpoint with all
2697 other threads stopped, then resume all threads again. Make sure
2698 to queue any signals that would otherwise be delivered or
2699 queued. */
2700 if (!any_pending && supports_breakpoints ())
2701 need_step_over
2702 = (struct lwp_info *) find_inferior (&all_lwps,
2703 need_step_over_p, NULL);
2704
2705 leave_all_stopped = (need_step_over != NULL || any_pending);
2706
2707 if (debug_threads)
2708 {
2709 if (need_step_over != NULL)
2710 fprintf (stderr, "Not resuming all, need step over\n");
2711 else if (any_pending)
2712 fprintf (stderr,
2713 "Not resuming, all-stop and found "
2714 "an LWP with pending status\n");
2715 else
2716 fprintf (stderr, "Resuming, no pending status or step over needed\n");
2717 }
2718
2719 /* Even if we're leaving threads stopped, queue all signals we'd
2720 otherwise deliver. */
2721 find_inferior (&all_threads, linux_resume_one_thread, &leave_all_stopped);
2722
2723 if (need_step_over)
2724 start_step_over (need_step_over);
2725 }
2726
2727 /* This function is called once per thread. We check the thread's
2728 last resume request, which will tell us whether to resume, step, or
2729 leave the thread stopped. Any signal the client requested to be
2730 delivered has already been enqueued at this point.
2731
2732 If any thread that GDB wants running is stopped at an internal
2733 breakpoint that needs stepping over, we start a step-over operation
2734 on that particular thread, and leave all others stopped. */
2735
2736 static void
2737 proceed_one_lwp (struct inferior_list_entry *entry)
2738 {
2739 struct lwp_info *lwp;
2740 int step;
2741
2742 lwp = (struct lwp_info *) entry;
2743
2744 if (debug_threads)
2745 fprintf (stderr,
2746 "proceed_one_lwp: lwp %ld\n", lwpid_of (lwp));
2747
2748 if (!lwp->stopped)
2749 {
2750 if (debug_threads)
2751 fprintf (stderr, " LWP %ld already running\n", lwpid_of (lwp));
2752 return;
2753 }
2754
2755 if (lwp->last_resume_kind == resume_stop)
2756 {
2757 if (debug_threads)
2758 fprintf (stderr, " client wants LWP %ld stopped\n", lwpid_of (lwp));
2759 return;
2760 }
2761
2762 if (lwp->status_pending_p)
2763 {
2764 if (debug_threads)
2765 fprintf (stderr, " LWP %ld has pending status, leaving stopped\n",
2766 lwpid_of (lwp));
2767 return;
2768 }
2769
2770 if (lwp->suspended)
2771 {
2772 if (debug_threads)
2773 fprintf (stderr, " LWP %ld is suspended\n", lwpid_of (lwp));
2774 return;
2775 }
2776
2777 step = lwp->last_resume_kind == resume_step;
2778 linux_resume_one_lwp (lwp, step, 0, NULL);
2779 }
2780
2781 /* When we finish a step-over, set threads running again. If there's
2782 another thread that may need a step-over, now's the time to start
2783 it. Eventually, we'll move all threads past their breakpoints. */
2784
2785 static void
2786 proceed_all_lwps (void)
2787 {
2788 struct lwp_info *need_step_over;
2789
2790 /* If there is a thread which would otherwise be resumed, which is
2791 stopped at a breakpoint that needs stepping over, then don't
2792 resume any threads - have it step over the breakpoint with all
2793 other threads stopped, then resume all threads again. */
2794
2795 if (supports_breakpoints ())
2796 {
2797 need_step_over
2798 = (struct lwp_info *) find_inferior (&all_lwps,
2799 need_step_over_p, NULL);
2800
2801 if (need_step_over != NULL)
2802 {
2803 if (debug_threads)
2804 fprintf (stderr, "proceed_all_lwps: found "
2805 "thread %ld needing a step-over\n",
2806 lwpid_of (need_step_over));
2807
2808 start_step_over (need_step_over);
2809 return;
2810 }
2811 }
2812
2813 if (debug_threads)
2814 fprintf (stderr, "Proceeding, no step-over needed\n");
2815
2816 for_each_inferior (&all_lwps, proceed_one_lwp);
2817 }
2818
2819 /* Stopped LWPs that the client wanted to be running, that don't have
2820 pending statuses, are set to run again, except for EXCEPT, if not
2821 NULL. This undoes a stop_all_lwps call. */
2822
2823 static void
2824 unstop_all_lwps (struct lwp_info *except)
2825 {
2826 if (debug_threads)
2827 {
2828 if (except)
2829 fprintf (stderr,
2830 "unstopping all lwps, except=(LWP %ld)\n", lwpid_of (except));
2831 else
2832 fprintf (stderr,
2833 "unstopping all lwps\n");
2834 }
2835
2836 /* Make sure proceed_one_lwp doesn't try to resume this thread. */
2837 if (except != NULL)
2838 ++except->suspended;
2839
2840 for_each_inferior (&all_lwps, proceed_one_lwp);
2841
2842 if (except != NULL)
2843 --except->suspended;
2844 }
2845
2846 #ifdef HAVE_LINUX_USRREGS
2847
2848 int
2849 register_addr (int regnum)
2850 {
2851 int addr;
2852
2853 if (regnum < 0 || regnum >= the_low_target.num_regs)
2854 error ("Invalid register number %d.", regnum);
2855
2856 addr = the_low_target.regmap[regnum];
2857
2858 return addr;
2859 }
2860
2861 /* Fetch one register. */
2862 static void
2863 fetch_register (struct regcache *regcache, int regno)
2864 {
2865 CORE_ADDR regaddr;
2866 int i, size;
2867 char *buf;
2868 int pid;
2869
2870 if (regno >= the_low_target.num_regs)
2871 return;
2872 if ((*the_low_target.cannot_fetch_register) (regno))
2873 return;
2874
2875 regaddr = register_addr (regno);
2876 if (regaddr == -1)
2877 return;
2878
2879 pid = lwpid_of (get_thread_lwp (current_inferior));
2880 size = ((register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
2881 & - sizeof (PTRACE_XFER_TYPE));
2882 buf = alloca (size);
2883 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
2884 {
2885 errno = 0;
2886 *(PTRACE_XFER_TYPE *) (buf + i) =
2887 ptrace (PTRACE_PEEKUSER, pid,
2888 /* Coerce to a uintptr_t first to avoid potential gcc warning
2889 of coercing an 8 byte integer to a 4 byte pointer. */
2890 (PTRACE_ARG3_TYPE) (uintptr_t) regaddr, 0);
2891 regaddr += sizeof (PTRACE_XFER_TYPE);
2892 if (errno != 0)
2893 error ("reading register %d: %s", regno, strerror (errno));
2894 }
2895
2896 if (the_low_target.supply_ptrace_register)
2897 the_low_target.supply_ptrace_register (regcache, regno, buf);
2898 else
2899 supply_register (regcache, regno, buf);
2900 }
2901
2902 /* Fetch all registers, or just one, from the child process. */
2903 static void
2904 usr_fetch_inferior_registers (struct regcache *regcache, int regno)
2905 {
2906 if (regno == -1)
2907 for (regno = 0; regno < the_low_target.num_regs; regno++)
2908 fetch_register (regcache, regno);
2909 else
2910 fetch_register (regcache, regno);
2911 }
2912
2913 /* Store our register values back into the inferior.
2914 If REGNO is -1, do this for all registers.
2915 Otherwise, REGNO specifies which register (so we can save time). */
2916 static void
2917 usr_store_inferior_registers (struct regcache *regcache, int regno)
2918 {
2919 CORE_ADDR regaddr;
2920 int i, size;
2921 char *buf;
2922 int pid;
2923
2924 if (regno >= 0)
2925 {
2926 if (regno >= the_low_target.num_regs)
2927 return;
2928
2929 if ((*the_low_target.cannot_store_register) (regno) == 1)
2930 return;
2931
2932 regaddr = register_addr (regno);
2933 if (regaddr == -1)
2934 return;
2935 errno = 0;
2936 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
2937 & - sizeof (PTRACE_XFER_TYPE);
2938 buf = alloca (size);
2939 memset (buf, 0, size);
2940
2941 if (the_low_target.collect_ptrace_register)
2942 the_low_target.collect_ptrace_register (regcache, regno, buf);
2943 else
2944 collect_register (regcache, regno, buf);
2945
2946 pid = lwpid_of (get_thread_lwp (current_inferior));
2947 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
2948 {
2949 errno = 0;
2950 ptrace (PTRACE_POKEUSER, pid,
2951 /* Coerce to a uintptr_t first to avoid potential gcc warning
2952 about coercing an 8 byte integer to a 4 byte pointer. */
2953 (PTRACE_ARG3_TYPE) (uintptr_t) regaddr,
2954 (PTRACE_ARG4_TYPE) *(PTRACE_XFER_TYPE *) (buf + i));
2955 if (errno != 0)
2956 {
2957 /* At this point, ESRCH should mean the process is
2958 already gone, in which case we simply ignore attempts
2959 to change its registers. See also the related
2960 comment in linux_resume_one_lwp. */
2961 if (errno == ESRCH)
2962 return;
2963
2964 if ((*the_low_target.cannot_store_register) (regno) == 0)
2965 error ("writing register %d: %s", regno, strerror (errno));
2966 }
2967 regaddr += sizeof (PTRACE_XFER_TYPE);
2968 }
2969 }
2970 else
2971 for (regno = 0; regno < the_low_target.num_regs; regno++)
2972 usr_store_inferior_registers (regcache, regno);
2973 }
2974 #endif /* HAVE_LINUX_USRREGS */
2975
2976
2977
2978 #ifdef HAVE_LINUX_REGSETS
2979
2980 static int
2981 regsets_fetch_inferior_registers (struct regcache *regcache)
2982 {
2983 struct regset_info *regset;
2984 int saw_general_regs = 0;
2985 int pid;
2986
2987 regset = target_regsets;
2988
2989 pid = lwpid_of (get_thread_lwp (current_inferior));
2990 while (regset->size >= 0)
2991 {
2992 void *buf;
2993 int res;
2994
2995 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
2996 {
2997 regset ++;
2998 continue;
2999 }
3000
3001 buf = xmalloc (regset->size);
3002 #ifndef __sparc__
3003 res = ptrace (regset->get_request, pid, 0, buf);
3004 #else
3005 res = ptrace (regset->get_request, pid, buf, 0);
3006 #endif
3007 if (res < 0)
3008 {
3009 if (errno == EIO)
3010 {
3011 /* If we get EIO on a regset, do not try it again for
3012 this process. */
3013 disabled_regsets[regset - target_regsets] = 1;
3014 free (buf);
3015 continue;
3016 }
3017 else
3018 {
3019 char s[256];
3020 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
3021 pid);
3022 perror (s);
3023 }
3024 }
3025 else if (regset->type == GENERAL_REGS)
3026 saw_general_regs = 1;
3027 regset->store_function (regcache, buf);
3028 regset ++;
3029 free (buf);
3030 }
3031 if (saw_general_regs)
3032 return 0;
3033 else
3034 return 1;
3035 }
3036
3037 static int
3038 regsets_store_inferior_registers (struct regcache *regcache)
3039 {
3040 struct regset_info *regset;
3041 int saw_general_regs = 0;
3042 int pid;
3043
3044 regset = target_regsets;
3045
3046 pid = lwpid_of (get_thread_lwp (current_inferior));
3047 while (regset->size >= 0)
3048 {
3049 void *buf;
3050 int res;
3051
3052 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
3053 {
3054 regset ++;
3055 continue;
3056 }
3057
3058 buf = xmalloc (regset->size);
3059
3060 /* First fill the buffer with the current register set contents,
3061 in case there are any items in the kernel's regset that are
3062 not in gdbserver's regcache. */
3063 #ifndef __sparc__
3064 res = ptrace (regset->get_request, pid, 0, buf);
3065 #else
3066 res = ptrace (regset->get_request, pid, buf, 0);
3067 #endif
3068
3069 if (res == 0)
3070 {
3071 /* Then overlay our cached registers on that. */
3072 regset->fill_function (regcache, buf);
3073
3074 /* Only now do we write the register set. */
3075 #ifndef __sparc__
3076 res = ptrace (regset->set_request, pid, 0, buf);
3077 #else
3078 res = ptrace (regset->set_request, pid, buf, 0);
3079 #endif
3080 }
3081
3082 if (res < 0)
3083 {
3084 if (errno == EIO)
3085 {
3086 /* If we get EIO on a regset, do not try it again for
3087 this process. */
3088 disabled_regsets[regset - target_regsets] = 1;
3089 free (buf);
3090 continue;
3091 }
3092 else if (errno == ESRCH)
3093 {
3094 /* At this point, ESRCH should mean the process is
3095 already gone, in which case we simply ignore attempts
3096 to change its registers. See also the related
3097 comment in linux_resume_one_lwp. */
3098 free (buf);
3099 return 0;
3100 }
3101 else
3102 {
3103 perror ("Warning: ptrace(regsets_store_inferior_registers)");
3104 }
3105 }
3106 else if (regset->type == GENERAL_REGS)
3107 saw_general_regs = 1;
3108 regset ++;
3109 free (buf);
3110 }
3111 if (saw_general_regs)
3112 return 0;
3113 else
3114 return 1;
3115 return 0;
3116 }
3117
3118 #endif /* HAVE_LINUX_REGSETS */
3119
3120
3121 void
3122 linux_fetch_registers (struct regcache *regcache, int regno)
3123 {
3124 #ifdef HAVE_LINUX_REGSETS
3125 if (regsets_fetch_inferior_registers (regcache) == 0)
3126 return;
3127 #endif
3128 #ifdef HAVE_LINUX_USRREGS
3129 usr_fetch_inferior_registers (regcache, regno);
3130 #endif
3131 }
3132
3133 void
3134 linux_store_registers (struct regcache *regcache, int regno)
3135 {
3136 #ifdef HAVE_LINUX_REGSETS
3137 if (regsets_store_inferior_registers (regcache) == 0)
3138 return;
3139 #endif
3140 #ifdef HAVE_LINUX_USRREGS
3141 usr_store_inferior_registers (regcache, regno);
3142 #endif
3143 }
3144
3145
3146 /* Copy LEN bytes from inferior's memory starting at MEMADDR
3147 to debugger memory starting at MYADDR. */
3148
3149 static int
3150 linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
3151 {
3152 register int i;
3153 /* Round starting address down to longword boundary. */
3154 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
3155 /* Round ending address up; get number of longwords that makes. */
3156 register int count
3157 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
3158 / sizeof (PTRACE_XFER_TYPE);
3159 /* Allocate buffer of that many longwords. */
3160 register PTRACE_XFER_TYPE *buffer
3161 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
3162 int fd;
3163 char filename[64];
3164 int pid = lwpid_of (get_thread_lwp (current_inferior));
3165
3166 /* Try using /proc. Don't bother for one word. */
3167 if (len >= 3 * sizeof (long))
3168 {
3169 /* We could keep this file open and cache it - possibly one per
3170 thread. That requires some juggling, but is even faster. */
3171 sprintf (filename, "/proc/%d/mem", pid);
3172 fd = open (filename, O_RDONLY | O_LARGEFILE);
3173 if (fd == -1)
3174 goto no_proc;
3175
3176 /* If pread64 is available, use it. It's faster if the kernel
3177 supports it (only one syscall), and it's 64-bit safe even on
3178 32-bit platforms (for instance, SPARC debugging a SPARC64
3179 application). */
3180 #ifdef HAVE_PREAD64
3181 if (pread64 (fd, myaddr, len, memaddr) != len)
3182 #else
3183 if (lseek (fd, memaddr, SEEK_SET) == -1 || read (fd, myaddr, len) != len)
3184 #endif
3185 {
3186 close (fd);
3187 goto no_proc;
3188 }
3189
3190 close (fd);
3191 return 0;
3192 }
3193
3194 no_proc:
3195 /* Read all the longwords */
3196 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
3197 {
3198 errno = 0;
3199 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
3200 about coercing an 8 byte integer to a 4 byte pointer. */
3201 buffer[i] = ptrace (PTRACE_PEEKTEXT, pid,
3202 (PTRACE_ARG3_TYPE) (uintptr_t) addr, 0);
3203 if (errno)
3204 return errno;
3205 }
3206
3207 /* Copy appropriate bytes out of the buffer. */
3208 memcpy (myaddr,
3209 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
3210 len);
3211
3212 return 0;
3213 }
3214
3215 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
3216 memory at MEMADDR. On failure (cannot write to the inferior)
3217 returns the value of errno. */
3218
3219 static int
3220 linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
3221 {
3222 register int i;
3223 /* Round starting address down to longword boundary. */
3224 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
3225 /* Round ending address up; get number of longwords that makes. */
3226 register int count
3227 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
3228 /* Allocate buffer of that many longwords. */
3229 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
3230 int pid = lwpid_of (get_thread_lwp (current_inferior));
3231
3232 if (debug_threads)
3233 {
3234 /* Dump up to four bytes. */
3235 unsigned int val = * (unsigned int *) myaddr;
3236 if (len == 1)
3237 val = val & 0xff;
3238 else if (len == 2)
3239 val = val & 0xffff;
3240 else if (len == 3)
3241 val = val & 0xffffff;
3242 fprintf (stderr, "Writing %0*x to 0x%08lx\n", 2 * ((len < 4) ? len : 4),
3243 val, (long)memaddr);
3244 }
3245
3246 /* Fill start and end extra bytes of buffer with existing memory data. */
3247
3248 errno = 0;
3249 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
3250 about coercing an 8 byte integer to a 4 byte pointer. */
3251 buffer[0] = ptrace (PTRACE_PEEKTEXT, pid,
3252 (PTRACE_ARG3_TYPE) (uintptr_t) addr, 0);
3253 if (errno)
3254 return errno;
3255
3256 if (count > 1)
3257 {
3258 errno = 0;
3259 buffer[count - 1]
3260 = ptrace (PTRACE_PEEKTEXT, pid,
3261 /* Coerce to a uintptr_t first to avoid potential gcc warning
3262 about coercing an 8 byte integer to a 4 byte pointer. */
3263 (PTRACE_ARG3_TYPE) (uintptr_t) (addr + (count - 1)
3264 * sizeof (PTRACE_XFER_TYPE)),
3265 0);
3266 if (errno)
3267 return errno;
3268 }
3269
3270 /* Copy data to be written over corresponding part of buffer. */
3271
3272 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
3273
3274 /* Write the entire buffer. */
3275
3276 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
3277 {
3278 errno = 0;
3279 ptrace (PTRACE_POKETEXT, pid,
3280 /* Coerce to a uintptr_t first to avoid potential gcc warning
3281 about coercing an 8 byte integer to a 4 byte pointer. */
3282 (PTRACE_ARG3_TYPE) (uintptr_t) addr,
3283 (PTRACE_ARG4_TYPE) buffer[i]);
3284 if (errno)
3285 return errno;
3286 }
3287
3288 return 0;
3289 }
3290
3291 /* Non-zero if the kernel supports PTRACE_O_TRACEFORK. */
3292 static int linux_supports_tracefork_flag;
3293
3294 /* Helper functions for linux_test_for_tracefork, called via clone (). */
3295
3296 static int
3297 linux_tracefork_grandchild (void *arg)
3298 {
3299 _exit (0);
3300 }
3301
3302 #define STACK_SIZE 4096
3303
3304 static int
3305 linux_tracefork_child (void *arg)
3306 {
3307 ptrace (PTRACE_TRACEME, 0, 0, 0);
3308 kill (getpid (), SIGSTOP);
3309
3310 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
3311
3312 if (fork () == 0)
3313 linux_tracefork_grandchild (NULL);
3314
3315 #else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3316
3317 #ifdef __ia64__
3318 __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE,
3319 CLONE_VM | SIGCHLD, NULL);
3320 #else
3321 clone (linux_tracefork_grandchild, arg + STACK_SIZE,
3322 CLONE_VM | SIGCHLD, NULL);
3323 #endif
3324
3325 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3326
3327 _exit (0);
3328 }
3329
3330 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
3331 sure that we can enable the option, and that it had the desired
3332 effect. */
3333
3334 static void
3335 linux_test_for_tracefork (void)
3336 {
3337 int child_pid, ret, status;
3338 long second_pid;
3339 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
3340 char *stack = xmalloc (STACK_SIZE * 4);
3341 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3342
3343 linux_supports_tracefork_flag = 0;
3344
3345 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
3346
3347 child_pid = fork ();
3348 if (child_pid == 0)
3349 linux_tracefork_child (NULL);
3350
3351 #else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3352
3353 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
3354 #ifdef __ia64__
3355 child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE,
3356 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
3357 #else /* !__ia64__ */
3358 child_pid = clone (linux_tracefork_child, stack + STACK_SIZE,
3359 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
3360 #endif /* !__ia64__ */
3361
3362 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3363
3364 if (child_pid == -1)
3365 perror_with_name ("clone");
3366
3367 ret = my_waitpid (child_pid, &status, 0);
3368 if (ret == -1)
3369 perror_with_name ("waitpid");
3370 else if (ret != child_pid)
3371 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
3372 if (! WIFSTOPPED (status))
3373 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
3374
3375 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
3376 (PTRACE_ARG4_TYPE) PTRACE_O_TRACEFORK);
3377 if (ret != 0)
3378 {
3379 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
3380 if (ret != 0)
3381 {
3382 warning ("linux_test_for_tracefork: failed to kill child");
3383 return;
3384 }
3385
3386 ret = my_waitpid (child_pid, &status, 0);
3387 if (ret != child_pid)
3388 warning ("linux_test_for_tracefork: failed to wait for killed child");
3389 else if (!WIFSIGNALED (status))
3390 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
3391 "killed child", status);
3392
3393 return;
3394 }
3395
3396 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
3397 if (ret != 0)
3398 warning ("linux_test_for_tracefork: failed to resume child");
3399
3400 ret = my_waitpid (child_pid, &status, 0);
3401
3402 if (ret == child_pid && WIFSTOPPED (status)
3403 && status >> 16 == PTRACE_EVENT_FORK)
3404 {
3405 second_pid = 0;
3406 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
3407 if (ret == 0 && second_pid != 0)
3408 {
3409 int second_status;
3410
3411 linux_supports_tracefork_flag = 1;
3412 my_waitpid (second_pid, &second_status, 0);
3413 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
3414 if (ret != 0)
3415 warning ("linux_test_for_tracefork: failed to kill second child");
3416 my_waitpid (second_pid, &status, 0);
3417 }
3418 }
3419 else
3420 warning ("linux_test_for_tracefork: unexpected result from waitpid "
3421 "(%d, status 0x%x)", ret, status);
3422
3423 do
3424 {
3425 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
3426 if (ret != 0)
3427 warning ("linux_test_for_tracefork: failed to kill child");
3428 my_waitpid (child_pid, &status, 0);
3429 }
3430 while (WIFSTOPPED (status));
3431
3432 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
3433 free (stack);
3434 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3435 }
3436
3437
3438 static void
3439 linux_look_up_symbols (void)
3440 {
3441 #ifdef USE_THREAD_DB
3442 struct process_info *proc = current_process ();
3443
3444 if (proc->private->thread_db != NULL)
3445 return;
3446
3447 /* If the kernel supports tracing forks then it also supports tracing
3448 clones, and then we don't need to use the magic thread event breakpoint
3449 to learn about threads. */
3450 thread_db_init (!linux_supports_tracefork_flag);
3451 #endif
3452 }
3453
3454 static void
3455 linux_request_interrupt (void)
3456 {
3457 extern unsigned long signal_pid;
3458
3459 if (!ptid_equal (cont_thread, null_ptid)
3460 && !ptid_equal (cont_thread, minus_one_ptid))
3461 {
3462 struct lwp_info *lwp;
3463 int lwpid;
3464
3465 lwp = get_thread_lwp (current_inferior);
3466 lwpid = lwpid_of (lwp);
3467 kill_lwp (lwpid, SIGINT);
3468 }
3469 else
3470 kill_lwp (signal_pid, SIGINT);
3471 }
3472
3473 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
3474 to debugger memory starting at MYADDR. */
3475
3476 static int
3477 linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
3478 {
3479 char filename[PATH_MAX];
3480 int fd, n;
3481 int pid = lwpid_of (get_thread_lwp (current_inferior));
3482
3483 snprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
3484
3485 fd = open (filename, O_RDONLY);
3486 if (fd < 0)
3487 return -1;
3488
3489 if (offset != (CORE_ADDR) 0
3490 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
3491 n = -1;
3492 else
3493 n = read (fd, myaddr, len);
3494
3495 close (fd);
3496
3497 return n;
3498 }
3499
3500 /* These breakpoint and watchpoint related wrapper functions simply
3501 pass on the function call if the target has registered a
3502 corresponding function. */
3503
3504 static int
3505 linux_insert_point (char type, CORE_ADDR addr, int len)
3506 {
3507 if (the_low_target.insert_point != NULL)
3508 return the_low_target.insert_point (type, addr, len);
3509 else
3510 /* Unsupported (see target.h). */
3511 return 1;
3512 }
3513
3514 static int
3515 linux_remove_point (char type, CORE_ADDR addr, int len)
3516 {
3517 if (the_low_target.remove_point != NULL)
3518 return the_low_target.remove_point (type, addr, len);
3519 else
3520 /* Unsupported (see target.h). */
3521 return 1;
3522 }
3523
3524 static int
3525 linux_stopped_by_watchpoint (void)
3526 {
3527 struct lwp_info *lwp = get_thread_lwp (current_inferior);
3528
3529 return lwp->stopped_by_watchpoint;
3530 }
3531
3532 static CORE_ADDR
3533 linux_stopped_data_address (void)
3534 {
3535 struct lwp_info *lwp = get_thread_lwp (current_inferior);
3536
3537 return lwp->stopped_data_address;
3538 }
3539
3540 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
3541 #if defined(__mcoldfire__)
3542 /* These should really be defined in the kernel's ptrace.h header. */
3543 #define PT_TEXT_ADDR 49*4
3544 #define PT_DATA_ADDR 50*4
3545 #define PT_TEXT_END_ADDR 51*4
3546 #endif
3547
3548 /* Under uClinux, programs are loaded at non-zero offsets, which we need
3549 to tell gdb about. */
3550
3551 static int
3552 linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
3553 {
3554 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
3555 unsigned long text, text_end, data;
3556 int pid = lwpid_of (get_thread_lwp (current_inferior));
3557
3558 errno = 0;
3559
3560 text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0);
3561 text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0);
3562 data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0);
3563
3564 if (errno == 0)
3565 {
3566 /* Both text and data offsets produced at compile-time (and so
3567 used by gdb) are relative to the beginning of the program,
3568 with the data segment immediately following the text segment.
3569 However, the actual runtime layout in memory may put the data
3570 somewhere else, so when we send gdb a data base-address, we
3571 use the real data base address and subtract the compile-time
3572 data base-address from it (which is just the length of the
3573 text segment). BSS immediately follows data in both
3574 cases. */
3575 *text_p = text;
3576 *data_p = data - (text_end - text);
3577
3578 return 1;
3579 }
3580 #endif
3581 return 0;
3582 }
3583 #endif
3584
3585 static int
3586 compare_ints (const void *xa, const void *xb)
3587 {
3588 int a = *(const int *)xa;
3589 int b = *(const int *)xb;
3590
3591 return a - b;
3592 }
3593
3594 static int *
3595 unique (int *b, int *e)
3596 {
3597 int *d = b;
3598 while (++b != e)
3599 if (*d != *b)
3600 *++d = *b;
3601 return ++d;
3602 }
3603
3604 /* Given PID, iterates over all threads in that process.
3605
3606 Information about each thread, in a format suitable for qXfer:osdata:thread
3607 is printed to BUFFER, if it's not NULL. BUFFER is assumed to be already
3608 initialized, and the caller is responsible for finishing and appending '\0'
3609 to it.
3610
3611 The list of cores that threads are running on is assigned to *CORES, if it
3612 is not NULL. If no cores are found, *CORES will be set to NULL. Caller
3613 should free *CORES. */
3614
3615 static void
3616 list_threads (int pid, struct buffer *buffer, char **cores)
3617 {
3618 int count = 0;
3619 int allocated = 10;
3620 int *core_numbers = xmalloc (sizeof (int) * allocated);
3621 char pathname[128];
3622 DIR *dir;
3623 struct dirent *dp;
3624 struct stat statbuf;
3625
3626 sprintf (pathname, "/proc/%d/task", pid);
3627 if (stat (pathname, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
3628 {
3629 dir = opendir (pathname);
3630 if (!dir)
3631 {
3632 free (core_numbers);
3633 return;
3634 }
3635
3636 while ((dp = readdir (dir)) != NULL)
3637 {
3638 unsigned long lwp = strtoul (dp->d_name, NULL, 10);
3639
3640 if (lwp != 0)
3641 {
3642 unsigned core = linux_core_of_thread (ptid_build (pid, lwp, 0));
3643
3644 if (core != -1)
3645 {
3646 char s[sizeof ("4294967295")];
3647 sprintf (s, "%u", core);
3648
3649 if (count == allocated)
3650 {
3651 allocated *= 2;
3652 core_numbers = realloc (core_numbers,
3653 sizeof (int) * allocated);
3654 }
3655 core_numbers[count++] = core;
3656 if (buffer)
3657 buffer_xml_printf (buffer,
3658 "<item>"
3659 "<column name=\"pid\">%d</column>"
3660 "<column name=\"tid\">%s</column>"
3661 "<column name=\"core\">%s</column>"
3662 "</item>", pid, dp->d_name, s);
3663 }
3664 else
3665 {
3666 if (buffer)
3667 buffer_xml_printf (buffer,
3668 "<item>"
3669 "<column name=\"pid\">%d</column>"
3670 "<column name=\"tid\">%s</column>"
3671 "</item>", pid, dp->d_name);
3672 }
3673 }
3674 }
3675 }
3676
3677 if (cores)
3678 {
3679 *cores = NULL;
3680 if (count > 0)
3681 {
3682 struct buffer buffer2;
3683 int *b;
3684 int *e;
3685 qsort (core_numbers, count, sizeof (int), compare_ints);
3686
3687 /* Remove duplicates. */
3688 b = core_numbers;
3689 e = unique (b, core_numbers + count);
3690
3691 buffer_init (&buffer2);
3692
3693 for (b = core_numbers; b != e; ++b)
3694 {
3695 char number[sizeof ("4294967295")];
3696 sprintf (number, "%u", *b);
3697 buffer_xml_printf (&buffer2, "%s%s",
3698 (b == core_numbers) ? "" : ",", number);
3699 }
3700 buffer_grow_str0 (&buffer2, "");
3701
3702 *cores = buffer_finish (&buffer2);
3703 }
3704 }
3705 free (core_numbers);
3706 }
3707
3708 static void
3709 show_process (int pid, const char *username, struct buffer *buffer)
3710 {
3711 char pathname[128];
3712 FILE *f;
3713 char cmd[MAXPATHLEN + 1];
3714
3715 sprintf (pathname, "/proc/%d/cmdline", pid);
3716
3717 if ((f = fopen (pathname, "r")) != NULL)
3718 {
3719 size_t len = fread (cmd, 1, sizeof (cmd) - 1, f);
3720 if (len > 0)
3721 {
3722 char *cores = 0;
3723 int i;
3724 for (i = 0; i < len; i++)
3725 if (cmd[i] == '\0')
3726 cmd[i] = ' ';
3727 cmd[len] = '\0';
3728
3729 buffer_xml_printf (buffer,
3730 "<item>"
3731 "<column name=\"pid\">%d</column>"
3732 "<column name=\"user\">%s</column>"
3733 "<column name=\"command\">%s</column>",
3734 pid,
3735 username,
3736 cmd);
3737
3738 /* This only collects core numbers, and does not print threads. */
3739 list_threads (pid, NULL, &cores);
3740
3741 if (cores)
3742 {
3743 buffer_xml_printf (buffer,
3744 "<column name=\"cores\">%s</column>", cores);
3745 free (cores);
3746 }
3747
3748 buffer_xml_printf (buffer, "</item>");
3749 }
3750 fclose (f);
3751 }
3752 }
3753
3754 static int
3755 linux_qxfer_osdata (const char *annex,
3756 unsigned char *readbuf, unsigned const char *writebuf,
3757 CORE_ADDR offset, int len)
3758 {
3759 /* We make the process list snapshot when the object starts to be
3760 read. */
3761 static const char *buf;
3762 static long len_avail = -1;
3763 static struct buffer buffer;
3764 int processes = 0;
3765 int threads = 0;
3766
3767 DIR *dirp;
3768
3769 if (strcmp (annex, "processes") == 0)
3770 processes = 1;
3771 else if (strcmp (annex, "threads") == 0)
3772 threads = 1;
3773 else
3774 return 0;
3775
3776 if (!readbuf || writebuf)
3777 return 0;
3778
3779 if (offset == 0)
3780 {
3781 if (len_avail != -1 && len_avail != 0)
3782 buffer_free (&buffer);
3783 len_avail = 0;
3784 buf = NULL;
3785 buffer_init (&buffer);
3786 if (processes)
3787 buffer_grow_str (&buffer, "<osdata type=\"processes\">");
3788 else if (threads)
3789 buffer_grow_str (&buffer, "<osdata type=\"threads\">");
3790
3791 dirp = opendir ("/proc");
3792 if (dirp)
3793 {
3794 struct dirent *dp;
3795 while ((dp = readdir (dirp)) != NULL)
3796 {
3797 struct stat statbuf;
3798 char procentry[sizeof ("/proc/4294967295")];
3799
3800 if (!isdigit (dp->d_name[0])
3801 || strlen (dp->d_name) > sizeof ("4294967295") - 1)
3802 continue;
3803
3804 sprintf (procentry, "/proc/%s", dp->d_name);
3805 if (stat (procentry, &statbuf) == 0
3806 && S_ISDIR (statbuf.st_mode))
3807 {
3808 int pid = (int) strtoul (dp->d_name, NULL, 10);
3809
3810 if (processes)
3811 {
3812 struct passwd *entry = getpwuid (statbuf.st_uid);
3813 show_process (pid, entry ? entry->pw_name : "?", &buffer);
3814 }
3815 else if (threads)
3816 {
3817 list_threads (pid, &buffer, NULL);
3818 }
3819 }
3820 }
3821
3822 closedir (dirp);
3823 }
3824 buffer_grow_str0 (&buffer, "</osdata>\n");
3825 buf = buffer_finish (&buffer);
3826 len_avail = strlen (buf);
3827 }
3828
3829 if (offset >= len_avail)
3830 {
3831 /* Done. Get rid of the data. */
3832 buffer_free (&buffer);
3833 buf = NULL;
3834 len_avail = 0;
3835 return 0;
3836 }
3837
3838 if (len > len_avail - offset)
3839 len = len_avail - offset;
3840 memcpy (readbuf, buf + offset, len);
3841
3842 return len;
3843 }
3844
3845 /* Convert a native/host siginfo object, into/from the siginfo in the
3846 layout of the inferiors' architecture. */
3847
3848 static void
3849 siginfo_fixup (struct siginfo *siginfo, void *inf_siginfo, int direction)
3850 {
3851 int done = 0;
3852
3853 if (the_low_target.siginfo_fixup != NULL)
3854 done = the_low_target.siginfo_fixup (siginfo, inf_siginfo, direction);
3855
3856 /* If there was no callback, or the callback didn't do anything,
3857 then just do a straight memcpy. */
3858 if (!done)
3859 {
3860 if (direction == 1)
3861 memcpy (siginfo, inf_siginfo, sizeof (struct siginfo));
3862 else
3863 memcpy (inf_siginfo, siginfo, sizeof (struct siginfo));
3864 }
3865 }
3866
3867 static int
3868 linux_xfer_siginfo (const char *annex, unsigned char *readbuf,
3869 unsigned const char *writebuf, CORE_ADDR offset, int len)
3870 {
3871 int pid;
3872 struct siginfo siginfo;
3873 char inf_siginfo[sizeof (struct siginfo)];
3874
3875 if (current_inferior == NULL)
3876 return -1;
3877
3878 pid = lwpid_of (get_thread_lwp (current_inferior));
3879
3880 if (debug_threads)
3881 fprintf (stderr, "%s siginfo for lwp %d.\n",
3882 readbuf != NULL ? "Reading" : "Writing",
3883 pid);
3884
3885 if (offset > sizeof (siginfo))
3886 return -1;
3887
3888 if (ptrace (PTRACE_GETSIGINFO, pid, 0, &siginfo) != 0)
3889 return -1;
3890
3891 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
3892 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3893 inferior with a 64-bit GDBSERVER should look the same as debugging it
3894 with a 32-bit GDBSERVER, we need to convert it. */
3895 siginfo_fixup (&siginfo, inf_siginfo, 0);
3896
3897 if (offset + len > sizeof (siginfo))
3898 len = sizeof (siginfo) - offset;
3899
3900 if (readbuf != NULL)
3901 memcpy (readbuf, inf_siginfo + offset, len);
3902 else
3903 {
3904 memcpy (inf_siginfo + offset, writebuf, len);
3905
3906 /* Convert back to ptrace layout before flushing it out. */
3907 siginfo_fixup (&siginfo, inf_siginfo, 1);
3908
3909 if (ptrace (PTRACE_SETSIGINFO, pid, 0, &siginfo) != 0)
3910 return -1;
3911 }
3912
3913 return len;
3914 }
3915
3916 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
3917 so we notice when children change state; as the handler for the
3918 sigsuspend in my_waitpid. */
3919
3920 static void
3921 sigchld_handler (int signo)
3922 {
3923 int old_errno = errno;
3924
3925 if (debug_threads)
3926 /* fprintf is not async-signal-safe, so call write directly. */
3927 write (2, "sigchld_handler\n", sizeof ("sigchld_handler\n") - 1);
3928
3929 if (target_is_async_p ())
3930 async_file_mark (); /* trigger a linux_wait */
3931
3932 errno = old_errno;
3933 }
3934
3935 static int
3936 linux_supports_non_stop (void)
3937 {
3938 return 1;
3939 }
3940
3941 static int
3942 linux_async (int enable)
3943 {
3944 int previous = (linux_event_pipe[0] != -1);
3945
3946 if (previous != enable)
3947 {
3948 sigset_t mask;
3949 sigemptyset (&mask);
3950 sigaddset (&mask, SIGCHLD);
3951
3952 sigprocmask (SIG_BLOCK, &mask, NULL);
3953
3954 if (enable)
3955 {
3956 if (pipe (linux_event_pipe) == -1)
3957 fatal ("creating event pipe failed.");
3958
3959 fcntl (linux_event_pipe[0], F_SETFL, O_NONBLOCK);
3960 fcntl (linux_event_pipe[1], F_SETFL, O_NONBLOCK);
3961
3962 /* Register the event loop handler. */
3963 add_file_handler (linux_event_pipe[0],
3964 handle_target_event, NULL);
3965
3966 /* Always trigger a linux_wait. */
3967 async_file_mark ();
3968 }
3969 else
3970 {
3971 delete_file_handler (linux_event_pipe[0]);
3972
3973 close (linux_event_pipe[0]);
3974 close (linux_event_pipe[1]);
3975 linux_event_pipe[0] = -1;
3976 linux_event_pipe[1] = -1;
3977 }
3978
3979 sigprocmask (SIG_UNBLOCK, &mask, NULL);
3980 }
3981
3982 return previous;
3983 }
3984
3985 static int
3986 linux_start_non_stop (int nonstop)
3987 {
3988 /* Register or unregister from event-loop accordingly. */
3989 linux_async (nonstop);
3990 return 0;
3991 }
3992
3993 static int
3994 linux_supports_multi_process (void)
3995 {
3996 return 1;
3997 }
3998
3999
4000 /* Enumerate spufs IDs for process PID. */
4001 static int
4002 spu_enumerate_spu_ids (long pid, unsigned char *buf, CORE_ADDR offset, int len)
4003 {
4004 int pos = 0;
4005 int written = 0;
4006 char path[128];
4007 DIR *dir;
4008 struct dirent *entry;
4009
4010 sprintf (path, "/proc/%ld/fd", pid);
4011 dir = opendir (path);
4012 if (!dir)
4013 return -1;
4014
4015 rewinddir (dir);
4016 while ((entry = readdir (dir)) != NULL)
4017 {
4018 struct stat st;
4019 struct statfs stfs;
4020 int fd;
4021
4022 fd = atoi (entry->d_name);
4023 if (!fd)
4024 continue;
4025
4026 sprintf (path, "/proc/%ld/fd/%d", pid, fd);
4027 if (stat (path, &st) != 0)
4028 continue;
4029 if (!S_ISDIR (st.st_mode))
4030 continue;
4031
4032 if (statfs (path, &stfs) != 0)
4033 continue;
4034 if (stfs.f_type != SPUFS_MAGIC)
4035 continue;
4036
4037 if (pos >= offset && pos + 4 <= offset + len)
4038 {
4039 *(unsigned int *)(buf + pos - offset) = fd;
4040 written += 4;
4041 }
4042 pos += 4;
4043 }
4044
4045 closedir (dir);
4046 return written;
4047 }
4048
4049 /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
4050 object type, using the /proc file system. */
4051 static int
4052 linux_qxfer_spu (const char *annex, unsigned char *readbuf,
4053 unsigned const char *writebuf,
4054 CORE_ADDR offset, int len)
4055 {
4056 long pid = lwpid_of (get_thread_lwp (current_inferior));
4057 char buf[128];
4058 int fd = 0;
4059 int ret = 0;
4060
4061 if (!writebuf && !readbuf)
4062 return -1;
4063
4064 if (!*annex)
4065 {
4066 if (!readbuf)
4067 return -1;
4068 else
4069 return spu_enumerate_spu_ids (pid, readbuf, offset, len);
4070 }
4071
4072 sprintf (buf, "/proc/%ld/fd/%s", pid, annex);
4073 fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
4074 if (fd <= 0)
4075 return -1;
4076
4077 if (offset != 0
4078 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
4079 {
4080 close (fd);
4081 return 0;
4082 }
4083
4084 if (writebuf)
4085 ret = write (fd, writebuf, (size_t) len);
4086 else
4087 ret = read (fd, readbuf, (size_t) len);
4088
4089 close (fd);
4090 return ret;
4091 }
4092
4093 static int
4094 linux_core_of_thread (ptid_t ptid)
4095 {
4096 char filename[sizeof ("/proc//task//stat")
4097 + 2 * 20 /* decimal digits for 2 numbers, max 2^64 bit each */
4098 + 1];
4099 FILE *f;
4100 char *content = NULL;
4101 char *p;
4102 char *ts = 0;
4103 int content_read = 0;
4104 int i;
4105 int core;
4106
4107 sprintf (filename, "/proc/%d/task/%ld/stat",
4108 ptid_get_pid (ptid), ptid_get_lwp (ptid));
4109 f = fopen (filename, "r");
4110 if (!f)
4111 return -1;
4112
4113 for (;;)
4114 {
4115 int n;
4116 content = realloc (content, content_read + 1024);
4117 n = fread (content + content_read, 1, 1024, f);
4118 content_read += n;
4119 if (n < 1024)
4120 {
4121 content[content_read] = '\0';
4122 break;
4123 }
4124 }
4125
4126 p = strchr (content, '(');
4127 p = strchr (p, ')') + 2; /* skip ")" and a whitespace. */
4128
4129 p = strtok_r (p, " ", &ts);
4130 for (i = 0; i != 36; ++i)
4131 p = strtok_r (NULL, " ", &ts);
4132
4133 if (sscanf (p, "%d", &core) == 0)
4134 core = -1;
4135
4136 free (content);
4137 fclose (f);
4138
4139 return core;
4140 }
4141
4142 static struct target_ops linux_target_ops = {
4143 linux_create_inferior,
4144 linux_attach,
4145 linux_kill,
4146 linux_detach,
4147 linux_join,
4148 linux_thread_alive,
4149 linux_resume,
4150 linux_wait,
4151 linux_fetch_registers,
4152 linux_store_registers,
4153 linux_read_memory,
4154 linux_write_memory,
4155 linux_look_up_symbols,
4156 linux_request_interrupt,
4157 linux_read_auxv,
4158 linux_insert_point,
4159 linux_remove_point,
4160 linux_stopped_by_watchpoint,
4161 linux_stopped_data_address,
4162 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4163 linux_read_offsets,
4164 #else
4165 NULL,
4166 #endif
4167 #ifdef USE_THREAD_DB
4168 thread_db_get_tls_address,
4169 #else
4170 NULL,
4171 #endif
4172 linux_qxfer_spu,
4173 hostio_last_error_from_errno,
4174 linux_qxfer_osdata,
4175 linux_xfer_siginfo,
4176 linux_supports_non_stop,
4177 linux_async,
4178 linux_start_non_stop,
4179 linux_supports_multi_process,
4180 #ifdef USE_THREAD_DB
4181 thread_db_handle_monitor_command,
4182 #else
4183 NULL,
4184 #endif
4185 linux_core_of_thread
4186 };
4187
4188 static void
4189 linux_init_signals ()
4190 {
4191 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
4192 to find what the cancel signal actually is. */
4193 #ifdef __SIGRTMIN /* Bionic doesn't use SIGRTMIN the way glibc does. */
4194 signal (__SIGRTMIN+1, SIG_IGN);
4195 #endif
4196 }
4197
4198 void
4199 initialize_low (void)
4200 {
4201 struct sigaction sigchld_action;
4202 memset (&sigchld_action, 0, sizeof (sigchld_action));
4203 set_target_ops (&linux_target_ops);
4204 set_breakpoint_data (the_low_target.breakpoint,
4205 the_low_target.breakpoint_len);
4206 linux_init_signals ();
4207 linux_test_for_tracefork ();
4208 #ifdef HAVE_LINUX_REGSETS
4209 for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++)
4210 ;
4211 disabled_regsets = xmalloc (num_regsets);
4212 #endif
4213
4214 sigchld_action.sa_handler = sigchld_handler;
4215 sigemptyset (&sigchld_action.sa_mask);
4216 sigchld_action.sa_flags = SA_RESTART;
4217 sigaction (SIGCHLD, &sigchld_action, NULL);
4218 }
This page took 0.132454 seconds and 4 git commands to generate.