1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2016 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"
26 #include "nat/linux-nat.h"
27 #include "nat/linux-waitpid.h"
29 #include "nat/gdb_ptrace.h"
30 #include "nat/linux-ptrace.h"
31 #include "nat/linux-procfs.h"
32 #include "nat/linux-personality.h"
34 #include <sys/ioctl.h>
37 #include <sys/syscall.h>
41 #include <sys/types.h>
46 #include "filestuff.h"
47 #include "tracepoint.h"
51 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
52 then ELFMAG0 will have been defined. If it didn't get included by
53 gdb_proc_service.h then including it will likely introduce a duplicate
54 definition of elf_fpregset_t. */
57 #include "nat/linux-namespaces.h"
60 #define SPUFS_MAGIC 0x23c9b64e
63 #ifdef HAVE_PERSONALITY
64 # include <sys/personality.h>
65 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
66 # define ADDR_NO_RANDOMIZE 0x0040000
74 /* Some targets did not define these ptrace constants from the start,
75 so gdbserver defines them locally here. In the future, these may
76 be removed after they are added to asm/ptrace.h. */
77 #if !(defined(PT_TEXT_ADDR) \
78 || defined(PT_DATA_ADDR) \
79 || defined(PT_TEXT_END_ADDR))
80 #if defined(__mcoldfire__)
81 /* These are still undefined in 3.10 kernels. */
82 #define PT_TEXT_ADDR 49*4
83 #define PT_DATA_ADDR 50*4
84 #define PT_TEXT_END_ADDR 51*4
85 /* BFIN already defines these since at least 2.6.32 kernels. */
87 #define PT_TEXT_ADDR 220
88 #define PT_TEXT_END_ADDR 224
89 #define PT_DATA_ADDR 228
90 /* These are still undefined in 3.10 kernels. */
91 #elif defined(__TMS320C6X__)
92 #define PT_TEXT_ADDR (0x10000*4)
93 #define PT_DATA_ADDR (0x10004*4)
94 #define PT_TEXT_END_ADDR (0x10008*4)
98 #ifdef HAVE_LINUX_BTRACE
99 # include "nat/linux-btrace.h"
100 # include "btrace-common.h"
103 #ifndef HAVE_ELF32_AUXV_T
104 /* Copied from glibc's elf.h. */
107 uint32_t a_type
; /* Entry type */
110 uint32_t a_val
; /* Integer value */
111 /* We use to have pointer elements added here. We cannot do that,
112 though, since it does not work when using 32-bit definitions
113 on 64-bit platforms and vice versa. */
118 #ifndef HAVE_ELF64_AUXV_T
119 /* Copied from glibc's elf.h. */
122 uint64_t a_type
; /* Entry type */
125 uint64_t a_val
; /* Integer value */
126 /* We use to have pointer elements added here. We cannot do that,
127 though, since it does not work when using 32-bit definitions
128 on 64-bit platforms and vice versa. */
133 /* Does the current host support PTRACE_GETREGSET? */
134 int have_ptrace_getregset
= -1;
138 /* See nat/linux-nat.h. */
141 ptid_of_lwp (struct lwp_info
*lwp
)
143 return ptid_of (get_lwp_thread (lwp
));
146 /* See nat/linux-nat.h. */
149 lwp_set_arch_private_info (struct lwp_info
*lwp
,
150 struct arch_lwp_info
*info
)
152 lwp
->arch_private
= info
;
155 /* See nat/linux-nat.h. */
157 struct arch_lwp_info
*
158 lwp_arch_private_info (struct lwp_info
*lwp
)
160 return lwp
->arch_private
;
163 /* See nat/linux-nat.h. */
166 lwp_is_stopped (struct lwp_info
*lwp
)
171 /* See nat/linux-nat.h. */
173 enum target_stop_reason
174 lwp_stop_reason (struct lwp_info
*lwp
)
176 return lwp
->stop_reason
;
179 /* A list of all unknown processes which receive stop signals. Some
180 other process will presumably claim each of these as forked
181 children momentarily. */
183 struct simple_pid_list
185 /* The process ID. */
188 /* The status as reported by waitpid. */
192 struct simple_pid_list
*next
;
194 struct simple_pid_list
*stopped_pids
;
196 /* Trivial list manipulation functions to keep track of a list of new
197 stopped processes. */
200 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
202 struct simple_pid_list
*new_pid
= XNEW (struct simple_pid_list
);
205 new_pid
->status
= status
;
206 new_pid
->next
= *listp
;
211 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
213 struct simple_pid_list
**p
;
215 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
216 if ((*p
)->pid
== pid
)
218 struct simple_pid_list
*next
= (*p
)->next
;
220 *statusp
= (*p
)->status
;
228 enum stopping_threads_kind
230 /* Not stopping threads presently. */
231 NOT_STOPPING_THREADS
,
233 /* Stopping threads. */
236 /* Stopping and suspending threads. */
237 STOPPING_AND_SUSPENDING_THREADS
240 /* This is set while stop_all_lwps is in effect. */
241 enum stopping_threads_kind stopping_threads
= NOT_STOPPING_THREADS
;
243 /* FIXME make into a target method? */
244 int using_threads
= 1;
246 /* True if we're presently stabilizing threads (moving them out of
248 static int stabilizing_threads
;
250 static void linux_resume_one_lwp (struct lwp_info
*lwp
,
251 int step
, int signal
, siginfo_t
*info
);
252 static void linux_resume (struct thread_resume
*resume_info
, size_t n
);
253 static void stop_all_lwps (int suspend
, struct lwp_info
*except
);
254 static void unstop_all_lwps (int unsuspend
, struct lwp_info
*except
);
255 static void unsuspend_all_lwps (struct lwp_info
*except
);
256 static int linux_wait_for_event_filtered (ptid_t wait_ptid
, ptid_t filter_ptid
,
257 int *wstat
, int options
);
258 static int linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
);
259 static struct lwp_info
*add_lwp (ptid_t ptid
);
260 static void linux_mourn (struct process_info
*process
);
261 static int linux_stopped_by_watchpoint (void);
262 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
263 static int lwp_is_marked_dead (struct lwp_info
*lwp
);
264 static void proceed_all_lwps (void);
265 static int finish_step_over (struct lwp_info
*lwp
);
266 static int kill_lwp (unsigned long lwpid
, int signo
);
267 static void enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
);
268 static void complete_ongoing_step_over (void);
269 static int linux_low_ptrace_options (int attached
);
270 static int check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
);
271 static int proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
);
273 /* When the event-loop is doing a step-over, this points at the thread
275 ptid_t step_over_bkpt
;
277 /* True if the low target can hardware single-step. */
280 can_hardware_single_step (void)
282 if (the_low_target
.supports_hardware_single_step
!= NULL
)
283 return the_low_target
.supports_hardware_single_step ();
288 /* True if the low target can software single-step. Such targets
289 implement the GET_NEXT_PCS callback. */
292 can_software_single_step (void)
294 return (the_low_target
.get_next_pcs
!= NULL
);
297 /* True if the low target supports memory breakpoints. If so, we'll
298 have a GET_PC implementation. */
301 supports_breakpoints (void)
303 return (the_low_target
.get_pc
!= NULL
);
306 /* Returns true if this target can support fast tracepoints. This
307 does not mean that the in-process agent has been loaded in the
311 supports_fast_tracepoints (void)
313 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
316 /* True if LWP is stopped in its stepping range. */
319 lwp_in_step_range (struct lwp_info
*lwp
)
321 CORE_ADDR pc
= lwp
->stop_pc
;
323 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
326 struct pending_signals
330 struct pending_signals
*prev
;
333 /* The read/write ends of the pipe registered as waitable file in the
335 static int linux_event_pipe
[2] = { -1, -1 };
337 /* True if we're currently in async mode. */
338 #define target_is_async_p() (linux_event_pipe[0] != -1)
340 static void send_sigstop (struct lwp_info
*lwp
);
341 static void wait_for_sigstop (void);
343 /* Return non-zero if HEADER is a 64-bit ELF file. */
346 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
348 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
349 && header
->e_ident
[EI_MAG1
] == ELFMAG1
350 && header
->e_ident
[EI_MAG2
] == ELFMAG2
351 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
353 *machine
= header
->e_machine
;
354 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
361 /* Return non-zero if FILE is a 64-bit ELF file,
362 zero if the file is not a 64-bit ELF file,
363 and -1 if the file is not accessible or doesn't exist. */
366 elf_64_file_p (const char *file
, unsigned int *machine
)
371 fd
= open (file
, O_RDONLY
);
375 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
382 return elf_64_header_p (&header
, machine
);
385 /* Accepts an integer PID; Returns true if the executable PID is
386 running is a 64-bit ELF file.. */
389 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
393 sprintf (file
, "/proc/%d/exe", pid
);
394 return elf_64_file_p (file
, machine
);
398 delete_lwp (struct lwp_info
*lwp
)
400 struct thread_info
*thr
= get_lwp_thread (lwp
);
403 debug_printf ("deleting %ld\n", lwpid_of (thr
));
406 free (lwp
->arch_private
);
410 /* Add a process to the common process list, and set its private
413 static struct process_info
*
414 linux_add_process (int pid
, int attached
)
416 struct process_info
*proc
;
418 proc
= add_process (pid
, attached
);
419 proc
->priv
= XCNEW (struct process_info_private
);
421 if (the_low_target
.new_process
!= NULL
)
422 proc
->priv
->arch_private
= the_low_target
.new_process ();
427 static CORE_ADDR
get_pc (struct lwp_info
*lwp
);
429 /* Call the target arch_setup function on the current thread. */
432 linux_arch_setup (void)
434 the_low_target
.arch_setup ();
437 /* Call the target arch_setup function on THREAD. */
440 linux_arch_setup_thread (struct thread_info
*thread
)
442 struct thread_info
*saved_thread
;
444 saved_thread
= current_thread
;
445 current_thread
= thread
;
449 current_thread
= saved_thread
;
452 /* Handle a GNU/Linux extended wait response. If we see a clone,
453 fork, or vfork event, we need to add the new LWP to our list
454 (and return 0 so as not to report the trap to higher layers).
455 If we see an exec event, we will modify ORIG_EVENT_LWP to point
456 to a new LWP representing the new program. */
459 handle_extended_wait (struct lwp_info
**orig_event_lwp
, int wstat
)
461 struct lwp_info
*event_lwp
= *orig_event_lwp
;
462 int event
= linux_ptrace_get_extended_event (wstat
);
463 struct thread_info
*event_thr
= get_lwp_thread (event_lwp
);
464 struct lwp_info
*new_lwp
;
466 gdb_assert (event_lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
468 /* All extended events we currently use are mid-syscall. Only
469 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
470 you have to be using PTRACE_SEIZE to get that. */
471 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
473 if ((event
== PTRACE_EVENT_FORK
) || (event
== PTRACE_EVENT_VFORK
)
474 || (event
== PTRACE_EVENT_CLONE
))
477 unsigned long new_pid
;
480 /* Get the pid of the new lwp. */
481 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_thr
), (PTRACE_TYPE_ARG3
) 0,
484 /* If we haven't already seen the new PID stop, wait for it now. */
485 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
487 /* The new child has a pending SIGSTOP. We can't affect it until it
488 hits the SIGSTOP, but we're already attached. */
490 ret
= my_waitpid (new_pid
, &status
, __WALL
);
493 perror_with_name ("waiting for new child");
494 else if (ret
!= new_pid
)
495 warning ("wait returned unexpected PID %d", ret
);
496 else if (!WIFSTOPPED (status
))
497 warning ("wait returned unexpected status 0x%x", status
);
500 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
502 struct process_info
*parent_proc
;
503 struct process_info
*child_proc
;
504 struct lwp_info
*child_lwp
;
505 struct thread_info
*child_thr
;
506 struct target_desc
*tdesc
;
508 ptid
= ptid_build (new_pid
, new_pid
, 0);
512 debug_printf ("HEW: Got fork event from LWP %ld, "
514 ptid_get_lwp (ptid_of (event_thr
)),
515 ptid_get_pid (ptid
));
518 /* Add the new process to the tables and clone the breakpoint
519 lists of the parent. We need to do this even if the new process
520 will be detached, since we will need the process object and the
521 breakpoints to remove any breakpoints from memory when we
522 detach, and the client side will access registers. */
523 child_proc
= linux_add_process (new_pid
, 0);
524 gdb_assert (child_proc
!= NULL
);
525 child_lwp
= add_lwp (ptid
);
526 gdb_assert (child_lwp
!= NULL
);
527 child_lwp
->stopped
= 1;
528 child_lwp
->must_set_ptrace_flags
= 1;
529 child_lwp
->status_pending_p
= 0;
530 child_thr
= get_lwp_thread (child_lwp
);
531 child_thr
->last_resume_kind
= resume_stop
;
532 child_thr
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
534 /* If we're suspending all threads, leave this one suspended
535 too. If the fork/clone parent is stepping over a breakpoint,
536 all other threads have been suspended already. Leave the
537 child suspended too. */
538 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
539 || event_lwp
->bp_reinsert
!= 0)
542 debug_printf ("HEW: leaving child suspended\n");
543 child_lwp
->suspended
= 1;
546 parent_proc
= get_thread_process (event_thr
);
547 child_proc
->attached
= parent_proc
->attached
;
549 if (event_lwp
->bp_reinsert
!= 0
550 && can_software_single_step ()
551 && event
== PTRACE_EVENT_VFORK
)
553 /* If we leave reinsert breakpoints there, child will
554 hit it, so uninsert reinsert breakpoints from parent
555 (and child). Once vfork child is done, reinsert
556 them back to parent. */
557 uninsert_reinsert_breakpoints (event_thr
);
560 clone_all_breakpoints (child_thr
, event_thr
);
562 tdesc
= XNEW (struct target_desc
);
563 copy_target_description (tdesc
, parent_proc
->tdesc
);
564 child_proc
->tdesc
= tdesc
;
566 /* Clone arch-specific process data. */
567 if (the_low_target
.new_fork
!= NULL
)
568 the_low_target
.new_fork (parent_proc
, child_proc
);
570 /* Save fork info in the parent thread. */
571 if (event
== PTRACE_EVENT_FORK
)
572 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_FORKED
;
573 else if (event
== PTRACE_EVENT_VFORK
)
574 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORKED
;
576 event_lwp
->waitstatus
.value
.related_pid
= ptid
;
578 /* The status_pending field contains bits denoting the
579 extended event, so when the pending event is handled,
580 the handler will look at lwp->waitstatus. */
581 event_lwp
->status_pending_p
= 1;
582 event_lwp
->status_pending
= wstat
;
584 /* If the parent thread is doing step-over with reinsert
585 breakpoints, the list of reinsert breakpoints are cloned
586 from the parent's. Remove them from the child process.
587 In case of vfork, we'll reinsert them back once vforked
589 if (event_lwp
->bp_reinsert
!= 0
590 && can_software_single_step ())
592 /* The child process is forked and stopped, so it is safe
593 to access its memory without stopping all other threads
594 from other processes. */
595 delete_reinsert_breakpoints (child_thr
);
597 gdb_assert (has_reinsert_breakpoints (event_thr
));
598 gdb_assert (!has_reinsert_breakpoints (child_thr
));
601 /* Report the event. */
606 debug_printf ("HEW: Got clone event "
607 "from LWP %ld, new child is LWP %ld\n",
608 lwpid_of (event_thr
), new_pid
);
610 ptid
= ptid_build (pid_of (event_thr
), new_pid
, 0);
611 new_lwp
= add_lwp (ptid
);
613 /* Either we're going to immediately resume the new thread
614 or leave it stopped. linux_resume_one_lwp is a nop if it
615 thinks the thread is currently running, so set this first
616 before calling linux_resume_one_lwp. */
617 new_lwp
->stopped
= 1;
619 /* If we're suspending all threads, leave this one suspended
620 too. If the fork/clone parent is stepping over a breakpoint,
621 all other threads have been suspended already. Leave the
622 child suspended too. */
623 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
624 || event_lwp
->bp_reinsert
!= 0)
625 new_lwp
->suspended
= 1;
627 /* Normally we will get the pending SIGSTOP. But in some cases
628 we might get another signal delivered to the group first.
629 If we do get another signal, be sure not to lose it. */
630 if (WSTOPSIG (status
) != SIGSTOP
)
632 new_lwp
->stop_expected
= 1;
633 new_lwp
->status_pending_p
= 1;
634 new_lwp
->status_pending
= status
;
636 else if (report_thread_events
)
638 new_lwp
->waitstatus
.kind
= TARGET_WAITKIND_THREAD_CREATED
;
639 new_lwp
->status_pending_p
= 1;
640 new_lwp
->status_pending
= status
;
643 /* Don't report the event. */
646 else if (event
== PTRACE_EVENT_VFORK_DONE
)
648 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
650 if (event_lwp
->bp_reinsert
!= 0 && can_software_single_step ())
652 reinsert_reinsert_breakpoints (event_thr
);
654 gdb_assert (has_reinsert_breakpoints (event_thr
));
657 /* Report the event. */
660 else if (event
== PTRACE_EVENT_EXEC
&& report_exec_events
)
662 struct process_info
*proc
;
663 VEC (int) *syscalls_to_catch
;
669 debug_printf ("HEW: Got exec event from LWP %ld\n",
670 lwpid_of (event_thr
));
673 /* Get the event ptid. */
674 event_ptid
= ptid_of (event_thr
);
675 event_pid
= ptid_get_pid (event_ptid
);
677 /* Save the syscall list from the execing process. */
678 proc
= get_thread_process (event_thr
);
679 syscalls_to_catch
= proc
->syscalls_to_catch
;
680 proc
->syscalls_to_catch
= NULL
;
682 /* Delete the execing process and all its threads. */
684 current_thread
= NULL
;
686 /* Create a new process/lwp/thread. */
687 proc
= linux_add_process (event_pid
, 0);
688 event_lwp
= add_lwp (event_ptid
);
689 event_thr
= get_lwp_thread (event_lwp
);
690 gdb_assert (current_thread
== event_thr
);
691 linux_arch_setup_thread (event_thr
);
693 /* Set the event status. */
694 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXECD
;
695 event_lwp
->waitstatus
.value
.execd_pathname
696 = xstrdup (linux_proc_pid_to_exec_file (lwpid_of (event_thr
)));
698 /* Mark the exec status as pending. */
699 event_lwp
->stopped
= 1;
700 event_lwp
->status_pending_p
= 1;
701 event_lwp
->status_pending
= wstat
;
702 event_thr
->last_resume_kind
= resume_continue
;
703 event_thr
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
705 /* Update syscall state in the new lwp, effectively mid-syscall too. */
706 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
708 /* Restore the list to catch. Don't rely on the client, which is free
709 to avoid sending a new list when the architecture doesn't change.
710 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
711 proc
->syscalls_to_catch
= syscalls_to_catch
;
713 /* Report the event. */
714 *orig_event_lwp
= event_lwp
;
718 internal_error (__FILE__
, __LINE__
, _("unknown ptrace event %d"), event
);
721 /* Return the PC as read from the regcache of LWP, without any
725 get_pc (struct lwp_info
*lwp
)
727 struct thread_info
*saved_thread
;
728 struct regcache
*regcache
;
731 if (the_low_target
.get_pc
== NULL
)
734 saved_thread
= current_thread
;
735 current_thread
= get_lwp_thread (lwp
);
737 regcache
= get_thread_regcache (current_thread
, 1);
738 pc
= (*the_low_target
.get_pc
) (regcache
);
741 debug_printf ("pc is 0x%lx\n", (long) pc
);
743 current_thread
= saved_thread
;
747 /* This function should only be called if LWP got a SYSCALL_SIGTRAP.
748 Fill *SYSNO with the syscall nr trapped. */
751 get_syscall_trapinfo (struct lwp_info
*lwp
, int *sysno
)
753 struct thread_info
*saved_thread
;
754 struct regcache
*regcache
;
756 if (the_low_target
.get_syscall_trapinfo
== NULL
)
758 /* If we cannot get the syscall trapinfo, report an unknown
759 system call number. */
760 *sysno
= UNKNOWN_SYSCALL
;
764 saved_thread
= current_thread
;
765 current_thread
= get_lwp_thread (lwp
);
767 regcache
= get_thread_regcache (current_thread
, 1);
768 (*the_low_target
.get_syscall_trapinfo
) (regcache
, sysno
);
771 debug_printf ("get_syscall_trapinfo sysno %d\n", *sysno
);
773 current_thread
= saved_thread
;
776 static int check_stopped_by_watchpoint (struct lwp_info
*child
);
778 /* Called when the LWP stopped for a signal/trap. If it stopped for a
779 trap check what caused it (breakpoint, watchpoint, trace, etc.),
780 and save the result in the LWP's stop_reason field. If it stopped
781 for a breakpoint, decrement the PC if necessary on the lwp's
782 architecture. Returns true if we now have the LWP's stop PC. */
785 save_stop_reason (struct lwp_info
*lwp
)
788 CORE_ADDR sw_breakpoint_pc
;
789 struct thread_info
*saved_thread
;
790 #if USE_SIGTRAP_SIGINFO
794 if (the_low_target
.get_pc
== NULL
)
798 sw_breakpoint_pc
= pc
- the_low_target
.decr_pc_after_break
;
800 /* breakpoint_at reads from the current thread. */
801 saved_thread
= current_thread
;
802 current_thread
= get_lwp_thread (lwp
);
804 #if USE_SIGTRAP_SIGINFO
805 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
806 (PTRACE_TYPE_ARG3
) 0, &siginfo
) == 0)
808 if (siginfo
.si_signo
== SIGTRAP
)
810 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
)
811 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
813 /* The si_code is ambiguous on this arch -- check debug
815 if (!check_stopped_by_watchpoint (lwp
))
816 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
818 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
))
820 /* If we determine the LWP stopped for a SW breakpoint,
821 trust it. Particularly don't check watchpoint
822 registers, because at least on s390, we'd find
823 stopped-by-watchpoint as long as there's a watchpoint
825 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
827 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
829 /* This can indicate either a hardware breakpoint or
830 hardware watchpoint. Check debug registers. */
831 if (!check_stopped_by_watchpoint (lwp
))
832 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
834 else if (siginfo
.si_code
== TRAP_TRACE
)
836 /* We may have single stepped an instruction that
837 triggered a watchpoint. In that case, on some
838 architectures (such as x86), instead of TRAP_HWBKPT,
839 si_code indicates TRAP_TRACE, and we need to check
840 the debug registers separately. */
841 if (!check_stopped_by_watchpoint (lwp
))
842 lwp
->stop_reason
= TARGET_STOPPED_BY_SINGLE_STEP
;
847 /* We may have just stepped a breakpoint instruction. E.g., in
848 non-stop mode, GDB first tells the thread A to step a range, and
849 then the user inserts a breakpoint inside the range. In that
850 case we need to report the breakpoint PC. */
851 if ((!lwp
->stepping
|| lwp
->stop_pc
== sw_breakpoint_pc
)
852 && (*the_low_target
.breakpoint_at
) (sw_breakpoint_pc
))
853 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
855 if (hardware_breakpoint_inserted_here (pc
))
856 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
858 if (lwp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
)
859 check_stopped_by_watchpoint (lwp
);
862 if (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
)
866 struct thread_info
*thr
= get_lwp_thread (lwp
);
868 debug_printf ("CSBB: %s stopped by software breakpoint\n",
869 target_pid_to_str (ptid_of (thr
)));
872 /* Back up the PC if necessary. */
873 if (pc
!= sw_breakpoint_pc
)
875 struct regcache
*regcache
876 = get_thread_regcache (current_thread
, 1);
877 (*the_low_target
.set_pc
) (regcache
, sw_breakpoint_pc
);
880 /* Update this so we record the correct stop PC below. */
881 pc
= sw_breakpoint_pc
;
883 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
887 struct thread_info
*thr
= get_lwp_thread (lwp
);
889 debug_printf ("CSBB: %s stopped by hardware breakpoint\n",
890 target_pid_to_str (ptid_of (thr
)));
893 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
897 struct thread_info
*thr
= get_lwp_thread (lwp
);
899 debug_printf ("CSBB: %s stopped by hardware watchpoint\n",
900 target_pid_to_str (ptid_of (thr
)));
903 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
)
907 struct thread_info
*thr
= get_lwp_thread (lwp
);
909 debug_printf ("CSBB: %s stopped by trace\n",
910 target_pid_to_str (ptid_of (thr
)));
915 current_thread
= saved_thread
;
919 static struct lwp_info
*
920 add_lwp (ptid_t ptid
)
922 struct lwp_info
*lwp
;
924 lwp
= XCNEW (struct lwp_info
);
926 lwp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
928 if (the_low_target
.new_thread
!= NULL
)
929 the_low_target
.new_thread (lwp
);
931 lwp
->thread
= add_thread (ptid
, lwp
);
936 /* Start an inferior process and returns its pid.
937 ALLARGS is a vector of program-name and args. */
940 linux_create_inferior (char *program
, char **allargs
)
942 struct lwp_info
*new_lwp
;
945 struct cleanup
*restore_personality
946 = maybe_disable_address_space_randomization (disable_randomization
);
948 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
954 perror_with_name ("fork");
959 ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
963 /* If gdbserver is connected to gdb via stdio, redirect the inferior's
964 stdout to stderr so that inferior i/o doesn't corrupt the connection.
965 Also, redirect stdin to /dev/null. */
966 if (remote_connection_is_stdio ())
969 open ("/dev/null", O_RDONLY
);
971 if (write (2, "stdin/stdout redirected\n",
972 sizeof ("stdin/stdout redirected\n") - 1) < 0)
974 /* Errors ignored. */;
978 execv (program
, allargs
);
980 execvp (program
, allargs
);
982 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
988 do_cleanups (restore_personality
);
990 linux_add_process (pid
, 0);
992 ptid
= ptid_build (pid
, pid
, 0);
993 new_lwp
= add_lwp (ptid
);
994 new_lwp
->must_set_ptrace_flags
= 1;
999 /* Implement the post_create_inferior target_ops method. */
1002 linux_post_create_inferior (void)
1004 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
1006 linux_arch_setup ();
1008 if (lwp
->must_set_ptrace_flags
)
1010 struct process_info
*proc
= current_process ();
1011 int options
= linux_low_ptrace_options (proc
->attached
);
1013 linux_enable_event_reporting (lwpid_of (current_thread
), options
);
1014 lwp
->must_set_ptrace_flags
= 0;
1018 /* Attach to an inferior process. Returns 0 on success, ERRNO on
1022 linux_attach_lwp (ptid_t ptid
)
1024 struct lwp_info
*new_lwp
;
1025 int lwpid
= ptid_get_lwp (ptid
);
1027 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0)
1031 new_lwp
= add_lwp (ptid
);
1033 /* We need to wait for SIGSTOP before being able to make the next
1034 ptrace call on this LWP. */
1035 new_lwp
->must_set_ptrace_flags
= 1;
1037 if (linux_proc_pid_is_stopped (lwpid
))
1040 debug_printf ("Attached to a stopped process\n");
1042 /* The process is definitely stopped. It is in a job control
1043 stop, unless the kernel predates the TASK_STOPPED /
1044 TASK_TRACED distinction, in which case it might be in a
1045 ptrace stop. Make sure it is in a ptrace stop; from there we
1046 can kill it, signal it, et cetera.
1048 First make sure there is a pending SIGSTOP. Since we are
1049 already attached, the process can not transition from stopped
1050 to running without a PTRACE_CONT; so we know this signal will
1051 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1052 probably already in the queue (unless this kernel is old
1053 enough to use TASK_STOPPED for ptrace stops); but since
1054 SIGSTOP is not an RT signal, it can only be queued once. */
1055 kill_lwp (lwpid
, SIGSTOP
);
1057 /* Finally, resume the stopped process. This will deliver the
1058 SIGSTOP (or a higher priority signal, just like normal
1059 PTRACE_ATTACH), which we'll catch later on. */
1060 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1063 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1064 brings it to a halt.
1066 There are several cases to consider here:
1068 1) gdbserver has already attached to the process and is being notified
1069 of a new thread that is being created.
1070 In this case we should ignore that SIGSTOP and resume the
1071 process. This is handled below by setting stop_expected = 1,
1072 and the fact that add_thread sets last_resume_kind ==
1075 2) This is the first thread (the process thread), and we're attaching
1076 to it via attach_inferior.
1077 In this case we want the process thread to stop.
1078 This is handled by having linux_attach set last_resume_kind ==
1079 resume_stop after we return.
1081 If the pid we are attaching to is also the tgid, we attach to and
1082 stop all the existing threads. Otherwise, we attach to pid and
1083 ignore any other threads in the same group as this pid.
1085 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1087 In this case we want the thread to stop.
1088 FIXME: This case is currently not properly handled.
1089 We should wait for the SIGSTOP but don't. Things work apparently
1090 because enough time passes between when we ptrace (ATTACH) and when
1091 gdb makes the next ptrace call on the thread.
1093 On the other hand, if we are currently trying to stop all threads, we
1094 should treat the new thread as if we had sent it a SIGSTOP. This works
1095 because we are guaranteed that the add_lwp call above added us to the
1096 end of the list, and so the new thread has not yet reached
1097 wait_for_sigstop (but will). */
1098 new_lwp
->stop_expected
= 1;
1103 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1104 already attached. Returns true if a new LWP is found, false
1108 attach_proc_task_lwp_callback (ptid_t ptid
)
1110 /* Is this a new thread? */
1111 if (find_thread_ptid (ptid
) == NULL
)
1113 int lwpid
= ptid_get_lwp (ptid
);
1117 debug_printf ("Found new lwp %d\n", lwpid
);
1119 err
= linux_attach_lwp (ptid
);
1121 /* Be quiet if we simply raced with the thread exiting. EPERM
1122 is returned if the thread's task still exists, and is marked
1123 as exited or zombie, as well as other conditions, so in that
1124 case, confirm the status in /proc/PID/status. */
1126 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1130 debug_printf ("Cannot attach to lwp %d: "
1131 "thread is gone (%d: %s)\n",
1132 lwpid
, err
, strerror (err
));
1137 warning (_("Cannot attach to lwp %d: %s"),
1139 linux_ptrace_attach_fail_reason_string (ptid
, err
));
1147 static void async_file_mark (void);
1149 /* Attach to PID. If PID is the tgid, attach to it and all
1153 linux_attach (unsigned long pid
)
1155 struct process_info
*proc
;
1156 struct thread_info
*initial_thread
;
1157 ptid_t ptid
= ptid_build (pid
, pid
, 0);
1160 /* Attach to PID. We will check for other threads
1162 err
= linux_attach_lwp (ptid
);
1164 error ("Cannot attach to process %ld: %s",
1165 pid
, linux_ptrace_attach_fail_reason_string (ptid
, err
));
1167 proc
= linux_add_process (pid
, 1);
1169 /* Don't ignore the initial SIGSTOP if we just attached to this
1170 process. It will be collected by wait shortly. */
1171 initial_thread
= find_thread_ptid (ptid_build (pid
, pid
, 0));
1172 initial_thread
->last_resume_kind
= resume_stop
;
1174 /* We must attach to every LWP. If /proc is mounted, use that to
1175 find them now. On the one hand, the inferior may be using raw
1176 clone instead of using pthreads. On the other hand, even if it
1177 is using pthreads, GDB may not be connected yet (thread_db needs
1178 to do symbol lookups, through qSymbol). Also, thread_db walks
1179 structures in the inferior's address space to find the list of
1180 threads/LWPs, and those structures may well be corrupted. Note
1181 that once thread_db is loaded, we'll still use it to list threads
1182 and associate pthread info with each LWP. */
1183 linux_proc_attach_tgid_threads (pid
, attach_proc_task_lwp_callback
);
1185 /* GDB will shortly read the xml target description for this
1186 process, to figure out the process' architecture. But the target
1187 description is only filled in when the first process/thread in
1188 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1189 that now, otherwise, if GDB is fast enough, it could read the
1190 target description _before_ that initial stop. */
1193 struct lwp_info
*lwp
;
1195 ptid_t pid_ptid
= pid_to_ptid (pid
);
1197 lwpid
= linux_wait_for_event_filtered (pid_ptid
, pid_ptid
,
1199 gdb_assert (lwpid
> 0);
1201 lwp
= find_lwp_pid (pid_to_ptid (lwpid
));
1203 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGSTOP
)
1205 lwp
->status_pending_p
= 1;
1206 lwp
->status_pending
= wstat
;
1209 initial_thread
->last_resume_kind
= resume_continue
;
1213 gdb_assert (proc
->tdesc
!= NULL
);
1226 second_thread_of_pid_p (struct inferior_list_entry
*entry
, void *args
)
1228 struct counter
*counter
= (struct counter
*) args
;
1230 if (ptid_get_pid (entry
->id
) == counter
->pid
)
1232 if (++counter
->count
> 1)
1240 last_thread_of_process_p (int pid
)
1242 struct counter counter
= { pid
, 0 };
1244 return (find_inferior (&all_threads
,
1245 second_thread_of_pid_p
, &counter
) == NULL
);
1251 linux_kill_one_lwp (struct lwp_info
*lwp
)
1253 struct thread_info
*thr
= get_lwp_thread (lwp
);
1254 int pid
= lwpid_of (thr
);
1256 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1257 there is no signal context, and ptrace(PTRACE_KILL) (or
1258 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1259 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1260 alternative is to kill with SIGKILL. We only need one SIGKILL
1261 per process, not one for each thread. But since we still support
1262 support debugging programs using raw clone without CLONE_THREAD,
1263 we send one for each thread. For years, we used PTRACE_KILL
1264 only, so we're being a bit paranoid about some old kernels where
1265 PTRACE_KILL might work better (dubious if there are any such, but
1266 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1267 second, and so we're fine everywhere. */
1270 kill_lwp (pid
, SIGKILL
);
1273 int save_errno
= errno
;
1275 debug_printf ("LKL: kill_lwp (SIGKILL) %s, 0, 0 (%s)\n",
1276 target_pid_to_str (ptid_of (thr
)),
1277 save_errno
? strerror (save_errno
) : "OK");
1281 ptrace (PTRACE_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1284 int save_errno
= errno
;
1286 debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
1287 target_pid_to_str (ptid_of (thr
)),
1288 save_errno
? strerror (save_errno
) : "OK");
1292 /* Kill LWP and wait for it to die. */
1295 kill_wait_lwp (struct lwp_info
*lwp
)
1297 struct thread_info
*thr
= get_lwp_thread (lwp
);
1298 int pid
= ptid_get_pid (ptid_of (thr
));
1299 int lwpid
= ptid_get_lwp (ptid_of (thr
));
1304 debug_printf ("kwl: killing lwp %d, for pid: %d\n", lwpid
, pid
);
1308 linux_kill_one_lwp (lwp
);
1310 /* Make sure it died. Notes:
1312 - The loop is most likely unnecessary.
1314 - We don't use linux_wait_for_event as that could delete lwps
1315 while we're iterating over them. We're not interested in
1316 any pending status at this point, only in making sure all
1317 wait status on the kernel side are collected until the
1320 - We don't use __WALL here as the __WALL emulation relies on
1321 SIGCHLD, and killing a stopped process doesn't generate
1322 one, nor an exit status.
1324 res
= my_waitpid (lwpid
, &wstat
, 0);
1325 if (res
== -1 && errno
== ECHILD
)
1326 res
= my_waitpid (lwpid
, &wstat
, __WCLONE
);
1327 } while (res
> 0 && WIFSTOPPED (wstat
));
1329 /* Even if it was stopped, the child may have already disappeared.
1330 E.g., if it was killed by SIGKILL. */
1331 if (res
< 0 && errno
!= ECHILD
)
1332 perror_with_name ("kill_wait_lwp");
1335 /* Callback for `find_inferior'. Kills an lwp of a given process,
1336 except the leader. */
1339 kill_one_lwp_callback (struct inferior_list_entry
*entry
, void *args
)
1341 struct thread_info
*thread
= (struct thread_info
*) entry
;
1342 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1343 int pid
= * (int *) args
;
1345 if (ptid_get_pid (entry
->id
) != pid
)
1348 /* We avoid killing the first thread here, because of a Linux kernel (at
1349 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1350 the children get a chance to be reaped, it will remain a zombie
1353 if (lwpid_of (thread
) == pid
)
1356 debug_printf ("lkop: is last of process %s\n",
1357 target_pid_to_str (entry
->id
));
1361 kill_wait_lwp (lwp
);
1366 linux_kill (int pid
)
1368 struct process_info
*process
;
1369 struct lwp_info
*lwp
;
1371 process
= find_process_pid (pid
);
1372 if (process
== NULL
)
1375 /* If we're killing a running inferior, make sure it is stopped
1376 first, as PTRACE_KILL will not work otherwise. */
1377 stop_all_lwps (0, NULL
);
1379 find_inferior (&all_threads
, kill_one_lwp_callback
, &pid
);
1381 /* See the comment in linux_kill_one_lwp. We did not kill the first
1382 thread in the list, so do so now. */
1383 lwp
= find_lwp_pid (pid_to_ptid (pid
));
1388 debug_printf ("lk_1: cannot find lwp for pid: %d\n",
1392 kill_wait_lwp (lwp
);
1394 the_target
->mourn (process
);
1396 /* Since we presently can only stop all lwps of all processes, we
1397 need to unstop lwps of other processes. */
1398 unstop_all_lwps (0, NULL
);
1402 /* Get pending signal of THREAD, for detaching purposes. This is the
1403 signal the thread last stopped for, which we need to deliver to the
1404 thread when detaching, otherwise, it'd be suppressed/lost. */
1407 get_detach_signal (struct thread_info
*thread
)
1409 enum gdb_signal signo
= GDB_SIGNAL_0
;
1411 struct lwp_info
*lp
= get_thread_lwp (thread
);
1413 if (lp
->status_pending_p
)
1414 status
= lp
->status_pending
;
1417 /* If the thread had been suspended by gdbserver, and it stopped
1418 cleanly, then it'll have stopped with SIGSTOP. But we don't
1419 want to deliver that SIGSTOP. */
1420 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1421 || thread
->last_status
.value
.sig
== GDB_SIGNAL_0
)
1424 /* Otherwise, we may need to deliver the signal we
1426 status
= lp
->last_status
;
1429 if (!WIFSTOPPED (status
))
1432 debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1433 target_pid_to_str (ptid_of (thread
)));
1437 /* Extended wait statuses aren't real SIGTRAPs. */
1438 if (WSTOPSIG (status
) == SIGTRAP
&& linux_is_extended_waitstatus (status
))
1441 debug_printf ("GPS: lwp %s had stopped with extended "
1442 "status: no pending signal\n",
1443 target_pid_to_str (ptid_of (thread
)));
1447 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1449 if (program_signals_p
&& !program_signals
[signo
])
1452 debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1453 target_pid_to_str (ptid_of (thread
)),
1454 gdb_signal_to_string (signo
));
1457 else if (!program_signals_p
1458 /* If we have no way to know which signals GDB does not
1459 want to have passed to the program, assume
1460 SIGTRAP/SIGINT, which is GDB's default. */
1461 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1464 debug_printf ("GPS: lwp %s had signal %s, "
1465 "but we don't know if we should pass it. "
1466 "Default to not.\n",
1467 target_pid_to_str (ptid_of (thread
)),
1468 gdb_signal_to_string (signo
));
1474 debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1475 target_pid_to_str (ptid_of (thread
)),
1476 gdb_signal_to_string (signo
));
1478 return WSTOPSIG (status
);
1482 /* Detach from LWP. */
1485 linux_detach_one_lwp (struct lwp_info
*lwp
)
1487 struct thread_info
*thread
= get_lwp_thread (lwp
);
1491 /* If there is a pending SIGSTOP, get rid of it. */
1492 if (lwp
->stop_expected
)
1495 debug_printf ("Sending SIGCONT to %s\n",
1496 target_pid_to_str (ptid_of (thread
)));
1498 kill_lwp (lwpid_of (thread
), SIGCONT
);
1499 lwp
->stop_expected
= 0;
1502 /* Pass on any pending signal for this thread. */
1503 sig
= get_detach_signal (thread
);
1505 /* Preparing to resume may try to write registers, and fail if the
1506 lwp is zombie. If that happens, ignore the error. We'll handle
1507 it below, when detach fails with ESRCH. */
1510 /* Flush any pending changes to the process's registers. */
1511 regcache_invalidate_thread (thread
);
1513 /* Finally, let it resume. */
1514 if (the_low_target
.prepare_to_resume
!= NULL
)
1515 the_low_target
.prepare_to_resume (lwp
);
1517 CATCH (ex
, RETURN_MASK_ERROR
)
1519 if (!check_ptrace_stopped_lwp_gone (lwp
))
1520 throw_exception (ex
);
1524 lwpid
= lwpid_of (thread
);
1525 if (ptrace (PTRACE_DETACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0,
1526 (PTRACE_TYPE_ARG4
) (long) sig
) < 0)
1528 int save_errno
= errno
;
1530 /* We know the thread exists, so ESRCH must mean the lwp is
1531 zombie. This can happen if one of the already-detached
1532 threads exits the whole thread group. In that case we're
1533 still attached, and must reap the lwp. */
1534 if (save_errno
== ESRCH
)
1538 ret
= my_waitpid (lwpid
, &status
, __WALL
);
1541 warning (_("Couldn't reap LWP %d while detaching: %s"),
1542 lwpid
, strerror (errno
));
1544 else if (!WIFEXITED (status
) && !WIFSIGNALED (status
))
1546 warning (_("Reaping LWP %d while detaching "
1547 "returned unexpected status 0x%x"),
1553 error (_("Can't detach %s: %s"),
1554 target_pid_to_str (ptid_of (thread
)),
1555 strerror (save_errno
));
1558 else if (debug_threads
)
1560 debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)\n",
1561 target_pid_to_str (ptid_of (thread
)),
1568 /* Callback for find_inferior. Detaches from non-leader threads of a
1572 linux_detach_lwp_callback (struct inferior_list_entry
*entry
, void *args
)
1574 struct thread_info
*thread
= (struct thread_info
*) entry
;
1575 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1576 int pid
= *(int *) args
;
1577 int lwpid
= lwpid_of (thread
);
1579 /* Skip other processes. */
1580 if (ptid_get_pid (entry
->id
) != pid
)
1583 /* We don't actually detach from the thread group leader just yet.
1584 If the thread group exits, we must reap the zombie clone lwps
1585 before we're able to reap the leader. */
1586 if (ptid_get_pid (entry
->id
) == lwpid
)
1589 linux_detach_one_lwp (lwp
);
1594 linux_detach (int pid
)
1596 struct process_info
*process
;
1597 struct lwp_info
*main_lwp
;
1599 process
= find_process_pid (pid
);
1600 if (process
== NULL
)
1603 /* As there's a step over already in progress, let it finish first,
1604 otherwise nesting a stabilize_threads operation on top gets real
1606 complete_ongoing_step_over ();
1608 /* Stop all threads before detaching. First, ptrace requires that
1609 the thread is stopped to sucessfully detach. Second, thread_db
1610 may need to uninstall thread event breakpoints from memory, which
1611 only works with a stopped process anyway. */
1612 stop_all_lwps (0, NULL
);
1614 #ifdef USE_THREAD_DB
1615 thread_db_detach (process
);
1618 /* Stabilize threads (move out of jump pads). */
1619 stabilize_threads ();
1621 /* Detach from the clone lwps first. If the thread group exits just
1622 while we're detaching, we must reap the clone lwps before we're
1623 able to reap the leader. */
1624 find_inferior (&all_threads
, linux_detach_lwp_callback
, &pid
);
1626 main_lwp
= find_lwp_pid (pid_to_ptid (pid
));
1627 linux_detach_one_lwp (main_lwp
);
1629 the_target
->mourn (process
);
1631 /* Since we presently can only stop all lwps of all processes, we
1632 need to unstop lwps of other processes. */
1633 unstop_all_lwps (0, NULL
);
1637 /* Remove all LWPs that belong to process PROC from the lwp list. */
1640 delete_lwp_callback (struct inferior_list_entry
*entry
, void *proc
)
1642 struct thread_info
*thread
= (struct thread_info
*) entry
;
1643 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1644 struct process_info
*process
= (struct process_info
*) proc
;
1646 if (pid_of (thread
) == pid_of (process
))
1653 linux_mourn (struct process_info
*process
)
1655 struct process_info_private
*priv
;
1657 #ifdef USE_THREAD_DB
1658 thread_db_mourn (process
);
1661 find_inferior (&all_threads
, delete_lwp_callback
, process
);
1663 /* Freeing all private data. */
1664 priv
= process
->priv
;
1665 free (priv
->arch_private
);
1667 process
->priv
= NULL
;
1669 remove_process (process
);
1673 linux_join (int pid
)
1678 ret
= my_waitpid (pid
, &status
, 0);
1679 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1681 } while (ret
!= -1 || errno
!= ECHILD
);
1684 /* Return nonzero if the given thread is still alive. */
1686 linux_thread_alive (ptid_t ptid
)
1688 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1690 /* We assume we always know if a thread exits. If a whole process
1691 exited but we still haven't been able to report it to GDB, we'll
1692 hold on to the last lwp of the dead process. */
1694 return !lwp_is_marked_dead (lwp
);
1699 /* Return 1 if this lwp still has an interesting status pending. If
1700 not (e.g., it had stopped for a breakpoint that is gone), return
1704 thread_still_has_status_pending_p (struct thread_info
*thread
)
1706 struct lwp_info
*lp
= get_thread_lwp (thread
);
1708 if (!lp
->status_pending_p
)
1711 if (thread
->last_resume_kind
!= resume_stop
1712 && (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1713 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
))
1715 struct thread_info
*saved_thread
;
1719 gdb_assert (lp
->last_status
!= 0);
1723 saved_thread
= current_thread
;
1724 current_thread
= thread
;
1726 if (pc
!= lp
->stop_pc
)
1729 debug_printf ("PC of %ld changed\n",
1734 #if !USE_SIGTRAP_SIGINFO
1735 else if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1736 && !(*the_low_target
.breakpoint_at
) (pc
))
1739 debug_printf ("previous SW breakpoint of %ld gone\n",
1743 else if (lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
1744 && !hardware_breakpoint_inserted_here (pc
))
1747 debug_printf ("previous HW breakpoint of %ld gone\n",
1753 current_thread
= saved_thread
;
1758 debug_printf ("discarding pending breakpoint status\n");
1759 lp
->status_pending_p
= 0;
1767 /* Returns true if LWP is resumed from the client's perspective. */
1770 lwp_resumed (struct lwp_info
*lwp
)
1772 struct thread_info
*thread
= get_lwp_thread (lwp
);
1774 if (thread
->last_resume_kind
!= resume_stop
)
1777 /* Did gdb send us a `vCont;t', but we haven't reported the
1778 corresponding stop to gdb yet? If so, the thread is still
1779 resumed/running from gdb's perspective. */
1780 if (thread
->last_resume_kind
== resume_stop
1781 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
1787 /* Return 1 if this lwp has an interesting status pending. */
1789 status_pending_p_callback (struct inferior_list_entry
*entry
, void *arg
)
1791 struct thread_info
*thread
= (struct thread_info
*) entry
;
1792 struct lwp_info
*lp
= get_thread_lwp (thread
);
1793 ptid_t ptid
= * (ptid_t
*) arg
;
1795 /* Check if we're only interested in events from a specific process
1796 or a specific LWP. */
1797 if (!ptid_match (ptid_of (thread
), ptid
))
1800 if (!lwp_resumed (lp
))
1803 if (lp
->status_pending_p
1804 && !thread_still_has_status_pending_p (thread
))
1806 linux_resume_one_lwp (lp
, lp
->stepping
, GDB_SIGNAL_0
, NULL
);
1810 return lp
->status_pending_p
;
1814 same_lwp (struct inferior_list_entry
*entry
, void *data
)
1816 ptid_t ptid
= *(ptid_t
*) data
;
1819 if (ptid_get_lwp (ptid
) != 0)
1820 lwp
= ptid_get_lwp (ptid
);
1822 lwp
= ptid_get_pid (ptid
);
1824 if (ptid_get_lwp (entry
->id
) == lwp
)
1831 find_lwp_pid (ptid_t ptid
)
1833 struct inferior_list_entry
*thread
1834 = find_inferior (&all_threads
, same_lwp
, &ptid
);
1839 return get_thread_lwp ((struct thread_info
*) thread
);
1842 /* Return the number of known LWPs in the tgid given by PID. */
1847 struct inferior_list_entry
*inf
, *tmp
;
1850 ALL_INFERIORS (&all_threads
, inf
, tmp
)
1852 if (ptid_get_pid (inf
->id
) == pid
)
1859 /* The arguments passed to iterate_over_lwps. */
1861 struct iterate_over_lwps_args
1863 /* The FILTER argument passed to iterate_over_lwps. */
1866 /* The CALLBACK argument passed to iterate_over_lwps. */
1867 iterate_over_lwps_ftype
*callback
;
1869 /* The DATA argument passed to iterate_over_lwps. */
1873 /* Callback for find_inferior used by iterate_over_lwps to filter
1874 calls to the callback supplied to that function. Returning a
1875 nonzero value causes find_inferiors to stop iterating and return
1876 the current inferior_list_entry. Returning zero indicates that
1877 find_inferiors should continue iterating. */
1880 iterate_over_lwps_filter (struct inferior_list_entry
*entry
, void *args_p
)
1882 struct iterate_over_lwps_args
*args
1883 = (struct iterate_over_lwps_args
*) args_p
;
1885 if (ptid_match (entry
->id
, args
->filter
))
1887 struct thread_info
*thr
= (struct thread_info
*) entry
;
1888 struct lwp_info
*lwp
= get_thread_lwp (thr
);
1890 return (*args
->callback
) (lwp
, args
->data
);
1896 /* See nat/linux-nat.h. */
1899 iterate_over_lwps (ptid_t filter
,
1900 iterate_over_lwps_ftype callback
,
1903 struct iterate_over_lwps_args args
= {filter
, callback
, data
};
1904 struct inferior_list_entry
*entry
;
1906 entry
= find_inferior (&all_threads
, iterate_over_lwps_filter
, &args
);
1910 return get_thread_lwp ((struct thread_info
*) entry
);
1913 /* Detect zombie thread group leaders, and "exit" them. We can't reap
1914 their exits until all other threads in the group have exited. */
1917 check_zombie_leaders (void)
1919 struct process_info
*proc
, *tmp
;
1921 ALL_PROCESSES (proc
, tmp
)
1923 pid_t leader_pid
= pid_of (proc
);
1924 struct lwp_info
*leader_lp
;
1926 leader_lp
= find_lwp_pid (pid_to_ptid (leader_pid
));
1929 debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1930 "num_lwps=%d, zombie=%d\n",
1931 leader_pid
, leader_lp
!= NULL
, num_lwps (leader_pid
),
1932 linux_proc_pid_is_zombie (leader_pid
));
1934 if (leader_lp
!= NULL
&& !leader_lp
->stopped
1935 /* Check if there are other threads in the group, as we may
1936 have raced with the inferior simply exiting. */
1937 && !last_thread_of_process_p (leader_pid
)
1938 && linux_proc_pid_is_zombie (leader_pid
))
1940 /* A leader zombie can mean one of two things:
1942 - It exited, and there's an exit status pending
1943 available, or only the leader exited (not the whole
1944 program). In the latter case, we can't waitpid the
1945 leader's exit status until all other threads are gone.
1947 - There are 3 or more threads in the group, and a thread
1948 other than the leader exec'd. On an exec, the Linux
1949 kernel destroys all other threads (except the execing
1950 one) in the thread group, and resets the execing thread's
1951 tid to the tgid. No exit notification is sent for the
1952 execing thread -- from the ptracer's perspective, it
1953 appears as though the execing thread just vanishes.
1954 Until we reap all other threads except the leader and the
1955 execing thread, the leader will be zombie, and the
1956 execing thread will be in `D (disc sleep)'. As soon as
1957 all other threads are reaped, the execing thread changes
1958 it's tid to the tgid, and the previous (zombie) leader
1959 vanishes, giving place to the "new" leader. We could try
1960 distinguishing the exit and exec cases, by waiting once
1961 more, and seeing if something comes out, but it doesn't
1962 sound useful. The previous leader _does_ go away, and
1963 we'll re-add the new one once we see the exec event
1964 (which is just the same as what would happen if the
1965 previous leader did exit voluntarily before some other
1970 "CZL: Thread group leader %d zombie "
1971 "(it exited, or another thread execd).\n",
1974 delete_lwp (leader_lp
);
1979 /* Callback for `find_inferior'. Returns the first LWP that is not
1980 stopped. ARG is a PTID filter. */
1983 not_stopped_callback (struct inferior_list_entry
*entry
, void *arg
)
1985 struct thread_info
*thr
= (struct thread_info
*) entry
;
1986 struct lwp_info
*lwp
;
1987 ptid_t filter
= *(ptid_t
*) arg
;
1989 if (!ptid_match (ptid_of (thr
), filter
))
1992 lwp
= get_thread_lwp (thr
);
1999 /* Increment LWP's suspend count. */
2002 lwp_suspended_inc (struct lwp_info
*lwp
)
2006 if (debug_threads
&& lwp
->suspended
> 4)
2008 struct thread_info
*thread
= get_lwp_thread (lwp
);
2010 debug_printf ("LWP %ld has a suspiciously high suspend count,"
2011 " suspended=%d\n", lwpid_of (thread
), lwp
->suspended
);
2015 /* Decrement LWP's suspend count. */
2018 lwp_suspended_decr (struct lwp_info
*lwp
)
2022 if (lwp
->suspended
< 0)
2024 struct thread_info
*thread
= get_lwp_thread (lwp
);
2026 internal_error (__FILE__
, __LINE__
,
2027 "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread
),
2032 /* This function should only be called if the LWP got a SIGTRAP.
2034 Handle any tracepoint steps or hits. Return true if a tracepoint
2035 event was handled, 0 otherwise. */
2038 handle_tracepoints (struct lwp_info
*lwp
)
2040 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
2041 int tpoint_related_event
= 0;
2043 gdb_assert (lwp
->suspended
== 0);
2045 /* If this tracepoint hit causes a tracing stop, we'll immediately
2046 uninsert tracepoints. To do this, we temporarily pause all
2047 threads, unpatch away, and then unpause threads. We need to make
2048 sure the unpausing doesn't resume LWP too. */
2049 lwp_suspended_inc (lwp
);
2051 /* And we need to be sure that any all-threads-stopping doesn't try
2052 to move threads out of the jump pads, as it could deadlock the
2053 inferior (LWP could be in the jump pad, maybe even holding the
2056 /* Do any necessary step collect actions. */
2057 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
2059 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
2061 /* See if we just hit a tracepoint and do its main collect
2063 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
2065 lwp_suspended_decr (lwp
);
2067 gdb_assert (lwp
->suspended
== 0);
2068 gdb_assert (!stabilizing_threads
|| lwp
->collecting_fast_tracepoint
);
2070 if (tpoint_related_event
)
2073 debug_printf ("got a tracepoint event\n");
2080 /* Convenience wrapper. Returns true if LWP is presently collecting a
2084 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
2085 struct fast_tpoint_collect_status
*status
)
2087 CORE_ADDR thread_area
;
2088 struct thread_info
*thread
= get_lwp_thread (lwp
);
2090 if (the_low_target
.get_thread_area
== NULL
)
2093 /* Get the thread area address. This is used to recognize which
2094 thread is which when tracing with the in-process agent library.
2095 We don't read anything from the address, and treat it as opaque;
2096 it's the address itself that we assume is unique per-thread. */
2097 if ((*the_low_target
.get_thread_area
) (lwpid_of (thread
), &thread_area
) == -1)
2100 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
2103 /* The reason we resume in the caller, is because we want to be able
2104 to pass lwp->status_pending as WSTAT, and we need to clear
2105 status_pending_p before resuming, otherwise, linux_resume_one_lwp
2106 refuses to resume. */
2109 maybe_move_out_of_jump_pad (struct lwp_info
*lwp
, int *wstat
)
2111 struct thread_info
*saved_thread
;
2113 saved_thread
= current_thread
;
2114 current_thread
= get_lwp_thread (lwp
);
2117 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
2118 && supports_fast_tracepoints ()
2119 && agent_loaded_p ())
2121 struct fast_tpoint_collect_status status
;
2125 debug_printf ("Checking whether LWP %ld needs to move out of the "
2127 lwpid_of (current_thread
));
2129 r
= linux_fast_tracepoint_collecting (lwp
, &status
);
2132 || (WSTOPSIG (*wstat
) != SIGILL
2133 && WSTOPSIG (*wstat
) != SIGFPE
2134 && WSTOPSIG (*wstat
) != SIGSEGV
2135 && WSTOPSIG (*wstat
) != SIGBUS
))
2137 lwp
->collecting_fast_tracepoint
= r
;
2141 if (r
== 1 && lwp
->exit_jump_pad_bkpt
== NULL
)
2143 /* Haven't executed the original instruction yet.
2144 Set breakpoint there, and wait till it's hit,
2145 then single-step until exiting the jump pad. */
2146 lwp
->exit_jump_pad_bkpt
2147 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
2151 debug_printf ("Checking whether LWP %ld needs to move out of "
2152 "the jump pad...it does\n",
2153 lwpid_of (current_thread
));
2154 current_thread
= saved_thread
;
2161 /* If we get a synchronous signal while collecting, *and*
2162 while executing the (relocated) original instruction,
2163 reset the PC to point at the tpoint address, before
2164 reporting to GDB. Otherwise, it's an IPA lib bug: just
2165 report the signal to GDB, and pray for the best. */
2167 lwp
->collecting_fast_tracepoint
= 0;
2170 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
2171 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
2174 struct regcache
*regcache
;
2176 /* The si_addr on a few signals references the address
2177 of the faulting instruction. Adjust that as
2179 if ((WSTOPSIG (*wstat
) == SIGILL
2180 || WSTOPSIG (*wstat
) == SIGFPE
2181 || WSTOPSIG (*wstat
) == SIGBUS
2182 || WSTOPSIG (*wstat
) == SIGSEGV
)
2183 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
2184 (PTRACE_TYPE_ARG3
) 0, &info
) == 0
2185 /* Final check just to make sure we don't clobber
2186 the siginfo of non-kernel-sent signals. */
2187 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
2189 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
2190 ptrace (PTRACE_SETSIGINFO
, lwpid_of (current_thread
),
2191 (PTRACE_TYPE_ARG3
) 0, &info
);
2194 regcache
= get_thread_regcache (current_thread
, 1);
2195 (*the_low_target
.set_pc
) (regcache
, status
.tpoint_addr
);
2196 lwp
->stop_pc
= status
.tpoint_addr
;
2198 /* Cancel any fast tracepoint lock this thread was
2200 force_unlock_trace_buffer ();
2203 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
2206 debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
2207 "stopping all threads momentarily.\n");
2209 stop_all_lwps (1, lwp
);
2211 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
2212 lwp
->exit_jump_pad_bkpt
= NULL
;
2214 unstop_all_lwps (1, lwp
);
2216 gdb_assert (lwp
->suspended
>= 0);
2222 debug_printf ("Checking whether LWP %ld needs to move out of the "
2224 lwpid_of (current_thread
));
2226 current_thread
= saved_thread
;
2230 /* Enqueue one signal in the "signals to report later when out of the
2234 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2236 struct pending_signals
*p_sig
;
2237 struct thread_info
*thread
= get_lwp_thread (lwp
);
2240 debug_printf ("Deferring signal %d for LWP %ld.\n",
2241 WSTOPSIG (*wstat
), lwpid_of (thread
));
2245 struct pending_signals
*sig
;
2247 for (sig
= lwp
->pending_signals_to_report
;
2250 debug_printf (" Already queued %d\n",
2253 debug_printf (" (no more currently queued signals)\n");
2256 /* Don't enqueue non-RT signals if they are already in the deferred
2257 queue. (SIGSTOP being the easiest signal to see ending up here
2259 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
2261 struct pending_signals
*sig
;
2263 for (sig
= lwp
->pending_signals_to_report
;
2267 if (sig
->signal
== WSTOPSIG (*wstat
))
2270 debug_printf ("Not requeuing already queued non-RT signal %d"
2279 p_sig
= XCNEW (struct pending_signals
);
2280 p_sig
->prev
= lwp
->pending_signals_to_report
;
2281 p_sig
->signal
= WSTOPSIG (*wstat
);
2283 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2286 lwp
->pending_signals_to_report
= p_sig
;
2289 /* Dequeue one signal from the "signals to report later when out of
2290 the jump pad" list. */
2293 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2295 struct thread_info
*thread
= get_lwp_thread (lwp
);
2297 if (lwp
->pending_signals_to_report
!= NULL
)
2299 struct pending_signals
**p_sig
;
2301 p_sig
= &lwp
->pending_signals_to_report
;
2302 while ((*p_sig
)->prev
!= NULL
)
2303 p_sig
= &(*p_sig
)->prev
;
2305 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
2306 if ((*p_sig
)->info
.si_signo
!= 0)
2307 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2313 debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
2314 WSTOPSIG (*wstat
), lwpid_of (thread
));
2318 struct pending_signals
*sig
;
2320 for (sig
= lwp
->pending_signals_to_report
;
2323 debug_printf (" Still queued %d\n",
2326 debug_printf (" (no more queued signals)\n");
2335 /* Fetch the possibly triggered data watchpoint info and store it in
2338 On some archs, like x86, that use debug registers to set
2339 watchpoints, it's possible that the way to know which watched
2340 address trapped, is to check the register that is used to select
2341 which address to watch. Problem is, between setting the watchpoint
2342 and reading back which data address trapped, the user may change
2343 the set of watchpoints, and, as a consequence, GDB changes the
2344 debug registers in the inferior. To avoid reading back a stale
2345 stopped-data-address when that happens, we cache in LP the fact
2346 that a watchpoint trapped, and the corresponding data address, as
2347 soon as we see CHILD stop with a SIGTRAP. If GDB changes the debug
2348 registers meanwhile, we have the cached data we can rely on. */
2351 check_stopped_by_watchpoint (struct lwp_info
*child
)
2353 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
2355 struct thread_info
*saved_thread
;
2357 saved_thread
= current_thread
;
2358 current_thread
= get_lwp_thread (child
);
2360 if (the_low_target
.stopped_by_watchpoint ())
2362 child
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2364 if (the_low_target
.stopped_data_address
!= NULL
)
2365 child
->stopped_data_address
2366 = the_low_target
.stopped_data_address ();
2368 child
->stopped_data_address
= 0;
2371 current_thread
= saved_thread
;
2374 return child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2377 /* Return the ptrace options that we want to try to enable. */
2380 linux_low_ptrace_options (int attached
)
2385 options
|= PTRACE_O_EXITKILL
;
2387 if (report_fork_events
)
2388 options
|= PTRACE_O_TRACEFORK
;
2390 if (report_vfork_events
)
2391 options
|= (PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEVFORKDONE
);
2393 if (report_exec_events
)
2394 options
|= PTRACE_O_TRACEEXEC
;
2396 options
|= PTRACE_O_TRACESYSGOOD
;
2401 /* Do low-level handling of the event, and check if we should go on
2402 and pass it to caller code. Return the affected lwp if we are, or
2405 static struct lwp_info
*
2406 linux_low_filter_event (int lwpid
, int wstat
)
2408 struct lwp_info
*child
;
2409 struct thread_info
*thread
;
2410 int have_stop_pc
= 0;
2412 child
= find_lwp_pid (pid_to_ptid (lwpid
));
2414 /* Check for stop events reported by a process we didn't already
2415 know about - anything not already in our LWP list.
2417 If we're expecting to receive stopped processes after
2418 fork, vfork, and clone events, then we'll just add the
2419 new one to our list and go back to waiting for the event
2420 to be reported - the stopped process might be returned
2421 from waitpid before or after the event is.
2423 But note the case of a non-leader thread exec'ing after the
2424 leader having exited, and gone from our lists (because
2425 check_zombie_leaders deleted it). The non-leader thread
2426 changes its tid to the tgid. */
2428 if (WIFSTOPPED (wstat
) && child
== NULL
&& WSTOPSIG (wstat
) == SIGTRAP
2429 && linux_ptrace_get_extended_event (wstat
) == PTRACE_EVENT_EXEC
)
2433 /* A multi-thread exec after we had seen the leader exiting. */
2436 debug_printf ("LLW: Re-adding thread group leader LWP %d"
2437 "after exec.\n", lwpid
);
2440 child_ptid
= ptid_build (lwpid
, lwpid
, 0);
2441 child
= add_lwp (child_ptid
);
2443 current_thread
= child
->thread
;
2446 /* If we didn't find a process, one of two things presumably happened:
2447 - A process we started and then detached from has exited. Ignore it.
2448 - A process we are controlling has forked and the new child's stop
2449 was reported to us by the kernel. Save its PID. */
2450 if (child
== NULL
&& WIFSTOPPED (wstat
))
2452 add_to_pid_list (&stopped_pids
, lwpid
, wstat
);
2455 else if (child
== NULL
)
2458 thread
= get_lwp_thread (child
);
2462 child
->last_status
= wstat
;
2464 /* Check if the thread has exited. */
2465 if ((WIFEXITED (wstat
) || WIFSIGNALED (wstat
)))
2468 debug_printf ("LLFE: %d exited.\n", lwpid
);
2470 if (finish_step_over (child
))
2472 /* Unsuspend all other LWPs, and set them back running again. */
2473 unsuspend_all_lwps (child
);
2476 /* If there is at least one more LWP, then the exit signal was
2477 not the end of the debugged application and should be
2478 ignored, unless GDB wants to hear about thread exits. */
2479 if (report_thread_events
2480 || last_thread_of_process_p (pid_of (thread
)))
2482 /* Since events are serialized to GDB core, and we can't
2483 report this one right now. Leave the status pending for
2484 the next time we're able to report it. */
2485 mark_lwp_dead (child
, wstat
);
2495 gdb_assert (WIFSTOPPED (wstat
));
2497 if (WIFSTOPPED (wstat
))
2499 struct process_info
*proc
;
2501 /* Architecture-specific setup after inferior is running. */
2502 proc
= find_process_pid (pid_of (thread
));
2503 if (proc
->tdesc
== NULL
)
2507 /* This needs to happen after we have attached to the
2508 inferior and it is stopped for the first time, but
2509 before we access any inferior registers. */
2510 linux_arch_setup_thread (thread
);
2514 /* The process is started, but GDBserver will do
2515 architecture-specific setup after the program stops at
2516 the first instruction. */
2517 child
->status_pending_p
= 1;
2518 child
->status_pending
= wstat
;
2524 if (WIFSTOPPED (wstat
) && child
->must_set_ptrace_flags
)
2526 struct process_info
*proc
= find_process_pid (pid_of (thread
));
2527 int options
= linux_low_ptrace_options (proc
->attached
);
2529 linux_enable_event_reporting (lwpid
, options
);
2530 child
->must_set_ptrace_flags
= 0;
2533 /* Always update syscall_state, even if it will be filtered later. */
2534 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SYSCALL_SIGTRAP
)
2536 child
->syscall_state
2537 = (child
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2538 ? TARGET_WAITKIND_SYSCALL_RETURN
2539 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2543 /* Almost all other ptrace-stops are known to be outside of system
2544 calls, with further exceptions in handle_extended_wait. */
2545 child
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2548 /* Be careful to not overwrite stop_pc until save_stop_reason is
2550 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
2551 && linux_is_extended_waitstatus (wstat
))
2553 child
->stop_pc
= get_pc (child
);
2554 if (handle_extended_wait (&child
, wstat
))
2556 /* The event has been handled, so just return without
2562 if (linux_wstatus_maybe_breakpoint (wstat
))
2564 if (save_stop_reason (child
))
2569 child
->stop_pc
= get_pc (child
);
2571 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGSTOP
2572 && child
->stop_expected
)
2575 debug_printf ("Expected stop.\n");
2576 child
->stop_expected
= 0;
2578 if (thread
->last_resume_kind
== resume_stop
)
2580 /* We want to report the stop to the core. Treat the
2581 SIGSTOP as a normal event. */
2583 debug_printf ("LLW: resume_stop SIGSTOP caught for %s.\n",
2584 target_pid_to_str (ptid_of (thread
)));
2586 else if (stopping_threads
!= NOT_STOPPING_THREADS
)
2588 /* Stopping threads. We don't want this SIGSTOP to end up
2591 debug_printf ("LLW: SIGSTOP caught for %s "
2592 "while stopping threads.\n",
2593 target_pid_to_str (ptid_of (thread
)));
2598 /* This is a delayed SIGSTOP. Filter out the event. */
2600 debug_printf ("LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2601 child
->stepping
? "step" : "continue",
2602 target_pid_to_str (ptid_of (thread
)));
2604 linux_resume_one_lwp (child
, child
->stepping
, 0, NULL
);
2609 child
->status_pending_p
= 1;
2610 child
->status_pending
= wstat
;
2614 /* Return true if THREAD is doing hardware single step. */
2617 maybe_hw_step (struct thread_info
*thread
)
2619 if (can_hardware_single_step ())
2623 /* GDBserver must insert reinsert breakpoint for software
2625 gdb_assert (has_reinsert_breakpoints (thread
));
2630 /* Resume LWPs that are currently stopped without any pending status
2631 to report, but are resumed from the core's perspective. */
2634 resume_stopped_resumed_lwps (struct inferior_list_entry
*entry
)
2636 struct thread_info
*thread
= (struct thread_info
*) entry
;
2637 struct lwp_info
*lp
= get_thread_lwp (thread
);
2641 && !lp
->status_pending_p
2642 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2646 if (thread
->last_resume_kind
== resume_step
)
2647 step
= maybe_hw_step (thread
);
2650 debug_printf ("RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
2651 target_pid_to_str (ptid_of (thread
)),
2652 paddress (lp
->stop_pc
),
2655 linux_resume_one_lwp (lp
, step
, GDB_SIGNAL_0
, NULL
);
2659 /* Wait for an event from child(ren) WAIT_PTID, and return any that
2660 match FILTER_PTID (leaving others pending). The PTIDs can be:
2661 minus_one_ptid, to specify any child; a pid PTID, specifying all
2662 lwps of a thread group; or a PTID representing a single lwp. Store
2663 the stop status through the status pointer WSTAT. OPTIONS is
2664 passed to the waitpid call. Return 0 if no event was found and
2665 OPTIONS contains WNOHANG. Return -1 if no unwaited-for children
2666 was found. Return the PID of the stopped child otherwise. */
2669 linux_wait_for_event_filtered (ptid_t wait_ptid
, ptid_t filter_ptid
,
2670 int *wstatp
, int options
)
2672 struct thread_info
*event_thread
;
2673 struct lwp_info
*event_child
, *requested_child
;
2674 sigset_t block_mask
, prev_mask
;
2677 /* N.B. event_thread points to the thread_info struct that contains
2678 event_child. Keep them in sync. */
2679 event_thread
= NULL
;
2681 requested_child
= NULL
;
2683 /* Check for a lwp with a pending status. */
2685 if (ptid_equal (filter_ptid
, minus_one_ptid
) || ptid_is_pid (filter_ptid
))
2687 event_thread
= (struct thread_info
*)
2688 find_inferior (&all_threads
, status_pending_p_callback
, &filter_ptid
);
2689 if (event_thread
!= NULL
)
2690 event_child
= get_thread_lwp (event_thread
);
2691 if (debug_threads
&& event_thread
)
2692 debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread
));
2694 else if (!ptid_equal (filter_ptid
, null_ptid
))
2696 requested_child
= find_lwp_pid (filter_ptid
);
2698 if (stopping_threads
== NOT_STOPPING_THREADS
2699 && requested_child
->status_pending_p
2700 && requested_child
->collecting_fast_tracepoint
)
2702 enqueue_one_deferred_signal (requested_child
,
2703 &requested_child
->status_pending
);
2704 requested_child
->status_pending_p
= 0;
2705 requested_child
->status_pending
= 0;
2706 linux_resume_one_lwp (requested_child
, 0, 0, NULL
);
2709 if (requested_child
->suspended
2710 && requested_child
->status_pending_p
)
2712 internal_error (__FILE__
, __LINE__
,
2713 "requesting an event out of a"
2714 " suspended child?");
2717 if (requested_child
->status_pending_p
)
2719 event_child
= requested_child
;
2720 event_thread
= get_lwp_thread (event_child
);
2724 if (event_child
!= NULL
)
2727 debug_printf ("Got an event from pending child %ld (%04x)\n",
2728 lwpid_of (event_thread
), event_child
->status_pending
);
2729 *wstatp
= event_child
->status_pending
;
2730 event_child
->status_pending_p
= 0;
2731 event_child
->status_pending
= 0;
2732 current_thread
= event_thread
;
2733 return lwpid_of (event_thread
);
2736 /* But if we don't find a pending event, we'll have to wait.
2738 We only enter this loop if no process has a pending wait status.
2739 Thus any action taken in response to a wait status inside this
2740 loop is responding as soon as we detect the status, not after any
2743 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2744 all signals while here. */
2745 sigfillset (&block_mask
);
2746 sigprocmask (SIG_BLOCK
, &block_mask
, &prev_mask
);
2748 /* Always pull all events out of the kernel. We'll randomly select
2749 an event LWP out of all that have events, to prevent
2751 while (event_child
== NULL
)
2755 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2758 - If the thread group leader exits while other threads in the
2759 thread group still exist, waitpid(TGID, ...) hangs. That
2760 waitpid won't return an exit status until the other threads
2761 in the group are reaped.
2763 - When a non-leader thread execs, that thread just vanishes
2764 without reporting an exit (so we'd hang if we waited for it
2765 explicitly in that case). The exec event is reported to
2768 ret
= my_waitpid (-1, wstatp
, options
| WNOHANG
);
2771 debug_printf ("LWFE: waitpid(-1, ...) returned %d, %s\n",
2772 ret
, errno
? strerror (errno
) : "ERRNO-OK");
2778 debug_printf ("LLW: waitpid %ld received %s\n",
2779 (long) ret
, status_to_str (*wstatp
));
2782 /* Filter all events. IOW, leave all events pending. We'll
2783 randomly select an event LWP out of all that have events
2785 linux_low_filter_event (ret
, *wstatp
);
2786 /* Retry until nothing comes out of waitpid. A single
2787 SIGCHLD can indicate more than one child stopped. */
2791 /* Now that we've pulled all events out of the kernel, resume
2792 LWPs that don't have an interesting event to report. */
2793 if (stopping_threads
== NOT_STOPPING_THREADS
)
2794 for_each_inferior (&all_threads
, resume_stopped_resumed_lwps
);
2796 /* ... and find an LWP with a status to report to the core, if
2798 event_thread
= (struct thread_info
*)
2799 find_inferior (&all_threads
, status_pending_p_callback
, &filter_ptid
);
2800 if (event_thread
!= NULL
)
2802 event_child
= get_thread_lwp (event_thread
);
2803 *wstatp
= event_child
->status_pending
;
2804 event_child
->status_pending_p
= 0;
2805 event_child
->status_pending
= 0;
2809 /* Check for zombie thread group leaders. Those can't be reaped
2810 until all other threads in the thread group are. */
2811 check_zombie_leaders ();
2813 /* If there are no resumed children left in the set of LWPs we
2814 want to wait for, bail. We can't just block in
2815 waitpid/sigsuspend, because lwps might have been left stopped
2816 in trace-stop state, and we'd be stuck forever waiting for
2817 their status to change (which would only happen if we resumed
2818 them). Even if WNOHANG is set, this return code is preferred
2819 over 0 (below), as it is more detailed. */
2820 if ((find_inferior (&all_threads
,
2821 not_stopped_callback
,
2822 &wait_ptid
) == NULL
))
2825 debug_printf ("LLW: exit (no unwaited-for LWP)\n");
2826 sigprocmask (SIG_SETMASK
, &prev_mask
, NULL
);
2830 /* No interesting event to report to the caller. */
2831 if ((options
& WNOHANG
))
2834 debug_printf ("WNOHANG set, no event found\n");
2836 sigprocmask (SIG_SETMASK
, &prev_mask
, NULL
);
2840 /* Block until we get an event reported with SIGCHLD. */
2842 debug_printf ("sigsuspend'ing\n");
2844 sigsuspend (&prev_mask
);
2845 sigprocmask (SIG_SETMASK
, &prev_mask
, NULL
);
2849 sigprocmask (SIG_SETMASK
, &prev_mask
, NULL
);
2851 current_thread
= event_thread
;
2853 return lwpid_of (event_thread
);
2856 /* Wait for an event from child(ren) PTID. PTIDs can be:
2857 minus_one_ptid, to specify any child; a pid PTID, specifying all
2858 lwps of a thread group; or a PTID representing a single lwp. Store
2859 the stop status through the status pointer WSTAT. OPTIONS is
2860 passed to the waitpid call. Return 0 if no event was found and
2861 OPTIONS contains WNOHANG. Return -1 if no unwaited-for children
2862 was found. Return the PID of the stopped child otherwise. */
2865 linux_wait_for_event (ptid_t ptid
, int *wstatp
, int options
)
2867 return linux_wait_for_event_filtered (ptid
, ptid
, wstatp
, options
);
2870 /* Count the LWP's that have had events. */
2873 count_events_callback (struct inferior_list_entry
*entry
, void *data
)
2875 struct thread_info
*thread
= (struct thread_info
*) entry
;
2876 struct lwp_info
*lp
= get_thread_lwp (thread
);
2877 int *count
= (int *) data
;
2879 gdb_assert (count
!= NULL
);
2881 /* Count only resumed LWPs that have an event pending. */
2882 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2883 && lp
->status_pending_p
)
2889 /* Select the LWP (if any) that is currently being single-stepped. */
2892 select_singlestep_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
2894 struct thread_info
*thread
= (struct thread_info
*) entry
;
2895 struct lwp_info
*lp
= get_thread_lwp (thread
);
2897 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2898 && thread
->last_resume_kind
== resume_step
2899 && lp
->status_pending_p
)
2905 /* Select the Nth LWP that has had an event. */
2908 select_event_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
2910 struct thread_info
*thread
= (struct thread_info
*) entry
;
2911 struct lwp_info
*lp
= get_thread_lwp (thread
);
2912 int *selector
= (int *) data
;
2914 gdb_assert (selector
!= NULL
);
2916 /* Select only resumed LWPs that have an event pending. */
2917 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2918 && lp
->status_pending_p
)
2919 if ((*selector
)-- == 0)
2925 /* Select one LWP out of those that have events pending. */
2928 select_event_lwp (struct lwp_info
**orig_lp
)
2931 int random_selector
;
2932 struct thread_info
*event_thread
= NULL
;
2934 /* In all-stop, give preference to the LWP that is being
2935 single-stepped. There will be at most one, and it's the LWP that
2936 the core is most interested in. If we didn't do this, then we'd
2937 have to handle pending step SIGTRAPs somehow in case the core
2938 later continues the previously-stepped thread, otherwise we'd
2939 report the pending SIGTRAP, and the core, not having stepped the
2940 thread, wouldn't understand what the trap was for, and therefore
2941 would report it to the user as a random signal. */
2945 = (struct thread_info
*) find_inferior (&all_threads
,
2946 select_singlestep_lwp_callback
,
2948 if (event_thread
!= NULL
)
2951 debug_printf ("SEL: Select single-step %s\n",
2952 target_pid_to_str (ptid_of (event_thread
)));
2955 if (event_thread
== NULL
)
2957 /* No single-stepping LWP. Select one at random, out of those
2958 which have had events. */
2960 /* First see how many events we have. */
2961 find_inferior (&all_threads
, count_events_callback
, &num_events
);
2962 gdb_assert (num_events
> 0);
2964 /* Now randomly pick a LWP out of those that have had
2966 random_selector
= (int)
2967 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2969 if (debug_threads
&& num_events
> 1)
2970 debug_printf ("SEL: Found %d SIGTRAP events, selecting #%d\n",
2971 num_events
, random_selector
);
2974 = (struct thread_info
*) find_inferior (&all_threads
,
2975 select_event_lwp_callback
,
2979 if (event_thread
!= NULL
)
2981 struct lwp_info
*event_lp
= get_thread_lwp (event_thread
);
2983 /* Switch the event LWP. */
2984 *orig_lp
= event_lp
;
2988 /* Decrement the suspend count of an LWP. */
2991 unsuspend_one_lwp (struct inferior_list_entry
*entry
, void *except
)
2993 struct thread_info
*thread
= (struct thread_info
*) entry
;
2994 struct lwp_info
*lwp
= get_thread_lwp (thread
);
2996 /* Ignore EXCEPT. */
3000 lwp_suspended_decr (lwp
);
3004 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
3008 unsuspend_all_lwps (struct lwp_info
*except
)
3010 find_inferior (&all_threads
, unsuspend_one_lwp
, except
);
3013 static void move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
);
3014 static int stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
,
3016 static int lwp_running (struct inferior_list_entry
*entry
, void *data
);
3017 static ptid_t
linux_wait_1 (ptid_t ptid
,
3018 struct target_waitstatus
*ourstatus
,
3019 int target_options
);
3021 /* Stabilize threads (move out of jump pads).
3023 If a thread is midway collecting a fast tracepoint, we need to
3024 finish the collection and move it out of the jump pad before
3025 reporting the signal.
3027 This avoids recursion while collecting (when a signal arrives
3028 midway, and the signal handler itself collects), which would trash
3029 the trace buffer. In case the user set a breakpoint in a signal
3030 handler, this avoids the backtrace showing the jump pad, etc..
3031 Most importantly, there are certain things we can't do safely if
3032 threads are stopped in a jump pad (or in its callee's). For
3035 - starting a new trace run. A thread still collecting the
3036 previous run, could trash the trace buffer when resumed. The trace
3037 buffer control structures would have been reset but the thread had
3038 no way to tell. The thread could even midway memcpy'ing to the
3039 buffer, which would mean that when resumed, it would clobber the
3040 trace buffer that had been set for a new run.
3042 - we can't rewrite/reuse the jump pads for new tracepoints
3043 safely. Say you do tstart while a thread is stopped midway while
3044 collecting. When the thread is later resumed, it finishes the
3045 collection, and returns to the jump pad, to execute the original
3046 instruction that was under the tracepoint jump at the time the
3047 older run had been started. If the jump pad had been rewritten
3048 since for something else in the new run, the thread would now
3049 execute the wrong / random instructions. */
3052 linux_stabilize_threads (void)
3054 struct thread_info
*saved_thread
;
3055 struct thread_info
*thread_stuck
;
3058 = (struct thread_info
*) find_inferior (&all_threads
,
3059 stuck_in_jump_pad_callback
,
3061 if (thread_stuck
!= NULL
)
3064 debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
3065 lwpid_of (thread_stuck
));
3069 saved_thread
= current_thread
;
3071 stabilizing_threads
= 1;
3074 for_each_inferior (&all_threads
, move_out_of_jump_pad_callback
);
3076 /* Loop until all are stopped out of the jump pads. */
3077 while (find_inferior (&all_threads
, lwp_running
, NULL
) != NULL
)
3079 struct target_waitstatus ourstatus
;
3080 struct lwp_info
*lwp
;
3083 /* Note that we go through the full wait even loop. While
3084 moving threads out of jump pad, we need to be able to step
3085 over internal breakpoints and such. */
3086 linux_wait_1 (minus_one_ptid
, &ourstatus
, 0);
3088 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
3090 lwp
= get_thread_lwp (current_thread
);
3093 lwp_suspended_inc (lwp
);
3095 if (ourstatus
.value
.sig
!= GDB_SIGNAL_0
3096 || current_thread
->last_resume_kind
== resume_stop
)
3098 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.value
.sig
));
3099 enqueue_one_deferred_signal (lwp
, &wstat
);
3104 unsuspend_all_lwps (NULL
);
3106 stabilizing_threads
= 0;
3108 current_thread
= saved_thread
;
3113 = (struct thread_info
*) find_inferior (&all_threads
,
3114 stuck_in_jump_pad_callback
,
3116 if (thread_stuck
!= NULL
)
3117 debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
3118 lwpid_of (thread_stuck
));
3122 /* Convenience function that is called when the kernel reports an
3123 event that is not passed out to GDB. */
3126 ignore_event (struct target_waitstatus
*ourstatus
)
3128 /* If we got an event, there may still be others, as a single
3129 SIGCHLD can indicate more than one child stopped. This forces
3130 another target_wait call. */
3133 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3137 /* Convenience function that is called when the kernel reports an exit
3138 event. This decides whether to report the event to GDB as a
3139 process exit event, a thread exit event, or to suppress the
3143 filter_exit_event (struct lwp_info
*event_child
,
3144 struct target_waitstatus
*ourstatus
)
3146 struct thread_info
*thread
= get_lwp_thread (event_child
);
3147 ptid_t ptid
= ptid_of (thread
);
3149 if (!last_thread_of_process_p (pid_of (thread
)))
3151 if (report_thread_events
)
3152 ourstatus
->kind
= TARGET_WAITKIND_THREAD_EXITED
;
3154 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3156 delete_lwp (event_child
);
3161 /* Returns 1 if GDB is interested in any event_child syscalls. */
3164 gdb_catching_syscalls_p (struct lwp_info
*event_child
)
3166 struct thread_info
*thread
= get_lwp_thread (event_child
);
3167 struct process_info
*proc
= get_thread_process (thread
);
3169 return !VEC_empty (int, proc
->syscalls_to_catch
);
3172 /* Returns 1 if GDB is interested in the event_child syscall.
3173 Only to be called when stopped reason is SYSCALL_SIGTRAP. */
3176 gdb_catch_this_syscall_p (struct lwp_info
*event_child
)
3180 struct thread_info
*thread
= get_lwp_thread (event_child
);
3181 struct process_info
*proc
= get_thread_process (thread
);
3183 if (VEC_empty (int, proc
->syscalls_to_catch
))
3186 if (VEC_index (int, proc
->syscalls_to_catch
, 0) == ANY_SYSCALL
)
3189 get_syscall_trapinfo (event_child
, &sysno
);
3191 VEC_iterate (int, proc
->syscalls_to_catch
, i
, iter
);
3199 /* Wait for process, returns status. */
3202 linux_wait_1 (ptid_t ptid
,
3203 struct target_waitstatus
*ourstatus
, int target_options
)
3206 struct lwp_info
*event_child
;
3209 int step_over_finished
;
3210 int bp_explains_trap
;
3211 int maybe_internal_trap
;
3220 debug_printf ("linux_wait_1: [%s]\n", target_pid_to_str (ptid
));
3223 /* Translate generic target options into linux options. */
3225 if (target_options
& TARGET_WNOHANG
)
3228 bp_explains_trap
= 0;
3231 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3233 /* Find a resumed LWP, if any. */
3234 if (find_inferior (&all_threads
,
3235 status_pending_p_callback
,
3236 &minus_one_ptid
) != NULL
)
3238 else if ((find_inferior (&all_threads
,
3239 not_stopped_callback
,
3240 &minus_one_ptid
) != NULL
))
3245 if (ptid_equal (step_over_bkpt
, null_ptid
))
3246 pid
= linux_wait_for_event (ptid
, &w
, options
);
3250 debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
3251 target_pid_to_str (step_over_bkpt
));
3252 pid
= linux_wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
3255 if (pid
== 0 || (pid
== -1 && !any_resumed
))
3257 gdb_assert (target_options
& TARGET_WNOHANG
);
3261 debug_printf ("linux_wait_1 ret = null_ptid, "
3262 "TARGET_WAITKIND_IGNORE\n");
3266 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3273 debug_printf ("linux_wait_1 ret = null_ptid, "
3274 "TARGET_WAITKIND_NO_RESUMED\n");
3278 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3282 event_child
= get_thread_lwp (current_thread
);
3284 /* linux_wait_for_event only returns an exit status for the last
3285 child of a process. Report it. */
3286 if (WIFEXITED (w
) || WIFSIGNALED (w
))
3290 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
3291 ourstatus
->value
.integer
= WEXITSTATUS (w
);
3295 debug_printf ("linux_wait_1 ret = %s, exited with "
3297 target_pid_to_str (ptid_of (current_thread
)),
3304 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
3305 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (w
));
3309 debug_printf ("linux_wait_1 ret = %s, terminated with "
3311 target_pid_to_str (ptid_of (current_thread
)),
3317 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3318 return filter_exit_event (event_child
, ourstatus
);
3320 return ptid_of (current_thread
);
3323 /* If step-over executes a breakpoint instruction, in the case of a
3324 hardware single step it means a gdb/gdbserver breakpoint had been
3325 planted on top of a permanent breakpoint, in the case of a software
3326 single step it may just mean that gdbserver hit the reinsert breakpoint.
3327 The PC has been adjusted by save_stop_reason to point at
3328 the breakpoint address.
3329 So in the case of the hardware single step advance the PC manually
3330 past the breakpoint and in the case of software single step advance only
3331 if it's not the reinsert_breakpoint we are hitting.
3332 This avoids that a program would keep trapping a permanent breakpoint
3334 if (!ptid_equal (step_over_bkpt
, null_ptid
)
3335 && event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3336 && (event_child
->stepping
3337 || !reinsert_breakpoint_inserted_here (event_child
->stop_pc
)))
3339 int increment_pc
= 0;
3340 int breakpoint_kind
= 0;
3341 CORE_ADDR stop_pc
= event_child
->stop_pc
;
3344 the_target
->breakpoint_kind_from_current_state (&stop_pc
);
3345 the_target
->sw_breakpoint_from_kind (breakpoint_kind
, &increment_pc
);
3349 debug_printf ("step-over for %s executed software breakpoint\n",
3350 target_pid_to_str (ptid_of (current_thread
)));
3353 if (increment_pc
!= 0)
3355 struct regcache
*regcache
3356 = get_thread_regcache (current_thread
, 1);
3358 event_child
->stop_pc
+= increment_pc
;
3359 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
3361 if (!(*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))
3362 event_child
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
3366 /* If this event was not handled before, and is not a SIGTRAP, we
3367 report it. SIGILL and SIGSEGV are also treated as traps in case
3368 a breakpoint is inserted at the current PC. If this target does
3369 not support internal breakpoints at all, we also report the
3370 SIGTRAP without further processing; it's of no concern to us. */
3372 = (supports_breakpoints ()
3373 && (WSTOPSIG (w
) == SIGTRAP
3374 || ((WSTOPSIG (w
) == SIGILL
3375 || WSTOPSIG (w
) == SIGSEGV
)
3376 && (*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))));
3378 if (maybe_internal_trap
)
3380 /* Handle anything that requires bookkeeping before deciding to
3381 report the event or continue waiting. */
3383 /* First check if we can explain the SIGTRAP with an internal
3384 breakpoint, or if we should possibly report the event to GDB.
3385 Do this before anything that may remove or insert a
3387 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
3389 /* We have a SIGTRAP, possibly a step-over dance has just
3390 finished. If so, tweak the state machine accordingly,
3391 reinsert breakpoints and delete any reinsert (software
3392 single-step) breakpoints. */
3393 step_over_finished
= finish_step_over (event_child
);
3395 /* Now invoke the callbacks of any internal breakpoints there. */
3396 check_breakpoints (event_child
->stop_pc
);
3398 /* Handle tracepoint data collecting. This may overflow the
3399 trace buffer, and cause a tracing stop, removing
3401 trace_event
= handle_tracepoints (event_child
);
3403 if (bp_explains_trap
)
3406 debug_printf ("Hit a gdbserver breakpoint.\n");
3411 /* We have some other signal, possibly a step-over dance was in
3412 progress, and it should be cancelled too. */
3413 step_over_finished
= finish_step_over (event_child
);
3416 /* We have all the data we need. Either report the event to GDB, or
3417 resume threads and keep waiting for more. */
3419 /* If we're collecting a fast tracepoint, finish the collection and
3420 move out of the jump pad before delivering a signal. See
3421 linux_stabilize_threads. */
3424 && WSTOPSIG (w
) != SIGTRAP
3425 && supports_fast_tracepoints ()
3426 && agent_loaded_p ())
3429 debug_printf ("Got signal %d for LWP %ld. Check if we need "
3430 "to defer or adjust it.\n",
3431 WSTOPSIG (w
), lwpid_of (current_thread
));
3433 /* Allow debugging the jump pad itself. */
3434 if (current_thread
->last_resume_kind
!= resume_step
3435 && maybe_move_out_of_jump_pad (event_child
, &w
))
3437 enqueue_one_deferred_signal (event_child
, &w
);
3440 debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
3441 WSTOPSIG (w
), lwpid_of (current_thread
));
3443 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
3445 return ignore_event (ourstatus
);
3449 if (event_child
->collecting_fast_tracepoint
)
3452 debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
3453 "Check if we're already there.\n",
3454 lwpid_of (current_thread
),
3455 event_child
->collecting_fast_tracepoint
);
3459 event_child
->collecting_fast_tracepoint
3460 = linux_fast_tracepoint_collecting (event_child
, NULL
);
3462 if (event_child
->collecting_fast_tracepoint
!= 1)
3464 /* No longer need this breakpoint. */
3465 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
3468 debug_printf ("No longer need exit-jump-pad bkpt; removing it."
3469 "stopping all threads momentarily.\n");
3471 /* Other running threads could hit this breakpoint.
3472 We don't handle moribund locations like GDB does,
3473 instead we always pause all threads when removing
3474 breakpoints, so that any step-over or
3475 decr_pc_after_break adjustment is always taken
3476 care of while the breakpoint is still
3478 stop_all_lwps (1, event_child
);
3480 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
3481 event_child
->exit_jump_pad_bkpt
= NULL
;
3483 unstop_all_lwps (1, event_child
);
3485 gdb_assert (event_child
->suspended
>= 0);
3489 if (event_child
->collecting_fast_tracepoint
== 0)
3492 debug_printf ("fast tracepoint finished "
3493 "collecting successfully.\n");
3495 /* We may have a deferred signal to report. */
3496 if (dequeue_one_deferred_signal (event_child
, &w
))
3499 debug_printf ("dequeued one signal.\n");
3504 debug_printf ("no deferred signals.\n");
3506 if (stabilizing_threads
)
3508 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3509 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3513 debug_printf ("linux_wait_1 ret = %s, stopped "
3514 "while stabilizing threads\n",
3515 target_pid_to_str (ptid_of (current_thread
)));
3519 return ptid_of (current_thread
);
3525 /* Check whether GDB would be interested in this event. */
3527 /* Check if GDB is interested in this syscall. */
3529 && WSTOPSIG (w
) == SYSCALL_SIGTRAP
3530 && !gdb_catch_this_syscall_p (event_child
))
3534 debug_printf ("Ignored syscall for LWP %ld.\n",
3535 lwpid_of (current_thread
));
3538 linux_resume_one_lwp (event_child
, event_child
->stepping
,
3540 return ignore_event (ourstatus
);
3543 /* If GDB is not interested in this signal, don't stop other
3544 threads, and don't report it to GDB. Just resume the inferior
3545 right away. We do this for threading-related signals as well as
3546 any that GDB specifically requested we ignore. But never ignore
3547 SIGSTOP if we sent it ourselves, and do not ignore signals when
3548 stepping - they may require special handling to skip the signal
3549 handler. Also never ignore signals that could be caused by a
3552 && current_thread
->last_resume_kind
!= resume_step
3554 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3555 (current_process ()->priv
->thread_db
!= NULL
3556 && (WSTOPSIG (w
) == __SIGRTMIN
3557 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
3560 (pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
3561 && !(WSTOPSIG (w
) == SIGSTOP
3562 && current_thread
->last_resume_kind
== resume_stop
)
3563 && !linux_wstatus_maybe_breakpoint (w
))))
3565 siginfo_t info
, *info_p
;
3568 debug_printf ("Ignored signal %d for LWP %ld.\n",
3569 WSTOPSIG (w
), lwpid_of (current_thread
));
3571 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
3572 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
3577 if (step_over_finished
)
3579 /* We cancelled this thread's step-over above. We still
3580 need to unsuspend all other LWPs, and set them back
3581 running again while the signal handler runs. */
3582 unsuspend_all_lwps (event_child
);
3584 /* Enqueue the pending signal info so that proceed_all_lwps
3586 enqueue_pending_signal (event_child
, WSTOPSIG (w
), info_p
);
3588 proceed_all_lwps ();
3592 linux_resume_one_lwp (event_child
, event_child
->stepping
,
3593 WSTOPSIG (w
), info_p
);
3595 return ignore_event (ourstatus
);
3598 /* Note that all addresses are always "out of the step range" when
3599 there's no range to begin with. */
3600 in_step_range
= lwp_in_step_range (event_child
);
3602 /* If GDB wanted this thread to single step, and the thread is out
3603 of the step range, we always want to report the SIGTRAP, and let
3604 GDB handle it. Watchpoints should always be reported. So should
3605 signals we can't explain. A SIGTRAP we can't explain could be a
3606 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3607 do, we're be able to handle GDB breakpoints on top of internal
3608 breakpoints, by handling the internal breakpoint and still
3609 reporting the event to GDB. If we don't, we're out of luck, GDB
3610 won't see the breakpoint hit. If we see a single-step event but
3611 the thread should be continuing, don't pass the trap to gdb.
3612 That indicates that we had previously finished a single-step but
3613 left the single-step pending -- see
3614 complete_ongoing_step_over. */
3615 report_to_gdb
= (!maybe_internal_trap
3616 || (current_thread
->last_resume_kind
== resume_step
3618 || event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3620 && !bp_explains_trap
3622 && !step_over_finished
3623 && !(current_thread
->last_resume_kind
== resume_continue
3624 && event_child
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
))
3625 || (gdb_breakpoint_here (event_child
->stop_pc
)
3626 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
3627 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
))
3628 || event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
);
3630 run_breakpoint_commands (event_child
->stop_pc
);
3632 /* We found no reason GDB would want us to stop. We either hit one
3633 of our own breakpoints, or finished an internal step GDB
3634 shouldn't know about. */
3639 if (bp_explains_trap
)
3640 debug_printf ("Hit a gdbserver breakpoint.\n");
3641 if (step_over_finished
)
3642 debug_printf ("Step-over finished.\n");
3644 debug_printf ("Tracepoint event.\n");
3645 if (lwp_in_step_range (event_child
))
3646 debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
3647 paddress (event_child
->stop_pc
),
3648 paddress (event_child
->step_range_start
),
3649 paddress (event_child
->step_range_end
));
3652 /* We're not reporting this breakpoint to GDB, so apply the
3653 decr_pc_after_break adjustment to the inferior's regcache
3656 if (the_low_target
.set_pc
!= NULL
)
3658 struct regcache
*regcache
3659 = get_thread_regcache (current_thread
, 1);
3660 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
3663 /* We may have finished stepping over a breakpoint. If so,
3664 we've stopped and suspended all LWPs momentarily except the
3665 stepping one. This is where we resume them all again. We're
3666 going to keep waiting, so use proceed, which handles stepping
3667 over the next breakpoint. */
3669 debug_printf ("proceeding all threads.\n");
3671 if (step_over_finished
)
3672 unsuspend_all_lwps (event_child
);
3674 proceed_all_lwps ();
3675 return ignore_event (ourstatus
);
3680 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3684 str
= target_waitstatus_to_string (&event_child
->waitstatus
);
3685 debug_printf ("LWP %ld: extended event with waitstatus %s\n",
3686 lwpid_of (get_lwp_thread (event_child
)), str
);
3689 if (current_thread
->last_resume_kind
== resume_step
)
3691 if (event_child
->step_range_start
== event_child
->step_range_end
)
3692 debug_printf ("GDB wanted to single-step, reporting event.\n");
3693 else if (!lwp_in_step_range (event_child
))
3694 debug_printf ("Out of step range, reporting event.\n");
3696 if (event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
3697 debug_printf ("Stopped by watchpoint.\n");
3698 else if (gdb_breakpoint_here (event_child
->stop_pc
))
3699 debug_printf ("Stopped by GDB breakpoint.\n");
3701 debug_printf ("Hit a non-gdbserver trap event.\n");
3704 /* Alright, we're going to report a stop. */
3706 /* Remove reinsert breakpoints. */
3707 if (can_software_single_step ())
3709 /* Remove reinsert breakpoints or not. It it is true, stop all
3710 lwps, so that other threads won't hit the breakpoint in the
3712 int remove_reinsert_breakpoints_p
= 0;
3716 remove_reinsert_breakpoints_p
3717 = has_reinsert_breakpoints (current_thread
);
3721 /* In all-stop, a stop reply cancels all previous resume
3722 requests. Delete all reinsert breakpoints. */
3723 struct inferior_list_entry
*inf
, *tmp
;
3725 ALL_INFERIORS (&all_threads
, inf
, tmp
)
3727 struct thread_info
*thread
= (struct thread_info
*) inf
;
3729 if (has_reinsert_breakpoints (thread
))
3731 remove_reinsert_breakpoints_p
= 1;
3737 if (remove_reinsert_breakpoints_p
)
3739 /* If we remove reinsert breakpoints from memory, stop all lwps,
3740 so that other threads won't hit the breakpoint in the staled
3742 stop_all_lwps (0, event_child
);
3746 gdb_assert (has_reinsert_breakpoints (current_thread
));
3747 delete_reinsert_breakpoints (current_thread
);
3751 struct inferior_list_entry
*inf
, *tmp
;
3753 ALL_INFERIORS (&all_threads
, inf
, tmp
)
3755 struct thread_info
*thread
= (struct thread_info
*) inf
;
3757 if (has_reinsert_breakpoints (thread
))
3758 delete_reinsert_breakpoints (thread
);
3762 unstop_all_lwps (0, event_child
);
3766 if (!stabilizing_threads
)
3768 /* In all-stop, stop all threads. */
3770 stop_all_lwps (0, NULL
);
3772 /* If we're not waiting for a specific LWP, choose an event LWP
3773 from among those that have had events. Giving equal priority
3774 to all LWPs that have had events helps prevent
3776 if (ptid_equal (ptid
, minus_one_ptid
))
3778 event_child
->status_pending_p
= 1;
3779 event_child
->status_pending
= w
;
3781 select_event_lwp (&event_child
);
3783 /* current_thread and event_child must stay in sync. */
3784 current_thread
= get_lwp_thread (event_child
);
3786 event_child
->status_pending_p
= 0;
3787 w
= event_child
->status_pending
;
3790 if (step_over_finished
)
3794 /* If we were doing a step-over, all other threads but
3795 the stepping one had been paused in start_step_over,
3796 with their suspend counts incremented. We don't want
3797 to do a full unstop/unpause, because we're in
3798 all-stop mode (so we want threads stopped), but we
3799 still need to unsuspend the other threads, to
3800 decrement their `suspended' count back. */
3801 unsuspend_all_lwps (event_child
);
3805 /* If we just finished a step-over, then all threads had
3806 been momentarily paused. In all-stop, that's fine,
3807 we want threads stopped by now anyway. In non-stop,
3808 we need to re-resume threads that GDB wanted to be
3810 unstop_all_lwps (1, event_child
);
3814 /* Stabilize threads (move out of jump pads). */
3816 stabilize_threads ();
3820 /* If we just finished a step-over, then all threads had been
3821 momentarily paused. In all-stop, that's fine, we want
3822 threads stopped by now anyway. In non-stop, we need to
3823 re-resume threads that GDB wanted to be running. */
3824 if (step_over_finished
)
3825 unstop_all_lwps (1, event_child
);
3828 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3830 /* If the reported event is an exit, fork, vfork or exec, let
3832 *ourstatus
= event_child
->waitstatus
;
3833 /* Clear the event lwp's waitstatus since we handled it already. */
3834 event_child
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3837 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3839 /* Now that we've selected our final event LWP, un-adjust its PC if
3840 it was a software breakpoint, and the client doesn't know we can
3841 adjust the breakpoint ourselves. */
3842 if (event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3843 && !swbreak_feature
)
3845 int decr_pc
= the_low_target
.decr_pc_after_break
;
3849 struct regcache
*regcache
3850 = get_thread_regcache (current_thread
, 1);
3851 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
+ decr_pc
);
3855 if (WSTOPSIG (w
) == SYSCALL_SIGTRAP
)
3857 get_syscall_trapinfo (event_child
,
3858 &ourstatus
->value
.syscall_number
);
3859 ourstatus
->kind
= event_child
->syscall_state
;
3861 else if (current_thread
->last_resume_kind
== resume_stop
3862 && WSTOPSIG (w
) == SIGSTOP
)
3864 /* A thread that has been requested to stop by GDB with vCont;t,
3865 and it stopped cleanly, so report as SIG0. The use of
3866 SIGSTOP is an implementation detail. */
3867 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3869 else if (current_thread
->last_resume_kind
== resume_stop
3870 && WSTOPSIG (w
) != SIGSTOP
)
3872 /* A thread that has been requested to stop by GDB with vCont;t,
3873 but, it stopped for other reasons. */
3874 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3876 else if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
)
3878 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3881 gdb_assert (ptid_equal (step_over_bkpt
, null_ptid
));
3885 debug_printf ("linux_wait_1 ret = %s, %d, %d\n",
3886 target_pid_to_str (ptid_of (current_thread
)),
3887 ourstatus
->kind
, ourstatus
->value
.sig
);
3891 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3892 return filter_exit_event (event_child
, ourstatus
);
3894 return ptid_of (current_thread
);
3897 /* Get rid of any pending event in the pipe. */
3899 async_file_flush (void)
3905 ret
= read (linux_event_pipe
[0], &buf
, 1);
3906 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
3909 /* Put something in the pipe, so the event loop wakes up. */
3911 async_file_mark (void)
3915 async_file_flush ();
3918 ret
= write (linux_event_pipe
[1], "+", 1);
3919 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
3921 /* Ignore EAGAIN. If the pipe is full, the event loop will already
3922 be awakened anyway. */
3926 linux_wait (ptid_t ptid
,
3927 struct target_waitstatus
*ourstatus
, int target_options
)
3931 /* Flush the async file first. */
3932 if (target_is_async_p ())
3933 async_file_flush ();
3937 event_ptid
= linux_wait_1 (ptid
, ourstatus
, target_options
);
3939 while ((target_options
& TARGET_WNOHANG
) == 0
3940 && ptid_equal (event_ptid
, null_ptid
)
3941 && ourstatus
->kind
== TARGET_WAITKIND_IGNORE
);
3943 /* If at least one stop was reported, there may be more. A single
3944 SIGCHLD can signal more than one child stop. */
3945 if (target_is_async_p ()
3946 && (target_options
& TARGET_WNOHANG
) != 0
3947 && !ptid_equal (event_ptid
, null_ptid
))
3953 /* Send a signal to an LWP. */
3956 kill_lwp (unsigned long lwpid
, int signo
)
3961 ret
= syscall (__NR_tkill
, lwpid
, signo
);
3962 if (errno
== ENOSYS
)
3964 /* If tkill fails, then we are not using nptl threads, a
3965 configuration we no longer support. */
3966 perror_with_name (("tkill"));
3972 linux_stop_lwp (struct lwp_info
*lwp
)
3978 send_sigstop (struct lwp_info
*lwp
)
3982 pid
= lwpid_of (get_lwp_thread (lwp
));
3984 /* If we already have a pending stop signal for this process, don't
3986 if (lwp
->stop_expected
)
3989 debug_printf ("Have pending sigstop for lwp %d\n", pid
);
3995 debug_printf ("Sending sigstop to lwp %d\n", pid
);
3997 lwp
->stop_expected
= 1;
3998 kill_lwp (pid
, SIGSTOP
);
4002 send_sigstop_callback (struct inferior_list_entry
*entry
, void *except
)
4004 struct thread_info
*thread
= (struct thread_info
*) entry
;
4005 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4007 /* Ignore EXCEPT. */
4018 /* Increment the suspend count of an LWP, and stop it, if not stopped
4021 suspend_and_send_sigstop_callback (struct inferior_list_entry
*entry
,
4024 struct thread_info
*thread
= (struct thread_info
*) entry
;
4025 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4027 /* Ignore EXCEPT. */
4031 lwp_suspended_inc (lwp
);
4033 return send_sigstop_callback (entry
, except
);
4037 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
4039 /* Store the exit status for later. */
4040 lwp
->status_pending_p
= 1;
4041 lwp
->status_pending
= wstat
;
4043 /* Store in waitstatus as well, as there's nothing else to process
4045 if (WIFEXITED (wstat
))
4047 lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXITED
;
4048 lwp
->waitstatus
.value
.integer
= WEXITSTATUS (wstat
);
4050 else if (WIFSIGNALED (wstat
))
4052 lwp
->waitstatus
.kind
= TARGET_WAITKIND_SIGNALLED
;
4053 lwp
->waitstatus
.value
.sig
= gdb_signal_from_host (WTERMSIG (wstat
));
4056 /* Prevent trying to stop it. */
4059 /* No further stops are expected from a dead lwp. */
4060 lwp
->stop_expected
= 0;
4063 /* Return true if LWP has exited already, and has a pending exit event
4064 to report to GDB. */
4067 lwp_is_marked_dead (struct lwp_info
*lwp
)
4069 return (lwp
->status_pending_p
4070 && (WIFEXITED (lwp
->status_pending
)
4071 || WIFSIGNALED (lwp
->status_pending
)));
4074 /* Wait for all children to stop for the SIGSTOPs we just queued. */
4077 wait_for_sigstop (void)
4079 struct thread_info
*saved_thread
;
4084 saved_thread
= current_thread
;
4085 if (saved_thread
!= NULL
)
4086 saved_tid
= saved_thread
->entry
.id
;
4088 saved_tid
= null_ptid
; /* avoid bogus unused warning */
4091 debug_printf ("wait_for_sigstop: pulling events\n");
4093 /* Passing NULL_PTID as filter indicates we want all events to be
4094 left pending. Eventually this returns when there are no
4095 unwaited-for children left. */
4096 ret
= linux_wait_for_event_filtered (minus_one_ptid
, null_ptid
,
4098 gdb_assert (ret
== -1);
4100 if (saved_thread
== NULL
|| linux_thread_alive (saved_tid
))
4101 current_thread
= saved_thread
;
4105 debug_printf ("Previously current thread died.\n");
4107 /* We can't change the current inferior behind GDB's back,
4108 otherwise, a subsequent command may apply to the wrong
4110 current_thread
= NULL
;
4114 /* Returns true if LWP ENTRY is stopped in a jump pad, and we can't
4115 move it out, because we need to report the stop event to GDB. For
4116 example, if the user puts a breakpoint in the jump pad, it's
4117 because she wants to debug it. */
4120 stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
, void *data
)
4122 struct thread_info
*thread
= (struct thread_info
*) entry
;
4123 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4125 if (lwp
->suspended
!= 0)
4127 internal_error (__FILE__
, __LINE__
,
4128 "LWP %ld is suspended, suspended=%d\n",
4129 lwpid_of (thread
), lwp
->suspended
);
4131 gdb_assert (lwp
->stopped
);
4133 /* Allow debugging the jump pad, gdb_collect, etc.. */
4134 return (supports_fast_tracepoints ()
4135 && agent_loaded_p ()
4136 && (gdb_breakpoint_here (lwp
->stop_pc
)
4137 || lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
4138 || thread
->last_resume_kind
== resume_step
)
4139 && linux_fast_tracepoint_collecting (lwp
, NULL
));
4143 move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
)
4145 struct thread_info
*thread
= (struct thread_info
*) entry
;
4146 struct thread_info
*saved_thread
;
4147 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4150 if (lwp
->suspended
!= 0)
4152 internal_error (__FILE__
, __LINE__
,
4153 "LWP %ld is suspended, suspended=%d\n",
4154 lwpid_of (thread
), lwp
->suspended
);
4156 gdb_assert (lwp
->stopped
);
4158 /* For gdb_breakpoint_here. */
4159 saved_thread
= current_thread
;
4160 current_thread
= thread
;
4162 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
4164 /* Allow debugging the jump pad, gdb_collect, etc. */
4165 if (!gdb_breakpoint_here (lwp
->stop_pc
)
4166 && lwp
->stop_reason
!= TARGET_STOPPED_BY_WATCHPOINT
4167 && thread
->last_resume_kind
!= resume_step
4168 && maybe_move_out_of_jump_pad (lwp
, wstat
))
4171 debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
4176 lwp
->status_pending_p
= 0;
4177 enqueue_one_deferred_signal (lwp
, wstat
);
4180 debug_printf ("Signal %d for LWP %ld deferred "
4182 WSTOPSIG (*wstat
), lwpid_of (thread
));
4185 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
4188 lwp_suspended_inc (lwp
);
4190 current_thread
= saved_thread
;
4194 lwp_running (struct inferior_list_entry
*entry
, void *data
)
4196 struct thread_info
*thread
= (struct thread_info
*) entry
;
4197 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4199 if (lwp_is_marked_dead (lwp
))
4206 /* Stop all lwps that aren't stopped yet, except EXCEPT, if not NULL.
4207 If SUSPEND, then also increase the suspend count of every LWP,
4211 stop_all_lwps (int suspend
, struct lwp_info
*except
)
4213 /* Should not be called recursively. */
4214 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
4219 debug_printf ("stop_all_lwps (%s, except=%s)\n",
4220 suspend
? "stop-and-suspend" : "stop",
4222 ? target_pid_to_str (ptid_of (get_lwp_thread (except
)))
4226 stopping_threads
= (suspend
4227 ? STOPPING_AND_SUSPENDING_THREADS
4228 : STOPPING_THREADS
);
4231 find_inferior (&all_threads
, suspend_and_send_sigstop_callback
, except
);
4233 find_inferior (&all_threads
, send_sigstop_callback
, except
);
4234 wait_for_sigstop ();
4235 stopping_threads
= NOT_STOPPING_THREADS
;
4239 debug_printf ("stop_all_lwps done, setting stopping_threads "
4240 "back to !stopping\n");
4245 /* Enqueue one signal in the chain of signals which need to be
4246 delivered to this process on next resume. */
4249 enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
)
4251 struct pending_signals
*p_sig
= XNEW (struct pending_signals
);
4253 p_sig
->prev
= lwp
->pending_signals
;
4254 p_sig
->signal
= signal
;
4256 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
4258 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
4259 lwp
->pending_signals
= p_sig
;
4262 /* Install breakpoints for software single stepping. */
4265 install_software_single_step_breakpoints (struct lwp_info
*lwp
)
4269 struct thread_info
*thread
= get_lwp_thread (lwp
);
4270 struct regcache
*regcache
= get_thread_regcache (thread
, 1);
4271 VEC (CORE_ADDR
) *next_pcs
= NULL
;
4272 struct cleanup
*old_chain
= make_cleanup_restore_current_thread ();
4274 make_cleanup (VEC_cleanup (CORE_ADDR
), &next_pcs
);
4276 current_thread
= thread
;
4277 next_pcs
= (*the_low_target
.get_next_pcs
) (regcache
);
4279 for (i
= 0; VEC_iterate (CORE_ADDR
, next_pcs
, i
, pc
); ++i
)
4280 set_reinsert_breakpoint (pc
, current_ptid
);
4282 do_cleanups (old_chain
);
4285 /* Single step via hardware or software single step.
4286 Return 1 if hardware single stepping, 0 if software single stepping
4287 or can't single step. */
4290 single_step (struct lwp_info
* lwp
)
4294 if (can_hardware_single_step ())
4298 else if (can_software_single_step ())
4300 install_software_single_step_breakpoints (lwp
);
4306 debug_printf ("stepping is not implemented on this target");
4312 /* The signal can be delivered to the inferior if we are not trying to
4313 finish a fast tracepoint collect. Since signal can be delivered in
4314 the step-over, the program may go to signal handler and trap again
4315 after return from the signal handler. We can live with the spurious
4319 lwp_signal_can_be_delivered (struct lwp_info
*lwp
)
4321 return !lwp
->collecting_fast_tracepoint
;
4324 /* Resume execution of LWP. If STEP is nonzero, single-step it. If
4325 SIGNAL is nonzero, give it that signal. */
4328 linux_resume_one_lwp_throw (struct lwp_info
*lwp
,
4329 int step
, int signal
, siginfo_t
*info
)
4331 struct thread_info
*thread
= get_lwp_thread (lwp
);
4332 struct thread_info
*saved_thread
;
4333 int fast_tp_collecting
;
4335 struct process_info
*proc
= get_thread_process (thread
);
4337 /* Note that target description may not be initialised
4338 (proc->tdesc == NULL) at this point because the program hasn't
4339 stopped at the first instruction yet. It means GDBserver skips
4340 the extra traps from the wrapper program (see option --wrapper).
4341 Code in this function that requires register access should be
4342 guarded by proc->tdesc == NULL or something else. */
4344 if (lwp
->stopped
== 0)
4347 gdb_assert (lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
4349 fast_tp_collecting
= lwp
->collecting_fast_tracepoint
;
4351 gdb_assert (!stabilizing_threads
|| fast_tp_collecting
);
4353 /* Cancel actions that rely on GDB not changing the PC (e.g., the
4354 user used the "jump" command, or "set $pc = foo"). */
4355 if (thread
->while_stepping
!= NULL
&& lwp
->stop_pc
!= get_pc (lwp
))
4357 /* Collecting 'while-stepping' actions doesn't make sense
4359 release_while_stepping_state_list (thread
);
4362 /* If we have pending signals or status, and a new signal, enqueue the
4363 signal. Also enqueue the signal if it can't be delivered to the
4364 inferior right now. */
4366 && (lwp
->status_pending_p
4367 || lwp
->pending_signals
!= NULL
4368 || !lwp_signal_can_be_delivered (lwp
)))
4370 enqueue_pending_signal (lwp
, signal
, info
);
4372 /* Postpone any pending signal. It was enqueued above. */
4376 if (lwp
->status_pending_p
)
4379 debug_printf ("Not resuming lwp %ld (%s, stop %s);"
4380 " has pending status\n",
4381 lwpid_of (thread
), step
? "step" : "continue",
4382 lwp
->stop_expected
? "expected" : "not expected");
4386 saved_thread
= current_thread
;
4387 current_thread
= thread
;
4389 /* This bit needs some thinking about. If we get a signal that
4390 we must report while a single-step reinsert is still pending,
4391 we often end up resuming the thread. It might be better to
4392 (ew) allow a stack of pending events; then we could be sure that
4393 the reinsert happened right away and not lose any signals.
4395 Making this stack would also shrink the window in which breakpoints are
4396 uninserted (see comment in linux_wait_for_lwp) but not enough for
4397 complete correctness, so it won't solve that problem. It may be
4398 worthwhile just to solve this one, however. */
4399 if (lwp
->bp_reinsert
!= 0)
4402 debug_printf (" pending reinsert at 0x%s\n",
4403 paddress (lwp
->bp_reinsert
));
4405 if (can_hardware_single_step ())
4407 if (fast_tp_collecting
== 0)
4410 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
4412 fprintf (stderr
, "BAD - reinserting and suspended(%d).\n",
4417 step
= maybe_hw_step (thread
);
4420 if (fast_tp_collecting
== 1)
4423 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4424 " (exit-jump-pad-bkpt)\n",
4427 else if (fast_tp_collecting
== 2)
4430 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4431 " single-stepping\n",
4434 if (can_hardware_single_step ())
4438 internal_error (__FILE__
, __LINE__
,
4439 "moving out of jump pad single-stepping"
4440 " not implemented on this target");
4444 /* If we have while-stepping actions in this thread set it stepping.
4445 If we have a signal to deliver, it may or may not be set to
4446 SIG_IGN, we don't know. Assume so, and allow collecting
4447 while-stepping into a signal handler. A possible smart thing to
4448 do would be to set an internal breakpoint at the signal return
4449 address, continue, and carry on catching this while-stepping
4450 action only when that breakpoint is hit. A future
4452 if (thread
->while_stepping
!= NULL
)
4455 debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
4458 step
= single_step (lwp
);
4461 if (proc
->tdesc
!= NULL
&& the_low_target
.get_pc
!= NULL
)
4463 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
4465 lwp
->stop_pc
= (*the_low_target
.get_pc
) (regcache
);
4469 debug_printf (" %s from pc 0x%lx\n", step
? "step" : "continue",
4470 (long) lwp
->stop_pc
);
4474 /* If we have pending signals, consume one if it can be delivered to
4476 if (lwp
->pending_signals
!= NULL
&& lwp_signal_can_be_delivered (lwp
))
4478 struct pending_signals
**p_sig
;
4480 p_sig
= &lwp
->pending_signals
;
4481 while ((*p_sig
)->prev
!= NULL
)
4482 p_sig
= &(*p_sig
)->prev
;
4484 signal
= (*p_sig
)->signal
;
4485 if ((*p_sig
)->info
.si_signo
!= 0)
4486 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
4494 debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
4495 lwpid_of (thread
), step
? "step" : "continue", signal
,
4496 lwp
->stop_expected
? "expected" : "not expected");
4498 if (the_low_target
.prepare_to_resume
!= NULL
)
4499 the_low_target
.prepare_to_resume (lwp
);
4501 regcache_invalidate_thread (thread
);
4503 lwp
->stepping
= step
;
4505 ptrace_request
= PTRACE_SINGLESTEP
;
4506 else if (gdb_catching_syscalls_p (lwp
))
4507 ptrace_request
= PTRACE_SYSCALL
;
4509 ptrace_request
= PTRACE_CONT
;
4510 ptrace (ptrace_request
,
4512 (PTRACE_TYPE_ARG3
) 0,
4513 /* Coerce to a uintptr_t first to avoid potential gcc warning
4514 of coercing an 8 byte integer to a 4 byte pointer. */
4515 (PTRACE_TYPE_ARG4
) (uintptr_t) signal
);
4517 current_thread
= saved_thread
;
4519 perror_with_name ("resuming thread");
4521 /* Successfully resumed. Clear state that no longer makes sense,
4522 and mark the LWP as running. Must not do this before resuming
4523 otherwise if that fails other code will be confused. E.g., we'd
4524 later try to stop the LWP and hang forever waiting for a stop
4525 status. Note that we must not throw after this is cleared,
4526 otherwise handle_zombie_lwp_error would get confused. */
4528 lwp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4531 /* Called when we try to resume a stopped LWP and that errors out. If
4532 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4533 or about to become), discard the error, clear any pending status
4534 the LWP may have, and return true (we'll collect the exit status
4535 soon enough). Otherwise, return false. */
4538 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
4540 struct thread_info
*thread
= get_lwp_thread (lp
);
4542 /* If we get an error after resuming the LWP successfully, we'd
4543 confuse !T state for the LWP being gone. */
4544 gdb_assert (lp
->stopped
);
4546 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4547 because even if ptrace failed with ESRCH, the tracee may be "not
4548 yet fully dead", but already refusing ptrace requests. In that
4549 case the tracee has 'R (Running)' state for a little bit
4550 (observed in Linux 3.18). See also the note on ESRCH in the
4551 ptrace(2) man page. Instead, check whether the LWP has any state
4552 other than ptrace-stopped. */
4554 /* Don't assume anything if /proc/PID/status can't be read. */
4555 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread
)) == 0)
4557 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4558 lp
->status_pending_p
= 0;
4564 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
4565 disappears while we try to resume it. */
4568 linux_resume_one_lwp (struct lwp_info
*lwp
,
4569 int step
, int signal
, siginfo_t
*info
)
4573 linux_resume_one_lwp_throw (lwp
, step
, signal
, info
);
4575 CATCH (ex
, RETURN_MASK_ERROR
)
4577 if (!check_ptrace_stopped_lwp_gone (lwp
))
4578 throw_exception (ex
);
4583 struct thread_resume_array
4585 struct thread_resume
*resume
;
4589 /* This function is called once per thread via find_inferior.
4590 ARG is a pointer to a thread_resume_array struct.
4591 We look up the thread specified by ENTRY in ARG, and mark the thread
4592 with a pointer to the appropriate resume request.
4594 This algorithm is O(threads * resume elements), but resume elements
4595 is small (and will remain small at least until GDB supports thread
4599 linux_set_resume_request (struct inferior_list_entry
*entry
, void *arg
)
4601 struct thread_info
*thread
= (struct thread_info
*) entry
;
4602 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4604 struct thread_resume_array
*r
;
4606 r
= (struct thread_resume_array
*) arg
;
4608 for (ndx
= 0; ndx
< r
->n
; ndx
++)
4610 ptid_t ptid
= r
->resume
[ndx
].thread
;
4611 if (ptid_equal (ptid
, minus_one_ptid
)
4612 || ptid_equal (ptid
, entry
->id
)
4613 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4615 || (ptid_get_pid (ptid
) == pid_of (thread
)
4616 && (ptid_is_pid (ptid
)
4617 || ptid_get_lwp (ptid
) == -1)))
4619 if (r
->resume
[ndx
].kind
== resume_stop
4620 && thread
->last_resume_kind
== resume_stop
)
4623 debug_printf ("already %s LWP %ld at GDB's request\n",
4624 (thread
->last_status
.kind
4625 == TARGET_WAITKIND_STOPPED
)
4633 lwp
->resume
= &r
->resume
[ndx
];
4634 thread
->last_resume_kind
= lwp
->resume
->kind
;
4636 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
4637 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
4639 /* If we had a deferred signal to report, dequeue one now.
4640 This can happen if LWP gets more than one signal while
4641 trying to get out of a jump pad. */
4643 && !lwp
->status_pending_p
4644 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
4646 lwp
->status_pending_p
= 1;
4649 debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
4650 "leaving status pending.\n",
4651 WSTOPSIG (lwp
->status_pending
),
4659 /* No resume action for this thread. */
4665 /* find_inferior callback for linux_resume.
4666 Set *FLAG_P if this lwp has an interesting status pending. */
4669 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
4671 struct thread_info
*thread
= (struct thread_info
*) entry
;
4672 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4674 /* LWPs which will not be resumed are not interesting, because
4675 we might not wait for them next time through linux_wait. */
4676 if (lwp
->resume
== NULL
)
4679 if (thread_still_has_status_pending_p (thread
))
4680 * (int *) flag_p
= 1;
4685 /* Return 1 if this lwp that GDB wants running is stopped at an
4686 internal breakpoint that we need to step over. It assumes that any
4687 required STOP_PC adjustment has already been propagated to the
4688 inferior's regcache. */
4691 need_step_over_p (struct inferior_list_entry
*entry
, void *dummy
)
4693 struct thread_info
*thread
= (struct thread_info
*) entry
;
4694 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4695 struct thread_info
*saved_thread
;
4697 struct process_info
*proc
= get_thread_process (thread
);
4699 /* GDBserver is skipping the extra traps from the wrapper program,
4700 don't have to do step over. */
4701 if (proc
->tdesc
== NULL
)
4704 /* LWPs which will not be resumed are not interesting, because we
4705 might not wait for them next time through linux_wait. */
4710 debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
4715 if (thread
->last_resume_kind
== resume_stop
)
4718 debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
4724 gdb_assert (lwp
->suspended
>= 0);
4729 debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
4734 if (lwp
->status_pending_p
)
4737 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4743 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4747 /* If the PC has changed since we stopped, then don't do anything,
4748 and let the breakpoint/tracepoint be hit. This happens if, for
4749 instance, GDB handled the decr_pc_after_break subtraction itself,
4750 GDB is OOL stepping this thread, or the user has issued a "jump"
4751 command, or poked thread's registers herself. */
4752 if (pc
!= lwp
->stop_pc
)
4755 debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4756 "Old stop_pc was 0x%s, PC is now 0x%s\n",
4758 paddress (lwp
->stop_pc
), paddress (pc
));
4762 /* On software single step target, resume the inferior with signal
4763 rather than stepping over. */
4764 if (can_software_single_step ()
4765 && lwp
->pending_signals
!= NULL
4766 && lwp_signal_can_be_delivered (lwp
))
4769 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4776 saved_thread
= current_thread
;
4777 current_thread
= thread
;
4779 /* We can only step over breakpoints we know about. */
4780 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
4782 /* Don't step over a breakpoint that GDB expects to hit
4783 though. If the condition is being evaluated on the target's side
4784 and it evaluate to false, step over this breakpoint as well. */
4785 if (gdb_breakpoint_here (pc
)
4786 && gdb_condition_true_at_breakpoint (pc
)
4787 && gdb_no_commands_at_breakpoint (pc
))
4790 debug_printf ("Need step over [LWP %ld]? yes, but found"
4791 " GDB breakpoint at 0x%s; skipping step over\n",
4792 lwpid_of (thread
), paddress (pc
));
4794 current_thread
= saved_thread
;
4800 debug_printf ("Need step over [LWP %ld]? yes, "
4801 "found breakpoint at 0x%s\n",
4802 lwpid_of (thread
), paddress (pc
));
4804 /* We've found an lwp that needs stepping over --- return 1 so
4805 that find_inferior stops looking. */
4806 current_thread
= saved_thread
;
4812 current_thread
= saved_thread
;
4815 debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
4817 lwpid_of (thread
), paddress (pc
));
4822 /* Start a step-over operation on LWP. When LWP stopped at a
4823 breakpoint, to make progress, we need to remove the breakpoint out
4824 of the way. If we let other threads run while we do that, they may
4825 pass by the breakpoint location and miss hitting it. To avoid
4826 that, a step-over momentarily stops all threads while LWP is
4827 single-stepped by either hardware or software while the breakpoint
4828 is temporarily uninserted from the inferior. When the single-step
4829 finishes, we reinsert the breakpoint, and let all threads that are
4830 supposed to be running, run again. */
4833 start_step_over (struct lwp_info
*lwp
)
4835 struct thread_info
*thread
= get_lwp_thread (lwp
);
4836 struct thread_info
*saved_thread
;
4841 debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
4844 stop_all_lwps (1, lwp
);
4846 if (lwp
->suspended
!= 0)
4848 internal_error (__FILE__
, __LINE__
,
4849 "LWP %ld suspended=%d\n", lwpid_of (thread
),
4854 debug_printf ("Done stopping all threads for step-over.\n");
4856 /* Note, we should always reach here with an already adjusted PC,
4857 either by GDB (if we're resuming due to GDB's request), or by our
4858 caller, if we just finished handling an internal breakpoint GDB
4859 shouldn't care about. */
4862 saved_thread
= current_thread
;
4863 current_thread
= thread
;
4865 lwp
->bp_reinsert
= pc
;
4866 uninsert_breakpoints_at (pc
);
4867 uninsert_fast_tracepoint_jumps_at (pc
);
4869 step
= single_step (lwp
);
4871 current_thread
= saved_thread
;
4873 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
4875 /* Require next event from this LWP. */
4876 step_over_bkpt
= thread
->entry
.id
;
4880 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
4881 start_step_over, if still there, and delete any reinsert
4882 breakpoints we've set, on non hardware single-step targets. */
4885 finish_step_over (struct lwp_info
*lwp
)
4887 if (lwp
->bp_reinsert
!= 0)
4889 struct thread_info
*saved_thread
= current_thread
;
4892 debug_printf ("Finished step over.\n");
4894 current_thread
= get_lwp_thread (lwp
);
4896 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4897 may be no breakpoint to reinsert there by now. */
4898 reinsert_breakpoints_at (lwp
->bp_reinsert
);
4899 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
4901 lwp
->bp_reinsert
= 0;
4903 /* Delete any software-single-step reinsert breakpoints. No
4904 longer needed. We don't have to worry about other threads
4905 hitting this trap, and later not being able to explain it,
4906 because we were stepping over a breakpoint, and we hold all
4907 threads but LWP stopped while doing that. */
4908 if (!can_hardware_single_step ())
4910 gdb_assert (has_reinsert_breakpoints (current_thread
));
4911 delete_reinsert_breakpoints (current_thread
);
4914 step_over_bkpt
= null_ptid
;
4915 current_thread
= saved_thread
;
4922 /* If there's a step over in progress, wait until all threads stop
4923 (that is, until the stepping thread finishes its step), and
4924 unsuspend all lwps. The stepping thread ends with its status
4925 pending, which is processed later when we get back to processing
4929 complete_ongoing_step_over (void)
4931 if (!ptid_equal (step_over_bkpt
, null_ptid
))
4933 struct lwp_info
*lwp
;
4938 debug_printf ("detach: step over in progress, finish it first\n");
4940 /* Passing NULL_PTID as filter indicates we want all events to
4941 be left pending. Eventually this returns when there are no
4942 unwaited-for children left. */
4943 ret
= linux_wait_for_event_filtered (minus_one_ptid
, null_ptid
,
4945 gdb_assert (ret
== -1);
4947 lwp
= find_lwp_pid (step_over_bkpt
);
4949 finish_step_over (lwp
);
4950 step_over_bkpt
= null_ptid
;
4951 unsuspend_all_lwps (lwp
);
4955 /* This function is called once per thread. We check the thread's resume
4956 request, which will tell us whether to resume, step, or leave the thread
4957 stopped; and what signal, if any, it should be sent.
4959 For threads which we aren't explicitly told otherwise, we preserve
4960 the stepping flag; this is used for stepping over gdbserver-placed
4963 If pending_flags was set in any thread, we queue any needed
4964 signals, since we won't actually resume. We already have a pending
4965 event to report, so we don't need to preserve any step requests;
4966 they should be re-issued if necessary. */
4969 linux_resume_one_thread (struct inferior_list_entry
*entry
, void *arg
)
4971 struct thread_info
*thread
= (struct thread_info
*) entry
;
4972 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4973 int leave_all_stopped
= * (int *) arg
;
4976 if (lwp
->resume
== NULL
)
4979 if (lwp
->resume
->kind
== resume_stop
)
4982 debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread
));
4987 debug_printf ("stopping LWP %ld\n", lwpid_of (thread
));
4989 /* Stop the thread, and wait for the event asynchronously,
4990 through the event loop. */
4996 debug_printf ("already stopped LWP %ld\n",
4999 /* The LWP may have been stopped in an internal event that
5000 was not meant to be notified back to GDB (e.g., gdbserver
5001 breakpoint), so we should be reporting a stop event in
5004 /* If the thread already has a pending SIGSTOP, this is a
5005 no-op. Otherwise, something later will presumably resume
5006 the thread and this will cause it to cancel any pending
5007 operation, due to last_resume_kind == resume_stop. If
5008 the thread already has a pending status to report, we
5009 will still report it the next time we wait - see
5010 status_pending_p_callback. */
5012 /* If we already have a pending signal to report, then
5013 there's no need to queue a SIGSTOP, as this means we're
5014 midway through moving the LWP out of the jumppad, and we
5015 will report the pending signal as soon as that is
5017 if (lwp
->pending_signals_to_report
== NULL
)
5021 /* For stop requests, we're done. */
5023 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
5027 /* If this thread which is about to be resumed has a pending status,
5028 then don't resume it - we can just report the pending status.
5029 Likewise if it is suspended, because e.g., another thread is
5030 stepping past a breakpoint. Make sure to queue any signals that
5031 would otherwise be sent. In all-stop mode, we do this decision
5032 based on if *any* thread has a pending status. If there's a
5033 thread that needs the step-over-breakpoint dance, then don't
5034 resume any other thread but that particular one. */
5035 leave_pending
= (lwp
->suspended
5036 || lwp
->status_pending_p
5037 || leave_all_stopped
);
5039 /* If we have a new signal, enqueue the signal. */
5040 if (lwp
->resume
->sig
!= 0)
5042 siginfo_t info
, *info_p
;
5044 /* If this is the same signal we were previously stopped by,
5045 make sure to queue its siginfo. */
5046 if (WIFSTOPPED (lwp
->last_status
)
5047 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
5048 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
),
5049 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
5054 enqueue_pending_signal (lwp
, lwp
->resume
->sig
, info_p
);
5060 debug_printf ("resuming LWP %ld\n", lwpid_of (thread
));
5062 proceed_one_lwp (entry
, NULL
);
5067 debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread
));
5070 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
5076 linux_resume (struct thread_resume
*resume_info
, size_t n
)
5078 struct thread_resume_array array
= { resume_info
, n
};
5079 struct thread_info
*need_step_over
= NULL
;
5081 int leave_all_stopped
;
5086 debug_printf ("linux_resume:\n");
5089 find_inferior (&all_threads
, linux_set_resume_request
, &array
);
5091 /* If there is a thread which would otherwise be resumed, which has
5092 a pending status, then don't resume any threads - we can just
5093 report the pending status. Make sure to queue any signals that
5094 would otherwise be sent. In non-stop mode, we'll apply this
5095 logic to each thread individually. We consume all pending events
5096 before considering to start a step-over (in all-stop). */
5099 find_inferior (&all_threads
, resume_status_pending_p
, &any_pending
);
5101 /* If there is a thread which would otherwise be resumed, which is
5102 stopped at a breakpoint that needs stepping over, then don't
5103 resume any threads - have it step over the breakpoint with all
5104 other threads stopped, then resume all threads again. Make sure
5105 to queue any signals that would otherwise be delivered or
5107 if (!any_pending
&& supports_breakpoints ())
5109 = (struct thread_info
*) find_inferior (&all_threads
,
5110 need_step_over_p
, NULL
);
5112 leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
5116 if (need_step_over
!= NULL
)
5117 debug_printf ("Not resuming all, need step over\n");
5118 else if (any_pending
)
5119 debug_printf ("Not resuming, all-stop and found "
5120 "an LWP with pending status\n");
5122 debug_printf ("Resuming, no pending status or step over needed\n");
5125 /* Even if we're leaving threads stopped, queue all signals we'd
5126 otherwise deliver. */
5127 find_inferior (&all_threads
, linux_resume_one_thread
, &leave_all_stopped
);
5130 start_step_over (get_thread_lwp (need_step_over
));
5134 debug_printf ("linux_resume done\n");
5138 /* We may have events that were pending that can/should be sent to
5139 the client now. Trigger a linux_wait call. */
5140 if (target_is_async_p ())
5144 /* This function is called once per thread. We check the thread's
5145 last resume request, which will tell us whether to resume, step, or
5146 leave the thread stopped. Any signal the client requested to be
5147 delivered has already been enqueued at this point.
5149 If any thread that GDB wants running is stopped at an internal
5150 breakpoint that needs stepping over, we start a step-over operation
5151 on that particular thread, and leave all others stopped. */
5154 proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
5156 struct thread_info
*thread
= (struct thread_info
*) entry
;
5157 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5164 debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread
));
5169 debug_printf (" LWP %ld already running\n", lwpid_of (thread
));
5173 if (thread
->last_resume_kind
== resume_stop
5174 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
5177 debug_printf (" client wants LWP to remain %ld stopped\n",
5182 if (lwp
->status_pending_p
)
5185 debug_printf (" LWP %ld has pending status, leaving stopped\n",
5190 gdb_assert (lwp
->suspended
>= 0);
5195 debug_printf (" LWP %ld is suspended\n", lwpid_of (thread
));
5199 if (thread
->last_resume_kind
== resume_stop
5200 && lwp
->pending_signals_to_report
== NULL
5201 && lwp
->collecting_fast_tracepoint
== 0)
5203 /* We haven't reported this LWP as stopped yet (otherwise, the
5204 last_status.kind check above would catch it, and we wouldn't
5205 reach here. This LWP may have been momentarily paused by a
5206 stop_all_lwps call while handling for example, another LWP's
5207 step-over. In that case, the pending expected SIGSTOP signal
5208 that was queued at vCont;t handling time will have already
5209 been consumed by wait_for_sigstop, and so we need to requeue
5210 another one here. Note that if the LWP already has a SIGSTOP
5211 pending, this is a no-op. */
5214 debug_printf ("Client wants LWP %ld to stop. "
5215 "Making sure it has a SIGSTOP pending\n",
5221 if (thread
->last_resume_kind
== resume_step
)
5224 debug_printf (" stepping LWP %ld, client wants it stepping\n",
5227 /* If resume_step is requested by GDB, install reinsert
5228 breakpoints when the thread is about to be actually resumed if
5229 the reinsert breakpoints weren't removed. */
5230 if (can_software_single_step () && !has_reinsert_breakpoints (thread
))
5231 install_software_single_step_breakpoints (lwp
);
5233 step
= maybe_hw_step (thread
);
5235 else if (lwp
->bp_reinsert
!= 0)
5238 debug_printf (" stepping LWP %ld, reinsert set\n",
5241 step
= maybe_hw_step (thread
);
5246 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
5251 unsuspend_and_proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
5253 struct thread_info
*thread
= (struct thread_info
*) entry
;
5254 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5259 lwp_suspended_decr (lwp
);
5261 return proceed_one_lwp (entry
, except
);
5264 /* When we finish a step-over, set threads running again. If there's
5265 another thread that may need a step-over, now's the time to start
5266 it. Eventually, we'll move all threads past their breakpoints. */
5269 proceed_all_lwps (void)
5271 struct thread_info
*need_step_over
;
5273 /* If there is a thread which would otherwise be resumed, which is
5274 stopped at a breakpoint that needs stepping over, then don't
5275 resume any threads - have it step over the breakpoint with all
5276 other threads stopped, then resume all threads again. */
5278 if (supports_breakpoints ())
5281 = (struct thread_info
*) find_inferior (&all_threads
,
5282 need_step_over_p
, NULL
);
5284 if (need_step_over
!= NULL
)
5287 debug_printf ("proceed_all_lwps: found "
5288 "thread %ld needing a step-over\n",
5289 lwpid_of (need_step_over
));
5291 start_step_over (get_thread_lwp (need_step_over
));
5297 debug_printf ("Proceeding, no step-over needed\n");
5299 find_inferior (&all_threads
, proceed_one_lwp
, NULL
);
5302 /* Stopped LWPs that the client wanted to be running, that don't have
5303 pending statuses, are set to run again, except for EXCEPT, if not
5304 NULL. This undoes a stop_all_lwps call. */
5307 unstop_all_lwps (int unsuspend
, struct lwp_info
*except
)
5313 debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
5314 lwpid_of (get_lwp_thread (except
)));
5316 debug_printf ("unstopping all lwps\n");
5320 find_inferior (&all_threads
, unsuspend_and_proceed_one_lwp
, except
);
5322 find_inferior (&all_threads
, proceed_one_lwp
, except
);
5326 debug_printf ("unstop_all_lwps done\n");
5332 #ifdef HAVE_LINUX_REGSETS
5334 #define use_linux_regsets 1
5336 /* Returns true if REGSET has been disabled. */
5339 regset_disabled (struct regsets_info
*info
, struct regset_info
*regset
)
5341 return (info
->disabled_regsets
!= NULL
5342 && info
->disabled_regsets
[regset
- info
->regsets
]);
5345 /* Disable REGSET. */
5348 disable_regset (struct regsets_info
*info
, struct regset_info
*regset
)
5352 dr_offset
= regset
- info
->regsets
;
5353 if (info
->disabled_regsets
== NULL
)
5354 info
->disabled_regsets
= (char *) xcalloc (1, info
->num_regsets
);
5355 info
->disabled_regsets
[dr_offset
] = 1;
5359 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
5360 struct regcache
*regcache
)
5362 struct regset_info
*regset
;
5363 int saw_general_regs
= 0;
5367 pid
= lwpid_of (current_thread
);
5368 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5373 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
5376 buf
= xmalloc (regset
->size
);
5378 nt_type
= regset
->nt_type
;
5382 iov
.iov_len
= regset
->size
;
5383 data
= (void *) &iov
;
5389 res
= ptrace (regset
->get_request
, pid
,
5390 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5392 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5398 /* If we get EIO on a regset, do not try it again for
5399 this process mode. */
5400 disable_regset (regsets_info
, regset
);
5402 else if (errno
== ENODATA
)
5404 /* ENODATA may be returned if the regset is currently
5405 not "active". This can happen in normal operation,
5406 so suppress the warning in this case. */
5411 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
5418 if (regset
->type
== GENERAL_REGS
)
5419 saw_general_regs
= 1;
5420 regset
->store_function (regcache
, buf
);
5424 if (saw_general_regs
)
5431 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
5432 struct regcache
*regcache
)
5434 struct regset_info
*regset
;
5435 int saw_general_regs
= 0;
5439 pid
= lwpid_of (current_thread
);
5440 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5445 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
)
5446 || regset
->fill_function
== NULL
)
5449 buf
= xmalloc (regset
->size
);
5451 /* First fill the buffer with the current register set contents,
5452 in case there are any items in the kernel's regset that are
5453 not in gdbserver's regcache. */
5455 nt_type
= regset
->nt_type
;
5459 iov
.iov_len
= regset
->size
;
5460 data
= (void *) &iov
;
5466 res
= ptrace (regset
->get_request
, pid
,
5467 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5469 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5474 /* Then overlay our cached registers on that. */
5475 regset
->fill_function (regcache
, buf
);
5477 /* Only now do we write the register set. */
5479 res
= ptrace (regset
->set_request
, pid
,
5480 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5482 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
5490 /* If we get EIO on a regset, do not try it again for
5491 this process mode. */
5492 disable_regset (regsets_info
, regset
);
5494 else if (errno
== ESRCH
)
5496 /* At this point, ESRCH should mean the process is
5497 already gone, in which case we simply ignore attempts
5498 to change its registers. See also the related
5499 comment in linux_resume_one_lwp. */
5505 perror ("Warning: ptrace(regsets_store_inferior_registers)");
5508 else if (regset
->type
== GENERAL_REGS
)
5509 saw_general_regs
= 1;
5512 if (saw_general_regs
)
5518 #else /* !HAVE_LINUX_REGSETS */
5520 #define use_linux_regsets 0
5521 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5522 #define regsets_store_inferior_registers(regsets_info, regcache) 1
5526 /* Return 1 if register REGNO is supported by one of the regset ptrace
5527 calls or 0 if it has to be transferred individually. */
5530 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
5532 unsigned char mask
= 1 << (regno
% 8);
5533 size_t index
= regno
/ 8;
5535 return (use_linux_regsets
5536 && (regs_info
->regset_bitmap
== NULL
5537 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
5540 #ifdef HAVE_LINUX_USRREGS
5543 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
5547 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
5548 error ("Invalid register number %d.", regnum
);
5550 addr
= usrregs
->regmap
[regnum
];
5555 /* Fetch one register. */
5557 fetch_register (const struct usrregs_info
*usrregs
,
5558 struct regcache
*regcache
, int regno
)
5565 if (regno
>= usrregs
->num_regs
)
5567 if ((*the_low_target
.cannot_fetch_register
) (regno
))
5570 regaddr
= register_addr (usrregs
, regno
);
5574 size
= ((register_size (regcache
->tdesc
, regno
)
5575 + sizeof (PTRACE_XFER_TYPE
) - 1)
5576 & -sizeof (PTRACE_XFER_TYPE
));
5577 buf
= (char *) alloca (size
);
5579 pid
= lwpid_of (current_thread
);
5580 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5583 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
5584 ptrace (PTRACE_PEEKUSER
, pid
,
5585 /* Coerce to a uintptr_t first to avoid potential gcc warning
5586 of coercing an 8 byte integer to a 4 byte pointer. */
5587 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
, (PTRACE_TYPE_ARG4
) 0);
5588 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5590 error ("reading register %d: %s", regno
, strerror (errno
));
5593 if (the_low_target
.supply_ptrace_register
)
5594 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
5596 supply_register (regcache
, regno
, buf
);
5599 /* Store one register. */
5601 store_register (const struct usrregs_info
*usrregs
,
5602 struct regcache
*regcache
, int regno
)
5609 if (regno
>= usrregs
->num_regs
)
5611 if ((*the_low_target
.cannot_store_register
) (regno
))
5614 regaddr
= register_addr (usrregs
, regno
);
5618 size
= ((register_size (regcache
->tdesc
, regno
)
5619 + sizeof (PTRACE_XFER_TYPE
) - 1)
5620 & -sizeof (PTRACE_XFER_TYPE
));
5621 buf
= (char *) alloca (size
);
5622 memset (buf
, 0, size
);
5624 if (the_low_target
.collect_ptrace_register
)
5625 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
5627 collect_register (regcache
, regno
, buf
);
5629 pid
= lwpid_of (current_thread
);
5630 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5633 ptrace (PTRACE_POKEUSER
, pid
,
5634 /* Coerce to a uintptr_t first to avoid potential gcc warning
5635 about coercing an 8 byte integer to a 4 byte pointer. */
5636 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
,
5637 (PTRACE_TYPE_ARG4
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
5640 /* At this point, ESRCH should mean the process is
5641 already gone, in which case we simply ignore attempts
5642 to change its registers. See also the related
5643 comment in linux_resume_one_lwp. */
5647 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
5648 error ("writing register %d: %s", regno
, strerror (errno
));
5650 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5654 /* Fetch all registers, or just one, from the child process.
5655 If REGNO is -1, do this for all registers, skipping any that are
5656 assumed to have been retrieved by regsets_fetch_inferior_registers,
5657 unless ALL is non-zero.
5658 Otherwise, REGNO specifies which register (so we can save time). */
5660 usr_fetch_inferior_registers (const struct regs_info
*regs_info
,
5661 struct regcache
*regcache
, int regno
, int all
)
5663 struct usrregs_info
*usr
= regs_info
->usrregs
;
5667 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5668 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5669 fetch_register (usr
, regcache
, regno
);
5672 fetch_register (usr
, regcache
, regno
);
5675 /* Store our register values back into the inferior.
5676 If REGNO is -1, do this for all registers, skipping any that are
5677 assumed to have been saved by regsets_store_inferior_registers,
5678 unless ALL is non-zero.
5679 Otherwise, REGNO specifies which register (so we can save time). */
5681 usr_store_inferior_registers (const struct regs_info
*regs_info
,
5682 struct regcache
*regcache
, int regno
, int all
)
5684 struct usrregs_info
*usr
= regs_info
->usrregs
;
5688 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5689 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5690 store_register (usr
, regcache
, regno
);
5693 store_register (usr
, regcache
, regno
);
5696 #else /* !HAVE_LINUX_USRREGS */
5698 #define usr_fetch_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
5699 #define usr_store_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
5705 linux_fetch_registers (struct regcache
*regcache
, int regno
)
5709 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
5713 if (the_low_target
.fetch_register
!= NULL
5714 && regs_info
->usrregs
!= NULL
)
5715 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
5716 (*the_low_target
.fetch_register
) (regcache
, regno
);
5718 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
5719 if (regs_info
->usrregs
!= NULL
)
5720 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
5724 if (the_low_target
.fetch_register
!= NULL
5725 && (*the_low_target
.fetch_register
) (regcache
, regno
))
5728 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5730 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
5732 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5733 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
5738 linux_store_registers (struct regcache
*regcache
, int regno
)
5742 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
5746 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5748 if (regs_info
->usrregs
!= NULL
)
5749 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
5753 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5755 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5757 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5758 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
5763 /* Copy LEN bytes from inferior's memory starting at MEMADDR
5764 to debugger memory starting at MYADDR. */
5767 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
5769 int pid
= lwpid_of (current_thread
);
5770 register PTRACE_XFER_TYPE
*buffer
;
5771 register CORE_ADDR addr
;
5778 /* Try using /proc. Don't bother for one word. */
5779 if (len
>= 3 * sizeof (long))
5783 /* We could keep this file open and cache it - possibly one per
5784 thread. That requires some juggling, but is even faster. */
5785 sprintf (filename
, "/proc/%d/mem", pid
);
5786 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
5790 /* If pread64 is available, use it. It's faster if the kernel
5791 supports it (only one syscall), and it's 64-bit safe even on
5792 32-bit platforms (for instance, SPARC debugging a SPARC64
5795 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
5798 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
5799 bytes
= read (fd
, myaddr
, len
);
5806 /* Some data was read, we'll try to get the rest with ptrace. */
5816 /* Round starting address down to longword boundary. */
5817 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5818 /* Round ending address up; get number of longwords that makes. */
5819 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5820 / sizeof (PTRACE_XFER_TYPE
));
5821 /* Allocate buffer of that many longwords. */
5822 buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5824 /* Read all the longwords */
5826 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5828 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5829 about coercing an 8 byte integer to a 4 byte pointer. */
5830 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
5831 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5832 (PTRACE_TYPE_ARG4
) 0);
5838 /* Copy appropriate bytes out of the buffer. */
5841 i
*= sizeof (PTRACE_XFER_TYPE
);
5842 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
5844 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5851 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5852 memory at MEMADDR. On failure (cannot write to the inferior)
5853 returns the value of errno. Always succeeds if LEN is zero. */
5856 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
5859 /* Round starting address down to longword boundary. */
5860 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5861 /* Round ending address up; get number of longwords that makes. */
5863 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5864 / sizeof (PTRACE_XFER_TYPE
);
5866 /* Allocate buffer of that many longwords. */
5867 register PTRACE_XFER_TYPE
*buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5869 int pid
= lwpid_of (current_thread
);
5873 /* Zero length write always succeeds. */
5879 /* Dump up to four bytes. */
5880 char str
[4 * 2 + 1];
5882 int dump
= len
< 4 ? len
: 4;
5884 for (i
= 0; i
< dump
; i
++)
5886 sprintf (p
, "%02x", myaddr
[i
]);
5891 debug_printf ("Writing %s to 0x%08lx in process %d\n",
5892 str
, (long) memaddr
, pid
);
5895 /* Fill start and end extra bytes of buffer with existing memory data. */
5898 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5899 about coercing an 8 byte integer to a 4 byte pointer. */
5900 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
5901 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5902 (PTRACE_TYPE_ARG4
) 0);
5910 = ptrace (PTRACE_PEEKTEXT
, pid
,
5911 /* Coerce to a uintptr_t first to avoid potential gcc warning
5912 about coercing an 8 byte integer to a 4 byte pointer. */
5913 (PTRACE_TYPE_ARG3
) (uintptr_t) (addr
+ (count
- 1)
5914 * sizeof (PTRACE_XFER_TYPE
)),
5915 (PTRACE_TYPE_ARG4
) 0);
5920 /* Copy data to be written over corresponding part of buffer. */
5922 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5925 /* Write the entire buffer. */
5927 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5930 ptrace (PTRACE_POKETEXT
, pid
,
5931 /* Coerce to a uintptr_t first to avoid potential gcc warning
5932 about coercing an 8 byte integer to a 4 byte pointer. */
5933 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5934 (PTRACE_TYPE_ARG4
) buffer
[i
]);
5943 linux_look_up_symbols (void)
5945 #ifdef USE_THREAD_DB
5946 struct process_info
*proc
= current_process ();
5948 if (proc
->priv
->thread_db
!= NULL
)
5956 linux_request_interrupt (void)
5958 extern unsigned long signal_pid
;
5960 /* Send a SIGINT to the process group. This acts just like the user
5961 typed a ^C on the controlling terminal. */
5962 kill (-signal_pid
, SIGINT
);
5965 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5966 to debugger memory starting at MYADDR. */
5969 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
5971 char filename
[PATH_MAX
];
5973 int pid
= lwpid_of (current_thread
);
5975 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5977 fd
= open (filename
, O_RDONLY
);
5981 if (offset
!= (CORE_ADDR
) 0
5982 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5985 n
= read (fd
, myaddr
, len
);
5992 /* These breakpoint and watchpoint related wrapper functions simply
5993 pass on the function call if the target has registered a
5994 corresponding function. */
5997 linux_supports_z_point_type (char z_type
)
5999 return (the_low_target
.supports_z_point_type
!= NULL
6000 && the_low_target
.supports_z_point_type (z_type
));
6004 linux_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
6005 int size
, struct raw_breakpoint
*bp
)
6007 if (type
== raw_bkpt_type_sw
)
6008 return insert_memory_breakpoint (bp
);
6009 else if (the_low_target
.insert_point
!= NULL
)
6010 return the_low_target
.insert_point (type
, addr
, size
, bp
);
6012 /* Unsupported (see target.h). */
6017 linux_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
6018 int size
, struct raw_breakpoint
*bp
)
6020 if (type
== raw_bkpt_type_sw
)
6021 return remove_memory_breakpoint (bp
);
6022 else if (the_low_target
.remove_point
!= NULL
)
6023 return the_low_target
.remove_point (type
, addr
, size
, bp
);
6025 /* Unsupported (see target.h). */
6029 /* Implement the to_stopped_by_sw_breakpoint target_ops
6033 linux_stopped_by_sw_breakpoint (void)
6035 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
6037 return (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
);
6040 /* Implement the to_supports_stopped_by_sw_breakpoint target_ops
6044 linux_supports_stopped_by_sw_breakpoint (void)
6046 return USE_SIGTRAP_SIGINFO
;
6049 /* Implement the to_stopped_by_hw_breakpoint target_ops
6053 linux_stopped_by_hw_breakpoint (void)
6055 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
6057 return (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
);
6060 /* Implement the to_supports_stopped_by_hw_breakpoint target_ops
6064 linux_supports_stopped_by_hw_breakpoint (void)
6066 return USE_SIGTRAP_SIGINFO
;
6069 /* Implement the supports_hardware_single_step target_ops method. */
6072 linux_supports_hardware_single_step (void)
6074 return can_hardware_single_step ();
6078 linux_supports_software_single_step (void)
6080 return can_software_single_step ();
6084 linux_stopped_by_watchpoint (void)
6086 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
6088 return lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
6092 linux_stopped_data_address (void)
6094 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
6096 return lwp
->stopped_data_address
;
6099 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
6100 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
6101 && defined(PT_TEXT_END_ADDR)
6103 /* This is only used for targets that define PT_TEXT_ADDR,
6104 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
6105 the target has different ways of acquiring this information, like
6108 /* Under uClinux, programs are loaded at non-zero offsets, which we need
6109 to tell gdb about. */
6112 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
6114 unsigned long text
, text_end
, data
;
6115 int pid
= lwpid_of (current_thread
);
6119 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_ADDR
,
6120 (PTRACE_TYPE_ARG4
) 0);
6121 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_END_ADDR
,
6122 (PTRACE_TYPE_ARG4
) 0);
6123 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_DATA_ADDR
,
6124 (PTRACE_TYPE_ARG4
) 0);
6128 /* Both text and data offsets produced at compile-time (and so
6129 used by gdb) are relative to the beginning of the program,
6130 with the data segment immediately following the text segment.
6131 However, the actual runtime layout in memory may put the data
6132 somewhere else, so when we send gdb a data base-address, we
6133 use the real data base address and subtract the compile-time
6134 data base-address from it (which is just the length of the
6135 text segment). BSS immediately follows data in both
6138 *data_p
= data
- (text_end
- text
);
6147 linux_qxfer_osdata (const char *annex
,
6148 unsigned char *readbuf
, unsigned const char *writebuf
,
6149 CORE_ADDR offset
, int len
)
6151 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
6154 /* Convert a native/host siginfo object, into/from the siginfo in the
6155 layout of the inferiors' architecture. */
6158 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
6162 if (the_low_target
.siginfo_fixup
!= NULL
)
6163 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
6165 /* If there was no callback, or the callback didn't do anything,
6166 then just do a straight memcpy. */
6170 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
6172 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
6177 linux_xfer_siginfo (const char *annex
, unsigned char *readbuf
,
6178 unsigned const char *writebuf
, CORE_ADDR offset
, int len
)
6182 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
6184 if (current_thread
== NULL
)
6187 pid
= lwpid_of (current_thread
);
6190 debug_printf ("%s siginfo for lwp %d.\n",
6191 readbuf
!= NULL
? "Reading" : "Writing",
6194 if (offset
>= sizeof (siginfo
))
6197 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6200 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
6201 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
6202 inferior with a 64-bit GDBSERVER should look the same as debugging it
6203 with a 32-bit GDBSERVER, we need to convert it. */
6204 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
6206 if (offset
+ len
> sizeof (siginfo
))
6207 len
= sizeof (siginfo
) - offset
;
6209 if (readbuf
!= NULL
)
6210 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
6213 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
6215 /* Convert back to ptrace layout before flushing it out. */
6216 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
6218 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6225 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
6226 so we notice when children change state; as the handler for the
6227 sigsuspend in my_waitpid. */
6230 sigchld_handler (int signo
)
6232 int old_errno
= errno
;
6238 /* fprintf is not async-signal-safe, so call write
6240 if (write (2, "sigchld_handler\n",
6241 sizeof ("sigchld_handler\n") - 1) < 0)
6242 break; /* just ignore */
6246 if (target_is_async_p ())
6247 async_file_mark (); /* trigger a linux_wait */
6253 linux_supports_non_stop (void)
6259 linux_async (int enable
)
6261 int previous
= target_is_async_p ();
6264 debug_printf ("linux_async (%d), previous=%d\n",
6267 if (previous
!= enable
)
6270 sigemptyset (&mask
);
6271 sigaddset (&mask
, SIGCHLD
);
6273 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
6277 if (pipe (linux_event_pipe
) == -1)
6279 linux_event_pipe
[0] = -1;
6280 linux_event_pipe
[1] = -1;
6281 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
6283 warning ("creating event pipe failed.");
6287 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
6288 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
6290 /* Register the event loop handler. */
6291 add_file_handler (linux_event_pipe
[0],
6292 handle_target_event
, NULL
);
6294 /* Always trigger a linux_wait. */
6299 delete_file_handler (linux_event_pipe
[0]);
6301 close (linux_event_pipe
[0]);
6302 close (linux_event_pipe
[1]);
6303 linux_event_pipe
[0] = -1;
6304 linux_event_pipe
[1] = -1;
6307 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
6314 linux_start_non_stop (int nonstop
)
6316 /* Register or unregister from event-loop accordingly. */
6317 linux_async (nonstop
);
6319 if (target_is_async_p () != (nonstop
!= 0))
6326 linux_supports_multi_process (void)
6331 /* Check if fork events are supported. */
6334 linux_supports_fork_events (void)
6336 return linux_supports_tracefork ();
6339 /* Check if vfork events are supported. */
6342 linux_supports_vfork_events (void)
6344 return linux_supports_tracefork ();
6347 /* Check if exec events are supported. */
6350 linux_supports_exec_events (void)
6352 return linux_supports_traceexec ();
6355 /* Callback for 'find_inferior'. Set the (possibly changed) ptrace
6356 options for the specified lwp. */
6359 reset_lwp_ptrace_options_callback (struct inferior_list_entry
*entry
,
6362 struct thread_info
*thread
= (struct thread_info
*) entry
;
6363 struct lwp_info
*lwp
= get_thread_lwp (thread
);
6367 /* Stop the lwp so we can modify its ptrace options. */
6368 lwp
->must_set_ptrace_flags
= 1;
6369 linux_stop_lwp (lwp
);
6373 /* Already stopped; go ahead and set the ptrace options. */
6374 struct process_info
*proc
= find_process_pid (pid_of (thread
));
6375 int options
= linux_low_ptrace_options (proc
->attached
);
6377 linux_enable_event_reporting (lwpid_of (thread
), options
);
6378 lwp
->must_set_ptrace_flags
= 0;
6384 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
6385 ptrace flags for all inferiors. This is in case the new GDB connection
6386 doesn't support the same set of events that the previous one did. */
6389 linux_handle_new_gdb_connection (void)
6393 /* Request that all the lwps reset their ptrace options. */
6394 find_inferior (&all_threads
, reset_lwp_ptrace_options_callback
, &pid
);
6398 linux_supports_disable_randomization (void)
6400 #ifdef HAVE_PERSONALITY
6408 linux_supports_agent (void)
6414 linux_supports_range_stepping (void)
6416 if (*the_low_target
.supports_range_stepping
== NULL
)
6419 return (*the_low_target
.supports_range_stepping
) ();
6422 /* Enumerate spufs IDs for process PID. */
6424 spu_enumerate_spu_ids (long pid
, unsigned char *buf
, CORE_ADDR offset
, int len
)
6430 struct dirent
*entry
;
6432 sprintf (path
, "/proc/%ld/fd", pid
);
6433 dir
= opendir (path
);
6438 while ((entry
= readdir (dir
)) != NULL
)
6444 fd
= atoi (entry
->d_name
);
6448 sprintf (path
, "/proc/%ld/fd/%d", pid
, fd
);
6449 if (stat (path
, &st
) != 0)
6451 if (!S_ISDIR (st
.st_mode
))
6454 if (statfs (path
, &stfs
) != 0)
6456 if (stfs
.f_type
!= SPUFS_MAGIC
)
6459 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
6461 *(unsigned int *)(buf
+ pos
- offset
) = fd
;
6471 /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
6472 object type, using the /proc file system. */
6474 linux_qxfer_spu (const char *annex
, unsigned char *readbuf
,
6475 unsigned const char *writebuf
,
6476 CORE_ADDR offset
, int len
)
6478 long pid
= lwpid_of (current_thread
);
6483 if (!writebuf
&& !readbuf
)
6491 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
6494 sprintf (buf
, "/proc/%ld/fd/%s", pid
, annex
);
6495 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
6500 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
6507 ret
= write (fd
, writebuf
, (size_t) len
);
6509 ret
= read (fd
, readbuf
, (size_t) len
);
6515 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
6516 struct target_loadseg
6518 /* Core address to which the segment is mapped. */
6520 /* VMA recorded in the program header. */
6522 /* Size of this segment in memory. */
6526 # if defined PT_GETDSBT
6527 struct target_loadmap
6529 /* Protocol version number, must be zero. */
6531 /* Pointer to the DSBT table, its size, and the DSBT index. */
6532 unsigned *dsbt_table
;
6533 unsigned dsbt_size
, dsbt_index
;
6534 /* Number of segments in this map. */
6536 /* The actual memory map. */
6537 struct target_loadseg segs
[/*nsegs*/];
6539 # define LINUX_LOADMAP PT_GETDSBT
6540 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6541 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6543 struct target_loadmap
6545 /* Protocol version number, must be zero. */
6547 /* Number of segments in this map. */
6549 /* The actual memory map. */
6550 struct target_loadseg segs
[/*nsegs*/];
6552 # define LINUX_LOADMAP PTRACE_GETFDPIC
6553 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6554 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6558 linux_read_loadmap (const char *annex
, CORE_ADDR offset
,
6559 unsigned char *myaddr
, unsigned int len
)
6561 int pid
= lwpid_of (current_thread
);
6563 struct target_loadmap
*data
= NULL
;
6564 unsigned int actual_length
, copy_length
;
6566 if (strcmp (annex
, "exec") == 0)
6567 addr
= (int) LINUX_LOADMAP_EXEC
;
6568 else if (strcmp (annex
, "interp") == 0)
6569 addr
= (int) LINUX_LOADMAP_INTERP
;
6573 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
6579 actual_length
= sizeof (struct target_loadmap
)
6580 + sizeof (struct target_loadseg
) * data
->nsegs
;
6582 if (offset
< 0 || offset
> actual_length
)
6585 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
6586 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
6590 # define linux_read_loadmap NULL
6591 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6594 linux_process_qsupported (char **features
, int count
)
6596 if (the_low_target
.process_qsupported
!= NULL
)
6597 the_low_target
.process_qsupported (features
, count
);
6601 linux_supports_catch_syscall (void)
6603 return (the_low_target
.get_syscall_trapinfo
!= NULL
6604 && linux_supports_tracesysgood ());
6608 linux_get_ipa_tdesc_idx (void)
6610 if (the_low_target
.get_ipa_tdesc_idx
== NULL
)
6613 return (*the_low_target
.get_ipa_tdesc_idx
) ();
6617 linux_supports_tracepoints (void)
6619 if (*the_low_target
.supports_tracepoints
== NULL
)
6622 return (*the_low_target
.supports_tracepoints
) ();
6626 linux_read_pc (struct regcache
*regcache
)
6628 if (the_low_target
.get_pc
== NULL
)
6631 return (*the_low_target
.get_pc
) (regcache
);
6635 linux_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
6637 gdb_assert (the_low_target
.set_pc
!= NULL
);
6639 (*the_low_target
.set_pc
) (regcache
, pc
);
6643 linux_thread_stopped (struct thread_info
*thread
)
6645 return get_thread_lwp (thread
)->stopped
;
6648 /* This exposes stop-all-threads functionality to other modules. */
6651 linux_pause_all (int freeze
)
6653 stop_all_lwps (freeze
, NULL
);
6656 /* This exposes unstop-all-threads functionality to other gdbserver
6660 linux_unpause_all (int unfreeze
)
6662 unstop_all_lwps (unfreeze
, NULL
);
6666 linux_prepare_to_access_memory (void)
6668 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6671 linux_pause_all (1);
6676 linux_done_accessing_memory (void)
6678 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6681 linux_unpause_all (1);
6685 linux_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
, CORE_ADDR tpaddr
,
6686 CORE_ADDR collector
,
6689 CORE_ADDR
*jump_entry
,
6690 CORE_ADDR
*trampoline
,
6691 ULONGEST
*trampoline_size
,
6692 unsigned char *jjump_pad_insn
,
6693 ULONGEST
*jjump_pad_insn_size
,
6694 CORE_ADDR
*adjusted_insn_addr
,
6695 CORE_ADDR
*adjusted_insn_addr_end
,
6698 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
6699 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
6700 jump_entry
, trampoline
, trampoline_size
,
6701 jjump_pad_insn
, jjump_pad_insn_size
,
6702 adjusted_insn_addr
, adjusted_insn_addr_end
,
6706 static struct emit_ops
*
6707 linux_emit_ops (void)
6709 if (the_low_target
.emit_ops
!= NULL
)
6710 return (*the_low_target
.emit_ops
) ();
6716 linux_get_min_fast_tracepoint_insn_len (void)
6718 return (*the_low_target
.get_min_fast_tracepoint_insn_len
) ();
6721 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6724 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
6725 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
6727 char filename
[PATH_MAX
];
6729 const int auxv_size
= is_elf64
6730 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
6731 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
6733 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
6735 fd
= open (filename
, O_RDONLY
);
6741 while (read (fd
, buf
, auxv_size
) == auxv_size
6742 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
6746 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
6748 switch (aux
->a_type
)
6751 *phdr_memaddr
= aux
->a_un
.a_val
;
6754 *num_phdr
= aux
->a_un
.a_val
;
6760 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
6762 switch (aux
->a_type
)
6765 *phdr_memaddr
= aux
->a_un
.a_val
;
6768 *num_phdr
= aux
->a_un
.a_val
;
6776 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
6778 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6779 "phdr_memaddr = %ld, phdr_num = %d",
6780 (long) *phdr_memaddr
, *num_phdr
);
6787 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6790 get_dynamic (const int pid
, const int is_elf64
)
6792 CORE_ADDR phdr_memaddr
, relocation
;
6794 unsigned char *phdr_buf
;
6795 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
6797 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
6800 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
6801 phdr_buf
= (unsigned char *) alloca (num_phdr
* phdr_size
);
6803 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
6806 /* Compute relocation: it is expected to be 0 for "regular" executables,
6807 non-zero for PIE ones. */
6809 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
6812 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6814 if (p
->p_type
== PT_PHDR
)
6815 relocation
= phdr_memaddr
- p
->p_vaddr
;
6819 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6821 if (p
->p_type
== PT_PHDR
)
6822 relocation
= phdr_memaddr
- p
->p_vaddr
;
6825 if (relocation
== -1)
6827 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6828 any real world executables, including PIE executables, have always
6829 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6830 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6831 or present DT_DEBUG anyway (fpc binaries are statically linked).
6833 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6835 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6840 for (i
= 0; i
< num_phdr
; i
++)
6844 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6846 if (p
->p_type
== PT_DYNAMIC
)
6847 return p
->p_vaddr
+ relocation
;
6851 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6853 if (p
->p_type
== PT_DYNAMIC
)
6854 return p
->p_vaddr
+ relocation
;
6861 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6862 can be 0 if the inferior does not yet have the library list initialized.
6863 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6864 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6867 get_r_debug (const int pid
, const int is_elf64
)
6869 CORE_ADDR dynamic_memaddr
;
6870 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
6871 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
6874 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
6875 if (dynamic_memaddr
== 0)
6878 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
6882 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
6883 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6887 unsigned char buf
[sizeof (Elf64_Xword
)];
6891 #ifdef DT_MIPS_RLD_MAP
6892 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6894 if (linux_read_memory (dyn
->d_un
.d_val
,
6895 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6900 #endif /* DT_MIPS_RLD_MAP */
6901 #ifdef DT_MIPS_RLD_MAP_REL
6902 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6904 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6905 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6910 #endif /* DT_MIPS_RLD_MAP_REL */
6912 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6913 map
= dyn
->d_un
.d_val
;
6915 if (dyn
->d_tag
== DT_NULL
)
6920 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
6921 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6925 unsigned char buf
[sizeof (Elf32_Word
)];
6929 #ifdef DT_MIPS_RLD_MAP
6930 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6932 if (linux_read_memory (dyn
->d_un
.d_val
,
6933 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6938 #endif /* DT_MIPS_RLD_MAP */
6939 #ifdef DT_MIPS_RLD_MAP_REL
6940 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6942 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6943 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6948 #endif /* DT_MIPS_RLD_MAP_REL */
6950 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6951 map
= dyn
->d_un
.d_val
;
6953 if (dyn
->d_tag
== DT_NULL
)
6957 dynamic_memaddr
+= dyn_size
;
6963 /* Read one pointer from MEMADDR in the inferior. */
6966 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
6970 /* Go through a union so this works on either big or little endian
6971 hosts, when the inferior's pointer size is smaller than the size
6972 of CORE_ADDR. It is assumed the inferior's endianness is the
6973 same of the superior's. */
6976 CORE_ADDR core_addr
;
6981 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
6984 if (ptr_size
== sizeof (CORE_ADDR
))
6985 *ptr
= addr
.core_addr
;
6986 else if (ptr_size
== sizeof (unsigned int))
6989 gdb_assert_not_reached ("unhandled pointer size");
6994 struct link_map_offsets
6996 /* Offset and size of r_debug.r_version. */
6997 int r_version_offset
;
6999 /* Offset and size of r_debug.r_map. */
7002 /* Offset to l_addr field in struct link_map. */
7005 /* Offset to l_name field in struct link_map. */
7008 /* Offset to l_ld field in struct link_map. */
7011 /* Offset to l_next field in struct link_map. */
7014 /* Offset to l_prev field in struct link_map. */
7018 /* Construct qXfer:libraries-svr4:read reply. */
7021 linux_qxfer_libraries_svr4 (const char *annex
, unsigned char *readbuf
,
7022 unsigned const char *writebuf
,
7023 CORE_ADDR offset
, int len
)
7026 unsigned document_len
;
7027 struct process_info_private
*const priv
= current_process ()->priv
;
7028 char filename
[PATH_MAX
];
7031 static const struct link_map_offsets lmo_32bit_offsets
=
7033 0, /* r_version offset. */
7034 4, /* r_debug.r_map offset. */
7035 0, /* l_addr offset in link_map. */
7036 4, /* l_name offset in link_map. */
7037 8, /* l_ld offset in link_map. */
7038 12, /* l_next offset in link_map. */
7039 16 /* l_prev offset in link_map. */
7042 static const struct link_map_offsets lmo_64bit_offsets
=
7044 0, /* r_version offset. */
7045 8, /* r_debug.r_map offset. */
7046 0, /* l_addr offset in link_map. */
7047 8, /* l_name offset in link_map. */
7048 16, /* l_ld offset in link_map. */
7049 24, /* l_next offset in link_map. */
7050 32 /* l_prev offset in link_map. */
7052 const struct link_map_offsets
*lmo
;
7053 unsigned int machine
;
7055 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
7056 int allocated
= 1024;
7058 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
7059 int header_done
= 0;
7061 if (writebuf
!= NULL
)
7063 if (readbuf
== NULL
)
7066 pid
= lwpid_of (current_thread
);
7067 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
7068 is_elf64
= elf_64_file_p (filename
, &machine
);
7069 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
7070 ptr_size
= is_elf64
? 8 : 4;
7072 while (annex
[0] != '\0')
7078 sep
= strchr (annex
, '=');
7083 if (len
== 5 && startswith (annex
, "start"))
7085 else if (len
== 4 && startswith (annex
, "prev"))
7089 annex
= strchr (sep
, ';');
7096 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
7103 if (priv
->r_debug
== 0)
7104 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
7106 /* We failed to find DT_DEBUG. Such situation will not change
7107 for this inferior - do not retry it. Report it to GDB as
7108 E01, see for the reasons at the GDB solib-svr4.c side. */
7109 if (priv
->r_debug
== (CORE_ADDR
) -1)
7112 if (priv
->r_debug
!= 0)
7114 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
7115 (unsigned char *) &r_version
,
7116 sizeof (r_version
)) != 0
7119 warning ("unexpected r_debug version %d", r_version
);
7121 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
7122 &lm_addr
, ptr_size
) != 0)
7124 warning ("unable to read r_map from 0x%lx",
7125 (long) priv
->r_debug
+ lmo
->r_map_offset
);
7130 document
= (char *) xmalloc (allocated
);
7131 strcpy (document
, "<library-list-svr4 version=\"1.0\"");
7132 p
= document
+ strlen (document
);
7135 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
7136 &l_name
, ptr_size
) == 0
7137 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
7138 &l_addr
, ptr_size
) == 0
7139 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
7140 &l_ld
, ptr_size
) == 0
7141 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
7142 &l_prev
, ptr_size
) == 0
7143 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
7144 &l_next
, ptr_size
) == 0)
7146 unsigned char libname
[PATH_MAX
];
7148 if (lm_prev
!= l_prev
)
7150 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
7151 (long) lm_prev
, (long) l_prev
);
7155 /* Ignore the first entry even if it has valid name as the first entry
7156 corresponds to the main executable. The first entry should not be
7157 skipped if the dynamic loader was loaded late by a static executable
7158 (see solib-svr4.c parameter ignore_first). But in such case the main
7159 executable does not have PT_DYNAMIC present and this function already
7160 exited above due to failed get_r_debug. */
7163 sprintf (p
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
7168 /* Not checking for error because reading may stop before
7169 we've got PATH_MAX worth of characters. */
7171 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
7172 libname
[sizeof (libname
) - 1] = '\0';
7173 if (libname
[0] != '\0')
7175 /* 6x the size for xml_escape_text below. */
7176 size_t len
= 6 * strlen ((char *) libname
);
7181 /* Terminate `<library-list-svr4'. */
7186 while (allocated
< p
- document
+ len
+ 200)
7188 /* Expand to guarantee sufficient storage. */
7189 uintptr_t document_len
= p
- document
;
7191 document
= (char *) xrealloc (document
, 2 * allocated
);
7193 p
= document
+ document_len
;
7196 name
= xml_escape_text ((char *) libname
);
7197 p
+= sprintf (p
, "<library name=\"%s\" lm=\"0x%lx\" "
7198 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
7199 name
, (unsigned long) lm_addr
,
7200 (unsigned long) l_addr
, (unsigned long) l_ld
);
7211 /* Empty list; terminate `<library-list-svr4'. */
7215 strcpy (p
, "</library-list-svr4>");
7217 document_len
= strlen (document
);
7218 if (offset
< document_len
)
7219 document_len
-= offset
;
7222 if (len
> document_len
)
7225 memcpy (readbuf
, document
+ offset
, len
);
7231 #ifdef HAVE_LINUX_BTRACE
7233 /* See to_disable_btrace target method. */
7236 linux_low_disable_btrace (struct btrace_target_info
*tinfo
)
7238 enum btrace_error err
;
7240 err
= linux_disable_btrace (tinfo
);
7241 return (err
== BTRACE_ERR_NONE
? 0 : -1);
7244 /* Encode an Intel Processor Trace configuration. */
7247 linux_low_encode_pt_config (struct buffer
*buffer
,
7248 const struct btrace_data_pt_config
*config
)
7250 buffer_grow_str (buffer
, "<pt-config>\n");
7252 switch (config
->cpu
.vendor
)
7255 buffer_xml_printf (buffer
, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
7256 "model=\"%u\" stepping=\"%u\"/>\n",
7257 config
->cpu
.family
, config
->cpu
.model
,
7258 config
->cpu
.stepping
);
7265 buffer_grow_str (buffer
, "</pt-config>\n");
7268 /* Encode a raw buffer. */
7271 linux_low_encode_raw (struct buffer
*buffer
, const gdb_byte
*data
,
7277 /* We use hex encoding - see common/rsp-low.h. */
7278 buffer_grow_str (buffer
, "<raw>\n");
7284 elem
[0] = tohex ((*data
>> 4) & 0xf);
7285 elem
[1] = tohex (*data
++ & 0xf);
7287 buffer_grow (buffer
, elem
, 2);
7290 buffer_grow_str (buffer
, "</raw>\n");
7293 /* See to_read_btrace target method. */
7296 linux_low_read_btrace (struct btrace_target_info
*tinfo
, struct buffer
*buffer
,
7297 enum btrace_read_type type
)
7299 struct btrace_data btrace
;
7300 struct btrace_block
*block
;
7301 enum btrace_error err
;
7304 btrace_data_init (&btrace
);
7306 err
= linux_read_btrace (&btrace
, tinfo
, type
);
7307 if (err
!= BTRACE_ERR_NONE
)
7309 if (err
== BTRACE_ERR_OVERFLOW
)
7310 buffer_grow_str0 (buffer
, "E.Overflow.");
7312 buffer_grow_str0 (buffer
, "E.Generic Error.");
7317 switch (btrace
.format
)
7319 case BTRACE_FORMAT_NONE
:
7320 buffer_grow_str0 (buffer
, "E.No Trace.");
7323 case BTRACE_FORMAT_BTS
:
7324 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7325 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7328 VEC_iterate (btrace_block_s
, btrace
.variant
.bts
.blocks
, i
, block
);
7330 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
7331 paddress (block
->begin
), paddress (block
->end
));
7333 buffer_grow_str0 (buffer
, "</btrace>\n");
7336 case BTRACE_FORMAT_PT
:
7337 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7338 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7339 buffer_grow_str (buffer
, "<pt>\n");
7341 linux_low_encode_pt_config (buffer
, &btrace
.variant
.pt
.config
);
7343 linux_low_encode_raw (buffer
, btrace
.variant
.pt
.data
,
7344 btrace
.variant
.pt
.size
);
7346 buffer_grow_str (buffer
, "</pt>\n");
7347 buffer_grow_str0 (buffer
, "</btrace>\n");
7351 buffer_grow_str0 (buffer
, "E.Unsupported Trace Format.");
7355 btrace_data_fini (&btrace
);
7359 btrace_data_fini (&btrace
);
7363 /* See to_btrace_conf target method. */
7366 linux_low_btrace_conf (const struct btrace_target_info
*tinfo
,
7367 struct buffer
*buffer
)
7369 const struct btrace_config
*conf
;
7371 buffer_grow_str (buffer
, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
7372 buffer_grow_str (buffer
, "<btrace-conf version=\"1.0\">\n");
7374 conf
= linux_btrace_conf (tinfo
);
7377 switch (conf
->format
)
7379 case BTRACE_FORMAT_NONE
:
7382 case BTRACE_FORMAT_BTS
:
7383 buffer_xml_printf (buffer
, "<bts");
7384 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->bts
.size
);
7385 buffer_xml_printf (buffer
, " />\n");
7388 case BTRACE_FORMAT_PT
:
7389 buffer_xml_printf (buffer
, "<pt");
7390 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->pt
.size
);
7391 buffer_xml_printf (buffer
, "/>\n");
7396 buffer_grow_str0 (buffer
, "</btrace-conf>\n");
7399 #endif /* HAVE_LINUX_BTRACE */
7401 /* See nat/linux-nat.h. */
7404 current_lwp_ptid (void)
7406 return ptid_of (current_thread
);
7409 /* Implementation of the target_ops method "breakpoint_kind_from_pc". */
7412 linux_breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
7414 if (the_low_target
.breakpoint_kind_from_pc
!= NULL
)
7415 return (*the_low_target
.breakpoint_kind_from_pc
) (pcptr
);
7417 return default_breakpoint_kind_from_pc (pcptr
);
7420 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
7422 static const gdb_byte
*
7423 linux_sw_breakpoint_from_kind (int kind
, int *size
)
7425 gdb_assert (the_low_target
.sw_breakpoint_from_kind
!= NULL
);
7427 return (*the_low_target
.sw_breakpoint_from_kind
) (kind
, size
);
7430 /* Implementation of the target_ops method
7431 "breakpoint_kind_from_current_state". */
7434 linux_breakpoint_kind_from_current_state (CORE_ADDR
*pcptr
)
7436 if (the_low_target
.breakpoint_kind_from_current_state
!= NULL
)
7437 return (*the_low_target
.breakpoint_kind_from_current_state
) (pcptr
);
7439 return linux_breakpoint_kind_from_pc (pcptr
);
7442 /* Default implementation of linux_target_ops method "set_pc" for
7443 32-bit pc register which is literally named "pc". */
7446 linux_set_pc_32bit (struct regcache
*regcache
, CORE_ADDR pc
)
7448 uint32_t newpc
= pc
;
7450 supply_register_by_name (regcache
, "pc", &newpc
);
7453 /* Default implementation of linux_target_ops method "get_pc" for
7454 32-bit pc register which is literally named "pc". */
7457 linux_get_pc_32bit (struct regcache
*regcache
)
7461 collect_register_by_name (regcache
, "pc", &pc
);
7463 debug_printf ("stop pc is 0x%" PRIx32
"\n", pc
);
7467 /* Default implementation of linux_target_ops method "set_pc" for
7468 64-bit pc register which is literally named "pc". */
7471 linux_set_pc_64bit (struct regcache
*regcache
, CORE_ADDR pc
)
7473 uint64_t newpc
= pc
;
7475 supply_register_by_name (regcache
, "pc", &newpc
);
7478 /* Default implementation of linux_target_ops method "get_pc" for
7479 64-bit pc register which is literally named "pc". */
7482 linux_get_pc_64bit (struct regcache
*regcache
)
7486 collect_register_by_name (regcache
, "pc", &pc
);
7488 debug_printf ("stop pc is 0x%" PRIx64
"\n", pc
);
7493 static struct target_ops linux_target_ops
= {
7494 linux_create_inferior
,
7495 linux_post_create_inferior
,
7504 linux_fetch_registers
,
7505 linux_store_registers
,
7506 linux_prepare_to_access_memory
,
7507 linux_done_accessing_memory
,
7510 linux_look_up_symbols
,
7511 linux_request_interrupt
,
7513 linux_supports_z_point_type
,
7516 linux_stopped_by_sw_breakpoint
,
7517 linux_supports_stopped_by_sw_breakpoint
,
7518 linux_stopped_by_hw_breakpoint
,
7519 linux_supports_stopped_by_hw_breakpoint
,
7520 linux_supports_hardware_single_step
,
7521 linux_stopped_by_watchpoint
,
7522 linux_stopped_data_address
,
7523 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
7524 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
7525 && defined(PT_TEXT_END_ADDR)
7530 #ifdef USE_THREAD_DB
7531 thread_db_get_tls_address
,
7536 hostio_last_error_from_errno
,
7539 linux_supports_non_stop
,
7541 linux_start_non_stop
,
7542 linux_supports_multi_process
,
7543 linux_supports_fork_events
,
7544 linux_supports_vfork_events
,
7545 linux_supports_exec_events
,
7546 linux_handle_new_gdb_connection
,
7547 #ifdef USE_THREAD_DB
7548 thread_db_handle_monitor_command
,
7552 linux_common_core_of_thread
,
7554 linux_process_qsupported
,
7555 linux_supports_tracepoints
,
7558 linux_thread_stopped
,
7562 linux_stabilize_threads
,
7563 linux_install_fast_tracepoint_jump_pad
,
7565 linux_supports_disable_randomization
,
7566 linux_get_min_fast_tracepoint_insn_len
,
7567 linux_qxfer_libraries_svr4
,
7568 linux_supports_agent
,
7569 #ifdef HAVE_LINUX_BTRACE
7570 linux_supports_btrace
,
7571 linux_enable_btrace
,
7572 linux_low_disable_btrace
,
7573 linux_low_read_btrace
,
7574 linux_low_btrace_conf
,
7582 linux_supports_range_stepping
,
7583 linux_proc_pid_to_exec_file
,
7584 linux_mntns_open_cloexec
,
7586 linux_mntns_readlink
,
7587 linux_breakpoint_kind_from_pc
,
7588 linux_sw_breakpoint_from_kind
,
7589 linux_proc_tid_get_name
,
7590 linux_breakpoint_kind_from_current_state
,
7591 linux_supports_software_single_step
,
7592 linux_supports_catch_syscall
,
7593 linux_get_ipa_tdesc_idx
,
7596 #ifdef HAVE_LINUX_REGSETS
7598 initialize_regsets_info (struct regsets_info
*info
)
7600 for (info
->num_regsets
= 0;
7601 info
->regsets
[info
->num_regsets
].size
>= 0;
7602 info
->num_regsets
++)
7608 initialize_low (void)
7610 struct sigaction sigchld_action
;
7612 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
7613 set_target_ops (&linux_target_ops
);
7615 linux_ptrace_init_warnings ();
7617 sigchld_action
.sa_handler
= sigchld_handler
;
7618 sigemptyset (&sigchld_action
.sa_mask
);
7619 sigchld_action
.sa_flags
= SA_RESTART
;
7620 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
7622 initialize_low_arch ();
7624 linux_check_ptrace_features ();