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, 2011 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "linux-low.h"
22 #include "linux-osdata.h"
26 #include <sys/param.h>
27 #include <sys/ptrace.h>
28 #include "linux-ptrace.h"
29 #include "linux-procfs.h"
31 #include <sys/ioctl.h>
37 #include <sys/syscall.h>
41 #include <sys/types.h>
47 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
48 then ELFMAG0 will have been defined. If it didn't get included by
49 gdb_proc_service.h then including it will likely introduce a duplicate
50 definition of elf_fpregset_t. */
55 #define SPUFS_MAGIC 0x23c9b64e
63 #define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
66 /* This is the kernel's hard limit. Not to be confused with
73 #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
78 /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
79 representation of the thread ID.
81 ``all_lwps'' is keyed by the process ID - which on Linux is (presently)
82 the same as the LWP ID.
84 ``all_processes'' is keyed by the "overall process ID", which
85 GNU/Linux calls tgid, "thread group ID". */
87 struct inferior_list all_lwps
;
89 /* A list of all unknown processes which receive stop signals. Some other
90 process will presumably claim each of these as forked children
93 struct inferior_list stopped_pids
;
95 /* FIXME this is a bit of a hack, and could be removed. */
98 /* FIXME make into a target method? */
99 int using_threads
= 1;
101 /* True if we're presently stabilizing threads (moving them out of
103 static int stabilizing_threads
;
105 /* This flag is true iff we've just created or attached to our first
106 inferior but it has not stopped yet. As soon as it does, we need
107 to call the low target's arch_setup callback. Doing this only on
108 the first inferior avoids reinializing the architecture on every
109 inferior, and avoids messing with the register caches of the
110 already running inferiors. NOTE: this assumes all inferiors under
111 control of gdbserver have the same architecture. */
112 static int new_inferior
;
114 static void linux_resume_one_lwp (struct lwp_info
*lwp
,
115 int step
, int signal
, siginfo_t
*info
);
116 static void linux_resume (struct thread_resume
*resume_info
, size_t n
);
117 static void stop_all_lwps (int suspend
, struct lwp_info
*except
);
118 static void unstop_all_lwps (int unsuspend
, struct lwp_info
*except
);
119 static int linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
);
120 static void *add_lwp (ptid_t ptid
);
121 static int linux_stopped_by_watchpoint (void);
122 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
123 static void proceed_all_lwps (void);
124 static int finish_step_over (struct lwp_info
*lwp
);
125 static CORE_ADDR
get_stop_pc (struct lwp_info
*lwp
);
126 static int kill_lwp (unsigned long lwpid
, int signo
);
127 static void linux_enable_event_reporting (int pid
);
129 /* True if the low target can hardware single-step. Such targets
130 don't need a BREAKPOINT_REINSERT_ADDR callback. */
133 can_hardware_single_step (void)
135 return (the_low_target
.breakpoint_reinsert_addr
== NULL
);
138 /* True if the low target supports memory breakpoints. If so, we'll
139 have a GET_PC implementation. */
142 supports_breakpoints (void)
144 return (the_low_target
.get_pc
!= NULL
);
147 /* Returns true if this target can support fast tracepoints. This
148 does not mean that the in-process agent has been loaded in the
152 supports_fast_tracepoints (void)
154 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
157 struct pending_signals
161 struct pending_signals
*prev
;
164 #define PTRACE_ARG3_TYPE void *
165 #define PTRACE_ARG4_TYPE void *
166 #define PTRACE_XFER_TYPE long
168 #ifdef HAVE_LINUX_REGSETS
169 static char *disabled_regsets
;
170 static int num_regsets
;
173 /* The read/write ends of the pipe registered as waitable file in the
175 static int linux_event_pipe
[2] = { -1, -1 };
177 /* True if we're currently in async mode. */
178 #define target_is_async_p() (linux_event_pipe[0] != -1)
180 static void send_sigstop (struct lwp_info
*lwp
);
181 static void wait_for_sigstop (struct inferior_list_entry
*entry
);
183 /* Accepts an integer PID; Returns a string representing a file that
184 can be opened to get info for the child process.
185 Space for the result is malloc'd, caller must free. */
188 linux_child_pid_to_exec_file (int pid
)
192 name1
= xmalloc (MAXPATHLEN
);
193 name2
= xmalloc (MAXPATHLEN
);
194 memset (name2
, 0, MAXPATHLEN
);
196 sprintf (name1
, "/proc/%d/exe", pid
);
197 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
209 /* Return non-zero if HEADER is a 64-bit ELF file. */
212 elf_64_header_p (const Elf64_Ehdr
*header
)
214 return (header
->e_ident
[EI_MAG0
] == ELFMAG0
215 && header
->e_ident
[EI_MAG1
] == ELFMAG1
216 && header
->e_ident
[EI_MAG2
] == ELFMAG2
217 && header
->e_ident
[EI_MAG3
] == ELFMAG3
218 && header
->e_ident
[EI_CLASS
] == ELFCLASS64
);
221 /* Return non-zero if FILE is a 64-bit ELF file,
222 zero if the file is not a 64-bit ELF file,
223 and -1 if the file is not accessible or doesn't exist. */
226 elf_64_file_p (const char *file
)
231 fd
= open (file
, O_RDONLY
);
235 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
242 return elf_64_header_p (&header
);
246 delete_lwp (struct lwp_info
*lwp
)
248 remove_thread (get_lwp_thread (lwp
));
249 remove_inferior (&all_lwps
, &lwp
->head
);
250 free (lwp
->arch_private
);
254 /* Add a process to the common process list, and set its private
257 static struct process_info
*
258 linux_add_process (int pid
, int attached
)
260 struct process_info
*proc
;
262 /* Is this the first process? If so, then set the arch. */
263 if (all_processes
.head
== NULL
)
266 proc
= add_process (pid
, attached
);
267 proc
->private = xcalloc (1, sizeof (*proc
->private));
269 if (the_low_target
.new_process
!= NULL
)
270 proc
->private->arch_private
= the_low_target
.new_process ();
275 /* Wrapper function for waitpid which handles EINTR, and emulates
276 __WALL for systems where that is not available. */
279 my_waitpid (int pid
, int *status
, int flags
)
284 fprintf (stderr
, "my_waitpid (%d, 0x%x)\n", pid
, flags
);
288 sigset_t block_mask
, org_mask
, wake_mask
;
291 wnohang
= (flags
& WNOHANG
) != 0;
292 flags
&= ~(__WALL
| __WCLONE
);
295 /* Block all signals while here. This avoids knowing about
296 LinuxThread's signals. */
297 sigfillset (&block_mask
);
298 sigprocmask (SIG_BLOCK
, &block_mask
, &org_mask
);
300 /* ... except during the sigsuspend below. */
301 sigemptyset (&wake_mask
);
305 /* Since all signals are blocked, there's no need to check
307 ret
= waitpid (pid
, status
, flags
);
310 if (ret
== -1 && out_errno
!= ECHILD
)
315 if (flags
& __WCLONE
)
317 /* We've tried both flavors now. If WNOHANG is set,
318 there's nothing else to do, just bail out. */
323 fprintf (stderr
, "blocking\n");
325 /* Block waiting for signals. */
326 sigsuspend (&wake_mask
);
332 sigprocmask (SIG_SETMASK
, &org_mask
, NULL
);
337 ret
= waitpid (pid
, status
, flags
);
338 while (ret
== -1 && errno
== EINTR
);
343 fprintf (stderr
, "my_waitpid (%d, 0x%x): status(%x), %d\n",
344 pid
, flags
, status
? *status
: -1, ret
);
350 /* Handle a GNU/Linux extended wait response. If we see a clone
351 event, we need to add the new LWP to our list (and not report the
352 trap to higher layers). */
355 handle_extended_wait (struct lwp_info
*event_child
, int wstat
)
357 int event
= wstat
>> 16;
358 struct lwp_info
*new_lwp
;
360 if (event
== PTRACE_EVENT_CLONE
)
363 unsigned long new_pid
;
364 int ret
, status
= W_STOPCODE (SIGSTOP
);
366 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_child
), 0, &new_pid
);
368 /* If we haven't already seen the new PID stop, wait for it now. */
369 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
371 /* The new child has a pending SIGSTOP. We can't affect it until it
372 hits the SIGSTOP, but we're already attached. */
374 ret
= my_waitpid (new_pid
, &status
, __WALL
);
377 perror_with_name ("waiting for new child");
378 else if (ret
!= new_pid
)
379 warning ("wait returned unexpected PID %d", ret
);
380 else if (!WIFSTOPPED (status
))
381 warning ("wait returned unexpected status 0x%x", status
);
384 linux_enable_event_reporting (new_pid
);
386 ptid
= ptid_build (pid_of (event_child
), new_pid
, 0);
387 new_lwp
= (struct lwp_info
*) add_lwp (ptid
);
388 add_thread (ptid
, new_lwp
);
390 /* Either we're going to immediately resume the new thread
391 or leave it stopped. linux_resume_one_lwp is a nop if it
392 thinks the thread is currently running, so set this first
393 before calling linux_resume_one_lwp. */
394 new_lwp
->stopped
= 1;
396 /* Normally we will get the pending SIGSTOP. But in some cases
397 we might get another signal delivered to the group first.
398 If we do get another signal, be sure not to lose it. */
399 if (WSTOPSIG (status
) == SIGSTOP
)
401 if (stopping_threads
)
402 new_lwp
->stop_pc
= get_stop_pc (new_lwp
);
404 linux_resume_one_lwp (new_lwp
, 0, 0, NULL
);
408 new_lwp
->stop_expected
= 1;
410 if (stopping_threads
)
412 new_lwp
->stop_pc
= get_stop_pc (new_lwp
);
413 new_lwp
->status_pending_p
= 1;
414 new_lwp
->status_pending
= status
;
417 /* Pass the signal on. This is what GDB does - except
418 shouldn't we really report it instead? */
419 linux_resume_one_lwp (new_lwp
, 0, WSTOPSIG (status
), NULL
);
422 /* Always resume the current thread. If we are stopping
423 threads, it will have a pending SIGSTOP; we may as well
425 linux_resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
429 /* Return the PC as read from the regcache of LWP, without any
433 get_pc (struct lwp_info
*lwp
)
435 struct thread_info
*saved_inferior
;
436 struct regcache
*regcache
;
439 if (the_low_target
.get_pc
== NULL
)
442 saved_inferior
= current_inferior
;
443 current_inferior
= get_lwp_thread (lwp
);
445 regcache
= get_thread_regcache (current_inferior
, 1);
446 pc
= (*the_low_target
.get_pc
) (regcache
);
449 fprintf (stderr
, "pc is 0x%lx\n", (long) pc
);
451 current_inferior
= saved_inferior
;
455 /* This function should only be called if LWP got a SIGTRAP.
456 The SIGTRAP could mean several things.
458 On i386, where decr_pc_after_break is non-zero:
459 If we were single-stepping this process using PTRACE_SINGLESTEP,
460 we will get only the one SIGTRAP (even if the instruction we
461 stepped over was a breakpoint). The value of $eip will be the
463 If we continue the process using PTRACE_CONT, we will get a
464 SIGTRAP when we hit a breakpoint. The value of $eip will be
465 the instruction after the breakpoint (i.e. needs to be
466 decremented). If we report the SIGTRAP to GDB, we must also
467 report the undecremented PC. If we cancel the SIGTRAP, we
468 must resume at the decremented PC.
470 (Presumably, not yet tested) On a non-decr_pc_after_break machine
471 with hardware or kernel single-step:
472 If we single-step over a breakpoint instruction, our PC will
473 point at the following instruction. If we continue and hit a
474 breakpoint instruction, our PC will point at the breakpoint
478 get_stop_pc (struct lwp_info
*lwp
)
482 if (the_low_target
.get_pc
== NULL
)
485 stop_pc
= get_pc (lwp
);
487 if (WSTOPSIG (lwp
->last_status
) == SIGTRAP
489 && !lwp
->stopped_by_watchpoint
490 && lwp
->last_status
>> 16 == 0)
491 stop_pc
-= the_low_target
.decr_pc_after_break
;
494 fprintf (stderr
, "stop pc is 0x%lx\n", (long) stop_pc
);
500 add_lwp (ptid_t ptid
)
502 struct lwp_info
*lwp
;
504 lwp
= (struct lwp_info
*) xmalloc (sizeof (*lwp
));
505 memset (lwp
, 0, sizeof (*lwp
));
509 if (the_low_target
.new_thread
!= NULL
)
510 lwp
->arch_private
= the_low_target
.new_thread ();
512 add_inferior_to_list (&all_lwps
, &lwp
->head
);
517 /* Start an inferior process and returns its pid.
518 ALLARGS is a vector of program-name and args. */
521 linux_create_inferior (char *program
, char **allargs
)
523 struct lwp_info
*new_lwp
;
527 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
533 perror_with_name ("fork");
537 ptrace (PTRACE_TRACEME
, 0, 0, 0);
539 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
540 signal (__SIGRTMIN
+ 1, SIG_DFL
);
545 execv (program
, allargs
);
547 execvp (program
, allargs
);
549 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
555 linux_add_process (pid
, 0);
557 ptid
= ptid_build (pid
, pid
, 0);
558 new_lwp
= add_lwp (ptid
);
559 add_thread (ptid
, new_lwp
);
560 new_lwp
->must_set_ptrace_flags
= 1;
565 /* Attach to an inferior process. */
568 linux_attach_lwp_1 (unsigned long lwpid
, int initial
)
571 struct lwp_info
*new_lwp
;
573 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) != 0)
577 /* If we fail to attach to an LWP, just warn. */
578 fprintf (stderr
, "Cannot attach to lwp %ld: %s (%d)\n", lwpid
,
579 strerror (errno
), errno
);
584 /* If we fail to attach to a process, report an error. */
585 error ("Cannot attach to lwp %ld: %s (%d)\n", lwpid
,
586 strerror (errno
), errno
);
590 /* If lwp is the tgid, we handle adding existing threads later.
591 Otherwise we just add lwp without bothering about any other
593 ptid
= ptid_build (lwpid
, lwpid
, 0);
596 /* Note that extracting the pid from the current inferior is
597 safe, since we're always called in the context of the same
598 process as this new thread. */
599 int pid
= pid_of (get_thread_lwp (current_inferior
));
600 ptid
= ptid_build (pid
, lwpid
, 0);
603 new_lwp
= (struct lwp_info
*) add_lwp (ptid
);
604 add_thread (ptid
, new_lwp
);
606 /* We need to wait for SIGSTOP before being able to make the next
607 ptrace call on this LWP. */
608 new_lwp
->must_set_ptrace_flags
= 1;
610 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
613 There are several cases to consider here:
615 1) gdbserver has already attached to the process and is being notified
616 of a new thread that is being created.
617 In this case we should ignore that SIGSTOP and resume the
618 process. This is handled below by setting stop_expected = 1,
619 and the fact that add_thread sets last_resume_kind ==
622 2) This is the first thread (the process thread), and we're attaching
623 to it via attach_inferior.
624 In this case we want the process thread to stop.
625 This is handled by having linux_attach set last_resume_kind ==
626 resume_stop after we return.
628 If the pid we are attaching to is also the tgid, we attach to and
629 stop all the existing threads. Otherwise, we attach to pid and
630 ignore any other threads in the same group as this pid.
632 3) GDB is connecting to gdbserver and is requesting an enumeration of all
634 In this case we want the thread to stop.
635 FIXME: This case is currently not properly handled.
636 We should wait for the SIGSTOP but don't. Things work apparently
637 because enough time passes between when we ptrace (ATTACH) and when
638 gdb makes the next ptrace call on the thread.
640 On the other hand, if we are currently trying to stop all threads, we
641 should treat the new thread as if we had sent it a SIGSTOP. This works
642 because we are guaranteed that the add_lwp call above added us to the
643 end of the list, and so the new thread has not yet reached
644 wait_for_sigstop (but will). */
645 new_lwp
->stop_expected
= 1;
649 linux_attach_lwp (unsigned long lwpid
)
651 linux_attach_lwp_1 (lwpid
, 0);
654 /* Attach to PID. If PID is the tgid, attach to it and all
658 linux_attach (unsigned long pid
)
660 /* Attach to PID. We will check for other threads
662 linux_attach_lwp_1 (pid
, 1);
663 linux_add_process (pid
, 1);
667 struct thread_info
*thread
;
669 /* Don't ignore the initial SIGSTOP if we just attached to this
670 process. It will be collected by wait shortly. */
671 thread
= find_thread_ptid (ptid_build (pid
, pid
, 0));
672 thread
->last_resume_kind
= resume_stop
;
675 if (linux_proc_get_tgid (pid
) == pid
)
680 sprintf (pathname
, "/proc/%ld/task", pid
);
682 dir
= opendir (pathname
);
686 fprintf (stderr
, "Could not open /proc/%ld/task.\n", pid
);
691 /* At this point we attached to the tgid. Scan the task for
694 int new_threads_found
;
698 while (iterations
< 2)
700 new_threads_found
= 0;
701 /* Add all the other threads. While we go through the
702 threads, new threads may be spawned. Cycle through
703 the list of threads until we have done two iterations without
704 finding new threads. */
705 while ((dp
= readdir (dir
)) != NULL
)
708 lwp
= strtoul (dp
->d_name
, NULL
, 10);
710 /* Is this a new thread? */
712 && find_thread_ptid (ptid_build (pid
, lwp
, 0)) == NULL
)
714 linux_attach_lwp_1 (lwp
, 0);
719 Found and attached to new lwp %ld\n", lwp
);
723 if (!new_threads_found
)
744 second_thread_of_pid_p (struct inferior_list_entry
*entry
, void *args
)
746 struct counter
*counter
= args
;
748 if (ptid_get_pid (entry
->id
) == counter
->pid
)
750 if (++counter
->count
> 1)
758 last_thread_of_process_p (struct thread_info
*thread
)
760 ptid_t ptid
= ((struct inferior_list_entry
*)thread
)->id
;
761 int pid
= ptid_get_pid (ptid
);
762 struct counter counter
= { pid
, 0 };
764 return (find_inferior (&all_threads
,
765 second_thread_of_pid_p
, &counter
) == NULL
);
768 /* Kill the inferior lwp. */
771 linux_kill_one_lwp (struct inferior_list_entry
*entry
, void *args
)
773 struct thread_info
*thread
= (struct thread_info
*) entry
;
774 struct lwp_info
*lwp
= get_thread_lwp (thread
);
776 int pid
= * (int *) args
;
778 if (ptid_get_pid (entry
->id
) != pid
)
781 /* We avoid killing the first thread here, because of a Linux kernel (at
782 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
783 the children get a chance to be reaped, it will remain a zombie
786 if (lwpid_of (lwp
) == pid
)
789 fprintf (stderr
, "lkop: is last of process %s\n",
790 target_pid_to_str (entry
->id
));
796 ptrace (PTRACE_KILL
, lwpid_of (lwp
), 0, 0);
798 /* Make sure it died. The loop is most likely unnecessary. */
799 pid
= linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
800 } while (pid
> 0 && WIFSTOPPED (wstat
));
808 struct process_info
*process
;
809 struct lwp_info
*lwp
;
813 process
= find_process_pid (pid
);
817 /* If we're killing a running inferior, make sure it is stopped
818 first, as PTRACE_KILL will not work otherwise. */
819 stop_all_lwps (0, NULL
);
821 find_inferior (&all_threads
, linux_kill_one_lwp
, &pid
);
823 /* See the comment in linux_kill_one_lwp. We did not kill the first
824 thread in the list, so do so now. */
825 lwp
= find_lwp_pid (pid_to_ptid (pid
));
828 fprintf (stderr
, "lk_1: killing lwp %ld, for pid: %d\n",
829 lwpid_of (lwp
), pid
);
833 ptrace (PTRACE_KILL
, lwpid_of (lwp
), 0, 0);
835 /* Make sure it died. The loop is most likely unnecessary. */
836 lwpid
= linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
837 } while (lwpid
> 0 && WIFSTOPPED (wstat
));
839 the_target
->mourn (process
);
841 /* Since we presently can only stop all lwps of all processes, we
842 need to unstop lwps of other processes. */
843 unstop_all_lwps (0, NULL
);
848 linux_detach_one_lwp (struct inferior_list_entry
*entry
, void *args
)
850 struct thread_info
*thread
= (struct thread_info
*) entry
;
851 struct lwp_info
*lwp
= get_thread_lwp (thread
);
852 int pid
= * (int *) args
;
854 if (ptid_get_pid (entry
->id
) != pid
)
857 /* If this process is stopped but is expecting a SIGSTOP, then make
858 sure we take care of that now. This isn't absolutely guaranteed
859 to collect the SIGSTOP, but is fairly likely to. */
860 if (lwp
->stop_expected
)
863 /* Clear stop_expected, so that the SIGSTOP will be reported. */
864 lwp
->stop_expected
= 0;
865 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
866 linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
869 /* Flush any pending changes to the process's registers. */
870 regcache_invalidate_one ((struct inferior_list_entry
*)
871 get_lwp_thread (lwp
));
873 /* Finally, let it resume. */
874 ptrace (PTRACE_DETACH
, lwpid_of (lwp
), 0, 0);
881 linux_detach (int pid
)
883 struct process_info
*process
;
885 process
= find_process_pid (pid
);
889 /* Stop all threads before detaching. First, ptrace requires that
890 the thread is stopped to sucessfully detach. Second, thread_db
891 may need to uninstall thread event breakpoints from memory, which
892 only works with a stopped process anyway. */
893 stop_all_lwps (0, NULL
);
896 thread_db_detach (process
);
899 /* Stabilize threads (move out of jump pads). */
900 stabilize_threads ();
902 find_inferior (&all_threads
, linux_detach_one_lwp
, &pid
);
904 the_target
->mourn (process
);
906 /* Since we presently can only stop all lwps of all processes, we
907 need to unstop lwps of other processes. */
908 unstop_all_lwps (0, NULL
);
912 /* Remove all LWPs that belong to process PROC from the lwp list. */
915 delete_lwp_callback (struct inferior_list_entry
*entry
, void *proc
)
917 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
918 struct process_info
*process
= proc
;
920 if (pid_of (lwp
) == pid_of (process
))
927 linux_mourn (struct process_info
*process
)
929 struct process_info_private
*priv
;
932 thread_db_mourn (process
);
935 find_inferior (&all_lwps
, delete_lwp_callback
, process
);
937 /* Freeing all private data. */
938 priv
= process
->private;
939 free (priv
->arch_private
);
941 process
->private = NULL
;
943 remove_process (process
);
952 ret
= my_waitpid (pid
, &status
, 0);
953 if (WIFEXITED (status
) || WIFSIGNALED (status
))
955 } while (ret
!= -1 || errno
!= ECHILD
);
958 /* Return nonzero if the given thread is still alive. */
960 linux_thread_alive (ptid_t ptid
)
962 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
964 /* We assume we always know if a thread exits. If a whole process
965 exited but we still haven't been able to report it to GDB, we'll
966 hold on to the last lwp of the dead process. */
973 /* Return 1 if this lwp has an interesting status pending. */
975 status_pending_p_callback (struct inferior_list_entry
*entry
, void *arg
)
977 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
978 ptid_t ptid
= * (ptid_t
*) arg
;
979 struct thread_info
*thread
;
981 /* Check if we're only interested in events from a specific process
983 if (!ptid_equal (minus_one_ptid
, ptid
)
984 && ptid_get_pid (ptid
) != ptid_get_pid (lwp
->head
.id
))
987 thread
= get_lwp_thread (lwp
);
989 /* If we got a `vCont;t', but we haven't reported a stop yet, do
990 report any status pending the LWP may have. */
991 if (thread
->last_resume_kind
== resume_stop
992 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
995 return lwp
->status_pending_p
;
999 same_lwp (struct inferior_list_entry
*entry
, void *data
)
1001 ptid_t ptid
= *(ptid_t
*) data
;
1004 if (ptid_get_lwp (ptid
) != 0)
1005 lwp
= ptid_get_lwp (ptid
);
1007 lwp
= ptid_get_pid (ptid
);
1009 if (ptid_get_lwp (entry
->id
) == lwp
)
1016 find_lwp_pid (ptid_t ptid
)
1018 return (struct lwp_info
*) find_inferior (&all_lwps
, same_lwp
, &ptid
);
1021 static struct lwp_info
*
1022 linux_wait_for_lwp (ptid_t ptid
, int *wstatp
, int options
)
1025 int to_wait_for
= -1;
1026 struct lwp_info
*child
= NULL
;
1029 fprintf (stderr
, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid
));
1031 if (ptid_equal (ptid
, minus_one_ptid
))
1032 to_wait_for
= -1; /* any child */
1034 to_wait_for
= ptid_get_lwp (ptid
); /* this lwp only */
1040 ret
= my_waitpid (to_wait_for
, wstatp
, options
);
1041 if (ret
== 0 || (ret
== -1 && errno
== ECHILD
&& (options
& WNOHANG
)))
1044 perror_with_name ("waitpid");
1047 && (!WIFSTOPPED (*wstatp
)
1048 || (WSTOPSIG (*wstatp
) != 32
1049 && WSTOPSIG (*wstatp
) != 33)))
1050 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
1052 child
= find_lwp_pid (pid_to_ptid (ret
));
1054 /* If we didn't find a process, one of two things presumably happened:
1055 - A process we started and then detached from has exited. Ignore it.
1056 - A process we are controlling has forked and the new child's stop
1057 was reported to us by the kernel. Save its PID. */
1058 if (child
== NULL
&& WIFSTOPPED (*wstatp
))
1060 add_pid_to_list (&stopped_pids
, ret
);
1063 else if (child
== NULL
)
1068 child
->last_status
= *wstatp
;
1070 /* Architecture-specific setup after inferior is running.
1071 This needs to happen after we have attached to the inferior
1072 and it is stopped for the first time, but before we access
1073 any inferior registers. */
1076 the_low_target
.arch_setup ();
1077 #ifdef HAVE_LINUX_REGSETS
1078 memset (disabled_regsets
, 0, num_regsets
);
1083 /* Fetch the possibly triggered data watchpoint info and store it in
1086 On some archs, like x86, that use debug registers to set
1087 watchpoints, it's possible that the way to know which watched
1088 address trapped, is to check the register that is used to select
1089 which address to watch. Problem is, between setting the
1090 watchpoint and reading back which data address trapped, the user
1091 may change the set of watchpoints, and, as a consequence, GDB
1092 changes the debug registers in the inferior. To avoid reading
1093 back a stale stopped-data-address when that happens, we cache in
1094 LP the fact that a watchpoint trapped, and the corresponding data
1095 address, as soon as we see CHILD stop with a SIGTRAP. If GDB
1096 changes the debug registers meanwhile, we have the cached data we
1099 if (WIFSTOPPED (*wstatp
) && WSTOPSIG (*wstatp
) == SIGTRAP
)
1101 if (the_low_target
.stopped_by_watchpoint
== NULL
)
1103 child
->stopped_by_watchpoint
= 0;
1107 struct thread_info
*saved_inferior
;
1109 saved_inferior
= current_inferior
;
1110 current_inferior
= get_lwp_thread (child
);
1112 child
->stopped_by_watchpoint
1113 = the_low_target
.stopped_by_watchpoint ();
1115 if (child
->stopped_by_watchpoint
)
1117 if (the_low_target
.stopped_data_address
!= NULL
)
1118 child
->stopped_data_address
1119 = the_low_target
.stopped_data_address ();
1121 child
->stopped_data_address
= 0;
1124 current_inferior
= saved_inferior
;
1128 /* Store the STOP_PC, with adjustment applied. This depends on the
1129 architecture being defined already (so that CHILD has a valid
1130 regcache), and on LAST_STATUS being set (to check for SIGTRAP or
1132 if (WIFSTOPPED (*wstatp
))
1133 child
->stop_pc
= get_stop_pc (child
);
1136 && WIFSTOPPED (*wstatp
)
1137 && the_low_target
.get_pc
!= NULL
)
1139 struct thread_info
*saved_inferior
= current_inferior
;
1140 struct regcache
*regcache
;
1143 current_inferior
= get_lwp_thread (child
);
1144 regcache
= get_thread_regcache (current_inferior
, 1);
1145 pc
= (*the_low_target
.get_pc
) (regcache
);
1146 fprintf (stderr
, "linux_wait_for_lwp: pc is 0x%lx\n", (long) pc
);
1147 current_inferior
= saved_inferior
;
1153 /* This function should only be called if the LWP got a SIGTRAP.
1155 Handle any tracepoint steps or hits. Return true if a tracepoint
1156 event was handled, 0 otherwise. */
1159 handle_tracepoints (struct lwp_info
*lwp
)
1161 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1162 int tpoint_related_event
= 0;
1164 /* If this tracepoint hit causes a tracing stop, we'll immediately
1165 uninsert tracepoints. To do this, we temporarily pause all
1166 threads, unpatch away, and then unpause threads. We need to make
1167 sure the unpausing doesn't resume LWP too. */
1170 /* And we need to be sure that any all-threads-stopping doesn't try
1171 to move threads out of the jump pads, as it could deadlock the
1172 inferior (LWP could be in the jump pad, maybe even holding the
1175 /* Do any necessary step collect actions. */
1176 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1178 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1180 /* See if we just hit a tracepoint and do its main collect
1182 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1186 gdb_assert (lwp
->suspended
== 0);
1187 gdb_assert (!stabilizing_threads
|| lwp
->collecting_fast_tracepoint
);
1189 if (tpoint_related_event
)
1192 fprintf (stderr
, "got a tracepoint event\n");
1199 /* Convenience wrapper. Returns true if LWP is presently collecting a
1203 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
1204 struct fast_tpoint_collect_status
*status
)
1206 CORE_ADDR thread_area
;
1208 if (the_low_target
.get_thread_area
== NULL
)
1211 /* Get the thread area address. This is used to recognize which
1212 thread is which when tracing with the in-process agent library.
1213 We don't read anything from the address, and treat it as opaque;
1214 it's the address itself that we assume is unique per-thread. */
1215 if ((*the_low_target
.get_thread_area
) (lwpid_of (lwp
), &thread_area
) == -1)
1218 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
1221 /* The reason we resume in the caller, is because we want to be able
1222 to pass lwp->status_pending as WSTAT, and we need to clear
1223 status_pending_p before resuming, otherwise, linux_resume_one_lwp
1224 refuses to resume. */
1227 maybe_move_out_of_jump_pad (struct lwp_info
*lwp
, int *wstat
)
1229 struct thread_info
*saved_inferior
;
1231 saved_inferior
= current_inferior
;
1232 current_inferior
= get_lwp_thread (lwp
);
1235 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
1236 && supports_fast_tracepoints ()
1237 && in_process_agent_loaded ())
1239 struct fast_tpoint_collect_status status
;
1244 Checking whether LWP %ld needs to move out of the jump pad.\n",
1247 r
= linux_fast_tracepoint_collecting (lwp
, &status
);
1250 || (WSTOPSIG (*wstat
) != SIGILL
1251 && WSTOPSIG (*wstat
) != SIGFPE
1252 && WSTOPSIG (*wstat
) != SIGSEGV
1253 && WSTOPSIG (*wstat
) != SIGBUS
))
1255 lwp
->collecting_fast_tracepoint
= r
;
1259 if (r
== 1 && lwp
->exit_jump_pad_bkpt
== NULL
)
1261 /* Haven't executed the original instruction yet.
1262 Set breakpoint there, and wait till it's hit,
1263 then single-step until exiting the jump pad. */
1264 lwp
->exit_jump_pad_bkpt
1265 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
1270 Checking whether LWP %ld needs to move out of the jump pad...it does\n",
1272 current_inferior
= saved_inferior
;
1279 /* If we get a synchronous signal while collecting, *and*
1280 while executing the (relocated) original instruction,
1281 reset the PC to point at the tpoint address, before
1282 reporting to GDB. Otherwise, it's an IPA lib bug: just
1283 report the signal to GDB, and pray for the best. */
1285 lwp
->collecting_fast_tracepoint
= 0;
1288 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
1289 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
1292 struct regcache
*regcache
;
1294 /* The si_addr on a few signals references the address
1295 of the faulting instruction. Adjust that as
1297 if ((WSTOPSIG (*wstat
) == SIGILL
1298 || WSTOPSIG (*wstat
) == SIGFPE
1299 || WSTOPSIG (*wstat
) == SIGBUS
1300 || WSTOPSIG (*wstat
) == SIGSEGV
)
1301 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
), 0, &info
) == 0
1302 /* Final check just to make sure we don't clobber
1303 the siginfo of non-kernel-sent signals. */
1304 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
1306 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
1307 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
), 0, &info
);
1310 regcache
= get_thread_regcache (get_lwp_thread (lwp
), 1);
1311 (*the_low_target
.set_pc
) (regcache
, status
.tpoint_addr
);
1312 lwp
->stop_pc
= status
.tpoint_addr
;
1314 /* Cancel any fast tracepoint lock this thread was
1316 force_unlock_trace_buffer ();
1319 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
1323 "Cancelling fast exit-jump-pad: removing bkpt. "
1324 "stopping all threads momentarily.\n");
1326 stop_all_lwps (1, lwp
);
1327 cancel_breakpoints ();
1329 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
1330 lwp
->exit_jump_pad_bkpt
= NULL
;
1332 unstop_all_lwps (1, lwp
);
1334 gdb_assert (lwp
->suspended
>= 0);
1341 Checking whether LWP %ld needs to move out of the jump pad...no\n",
1344 current_inferior
= saved_inferior
;
1348 /* Enqueue one signal in the "signals to report later when out of the
1352 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1354 struct pending_signals
*p_sig
;
1358 Deferring signal %d for LWP %ld.\n", WSTOPSIG (*wstat
), lwpid_of (lwp
));
1362 struct pending_signals
*sig
;
1364 for (sig
= lwp
->pending_signals_to_report
;
1368 " Already queued %d\n",
1371 fprintf (stderr
, " (no more currently queued signals)\n");
1374 /* Don't enqueue non-RT signals if they are already in the deferred
1375 queue. (SIGSTOP being the easiest signal to see ending up here
1377 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
1379 struct pending_signals
*sig
;
1381 for (sig
= lwp
->pending_signals_to_report
;
1385 if (sig
->signal
== WSTOPSIG (*wstat
))
1389 "Not requeuing already queued non-RT signal %d"
1398 p_sig
= xmalloc (sizeof (*p_sig
));
1399 p_sig
->prev
= lwp
->pending_signals_to_report
;
1400 p_sig
->signal
= WSTOPSIG (*wstat
);
1401 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1402 ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
), 0, &p_sig
->info
);
1404 lwp
->pending_signals_to_report
= p_sig
;
1407 /* Dequeue one signal from the "signals to report later when out of
1408 the jump pad" list. */
1411 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1413 if (lwp
->pending_signals_to_report
!= NULL
)
1415 struct pending_signals
**p_sig
;
1417 p_sig
= &lwp
->pending_signals_to_report
;
1418 while ((*p_sig
)->prev
!= NULL
)
1419 p_sig
= &(*p_sig
)->prev
;
1421 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
1422 if ((*p_sig
)->info
.si_signo
!= 0)
1423 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
), 0, &(*p_sig
)->info
);
1428 fprintf (stderr
, "Reporting deferred signal %d for LWP %ld.\n",
1429 WSTOPSIG (*wstat
), lwpid_of (lwp
));
1433 struct pending_signals
*sig
;
1435 for (sig
= lwp
->pending_signals_to_report
;
1439 " Still queued %d\n",
1442 fprintf (stderr
, " (no more queued signals)\n");
1451 /* Arrange for a breakpoint to be hit again later. We don't keep the
1452 SIGTRAP status and don't forward the SIGTRAP signal to the LWP. We
1453 will handle the current event, eventually we will resume this LWP,
1454 and this breakpoint will trap again. */
1457 cancel_breakpoint (struct lwp_info
*lwp
)
1459 struct thread_info
*saved_inferior
;
1461 /* There's nothing to do if we don't support breakpoints. */
1462 if (!supports_breakpoints ())
1465 /* breakpoint_at reads from current inferior. */
1466 saved_inferior
= current_inferior
;
1467 current_inferior
= get_lwp_thread (lwp
);
1469 if ((*the_low_target
.breakpoint_at
) (lwp
->stop_pc
))
1473 "CB: Push back breakpoint for %s\n",
1474 target_pid_to_str (ptid_of (lwp
)));
1476 /* Back up the PC if necessary. */
1477 if (the_low_target
.decr_pc_after_break
)
1479 struct regcache
*regcache
1480 = get_thread_regcache (current_inferior
, 1);
1481 (*the_low_target
.set_pc
) (regcache
, lwp
->stop_pc
);
1484 current_inferior
= saved_inferior
;
1491 "CB: No breakpoint found at %s for [%s]\n",
1492 paddress (lwp
->stop_pc
),
1493 target_pid_to_str (ptid_of (lwp
)));
1496 current_inferior
= saved_inferior
;
1500 /* When the event-loop is doing a step-over, this points at the thread
1502 ptid_t step_over_bkpt
;
1504 /* Wait for an event from child PID. If PID is -1, wait for any
1505 child. Store the stop status through the status pointer WSTAT.
1506 OPTIONS is passed to the waitpid call. Return 0 if no child stop
1507 event was found and OPTIONS contains WNOHANG. Return the PID of
1508 the stopped child otherwise. */
1511 linux_wait_for_event_1 (ptid_t ptid
, int *wstat
, int options
)
1513 struct lwp_info
*event_child
, *requested_child
;
1516 requested_child
= NULL
;
1518 /* Check for a lwp with a pending status. */
1520 if (ptid_equal (ptid
, minus_one_ptid
)
1521 || ptid_equal (pid_to_ptid (ptid_get_pid (ptid
)), ptid
))
1523 event_child
= (struct lwp_info
*)
1524 find_inferior (&all_lwps
, status_pending_p_callback
, &ptid
);
1525 if (debug_threads
&& event_child
)
1526 fprintf (stderr
, "Got a pending child %ld\n", lwpid_of (event_child
));
1530 requested_child
= find_lwp_pid (ptid
);
1532 if (!stopping_threads
1533 && requested_child
->status_pending_p
1534 && requested_child
->collecting_fast_tracepoint
)
1536 enqueue_one_deferred_signal (requested_child
,
1537 &requested_child
->status_pending
);
1538 requested_child
->status_pending_p
= 0;
1539 requested_child
->status_pending
= 0;
1540 linux_resume_one_lwp (requested_child
, 0, 0, NULL
);
1543 if (requested_child
->suspended
1544 && requested_child
->status_pending_p
)
1545 fatal ("requesting an event out of a suspended child?");
1547 if (requested_child
->status_pending_p
)
1548 event_child
= requested_child
;
1551 if (event_child
!= NULL
)
1554 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
1555 lwpid_of (event_child
), event_child
->status_pending
);
1556 *wstat
= event_child
->status_pending
;
1557 event_child
->status_pending_p
= 0;
1558 event_child
->status_pending
= 0;
1559 current_inferior
= get_lwp_thread (event_child
);
1560 return lwpid_of (event_child
);
1563 /* We only enter this loop if no process has a pending wait status. Thus
1564 any action taken in response to a wait status inside this loop is
1565 responding as soon as we detect the status, not after any pending
1569 event_child
= linux_wait_for_lwp (ptid
, wstat
, options
);
1571 if ((options
& WNOHANG
) && event_child
== NULL
)
1574 fprintf (stderr
, "WNOHANG set, no event found\n");
1578 if (event_child
== NULL
)
1579 error ("event from unknown child");
1581 current_inferior
= get_lwp_thread (event_child
);
1583 /* Check for thread exit. */
1584 if (! WIFSTOPPED (*wstat
))
1587 fprintf (stderr
, "LWP %ld exiting\n", lwpid_of (event_child
));
1589 /* If the last thread is exiting, just return. */
1590 if (last_thread_of_process_p (current_inferior
))
1593 fprintf (stderr
, "LWP %ld is last lwp of process\n",
1594 lwpid_of (event_child
));
1595 return lwpid_of (event_child
);
1600 current_inferior
= (struct thread_info
*) all_threads
.head
;
1602 fprintf (stderr
, "Current inferior is now %ld\n",
1603 lwpid_of (get_thread_lwp (current_inferior
)));
1607 current_inferior
= NULL
;
1609 fprintf (stderr
, "Current inferior is now <NULL>\n");
1612 /* If we were waiting for this particular child to do something...
1613 well, it did something. */
1614 if (requested_child
!= NULL
)
1616 int lwpid
= lwpid_of (event_child
);
1618 /* Cancel the step-over operation --- the thread that
1619 started it is gone. */
1620 if (finish_step_over (event_child
))
1621 unstop_all_lwps (1, event_child
);
1622 delete_lwp (event_child
);
1626 delete_lwp (event_child
);
1628 /* Wait for a more interesting event. */
1632 if (event_child
->must_set_ptrace_flags
)
1634 linux_enable_event_reporting (lwpid_of (event_child
));
1635 event_child
->must_set_ptrace_flags
= 0;
1638 if (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) == SIGTRAP
1639 && *wstat
>> 16 != 0)
1641 handle_extended_wait (event_child
, *wstat
);
1645 if (WIFSTOPPED (*wstat
)
1646 && WSTOPSIG (*wstat
) == SIGSTOP
1647 && event_child
->stop_expected
)
1652 fprintf (stderr
, "Expected stop.\n");
1653 event_child
->stop_expected
= 0;
1655 should_stop
= (current_inferior
->last_resume_kind
== resume_stop
1656 || stopping_threads
);
1660 linux_resume_one_lwp (event_child
,
1661 event_child
->stepping
, 0, NULL
);
1666 return lwpid_of (event_child
);
1674 linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
)
1678 if (ptid_is_pid (ptid
))
1680 /* A request to wait for a specific tgid. This is not possible
1681 with waitpid, so instead, we wait for any child, and leave
1682 children we're not interested in right now with a pending
1683 status to report later. */
1684 wait_ptid
= minus_one_ptid
;
1693 event_pid
= linux_wait_for_event_1 (wait_ptid
, wstat
, options
);
1696 && ptid_is_pid (ptid
) && ptid_get_pid (ptid
) != event_pid
)
1698 struct lwp_info
*event_child
1699 = find_lwp_pid (pid_to_ptid (event_pid
));
1701 if (! WIFSTOPPED (*wstat
))
1702 mark_lwp_dead (event_child
, *wstat
);
1705 event_child
->status_pending_p
= 1;
1706 event_child
->status_pending
= *wstat
;
1715 /* Count the LWP's that have had events. */
1718 count_events_callback (struct inferior_list_entry
*entry
, void *data
)
1720 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
1721 struct thread_info
*thread
= get_lwp_thread (lp
);
1724 gdb_assert (count
!= NULL
);
1726 /* Count only resumed LWPs that have a SIGTRAP event pending that
1727 should be reported to GDB. */
1728 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
1729 && thread
->last_resume_kind
!= resume_stop
1730 && lp
->status_pending_p
1731 && WIFSTOPPED (lp
->status_pending
)
1732 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
1733 && !breakpoint_inserted_here (lp
->stop_pc
))
1739 /* Select the LWP (if any) that is currently being single-stepped. */
1742 select_singlestep_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
1744 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
1745 struct thread_info
*thread
= get_lwp_thread (lp
);
1747 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
1748 && thread
->last_resume_kind
== resume_step
1749 && lp
->status_pending_p
)
1755 /* Select the Nth LWP that has had a SIGTRAP event that should be
1759 select_event_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
1761 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
1762 struct thread_info
*thread
= get_lwp_thread (lp
);
1763 int *selector
= data
;
1765 gdb_assert (selector
!= NULL
);
1767 /* Select only resumed LWPs that have a SIGTRAP event pending. */
1768 if (thread
->last_resume_kind
!= resume_stop
1769 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
1770 && lp
->status_pending_p
1771 && WIFSTOPPED (lp
->status_pending
)
1772 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
1773 && !breakpoint_inserted_here (lp
->stop_pc
))
1774 if ((*selector
)-- == 0)
1781 cancel_breakpoints_callback (struct inferior_list_entry
*entry
, void *data
)
1783 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
1784 struct thread_info
*thread
= get_lwp_thread (lp
);
1785 struct lwp_info
*event_lp
= data
;
1787 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
1791 /* If a LWP other than the LWP that we're reporting an event for has
1792 hit a GDB breakpoint (as opposed to some random trap signal),
1793 then just arrange for it to hit it again later. We don't keep
1794 the SIGTRAP status and don't forward the SIGTRAP signal to the
1795 LWP. We will handle the current event, eventually we will resume
1796 all LWPs, and this one will get its breakpoint trap again.
1798 If we do not do this, then we run the risk that the user will
1799 delete or disable the breakpoint, but the LWP will have already
1802 if (thread
->last_resume_kind
!= resume_stop
1803 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
1804 && lp
->status_pending_p
1805 && WIFSTOPPED (lp
->status_pending
)
1806 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
1808 && !lp
->stopped_by_watchpoint
1809 && cancel_breakpoint (lp
))
1810 /* Throw away the SIGTRAP. */
1811 lp
->status_pending_p
= 0;
1817 linux_cancel_breakpoints (void)
1819 find_inferior (&all_lwps
, cancel_breakpoints_callback
, NULL
);
1822 /* Select one LWP out of those that have events pending. */
1825 select_event_lwp (struct lwp_info
**orig_lp
)
1828 int random_selector
;
1829 struct lwp_info
*event_lp
;
1831 /* Give preference to any LWP that is being single-stepped. */
1833 = (struct lwp_info
*) find_inferior (&all_lwps
,
1834 select_singlestep_lwp_callback
, NULL
);
1835 if (event_lp
!= NULL
)
1839 "SEL: Select single-step %s\n",
1840 target_pid_to_str (ptid_of (event_lp
)));
1844 /* No single-stepping LWP. Select one at random, out of those
1845 which have had SIGTRAP events. */
1847 /* First see how many SIGTRAP events we have. */
1848 find_inferior (&all_lwps
, count_events_callback
, &num_events
);
1850 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
1851 random_selector
= (int)
1852 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
1854 if (debug_threads
&& num_events
> 1)
1856 "SEL: Found %d SIGTRAP events, selecting #%d\n",
1857 num_events
, random_selector
);
1859 event_lp
= (struct lwp_info
*) find_inferior (&all_lwps
,
1860 select_event_lwp_callback
,
1864 if (event_lp
!= NULL
)
1866 /* Switch the event LWP. */
1867 *orig_lp
= event_lp
;
1871 /* Decrement the suspend count of an LWP. */
1874 unsuspend_one_lwp (struct inferior_list_entry
*entry
, void *except
)
1876 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
1878 /* Ignore EXCEPT. */
1884 gdb_assert (lwp
->suspended
>= 0);
1888 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
1892 unsuspend_all_lwps (struct lwp_info
*except
)
1894 find_inferior (&all_lwps
, unsuspend_one_lwp
, except
);
1897 static void move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
);
1898 static int stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
,
1900 static int lwp_running (struct inferior_list_entry
*entry
, void *data
);
1901 static ptid_t
linux_wait_1 (ptid_t ptid
,
1902 struct target_waitstatus
*ourstatus
,
1903 int target_options
);
1905 /* Stabilize threads (move out of jump pads).
1907 If a thread is midway collecting a fast tracepoint, we need to
1908 finish the collection and move it out of the jump pad before
1909 reporting the signal.
1911 This avoids recursion while collecting (when a signal arrives
1912 midway, and the signal handler itself collects), which would trash
1913 the trace buffer. In case the user set a breakpoint in a signal
1914 handler, this avoids the backtrace showing the jump pad, etc..
1915 Most importantly, there are certain things we can't do safely if
1916 threads are stopped in a jump pad (or in its callee's). For
1919 - starting a new trace run. A thread still collecting the
1920 previous run, could trash the trace buffer when resumed. The trace
1921 buffer control structures would have been reset but the thread had
1922 no way to tell. The thread could even midway memcpy'ing to the
1923 buffer, which would mean that when resumed, it would clobber the
1924 trace buffer that had been set for a new run.
1926 - we can't rewrite/reuse the jump pads for new tracepoints
1927 safely. Say you do tstart while a thread is stopped midway while
1928 collecting. When the thread is later resumed, it finishes the
1929 collection, and returns to the jump pad, to execute the original
1930 instruction that was under the tracepoint jump at the time the
1931 older run had been started. If the jump pad had been rewritten
1932 since for something else in the new run, the thread would now
1933 execute the wrong / random instructions. */
1936 linux_stabilize_threads (void)
1938 struct thread_info
*save_inferior
;
1939 struct lwp_info
*lwp_stuck
;
1942 = (struct lwp_info
*) find_inferior (&all_lwps
,
1943 stuck_in_jump_pad_callback
, NULL
);
1944 if (lwp_stuck
!= NULL
)
1947 fprintf (stderr
, "can't stabilize, LWP %ld is stuck in jump pad\n",
1948 lwpid_of (lwp_stuck
));
1952 save_inferior
= current_inferior
;
1954 stabilizing_threads
= 1;
1957 for_each_inferior (&all_lwps
, move_out_of_jump_pad_callback
);
1959 /* Loop until all are stopped out of the jump pads. */
1960 while (find_inferior (&all_lwps
, lwp_running
, NULL
) != NULL
)
1962 struct target_waitstatus ourstatus
;
1963 struct lwp_info
*lwp
;
1966 /* Note that we go through the full wait even loop. While
1967 moving threads out of jump pad, we need to be able to step
1968 over internal breakpoints and such. */
1969 linux_wait_1 (minus_one_ptid
, &ourstatus
, 0);
1971 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
1973 lwp
= get_thread_lwp (current_inferior
);
1978 if (ourstatus
.value
.sig
!= TARGET_SIGNAL_0
1979 || current_inferior
->last_resume_kind
== resume_stop
)
1981 wstat
= W_STOPCODE (target_signal_to_host (ourstatus
.value
.sig
));
1982 enqueue_one_deferred_signal (lwp
, &wstat
);
1987 find_inferior (&all_lwps
, unsuspend_one_lwp
, NULL
);
1989 stabilizing_threads
= 0;
1991 current_inferior
= save_inferior
;
1996 = (struct lwp_info
*) find_inferior (&all_lwps
,
1997 stuck_in_jump_pad_callback
, NULL
);
1998 if (lwp_stuck
!= NULL
)
1999 fprintf (stderr
, "couldn't stabilize, LWP %ld got stuck in jump pad\n",
2000 lwpid_of (lwp_stuck
));
2004 /* Wait for process, returns status. */
2007 linux_wait_1 (ptid_t ptid
,
2008 struct target_waitstatus
*ourstatus
, int target_options
)
2011 struct lwp_info
*event_child
;
2014 int step_over_finished
;
2015 int bp_explains_trap
;
2016 int maybe_internal_trap
;
2020 /* Translate generic target options into linux options. */
2022 if (target_options
& TARGET_WNOHANG
)
2026 bp_explains_trap
= 0;
2028 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2030 /* If we were only supposed to resume one thread, only wait for
2031 that thread - if it's still alive. If it died, however - which
2032 can happen if we're coming from the thread death case below -
2033 then we need to make sure we restart the other threads. We could
2034 pick a thread at random or restart all; restarting all is less
2037 && !ptid_equal (cont_thread
, null_ptid
)
2038 && !ptid_equal (cont_thread
, minus_one_ptid
))
2040 struct thread_info
*thread
;
2042 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
2045 /* No stepping, no signal - unless one is pending already, of course. */
2048 struct thread_resume resume_info
;
2049 resume_info
.thread
= minus_one_ptid
;
2050 resume_info
.kind
= resume_continue
;
2051 resume_info
.sig
= 0;
2052 linux_resume (&resume_info
, 1);
2058 if (ptid_equal (step_over_bkpt
, null_ptid
))
2059 pid
= linux_wait_for_event (ptid
, &w
, options
);
2063 fprintf (stderr
, "step_over_bkpt set [%s], doing a blocking wait\n",
2064 target_pid_to_str (step_over_bkpt
));
2065 pid
= linux_wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
2068 if (pid
== 0) /* only if TARGET_WNOHANG */
2071 event_child
= get_thread_lwp (current_inferior
);
2073 /* If we are waiting for a particular child, and it exited,
2074 linux_wait_for_event will return its exit status. Similarly if
2075 the last child exited. If this is not the last child, however,
2076 do not report it as exited until there is a 'thread exited' response
2077 available in the remote protocol. Instead, just wait for another event.
2078 This should be safe, because if the thread crashed we will already
2079 have reported the termination signal to GDB; that should stop any
2080 in-progress stepping operations, etc.
2082 Report the exit status of the last thread to exit. This matches
2083 LinuxThreads' behavior. */
2085 if (last_thread_of_process_p (current_inferior
))
2087 if (WIFEXITED (w
) || WIFSIGNALED (w
))
2091 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
2092 ourstatus
->value
.integer
= WEXITSTATUS (w
);
2096 "\nChild exited with retcode = %x \n",
2101 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
2102 ourstatus
->value
.sig
= target_signal_from_host (WTERMSIG (w
));
2106 "\nChild terminated with signal = %x \n",
2111 return ptid_of (event_child
);
2116 if (!WIFSTOPPED (w
))
2120 /* If this event was not handled before, and is not a SIGTRAP, we
2121 report it. SIGILL and SIGSEGV are also treated as traps in case
2122 a breakpoint is inserted at the current PC. If this target does
2123 not support internal breakpoints at all, we also report the
2124 SIGTRAP without further processing; it's of no concern to us. */
2126 = (supports_breakpoints ()
2127 && (WSTOPSIG (w
) == SIGTRAP
2128 || ((WSTOPSIG (w
) == SIGILL
2129 || WSTOPSIG (w
) == SIGSEGV
)
2130 && (*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))));
2132 if (maybe_internal_trap
)
2134 /* Handle anything that requires bookkeeping before deciding to
2135 report the event or continue waiting. */
2137 /* First check if we can explain the SIGTRAP with an internal
2138 breakpoint, or if we should possibly report the event to GDB.
2139 Do this before anything that may remove or insert a
2141 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
2143 /* We have a SIGTRAP, possibly a step-over dance has just
2144 finished. If so, tweak the state machine accordingly,
2145 reinsert breakpoints and delete any reinsert (software
2146 single-step) breakpoints. */
2147 step_over_finished
= finish_step_over (event_child
);
2149 /* Now invoke the callbacks of any internal breakpoints there. */
2150 check_breakpoints (event_child
->stop_pc
);
2152 /* Handle tracepoint data collecting. This may overflow the
2153 trace buffer, and cause a tracing stop, removing
2155 trace_event
= handle_tracepoints (event_child
);
2157 if (bp_explains_trap
)
2159 /* If we stepped or ran into an internal breakpoint, we've
2160 already handled it. So next time we resume (from this
2161 PC), we should step over it. */
2163 fprintf (stderr
, "Hit a gdbserver breakpoint.\n");
2165 if (breakpoint_here (event_child
->stop_pc
))
2166 event_child
->need_step_over
= 1;
2171 /* We have some other signal, possibly a step-over dance was in
2172 progress, and it should be cancelled too. */
2173 step_over_finished
= finish_step_over (event_child
);
2176 /* We have all the data we need. Either report the event to GDB, or
2177 resume threads and keep waiting for more. */
2179 /* If we're collecting a fast tracepoint, finish the collection and
2180 move out of the jump pad before delivering a signal. See
2181 linux_stabilize_threads. */
2184 && WSTOPSIG (w
) != SIGTRAP
2185 && supports_fast_tracepoints ()
2186 && in_process_agent_loaded ())
2190 "Got signal %d for LWP %ld. Check if we need "
2191 "to defer or adjust it.\n",
2192 WSTOPSIG (w
), lwpid_of (event_child
));
2194 /* Allow debugging the jump pad itself. */
2195 if (current_inferior
->last_resume_kind
!= resume_step
2196 && maybe_move_out_of_jump_pad (event_child
, &w
))
2198 enqueue_one_deferred_signal (event_child
, &w
);
2202 "Signal %d for LWP %ld deferred (in jump pad)\n",
2203 WSTOPSIG (w
), lwpid_of (event_child
));
2205 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
2210 if (event_child
->collecting_fast_tracepoint
)
2214 LWP %ld was trying to move out of the jump pad (%d). \
2215 Check if we're already there.\n",
2216 lwpid_of (event_child
),
2217 event_child
->collecting_fast_tracepoint
);
2221 event_child
->collecting_fast_tracepoint
2222 = linux_fast_tracepoint_collecting (event_child
, NULL
);
2224 if (event_child
->collecting_fast_tracepoint
!= 1)
2226 /* No longer need this breakpoint. */
2227 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
2231 "No longer need exit-jump-pad bkpt; removing it."
2232 "stopping all threads momentarily.\n");
2234 /* Other running threads could hit this breakpoint.
2235 We don't handle moribund locations like GDB does,
2236 instead we always pause all threads when removing
2237 breakpoints, so that any step-over or
2238 decr_pc_after_break adjustment is always taken
2239 care of while the breakpoint is still
2241 stop_all_lwps (1, event_child
);
2242 cancel_breakpoints ();
2244 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
2245 event_child
->exit_jump_pad_bkpt
= NULL
;
2247 unstop_all_lwps (1, event_child
);
2249 gdb_assert (event_child
->suspended
>= 0);
2253 if (event_child
->collecting_fast_tracepoint
== 0)
2257 "fast tracepoint finished "
2258 "collecting successfully.\n");
2260 /* We may have a deferred signal to report. */
2261 if (dequeue_one_deferred_signal (event_child
, &w
))
2264 fprintf (stderr
, "dequeued one signal.\n");
2269 fprintf (stderr
, "no deferred signals.\n");
2271 if (stabilizing_threads
)
2273 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
2274 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
2275 return ptid_of (event_child
);
2281 /* Check whether GDB would be interested in this event. */
2283 /* If GDB is not interested in this signal, don't stop other
2284 threads, and don't report it to GDB. Just resume the inferior
2285 right away. We do this for threading-related signals as well as
2286 any that GDB specifically requested we ignore. But never ignore
2287 SIGSTOP if we sent it ourselves, and do not ignore signals when
2288 stepping - they may require special handling to skip the signal
2290 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
2293 && current_inferior
->last_resume_kind
!= resume_step
2295 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
2296 (current_process ()->private->thread_db
!= NULL
2297 && (WSTOPSIG (w
) == __SIGRTMIN
2298 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
2301 (pass_signals
[target_signal_from_host (WSTOPSIG (w
))]
2302 && !(WSTOPSIG (w
) == SIGSTOP
2303 && current_inferior
->last_resume_kind
== resume_stop
))))
2305 siginfo_t info
, *info_p
;
2308 fprintf (stderr
, "Ignored signal %d for LWP %ld.\n",
2309 WSTOPSIG (w
), lwpid_of (event_child
));
2311 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (event_child
), 0, &info
) == 0)
2315 linux_resume_one_lwp (event_child
, event_child
->stepping
,
2316 WSTOPSIG (w
), info_p
);
2320 /* If GDB wanted this thread to single step, we always want to
2321 report the SIGTRAP, and let GDB handle it. Watchpoints should
2322 always be reported. So should signals we can't explain. A
2323 SIGTRAP we can't explain could be a GDB breakpoint --- we may or
2324 not support Z0 breakpoints. If we do, we're be able to handle
2325 GDB breakpoints on top of internal breakpoints, by handling the
2326 internal breakpoint and still reporting the event to GDB. If we
2327 don't, we're out of luck, GDB won't see the breakpoint hit. */
2328 report_to_gdb
= (!maybe_internal_trap
2329 || current_inferior
->last_resume_kind
== resume_step
2330 || event_child
->stopped_by_watchpoint
2331 || (!step_over_finished
2332 && !bp_explains_trap
&& !trace_event
)
2333 || gdb_breakpoint_here (event_child
->stop_pc
));
2335 /* We found no reason GDB would want us to stop. We either hit one
2336 of our own breakpoints, or finished an internal step GDB
2337 shouldn't know about. */
2342 if (bp_explains_trap
)
2343 fprintf (stderr
, "Hit a gdbserver breakpoint.\n");
2344 if (step_over_finished
)
2345 fprintf (stderr
, "Step-over finished.\n");
2347 fprintf (stderr
, "Tracepoint event.\n");
2350 /* We're not reporting this breakpoint to GDB, so apply the
2351 decr_pc_after_break adjustment to the inferior's regcache
2354 if (the_low_target
.set_pc
!= NULL
)
2356 struct regcache
*regcache
2357 = get_thread_regcache (get_lwp_thread (event_child
), 1);
2358 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
2361 /* We may have finished stepping over a breakpoint. If so,
2362 we've stopped and suspended all LWPs momentarily except the
2363 stepping one. This is where we resume them all again. We're
2364 going to keep waiting, so use proceed, which handles stepping
2365 over the next breakpoint. */
2367 fprintf (stderr
, "proceeding all threads.\n");
2369 if (step_over_finished
)
2370 unsuspend_all_lwps (event_child
);
2372 proceed_all_lwps ();
2378 if (current_inferior
->last_resume_kind
== resume_step
)
2379 fprintf (stderr
, "GDB wanted to single-step, reporting event.\n");
2380 if (event_child
->stopped_by_watchpoint
)
2381 fprintf (stderr
, "Stopped by watchpoint.\n");
2382 if (gdb_breakpoint_here (event_child
->stop_pc
))
2383 fprintf (stderr
, "Stopped by GDB breakpoint.\n");
2385 fprintf (stderr
, "Hit a non-gdbserver trap event.\n");
2388 /* Alright, we're going to report a stop. */
2390 if (!non_stop
&& !stabilizing_threads
)
2392 /* In all-stop, stop all threads. */
2393 stop_all_lwps (0, NULL
);
2395 /* If we're not waiting for a specific LWP, choose an event LWP
2396 from among those that have had events. Giving equal priority
2397 to all LWPs that have had events helps prevent
2399 if (ptid_equal (ptid
, minus_one_ptid
))
2401 event_child
->status_pending_p
= 1;
2402 event_child
->status_pending
= w
;
2404 select_event_lwp (&event_child
);
2406 event_child
->status_pending_p
= 0;
2407 w
= event_child
->status_pending
;
2410 /* Now that we've selected our final event LWP, cancel any
2411 breakpoints in other LWPs that have hit a GDB breakpoint.
2412 See the comment in cancel_breakpoints_callback to find out
2414 find_inferior (&all_lwps
, cancel_breakpoints_callback
, event_child
);
2416 /* Stabilize threads (move out of jump pads). */
2417 stabilize_threads ();
2421 /* If we just finished a step-over, then all threads had been
2422 momentarily paused. In all-stop, that's fine, we want
2423 threads stopped by now anyway. In non-stop, we need to
2424 re-resume threads that GDB wanted to be running. */
2425 if (step_over_finished
)
2426 unstop_all_lwps (1, event_child
);
2429 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
2431 if (current_inferior
->last_resume_kind
== resume_stop
2432 && WSTOPSIG (w
) == SIGSTOP
)
2434 /* A thread that has been requested to stop by GDB with vCont;t,
2435 and it stopped cleanly, so report as SIG0. The use of
2436 SIGSTOP is an implementation detail. */
2437 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
2439 else if (current_inferior
->last_resume_kind
== resume_stop
2440 && WSTOPSIG (w
) != SIGSTOP
)
2442 /* A thread that has been requested to stop by GDB with vCont;t,
2443 but, it stopped for other reasons. */
2444 ourstatus
->value
.sig
= target_signal_from_host (WSTOPSIG (w
));
2448 ourstatus
->value
.sig
= target_signal_from_host (WSTOPSIG (w
));
2451 gdb_assert (ptid_equal (step_over_bkpt
, null_ptid
));
2454 fprintf (stderr
, "linux_wait ret = %s, %d, %d\n",
2455 target_pid_to_str (ptid_of (event_child
)),
2457 ourstatus
->value
.sig
);
2459 return ptid_of (event_child
);
2462 /* Get rid of any pending event in the pipe. */
2464 async_file_flush (void)
2470 ret
= read (linux_event_pipe
[0], &buf
, 1);
2471 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
2474 /* Put something in the pipe, so the event loop wakes up. */
2476 async_file_mark (void)
2480 async_file_flush ();
2483 ret
= write (linux_event_pipe
[1], "+", 1);
2484 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
2486 /* Ignore EAGAIN. If the pipe is full, the event loop will already
2487 be awakened anyway. */
2491 linux_wait (ptid_t ptid
,
2492 struct target_waitstatus
*ourstatus
, int target_options
)
2497 fprintf (stderr
, "linux_wait: [%s]\n", target_pid_to_str (ptid
));
2499 /* Flush the async file first. */
2500 if (target_is_async_p ())
2501 async_file_flush ();
2503 event_ptid
= linux_wait_1 (ptid
, ourstatus
, target_options
);
2505 /* If at least one stop was reported, there may be more. A single
2506 SIGCHLD can signal more than one child stop. */
2507 if (target_is_async_p ()
2508 && (target_options
& TARGET_WNOHANG
) != 0
2509 && !ptid_equal (event_ptid
, null_ptid
))
2515 /* Send a signal to an LWP. */
2518 kill_lwp (unsigned long lwpid
, int signo
)
2520 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2521 fails, then we are not using nptl threads and we should be using kill. */
2525 static int tkill_failed
;
2532 ret
= syscall (__NR_tkill
, lwpid
, signo
);
2533 if (errno
!= ENOSYS
)
2540 return kill (lwpid
, signo
);
2544 linux_stop_lwp (struct lwp_info
*lwp
)
2550 send_sigstop (struct lwp_info
*lwp
)
2554 pid
= lwpid_of (lwp
);
2556 /* If we already have a pending stop signal for this process, don't
2558 if (lwp
->stop_expected
)
2561 fprintf (stderr
, "Have pending sigstop for lwp %d\n", pid
);
2567 fprintf (stderr
, "Sending sigstop to lwp %d\n", pid
);
2569 lwp
->stop_expected
= 1;
2570 kill_lwp (pid
, SIGSTOP
);
2574 send_sigstop_callback (struct inferior_list_entry
*entry
, void *except
)
2576 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2578 /* Ignore EXCEPT. */
2589 /* Increment the suspend count of an LWP, and stop it, if not stopped
2592 suspend_and_send_sigstop_callback (struct inferior_list_entry
*entry
,
2595 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2597 /* Ignore EXCEPT. */
2603 return send_sigstop_callback (entry
, except
);
2607 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
2609 /* It's dead, really. */
2612 /* Store the exit status for later. */
2613 lwp
->status_pending_p
= 1;
2614 lwp
->status_pending
= wstat
;
2616 /* Prevent trying to stop it. */
2619 /* No further stops are expected from a dead lwp. */
2620 lwp
->stop_expected
= 0;
2624 wait_for_sigstop (struct inferior_list_entry
*entry
)
2626 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2627 struct thread_info
*saved_inferior
;
2636 fprintf (stderr
, "wait_for_sigstop: LWP %ld already stopped\n",
2641 saved_inferior
= current_inferior
;
2642 if (saved_inferior
!= NULL
)
2643 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
2645 saved_tid
= null_ptid
; /* avoid bogus unused warning */
2647 ptid
= lwp
->head
.id
;
2650 fprintf (stderr
, "wait_for_sigstop: pulling one event\n");
2652 pid
= linux_wait_for_event (ptid
, &wstat
, __WALL
);
2654 /* If we stopped with a non-SIGSTOP signal, save it for later
2655 and record the pending SIGSTOP. If the process exited, just
2657 if (WIFSTOPPED (wstat
))
2660 fprintf (stderr
, "LWP %ld stopped with signal %d\n",
2661 lwpid_of (lwp
), WSTOPSIG (wstat
));
2663 if (WSTOPSIG (wstat
) != SIGSTOP
)
2666 fprintf (stderr
, "LWP %ld stopped with non-sigstop status %06x\n",
2667 lwpid_of (lwp
), wstat
);
2669 lwp
->status_pending_p
= 1;
2670 lwp
->status_pending
= wstat
;
2676 fprintf (stderr
, "Process %d exited while stopping LWPs\n", pid
);
2678 lwp
= find_lwp_pid (pid_to_ptid (pid
));
2681 /* Leave this status pending for the next time we're able to
2682 report it. In the mean time, we'll report this lwp as
2683 dead to GDB, so GDB doesn't try to read registers and
2684 memory from it. This can only happen if this was the
2685 last thread of the process; otherwise, PID is removed
2686 from the thread tables before linux_wait_for_event
2688 mark_lwp_dead (lwp
, wstat
);
2692 if (saved_inferior
== NULL
|| linux_thread_alive (saved_tid
))
2693 current_inferior
= saved_inferior
;
2697 fprintf (stderr
, "Previously current thread died.\n");
2701 /* We can't change the current inferior behind GDB's back,
2702 otherwise, a subsequent command may apply to the wrong
2704 current_inferior
= NULL
;
2708 /* Set a valid thread as current. */
2709 set_desired_inferior (0);
2714 /* Returns true if LWP ENTRY is stopped in a jump pad, and we can't
2715 move it out, because we need to report the stop event to GDB. For
2716 example, if the user puts a breakpoint in the jump pad, it's
2717 because she wants to debug it. */
2720 stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
, void *data
)
2722 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2723 struct thread_info
*thread
= get_lwp_thread (lwp
);
2725 gdb_assert (lwp
->suspended
== 0);
2726 gdb_assert (lwp
->stopped
);
2728 /* Allow debugging the jump pad, gdb_collect, etc.. */
2729 return (supports_fast_tracepoints ()
2730 && in_process_agent_loaded ()
2731 && (gdb_breakpoint_here (lwp
->stop_pc
)
2732 || lwp
->stopped_by_watchpoint
2733 || thread
->last_resume_kind
== resume_step
)
2734 && linux_fast_tracepoint_collecting (lwp
, NULL
));
2738 move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
)
2740 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2741 struct thread_info
*thread
= get_lwp_thread (lwp
);
2744 gdb_assert (lwp
->suspended
== 0);
2745 gdb_assert (lwp
->stopped
);
2747 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
2749 /* Allow debugging the jump pad, gdb_collect, etc. */
2750 if (!gdb_breakpoint_here (lwp
->stop_pc
)
2751 && !lwp
->stopped_by_watchpoint
2752 && thread
->last_resume_kind
!= resume_step
2753 && maybe_move_out_of_jump_pad (lwp
, wstat
))
2757 "LWP %ld needs stabilizing (in jump pad)\n",
2762 lwp
->status_pending_p
= 0;
2763 enqueue_one_deferred_signal (lwp
, wstat
);
2767 "Signal %d for LWP %ld deferred "
2769 WSTOPSIG (*wstat
), lwpid_of (lwp
));
2772 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
2779 lwp_running (struct inferior_list_entry
*entry
, void *data
)
2781 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2790 /* Stop all lwps that aren't stopped yet, except EXCEPT, if not NULL.
2791 If SUSPEND, then also increase the suspend count of every LWP,
2795 stop_all_lwps (int suspend
, struct lwp_info
*except
)
2797 stopping_threads
= 1;
2800 find_inferior (&all_lwps
, suspend_and_send_sigstop_callback
, except
);
2802 find_inferior (&all_lwps
, send_sigstop_callback
, except
);
2803 for_each_inferior (&all_lwps
, wait_for_sigstop
);
2804 stopping_threads
= 0;
2807 /* Resume execution of the inferior process.
2808 If STEP is nonzero, single-step it.
2809 If SIGNAL is nonzero, give it that signal. */
2812 linux_resume_one_lwp (struct lwp_info
*lwp
,
2813 int step
, int signal
, siginfo_t
*info
)
2815 struct thread_info
*saved_inferior
;
2816 int fast_tp_collecting
;
2818 if (lwp
->stopped
== 0)
2821 fast_tp_collecting
= lwp
->collecting_fast_tracepoint
;
2823 gdb_assert (!stabilizing_threads
|| fast_tp_collecting
);
2825 /* Cancel actions that rely on GDB not changing the PC (e.g., the
2826 user used the "jump" command, or "set $pc = foo"). */
2827 if (lwp
->stop_pc
!= get_pc (lwp
))
2829 /* Collecting 'while-stepping' actions doesn't make sense
2831 release_while_stepping_state_list (get_lwp_thread (lwp
));
2834 /* If we have pending signals or status, and a new signal, enqueue the
2835 signal. Also enqueue the signal if we are waiting to reinsert a
2836 breakpoint; it will be picked up again below. */
2838 && (lwp
->status_pending_p
2839 || lwp
->pending_signals
!= NULL
2840 || lwp
->bp_reinsert
!= 0
2841 || fast_tp_collecting
))
2843 struct pending_signals
*p_sig
;
2844 p_sig
= xmalloc (sizeof (*p_sig
));
2845 p_sig
->prev
= lwp
->pending_signals
;
2846 p_sig
->signal
= signal
;
2848 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
2850 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
2851 lwp
->pending_signals
= p_sig
;
2854 if (lwp
->status_pending_p
)
2857 fprintf (stderr
, "Not resuming lwp %ld (%s, signal %d, stop %s);"
2858 " has pending status\n",
2859 lwpid_of (lwp
), step
? "step" : "continue", signal
,
2860 lwp
->stop_expected
? "expected" : "not expected");
2864 saved_inferior
= current_inferior
;
2865 current_inferior
= get_lwp_thread (lwp
);
2868 fprintf (stderr
, "Resuming lwp %ld (%s, signal %d, stop %s)\n",
2869 lwpid_of (lwp
), step
? "step" : "continue", signal
,
2870 lwp
->stop_expected
? "expected" : "not expected");
2872 /* This bit needs some thinking about. If we get a signal that
2873 we must report while a single-step reinsert is still pending,
2874 we often end up resuming the thread. It might be better to
2875 (ew) allow a stack of pending events; then we could be sure that
2876 the reinsert happened right away and not lose any signals.
2878 Making this stack would also shrink the window in which breakpoints are
2879 uninserted (see comment in linux_wait_for_lwp) but not enough for
2880 complete correctness, so it won't solve that problem. It may be
2881 worthwhile just to solve this one, however. */
2882 if (lwp
->bp_reinsert
!= 0)
2885 fprintf (stderr
, " pending reinsert at 0x%s\n",
2886 paddress (lwp
->bp_reinsert
));
2888 if (lwp
->bp_reinsert
!= 0 && can_hardware_single_step ())
2890 if (fast_tp_collecting
== 0)
2893 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
2895 fprintf (stderr
, "BAD - reinserting and suspended(%d).\n",
2902 /* Postpone any pending signal. It was enqueued above. */
2906 if (fast_tp_collecting
== 1)
2910 lwp %ld wants to get out of fast tracepoint jump pad (exit-jump-pad-bkpt)\n",
2913 /* Postpone any pending signal. It was enqueued above. */
2916 else if (fast_tp_collecting
== 2)
2920 lwp %ld wants to get out of fast tracepoint jump pad single-stepping\n",
2923 if (can_hardware_single_step ())
2926 fatal ("moving out of jump pad single-stepping"
2927 " not implemented on this target");
2929 /* Postpone any pending signal. It was enqueued above. */
2933 /* If we have while-stepping actions in this thread set it stepping.
2934 If we have a signal to deliver, it may or may not be set to
2935 SIG_IGN, we don't know. Assume so, and allow collecting
2936 while-stepping into a signal handler. A possible smart thing to
2937 do would be to set an internal breakpoint at the signal return
2938 address, continue, and carry on catching this while-stepping
2939 action only when that breakpoint is hit. A future
2941 if (get_lwp_thread (lwp
)->while_stepping
!= NULL
2942 && can_hardware_single_step ())
2946 "lwp %ld has a while-stepping action -> forcing step.\n",
2951 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
2953 struct regcache
*regcache
= get_thread_regcache (current_inferior
, 1);
2954 CORE_ADDR pc
= (*the_low_target
.get_pc
) (regcache
);
2955 fprintf (stderr
, " resuming from pc 0x%lx\n", (long) pc
);
2958 /* If we have pending signals, consume one unless we are trying to
2959 reinsert a breakpoint or we're trying to finish a fast tracepoint
2961 if (lwp
->pending_signals
!= NULL
2962 && lwp
->bp_reinsert
== 0
2963 && fast_tp_collecting
== 0)
2965 struct pending_signals
**p_sig
;
2967 p_sig
= &lwp
->pending_signals
;
2968 while ((*p_sig
)->prev
!= NULL
)
2969 p_sig
= &(*p_sig
)->prev
;
2971 signal
= (*p_sig
)->signal
;
2972 if ((*p_sig
)->info
.si_signo
!= 0)
2973 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
), 0, &(*p_sig
)->info
);
2979 if (the_low_target
.prepare_to_resume
!= NULL
)
2980 the_low_target
.prepare_to_resume (lwp
);
2982 regcache_invalidate_one ((struct inferior_list_entry
*)
2983 get_lwp_thread (lwp
));
2986 lwp
->stopped_by_watchpoint
= 0;
2987 lwp
->stepping
= step
;
2988 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, lwpid_of (lwp
), 0,
2989 /* Coerce to a uintptr_t first to avoid potential gcc warning
2990 of coercing an 8 byte integer to a 4 byte pointer. */
2991 (PTRACE_ARG4_TYPE
) (uintptr_t) signal
);
2993 current_inferior
= saved_inferior
;
2996 /* ESRCH from ptrace either means that the thread was already
2997 running (an error) or that it is gone (a race condition). If
2998 it's gone, we will get a notification the next time we wait,
2999 so we can ignore the error. We could differentiate these
3000 two, but it's tricky without waiting; the thread still exists
3001 as a zombie, so sending it signal 0 would succeed. So just
3006 perror_with_name ("ptrace");
3010 struct thread_resume_array
3012 struct thread_resume
*resume
;
3016 /* This function is called once per thread. We look up the thread
3017 in RESUME_PTR, and mark the thread with a pointer to the appropriate
3020 This algorithm is O(threads * resume elements), but resume elements
3021 is small (and will remain small at least until GDB supports thread
3024 linux_set_resume_request (struct inferior_list_entry
*entry
, void *arg
)
3026 struct lwp_info
*lwp
;
3027 struct thread_info
*thread
;
3029 struct thread_resume_array
*r
;
3031 thread
= (struct thread_info
*) entry
;
3032 lwp
= get_thread_lwp (thread
);
3035 for (ndx
= 0; ndx
< r
->n
; ndx
++)
3037 ptid_t ptid
= r
->resume
[ndx
].thread
;
3038 if (ptid_equal (ptid
, minus_one_ptid
)
3039 || ptid_equal (ptid
, entry
->id
)
3040 || (ptid_is_pid (ptid
)
3041 && (ptid_get_pid (ptid
) == pid_of (lwp
)))
3042 || (ptid_get_lwp (ptid
) == -1
3043 && (ptid_get_pid (ptid
) == pid_of (lwp
))))
3045 if (r
->resume
[ndx
].kind
== resume_stop
3046 && thread
->last_resume_kind
== resume_stop
)
3049 fprintf (stderr
, "already %s LWP %ld at GDB's request\n",
3050 thread
->last_status
.kind
== TARGET_WAITKIND_STOPPED
3058 lwp
->resume
= &r
->resume
[ndx
];
3059 thread
->last_resume_kind
= lwp
->resume
->kind
;
3061 /* If we had a deferred signal to report, dequeue one now.
3062 This can happen if LWP gets more than one signal while
3063 trying to get out of a jump pad. */
3065 && !lwp
->status_pending_p
3066 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
3068 lwp
->status_pending_p
= 1;
3072 "Dequeueing deferred signal %d for LWP %ld, "
3073 "leaving status pending.\n",
3074 WSTOPSIG (lwp
->status_pending
), lwpid_of (lwp
));
3081 /* No resume action for this thread. */
3088 /* Set *FLAG_P if this lwp has an interesting status pending. */
3090 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
3092 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3094 /* LWPs which will not be resumed are not interesting, because
3095 we might not wait for them next time through linux_wait. */
3096 if (lwp
->resume
== NULL
)
3099 if (lwp
->status_pending_p
)
3100 * (int *) flag_p
= 1;
3105 /* Return 1 if this lwp that GDB wants running is stopped at an
3106 internal breakpoint that we need to step over. It assumes that any
3107 required STOP_PC adjustment has already been propagated to the
3108 inferior's regcache. */
3111 need_step_over_p (struct inferior_list_entry
*entry
, void *dummy
)
3113 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3114 struct thread_info
*thread
;
3115 struct thread_info
*saved_inferior
;
3118 /* LWPs which will not be resumed are not interesting, because we
3119 might not wait for them next time through linux_wait. */
3125 "Need step over [LWP %ld]? Ignoring, not stopped\n",
3130 thread
= get_lwp_thread (lwp
);
3132 if (thread
->last_resume_kind
== resume_stop
)
3136 "Need step over [LWP %ld]? Ignoring, should remain stopped\n",
3141 gdb_assert (lwp
->suspended
>= 0);
3147 "Need step over [LWP %ld]? Ignoring, suspended\n",
3152 if (!lwp
->need_step_over
)
3156 "Need step over [LWP %ld]? No\n", lwpid_of (lwp
));
3159 if (lwp
->status_pending_p
)
3163 "Need step over [LWP %ld]? Ignoring, has pending status.\n",
3168 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
3172 /* If the PC has changed since we stopped, then don't do anything,
3173 and let the breakpoint/tracepoint be hit. This happens if, for
3174 instance, GDB handled the decr_pc_after_break subtraction itself,
3175 GDB is OOL stepping this thread, or the user has issued a "jump"
3176 command, or poked thread's registers herself. */
3177 if (pc
!= lwp
->stop_pc
)
3181 "Need step over [LWP %ld]? Cancelling, PC was changed. "
3182 "Old stop_pc was 0x%s, PC is now 0x%s\n",
3183 lwpid_of (lwp
), paddress (lwp
->stop_pc
), paddress (pc
));
3185 lwp
->need_step_over
= 0;
3189 saved_inferior
= current_inferior
;
3190 current_inferior
= thread
;
3192 /* We can only step over breakpoints we know about. */
3193 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
3195 /* Don't step over a breakpoint that GDB expects to hit
3197 if (gdb_breakpoint_here (pc
))
3201 "Need step over [LWP %ld]? yes, but found"
3202 " GDB breakpoint at 0x%s; skipping step over\n",
3203 lwpid_of (lwp
), paddress (pc
));
3205 current_inferior
= saved_inferior
;
3212 "Need step over [LWP %ld]? yes, "
3213 "found breakpoint at 0x%s\n",
3214 lwpid_of (lwp
), paddress (pc
));
3216 /* We've found an lwp that needs stepping over --- return 1 so
3217 that find_inferior stops looking. */
3218 current_inferior
= saved_inferior
;
3220 /* If the step over is cancelled, this is set again. */
3221 lwp
->need_step_over
= 0;
3226 current_inferior
= saved_inferior
;
3230 "Need step over [LWP %ld]? No, no breakpoint found at 0x%s\n",
3231 lwpid_of (lwp
), paddress (pc
));
3236 /* Start a step-over operation on LWP. When LWP stopped at a
3237 breakpoint, to make progress, we need to remove the breakpoint out
3238 of the way. If we let other threads run while we do that, they may
3239 pass by the breakpoint location and miss hitting it. To avoid
3240 that, a step-over momentarily stops all threads while LWP is
3241 single-stepped while the breakpoint is temporarily uninserted from
3242 the inferior. When the single-step finishes, we reinsert the
3243 breakpoint, and let all threads that are supposed to be running,
3246 On targets that don't support hardware single-step, we don't
3247 currently support full software single-stepping. Instead, we only
3248 support stepping over the thread event breakpoint, by asking the
3249 low target where to place a reinsert breakpoint. Since this
3250 routine assumes the breakpoint being stepped over is a thread event
3251 breakpoint, it usually assumes the return address of the current
3252 function is a good enough place to set the reinsert breakpoint. */
3255 start_step_over (struct lwp_info
*lwp
)
3257 struct thread_info
*saved_inferior
;
3263 "Starting step-over on LWP %ld. Stopping all threads\n",
3266 stop_all_lwps (1, lwp
);
3267 gdb_assert (lwp
->suspended
== 0);
3270 fprintf (stderr
, "Done stopping all threads for step-over.\n");
3272 /* Note, we should always reach here with an already adjusted PC,
3273 either by GDB (if we're resuming due to GDB's request), or by our
3274 caller, if we just finished handling an internal breakpoint GDB
3275 shouldn't care about. */
3278 saved_inferior
= current_inferior
;
3279 current_inferior
= get_lwp_thread (lwp
);
3281 lwp
->bp_reinsert
= pc
;
3282 uninsert_breakpoints_at (pc
);
3283 uninsert_fast_tracepoint_jumps_at (pc
);
3285 if (can_hardware_single_step ())
3291 CORE_ADDR raddr
= (*the_low_target
.breakpoint_reinsert_addr
) ();
3292 set_reinsert_breakpoint (raddr
);
3296 current_inferior
= saved_inferior
;
3298 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
3300 /* Require next event from this LWP. */
3301 step_over_bkpt
= lwp
->head
.id
;
3305 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
3306 start_step_over, if still there, and delete any reinsert
3307 breakpoints we've set, on non hardware single-step targets. */
3310 finish_step_over (struct lwp_info
*lwp
)
3312 if (lwp
->bp_reinsert
!= 0)
3315 fprintf (stderr
, "Finished step over.\n");
3317 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
3318 may be no breakpoint to reinsert there by now. */
3319 reinsert_breakpoints_at (lwp
->bp_reinsert
);
3320 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
3322 lwp
->bp_reinsert
= 0;
3324 /* Delete any software-single-step reinsert breakpoints. No
3325 longer needed. We don't have to worry about other threads
3326 hitting this trap, and later not being able to explain it,
3327 because we were stepping over a breakpoint, and we hold all
3328 threads but LWP stopped while doing that. */
3329 if (!can_hardware_single_step ())
3330 delete_reinsert_breakpoints ();
3332 step_over_bkpt
= null_ptid
;
3339 /* This function is called once per thread. We check the thread's resume
3340 request, which will tell us whether to resume, step, or leave the thread
3341 stopped; and what signal, if any, it should be sent.
3343 For threads which we aren't explicitly told otherwise, we preserve
3344 the stepping flag; this is used for stepping over gdbserver-placed
3347 If pending_flags was set in any thread, we queue any needed
3348 signals, since we won't actually resume. We already have a pending
3349 event to report, so we don't need to preserve any step requests;
3350 they should be re-issued if necessary. */
3353 linux_resume_one_thread (struct inferior_list_entry
*entry
, void *arg
)
3355 struct lwp_info
*lwp
;
3356 struct thread_info
*thread
;
3358 int leave_all_stopped
= * (int *) arg
;
3361 thread
= (struct thread_info
*) entry
;
3362 lwp
= get_thread_lwp (thread
);
3364 if (lwp
->resume
== NULL
)
3367 if (lwp
->resume
->kind
== resume_stop
)
3370 fprintf (stderr
, "resume_stop request for LWP %ld\n", lwpid_of (lwp
));
3375 fprintf (stderr
, "stopping LWP %ld\n", lwpid_of (lwp
));
3377 /* Stop the thread, and wait for the event asynchronously,
3378 through the event loop. */
3384 fprintf (stderr
, "already stopped LWP %ld\n",
3387 /* The LWP may have been stopped in an internal event that
3388 was not meant to be notified back to GDB (e.g., gdbserver
3389 breakpoint), so we should be reporting a stop event in
3392 /* If the thread already has a pending SIGSTOP, this is a
3393 no-op. Otherwise, something later will presumably resume
3394 the thread and this will cause it to cancel any pending
3395 operation, due to last_resume_kind == resume_stop. If
3396 the thread already has a pending status to report, we
3397 will still report it the next time we wait - see
3398 status_pending_p_callback. */
3400 /* If we already have a pending signal to report, then
3401 there's no need to queue a SIGSTOP, as this means we're
3402 midway through moving the LWP out of the jumppad, and we
3403 will report the pending signal as soon as that is
3405 if (lwp
->pending_signals_to_report
== NULL
)
3409 /* For stop requests, we're done. */
3411 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
3415 /* If this thread which is about to be resumed has a pending status,
3416 then don't resume any threads - we can just report the pending
3417 status. Make sure to queue any signals that would otherwise be
3418 sent. In all-stop mode, we do this decision based on if *any*
3419 thread has a pending status. If there's a thread that needs the
3420 step-over-breakpoint dance, then don't resume any other thread
3421 but that particular one. */
3422 leave_pending
= (lwp
->status_pending_p
|| leave_all_stopped
);
3427 fprintf (stderr
, "resuming LWP %ld\n", lwpid_of (lwp
));
3429 step
= (lwp
->resume
->kind
== resume_step
);
3430 linux_resume_one_lwp (lwp
, step
, lwp
->resume
->sig
, NULL
);
3435 fprintf (stderr
, "leaving LWP %ld stopped\n", lwpid_of (lwp
));
3437 /* If we have a new signal, enqueue the signal. */
3438 if (lwp
->resume
->sig
!= 0)
3440 struct pending_signals
*p_sig
;
3441 p_sig
= xmalloc (sizeof (*p_sig
));
3442 p_sig
->prev
= lwp
->pending_signals
;
3443 p_sig
->signal
= lwp
->resume
->sig
;
3444 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
3446 /* If this is the same signal we were previously stopped by,
3447 make sure to queue its siginfo. We can ignore the return
3448 value of ptrace; if it fails, we'll skip
3449 PTRACE_SETSIGINFO. */
3450 if (WIFSTOPPED (lwp
->last_status
)
3451 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
)
3452 ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
), 0, &p_sig
->info
);
3454 lwp
->pending_signals
= p_sig
;
3458 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
3464 linux_resume (struct thread_resume
*resume_info
, size_t n
)
3466 struct thread_resume_array array
= { resume_info
, n
};
3467 struct lwp_info
*need_step_over
= NULL
;
3469 int leave_all_stopped
;
3471 find_inferior (&all_threads
, linux_set_resume_request
, &array
);
3473 /* If there is a thread which would otherwise be resumed, which has
3474 a pending status, then don't resume any threads - we can just
3475 report the pending status. Make sure to queue any signals that
3476 would otherwise be sent. In non-stop mode, we'll apply this
3477 logic to each thread individually. We consume all pending events
3478 before considering to start a step-over (in all-stop). */
3481 find_inferior (&all_lwps
, resume_status_pending_p
, &any_pending
);
3483 /* If there is a thread which would otherwise be resumed, which is
3484 stopped at a breakpoint that needs stepping over, then don't
3485 resume any threads - have it step over the breakpoint with all
3486 other threads stopped, then resume all threads again. Make sure
3487 to queue any signals that would otherwise be delivered or
3489 if (!any_pending
&& supports_breakpoints ())
3491 = (struct lwp_info
*) find_inferior (&all_lwps
,
3492 need_step_over_p
, NULL
);
3494 leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
3498 if (need_step_over
!= NULL
)
3499 fprintf (stderr
, "Not resuming all, need step over\n");
3500 else if (any_pending
)
3502 "Not resuming, all-stop and found "
3503 "an LWP with pending status\n");
3505 fprintf (stderr
, "Resuming, no pending status or step over needed\n");
3508 /* Even if we're leaving threads stopped, queue all signals we'd
3509 otherwise deliver. */
3510 find_inferior (&all_threads
, linux_resume_one_thread
, &leave_all_stopped
);
3513 start_step_over (need_step_over
);
3516 /* This function is called once per thread. We check the thread's
3517 last resume request, which will tell us whether to resume, step, or
3518 leave the thread stopped. Any signal the client requested to be
3519 delivered has already been enqueued at this point.
3521 If any thread that GDB wants running is stopped at an internal
3522 breakpoint that needs stepping over, we start a step-over operation
3523 on that particular thread, and leave all others stopped. */
3526 proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
3528 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3529 struct thread_info
*thread
;
3537 "proceed_one_lwp: lwp %ld\n", lwpid_of (lwp
));
3542 fprintf (stderr
, " LWP %ld already running\n", lwpid_of (lwp
));
3546 thread
= get_lwp_thread (lwp
);
3548 if (thread
->last_resume_kind
== resume_stop
3549 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3552 fprintf (stderr
, " client wants LWP to remain %ld stopped\n",
3557 if (lwp
->status_pending_p
)
3560 fprintf (stderr
, " LWP %ld has pending status, leaving stopped\n",
3565 gdb_assert (lwp
->suspended
>= 0);
3570 fprintf (stderr
, " LWP %ld is suspended\n", lwpid_of (lwp
));
3574 if (thread
->last_resume_kind
== resume_stop
3575 && lwp
->pending_signals_to_report
== NULL
3576 && lwp
->collecting_fast_tracepoint
== 0)
3578 /* We haven't reported this LWP as stopped yet (otherwise, the
3579 last_status.kind check above would catch it, and we wouldn't
3580 reach here. This LWP may have been momentarily paused by a
3581 stop_all_lwps call while handling for example, another LWP's
3582 step-over. In that case, the pending expected SIGSTOP signal
3583 that was queued at vCont;t handling time will have already
3584 been consumed by wait_for_sigstop, and so we need to requeue
3585 another one here. Note that if the LWP already has a SIGSTOP
3586 pending, this is a no-op. */
3590 "Client wants LWP %ld to stop. "
3591 "Making sure it has a SIGSTOP pending\n",
3597 step
= thread
->last_resume_kind
== resume_step
;
3598 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
3603 unsuspend_and_proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
3605 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3611 gdb_assert (lwp
->suspended
>= 0);
3613 return proceed_one_lwp (entry
, except
);
3616 /* When we finish a step-over, set threads running again. If there's
3617 another thread that may need a step-over, now's the time to start
3618 it. Eventually, we'll move all threads past their breakpoints. */
3621 proceed_all_lwps (void)
3623 struct lwp_info
*need_step_over
;
3625 /* If there is a thread which would otherwise be resumed, which is
3626 stopped at a breakpoint that needs stepping over, then don't
3627 resume any threads - have it step over the breakpoint with all
3628 other threads stopped, then resume all threads again. */
3630 if (supports_breakpoints ())
3633 = (struct lwp_info
*) find_inferior (&all_lwps
,
3634 need_step_over_p
, NULL
);
3636 if (need_step_over
!= NULL
)
3639 fprintf (stderr
, "proceed_all_lwps: found "
3640 "thread %ld needing a step-over\n",
3641 lwpid_of (need_step_over
));
3643 start_step_over (need_step_over
);
3649 fprintf (stderr
, "Proceeding, no step-over needed\n");
3651 find_inferior (&all_lwps
, proceed_one_lwp
, NULL
);
3654 /* Stopped LWPs that the client wanted to be running, that don't have
3655 pending statuses, are set to run again, except for EXCEPT, if not
3656 NULL. This undoes a stop_all_lwps call. */
3659 unstop_all_lwps (int unsuspend
, struct lwp_info
*except
)
3665 "unstopping all lwps, except=(LWP %ld)\n", lwpid_of (except
));
3668 "unstopping all lwps\n");
3672 find_inferior (&all_lwps
, unsuspend_and_proceed_one_lwp
, except
);
3674 find_inferior (&all_lwps
, proceed_one_lwp
, except
);
3677 #ifdef HAVE_LINUX_USRREGS
3680 register_addr (int regnum
)
3684 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
3685 error ("Invalid register number %d.", regnum
);
3687 addr
= the_low_target
.regmap
[regnum
];
3692 /* Fetch one register. */
3694 fetch_register (struct regcache
*regcache
, int regno
)
3701 if (regno
>= the_low_target
.num_regs
)
3703 if ((*the_low_target
.cannot_fetch_register
) (regno
))
3706 regaddr
= register_addr (regno
);
3710 pid
= lwpid_of (get_thread_lwp (current_inferior
));
3711 size
= ((register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
3712 & - sizeof (PTRACE_XFER_TYPE
));
3713 buf
= alloca (size
);
3714 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
3717 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
3718 ptrace (PTRACE_PEEKUSER
, pid
,
3719 /* Coerce to a uintptr_t first to avoid potential gcc warning
3720 of coercing an 8 byte integer to a 4 byte pointer. */
3721 (PTRACE_ARG3_TYPE
) (uintptr_t) regaddr
, 0);
3722 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
3724 error ("reading register %d: %s", regno
, strerror (errno
));
3727 if (the_low_target
.supply_ptrace_register
)
3728 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
3730 supply_register (regcache
, regno
, buf
);
3733 /* Fetch all registers, or just one, from the child process. */
3735 usr_fetch_inferior_registers (struct regcache
*regcache
, int regno
)
3738 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
3739 fetch_register (regcache
, regno
);
3741 fetch_register (regcache
, regno
);
3744 /* Store our register values back into the inferior.
3745 If REGNO is -1, do this for all registers.
3746 Otherwise, REGNO specifies which register (so we can save time). */
3748 usr_store_inferior_registers (struct regcache
*regcache
, int regno
)
3757 if (regno
>= the_low_target
.num_regs
)
3760 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
3763 regaddr
= register_addr (regno
);
3767 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
3768 & - sizeof (PTRACE_XFER_TYPE
);
3769 buf
= alloca (size
);
3770 memset (buf
, 0, size
);
3772 if (the_low_target
.collect_ptrace_register
)
3773 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
3775 collect_register (regcache
, regno
, buf
);
3777 pid
= lwpid_of (get_thread_lwp (current_inferior
));
3778 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
3781 ptrace (PTRACE_POKEUSER
, pid
,
3782 /* Coerce to a uintptr_t first to avoid potential gcc warning
3783 about coercing an 8 byte integer to a 4 byte pointer. */
3784 (PTRACE_ARG3_TYPE
) (uintptr_t) regaddr
,
3785 (PTRACE_ARG4_TYPE
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
3788 /* At this point, ESRCH should mean the process is
3789 already gone, in which case we simply ignore attempts
3790 to change its registers. See also the related
3791 comment in linux_resume_one_lwp. */
3795 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
3796 error ("writing register %d: %s", regno
, strerror (errno
));
3798 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
3802 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
3803 usr_store_inferior_registers (regcache
, regno
);
3805 #endif /* HAVE_LINUX_USRREGS */
3809 #ifdef HAVE_LINUX_REGSETS
3812 regsets_fetch_inferior_registers (struct regcache
*regcache
)
3814 struct regset_info
*regset
;
3815 int saw_general_regs
= 0;
3819 regset
= target_regsets
;
3821 pid
= lwpid_of (get_thread_lwp (current_inferior
));
3822 while (regset
->size
>= 0)
3827 if (regset
->size
== 0 || disabled_regsets
[regset
- target_regsets
])
3833 buf
= xmalloc (regset
->size
);
3835 nt_type
= regset
->nt_type
;
3839 iov
.iov_len
= regset
->size
;
3840 data
= (void *) &iov
;
3846 res
= ptrace (regset
->get_request
, pid
, nt_type
, data
);
3848 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
3854 /* If we get EIO on a regset, do not try it again for
3856 disabled_regsets
[regset
- target_regsets
] = 1;
3863 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
3868 else if (regset
->type
== GENERAL_REGS
)
3869 saw_general_regs
= 1;
3870 regset
->store_function (regcache
, buf
);
3874 if (saw_general_regs
)
3881 regsets_store_inferior_registers (struct regcache
*regcache
)
3883 struct regset_info
*regset
;
3884 int saw_general_regs
= 0;
3888 regset
= target_regsets
;
3890 pid
= lwpid_of (get_thread_lwp (current_inferior
));
3891 while (regset
->size
>= 0)
3896 if (regset
->size
== 0 || disabled_regsets
[regset
- target_regsets
])
3902 buf
= xmalloc (regset
->size
);
3904 /* First fill the buffer with the current register set contents,
3905 in case there are any items in the kernel's regset that are
3906 not in gdbserver's regcache. */
3908 nt_type
= regset
->nt_type
;
3912 iov
.iov_len
= regset
->size
;
3913 data
= (void *) &iov
;
3919 res
= ptrace (regset
->get_request
, pid
, nt_type
, data
);
3921 res
= ptrace (regset
->get_request
, pid
, &iov
, data
);
3926 /* Then overlay our cached registers on that. */
3927 regset
->fill_function (regcache
, buf
);
3929 /* Only now do we write the register set. */
3931 res
= ptrace (regset
->set_request
, pid
, nt_type
, data
);
3933 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
3941 /* If we get EIO on a regset, do not try it again for
3943 disabled_regsets
[regset
- target_regsets
] = 1;
3947 else if (errno
== ESRCH
)
3949 /* At this point, ESRCH should mean the process is
3950 already gone, in which case we simply ignore attempts
3951 to change its registers. See also the related
3952 comment in linux_resume_one_lwp. */
3958 perror ("Warning: ptrace(regsets_store_inferior_registers)");
3961 else if (regset
->type
== GENERAL_REGS
)
3962 saw_general_regs
= 1;
3966 if (saw_general_regs
)
3973 #endif /* HAVE_LINUX_REGSETS */
3977 linux_fetch_registers (struct regcache
*regcache
, int regno
)
3979 #ifdef HAVE_LINUX_REGSETS
3980 if (regsets_fetch_inferior_registers (regcache
) == 0)
3983 #ifdef HAVE_LINUX_USRREGS
3984 usr_fetch_inferior_registers (regcache
, regno
);
3989 linux_store_registers (struct regcache
*regcache
, int regno
)
3991 #ifdef HAVE_LINUX_REGSETS
3992 if (regsets_store_inferior_registers (regcache
) == 0)
3995 #ifdef HAVE_LINUX_USRREGS
3996 usr_store_inferior_registers (regcache
, regno
);
4001 /* Copy LEN bytes from inferior's memory starting at MEMADDR
4002 to debugger memory starting at MYADDR. */
4005 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
4008 /* Round starting address down to longword boundary. */
4009 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
4010 /* Round ending address up; get number of longwords that makes. */
4012 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4013 / sizeof (PTRACE_XFER_TYPE
);
4014 /* Allocate buffer of that many longwords. */
4015 register PTRACE_XFER_TYPE
*buffer
4016 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
4019 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4021 /* Try using /proc. Don't bother for one word. */
4022 if (len
>= 3 * sizeof (long))
4024 /* We could keep this file open and cache it - possibly one per
4025 thread. That requires some juggling, but is even faster. */
4026 sprintf (filename
, "/proc/%d/mem", pid
);
4027 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4031 /* If pread64 is available, use it. It's faster if the kernel
4032 supports it (only one syscall), and it's 64-bit safe even on
4033 32-bit platforms (for instance, SPARC debugging a SPARC64
4036 if (pread64 (fd
, myaddr
, len
, memaddr
) != len
)
4038 if (lseek (fd
, memaddr
, SEEK_SET
) == -1 || read (fd
, myaddr
, len
) != len
)
4050 /* Read all the longwords */
4051 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
4054 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4055 about coercing an 8 byte integer to a 4 byte pointer. */
4056 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
4057 (PTRACE_ARG3_TYPE
) (uintptr_t) addr
, 0);
4062 /* Copy appropriate bytes out of the buffer. */
4064 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
4070 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
4071 memory at MEMADDR. On failure (cannot write to the inferior)
4072 returns the value of errno. */
4075 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
4078 /* Round starting address down to longword boundary. */
4079 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
4080 /* Round ending address up; get number of longwords that makes. */
4082 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4083 / sizeof (PTRACE_XFER_TYPE
);
4085 /* Allocate buffer of that many longwords. */
4086 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*)
4087 alloca (count
* sizeof (PTRACE_XFER_TYPE
));
4089 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4093 /* Dump up to four bytes. */
4094 unsigned int val
= * (unsigned int *) myaddr
;
4100 val
= val
& 0xffffff;
4101 fprintf (stderr
, "Writing %0*x to 0x%08lx\n", 2 * ((len
< 4) ? len
: 4),
4102 val
, (long)memaddr
);
4105 /* Fill start and end extra bytes of buffer with existing memory data. */
4108 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4109 about coercing an 8 byte integer to a 4 byte pointer. */
4110 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
4111 (PTRACE_ARG3_TYPE
) (uintptr_t) addr
, 0);
4119 = ptrace (PTRACE_PEEKTEXT
, pid
,
4120 /* Coerce to a uintptr_t first to avoid potential gcc warning
4121 about coercing an 8 byte integer to a 4 byte pointer. */
4122 (PTRACE_ARG3_TYPE
) (uintptr_t) (addr
+ (count
- 1)
4123 * sizeof (PTRACE_XFER_TYPE
)),
4129 /* Copy data to be written over corresponding part of buffer. */
4131 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
4134 /* Write the entire buffer. */
4136 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
4139 ptrace (PTRACE_POKETEXT
, pid
,
4140 /* Coerce to a uintptr_t first to avoid potential gcc warning
4141 about coercing an 8 byte integer to a 4 byte pointer. */
4142 (PTRACE_ARG3_TYPE
) (uintptr_t) addr
,
4143 (PTRACE_ARG4_TYPE
) buffer
[i
]);
4151 /* Non-zero if the kernel supports PTRACE_O_TRACEFORK. */
4152 static int linux_supports_tracefork_flag
;
4155 linux_enable_event_reporting (int pid
)
4157 if (!linux_supports_tracefork_flag
)
4160 ptrace (PTRACE_SETOPTIONS
, pid
, 0, (PTRACE_ARG4_TYPE
) PTRACE_O_TRACECLONE
);
4163 /* Helper functions for linux_test_for_tracefork, called via clone (). */
4166 linux_tracefork_grandchild (void *arg
)
4171 #define STACK_SIZE 4096
4174 linux_tracefork_child (void *arg
)
4176 ptrace (PTRACE_TRACEME
, 0, 0, 0);
4177 kill (getpid (), SIGSTOP
);
4179 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
4182 linux_tracefork_grandchild (NULL
);
4184 #else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4187 __clone2 (linux_tracefork_grandchild
, arg
, STACK_SIZE
,
4188 CLONE_VM
| SIGCHLD
, NULL
);
4190 clone (linux_tracefork_grandchild
, (char *) arg
+ STACK_SIZE
,
4191 CLONE_VM
| SIGCHLD
, NULL
);
4194 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4199 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
4200 sure that we can enable the option, and that it had the desired
4204 linux_test_for_tracefork (void)
4206 int child_pid
, ret
, status
;
4208 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4209 char *stack
= xmalloc (STACK_SIZE
* 4);
4210 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4212 linux_supports_tracefork_flag
= 0;
4214 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
4216 child_pid
= fork ();
4218 linux_tracefork_child (NULL
);
4220 #else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4222 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
4224 child_pid
= __clone2 (linux_tracefork_child
, stack
, STACK_SIZE
,
4225 CLONE_VM
| SIGCHLD
, stack
+ STACK_SIZE
* 2);
4226 #else /* !__ia64__ */
4227 child_pid
= clone (linux_tracefork_child
, stack
+ STACK_SIZE
,
4228 CLONE_VM
| SIGCHLD
, stack
+ STACK_SIZE
* 2);
4229 #endif /* !__ia64__ */
4231 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4233 if (child_pid
== -1)
4234 perror_with_name ("clone");
4236 ret
= my_waitpid (child_pid
, &status
, 0);
4238 perror_with_name ("waitpid");
4239 else if (ret
!= child_pid
)
4240 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
4241 if (! WIFSTOPPED (status
))
4242 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
4244 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
4245 (PTRACE_ARG4_TYPE
) PTRACE_O_TRACEFORK
);
4248 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
4251 warning ("linux_test_for_tracefork: failed to kill child");
4255 ret
= my_waitpid (child_pid
, &status
, 0);
4256 if (ret
!= child_pid
)
4257 warning ("linux_test_for_tracefork: failed to wait for killed child");
4258 else if (!WIFSIGNALED (status
))
4259 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
4260 "killed child", status
);
4265 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
4267 warning ("linux_test_for_tracefork: failed to resume child");
4269 ret
= my_waitpid (child_pid
, &status
, 0);
4271 if (ret
== child_pid
&& WIFSTOPPED (status
)
4272 && status
>> 16 == PTRACE_EVENT_FORK
)
4275 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
4276 if (ret
== 0 && second_pid
!= 0)
4280 linux_supports_tracefork_flag
= 1;
4281 my_waitpid (second_pid
, &second_status
, 0);
4282 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
4284 warning ("linux_test_for_tracefork: failed to kill second child");
4285 my_waitpid (second_pid
, &status
, 0);
4289 warning ("linux_test_for_tracefork: unexpected result from waitpid "
4290 "(%d, status 0x%x)", ret
, status
);
4294 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
4296 warning ("linux_test_for_tracefork: failed to kill child");
4297 my_waitpid (child_pid
, &status
, 0);
4299 while (WIFSTOPPED (status
));
4301 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4303 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4308 linux_look_up_symbols (void)
4310 #ifdef USE_THREAD_DB
4311 struct process_info
*proc
= current_process ();
4313 if (proc
->private->thread_db
!= NULL
)
4316 /* If the kernel supports tracing forks then it also supports tracing
4317 clones, and then we don't need to use the magic thread event breakpoint
4318 to learn about threads. */
4319 thread_db_init (!linux_supports_tracefork_flag
);
4324 linux_request_interrupt (void)
4326 extern unsigned long signal_pid
;
4328 if (!ptid_equal (cont_thread
, null_ptid
)
4329 && !ptid_equal (cont_thread
, minus_one_ptid
))
4331 struct lwp_info
*lwp
;
4334 lwp
= get_thread_lwp (current_inferior
);
4335 lwpid
= lwpid_of (lwp
);
4336 kill_lwp (lwpid
, SIGINT
);
4339 kill_lwp (signal_pid
, SIGINT
);
4342 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
4343 to debugger memory starting at MYADDR. */
4346 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
4348 char filename
[PATH_MAX
];
4350 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4352 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
4354 fd
= open (filename
, O_RDONLY
);
4358 if (offset
!= (CORE_ADDR
) 0
4359 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4362 n
= read (fd
, myaddr
, len
);
4369 /* These breakpoint and watchpoint related wrapper functions simply
4370 pass on the function call if the target has registered a
4371 corresponding function. */
4374 linux_insert_point (char type
, CORE_ADDR addr
, int len
)
4376 if (the_low_target
.insert_point
!= NULL
)
4377 return the_low_target
.insert_point (type
, addr
, len
);
4379 /* Unsupported (see target.h). */
4384 linux_remove_point (char type
, CORE_ADDR addr
, int len
)
4386 if (the_low_target
.remove_point
!= NULL
)
4387 return the_low_target
.remove_point (type
, addr
, len
);
4389 /* Unsupported (see target.h). */
4394 linux_stopped_by_watchpoint (void)
4396 struct lwp_info
*lwp
= get_thread_lwp (current_inferior
);
4398 return lwp
->stopped_by_watchpoint
;
4402 linux_stopped_data_address (void)
4404 struct lwp_info
*lwp
= get_thread_lwp (current_inferior
);
4406 return lwp
->stopped_data_address
;
4409 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4410 #if defined(__mcoldfire__)
4411 /* These should really be defined in the kernel's ptrace.h header. */
4412 #define PT_TEXT_ADDR 49*4
4413 #define PT_DATA_ADDR 50*4
4414 #define PT_TEXT_END_ADDR 51*4
4416 #define PT_TEXT_ADDR 220
4417 #define PT_TEXT_END_ADDR 224
4418 #define PT_DATA_ADDR 228
4419 #elif defined(__TMS320C6X__)
4420 #define PT_TEXT_ADDR (0x10000*4)
4421 #define PT_DATA_ADDR (0x10004*4)
4422 #define PT_TEXT_END_ADDR (0x10008*4)
4425 /* Under uClinux, programs are loaded at non-zero offsets, which we need
4426 to tell gdb about. */
4429 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
4431 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
4432 unsigned long text
, text_end
, data
;
4433 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4437 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
4438 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
4439 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
4443 /* Both text and data offsets produced at compile-time (and so
4444 used by gdb) are relative to the beginning of the program,
4445 with the data segment immediately following the text segment.
4446 However, the actual runtime layout in memory may put the data
4447 somewhere else, so when we send gdb a data base-address, we
4448 use the real data base address and subtract the compile-time
4449 data base-address from it (which is just the length of the
4450 text segment). BSS immediately follows data in both
4453 *data_p
= data
- (text_end
- text
);
4463 linux_qxfer_osdata (const char *annex
,
4464 unsigned char *readbuf
, unsigned const char *writebuf
,
4465 CORE_ADDR offset
, int len
)
4467 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4470 /* Convert a native/host siginfo object, into/from the siginfo in the
4471 layout of the inferiors' architecture. */
4474 siginfo_fixup (struct siginfo
*siginfo
, void *inf_siginfo
, int direction
)
4478 if (the_low_target
.siginfo_fixup
!= NULL
)
4479 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
4481 /* If there was no callback, or the callback didn't do anything,
4482 then just do a straight memcpy. */
4486 memcpy (siginfo
, inf_siginfo
, sizeof (struct siginfo
));
4488 memcpy (inf_siginfo
, siginfo
, sizeof (struct siginfo
));
4493 linux_xfer_siginfo (const char *annex
, unsigned char *readbuf
,
4494 unsigned const char *writebuf
, CORE_ADDR offset
, int len
)
4497 struct siginfo siginfo
;
4498 char inf_siginfo
[sizeof (struct siginfo
)];
4500 if (current_inferior
== NULL
)
4503 pid
= lwpid_of (get_thread_lwp (current_inferior
));
4506 fprintf (stderr
, "%s siginfo for lwp %d.\n",
4507 readbuf
!= NULL
? "Reading" : "Writing",
4510 if (offset
>= sizeof (siginfo
))
4513 if (ptrace (PTRACE_GETSIGINFO
, pid
, 0, &siginfo
) != 0)
4516 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
4517 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
4518 inferior with a 64-bit GDBSERVER should look the same as debugging it
4519 with a 32-bit GDBSERVER, we need to convert it. */
4520 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
4522 if (offset
+ len
> sizeof (siginfo
))
4523 len
= sizeof (siginfo
) - offset
;
4525 if (readbuf
!= NULL
)
4526 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
4529 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
4531 /* Convert back to ptrace layout before flushing it out. */
4532 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
4534 if (ptrace (PTRACE_SETSIGINFO
, pid
, 0, &siginfo
) != 0)
4541 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4542 so we notice when children change state; as the handler for the
4543 sigsuspend in my_waitpid. */
4546 sigchld_handler (int signo
)
4548 int old_errno
= errno
;
4554 /* fprintf is not async-signal-safe, so call write
4556 if (write (2, "sigchld_handler\n",
4557 sizeof ("sigchld_handler\n") - 1) < 0)
4558 break; /* just ignore */
4562 if (target_is_async_p ())
4563 async_file_mark (); /* trigger a linux_wait */
4569 linux_supports_non_stop (void)
4575 linux_async (int enable
)
4577 int previous
= (linux_event_pipe
[0] != -1);
4580 fprintf (stderr
, "linux_async (%d), previous=%d\n",
4583 if (previous
!= enable
)
4586 sigemptyset (&mask
);
4587 sigaddset (&mask
, SIGCHLD
);
4589 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4593 if (pipe (linux_event_pipe
) == -1)
4594 fatal ("creating event pipe failed.");
4596 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4597 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4599 /* Register the event loop handler. */
4600 add_file_handler (linux_event_pipe
[0],
4601 handle_target_event
, NULL
);
4603 /* Always trigger a linux_wait. */
4608 delete_file_handler (linux_event_pipe
[0]);
4610 close (linux_event_pipe
[0]);
4611 close (linux_event_pipe
[1]);
4612 linux_event_pipe
[0] = -1;
4613 linux_event_pipe
[1] = -1;
4616 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
4623 linux_start_non_stop (int nonstop
)
4625 /* Register or unregister from event-loop accordingly. */
4626 linux_async (nonstop
);
4631 linux_supports_multi_process (void)
4637 /* Enumerate spufs IDs for process PID. */
4639 spu_enumerate_spu_ids (long pid
, unsigned char *buf
, CORE_ADDR offset
, int len
)
4645 struct dirent
*entry
;
4647 sprintf (path
, "/proc/%ld/fd", pid
);
4648 dir
= opendir (path
);
4653 while ((entry
= readdir (dir
)) != NULL
)
4659 fd
= atoi (entry
->d_name
);
4663 sprintf (path
, "/proc/%ld/fd/%d", pid
, fd
);
4664 if (stat (path
, &st
) != 0)
4666 if (!S_ISDIR (st
.st_mode
))
4669 if (statfs (path
, &stfs
) != 0)
4671 if (stfs
.f_type
!= SPUFS_MAGIC
)
4674 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
4676 *(unsigned int *)(buf
+ pos
- offset
) = fd
;
4686 /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
4687 object type, using the /proc file system. */
4689 linux_qxfer_spu (const char *annex
, unsigned char *readbuf
,
4690 unsigned const char *writebuf
,
4691 CORE_ADDR offset
, int len
)
4693 long pid
= lwpid_of (get_thread_lwp (current_inferior
));
4698 if (!writebuf
&& !readbuf
)
4706 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
4709 sprintf (buf
, "/proc/%ld/fd/%s", pid
, annex
);
4710 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
4715 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4722 ret
= write (fd
, writebuf
, (size_t) len
);
4724 ret
= read (fd
, readbuf
, (size_t) len
);
4730 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
4731 struct target_loadseg
4733 /* Core address to which the segment is mapped. */
4735 /* VMA recorded in the program header. */
4737 /* Size of this segment in memory. */
4741 # if defined PT_GETDSBT
4742 struct target_loadmap
4744 /* Protocol version number, must be zero. */
4746 /* Pointer to the DSBT table, its size, and the DSBT index. */
4747 unsigned *dsbt_table
;
4748 unsigned dsbt_size
, dsbt_index
;
4749 /* Number of segments in this map. */
4751 /* The actual memory map. */
4752 struct target_loadseg segs
[/*nsegs*/];
4754 # define LINUX_LOADMAP PT_GETDSBT
4755 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
4756 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
4758 struct target_loadmap
4760 /* Protocol version number, must be zero. */
4762 /* Number of segments in this map. */
4764 /* The actual memory map. */
4765 struct target_loadseg segs
[/*nsegs*/];
4767 # define LINUX_LOADMAP PTRACE_GETFDPIC
4768 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
4769 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
4773 linux_read_loadmap (const char *annex
, CORE_ADDR offset
,
4774 unsigned char *myaddr
, unsigned int len
)
4776 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4778 struct target_loadmap
*data
= NULL
;
4779 unsigned int actual_length
, copy_length
;
4781 if (strcmp (annex
, "exec") == 0)
4782 addr
= (int) LINUX_LOADMAP_EXEC
;
4783 else if (strcmp (annex
, "interp") == 0)
4784 addr
= (int) LINUX_LOADMAP_INTERP
;
4788 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
4794 actual_length
= sizeof (struct target_loadmap
)
4795 + sizeof (struct target_loadseg
) * data
->nsegs
;
4797 if (offset
< 0 || offset
> actual_length
)
4800 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
4801 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
4805 # define linux_read_loadmap NULL
4806 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
4809 linux_process_qsupported (const char *query
)
4811 if (the_low_target
.process_qsupported
!= NULL
)
4812 the_low_target
.process_qsupported (query
);
4816 linux_supports_tracepoints (void)
4818 if (*the_low_target
.supports_tracepoints
== NULL
)
4821 return (*the_low_target
.supports_tracepoints
) ();
4825 linux_read_pc (struct regcache
*regcache
)
4827 if (the_low_target
.get_pc
== NULL
)
4830 return (*the_low_target
.get_pc
) (regcache
);
4834 linux_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
4836 gdb_assert (the_low_target
.set_pc
!= NULL
);
4838 (*the_low_target
.set_pc
) (regcache
, pc
);
4842 linux_thread_stopped (struct thread_info
*thread
)
4844 return get_thread_lwp (thread
)->stopped
;
4847 /* This exposes stop-all-threads functionality to other modules. */
4850 linux_pause_all (int freeze
)
4852 stop_all_lwps (freeze
, NULL
);
4855 /* This exposes unstop-all-threads functionality to other gdbserver
4859 linux_unpause_all (int unfreeze
)
4861 unstop_all_lwps (unfreeze
, NULL
);
4865 linux_prepare_to_access_memory (void)
4867 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
4870 linux_pause_all (1);
4875 linux_done_accessing_memory (void)
4877 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
4880 linux_unpause_all (1);
4884 linux_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
, CORE_ADDR tpaddr
,
4885 CORE_ADDR collector
,
4888 CORE_ADDR
*jump_entry
,
4889 unsigned char *jjump_pad_insn
,
4890 ULONGEST
*jjump_pad_insn_size
,
4891 CORE_ADDR
*adjusted_insn_addr
,
4892 CORE_ADDR
*adjusted_insn_addr_end
)
4894 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
4895 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
4896 jump_entry
, jjump_pad_insn
, jjump_pad_insn_size
,
4897 adjusted_insn_addr
, adjusted_insn_addr_end
);
4900 static struct emit_ops
*
4901 linux_emit_ops (void)
4903 if (the_low_target
.emit_ops
!= NULL
)
4904 return (*the_low_target
.emit_ops
) ();
4909 static struct target_ops linux_target_ops
= {
4910 linux_create_inferior
,
4919 linux_fetch_registers
,
4920 linux_store_registers
,
4921 linux_prepare_to_access_memory
,
4922 linux_done_accessing_memory
,
4925 linux_look_up_symbols
,
4926 linux_request_interrupt
,
4930 linux_stopped_by_watchpoint
,
4931 linux_stopped_data_address
,
4932 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4937 #ifdef USE_THREAD_DB
4938 thread_db_get_tls_address
,
4943 hostio_last_error_from_errno
,
4946 linux_supports_non_stop
,
4948 linux_start_non_stop
,
4949 linux_supports_multi_process
,
4950 #ifdef USE_THREAD_DB
4951 thread_db_handle_monitor_command
,
4955 linux_common_core_of_thread
,
4957 linux_process_qsupported
,
4958 linux_supports_tracepoints
,
4961 linux_thread_stopped
,
4965 linux_cancel_breakpoints
,
4966 linux_stabilize_threads
,
4967 linux_install_fast_tracepoint_jump_pad
,
4972 linux_init_signals ()
4974 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
4975 to find what the cancel signal actually is. */
4976 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
4977 signal (__SIGRTMIN
+1, SIG_IGN
);
4982 initialize_low (void)
4984 struct sigaction sigchld_action
;
4985 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
4986 set_target_ops (&linux_target_ops
);
4987 set_breakpoint_data (the_low_target
.breakpoint
,
4988 the_low_target
.breakpoint_len
);
4989 linux_init_signals ();
4990 linux_test_for_tracefork ();
4991 #ifdef HAVE_LINUX_REGSETS
4992 for (num_regsets
= 0; target_regsets
[num_regsets
].size
>= 0; num_regsets
++)
4994 disabled_regsets
= xmalloc (num_regsets
);
4997 sigchld_action
.sa_handler
= sigchld_handler
;
4998 sigemptyset (&sigchld_action
.sa_mask
);
4999 sigchld_action
.sa_flags
= SA_RESTART
;
5000 sigaction (SIGCHLD
, &sigchld_action
, NULL
);