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