1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "linux-low.h"
22 #include "ansidecl.h" /* For ATTRIBUTE_PACKED, must be bug in external.h. */
23 #include "elf/common.h"
24 #include "elf/external.h"
28 #include <sys/param.h>
29 #include <sys/ptrace.h>
31 #include <sys/ioctl.h>
37 #include <sys/syscall.h>
41 #include <sys/types.h>
44 #ifndef PTRACE_GETSIGINFO
45 # define PTRACE_GETSIGINFO 0x4202
46 # define PTRACE_SETSIGINFO 0x4203
53 /* If the system headers did not provide the constants, hard-code the normal
55 #ifndef PTRACE_EVENT_FORK
57 #define PTRACE_SETOPTIONS 0x4200
58 #define PTRACE_GETEVENTMSG 0x4201
60 /* options set using PTRACE_SETOPTIONS */
61 #define PTRACE_O_TRACESYSGOOD 0x00000001
62 #define PTRACE_O_TRACEFORK 0x00000002
63 #define PTRACE_O_TRACEVFORK 0x00000004
64 #define PTRACE_O_TRACECLONE 0x00000008
65 #define PTRACE_O_TRACEEXEC 0x00000010
66 #define PTRACE_O_TRACEVFORKDONE 0x00000020
67 #define PTRACE_O_TRACEEXIT 0x00000040
69 /* Wait extended result codes for the above trace options. */
70 #define PTRACE_EVENT_FORK 1
71 #define PTRACE_EVENT_VFORK 2
72 #define PTRACE_EVENT_CLONE 3
73 #define PTRACE_EVENT_EXEC 4
74 #define PTRACE_EVENT_VFORK_DONE 5
75 #define PTRACE_EVENT_EXIT 6
77 #endif /* PTRACE_EVENT_FORK */
79 /* We can't always assume that this flag is available, but all systems
80 with the ptrace event handlers also have __WALL, so it's safe to use
83 #define __WALL 0x40000000 /* Wait for any child. */
87 #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
92 /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
93 representation of the thread ID.
95 ``all_lwps'' is keyed by the process ID - which on Linux is (presently)
96 the same as the LWP ID.
98 ``all_processes'' is keyed by the "overall process ID", which
99 GNU/Linux calls tgid, "thread group ID". */
101 struct inferior_list all_lwps
;
103 /* A list of all unknown processes which receive stop signals. Some other
104 process will presumably claim each of these as forked children
107 struct inferior_list stopped_pids
;
109 /* FIXME this is a bit of a hack, and could be removed. */
110 int stopping_threads
;
112 /* FIXME make into a target method? */
113 int using_threads
= 1;
115 /* This flag is true iff we've just created or attached to our first
116 inferior but it has not stopped yet. As soon as it does, we need
117 to call the low target's arch_setup callback. Doing this only on
118 the first inferior avoids reinializing the architecture on every
119 inferior, and avoids messing with the register caches of the
120 already running inferiors. NOTE: this assumes all inferiors under
121 control of gdbserver have the same architecture. */
122 static int new_inferior
;
124 static void linux_resume_one_lwp (struct lwp_info
*lwp
,
125 int step
, int signal
, siginfo_t
*info
);
126 static void linux_resume (struct thread_resume
*resume_info
, size_t n
);
127 static void stop_all_lwps (void);
128 static int linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
);
129 static int check_removed_breakpoint (struct lwp_info
*event_child
);
130 static void *add_lwp (ptid_t ptid
);
131 static int my_waitpid (int pid
, int *status
, int flags
);
132 static int linux_stopped_by_watchpoint (void);
133 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
135 struct pending_signals
139 struct pending_signals
*prev
;
142 #define PTRACE_ARG3_TYPE long
143 #define PTRACE_XFER_TYPE long
145 #ifdef HAVE_LINUX_REGSETS
146 static char *disabled_regsets
;
147 static int num_regsets
;
150 /* The read/write ends of the pipe registered as waitable file in the
152 static int linux_event_pipe
[2] = { -1, -1 };
154 /* True if we're currently in async mode. */
155 #define target_is_async_p() (linux_event_pipe[0] != -1)
157 static void send_sigstop (struct inferior_list_entry
*entry
);
158 static void wait_for_sigstop (struct inferior_list_entry
*entry
);
160 /* Accepts an integer PID; Returns a string representing a file that
161 can be opened to get info for the child process.
162 Space for the result is malloc'd, caller must free. */
165 linux_child_pid_to_exec_file (int pid
)
169 name1
= xmalloc (MAXPATHLEN
);
170 name2
= xmalloc (MAXPATHLEN
);
171 memset (name2
, 0, MAXPATHLEN
);
173 sprintf (name1
, "/proc/%d/exe", pid
);
174 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
186 /* Return non-zero if HEADER is a 64-bit ELF file. */
189 elf_64_header_p (const Elf64_External_Ehdr
*header
)
191 return (header
->e_ident
[EI_MAG0
] == ELFMAG0
192 && header
->e_ident
[EI_MAG1
] == ELFMAG1
193 && header
->e_ident
[EI_MAG2
] == ELFMAG2
194 && header
->e_ident
[EI_MAG3
] == ELFMAG3
195 && header
->e_ident
[EI_CLASS
] == ELFCLASS64
);
198 /* Return non-zero if FILE is a 64-bit ELF file,
199 zero if the file is not a 64-bit ELF file,
200 and -1 if the file is not accessible or doesn't exist. */
203 elf_64_file_p (const char *file
)
205 Elf64_External_Ehdr header
;
208 fd
= open (file
, O_RDONLY
);
212 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
219 return elf_64_header_p (&header
);
223 delete_lwp (struct lwp_info
*lwp
)
225 remove_thread (get_lwp_thread (lwp
));
226 remove_inferior (&all_lwps
, &lwp
->head
);
230 /* Add a process to the common process list, and set its private
233 static struct process_info
*
234 linux_add_process (int pid
, int attached
)
236 struct process_info
*proc
;
238 /* Is this the first process? If so, then set the arch. */
239 if (all_processes
.head
== NULL
)
242 proc
= add_process (pid
, attached
);
243 proc
->private = xcalloc (1, sizeof (*proc
->private));
248 /* Remove a process from the common process list,
249 also freeing all private data. */
252 linux_remove_process (struct process_info
*process
)
254 free (process
->private);
255 remove_process (process
);
258 /* Handle a GNU/Linux extended wait response. If we see a clone
259 event, we need to add the new LWP to our list (and not report the
260 trap to higher layers). */
263 handle_extended_wait (struct lwp_info
*event_child
, int wstat
)
265 int event
= wstat
>> 16;
266 struct lwp_info
*new_lwp
;
268 if (event
== PTRACE_EVENT_CLONE
)
271 unsigned long new_pid
;
272 int ret
, status
= W_STOPCODE (SIGSTOP
);
274 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_child
), 0, &new_pid
);
276 /* If we haven't already seen the new PID stop, wait for it now. */
277 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
279 /* The new child has a pending SIGSTOP. We can't affect it until it
280 hits the SIGSTOP, but we're already attached. */
282 ret
= my_waitpid (new_pid
, &status
, __WALL
);
285 perror_with_name ("waiting for new child");
286 else if (ret
!= new_pid
)
287 warning ("wait returned unexpected PID %d", ret
);
288 else if (!WIFSTOPPED (status
))
289 warning ("wait returned unexpected status 0x%x", status
);
292 ptrace (PTRACE_SETOPTIONS
, new_pid
, 0, PTRACE_O_TRACECLONE
);
294 ptid
= ptid_build (pid_of (event_child
), new_pid
, 0);
295 new_lwp
= (struct lwp_info
*) add_lwp (ptid
);
296 add_thread (ptid
, new_lwp
);
298 /* Either we're going to immediately resume the new thread
299 or leave it stopped. linux_resume_one_lwp is a nop if it
300 thinks the thread is currently running, so set this first
301 before calling linux_resume_one_lwp. */
302 new_lwp
->stopped
= 1;
304 /* Normally we will get the pending SIGSTOP. But in some cases
305 we might get another signal delivered to the group first.
306 If we do get another signal, be sure not to lose it. */
307 if (WSTOPSIG (status
) == SIGSTOP
)
309 if (! stopping_threads
)
310 linux_resume_one_lwp (new_lwp
, 0, 0, NULL
);
314 new_lwp
->stop_expected
= 1;
315 if (stopping_threads
)
317 new_lwp
->status_pending_p
= 1;
318 new_lwp
->status_pending
= status
;
321 /* Pass the signal on. This is what GDB does - except
322 shouldn't we really report it instead? */
323 linux_resume_one_lwp (new_lwp
, 0, WSTOPSIG (status
), NULL
);
326 /* Always resume the current thread. If we are stopping
327 threads, it will have a pending SIGSTOP; we may as well
329 linux_resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
333 /* This function should only be called if the process got a SIGTRAP.
334 The SIGTRAP could mean several things.
336 On i386, where decr_pc_after_break is non-zero:
337 If we were single-stepping this process using PTRACE_SINGLESTEP,
338 we will get only the one SIGTRAP (even if the instruction we
339 stepped over was a breakpoint). The value of $eip will be the
341 If we continue the process using PTRACE_CONT, we will get a
342 SIGTRAP when we hit a breakpoint. The value of $eip will be
343 the instruction after the breakpoint (i.e. needs to be
344 decremented). If we report the SIGTRAP to GDB, we must also
345 report the undecremented PC. If we cancel the SIGTRAP, we
346 must resume at the decremented PC.
348 (Presumably, not yet tested) On a non-decr_pc_after_break machine
349 with hardware or kernel single-step:
350 If we single-step over a breakpoint instruction, our PC will
351 point at the following instruction. If we continue and hit a
352 breakpoint instruction, our PC will point at the breakpoint
358 CORE_ADDR stop_pc
= (*the_low_target
.get_pc
) ();
360 if (! get_thread_lwp (current_inferior
)->stepping
)
361 stop_pc
-= the_low_target
.decr_pc_after_break
;
364 fprintf (stderr
, "stop pc is 0x%lx\n", (long) stop_pc
);
370 add_lwp (ptid_t ptid
)
372 struct lwp_info
*lwp
;
374 lwp
= (struct lwp_info
*) xmalloc (sizeof (*lwp
));
375 memset (lwp
, 0, sizeof (*lwp
));
379 add_inferior_to_list (&all_lwps
, &lwp
->head
);
384 /* Start an inferior process and returns its pid.
385 ALLARGS is a vector of program-name and args. */
388 linux_create_inferior (char *program
, char **allargs
)
390 struct lwp_info
*new_lwp
;
394 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
400 perror_with_name ("fork");
404 ptrace (PTRACE_TRACEME
, 0, 0, 0);
406 signal (__SIGRTMIN
+ 1, SIG_DFL
);
410 execv (program
, allargs
);
412 execvp (program
, allargs
);
414 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
420 linux_add_process (pid
, 0);
422 ptid
= ptid_build (pid
, pid
, 0);
423 new_lwp
= add_lwp (ptid
);
424 add_thread (ptid
, new_lwp
);
425 new_lwp
->must_set_ptrace_flags
= 1;
430 /* Attach to an inferior process. */
433 linux_attach_lwp_1 (unsigned long lwpid
, int initial
)
436 struct lwp_info
*new_lwp
;
438 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) != 0)
442 /* If we fail to attach to an LWP, just warn. */
443 fprintf (stderr
, "Cannot attach to lwp %ld: %s (%d)\n", lwpid
,
444 strerror (errno
), errno
);
449 /* If we fail to attach to a process, report an error. */
450 error ("Cannot attach to lwp %ld: %s (%d)\n", lwpid
,
451 strerror (errno
), errno
);
455 /* NOTE/FIXME: This lwp might have not been the tgid. */
456 ptid
= ptid_build (lwpid
, lwpid
, 0);
459 /* Note that extracting the pid from the current inferior is
460 safe, since we're always called in the context of the same
461 process as this new thread. */
462 int pid
= pid_of (get_thread_lwp (current_inferior
));
463 ptid
= ptid_build (pid
, lwpid
, 0);
466 new_lwp
= (struct lwp_info
*) add_lwp (ptid
);
467 add_thread (ptid
, new_lwp
);
470 /* We need to wait for SIGSTOP before being able to make the next
471 ptrace call on this LWP. */
472 new_lwp
->must_set_ptrace_flags
= 1;
474 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
477 There are several cases to consider here:
479 1) gdbserver has already attached to the process and is being notified
480 of a new thread that is being created.
481 In this case we should ignore that SIGSTOP and resume the process.
482 This is handled below by setting stop_expected = 1.
484 2) This is the first thread (the process thread), and we're attaching
485 to it via attach_inferior.
486 In this case we want the process thread to stop.
487 This is handled by having linux_attach clear stop_expected after
489 ??? If the process already has several threads we leave the other
492 3) GDB is connecting to gdbserver and is requesting an enumeration of all
494 In this case we want the thread to stop.
495 FIXME: This case is currently not properly handled.
496 We should wait for the SIGSTOP but don't. Things work apparently
497 because enough time passes between when we ptrace (ATTACH) and when
498 gdb makes the next ptrace call on the thread.
500 On the other hand, if we are currently trying to stop all threads, we
501 should treat the new thread as if we had sent it a SIGSTOP. This works
502 because we are guaranteed that the add_lwp call above added us to the
503 end of the list, and so the new thread has not yet reached
504 wait_for_sigstop (but will). */
505 if (! stopping_threads
)
506 new_lwp
->stop_expected
= 1;
510 linux_attach_lwp (unsigned long lwpid
)
512 linux_attach_lwp_1 (lwpid
, 0);
516 linux_attach (unsigned long pid
)
518 struct lwp_info
*lwp
;
520 linux_attach_lwp_1 (pid
, 1);
522 linux_add_process (pid
, 1);
526 /* Don't ignore the initial SIGSTOP if we just attached to this
527 process. It will be collected by wait shortly. */
528 lwp
= (struct lwp_info
*) find_inferior_id (&all_lwps
,
529 ptid_build (pid
, pid
, 0));
530 lwp
->stop_expected
= 0;
543 second_thread_of_pid_p (struct inferior_list_entry
*entry
, void *args
)
545 struct counter
*counter
= args
;
547 if (ptid_get_pid (entry
->id
) == counter
->pid
)
549 if (++counter
->count
> 1)
557 last_thread_of_process_p (struct thread_info
*thread
)
559 ptid_t ptid
= ((struct inferior_list_entry
*)thread
)->id
;
560 int pid
= ptid_get_pid (ptid
);
561 struct counter counter
= { pid
, 0 };
563 return (find_inferior (&all_threads
,
564 second_thread_of_pid_p
, &counter
) == NULL
);
567 /* Kill the inferior lwp. */
570 linux_kill_one_lwp (struct inferior_list_entry
*entry
, void *args
)
572 struct thread_info
*thread
= (struct thread_info
*) entry
;
573 struct lwp_info
*lwp
= get_thread_lwp (thread
);
575 int pid
= * (int *) args
;
577 if (ptid_get_pid (entry
->id
) != pid
)
580 /* We avoid killing the first thread here, because of a Linux kernel (at
581 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
582 the children get a chance to be reaped, it will remain a zombie
585 if (last_thread_of_process_p (thread
))
588 fprintf (stderr
, "lkop: is last of process %s\n",
589 target_pid_to_str (entry
->id
));
593 /* If we're killing a running inferior, make sure it is stopped
594 first, as PTRACE_KILL will not work otherwise. */
596 send_sigstop (&lwp
->head
);
600 ptrace (PTRACE_KILL
, lwpid_of (lwp
), 0, 0);
602 /* Make sure it died. The loop is most likely unnecessary. */
603 pid
= linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
604 } while (pid
> 0 && WIFSTOPPED (wstat
));
612 struct process_info
*process
;
613 struct lwp_info
*lwp
;
614 struct thread_info
*thread
;
618 process
= find_process_pid (pid
);
622 find_inferior (&all_threads
, linux_kill_one_lwp
, &pid
);
624 /* See the comment in linux_kill_one_lwp. We did not kill the first
625 thread in the list, so do so now. */
626 lwp
= find_lwp_pid (pid_to_ptid (pid
));
627 thread
= get_lwp_thread (lwp
);
630 fprintf (stderr
, "lk_1: killing lwp %ld, for pid: %d\n",
631 lwpid_of (lwp
), pid
);
633 /* If we're killing a running inferior, make sure it is stopped
634 first, as PTRACE_KILL will not work otherwise. */
636 send_sigstop (&lwp
->head
);
640 ptrace (PTRACE_KILL
, lwpid_of (lwp
), 0, 0);
642 /* Make sure it died. The loop is most likely unnecessary. */
643 lwpid
= linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
644 } while (lwpid
> 0 && WIFSTOPPED (wstat
));
647 linux_remove_process (process
);
652 linux_detach_one_lwp (struct inferior_list_entry
*entry
, void *args
)
654 struct thread_info
*thread
= (struct thread_info
*) entry
;
655 struct lwp_info
*lwp
= get_thread_lwp (thread
);
656 int pid
= * (int *) args
;
658 if (ptid_get_pid (entry
->id
) != pid
)
661 /* If we're detaching from a running inferior, make sure it is
662 stopped first, as PTRACE_DETACH will not work otherwise. */
665 int lwpid
= lwpid_of (lwp
);
667 stopping_threads
= 1;
668 send_sigstop (&lwp
->head
);
670 /* If this detects a new thread through a clone event, the new
671 thread is appended to the end of the lwp list, so we'll
672 eventually detach from it. */
673 wait_for_sigstop (&lwp
->head
);
674 stopping_threads
= 0;
676 /* If LWP exits while we're trying to stop it, there's nothing
678 lwp
= find_lwp_pid (pid_to_ptid (lwpid
));
683 /* Make sure the process isn't stopped at a breakpoint that's
685 check_removed_breakpoint (lwp
);
687 /* If this process is stopped but is expecting a SIGSTOP, then make
688 sure we take care of that now. This isn't absolutely guaranteed
689 to collect the SIGSTOP, but is fairly likely to. */
690 if (lwp
->stop_expected
)
693 /* Clear stop_expected, so that the SIGSTOP will be reported. */
694 lwp
->stop_expected
= 0;
696 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
697 linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
700 /* Flush any pending changes to the process's registers. */
701 regcache_invalidate_one ((struct inferior_list_entry
*)
702 get_lwp_thread (lwp
));
704 /* Finally, let it resume. */
705 ptrace (PTRACE_DETACH
, lwpid_of (lwp
), 0, 0);
712 any_thread_of (struct inferior_list_entry
*entry
, void *args
)
716 if (ptid_get_pid (entry
->id
) == *pid_p
)
723 linux_detach (int pid
)
725 struct process_info
*process
;
727 process
= find_process_pid (pid
);
732 (struct thread_info
*) find_inferior (&all_threads
, any_thread_of
, &pid
);
734 delete_all_breakpoints ();
735 find_inferior (&all_threads
, linux_detach_one_lwp
, &pid
);
736 linux_remove_process (process
);
744 struct process_info
*process
;
746 process
= find_process_pid (pid
);
751 ret
= my_waitpid (pid
, &status
, 0);
752 if (WIFEXITED (status
) || WIFSIGNALED (status
))
754 } while (ret
!= -1 || errno
!= ECHILD
);
757 /* Return nonzero if the given thread is still alive. */
759 linux_thread_alive (ptid_t ptid
)
761 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
763 /* We assume we always know if a thread exits. If a whole process
764 exited but we still haven't been able to report it to GDB, we'll
765 hold on to the last lwp of the dead process. */
772 /* Return nonzero if this process stopped at a breakpoint which
773 no longer appears to be inserted. Also adjust the PC
774 appropriately to resume where the breakpoint used to be. */
776 check_removed_breakpoint (struct lwp_info
*event_child
)
779 struct thread_info
*saved_inferior
;
781 if (event_child
->pending_is_breakpoint
== 0)
785 fprintf (stderr
, "Checking for breakpoint in lwp %ld.\n",
786 lwpid_of (event_child
));
788 saved_inferior
= current_inferior
;
789 current_inferior
= get_lwp_thread (event_child
);
791 stop_pc
= get_stop_pc ();
793 /* If the PC has changed since we stopped, then we shouldn't do
794 anything. This happens if, for instance, GDB handled the
795 decr_pc_after_break subtraction itself. */
796 if (stop_pc
!= event_child
->pending_stop_pc
)
799 fprintf (stderr
, "Ignoring, PC was changed. Old PC was 0x%08llx\n",
800 event_child
->pending_stop_pc
);
802 event_child
->pending_is_breakpoint
= 0;
803 current_inferior
= saved_inferior
;
807 /* If the breakpoint is still there, we will report hitting it. */
808 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
811 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
812 current_inferior
= saved_inferior
;
817 fprintf (stderr
, "Removed breakpoint.\n");
819 /* For decr_pc_after_break targets, here is where we perform the
820 decrement. We go immediately from this function to resuming,
821 and can not safely call get_stop_pc () again. */
822 if (the_low_target
.set_pc
!= NULL
)
825 fprintf (stderr
, "Set pc to 0x%lx\n", (long) stop_pc
);
826 (*the_low_target
.set_pc
) (stop_pc
);
829 /* We consumed the pending SIGTRAP. */
830 event_child
->pending_is_breakpoint
= 0;
831 event_child
->status_pending_p
= 0;
832 event_child
->status_pending
= 0;
834 current_inferior
= saved_inferior
;
838 /* Return 1 if this lwp has an interesting status pending. This
839 function may silently resume an inferior lwp. */
841 status_pending_p (struct inferior_list_entry
*entry
, void *arg
)
843 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
844 ptid_t ptid
= * (ptid_t
*) arg
;
846 /* Check if we're only interested in events from a specific process
848 if (!ptid_equal (minus_one_ptid
, ptid
)
849 && ptid_get_pid (ptid
) != ptid_get_pid (lwp
->head
.id
))
852 if (lwp
->status_pending_p
&& !lwp
->suspended
)
853 if (check_removed_breakpoint (lwp
))
855 /* This thread was stopped at a breakpoint, and the breakpoint
856 is now gone. We were told to continue (or step...) all threads,
857 so GDB isn't trying to single-step past this breakpoint.
858 So instead of reporting the old SIGTRAP, pretend we got to
859 the breakpoint just after it was removed instead of just
860 before; resume the process. */
861 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
865 return (lwp
->status_pending_p
&& !lwp
->suspended
);
869 same_lwp (struct inferior_list_entry
*entry
, void *data
)
871 ptid_t ptid
= *(ptid_t
*) data
;
874 if (ptid_get_lwp (ptid
) != 0)
875 lwp
= ptid_get_lwp (ptid
);
877 lwp
= ptid_get_pid (ptid
);
879 if (ptid_get_lwp (entry
->id
) == lwp
)
886 find_lwp_pid (ptid_t ptid
)
888 return (struct lwp_info
*) find_inferior (&all_lwps
, same_lwp
, &ptid
);
891 static struct lwp_info
*
892 linux_wait_for_lwp (ptid_t ptid
, int *wstatp
, int options
)
895 int to_wait_for
= -1;
896 struct lwp_info
*child
= NULL
;
899 fprintf (stderr
, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid
));
901 if (ptid_equal (ptid
, minus_one_ptid
))
902 to_wait_for
= -1; /* any child */
904 to_wait_for
= ptid_get_lwp (ptid
); /* this lwp only */
910 ret
= my_waitpid (to_wait_for
, wstatp
, options
);
911 if (ret
== 0 || (ret
== -1 && errno
== ECHILD
&& (options
& WNOHANG
)))
914 perror_with_name ("waitpid");
917 && (!WIFSTOPPED (*wstatp
)
918 || (WSTOPSIG (*wstatp
) != 32
919 && WSTOPSIG (*wstatp
) != 33)))
920 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
922 child
= find_lwp_pid (pid_to_ptid (ret
));
924 /* If we didn't find a process, one of two things presumably happened:
925 - A process we started and then detached from has exited. Ignore it.
926 - A process we are controlling has forked and the new child's stop
927 was reported to us by the kernel. Save its PID. */
928 if (child
== NULL
&& WIFSTOPPED (*wstatp
))
930 add_pid_to_list (&stopped_pids
, ret
);
933 else if (child
== NULL
)
937 child
->pending_is_breakpoint
= 0;
939 child
->last_status
= *wstatp
;
941 /* Architecture-specific setup after inferior is running.
942 This needs to happen after we have attached to the inferior
943 and it is stopped for the first time, but before we access
944 any inferior registers. */
947 the_low_target
.arch_setup ();
948 #ifdef HAVE_LINUX_REGSETS
949 memset (disabled_regsets
, 0, num_regsets
);
955 && WIFSTOPPED (*wstatp
)
956 && the_low_target
.get_pc
!= NULL
)
958 struct thread_info
*saved_inferior
= current_inferior
;
961 current_inferior
= (struct thread_info
*)
962 find_inferior_id (&all_threads
, child
->head
.id
);
963 pc
= (*the_low_target
.get_pc
) ();
964 fprintf (stderr
, "linux_wait_for_lwp: pc is 0x%lx\n", (long) pc
);
965 current_inferior
= saved_inferior
;
971 /* Wait for an event from child PID. If PID is -1, wait for any
972 child. Store the stop status through the status pointer WSTAT.
973 OPTIONS is passed to the waitpid call. Return 0 if no child stop
974 event was found and OPTIONS contains WNOHANG. Return the PID of
975 the stopped child otherwise. */
978 linux_wait_for_event_1 (ptid_t ptid
, int *wstat
, int options
)
981 struct lwp_info
*event_child
= NULL
;
983 struct lwp_info
*requested_child
= NULL
;
985 /* Check for a lwp with a pending status. */
986 /* It is possible that the user changed the pending task's registers since
987 it stopped. We correctly handle the change of PC if we hit a breakpoint
988 (in check_removed_breakpoint); signals should be reported anyway. */
990 if (ptid_equal (ptid
, minus_one_ptid
)
991 || ptid_equal (pid_to_ptid (ptid_get_pid (ptid
)), ptid
))
993 event_child
= (struct lwp_info
*)
994 find_inferior (&all_lwps
, status_pending_p
, &ptid
);
995 if (debug_threads
&& event_child
)
996 fprintf (stderr
, "Got a pending child %ld\n", lwpid_of (event_child
));
1000 requested_child
= find_lwp_pid (ptid
);
1001 if (requested_child
->status_pending_p
1002 && !check_removed_breakpoint (requested_child
))
1003 event_child
= requested_child
;
1006 if (event_child
!= NULL
)
1009 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
1010 lwpid_of (event_child
), event_child
->status_pending
);
1011 *wstat
= event_child
->status_pending
;
1012 event_child
->status_pending_p
= 0;
1013 event_child
->status_pending
= 0;
1014 current_inferior
= get_lwp_thread (event_child
);
1015 return lwpid_of (event_child
);
1018 /* We only enter this loop if no process has a pending wait status. Thus
1019 any action taken in response to a wait status inside this loop is
1020 responding as soon as we detect the status, not after any pending
1024 event_child
= linux_wait_for_lwp (ptid
, wstat
, options
);
1026 if ((options
& WNOHANG
) && event_child
== NULL
)
1029 if (event_child
== NULL
)
1030 error ("event from unknown child");
1032 current_inferior
= get_lwp_thread (event_child
);
1034 /* Check for thread exit. */
1035 if (! WIFSTOPPED (*wstat
))
1038 fprintf (stderr
, "LWP %ld exiting\n", lwpid_of (event_child
));
1040 /* If the last thread is exiting, just return. */
1041 if (last_thread_of_process_p (current_inferior
))
1044 fprintf (stderr
, "LWP %ld is last lwp of process\n",
1045 lwpid_of (event_child
));
1046 return lwpid_of (event_child
);
1049 delete_lwp (event_child
);
1053 current_inferior
= (struct thread_info
*) all_threads
.head
;
1055 fprintf (stderr
, "Current inferior is now %ld\n",
1056 lwpid_of (get_thread_lwp (current_inferior
)));
1060 current_inferior
= NULL
;
1062 fprintf (stderr
, "Current inferior is now <NULL>\n");
1065 /* If we were waiting for this particular child to do something...
1066 well, it did something. */
1067 if (requested_child
!= NULL
)
1068 return lwpid_of (event_child
);
1070 /* Wait for a more interesting event. */
1074 if (event_child
->must_set_ptrace_flags
)
1076 ptrace (PTRACE_SETOPTIONS
, lwpid_of (event_child
),
1077 0, PTRACE_O_TRACECLONE
);
1078 event_child
->must_set_ptrace_flags
= 0;
1081 if (WIFSTOPPED (*wstat
)
1082 && WSTOPSIG (*wstat
) == SIGSTOP
1083 && event_child
->stop_expected
)
1086 fprintf (stderr
, "Expected stop.\n");
1087 event_child
->stop_expected
= 0;
1088 linux_resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
1092 if (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) == SIGTRAP
1093 && *wstat
>> 16 != 0)
1095 handle_extended_wait (event_child
, *wstat
);
1099 /* If GDB is not interested in this signal, don't stop other
1100 threads, and don't report it to GDB. Just resume the
1101 inferior right away. We do this for threading-related
1102 signals as well as any that GDB specifically requested we
1103 ignore. But never ignore SIGSTOP if we sent it ourselves,
1104 and do not ignore signals when stepping - they may require
1105 special handling to skip the signal handler. */
1106 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
1108 if (WIFSTOPPED (*wstat
)
1109 && !event_child
->stepping
1111 #ifdef USE_THREAD_DB
1112 (current_process ()->private->thread_db_active
1113 && (WSTOPSIG (*wstat
) == __SIGRTMIN
1114 || WSTOPSIG (*wstat
) == __SIGRTMIN
+ 1))
1117 (pass_signals
[target_signal_from_host (WSTOPSIG (*wstat
))]
1118 && (WSTOPSIG (*wstat
) != SIGSTOP
|| !stopping_threads
))))
1120 siginfo_t info
, *info_p
;
1123 fprintf (stderr
, "Ignored signal %d for LWP %ld.\n",
1124 WSTOPSIG (*wstat
), lwpid_of (event_child
));
1126 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (event_child
), 0, &info
) == 0)
1130 linux_resume_one_lwp (event_child
,
1131 event_child
->stepping
,
1132 WSTOPSIG (*wstat
), info_p
);
1136 /* If this event was not handled above, and is not a SIGTRAP, report
1138 if (!WIFSTOPPED (*wstat
) || WSTOPSIG (*wstat
) != SIGTRAP
)
1139 return lwpid_of (event_child
);
1141 /* If this target does not support breakpoints, we simply report the
1142 SIGTRAP; it's of no concern to us. */
1143 if (the_low_target
.get_pc
== NULL
)
1144 return lwpid_of (event_child
);
1146 stop_pc
= get_stop_pc ();
1148 /* bp_reinsert will only be set if we were single-stepping.
1149 Notice that we will resume the process after hitting
1150 a gdbserver breakpoint; single-stepping to/over one
1151 is not supported (yet). */
1152 if (event_child
->bp_reinsert
!= 0)
1155 fprintf (stderr
, "Reinserted breakpoint.\n");
1156 reinsert_breakpoint (event_child
->bp_reinsert
);
1157 event_child
->bp_reinsert
= 0;
1159 /* Clear the single-stepping flag and SIGTRAP as we resume. */
1160 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
1164 bp_status
= check_breakpoints (stop_pc
);
1169 fprintf (stderr
, "Hit a gdbserver breakpoint.\n");
1171 /* We hit one of our own breakpoints. We mark it as a pending
1172 breakpoint, so that check_removed_breakpoint () will do the PC
1173 adjustment for us at the appropriate time. */
1174 event_child
->pending_is_breakpoint
= 1;
1175 event_child
->pending_stop_pc
= stop_pc
;
1177 /* We may need to put the breakpoint back. We continue in the event
1178 loop instead of simply replacing the breakpoint right away,
1179 in order to not lose signals sent to the thread that hit the
1180 breakpoint. Unfortunately this increases the window where another
1181 thread could sneak past the removed breakpoint. For the current
1182 use of server-side breakpoints (thread creation) this is
1183 acceptable; but it needs to be considered before this breakpoint
1184 mechanism can be used in more general ways. For some breakpoints
1185 it may be necessary to stop all other threads, but that should
1186 be avoided where possible.
1188 If breakpoint_reinsert_addr is NULL, that means that we can
1189 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
1190 mark it for reinsertion, and single-step.
1192 Otherwise, call the target function to figure out where we need
1193 our temporary breakpoint, create it, and continue executing this
1196 /* NOTE: we're lifting breakpoints in non-stop mode. This
1197 is currently only used for thread event breakpoints, so
1198 it isn't that bad as long as we have PTRACE_EVENT_CLONE
1201 /* No need to reinsert. */
1202 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
1203 else if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
1205 event_child
->bp_reinsert
= stop_pc
;
1206 uninsert_breakpoint (stop_pc
);
1207 linux_resume_one_lwp (event_child
, 1, 0, NULL
);
1211 reinsert_breakpoint_by_bp
1212 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
1213 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
1220 fprintf (stderr
, "Hit a non-gdbserver breakpoint.\n");
1222 /* If we were single-stepping, we definitely want to report the
1223 SIGTRAP. Although the single-step operation has completed,
1224 do not clear clear the stepping flag yet; we need to check it
1225 in wait_for_sigstop. */
1226 if (event_child
->stepping
)
1227 return lwpid_of (event_child
);
1229 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
1230 Check if it is a breakpoint, and if so mark the process information
1231 accordingly. This will handle both the necessary fiddling with the
1232 PC on decr_pc_after_break targets and suppressing extra threads
1233 hitting a breakpoint if two hit it at once and then GDB removes it
1234 after the first is reported. Arguably it would be better to report
1235 multiple threads hitting breakpoints simultaneously, but the current
1236 remote protocol does not allow this. */
1237 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
1239 event_child
->pending_is_breakpoint
= 1;
1240 event_child
->pending_stop_pc
= stop_pc
;
1243 return lwpid_of (event_child
);
1251 linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
)
1255 if (ptid_is_pid (ptid
))
1257 /* A request to wait for a specific tgid. This is not possible
1258 with waitpid, so instead, we wait for any child, and leave
1259 children we're not interested in right now with a pending
1260 status to report later. */
1261 wait_ptid
= minus_one_ptid
;
1270 event_pid
= linux_wait_for_event_1 (wait_ptid
, wstat
, options
);
1273 && ptid_is_pid (ptid
) && ptid_get_pid (ptid
) != event_pid
)
1275 struct lwp_info
*event_child
= find_lwp_pid (pid_to_ptid (event_pid
));
1277 if (! WIFSTOPPED (*wstat
))
1278 mark_lwp_dead (event_child
, *wstat
);
1281 event_child
->status_pending_p
= 1;
1282 event_child
->status_pending
= *wstat
;
1290 /* Wait for process, returns status. */
1293 linux_wait_1 (ptid_t ptid
,
1294 struct target_waitstatus
*ourstatus
, int target_options
)
1297 struct thread_info
*thread
= NULL
;
1298 struct lwp_info
*lwp
= NULL
;
1302 /* Translate generic target options into linux options. */
1304 if (target_options
& TARGET_WNOHANG
)
1308 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1310 /* If we were only supposed to resume one thread, only wait for
1311 that thread - if it's still alive. If it died, however - which
1312 can happen if we're coming from the thread death case below -
1313 then we need to make sure we restart the other threads. We could
1314 pick a thread at random or restart all; restarting all is less
1317 && !ptid_equal (cont_thread
, null_ptid
)
1318 && !ptid_equal (cont_thread
, minus_one_ptid
))
1320 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
1323 /* No stepping, no signal - unless one is pending already, of course. */
1326 struct thread_resume resume_info
;
1327 resume_info
.thread
= minus_one_ptid
;
1328 resume_info
.kind
= resume_continue
;
1329 resume_info
.sig
= 0;
1330 linux_resume (&resume_info
, 1);
1336 pid
= linux_wait_for_event (ptid
, &w
, options
);
1337 if (pid
== 0) /* only if TARGET_WNOHANG */
1340 lwp
= get_thread_lwp (current_inferior
);
1342 /* If we are waiting for a particular child, and it exited,
1343 linux_wait_for_event will return its exit status. Similarly if
1344 the last child exited. If this is not the last child, however,
1345 do not report it as exited until there is a 'thread exited' response
1346 available in the remote protocol. Instead, just wait for another event.
1347 This should be safe, because if the thread crashed we will already
1348 have reported the termination signal to GDB; that should stop any
1349 in-progress stepping operations, etc.
1351 Report the exit status of the last thread to exit. This matches
1352 LinuxThreads' behavior. */
1354 if (last_thread_of_process_p (current_inferior
))
1356 if (WIFEXITED (w
) || WIFSIGNALED (w
))
1358 int pid
= pid_of (lwp
);
1359 struct process_info
*process
= find_process_pid (pid
);
1362 linux_remove_process (process
);
1364 current_inferior
= NULL
;
1368 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1369 ourstatus
->value
.integer
= WEXITSTATUS (w
);
1372 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
1376 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
1377 ourstatus
->value
.sig
= target_signal_from_host (WTERMSIG (w
));
1380 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
1384 return pid_to_ptid (pid
);
1389 if (!WIFSTOPPED (w
))
1393 /* In all-stop, stop all threads. Be careful to only do this if
1394 we're about to report an event to GDB. */
1398 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1400 if (lwp
->suspended
&& WSTOPSIG (w
) == SIGSTOP
)
1402 /* A thread that has been requested to stop by GDB with vCont;t,
1403 and it stopped cleanly, so report as SIG0. The use of
1404 SIGSTOP is an implementation detail. */
1405 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
1407 else if (lwp
->suspended
&& WSTOPSIG (w
) != SIGSTOP
)
1409 /* A thread that has been requested to stop by GDB with vCont;t,
1410 but, it stopped for other reasons. Set stop_expected so the
1411 pending SIGSTOP is ignored and the LWP is resumed. */
1412 lwp
->stop_expected
= 1;
1413 ourstatus
->value
.sig
= target_signal_from_host (WSTOPSIG (w
));
1417 ourstatus
->value
.sig
= target_signal_from_host (WSTOPSIG (w
));
1421 fprintf (stderr
, "linux_wait ret = %s, %d, %d\n",
1422 target_pid_to_str (lwp
->head
.id
),
1424 ourstatus
->value
.sig
);
1426 return lwp
->head
.id
;
1429 /* Get rid of any pending event in the pipe. */
1431 async_file_flush (void)
1437 ret
= read (linux_event_pipe
[0], &buf
, 1);
1438 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
1441 /* Put something in the pipe, so the event loop wakes up. */
1443 async_file_mark (void)
1447 async_file_flush ();
1450 ret
= write (linux_event_pipe
[1], "+", 1);
1451 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
1453 /* Ignore EAGAIN. If the pipe is full, the event loop will already
1454 be awakened anyway. */
1458 linux_wait (ptid_t ptid
,
1459 struct target_waitstatus
*ourstatus
, int target_options
)
1464 fprintf (stderr
, "linux_wait: [%s]\n", target_pid_to_str (ptid
));
1466 /* Flush the async file first. */
1467 if (target_is_async_p ())
1468 async_file_flush ();
1470 event_ptid
= linux_wait_1 (ptid
, ourstatus
, target_options
);
1472 /* If at least one stop was reported, there may be more. A single
1473 SIGCHLD can signal more than one child stop. */
1474 if (target_is_async_p ()
1475 && (target_options
& TARGET_WNOHANG
) != 0
1476 && !ptid_equal (event_ptid
, null_ptid
))
1482 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
1483 thread groups are in use, we need to use tkill. */
1486 kill_lwp (unsigned long lwpid
, int signo
)
1488 static int tkill_failed
;
1495 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
1496 if (errno
!= ENOSYS
)
1503 return kill (lwpid
, signo
);
1507 send_sigstop (struct inferior_list_entry
*entry
)
1509 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
1515 pid
= lwpid_of (lwp
);
1517 /* If we already have a pending stop signal for this process, don't
1519 if (lwp
->stop_expected
)
1522 fprintf (stderr
, "Have pending sigstop for lwp %d\n", pid
);
1524 /* We clear the stop_expected flag so that wait_for_sigstop
1525 will receive the SIGSTOP event (instead of silently resuming and
1526 waiting again). It'll be reset below. */
1527 lwp
->stop_expected
= 0;
1532 fprintf (stderr
, "Sending sigstop to lwp %d\n", pid
);
1534 kill_lwp (pid
, SIGSTOP
);
1538 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
1540 /* It's dead, really. */
1543 /* Store the exit status for later. */
1544 lwp
->status_pending_p
= 1;
1545 lwp
->status_pending
= wstat
;
1547 /* So that check_removed_breakpoint doesn't try to figure out if
1548 this is stopped at a breakpoint. */
1549 lwp
->pending_is_breakpoint
= 0;
1551 /* Prevent trying to stop it. */
1554 /* No further stops are expected from a dead lwp. */
1555 lwp
->stop_expected
= 0;
1559 wait_for_sigstop (struct inferior_list_entry
*entry
)
1561 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
1562 struct thread_info
*saved_inferior
;
1570 saved_inferior
= current_inferior
;
1571 if (saved_inferior
!= NULL
)
1572 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
1574 saved_tid
= null_ptid
; /* avoid bogus unused warning */
1576 ptid
= lwp
->head
.id
;
1578 linux_wait_for_event (ptid
, &wstat
, __WALL
);
1580 /* If we stopped with a non-SIGSTOP signal, save it for later
1581 and record the pending SIGSTOP. If the process exited, just
1583 if (WIFSTOPPED (wstat
)
1584 && WSTOPSIG (wstat
) != SIGSTOP
)
1587 fprintf (stderr
, "LWP %ld stopped with non-sigstop status %06x\n",
1588 lwpid_of (lwp
), wstat
);
1590 /* Do not leave a pending single-step finish to be reported to
1591 the client. The client will give us a new action for this
1592 thread, possibly a continue request --- otherwise, the client
1593 would consider this pending SIGTRAP reported later a spurious
1595 if (WSTOPSIG (wstat
) == SIGTRAP
1597 && !linux_stopped_by_watchpoint ())
1600 fprintf (stderr
, " single-step SIGTRAP ignored\n");
1604 lwp
->status_pending_p
= 1;
1605 lwp
->status_pending
= wstat
;
1607 lwp
->stop_expected
= 1;
1609 else if (!WIFSTOPPED (wstat
))
1612 fprintf (stderr
, "Process %ld exited while stopping LWPs\n",
1615 /* Leave this status pending for the next time we're able to
1616 report it. In the mean time, we'll report this lwp as dead
1617 to GDB, so GDB doesn't try to read registers and memory from
1619 mark_lwp_dead (lwp
, wstat
);
1622 if (saved_inferior
== NULL
|| linux_thread_alive (saved_tid
))
1623 current_inferior
= saved_inferior
;
1627 fprintf (stderr
, "Previously current thread died.\n");
1631 /* We can't change the current inferior behind GDB's back,
1632 otherwise, a subsequent command may apply to the wrong
1634 current_inferior
= NULL
;
1638 /* Set a valid thread as current. */
1639 set_desired_inferior (0);
1645 stop_all_lwps (void)
1647 stopping_threads
= 1;
1648 for_each_inferior (&all_lwps
, send_sigstop
);
1649 for_each_inferior (&all_lwps
, wait_for_sigstop
);
1650 stopping_threads
= 0;
1653 /* Resume execution of the inferior process.
1654 If STEP is nonzero, single-step it.
1655 If SIGNAL is nonzero, give it that signal. */
1658 linux_resume_one_lwp (struct lwp_info
*lwp
,
1659 int step
, int signal
, siginfo_t
*info
)
1661 struct thread_info
*saved_inferior
;
1663 if (lwp
->stopped
== 0)
1666 /* If we have pending signals or status, and a new signal, enqueue the
1667 signal. Also enqueue the signal if we are waiting to reinsert a
1668 breakpoint; it will be picked up again below. */
1670 && (lwp
->status_pending_p
|| lwp
->pending_signals
!= NULL
1671 || lwp
->bp_reinsert
!= 0))
1673 struct pending_signals
*p_sig
;
1674 p_sig
= xmalloc (sizeof (*p_sig
));
1675 p_sig
->prev
= lwp
->pending_signals
;
1676 p_sig
->signal
= signal
;
1678 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1680 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
1681 lwp
->pending_signals
= p_sig
;
1684 if (lwp
->status_pending_p
&& !check_removed_breakpoint (lwp
))
1687 saved_inferior
= current_inferior
;
1688 current_inferior
= get_lwp_thread (lwp
);
1691 fprintf (stderr
, "Resuming lwp %ld (%s, signal %d, stop %s)\n",
1692 lwpid_of (lwp
), step
? "step" : "continue", signal
,
1693 lwp
->stop_expected
? "expected" : "not expected");
1695 /* This bit needs some thinking about. If we get a signal that
1696 we must report while a single-step reinsert is still pending,
1697 we often end up resuming the thread. It might be better to
1698 (ew) allow a stack of pending events; then we could be sure that
1699 the reinsert happened right away and not lose any signals.
1701 Making this stack would also shrink the window in which breakpoints are
1702 uninserted (see comment in linux_wait_for_lwp) but not enough for
1703 complete correctness, so it won't solve that problem. It may be
1704 worthwhile just to solve this one, however. */
1705 if (lwp
->bp_reinsert
!= 0)
1708 fprintf (stderr
, " pending reinsert at %08lx", (long)lwp
->bp_reinsert
);
1710 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
1713 /* Postpone any pending signal. It was enqueued above. */
1717 check_removed_breakpoint (lwp
);
1719 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
1721 CORE_ADDR pc
= (*the_low_target
.get_pc
) ();
1722 fprintf (stderr
, " resuming from pc 0x%lx\n", (long) pc
);
1725 /* If we have pending signals, consume one unless we are trying to reinsert
1727 if (lwp
->pending_signals
!= NULL
&& lwp
->bp_reinsert
== 0)
1729 struct pending_signals
**p_sig
;
1731 p_sig
= &lwp
->pending_signals
;
1732 while ((*p_sig
)->prev
!= NULL
)
1733 p_sig
= &(*p_sig
)->prev
;
1735 signal
= (*p_sig
)->signal
;
1736 if ((*p_sig
)->info
.si_signo
!= 0)
1737 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
), 0, &(*p_sig
)->info
);
1743 regcache_invalidate_one ((struct inferior_list_entry
*)
1744 get_lwp_thread (lwp
));
1747 lwp
->stepping
= step
;
1748 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, lwpid_of (lwp
), 0, signal
);
1750 current_inferior
= saved_inferior
;
1753 /* ESRCH from ptrace either means that the thread was already
1754 running (an error) or that it is gone (a race condition). If
1755 it's gone, we will get a notification the next time we wait,
1756 so we can ignore the error. We could differentiate these
1757 two, but it's tricky without waiting; the thread still exists
1758 as a zombie, so sending it signal 0 would succeed. So just
1763 perror_with_name ("ptrace");
1767 struct thread_resume_array
1769 struct thread_resume
*resume
;
1773 /* This function is called once per thread. We look up the thread
1774 in RESUME_PTR, and mark the thread with a pointer to the appropriate
1777 This algorithm is O(threads * resume elements), but resume elements
1778 is small (and will remain small at least until GDB supports thread
1781 linux_set_resume_request (struct inferior_list_entry
*entry
, void *arg
)
1783 struct lwp_info
*lwp
;
1784 struct thread_info
*thread
;
1786 struct thread_resume_array
*r
;
1788 thread
= (struct thread_info
*) entry
;
1789 lwp
= get_thread_lwp (thread
);
1792 for (ndx
= 0; ndx
< r
->n
; ndx
++)
1794 ptid_t ptid
= r
->resume
[ndx
].thread
;
1795 if (ptid_equal (ptid
, minus_one_ptid
)
1796 || ptid_equal (ptid
, entry
->id
)
1797 || (ptid_is_pid (ptid
)
1798 && (ptid_get_pid (ptid
) == pid_of (lwp
)))
1799 || (ptid_get_lwp (ptid
) == -1
1800 && (ptid_get_pid (ptid
) == pid_of (lwp
))))
1802 lwp
->resume
= &r
->resume
[ndx
];
1807 /* No resume action for this thread. */
1814 /* Set *FLAG_P if this lwp has an interesting status pending. */
1816 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1818 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
1820 /* LWPs which will not be resumed are not interesting, because
1821 we might not wait for them next time through linux_wait. */
1822 if (lwp
->resume
== NULL
)
1825 /* If this thread has a removed breakpoint, we won't have any
1826 events to report later, so check now. check_removed_breakpoint
1827 may clear status_pending_p. We avoid calling check_removed_breakpoint
1828 for any thread that we are not otherwise going to resume - this
1829 lets us preserve stopped status when two threads hit a breakpoint.
1830 GDB removes the breakpoint to single-step a particular thread
1831 past it, then re-inserts it and resumes all threads. We want
1832 to report the second thread without resuming it in the interim. */
1833 if (lwp
->status_pending_p
)
1834 check_removed_breakpoint (lwp
);
1836 if (lwp
->status_pending_p
)
1837 * (int *) flag_p
= 1;
1842 /* This function is called once per thread. We check the thread's resume
1843 request, which will tell us whether to resume, step, or leave the thread
1844 stopped; and what signal, if any, it should be sent.
1846 For threads which we aren't explicitly told otherwise, we preserve
1847 the stepping flag; this is used for stepping over gdbserver-placed
1850 If pending_flags was set in any thread, we queue any needed
1851 signals, since we won't actually resume. We already have a pending
1852 event to report, so we don't need to preserve any step requests;
1853 they should be re-issued if necessary. */
1856 linux_resume_one_thread (struct inferior_list_entry
*entry
, void *arg
)
1858 struct lwp_info
*lwp
;
1859 struct thread_info
*thread
;
1861 int pending_flag
= * (int *) arg
;
1863 thread
= (struct thread_info
*) entry
;
1864 lwp
= get_thread_lwp (thread
);
1866 if (lwp
->resume
== NULL
)
1869 if (lwp
->resume
->kind
== resume_stop
)
1872 fprintf (stderr
, "suspending LWP %ld\n", lwpid_of (lwp
));
1877 fprintf (stderr
, "running -> suspending LWP %ld\n", lwpid_of (lwp
));
1880 send_sigstop (&lwp
->head
);
1887 fprintf (stderr
, "already stopped/suspended LWP %ld\n",
1890 fprintf (stderr
, "already stopped/not suspended LWP %ld\n",
1894 /* Make sure we leave the LWP suspended, so we don't try to
1895 resume it without GDB telling us to. FIXME: The LWP may
1896 have been stopped in an internal event that was not meant
1897 to be notified back to GDB (e.g., gdbserver breakpoint),
1898 so we should be reporting a stop event in that case
1903 /* For stop requests, we're done. */
1910 /* If this thread which is about to be resumed has a pending status,
1911 then don't resume any threads - we can just report the pending
1912 status. Make sure to queue any signals that would otherwise be
1913 sent. In all-stop mode, we do this decision based on if *any*
1914 thread has a pending status. */
1916 resume_status_pending_p (&lwp
->head
, &pending_flag
);
1921 fprintf (stderr
, "resuming LWP %ld\n", lwpid_of (lwp
));
1923 if (ptid_equal (lwp
->resume
->thread
, minus_one_ptid
)
1925 && lwp
->pending_is_breakpoint
)
1928 step
= (lwp
->resume
->kind
== resume_step
);
1930 linux_resume_one_lwp (lwp
, step
, lwp
->resume
->sig
, NULL
);
1935 fprintf (stderr
, "leaving LWP %ld stopped\n", lwpid_of (lwp
));
1937 /* If we have a new signal, enqueue the signal. */
1938 if (lwp
->resume
->sig
!= 0)
1940 struct pending_signals
*p_sig
;
1941 p_sig
= xmalloc (sizeof (*p_sig
));
1942 p_sig
->prev
= lwp
->pending_signals
;
1943 p_sig
->signal
= lwp
->resume
->sig
;
1944 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1946 /* If this is the same signal we were previously stopped by,
1947 make sure to queue its siginfo. We can ignore the return
1948 value of ptrace; if it fails, we'll skip
1949 PTRACE_SETSIGINFO. */
1950 if (WIFSTOPPED (lwp
->last_status
)
1951 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
)
1952 ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
), 0, &p_sig
->info
);
1954 lwp
->pending_signals
= p_sig
;
1963 linux_resume (struct thread_resume
*resume_info
, size_t n
)
1966 struct thread_resume_array array
= { resume_info
, n
};
1968 find_inferior (&all_threads
, linux_set_resume_request
, &array
);
1970 /* If there is a thread which would otherwise be resumed, which
1971 has a pending status, then don't resume any threads - we can just
1972 report the pending status. Make sure to queue any signals
1973 that would otherwise be sent. In non-stop mode, we'll apply this
1974 logic to each thread individually. */
1977 find_inferior (&all_lwps
, resume_status_pending_p
, &pending_flag
);
1982 fprintf (stderr
, "Not resuming, pending status\n");
1984 fprintf (stderr
, "Resuming, no pending status\n");
1987 find_inferior (&all_threads
, linux_resume_one_thread
, &pending_flag
);
1990 #ifdef HAVE_LINUX_USRREGS
1993 register_addr (int regnum
)
1997 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1998 error ("Invalid register number %d.", regnum
);
2000 addr
= the_low_target
.regmap
[regnum
];
2005 /* Fetch one register. */
2007 fetch_register (int regno
)
2014 if (regno
>= the_low_target
.num_regs
)
2016 if ((*the_low_target
.cannot_fetch_register
) (regno
))
2019 regaddr
= register_addr (regno
);
2023 pid
= lwpid_of (get_thread_lwp (current_inferior
));
2024 size
= ((register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
2025 & - sizeof (PTRACE_XFER_TYPE
));
2026 buf
= alloca (size
);
2027 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
2030 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
2031 ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
2032 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
2035 /* Warning, not error, in case we are attached; sometimes the
2036 kernel doesn't let us at the registers. */
2037 char *err
= strerror (errno
);
2038 char *msg
= alloca (strlen (err
) + 128);
2039 sprintf (msg
, "reading register %d: %s", regno
, err
);
2045 if (the_low_target
.supply_ptrace_register
)
2046 the_low_target
.supply_ptrace_register (regno
, buf
);
2048 supply_register (regno
, buf
);
2053 /* Fetch all registers, or just one, from the child process. */
2055 usr_fetch_inferior_registers (int regno
)
2057 if (regno
== -1 || regno
== 0)
2058 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
2059 fetch_register (regno
);
2061 fetch_register (regno
);
2064 /* Store our register values back into the inferior.
2065 If REGNO is -1, do this for all registers.
2066 Otherwise, REGNO specifies which register (so we can save time). */
2068 usr_store_inferior_registers (int regno
)
2077 if (regno
>= the_low_target
.num_regs
)
2080 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
2083 regaddr
= register_addr (regno
);
2087 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
2088 & - sizeof (PTRACE_XFER_TYPE
);
2089 buf
= alloca (size
);
2090 memset (buf
, 0, size
);
2092 if (the_low_target
.collect_ptrace_register
)
2093 the_low_target
.collect_ptrace_register (regno
, buf
);
2095 collect_register (regno
, buf
);
2097 pid
= lwpid_of (get_thread_lwp (current_inferior
));
2098 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
2101 ptrace (PTRACE_POKEUSER
, pid
, (PTRACE_ARG3_TYPE
) regaddr
,
2102 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
2105 /* At this point, ESRCH should mean the process is
2106 already gone, in which case we simply ignore attempts
2107 to change its registers. See also the related
2108 comment in linux_resume_one_lwp. */
2112 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
2114 char *err
= strerror (errno
);
2115 char *msg
= alloca (strlen (err
) + 128);
2116 sprintf (msg
, "writing register %d: %s",
2122 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
2126 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
2127 usr_store_inferior_registers (regno
);
2129 #endif /* HAVE_LINUX_USRREGS */
2133 #ifdef HAVE_LINUX_REGSETS
2136 regsets_fetch_inferior_registers ()
2138 struct regset_info
*regset
;
2139 int saw_general_regs
= 0;
2142 regset
= target_regsets
;
2144 pid
= lwpid_of (get_thread_lwp (current_inferior
));
2145 while (regset
->size
>= 0)
2150 if (regset
->size
== 0 || disabled_regsets
[regset
- target_regsets
])
2156 buf
= xmalloc (regset
->size
);
2158 res
= ptrace (regset
->get_request
, pid
, 0, buf
);
2160 res
= ptrace (regset
->get_request
, pid
, buf
, 0);
2166 /* If we get EIO on a regset, do not try it again for
2168 disabled_regsets
[regset
- target_regsets
] = 1;
2175 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
2180 else if (regset
->type
== GENERAL_REGS
)
2181 saw_general_regs
= 1;
2182 regset
->store_function (buf
);
2186 if (saw_general_regs
)
2193 regsets_store_inferior_registers ()
2195 struct regset_info
*regset
;
2196 int saw_general_regs
= 0;
2199 regset
= target_regsets
;
2201 pid
= lwpid_of (get_thread_lwp (current_inferior
));
2202 while (regset
->size
>= 0)
2207 if (regset
->size
== 0 || disabled_regsets
[regset
- target_regsets
])
2213 buf
= xmalloc (regset
->size
);
2215 /* First fill the buffer with the current register set contents,
2216 in case there are any items in the kernel's regset that are
2217 not in gdbserver's regcache. */
2219 res
= ptrace (regset
->get_request
, pid
, 0, buf
);
2221 res
= ptrace (regset
->get_request
, pid
, buf
, 0);
2226 /* Then overlay our cached registers on that. */
2227 regset
->fill_function (buf
);
2229 /* Only now do we write the register set. */
2231 res
= ptrace (regset
->set_request
, pid
, 0, buf
);
2233 res
= ptrace (regset
->set_request
, pid
, buf
, 0);
2241 /* If we get EIO on a regset, do not try it again for
2243 disabled_regsets
[regset
- target_regsets
] = 1;
2247 else if (errno
== ESRCH
)
2249 /* At this point, ESRCH should mean the process is
2250 already gone, in which case we simply ignore attempts
2251 to change its registers. See also the related
2252 comment in linux_resume_one_lwp. */
2258 perror ("Warning: ptrace(regsets_store_inferior_registers)");
2261 else if (regset
->type
== GENERAL_REGS
)
2262 saw_general_regs
= 1;
2266 if (saw_general_regs
)
2273 #endif /* HAVE_LINUX_REGSETS */
2277 linux_fetch_registers (int regno
)
2279 #ifdef HAVE_LINUX_REGSETS
2280 if (regsets_fetch_inferior_registers () == 0)
2283 #ifdef HAVE_LINUX_USRREGS
2284 usr_fetch_inferior_registers (regno
);
2289 linux_store_registers (int regno
)
2291 #ifdef HAVE_LINUX_REGSETS
2292 if (regsets_store_inferior_registers () == 0)
2295 #ifdef HAVE_LINUX_USRREGS
2296 usr_store_inferior_registers (regno
);
2301 /* Copy LEN bytes from inferior's memory starting at MEMADDR
2302 to debugger memory starting at MYADDR. */
2305 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
2308 /* Round starting address down to longword boundary. */
2309 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
2310 /* Round ending address up; get number of longwords that makes. */
2312 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
2313 / sizeof (PTRACE_XFER_TYPE
);
2314 /* Allocate buffer of that many longwords. */
2315 register PTRACE_XFER_TYPE
*buffer
2316 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
2319 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
2321 /* Try using /proc. Don't bother for one word. */
2322 if (len
>= 3 * sizeof (long))
2324 /* We could keep this file open and cache it - possibly one per
2325 thread. That requires some juggling, but is even faster. */
2326 sprintf (filename
, "/proc/%d/mem", pid
);
2327 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
2331 /* If pread64 is available, use it. It's faster if the kernel
2332 supports it (only one syscall), and it's 64-bit safe even on
2333 32-bit platforms (for instance, SPARC debugging a SPARC64
2336 if (pread64 (fd
, myaddr
, len
, memaddr
) != len
)
2338 if (lseek (fd
, memaddr
, SEEK_SET
) == -1 || read (fd
, memaddr
, len
) != len
)
2350 /* Read all the longwords */
2351 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
2354 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
2359 /* Copy appropriate bytes out of the buffer. */
2361 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
2367 /* Copy LEN bytes of data from debugger memory at MYADDR
2368 to inferior's memory at MEMADDR.
2369 On failure (cannot write the inferior)
2370 returns the value of errno. */
2373 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
2376 /* Round starting address down to longword boundary. */
2377 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
2378 /* Round ending address up; get number of longwords that makes. */
2380 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
2381 /* Allocate buffer of that many longwords. */
2382 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
2383 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
2387 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
2390 /* Fill start and end extra bytes of buffer with existing memory data. */
2392 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
2397 = ptrace (PTRACE_PEEKTEXT
, pid
,
2398 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
2399 * sizeof (PTRACE_XFER_TYPE
)),
2403 /* Copy data to be written over corresponding part of buffer */
2405 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
2407 /* Write the entire buffer. */
2409 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
2412 ptrace (PTRACE_POKETEXT
, pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
2420 static int linux_supports_tracefork_flag
;
2422 /* Helper functions for linux_test_for_tracefork, called via clone (). */
2425 linux_tracefork_grandchild (void *arg
)
2430 #define STACK_SIZE 4096
2433 linux_tracefork_child (void *arg
)
2435 ptrace (PTRACE_TRACEME
, 0, 0, 0);
2436 kill (getpid (), SIGSTOP
);
2438 __clone2 (linux_tracefork_grandchild
, arg
, STACK_SIZE
,
2439 CLONE_VM
| SIGCHLD
, NULL
);
2441 clone (linux_tracefork_grandchild
, arg
+ STACK_SIZE
,
2442 CLONE_VM
| SIGCHLD
, NULL
);
2447 /* Wrapper function for waitpid which handles EINTR, and emulates
2448 __WALL for systems where that is not available. */
2451 my_waitpid (int pid
, int *status
, int flags
)
2456 fprintf (stderr
, "my_waitpid (%d, 0x%x)\n", pid
, flags
);
2460 sigset_t block_mask
, org_mask
, wake_mask
;
2463 wnohang
= (flags
& WNOHANG
) != 0;
2464 flags
&= ~(__WALL
| __WCLONE
);
2467 /* Block all signals while here. This avoids knowing about
2468 LinuxThread's signals. */
2469 sigfillset (&block_mask
);
2470 sigprocmask (SIG_BLOCK
, &block_mask
, &org_mask
);
2472 /* ... except during the sigsuspend below. */
2473 sigemptyset (&wake_mask
);
2477 /* Since all signals are blocked, there's no need to check
2479 ret
= waitpid (pid
, status
, flags
);
2482 if (ret
== -1 && out_errno
!= ECHILD
)
2487 if (flags
& __WCLONE
)
2489 /* We've tried both flavors now. If WNOHANG is set,
2490 there's nothing else to do, just bail out. */
2495 fprintf (stderr
, "blocking\n");
2497 /* Block waiting for signals. */
2498 sigsuspend (&wake_mask
);
2504 sigprocmask (SIG_SETMASK
, &org_mask
, NULL
);
2509 ret
= waitpid (pid
, status
, flags
);
2510 while (ret
== -1 && errno
== EINTR
);
2515 fprintf (stderr
, "my_waitpid (%d, 0x%x): status(%x), %d\n",
2516 pid
, flags
, status
? *status
: -1, ret
);
2522 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
2523 sure that we can enable the option, and that it had the desired
2527 linux_test_for_tracefork (void)
2529 int child_pid
, ret
, status
;
2531 char *stack
= xmalloc (STACK_SIZE
* 4);
2533 linux_supports_tracefork_flag
= 0;
2535 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
2537 child_pid
= __clone2 (linux_tracefork_child
, stack
, STACK_SIZE
,
2538 CLONE_VM
| SIGCHLD
, stack
+ STACK_SIZE
* 2);
2540 child_pid
= clone (linux_tracefork_child
, stack
+ STACK_SIZE
,
2541 CLONE_VM
| SIGCHLD
, stack
+ STACK_SIZE
* 2);
2543 if (child_pid
== -1)
2544 perror_with_name ("clone");
2546 ret
= my_waitpid (child_pid
, &status
, 0);
2548 perror_with_name ("waitpid");
2549 else if (ret
!= child_pid
)
2550 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
2551 if (! WIFSTOPPED (status
))
2552 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
2554 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
2557 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
2560 warning ("linux_test_for_tracefork: failed to kill child");
2564 ret
= my_waitpid (child_pid
, &status
, 0);
2565 if (ret
!= child_pid
)
2566 warning ("linux_test_for_tracefork: failed to wait for killed child");
2567 else if (!WIFSIGNALED (status
))
2568 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
2569 "killed child", status
);
2574 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
2576 warning ("linux_test_for_tracefork: failed to resume child");
2578 ret
= my_waitpid (child_pid
, &status
, 0);
2580 if (ret
== child_pid
&& WIFSTOPPED (status
)
2581 && status
>> 16 == PTRACE_EVENT_FORK
)
2584 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
2585 if (ret
== 0 && second_pid
!= 0)
2589 linux_supports_tracefork_flag
= 1;
2590 my_waitpid (second_pid
, &second_status
, 0);
2591 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
2593 warning ("linux_test_for_tracefork: failed to kill second child");
2594 my_waitpid (second_pid
, &status
, 0);
2598 warning ("linux_test_for_tracefork: unexpected result from waitpid "
2599 "(%d, status 0x%x)", ret
, status
);
2603 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
2605 warning ("linux_test_for_tracefork: failed to kill child");
2606 my_waitpid (child_pid
, &status
, 0);
2608 while (WIFSTOPPED (status
));
2615 linux_look_up_symbols (void)
2617 #ifdef USE_THREAD_DB
2618 struct process_info
*proc
= current_process ();
2620 if (proc
->private->thread_db_active
)
2623 proc
->private->thread_db_active
2624 = thread_db_init (!linux_supports_tracefork_flag
);
2629 linux_request_interrupt (void)
2631 extern unsigned long signal_pid
;
2633 if (!ptid_equal (cont_thread
, null_ptid
)
2634 && !ptid_equal (cont_thread
, minus_one_ptid
))
2636 struct lwp_info
*lwp
;
2639 lwp
= get_thread_lwp (current_inferior
);
2640 lwpid
= lwpid_of (lwp
);
2641 kill_lwp (lwpid
, SIGINT
);
2644 kill_lwp (signal_pid
, SIGINT
);
2647 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
2648 to debugger memory starting at MYADDR. */
2651 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
2653 char filename
[PATH_MAX
];
2655 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
2657 snprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
2659 fd
= open (filename
, O_RDONLY
);
2663 if (offset
!= (CORE_ADDR
) 0
2664 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
2667 n
= read (fd
, myaddr
, len
);
2674 /* These watchpoint related wrapper functions simply pass on the function call
2675 if the target has registered a corresponding function. */
2678 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
2680 if (the_low_target
.insert_watchpoint
!= NULL
)
2681 return the_low_target
.insert_watchpoint (type
, addr
, len
);
2683 /* Unsupported (see target.h). */
2688 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
2690 if (the_low_target
.remove_watchpoint
!= NULL
)
2691 return the_low_target
.remove_watchpoint (type
, addr
, len
);
2693 /* Unsupported (see target.h). */
2698 linux_stopped_by_watchpoint (void)
2700 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
2701 return the_low_target
.stopped_by_watchpoint ();
2707 linux_stopped_data_address (void)
2709 if (the_low_target
.stopped_data_address
!= NULL
)
2710 return the_low_target
.stopped_data_address ();
2715 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
2716 #if defined(__mcoldfire__)
2717 /* These should really be defined in the kernel's ptrace.h header. */
2718 #define PT_TEXT_ADDR 49*4
2719 #define PT_DATA_ADDR 50*4
2720 #define PT_TEXT_END_ADDR 51*4
2723 /* Under uClinux, programs are loaded at non-zero offsets, which we need
2724 to tell gdb about. */
2727 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
2729 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
2730 unsigned long text
, text_end
, data
;
2731 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
2735 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
2736 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
2737 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
2741 /* Both text and data offsets produced at compile-time (and so
2742 used by gdb) are relative to the beginning of the program,
2743 with the data segment immediately following the text segment.
2744 However, the actual runtime layout in memory may put the data
2745 somewhere else, so when we send gdb a data base-address, we
2746 use the real data base address and subtract the compile-time
2747 data base-address from it (which is just the length of the
2748 text segment). BSS immediately follows data in both
2751 *data_p
= data
- (text_end
- text
);
2761 linux_qxfer_osdata (const char *annex
,
2762 unsigned char *readbuf
, unsigned const char *writebuf
,
2763 CORE_ADDR offset
, int len
)
2765 /* We make the process list snapshot when the object starts to be
2767 static const char *buf
;
2768 static long len_avail
= -1;
2769 static struct buffer buffer
;
2773 if (strcmp (annex
, "processes") != 0)
2776 if (!readbuf
|| writebuf
)
2781 if (len_avail
!= -1 && len_avail
!= 0)
2782 buffer_free (&buffer
);
2785 buffer_init (&buffer
);
2786 buffer_grow_str (&buffer
, "<osdata type=\"processes\">");
2788 dirp
= opendir ("/proc");
2792 while ((dp
= readdir (dirp
)) != NULL
)
2794 struct stat statbuf
;
2795 char procentry
[sizeof ("/proc/4294967295")];
2797 if (!isdigit (dp
->d_name
[0])
2798 || strlen (dp
->d_name
) > sizeof ("4294967295") - 1)
2801 sprintf (procentry
, "/proc/%s", dp
->d_name
);
2802 if (stat (procentry
, &statbuf
) == 0
2803 && S_ISDIR (statbuf
.st_mode
))
2807 char cmd
[MAXPATHLEN
+ 1];
2808 struct passwd
*entry
;
2810 sprintf (pathname
, "/proc/%s/cmdline", dp
->d_name
);
2811 entry
= getpwuid (statbuf
.st_uid
);
2813 if ((f
= fopen (pathname
, "r")) != NULL
)
2815 size_t len
= fread (cmd
, 1, sizeof (cmd
) - 1, f
);
2819 for (i
= 0; i
< len
; i
++)
2827 "<column name=\"pid\">%s</column>"
2828 "<column name=\"user\">%s</column>"
2829 "<column name=\"command\">%s</column>"
2832 entry
? entry
->pw_name
: "?",
2842 buffer_grow_str0 (&buffer
, "</osdata>\n");
2843 buf
= buffer_finish (&buffer
);
2844 len_avail
= strlen (buf
);
2847 if (offset
>= len_avail
)
2849 /* Done. Get rid of the data. */
2850 buffer_free (&buffer
);
2856 if (len
> len_avail
- offset
)
2857 len
= len_avail
- offset
;
2858 memcpy (readbuf
, buf
+ offset
, len
);
2863 /* Convert a native/host siginfo object, into/from the siginfo in the
2864 layout of the inferiors' architecture. */
2867 siginfo_fixup (struct siginfo
*siginfo
, void *inf_siginfo
, int direction
)
2871 if (the_low_target
.siginfo_fixup
!= NULL
)
2872 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
2874 /* If there was no callback, or the callback didn't do anything,
2875 then just do a straight memcpy. */
2879 memcpy (siginfo
, inf_siginfo
, sizeof (struct siginfo
));
2881 memcpy (inf_siginfo
, siginfo
, sizeof (struct siginfo
));
2886 linux_xfer_siginfo (const char *annex
, unsigned char *readbuf
,
2887 unsigned const char *writebuf
, CORE_ADDR offset
, int len
)
2890 struct siginfo siginfo
;
2891 char inf_siginfo
[sizeof (struct siginfo
)];
2893 if (current_inferior
== NULL
)
2896 pid
= lwpid_of (get_thread_lwp (current_inferior
));
2899 fprintf (stderr
, "%s siginfo for lwp %d.\n",
2900 readbuf
!= NULL
? "Reading" : "Writing",
2903 if (offset
> sizeof (siginfo
))
2906 if (ptrace (PTRACE_GETSIGINFO
, pid
, 0, &siginfo
) != 0)
2909 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
2910 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
2911 inferior with a 64-bit GDBSERVER should look the same as debugging it
2912 with a 32-bit GDBSERVER, we need to convert it. */
2913 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
2915 if (offset
+ len
> sizeof (siginfo
))
2916 len
= sizeof (siginfo
) - offset
;
2918 if (readbuf
!= NULL
)
2919 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
2922 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
2924 /* Convert back to ptrace layout before flushing it out. */
2925 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
2927 if (ptrace (PTRACE_SETSIGINFO
, pid
, 0, &siginfo
) != 0)
2934 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
2935 so we notice when children change state; as the handler for the
2936 sigsuspend in my_waitpid. */
2939 sigchld_handler (int signo
)
2941 int old_errno
= errno
;
2944 /* fprintf is not async-signal-safe, so call write directly. */
2945 write (2, "sigchld_handler\n", sizeof ("sigchld_handler\n") - 1);
2947 if (target_is_async_p ())
2948 async_file_mark (); /* trigger a linux_wait */
2954 linux_supports_non_stop (void)
2960 linux_async (int enable
)
2962 int previous
= (linux_event_pipe
[0] != -1);
2964 if (previous
!= enable
)
2967 sigemptyset (&mask
);
2968 sigaddset (&mask
, SIGCHLD
);
2970 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
2974 if (pipe (linux_event_pipe
) == -1)
2975 fatal ("creating event pipe failed.");
2977 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
2978 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
2980 /* Register the event loop handler. */
2981 add_file_handler (linux_event_pipe
[0],
2982 handle_target_event
, NULL
);
2984 /* Always trigger a linux_wait. */
2989 delete_file_handler (linux_event_pipe
[0]);
2991 close (linux_event_pipe
[0]);
2992 close (linux_event_pipe
[1]);
2993 linux_event_pipe
[0] = -1;
2994 linux_event_pipe
[1] = -1;
2997 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
3004 linux_start_non_stop (int nonstop
)
3006 /* Register or unregister from event-loop accordingly. */
3007 linux_async (nonstop
);
3011 static struct target_ops linux_target_ops
= {
3012 linux_create_inferior
,
3020 linux_fetch_registers
,
3021 linux_store_registers
,
3024 linux_look_up_symbols
,
3025 linux_request_interrupt
,
3027 linux_insert_watchpoint
,
3028 linux_remove_watchpoint
,
3029 linux_stopped_by_watchpoint
,
3030 linux_stopped_data_address
,
3031 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
3036 #ifdef USE_THREAD_DB
3037 thread_db_get_tls_address
,
3042 hostio_last_error_from_errno
,
3045 linux_supports_non_stop
,
3047 linux_start_non_stop
,
3051 linux_init_signals ()
3053 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
3054 to find what the cancel signal actually is. */
3055 signal (__SIGRTMIN
+1, SIG_IGN
);
3059 initialize_low (void)
3061 struct sigaction sigchld_action
;
3062 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
3063 set_target_ops (&linux_target_ops
);
3064 set_breakpoint_data (the_low_target
.breakpoint
,
3065 the_low_target
.breakpoint_len
);
3066 linux_init_signals ();
3067 linux_test_for_tracefork ();
3068 #ifdef HAVE_LINUX_REGSETS
3069 for (num_regsets
= 0; target_regsets
[num_regsets
].size
>= 0; num_regsets
++)
3071 disabled_regsets
= xmalloc (num_regsets
);
3074 sigchld_action
.sa_handler
= sigchld_handler
;
3075 sigemptyset (&sigchld_action
.sa_mask
);
3076 sigchld_action
.sa_flags
= SA_RESTART
;
3077 sigaction (SIGCHLD
, &sigchld_action
, NULL
);