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