1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2014 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"
24 #include "nat/linux-nat.h"
25 #include "nat/linux-waitpid.h"
28 #include <sys/ptrace.h>
29 #include "linux-ptrace.h"
30 #include "linux-procfs.h"
32 #include <sys/ioctl.h>
38 #include <sys/syscall.h>
42 #include <sys/types.h>
47 #include "filestuff.h"
48 #include "tracepoint.h"
51 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
52 then ELFMAG0 will have been defined. If it didn't get included by
53 gdb_proc_service.h then including it will likely introduce a duplicate
54 definition of elf_fpregset_t. */
59 #define SPUFS_MAGIC 0x23c9b64e
62 #ifdef HAVE_PERSONALITY
63 # include <sys/personality.h>
64 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
65 # define ADDR_NO_RANDOMIZE 0x0040000
74 #define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
77 /* This is the kernel's hard limit. Not to be confused with
83 /* Some targets did not define these ptrace constants from the start,
84 so gdbserver defines them locally here. In the future, these may
85 be removed after they are added to asm/ptrace.h. */
86 #if !(defined(PT_TEXT_ADDR) \
87 || defined(PT_DATA_ADDR) \
88 || defined(PT_TEXT_END_ADDR))
89 #if defined(__mcoldfire__)
90 /* These are still undefined in 3.10 kernels. */
91 #define PT_TEXT_ADDR 49*4
92 #define PT_DATA_ADDR 50*4
93 #define PT_TEXT_END_ADDR 51*4
94 /* BFIN already defines these since at least 2.6.32 kernels. */
96 #define PT_TEXT_ADDR 220
97 #define PT_TEXT_END_ADDR 224
98 #define PT_DATA_ADDR 228
99 /* These are still undefined in 3.10 kernels. */
100 #elif defined(__TMS320C6X__)
101 #define PT_TEXT_ADDR (0x10000*4)
102 #define PT_DATA_ADDR (0x10004*4)
103 #define PT_TEXT_END_ADDR (0x10008*4)
107 #ifdef HAVE_LINUX_BTRACE
108 # include "linux-btrace.h"
111 #ifndef HAVE_ELF32_AUXV_T
112 /* Copied from glibc's elf.h. */
115 uint32_t a_type
; /* Entry type */
118 uint32_t a_val
; /* Integer value */
119 /* We use to have pointer elements added here. We cannot do that,
120 though, since it does not work when using 32-bit definitions
121 on 64-bit platforms and vice versa. */
126 #ifndef HAVE_ELF64_AUXV_T
127 /* Copied from glibc's elf.h. */
130 uint64_t a_type
; /* Entry type */
133 uint64_t a_val
; /* Integer value */
134 /* We use to have pointer elements added here. We cannot do that,
135 though, since it does not work when using 32-bit definitions
136 on 64-bit platforms and vice versa. */
141 /* A list of all unknown processes which receive stop signals. Some
142 other process will presumably claim each of these as forked
143 children momentarily. */
145 struct simple_pid_list
147 /* The process ID. */
150 /* The status as reported by waitpid. */
154 struct simple_pid_list
*next
;
156 struct simple_pid_list
*stopped_pids
;
158 /* Trivial list manipulation functions to keep track of a list of new
159 stopped processes. */
162 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
164 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
167 new_pid
->status
= status
;
168 new_pid
->next
= *listp
;
173 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
175 struct simple_pid_list
**p
;
177 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
178 if ((*p
)->pid
== pid
)
180 struct simple_pid_list
*next
= (*p
)->next
;
182 *statusp
= (*p
)->status
;
190 enum stopping_threads_kind
192 /* Not stopping threads presently. */
193 NOT_STOPPING_THREADS
,
195 /* Stopping threads. */
198 /* Stopping and suspending threads. */
199 STOPPING_AND_SUSPENDING_THREADS
202 /* This is set while stop_all_lwps is in effect. */
203 enum stopping_threads_kind stopping_threads
= NOT_STOPPING_THREADS
;
205 /* FIXME make into a target method? */
206 int using_threads
= 1;
208 /* True if we're presently stabilizing threads (moving them out of
210 static int stabilizing_threads
;
212 static void linux_resume_one_lwp (struct lwp_info
*lwp
,
213 int step
, int signal
, siginfo_t
*info
);
214 static void linux_resume (struct thread_resume
*resume_info
, size_t n
);
215 static void stop_all_lwps (int suspend
, struct lwp_info
*except
);
216 static void unstop_all_lwps (int unsuspend
, struct lwp_info
*except
);
217 static int linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
);
218 static struct lwp_info
*add_lwp (ptid_t ptid
);
219 static int linux_stopped_by_watchpoint (void);
220 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
221 static void proceed_all_lwps (void);
222 static int finish_step_over (struct lwp_info
*lwp
);
223 static CORE_ADDR
get_stop_pc (struct lwp_info
*lwp
);
224 static int kill_lwp (unsigned long lwpid
, int signo
);
226 /* True if the low target can hardware single-step. Such targets
227 don't need a BREAKPOINT_REINSERT_ADDR callback. */
230 can_hardware_single_step (void)
232 return (the_low_target
.breakpoint_reinsert_addr
== NULL
);
235 /* True if the low target supports memory breakpoints. If so, we'll
236 have a GET_PC implementation. */
239 supports_breakpoints (void)
241 return (the_low_target
.get_pc
!= NULL
);
244 /* Returns true if this target can support fast tracepoints. This
245 does not mean that the in-process agent has been loaded in the
249 supports_fast_tracepoints (void)
251 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
254 /* True if LWP is stopped in its stepping range. */
257 lwp_in_step_range (struct lwp_info
*lwp
)
259 CORE_ADDR pc
= lwp
->stop_pc
;
261 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
264 struct pending_signals
268 struct pending_signals
*prev
;
271 /* The read/write ends of the pipe registered as waitable file in the
273 static int linux_event_pipe
[2] = { -1, -1 };
275 /* True if we're currently in async mode. */
276 #define target_is_async_p() (linux_event_pipe[0] != -1)
278 static void send_sigstop (struct lwp_info
*lwp
);
279 static void wait_for_sigstop (struct inferior_list_entry
*entry
);
281 /* Return non-zero if HEADER is a 64-bit ELF file. */
284 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
286 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
287 && header
->e_ident
[EI_MAG1
] == ELFMAG1
288 && header
->e_ident
[EI_MAG2
] == ELFMAG2
289 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
291 *machine
= header
->e_machine
;
292 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
299 /* Return non-zero if FILE is a 64-bit ELF file,
300 zero if the file is not a 64-bit ELF file,
301 and -1 if the file is not accessible or doesn't exist. */
304 elf_64_file_p (const char *file
, unsigned int *machine
)
309 fd
= open (file
, O_RDONLY
);
313 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
320 return elf_64_header_p (&header
, machine
);
323 /* Accepts an integer PID; Returns true if the executable PID is
324 running is a 64-bit ELF file.. */
327 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
331 sprintf (file
, "/proc/%d/exe", pid
);
332 return elf_64_file_p (file
, machine
);
336 delete_lwp (struct lwp_info
*lwp
)
338 remove_thread (get_lwp_thread (lwp
));
339 free (lwp
->arch_private
);
343 /* Add a process to the common process list, and set its private
346 static struct process_info
*
347 linux_add_process (int pid
, int attached
)
349 struct process_info
*proc
;
351 proc
= add_process (pid
, attached
);
352 proc
->private = xcalloc (1, sizeof (*proc
->private));
354 /* Set the arch when the first LWP stops. */
355 proc
->private->new_inferior
= 1;
357 if (the_low_target
.new_process
!= NULL
)
358 proc
->private->arch_private
= the_low_target
.new_process ();
363 /* Handle a GNU/Linux extended wait response. If we see a clone
364 event, we need to add the new LWP to our list (and not report the
365 trap to higher layers). */
368 handle_extended_wait (struct lwp_info
*event_child
, int wstat
)
370 int event
= wstat
>> 16;
371 struct thread_info
*event_thr
= get_lwp_thread (event_child
);
372 struct lwp_info
*new_lwp
;
374 if (event
== PTRACE_EVENT_CLONE
)
377 unsigned long new_pid
;
380 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_thr
), (PTRACE_TYPE_ARG3
) 0,
383 /* If we haven't already seen the new PID stop, wait for it now. */
384 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
386 /* The new child has a pending SIGSTOP. We can't affect it until it
387 hits the SIGSTOP, but we're already attached. */
389 ret
= my_waitpid (new_pid
, &status
, __WALL
);
392 perror_with_name ("waiting for new child");
393 else if (ret
!= new_pid
)
394 warning ("wait returned unexpected PID %d", ret
);
395 else if (!WIFSTOPPED (status
))
396 warning ("wait returned unexpected status 0x%x", status
);
399 ptid
= ptid_build (pid_of (event_thr
), new_pid
, 0);
400 new_lwp
= add_lwp (ptid
);
402 /* Either we're going to immediately resume the new thread
403 or leave it stopped. linux_resume_one_lwp is a nop if it
404 thinks the thread is currently running, so set this first
405 before calling linux_resume_one_lwp. */
406 new_lwp
->stopped
= 1;
408 /* If we're suspending all threads, leave this one suspended
410 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
)
411 new_lwp
->suspended
= 1;
413 /* Normally we will get the pending SIGSTOP. But in some cases
414 we might get another signal delivered to the group first.
415 If we do get another signal, be sure not to lose it. */
416 if (WSTOPSIG (status
) == SIGSTOP
)
418 if (stopping_threads
!= NOT_STOPPING_THREADS
)
419 new_lwp
->stop_pc
= get_stop_pc (new_lwp
);
421 linux_resume_one_lwp (new_lwp
, 0, 0, NULL
);
425 new_lwp
->stop_expected
= 1;
427 if (stopping_threads
!= NOT_STOPPING_THREADS
)
429 new_lwp
->stop_pc
= get_stop_pc (new_lwp
);
430 new_lwp
->status_pending_p
= 1;
431 new_lwp
->status_pending
= status
;
434 /* Pass the signal on. This is what GDB does - except
435 shouldn't we really report it instead? */
436 linux_resume_one_lwp (new_lwp
, 0, WSTOPSIG (status
), NULL
);
439 /* Always resume the current thread. If we are stopping
440 threads, it will have a pending SIGSTOP; we may as well
442 linux_resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
446 /* Return the PC as read from the regcache of LWP, without any
450 get_pc (struct lwp_info
*lwp
)
452 struct thread_info
*saved_inferior
;
453 struct regcache
*regcache
;
456 if (the_low_target
.get_pc
== NULL
)
459 saved_inferior
= current_inferior
;
460 current_inferior
= get_lwp_thread (lwp
);
462 regcache
= get_thread_regcache (current_inferior
, 1);
463 pc
= (*the_low_target
.get_pc
) (regcache
);
466 debug_printf ("pc is 0x%lx\n", (long) pc
);
468 current_inferior
= saved_inferior
;
472 /* This function should only be called if LWP got a SIGTRAP.
473 The SIGTRAP could mean several things.
475 On i386, where decr_pc_after_break is non-zero:
476 If we were single-stepping this process using PTRACE_SINGLESTEP,
477 we will get only the one SIGTRAP (even if the instruction we
478 stepped over was a breakpoint). The value of $eip will be the
480 If we continue the process using PTRACE_CONT, we will get a
481 SIGTRAP when we hit a breakpoint. The value of $eip will be
482 the instruction after the breakpoint (i.e. needs to be
483 decremented). If we report the SIGTRAP to GDB, we must also
484 report the undecremented PC. If we cancel the SIGTRAP, we
485 must resume at the decremented PC.
487 (Presumably, not yet tested) On a non-decr_pc_after_break machine
488 with hardware or kernel single-step:
489 If we single-step over a breakpoint instruction, our PC will
490 point at the following instruction. If we continue and hit a
491 breakpoint instruction, our PC will point at the breakpoint
495 get_stop_pc (struct lwp_info
*lwp
)
499 if (the_low_target
.get_pc
== NULL
)
502 stop_pc
= get_pc (lwp
);
504 if (WSTOPSIG (lwp
->last_status
) == SIGTRAP
506 && !lwp
->stopped_by_watchpoint
507 && lwp
->last_status
>> 16 == 0)
508 stop_pc
-= the_low_target
.decr_pc_after_break
;
511 debug_printf ("stop pc is 0x%lx\n", (long) stop_pc
);
516 static struct lwp_info
*
517 add_lwp (ptid_t ptid
)
519 struct lwp_info
*lwp
;
521 lwp
= (struct lwp_info
*) xmalloc (sizeof (*lwp
));
522 memset (lwp
, 0, sizeof (*lwp
));
524 if (the_low_target
.new_thread
!= NULL
)
525 lwp
->arch_private
= the_low_target
.new_thread ();
527 lwp
->thread
= add_thread (ptid
, lwp
);
532 /* Start an inferior process and returns its pid.
533 ALLARGS is a vector of program-name and args. */
536 linux_create_inferior (char *program
, char **allargs
)
538 #ifdef HAVE_PERSONALITY
539 int personality_orig
= 0, personality_set
= 0;
541 struct lwp_info
*new_lwp
;
545 #ifdef HAVE_PERSONALITY
546 if (disable_randomization
)
549 personality_orig
= personality (0xffffffff);
550 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
553 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
555 if (errno
!= 0 || (personality_set
556 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
557 warning ("Error disabling address space randomization: %s",
562 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
568 perror_with_name ("fork");
573 ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
575 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
576 signal (__SIGRTMIN
+ 1, SIG_DFL
);
581 /* If gdbserver is connected to gdb via stdio, redirect the inferior's
582 stdout to stderr so that inferior i/o doesn't corrupt the connection.
583 Also, redirect stdin to /dev/null. */
584 if (remote_connection_is_stdio ())
587 open ("/dev/null", O_RDONLY
);
589 if (write (2, "stdin/stdout redirected\n",
590 sizeof ("stdin/stdout redirected\n") - 1) < 0)
592 /* Errors ignored. */;
596 execv (program
, allargs
);
598 execvp (program
, allargs
);
600 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
606 #ifdef HAVE_PERSONALITY
610 personality (personality_orig
);
612 warning ("Error restoring address space randomization: %s",
617 linux_add_process (pid
, 0);
619 ptid
= ptid_build (pid
, pid
, 0);
620 new_lwp
= add_lwp (ptid
);
621 new_lwp
->must_set_ptrace_flags
= 1;
626 /* Attach to an inferior process. */
629 linux_attach_lwp_1 (unsigned long lwpid
, int initial
)
632 struct lwp_info
*new_lwp
;
634 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0)
637 struct buffer buffer
;
641 /* If we fail to attach to an LWP, just warn. */
642 fprintf (stderr
, "Cannot attach to lwp %ld: %s (%d)\n", lwpid
,
643 strerror (errno
), errno
);
648 /* If we fail to attach to a process, report an error. */
649 buffer_init (&buffer
);
650 linux_ptrace_attach_warnings (lwpid
, &buffer
);
651 buffer_grow_str0 (&buffer
, "");
652 error ("%sCannot attach to lwp %ld: %s (%d)", buffer_finish (&buffer
),
653 lwpid
, strerror (errno
), errno
);
657 /* If lwp is the tgid, we handle adding existing threads later.
658 Otherwise we just add lwp without bothering about any other
660 ptid
= ptid_build (lwpid
, lwpid
, 0);
663 /* Note that extracting the pid from the current inferior is
664 safe, since we're always called in the context of the same
665 process as this new thread. */
666 int pid
= pid_of (current_inferior
);
667 ptid
= ptid_build (pid
, lwpid
, 0);
670 new_lwp
= add_lwp (ptid
);
672 /* We need to wait for SIGSTOP before being able to make the next
673 ptrace call on this LWP. */
674 new_lwp
->must_set_ptrace_flags
= 1;
676 if (linux_proc_pid_is_stopped (lwpid
))
679 debug_printf ("Attached to a stopped process\n");
681 /* The process is definitely stopped. It is in a job control
682 stop, unless the kernel predates the TASK_STOPPED /
683 TASK_TRACED distinction, in which case it might be in a
684 ptrace stop. Make sure it is in a ptrace stop; from there we
685 can kill it, signal it, et cetera.
687 First make sure there is a pending SIGSTOP. Since we are
688 already attached, the process can not transition from stopped
689 to running without a PTRACE_CONT; so we know this signal will
690 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
691 probably already in the queue (unless this kernel is old
692 enough to use TASK_STOPPED for ptrace stops); but since
693 SIGSTOP is not an RT signal, it can only be queued once. */
694 kill_lwp (lwpid
, SIGSTOP
);
696 /* Finally, resume the stopped process. This will deliver the
697 SIGSTOP (or a higher priority signal, just like normal
698 PTRACE_ATTACH), which we'll catch later on. */
699 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
702 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
705 There are several cases to consider here:
707 1) gdbserver has already attached to the process and is being notified
708 of a new thread that is being created.
709 In this case we should ignore that SIGSTOP and resume the
710 process. This is handled below by setting stop_expected = 1,
711 and the fact that add_thread sets last_resume_kind ==
714 2) This is the first thread (the process thread), and we're attaching
715 to it via attach_inferior.
716 In this case we want the process thread to stop.
717 This is handled by having linux_attach set last_resume_kind ==
718 resume_stop after we return.
720 If the pid we are attaching to is also the tgid, we attach to and
721 stop all the existing threads. Otherwise, we attach to pid and
722 ignore any other threads in the same group as this pid.
724 3) GDB is connecting to gdbserver and is requesting an enumeration of all
726 In this case we want the thread to stop.
727 FIXME: This case is currently not properly handled.
728 We should wait for the SIGSTOP but don't. Things work apparently
729 because enough time passes between when we ptrace (ATTACH) and when
730 gdb makes the next ptrace call on the thread.
732 On the other hand, if we are currently trying to stop all threads, we
733 should treat the new thread as if we had sent it a SIGSTOP. This works
734 because we are guaranteed that the add_lwp call above added us to the
735 end of the list, and so the new thread has not yet reached
736 wait_for_sigstop (but will). */
737 new_lwp
->stop_expected
= 1;
741 linux_attach_lwp (unsigned long lwpid
)
743 linux_attach_lwp_1 (lwpid
, 0);
746 /* Attach to PID. If PID is the tgid, attach to it and all
750 linux_attach (unsigned long pid
)
752 /* Attach to PID. We will check for other threads
754 linux_attach_lwp_1 (pid
, 1);
755 linux_add_process (pid
, 1);
759 struct thread_info
*thread
;
761 /* Don't ignore the initial SIGSTOP if we just attached to this
762 process. It will be collected by wait shortly. */
763 thread
= find_thread_ptid (ptid_build (pid
, pid
, 0));
764 thread
->last_resume_kind
= resume_stop
;
767 if (linux_proc_get_tgid (pid
) == pid
)
772 sprintf (pathname
, "/proc/%ld/task", pid
);
774 dir
= opendir (pathname
);
778 fprintf (stderr
, "Could not open /proc/%ld/task.\n", pid
);
783 /* At this point we attached to the tgid. Scan the task for
786 int new_threads_found
;
790 while (iterations
< 2)
792 new_threads_found
= 0;
793 /* Add all the other threads. While we go through the
794 threads, new threads may be spawned. Cycle through
795 the list of threads until we have done two iterations without
796 finding new threads. */
797 while ((dp
= readdir (dir
)) != NULL
)
800 lwp
= strtoul (dp
->d_name
, NULL
, 10);
802 /* Is this a new thread? */
804 && find_thread_ptid (ptid_build (pid
, lwp
, 0)) == NULL
)
806 linux_attach_lwp_1 (lwp
, 0);
810 debug_printf ("Found and attached to new lwp %ld\n",
815 if (!new_threads_found
)
836 second_thread_of_pid_p (struct inferior_list_entry
*entry
, void *args
)
838 struct counter
*counter
= args
;
840 if (ptid_get_pid (entry
->id
) == counter
->pid
)
842 if (++counter
->count
> 1)
850 last_thread_of_process_p (struct thread_info
*thread
)
852 ptid_t ptid
= thread
->entry
.id
;
853 int pid
= ptid_get_pid (ptid
);
854 struct counter counter
= { pid
, 0 };
856 return (find_inferior (&all_threads
,
857 second_thread_of_pid_p
, &counter
) == NULL
);
863 linux_kill_one_lwp (struct lwp_info
*lwp
)
865 struct thread_info
*thr
= get_lwp_thread (lwp
);
866 int pid
= lwpid_of (thr
);
868 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
869 there is no signal context, and ptrace(PTRACE_KILL) (or
870 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
871 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
872 alternative is to kill with SIGKILL. We only need one SIGKILL
873 per process, not one for each thread. But since we still support
874 linuxthreads, and we also support debugging programs using raw
875 clone without CLONE_THREAD, we send one for each thread. For
876 years, we used PTRACE_KILL only, so we're being a bit paranoid
877 about some old kernels where PTRACE_KILL might work better
878 (dubious if there are any such, but that's why it's paranoia), so
879 we try SIGKILL first, PTRACE_KILL second, and so we're fine
885 debug_printf ("LKL: kill (SIGKILL) %s, 0, 0 (%s)\n",
886 target_pid_to_str (ptid_of (thr
)),
887 errno
? strerror (errno
) : "OK");
890 ptrace (PTRACE_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
892 debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
893 target_pid_to_str (ptid_of (thr
)),
894 errno
? strerror (errno
) : "OK");
897 /* Callback for `find_inferior'. Kills an lwp of a given process,
898 except the leader. */
901 kill_one_lwp_callback (struct inferior_list_entry
*entry
, void *args
)
903 struct thread_info
*thread
= (struct thread_info
*) entry
;
904 struct lwp_info
*lwp
= get_thread_lwp (thread
);
906 int pid
= * (int *) args
;
908 if (ptid_get_pid (entry
->id
) != pid
)
911 /* We avoid killing the first thread here, because of a Linux kernel (at
912 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
913 the children get a chance to be reaped, it will remain a zombie
916 if (lwpid_of (thread
) == pid
)
919 debug_printf ("lkop: is last of process %s\n",
920 target_pid_to_str (entry
->id
));
926 linux_kill_one_lwp (lwp
);
928 /* Make sure it died. The loop is most likely unnecessary. */
929 pid
= linux_wait_for_event (thread
->entry
.id
, &wstat
, __WALL
);
930 } while (pid
> 0 && WIFSTOPPED (wstat
));
938 struct process_info
*process
;
939 struct lwp_info
*lwp
;
943 process
= find_process_pid (pid
);
947 /* If we're killing a running inferior, make sure it is stopped
948 first, as PTRACE_KILL will not work otherwise. */
949 stop_all_lwps (0, NULL
);
951 find_inferior (&all_threads
, kill_one_lwp_callback
, &pid
);
953 /* See the comment in linux_kill_one_lwp. We did not kill the first
954 thread in the list, so do so now. */
955 lwp
= find_lwp_pid (pid_to_ptid (pid
));
960 debug_printf ("lk_1: cannot find lwp for pid: %d\n",
965 struct thread_info
*thr
= get_lwp_thread (lwp
);
968 debug_printf ("lk_1: killing lwp %ld, for pid: %d\n",
969 lwpid_of (thr
), pid
);
973 linux_kill_one_lwp (lwp
);
975 /* Make sure it died. The loop is most likely unnecessary. */
976 lwpid
= linux_wait_for_event (thr
->entry
.id
, &wstat
, __WALL
);
977 } while (lwpid
> 0 && WIFSTOPPED (wstat
));
980 the_target
->mourn (process
);
982 /* Since we presently can only stop all lwps of all processes, we
983 need to unstop lwps of other processes. */
984 unstop_all_lwps (0, NULL
);
988 /* Get pending signal of THREAD, for detaching purposes. This is the
989 signal the thread last stopped for, which we need to deliver to the
990 thread when detaching, otherwise, it'd be suppressed/lost. */
993 get_detach_signal (struct thread_info
*thread
)
995 enum gdb_signal signo
= GDB_SIGNAL_0
;
997 struct lwp_info
*lp
= get_thread_lwp (thread
);
999 if (lp
->status_pending_p
)
1000 status
= lp
->status_pending
;
1003 /* If the thread had been suspended by gdbserver, and it stopped
1004 cleanly, then it'll have stopped with SIGSTOP. But we don't
1005 want to deliver that SIGSTOP. */
1006 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1007 || thread
->last_status
.value
.sig
== GDB_SIGNAL_0
)
1010 /* Otherwise, we may need to deliver the signal we
1012 status
= lp
->last_status
;
1015 if (!WIFSTOPPED (status
))
1018 debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1019 target_pid_to_str (ptid_of (thread
)));
1023 /* Extended wait statuses aren't real SIGTRAPs. */
1024 if (WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1027 debug_printf ("GPS: lwp %s had stopped with extended "
1028 "status: no pending signal\n",
1029 target_pid_to_str (ptid_of (thread
)));
1033 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1035 if (program_signals_p
&& !program_signals
[signo
])
1038 debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1039 target_pid_to_str (ptid_of (thread
)),
1040 gdb_signal_to_string (signo
));
1043 else if (!program_signals_p
1044 /* If we have no way to know which signals GDB does not
1045 want to have passed to the program, assume
1046 SIGTRAP/SIGINT, which is GDB's default. */
1047 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1050 debug_printf ("GPS: lwp %s had signal %s, "
1051 "but we don't know if we should pass it. "
1052 "Default to not.\n",
1053 target_pid_to_str (ptid_of (thread
)),
1054 gdb_signal_to_string (signo
));
1060 debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1061 target_pid_to_str (ptid_of (thread
)),
1062 gdb_signal_to_string (signo
));
1064 return WSTOPSIG (status
);
1069 linux_detach_one_lwp (struct inferior_list_entry
*entry
, void *args
)
1071 struct thread_info
*thread
= (struct thread_info
*) entry
;
1072 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1073 int pid
= * (int *) args
;
1076 if (ptid_get_pid (entry
->id
) != pid
)
1079 /* If there is a pending SIGSTOP, get rid of it. */
1080 if (lwp
->stop_expected
)
1083 debug_printf ("Sending SIGCONT to %s\n",
1084 target_pid_to_str (ptid_of (thread
)));
1086 kill_lwp (lwpid_of (thread
), SIGCONT
);
1087 lwp
->stop_expected
= 0;
1090 /* Flush any pending changes to the process's registers. */
1091 regcache_invalidate_thread (thread
);
1093 /* Pass on any pending signal for this thread. */
1094 sig
= get_detach_signal (thread
);
1096 /* Finally, let it resume. */
1097 if (the_low_target
.prepare_to_resume
!= NULL
)
1098 the_low_target
.prepare_to_resume (lwp
);
1099 if (ptrace (PTRACE_DETACH
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
1100 (PTRACE_TYPE_ARG4
) (long) sig
) < 0)
1101 error (_("Can't detach %s: %s"),
1102 target_pid_to_str (ptid_of (thread
)),
1110 linux_detach (int pid
)
1112 struct process_info
*process
;
1114 process
= find_process_pid (pid
);
1115 if (process
== NULL
)
1118 /* Stop all threads before detaching. First, ptrace requires that
1119 the thread is stopped to sucessfully detach. Second, thread_db
1120 may need to uninstall thread event breakpoints from memory, which
1121 only works with a stopped process anyway. */
1122 stop_all_lwps (0, NULL
);
1124 #ifdef USE_THREAD_DB
1125 thread_db_detach (process
);
1128 /* Stabilize threads (move out of jump pads). */
1129 stabilize_threads ();
1131 find_inferior (&all_threads
, linux_detach_one_lwp
, &pid
);
1133 the_target
->mourn (process
);
1135 /* Since we presently can only stop all lwps of all processes, we
1136 need to unstop lwps of other processes. */
1137 unstop_all_lwps (0, NULL
);
1141 /* Remove all LWPs that belong to process PROC from the lwp list. */
1144 delete_lwp_callback (struct inferior_list_entry
*entry
, void *proc
)
1146 struct thread_info
*thread
= (struct thread_info
*) entry
;
1147 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1148 struct process_info
*process
= proc
;
1150 if (pid_of (thread
) == pid_of (process
))
1157 linux_mourn (struct process_info
*process
)
1159 struct process_info_private
*priv
;
1161 #ifdef USE_THREAD_DB
1162 thread_db_mourn (process
);
1165 find_inferior (&all_threads
, delete_lwp_callback
, process
);
1167 /* Freeing all private data. */
1168 priv
= process
->private;
1169 free (priv
->arch_private
);
1171 process
->private = NULL
;
1173 remove_process (process
);
1177 linux_join (int pid
)
1182 ret
= my_waitpid (pid
, &status
, 0);
1183 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1185 } while (ret
!= -1 || errno
!= ECHILD
);
1188 /* Return nonzero if the given thread is still alive. */
1190 linux_thread_alive (ptid_t ptid
)
1192 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1194 /* We assume we always know if a thread exits. If a whole process
1195 exited but we still haven't been able to report it to GDB, we'll
1196 hold on to the last lwp of the dead process. */
1203 /* Return 1 if this lwp has an interesting status pending. */
1205 status_pending_p_callback (struct inferior_list_entry
*entry
, void *arg
)
1207 struct thread_info
*thread
= (struct thread_info
*) entry
;
1208 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1209 ptid_t ptid
= * (ptid_t
*) arg
;
1211 /* Check if we're only interested in events from a specific process
1213 if (!ptid_equal (minus_one_ptid
, ptid
)
1214 && ptid_get_pid (ptid
) != ptid_get_pid (thread
->entry
.id
))
1217 /* If we got a `vCont;t', but we haven't reported a stop yet, do
1218 report any status pending the LWP may have. */
1219 if (thread
->last_resume_kind
== resume_stop
1220 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1223 return lwp
->status_pending_p
;
1227 same_lwp (struct inferior_list_entry
*entry
, void *data
)
1229 ptid_t ptid
= *(ptid_t
*) data
;
1232 if (ptid_get_lwp (ptid
) != 0)
1233 lwp
= ptid_get_lwp (ptid
);
1235 lwp
= ptid_get_pid (ptid
);
1237 if (ptid_get_lwp (entry
->id
) == lwp
)
1244 find_lwp_pid (ptid_t ptid
)
1246 struct inferior_list_entry
*thread
1247 = find_inferior (&all_threads
, same_lwp
, &ptid
);
1252 return get_thread_lwp ((struct thread_info
*) thread
);
1255 static struct lwp_info
*
1256 linux_wait_for_lwp (ptid_t ptid
, int *wstatp
, int options
)
1259 int to_wait_for
= -1;
1260 struct lwp_info
*child
;
1261 struct thread_info
*thread
;
1264 debug_printf ("linux_wait_for_lwp: %s\n", target_pid_to_str (ptid
));
1266 if (ptid_equal (ptid
, minus_one_ptid
))
1267 to_wait_for
= -1; /* any child */
1269 to_wait_for
= ptid_get_lwp (ptid
); /* this lwp only */
1275 ret
= my_waitpid (to_wait_for
, wstatp
, options
);
1276 if (ret
== 0 || (ret
== -1 && errno
== ECHILD
&& (options
& WNOHANG
)))
1279 perror_with_name ("waitpid");
1282 && (!WIFSTOPPED (*wstatp
)
1283 || (WSTOPSIG (*wstatp
) != 32
1284 && WSTOPSIG (*wstatp
) != 33)))
1285 debug_printf ("Got an event from %d (%x)\n", ret
, *wstatp
);
1287 child
= find_lwp_pid (pid_to_ptid (ret
));
1289 thread
= get_lwp_thread (child
);
1293 /* If we didn't find a process, one of two things presumably happened:
1294 - A process we started and then detached from has exited. Ignore it.
1295 - A process we are controlling has forked and the new child's stop
1296 was reported to us by the kernel. Save its PID. */
1297 if (child
== NULL
&& WIFSTOPPED (*wstatp
))
1299 add_to_pid_list (&stopped_pids
, ret
, *wstatp
);
1302 else if (child
== NULL
)
1307 child
->last_status
= *wstatp
;
1309 if (WIFSTOPPED (*wstatp
))
1311 struct process_info
*proc
;
1313 /* Architecture-specific setup after inferior is running. This
1314 needs to happen after we have attached to the inferior and it
1315 is stopped for the first time, but before we access any
1316 inferior registers. */
1317 proc
= find_process_pid (pid_of (thread
));
1318 if (proc
->private->new_inferior
)
1320 struct thread_info
*saved_inferior
;
1322 saved_inferior
= current_inferior
;
1323 current_inferior
= thread
;
1325 the_low_target
.arch_setup ();
1327 current_inferior
= saved_inferior
;
1329 proc
->private->new_inferior
= 0;
1333 /* Fetch the possibly triggered data watchpoint info and store it in
1336 On some archs, like x86, that use debug registers to set
1337 watchpoints, it's possible that the way to know which watched
1338 address trapped, is to check the register that is used to select
1339 which address to watch. Problem is, between setting the
1340 watchpoint and reading back which data address trapped, the user
1341 may change the set of watchpoints, and, as a consequence, GDB
1342 changes the debug registers in the inferior. To avoid reading
1343 back a stale stopped-data-address when that happens, we cache in
1344 LP the fact that a watchpoint trapped, and the corresponding data
1345 address, as soon as we see CHILD stop with a SIGTRAP. If GDB
1346 changes the debug registers meanwhile, we have the cached data we
1349 if (WIFSTOPPED (*wstatp
) && WSTOPSIG (*wstatp
) == SIGTRAP
)
1351 if (the_low_target
.stopped_by_watchpoint
== NULL
)
1353 child
->stopped_by_watchpoint
= 0;
1357 struct thread_info
*saved_inferior
;
1359 saved_inferior
= current_inferior
;
1360 current_inferior
= get_lwp_thread (child
);
1362 child
->stopped_by_watchpoint
1363 = the_low_target
.stopped_by_watchpoint ();
1365 if (child
->stopped_by_watchpoint
)
1367 if (the_low_target
.stopped_data_address
!= NULL
)
1368 child
->stopped_data_address
1369 = the_low_target
.stopped_data_address ();
1371 child
->stopped_data_address
= 0;
1374 current_inferior
= saved_inferior
;
1378 /* Store the STOP_PC, with adjustment applied. This depends on the
1379 architecture being defined already (so that CHILD has a valid
1380 regcache), and on LAST_STATUS being set (to check for SIGTRAP or
1382 if (WIFSTOPPED (*wstatp
))
1383 child
->stop_pc
= get_stop_pc (child
);
1386 && WIFSTOPPED (*wstatp
)
1387 && the_low_target
.get_pc
!= NULL
)
1389 struct thread_info
*saved_inferior
= current_inferior
;
1390 struct regcache
*regcache
;
1393 current_inferior
= get_lwp_thread (child
);
1394 regcache
= get_thread_regcache (current_inferior
, 1);
1395 pc
= (*the_low_target
.get_pc
) (regcache
);
1396 debug_printf ("linux_wait_for_lwp: pc is 0x%lx\n", (long) pc
);
1397 current_inferior
= saved_inferior
;
1403 /* This function should only be called if the LWP got a SIGTRAP.
1405 Handle any tracepoint steps or hits. Return true if a tracepoint
1406 event was handled, 0 otherwise. */
1409 handle_tracepoints (struct lwp_info
*lwp
)
1411 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1412 int tpoint_related_event
= 0;
1414 /* If this tracepoint hit causes a tracing stop, we'll immediately
1415 uninsert tracepoints. To do this, we temporarily pause all
1416 threads, unpatch away, and then unpause threads. We need to make
1417 sure the unpausing doesn't resume LWP too. */
1420 /* And we need to be sure that any all-threads-stopping doesn't try
1421 to move threads out of the jump pads, as it could deadlock the
1422 inferior (LWP could be in the jump pad, maybe even holding the
1425 /* Do any necessary step collect actions. */
1426 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1428 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1430 /* See if we just hit a tracepoint and do its main collect
1432 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1436 gdb_assert (lwp
->suspended
== 0);
1437 gdb_assert (!stabilizing_threads
|| lwp
->collecting_fast_tracepoint
);
1439 if (tpoint_related_event
)
1442 debug_printf ("got a tracepoint event\n");
1449 /* Convenience wrapper. Returns true if LWP is presently collecting a
1453 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
1454 struct fast_tpoint_collect_status
*status
)
1456 CORE_ADDR thread_area
;
1457 struct thread_info
*thread
= get_lwp_thread (lwp
);
1459 if (the_low_target
.get_thread_area
== NULL
)
1462 /* Get the thread area address. This is used to recognize which
1463 thread is which when tracing with the in-process agent library.
1464 We don't read anything from the address, and treat it as opaque;
1465 it's the address itself that we assume is unique per-thread. */
1466 if ((*the_low_target
.get_thread_area
) (lwpid_of (thread
), &thread_area
) == -1)
1469 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
1472 /* The reason we resume in the caller, is because we want to be able
1473 to pass lwp->status_pending as WSTAT, and we need to clear
1474 status_pending_p before resuming, otherwise, linux_resume_one_lwp
1475 refuses to resume. */
1478 maybe_move_out_of_jump_pad (struct lwp_info
*lwp
, int *wstat
)
1480 struct thread_info
*saved_inferior
;
1482 saved_inferior
= current_inferior
;
1483 current_inferior
= get_lwp_thread (lwp
);
1486 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
1487 && supports_fast_tracepoints ()
1488 && agent_loaded_p ())
1490 struct fast_tpoint_collect_status status
;
1494 debug_printf ("Checking whether LWP %ld needs to move out of the "
1496 lwpid_of (current_inferior
));
1498 r
= linux_fast_tracepoint_collecting (lwp
, &status
);
1501 || (WSTOPSIG (*wstat
) != SIGILL
1502 && WSTOPSIG (*wstat
) != SIGFPE
1503 && WSTOPSIG (*wstat
) != SIGSEGV
1504 && WSTOPSIG (*wstat
) != SIGBUS
))
1506 lwp
->collecting_fast_tracepoint
= r
;
1510 if (r
== 1 && lwp
->exit_jump_pad_bkpt
== NULL
)
1512 /* Haven't executed the original instruction yet.
1513 Set breakpoint there, and wait till it's hit,
1514 then single-step until exiting the jump pad. */
1515 lwp
->exit_jump_pad_bkpt
1516 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
1520 debug_printf ("Checking whether LWP %ld needs to move out of "
1521 "the jump pad...it does\n",
1522 lwpid_of (current_inferior
));
1523 current_inferior
= saved_inferior
;
1530 /* If we get a synchronous signal while collecting, *and*
1531 while executing the (relocated) original instruction,
1532 reset the PC to point at the tpoint address, before
1533 reporting to GDB. Otherwise, it's an IPA lib bug: just
1534 report the signal to GDB, and pray for the best. */
1536 lwp
->collecting_fast_tracepoint
= 0;
1539 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
1540 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
1543 struct regcache
*regcache
;
1545 /* The si_addr on a few signals references the address
1546 of the faulting instruction. Adjust that as
1548 if ((WSTOPSIG (*wstat
) == SIGILL
1549 || WSTOPSIG (*wstat
) == SIGFPE
1550 || WSTOPSIG (*wstat
) == SIGBUS
1551 || WSTOPSIG (*wstat
) == SIGSEGV
)
1552 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_inferior
),
1553 (PTRACE_TYPE_ARG3
) 0, &info
) == 0
1554 /* Final check just to make sure we don't clobber
1555 the siginfo of non-kernel-sent signals. */
1556 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
1558 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
1559 ptrace (PTRACE_SETSIGINFO
, lwpid_of (current_inferior
),
1560 (PTRACE_TYPE_ARG3
) 0, &info
);
1563 regcache
= get_thread_regcache (current_inferior
, 1);
1564 (*the_low_target
.set_pc
) (regcache
, status
.tpoint_addr
);
1565 lwp
->stop_pc
= status
.tpoint_addr
;
1567 /* Cancel any fast tracepoint lock this thread was
1569 force_unlock_trace_buffer ();
1572 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
1575 debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
1576 "stopping all threads momentarily.\n");
1578 stop_all_lwps (1, lwp
);
1579 cancel_breakpoints ();
1581 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
1582 lwp
->exit_jump_pad_bkpt
= NULL
;
1584 unstop_all_lwps (1, lwp
);
1586 gdb_assert (lwp
->suspended
>= 0);
1592 debug_printf ("Checking whether LWP %ld needs to move out of the "
1594 lwpid_of (current_inferior
));
1596 current_inferior
= saved_inferior
;
1600 /* Enqueue one signal in the "signals to report later when out of the
1604 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1606 struct pending_signals
*p_sig
;
1607 struct thread_info
*thread
= get_lwp_thread (lwp
);
1610 debug_printf ("Deferring signal %d for LWP %ld.\n",
1611 WSTOPSIG (*wstat
), lwpid_of (thread
));
1615 struct pending_signals
*sig
;
1617 for (sig
= lwp
->pending_signals_to_report
;
1620 debug_printf (" Already queued %d\n",
1623 debug_printf (" (no more currently queued signals)\n");
1626 /* Don't enqueue non-RT signals if they are already in the deferred
1627 queue. (SIGSTOP being the easiest signal to see ending up here
1629 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
1631 struct pending_signals
*sig
;
1633 for (sig
= lwp
->pending_signals_to_report
;
1637 if (sig
->signal
== WSTOPSIG (*wstat
))
1640 debug_printf ("Not requeuing already queued non-RT signal %d"
1649 p_sig
= xmalloc (sizeof (*p_sig
));
1650 p_sig
->prev
= lwp
->pending_signals_to_report
;
1651 p_sig
->signal
= WSTOPSIG (*wstat
);
1652 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1653 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
1656 lwp
->pending_signals_to_report
= p_sig
;
1659 /* Dequeue one signal from the "signals to report later when out of
1660 the jump pad" list. */
1663 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1665 struct thread_info
*thread
= get_lwp_thread (lwp
);
1667 if (lwp
->pending_signals_to_report
!= NULL
)
1669 struct pending_signals
**p_sig
;
1671 p_sig
= &lwp
->pending_signals_to_report
;
1672 while ((*p_sig
)->prev
!= NULL
)
1673 p_sig
= &(*p_sig
)->prev
;
1675 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
1676 if ((*p_sig
)->info
.si_signo
!= 0)
1677 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
1683 debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
1684 WSTOPSIG (*wstat
), lwpid_of (thread
));
1688 struct pending_signals
*sig
;
1690 for (sig
= lwp
->pending_signals_to_report
;
1693 debug_printf (" Still queued %d\n",
1696 debug_printf (" (no more queued signals)\n");
1705 /* Arrange for a breakpoint to be hit again later. We don't keep the
1706 SIGTRAP status and don't forward the SIGTRAP signal to the LWP. We
1707 will handle the current event, eventually we will resume this LWP,
1708 and this breakpoint will trap again. */
1711 cancel_breakpoint (struct lwp_info
*lwp
)
1713 struct thread_info
*saved_inferior
;
1715 /* There's nothing to do if we don't support breakpoints. */
1716 if (!supports_breakpoints ())
1719 /* breakpoint_at reads from current inferior. */
1720 saved_inferior
= current_inferior
;
1721 current_inferior
= get_lwp_thread (lwp
);
1723 if ((*the_low_target
.breakpoint_at
) (lwp
->stop_pc
))
1726 debug_printf ("CB: Push back breakpoint for %s\n",
1727 target_pid_to_str (ptid_of (current_inferior
)));
1729 /* Back up the PC if necessary. */
1730 if (the_low_target
.decr_pc_after_break
)
1732 struct regcache
*regcache
1733 = get_thread_regcache (current_inferior
, 1);
1734 (*the_low_target
.set_pc
) (regcache
, lwp
->stop_pc
);
1737 current_inferior
= saved_inferior
;
1743 debug_printf ("CB: No breakpoint found at %s for [%s]\n",
1744 paddress (lwp
->stop_pc
),
1745 target_pid_to_str (ptid_of (current_inferior
)));
1748 current_inferior
= saved_inferior
;
1752 /* When the event-loop is doing a step-over, this points at the thread
1754 ptid_t step_over_bkpt
;
1756 /* Wait for an event from child PID. If PID is -1, wait for any
1757 child. Store the stop status through the status pointer WSTAT.
1758 OPTIONS is passed to the waitpid call. Return 0 if no child stop
1759 event was found and OPTIONS contains WNOHANG. Return the PID of
1760 the stopped child and update current_inferior otherwise. */
1763 linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
)
1765 struct thread_info
*event_thread
;
1766 struct lwp_info
*event_child
, *requested_child
;
1769 /* N.B. event_thread points to the thread_info struct that contains
1770 event_child. Keep them in sync. */
1771 event_thread
= NULL
;
1773 requested_child
= NULL
;
1775 /* Check for a lwp with a pending status. */
1777 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
1779 event_thread
= (struct thread_info
*)
1780 find_inferior (&all_threads
, status_pending_p_callback
, &ptid
);
1781 if (event_thread
!= NULL
)
1782 event_child
= get_thread_lwp (event_thread
);
1783 if (debug_threads
&& event_thread
)
1784 debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread
));
1788 requested_child
= find_lwp_pid (ptid
);
1790 if (stopping_threads
== NOT_STOPPING_THREADS
1791 && requested_child
->status_pending_p
1792 && requested_child
->collecting_fast_tracepoint
)
1794 enqueue_one_deferred_signal (requested_child
,
1795 &requested_child
->status_pending
);
1796 requested_child
->status_pending_p
= 0;
1797 requested_child
->status_pending
= 0;
1798 linux_resume_one_lwp (requested_child
, 0, 0, NULL
);
1801 if (requested_child
->suspended
1802 && requested_child
->status_pending_p
)
1803 fatal ("requesting an event out of a suspended child?");
1805 if (requested_child
->status_pending_p
)
1807 event_child
= requested_child
;
1808 event_thread
= get_lwp_thread (event_child
);
1812 if (event_child
!= NULL
)
1815 debug_printf ("Got an event from pending child %ld (%04x)\n",
1816 lwpid_of (event_thread
), event_child
->status_pending
);
1817 *wstat
= event_child
->status_pending
;
1818 event_child
->status_pending_p
= 0;
1819 event_child
->status_pending
= 0;
1820 current_inferior
= event_thread
;
1821 return lwpid_of (event_thread
);
1824 if (ptid_is_pid (ptid
))
1826 /* A request to wait for a specific tgid. This is not possible
1827 with waitpid, so instead, we wait for any child, and leave
1828 children we're not interested in right now with a pending
1829 status to report later. */
1830 wait_ptid
= minus_one_ptid
;
1835 /* We only enter this loop if no process has a pending wait status. Thus
1836 any action taken in response to a wait status inside this loop is
1837 responding as soon as we detect the status, not after any pending
1841 event_child
= linux_wait_for_lwp (wait_ptid
, wstat
, options
);
1843 if ((options
& WNOHANG
) && event_child
== NULL
)
1846 debug_printf ("WNOHANG set, no event found\n");
1850 if (event_child
== NULL
)
1851 error ("event from unknown child");
1853 event_thread
= get_lwp_thread (event_child
);
1854 if (ptid_is_pid (ptid
)
1855 && ptid_get_pid (ptid
) != ptid_get_pid (ptid_of (event_thread
)))
1857 if (! WIFSTOPPED (*wstat
))
1858 mark_lwp_dead (event_child
, *wstat
);
1861 event_child
->status_pending_p
= 1;
1862 event_child
->status_pending
= *wstat
;
1867 current_inferior
= event_thread
;
1869 /* Check for thread exit. */
1870 if (! WIFSTOPPED (*wstat
))
1873 debug_printf ("LWP %ld exiting\n", lwpid_of (event_thread
));
1875 /* If the last thread is exiting, just return. */
1876 if (last_thread_of_process_p (current_inferior
))
1879 debug_printf ("LWP %ld is last lwp of process\n",
1880 lwpid_of (event_thread
));
1881 return lwpid_of (event_thread
);
1886 current_inferior
= get_first_thread ();
1888 debug_printf ("Current inferior is now %ld\n",
1889 lwpid_of (current_inferior
));
1893 current_inferior
= NULL
;
1895 debug_printf ("Current inferior is now <NULL>\n");
1898 /* If we were waiting for this particular child to do something...
1899 well, it did something. */
1900 if (requested_child
!= NULL
)
1902 int lwpid
= lwpid_of (event_thread
);
1904 /* Cancel the step-over operation --- the thread that
1905 started it is gone. */
1906 if (finish_step_over (event_child
))
1907 unstop_all_lwps (1, event_child
);
1908 delete_lwp (event_child
);
1912 delete_lwp (event_child
);
1914 /* Wait for a more interesting event. */
1918 if (event_child
->must_set_ptrace_flags
)
1920 linux_enable_event_reporting (lwpid_of (event_thread
));
1921 event_child
->must_set_ptrace_flags
= 0;
1924 if (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) == SIGTRAP
1925 && *wstat
>> 16 != 0)
1927 handle_extended_wait (event_child
, *wstat
);
1931 if (WIFSTOPPED (*wstat
)
1932 && WSTOPSIG (*wstat
) == SIGSTOP
1933 && event_child
->stop_expected
)
1938 debug_printf ("Expected stop.\n");
1939 event_child
->stop_expected
= 0;
1941 should_stop
= (current_inferior
->last_resume_kind
== resume_stop
1942 || stopping_threads
!= NOT_STOPPING_THREADS
);
1946 linux_resume_one_lwp (event_child
,
1947 event_child
->stepping
, 0, NULL
);
1952 return lwpid_of (event_thread
);
1959 /* Count the LWP's that have had events. */
1962 count_events_callback (struct inferior_list_entry
*entry
, void *data
)
1964 struct thread_info
*thread
= (struct thread_info
*) entry
;
1965 struct lwp_info
*lp
= get_thread_lwp (thread
);
1968 gdb_assert (count
!= NULL
);
1970 /* Count only resumed LWPs that have a SIGTRAP event pending that
1971 should be reported to GDB. */
1972 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
1973 && thread
->last_resume_kind
!= resume_stop
1974 && lp
->status_pending_p
1975 && WIFSTOPPED (lp
->status_pending
)
1976 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
1977 && !breakpoint_inserted_here (lp
->stop_pc
))
1983 /* Select the LWP (if any) that is currently being single-stepped. */
1986 select_singlestep_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
1988 struct thread_info
*thread
= (struct thread_info
*) entry
;
1989 struct lwp_info
*lp
= get_thread_lwp (thread
);
1991 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
1992 && thread
->last_resume_kind
== resume_step
1993 && lp
->status_pending_p
)
1999 /* Select the Nth LWP that has had a SIGTRAP event that should be
2003 select_event_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
2005 struct thread_info
*thread
= (struct thread_info
*) entry
;
2006 struct lwp_info
*lp
= get_thread_lwp (thread
);
2007 int *selector
= data
;
2009 gdb_assert (selector
!= NULL
);
2011 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2012 if (thread
->last_resume_kind
!= resume_stop
2013 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2014 && lp
->status_pending_p
2015 && WIFSTOPPED (lp
->status_pending
)
2016 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
2017 && !breakpoint_inserted_here (lp
->stop_pc
))
2018 if ((*selector
)-- == 0)
2025 cancel_breakpoints_callback (struct inferior_list_entry
*entry
, void *data
)
2027 struct thread_info
*thread
= (struct thread_info
*) entry
;
2028 struct lwp_info
*lp
= get_thread_lwp (thread
);
2029 struct lwp_info
*event_lp
= data
;
2031 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2035 /* If a LWP other than the LWP that we're reporting an event for has
2036 hit a GDB breakpoint (as opposed to some random trap signal),
2037 then just arrange for it to hit it again later. We don't keep
2038 the SIGTRAP status and don't forward the SIGTRAP signal to the
2039 LWP. We will handle the current event, eventually we will resume
2040 all LWPs, and this one will get its breakpoint trap again.
2042 If we do not do this, then we run the risk that the user will
2043 delete or disable the breakpoint, but the LWP will have already
2046 if (thread
->last_resume_kind
!= resume_stop
2047 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2048 && lp
->status_pending_p
2049 && WIFSTOPPED (lp
->status_pending
)
2050 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
2052 && !lp
->stopped_by_watchpoint
2053 && cancel_breakpoint (lp
))
2054 /* Throw away the SIGTRAP. */
2055 lp
->status_pending_p
= 0;
2061 linux_cancel_breakpoints (void)
2063 find_inferior (&all_threads
, cancel_breakpoints_callback
, NULL
);
2066 /* Select one LWP out of those that have events pending. */
2069 select_event_lwp (struct lwp_info
**orig_lp
)
2072 int random_selector
;
2073 struct thread_info
*event_thread
;
2075 /* Give preference to any LWP that is being single-stepped. */
2077 = (struct thread_info
*) find_inferior (&all_threads
,
2078 select_singlestep_lwp_callback
,
2080 if (event_thread
!= NULL
)
2083 debug_printf ("SEL: Select single-step %s\n",
2084 target_pid_to_str (ptid_of (event_thread
)));
2088 /* No single-stepping LWP. Select one at random, out of those
2089 which have had SIGTRAP events. */
2091 /* First see how many SIGTRAP events we have. */
2092 find_inferior (&all_threads
, count_events_callback
, &num_events
);
2094 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2095 random_selector
= (int)
2096 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2098 if (debug_threads
&& num_events
> 1)
2099 debug_printf ("SEL: Found %d SIGTRAP events, selecting #%d\n",
2100 num_events
, random_selector
);
2103 = (struct thread_info
*) find_inferior (&all_threads
,
2104 select_event_lwp_callback
,
2108 if (event_thread
!= NULL
)
2110 struct lwp_info
*event_lp
= get_thread_lwp (event_thread
);
2112 /* Switch the event LWP. */
2113 *orig_lp
= event_lp
;
2117 /* Decrement the suspend count of an LWP. */
2120 unsuspend_one_lwp (struct inferior_list_entry
*entry
, void *except
)
2122 struct thread_info
*thread
= (struct thread_info
*) entry
;
2123 struct lwp_info
*lwp
= get_thread_lwp (thread
);
2125 /* Ignore EXCEPT. */
2131 gdb_assert (lwp
->suspended
>= 0);
2135 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2139 unsuspend_all_lwps (struct lwp_info
*except
)
2141 find_inferior (&all_threads
, unsuspend_one_lwp
, except
);
2144 static void move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
);
2145 static int stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
,
2147 static int lwp_running (struct inferior_list_entry
*entry
, void *data
);
2148 static ptid_t
linux_wait_1 (ptid_t ptid
,
2149 struct target_waitstatus
*ourstatus
,
2150 int target_options
);
2152 /* Stabilize threads (move out of jump pads).
2154 If a thread is midway collecting a fast tracepoint, we need to
2155 finish the collection and move it out of the jump pad before
2156 reporting the signal.
2158 This avoids recursion while collecting (when a signal arrives
2159 midway, and the signal handler itself collects), which would trash
2160 the trace buffer. In case the user set a breakpoint in a signal
2161 handler, this avoids the backtrace showing the jump pad, etc..
2162 Most importantly, there are certain things we can't do safely if
2163 threads are stopped in a jump pad (or in its callee's). For
2166 - starting a new trace run. A thread still collecting the
2167 previous run, could trash the trace buffer when resumed. The trace
2168 buffer control structures would have been reset but the thread had
2169 no way to tell. The thread could even midway memcpy'ing to the
2170 buffer, which would mean that when resumed, it would clobber the
2171 trace buffer that had been set for a new run.
2173 - we can't rewrite/reuse the jump pads for new tracepoints
2174 safely. Say you do tstart while a thread is stopped midway while
2175 collecting. When the thread is later resumed, it finishes the
2176 collection, and returns to the jump pad, to execute the original
2177 instruction that was under the tracepoint jump at the time the
2178 older run had been started. If the jump pad had been rewritten
2179 since for something else in the new run, the thread would now
2180 execute the wrong / random instructions. */
2183 linux_stabilize_threads (void)
2185 struct thread_info
*save_inferior
;
2186 struct thread_info
*thread_stuck
;
2189 = (struct thread_info
*) find_inferior (&all_threads
,
2190 stuck_in_jump_pad_callback
,
2192 if (thread_stuck
!= NULL
)
2195 debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
2196 lwpid_of (thread_stuck
));
2200 save_inferior
= current_inferior
;
2202 stabilizing_threads
= 1;
2205 for_each_inferior (&all_threads
, move_out_of_jump_pad_callback
);
2207 /* Loop until all are stopped out of the jump pads. */
2208 while (find_inferior (&all_threads
, lwp_running
, NULL
) != NULL
)
2210 struct target_waitstatus ourstatus
;
2211 struct lwp_info
*lwp
;
2214 /* Note that we go through the full wait even loop. While
2215 moving threads out of jump pad, we need to be able to step
2216 over internal breakpoints and such. */
2217 linux_wait_1 (minus_one_ptid
, &ourstatus
, 0);
2219 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
2221 lwp
= get_thread_lwp (current_inferior
);
2226 if (ourstatus
.value
.sig
!= GDB_SIGNAL_0
2227 || current_inferior
->last_resume_kind
== resume_stop
)
2229 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.value
.sig
));
2230 enqueue_one_deferred_signal (lwp
, &wstat
);
2235 find_inferior (&all_threads
, unsuspend_one_lwp
, NULL
);
2237 stabilizing_threads
= 0;
2239 current_inferior
= save_inferior
;
2244 = (struct thread_info
*) find_inferior (&all_threads
,
2245 stuck_in_jump_pad_callback
,
2247 if (thread_stuck
!= NULL
)
2248 debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
2249 lwpid_of (thread_stuck
));
2253 /* Wait for process, returns status. */
2256 linux_wait_1 (ptid_t ptid
,
2257 struct target_waitstatus
*ourstatus
, int target_options
)
2260 struct lwp_info
*event_child
;
2263 int step_over_finished
;
2264 int bp_explains_trap
;
2265 int maybe_internal_trap
;
2273 debug_printf ("linux_wait_1: [%s]\n", target_pid_to_str (ptid
));
2276 /* Translate generic target options into linux options. */
2278 if (target_options
& TARGET_WNOHANG
)
2282 bp_explains_trap
= 0;
2285 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2287 /* If we were only supposed to resume one thread, only wait for
2288 that thread - if it's still alive. If it died, however - which
2289 can happen if we're coming from the thread death case below -
2290 then we need to make sure we restart the other threads. We could
2291 pick a thread at random or restart all; restarting all is less
2294 && !ptid_equal (cont_thread
, null_ptid
)
2295 && !ptid_equal (cont_thread
, minus_one_ptid
))
2297 struct thread_info
*thread
;
2299 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
2302 /* No stepping, no signal - unless one is pending already, of course. */
2305 struct thread_resume resume_info
;
2306 resume_info
.thread
= minus_one_ptid
;
2307 resume_info
.kind
= resume_continue
;
2308 resume_info
.sig
= 0;
2309 linux_resume (&resume_info
, 1);
2315 if (ptid_equal (step_over_bkpt
, null_ptid
))
2316 pid
= linux_wait_for_event (ptid
, &w
, options
);
2320 debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
2321 target_pid_to_str (step_over_bkpt
));
2322 pid
= linux_wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
2325 if (pid
== 0) /* only if TARGET_WNOHANG */
2329 debug_printf ("linux_wait_1 ret = null_ptid\n");
2335 event_child
= get_thread_lwp (current_inferior
);
2337 /* If we are waiting for a particular child, and it exited,
2338 linux_wait_for_event will return its exit status. Similarly if
2339 the last child exited. If this is not the last child, however,
2340 do not report it as exited until there is a 'thread exited' response
2341 available in the remote protocol. Instead, just wait for another event.
2342 This should be safe, because if the thread crashed we will already
2343 have reported the termination signal to GDB; that should stop any
2344 in-progress stepping operations, etc.
2346 Report the exit status of the last thread to exit. This matches
2347 LinuxThreads' behavior. */
2349 if (last_thread_of_process_p (current_inferior
))
2351 if (WIFEXITED (w
) || WIFSIGNALED (w
))
2355 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
2356 ourstatus
->value
.integer
= WEXITSTATUS (w
);
2360 debug_printf ("linux_wait_1 ret = %s, exited with "
2362 target_pid_to_str (ptid_of (current_inferior
)),
2369 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
2370 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (w
));
2374 debug_printf ("linux_wait_1 ret = %s, terminated with "
2376 target_pid_to_str (ptid_of (current_inferior
)),
2382 return ptid_of (current_inferior
);
2387 if (!WIFSTOPPED (w
))
2391 /* If this event was not handled before, and is not a SIGTRAP, we
2392 report it. SIGILL and SIGSEGV are also treated as traps in case
2393 a breakpoint is inserted at the current PC. If this target does
2394 not support internal breakpoints at all, we also report the
2395 SIGTRAP without further processing; it's of no concern to us. */
2397 = (supports_breakpoints ()
2398 && (WSTOPSIG (w
) == SIGTRAP
2399 || ((WSTOPSIG (w
) == SIGILL
2400 || WSTOPSIG (w
) == SIGSEGV
)
2401 && (*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))));
2403 if (maybe_internal_trap
)
2405 /* Handle anything that requires bookkeeping before deciding to
2406 report the event or continue waiting. */
2408 /* First check if we can explain the SIGTRAP with an internal
2409 breakpoint, or if we should possibly report the event to GDB.
2410 Do this before anything that may remove or insert a
2412 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
2414 /* We have a SIGTRAP, possibly a step-over dance has just
2415 finished. If so, tweak the state machine accordingly,
2416 reinsert breakpoints and delete any reinsert (software
2417 single-step) breakpoints. */
2418 step_over_finished
= finish_step_over (event_child
);
2420 /* Now invoke the callbacks of any internal breakpoints there. */
2421 check_breakpoints (event_child
->stop_pc
);
2423 /* Handle tracepoint data collecting. This may overflow the
2424 trace buffer, and cause a tracing stop, removing
2426 trace_event
= handle_tracepoints (event_child
);
2428 if (bp_explains_trap
)
2430 /* If we stepped or ran into an internal breakpoint, we've
2431 already handled it. So next time we resume (from this
2432 PC), we should step over it. */
2434 debug_printf ("Hit a gdbserver breakpoint.\n");
2436 if (breakpoint_here (event_child
->stop_pc
))
2437 event_child
->need_step_over
= 1;
2442 /* We have some other signal, possibly a step-over dance was in
2443 progress, and it should be cancelled too. */
2444 step_over_finished
= finish_step_over (event_child
);
2447 /* We have all the data we need. Either report the event to GDB, or
2448 resume threads and keep waiting for more. */
2450 /* If we're collecting a fast tracepoint, finish the collection and
2451 move out of the jump pad before delivering a signal. See
2452 linux_stabilize_threads. */
2455 && WSTOPSIG (w
) != SIGTRAP
2456 && supports_fast_tracepoints ()
2457 && agent_loaded_p ())
2460 debug_printf ("Got signal %d for LWP %ld. Check if we need "
2461 "to defer or adjust it.\n",
2462 WSTOPSIG (w
), lwpid_of (current_inferior
));
2464 /* Allow debugging the jump pad itself. */
2465 if (current_inferior
->last_resume_kind
!= resume_step
2466 && maybe_move_out_of_jump_pad (event_child
, &w
))
2468 enqueue_one_deferred_signal (event_child
, &w
);
2471 debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
2472 WSTOPSIG (w
), lwpid_of (current_inferior
));
2474 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
2479 if (event_child
->collecting_fast_tracepoint
)
2482 debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
2483 "Check if we're already there.\n",
2484 lwpid_of (current_inferior
),
2485 event_child
->collecting_fast_tracepoint
);
2489 event_child
->collecting_fast_tracepoint
2490 = linux_fast_tracepoint_collecting (event_child
, NULL
);
2492 if (event_child
->collecting_fast_tracepoint
!= 1)
2494 /* No longer need this breakpoint. */
2495 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
2498 debug_printf ("No longer need exit-jump-pad bkpt; removing it."
2499 "stopping all threads momentarily.\n");
2501 /* Other running threads could hit this breakpoint.
2502 We don't handle moribund locations like GDB does,
2503 instead we always pause all threads when removing
2504 breakpoints, so that any step-over or
2505 decr_pc_after_break adjustment is always taken
2506 care of while the breakpoint is still
2508 stop_all_lwps (1, event_child
);
2509 cancel_breakpoints ();
2511 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
2512 event_child
->exit_jump_pad_bkpt
= NULL
;
2514 unstop_all_lwps (1, event_child
);
2516 gdb_assert (event_child
->suspended
>= 0);
2520 if (event_child
->collecting_fast_tracepoint
== 0)
2523 debug_printf ("fast tracepoint finished "
2524 "collecting successfully.\n");
2526 /* We may have a deferred signal to report. */
2527 if (dequeue_one_deferred_signal (event_child
, &w
))
2530 debug_printf ("dequeued one signal.\n");
2535 debug_printf ("no deferred signals.\n");
2537 if (stabilizing_threads
)
2539 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
2540 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
2544 debug_printf ("linux_wait_1 ret = %s, stopped "
2545 "while stabilizing threads\n",
2546 target_pid_to_str (ptid_of (current_inferior
)));
2550 return ptid_of (current_inferior
);
2556 /* Check whether GDB would be interested in this event. */
2558 /* If GDB is not interested in this signal, don't stop other
2559 threads, and don't report it to GDB. Just resume the inferior
2560 right away. We do this for threading-related signals as well as
2561 any that GDB specifically requested we ignore. But never ignore
2562 SIGSTOP if we sent it ourselves, and do not ignore signals when
2563 stepping - they may require special handling to skip the signal
2565 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
2568 && current_inferior
->last_resume_kind
!= resume_step
2570 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
2571 (current_process ()->private->thread_db
!= NULL
2572 && (WSTOPSIG (w
) == __SIGRTMIN
2573 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
2576 (pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
2577 && !(WSTOPSIG (w
) == SIGSTOP
2578 && current_inferior
->last_resume_kind
== resume_stop
))))
2580 siginfo_t info
, *info_p
;
2583 debug_printf ("Ignored signal %d for LWP %ld.\n",
2584 WSTOPSIG (w
), lwpid_of (current_inferior
));
2586 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_inferior
),
2587 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
2591 linux_resume_one_lwp (event_child
, event_child
->stepping
,
2592 WSTOPSIG (w
), info_p
);
2596 /* Note that all addresses are always "out of the step range" when
2597 there's no range to begin with. */
2598 in_step_range
= lwp_in_step_range (event_child
);
2600 /* If GDB wanted this thread to single step, and the thread is out
2601 of the step range, we always want to report the SIGTRAP, and let
2602 GDB handle it. Watchpoints should always be reported. So should
2603 signals we can't explain. A SIGTRAP we can't explain could be a
2604 GDB breakpoint --- we may or not support Z0 breakpoints. If we
2605 do, we're be able to handle GDB breakpoints on top of internal
2606 breakpoints, by handling the internal breakpoint and still
2607 reporting the event to GDB. If we don't, we're out of luck, GDB
2608 won't see the breakpoint hit. */
2609 report_to_gdb
= (!maybe_internal_trap
2610 || (current_inferior
->last_resume_kind
== resume_step
2612 || event_child
->stopped_by_watchpoint
2613 || (!step_over_finished
&& !in_step_range
2614 && !bp_explains_trap
&& !trace_event
)
2615 || (gdb_breakpoint_here (event_child
->stop_pc
)
2616 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
2617 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
)));
2619 run_breakpoint_commands (event_child
->stop_pc
);
2621 /* We found no reason GDB would want us to stop. We either hit one
2622 of our own breakpoints, or finished an internal step GDB
2623 shouldn't know about. */
2628 if (bp_explains_trap
)
2629 debug_printf ("Hit a gdbserver breakpoint.\n");
2630 if (step_over_finished
)
2631 debug_printf ("Step-over finished.\n");
2633 debug_printf ("Tracepoint event.\n");
2634 if (lwp_in_step_range (event_child
))
2635 debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
2636 paddress (event_child
->stop_pc
),
2637 paddress (event_child
->step_range_start
),
2638 paddress (event_child
->step_range_end
));
2641 /* We're not reporting this breakpoint to GDB, so apply the
2642 decr_pc_after_break adjustment to the inferior's regcache
2645 if (the_low_target
.set_pc
!= NULL
)
2647 struct regcache
*regcache
2648 = get_thread_regcache (current_inferior
, 1);
2649 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
2652 /* We may have finished stepping over a breakpoint. If so,
2653 we've stopped and suspended all LWPs momentarily except the
2654 stepping one. This is where we resume them all again. We're
2655 going to keep waiting, so use proceed, which handles stepping
2656 over the next breakpoint. */
2658 debug_printf ("proceeding all threads.\n");
2660 if (step_over_finished
)
2661 unsuspend_all_lwps (event_child
);
2663 proceed_all_lwps ();
2669 if (current_inferior
->last_resume_kind
== resume_step
)
2671 if (event_child
->step_range_start
== event_child
->step_range_end
)
2672 debug_printf ("GDB wanted to single-step, reporting event.\n");
2673 else if (!lwp_in_step_range (event_child
))
2674 debug_printf ("Out of step range, reporting event.\n");
2676 if (event_child
->stopped_by_watchpoint
)
2677 debug_printf ("Stopped by watchpoint.\n");
2678 if (gdb_breakpoint_here (event_child
->stop_pc
))
2679 debug_printf ("Stopped by GDB breakpoint.\n");
2681 debug_printf ("Hit a non-gdbserver trap event.\n");
2684 /* Alright, we're going to report a stop. */
2686 if (!non_stop
&& !stabilizing_threads
)
2688 /* In all-stop, stop all threads. */
2689 stop_all_lwps (0, NULL
);
2691 /* If we're not waiting for a specific LWP, choose an event LWP
2692 from among those that have had events. Giving equal priority
2693 to all LWPs that have had events helps prevent
2695 if (ptid_equal (ptid
, minus_one_ptid
))
2697 event_child
->status_pending_p
= 1;
2698 event_child
->status_pending
= w
;
2700 select_event_lwp (&event_child
);
2702 /* current_inferior and event_child must stay in sync. */
2703 current_inferior
= get_lwp_thread (event_child
);
2705 event_child
->status_pending_p
= 0;
2706 w
= event_child
->status_pending
;
2709 /* Now that we've selected our final event LWP, cancel any
2710 breakpoints in other LWPs that have hit a GDB breakpoint.
2711 See the comment in cancel_breakpoints_callback to find out
2713 find_inferior (&all_threads
, cancel_breakpoints_callback
, event_child
);
2715 /* If we were going a step-over, all other threads but the stepping one
2716 had been paused in start_step_over, with their suspend counts
2717 incremented. We don't want to do a full unstop/unpause, because we're
2718 in all-stop mode (so we want threads stopped), but we still need to
2719 unsuspend the other threads, to decrement their `suspended' count
2721 if (step_over_finished
)
2722 unsuspend_all_lwps (event_child
);
2724 /* Stabilize threads (move out of jump pads). */
2725 stabilize_threads ();
2729 /* If we just finished a step-over, then all threads had been
2730 momentarily paused. In all-stop, that's fine, we want
2731 threads stopped by now anyway. In non-stop, we need to
2732 re-resume threads that GDB wanted to be running. */
2733 if (step_over_finished
)
2734 unstop_all_lwps (1, event_child
);
2737 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
2739 if (current_inferior
->last_resume_kind
== resume_stop
2740 && WSTOPSIG (w
) == SIGSTOP
)
2742 /* A thread that has been requested to stop by GDB with vCont;t,
2743 and it stopped cleanly, so report as SIG0. The use of
2744 SIGSTOP is an implementation detail. */
2745 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
2747 else if (current_inferior
->last_resume_kind
== resume_stop
2748 && WSTOPSIG (w
) != SIGSTOP
)
2750 /* A thread that has been requested to stop by GDB with vCont;t,
2751 but, it stopped for other reasons. */
2752 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
2756 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
2759 gdb_assert (ptid_equal (step_over_bkpt
, null_ptid
));
2763 debug_printf ("linux_wait_1 ret = %s, %d, %d\n",
2764 target_pid_to_str (ptid_of (current_inferior
)),
2765 ourstatus
->kind
, ourstatus
->value
.sig
);
2769 return ptid_of (current_inferior
);
2772 /* Get rid of any pending event in the pipe. */
2774 async_file_flush (void)
2780 ret
= read (linux_event_pipe
[0], &buf
, 1);
2781 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
2784 /* Put something in the pipe, so the event loop wakes up. */
2786 async_file_mark (void)
2790 async_file_flush ();
2793 ret
= write (linux_event_pipe
[1], "+", 1);
2794 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
2796 /* Ignore EAGAIN. If the pipe is full, the event loop will already
2797 be awakened anyway. */
2801 linux_wait (ptid_t ptid
,
2802 struct target_waitstatus
*ourstatus
, int target_options
)
2806 /* Flush the async file first. */
2807 if (target_is_async_p ())
2808 async_file_flush ();
2810 event_ptid
= linux_wait_1 (ptid
, ourstatus
, target_options
);
2812 /* If at least one stop was reported, there may be more. A single
2813 SIGCHLD can signal more than one child stop. */
2814 if (target_is_async_p ()
2815 && (target_options
& TARGET_WNOHANG
) != 0
2816 && !ptid_equal (event_ptid
, null_ptid
))
2822 /* Send a signal to an LWP. */
2825 kill_lwp (unsigned long lwpid
, int signo
)
2827 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2828 fails, then we are not using nptl threads and we should be using kill. */
2832 static int tkill_failed
;
2839 ret
= syscall (__NR_tkill
, lwpid
, signo
);
2840 if (errno
!= ENOSYS
)
2847 return kill (lwpid
, signo
);
2851 linux_stop_lwp (struct lwp_info
*lwp
)
2857 send_sigstop (struct lwp_info
*lwp
)
2861 pid
= lwpid_of (get_lwp_thread (lwp
));
2863 /* If we already have a pending stop signal for this process, don't
2865 if (lwp
->stop_expected
)
2868 debug_printf ("Have pending sigstop for lwp %d\n", pid
);
2874 debug_printf ("Sending sigstop to lwp %d\n", pid
);
2876 lwp
->stop_expected
= 1;
2877 kill_lwp (pid
, SIGSTOP
);
2881 send_sigstop_callback (struct inferior_list_entry
*entry
, void *except
)
2883 struct thread_info
*thread
= (struct thread_info
*) entry
;
2884 struct lwp_info
*lwp
= get_thread_lwp (thread
);
2886 /* Ignore EXCEPT. */
2897 /* Increment the suspend count of an LWP, and stop it, if not stopped
2900 suspend_and_send_sigstop_callback (struct inferior_list_entry
*entry
,
2903 struct thread_info
*thread
= (struct thread_info
*) entry
;
2904 struct lwp_info
*lwp
= get_thread_lwp (thread
);
2906 /* Ignore EXCEPT. */
2912 return send_sigstop_callback (entry
, except
);
2916 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
2918 /* It's dead, really. */
2921 /* Store the exit status for later. */
2922 lwp
->status_pending_p
= 1;
2923 lwp
->status_pending
= wstat
;
2925 /* Prevent trying to stop it. */
2928 /* No further stops are expected from a dead lwp. */
2929 lwp
->stop_expected
= 0;
2933 wait_for_sigstop (struct inferior_list_entry
*entry
)
2935 struct thread_info
*thread
= (struct thread_info
*) entry
;
2936 struct lwp_info
*lwp
= get_thread_lwp (thread
);
2937 struct thread_info
*saved_inferior
;
2946 debug_printf ("wait_for_sigstop: LWP %ld already stopped\n",
2951 saved_inferior
= current_inferior
;
2952 if (saved_inferior
!= NULL
)
2953 saved_tid
= saved_inferior
->entry
.id
;
2955 saved_tid
= null_ptid
; /* avoid bogus unused warning */
2957 ptid
= thread
->entry
.id
;
2960 debug_printf ("wait_for_sigstop: pulling one event\n");
2962 pid
= linux_wait_for_event (ptid
, &wstat
, __WALL
);
2964 /* If we stopped with a non-SIGSTOP signal, save it for later
2965 and record the pending SIGSTOP. If the process exited, just
2967 if (WIFSTOPPED (wstat
))
2970 debug_printf ("LWP %ld stopped with signal %d\n",
2971 lwpid_of (thread
), WSTOPSIG (wstat
));
2973 if (WSTOPSIG (wstat
) != SIGSTOP
)
2976 debug_printf ("LWP %ld stopped with non-sigstop status %06x\n",
2977 lwpid_of (thread
), wstat
);
2979 lwp
->status_pending_p
= 1;
2980 lwp
->status_pending
= wstat
;
2986 debug_printf ("Process %d exited while stopping LWPs\n", pid
);
2988 lwp
= find_lwp_pid (pid_to_ptid (pid
));
2991 /* Leave this status pending for the next time we're able to
2992 report it. In the mean time, we'll report this lwp as
2993 dead to GDB, so GDB doesn't try to read registers and
2994 memory from it. This can only happen if this was the
2995 last thread of the process; otherwise, PID is removed
2996 from the thread tables before linux_wait_for_event
2998 mark_lwp_dead (lwp
, wstat
);
3002 if (saved_inferior
== NULL
|| linux_thread_alive (saved_tid
))
3003 current_inferior
= saved_inferior
;
3007 debug_printf ("Previously current thread died.\n");
3011 /* We can't change the current inferior behind GDB's back,
3012 otherwise, a subsequent command may apply to the wrong
3014 current_inferior
= NULL
;
3018 /* Set a valid thread as current. */
3019 set_desired_inferior (0);
3024 /* Returns true if LWP ENTRY is stopped in a jump pad, and we can't
3025 move it out, because we need to report the stop event to GDB. For
3026 example, if the user puts a breakpoint in the jump pad, it's
3027 because she wants to debug it. */
3030 stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
, void *data
)
3032 struct thread_info
*thread
= (struct thread_info
*) entry
;
3033 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3035 gdb_assert (lwp
->suspended
== 0);
3036 gdb_assert (lwp
->stopped
);
3038 /* Allow debugging the jump pad, gdb_collect, etc.. */
3039 return (supports_fast_tracepoints ()
3040 && agent_loaded_p ()
3041 && (gdb_breakpoint_here (lwp
->stop_pc
)
3042 || lwp
->stopped_by_watchpoint
3043 || thread
->last_resume_kind
== resume_step
)
3044 && linux_fast_tracepoint_collecting (lwp
, NULL
));
3048 move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
)
3050 struct thread_info
*thread
= (struct thread_info
*) entry
;
3051 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3054 gdb_assert (lwp
->suspended
== 0);
3055 gdb_assert (lwp
->stopped
);
3057 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
3059 /* Allow debugging the jump pad, gdb_collect, etc. */
3060 if (!gdb_breakpoint_here (lwp
->stop_pc
)
3061 && !lwp
->stopped_by_watchpoint
3062 && thread
->last_resume_kind
!= resume_step
3063 && maybe_move_out_of_jump_pad (lwp
, wstat
))
3066 debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
3071 lwp
->status_pending_p
= 0;
3072 enqueue_one_deferred_signal (lwp
, wstat
);
3075 debug_printf ("Signal %d for LWP %ld deferred "
3077 WSTOPSIG (*wstat
), lwpid_of (thread
));
3080 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
3087 lwp_running (struct inferior_list_entry
*entry
, void *data
)
3089 struct thread_info
*thread
= (struct thread_info
*) entry
;
3090 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3099 /* Stop all lwps that aren't stopped yet, except EXCEPT, if not NULL.
3100 If SUSPEND, then also increase the suspend count of every LWP,
3104 stop_all_lwps (int suspend
, struct lwp_info
*except
)
3106 /* Should not be called recursively. */
3107 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
3112 debug_printf ("stop_all_lwps (%s, except=%s)\n",
3113 suspend
? "stop-and-suspend" : "stop",
3115 ? target_pid_to_str (ptid_of (get_lwp_thread (except
)))
3119 stopping_threads
= (suspend
3120 ? STOPPING_AND_SUSPENDING_THREADS
3121 : STOPPING_THREADS
);
3124 find_inferior (&all_threads
, suspend_and_send_sigstop_callback
, except
);
3126 find_inferior (&all_threads
, send_sigstop_callback
, except
);
3127 for_each_inferior (&all_threads
, wait_for_sigstop
);
3128 stopping_threads
= NOT_STOPPING_THREADS
;
3132 debug_printf ("stop_all_lwps done, setting stopping_threads "
3133 "back to !stopping\n");
3138 /* Resume execution of the inferior process.
3139 If STEP is nonzero, single-step it.
3140 If SIGNAL is nonzero, give it that signal. */
3143 linux_resume_one_lwp (struct lwp_info
*lwp
,
3144 int step
, int signal
, siginfo_t
*info
)
3146 struct thread_info
*thread
= get_lwp_thread (lwp
);
3147 struct thread_info
*saved_inferior
;
3148 int fast_tp_collecting
;
3150 if (lwp
->stopped
== 0)
3153 fast_tp_collecting
= lwp
->collecting_fast_tracepoint
;
3155 gdb_assert (!stabilizing_threads
|| fast_tp_collecting
);
3157 /* Cancel actions that rely on GDB not changing the PC (e.g., the
3158 user used the "jump" command, or "set $pc = foo"). */
3159 if (lwp
->stop_pc
!= get_pc (lwp
))
3161 /* Collecting 'while-stepping' actions doesn't make sense
3163 release_while_stepping_state_list (thread
);
3166 /* If we have pending signals or status, and a new signal, enqueue the
3167 signal. Also enqueue the signal if we are waiting to reinsert a
3168 breakpoint; it will be picked up again below. */
3170 && (lwp
->status_pending_p
3171 || lwp
->pending_signals
!= NULL
3172 || lwp
->bp_reinsert
!= 0
3173 || fast_tp_collecting
))
3175 struct pending_signals
*p_sig
;
3176 p_sig
= xmalloc (sizeof (*p_sig
));
3177 p_sig
->prev
= lwp
->pending_signals
;
3178 p_sig
->signal
= signal
;
3180 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
3182 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
3183 lwp
->pending_signals
= p_sig
;
3186 if (lwp
->status_pending_p
)
3189 debug_printf ("Not resuming lwp %ld (%s, signal %d, stop %s);"
3190 " has pending status\n",
3191 lwpid_of (thread
), step
? "step" : "continue", signal
,
3192 lwp
->stop_expected
? "expected" : "not expected");
3196 saved_inferior
= current_inferior
;
3197 current_inferior
= thread
;
3200 debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
3201 lwpid_of (thread
), step
? "step" : "continue", signal
,
3202 lwp
->stop_expected
? "expected" : "not expected");
3204 /* This bit needs some thinking about. If we get a signal that
3205 we must report while a single-step reinsert is still pending,
3206 we often end up resuming the thread. It might be better to
3207 (ew) allow a stack of pending events; then we could be sure that
3208 the reinsert happened right away and not lose any signals.
3210 Making this stack would also shrink the window in which breakpoints are
3211 uninserted (see comment in linux_wait_for_lwp) but not enough for
3212 complete correctness, so it won't solve that problem. It may be
3213 worthwhile just to solve this one, however. */
3214 if (lwp
->bp_reinsert
!= 0)
3217 debug_printf (" pending reinsert at 0x%s\n",
3218 paddress (lwp
->bp_reinsert
));
3220 if (can_hardware_single_step ())
3222 if (fast_tp_collecting
== 0)
3225 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
3227 fprintf (stderr
, "BAD - reinserting and suspended(%d).\n",
3234 /* Postpone any pending signal. It was enqueued above. */
3238 if (fast_tp_collecting
== 1)
3241 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
3242 " (exit-jump-pad-bkpt)\n",
3245 /* Postpone any pending signal. It was enqueued above. */
3248 else if (fast_tp_collecting
== 2)
3251 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
3252 " single-stepping\n",
3255 if (can_hardware_single_step ())
3258 fatal ("moving out of jump pad single-stepping"
3259 " not implemented on this target");
3261 /* Postpone any pending signal. It was enqueued above. */
3265 /* If we have while-stepping actions in this thread set it stepping.
3266 If we have a signal to deliver, it may or may not be set to
3267 SIG_IGN, we don't know. Assume so, and allow collecting
3268 while-stepping into a signal handler. A possible smart thing to
3269 do would be to set an internal breakpoint at the signal return
3270 address, continue, and carry on catching this while-stepping
3271 action only when that breakpoint is hit. A future
3273 if (thread
->while_stepping
!= NULL
3274 && can_hardware_single_step ())
3277 debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
3282 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
3284 struct regcache
*regcache
= get_thread_regcache (current_inferior
, 1);
3285 CORE_ADDR pc
= (*the_low_target
.get_pc
) (regcache
);
3286 debug_printf (" resuming from pc 0x%lx\n", (long) pc
);
3289 /* If we have pending signals, consume one unless we are trying to
3290 reinsert a breakpoint or we're trying to finish a fast tracepoint
3292 if (lwp
->pending_signals
!= NULL
3293 && lwp
->bp_reinsert
== 0
3294 && fast_tp_collecting
== 0)
3296 struct pending_signals
**p_sig
;
3298 p_sig
= &lwp
->pending_signals
;
3299 while ((*p_sig
)->prev
!= NULL
)
3300 p_sig
= &(*p_sig
)->prev
;
3302 signal
= (*p_sig
)->signal
;
3303 if ((*p_sig
)->info
.si_signo
!= 0)
3304 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
3311 if (the_low_target
.prepare_to_resume
!= NULL
)
3312 the_low_target
.prepare_to_resume (lwp
);
3314 regcache_invalidate_thread (thread
);
3317 lwp
->stopped_by_watchpoint
= 0;
3318 lwp
->stepping
= step
;
3319 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, lwpid_of (thread
),
3320 (PTRACE_TYPE_ARG3
) 0,
3321 /* Coerce to a uintptr_t first to avoid potential gcc warning
3322 of coercing an 8 byte integer to a 4 byte pointer. */
3323 (PTRACE_TYPE_ARG4
) (uintptr_t) signal
);
3325 current_inferior
= saved_inferior
;
3328 /* ESRCH from ptrace either means that the thread was already
3329 running (an error) or that it is gone (a race condition). If
3330 it's gone, we will get a notification the next time we wait,
3331 so we can ignore the error. We could differentiate these
3332 two, but it's tricky without waiting; the thread still exists
3333 as a zombie, so sending it signal 0 would succeed. So just
3338 perror_with_name ("ptrace");
3342 struct thread_resume_array
3344 struct thread_resume
*resume
;
3348 /* This function is called once per thread via find_inferior.
3349 ARG is a pointer to a thread_resume_array struct.
3350 We look up the thread specified by ENTRY in ARG, and mark the thread
3351 with a pointer to the appropriate resume request.
3353 This algorithm is O(threads * resume elements), but resume elements
3354 is small (and will remain small at least until GDB supports thread
3358 linux_set_resume_request (struct inferior_list_entry
*entry
, void *arg
)
3360 struct thread_info
*thread
= (struct thread_info
*) entry
;
3361 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3363 struct thread_resume_array
*r
;
3367 for (ndx
= 0; ndx
< r
->n
; ndx
++)
3369 ptid_t ptid
= r
->resume
[ndx
].thread
;
3370 if (ptid_equal (ptid
, minus_one_ptid
)
3371 || ptid_equal (ptid
, entry
->id
)
3372 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
3374 || (ptid_get_pid (ptid
) == pid_of (thread
)
3375 && (ptid_is_pid (ptid
)
3376 || ptid_get_lwp (ptid
) == -1)))
3378 if (r
->resume
[ndx
].kind
== resume_stop
3379 && thread
->last_resume_kind
== resume_stop
)
3382 debug_printf ("already %s LWP %ld at GDB's request\n",
3383 (thread
->last_status
.kind
3384 == TARGET_WAITKIND_STOPPED
)
3392 lwp
->resume
= &r
->resume
[ndx
];
3393 thread
->last_resume_kind
= lwp
->resume
->kind
;
3395 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
3396 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
3398 /* If we had a deferred signal to report, dequeue one now.
3399 This can happen if LWP gets more than one signal while
3400 trying to get out of a jump pad. */
3402 && !lwp
->status_pending_p
3403 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
3405 lwp
->status_pending_p
= 1;
3408 debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
3409 "leaving status pending.\n",
3410 WSTOPSIG (lwp
->status_pending
),
3418 /* No resume action for this thread. */
3424 /* find_inferior callback for linux_resume.
3425 Set *FLAG_P if this lwp has an interesting status pending. */
3428 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
3430 struct thread_info
*thread
= (struct thread_info
*) entry
;
3431 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3433 /* LWPs which will not be resumed are not interesting, because
3434 we might not wait for them next time through linux_wait. */
3435 if (lwp
->resume
== NULL
)
3438 if (lwp
->status_pending_p
)
3439 * (int *) flag_p
= 1;
3444 /* Return 1 if this lwp that GDB wants running is stopped at an
3445 internal breakpoint that we need to step over. It assumes that any
3446 required STOP_PC adjustment has already been propagated to the
3447 inferior's regcache. */
3450 need_step_over_p (struct inferior_list_entry
*entry
, void *dummy
)
3452 struct thread_info
*thread
= (struct thread_info
*) entry
;
3453 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3454 struct thread_info
*saved_inferior
;
3457 /* LWPs which will not be resumed are not interesting, because we
3458 might not wait for them next time through linux_wait. */
3463 debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
3468 if (thread
->last_resume_kind
== resume_stop
)
3471 debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
3477 gdb_assert (lwp
->suspended
>= 0);
3482 debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
3487 if (!lwp
->need_step_over
)
3490 debug_printf ("Need step over [LWP %ld]? No\n", lwpid_of (thread
));
3493 if (lwp
->status_pending_p
)
3496 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
3502 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
3506 /* If the PC has changed since we stopped, then don't do anything,
3507 and let the breakpoint/tracepoint be hit. This happens if, for
3508 instance, GDB handled the decr_pc_after_break subtraction itself,
3509 GDB is OOL stepping this thread, or the user has issued a "jump"
3510 command, or poked thread's registers herself. */
3511 if (pc
!= lwp
->stop_pc
)
3514 debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
3515 "Old stop_pc was 0x%s, PC is now 0x%s\n",
3517 paddress (lwp
->stop_pc
), paddress (pc
));
3519 lwp
->need_step_over
= 0;
3523 saved_inferior
= current_inferior
;
3524 current_inferior
= thread
;
3526 /* We can only step over breakpoints we know about. */
3527 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
3529 /* Don't step over a breakpoint that GDB expects to hit
3530 though. If the condition is being evaluated on the target's side
3531 and it evaluate to false, step over this breakpoint as well. */
3532 if (gdb_breakpoint_here (pc
)
3533 && gdb_condition_true_at_breakpoint (pc
)
3534 && gdb_no_commands_at_breakpoint (pc
))
3537 debug_printf ("Need step over [LWP %ld]? yes, but found"
3538 " GDB breakpoint at 0x%s; skipping step over\n",
3539 lwpid_of (thread
), paddress (pc
));
3541 current_inferior
= saved_inferior
;
3547 debug_printf ("Need step over [LWP %ld]? yes, "
3548 "found breakpoint at 0x%s\n",
3549 lwpid_of (thread
), paddress (pc
));
3551 /* We've found an lwp that needs stepping over --- return 1 so
3552 that find_inferior stops looking. */
3553 current_inferior
= saved_inferior
;
3555 /* If the step over is cancelled, this is set again. */
3556 lwp
->need_step_over
= 0;
3561 current_inferior
= saved_inferior
;
3564 debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
3566 lwpid_of (thread
), paddress (pc
));
3571 /* Start a step-over operation on LWP. When LWP stopped at a
3572 breakpoint, to make progress, we need to remove the breakpoint out
3573 of the way. If we let other threads run while we do that, they may
3574 pass by the breakpoint location and miss hitting it. To avoid
3575 that, a step-over momentarily stops all threads while LWP is
3576 single-stepped while the breakpoint is temporarily uninserted from
3577 the inferior. When the single-step finishes, we reinsert the
3578 breakpoint, and let all threads that are supposed to be running,
3581 On targets that don't support hardware single-step, we don't
3582 currently support full software single-stepping. Instead, we only
3583 support stepping over the thread event breakpoint, by asking the
3584 low target where to place a reinsert breakpoint. Since this
3585 routine assumes the breakpoint being stepped over is a thread event
3586 breakpoint, it usually assumes the return address of the current
3587 function is a good enough place to set the reinsert breakpoint. */
3590 start_step_over (struct lwp_info
*lwp
)
3592 struct thread_info
*thread
= get_lwp_thread (lwp
);
3593 struct thread_info
*saved_inferior
;
3598 debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
3601 stop_all_lwps (1, lwp
);
3602 gdb_assert (lwp
->suspended
== 0);
3605 debug_printf ("Done stopping all threads for step-over.\n");
3607 /* Note, we should always reach here with an already adjusted PC,
3608 either by GDB (if we're resuming due to GDB's request), or by our
3609 caller, if we just finished handling an internal breakpoint GDB
3610 shouldn't care about. */
3613 saved_inferior
= current_inferior
;
3614 current_inferior
= thread
;
3616 lwp
->bp_reinsert
= pc
;
3617 uninsert_breakpoints_at (pc
);
3618 uninsert_fast_tracepoint_jumps_at (pc
);
3620 if (can_hardware_single_step ())
3626 CORE_ADDR raddr
= (*the_low_target
.breakpoint_reinsert_addr
) ();
3627 set_reinsert_breakpoint (raddr
);
3631 current_inferior
= saved_inferior
;
3633 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
3635 /* Require next event from this LWP. */
3636 step_over_bkpt
= thread
->entry
.id
;
3640 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
3641 start_step_over, if still there, and delete any reinsert
3642 breakpoints we've set, on non hardware single-step targets. */
3645 finish_step_over (struct lwp_info
*lwp
)
3647 if (lwp
->bp_reinsert
!= 0)
3650 debug_printf ("Finished step over.\n");
3652 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
3653 may be no breakpoint to reinsert there by now. */
3654 reinsert_breakpoints_at (lwp
->bp_reinsert
);
3655 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
3657 lwp
->bp_reinsert
= 0;
3659 /* Delete any software-single-step reinsert breakpoints. No
3660 longer needed. We don't have to worry about other threads
3661 hitting this trap, and later not being able to explain it,
3662 because we were stepping over a breakpoint, and we hold all
3663 threads but LWP stopped while doing that. */
3664 if (!can_hardware_single_step ())
3665 delete_reinsert_breakpoints ();
3667 step_over_bkpt
= null_ptid
;
3674 /* This function is called once per thread. We check the thread's resume
3675 request, which will tell us whether to resume, step, or leave the thread
3676 stopped; and what signal, if any, it should be sent.
3678 For threads which we aren't explicitly told otherwise, we preserve
3679 the stepping flag; this is used for stepping over gdbserver-placed
3682 If pending_flags was set in any thread, we queue any needed
3683 signals, since we won't actually resume. We already have a pending
3684 event to report, so we don't need to preserve any step requests;
3685 they should be re-issued if necessary. */
3688 linux_resume_one_thread (struct inferior_list_entry
*entry
, void *arg
)
3690 struct thread_info
*thread
= (struct thread_info
*) entry
;
3691 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3693 int leave_all_stopped
= * (int *) arg
;
3696 if (lwp
->resume
== NULL
)
3699 if (lwp
->resume
->kind
== resume_stop
)
3702 debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread
));
3707 debug_printf ("stopping LWP %ld\n", lwpid_of (thread
));
3709 /* Stop the thread, and wait for the event asynchronously,
3710 through the event loop. */
3716 debug_printf ("already stopped LWP %ld\n",
3719 /* The LWP may have been stopped in an internal event that
3720 was not meant to be notified back to GDB (e.g., gdbserver
3721 breakpoint), so we should be reporting a stop event in
3724 /* If the thread already has a pending SIGSTOP, this is a
3725 no-op. Otherwise, something later will presumably resume
3726 the thread and this will cause it to cancel any pending
3727 operation, due to last_resume_kind == resume_stop. If
3728 the thread already has a pending status to report, we
3729 will still report it the next time we wait - see
3730 status_pending_p_callback. */
3732 /* If we already have a pending signal to report, then
3733 there's no need to queue a SIGSTOP, as this means we're
3734 midway through moving the LWP out of the jumppad, and we
3735 will report the pending signal as soon as that is
3737 if (lwp
->pending_signals_to_report
== NULL
)
3741 /* For stop requests, we're done. */
3743 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
3747 /* If this thread which is about to be resumed has a pending status,
3748 then don't resume any threads - we can just report the pending
3749 status. Make sure to queue any signals that would otherwise be
3750 sent. In all-stop mode, we do this decision based on if *any*
3751 thread has a pending status. If there's a thread that needs the
3752 step-over-breakpoint dance, then don't resume any other thread
3753 but that particular one. */
3754 leave_pending
= (lwp
->status_pending_p
|| leave_all_stopped
);
3759 debug_printf ("resuming LWP %ld\n", lwpid_of (thread
));
3761 step
= (lwp
->resume
->kind
== resume_step
);
3762 linux_resume_one_lwp (lwp
, step
, lwp
->resume
->sig
, NULL
);
3767 debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread
));
3769 /* If we have a new signal, enqueue the signal. */
3770 if (lwp
->resume
->sig
!= 0)
3772 struct pending_signals
*p_sig
;
3773 p_sig
= xmalloc (sizeof (*p_sig
));
3774 p_sig
->prev
= lwp
->pending_signals
;
3775 p_sig
->signal
= lwp
->resume
->sig
;
3776 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
3778 /* If this is the same signal we were previously stopped by,
3779 make sure to queue its siginfo. We can ignore the return
3780 value of ptrace; if it fails, we'll skip
3781 PTRACE_SETSIGINFO. */
3782 if (WIFSTOPPED (lwp
->last_status
)
3783 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
)
3784 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
3787 lwp
->pending_signals
= p_sig
;
3791 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
3797 linux_resume (struct thread_resume
*resume_info
, size_t n
)
3799 struct thread_resume_array array
= { resume_info
, n
};
3800 struct thread_info
*need_step_over
= NULL
;
3802 int leave_all_stopped
;
3807 debug_printf ("linux_resume:\n");
3810 find_inferior (&all_threads
, linux_set_resume_request
, &array
);
3812 /* If there is a thread which would otherwise be resumed, which has
3813 a pending status, then don't resume any threads - we can just
3814 report the pending status. Make sure to queue any signals that
3815 would otherwise be sent. In non-stop mode, we'll apply this
3816 logic to each thread individually. We consume all pending events
3817 before considering to start a step-over (in all-stop). */
3820 find_inferior (&all_threads
, resume_status_pending_p
, &any_pending
);
3822 /* If there is a thread which would otherwise be resumed, which is
3823 stopped at a breakpoint that needs stepping over, then don't
3824 resume any threads - have it step over the breakpoint with all
3825 other threads stopped, then resume all threads again. Make sure
3826 to queue any signals that would otherwise be delivered or
3828 if (!any_pending
&& supports_breakpoints ())
3830 = (struct thread_info
*) find_inferior (&all_threads
,
3831 need_step_over_p
, NULL
);
3833 leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
3837 if (need_step_over
!= NULL
)
3838 debug_printf ("Not resuming all, need step over\n");
3839 else if (any_pending
)
3840 debug_printf ("Not resuming, all-stop and found "
3841 "an LWP with pending status\n");
3843 debug_printf ("Resuming, no pending status or step over needed\n");
3846 /* Even if we're leaving threads stopped, queue all signals we'd
3847 otherwise deliver. */
3848 find_inferior (&all_threads
, linux_resume_one_thread
, &leave_all_stopped
);
3851 start_step_over (get_thread_lwp (need_step_over
));
3855 debug_printf ("linux_resume done\n");
3860 /* This function is called once per thread. We check the thread's
3861 last resume request, which will tell us whether to resume, step, or
3862 leave the thread stopped. Any signal the client requested to be
3863 delivered has already been enqueued at this point.
3865 If any thread that GDB wants running is stopped at an internal
3866 breakpoint that needs stepping over, we start a step-over operation
3867 on that particular thread, and leave all others stopped. */
3870 proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
3872 struct thread_info
*thread
= (struct thread_info
*) entry
;
3873 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3880 debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread
));
3885 debug_printf (" LWP %ld already running\n", lwpid_of (thread
));
3889 if (thread
->last_resume_kind
== resume_stop
3890 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3893 debug_printf (" client wants LWP to remain %ld stopped\n",
3898 if (lwp
->status_pending_p
)
3901 debug_printf (" LWP %ld has pending status, leaving stopped\n",
3906 gdb_assert (lwp
->suspended
>= 0);
3911 debug_printf (" LWP %ld is suspended\n", lwpid_of (thread
));
3915 if (thread
->last_resume_kind
== resume_stop
3916 && lwp
->pending_signals_to_report
== NULL
3917 && lwp
->collecting_fast_tracepoint
== 0)
3919 /* We haven't reported this LWP as stopped yet (otherwise, the
3920 last_status.kind check above would catch it, and we wouldn't
3921 reach here. This LWP may have been momentarily paused by a
3922 stop_all_lwps call while handling for example, another LWP's
3923 step-over. In that case, the pending expected SIGSTOP signal
3924 that was queued at vCont;t handling time will have already
3925 been consumed by wait_for_sigstop, and so we need to requeue
3926 another one here. Note that if the LWP already has a SIGSTOP
3927 pending, this is a no-op. */
3930 debug_printf ("Client wants LWP %ld to stop. "
3931 "Making sure it has a SIGSTOP pending\n",
3937 step
= thread
->last_resume_kind
== resume_step
;
3938 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
3943 unsuspend_and_proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
3945 struct thread_info
*thread
= (struct thread_info
*) entry
;
3946 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3952 gdb_assert (lwp
->suspended
>= 0);
3954 return proceed_one_lwp (entry
, except
);
3957 /* When we finish a step-over, set threads running again. If there's
3958 another thread that may need a step-over, now's the time to start
3959 it. Eventually, we'll move all threads past their breakpoints. */
3962 proceed_all_lwps (void)
3964 struct thread_info
*need_step_over
;
3966 /* If there is a thread which would otherwise be resumed, which is
3967 stopped at a breakpoint that needs stepping over, then don't
3968 resume any threads - have it step over the breakpoint with all
3969 other threads stopped, then resume all threads again. */
3971 if (supports_breakpoints ())
3974 = (struct thread_info
*) find_inferior (&all_threads
,
3975 need_step_over_p
, NULL
);
3977 if (need_step_over
!= NULL
)
3980 debug_printf ("proceed_all_lwps: found "
3981 "thread %ld needing a step-over\n",
3982 lwpid_of (need_step_over
));
3984 start_step_over (get_thread_lwp (need_step_over
));
3990 debug_printf ("Proceeding, no step-over needed\n");
3992 find_inferior (&all_threads
, proceed_one_lwp
, NULL
);
3995 /* Stopped LWPs that the client wanted to be running, that don't have
3996 pending statuses, are set to run again, except for EXCEPT, if not
3997 NULL. This undoes a stop_all_lwps call. */
4000 unstop_all_lwps (int unsuspend
, struct lwp_info
*except
)
4006 debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
4007 lwpid_of (get_lwp_thread (except
)));
4009 debug_printf ("unstopping all lwps\n");
4013 find_inferior (&all_threads
, unsuspend_and_proceed_one_lwp
, except
);
4015 find_inferior (&all_threads
, proceed_one_lwp
, except
);
4019 debug_printf ("unstop_all_lwps done\n");
4025 #ifdef HAVE_LINUX_REGSETS
4027 #define use_linux_regsets 1
4029 /* Returns true if REGSET has been disabled. */
4032 regset_disabled (struct regsets_info
*info
, struct regset_info
*regset
)
4034 return (info
->disabled_regsets
!= NULL
4035 && info
->disabled_regsets
[regset
- info
->regsets
]);
4038 /* Disable REGSET. */
4041 disable_regset (struct regsets_info
*info
, struct regset_info
*regset
)
4045 dr_offset
= regset
- info
->regsets
;
4046 if (info
->disabled_regsets
== NULL
)
4047 info
->disabled_regsets
= xcalloc (1, info
->num_regsets
);
4048 info
->disabled_regsets
[dr_offset
] = 1;
4052 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
4053 struct regcache
*regcache
)
4055 struct regset_info
*regset
;
4056 int saw_general_regs
= 0;
4060 regset
= regsets_info
->regsets
;
4062 pid
= lwpid_of (current_inferior
);
4063 while (regset
->size
>= 0)
4068 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
4074 buf
= xmalloc (regset
->size
);
4076 nt_type
= regset
->nt_type
;
4080 iov
.iov_len
= regset
->size
;
4081 data
= (void *) &iov
;
4087 res
= ptrace (regset
->get_request
, pid
,
4088 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
4090 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4096 /* If we get EIO on a regset, do not try it again for
4097 this process mode. */
4098 disable_regset (regsets_info
, regset
);
4105 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
4110 else if (regset
->type
== GENERAL_REGS
)
4111 saw_general_regs
= 1;
4112 regset
->store_function (regcache
, buf
);
4116 if (saw_general_regs
)
4123 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
4124 struct regcache
*regcache
)
4126 struct regset_info
*regset
;
4127 int saw_general_regs
= 0;
4131 regset
= regsets_info
->regsets
;
4133 pid
= lwpid_of (current_inferior
);
4134 while (regset
->size
>= 0)
4139 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
4145 buf
= xmalloc (regset
->size
);
4147 /* First fill the buffer with the current register set contents,
4148 in case there are any items in the kernel's regset that are
4149 not in gdbserver's regcache. */
4151 nt_type
= regset
->nt_type
;
4155 iov
.iov_len
= regset
->size
;
4156 data
= (void *) &iov
;
4162 res
= ptrace (regset
->get_request
, pid
,
4163 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
4165 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4170 /* Then overlay our cached registers on that. */
4171 regset
->fill_function (regcache
, buf
);
4173 /* Only now do we write the register set. */
4175 res
= ptrace (regset
->set_request
, pid
,
4176 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
4178 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
4186 /* If we get EIO on a regset, do not try it again for
4187 this process mode. */
4188 disable_regset (regsets_info
, regset
);
4192 else if (errno
== ESRCH
)
4194 /* At this point, ESRCH should mean the process is
4195 already gone, in which case we simply ignore attempts
4196 to change its registers. See also the related
4197 comment in linux_resume_one_lwp. */
4203 perror ("Warning: ptrace(regsets_store_inferior_registers)");
4206 else if (regset
->type
== GENERAL_REGS
)
4207 saw_general_regs
= 1;
4211 if (saw_general_regs
)
4217 #else /* !HAVE_LINUX_REGSETS */
4219 #define use_linux_regsets 0
4220 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
4221 #define regsets_store_inferior_registers(regsets_info, regcache) 1
4225 /* Return 1 if register REGNO is supported by one of the regset ptrace
4226 calls or 0 if it has to be transferred individually. */
4229 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
4231 unsigned char mask
= 1 << (regno
% 8);
4232 size_t index
= regno
/ 8;
4234 return (use_linux_regsets
4235 && (regs_info
->regset_bitmap
== NULL
4236 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
4239 #ifdef HAVE_LINUX_USRREGS
4242 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
4246 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
4247 error ("Invalid register number %d.", regnum
);
4249 addr
= usrregs
->regmap
[regnum
];
4254 /* Fetch one register. */
4256 fetch_register (const struct usrregs_info
*usrregs
,
4257 struct regcache
*regcache
, int regno
)
4264 if (regno
>= usrregs
->num_regs
)
4266 if ((*the_low_target
.cannot_fetch_register
) (regno
))
4269 regaddr
= register_addr (usrregs
, regno
);
4273 size
= ((register_size (regcache
->tdesc
, regno
)
4274 + sizeof (PTRACE_XFER_TYPE
) - 1)
4275 & -sizeof (PTRACE_XFER_TYPE
));
4276 buf
= alloca (size
);
4278 pid
= lwpid_of (current_inferior
);
4279 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
4282 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
4283 ptrace (PTRACE_PEEKUSER
, pid
,
4284 /* Coerce to a uintptr_t first to avoid potential gcc warning
4285 of coercing an 8 byte integer to a 4 byte pointer. */
4286 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
, (PTRACE_TYPE_ARG4
) 0);
4287 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
4289 error ("reading register %d: %s", regno
, strerror (errno
));
4292 if (the_low_target
.supply_ptrace_register
)
4293 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
4295 supply_register (regcache
, regno
, buf
);
4298 /* Store one register. */
4300 store_register (const struct usrregs_info
*usrregs
,
4301 struct regcache
*regcache
, int regno
)
4308 if (regno
>= usrregs
->num_regs
)
4310 if ((*the_low_target
.cannot_store_register
) (regno
))
4313 regaddr
= register_addr (usrregs
, regno
);
4317 size
= ((register_size (regcache
->tdesc
, regno
)
4318 + sizeof (PTRACE_XFER_TYPE
) - 1)
4319 & -sizeof (PTRACE_XFER_TYPE
));
4320 buf
= alloca (size
);
4321 memset (buf
, 0, size
);
4323 if (the_low_target
.collect_ptrace_register
)
4324 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
4326 collect_register (regcache
, regno
, buf
);
4328 pid
= lwpid_of (current_inferior
);
4329 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
4332 ptrace (PTRACE_POKEUSER
, pid
,
4333 /* Coerce to a uintptr_t first to avoid potential gcc warning
4334 about coercing an 8 byte integer to a 4 byte pointer. */
4335 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
,
4336 (PTRACE_TYPE_ARG4
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
4339 /* At this point, ESRCH should mean the process is
4340 already gone, in which case we simply ignore attempts
4341 to change its registers. See also the related
4342 comment in linux_resume_one_lwp. */
4346 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
4347 error ("writing register %d: %s", regno
, strerror (errno
));
4349 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
4353 /* Fetch all registers, or just one, from the child process.
4354 If REGNO is -1, do this for all registers, skipping any that are
4355 assumed to have been retrieved by regsets_fetch_inferior_registers,
4356 unless ALL is non-zero.
4357 Otherwise, REGNO specifies which register (so we can save time). */
4359 usr_fetch_inferior_registers (const struct regs_info
*regs_info
,
4360 struct regcache
*regcache
, int regno
, int all
)
4362 struct usrregs_info
*usr
= regs_info
->usrregs
;
4366 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
4367 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
4368 fetch_register (usr
, regcache
, regno
);
4371 fetch_register (usr
, regcache
, regno
);
4374 /* Store our register values back into the inferior.
4375 If REGNO is -1, do this for all registers, skipping any that are
4376 assumed to have been saved by regsets_store_inferior_registers,
4377 unless ALL is non-zero.
4378 Otherwise, REGNO specifies which register (so we can save time). */
4380 usr_store_inferior_registers (const struct regs_info
*regs_info
,
4381 struct regcache
*regcache
, int regno
, int all
)
4383 struct usrregs_info
*usr
= regs_info
->usrregs
;
4387 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
4388 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
4389 store_register (usr
, regcache
, regno
);
4392 store_register (usr
, regcache
, regno
);
4395 #else /* !HAVE_LINUX_USRREGS */
4397 #define usr_fetch_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
4398 #define usr_store_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
4404 linux_fetch_registers (struct regcache
*regcache
, int regno
)
4408 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
4412 if (the_low_target
.fetch_register
!= NULL
4413 && regs_info
->usrregs
!= NULL
)
4414 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
4415 (*the_low_target
.fetch_register
) (regcache
, regno
);
4417 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
4418 if (regs_info
->usrregs
!= NULL
)
4419 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
4423 if (the_low_target
.fetch_register
!= NULL
4424 && (*the_low_target
.fetch_register
) (regcache
, regno
))
4427 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
4429 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
4431 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
4432 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
4437 linux_store_registers (struct regcache
*regcache
, int regno
)
4441 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
4445 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
4447 if (regs_info
->usrregs
!= NULL
)
4448 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
4452 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
4454 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
4456 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
4457 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
4462 /* Copy LEN bytes from inferior's memory starting at MEMADDR
4463 to debugger memory starting at MYADDR. */
4466 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
4468 int pid
= lwpid_of (current_inferior
);
4469 register PTRACE_XFER_TYPE
*buffer
;
4470 register CORE_ADDR addr
;
4477 /* Try using /proc. Don't bother for one word. */
4478 if (len
>= 3 * sizeof (long))
4482 /* We could keep this file open and cache it - possibly one per
4483 thread. That requires some juggling, but is even faster. */
4484 sprintf (filename
, "/proc/%d/mem", pid
);
4485 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4489 /* If pread64 is available, use it. It's faster if the kernel
4490 supports it (only one syscall), and it's 64-bit safe even on
4491 32-bit platforms (for instance, SPARC debugging a SPARC64
4494 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
4497 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
4498 bytes
= read (fd
, myaddr
, len
);
4505 /* Some data was read, we'll try to get the rest with ptrace. */
4515 /* Round starting address down to longword boundary. */
4516 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
4517 /* Round ending address up; get number of longwords that makes. */
4518 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4519 / sizeof (PTRACE_XFER_TYPE
));
4520 /* Allocate buffer of that many longwords. */
4521 buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
4523 /* Read all the longwords */
4525 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
4527 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4528 about coercing an 8 byte integer to a 4 byte pointer. */
4529 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
4530 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
4531 (PTRACE_TYPE_ARG4
) 0);
4537 /* Copy appropriate bytes out of the buffer. */
4540 i
*= sizeof (PTRACE_XFER_TYPE
);
4541 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
4543 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
4550 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
4551 memory at MEMADDR. On failure (cannot write to the inferior)
4552 returns the value of errno. Always succeeds if LEN is zero. */
4555 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
4558 /* Round starting address down to longword boundary. */
4559 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
4560 /* Round ending address up; get number of longwords that makes. */
4562 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4563 / sizeof (PTRACE_XFER_TYPE
);
4565 /* Allocate buffer of that many longwords. */
4566 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*)
4567 alloca (count
* sizeof (PTRACE_XFER_TYPE
));
4569 int pid
= lwpid_of (current_inferior
);
4573 /* Zero length write always succeeds. */
4579 /* Dump up to four bytes. */
4580 unsigned int val
= * (unsigned int *) myaddr
;
4586 val
= val
& 0xffffff;
4587 debug_printf ("Writing %0*x to 0x%08lx\n", 2 * ((len
< 4) ? len
: 4),
4588 val
, (long)memaddr
);
4591 /* Fill start and end extra bytes of buffer with existing memory data. */
4594 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4595 about coercing an 8 byte integer to a 4 byte pointer. */
4596 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
4597 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
4598 (PTRACE_TYPE_ARG4
) 0);
4606 = ptrace (PTRACE_PEEKTEXT
, pid
,
4607 /* Coerce to a uintptr_t first to avoid potential gcc warning
4608 about coercing an 8 byte integer to a 4 byte pointer. */
4609 (PTRACE_TYPE_ARG3
) (uintptr_t) (addr
+ (count
- 1)
4610 * sizeof (PTRACE_XFER_TYPE
)),
4611 (PTRACE_TYPE_ARG4
) 0);
4616 /* Copy data to be written over corresponding part of buffer. */
4618 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
4621 /* Write the entire buffer. */
4623 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
4626 ptrace (PTRACE_POKETEXT
, pid
,
4627 /* Coerce to a uintptr_t first to avoid potential gcc warning
4628 about coercing an 8 byte integer to a 4 byte pointer. */
4629 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
4630 (PTRACE_TYPE_ARG4
) buffer
[i
]);
4639 linux_look_up_symbols (void)
4641 #ifdef USE_THREAD_DB
4642 struct process_info
*proc
= current_process ();
4644 if (proc
->private->thread_db
!= NULL
)
4647 /* If the kernel supports tracing clones, then we don't need to
4648 use the magic thread event breakpoint to learn about
4650 thread_db_init (!linux_supports_traceclone ());
4655 linux_request_interrupt (void)
4657 extern unsigned long signal_pid
;
4659 if (!ptid_equal (cont_thread
, null_ptid
)
4660 && !ptid_equal (cont_thread
, minus_one_ptid
))
4664 lwpid
= lwpid_of (current_inferior
);
4665 kill_lwp (lwpid
, SIGINT
);
4668 kill_lwp (signal_pid
, SIGINT
);
4671 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
4672 to debugger memory starting at MYADDR. */
4675 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
4677 char filename
[PATH_MAX
];
4679 int pid
= lwpid_of (current_inferior
);
4681 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
4683 fd
= open (filename
, O_RDONLY
);
4687 if (offset
!= (CORE_ADDR
) 0
4688 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4691 n
= read (fd
, myaddr
, len
);
4698 /* These breakpoint and watchpoint related wrapper functions simply
4699 pass on the function call if the target has registered a
4700 corresponding function. */
4703 linux_insert_point (char type
, CORE_ADDR addr
, int len
)
4705 if (the_low_target
.insert_point
!= NULL
)
4706 return the_low_target
.insert_point (type
, addr
, len
);
4708 /* Unsupported (see target.h). */
4713 linux_remove_point (char type
, CORE_ADDR addr
, int len
)
4715 if (the_low_target
.remove_point
!= NULL
)
4716 return the_low_target
.remove_point (type
, addr
, len
);
4718 /* Unsupported (see target.h). */
4723 linux_stopped_by_watchpoint (void)
4725 struct lwp_info
*lwp
= get_thread_lwp (current_inferior
);
4727 return lwp
->stopped_by_watchpoint
;
4731 linux_stopped_data_address (void)
4733 struct lwp_info
*lwp
= get_thread_lwp (current_inferior
);
4735 return lwp
->stopped_data_address
;
4738 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
4739 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
4740 && defined(PT_TEXT_END_ADDR)
4742 /* This is only used for targets that define PT_TEXT_ADDR,
4743 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
4744 the target has different ways of acquiring this information, like
4747 /* Under uClinux, programs are loaded at non-zero offsets, which we need
4748 to tell gdb about. */
4751 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
4753 unsigned long text
, text_end
, data
;
4754 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4758 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_ADDR
,
4759 (PTRACE_TYPE_ARG4
) 0);
4760 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_END_ADDR
,
4761 (PTRACE_TYPE_ARG4
) 0);
4762 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_DATA_ADDR
,
4763 (PTRACE_TYPE_ARG4
) 0);
4767 /* Both text and data offsets produced at compile-time (and so
4768 used by gdb) are relative to the beginning of the program,
4769 with the data segment immediately following the text segment.
4770 However, the actual runtime layout in memory may put the data
4771 somewhere else, so when we send gdb a data base-address, we
4772 use the real data base address and subtract the compile-time
4773 data base-address from it (which is just the length of the
4774 text segment). BSS immediately follows data in both
4777 *data_p
= data
- (text_end
- text
);
4786 linux_qxfer_osdata (const char *annex
,
4787 unsigned char *readbuf
, unsigned const char *writebuf
,
4788 CORE_ADDR offset
, int len
)
4790 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4793 /* Convert a native/host siginfo object, into/from the siginfo in the
4794 layout of the inferiors' architecture. */
4797 siginfo_fixup (siginfo_t
*siginfo
, void *inf_siginfo
, int direction
)
4801 if (the_low_target
.siginfo_fixup
!= NULL
)
4802 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
4804 /* If there was no callback, or the callback didn't do anything,
4805 then just do a straight memcpy. */
4809 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
4811 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
4816 linux_xfer_siginfo (const char *annex
, unsigned char *readbuf
,
4817 unsigned const char *writebuf
, CORE_ADDR offset
, int len
)
4821 char inf_siginfo
[sizeof (siginfo_t
)];
4823 if (current_inferior
== NULL
)
4826 pid
= lwpid_of (current_inferior
);
4829 debug_printf ("%s siginfo for lwp %d.\n",
4830 readbuf
!= NULL
? "Reading" : "Writing",
4833 if (offset
>= sizeof (siginfo
))
4836 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
4839 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
4840 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
4841 inferior with a 64-bit GDBSERVER should look the same as debugging it
4842 with a 32-bit GDBSERVER, we need to convert it. */
4843 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
4845 if (offset
+ len
> sizeof (siginfo
))
4846 len
= sizeof (siginfo
) - offset
;
4848 if (readbuf
!= NULL
)
4849 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
4852 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
4854 /* Convert back to ptrace layout before flushing it out. */
4855 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
4857 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
4864 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4865 so we notice when children change state; as the handler for the
4866 sigsuspend in my_waitpid. */
4869 sigchld_handler (int signo
)
4871 int old_errno
= errno
;
4877 /* fprintf is not async-signal-safe, so call write
4879 if (write (2, "sigchld_handler\n",
4880 sizeof ("sigchld_handler\n") - 1) < 0)
4881 break; /* just ignore */
4885 if (target_is_async_p ())
4886 async_file_mark (); /* trigger a linux_wait */
4892 linux_supports_non_stop (void)
4898 linux_async (int enable
)
4900 int previous
= (linux_event_pipe
[0] != -1);
4903 debug_printf ("linux_async (%d), previous=%d\n",
4906 if (previous
!= enable
)
4909 sigemptyset (&mask
);
4910 sigaddset (&mask
, SIGCHLD
);
4912 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4916 if (pipe (linux_event_pipe
) == -1)
4917 fatal ("creating event pipe failed.");
4919 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4920 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4922 /* Register the event loop handler. */
4923 add_file_handler (linux_event_pipe
[0],
4924 handle_target_event
, NULL
);
4926 /* Always trigger a linux_wait. */
4931 delete_file_handler (linux_event_pipe
[0]);
4933 close (linux_event_pipe
[0]);
4934 close (linux_event_pipe
[1]);
4935 linux_event_pipe
[0] = -1;
4936 linux_event_pipe
[1] = -1;
4939 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
4946 linux_start_non_stop (int nonstop
)
4948 /* Register or unregister from event-loop accordingly. */
4949 linux_async (nonstop
);
4954 linux_supports_multi_process (void)
4960 linux_supports_disable_randomization (void)
4962 #ifdef HAVE_PERSONALITY
4970 linux_supports_agent (void)
4976 linux_supports_range_stepping (void)
4978 if (*the_low_target
.supports_range_stepping
== NULL
)
4981 return (*the_low_target
.supports_range_stepping
) ();
4984 /* Enumerate spufs IDs for process PID. */
4986 spu_enumerate_spu_ids (long pid
, unsigned char *buf
, CORE_ADDR offset
, int len
)
4992 struct dirent
*entry
;
4994 sprintf (path
, "/proc/%ld/fd", pid
);
4995 dir
= opendir (path
);
5000 while ((entry
= readdir (dir
)) != NULL
)
5006 fd
= atoi (entry
->d_name
);
5010 sprintf (path
, "/proc/%ld/fd/%d", pid
, fd
);
5011 if (stat (path
, &st
) != 0)
5013 if (!S_ISDIR (st
.st_mode
))
5016 if (statfs (path
, &stfs
) != 0)
5018 if (stfs
.f_type
!= SPUFS_MAGIC
)
5021 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
5023 *(unsigned int *)(buf
+ pos
- offset
) = fd
;
5033 /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
5034 object type, using the /proc file system. */
5036 linux_qxfer_spu (const char *annex
, unsigned char *readbuf
,
5037 unsigned const char *writebuf
,
5038 CORE_ADDR offset
, int len
)
5040 long pid
= lwpid_of (current_inferior
);
5045 if (!writebuf
&& !readbuf
)
5053 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
5056 sprintf (buf
, "/proc/%ld/fd/%s", pid
, annex
);
5057 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
5062 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5069 ret
= write (fd
, writebuf
, (size_t) len
);
5071 ret
= read (fd
, readbuf
, (size_t) len
);
5077 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
5078 struct target_loadseg
5080 /* Core address to which the segment is mapped. */
5082 /* VMA recorded in the program header. */
5084 /* Size of this segment in memory. */
5088 # if defined PT_GETDSBT
5089 struct target_loadmap
5091 /* Protocol version number, must be zero. */
5093 /* Pointer to the DSBT table, its size, and the DSBT index. */
5094 unsigned *dsbt_table
;
5095 unsigned dsbt_size
, dsbt_index
;
5096 /* Number of segments in this map. */
5098 /* The actual memory map. */
5099 struct target_loadseg segs
[/*nsegs*/];
5101 # define LINUX_LOADMAP PT_GETDSBT
5102 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
5103 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
5105 struct target_loadmap
5107 /* Protocol version number, must be zero. */
5109 /* Number of segments in this map. */
5111 /* The actual memory map. */
5112 struct target_loadseg segs
[/*nsegs*/];
5114 # define LINUX_LOADMAP PTRACE_GETFDPIC
5115 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
5116 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
5120 linux_read_loadmap (const char *annex
, CORE_ADDR offset
,
5121 unsigned char *myaddr
, unsigned int len
)
5123 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
5125 struct target_loadmap
*data
= NULL
;
5126 unsigned int actual_length
, copy_length
;
5128 if (strcmp (annex
, "exec") == 0)
5129 addr
= (int) LINUX_LOADMAP_EXEC
;
5130 else if (strcmp (annex
, "interp") == 0)
5131 addr
= (int) LINUX_LOADMAP_INTERP
;
5135 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
5141 actual_length
= sizeof (struct target_loadmap
)
5142 + sizeof (struct target_loadseg
) * data
->nsegs
;
5144 if (offset
< 0 || offset
> actual_length
)
5147 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
5148 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
5152 # define linux_read_loadmap NULL
5153 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
5156 linux_process_qsupported (const char *query
)
5158 if (the_low_target
.process_qsupported
!= NULL
)
5159 the_low_target
.process_qsupported (query
);
5163 linux_supports_tracepoints (void)
5165 if (*the_low_target
.supports_tracepoints
== NULL
)
5168 return (*the_low_target
.supports_tracepoints
) ();
5172 linux_read_pc (struct regcache
*regcache
)
5174 if (the_low_target
.get_pc
== NULL
)
5177 return (*the_low_target
.get_pc
) (regcache
);
5181 linux_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
5183 gdb_assert (the_low_target
.set_pc
!= NULL
);
5185 (*the_low_target
.set_pc
) (regcache
, pc
);
5189 linux_thread_stopped (struct thread_info
*thread
)
5191 return get_thread_lwp (thread
)->stopped
;
5194 /* This exposes stop-all-threads functionality to other modules. */
5197 linux_pause_all (int freeze
)
5199 stop_all_lwps (freeze
, NULL
);
5202 /* This exposes unstop-all-threads functionality to other gdbserver
5206 linux_unpause_all (int unfreeze
)
5208 unstop_all_lwps (unfreeze
, NULL
);
5212 linux_prepare_to_access_memory (void)
5214 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5217 linux_pause_all (1);
5222 linux_done_accessing_memory (void)
5224 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5227 linux_unpause_all (1);
5231 linux_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
, CORE_ADDR tpaddr
,
5232 CORE_ADDR collector
,
5235 CORE_ADDR
*jump_entry
,
5236 CORE_ADDR
*trampoline
,
5237 ULONGEST
*trampoline_size
,
5238 unsigned char *jjump_pad_insn
,
5239 ULONGEST
*jjump_pad_insn_size
,
5240 CORE_ADDR
*adjusted_insn_addr
,
5241 CORE_ADDR
*adjusted_insn_addr_end
,
5244 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
5245 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
5246 jump_entry
, trampoline
, trampoline_size
,
5247 jjump_pad_insn
, jjump_pad_insn_size
,
5248 adjusted_insn_addr
, adjusted_insn_addr_end
,
5252 static struct emit_ops
*
5253 linux_emit_ops (void)
5255 if (the_low_target
.emit_ops
!= NULL
)
5256 return (*the_low_target
.emit_ops
) ();
5262 linux_get_min_fast_tracepoint_insn_len (void)
5264 return (*the_low_target
.get_min_fast_tracepoint_insn_len
) ();
5267 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
5270 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
5271 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
5273 char filename
[PATH_MAX
];
5275 const int auxv_size
= is_elf64
5276 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
5277 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
5279 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5281 fd
= open (filename
, O_RDONLY
);
5287 while (read (fd
, buf
, auxv_size
) == auxv_size
5288 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
5292 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
5294 switch (aux
->a_type
)
5297 *phdr_memaddr
= aux
->a_un
.a_val
;
5300 *num_phdr
= aux
->a_un
.a_val
;
5306 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
5308 switch (aux
->a_type
)
5311 *phdr_memaddr
= aux
->a_un
.a_val
;
5314 *num_phdr
= aux
->a_un
.a_val
;
5322 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
5324 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
5325 "phdr_memaddr = %ld, phdr_num = %d",
5326 (long) *phdr_memaddr
, *num_phdr
);
5333 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
5336 get_dynamic (const int pid
, const int is_elf64
)
5338 CORE_ADDR phdr_memaddr
, relocation
;
5340 unsigned char *phdr_buf
;
5341 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
5343 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
5346 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
5347 phdr_buf
= alloca (num_phdr
* phdr_size
);
5349 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
5352 /* Compute relocation: it is expected to be 0 for "regular" executables,
5353 non-zero for PIE ones. */
5355 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
5358 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5360 if (p
->p_type
== PT_PHDR
)
5361 relocation
= phdr_memaddr
- p
->p_vaddr
;
5365 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5367 if (p
->p_type
== PT_PHDR
)
5368 relocation
= phdr_memaddr
- p
->p_vaddr
;
5371 if (relocation
== -1)
5373 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
5374 any real world executables, including PIE executables, have always
5375 PT_PHDR present. PT_PHDR is not present in some shared libraries or
5376 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
5377 or present DT_DEBUG anyway (fpc binaries are statically linked).
5379 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
5381 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
5386 for (i
= 0; i
< num_phdr
; i
++)
5390 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5392 if (p
->p_type
== PT_DYNAMIC
)
5393 return p
->p_vaddr
+ relocation
;
5397 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5399 if (p
->p_type
== PT_DYNAMIC
)
5400 return p
->p_vaddr
+ relocation
;
5407 /* Return &_r_debug in the inferior, or -1 if not present. Return value
5408 can be 0 if the inferior does not yet have the library list initialized.
5409 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
5410 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
5413 get_r_debug (const int pid
, const int is_elf64
)
5415 CORE_ADDR dynamic_memaddr
;
5416 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
5417 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
5420 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
5421 if (dynamic_memaddr
== 0)
5424 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
5428 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
5429 #ifdef DT_MIPS_RLD_MAP
5433 unsigned char buf
[sizeof (Elf64_Xword
)];
5437 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
5439 if (linux_read_memory (dyn
->d_un
.d_val
,
5440 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
5445 #endif /* DT_MIPS_RLD_MAP */
5447 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
5448 map
= dyn
->d_un
.d_val
;
5450 if (dyn
->d_tag
== DT_NULL
)
5455 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
5456 #ifdef DT_MIPS_RLD_MAP
5460 unsigned char buf
[sizeof (Elf32_Word
)];
5464 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
5466 if (linux_read_memory (dyn
->d_un
.d_val
,
5467 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
5472 #endif /* DT_MIPS_RLD_MAP */
5474 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
5475 map
= dyn
->d_un
.d_val
;
5477 if (dyn
->d_tag
== DT_NULL
)
5481 dynamic_memaddr
+= dyn_size
;
5487 /* Read one pointer from MEMADDR in the inferior. */
5490 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
5494 /* Go through a union so this works on either big or little endian
5495 hosts, when the inferior's pointer size is smaller than the size
5496 of CORE_ADDR. It is assumed the inferior's endianness is the
5497 same of the superior's. */
5500 CORE_ADDR core_addr
;
5505 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
5508 if (ptr_size
== sizeof (CORE_ADDR
))
5509 *ptr
= addr
.core_addr
;
5510 else if (ptr_size
== sizeof (unsigned int))
5513 gdb_assert_not_reached ("unhandled pointer size");
5518 struct link_map_offsets
5520 /* Offset and size of r_debug.r_version. */
5521 int r_version_offset
;
5523 /* Offset and size of r_debug.r_map. */
5526 /* Offset to l_addr field in struct link_map. */
5529 /* Offset to l_name field in struct link_map. */
5532 /* Offset to l_ld field in struct link_map. */
5535 /* Offset to l_next field in struct link_map. */
5538 /* Offset to l_prev field in struct link_map. */
5542 /* Construct qXfer:libraries-svr4:read reply. */
5545 linux_qxfer_libraries_svr4 (const char *annex
, unsigned char *readbuf
,
5546 unsigned const char *writebuf
,
5547 CORE_ADDR offset
, int len
)
5550 unsigned document_len
;
5551 struct process_info_private
*const priv
= current_process ()->private;
5552 char filename
[PATH_MAX
];
5555 static const struct link_map_offsets lmo_32bit_offsets
=
5557 0, /* r_version offset. */
5558 4, /* r_debug.r_map offset. */
5559 0, /* l_addr offset in link_map. */
5560 4, /* l_name offset in link_map. */
5561 8, /* l_ld offset in link_map. */
5562 12, /* l_next offset in link_map. */
5563 16 /* l_prev offset in link_map. */
5566 static const struct link_map_offsets lmo_64bit_offsets
=
5568 0, /* r_version offset. */
5569 8, /* r_debug.r_map offset. */
5570 0, /* l_addr offset in link_map. */
5571 8, /* l_name offset in link_map. */
5572 16, /* l_ld offset in link_map. */
5573 24, /* l_next offset in link_map. */
5574 32 /* l_prev offset in link_map. */
5576 const struct link_map_offsets
*lmo
;
5577 unsigned int machine
;
5579 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
5580 int allocated
= 1024;
5582 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
5583 int header_done
= 0;
5585 if (writebuf
!= NULL
)
5587 if (readbuf
== NULL
)
5590 pid
= lwpid_of (current_inferior
);
5591 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
5592 is_elf64
= elf_64_file_p (filename
, &machine
);
5593 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
5594 ptr_size
= is_elf64
? 8 : 4;
5596 while (annex
[0] != '\0')
5602 sep
= strchr (annex
, '=');
5607 if (len
== 5 && strncmp (annex
, "start", 5) == 0)
5609 else if (len
== 4 && strncmp (annex
, "prev", 4) == 0)
5613 annex
= strchr (sep
, ';');
5620 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
5627 if (priv
->r_debug
== 0)
5628 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
5630 /* We failed to find DT_DEBUG. Such situation will not change
5631 for this inferior - do not retry it. Report it to GDB as
5632 E01, see for the reasons at the GDB solib-svr4.c side. */
5633 if (priv
->r_debug
== (CORE_ADDR
) -1)
5636 if (priv
->r_debug
!= 0)
5638 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
5639 (unsigned char *) &r_version
,
5640 sizeof (r_version
)) != 0
5643 warning ("unexpected r_debug version %d", r_version
);
5645 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
5646 &lm_addr
, ptr_size
) != 0)
5648 warning ("unable to read r_map from 0x%lx",
5649 (long) priv
->r_debug
+ lmo
->r_map_offset
);
5654 document
= xmalloc (allocated
);
5655 strcpy (document
, "<library-list-svr4 version=\"1.0\"");
5656 p
= document
+ strlen (document
);
5659 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
5660 &l_name
, ptr_size
) == 0
5661 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
5662 &l_addr
, ptr_size
) == 0
5663 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
5664 &l_ld
, ptr_size
) == 0
5665 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
5666 &l_prev
, ptr_size
) == 0
5667 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
5668 &l_next
, ptr_size
) == 0)
5670 unsigned char libname
[PATH_MAX
];
5672 if (lm_prev
!= l_prev
)
5674 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
5675 (long) lm_prev
, (long) l_prev
);
5679 /* Ignore the first entry even if it has valid name as the first entry
5680 corresponds to the main executable. The first entry should not be
5681 skipped if the dynamic loader was loaded late by a static executable
5682 (see solib-svr4.c parameter ignore_first). But in such case the main
5683 executable does not have PT_DYNAMIC present and this function already
5684 exited above due to failed get_r_debug. */
5687 sprintf (p
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
5692 /* Not checking for error because reading may stop before
5693 we've got PATH_MAX worth of characters. */
5695 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
5696 libname
[sizeof (libname
) - 1] = '\0';
5697 if (libname
[0] != '\0')
5699 /* 6x the size for xml_escape_text below. */
5700 size_t len
= 6 * strlen ((char *) libname
);
5705 /* Terminate `<library-list-svr4'. */
5710 while (allocated
< p
- document
+ len
+ 200)
5712 /* Expand to guarantee sufficient storage. */
5713 uintptr_t document_len
= p
- document
;
5715 document
= xrealloc (document
, 2 * allocated
);
5717 p
= document
+ document_len
;
5720 name
= xml_escape_text ((char *) libname
);
5721 p
+= sprintf (p
, "<library name=\"%s\" lm=\"0x%lx\" "
5722 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
5723 name
, (unsigned long) lm_addr
,
5724 (unsigned long) l_addr
, (unsigned long) l_ld
);
5735 /* Empty list; terminate `<library-list-svr4'. */
5739 strcpy (p
, "</library-list-svr4>");
5741 document_len
= strlen (document
);
5742 if (offset
< document_len
)
5743 document_len
-= offset
;
5746 if (len
> document_len
)
5749 memcpy (readbuf
, document
+ offset
, len
);
5755 #ifdef HAVE_LINUX_BTRACE
5757 /* See to_enable_btrace target method. */
5759 static struct btrace_target_info
*
5760 linux_low_enable_btrace (ptid_t ptid
)
5762 struct btrace_target_info
*tinfo
;
5764 tinfo
= linux_enable_btrace (ptid
);
5768 struct thread_info
*thread
= find_thread_ptid (ptid
);
5769 struct regcache
*regcache
= get_thread_regcache (thread
, 0);
5771 tinfo
->ptr_bits
= register_size (regcache
->tdesc
, 0) * 8;
5777 /* See to_disable_btrace target method. */
5780 linux_low_disable_btrace (struct btrace_target_info
*tinfo
)
5782 enum btrace_error err
;
5784 err
= linux_disable_btrace (tinfo
);
5785 return (err
== BTRACE_ERR_NONE
? 0 : -1);
5788 /* See to_read_btrace target method. */
5791 linux_low_read_btrace (struct btrace_target_info
*tinfo
, struct buffer
*buffer
,
5794 VEC (btrace_block_s
) *btrace
;
5795 struct btrace_block
*block
;
5796 enum btrace_error err
;
5800 err
= linux_read_btrace (&btrace
, tinfo
, type
);
5801 if (err
!= BTRACE_ERR_NONE
)
5803 if (err
== BTRACE_ERR_OVERFLOW
)
5804 buffer_grow_str0 (buffer
, "E.Overflow.");
5806 buffer_grow_str0 (buffer
, "E.Generic Error.");
5811 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
5812 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
5814 for (i
= 0; VEC_iterate (btrace_block_s
, btrace
, i
, block
); i
++)
5815 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
5816 paddress (block
->begin
), paddress (block
->end
));
5818 buffer_grow_str0 (buffer
, "</btrace>\n");
5820 VEC_free (btrace_block_s
, btrace
);
5824 #endif /* HAVE_LINUX_BTRACE */
5826 static struct target_ops linux_target_ops
= {
5827 linux_create_inferior
,
5836 linux_fetch_registers
,
5837 linux_store_registers
,
5838 linux_prepare_to_access_memory
,
5839 linux_done_accessing_memory
,
5842 linux_look_up_symbols
,
5843 linux_request_interrupt
,
5847 linux_stopped_by_watchpoint
,
5848 linux_stopped_data_address
,
5849 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
5850 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
5851 && defined(PT_TEXT_END_ADDR)
5856 #ifdef USE_THREAD_DB
5857 thread_db_get_tls_address
,
5862 hostio_last_error_from_errno
,
5865 linux_supports_non_stop
,
5867 linux_start_non_stop
,
5868 linux_supports_multi_process
,
5869 #ifdef USE_THREAD_DB
5870 thread_db_handle_monitor_command
,
5874 linux_common_core_of_thread
,
5876 linux_process_qsupported
,
5877 linux_supports_tracepoints
,
5880 linux_thread_stopped
,
5884 linux_cancel_breakpoints
,
5885 linux_stabilize_threads
,
5886 linux_install_fast_tracepoint_jump_pad
,
5888 linux_supports_disable_randomization
,
5889 linux_get_min_fast_tracepoint_insn_len
,
5890 linux_qxfer_libraries_svr4
,
5891 linux_supports_agent
,
5892 #ifdef HAVE_LINUX_BTRACE
5893 linux_supports_btrace
,
5894 linux_low_enable_btrace
,
5895 linux_low_disable_btrace
,
5896 linux_low_read_btrace
,
5903 linux_supports_range_stepping
,
5907 linux_init_signals ()
5909 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
5910 to find what the cancel signal actually is. */
5911 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
5912 signal (__SIGRTMIN
+1, SIG_IGN
);
5916 #ifdef HAVE_LINUX_REGSETS
5918 initialize_regsets_info (struct regsets_info
*info
)
5920 for (info
->num_regsets
= 0;
5921 info
->regsets
[info
->num_regsets
].size
>= 0;
5922 info
->num_regsets
++)
5928 initialize_low (void)
5930 struct sigaction sigchld_action
;
5931 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
5932 set_target_ops (&linux_target_ops
);
5933 set_breakpoint_data (the_low_target
.breakpoint
,
5934 the_low_target
.breakpoint_len
);
5935 linux_init_signals ();
5936 linux_ptrace_init_warnings ();
5938 sigchld_action
.sa_handler
= sigchld_handler
;
5939 sigemptyset (&sigchld_action
.sa_mask
);
5940 sigchld_action
.sa_flags
= SA_RESTART
;
5941 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
5943 initialize_low_arch ();