1*56bb7041Schristos /* Low level interface to ptrace, for the remote server for GDB.
2*56bb7041Schristos Copyright (C) 1995-2020 Free Software Foundation, Inc.
3*56bb7041Schristos
4*56bb7041Schristos This file is part of GDB.
5*56bb7041Schristos
6*56bb7041Schristos This program is free software; you can redistribute it and/or modify
7*56bb7041Schristos it under the terms of the GNU General Public License as published by
8*56bb7041Schristos the Free Software Foundation; either version 3 of the License, or
9*56bb7041Schristos (at your option) any later version.
10*56bb7041Schristos
11*56bb7041Schristos This program is distributed in the hope that it will be useful,
12*56bb7041Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
13*56bb7041Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*56bb7041Schristos GNU General Public License for more details.
15*56bb7041Schristos
16*56bb7041Schristos You should have received a copy of the GNU General Public License
17*56bb7041Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */
18*56bb7041Schristos
19*56bb7041Schristos #include "server.h"
20*56bb7041Schristos #include "linux-low.h"
21*56bb7041Schristos #include "nat/linux-osdata.h"
22*56bb7041Schristos #include "gdbsupport/agent.h"
23*56bb7041Schristos #include "tdesc.h"
24*56bb7041Schristos #include "gdbsupport/rsp-low.h"
25*56bb7041Schristos #include "gdbsupport/signals-state-save-restore.h"
26*56bb7041Schristos #include "nat/linux-nat.h"
27*56bb7041Schristos #include "nat/linux-waitpid.h"
28*56bb7041Schristos #include "gdbsupport/gdb_wait.h"
29*56bb7041Schristos #include "nat/gdb_ptrace.h"
30*56bb7041Schristos #include "nat/linux-ptrace.h"
31*56bb7041Schristos #include "nat/linux-procfs.h"
32*56bb7041Schristos #include "nat/linux-personality.h"
33*56bb7041Schristos #include <signal.h>
34*56bb7041Schristos #include <sys/ioctl.h>
35*56bb7041Schristos #include <fcntl.h>
36*56bb7041Schristos #include <unistd.h>
37*56bb7041Schristos #include <sys/syscall.h>
38*56bb7041Schristos #include <sched.h>
39*56bb7041Schristos #include <ctype.h>
40*56bb7041Schristos #include <pwd.h>
41*56bb7041Schristos #include <sys/types.h>
42*56bb7041Schristos #include <dirent.h>
43*56bb7041Schristos #include <sys/stat.h>
44*56bb7041Schristos #include <sys/vfs.h>
45*56bb7041Schristos #include <sys/uio.h>
46*56bb7041Schristos #include "gdbsupport/filestuff.h"
47*56bb7041Schristos #include "tracepoint.h"
48*56bb7041Schristos #include <inttypes.h>
49*56bb7041Schristos #include "gdbsupport/common-inferior.h"
50*56bb7041Schristos #include "nat/fork-inferior.h"
51*56bb7041Schristos #include "gdbsupport/environ.h"
52*56bb7041Schristos #include "gdbsupport/gdb-sigmask.h"
53*56bb7041Schristos #include "gdbsupport/scoped_restore.h"
54*56bb7041Schristos #ifndef ELFMAG0
55*56bb7041Schristos /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
56*56bb7041Schristos then ELFMAG0 will have been defined. If it didn't get included by
57*56bb7041Schristos gdb_proc_service.h then including it will likely introduce a duplicate
58*56bb7041Schristos definition of elf_fpregset_t. */
59*56bb7041Schristos #include <elf.h>
60*56bb7041Schristos #endif
61*56bb7041Schristos #include "nat/linux-namespaces.h"
62*56bb7041Schristos
63*56bb7041Schristos #ifdef HAVE_PERSONALITY
64*56bb7041Schristos # include <sys/personality.h>
65*56bb7041Schristos # if !HAVE_DECL_ADDR_NO_RANDOMIZE
66*56bb7041Schristos # define ADDR_NO_RANDOMIZE 0x0040000
67*56bb7041Schristos # endif
68*56bb7041Schristos #endif
69*56bb7041Schristos
70*56bb7041Schristos #ifndef O_LARGEFILE
71*56bb7041Schristos #define O_LARGEFILE 0
72*56bb7041Schristos #endif
73*56bb7041Schristos
74*56bb7041Schristos #ifndef AT_HWCAP2
75*56bb7041Schristos #define AT_HWCAP2 26
76*56bb7041Schristos #endif
77*56bb7041Schristos
78*56bb7041Schristos /* Some targets did not define these ptrace constants from the start,
79*56bb7041Schristos so gdbserver defines them locally here. In the future, these may
80*56bb7041Schristos be removed after they are added to asm/ptrace.h. */
81*56bb7041Schristos #if !(defined(PT_TEXT_ADDR) \
82*56bb7041Schristos || defined(PT_DATA_ADDR) \
83*56bb7041Schristos || defined(PT_TEXT_END_ADDR))
84*56bb7041Schristos #if defined(__mcoldfire__)
85*56bb7041Schristos /* These are still undefined in 3.10 kernels. */
86*56bb7041Schristos #define PT_TEXT_ADDR 49*4
87*56bb7041Schristos #define PT_DATA_ADDR 50*4
88*56bb7041Schristos #define PT_TEXT_END_ADDR 51*4
89*56bb7041Schristos /* These are still undefined in 3.10 kernels. */
90*56bb7041Schristos #elif defined(__TMS320C6X__)
91*56bb7041Schristos #define PT_TEXT_ADDR (0x10000*4)
92*56bb7041Schristos #define PT_DATA_ADDR (0x10004*4)
93*56bb7041Schristos #define PT_TEXT_END_ADDR (0x10008*4)
94*56bb7041Schristos #endif
95*56bb7041Schristos #endif
96*56bb7041Schristos
97*56bb7041Schristos #if (defined(__UCLIBC__) \
98*56bb7041Schristos && defined(HAS_NOMMU) \
99*56bb7041Schristos && defined(PT_TEXT_ADDR) \
100*56bb7041Schristos && defined(PT_DATA_ADDR) \
101*56bb7041Schristos && defined(PT_TEXT_END_ADDR))
102*56bb7041Schristos #define SUPPORTS_READ_OFFSETS
103*56bb7041Schristos #endif
104*56bb7041Schristos
105*56bb7041Schristos #ifdef HAVE_LINUX_BTRACE
106*56bb7041Schristos # include "nat/linux-btrace.h"
107*56bb7041Schristos # include "gdbsupport/btrace-common.h"
108*56bb7041Schristos #endif
109*56bb7041Schristos
110*56bb7041Schristos #ifndef HAVE_ELF32_AUXV_T
111*56bb7041Schristos /* Copied from glibc's elf.h. */
112*56bb7041Schristos typedef struct
113*56bb7041Schristos {
114*56bb7041Schristos uint32_t a_type; /* Entry type */
115*56bb7041Schristos union
116*56bb7041Schristos {
117*56bb7041Schristos uint32_t a_val; /* Integer value */
118*56bb7041Schristos /* We use to have pointer elements added here. We cannot do that,
119*56bb7041Schristos though, since it does not work when using 32-bit definitions
120*56bb7041Schristos on 64-bit platforms and vice versa. */
121*56bb7041Schristos } a_un;
122*56bb7041Schristos } Elf32_auxv_t;
123*56bb7041Schristos #endif
124*56bb7041Schristos
125*56bb7041Schristos #ifndef HAVE_ELF64_AUXV_T
126*56bb7041Schristos /* Copied from glibc's elf.h. */
127*56bb7041Schristos typedef struct
128*56bb7041Schristos {
129*56bb7041Schristos uint64_t a_type; /* Entry type */
130*56bb7041Schristos union
131*56bb7041Schristos {
132*56bb7041Schristos uint64_t a_val; /* Integer value */
133*56bb7041Schristos /* We use to have pointer elements added here. We cannot do that,
134*56bb7041Schristos though, since it does not work when using 32-bit definitions
135*56bb7041Schristos on 64-bit platforms and vice versa. */
136*56bb7041Schristos } a_un;
137*56bb7041Schristos } Elf64_auxv_t;
138*56bb7041Schristos #endif
139*56bb7041Schristos
140*56bb7041Schristos /* Does the current host support PTRACE_GETREGSET? */
141*56bb7041Schristos int have_ptrace_getregset = -1;
142*56bb7041Schristos
143*56bb7041Schristos /* LWP accessors. */
144*56bb7041Schristos
145*56bb7041Schristos /* See nat/linux-nat.h. */
146*56bb7041Schristos
147*56bb7041Schristos ptid_t
ptid_of_lwp(struct lwp_info * lwp)148*56bb7041Schristos ptid_of_lwp (struct lwp_info *lwp)
149*56bb7041Schristos {
150*56bb7041Schristos return ptid_of (get_lwp_thread (lwp));
151*56bb7041Schristos }
152*56bb7041Schristos
153*56bb7041Schristos /* See nat/linux-nat.h. */
154*56bb7041Schristos
155*56bb7041Schristos void
lwp_set_arch_private_info(struct lwp_info * lwp,struct arch_lwp_info * info)156*56bb7041Schristos lwp_set_arch_private_info (struct lwp_info *lwp,
157*56bb7041Schristos struct arch_lwp_info *info)
158*56bb7041Schristos {
159*56bb7041Schristos lwp->arch_private = info;
160*56bb7041Schristos }
161*56bb7041Schristos
162*56bb7041Schristos /* See nat/linux-nat.h. */
163*56bb7041Schristos
164*56bb7041Schristos struct arch_lwp_info *
lwp_arch_private_info(struct lwp_info * lwp)165*56bb7041Schristos lwp_arch_private_info (struct lwp_info *lwp)
166*56bb7041Schristos {
167*56bb7041Schristos return lwp->arch_private;
168*56bb7041Schristos }
169*56bb7041Schristos
170*56bb7041Schristos /* See nat/linux-nat.h. */
171*56bb7041Schristos
172*56bb7041Schristos int
lwp_is_stopped(struct lwp_info * lwp)173*56bb7041Schristos lwp_is_stopped (struct lwp_info *lwp)
174*56bb7041Schristos {
175*56bb7041Schristos return lwp->stopped;
176*56bb7041Schristos }
177*56bb7041Schristos
178*56bb7041Schristos /* See nat/linux-nat.h. */
179*56bb7041Schristos
180*56bb7041Schristos enum target_stop_reason
lwp_stop_reason(struct lwp_info * lwp)181*56bb7041Schristos lwp_stop_reason (struct lwp_info *lwp)
182*56bb7041Schristos {
183*56bb7041Schristos return lwp->stop_reason;
184*56bb7041Schristos }
185*56bb7041Schristos
186*56bb7041Schristos /* See nat/linux-nat.h. */
187*56bb7041Schristos
188*56bb7041Schristos int
lwp_is_stepping(struct lwp_info * lwp)189*56bb7041Schristos lwp_is_stepping (struct lwp_info *lwp)
190*56bb7041Schristos {
191*56bb7041Schristos return lwp->stepping;
192*56bb7041Schristos }
193*56bb7041Schristos
194*56bb7041Schristos /* A list of all unknown processes which receive stop signals. Some
195*56bb7041Schristos other process will presumably claim each of these as forked
196*56bb7041Schristos children momentarily. */
197*56bb7041Schristos
198*56bb7041Schristos struct simple_pid_list
199*56bb7041Schristos {
200*56bb7041Schristos /* The process ID. */
201*56bb7041Schristos int pid;
202*56bb7041Schristos
203*56bb7041Schristos /* The status as reported by waitpid. */
204*56bb7041Schristos int status;
205*56bb7041Schristos
206*56bb7041Schristos /* Next in chain. */
207*56bb7041Schristos struct simple_pid_list *next;
208*56bb7041Schristos };
209*56bb7041Schristos static struct simple_pid_list *stopped_pids;
210*56bb7041Schristos
211*56bb7041Schristos /* Trivial list manipulation functions to keep track of a list of new
212*56bb7041Schristos stopped processes. */
213*56bb7041Schristos
214*56bb7041Schristos static void
add_to_pid_list(struct simple_pid_list ** listp,int pid,int status)215*56bb7041Schristos add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
216*56bb7041Schristos {
217*56bb7041Schristos struct simple_pid_list *new_pid = XNEW (struct simple_pid_list);
218*56bb7041Schristos
219*56bb7041Schristos new_pid->pid = pid;
220*56bb7041Schristos new_pid->status = status;
221*56bb7041Schristos new_pid->next = *listp;
222*56bb7041Schristos *listp = new_pid;
223*56bb7041Schristos }
224*56bb7041Schristos
225*56bb7041Schristos static int
pull_pid_from_list(struct simple_pid_list ** listp,int pid,int * statusp)226*56bb7041Schristos pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
227*56bb7041Schristos {
228*56bb7041Schristos struct simple_pid_list **p;
229*56bb7041Schristos
230*56bb7041Schristos for (p = listp; *p != NULL; p = &(*p)->next)
231*56bb7041Schristos if ((*p)->pid == pid)
232*56bb7041Schristos {
233*56bb7041Schristos struct simple_pid_list *next = (*p)->next;
234*56bb7041Schristos
235*56bb7041Schristos *statusp = (*p)->status;
236*56bb7041Schristos xfree (*p);
237*56bb7041Schristos *p = next;
238*56bb7041Schristos return 1;
239*56bb7041Schristos }
240*56bb7041Schristos return 0;
241*56bb7041Schristos }
242*56bb7041Schristos
243*56bb7041Schristos enum stopping_threads_kind
244*56bb7041Schristos {
245*56bb7041Schristos /* Not stopping threads presently. */
246*56bb7041Schristos NOT_STOPPING_THREADS,
247*56bb7041Schristos
248*56bb7041Schristos /* Stopping threads. */
249*56bb7041Schristos STOPPING_THREADS,
250*56bb7041Schristos
251*56bb7041Schristos /* Stopping and suspending threads. */
252*56bb7041Schristos STOPPING_AND_SUSPENDING_THREADS
253*56bb7041Schristos };
254*56bb7041Schristos
255*56bb7041Schristos /* This is set while stop_all_lwps is in effect. */
256*56bb7041Schristos enum stopping_threads_kind stopping_threads = NOT_STOPPING_THREADS;
257*56bb7041Schristos
258*56bb7041Schristos /* FIXME make into a target method? */
259*56bb7041Schristos int using_threads = 1;
260*56bb7041Schristos
261*56bb7041Schristos /* True if we're presently stabilizing threads (moving them out of
262*56bb7041Schristos jump pads). */
263*56bb7041Schristos static int stabilizing_threads;
264*56bb7041Schristos
265*56bb7041Schristos static void unsuspend_all_lwps (struct lwp_info *except);
266*56bb7041Schristos static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
267*56bb7041Schristos static int lwp_is_marked_dead (struct lwp_info *lwp);
268*56bb7041Schristos static int kill_lwp (unsigned long lwpid, int signo);
269*56bb7041Schristos static void enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info);
270*56bb7041Schristos static int linux_low_ptrace_options (int attached);
271*56bb7041Schristos static int check_ptrace_stopped_lwp_gone (struct lwp_info *lp);
272*56bb7041Schristos
273*56bb7041Schristos /* When the event-loop is doing a step-over, this points at the thread
274*56bb7041Schristos being stepped. */
275*56bb7041Schristos ptid_t step_over_bkpt;
276*56bb7041Schristos
277*56bb7041Schristos bool
low_supports_breakpoints()278*56bb7041Schristos linux_process_target::low_supports_breakpoints ()
279*56bb7041Schristos {
280*56bb7041Schristos return false;
281*56bb7041Schristos }
282*56bb7041Schristos
283*56bb7041Schristos CORE_ADDR
low_get_pc(regcache * regcache)284*56bb7041Schristos linux_process_target::low_get_pc (regcache *regcache)
285*56bb7041Schristos {
286*56bb7041Schristos return 0;
287*56bb7041Schristos }
288*56bb7041Schristos
289*56bb7041Schristos void
low_set_pc(regcache * regcache,CORE_ADDR newpc)290*56bb7041Schristos linux_process_target::low_set_pc (regcache *regcache, CORE_ADDR newpc)
291*56bb7041Schristos {
292*56bb7041Schristos gdb_assert_not_reached ("linux target op low_set_pc is not implemented");
293*56bb7041Schristos }
294*56bb7041Schristos
295*56bb7041Schristos std::vector<CORE_ADDR>
low_get_next_pcs(regcache * regcache)296*56bb7041Schristos linux_process_target::low_get_next_pcs (regcache *regcache)
297*56bb7041Schristos {
298*56bb7041Schristos gdb_assert_not_reached ("linux target op low_get_next_pcs is not "
299*56bb7041Schristos "implemented");
300*56bb7041Schristos }
301*56bb7041Schristos
302*56bb7041Schristos int
low_decr_pc_after_break()303*56bb7041Schristos linux_process_target::low_decr_pc_after_break ()
304*56bb7041Schristos {
305*56bb7041Schristos return 0;
306*56bb7041Schristos }
307*56bb7041Schristos
308*56bb7041Schristos /* True if LWP is stopped in its stepping range. */
309*56bb7041Schristos
310*56bb7041Schristos static int
lwp_in_step_range(struct lwp_info * lwp)311*56bb7041Schristos lwp_in_step_range (struct lwp_info *lwp)
312*56bb7041Schristos {
313*56bb7041Schristos CORE_ADDR pc = lwp->stop_pc;
314*56bb7041Schristos
315*56bb7041Schristos return (pc >= lwp->step_range_start && pc < lwp->step_range_end);
316*56bb7041Schristos }
317*56bb7041Schristos
318*56bb7041Schristos /* The read/write ends of the pipe registered as waitable file in the
319*56bb7041Schristos event loop. */
320*56bb7041Schristos static int linux_event_pipe[2] = { -1, -1 };
321*56bb7041Schristos
322*56bb7041Schristos /* True if we're currently in async mode. */
323*56bb7041Schristos #define target_is_async_p() (linux_event_pipe[0] != -1)
324*56bb7041Schristos
325*56bb7041Schristos static void send_sigstop (struct lwp_info *lwp);
326*56bb7041Schristos
327*56bb7041Schristos /* Return non-zero if HEADER is a 64-bit ELF file. */
328*56bb7041Schristos
329*56bb7041Schristos static int
elf_64_header_p(const Elf64_Ehdr * header,unsigned int * machine)330*56bb7041Schristos elf_64_header_p (const Elf64_Ehdr *header, unsigned int *machine)
331*56bb7041Schristos {
332*56bb7041Schristos if (header->e_ident[EI_MAG0] == ELFMAG0
333*56bb7041Schristos && header->e_ident[EI_MAG1] == ELFMAG1
334*56bb7041Schristos && header->e_ident[EI_MAG2] == ELFMAG2
335*56bb7041Schristos && header->e_ident[EI_MAG3] == ELFMAG3)
336*56bb7041Schristos {
337*56bb7041Schristos *machine = header->e_machine;
338*56bb7041Schristos return header->e_ident[EI_CLASS] == ELFCLASS64;
339*56bb7041Schristos
340*56bb7041Schristos }
341*56bb7041Schristos *machine = EM_NONE;
342*56bb7041Schristos return -1;
343*56bb7041Schristos }
344*56bb7041Schristos
345*56bb7041Schristos /* Return non-zero if FILE is a 64-bit ELF file,
346*56bb7041Schristos zero if the file is not a 64-bit ELF file,
347*56bb7041Schristos and -1 if the file is not accessible or doesn't exist. */
348*56bb7041Schristos
349*56bb7041Schristos static int
elf_64_file_p(const char * file,unsigned int * machine)350*56bb7041Schristos elf_64_file_p (const char *file, unsigned int *machine)
351*56bb7041Schristos {
352*56bb7041Schristos Elf64_Ehdr header;
353*56bb7041Schristos int fd;
354*56bb7041Schristos
355*56bb7041Schristos fd = open (file, O_RDONLY);
356*56bb7041Schristos if (fd < 0)
357*56bb7041Schristos return -1;
358*56bb7041Schristos
359*56bb7041Schristos if (read (fd, &header, sizeof (header)) != sizeof (header))
360*56bb7041Schristos {
361*56bb7041Schristos close (fd);
362*56bb7041Schristos return 0;
363*56bb7041Schristos }
364*56bb7041Schristos close (fd);
365*56bb7041Schristos
366*56bb7041Schristos return elf_64_header_p (&header, machine);
367*56bb7041Schristos }
368*56bb7041Schristos
369*56bb7041Schristos /* Accepts an integer PID; Returns true if the executable PID is
370*56bb7041Schristos running is a 64-bit ELF file.. */
371*56bb7041Schristos
372*56bb7041Schristos int
linux_pid_exe_is_elf_64_file(int pid,unsigned int * machine)373*56bb7041Schristos linux_pid_exe_is_elf_64_file (int pid, unsigned int *machine)
374*56bb7041Schristos {
375*56bb7041Schristos char file[PATH_MAX];
376*56bb7041Schristos
377*56bb7041Schristos sprintf (file, "/proc/%d/exe", pid);
378*56bb7041Schristos return elf_64_file_p (file, machine);
379*56bb7041Schristos }
380*56bb7041Schristos
381*56bb7041Schristos void
delete_lwp(lwp_info * lwp)382*56bb7041Schristos linux_process_target::delete_lwp (lwp_info *lwp)
383*56bb7041Schristos {
384*56bb7041Schristos struct thread_info *thr = get_lwp_thread (lwp);
385*56bb7041Schristos
386*56bb7041Schristos if (debug_threads)
387*56bb7041Schristos debug_printf ("deleting %ld\n", lwpid_of (thr));
388*56bb7041Schristos
389*56bb7041Schristos remove_thread (thr);
390*56bb7041Schristos
391*56bb7041Schristos low_delete_thread (lwp->arch_private);
392*56bb7041Schristos
393*56bb7041Schristos delete lwp;
394*56bb7041Schristos }
395*56bb7041Schristos
396*56bb7041Schristos void
low_delete_thread(arch_lwp_info * info)397*56bb7041Schristos linux_process_target::low_delete_thread (arch_lwp_info *info)
398*56bb7041Schristos {
399*56bb7041Schristos /* Default implementation should be overridden if architecture-specific
400*56bb7041Schristos info is being used. */
401*56bb7041Schristos gdb_assert (info == nullptr);
402*56bb7041Schristos }
403*56bb7041Schristos
404*56bb7041Schristos process_info *
add_linux_process(int pid,int attached)405*56bb7041Schristos linux_process_target::add_linux_process (int pid, int attached)
406*56bb7041Schristos {
407*56bb7041Schristos struct process_info *proc;
408*56bb7041Schristos
409*56bb7041Schristos proc = add_process (pid, attached);
410*56bb7041Schristos proc->priv = XCNEW (struct process_info_private);
411*56bb7041Schristos
412*56bb7041Schristos proc->priv->arch_private = low_new_process ();
413*56bb7041Schristos
414*56bb7041Schristos return proc;
415*56bb7041Schristos }
416*56bb7041Schristos
417*56bb7041Schristos arch_process_info *
low_new_process()418*56bb7041Schristos linux_process_target::low_new_process ()
419*56bb7041Schristos {
420*56bb7041Schristos return nullptr;
421*56bb7041Schristos }
422*56bb7041Schristos
423*56bb7041Schristos void
low_delete_process(arch_process_info * info)424*56bb7041Schristos linux_process_target::low_delete_process (arch_process_info *info)
425*56bb7041Schristos {
426*56bb7041Schristos /* Default implementation must be overridden if architecture-specific
427*56bb7041Schristos info exists. */
428*56bb7041Schristos gdb_assert (info == nullptr);
429*56bb7041Schristos }
430*56bb7041Schristos
431*56bb7041Schristos void
low_new_fork(process_info * parent,process_info * child)432*56bb7041Schristos linux_process_target::low_new_fork (process_info *parent, process_info *child)
433*56bb7041Schristos {
434*56bb7041Schristos /* Nop. */
435*56bb7041Schristos }
436*56bb7041Schristos
437*56bb7041Schristos void
arch_setup_thread(thread_info * thread)438*56bb7041Schristos linux_process_target::arch_setup_thread (thread_info *thread)
439*56bb7041Schristos {
440*56bb7041Schristos struct thread_info *saved_thread;
441*56bb7041Schristos
442*56bb7041Schristos saved_thread = current_thread;
443*56bb7041Schristos current_thread = thread;
444*56bb7041Schristos
445*56bb7041Schristos low_arch_setup ();
446*56bb7041Schristos
447*56bb7041Schristos current_thread = saved_thread;
448*56bb7041Schristos }
449*56bb7041Schristos
450*56bb7041Schristos int
handle_extended_wait(lwp_info ** orig_event_lwp,int wstat)451*56bb7041Schristos linux_process_target::handle_extended_wait (lwp_info **orig_event_lwp,
452*56bb7041Schristos int wstat)
453*56bb7041Schristos {
454*56bb7041Schristos client_state &cs = get_client_state ();
455*56bb7041Schristos struct lwp_info *event_lwp = *orig_event_lwp;
456*56bb7041Schristos int event = linux_ptrace_get_extended_event (wstat);
457*56bb7041Schristos struct thread_info *event_thr = get_lwp_thread (event_lwp);
458*56bb7041Schristos struct lwp_info *new_lwp;
459*56bb7041Schristos
460*56bb7041Schristos gdb_assert (event_lwp->waitstatus.kind == TARGET_WAITKIND_IGNORE);
461*56bb7041Schristos
462*56bb7041Schristos /* All extended events we currently use are mid-syscall. Only
463*56bb7041Schristos PTRACE_EVENT_STOP is delivered more like a signal-stop, but
464*56bb7041Schristos you have to be using PTRACE_SEIZE to get that. */
465*56bb7041Schristos event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
466*56bb7041Schristos
467*56bb7041Schristos if ((event == PTRACE_EVENT_FORK) || (event == PTRACE_EVENT_VFORK)
468*56bb7041Schristos || (event == PTRACE_EVENT_CLONE))
469*56bb7041Schristos {
470*56bb7041Schristos ptid_t ptid;
471*56bb7041Schristos unsigned long new_pid;
472*56bb7041Schristos int ret, status;
473*56bb7041Schristos
474*56bb7041Schristos /* Get the pid of the new lwp. */
475*56bb7041Schristos ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_thr), (PTRACE_TYPE_ARG3) 0,
476*56bb7041Schristos &new_pid);
477*56bb7041Schristos
478*56bb7041Schristos /* If we haven't already seen the new PID stop, wait for it now. */
479*56bb7041Schristos if (!pull_pid_from_list (&stopped_pids, new_pid, &status))
480*56bb7041Schristos {
481*56bb7041Schristos /* The new child has a pending SIGSTOP. We can't affect it until it
482*56bb7041Schristos hits the SIGSTOP, but we're already attached. */
483*56bb7041Schristos
484*56bb7041Schristos ret = my_waitpid (new_pid, &status, __WALL);
485*56bb7041Schristos
486*56bb7041Schristos if (ret == -1)
487*56bb7041Schristos perror_with_name ("waiting for new child");
488*56bb7041Schristos else if (ret != new_pid)
489*56bb7041Schristos warning ("wait returned unexpected PID %d", ret);
490*56bb7041Schristos else if (!WIFSTOPPED (status))
491*56bb7041Schristos warning ("wait returned unexpected status 0x%x", status);
492*56bb7041Schristos }
493*56bb7041Schristos
494*56bb7041Schristos if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
495*56bb7041Schristos {
496*56bb7041Schristos struct process_info *parent_proc;
497*56bb7041Schristos struct process_info *child_proc;
498*56bb7041Schristos struct lwp_info *child_lwp;
499*56bb7041Schristos struct thread_info *child_thr;
500*56bb7041Schristos struct target_desc *tdesc;
501*56bb7041Schristos
502*56bb7041Schristos ptid = ptid_t (new_pid, new_pid, 0);
503*56bb7041Schristos
504*56bb7041Schristos if (debug_threads)
505*56bb7041Schristos {
506*56bb7041Schristos debug_printf ("HEW: Got fork event from LWP %ld, "
507*56bb7041Schristos "new child is %d\n",
508*56bb7041Schristos ptid_of (event_thr).lwp (),
509*56bb7041Schristos ptid.pid ());
510*56bb7041Schristos }
511*56bb7041Schristos
512*56bb7041Schristos /* Add the new process to the tables and clone the breakpoint
513*56bb7041Schristos lists of the parent. We need to do this even if the new process
514*56bb7041Schristos will be detached, since we will need the process object and the
515*56bb7041Schristos breakpoints to remove any breakpoints from memory when we
516*56bb7041Schristos detach, and the client side will access registers. */
517*56bb7041Schristos child_proc = add_linux_process (new_pid, 0);
518*56bb7041Schristos gdb_assert (child_proc != NULL);
519*56bb7041Schristos child_lwp = add_lwp (ptid);
520*56bb7041Schristos gdb_assert (child_lwp != NULL);
521*56bb7041Schristos child_lwp->stopped = 1;
522*56bb7041Schristos child_lwp->must_set_ptrace_flags = 1;
523*56bb7041Schristos child_lwp->status_pending_p = 0;
524*56bb7041Schristos child_thr = get_lwp_thread (child_lwp);
525*56bb7041Schristos child_thr->last_resume_kind = resume_stop;
526*56bb7041Schristos child_thr->last_status.kind = TARGET_WAITKIND_STOPPED;
527*56bb7041Schristos
528*56bb7041Schristos /* If we're suspending all threads, leave this one suspended
529*56bb7041Schristos too. If the fork/clone parent is stepping over a breakpoint,
530*56bb7041Schristos all other threads have been suspended already. Leave the
531*56bb7041Schristos child suspended too. */
532*56bb7041Schristos if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
533*56bb7041Schristos || event_lwp->bp_reinsert != 0)
534*56bb7041Schristos {
535*56bb7041Schristos if (debug_threads)
536*56bb7041Schristos debug_printf ("HEW: leaving child suspended\n");
537*56bb7041Schristos child_lwp->suspended = 1;
538*56bb7041Schristos }
539*56bb7041Schristos
540*56bb7041Schristos parent_proc = get_thread_process (event_thr);
541*56bb7041Schristos child_proc->attached = parent_proc->attached;
542*56bb7041Schristos
543*56bb7041Schristos if (event_lwp->bp_reinsert != 0
544*56bb7041Schristos && supports_software_single_step ()
545*56bb7041Schristos && event == PTRACE_EVENT_VFORK)
546*56bb7041Schristos {
547*56bb7041Schristos /* If we leave single-step breakpoints there, child will
548*56bb7041Schristos hit it, so uninsert single-step breakpoints from parent
549*56bb7041Schristos (and child). Once vfork child is done, reinsert
550*56bb7041Schristos them back to parent. */
551*56bb7041Schristos uninsert_single_step_breakpoints (event_thr);
552*56bb7041Schristos }
553*56bb7041Schristos
554*56bb7041Schristos clone_all_breakpoints (child_thr, event_thr);
555*56bb7041Schristos
556*56bb7041Schristos tdesc = allocate_target_description ();
557*56bb7041Schristos copy_target_description (tdesc, parent_proc->tdesc);
558*56bb7041Schristos child_proc->tdesc = tdesc;
559*56bb7041Schristos
560*56bb7041Schristos /* Clone arch-specific process data. */
561*56bb7041Schristos low_new_fork (parent_proc, child_proc);
562*56bb7041Schristos
563*56bb7041Schristos /* Save fork info in the parent thread. */
564*56bb7041Schristos if (event == PTRACE_EVENT_FORK)
565*56bb7041Schristos event_lwp->waitstatus.kind = TARGET_WAITKIND_FORKED;
566*56bb7041Schristos else if (event == PTRACE_EVENT_VFORK)
567*56bb7041Schristos event_lwp->waitstatus.kind = TARGET_WAITKIND_VFORKED;
568*56bb7041Schristos
569*56bb7041Schristos event_lwp->waitstatus.value.related_pid = ptid;
570*56bb7041Schristos
571*56bb7041Schristos /* The status_pending field contains bits denoting the
572*56bb7041Schristos extended event, so when the pending event is handled,
573*56bb7041Schristos the handler will look at lwp->waitstatus. */
574*56bb7041Schristos event_lwp->status_pending_p = 1;
575*56bb7041Schristos event_lwp->status_pending = wstat;
576*56bb7041Schristos
577*56bb7041Schristos /* Link the threads until the parent event is passed on to
578*56bb7041Schristos higher layers. */
579*56bb7041Schristos event_lwp->fork_relative = child_lwp;
580*56bb7041Schristos child_lwp->fork_relative = event_lwp;
581*56bb7041Schristos
582*56bb7041Schristos /* If the parent thread is doing step-over with single-step
583*56bb7041Schristos breakpoints, the list of single-step breakpoints are cloned
584*56bb7041Schristos from the parent's. Remove them from the child process.
585*56bb7041Schristos In case of vfork, we'll reinsert them back once vforked
586*56bb7041Schristos child is done. */
587*56bb7041Schristos if (event_lwp->bp_reinsert != 0
588*56bb7041Schristos && supports_software_single_step ())
589*56bb7041Schristos {
590*56bb7041Schristos /* The child process is forked and stopped, so it is safe
591*56bb7041Schristos to access its memory without stopping all other threads
592*56bb7041Schristos from other processes. */
593*56bb7041Schristos delete_single_step_breakpoints (child_thr);
594*56bb7041Schristos
595*56bb7041Schristos gdb_assert (has_single_step_breakpoints (event_thr));
596*56bb7041Schristos gdb_assert (!has_single_step_breakpoints (child_thr));
597*56bb7041Schristos }
598*56bb7041Schristos
599*56bb7041Schristos /* Report the event. */
600*56bb7041Schristos return 0;
601*56bb7041Schristos }
602*56bb7041Schristos
603*56bb7041Schristos if (debug_threads)
604*56bb7041Schristos debug_printf ("HEW: Got clone event "
605*56bb7041Schristos "from LWP %ld, new child is LWP %ld\n",
606*56bb7041Schristos lwpid_of (event_thr), new_pid);
607*56bb7041Schristos
608*56bb7041Schristos ptid = ptid_t (pid_of (event_thr), new_pid, 0);
609*56bb7041Schristos new_lwp = add_lwp (ptid);
610*56bb7041Schristos
611*56bb7041Schristos /* Either we're going to immediately resume the new thread
612*56bb7041Schristos or leave it stopped. resume_one_lwp is a nop if it
613*56bb7041Schristos thinks the thread is currently running, so set this first
614*56bb7041Schristos before calling resume_one_lwp. */
615*56bb7041Schristos new_lwp->stopped = 1;
616*56bb7041Schristos
617*56bb7041Schristos /* If we're suspending all threads, leave this one suspended
618*56bb7041Schristos too. If the fork/clone parent is stepping over a breakpoint,
619*56bb7041Schristos all other threads have been suspended already. Leave the
620*56bb7041Schristos child suspended too. */
621*56bb7041Schristos if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
622*56bb7041Schristos || event_lwp->bp_reinsert != 0)
623*56bb7041Schristos new_lwp->suspended = 1;
624*56bb7041Schristos
625*56bb7041Schristos /* Normally we will get the pending SIGSTOP. But in some cases
626*56bb7041Schristos we might get another signal delivered to the group first.
627*56bb7041Schristos If we do get another signal, be sure not to lose it. */
628*56bb7041Schristos if (WSTOPSIG (status) != SIGSTOP)
629*56bb7041Schristos {
630*56bb7041Schristos new_lwp->stop_expected = 1;
631*56bb7041Schristos new_lwp->status_pending_p = 1;
632*56bb7041Schristos new_lwp->status_pending = status;
633*56bb7041Schristos }
634*56bb7041Schristos else if (cs.report_thread_events)
635*56bb7041Schristos {
636*56bb7041Schristos new_lwp->waitstatus.kind = TARGET_WAITKIND_THREAD_CREATED;
637*56bb7041Schristos new_lwp->status_pending_p = 1;
638*56bb7041Schristos new_lwp->status_pending = status;
639*56bb7041Schristos }
640*56bb7041Schristos
641*56bb7041Schristos #ifdef USE_THREAD_DB
642*56bb7041Schristos thread_db_notice_clone (event_thr, ptid);
643*56bb7041Schristos #endif
644*56bb7041Schristos
645*56bb7041Schristos /* Don't report the event. */
646*56bb7041Schristos return 1;
647*56bb7041Schristos }
648*56bb7041Schristos else if (event == PTRACE_EVENT_VFORK_DONE)
649*56bb7041Schristos {
650*56bb7041Schristos event_lwp->waitstatus.kind = TARGET_WAITKIND_VFORK_DONE;
651*56bb7041Schristos
652*56bb7041Schristos if (event_lwp->bp_reinsert != 0 && supports_software_single_step ())
653*56bb7041Schristos {
654*56bb7041Schristos reinsert_single_step_breakpoints (event_thr);
655*56bb7041Schristos
656*56bb7041Schristos gdb_assert (has_single_step_breakpoints (event_thr));
657*56bb7041Schristos }
658*56bb7041Schristos
659*56bb7041Schristos /* Report the event. */
660*56bb7041Schristos return 0;
661*56bb7041Schristos }
662*56bb7041Schristos else if (event == PTRACE_EVENT_EXEC && cs.report_exec_events)
663*56bb7041Schristos {
664*56bb7041Schristos struct process_info *proc;
665*56bb7041Schristos std::vector<int> syscalls_to_catch;
666*56bb7041Schristos ptid_t event_ptid;
667*56bb7041Schristos pid_t event_pid;
668*56bb7041Schristos
669*56bb7041Schristos if (debug_threads)
670*56bb7041Schristos {
671*56bb7041Schristos debug_printf ("HEW: Got exec event from LWP %ld\n",
672*56bb7041Schristos lwpid_of (event_thr));
673*56bb7041Schristos }
674*56bb7041Schristos
675*56bb7041Schristos /* Get the event ptid. */
676*56bb7041Schristos event_ptid = ptid_of (event_thr);
677*56bb7041Schristos event_pid = event_ptid.pid ();
678*56bb7041Schristos
679*56bb7041Schristos /* Save the syscall list from the execing process. */
680*56bb7041Schristos proc = get_thread_process (event_thr);
681*56bb7041Schristos syscalls_to_catch = std::move (proc->syscalls_to_catch);
682*56bb7041Schristos
683*56bb7041Schristos /* Delete the execing process and all its threads. */
684*56bb7041Schristos mourn (proc);
685*56bb7041Schristos current_thread = NULL;
686*56bb7041Schristos
687*56bb7041Schristos /* Create a new process/lwp/thread. */
688*56bb7041Schristos proc = add_linux_process (event_pid, 0);
689*56bb7041Schristos event_lwp = add_lwp (event_ptid);
690*56bb7041Schristos event_thr = get_lwp_thread (event_lwp);
691*56bb7041Schristos gdb_assert (current_thread == event_thr);
692*56bb7041Schristos arch_setup_thread (event_thr);
693*56bb7041Schristos
694*56bb7041Schristos /* Set the event status. */
695*56bb7041Schristos event_lwp->waitstatus.kind = TARGET_WAITKIND_EXECD;
696*56bb7041Schristos event_lwp->waitstatus.value.execd_pathname
697*56bb7041Schristos = xstrdup (linux_proc_pid_to_exec_file (lwpid_of (event_thr)));
698*56bb7041Schristos
699*56bb7041Schristos /* Mark the exec status as pending. */
700*56bb7041Schristos event_lwp->stopped = 1;
701*56bb7041Schristos event_lwp->status_pending_p = 1;
702*56bb7041Schristos event_lwp->status_pending = wstat;
703*56bb7041Schristos event_thr->last_resume_kind = resume_continue;
704*56bb7041Schristos event_thr->last_status.kind = TARGET_WAITKIND_IGNORE;
705*56bb7041Schristos
706*56bb7041Schristos /* Update syscall state in the new lwp, effectively mid-syscall too. */
707*56bb7041Schristos event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
708*56bb7041Schristos
709*56bb7041Schristos /* Restore the list to catch. Don't rely on the client, which is free
710*56bb7041Schristos to avoid sending a new list when the architecture doesn't change.
711*56bb7041Schristos Also, for ANY_SYSCALL, the architecture doesn't really matter. */
712*56bb7041Schristos proc->syscalls_to_catch = std::move (syscalls_to_catch);
713*56bb7041Schristos
714*56bb7041Schristos /* Report the event. */
715*56bb7041Schristos *orig_event_lwp = event_lwp;
716*56bb7041Schristos return 0;
717*56bb7041Schristos }
718*56bb7041Schristos
719*56bb7041Schristos internal_error (__FILE__, __LINE__, _("unknown ptrace event %d"), event);
720*56bb7041Schristos }
721*56bb7041Schristos
722*56bb7041Schristos CORE_ADDR
get_pc(lwp_info * lwp)723*56bb7041Schristos linux_process_target::get_pc (lwp_info *lwp)
724*56bb7041Schristos {
725*56bb7041Schristos struct thread_info *saved_thread;
726*56bb7041Schristos struct regcache *regcache;
727*56bb7041Schristos CORE_ADDR pc;
728*56bb7041Schristos
729*56bb7041Schristos if (!low_supports_breakpoints ())
730*56bb7041Schristos return 0;
731*56bb7041Schristos
732*56bb7041Schristos saved_thread = current_thread;
733*56bb7041Schristos current_thread = get_lwp_thread (lwp);
734*56bb7041Schristos
735*56bb7041Schristos regcache = get_thread_regcache (current_thread, 1);
736*56bb7041Schristos pc = low_get_pc (regcache);
737*56bb7041Schristos
738*56bb7041Schristos if (debug_threads)
739*56bb7041Schristos debug_printf ("pc is 0x%lx\n", (long) pc);
740*56bb7041Schristos
741*56bb7041Schristos current_thread = saved_thread;
742*56bb7041Schristos return pc;
743*56bb7041Schristos }
744*56bb7041Schristos
745*56bb7041Schristos void
get_syscall_trapinfo(lwp_info * lwp,int * sysno)746*56bb7041Schristos linux_process_target::get_syscall_trapinfo (lwp_info *lwp, int *sysno)
747*56bb7041Schristos {
748*56bb7041Schristos struct thread_info *saved_thread;
749*56bb7041Schristos struct regcache *regcache;
750*56bb7041Schristos
751*56bb7041Schristos saved_thread = current_thread;
752*56bb7041Schristos current_thread = get_lwp_thread (lwp);
753*56bb7041Schristos
754*56bb7041Schristos regcache = get_thread_regcache (current_thread, 1);
755*56bb7041Schristos low_get_syscall_trapinfo (regcache, sysno);
756*56bb7041Schristos
757*56bb7041Schristos if (debug_threads)
758*56bb7041Schristos debug_printf ("get_syscall_trapinfo sysno %d\n", *sysno);
759*56bb7041Schristos
760*56bb7041Schristos current_thread = saved_thread;
761*56bb7041Schristos }
762*56bb7041Schristos
763*56bb7041Schristos void
low_get_syscall_trapinfo(regcache * regcache,int * sysno)764*56bb7041Schristos linux_process_target::low_get_syscall_trapinfo (regcache *regcache, int *sysno)
765*56bb7041Schristos {
766*56bb7041Schristos /* By default, report an unknown system call number. */
767*56bb7041Schristos *sysno = UNKNOWN_SYSCALL;
768*56bb7041Schristos }
769*56bb7041Schristos
770*56bb7041Schristos bool
save_stop_reason(lwp_info * lwp)771*56bb7041Schristos linux_process_target::save_stop_reason (lwp_info *lwp)
772*56bb7041Schristos {
773*56bb7041Schristos CORE_ADDR pc;
774*56bb7041Schristos CORE_ADDR sw_breakpoint_pc;
775*56bb7041Schristos struct thread_info *saved_thread;
776*56bb7041Schristos #if USE_SIGTRAP_SIGINFO
777*56bb7041Schristos siginfo_t siginfo;
778*56bb7041Schristos #endif
779*56bb7041Schristos
780*56bb7041Schristos if (!low_supports_breakpoints ())
781*56bb7041Schristos return false;
782*56bb7041Schristos
783*56bb7041Schristos pc = get_pc (lwp);
784*56bb7041Schristos sw_breakpoint_pc = pc - low_decr_pc_after_break ();
785*56bb7041Schristos
786*56bb7041Schristos /* breakpoint_at reads from the current thread. */
787*56bb7041Schristos saved_thread = current_thread;
788*56bb7041Schristos current_thread = get_lwp_thread (lwp);
789*56bb7041Schristos
790*56bb7041Schristos #if USE_SIGTRAP_SIGINFO
791*56bb7041Schristos if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
792*56bb7041Schristos (PTRACE_TYPE_ARG3) 0, &siginfo) == 0)
793*56bb7041Schristos {
794*56bb7041Schristos if (siginfo.si_signo == SIGTRAP)
795*56bb7041Schristos {
796*56bb7041Schristos if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)
797*56bb7041Schristos && GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
798*56bb7041Schristos {
799*56bb7041Schristos /* The si_code is ambiguous on this arch -- check debug
800*56bb7041Schristos registers. */
801*56bb7041Schristos if (!check_stopped_by_watchpoint (lwp))
802*56bb7041Schristos lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
803*56bb7041Schristos }
804*56bb7041Schristos else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
805*56bb7041Schristos {
806*56bb7041Schristos /* If we determine the LWP stopped for a SW breakpoint,
807*56bb7041Schristos trust it. Particularly don't check watchpoint
808*56bb7041Schristos registers, because at least on s390, we'd find
809*56bb7041Schristos stopped-by-watchpoint as long as there's a watchpoint
810*56bb7041Schristos set. */
811*56bb7041Schristos lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
812*56bb7041Schristos }
813*56bb7041Schristos else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
814*56bb7041Schristos {
815*56bb7041Schristos /* This can indicate either a hardware breakpoint or
816*56bb7041Schristos hardware watchpoint. Check debug registers. */
817*56bb7041Schristos if (!check_stopped_by_watchpoint (lwp))
818*56bb7041Schristos lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
819*56bb7041Schristos }
820*56bb7041Schristos else if (siginfo.si_code == TRAP_TRACE)
821*56bb7041Schristos {
822*56bb7041Schristos /* We may have single stepped an instruction that
823*56bb7041Schristos triggered a watchpoint. In that case, on some
824*56bb7041Schristos architectures (such as x86), instead of TRAP_HWBKPT,
825*56bb7041Schristos si_code indicates TRAP_TRACE, and we need to check
826*56bb7041Schristos the debug registers separately. */
827*56bb7041Schristos if (!check_stopped_by_watchpoint (lwp))
828*56bb7041Schristos lwp->stop_reason = TARGET_STOPPED_BY_SINGLE_STEP;
829*56bb7041Schristos }
830*56bb7041Schristos }
831*56bb7041Schristos }
832*56bb7041Schristos #else
833*56bb7041Schristos /* We may have just stepped a breakpoint instruction. E.g., in
834*56bb7041Schristos non-stop mode, GDB first tells the thread A to step a range, and
835*56bb7041Schristos then the user inserts a breakpoint inside the range. In that
836*56bb7041Schristos case we need to report the breakpoint PC. */
837*56bb7041Schristos if ((!lwp->stepping || lwp->stop_pc == sw_breakpoint_pc)
838*56bb7041Schristos && low_breakpoint_at (sw_breakpoint_pc))
839*56bb7041Schristos lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
840*56bb7041Schristos
841*56bb7041Schristos if (hardware_breakpoint_inserted_here (pc))
842*56bb7041Schristos lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
843*56bb7041Schristos
844*56bb7041Schristos if (lwp->stop_reason == TARGET_STOPPED_BY_NO_REASON)
845*56bb7041Schristos check_stopped_by_watchpoint (lwp);
846*56bb7041Schristos #endif
847*56bb7041Schristos
848*56bb7041Schristos if (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT)
849*56bb7041Schristos {
850*56bb7041Schristos if (debug_threads)
851*56bb7041Schristos {
852*56bb7041Schristos struct thread_info *thr = get_lwp_thread (lwp);
853*56bb7041Schristos
854*56bb7041Schristos debug_printf ("CSBB: %s stopped by software breakpoint\n",
855*56bb7041Schristos target_pid_to_str (ptid_of (thr)));
856*56bb7041Schristos }
857*56bb7041Schristos
858*56bb7041Schristos /* Back up the PC if necessary. */
859*56bb7041Schristos if (pc != sw_breakpoint_pc)
860*56bb7041Schristos {
861*56bb7041Schristos struct regcache *regcache
862*56bb7041Schristos = get_thread_regcache (current_thread, 1);
863*56bb7041Schristos low_set_pc (regcache, sw_breakpoint_pc);
864*56bb7041Schristos }
865*56bb7041Schristos
866*56bb7041Schristos /* Update this so we record the correct stop PC below. */
867*56bb7041Schristos pc = sw_breakpoint_pc;
868*56bb7041Schristos }
869*56bb7041Schristos else if (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
870*56bb7041Schristos {
871*56bb7041Schristos if (debug_threads)
872*56bb7041Schristos {
873*56bb7041Schristos struct thread_info *thr = get_lwp_thread (lwp);
874*56bb7041Schristos
875*56bb7041Schristos debug_printf ("CSBB: %s stopped by hardware breakpoint\n",
876*56bb7041Schristos target_pid_to_str (ptid_of (thr)));
877*56bb7041Schristos }
878*56bb7041Schristos }
879*56bb7041Schristos else if (lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
880*56bb7041Schristos {
881*56bb7041Schristos if (debug_threads)
882*56bb7041Schristos {
883*56bb7041Schristos struct thread_info *thr = get_lwp_thread (lwp);
884*56bb7041Schristos
885*56bb7041Schristos debug_printf ("CSBB: %s stopped by hardware watchpoint\n",
886*56bb7041Schristos target_pid_to_str (ptid_of (thr)));
887*56bb7041Schristos }
888*56bb7041Schristos }
889*56bb7041Schristos else if (lwp->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP)
890*56bb7041Schristos {
891*56bb7041Schristos if (debug_threads)
892*56bb7041Schristos {
893*56bb7041Schristos struct thread_info *thr = get_lwp_thread (lwp);
894*56bb7041Schristos
895*56bb7041Schristos debug_printf ("CSBB: %s stopped by trace\n",
896*56bb7041Schristos target_pid_to_str (ptid_of (thr)));
897*56bb7041Schristos }
898*56bb7041Schristos }
899*56bb7041Schristos
900*56bb7041Schristos lwp->stop_pc = pc;
901*56bb7041Schristos current_thread = saved_thread;
902*56bb7041Schristos return true;
903*56bb7041Schristos }
904*56bb7041Schristos
905*56bb7041Schristos lwp_info *
add_lwp(ptid_t ptid)906*56bb7041Schristos linux_process_target::add_lwp (ptid_t ptid)
907*56bb7041Schristos {
908*56bb7041Schristos struct lwp_info *lwp;
909*56bb7041Schristos
910*56bb7041Schristos lwp = new lwp_info {};
911*56bb7041Schristos
912*56bb7041Schristos lwp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
913*56bb7041Schristos
914*56bb7041Schristos lwp->thread = add_thread (ptid, lwp);
915*56bb7041Schristos
916*56bb7041Schristos low_new_thread (lwp);
917*56bb7041Schristos
918*56bb7041Schristos return lwp;
919*56bb7041Schristos }
920*56bb7041Schristos
921*56bb7041Schristos void
low_new_thread(lwp_info * info)922*56bb7041Schristos linux_process_target::low_new_thread (lwp_info *info)
923*56bb7041Schristos {
924*56bb7041Schristos /* Nop. */
925*56bb7041Schristos }
926*56bb7041Schristos
927*56bb7041Schristos /* Callback to be used when calling fork_inferior, responsible for
928*56bb7041Schristos actually initiating the tracing of the inferior. */
929*56bb7041Schristos
930*56bb7041Schristos static void
linux_ptrace_fun()931*56bb7041Schristos linux_ptrace_fun ()
932*56bb7041Schristos {
933*56bb7041Schristos if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
934*56bb7041Schristos (PTRACE_TYPE_ARG4) 0) < 0)
935*56bb7041Schristos trace_start_error_with_name ("ptrace");
936*56bb7041Schristos
937*56bb7041Schristos if (setpgid (0, 0) < 0)
938*56bb7041Schristos trace_start_error_with_name ("setpgid");
939*56bb7041Schristos
940*56bb7041Schristos /* If GDBserver is connected to gdb via stdio, redirect the inferior's
941*56bb7041Schristos stdout to stderr so that inferior i/o doesn't corrupt the connection.
942*56bb7041Schristos Also, redirect stdin to /dev/null. */
943*56bb7041Schristos if (remote_connection_is_stdio ())
944*56bb7041Schristos {
945*56bb7041Schristos if (close (0) < 0)
946*56bb7041Schristos trace_start_error_with_name ("close");
947*56bb7041Schristos if (open ("/dev/null", O_RDONLY) < 0)
948*56bb7041Schristos trace_start_error_with_name ("open");
949*56bb7041Schristos if (dup2 (2, 1) < 0)
950*56bb7041Schristos trace_start_error_with_name ("dup2");
951*56bb7041Schristos if (write (2, "stdin/stdout redirected\n",
952*56bb7041Schristos sizeof ("stdin/stdout redirected\n") - 1) < 0)
953*56bb7041Schristos {
954*56bb7041Schristos /* Errors ignored. */;
955*56bb7041Schristos }
956*56bb7041Schristos }
957*56bb7041Schristos }
958*56bb7041Schristos
959*56bb7041Schristos /* Start an inferior process and returns its pid.
960*56bb7041Schristos PROGRAM is the name of the program to be started, and PROGRAM_ARGS
961*56bb7041Schristos are its arguments. */
962*56bb7041Schristos
963*56bb7041Schristos int
create_inferior(const char * program,const std::vector<char * > & program_args)964*56bb7041Schristos linux_process_target::create_inferior (const char *program,
965*56bb7041Schristos const std::vector<char *> &program_args)
966*56bb7041Schristos {
967*56bb7041Schristos client_state &cs = get_client_state ();
968*56bb7041Schristos struct lwp_info *new_lwp;
969*56bb7041Schristos int pid;
970*56bb7041Schristos ptid_t ptid;
971*56bb7041Schristos
972*56bb7041Schristos {
973*56bb7041Schristos maybe_disable_address_space_randomization restore_personality
974*56bb7041Schristos (cs.disable_randomization);
975*56bb7041Schristos std::string str_program_args = construct_inferior_arguments (program_args);
976*56bb7041Schristos
977*56bb7041Schristos pid = fork_inferior (program,
978*56bb7041Schristos str_program_args.c_str (),
979*56bb7041Schristos get_environ ()->envp (), linux_ptrace_fun,
980*56bb7041Schristos NULL, NULL, NULL, NULL);
981*56bb7041Schristos }
982*56bb7041Schristos
983*56bb7041Schristos add_linux_process (pid, 0);
984*56bb7041Schristos
985*56bb7041Schristos ptid = ptid_t (pid, pid, 0);
986*56bb7041Schristos new_lwp = add_lwp (ptid);
987*56bb7041Schristos new_lwp->must_set_ptrace_flags = 1;
988*56bb7041Schristos
989*56bb7041Schristos post_fork_inferior (pid, program);
990*56bb7041Schristos
991*56bb7041Schristos return pid;
992*56bb7041Schristos }
993*56bb7041Schristos
994*56bb7041Schristos /* Implement the post_create_inferior target_ops method. */
995*56bb7041Schristos
996*56bb7041Schristos void
post_create_inferior()997*56bb7041Schristos linux_process_target::post_create_inferior ()
998*56bb7041Schristos {
999*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (current_thread);
1000*56bb7041Schristos
1001*56bb7041Schristos low_arch_setup ();
1002*56bb7041Schristos
1003*56bb7041Schristos if (lwp->must_set_ptrace_flags)
1004*56bb7041Schristos {
1005*56bb7041Schristos struct process_info *proc = current_process ();
1006*56bb7041Schristos int options = linux_low_ptrace_options (proc->attached);
1007*56bb7041Schristos
1008*56bb7041Schristos linux_enable_event_reporting (lwpid_of (current_thread), options);
1009*56bb7041Schristos lwp->must_set_ptrace_flags = 0;
1010*56bb7041Schristos }
1011*56bb7041Schristos }
1012*56bb7041Schristos
1013*56bb7041Schristos int
attach_lwp(ptid_t ptid)1014*56bb7041Schristos linux_process_target::attach_lwp (ptid_t ptid)
1015*56bb7041Schristos {
1016*56bb7041Schristos struct lwp_info *new_lwp;
1017*56bb7041Schristos int lwpid = ptid.lwp ();
1018*56bb7041Schristos
1019*56bb7041Schristos if (ptrace (PTRACE_ATTACH, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0)
1020*56bb7041Schristos != 0)
1021*56bb7041Schristos return errno;
1022*56bb7041Schristos
1023*56bb7041Schristos new_lwp = add_lwp (ptid);
1024*56bb7041Schristos
1025*56bb7041Schristos /* We need to wait for SIGSTOP before being able to make the next
1026*56bb7041Schristos ptrace call on this LWP. */
1027*56bb7041Schristos new_lwp->must_set_ptrace_flags = 1;
1028*56bb7041Schristos
1029*56bb7041Schristos if (linux_proc_pid_is_stopped (lwpid))
1030*56bb7041Schristos {
1031*56bb7041Schristos if (debug_threads)
1032*56bb7041Schristos debug_printf ("Attached to a stopped process\n");
1033*56bb7041Schristos
1034*56bb7041Schristos /* The process is definitely stopped. It is in a job control
1035*56bb7041Schristos stop, unless the kernel predates the TASK_STOPPED /
1036*56bb7041Schristos TASK_TRACED distinction, in which case it might be in a
1037*56bb7041Schristos ptrace stop. Make sure it is in a ptrace stop; from there we
1038*56bb7041Schristos can kill it, signal it, et cetera.
1039*56bb7041Schristos
1040*56bb7041Schristos First make sure there is a pending SIGSTOP. Since we are
1041*56bb7041Schristos already attached, the process can not transition from stopped
1042*56bb7041Schristos to running without a PTRACE_CONT; so we know this signal will
1043*56bb7041Schristos go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1044*56bb7041Schristos probably already in the queue (unless this kernel is old
1045*56bb7041Schristos enough to use TASK_STOPPED for ptrace stops); but since
1046*56bb7041Schristos SIGSTOP is not an RT signal, it can only be queued once. */
1047*56bb7041Schristos kill_lwp (lwpid, SIGSTOP);
1048*56bb7041Schristos
1049*56bb7041Schristos /* Finally, resume the stopped process. This will deliver the
1050*56bb7041Schristos SIGSTOP (or a higher priority signal, just like normal
1051*56bb7041Schristos PTRACE_ATTACH), which we'll catch later on. */
1052*56bb7041Schristos ptrace (PTRACE_CONT, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
1053*56bb7041Schristos }
1054*56bb7041Schristos
1055*56bb7041Schristos /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1056*56bb7041Schristos brings it to a halt.
1057*56bb7041Schristos
1058*56bb7041Schristos There are several cases to consider here:
1059*56bb7041Schristos
1060*56bb7041Schristos 1) gdbserver has already attached to the process and is being notified
1061*56bb7041Schristos of a new thread that is being created.
1062*56bb7041Schristos In this case we should ignore that SIGSTOP and resume the
1063*56bb7041Schristos process. This is handled below by setting stop_expected = 1,
1064*56bb7041Schristos and the fact that add_thread sets last_resume_kind ==
1065*56bb7041Schristos resume_continue.
1066*56bb7041Schristos
1067*56bb7041Schristos 2) This is the first thread (the process thread), and we're attaching
1068*56bb7041Schristos to it via attach_inferior.
1069*56bb7041Schristos In this case we want the process thread to stop.
1070*56bb7041Schristos This is handled by having linux_attach set last_resume_kind ==
1071*56bb7041Schristos resume_stop after we return.
1072*56bb7041Schristos
1073*56bb7041Schristos If the pid we are attaching to is also the tgid, we attach to and
1074*56bb7041Schristos stop all the existing threads. Otherwise, we attach to pid and
1075*56bb7041Schristos ignore any other threads in the same group as this pid.
1076*56bb7041Schristos
1077*56bb7041Schristos 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1078*56bb7041Schristos existing threads.
1079*56bb7041Schristos In this case we want the thread to stop.
1080*56bb7041Schristos FIXME: This case is currently not properly handled.
1081*56bb7041Schristos We should wait for the SIGSTOP but don't. Things work apparently
1082*56bb7041Schristos because enough time passes between when we ptrace (ATTACH) and when
1083*56bb7041Schristos gdb makes the next ptrace call on the thread.
1084*56bb7041Schristos
1085*56bb7041Schristos On the other hand, if we are currently trying to stop all threads, we
1086*56bb7041Schristos should treat the new thread as if we had sent it a SIGSTOP. This works
1087*56bb7041Schristos because we are guaranteed that the add_lwp call above added us to the
1088*56bb7041Schristos end of the list, and so the new thread has not yet reached
1089*56bb7041Schristos wait_for_sigstop (but will). */
1090*56bb7041Schristos new_lwp->stop_expected = 1;
1091*56bb7041Schristos
1092*56bb7041Schristos return 0;
1093*56bb7041Schristos }
1094*56bb7041Schristos
1095*56bb7041Schristos /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1096*56bb7041Schristos already attached. Returns true if a new LWP is found, false
1097*56bb7041Schristos otherwise. */
1098*56bb7041Schristos
1099*56bb7041Schristos static int
attach_proc_task_lwp_callback(ptid_t ptid)1100*56bb7041Schristos attach_proc_task_lwp_callback (ptid_t ptid)
1101*56bb7041Schristos {
1102*56bb7041Schristos /* Is this a new thread? */
1103*56bb7041Schristos if (find_thread_ptid (ptid) == NULL)
1104*56bb7041Schristos {
1105*56bb7041Schristos int lwpid = ptid.lwp ();
1106*56bb7041Schristos int err;
1107*56bb7041Schristos
1108*56bb7041Schristos if (debug_threads)
1109*56bb7041Schristos debug_printf ("Found new lwp %d\n", lwpid);
1110*56bb7041Schristos
1111*56bb7041Schristos err = the_linux_target->attach_lwp (ptid);
1112*56bb7041Schristos
1113*56bb7041Schristos /* Be quiet if we simply raced with the thread exiting. EPERM
1114*56bb7041Schristos is returned if the thread's task still exists, and is marked
1115*56bb7041Schristos as exited or zombie, as well as other conditions, so in that
1116*56bb7041Schristos case, confirm the status in /proc/PID/status. */
1117*56bb7041Schristos if (err == ESRCH
1118*56bb7041Schristos || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
1119*56bb7041Schristos {
1120*56bb7041Schristos if (debug_threads)
1121*56bb7041Schristos {
1122*56bb7041Schristos debug_printf ("Cannot attach to lwp %d: "
1123*56bb7041Schristos "thread is gone (%d: %s)\n",
1124*56bb7041Schristos lwpid, err, safe_strerror (err));
1125*56bb7041Schristos }
1126*56bb7041Schristos }
1127*56bb7041Schristos else if (err != 0)
1128*56bb7041Schristos {
1129*56bb7041Schristos std::string reason
1130*56bb7041Schristos = linux_ptrace_attach_fail_reason_string (ptid, err);
1131*56bb7041Schristos
1132*56bb7041Schristos warning (_("Cannot attach to lwp %d: %s"), lwpid, reason.c_str ());
1133*56bb7041Schristos }
1134*56bb7041Schristos
1135*56bb7041Schristos return 1;
1136*56bb7041Schristos }
1137*56bb7041Schristos return 0;
1138*56bb7041Schristos }
1139*56bb7041Schristos
1140*56bb7041Schristos static void async_file_mark (void);
1141*56bb7041Schristos
1142*56bb7041Schristos /* Attach to PID. If PID is the tgid, attach to it and all
1143*56bb7041Schristos of its threads. */
1144*56bb7041Schristos
1145*56bb7041Schristos int
attach(unsigned long pid)1146*56bb7041Schristos linux_process_target::attach (unsigned long pid)
1147*56bb7041Schristos {
1148*56bb7041Schristos struct process_info *proc;
1149*56bb7041Schristos struct thread_info *initial_thread;
1150*56bb7041Schristos ptid_t ptid = ptid_t (pid, pid, 0);
1151*56bb7041Schristos int err;
1152*56bb7041Schristos
1153*56bb7041Schristos proc = add_linux_process (pid, 1);
1154*56bb7041Schristos
1155*56bb7041Schristos /* Attach to PID. We will check for other threads
1156*56bb7041Schristos soon. */
1157*56bb7041Schristos err = attach_lwp (ptid);
1158*56bb7041Schristos if (err != 0)
1159*56bb7041Schristos {
1160*56bb7041Schristos remove_process (proc);
1161*56bb7041Schristos
1162*56bb7041Schristos std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
1163*56bb7041Schristos error ("Cannot attach to process %ld: %s", pid, reason.c_str ());
1164*56bb7041Schristos }
1165*56bb7041Schristos
1166*56bb7041Schristos /* Don't ignore the initial SIGSTOP if we just attached to this
1167*56bb7041Schristos process. It will be collected by wait shortly. */
1168*56bb7041Schristos initial_thread = find_thread_ptid (ptid_t (pid, pid, 0));
1169*56bb7041Schristos initial_thread->last_resume_kind = resume_stop;
1170*56bb7041Schristos
1171*56bb7041Schristos /* We must attach to every LWP. If /proc is mounted, use that to
1172*56bb7041Schristos find them now. On the one hand, the inferior may be using raw
1173*56bb7041Schristos clone instead of using pthreads. On the other hand, even if it
1174*56bb7041Schristos is using pthreads, GDB may not be connected yet (thread_db needs
1175*56bb7041Schristos to do symbol lookups, through qSymbol). Also, thread_db walks
1176*56bb7041Schristos structures in the inferior's address space to find the list of
1177*56bb7041Schristos threads/LWPs, and those structures may well be corrupted. Note
1178*56bb7041Schristos that once thread_db is loaded, we'll still use it to list threads
1179*56bb7041Schristos and associate pthread info with each LWP. */
1180*56bb7041Schristos linux_proc_attach_tgid_threads (pid, attach_proc_task_lwp_callback);
1181*56bb7041Schristos
1182*56bb7041Schristos /* GDB will shortly read the xml target description for this
1183*56bb7041Schristos process, to figure out the process' architecture. But the target
1184*56bb7041Schristos description is only filled in when the first process/thread in
1185*56bb7041Schristos the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1186*56bb7041Schristos that now, otherwise, if GDB is fast enough, it could read the
1187*56bb7041Schristos target description _before_ that initial stop. */
1188*56bb7041Schristos if (non_stop)
1189*56bb7041Schristos {
1190*56bb7041Schristos struct lwp_info *lwp;
1191*56bb7041Schristos int wstat, lwpid;
1192*56bb7041Schristos ptid_t pid_ptid = ptid_t (pid);
1193*56bb7041Schristos
1194*56bb7041Schristos lwpid = wait_for_event_filtered (pid_ptid, pid_ptid, &wstat, __WALL);
1195*56bb7041Schristos gdb_assert (lwpid > 0);
1196*56bb7041Schristos
1197*56bb7041Schristos lwp = find_lwp_pid (ptid_t (lwpid));
1198*56bb7041Schristos
1199*56bb7041Schristos if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGSTOP)
1200*56bb7041Schristos {
1201*56bb7041Schristos lwp->status_pending_p = 1;
1202*56bb7041Schristos lwp->status_pending = wstat;
1203*56bb7041Schristos }
1204*56bb7041Schristos
1205*56bb7041Schristos initial_thread->last_resume_kind = resume_continue;
1206*56bb7041Schristos
1207*56bb7041Schristos async_file_mark ();
1208*56bb7041Schristos
1209*56bb7041Schristos gdb_assert (proc->tdesc != NULL);
1210*56bb7041Schristos }
1211*56bb7041Schristos
1212*56bb7041Schristos return 0;
1213*56bb7041Schristos }
1214*56bb7041Schristos
1215*56bb7041Schristos static int
last_thread_of_process_p(int pid)1216*56bb7041Schristos last_thread_of_process_p (int pid)
1217*56bb7041Schristos {
1218*56bb7041Schristos bool seen_one = false;
1219*56bb7041Schristos
1220*56bb7041Schristos thread_info *thread = find_thread (pid, [&] (thread_info *thr_arg)
1221*56bb7041Schristos {
1222*56bb7041Schristos if (!seen_one)
1223*56bb7041Schristos {
1224*56bb7041Schristos /* This is the first thread of this process we see. */
1225*56bb7041Schristos seen_one = true;
1226*56bb7041Schristos return false;
1227*56bb7041Schristos }
1228*56bb7041Schristos else
1229*56bb7041Schristos {
1230*56bb7041Schristos /* This is the second thread of this process we see. */
1231*56bb7041Schristos return true;
1232*56bb7041Schristos }
1233*56bb7041Schristos });
1234*56bb7041Schristos
1235*56bb7041Schristos return thread == NULL;
1236*56bb7041Schristos }
1237*56bb7041Schristos
1238*56bb7041Schristos /* Kill LWP. */
1239*56bb7041Schristos
1240*56bb7041Schristos static void
linux_kill_one_lwp(struct lwp_info * lwp)1241*56bb7041Schristos linux_kill_one_lwp (struct lwp_info *lwp)
1242*56bb7041Schristos {
1243*56bb7041Schristos struct thread_info *thr = get_lwp_thread (lwp);
1244*56bb7041Schristos int pid = lwpid_of (thr);
1245*56bb7041Schristos
1246*56bb7041Schristos /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1247*56bb7041Schristos there is no signal context, and ptrace(PTRACE_KILL) (or
1248*56bb7041Schristos ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1249*56bb7041Schristos ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1250*56bb7041Schristos alternative is to kill with SIGKILL. We only need one SIGKILL
1251*56bb7041Schristos per process, not one for each thread. But since we still support
1252*56bb7041Schristos support debugging programs using raw clone without CLONE_THREAD,
1253*56bb7041Schristos we send one for each thread. For years, we used PTRACE_KILL
1254*56bb7041Schristos only, so we're being a bit paranoid about some old kernels where
1255*56bb7041Schristos PTRACE_KILL might work better (dubious if there are any such, but
1256*56bb7041Schristos that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1257*56bb7041Schristos second, and so we're fine everywhere. */
1258*56bb7041Schristos
1259*56bb7041Schristos errno = 0;
1260*56bb7041Schristos kill_lwp (pid, SIGKILL);
1261*56bb7041Schristos if (debug_threads)
1262*56bb7041Schristos {
1263*56bb7041Schristos int save_errno = errno;
1264*56bb7041Schristos
1265*56bb7041Schristos debug_printf ("LKL: kill_lwp (SIGKILL) %s, 0, 0 (%s)\n",
1266*56bb7041Schristos target_pid_to_str (ptid_of (thr)),
1267*56bb7041Schristos save_errno ? safe_strerror (save_errno) : "OK");
1268*56bb7041Schristos }
1269*56bb7041Schristos
1270*56bb7041Schristos errno = 0;
1271*56bb7041Schristos ptrace (PTRACE_KILL, pid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
1272*56bb7041Schristos if (debug_threads)
1273*56bb7041Schristos {
1274*56bb7041Schristos int save_errno = errno;
1275*56bb7041Schristos
1276*56bb7041Schristos debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
1277*56bb7041Schristos target_pid_to_str (ptid_of (thr)),
1278*56bb7041Schristos save_errno ? safe_strerror (save_errno) : "OK");
1279*56bb7041Schristos }
1280*56bb7041Schristos }
1281*56bb7041Schristos
1282*56bb7041Schristos /* Kill LWP and wait for it to die. */
1283*56bb7041Schristos
1284*56bb7041Schristos static void
kill_wait_lwp(struct lwp_info * lwp)1285*56bb7041Schristos kill_wait_lwp (struct lwp_info *lwp)
1286*56bb7041Schristos {
1287*56bb7041Schristos struct thread_info *thr = get_lwp_thread (lwp);
1288*56bb7041Schristos int pid = ptid_of (thr).pid ();
1289*56bb7041Schristos int lwpid = ptid_of (thr).lwp ();
1290*56bb7041Schristos int wstat;
1291*56bb7041Schristos int res;
1292*56bb7041Schristos
1293*56bb7041Schristos if (debug_threads)
1294*56bb7041Schristos debug_printf ("kwl: killing lwp %d, for pid: %d\n", lwpid, pid);
1295*56bb7041Schristos
1296*56bb7041Schristos do
1297*56bb7041Schristos {
1298*56bb7041Schristos linux_kill_one_lwp (lwp);
1299*56bb7041Schristos
1300*56bb7041Schristos /* Make sure it died. Notes:
1301*56bb7041Schristos
1302*56bb7041Schristos - The loop is most likely unnecessary.
1303*56bb7041Schristos
1304*56bb7041Schristos - We don't use wait_for_event as that could delete lwps
1305*56bb7041Schristos while we're iterating over them. We're not interested in
1306*56bb7041Schristos any pending status at this point, only in making sure all
1307*56bb7041Schristos wait status on the kernel side are collected until the
1308*56bb7041Schristos process is reaped.
1309*56bb7041Schristos
1310*56bb7041Schristos - We don't use __WALL here as the __WALL emulation relies on
1311*56bb7041Schristos SIGCHLD, and killing a stopped process doesn't generate
1312*56bb7041Schristos one, nor an exit status.
1313*56bb7041Schristos */
1314*56bb7041Schristos res = my_waitpid (lwpid, &wstat, 0);
1315*56bb7041Schristos if (res == -1 && errno == ECHILD)
1316*56bb7041Schristos res = my_waitpid (lwpid, &wstat, __WCLONE);
1317*56bb7041Schristos } while (res > 0 && WIFSTOPPED (wstat));
1318*56bb7041Schristos
1319*56bb7041Schristos /* Even if it was stopped, the child may have already disappeared.
1320*56bb7041Schristos E.g., if it was killed by SIGKILL. */
1321*56bb7041Schristos if (res < 0 && errno != ECHILD)
1322*56bb7041Schristos perror_with_name ("kill_wait_lwp");
1323*56bb7041Schristos }
1324*56bb7041Schristos
1325*56bb7041Schristos /* Callback for `for_each_thread'. Kills an lwp of a given process,
1326*56bb7041Schristos except the leader. */
1327*56bb7041Schristos
1328*56bb7041Schristos static void
kill_one_lwp_callback(thread_info * thread,int pid)1329*56bb7041Schristos kill_one_lwp_callback (thread_info *thread, int pid)
1330*56bb7041Schristos {
1331*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
1332*56bb7041Schristos
1333*56bb7041Schristos /* We avoid killing the first thread here, because of a Linux kernel (at
1334*56bb7041Schristos least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1335*56bb7041Schristos the children get a chance to be reaped, it will remain a zombie
1336*56bb7041Schristos forever. */
1337*56bb7041Schristos
1338*56bb7041Schristos if (lwpid_of (thread) == pid)
1339*56bb7041Schristos {
1340*56bb7041Schristos if (debug_threads)
1341*56bb7041Schristos debug_printf ("lkop: is last of process %s\n",
1342*56bb7041Schristos target_pid_to_str (thread->id));
1343*56bb7041Schristos return;
1344*56bb7041Schristos }
1345*56bb7041Schristos
1346*56bb7041Schristos kill_wait_lwp (lwp);
1347*56bb7041Schristos }
1348*56bb7041Schristos
1349*56bb7041Schristos int
kill(process_info * process)1350*56bb7041Schristos linux_process_target::kill (process_info *process)
1351*56bb7041Schristos {
1352*56bb7041Schristos int pid = process->pid;
1353*56bb7041Schristos
1354*56bb7041Schristos /* If we're killing a running inferior, make sure it is stopped
1355*56bb7041Schristos first, as PTRACE_KILL will not work otherwise. */
1356*56bb7041Schristos stop_all_lwps (0, NULL);
1357*56bb7041Schristos
1358*56bb7041Schristos for_each_thread (pid, [&] (thread_info *thread)
1359*56bb7041Schristos {
1360*56bb7041Schristos kill_one_lwp_callback (thread, pid);
1361*56bb7041Schristos });
1362*56bb7041Schristos
1363*56bb7041Schristos /* See the comment in linux_kill_one_lwp. We did not kill the first
1364*56bb7041Schristos thread in the list, so do so now. */
1365*56bb7041Schristos lwp_info *lwp = find_lwp_pid (ptid_t (pid));
1366*56bb7041Schristos
1367*56bb7041Schristos if (lwp == NULL)
1368*56bb7041Schristos {
1369*56bb7041Schristos if (debug_threads)
1370*56bb7041Schristos debug_printf ("lk_1: cannot find lwp for pid: %d\n",
1371*56bb7041Schristos pid);
1372*56bb7041Schristos }
1373*56bb7041Schristos else
1374*56bb7041Schristos kill_wait_lwp (lwp);
1375*56bb7041Schristos
1376*56bb7041Schristos mourn (process);
1377*56bb7041Schristos
1378*56bb7041Schristos /* Since we presently can only stop all lwps of all processes, we
1379*56bb7041Schristos need to unstop lwps of other processes. */
1380*56bb7041Schristos unstop_all_lwps (0, NULL);
1381*56bb7041Schristos return 0;
1382*56bb7041Schristos }
1383*56bb7041Schristos
1384*56bb7041Schristos /* Get pending signal of THREAD, for detaching purposes. This is the
1385*56bb7041Schristos signal the thread last stopped for, which we need to deliver to the
1386*56bb7041Schristos thread when detaching, otherwise, it'd be suppressed/lost. */
1387*56bb7041Schristos
1388*56bb7041Schristos static int
get_detach_signal(struct thread_info * thread)1389*56bb7041Schristos get_detach_signal (struct thread_info *thread)
1390*56bb7041Schristos {
1391*56bb7041Schristos client_state &cs = get_client_state ();
1392*56bb7041Schristos enum gdb_signal signo = GDB_SIGNAL_0;
1393*56bb7041Schristos int status;
1394*56bb7041Schristos struct lwp_info *lp = get_thread_lwp (thread);
1395*56bb7041Schristos
1396*56bb7041Schristos if (lp->status_pending_p)
1397*56bb7041Schristos status = lp->status_pending;
1398*56bb7041Schristos else
1399*56bb7041Schristos {
1400*56bb7041Schristos /* If the thread had been suspended by gdbserver, and it stopped
1401*56bb7041Schristos cleanly, then it'll have stopped with SIGSTOP. But we don't
1402*56bb7041Schristos want to deliver that SIGSTOP. */
1403*56bb7041Schristos if (thread->last_status.kind != TARGET_WAITKIND_STOPPED
1404*56bb7041Schristos || thread->last_status.value.sig == GDB_SIGNAL_0)
1405*56bb7041Schristos return 0;
1406*56bb7041Schristos
1407*56bb7041Schristos /* Otherwise, we may need to deliver the signal we
1408*56bb7041Schristos intercepted. */
1409*56bb7041Schristos status = lp->last_status;
1410*56bb7041Schristos }
1411*56bb7041Schristos
1412*56bb7041Schristos if (!WIFSTOPPED (status))
1413*56bb7041Schristos {
1414*56bb7041Schristos if (debug_threads)
1415*56bb7041Schristos debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1416*56bb7041Schristos target_pid_to_str (ptid_of (thread)));
1417*56bb7041Schristos return 0;
1418*56bb7041Schristos }
1419*56bb7041Schristos
1420*56bb7041Schristos /* Extended wait statuses aren't real SIGTRAPs. */
1421*56bb7041Schristos if (WSTOPSIG (status) == SIGTRAP && linux_is_extended_waitstatus (status))
1422*56bb7041Schristos {
1423*56bb7041Schristos if (debug_threads)
1424*56bb7041Schristos debug_printf ("GPS: lwp %s had stopped with extended "
1425*56bb7041Schristos "status: no pending signal\n",
1426*56bb7041Schristos target_pid_to_str (ptid_of (thread)));
1427*56bb7041Schristos return 0;
1428*56bb7041Schristos }
1429*56bb7041Schristos
1430*56bb7041Schristos signo = gdb_signal_from_host (WSTOPSIG (status));
1431*56bb7041Schristos
1432*56bb7041Schristos if (cs.program_signals_p && !cs.program_signals[signo])
1433*56bb7041Schristos {
1434*56bb7041Schristos if (debug_threads)
1435*56bb7041Schristos debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1436*56bb7041Schristos target_pid_to_str (ptid_of (thread)),
1437*56bb7041Schristos gdb_signal_to_string (signo));
1438*56bb7041Schristos return 0;
1439*56bb7041Schristos }
1440*56bb7041Schristos else if (!cs.program_signals_p
1441*56bb7041Schristos /* If we have no way to know which signals GDB does not
1442*56bb7041Schristos want to have passed to the program, assume
1443*56bb7041Schristos SIGTRAP/SIGINT, which is GDB's default. */
1444*56bb7041Schristos && (signo == GDB_SIGNAL_TRAP || signo == GDB_SIGNAL_INT))
1445*56bb7041Schristos {
1446*56bb7041Schristos if (debug_threads)
1447*56bb7041Schristos debug_printf ("GPS: lwp %s had signal %s, "
1448*56bb7041Schristos "but we don't know if we should pass it. "
1449*56bb7041Schristos "Default to not.\n",
1450*56bb7041Schristos target_pid_to_str (ptid_of (thread)),
1451*56bb7041Schristos gdb_signal_to_string (signo));
1452*56bb7041Schristos return 0;
1453*56bb7041Schristos }
1454*56bb7041Schristos else
1455*56bb7041Schristos {
1456*56bb7041Schristos if (debug_threads)
1457*56bb7041Schristos debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1458*56bb7041Schristos target_pid_to_str (ptid_of (thread)),
1459*56bb7041Schristos gdb_signal_to_string (signo));
1460*56bb7041Schristos
1461*56bb7041Schristos return WSTOPSIG (status);
1462*56bb7041Schristos }
1463*56bb7041Schristos }
1464*56bb7041Schristos
1465*56bb7041Schristos void
detach_one_lwp(lwp_info * lwp)1466*56bb7041Schristos linux_process_target::detach_one_lwp (lwp_info *lwp)
1467*56bb7041Schristos {
1468*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
1469*56bb7041Schristos int sig;
1470*56bb7041Schristos int lwpid;
1471*56bb7041Schristos
1472*56bb7041Schristos /* If there is a pending SIGSTOP, get rid of it. */
1473*56bb7041Schristos if (lwp->stop_expected)
1474*56bb7041Schristos {
1475*56bb7041Schristos if (debug_threads)
1476*56bb7041Schristos debug_printf ("Sending SIGCONT to %s\n",
1477*56bb7041Schristos target_pid_to_str (ptid_of (thread)));
1478*56bb7041Schristos
1479*56bb7041Schristos kill_lwp (lwpid_of (thread), SIGCONT);
1480*56bb7041Schristos lwp->stop_expected = 0;
1481*56bb7041Schristos }
1482*56bb7041Schristos
1483*56bb7041Schristos /* Pass on any pending signal for this thread. */
1484*56bb7041Schristos sig = get_detach_signal (thread);
1485*56bb7041Schristos
1486*56bb7041Schristos /* Preparing to resume may try to write registers, and fail if the
1487*56bb7041Schristos lwp is zombie. If that happens, ignore the error. We'll handle
1488*56bb7041Schristos it below, when detach fails with ESRCH. */
1489*56bb7041Schristos try
1490*56bb7041Schristos {
1491*56bb7041Schristos /* Flush any pending changes to the process's registers. */
1492*56bb7041Schristos regcache_invalidate_thread (thread);
1493*56bb7041Schristos
1494*56bb7041Schristos /* Finally, let it resume. */
1495*56bb7041Schristos low_prepare_to_resume (lwp);
1496*56bb7041Schristos }
1497*56bb7041Schristos catch (const gdb_exception_error &ex)
1498*56bb7041Schristos {
1499*56bb7041Schristos if (!check_ptrace_stopped_lwp_gone (lwp))
1500*56bb7041Schristos throw;
1501*56bb7041Schristos }
1502*56bb7041Schristos
1503*56bb7041Schristos lwpid = lwpid_of (thread);
1504*56bb7041Schristos if (ptrace (PTRACE_DETACH, lwpid, (PTRACE_TYPE_ARG3) 0,
1505*56bb7041Schristos (PTRACE_TYPE_ARG4) (long) sig) < 0)
1506*56bb7041Schristos {
1507*56bb7041Schristos int save_errno = errno;
1508*56bb7041Schristos
1509*56bb7041Schristos /* We know the thread exists, so ESRCH must mean the lwp is
1510*56bb7041Schristos zombie. This can happen if one of the already-detached
1511*56bb7041Schristos threads exits the whole thread group. In that case we're
1512*56bb7041Schristos still attached, and must reap the lwp. */
1513*56bb7041Schristos if (save_errno == ESRCH)
1514*56bb7041Schristos {
1515*56bb7041Schristos int ret, status;
1516*56bb7041Schristos
1517*56bb7041Schristos ret = my_waitpid (lwpid, &status, __WALL);
1518*56bb7041Schristos if (ret == -1)
1519*56bb7041Schristos {
1520*56bb7041Schristos warning (_("Couldn't reap LWP %d while detaching: %s"),
1521*56bb7041Schristos lwpid, safe_strerror (errno));
1522*56bb7041Schristos }
1523*56bb7041Schristos else if (!WIFEXITED (status) && !WIFSIGNALED (status))
1524*56bb7041Schristos {
1525*56bb7041Schristos warning (_("Reaping LWP %d while detaching "
1526*56bb7041Schristos "returned unexpected status 0x%x"),
1527*56bb7041Schristos lwpid, status);
1528*56bb7041Schristos }
1529*56bb7041Schristos }
1530*56bb7041Schristos else
1531*56bb7041Schristos {
1532*56bb7041Schristos error (_("Can't detach %s: %s"),
1533*56bb7041Schristos target_pid_to_str (ptid_of (thread)),
1534*56bb7041Schristos safe_strerror (save_errno));
1535*56bb7041Schristos }
1536*56bb7041Schristos }
1537*56bb7041Schristos else if (debug_threads)
1538*56bb7041Schristos {
1539*56bb7041Schristos debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)\n",
1540*56bb7041Schristos target_pid_to_str (ptid_of (thread)),
1541*56bb7041Schristos strsignal (sig));
1542*56bb7041Schristos }
1543*56bb7041Schristos
1544*56bb7041Schristos delete_lwp (lwp);
1545*56bb7041Schristos }
1546*56bb7041Schristos
1547*56bb7041Schristos int
detach(process_info * process)1548*56bb7041Schristos linux_process_target::detach (process_info *process)
1549*56bb7041Schristos {
1550*56bb7041Schristos struct lwp_info *main_lwp;
1551*56bb7041Schristos
1552*56bb7041Schristos /* As there's a step over already in progress, let it finish first,
1553*56bb7041Schristos otherwise nesting a stabilize_threads operation on top gets real
1554*56bb7041Schristos messy. */
1555*56bb7041Schristos complete_ongoing_step_over ();
1556*56bb7041Schristos
1557*56bb7041Schristos /* Stop all threads before detaching. First, ptrace requires that
1558*56bb7041Schristos the thread is stopped to successfully detach. Second, thread_db
1559*56bb7041Schristos may need to uninstall thread event breakpoints from memory, which
1560*56bb7041Schristos only works with a stopped process anyway. */
1561*56bb7041Schristos stop_all_lwps (0, NULL);
1562*56bb7041Schristos
1563*56bb7041Schristos #ifdef USE_THREAD_DB
1564*56bb7041Schristos thread_db_detach (process);
1565*56bb7041Schristos #endif
1566*56bb7041Schristos
1567*56bb7041Schristos /* Stabilize threads (move out of jump pads). */
1568*56bb7041Schristos target_stabilize_threads ();
1569*56bb7041Schristos
1570*56bb7041Schristos /* Detach from the clone lwps first. If the thread group exits just
1571*56bb7041Schristos while we're detaching, we must reap the clone lwps before we're
1572*56bb7041Schristos able to reap the leader. */
1573*56bb7041Schristos for_each_thread (process->pid, [this] (thread_info *thread)
1574*56bb7041Schristos {
1575*56bb7041Schristos /* We don't actually detach from the thread group leader just yet.
1576*56bb7041Schristos If the thread group exits, we must reap the zombie clone lwps
1577*56bb7041Schristos before we're able to reap the leader. */
1578*56bb7041Schristos if (thread->id.pid () == thread->id.lwp ())
1579*56bb7041Schristos return;
1580*56bb7041Schristos
1581*56bb7041Schristos lwp_info *lwp = get_thread_lwp (thread);
1582*56bb7041Schristos detach_one_lwp (lwp);
1583*56bb7041Schristos });
1584*56bb7041Schristos
1585*56bb7041Schristos main_lwp = find_lwp_pid (ptid_t (process->pid));
1586*56bb7041Schristos detach_one_lwp (main_lwp);
1587*56bb7041Schristos
1588*56bb7041Schristos mourn (process);
1589*56bb7041Schristos
1590*56bb7041Schristos /* Since we presently can only stop all lwps of all processes, we
1591*56bb7041Schristos need to unstop lwps of other processes. */
1592*56bb7041Schristos unstop_all_lwps (0, NULL);
1593*56bb7041Schristos return 0;
1594*56bb7041Schristos }
1595*56bb7041Schristos
1596*56bb7041Schristos /* Remove all LWPs that belong to process PROC from the lwp list. */
1597*56bb7041Schristos
1598*56bb7041Schristos void
mourn(process_info * process)1599*56bb7041Schristos linux_process_target::mourn (process_info *process)
1600*56bb7041Schristos {
1601*56bb7041Schristos struct process_info_private *priv;
1602*56bb7041Schristos
1603*56bb7041Schristos #ifdef USE_THREAD_DB
1604*56bb7041Schristos thread_db_mourn (process);
1605*56bb7041Schristos #endif
1606*56bb7041Schristos
1607*56bb7041Schristos for_each_thread (process->pid, [this] (thread_info *thread)
1608*56bb7041Schristos {
1609*56bb7041Schristos delete_lwp (get_thread_lwp (thread));
1610*56bb7041Schristos });
1611*56bb7041Schristos
1612*56bb7041Schristos /* Freeing all private data. */
1613*56bb7041Schristos priv = process->priv;
1614*56bb7041Schristos low_delete_process (priv->arch_private);
1615*56bb7041Schristos free (priv);
1616*56bb7041Schristos process->priv = NULL;
1617*56bb7041Schristos
1618*56bb7041Schristos remove_process (process);
1619*56bb7041Schristos }
1620*56bb7041Schristos
1621*56bb7041Schristos void
join(int pid)1622*56bb7041Schristos linux_process_target::join (int pid)
1623*56bb7041Schristos {
1624*56bb7041Schristos int status, ret;
1625*56bb7041Schristos
1626*56bb7041Schristos do {
1627*56bb7041Schristos ret = my_waitpid (pid, &status, 0);
1628*56bb7041Schristos if (WIFEXITED (status) || WIFSIGNALED (status))
1629*56bb7041Schristos break;
1630*56bb7041Schristos } while (ret != -1 || errno != ECHILD);
1631*56bb7041Schristos }
1632*56bb7041Schristos
1633*56bb7041Schristos /* Return true if the given thread is still alive. */
1634*56bb7041Schristos
1635*56bb7041Schristos bool
thread_alive(ptid_t ptid)1636*56bb7041Schristos linux_process_target::thread_alive (ptid_t ptid)
1637*56bb7041Schristos {
1638*56bb7041Schristos struct lwp_info *lwp = find_lwp_pid (ptid);
1639*56bb7041Schristos
1640*56bb7041Schristos /* We assume we always know if a thread exits. If a whole process
1641*56bb7041Schristos exited but we still haven't been able to report it to GDB, we'll
1642*56bb7041Schristos hold on to the last lwp of the dead process. */
1643*56bb7041Schristos if (lwp != NULL)
1644*56bb7041Schristos return !lwp_is_marked_dead (lwp);
1645*56bb7041Schristos else
1646*56bb7041Schristos return 0;
1647*56bb7041Schristos }
1648*56bb7041Schristos
1649*56bb7041Schristos bool
thread_still_has_status_pending(thread_info * thread)1650*56bb7041Schristos linux_process_target::thread_still_has_status_pending (thread_info *thread)
1651*56bb7041Schristos {
1652*56bb7041Schristos struct lwp_info *lp = get_thread_lwp (thread);
1653*56bb7041Schristos
1654*56bb7041Schristos if (!lp->status_pending_p)
1655*56bb7041Schristos return 0;
1656*56bb7041Schristos
1657*56bb7041Schristos if (thread->last_resume_kind != resume_stop
1658*56bb7041Schristos && (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
1659*56bb7041Schristos || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT))
1660*56bb7041Schristos {
1661*56bb7041Schristos struct thread_info *saved_thread;
1662*56bb7041Schristos CORE_ADDR pc;
1663*56bb7041Schristos int discard = 0;
1664*56bb7041Schristos
1665*56bb7041Schristos gdb_assert (lp->last_status != 0);
1666*56bb7041Schristos
1667*56bb7041Schristos pc = get_pc (lp);
1668*56bb7041Schristos
1669*56bb7041Schristos saved_thread = current_thread;
1670*56bb7041Schristos current_thread = thread;
1671*56bb7041Schristos
1672*56bb7041Schristos if (pc != lp->stop_pc)
1673*56bb7041Schristos {
1674*56bb7041Schristos if (debug_threads)
1675*56bb7041Schristos debug_printf ("PC of %ld changed\n",
1676*56bb7041Schristos lwpid_of (thread));
1677*56bb7041Schristos discard = 1;
1678*56bb7041Schristos }
1679*56bb7041Schristos
1680*56bb7041Schristos #if !USE_SIGTRAP_SIGINFO
1681*56bb7041Schristos else if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
1682*56bb7041Schristos && !low_breakpoint_at (pc))
1683*56bb7041Schristos {
1684*56bb7041Schristos if (debug_threads)
1685*56bb7041Schristos debug_printf ("previous SW breakpoint of %ld gone\n",
1686*56bb7041Schristos lwpid_of (thread));
1687*56bb7041Schristos discard = 1;
1688*56bb7041Schristos }
1689*56bb7041Schristos else if (lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT
1690*56bb7041Schristos && !hardware_breakpoint_inserted_here (pc))
1691*56bb7041Schristos {
1692*56bb7041Schristos if (debug_threads)
1693*56bb7041Schristos debug_printf ("previous HW breakpoint of %ld gone\n",
1694*56bb7041Schristos lwpid_of (thread));
1695*56bb7041Schristos discard = 1;
1696*56bb7041Schristos }
1697*56bb7041Schristos #endif
1698*56bb7041Schristos
1699*56bb7041Schristos current_thread = saved_thread;
1700*56bb7041Schristos
1701*56bb7041Schristos if (discard)
1702*56bb7041Schristos {
1703*56bb7041Schristos if (debug_threads)
1704*56bb7041Schristos debug_printf ("discarding pending breakpoint status\n");
1705*56bb7041Schristos lp->status_pending_p = 0;
1706*56bb7041Schristos return 0;
1707*56bb7041Schristos }
1708*56bb7041Schristos }
1709*56bb7041Schristos
1710*56bb7041Schristos return 1;
1711*56bb7041Schristos }
1712*56bb7041Schristos
1713*56bb7041Schristos /* Returns true if LWP is resumed from the client's perspective. */
1714*56bb7041Schristos
1715*56bb7041Schristos static int
lwp_resumed(struct lwp_info * lwp)1716*56bb7041Schristos lwp_resumed (struct lwp_info *lwp)
1717*56bb7041Schristos {
1718*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
1719*56bb7041Schristos
1720*56bb7041Schristos if (thread->last_resume_kind != resume_stop)
1721*56bb7041Schristos return 1;
1722*56bb7041Schristos
1723*56bb7041Schristos /* Did gdb send us a `vCont;t', but we haven't reported the
1724*56bb7041Schristos corresponding stop to gdb yet? If so, the thread is still
1725*56bb7041Schristos resumed/running from gdb's perspective. */
1726*56bb7041Schristos if (thread->last_resume_kind == resume_stop
1727*56bb7041Schristos && thread->last_status.kind == TARGET_WAITKIND_IGNORE)
1728*56bb7041Schristos return 1;
1729*56bb7041Schristos
1730*56bb7041Schristos return 0;
1731*56bb7041Schristos }
1732*56bb7041Schristos
1733*56bb7041Schristos bool
status_pending_p_callback(thread_info * thread,ptid_t ptid)1734*56bb7041Schristos linux_process_target::status_pending_p_callback (thread_info *thread,
1735*56bb7041Schristos ptid_t ptid)
1736*56bb7041Schristos {
1737*56bb7041Schristos struct lwp_info *lp = get_thread_lwp (thread);
1738*56bb7041Schristos
1739*56bb7041Schristos /* Check if we're only interested in events from a specific process
1740*56bb7041Schristos or a specific LWP. */
1741*56bb7041Schristos if (!thread->id.matches (ptid))
1742*56bb7041Schristos return 0;
1743*56bb7041Schristos
1744*56bb7041Schristos if (!lwp_resumed (lp))
1745*56bb7041Schristos return 0;
1746*56bb7041Schristos
1747*56bb7041Schristos if (lp->status_pending_p
1748*56bb7041Schristos && !thread_still_has_status_pending (thread))
1749*56bb7041Schristos {
1750*56bb7041Schristos resume_one_lwp (lp, lp->stepping, GDB_SIGNAL_0, NULL);
1751*56bb7041Schristos return 0;
1752*56bb7041Schristos }
1753*56bb7041Schristos
1754*56bb7041Schristos return lp->status_pending_p;
1755*56bb7041Schristos }
1756*56bb7041Schristos
1757*56bb7041Schristos struct lwp_info *
find_lwp_pid(ptid_t ptid)1758*56bb7041Schristos find_lwp_pid (ptid_t ptid)
1759*56bb7041Schristos {
1760*56bb7041Schristos thread_info *thread = find_thread ([&] (thread_info *thr_arg)
1761*56bb7041Schristos {
1762*56bb7041Schristos int lwp = ptid.lwp () != 0 ? ptid.lwp () : ptid.pid ();
1763*56bb7041Schristos return thr_arg->id.lwp () == lwp;
1764*56bb7041Schristos });
1765*56bb7041Schristos
1766*56bb7041Schristos if (thread == NULL)
1767*56bb7041Schristos return NULL;
1768*56bb7041Schristos
1769*56bb7041Schristos return get_thread_lwp (thread);
1770*56bb7041Schristos }
1771*56bb7041Schristos
1772*56bb7041Schristos /* Return the number of known LWPs in the tgid given by PID. */
1773*56bb7041Schristos
1774*56bb7041Schristos static int
num_lwps(int pid)1775*56bb7041Schristos num_lwps (int pid)
1776*56bb7041Schristos {
1777*56bb7041Schristos int count = 0;
1778*56bb7041Schristos
1779*56bb7041Schristos for_each_thread (pid, [&] (thread_info *thread)
1780*56bb7041Schristos {
1781*56bb7041Schristos count++;
1782*56bb7041Schristos });
1783*56bb7041Schristos
1784*56bb7041Schristos return count;
1785*56bb7041Schristos }
1786*56bb7041Schristos
1787*56bb7041Schristos /* See nat/linux-nat.h. */
1788*56bb7041Schristos
1789*56bb7041Schristos struct lwp_info *
iterate_over_lwps(ptid_t filter,gdb::function_view<iterate_over_lwps_ftype> callback)1790*56bb7041Schristos iterate_over_lwps (ptid_t filter,
1791*56bb7041Schristos gdb::function_view<iterate_over_lwps_ftype> callback)
1792*56bb7041Schristos {
1793*56bb7041Schristos thread_info *thread = find_thread (filter, [&] (thread_info *thr_arg)
1794*56bb7041Schristos {
1795*56bb7041Schristos lwp_info *lwp = get_thread_lwp (thr_arg);
1796*56bb7041Schristos
1797*56bb7041Schristos return callback (lwp);
1798*56bb7041Schristos });
1799*56bb7041Schristos
1800*56bb7041Schristos if (thread == NULL)
1801*56bb7041Schristos return NULL;
1802*56bb7041Schristos
1803*56bb7041Schristos return get_thread_lwp (thread);
1804*56bb7041Schristos }
1805*56bb7041Schristos
1806*56bb7041Schristos void
check_zombie_leaders()1807*56bb7041Schristos linux_process_target::check_zombie_leaders ()
1808*56bb7041Schristos {
1809*56bb7041Schristos for_each_process ([this] (process_info *proc) {
1810*56bb7041Schristos pid_t leader_pid = pid_of (proc);
1811*56bb7041Schristos struct lwp_info *leader_lp;
1812*56bb7041Schristos
1813*56bb7041Schristos leader_lp = find_lwp_pid (ptid_t (leader_pid));
1814*56bb7041Schristos
1815*56bb7041Schristos if (debug_threads)
1816*56bb7041Schristos debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1817*56bb7041Schristos "num_lwps=%d, zombie=%d\n",
1818*56bb7041Schristos leader_pid, leader_lp!= NULL, num_lwps (leader_pid),
1819*56bb7041Schristos linux_proc_pid_is_zombie (leader_pid));
1820*56bb7041Schristos
1821*56bb7041Schristos if (leader_lp != NULL && !leader_lp->stopped
1822*56bb7041Schristos /* Check if there are other threads in the group, as we may
1823*56bb7041Schristos have raced with the inferior simply exiting. */
1824*56bb7041Schristos && !last_thread_of_process_p (leader_pid)
1825*56bb7041Schristos && linux_proc_pid_is_zombie (leader_pid))
1826*56bb7041Schristos {
1827*56bb7041Schristos /* A leader zombie can mean one of two things:
1828*56bb7041Schristos
1829*56bb7041Schristos - It exited, and there's an exit status pending
1830*56bb7041Schristos available, or only the leader exited (not the whole
1831*56bb7041Schristos program). In the latter case, we can't waitpid the
1832*56bb7041Schristos leader's exit status until all other threads are gone.
1833*56bb7041Schristos
1834*56bb7041Schristos - There are 3 or more threads in the group, and a thread
1835*56bb7041Schristos other than the leader exec'd. On an exec, the Linux
1836*56bb7041Schristos kernel destroys all other threads (except the execing
1837*56bb7041Schristos one) in the thread group, and resets the execing thread's
1838*56bb7041Schristos tid to the tgid. No exit notification is sent for the
1839*56bb7041Schristos execing thread -- from the ptracer's perspective, it
1840*56bb7041Schristos appears as though the execing thread just vanishes.
1841*56bb7041Schristos Until we reap all other threads except the leader and the
1842*56bb7041Schristos execing thread, the leader will be zombie, and the
1843*56bb7041Schristos execing thread will be in `D (disc sleep)'. As soon as
1844*56bb7041Schristos all other threads are reaped, the execing thread changes
1845*56bb7041Schristos it's tid to the tgid, and the previous (zombie) leader
1846*56bb7041Schristos vanishes, giving place to the "new" leader. We could try
1847*56bb7041Schristos distinguishing the exit and exec cases, by waiting once
1848*56bb7041Schristos more, and seeing if something comes out, but it doesn't
1849*56bb7041Schristos sound useful. The previous leader _does_ go away, and
1850*56bb7041Schristos we'll re-add the new one once we see the exec event
1851*56bb7041Schristos (which is just the same as what would happen if the
1852*56bb7041Schristos previous leader did exit voluntarily before some other
1853*56bb7041Schristos thread execs). */
1854*56bb7041Schristos
1855*56bb7041Schristos if (debug_threads)
1856*56bb7041Schristos debug_printf ("CZL: Thread group leader %d zombie "
1857*56bb7041Schristos "(it exited, or another thread execd).\n",
1858*56bb7041Schristos leader_pid);
1859*56bb7041Schristos
1860*56bb7041Schristos delete_lwp (leader_lp);
1861*56bb7041Schristos }
1862*56bb7041Schristos });
1863*56bb7041Schristos }
1864*56bb7041Schristos
1865*56bb7041Schristos /* Callback for `find_thread'. Returns the first LWP that is not
1866*56bb7041Schristos stopped. */
1867*56bb7041Schristos
1868*56bb7041Schristos static bool
not_stopped_callback(thread_info * thread,ptid_t filter)1869*56bb7041Schristos not_stopped_callback (thread_info *thread, ptid_t filter)
1870*56bb7041Schristos {
1871*56bb7041Schristos if (!thread->id.matches (filter))
1872*56bb7041Schristos return false;
1873*56bb7041Schristos
1874*56bb7041Schristos lwp_info *lwp = get_thread_lwp (thread);
1875*56bb7041Schristos
1876*56bb7041Schristos return !lwp->stopped;
1877*56bb7041Schristos }
1878*56bb7041Schristos
1879*56bb7041Schristos /* Increment LWP's suspend count. */
1880*56bb7041Schristos
1881*56bb7041Schristos static void
lwp_suspended_inc(struct lwp_info * lwp)1882*56bb7041Schristos lwp_suspended_inc (struct lwp_info *lwp)
1883*56bb7041Schristos {
1884*56bb7041Schristos lwp->suspended++;
1885*56bb7041Schristos
1886*56bb7041Schristos if (debug_threads && lwp->suspended > 4)
1887*56bb7041Schristos {
1888*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
1889*56bb7041Schristos
1890*56bb7041Schristos debug_printf ("LWP %ld has a suspiciously high suspend count,"
1891*56bb7041Schristos " suspended=%d\n", lwpid_of (thread), lwp->suspended);
1892*56bb7041Schristos }
1893*56bb7041Schristos }
1894*56bb7041Schristos
1895*56bb7041Schristos /* Decrement LWP's suspend count. */
1896*56bb7041Schristos
1897*56bb7041Schristos static void
lwp_suspended_decr(struct lwp_info * lwp)1898*56bb7041Schristos lwp_suspended_decr (struct lwp_info *lwp)
1899*56bb7041Schristos {
1900*56bb7041Schristos lwp->suspended--;
1901*56bb7041Schristos
1902*56bb7041Schristos if (lwp->suspended < 0)
1903*56bb7041Schristos {
1904*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
1905*56bb7041Schristos
1906*56bb7041Schristos internal_error (__FILE__, __LINE__,
1907*56bb7041Schristos "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread),
1908*56bb7041Schristos lwp->suspended);
1909*56bb7041Schristos }
1910*56bb7041Schristos }
1911*56bb7041Schristos
1912*56bb7041Schristos /* This function should only be called if the LWP got a SIGTRAP.
1913*56bb7041Schristos
1914*56bb7041Schristos Handle any tracepoint steps or hits. Return true if a tracepoint
1915*56bb7041Schristos event was handled, 0 otherwise. */
1916*56bb7041Schristos
1917*56bb7041Schristos static int
handle_tracepoints(struct lwp_info * lwp)1918*56bb7041Schristos handle_tracepoints (struct lwp_info *lwp)
1919*56bb7041Schristos {
1920*56bb7041Schristos struct thread_info *tinfo = get_lwp_thread (lwp);
1921*56bb7041Schristos int tpoint_related_event = 0;
1922*56bb7041Schristos
1923*56bb7041Schristos gdb_assert (lwp->suspended == 0);
1924*56bb7041Schristos
1925*56bb7041Schristos /* If this tracepoint hit causes a tracing stop, we'll immediately
1926*56bb7041Schristos uninsert tracepoints. To do this, we temporarily pause all
1927*56bb7041Schristos threads, unpatch away, and then unpause threads. We need to make
1928*56bb7041Schristos sure the unpausing doesn't resume LWP too. */
1929*56bb7041Schristos lwp_suspended_inc (lwp);
1930*56bb7041Schristos
1931*56bb7041Schristos /* And we need to be sure that any all-threads-stopping doesn't try
1932*56bb7041Schristos to move threads out of the jump pads, as it could deadlock the
1933*56bb7041Schristos inferior (LWP could be in the jump pad, maybe even holding the
1934*56bb7041Schristos lock.) */
1935*56bb7041Schristos
1936*56bb7041Schristos /* Do any necessary step collect actions. */
1937*56bb7041Schristos tpoint_related_event |= tracepoint_finished_step (tinfo, lwp->stop_pc);
1938*56bb7041Schristos
1939*56bb7041Schristos tpoint_related_event |= handle_tracepoint_bkpts (tinfo, lwp->stop_pc);
1940*56bb7041Schristos
1941*56bb7041Schristos /* See if we just hit a tracepoint and do its main collect
1942*56bb7041Schristos actions. */
1943*56bb7041Schristos tpoint_related_event |= tracepoint_was_hit (tinfo, lwp->stop_pc);
1944*56bb7041Schristos
1945*56bb7041Schristos lwp_suspended_decr (lwp);
1946*56bb7041Schristos
1947*56bb7041Schristos gdb_assert (lwp->suspended == 0);
1948*56bb7041Schristos gdb_assert (!stabilizing_threads
1949*56bb7041Schristos || (lwp->collecting_fast_tracepoint
1950*56bb7041Schristos != fast_tpoint_collect_result::not_collecting));
1951*56bb7041Schristos
1952*56bb7041Schristos if (tpoint_related_event)
1953*56bb7041Schristos {
1954*56bb7041Schristos if (debug_threads)
1955*56bb7041Schristos debug_printf ("got a tracepoint event\n");
1956*56bb7041Schristos return 1;
1957*56bb7041Schristos }
1958*56bb7041Schristos
1959*56bb7041Schristos return 0;
1960*56bb7041Schristos }
1961*56bb7041Schristos
1962*56bb7041Schristos fast_tpoint_collect_result
linux_fast_tracepoint_collecting(lwp_info * lwp,fast_tpoint_collect_status * status)1963*56bb7041Schristos linux_process_target::linux_fast_tracepoint_collecting
1964*56bb7041Schristos (lwp_info *lwp, fast_tpoint_collect_status *status)
1965*56bb7041Schristos {
1966*56bb7041Schristos CORE_ADDR thread_area;
1967*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
1968*56bb7041Schristos
1969*56bb7041Schristos /* Get the thread area address. This is used to recognize which
1970*56bb7041Schristos thread is which when tracing with the in-process agent library.
1971*56bb7041Schristos We don't read anything from the address, and treat it as opaque;
1972*56bb7041Schristos it's the address itself that we assume is unique per-thread. */
1973*56bb7041Schristos if (low_get_thread_area (lwpid_of (thread), &thread_area) == -1)
1974*56bb7041Schristos return fast_tpoint_collect_result::not_collecting;
1975*56bb7041Schristos
1976*56bb7041Schristos return fast_tracepoint_collecting (thread_area, lwp->stop_pc, status);
1977*56bb7041Schristos }
1978*56bb7041Schristos
1979*56bb7041Schristos int
low_get_thread_area(int lwpid,CORE_ADDR * addrp)1980*56bb7041Schristos linux_process_target::low_get_thread_area (int lwpid, CORE_ADDR *addrp)
1981*56bb7041Schristos {
1982*56bb7041Schristos return -1;
1983*56bb7041Schristos }
1984*56bb7041Schristos
1985*56bb7041Schristos bool
maybe_move_out_of_jump_pad(lwp_info * lwp,int * wstat)1986*56bb7041Schristos linux_process_target::maybe_move_out_of_jump_pad (lwp_info *lwp, int *wstat)
1987*56bb7041Schristos {
1988*56bb7041Schristos struct thread_info *saved_thread;
1989*56bb7041Schristos
1990*56bb7041Schristos saved_thread = current_thread;
1991*56bb7041Schristos current_thread = get_lwp_thread (lwp);
1992*56bb7041Schristos
1993*56bb7041Schristos if ((wstat == NULL
1994*56bb7041Schristos || (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) != SIGTRAP))
1995*56bb7041Schristos && supports_fast_tracepoints ()
1996*56bb7041Schristos && agent_loaded_p ())
1997*56bb7041Schristos {
1998*56bb7041Schristos struct fast_tpoint_collect_status status;
1999*56bb7041Schristos
2000*56bb7041Schristos if (debug_threads)
2001*56bb7041Schristos debug_printf ("Checking whether LWP %ld needs to move out of the "
2002*56bb7041Schristos "jump pad.\n",
2003*56bb7041Schristos lwpid_of (current_thread));
2004*56bb7041Schristos
2005*56bb7041Schristos fast_tpoint_collect_result r
2006*56bb7041Schristos = linux_fast_tracepoint_collecting (lwp, &status);
2007*56bb7041Schristos
2008*56bb7041Schristos if (wstat == NULL
2009*56bb7041Schristos || (WSTOPSIG (*wstat) != SIGILL
2010*56bb7041Schristos && WSTOPSIG (*wstat) != SIGFPE
2011*56bb7041Schristos && WSTOPSIG (*wstat) != SIGSEGV
2012*56bb7041Schristos && WSTOPSIG (*wstat) != SIGBUS))
2013*56bb7041Schristos {
2014*56bb7041Schristos lwp->collecting_fast_tracepoint = r;
2015*56bb7041Schristos
2016*56bb7041Schristos if (r != fast_tpoint_collect_result::not_collecting)
2017*56bb7041Schristos {
2018*56bb7041Schristos if (r == fast_tpoint_collect_result::before_insn
2019*56bb7041Schristos && lwp->exit_jump_pad_bkpt == NULL)
2020*56bb7041Schristos {
2021*56bb7041Schristos /* Haven't executed the original instruction yet.
2022*56bb7041Schristos Set breakpoint there, and wait till it's hit,
2023*56bb7041Schristos then single-step until exiting the jump pad. */
2024*56bb7041Schristos lwp->exit_jump_pad_bkpt
2025*56bb7041Schristos = set_breakpoint_at (status.adjusted_insn_addr, NULL);
2026*56bb7041Schristos }
2027*56bb7041Schristos
2028*56bb7041Schristos if (debug_threads)
2029*56bb7041Schristos debug_printf ("Checking whether LWP %ld needs to move out of "
2030*56bb7041Schristos "the jump pad...it does\n",
2031*56bb7041Schristos lwpid_of (current_thread));
2032*56bb7041Schristos current_thread = saved_thread;
2033*56bb7041Schristos
2034*56bb7041Schristos return true;
2035*56bb7041Schristos }
2036*56bb7041Schristos }
2037*56bb7041Schristos else
2038*56bb7041Schristos {
2039*56bb7041Schristos /* If we get a synchronous signal while collecting, *and*
2040*56bb7041Schristos while executing the (relocated) original instruction,
2041*56bb7041Schristos reset the PC to point at the tpoint address, before
2042*56bb7041Schristos reporting to GDB. Otherwise, it's an IPA lib bug: just
2043*56bb7041Schristos report the signal to GDB, and pray for the best. */
2044*56bb7041Schristos
2045*56bb7041Schristos lwp->collecting_fast_tracepoint
2046*56bb7041Schristos = fast_tpoint_collect_result::not_collecting;
2047*56bb7041Schristos
2048*56bb7041Schristos if (r != fast_tpoint_collect_result::not_collecting
2049*56bb7041Schristos && (status.adjusted_insn_addr <= lwp->stop_pc
2050*56bb7041Schristos && lwp->stop_pc < status.adjusted_insn_addr_end))
2051*56bb7041Schristos {
2052*56bb7041Schristos siginfo_t info;
2053*56bb7041Schristos struct regcache *regcache;
2054*56bb7041Schristos
2055*56bb7041Schristos /* The si_addr on a few signals references the address
2056*56bb7041Schristos of the faulting instruction. Adjust that as
2057*56bb7041Schristos well. */
2058*56bb7041Schristos if ((WSTOPSIG (*wstat) == SIGILL
2059*56bb7041Schristos || WSTOPSIG (*wstat) == SIGFPE
2060*56bb7041Schristos || WSTOPSIG (*wstat) == SIGBUS
2061*56bb7041Schristos || WSTOPSIG (*wstat) == SIGSEGV)
2062*56bb7041Schristos && ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
2063*56bb7041Schristos (PTRACE_TYPE_ARG3) 0, &info) == 0
2064*56bb7041Schristos /* Final check just to make sure we don't clobber
2065*56bb7041Schristos the siginfo of non-kernel-sent signals. */
2066*56bb7041Schristos && (uintptr_t) info.si_addr == lwp->stop_pc)
2067*56bb7041Schristos {
2068*56bb7041Schristos info.si_addr = (void *) (uintptr_t) status.tpoint_addr;
2069*56bb7041Schristos ptrace (PTRACE_SETSIGINFO, lwpid_of (current_thread),
2070*56bb7041Schristos (PTRACE_TYPE_ARG3) 0, &info);
2071*56bb7041Schristos }
2072*56bb7041Schristos
2073*56bb7041Schristos regcache = get_thread_regcache (current_thread, 1);
2074*56bb7041Schristos low_set_pc (regcache, status.tpoint_addr);
2075*56bb7041Schristos lwp->stop_pc = status.tpoint_addr;
2076*56bb7041Schristos
2077*56bb7041Schristos /* Cancel any fast tracepoint lock this thread was
2078*56bb7041Schristos holding. */
2079*56bb7041Schristos force_unlock_trace_buffer ();
2080*56bb7041Schristos }
2081*56bb7041Schristos
2082*56bb7041Schristos if (lwp->exit_jump_pad_bkpt != NULL)
2083*56bb7041Schristos {
2084*56bb7041Schristos if (debug_threads)
2085*56bb7041Schristos debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
2086*56bb7041Schristos "stopping all threads momentarily.\n");
2087*56bb7041Schristos
2088*56bb7041Schristos stop_all_lwps (1, lwp);
2089*56bb7041Schristos
2090*56bb7041Schristos delete_breakpoint (lwp->exit_jump_pad_bkpt);
2091*56bb7041Schristos lwp->exit_jump_pad_bkpt = NULL;
2092*56bb7041Schristos
2093*56bb7041Schristos unstop_all_lwps (1, lwp);
2094*56bb7041Schristos
2095*56bb7041Schristos gdb_assert (lwp->suspended >= 0);
2096*56bb7041Schristos }
2097*56bb7041Schristos }
2098*56bb7041Schristos }
2099*56bb7041Schristos
2100*56bb7041Schristos if (debug_threads)
2101*56bb7041Schristos debug_printf ("Checking whether LWP %ld needs to move out of the "
2102*56bb7041Schristos "jump pad...no\n",
2103*56bb7041Schristos lwpid_of (current_thread));
2104*56bb7041Schristos
2105*56bb7041Schristos current_thread = saved_thread;
2106*56bb7041Schristos return false;
2107*56bb7041Schristos }
2108*56bb7041Schristos
2109*56bb7041Schristos /* Enqueue one signal in the "signals to report later when out of the
2110*56bb7041Schristos jump pad" list. */
2111*56bb7041Schristos
2112*56bb7041Schristos static void
enqueue_one_deferred_signal(struct lwp_info * lwp,int * wstat)2113*56bb7041Schristos enqueue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
2114*56bb7041Schristos {
2115*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
2116*56bb7041Schristos
2117*56bb7041Schristos if (debug_threads)
2118*56bb7041Schristos debug_printf ("Deferring signal %d for LWP %ld.\n",
2119*56bb7041Schristos WSTOPSIG (*wstat), lwpid_of (thread));
2120*56bb7041Schristos
2121*56bb7041Schristos if (debug_threads)
2122*56bb7041Schristos {
2123*56bb7041Schristos for (const auto &sig : lwp->pending_signals_to_report)
2124*56bb7041Schristos debug_printf (" Already queued %d\n",
2125*56bb7041Schristos sig.signal);
2126*56bb7041Schristos
2127*56bb7041Schristos debug_printf (" (no more currently queued signals)\n");
2128*56bb7041Schristos }
2129*56bb7041Schristos
2130*56bb7041Schristos /* Don't enqueue non-RT signals if they are already in the deferred
2131*56bb7041Schristos queue. (SIGSTOP being the easiest signal to see ending up here
2132*56bb7041Schristos twice) */
2133*56bb7041Schristos if (WSTOPSIG (*wstat) < __SIGRTMIN)
2134*56bb7041Schristos {
2135*56bb7041Schristos for (const auto &sig : lwp->pending_signals_to_report)
2136*56bb7041Schristos {
2137*56bb7041Schristos if (sig.signal == WSTOPSIG (*wstat))
2138*56bb7041Schristos {
2139*56bb7041Schristos if (debug_threads)
2140*56bb7041Schristos debug_printf ("Not requeuing already queued non-RT signal %d"
2141*56bb7041Schristos " for LWP %ld\n",
2142*56bb7041Schristos sig.signal,
2143*56bb7041Schristos lwpid_of (thread));
2144*56bb7041Schristos return;
2145*56bb7041Schristos }
2146*56bb7041Schristos }
2147*56bb7041Schristos }
2148*56bb7041Schristos
2149*56bb7041Schristos lwp->pending_signals_to_report.emplace_back (WSTOPSIG (*wstat));
2150*56bb7041Schristos
2151*56bb7041Schristos ptrace (PTRACE_GETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
2152*56bb7041Schristos &lwp->pending_signals_to_report.back ().info);
2153*56bb7041Schristos }
2154*56bb7041Schristos
2155*56bb7041Schristos /* Dequeue one signal from the "signals to report later when out of
2156*56bb7041Schristos the jump pad" list. */
2157*56bb7041Schristos
2158*56bb7041Schristos static int
dequeue_one_deferred_signal(struct lwp_info * lwp,int * wstat)2159*56bb7041Schristos dequeue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
2160*56bb7041Schristos {
2161*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
2162*56bb7041Schristos
2163*56bb7041Schristos if (!lwp->pending_signals_to_report.empty ())
2164*56bb7041Schristos {
2165*56bb7041Schristos const pending_signal &p_sig = lwp->pending_signals_to_report.front ();
2166*56bb7041Schristos
2167*56bb7041Schristos *wstat = W_STOPCODE (p_sig.signal);
2168*56bb7041Schristos if (p_sig.info.si_signo != 0)
2169*56bb7041Schristos ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
2170*56bb7041Schristos &p_sig.info);
2171*56bb7041Schristos
2172*56bb7041Schristos lwp->pending_signals_to_report.pop_front ();
2173*56bb7041Schristos
2174*56bb7041Schristos if (debug_threads)
2175*56bb7041Schristos debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
2176*56bb7041Schristos WSTOPSIG (*wstat), lwpid_of (thread));
2177*56bb7041Schristos
2178*56bb7041Schristos if (debug_threads)
2179*56bb7041Schristos {
2180*56bb7041Schristos for (const auto &sig : lwp->pending_signals_to_report)
2181*56bb7041Schristos debug_printf (" Still queued %d\n",
2182*56bb7041Schristos sig.signal);
2183*56bb7041Schristos
2184*56bb7041Schristos debug_printf (" (no more queued signals)\n");
2185*56bb7041Schristos }
2186*56bb7041Schristos
2187*56bb7041Schristos return 1;
2188*56bb7041Schristos }
2189*56bb7041Schristos
2190*56bb7041Schristos return 0;
2191*56bb7041Schristos }
2192*56bb7041Schristos
2193*56bb7041Schristos bool
check_stopped_by_watchpoint(lwp_info * child)2194*56bb7041Schristos linux_process_target::check_stopped_by_watchpoint (lwp_info *child)
2195*56bb7041Schristos {
2196*56bb7041Schristos struct thread_info *saved_thread = current_thread;
2197*56bb7041Schristos current_thread = get_lwp_thread (child);
2198*56bb7041Schristos
2199*56bb7041Schristos if (low_stopped_by_watchpoint ())
2200*56bb7041Schristos {
2201*56bb7041Schristos child->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
2202*56bb7041Schristos child->stopped_data_address = low_stopped_data_address ();
2203*56bb7041Schristos }
2204*56bb7041Schristos
2205*56bb7041Schristos current_thread = saved_thread;
2206*56bb7041Schristos
2207*56bb7041Schristos return child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2208*56bb7041Schristos }
2209*56bb7041Schristos
2210*56bb7041Schristos bool
low_stopped_by_watchpoint()2211*56bb7041Schristos linux_process_target::low_stopped_by_watchpoint ()
2212*56bb7041Schristos {
2213*56bb7041Schristos return false;
2214*56bb7041Schristos }
2215*56bb7041Schristos
2216*56bb7041Schristos CORE_ADDR
low_stopped_data_address()2217*56bb7041Schristos linux_process_target::low_stopped_data_address ()
2218*56bb7041Schristos {
2219*56bb7041Schristos return 0;
2220*56bb7041Schristos }
2221*56bb7041Schristos
2222*56bb7041Schristos /* Return the ptrace options that we want to try to enable. */
2223*56bb7041Schristos
2224*56bb7041Schristos static int
linux_low_ptrace_options(int attached)2225*56bb7041Schristos linux_low_ptrace_options (int attached)
2226*56bb7041Schristos {
2227*56bb7041Schristos client_state &cs = get_client_state ();
2228*56bb7041Schristos int options = 0;
2229*56bb7041Schristos
2230*56bb7041Schristos if (!attached)
2231*56bb7041Schristos options |= PTRACE_O_EXITKILL;
2232*56bb7041Schristos
2233*56bb7041Schristos if (cs.report_fork_events)
2234*56bb7041Schristos options |= PTRACE_O_TRACEFORK;
2235*56bb7041Schristos
2236*56bb7041Schristos if (cs.report_vfork_events)
2237*56bb7041Schristos options |= (PTRACE_O_TRACEVFORK | PTRACE_O_TRACEVFORKDONE);
2238*56bb7041Schristos
2239*56bb7041Schristos if (cs.report_exec_events)
2240*56bb7041Schristos options |= PTRACE_O_TRACEEXEC;
2241*56bb7041Schristos
2242*56bb7041Schristos options |= PTRACE_O_TRACESYSGOOD;
2243*56bb7041Schristos
2244*56bb7041Schristos return options;
2245*56bb7041Schristos }
2246*56bb7041Schristos
2247*56bb7041Schristos lwp_info *
filter_event(int lwpid,int wstat)2248*56bb7041Schristos linux_process_target::filter_event (int lwpid, int wstat)
2249*56bb7041Schristos {
2250*56bb7041Schristos client_state &cs = get_client_state ();
2251*56bb7041Schristos struct lwp_info *child;
2252*56bb7041Schristos struct thread_info *thread;
2253*56bb7041Schristos int have_stop_pc = 0;
2254*56bb7041Schristos
2255*56bb7041Schristos child = find_lwp_pid (ptid_t (lwpid));
2256*56bb7041Schristos
2257*56bb7041Schristos /* Check for stop events reported by a process we didn't already
2258*56bb7041Schristos know about - anything not already in our LWP list.
2259*56bb7041Schristos
2260*56bb7041Schristos If we're expecting to receive stopped processes after
2261*56bb7041Schristos fork, vfork, and clone events, then we'll just add the
2262*56bb7041Schristos new one to our list and go back to waiting for the event
2263*56bb7041Schristos to be reported - the stopped process might be returned
2264*56bb7041Schristos from waitpid before or after the event is.
2265*56bb7041Schristos
2266*56bb7041Schristos But note the case of a non-leader thread exec'ing after the
2267*56bb7041Schristos leader having exited, and gone from our lists (because
2268*56bb7041Schristos check_zombie_leaders deleted it). The non-leader thread
2269*56bb7041Schristos changes its tid to the tgid. */
2270*56bb7041Schristos
2271*56bb7041Schristos if (WIFSTOPPED (wstat) && child == NULL && WSTOPSIG (wstat) == SIGTRAP
2272*56bb7041Schristos && linux_ptrace_get_extended_event (wstat) == PTRACE_EVENT_EXEC)
2273*56bb7041Schristos {
2274*56bb7041Schristos ptid_t child_ptid;
2275*56bb7041Schristos
2276*56bb7041Schristos /* A multi-thread exec after we had seen the leader exiting. */
2277*56bb7041Schristos if (debug_threads)
2278*56bb7041Schristos {
2279*56bb7041Schristos debug_printf ("LLW: Re-adding thread group leader LWP %d"
2280*56bb7041Schristos "after exec.\n", lwpid);
2281*56bb7041Schristos }
2282*56bb7041Schristos
2283*56bb7041Schristos child_ptid = ptid_t (lwpid, lwpid, 0);
2284*56bb7041Schristos child = add_lwp (child_ptid);
2285*56bb7041Schristos child->stopped = 1;
2286*56bb7041Schristos current_thread = child->thread;
2287*56bb7041Schristos }
2288*56bb7041Schristos
2289*56bb7041Schristos /* If we didn't find a process, one of two things presumably happened:
2290*56bb7041Schristos - A process we started and then detached from has exited. Ignore it.
2291*56bb7041Schristos - A process we are controlling has forked and the new child's stop
2292*56bb7041Schristos was reported to us by the kernel. Save its PID. */
2293*56bb7041Schristos if (child == NULL && WIFSTOPPED (wstat))
2294*56bb7041Schristos {
2295*56bb7041Schristos add_to_pid_list (&stopped_pids, lwpid, wstat);
2296*56bb7041Schristos return NULL;
2297*56bb7041Schristos }
2298*56bb7041Schristos else if (child == NULL)
2299*56bb7041Schristos return NULL;
2300*56bb7041Schristos
2301*56bb7041Schristos thread = get_lwp_thread (child);
2302*56bb7041Schristos
2303*56bb7041Schristos child->stopped = 1;
2304*56bb7041Schristos
2305*56bb7041Schristos child->last_status = wstat;
2306*56bb7041Schristos
2307*56bb7041Schristos /* Check if the thread has exited. */
2308*56bb7041Schristos if ((WIFEXITED (wstat) || WIFSIGNALED (wstat)))
2309*56bb7041Schristos {
2310*56bb7041Schristos if (debug_threads)
2311*56bb7041Schristos debug_printf ("LLFE: %d exited.\n", lwpid);
2312*56bb7041Schristos
2313*56bb7041Schristos if (finish_step_over (child))
2314*56bb7041Schristos {
2315*56bb7041Schristos /* Unsuspend all other LWPs, and set them back running again. */
2316*56bb7041Schristos unsuspend_all_lwps (child);
2317*56bb7041Schristos }
2318*56bb7041Schristos
2319*56bb7041Schristos /* If there is at least one more LWP, then the exit signal was
2320*56bb7041Schristos not the end of the debugged application and should be
2321*56bb7041Schristos ignored, unless GDB wants to hear about thread exits. */
2322*56bb7041Schristos if (cs.report_thread_events
2323*56bb7041Schristos || last_thread_of_process_p (pid_of (thread)))
2324*56bb7041Schristos {
2325*56bb7041Schristos /* Since events are serialized to GDB core, and we can't
2326*56bb7041Schristos report this one right now. Leave the status pending for
2327*56bb7041Schristos the next time we're able to report it. */
2328*56bb7041Schristos mark_lwp_dead (child, wstat);
2329*56bb7041Schristos return child;
2330*56bb7041Schristos }
2331*56bb7041Schristos else
2332*56bb7041Schristos {
2333*56bb7041Schristos delete_lwp (child);
2334*56bb7041Schristos return NULL;
2335*56bb7041Schristos }
2336*56bb7041Schristos }
2337*56bb7041Schristos
2338*56bb7041Schristos gdb_assert (WIFSTOPPED (wstat));
2339*56bb7041Schristos
2340*56bb7041Schristos if (WIFSTOPPED (wstat))
2341*56bb7041Schristos {
2342*56bb7041Schristos struct process_info *proc;
2343*56bb7041Schristos
2344*56bb7041Schristos /* Architecture-specific setup after inferior is running. */
2345*56bb7041Schristos proc = find_process_pid (pid_of (thread));
2346*56bb7041Schristos if (proc->tdesc == NULL)
2347*56bb7041Schristos {
2348*56bb7041Schristos if (proc->attached)
2349*56bb7041Schristos {
2350*56bb7041Schristos /* This needs to happen after we have attached to the
2351*56bb7041Schristos inferior and it is stopped for the first time, but
2352*56bb7041Schristos before we access any inferior registers. */
2353*56bb7041Schristos arch_setup_thread (thread);
2354*56bb7041Schristos }
2355*56bb7041Schristos else
2356*56bb7041Schristos {
2357*56bb7041Schristos /* The process is started, but GDBserver will do
2358*56bb7041Schristos architecture-specific setup after the program stops at
2359*56bb7041Schristos the first instruction. */
2360*56bb7041Schristos child->status_pending_p = 1;
2361*56bb7041Schristos child->status_pending = wstat;
2362*56bb7041Schristos return child;
2363*56bb7041Schristos }
2364*56bb7041Schristos }
2365*56bb7041Schristos }
2366*56bb7041Schristos
2367*56bb7041Schristos if (WIFSTOPPED (wstat) && child->must_set_ptrace_flags)
2368*56bb7041Schristos {
2369*56bb7041Schristos struct process_info *proc = find_process_pid (pid_of (thread));
2370*56bb7041Schristos int options = linux_low_ptrace_options (proc->attached);
2371*56bb7041Schristos
2372*56bb7041Schristos linux_enable_event_reporting (lwpid, options);
2373*56bb7041Schristos child->must_set_ptrace_flags = 0;
2374*56bb7041Schristos }
2375*56bb7041Schristos
2376*56bb7041Schristos /* Always update syscall_state, even if it will be filtered later. */
2377*56bb7041Schristos if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SYSCALL_SIGTRAP)
2378*56bb7041Schristos {
2379*56bb7041Schristos child->syscall_state
2380*56bb7041Schristos = (child->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
2381*56bb7041Schristos ? TARGET_WAITKIND_SYSCALL_RETURN
2382*56bb7041Schristos : TARGET_WAITKIND_SYSCALL_ENTRY);
2383*56bb7041Schristos }
2384*56bb7041Schristos else
2385*56bb7041Schristos {
2386*56bb7041Schristos /* Almost all other ptrace-stops are known to be outside of system
2387*56bb7041Schristos calls, with further exceptions in handle_extended_wait. */
2388*56bb7041Schristos child->syscall_state = TARGET_WAITKIND_IGNORE;
2389*56bb7041Schristos }
2390*56bb7041Schristos
2391*56bb7041Schristos /* Be careful to not overwrite stop_pc until save_stop_reason is
2392*56bb7041Schristos called. */
2393*56bb7041Schristos if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGTRAP
2394*56bb7041Schristos && linux_is_extended_waitstatus (wstat))
2395*56bb7041Schristos {
2396*56bb7041Schristos child->stop_pc = get_pc (child);
2397*56bb7041Schristos if (handle_extended_wait (&child, wstat))
2398*56bb7041Schristos {
2399*56bb7041Schristos /* The event has been handled, so just return without
2400*56bb7041Schristos reporting it. */
2401*56bb7041Schristos return NULL;
2402*56bb7041Schristos }
2403*56bb7041Schristos }
2404*56bb7041Schristos
2405*56bb7041Schristos if (linux_wstatus_maybe_breakpoint (wstat))
2406*56bb7041Schristos {
2407*56bb7041Schristos if (save_stop_reason (child))
2408*56bb7041Schristos have_stop_pc = 1;
2409*56bb7041Schristos }
2410*56bb7041Schristos
2411*56bb7041Schristos if (!have_stop_pc)
2412*56bb7041Schristos child->stop_pc = get_pc (child);
2413*56bb7041Schristos
2414*56bb7041Schristos if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGSTOP
2415*56bb7041Schristos && child->stop_expected)
2416*56bb7041Schristos {
2417*56bb7041Schristos if (debug_threads)
2418*56bb7041Schristos debug_printf ("Expected stop.\n");
2419*56bb7041Schristos child->stop_expected = 0;
2420*56bb7041Schristos
2421*56bb7041Schristos if (thread->last_resume_kind == resume_stop)
2422*56bb7041Schristos {
2423*56bb7041Schristos /* We want to report the stop to the core. Treat the
2424*56bb7041Schristos SIGSTOP as a normal event. */
2425*56bb7041Schristos if (debug_threads)
2426*56bb7041Schristos debug_printf ("LLW: resume_stop SIGSTOP caught for %s.\n",
2427*56bb7041Schristos target_pid_to_str (ptid_of (thread)));
2428*56bb7041Schristos }
2429*56bb7041Schristos else if (stopping_threads != NOT_STOPPING_THREADS)
2430*56bb7041Schristos {
2431*56bb7041Schristos /* Stopping threads. We don't want this SIGSTOP to end up
2432*56bb7041Schristos pending. */
2433*56bb7041Schristos if (debug_threads)
2434*56bb7041Schristos debug_printf ("LLW: SIGSTOP caught for %s "
2435*56bb7041Schristos "while stopping threads.\n",
2436*56bb7041Schristos target_pid_to_str (ptid_of (thread)));
2437*56bb7041Schristos return NULL;
2438*56bb7041Schristos }
2439*56bb7041Schristos else
2440*56bb7041Schristos {
2441*56bb7041Schristos /* This is a delayed SIGSTOP. Filter out the event. */
2442*56bb7041Schristos if (debug_threads)
2443*56bb7041Schristos debug_printf ("LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2444*56bb7041Schristos child->stepping ? "step" : "continue",
2445*56bb7041Schristos target_pid_to_str (ptid_of (thread)));
2446*56bb7041Schristos
2447*56bb7041Schristos resume_one_lwp (child, child->stepping, 0, NULL);
2448*56bb7041Schristos return NULL;
2449*56bb7041Schristos }
2450*56bb7041Schristos }
2451*56bb7041Schristos
2452*56bb7041Schristos child->status_pending_p = 1;
2453*56bb7041Schristos child->status_pending = wstat;
2454*56bb7041Schristos return child;
2455*56bb7041Schristos }
2456*56bb7041Schristos
2457*56bb7041Schristos bool
maybe_hw_step(thread_info * thread)2458*56bb7041Schristos linux_process_target::maybe_hw_step (thread_info *thread)
2459*56bb7041Schristos {
2460*56bb7041Schristos if (supports_hardware_single_step ())
2461*56bb7041Schristos return true;
2462*56bb7041Schristos else
2463*56bb7041Schristos {
2464*56bb7041Schristos /* GDBserver must insert single-step breakpoint for software
2465*56bb7041Schristos single step. */
2466*56bb7041Schristos gdb_assert (has_single_step_breakpoints (thread));
2467*56bb7041Schristos return false;
2468*56bb7041Schristos }
2469*56bb7041Schristos }
2470*56bb7041Schristos
2471*56bb7041Schristos void
resume_stopped_resumed_lwps(thread_info * thread)2472*56bb7041Schristos linux_process_target::resume_stopped_resumed_lwps (thread_info *thread)
2473*56bb7041Schristos {
2474*56bb7041Schristos struct lwp_info *lp = get_thread_lwp (thread);
2475*56bb7041Schristos
2476*56bb7041Schristos if (lp->stopped
2477*56bb7041Schristos && !lp->suspended
2478*56bb7041Schristos && !lp->status_pending_p
2479*56bb7041Schristos && thread->last_status.kind == TARGET_WAITKIND_IGNORE)
2480*56bb7041Schristos {
2481*56bb7041Schristos int step = 0;
2482*56bb7041Schristos
2483*56bb7041Schristos if (thread->last_resume_kind == resume_step)
2484*56bb7041Schristos step = maybe_hw_step (thread);
2485*56bb7041Schristos
2486*56bb7041Schristos if (debug_threads)
2487*56bb7041Schristos debug_printf ("RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
2488*56bb7041Schristos target_pid_to_str (ptid_of (thread)),
2489*56bb7041Schristos paddress (lp->stop_pc),
2490*56bb7041Schristos step);
2491*56bb7041Schristos
2492*56bb7041Schristos resume_one_lwp (lp, step, GDB_SIGNAL_0, NULL);
2493*56bb7041Schristos }
2494*56bb7041Schristos }
2495*56bb7041Schristos
2496*56bb7041Schristos int
wait_for_event_filtered(ptid_t wait_ptid,ptid_t filter_ptid,int * wstatp,int options)2497*56bb7041Schristos linux_process_target::wait_for_event_filtered (ptid_t wait_ptid,
2498*56bb7041Schristos ptid_t filter_ptid,
2499*56bb7041Schristos int *wstatp, int options)
2500*56bb7041Schristos {
2501*56bb7041Schristos struct thread_info *event_thread;
2502*56bb7041Schristos struct lwp_info *event_child, *requested_child;
2503*56bb7041Schristos sigset_t block_mask, prev_mask;
2504*56bb7041Schristos
2505*56bb7041Schristos retry:
2506*56bb7041Schristos /* N.B. event_thread points to the thread_info struct that contains
2507*56bb7041Schristos event_child. Keep them in sync. */
2508*56bb7041Schristos event_thread = NULL;
2509*56bb7041Schristos event_child = NULL;
2510*56bb7041Schristos requested_child = NULL;
2511*56bb7041Schristos
2512*56bb7041Schristos /* Check for a lwp with a pending status. */
2513*56bb7041Schristos
2514*56bb7041Schristos if (filter_ptid == minus_one_ptid || filter_ptid.is_pid ())
2515*56bb7041Schristos {
2516*56bb7041Schristos event_thread = find_thread_in_random ([&] (thread_info *thread)
2517*56bb7041Schristos {
2518*56bb7041Schristos return status_pending_p_callback (thread, filter_ptid);
2519*56bb7041Schristos });
2520*56bb7041Schristos
2521*56bb7041Schristos if (event_thread != NULL)
2522*56bb7041Schristos event_child = get_thread_lwp (event_thread);
2523*56bb7041Schristos if (debug_threads && event_thread)
2524*56bb7041Schristos debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread));
2525*56bb7041Schristos }
2526*56bb7041Schristos else if (filter_ptid != null_ptid)
2527*56bb7041Schristos {
2528*56bb7041Schristos requested_child = find_lwp_pid (filter_ptid);
2529*56bb7041Schristos
2530*56bb7041Schristos if (stopping_threads == NOT_STOPPING_THREADS
2531*56bb7041Schristos && requested_child->status_pending_p
2532*56bb7041Schristos && (requested_child->collecting_fast_tracepoint
2533*56bb7041Schristos != fast_tpoint_collect_result::not_collecting))
2534*56bb7041Schristos {
2535*56bb7041Schristos enqueue_one_deferred_signal (requested_child,
2536*56bb7041Schristos &requested_child->status_pending);
2537*56bb7041Schristos requested_child->status_pending_p = 0;
2538*56bb7041Schristos requested_child->status_pending = 0;
2539*56bb7041Schristos resume_one_lwp (requested_child, 0, 0, NULL);
2540*56bb7041Schristos }
2541*56bb7041Schristos
2542*56bb7041Schristos if (requested_child->suspended
2543*56bb7041Schristos && requested_child->status_pending_p)
2544*56bb7041Schristos {
2545*56bb7041Schristos internal_error (__FILE__, __LINE__,
2546*56bb7041Schristos "requesting an event out of a"
2547*56bb7041Schristos " suspended child?");
2548*56bb7041Schristos }
2549*56bb7041Schristos
2550*56bb7041Schristos if (requested_child->status_pending_p)
2551*56bb7041Schristos {
2552*56bb7041Schristos event_child = requested_child;
2553*56bb7041Schristos event_thread = get_lwp_thread (event_child);
2554*56bb7041Schristos }
2555*56bb7041Schristos }
2556*56bb7041Schristos
2557*56bb7041Schristos if (event_child != NULL)
2558*56bb7041Schristos {
2559*56bb7041Schristos if (debug_threads)
2560*56bb7041Schristos debug_printf ("Got an event from pending child %ld (%04x)\n",
2561*56bb7041Schristos lwpid_of (event_thread), event_child->status_pending);
2562*56bb7041Schristos *wstatp = event_child->status_pending;
2563*56bb7041Schristos event_child->status_pending_p = 0;
2564*56bb7041Schristos event_child->status_pending = 0;
2565*56bb7041Schristos current_thread = event_thread;
2566*56bb7041Schristos return lwpid_of (event_thread);
2567*56bb7041Schristos }
2568*56bb7041Schristos
2569*56bb7041Schristos /* But if we don't find a pending event, we'll have to wait.
2570*56bb7041Schristos
2571*56bb7041Schristos We only enter this loop if no process has a pending wait status.
2572*56bb7041Schristos Thus any action taken in response to a wait status inside this
2573*56bb7041Schristos loop is responding as soon as we detect the status, not after any
2574*56bb7041Schristos pending events. */
2575*56bb7041Schristos
2576*56bb7041Schristos /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2577*56bb7041Schristos all signals while here. */
2578*56bb7041Schristos sigfillset (&block_mask);
2579*56bb7041Schristos gdb_sigmask (SIG_BLOCK, &block_mask, &prev_mask);
2580*56bb7041Schristos
2581*56bb7041Schristos /* Always pull all events out of the kernel. We'll randomly select
2582*56bb7041Schristos an event LWP out of all that have events, to prevent
2583*56bb7041Schristos starvation. */
2584*56bb7041Schristos while (event_child == NULL)
2585*56bb7041Schristos {
2586*56bb7041Schristos pid_t ret = 0;
2587*56bb7041Schristos
2588*56bb7041Schristos /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2589*56bb7041Schristos quirks:
2590*56bb7041Schristos
2591*56bb7041Schristos - If the thread group leader exits while other threads in the
2592*56bb7041Schristos thread group still exist, waitpid(TGID, ...) hangs. That
2593*56bb7041Schristos waitpid won't return an exit status until the other threads
2594*56bb7041Schristos in the group are reaped.
2595*56bb7041Schristos
2596*56bb7041Schristos - When a non-leader thread execs, that thread just vanishes
2597*56bb7041Schristos without reporting an exit (so we'd hang if we waited for it
2598*56bb7041Schristos explicitly in that case). The exec event is reported to
2599*56bb7041Schristos the TGID pid. */
2600*56bb7041Schristos errno = 0;
2601*56bb7041Schristos ret = my_waitpid (-1, wstatp, options | WNOHANG);
2602*56bb7041Schristos
2603*56bb7041Schristos if (debug_threads)
2604*56bb7041Schristos debug_printf ("LWFE: waitpid(-1, ...) returned %d, %s\n",
2605*56bb7041Schristos ret, errno ? safe_strerror (errno) : "ERRNO-OK");
2606*56bb7041Schristos
2607*56bb7041Schristos if (ret > 0)
2608*56bb7041Schristos {
2609*56bb7041Schristos if (debug_threads)
2610*56bb7041Schristos {
2611*56bb7041Schristos debug_printf ("LLW: waitpid %ld received %s\n",
2612*56bb7041Schristos (long) ret, status_to_str (*wstatp));
2613*56bb7041Schristos }
2614*56bb7041Schristos
2615*56bb7041Schristos /* Filter all events. IOW, leave all events pending. We'll
2616*56bb7041Schristos randomly select an event LWP out of all that have events
2617*56bb7041Schristos below. */
2618*56bb7041Schristos filter_event (ret, *wstatp);
2619*56bb7041Schristos /* Retry until nothing comes out of waitpid. A single
2620*56bb7041Schristos SIGCHLD can indicate more than one child stopped. */
2621*56bb7041Schristos continue;
2622*56bb7041Schristos }
2623*56bb7041Schristos
2624*56bb7041Schristos /* Now that we've pulled all events out of the kernel, resume
2625*56bb7041Schristos LWPs that don't have an interesting event to report. */
2626*56bb7041Schristos if (stopping_threads == NOT_STOPPING_THREADS)
2627*56bb7041Schristos for_each_thread ([this] (thread_info *thread)
2628*56bb7041Schristos {
2629*56bb7041Schristos resume_stopped_resumed_lwps (thread);
2630*56bb7041Schristos });
2631*56bb7041Schristos
2632*56bb7041Schristos /* ... and find an LWP with a status to report to the core, if
2633*56bb7041Schristos any. */
2634*56bb7041Schristos event_thread = find_thread_in_random ([&] (thread_info *thread)
2635*56bb7041Schristos {
2636*56bb7041Schristos return status_pending_p_callback (thread, filter_ptid);
2637*56bb7041Schristos });
2638*56bb7041Schristos
2639*56bb7041Schristos if (event_thread != NULL)
2640*56bb7041Schristos {
2641*56bb7041Schristos event_child = get_thread_lwp (event_thread);
2642*56bb7041Schristos *wstatp = event_child->status_pending;
2643*56bb7041Schristos event_child->status_pending_p = 0;
2644*56bb7041Schristos event_child->status_pending = 0;
2645*56bb7041Schristos break;
2646*56bb7041Schristos }
2647*56bb7041Schristos
2648*56bb7041Schristos /* Check for zombie thread group leaders. Those can't be reaped
2649*56bb7041Schristos until all other threads in the thread group are. */
2650*56bb7041Schristos check_zombie_leaders ();
2651*56bb7041Schristos
2652*56bb7041Schristos auto not_stopped = [&] (thread_info *thread)
2653*56bb7041Schristos {
2654*56bb7041Schristos return not_stopped_callback (thread, wait_ptid);
2655*56bb7041Schristos };
2656*56bb7041Schristos
2657*56bb7041Schristos /* If there are no resumed children left in the set of LWPs we
2658*56bb7041Schristos want to wait for, bail. We can't just block in
2659*56bb7041Schristos waitpid/sigsuspend, because lwps might have been left stopped
2660*56bb7041Schristos in trace-stop state, and we'd be stuck forever waiting for
2661*56bb7041Schristos their status to change (which would only happen if we resumed
2662*56bb7041Schristos them). Even if WNOHANG is set, this return code is preferred
2663*56bb7041Schristos over 0 (below), as it is more detailed. */
2664*56bb7041Schristos if (find_thread (not_stopped) == NULL)
2665*56bb7041Schristos {
2666*56bb7041Schristos if (debug_threads)
2667*56bb7041Schristos debug_printf ("LLW: exit (no unwaited-for LWP)\n");
2668*56bb7041Schristos gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2669*56bb7041Schristos return -1;
2670*56bb7041Schristos }
2671*56bb7041Schristos
2672*56bb7041Schristos /* No interesting event to report to the caller. */
2673*56bb7041Schristos if ((options & WNOHANG))
2674*56bb7041Schristos {
2675*56bb7041Schristos if (debug_threads)
2676*56bb7041Schristos debug_printf ("WNOHANG set, no event found\n");
2677*56bb7041Schristos
2678*56bb7041Schristos gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2679*56bb7041Schristos return 0;
2680*56bb7041Schristos }
2681*56bb7041Schristos
2682*56bb7041Schristos /* Block until we get an event reported with SIGCHLD. */
2683*56bb7041Schristos if (debug_threads)
2684*56bb7041Schristos debug_printf ("sigsuspend'ing\n");
2685*56bb7041Schristos
2686*56bb7041Schristos sigsuspend (&prev_mask);
2687*56bb7041Schristos gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2688*56bb7041Schristos goto retry;
2689*56bb7041Schristos }
2690*56bb7041Schristos
2691*56bb7041Schristos gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2692*56bb7041Schristos
2693*56bb7041Schristos current_thread = event_thread;
2694*56bb7041Schristos
2695*56bb7041Schristos return lwpid_of (event_thread);
2696*56bb7041Schristos }
2697*56bb7041Schristos
2698*56bb7041Schristos int
wait_for_event(ptid_t ptid,int * wstatp,int options)2699*56bb7041Schristos linux_process_target::wait_for_event (ptid_t ptid, int *wstatp, int options)
2700*56bb7041Schristos {
2701*56bb7041Schristos return wait_for_event_filtered (ptid, ptid, wstatp, options);
2702*56bb7041Schristos }
2703*56bb7041Schristos
2704*56bb7041Schristos /* Select one LWP out of those that have events pending. */
2705*56bb7041Schristos
2706*56bb7041Schristos static void
select_event_lwp(struct lwp_info ** orig_lp)2707*56bb7041Schristos select_event_lwp (struct lwp_info **orig_lp)
2708*56bb7041Schristos {
2709*56bb7041Schristos struct thread_info *event_thread = NULL;
2710*56bb7041Schristos
2711*56bb7041Schristos /* In all-stop, give preference to the LWP that is being
2712*56bb7041Schristos single-stepped. There will be at most one, and it's the LWP that
2713*56bb7041Schristos the core is most interested in. If we didn't do this, then we'd
2714*56bb7041Schristos have to handle pending step SIGTRAPs somehow in case the core
2715*56bb7041Schristos later continues the previously-stepped thread, otherwise we'd
2716*56bb7041Schristos report the pending SIGTRAP, and the core, not having stepped the
2717*56bb7041Schristos thread, wouldn't understand what the trap was for, and therefore
2718*56bb7041Schristos would report it to the user as a random signal. */
2719*56bb7041Schristos if (!non_stop)
2720*56bb7041Schristos {
2721*56bb7041Schristos event_thread = find_thread ([] (thread_info *thread)
2722*56bb7041Schristos {
2723*56bb7041Schristos lwp_info *lp = get_thread_lwp (thread);
2724*56bb7041Schristos
2725*56bb7041Schristos return (thread->last_status.kind == TARGET_WAITKIND_IGNORE
2726*56bb7041Schristos && thread->last_resume_kind == resume_step
2727*56bb7041Schristos && lp->status_pending_p);
2728*56bb7041Schristos });
2729*56bb7041Schristos
2730*56bb7041Schristos if (event_thread != NULL)
2731*56bb7041Schristos {
2732*56bb7041Schristos if (debug_threads)
2733*56bb7041Schristos debug_printf ("SEL: Select single-step %s\n",
2734*56bb7041Schristos target_pid_to_str (ptid_of (event_thread)));
2735*56bb7041Schristos }
2736*56bb7041Schristos }
2737*56bb7041Schristos if (event_thread == NULL)
2738*56bb7041Schristos {
2739*56bb7041Schristos /* No single-stepping LWP. Select one at random, out of those
2740*56bb7041Schristos which have had events. */
2741*56bb7041Schristos
2742*56bb7041Schristos event_thread = find_thread_in_random ([&] (thread_info *thread)
2743*56bb7041Schristos {
2744*56bb7041Schristos lwp_info *lp = get_thread_lwp (thread);
2745*56bb7041Schristos
2746*56bb7041Schristos /* Only resumed LWPs that have an event pending. */
2747*56bb7041Schristos return (thread->last_status.kind == TARGET_WAITKIND_IGNORE
2748*56bb7041Schristos && lp->status_pending_p);
2749*56bb7041Schristos });
2750*56bb7041Schristos }
2751*56bb7041Schristos
2752*56bb7041Schristos if (event_thread != NULL)
2753*56bb7041Schristos {
2754*56bb7041Schristos struct lwp_info *event_lp = get_thread_lwp (event_thread);
2755*56bb7041Schristos
2756*56bb7041Schristos /* Switch the event LWP. */
2757*56bb7041Schristos *orig_lp = event_lp;
2758*56bb7041Schristos }
2759*56bb7041Schristos }
2760*56bb7041Schristos
2761*56bb7041Schristos /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2762*56bb7041Schristos NULL. */
2763*56bb7041Schristos
2764*56bb7041Schristos static void
unsuspend_all_lwps(struct lwp_info * except)2765*56bb7041Schristos unsuspend_all_lwps (struct lwp_info *except)
2766*56bb7041Schristos {
2767*56bb7041Schristos for_each_thread ([&] (thread_info *thread)
2768*56bb7041Schristos {
2769*56bb7041Schristos lwp_info *lwp = get_thread_lwp (thread);
2770*56bb7041Schristos
2771*56bb7041Schristos if (lwp != except)
2772*56bb7041Schristos lwp_suspended_decr (lwp);
2773*56bb7041Schristos });
2774*56bb7041Schristos }
2775*56bb7041Schristos
2776*56bb7041Schristos static bool lwp_running (thread_info *thread);
2777*56bb7041Schristos
2778*56bb7041Schristos /* Stabilize threads (move out of jump pads).
2779*56bb7041Schristos
2780*56bb7041Schristos If a thread is midway collecting a fast tracepoint, we need to
2781*56bb7041Schristos finish the collection and move it out of the jump pad before
2782*56bb7041Schristos reporting the signal.
2783*56bb7041Schristos
2784*56bb7041Schristos This avoids recursion while collecting (when a signal arrives
2785*56bb7041Schristos midway, and the signal handler itself collects), which would trash
2786*56bb7041Schristos the trace buffer. In case the user set a breakpoint in a signal
2787*56bb7041Schristos handler, this avoids the backtrace showing the jump pad, etc..
2788*56bb7041Schristos Most importantly, there are certain things we can't do safely if
2789*56bb7041Schristos threads are stopped in a jump pad (or in its callee's). For
2790*56bb7041Schristos example:
2791*56bb7041Schristos
2792*56bb7041Schristos - starting a new trace run. A thread still collecting the
2793*56bb7041Schristos previous run, could trash the trace buffer when resumed. The trace
2794*56bb7041Schristos buffer control structures would have been reset but the thread had
2795*56bb7041Schristos no way to tell. The thread could even midway memcpy'ing to the
2796*56bb7041Schristos buffer, which would mean that when resumed, it would clobber the
2797*56bb7041Schristos trace buffer that had been set for a new run.
2798*56bb7041Schristos
2799*56bb7041Schristos - we can't rewrite/reuse the jump pads for new tracepoints
2800*56bb7041Schristos safely. Say you do tstart while a thread is stopped midway while
2801*56bb7041Schristos collecting. When the thread is later resumed, it finishes the
2802*56bb7041Schristos collection, and returns to the jump pad, to execute the original
2803*56bb7041Schristos instruction that was under the tracepoint jump at the time the
2804*56bb7041Schristos older run had been started. If the jump pad had been rewritten
2805*56bb7041Schristos since for something else in the new run, the thread would now
2806*56bb7041Schristos execute the wrong / random instructions. */
2807*56bb7041Schristos
2808*56bb7041Schristos void
stabilize_threads()2809*56bb7041Schristos linux_process_target::stabilize_threads ()
2810*56bb7041Schristos {
2811*56bb7041Schristos thread_info *thread_stuck = find_thread ([this] (thread_info *thread)
2812*56bb7041Schristos {
2813*56bb7041Schristos return stuck_in_jump_pad (thread);
2814*56bb7041Schristos });
2815*56bb7041Schristos
2816*56bb7041Schristos if (thread_stuck != NULL)
2817*56bb7041Schristos {
2818*56bb7041Schristos if (debug_threads)
2819*56bb7041Schristos debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
2820*56bb7041Schristos lwpid_of (thread_stuck));
2821*56bb7041Schristos return;
2822*56bb7041Schristos }
2823*56bb7041Schristos
2824*56bb7041Schristos thread_info *saved_thread = current_thread;
2825*56bb7041Schristos
2826*56bb7041Schristos stabilizing_threads = 1;
2827*56bb7041Schristos
2828*56bb7041Schristos /* Kick 'em all. */
2829*56bb7041Schristos for_each_thread ([this] (thread_info *thread)
2830*56bb7041Schristos {
2831*56bb7041Schristos move_out_of_jump_pad (thread);
2832*56bb7041Schristos });
2833*56bb7041Schristos
2834*56bb7041Schristos /* Loop until all are stopped out of the jump pads. */
2835*56bb7041Schristos while (find_thread (lwp_running) != NULL)
2836*56bb7041Schristos {
2837*56bb7041Schristos struct target_waitstatus ourstatus;
2838*56bb7041Schristos struct lwp_info *lwp;
2839*56bb7041Schristos int wstat;
2840*56bb7041Schristos
2841*56bb7041Schristos /* Note that we go through the full wait even loop. While
2842*56bb7041Schristos moving threads out of jump pad, we need to be able to step
2843*56bb7041Schristos over internal breakpoints and such. */
2844*56bb7041Schristos wait_1 (minus_one_ptid, &ourstatus, 0);
2845*56bb7041Schristos
2846*56bb7041Schristos if (ourstatus.kind == TARGET_WAITKIND_STOPPED)
2847*56bb7041Schristos {
2848*56bb7041Schristos lwp = get_thread_lwp (current_thread);
2849*56bb7041Schristos
2850*56bb7041Schristos /* Lock it. */
2851*56bb7041Schristos lwp_suspended_inc (lwp);
2852*56bb7041Schristos
2853*56bb7041Schristos if (ourstatus.value.sig != GDB_SIGNAL_0
2854*56bb7041Schristos || current_thread->last_resume_kind == resume_stop)
2855*56bb7041Schristos {
2856*56bb7041Schristos wstat = W_STOPCODE (gdb_signal_to_host (ourstatus.value.sig));
2857*56bb7041Schristos enqueue_one_deferred_signal (lwp, &wstat);
2858*56bb7041Schristos }
2859*56bb7041Schristos }
2860*56bb7041Schristos }
2861*56bb7041Schristos
2862*56bb7041Schristos unsuspend_all_lwps (NULL);
2863*56bb7041Schristos
2864*56bb7041Schristos stabilizing_threads = 0;
2865*56bb7041Schristos
2866*56bb7041Schristos current_thread = saved_thread;
2867*56bb7041Schristos
2868*56bb7041Schristos if (debug_threads)
2869*56bb7041Schristos {
2870*56bb7041Schristos thread_stuck = find_thread ([this] (thread_info *thread)
2871*56bb7041Schristos {
2872*56bb7041Schristos return stuck_in_jump_pad (thread);
2873*56bb7041Schristos });
2874*56bb7041Schristos
2875*56bb7041Schristos if (thread_stuck != NULL)
2876*56bb7041Schristos debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
2877*56bb7041Schristos lwpid_of (thread_stuck));
2878*56bb7041Schristos }
2879*56bb7041Schristos }
2880*56bb7041Schristos
2881*56bb7041Schristos /* Convenience function that is called when the kernel reports an
2882*56bb7041Schristos event that is not passed out to GDB. */
2883*56bb7041Schristos
2884*56bb7041Schristos static ptid_t
ignore_event(struct target_waitstatus * ourstatus)2885*56bb7041Schristos ignore_event (struct target_waitstatus *ourstatus)
2886*56bb7041Schristos {
2887*56bb7041Schristos /* If we got an event, there may still be others, as a single
2888*56bb7041Schristos SIGCHLD can indicate more than one child stopped. This forces
2889*56bb7041Schristos another target_wait call. */
2890*56bb7041Schristos async_file_mark ();
2891*56bb7041Schristos
2892*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_IGNORE;
2893*56bb7041Schristos return null_ptid;
2894*56bb7041Schristos }
2895*56bb7041Schristos
2896*56bb7041Schristos ptid_t
filter_exit_event(lwp_info * event_child,target_waitstatus * ourstatus)2897*56bb7041Schristos linux_process_target::filter_exit_event (lwp_info *event_child,
2898*56bb7041Schristos target_waitstatus *ourstatus)
2899*56bb7041Schristos {
2900*56bb7041Schristos client_state &cs = get_client_state ();
2901*56bb7041Schristos struct thread_info *thread = get_lwp_thread (event_child);
2902*56bb7041Schristos ptid_t ptid = ptid_of (thread);
2903*56bb7041Schristos
2904*56bb7041Schristos if (!last_thread_of_process_p (pid_of (thread)))
2905*56bb7041Schristos {
2906*56bb7041Schristos if (cs.report_thread_events)
2907*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_THREAD_EXITED;
2908*56bb7041Schristos else
2909*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_IGNORE;
2910*56bb7041Schristos
2911*56bb7041Schristos delete_lwp (event_child);
2912*56bb7041Schristos }
2913*56bb7041Schristos return ptid;
2914*56bb7041Schristos }
2915*56bb7041Schristos
2916*56bb7041Schristos /* Returns 1 if GDB is interested in any event_child syscalls. */
2917*56bb7041Schristos
2918*56bb7041Schristos static int
gdb_catching_syscalls_p(struct lwp_info * event_child)2919*56bb7041Schristos gdb_catching_syscalls_p (struct lwp_info *event_child)
2920*56bb7041Schristos {
2921*56bb7041Schristos struct thread_info *thread = get_lwp_thread (event_child);
2922*56bb7041Schristos struct process_info *proc = get_thread_process (thread);
2923*56bb7041Schristos
2924*56bb7041Schristos return !proc->syscalls_to_catch.empty ();
2925*56bb7041Schristos }
2926*56bb7041Schristos
2927*56bb7041Schristos bool
gdb_catch_this_syscall(lwp_info * event_child)2928*56bb7041Schristos linux_process_target::gdb_catch_this_syscall (lwp_info *event_child)
2929*56bb7041Schristos {
2930*56bb7041Schristos int sysno;
2931*56bb7041Schristos struct thread_info *thread = get_lwp_thread (event_child);
2932*56bb7041Schristos struct process_info *proc = get_thread_process (thread);
2933*56bb7041Schristos
2934*56bb7041Schristos if (proc->syscalls_to_catch.empty ())
2935*56bb7041Schristos return false;
2936*56bb7041Schristos
2937*56bb7041Schristos if (proc->syscalls_to_catch[0] == ANY_SYSCALL)
2938*56bb7041Schristos return true;
2939*56bb7041Schristos
2940*56bb7041Schristos get_syscall_trapinfo (event_child, &sysno);
2941*56bb7041Schristos
2942*56bb7041Schristos for (int iter : proc->syscalls_to_catch)
2943*56bb7041Schristos if (iter == sysno)
2944*56bb7041Schristos return true;
2945*56bb7041Schristos
2946*56bb7041Schristos return false;
2947*56bb7041Schristos }
2948*56bb7041Schristos
2949*56bb7041Schristos ptid_t
wait_1(ptid_t ptid,target_waitstatus * ourstatus,int target_options)2950*56bb7041Schristos linux_process_target::wait_1 (ptid_t ptid, target_waitstatus *ourstatus,
2951*56bb7041Schristos int target_options)
2952*56bb7041Schristos {
2953*56bb7041Schristos client_state &cs = get_client_state ();
2954*56bb7041Schristos int w;
2955*56bb7041Schristos struct lwp_info *event_child;
2956*56bb7041Schristos int options;
2957*56bb7041Schristos int pid;
2958*56bb7041Schristos int step_over_finished;
2959*56bb7041Schristos int bp_explains_trap;
2960*56bb7041Schristos int maybe_internal_trap;
2961*56bb7041Schristos int report_to_gdb;
2962*56bb7041Schristos int trace_event;
2963*56bb7041Schristos int in_step_range;
2964*56bb7041Schristos int any_resumed;
2965*56bb7041Schristos
2966*56bb7041Schristos if (debug_threads)
2967*56bb7041Schristos {
2968*56bb7041Schristos debug_enter ();
2969*56bb7041Schristos debug_printf ("wait_1: [%s]\n", target_pid_to_str (ptid));
2970*56bb7041Schristos }
2971*56bb7041Schristos
2972*56bb7041Schristos /* Translate generic target options into linux options. */
2973*56bb7041Schristos options = __WALL;
2974*56bb7041Schristos if (target_options & TARGET_WNOHANG)
2975*56bb7041Schristos options |= WNOHANG;
2976*56bb7041Schristos
2977*56bb7041Schristos bp_explains_trap = 0;
2978*56bb7041Schristos trace_event = 0;
2979*56bb7041Schristos in_step_range = 0;
2980*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_IGNORE;
2981*56bb7041Schristos
2982*56bb7041Schristos auto status_pending_p_any = [&] (thread_info *thread)
2983*56bb7041Schristos {
2984*56bb7041Schristos return status_pending_p_callback (thread, minus_one_ptid);
2985*56bb7041Schristos };
2986*56bb7041Schristos
2987*56bb7041Schristos auto not_stopped = [&] (thread_info *thread)
2988*56bb7041Schristos {
2989*56bb7041Schristos return not_stopped_callback (thread, minus_one_ptid);
2990*56bb7041Schristos };
2991*56bb7041Schristos
2992*56bb7041Schristos /* Find a resumed LWP, if any. */
2993*56bb7041Schristos if (find_thread (status_pending_p_any) != NULL)
2994*56bb7041Schristos any_resumed = 1;
2995*56bb7041Schristos else if (find_thread (not_stopped) != NULL)
2996*56bb7041Schristos any_resumed = 1;
2997*56bb7041Schristos else
2998*56bb7041Schristos any_resumed = 0;
2999*56bb7041Schristos
3000*56bb7041Schristos if (step_over_bkpt == null_ptid)
3001*56bb7041Schristos pid = wait_for_event (ptid, &w, options);
3002*56bb7041Schristos else
3003*56bb7041Schristos {
3004*56bb7041Schristos if (debug_threads)
3005*56bb7041Schristos debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
3006*56bb7041Schristos target_pid_to_str (step_over_bkpt));
3007*56bb7041Schristos pid = wait_for_event (step_over_bkpt, &w, options & ~WNOHANG);
3008*56bb7041Schristos }
3009*56bb7041Schristos
3010*56bb7041Schristos if (pid == 0 || (pid == -1 && !any_resumed))
3011*56bb7041Schristos {
3012*56bb7041Schristos gdb_assert (target_options & TARGET_WNOHANG);
3013*56bb7041Schristos
3014*56bb7041Schristos if (debug_threads)
3015*56bb7041Schristos {
3016*56bb7041Schristos debug_printf ("wait_1 ret = null_ptid, "
3017*56bb7041Schristos "TARGET_WAITKIND_IGNORE\n");
3018*56bb7041Schristos debug_exit ();
3019*56bb7041Schristos }
3020*56bb7041Schristos
3021*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_IGNORE;
3022*56bb7041Schristos return null_ptid;
3023*56bb7041Schristos }
3024*56bb7041Schristos else if (pid == -1)
3025*56bb7041Schristos {
3026*56bb7041Schristos if (debug_threads)
3027*56bb7041Schristos {
3028*56bb7041Schristos debug_printf ("wait_1 ret = null_ptid, "
3029*56bb7041Schristos "TARGET_WAITKIND_NO_RESUMED\n");
3030*56bb7041Schristos debug_exit ();
3031*56bb7041Schristos }
3032*56bb7041Schristos
3033*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_NO_RESUMED;
3034*56bb7041Schristos return null_ptid;
3035*56bb7041Schristos }
3036*56bb7041Schristos
3037*56bb7041Schristos event_child = get_thread_lwp (current_thread);
3038*56bb7041Schristos
3039*56bb7041Schristos /* wait_for_event only returns an exit status for the last
3040*56bb7041Schristos child of a process. Report it. */
3041*56bb7041Schristos if (WIFEXITED (w) || WIFSIGNALED (w))
3042*56bb7041Schristos {
3043*56bb7041Schristos if (WIFEXITED (w))
3044*56bb7041Schristos {
3045*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_EXITED;
3046*56bb7041Schristos ourstatus->value.integer = WEXITSTATUS (w);
3047*56bb7041Schristos
3048*56bb7041Schristos if (debug_threads)
3049*56bb7041Schristos {
3050*56bb7041Schristos debug_printf ("wait_1 ret = %s, exited with "
3051*56bb7041Schristos "retcode %d\n",
3052*56bb7041Schristos target_pid_to_str (ptid_of (current_thread)),
3053*56bb7041Schristos WEXITSTATUS (w));
3054*56bb7041Schristos debug_exit ();
3055*56bb7041Schristos }
3056*56bb7041Schristos }
3057*56bb7041Schristos else
3058*56bb7041Schristos {
3059*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
3060*56bb7041Schristos ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (w));
3061*56bb7041Schristos
3062*56bb7041Schristos if (debug_threads)
3063*56bb7041Schristos {
3064*56bb7041Schristos debug_printf ("wait_1 ret = %s, terminated with "
3065*56bb7041Schristos "signal %d\n",
3066*56bb7041Schristos target_pid_to_str (ptid_of (current_thread)),
3067*56bb7041Schristos WTERMSIG (w));
3068*56bb7041Schristos debug_exit ();
3069*56bb7041Schristos }
3070*56bb7041Schristos }
3071*56bb7041Schristos
3072*56bb7041Schristos if (ourstatus->kind == TARGET_WAITKIND_EXITED)
3073*56bb7041Schristos return filter_exit_event (event_child, ourstatus);
3074*56bb7041Schristos
3075*56bb7041Schristos return ptid_of (current_thread);
3076*56bb7041Schristos }
3077*56bb7041Schristos
3078*56bb7041Schristos /* If step-over executes a breakpoint instruction, in the case of a
3079*56bb7041Schristos hardware single step it means a gdb/gdbserver breakpoint had been
3080*56bb7041Schristos planted on top of a permanent breakpoint, in the case of a software
3081*56bb7041Schristos single step it may just mean that gdbserver hit the reinsert breakpoint.
3082*56bb7041Schristos The PC has been adjusted by save_stop_reason to point at
3083*56bb7041Schristos the breakpoint address.
3084*56bb7041Schristos So in the case of the hardware single step advance the PC manually
3085*56bb7041Schristos past the breakpoint and in the case of software single step advance only
3086*56bb7041Schristos if it's not the single_step_breakpoint we are hitting.
3087*56bb7041Schristos This avoids that a program would keep trapping a permanent breakpoint
3088*56bb7041Schristos forever. */
3089*56bb7041Schristos if (step_over_bkpt != null_ptid
3090*56bb7041Schristos && event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
3091*56bb7041Schristos && (event_child->stepping
3092*56bb7041Schristos || !single_step_breakpoint_inserted_here (event_child->stop_pc)))
3093*56bb7041Schristos {
3094*56bb7041Schristos int increment_pc = 0;
3095*56bb7041Schristos int breakpoint_kind = 0;
3096*56bb7041Schristos CORE_ADDR stop_pc = event_child->stop_pc;
3097*56bb7041Schristos
3098*56bb7041Schristos breakpoint_kind = breakpoint_kind_from_current_state (&stop_pc);
3099*56bb7041Schristos sw_breakpoint_from_kind (breakpoint_kind, &increment_pc);
3100*56bb7041Schristos
3101*56bb7041Schristos if (debug_threads)
3102*56bb7041Schristos {
3103*56bb7041Schristos debug_printf ("step-over for %s executed software breakpoint\n",
3104*56bb7041Schristos target_pid_to_str (ptid_of (current_thread)));
3105*56bb7041Schristos }
3106*56bb7041Schristos
3107*56bb7041Schristos if (increment_pc != 0)
3108*56bb7041Schristos {
3109*56bb7041Schristos struct regcache *regcache
3110*56bb7041Schristos = get_thread_regcache (current_thread, 1);
3111*56bb7041Schristos
3112*56bb7041Schristos event_child->stop_pc += increment_pc;
3113*56bb7041Schristos low_set_pc (regcache, event_child->stop_pc);
3114*56bb7041Schristos
3115*56bb7041Schristos if (!low_breakpoint_at (event_child->stop_pc))
3116*56bb7041Schristos event_child->stop_reason = TARGET_STOPPED_BY_NO_REASON;
3117*56bb7041Schristos }
3118*56bb7041Schristos }
3119*56bb7041Schristos
3120*56bb7041Schristos /* If this event was not handled before, and is not a SIGTRAP, we
3121*56bb7041Schristos report it. SIGILL and SIGSEGV are also treated as traps in case
3122*56bb7041Schristos a breakpoint is inserted at the current PC. If this target does
3123*56bb7041Schristos not support internal breakpoints at all, we also report the
3124*56bb7041Schristos SIGTRAP without further processing; it's of no concern to us. */
3125*56bb7041Schristos maybe_internal_trap
3126*56bb7041Schristos = (low_supports_breakpoints ()
3127*56bb7041Schristos && (WSTOPSIG (w) == SIGTRAP
3128*56bb7041Schristos || ((WSTOPSIG (w) == SIGILL
3129*56bb7041Schristos || WSTOPSIG (w) == SIGSEGV)
3130*56bb7041Schristos && low_breakpoint_at (event_child->stop_pc))));
3131*56bb7041Schristos
3132*56bb7041Schristos if (maybe_internal_trap)
3133*56bb7041Schristos {
3134*56bb7041Schristos /* Handle anything that requires bookkeeping before deciding to
3135*56bb7041Schristos report the event or continue waiting. */
3136*56bb7041Schristos
3137*56bb7041Schristos /* First check if we can explain the SIGTRAP with an internal
3138*56bb7041Schristos breakpoint, or if we should possibly report the event to GDB.
3139*56bb7041Schristos Do this before anything that may remove or insert a
3140*56bb7041Schristos breakpoint. */
3141*56bb7041Schristos bp_explains_trap = breakpoint_inserted_here (event_child->stop_pc);
3142*56bb7041Schristos
3143*56bb7041Schristos /* We have a SIGTRAP, possibly a step-over dance has just
3144*56bb7041Schristos finished. If so, tweak the state machine accordingly,
3145*56bb7041Schristos reinsert breakpoints and delete any single-step
3146*56bb7041Schristos breakpoints. */
3147*56bb7041Schristos step_over_finished = finish_step_over (event_child);
3148*56bb7041Schristos
3149*56bb7041Schristos /* Now invoke the callbacks of any internal breakpoints there. */
3150*56bb7041Schristos check_breakpoints (event_child->stop_pc);
3151*56bb7041Schristos
3152*56bb7041Schristos /* Handle tracepoint data collecting. This may overflow the
3153*56bb7041Schristos trace buffer, and cause a tracing stop, removing
3154*56bb7041Schristos breakpoints. */
3155*56bb7041Schristos trace_event = handle_tracepoints (event_child);
3156*56bb7041Schristos
3157*56bb7041Schristos if (bp_explains_trap)
3158*56bb7041Schristos {
3159*56bb7041Schristos if (debug_threads)
3160*56bb7041Schristos debug_printf ("Hit a gdbserver breakpoint.\n");
3161*56bb7041Schristos }
3162*56bb7041Schristos }
3163*56bb7041Schristos else
3164*56bb7041Schristos {
3165*56bb7041Schristos /* We have some other signal, possibly a step-over dance was in
3166*56bb7041Schristos progress, and it should be cancelled too. */
3167*56bb7041Schristos step_over_finished = finish_step_over (event_child);
3168*56bb7041Schristos }
3169*56bb7041Schristos
3170*56bb7041Schristos /* We have all the data we need. Either report the event to GDB, or
3171*56bb7041Schristos resume threads and keep waiting for more. */
3172*56bb7041Schristos
3173*56bb7041Schristos /* If we're collecting a fast tracepoint, finish the collection and
3174*56bb7041Schristos move out of the jump pad before delivering a signal. See
3175*56bb7041Schristos linux_stabilize_threads. */
3176*56bb7041Schristos
3177*56bb7041Schristos if (WIFSTOPPED (w)
3178*56bb7041Schristos && WSTOPSIG (w) != SIGTRAP
3179*56bb7041Schristos && supports_fast_tracepoints ()
3180*56bb7041Schristos && agent_loaded_p ())
3181*56bb7041Schristos {
3182*56bb7041Schristos if (debug_threads)
3183*56bb7041Schristos debug_printf ("Got signal %d for LWP %ld. Check if we need "
3184*56bb7041Schristos "to defer or adjust it.\n",
3185*56bb7041Schristos WSTOPSIG (w), lwpid_of (current_thread));
3186*56bb7041Schristos
3187*56bb7041Schristos /* Allow debugging the jump pad itself. */
3188*56bb7041Schristos if (current_thread->last_resume_kind != resume_step
3189*56bb7041Schristos && maybe_move_out_of_jump_pad (event_child, &w))
3190*56bb7041Schristos {
3191*56bb7041Schristos enqueue_one_deferred_signal (event_child, &w);
3192*56bb7041Schristos
3193*56bb7041Schristos if (debug_threads)
3194*56bb7041Schristos debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
3195*56bb7041Schristos WSTOPSIG (w), lwpid_of (current_thread));
3196*56bb7041Schristos
3197*56bb7041Schristos resume_one_lwp (event_child, 0, 0, NULL);
3198*56bb7041Schristos
3199*56bb7041Schristos if (debug_threads)
3200*56bb7041Schristos debug_exit ();
3201*56bb7041Schristos return ignore_event (ourstatus);
3202*56bb7041Schristos }
3203*56bb7041Schristos }
3204*56bb7041Schristos
3205*56bb7041Schristos if (event_child->collecting_fast_tracepoint
3206*56bb7041Schristos != fast_tpoint_collect_result::not_collecting)
3207*56bb7041Schristos {
3208*56bb7041Schristos if (debug_threads)
3209*56bb7041Schristos debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
3210*56bb7041Schristos "Check if we're already there.\n",
3211*56bb7041Schristos lwpid_of (current_thread),
3212*56bb7041Schristos (int) event_child->collecting_fast_tracepoint);
3213*56bb7041Schristos
3214*56bb7041Schristos trace_event = 1;
3215*56bb7041Schristos
3216*56bb7041Schristos event_child->collecting_fast_tracepoint
3217*56bb7041Schristos = linux_fast_tracepoint_collecting (event_child, NULL);
3218*56bb7041Schristos
3219*56bb7041Schristos if (event_child->collecting_fast_tracepoint
3220*56bb7041Schristos != fast_tpoint_collect_result::before_insn)
3221*56bb7041Schristos {
3222*56bb7041Schristos /* No longer need this breakpoint. */
3223*56bb7041Schristos if (event_child->exit_jump_pad_bkpt != NULL)
3224*56bb7041Schristos {
3225*56bb7041Schristos if (debug_threads)
3226*56bb7041Schristos debug_printf ("No longer need exit-jump-pad bkpt; removing it."
3227*56bb7041Schristos "stopping all threads momentarily.\n");
3228*56bb7041Schristos
3229*56bb7041Schristos /* Other running threads could hit this breakpoint.
3230*56bb7041Schristos We don't handle moribund locations like GDB does,
3231*56bb7041Schristos instead we always pause all threads when removing
3232*56bb7041Schristos breakpoints, so that any step-over or
3233*56bb7041Schristos decr_pc_after_break adjustment is always taken
3234*56bb7041Schristos care of while the breakpoint is still
3235*56bb7041Schristos inserted. */
3236*56bb7041Schristos stop_all_lwps (1, event_child);
3237*56bb7041Schristos
3238*56bb7041Schristos delete_breakpoint (event_child->exit_jump_pad_bkpt);
3239*56bb7041Schristos event_child->exit_jump_pad_bkpt = NULL;
3240*56bb7041Schristos
3241*56bb7041Schristos unstop_all_lwps (1, event_child);
3242*56bb7041Schristos
3243*56bb7041Schristos gdb_assert (event_child->suspended >= 0);
3244*56bb7041Schristos }
3245*56bb7041Schristos }
3246*56bb7041Schristos
3247*56bb7041Schristos if (event_child->collecting_fast_tracepoint
3248*56bb7041Schristos == fast_tpoint_collect_result::not_collecting)
3249*56bb7041Schristos {
3250*56bb7041Schristos if (debug_threads)
3251*56bb7041Schristos debug_printf ("fast tracepoint finished "
3252*56bb7041Schristos "collecting successfully.\n");
3253*56bb7041Schristos
3254*56bb7041Schristos /* We may have a deferred signal to report. */
3255*56bb7041Schristos if (dequeue_one_deferred_signal (event_child, &w))
3256*56bb7041Schristos {
3257*56bb7041Schristos if (debug_threads)
3258*56bb7041Schristos debug_printf ("dequeued one signal.\n");
3259*56bb7041Schristos }
3260*56bb7041Schristos else
3261*56bb7041Schristos {
3262*56bb7041Schristos if (debug_threads)
3263*56bb7041Schristos debug_printf ("no deferred signals.\n");
3264*56bb7041Schristos
3265*56bb7041Schristos if (stabilizing_threads)
3266*56bb7041Schristos {
3267*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_STOPPED;
3268*56bb7041Schristos ourstatus->value.sig = GDB_SIGNAL_0;
3269*56bb7041Schristos
3270*56bb7041Schristos if (debug_threads)
3271*56bb7041Schristos {
3272*56bb7041Schristos debug_printf ("wait_1 ret = %s, stopped "
3273*56bb7041Schristos "while stabilizing threads\n",
3274*56bb7041Schristos target_pid_to_str (ptid_of (current_thread)));
3275*56bb7041Schristos debug_exit ();
3276*56bb7041Schristos }
3277*56bb7041Schristos
3278*56bb7041Schristos return ptid_of (current_thread);
3279*56bb7041Schristos }
3280*56bb7041Schristos }
3281*56bb7041Schristos }
3282*56bb7041Schristos }
3283*56bb7041Schristos
3284*56bb7041Schristos /* Check whether GDB would be interested in this event. */
3285*56bb7041Schristos
3286*56bb7041Schristos /* Check if GDB is interested in this syscall. */
3287*56bb7041Schristos if (WIFSTOPPED (w)
3288*56bb7041Schristos && WSTOPSIG (w) == SYSCALL_SIGTRAP
3289*56bb7041Schristos && !gdb_catch_this_syscall (event_child))
3290*56bb7041Schristos {
3291*56bb7041Schristos if (debug_threads)
3292*56bb7041Schristos {
3293*56bb7041Schristos debug_printf ("Ignored syscall for LWP %ld.\n",
3294*56bb7041Schristos lwpid_of (current_thread));
3295*56bb7041Schristos }
3296*56bb7041Schristos
3297*56bb7041Schristos resume_one_lwp (event_child, event_child->stepping, 0, NULL);
3298*56bb7041Schristos
3299*56bb7041Schristos if (debug_threads)
3300*56bb7041Schristos debug_exit ();
3301*56bb7041Schristos return ignore_event (ourstatus);
3302*56bb7041Schristos }
3303*56bb7041Schristos
3304*56bb7041Schristos /* If GDB is not interested in this signal, don't stop other
3305*56bb7041Schristos threads, and don't report it to GDB. Just resume the inferior
3306*56bb7041Schristos right away. We do this for threading-related signals as well as
3307*56bb7041Schristos any that GDB specifically requested we ignore. But never ignore
3308*56bb7041Schristos SIGSTOP if we sent it ourselves, and do not ignore signals when
3309*56bb7041Schristos stepping - they may require special handling to skip the signal
3310*56bb7041Schristos handler. Also never ignore signals that could be caused by a
3311*56bb7041Schristos breakpoint. */
3312*56bb7041Schristos if (WIFSTOPPED (w)
3313*56bb7041Schristos && current_thread->last_resume_kind != resume_step
3314*56bb7041Schristos && (
3315*56bb7041Schristos #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3316*56bb7041Schristos (current_process ()->priv->thread_db != NULL
3317*56bb7041Schristos && (WSTOPSIG (w) == __SIGRTMIN
3318*56bb7041Schristos || WSTOPSIG (w) == __SIGRTMIN + 1))
3319*56bb7041Schristos ||
3320*56bb7041Schristos #endif
3321*56bb7041Schristos (cs.pass_signals[gdb_signal_from_host (WSTOPSIG (w))]
3322*56bb7041Schristos && !(WSTOPSIG (w) == SIGSTOP
3323*56bb7041Schristos && current_thread->last_resume_kind == resume_stop)
3324*56bb7041Schristos && !linux_wstatus_maybe_breakpoint (w))))
3325*56bb7041Schristos {
3326*56bb7041Schristos siginfo_t info, *info_p;
3327*56bb7041Schristos
3328*56bb7041Schristos if (debug_threads)
3329*56bb7041Schristos debug_printf ("Ignored signal %d for LWP %ld.\n",
3330*56bb7041Schristos WSTOPSIG (w), lwpid_of (current_thread));
3331*56bb7041Schristos
3332*56bb7041Schristos if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
3333*56bb7041Schristos (PTRACE_TYPE_ARG3) 0, &info) == 0)
3334*56bb7041Schristos info_p = &info;
3335*56bb7041Schristos else
3336*56bb7041Schristos info_p = NULL;
3337*56bb7041Schristos
3338*56bb7041Schristos if (step_over_finished)
3339*56bb7041Schristos {
3340*56bb7041Schristos /* We cancelled this thread's step-over above. We still
3341*56bb7041Schristos need to unsuspend all other LWPs, and set them back
3342*56bb7041Schristos running again while the signal handler runs. */
3343*56bb7041Schristos unsuspend_all_lwps (event_child);
3344*56bb7041Schristos
3345*56bb7041Schristos /* Enqueue the pending signal info so that proceed_all_lwps
3346*56bb7041Schristos doesn't lose it. */
3347*56bb7041Schristos enqueue_pending_signal (event_child, WSTOPSIG (w), info_p);
3348*56bb7041Schristos
3349*56bb7041Schristos proceed_all_lwps ();
3350*56bb7041Schristos }
3351*56bb7041Schristos else
3352*56bb7041Schristos {
3353*56bb7041Schristos resume_one_lwp (event_child, event_child->stepping,
3354*56bb7041Schristos WSTOPSIG (w), info_p);
3355*56bb7041Schristos }
3356*56bb7041Schristos
3357*56bb7041Schristos if (debug_threads)
3358*56bb7041Schristos debug_exit ();
3359*56bb7041Schristos
3360*56bb7041Schristos return ignore_event (ourstatus);
3361*56bb7041Schristos }
3362*56bb7041Schristos
3363*56bb7041Schristos /* Note that all addresses are always "out of the step range" when
3364*56bb7041Schristos there's no range to begin with. */
3365*56bb7041Schristos in_step_range = lwp_in_step_range (event_child);
3366*56bb7041Schristos
3367*56bb7041Schristos /* If GDB wanted this thread to single step, and the thread is out
3368*56bb7041Schristos of the step range, we always want to report the SIGTRAP, and let
3369*56bb7041Schristos GDB handle it. Watchpoints should always be reported. So should
3370*56bb7041Schristos signals we can't explain. A SIGTRAP we can't explain could be a
3371*56bb7041Schristos GDB breakpoint --- we may or not support Z0 breakpoints. If we
3372*56bb7041Schristos do, we're be able to handle GDB breakpoints on top of internal
3373*56bb7041Schristos breakpoints, by handling the internal breakpoint and still
3374*56bb7041Schristos reporting the event to GDB. If we don't, we're out of luck, GDB
3375*56bb7041Schristos won't see the breakpoint hit. If we see a single-step event but
3376*56bb7041Schristos the thread should be continuing, don't pass the trap to gdb.
3377*56bb7041Schristos That indicates that we had previously finished a single-step but
3378*56bb7041Schristos left the single-step pending -- see
3379*56bb7041Schristos complete_ongoing_step_over. */
3380*56bb7041Schristos report_to_gdb = (!maybe_internal_trap
3381*56bb7041Schristos || (current_thread->last_resume_kind == resume_step
3382*56bb7041Schristos && !in_step_range)
3383*56bb7041Schristos || event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
3384*56bb7041Schristos || (!in_step_range
3385*56bb7041Schristos && !bp_explains_trap
3386*56bb7041Schristos && !trace_event
3387*56bb7041Schristos && !step_over_finished
3388*56bb7041Schristos && !(current_thread->last_resume_kind == resume_continue
3389*56bb7041Schristos && event_child->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP))
3390*56bb7041Schristos || (gdb_breakpoint_here (event_child->stop_pc)
3391*56bb7041Schristos && gdb_condition_true_at_breakpoint (event_child->stop_pc)
3392*56bb7041Schristos && gdb_no_commands_at_breakpoint (event_child->stop_pc))
3393*56bb7041Schristos || event_child->waitstatus.kind != TARGET_WAITKIND_IGNORE);
3394*56bb7041Schristos
3395*56bb7041Schristos run_breakpoint_commands (event_child->stop_pc);
3396*56bb7041Schristos
3397*56bb7041Schristos /* We found no reason GDB would want us to stop. We either hit one
3398*56bb7041Schristos of our own breakpoints, or finished an internal step GDB
3399*56bb7041Schristos shouldn't know about. */
3400*56bb7041Schristos if (!report_to_gdb)
3401*56bb7041Schristos {
3402*56bb7041Schristos if (debug_threads)
3403*56bb7041Schristos {
3404*56bb7041Schristos if (bp_explains_trap)
3405*56bb7041Schristos debug_printf ("Hit a gdbserver breakpoint.\n");
3406*56bb7041Schristos if (step_over_finished)
3407*56bb7041Schristos debug_printf ("Step-over finished.\n");
3408*56bb7041Schristos if (trace_event)
3409*56bb7041Schristos debug_printf ("Tracepoint event.\n");
3410*56bb7041Schristos if (lwp_in_step_range (event_child))
3411*56bb7041Schristos debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
3412*56bb7041Schristos paddress (event_child->stop_pc),
3413*56bb7041Schristos paddress (event_child->step_range_start),
3414*56bb7041Schristos paddress (event_child->step_range_end));
3415*56bb7041Schristos }
3416*56bb7041Schristos
3417*56bb7041Schristos /* We're not reporting this breakpoint to GDB, so apply the
3418*56bb7041Schristos decr_pc_after_break adjustment to the inferior's regcache
3419*56bb7041Schristos ourselves. */
3420*56bb7041Schristos
3421*56bb7041Schristos if (low_supports_breakpoints ())
3422*56bb7041Schristos {
3423*56bb7041Schristos struct regcache *regcache
3424*56bb7041Schristos = get_thread_regcache (current_thread, 1);
3425*56bb7041Schristos low_set_pc (regcache, event_child->stop_pc);
3426*56bb7041Schristos }
3427*56bb7041Schristos
3428*56bb7041Schristos if (step_over_finished)
3429*56bb7041Schristos {
3430*56bb7041Schristos /* If we have finished stepping over a breakpoint, we've
3431*56bb7041Schristos stopped and suspended all LWPs momentarily except the
3432*56bb7041Schristos stepping one. This is where we resume them all again.
3433*56bb7041Schristos We're going to keep waiting, so use proceed, which
3434*56bb7041Schristos handles stepping over the next breakpoint. */
3435*56bb7041Schristos unsuspend_all_lwps (event_child);
3436*56bb7041Schristos }
3437*56bb7041Schristos else
3438*56bb7041Schristos {
3439*56bb7041Schristos /* Remove the single-step breakpoints if any. Note that
3440*56bb7041Schristos there isn't single-step breakpoint if we finished stepping
3441*56bb7041Schristos over. */
3442*56bb7041Schristos if (supports_software_single_step ()
3443*56bb7041Schristos && has_single_step_breakpoints (current_thread))
3444*56bb7041Schristos {
3445*56bb7041Schristos stop_all_lwps (0, event_child);
3446*56bb7041Schristos delete_single_step_breakpoints (current_thread);
3447*56bb7041Schristos unstop_all_lwps (0, event_child);
3448*56bb7041Schristos }
3449*56bb7041Schristos }
3450*56bb7041Schristos
3451*56bb7041Schristos if (debug_threads)
3452*56bb7041Schristos debug_printf ("proceeding all threads.\n");
3453*56bb7041Schristos proceed_all_lwps ();
3454*56bb7041Schristos
3455*56bb7041Schristos if (debug_threads)
3456*56bb7041Schristos debug_exit ();
3457*56bb7041Schristos
3458*56bb7041Schristos return ignore_event (ourstatus);
3459*56bb7041Schristos }
3460*56bb7041Schristos
3461*56bb7041Schristos if (debug_threads)
3462*56bb7041Schristos {
3463*56bb7041Schristos if (event_child->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3464*56bb7041Schristos {
3465*56bb7041Schristos std::string str
3466*56bb7041Schristos = target_waitstatus_to_string (&event_child->waitstatus);
3467*56bb7041Schristos
3468*56bb7041Schristos debug_printf ("LWP %ld: extended event with waitstatus %s\n",
3469*56bb7041Schristos lwpid_of (get_lwp_thread (event_child)), str.c_str ());
3470*56bb7041Schristos }
3471*56bb7041Schristos if (current_thread->last_resume_kind == resume_step)
3472*56bb7041Schristos {
3473*56bb7041Schristos if (event_child->step_range_start == event_child->step_range_end)
3474*56bb7041Schristos debug_printf ("GDB wanted to single-step, reporting event.\n");
3475*56bb7041Schristos else if (!lwp_in_step_range (event_child))
3476*56bb7041Schristos debug_printf ("Out of step range, reporting event.\n");
3477*56bb7041Schristos }
3478*56bb7041Schristos if (event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
3479*56bb7041Schristos debug_printf ("Stopped by watchpoint.\n");
3480*56bb7041Schristos else if (gdb_breakpoint_here (event_child->stop_pc))
3481*56bb7041Schristos debug_printf ("Stopped by GDB breakpoint.\n");
3482*56bb7041Schristos if (debug_threads)
3483*56bb7041Schristos debug_printf ("Hit a non-gdbserver trap event.\n");
3484*56bb7041Schristos }
3485*56bb7041Schristos
3486*56bb7041Schristos /* Alright, we're going to report a stop. */
3487*56bb7041Schristos
3488*56bb7041Schristos /* Remove single-step breakpoints. */
3489*56bb7041Schristos if (supports_software_single_step ())
3490*56bb7041Schristos {
3491*56bb7041Schristos /* Remove single-step breakpoints or not. It it is true, stop all
3492*56bb7041Schristos lwps, so that other threads won't hit the breakpoint in the
3493*56bb7041Schristos staled memory. */
3494*56bb7041Schristos int remove_single_step_breakpoints_p = 0;
3495*56bb7041Schristos
3496*56bb7041Schristos if (non_stop)
3497*56bb7041Schristos {
3498*56bb7041Schristos remove_single_step_breakpoints_p
3499*56bb7041Schristos = has_single_step_breakpoints (current_thread);
3500*56bb7041Schristos }
3501*56bb7041Schristos else
3502*56bb7041Schristos {
3503*56bb7041Schristos /* In all-stop, a stop reply cancels all previous resume
3504*56bb7041Schristos requests. Delete all single-step breakpoints. */
3505*56bb7041Schristos
3506*56bb7041Schristos find_thread ([&] (thread_info *thread) {
3507*56bb7041Schristos if (has_single_step_breakpoints (thread))
3508*56bb7041Schristos {
3509*56bb7041Schristos remove_single_step_breakpoints_p = 1;
3510*56bb7041Schristos return true;
3511*56bb7041Schristos }
3512*56bb7041Schristos
3513*56bb7041Schristos return false;
3514*56bb7041Schristos });
3515*56bb7041Schristos }
3516*56bb7041Schristos
3517*56bb7041Schristos if (remove_single_step_breakpoints_p)
3518*56bb7041Schristos {
3519*56bb7041Schristos /* If we remove single-step breakpoints from memory, stop all lwps,
3520*56bb7041Schristos so that other threads won't hit the breakpoint in the staled
3521*56bb7041Schristos memory. */
3522*56bb7041Schristos stop_all_lwps (0, event_child);
3523*56bb7041Schristos
3524*56bb7041Schristos if (non_stop)
3525*56bb7041Schristos {
3526*56bb7041Schristos gdb_assert (has_single_step_breakpoints (current_thread));
3527*56bb7041Schristos delete_single_step_breakpoints (current_thread);
3528*56bb7041Schristos }
3529*56bb7041Schristos else
3530*56bb7041Schristos {
3531*56bb7041Schristos for_each_thread ([] (thread_info *thread){
3532*56bb7041Schristos if (has_single_step_breakpoints (thread))
3533*56bb7041Schristos delete_single_step_breakpoints (thread);
3534*56bb7041Schristos });
3535*56bb7041Schristos }
3536*56bb7041Schristos
3537*56bb7041Schristos unstop_all_lwps (0, event_child);
3538*56bb7041Schristos }
3539*56bb7041Schristos }
3540*56bb7041Schristos
3541*56bb7041Schristos if (!stabilizing_threads)
3542*56bb7041Schristos {
3543*56bb7041Schristos /* In all-stop, stop all threads. */
3544*56bb7041Schristos if (!non_stop)
3545*56bb7041Schristos stop_all_lwps (0, NULL);
3546*56bb7041Schristos
3547*56bb7041Schristos if (step_over_finished)
3548*56bb7041Schristos {
3549*56bb7041Schristos if (!non_stop)
3550*56bb7041Schristos {
3551*56bb7041Schristos /* If we were doing a step-over, all other threads but
3552*56bb7041Schristos the stepping one had been paused in start_step_over,
3553*56bb7041Schristos with their suspend counts incremented. We don't want
3554*56bb7041Schristos to do a full unstop/unpause, because we're in
3555*56bb7041Schristos all-stop mode (so we want threads stopped), but we
3556*56bb7041Schristos still need to unsuspend the other threads, to
3557*56bb7041Schristos decrement their `suspended' count back. */
3558*56bb7041Schristos unsuspend_all_lwps (event_child);
3559*56bb7041Schristos }
3560*56bb7041Schristos else
3561*56bb7041Schristos {
3562*56bb7041Schristos /* If we just finished a step-over, then all threads had
3563*56bb7041Schristos been momentarily paused. In all-stop, that's fine,
3564*56bb7041Schristos we want threads stopped by now anyway. In non-stop,
3565*56bb7041Schristos we need to re-resume threads that GDB wanted to be
3566*56bb7041Schristos running. */
3567*56bb7041Schristos unstop_all_lwps (1, event_child);
3568*56bb7041Schristos }
3569*56bb7041Schristos }
3570*56bb7041Schristos
3571*56bb7041Schristos /* If we're not waiting for a specific LWP, choose an event LWP
3572*56bb7041Schristos from among those that have had events. Giving equal priority
3573*56bb7041Schristos to all LWPs that have had events helps prevent
3574*56bb7041Schristos starvation. */
3575*56bb7041Schristos if (ptid == minus_one_ptid)
3576*56bb7041Schristos {
3577*56bb7041Schristos event_child->status_pending_p = 1;
3578*56bb7041Schristos event_child->status_pending = w;
3579*56bb7041Schristos
3580*56bb7041Schristos select_event_lwp (&event_child);
3581*56bb7041Schristos
3582*56bb7041Schristos /* current_thread and event_child must stay in sync. */
3583*56bb7041Schristos current_thread = get_lwp_thread (event_child);
3584*56bb7041Schristos
3585*56bb7041Schristos event_child->status_pending_p = 0;
3586*56bb7041Schristos w = event_child->status_pending;
3587*56bb7041Schristos }
3588*56bb7041Schristos
3589*56bb7041Schristos
3590*56bb7041Schristos /* Stabilize threads (move out of jump pads). */
3591*56bb7041Schristos if (!non_stop)
3592*56bb7041Schristos target_stabilize_threads ();
3593*56bb7041Schristos }
3594*56bb7041Schristos else
3595*56bb7041Schristos {
3596*56bb7041Schristos /* If we just finished a step-over, then all threads had been
3597*56bb7041Schristos momentarily paused. In all-stop, that's fine, we want
3598*56bb7041Schristos threads stopped by now anyway. In non-stop, we need to
3599*56bb7041Schristos re-resume threads that GDB wanted to be running. */
3600*56bb7041Schristos if (step_over_finished)
3601*56bb7041Schristos unstop_all_lwps (1, event_child);
3602*56bb7041Schristos }
3603*56bb7041Schristos
3604*56bb7041Schristos if (event_child->waitstatus.kind != TARGET_WAITKIND_IGNORE)
3605*56bb7041Schristos {
3606*56bb7041Schristos /* If the reported event is an exit, fork, vfork or exec, let
3607*56bb7041Schristos GDB know. */
3608*56bb7041Schristos
3609*56bb7041Schristos /* Break the unreported fork relationship chain. */
3610*56bb7041Schristos if (event_child->waitstatus.kind == TARGET_WAITKIND_FORKED
3611*56bb7041Schristos || event_child->waitstatus.kind == TARGET_WAITKIND_VFORKED)
3612*56bb7041Schristos {
3613*56bb7041Schristos event_child->fork_relative->fork_relative = NULL;
3614*56bb7041Schristos event_child->fork_relative = NULL;
3615*56bb7041Schristos }
3616*56bb7041Schristos
3617*56bb7041Schristos *ourstatus = event_child->waitstatus;
3618*56bb7041Schristos /* Clear the event lwp's waitstatus since we handled it already. */
3619*56bb7041Schristos event_child->waitstatus.kind = TARGET_WAITKIND_IGNORE;
3620*56bb7041Schristos }
3621*56bb7041Schristos else
3622*56bb7041Schristos ourstatus->kind = TARGET_WAITKIND_STOPPED;
3623*56bb7041Schristos
3624*56bb7041Schristos /* Now that we've selected our final event LWP, un-adjust its PC if
3625*56bb7041Schristos it was a software breakpoint, and the client doesn't know we can
3626*56bb7041Schristos adjust the breakpoint ourselves. */
3627*56bb7041Schristos if (event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
3628*56bb7041Schristos && !cs.swbreak_feature)
3629*56bb7041Schristos {
3630*56bb7041Schristos int decr_pc = low_decr_pc_after_break ();
3631*56bb7041Schristos
3632*56bb7041Schristos if (decr_pc != 0)
3633*56bb7041Schristos {
3634*56bb7041Schristos struct regcache *regcache
3635*56bb7041Schristos = get_thread_regcache (current_thread, 1);
3636*56bb7041Schristos low_set_pc (regcache, event_child->stop_pc + decr_pc);
3637*56bb7041Schristos }
3638*56bb7041Schristos }
3639*56bb7041Schristos
3640*56bb7041Schristos if (WSTOPSIG (w) == SYSCALL_SIGTRAP)
3641*56bb7041Schristos {
3642*56bb7041Schristos get_syscall_trapinfo (event_child,
3643*56bb7041Schristos &ourstatus->value.syscall_number);
3644*56bb7041Schristos ourstatus->kind = event_child->syscall_state;
3645*56bb7041Schristos }
3646*56bb7041Schristos else if (current_thread->last_resume_kind == resume_stop
3647*56bb7041Schristos && WSTOPSIG (w) == SIGSTOP)
3648*56bb7041Schristos {
3649*56bb7041Schristos /* A thread that has been requested to stop by GDB with vCont;t,
3650*56bb7041Schristos and it stopped cleanly, so report as SIG0. The use of
3651*56bb7041Schristos SIGSTOP is an implementation detail. */
3652*56bb7041Schristos ourstatus->value.sig = GDB_SIGNAL_0;
3653*56bb7041Schristos }
3654*56bb7041Schristos else if (current_thread->last_resume_kind == resume_stop
3655*56bb7041Schristos && WSTOPSIG (w) != SIGSTOP)
3656*56bb7041Schristos {
3657*56bb7041Schristos /* A thread that has been requested to stop by GDB with vCont;t,
3658*56bb7041Schristos but, it stopped for other reasons. */
3659*56bb7041Schristos ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (w));
3660*56bb7041Schristos }
3661*56bb7041Schristos else if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
3662*56bb7041Schristos {
3663*56bb7041Schristos ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (w));
3664*56bb7041Schristos }
3665*56bb7041Schristos
3666*56bb7041Schristos gdb_assert (step_over_bkpt == null_ptid);
3667*56bb7041Schristos
3668*56bb7041Schristos if (debug_threads)
3669*56bb7041Schristos {
3670*56bb7041Schristos debug_printf ("wait_1 ret = %s, %d, %d\n",
3671*56bb7041Schristos target_pid_to_str (ptid_of (current_thread)),
3672*56bb7041Schristos ourstatus->kind, ourstatus->value.sig);
3673*56bb7041Schristos debug_exit ();
3674*56bb7041Schristos }
3675*56bb7041Schristos
3676*56bb7041Schristos if (ourstatus->kind == TARGET_WAITKIND_EXITED)
3677*56bb7041Schristos return filter_exit_event (event_child, ourstatus);
3678*56bb7041Schristos
3679*56bb7041Schristos return ptid_of (current_thread);
3680*56bb7041Schristos }
3681*56bb7041Schristos
3682*56bb7041Schristos /* Get rid of any pending event in the pipe. */
3683*56bb7041Schristos static void
async_file_flush(void)3684*56bb7041Schristos async_file_flush (void)
3685*56bb7041Schristos {
3686*56bb7041Schristos int ret;
3687*56bb7041Schristos char buf;
3688*56bb7041Schristos
3689*56bb7041Schristos do
3690*56bb7041Schristos ret = read (linux_event_pipe[0], &buf, 1);
3691*56bb7041Schristos while (ret >= 0 || (ret == -1 && errno == EINTR));
3692*56bb7041Schristos }
3693*56bb7041Schristos
3694*56bb7041Schristos /* Put something in the pipe, so the event loop wakes up. */
3695*56bb7041Schristos static void
async_file_mark(void)3696*56bb7041Schristos async_file_mark (void)
3697*56bb7041Schristos {
3698*56bb7041Schristos int ret;
3699*56bb7041Schristos
3700*56bb7041Schristos async_file_flush ();
3701*56bb7041Schristos
3702*56bb7041Schristos do
3703*56bb7041Schristos ret = write (linux_event_pipe[1], "+", 1);
3704*56bb7041Schristos while (ret == 0 || (ret == -1 && errno == EINTR));
3705*56bb7041Schristos
3706*56bb7041Schristos /* Ignore EAGAIN. If the pipe is full, the event loop will already
3707*56bb7041Schristos be awakened anyway. */
3708*56bb7041Schristos }
3709*56bb7041Schristos
3710*56bb7041Schristos ptid_t
wait(ptid_t ptid,target_waitstatus * ourstatus,int target_options)3711*56bb7041Schristos linux_process_target::wait (ptid_t ptid,
3712*56bb7041Schristos target_waitstatus *ourstatus,
3713*56bb7041Schristos int target_options)
3714*56bb7041Schristos {
3715*56bb7041Schristos ptid_t event_ptid;
3716*56bb7041Schristos
3717*56bb7041Schristos /* Flush the async file first. */
3718*56bb7041Schristos if (target_is_async_p ())
3719*56bb7041Schristos async_file_flush ();
3720*56bb7041Schristos
3721*56bb7041Schristos do
3722*56bb7041Schristos {
3723*56bb7041Schristos event_ptid = wait_1 (ptid, ourstatus, target_options);
3724*56bb7041Schristos }
3725*56bb7041Schristos while ((target_options & TARGET_WNOHANG) == 0
3726*56bb7041Schristos && event_ptid == null_ptid
3727*56bb7041Schristos && ourstatus->kind == TARGET_WAITKIND_IGNORE);
3728*56bb7041Schristos
3729*56bb7041Schristos /* If at least one stop was reported, there may be more. A single
3730*56bb7041Schristos SIGCHLD can signal more than one child stop. */
3731*56bb7041Schristos if (target_is_async_p ()
3732*56bb7041Schristos && (target_options & TARGET_WNOHANG) != 0
3733*56bb7041Schristos && event_ptid != null_ptid)
3734*56bb7041Schristos async_file_mark ();
3735*56bb7041Schristos
3736*56bb7041Schristos return event_ptid;
3737*56bb7041Schristos }
3738*56bb7041Schristos
3739*56bb7041Schristos /* Send a signal to an LWP. */
3740*56bb7041Schristos
3741*56bb7041Schristos static int
kill_lwp(unsigned long lwpid,int signo)3742*56bb7041Schristos kill_lwp (unsigned long lwpid, int signo)
3743*56bb7041Schristos {
3744*56bb7041Schristos int ret;
3745*56bb7041Schristos
3746*56bb7041Schristos errno = 0;
3747*56bb7041Schristos ret = syscall (__NR_tkill, lwpid, signo);
3748*56bb7041Schristos if (errno == ENOSYS)
3749*56bb7041Schristos {
3750*56bb7041Schristos /* If tkill fails, then we are not using nptl threads, a
3751*56bb7041Schristos configuration we no longer support. */
3752*56bb7041Schristos perror_with_name (("tkill"));
3753*56bb7041Schristos }
3754*56bb7041Schristos return ret;
3755*56bb7041Schristos }
3756*56bb7041Schristos
3757*56bb7041Schristos void
linux_stop_lwp(struct lwp_info * lwp)3758*56bb7041Schristos linux_stop_lwp (struct lwp_info *lwp)
3759*56bb7041Schristos {
3760*56bb7041Schristos send_sigstop (lwp);
3761*56bb7041Schristos }
3762*56bb7041Schristos
3763*56bb7041Schristos static void
send_sigstop(struct lwp_info * lwp)3764*56bb7041Schristos send_sigstop (struct lwp_info *lwp)
3765*56bb7041Schristos {
3766*56bb7041Schristos int pid;
3767*56bb7041Schristos
3768*56bb7041Schristos pid = lwpid_of (get_lwp_thread (lwp));
3769*56bb7041Schristos
3770*56bb7041Schristos /* If we already have a pending stop signal for this process, don't
3771*56bb7041Schristos send another. */
3772*56bb7041Schristos if (lwp->stop_expected)
3773*56bb7041Schristos {
3774*56bb7041Schristos if (debug_threads)
3775*56bb7041Schristos debug_printf ("Have pending sigstop for lwp %d\n", pid);
3776*56bb7041Schristos
3777*56bb7041Schristos return;
3778*56bb7041Schristos }
3779*56bb7041Schristos
3780*56bb7041Schristos if (debug_threads)
3781*56bb7041Schristos debug_printf ("Sending sigstop to lwp %d\n", pid);
3782*56bb7041Schristos
3783*56bb7041Schristos lwp->stop_expected = 1;
3784*56bb7041Schristos kill_lwp (pid, SIGSTOP);
3785*56bb7041Schristos }
3786*56bb7041Schristos
3787*56bb7041Schristos static void
send_sigstop(thread_info * thread,lwp_info * except)3788*56bb7041Schristos send_sigstop (thread_info *thread, lwp_info *except)
3789*56bb7041Schristos {
3790*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
3791*56bb7041Schristos
3792*56bb7041Schristos /* Ignore EXCEPT. */
3793*56bb7041Schristos if (lwp == except)
3794*56bb7041Schristos return;
3795*56bb7041Schristos
3796*56bb7041Schristos if (lwp->stopped)
3797*56bb7041Schristos return;
3798*56bb7041Schristos
3799*56bb7041Schristos send_sigstop (lwp);
3800*56bb7041Schristos }
3801*56bb7041Schristos
3802*56bb7041Schristos /* Increment the suspend count of an LWP, and stop it, if not stopped
3803*56bb7041Schristos yet. */
3804*56bb7041Schristos static void
suspend_and_send_sigstop(thread_info * thread,lwp_info * except)3805*56bb7041Schristos suspend_and_send_sigstop (thread_info *thread, lwp_info *except)
3806*56bb7041Schristos {
3807*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
3808*56bb7041Schristos
3809*56bb7041Schristos /* Ignore EXCEPT. */
3810*56bb7041Schristos if (lwp == except)
3811*56bb7041Schristos return;
3812*56bb7041Schristos
3813*56bb7041Schristos lwp_suspended_inc (lwp);
3814*56bb7041Schristos
3815*56bb7041Schristos send_sigstop (thread, except);
3816*56bb7041Schristos }
3817*56bb7041Schristos
3818*56bb7041Schristos static void
mark_lwp_dead(struct lwp_info * lwp,int wstat)3819*56bb7041Schristos mark_lwp_dead (struct lwp_info *lwp, int wstat)
3820*56bb7041Schristos {
3821*56bb7041Schristos /* Store the exit status for later. */
3822*56bb7041Schristos lwp->status_pending_p = 1;
3823*56bb7041Schristos lwp->status_pending = wstat;
3824*56bb7041Schristos
3825*56bb7041Schristos /* Store in waitstatus as well, as there's nothing else to process
3826*56bb7041Schristos for this event. */
3827*56bb7041Schristos if (WIFEXITED (wstat))
3828*56bb7041Schristos {
3829*56bb7041Schristos lwp->waitstatus.kind = TARGET_WAITKIND_EXITED;
3830*56bb7041Schristos lwp->waitstatus.value.integer = WEXITSTATUS (wstat);
3831*56bb7041Schristos }
3832*56bb7041Schristos else if (WIFSIGNALED (wstat))
3833*56bb7041Schristos {
3834*56bb7041Schristos lwp->waitstatus.kind = TARGET_WAITKIND_SIGNALLED;
3835*56bb7041Schristos lwp->waitstatus.value.sig = gdb_signal_from_host (WTERMSIG (wstat));
3836*56bb7041Schristos }
3837*56bb7041Schristos
3838*56bb7041Schristos /* Prevent trying to stop it. */
3839*56bb7041Schristos lwp->stopped = 1;
3840*56bb7041Schristos
3841*56bb7041Schristos /* No further stops are expected from a dead lwp. */
3842*56bb7041Schristos lwp->stop_expected = 0;
3843*56bb7041Schristos }
3844*56bb7041Schristos
3845*56bb7041Schristos /* Return true if LWP has exited already, and has a pending exit event
3846*56bb7041Schristos to report to GDB. */
3847*56bb7041Schristos
3848*56bb7041Schristos static int
lwp_is_marked_dead(struct lwp_info * lwp)3849*56bb7041Schristos lwp_is_marked_dead (struct lwp_info *lwp)
3850*56bb7041Schristos {
3851*56bb7041Schristos return (lwp->status_pending_p
3852*56bb7041Schristos && (WIFEXITED (lwp->status_pending)
3853*56bb7041Schristos || WIFSIGNALED (lwp->status_pending)));
3854*56bb7041Schristos }
3855*56bb7041Schristos
3856*56bb7041Schristos void
wait_for_sigstop()3857*56bb7041Schristos linux_process_target::wait_for_sigstop ()
3858*56bb7041Schristos {
3859*56bb7041Schristos struct thread_info *saved_thread;
3860*56bb7041Schristos ptid_t saved_tid;
3861*56bb7041Schristos int wstat;
3862*56bb7041Schristos int ret;
3863*56bb7041Schristos
3864*56bb7041Schristos saved_thread = current_thread;
3865*56bb7041Schristos if (saved_thread != NULL)
3866*56bb7041Schristos saved_tid = saved_thread->id;
3867*56bb7041Schristos else
3868*56bb7041Schristos saved_tid = null_ptid; /* avoid bogus unused warning */
3869*56bb7041Schristos
3870*56bb7041Schristos if (debug_threads)
3871*56bb7041Schristos debug_printf ("wait_for_sigstop: pulling events\n");
3872*56bb7041Schristos
3873*56bb7041Schristos /* Passing NULL_PTID as filter indicates we want all events to be
3874*56bb7041Schristos left pending. Eventually this returns when there are no
3875*56bb7041Schristos unwaited-for children left. */
3876*56bb7041Schristos ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat, __WALL);
3877*56bb7041Schristos gdb_assert (ret == -1);
3878*56bb7041Schristos
3879*56bb7041Schristos if (saved_thread == NULL || mythread_alive (saved_tid))
3880*56bb7041Schristos current_thread = saved_thread;
3881*56bb7041Schristos else
3882*56bb7041Schristos {
3883*56bb7041Schristos if (debug_threads)
3884*56bb7041Schristos debug_printf ("Previously current thread died.\n");
3885*56bb7041Schristos
3886*56bb7041Schristos /* We can't change the current inferior behind GDB's back,
3887*56bb7041Schristos otherwise, a subsequent command may apply to the wrong
3888*56bb7041Schristos process. */
3889*56bb7041Schristos current_thread = NULL;
3890*56bb7041Schristos }
3891*56bb7041Schristos }
3892*56bb7041Schristos
3893*56bb7041Schristos bool
stuck_in_jump_pad(thread_info * thread)3894*56bb7041Schristos linux_process_target::stuck_in_jump_pad (thread_info *thread)
3895*56bb7041Schristos {
3896*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
3897*56bb7041Schristos
3898*56bb7041Schristos if (lwp->suspended != 0)
3899*56bb7041Schristos {
3900*56bb7041Schristos internal_error (__FILE__, __LINE__,
3901*56bb7041Schristos "LWP %ld is suspended, suspended=%d\n",
3902*56bb7041Schristos lwpid_of (thread), lwp->suspended);
3903*56bb7041Schristos }
3904*56bb7041Schristos gdb_assert (lwp->stopped);
3905*56bb7041Schristos
3906*56bb7041Schristos /* Allow debugging the jump pad, gdb_collect, etc.. */
3907*56bb7041Schristos return (supports_fast_tracepoints ()
3908*56bb7041Schristos && agent_loaded_p ()
3909*56bb7041Schristos && (gdb_breakpoint_here (lwp->stop_pc)
3910*56bb7041Schristos || lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
3911*56bb7041Schristos || thread->last_resume_kind == resume_step)
3912*56bb7041Schristos && (linux_fast_tracepoint_collecting (lwp, NULL)
3913*56bb7041Schristos != fast_tpoint_collect_result::not_collecting));
3914*56bb7041Schristos }
3915*56bb7041Schristos
3916*56bb7041Schristos void
move_out_of_jump_pad(thread_info * thread)3917*56bb7041Schristos linux_process_target::move_out_of_jump_pad (thread_info *thread)
3918*56bb7041Schristos {
3919*56bb7041Schristos struct thread_info *saved_thread;
3920*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
3921*56bb7041Schristos int *wstat;
3922*56bb7041Schristos
3923*56bb7041Schristos if (lwp->suspended != 0)
3924*56bb7041Schristos {
3925*56bb7041Schristos internal_error (__FILE__, __LINE__,
3926*56bb7041Schristos "LWP %ld is suspended, suspended=%d\n",
3927*56bb7041Schristos lwpid_of (thread), lwp->suspended);
3928*56bb7041Schristos }
3929*56bb7041Schristos gdb_assert (lwp->stopped);
3930*56bb7041Schristos
3931*56bb7041Schristos /* For gdb_breakpoint_here. */
3932*56bb7041Schristos saved_thread = current_thread;
3933*56bb7041Schristos current_thread = thread;
3934*56bb7041Schristos
3935*56bb7041Schristos wstat = lwp->status_pending_p ? &lwp->status_pending : NULL;
3936*56bb7041Schristos
3937*56bb7041Schristos /* Allow debugging the jump pad, gdb_collect, etc. */
3938*56bb7041Schristos if (!gdb_breakpoint_here (lwp->stop_pc)
3939*56bb7041Schristos && lwp->stop_reason != TARGET_STOPPED_BY_WATCHPOINT
3940*56bb7041Schristos && thread->last_resume_kind != resume_step
3941*56bb7041Schristos && maybe_move_out_of_jump_pad (lwp, wstat))
3942*56bb7041Schristos {
3943*56bb7041Schristos if (debug_threads)
3944*56bb7041Schristos debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
3945*56bb7041Schristos lwpid_of (thread));
3946*56bb7041Schristos
3947*56bb7041Schristos if (wstat)
3948*56bb7041Schristos {
3949*56bb7041Schristos lwp->status_pending_p = 0;
3950*56bb7041Schristos enqueue_one_deferred_signal (lwp, wstat);
3951*56bb7041Schristos
3952*56bb7041Schristos if (debug_threads)
3953*56bb7041Schristos debug_printf ("Signal %d for LWP %ld deferred "
3954*56bb7041Schristos "(in jump pad)\n",
3955*56bb7041Schristos WSTOPSIG (*wstat), lwpid_of (thread));
3956*56bb7041Schristos }
3957*56bb7041Schristos
3958*56bb7041Schristos resume_one_lwp (lwp, 0, 0, NULL);
3959*56bb7041Schristos }
3960*56bb7041Schristos else
3961*56bb7041Schristos lwp_suspended_inc (lwp);
3962*56bb7041Schristos
3963*56bb7041Schristos current_thread = saved_thread;
3964*56bb7041Schristos }
3965*56bb7041Schristos
3966*56bb7041Schristos static bool
lwp_running(thread_info * thread)3967*56bb7041Schristos lwp_running (thread_info *thread)
3968*56bb7041Schristos {
3969*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
3970*56bb7041Schristos
3971*56bb7041Schristos if (lwp_is_marked_dead (lwp))
3972*56bb7041Schristos return false;
3973*56bb7041Schristos
3974*56bb7041Schristos return !lwp->stopped;
3975*56bb7041Schristos }
3976*56bb7041Schristos
3977*56bb7041Schristos void
stop_all_lwps(int suspend,lwp_info * except)3978*56bb7041Schristos linux_process_target::stop_all_lwps (int suspend, lwp_info *except)
3979*56bb7041Schristos {
3980*56bb7041Schristos /* Should not be called recursively. */
3981*56bb7041Schristos gdb_assert (stopping_threads == NOT_STOPPING_THREADS);
3982*56bb7041Schristos
3983*56bb7041Schristos if (debug_threads)
3984*56bb7041Schristos {
3985*56bb7041Schristos debug_enter ();
3986*56bb7041Schristos debug_printf ("stop_all_lwps (%s, except=%s)\n",
3987*56bb7041Schristos suspend ? "stop-and-suspend" : "stop",
3988*56bb7041Schristos except != NULL
3989*56bb7041Schristos ? target_pid_to_str (ptid_of (get_lwp_thread (except)))
3990*56bb7041Schristos : "none");
3991*56bb7041Schristos }
3992*56bb7041Schristos
3993*56bb7041Schristos stopping_threads = (suspend
3994*56bb7041Schristos ? STOPPING_AND_SUSPENDING_THREADS
3995*56bb7041Schristos : STOPPING_THREADS);
3996*56bb7041Schristos
3997*56bb7041Schristos if (suspend)
3998*56bb7041Schristos for_each_thread ([&] (thread_info *thread)
3999*56bb7041Schristos {
4000*56bb7041Schristos suspend_and_send_sigstop (thread, except);
4001*56bb7041Schristos });
4002*56bb7041Schristos else
4003*56bb7041Schristos for_each_thread ([&] (thread_info *thread)
4004*56bb7041Schristos {
4005*56bb7041Schristos send_sigstop (thread, except);
4006*56bb7041Schristos });
4007*56bb7041Schristos
4008*56bb7041Schristos wait_for_sigstop ();
4009*56bb7041Schristos stopping_threads = NOT_STOPPING_THREADS;
4010*56bb7041Schristos
4011*56bb7041Schristos if (debug_threads)
4012*56bb7041Schristos {
4013*56bb7041Schristos debug_printf ("stop_all_lwps done, setting stopping_threads "
4014*56bb7041Schristos "back to !stopping\n");
4015*56bb7041Schristos debug_exit ();
4016*56bb7041Schristos }
4017*56bb7041Schristos }
4018*56bb7041Schristos
4019*56bb7041Schristos /* Enqueue one signal in the chain of signals which need to be
4020*56bb7041Schristos delivered to this process on next resume. */
4021*56bb7041Schristos
4022*56bb7041Schristos static void
enqueue_pending_signal(struct lwp_info * lwp,int signal,siginfo_t * info)4023*56bb7041Schristos enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info)
4024*56bb7041Schristos {
4025*56bb7041Schristos lwp->pending_signals.emplace_back (signal);
4026*56bb7041Schristos if (info == nullptr)
4027*56bb7041Schristos memset (&lwp->pending_signals.back ().info, 0, sizeof (siginfo_t));
4028*56bb7041Schristos else
4029*56bb7041Schristos lwp->pending_signals.back ().info = *info;
4030*56bb7041Schristos }
4031*56bb7041Schristos
4032*56bb7041Schristos void
install_software_single_step_breakpoints(lwp_info * lwp)4033*56bb7041Schristos linux_process_target::install_software_single_step_breakpoints (lwp_info *lwp)
4034*56bb7041Schristos {
4035*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
4036*56bb7041Schristos struct regcache *regcache = get_thread_regcache (thread, 1);
4037*56bb7041Schristos
4038*56bb7041Schristos scoped_restore save_current_thread = make_scoped_restore (¤t_thread);
4039*56bb7041Schristos
4040*56bb7041Schristos current_thread = thread;
4041*56bb7041Schristos std::vector<CORE_ADDR> next_pcs = low_get_next_pcs (regcache);
4042*56bb7041Schristos
4043*56bb7041Schristos for (CORE_ADDR pc : next_pcs)
4044*56bb7041Schristos set_single_step_breakpoint (pc, current_ptid);
4045*56bb7041Schristos }
4046*56bb7041Schristos
4047*56bb7041Schristos int
single_step(lwp_info * lwp)4048*56bb7041Schristos linux_process_target::single_step (lwp_info* lwp)
4049*56bb7041Schristos {
4050*56bb7041Schristos int step = 0;
4051*56bb7041Schristos
4052*56bb7041Schristos if (supports_hardware_single_step ())
4053*56bb7041Schristos {
4054*56bb7041Schristos step = 1;
4055*56bb7041Schristos }
4056*56bb7041Schristos else if (supports_software_single_step ())
4057*56bb7041Schristos {
4058*56bb7041Schristos install_software_single_step_breakpoints (lwp);
4059*56bb7041Schristos step = 0;
4060*56bb7041Schristos }
4061*56bb7041Schristos else
4062*56bb7041Schristos {
4063*56bb7041Schristos if (debug_threads)
4064*56bb7041Schristos debug_printf ("stepping is not implemented on this target");
4065*56bb7041Schristos }
4066*56bb7041Schristos
4067*56bb7041Schristos return step;
4068*56bb7041Schristos }
4069*56bb7041Schristos
4070*56bb7041Schristos /* The signal can be delivered to the inferior if we are not trying to
4071*56bb7041Schristos finish a fast tracepoint collect. Since signal can be delivered in
4072*56bb7041Schristos the step-over, the program may go to signal handler and trap again
4073*56bb7041Schristos after return from the signal handler. We can live with the spurious
4074*56bb7041Schristos double traps. */
4075*56bb7041Schristos
4076*56bb7041Schristos static int
lwp_signal_can_be_delivered(struct lwp_info * lwp)4077*56bb7041Schristos lwp_signal_can_be_delivered (struct lwp_info *lwp)
4078*56bb7041Schristos {
4079*56bb7041Schristos return (lwp->collecting_fast_tracepoint
4080*56bb7041Schristos == fast_tpoint_collect_result::not_collecting);
4081*56bb7041Schristos }
4082*56bb7041Schristos
4083*56bb7041Schristos void
resume_one_lwp_throw(lwp_info * lwp,int step,int signal,siginfo_t * info)4084*56bb7041Schristos linux_process_target::resume_one_lwp_throw (lwp_info *lwp, int step,
4085*56bb7041Schristos int signal, siginfo_t *info)
4086*56bb7041Schristos {
4087*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
4088*56bb7041Schristos struct thread_info *saved_thread;
4089*56bb7041Schristos int ptrace_request;
4090*56bb7041Schristos struct process_info *proc = get_thread_process (thread);
4091*56bb7041Schristos
4092*56bb7041Schristos /* Note that target description may not be initialised
4093*56bb7041Schristos (proc->tdesc == NULL) at this point because the program hasn't
4094*56bb7041Schristos stopped at the first instruction yet. It means GDBserver skips
4095*56bb7041Schristos the extra traps from the wrapper program (see option --wrapper).
4096*56bb7041Schristos Code in this function that requires register access should be
4097*56bb7041Schristos guarded by proc->tdesc == NULL or something else. */
4098*56bb7041Schristos
4099*56bb7041Schristos if (lwp->stopped == 0)
4100*56bb7041Schristos return;
4101*56bb7041Schristos
4102*56bb7041Schristos gdb_assert (lwp->waitstatus.kind == TARGET_WAITKIND_IGNORE);
4103*56bb7041Schristos
4104*56bb7041Schristos fast_tpoint_collect_result fast_tp_collecting
4105*56bb7041Schristos = lwp->collecting_fast_tracepoint;
4106*56bb7041Schristos
4107*56bb7041Schristos gdb_assert (!stabilizing_threads
4108*56bb7041Schristos || (fast_tp_collecting
4109*56bb7041Schristos != fast_tpoint_collect_result::not_collecting));
4110*56bb7041Schristos
4111*56bb7041Schristos /* Cancel actions that rely on GDB not changing the PC (e.g., the
4112*56bb7041Schristos user used the "jump" command, or "set $pc = foo"). */
4113*56bb7041Schristos if (thread->while_stepping != NULL && lwp->stop_pc != get_pc (lwp))
4114*56bb7041Schristos {
4115*56bb7041Schristos /* Collecting 'while-stepping' actions doesn't make sense
4116*56bb7041Schristos anymore. */
4117*56bb7041Schristos release_while_stepping_state_list (thread);
4118*56bb7041Schristos }
4119*56bb7041Schristos
4120*56bb7041Schristos /* If we have pending signals or status, and a new signal, enqueue the
4121*56bb7041Schristos signal. Also enqueue the signal if it can't be delivered to the
4122*56bb7041Schristos inferior right now. */
4123*56bb7041Schristos if (signal != 0
4124*56bb7041Schristos && (lwp->status_pending_p
4125*56bb7041Schristos || !lwp->pending_signals.empty ()
4126*56bb7041Schristos || !lwp_signal_can_be_delivered (lwp)))
4127*56bb7041Schristos {
4128*56bb7041Schristos enqueue_pending_signal (lwp, signal, info);
4129*56bb7041Schristos
4130*56bb7041Schristos /* Postpone any pending signal. It was enqueued above. */
4131*56bb7041Schristos signal = 0;
4132*56bb7041Schristos }
4133*56bb7041Schristos
4134*56bb7041Schristos if (lwp->status_pending_p)
4135*56bb7041Schristos {
4136*56bb7041Schristos if (debug_threads)
4137*56bb7041Schristos debug_printf ("Not resuming lwp %ld (%s, stop %s);"
4138*56bb7041Schristos " has pending status\n",
4139*56bb7041Schristos lwpid_of (thread), step ? "step" : "continue",
4140*56bb7041Schristos lwp->stop_expected ? "expected" : "not expected");
4141*56bb7041Schristos return;
4142*56bb7041Schristos }
4143*56bb7041Schristos
4144*56bb7041Schristos saved_thread = current_thread;
4145*56bb7041Schristos current_thread = thread;
4146*56bb7041Schristos
4147*56bb7041Schristos /* This bit needs some thinking about. If we get a signal that
4148*56bb7041Schristos we must report while a single-step reinsert is still pending,
4149*56bb7041Schristos we often end up resuming the thread. It might be better to
4150*56bb7041Schristos (ew) allow a stack of pending events; then we could be sure that
4151*56bb7041Schristos the reinsert happened right away and not lose any signals.
4152*56bb7041Schristos
4153*56bb7041Schristos Making this stack would also shrink the window in which breakpoints are
4154*56bb7041Schristos uninserted (see comment in linux_wait_for_lwp) but not enough for
4155*56bb7041Schristos complete correctness, so it won't solve that problem. It may be
4156*56bb7041Schristos worthwhile just to solve this one, however. */
4157*56bb7041Schristos if (lwp->bp_reinsert != 0)
4158*56bb7041Schristos {
4159*56bb7041Schristos if (debug_threads)
4160*56bb7041Schristos debug_printf (" pending reinsert at 0x%s\n",
4161*56bb7041Schristos paddress (lwp->bp_reinsert));
4162*56bb7041Schristos
4163*56bb7041Schristos if (supports_hardware_single_step ())
4164*56bb7041Schristos {
4165*56bb7041Schristos if (fast_tp_collecting == fast_tpoint_collect_result::not_collecting)
4166*56bb7041Schristos {
4167*56bb7041Schristos if (step == 0)
4168*56bb7041Schristos warning ("BAD - reinserting but not stepping.");
4169*56bb7041Schristos if (lwp->suspended)
4170*56bb7041Schristos warning ("BAD - reinserting and suspended(%d).",
4171*56bb7041Schristos lwp->suspended);
4172*56bb7041Schristos }
4173*56bb7041Schristos }
4174*56bb7041Schristos
4175*56bb7041Schristos step = maybe_hw_step (thread);
4176*56bb7041Schristos }
4177*56bb7041Schristos
4178*56bb7041Schristos if (fast_tp_collecting == fast_tpoint_collect_result::before_insn)
4179*56bb7041Schristos {
4180*56bb7041Schristos if (debug_threads)
4181*56bb7041Schristos debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4182*56bb7041Schristos " (exit-jump-pad-bkpt)\n",
4183*56bb7041Schristos lwpid_of (thread));
4184*56bb7041Schristos }
4185*56bb7041Schristos else if (fast_tp_collecting == fast_tpoint_collect_result::at_insn)
4186*56bb7041Schristos {
4187*56bb7041Schristos if (debug_threads)
4188*56bb7041Schristos debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4189*56bb7041Schristos " single-stepping\n",
4190*56bb7041Schristos lwpid_of (thread));
4191*56bb7041Schristos
4192*56bb7041Schristos if (supports_hardware_single_step ())
4193*56bb7041Schristos step = 1;
4194*56bb7041Schristos else
4195*56bb7041Schristos {
4196*56bb7041Schristos internal_error (__FILE__, __LINE__,
4197*56bb7041Schristos "moving out of jump pad single-stepping"
4198*56bb7041Schristos " not implemented on this target");
4199*56bb7041Schristos }
4200*56bb7041Schristos }
4201*56bb7041Schristos
4202*56bb7041Schristos /* If we have while-stepping actions in this thread set it stepping.
4203*56bb7041Schristos If we have a signal to deliver, it may or may not be set to
4204*56bb7041Schristos SIG_IGN, we don't know. Assume so, and allow collecting
4205*56bb7041Schristos while-stepping into a signal handler. A possible smart thing to
4206*56bb7041Schristos do would be to set an internal breakpoint at the signal return
4207*56bb7041Schristos address, continue, and carry on catching this while-stepping
4208*56bb7041Schristos action only when that breakpoint is hit. A future
4209*56bb7041Schristos enhancement. */
4210*56bb7041Schristos if (thread->while_stepping != NULL)
4211*56bb7041Schristos {
4212*56bb7041Schristos if (debug_threads)
4213*56bb7041Schristos debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
4214*56bb7041Schristos lwpid_of (thread));
4215*56bb7041Schristos
4216*56bb7041Schristos step = single_step (lwp);
4217*56bb7041Schristos }
4218*56bb7041Schristos
4219*56bb7041Schristos if (proc->tdesc != NULL && low_supports_breakpoints ())
4220*56bb7041Schristos {
4221*56bb7041Schristos struct regcache *regcache = get_thread_regcache (current_thread, 1);
4222*56bb7041Schristos
4223*56bb7041Schristos lwp->stop_pc = low_get_pc (regcache);
4224*56bb7041Schristos
4225*56bb7041Schristos if (debug_threads)
4226*56bb7041Schristos {
4227*56bb7041Schristos debug_printf (" %s from pc 0x%lx\n", step ? "step" : "continue",
4228*56bb7041Schristos (long) lwp->stop_pc);
4229*56bb7041Schristos }
4230*56bb7041Schristos }
4231*56bb7041Schristos
4232*56bb7041Schristos /* If we have pending signals, consume one if it can be delivered to
4233*56bb7041Schristos the inferior. */
4234*56bb7041Schristos if (!lwp->pending_signals.empty () && lwp_signal_can_be_delivered (lwp))
4235*56bb7041Schristos {
4236*56bb7041Schristos const pending_signal &p_sig = lwp->pending_signals.front ();
4237*56bb7041Schristos
4238*56bb7041Schristos signal = p_sig.signal;
4239*56bb7041Schristos if (p_sig.info.si_signo != 0)
4240*56bb7041Schristos ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
4241*56bb7041Schristos &p_sig.info);
4242*56bb7041Schristos
4243*56bb7041Schristos lwp->pending_signals.pop_front ();
4244*56bb7041Schristos }
4245*56bb7041Schristos
4246*56bb7041Schristos if (debug_threads)
4247*56bb7041Schristos debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
4248*56bb7041Schristos lwpid_of (thread), step ? "step" : "continue", signal,
4249*56bb7041Schristos lwp->stop_expected ? "expected" : "not expected");
4250*56bb7041Schristos
4251*56bb7041Schristos low_prepare_to_resume (lwp);
4252*56bb7041Schristos
4253*56bb7041Schristos regcache_invalidate_thread (thread);
4254*56bb7041Schristos errno = 0;
4255*56bb7041Schristos lwp->stepping = step;
4256*56bb7041Schristos if (step)
4257*56bb7041Schristos ptrace_request = PTRACE_SINGLESTEP;
4258*56bb7041Schristos else if (gdb_catching_syscalls_p (lwp))
4259*56bb7041Schristos ptrace_request = PTRACE_SYSCALL;
4260*56bb7041Schristos else
4261*56bb7041Schristos ptrace_request = PTRACE_CONT;
4262*56bb7041Schristos ptrace (ptrace_request,
4263*56bb7041Schristos lwpid_of (thread),
4264*56bb7041Schristos (PTRACE_TYPE_ARG3) 0,
4265*56bb7041Schristos /* Coerce to a uintptr_t first to avoid potential gcc warning
4266*56bb7041Schristos of coercing an 8 byte integer to a 4 byte pointer. */
4267*56bb7041Schristos (PTRACE_TYPE_ARG4) (uintptr_t) signal);
4268*56bb7041Schristos
4269*56bb7041Schristos current_thread = saved_thread;
4270*56bb7041Schristos if (errno)
4271*56bb7041Schristos perror_with_name ("resuming thread");
4272*56bb7041Schristos
4273*56bb7041Schristos /* Successfully resumed. Clear state that no longer makes sense,
4274*56bb7041Schristos and mark the LWP as running. Must not do this before resuming
4275*56bb7041Schristos otherwise if that fails other code will be confused. E.g., we'd
4276*56bb7041Schristos later try to stop the LWP and hang forever waiting for a stop
4277*56bb7041Schristos status. Note that we must not throw after this is cleared,
4278*56bb7041Schristos otherwise handle_zombie_lwp_error would get confused. */
4279*56bb7041Schristos lwp->stopped = 0;
4280*56bb7041Schristos lwp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4281*56bb7041Schristos }
4282*56bb7041Schristos
4283*56bb7041Schristos void
low_prepare_to_resume(lwp_info * lwp)4284*56bb7041Schristos linux_process_target::low_prepare_to_resume (lwp_info *lwp)
4285*56bb7041Schristos {
4286*56bb7041Schristos /* Nop. */
4287*56bb7041Schristos }
4288*56bb7041Schristos
4289*56bb7041Schristos /* Called when we try to resume a stopped LWP and that errors out. If
4290*56bb7041Schristos the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4291*56bb7041Schristos or about to become), discard the error, clear any pending status
4292*56bb7041Schristos the LWP may have, and return true (we'll collect the exit status
4293*56bb7041Schristos soon enough). Otherwise, return false. */
4294*56bb7041Schristos
4295*56bb7041Schristos static int
check_ptrace_stopped_lwp_gone(struct lwp_info * lp)4296*56bb7041Schristos check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
4297*56bb7041Schristos {
4298*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lp);
4299*56bb7041Schristos
4300*56bb7041Schristos /* If we get an error after resuming the LWP successfully, we'd
4301*56bb7041Schristos confuse !T state for the LWP being gone. */
4302*56bb7041Schristos gdb_assert (lp->stopped);
4303*56bb7041Schristos
4304*56bb7041Schristos /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4305*56bb7041Schristos because even if ptrace failed with ESRCH, the tracee may be "not
4306*56bb7041Schristos yet fully dead", but already refusing ptrace requests. In that
4307*56bb7041Schristos case the tracee has 'R (Running)' state for a little bit
4308*56bb7041Schristos (observed in Linux 3.18). See also the note on ESRCH in the
4309*56bb7041Schristos ptrace(2) man page. Instead, check whether the LWP has any state
4310*56bb7041Schristos other than ptrace-stopped. */
4311*56bb7041Schristos
4312*56bb7041Schristos /* Don't assume anything if /proc/PID/status can't be read. */
4313*56bb7041Schristos if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread)) == 0)
4314*56bb7041Schristos {
4315*56bb7041Schristos lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4316*56bb7041Schristos lp->status_pending_p = 0;
4317*56bb7041Schristos return 1;
4318*56bb7041Schristos }
4319*56bb7041Schristos return 0;
4320*56bb7041Schristos }
4321*56bb7041Schristos
4322*56bb7041Schristos void
resume_one_lwp(lwp_info * lwp,int step,int signal,siginfo_t * info)4323*56bb7041Schristos linux_process_target::resume_one_lwp (lwp_info *lwp, int step, int signal,
4324*56bb7041Schristos siginfo_t *info)
4325*56bb7041Schristos {
4326*56bb7041Schristos try
4327*56bb7041Schristos {
4328*56bb7041Schristos resume_one_lwp_throw (lwp, step, signal, info);
4329*56bb7041Schristos }
4330*56bb7041Schristos catch (const gdb_exception_error &ex)
4331*56bb7041Schristos {
4332*56bb7041Schristos if (!check_ptrace_stopped_lwp_gone (lwp))
4333*56bb7041Schristos throw;
4334*56bb7041Schristos }
4335*56bb7041Schristos }
4336*56bb7041Schristos
4337*56bb7041Schristos /* This function is called once per thread via for_each_thread.
4338*56bb7041Schristos We look up which resume request applies to THREAD and mark it with a
4339*56bb7041Schristos pointer to the appropriate resume request.
4340*56bb7041Schristos
4341*56bb7041Schristos This algorithm is O(threads * resume elements), but resume elements
4342*56bb7041Schristos is small (and will remain small at least until GDB supports thread
4343*56bb7041Schristos suspension). */
4344*56bb7041Schristos
4345*56bb7041Schristos static void
linux_set_resume_request(thread_info * thread,thread_resume * resume,size_t n)4346*56bb7041Schristos linux_set_resume_request (thread_info *thread, thread_resume *resume, size_t n)
4347*56bb7041Schristos {
4348*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
4349*56bb7041Schristos
4350*56bb7041Schristos for (int ndx = 0; ndx < n; ndx++)
4351*56bb7041Schristos {
4352*56bb7041Schristos ptid_t ptid = resume[ndx].thread;
4353*56bb7041Schristos if (ptid == minus_one_ptid
4354*56bb7041Schristos || ptid == thread->id
4355*56bb7041Schristos /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4356*56bb7041Schristos of PID'. */
4357*56bb7041Schristos || (ptid.pid () == pid_of (thread)
4358*56bb7041Schristos && (ptid.is_pid ()
4359*56bb7041Schristos || ptid.lwp () == -1)))
4360*56bb7041Schristos {
4361*56bb7041Schristos if (resume[ndx].kind == resume_stop
4362*56bb7041Schristos && thread->last_resume_kind == resume_stop)
4363*56bb7041Schristos {
4364*56bb7041Schristos if (debug_threads)
4365*56bb7041Schristos debug_printf ("already %s LWP %ld at GDB's request\n",
4366*56bb7041Schristos (thread->last_status.kind
4367*56bb7041Schristos == TARGET_WAITKIND_STOPPED)
4368*56bb7041Schristos ? "stopped"
4369*56bb7041Schristos : "stopping",
4370*56bb7041Schristos lwpid_of (thread));
4371*56bb7041Schristos
4372*56bb7041Schristos continue;
4373*56bb7041Schristos }
4374*56bb7041Schristos
4375*56bb7041Schristos /* Ignore (wildcard) resume requests for already-resumed
4376*56bb7041Schristos threads. */
4377*56bb7041Schristos if (resume[ndx].kind != resume_stop
4378*56bb7041Schristos && thread->last_resume_kind != resume_stop)
4379*56bb7041Schristos {
4380*56bb7041Schristos if (debug_threads)
4381*56bb7041Schristos debug_printf ("already %s LWP %ld at GDB's request\n",
4382*56bb7041Schristos (thread->last_resume_kind
4383*56bb7041Schristos == resume_step)
4384*56bb7041Schristos ? "stepping"
4385*56bb7041Schristos : "continuing",
4386*56bb7041Schristos lwpid_of (thread));
4387*56bb7041Schristos continue;
4388*56bb7041Schristos }
4389*56bb7041Schristos
4390*56bb7041Schristos /* Don't let wildcard resumes resume fork children that GDB
4391*56bb7041Schristos does not yet know are new fork children. */
4392*56bb7041Schristos if (lwp->fork_relative != NULL)
4393*56bb7041Schristos {
4394*56bb7041Schristos struct lwp_info *rel = lwp->fork_relative;
4395*56bb7041Schristos
4396*56bb7041Schristos if (rel->status_pending_p
4397*56bb7041Schristos && (rel->waitstatus.kind == TARGET_WAITKIND_FORKED
4398*56bb7041Schristos || rel->waitstatus.kind == TARGET_WAITKIND_VFORKED))
4399*56bb7041Schristos {
4400*56bb7041Schristos if (debug_threads)
4401*56bb7041Schristos debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4402*56bb7041Schristos lwpid_of (thread));
4403*56bb7041Schristos continue;
4404*56bb7041Schristos }
4405*56bb7041Schristos }
4406*56bb7041Schristos
4407*56bb7041Schristos /* If the thread has a pending event that has already been
4408*56bb7041Schristos reported to GDBserver core, but GDB has not pulled the
4409*56bb7041Schristos event out of the vStopped queue yet, likewise, ignore the
4410*56bb7041Schristos (wildcard) resume request. */
4411*56bb7041Schristos if (in_queued_stop_replies (thread->id))
4412*56bb7041Schristos {
4413*56bb7041Schristos if (debug_threads)
4414*56bb7041Schristos debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4415*56bb7041Schristos lwpid_of (thread));
4416*56bb7041Schristos continue;
4417*56bb7041Schristos }
4418*56bb7041Schristos
4419*56bb7041Schristos lwp->resume = &resume[ndx];
4420*56bb7041Schristos thread->last_resume_kind = lwp->resume->kind;
4421*56bb7041Schristos
4422*56bb7041Schristos lwp->step_range_start = lwp->resume->step_range_start;
4423*56bb7041Schristos lwp->step_range_end = lwp->resume->step_range_end;
4424*56bb7041Schristos
4425*56bb7041Schristos /* If we had a deferred signal to report, dequeue one now.
4426*56bb7041Schristos This can happen if LWP gets more than one signal while
4427*56bb7041Schristos trying to get out of a jump pad. */
4428*56bb7041Schristos if (lwp->stopped
4429*56bb7041Schristos && !lwp->status_pending_p
4430*56bb7041Schristos && dequeue_one_deferred_signal (lwp, &lwp->status_pending))
4431*56bb7041Schristos {
4432*56bb7041Schristos lwp->status_pending_p = 1;
4433*56bb7041Schristos
4434*56bb7041Schristos if (debug_threads)
4435*56bb7041Schristos debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
4436*56bb7041Schristos "leaving status pending.\n",
4437*56bb7041Schristos WSTOPSIG (lwp->status_pending),
4438*56bb7041Schristos lwpid_of (thread));
4439*56bb7041Schristos }
4440*56bb7041Schristos
4441*56bb7041Schristos return;
4442*56bb7041Schristos }
4443*56bb7041Schristos }
4444*56bb7041Schristos
4445*56bb7041Schristos /* No resume action for this thread. */
4446*56bb7041Schristos lwp->resume = NULL;
4447*56bb7041Schristos }
4448*56bb7041Schristos
4449*56bb7041Schristos bool
resume_status_pending(thread_info * thread)4450*56bb7041Schristos linux_process_target::resume_status_pending (thread_info *thread)
4451*56bb7041Schristos {
4452*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
4453*56bb7041Schristos
4454*56bb7041Schristos /* LWPs which will not be resumed are not interesting, because
4455*56bb7041Schristos we might not wait for them next time through linux_wait. */
4456*56bb7041Schristos if (lwp->resume == NULL)
4457*56bb7041Schristos return false;
4458*56bb7041Schristos
4459*56bb7041Schristos return thread_still_has_status_pending (thread);
4460*56bb7041Schristos }
4461*56bb7041Schristos
4462*56bb7041Schristos bool
thread_needs_step_over(thread_info * thread)4463*56bb7041Schristos linux_process_target::thread_needs_step_over (thread_info *thread)
4464*56bb7041Schristos {
4465*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
4466*56bb7041Schristos struct thread_info *saved_thread;
4467*56bb7041Schristos CORE_ADDR pc;
4468*56bb7041Schristos struct process_info *proc = get_thread_process (thread);
4469*56bb7041Schristos
4470*56bb7041Schristos /* GDBserver is skipping the extra traps from the wrapper program,
4471*56bb7041Schristos don't have to do step over. */
4472*56bb7041Schristos if (proc->tdesc == NULL)
4473*56bb7041Schristos return false;
4474*56bb7041Schristos
4475*56bb7041Schristos /* LWPs which will not be resumed are not interesting, because we
4476*56bb7041Schristos might not wait for them next time through linux_wait. */
4477*56bb7041Schristos
4478*56bb7041Schristos if (!lwp->stopped)
4479*56bb7041Schristos {
4480*56bb7041Schristos if (debug_threads)
4481*56bb7041Schristos debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
4482*56bb7041Schristos lwpid_of (thread));
4483*56bb7041Schristos return false;
4484*56bb7041Schristos }
4485*56bb7041Schristos
4486*56bb7041Schristos if (thread->last_resume_kind == resume_stop)
4487*56bb7041Schristos {
4488*56bb7041Schristos if (debug_threads)
4489*56bb7041Schristos debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
4490*56bb7041Schristos " stopped\n",
4491*56bb7041Schristos lwpid_of (thread));
4492*56bb7041Schristos return false;
4493*56bb7041Schristos }
4494*56bb7041Schristos
4495*56bb7041Schristos gdb_assert (lwp->suspended >= 0);
4496*56bb7041Schristos
4497*56bb7041Schristos if (lwp->suspended)
4498*56bb7041Schristos {
4499*56bb7041Schristos if (debug_threads)
4500*56bb7041Schristos debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
4501*56bb7041Schristos lwpid_of (thread));
4502*56bb7041Schristos return false;
4503*56bb7041Schristos }
4504*56bb7041Schristos
4505*56bb7041Schristos if (lwp->status_pending_p)
4506*56bb7041Schristos {
4507*56bb7041Schristos if (debug_threads)
4508*56bb7041Schristos debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4509*56bb7041Schristos " status.\n",
4510*56bb7041Schristos lwpid_of (thread));
4511*56bb7041Schristos return false;
4512*56bb7041Schristos }
4513*56bb7041Schristos
4514*56bb7041Schristos /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4515*56bb7041Schristos or we have. */
4516*56bb7041Schristos pc = get_pc (lwp);
4517*56bb7041Schristos
4518*56bb7041Schristos /* If the PC has changed since we stopped, then don't do anything,
4519*56bb7041Schristos and let the breakpoint/tracepoint be hit. This happens if, for
4520*56bb7041Schristos instance, GDB handled the decr_pc_after_break subtraction itself,
4521*56bb7041Schristos GDB is OOL stepping this thread, or the user has issued a "jump"
4522*56bb7041Schristos command, or poked thread's registers herself. */
4523*56bb7041Schristos if (pc != lwp->stop_pc)
4524*56bb7041Schristos {
4525*56bb7041Schristos if (debug_threads)
4526*56bb7041Schristos debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4527*56bb7041Schristos "Old stop_pc was 0x%s, PC is now 0x%s\n",
4528*56bb7041Schristos lwpid_of (thread),
4529*56bb7041Schristos paddress (lwp->stop_pc), paddress (pc));
4530*56bb7041Schristos return false;
4531*56bb7041Schristos }
4532*56bb7041Schristos
4533*56bb7041Schristos /* On software single step target, resume the inferior with signal
4534*56bb7041Schristos rather than stepping over. */
4535*56bb7041Schristos if (supports_software_single_step ()
4536*56bb7041Schristos && !lwp->pending_signals.empty ()
4537*56bb7041Schristos && lwp_signal_can_be_delivered (lwp))
4538*56bb7041Schristos {
4539*56bb7041Schristos if (debug_threads)
4540*56bb7041Schristos debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4541*56bb7041Schristos " signals.\n",
4542*56bb7041Schristos lwpid_of (thread));
4543*56bb7041Schristos
4544*56bb7041Schristos return false;
4545*56bb7041Schristos }
4546*56bb7041Schristos
4547*56bb7041Schristos saved_thread = current_thread;
4548*56bb7041Schristos current_thread = thread;
4549*56bb7041Schristos
4550*56bb7041Schristos /* We can only step over breakpoints we know about. */
4551*56bb7041Schristos if (breakpoint_here (pc) || fast_tracepoint_jump_here (pc))
4552*56bb7041Schristos {
4553*56bb7041Schristos /* Don't step over a breakpoint that GDB expects to hit
4554*56bb7041Schristos though. If the condition is being evaluated on the target's side
4555*56bb7041Schristos and it evaluate to false, step over this breakpoint as well. */
4556*56bb7041Schristos if (gdb_breakpoint_here (pc)
4557*56bb7041Schristos && gdb_condition_true_at_breakpoint (pc)
4558*56bb7041Schristos && gdb_no_commands_at_breakpoint (pc))
4559*56bb7041Schristos {
4560*56bb7041Schristos if (debug_threads)
4561*56bb7041Schristos debug_printf ("Need step over [LWP %ld]? yes, but found"
4562*56bb7041Schristos " GDB breakpoint at 0x%s; skipping step over\n",
4563*56bb7041Schristos lwpid_of (thread), paddress (pc));
4564*56bb7041Schristos
4565*56bb7041Schristos current_thread = saved_thread;
4566*56bb7041Schristos return false;
4567*56bb7041Schristos }
4568*56bb7041Schristos else
4569*56bb7041Schristos {
4570*56bb7041Schristos if (debug_threads)
4571*56bb7041Schristos debug_printf ("Need step over [LWP %ld]? yes, "
4572*56bb7041Schristos "found breakpoint at 0x%s\n",
4573*56bb7041Schristos lwpid_of (thread), paddress (pc));
4574*56bb7041Schristos
4575*56bb7041Schristos /* We've found an lwp that needs stepping over --- return 1 so
4576*56bb7041Schristos that find_thread stops looking. */
4577*56bb7041Schristos current_thread = saved_thread;
4578*56bb7041Schristos
4579*56bb7041Schristos return true;
4580*56bb7041Schristos }
4581*56bb7041Schristos }
4582*56bb7041Schristos
4583*56bb7041Schristos current_thread = saved_thread;
4584*56bb7041Schristos
4585*56bb7041Schristos if (debug_threads)
4586*56bb7041Schristos debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
4587*56bb7041Schristos " at 0x%s\n",
4588*56bb7041Schristos lwpid_of (thread), paddress (pc));
4589*56bb7041Schristos
4590*56bb7041Schristos return false;
4591*56bb7041Schristos }
4592*56bb7041Schristos
4593*56bb7041Schristos void
start_step_over(lwp_info * lwp)4594*56bb7041Schristos linux_process_target::start_step_over (lwp_info *lwp)
4595*56bb7041Schristos {
4596*56bb7041Schristos struct thread_info *thread = get_lwp_thread (lwp);
4597*56bb7041Schristos struct thread_info *saved_thread;
4598*56bb7041Schristos CORE_ADDR pc;
4599*56bb7041Schristos int step;
4600*56bb7041Schristos
4601*56bb7041Schristos if (debug_threads)
4602*56bb7041Schristos debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
4603*56bb7041Schristos lwpid_of (thread));
4604*56bb7041Schristos
4605*56bb7041Schristos stop_all_lwps (1, lwp);
4606*56bb7041Schristos
4607*56bb7041Schristos if (lwp->suspended != 0)
4608*56bb7041Schristos {
4609*56bb7041Schristos internal_error (__FILE__, __LINE__,
4610*56bb7041Schristos "LWP %ld suspended=%d\n", lwpid_of (thread),
4611*56bb7041Schristos lwp->suspended);
4612*56bb7041Schristos }
4613*56bb7041Schristos
4614*56bb7041Schristos if (debug_threads)
4615*56bb7041Schristos debug_printf ("Done stopping all threads for step-over.\n");
4616*56bb7041Schristos
4617*56bb7041Schristos /* Note, we should always reach here with an already adjusted PC,
4618*56bb7041Schristos either by GDB (if we're resuming due to GDB's request), or by our
4619*56bb7041Schristos caller, if we just finished handling an internal breakpoint GDB
4620*56bb7041Schristos shouldn't care about. */
4621*56bb7041Schristos pc = get_pc (lwp);
4622*56bb7041Schristos
4623*56bb7041Schristos saved_thread = current_thread;
4624*56bb7041Schristos current_thread = thread;
4625*56bb7041Schristos
4626*56bb7041Schristos lwp->bp_reinsert = pc;
4627*56bb7041Schristos uninsert_breakpoints_at (pc);
4628*56bb7041Schristos uninsert_fast_tracepoint_jumps_at (pc);
4629*56bb7041Schristos
4630*56bb7041Schristos step = single_step (lwp);
4631*56bb7041Schristos
4632*56bb7041Schristos current_thread = saved_thread;
4633*56bb7041Schristos
4634*56bb7041Schristos resume_one_lwp (lwp, step, 0, NULL);
4635*56bb7041Schristos
4636*56bb7041Schristos /* Require next event from this LWP. */
4637*56bb7041Schristos step_over_bkpt = thread->id;
4638*56bb7041Schristos }
4639*56bb7041Schristos
4640*56bb7041Schristos bool
finish_step_over(lwp_info * lwp)4641*56bb7041Schristos linux_process_target::finish_step_over (lwp_info *lwp)
4642*56bb7041Schristos {
4643*56bb7041Schristos if (lwp->bp_reinsert != 0)
4644*56bb7041Schristos {
4645*56bb7041Schristos struct thread_info *saved_thread = current_thread;
4646*56bb7041Schristos
4647*56bb7041Schristos if (debug_threads)
4648*56bb7041Schristos debug_printf ("Finished step over.\n");
4649*56bb7041Schristos
4650*56bb7041Schristos current_thread = get_lwp_thread (lwp);
4651*56bb7041Schristos
4652*56bb7041Schristos /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4653*56bb7041Schristos may be no breakpoint to reinsert there by now. */
4654*56bb7041Schristos reinsert_breakpoints_at (lwp->bp_reinsert);
4655*56bb7041Schristos reinsert_fast_tracepoint_jumps_at (lwp->bp_reinsert);
4656*56bb7041Schristos
4657*56bb7041Schristos lwp->bp_reinsert = 0;
4658*56bb7041Schristos
4659*56bb7041Schristos /* Delete any single-step breakpoints. No longer needed. We
4660*56bb7041Schristos don't have to worry about other threads hitting this trap,
4661*56bb7041Schristos and later not being able to explain it, because we were
4662*56bb7041Schristos stepping over a breakpoint, and we hold all threads but
4663*56bb7041Schristos LWP stopped while doing that. */
4664*56bb7041Schristos if (!supports_hardware_single_step ())
4665*56bb7041Schristos {
4666*56bb7041Schristos gdb_assert (has_single_step_breakpoints (current_thread));
4667*56bb7041Schristos delete_single_step_breakpoints (current_thread);
4668*56bb7041Schristos }
4669*56bb7041Schristos
4670*56bb7041Schristos step_over_bkpt = null_ptid;
4671*56bb7041Schristos current_thread = saved_thread;
4672*56bb7041Schristos return true;
4673*56bb7041Schristos }
4674*56bb7041Schristos else
4675*56bb7041Schristos return false;
4676*56bb7041Schristos }
4677*56bb7041Schristos
4678*56bb7041Schristos void
complete_ongoing_step_over()4679*56bb7041Schristos linux_process_target::complete_ongoing_step_over ()
4680*56bb7041Schristos {
4681*56bb7041Schristos if (step_over_bkpt != null_ptid)
4682*56bb7041Schristos {
4683*56bb7041Schristos struct lwp_info *lwp;
4684*56bb7041Schristos int wstat;
4685*56bb7041Schristos int ret;
4686*56bb7041Schristos
4687*56bb7041Schristos if (debug_threads)
4688*56bb7041Schristos debug_printf ("detach: step over in progress, finish it first\n");
4689*56bb7041Schristos
4690*56bb7041Schristos /* Passing NULL_PTID as filter indicates we want all events to
4691*56bb7041Schristos be left pending. Eventually this returns when there are no
4692*56bb7041Schristos unwaited-for children left. */
4693*56bb7041Schristos ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat,
4694*56bb7041Schristos __WALL);
4695*56bb7041Schristos gdb_assert (ret == -1);
4696*56bb7041Schristos
4697*56bb7041Schristos lwp = find_lwp_pid (step_over_bkpt);
4698*56bb7041Schristos if (lwp != NULL)
4699*56bb7041Schristos finish_step_over (lwp);
4700*56bb7041Schristos step_over_bkpt = null_ptid;
4701*56bb7041Schristos unsuspend_all_lwps (lwp);
4702*56bb7041Schristos }
4703*56bb7041Schristos }
4704*56bb7041Schristos
4705*56bb7041Schristos void
resume_one_thread(thread_info * thread,bool leave_all_stopped)4706*56bb7041Schristos linux_process_target::resume_one_thread (thread_info *thread,
4707*56bb7041Schristos bool leave_all_stopped)
4708*56bb7041Schristos {
4709*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
4710*56bb7041Schristos int leave_pending;
4711*56bb7041Schristos
4712*56bb7041Schristos if (lwp->resume == NULL)
4713*56bb7041Schristos return;
4714*56bb7041Schristos
4715*56bb7041Schristos if (lwp->resume->kind == resume_stop)
4716*56bb7041Schristos {
4717*56bb7041Schristos if (debug_threads)
4718*56bb7041Schristos debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread));
4719*56bb7041Schristos
4720*56bb7041Schristos if (!lwp->stopped)
4721*56bb7041Schristos {
4722*56bb7041Schristos if (debug_threads)
4723*56bb7041Schristos debug_printf ("stopping LWP %ld\n", lwpid_of (thread));
4724*56bb7041Schristos
4725*56bb7041Schristos /* Stop the thread, and wait for the event asynchronously,
4726*56bb7041Schristos through the event loop. */
4727*56bb7041Schristos send_sigstop (lwp);
4728*56bb7041Schristos }
4729*56bb7041Schristos else
4730*56bb7041Schristos {
4731*56bb7041Schristos if (debug_threads)
4732*56bb7041Schristos debug_printf ("already stopped LWP %ld\n",
4733*56bb7041Schristos lwpid_of (thread));
4734*56bb7041Schristos
4735*56bb7041Schristos /* The LWP may have been stopped in an internal event that
4736*56bb7041Schristos was not meant to be notified back to GDB (e.g., gdbserver
4737*56bb7041Schristos breakpoint), so we should be reporting a stop event in
4738*56bb7041Schristos this case too. */
4739*56bb7041Schristos
4740*56bb7041Schristos /* If the thread already has a pending SIGSTOP, this is a
4741*56bb7041Schristos no-op. Otherwise, something later will presumably resume
4742*56bb7041Schristos the thread and this will cause it to cancel any pending
4743*56bb7041Schristos operation, due to last_resume_kind == resume_stop. If
4744*56bb7041Schristos the thread already has a pending status to report, we
4745*56bb7041Schristos will still report it the next time we wait - see
4746*56bb7041Schristos status_pending_p_callback. */
4747*56bb7041Schristos
4748*56bb7041Schristos /* If we already have a pending signal to report, then
4749*56bb7041Schristos there's no need to queue a SIGSTOP, as this means we're
4750*56bb7041Schristos midway through moving the LWP out of the jumppad, and we
4751*56bb7041Schristos will report the pending signal as soon as that is
4752*56bb7041Schristos finished. */
4753*56bb7041Schristos if (lwp->pending_signals_to_report.empty ())
4754*56bb7041Schristos send_sigstop (lwp);
4755*56bb7041Schristos }
4756*56bb7041Schristos
4757*56bb7041Schristos /* For stop requests, we're done. */
4758*56bb7041Schristos lwp->resume = NULL;
4759*56bb7041Schristos thread->last_status.kind = TARGET_WAITKIND_IGNORE;
4760*56bb7041Schristos return;
4761*56bb7041Schristos }
4762*56bb7041Schristos
4763*56bb7041Schristos /* If this thread which is about to be resumed has a pending status,
4764*56bb7041Schristos then don't resume it - we can just report the pending status.
4765*56bb7041Schristos Likewise if it is suspended, because e.g., another thread is
4766*56bb7041Schristos stepping past a breakpoint. Make sure to queue any signals that
4767*56bb7041Schristos would otherwise be sent. In all-stop mode, we do this decision
4768*56bb7041Schristos based on if *any* thread has a pending status. If there's a
4769*56bb7041Schristos thread that needs the step-over-breakpoint dance, then don't
4770*56bb7041Schristos resume any other thread but that particular one. */
4771*56bb7041Schristos leave_pending = (lwp->suspended
4772*56bb7041Schristos || lwp->status_pending_p
4773*56bb7041Schristos || leave_all_stopped);
4774*56bb7041Schristos
4775*56bb7041Schristos /* If we have a new signal, enqueue the signal. */
4776*56bb7041Schristos if (lwp->resume->sig != 0)
4777*56bb7041Schristos {
4778*56bb7041Schristos siginfo_t info, *info_p;
4779*56bb7041Schristos
4780*56bb7041Schristos /* If this is the same signal we were previously stopped by,
4781*56bb7041Schristos make sure to queue its siginfo. */
4782*56bb7041Schristos if (WIFSTOPPED (lwp->last_status)
4783*56bb7041Schristos && WSTOPSIG (lwp->last_status) == lwp->resume->sig
4784*56bb7041Schristos && ptrace (PTRACE_GETSIGINFO, lwpid_of (thread),
4785*56bb7041Schristos (PTRACE_TYPE_ARG3) 0, &info) == 0)
4786*56bb7041Schristos info_p = &info;
4787*56bb7041Schristos else
4788*56bb7041Schristos info_p = NULL;
4789*56bb7041Schristos
4790*56bb7041Schristos enqueue_pending_signal (lwp, lwp->resume->sig, info_p);
4791*56bb7041Schristos }
4792*56bb7041Schristos
4793*56bb7041Schristos if (!leave_pending)
4794*56bb7041Schristos {
4795*56bb7041Schristos if (debug_threads)
4796*56bb7041Schristos debug_printf ("resuming LWP %ld\n", lwpid_of (thread));
4797*56bb7041Schristos
4798*56bb7041Schristos proceed_one_lwp (thread, NULL);
4799*56bb7041Schristos }
4800*56bb7041Schristos else
4801*56bb7041Schristos {
4802*56bb7041Schristos if (debug_threads)
4803*56bb7041Schristos debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread));
4804*56bb7041Schristos }
4805*56bb7041Schristos
4806*56bb7041Schristos thread->last_status.kind = TARGET_WAITKIND_IGNORE;
4807*56bb7041Schristos lwp->resume = NULL;
4808*56bb7041Schristos }
4809*56bb7041Schristos
4810*56bb7041Schristos void
resume(thread_resume * resume_info,size_t n)4811*56bb7041Schristos linux_process_target::resume (thread_resume *resume_info, size_t n)
4812*56bb7041Schristos {
4813*56bb7041Schristos struct thread_info *need_step_over = NULL;
4814*56bb7041Schristos
4815*56bb7041Schristos if (debug_threads)
4816*56bb7041Schristos {
4817*56bb7041Schristos debug_enter ();
4818*56bb7041Schristos debug_printf ("linux_resume:\n");
4819*56bb7041Schristos }
4820*56bb7041Schristos
4821*56bb7041Schristos for_each_thread ([&] (thread_info *thread)
4822*56bb7041Schristos {
4823*56bb7041Schristos linux_set_resume_request (thread, resume_info, n);
4824*56bb7041Schristos });
4825*56bb7041Schristos
4826*56bb7041Schristos /* If there is a thread which would otherwise be resumed, which has
4827*56bb7041Schristos a pending status, then don't resume any threads - we can just
4828*56bb7041Schristos report the pending status. Make sure to queue any signals that
4829*56bb7041Schristos would otherwise be sent. In non-stop mode, we'll apply this
4830*56bb7041Schristos logic to each thread individually. We consume all pending events
4831*56bb7041Schristos before considering to start a step-over (in all-stop). */
4832*56bb7041Schristos bool any_pending = false;
4833*56bb7041Schristos if (!non_stop)
4834*56bb7041Schristos any_pending = find_thread ([this] (thread_info *thread)
4835*56bb7041Schristos {
4836*56bb7041Schristos return resume_status_pending (thread);
4837*56bb7041Schristos }) != nullptr;
4838*56bb7041Schristos
4839*56bb7041Schristos /* If there is a thread which would otherwise be resumed, which is
4840*56bb7041Schristos stopped at a breakpoint that needs stepping over, then don't
4841*56bb7041Schristos resume any threads - have it step over the breakpoint with all
4842*56bb7041Schristos other threads stopped, then resume all threads again. Make sure
4843*56bb7041Schristos to queue any signals that would otherwise be delivered or
4844*56bb7041Schristos queued. */
4845*56bb7041Schristos if (!any_pending && low_supports_breakpoints ())
4846*56bb7041Schristos need_step_over = find_thread ([this] (thread_info *thread)
4847*56bb7041Schristos {
4848*56bb7041Schristos return thread_needs_step_over (thread);
4849*56bb7041Schristos });
4850*56bb7041Schristos
4851*56bb7041Schristos bool leave_all_stopped = (need_step_over != NULL || any_pending);
4852*56bb7041Schristos
4853*56bb7041Schristos if (debug_threads)
4854*56bb7041Schristos {
4855*56bb7041Schristos if (need_step_over != NULL)
4856*56bb7041Schristos debug_printf ("Not resuming all, need step over\n");
4857*56bb7041Schristos else if (any_pending)
4858*56bb7041Schristos debug_printf ("Not resuming, all-stop and found "
4859*56bb7041Schristos "an LWP with pending status\n");
4860*56bb7041Schristos else
4861*56bb7041Schristos debug_printf ("Resuming, no pending status or step over needed\n");
4862*56bb7041Schristos }
4863*56bb7041Schristos
4864*56bb7041Schristos /* Even if we're leaving threads stopped, queue all signals we'd
4865*56bb7041Schristos otherwise deliver. */
4866*56bb7041Schristos for_each_thread ([&] (thread_info *thread)
4867*56bb7041Schristos {
4868*56bb7041Schristos resume_one_thread (thread, leave_all_stopped);
4869*56bb7041Schristos });
4870*56bb7041Schristos
4871*56bb7041Schristos if (need_step_over)
4872*56bb7041Schristos start_step_over (get_thread_lwp (need_step_over));
4873*56bb7041Schristos
4874*56bb7041Schristos if (debug_threads)
4875*56bb7041Schristos {
4876*56bb7041Schristos debug_printf ("linux_resume done\n");
4877*56bb7041Schristos debug_exit ();
4878*56bb7041Schristos }
4879*56bb7041Schristos
4880*56bb7041Schristos /* We may have events that were pending that can/should be sent to
4881*56bb7041Schristos the client now. Trigger a linux_wait call. */
4882*56bb7041Schristos if (target_is_async_p ())
4883*56bb7041Schristos async_file_mark ();
4884*56bb7041Schristos }
4885*56bb7041Schristos
4886*56bb7041Schristos void
proceed_one_lwp(thread_info * thread,lwp_info * except)4887*56bb7041Schristos linux_process_target::proceed_one_lwp (thread_info *thread, lwp_info *except)
4888*56bb7041Schristos {
4889*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
4890*56bb7041Schristos int step;
4891*56bb7041Schristos
4892*56bb7041Schristos if (lwp == except)
4893*56bb7041Schristos return;
4894*56bb7041Schristos
4895*56bb7041Schristos if (debug_threads)
4896*56bb7041Schristos debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread));
4897*56bb7041Schristos
4898*56bb7041Schristos if (!lwp->stopped)
4899*56bb7041Schristos {
4900*56bb7041Schristos if (debug_threads)
4901*56bb7041Schristos debug_printf (" LWP %ld already running\n", lwpid_of (thread));
4902*56bb7041Schristos return;
4903*56bb7041Schristos }
4904*56bb7041Schristos
4905*56bb7041Schristos if (thread->last_resume_kind == resume_stop
4906*56bb7041Schristos && thread->last_status.kind != TARGET_WAITKIND_IGNORE)
4907*56bb7041Schristos {
4908*56bb7041Schristos if (debug_threads)
4909*56bb7041Schristos debug_printf (" client wants LWP to remain %ld stopped\n",
4910*56bb7041Schristos lwpid_of (thread));
4911*56bb7041Schristos return;
4912*56bb7041Schristos }
4913*56bb7041Schristos
4914*56bb7041Schristos if (lwp->status_pending_p)
4915*56bb7041Schristos {
4916*56bb7041Schristos if (debug_threads)
4917*56bb7041Schristos debug_printf (" LWP %ld has pending status, leaving stopped\n",
4918*56bb7041Schristos lwpid_of (thread));
4919*56bb7041Schristos return;
4920*56bb7041Schristos }
4921*56bb7041Schristos
4922*56bb7041Schristos gdb_assert (lwp->suspended >= 0);
4923*56bb7041Schristos
4924*56bb7041Schristos if (lwp->suspended)
4925*56bb7041Schristos {
4926*56bb7041Schristos if (debug_threads)
4927*56bb7041Schristos debug_printf (" LWP %ld is suspended\n", lwpid_of (thread));
4928*56bb7041Schristos return;
4929*56bb7041Schristos }
4930*56bb7041Schristos
4931*56bb7041Schristos if (thread->last_resume_kind == resume_stop
4932*56bb7041Schristos && lwp->pending_signals_to_report.empty ()
4933*56bb7041Schristos && (lwp->collecting_fast_tracepoint
4934*56bb7041Schristos == fast_tpoint_collect_result::not_collecting))
4935*56bb7041Schristos {
4936*56bb7041Schristos /* We haven't reported this LWP as stopped yet (otherwise, the
4937*56bb7041Schristos last_status.kind check above would catch it, and we wouldn't
4938*56bb7041Schristos reach here. This LWP may have been momentarily paused by a
4939*56bb7041Schristos stop_all_lwps call while handling for example, another LWP's
4940*56bb7041Schristos step-over. In that case, the pending expected SIGSTOP signal
4941*56bb7041Schristos that was queued at vCont;t handling time will have already
4942*56bb7041Schristos been consumed by wait_for_sigstop, and so we need to requeue
4943*56bb7041Schristos another one here. Note that if the LWP already has a SIGSTOP
4944*56bb7041Schristos pending, this is a no-op. */
4945*56bb7041Schristos
4946*56bb7041Schristos if (debug_threads)
4947*56bb7041Schristos debug_printf ("Client wants LWP %ld to stop. "
4948*56bb7041Schristos "Making sure it has a SIGSTOP pending\n",
4949*56bb7041Schristos lwpid_of (thread));
4950*56bb7041Schristos
4951*56bb7041Schristos send_sigstop (lwp);
4952*56bb7041Schristos }
4953*56bb7041Schristos
4954*56bb7041Schristos if (thread->last_resume_kind == resume_step)
4955*56bb7041Schristos {
4956*56bb7041Schristos if (debug_threads)
4957*56bb7041Schristos debug_printf (" stepping LWP %ld, client wants it stepping\n",
4958*56bb7041Schristos lwpid_of (thread));
4959*56bb7041Schristos
4960*56bb7041Schristos /* If resume_step is requested by GDB, install single-step
4961*56bb7041Schristos breakpoints when the thread is about to be actually resumed if
4962*56bb7041Schristos the single-step breakpoints weren't removed. */
4963*56bb7041Schristos if (supports_software_single_step ()
4964*56bb7041Schristos && !has_single_step_breakpoints (thread))
4965*56bb7041Schristos install_software_single_step_breakpoints (lwp);
4966*56bb7041Schristos
4967*56bb7041Schristos step = maybe_hw_step (thread);
4968*56bb7041Schristos }
4969*56bb7041Schristos else if (lwp->bp_reinsert != 0)
4970*56bb7041Schristos {
4971*56bb7041Schristos if (debug_threads)
4972*56bb7041Schristos debug_printf (" stepping LWP %ld, reinsert set\n",
4973*56bb7041Schristos lwpid_of (thread));
4974*56bb7041Schristos
4975*56bb7041Schristos step = maybe_hw_step (thread);
4976*56bb7041Schristos }
4977*56bb7041Schristos else
4978*56bb7041Schristos step = 0;
4979*56bb7041Schristos
4980*56bb7041Schristos resume_one_lwp (lwp, step, 0, NULL);
4981*56bb7041Schristos }
4982*56bb7041Schristos
4983*56bb7041Schristos void
unsuspend_and_proceed_one_lwp(thread_info * thread,lwp_info * except)4984*56bb7041Schristos linux_process_target::unsuspend_and_proceed_one_lwp (thread_info *thread,
4985*56bb7041Schristos lwp_info *except)
4986*56bb7041Schristos {
4987*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
4988*56bb7041Schristos
4989*56bb7041Schristos if (lwp == except)
4990*56bb7041Schristos return;
4991*56bb7041Schristos
4992*56bb7041Schristos lwp_suspended_decr (lwp);
4993*56bb7041Schristos
4994*56bb7041Schristos proceed_one_lwp (thread, except);
4995*56bb7041Schristos }
4996*56bb7041Schristos
4997*56bb7041Schristos void
proceed_all_lwps()4998*56bb7041Schristos linux_process_target::proceed_all_lwps ()
4999*56bb7041Schristos {
5000*56bb7041Schristos struct thread_info *need_step_over;
5001*56bb7041Schristos
5002*56bb7041Schristos /* If there is a thread which would otherwise be resumed, which is
5003*56bb7041Schristos stopped at a breakpoint that needs stepping over, then don't
5004*56bb7041Schristos resume any threads - have it step over the breakpoint with all
5005*56bb7041Schristos other threads stopped, then resume all threads again. */
5006*56bb7041Schristos
5007*56bb7041Schristos if (low_supports_breakpoints ())
5008*56bb7041Schristos {
5009*56bb7041Schristos need_step_over = find_thread ([this] (thread_info *thread)
5010*56bb7041Schristos {
5011*56bb7041Schristos return thread_needs_step_over (thread);
5012*56bb7041Schristos });
5013*56bb7041Schristos
5014*56bb7041Schristos if (need_step_over != NULL)
5015*56bb7041Schristos {
5016*56bb7041Schristos if (debug_threads)
5017*56bb7041Schristos debug_printf ("proceed_all_lwps: found "
5018*56bb7041Schristos "thread %ld needing a step-over\n",
5019*56bb7041Schristos lwpid_of (need_step_over));
5020*56bb7041Schristos
5021*56bb7041Schristos start_step_over (get_thread_lwp (need_step_over));
5022*56bb7041Schristos return;
5023*56bb7041Schristos }
5024*56bb7041Schristos }
5025*56bb7041Schristos
5026*56bb7041Schristos if (debug_threads)
5027*56bb7041Schristos debug_printf ("Proceeding, no step-over needed\n");
5028*56bb7041Schristos
5029*56bb7041Schristos for_each_thread ([this] (thread_info *thread)
5030*56bb7041Schristos {
5031*56bb7041Schristos proceed_one_lwp (thread, NULL);
5032*56bb7041Schristos });
5033*56bb7041Schristos }
5034*56bb7041Schristos
5035*56bb7041Schristos void
unstop_all_lwps(int unsuspend,lwp_info * except)5036*56bb7041Schristos linux_process_target::unstop_all_lwps (int unsuspend, lwp_info *except)
5037*56bb7041Schristos {
5038*56bb7041Schristos if (debug_threads)
5039*56bb7041Schristos {
5040*56bb7041Schristos debug_enter ();
5041*56bb7041Schristos if (except)
5042*56bb7041Schristos debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
5043*56bb7041Schristos lwpid_of (get_lwp_thread (except)));
5044*56bb7041Schristos else
5045*56bb7041Schristos debug_printf ("unstopping all lwps\n");
5046*56bb7041Schristos }
5047*56bb7041Schristos
5048*56bb7041Schristos if (unsuspend)
5049*56bb7041Schristos for_each_thread ([&] (thread_info *thread)
5050*56bb7041Schristos {
5051*56bb7041Schristos unsuspend_and_proceed_one_lwp (thread, except);
5052*56bb7041Schristos });
5053*56bb7041Schristos else
5054*56bb7041Schristos for_each_thread ([&] (thread_info *thread)
5055*56bb7041Schristos {
5056*56bb7041Schristos proceed_one_lwp (thread, except);
5057*56bb7041Schristos });
5058*56bb7041Schristos
5059*56bb7041Schristos if (debug_threads)
5060*56bb7041Schristos {
5061*56bb7041Schristos debug_printf ("unstop_all_lwps done\n");
5062*56bb7041Schristos debug_exit ();
5063*56bb7041Schristos }
5064*56bb7041Schristos }
5065*56bb7041Schristos
5066*56bb7041Schristos
5067*56bb7041Schristos #ifdef HAVE_LINUX_REGSETS
5068*56bb7041Schristos
5069*56bb7041Schristos #define use_linux_regsets 1
5070*56bb7041Schristos
5071*56bb7041Schristos /* Returns true if REGSET has been disabled. */
5072*56bb7041Schristos
5073*56bb7041Schristos static int
regset_disabled(struct regsets_info * info,struct regset_info * regset)5074*56bb7041Schristos regset_disabled (struct regsets_info *info, struct regset_info *regset)
5075*56bb7041Schristos {
5076*56bb7041Schristos return (info->disabled_regsets != NULL
5077*56bb7041Schristos && info->disabled_regsets[regset - info->regsets]);
5078*56bb7041Schristos }
5079*56bb7041Schristos
5080*56bb7041Schristos /* Disable REGSET. */
5081*56bb7041Schristos
5082*56bb7041Schristos static void
disable_regset(struct regsets_info * info,struct regset_info * regset)5083*56bb7041Schristos disable_regset (struct regsets_info *info, struct regset_info *regset)
5084*56bb7041Schristos {
5085*56bb7041Schristos int dr_offset;
5086*56bb7041Schristos
5087*56bb7041Schristos dr_offset = regset - info->regsets;
5088*56bb7041Schristos if (info->disabled_regsets == NULL)
5089*56bb7041Schristos info->disabled_regsets = (char *) xcalloc (1, info->num_regsets);
5090*56bb7041Schristos info->disabled_regsets[dr_offset] = 1;
5091*56bb7041Schristos }
5092*56bb7041Schristos
5093*56bb7041Schristos static int
regsets_fetch_inferior_registers(struct regsets_info * regsets_info,struct regcache * regcache)5094*56bb7041Schristos regsets_fetch_inferior_registers (struct regsets_info *regsets_info,
5095*56bb7041Schristos struct regcache *regcache)
5096*56bb7041Schristos {
5097*56bb7041Schristos struct regset_info *regset;
5098*56bb7041Schristos int saw_general_regs = 0;
5099*56bb7041Schristos int pid;
5100*56bb7041Schristos struct iovec iov;
5101*56bb7041Schristos
5102*56bb7041Schristos pid = lwpid_of (current_thread);
5103*56bb7041Schristos for (regset = regsets_info->regsets; regset->size >= 0; regset++)
5104*56bb7041Schristos {
5105*56bb7041Schristos void *buf, *data;
5106*56bb7041Schristos int nt_type, res;
5107*56bb7041Schristos
5108*56bb7041Schristos if (regset->size == 0 || regset_disabled (regsets_info, regset))
5109*56bb7041Schristos continue;
5110*56bb7041Schristos
5111*56bb7041Schristos buf = xmalloc (regset->size);
5112*56bb7041Schristos
5113*56bb7041Schristos nt_type = regset->nt_type;
5114*56bb7041Schristos if (nt_type)
5115*56bb7041Schristos {
5116*56bb7041Schristos iov.iov_base = buf;
5117*56bb7041Schristos iov.iov_len = regset->size;
5118*56bb7041Schristos data = (void *) &iov;
5119*56bb7041Schristos }
5120*56bb7041Schristos else
5121*56bb7041Schristos data = buf;
5122*56bb7041Schristos
5123*56bb7041Schristos #ifndef __sparc__
5124*56bb7041Schristos res = ptrace (regset->get_request, pid,
5125*56bb7041Schristos (PTRACE_TYPE_ARG3) (long) nt_type, data);
5126*56bb7041Schristos #else
5127*56bb7041Schristos res = ptrace (regset->get_request, pid, data, nt_type);
5128*56bb7041Schristos #endif
5129*56bb7041Schristos if (res < 0)
5130*56bb7041Schristos {
5131*56bb7041Schristos if (errno == EIO
5132*56bb7041Schristos || (errno == EINVAL && regset->type == OPTIONAL_REGS))
5133*56bb7041Schristos {
5134*56bb7041Schristos /* If we get EIO on a regset, or an EINVAL and the regset is
5135*56bb7041Schristos optional, do not try it again for this process mode. */
5136*56bb7041Schristos disable_regset (regsets_info, regset);
5137*56bb7041Schristos }
5138*56bb7041Schristos else if (errno == ENODATA)
5139*56bb7041Schristos {
5140*56bb7041Schristos /* ENODATA may be returned if the regset is currently
5141*56bb7041Schristos not "active". This can happen in normal operation,
5142*56bb7041Schristos so suppress the warning in this case. */
5143*56bb7041Schristos }
5144*56bb7041Schristos else if (errno == ESRCH)
5145*56bb7041Schristos {
5146*56bb7041Schristos /* At this point, ESRCH should mean the process is
5147*56bb7041Schristos already gone, in which case we simply ignore attempts
5148*56bb7041Schristos to read its registers. */
5149*56bb7041Schristos }
5150*56bb7041Schristos else
5151*56bb7041Schristos {
5152*56bb7041Schristos char s[256];
5153*56bb7041Schristos sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
5154*56bb7041Schristos pid);
5155*56bb7041Schristos perror (s);
5156*56bb7041Schristos }
5157*56bb7041Schristos }
5158*56bb7041Schristos else
5159*56bb7041Schristos {
5160*56bb7041Schristos if (regset->type == GENERAL_REGS)
5161*56bb7041Schristos saw_general_regs = 1;
5162*56bb7041Schristos regset->store_function (regcache, buf);
5163*56bb7041Schristos }
5164*56bb7041Schristos free (buf);
5165*56bb7041Schristos }
5166*56bb7041Schristos if (saw_general_regs)
5167*56bb7041Schristos return 0;
5168*56bb7041Schristos else
5169*56bb7041Schristos return 1;
5170*56bb7041Schristos }
5171*56bb7041Schristos
5172*56bb7041Schristos static int
regsets_store_inferior_registers(struct regsets_info * regsets_info,struct regcache * regcache)5173*56bb7041Schristos regsets_store_inferior_registers (struct regsets_info *regsets_info,
5174*56bb7041Schristos struct regcache *regcache)
5175*56bb7041Schristos {
5176*56bb7041Schristos struct regset_info *regset;
5177*56bb7041Schristos int saw_general_regs = 0;
5178*56bb7041Schristos int pid;
5179*56bb7041Schristos struct iovec iov;
5180*56bb7041Schristos
5181*56bb7041Schristos pid = lwpid_of (current_thread);
5182*56bb7041Schristos for (regset = regsets_info->regsets; regset->size >= 0; regset++)
5183*56bb7041Schristos {
5184*56bb7041Schristos void *buf, *data;
5185*56bb7041Schristos int nt_type, res;
5186*56bb7041Schristos
5187*56bb7041Schristos if (regset->size == 0 || regset_disabled (regsets_info, regset)
5188*56bb7041Schristos || regset->fill_function == NULL)
5189*56bb7041Schristos continue;
5190*56bb7041Schristos
5191*56bb7041Schristos buf = xmalloc (regset->size);
5192*56bb7041Schristos
5193*56bb7041Schristos /* First fill the buffer with the current register set contents,
5194*56bb7041Schristos in case there are any items in the kernel's regset that are
5195*56bb7041Schristos not in gdbserver's regcache. */
5196*56bb7041Schristos
5197*56bb7041Schristos nt_type = regset->nt_type;
5198*56bb7041Schristos if (nt_type)
5199*56bb7041Schristos {
5200*56bb7041Schristos iov.iov_base = buf;
5201*56bb7041Schristos iov.iov_len = regset->size;
5202*56bb7041Schristos data = (void *) &iov;
5203*56bb7041Schristos }
5204*56bb7041Schristos else
5205*56bb7041Schristos data = buf;
5206*56bb7041Schristos
5207*56bb7041Schristos #ifndef __sparc__
5208*56bb7041Schristos res = ptrace (regset->get_request, pid,
5209*56bb7041Schristos (PTRACE_TYPE_ARG3) (long) nt_type, data);
5210*56bb7041Schristos #else
5211*56bb7041Schristos res = ptrace (regset->get_request, pid, data, nt_type);
5212*56bb7041Schristos #endif
5213*56bb7041Schristos
5214*56bb7041Schristos if (res == 0)
5215*56bb7041Schristos {
5216*56bb7041Schristos /* Then overlay our cached registers on that. */
5217*56bb7041Schristos regset->fill_function (regcache, buf);
5218*56bb7041Schristos
5219*56bb7041Schristos /* Only now do we write the register set. */
5220*56bb7041Schristos #ifndef __sparc__
5221*56bb7041Schristos res = ptrace (regset->set_request, pid,
5222*56bb7041Schristos (PTRACE_TYPE_ARG3) (long) nt_type, data);
5223*56bb7041Schristos #else
5224*56bb7041Schristos res = ptrace (regset->set_request, pid, data, nt_type);
5225*56bb7041Schristos #endif
5226*56bb7041Schristos }
5227*56bb7041Schristos
5228*56bb7041Schristos if (res < 0)
5229*56bb7041Schristos {
5230*56bb7041Schristos if (errno == EIO
5231*56bb7041Schristos || (errno == EINVAL && regset->type == OPTIONAL_REGS))
5232*56bb7041Schristos {
5233*56bb7041Schristos /* If we get EIO on a regset, or an EINVAL and the regset is
5234*56bb7041Schristos optional, do not try it again for this process mode. */
5235*56bb7041Schristos disable_regset (regsets_info, regset);
5236*56bb7041Schristos }
5237*56bb7041Schristos else if (errno == ESRCH)
5238*56bb7041Schristos {
5239*56bb7041Schristos /* At this point, ESRCH should mean the process is
5240*56bb7041Schristos already gone, in which case we simply ignore attempts
5241*56bb7041Schristos to change its registers. See also the related
5242*56bb7041Schristos comment in resume_one_lwp. */
5243*56bb7041Schristos free (buf);
5244*56bb7041Schristos return 0;
5245*56bb7041Schristos }
5246*56bb7041Schristos else
5247*56bb7041Schristos {
5248*56bb7041Schristos perror ("Warning: ptrace(regsets_store_inferior_registers)");
5249*56bb7041Schristos }
5250*56bb7041Schristos }
5251*56bb7041Schristos else if (regset->type == GENERAL_REGS)
5252*56bb7041Schristos saw_general_regs = 1;
5253*56bb7041Schristos free (buf);
5254*56bb7041Schristos }
5255*56bb7041Schristos if (saw_general_regs)
5256*56bb7041Schristos return 0;
5257*56bb7041Schristos else
5258*56bb7041Schristos return 1;
5259*56bb7041Schristos }
5260*56bb7041Schristos
5261*56bb7041Schristos #else /* !HAVE_LINUX_REGSETS */
5262*56bb7041Schristos
5263*56bb7041Schristos #define use_linux_regsets 0
5264*56bb7041Schristos #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5265*56bb7041Schristos #define regsets_store_inferior_registers(regsets_info, regcache) 1
5266*56bb7041Schristos
5267*56bb7041Schristos #endif
5268*56bb7041Schristos
5269*56bb7041Schristos /* Return 1 if register REGNO is supported by one of the regset ptrace
5270*56bb7041Schristos calls or 0 if it has to be transferred individually. */
5271*56bb7041Schristos
5272*56bb7041Schristos static int
linux_register_in_regsets(const struct regs_info * regs_info,int regno)5273*56bb7041Schristos linux_register_in_regsets (const struct regs_info *regs_info, int regno)
5274*56bb7041Schristos {
5275*56bb7041Schristos unsigned char mask = 1 << (regno % 8);
5276*56bb7041Schristos size_t index = regno / 8;
5277*56bb7041Schristos
5278*56bb7041Schristos return (use_linux_regsets
5279*56bb7041Schristos && (regs_info->regset_bitmap == NULL
5280*56bb7041Schristos || (regs_info->regset_bitmap[index] & mask) != 0));
5281*56bb7041Schristos }
5282*56bb7041Schristos
5283*56bb7041Schristos #ifdef HAVE_LINUX_USRREGS
5284*56bb7041Schristos
5285*56bb7041Schristos static int
register_addr(const struct usrregs_info * usrregs,int regnum)5286*56bb7041Schristos register_addr (const struct usrregs_info *usrregs, int regnum)
5287*56bb7041Schristos {
5288*56bb7041Schristos int addr;
5289*56bb7041Schristos
5290*56bb7041Schristos if (regnum < 0 || regnum >= usrregs->num_regs)
5291*56bb7041Schristos error ("Invalid register number %d.", regnum);
5292*56bb7041Schristos
5293*56bb7041Schristos addr = usrregs->regmap[regnum];
5294*56bb7041Schristos
5295*56bb7041Schristos return addr;
5296*56bb7041Schristos }
5297*56bb7041Schristos
5298*56bb7041Schristos
5299*56bb7041Schristos void
fetch_register(const usrregs_info * usrregs,regcache * regcache,int regno)5300*56bb7041Schristos linux_process_target::fetch_register (const usrregs_info *usrregs,
5301*56bb7041Schristos regcache *regcache, int regno)
5302*56bb7041Schristos {
5303*56bb7041Schristos CORE_ADDR regaddr;
5304*56bb7041Schristos int i, size;
5305*56bb7041Schristos char *buf;
5306*56bb7041Schristos int pid;
5307*56bb7041Schristos
5308*56bb7041Schristos if (regno >= usrregs->num_regs)
5309*56bb7041Schristos return;
5310*56bb7041Schristos if (low_cannot_fetch_register (regno))
5311*56bb7041Schristos return;
5312*56bb7041Schristos
5313*56bb7041Schristos regaddr = register_addr (usrregs, regno);
5314*56bb7041Schristos if (regaddr == -1)
5315*56bb7041Schristos return;
5316*56bb7041Schristos
5317*56bb7041Schristos size = ((register_size (regcache->tdesc, regno)
5318*56bb7041Schristos + sizeof (PTRACE_XFER_TYPE) - 1)
5319*56bb7041Schristos & -sizeof (PTRACE_XFER_TYPE));
5320*56bb7041Schristos buf = (char *) alloca (size);
5321*56bb7041Schristos
5322*56bb7041Schristos pid = lwpid_of (current_thread);
5323*56bb7041Schristos for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
5324*56bb7041Schristos {
5325*56bb7041Schristos errno = 0;
5326*56bb7041Schristos *(PTRACE_XFER_TYPE *) (buf + i) =
5327*56bb7041Schristos ptrace (PTRACE_PEEKUSER, pid,
5328*56bb7041Schristos /* Coerce to a uintptr_t first to avoid potential gcc warning
5329*56bb7041Schristos of coercing an 8 byte integer to a 4 byte pointer. */
5330*56bb7041Schristos (PTRACE_TYPE_ARG3) (uintptr_t) regaddr, (PTRACE_TYPE_ARG4) 0);
5331*56bb7041Schristos regaddr += sizeof (PTRACE_XFER_TYPE);
5332*56bb7041Schristos if (errno != 0)
5333*56bb7041Schristos {
5334*56bb7041Schristos /* Mark register REGNO unavailable. */
5335*56bb7041Schristos supply_register (regcache, regno, NULL);
5336*56bb7041Schristos return;
5337*56bb7041Schristos }
5338*56bb7041Schristos }
5339*56bb7041Schristos
5340*56bb7041Schristos low_supply_ptrace_register (regcache, regno, buf);
5341*56bb7041Schristos }
5342*56bb7041Schristos
5343*56bb7041Schristos void
store_register(const usrregs_info * usrregs,regcache * regcache,int regno)5344*56bb7041Schristos linux_process_target::store_register (const usrregs_info *usrregs,
5345*56bb7041Schristos regcache *regcache, int regno)
5346*56bb7041Schristos {
5347*56bb7041Schristos CORE_ADDR regaddr;
5348*56bb7041Schristos int i, size;
5349*56bb7041Schristos char *buf;
5350*56bb7041Schristos int pid;
5351*56bb7041Schristos
5352*56bb7041Schristos if (regno >= usrregs->num_regs)
5353*56bb7041Schristos return;
5354*56bb7041Schristos if (low_cannot_store_register (regno))
5355*56bb7041Schristos return;
5356*56bb7041Schristos
5357*56bb7041Schristos regaddr = register_addr (usrregs, regno);
5358*56bb7041Schristos if (regaddr == -1)
5359*56bb7041Schristos return;
5360*56bb7041Schristos
5361*56bb7041Schristos size = ((register_size (regcache->tdesc, regno)
5362*56bb7041Schristos + sizeof (PTRACE_XFER_TYPE) - 1)
5363*56bb7041Schristos & -sizeof (PTRACE_XFER_TYPE));
5364*56bb7041Schristos buf = (char *) alloca (size);
5365*56bb7041Schristos memset (buf, 0, size);
5366*56bb7041Schristos
5367*56bb7041Schristos low_collect_ptrace_register (regcache, regno, buf);
5368*56bb7041Schristos
5369*56bb7041Schristos pid = lwpid_of (current_thread);
5370*56bb7041Schristos for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
5371*56bb7041Schristos {
5372*56bb7041Schristos errno = 0;
5373*56bb7041Schristos ptrace (PTRACE_POKEUSER, pid,
5374*56bb7041Schristos /* Coerce to a uintptr_t first to avoid potential gcc warning
5375*56bb7041Schristos about coercing an 8 byte integer to a 4 byte pointer. */
5376*56bb7041Schristos (PTRACE_TYPE_ARG3) (uintptr_t) regaddr,
5377*56bb7041Schristos (PTRACE_TYPE_ARG4) *(PTRACE_XFER_TYPE *) (buf + i));
5378*56bb7041Schristos if (errno != 0)
5379*56bb7041Schristos {
5380*56bb7041Schristos /* At this point, ESRCH should mean the process is
5381*56bb7041Schristos already gone, in which case we simply ignore attempts
5382*56bb7041Schristos to change its registers. See also the related
5383*56bb7041Schristos comment in resume_one_lwp. */
5384*56bb7041Schristos if (errno == ESRCH)
5385*56bb7041Schristos return;
5386*56bb7041Schristos
5387*56bb7041Schristos
5388*56bb7041Schristos if (!low_cannot_store_register (regno))
5389*56bb7041Schristos error ("writing register %d: %s", regno, safe_strerror (errno));
5390*56bb7041Schristos }
5391*56bb7041Schristos regaddr += sizeof (PTRACE_XFER_TYPE);
5392*56bb7041Schristos }
5393*56bb7041Schristos }
5394*56bb7041Schristos #endif /* HAVE_LINUX_USRREGS */
5395*56bb7041Schristos
5396*56bb7041Schristos void
low_collect_ptrace_register(regcache * regcache,int regno,char * buf)5397*56bb7041Schristos linux_process_target::low_collect_ptrace_register (regcache *regcache,
5398*56bb7041Schristos int regno, char *buf)
5399*56bb7041Schristos {
5400*56bb7041Schristos collect_register (regcache, regno, buf);
5401*56bb7041Schristos }
5402*56bb7041Schristos
5403*56bb7041Schristos void
low_supply_ptrace_register(regcache * regcache,int regno,const char * buf)5404*56bb7041Schristos linux_process_target::low_supply_ptrace_register (regcache *regcache,
5405*56bb7041Schristos int regno, const char *buf)
5406*56bb7041Schristos {
5407*56bb7041Schristos supply_register (regcache, regno, buf);
5408*56bb7041Schristos }
5409*56bb7041Schristos
5410*56bb7041Schristos void
usr_fetch_inferior_registers(const regs_info * regs_info,regcache * regcache,int regno,int all)5411*56bb7041Schristos linux_process_target::usr_fetch_inferior_registers (const regs_info *regs_info,
5412*56bb7041Schristos regcache *regcache,
5413*56bb7041Schristos int regno, int all)
5414*56bb7041Schristos {
5415*56bb7041Schristos #ifdef HAVE_LINUX_USRREGS
5416*56bb7041Schristos struct usrregs_info *usr = regs_info->usrregs;
5417*56bb7041Schristos
5418*56bb7041Schristos if (regno == -1)
5419*56bb7041Schristos {
5420*56bb7041Schristos for (regno = 0; regno < usr->num_regs; regno++)
5421*56bb7041Schristos if (all || !linux_register_in_regsets (regs_info, regno))
5422*56bb7041Schristos fetch_register (usr, regcache, regno);
5423*56bb7041Schristos }
5424*56bb7041Schristos else
5425*56bb7041Schristos fetch_register (usr, regcache, regno);
5426*56bb7041Schristos #endif
5427*56bb7041Schristos }
5428*56bb7041Schristos
5429*56bb7041Schristos void
usr_store_inferior_registers(const regs_info * regs_info,regcache * regcache,int regno,int all)5430*56bb7041Schristos linux_process_target::usr_store_inferior_registers (const regs_info *regs_info,
5431*56bb7041Schristos regcache *regcache,
5432*56bb7041Schristos int regno, int all)
5433*56bb7041Schristos {
5434*56bb7041Schristos #ifdef HAVE_LINUX_USRREGS
5435*56bb7041Schristos struct usrregs_info *usr = regs_info->usrregs;
5436*56bb7041Schristos
5437*56bb7041Schristos if (regno == -1)
5438*56bb7041Schristos {
5439*56bb7041Schristos for (regno = 0; regno < usr->num_regs; regno++)
5440*56bb7041Schristos if (all || !linux_register_in_regsets (regs_info, regno))
5441*56bb7041Schristos store_register (usr, regcache, regno);
5442*56bb7041Schristos }
5443*56bb7041Schristos else
5444*56bb7041Schristos store_register (usr, regcache, regno);
5445*56bb7041Schristos #endif
5446*56bb7041Schristos }
5447*56bb7041Schristos
5448*56bb7041Schristos void
fetch_registers(regcache * regcache,int regno)5449*56bb7041Schristos linux_process_target::fetch_registers (regcache *regcache, int regno)
5450*56bb7041Schristos {
5451*56bb7041Schristos int use_regsets;
5452*56bb7041Schristos int all = 0;
5453*56bb7041Schristos const regs_info *regs_info = get_regs_info ();
5454*56bb7041Schristos
5455*56bb7041Schristos if (regno == -1)
5456*56bb7041Schristos {
5457*56bb7041Schristos if (regs_info->usrregs != NULL)
5458*56bb7041Schristos for (regno = 0; regno < regs_info->usrregs->num_regs; regno++)
5459*56bb7041Schristos low_fetch_register (regcache, regno);
5460*56bb7041Schristos
5461*56bb7041Schristos all = regsets_fetch_inferior_registers (regs_info->regsets_info, regcache);
5462*56bb7041Schristos if (regs_info->usrregs != NULL)
5463*56bb7041Schristos usr_fetch_inferior_registers (regs_info, regcache, -1, all);
5464*56bb7041Schristos }
5465*56bb7041Schristos else
5466*56bb7041Schristos {
5467*56bb7041Schristos if (low_fetch_register (regcache, regno))
5468*56bb7041Schristos return;
5469*56bb7041Schristos
5470*56bb7041Schristos use_regsets = linux_register_in_regsets (regs_info, regno);
5471*56bb7041Schristos if (use_regsets)
5472*56bb7041Schristos all = regsets_fetch_inferior_registers (regs_info->regsets_info,
5473*56bb7041Schristos regcache);
5474*56bb7041Schristos if ((!use_regsets || all) && regs_info->usrregs != NULL)
5475*56bb7041Schristos usr_fetch_inferior_registers (regs_info, regcache, regno, 1);
5476*56bb7041Schristos }
5477*56bb7041Schristos }
5478*56bb7041Schristos
5479*56bb7041Schristos void
store_registers(regcache * regcache,int regno)5480*56bb7041Schristos linux_process_target::store_registers (regcache *regcache, int regno)
5481*56bb7041Schristos {
5482*56bb7041Schristos int use_regsets;
5483*56bb7041Schristos int all = 0;
5484*56bb7041Schristos const regs_info *regs_info = get_regs_info ();
5485*56bb7041Schristos
5486*56bb7041Schristos if (regno == -1)
5487*56bb7041Schristos {
5488*56bb7041Schristos all = regsets_store_inferior_registers (regs_info->regsets_info,
5489*56bb7041Schristos regcache);
5490*56bb7041Schristos if (regs_info->usrregs != NULL)
5491*56bb7041Schristos usr_store_inferior_registers (regs_info, regcache, regno, all);
5492*56bb7041Schristos }
5493*56bb7041Schristos else
5494*56bb7041Schristos {
5495*56bb7041Schristos use_regsets = linux_register_in_regsets (regs_info, regno);
5496*56bb7041Schristos if (use_regsets)
5497*56bb7041Schristos all = regsets_store_inferior_registers (regs_info->regsets_info,
5498*56bb7041Schristos regcache);
5499*56bb7041Schristos if ((!use_regsets || all) && regs_info->usrregs != NULL)
5500*56bb7041Schristos usr_store_inferior_registers (regs_info, regcache, regno, 1);
5501*56bb7041Schristos }
5502*56bb7041Schristos }
5503*56bb7041Schristos
5504*56bb7041Schristos bool
low_fetch_register(regcache * regcache,int regno)5505*56bb7041Schristos linux_process_target::low_fetch_register (regcache *regcache, int regno)
5506*56bb7041Schristos {
5507*56bb7041Schristos return false;
5508*56bb7041Schristos }
5509*56bb7041Schristos
5510*56bb7041Schristos /* A wrapper for the read_memory target op. */
5511*56bb7041Schristos
5512*56bb7041Schristos static int
linux_read_memory(CORE_ADDR memaddr,unsigned char * myaddr,int len)5513*56bb7041Schristos linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
5514*56bb7041Schristos {
5515*56bb7041Schristos return the_target->read_memory (memaddr, myaddr, len);
5516*56bb7041Schristos }
5517*56bb7041Schristos
5518*56bb7041Schristos /* Copy LEN bytes from inferior's memory starting at MEMADDR
5519*56bb7041Schristos to debugger memory starting at MYADDR. */
5520*56bb7041Schristos
5521*56bb7041Schristos int
read_memory(CORE_ADDR memaddr,unsigned char * myaddr,int len)5522*56bb7041Schristos linux_process_target::read_memory (CORE_ADDR memaddr,
5523*56bb7041Schristos unsigned char *myaddr, int len)
5524*56bb7041Schristos {
5525*56bb7041Schristos int pid = lwpid_of (current_thread);
5526*56bb7041Schristos PTRACE_XFER_TYPE *buffer;
5527*56bb7041Schristos CORE_ADDR addr;
5528*56bb7041Schristos int count;
5529*56bb7041Schristos char filename[64];
5530*56bb7041Schristos int i;
5531*56bb7041Schristos int ret;
5532*56bb7041Schristos int fd;
5533*56bb7041Schristos
5534*56bb7041Schristos /* Try using /proc. Don't bother for one word. */
5535*56bb7041Schristos if (len >= 3 * sizeof (long))
5536*56bb7041Schristos {
5537*56bb7041Schristos int bytes;
5538*56bb7041Schristos
5539*56bb7041Schristos /* We could keep this file open and cache it - possibly one per
5540*56bb7041Schristos thread. That requires some juggling, but is even faster. */
5541*56bb7041Schristos sprintf (filename, "/proc/%d/mem", pid);
5542*56bb7041Schristos fd = open (filename, O_RDONLY | O_LARGEFILE);
5543*56bb7041Schristos if (fd == -1)
5544*56bb7041Schristos goto no_proc;
5545*56bb7041Schristos
5546*56bb7041Schristos /* If pread64 is available, use it. It's faster if the kernel
5547*56bb7041Schristos supports it (only one syscall), and it's 64-bit safe even on
5548*56bb7041Schristos 32-bit platforms (for instance, SPARC debugging a SPARC64
5549*56bb7041Schristos application). */
5550*56bb7041Schristos #ifdef HAVE_PREAD64
5551*56bb7041Schristos bytes = pread64 (fd, myaddr, len, memaddr);
5552*56bb7041Schristos #else
5553*56bb7041Schristos bytes = -1;
5554*56bb7041Schristos if (lseek (fd, memaddr, SEEK_SET) != -1)
5555*56bb7041Schristos bytes = read (fd, myaddr, len);
5556*56bb7041Schristos #endif
5557*56bb7041Schristos
5558*56bb7041Schristos close (fd);
5559*56bb7041Schristos if (bytes == len)
5560*56bb7041Schristos return 0;
5561*56bb7041Schristos
5562*56bb7041Schristos /* Some data was read, we'll try to get the rest with ptrace. */
5563*56bb7041Schristos if (bytes > 0)
5564*56bb7041Schristos {
5565*56bb7041Schristos memaddr += bytes;
5566*56bb7041Schristos myaddr += bytes;
5567*56bb7041Schristos len -= bytes;
5568*56bb7041Schristos }
5569*56bb7041Schristos }
5570*56bb7041Schristos
5571*56bb7041Schristos no_proc:
5572*56bb7041Schristos /* Round starting address down to longword boundary. */
5573*56bb7041Schristos addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
5574*56bb7041Schristos /* Round ending address up; get number of longwords that makes. */
5575*56bb7041Schristos count = ((((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
5576*56bb7041Schristos / sizeof (PTRACE_XFER_TYPE));
5577*56bb7041Schristos /* Allocate buffer of that many longwords. */
5578*56bb7041Schristos buffer = XALLOCAVEC (PTRACE_XFER_TYPE, count);
5579*56bb7041Schristos
5580*56bb7041Schristos /* Read all the longwords */
5581*56bb7041Schristos errno = 0;
5582*56bb7041Schristos for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
5583*56bb7041Schristos {
5584*56bb7041Schristos /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5585*56bb7041Schristos about coercing an 8 byte integer to a 4 byte pointer. */
5586*56bb7041Schristos buffer[i] = ptrace (PTRACE_PEEKTEXT, pid,
5587*56bb7041Schristos (PTRACE_TYPE_ARG3) (uintptr_t) addr,
5588*56bb7041Schristos (PTRACE_TYPE_ARG4) 0);
5589*56bb7041Schristos if (errno)
5590*56bb7041Schristos break;
5591*56bb7041Schristos }
5592*56bb7041Schristos ret = errno;
5593*56bb7041Schristos
5594*56bb7041Schristos /* Copy appropriate bytes out of the buffer. */
5595*56bb7041Schristos if (i > 0)
5596*56bb7041Schristos {
5597*56bb7041Schristos i *= sizeof (PTRACE_XFER_TYPE);
5598*56bb7041Schristos i -= memaddr & (sizeof (PTRACE_XFER_TYPE) - 1);
5599*56bb7041Schristos memcpy (myaddr,
5600*56bb7041Schristos (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
5601*56bb7041Schristos i < len ? i : len);
5602*56bb7041Schristos }
5603*56bb7041Schristos
5604*56bb7041Schristos return ret;
5605*56bb7041Schristos }
5606*56bb7041Schristos
5607*56bb7041Schristos /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5608*56bb7041Schristos memory at MEMADDR. On failure (cannot write to the inferior)
5609*56bb7041Schristos returns the value of errno. Always succeeds if LEN is zero. */
5610*56bb7041Schristos
5611*56bb7041Schristos int
write_memory(CORE_ADDR memaddr,const unsigned char * myaddr,int len)5612*56bb7041Schristos linux_process_target::write_memory (CORE_ADDR memaddr,
5613*56bb7041Schristos const unsigned char *myaddr, int len)
5614*56bb7041Schristos {
5615*56bb7041Schristos int i;
5616*56bb7041Schristos /* Round starting address down to longword boundary. */
5617*56bb7041Schristos CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
5618*56bb7041Schristos /* Round ending address up; get number of longwords that makes. */
5619*56bb7041Schristos int count
5620*56bb7041Schristos = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
5621*56bb7041Schristos / sizeof (PTRACE_XFER_TYPE);
5622*56bb7041Schristos
5623*56bb7041Schristos /* Allocate buffer of that many longwords. */
5624*56bb7041Schristos PTRACE_XFER_TYPE *buffer = XALLOCAVEC (PTRACE_XFER_TYPE, count);
5625*56bb7041Schristos
5626*56bb7041Schristos int pid = lwpid_of (current_thread);
5627*56bb7041Schristos
5628*56bb7041Schristos if (len == 0)
5629*56bb7041Schristos {
5630*56bb7041Schristos /* Zero length write always succeeds. */
5631*56bb7041Schristos return 0;
5632*56bb7041Schristos }
5633*56bb7041Schristos
5634*56bb7041Schristos if (debug_threads)
5635*56bb7041Schristos {
5636*56bb7041Schristos /* Dump up to four bytes. */
5637*56bb7041Schristos char str[4 * 2 + 1];
5638*56bb7041Schristos char *p = str;
5639*56bb7041Schristos int dump = len < 4 ? len : 4;
5640*56bb7041Schristos
5641*56bb7041Schristos for (i = 0; i < dump; i++)
5642*56bb7041Schristos {
5643*56bb7041Schristos sprintf (p, "%02x", myaddr[i]);
5644*56bb7041Schristos p += 2;
5645*56bb7041Schristos }
5646*56bb7041Schristos *p = '\0';
5647*56bb7041Schristos
5648*56bb7041Schristos debug_printf ("Writing %s to 0x%08lx in process %d\n",
5649*56bb7041Schristos str, (long) memaddr, pid);
5650*56bb7041Schristos }
5651*56bb7041Schristos
5652*56bb7041Schristos /* Fill start and end extra bytes of buffer with existing memory data. */
5653*56bb7041Schristos
5654*56bb7041Schristos errno = 0;
5655*56bb7041Schristos /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5656*56bb7041Schristos about coercing an 8 byte integer to a 4 byte pointer. */
5657*56bb7041Schristos buffer[0] = ptrace (PTRACE_PEEKTEXT, pid,
5658*56bb7041Schristos (PTRACE_TYPE_ARG3) (uintptr_t) addr,
5659*56bb7041Schristos (PTRACE_TYPE_ARG4) 0);
5660*56bb7041Schristos if (errno)
5661*56bb7041Schristos return errno;
5662*56bb7041Schristos
5663*56bb7041Schristos if (count > 1)
5664*56bb7041Schristos {
5665*56bb7041Schristos errno = 0;
5666*56bb7041Schristos buffer[count - 1]
5667*56bb7041Schristos = ptrace (PTRACE_PEEKTEXT, pid,
5668*56bb7041Schristos /* Coerce to a uintptr_t first to avoid potential gcc warning
5669*56bb7041Schristos about coercing an 8 byte integer to a 4 byte pointer. */
5670*56bb7041Schristos (PTRACE_TYPE_ARG3) (uintptr_t) (addr + (count - 1)
5671*56bb7041Schristos * sizeof (PTRACE_XFER_TYPE)),
5672*56bb7041Schristos (PTRACE_TYPE_ARG4) 0);
5673*56bb7041Schristos if (errno)
5674*56bb7041Schristos return errno;
5675*56bb7041Schristos }
5676*56bb7041Schristos
5677*56bb7041Schristos /* Copy data to be written over corresponding part of buffer. */
5678*56bb7041Schristos
5679*56bb7041Schristos memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
5680*56bb7041Schristos myaddr, len);
5681*56bb7041Schristos
5682*56bb7041Schristos /* Write the entire buffer. */
5683*56bb7041Schristos
5684*56bb7041Schristos for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
5685*56bb7041Schristos {
5686*56bb7041Schristos errno = 0;
5687*56bb7041Schristos ptrace (PTRACE_POKETEXT, pid,
5688*56bb7041Schristos /* Coerce to a uintptr_t first to avoid potential gcc warning
5689*56bb7041Schristos about coercing an 8 byte integer to a 4 byte pointer. */
5690*56bb7041Schristos (PTRACE_TYPE_ARG3) (uintptr_t) addr,
5691*56bb7041Schristos (PTRACE_TYPE_ARG4) buffer[i]);
5692*56bb7041Schristos if (errno)
5693*56bb7041Schristos return errno;
5694*56bb7041Schristos }
5695*56bb7041Schristos
5696*56bb7041Schristos return 0;
5697*56bb7041Schristos }
5698*56bb7041Schristos
5699*56bb7041Schristos void
look_up_symbols()5700*56bb7041Schristos linux_process_target::look_up_symbols ()
5701*56bb7041Schristos {
5702*56bb7041Schristos #ifdef USE_THREAD_DB
5703*56bb7041Schristos struct process_info *proc = current_process ();
5704*56bb7041Schristos
5705*56bb7041Schristos if (proc->priv->thread_db != NULL)
5706*56bb7041Schristos return;
5707*56bb7041Schristos
5708*56bb7041Schristos thread_db_init ();
5709*56bb7041Schristos #endif
5710*56bb7041Schristos }
5711*56bb7041Schristos
5712*56bb7041Schristos void
request_interrupt()5713*56bb7041Schristos linux_process_target::request_interrupt ()
5714*56bb7041Schristos {
5715*56bb7041Schristos /* Send a SIGINT to the process group. This acts just like the user
5716*56bb7041Schristos typed a ^C on the controlling terminal. */
5717*56bb7041Schristos ::kill (-signal_pid, SIGINT);
5718*56bb7041Schristos }
5719*56bb7041Schristos
5720*56bb7041Schristos bool
supports_read_auxv()5721*56bb7041Schristos linux_process_target::supports_read_auxv ()
5722*56bb7041Schristos {
5723*56bb7041Schristos return true;
5724*56bb7041Schristos }
5725*56bb7041Schristos
5726*56bb7041Schristos /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5727*56bb7041Schristos to debugger memory starting at MYADDR. */
5728*56bb7041Schristos
5729*56bb7041Schristos int
read_auxv(CORE_ADDR offset,unsigned char * myaddr,unsigned int len)5730*56bb7041Schristos linux_process_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
5731*56bb7041Schristos unsigned int len)
5732*56bb7041Schristos {
5733*56bb7041Schristos char filename[PATH_MAX];
5734*56bb7041Schristos int fd, n;
5735*56bb7041Schristos int pid = lwpid_of (current_thread);
5736*56bb7041Schristos
5737*56bb7041Schristos xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
5738*56bb7041Schristos
5739*56bb7041Schristos fd = open (filename, O_RDONLY);
5740*56bb7041Schristos if (fd < 0)
5741*56bb7041Schristos return -1;
5742*56bb7041Schristos
5743*56bb7041Schristos if (offset != (CORE_ADDR) 0
5744*56bb7041Schristos && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
5745*56bb7041Schristos n = -1;
5746*56bb7041Schristos else
5747*56bb7041Schristos n = read (fd, myaddr, len);
5748*56bb7041Schristos
5749*56bb7041Schristos close (fd);
5750*56bb7041Schristos
5751*56bb7041Schristos return n;
5752*56bb7041Schristos }
5753*56bb7041Schristos
5754*56bb7041Schristos int
insert_point(enum raw_bkpt_type type,CORE_ADDR addr,int size,raw_breakpoint * bp)5755*56bb7041Schristos linux_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
5756*56bb7041Schristos int size, raw_breakpoint *bp)
5757*56bb7041Schristos {
5758*56bb7041Schristos if (type == raw_bkpt_type_sw)
5759*56bb7041Schristos return insert_memory_breakpoint (bp);
5760*56bb7041Schristos else
5761*56bb7041Schristos return low_insert_point (type, addr, size, bp);
5762*56bb7041Schristos }
5763*56bb7041Schristos
5764*56bb7041Schristos int
low_insert_point(raw_bkpt_type type,CORE_ADDR addr,int size,raw_breakpoint * bp)5765*56bb7041Schristos linux_process_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
5766*56bb7041Schristos int size, raw_breakpoint *bp)
5767*56bb7041Schristos {
5768*56bb7041Schristos /* Unsupported (see target.h). */
5769*56bb7041Schristos return 1;
5770*56bb7041Schristos }
5771*56bb7041Schristos
5772*56bb7041Schristos int
remove_point(enum raw_bkpt_type type,CORE_ADDR addr,int size,raw_breakpoint * bp)5773*56bb7041Schristos linux_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
5774*56bb7041Schristos int size, raw_breakpoint *bp)
5775*56bb7041Schristos {
5776*56bb7041Schristos if (type == raw_bkpt_type_sw)
5777*56bb7041Schristos return remove_memory_breakpoint (bp);
5778*56bb7041Schristos else
5779*56bb7041Schristos return low_remove_point (type, addr, size, bp);
5780*56bb7041Schristos }
5781*56bb7041Schristos
5782*56bb7041Schristos int
low_remove_point(raw_bkpt_type type,CORE_ADDR addr,int size,raw_breakpoint * bp)5783*56bb7041Schristos linux_process_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
5784*56bb7041Schristos int size, raw_breakpoint *bp)
5785*56bb7041Schristos {
5786*56bb7041Schristos /* Unsupported (see target.h). */
5787*56bb7041Schristos return 1;
5788*56bb7041Schristos }
5789*56bb7041Schristos
5790*56bb7041Schristos /* Implement the stopped_by_sw_breakpoint target_ops
5791*56bb7041Schristos method. */
5792*56bb7041Schristos
5793*56bb7041Schristos bool
stopped_by_sw_breakpoint()5794*56bb7041Schristos linux_process_target::stopped_by_sw_breakpoint ()
5795*56bb7041Schristos {
5796*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (current_thread);
5797*56bb7041Schristos
5798*56bb7041Schristos return (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT);
5799*56bb7041Schristos }
5800*56bb7041Schristos
5801*56bb7041Schristos /* Implement the supports_stopped_by_sw_breakpoint target_ops
5802*56bb7041Schristos method. */
5803*56bb7041Schristos
5804*56bb7041Schristos bool
supports_stopped_by_sw_breakpoint()5805*56bb7041Schristos linux_process_target::supports_stopped_by_sw_breakpoint ()
5806*56bb7041Schristos {
5807*56bb7041Schristos return USE_SIGTRAP_SIGINFO;
5808*56bb7041Schristos }
5809*56bb7041Schristos
5810*56bb7041Schristos /* Implement the stopped_by_hw_breakpoint target_ops
5811*56bb7041Schristos method. */
5812*56bb7041Schristos
5813*56bb7041Schristos bool
stopped_by_hw_breakpoint()5814*56bb7041Schristos linux_process_target::stopped_by_hw_breakpoint ()
5815*56bb7041Schristos {
5816*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (current_thread);
5817*56bb7041Schristos
5818*56bb7041Schristos return (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT);
5819*56bb7041Schristos }
5820*56bb7041Schristos
5821*56bb7041Schristos /* Implement the supports_stopped_by_hw_breakpoint target_ops
5822*56bb7041Schristos method. */
5823*56bb7041Schristos
5824*56bb7041Schristos bool
supports_stopped_by_hw_breakpoint()5825*56bb7041Schristos linux_process_target::supports_stopped_by_hw_breakpoint ()
5826*56bb7041Schristos {
5827*56bb7041Schristos return USE_SIGTRAP_SIGINFO;
5828*56bb7041Schristos }
5829*56bb7041Schristos
5830*56bb7041Schristos /* Implement the supports_hardware_single_step target_ops method. */
5831*56bb7041Schristos
5832*56bb7041Schristos bool
supports_hardware_single_step()5833*56bb7041Schristos linux_process_target::supports_hardware_single_step ()
5834*56bb7041Schristos {
5835*56bb7041Schristos return true;
5836*56bb7041Schristos }
5837*56bb7041Schristos
5838*56bb7041Schristos bool
stopped_by_watchpoint()5839*56bb7041Schristos linux_process_target::stopped_by_watchpoint ()
5840*56bb7041Schristos {
5841*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (current_thread);
5842*56bb7041Schristos
5843*56bb7041Schristos return lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
5844*56bb7041Schristos }
5845*56bb7041Schristos
5846*56bb7041Schristos CORE_ADDR
stopped_data_address()5847*56bb7041Schristos linux_process_target::stopped_data_address ()
5848*56bb7041Schristos {
5849*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (current_thread);
5850*56bb7041Schristos
5851*56bb7041Schristos return lwp->stopped_data_address;
5852*56bb7041Schristos }
5853*56bb7041Schristos
5854*56bb7041Schristos /* This is only used for targets that define PT_TEXT_ADDR,
5855*56bb7041Schristos PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5856*56bb7041Schristos the target has different ways of acquiring this information, like
5857*56bb7041Schristos loadmaps. */
5858*56bb7041Schristos
5859*56bb7041Schristos bool
supports_read_offsets()5860*56bb7041Schristos linux_process_target::supports_read_offsets ()
5861*56bb7041Schristos {
5862*56bb7041Schristos #ifdef SUPPORTS_READ_OFFSETS
5863*56bb7041Schristos return true;
5864*56bb7041Schristos #else
5865*56bb7041Schristos return false;
5866*56bb7041Schristos #endif
5867*56bb7041Schristos }
5868*56bb7041Schristos
5869*56bb7041Schristos /* Under uClinux, programs are loaded at non-zero offsets, which we need
5870*56bb7041Schristos to tell gdb about. */
5871*56bb7041Schristos
5872*56bb7041Schristos int
read_offsets(CORE_ADDR * text_p,CORE_ADDR * data_p)5873*56bb7041Schristos linux_process_target::read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
5874*56bb7041Schristos {
5875*56bb7041Schristos #ifdef SUPPORTS_READ_OFFSETS
5876*56bb7041Schristos unsigned long text, text_end, data;
5877*56bb7041Schristos int pid = lwpid_of (current_thread);
5878*56bb7041Schristos
5879*56bb7041Schristos errno = 0;
5880*56bb7041Schristos
5881*56bb7041Schristos text = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_ADDR,
5882*56bb7041Schristos (PTRACE_TYPE_ARG4) 0);
5883*56bb7041Schristos text_end = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_END_ADDR,
5884*56bb7041Schristos (PTRACE_TYPE_ARG4) 0);
5885*56bb7041Schristos data = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_DATA_ADDR,
5886*56bb7041Schristos (PTRACE_TYPE_ARG4) 0);
5887*56bb7041Schristos
5888*56bb7041Schristos if (errno == 0)
5889*56bb7041Schristos {
5890*56bb7041Schristos /* Both text and data offsets produced at compile-time (and so
5891*56bb7041Schristos used by gdb) are relative to the beginning of the program,
5892*56bb7041Schristos with the data segment immediately following the text segment.
5893*56bb7041Schristos However, the actual runtime layout in memory may put the data
5894*56bb7041Schristos somewhere else, so when we send gdb a data base-address, we
5895*56bb7041Schristos use the real data base address and subtract the compile-time
5896*56bb7041Schristos data base-address from it (which is just the length of the
5897*56bb7041Schristos text segment). BSS immediately follows data in both
5898*56bb7041Schristos cases. */
5899*56bb7041Schristos *text_p = text;
5900*56bb7041Schristos *data_p = data - (text_end - text);
5901*56bb7041Schristos
5902*56bb7041Schristos return 1;
5903*56bb7041Schristos }
5904*56bb7041Schristos return 0;
5905*56bb7041Schristos #else
5906*56bb7041Schristos gdb_assert_not_reached ("target op read_offsets not supported");
5907*56bb7041Schristos #endif
5908*56bb7041Schristos }
5909*56bb7041Schristos
5910*56bb7041Schristos bool
supports_get_tls_address()5911*56bb7041Schristos linux_process_target::supports_get_tls_address ()
5912*56bb7041Schristos {
5913*56bb7041Schristos #ifdef USE_THREAD_DB
5914*56bb7041Schristos return true;
5915*56bb7041Schristos #else
5916*56bb7041Schristos return false;
5917*56bb7041Schristos #endif
5918*56bb7041Schristos }
5919*56bb7041Schristos
5920*56bb7041Schristos int
get_tls_address(thread_info * thread,CORE_ADDR offset,CORE_ADDR load_module,CORE_ADDR * address)5921*56bb7041Schristos linux_process_target::get_tls_address (thread_info *thread,
5922*56bb7041Schristos CORE_ADDR offset,
5923*56bb7041Schristos CORE_ADDR load_module,
5924*56bb7041Schristos CORE_ADDR *address)
5925*56bb7041Schristos {
5926*56bb7041Schristos #ifdef USE_THREAD_DB
5927*56bb7041Schristos return thread_db_get_tls_address (thread, offset, load_module, address);
5928*56bb7041Schristos #else
5929*56bb7041Schristos return -1;
5930*56bb7041Schristos #endif
5931*56bb7041Schristos }
5932*56bb7041Schristos
5933*56bb7041Schristos bool
supports_qxfer_osdata()5934*56bb7041Schristos linux_process_target::supports_qxfer_osdata ()
5935*56bb7041Schristos {
5936*56bb7041Schristos return true;
5937*56bb7041Schristos }
5938*56bb7041Schristos
5939*56bb7041Schristos int
qxfer_osdata(const char * annex,unsigned char * readbuf,unsigned const char * writebuf,CORE_ADDR offset,int len)5940*56bb7041Schristos linux_process_target::qxfer_osdata (const char *annex,
5941*56bb7041Schristos unsigned char *readbuf,
5942*56bb7041Schristos unsigned const char *writebuf,
5943*56bb7041Schristos CORE_ADDR offset, int len)
5944*56bb7041Schristos {
5945*56bb7041Schristos return linux_common_xfer_osdata (annex, readbuf, offset, len);
5946*56bb7041Schristos }
5947*56bb7041Schristos
5948*56bb7041Schristos void
siginfo_fixup(siginfo_t * siginfo,gdb_byte * inf_siginfo,int direction)5949*56bb7041Schristos linux_process_target::siginfo_fixup (siginfo_t *siginfo,
5950*56bb7041Schristos gdb_byte *inf_siginfo, int direction)
5951*56bb7041Schristos {
5952*56bb7041Schristos bool done = low_siginfo_fixup (siginfo, inf_siginfo, direction);
5953*56bb7041Schristos
5954*56bb7041Schristos /* If there was no callback, or the callback didn't do anything,
5955*56bb7041Schristos then just do a straight memcpy. */
5956*56bb7041Schristos if (!done)
5957*56bb7041Schristos {
5958*56bb7041Schristos if (direction == 1)
5959*56bb7041Schristos memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
5960*56bb7041Schristos else
5961*56bb7041Schristos memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
5962*56bb7041Schristos }
5963*56bb7041Schristos }
5964*56bb7041Schristos
5965*56bb7041Schristos bool
low_siginfo_fixup(siginfo_t * native,gdb_byte * inf,int direction)5966*56bb7041Schristos linux_process_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
5967*56bb7041Schristos int direction)
5968*56bb7041Schristos {
5969*56bb7041Schristos return false;
5970*56bb7041Schristos }
5971*56bb7041Schristos
5972*56bb7041Schristos bool
supports_qxfer_siginfo()5973*56bb7041Schristos linux_process_target::supports_qxfer_siginfo ()
5974*56bb7041Schristos {
5975*56bb7041Schristos return true;
5976*56bb7041Schristos }
5977*56bb7041Schristos
5978*56bb7041Schristos int
qxfer_siginfo(const char * annex,unsigned char * readbuf,unsigned const char * writebuf,CORE_ADDR offset,int len)5979*56bb7041Schristos linux_process_target::qxfer_siginfo (const char *annex,
5980*56bb7041Schristos unsigned char *readbuf,
5981*56bb7041Schristos unsigned const char *writebuf,
5982*56bb7041Schristos CORE_ADDR offset, int len)
5983*56bb7041Schristos {
5984*56bb7041Schristos int pid;
5985*56bb7041Schristos siginfo_t siginfo;
5986*56bb7041Schristos gdb_byte inf_siginfo[sizeof (siginfo_t)];
5987*56bb7041Schristos
5988*56bb7041Schristos if (current_thread == NULL)
5989*56bb7041Schristos return -1;
5990*56bb7041Schristos
5991*56bb7041Schristos pid = lwpid_of (current_thread);
5992*56bb7041Schristos
5993*56bb7041Schristos if (debug_threads)
5994*56bb7041Schristos debug_printf ("%s siginfo for lwp %d.\n",
5995*56bb7041Schristos readbuf != NULL ? "Reading" : "Writing",
5996*56bb7041Schristos pid);
5997*56bb7041Schristos
5998*56bb7041Schristos if (offset >= sizeof (siginfo))
5999*56bb7041Schristos return -1;
6000*56bb7041Schristos
6001*56bb7041Schristos if (ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
6002*56bb7041Schristos return -1;
6003*56bb7041Schristos
6004*56bb7041Schristos /* When GDBSERVER is built as a 64-bit application, ptrace writes into
6005*56bb7041Schristos SIGINFO an object with 64-bit layout. Since debugging a 32-bit
6006*56bb7041Schristos inferior with a 64-bit GDBSERVER should look the same as debugging it
6007*56bb7041Schristos with a 32-bit GDBSERVER, we need to convert it. */
6008*56bb7041Schristos siginfo_fixup (&siginfo, inf_siginfo, 0);
6009*56bb7041Schristos
6010*56bb7041Schristos if (offset + len > sizeof (siginfo))
6011*56bb7041Schristos len = sizeof (siginfo) - offset;
6012*56bb7041Schristos
6013*56bb7041Schristos if (readbuf != NULL)
6014*56bb7041Schristos memcpy (readbuf, inf_siginfo + offset, len);
6015*56bb7041Schristos else
6016*56bb7041Schristos {
6017*56bb7041Schristos memcpy (inf_siginfo + offset, writebuf, len);
6018*56bb7041Schristos
6019*56bb7041Schristos /* Convert back to ptrace layout before flushing it out. */
6020*56bb7041Schristos siginfo_fixup (&siginfo, inf_siginfo, 1);
6021*56bb7041Schristos
6022*56bb7041Schristos if (ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
6023*56bb7041Schristos return -1;
6024*56bb7041Schristos }
6025*56bb7041Schristos
6026*56bb7041Schristos return len;
6027*56bb7041Schristos }
6028*56bb7041Schristos
6029*56bb7041Schristos /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
6030*56bb7041Schristos so we notice when children change state; as the handler for the
6031*56bb7041Schristos sigsuspend in my_waitpid. */
6032*56bb7041Schristos
6033*56bb7041Schristos static void
sigchld_handler(int signo)6034*56bb7041Schristos sigchld_handler (int signo)
6035*56bb7041Schristos {
6036*56bb7041Schristos int old_errno = errno;
6037*56bb7041Schristos
6038*56bb7041Schristos if (debug_threads)
6039*56bb7041Schristos {
6040*56bb7041Schristos do
6041*56bb7041Schristos {
6042*56bb7041Schristos /* Use the async signal safe debug function. */
6043*56bb7041Schristos if (debug_write ("sigchld_handler\n",
6044*56bb7041Schristos sizeof ("sigchld_handler\n") - 1) < 0)
6045*56bb7041Schristos break; /* just ignore */
6046*56bb7041Schristos } while (0);
6047*56bb7041Schristos }
6048*56bb7041Schristos
6049*56bb7041Schristos if (target_is_async_p ())
6050*56bb7041Schristos async_file_mark (); /* trigger a linux_wait */
6051*56bb7041Schristos
6052*56bb7041Schristos errno = old_errno;
6053*56bb7041Schristos }
6054*56bb7041Schristos
6055*56bb7041Schristos bool
supports_non_stop()6056*56bb7041Schristos linux_process_target::supports_non_stop ()
6057*56bb7041Schristos {
6058*56bb7041Schristos return true;
6059*56bb7041Schristos }
6060*56bb7041Schristos
6061*56bb7041Schristos bool
async(bool enable)6062*56bb7041Schristos linux_process_target::async (bool enable)
6063*56bb7041Schristos {
6064*56bb7041Schristos bool previous = target_is_async_p ();
6065*56bb7041Schristos
6066*56bb7041Schristos if (debug_threads)
6067*56bb7041Schristos debug_printf ("linux_async (%d), previous=%d\n",
6068*56bb7041Schristos enable, previous);
6069*56bb7041Schristos
6070*56bb7041Schristos if (previous != enable)
6071*56bb7041Schristos {
6072*56bb7041Schristos sigset_t mask;
6073*56bb7041Schristos sigemptyset (&mask);
6074*56bb7041Schristos sigaddset (&mask, SIGCHLD);
6075*56bb7041Schristos
6076*56bb7041Schristos gdb_sigmask (SIG_BLOCK, &mask, NULL);
6077*56bb7041Schristos
6078*56bb7041Schristos if (enable)
6079*56bb7041Schristos {
6080*56bb7041Schristos if (pipe (linux_event_pipe) == -1)
6081*56bb7041Schristos {
6082*56bb7041Schristos linux_event_pipe[0] = -1;
6083*56bb7041Schristos linux_event_pipe[1] = -1;
6084*56bb7041Schristos gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
6085*56bb7041Schristos
6086*56bb7041Schristos warning ("creating event pipe failed.");
6087*56bb7041Schristos return previous;
6088*56bb7041Schristos }
6089*56bb7041Schristos
6090*56bb7041Schristos fcntl (linux_event_pipe[0], F_SETFL, O_NONBLOCK);
6091*56bb7041Schristos fcntl (linux_event_pipe[1], F_SETFL, O_NONBLOCK);
6092*56bb7041Schristos
6093*56bb7041Schristos /* Register the event loop handler. */
6094*56bb7041Schristos add_file_handler (linux_event_pipe[0],
6095*56bb7041Schristos handle_target_event, NULL);
6096*56bb7041Schristos
6097*56bb7041Schristos /* Always trigger a linux_wait. */
6098*56bb7041Schristos async_file_mark ();
6099*56bb7041Schristos }
6100*56bb7041Schristos else
6101*56bb7041Schristos {
6102*56bb7041Schristos delete_file_handler (linux_event_pipe[0]);
6103*56bb7041Schristos
6104*56bb7041Schristos close (linux_event_pipe[0]);
6105*56bb7041Schristos close (linux_event_pipe[1]);
6106*56bb7041Schristos linux_event_pipe[0] = -1;
6107*56bb7041Schristos linux_event_pipe[1] = -1;
6108*56bb7041Schristos }
6109*56bb7041Schristos
6110*56bb7041Schristos gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
6111*56bb7041Schristos }
6112*56bb7041Schristos
6113*56bb7041Schristos return previous;
6114*56bb7041Schristos }
6115*56bb7041Schristos
6116*56bb7041Schristos int
start_non_stop(bool nonstop)6117*56bb7041Schristos linux_process_target::start_non_stop (bool nonstop)
6118*56bb7041Schristos {
6119*56bb7041Schristos /* Register or unregister from event-loop accordingly. */
6120*56bb7041Schristos target_async (nonstop);
6121*56bb7041Schristos
6122*56bb7041Schristos if (target_is_async_p () != (nonstop != false))
6123*56bb7041Schristos return -1;
6124*56bb7041Schristos
6125*56bb7041Schristos return 0;
6126*56bb7041Schristos }
6127*56bb7041Schristos
6128*56bb7041Schristos bool
supports_multi_process()6129*56bb7041Schristos linux_process_target::supports_multi_process ()
6130*56bb7041Schristos {
6131*56bb7041Schristos return true;
6132*56bb7041Schristos }
6133*56bb7041Schristos
6134*56bb7041Schristos /* Check if fork events are supported. */
6135*56bb7041Schristos
6136*56bb7041Schristos bool
supports_fork_events()6137*56bb7041Schristos linux_process_target::supports_fork_events ()
6138*56bb7041Schristos {
6139*56bb7041Schristos return linux_supports_tracefork ();
6140*56bb7041Schristos }
6141*56bb7041Schristos
6142*56bb7041Schristos /* Check if vfork events are supported. */
6143*56bb7041Schristos
6144*56bb7041Schristos bool
supports_vfork_events()6145*56bb7041Schristos linux_process_target::supports_vfork_events ()
6146*56bb7041Schristos {
6147*56bb7041Schristos return linux_supports_tracefork ();
6148*56bb7041Schristos }
6149*56bb7041Schristos
6150*56bb7041Schristos /* Check if exec events are supported. */
6151*56bb7041Schristos
6152*56bb7041Schristos bool
supports_exec_events()6153*56bb7041Schristos linux_process_target::supports_exec_events ()
6154*56bb7041Schristos {
6155*56bb7041Schristos return linux_supports_traceexec ();
6156*56bb7041Schristos }
6157*56bb7041Schristos
6158*56bb7041Schristos /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
6159*56bb7041Schristos ptrace flags for all inferiors. This is in case the new GDB connection
6160*56bb7041Schristos doesn't support the same set of events that the previous one did. */
6161*56bb7041Schristos
6162*56bb7041Schristos void
handle_new_gdb_connection()6163*56bb7041Schristos linux_process_target::handle_new_gdb_connection ()
6164*56bb7041Schristos {
6165*56bb7041Schristos /* Request that all the lwps reset their ptrace options. */
6166*56bb7041Schristos for_each_thread ([] (thread_info *thread)
6167*56bb7041Schristos {
6168*56bb7041Schristos struct lwp_info *lwp = get_thread_lwp (thread);
6169*56bb7041Schristos
6170*56bb7041Schristos if (!lwp->stopped)
6171*56bb7041Schristos {
6172*56bb7041Schristos /* Stop the lwp so we can modify its ptrace options. */
6173*56bb7041Schristos lwp->must_set_ptrace_flags = 1;
6174*56bb7041Schristos linux_stop_lwp (lwp);
6175*56bb7041Schristos }
6176*56bb7041Schristos else
6177*56bb7041Schristos {
6178*56bb7041Schristos /* Already stopped; go ahead and set the ptrace options. */
6179*56bb7041Schristos struct process_info *proc = find_process_pid (pid_of (thread));
6180*56bb7041Schristos int options = linux_low_ptrace_options (proc->attached);
6181*56bb7041Schristos
6182*56bb7041Schristos linux_enable_event_reporting (lwpid_of (thread), options);
6183*56bb7041Schristos lwp->must_set_ptrace_flags = 0;
6184*56bb7041Schristos }
6185*56bb7041Schristos });
6186*56bb7041Schristos }
6187*56bb7041Schristos
6188*56bb7041Schristos int
handle_monitor_command(char * mon)6189*56bb7041Schristos linux_process_target::handle_monitor_command (char *mon)
6190*56bb7041Schristos {
6191*56bb7041Schristos #ifdef USE_THREAD_DB
6192*56bb7041Schristos return thread_db_handle_monitor_command (mon);
6193*56bb7041Schristos #else
6194*56bb7041Schristos return 0;
6195*56bb7041Schristos #endif
6196*56bb7041Schristos }
6197*56bb7041Schristos
6198*56bb7041Schristos int
core_of_thread(ptid_t ptid)6199*56bb7041Schristos linux_process_target::core_of_thread (ptid_t ptid)
6200*56bb7041Schristos {
6201*56bb7041Schristos return linux_common_core_of_thread (ptid);
6202*56bb7041Schristos }
6203*56bb7041Schristos
6204*56bb7041Schristos bool
supports_disable_randomization()6205*56bb7041Schristos linux_process_target::supports_disable_randomization ()
6206*56bb7041Schristos {
6207*56bb7041Schristos #ifdef HAVE_PERSONALITY
6208*56bb7041Schristos return true;
6209*56bb7041Schristos #else
6210*56bb7041Schristos return false;
6211*56bb7041Schristos #endif
6212*56bb7041Schristos }
6213*56bb7041Schristos
6214*56bb7041Schristos bool
supports_agent()6215*56bb7041Schristos linux_process_target::supports_agent ()
6216*56bb7041Schristos {
6217*56bb7041Schristos return true;
6218*56bb7041Schristos }
6219*56bb7041Schristos
6220*56bb7041Schristos bool
supports_range_stepping()6221*56bb7041Schristos linux_process_target::supports_range_stepping ()
6222*56bb7041Schristos {
6223*56bb7041Schristos if (supports_software_single_step ())
6224*56bb7041Schristos return true;
6225*56bb7041Schristos
6226*56bb7041Schristos return low_supports_range_stepping ();
6227*56bb7041Schristos }
6228*56bb7041Schristos
6229*56bb7041Schristos bool
low_supports_range_stepping()6230*56bb7041Schristos linux_process_target::low_supports_range_stepping ()
6231*56bb7041Schristos {
6232*56bb7041Schristos return false;
6233*56bb7041Schristos }
6234*56bb7041Schristos
6235*56bb7041Schristos bool
supports_pid_to_exec_file()6236*56bb7041Schristos linux_process_target::supports_pid_to_exec_file ()
6237*56bb7041Schristos {
6238*56bb7041Schristos return true;
6239*56bb7041Schristos }
6240*56bb7041Schristos
6241*56bb7041Schristos char *
pid_to_exec_file(int pid)6242*56bb7041Schristos linux_process_target::pid_to_exec_file (int pid)
6243*56bb7041Schristos {
6244*56bb7041Schristos return linux_proc_pid_to_exec_file (pid);
6245*56bb7041Schristos }
6246*56bb7041Schristos
6247*56bb7041Schristos bool
supports_multifs()6248*56bb7041Schristos linux_process_target::supports_multifs ()
6249*56bb7041Schristos {
6250*56bb7041Schristos return true;
6251*56bb7041Schristos }
6252*56bb7041Schristos
6253*56bb7041Schristos int
multifs_open(int pid,const char * filename,int flags,mode_t mode)6254*56bb7041Schristos linux_process_target::multifs_open (int pid, const char *filename,
6255*56bb7041Schristos int flags, mode_t mode)
6256*56bb7041Schristos {
6257*56bb7041Schristos return linux_mntns_open_cloexec (pid, filename, flags, mode);
6258*56bb7041Schristos }
6259*56bb7041Schristos
6260*56bb7041Schristos int
multifs_unlink(int pid,const char * filename)6261*56bb7041Schristos linux_process_target::multifs_unlink (int pid, const char *filename)
6262*56bb7041Schristos {
6263*56bb7041Schristos return linux_mntns_unlink (pid, filename);
6264*56bb7041Schristos }
6265*56bb7041Schristos
6266*56bb7041Schristos ssize_t
multifs_readlink(int pid,const char * filename,char * buf,size_t bufsiz)6267*56bb7041Schristos linux_process_target::multifs_readlink (int pid, const char *filename,
6268*56bb7041Schristos char *buf, size_t bufsiz)
6269*56bb7041Schristos {
6270*56bb7041Schristos return linux_mntns_readlink (pid, filename, buf, bufsiz);
6271*56bb7041Schristos }
6272*56bb7041Schristos
6273*56bb7041Schristos #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
6274*56bb7041Schristos struct target_loadseg
6275*56bb7041Schristos {
6276*56bb7041Schristos /* Core address to which the segment is mapped. */
6277*56bb7041Schristos Elf32_Addr addr;
6278*56bb7041Schristos /* VMA recorded in the program header. */
6279*56bb7041Schristos Elf32_Addr p_vaddr;
6280*56bb7041Schristos /* Size of this segment in memory. */
6281*56bb7041Schristos Elf32_Word p_memsz;
6282*56bb7041Schristos };
6283*56bb7041Schristos
6284*56bb7041Schristos # if defined PT_GETDSBT
6285*56bb7041Schristos struct target_loadmap
6286*56bb7041Schristos {
6287*56bb7041Schristos /* Protocol version number, must be zero. */
6288*56bb7041Schristos Elf32_Word version;
6289*56bb7041Schristos /* Pointer to the DSBT table, its size, and the DSBT index. */
6290*56bb7041Schristos unsigned *dsbt_table;
6291*56bb7041Schristos unsigned dsbt_size, dsbt_index;
6292*56bb7041Schristos /* Number of segments in this map. */
6293*56bb7041Schristos Elf32_Word nsegs;
6294*56bb7041Schristos /* The actual memory map. */
6295*56bb7041Schristos struct target_loadseg segs[/*nsegs*/];
6296*56bb7041Schristos };
6297*56bb7041Schristos # define LINUX_LOADMAP PT_GETDSBT
6298*56bb7041Schristos # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6299*56bb7041Schristos # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6300*56bb7041Schristos # else
6301*56bb7041Schristos struct target_loadmap
6302*56bb7041Schristos {
6303*56bb7041Schristos /* Protocol version number, must be zero. */
6304*56bb7041Schristos Elf32_Half version;
6305*56bb7041Schristos /* Number of segments in this map. */
6306*56bb7041Schristos Elf32_Half nsegs;
6307*56bb7041Schristos /* The actual memory map. */
6308*56bb7041Schristos struct target_loadseg segs[/*nsegs*/];
6309*56bb7041Schristos };
6310*56bb7041Schristos # define LINUX_LOADMAP PTRACE_GETFDPIC
6311*56bb7041Schristos # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6312*56bb7041Schristos # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6313*56bb7041Schristos # endif
6314*56bb7041Schristos
6315*56bb7041Schristos bool
supports_read_loadmap()6316*56bb7041Schristos linux_process_target::supports_read_loadmap ()
6317*56bb7041Schristos {
6318*56bb7041Schristos return true;
6319*56bb7041Schristos }
6320*56bb7041Schristos
6321*56bb7041Schristos int
read_loadmap(const char * annex,CORE_ADDR offset,unsigned char * myaddr,unsigned int len)6322*56bb7041Schristos linux_process_target::read_loadmap (const char *annex, CORE_ADDR offset,
6323*56bb7041Schristos unsigned char *myaddr, unsigned int len)
6324*56bb7041Schristos {
6325*56bb7041Schristos int pid = lwpid_of (current_thread);
6326*56bb7041Schristos int addr = -1;
6327*56bb7041Schristos struct target_loadmap *data = NULL;
6328*56bb7041Schristos unsigned int actual_length, copy_length;
6329*56bb7041Schristos
6330*56bb7041Schristos if (strcmp (annex, "exec") == 0)
6331*56bb7041Schristos addr = (int) LINUX_LOADMAP_EXEC;
6332*56bb7041Schristos else if (strcmp (annex, "interp") == 0)
6333*56bb7041Schristos addr = (int) LINUX_LOADMAP_INTERP;
6334*56bb7041Schristos else
6335*56bb7041Schristos return -1;
6336*56bb7041Schristos
6337*56bb7041Schristos if (ptrace (LINUX_LOADMAP, pid, addr, &data) != 0)
6338*56bb7041Schristos return -1;
6339*56bb7041Schristos
6340*56bb7041Schristos if (data == NULL)
6341*56bb7041Schristos return -1;
6342*56bb7041Schristos
6343*56bb7041Schristos actual_length = sizeof (struct target_loadmap)
6344*56bb7041Schristos + sizeof (struct target_loadseg) * data->nsegs;
6345*56bb7041Schristos
6346*56bb7041Schristos if (offset < 0 || offset > actual_length)
6347*56bb7041Schristos return -1;
6348*56bb7041Schristos
6349*56bb7041Schristos copy_length = actual_length - offset < len ? actual_length - offset : len;
6350*56bb7041Schristos memcpy (myaddr, (char *) data + offset, copy_length);
6351*56bb7041Schristos return copy_length;
6352*56bb7041Schristos }
6353*56bb7041Schristos #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6354*56bb7041Schristos
6355*56bb7041Schristos bool
supports_catch_syscall()6356*56bb7041Schristos linux_process_target::supports_catch_syscall ()
6357*56bb7041Schristos {
6358*56bb7041Schristos return (low_supports_catch_syscall ()
6359*56bb7041Schristos && linux_supports_tracesysgood ());
6360*56bb7041Schristos }
6361*56bb7041Schristos
6362*56bb7041Schristos bool
low_supports_catch_syscall()6363*56bb7041Schristos linux_process_target::low_supports_catch_syscall ()
6364*56bb7041Schristos {
6365*56bb7041Schristos return false;
6366*56bb7041Schristos }
6367*56bb7041Schristos
6368*56bb7041Schristos CORE_ADDR
read_pc(regcache * regcache)6369*56bb7041Schristos linux_process_target::read_pc (regcache *regcache)
6370*56bb7041Schristos {
6371*56bb7041Schristos if (!low_supports_breakpoints ())
6372*56bb7041Schristos return 0;
6373*56bb7041Schristos
6374*56bb7041Schristos return low_get_pc (regcache);
6375*56bb7041Schristos }
6376*56bb7041Schristos
6377*56bb7041Schristos void
write_pc(regcache * regcache,CORE_ADDR pc)6378*56bb7041Schristos linux_process_target::write_pc (regcache *regcache, CORE_ADDR pc)
6379*56bb7041Schristos {
6380*56bb7041Schristos gdb_assert (low_supports_breakpoints ());
6381*56bb7041Schristos
6382*56bb7041Schristos low_set_pc (regcache, pc);
6383*56bb7041Schristos }
6384*56bb7041Schristos
6385*56bb7041Schristos bool
supports_thread_stopped()6386*56bb7041Schristos linux_process_target::supports_thread_stopped ()
6387*56bb7041Schristos {
6388*56bb7041Schristos return true;
6389*56bb7041Schristos }
6390*56bb7041Schristos
6391*56bb7041Schristos bool
thread_stopped(thread_info * thread)6392*56bb7041Schristos linux_process_target::thread_stopped (thread_info *thread)
6393*56bb7041Schristos {
6394*56bb7041Schristos return get_thread_lwp (thread)->stopped;
6395*56bb7041Schristos }
6396*56bb7041Schristos
6397*56bb7041Schristos /* This exposes stop-all-threads functionality to other modules. */
6398*56bb7041Schristos
6399*56bb7041Schristos void
pause_all(bool freeze)6400*56bb7041Schristos linux_process_target::pause_all (bool freeze)
6401*56bb7041Schristos {
6402*56bb7041Schristos stop_all_lwps (freeze, NULL);
6403*56bb7041Schristos }
6404*56bb7041Schristos
6405*56bb7041Schristos /* This exposes unstop-all-threads functionality to other gdbserver
6406*56bb7041Schristos modules. */
6407*56bb7041Schristos
6408*56bb7041Schristos void
unpause_all(bool unfreeze)6409*56bb7041Schristos linux_process_target::unpause_all (bool unfreeze)
6410*56bb7041Schristos {
6411*56bb7041Schristos unstop_all_lwps (unfreeze, NULL);
6412*56bb7041Schristos }
6413*56bb7041Schristos
6414*56bb7041Schristos int
prepare_to_access_memory()6415*56bb7041Schristos linux_process_target::prepare_to_access_memory ()
6416*56bb7041Schristos {
6417*56bb7041Schristos /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6418*56bb7041Schristos running LWP. */
6419*56bb7041Schristos if (non_stop)
6420*56bb7041Schristos target_pause_all (true);
6421*56bb7041Schristos return 0;
6422*56bb7041Schristos }
6423*56bb7041Schristos
6424*56bb7041Schristos void
done_accessing_memory()6425*56bb7041Schristos linux_process_target::done_accessing_memory ()
6426*56bb7041Schristos {
6427*56bb7041Schristos /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6428*56bb7041Schristos running LWP. */
6429*56bb7041Schristos if (non_stop)
6430*56bb7041Schristos target_unpause_all (true);
6431*56bb7041Schristos }
6432*56bb7041Schristos
6433*56bb7041Schristos /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6434*56bb7041Schristos
6435*56bb7041Schristos static int
get_phdr_phnum_from_proc_auxv(const int pid,const int is_elf64,CORE_ADDR * phdr_memaddr,int * num_phdr)6436*56bb7041Schristos get_phdr_phnum_from_proc_auxv (const int pid, const int is_elf64,
6437*56bb7041Schristos CORE_ADDR *phdr_memaddr, int *num_phdr)
6438*56bb7041Schristos {
6439*56bb7041Schristos char filename[PATH_MAX];
6440*56bb7041Schristos int fd;
6441*56bb7041Schristos const int auxv_size = is_elf64
6442*56bb7041Schristos ? sizeof (Elf64_auxv_t) : sizeof (Elf32_auxv_t);
6443*56bb7041Schristos char buf[sizeof (Elf64_auxv_t)]; /* The larger of the two. */
6444*56bb7041Schristos
6445*56bb7041Schristos xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
6446*56bb7041Schristos
6447*56bb7041Schristos fd = open (filename, O_RDONLY);
6448*56bb7041Schristos if (fd < 0)
6449*56bb7041Schristos return 1;
6450*56bb7041Schristos
6451*56bb7041Schristos *phdr_memaddr = 0;
6452*56bb7041Schristos *num_phdr = 0;
6453*56bb7041Schristos while (read (fd, buf, auxv_size) == auxv_size
6454*56bb7041Schristos && (*phdr_memaddr == 0 || *num_phdr == 0))
6455*56bb7041Schristos {
6456*56bb7041Schristos if (is_elf64)
6457*56bb7041Schristos {
6458*56bb7041Schristos Elf64_auxv_t *const aux = (Elf64_auxv_t *) buf;
6459*56bb7041Schristos
6460*56bb7041Schristos switch (aux->a_type)
6461*56bb7041Schristos {
6462*56bb7041Schristos case AT_PHDR:
6463*56bb7041Schristos *phdr_memaddr = aux->a_un.a_val;
6464*56bb7041Schristos break;
6465*56bb7041Schristos case AT_PHNUM:
6466*56bb7041Schristos *num_phdr = aux->a_un.a_val;
6467*56bb7041Schristos break;
6468*56bb7041Schristos }
6469*56bb7041Schristos }
6470*56bb7041Schristos else
6471*56bb7041Schristos {
6472*56bb7041Schristos Elf32_auxv_t *const aux = (Elf32_auxv_t *) buf;
6473*56bb7041Schristos
6474*56bb7041Schristos switch (aux->a_type)
6475*56bb7041Schristos {
6476*56bb7041Schristos case AT_PHDR:
6477*56bb7041Schristos *phdr_memaddr = aux->a_un.a_val;
6478*56bb7041Schristos break;
6479*56bb7041Schristos case AT_PHNUM:
6480*56bb7041Schristos *num_phdr = aux->a_un.a_val;
6481*56bb7041Schristos break;
6482*56bb7041Schristos }
6483*56bb7041Schristos }
6484*56bb7041Schristos }
6485*56bb7041Schristos
6486*56bb7041Schristos close (fd);
6487*56bb7041Schristos
6488*56bb7041Schristos if (*phdr_memaddr == 0 || *num_phdr == 0)
6489*56bb7041Schristos {
6490*56bb7041Schristos warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6491*56bb7041Schristos "phdr_memaddr = %ld, phdr_num = %d",
6492*56bb7041Schristos (long) *phdr_memaddr, *num_phdr);
6493*56bb7041Schristos return 2;
6494*56bb7041Schristos }
6495*56bb7041Schristos
6496*56bb7041Schristos return 0;
6497*56bb7041Schristos }
6498*56bb7041Schristos
6499*56bb7041Schristos /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6500*56bb7041Schristos
6501*56bb7041Schristos static CORE_ADDR
get_dynamic(const int pid,const int is_elf64)6502*56bb7041Schristos get_dynamic (const int pid, const int is_elf64)
6503*56bb7041Schristos {
6504*56bb7041Schristos CORE_ADDR phdr_memaddr, relocation;
6505*56bb7041Schristos int num_phdr, i;
6506*56bb7041Schristos unsigned char *phdr_buf;
6507*56bb7041Schristos const int phdr_size = is_elf64 ? sizeof (Elf64_Phdr) : sizeof (Elf32_Phdr);
6508*56bb7041Schristos
6509*56bb7041Schristos if (get_phdr_phnum_from_proc_auxv (pid, is_elf64, &phdr_memaddr, &num_phdr))
6510*56bb7041Schristos return 0;
6511*56bb7041Schristos
6512*56bb7041Schristos gdb_assert (num_phdr < 100); /* Basic sanity check. */
6513*56bb7041Schristos phdr_buf = (unsigned char *) alloca (num_phdr * phdr_size);
6514*56bb7041Schristos
6515*56bb7041Schristos if (linux_read_memory (phdr_memaddr, phdr_buf, num_phdr * phdr_size))
6516*56bb7041Schristos return 0;
6517*56bb7041Schristos
6518*56bb7041Schristos /* Compute relocation: it is expected to be 0 for "regular" executables,
6519*56bb7041Schristos non-zero for PIE ones. */
6520*56bb7041Schristos relocation = -1;
6521*56bb7041Schristos for (i = 0; relocation == -1 && i < num_phdr; i++)
6522*56bb7041Schristos if (is_elf64)
6523*56bb7041Schristos {
6524*56bb7041Schristos Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
6525*56bb7041Schristos
6526*56bb7041Schristos if (p->p_type == PT_PHDR)
6527*56bb7041Schristos relocation = phdr_memaddr - p->p_vaddr;
6528*56bb7041Schristos }
6529*56bb7041Schristos else
6530*56bb7041Schristos {
6531*56bb7041Schristos Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
6532*56bb7041Schristos
6533*56bb7041Schristos if (p->p_type == PT_PHDR)
6534*56bb7041Schristos relocation = phdr_memaddr - p->p_vaddr;
6535*56bb7041Schristos }
6536*56bb7041Schristos
6537*56bb7041Schristos if (relocation == -1)
6538*56bb7041Schristos {
6539*56bb7041Schristos /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6540*56bb7041Schristos any real world executables, including PIE executables, have always
6541*56bb7041Schristos PT_PHDR present. PT_PHDR is not present in some shared libraries or
6542*56bb7041Schristos in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6543*56bb7041Schristos or present DT_DEBUG anyway (fpc binaries are statically linked).
6544*56bb7041Schristos
6545*56bb7041Schristos Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6546*56bb7041Schristos
6547*56bb7041Schristos GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6548*56bb7041Schristos
6549*56bb7041Schristos return 0;
6550*56bb7041Schristos }
6551*56bb7041Schristos
6552*56bb7041Schristos for (i = 0; i < num_phdr; i++)
6553*56bb7041Schristos {
6554*56bb7041Schristos if (is_elf64)
6555*56bb7041Schristos {
6556*56bb7041Schristos Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
6557*56bb7041Schristos
6558*56bb7041Schristos if (p->p_type == PT_DYNAMIC)
6559*56bb7041Schristos return p->p_vaddr + relocation;
6560*56bb7041Schristos }
6561*56bb7041Schristos else
6562*56bb7041Schristos {
6563*56bb7041Schristos Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
6564*56bb7041Schristos
6565*56bb7041Schristos if (p->p_type == PT_DYNAMIC)
6566*56bb7041Schristos return p->p_vaddr + relocation;
6567*56bb7041Schristos }
6568*56bb7041Schristos }
6569*56bb7041Schristos
6570*56bb7041Schristos return 0;
6571*56bb7041Schristos }
6572*56bb7041Schristos
6573*56bb7041Schristos /* Return &_r_debug in the inferior, or -1 if not present. Return value
6574*56bb7041Schristos can be 0 if the inferior does not yet have the library list initialized.
6575*56bb7041Schristos We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6576*56bb7041Schristos DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6577*56bb7041Schristos
6578*56bb7041Schristos static CORE_ADDR
get_r_debug(const int pid,const int is_elf64)6579*56bb7041Schristos get_r_debug (const int pid, const int is_elf64)
6580*56bb7041Schristos {
6581*56bb7041Schristos CORE_ADDR dynamic_memaddr;
6582*56bb7041Schristos const int dyn_size = is_elf64 ? sizeof (Elf64_Dyn) : sizeof (Elf32_Dyn);
6583*56bb7041Schristos unsigned char buf[sizeof (Elf64_Dyn)]; /* The larger of the two. */
6584*56bb7041Schristos CORE_ADDR map = -1;
6585*56bb7041Schristos
6586*56bb7041Schristos dynamic_memaddr = get_dynamic (pid, is_elf64);
6587*56bb7041Schristos if (dynamic_memaddr == 0)
6588*56bb7041Schristos return map;
6589*56bb7041Schristos
6590*56bb7041Schristos while (linux_read_memory (dynamic_memaddr, buf, dyn_size) == 0)
6591*56bb7041Schristos {
6592*56bb7041Schristos if (is_elf64)
6593*56bb7041Schristos {
6594*56bb7041Schristos Elf64_Dyn *const dyn = (Elf64_Dyn *) buf;
6595*56bb7041Schristos #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6596*56bb7041Schristos union
6597*56bb7041Schristos {
6598*56bb7041Schristos Elf64_Xword map;
6599*56bb7041Schristos unsigned char buf[sizeof (Elf64_Xword)];
6600*56bb7041Schristos }
6601*56bb7041Schristos rld_map;
6602*56bb7041Schristos #endif
6603*56bb7041Schristos #ifdef DT_MIPS_RLD_MAP
6604*56bb7041Schristos if (dyn->d_tag == DT_MIPS_RLD_MAP)
6605*56bb7041Schristos {
6606*56bb7041Schristos if (linux_read_memory (dyn->d_un.d_val,
6607*56bb7041Schristos rld_map.buf, sizeof (rld_map.buf)) == 0)
6608*56bb7041Schristos return rld_map.map;
6609*56bb7041Schristos else
6610*56bb7041Schristos break;
6611*56bb7041Schristos }
6612*56bb7041Schristos #endif /* DT_MIPS_RLD_MAP */
6613*56bb7041Schristos #ifdef DT_MIPS_RLD_MAP_REL
6614*56bb7041Schristos if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
6615*56bb7041Schristos {
6616*56bb7041Schristos if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
6617*56bb7041Schristos rld_map.buf, sizeof (rld_map.buf)) == 0)
6618*56bb7041Schristos return rld_map.map;
6619*56bb7041Schristos else
6620*56bb7041Schristos break;
6621*56bb7041Schristos }
6622*56bb7041Schristos #endif /* DT_MIPS_RLD_MAP_REL */
6623*56bb7041Schristos
6624*56bb7041Schristos if (dyn->d_tag == DT_DEBUG && map == -1)
6625*56bb7041Schristos map = dyn->d_un.d_val;
6626*56bb7041Schristos
6627*56bb7041Schristos if (dyn->d_tag == DT_NULL)
6628*56bb7041Schristos break;
6629*56bb7041Schristos }
6630*56bb7041Schristos else
6631*56bb7041Schristos {
6632*56bb7041Schristos Elf32_Dyn *const dyn = (Elf32_Dyn *) buf;
6633*56bb7041Schristos #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6634*56bb7041Schristos union
6635*56bb7041Schristos {
6636*56bb7041Schristos Elf32_Word map;
6637*56bb7041Schristos unsigned char buf[sizeof (Elf32_Word)];
6638*56bb7041Schristos }
6639*56bb7041Schristos rld_map;
6640*56bb7041Schristos #endif
6641*56bb7041Schristos #ifdef DT_MIPS_RLD_MAP
6642*56bb7041Schristos if (dyn->d_tag == DT_MIPS_RLD_MAP)
6643*56bb7041Schristos {
6644*56bb7041Schristos if (linux_read_memory (dyn->d_un.d_val,
6645*56bb7041Schristos rld_map.buf, sizeof (rld_map.buf)) == 0)
6646*56bb7041Schristos return rld_map.map;
6647*56bb7041Schristos else
6648*56bb7041Schristos break;
6649*56bb7041Schristos }
6650*56bb7041Schristos #endif /* DT_MIPS_RLD_MAP */
6651*56bb7041Schristos #ifdef DT_MIPS_RLD_MAP_REL
6652*56bb7041Schristos if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
6653*56bb7041Schristos {
6654*56bb7041Schristos if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
6655*56bb7041Schristos rld_map.buf, sizeof (rld_map.buf)) == 0)
6656*56bb7041Schristos return rld_map.map;
6657*56bb7041Schristos else
6658*56bb7041Schristos break;
6659*56bb7041Schristos }
6660*56bb7041Schristos #endif /* DT_MIPS_RLD_MAP_REL */
6661*56bb7041Schristos
6662*56bb7041Schristos if (dyn->d_tag == DT_DEBUG && map == -1)
6663*56bb7041Schristos map = dyn->d_un.d_val;
6664*56bb7041Schristos
6665*56bb7041Schristos if (dyn->d_tag == DT_NULL)
6666*56bb7041Schristos break;
6667*56bb7041Schristos }
6668*56bb7041Schristos
6669*56bb7041Schristos dynamic_memaddr += dyn_size;
6670*56bb7041Schristos }
6671*56bb7041Schristos
6672*56bb7041Schristos return map;
6673*56bb7041Schristos }
6674*56bb7041Schristos
6675*56bb7041Schristos /* Read one pointer from MEMADDR in the inferior. */
6676*56bb7041Schristos
6677*56bb7041Schristos static int
read_one_ptr(CORE_ADDR memaddr,CORE_ADDR * ptr,int ptr_size)6678*56bb7041Schristos read_one_ptr (CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size)
6679*56bb7041Schristos {
6680*56bb7041Schristos int ret;
6681*56bb7041Schristos
6682*56bb7041Schristos /* Go through a union so this works on either big or little endian
6683*56bb7041Schristos hosts, when the inferior's pointer size is smaller than the size
6684*56bb7041Schristos of CORE_ADDR. It is assumed the inferior's endianness is the
6685*56bb7041Schristos same of the superior's. */
6686*56bb7041Schristos union
6687*56bb7041Schristos {
6688*56bb7041Schristos CORE_ADDR core_addr;
6689*56bb7041Schristos unsigned int ui;
6690*56bb7041Schristos unsigned char uc;
6691*56bb7041Schristos } addr;
6692*56bb7041Schristos
6693*56bb7041Schristos ret = linux_read_memory (memaddr, &addr.uc, ptr_size);
6694*56bb7041Schristos if (ret == 0)
6695*56bb7041Schristos {
6696*56bb7041Schristos if (ptr_size == sizeof (CORE_ADDR))
6697*56bb7041Schristos *ptr = addr.core_addr;
6698*56bb7041Schristos else if (ptr_size == sizeof (unsigned int))
6699*56bb7041Schristos *ptr = addr.ui;
6700*56bb7041Schristos else
6701*56bb7041Schristos gdb_assert_not_reached ("unhandled pointer size");
6702*56bb7041Schristos }
6703*56bb7041Schristos return ret;
6704*56bb7041Schristos }
6705*56bb7041Schristos
6706*56bb7041Schristos bool
supports_qxfer_libraries_svr4()6707*56bb7041Schristos linux_process_target::supports_qxfer_libraries_svr4 ()
6708*56bb7041Schristos {
6709*56bb7041Schristos return true;
6710*56bb7041Schristos }
6711*56bb7041Schristos
6712*56bb7041Schristos struct link_map_offsets
6713*56bb7041Schristos {
6714*56bb7041Schristos /* Offset and size of r_debug.r_version. */
6715*56bb7041Schristos int r_version_offset;
6716*56bb7041Schristos
6717*56bb7041Schristos /* Offset and size of r_debug.r_map. */
6718*56bb7041Schristos int r_map_offset;
6719*56bb7041Schristos
6720*56bb7041Schristos /* Offset to l_addr field in struct link_map. */
6721*56bb7041Schristos int l_addr_offset;
6722*56bb7041Schristos
6723*56bb7041Schristos /* Offset to l_name field in struct link_map. */
6724*56bb7041Schristos int l_name_offset;
6725*56bb7041Schristos
6726*56bb7041Schristos /* Offset to l_ld field in struct link_map. */
6727*56bb7041Schristos int l_ld_offset;
6728*56bb7041Schristos
6729*56bb7041Schristos /* Offset to l_next field in struct link_map. */
6730*56bb7041Schristos int l_next_offset;
6731*56bb7041Schristos
6732*56bb7041Schristos /* Offset to l_prev field in struct link_map. */
6733*56bb7041Schristos int l_prev_offset;
6734*56bb7041Schristos };
6735*56bb7041Schristos
6736*56bb7041Schristos /* Construct qXfer:libraries-svr4:read reply. */
6737*56bb7041Schristos
6738*56bb7041Schristos int
qxfer_libraries_svr4(const char * annex,unsigned char * readbuf,unsigned const char * writebuf,CORE_ADDR offset,int len)6739*56bb7041Schristos linux_process_target::qxfer_libraries_svr4 (const char *annex,
6740*56bb7041Schristos unsigned char *readbuf,
6741*56bb7041Schristos unsigned const char *writebuf,
6742*56bb7041Schristos CORE_ADDR offset, int len)
6743*56bb7041Schristos {
6744*56bb7041Schristos struct process_info_private *const priv = current_process ()->priv;
6745*56bb7041Schristos char filename[PATH_MAX];
6746*56bb7041Schristos int pid, is_elf64;
6747*56bb7041Schristos
6748*56bb7041Schristos static const struct link_map_offsets lmo_32bit_offsets =
6749*56bb7041Schristos {
6750*56bb7041Schristos 0, /* r_version offset. */
6751*56bb7041Schristos 4, /* r_debug.r_map offset. */
6752*56bb7041Schristos 0, /* l_addr offset in link_map. */
6753*56bb7041Schristos 4, /* l_name offset in link_map. */
6754*56bb7041Schristos 8, /* l_ld offset in link_map. */
6755*56bb7041Schristos 12, /* l_next offset in link_map. */
6756*56bb7041Schristos 16 /* l_prev offset in link_map. */
6757*56bb7041Schristos };
6758*56bb7041Schristos
6759*56bb7041Schristos static const struct link_map_offsets lmo_64bit_offsets =
6760*56bb7041Schristos {
6761*56bb7041Schristos 0, /* r_version offset. */
6762*56bb7041Schristos 8, /* r_debug.r_map offset. */
6763*56bb7041Schristos 0, /* l_addr offset in link_map. */
6764*56bb7041Schristos 8, /* l_name offset in link_map. */
6765*56bb7041Schristos 16, /* l_ld offset in link_map. */
6766*56bb7041Schristos 24, /* l_next offset in link_map. */
6767*56bb7041Schristos 32 /* l_prev offset in link_map. */
6768*56bb7041Schristos };
6769*56bb7041Schristos const struct link_map_offsets *lmo;
6770*56bb7041Schristos unsigned int machine;
6771*56bb7041Schristos int ptr_size;
6772*56bb7041Schristos CORE_ADDR lm_addr = 0, lm_prev = 0;
6773*56bb7041Schristos CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
6774*56bb7041Schristos int header_done = 0;
6775*56bb7041Schristos
6776*56bb7041Schristos if (writebuf != NULL)
6777*56bb7041Schristos return -2;
6778*56bb7041Schristos if (readbuf == NULL)
6779*56bb7041Schristos return -1;
6780*56bb7041Schristos
6781*56bb7041Schristos pid = lwpid_of (current_thread);
6782*56bb7041Schristos xsnprintf (filename, sizeof filename, "/proc/%d/exe", pid);
6783*56bb7041Schristos is_elf64 = elf_64_file_p (filename, &machine);
6784*56bb7041Schristos lmo = is_elf64 ? &lmo_64bit_offsets : &lmo_32bit_offsets;
6785*56bb7041Schristos ptr_size = is_elf64 ? 8 : 4;
6786*56bb7041Schristos
6787*56bb7041Schristos while (annex[0] != '\0')
6788*56bb7041Schristos {
6789*56bb7041Schristos const char *sep;
6790*56bb7041Schristos CORE_ADDR *addrp;
6791*56bb7041Schristos int name_len;
6792*56bb7041Schristos
6793*56bb7041Schristos sep = strchr (annex, '=');
6794*56bb7041Schristos if (sep == NULL)
6795*56bb7041Schristos break;
6796*56bb7041Schristos
6797*56bb7041Schristos name_len = sep - annex;
6798*56bb7041Schristos if (name_len == 5 && startswith (annex, "start"))
6799*56bb7041Schristos addrp = &lm_addr;
6800*56bb7041Schristos else if (name_len == 4 && startswith (annex, "prev"))
6801*56bb7041Schristos addrp = &lm_prev;
6802*56bb7041Schristos else
6803*56bb7041Schristos {
6804*56bb7041Schristos annex = strchr (sep, ';');
6805*56bb7041Schristos if (annex == NULL)
6806*56bb7041Schristos break;
6807*56bb7041Schristos annex++;
6808*56bb7041Schristos continue;
6809*56bb7041Schristos }
6810*56bb7041Schristos
6811*56bb7041Schristos annex = decode_address_to_semicolon (addrp, sep + 1);
6812*56bb7041Schristos }
6813*56bb7041Schristos
6814*56bb7041Schristos if (lm_addr == 0)
6815*56bb7041Schristos {
6816*56bb7041Schristos int r_version = 0;
6817*56bb7041Schristos
6818*56bb7041Schristos if (priv->r_debug == 0)
6819*56bb7041Schristos priv->r_debug = get_r_debug (pid, is_elf64);
6820*56bb7041Schristos
6821*56bb7041Schristos /* We failed to find DT_DEBUG. Such situation will not change
6822*56bb7041Schristos for this inferior - do not retry it. Report it to GDB as
6823*56bb7041Schristos E01, see for the reasons at the GDB solib-svr4.c side. */
6824*56bb7041Schristos if (priv->r_debug == (CORE_ADDR) -1)
6825*56bb7041Schristos return -1;
6826*56bb7041Schristos
6827*56bb7041Schristos if (priv->r_debug != 0)
6828*56bb7041Schristos {
6829*56bb7041Schristos if (linux_read_memory (priv->r_debug + lmo->r_version_offset,
6830*56bb7041Schristos (unsigned char *) &r_version,
6831*56bb7041Schristos sizeof (r_version)) != 0
6832*56bb7041Schristos || r_version != 1)
6833*56bb7041Schristos {
6834*56bb7041Schristos warning ("unexpected r_debug version %d", r_version);
6835*56bb7041Schristos }
6836*56bb7041Schristos else if (read_one_ptr (priv->r_debug + lmo->r_map_offset,
6837*56bb7041Schristos &lm_addr, ptr_size) != 0)
6838*56bb7041Schristos {
6839*56bb7041Schristos warning ("unable to read r_map from 0x%lx",
6840*56bb7041Schristos (long) priv->r_debug + lmo->r_map_offset);
6841*56bb7041Schristos }
6842*56bb7041Schristos }
6843*56bb7041Schristos }
6844*56bb7041Schristos
6845*56bb7041Schristos std::string document = "<library-list-svr4 version=\"1.0\"";
6846*56bb7041Schristos
6847*56bb7041Schristos while (lm_addr
6848*56bb7041Schristos && read_one_ptr (lm_addr + lmo->l_name_offset,
6849*56bb7041Schristos &l_name, ptr_size) == 0
6850*56bb7041Schristos && read_one_ptr (lm_addr + lmo->l_addr_offset,
6851*56bb7041Schristos &l_addr, ptr_size) == 0
6852*56bb7041Schristos && read_one_ptr (lm_addr + lmo->l_ld_offset,
6853*56bb7041Schristos &l_ld, ptr_size) == 0
6854*56bb7041Schristos && read_one_ptr (lm_addr + lmo->l_prev_offset,
6855*56bb7041Schristos &l_prev, ptr_size) == 0
6856*56bb7041Schristos && read_one_ptr (lm_addr + lmo->l_next_offset,
6857*56bb7041Schristos &l_next, ptr_size) == 0)
6858*56bb7041Schristos {
6859*56bb7041Schristos unsigned char libname[PATH_MAX];
6860*56bb7041Schristos
6861*56bb7041Schristos if (lm_prev != l_prev)
6862*56bb7041Schristos {
6863*56bb7041Schristos warning ("Corrupted shared library list: 0x%lx != 0x%lx",
6864*56bb7041Schristos (long) lm_prev, (long) l_prev);
6865*56bb7041Schristos break;
6866*56bb7041Schristos }
6867*56bb7041Schristos
6868*56bb7041Schristos /* Ignore the first entry even if it has valid name as the first entry
6869*56bb7041Schristos corresponds to the main executable. The first entry should not be
6870*56bb7041Schristos skipped if the dynamic loader was loaded late by a static executable
6871*56bb7041Schristos (see solib-svr4.c parameter ignore_first). But in such case the main
6872*56bb7041Schristos executable does not have PT_DYNAMIC present and this function already
6873*56bb7041Schristos exited above due to failed get_r_debug. */
6874*56bb7041Schristos if (lm_prev == 0)
6875*56bb7041Schristos string_appendf (document, " main-lm=\"0x%lx\"", (unsigned long) lm_addr);
6876*56bb7041Schristos else
6877*56bb7041Schristos {
6878*56bb7041Schristos /* Not checking for error because reading may stop before
6879*56bb7041Schristos we've got PATH_MAX worth of characters. */
6880*56bb7041Schristos libname[0] = '\0';
6881*56bb7041Schristos linux_read_memory (l_name, libname, sizeof (libname) - 1);
6882*56bb7041Schristos libname[sizeof (libname) - 1] = '\0';
6883*56bb7041Schristos if (libname[0] != '\0')
6884*56bb7041Schristos {
6885*56bb7041Schristos if (!header_done)
6886*56bb7041Schristos {
6887*56bb7041Schristos /* Terminate `<library-list-svr4'. */
6888*56bb7041Schristos document += '>';
6889*56bb7041Schristos header_done = 1;
6890*56bb7041Schristos }
6891*56bb7041Schristos
6892*56bb7041Schristos string_appendf (document, "<library name=\"");
6893*56bb7041Schristos xml_escape_text_append (&document, (char *) libname);
6894*56bb7041Schristos string_appendf (document, "\" lm=\"0x%lx\" "
6895*56bb7041Schristos "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
6896*56bb7041Schristos (unsigned long) lm_addr, (unsigned long) l_addr,
6897*56bb7041Schristos (unsigned long) l_ld);
6898*56bb7041Schristos }
6899*56bb7041Schristos }
6900*56bb7041Schristos
6901*56bb7041Schristos lm_prev = lm_addr;
6902*56bb7041Schristos lm_addr = l_next;
6903*56bb7041Schristos }
6904*56bb7041Schristos
6905*56bb7041Schristos if (!header_done)
6906*56bb7041Schristos {
6907*56bb7041Schristos /* Empty list; terminate `<library-list-svr4'. */
6908*56bb7041Schristos document += "/>";
6909*56bb7041Schristos }
6910*56bb7041Schristos else
6911*56bb7041Schristos document += "</library-list-svr4>";
6912*56bb7041Schristos
6913*56bb7041Schristos int document_len = document.length ();
6914*56bb7041Schristos if (offset < document_len)
6915*56bb7041Schristos document_len -= offset;
6916*56bb7041Schristos else
6917*56bb7041Schristos document_len = 0;
6918*56bb7041Schristos if (len > document_len)
6919*56bb7041Schristos len = document_len;
6920*56bb7041Schristos
6921*56bb7041Schristos memcpy (readbuf, document.data () + offset, len);
6922*56bb7041Schristos
6923*56bb7041Schristos return len;
6924*56bb7041Schristos }
6925*56bb7041Schristos
6926*56bb7041Schristos #ifdef HAVE_LINUX_BTRACE
6927*56bb7041Schristos
6928*56bb7041Schristos btrace_target_info *
enable_btrace(ptid_t ptid,const btrace_config * conf)6929*56bb7041Schristos linux_process_target::enable_btrace (ptid_t ptid,
6930*56bb7041Schristos const btrace_config *conf)
6931*56bb7041Schristos {
6932*56bb7041Schristos return linux_enable_btrace (ptid, conf);
6933*56bb7041Schristos }
6934*56bb7041Schristos
6935*56bb7041Schristos /* See to_disable_btrace target method. */
6936*56bb7041Schristos
6937*56bb7041Schristos int
disable_btrace(btrace_target_info * tinfo)6938*56bb7041Schristos linux_process_target::disable_btrace (btrace_target_info *tinfo)
6939*56bb7041Schristos {
6940*56bb7041Schristos enum btrace_error err;
6941*56bb7041Schristos
6942*56bb7041Schristos err = linux_disable_btrace (tinfo);
6943*56bb7041Schristos return (err == BTRACE_ERR_NONE ? 0 : -1);
6944*56bb7041Schristos }
6945*56bb7041Schristos
6946*56bb7041Schristos /* Encode an Intel Processor Trace configuration. */
6947*56bb7041Schristos
6948*56bb7041Schristos static void
linux_low_encode_pt_config(struct buffer * buffer,const struct btrace_data_pt_config * config)6949*56bb7041Schristos linux_low_encode_pt_config (struct buffer *buffer,
6950*56bb7041Schristos const struct btrace_data_pt_config *config)
6951*56bb7041Schristos {
6952*56bb7041Schristos buffer_grow_str (buffer, "<pt-config>\n");
6953*56bb7041Schristos
6954*56bb7041Schristos switch (config->cpu.vendor)
6955*56bb7041Schristos {
6956*56bb7041Schristos case CV_INTEL:
6957*56bb7041Schristos buffer_xml_printf (buffer, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
6958*56bb7041Schristos "model=\"%u\" stepping=\"%u\"/>\n",
6959*56bb7041Schristos config->cpu.family, config->cpu.model,
6960*56bb7041Schristos config->cpu.stepping);
6961*56bb7041Schristos break;
6962*56bb7041Schristos
6963*56bb7041Schristos default:
6964*56bb7041Schristos break;
6965*56bb7041Schristos }
6966*56bb7041Schristos
6967*56bb7041Schristos buffer_grow_str (buffer, "</pt-config>\n");
6968*56bb7041Schristos }
6969*56bb7041Schristos
6970*56bb7041Schristos /* Encode a raw buffer. */
6971*56bb7041Schristos
6972*56bb7041Schristos static void
linux_low_encode_raw(struct buffer * buffer,const gdb_byte * data,unsigned int size)6973*56bb7041Schristos linux_low_encode_raw (struct buffer *buffer, const gdb_byte *data,
6974*56bb7041Schristos unsigned int size)
6975*56bb7041Schristos {
6976*56bb7041Schristos if (size == 0)
6977*56bb7041Schristos return;
6978*56bb7041Schristos
6979*56bb7041Schristos /* We use hex encoding - see gdbsupport/rsp-low.h. */
6980*56bb7041Schristos buffer_grow_str (buffer, "<raw>\n");
6981*56bb7041Schristos
6982*56bb7041Schristos while (size-- > 0)
6983*56bb7041Schristos {
6984*56bb7041Schristos char elem[2];
6985*56bb7041Schristos
6986*56bb7041Schristos elem[0] = tohex ((*data >> 4) & 0xf);
6987*56bb7041Schristos elem[1] = tohex (*data++ & 0xf);
6988*56bb7041Schristos
6989*56bb7041Schristos buffer_grow (buffer, elem, 2);
6990*56bb7041Schristos }
6991*56bb7041Schristos
6992*56bb7041Schristos buffer_grow_str (buffer, "</raw>\n");
6993*56bb7041Schristos }
6994*56bb7041Schristos
6995*56bb7041Schristos /* See to_read_btrace target method. */
6996*56bb7041Schristos
6997*56bb7041Schristos int
read_btrace(btrace_target_info * tinfo,buffer * buffer,enum btrace_read_type type)6998*56bb7041Schristos linux_process_target::read_btrace (btrace_target_info *tinfo,
6999*56bb7041Schristos buffer *buffer,
7000*56bb7041Schristos enum btrace_read_type type)
7001*56bb7041Schristos {
7002*56bb7041Schristos struct btrace_data btrace;
7003*56bb7041Schristos enum btrace_error err;
7004*56bb7041Schristos
7005*56bb7041Schristos err = linux_read_btrace (&btrace, tinfo, type);
7006*56bb7041Schristos if (err != BTRACE_ERR_NONE)
7007*56bb7041Schristos {
7008*56bb7041Schristos if (err == BTRACE_ERR_OVERFLOW)
7009*56bb7041Schristos buffer_grow_str0 (buffer, "E.Overflow.");
7010*56bb7041Schristos else
7011*56bb7041Schristos buffer_grow_str0 (buffer, "E.Generic Error.");
7012*56bb7041Schristos
7013*56bb7041Schristos return -1;
7014*56bb7041Schristos }
7015*56bb7041Schristos
7016*56bb7041Schristos switch (btrace.format)
7017*56bb7041Schristos {
7018*56bb7041Schristos case BTRACE_FORMAT_NONE:
7019*56bb7041Schristos buffer_grow_str0 (buffer, "E.No Trace.");
7020*56bb7041Schristos return -1;
7021*56bb7041Schristos
7022*56bb7041Schristos case BTRACE_FORMAT_BTS:
7023*56bb7041Schristos buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7024*56bb7041Schristos buffer_grow_str (buffer, "<btrace version=\"1.0\">\n");
7025*56bb7041Schristos
7026*56bb7041Schristos for (const btrace_block &block : *btrace.variant.bts.blocks)
7027*56bb7041Schristos buffer_xml_printf (buffer, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
7028*56bb7041Schristos paddress (block.begin), paddress (block.end));
7029*56bb7041Schristos
7030*56bb7041Schristos buffer_grow_str0 (buffer, "</btrace>\n");
7031*56bb7041Schristos break;
7032*56bb7041Schristos
7033*56bb7041Schristos case BTRACE_FORMAT_PT:
7034*56bb7041Schristos buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7035*56bb7041Schristos buffer_grow_str (buffer, "<btrace version=\"1.0\">\n");
7036*56bb7041Schristos buffer_grow_str (buffer, "<pt>\n");
7037*56bb7041Schristos
7038*56bb7041Schristos linux_low_encode_pt_config (buffer, &btrace.variant.pt.config);
7039*56bb7041Schristos
7040*56bb7041Schristos linux_low_encode_raw (buffer, btrace.variant.pt.data,
7041*56bb7041Schristos btrace.variant.pt.size);
7042*56bb7041Schristos
7043*56bb7041Schristos buffer_grow_str (buffer, "</pt>\n");
7044*56bb7041Schristos buffer_grow_str0 (buffer, "</btrace>\n");
7045*56bb7041Schristos break;
7046*56bb7041Schristos
7047*56bb7041Schristos default:
7048*56bb7041Schristos buffer_grow_str0 (buffer, "E.Unsupported Trace Format.");
7049*56bb7041Schristos return -1;
7050*56bb7041Schristos }
7051*56bb7041Schristos
7052*56bb7041Schristos return 0;
7053*56bb7041Schristos }
7054*56bb7041Schristos
7055*56bb7041Schristos /* See to_btrace_conf target method. */
7056*56bb7041Schristos
7057*56bb7041Schristos int
read_btrace_conf(const btrace_target_info * tinfo,buffer * buffer)7058*56bb7041Schristos linux_process_target::read_btrace_conf (const btrace_target_info *tinfo,
7059*56bb7041Schristos buffer *buffer)
7060*56bb7041Schristos {
7061*56bb7041Schristos const struct btrace_config *conf;
7062*56bb7041Schristos
7063*56bb7041Schristos buffer_grow_str (buffer, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
7064*56bb7041Schristos buffer_grow_str (buffer, "<btrace-conf version=\"1.0\">\n");
7065*56bb7041Schristos
7066*56bb7041Schristos conf = linux_btrace_conf (tinfo);
7067*56bb7041Schristos if (conf != NULL)
7068*56bb7041Schristos {
7069*56bb7041Schristos switch (conf->format)
7070*56bb7041Schristos {
7071*56bb7041Schristos case BTRACE_FORMAT_NONE:
7072*56bb7041Schristos break;
7073*56bb7041Schristos
7074*56bb7041Schristos case BTRACE_FORMAT_BTS:
7075*56bb7041Schristos buffer_xml_printf (buffer, "<bts");
7076*56bb7041Schristos buffer_xml_printf (buffer, " size=\"0x%x\"", conf->bts.size);
7077*56bb7041Schristos buffer_xml_printf (buffer, " />\n");
7078*56bb7041Schristos break;
7079*56bb7041Schristos
7080*56bb7041Schristos case BTRACE_FORMAT_PT:
7081*56bb7041Schristos buffer_xml_printf (buffer, "<pt");
7082*56bb7041Schristos buffer_xml_printf (buffer, " size=\"0x%x\"", conf->pt.size);
7083*56bb7041Schristos buffer_xml_printf (buffer, "/>\n");
7084*56bb7041Schristos break;
7085*56bb7041Schristos }
7086*56bb7041Schristos }
7087*56bb7041Schristos
7088*56bb7041Schristos buffer_grow_str0 (buffer, "</btrace-conf>\n");
7089*56bb7041Schristos return 0;
7090*56bb7041Schristos }
7091*56bb7041Schristos #endif /* HAVE_LINUX_BTRACE */
7092*56bb7041Schristos
7093*56bb7041Schristos /* See nat/linux-nat.h. */
7094*56bb7041Schristos
7095*56bb7041Schristos ptid_t
current_lwp_ptid(void)7096*56bb7041Schristos current_lwp_ptid (void)
7097*56bb7041Schristos {
7098*56bb7041Schristos return ptid_of (current_thread);
7099*56bb7041Schristos }
7100*56bb7041Schristos
7101*56bb7041Schristos const char *
thread_name(ptid_t thread)7102*56bb7041Schristos linux_process_target::thread_name (ptid_t thread)
7103*56bb7041Schristos {
7104*56bb7041Schristos return linux_proc_tid_get_name (thread);
7105*56bb7041Schristos }
7106*56bb7041Schristos
7107*56bb7041Schristos #if USE_THREAD_DB
7108*56bb7041Schristos bool
thread_handle(ptid_t ptid,gdb_byte ** handle,int * handle_len)7109*56bb7041Schristos linux_process_target::thread_handle (ptid_t ptid, gdb_byte **handle,
7110*56bb7041Schristos int *handle_len)
7111*56bb7041Schristos {
7112*56bb7041Schristos return thread_db_thread_handle (ptid, handle, handle_len);
7113*56bb7041Schristos }
7114*56bb7041Schristos #endif
7115*56bb7041Schristos
7116*56bb7041Schristos /* Default implementation of linux_target_ops method "set_pc" for
7117*56bb7041Schristos 32-bit pc register which is literally named "pc". */
7118*56bb7041Schristos
7119*56bb7041Schristos void
linux_set_pc_32bit(struct regcache * regcache,CORE_ADDR pc)7120*56bb7041Schristos linux_set_pc_32bit (struct regcache *regcache, CORE_ADDR pc)
7121*56bb7041Schristos {
7122*56bb7041Schristos uint32_t newpc = pc;
7123*56bb7041Schristos
7124*56bb7041Schristos supply_register_by_name (regcache, "pc", &newpc);
7125*56bb7041Schristos }
7126*56bb7041Schristos
7127*56bb7041Schristos /* Default implementation of linux_target_ops method "get_pc" for
7128*56bb7041Schristos 32-bit pc register which is literally named "pc". */
7129*56bb7041Schristos
7130*56bb7041Schristos CORE_ADDR
linux_get_pc_32bit(struct regcache * regcache)7131*56bb7041Schristos linux_get_pc_32bit (struct regcache *regcache)
7132*56bb7041Schristos {
7133*56bb7041Schristos uint32_t pc;
7134*56bb7041Schristos
7135*56bb7041Schristos collect_register_by_name (regcache, "pc", &pc);
7136*56bb7041Schristos if (debug_threads)
7137*56bb7041Schristos debug_printf ("stop pc is 0x%" PRIx32 "\n", pc);
7138*56bb7041Schristos return pc;
7139*56bb7041Schristos }
7140*56bb7041Schristos
7141*56bb7041Schristos /* Default implementation of linux_target_ops method "set_pc" for
7142*56bb7041Schristos 64-bit pc register which is literally named "pc". */
7143*56bb7041Schristos
7144*56bb7041Schristos void
linux_set_pc_64bit(struct regcache * regcache,CORE_ADDR pc)7145*56bb7041Schristos linux_set_pc_64bit (struct regcache *regcache, CORE_ADDR pc)
7146*56bb7041Schristos {
7147*56bb7041Schristos uint64_t newpc = pc;
7148*56bb7041Schristos
7149*56bb7041Schristos supply_register_by_name (regcache, "pc", &newpc);
7150*56bb7041Schristos }
7151*56bb7041Schristos
7152*56bb7041Schristos /* Default implementation of linux_target_ops method "get_pc" for
7153*56bb7041Schristos 64-bit pc register which is literally named "pc". */
7154*56bb7041Schristos
7155*56bb7041Schristos CORE_ADDR
linux_get_pc_64bit(struct regcache * regcache)7156*56bb7041Schristos linux_get_pc_64bit (struct regcache *regcache)
7157*56bb7041Schristos {
7158*56bb7041Schristos uint64_t pc;
7159*56bb7041Schristos
7160*56bb7041Schristos collect_register_by_name (regcache, "pc", &pc);
7161*56bb7041Schristos if (debug_threads)
7162*56bb7041Schristos debug_printf ("stop pc is 0x%" PRIx64 "\n", pc);
7163*56bb7041Schristos return pc;
7164*56bb7041Schristos }
7165*56bb7041Schristos
7166*56bb7041Schristos /* See linux-low.h. */
7167*56bb7041Schristos
7168*56bb7041Schristos int
linux_get_auxv(int wordsize,CORE_ADDR match,CORE_ADDR * valp)7169*56bb7041Schristos linux_get_auxv (int wordsize, CORE_ADDR match, CORE_ADDR *valp)
7170*56bb7041Schristos {
7171*56bb7041Schristos gdb_byte *data = (gdb_byte *) alloca (2 * wordsize);
7172*56bb7041Schristos int offset = 0;
7173*56bb7041Schristos
7174*56bb7041Schristos gdb_assert (wordsize == 4 || wordsize == 8);
7175*56bb7041Schristos
7176*56bb7041Schristos while (the_target->read_auxv (offset, data, 2 * wordsize) == 2 * wordsize)
7177*56bb7041Schristos {
7178*56bb7041Schristos if (wordsize == 4)
7179*56bb7041Schristos {
7180*56bb7041Schristos uint32_t *data_p = (uint32_t *) data;
7181*56bb7041Schristos if (data_p[0] == match)
7182*56bb7041Schristos {
7183*56bb7041Schristos *valp = data_p[1];
7184*56bb7041Schristos return 1;
7185*56bb7041Schristos }
7186*56bb7041Schristos }
7187*56bb7041Schristos else
7188*56bb7041Schristos {
7189*56bb7041Schristos uint64_t *data_p = (uint64_t *) data;
7190*56bb7041Schristos if (data_p[0] == match)
7191*56bb7041Schristos {
7192*56bb7041Schristos *valp = data_p[1];
7193*56bb7041Schristos return 1;
7194*56bb7041Schristos }
7195*56bb7041Schristos }
7196*56bb7041Schristos
7197*56bb7041Schristos offset += 2 * wordsize;
7198*56bb7041Schristos }
7199*56bb7041Schristos
7200*56bb7041Schristos return 0;
7201*56bb7041Schristos }
7202*56bb7041Schristos
7203*56bb7041Schristos /* See linux-low.h. */
7204*56bb7041Schristos
7205*56bb7041Schristos CORE_ADDR
linux_get_hwcap(int wordsize)7206*56bb7041Schristos linux_get_hwcap (int wordsize)
7207*56bb7041Schristos {
7208*56bb7041Schristos CORE_ADDR hwcap = 0;
7209*56bb7041Schristos linux_get_auxv (wordsize, AT_HWCAP, &hwcap);
7210*56bb7041Schristos return hwcap;
7211*56bb7041Schristos }
7212*56bb7041Schristos
7213*56bb7041Schristos /* See linux-low.h. */
7214*56bb7041Schristos
7215*56bb7041Schristos CORE_ADDR
linux_get_hwcap2(int wordsize)7216*56bb7041Schristos linux_get_hwcap2 (int wordsize)
7217*56bb7041Schristos {
7218*56bb7041Schristos CORE_ADDR hwcap2 = 0;
7219*56bb7041Schristos linux_get_auxv (wordsize, AT_HWCAP2, &hwcap2);
7220*56bb7041Schristos return hwcap2;
7221*56bb7041Schristos }
7222*56bb7041Schristos
7223*56bb7041Schristos #ifdef HAVE_LINUX_REGSETS
7224*56bb7041Schristos void
initialize_regsets_info(struct regsets_info * info)7225*56bb7041Schristos initialize_regsets_info (struct regsets_info *info)
7226*56bb7041Schristos {
7227*56bb7041Schristos for (info->num_regsets = 0;
7228*56bb7041Schristos info->regsets[info->num_regsets].size >= 0;
7229*56bb7041Schristos info->num_regsets++)
7230*56bb7041Schristos ;
7231*56bb7041Schristos }
7232*56bb7041Schristos #endif
7233*56bb7041Schristos
7234*56bb7041Schristos void
initialize_low(void)7235*56bb7041Schristos initialize_low (void)
7236*56bb7041Schristos {
7237*56bb7041Schristos struct sigaction sigchld_action;
7238*56bb7041Schristos
7239*56bb7041Schristos memset (&sigchld_action, 0, sizeof (sigchld_action));
7240*56bb7041Schristos set_target_ops (the_linux_target);
7241*56bb7041Schristos
7242*56bb7041Schristos linux_ptrace_init_warnings ();
7243*56bb7041Schristos linux_proc_init_warnings ();
7244*56bb7041Schristos
7245*56bb7041Schristos sigchld_action.sa_handler = sigchld_handler;
7246*56bb7041Schristos sigemptyset (&sigchld_action.sa_mask);
7247*56bb7041Schristos sigchld_action.sa_flags = SA_RESTART;
7248*56bb7041Schristos sigaction (SIGCHLD, &sigchld_action, NULL);
7249*56bb7041Schristos
7250*56bb7041Schristos initialize_low_arch ();
7251*56bb7041Schristos
7252*56bb7041Schristos linux_check_ptrace_features ();
7253*56bb7041Schristos }
7254