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