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