1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2015 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "linux-low.h"
21 #include "nat/linux-osdata.h"
25 #include "nat/linux-nat.h"
26 #include "nat/linux-waitpid.h"
28 #include <sys/ptrace.h>
29 #include "nat/linux-ptrace.h"
30 #include "nat/linux-procfs.h"
31 #include "nat/linux-personality.h"
33 #include <sys/ioctl.h>
36 #include <sys/syscall.h>
40 #include <sys/types.h>
45 #include "filestuff.h"
46 #include "tracepoint.h"
49 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
50 then ELFMAG0 will have been defined. If it didn't get included by
51 gdb_proc_service.h then including it will likely introduce a duplicate
52 definition of elf_fpregset_t. */
57 #define SPUFS_MAGIC 0x23c9b64e
60 #ifdef HAVE_PERSONALITY
61 # include <sys/personality.h>
62 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
63 # define ADDR_NO_RANDOMIZE 0x0040000
72 #define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
75 /* This is the kernel's hard limit. Not to be confused with
81 /* Some targets did not define these ptrace constants from the start,
82 so gdbserver defines them locally here. In the future, these may
83 be removed after they are added to asm/ptrace.h. */
84 #if !(defined(PT_TEXT_ADDR) \
85 || defined(PT_DATA_ADDR) \
86 || defined(PT_TEXT_END_ADDR))
87 #if defined(__mcoldfire__)
88 /* These are still undefined in 3.10 kernels. */
89 #define PT_TEXT_ADDR 49*4
90 #define PT_DATA_ADDR 50*4
91 #define PT_TEXT_END_ADDR 51*4
92 /* BFIN already defines these since at least 2.6.32 kernels. */
94 #define PT_TEXT_ADDR 220
95 #define PT_TEXT_END_ADDR 224
96 #define PT_DATA_ADDR 228
97 /* These are still undefined in 3.10 kernels. */
98 #elif defined(__TMS320C6X__)
99 #define PT_TEXT_ADDR (0x10000*4)
100 #define PT_DATA_ADDR (0x10004*4)
101 #define PT_TEXT_END_ADDR (0x10008*4)
105 #ifdef HAVE_LINUX_BTRACE
106 # include "nat/linux-btrace.h"
107 # include "btrace-common.h"
110 #ifndef HAVE_ELF32_AUXV_T
111 /* Copied from glibc's elf.h. */
114 uint32_t a_type
; /* Entry type */
117 uint32_t a_val
; /* Integer value */
118 /* We use to have pointer elements added here. We cannot do that,
119 though, since it does not work when using 32-bit definitions
120 on 64-bit platforms and vice versa. */
125 #ifndef HAVE_ELF64_AUXV_T
126 /* Copied from glibc's elf.h. */
129 uint64_t a_type
; /* Entry type */
132 uint64_t a_val
; /* Integer value */
133 /* We use to have pointer elements added here. We cannot do that,
134 though, since it does not work when using 32-bit definitions
135 on 64-bit platforms and vice versa. */
142 /* See nat/linux-nat.h. */
145 ptid_of_lwp (struct lwp_info
*lwp
)
147 return ptid_of (get_lwp_thread (lwp
));
150 /* See nat/linux-nat.h. */
153 lwp_set_arch_private_info (struct lwp_info
*lwp
,
154 struct arch_lwp_info
*info
)
156 lwp
->arch_private
= info
;
159 /* See nat/linux-nat.h. */
161 struct arch_lwp_info
*
162 lwp_arch_private_info (struct lwp_info
*lwp
)
164 return lwp
->arch_private
;
167 /* See nat/linux-nat.h. */
170 lwp_is_stopped (struct lwp_info
*lwp
)
175 /* See nat/linux-nat.h. */
177 enum target_stop_reason
178 lwp_stop_reason (struct lwp_info
*lwp
)
180 return lwp
->stop_reason
;
183 /* A list of all unknown processes which receive stop signals. Some
184 other process will presumably claim each of these as forked
185 children momentarily. */
187 struct simple_pid_list
189 /* The process ID. */
192 /* The status as reported by waitpid. */
196 struct simple_pid_list
*next
;
198 struct simple_pid_list
*stopped_pids
;
200 /* Trivial list manipulation functions to keep track of a list of new
201 stopped processes. */
204 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
206 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
209 new_pid
->status
= status
;
210 new_pid
->next
= *listp
;
215 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
217 struct simple_pid_list
**p
;
219 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
220 if ((*p
)->pid
== pid
)
222 struct simple_pid_list
*next
= (*p
)->next
;
224 *statusp
= (*p
)->status
;
232 enum stopping_threads_kind
234 /* Not stopping threads presently. */
235 NOT_STOPPING_THREADS
,
237 /* Stopping threads. */
240 /* Stopping and suspending threads. */
241 STOPPING_AND_SUSPENDING_THREADS
244 /* This is set while stop_all_lwps is in effect. */
245 enum stopping_threads_kind stopping_threads
= NOT_STOPPING_THREADS
;
247 /* FIXME make into a target method? */
248 int using_threads
= 1;
250 /* True if we're presently stabilizing threads (moving them out of
252 static int stabilizing_threads
;
254 static void linux_resume_one_lwp (struct lwp_info
*lwp
,
255 int step
, int signal
, siginfo_t
*info
);
256 static void linux_resume (struct thread_resume
*resume_info
, size_t n
);
257 static void stop_all_lwps (int suspend
, struct lwp_info
*except
);
258 static void unstop_all_lwps (int unsuspend
, struct lwp_info
*except
);
259 static int linux_wait_for_event_filtered (ptid_t wait_ptid
, ptid_t filter_ptid
,
260 int *wstat
, int options
);
261 static int linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
);
262 static struct lwp_info
*add_lwp (ptid_t ptid
);
263 static int linux_stopped_by_watchpoint (void);
264 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
265 static void proceed_all_lwps (void);
266 static int finish_step_over (struct lwp_info
*lwp
);
267 static int kill_lwp (unsigned long lwpid
, int signo
);
269 /* When the event-loop is doing a step-over, this points at the thread
271 ptid_t step_over_bkpt
;
273 /* True if the low target can hardware single-step. Such targets
274 don't need a BREAKPOINT_REINSERT_ADDR callback. */
277 can_hardware_single_step (void)
279 return (the_low_target
.breakpoint_reinsert_addr
== NULL
);
282 /* True if the low target supports memory breakpoints. If so, we'll
283 have a GET_PC implementation. */
286 supports_breakpoints (void)
288 return (the_low_target
.get_pc
!= NULL
);
291 /* Returns true if this target can support fast tracepoints. This
292 does not mean that the in-process agent has been loaded in the
296 supports_fast_tracepoints (void)
298 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
301 /* True if LWP is stopped in its stepping range. */
304 lwp_in_step_range (struct lwp_info
*lwp
)
306 CORE_ADDR pc
= lwp
->stop_pc
;
308 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
311 struct pending_signals
315 struct pending_signals
*prev
;
318 /* The read/write ends of the pipe registered as waitable file in the
320 static int linux_event_pipe
[2] = { -1, -1 };
322 /* True if we're currently in async mode. */
323 #define target_is_async_p() (linux_event_pipe[0] != -1)
325 static void send_sigstop (struct lwp_info
*lwp
);
326 static void wait_for_sigstop (void);
328 /* Return non-zero if HEADER is a 64-bit ELF file. */
331 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
333 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
334 && header
->e_ident
[EI_MAG1
] == ELFMAG1
335 && header
->e_ident
[EI_MAG2
] == ELFMAG2
336 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
338 *machine
= header
->e_machine
;
339 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
346 /* Return non-zero if FILE is a 64-bit ELF file,
347 zero if the file is not a 64-bit ELF file,
348 and -1 if the file is not accessible or doesn't exist. */
351 elf_64_file_p (const char *file
, unsigned int *machine
)
356 fd
= open (file
, O_RDONLY
);
360 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
367 return elf_64_header_p (&header
, machine
);
370 /* Accepts an integer PID; Returns true if the executable PID is
371 running is a 64-bit ELF file.. */
374 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
378 sprintf (file
, "/proc/%d/exe", pid
);
379 return elf_64_file_p (file
, machine
);
383 delete_lwp (struct lwp_info
*lwp
)
385 struct thread_info
*thr
= get_lwp_thread (lwp
);
388 debug_printf ("deleting %ld\n", lwpid_of (thr
));
391 free (lwp
->arch_private
);
395 /* Add a process to the common process list, and set its private
398 static struct process_info
*
399 linux_add_process (int pid
, int attached
)
401 struct process_info
*proc
;
403 proc
= add_process (pid
, attached
);
404 proc
->priv
= xcalloc (1, sizeof (*proc
->priv
));
406 /* Set the arch when the first LWP stops. */
407 proc
->priv
->new_inferior
= 1;
409 if (the_low_target
.new_process
!= NULL
)
410 proc
->priv
->arch_private
= the_low_target
.new_process ();
415 static CORE_ADDR
get_pc (struct lwp_info
*lwp
);
417 /* Handle a GNU/Linux extended wait response. If we see a clone
418 event, we need to add the new LWP to our list (and return 0 so as
419 not to report the trap to higher layers). */
422 handle_extended_wait (struct lwp_info
*event_lwp
, int wstat
)
424 int event
= linux_ptrace_get_extended_event (wstat
);
425 struct thread_info
*event_thr
= get_lwp_thread (event_lwp
);
426 struct lwp_info
*new_lwp
;
428 if ((event
== PTRACE_EVENT_FORK
) || (event
== PTRACE_EVENT_VFORK
)
429 || (event
== PTRACE_EVENT_CLONE
))
432 unsigned long new_pid
;
435 /* Get the pid of the new lwp. */
436 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_thr
), (PTRACE_TYPE_ARG3
) 0,
439 /* If we haven't already seen the new PID stop, wait for it now. */
440 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
442 /* The new child has a pending SIGSTOP. We can't affect it until it
443 hits the SIGSTOP, but we're already attached. */
445 ret
= my_waitpid (new_pid
, &status
, __WALL
);
448 perror_with_name ("waiting for new child");
449 else if (ret
!= new_pid
)
450 warning ("wait returned unexpected PID %d", ret
);
451 else if (!WIFSTOPPED (status
))
452 warning ("wait returned unexpected status 0x%x", status
);
455 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
457 struct process_info
*parent_proc
;
458 struct process_info
*child_proc
;
459 struct lwp_info
*child_lwp
;
460 struct thread_info
*child_thr
;
461 struct target_desc
*tdesc
;
463 ptid
= ptid_build (new_pid
, new_pid
, 0);
467 debug_printf ("HEW: Got fork event from LWP %ld, "
469 ptid_get_lwp (ptid_of (event_thr
)),
470 ptid_get_pid (ptid
));
473 /* Add the new process to the tables and clone the breakpoint
474 lists of the parent. We need to do this even if the new process
475 will be detached, since we will need the process object and the
476 breakpoints to remove any breakpoints from memory when we
477 detach, and the client side will access registers. */
478 child_proc
= linux_add_process (new_pid
, 0);
479 gdb_assert (child_proc
!= NULL
);
480 child_lwp
= add_lwp (ptid
);
481 gdb_assert (child_lwp
!= NULL
);
482 child_lwp
->stopped
= 1;
483 child_lwp
->must_set_ptrace_flags
= 1;
484 child_lwp
->status_pending_p
= 0;
485 child_thr
= get_lwp_thread (child_lwp
);
486 child_thr
->last_resume_kind
= resume_stop
;
487 parent_proc
= get_thread_process (event_thr
);
488 child_proc
->attached
= parent_proc
->attached
;
489 clone_all_breakpoints (&child_proc
->breakpoints
,
490 &child_proc
->raw_breakpoints
,
491 parent_proc
->breakpoints
);
493 tdesc
= xmalloc (sizeof (struct target_desc
));
494 copy_target_description (tdesc
, parent_proc
->tdesc
);
495 child_proc
->tdesc
= tdesc
;
497 /* Clone arch-specific process data. */
498 if (the_low_target
.new_fork
!= NULL
)
499 the_low_target
.new_fork (parent_proc
, child_proc
);
501 /* Save fork info in the parent thread. */
502 if (event
== PTRACE_EVENT_FORK
)
503 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_FORKED
;
504 else if (event
== PTRACE_EVENT_VFORK
)
505 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORKED
;
507 event_lwp
->waitstatus
.value
.related_pid
= ptid
;
509 /* The status_pending field contains bits denoting the
510 extended event, so when the pending event is handled,
511 the handler will look at lwp->waitstatus. */
512 event_lwp
->status_pending_p
= 1;
513 event_lwp
->status_pending
= wstat
;
515 /* Report the event. */
520 debug_printf ("HEW: Got clone event "
521 "from LWP %ld, new child is LWP %ld\n",
522 lwpid_of (event_thr
), new_pid
);
524 ptid
= ptid_build (pid_of (event_thr
), new_pid
, 0);
525 new_lwp
= add_lwp (ptid
);
527 /* Either we're going to immediately resume the new thread
528 or leave it stopped. linux_resume_one_lwp is a nop if it
529 thinks the thread is currently running, so set this first
530 before calling linux_resume_one_lwp. */
531 new_lwp
->stopped
= 1;
533 /* If we're suspending all threads, leave this one suspended
535 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
)
536 new_lwp
->suspended
= 1;
538 /* Normally we will get the pending SIGSTOP. But in some cases
539 we might get another signal delivered to the group first.
540 If we do get another signal, be sure not to lose it. */
541 if (WSTOPSIG (status
) != SIGSTOP
)
543 new_lwp
->stop_expected
= 1;
544 new_lwp
->status_pending_p
= 1;
545 new_lwp
->status_pending
= status
;
548 /* Don't report the event. */
551 else if (event
== PTRACE_EVENT_VFORK_DONE
)
553 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
555 /* Report the event. */
559 internal_error (__FILE__
, __LINE__
, _("unknown ptrace event %d"), event
);
562 /* Return the PC as read from the regcache of LWP, without any
566 get_pc (struct lwp_info
*lwp
)
568 struct thread_info
*saved_thread
;
569 struct regcache
*regcache
;
572 if (the_low_target
.get_pc
== NULL
)
575 saved_thread
= current_thread
;
576 current_thread
= get_lwp_thread (lwp
);
578 regcache
= get_thread_regcache (current_thread
, 1);
579 pc
= (*the_low_target
.get_pc
) (regcache
);
582 debug_printf ("pc is 0x%lx\n", (long) pc
);
584 current_thread
= saved_thread
;
588 /* This function should only be called if LWP got a SIGTRAP.
589 The SIGTRAP could mean several things.
591 On i386, where decr_pc_after_break is non-zero:
593 If we were single-stepping this process using PTRACE_SINGLESTEP, we
594 will get only the one SIGTRAP. The value of $eip will be the next
595 instruction. If the instruction we stepped over was a breakpoint,
596 we need to decrement the PC.
598 If we continue the process using PTRACE_CONT, we will get a
599 SIGTRAP when we hit a breakpoint. The value of $eip will be
600 the instruction after the breakpoint (i.e. needs to be
601 decremented). If we report the SIGTRAP to GDB, we must also
602 report the undecremented PC. If the breakpoint is removed, we
603 must resume at the decremented PC.
605 On a non-decr_pc_after_break machine with hardware or kernel
608 If we either single-step a breakpoint instruction, or continue and
609 hit a breakpoint instruction, our PC will point at the breakpoint
613 check_stopped_by_breakpoint (struct lwp_info
*lwp
)
616 CORE_ADDR sw_breakpoint_pc
;
617 struct thread_info
*saved_thread
;
618 #if USE_SIGTRAP_SIGINFO
622 if (the_low_target
.get_pc
== NULL
)
626 sw_breakpoint_pc
= pc
- the_low_target
.decr_pc_after_break
;
628 /* breakpoint_at reads from the current thread. */
629 saved_thread
= current_thread
;
630 current_thread
= get_lwp_thread (lwp
);
632 #if USE_SIGTRAP_SIGINFO
633 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
634 (PTRACE_TYPE_ARG3
) 0, &siginfo
) == 0)
636 if (siginfo
.si_signo
== SIGTRAP
)
638 if (siginfo
.si_code
== GDB_ARCH_TRAP_BRKPT
)
642 struct thread_info
*thr
= get_lwp_thread (lwp
);
644 debug_printf ("CSBB: %s stopped by software breakpoint\n",
645 target_pid_to_str (ptid_of (thr
)));
648 /* Back up the PC if necessary. */
649 if (pc
!= sw_breakpoint_pc
)
651 struct regcache
*regcache
652 = get_thread_regcache (current_thread
, 1);
653 (*the_low_target
.set_pc
) (regcache
, sw_breakpoint_pc
);
656 lwp
->stop_pc
= sw_breakpoint_pc
;
657 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
658 current_thread
= saved_thread
;
661 else if (siginfo
.si_code
== TRAP_HWBKPT
)
665 struct thread_info
*thr
= get_lwp_thread (lwp
);
667 debug_printf ("CSBB: %s stopped by hardware "
668 "breakpoint/watchpoint\n",
669 target_pid_to_str (ptid_of (thr
)));
673 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
674 current_thread
= saved_thread
;
677 else if (siginfo
.si_code
== TRAP_TRACE
)
681 struct thread_info
*thr
= get_lwp_thread (lwp
);
683 debug_printf ("CSBB: %s stopped by trace\n",
684 target_pid_to_str (ptid_of (thr
)));
690 /* We may have just stepped a breakpoint instruction. E.g., in
691 non-stop mode, GDB first tells the thread A to step a range, and
692 then the user inserts a breakpoint inside the range. In that
693 case we need to report the breakpoint PC. */
694 if ((!lwp
->stepping
|| lwp
->stop_pc
== sw_breakpoint_pc
)
695 && (*the_low_target
.breakpoint_at
) (sw_breakpoint_pc
))
699 struct thread_info
*thr
= get_lwp_thread (lwp
);
701 debug_printf ("CSBB: %s stopped by software breakpoint\n",
702 target_pid_to_str (ptid_of (thr
)));
705 /* Back up the PC if necessary. */
706 if (pc
!= sw_breakpoint_pc
)
708 struct regcache
*regcache
709 = get_thread_regcache (current_thread
, 1);
710 (*the_low_target
.set_pc
) (regcache
, sw_breakpoint_pc
);
713 lwp
->stop_pc
= sw_breakpoint_pc
;
714 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
715 current_thread
= saved_thread
;
719 if (hardware_breakpoint_inserted_here (pc
))
723 struct thread_info
*thr
= get_lwp_thread (lwp
);
725 debug_printf ("CSBB: %s stopped by hardware breakpoint\n",
726 target_pid_to_str (ptid_of (thr
)));
730 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
731 current_thread
= saved_thread
;
736 current_thread
= saved_thread
;
740 static struct lwp_info
*
741 add_lwp (ptid_t ptid
)
743 struct lwp_info
*lwp
;
745 lwp
= (struct lwp_info
*) xmalloc (sizeof (*lwp
));
746 memset (lwp
, 0, sizeof (*lwp
));
748 if (the_low_target
.new_thread
!= NULL
)
749 the_low_target
.new_thread (lwp
);
751 lwp
->thread
= add_thread (ptid
, lwp
);
756 /* Start an inferior process and returns its pid.
757 ALLARGS is a vector of program-name and args. */
760 linux_create_inferior (char *program
, char **allargs
)
762 struct lwp_info
*new_lwp
;
765 struct cleanup
*restore_personality
766 = maybe_disable_address_space_randomization (disable_randomization
);
768 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
774 perror_with_name ("fork");
779 ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
781 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
782 signal (__SIGRTMIN
+ 1, SIG_DFL
);
787 /* If gdbserver is connected to gdb via stdio, redirect the inferior's
788 stdout to stderr so that inferior i/o doesn't corrupt the connection.
789 Also, redirect stdin to /dev/null. */
790 if (remote_connection_is_stdio ())
793 open ("/dev/null", O_RDONLY
);
795 if (write (2, "stdin/stdout redirected\n",
796 sizeof ("stdin/stdout redirected\n") - 1) < 0)
798 /* Errors ignored. */;
802 execv (program
, allargs
);
804 execvp (program
, allargs
);
806 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
812 do_cleanups (restore_personality
);
814 linux_add_process (pid
, 0);
816 ptid
= ptid_build (pid
, pid
, 0);
817 new_lwp
= add_lwp (ptid
);
818 new_lwp
->must_set_ptrace_flags
= 1;
823 /* Attach to an inferior process. Returns 0 on success, ERRNO on
827 linux_attach_lwp (ptid_t ptid
)
829 struct lwp_info
*new_lwp
;
830 int lwpid
= ptid_get_lwp (ptid
);
832 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0)
836 new_lwp
= add_lwp (ptid
);
838 /* We need to wait for SIGSTOP before being able to make the next
839 ptrace call on this LWP. */
840 new_lwp
->must_set_ptrace_flags
= 1;
842 if (linux_proc_pid_is_stopped (lwpid
))
845 debug_printf ("Attached to a stopped process\n");
847 /* The process is definitely stopped. It is in a job control
848 stop, unless the kernel predates the TASK_STOPPED /
849 TASK_TRACED distinction, in which case it might be in a
850 ptrace stop. Make sure it is in a ptrace stop; from there we
851 can kill it, signal it, et cetera.
853 First make sure there is a pending SIGSTOP. Since we are
854 already attached, the process can not transition from stopped
855 to running without a PTRACE_CONT; so we know this signal will
856 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
857 probably already in the queue (unless this kernel is old
858 enough to use TASK_STOPPED for ptrace stops); but since
859 SIGSTOP is not an RT signal, it can only be queued once. */
860 kill_lwp (lwpid
, SIGSTOP
);
862 /* Finally, resume the stopped process. This will deliver the
863 SIGSTOP (or a higher priority signal, just like normal
864 PTRACE_ATTACH), which we'll catch later on. */
865 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
868 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
871 There are several cases to consider here:
873 1) gdbserver has already attached to the process and is being notified
874 of a new thread that is being created.
875 In this case we should ignore that SIGSTOP and resume the
876 process. This is handled below by setting stop_expected = 1,
877 and the fact that add_thread sets last_resume_kind ==
880 2) This is the first thread (the process thread), and we're attaching
881 to it via attach_inferior.
882 In this case we want the process thread to stop.
883 This is handled by having linux_attach set last_resume_kind ==
884 resume_stop after we return.
886 If the pid we are attaching to is also the tgid, we attach to and
887 stop all the existing threads. Otherwise, we attach to pid and
888 ignore any other threads in the same group as this pid.
890 3) GDB is connecting to gdbserver and is requesting an enumeration of all
892 In this case we want the thread to stop.
893 FIXME: This case is currently not properly handled.
894 We should wait for the SIGSTOP but don't. Things work apparently
895 because enough time passes between when we ptrace (ATTACH) and when
896 gdb makes the next ptrace call on the thread.
898 On the other hand, if we are currently trying to stop all threads, we
899 should treat the new thread as if we had sent it a SIGSTOP. This works
900 because we are guaranteed that the add_lwp call above added us to the
901 end of the list, and so the new thread has not yet reached
902 wait_for_sigstop (but will). */
903 new_lwp
->stop_expected
= 1;
908 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
909 already attached. Returns true if a new LWP is found, false
913 attach_proc_task_lwp_callback (ptid_t ptid
)
915 /* Is this a new thread? */
916 if (find_thread_ptid (ptid
) == NULL
)
918 int lwpid
= ptid_get_lwp (ptid
);
922 debug_printf ("Found new lwp %d\n", lwpid
);
924 err
= linux_attach_lwp (ptid
);
926 /* Be quiet if we simply raced with the thread exiting. EPERM
927 is returned if the thread's task still exists, and is marked
928 as exited or zombie, as well as other conditions, so in that
929 case, confirm the status in /proc/PID/status. */
931 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
935 debug_printf ("Cannot attach to lwp %d: "
936 "thread is gone (%d: %s)\n",
937 lwpid
, err
, strerror (err
));
942 warning (_("Cannot attach to lwp %d: %s"),
944 linux_ptrace_attach_fail_reason_string (ptid
, err
));
952 /* Attach to PID. If PID is the tgid, attach to it and all
956 linux_attach (unsigned long pid
)
958 ptid_t ptid
= ptid_build (pid
, pid
, 0);
961 /* Attach to PID. We will check for other threads
963 err
= linux_attach_lwp (ptid
);
965 error ("Cannot attach to process %ld: %s",
966 pid
, linux_ptrace_attach_fail_reason_string (ptid
, err
));
968 linux_add_process (pid
, 1);
972 struct thread_info
*thread
;
974 /* Don't ignore the initial SIGSTOP if we just attached to this
975 process. It will be collected by wait shortly. */
976 thread
= find_thread_ptid (ptid_build (pid
, pid
, 0));
977 thread
->last_resume_kind
= resume_stop
;
980 /* We must attach to every LWP. If /proc is mounted, use that to
981 find them now. On the one hand, the inferior may be using raw
982 clone instead of using pthreads. On the other hand, even if it
983 is using pthreads, GDB may not be connected yet (thread_db needs
984 to do symbol lookups, through qSymbol). Also, thread_db walks
985 structures in the inferior's address space to find the list of
986 threads/LWPs, and those structures may well be corrupted. Note
987 that once thread_db is loaded, we'll still use it to list threads
988 and associate pthread info with each LWP. */
989 linux_proc_attach_tgid_threads (pid
, attach_proc_task_lwp_callback
);
1000 second_thread_of_pid_p (struct inferior_list_entry
*entry
, void *args
)
1002 struct counter
*counter
= args
;
1004 if (ptid_get_pid (entry
->id
) == counter
->pid
)
1006 if (++counter
->count
> 1)
1014 last_thread_of_process_p (int pid
)
1016 struct counter counter
= { pid
, 0 };
1018 return (find_inferior (&all_threads
,
1019 second_thread_of_pid_p
, &counter
) == NULL
);
1025 linux_kill_one_lwp (struct lwp_info
*lwp
)
1027 struct thread_info
*thr
= get_lwp_thread (lwp
);
1028 int pid
= lwpid_of (thr
);
1030 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1031 there is no signal context, and ptrace(PTRACE_KILL) (or
1032 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1033 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1034 alternative is to kill with SIGKILL. We only need one SIGKILL
1035 per process, not one for each thread. But since we still support
1036 linuxthreads, and we also support debugging programs using raw
1037 clone without CLONE_THREAD, we send one for each thread. For
1038 years, we used PTRACE_KILL only, so we're being a bit paranoid
1039 about some old kernels where PTRACE_KILL might work better
1040 (dubious if there are any such, but that's why it's paranoia), so
1041 we try SIGKILL first, PTRACE_KILL second, and so we're fine
1045 kill_lwp (pid
, SIGKILL
);
1048 int save_errno
= errno
;
1050 debug_printf ("LKL: kill_lwp (SIGKILL) %s, 0, 0 (%s)\n",
1051 target_pid_to_str (ptid_of (thr
)),
1052 save_errno
? strerror (save_errno
) : "OK");
1056 ptrace (PTRACE_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1059 int save_errno
= errno
;
1061 debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
1062 target_pid_to_str (ptid_of (thr
)),
1063 save_errno
? strerror (save_errno
) : "OK");
1067 /* Kill LWP and wait for it to die. */
1070 kill_wait_lwp (struct lwp_info
*lwp
)
1072 struct thread_info
*thr
= get_lwp_thread (lwp
);
1073 int pid
= ptid_get_pid (ptid_of (thr
));
1074 int lwpid
= ptid_get_lwp (ptid_of (thr
));
1079 debug_printf ("kwl: killing lwp %d, for pid: %d\n", lwpid
, pid
);
1083 linux_kill_one_lwp (lwp
);
1085 /* Make sure it died. Notes:
1087 - The loop is most likely unnecessary.
1089 - We don't use linux_wait_for_event as that could delete lwps
1090 while we're iterating over them. We're not interested in
1091 any pending status at this point, only in making sure all
1092 wait status on the kernel side are collected until the
1095 - We don't use __WALL here as the __WALL emulation relies on
1096 SIGCHLD, and killing a stopped process doesn't generate
1097 one, nor an exit status.
1099 res
= my_waitpid (lwpid
, &wstat
, 0);
1100 if (res
== -1 && errno
== ECHILD
)
1101 res
= my_waitpid (lwpid
, &wstat
, __WCLONE
);
1102 } while (res
> 0 && WIFSTOPPED (wstat
));
1104 gdb_assert (res
> 0);
1107 /* Callback for `find_inferior'. Kills an lwp of a given process,
1108 except the leader. */
1111 kill_one_lwp_callback (struct inferior_list_entry
*entry
, void *args
)
1113 struct thread_info
*thread
= (struct thread_info
*) entry
;
1114 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1115 int pid
= * (int *) args
;
1117 if (ptid_get_pid (entry
->id
) != pid
)
1120 /* We avoid killing the first thread here, because of a Linux kernel (at
1121 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1122 the children get a chance to be reaped, it will remain a zombie
1125 if (lwpid_of (thread
) == pid
)
1128 debug_printf ("lkop: is last of process %s\n",
1129 target_pid_to_str (entry
->id
));
1133 kill_wait_lwp (lwp
);
1138 linux_kill (int pid
)
1140 struct process_info
*process
;
1141 struct lwp_info
*lwp
;
1143 process
= find_process_pid (pid
);
1144 if (process
== NULL
)
1147 /* If we're killing a running inferior, make sure it is stopped
1148 first, as PTRACE_KILL will not work otherwise. */
1149 stop_all_lwps (0, NULL
);
1151 find_inferior (&all_threads
, kill_one_lwp_callback
, &pid
);
1153 /* See the comment in linux_kill_one_lwp. We did not kill the first
1154 thread in the list, so do so now. */
1155 lwp
= find_lwp_pid (pid_to_ptid (pid
));
1160 debug_printf ("lk_1: cannot find lwp for pid: %d\n",
1164 kill_wait_lwp (lwp
);
1166 the_target
->mourn (process
);
1168 /* Since we presently can only stop all lwps of all processes, we
1169 need to unstop lwps of other processes. */
1170 unstop_all_lwps (0, NULL
);
1174 /* Get pending signal of THREAD, for detaching purposes. This is the
1175 signal the thread last stopped for, which we need to deliver to the
1176 thread when detaching, otherwise, it'd be suppressed/lost. */
1179 get_detach_signal (struct thread_info
*thread
)
1181 enum gdb_signal signo
= GDB_SIGNAL_0
;
1183 struct lwp_info
*lp
= get_thread_lwp (thread
);
1185 if (lp
->status_pending_p
)
1186 status
= lp
->status_pending
;
1189 /* If the thread had been suspended by gdbserver, and it stopped
1190 cleanly, then it'll have stopped with SIGSTOP. But we don't
1191 want to deliver that SIGSTOP. */
1192 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1193 || thread
->last_status
.value
.sig
== GDB_SIGNAL_0
)
1196 /* Otherwise, we may need to deliver the signal we
1198 status
= lp
->last_status
;
1201 if (!WIFSTOPPED (status
))
1204 debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1205 target_pid_to_str (ptid_of (thread
)));
1209 /* Extended wait statuses aren't real SIGTRAPs. */
1210 if (WSTOPSIG (status
) == SIGTRAP
&& linux_is_extended_waitstatus (status
))
1213 debug_printf ("GPS: lwp %s had stopped with extended "
1214 "status: no pending signal\n",
1215 target_pid_to_str (ptid_of (thread
)));
1219 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1221 if (program_signals_p
&& !program_signals
[signo
])
1224 debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1225 target_pid_to_str (ptid_of (thread
)),
1226 gdb_signal_to_string (signo
));
1229 else if (!program_signals_p
1230 /* If we have no way to know which signals GDB does not
1231 want to have passed to the program, assume
1232 SIGTRAP/SIGINT, which is GDB's default. */
1233 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1236 debug_printf ("GPS: lwp %s had signal %s, "
1237 "but we don't know if we should pass it. "
1238 "Default to not.\n",
1239 target_pid_to_str (ptid_of (thread
)),
1240 gdb_signal_to_string (signo
));
1246 debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1247 target_pid_to_str (ptid_of (thread
)),
1248 gdb_signal_to_string (signo
));
1250 return WSTOPSIG (status
);
1255 linux_detach_one_lwp (struct inferior_list_entry
*entry
, void *args
)
1257 struct thread_info
*thread
= (struct thread_info
*) entry
;
1258 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1259 int pid
= * (int *) args
;
1262 if (ptid_get_pid (entry
->id
) != pid
)
1265 /* If there is a pending SIGSTOP, get rid of it. */
1266 if (lwp
->stop_expected
)
1269 debug_printf ("Sending SIGCONT to %s\n",
1270 target_pid_to_str (ptid_of (thread
)));
1272 kill_lwp (lwpid_of (thread
), SIGCONT
);
1273 lwp
->stop_expected
= 0;
1276 /* Flush any pending changes to the process's registers. */
1277 regcache_invalidate_thread (thread
);
1279 /* Pass on any pending signal for this thread. */
1280 sig
= get_detach_signal (thread
);
1282 /* Finally, let it resume. */
1283 if (the_low_target
.prepare_to_resume
!= NULL
)
1284 the_low_target
.prepare_to_resume (lwp
);
1285 if (ptrace (PTRACE_DETACH
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
1286 (PTRACE_TYPE_ARG4
) (long) sig
) < 0)
1287 error (_("Can't detach %s: %s"),
1288 target_pid_to_str (ptid_of (thread
)),
1296 linux_detach (int pid
)
1298 struct process_info
*process
;
1300 process
= find_process_pid (pid
);
1301 if (process
== NULL
)
1304 /* Stop all threads before detaching. First, ptrace requires that
1305 the thread is stopped to sucessfully detach. Second, thread_db
1306 may need to uninstall thread event breakpoints from memory, which
1307 only works with a stopped process anyway. */
1308 stop_all_lwps (0, NULL
);
1310 #ifdef USE_THREAD_DB
1311 thread_db_detach (process
);
1314 /* Stabilize threads (move out of jump pads). */
1315 stabilize_threads ();
1317 find_inferior (&all_threads
, linux_detach_one_lwp
, &pid
);
1319 the_target
->mourn (process
);
1321 /* Since we presently can only stop all lwps of all processes, we
1322 need to unstop lwps of other processes. */
1323 unstop_all_lwps (0, NULL
);
1327 /* Remove all LWPs that belong to process PROC from the lwp list. */
1330 delete_lwp_callback (struct inferior_list_entry
*entry
, void *proc
)
1332 struct thread_info
*thread
= (struct thread_info
*) entry
;
1333 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1334 struct process_info
*process
= proc
;
1336 if (pid_of (thread
) == pid_of (process
))
1343 linux_mourn (struct process_info
*process
)
1345 struct process_info_private
*priv
;
1347 #ifdef USE_THREAD_DB
1348 thread_db_mourn (process
);
1351 find_inferior (&all_threads
, delete_lwp_callback
, process
);
1353 /* Freeing all private data. */
1354 priv
= process
->priv
;
1355 free (priv
->arch_private
);
1357 process
->priv
= NULL
;
1359 remove_process (process
);
1363 linux_join (int pid
)
1368 ret
= my_waitpid (pid
, &status
, 0);
1369 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1371 } while (ret
!= -1 || errno
!= ECHILD
);
1374 /* Return nonzero if the given thread is still alive. */
1376 linux_thread_alive (ptid_t ptid
)
1378 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1380 /* We assume we always know if a thread exits. If a whole process
1381 exited but we still haven't been able to report it to GDB, we'll
1382 hold on to the last lwp of the dead process. */
1389 /* Return 1 if this lwp still has an interesting status pending. If
1390 not (e.g., it had stopped for a breakpoint that is gone), return
1394 thread_still_has_status_pending_p (struct thread_info
*thread
)
1396 struct lwp_info
*lp
= get_thread_lwp (thread
);
1398 if (!lp
->status_pending_p
)
1401 /* If we got a `vCont;t', but we haven't reported a stop yet, do
1402 report any status pending the LWP may have. */
1403 if (thread
->last_resume_kind
== resume_stop
1404 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1407 if (thread
->last_resume_kind
!= resume_stop
1408 && (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1409 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
))
1411 struct thread_info
*saved_thread
;
1415 gdb_assert (lp
->last_status
!= 0);
1419 saved_thread
= current_thread
;
1420 current_thread
= thread
;
1422 if (pc
!= lp
->stop_pc
)
1425 debug_printf ("PC of %ld changed\n",
1430 #if !USE_SIGTRAP_SIGINFO
1431 else if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1432 && !(*the_low_target
.breakpoint_at
) (pc
))
1435 debug_printf ("previous SW breakpoint of %ld gone\n",
1439 else if (lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
1440 && !hardware_breakpoint_inserted_here (pc
))
1443 debug_printf ("previous HW breakpoint of %ld gone\n",
1449 current_thread
= saved_thread
;
1454 debug_printf ("discarding pending breakpoint status\n");
1455 lp
->status_pending_p
= 0;
1463 /* Return 1 if this lwp has an interesting status pending. */
1465 status_pending_p_callback (struct inferior_list_entry
*entry
, void *arg
)
1467 struct thread_info
*thread
= (struct thread_info
*) entry
;
1468 struct lwp_info
*lp
= get_thread_lwp (thread
);
1469 ptid_t ptid
= * (ptid_t
*) arg
;
1471 /* Check if we're only interested in events from a specific process
1472 or a specific LWP. */
1473 if (!ptid_match (ptid_of (thread
), ptid
))
1476 if (lp
->status_pending_p
1477 && !thread_still_has_status_pending_p (thread
))
1479 linux_resume_one_lwp (lp
, lp
->stepping
, GDB_SIGNAL_0
, NULL
);
1483 return lp
->status_pending_p
;
1487 same_lwp (struct inferior_list_entry
*entry
, void *data
)
1489 ptid_t ptid
= *(ptid_t
*) data
;
1492 if (ptid_get_lwp (ptid
) != 0)
1493 lwp
= ptid_get_lwp (ptid
);
1495 lwp
= ptid_get_pid (ptid
);
1497 if (ptid_get_lwp (entry
->id
) == lwp
)
1504 find_lwp_pid (ptid_t ptid
)
1506 struct inferior_list_entry
*thread
1507 = find_inferior (&all_threads
, same_lwp
, &ptid
);
1512 return get_thread_lwp ((struct thread_info
*) thread
);
1515 /* Return the number of known LWPs in the tgid given by PID. */
1520 struct inferior_list_entry
*inf
, *tmp
;
1523 ALL_INFERIORS (&all_threads
, inf
, tmp
)
1525 if (ptid_get_pid (inf
->id
) == pid
)
1532 /* The arguments passed to iterate_over_lwps. */
1534 struct iterate_over_lwps_args
1536 /* The FILTER argument passed to iterate_over_lwps. */
1539 /* The CALLBACK argument passed to iterate_over_lwps. */
1540 iterate_over_lwps_ftype
*callback
;
1542 /* The DATA argument passed to iterate_over_lwps. */
1546 /* Callback for find_inferior used by iterate_over_lwps to filter
1547 calls to the callback supplied to that function. Returning a
1548 nonzero value causes find_inferiors to stop iterating and return
1549 the current inferior_list_entry. Returning zero indicates that
1550 find_inferiors should continue iterating. */
1553 iterate_over_lwps_filter (struct inferior_list_entry
*entry
, void *args_p
)
1555 struct iterate_over_lwps_args
*args
1556 = (struct iterate_over_lwps_args
*) args_p
;
1558 if (ptid_match (entry
->id
, args
->filter
))
1560 struct thread_info
*thr
= (struct thread_info
*) entry
;
1561 struct lwp_info
*lwp
= get_thread_lwp (thr
);
1563 return (*args
->callback
) (lwp
, args
->data
);
1569 /* See nat/linux-nat.h. */
1572 iterate_over_lwps (ptid_t filter
,
1573 iterate_over_lwps_ftype callback
,
1576 struct iterate_over_lwps_args args
= {filter
, callback
, data
};
1577 struct inferior_list_entry
*entry
;
1579 entry
= find_inferior (&all_threads
, iterate_over_lwps_filter
, &args
);
1583 return get_thread_lwp ((struct thread_info
*) entry
);
1586 /* Detect zombie thread group leaders, and "exit" them. We can't reap
1587 their exits until all other threads in the group have exited. */
1590 check_zombie_leaders (void)
1592 struct process_info
*proc
, *tmp
;
1594 ALL_PROCESSES (proc
, tmp
)
1596 pid_t leader_pid
= pid_of (proc
);
1597 struct lwp_info
*leader_lp
;
1599 leader_lp
= find_lwp_pid (pid_to_ptid (leader_pid
));
1602 debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1603 "num_lwps=%d, zombie=%d\n",
1604 leader_pid
, leader_lp
!= NULL
, num_lwps (leader_pid
),
1605 linux_proc_pid_is_zombie (leader_pid
));
1607 if (leader_lp
!= NULL
1608 /* Check if there are other threads in the group, as we may
1609 have raced with the inferior simply exiting. */
1610 && !last_thread_of_process_p (leader_pid
)
1611 && linux_proc_pid_is_zombie (leader_pid
))
1613 /* A leader zombie can mean one of two things:
1615 - It exited, and there's an exit status pending
1616 available, or only the leader exited (not the whole
1617 program). In the latter case, we can't waitpid the
1618 leader's exit status until all other threads are gone.
1620 - There are 3 or more threads in the group, and a thread
1621 other than the leader exec'd. On an exec, the Linux
1622 kernel destroys all other threads (except the execing
1623 one) in the thread group, and resets the execing thread's
1624 tid to the tgid. No exit notification is sent for the
1625 execing thread -- from the ptracer's perspective, it
1626 appears as though the execing thread just vanishes.
1627 Until we reap all other threads except the leader and the
1628 execing thread, the leader will be zombie, and the
1629 execing thread will be in `D (disc sleep)'. As soon as
1630 all other threads are reaped, the execing thread changes
1631 it's tid to the tgid, and the previous (zombie) leader
1632 vanishes, giving place to the "new" leader. We could try
1633 distinguishing the exit and exec cases, by waiting once
1634 more, and seeing if something comes out, but it doesn't
1635 sound useful. The previous leader _does_ go away, and
1636 we'll re-add the new one once we see the exec event
1637 (which is just the same as what would happen if the
1638 previous leader did exit voluntarily before some other
1643 "CZL: Thread group leader %d zombie "
1644 "(it exited, or another thread execd).\n",
1647 delete_lwp (leader_lp
);
1652 /* Callback for `find_inferior'. Returns the first LWP that is not
1653 stopped. ARG is a PTID filter. */
1656 not_stopped_callback (struct inferior_list_entry
*entry
, void *arg
)
1658 struct thread_info
*thr
= (struct thread_info
*) entry
;
1659 struct lwp_info
*lwp
;
1660 ptid_t filter
= *(ptid_t
*) arg
;
1662 if (!ptid_match (ptid_of (thr
), filter
))
1665 lwp
= get_thread_lwp (thr
);
1672 /* This function should only be called if the LWP got a SIGTRAP.
1674 Handle any tracepoint steps or hits. Return true if a tracepoint
1675 event was handled, 0 otherwise. */
1678 handle_tracepoints (struct lwp_info
*lwp
)
1680 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1681 int tpoint_related_event
= 0;
1683 gdb_assert (lwp
->suspended
== 0);
1685 /* If this tracepoint hit causes a tracing stop, we'll immediately
1686 uninsert tracepoints. To do this, we temporarily pause all
1687 threads, unpatch away, and then unpause threads. We need to make
1688 sure the unpausing doesn't resume LWP too. */
1691 /* And we need to be sure that any all-threads-stopping doesn't try
1692 to move threads out of the jump pads, as it could deadlock the
1693 inferior (LWP could be in the jump pad, maybe even holding the
1696 /* Do any necessary step collect actions. */
1697 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1699 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1701 /* See if we just hit a tracepoint and do its main collect
1703 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1707 gdb_assert (lwp
->suspended
== 0);
1708 gdb_assert (!stabilizing_threads
|| lwp
->collecting_fast_tracepoint
);
1710 if (tpoint_related_event
)
1713 debug_printf ("got a tracepoint event\n");
1720 /* Convenience wrapper. Returns true if LWP is presently collecting a
1724 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
1725 struct fast_tpoint_collect_status
*status
)
1727 CORE_ADDR thread_area
;
1728 struct thread_info
*thread
= get_lwp_thread (lwp
);
1730 if (the_low_target
.get_thread_area
== NULL
)
1733 /* Get the thread area address. This is used to recognize which
1734 thread is which when tracing with the in-process agent library.
1735 We don't read anything from the address, and treat it as opaque;
1736 it's the address itself that we assume is unique per-thread. */
1737 if ((*the_low_target
.get_thread_area
) (lwpid_of (thread
), &thread_area
) == -1)
1740 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
1743 /* The reason we resume in the caller, is because we want to be able
1744 to pass lwp->status_pending as WSTAT, and we need to clear
1745 status_pending_p before resuming, otherwise, linux_resume_one_lwp
1746 refuses to resume. */
1749 maybe_move_out_of_jump_pad (struct lwp_info
*lwp
, int *wstat
)
1751 struct thread_info
*saved_thread
;
1753 saved_thread
= current_thread
;
1754 current_thread
= get_lwp_thread (lwp
);
1757 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
1758 && supports_fast_tracepoints ()
1759 && agent_loaded_p ())
1761 struct fast_tpoint_collect_status status
;
1765 debug_printf ("Checking whether LWP %ld needs to move out of the "
1767 lwpid_of (current_thread
));
1769 r
= linux_fast_tracepoint_collecting (lwp
, &status
);
1772 || (WSTOPSIG (*wstat
) != SIGILL
1773 && WSTOPSIG (*wstat
) != SIGFPE
1774 && WSTOPSIG (*wstat
) != SIGSEGV
1775 && WSTOPSIG (*wstat
) != SIGBUS
))
1777 lwp
->collecting_fast_tracepoint
= r
;
1781 if (r
== 1 && lwp
->exit_jump_pad_bkpt
== NULL
)
1783 /* Haven't executed the original instruction yet.
1784 Set breakpoint there, and wait till it's hit,
1785 then single-step until exiting the jump pad. */
1786 lwp
->exit_jump_pad_bkpt
1787 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
1791 debug_printf ("Checking whether LWP %ld needs to move out of "
1792 "the jump pad...it does\n",
1793 lwpid_of (current_thread
));
1794 current_thread
= saved_thread
;
1801 /* If we get a synchronous signal while collecting, *and*
1802 while executing the (relocated) original instruction,
1803 reset the PC to point at the tpoint address, before
1804 reporting to GDB. Otherwise, it's an IPA lib bug: just
1805 report the signal to GDB, and pray for the best. */
1807 lwp
->collecting_fast_tracepoint
= 0;
1810 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
1811 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
1814 struct regcache
*regcache
;
1816 /* The si_addr on a few signals references the address
1817 of the faulting instruction. Adjust that as
1819 if ((WSTOPSIG (*wstat
) == SIGILL
1820 || WSTOPSIG (*wstat
) == SIGFPE
1821 || WSTOPSIG (*wstat
) == SIGBUS
1822 || WSTOPSIG (*wstat
) == SIGSEGV
)
1823 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
1824 (PTRACE_TYPE_ARG3
) 0, &info
) == 0
1825 /* Final check just to make sure we don't clobber
1826 the siginfo of non-kernel-sent signals. */
1827 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
1829 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
1830 ptrace (PTRACE_SETSIGINFO
, lwpid_of (current_thread
),
1831 (PTRACE_TYPE_ARG3
) 0, &info
);
1834 regcache
= get_thread_regcache (current_thread
, 1);
1835 (*the_low_target
.set_pc
) (regcache
, status
.tpoint_addr
);
1836 lwp
->stop_pc
= status
.tpoint_addr
;
1838 /* Cancel any fast tracepoint lock this thread was
1840 force_unlock_trace_buffer ();
1843 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
1846 debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
1847 "stopping all threads momentarily.\n");
1849 stop_all_lwps (1, lwp
);
1851 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
1852 lwp
->exit_jump_pad_bkpt
= NULL
;
1854 unstop_all_lwps (1, lwp
);
1856 gdb_assert (lwp
->suspended
>= 0);
1862 debug_printf ("Checking whether LWP %ld needs to move out of the "
1864 lwpid_of (current_thread
));
1866 current_thread
= saved_thread
;
1870 /* Enqueue one signal in the "signals to report later when out of the
1874 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1876 struct pending_signals
*p_sig
;
1877 struct thread_info
*thread
= get_lwp_thread (lwp
);
1880 debug_printf ("Deferring signal %d for LWP %ld.\n",
1881 WSTOPSIG (*wstat
), lwpid_of (thread
));
1885 struct pending_signals
*sig
;
1887 for (sig
= lwp
->pending_signals_to_report
;
1890 debug_printf (" Already queued %d\n",
1893 debug_printf (" (no more currently queued signals)\n");
1896 /* Don't enqueue non-RT signals if they are already in the deferred
1897 queue. (SIGSTOP being the easiest signal to see ending up here
1899 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
1901 struct pending_signals
*sig
;
1903 for (sig
= lwp
->pending_signals_to_report
;
1907 if (sig
->signal
== WSTOPSIG (*wstat
))
1910 debug_printf ("Not requeuing already queued non-RT signal %d"
1919 p_sig
= xmalloc (sizeof (*p_sig
));
1920 p_sig
->prev
= lwp
->pending_signals_to_report
;
1921 p_sig
->signal
= WSTOPSIG (*wstat
);
1922 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1923 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
1926 lwp
->pending_signals_to_report
= p_sig
;
1929 /* Dequeue one signal from the "signals to report later when out of
1930 the jump pad" list. */
1933 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1935 struct thread_info
*thread
= get_lwp_thread (lwp
);
1937 if (lwp
->pending_signals_to_report
!= NULL
)
1939 struct pending_signals
**p_sig
;
1941 p_sig
= &lwp
->pending_signals_to_report
;
1942 while ((*p_sig
)->prev
!= NULL
)
1943 p_sig
= &(*p_sig
)->prev
;
1945 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
1946 if ((*p_sig
)->info
.si_signo
!= 0)
1947 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
1953 debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
1954 WSTOPSIG (*wstat
), lwpid_of (thread
));
1958 struct pending_signals
*sig
;
1960 for (sig
= lwp
->pending_signals_to_report
;
1963 debug_printf (" Still queued %d\n",
1966 debug_printf (" (no more queued signals)\n");
1975 /* Fetch the possibly triggered data watchpoint info and store it in
1978 On some archs, like x86, that use debug registers to set
1979 watchpoints, it's possible that the way to know which watched
1980 address trapped, is to check the register that is used to select
1981 which address to watch. Problem is, between setting the watchpoint
1982 and reading back which data address trapped, the user may change
1983 the set of watchpoints, and, as a consequence, GDB changes the
1984 debug registers in the inferior. To avoid reading back a stale
1985 stopped-data-address when that happens, we cache in LP the fact
1986 that a watchpoint trapped, and the corresponding data address, as
1987 soon as we see CHILD stop with a SIGTRAP. If GDB changes the debug
1988 registers meanwhile, we have the cached data we can rely on. */
1991 check_stopped_by_watchpoint (struct lwp_info
*child
)
1993 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1995 struct thread_info
*saved_thread
;
1997 saved_thread
= current_thread
;
1998 current_thread
= get_lwp_thread (child
);
2000 if (the_low_target
.stopped_by_watchpoint ())
2002 child
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2004 if (the_low_target
.stopped_data_address
!= NULL
)
2005 child
->stopped_data_address
2006 = the_low_target
.stopped_data_address ();
2008 child
->stopped_data_address
= 0;
2011 current_thread
= saved_thread
;
2014 return child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2017 /* Return the ptrace options that we want to try to enable. */
2020 linux_low_ptrace_options (int attached
)
2025 options
|= PTRACE_O_EXITKILL
;
2027 if (report_fork_events
)
2028 options
|= PTRACE_O_TRACEFORK
;
2030 if (report_vfork_events
)
2031 options
|= (PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEVFORKDONE
);
2036 /* Do low-level handling of the event, and check if we should go on
2037 and pass it to caller code. Return the affected lwp if we are, or
2040 static struct lwp_info
*
2041 linux_low_filter_event (int lwpid
, int wstat
)
2043 struct lwp_info
*child
;
2044 struct thread_info
*thread
;
2045 int have_stop_pc
= 0;
2047 child
= find_lwp_pid (pid_to_ptid (lwpid
));
2049 /* If we didn't find a process, one of two things presumably happened:
2050 - A process we started and then detached from has exited. Ignore it.
2051 - A process we are controlling has forked and the new child's stop
2052 was reported to us by the kernel. Save its PID. */
2053 if (child
== NULL
&& WIFSTOPPED (wstat
))
2055 add_to_pid_list (&stopped_pids
, lwpid
, wstat
);
2058 else if (child
== NULL
)
2061 thread
= get_lwp_thread (child
);
2065 child
->last_status
= wstat
;
2067 /* Check if the thread has exited. */
2068 if ((WIFEXITED (wstat
) || WIFSIGNALED (wstat
)))
2071 debug_printf ("LLFE: %d exited.\n", lwpid
);
2072 if (num_lwps (pid_of (thread
)) > 1)
2075 /* If there is at least one more LWP, then the exit signal was
2076 not the end of the debugged application and should be
2083 /* This was the last lwp in the process. Since events are
2084 serialized to GDB core, and we can't report this one
2085 right now, but GDB core and the other target layers will
2086 want to be notified about the exit code/signal, leave the
2087 status pending for the next time we're able to report
2089 mark_lwp_dead (child
, wstat
);
2094 gdb_assert (WIFSTOPPED (wstat
));
2096 if (WIFSTOPPED (wstat
))
2098 struct process_info
*proc
;
2100 /* Architecture-specific setup after inferior is running. This
2101 needs to happen after we have attached to the inferior and it
2102 is stopped for the first time, but before we access any
2103 inferior registers. */
2104 proc
= find_process_pid (pid_of (thread
));
2105 if (proc
->priv
->new_inferior
)
2107 struct thread_info
*saved_thread
;
2109 saved_thread
= current_thread
;
2110 current_thread
= thread
;
2112 the_low_target
.arch_setup ();
2114 current_thread
= saved_thread
;
2116 proc
->priv
->new_inferior
= 0;
2120 if (WIFSTOPPED (wstat
) && child
->must_set_ptrace_flags
)
2122 struct process_info
*proc
= find_process_pid (pid_of (thread
));
2123 int options
= linux_low_ptrace_options (proc
->attached
);
2125 linux_enable_event_reporting (lwpid
, options
);
2126 child
->must_set_ptrace_flags
= 0;
2129 /* Be careful to not overwrite stop_pc until
2130 check_stopped_by_breakpoint is called. */
2131 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
2132 && linux_is_extended_waitstatus (wstat
))
2134 child
->stop_pc
= get_pc (child
);
2135 if (handle_extended_wait (child
, wstat
))
2137 /* The event has been handled, so just return without
2143 /* Check first whether this was a SW/HW breakpoint before checking
2144 watchpoints, because at least s390 can't tell the data address of
2145 hardware watchpoint hits, and returns stopped-by-watchpoint as
2146 long as there's a watchpoint set. */
2147 if (WIFSTOPPED (wstat
) && linux_wstatus_maybe_breakpoint (wstat
))
2149 if (check_stopped_by_breakpoint (child
))
2153 /* Note that TRAP_HWBKPT can indicate either a hardware breakpoint
2154 or hardware watchpoint. Check which is which if we got
2155 TARGET_STOPPED_BY_HW_BREAKPOINT. */
2156 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
2157 && (child
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
2158 || child
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
))
2159 check_stopped_by_watchpoint (child
);
2162 child
->stop_pc
= get_pc (child
);
2164 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGSTOP
2165 && child
->stop_expected
)
2168 debug_printf ("Expected stop.\n");
2169 child
->stop_expected
= 0;
2171 if (thread
->last_resume_kind
== resume_stop
)
2173 /* We want to report the stop to the core. Treat the
2174 SIGSTOP as a normal event. */
2176 debug_printf ("LLW: resume_stop SIGSTOP caught for %s.\n",
2177 target_pid_to_str (ptid_of (thread
)));
2179 else if (stopping_threads
!= NOT_STOPPING_THREADS
)
2181 /* Stopping threads. We don't want this SIGSTOP to end up
2184 debug_printf ("LLW: SIGSTOP caught for %s "
2185 "while stopping threads.\n",
2186 target_pid_to_str (ptid_of (thread
)));
2191 /* This is a delayed SIGSTOP. Filter out the event. */
2193 debug_printf ("LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2194 child
->stepping
? "step" : "continue",
2195 target_pid_to_str (ptid_of (thread
)));
2197 linux_resume_one_lwp (child
, child
->stepping
, 0, NULL
);
2202 child
->status_pending_p
= 1;
2203 child
->status_pending
= wstat
;
2207 /* Resume LWPs that are currently stopped without any pending status
2208 to report, but are resumed from the core's perspective. */
2211 resume_stopped_resumed_lwps (struct inferior_list_entry
*entry
)
2213 struct thread_info
*thread
= (struct thread_info
*) entry
;
2214 struct lwp_info
*lp
= get_thread_lwp (thread
);
2217 && !lp
->status_pending_p
2218 && thread
->last_resume_kind
!= resume_stop
2219 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2221 int step
= thread
->last_resume_kind
== resume_step
;
2224 debug_printf ("RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
2225 target_pid_to_str (ptid_of (thread
)),
2226 paddress (lp
->stop_pc
),
2229 linux_resume_one_lwp (lp
, step
, GDB_SIGNAL_0
, NULL
);
2233 /* Wait for an event from child(ren) WAIT_PTID, and return any that
2234 match FILTER_PTID (leaving others pending). The PTIDs can be:
2235 minus_one_ptid, to specify any child; a pid PTID, specifying all
2236 lwps of a thread group; or a PTID representing a single lwp. Store
2237 the stop status through the status pointer WSTAT. OPTIONS is
2238 passed to the waitpid call. Return 0 if no event was found and
2239 OPTIONS contains WNOHANG. Return -1 if no unwaited-for children
2240 was found. Return the PID of the stopped child otherwise. */
2243 linux_wait_for_event_filtered (ptid_t wait_ptid
, ptid_t filter_ptid
,
2244 int *wstatp
, int options
)
2246 struct thread_info
*event_thread
;
2247 struct lwp_info
*event_child
, *requested_child
;
2248 sigset_t block_mask
, prev_mask
;
2251 /* N.B. event_thread points to the thread_info struct that contains
2252 event_child. Keep them in sync. */
2253 event_thread
= NULL
;
2255 requested_child
= NULL
;
2257 /* Check for a lwp with a pending status. */
2259 if (ptid_equal (filter_ptid
, minus_one_ptid
) || ptid_is_pid (filter_ptid
))
2261 event_thread
= (struct thread_info
*)
2262 find_inferior (&all_threads
, status_pending_p_callback
, &filter_ptid
);
2263 if (event_thread
!= NULL
)
2264 event_child
= get_thread_lwp (event_thread
);
2265 if (debug_threads
&& event_thread
)
2266 debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread
));
2268 else if (!ptid_equal (filter_ptid
, null_ptid
))
2270 requested_child
= find_lwp_pid (filter_ptid
);
2272 if (stopping_threads
== NOT_STOPPING_THREADS
2273 && requested_child
->status_pending_p
2274 && requested_child
->collecting_fast_tracepoint
)
2276 enqueue_one_deferred_signal (requested_child
,
2277 &requested_child
->status_pending
);
2278 requested_child
->status_pending_p
= 0;
2279 requested_child
->status_pending
= 0;
2280 linux_resume_one_lwp (requested_child
, 0, 0, NULL
);
2283 if (requested_child
->suspended
2284 && requested_child
->status_pending_p
)
2286 internal_error (__FILE__
, __LINE__
,
2287 "requesting an event out of a"
2288 " suspended child?");
2291 if (requested_child
->status_pending_p
)
2293 event_child
= requested_child
;
2294 event_thread
= get_lwp_thread (event_child
);
2298 if (event_child
!= NULL
)
2301 debug_printf ("Got an event from pending child %ld (%04x)\n",
2302 lwpid_of (event_thread
), event_child
->status_pending
);
2303 *wstatp
= event_child
->status_pending
;
2304 event_child
->status_pending_p
= 0;
2305 event_child
->status_pending
= 0;
2306 current_thread
= event_thread
;
2307 return lwpid_of (event_thread
);
2310 /* But if we don't find a pending event, we'll have to wait.
2312 We only enter this loop if no process has a pending wait status.
2313 Thus any action taken in response to a wait status inside this
2314 loop is responding as soon as we detect the status, not after any
2317 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2318 all signals while here. */
2319 sigfillset (&block_mask
);
2320 sigprocmask (SIG_BLOCK
, &block_mask
, &prev_mask
);
2322 /* Always pull all events out of the kernel. We'll randomly select
2323 an event LWP out of all that have events, to prevent
2325 while (event_child
== NULL
)
2329 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2332 - If the thread group leader exits while other threads in the
2333 thread group still exist, waitpid(TGID, ...) hangs. That
2334 waitpid won't return an exit status until the other threads
2335 in the group are reaped.
2337 - When a non-leader thread execs, that thread just vanishes
2338 without reporting an exit (so we'd hang if we waited for it
2339 explicitly in that case). The exec event is reported to
2340 the TGID pid (although we don't currently enable exec
2343 ret
= my_waitpid (-1, wstatp
, options
| WNOHANG
);
2346 debug_printf ("LWFE: waitpid(-1, ...) returned %d, %s\n",
2347 ret
, errno
? strerror (errno
) : "ERRNO-OK");
2353 debug_printf ("LLW: waitpid %ld received %s\n",
2354 (long) ret
, status_to_str (*wstatp
));
2357 /* Filter all events. IOW, leave all events pending. We'll
2358 randomly select an event LWP out of all that have events
2360 linux_low_filter_event (ret
, *wstatp
);
2361 /* Retry until nothing comes out of waitpid. A single
2362 SIGCHLD can indicate more than one child stopped. */
2366 /* Now that we've pulled all events out of the kernel, resume
2367 LWPs that don't have an interesting event to report. */
2368 if (stopping_threads
== NOT_STOPPING_THREADS
)
2369 for_each_inferior (&all_threads
, resume_stopped_resumed_lwps
);
2371 /* ... and find an LWP with a status to report to the core, if
2373 event_thread
= (struct thread_info
*)
2374 find_inferior (&all_threads
, status_pending_p_callback
, &filter_ptid
);
2375 if (event_thread
!= NULL
)
2377 event_child
= get_thread_lwp (event_thread
);
2378 *wstatp
= event_child
->status_pending
;
2379 event_child
->status_pending_p
= 0;
2380 event_child
->status_pending
= 0;
2384 /* Check for zombie thread group leaders. Those can't be reaped
2385 until all other threads in the thread group are. */
2386 check_zombie_leaders ();
2388 /* If there are no resumed children left in the set of LWPs we
2389 want to wait for, bail. We can't just block in
2390 waitpid/sigsuspend, because lwps might have been left stopped
2391 in trace-stop state, and we'd be stuck forever waiting for
2392 their status to change (which would only happen if we resumed
2393 them). Even if WNOHANG is set, this return code is preferred
2394 over 0 (below), as it is more detailed. */
2395 if ((find_inferior (&all_threads
,
2396 not_stopped_callback
,
2397 &wait_ptid
) == NULL
))
2400 debug_printf ("LLW: exit (no unwaited-for LWP)\n");
2401 sigprocmask (SIG_SETMASK
, &prev_mask
, NULL
);
2405 /* No interesting event to report to the caller. */
2406 if ((options
& WNOHANG
))
2409 debug_printf ("WNOHANG set, no event found\n");
2411 sigprocmask (SIG_SETMASK
, &prev_mask
, NULL
);
2415 /* Block until we get an event reported with SIGCHLD. */
2417 debug_printf ("sigsuspend'ing\n");
2419 sigsuspend (&prev_mask
);
2420 sigprocmask (SIG_SETMASK
, &prev_mask
, NULL
);
2424 sigprocmask (SIG_SETMASK
, &prev_mask
, NULL
);
2426 current_thread
= event_thread
;
2428 /* Check for thread exit. */
2429 if (! WIFSTOPPED (*wstatp
))
2431 gdb_assert (last_thread_of_process_p (pid_of (event_thread
)));
2434 debug_printf ("LWP %d is the last lwp of process. "
2435 "Process %ld exiting.\n",
2436 pid_of (event_thread
), lwpid_of (event_thread
));
2437 return lwpid_of (event_thread
);
2440 return lwpid_of (event_thread
);
2443 /* Wait for an event from child(ren) PTID. PTIDs can be:
2444 minus_one_ptid, to specify any child; a pid PTID, specifying all
2445 lwps of a thread group; or a PTID representing a single lwp. Store
2446 the stop status through the status pointer WSTAT. OPTIONS is
2447 passed to the waitpid call. Return 0 if no event was found and
2448 OPTIONS contains WNOHANG. Return -1 if no unwaited-for children
2449 was found. Return the PID of the stopped child otherwise. */
2452 linux_wait_for_event (ptid_t ptid
, int *wstatp
, int options
)
2454 return linux_wait_for_event_filtered (ptid
, ptid
, wstatp
, options
);
2457 /* Count the LWP's that have had events. */
2460 count_events_callback (struct inferior_list_entry
*entry
, void *data
)
2462 struct thread_info
*thread
= (struct thread_info
*) entry
;
2463 struct lwp_info
*lp
= get_thread_lwp (thread
);
2466 gdb_assert (count
!= NULL
);
2468 /* Count only resumed LWPs that have an event pending. */
2469 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2470 && lp
->status_pending_p
)
2476 /* Select the LWP (if any) that is currently being single-stepped. */
2479 select_singlestep_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
2481 struct thread_info
*thread
= (struct thread_info
*) entry
;
2482 struct lwp_info
*lp
= get_thread_lwp (thread
);
2484 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2485 && thread
->last_resume_kind
== resume_step
2486 && lp
->status_pending_p
)
2492 /* Select the Nth LWP that has had an event. */
2495 select_event_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
2497 struct thread_info
*thread
= (struct thread_info
*) entry
;
2498 struct lwp_info
*lp
= get_thread_lwp (thread
);
2499 int *selector
= data
;
2501 gdb_assert (selector
!= NULL
);
2503 /* Select only resumed LWPs that have an event pending. */
2504 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2505 && lp
->status_pending_p
)
2506 if ((*selector
)-- == 0)
2512 /* Select one LWP out of those that have events pending. */
2515 select_event_lwp (struct lwp_info
**orig_lp
)
2518 int random_selector
;
2519 struct thread_info
*event_thread
= NULL
;
2521 /* In all-stop, give preference to the LWP that is being
2522 single-stepped. There will be at most one, and it's the LWP that
2523 the core is most interested in. If we didn't do this, then we'd
2524 have to handle pending step SIGTRAPs somehow in case the core
2525 later continues the previously-stepped thread, otherwise we'd
2526 report the pending SIGTRAP, and the core, not having stepped the
2527 thread, wouldn't understand what the trap was for, and therefore
2528 would report it to the user as a random signal. */
2532 = (struct thread_info
*) find_inferior (&all_threads
,
2533 select_singlestep_lwp_callback
,
2535 if (event_thread
!= NULL
)
2538 debug_printf ("SEL: Select single-step %s\n",
2539 target_pid_to_str (ptid_of (event_thread
)));
2542 if (event_thread
== NULL
)
2544 /* No single-stepping LWP. Select one at random, out of those
2545 which have had events. */
2547 /* First see how many events we have. */
2548 find_inferior (&all_threads
, count_events_callback
, &num_events
);
2549 gdb_assert (num_events
> 0);
2551 /* Now randomly pick a LWP out of those that have had
2553 random_selector
= (int)
2554 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2556 if (debug_threads
&& num_events
> 1)
2557 debug_printf ("SEL: Found %d SIGTRAP events, selecting #%d\n",
2558 num_events
, random_selector
);
2561 = (struct thread_info
*) find_inferior (&all_threads
,
2562 select_event_lwp_callback
,
2566 if (event_thread
!= NULL
)
2568 struct lwp_info
*event_lp
= get_thread_lwp (event_thread
);
2570 /* Switch the event LWP. */
2571 *orig_lp
= event_lp
;
2575 /* Decrement the suspend count of an LWP. */
2578 unsuspend_one_lwp (struct inferior_list_entry
*entry
, void *except
)
2580 struct thread_info
*thread
= (struct thread_info
*) entry
;
2581 struct lwp_info
*lwp
= get_thread_lwp (thread
);
2583 /* Ignore EXCEPT. */
2589 gdb_assert (lwp
->suspended
>= 0);
2593 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2597 unsuspend_all_lwps (struct lwp_info
*except
)
2599 find_inferior (&all_threads
, unsuspend_one_lwp
, except
);
2602 static void move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
);
2603 static int stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
,
2605 static int lwp_running (struct inferior_list_entry
*entry
, void *data
);
2606 static ptid_t
linux_wait_1 (ptid_t ptid
,
2607 struct target_waitstatus
*ourstatus
,
2608 int target_options
);
2610 /* Stabilize threads (move out of jump pads).
2612 If a thread is midway collecting a fast tracepoint, we need to
2613 finish the collection and move it out of the jump pad before
2614 reporting the signal.
2616 This avoids recursion while collecting (when a signal arrives
2617 midway, and the signal handler itself collects), which would trash
2618 the trace buffer. In case the user set a breakpoint in a signal
2619 handler, this avoids the backtrace showing the jump pad, etc..
2620 Most importantly, there are certain things we can't do safely if
2621 threads are stopped in a jump pad (or in its callee's). For
2624 - starting a new trace run. A thread still collecting the
2625 previous run, could trash the trace buffer when resumed. The trace
2626 buffer control structures would have been reset but the thread had
2627 no way to tell. The thread could even midway memcpy'ing to the
2628 buffer, which would mean that when resumed, it would clobber the
2629 trace buffer that had been set for a new run.
2631 - we can't rewrite/reuse the jump pads for new tracepoints
2632 safely. Say you do tstart while a thread is stopped midway while
2633 collecting. When the thread is later resumed, it finishes the
2634 collection, and returns to the jump pad, to execute the original
2635 instruction that was under the tracepoint jump at the time the
2636 older run had been started. If the jump pad had been rewritten
2637 since for something else in the new run, the thread would now
2638 execute the wrong / random instructions. */
2641 linux_stabilize_threads (void)
2643 struct thread_info
*saved_thread
;
2644 struct thread_info
*thread_stuck
;
2647 = (struct thread_info
*) find_inferior (&all_threads
,
2648 stuck_in_jump_pad_callback
,
2650 if (thread_stuck
!= NULL
)
2653 debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
2654 lwpid_of (thread_stuck
));
2658 saved_thread
= current_thread
;
2660 stabilizing_threads
= 1;
2663 for_each_inferior (&all_threads
, move_out_of_jump_pad_callback
);
2665 /* Loop until all are stopped out of the jump pads. */
2666 while (find_inferior (&all_threads
, lwp_running
, NULL
) != NULL
)
2668 struct target_waitstatus ourstatus
;
2669 struct lwp_info
*lwp
;
2672 /* Note that we go through the full wait even loop. While
2673 moving threads out of jump pad, we need to be able to step
2674 over internal breakpoints and such. */
2675 linux_wait_1 (minus_one_ptid
, &ourstatus
, 0);
2677 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
2679 lwp
= get_thread_lwp (current_thread
);
2684 if (ourstatus
.value
.sig
!= GDB_SIGNAL_0
2685 || current_thread
->last_resume_kind
== resume_stop
)
2687 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.value
.sig
));
2688 enqueue_one_deferred_signal (lwp
, &wstat
);
2693 find_inferior (&all_threads
, unsuspend_one_lwp
, NULL
);
2695 stabilizing_threads
= 0;
2697 current_thread
= saved_thread
;
2702 = (struct thread_info
*) find_inferior (&all_threads
,
2703 stuck_in_jump_pad_callback
,
2705 if (thread_stuck
!= NULL
)
2706 debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
2707 lwpid_of (thread_stuck
));
2711 static void async_file_mark (void);
2713 /* Convenience function that is called when the kernel reports an
2714 event that is not passed out to GDB. */
2717 ignore_event (struct target_waitstatus
*ourstatus
)
2719 /* If we got an event, there may still be others, as a single
2720 SIGCHLD can indicate more than one child stopped. This forces
2721 another target_wait call. */
2724 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2728 /* Return non-zero if WAITSTATUS reflects an extended linux
2729 event. Otherwise, return zero. */
2732 extended_event_reported (const struct target_waitstatus
*waitstatus
)
2734 if (waitstatus
== NULL
)
2737 return (waitstatus
->kind
== TARGET_WAITKIND_FORKED
2738 || waitstatus
->kind
== TARGET_WAITKIND_VFORKED
2739 || waitstatus
->kind
== TARGET_WAITKIND_VFORK_DONE
);
2742 /* Wait for process, returns status. */
2745 linux_wait_1 (ptid_t ptid
,
2746 struct target_waitstatus
*ourstatus
, int target_options
)
2749 struct lwp_info
*event_child
;
2752 int step_over_finished
;
2753 int bp_explains_trap
;
2754 int maybe_internal_trap
;
2762 debug_printf ("linux_wait_1: [%s]\n", target_pid_to_str (ptid
));
2765 /* Translate generic target options into linux options. */
2767 if (target_options
& TARGET_WNOHANG
)
2770 bp_explains_trap
= 0;
2773 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2775 if (ptid_equal (step_over_bkpt
, null_ptid
))
2776 pid
= linux_wait_for_event (ptid
, &w
, options
);
2780 debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
2781 target_pid_to_str (step_over_bkpt
));
2782 pid
= linux_wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
2787 gdb_assert (target_options
& TARGET_WNOHANG
);
2791 debug_printf ("linux_wait_1 ret = null_ptid, "
2792 "TARGET_WAITKIND_IGNORE\n");
2796 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2803 debug_printf ("linux_wait_1 ret = null_ptid, "
2804 "TARGET_WAITKIND_NO_RESUMED\n");
2808 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
2812 event_child
= get_thread_lwp (current_thread
);
2814 /* linux_wait_for_event only returns an exit status for the last
2815 child of a process. Report it. */
2816 if (WIFEXITED (w
) || WIFSIGNALED (w
))
2820 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
2821 ourstatus
->value
.integer
= WEXITSTATUS (w
);
2825 debug_printf ("linux_wait_1 ret = %s, exited with "
2827 target_pid_to_str (ptid_of (current_thread
)),
2834 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
2835 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (w
));
2839 debug_printf ("linux_wait_1 ret = %s, terminated with "
2841 target_pid_to_str (ptid_of (current_thread
)),
2847 return ptid_of (current_thread
);
2850 /* If step-over executes a breakpoint instruction, it means a
2851 gdb/gdbserver breakpoint had been planted on top of a permanent
2852 breakpoint. The PC has been adjusted by
2853 check_stopped_by_breakpoint to point at the breakpoint address.
2854 Advance the PC manually past the breakpoint, otherwise the
2855 program would keep trapping the permanent breakpoint forever. */
2856 if (!ptid_equal (step_over_bkpt
, null_ptid
)
2857 && event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
)
2859 unsigned int increment_pc
= the_low_target
.breakpoint_len
;
2863 debug_printf ("step-over for %s executed software breakpoint\n",
2864 target_pid_to_str (ptid_of (current_thread
)));
2867 if (increment_pc
!= 0)
2869 struct regcache
*regcache
2870 = get_thread_regcache (current_thread
, 1);
2872 event_child
->stop_pc
+= increment_pc
;
2873 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
2875 if (!(*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))
2876 event_child
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
2880 /* If this event was not handled before, and is not a SIGTRAP, we
2881 report it. SIGILL and SIGSEGV are also treated as traps in case
2882 a breakpoint is inserted at the current PC. If this target does
2883 not support internal breakpoints at all, we also report the
2884 SIGTRAP without further processing; it's of no concern to us. */
2886 = (supports_breakpoints ()
2887 && (WSTOPSIG (w
) == SIGTRAP
2888 || ((WSTOPSIG (w
) == SIGILL
2889 || WSTOPSIG (w
) == SIGSEGV
)
2890 && (*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))));
2892 if (maybe_internal_trap
)
2894 /* Handle anything that requires bookkeeping before deciding to
2895 report the event or continue waiting. */
2897 /* First check if we can explain the SIGTRAP with an internal
2898 breakpoint, or if we should possibly report the event to GDB.
2899 Do this before anything that may remove or insert a
2901 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
2903 /* We have a SIGTRAP, possibly a step-over dance has just
2904 finished. If so, tweak the state machine accordingly,
2905 reinsert breakpoints and delete any reinsert (software
2906 single-step) breakpoints. */
2907 step_over_finished
= finish_step_over (event_child
);
2909 /* Now invoke the callbacks of any internal breakpoints there. */
2910 check_breakpoints (event_child
->stop_pc
);
2912 /* Handle tracepoint data collecting. This may overflow the
2913 trace buffer, and cause a tracing stop, removing
2915 trace_event
= handle_tracepoints (event_child
);
2917 if (bp_explains_trap
)
2919 /* If we stepped or ran into an internal breakpoint, we've
2920 already handled it. So next time we resume (from this
2921 PC), we should step over it. */
2923 debug_printf ("Hit a gdbserver breakpoint.\n");
2925 if (breakpoint_here (event_child
->stop_pc
))
2926 event_child
->need_step_over
= 1;
2931 /* We have some other signal, possibly a step-over dance was in
2932 progress, and it should be cancelled too. */
2933 step_over_finished
= finish_step_over (event_child
);
2936 /* We have all the data we need. Either report the event to GDB, or
2937 resume threads and keep waiting for more. */
2939 /* If we're collecting a fast tracepoint, finish the collection and
2940 move out of the jump pad before delivering a signal. See
2941 linux_stabilize_threads. */
2944 && WSTOPSIG (w
) != SIGTRAP
2945 && supports_fast_tracepoints ()
2946 && agent_loaded_p ())
2949 debug_printf ("Got signal %d for LWP %ld. Check if we need "
2950 "to defer or adjust it.\n",
2951 WSTOPSIG (w
), lwpid_of (current_thread
));
2953 /* Allow debugging the jump pad itself. */
2954 if (current_thread
->last_resume_kind
!= resume_step
2955 && maybe_move_out_of_jump_pad (event_child
, &w
))
2957 enqueue_one_deferred_signal (event_child
, &w
);
2960 debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
2961 WSTOPSIG (w
), lwpid_of (current_thread
));
2963 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
2965 return ignore_event (ourstatus
);
2969 if (event_child
->collecting_fast_tracepoint
)
2972 debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
2973 "Check if we're already there.\n",
2974 lwpid_of (current_thread
),
2975 event_child
->collecting_fast_tracepoint
);
2979 event_child
->collecting_fast_tracepoint
2980 = linux_fast_tracepoint_collecting (event_child
, NULL
);
2982 if (event_child
->collecting_fast_tracepoint
!= 1)
2984 /* No longer need this breakpoint. */
2985 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
2988 debug_printf ("No longer need exit-jump-pad bkpt; removing it."
2989 "stopping all threads momentarily.\n");
2991 /* Other running threads could hit this breakpoint.
2992 We don't handle moribund locations like GDB does,
2993 instead we always pause all threads when removing
2994 breakpoints, so that any step-over or
2995 decr_pc_after_break adjustment is always taken
2996 care of while the breakpoint is still
2998 stop_all_lwps (1, event_child
);
3000 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
3001 event_child
->exit_jump_pad_bkpt
= NULL
;
3003 unstop_all_lwps (1, event_child
);
3005 gdb_assert (event_child
->suspended
>= 0);
3009 if (event_child
->collecting_fast_tracepoint
== 0)
3012 debug_printf ("fast tracepoint finished "
3013 "collecting successfully.\n");
3015 /* We may have a deferred signal to report. */
3016 if (dequeue_one_deferred_signal (event_child
, &w
))
3019 debug_printf ("dequeued one signal.\n");
3024 debug_printf ("no deferred signals.\n");
3026 if (stabilizing_threads
)
3028 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3029 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3033 debug_printf ("linux_wait_1 ret = %s, stopped "
3034 "while stabilizing threads\n",
3035 target_pid_to_str (ptid_of (current_thread
)));
3039 return ptid_of (current_thread
);
3045 /* Check whether GDB would be interested in this event. */
3047 /* If GDB is not interested in this signal, don't stop other
3048 threads, and don't report it to GDB. Just resume the inferior
3049 right away. We do this for threading-related signals as well as
3050 any that GDB specifically requested we ignore. But never ignore
3051 SIGSTOP if we sent it ourselves, and do not ignore signals when
3052 stepping - they may require special handling to skip the signal
3053 handler. Also never ignore signals that could be caused by a
3055 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
3058 && current_thread
->last_resume_kind
!= resume_step
3060 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3061 (current_process ()->priv
->thread_db
!= NULL
3062 && (WSTOPSIG (w
) == __SIGRTMIN
3063 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
3066 (pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
3067 && !(WSTOPSIG (w
) == SIGSTOP
3068 && current_thread
->last_resume_kind
== resume_stop
)
3069 && !linux_wstatus_maybe_breakpoint (w
))))
3071 siginfo_t info
, *info_p
;
3074 debug_printf ("Ignored signal %d for LWP %ld.\n",
3075 WSTOPSIG (w
), lwpid_of (current_thread
));
3077 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
3078 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
3082 linux_resume_one_lwp (event_child
, event_child
->stepping
,
3083 WSTOPSIG (w
), info_p
);
3084 return ignore_event (ourstatus
);
3087 /* Note that all addresses are always "out of the step range" when
3088 there's no range to begin with. */
3089 in_step_range
= lwp_in_step_range (event_child
);
3091 /* If GDB wanted this thread to single step, and the thread is out
3092 of the step range, we always want to report the SIGTRAP, and let
3093 GDB handle it. Watchpoints should always be reported. So should
3094 signals we can't explain. A SIGTRAP we can't explain could be a
3095 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3096 do, we're be able to handle GDB breakpoints on top of internal
3097 breakpoints, by handling the internal breakpoint and still
3098 reporting the event to GDB. If we don't, we're out of luck, GDB
3099 won't see the breakpoint hit. */
3100 report_to_gdb
= (!maybe_internal_trap
3101 || (current_thread
->last_resume_kind
== resume_step
3103 || event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3104 || (!step_over_finished
&& !in_step_range
3105 && !bp_explains_trap
&& !trace_event
)
3106 || (gdb_breakpoint_here (event_child
->stop_pc
)
3107 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
3108 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
))
3109 || extended_event_reported (&event_child
->waitstatus
));
3111 run_breakpoint_commands (event_child
->stop_pc
);
3113 /* We found no reason GDB would want us to stop. We either hit one
3114 of our own breakpoints, or finished an internal step GDB
3115 shouldn't know about. */
3120 if (bp_explains_trap
)
3121 debug_printf ("Hit a gdbserver breakpoint.\n");
3122 if (step_over_finished
)
3123 debug_printf ("Step-over finished.\n");
3125 debug_printf ("Tracepoint event.\n");
3126 if (lwp_in_step_range (event_child
))
3127 debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
3128 paddress (event_child
->stop_pc
),
3129 paddress (event_child
->step_range_start
),
3130 paddress (event_child
->step_range_end
));
3131 if (extended_event_reported (&event_child
->waitstatus
))
3133 char *str
= target_waitstatus_to_string (ourstatus
);
3134 debug_printf ("LWP %ld: extended event with waitstatus %s\n",
3135 lwpid_of (get_lwp_thread (event_child
)), str
);
3140 /* We're not reporting this breakpoint to GDB, so apply the
3141 decr_pc_after_break adjustment to the inferior's regcache
3144 if (the_low_target
.set_pc
!= NULL
)
3146 struct regcache
*regcache
3147 = get_thread_regcache (current_thread
, 1);
3148 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
3151 /* We may have finished stepping over a breakpoint. If so,
3152 we've stopped and suspended all LWPs momentarily except the
3153 stepping one. This is where we resume them all again. We're
3154 going to keep waiting, so use proceed, which handles stepping
3155 over the next breakpoint. */
3157 debug_printf ("proceeding all threads.\n");
3159 if (step_over_finished
)
3160 unsuspend_all_lwps (event_child
);
3162 proceed_all_lwps ();
3163 return ignore_event (ourstatus
);
3168 if (current_thread
->last_resume_kind
== resume_step
)
3170 if (event_child
->step_range_start
== event_child
->step_range_end
)
3171 debug_printf ("GDB wanted to single-step, reporting event.\n");
3172 else if (!lwp_in_step_range (event_child
))
3173 debug_printf ("Out of step range, reporting event.\n");
3175 if (event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
3176 debug_printf ("Stopped by watchpoint.\n");
3177 else if (gdb_breakpoint_here (event_child
->stop_pc
))
3178 debug_printf ("Stopped by GDB breakpoint.\n");
3180 debug_printf ("Hit a non-gdbserver trap event.\n");
3183 /* Alright, we're going to report a stop. */
3185 if (!stabilizing_threads
)
3187 /* In all-stop, stop all threads. */
3189 stop_all_lwps (0, NULL
);
3191 /* If we're not waiting for a specific LWP, choose an event LWP
3192 from among those that have had events. Giving equal priority
3193 to all LWPs that have had events helps prevent
3195 if (ptid_equal (ptid
, minus_one_ptid
))
3197 event_child
->status_pending_p
= 1;
3198 event_child
->status_pending
= w
;
3200 select_event_lwp (&event_child
);
3202 /* current_thread and event_child must stay in sync. */
3203 current_thread
= get_lwp_thread (event_child
);
3205 event_child
->status_pending_p
= 0;
3206 w
= event_child
->status_pending
;
3209 if (step_over_finished
)
3213 /* If we were doing a step-over, all other threads but
3214 the stepping one had been paused in start_step_over,
3215 with their suspend counts incremented. We don't want
3216 to do a full unstop/unpause, because we're in
3217 all-stop mode (so we want threads stopped), but we
3218 still need to unsuspend the other threads, to
3219 decrement their `suspended' count back. */
3220 unsuspend_all_lwps (event_child
);
3224 /* If we just finished a step-over, then all threads had
3225 been momentarily paused. In all-stop, that's fine,
3226 we want threads stopped by now anyway. In non-stop,
3227 we need to re-resume threads that GDB wanted to be
3229 unstop_all_lwps (1, event_child
);
3233 /* Stabilize threads (move out of jump pads). */
3235 stabilize_threads ();
3239 /* If we just finished a step-over, then all threads had been
3240 momentarily paused. In all-stop, that's fine, we want
3241 threads stopped by now anyway. In non-stop, we need to
3242 re-resume threads that GDB wanted to be running. */
3243 if (step_over_finished
)
3244 unstop_all_lwps (1, event_child
);
3247 if (extended_event_reported (&event_child
->waitstatus
))
3249 /* If the reported event is a fork, vfork or exec, let GDB know. */
3250 ourstatus
->kind
= event_child
->waitstatus
.kind
;
3251 ourstatus
->value
= event_child
->waitstatus
.value
;
3253 /* Clear the event lwp's waitstatus since we handled it already. */
3254 event_child
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3257 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3259 /* Now that we've selected our final event LWP, un-adjust its PC if
3260 it was a software breakpoint, and the client doesn't know we can
3261 adjust the breakpoint ourselves. */
3262 if (event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3263 && !swbreak_feature
)
3265 int decr_pc
= the_low_target
.decr_pc_after_break
;
3269 struct regcache
*regcache
3270 = get_thread_regcache (current_thread
, 1);
3271 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
+ decr_pc
);
3275 if (current_thread
->last_resume_kind
== resume_stop
3276 && WSTOPSIG (w
) == SIGSTOP
)
3278 /* A thread that has been requested to stop by GDB with vCont;t,
3279 and it stopped cleanly, so report as SIG0. The use of
3280 SIGSTOP is an implementation detail. */
3281 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3283 else if (current_thread
->last_resume_kind
== resume_stop
3284 && WSTOPSIG (w
) != SIGSTOP
)
3286 /* A thread that has been requested to stop by GDB with vCont;t,
3287 but, it stopped for other reasons. */
3288 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3290 else if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
)
3292 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3295 gdb_assert (ptid_equal (step_over_bkpt
, null_ptid
));
3299 debug_printf ("linux_wait_1 ret = %s, %d, %d\n",
3300 target_pid_to_str (ptid_of (current_thread
)),
3301 ourstatus
->kind
, ourstatus
->value
.sig
);
3305 return ptid_of (current_thread
);
3308 /* Get rid of any pending event in the pipe. */
3310 async_file_flush (void)
3316 ret
= read (linux_event_pipe
[0], &buf
, 1);
3317 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
3320 /* Put something in the pipe, so the event loop wakes up. */
3322 async_file_mark (void)
3326 async_file_flush ();
3329 ret
= write (linux_event_pipe
[1], "+", 1);
3330 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
3332 /* Ignore EAGAIN. If the pipe is full, the event loop will already
3333 be awakened anyway. */
3337 linux_wait (ptid_t ptid
,
3338 struct target_waitstatus
*ourstatus
, int target_options
)
3342 /* Flush the async file first. */
3343 if (target_is_async_p ())
3344 async_file_flush ();
3348 event_ptid
= linux_wait_1 (ptid
, ourstatus
, target_options
);
3350 while ((target_options
& TARGET_WNOHANG
) == 0
3351 && ptid_equal (event_ptid
, null_ptid
)
3352 && ourstatus
->kind
== TARGET_WAITKIND_IGNORE
);
3354 /* If at least one stop was reported, there may be more. A single
3355 SIGCHLD can signal more than one child stop. */
3356 if (target_is_async_p ()
3357 && (target_options
& TARGET_WNOHANG
) != 0
3358 && !ptid_equal (event_ptid
, null_ptid
))
3364 /* Send a signal to an LWP. */
3367 kill_lwp (unsigned long lwpid
, int signo
)
3369 /* Use tkill, if possible, in case we are using nptl threads. If tkill
3370 fails, then we are not using nptl threads and we should be using kill. */
3374 static int tkill_failed
;
3381 ret
= syscall (__NR_tkill
, lwpid
, signo
);
3382 if (errno
!= ENOSYS
)
3389 return kill (lwpid
, signo
);
3393 linux_stop_lwp (struct lwp_info
*lwp
)
3399 send_sigstop (struct lwp_info
*lwp
)
3403 pid
= lwpid_of (get_lwp_thread (lwp
));
3405 /* If we already have a pending stop signal for this process, don't
3407 if (lwp
->stop_expected
)
3410 debug_printf ("Have pending sigstop for lwp %d\n", pid
);
3416 debug_printf ("Sending sigstop to lwp %d\n", pid
);
3418 lwp
->stop_expected
= 1;
3419 kill_lwp (pid
, SIGSTOP
);
3423 send_sigstop_callback (struct inferior_list_entry
*entry
, void *except
)
3425 struct thread_info
*thread
= (struct thread_info
*) entry
;
3426 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3428 /* Ignore EXCEPT. */
3439 /* Increment the suspend count of an LWP, and stop it, if not stopped
3442 suspend_and_send_sigstop_callback (struct inferior_list_entry
*entry
,
3445 struct thread_info
*thread
= (struct thread_info
*) entry
;
3446 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3448 /* Ignore EXCEPT. */
3454 return send_sigstop_callback (entry
, except
);
3458 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
3460 /* It's dead, really. */
3463 /* Store the exit status for later. */
3464 lwp
->status_pending_p
= 1;
3465 lwp
->status_pending
= wstat
;
3467 /* Prevent trying to stop it. */
3470 /* No further stops are expected from a dead lwp. */
3471 lwp
->stop_expected
= 0;
3474 /* Wait for all children to stop for the SIGSTOPs we just queued. */
3477 wait_for_sigstop (void)
3479 struct thread_info
*saved_thread
;
3484 saved_thread
= current_thread
;
3485 if (saved_thread
!= NULL
)
3486 saved_tid
= saved_thread
->entry
.id
;
3488 saved_tid
= null_ptid
; /* avoid bogus unused warning */
3491 debug_printf ("wait_for_sigstop: pulling events\n");
3493 /* Passing NULL_PTID as filter indicates we want all events to be
3494 left pending. Eventually this returns when there are no
3495 unwaited-for children left. */
3496 ret
= linux_wait_for_event_filtered (minus_one_ptid
, null_ptid
,
3498 gdb_assert (ret
== -1);
3500 if (saved_thread
== NULL
|| linux_thread_alive (saved_tid
))
3501 current_thread
= saved_thread
;
3505 debug_printf ("Previously current thread died.\n");
3509 /* We can't change the current inferior behind GDB's back,
3510 otherwise, a subsequent command may apply to the wrong
3512 current_thread
= NULL
;
3516 /* Set a valid thread as current. */
3517 set_desired_thread (0);
3522 /* Returns true if LWP ENTRY is stopped in a jump pad, and we can't
3523 move it out, because we need to report the stop event to GDB. For
3524 example, if the user puts a breakpoint in the jump pad, it's
3525 because she wants to debug it. */
3528 stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
, void *data
)
3530 struct thread_info
*thread
= (struct thread_info
*) entry
;
3531 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3533 gdb_assert (lwp
->suspended
== 0);
3534 gdb_assert (lwp
->stopped
);
3536 /* Allow debugging the jump pad, gdb_collect, etc.. */
3537 return (supports_fast_tracepoints ()
3538 && agent_loaded_p ()
3539 && (gdb_breakpoint_here (lwp
->stop_pc
)
3540 || lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3541 || thread
->last_resume_kind
== resume_step
)
3542 && linux_fast_tracepoint_collecting (lwp
, NULL
));
3546 move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
)
3548 struct thread_info
*thread
= (struct thread_info
*) entry
;
3549 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3552 gdb_assert (lwp
->suspended
== 0);
3553 gdb_assert (lwp
->stopped
);
3555 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
3557 /* Allow debugging the jump pad, gdb_collect, etc. */
3558 if (!gdb_breakpoint_here (lwp
->stop_pc
)
3559 && lwp
->stop_reason
!= TARGET_STOPPED_BY_WATCHPOINT
3560 && thread
->last_resume_kind
!= resume_step
3561 && maybe_move_out_of_jump_pad (lwp
, wstat
))
3564 debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
3569 lwp
->status_pending_p
= 0;
3570 enqueue_one_deferred_signal (lwp
, wstat
);
3573 debug_printf ("Signal %d for LWP %ld deferred "
3575 WSTOPSIG (*wstat
), lwpid_of (thread
));
3578 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
3585 lwp_running (struct inferior_list_entry
*entry
, void *data
)
3587 struct thread_info
*thread
= (struct thread_info
*) entry
;
3588 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3597 /* Stop all lwps that aren't stopped yet, except EXCEPT, if not NULL.
3598 If SUSPEND, then also increase the suspend count of every LWP,
3602 stop_all_lwps (int suspend
, struct lwp_info
*except
)
3604 /* Should not be called recursively. */
3605 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
3610 debug_printf ("stop_all_lwps (%s, except=%s)\n",
3611 suspend
? "stop-and-suspend" : "stop",
3613 ? target_pid_to_str (ptid_of (get_lwp_thread (except
)))
3617 stopping_threads
= (suspend
3618 ? STOPPING_AND_SUSPENDING_THREADS
3619 : STOPPING_THREADS
);
3622 find_inferior (&all_threads
, suspend_and_send_sigstop_callback
, except
);
3624 find_inferior (&all_threads
, send_sigstop_callback
, except
);
3625 wait_for_sigstop ();
3626 stopping_threads
= NOT_STOPPING_THREADS
;
3630 debug_printf ("stop_all_lwps done, setting stopping_threads "
3631 "back to !stopping\n");
3636 /* Resume execution of LWP. If STEP is nonzero, single-step it. If
3637 SIGNAL is nonzero, give it that signal. */
3640 linux_resume_one_lwp_throw (struct lwp_info
*lwp
,
3641 int step
, int signal
, siginfo_t
*info
)
3643 struct thread_info
*thread
= get_lwp_thread (lwp
);
3644 struct thread_info
*saved_thread
;
3645 int fast_tp_collecting
;
3647 if (lwp
->stopped
== 0)
3650 fast_tp_collecting
= lwp
->collecting_fast_tracepoint
;
3652 gdb_assert (!stabilizing_threads
|| fast_tp_collecting
);
3654 /* Cancel actions that rely on GDB not changing the PC (e.g., the
3655 user used the "jump" command, or "set $pc = foo"). */
3656 if (lwp
->stop_pc
!= get_pc (lwp
))
3658 /* Collecting 'while-stepping' actions doesn't make sense
3660 release_while_stepping_state_list (thread
);
3663 /* If we have pending signals or status, and a new signal, enqueue the
3664 signal. Also enqueue the signal if we are waiting to reinsert a
3665 breakpoint; it will be picked up again below. */
3667 && (lwp
->status_pending_p
3668 || lwp
->pending_signals
!= NULL
3669 || lwp
->bp_reinsert
!= 0
3670 || fast_tp_collecting
))
3672 struct pending_signals
*p_sig
;
3673 p_sig
= xmalloc (sizeof (*p_sig
));
3674 p_sig
->prev
= lwp
->pending_signals
;
3675 p_sig
->signal
= signal
;
3677 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
3679 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
3680 lwp
->pending_signals
= p_sig
;
3683 if (lwp
->status_pending_p
)
3686 debug_printf ("Not resuming lwp %ld (%s, signal %d, stop %s);"
3687 " has pending status\n",
3688 lwpid_of (thread
), step
? "step" : "continue", signal
,
3689 lwp
->stop_expected
? "expected" : "not expected");
3693 saved_thread
= current_thread
;
3694 current_thread
= thread
;
3697 debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
3698 lwpid_of (thread
), step
? "step" : "continue", signal
,
3699 lwp
->stop_expected
? "expected" : "not expected");
3701 /* This bit needs some thinking about. If we get a signal that
3702 we must report while a single-step reinsert is still pending,
3703 we often end up resuming the thread. It might be better to
3704 (ew) allow a stack of pending events; then we could be sure that
3705 the reinsert happened right away and not lose any signals.
3707 Making this stack would also shrink the window in which breakpoints are
3708 uninserted (see comment in linux_wait_for_lwp) but not enough for
3709 complete correctness, so it won't solve that problem. It may be
3710 worthwhile just to solve this one, however. */
3711 if (lwp
->bp_reinsert
!= 0)
3714 debug_printf (" pending reinsert at 0x%s\n",
3715 paddress (lwp
->bp_reinsert
));
3717 if (can_hardware_single_step ())
3719 if (fast_tp_collecting
== 0)
3722 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
3724 fprintf (stderr
, "BAD - reinserting and suspended(%d).\n",
3731 /* Postpone any pending signal. It was enqueued above. */
3735 if (fast_tp_collecting
== 1)
3738 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
3739 " (exit-jump-pad-bkpt)\n",
3742 /* Postpone any pending signal. It was enqueued above. */
3745 else if (fast_tp_collecting
== 2)
3748 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
3749 " single-stepping\n",
3752 if (can_hardware_single_step ())
3756 internal_error (__FILE__
, __LINE__
,
3757 "moving out of jump pad single-stepping"
3758 " not implemented on this target");
3761 /* Postpone any pending signal. It was enqueued above. */
3765 /* If we have while-stepping actions in this thread set it stepping.
3766 If we have a signal to deliver, it may or may not be set to
3767 SIG_IGN, we don't know. Assume so, and allow collecting
3768 while-stepping into a signal handler. A possible smart thing to
3769 do would be to set an internal breakpoint at the signal return
3770 address, continue, and carry on catching this while-stepping
3771 action only when that breakpoint is hit. A future
3773 if (thread
->while_stepping
!= NULL
3774 && can_hardware_single_step ())
3777 debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
3782 if (the_low_target
.get_pc
!= NULL
)
3784 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
3786 lwp
->stop_pc
= (*the_low_target
.get_pc
) (regcache
);
3790 debug_printf (" %s from pc 0x%lx\n", step
? "step" : "continue",
3791 (long) lwp
->stop_pc
);
3795 /* If we have pending signals, consume one unless we are trying to
3796 reinsert a breakpoint or we're trying to finish a fast tracepoint
3798 if (lwp
->pending_signals
!= NULL
3799 && lwp
->bp_reinsert
== 0
3800 && fast_tp_collecting
== 0)
3802 struct pending_signals
**p_sig
;
3804 p_sig
= &lwp
->pending_signals
;
3805 while ((*p_sig
)->prev
!= NULL
)
3806 p_sig
= &(*p_sig
)->prev
;
3808 signal
= (*p_sig
)->signal
;
3809 if ((*p_sig
)->info
.si_signo
!= 0)
3810 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
3817 if (the_low_target
.prepare_to_resume
!= NULL
)
3818 the_low_target
.prepare_to_resume (lwp
);
3820 regcache_invalidate_thread (thread
);
3822 lwp
->stepping
= step
;
3823 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, lwpid_of (thread
),
3824 (PTRACE_TYPE_ARG3
) 0,
3825 /* Coerce to a uintptr_t first to avoid potential gcc warning
3826 of coercing an 8 byte integer to a 4 byte pointer. */
3827 (PTRACE_TYPE_ARG4
) (uintptr_t) signal
);
3829 current_thread
= saved_thread
;
3831 perror_with_name ("resuming thread");
3833 /* Successfully resumed. Clear state that no longer makes sense,
3834 and mark the LWP as running. Must not do this before resuming
3835 otherwise if that fails other code will be confused. E.g., we'd
3836 later try to stop the LWP and hang forever waiting for a stop
3837 status. Note that we must not throw after this is cleared,
3838 otherwise handle_zombie_lwp_error would get confused. */
3840 lwp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
3843 /* Called when we try to resume a stopped LWP and that errors out. If
3844 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
3845 or about to become), discard the error, clear any pending status
3846 the LWP may have, and return true (we'll collect the exit status
3847 soon enough). Otherwise, return false. */
3850 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
3852 struct thread_info
*thread
= get_lwp_thread (lp
);
3854 /* If we get an error after resuming the LWP successfully, we'd
3855 confuse !T state for the LWP being gone. */
3856 gdb_assert (lp
->stopped
);
3858 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
3859 because even if ptrace failed with ESRCH, the tracee may be "not
3860 yet fully dead", but already refusing ptrace requests. In that
3861 case the tracee has 'R (Running)' state for a little bit
3862 (observed in Linux 3.18). See also the note on ESRCH in the
3863 ptrace(2) man page. Instead, check whether the LWP has any state
3864 other than ptrace-stopped. */
3866 /* Don't assume anything if /proc/PID/status can't be read. */
3867 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread
)) == 0)
3869 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
3870 lp
->status_pending_p
= 0;
3876 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
3877 disappears while we try to resume it. */
3880 linux_resume_one_lwp (struct lwp_info
*lwp
,
3881 int step
, int signal
, siginfo_t
*info
)
3885 linux_resume_one_lwp_throw (lwp
, step
, signal
, info
);
3887 CATCH (ex
, RETURN_MASK_ERROR
)
3889 if (!check_ptrace_stopped_lwp_gone (lwp
))
3890 throw_exception (ex
);
3895 struct thread_resume_array
3897 struct thread_resume
*resume
;
3901 /* This function is called once per thread via find_inferior.
3902 ARG is a pointer to a thread_resume_array struct.
3903 We look up the thread specified by ENTRY in ARG, and mark the thread
3904 with a pointer to the appropriate resume request.
3906 This algorithm is O(threads * resume elements), but resume elements
3907 is small (and will remain small at least until GDB supports thread
3911 linux_set_resume_request (struct inferior_list_entry
*entry
, void *arg
)
3913 struct thread_info
*thread
= (struct thread_info
*) entry
;
3914 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3916 struct thread_resume_array
*r
;
3920 for (ndx
= 0; ndx
< r
->n
; ndx
++)
3922 ptid_t ptid
= r
->resume
[ndx
].thread
;
3923 if (ptid_equal (ptid
, minus_one_ptid
)
3924 || ptid_equal (ptid
, entry
->id
)
3925 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
3927 || (ptid_get_pid (ptid
) == pid_of (thread
)
3928 && (ptid_is_pid (ptid
)
3929 || ptid_get_lwp (ptid
) == -1)))
3931 if (r
->resume
[ndx
].kind
== resume_stop
3932 && thread
->last_resume_kind
== resume_stop
)
3935 debug_printf ("already %s LWP %ld at GDB's request\n",
3936 (thread
->last_status
.kind
3937 == TARGET_WAITKIND_STOPPED
)
3945 lwp
->resume
= &r
->resume
[ndx
];
3946 thread
->last_resume_kind
= lwp
->resume
->kind
;
3948 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
3949 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
3951 /* If we had a deferred signal to report, dequeue one now.
3952 This can happen if LWP gets more than one signal while
3953 trying to get out of a jump pad. */
3955 && !lwp
->status_pending_p
3956 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
3958 lwp
->status_pending_p
= 1;
3961 debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
3962 "leaving status pending.\n",
3963 WSTOPSIG (lwp
->status_pending
),
3971 /* No resume action for this thread. */
3977 /* find_inferior callback for linux_resume.
3978 Set *FLAG_P if this lwp has an interesting status pending. */
3981 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
3983 struct thread_info
*thread
= (struct thread_info
*) entry
;
3984 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3986 /* LWPs which will not be resumed are not interesting, because
3987 we might not wait for them next time through linux_wait. */
3988 if (lwp
->resume
== NULL
)
3991 if (thread_still_has_status_pending_p (thread
))
3992 * (int *) flag_p
= 1;
3997 /* Return 1 if this lwp that GDB wants running is stopped at an
3998 internal breakpoint that we need to step over. It assumes that any
3999 required STOP_PC adjustment has already been propagated to the
4000 inferior's regcache. */
4003 need_step_over_p (struct inferior_list_entry
*entry
, void *dummy
)
4005 struct thread_info
*thread
= (struct thread_info
*) entry
;
4006 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4007 struct thread_info
*saved_thread
;
4010 /* LWPs which will not be resumed are not interesting, because we
4011 might not wait for them next time through linux_wait. */
4016 debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
4021 if (thread
->last_resume_kind
== resume_stop
)
4024 debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
4030 gdb_assert (lwp
->suspended
>= 0);
4035 debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
4040 if (!lwp
->need_step_over
)
4043 debug_printf ("Need step over [LWP %ld]? No\n", lwpid_of (thread
));
4046 if (lwp
->status_pending_p
)
4049 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4055 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4059 /* If the PC has changed since we stopped, then don't do anything,
4060 and let the breakpoint/tracepoint be hit. This happens if, for
4061 instance, GDB handled the decr_pc_after_break subtraction itself,
4062 GDB is OOL stepping this thread, or the user has issued a "jump"
4063 command, or poked thread's registers herself. */
4064 if (pc
!= lwp
->stop_pc
)
4067 debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4068 "Old stop_pc was 0x%s, PC is now 0x%s\n",
4070 paddress (lwp
->stop_pc
), paddress (pc
));
4072 lwp
->need_step_over
= 0;
4076 saved_thread
= current_thread
;
4077 current_thread
= thread
;
4079 /* We can only step over breakpoints we know about. */
4080 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
4082 /* Don't step over a breakpoint that GDB expects to hit
4083 though. If the condition is being evaluated on the target's side
4084 and it evaluate to false, step over this breakpoint as well. */
4085 if (gdb_breakpoint_here (pc
)
4086 && gdb_condition_true_at_breakpoint (pc
)
4087 && gdb_no_commands_at_breakpoint (pc
))
4090 debug_printf ("Need step over [LWP %ld]? yes, but found"
4091 " GDB breakpoint at 0x%s; skipping step over\n",
4092 lwpid_of (thread
), paddress (pc
));
4094 current_thread
= saved_thread
;
4100 debug_printf ("Need step over [LWP %ld]? yes, "
4101 "found breakpoint at 0x%s\n",
4102 lwpid_of (thread
), paddress (pc
));
4104 /* We've found an lwp that needs stepping over --- return 1 so
4105 that find_inferior stops looking. */
4106 current_thread
= saved_thread
;
4108 /* If the step over is cancelled, this is set again. */
4109 lwp
->need_step_over
= 0;
4114 current_thread
= saved_thread
;
4117 debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
4119 lwpid_of (thread
), paddress (pc
));
4124 /* Start a step-over operation on LWP. When LWP stopped at a
4125 breakpoint, to make progress, we need to remove the breakpoint out
4126 of the way. If we let other threads run while we do that, they may
4127 pass by the breakpoint location and miss hitting it. To avoid
4128 that, a step-over momentarily stops all threads while LWP is
4129 single-stepped while the breakpoint is temporarily uninserted from
4130 the inferior. When the single-step finishes, we reinsert the
4131 breakpoint, and let all threads that are supposed to be running,
4134 On targets that don't support hardware single-step, we don't
4135 currently support full software single-stepping. Instead, we only
4136 support stepping over the thread event breakpoint, by asking the
4137 low target where to place a reinsert breakpoint. Since this
4138 routine assumes the breakpoint being stepped over is a thread event
4139 breakpoint, it usually assumes the return address of the current
4140 function is a good enough place to set the reinsert breakpoint. */
4143 start_step_over (struct lwp_info
*lwp
)
4145 struct thread_info
*thread
= get_lwp_thread (lwp
);
4146 struct thread_info
*saved_thread
;
4151 debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
4154 stop_all_lwps (1, lwp
);
4155 gdb_assert (lwp
->suspended
== 0);
4158 debug_printf ("Done stopping all threads for step-over.\n");
4160 /* Note, we should always reach here with an already adjusted PC,
4161 either by GDB (if we're resuming due to GDB's request), or by our
4162 caller, if we just finished handling an internal breakpoint GDB
4163 shouldn't care about. */
4166 saved_thread
= current_thread
;
4167 current_thread
= thread
;
4169 lwp
->bp_reinsert
= pc
;
4170 uninsert_breakpoints_at (pc
);
4171 uninsert_fast_tracepoint_jumps_at (pc
);
4173 if (can_hardware_single_step ())
4179 CORE_ADDR raddr
= (*the_low_target
.breakpoint_reinsert_addr
) ();
4180 set_reinsert_breakpoint (raddr
);
4184 current_thread
= saved_thread
;
4186 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
4188 /* Require next event from this LWP. */
4189 step_over_bkpt
= thread
->entry
.id
;
4193 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
4194 start_step_over, if still there, and delete any reinsert
4195 breakpoints we've set, on non hardware single-step targets. */
4198 finish_step_over (struct lwp_info
*lwp
)
4200 if (lwp
->bp_reinsert
!= 0)
4203 debug_printf ("Finished step over.\n");
4205 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4206 may be no breakpoint to reinsert there by now. */
4207 reinsert_breakpoints_at (lwp
->bp_reinsert
);
4208 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
4210 lwp
->bp_reinsert
= 0;
4212 /* Delete any software-single-step reinsert breakpoints. No
4213 longer needed. We don't have to worry about other threads
4214 hitting this trap, and later not being able to explain it,
4215 because we were stepping over a breakpoint, and we hold all
4216 threads but LWP stopped while doing that. */
4217 if (!can_hardware_single_step ())
4218 delete_reinsert_breakpoints ();
4220 step_over_bkpt
= null_ptid
;
4227 /* This function is called once per thread. We check the thread's resume
4228 request, which will tell us whether to resume, step, or leave the thread
4229 stopped; and what signal, if any, it should be sent.
4231 For threads which we aren't explicitly told otherwise, we preserve
4232 the stepping flag; this is used for stepping over gdbserver-placed
4235 If pending_flags was set in any thread, we queue any needed
4236 signals, since we won't actually resume. We already have a pending
4237 event to report, so we don't need to preserve any step requests;
4238 they should be re-issued if necessary. */
4241 linux_resume_one_thread (struct inferior_list_entry
*entry
, void *arg
)
4243 struct thread_info
*thread
= (struct thread_info
*) entry
;
4244 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4246 int leave_all_stopped
= * (int *) arg
;
4249 if (lwp
->resume
== NULL
)
4252 if (lwp
->resume
->kind
== resume_stop
)
4255 debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread
));
4260 debug_printf ("stopping LWP %ld\n", lwpid_of (thread
));
4262 /* Stop the thread, and wait for the event asynchronously,
4263 through the event loop. */
4269 debug_printf ("already stopped LWP %ld\n",
4272 /* The LWP may have been stopped in an internal event that
4273 was not meant to be notified back to GDB (e.g., gdbserver
4274 breakpoint), so we should be reporting a stop event in
4277 /* If the thread already has a pending SIGSTOP, this is a
4278 no-op. Otherwise, something later will presumably resume
4279 the thread and this will cause it to cancel any pending
4280 operation, due to last_resume_kind == resume_stop. If
4281 the thread already has a pending status to report, we
4282 will still report it the next time we wait - see
4283 status_pending_p_callback. */
4285 /* If we already have a pending signal to report, then
4286 there's no need to queue a SIGSTOP, as this means we're
4287 midway through moving the LWP out of the jumppad, and we
4288 will report the pending signal as soon as that is
4290 if (lwp
->pending_signals_to_report
== NULL
)
4294 /* For stop requests, we're done. */
4296 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4300 /* If this thread which is about to be resumed has a pending status,
4301 then don't resume any threads - we can just report the pending
4302 status. Make sure to queue any signals that would otherwise be
4303 sent. In all-stop mode, we do this decision based on if *any*
4304 thread has a pending status. If there's a thread that needs the
4305 step-over-breakpoint dance, then don't resume any other thread
4306 but that particular one. */
4307 leave_pending
= (lwp
->status_pending_p
|| leave_all_stopped
);
4312 debug_printf ("resuming LWP %ld\n", lwpid_of (thread
));
4314 step
= (lwp
->resume
->kind
== resume_step
);
4315 linux_resume_one_lwp (lwp
, step
, lwp
->resume
->sig
, NULL
);
4320 debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread
));
4322 /* If we have a new signal, enqueue the signal. */
4323 if (lwp
->resume
->sig
!= 0)
4325 struct pending_signals
*p_sig
;
4326 p_sig
= xmalloc (sizeof (*p_sig
));
4327 p_sig
->prev
= lwp
->pending_signals
;
4328 p_sig
->signal
= lwp
->resume
->sig
;
4329 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
4331 /* If this is the same signal we were previously stopped by,
4332 make sure to queue its siginfo. We can ignore the return
4333 value of ptrace; if it fails, we'll skip
4334 PTRACE_SETSIGINFO. */
4335 if (WIFSTOPPED (lwp
->last_status
)
4336 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
)
4337 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
4340 lwp
->pending_signals
= p_sig
;
4344 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4350 linux_resume (struct thread_resume
*resume_info
, size_t n
)
4352 struct thread_resume_array array
= { resume_info
, n
};
4353 struct thread_info
*need_step_over
= NULL
;
4355 int leave_all_stopped
;
4360 debug_printf ("linux_resume:\n");
4363 find_inferior (&all_threads
, linux_set_resume_request
, &array
);
4365 /* If there is a thread which would otherwise be resumed, which has
4366 a pending status, then don't resume any threads - we can just
4367 report the pending status. Make sure to queue any signals that
4368 would otherwise be sent. In non-stop mode, we'll apply this
4369 logic to each thread individually. We consume all pending events
4370 before considering to start a step-over (in all-stop). */
4373 find_inferior (&all_threads
, resume_status_pending_p
, &any_pending
);
4375 /* If there is a thread which would otherwise be resumed, which is
4376 stopped at a breakpoint that needs stepping over, then don't
4377 resume any threads - have it step over the breakpoint with all
4378 other threads stopped, then resume all threads again. Make sure
4379 to queue any signals that would otherwise be delivered or
4381 if (!any_pending
&& supports_breakpoints ())
4383 = (struct thread_info
*) find_inferior (&all_threads
,
4384 need_step_over_p
, NULL
);
4386 leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
4390 if (need_step_over
!= NULL
)
4391 debug_printf ("Not resuming all, need step over\n");
4392 else if (any_pending
)
4393 debug_printf ("Not resuming, all-stop and found "
4394 "an LWP with pending status\n");
4396 debug_printf ("Resuming, no pending status or step over needed\n");
4399 /* Even if we're leaving threads stopped, queue all signals we'd
4400 otherwise deliver. */
4401 find_inferior (&all_threads
, linux_resume_one_thread
, &leave_all_stopped
);
4404 start_step_over (get_thread_lwp (need_step_over
));
4408 debug_printf ("linux_resume done\n");
4413 /* This function is called once per thread. We check the thread's
4414 last resume request, which will tell us whether to resume, step, or
4415 leave the thread stopped. Any signal the client requested to be
4416 delivered has already been enqueued at this point.
4418 If any thread that GDB wants running is stopped at an internal
4419 breakpoint that needs stepping over, we start a step-over operation
4420 on that particular thread, and leave all others stopped. */
4423 proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
4425 struct thread_info
*thread
= (struct thread_info
*) entry
;
4426 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4433 debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread
));
4438 debug_printf (" LWP %ld already running\n", lwpid_of (thread
));
4442 if (thread
->last_resume_kind
== resume_stop
4443 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4446 debug_printf (" client wants LWP to remain %ld stopped\n",
4451 if (lwp
->status_pending_p
)
4454 debug_printf (" LWP %ld has pending status, leaving stopped\n",
4459 gdb_assert (lwp
->suspended
>= 0);
4464 debug_printf (" LWP %ld is suspended\n", lwpid_of (thread
));
4468 if (thread
->last_resume_kind
== resume_stop
4469 && lwp
->pending_signals_to_report
== NULL
4470 && lwp
->collecting_fast_tracepoint
== 0)
4472 /* We haven't reported this LWP as stopped yet (otherwise, the
4473 last_status.kind check above would catch it, and we wouldn't
4474 reach here. This LWP may have been momentarily paused by a
4475 stop_all_lwps call while handling for example, another LWP's
4476 step-over. In that case, the pending expected SIGSTOP signal
4477 that was queued at vCont;t handling time will have already
4478 been consumed by wait_for_sigstop, and so we need to requeue
4479 another one here. Note that if the LWP already has a SIGSTOP
4480 pending, this is a no-op. */
4483 debug_printf ("Client wants LWP %ld to stop. "
4484 "Making sure it has a SIGSTOP pending\n",
4490 step
= thread
->last_resume_kind
== resume_step
;
4491 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
4496 unsuspend_and_proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
4498 struct thread_info
*thread
= (struct thread_info
*) entry
;
4499 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4505 gdb_assert (lwp
->suspended
>= 0);
4507 return proceed_one_lwp (entry
, except
);
4510 /* When we finish a step-over, set threads running again. If there's
4511 another thread that may need a step-over, now's the time to start
4512 it. Eventually, we'll move all threads past their breakpoints. */
4515 proceed_all_lwps (void)
4517 struct thread_info
*need_step_over
;
4519 /* If there is a thread which would otherwise be resumed, which is
4520 stopped at a breakpoint that needs stepping over, then don't
4521 resume any threads - have it step over the breakpoint with all
4522 other threads stopped, then resume all threads again. */
4524 if (supports_breakpoints ())
4527 = (struct thread_info
*) find_inferior (&all_threads
,
4528 need_step_over_p
, NULL
);
4530 if (need_step_over
!= NULL
)
4533 debug_printf ("proceed_all_lwps: found "
4534 "thread %ld needing a step-over\n",
4535 lwpid_of (need_step_over
));
4537 start_step_over (get_thread_lwp (need_step_over
));
4543 debug_printf ("Proceeding, no step-over needed\n");
4545 find_inferior (&all_threads
, proceed_one_lwp
, NULL
);
4548 /* Stopped LWPs that the client wanted to be running, that don't have
4549 pending statuses, are set to run again, except for EXCEPT, if not
4550 NULL. This undoes a stop_all_lwps call. */
4553 unstop_all_lwps (int unsuspend
, struct lwp_info
*except
)
4559 debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
4560 lwpid_of (get_lwp_thread (except
)));
4562 debug_printf ("unstopping all lwps\n");
4566 find_inferior (&all_threads
, unsuspend_and_proceed_one_lwp
, except
);
4568 find_inferior (&all_threads
, proceed_one_lwp
, except
);
4572 debug_printf ("unstop_all_lwps done\n");
4578 #ifdef HAVE_LINUX_REGSETS
4580 #define use_linux_regsets 1
4582 /* Returns true if REGSET has been disabled. */
4585 regset_disabled (struct regsets_info
*info
, struct regset_info
*regset
)
4587 return (info
->disabled_regsets
!= NULL
4588 && info
->disabled_regsets
[regset
- info
->regsets
]);
4591 /* Disable REGSET. */
4594 disable_regset (struct regsets_info
*info
, struct regset_info
*regset
)
4598 dr_offset
= regset
- info
->regsets
;
4599 if (info
->disabled_regsets
== NULL
)
4600 info
->disabled_regsets
= xcalloc (1, info
->num_regsets
);
4601 info
->disabled_regsets
[dr_offset
] = 1;
4605 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
4606 struct regcache
*regcache
)
4608 struct regset_info
*regset
;
4609 int saw_general_regs
= 0;
4613 pid
= lwpid_of (current_thread
);
4614 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
4619 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
4622 buf
= xmalloc (regset
->size
);
4624 nt_type
= regset
->nt_type
;
4628 iov
.iov_len
= regset
->size
;
4629 data
= (void *) &iov
;
4635 res
= ptrace (regset
->get_request
, pid
,
4636 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
4638 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4644 /* If we get EIO on a regset, do not try it again for
4645 this process mode. */
4646 disable_regset (regsets_info
, regset
);
4648 else if (errno
== ENODATA
)
4650 /* ENODATA may be returned if the regset is currently
4651 not "active". This can happen in normal operation,
4652 so suppress the warning in this case. */
4657 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
4664 if (regset
->type
== GENERAL_REGS
)
4665 saw_general_regs
= 1;
4666 regset
->store_function (regcache
, buf
);
4670 if (saw_general_regs
)
4677 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
4678 struct regcache
*regcache
)
4680 struct regset_info
*regset
;
4681 int saw_general_regs
= 0;
4685 pid
= lwpid_of (current_thread
);
4686 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
4691 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
)
4692 || regset
->fill_function
== NULL
)
4695 buf
= xmalloc (regset
->size
);
4697 /* First fill the buffer with the current register set contents,
4698 in case there are any items in the kernel's regset that are
4699 not in gdbserver's regcache. */
4701 nt_type
= regset
->nt_type
;
4705 iov
.iov_len
= regset
->size
;
4706 data
= (void *) &iov
;
4712 res
= ptrace (regset
->get_request
, pid
,
4713 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
4715 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4720 /* Then overlay our cached registers on that. */
4721 regset
->fill_function (regcache
, buf
);
4723 /* Only now do we write the register set. */
4725 res
= ptrace (regset
->set_request
, pid
,
4726 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
4728 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
4736 /* If we get EIO on a regset, do not try it again for
4737 this process mode. */
4738 disable_regset (regsets_info
, regset
);
4740 else if (errno
== ESRCH
)
4742 /* At this point, ESRCH should mean the process is
4743 already gone, in which case we simply ignore attempts
4744 to change its registers. See also the related
4745 comment in linux_resume_one_lwp. */
4751 perror ("Warning: ptrace(regsets_store_inferior_registers)");
4754 else if (regset
->type
== GENERAL_REGS
)
4755 saw_general_regs
= 1;
4758 if (saw_general_regs
)
4764 #else /* !HAVE_LINUX_REGSETS */
4766 #define use_linux_regsets 0
4767 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
4768 #define regsets_store_inferior_registers(regsets_info, regcache) 1
4772 /* Return 1 if register REGNO is supported by one of the regset ptrace
4773 calls or 0 if it has to be transferred individually. */
4776 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
4778 unsigned char mask
= 1 << (regno
% 8);
4779 size_t index
= regno
/ 8;
4781 return (use_linux_regsets
4782 && (regs_info
->regset_bitmap
== NULL
4783 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
4786 #ifdef HAVE_LINUX_USRREGS
4789 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
4793 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
4794 error ("Invalid register number %d.", regnum
);
4796 addr
= usrregs
->regmap
[regnum
];
4801 /* Fetch one register. */
4803 fetch_register (const struct usrregs_info
*usrregs
,
4804 struct regcache
*regcache
, int regno
)
4811 if (regno
>= usrregs
->num_regs
)
4813 if ((*the_low_target
.cannot_fetch_register
) (regno
))
4816 regaddr
= register_addr (usrregs
, regno
);
4820 size
= ((register_size (regcache
->tdesc
, regno
)
4821 + sizeof (PTRACE_XFER_TYPE
) - 1)
4822 & -sizeof (PTRACE_XFER_TYPE
));
4823 buf
= alloca (size
);
4825 pid
= lwpid_of (current_thread
);
4826 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
4829 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
4830 ptrace (PTRACE_PEEKUSER
, pid
,
4831 /* Coerce to a uintptr_t first to avoid potential gcc warning
4832 of coercing an 8 byte integer to a 4 byte pointer. */
4833 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
, (PTRACE_TYPE_ARG4
) 0);
4834 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
4836 error ("reading register %d: %s", regno
, strerror (errno
));
4839 if (the_low_target
.supply_ptrace_register
)
4840 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
4842 supply_register (regcache
, regno
, buf
);
4845 /* Store one register. */
4847 store_register (const struct usrregs_info
*usrregs
,
4848 struct regcache
*regcache
, int regno
)
4855 if (regno
>= usrregs
->num_regs
)
4857 if ((*the_low_target
.cannot_store_register
) (regno
))
4860 regaddr
= register_addr (usrregs
, regno
);
4864 size
= ((register_size (regcache
->tdesc
, regno
)
4865 + sizeof (PTRACE_XFER_TYPE
) - 1)
4866 & -sizeof (PTRACE_XFER_TYPE
));
4867 buf
= alloca (size
);
4868 memset (buf
, 0, size
);
4870 if (the_low_target
.collect_ptrace_register
)
4871 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
4873 collect_register (regcache
, regno
, buf
);
4875 pid
= lwpid_of (current_thread
);
4876 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
4879 ptrace (PTRACE_POKEUSER
, pid
,
4880 /* Coerce to a uintptr_t first to avoid potential gcc warning
4881 about coercing an 8 byte integer to a 4 byte pointer. */
4882 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
,
4883 (PTRACE_TYPE_ARG4
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
4886 /* At this point, ESRCH should mean the process is
4887 already gone, in which case we simply ignore attempts
4888 to change its registers. See also the related
4889 comment in linux_resume_one_lwp. */
4893 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
4894 error ("writing register %d: %s", regno
, strerror (errno
));
4896 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
4900 /* Fetch all registers, or just one, from the child process.
4901 If REGNO is -1, do this for all registers, skipping any that are
4902 assumed to have been retrieved by regsets_fetch_inferior_registers,
4903 unless ALL is non-zero.
4904 Otherwise, REGNO specifies which register (so we can save time). */
4906 usr_fetch_inferior_registers (const struct regs_info
*regs_info
,
4907 struct regcache
*regcache
, int regno
, int all
)
4909 struct usrregs_info
*usr
= regs_info
->usrregs
;
4913 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
4914 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
4915 fetch_register (usr
, regcache
, regno
);
4918 fetch_register (usr
, regcache
, regno
);
4921 /* Store our register values back into the inferior.
4922 If REGNO is -1, do this for all registers, skipping any that are
4923 assumed to have been saved by regsets_store_inferior_registers,
4924 unless ALL is non-zero.
4925 Otherwise, REGNO specifies which register (so we can save time). */
4927 usr_store_inferior_registers (const struct regs_info
*regs_info
,
4928 struct regcache
*regcache
, int regno
, int all
)
4930 struct usrregs_info
*usr
= regs_info
->usrregs
;
4934 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
4935 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
4936 store_register (usr
, regcache
, regno
);
4939 store_register (usr
, regcache
, regno
);
4942 #else /* !HAVE_LINUX_USRREGS */
4944 #define usr_fetch_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
4945 #define usr_store_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
4951 linux_fetch_registers (struct regcache
*regcache
, int regno
)
4955 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
4959 if (the_low_target
.fetch_register
!= NULL
4960 && regs_info
->usrregs
!= NULL
)
4961 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
4962 (*the_low_target
.fetch_register
) (regcache
, regno
);
4964 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
4965 if (regs_info
->usrregs
!= NULL
)
4966 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
4970 if (the_low_target
.fetch_register
!= NULL
4971 && (*the_low_target
.fetch_register
) (regcache
, regno
))
4974 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
4976 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
4978 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
4979 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
4984 linux_store_registers (struct regcache
*regcache
, int regno
)
4988 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
4992 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
4994 if (regs_info
->usrregs
!= NULL
)
4995 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
4999 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5001 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5003 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5004 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
5009 /* Copy LEN bytes from inferior's memory starting at MEMADDR
5010 to debugger memory starting at MYADDR. */
5013 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
5015 int pid
= lwpid_of (current_thread
);
5016 register PTRACE_XFER_TYPE
*buffer
;
5017 register CORE_ADDR addr
;
5024 /* Try using /proc. Don't bother for one word. */
5025 if (len
>= 3 * sizeof (long))
5029 /* We could keep this file open and cache it - possibly one per
5030 thread. That requires some juggling, but is even faster. */
5031 sprintf (filename
, "/proc/%d/mem", pid
);
5032 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
5036 /* If pread64 is available, use it. It's faster if the kernel
5037 supports it (only one syscall), and it's 64-bit safe even on
5038 32-bit platforms (for instance, SPARC debugging a SPARC64
5041 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
5044 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
5045 bytes
= read (fd
, myaddr
, len
);
5052 /* Some data was read, we'll try to get the rest with ptrace. */
5062 /* Round starting address down to longword boundary. */
5063 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5064 /* Round ending address up; get number of longwords that makes. */
5065 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5066 / sizeof (PTRACE_XFER_TYPE
));
5067 /* Allocate buffer of that many longwords. */
5068 buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
5070 /* Read all the longwords */
5072 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5074 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5075 about coercing an 8 byte integer to a 4 byte pointer. */
5076 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
5077 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5078 (PTRACE_TYPE_ARG4
) 0);
5084 /* Copy appropriate bytes out of the buffer. */
5087 i
*= sizeof (PTRACE_XFER_TYPE
);
5088 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
5090 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5097 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5098 memory at MEMADDR. On failure (cannot write to the inferior)
5099 returns the value of errno. Always succeeds if LEN is zero. */
5102 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
5105 /* Round starting address down to longword boundary. */
5106 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5107 /* Round ending address up; get number of longwords that makes. */
5109 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5110 / sizeof (PTRACE_XFER_TYPE
);
5112 /* Allocate buffer of that many longwords. */
5113 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*)
5114 alloca (count
* sizeof (PTRACE_XFER_TYPE
));
5116 int pid
= lwpid_of (current_thread
);
5120 /* Zero length write always succeeds. */
5126 /* Dump up to four bytes. */
5127 unsigned int val
= * (unsigned int *) myaddr
;
5133 val
= val
& 0xffffff;
5134 debug_printf ("Writing %0*x to 0x%08lx in process %d\n",
5135 2 * ((len
< 4) ? len
: 4), val
, (long)memaddr
, pid
);
5138 /* Fill start and end extra bytes of buffer with existing memory data. */
5141 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5142 about coercing an 8 byte integer to a 4 byte pointer. */
5143 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
5144 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5145 (PTRACE_TYPE_ARG4
) 0);
5153 = ptrace (PTRACE_PEEKTEXT
, pid
,
5154 /* Coerce to a uintptr_t first to avoid potential gcc warning
5155 about coercing an 8 byte integer to a 4 byte pointer. */
5156 (PTRACE_TYPE_ARG3
) (uintptr_t) (addr
+ (count
- 1)
5157 * sizeof (PTRACE_XFER_TYPE
)),
5158 (PTRACE_TYPE_ARG4
) 0);
5163 /* Copy data to be written over corresponding part of buffer. */
5165 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5168 /* Write the entire buffer. */
5170 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5173 ptrace (PTRACE_POKETEXT
, pid
,
5174 /* Coerce to a uintptr_t first to avoid potential gcc warning
5175 about coercing an 8 byte integer to a 4 byte pointer. */
5176 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5177 (PTRACE_TYPE_ARG4
) buffer
[i
]);
5186 linux_look_up_symbols (void)
5188 #ifdef USE_THREAD_DB
5189 struct process_info
*proc
= current_process ();
5191 if (proc
->priv
->thread_db
!= NULL
)
5194 /* If the kernel supports tracing clones, then we don't need to
5195 use the magic thread event breakpoint to learn about
5197 thread_db_init (!linux_supports_traceclone ());
5202 linux_request_interrupt (void)
5204 extern unsigned long signal_pid
;
5206 /* Send a SIGINT to the process group. This acts just like the user
5207 typed a ^C on the controlling terminal. */
5208 kill (-signal_pid
, SIGINT
);
5211 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5212 to debugger memory starting at MYADDR. */
5215 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
5217 char filename
[PATH_MAX
];
5219 int pid
= lwpid_of (current_thread
);
5221 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5223 fd
= open (filename
, O_RDONLY
);
5227 if (offset
!= (CORE_ADDR
) 0
5228 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5231 n
= read (fd
, myaddr
, len
);
5238 /* These breakpoint and watchpoint related wrapper functions simply
5239 pass on the function call if the target has registered a
5240 corresponding function. */
5243 linux_supports_z_point_type (char z_type
)
5245 return (the_low_target
.supports_z_point_type
!= NULL
5246 && the_low_target
.supports_z_point_type (z_type
));
5250 linux_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5251 int size
, struct raw_breakpoint
*bp
)
5253 if (type
== raw_bkpt_type_sw
)
5254 return insert_memory_breakpoint (bp
);
5255 else if (the_low_target
.insert_point
!= NULL
)
5256 return the_low_target
.insert_point (type
, addr
, size
, bp
);
5258 /* Unsupported (see target.h). */
5263 linux_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5264 int size
, struct raw_breakpoint
*bp
)
5266 if (type
== raw_bkpt_type_sw
)
5267 return remove_memory_breakpoint (bp
);
5268 else if (the_low_target
.remove_point
!= NULL
)
5269 return the_low_target
.remove_point (type
, addr
, size
, bp
);
5271 /* Unsupported (see target.h). */
5275 /* Implement the to_stopped_by_sw_breakpoint target_ops
5279 linux_stopped_by_sw_breakpoint (void)
5281 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5283 return (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
);
5286 /* Implement the to_supports_stopped_by_sw_breakpoint target_ops
5290 linux_supports_stopped_by_sw_breakpoint (void)
5292 return USE_SIGTRAP_SIGINFO
;
5295 /* Implement the to_stopped_by_hw_breakpoint target_ops
5299 linux_stopped_by_hw_breakpoint (void)
5301 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5303 return (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
);
5306 /* Implement the to_supports_stopped_by_hw_breakpoint target_ops
5310 linux_supports_stopped_by_hw_breakpoint (void)
5312 return USE_SIGTRAP_SIGINFO
;
5315 /* Implement the supports_conditional_breakpoints target_ops
5319 linux_supports_conditional_breakpoints (void)
5321 /* GDBserver needs to step over the breakpoint if the condition is
5322 false. GDBserver software single step is too simple, so disable
5323 conditional breakpoints if the target doesn't have hardware single
5325 return can_hardware_single_step ();
5329 linux_stopped_by_watchpoint (void)
5331 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5333 return lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
5337 linux_stopped_data_address (void)
5339 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5341 return lwp
->stopped_data_address
;
5344 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
5345 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
5346 && defined(PT_TEXT_END_ADDR)
5348 /* This is only used for targets that define PT_TEXT_ADDR,
5349 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5350 the target has different ways of acquiring this information, like
5353 /* Under uClinux, programs are loaded at non-zero offsets, which we need
5354 to tell gdb about. */
5357 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
5359 unsigned long text
, text_end
, data
;
5360 int pid
= lwpid_of (current_thread
);
5364 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_ADDR
,
5365 (PTRACE_TYPE_ARG4
) 0);
5366 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_END_ADDR
,
5367 (PTRACE_TYPE_ARG4
) 0);
5368 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_DATA_ADDR
,
5369 (PTRACE_TYPE_ARG4
) 0);
5373 /* Both text and data offsets produced at compile-time (and so
5374 used by gdb) are relative to the beginning of the program,
5375 with the data segment immediately following the text segment.
5376 However, the actual runtime layout in memory may put the data
5377 somewhere else, so when we send gdb a data base-address, we
5378 use the real data base address and subtract the compile-time
5379 data base-address from it (which is just the length of the
5380 text segment). BSS immediately follows data in both
5383 *data_p
= data
- (text_end
- text
);
5392 linux_qxfer_osdata (const char *annex
,
5393 unsigned char *readbuf
, unsigned const char *writebuf
,
5394 CORE_ADDR offset
, int len
)
5396 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
5399 /* Convert a native/host siginfo object, into/from the siginfo in the
5400 layout of the inferiors' architecture. */
5403 siginfo_fixup (siginfo_t
*siginfo
, void *inf_siginfo
, int direction
)
5407 if (the_low_target
.siginfo_fixup
!= NULL
)
5408 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
5410 /* If there was no callback, or the callback didn't do anything,
5411 then just do a straight memcpy. */
5415 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
5417 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
5422 linux_xfer_siginfo (const char *annex
, unsigned char *readbuf
,
5423 unsigned const char *writebuf
, CORE_ADDR offset
, int len
)
5427 char inf_siginfo
[sizeof (siginfo_t
)];
5429 if (current_thread
== NULL
)
5432 pid
= lwpid_of (current_thread
);
5435 debug_printf ("%s siginfo for lwp %d.\n",
5436 readbuf
!= NULL
? "Reading" : "Writing",
5439 if (offset
>= sizeof (siginfo
))
5442 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
5445 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
5446 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
5447 inferior with a 64-bit GDBSERVER should look the same as debugging it
5448 with a 32-bit GDBSERVER, we need to convert it. */
5449 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
5451 if (offset
+ len
> sizeof (siginfo
))
5452 len
= sizeof (siginfo
) - offset
;
5454 if (readbuf
!= NULL
)
5455 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
5458 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
5460 /* Convert back to ptrace layout before flushing it out. */
5461 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
5463 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
5470 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
5471 so we notice when children change state; as the handler for the
5472 sigsuspend in my_waitpid. */
5475 sigchld_handler (int signo
)
5477 int old_errno
= errno
;
5483 /* fprintf is not async-signal-safe, so call write
5485 if (write (2, "sigchld_handler\n",
5486 sizeof ("sigchld_handler\n") - 1) < 0)
5487 break; /* just ignore */
5491 if (target_is_async_p ())
5492 async_file_mark (); /* trigger a linux_wait */
5498 linux_supports_non_stop (void)
5504 linux_async (int enable
)
5506 int previous
= target_is_async_p ();
5509 debug_printf ("linux_async (%d), previous=%d\n",
5512 if (previous
!= enable
)
5515 sigemptyset (&mask
);
5516 sigaddset (&mask
, SIGCHLD
);
5518 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
5522 if (pipe (linux_event_pipe
) == -1)
5524 linux_event_pipe
[0] = -1;
5525 linux_event_pipe
[1] = -1;
5526 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
5528 warning ("creating event pipe failed.");
5532 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
5533 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
5535 /* Register the event loop handler. */
5536 add_file_handler (linux_event_pipe
[0],
5537 handle_target_event
, NULL
);
5539 /* Always trigger a linux_wait. */
5544 delete_file_handler (linux_event_pipe
[0]);
5546 close (linux_event_pipe
[0]);
5547 close (linux_event_pipe
[1]);
5548 linux_event_pipe
[0] = -1;
5549 linux_event_pipe
[1] = -1;
5552 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
5559 linux_start_non_stop (int nonstop
)
5561 /* Register or unregister from event-loop accordingly. */
5562 linux_async (nonstop
);
5564 if (target_is_async_p () != (nonstop
!= 0))
5571 linux_supports_multi_process (void)
5576 /* Check if fork events are supported. */
5579 linux_supports_fork_events (void)
5581 return linux_supports_tracefork ();
5584 /* Check if vfork events are supported. */
5587 linux_supports_vfork_events (void)
5589 return linux_supports_tracefork ();
5592 /* Callback for 'find_inferior'. Set the (possibly changed) ptrace
5593 options for the specified lwp. */
5596 reset_lwp_ptrace_options_callback (struct inferior_list_entry
*entry
,
5599 struct thread_info
*thread
= (struct thread_info
*) entry
;
5600 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5604 /* Stop the lwp so we can modify its ptrace options. */
5605 lwp
->must_set_ptrace_flags
= 1;
5606 linux_stop_lwp (lwp
);
5610 /* Already stopped; go ahead and set the ptrace options. */
5611 struct process_info
*proc
= find_process_pid (pid_of (thread
));
5612 int options
= linux_low_ptrace_options (proc
->attached
);
5614 linux_enable_event_reporting (lwpid_of (thread
), options
);
5615 lwp
->must_set_ptrace_flags
= 0;
5621 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
5622 ptrace flags for all inferiors. This is in case the new GDB connection
5623 doesn't support the same set of events that the previous one did. */
5626 linux_handle_new_gdb_connection (void)
5630 /* Request that all the lwps reset their ptrace options. */
5631 find_inferior (&all_threads
, reset_lwp_ptrace_options_callback
, &pid
);
5635 linux_supports_disable_randomization (void)
5637 #ifdef HAVE_PERSONALITY
5645 linux_supports_agent (void)
5651 linux_supports_range_stepping (void)
5653 if (*the_low_target
.supports_range_stepping
== NULL
)
5656 return (*the_low_target
.supports_range_stepping
) ();
5659 /* Enumerate spufs IDs for process PID. */
5661 spu_enumerate_spu_ids (long pid
, unsigned char *buf
, CORE_ADDR offset
, int len
)
5667 struct dirent
*entry
;
5669 sprintf (path
, "/proc/%ld/fd", pid
);
5670 dir
= opendir (path
);
5675 while ((entry
= readdir (dir
)) != NULL
)
5681 fd
= atoi (entry
->d_name
);
5685 sprintf (path
, "/proc/%ld/fd/%d", pid
, fd
);
5686 if (stat (path
, &st
) != 0)
5688 if (!S_ISDIR (st
.st_mode
))
5691 if (statfs (path
, &stfs
) != 0)
5693 if (stfs
.f_type
!= SPUFS_MAGIC
)
5696 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
5698 *(unsigned int *)(buf
+ pos
- offset
) = fd
;
5708 /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
5709 object type, using the /proc file system. */
5711 linux_qxfer_spu (const char *annex
, unsigned char *readbuf
,
5712 unsigned const char *writebuf
,
5713 CORE_ADDR offset
, int len
)
5715 long pid
= lwpid_of (current_thread
);
5720 if (!writebuf
&& !readbuf
)
5728 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
5731 sprintf (buf
, "/proc/%ld/fd/%s", pid
, annex
);
5732 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
5737 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5744 ret
= write (fd
, writebuf
, (size_t) len
);
5746 ret
= read (fd
, readbuf
, (size_t) len
);
5752 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
5753 struct target_loadseg
5755 /* Core address to which the segment is mapped. */
5757 /* VMA recorded in the program header. */
5759 /* Size of this segment in memory. */
5763 # if defined PT_GETDSBT
5764 struct target_loadmap
5766 /* Protocol version number, must be zero. */
5768 /* Pointer to the DSBT table, its size, and the DSBT index. */
5769 unsigned *dsbt_table
;
5770 unsigned dsbt_size
, dsbt_index
;
5771 /* Number of segments in this map. */
5773 /* The actual memory map. */
5774 struct target_loadseg segs
[/*nsegs*/];
5776 # define LINUX_LOADMAP PT_GETDSBT
5777 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
5778 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
5780 struct target_loadmap
5782 /* Protocol version number, must be zero. */
5784 /* Number of segments in this map. */
5786 /* The actual memory map. */
5787 struct target_loadseg segs
[/*nsegs*/];
5789 # define LINUX_LOADMAP PTRACE_GETFDPIC
5790 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
5791 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
5795 linux_read_loadmap (const char *annex
, CORE_ADDR offset
,
5796 unsigned char *myaddr
, unsigned int len
)
5798 int pid
= lwpid_of (current_thread
);
5800 struct target_loadmap
*data
= NULL
;
5801 unsigned int actual_length
, copy_length
;
5803 if (strcmp (annex
, "exec") == 0)
5804 addr
= (int) LINUX_LOADMAP_EXEC
;
5805 else if (strcmp (annex
, "interp") == 0)
5806 addr
= (int) LINUX_LOADMAP_INTERP
;
5810 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
5816 actual_length
= sizeof (struct target_loadmap
)
5817 + sizeof (struct target_loadseg
) * data
->nsegs
;
5819 if (offset
< 0 || offset
> actual_length
)
5822 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
5823 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
5827 # define linux_read_loadmap NULL
5828 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
5831 linux_process_qsupported (const char *query
)
5833 if (the_low_target
.process_qsupported
!= NULL
)
5834 the_low_target
.process_qsupported (query
);
5838 linux_supports_tracepoints (void)
5840 if (*the_low_target
.supports_tracepoints
== NULL
)
5843 return (*the_low_target
.supports_tracepoints
) ();
5847 linux_read_pc (struct regcache
*regcache
)
5849 if (the_low_target
.get_pc
== NULL
)
5852 return (*the_low_target
.get_pc
) (regcache
);
5856 linux_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
5858 gdb_assert (the_low_target
.set_pc
!= NULL
);
5860 (*the_low_target
.set_pc
) (regcache
, pc
);
5864 linux_thread_stopped (struct thread_info
*thread
)
5866 return get_thread_lwp (thread
)->stopped
;
5869 /* This exposes stop-all-threads functionality to other modules. */
5872 linux_pause_all (int freeze
)
5874 stop_all_lwps (freeze
, NULL
);
5877 /* This exposes unstop-all-threads functionality to other gdbserver
5881 linux_unpause_all (int unfreeze
)
5883 unstop_all_lwps (unfreeze
, NULL
);
5887 linux_prepare_to_access_memory (void)
5889 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5892 linux_pause_all (1);
5897 linux_done_accessing_memory (void)
5899 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5902 linux_unpause_all (1);
5906 linux_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
, CORE_ADDR tpaddr
,
5907 CORE_ADDR collector
,
5910 CORE_ADDR
*jump_entry
,
5911 CORE_ADDR
*trampoline
,
5912 ULONGEST
*trampoline_size
,
5913 unsigned char *jjump_pad_insn
,
5914 ULONGEST
*jjump_pad_insn_size
,
5915 CORE_ADDR
*adjusted_insn_addr
,
5916 CORE_ADDR
*adjusted_insn_addr_end
,
5919 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
5920 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
5921 jump_entry
, trampoline
, trampoline_size
,
5922 jjump_pad_insn
, jjump_pad_insn_size
,
5923 adjusted_insn_addr
, adjusted_insn_addr_end
,
5927 static struct emit_ops
*
5928 linux_emit_ops (void)
5930 if (the_low_target
.emit_ops
!= NULL
)
5931 return (*the_low_target
.emit_ops
) ();
5937 linux_get_min_fast_tracepoint_insn_len (void)
5939 return (*the_low_target
.get_min_fast_tracepoint_insn_len
) ();
5942 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
5945 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
5946 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
5948 char filename
[PATH_MAX
];
5950 const int auxv_size
= is_elf64
5951 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
5952 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
5954 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5956 fd
= open (filename
, O_RDONLY
);
5962 while (read (fd
, buf
, auxv_size
) == auxv_size
5963 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
5967 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
5969 switch (aux
->a_type
)
5972 *phdr_memaddr
= aux
->a_un
.a_val
;
5975 *num_phdr
= aux
->a_un
.a_val
;
5981 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
5983 switch (aux
->a_type
)
5986 *phdr_memaddr
= aux
->a_un
.a_val
;
5989 *num_phdr
= aux
->a_un
.a_val
;
5997 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
5999 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6000 "phdr_memaddr = %ld, phdr_num = %d",
6001 (long) *phdr_memaddr
, *num_phdr
);
6008 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6011 get_dynamic (const int pid
, const int is_elf64
)
6013 CORE_ADDR phdr_memaddr
, relocation
;
6015 unsigned char *phdr_buf
;
6016 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
6018 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
6021 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
6022 phdr_buf
= alloca (num_phdr
* phdr_size
);
6024 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
6027 /* Compute relocation: it is expected to be 0 for "regular" executables,
6028 non-zero for PIE ones. */
6030 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
6033 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6035 if (p
->p_type
== PT_PHDR
)
6036 relocation
= phdr_memaddr
- p
->p_vaddr
;
6040 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6042 if (p
->p_type
== PT_PHDR
)
6043 relocation
= phdr_memaddr
- p
->p_vaddr
;
6046 if (relocation
== -1)
6048 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6049 any real world executables, including PIE executables, have always
6050 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6051 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6052 or present DT_DEBUG anyway (fpc binaries are statically linked).
6054 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6056 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6061 for (i
= 0; i
< num_phdr
; i
++)
6065 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6067 if (p
->p_type
== PT_DYNAMIC
)
6068 return p
->p_vaddr
+ relocation
;
6072 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6074 if (p
->p_type
== PT_DYNAMIC
)
6075 return p
->p_vaddr
+ relocation
;
6082 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6083 can be 0 if the inferior does not yet have the library list initialized.
6084 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6085 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6088 get_r_debug (const int pid
, const int is_elf64
)
6090 CORE_ADDR dynamic_memaddr
;
6091 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
6092 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
6095 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
6096 if (dynamic_memaddr
== 0)
6099 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
6103 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
6104 #ifdef DT_MIPS_RLD_MAP
6108 unsigned char buf
[sizeof (Elf64_Xword
)];
6112 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6114 if (linux_read_memory (dyn
->d_un
.d_val
,
6115 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6120 #endif /* DT_MIPS_RLD_MAP */
6122 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6123 map
= dyn
->d_un
.d_val
;
6125 if (dyn
->d_tag
== DT_NULL
)
6130 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
6131 #ifdef DT_MIPS_RLD_MAP
6135 unsigned char buf
[sizeof (Elf32_Word
)];
6139 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6141 if (linux_read_memory (dyn
->d_un
.d_val
,
6142 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6147 #endif /* DT_MIPS_RLD_MAP */
6149 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6150 map
= dyn
->d_un
.d_val
;
6152 if (dyn
->d_tag
== DT_NULL
)
6156 dynamic_memaddr
+= dyn_size
;
6162 /* Read one pointer from MEMADDR in the inferior. */
6165 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
6169 /* Go through a union so this works on either big or little endian
6170 hosts, when the inferior's pointer size is smaller than the size
6171 of CORE_ADDR. It is assumed the inferior's endianness is the
6172 same of the superior's. */
6175 CORE_ADDR core_addr
;
6180 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
6183 if (ptr_size
== sizeof (CORE_ADDR
))
6184 *ptr
= addr
.core_addr
;
6185 else if (ptr_size
== sizeof (unsigned int))
6188 gdb_assert_not_reached ("unhandled pointer size");
6193 struct link_map_offsets
6195 /* Offset and size of r_debug.r_version. */
6196 int r_version_offset
;
6198 /* Offset and size of r_debug.r_map. */
6201 /* Offset to l_addr field in struct link_map. */
6204 /* Offset to l_name field in struct link_map. */
6207 /* Offset to l_ld field in struct link_map. */
6210 /* Offset to l_next field in struct link_map. */
6213 /* Offset to l_prev field in struct link_map. */
6217 /* Construct qXfer:libraries-svr4:read reply. */
6220 linux_qxfer_libraries_svr4 (const char *annex
, unsigned char *readbuf
,
6221 unsigned const char *writebuf
,
6222 CORE_ADDR offset
, int len
)
6225 unsigned document_len
;
6226 struct process_info_private
*const priv
= current_process ()->priv
;
6227 char filename
[PATH_MAX
];
6230 static const struct link_map_offsets lmo_32bit_offsets
=
6232 0, /* r_version offset. */
6233 4, /* r_debug.r_map offset. */
6234 0, /* l_addr offset in link_map. */
6235 4, /* l_name offset in link_map. */
6236 8, /* l_ld offset in link_map. */
6237 12, /* l_next offset in link_map. */
6238 16 /* l_prev offset in link_map. */
6241 static const struct link_map_offsets lmo_64bit_offsets
=
6243 0, /* r_version offset. */
6244 8, /* r_debug.r_map offset. */
6245 0, /* l_addr offset in link_map. */
6246 8, /* l_name offset in link_map. */
6247 16, /* l_ld offset in link_map. */
6248 24, /* l_next offset in link_map. */
6249 32 /* l_prev offset in link_map. */
6251 const struct link_map_offsets
*lmo
;
6252 unsigned int machine
;
6254 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
6255 int allocated
= 1024;
6257 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
6258 int header_done
= 0;
6260 if (writebuf
!= NULL
)
6262 if (readbuf
== NULL
)
6265 pid
= lwpid_of (current_thread
);
6266 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
6267 is_elf64
= elf_64_file_p (filename
, &machine
);
6268 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
6269 ptr_size
= is_elf64
? 8 : 4;
6271 while (annex
[0] != '\0')
6277 sep
= strchr (annex
, '=');
6282 if (len
== 5 && startswith (annex
, "start"))
6284 else if (len
== 4 && startswith (annex
, "prev"))
6288 annex
= strchr (sep
, ';');
6295 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
6302 if (priv
->r_debug
== 0)
6303 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
6305 /* We failed to find DT_DEBUG. Such situation will not change
6306 for this inferior - do not retry it. Report it to GDB as
6307 E01, see for the reasons at the GDB solib-svr4.c side. */
6308 if (priv
->r_debug
== (CORE_ADDR
) -1)
6311 if (priv
->r_debug
!= 0)
6313 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
6314 (unsigned char *) &r_version
,
6315 sizeof (r_version
)) != 0
6318 warning ("unexpected r_debug version %d", r_version
);
6320 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
6321 &lm_addr
, ptr_size
) != 0)
6323 warning ("unable to read r_map from 0x%lx",
6324 (long) priv
->r_debug
+ lmo
->r_map_offset
);
6329 document
= xmalloc (allocated
);
6330 strcpy (document
, "<library-list-svr4 version=\"1.0\"");
6331 p
= document
+ strlen (document
);
6334 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
6335 &l_name
, ptr_size
) == 0
6336 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
6337 &l_addr
, ptr_size
) == 0
6338 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
6339 &l_ld
, ptr_size
) == 0
6340 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
6341 &l_prev
, ptr_size
) == 0
6342 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
6343 &l_next
, ptr_size
) == 0)
6345 unsigned char libname
[PATH_MAX
];
6347 if (lm_prev
!= l_prev
)
6349 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
6350 (long) lm_prev
, (long) l_prev
);
6354 /* Ignore the first entry even if it has valid name as the first entry
6355 corresponds to the main executable. The first entry should not be
6356 skipped if the dynamic loader was loaded late by a static executable
6357 (see solib-svr4.c parameter ignore_first). But in such case the main
6358 executable does not have PT_DYNAMIC present and this function already
6359 exited above due to failed get_r_debug. */
6362 sprintf (p
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
6367 /* Not checking for error because reading may stop before
6368 we've got PATH_MAX worth of characters. */
6370 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
6371 libname
[sizeof (libname
) - 1] = '\0';
6372 if (libname
[0] != '\0')
6374 /* 6x the size for xml_escape_text below. */
6375 size_t len
= 6 * strlen ((char *) libname
);
6380 /* Terminate `<library-list-svr4'. */
6385 while (allocated
< p
- document
+ len
+ 200)
6387 /* Expand to guarantee sufficient storage. */
6388 uintptr_t document_len
= p
- document
;
6390 document
= xrealloc (document
, 2 * allocated
);
6392 p
= document
+ document_len
;
6395 name
= xml_escape_text ((char *) libname
);
6396 p
+= sprintf (p
, "<library name=\"%s\" lm=\"0x%lx\" "
6397 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
6398 name
, (unsigned long) lm_addr
,
6399 (unsigned long) l_addr
, (unsigned long) l_ld
);
6410 /* Empty list; terminate `<library-list-svr4'. */
6414 strcpy (p
, "</library-list-svr4>");
6416 document_len
= strlen (document
);
6417 if (offset
< document_len
)
6418 document_len
-= offset
;
6421 if (len
> document_len
)
6424 memcpy (readbuf
, document
+ offset
, len
);
6430 #ifdef HAVE_LINUX_BTRACE
6432 /* See to_enable_btrace target method. */
6434 static struct btrace_target_info
*
6435 linux_low_enable_btrace (ptid_t ptid
, const struct btrace_config
*conf
)
6437 struct btrace_target_info
*tinfo
;
6439 tinfo
= linux_enable_btrace (ptid
, conf
);
6441 if (tinfo
!= NULL
&& tinfo
->ptr_bits
== 0)
6443 struct thread_info
*thread
= find_thread_ptid (ptid
);
6444 struct regcache
*regcache
= get_thread_regcache (thread
, 0);
6446 tinfo
->ptr_bits
= register_size (regcache
->tdesc
, 0) * 8;
6452 /* See to_disable_btrace target method. */
6455 linux_low_disable_btrace (struct btrace_target_info
*tinfo
)
6457 enum btrace_error err
;
6459 err
= linux_disable_btrace (tinfo
);
6460 return (err
== BTRACE_ERR_NONE
? 0 : -1);
6463 /* See to_read_btrace target method. */
6466 linux_low_read_btrace (struct btrace_target_info
*tinfo
, struct buffer
*buffer
,
6469 struct btrace_data btrace
;
6470 struct btrace_block
*block
;
6471 enum btrace_error err
;
6474 btrace_data_init (&btrace
);
6476 err
= linux_read_btrace (&btrace
, tinfo
, type
);
6477 if (err
!= BTRACE_ERR_NONE
)
6479 if (err
== BTRACE_ERR_OVERFLOW
)
6480 buffer_grow_str0 (buffer
, "E.Overflow.");
6482 buffer_grow_str0 (buffer
, "E.Generic Error.");
6484 btrace_data_fini (&btrace
);
6488 switch (btrace
.format
)
6490 case BTRACE_FORMAT_NONE
:
6491 buffer_grow_str0 (buffer
, "E.No Trace.");
6494 case BTRACE_FORMAT_BTS
:
6495 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
6496 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
6499 VEC_iterate (btrace_block_s
, btrace
.variant
.bts
.blocks
, i
, block
);
6501 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
6502 paddress (block
->begin
), paddress (block
->end
));
6504 buffer_grow_str0 (buffer
, "</btrace>\n");
6508 buffer_grow_str0 (buffer
, "E.Unknown Trace Format.");
6510 btrace_data_fini (&btrace
);
6514 btrace_data_fini (&btrace
);
6518 /* See to_btrace_conf target method. */
6521 linux_low_btrace_conf (const struct btrace_target_info
*tinfo
,
6522 struct buffer
*buffer
)
6524 const struct btrace_config
*conf
;
6526 buffer_grow_str (buffer
, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
6527 buffer_grow_str (buffer
, "<btrace-conf version=\"1.0\">\n");
6529 conf
= linux_btrace_conf (tinfo
);
6532 switch (conf
->format
)
6534 case BTRACE_FORMAT_NONE
:
6537 case BTRACE_FORMAT_BTS
:
6538 buffer_xml_printf (buffer
, "<bts");
6539 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->bts
.size
);
6540 buffer_xml_printf (buffer
, " />\n");
6545 buffer_grow_str0 (buffer
, "</btrace-conf>\n");
6548 #endif /* HAVE_LINUX_BTRACE */
6550 /* See nat/linux-nat.h. */
6553 current_lwp_ptid (void)
6555 return ptid_of (current_thread
);
6558 static struct target_ops linux_target_ops
= {
6559 linux_create_inferior
,
6568 linux_fetch_registers
,
6569 linux_store_registers
,
6570 linux_prepare_to_access_memory
,
6571 linux_done_accessing_memory
,
6574 linux_look_up_symbols
,
6575 linux_request_interrupt
,
6577 linux_supports_z_point_type
,
6580 linux_stopped_by_sw_breakpoint
,
6581 linux_supports_stopped_by_sw_breakpoint
,
6582 linux_stopped_by_hw_breakpoint
,
6583 linux_supports_stopped_by_hw_breakpoint
,
6584 linux_supports_conditional_breakpoints
,
6585 linux_stopped_by_watchpoint
,
6586 linux_stopped_data_address
,
6587 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
6588 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
6589 && defined(PT_TEXT_END_ADDR)
6594 #ifdef USE_THREAD_DB
6595 thread_db_get_tls_address
,
6600 hostio_last_error_from_errno
,
6603 linux_supports_non_stop
,
6605 linux_start_non_stop
,
6606 linux_supports_multi_process
,
6607 linux_supports_fork_events
,
6608 linux_supports_vfork_events
,
6609 linux_handle_new_gdb_connection
,
6610 #ifdef USE_THREAD_DB
6611 thread_db_handle_monitor_command
,
6615 linux_common_core_of_thread
,
6617 linux_process_qsupported
,
6618 linux_supports_tracepoints
,
6621 linux_thread_stopped
,
6625 linux_stabilize_threads
,
6626 linux_install_fast_tracepoint_jump_pad
,
6628 linux_supports_disable_randomization
,
6629 linux_get_min_fast_tracepoint_insn_len
,
6630 linux_qxfer_libraries_svr4
,
6631 linux_supports_agent
,
6632 #ifdef HAVE_LINUX_BTRACE
6633 linux_supports_btrace
,
6634 linux_low_enable_btrace
,
6635 linux_low_disable_btrace
,
6636 linux_low_read_btrace
,
6637 linux_low_btrace_conf
,
6645 linux_supports_range_stepping
,
6646 linux_proc_pid_to_exec_file
,
6650 linux_init_signals ()
6652 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
6653 to find what the cancel signal actually is. */
6654 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
6655 signal (__SIGRTMIN
+1, SIG_IGN
);
6659 #ifdef HAVE_LINUX_REGSETS
6661 initialize_regsets_info (struct regsets_info
*info
)
6663 for (info
->num_regsets
= 0;
6664 info
->regsets
[info
->num_regsets
].size
>= 0;
6665 info
->num_regsets
++)
6671 initialize_low (void)
6673 struct sigaction sigchld_action
;
6674 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
6675 set_target_ops (&linux_target_ops
);
6676 set_breakpoint_data (the_low_target
.breakpoint
,
6677 the_low_target
.breakpoint_len
);
6678 linux_init_signals ();
6679 linux_ptrace_init_warnings ();
6681 sigchld_action
.sa_handler
= sigchld_handler
;
6682 sigemptyset (&sigchld_action
.sa_mask
);
6683 sigchld_action
.sa_flags
= SA_RESTART
;
6684 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
6686 initialize_low_arch ();
6688 linux_check_ptrace_features ();