1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2013 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 "linux-osdata.h"
26 #include <sys/param.h>
27 #include <sys/ptrace.h>
28 #include "linux-ptrace.h"
29 #include "linux-procfs.h"
31 #include <sys/ioctl.h>
37 #include <sys/syscall.h>
41 #include <sys/types.h>
47 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
48 then ELFMAG0 will have been defined. If it didn't get included by
49 gdb_proc_service.h then including it will likely introduce a duplicate
50 definition of elf_fpregset_t. */
55 #define SPUFS_MAGIC 0x23c9b64e
58 #ifdef HAVE_PERSONALITY
59 # include <sys/personality.h>
60 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
61 # define ADDR_NO_RANDOMIZE 0x0040000
70 #define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
73 /* This is the kernel's hard limit. Not to be confused with
80 #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
81 /* PTRACE_TEXT_ADDR and friends. */
82 #include <asm/ptrace.h>
87 /* Some targets did not define these ptrace constants from the start,
88 so gdbserver defines them locally here. In the future, these may
89 be removed after they are added to asm/ptrace.h. */
90 #if !(defined(PT_TEXT_ADDR) \
91 || defined(PT_DATA_ADDR) \
92 || defined(PT_TEXT_END_ADDR))
93 #if defined(__mcoldfire__)
94 /* These are still undefined in 3.10 kernels. */
95 #define PT_TEXT_ADDR 49*4
96 #define PT_DATA_ADDR 50*4
97 #define PT_TEXT_END_ADDR 51*4
98 /* BFIN already defines these since at least 2.6.32 kernels. */
100 #define PT_TEXT_ADDR 220
101 #define PT_TEXT_END_ADDR 224
102 #define PT_DATA_ADDR 228
103 /* These are still undefined in 3.10 kernels. */
104 #elif defined(__TMS320C6X__)
105 #define PT_TEXT_ADDR (0x10000*4)
106 #define PT_DATA_ADDR (0x10004*4)
107 #define PT_TEXT_END_ADDR (0x10008*4)
111 #ifdef HAVE_LINUX_BTRACE
112 # include "linux-btrace.h"
115 #ifndef HAVE_ELF32_AUXV_T
116 /* Copied from glibc's elf.h. */
119 uint32_t a_type
; /* Entry type */
122 uint32_t a_val
; /* Integer value */
123 /* We use to have pointer elements added here. We cannot do that,
124 though, since it does not work when using 32-bit definitions
125 on 64-bit platforms and vice versa. */
130 #ifndef HAVE_ELF64_AUXV_T
131 /* Copied from glibc's elf.h. */
134 uint64_t a_type
; /* Entry type */
137 uint64_t a_val
; /* Integer value */
138 /* We use to have pointer elements added here. We cannot do that,
139 though, since it does not work when using 32-bit definitions
140 on 64-bit platforms and vice versa. */
145 /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
146 representation of the thread ID.
148 ``all_lwps'' is keyed by the process ID - which on Linux is (presently)
149 the same as the LWP ID.
151 ``all_processes'' is keyed by the "overall process ID", which
152 GNU/Linux calls tgid, "thread group ID". */
154 struct inferior_list all_lwps
;
156 /* A list of all unknown processes which receive stop signals. Some
157 other process will presumably claim each of these as forked
158 children momentarily. */
160 struct simple_pid_list
162 /* The process ID. */
165 /* The status as reported by waitpid. */
169 struct simple_pid_list
*next
;
171 struct simple_pid_list
*stopped_pids
;
173 /* Trivial list manipulation functions to keep track of a list of new
174 stopped processes. */
177 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
179 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
182 new_pid
->status
= status
;
183 new_pid
->next
= *listp
;
188 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
190 struct simple_pid_list
**p
;
192 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
193 if ((*p
)->pid
== pid
)
195 struct simple_pid_list
*next
= (*p
)->next
;
197 *statusp
= (*p
)->status
;
205 enum stopping_threads_kind
207 /* Not stopping threads presently. */
208 NOT_STOPPING_THREADS
,
210 /* Stopping threads. */
213 /* Stopping and suspending threads. */
214 STOPPING_AND_SUSPENDING_THREADS
217 /* This is set while stop_all_lwps is in effect. */
218 enum stopping_threads_kind stopping_threads
= NOT_STOPPING_THREADS
;
220 /* FIXME make into a target method? */
221 int using_threads
= 1;
223 /* True if we're presently stabilizing threads (moving them out of
225 static int stabilizing_threads
;
227 static void linux_resume_one_lwp (struct lwp_info
*lwp
,
228 int step
, int signal
, siginfo_t
*info
);
229 static void linux_resume (struct thread_resume
*resume_info
, size_t n
);
230 static void stop_all_lwps (int suspend
, struct lwp_info
*except
);
231 static void unstop_all_lwps (int unsuspend
, struct lwp_info
*except
);
232 static int linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
);
233 static void *add_lwp (ptid_t ptid
);
234 static int linux_stopped_by_watchpoint (void);
235 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
236 static void proceed_all_lwps (void);
237 static int finish_step_over (struct lwp_info
*lwp
);
238 static CORE_ADDR
get_stop_pc (struct lwp_info
*lwp
);
239 static int kill_lwp (unsigned long lwpid
, int signo
);
240 static void linux_enable_event_reporting (int pid
);
242 /* True if the low target can hardware single-step. Such targets
243 don't need a BREAKPOINT_REINSERT_ADDR callback. */
246 can_hardware_single_step (void)
248 return (the_low_target
.breakpoint_reinsert_addr
== NULL
);
251 /* True if the low target supports memory breakpoints. If so, we'll
252 have a GET_PC implementation. */
255 supports_breakpoints (void)
257 return (the_low_target
.get_pc
!= NULL
);
260 /* Returns true if this target can support fast tracepoints. This
261 does not mean that the in-process agent has been loaded in the
265 supports_fast_tracepoints (void)
267 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
270 /* True if LWP is stopped in its stepping range. */
273 lwp_in_step_range (struct lwp_info
*lwp
)
275 CORE_ADDR pc
= lwp
->stop_pc
;
277 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
280 struct pending_signals
284 struct pending_signals
*prev
;
287 /* The read/write ends of the pipe registered as waitable file in the
289 static int linux_event_pipe
[2] = { -1, -1 };
291 /* True if we're currently in async mode. */
292 #define target_is_async_p() (linux_event_pipe[0] != -1)
294 static void send_sigstop (struct lwp_info
*lwp
);
295 static void wait_for_sigstop (struct inferior_list_entry
*entry
);
297 /* Return non-zero if HEADER is a 64-bit ELF file. */
300 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
302 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
303 && header
->e_ident
[EI_MAG1
] == ELFMAG1
304 && header
->e_ident
[EI_MAG2
] == ELFMAG2
305 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
307 *machine
= header
->e_machine
;
308 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
315 /* Return non-zero if FILE is a 64-bit ELF file,
316 zero if the file is not a 64-bit ELF file,
317 and -1 if the file is not accessible or doesn't exist. */
320 elf_64_file_p (const char *file
, unsigned int *machine
)
325 fd
= open (file
, O_RDONLY
);
329 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
336 return elf_64_header_p (&header
, machine
);
339 /* Accepts an integer PID; Returns true if the executable PID is
340 running is a 64-bit ELF file.. */
343 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
345 char file
[MAXPATHLEN
];
347 sprintf (file
, "/proc/%d/exe", pid
);
348 return elf_64_file_p (file
, machine
);
352 delete_lwp (struct lwp_info
*lwp
)
354 remove_thread (get_lwp_thread (lwp
));
355 remove_inferior (&all_lwps
, &lwp
->head
);
356 free (lwp
->arch_private
);
360 /* Add a process to the common process list, and set its private
363 static struct process_info
*
364 linux_add_process (int pid
, int attached
)
366 struct process_info
*proc
;
368 proc
= add_process (pid
, attached
);
369 proc
->private = xcalloc (1, sizeof (*proc
->private));
371 /* Set the arch when the first LWP stops. */
372 proc
->private->new_inferior
= 1;
374 if (the_low_target
.new_process
!= NULL
)
375 proc
->private->arch_private
= the_low_target
.new_process ();
380 /* Wrapper function for waitpid which handles EINTR, and emulates
381 __WALL for systems where that is not available. */
384 my_waitpid (int pid
, int *status
, int flags
)
389 fprintf (stderr
, "my_waitpid (%d, 0x%x)\n", pid
, flags
);
393 sigset_t block_mask
, org_mask
, wake_mask
;
396 wnohang
= (flags
& WNOHANG
) != 0;
397 flags
&= ~(__WALL
| __WCLONE
);
400 /* Block all signals while here. This avoids knowing about
401 LinuxThread's signals. */
402 sigfillset (&block_mask
);
403 sigprocmask (SIG_BLOCK
, &block_mask
, &org_mask
);
405 /* ... except during the sigsuspend below. */
406 sigemptyset (&wake_mask
);
410 /* Since all signals are blocked, there's no need to check
412 ret
= waitpid (pid
, status
, flags
);
415 if (ret
== -1 && out_errno
!= ECHILD
)
420 if (flags
& __WCLONE
)
422 /* We've tried both flavors now. If WNOHANG is set,
423 there's nothing else to do, just bail out. */
428 fprintf (stderr
, "blocking\n");
430 /* Block waiting for signals. */
431 sigsuspend (&wake_mask
);
437 sigprocmask (SIG_SETMASK
, &org_mask
, NULL
);
442 ret
= waitpid (pid
, status
, flags
);
443 while (ret
== -1 && errno
== EINTR
);
448 fprintf (stderr
, "my_waitpid (%d, 0x%x): status(%x), %d\n",
449 pid
, flags
, status
? *status
: -1, ret
);
455 /* Handle a GNU/Linux extended wait response. If we see a clone
456 event, we need to add the new LWP to our list (and not report the
457 trap to higher layers). */
460 handle_extended_wait (struct lwp_info
*event_child
, int wstat
)
462 int event
= wstat
>> 16;
463 struct lwp_info
*new_lwp
;
465 if (event
== PTRACE_EVENT_CLONE
)
468 unsigned long new_pid
;
471 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_child
), (PTRACE_ARG3_TYPE
) 0,
474 /* If we haven't already seen the new PID stop, wait for it now. */
475 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
477 /* The new child has a pending SIGSTOP. We can't affect it until it
478 hits the SIGSTOP, but we're already attached. */
480 ret
= my_waitpid (new_pid
, &status
, __WALL
);
483 perror_with_name ("waiting for new child");
484 else if (ret
!= new_pid
)
485 warning ("wait returned unexpected PID %d", ret
);
486 else if (!WIFSTOPPED (status
))
487 warning ("wait returned unexpected status 0x%x", status
);
490 ptid
= ptid_build (pid_of (event_child
), new_pid
, 0);
491 new_lwp
= (struct lwp_info
*) add_lwp (ptid
);
492 add_thread (ptid
, new_lwp
);
494 /* Either we're going to immediately resume the new thread
495 or leave it stopped. linux_resume_one_lwp is a nop if it
496 thinks the thread is currently running, so set this first
497 before calling linux_resume_one_lwp. */
498 new_lwp
->stopped
= 1;
500 /* If we're suspending all threads, leave this one suspended
502 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
)
503 new_lwp
->suspended
= 1;
505 /* Normally we will get the pending SIGSTOP. But in some cases
506 we might get another signal delivered to the group first.
507 If we do get another signal, be sure not to lose it. */
508 if (WSTOPSIG (status
) == SIGSTOP
)
510 if (stopping_threads
!= NOT_STOPPING_THREADS
)
511 new_lwp
->stop_pc
= get_stop_pc (new_lwp
);
513 linux_resume_one_lwp (new_lwp
, 0, 0, NULL
);
517 new_lwp
->stop_expected
= 1;
519 if (stopping_threads
!= NOT_STOPPING_THREADS
)
521 new_lwp
->stop_pc
= get_stop_pc (new_lwp
);
522 new_lwp
->status_pending_p
= 1;
523 new_lwp
->status_pending
= status
;
526 /* Pass the signal on. This is what GDB does - except
527 shouldn't we really report it instead? */
528 linux_resume_one_lwp (new_lwp
, 0, WSTOPSIG (status
), NULL
);
531 /* Always resume the current thread. If we are stopping
532 threads, it will have a pending SIGSTOP; we may as well
534 linux_resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
538 /* Return the PC as read from the regcache of LWP, without any
542 get_pc (struct lwp_info
*lwp
)
544 struct thread_info
*saved_inferior
;
545 struct regcache
*regcache
;
548 if (the_low_target
.get_pc
== NULL
)
551 saved_inferior
= current_inferior
;
552 current_inferior
= get_lwp_thread (lwp
);
554 regcache
= get_thread_regcache (current_inferior
, 1);
555 pc
= (*the_low_target
.get_pc
) (regcache
);
558 fprintf (stderr
, "pc is 0x%lx\n", (long) pc
);
560 current_inferior
= saved_inferior
;
564 /* This function should only be called if LWP got a SIGTRAP.
565 The SIGTRAP could mean several things.
567 On i386, where decr_pc_after_break is non-zero:
568 If we were single-stepping this process using PTRACE_SINGLESTEP,
569 we will get only the one SIGTRAP (even if the instruction we
570 stepped over was a breakpoint). The value of $eip will be the
572 If we continue the process using PTRACE_CONT, we will get a
573 SIGTRAP when we hit a breakpoint. The value of $eip will be
574 the instruction after the breakpoint (i.e. needs to be
575 decremented). If we report the SIGTRAP to GDB, we must also
576 report the undecremented PC. If we cancel the SIGTRAP, we
577 must resume at the decremented PC.
579 (Presumably, not yet tested) On a non-decr_pc_after_break machine
580 with hardware or kernel single-step:
581 If we single-step over a breakpoint instruction, our PC will
582 point at the following instruction. If we continue and hit a
583 breakpoint instruction, our PC will point at the breakpoint
587 get_stop_pc (struct lwp_info
*lwp
)
591 if (the_low_target
.get_pc
== NULL
)
594 stop_pc
= get_pc (lwp
);
596 if (WSTOPSIG (lwp
->last_status
) == SIGTRAP
598 && !lwp
->stopped_by_watchpoint
599 && lwp
->last_status
>> 16 == 0)
600 stop_pc
-= the_low_target
.decr_pc_after_break
;
603 fprintf (stderr
, "stop pc is 0x%lx\n", (long) stop_pc
);
609 add_lwp (ptid_t ptid
)
611 struct lwp_info
*lwp
;
613 lwp
= (struct lwp_info
*) xmalloc (sizeof (*lwp
));
614 memset (lwp
, 0, sizeof (*lwp
));
618 if (the_low_target
.new_thread
!= NULL
)
619 lwp
->arch_private
= the_low_target
.new_thread ();
621 add_inferior_to_list (&all_lwps
, &lwp
->head
);
626 /* Start an inferior process and returns its pid.
627 ALLARGS is a vector of program-name and args. */
630 linux_create_inferior (char *program
, char **allargs
)
632 #ifdef HAVE_PERSONALITY
633 int personality_orig
= 0, personality_set
= 0;
635 struct lwp_info
*new_lwp
;
639 #ifdef HAVE_PERSONALITY
640 if (disable_randomization
)
643 personality_orig
= personality (0xffffffff);
644 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
647 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
649 if (errno
!= 0 || (personality_set
650 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
651 warning ("Error disabling address space randomization: %s",
656 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
662 perror_with_name ("fork");
666 ptrace (PTRACE_TRACEME
, 0, (PTRACE_ARG3_TYPE
) 0, (PTRACE_ARG4_TYPE
) 0);
668 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
669 signal (__SIGRTMIN
+ 1, SIG_DFL
);
674 /* If gdbserver is connected to gdb via stdio, redirect the inferior's
675 stdout to stderr so that inferior i/o doesn't corrupt the connection.
676 Also, redirect stdin to /dev/null. */
677 if (remote_connection_is_stdio ())
680 open ("/dev/null", O_RDONLY
);
682 if (write (2, "stdin/stdout redirected\n",
683 sizeof ("stdin/stdout redirected\n") - 1) < 0)
685 /* Errors ignored. */;
689 execv (program
, allargs
);
691 execvp (program
, allargs
);
693 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
699 #ifdef HAVE_PERSONALITY
703 personality (personality_orig
);
705 warning ("Error restoring address space randomization: %s",
710 linux_add_process (pid
, 0);
712 ptid
= ptid_build (pid
, pid
, 0);
713 new_lwp
= add_lwp (ptid
);
714 add_thread (ptid
, new_lwp
);
715 new_lwp
->must_set_ptrace_flags
= 1;
720 /* Attach to an inferior process. */
723 linux_attach_lwp_1 (unsigned long lwpid
, int initial
)
726 struct lwp_info
*new_lwp
;
728 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_ARG3_TYPE
) 0, (PTRACE_ARG4_TYPE
) 0)
731 struct buffer buffer
;
735 /* If we fail to attach to an LWP, just warn. */
736 fprintf (stderr
, "Cannot attach to lwp %ld: %s (%d)\n", lwpid
,
737 strerror (errno
), errno
);
742 /* If we fail to attach to a process, report an error. */
743 buffer_init (&buffer
);
744 linux_ptrace_attach_warnings (lwpid
, &buffer
);
745 buffer_grow_str0 (&buffer
, "");
746 error ("%sCannot attach to lwp %ld: %s (%d)", buffer_finish (&buffer
),
747 lwpid
, strerror (errno
), errno
);
751 /* If lwp is the tgid, we handle adding existing threads later.
752 Otherwise we just add lwp without bothering about any other
754 ptid
= ptid_build (lwpid
, lwpid
, 0);
757 /* Note that extracting the pid from the current inferior is
758 safe, since we're always called in the context of the same
759 process as this new thread. */
760 int pid
= pid_of (get_thread_lwp (current_inferior
));
761 ptid
= ptid_build (pid
, lwpid
, 0);
764 new_lwp
= (struct lwp_info
*) add_lwp (ptid
);
765 add_thread (ptid
, new_lwp
);
767 /* We need to wait for SIGSTOP before being able to make the next
768 ptrace call on this LWP. */
769 new_lwp
->must_set_ptrace_flags
= 1;
771 if (linux_proc_pid_is_stopped (lwpid
))
775 "Attached to a stopped process\n");
777 /* The process is definitely stopped. It is in a job control
778 stop, unless the kernel predates the TASK_STOPPED /
779 TASK_TRACED distinction, in which case it might be in a
780 ptrace stop. Make sure it is in a ptrace stop; from there we
781 can kill it, signal it, et cetera.
783 First make sure there is a pending SIGSTOP. Since we are
784 already attached, the process can not transition from stopped
785 to running without a PTRACE_CONT; so we know this signal will
786 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
787 probably already in the queue (unless this kernel is old
788 enough to use TASK_STOPPED for ptrace stops); but since
789 SIGSTOP is not an RT signal, it can only be queued once. */
790 kill_lwp (lwpid
, SIGSTOP
);
792 /* Finally, resume the stopped process. This will deliver the
793 SIGSTOP (or a higher priority signal, just like normal
794 PTRACE_ATTACH), which we'll catch later on. */
795 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_ARG3_TYPE
) 0, (PTRACE_ARG4_TYPE
) 0);
798 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
801 There are several cases to consider here:
803 1) gdbserver has already attached to the process and is being notified
804 of a new thread that is being created.
805 In this case we should ignore that SIGSTOP and resume the
806 process. This is handled below by setting stop_expected = 1,
807 and the fact that add_thread sets last_resume_kind ==
810 2) This is the first thread (the process thread), and we're attaching
811 to it via attach_inferior.
812 In this case we want the process thread to stop.
813 This is handled by having linux_attach set last_resume_kind ==
814 resume_stop after we return.
816 If the pid we are attaching to is also the tgid, we attach to and
817 stop all the existing threads. Otherwise, we attach to pid and
818 ignore any other threads in the same group as this pid.
820 3) GDB is connecting to gdbserver and is requesting an enumeration of all
822 In this case we want the thread to stop.
823 FIXME: This case is currently not properly handled.
824 We should wait for the SIGSTOP but don't. Things work apparently
825 because enough time passes between when we ptrace (ATTACH) and when
826 gdb makes the next ptrace call on the thread.
828 On the other hand, if we are currently trying to stop all threads, we
829 should treat the new thread as if we had sent it a SIGSTOP. This works
830 because we are guaranteed that the add_lwp call above added us to the
831 end of the list, and so the new thread has not yet reached
832 wait_for_sigstop (but will). */
833 new_lwp
->stop_expected
= 1;
837 linux_attach_lwp (unsigned long lwpid
)
839 linux_attach_lwp_1 (lwpid
, 0);
842 /* Attach to PID. If PID is the tgid, attach to it and all
846 linux_attach (unsigned long pid
)
848 /* Attach to PID. We will check for other threads
850 linux_attach_lwp_1 (pid
, 1);
851 linux_add_process (pid
, 1);
855 struct thread_info
*thread
;
857 /* Don't ignore the initial SIGSTOP if we just attached to this
858 process. It will be collected by wait shortly. */
859 thread
= find_thread_ptid (ptid_build (pid
, pid
, 0));
860 thread
->last_resume_kind
= resume_stop
;
863 if (linux_proc_get_tgid (pid
) == pid
)
868 sprintf (pathname
, "/proc/%ld/task", pid
);
870 dir
= opendir (pathname
);
874 fprintf (stderr
, "Could not open /proc/%ld/task.\n", pid
);
879 /* At this point we attached to the tgid. Scan the task for
882 int new_threads_found
;
886 while (iterations
< 2)
888 new_threads_found
= 0;
889 /* Add all the other threads. While we go through the
890 threads, new threads may be spawned. Cycle through
891 the list of threads until we have done two iterations without
892 finding new threads. */
893 while ((dp
= readdir (dir
)) != NULL
)
896 lwp
= strtoul (dp
->d_name
, NULL
, 10);
898 /* Is this a new thread? */
900 && find_thread_ptid (ptid_build (pid
, lwp
, 0)) == NULL
)
902 linux_attach_lwp_1 (lwp
, 0);
907 Found and attached to new lwp %ld\n", lwp
);
911 if (!new_threads_found
)
932 second_thread_of_pid_p (struct inferior_list_entry
*entry
, void *args
)
934 struct counter
*counter
= args
;
936 if (ptid_get_pid (entry
->id
) == counter
->pid
)
938 if (++counter
->count
> 1)
946 last_thread_of_process_p (struct thread_info
*thread
)
948 ptid_t ptid
= ((struct inferior_list_entry
*)thread
)->id
;
949 int pid
= ptid_get_pid (ptid
);
950 struct counter counter
= { pid
, 0 };
952 return (find_inferior (&all_threads
,
953 second_thread_of_pid_p
, &counter
) == NULL
);
959 linux_kill_one_lwp (struct lwp_info
*lwp
)
961 int pid
= lwpid_of (lwp
);
963 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
964 there is no signal context, and ptrace(PTRACE_KILL) (or
965 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
966 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
967 alternative is to kill with SIGKILL. We only need one SIGKILL
968 per process, not one for each thread. But since we still support
969 linuxthreads, and we also support debugging programs using raw
970 clone without CLONE_THREAD, we send one for each thread. For
971 years, we used PTRACE_KILL only, so we're being a bit paranoid
972 about some old kernels where PTRACE_KILL might work better
973 (dubious if there are any such, but that's why it's paranoia), so
974 we try SIGKILL first, PTRACE_KILL second, and so we're fine
981 "LKL: kill (SIGKILL) %s, 0, 0 (%s)\n",
982 target_pid_to_str (ptid_of (lwp
)),
983 errno
? strerror (errno
) : "OK");
986 ptrace (PTRACE_KILL
, pid
, (PTRACE_ARG3_TYPE
) 0, (PTRACE_ARG4_TYPE
) 0);
989 "LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
990 target_pid_to_str (ptid_of (lwp
)),
991 errno
? strerror (errno
) : "OK");
994 /* Callback for `find_inferior'. Kills an lwp of a given process,
995 except the leader. */
998 kill_one_lwp_callback (struct inferior_list_entry
*entry
, void *args
)
1000 struct thread_info
*thread
= (struct thread_info
*) entry
;
1001 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1003 int pid
= * (int *) args
;
1005 if (ptid_get_pid (entry
->id
) != pid
)
1008 /* We avoid killing the first thread here, because of a Linux kernel (at
1009 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1010 the children get a chance to be reaped, it will remain a zombie
1013 if (lwpid_of (lwp
) == pid
)
1016 fprintf (stderr
, "lkop: is last of process %s\n",
1017 target_pid_to_str (entry
->id
));
1023 linux_kill_one_lwp (lwp
);
1025 /* Make sure it died. The loop is most likely unnecessary. */
1026 pid
= linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
1027 } while (pid
> 0 && WIFSTOPPED (wstat
));
1033 linux_kill (int pid
)
1035 struct process_info
*process
;
1036 struct lwp_info
*lwp
;
1040 process
= find_process_pid (pid
);
1041 if (process
== NULL
)
1044 /* If we're killing a running inferior, make sure it is stopped
1045 first, as PTRACE_KILL will not work otherwise. */
1046 stop_all_lwps (0, NULL
);
1048 find_inferior (&all_threads
, kill_one_lwp_callback
, &pid
);
1050 /* See the comment in linux_kill_one_lwp. We did not kill the first
1051 thread in the list, so do so now. */
1052 lwp
= find_lwp_pid (pid_to_ptid (pid
));
1057 fprintf (stderr
, "lk_1: cannot find lwp %ld, for pid: %d\n",
1058 lwpid_of (lwp
), pid
);
1063 fprintf (stderr
, "lk_1: killing lwp %ld, for pid: %d\n",
1064 lwpid_of (lwp
), pid
);
1068 linux_kill_one_lwp (lwp
);
1070 /* Make sure it died. The loop is most likely unnecessary. */
1071 lwpid
= linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
1072 } while (lwpid
> 0 && WIFSTOPPED (wstat
));
1075 the_target
->mourn (process
);
1077 /* Since we presently can only stop all lwps of all processes, we
1078 need to unstop lwps of other processes. */
1079 unstop_all_lwps (0, NULL
);
1083 /* Get pending signal of THREAD, for detaching purposes. This is the
1084 signal the thread last stopped for, which we need to deliver to the
1085 thread when detaching, otherwise, it'd be suppressed/lost. */
1088 get_detach_signal (struct thread_info
*thread
)
1090 enum gdb_signal signo
= GDB_SIGNAL_0
;
1092 struct lwp_info
*lp
= get_thread_lwp (thread
);
1094 if (lp
->status_pending_p
)
1095 status
= lp
->status_pending
;
1098 /* If the thread had been suspended by gdbserver, and it stopped
1099 cleanly, then it'll have stopped with SIGSTOP. But we don't
1100 want to deliver that SIGSTOP. */
1101 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1102 || thread
->last_status
.value
.sig
== GDB_SIGNAL_0
)
1105 /* Otherwise, we may need to deliver the signal we
1107 status
= lp
->last_status
;
1110 if (!WIFSTOPPED (status
))
1114 "GPS: lwp %s hasn't stopped: no pending signal\n",
1115 target_pid_to_str (ptid_of (lp
)));
1119 /* Extended wait statuses aren't real SIGTRAPs. */
1120 if (WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1124 "GPS: lwp %s had stopped with extended "
1125 "status: no pending signal\n",
1126 target_pid_to_str (ptid_of (lp
)));
1130 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1132 if (program_signals_p
&& !program_signals
[signo
])
1136 "GPS: lwp %s had signal %s, but it is in nopass state\n",
1137 target_pid_to_str (ptid_of (lp
)),
1138 gdb_signal_to_string (signo
));
1141 else if (!program_signals_p
1142 /* If we have no way to know which signals GDB does not
1143 want to have passed to the program, assume
1144 SIGTRAP/SIGINT, which is GDB's default. */
1145 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1149 "GPS: lwp %s had signal %s, "
1150 "but we don't know if we should pass it. Default to not.\n",
1151 target_pid_to_str (ptid_of (lp
)),
1152 gdb_signal_to_string (signo
));
1159 "GPS: lwp %s has pending signal %s: delivering it.\n",
1160 target_pid_to_str (ptid_of (lp
)),
1161 gdb_signal_to_string (signo
));
1163 return WSTOPSIG (status
);
1168 linux_detach_one_lwp (struct inferior_list_entry
*entry
, void *args
)
1170 struct thread_info
*thread
= (struct thread_info
*) entry
;
1171 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1172 int pid
= * (int *) args
;
1175 if (ptid_get_pid (entry
->id
) != pid
)
1178 /* If there is a pending SIGSTOP, get rid of it. */
1179 if (lwp
->stop_expected
)
1183 "Sending SIGCONT to %s\n",
1184 target_pid_to_str (ptid_of (lwp
)));
1186 kill_lwp (lwpid_of (lwp
), SIGCONT
);
1187 lwp
->stop_expected
= 0;
1190 /* Flush any pending changes to the process's registers. */
1191 regcache_invalidate_thread (get_lwp_thread (lwp
));
1193 /* Pass on any pending signal for this thread. */
1194 sig
= get_detach_signal (thread
);
1196 /* Finally, let it resume. */
1197 if (the_low_target
.prepare_to_resume
!= NULL
)
1198 the_low_target
.prepare_to_resume (lwp
);
1199 if (ptrace (PTRACE_DETACH
, lwpid_of (lwp
), (PTRACE_ARG3_TYPE
) 0,
1200 (PTRACE_ARG4_TYPE
) (long) sig
) < 0)
1201 error (_("Can't detach %s: %s"),
1202 target_pid_to_str (ptid_of (lwp
)),
1210 linux_detach (int pid
)
1212 struct process_info
*process
;
1214 process
= find_process_pid (pid
);
1215 if (process
== NULL
)
1218 /* Stop all threads before detaching. First, ptrace requires that
1219 the thread is stopped to sucessfully detach. Second, thread_db
1220 may need to uninstall thread event breakpoints from memory, which
1221 only works with a stopped process anyway. */
1222 stop_all_lwps (0, NULL
);
1224 #ifdef USE_THREAD_DB
1225 thread_db_detach (process
);
1228 /* Stabilize threads (move out of jump pads). */
1229 stabilize_threads ();
1231 find_inferior (&all_threads
, linux_detach_one_lwp
, &pid
);
1233 the_target
->mourn (process
);
1235 /* Since we presently can only stop all lwps of all processes, we
1236 need to unstop lwps of other processes. */
1237 unstop_all_lwps (0, NULL
);
1241 /* Remove all LWPs that belong to process PROC from the lwp list. */
1244 delete_lwp_callback (struct inferior_list_entry
*entry
, void *proc
)
1246 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
1247 struct process_info
*process
= proc
;
1249 if (pid_of (lwp
) == pid_of (process
))
1256 linux_mourn (struct process_info
*process
)
1258 struct process_info_private
*priv
;
1260 #ifdef USE_THREAD_DB
1261 thread_db_mourn (process
);
1264 find_inferior (&all_lwps
, delete_lwp_callback
, process
);
1266 /* Freeing all private data. */
1267 priv
= process
->private;
1268 free (priv
->arch_private
);
1270 process
->private = NULL
;
1272 remove_process (process
);
1276 linux_join (int pid
)
1281 ret
= my_waitpid (pid
, &status
, 0);
1282 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1284 } while (ret
!= -1 || errno
!= ECHILD
);
1287 /* Return nonzero if the given thread is still alive. */
1289 linux_thread_alive (ptid_t ptid
)
1291 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1293 /* We assume we always know if a thread exits. If a whole process
1294 exited but we still haven't been able to report it to GDB, we'll
1295 hold on to the last lwp of the dead process. */
1302 /* Return 1 if this lwp has an interesting status pending. */
1304 status_pending_p_callback (struct inferior_list_entry
*entry
, void *arg
)
1306 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
1307 ptid_t ptid
= * (ptid_t
*) arg
;
1308 struct thread_info
*thread
;
1310 /* Check if we're only interested in events from a specific process
1312 if (!ptid_equal (minus_one_ptid
, ptid
)
1313 && ptid_get_pid (ptid
) != ptid_get_pid (lwp
->head
.id
))
1316 thread
= get_lwp_thread (lwp
);
1318 /* If we got a `vCont;t', but we haven't reported a stop yet, do
1319 report any status pending the LWP may have. */
1320 if (thread
->last_resume_kind
== resume_stop
1321 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1324 return lwp
->status_pending_p
;
1328 same_lwp (struct inferior_list_entry
*entry
, void *data
)
1330 ptid_t ptid
= *(ptid_t
*) data
;
1333 if (ptid_get_lwp (ptid
) != 0)
1334 lwp
= ptid_get_lwp (ptid
);
1336 lwp
= ptid_get_pid (ptid
);
1338 if (ptid_get_lwp (entry
->id
) == lwp
)
1345 find_lwp_pid (ptid_t ptid
)
1347 return (struct lwp_info
*) find_inferior (&all_lwps
, same_lwp
, &ptid
);
1350 static struct lwp_info
*
1351 linux_wait_for_lwp (ptid_t ptid
, int *wstatp
, int options
)
1354 int to_wait_for
= -1;
1355 struct lwp_info
*child
= NULL
;
1358 fprintf (stderr
, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid
));
1360 if (ptid_equal (ptid
, minus_one_ptid
))
1361 to_wait_for
= -1; /* any child */
1363 to_wait_for
= ptid_get_lwp (ptid
); /* this lwp only */
1369 ret
= my_waitpid (to_wait_for
, wstatp
, options
);
1370 if (ret
== 0 || (ret
== -1 && errno
== ECHILD
&& (options
& WNOHANG
)))
1373 perror_with_name ("waitpid");
1376 && (!WIFSTOPPED (*wstatp
)
1377 || (WSTOPSIG (*wstatp
) != 32
1378 && WSTOPSIG (*wstatp
) != 33)))
1379 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
1381 child
= find_lwp_pid (pid_to_ptid (ret
));
1383 /* If we didn't find a process, one of two things presumably happened:
1384 - A process we started and then detached from has exited. Ignore it.
1385 - A process we are controlling has forked and the new child's stop
1386 was reported to us by the kernel. Save its PID. */
1387 if (child
== NULL
&& WIFSTOPPED (*wstatp
))
1389 add_to_pid_list (&stopped_pids
, ret
, *wstatp
);
1392 else if (child
== NULL
)
1397 child
->last_status
= *wstatp
;
1399 if (WIFSTOPPED (*wstatp
))
1401 struct process_info
*proc
;
1403 /* Architecture-specific setup after inferior is running. This
1404 needs to happen after we have attached to the inferior and it
1405 is stopped for the first time, but before we access any
1406 inferior registers. */
1407 proc
= find_process_pid (pid_of (child
));
1408 if (proc
->private->new_inferior
)
1410 struct thread_info
*saved_inferior
;
1412 saved_inferior
= current_inferior
;
1413 current_inferior
= get_lwp_thread (child
);
1415 the_low_target
.arch_setup ();
1417 current_inferior
= saved_inferior
;
1419 proc
->private->new_inferior
= 0;
1423 /* Fetch the possibly triggered data watchpoint info and store it in
1426 On some archs, like x86, that use debug registers to set
1427 watchpoints, it's possible that the way to know which watched
1428 address trapped, is to check the register that is used to select
1429 which address to watch. Problem is, between setting the
1430 watchpoint and reading back which data address trapped, the user
1431 may change the set of watchpoints, and, as a consequence, GDB
1432 changes the debug registers in the inferior. To avoid reading
1433 back a stale stopped-data-address when that happens, we cache in
1434 LP the fact that a watchpoint trapped, and the corresponding data
1435 address, as soon as we see CHILD stop with a SIGTRAP. If GDB
1436 changes the debug registers meanwhile, we have the cached data we
1439 if (WIFSTOPPED (*wstatp
) && WSTOPSIG (*wstatp
) == SIGTRAP
)
1441 if (the_low_target
.stopped_by_watchpoint
== NULL
)
1443 child
->stopped_by_watchpoint
= 0;
1447 struct thread_info
*saved_inferior
;
1449 saved_inferior
= current_inferior
;
1450 current_inferior
= get_lwp_thread (child
);
1452 child
->stopped_by_watchpoint
1453 = the_low_target
.stopped_by_watchpoint ();
1455 if (child
->stopped_by_watchpoint
)
1457 if (the_low_target
.stopped_data_address
!= NULL
)
1458 child
->stopped_data_address
1459 = the_low_target
.stopped_data_address ();
1461 child
->stopped_data_address
= 0;
1464 current_inferior
= saved_inferior
;
1468 /* Store the STOP_PC, with adjustment applied. This depends on the
1469 architecture being defined already (so that CHILD has a valid
1470 regcache), and on LAST_STATUS being set (to check for SIGTRAP or
1472 if (WIFSTOPPED (*wstatp
))
1473 child
->stop_pc
= get_stop_pc (child
);
1476 && WIFSTOPPED (*wstatp
)
1477 && the_low_target
.get_pc
!= NULL
)
1479 struct thread_info
*saved_inferior
= current_inferior
;
1480 struct regcache
*regcache
;
1483 current_inferior
= get_lwp_thread (child
);
1484 regcache
= get_thread_regcache (current_inferior
, 1);
1485 pc
= (*the_low_target
.get_pc
) (regcache
);
1486 fprintf (stderr
, "linux_wait_for_lwp: pc is 0x%lx\n", (long) pc
);
1487 current_inferior
= saved_inferior
;
1493 /* This function should only be called if the LWP got a SIGTRAP.
1495 Handle any tracepoint steps or hits. Return true if a tracepoint
1496 event was handled, 0 otherwise. */
1499 handle_tracepoints (struct lwp_info
*lwp
)
1501 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1502 int tpoint_related_event
= 0;
1504 /* If this tracepoint hit causes a tracing stop, we'll immediately
1505 uninsert tracepoints. To do this, we temporarily pause all
1506 threads, unpatch away, and then unpause threads. We need to make
1507 sure the unpausing doesn't resume LWP too. */
1510 /* And we need to be sure that any all-threads-stopping doesn't try
1511 to move threads out of the jump pads, as it could deadlock the
1512 inferior (LWP could be in the jump pad, maybe even holding the
1515 /* Do any necessary step collect actions. */
1516 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1518 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1520 /* See if we just hit a tracepoint and do its main collect
1522 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1526 gdb_assert (lwp
->suspended
== 0);
1527 gdb_assert (!stabilizing_threads
|| lwp
->collecting_fast_tracepoint
);
1529 if (tpoint_related_event
)
1532 fprintf (stderr
, "got a tracepoint event\n");
1539 /* Convenience wrapper. Returns true if LWP is presently collecting a
1543 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
1544 struct fast_tpoint_collect_status
*status
)
1546 CORE_ADDR thread_area
;
1548 if (the_low_target
.get_thread_area
== NULL
)
1551 /* Get the thread area address. This is used to recognize which
1552 thread is which when tracing with the in-process agent library.
1553 We don't read anything from the address, and treat it as opaque;
1554 it's the address itself that we assume is unique per-thread. */
1555 if ((*the_low_target
.get_thread_area
) (lwpid_of (lwp
), &thread_area
) == -1)
1558 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
1561 /* The reason we resume in the caller, is because we want to be able
1562 to pass lwp->status_pending as WSTAT, and we need to clear
1563 status_pending_p before resuming, otherwise, linux_resume_one_lwp
1564 refuses to resume. */
1567 maybe_move_out_of_jump_pad (struct lwp_info
*lwp
, int *wstat
)
1569 struct thread_info
*saved_inferior
;
1571 saved_inferior
= current_inferior
;
1572 current_inferior
= get_lwp_thread (lwp
);
1575 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
1576 && supports_fast_tracepoints ()
1577 && agent_loaded_p ())
1579 struct fast_tpoint_collect_status status
;
1584 Checking whether LWP %ld needs to move out of the jump pad.\n",
1587 r
= linux_fast_tracepoint_collecting (lwp
, &status
);
1590 || (WSTOPSIG (*wstat
) != SIGILL
1591 && WSTOPSIG (*wstat
) != SIGFPE
1592 && WSTOPSIG (*wstat
) != SIGSEGV
1593 && WSTOPSIG (*wstat
) != SIGBUS
))
1595 lwp
->collecting_fast_tracepoint
= r
;
1599 if (r
== 1 && lwp
->exit_jump_pad_bkpt
== NULL
)
1601 /* Haven't executed the original instruction yet.
1602 Set breakpoint there, and wait till it's hit,
1603 then single-step until exiting the jump pad. */
1604 lwp
->exit_jump_pad_bkpt
1605 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
1610 Checking whether LWP %ld needs to move out of the jump pad...it does\n",
1612 current_inferior
= saved_inferior
;
1619 /* If we get a synchronous signal while collecting, *and*
1620 while executing the (relocated) original instruction,
1621 reset the PC to point at the tpoint address, before
1622 reporting to GDB. Otherwise, it's an IPA lib bug: just
1623 report the signal to GDB, and pray for the best. */
1625 lwp
->collecting_fast_tracepoint
= 0;
1628 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
1629 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
1632 struct regcache
*regcache
;
1634 /* The si_addr on a few signals references the address
1635 of the faulting instruction. Adjust that as
1637 if ((WSTOPSIG (*wstat
) == SIGILL
1638 || WSTOPSIG (*wstat
) == SIGFPE
1639 || WSTOPSIG (*wstat
) == SIGBUS
1640 || WSTOPSIG (*wstat
) == SIGSEGV
)
1641 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
),
1642 (PTRACE_ARG3_TYPE
) 0, &info
) == 0
1643 /* Final check just to make sure we don't clobber
1644 the siginfo of non-kernel-sent signals. */
1645 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
1647 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
1648 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
),
1649 (PTRACE_ARG3_TYPE
) 0, &info
);
1652 regcache
= get_thread_regcache (get_lwp_thread (lwp
), 1);
1653 (*the_low_target
.set_pc
) (regcache
, status
.tpoint_addr
);
1654 lwp
->stop_pc
= status
.tpoint_addr
;
1656 /* Cancel any fast tracepoint lock this thread was
1658 force_unlock_trace_buffer ();
1661 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
1665 "Cancelling fast exit-jump-pad: removing bkpt. "
1666 "stopping all threads momentarily.\n");
1668 stop_all_lwps (1, lwp
);
1669 cancel_breakpoints ();
1671 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
1672 lwp
->exit_jump_pad_bkpt
= NULL
;
1674 unstop_all_lwps (1, lwp
);
1676 gdb_assert (lwp
->suspended
>= 0);
1683 Checking whether LWP %ld needs to move out of the jump pad...no\n",
1686 current_inferior
= saved_inferior
;
1690 /* Enqueue one signal in the "signals to report later when out of the
1694 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1696 struct pending_signals
*p_sig
;
1700 Deferring signal %d for LWP %ld.\n", WSTOPSIG (*wstat
), lwpid_of (lwp
));
1704 struct pending_signals
*sig
;
1706 for (sig
= lwp
->pending_signals_to_report
;
1710 " Already queued %d\n",
1713 fprintf (stderr
, " (no more currently queued signals)\n");
1716 /* Don't enqueue non-RT signals if they are already in the deferred
1717 queue. (SIGSTOP being the easiest signal to see ending up here
1719 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
1721 struct pending_signals
*sig
;
1723 for (sig
= lwp
->pending_signals_to_report
;
1727 if (sig
->signal
== WSTOPSIG (*wstat
))
1731 "Not requeuing already queued non-RT signal %d"
1740 p_sig
= xmalloc (sizeof (*p_sig
));
1741 p_sig
->prev
= lwp
->pending_signals_to_report
;
1742 p_sig
->signal
= WSTOPSIG (*wstat
);
1743 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1744 ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
), (PTRACE_ARG3_TYPE
) 0,
1747 lwp
->pending_signals_to_report
= p_sig
;
1750 /* Dequeue one signal from the "signals to report later when out of
1751 the jump pad" list. */
1754 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1756 if (lwp
->pending_signals_to_report
!= NULL
)
1758 struct pending_signals
**p_sig
;
1760 p_sig
= &lwp
->pending_signals_to_report
;
1761 while ((*p_sig
)->prev
!= NULL
)
1762 p_sig
= &(*p_sig
)->prev
;
1764 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
1765 if ((*p_sig
)->info
.si_signo
!= 0)
1766 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
), (PTRACE_ARG3_TYPE
) 0,
1772 fprintf (stderr
, "Reporting deferred signal %d for LWP %ld.\n",
1773 WSTOPSIG (*wstat
), lwpid_of (lwp
));
1777 struct pending_signals
*sig
;
1779 for (sig
= lwp
->pending_signals_to_report
;
1783 " Still queued %d\n",
1786 fprintf (stderr
, " (no more queued signals)\n");
1795 /* Arrange for a breakpoint to be hit again later. We don't keep the
1796 SIGTRAP status and don't forward the SIGTRAP signal to the LWP. We
1797 will handle the current event, eventually we will resume this LWP,
1798 and this breakpoint will trap again. */
1801 cancel_breakpoint (struct lwp_info
*lwp
)
1803 struct thread_info
*saved_inferior
;
1805 /* There's nothing to do if we don't support breakpoints. */
1806 if (!supports_breakpoints ())
1809 /* breakpoint_at reads from current inferior. */
1810 saved_inferior
= current_inferior
;
1811 current_inferior
= get_lwp_thread (lwp
);
1813 if ((*the_low_target
.breakpoint_at
) (lwp
->stop_pc
))
1817 "CB: Push back breakpoint for %s\n",
1818 target_pid_to_str (ptid_of (lwp
)));
1820 /* Back up the PC if necessary. */
1821 if (the_low_target
.decr_pc_after_break
)
1823 struct regcache
*regcache
1824 = get_thread_regcache (current_inferior
, 1);
1825 (*the_low_target
.set_pc
) (regcache
, lwp
->stop_pc
);
1828 current_inferior
= saved_inferior
;
1835 "CB: No breakpoint found at %s for [%s]\n",
1836 paddress (lwp
->stop_pc
),
1837 target_pid_to_str (ptid_of (lwp
)));
1840 current_inferior
= saved_inferior
;
1844 /* When the event-loop is doing a step-over, this points at the thread
1846 ptid_t step_over_bkpt
;
1848 /* Wait for an event from child PID. If PID is -1, wait for any
1849 child. Store the stop status through the status pointer WSTAT.
1850 OPTIONS is passed to the waitpid call. Return 0 if no child stop
1851 event was found and OPTIONS contains WNOHANG. Return the PID of
1852 the stopped child otherwise. */
1855 linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
)
1857 struct lwp_info
*event_child
, *requested_child
;
1861 requested_child
= NULL
;
1863 /* Check for a lwp with a pending status. */
1865 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
1867 event_child
= (struct lwp_info
*)
1868 find_inferior (&all_lwps
, status_pending_p_callback
, &ptid
);
1869 if (debug_threads
&& event_child
)
1870 fprintf (stderr
, "Got a pending child %ld\n", lwpid_of (event_child
));
1874 requested_child
= find_lwp_pid (ptid
);
1876 if (stopping_threads
== NOT_STOPPING_THREADS
1877 && requested_child
->status_pending_p
1878 && requested_child
->collecting_fast_tracepoint
)
1880 enqueue_one_deferred_signal (requested_child
,
1881 &requested_child
->status_pending
);
1882 requested_child
->status_pending_p
= 0;
1883 requested_child
->status_pending
= 0;
1884 linux_resume_one_lwp (requested_child
, 0, 0, NULL
);
1887 if (requested_child
->suspended
1888 && requested_child
->status_pending_p
)
1889 fatal ("requesting an event out of a suspended child?");
1891 if (requested_child
->status_pending_p
)
1892 event_child
= requested_child
;
1895 if (event_child
!= NULL
)
1898 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
1899 lwpid_of (event_child
), event_child
->status_pending
);
1900 *wstat
= event_child
->status_pending
;
1901 event_child
->status_pending_p
= 0;
1902 event_child
->status_pending
= 0;
1903 current_inferior
= get_lwp_thread (event_child
);
1904 return lwpid_of (event_child
);
1907 if (ptid_is_pid (ptid
))
1909 /* A request to wait for a specific tgid. This is not possible
1910 with waitpid, so instead, we wait for any child, and leave
1911 children we're not interested in right now with a pending
1912 status to report later. */
1913 wait_ptid
= minus_one_ptid
;
1918 /* We only enter this loop if no process has a pending wait status. Thus
1919 any action taken in response to a wait status inside this loop is
1920 responding as soon as we detect the status, not after any pending
1924 event_child
= linux_wait_for_lwp (wait_ptid
, wstat
, options
);
1926 if ((options
& WNOHANG
) && event_child
== NULL
)
1929 fprintf (stderr
, "WNOHANG set, no event found\n");
1933 if (event_child
== NULL
)
1934 error ("event from unknown child");
1936 if (ptid_is_pid (ptid
)
1937 && ptid_get_pid (ptid
) != ptid_get_pid (ptid_of (event_child
)))
1939 if (! WIFSTOPPED (*wstat
))
1940 mark_lwp_dead (event_child
, *wstat
);
1943 event_child
->status_pending_p
= 1;
1944 event_child
->status_pending
= *wstat
;
1949 current_inferior
= get_lwp_thread (event_child
);
1951 /* Check for thread exit. */
1952 if (! WIFSTOPPED (*wstat
))
1955 fprintf (stderr
, "LWP %ld exiting\n", lwpid_of (event_child
));
1957 /* If the last thread is exiting, just return. */
1958 if (last_thread_of_process_p (current_inferior
))
1961 fprintf (stderr
, "LWP %ld is last lwp of process\n",
1962 lwpid_of (event_child
));
1963 return lwpid_of (event_child
);
1968 current_inferior
= (struct thread_info
*) all_threads
.head
;
1970 fprintf (stderr
, "Current inferior is now %ld\n",
1971 lwpid_of (get_thread_lwp (current_inferior
)));
1975 current_inferior
= NULL
;
1977 fprintf (stderr
, "Current inferior is now <NULL>\n");
1980 /* If we were waiting for this particular child to do something...
1981 well, it did something. */
1982 if (requested_child
!= NULL
)
1984 int lwpid
= lwpid_of (event_child
);
1986 /* Cancel the step-over operation --- the thread that
1987 started it is gone. */
1988 if (finish_step_over (event_child
))
1989 unstop_all_lwps (1, event_child
);
1990 delete_lwp (event_child
);
1994 delete_lwp (event_child
);
1996 /* Wait for a more interesting event. */
2000 if (event_child
->must_set_ptrace_flags
)
2002 linux_enable_event_reporting (lwpid_of (event_child
));
2003 event_child
->must_set_ptrace_flags
= 0;
2006 if (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) == SIGTRAP
2007 && *wstat
>> 16 != 0)
2009 handle_extended_wait (event_child
, *wstat
);
2013 if (WIFSTOPPED (*wstat
)
2014 && WSTOPSIG (*wstat
) == SIGSTOP
2015 && event_child
->stop_expected
)
2020 fprintf (stderr
, "Expected stop.\n");
2021 event_child
->stop_expected
= 0;
2023 should_stop
= (current_inferior
->last_resume_kind
== resume_stop
2024 || stopping_threads
!= NOT_STOPPING_THREADS
);
2028 linux_resume_one_lwp (event_child
,
2029 event_child
->stepping
, 0, NULL
);
2034 return lwpid_of (event_child
);
2041 /* Count the LWP's that have had events. */
2044 count_events_callback (struct inferior_list_entry
*entry
, void *data
)
2046 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
2047 struct thread_info
*thread
= get_lwp_thread (lp
);
2050 gdb_assert (count
!= NULL
);
2052 /* Count only resumed LWPs that have a SIGTRAP event pending that
2053 should be reported to GDB. */
2054 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2055 && thread
->last_resume_kind
!= resume_stop
2056 && lp
->status_pending_p
2057 && WIFSTOPPED (lp
->status_pending
)
2058 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
2059 && !breakpoint_inserted_here (lp
->stop_pc
))
2065 /* Select the LWP (if any) that is currently being single-stepped. */
2068 select_singlestep_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
2070 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
2071 struct thread_info
*thread
= get_lwp_thread (lp
);
2073 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2074 && thread
->last_resume_kind
== resume_step
2075 && lp
->status_pending_p
)
2081 /* Select the Nth LWP that has had a SIGTRAP event that should be
2085 select_event_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
2087 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
2088 struct thread_info
*thread
= get_lwp_thread (lp
);
2089 int *selector
= data
;
2091 gdb_assert (selector
!= NULL
);
2093 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2094 if (thread
->last_resume_kind
!= resume_stop
2095 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2096 && lp
->status_pending_p
2097 && WIFSTOPPED (lp
->status_pending
)
2098 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
2099 && !breakpoint_inserted_here (lp
->stop_pc
))
2100 if ((*selector
)-- == 0)
2107 cancel_breakpoints_callback (struct inferior_list_entry
*entry
, void *data
)
2109 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
2110 struct thread_info
*thread
= get_lwp_thread (lp
);
2111 struct lwp_info
*event_lp
= data
;
2113 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2117 /* If a LWP other than the LWP that we're reporting an event for has
2118 hit a GDB breakpoint (as opposed to some random trap signal),
2119 then just arrange for it to hit it again later. We don't keep
2120 the SIGTRAP status and don't forward the SIGTRAP signal to the
2121 LWP. We will handle the current event, eventually we will resume
2122 all LWPs, and this one will get its breakpoint trap again.
2124 If we do not do this, then we run the risk that the user will
2125 delete or disable the breakpoint, but the LWP will have already
2128 if (thread
->last_resume_kind
!= resume_stop
2129 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2130 && lp
->status_pending_p
2131 && WIFSTOPPED (lp
->status_pending
)
2132 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
2134 && !lp
->stopped_by_watchpoint
2135 && cancel_breakpoint (lp
))
2136 /* Throw away the SIGTRAP. */
2137 lp
->status_pending_p
= 0;
2143 linux_cancel_breakpoints (void)
2145 find_inferior (&all_lwps
, cancel_breakpoints_callback
, NULL
);
2148 /* Select one LWP out of those that have events pending. */
2151 select_event_lwp (struct lwp_info
**orig_lp
)
2154 int random_selector
;
2155 struct lwp_info
*event_lp
;
2157 /* Give preference to any LWP that is being single-stepped. */
2159 = (struct lwp_info
*) find_inferior (&all_lwps
,
2160 select_singlestep_lwp_callback
, NULL
);
2161 if (event_lp
!= NULL
)
2165 "SEL: Select single-step %s\n",
2166 target_pid_to_str (ptid_of (event_lp
)));
2170 /* No single-stepping LWP. Select one at random, out of those
2171 which have had SIGTRAP events. */
2173 /* First see how many SIGTRAP events we have. */
2174 find_inferior (&all_lwps
, count_events_callback
, &num_events
);
2176 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2177 random_selector
= (int)
2178 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2180 if (debug_threads
&& num_events
> 1)
2182 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2183 num_events
, random_selector
);
2185 event_lp
= (struct lwp_info
*) find_inferior (&all_lwps
,
2186 select_event_lwp_callback
,
2190 if (event_lp
!= NULL
)
2192 /* Switch the event LWP. */
2193 *orig_lp
= event_lp
;
2197 /* Decrement the suspend count of an LWP. */
2200 unsuspend_one_lwp (struct inferior_list_entry
*entry
, void *except
)
2202 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2204 /* Ignore EXCEPT. */
2210 gdb_assert (lwp
->suspended
>= 0);
2214 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2218 unsuspend_all_lwps (struct lwp_info
*except
)
2220 find_inferior (&all_lwps
, unsuspend_one_lwp
, except
);
2223 static void move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
);
2224 static int stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
,
2226 static int lwp_running (struct inferior_list_entry
*entry
, void *data
);
2227 static ptid_t
linux_wait_1 (ptid_t ptid
,
2228 struct target_waitstatus
*ourstatus
,
2229 int target_options
);
2231 /* Stabilize threads (move out of jump pads).
2233 If a thread is midway collecting a fast tracepoint, we need to
2234 finish the collection and move it out of the jump pad before
2235 reporting the signal.
2237 This avoids recursion while collecting (when a signal arrives
2238 midway, and the signal handler itself collects), which would trash
2239 the trace buffer. In case the user set a breakpoint in a signal
2240 handler, this avoids the backtrace showing the jump pad, etc..
2241 Most importantly, there are certain things we can't do safely if
2242 threads are stopped in a jump pad (or in its callee's). For
2245 - starting a new trace run. A thread still collecting the
2246 previous run, could trash the trace buffer when resumed. The trace
2247 buffer control structures would have been reset but the thread had
2248 no way to tell. The thread could even midway memcpy'ing to the
2249 buffer, which would mean that when resumed, it would clobber the
2250 trace buffer that had been set for a new run.
2252 - we can't rewrite/reuse the jump pads for new tracepoints
2253 safely. Say you do tstart while a thread is stopped midway while
2254 collecting. When the thread is later resumed, it finishes the
2255 collection, and returns to the jump pad, to execute the original
2256 instruction that was under the tracepoint jump at the time the
2257 older run had been started. If the jump pad had been rewritten
2258 since for something else in the new run, the thread would now
2259 execute the wrong / random instructions. */
2262 linux_stabilize_threads (void)
2264 struct thread_info
*save_inferior
;
2265 struct lwp_info
*lwp_stuck
;
2268 = (struct lwp_info
*) find_inferior (&all_lwps
,
2269 stuck_in_jump_pad_callback
, NULL
);
2270 if (lwp_stuck
!= NULL
)
2273 fprintf (stderr
, "can't stabilize, LWP %ld is stuck in jump pad\n",
2274 lwpid_of (lwp_stuck
));
2278 save_inferior
= current_inferior
;
2280 stabilizing_threads
= 1;
2283 for_each_inferior (&all_lwps
, move_out_of_jump_pad_callback
);
2285 /* Loop until all are stopped out of the jump pads. */
2286 while (find_inferior (&all_lwps
, lwp_running
, NULL
) != NULL
)
2288 struct target_waitstatus ourstatus
;
2289 struct lwp_info
*lwp
;
2292 /* Note that we go through the full wait even loop. While
2293 moving threads out of jump pad, we need to be able to step
2294 over internal breakpoints and such. */
2295 linux_wait_1 (minus_one_ptid
, &ourstatus
, 0);
2297 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
2299 lwp
= get_thread_lwp (current_inferior
);
2304 if (ourstatus
.value
.sig
!= GDB_SIGNAL_0
2305 || current_inferior
->last_resume_kind
== resume_stop
)
2307 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.value
.sig
));
2308 enqueue_one_deferred_signal (lwp
, &wstat
);
2313 find_inferior (&all_lwps
, unsuspend_one_lwp
, NULL
);
2315 stabilizing_threads
= 0;
2317 current_inferior
= save_inferior
;
2322 = (struct lwp_info
*) find_inferior (&all_lwps
,
2323 stuck_in_jump_pad_callback
, NULL
);
2324 if (lwp_stuck
!= NULL
)
2325 fprintf (stderr
, "couldn't stabilize, LWP %ld got stuck in jump pad\n",
2326 lwpid_of (lwp_stuck
));
2330 /* Wait for process, returns status. */
2333 linux_wait_1 (ptid_t ptid
,
2334 struct target_waitstatus
*ourstatus
, int target_options
)
2337 struct lwp_info
*event_child
;
2340 int step_over_finished
;
2341 int bp_explains_trap
;
2342 int maybe_internal_trap
;
2347 /* Translate generic target options into linux options. */
2349 if (target_options
& TARGET_WNOHANG
)
2353 bp_explains_trap
= 0;
2356 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2358 /* If we were only supposed to resume one thread, only wait for
2359 that thread - if it's still alive. If it died, however - which
2360 can happen if we're coming from the thread death case below -
2361 then we need to make sure we restart the other threads. We could
2362 pick a thread at random or restart all; restarting all is less
2365 && !ptid_equal (cont_thread
, null_ptid
)
2366 && !ptid_equal (cont_thread
, minus_one_ptid
))
2368 struct thread_info
*thread
;
2370 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
2373 /* No stepping, no signal - unless one is pending already, of course. */
2376 struct thread_resume resume_info
;
2377 resume_info
.thread
= minus_one_ptid
;
2378 resume_info
.kind
= resume_continue
;
2379 resume_info
.sig
= 0;
2380 linux_resume (&resume_info
, 1);
2386 if (ptid_equal (step_over_bkpt
, null_ptid
))
2387 pid
= linux_wait_for_event (ptid
, &w
, options
);
2391 fprintf (stderr
, "step_over_bkpt set [%s], doing a blocking wait\n",
2392 target_pid_to_str (step_over_bkpt
));
2393 pid
= linux_wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
2396 if (pid
== 0) /* only if TARGET_WNOHANG */
2399 event_child
= get_thread_lwp (current_inferior
);
2401 /* If we are waiting for a particular child, and it exited,
2402 linux_wait_for_event will return its exit status. Similarly if
2403 the last child exited. If this is not the last child, however,
2404 do not report it as exited until there is a 'thread exited' response
2405 available in the remote protocol. Instead, just wait for another event.
2406 This should be safe, because if the thread crashed we will already
2407 have reported the termination signal to GDB; that should stop any
2408 in-progress stepping operations, etc.
2410 Report the exit status of the last thread to exit. This matches
2411 LinuxThreads' behavior. */
2413 if (last_thread_of_process_p (current_inferior
))
2415 if (WIFEXITED (w
) || WIFSIGNALED (w
))
2419 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
2420 ourstatus
->value
.integer
= WEXITSTATUS (w
);
2424 "\nChild exited with retcode = %x \n",
2429 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
2430 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (w
));
2434 "\nChild terminated with signal = %x \n",
2439 return ptid_of (event_child
);
2444 if (!WIFSTOPPED (w
))
2448 /* If this event was not handled before, and is not a SIGTRAP, we
2449 report it. SIGILL and SIGSEGV are also treated as traps in case
2450 a breakpoint is inserted at the current PC. If this target does
2451 not support internal breakpoints at all, we also report the
2452 SIGTRAP without further processing; it's of no concern to us. */
2454 = (supports_breakpoints ()
2455 && (WSTOPSIG (w
) == SIGTRAP
2456 || ((WSTOPSIG (w
) == SIGILL
2457 || WSTOPSIG (w
) == SIGSEGV
)
2458 && (*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))));
2460 if (maybe_internal_trap
)
2462 /* Handle anything that requires bookkeeping before deciding to
2463 report the event or continue waiting. */
2465 /* First check if we can explain the SIGTRAP with an internal
2466 breakpoint, or if we should possibly report the event to GDB.
2467 Do this before anything that may remove or insert a
2469 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
2471 /* We have a SIGTRAP, possibly a step-over dance has just
2472 finished. If so, tweak the state machine accordingly,
2473 reinsert breakpoints and delete any reinsert (software
2474 single-step) breakpoints. */
2475 step_over_finished
= finish_step_over (event_child
);
2477 /* Now invoke the callbacks of any internal breakpoints there. */
2478 check_breakpoints (event_child
->stop_pc
);
2480 /* Handle tracepoint data collecting. This may overflow the
2481 trace buffer, and cause a tracing stop, removing
2483 trace_event
= handle_tracepoints (event_child
);
2485 if (bp_explains_trap
)
2487 /* If we stepped or ran into an internal breakpoint, we've
2488 already handled it. So next time we resume (from this
2489 PC), we should step over it. */
2491 fprintf (stderr
, "Hit a gdbserver breakpoint.\n");
2493 if (breakpoint_here (event_child
->stop_pc
))
2494 event_child
->need_step_over
= 1;
2499 /* We have some other signal, possibly a step-over dance was in
2500 progress, and it should be cancelled too. */
2501 step_over_finished
= finish_step_over (event_child
);
2504 /* We have all the data we need. Either report the event to GDB, or
2505 resume threads and keep waiting for more. */
2507 /* If we're collecting a fast tracepoint, finish the collection and
2508 move out of the jump pad before delivering a signal. See
2509 linux_stabilize_threads. */
2512 && WSTOPSIG (w
) != SIGTRAP
2513 && supports_fast_tracepoints ()
2514 && agent_loaded_p ())
2518 "Got signal %d for LWP %ld. Check if we need "
2519 "to defer or adjust it.\n",
2520 WSTOPSIG (w
), lwpid_of (event_child
));
2522 /* Allow debugging the jump pad itself. */
2523 if (current_inferior
->last_resume_kind
!= resume_step
2524 && maybe_move_out_of_jump_pad (event_child
, &w
))
2526 enqueue_one_deferred_signal (event_child
, &w
);
2530 "Signal %d for LWP %ld deferred (in jump pad)\n",
2531 WSTOPSIG (w
), lwpid_of (event_child
));
2533 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
2538 if (event_child
->collecting_fast_tracepoint
)
2542 LWP %ld was trying to move out of the jump pad (%d). \
2543 Check if we're already there.\n",
2544 lwpid_of (event_child
),
2545 event_child
->collecting_fast_tracepoint
);
2549 event_child
->collecting_fast_tracepoint
2550 = linux_fast_tracepoint_collecting (event_child
, NULL
);
2552 if (event_child
->collecting_fast_tracepoint
!= 1)
2554 /* No longer need this breakpoint. */
2555 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
2559 "No longer need exit-jump-pad bkpt; removing it."
2560 "stopping all threads momentarily.\n");
2562 /* Other running threads could hit this breakpoint.
2563 We don't handle moribund locations like GDB does,
2564 instead we always pause all threads when removing
2565 breakpoints, so that any step-over or
2566 decr_pc_after_break adjustment is always taken
2567 care of while the breakpoint is still
2569 stop_all_lwps (1, event_child
);
2570 cancel_breakpoints ();
2572 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
2573 event_child
->exit_jump_pad_bkpt
= NULL
;
2575 unstop_all_lwps (1, event_child
);
2577 gdb_assert (event_child
->suspended
>= 0);
2581 if (event_child
->collecting_fast_tracepoint
== 0)
2585 "fast tracepoint finished "
2586 "collecting successfully.\n");
2588 /* We may have a deferred signal to report. */
2589 if (dequeue_one_deferred_signal (event_child
, &w
))
2592 fprintf (stderr
, "dequeued one signal.\n");
2597 fprintf (stderr
, "no deferred signals.\n");
2599 if (stabilizing_threads
)
2601 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
2602 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
2603 return ptid_of (event_child
);
2609 /* Check whether GDB would be interested in this event. */
2611 /* If GDB is not interested in this signal, don't stop other
2612 threads, and don't report it to GDB. Just resume the inferior
2613 right away. We do this for threading-related signals as well as
2614 any that GDB specifically requested we ignore. But never ignore
2615 SIGSTOP if we sent it ourselves, and do not ignore signals when
2616 stepping - they may require special handling to skip the signal
2618 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
2621 && current_inferior
->last_resume_kind
!= resume_step
2623 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
2624 (current_process ()->private->thread_db
!= NULL
2625 && (WSTOPSIG (w
) == __SIGRTMIN
2626 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
2629 (pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
2630 && !(WSTOPSIG (w
) == SIGSTOP
2631 && current_inferior
->last_resume_kind
== resume_stop
))))
2633 siginfo_t info
, *info_p
;
2636 fprintf (stderr
, "Ignored signal %d for LWP %ld.\n",
2637 WSTOPSIG (w
), lwpid_of (event_child
));
2639 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (event_child
),
2640 (PTRACE_ARG3_TYPE
) 0, &info
) == 0)
2644 linux_resume_one_lwp (event_child
, event_child
->stepping
,
2645 WSTOPSIG (w
), info_p
);
2649 /* Note that all addresses are always "out of the step range" when
2650 there's no range to begin with. */
2651 in_step_range
= lwp_in_step_range (event_child
);
2653 /* If GDB wanted this thread to single step, and the thread is out
2654 of the step range, we always want to report the SIGTRAP, and let
2655 GDB handle it. Watchpoints should always be reported. So should
2656 signals we can't explain. A SIGTRAP we can't explain could be a
2657 GDB breakpoint --- we may or not support Z0 breakpoints. If we
2658 do, we're be able to handle GDB breakpoints on top of internal
2659 breakpoints, by handling the internal breakpoint and still
2660 reporting the event to GDB. If we don't, we're out of luck, GDB
2661 won't see the breakpoint hit. */
2662 report_to_gdb
= (!maybe_internal_trap
2663 || (current_inferior
->last_resume_kind
== resume_step
2665 || event_child
->stopped_by_watchpoint
2666 || (!step_over_finished
&& !in_step_range
2667 && !bp_explains_trap
&& !trace_event
)
2668 || (gdb_breakpoint_here (event_child
->stop_pc
)
2669 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
2670 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
)));
2672 run_breakpoint_commands (event_child
->stop_pc
);
2674 /* We found no reason GDB would want us to stop. We either hit one
2675 of our own breakpoints, or finished an internal step GDB
2676 shouldn't know about. */
2681 if (bp_explains_trap
)
2682 fprintf (stderr
, "Hit a gdbserver breakpoint.\n");
2683 if (step_over_finished
)
2684 fprintf (stderr
, "Step-over finished.\n");
2686 fprintf (stderr
, "Tracepoint event.\n");
2687 if (lwp_in_step_range (event_child
))
2688 fprintf (stderr
, "Range stepping pc 0x%s [0x%s, 0x%s).\n",
2689 paddress (event_child
->stop_pc
),
2690 paddress (event_child
->step_range_start
),
2691 paddress (event_child
->step_range_end
));
2694 /* We're not reporting this breakpoint to GDB, so apply the
2695 decr_pc_after_break adjustment to the inferior's regcache
2698 if (the_low_target
.set_pc
!= NULL
)
2700 struct regcache
*regcache
2701 = get_thread_regcache (get_lwp_thread (event_child
), 1);
2702 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
2705 /* We may have finished stepping over a breakpoint. If so,
2706 we've stopped and suspended all LWPs momentarily except the
2707 stepping one. This is where we resume them all again. We're
2708 going to keep waiting, so use proceed, which handles stepping
2709 over the next breakpoint. */
2711 fprintf (stderr
, "proceeding all threads.\n");
2713 if (step_over_finished
)
2714 unsuspend_all_lwps (event_child
);
2716 proceed_all_lwps ();
2722 if (current_inferior
->last_resume_kind
== resume_step
)
2724 if (event_child
->step_range_start
== event_child
->step_range_end
)
2725 fprintf (stderr
, "GDB wanted to single-step, reporting event.\n");
2726 else if (!lwp_in_step_range (event_child
))
2727 fprintf (stderr
, "Out of step range, reporting event.\n");
2729 if (event_child
->stopped_by_watchpoint
)
2730 fprintf (stderr
, "Stopped by watchpoint.\n");
2731 if (gdb_breakpoint_here (event_child
->stop_pc
))
2732 fprintf (stderr
, "Stopped by GDB breakpoint.\n");
2734 fprintf (stderr
, "Hit a non-gdbserver trap event.\n");
2737 /* Alright, we're going to report a stop. */
2739 if (!non_stop
&& !stabilizing_threads
)
2741 /* In all-stop, stop all threads. */
2742 stop_all_lwps (0, NULL
);
2744 /* If we're not waiting for a specific LWP, choose an event LWP
2745 from among those that have had events. Giving equal priority
2746 to all LWPs that have had events helps prevent
2748 if (ptid_equal (ptid
, minus_one_ptid
))
2750 event_child
->status_pending_p
= 1;
2751 event_child
->status_pending
= w
;
2753 select_event_lwp (&event_child
);
2755 event_child
->status_pending_p
= 0;
2756 w
= event_child
->status_pending
;
2759 /* Now that we've selected our final event LWP, cancel any
2760 breakpoints in other LWPs that have hit a GDB breakpoint.
2761 See the comment in cancel_breakpoints_callback to find out
2763 find_inferior (&all_lwps
, cancel_breakpoints_callback
, event_child
);
2765 /* If we were going a step-over, all other threads but the stepping one
2766 had been paused in start_step_over, with their suspend counts
2767 incremented. We don't want to do a full unstop/unpause, because we're
2768 in all-stop mode (so we want threads stopped), but we still need to
2769 unsuspend the other threads, to decrement their `suspended' count
2771 if (step_over_finished
)
2772 unsuspend_all_lwps (event_child
);
2774 /* Stabilize threads (move out of jump pads). */
2775 stabilize_threads ();
2779 /* If we just finished a step-over, then all threads had been
2780 momentarily paused. In all-stop, that's fine, we want
2781 threads stopped by now anyway. In non-stop, we need to
2782 re-resume threads that GDB wanted to be running. */
2783 if (step_over_finished
)
2784 unstop_all_lwps (1, event_child
);
2787 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
2789 if (current_inferior
->last_resume_kind
== resume_stop
2790 && WSTOPSIG (w
) == SIGSTOP
)
2792 /* A thread that has been requested to stop by GDB with vCont;t,
2793 and it stopped cleanly, so report as SIG0. The use of
2794 SIGSTOP is an implementation detail. */
2795 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
2797 else if (current_inferior
->last_resume_kind
== resume_stop
2798 && WSTOPSIG (w
) != SIGSTOP
)
2800 /* A thread that has been requested to stop by GDB with vCont;t,
2801 but, it stopped for other reasons. */
2802 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
2806 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
2809 gdb_assert (ptid_equal (step_over_bkpt
, null_ptid
));
2812 fprintf (stderr
, "linux_wait ret = %s, %d, %d\n",
2813 target_pid_to_str (ptid_of (event_child
)),
2815 ourstatus
->value
.sig
);
2817 return ptid_of (event_child
);
2820 /* Get rid of any pending event in the pipe. */
2822 async_file_flush (void)
2828 ret
= read (linux_event_pipe
[0], &buf
, 1);
2829 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
2832 /* Put something in the pipe, so the event loop wakes up. */
2834 async_file_mark (void)
2838 async_file_flush ();
2841 ret
= write (linux_event_pipe
[1], "+", 1);
2842 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
2844 /* Ignore EAGAIN. If the pipe is full, the event loop will already
2845 be awakened anyway. */
2849 linux_wait (ptid_t ptid
,
2850 struct target_waitstatus
*ourstatus
, int target_options
)
2855 fprintf (stderr
, "linux_wait: [%s]\n", target_pid_to_str (ptid
));
2857 /* Flush the async file first. */
2858 if (target_is_async_p ())
2859 async_file_flush ();
2861 event_ptid
= linux_wait_1 (ptid
, ourstatus
, target_options
);
2863 /* If at least one stop was reported, there may be more. A single
2864 SIGCHLD can signal more than one child stop. */
2865 if (target_is_async_p ()
2866 && (target_options
& TARGET_WNOHANG
) != 0
2867 && !ptid_equal (event_ptid
, null_ptid
))
2873 /* Send a signal to an LWP. */
2876 kill_lwp (unsigned long lwpid
, int signo
)
2878 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2879 fails, then we are not using nptl threads and we should be using kill. */
2883 static int tkill_failed
;
2890 ret
= syscall (__NR_tkill
, lwpid
, signo
);
2891 if (errno
!= ENOSYS
)
2898 return kill (lwpid
, signo
);
2902 linux_stop_lwp (struct lwp_info
*lwp
)
2908 send_sigstop (struct lwp_info
*lwp
)
2912 pid
= lwpid_of (lwp
);
2914 /* If we already have a pending stop signal for this process, don't
2916 if (lwp
->stop_expected
)
2919 fprintf (stderr
, "Have pending sigstop for lwp %d\n", pid
);
2925 fprintf (stderr
, "Sending sigstop to lwp %d\n", pid
);
2927 lwp
->stop_expected
= 1;
2928 kill_lwp (pid
, SIGSTOP
);
2932 send_sigstop_callback (struct inferior_list_entry
*entry
, void *except
)
2934 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2936 /* Ignore EXCEPT. */
2947 /* Increment the suspend count of an LWP, and stop it, if not stopped
2950 suspend_and_send_sigstop_callback (struct inferior_list_entry
*entry
,
2953 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2955 /* Ignore EXCEPT. */
2961 return send_sigstop_callback (entry
, except
);
2965 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
2967 /* It's dead, really. */
2970 /* Store the exit status for later. */
2971 lwp
->status_pending_p
= 1;
2972 lwp
->status_pending
= wstat
;
2974 /* Prevent trying to stop it. */
2977 /* No further stops are expected from a dead lwp. */
2978 lwp
->stop_expected
= 0;
2982 wait_for_sigstop (struct inferior_list_entry
*entry
)
2984 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2985 struct thread_info
*saved_inferior
;
2994 fprintf (stderr
, "wait_for_sigstop: LWP %ld already stopped\n",
2999 saved_inferior
= current_inferior
;
3000 if (saved_inferior
!= NULL
)
3001 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
3003 saved_tid
= null_ptid
; /* avoid bogus unused warning */
3005 ptid
= lwp
->head
.id
;
3008 fprintf (stderr
, "wait_for_sigstop: pulling one event\n");
3010 pid
= linux_wait_for_event (ptid
, &wstat
, __WALL
);
3012 /* If we stopped with a non-SIGSTOP signal, save it for later
3013 and record the pending SIGSTOP. If the process exited, just
3015 if (WIFSTOPPED (wstat
))
3018 fprintf (stderr
, "LWP %ld stopped with signal %d\n",
3019 lwpid_of (lwp
), WSTOPSIG (wstat
));
3021 if (WSTOPSIG (wstat
) != SIGSTOP
)
3024 fprintf (stderr
, "LWP %ld stopped with non-sigstop status %06x\n",
3025 lwpid_of (lwp
), wstat
);
3027 lwp
->status_pending_p
= 1;
3028 lwp
->status_pending
= wstat
;
3034 fprintf (stderr
, "Process %d exited while stopping LWPs\n", pid
);
3036 lwp
= find_lwp_pid (pid_to_ptid (pid
));
3039 /* Leave this status pending for the next time we're able to
3040 report it. In the mean time, we'll report this lwp as
3041 dead to GDB, so GDB doesn't try to read registers and
3042 memory from it. This can only happen if this was the
3043 last thread of the process; otherwise, PID is removed
3044 from the thread tables before linux_wait_for_event
3046 mark_lwp_dead (lwp
, wstat
);
3050 if (saved_inferior
== NULL
|| linux_thread_alive (saved_tid
))
3051 current_inferior
= saved_inferior
;
3055 fprintf (stderr
, "Previously current thread died.\n");
3059 /* We can't change the current inferior behind GDB's back,
3060 otherwise, a subsequent command may apply to the wrong
3062 current_inferior
= NULL
;
3066 /* Set a valid thread as current. */
3067 set_desired_inferior (0);
3072 /* Returns true if LWP ENTRY is stopped in a jump pad, and we can't
3073 move it out, because we need to report the stop event to GDB. For
3074 example, if the user puts a breakpoint in the jump pad, it's
3075 because she wants to debug it. */
3078 stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
, void *data
)
3080 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3081 struct thread_info
*thread
= get_lwp_thread (lwp
);
3083 gdb_assert (lwp
->suspended
== 0);
3084 gdb_assert (lwp
->stopped
);
3086 /* Allow debugging the jump pad, gdb_collect, etc.. */
3087 return (supports_fast_tracepoints ()
3088 && agent_loaded_p ()
3089 && (gdb_breakpoint_here (lwp
->stop_pc
)
3090 || lwp
->stopped_by_watchpoint
3091 || thread
->last_resume_kind
== resume_step
)
3092 && linux_fast_tracepoint_collecting (lwp
, NULL
));
3096 move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
)
3098 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3099 struct thread_info
*thread
= get_lwp_thread (lwp
);
3102 gdb_assert (lwp
->suspended
== 0);
3103 gdb_assert (lwp
->stopped
);
3105 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
3107 /* Allow debugging the jump pad, gdb_collect, etc. */
3108 if (!gdb_breakpoint_here (lwp
->stop_pc
)
3109 && !lwp
->stopped_by_watchpoint
3110 && thread
->last_resume_kind
!= resume_step
3111 && maybe_move_out_of_jump_pad (lwp
, wstat
))
3115 "LWP %ld needs stabilizing (in jump pad)\n",
3120 lwp
->status_pending_p
= 0;
3121 enqueue_one_deferred_signal (lwp
, wstat
);
3125 "Signal %d for LWP %ld deferred "
3127 WSTOPSIG (*wstat
), lwpid_of (lwp
));
3130 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
3137 lwp_running (struct inferior_list_entry
*entry
, void *data
)
3139 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3148 /* Stop all lwps that aren't stopped yet, except EXCEPT, if not NULL.
3149 If SUSPEND, then also increase the suspend count of every LWP,
3153 stop_all_lwps (int suspend
, struct lwp_info
*except
)
3155 /* Should not be called recursively. */
3156 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
3158 stopping_threads
= (suspend
3159 ? STOPPING_AND_SUSPENDING_THREADS
3160 : STOPPING_THREADS
);
3163 find_inferior (&all_lwps
, suspend_and_send_sigstop_callback
, except
);
3165 find_inferior (&all_lwps
, send_sigstop_callback
, except
);
3166 for_each_inferior (&all_lwps
, wait_for_sigstop
);
3167 stopping_threads
= NOT_STOPPING_THREADS
;
3170 /* Resume execution of the inferior process.
3171 If STEP is nonzero, single-step it.
3172 If SIGNAL is nonzero, give it that signal. */
3175 linux_resume_one_lwp (struct lwp_info
*lwp
,
3176 int step
, int signal
, siginfo_t
*info
)
3178 struct thread_info
*saved_inferior
;
3179 int fast_tp_collecting
;
3181 if (lwp
->stopped
== 0)
3184 fast_tp_collecting
= lwp
->collecting_fast_tracepoint
;
3186 gdb_assert (!stabilizing_threads
|| fast_tp_collecting
);
3188 /* Cancel actions that rely on GDB not changing the PC (e.g., the
3189 user used the "jump" command, or "set $pc = foo"). */
3190 if (lwp
->stop_pc
!= get_pc (lwp
))
3192 /* Collecting 'while-stepping' actions doesn't make sense
3194 release_while_stepping_state_list (get_lwp_thread (lwp
));
3197 /* If we have pending signals or status, and a new signal, enqueue the
3198 signal. Also enqueue the signal if we are waiting to reinsert a
3199 breakpoint; it will be picked up again below. */
3201 && (lwp
->status_pending_p
3202 || lwp
->pending_signals
!= NULL
3203 || lwp
->bp_reinsert
!= 0
3204 || fast_tp_collecting
))
3206 struct pending_signals
*p_sig
;
3207 p_sig
= xmalloc (sizeof (*p_sig
));
3208 p_sig
->prev
= lwp
->pending_signals
;
3209 p_sig
->signal
= signal
;
3211 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
3213 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
3214 lwp
->pending_signals
= p_sig
;
3217 if (lwp
->status_pending_p
)
3220 fprintf (stderr
, "Not resuming lwp %ld (%s, signal %d, stop %s);"
3221 " has pending status\n",
3222 lwpid_of (lwp
), step
? "step" : "continue", signal
,
3223 lwp
->stop_expected
? "expected" : "not expected");
3227 saved_inferior
= current_inferior
;
3228 current_inferior
= get_lwp_thread (lwp
);
3231 fprintf (stderr
, "Resuming lwp %ld (%s, signal %d, stop %s)\n",
3232 lwpid_of (lwp
), step
? "step" : "continue", signal
,
3233 lwp
->stop_expected
? "expected" : "not expected");
3235 /* This bit needs some thinking about. If we get a signal that
3236 we must report while a single-step reinsert is still pending,
3237 we often end up resuming the thread. It might be better to
3238 (ew) allow a stack of pending events; then we could be sure that
3239 the reinsert happened right away and not lose any signals.
3241 Making this stack would also shrink the window in which breakpoints are
3242 uninserted (see comment in linux_wait_for_lwp) but not enough for
3243 complete correctness, so it won't solve that problem. It may be
3244 worthwhile just to solve this one, however. */
3245 if (lwp
->bp_reinsert
!= 0)
3248 fprintf (stderr
, " pending reinsert at 0x%s\n",
3249 paddress (lwp
->bp_reinsert
));
3251 if (can_hardware_single_step ())
3253 if (fast_tp_collecting
== 0)
3256 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
3258 fprintf (stderr
, "BAD - reinserting and suspended(%d).\n",
3265 /* Postpone any pending signal. It was enqueued above. */
3269 if (fast_tp_collecting
== 1)
3273 lwp %ld wants to get out of fast tracepoint jump pad (exit-jump-pad-bkpt)\n",
3276 /* Postpone any pending signal. It was enqueued above. */
3279 else if (fast_tp_collecting
== 2)
3283 lwp %ld wants to get out of fast tracepoint jump pad single-stepping\n",
3286 if (can_hardware_single_step ())
3289 fatal ("moving out of jump pad single-stepping"
3290 " not implemented on this target");
3292 /* Postpone any pending signal. It was enqueued above. */
3296 /* If we have while-stepping actions in this thread set it stepping.
3297 If we have a signal to deliver, it may or may not be set to
3298 SIG_IGN, we don't know. Assume so, and allow collecting
3299 while-stepping into a signal handler. A possible smart thing to
3300 do would be to set an internal breakpoint at the signal return
3301 address, continue, and carry on catching this while-stepping
3302 action only when that breakpoint is hit. A future
3304 if (get_lwp_thread (lwp
)->while_stepping
!= NULL
3305 && can_hardware_single_step ())
3309 "lwp %ld has a while-stepping action -> forcing step.\n",
3314 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
3316 struct regcache
*regcache
= get_thread_regcache (current_inferior
, 1);
3317 CORE_ADDR pc
= (*the_low_target
.get_pc
) (regcache
);
3318 fprintf (stderr
, " resuming from pc 0x%lx\n", (long) pc
);
3321 /* If we have pending signals, consume one unless we are trying to
3322 reinsert a breakpoint or we're trying to finish a fast tracepoint
3324 if (lwp
->pending_signals
!= NULL
3325 && lwp
->bp_reinsert
== 0
3326 && fast_tp_collecting
== 0)
3328 struct pending_signals
**p_sig
;
3330 p_sig
= &lwp
->pending_signals
;
3331 while ((*p_sig
)->prev
!= NULL
)
3332 p_sig
= &(*p_sig
)->prev
;
3334 signal
= (*p_sig
)->signal
;
3335 if ((*p_sig
)->info
.si_signo
!= 0)
3336 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
), (PTRACE_ARG3_TYPE
) 0,
3343 if (the_low_target
.prepare_to_resume
!= NULL
)
3344 the_low_target
.prepare_to_resume (lwp
);
3346 regcache_invalidate_thread (get_lwp_thread (lwp
));
3349 lwp
->stopped_by_watchpoint
= 0;
3350 lwp
->stepping
= step
;
3351 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, lwpid_of (lwp
),
3352 (PTRACE_ARG3_TYPE
) 0,
3353 /* Coerce to a uintptr_t first to avoid potential gcc warning
3354 of coercing an 8 byte integer to a 4 byte pointer. */
3355 (PTRACE_ARG4_TYPE
) (uintptr_t) signal
);
3357 current_inferior
= saved_inferior
;
3360 /* ESRCH from ptrace either means that the thread was already
3361 running (an error) or that it is gone (a race condition). If
3362 it's gone, we will get a notification the next time we wait,
3363 so we can ignore the error. We could differentiate these
3364 two, but it's tricky without waiting; the thread still exists
3365 as a zombie, so sending it signal 0 would succeed. So just
3370 perror_with_name ("ptrace");
3374 struct thread_resume_array
3376 struct thread_resume
*resume
;
3380 /* This function is called once per thread. We look up the thread
3381 in RESUME_PTR, and mark the thread with a pointer to the appropriate
3384 This algorithm is O(threads * resume elements), but resume elements
3385 is small (and will remain small at least until GDB supports thread
3388 linux_set_resume_request (struct inferior_list_entry
*entry
, void *arg
)
3390 struct lwp_info
*lwp
;
3391 struct thread_info
*thread
;
3393 struct thread_resume_array
*r
;
3395 thread
= (struct thread_info
*) entry
;
3396 lwp
= get_thread_lwp (thread
);
3399 for (ndx
= 0; ndx
< r
->n
; ndx
++)
3401 ptid_t ptid
= r
->resume
[ndx
].thread
;
3402 if (ptid_equal (ptid
, minus_one_ptid
)
3403 || ptid_equal (ptid
, entry
->id
)
3404 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
3406 || (ptid_get_pid (ptid
) == pid_of (lwp
)
3407 && (ptid_is_pid (ptid
)
3408 || ptid_get_lwp (ptid
) == -1)))
3410 if (r
->resume
[ndx
].kind
== resume_stop
3411 && thread
->last_resume_kind
== resume_stop
)
3414 fprintf (stderr
, "already %s LWP %ld at GDB's request\n",
3415 thread
->last_status
.kind
== TARGET_WAITKIND_STOPPED
3423 lwp
->resume
= &r
->resume
[ndx
];
3424 thread
->last_resume_kind
= lwp
->resume
->kind
;
3426 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
3427 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
3429 /* If we had a deferred signal to report, dequeue one now.
3430 This can happen if LWP gets more than one signal while
3431 trying to get out of a jump pad. */
3433 && !lwp
->status_pending_p
3434 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
3436 lwp
->status_pending_p
= 1;
3440 "Dequeueing deferred signal %d for LWP %ld, "
3441 "leaving status pending.\n",
3442 WSTOPSIG (lwp
->status_pending
), lwpid_of (lwp
));
3449 /* No resume action for this thread. */
3456 /* Set *FLAG_P if this lwp has an interesting status pending. */
3458 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
3460 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3462 /* LWPs which will not be resumed are not interesting, because
3463 we might not wait for them next time through linux_wait. */
3464 if (lwp
->resume
== NULL
)
3467 if (lwp
->status_pending_p
)
3468 * (int *) flag_p
= 1;
3473 /* Return 1 if this lwp that GDB wants running is stopped at an
3474 internal breakpoint that we need to step over. It assumes that any
3475 required STOP_PC adjustment has already been propagated to the
3476 inferior's regcache. */
3479 need_step_over_p (struct inferior_list_entry
*entry
, void *dummy
)
3481 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3482 struct thread_info
*thread
;
3483 struct thread_info
*saved_inferior
;
3486 /* LWPs which will not be resumed are not interesting, because we
3487 might not wait for them next time through linux_wait. */
3493 "Need step over [LWP %ld]? Ignoring, not stopped\n",
3498 thread
= get_lwp_thread (lwp
);
3500 if (thread
->last_resume_kind
== resume_stop
)
3504 "Need step over [LWP %ld]? Ignoring, should remain stopped\n",
3509 gdb_assert (lwp
->suspended
>= 0);
3515 "Need step over [LWP %ld]? Ignoring, suspended\n",
3520 if (!lwp
->need_step_over
)
3524 "Need step over [LWP %ld]? No\n", lwpid_of (lwp
));
3527 if (lwp
->status_pending_p
)
3531 "Need step over [LWP %ld]? Ignoring, has pending status.\n",
3536 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
3540 /* If the PC has changed since we stopped, then don't do anything,
3541 and let the breakpoint/tracepoint be hit. This happens if, for
3542 instance, GDB handled the decr_pc_after_break subtraction itself,
3543 GDB is OOL stepping this thread, or the user has issued a "jump"
3544 command, or poked thread's registers herself. */
3545 if (pc
!= lwp
->stop_pc
)
3549 "Need step over [LWP %ld]? Cancelling, PC was changed. "
3550 "Old stop_pc was 0x%s, PC is now 0x%s\n",
3551 lwpid_of (lwp
), paddress (lwp
->stop_pc
), paddress (pc
));
3553 lwp
->need_step_over
= 0;
3557 saved_inferior
= current_inferior
;
3558 current_inferior
= thread
;
3560 /* We can only step over breakpoints we know about. */
3561 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
3563 /* Don't step over a breakpoint that GDB expects to hit
3564 though. If the condition is being evaluated on the target's side
3565 and it evaluate to false, step over this breakpoint as well. */
3566 if (gdb_breakpoint_here (pc
)
3567 && gdb_condition_true_at_breakpoint (pc
)
3568 && gdb_no_commands_at_breakpoint (pc
))
3572 "Need step over [LWP %ld]? yes, but found"
3573 " GDB breakpoint at 0x%s; skipping step over\n",
3574 lwpid_of (lwp
), paddress (pc
));
3576 current_inferior
= saved_inferior
;
3583 "Need step over [LWP %ld]? yes, "
3584 "found breakpoint at 0x%s\n",
3585 lwpid_of (lwp
), paddress (pc
));
3587 /* We've found an lwp that needs stepping over --- return 1 so
3588 that find_inferior stops looking. */
3589 current_inferior
= saved_inferior
;
3591 /* If the step over is cancelled, this is set again. */
3592 lwp
->need_step_over
= 0;
3597 current_inferior
= saved_inferior
;
3601 "Need step over [LWP %ld]? No, no breakpoint found at 0x%s\n",
3602 lwpid_of (lwp
), paddress (pc
));
3607 /* Start a step-over operation on LWP. When LWP stopped at a
3608 breakpoint, to make progress, we need to remove the breakpoint out
3609 of the way. If we let other threads run while we do that, they may
3610 pass by the breakpoint location and miss hitting it. To avoid
3611 that, a step-over momentarily stops all threads while LWP is
3612 single-stepped while the breakpoint is temporarily uninserted from
3613 the inferior. When the single-step finishes, we reinsert the
3614 breakpoint, and let all threads that are supposed to be running,
3617 On targets that don't support hardware single-step, we don't
3618 currently support full software single-stepping. Instead, we only
3619 support stepping over the thread event breakpoint, by asking the
3620 low target where to place a reinsert breakpoint. Since this
3621 routine assumes the breakpoint being stepped over is a thread event
3622 breakpoint, it usually assumes the return address of the current
3623 function is a good enough place to set the reinsert breakpoint. */
3626 start_step_over (struct lwp_info
*lwp
)
3628 struct thread_info
*saved_inferior
;
3634 "Starting step-over on LWP %ld. Stopping all threads\n",
3637 stop_all_lwps (1, lwp
);
3638 gdb_assert (lwp
->suspended
== 0);
3641 fprintf (stderr
, "Done stopping all threads for step-over.\n");
3643 /* Note, we should always reach here with an already adjusted PC,
3644 either by GDB (if we're resuming due to GDB's request), or by our
3645 caller, if we just finished handling an internal breakpoint GDB
3646 shouldn't care about. */
3649 saved_inferior
= current_inferior
;
3650 current_inferior
= get_lwp_thread (lwp
);
3652 lwp
->bp_reinsert
= pc
;
3653 uninsert_breakpoints_at (pc
);
3654 uninsert_fast_tracepoint_jumps_at (pc
);
3656 if (can_hardware_single_step ())
3662 CORE_ADDR raddr
= (*the_low_target
.breakpoint_reinsert_addr
) ();
3663 set_reinsert_breakpoint (raddr
);
3667 current_inferior
= saved_inferior
;
3669 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
3671 /* Require next event from this LWP. */
3672 step_over_bkpt
= lwp
->head
.id
;
3676 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
3677 start_step_over, if still there, and delete any reinsert
3678 breakpoints we've set, on non hardware single-step targets. */
3681 finish_step_over (struct lwp_info
*lwp
)
3683 if (lwp
->bp_reinsert
!= 0)
3686 fprintf (stderr
, "Finished step over.\n");
3688 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
3689 may be no breakpoint to reinsert there by now. */
3690 reinsert_breakpoints_at (lwp
->bp_reinsert
);
3691 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
3693 lwp
->bp_reinsert
= 0;
3695 /* Delete any software-single-step reinsert breakpoints. No
3696 longer needed. We don't have to worry about other threads
3697 hitting this trap, and later not being able to explain it,
3698 because we were stepping over a breakpoint, and we hold all
3699 threads but LWP stopped while doing that. */
3700 if (!can_hardware_single_step ())
3701 delete_reinsert_breakpoints ();
3703 step_over_bkpt
= null_ptid
;
3710 /* This function is called once per thread. We check the thread's resume
3711 request, which will tell us whether to resume, step, or leave the thread
3712 stopped; and what signal, if any, it should be sent.
3714 For threads which we aren't explicitly told otherwise, we preserve
3715 the stepping flag; this is used for stepping over gdbserver-placed
3718 If pending_flags was set in any thread, we queue any needed
3719 signals, since we won't actually resume. We already have a pending
3720 event to report, so we don't need to preserve any step requests;
3721 they should be re-issued if necessary. */
3724 linux_resume_one_thread (struct inferior_list_entry
*entry
, void *arg
)
3726 struct lwp_info
*lwp
;
3727 struct thread_info
*thread
;
3729 int leave_all_stopped
= * (int *) arg
;
3732 thread
= (struct thread_info
*) entry
;
3733 lwp
= get_thread_lwp (thread
);
3735 if (lwp
->resume
== NULL
)
3738 if (lwp
->resume
->kind
== resume_stop
)
3741 fprintf (stderr
, "resume_stop request for LWP %ld\n", lwpid_of (lwp
));
3746 fprintf (stderr
, "stopping LWP %ld\n", lwpid_of (lwp
));
3748 /* Stop the thread, and wait for the event asynchronously,
3749 through the event loop. */
3755 fprintf (stderr
, "already stopped LWP %ld\n",
3758 /* The LWP may have been stopped in an internal event that
3759 was not meant to be notified back to GDB (e.g., gdbserver
3760 breakpoint), so we should be reporting a stop event in
3763 /* If the thread already has a pending SIGSTOP, this is a
3764 no-op. Otherwise, something later will presumably resume
3765 the thread and this will cause it to cancel any pending
3766 operation, due to last_resume_kind == resume_stop. If
3767 the thread already has a pending status to report, we
3768 will still report it the next time we wait - see
3769 status_pending_p_callback. */
3771 /* If we already have a pending signal to report, then
3772 there's no need to queue a SIGSTOP, as this means we're
3773 midway through moving the LWP out of the jumppad, and we
3774 will report the pending signal as soon as that is
3776 if (lwp
->pending_signals_to_report
== NULL
)
3780 /* For stop requests, we're done. */
3782 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
3786 /* If this thread which is about to be resumed has a pending status,
3787 then don't resume any threads - we can just report the pending
3788 status. Make sure to queue any signals that would otherwise be
3789 sent. In all-stop mode, we do this decision based on if *any*
3790 thread has a pending status. If there's a thread that needs the
3791 step-over-breakpoint dance, then don't resume any other thread
3792 but that particular one. */
3793 leave_pending
= (lwp
->status_pending_p
|| leave_all_stopped
);
3798 fprintf (stderr
, "resuming LWP %ld\n", lwpid_of (lwp
));
3800 step
= (lwp
->resume
->kind
== resume_step
);
3801 linux_resume_one_lwp (lwp
, step
, lwp
->resume
->sig
, NULL
);
3806 fprintf (stderr
, "leaving LWP %ld stopped\n", lwpid_of (lwp
));
3808 /* If we have a new signal, enqueue the signal. */
3809 if (lwp
->resume
->sig
!= 0)
3811 struct pending_signals
*p_sig
;
3812 p_sig
= xmalloc (sizeof (*p_sig
));
3813 p_sig
->prev
= lwp
->pending_signals
;
3814 p_sig
->signal
= lwp
->resume
->sig
;
3815 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
3817 /* If this is the same signal we were previously stopped by,
3818 make sure to queue its siginfo. We can ignore the return
3819 value of ptrace; if it fails, we'll skip
3820 PTRACE_SETSIGINFO. */
3821 if (WIFSTOPPED (lwp
->last_status
)
3822 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
)
3823 ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
), (PTRACE_ARG3_TYPE
) 0,
3826 lwp
->pending_signals
= p_sig
;
3830 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
3836 linux_resume (struct thread_resume
*resume_info
, size_t n
)
3838 struct thread_resume_array array
= { resume_info
, n
};
3839 struct lwp_info
*need_step_over
= NULL
;
3841 int leave_all_stopped
;
3843 find_inferior (&all_threads
, linux_set_resume_request
, &array
);
3845 /* If there is a thread which would otherwise be resumed, which has
3846 a pending status, then don't resume any threads - we can just
3847 report the pending status. Make sure to queue any signals that
3848 would otherwise be sent. In non-stop mode, we'll apply this
3849 logic to each thread individually. We consume all pending events
3850 before considering to start a step-over (in all-stop). */
3853 find_inferior (&all_lwps
, resume_status_pending_p
, &any_pending
);
3855 /* If there is a thread which would otherwise be resumed, which is
3856 stopped at a breakpoint that needs stepping over, then don't
3857 resume any threads - have it step over the breakpoint with all
3858 other threads stopped, then resume all threads again. Make sure
3859 to queue any signals that would otherwise be delivered or
3861 if (!any_pending
&& supports_breakpoints ())
3863 = (struct lwp_info
*) find_inferior (&all_lwps
,
3864 need_step_over_p
, NULL
);
3866 leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
3870 if (need_step_over
!= NULL
)
3871 fprintf (stderr
, "Not resuming all, need step over\n");
3872 else if (any_pending
)
3874 "Not resuming, all-stop and found "
3875 "an LWP with pending status\n");
3877 fprintf (stderr
, "Resuming, no pending status or step over needed\n");
3880 /* Even if we're leaving threads stopped, queue all signals we'd
3881 otherwise deliver. */
3882 find_inferior (&all_threads
, linux_resume_one_thread
, &leave_all_stopped
);
3885 start_step_over (need_step_over
);
3888 /* This function is called once per thread. We check the thread's
3889 last resume request, which will tell us whether to resume, step, or
3890 leave the thread stopped. Any signal the client requested to be
3891 delivered has already been enqueued at this point.
3893 If any thread that GDB wants running is stopped at an internal
3894 breakpoint that needs stepping over, we start a step-over operation
3895 on that particular thread, and leave all others stopped. */
3898 proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
3900 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3901 struct thread_info
*thread
;
3909 "proceed_one_lwp: lwp %ld\n", lwpid_of (lwp
));
3914 fprintf (stderr
, " LWP %ld already running\n", lwpid_of (lwp
));
3918 thread
= get_lwp_thread (lwp
);
3920 if (thread
->last_resume_kind
== resume_stop
3921 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3924 fprintf (stderr
, " client wants LWP to remain %ld stopped\n",
3929 if (lwp
->status_pending_p
)
3932 fprintf (stderr
, " LWP %ld has pending status, leaving stopped\n",
3937 gdb_assert (lwp
->suspended
>= 0);
3942 fprintf (stderr
, " LWP %ld is suspended\n", lwpid_of (lwp
));
3946 if (thread
->last_resume_kind
== resume_stop
3947 && lwp
->pending_signals_to_report
== NULL
3948 && lwp
->collecting_fast_tracepoint
== 0)
3950 /* We haven't reported this LWP as stopped yet (otherwise, the
3951 last_status.kind check above would catch it, and we wouldn't
3952 reach here. This LWP may have been momentarily paused by a
3953 stop_all_lwps call while handling for example, another LWP's
3954 step-over. In that case, the pending expected SIGSTOP signal
3955 that was queued at vCont;t handling time will have already
3956 been consumed by wait_for_sigstop, and so we need to requeue
3957 another one here. Note that if the LWP already has a SIGSTOP
3958 pending, this is a no-op. */
3962 "Client wants LWP %ld to stop. "
3963 "Making sure it has a SIGSTOP pending\n",
3969 step
= thread
->last_resume_kind
== resume_step
;
3970 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
3975 unsuspend_and_proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
3977 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3983 gdb_assert (lwp
->suspended
>= 0);
3985 return proceed_one_lwp (entry
, except
);
3988 /* When we finish a step-over, set threads running again. If there's
3989 another thread that may need a step-over, now's the time to start
3990 it. Eventually, we'll move all threads past their breakpoints. */
3993 proceed_all_lwps (void)
3995 struct lwp_info
*need_step_over
;
3997 /* If there is a thread which would otherwise be resumed, which is
3998 stopped at a breakpoint that needs stepping over, then don't
3999 resume any threads - have it step over the breakpoint with all
4000 other threads stopped, then resume all threads again. */
4002 if (supports_breakpoints ())
4005 = (struct lwp_info
*) find_inferior (&all_lwps
,
4006 need_step_over_p
, NULL
);
4008 if (need_step_over
!= NULL
)
4011 fprintf (stderr
, "proceed_all_lwps: found "
4012 "thread %ld needing a step-over\n",
4013 lwpid_of (need_step_over
));
4015 start_step_over (need_step_over
);
4021 fprintf (stderr
, "Proceeding, no step-over needed\n");
4023 find_inferior (&all_lwps
, proceed_one_lwp
, NULL
);
4026 /* Stopped LWPs that the client wanted to be running, that don't have
4027 pending statuses, are set to run again, except for EXCEPT, if not
4028 NULL. This undoes a stop_all_lwps call. */
4031 unstop_all_lwps (int unsuspend
, struct lwp_info
*except
)
4037 "unstopping all lwps, except=(LWP %ld)\n", lwpid_of (except
));
4040 "unstopping all lwps\n");
4044 find_inferior (&all_lwps
, unsuspend_and_proceed_one_lwp
, except
);
4046 find_inferior (&all_lwps
, proceed_one_lwp
, except
);
4050 #ifdef HAVE_LINUX_REGSETS
4052 #define use_linux_regsets 1
4055 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
4056 struct regcache
*regcache
)
4058 struct regset_info
*regset
;
4059 int saw_general_regs
= 0;
4063 regset
= regsets_info
->regsets
;
4065 pid
= lwpid_of (get_thread_lwp (current_inferior
));
4066 while (regset
->size
>= 0)
4071 if (regset
->size
== 0
4072 || regsets_info
->disabled_regsets
[regset
- regsets_info
->regsets
])
4078 buf
= xmalloc (regset
->size
);
4080 nt_type
= regset
->nt_type
;
4084 iov
.iov_len
= regset
->size
;
4085 data
= (void *) &iov
;
4091 res
= ptrace (regset
->get_request
, pid
,
4092 (PTRACE_ARG3_TYPE
) (long) nt_type
, data
);
4094 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4102 /* If we get EIO on a regset, do not try it again for
4103 this process mode. */
4104 dr_offset
= regset
- regsets_info
->regsets
;
4105 regsets_info
->disabled_regsets
[dr_offset
] = 1;
4112 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
4117 else if (regset
->type
== GENERAL_REGS
)
4118 saw_general_regs
= 1;
4119 regset
->store_function (regcache
, buf
);
4123 if (saw_general_regs
)
4130 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
4131 struct regcache
*regcache
)
4133 struct regset_info
*regset
;
4134 int saw_general_regs
= 0;
4138 regset
= regsets_info
->regsets
;
4140 pid
= lwpid_of (get_thread_lwp (current_inferior
));
4141 while (regset
->size
>= 0)
4146 if (regset
->size
== 0
4147 || regsets_info
->disabled_regsets
[regset
- regsets_info
->regsets
])
4153 buf
= xmalloc (regset
->size
);
4155 /* First fill the buffer with the current register set contents,
4156 in case there are any items in the kernel's regset that are
4157 not in gdbserver's regcache. */
4159 nt_type
= regset
->nt_type
;
4163 iov
.iov_len
= regset
->size
;
4164 data
= (void *) &iov
;
4170 res
= ptrace (regset
->get_request
, pid
,
4171 (PTRACE_ARG3_TYPE
) (long) nt_type
, data
);
4173 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4178 /* Then overlay our cached registers on that. */
4179 regset
->fill_function (regcache
, buf
);
4181 /* Only now do we write the register set. */
4183 res
= ptrace (regset
->set_request
, pid
,
4184 (PTRACE_ARG3_TYPE
) (long) nt_type
, data
);
4186 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
4196 /* If we get EIO on a regset, do not try it again for
4197 this process mode. */
4198 dr_offset
= regset
- regsets_info
->regsets
;
4199 regsets_info
->disabled_regsets
[dr_offset
] = 1;
4203 else if (errno
== ESRCH
)
4205 /* At this point, ESRCH should mean the process is
4206 already gone, in which case we simply ignore attempts
4207 to change its registers. See also the related
4208 comment in linux_resume_one_lwp. */
4214 perror ("Warning: ptrace(regsets_store_inferior_registers)");
4217 else if (regset
->type
== GENERAL_REGS
)
4218 saw_general_regs
= 1;
4222 if (saw_general_regs
)
4228 #else /* !HAVE_LINUX_REGSETS */
4230 #define use_linux_regsets 0
4231 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
4232 #define regsets_store_inferior_registers(regsets_info, regcache) 1
4236 /* Return 1 if register REGNO is supported by one of the regset ptrace
4237 calls or 0 if it has to be transferred individually. */
4240 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
4242 unsigned char mask
= 1 << (regno
% 8);
4243 size_t index
= regno
/ 8;
4245 return (use_linux_regsets
4246 && (regs_info
->regset_bitmap
== NULL
4247 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
4250 #ifdef HAVE_LINUX_USRREGS
4253 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
4257 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
4258 error ("Invalid register number %d.", regnum
);
4260 addr
= usrregs
->regmap
[regnum
];
4265 /* Fetch one register. */
4267 fetch_register (const struct usrregs_info
*usrregs
,
4268 struct regcache
*regcache
, int regno
)
4275 if (regno
>= usrregs
->num_regs
)
4277 if ((*the_low_target
.cannot_fetch_register
) (regno
))
4280 regaddr
= register_addr (usrregs
, regno
);
4284 size
= ((register_size (regcache
->tdesc
, regno
)
4285 + sizeof (PTRACE_XFER_TYPE
) - 1)
4286 & -sizeof (PTRACE_XFER_TYPE
));
4287 buf
= alloca (size
);
4289 pid
= lwpid_of (get_thread_lwp (current_inferior
));
4290 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
4293 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
4294 ptrace (PTRACE_PEEKUSER
, pid
,
4295 /* Coerce to a uintptr_t first to avoid potential gcc warning
4296 of coercing an 8 byte integer to a 4 byte pointer. */
4297 (PTRACE_ARG3_TYPE
) (uintptr_t) regaddr
, (PTRACE_ARG4_TYPE
) 0);
4298 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
4300 error ("reading register %d: %s", regno
, strerror (errno
));
4303 if (the_low_target
.supply_ptrace_register
)
4304 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
4306 supply_register (regcache
, regno
, buf
);
4309 /* Store one register. */
4311 store_register (const struct usrregs_info
*usrregs
,
4312 struct regcache
*regcache
, int regno
)
4319 if (regno
>= usrregs
->num_regs
)
4321 if ((*the_low_target
.cannot_store_register
) (regno
))
4324 regaddr
= register_addr (usrregs
, regno
);
4328 size
= ((register_size (regcache
->tdesc
, regno
)
4329 + sizeof (PTRACE_XFER_TYPE
) - 1)
4330 & -sizeof (PTRACE_XFER_TYPE
));
4331 buf
= alloca (size
);
4332 memset (buf
, 0, size
);
4334 if (the_low_target
.collect_ptrace_register
)
4335 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
4337 collect_register (regcache
, regno
, buf
);
4339 pid
= lwpid_of (get_thread_lwp (current_inferior
));
4340 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
4343 ptrace (PTRACE_POKEUSER
, pid
,
4344 /* Coerce to a uintptr_t first to avoid potential gcc warning
4345 about coercing an 8 byte integer to a 4 byte pointer. */
4346 (PTRACE_ARG3_TYPE
) (uintptr_t) regaddr
,
4347 (PTRACE_ARG4_TYPE
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
4350 /* At this point, ESRCH should mean the process is
4351 already gone, in which case we simply ignore attempts
4352 to change its registers. See also the related
4353 comment in linux_resume_one_lwp. */
4357 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
4358 error ("writing register %d: %s", regno
, strerror (errno
));
4360 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
4364 /* Fetch all registers, or just one, from the child process.
4365 If REGNO is -1, do this for all registers, skipping any that are
4366 assumed to have been retrieved by regsets_fetch_inferior_registers,
4367 unless ALL is non-zero.
4368 Otherwise, REGNO specifies which register (so we can save time). */
4370 usr_fetch_inferior_registers (const struct regs_info
*regs_info
,
4371 struct regcache
*regcache
, int regno
, int all
)
4373 struct usrregs_info
*usr
= regs_info
->usrregs
;
4377 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
4378 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
4379 fetch_register (usr
, regcache
, regno
);
4382 fetch_register (usr
, regcache
, regno
);
4385 /* Store our register values back into the inferior.
4386 If REGNO is -1, do this for all registers, skipping any that are
4387 assumed to have been saved by regsets_store_inferior_registers,
4388 unless ALL is non-zero.
4389 Otherwise, REGNO specifies which register (so we can save time). */
4391 usr_store_inferior_registers (const struct regs_info
*regs_info
,
4392 struct regcache
*regcache
, int regno
, int all
)
4394 struct usrregs_info
*usr
= regs_info
->usrregs
;
4398 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
4399 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
4400 store_register (usr
, regcache
, regno
);
4403 store_register (usr
, regcache
, regno
);
4406 #else /* !HAVE_LINUX_USRREGS */
4408 #define usr_fetch_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
4409 #define usr_store_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
4415 linux_fetch_registers (struct regcache
*regcache
, int regno
)
4419 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
4423 if (the_low_target
.fetch_register
!= NULL
4424 && regs_info
->usrregs
!= NULL
)
4425 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
4426 (*the_low_target
.fetch_register
) (regcache
, regno
);
4428 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
4429 if (regs_info
->usrregs
!= NULL
)
4430 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
4434 if (the_low_target
.fetch_register
!= NULL
4435 && (*the_low_target
.fetch_register
) (regcache
, regno
))
4438 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
4440 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
4442 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
4443 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
4448 linux_store_registers (struct regcache
*regcache
, int regno
)
4452 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
4456 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
4458 if (regs_info
->usrregs
!= NULL
)
4459 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
4463 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
4465 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
4467 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
4468 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
4473 /* Copy LEN bytes from inferior's memory starting at MEMADDR
4474 to debugger memory starting at MYADDR. */
4477 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
4479 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4480 register PTRACE_XFER_TYPE
*buffer
;
4481 register CORE_ADDR addr
;
4488 /* Try using /proc. Don't bother for one word. */
4489 if (len
>= 3 * sizeof (long))
4493 /* We could keep this file open and cache it - possibly one per
4494 thread. That requires some juggling, but is even faster. */
4495 sprintf (filename
, "/proc/%d/mem", pid
);
4496 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4500 /* If pread64 is available, use it. It's faster if the kernel
4501 supports it (only one syscall), and it's 64-bit safe even on
4502 32-bit platforms (for instance, SPARC debugging a SPARC64
4505 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
4508 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
4509 bytes
= read (fd
, myaddr
, len
);
4516 /* Some data was read, we'll try to get the rest with ptrace. */
4526 /* Round starting address down to longword boundary. */
4527 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
4528 /* Round ending address up; get number of longwords that makes. */
4529 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4530 / sizeof (PTRACE_XFER_TYPE
));
4531 /* Allocate buffer of that many longwords. */
4532 buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
4534 /* Read all the longwords */
4536 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
4538 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4539 about coercing an 8 byte integer to a 4 byte pointer. */
4540 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
4541 (PTRACE_ARG3_TYPE
) (uintptr_t) addr
,
4542 (PTRACE_ARG4_TYPE
) 0);
4548 /* Copy appropriate bytes out of the buffer. */
4551 i
*= sizeof (PTRACE_XFER_TYPE
);
4552 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
4554 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
4561 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
4562 memory at MEMADDR. On failure (cannot write to the inferior)
4563 returns the value of errno. Always succeeds if LEN is zero. */
4566 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
4569 /* Round starting address down to longword boundary. */
4570 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
4571 /* Round ending address up; get number of longwords that makes. */
4573 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4574 / sizeof (PTRACE_XFER_TYPE
);
4576 /* Allocate buffer of that many longwords. */
4577 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*)
4578 alloca (count
* sizeof (PTRACE_XFER_TYPE
));
4580 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4584 /* Zero length write always succeeds. */
4590 /* Dump up to four bytes. */
4591 unsigned int val
= * (unsigned int *) myaddr
;
4597 val
= val
& 0xffffff;
4598 fprintf (stderr
, "Writing %0*x to 0x%08lx\n", 2 * ((len
< 4) ? len
: 4),
4599 val
, (long)memaddr
);
4602 /* Fill start and end extra bytes of buffer with existing memory data. */
4605 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4606 about coercing an 8 byte integer to a 4 byte pointer. */
4607 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
4608 (PTRACE_ARG3_TYPE
) (uintptr_t) addr
,
4609 (PTRACE_ARG4_TYPE
) 0);
4617 = ptrace (PTRACE_PEEKTEXT
, pid
,
4618 /* Coerce to a uintptr_t first to avoid potential gcc warning
4619 about coercing an 8 byte integer to a 4 byte pointer. */
4620 (PTRACE_ARG3_TYPE
) (uintptr_t) (addr
+ (count
- 1)
4621 * sizeof (PTRACE_XFER_TYPE
)),
4622 (PTRACE_ARG4_TYPE
) 0);
4627 /* Copy data to be written over corresponding part of buffer. */
4629 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
4632 /* Write the entire buffer. */
4634 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
4637 ptrace (PTRACE_POKETEXT
, pid
,
4638 /* Coerce to a uintptr_t first to avoid potential gcc warning
4639 about coercing an 8 byte integer to a 4 byte pointer. */
4640 (PTRACE_ARG3_TYPE
) (uintptr_t) addr
,
4641 (PTRACE_ARG4_TYPE
) buffer
[i
]);
4649 /* Non-zero if the kernel supports PTRACE_O_TRACEFORK. */
4650 static int linux_supports_tracefork_flag
;
4653 linux_enable_event_reporting (int pid
)
4655 if (!linux_supports_tracefork_flag
)
4658 ptrace (PTRACE_SETOPTIONS
, pid
, (PTRACE_ARG3_TYPE
) 0,
4659 (PTRACE_ARG4_TYPE
) PTRACE_O_TRACECLONE
);
4662 /* Helper functions for linux_test_for_tracefork, called via clone (). */
4665 linux_tracefork_grandchild (void *arg
)
4670 #define STACK_SIZE 4096
4673 linux_tracefork_child (void *arg
)
4675 ptrace (PTRACE_TRACEME
, 0, (PTRACE_ARG3_TYPE
) 0, (PTRACE_ARG4_TYPE
) 0);
4676 kill (getpid (), SIGSTOP
);
4678 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
4681 linux_tracefork_grandchild (NULL
);
4683 #else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4686 __clone2 (linux_tracefork_grandchild
, arg
, STACK_SIZE
,
4687 CLONE_VM
| SIGCHLD
, NULL
);
4689 clone (linux_tracefork_grandchild
, (char *) arg
+ STACK_SIZE
,
4690 CLONE_VM
| SIGCHLD
, NULL
);
4693 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4698 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
4699 sure that we can enable the option, and that it had the desired
4703 linux_test_for_tracefork (void)
4705 int child_pid
, ret
, status
;
4707 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4708 char *stack
= xmalloc (STACK_SIZE
* 4);
4709 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4711 linux_supports_tracefork_flag
= 0;
4713 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
4715 child_pid
= fork ();
4717 linux_tracefork_child (NULL
);
4719 #else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4721 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
4723 child_pid
= __clone2 (linux_tracefork_child
, stack
, STACK_SIZE
,
4724 CLONE_VM
| SIGCHLD
, stack
+ STACK_SIZE
* 2);
4725 #else /* !__ia64__ */
4726 child_pid
= clone (linux_tracefork_child
, stack
+ STACK_SIZE
,
4727 CLONE_VM
| SIGCHLD
, stack
+ STACK_SIZE
* 2);
4728 #endif /* !__ia64__ */
4730 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4732 if (child_pid
== -1)
4733 perror_with_name ("clone");
4735 ret
= my_waitpid (child_pid
, &status
, 0);
4737 perror_with_name ("waitpid");
4738 else if (ret
!= child_pid
)
4739 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
4740 if (! WIFSTOPPED (status
))
4741 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
4743 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, (PTRACE_ARG3_TYPE
) 0,
4744 (PTRACE_ARG4_TYPE
) PTRACE_O_TRACEFORK
);
4747 ret
= ptrace (PTRACE_KILL
, child_pid
, (PTRACE_ARG3_TYPE
) 0,
4748 (PTRACE_ARG4_TYPE
) 0);
4751 warning ("linux_test_for_tracefork: failed to kill child");
4755 ret
= my_waitpid (child_pid
, &status
, 0);
4756 if (ret
!= child_pid
)
4757 warning ("linux_test_for_tracefork: failed to wait for killed child");
4758 else if (!WIFSIGNALED (status
))
4759 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
4760 "killed child", status
);
4765 ret
= ptrace (PTRACE_CONT
, child_pid
, (PTRACE_ARG3_TYPE
) 0,
4766 (PTRACE_ARG4_TYPE
) 0);
4768 warning ("linux_test_for_tracefork: failed to resume child");
4770 ret
= my_waitpid (child_pid
, &status
, 0);
4772 if (ret
== child_pid
&& WIFSTOPPED (status
)
4773 && status
>> 16 == PTRACE_EVENT_FORK
)
4776 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, (PTRACE_ARG3_TYPE
) 0,
4778 if (ret
== 0 && second_pid
!= 0)
4782 linux_supports_tracefork_flag
= 1;
4783 my_waitpid (second_pid
, &second_status
, 0);
4784 ret
= ptrace (PTRACE_KILL
, second_pid
, (PTRACE_ARG3_TYPE
) 0,
4785 (PTRACE_ARG4_TYPE
) 0);
4787 warning ("linux_test_for_tracefork: failed to kill second child");
4788 my_waitpid (second_pid
, &status
, 0);
4792 warning ("linux_test_for_tracefork: unexpected result from waitpid "
4793 "(%d, status 0x%x)", ret
, status
);
4797 ret
= ptrace (PTRACE_KILL
, child_pid
, (PTRACE_ARG3_TYPE
) 0,
4798 (PTRACE_ARG4_TYPE
) 0);
4800 warning ("linux_test_for_tracefork: failed to kill child");
4801 my_waitpid (child_pid
, &status
, 0);
4803 while (WIFSTOPPED (status
));
4805 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4807 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4812 linux_look_up_symbols (void)
4814 #ifdef USE_THREAD_DB
4815 struct process_info
*proc
= current_process ();
4817 if (proc
->private->thread_db
!= NULL
)
4820 /* If the kernel supports tracing forks then it also supports tracing
4821 clones, and then we don't need to use the magic thread event breakpoint
4822 to learn about threads. */
4823 thread_db_init (!linux_supports_tracefork_flag
);
4828 linux_request_interrupt (void)
4830 extern unsigned long signal_pid
;
4832 if (!ptid_equal (cont_thread
, null_ptid
)
4833 && !ptid_equal (cont_thread
, minus_one_ptid
))
4835 struct lwp_info
*lwp
;
4838 lwp
= get_thread_lwp (current_inferior
);
4839 lwpid
= lwpid_of (lwp
);
4840 kill_lwp (lwpid
, SIGINT
);
4843 kill_lwp (signal_pid
, SIGINT
);
4846 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
4847 to debugger memory starting at MYADDR. */
4850 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
4852 char filename
[PATH_MAX
];
4854 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4856 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
4858 fd
= open (filename
, O_RDONLY
);
4862 if (offset
!= (CORE_ADDR
) 0
4863 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4866 n
= read (fd
, myaddr
, len
);
4873 /* These breakpoint and watchpoint related wrapper functions simply
4874 pass on the function call if the target has registered a
4875 corresponding function. */
4878 linux_insert_point (char type
, CORE_ADDR addr
, int len
)
4880 if (the_low_target
.insert_point
!= NULL
)
4881 return the_low_target
.insert_point (type
, addr
, len
);
4883 /* Unsupported (see target.h). */
4888 linux_remove_point (char type
, CORE_ADDR addr
, int len
)
4890 if (the_low_target
.remove_point
!= NULL
)
4891 return the_low_target
.remove_point (type
, addr
, len
);
4893 /* Unsupported (see target.h). */
4898 linux_stopped_by_watchpoint (void)
4900 struct lwp_info
*lwp
= get_thread_lwp (current_inferior
);
4902 return lwp
->stopped_by_watchpoint
;
4906 linux_stopped_data_address (void)
4908 struct lwp_info
*lwp
= get_thread_lwp (current_inferior
);
4910 return lwp
->stopped_data_address
;
4913 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
4914 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
4915 && defined(PT_TEXT_END_ADDR)
4917 /* This is only used for targets that define PT_TEXT_ADDR,
4918 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
4919 the target has different ways of acquiring this information, like
4922 /* Under uClinux, programs are loaded at non-zero offsets, which we need
4923 to tell gdb about. */
4926 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
4928 unsigned long text
, text_end
, data
;
4929 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4933 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_ARG3_TYPE
) PT_TEXT_ADDR
,
4934 (PTRACE_ARG4_TYPE
) 0);
4935 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_ARG3_TYPE
) PT_TEXT_END_ADDR
,
4936 (PTRACE_ARG4_TYPE
) 0);
4937 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_ARG3_TYPE
) PT_DATA_ADDR
,
4938 (PTRACE_ARG4_TYPE
) 0);
4942 /* Both text and data offsets produced at compile-time (and so
4943 used by gdb) are relative to the beginning of the program,
4944 with the data segment immediately following the text segment.
4945 However, the actual runtime layout in memory may put the data
4946 somewhere else, so when we send gdb a data base-address, we
4947 use the real data base address and subtract the compile-time
4948 data base-address from it (which is just the length of the
4949 text segment). BSS immediately follows data in both
4952 *data_p
= data
- (text_end
- text
);
4961 linux_qxfer_osdata (const char *annex
,
4962 unsigned char *readbuf
, unsigned const char *writebuf
,
4963 CORE_ADDR offset
, int len
)
4965 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4968 /* Convert a native/host siginfo object, into/from the siginfo in the
4969 layout of the inferiors' architecture. */
4972 siginfo_fixup (siginfo_t
*siginfo
, void *inf_siginfo
, int direction
)
4976 if (the_low_target
.siginfo_fixup
!= NULL
)
4977 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
4979 /* If there was no callback, or the callback didn't do anything,
4980 then just do a straight memcpy. */
4984 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
4986 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
4991 linux_xfer_siginfo (const char *annex
, unsigned char *readbuf
,
4992 unsigned const char *writebuf
, CORE_ADDR offset
, int len
)
4996 char inf_siginfo
[sizeof (siginfo_t
)];
4998 if (current_inferior
== NULL
)
5001 pid
= lwpid_of (get_thread_lwp (current_inferior
));
5004 fprintf (stderr
, "%s siginfo for lwp %d.\n",
5005 readbuf
!= NULL
? "Reading" : "Writing",
5008 if (offset
>= sizeof (siginfo
))
5011 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_ARG3_TYPE
) 0, &siginfo
) != 0)
5014 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
5015 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
5016 inferior with a 64-bit GDBSERVER should look the same as debugging it
5017 with a 32-bit GDBSERVER, we need to convert it. */
5018 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
5020 if (offset
+ len
> sizeof (siginfo
))
5021 len
= sizeof (siginfo
) - offset
;
5023 if (readbuf
!= NULL
)
5024 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
5027 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
5029 /* Convert back to ptrace layout before flushing it out. */
5030 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
5032 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_ARG3_TYPE
) 0, &siginfo
) != 0)
5039 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
5040 so we notice when children change state; as the handler for the
5041 sigsuspend in my_waitpid. */
5044 sigchld_handler (int signo
)
5046 int old_errno
= errno
;
5052 /* fprintf is not async-signal-safe, so call write
5054 if (write (2, "sigchld_handler\n",
5055 sizeof ("sigchld_handler\n") - 1) < 0)
5056 break; /* just ignore */
5060 if (target_is_async_p ())
5061 async_file_mark (); /* trigger a linux_wait */
5067 linux_supports_non_stop (void)
5073 linux_async (int enable
)
5075 int previous
= (linux_event_pipe
[0] != -1);
5078 fprintf (stderr
, "linux_async (%d), previous=%d\n",
5081 if (previous
!= enable
)
5084 sigemptyset (&mask
);
5085 sigaddset (&mask
, SIGCHLD
);
5087 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
5091 if (pipe (linux_event_pipe
) == -1)
5092 fatal ("creating event pipe failed.");
5094 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
5095 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
5097 /* Register the event loop handler. */
5098 add_file_handler (linux_event_pipe
[0],
5099 handle_target_event
, NULL
);
5101 /* Always trigger a linux_wait. */
5106 delete_file_handler (linux_event_pipe
[0]);
5108 close (linux_event_pipe
[0]);
5109 close (linux_event_pipe
[1]);
5110 linux_event_pipe
[0] = -1;
5111 linux_event_pipe
[1] = -1;
5114 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
5121 linux_start_non_stop (int nonstop
)
5123 /* Register or unregister from event-loop accordingly. */
5124 linux_async (nonstop
);
5129 linux_supports_multi_process (void)
5135 linux_supports_disable_randomization (void)
5137 #ifdef HAVE_PERSONALITY
5145 linux_supports_agent (void)
5151 linux_supports_range_stepping (void)
5153 if (*the_low_target
.supports_range_stepping
== NULL
)
5156 return (*the_low_target
.supports_range_stepping
) ();
5159 /* Enumerate spufs IDs for process PID. */
5161 spu_enumerate_spu_ids (long pid
, unsigned char *buf
, CORE_ADDR offset
, int len
)
5167 struct dirent
*entry
;
5169 sprintf (path
, "/proc/%ld/fd", pid
);
5170 dir
= opendir (path
);
5175 while ((entry
= readdir (dir
)) != NULL
)
5181 fd
= atoi (entry
->d_name
);
5185 sprintf (path
, "/proc/%ld/fd/%d", pid
, fd
);
5186 if (stat (path
, &st
) != 0)
5188 if (!S_ISDIR (st
.st_mode
))
5191 if (statfs (path
, &stfs
) != 0)
5193 if (stfs
.f_type
!= SPUFS_MAGIC
)
5196 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
5198 *(unsigned int *)(buf
+ pos
- offset
) = fd
;
5208 /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
5209 object type, using the /proc file system. */
5211 linux_qxfer_spu (const char *annex
, unsigned char *readbuf
,
5212 unsigned const char *writebuf
,
5213 CORE_ADDR offset
, int len
)
5215 long pid
= lwpid_of (get_thread_lwp (current_inferior
));
5220 if (!writebuf
&& !readbuf
)
5228 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
5231 sprintf (buf
, "/proc/%ld/fd/%s", pid
, annex
);
5232 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
5237 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5244 ret
= write (fd
, writebuf
, (size_t) len
);
5246 ret
= read (fd
, readbuf
, (size_t) len
);
5252 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
5253 struct target_loadseg
5255 /* Core address to which the segment is mapped. */
5257 /* VMA recorded in the program header. */
5259 /* Size of this segment in memory. */
5263 # if defined PT_GETDSBT
5264 struct target_loadmap
5266 /* Protocol version number, must be zero. */
5268 /* Pointer to the DSBT table, its size, and the DSBT index. */
5269 unsigned *dsbt_table
;
5270 unsigned dsbt_size
, dsbt_index
;
5271 /* Number of segments in this map. */
5273 /* The actual memory map. */
5274 struct target_loadseg segs
[/*nsegs*/];
5276 # define LINUX_LOADMAP PT_GETDSBT
5277 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
5278 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
5280 struct target_loadmap
5282 /* Protocol version number, must be zero. */
5284 /* Number of segments in this map. */
5286 /* The actual memory map. */
5287 struct target_loadseg segs
[/*nsegs*/];
5289 # define LINUX_LOADMAP PTRACE_GETFDPIC
5290 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
5291 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
5295 linux_read_loadmap (const char *annex
, CORE_ADDR offset
,
5296 unsigned char *myaddr
, unsigned int len
)
5298 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
5300 struct target_loadmap
*data
= NULL
;
5301 unsigned int actual_length
, copy_length
;
5303 if (strcmp (annex
, "exec") == 0)
5304 addr
= (int) LINUX_LOADMAP_EXEC
;
5305 else if (strcmp (annex
, "interp") == 0)
5306 addr
= (int) LINUX_LOADMAP_INTERP
;
5310 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
5316 actual_length
= sizeof (struct target_loadmap
)
5317 + sizeof (struct target_loadseg
) * data
->nsegs
;
5319 if (offset
< 0 || offset
> actual_length
)
5322 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
5323 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
5327 # define linux_read_loadmap NULL
5328 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
5331 linux_process_qsupported (const char *query
)
5333 if (the_low_target
.process_qsupported
!= NULL
)
5334 the_low_target
.process_qsupported (query
);
5338 linux_supports_tracepoints (void)
5340 if (*the_low_target
.supports_tracepoints
== NULL
)
5343 return (*the_low_target
.supports_tracepoints
) ();
5347 linux_read_pc (struct regcache
*regcache
)
5349 if (the_low_target
.get_pc
== NULL
)
5352 return (*the_low_target
.get_pc
) (regcache
);
5356 linux_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
5358 gdb_assert (the_low_target
.set_pc
!= NULL
);
5360 (*the_low_target
.set_pc
) (regcache
, pc
);
5364 linux_thread_stopped (struct thread_info
*thread
)
5366 return get_thread_lwp (thread
)->stopped
;
5369 /* This exposes stop-all-threads functionality to other modules. */
5372 linux_pause_all (int freeze
)
5374 stop_all_lwps (freeze
, NULL
);
5377 /* This exposes unstop-all-threads functionality to other gdbserver
5381 linux_unpause_all (int unfreeze
)
5383 unstop_all_lwps (unfreeze
, NULL
);
5387 linux_prepare_to_access_memory (void)
5389 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5392 linux_pause_all (1);
5397 linux_done_accessing_memory (void)
5399 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5402 linux_unpause_all (1);
5406 linux_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
, CORE_ADDR tpaddr
,
5407 CORE_ADDR collector
,
5410 CORE_ADDR
*jump_entry
,
5411 CORE_ADDR
*trampoline
,
5412 ULONGEST
*trampoline_size
,
5413 unsigned char *jjump_pad_insn
,
5414 ULONGEST
*jjump_pad_insn_size
,
5415 CORE_ADDR
*adjusted_insn_addr
,
5416 CORE_ADDR
*adjusted_insn_addr_end
,
5419 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
5420 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
5421 jump_entry
, trampoline
, trampoline_size
,
5422 jjump_pad_insn
, jjump_pad_insn_size
,
5423 adjusted_insn_addr
, adjusted_insn_addr_end
,
5427 static struct emit_ops
*
5428 linux_emit_ops (void)
5430 if (the_low_target
.emit_ops
!= NULL
)
5431 return (*the_low_target
.emit_ops
) ();
5437 linux_get_min_fast_tracepoint_insn_len (void)
5439 return (*the_low_target
.get_min_fast_tracepoint_insn_len
) ();
5442 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
5445 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
5446 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
5448 char filename
[PATH_MAX
];
5450 const int auxv_size
= is_elf64
5451 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
5452 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
5454 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5456 fd
= open (filename
, O_RDONLY
);
5462 while (read (fd
, buf
, auxv_size
) == auxv_size
5463 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
5467 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
5469 switch (aux
->a_type
)
5472 *phdr_memaddr
= aux
->a_un
.a_val
;
5475 *num_phdr
= aux
->a_un
.a_val
;
5481 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
5483 switch (aux
->a_type
)
5486 *phdr_memaddr
= aux
->a_un
.a_val
;
5489 *num_phdr
= aux
->a_un
.a_val
;
5497 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
5499 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
5500 "phdr_memaddr = %ld, phdr_num = %d",
5501 (long) *phdr_memaddr
, *num_phdr
);
5508 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
5511 get_dynamic (const int pid
, const int is_elf64
)
5513 CORE_ADDR phdr_memaddr
, relocation
;
5515 unsigned char *phdr_buf
;
5516 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
5518 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
5521 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
5522 phdr_buf
= alloca (num_phdr
* phdr_size
);
5524 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
5527 /* Compute relocation: it is expected to be 0 for "regular" executables,
5528 non-zero for PIE ones. */
5530 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
5533 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5535 if (p
->p_type
== PT_PHDR
)
5536 relocation
= phdr_memaddr
- p
->p_vaddr
;
5540 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5542 if (p
->p_type
== PT_PHDR
)
5543 relocation
= phdr_memaddr
- p
->p_vaddr
;
5546 if (relocation
== -1)
5548 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
5549 any real world executables, including PIE executables, have always
5550 PT_PHDR present. PT_PHDR is not present in some shared libraries or
5551 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
5552 or present DT_DEBUG anyway (fpc binaries are statically linked).
5554 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
5556 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
5561 for (i
= 0; i
< num_phdr
; i
++)
5565 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5567 if (p
->p_type
== PT_DYNAMIC
)
5568 return p
->p_vaddr
+ relocation
;
5572 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5574 if (p
->p_type
== PT_DYNAMIC
)
5575 return p
->p_vaddr
+ relocation
;
5582 /* Return &_r_debug in the inferior, or -1 if not present. Return value
5583 can be 0 if the inferior does not yet have the library list initialized.
5584 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
5585 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
5588 get_r_debug (const int pid
, const int is_elf64
)
5590 CORE_ADDR dynamic_memaddr
;
5591 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
5592 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
5595 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
5596 if (dynamic_memaddr
== 0)
5599 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
5603 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
5604 #ifdef DT_MIPS_RLD_MAP
5608 unsigned char buf
[sizeof (Elf64_Xword
)];
5612 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
5614 if (linux_read_memory (dyn
->d_un
.d_val
,
5615 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
5620 #endif /* DT_MIPS_RLD_MAP */
5622 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
5623 map
= dyn
->d_un
.d_val
;
5625 if (dyn
->d_tag
== DT_NULL
)
5630 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
5631 #ifdef DT_MIPS_RLD_MAP
5635 unsigned char buf
[sizeof (Elf32_Word
)];
5639 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
5641 if (linux_read_memory (dyn
->d_un
.d_val
,
5642 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
5647 #endif /* DT_MIPS_RLD_MAP */
5649 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
5650 map
= dyn
->d_un
.d_val
;
5652 if (dyn
->d_tag
== DT_NULL
)
5656 dynamic_memaddr
+= dyn_size
;
5662 /* Read one pointer from MEMADDR in the inferior. */
5665 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
5669 /* Go through a union so this works on either big or little endian
5670 hosts, when the inferior's pointer size is smaller than the size
5671 of CORE_ADDR. It is assumed the inferior's endianness is the
5672 same of the superior's. */
5675 CORE_ADDR core_addr
;
5680 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
5683 if (ptr_size
== sizeof (CORE_ADDR
))
5684 *ptr
= addr
.core_addr
;
5685 else if (ptr_size
== sizeof (unsigned int))
5688 gdb_assert_not_reached ("unhandled pointer size");
5693 struct link_map_offsets
5695 /* Offset and size of r_debug.r_version. */
5696 int r_version_offset
;
5698 /* Offset and size of r_debug.r_map. */
5701 /* Offset to l_addr field in struct link_map. */
5704 /* Offset to l_name field in struct link_map. */
5707 /* Offset to l_ld field in struct link_map. */
5710 /* Offset to l_next field in struct link_map. */
5713 /* Offset to l_prev field in struct link_map. */
5717 /* Construct qXfer:libraries-svr4:read reply. */
5720 linux_qxfer_libraries_svr4 (const char *annex
, unsigned char *readbuf
,
5721 unsigned const char *writebuf
,
5722 CORE_ADDR offset
, int len
)
5725 unsigned document_len
;
5726 struct process_info_private
*const priv
= current_process ()->private;
5727 char filename
[PATH_MAX
];
5730 static const struct link_map_offsets lmo_32bit_offsets
=
5732 0, /* r_version offset. */
5733 4, /* r_debug.r_map offset. */
5734 0, /* l_addr offset in link_map. */
5735 4, /* l_name offset in link_map. */
5736 8, /* l_ld offset in link_map. */
5737 12, /* l_next offset in link_map. */
5738 16 /* l_prev offset in link_map. */
5741 static const struct link_map_offsets lmo_64bit_offsets
=
5743 0, /* r_version offset. */
5744 8, /* r_debug.r_map offset. */
5745 0, /* l_addr offset in link_map. */
5746 8, /* l_name offset in link_map. */
5747 16, /* l_ld offset in link_map. */
5748 24, /* l_next offset in link_map. */
5749 32 /* l_prev offset in link_map. */
5751 const struct link_map_offsets
*lmo
;
5752 unsigned int machine
;
5754 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
5755 int allocated
= 1024;
5757 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
5758 int header_done
= 0;
5760 if (writebuf
!= NULL
)
5762 if (readbuf
== NULL
)
5765 pid
= lwpid_of (get_thread_lwp (current_inferior
));
5766 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
5767 is_elf64
= elf_64_file_p (filename
, &machine
);
5768 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
5769 ptr_size
= is_elf64
? 8 : 4;
5771 while (annex
[0] != '\0')
5777 sep
= strchr (annex
, '=');
5782 if (len
== 5 && strncmp (annex
, "start", 5) == 0)
5784 else if (len
== 4 && strncmp (annex
, "prev", 4) == 0)
5788 annex
= strchr (sep
, ';');
5795 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
5802 if (priv
->r_debug
== 0)
5803 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
5805 /* We failed to find DT_DEBUG. Such situation will not change
5806 for this inferior - do not retry it. Report it to GDB as
5807 E01, see for the reasons at the GDB solib-svr4.c side. */
5808 if (priv
->r_debug
== (CORE_ADDR
) -1)
5811 if (priv
->r_debug
!= 0)
5813 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
5814 (unsigned char *) &r_version
,
5815 sizeof (r_version
)) != 0
5818 warning ("unexpected r_debug version %d", r_version
);
5820 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
5821 &lm_addr
, ptr_size
) != 0)
5823 warning ("unable to read r_map from 0x%lx",
5824 (long) priv
->r_debug
+ lmo
->r_map_offset
);
5829 document
= xmalloc (allocated
);
5830 strcpy (document
, "<library-list-svr4 version=\"1.0\"");
5831 p
= document
+ strlen (document
);
5834 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
5835 &l_name
, ptr_size
) == 0
5836 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
5837 &l_addr
, ptr_size
) == 0
5838 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
5839 &l_ld
, ptr_size
) == 0
5840 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
5841 &l_prev
, ptr_size
) == 0
5842 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
5843 &l_next
, ptr_size
) == 0)
5845 unsigned char libname
[PATH_MAX
];
5847 if (lm_prev
!= l_prev
)
5849 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
5850 (long) lm_prev
, (long) l_prev
);
5854 /* Not checking for error because reading may stop before
5855 we've got PATH_MAX worth of characters. */
5857 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
5858 libname
[sizeof (libname
) - 1] = '\0';
5859 if (libname
[0] != '\0')
5861 /* 6x the size for xml_escape_text below. */
5862 size_t len
= 6 * strlen ((char *) libname
);
5867 /* Terminate `<library-list-svr4'. */
5872 while (allocated
< p
- document
+ len
+ 200)
5874 /* Expand to guarantee sufficient storage. */
5875 uintptr_t document_len
= p
- document
;
5877 document
= xrealloc (document
, 2 * allocated
);
5879 p
= document
+ document_len
;
5882 name
= xml_escape_text ((char *) libname
);
5883 p
+= sprintf (p
, "<library name=\"%s\" lm=\"0x%lx\" "
5884 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
5885 name
, (unsigned long) lm_addr
,
5886 (unsigned long) l_addr
, (unsigned long) l_ld
);
5889 else if (lm_prev
== 0)
5891 sprintf (p
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
5901 /* Empty list; terminate `<library-list-svr4'. */
5905 strcpy (p
, "</library-list-svr4>");
5907 document_len
= strlen (document
);
5908 if (offset
< document_len
)
5909 document_len
-= offset
;
5912 if (len
> document_len
)
5915 memcpy (readbuf
, document
+ offset
, len
);
5921 #ifdef HAVE_LINUX_BTRACE
5923 /* Enable branch tracing. */
5925 static struct btrace_target_info
*
5926 linux_low_enable_btrace (ptid_t ptid
)
5928 struct btrace_target_info
*tinfo
;
5930 tinfo
= linux_enable_btrace (ptid
);
5934 struct thread_info
*thread
= find_thread_ptid (ptid
);
5935 struct regcache
*regcache
= get_thread_regcache (thread
, 0);
5937 tinfo
->ptr_bits
= register_size (regcache
->tdesc
, 0) * 8;
5943 /* Read branch trace data as btrace xml document. */
5946 linux_low_read_btrace (struct btrace_target_info
*tinfo
, struct buffer
*buffer
,
5949 VEC (btrace_block_s
) *btrace
;
5950 struct btrace_block
*block
;
5953 btrace
= linux_read_btrace (tinfo
, type
);
5955 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
5956 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
5958 for (i
= 0; VEC_iterate (btrace_block_s
, btrace
, i
, block
); i
++)
5959 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
5960 paddress (block
->begin
), paddress (block
->end
));
5962 buffer_grow_str (buffer
, "</btrace>\n");
5964 VEC_free (btrace_block_s
, btrace
);
5966 #endif /* HAVE_LINUX_BTRACE */
5968 static struct target_ops linux_target_ops
= {
5969 linux_create_inferior
,
5978 linux_fetch_registers
,
5979 linux_store_registers
,
5980 linux_prepare_to_access_memory
,
5981 linux_done_accessing_memory
,
5984 linux_look_up_symbols
,
5985 linux_request_interrupt
,
5989 linux_stopped_by_watchpoint
,
5990 linux_stopped_data_address
,
5991 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
5992 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
5993 && defined(PT_TEXT_END_ADDR)
5998 #ifdef USE_THREAD_DB
5999 thread_db_get_tls_address
,
6004 hostio_last_error_from_errno
,
6007 linux_supports_non_stop
,
6009 linux_start_non_stop
,
6010 linux_supports_multi_process
,
6011 #ifdef USE_THREAD_DB
6012 thread_db_handle_monitor_command
,
6016 linux_common_core_of_thread
,
6018 linux_process_qsupported
,
6019 linux_supports_tracepoints
,
6022 linux_thread_stopped
,
6026 linux_cancel_breakpoints
,
6027 linux_stabilize_threads
,
6028 linux_install_fast_tracepoint_jump_pad
,
6030 linux_supports_disable_randomization
,
6031 linux_get_min_fast_tracepoint_insn_len
,
6032 linux_qxfer_libraries_svr4
,
6033 linux_supports_agent
,
6034 #ifdef HAVE_LINUX_BTRACE
6035 linux_supports_btrace
,
6036 linux_low_enable_btrace
,
6037 linux_disable_btrace
,
6038 linux_low_read_btrace
,
6045 linux_supports_range_stepping
,
6049 linux_init_signals ()
6051 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
6052 to find what the cancel signal actually is. */
6053 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
6054 signal (__SIGRTMIN
+1, SIG_IGN
);
6058 #ifdef HAVE_LINUX_REGSETS
6060 initialize_regsets_info (struct regsets_info
*info
)
6062 for (info
->num_regsets
= 0;
6063 info
->regsets
[info
->num_regsets
].size
>= 0;
6064 info
->num_regsets
++)
6066 info
->disabled_regsets
= xmalloc (info
->num_regsets
);
6071 initialize_low (void)
6073 struct sigaction sigchld_action
;
6074 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
6075 set_target_ops (&linux_target_ops
);
6076 set_breakpoint_data (the_low_target
.breakpoint
,
6077 the_low_target
.breakpoint_len
);
6078 linux_init_signals ();
6079 linux_test_for_tracefork ();
6080 linux_ptrace_init_warnings ();
6082 sigchld_action
.sa_handler
= sigchld_handler
;
6083 sigemptyset (&sigchld_action
.sa_mask
);
6084 sigchld_action
.sa_flags
= SA_RESTART
;
6085 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
6087 initialize_low_arch ();