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