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