Remove all_lwps global.
[binutils-gdb.git] / gdb / gdbserver / linux-low.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2014 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "server.h"
20 #include "linux-low.h"
21 #include "linux-osdata.h"
22 #include "agent.h"
23
24 #include "nat/linux-nat.h"
25 #include "nat/linux-waitpid.h"
26 #include "gdb_wait.h"
27 #include <stdio.h>
28 #include <sys/ptrace.h>
29 #include "linux-ptrace.h"
30 #include "linux-procfs.h"
31 #include <signal.h>
32 #include <sys/ioctl.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <sys/syscall.h>
39 #include <sched.h>
40 #include <ctype.h>
41 #include <pwd.h>
42 #include <sys/types.h>
43 #include <dirent.h>
44 #include <sys/stat.h>
45 #include <sys/vfs.h>
46 #include <sys/uio.h>
47 #include "filestuff.h"
48 #include "tracepoint.h"
49 #include "hostio.h"
50 #ifndef ELFMAG0
51 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
52 then ELFMAG0 will have been defined. If it didn't get included by
53 gdb_proc_service.h then including it will likely introduce a duplicate
54 definition of elf_fpregset_t. */
55 #include <elf.h>
56 #endif
57
58 #ifndef SPUFS_MAGIC
59 #define SPUFS_MAGIC 0x23c9b64e
60 #endif
61
62 #ifdef HAVE_PERSONALITY
63 # include <sys/personality.h>
64 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
65 # define ADDR_NO_RANDOMIZE 0x0040000
66 # endif
67 #endif
68
69 #ifndef O_LARGEFILE
70 #define O_LARGEFILE 0
71 #endif
72
73 #ifndef W_STOPCODE
74 #define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
75 #endif
76
77 /* This is the kernel's hard limit. Not to be confused with
78 SIGRTMIN. */
79 #ifndef __SIGRTMIN
80 #define __SIGRTMIN 32
81 #endif
82
83 /* Some targets did not define these ptrace constants from the start,
84 so gdbserver defines them locally here. In the future, these may
85 be removed after they are added to asm/ptrace.h. */
86 #if !(defined(PT_TEXT_ADDR) \
87 || defined(PT_DATA_ADDR) \
88 || defined(PT_TEXT_END_ADDR))
89 #if defined(__mcoldfire__)
90 /* These are still undefined in 3.10 kernels. */
91 #define PT_TEXT_ADDR 49*4
92 #define PT_DATA_ADDR 50*4
93 #define PT_TEXT_END_ADDR 51*4
94 /* BFIN already defines these since at least 2.6.32 kernels. */
95 #elif defined(BFIN)
96 #define PT_TEXT_ADDR 220
97 #define PT_TEXT_END_ADDR 224
98 #define PT_DATA_ADDR 228
99 /* These are still undefined in 3.10 kernels. */
100 #elif defined(__TMS320C6X__)
101 #define PT_TEXT_ADDR (0x10000*4)
102 #define PT_DATA_ADDR (0x10004*4)
103 #define PT_TEXT_END_ADDR (0x10008*4)
104 #endif
105 #endif
106
107 #ifdef HAVE_LINUX_BTRACE
108 # include "linux-btrace.h"
109 #endif
110
111 #ifndef HAVE_ELF32_AUXV_T
112 /* Copied from glibc's elf.h. */
113 typedef struct
114 {
115 uint32_t a_type; /* Entry type */
116 union
117 {
118 uint32_t a_val; /* Integer value */
119 /* We use to have pointer elements added here. We cannot do that,
120 though, since it does not work when using 32-bit definitions
121 on 64-bit platforms and vice versa. */
122 } a_un;
123 } Elf32_auxv_t;
124 #endif
125
126 #ifndef HAVE_ELF64_AUXV_T
127 /* Copied from glibc's elf.h. */
128 typedef struct
129 {
130 uint64_t a_type; /* Entry type */
131 union
132 {
133 uint64_t a_val; /* Integer value */
134 /* We use to have pointer elements added here. We cannot do that,
135 though, since it does not work when using 32-bit definitions
136 on 64-bit platforms and vice versa. */
137 } a_un;
138 } Elf64_auxv_t;
139 #endif
140
141 /* A list of all unknown processes which receive stop signals. Some
142 other process will presumably claim each of these as forked
143 children momentarily. */
144
145 struct simple_pid_list
146 {
147 /* The process ID. */
148 int pid;
149
150 /* The status as reported by waitpid. */
151 int status;
152
153 /* Next in chain. */
154 struct simple_pid_list *next;
155 };
156 struct simple_pid_list *stopped_pids;
157
158 /* Trivial list manipulation functions to keep track of a list of new
159 stopped processes. */
160
161 static void
162 add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
163 {
164 struct simple_pid_list *new_pid = xmalloc (sizeof (struct simple_pid_list));
165
166 new_pid->pid = pid;
167 new_pid->status = status;
168 new_pid->next = *listp;
169 *listp = new_pid;
170 }
171
172 static int
173 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
174 {
175 struct simple_pid_list **p;
176
177 for (p = listp; *p != NULL; p = &(*p)->next)
178 if ((*p)->pid == pid)
179 {
180 struct simple_pid_list *next = (*p)->next;
181
182 *statusp = (*p)->status;
183 xfree (*p);
184 *p = next;
185 return 1;
186 }
187 return 0;
188 }
189
190 enum stopping_threads_kind
191 {
192 /* Not stopping threads presently. */
193 NOT_STOPPING_THREADS,
194
195 /* Stopping threads. */
196 STOPPING_THREADS,
197
198 /* Stopping and suspending threads. */
199 STOPPING_AND_SUSPENDING_THREADS
200 };
201
202 /* This is set while stop_all_lwps is in effect. */
203 enum stopping_threads_kind stopping_threads = NOT_STOPPING_THREADS;
204
205 /* FIXME make into a target method? */
206 int using_threads = 1;
207
208 /* True if we're presently stabilizing threads (moving them out of
209 jump pads). */
210 static int stabilizing_threads;
211
212 static void linux_resume_one_lwp (struct lwp_info *lwp,
213 int step, int signal, siginfo_t *info);
214 static void linux_resume (struct thread_resume *resume_info, size_t n);
215 static void stop_all_lwps (int suspend, struct lwp_info *except);
216 static void unstop_all_lwps (int unsuspend, struct lwp_info *except);
217 static int linux_wait_for_event (ptid_t ptid, int *wstat, int options);
218 static struct lwp_info *add_lwp (ptid_t ptid);
219 static int linux_stopped_by_watchpoint (void);
220 static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
221 static void proceed_all_lwps (void);
222 static int finish_step_over (struct lwp_info *lwp);
223 static CORE_ADDR get_stop_pc (struct lwp_info *lwp);
224 static int kill_lwp (unsigned long lwpid, int signo);
225
226 /* True if the low target can hardware single-step. Such targets
227 don't need a BREAKPOINT_REINSERT_ADDR callback. */
228
229 static int
230 can_hardware_single_step (void)
231 {
232 return (the_low_target.breakpoint_reinsert_addr == NULL);
233 }
234
235 /* True if the low target supports memory breakpoints. If so, we'll
236 have a GET_PC implementation. */
237
238 static int
239 supports_breakpoints (void)
240 {
241 return (the_low_target.get_pc != NULL);
242 }
243
244 /* Returns true if this target can support fast tracepoints. This
245 does not mean that the in-process agent has been loaded in the
246 inferior. */
247
248 static int
249 supports_fast_tracepoints (void)
250 {
251 return the_low_target.install_fast_tracepoint_jump_pad != NULL;
252 }
253
254 /* True if LWP is stopped in its stepping range. */
255
256 static int
257 lwp_in_step_range (struct lwp_info *lwp)
258 {
259 CORE_ADDR pc = lwp->stop_pc;
260
261 return (pc >= lwp->step_range_start && pc < lwp->step_range_end);
262 }
263
264 struct pending_signals
265 {
266 int signal;
267 siginfo_t info;
268 struct pending_signals *prev;
269 };
270
271 /* The read/write ends of the pipe registered as waitable file in the
272 event loop. */
273 static int linux_event_pipe[2] = { -1, -1 };
274
275 /* True if we're currently in async mode. */
276 #define target_is_async_p() (linux_event_pipe[0] != -1)
277
278 static void send_sigstop (struct lwp_info *lwp);
279 static void wait_for_sigstop (struct inferior_list_entry *entry);
280
281 /* Return non-zero if HEADER is a 64-bit ELF file. */
282
283 static int
284 elf_64_header_p (const Elf64_Ehdr *header, unsigned int *machine)
285 {
286 if (header->e_ident[EI_MAG0] == ELFMAG0
287 && header->e_ident[EI_MAG1] == ELFMAG1
288 && header->e_ident[EI_MAG2] == ELFMAG2
289 && header->e_ident[EI_MAG3] == ELFMAG3)
290 {
291 *machine = header->e_machine;
292 return header->e_ident[EI_CLASS] == ELFCLASS64;
293
294 }
295 *machine = EM_NONE;
296 return -1;
297 }
298
299 /* Return non-zero if FILE is a 64-bit ELF file,
300 zero if the file is not a 64-bit ELF file,
301 and -1 if the file is not accessible or doesn't exist. */
302
303 static int
304 elf_64_file_p (const char *file, unsigned int *machine)
305 {
306 Elf64_Ehdr header;
307 int fd;
308
309 fd = open (file, O_RDONLY);
310 if (fd < 0)
311 return -1;
312
313 if (read (fd, &header, sizeof (header)) != sizeof (header))
314 {
315 close (fd);
316 return 0;
317 }
318 close (fd);
319
320 return elf_64_header_p (&header, machine);
321 }
322
323 /* Accepts an integer PID; Returns true if the executable PID is
324 running is a 64-bit ELF file.. */
325
326 int
327 linux_pid_exe_is_elf_64_file (int pid, unsigned int *machine)
328 {
329 char file[PATH_MAX];
330
331 sprintf (file, "/proc/%d/exe", pid);
332 return elf_64_file_p (file, machine);
333 }
334
335 static void
336 delete_lwp (struct lwp_info *lwp)
337 {
338 remove_thread (get_lwp_thread (lwp));
339 free (lwp->arch_private);
340 free (lwp);
341 }
342
343 /* Add a process to the common process list, and set its private
344 data. */
345
346 static struct process_info *
347 linux_add_process (int pid, int attached)
348 {
349 struct process_info *proc;
350
351 proc = add_process (pid, attached);
352 proc->private = xcalloc (1, sizeof (*proc->private));
353
354 /* Set the arch when the first LWP stops. */
355 proc->private->new_inferior = 1;
356
357 if (the_low_target.new_process != NULL)
358 proc->private->arch_private = the_low_target.new_process ();
359
360 return proc;
361 }
362
363 /* Handle a GNU/Linux extended wait response. If we see a clone
364 event, we need to add the new LWP to our list (and not report the
365 trap to higher layers). */
366
367 static void
368 handle_extended_wait (struct lwp_info *event_child, int wstat)
369 {
370 int event = wstat >> 16;
371 struct thread_info *event_thr = get_lwp_thread (event_child);
372 struct lwp_info *new_lwp;
373
374 if (event == PTRACE_EVENT_CLONE)
375 {
376 ptid_t ptid;
377 unsigned long new_pid;
378 int ret, status;
379
380 ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_thr), (PTRACE_TYPE_ARG3) 0,
381 &new_pid);
382
383 /* If we haven't already seen the new PID stop, wait for it now. */
384 if (!pull_pid_from_list (&stopped_pids, new_pid, &status))
385 {
386 /* The new child has a pending SIGSTOP. We can't affect it until it
387 hits the SIGSTOP, but we're already attached. */
388
389 ret = my_waitpid (new_pid, &status, __WALL);
390
391 if (ret == -1)
392 perror_with_name ("waiting for new child");
393 else if (ret != new_pid)
394 warning ("wait returned unexpected PID %d", ret);
395 else if (!WIFSTOPPED (status))
396 warning ("wait returned unexpected status 0x%x", status);
397 }
398
399 ptid = ptid_build (pid_of (event_thr), new_pid, 0);
400 new_lwp = add_lwp (ptid);
401
402 /* Either we're going to immediately resume the new thread
403 or leave it stopped. linux_resume_one_lwp is a nop if it
404 thinks the thread is currently running, so set this first
405 before calling linux_resume_one_lwp. */
406 new_lwp->stopped = 1;
407
408 /* If we're suspending all threads, leave this one suspended
409 too. */
410 if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS)
411 new_lwp->suspended = 1;
412
413 /* Normally we will get the pending SIGSTOP. But in some cases
414 we might get another signal delivered to the group first.
415 If we do get another signal, be sure not to lose it. */
416 if (WSTOPSIG (status) == SIGSTOP)
417 {
418 if (stopping_threads != NOT_STOPPING_THREADS)
419 new_lwp->stop_pc = get_stop_pc (new_lwp);
420 else
421 linux_resume_one_lwp (new_lwp, 0, 0, NULL);
422 }
423 else
424 {
425 new_lwp->stop_expected = 1;
426
427 if (stopping_threads != NOT_STOPPING_THREADS)
428 {
429 new_lwp->stop_pc = get_stop_pc (new_lwp);
430 new_lwp->status_pending_p = 1;
431 new_lwp->status_pending = status;
432 }
433 else
434 /* Pass the signal on. This is what GDB does - except
435 shouldn't we really report it instead? */
436 linux_resume_one_lwp (new_lwp, 0, WSTOPSIG (status), NULL);
437 }
438
439 /* Always resume the current thread. If we are stopping
440 threads, it will have a pending SIGSTOP; we may as well
441 collect it now. */
442 linux_resume_one_lwp (event_child, event_child->stepping, 0, NULL);
443 }
444 }
445
446 /* Return the PC as read from the regcache of LWP, without any
447 adjustment. */
448
449 static CORE_ADDR
450 get_pc (struct lwp_info *lwp)
451 {
452 struct thread_info *saved_inferior;
453 struct regcache *regcache;
454 CORE_ADDR pc;
455
456 if (the_low_target.get_pc == NULL)
457 return 0;
458
459 saved_inferior = current_inferior;
460 current_inferior = get_lwp_thread (lwp);
461
462 regcache = get_thread_regcache (current_inferior, 1);
463 pc = (*the_low_target.get_pc) (regcache);
464
465 if (debug_threads)
466 debug_printf ("pc is 0x%lx\n", (long) pc);
467
468 current_inferior = saved_inferior;
469 return pc;
470 }
471
472 /* This function should only be called if LWP got a SIGTRAP.
473 The SIGTRAP could mean several things.
474
475 On i386, where decr_pc_after_break is non-zero:
476 If we were single-stepping this process using PTRACE_SINGLESTEP,
477 we will get only the one SIGTRAP (even if the instruction we
478 stepped over was a breakpoint). The value of $eip will be the
479 next instruction.
480 If we continue the process using PTRACE_CONT, we will get a
481 SIGTRAP when we hit a breakpoint. The value of $eip will be
482 the instruction after the breakpoint (i.e. needs to be
483 decremented). If we report the SIGTRAP to GDB, we must also
484 report the undecremented PC. If we cancel the SIGTRAP, we
485 must resume at the decremented PC.
486
487 (Presumably, not yet tested) On a non-decr_pc_after_break machine
488 with hardware or kernel single-step:
489 If we single-step over a breakpoint instruction, our PC will
490 point at the following instruction. If we continue and hit a
491 breakpoint instruction, our PC will point at the breakpoint
492 instruction. */
493
494 static CORE_ADDR
495 get_stop_pc (struct lwp_info *lwp)
496 {
497 CORE_ADDR stop_pc;
498
499 if (the_low_target.get_pc == NULL)
500 return 0;
501
502 stop_pc = get_pc (lwp);
503
504 if (WSTOPSIG (lwp->last_status) == SIGTRAP
505 && !lwp->stepping
506 && !lwp->stopped_by_watchpoint
507 && lwp->last_status >> 16 == 0)
508 stop_pc -= the_low_target.decr_pc_after_break;
509
510 if (debug_threads)
511 debug_printf ("stop pc is 0x%lx\n", (long) stop_pc);
512
513 return stop_pc;
514 }
515
516 static struct lwp_info *
517 add_lwp (ptid_t ptid)
518 {
519 struct lwp_info *lwp;
520
521 lwp = (struct lwp_info *) xmalloc (sizeof (*lwp));
522 memset (lwp, 0, sizeof (*lwp));
523
524 if (the_low_target.new_thread != NULL)
525 lwp->arch_private = the_low_target.new_thread ();
526
527 lwp->thread = add_thread (ptid, lwp);
528
529 return lwp;
530 }
531
532 /* Start an inferior process and returns its pid.
533 ALLARGS is a vector of program-name and args. */
534
535 static int
536 linux_create_inferior (char *program, char **allargs)
537 {
538 #ifdef HAVE_PERSONALITY
539 int personality_orig = 0, personality_set = 0;
540 #endif
541 struct lwp_info *new_lwp;
542 int pid;
543 ptid_t ptid;
544
545 #ifdef HAVE_PERSONALITY
546 if (disable_randomization)
547 {
548 errno = 0;
549 personality_orig = personality (0xffffffff);
550 if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
551 {
552 personality_set = 1;
553 personality (personality_orig | ADDR_NO_RANDOMIZE);
554 }
555 if (errno != 0 || (personality_set
556 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
557 warning ("Error disabling address space randomization: %s",
558 strerror (errno));
559 }
560 #endif
561
562 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
563 pid = vfork ();
564 #else
565 pid = fork ();
566 #endif
567 if (pid < 0)
568 perror_with_name ("fork");
569
570 if (pid == 0)
571 {
572 close_most_fds ();
573 ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
574
575 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
576 signal (__SIGRTMIN + 1, SIG_DFL);
577 #endif
578
579 setpgid (0, 0);
580
581 /* If gdbserver is connected to gdb via stdio, redirect the inferior's
582 stdout to stderr so that inferior i/o doesn't corrupt the connection.
583 Also, redirect stdin to /dev/null. */
584 if (remote_connection_is_stdio ())
585 {
586 close (0);
587 open ("/dev/null", O_RDONLY);
588 dup2 (2, 1);
589 if (write (2, "stdin/stdout redirected\n",
590 sizeof ("stdin/stdout redirected\n") - 1) < 0)
591 {
592 /* Errors ignored. */;
593 }
594 }
595
596 execv (program, allargs);
597 if (errno == ENOENT)
598 execvp (program, allargs);
599
600 fprintf (stderr, "Cannot exec %s: %s.\n", program,
601 strerror (errno));
602 fflush (stderr);
603 _exit (0177);
604 }
605
606 #ifdef HAVE_PERSONALITY
607 if (personality_set)
608 {
609 errno = 0;
610 personality (personality_orig);
611 if (errno != 0)
612 warning ("Error restoring address space randomization: %s",
613 strerror (errno));
614 }
615 #endif
616
617 linux_add_process (pid, 0);
618
619 ptid = ptid_build (pid, pid, 0);
620 new_lwp = add_lwp (ptid);
621 new_lwp->must_set_ptrace_flags = 1;
622
623 return pid;
624 }
625
626 /* Attach to an inferior process. */
627
628 static void
629 linux_attach_lwp_1 (unsigned long lwpid, int initial)
630 {
631 ptid_t ptid;
632 struct lwp_info *new_lwp;
633
634 if (ptrace (PTRACE_ATTACH, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0)
635 != 0)
636 {
637 struct buffer buffer;
638
639 if (!initial)
640 {
641 /* If we fail to attach to an LWP, just warn. */
642 fprintf (stderr, "Cannot attach to lwp %ld: %s (%d)\n", lwpid,
643 strerror (errno), errno);
644 fflush (stderr);
645 return;
646 }
647
648 /* If we fail to attach to a process, report an error. */
649 buffer_init (&buffer);
650 linux_ptrace_attach_warnings (lwpid, &buffer);
651 buffer_grow_str0 (&buffer, "");
652 error ("%sCannot attach to lwp %ld: %s (%d)", buffer_finish (&buffer),
653 lwpid, strerror (errno), errno);
654 }
655
656 if (initial)
657 /* If lwp is the tgid, we handle adding existing threads later.
658 Otherwise we just add lwp without bothering about any other
659 threads. */
660 ptid = ptid_build (lwpid, lwpid, 0);
661 else
662 {
663 /* Note that extracting the pid from the current inferior is
664 safe, since we're always called in the context of the same
665 process as this new thread. */
666 int pid = pid_of (current_inferior);
667 ptid = ptid_build (pid, lwpid, 0);
668 }
669
670 new_lwp = add_lwp (ptid);
671
672 /* We need to wait for SIGSTOP before being able to make the next
673 ptrace call on this LWP. */
674 new_lwp->must_set_ptrace_flags = 1;
675
676 if (linux_proc_pid_is_stopped (lwpid))
677 {
678 if (debug_threads)
679 debug_printf ("Attached to a stopped process\n");
680
681 /* The process is definitely stopped. It is in a job control
682 stop, unless the kernel predates the TASK_STOPPED /
683 TASK_TRACED distinction, in which case it might be in a
684 ptrace stop. Make sure it is in a ptrace stop; from there we
685 can kill it, signal it, et cetera.
686
687 First make sure there is a pending SIGSTOP. Since we are
688 already attached, the process can not transition from stopped
689 to running without a PTRACE_CONT; so we know this signal will
690 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
691 probably already in the queue (unless this kernel is old
692 enough to use TASK_STOPPED for ptrace stops); but since
693 SIGSTOP is not an RT signal, it can only be queued once. */
694 kill_lwp (lwpid, SIGSTOP);
695
696 /* Finally, resume the stopped process. This will deliver the
697 SIGSTOP (or a higher priority signal, just like normal
698 PTRACE_ATTACH), which we'll catch later on. */
699 ptrace (PTRACE_CONT, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
700 }
701
702 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
703 brings it to a halt.
704
705 There are several cases to consider here:
706
707 1) gdbserver has already attached to the process and is being notified
708 of a new thread that is being created.
709 In this case we should ignore that SIGSTOP and resume the
710 process. This is handled below by setting stop_expected = 1,
711 and the fact that add_thread sets last_resume_kind ==
712 resume_continue.
713
714 2) This is the first thread (the process thread), and we're attaching
715 to it via attach_inferior.
716 In this case we want the process thread to stop.
717 This is handled by having linux_attach set last_resume_kind ==
718 resume_stop after we return.
719
720 If the pid we are attaching to is also the tgid, we attach to and
721 stop all the existing threads. Otherwise, we attach to pid and
722 ignore any other threads in the same group as this pid.
723
724 3) GDB is connecting to gdbserver and is requesting an enumeration of all
725 existing threads.
726 In this case we want the thread to stop.
727 FIXME: This case is currently not properly handled.
728 We should wait for the SIGSTOP but don't. Things work apparently
729 because enough time passes between when we ptrace (ATTACH) and when
730 gdb makes the next ptrace call on the thread.
731
732 On the other hand, if we are currently trying to stop all threads, we
733 should treat the new thread as if we had sent it a SIGSTOP. This works
734 because we are guaranteed that the add_lwp call above added us to the
735 end of the list, and so the new thread has not yet reached
736 wait_for_sigstop (but will). */
737 new_lwp->stop_expected = 1;
738 }
739
740 void
741 linux_attach_lwp (unsigned long lwpid)
742 {
743 linux_attach_lwp_1 (lwpid, 0);
744 }
745
746 /* Attach to PID. If PID is the tgid, attach to it and all
747 of its threads. */
748
749 static int
750 linux_attach (unsigned long pid)
751 {
752 /* Attach to PID. We will check for other threads
753 soon. */
754 linux_attach_lwp_1 (pid, 1);
755 linux_add_process (pid, 1);
756
757 if (!non_stop)
758 {
759 struct thread_info *thread;
760
761 /* Don't ignore the initial SIGSTOP if we just attached to this
762 process. It will be collected by wait shortly. */
763 thread = find_thread_ptid (ptid_build (pid, pid, 0));
764 thread->last_resume_kind = resume_stop;
765 }
766
767 if (linux_proc_get_tgid (pid) == pid)
768 {
769 DIR *dir;
770 char pathname[128];
771
772 sprintf (pathname, "/proc/%ld/task", pid);
773
774 dir = opendir (pathname);
775
776 if (!dir)
777 {
778 fprintf (stderr, "Could not open /proc/%ld/task.\n", pid);
779 fflush (stderr);
780 }
781 else
782 {
783 /* At this point we attached to the tgid. Scan the task for
784 existing threads. */
785 unsigned long lwp;
786 int new_threads_found;
787 int iterations = 0;
788 struct dirent *dp;
789
790 while (iterations < 2)
791 {
792 new_threads_found = 0;
793 /* Add all the other threads. While we go through the
794 threads, new threads may be spawned. Cycle through
795 the list of threads until we have done two iterations without
796 finding new threads. */
797 while ((dp = readdir (dir)) != NULL)
798 {
799 /* Fetch one lwp. */
800 lwp = strtoul (dp->d_name, NULL, 10);
801
802 /* Is this a new thread? */
803 if (lwp
804 && find_thread_ptid (ptid_build (pid, lwp, 0)) == NULL)
805 {
806 linux_attach_lwp_1 (lwp, 0);
807 new_threads_found++;
808
809 if (debug_threads)
810 debug_printf ("Found and attached to new lwp %ld\n",
811 lwp);
812 }
813 }
814
815 if (!new_threads_found)
816 iterations++;
817 else
818 iterations = 0;
819
820 rewinddir (dir);
821 }
822 closedir (dir);
823 }
824 }
825
826 return 0;
827 }
828
829 struct counter
830 {
831 int pid;
832 int count;
833 };
834
835 static int
836 second_thread_of_pid_p (struct inferior_list_entry *entry, void *args)
837 {
838 struct counter *counter = args;
839
840 if (ptid_get_pid (entry->id) == counter->pid)
841 {
842 if (++counter->count > 1)
843 return 1;
844 }
845
846 return 0;
847 }
848
849 static int
850 last_thread_of_process_p (struct thread_info *thread)
851 {
852 ptid_t ptid = thread->entry.id;
853 int pid = ptid_get_pid (ptid);
854 struct counter counter = { pid , 0 };
855
856 return (find_inferior (&all_threads,
857 second_thread_of_pid_p, &counter) == NULL);
858 }
859
860 /* Kill LWP. */
861
862 static void
863 linux_kill_one_lwp (struct lwp_info *lwp)
864 {
865 struct thread_info *thr = get_lwp_thread (lwp);
866 int pid = lwpid_of (thr);
867
868 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
869 there is no signal context, and ptrace(PTRACE_KILL) (or
870 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
871 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
872 alternative is to kill with SIGKILL. We only need one SIGKILL
873 per process, not one for each thread. But since we still support
874 linuxthreads, and we also support debugging programs using raw
875 clone without CLONE_THREAD, we send one for each thread. For
876 years, we used PTRACE_KILL only, so we're being a bit paranoid
877 about some old kernels where PTRACE_KILL might work better
878 (dubious if there are any such, but that's why it's paranoia), so
879 we try SIGKILL first, PTRACE_KILL second, and so we're fine
880 everywhere. */
881
882 errno = 0;
883 kill (pid, SIGKILL);
884 if (debug_threads)
885 debug_printf ("LKL: kill (SIGKILL) %s, 0, 0 (%s)\n",
886 target_pid_to_str (ptid_of (thr)),
887 errno ? strerror (errno) : "OK");
888
889 errno = 0;
890 ptrace (PTRACE_KILL, pid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
891 if (debug_threads)
892 debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
893 target_pid_to_str (ptid_of (thr)),
894 errno ? strerror (errno) : "OK");
895 }
896
897 /* Callback for `find_inferior'. Kills an lwp of a given process,
898 except the leader. */
899
900 static int
901 kill_one_lwp_callback (struct inferior_list_entry *entry, void *args)
902 {
903 struct thread_info *thread = (struct thread_info *) entry;
904 struct lwp_info *lwp = get_thread_lwp (thread);
905 int wstat;
906 int pid = * (int *) args;
907
908 if (ptid_get_pid (entry->id) != pid)
909 return 0;
910
911 /* We avoid killing the first thread here, because of a Linux kernel (at
912 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
913 the children get a chance to be reaped, it will remain a zombie
914 forever. */
915
916 if (lwpid_of (thread) == pid)
917 {
918 if (debug_threads)
919 debug_printf ("lkop: is last of process %s\n",
920 target_pid_to_str (entry->id));
921 return 0;
922 }
923
924 do
925 {
926 linux_kill_one_lwp (lwp);
927
928 /* Make sure it died. The loop is most likely unnecessary. */
929 pid = linux_wait_for_event (thread->entry.id, &wstat, __WALL);
930 } while (pid > 0 && WIFSTOPPED (wstat));
931
932 return 0;
933 }
934
935 static int
936 linux_kill (int pid)
937 {
938 struct process_info *process;
939 struct lwp_info *lwp;
940 int wstat;
941 int lwpid;
942
943 process = find_process_pid (pid);
944 if (process == NULL)
945 return -1;
946
947 /* If we're killing a running inferior, make sure it is stopped
948 first, as PTRACE_KILL will not work otherwise. */
949 stop_all_lwps (0, NULL);
950
951 find_inferior (&all_threads, kill_one_lwp_callback , &pid);
952
953 /* See the comment in linux_kill_one_lwp. We did not kill the first
954 thread in the list, so do so now. */
955 lwp = find_lwp_pid (pid_to_ptid (pid));
956
957 if (lwp == NULL)
958 {
959 if (debug_threads)
960 debug_printf ("lk_1: cannot find lwp for pid: %d\n",
961 pid);
962 }
963 else
964 {
965 struct thread_info *thr = get_lwp_thread (lwp);
966
967 if (debug_threads)
968 debug_printf ("lk_1: killing lwp %ld, for pid: %d\n",
969 lwpid_of (thr), pid);
970
971 do
972 {
973 linux_kill_one_lwp (lwp);
974
975 /* Make sure it died. The loop is most likely unnecessary. */
976 lwpid = linux_wait_for_event (thr->entry.id, &wstat, __WALL);
977 } while (lwpid > 0 && WIFSTOPPED (wstat));
978 }
979
980 the_target->mourn (process);
981
982 /* Since we presently can only stop all lwps of all processes, we
983 need to unstop lwps of other processes. */
984 unstop_all_lwps (0, NULL);
985 return 0;
986 }
987
988 /* Get pending signal of THREAD, for detaching purposes. This is the
989 signal the thread last stopped for, which we need to deliver to the
990 thread when detaching, otherwise, it'd be suppressed/lost. */
991
992 static int
993 get_detach_signal (struct thread_info *thread)
994 {
995 enum gdb_signal signo = GDB_SIGNAL_0;
996 int status;
997 struct lwp_info *lp = get_thread_lwp (thread);
998
999 if (lp->status_pending_p)
1000 status = lp->status_pending;
1001 else
1002 {
1003 /* If the thread had been suspended by gdbserver, and it stopped
1004 cleanly, then it'll have stopped with SIGSTOP. But we don't
1005 want to deliver that SIGSTOP. */
1006 if (thread->last_status.kind != TARGET_WAITKIND_STOPPED
1007 || thread->last_status.value.sig == GDB_SIGNAL_0)
1008 return 0;
1009
1010 /* Otherwise, we may need to deliver the signal we
1011 intercepted. */
1012 status = lp->last_status;
1013 }
1014
1015 if (!WIFSTOPPED (status))
1016 {
1017 if (debug_threads)
1018 debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1019 target_pid_to_str (ptid_of (thread)));
1020 return 0;
1021 }
1022
1023 /* Extended wait statuses aren't real SIGTRAPs. */
1024 if (WSTOPSIG (status) == SIGTRAP && status >> 16 != 0)
1025 {
1026 if (debug_threads)
1027 debug_printf ("GPS: lwp %s had stopped with extended "
1028 "status: no pending signal\n",
1029 target_pid_to_str (ptid_of (thread)));
1030 return 0;
1031 }
1032
1033 signo = gdb_signal_from_host (WSTOPSIG (status));
1034
1035 if (program_signals_p && !program_signals[signo])
1036 {
1037 if (debug_threads)
1038 debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1039 target_pid_to_str (ptid_of (thread)),
1040 gdb_signal_to_string (signo));
1041 return 0;
1042 }
1043 else if (!program_signals_p
1044 /* If we have no way to know which signals GDB does not
1045 want to have passed to the program, assume
1046 SIGTRAP/SIGINT, which is GDB's default. */
1047 && (signo == GDB_SIGNAL_TRAP || signo == GDB_SIGNAL_INT))
1048 {
1049 if (debug_threads)
1050 debug_printf ("GPS: lwp %s had signal %s, "
1051 "but we don't know if we should pass it. "
1052 "Default to not.\n",
1053 target_pid_to_str (ptid_of (thread)),
1054 gdb_signal_to_string (signo));
1055 return 0;
1056 }
1057 else
1058 {
1059 if (debug_threads)
1060 debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1061 target_pid_to_str (ptid_of (thread)),
1062 gdb_signal_to_string (signo));
1063
1064 return WSTOPSIG (status);
1065 }
1066 }
1067
1068 static int
1069 linux_detach_one_lwp (struct inferior_list_entry *entry, void *args)
1070 {
1071 struct thread_info *thread = (struct thread_info *) entry;
1072 struct lwp_info *lwp = get_thread_lwp (thread);
1073 int pid = * (int *) args;
1074 int sig;
1075
1076 if (ptid_get_pid (entry->id) != pid)
1077 return 0;
1078
1079 /* If there is a pending SIGSTOP, get rid of it. */
1080 if (lwp->stop_expected)
1081 {
1082 if (debug_threads)
1083 debug_printf ("Sending SIGCONT to %s\n",
1084 target_pid_to_str (ptid_of (thread)));
1085
1086 kill_lwp (lwpid_of (thread), SIGCONT);
1087 lwp->stop_expected = 0;
1088 }
1089
1090 /* Flush any pending changes to the process's registers. */
1091 regcache_invalidate_thread (thread);
1092
1093 /* Pass on any pending signal for this thread. */
1094 sig = get_detach_signal (thread);
1095
1096 /* Finally, let it resume. */
1097 if (the_low_target.prepare_to_resume != NULL)
1098 the_low_target.prepare_to_resume (lwp);
1099 if (ptrace (PTRACE_DETACH, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
1100 (PTRACE_TYPE_ARG4) (long) sig) < 0)
1101 error (_("Can't detach %s: %s"),
1102 target_pid_to_str (ptid_of (thread)),
1103 strerror (errno));
1104
1105 delete_lwp (lwp);
1106 return 0;
1107 }
1108
1109 static int
1110 linux_detach (int pid)
1111 {
1112 struct process_info *process;
1113
1114 process = find_process_pid (pid);
1115 if (process == NULL)
1116 return -1;
1117
1118 /* Stop all threads before detaching. First, ptrace requires that
1119 the thread is stopped to sucessfully detach. Second, thread_db
1120 may need to uninstall thread event breakpoints from memory, which
1121 only works with a stopped process anyway. */
1122 stop_all_lwps (0, NULL);
1123
1124 #ifdef USE_THREAD_DB
1125 thread_db_detach (process);
1126 #endif
1127
1128 /* Stabilize threads (move out of jump pads). */
1129 stabilize_threads ();
1130
1131 find_inferior (&all_threads, linux_detach_one_lwp, &pid);
1132
1133 the_target->mourn (process);
1134
1135 /* Since we presently can only stop all lwps of all processes, we
1136 need to unstop lwps of other processes. */
1137 unstop_all_lwps (0, NULL);
1138 return 0;
1139 }
1140
1141 /* Remove all LWPs that belong to process PROC from the lwp list. */
1142
1143 static int
1144 delete_lwp_callback (struct inferior_list_entry *entry, void *proc)
1145 {
1146 struct thread_info *thread = (struct thread_info *) entry;
1147 struct lwp_info *lwp = get_thread_lwp (thread);
1148 struct process_info *process = proc;
1149
1150 if (pid_of (thread) == pid_of (process))
1151 delete_lwp (lwp);
1152
1153 return 0;
1154 }
1155
1156 static void
1157 linux_mourn (struct process_info *process)
1158 {
1159 struct process_info_private *priv;
1160
1161 #ifdef USE_THREAD_DB
1162 thread_db_mourn (process);
1163 #endif
1164
1165 find_inferior (&all_threads, delete_lwp_callback, process);
1166
1167 /* Freeing all private data. */
1168 priv = process->private;
1169 free (priv->arch_private);
1170 free (priv);
1171 process->private = NULL;
1172
1173 remove_process (process);
1174 }
1175
1176 static void
1177 linux_join (int pid)
1178 {
1179 int status, ret;
1180
1181 do {
1182 ret = my_waitpid (pid, &status, 0);
1183 if (WIFEXITED (status) || WIFSIGNALED (status))
1184 break;
1185 } while (ret != -1 || errno != ECHILD);
1186 }
1187
1188 /* Return nonzero if the given thread is still alive. */
1189 static int
1190 linux_thread_alive (ptid_t ptid)
1191 {
1192 struct lwp_info *lwp = find_lwp_pid (ptid);
1193
1194 /* We assume we always know if a thread exits. If a whole process
1195 exited but we still haven't been able to report it to GDB, we'll
1196 hold on to the last lwp of the dead process. */
1197 if (lwp != NULL)
1198 return !lwp->dead;
1199 else
1200 return 0;
1201 }
1202
1203 /* Return 1 if this lwp has an interesting status pending. */
1204 static int
1205 status_pending_p_callback (struct inferior_list_entry *entry, void *arg)
1206 {
1207 struct thread_info *thread = (struct thread_info *) entry;
1208 struct lwp_info *lwp = get_thread_lwp (thread);
1209 ptid_t ptid = * (ptid_t *) arg;
1210
1211 /* Check if we're only interested in events from a specific process
1212 or its lwps. */
1213 if (!ptid_equal (minus_one_ptid, ptid)
1214 && ptid_get_pid (ptid) != ptid_get_pid (thread->entry.id))
1215 return 0;
1216
1217 /* If we got a `vCont;t', but we haven't reported a stop yet, do
1218 report any status pending the LWP may have. */
1219 if (thread->last_resume_kind == resume_stop
1220 && thread->last_status.kind != TARGET_WAITKIND_IGNORE)
1221 return 0;
1222
1223 return lwp->status_pending_p;
1224 }
1225
1226 static int
1227 same_lwp (struct inferior_list_entry *entry, void *data)
1228 {
1229 ptid_t ptid = *(ptid_t *) data;
1230 int lwp;
1231
1232 if (ptid_get_lwp (ptid) != 0)
1233 lwp = ptid_get_lwp (ptid);
1234 else
1235 lwp = ptid_get_pid (ptid);
1236
1237 if (ptid_get_lwp (entry->id) == lwp)
1238 return 1;
1239
1240 return 0;
1241 }
1242
1243 struct lwp_info *
1244 find_lwp_pid (ptid_t ptid)
1245 {
1246 struct inferior_list_entry *thread
1247 = find_inferior (&all_threads, same_lwp, &ptid);
1248
1249 if (thread == NULL)
1250 return NULL;
1251
1252 return get_thread_lwp ((struct thread_info *) thread);
1253 }
1254
1255 static struct lwp_info *
1256 linux_wait_for_lwp (ptid_t ptid, int *wstatp, int options)
1257 {
1258 int ret;
1259 int to_wait_for = -1;
1260 struct lwp_info *child;
1261 struct thread_info *thread;
1262
1263 if (debug_threads)
1264 debug_printf ("linux_wait_for_lwp: %s\n", target_pid_to_str (ptid));
1265
1266 if (ptid_equal (ptid, minus_one_ptid))
1267 to_wait_for = -1; /* any child */
1268 else
1269 to_wait_for = ptid_get_lwp (ptid); /* this lwp only */
1270
1271 options |= __WALL;
1272
1273 retry:
1274
1275 ret = my_waitpid (to_wait_for, wstatp, options);
1276 if (ret == 0 || (ret == -1 && errno == ECHILD && (options & WNOHANG)))
1277 return NULL;
1278 else if (ret == -1)
1279 perror_with_name ("waitpid");
1280
1281 if (debug_threads
1282 && (!WIFSTOPPED (*wstatp)
1283 || (WSTOPSIG (*wstatp) != 32
1284 && WSTOPSIG (*wstatp) != 33)))
1285 debug_printf ("Got an event from %d (%x)\n", ret, *wstatp);
1286
1287 child = find_lwp_pid (pid_to_ptid (ret));
1288 if (child != NULL)
1289 thread = get_lwp_thread (child);
1290 else
1291 thread = NULL;
1292
1293 /* If we didn't find a process, one of two things presumably happened:
1294 - A process we started and then detached from has exited. Ignore it.
1295 - A process we are controlling has forked and the new child's stop
1296 was reported to us by the kernel. Save its PID. */
1297 if (child == NULL && WIFSTOPPED (*wstatp))
1298 {
1299 add_to_pid_list (&stopped_pids, ret, *wstatp);
1300 goto retry;
1301 }
1302 else if (child == NULL)
1303 goto retry;
1304
1305 child->stopped = 1;
1306
1307 child->last_status = *wstatp;
1308
1309 if (WIFSTOPPED (*wstatp))
1310 {
1311 struct process_info *proc;
1312
1313 /* Architecture-specific setup after inferior is running. This
1314 needs to happen after we have attached to the inferior and it
1315 is stopped for the first time, but before we access any
1316 inferior registers. */
1317 proc = find_process_pid (pid_of (thread));
1318 if (proc->private->new_inferior)
1319 {
1320 struct thread_info *saved_inferior;
1321
1322 saved_inferior = current_inferior;
1323 current_inferior = thread;
1324
1325 the_low_target.arch_setup ();
1326
1327 current_inferior = saved_inferior;
1328
1329 proc->private->new_inferior = 0;
1330 }
1331 }
1332
1333 /* Fetch the possibly triggered data watchpoint info and store it in
1334 CHILD.
1335
1336 On some archs, like x86, that use debug registers to set
1337 watchpoints, it's possible that the way to know which watched
1338 address trapped, is to check the register that is used to select
1339 which address to watch. Problem is, between setting the
1340 watchpoint and reading back which data address trapped, the user
1341 may change the set of watchpoints, and, as a consequence, GDB
1342 changes the debug registers in the inferior. To avoid reading
1343 back a stale stopped-data-address when that happens, we cache in
1344 LP the fact that a watchpoint trapped, and the corresponding data
1345 address, as soon as we see CHILD stop with a SIGTRAP. If GDB
1346 changes the debug registers meanwhile, we have the cached data we
1347 can rely on. */
1348
1349 if (WIFSTOPPED (*wstatp) && WSTOPSIG (*wstatp) == SIGTRAP)
1350 {
1351 if (the_low_target.stopped_by_watchpoint == NULL)
1352 {
1353 child->stopped_by_watchpoint = 0;
1354 }
1355 else
1356 {
1357 struct thread_info *saved_inferior;
1358
1359 saved_inferior = current_inferior;
1360 current_inferior = get_lwp_thread (child);
1361
1362 child->stopped_by_watchpoint
1363 = the_low_target.stopped_by_watchpoint ();
1364
1365 if (child->stopped_by_watchpoint)
1366 {
1367 if (the_low_target.stopped_data_address != NULL)
1368 child->stopped_data_address
1369 = the_low_target.stopped_data_address ();
1370 else
1371 child->stopped_data_address = 0;
1372 }
1373
1374 current_inferior = saved_inferior;
1375 }
1376 }
1377
1378 /* Store the STOP_PC, with adjustment applied. This depends on the
1379 architecture being defined already (so that CHILD has a valid
1380 regcache), and on LAST_STATUS being set (to check for SIGTRAP or
1381 not). */
1382 if (WIFSTOPPED (*wstatp))
1383 child->stop_pc = get_stop_pc (child);
1384
1385 if (debug_threads
1386 && WIFSTOPPED (*wstatp)
1387 && the_low_target.get_pc != NULL)
1388 {
1389 struct thread_info *saved_inferior = current_inferior;
1390 struct regcache *regcache;
1391 CORE_ADDR pc;
1392
1393 current_inferior = get_lwp_thread (child);
1394 regcache = get_thread_regcache (current_inferior, 1);
1395 pc = (*the_low_target.get_pc) (regcache);
1396 debug_printf ("linux_wait_for_lwp: pc is 0x%lx\n", (long) pc);
1397 current_inferior = saved_inferior;
1398 }
1399
1400 return child;
1401 }
1402
1403 /* This function should only be called if the LWP got a SIGTRAP.
1404
1405 Handle any tracepoint steps or hits. Return true if a tracepoint
1406 event was handled, 0 otherwise. */
1407
1408 static int
1409 handle_tracepoints (struct lwp_info *lwp)
1410 {
1411 struct thread_info *tinfo = get_lwp_thread (lwp);
1412 int tpoint_related_event = 0;
1413
1414 /* If this tracepoint hit causes a tracing stop, we'll immediately
1415 uninsert tracepoints. To do this, we temporarily pause all
1416 threads, unpatch away, and then unpause threads. We need to make
1417 sure the unpausing doesn't resume LWP too. */
1418 lwp->suspended++;
1419
1420 /* And we need to be sure that any all-threads-stopping doesn't try
1421 to move threads out of the jump pads, as it could deadlock the
1422 inferior (LWP could be in the jump pad, maybe even holding the
1423 lock.) */
1424
1425 /* Do any necessary step collect actions. */
1426 tpoint_related_event |= tracepoint_finished_step (tinfo, lwp->stop_pc);
1427
1428 tpoint_related_event |= handle_tracepoint_bkpts (tinfo, lwp->stop_pc);
1429
1430 /* See if we just hit a tracepoint and do its main collect
1431 actions. */
1432 tpoint_related_event |= tracepoint_was_hit (tinfo, lwp->stop_pc);
1433
1434 lwp->suspended--;
1435
1436 gdb_assert (lwp->suspended == 0);
1437 gdb_assert (!stabilizing_threads || lwp->collecting_fast_tracepoint);
1438
1439 if (tpoint_related_event)
1440 {
1441 if (debug_threads)
1442 debug_printf ("got a tracepoint event\n");
1443 return 1;
1444 }
1445
1446 return 0;
1447 }
1448
1449 /* Convenience wrapper. Returns true if LWP is presently collecting a
1450 fast tracepoint. */
1451
1452 static int
1453 linux_fast_tracepoint_collecting (struct lwp_info *lwp,
1454 struct fast_tpoint_collect_status *status)
1455 {
1456 CORE_ADDR thread_area;
1457 struct thread_info *thread = get_lwp_thread (lwp);
1458
1459 if (the_low_target.get_thread_area == NULL)
1460 return 0;
1461
1462 /* Get the thread area address. This is used to recognize which
1463 thread is which when tracing with the in-process agent library.
1464 We don't read anything from the address, and treat it as opaque;
1465 it's the address itself that we assume is unique per-thread. */
1466 if ((*the_low_target.get_thread_area) (lwpid_of (thread), &thread_area) == -1)
1467 return 0;
1468
1469 return fast_tracepoint_collecting (thread_area, lwp->stop_pc, status);
1470 }
1471
1472 /* The reason we resume in the caller, is because we want to be able
1473 to pass lwp->status_pending as WSTAT, and we need to clear
1474 status_pending_p before resuming, otherwise, linux_resume_one_lwp
1475 refuses to resume. */
1476
1477 static int
1478 maybe_move_out_of_jump_pad (struct lwp_info *lwp, int *wstat)
1479 {
1480 struct thread_info *saved_inferior;
1481
1482 saved_inferior = current_inferior;
1483 current_inferior = get_lwp_thread (lwp);
1484
1485 if ((wstat == NULL
1486 || (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) != SIGTRAP))
1487 && supports_fast_tracepoints ()
1488 && agent_loaded_p ())
1489 {
1490 struct fast_tpoint_collect_status status;
1491 int r;
1492
1493 if (debug_threads)
1494 debug_printf ("Checking whether LWP %ld needs to move out of the "
1495 "jump pad.\n",
1496 lwpid_of (current_inferior));
1497
1498 r = linux_fast_tracepoint_collecting (lwp, &status);
1499
1500 if (wstat == NULL
1501 || (WSTOPSIG (*wstat) != SIGILL
1502 && WSTOPSIG (*wstat) != SIGFPE
1503 && WSTOPSIG (*wstat) != SIGSEGV
1504 && WSTOPSIG (*wstat) != SIGBUS))
1505 {
1506 lwp->collecting_fast_tracepoint = r;
1507
1508 if (r != 0)
1509 {
1510 if (r == 1 && lwp->exit_jump_pad_bkpt == NULL)
1511 {
1512 /* Haven't executed the original instruction yet.
1513 Set breakpoint there, and wait till it's hit,
1514 then single-step until exiting the jump pad. */
1515 lwp->exit_jump_pad_bkpt
1516 = set_breakpoint_at (status.adjusted_insn_addr, NULL);
1517 }
1518
1519 if (debug_threads)
1520 debug_printf ("Checking whether LWP %ld needs to move out of "
1521 "the jump pad...it does\n",
1522 lwpid_of (current_inferior));
1523 current_inferior = saved_inferior;
1524
1525 return 1;
1526 }
1527 }
1528 else
1529 {
1530 /* If we get a synchronous signal while collecting, *and*
1531 while executing the (relocated) original instruction,
1532 reset the PC to point at the tpoint address, before
1533 reporting to GDB. Otherwise, it's an IPA lib bug: just
1534 report the signal to GDB, and pray for the best. */
1535
1536 lwp->collecting_fast_tracepoint = 0;
1537
1538 if (r != 0
1539 && (status.adjusted_insn_addr <= lwp->stop_pc
1540 && lwp->stop_pc < status.adjusted_insn_addr_end))
1541 {
1542 siginfo_t info;
1543 struct regcache *regcache;
1544
1545 /* The si_addr on a few signals references the address
1546 of the faulting instruction. Adjust that as
1547 well. */
1548 if ((WSTOPSIG (*wstat) == SIGILL
1549 || WSTOPSIG (*wstat) == SIGFPE
1550 || WSTOPSIG (*wstat) == SIGBUS
1551 || WSTOPSIG (*wstat) == SIGSEGV)
1552 && ptrace (PTRACE_GETSIGINFO, lwpid_of (current_inferior),
1553 (PTRACE_TYPE_ARG3) 0, &info) == 0
1554 /* Final check just to make sure we don't clobber
1555 the siginfo of non-kernel-sent signals. */
1556 && (uintptr_t) info.si_addr == lwp->stop_pc)
1557 {
1558 info.si_addr = (void *) (uintptr_t) status.tpoint_addr;
1559 ptrace (PTRACE_SETSIGINFO, lwpid_of (current_inferior),
1560 (PTRACE_TYPE_ARG3) 0, &info);
1561 }
1562
1563 regcache = get_thread_regcache (current_inferior, 1);
1564 (*the_low_target.set_pc) (regcache, status.tpoint_addr);
1565 lwp->stop_pc = status.tpoint_addr;
1566
1567 /* Cancel any fast tracepoint lock this thread was
1568 holding. */
1569 force_unlock_trace_buffer ();
1570 }
1571
1572 if (lwp->exit_jump_pad_bkpt != NULL)
1573 {
1574 if (debug_threads)
1575 debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
1576 "stopping all threads momentarily.\n");
1577
1578 stop_all_lwps (1, lwp);
1579 cancel_breakpoints ();
1580
1581 delete_breakpoint (lwp->exit_jump_pad_bkpt);
1582 lwp->exit_jump_pad_bkpt = NULL;
1583
1584 unstop_all_lwps (1, lwp);
1585
1586 gdb_assert (lwp->suspended >= 0);
1587 }
1588 }
1589 }
1590
1591 if (debug_threads)
1592 debug_printf ("Checking whether LWP %ld needs to move out of the "
1593 "jump pad...no\n",
1594 lwpid_of (current_inferior));
1595
1596 current_inferior = saved_inferior;
1597 return 0;
1598 }
1599
1600 /* Enqueue one signal in the "signals to report later when out of the
1601 jump pad" list. */
1602
1603 static void
1604 enqueue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
1605 {
1606 struct pending_signals *p_sig;
1607 struct thread_info *thread = get_lwp_thread (lwp);
1608
1609 if (debug_threads)
1610 debug_printf ("Deferring signal %d for LWP %ld.\n",
1611 WSTOPSIG (*wstat), lwpid_of (thread));
1612
1613 if (debug_threads)
1614 {
1615 struct pending_signals *sig;
1616
1617 for (sig = lwp->pending_signals_to_report;
1618 sig != NULL;
1619 sig = sig->prev)
1620 debug_printf (" Already queued %d\n",
1621 sig->signal);
1622
1623 debug_printf (" (no more currently queued signals)\n");
1624 }
1625
1626 /* Don't enqueue non-RT signals if they are already in the deferred
1627 queue. (SIGSTOP being the easiest signal to see ending up here
1628 twice) */
1629 if (WSTOPSIG (*wstat) < __SIGRTMIN)
1630 {
1631 struct pending_signals *sig;
1632
1633 for (sig = lwp->pending_signals_to_report;
1634 sig != NULL;
1635 sig = sig->prev)
1636 {
1637 if (sig->signal == WSTOPSIG (*wstat))
1638 {
1639 if (debug_threads)
1640 debug_printf ("Not requeuing already queued non-RT signal %d"
1641 " for LWP %ld\n",
1642 sig->signal,
1643 lwpid_of (thread));
1644 return;
1645 }
1646 }
1647 }
1648
1649 p_sig = xmalloc (sizeof (*p_sig));
1650 p_sig->prev = lwp->pending_signals_to_report;
1651 p_sig->signal = WSTOPSIG (*wstat);
1652 memset (&p_sig->info, 0, sizeof (siginfo_t));
1653 ptrace (PTRACE_GETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
1654 &p_sig->info);
1655
1656 lwp->pending_signals_to_report = p_sig;
1657 }
1658
1659 /* Dequeue one signal from the "signals to report later when out of
1660 the jump pad" list. */
1661
1662 static int
1663 dequeue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
1664 {
1665 struct thread_info *thread = get_lwp_thread (lwp);
1666
1667 if (lwp->pending_signals_to_report != NULL)
1668 {
1669 struct pending_signals **p_sig;
1670
1671 p_sig = &lwp->pending_signals_to_report;
1672 while ((*p_sig)->prev != NULL)
1673 p_sig = &(*p_sig)->prev;
1674
1675 *wstat = W_STOPCODE ((*p_sig)->signal);
1676 if ((*p_sig)->info.si_signo != 0)
1677 ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
1678 &(*p_sig)->info);
1679 free (*p_sig);
1680 *p_sig = NULL;
1681
1682 if (debug_threads)
1683 debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
1684 WSTOPSIG (*wstat), lwpid_of (thread));
1685
1686 if (debug_threads)
1687 {
1688 struct pending_signals *sig;
1689
1690 for (sig = lwp->pending_signals_to_report;
1691 sig != NULL;
1692 sig = sig->prev)
1693 debug_printf (" Still queued %d\n",
1694 sig->signal);
1695
1696 debug_printf (" (no more queued signals)\n");
1697 }
1698
1699 return 1;
1700 }
1701
1702 return 0;
1703 }
1704
1705 /* Arrange for a breakpoint to be hit again later. We don't keep the
1706 SIGTRAP status and don't forward the SIGTRAP signal to the LWP. We
1707 will handle the current event, eventually we will resume this LWP,
1708 and this breakpoint will trap again. */
1709
1710 static int
1711 cancel_breakpoint (struct lwp_info *lwp)
1712 {
1713 struct thread_info *saved_inferior;
1714
1715 /* There's nothing to do if we don't support breakpoints. */
1716 if (!supports_breakpoints ())
1717 return 0;
1718
1719 /* breakpoint_at reads from current inferior. */
1720 saved_inferior = current_inferior;
1721 current_inferior = get_lwp_thread (lwp);
1722
1723 if ((*the_low_target.breakpoint_at) (lwp->stop_pc))
1724 {
1725 if (debug_threads)
1726 debug_printf ("CB: Push back breakpoint for %s\n",
1727 target_pid_to_str (ptid_of (current_inferior)));
1728
1729 /* Back up the PC if necessary. */
1730 if (the_low_target.decr_pc_after_break)
1731 {
1732 struct regcache *regcache
1733 = get_thread_regcache (current_inferior, 1);
1734 (*the_low_target.set_pc) (regcache, lwp->stop_pc);
1735 }
1736
1737 current_inferior = saved_inferior;
1738 return 1;
1739 }
1740 else
1741 {
1742 if (debug_threads)
1743 debug_printf ("CB: No breakpoint found at %s for [%s]\n",
1744 paddress (lwp->stop_pc),
1745 target_pid_to_str (ptid_of (current_inferior)));
1746 }
1747
1748 current_inferior = saved_inferior;
1749 return 0;
1750 }
1751
1752 /* When the event-loop is doing a step-over, this points at the thread
1753 being stepped. */
1754 ptid_t step_over_bkpt;
1755
1756 /* Wait for an event from child PID. If PID is -1, wait for any
1757 child. Store the stop status through the status pointer WSTAT.
1758 OPTIONS is passed to the waitpid call. Return 0 if no child stop
1759 event was found and OPTIONS contains WNOHANG. Return the PID of
1760 the stopped child and update current_inferior otherwise. */
1761
1762 static int
1763 linux_wait_for_event (ptid_t ptid, int *wstat, int options)
1764 {
1765 struct thread_info *event_thread;
1766 struct lwp_info *event_child, *requested_child;
1767 ptid_t wait_ptid;
1768
1769 /* N.B. event_thread points to the thread_info struct that contains
1770 event_child. Keep them in sync. */
1771 event_thread = NULL;
1772 event_child = NULL;
1773 requested_child = NULL;
1774
1775 /* Check for a lwp with a pending status. */
1776
1777 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
1778 {
1779 event_thread = (struct thread_info *)
1780 find_inferior (&all_threads, status_pending_p_callback, &ptid);
1781 if (event_thread != NULL)
1782 event_child = get_thread_lwp (event_thread);
1783 if (debug_threads && event_thread)
1784 debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread));
1785 }
1786 else
1787 {
1788 requested_child = find_lwp_pid (ptid);
1789
1790 if (stopping_threads == NOT_STOPPING_THREADS
1791 && requested_child->status_pending_p
1792 && requested_child->collecting_fast_tracepoint)
1793 {
1794 enqueue_one_deferred_signal (requested_child,
1795 &requested_child->status_pending);
1796 requested_child->status_pending_p = 0;
1797 requested_child->status_pending = 0;
1798 linux_resume_one_lwp (requested_child, 0, 0, NULL);
1799 }
1800
1801 if (requested_child->suspended
1802 && requested_child->status_pending_p)
1803 fatal ("requesting an event out of a suspended child?");
1804
1805 if (requested_child->status_pending_p)
1806 {
1807 event_child = requested_child;
1808 event_thread = get_lwp_thread (event_child);
1809 }
1810 }
1811
1812 if (event_child != NULL)
1813 {
1814 if (debug_threads)
1815 debug_printf ("Got an event from pending child %ld (%04x)\n",
1816 lwpid_of (event_thread), event_child->status_pending);
1817 *wstat = event_child->status_pending;
1818 event_child->status_pending_p = 0;
1819 event_child->status_pending = 0;
1820 current_inferior = event_thread;
1821 return lwpid_of (event_thread);
1822 }
1823
1824 if (ptid_is_pid (ptid))
1825 {
1826 /* A request to wait for a specific tgid. This is not possible
1827 with waitpid, so instead, we wait for any child, and leave
1828 children we're not interested in right now with a pending
1829 status to report later. */
1830 wait_ptid = minus_one_ptid;
1831 }
1832 else
1833 wait_ptid = ptid;
1834
1835 /* We only enter this loop if no process has a pending wait status. Thus
1836 any action taken in response to a wait status inside this loop is
1837 responding as soon as we detect the status, not after any pending
1838 events. */
1839 while (1)
1840 {
1841 event_child = linux_wait_for_lwp (wait_ptid, wstat, options);
1842
1843 if ((options & WNOHANG) && event_child == NULL)
1844 {
1845 if (debug_threads)
1846 debug_printf ("WNOHANG set, no event found\n");
1847 return 0;
1848 }
1849
1850 if (event_child == NULL)
1851 error ("event from unknown child");
1852
1853 event_thread = get_lwp_thread (event_child);
1854 if (ptid_is_pid (ptid)
1855 && ptid_get_pid (ptid) != ptid_get_pid (ptid_of (event_thread)))
1856 {
1857 if (! WIFSTOPPED (*wstat))
1858 mark_lwp_dead (event_child, *wstat);
1859 else
1860 {
1861 event_child->status_pending_p = 1;
1862 event_child->status_pending = *wstat;
1863 }
1864 continue;
1865 }
1866
1867 current_inferior = event_thread;
1868
1869 /* Check for thread exit. */
1870 if (! WIFSTOPPED (*wstat))
1871 {
1872 if (debug_threads)
1873 debug_printf ("LWP %ld exiting\n", lwpid_of (event_thread));
1874
1875 /* If the last thread is exiting, just return. */
1876 if (last_thread_of_process_p (current_inferior))
1877 {
1878 if (debug_threads)
1879 debug_printf ("LWP %ld is last lwp of process\n",
1880 lwpid_of (event_thread));
1881 return lwpid_of (event_thread);
1882 }
1883
1884 if (!non_stop)
1885 {
1886 current_inferior = get_first_thread ();
1887 if (debug_threads)
1888 debug_printf ("Current inferior is now %ld\n",
1889 lwpid_of (current_inferior));
1890 }
1891 else
1892 {
1893 current_inferior = NULL;
1894 if (debug_threads)
1895 debug_printf ("Current inferior is now <NULL>\n");
1896 }
1897
1898 /* If we were waiting for this particular child to do something...
1899 well, it did something. */
1900 if (requested_child != NULL)
1901 {
1902 int lwpid = lwpid_of (event_thread);
1903
1904 /* Cancel the step-over operation --- the thread that
1905 started it is gone. */
1906 if (finish_step_over (event_child))
1907 unstop_all_lwps (1, event_child);
1908 delete_lwp (event_child);
1909 return lwpid;
1910 }
1911
1912 delete_lwp (event_child);
1913
1914 /* Wait for a more interesting event. */
1915 continue;
1916 }
1917
1918 if (event_child->must_set_ptrace_flags)
1919 {
1920 linux_enable_event_reporting (lwpid_of (event_thread));
1921 event_child->must_set_ptrace_flags = 0;
1922 }
1923
1924 if (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) == SIGTRAP
1925 && *wstat >> 16 != 0)
1926 {
1927 handle_extended_wait (event_child, *wstat);
1928 continue;
1929 }
1930
1931 if (WIFSTOPPED (*wstat)
1932 && WSTOPSIG (*wstat) == SIGSTOP
1933 && event_child->stop_expected)
1934 {
1935 int should_stop;
1936
1937 if (debug_threads)
1938 debug_printf ("Expected stop.\n");
1939 event_child->stop_expected = 0;
1940
1941 should_stop = (current_inferior->last_resume_kind == resume_stop
1942 || stopping_threads != NOT_STOPPING_THREADS);
1943
1944 if (!should_stop)
1945 {
1946 linux_resume_one_lwp (event_child,
1947 event_child->stepping, 0, NULL);
1948 continue;
1949 }
1950 }
1951
1952 return lwpid_of (event_thread);
1953 }
1954
1955 /* NOTREACHED */
1956 return 0;
1957 }
1958
1959 /* Count the LWP's that have had events. */
1960
1961 static int
1962 count_events_callback (struct inferior_list_entry *entry, void *data)
1963 {
1964 struct thread_info *thread = (struct thread_info *) entry;
1965 struct lwp_info *lp = get_thread_lwp (thread);
1966 int *count = data;
1967
1968 gdb_assert (count != NULL);
1969
1970 /* Count only resumed LWPs that have a SIGTRAP event pending that
1971 should be reported to GDB. */
1972 if (thread->last_status.kind == TARGET_WAITKIND_IGNORE
1973 && thread->last_resume_kind != resume_stop
1974 && lp->status_pending_p
1975 && WIFSTOPPED (lp->status_pending)
1976 && WSTOPSIG (lp->status_pending) == SIGTRAP
1977 && !breakpoint_inserted_here (lp->stop_pc))
1978 (*count)++;
1979
1980 return 0;
1981 }
1982
1983 /* Select the LWP (if any) that is currently being single-stepped. */
1984
1985 static int
1986 select_singlestep_lwp_callback (struct inferior_list_entry *entry, void *data)
1987 {
1988 struct thread_info *thread = (struct thread_info *) entry;
1989 struct lwp_info *lp = get_thread_lwp (thread);
1990
1991 if (thread->last_status.kind == TARGET_WAITKIND_IGNORE
1992 && thread->last_resume_kind == resume_step
1993 && lp->status_pending_p)
1994 return 1;
1995 else
1996 return 0;
1997 }
1998
1999 /* Select the Nth LWP that has had a SIGTRAP event that should be
2000 reported to GDB. */
2001
2002 static int
2003 select_event_lwp_callback (struct inferior_list_entry *entry, void *data)
2004 {
2005 struct thread_info *thread = (struct thread_info *) entry;
2006 struct lwp_info *lp = get_thread_lwp (thread);
2007 int *selector = data;
2008
2009 gdb_assert (selector != NULL);
2010
2011 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2012 if (thread->last_resume_kind != resume_stop
2013 && thread->last_status.kind == TARGET_WAITKIND_IGNORE
2014 && lp->status_pending_p
2015 && WIFSTOPPED (lp->status_pending)
2016 && WSTOPSIG (lp->status_pending) == SIGTRAP
2017 && !breakpoint_inserted_here (lp->stop_pc))
2018 if ((*selector)-- == 0)
2019 return 1;
2020
2021 return 0;
2022 }
2023
2024 static int
2025 cancel_breakpoints_callback (struct inferior_list_entry *entry, void *data)
2026 {
2027 struct thread_info *thread = (struct thread_info *) entry;
2028 struct lwp_info *lp = get_thread_lwp (thread);
2029 struct lwp_info *event_lp = data;
2030
2031 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2032 if (lp == event_lp)
2033 return 0;
2034
2035 /* If a LWP other than the LWP that we're reporting an event for has
2036 hit a GDB breakpoint (as opposed to some random trap signal),
2037 then just arrange for it to hit it again later. We don't keep
2038 the SIGTRAP status and don't forward the SIGTRAP signal to the
2039 LWP. We will handle the current event, eventually we will resume
2040 all LWPs, and this one will get its breakpoint trap again.
2041
2042 If we do not do this, then we run the risk that the user will
2043 delete or disable the breakpoint, but the LWP will have already
2044 tripped on it. */
2045
2046 if (thread->last_resume_kind != resume_stop
2047 && thread->last_status.kind == TARGET_WAITKIND_IGNORE
2048 && lp->status_pending_p
2049 && WIFSTOPPED (lp->status_pending)
2050 && WSTOPSIG (lp->status_pending) == SIGTRAP
2051 && !lp->stepping
2052 && !lp->stopped_by_watchpoint
2053 && cancel_breakpoint (lp))
2054 /* Throw away the SIGTRAP. */
2055 lp->status_pending_p = 0;
2056
2057 return 0;
2058 }
2059
2060 static void
2061 linux_cancel_breakpoints (void)
2062 {
2063 find_inferior (&all_threads, cancel_breakpoints_callback, NULL);
2064 }
2065
2066 /* Select one LWP out of those that have events pending. */
2067
2068 static void
2069 select_event_lwp (struct lwp_info **orig_lp)
2070 {
2071 int num_events = 0;
2072 int random_selector;
2073 struct thread_info *event_thread;
2074
2075 /* Give preference to any LWP that is being single-stepped. */
2076 event_thread
2077 = (struct thread_info *) find_inferior (&all_threads,
2078 select_singlestep_lwp_callback,
2079 NULL);
2080 if (event_thread != NULL)
2081 {
2082 if (debug_threads)
2083 debug_printf ("SEL: Select single-step %s\n",
2084 target_pid_to_str (ptid_of (event_thread)));
2085 }
2086 else
2087 {
2088 /* No single-stepping LWP. Select one at random, out of those
2089 which have had SIGTRAP events. */
2090
2091 /* First see how many SIGTRAP events we have. */
2092 find_inferior (&all_threads, count_events_callback, &num_events);
2093
2094 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2095 random_selector = (int)
2096 ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
2097
2098 if (debug_threads && num_events > 1)
2099 debug_printf ("SEL: Found %d SIGTRAP events, selecting #%d\n",
2100 num_events, random_selector);
2101
2102 event_thread
2103 = (struct thread_info *) find_inferior (&all_threads,
2104 select_event_lwp_callback,
2105 &random_selector);
2106 }
2107
2108 if (event_thread != NULL)
2109 {
2110 struct lwp_info *event_lp = get_thread_lwp (event_thread);
2111
2112 /* Switch the event LWP. */
2113 *orig_lp = event_lp;
2114 }
2115 }
2116
2117 /* Decrement the suspend count of an LWP. */
2118
2119 static int
2120 unsuspend_one_lwp (struct inferior_list_entry *entry, void *except)
2121 {
2122 struct thread_info *thread = (struct thread_info *) entry;
2123 struct lwp_info *lwp = get_thread_lwp (thread);
2124
2125 /* Ignore EXCEPT. */
2126 if (lwp == except)
2127 return 0;
2128
2129 lwp->suspended--;
2130
2131 gdb_assert (lwp->suspended >= 0);
2132 return 0;
2133 }
2134
2135 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2136 NULL. */
2137
2138 static void
2139 unsuspend_all_lwps (struct lwp_info *except)
2140 {
2141 find_inferior (&all_threads, unsuspend_one_lwp, except);
2142 }
2143
2144 static void move_out_of_jump_pad_callback (struct inferior_list_entry *entry);
2145 static int stuck_in_jump_pad_callback (struct inferior_list_entry *entry,
2146 void *data);
2147 static int lwp_running (struct inferior_list_entry *entry, void *data);
2148 static ptid_t linux_wait_1 (ptid_t ptid,
2149 struct target_waitstatus *ourstatus,
2150 int target_options);
2151
2152 /* Stabilize threads (move out of jump pads).
2153
2154 If a thread is midway collecting a fast tracepoint, we need to
2155 finish the collection and move it out of the jump pad before
2156 reporting the signal.
2157
2158 This avoids recursion while collecting (when a signal arrives
2159 midway, and the signal handler itself collects), which would trash
2160 the trace buffer. In case the user set a breakpoint in a signal
2161 handler, this avoids the backtrace showing the jump pad, etc..
2162 Most importantly, there are certain things we can't do safely if
2163 threads are stopped in a jump pad (or in its callee's). For
2164 example:
2165
2166 - starting a new trace run. A thread still collecting the
2167 previous run, could trash the trace buffer when resumed. The trace
2168 buffer control structures would have been reset but the thread had
2169 no way to tell. The thread could even midway memcpy'ing to the
2170 buffer, which would mean that when resumed, it would clobber the
2171 trace buffer that had been set for a new run.
2172
2173 - we can't rewrite/reuse the jump pads for new tracepoints
2174 safely. Say you do tstart while a thread is stopped midway while
2175 collecting. When the thread is later resumed, it finishes the
2176 collection, and returns to the jump pad, to execute the original
2177 instruction that was under the tracepoint jump at the time the
2178 older run had been started. If the jump pad had been rewritten
2179 since for something else in the new run, the thread would now
2180 execute the wrong / random instructions. */
2181
2182 static void
2183 linux_stabilize_threads (void)
2184 {
2185 struct thread_info *save_inferior;
2186 struct thread_info *thread_stuck;
2187
2188 thread_stuck
2189 = (struct thread_info *) find_inferior (&all_threads,
2190 stuck_in_jump_pad_callback,
2191 NULL);
2192 if (thread_stuck != NULL)
2193 {
2194 if (debug_threads)
2195 debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
2196 lwpid_of (thread_stuck));
2197 return;
2198 }
2199
2200 save_inferior = current_inferior;
2201
2202 stabilizing_threads = 1;
2203
2204 /* Kick 'em all. */
2205 for_each_inferior (&all_threads, move_out_of_jump_pad_callback);
2206
2207 /* Loop until all are stopped out of the jump pads. */
2208 while (find_inferior (&all_threads, lwp_running, NULL) != NULL)
2209 {
2210 struct target_waitstatus ourstatus;
2211 struct lwp_info *lwp;
2212 int wstat;
2213
2214 /* Note that we go through the full wait even loop. While
2215 moving threads out of jump pad, we need to be able to step
2216 over internal breakpoints and such. */
2217 linux_wait_1 (minus_one_ptid, &ourstatus, 0);
2218
2219 if (ourstatus.kind == TARGET_WAITKIND_STOPPED)
2220 {
2221 lwp = get_thread_lwp (current_inferior);
2222
2223 /* Lock it. */
2224 lwp->suspended++;
2225
2226 if (ourstatus.value.sig != GDB_SIGNAL_0
2227 || current_inferior->last_resume_kind == resume_stop)
2228 {
2229 wstat = W_STOPCODE (gdb_signal_to_host (ourstatus.value.sig));
2230 enqueue_one_deferred_signal (lwp, &wstat);
2231 }
2232 }
2233 }
2234
2235 find_inferior (&all_threads, unsuspend_one_lwp, NULL);
2236
2237 stabilizing_threads = 0;
2238
2239 current_inferior = save_inferior;
2240
2241 if (debug_threads)
2242 {
2243 thread_stuck
2244 = (struct thread_info *) find_inferior (&all_threads,
2245 stuck_in_jump_pad_callback,
2246 NULL);
2247 if (thread_stuck != NULL)
2248 debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
2249 lwpid_of (thread_stuck));
2250 }
2251 }
2252
2253 /* Wait for process, returns status. */
2254
2255 static ptid_t
2256 linux_wait_1 (ptid_t ptid,
2257 struct target_waitstatus *ourstatus, int target_options)
2258 {
2259 int w;
2260 struct lwp_info *event_child;
2261 int options;
2262 int pid;
2263 int step_over_finished;
2264 int bp_explains_trap;
2265 int maybe_internal_trap;
2266 int report_to_gdb;
2267 int trace_event;
2268 int in_step_range;
2269
2270 if (debug_threads)
2271 {
2272 debug_enter ();
2273 debug_printf ("linux_wait_1: [%s]\n", target_pid_to_str (ptid));
2274 }
2275
2276 /* Translate generic target options into linux options. */
2277 options = __WALL;
2278 if (target_options & TARGET_WNOHANG)
2279 options |= WNOHANG;
2280
2281 retry:
2282 bp_explains_trap = 0;
2283 trace_event = 0;
2284 in_step_range = 0;
2285 ourstatus->kind = TARGET_WAITKIND_IGNORE;
2286
2287 /* If we were only supposed to resume one thread, only wait for
2288 that thread - if it's still alive. If it died, however - which
2289 can happen if we're coming from the thread death case below -
2290 then we need to make sure we restart the other threads. We could
2291 pick a thread at random or restart all; restarting all is less
2292 arbitrary. */
2293 if (!non_stop
2294 && !ptid_equal (cont_thread, null_ptid)
2295 && !ptid_equal (cont_thread, minus_one_ptid))
2296 {
2297 struct thread_info *thread;
2298
2299 thread = (struct thread_info *) find_inferior_id (&all_threads,
2300 cont_thread);
2301
2302 /* No stepping, no signal - unless one is pending already, of course. */
2303 if (thread == NULL)
2304 {
2305 struct thread_resume resume_info;
2306 resume_info.thread = minus_one_ptid;
2307 resume_info.kind = resume_continue;
2308 resume_info.sig = 0;
2309 linux_resume (&resume_info, 1);
2310 }
2311 else
2312 ptid = cont_thread;
2313 }
2314
2315 if (ptid_equal (step_over_bkpt, null_ptid))
2316 pid = linux_wait_for_event (ptid, &w, options);
2317 else
2318 {
2319 if (debug_threads)
2320 debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
2321 target_pid_to_str (step_over_bkpt));
2322 pid = linux_wait_for_event (step_over_bkpt, &w, options & ~WNOHANG);
2323 }
2324
2325 if (pid == 0) /* only if TARGET_WNOHANG */
2326 {
2327 if (debug_threads)
2328 {
2329 debug_printf ("linux_wait_1 ret = null_ptid\n");
2330 debug_exit ();
2331 }
2332 return null_ptid;
2333 }
2334
2335 event_child = get_thread_lwp (current_inferior);
2336
2337 /* If we are waiting for a particular child, and it exited,
2338 linux_wait_for_event will return its exit status. Similarly if
2339 the last child exited. If this is not the last child, however,
2340 do not report it as exited until there is a 'thread exited' response
2341 available in the remote protocol. Instead, just wait for another event.
2342 This should be safe, because if the thread crashed we will already
2343 have reported the termination signal to GDB; that should stop any
2344 in-progress stepping operations, etc.
2345
2346 Report the exit status of the last thread to exit. This matches
2347 LinuxThreads' behavior. */
2348
2349 if (last_thread_of_process_p (current_inferior))
2350 {
2351 if (WIFEXITED (w) || WIFSIGNALED (w))
2352 {
2353 if (WIFEXITED (w))
2354 {
2355 ourstatus->kind = TARGET_WAITKIND_EXITED;
2356 ourstatus->value.integer = WEXITSTATUS (w);
2357
2358 if (debug_threads)
2359 {
2360 debug_printf ("linux_wait_1 ret = %s, exited with "
2361 "retcode %d\n",
2362 target_pid_to_str (ptid_of (current_inferior)),
2363 WEXITSTATUS (w));
2364 debug_exit ();
2365 }
2366 }
2367 else
2368 {
2369 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2370 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (w));
2371
2372 if (debug_threads)
2373 {
2374 debug_printf ("linux_wait_1 ret = %s, terminated with "
2375 "signal %d\n",
2376 target_pid_to_str (ptid_of (current_inferior)),
2377 WTERMSIG (w));
2378 debug_exit ();
2379 }
2380 }
2381
2382 return ptid_of (current_inferior);
2383 }
2384 }
2385 else
2386 {
2387 if (!WIFSTOPPED (w))
2388 goto retry;
2389 }
2390
2391 /* If this event was not handled before, and is not a SIGTRAP, we
2392 report it. SIGILL and SIGSEGV are also treated as traps in case
2393 a breakpoint is inserted at the current PC. If this target does
2394 not support internal breakpoints at all, we also report the
2395 SIGTRAP without further processing; it's of no concern to us. */
2396 maybe_internal_trap
2397 = (supports_breakpoints ()
2398 && (WSTOPSIG (w) == SIGTRAP
2399 || ((WSTOPSIG (w) == SIGILL
2400 || WSTOPSIG (w) == SIGSEGV)
2401 && (*the_low_target.breakpoint_at) (event_child->stop_pc))));
2402
2403 if (maybe_internal_trap)
2404 {
2405 /* Handle anything that requires bookkeeping before deciding to
2406 report the event or continue waiting. */
2407
2408 /* First check if we can explain the SIGTRAP with an internal
2409 breakpoint, or if we should possibly report the event to GDB.
2410 Do this before anything that may remove or insert a
2411 breakpoint. */
2412 bp_explains_trap = breakpoint_inserted_here (event_child->stop_pc);
2413
2414 /* We have a SIGTRAP, possibly a step-over dance has just
2415 finished. If so, tweak the state machine accordingly,
2416 reinsert breakpoints and delete any reinsert (software
2417 single-step) breakpoints. */
2418 step_over_finished = finish_step_over (event_child);
2419
2420 /* Now invoke the callbacks of any internal breakpoints there. */
2421 check_breakpoints (event_child->stop_pc);
2422
2423 /* Handle tracepoint data collecting. This may overflow the
2424 trace buffer, and cause a tracing stop, removing
2425 breakpoints. */
2426 trace_event = handle_tracepoints (event_child);
2427
2428 if (bp_explains_trap)
2429 {
2430 /* If we stepped or ran into an internal breakpoint, we've
2431 already handled it. So next time we resume (from this
2432 PC), we should step over it. */
2433 if (debug_threads)
2434 debug_printf ("Hit a gdbserver breakpoint.\n");
2435
2436 if (breakpoint_here (event_child->stop_pc))
2437 event_child->need_step_over = 1;
2438 }
2439 }
2440 else
2441 {
2442 /* We have some other signal, possibly a step-over dance was in
2443 progress, and it should be cancelled too. */
2444 step_over_finished = finish_step_over (event_child);
2445 }
2446
2447 /* We have all the data we need. Either report the event to GDB, or
2448 resume threads and keep waiting for more. */
2449
2450 /* If we're collecting a fast tracepoint, finish the collection and
2451 move out of the jump pad before delivering a signal. See
2452 linux_stabilize_threads. */
2453
2454 if (WIFSTOPPED (w)
2455 && WSTOPSIG (w) != SIGTRAP
2456 && supports_fast_tracepoints ()
2457 && agent_loaded_p ())
2458 {
2459 if (debug_threads)
2460 debug_printf ("Got signal %d for LWP %ld. Check if we need "
2461 "to defer or adjust it.\n",
2462 WSTOPSIG (w), lwpid_of (current_inferior));
2463
2464 /* Allow debugging the jump pad itself. */
2465 if (current_inferior->last_resume_kind != resume_step
2466 && maybe_move_out_of_jump_pad (event_child, &w))
2467 {
2468 enqueue_one_deferred_signal (event_child, &w);
2469
2470 if (debug_threads)
2471 debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
2472 WSTOPSIG (w), lwpid_of (current_inferior));
2473
2474 linux_resume_one_lwp (event_child, 0, 0, NULL);
2475 goto retry;
2476 }
2477 }
2478
2479 if (event_child->collecting_fast_tracepoint)
2480 {
2481 if (debug_threads)
2482 debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
2483 "Check if we're already there.\n",
2484 lwpid_of (current_inferior),
2485 event_child->collecting_fast_tracepoint);
2486
2487 trace_event = 1;
2488
2489 event_child->collecting_fast_tracepoint
2490 = linux_fast_tracepoint_collecting (event_child, NULL);
2491
2492 if (event_child->collecting_fast_tracepoint != 1)
2493 {
2494 /* No longer need this breakpoint. */
2495 if (event_child->exit_jump_pad_bkpt != NULL)
2496 {
2497 if (debug_threads)
2498 debug_printf ("No longer need exit-jump-pad bkpt; removing it."
2499 "stopping all threads momentarily.\n");
2500
2501 /* Other running threads could hit this breakpoint.
2502 We don't handle moribund locations like GDB does,
2503 instead we always pause all threads when removing
2504 breakpoints, so that any step-over or
2505 decr_pc_after_break adjustment is always taken
2506 care of while the breakpoint is still
2507 inserted. */
2508 stop_all_lwps (1, event_child);
2509 cancel_breakpoints ();
2510
2511 delete_breakpoint (event_child->exit_jump_pad_bkpt);
2512 event_child->exit_jump_pad_bkpt = NULL;
2513
2514 unstop_all_lwps (1, event_child);
2515
2516 gdb_assert (event_child->suspended >= 0);
2517 }
2518 }
2519
2520 if (event_child->collecting_fast_tracepoint == 0)
2521 {
2522 if (debug_threads)
2523 debug_printf ("fast tracepoint finished "
2524 "collecting successfully.\n");
2525
2526 /* We may have a deferred signal to report. */
2527 if (dequeue_one_deferred_signal (event_child, &w))
2528 {
2529 if (debug_threads)
2530 debug_printf ("dequeued one signal.\n");
2531 }
2532 else
2533 {
2534 if (debug_threads)
2535 debug_printf ("no deferred signals.\n");
2536
2537 if (stabilizing_threads)
2538 {
2539 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2540 ourstatus->value.sig = GDB_SIGNAL_0;
2541
2542 if (debug_threads)
2543 {
2544 debug_printf ("linux_wait_1 ret = %s, stopped "
2545 "while stabilizing threads\n",
2546 target_pid_to_str (ptid_of (current_inferior)));
2547 debug_exit ();
2548 }
2549
2550 return ptid_of (current_inferior);
2551 }
2552 }
2553 }
2554 }
2555
2556 /* Check whether GDB would be interested in this event. */
2557
2558 /* If GDB is not interested in this signal, don't stop other
2559 threads, and don't report it to GDB. Just resume the inferior
2560 right away. We do this for threading-related signals as well as
2561 any that GDB specifically requested we ignore. But never ignore
2562 SIGSTOP if we sent it ourselves, and do not ignore signals when
2563 stepping - they may require special handling to skip the signal
2564 handler. */
2565 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
2566 thread library? */
2567 if (WIFSTOPPED (w)
2568 && current_inferior->last_resume_kind != resume_step
2569 && (
2570 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
2571 (current_process ()->private->thread_db != NULL
2572 && (WSTOPSIG (w) == __SIGRTMIN
2573 || WSTOPSIG (w) == __SIGRTMIN + 1))
2574 ||
2575 #endif
2576 (pass_signals[gdb_signal_from_host (WSTOPSIG (w))]
2577 && !(WSTOPSIG (w) == SIGSTOP
2578 && current_inferior->last_resume_kind == resume_stop))))
2579 {
2580 siginfo_t info, *info_p;
2581
2582 if (debug_threads)
2583 debug_printf ("Ignored signal %d for LWP %ld.\n",
2584 WSTOPSIG (w), lwpid_of (current_inferior));
2585
2586 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_inferior),
2587 (PTRACE_TYPE_ARG3) 0, &info) == 0)
2588 info_p = &info;
2589 else
2590 info_p = NULL;
2591 linux_resume_one_lwp (event_child, event_child->stepping,
2592 WSTOPSIG (w), info_p);
2593 goto retry;
2594 }
2595
2596 /* Note that all addresses are always "out of the step range" when
2597 there's no range to begin with. */
2598 in_step_range = lwp_in_step_range (event_child);
2599
2600 /* If GDB wanted this thread to single step, and the thread is out
2601 of the step range, we always want to report the SIGTRAP, and let
2602 GDB handle it. Watchpoints should always be reported. So should
2603 signals we can't explain. A SIGTRAP we can't explain could be a
2604 GDB breakpoint --- we may or not support Z0 breakpoints. If we
2605 do, we're be able to handle GDB breakpoints on top of internal
2606 breakpoints, by handling the internal breakpoint and still
2607 reporting the event to GDB. If we don't, we're out of luck, GDB
2608 won't see the breakpoint hit. */
2609 report_to_gdb = (!maybe_internal_trap
2610 || (current_inferior->last_resume_kind == resume_step
2611 && !in_step_range)
2612 || event_child->stopped_by_watchpoint
2613 || (!step_over_finished && !in_step_range
2614 && !bp_explains_trap && !trace_event)
2615 || (gdb_breakpoint_here (event_child->stop_pc)
2616 && gdb_condition_true_at_breakpoint (event_child->stop_pc)
2617 && gdb_no_commands_at_breakpoint (event_child->stop_pc)));
2618
2619 run_breakpoint_commands (event_child->stop_pc);
2620
2621 /* We found no reason GDB would want us to stop. We either hit one
2622 of our own breakpoints, or finished an internal step GDB
2623 shouldn't know about. */
2624 if (!report_to_gdb)
2625 {
2626 if (debug_threads)
2627 {
2628 if (bp_explains_trap)
2629 debug_printf ("Hit a gdbserver breakpoint.\n");
2630 if (step_over_finished)
2631 debug_printf ("Step-over finished.\n");
2632 if (trace_event)
2633 debug_printf ("Tracepoint event.\n");
2634 if (lwp_in_step_range (event_child))
2635 debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
2636 paddress (event_child->stop_pc),
2637 paddress (event_child->step_range_start),
2638 paddress (event_child->step_range_end));
2639 }
2640
2641 /* We're not reporting this breakpoint to GDB, so apply the
2642 decr_pc_after_break adjustment to the inferior's regcache
2643 ourselves. */
2644
2645 if (the_low_target.set_pc != NULL)
2646 {
2647 struct regcache *regcache
2648 = get_thread_regcache (current_inferior, 1);
2649 (*the_low_target.set_pc) (regcache, event_child->stop_pc);
2650 }
2651
2652 /* We may have finished stepping over a breakpoint. If so,
2653 we've stopped and suspended all LWPs momentarily except the
2654 stepping one. This is where we resume them all again. We're
2655 going to keep waiting, so use proceed, which handles stepping
2656 over the next breakpoint. */
2657 if (debug_threads)
2658 debug_printf ("proceeding all threads.\n");
2659
2660 if (step_over_finished)
2661 unsuspend_all_lwps (event_child);
2662
2663 proceed_all_lwps ();
2664 goto retry;
2665 }
2666
2667 if (debug_threads)
2668 {
2669 if (current_inferior->last_resume_kind == resume_step)
2670 {
2671 if (event_child->step_range_start == event_child->step_range_end)
2672 debug_printf ("GDB wanted to single-step, reporting event.\n");
2673 else if (!lwp_in_step_range (event_child))
2674 debug_printf ("Out of step range, reporting event.\n");
2675 }
2676 if (event_child->stopped_by_watchpoint)
2677 debug_printf ("Stopped by watchpoint.\n");
2678 if (gdb_breakpoint_here (event_child->stop_pc))
2679 debug_printf ("Stopped by GDB breakpoint.\n");
2680 if (debug_threads)
2681 debug_printf ("Hit a non-gdbserver trap event.\n");
2682 }
2683
2684 /* Alright, we're going to report a stop. */
2685
2686 if (!non_stop && !stabilizing_threads)
2687 {
2688 /* In all-stop, stop all threads. */
2689 stop_all_lwps (0, NULL);
2690
2691 /* If we're not waiting for a specific LWP, choose an event LWP
2692 from among those that have had events. Giving equal priority
2693 to all LWPs that have had events helps prevent
2694 starvation. */
2695 if (ptid_equal (ptid, minus_one_ptid))
2696 {
2697 event_child->status_pending_p = 1;
2698 event_child->status_pending = w;
2699
2700 select_event_lwp (&event_child);
2701
2702 /* current_inferior and event_child must stay in sync. */
2703 current_inferior = get_lwp_thread (event_child);
2704
2705 event_child->status_pending_p = 0;
2706 w = event_child->status_pending;
2707 }
2708
2709 /* Now that we've selected our final event LWP, cancel any
2710 breakpoints in other LWPs that have hit a GDB breakpoint.
2711 See the comment in cancel_breakpoints_callback to find out
2712 why. */
2713 find_inferior (&all_threads, cancel_breakpoints_callback, event_child);
2714
2715 /* If we were going a step-over, all other threads but the stepping one
2716 had been paused in start_step_over, with their suspend counts
2717 incremented. We don't want to do a full unstop/unpause, because we're
2718 in all-stop mode (so we want threads stopped), but we still need to
2719 unsuspend the other threads, to decrement their `suspended' count
2720 back. */
2721 if (step_over_finished)
2722 unsuspend_all_lwps (event_child);
2723
2724 /* Stabilize threads (move out of jump pads). */
2725 stabilize_threads ();
2726 }
2727 else
2728 {
2729 /* If we just finished a step-over, then all threads had been
2730 momentarily paused. In all-stop, that's fine, we want
2731 threads stopped by now anyway. In non-stop, we need to
2732 re-resume threads that GDB wanted to be running. */
2733 if (step_over_finished)
2734 unstop_all_lwps (1, event_child);
2735 }
2736
2737 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2738
2739 if (current_inferior->last_resume_kind == resume_stop
2740 && WSTOPSIG (w) == SIGSTOP)
2741 {
2742 /* A thread that has been requested to stop by GDB with vCont;t,
2743 and it stopped cleanly, so report as SIG0. The use of
2744 SIGSTOP is an implementation detail. */
2745 ourstatus->value.sig = GDB_SIGNAL_0;
2746 }
2747 else if (current_inferior->last_resume_kind == resume_stop
2748 && WSTOPSIG (w) != SIGSTOP)
2749 {
2750 /* A thread that has been requested to stop by GDB with vCont;t,
2751 but, it stopped for other reasons. */
2752 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (w));
2753 }
2754 else
2755 {
2756 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (w));
2757 }
2758
2759 gdb_assert (ptid_equal (step_over_bkpt, null_ptid));
2760
2761 if (debug_threads)
2762 {
2763 debug_printf ("linux_wait_1 ret = %s, %d, %d\n",
2764 target_pid_to_str (ptid_of (current_inferior)),
2765 ourstatus->kind, ourstatus->value.sig);
2766 debug_exit ();
2767 }
2768
2769 return ptid_of (current_inferior);
2770 }
2771
2772 /* Get rid of any pending event in the pipe. */
2773 static void
2774 async_file_flush (void)
2775 {
2776 int ret;
2777 char buf;
2778
2779 do
2780 ret = read (linux_event_pipe[0], &buf, 1);
2781 while (ret >= 0 || (ret == -1 && errno == EINTR));
2782 }
2783
2784 /* Put something in the pipe, so the event loop wakes up. */
2785 static void
2786 async_file_mark (void)
2787 {
2788 int ret;
2789
2790 async_file_flush ();
2791
2792 do
2793 ret = write (linux_event_pipe[1], "+", 1);
2794 while (ret == 0 || (ret == -1 && errno == EINTR));
2795
2796 /* Ignore EAGAIN. If the pipe is full, the event loop will already
2797 be awakened anyway. */
2798 }
2799
2800 static ptid_t
2801 linux_wait (ptid_t ptid,
2802 struct target_waitstatus *ourstatus, int target_options)
2803 {
2804 ptid_t event_ptid;
2805
2806 /* Flush the async file first. */
2807 if (target_is_async_p ())
2808 async_file_flush ();
2809
2810 event_ptid = linux_wait_1 (ptid, ourstatus, target_options);
2811
2812 /* If at least one stop was reported, there may be more. A single
2813 SIGCHLD can signal more than one child stop. */
2814 if (target_is_async_p ()
2815 && (target_options & TARGET_WNOHANG) != 0
2816 && !ptid_equal (event_ptid, null_ptid))
2817 async_file_mark ();
2818
2819 return event_ptid;
2820 }
2821
2822 /* Send a signal to an LWP. */
2823
2824 static int
2825 kill_lwp (unsigned long lwpid, int signo)
2826 {
2827 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2828 fails, then we are not using nptl threads and we should be using kill. */
2829
2830 #ifdef __NR_tkill
2831 {
2832 static int tkill_failed;
2833
2834 if (!tkill_failed)
2835 {
2836 int ret;
2837
2838 errno = 0;
2839 ret = syscall (__NR_tkill, lwpid, signo);
2840 if (errno != ENOSYS)
2841 return ret;
2842 tkill_failed = 1;
2843 }
2844 }
2845 #endif
2846
2847 return kill (lwpid, signo);
2848 }
2849
2850 void
2851 linux_stop_lwp (struct lwp_info *lwp)
2852 {
2853 send_sigstop (lwp);
2854 }
2855
2856 static void
2857 send_sigstop (struct lwp_info *lwp)
2858 {
2859 int pid;
2860
2861 pid = lwpid_of (get_lwp_thread (lwp));
2862
2863 /* If we already have a pending stop signal for this process, don't
2864 send another. */
2865 if (lwp->stop_expected)
2866 {
2867 if (debug_threads)
2868 debug_printf ("Have pending sigstop for lwp %d\n", pid);
2869
2870 return;
2871 }
2872
2873 if (debug_threads)
2874 debug_printf ("Sending sigstop to lwp %d\n", pid);
2875
2876 lwp->stop_expected = 1;
2877 kill_lwp (pid, SIGSTOP);
2878 }
2879
2880 static int
2881 send_sigstop_callback (struct inferior_list_entry *entry, void *except)
2882 {
2883 struct thread_info *thread = (struct thread_info *) entry;
2884 struct lwp_info *lwp = get_thread_lwp (thread);
2885
2886 /* Ignore EXCEPT. */
2887 if (lwp == except)
2888 return 0;
2889
2890 if (lwp->stopped)
2891 return 0;
2892
2893 send_sigstop (lwp);
2894 return 0;
2895 }
2896
2897 /* Increment the suspend count of an LWP, and stop it, if not stopped
2898 yet. */
2899 static int
2900 suspend_and_send_sigstop_callback (struct inferior_list_entry *entry,
2901 void *except)
2902 {
2903 struct thread_info *thread = (struct thread_info *) entry;
2904 struct lwp_info *lwp = get_thread_lwp (thread);
2905
2906 /* Ignore EXCEPT. */
2907 if (lwp == except)
2908 return 0;
2909
2910 lwp->suspended++;
2911
2912 return send_sigstop_callback (entry, except);
2913 }
2914
2915 static void
2916 mark_lwp_dead (struct lwp_info *lwp, int wstat)
2917 {
2918 /* It's dead, really. */
2919 lwp->dead = 1;
2920
2921 /* Store the exit status for later. */
2922 lwp->status_pending_p = 1;
2923 lwp->status_pending = wstat;
2924
2925 /* Prevent trying to stop it. */
2926 lwp->stopped = 1;
2927
2928 /* No further stops are expected from a dead lwp. */
2929 lwp->stop_expected = 0;
2930 }
2931
2932 static void
2933 wait_for_sigstop (struct inferior_list_entry *entry)
2934 {
2935 struct thread_info *thread = (struct thread_info *) entry;
2936 struct lwp_info *lwp = get_thread_lwp (thread);
2937 struct thread_info *saved_inferior;
2938 int wstat;
2939 ptid_t saved_tid;
2940 ptid_t ptid;
2941 int pid;
2942
2943 if (lwp->stopped)
2944 {
2945 if (debug_threads)
2946 debug_printf ("wait_for_sigstop: LWP %ld already stopped\n",
2947 lwpid_of (thread));
2948 return;
2949 }
2950
2951 saved_inferior = current_inferior;
2952 if (saved_inferior != NULL)
2953 saved_tid = saved_inferior->entry.id;
2954 else
2955 saved_tid = null_ptid; /* avoid bogus unused warning */
2956
2957 ptid = thread->entry.id;
2958
2959 if (debug_threads)
2960 debug_printf ("wait_for_sigstop: pulling one event\n");
2961
2962 pid = linux_wait_for_event (ptid, &wstat, __WALL);
2963
2964 /* If we stopped with a non-SIGSTOP signal, save it for later
2965 and record the pending SIGSTOP. If the process exited, just
2966 return. */
2967 if (WIFSTOPPED (wstat))
2968 {
2969 if (debug_threads)
2970 debug_printf ("LWP %ld stopped with signal %d\n",
2971 lwpid_of (thread), WSTOPSIG (wstat));
2972
2973 if (WSTOPSIG (wstat) != SIGSTOP)
2974 {
2975 if (debug_threads)
2976 debug_printf ("LWP %ld stopped with non-sigstop status %06x\n",
2977 lwpid_of (thread), wstat);
2978
2979 lwp->status_pending_p = 1;
2980 lwp->status_pending = wstat;
2981 }
2982 }
2983 else
2984 {
2985 if (debug_threads)
2986 debug_printf ("Process %d exited while stopping LWPs\n", pid);
2987
2988 lwp = find_lwp_pid (pid_to_ptid (pid));
2989 if (lwp)
2990 {
2991 /* Leave this status pending for the next time we're able to
2992 report it. In the mean time, we'll report this lwp as
2993 dead to GDB, so GDB doesn't try to read registers and
2994 memory from it. This can only happen if this was the
2995 last thread of the process; otherwise, PID is removed
2996 from the thread tables before linux_wait_for_event
2997 returns. */
2998 mark_lwp_dead (lwp, wstat);
2999 }
3000 }
3001
3002 if (saved_inferior == NULL || linux_thread_alive (saved_tid))
3003 current_inferior = saved_inferior;
3004 else
3005 {
3006 if (debug_threads)
3007 debug_printf ("Previously current thread died.\n");
3008
3009 if (non_stop)
3010 {
3011 /* We can't change the current inferior behind GDB's back,
3012 otherwise, a subsequent command may apply to the wrong
3013 process. */
3014 current_inferior = NULL;
3015 }
3016 else
3017 {
3018 /* Set a valid thread as current. */
3019 set_desired_inferior (0);
3020 }
3021 }
3022 }
3023
3024 /* Returns true if LWP ENTRY is stopped in a jump pad, and we can't
3025 move it out, because we need to report the stop event to GDB. For
3026 example, if the user puts a breakpoint in the jump pad, it's
3027 because she wants to debug it. */
3028
3029 static int
3030 stuck_in_jump_pad_callback (struct inferior_list_entry *entry, void *data)
3031 {
3032 struct thread_info *thread = (struct thread_info *) entry;
3033 struct lwp_info *lwp = get_thread_lwp (thread);
3034
3035 gdb_assert (lwp->suspended == 0);
3036 gdb_assert (lwp->stopped);
3037
3038 /* Allow debugging the jump pad, gdb_collect, etc.. */
3039 return (supports_fast_tracepoints ()
3040 && agent_loaded_p ()
3041 && (gdb_breakpoint_here (lwp->stop_pc)
3042 || lwp->stopped_by_watchpoint
3043 || thread->last_resume_kind == resume_step)
3044 && linux_fast_tracepoint_collecting (lwp, NULL));
3045 }
3046
3047 static void
3048 move_out_of_jump_pad_callback (struct inferior_list_entry *entry)
3049 {
3050 struct thread_info *thread = (struct thread_info *) entry;
3051 struct lwp_info *lwp = get_thread_lwp (thread);
3052 int *wstat;
3053
3054 gdb_assert (lwp->suspended == 0);
3055 gdb_assert (lwp->stopped);
3056
3057 wstat = lwp->status_pending_p ? &lwp->status_pending : NULL;
3058
3059 /* Allow debugging the jump pad, gdb_collect, etc. */
3060 if (!gdb_breakpoint_here (lwp->stop_pc)
3061 && !lwp->stopped_by_watchpoint
3062 && thread->last_resume_kind != resume_step
3063 && maybe_move_out_of_jump_pad (lwp, wstat))
3064 {
3065 if (debug_threads)
3066 debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
3067 lwpid_of (thread));
3068
3069 if (wstat)
3070 {
3071 lwp->status_pending_p = 0;
3072 enqueue_one_deferred_signal (lwp, wstat);
3073
3074 if (debug_threads)
3075 debug_printf ("Signal %d for LWP %ld deferred "
3076 "(in jump pad)\n",
3077 WSTOPSIG (*wstat), lwpid_of (thread));
3078 }
3079
3080 linux_resume_one_lwp (lwp, 0, 0, NULL);
3081 }
3082 else
3083 lwp->suspended++;
3084 }
3085
3086 static int
3087 lwp_running (struct inferior_list_entry *entry, void *data)
3088 {
3089 struct thread_info *thread = (struct thread_info *) entry;
3090 struct lwp_info *lwp = get_thread_lwp (thread);
3091
3092 if (lwp->dead)
3093 return 0;
3094 if (lwp->stopped)
3095 return 0;
3096 return 1;
3097 }
3098
3099 /* Stop all lwps that aren't stopped yet, except EXCEPT, if not NULL.
3100 If SUSPEND, then also increase the suspend count of every LWP,
3101 except EXCEPT. */
3102
3103 static void
3104 stop_all_lwps (int suspend, struct lwp_info *except)
3105 {
3106 /* Should not be called recursively. */
3107 gdb_assert (stopping_threads == NOT_STOPPING_THREADS);
3108
3109 if (debug_threads)
3110 {
3111 debug_enter ();
3112 debug_printf ("stop_all_lwps (%s, except=%s)\n",
3113 suspend ? "stop-and-suspend" : "stop",
3114 except != NULL
3115 ? target_pid_to_str (ptid_of (get_lwp_thread (except)))
3116 : "none");
3117 }
3118
3119 stopping_threads = (suspend
3120 ? STOPPING_AND_SUSPENDING_THREADS
3121 : STOPPING_THREADS);
3122
3123 if (suspend)
3124 find_inferior (&all_threads, suspend_and_send_sigstop_callback, except);
3125 else
3126 find_inferior (&all_threads, send_sigstop_callback, except);
3127 for_each_inferior (&all_threads, wait_for_sigstop);
3128 stopping_threads = NOT_STOPPING_THREADS;
3129
3130 if (debug_threads)
3131 {
3132 debug_printf ("stop_all_lwps done, setting stopping_threads "
3133 "back to !stopping\n");
3134 debug_exit ();
3135 }
3136 }
3137
3138 /* Resume execution of the inferior process.
3139 If STEP is nonzero, single-step it.
3140 If SIGNAL is nonzero, give it that signal. */
3141
3142 static void
3143 linux_resume_one_lwp (struct lwp_info *lwp,
3144 int step, int signal, siginfo_t *info)
3145 {
3146 struct thread_info *thread = get_lwp_thread (lwp);
3147 struct thread_info *saved_inferior;
3148 int fast_tp_collecting;
3149
3150 if (lwp->stopped == 0)
3151 return;
3152
3153 fast_tp_collecting = lwp->collecting_fast_tracepoint;
3154
3155 gdb_assert (!stabilizing_threads || fast_tp_collecting);
3156
3157 /* Cancel actions that rely on GDB not changing the PC (e.g., the
3158 user used the "jump" command, or "set $pc = foo"). */
3159 if (lwp->stop_pc != get_pc (lwp))
3160 {
3161 /* Collecting 'while-stepping' actions doesn't make sense
3162 anymore. */
3163 release_while_stepping_state_list (thread);
3164 }
3165
3166 /* If we have pending signals or status, and a new signal, enqueue the
3167 signal. Also enqueue the signal if we are waiting to reinsert a
3168 breakpoint; it will be picked up again below. */
3169 if (signal != 0
3170 && (lwp->status_pending_p
3171 || lwp->pending_signals != NULL
3172 || lwp->bp_reinsert != 0
3173 || fast_tp_collecting))
3174 {
3175 struct pending_signals *p_sig;
3176 p_sig = xmalloc (sizeof (*p_sig));
3177 p_sig->prev = lwp->pending_signals;
3178 p_sig->signal = signal;
3179 if (info == NULL)
3180 memset (&p_sig->info, 0, sizeof (siginfo_t));
3181 else
3182 memcpy (&p_sig->info, info, sizeof (siginfo_t));
3183 lwp->pending_signals = p_sig;
3184 }
3185
3186 if (lwp->status_pending_p)
3187 {
3188 if (debug_threads)
3189 debug_printf ("Not resuming lwp %ld (%s, signal %d, stop %s);"
3190 " has pending status\n",
3191 lwpid_of (thread), step ? "step" : "continue", signal,
3192 lwp->stop_expected ? "expected" : "not expected");
3193 return;
3194 }
3195
3196 saved_inferior = current_inferior;
3197 current_inferior = thread;
3198
3199 if (debug_threads)
3200 debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
3201 lwpid_of (thread), step ? "step" : "continue", signal,
3202 lwp->stop_expected ? "expected" : "not expected");
3203
3204 /* This bit needs some thinking about. If we get a signal that
3205 we must report while a single-step reinsert is still pending,
3206 we often end up resuming the thread. It might be better to
3207 (ew) allow a stack of pending events; then we could be sure that
3208 the reinsert happened right away and not lose any signals.
3209
3210 Making this stack would also shrink the window in which breakpoints are
3211 uninserted (see comment in linux_wait_for_lwp) but not enough for
3212 complete correctness, so it won't solve that problem. It may be
3213 worthwhile just to solve this one, however. */
3214 if (lwp->bp_reinsert != 0)
3215 {
3216 if (debug_threads)
3217 debug_printf (" pending reinsert at 0x%s\n",
3218 paddress (lwp->bp_reinsert));
3219
3220 if (can_hardware_single_step ())
3221 {
3222 if (fast_tp_collecting == 0)
3223 {
3224 if (step == 0)
3225 fprintf (stderr, "BAD - reinserting but not stepping.\n");
3226 if (lwp->suspended)
3227 fprintf (stderr, "BAD - reinserting and suspended(%d).\n",
3228 lwp->suspended);
3229 }
3230
3231 step = 1;
3232 }
3233
3234 /* Postpone any pending signal. It was enqueued above. */
3235 signal = 0;
3236 }
3237
3238 if (fast_tp_collecting == 1)
3239 {
3240 if (debug_threads)
3241 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
3242 " (exit-jump-pad-bkpt)\n",
3243 lwpid_of (thread));
3244
3245 /* Postpone any pending signal. It was enqueued above. */
3246 signal = 0;
3247 }
3248 else if (fast_tp_collecting == 2)
3249 {
3250 if (debug_threads)
3251 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
3252 " single-stepping\n",
3253 lwpid_of (thread));
3254
3255 if (can_hardware_single_step ())
3256 step = 1;
3257 else
3258 fatal ("moving out of jump pad single-stepping"
3259 " not implemented on this target");
3260
3261 /* Postpone any pending signal. It was enqueued above. */
3262 signal = 0;
3263 }
3264
3265 /* If we have while-stepping actions in this thread set it stepping.
3266 If we have a signal to deliver, it may or may not be set to
3267 SIG_IGN, we don't know. Assume so, and allow collecting
3268 while-stepping into a signal handler. A possible smart thing to
3269 do would be to set an internal breakpoint at the signal return
3270 address, continue, and carry on catching this while-stepping
3271 action only when that breakpoint is hit. A future
3272 enhancement. */
3273 if (thread->while_stepping != NULL
3274 && can_hardware_single_step ())
3275 {
3276 if (debug_threads)
3277 debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
3278 lwpid_of (thread));
3279 step = 1;
3280 }
3281
3282 if (debug_threads && the_low_target.get_pc != NULL)
3283 {
3284 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
3285 CORE_ADDR pc = (*the_low_target.get_pc) (regcache);
3286 debug_printf (" resuming from pc 0x%lx\n", (long) pc);
3287 }
3288
3289 /* If we have pending signals, consume one unless we are trying to
3290 reinsert a breakpoint or we're trying to finish a fast tracepoint
3291 collect. */
3292 if (lwp->pending_signals != NULL
3293 && lwp->bp_reinsert == 0
3294 && fast_tp_collecting == 0)
3295 {
3296 struct pending_signals **p_sig;
3297
3298 p_sig = &lwp->pending_signals;
3299 while ((*p_sig)->prev != NULL)
3300 p_sig = &(*p_sig)->prev;
3301
3302 signal = (*p_sig)->signal;
3303 if ((*p_sig)->info.si_signo != 0)
3304 ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
3305 &(*p_sig)->info);
3306
3307 free (*p_sig);
3308 *p_sig = NULL;
3309 }
3310
3311 if (the_low_target.prepare_to_resume != NULL)
3312 the_low_target.prepare_to_resume (lwp);
3313
3314 regcache_invalidate_thread (thread);
3315 errno = 0;
3316 lwp->stopped = 0;
3317 lwp->stopped_by_watchpoint = 0;
3318 lwp->stepping = step;
3319 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, lwpid_of (thread),
3320 (PTRACE_TYPE_ARG3) 0,
3321 /* Coerce to a uintptr_t first to avoid potential gcc warning
3322 of coercing an 8 byte integer to a 4 byte pointer. */
3323 (PTRACE_TYPE_ARG4) (uintptr_t) signal);
3324
3325 current_inferior = saved_inferior;
3326 if (errno)
3327 {
3328 /* ESRCH from ptrace either means that the thread was already
3329 running (an error) or that it is gone (a race condition). If
3330 it's gone, we will get a notification the next time we wait,
3331 so we can ignore the error. We could differentiate these
3332 two, but it's tricky without waiting; the thread still exists
3333 as a zombie, so sending it signal 0 would succeed. So just
3334 ignore ESRCH. */
3335 if (errno == ESRCH)
3336 return;
3337
3338 perror_with_name ("ptrace");
3339 }
3340 }
3341
3342 struct thread_resume_array
3343 {
3344 struct thread_resume *resume;
3345 size_t n;
3346 };
3347
3348 /* This function is called once per thread via find_inferior.
3349 ARG is a pointer to a thread_resume_array struct.
3350 We look up the thread specified by ENTRY in ARG, and mark the thread
3351 with a pointer to the appropriate resume request.
3352
3353 This algorithm is O(threads * resume elements), but resume elements
3354 is small (and will remain small at least until GDB supports thread
3355 suspension). */
3356
3357 static int
3358 linux_set_resume_request (struct inferior_list_entry *entry, void *arg)
3359 {
3360 struct thread_info *thread = (struct thread_info *) entry;
3361 struct lwp_info *lwp = get_thread_lwp (thread);
3362 int ndx;
3363 struct thread_resume_array *r;
3364
3365 r = arg;
3366
3367 for (ndx = 0; ndx < r->n; ndx++)
3368 {
3369 ptid_t ptid = r->resume[ndx].thread;
3370 if (ptid_equal (ptid, minus_one_ptid)
3371 || ptid_equal (ptid, entry->id)
3372 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
3373 of PID'. */
3374 || (ptid_get_pid (ptid) == pid_of (thread)
3375 && (ptid_is_pid (ptid)
3376 || ptid_get_lwp (ptid) == -1)))
3377 {
3378 if (r->resume[ndx].kind == resume_stop
3379 && thread->last_resume_kind == resume_stop)
3380 {
3381 if (debug_threads)
3382 debug_printf ("already %s LWP %ld at GDB's request\n",
3383 (thread->last_status.kind
3384 == TARGET_WAITKIND_STOPPED)
3385 ? "stopped"
3386 : "stopping",
3387 lwpid_of (thread));
3388
3389 continue;
3390 }
3391
3392 lwp->resume = &r->resume[ndx];
3393 thread->last_resume_kind = lwp->resume->kind;
3394
3395 lwp->step_range_start = lwp->resume->step_range_start;
3396 lwp->step_range_end = lwp->resume->step_range_end;
3397
3398 /* If we had a deferred signal to report, dequeue one now.
3399 This can happen if LWP gets more than one signal while
3400 trying to get out of a jump pad. */
3401 if (lwp->stopped
3402 && !lwp->status_pending_p
3403 && dequeue_one_deferred_signal (lwp, &lwp->status_pending))
3404 {
3405 lwp->status_pending_p = 1;
3406
3407 if (debug_threads)
3408 debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
3409 "leaving status pending.\n",
3410 WSTOPSIG (lwp->status_pending),
3411 lwpid_of (thread));
3412 }
3413
3414 return 0;
3415 }
3416 }
3417
3418 /* No resume action for this thread. */
3419 lwp->resume = NULL;
3420
3421 return 0;
3422 }
3423
3424 /* find_inferior callback for linux_resume.
3425 Set *FLAG_P if this lwp has an interesting status pending. */
3426
3427 static int
3428 resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
3429 {
3430 struct thread_info *thread = (struct thread_info *) entry;
3431 struct lwp_info *lwp = get_thread_lwp (thread);
3432
3433 /* LWPs which will not be resumed are not interesting, because
3434 we might not wait for them next time through linux_wait. */
3435 if (lwp->resume == NULL)
3436 return 0;
3437
3438 if (lwp->status_pending_p)
3439 * (int *) flag_p = 1;
3440
3441 return 0;
3442 }
3443
3444 /* Return 1 if this lwp that GDB wants running is stopped at an
3445 internal breakpoint that we need to step over. It assumes that any
3446 required STOP_PC adjustment has already been propagated to the
3447 inferior's regcache. */
3448
3449 static int
3450 need_step_over_p (struct inferior_list_entry *entry, void *dummy)
3451 {
3452 struct thread_info *thread = (struct thread_info *) entry;
3453 struct lwp_info *lwp = get_thread_lwp (thread);
3454 struct thread_info *saved_inferior;
3455 CORE_ADDR pc;
3456
3457 /* LWPs which will not be resumed are not interesting, because we
3458 might not wait for them next time through linux_wait. */
3459
3460 if (!lwp->stopped)
3461 {
3462 if (debug_threads)
3463 debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
3464 lwpid_of (thread));
3465 return 0;
3466 }
3467
3468 if (thread->last_resume_kind == resume_stop)
3469 {
3470 if (debug_threads)
3471 debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
3472 " stopped\n",
3473 lwpid_of (thread));
3474 return 0;
3475 }
3476
3477 gdb_assert (lwp->suspended >= 0);
3478
3479 if (lwp->suspended)
3480 {
3481 if (debug_threads)
3482 debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
3483 lwpid_of (thread));
3484 return 0;
3485 }
3486
3487 if (!lwp->need_step_over)
3488 {
3489 if (debug_threads)
3490 debug_printf ("Need step over [LWP %ld]? No\n", lwpid_of (thread));
3491 }
3492
3493 if (lwp->status_pending_p)
3494 {
3495 if (debug_threads)
3496 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
3497 " status.\n",
3498 lwpid_of (thread));
3499 return 0;
3500 }
3501
3502 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
3503 or we have. */
3504 pc = get_pc (lwp);
3505
3506 /* If the PC has changed since we stopped, then don't do anything,
3507 and let the breakpoint/tracepoint be hit. This happens if, for
3508 instance, GDB handled the decr_pc_after_break subtraction itself,
3509 GDB is OOL stepping this thread, or the user has issued a "jump"
3510 command, or poked thread's registers herself. */
3511 if (pc != lwp->stop_pc)
3512 {
3513 if (debug_threads)
3514 debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
3515 "Old stop_pc was 0x%s, PC is now 0x%s\n",
3516 lwpid_of (thread),
3517 paddress (lwp->stop_pc), paddress (pc));
3518
3519 lwp->need_step_over = 0;
3520 return 0;
3521 }
3522
3523 saved_inferior = current_inferior;
3524 current_inferior = thread;
3525
3526 /* We can only step over breakpoints we know about. */
3527 if (breakpoint_here (pc) || fast_tracepoint_jump_here (pc))
3528 {
3529 /* Don't step over a breakpoint that GDB expects to hit
3530 though. If the condition is being evaluated on the target's side
3531 and it evaluate to false, step over this breakpoint as well. */
3532 if (gdb_breakpoint_here (pc)
3533 && gdb_condition_true_at_breakpoint (pc)
3534 && gdb_no_commands_at_breakpoint (pc))
3535 {
3536 if (debug_threads)
3537 debug_printf ("Need step over [LWP %ld]? yes, but found"
3538 " GDB breakpoint at 0x%s; skipping step over\n",
3539 lwpid_of (thread), paddress (pc));
3540
3541 current_inferior = saved_inferior;
3542 return 0;
3543 }
3544 else
3545 {
3546 if (debug_threads)
3547 debug_printf ("Need step over [LWP %ld]? yes, "
3548 "found breakpoint at 0x%s\n",
3549 lwpid_of (thread), paddress (pc));
3550
3551 /* We've found an lwp that needs stepping over --- return 1 so
3552 that find_inferior stops looking. */
3553 current_inferior = saved_inferior;
3554
3555 /* If the step over is cancelled, this is set again. */
3556 lwp->need_step_over = 0;
3557 return 1;
3558 }
3559 }
3560
3561 current_inferior = saved_inferior;
3562
3563 if (debug_threads)
3564 debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
3565 " at 0x%s\n",
3566 lwpid_of (thread), paddress (pc));
3567
3568 return 0;
3569 }
3570
3571 /* Start a step-over operation on LWP. When LWP stopped at a
3572 breakpoint, to make progress, we need to remove the breakpoint out
3573 of the way. If we let other threads run while we do that, they may
3574 pass by the breakpoint location and miss hitting it. To avoid
3575 that, a step-over momentarily stops all threads while LWP is
3576 single-stepped while the breakpoint is temporarily uninserted from
3577 the inferior. When the single-step finishes, we reinsert the
3578 breakpoint, and let all threads that are supposed to be running,
3579 run again.
3580
3581 On targets that don't support hardware single-step, we don't
3582 currently support full software single-stepping. Instead, we only
3583 support stepping over the thread event breakpoint, by asking the
3584 low target where to place a reinsert breakpoint. Since this
3585 routine assumes the breakpoint being stepped over is a thread event
3586 breakpoint, it usually assumes the return address of the current
3587 function is a good enough place to set the reinsert breakpoint. */
3588
3589 static int
3590 start_step_over (struct lwp_info *lwp)
3591 {
3592 struct thread_info *thread = get_lwp_thread (lwp);
3593 struct thread_info *saved_inferior;
3594 CORE_ADDR pc;
3595 int step;
3596
3597 if (debug_threads)
3598 debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
3599 lwpid_of (thread));
3600
3601 stop_all_lwps (1, lwp);
3602 gdb_assert (lwp->suspended == 0);
3603
3604 if (debug_threads)
3605 debug_printf ("Done stopping all threads for step-over.\n");
3606
3607 /* Note, we should always reach here with an already adjusted PC,
3608 either by GDB (if we're resuming due to GDB's request), or by our
3609 caller, if we just finished handling an internal breakpoint GDB
3610 shouldn't care about. */
3611 pc = get_pc (lwp);
3612
3613 saved_inferior = current_inferior;
3614 current_inferior = thread;
3615
3616 lwp->bp_reinsert = pc;
3617 uninsert_breakpoints_at (pc);
3618 uninsert_fast_tracepoint_jumps_at (pc);
3619
3620 if (can_hardware_single_step ())
3621 {
3622 step = 1;
3623 }
3624 else
3625 {
3626 CORE_ADDR raddr = (*the_low_target.breakpoint_reinsert_addr) ();
3627 set_reinsert_breakpoint (raddr);
3628 step = 0;
3629 }
3630
3631 current_inferior = saved_inferior;
3632
3633 linux_resume_one_lwp (lwp, step, 0, NULL);
3634
3635 /* Require next event from this LWP. */
3636 step_over_bkpt = thread->entry.id;
3637 return 1;
3638 }
3639
3640 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
3641 start_step_over, if still there, and delete any reinsert
3642 breakpoints we've set, on non hardware single-step targets. */
3643
3644 static int
3645 finish_step_over (struct lwp_info *lwp)
3646 {
3647 if (lwp->bp_reinsert != 0)
3648 {
3649 if (debug_threads)
3650 debug_printf ("Finished step over.\n");
3651
3652 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
3653 may be no breakpoint to reinsert there by now. */
3654 reinsert_breakpoints_at (lwp->bp_reinsert);
3655 reinsert_fast_tracepoint_jumps_at (lwp->bp_reinsert);
3656
3657 lwp->bp_reinsert = 0;
3658
3659 /* Delete any software-single-step reinsert breakpoints. No
3660 longer needed. We don't have to worry about other threads
3661 hitting this trap, and later not being able to explain it,
3662 because we were stepping over a breakpoint, and we hold all
3663 threads but LWP stopped while doing that. */
3664 if (!can_hardware_single_step ())
3665 delete_reinsert_breakpoints ();
3666
3667 step_over_bkpt = null_ptid;
3668 return 1;
3669 }
3670 else
3671 return 0;
3672 }
3673
3674 /* This function is called once per thread. We check the thread's resume
3675 request, which will tell us whether to resume, step, or leave the thread
3676 stopped; and what signal, if any, it should be sent.
3677
3678 For threads which we aren't explicitly told otherwise, we preserve
3679 the stepping flag; this is used for stepping over gdbserver-placed
3680 breakpoints.
3681
3682 If pending_flags was set in any thread, we queue any needed
3683 signals, since we won't actually resume. We already have a pending
3684 event to report, so we don't need to preserve any step requests;
3685 they should be re-issued if necessary. */
3686
3687 static int
3688 linux_resume_one_thread (struct inferior_list_entry *entry, void *arg)
3689 {
3690 struct thread_info *thread = (struct thread_info *) entry;
3691 struct lwp_info *lwp = get_thread_lwp (thread);
3692 int step;
3693 int leave_all_stopped = * (int *) arg;
3694 int leave_pending;
3695
3696 if (lwp->resume == NULL)
3697 return 0;
3698
3699 if (lwp->resume->kind == resume_stop)
3700 {
3701 if (debug_threads)
3702 debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread));
3703
3704 if (!lwp->stopped)
3705 {
3706 if (debug_threads)
3707 debug_printf ("stopping LWP %ld\n", lwpid_of (thread));
3708
3709 /* Stop the thread, and wait for the event asynchronously,
3710 through the event loop. */
3711 send_sigstop (lwp);
3712 }
3713 else
3714 {
3715 if (debug_threads)
3716 debug_printf ("already stopped LWP %ld\n",
3717 lwpid_of (thread));
3718
3719 /* The LWP may have been stopped in an internal event that
3720 was not meant to be notified back to GDB (e.g., gdbserver
3721 breakpoint), so we should be reporting a stop event in
3722 this case too. */
3723
3724 /* If the thread already has a pending SIGSTOP, this is a
3725 no-op. Otherwise, something later will presumably resume
3726 the thread and this will cause it to cancel any pending
3727 operation, due to last_resume_kind == resume_stop. If
3728 the thread already has a pending status to report, we
3729 will still report it the next time we wait - see
3730 status_pending_p_callback. */
3731
3732 /* If we already have a pending signal to report, then
3733 there's no need to queue a SIGSTOP, as this means we're
3734 midway through moving the LWP out of the jumppad, and we
3735 will report the pending signal as soon as that is
3736 finished. */
3737 if (lwp->pending_signals_to_report == NULL)
3738 send_sigstop (lwp);
3739 }
3740
3741 /* For stop requests, we're done. */
3742 lwp->resume = NULL;
3743 thread->last_status.kind = TARGET_WAITKIND_IGNORE;
3744 return 0;
3745 }
3746
3747 /* If this thread which is about to be resumed has a pending status,
3748 then don't resume any threads - we can just report the pending
3749 status. Make sure to queue any signals that would otherwise be
3750 sent. In all-stop mode, we do this decision based on if *any*
3751 thread has a pending status. If there's a thread that needs the
3752 step-over-breakpoint dance, then don't resume any other thread
3753 but that particular one. */
3754 leave_pending = (lwp->status_pending_p || leave_all_stopped);
3755
3756 if (!leave_pending)
3757 {
3758 if (debug_threads)
3759 debug_printf ("resuming LWP %ld\n", lwpid_of (thread));
3760
3761 step = (lwp->resume->kind == resume_step);
3762 linux_resume_one_lwp (lwp, step, lwp->resume->sig, NULL);
3763 }
3764 else
3765 {
3766 if (debug_threads)
3767 debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread));
3768
3769 /* If we have a new signal, enqueue the signal. */
3770 if (lwp->resume->sig != 0)
3771 {
3772 struct pending_signals *p_sig;
3773 p_sig = xmalloc (sizeof (*p_sig));
3774 p_sig->prev = lwp->pending_signals;
3775 p_sig->signal = lwp->resume->sig;
3776 memset (&p_sig->info, 0, sizeof (siginfo_t));
3777
3778 /* If this is the same signal we were previously stopped by,
3779 make sure to queue its siginfo. We can ignore the return
3780 value of ptrace; if it fails, we'll skip
3781 PTRACE_SETSIGINFO. */
3782 if (WIFSTOPPED (lwp->last_status)
3783 && WSTOPSIG (lwp->last_status) == lwp->resume->sig)
3784 ptrace (PTRACE_GETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
3785 &p_sig->info);
3786
3787 lwp->pending_signals = p_sig;
3788 }
3789 }
3790
3791 thread->last_status.kind = TARGET_WAITKIND_IGNORE;
3792 lwp->resume = NULL;
3793 return 0;
3794 }
3795
3796 static void
3797 linux_resume (struct thread_resume *resume_info, size_t n)
3798 {
3799 struct thread_resume_array array = { resume_info, n };
3800 struct thread_info *need_step_over = NULL;
3801 int any_pending;
3802 int leave_all_stopped;
3803
3804 if (debug_threads)
3805 {
3806 debug_enter ();
3807 debug_printf ("linux_resume:\n");
3808 }
3809
3810 find_inferior (&all_threads, linux_set_resume_request, &array);
3811
3812 /* If there is a thread which would otherwise be resumed, which has
3813 a pending status, then don't resume any threads - we can just
3814 report the pending status. Make sure to queue any signals that
3815 would otherwise be sent. In non-stop mode, we'll apply this
3816 logic to each thread individually. We consume all pending events
3817 before considering to start a step-over (in all-stop). */
3818 any_pending = 0;
3819 if (!non_stop)
3820 find_inferior (&all_threads, resume_status_pending_p, &any_pending);
3821
3822 /* If there is a thread which would otherwise be resumed, which is
3823 stopped at a breakpoint that needs stepping over, then don't
3824 resume any threads - have it step over the breakpoint with all
3825 other threads stopped, then resume all threads again. Make sure
3826 to queue any signals that would otherwise be delivered or
3827 queued. */
3828 if (!any_pending && supports_breakpoints ())
3829 need_step_over
3830 = (struct thread_info *) find_inferior (&all_threads,
3831 need_step_over_p, NULL);
3832
3833 leave_all_stopped = (need_step_over != NULL || any_pending);
3834
3835 if (debug_threads)
3836 {
3837 if (need_step_over != NULL)
3838 debug_printf ("Not resuming all, need step over\n");
3839 else if (any_pending)
3840 debug_printf ("Not resuming, all-stop and found "
3841 "an LWP with pending status\n");
3842 else
3843 debug_printf ("Resuming, no pending status or step over needed\n");
3844 }
3845
3846 /* Even if we're leaving threads stopped, queue all signals we'd
3847 otherwise deliver. */
3848 find_inferior (&all_threads, linux_resume_one_thread, &leave_all_stopped);
3849
3850 if (need_step_over)
3851 start_step_over (get_thread_lwp (need_step_over));
3852
3853 if (debug_threads)
3854 {
3855 debug_printf ("linux_resume done\n");
3856 debug_exit ();
3857 }
3858 }
3859
3860 /* This function is called once per thread. We check the thread's
3861 last resume request, which will tell us whether to resume, step, or
3862 leave the thread stopped. Any signal the client requested to be
3863 delivered has already been enqueued at this point.
3864
3865 If any thread that GDB wants running is stopped at an internal
3866 breakpoint that needs stepping over, we start a step-over operation
3867 on that particular thread, and leave all others stopped. */
3868
3869 static int
3870 proceed_one_lwp (struct inferior_list_entry *entry, void *except)
3871 {
3872 struct thread_info *thread = (struct thread_info *) entry;
3873 struct lwp_info *lwp = get_thread_lwp (thread);
3874 int step;
3875
3876 if (lwp == except)
3877 return 0;
3878
3879 if (debug_threads)
3880 debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread));
3881
3882 if (!lwp->stopped)
3883 {
3884 if (debug_threads)
3885 debug_printf (" LWP %ld already running\n", lwpid_of (thread));
3886 return 0;
3887 }
3888
3889 if (thread->last_resume_kind == resume_stop
3890 && thread->last_status.kind != TARGET_WAITKIND_IGNORE)
3891 {
3892 if (debug_threads)
3893 debug_printf (" client wants LWP to remain %ld stopped\n",
3894 lwpid_of (thread));
3895 return 0;
3896 }
3897
3898 if (lwp->status_pending_p)
3899 {
3900 if (debug_threads)
3901 debug_printf (" LWP %ld has pending status, leaving stopped\n",
3902 lwpid_of (thread));
3903 return 0;
3904 }
3905
3906 gdb_assert (lwp->suspended >= 0);
3907
3908 if (lwp->suspended)
3909 {
3910 if (debug_threads)
3911 debug_printf (" LWP %ld is suspended\n", lwpid_of (thread));
3912 return 0;
3913 }
3914
3915 if (thread->last_resume_kind == resume_stop
3916 && lwp->pending_signals_to_report == NULL
3917 && lwp->collecting_fast_tracepoint == 0)
3918 {
3919 /* We haven't reported this LWP as stopped yet (otherwise, the
3920 last_status.kind check above would catch it, and we wouldn't
3921 reach here. This LWP may have been momentarily paused by a
3922 stop_all_lwps call while handling for example, another LWP's
3923 step-over. In that case, the pending expected SIGSTOP signal
3924 that was queued at vCont;t handling time will have already
3925 been consumed by wait_for_sigstop, and so we need to requeue
3926 another one here. Note that if the LWP already has a SIGSTOP
3927 pending, this is a no-op. */
3928
3929 if (debug_threads)
3930 debug_printf ("Client wants LWP %ld to stop. "
3931 "Making sure it has a SIGSTOP pending\n",
3932 lwpid_of (thread));
3933
3934 send_sigstop (lwp);
3935 }
3936
3937 step = thread->last_resume_kind == resume_step;
3938 linux_resume_one_lwp (lwp, step, 0, NULL);
3939 return 0;
3940 }
3941
3942 static int
3943 unsuspend_and_proceed_one_lwp (struct inferior_list_entry *entry, void *except)
3944 {
3945 struct thread_info *thread = (struct thread_info *) entry;
3946 struct lwp_info *lwp = get_thread_lwp (thread);
3947
3948 if (lwp == except)
3949 return 0;
3950
3951 lwp->suspended--;
3952 gdb_assert (lwp->suspended >= 0);
3953
3954 return proceed_one_lwp (entry, except);
3955 }
3956
3957 /* When we finish a step-over, set threads running again. If there's
3958 another thread that may need a step-over, now's the time to start
3959 it. Eventually, we'll move all threads past their breakpoints. */
3960
3961 static void
3962 proceed_all_lwps (void)
3963 {
3964 struct thread_info *need_step_over;
3965
3966 /* If there is a thread which would otherwise be resumed, which is
3967 stopped at a breakpoint that needs stepping over, then don't
3968 resume any threads - have it step over the breakpoint with all
3969 other threads stopped, then resume all threads again. */
3970
3971 if (supports_breakpoints ())
3972 {
3973 need_step_over
3974 = (struct thread_info *) find_inferior (&all_threads,
3975 need_step_over_p, NULL);
3976
3977 if (need_step_over != NULL)
3978 {
3979 if (debug_threads)
3980 debug_printf ("proceed_all_lwps: found "
3981 "thread %ld needing a step-over\n",
3982 lwpid_of (need_step_over));
3983
3984 start_step_over (get_thread_lwp (need_step_over));
3985 return;
3986 }
3987 }
3988
3989 if (debug_threads)
3990 debug_printf ("Proceeding, no step-over needed\n");
3991
3992 find_inferior (&all_threads, proceed_one_lwp, NULL);
3993 }
3994
3995 /* Stopped LWPs that the client wanted to be running, that don't have
3996 pending statuses, are set to run again, except for EXCEPT, if not
3997 NULL. This undoes a stop_all_lwps call. */
3998
3999 static void
4000 unstop_all_lwps (int unsuspend, struct lwp_info *except)
4001 {
4002 if (debug_threads)
4003 {
4004 debug_enter ();
4005 if (except)
4006 debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
4007 lwpid_of (get_lwp_thread (except)));
4008 else
4009 debug_printf ("unstopping all lwps\n");
4010 }
4011
4012 if (unsuspend)
4013 find_inferior (&all_threads, unsuspend_and_proceed_one_lwp, except);
4014 else
4015 find_inferior (&all_threads, proceed_one_lwp, except);
4016
4017 if (debug_threads)
4018 {
4019 debug_printf ("unstop_all_lwps done\n");
4020 debug_exit ();
4021 }
4022 }
4023
4024
4025 #ifdef HAVE_LINUX_REGSETS
4026
4027 #define use_linux_regsets 1
4028
4029 /* Returns true if REGSET has been disabled. */
4030
4031 static int
4032 regset_disabled (struct regsets_info *info, struct regset_info *regset)
4033 {
4034 return (info->disabled_regsets != NULL
4035 && info->disabled_regsets[regset - info->regsets]);
4036 }
4037
4038 /* Disable REGSET. */
4039
4040 static void
4041 disable_regset (struct regsets_info *info, struct regset_info *regset)
4042 {
4043 int dr_offset;
4044
4045 dr_offset = regset - info->regsets;
4046 if (info->disabled_regsets == NULL)
4047 info->disabled_regsets = xcalloc (1, info->num_regsets);
4048 info->disabled_regsets[dr_offset] = 1;
4049 }
4050
4051 static int
4052 regsets_fetch_inferior_registers (struct regsets_info *regsets_info,
4053 struct regcache *regcache)
4054 {
4055 struct regset_info *regset;
4056 int saw_general_regs = 0;
4057 int pid;
4058 struct iovec iov;
4059
4060 regset = regsets_info->regsets;
4061
4062 pid = lwpid_of (current_inferior);
4063 while (regset->size >= 0)
4064 {
4065 void *buf, *data;
4066 int nt_type, res;
4067
4068 if (regset->size == 0 || regset_disabled (regsets_info, regset))
4069 {
4070 regset ++;
4071 continue;
4072 }
4073
4074 buf = xmalloc (regset->size);
4075
4076 nt_type = regset->nt_type;
4077 if (nt_type)
4078 {
4079 iov.iov_base = buf;
4080 iov.iov_len = regset->size;
4081 data = (void *) &iov;
4082 }
4083 else
4084 data = buf;
4085
4086 #ifndef __sparc__
4087 res = ptrace (regset->get_request, pid,
4088 (PTRACE_TYPE_ARG3) (long) nt_type, data);
4089 #else
4090 res = ptrace (regset->get_request, pid, data, nt_type);
4091 #endif
4092 if (res < 0)
4093 {
4094 if (errno == EIO)
4095 {
4096 /* If we get EIO on a regset, do not try it again for
4097 this process mode. */
4098 disable_regset (regsets_info, regset);
4099 free (buf);
4100 continue;
4101 }
4102 else
4103 {
4104 char s[256];
4105 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
4106 pid);
4107 perror (s);
4108 }
4109 }
4110 else if (regset->type == GENERAL_REGS)
4111 saw_general_regs = 1;
4112 regset->store_function (regcache, buf);
4113 regset ++;
4114 free (buf);
4115 }
4116 if (saw_general_regs)
4117 return 0;
4118 else
4119 return 1;
4120 }
4121
4122 static int
4123 regsets_store_inferior_registers (struct regsets_info *regsets_info,
4124 struct regcache *regcache)
4125 {
4126 struct regset_info *regset;
4127 int saw_general_regs = 0;
4128 int pid;
4129 struct iovec iov;
4130
4131 regset = regsets_info->regsets;
4132
4133 pid = lwpid_of (current_inferior);
4134 while (regset->size >= 0)
4135 {
4136 void *buf, *data;
4137 int nt_type, res;
4138
4139 if (regset->size == 0 || regset_disabled (regsets_info, regset))
4140 {
4141 regset ++;
4142 continue;
4143 }
4144
4145 buf = xmalloc (regset->size);
4146
4147 /* First fill the buffer with the current register set contents,
4148 in case there are any items in the kernel's regset that are
4149 not in gdbserver's regcache. */
4150
4151 nt_type = regset->nt_type;
4152 if (nt_type)
4153 {
4154 iov.iov_base = buf;
4155 iov.iov_len = regset->size;
4156 data = (void *) &iov;
4157 }
4158 else
4159 data = buf;
4160
4161 #ifndef __sparc__
4162 res = ptrace (regset->get_request, pid,
4163 (PTRACE_TYPE_ARG3) (long) nt_type, data);
4164 #else
4165 res = ptrace (regset->get_request, pid, data, nt_type);
4166 #endif
4167
4168 if (res == 0)
4169 {
4170 /* Then overlay our cached registers on that. */
4171 regset->fill_function (regcache, buf);
4172
4173 /* Only now do we write the register set. */
4174 #ifndef __sparc__
4175 res = ptrace (regset->set_request, pid,
4176 (PTRACE_TYPE_ARG3) (long) nt_type, data);
4177 #else
4178 res = ptrace (regset->set_request, pid, data, nt_type);
4179 #endif
4180 }
4181
4182 if (res < 0)
4183 {
4184 if (errno == EIO)
4185 {
4186 /* If we get EIO on a regset, do not try it again for
4187 this process mode. */
4188 disable_regset (regsets_info, regset);
4189 free (buf);
4190 continue;
4191 }
4192 else if (errno == ESRCH)
4193 {
4194 /* At this point, ESRCH should mean the process is
4195 already gone, in which case we simply ignore attempts
4196 to change its registers. See also the related
4197 comment in linux_resume_one_lwp. */
4198 free (buf);
4199 return 0;
4200 }
4201 else
4202 {
4203 perror ("Warning: ptrace(regsets_store_inferior_registers)");
4204 }
4205 }
4206 else if (regset->type == GENERAL_REGS)
4207 saw_general_regs = 1;
4208 regset ++;
4209 free (buf);
4210 }
4211 if (saw_general_regs)
4212 return 0;
4213 else
4214 return 1;
4215 }
4216
4217 #else /* !HAVE_LINUX_REGSETS */
4218
4219 #define use_linux_regsets 0
4220 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
4221 #define regsets_store_inferior_registers(regsets_info, regcache) 1
4222
4223 #endif
4224
4225 /* Return 1 if register REGNO is supported by one of the regset ptrace
4226 calls or 0 if it has to be transferred individually. */
4227
4228 static int
4229 linux_register_in_regsets (const struct regs_info *regs_info, int regno)
4230 {
4231 unsigned char mask = 1 << (regno % 8);
4232 size_t index = regno / 8;
4233
4234 return (use_linux_regsets
4235 && (regs_info->regset_bitmap == NULL
4236 || (regs_info->regset_bitmap[index] & mask) != 0));
4237 }
4238
4239 #ifdef HAVE_LINUX_USRREGS
4240
4241 int
4242 register_addr (const struct usrregs_info *usrregs, int regnum)
4243 {
4244 int addr;
4245
4246 if (regnum < 0 || regnum >= usrregs->num_regs)
4247 error ("Invalid register number %d.", regnum);
4248
4249 addr = usrregs->regmap[regnum];
4250
4251 return addr;
4252 }
4253
4254 /* Fetch one register. */
4255 static void
4256 fetch_register (const struct usrregs_info *usrregs,
4257 struct regcache *regcache, int regno)
4258 {
4259 CORE_ADDR regaddr;
4260 int i, size;
4261 char *buf;
4262 int pid;
4263
4264 if (regno >= usrregs->num_regs)
4265 return;
4266 if ((*the_low_target.cannot_fetch_register) (regno))
4267 return;
4268
4269 regaddr = register_addr (usrregs, regno);
4270 if (regaddr == -1)
4271 return;
4272
4273 size = ((register_size (regcache->tdesc, regno)
4274 + sizeof (PTRACE_XFER_TYPE) - 1)
4275 & -sizeof (PTRACE_XFER_TYPE));
4276 buf = alloca (size);
4277
4278 pid = lwpid_of (current_inferior);
4279 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
4280 {
4281 errno = 0;
4282 *(PTRACE_XFER_TYPE *) (buf + i) =
4283 ptrace (PTRACE_PEEKUSER, pid,
4284 /* Coerce to a uintptr_t first to avoid potential gcc warning
4285 of coercing an 8 byte integer to a 4 byte pointer. */
4286 (PTRACE_TYPE_ARG3) (uintptr_t) regaddr, (PTRACE_TYPE_ARG4) 0);
4287 regaddr += sizeof (PTRACE_XFER_TYPE);
4288 if (errno != 0)
4289 error ("reading register %d: %s", regno, strerror (errno));
4290 }
4291
4292 if (the_low_target.supply_ptrace_register)
4293 the_low_target.supply_ptrace_register (regcache, regno, buf);
4294 else
4295 supply_register (regcache, regno, buf);
4296 }
4297
4298 /* Store one register. */
4299 static void
4300 store_register (const struct usrregs_info *usrregs,
4301 struct regcache *regcache, int regno)
4302 {
4303 CORE_ADDR regaddr;
4304 int i, size;
4305 char *buf;
4306 int pid;
4307
4308 if (regno >= usrregs->num_regs)
4309 return;
4310 if ((*the_low_target.cannot_store_register) (regno))
4311 return;
4312
4313 regaddr = register_addr (usrregs, regno);
4314 if (regaddr == -1)
4315 return;
4316
4317 size = ((register_size (regcache->tdesc, regno)
4318 + sizeof (PTRACE_XFER_TYPE) - 1)
4319 & -sizeof (PTRACE_XFER_TYPE));
4320 buf = alloca (size);
4321 memset (buf, 0, size);
4322
4323 if (the_low_target.collect_ptrace_register)
4324 the_low_target.collect_ptrace_register (regcache, regno, buf);
4325 else
4326 collect_register (regcache, regno, buf);
4327
4328 pid = lwpid_of (current_inferior);
4329 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
4330 {
4331 errno = 0;
4332 ptrace (PTRACE_POKEUSER, pid,
4333 /* Coerce to a uintptr_t first to avoid potential gcc warning
4334 about coercing an 8 byte integer to a 4 byte pointer. */
4335 (PTRACE_TYPE_ARG3) (uintptr_t) regaddr,
4336 (PTRACE_TYPE_ARG4) *(PTRACE_XFER_TYPE *) (buf + i));
4337 if (errno != 0)
4338 {
4339 /* At this point, ESRCH should mean the process is
4340 already gone, in which case we simply ignore attempts
4341 to change its registers. See also the related
4342 comment in linux_resume_one_lwp. */
4343 if (errno == ESRCH)
4344 return;
4345
4346 if ((*the_low_target.cannot_store_register) (regno) == 0)
4347 error ("writing register %d: %s", regno, strerror (errno));
4348 }
4349 regaddr += sizeof (PTRACE_XFER_TYPE);
4350 }
4351 }
4352
4353 /* Fetch all registers, or just one, from the child process.
4354 If REGNO is -1, do this for all registers, skipping any that are
4355 assumed to have been retrieved by regsets_fetch_inferior_registers,
4356 unless ALL is non-zero.
4357 Otherwise, REGNO specifies which register (so we can save time). */
4358 static void
4359 usr_fetch_inferior_registers (const struct regs_info *regs_info,
4360 struct regcache *regcache, int regno, int all)
4361 {
4362 struct usrregs_info *usr = regs_info->usrregs;
4363
4364 if (regno == -1)
4365 {
4366 for (regno = 0; regno < usr->num_regs; regno++)
4367 if (all || !linux_register_in_regsets (regs_info, regno))
4368 fetch_register (usr, regcache, regno);
4369 }
4370 else
4371 fetch_register (usr, regcache, regno);
4372 }
4373
4374 /* Store our register values back into the inferior.
4375 If REGNO is -1, do this for all registers, skipping any that are
4376 assumed to have been saved by regsets_store_inferior_registers,
4377 unless ALL is non-zero.
4378 Otherwise, REGNO specifies which register (so we can save time). */
4379 static void
4380 usr_store_inferior_registers (const struct regs_info *regs_info,
4381 struct regcache *regcache, int regno, int all)
4382 {
4383 struct usrregs_info *usr = regs_info->usrregs;
4384
4385 if (regno == -1)
4386 {
4387 for (regno = 0; regno < usr->num_regs; regno++)
4388 if (all || !linux_register_in_regsets (regs_info, regno))
4389 store_register (usr, regcache, regno);
4390 }
4391 else
4392 store_register (usr, regcache, regno);
4393 }
4394
4395 #else /* !HAVE_LINUX_USRREGS */
4396
4397 #define usr_fetch_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
4398 #define usr_store_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
4399
4400 #endif
4401
4402
4403 void
4404 linux_fetch_registers (struct regcache *regcache, int regno)
4405 {
4406 int use_regsets;
4407 int all = 0;
4408 const struct regs_info *regs_info = (*the_low_target.regs_info) ();
4409
4410 if (regno == -1)
4411 {
4412 if (the_low_target.fetch_register != NULL
4413 && regs_info->usrregs != NULL)
4414 for (regno = 0; regno < regs_info->usrregs->num_regs; regno++)
4415 (*the_low_target.fetch_register) (regcache, regno);
4416
4417 all = regsets_fetch_inferior_registers (regs_info->regsets_info, regcache);
4418 if (regs_info->usrregs != NULL)
4419 usr_fetch_inferior_registers (regs_info, regcache, -1, all);
4420 }
4421 else
4422 {
4423 if (the_low_target.fetch_register != NULL
4424 && (*the_low_target.fetch_register) (regcache, regno))
4425 return;
4426
4427 use_regsets = linux_register_in_regsets (regs_info, regno);
4428 if (use_regsets)
4429 all = regsets_fetch_inferior_registers (regs_info->regsets_info,
4430 regcache);
4431 if ((!use_regsets || all) && regs_info->usrregs != NULL)
4432 usr_fetch_inferior_registers (regs_info, regcache, regno, 1);
4433 }
4434 }
4435
4436 void
4437 linux_store_registers (struct regcache *regcache, int regno)
4438 {
4439 int use_regsets;
4440 int all = 0;
4441 const struct regs_info *regs_info = (*the_low_target.regs_info) ();
4442
4443 if (regno == -1)
4444 {
4445 all = regsets_store_inferior_registers (regs_info->regsets_info,
4446 regcache);
4447 if (regs_info->usrregs != NULL)
4448 usr_store_inferior_registers (regs_info, regcache, regno, all);
4449 }
4450 else
4451 {
4452 use_regsets = linux_register_in_regsets (regs_info, regno);
4453 if (use_regsets)
4454 all = regsets_store_inferior_registers (regs_info->regsets_info,
4455 regcache);
4456 if ((!use_regsets || all) && regs_info->usrregs != NULL)
4457 usr_store_inferior_registers (regs_info, regcache, regno, 1);
4458 }
4459 }
4460
4461
4462 /* Copy LEN bytes from inferior's memory starting at MEMADDR
4463 to debugger memory starting at MYADDR. */
4464
4465 static int
4466 linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
4467 {
4468 int pid = lwpid_of (current_inferior);
4469 register PTRACE_XFER_TYPE *buffer;
4470 register CORE_ADDR addr;
4471 register int count;
4472 char filename[64];
4473 register int i;
4474 int ret;
4475 int fd;
4476
4477 /* Try using /proc. Don't bother for one word. */
4478 if (len >= 3 * sizeof (long))
4479 {
4480 int bytes;
4481
4482 /* We could keep this file open and cache it - possibly one per
4483 thread. That requires some juggling, but is even faster. */
4484 sprintf (filename, "/proc/%d/mem", pid);
4485 fd = open (filename, O_RDONLY | O_LARGEFILE);
4486 if (fd == -1)
4487 goto no_proc;
4488
4489 /* If pread64 is available, use it. It's faster if the kernel
4490 supports it (only one syscall), and it's 64-bit safe even on
4491 32-bit platforms (for instance, SPARC debugging a SPARC64
4492 application). */
4493 #ifdef HAVE_PREAD64
4494 bytes = pread64 (fd, myaddr, len, memaddr);
4495 #else
4496 bytes = -1;
4497 if (lseek (fd, memaddr, SEEK_SET) != -1)
4498 bytes = read (fd, myaddr, len);
4499 #endif
4500
4501 close (fd);
4502 if (bytes == len)
4503 return 0;
4504
4505 /* Some data was read, we'll try to get the rest with ptrace. */
4506 if (bytes > 0)
4507 {
4508 memaddr += bytes;
4509 myaddr += bytes;
4510 len -= bytes;
4511 }
4512 }
4513
4514 no_proc:
4515 /* Round starting address down to longword boundary. */
4516 addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
4517 /* Round ending address up; get number of longwords that makes. */
4518 count = ((((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
4519 / sizeof (PTRACE_XFER_TYPE));
4520 /* Allocate buffer of that many longwords. */
4521 buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
4522
4523 /* Read all the longwords */
4524 errno = 0;
4525 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
4526 {
4527 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4528 about coercing an 8 byte integer to a 4 byte pointer. */
4529 buffer[i] = ptrace (PTRACE_PEEKTEXT, pid,
4530 (PTRACE_TYPE_ARG3) (uintptr_t) addr,
4531 (PTRACE_TYPE_ARG4) 0);
4532 if (errno)
4533 break;
4534 }
4535 ret = errno;
4536
4537 /* Copy appropriate bytes out of the buffer. */
4538 if (i > 0)
4539 {
4540 i *= sizeof (PTRACE_XFER_TYPE);
4541 i -= memaddr & (sizeof (PTRACE_XFER_TYPE) - 1);
4542 memcpy (myaddr,
4543 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
4544 i < len ? i : len);
4545 }
4546
4547 return ret;
4548 }
4549
4550 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
4551 memory at MEMADDR. On failure (cannot write to the inferior)
4552 returns the value of errno. Always succeeds if LEN is zero. */
4553
4554 static int
4555 linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
4556 {
4557 register int i;
4558 /* Round starting address down to longword boundary. */
4559 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
4560 /* Round ending address up; get number of longwords that makes. */
4561 register int count
4562 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
4563 / sizeof (PTRACE_XFER_TYPE);
4564
4565 /* Allocate buffer of that many longwords. */
4566 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *)
4567 alloca (count * sizeof (PTRACE_XFER_TYPE));
4568
4569 int pid = lwpid_of (current_inferior);
4570
4571 if (len == 0)
4572 {
4573 /* Zero length write always succeeds. */
4574 return 0;
4575 }
4576
4577 if (debug_threads)
4578 {
4579 /* Dump up to four bytes. */
4580 unsigned int val = * (unsigned int *) myaddr;
4581 if (len == 1)
4582 val = val & 0xff;
4583 else if (len == 2)
4584 val = val & 0xffff;
4585 else if (len == 3)
4586 val = val & 0xffffff;
4587 debug_printf ("Writing %0*x to 0x%08lx\n", 2 * ((len < 4) ? len : 4),
4588 val, (long)memaddr);
4589 }
4590
4591 /* Fill start and end extra bytes of buffer with existing memory data. */
4592
4593 errno = 0;
4594 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4595 about coercing an 8 byte integer to a 4 byte pointer. */
4596 buffer[0] = ptrace (PTRACE_PEEKTEXT, pid,
4597 (PTRACE_TYPE_ARG3) (uintptr_t) addr,
4598 (PTRACE_TYPE_ARG4) 0);
4599 if (errno)
4600 return errno;
4601
4602 if (count > 1)
4603 {
4604 errno = 0;
4605 buffer[count - 1]
4606 = ptrace (PTRACE_PEEKTEXT, pid,
4607 /* Coerce to a uintptr_t first to avoid potential gcc warning
4608 about coercing an 8 byte integer to a 4 byte pointer. */
4609 (PTRACE_TYPE_ARG3) (uintptr_t) (addr + (count - 1)
4610 * sizeof (PTRACE_XFER_TYPE)),
4611 (PTRACE_TYPE_ARG4) 0);
4612 if (errno)
4613 return errno;
4614 }
4615
4616 /* Copy data to be written over corresponding part of buffer. */
4617
4618 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
4619 myaddr, len);
4620
4621 /* Write the entire buffer. */
4622
4623 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
4624 {
4625 errno = 0;
4626 ptrace (PTRACE_POKETEXT, pid,
4627 /* Coerce to a uintptr_t first to avoid potential gcc warning
4628 about coercing an 8 byte integer to a 4 byte pointer. */
4629 (PTRACE_TYPE_ARG3) (uintptr_t) addr,
4630 (PTRACE_TYPE_ARG4) buffer[i]);
4631 if (errno)
4632 return errno;
4633 }
4634
4635 return 0;
4636 }
4637
4638 static void
4639 linux_look_up_symbols (void)
4640 {
4641 #ifdef USE_THREAD_DB
4642 struct process_info *proc = current_process ();
4643
4644 if (proc->private->thread_db != NULL)
4645 return;
4646
4647 /* If the kernel supports tracing clones, then we don't need to
4648 use the magic thread event breakpoint to learn about
4649 threads. */
4650 thread_db_init (!linux_supports_traceclone ());
4651 #endif
4652 }
4653
4654 static void
4655 linux_request_interrupt (void)
4656 {
4657 extern unsigned long signal_pid;
4658
4659 if (!ptid_equal (cont_thread, null_ptid)
4660 && !ptid_equal (cont_thread, minus_one_ptid))
4661 {
4662 int lwpid;
4663
4664 lwpid = lwpid_of (current_inferior);
4665 kill_lwp (lwpid, SIGINT);
4666 }
4667 else
4668 kill_lwp (signal_pid, SIGINT);
4669 }
4670
4671 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
4672 to debugger memory starting at MYADDR. */
4673
4674 static int
4675 linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
4676 {
4677 char filename[PATH_MAX];
4678 int fd, n;
4679 int pid = lwpid_of (current_inferior);
4680
4681 xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
4682
4683 fd = open (filename, O_RDONLY);
4684 if (fd < 0)
4685 return -1;
4686
4687 if (offset != (CORE_ADDR) 0
4688 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
4689 n = -1;
4690 else
4691 n = read (fd, myaddr, len);
4692
4693 close (fd);
4694
4695 return n;
4696 }
4697
4698 /* These breakpoint and watchpoint related wrapper functions simply
4699 pass on the function call if the target has registered a
4700 corresponding function. */
4701
4702 static int
4703 linux_insert_point (char type, CORE_ADDR addr, int len)
4704 {
4705 if (the_low_target.insert_point != NULL)
4706 return the_low_target.insert_point (type, addr, len);
4707 else
4708 /* Unsupported (see target.h). */
4709 return 1;
4710 }
4711
4712 static int
4713 linux_remove_point (char type, CORE_ADDR addr, int len)
4714 {
4715 if (the_low_target.remove_point != NULL)
4716 return the_low_target.remove_point (type, addr, len);
4717 else
4718 /* Unsupported (see target.h). */
4719 return 1;
4720 }
4721
4722 static int
4723 linux_stopped_by_watchpoint (void)
4724 {
4725 struct lwp_info *lwp = get_thread_lwp (current_inferior);
4726
4727 return lwp->stopped_by_watchpoint;
4728 }
4729
4730 static CORE_ADDR
4731 linux_stopped_data_address (void)
4732 {
4733 struct lwp_info *lwp = get_thread_lwp (current_inferior);
4734
4735 return lwp->stopped_data_address;
4736 }
4737
4738 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
4739 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
4740 && defined(PT_TEXT_END_ADDR)
4741
4742 /* This is only used for targets that define PT_TEXT_ADDR,
4743 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
4744 the target has different ways of acquiring this information, like
4745 loadmaps. */
4746
4747 /* Under uClinux, programs are loaded at non-zero offsets, which we need
4748 to tell gdb about. */
4749
4750 static int
4751 linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
4752 {
4753 unsigned long text, text_end, data;
4754 int pid = lwpid_of (get_thread_lwp (current_inferior));
4755
4756 errno = 0;
4757
4758 text = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_ADDR,
4759 (PTRACE_TYPE_ARG4) 0);
4760 text_end = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_END_ADDR,
4761 (PTRACE_TYPE_ARG4) 0);
4762 data = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_DATA_ADDR,
4763 (PTRACE_TYPE_ARG4) 0);
4764
4765 if (errno == 0)
4766 {
4767 /* Both text and data offsets produced at compile-time (and so
4768 used by gdb) are relative to the beginning of the program,
4769 with the data segment immediately following the text segment.
4770 However, the actual runtime layout in memory may put the data
4771 somewhere else, so when we send gdb a data base-address, we
4772 use the real data base address and subtract the compile-time
4773 data base-address from it (which is just the length of the
4774 text segment). BSS immediately follows data in both
4775 cases. */
4776 *text_p = text;
4777 *data_p = data - (text_end - text);
4778
4779 return 1;
4780 }
4781 return 0;
4782 }
4783 #endif
4784
4785 static int
4786 linux_qxfer_osdata (const char *annex,
4787 unsigned char *readbuf, unsigned const char *writebuf,
4788 CORE_ADDR offset, int len)
4789 {
4790 return linux_common_xfer_osdata (annex, readbuf, offset, len);
4791 }
4792
4793 /* Convert a native/host siginfo object, into/from the siginfo in the
4794 layout of the inferiors' architecture. */
4795
4796 static void
4797 siginfo_fixup (siginfo_t *siginfo, void *inf_siginfo, int direction)
4798 {
4799 int done = 0;
4800
4801 if (the_low_target.siginfo_fixup != NULL)
4802 done = the_low_target.siginfo_fixup (siginfo, inf_siginfo, direction);
4803
4804 /* If there was no callback, or the callback didn't do anything,
4805 then just do a straight memcpy. */
4806 if (!done)
4807 {
4808 if (direction == 1)
4809 memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
4810 else
4811 memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
4812 }
4813 }
4814
4815 static int
4816 linux_xfer_siginfo (const char *annex, unsigned char *readbuf,
4817 unsigned const char *writebuf, CORE_ADDR offset, int len)
4818 {
4819 int pid;
4820 siginfo_t siginfo;
4821 char inf_siginfo[sizeof (siginfo_t)];
4822
4823 if (current_inferior == NULL)
4824 return -1;
4825
4826 pid = lwpid_of (current_inferior);
4827
4828 if (debug_threads)
4829 debug_printf ("%s siginfo for lwp %d.\n",
4830 readbuf != NULL ? "Reading" : "Writing",
4831 pid);
4832
4833 if (offset >= sizeof (siginfo))
4834 return -1;
4835
4836 if (ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
4837 return -1;
4838
4839 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
4840 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
4841 inferior with a 64-bit GDBSERVER should look the same as debugging it
4842 with a 32-bit GDBSERVER, we need to convert it. */
4843 siginfo_fixup (&siginfo, inf_siginfo, 0);
4844
4845 if (offset + len > sizeof (siginfo))
4846 len = sizeof (siginfo) - offset;
4847
4848 if (readbuf != NULL)
4849 memcpy (readbuf, inf_siginfo + offset, len);
4850 else
4851 {
4852 memcpy (inf_siginfo + offset, writebuf, len);
4853
4854 /* Convert back to ptrace layout before flushing it out. */
4855 siginfo_fixup (&siginfo, inf_siginfo, 1);
4856
4857 if (ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
4858 return -1;
4859 }
4860
4861 return len;
4862 }
4863
4864 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4865 so we notice when children change state; as the handler for the
4866 sigsuspend in my_waitpid. */
4867
4868 static void
4869 sigchld_handler (int signo)
4870 {
4871 int old_errno = errno;
4872
4873 if (debug_threads)
4874 {
4875 do
4876 {
4877 /* fprintf is not async-signal-safe, so call write
4878 directly. */
4879 if (write (2, "sigchld_handler\n",
4880 sizeof ("sigchld_handler\n") - 1) < 0)
4881 break; /* just ignore */
4882 } while (0);
4883 }
4884
4885 if (target_is_async_p ())
4886 async_file_mark (); /* trigger a linux_wait */
4887
4888 errno = old_errno;
4889 }
4890
4891 static int
4892 linux_supports_non_stop (void)
4893 {
4894 return 1;
4895 }
4896
4897 static int
4898 linux_async (int enable)
4899 {
4900 int previous = (linux_event_pipe[0] != -1);
4901
4902 if (debug_threads)
4903 debug_printf ("linux_async (%d), previous=%d\n",
4904 enable, previous);
4905
4906 if (previous != enable)
4907 {
4908 sigset_t mask;
4909 sigemptyset (&mask);
4910 sigaddset (&mask, SIGCHLD);
4911
4912 sigprocmask (SIG_BLOCK, &mask, NULL);
4913
4914 if (enable)
4915 {
4916 if (pipe (linux_event_pipe) == -1)
4917 fatal ("creating event pipe failed.");
4918
4919 fcntl (linux_event_pipe[0], F_SETFL, O_NONBLOCK);
4920 fcntl (linux_event_pipe[1], F_SETFL, O_NONBLOCK);
4921
4922 /* Register the event loop handler. */
4923 add_file_handler (linux_event_pipe[0],
4924 handle_target_event, NULL);
4925
4926 /* Always trigger a linux_wait. */
4927 async_file_mark ();
4928 }
4929 else
4930 {
4931 delete_file_handler (linux_event_pipe[0]);
4932
4933 close (linux_event_pipe[0]);
4934 close (linux_event_pipe[1]);
4935 linux_event_pipe[0] = -1;
4936 linux_event_pipe[1] = -1;
4937 }
4938
4939 sigprocmask (SIG_UNBLOCK, &mask, NULL);
4940 }
4941
4942 return previous;
4943 }
4944
4945 static int
4946 linux_start_non_stop (int nonstop)
4947 {
4948 /* Register or unregister from event-loop accordingly. */
4949 linux_async (nonstop);
4950 return 0;
4951 }
4952
4953 static int
4954 linux_supports_multi_process (void)
4955 {
4956 return 1;
4957 }
4958
4959 static int
4960 linux_supports_disable_randomization (void)
4961 {
4962 #ifdef HAVE_PERSONALITY
4963 return 1;
4964 #else
4965 return 0;
4966 #endif
4967 }
4968
4969 static int
4970 linux_supports_agent (void)
4971 {
4972 return 1;
4973 }
4974
4975 static int
4976 linux_supports_range_stepping (void)
4977 {
4978 if (*the_low_target.supports_range_stepping == NULL)
4979 return 0;
4980
4981 return (*the_low_target.supports_range_stepping) ();
4982 }
4983
4984 /* Enumerate spufs IDs for process PID. */
4985 static int
4986 spu_enumerate_spu_ids (long pid, unsigned char *buf, CORE_ADDR offset, int len)
4987 {
4988 int pos = 0;
4989 int written = 0;
4990 char path[128];
4991 DIR *dir;
4992 struct dirent *entry;
4993
4994 sprintf (path, "/proc/%ld/fd", pid);
4995 dir = opendir (path);
4996 if (!dir)
4997 return -1;
4998
4999 rewinddir (dir);
5000 while ((entry = readdir (dir)) != NULL)
5001 {
5002 struct stat st;
5003 struct statfs stfs;
5004 int fd;
5005
5006 fd = atoi (entry->d_name);
5007 if (!fd)
5008 continue;
5009
5010 sprintf (path, "/proc/%ld/fd/%d", pid, fd);
5011 if (stat (path, &st) != 0)
5012 continue;
5013 if (!S_ISDIR (st.st_mode))
5014 continue;
5015
5016 if (statfs (path, &stfs) != 0)
5017 continue;
5018 if (stfs.f_type != SPUFS_MAGIC)
5019 continue;
5020
5021 if (pos >= offset && pos + 4 <= offset + len)
5022 {
5023 *(unsigned int *)(buf + pos - offset) = fd;
5024 written += 4;
5025 }
5026 pos += 4;
5027 }
5028
5029 closedir (dir);
5030 return written;
5031 }
5032
5033 /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
5034 object type, using the /proc file system. */
5035 static int
5036 linux_qxfer_spu (const char *annex, unsigned char *readbuf,
5037 unsigned const char *writebuf,
5038 CORE_ADDR offset, int len)
5039 {
5040 long pid = lwpid_of (current_inferior);
5041 char buf[128];
5042 int fd = 0;
5043 int ret = 0;
5044
5045 if (!writebuf && !readbuf)
5046 return -1;
5047
5048 if (!*annex)
5049 {
5050 if (!readbuf)
5051 return -1;
5052 else
5053 return spu_enumerate_spu_ids (pid, readbuf, offset, len);
5054 }
5055
5056 sprintf (buf, "/proc/%ld/fd/%s", pid, annex);
5057 fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
5058 if (fd <= 0)
5059 return -1;
5060
5061 if (offset != 0
5062 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
5063 {
5064 close (fd);
5065 return 0;
5066 }
5067
5068 if (writebuf)
5069 ret = write (fd, writebuf, (size_t) len);
5070 else
5071 ret = read (fd, readbuf, (size_t) len);
5072
5073 close (fd);
5074 return ret;
5075 }
5076
5077 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
5078 struct target_loadseg
5079 {
5080 /* Core address to which the segment is mapped. */
5081 Elf32_Addr addr;
5082 /* VMA recorded in the program header. */
5083 Elf32_Addr p_vaddr;
5084 /* Size of this segment in memory. */
5085 Elf32_Word p_memsz;
5086 };
5087
5088 # if defined PT_GETDSBT
5089 struct target_loadmap
5090 {
5091 /* Protocol version number, must be zero. */
5092 Elf32_Word version;
5093 /* Pointer to the DSBT table, its size, and the DSBT index. */
5094 unsigned *dsbt_table;
5095 unsigned dsbt_size, dsbt_index;
5096 /* Number of segments in this map. */
5097 Elf32_Word nsegs;
5098 /* The actual memory map. */
5099 struct target_loadseg segs[/*nsegs*/];
5100 };
5101 # define LINUX_LOADMAP PT_GETDSBT
5102 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
5103 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
5104 # else
5105 struct target_loadmap
5106 {
5107 /* Protocol version number, must be zero. */
5108 Elf32_Half version;
5109 /* Number of segments in this map. */
5110 Elf32_Half nsegs;
5111 /* The actual memory map. */
5112 struct target_loadseg segs[/*nsegs*/];
5113 };
5114 # define LINUX_LOADMAP PTRACE_GETFDPIC
5115 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
5116 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
5117 # endif
5118
5119 static int
5120 linux_read_loadmap (const char *annex, CORE_ADDR offset,
5121 unsigned char *myaddr, unsigned int len)
5122 {
5123 int pid = lwpid_of (get_thread_lwp (current_inferior));
5124 int addr = -1;
5125 struct target_loadmap *data = NULL;
5126 unsigned int actual_length, copy_length;
5127
5128 if (strcmp (annex, "exec") == 0)
5129 addr = (int) LINUX_LOADMAP_EXEC;
5130 else if (strcmp (annex, "interp") == 0)
5131 addr = (int) LINUX_LOADMAP_INTERP;
5132 else
5133 return -1;
5134
5135 if (ptrace (LINUX_LOADMAP, pid, addr, &data) != 0)
5136 return -1;
5137
5138 if (data == NULL)
5139 return -1;
5140
5141 actual_length = sizeof (struct target_loadmap)
5142 + sizeof (struct target_loadseg) * data->nsegs;
5143
5144 if (offset < 0 || offset > actual_length)
5145 return -1;
5146
5147 copy_length = actual_length - offset < len ? actual_length - offset : len;
5148 memcpy (myaddr, (char *) data + offset, copy_length);
5149 return copy_length;
5150 }
5151 #else
5152 # define linux_read_loadmap NULL
5153 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
5154
5155 static void
5156 linux_process_qsupported (const char *query)
5157 {
5158 if (the_low_target.process_qsupported != NULL)
5159 the_low_target.process_qsupported (query);
5160 }
5161
5162 static int
5163 linux_supports_tracepoints (void)
5164 {
5165 if (*the_low_target.supports_tracepoints == NULL)
5166 return 0;
5167
5168 return (*the_low_target.supports_tracepoints) ();
5169 }
5170
5171 static CORE_ADDR
5172 linux_read_pc (struct regcache *regcache)
5173 {
5174 if (the_low_target.get_pc == NULL)
5175 return 0;
5176
5177 return (*the_low_target.get_pc) (regcache);
5178 }
5179
5180 static void
5181 linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
5182 {
5183 gdb_assert (the_low_target.set_pc != NULL);
5184
5185 (*the_low_target.set_pc) (regcache, pc);
5186 }
5187
5188 static int
5189 linux_thread_stopped (struct thread_info *thread)
5190 {
5191 return get_thread_lwp (thread)->stopped;
5192 }
5193
5194 /* This exposes stop-all-threads functionality to other modules. */
5195
5196 static void
5197 linux_pause_all (int freeze)
5198 {
5199 stop_all_lwps (freeze, NULL);
5200 }
5201
5202 /* This exposes unstop-all-threads functionality to other gdbserver
5203 modules. */
5204
5205 static void
5206 linux_unpause_all (int unfreeze)
5207 {
5208 unstop_all_lwps (unfreeze, NULL);
5209 }
5210
5211 static int
5212 linux_prepare_to_access_memory (void)
5213 {
5214 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5215 running LWP. */
5216 if (non_stop)
5217 linux_pause_all (1);
5218 return 0;
5219 }
5220
5221 static void
5222 linux_done_accessing_memory (void)
5223 {
5224 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5225 running LWP. */
5226 if (non_stop)
5227 linux_unpause_all (1);
5228 }
5229
5230 static int
5231 linux_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr,
5232 CORE_ADDR collector,
5233 CORE_ADDR lockaddr,
5234 ULONGEST orig_size,
5235 CORE_ADDR *jump_entry,
5236 CORE_ADDR *trampoline,
5237 ULONGEST *trampoline_size,
5238 unsigned char *jjump_pad_insn,
5239 ULONGEST *jjump_pad_insn_size,
5240 CORE_ADDR *adjusted_insn_addr,
5241 CORE_ADDR *adjusted_insn_addr_end,
5242 char *err)
5243 {
5244 return (*the_low_target.install_fast_tracepoint_jump_pad)
5245 (tpoint, tpaddr, collector, lockaddr, orig_size,
5246 jump_entry, trampoline, trampoline_size,
5247 jjump_pad_insn, jjump_pad_insn_size,
5248 adjusted_insn_addr, adjusted_insn_addr_end,
5249 err);
5250 }
5251
5252 static struct emit_ops *
5253 linux_emit_ops (void)
5254 {
5255 if (the_low_target.emit_ops != NULL)
5256 return (*the_low_target.emit_ops) ();
5257 else
5258 return NULL;
5259 }
5260
5261 static int
5262 linux_get_min_fast_tracepoint_insn_len (void)
5263 {
5264 return (*the_low_target.get_min_fast_tracepoint_insn_len) ();
5265 }
5266
5267 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
5268
5269 static int
5270 get_phdr_phnum_from_proc_auxv (const int pid, const int is_elf64,
5271 CORE_ADDR *phdr_memaddr, int *num_phdr)
5272 {
5273 char filename[PATH_MAX];
5274 int fd;
5275 const int auxv_size = is_elf64
5276 ? sizeof (Elf64_auxv_t) : sizeof (Elf32_auxv_t);
5277 char buf[sizeof (Elf64_auxv_t)]; /* The larger of the two. */
5278
5279 xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
5280
5281 fd = open (filename, O_RDONLY);
5282 if (fd < 0)
5283 return 1;
5284
5285 *phdr_memaddr = 0;
5286 *num_phdr = 0;
5287 while (read (fd, buf, auxv_size) == auxv_size
5288 && (*phdr_memaddr == 0 || *num_phdr == 0))
5289 {
5290 if (is_elf64)
5291 {
5292 Elf64_auxv_t *const aux = (Elf64_auxv_t *) buf;
5293
5294 switch (aux->a_type)
5295 {
5296 case AT_PHDR:
5297 *phdr_memaddr = aux->a_un.a_val;
5298 break;
5299 case AT_PHNUM:
5300 *num_phdr = aux->a_un.a_val;
5301 break;
5302 }
5303 }
5304 else
5305 {
5306 Elf32_auxv_t *const aux = (Elf32_auxv_t *) buf;
5307
5308 switch (aux->a_type)
5309 {
5310 case AT_PHDR:
5311 *phdr_memaddr = aux->a_un.a_val;
5312 break;
5313 case AT_PHNUM:
5314 *num_phdr = aux->a_un.a_val;
5315 break;
5316 }
5317 }
5318 }
5319
5320 close (fd);
5321
5322 if (*phdr_memaddr == 0 || *num_phdr == 0)
5323 {
5324 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
5325 "phdr_memaddr = %ld, phdr_num = %d",
5326 (long) *phdr_memaddr, *num_phdr);
5327 return 2;
5328 }
5329
5330 return 0;
5331 }
5332
5333 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
5334
5335 static CORE_ADDR
5336 get_dynamic (const int pid, const int is_elf64)
5337 {
5338 CORE_ADDR phdr_memaddr, relocation;
5339 int num_phdr, i;
5340 unsigned char *phdr_buf;
5341 const int phdr_size = is_elf64 ? sizeof (Elf64_Phdr) : sizeof (Elf32_Phdr);
5342
5343 if (get_phdr_phnum_from_proc_auxv (pid, is_elf64, &phdr_memaddr, &num_phdr))
5344 return 0;
5345
5346 gdb_assert (num_phdr < 100); /* Basic sanity check. */
5347 phdr_buf = alloca (num_phdr * phdr_size);
5348
5349 if (linux_read_memory (phdr_memaddr, phdr_buf, num_phdr * phdr_size))
5350 return 0;
5351
5352 /* Compute relocation: it is expected to be 0 for "regular" executables,
5353 non-zero for PIE ones. */
5354 relocation = -1;
5355 for (i = 0; relocation == -1 && i < num_phdr; i++)
5356 if (is_elf64)
5357 {
5358 Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
5359
5360 if (p->p_type == PT_PHDR)
5361 relocation = phdr_memaddr - p->p_vaddr;
5362 }
5363 else
5364 {
5365 Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
5366
5367 if (p->p_type == PT_PHDR)
5368 relocation = phdr_memaddr - p->p_vaddr;
5369 }
5370
5371 if (relocation == -1)
5372 {
5373 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
5374 any real world executables, including PIE executables, have always
5375 PT_PHDR present. PT_PHDR is not present in some shared libraries or
5376 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
5377 or present DT_DEBUG anyway (fpc binaries are statically linked).
5378
5379 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
5380
5381 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
5382
5383 return 0;
5384 }
5385
5386 for (i = 0; i < num_phdr; i++)
5387 {
5388 if (is_elf64)
5389 {
5390 Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
5391
5392 if (p->p_type == PT_DYNAMIC)
5393 return p->p_vaddr + relocation;
5394 }
5395 else
5396 {
5397 Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
5398
5399 if (p->p_type == PT_DYNAMIC)
5400 return p->p_vaddr + relocation;
5401 }
5402 }
5403
5404 return 0;
5405 }
5406
5407 /* Return &_r_debug in the inferior, or -1 if not present. Return value
5408 can be 0 if the inferior does not yet have the library list initialized.
5409 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
5410 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
5411
5412 static CORE_ADDR
5413 get_r_debug (const int pid, const int is_elf64)
5414 {
5415 CORE_ADDR dynamic_memaddr;
5416 const int dyn_size = is_elf64 ? sizeof (Elf64_Dyn) : sizeof (Elf32_Dyn);
5417 unsigned char buf[sizeof (Elf64_Dyn)]; /* The larger of the two. */
5418 CORE_ADDR map = -1;
5419
5420 dynamic_memaddr = get_dynamic (pid, is_elf64);
5421 if (dynamic_memaddr == 0)
5422 return map;
5423
5424 while (linux_read_memory (dynamic_memaddr, buf, dyn_size) == 0)
5425 {
5426 if (is_elf64)
5427 {
5428 Elf64_Dyn *const dyn = (Elf64_Dyn *) buf;
5429 #ifdef DT_MIPS_RLD_MAP
5430 union
5431 {
5432 Elf64_Xword map;
5433 unsigned char buf[sizeof (Elf64_Xword)];
5434 }
5435 rld_map;
5436
5437 if (dyn->d_tag == DT_MIPS_RLD_MAP)
5438 {
5439 if (linux_read_memory (dyn->d_un.d_val,
5440 rld_map.buf, sizeof (rld_map.buf)) == 0)
5441 return rld_map.map;
5442 else
5443 break;
5444 }
5445 #endif /* DT_MIPS_RLD_MAP */
5446
5447 if (dyn->d_tag == DT_DEBUG && map == -1)
5448 map = dyn->d_un.d_val;
5449
5450 if (dyn->d_tag == DT_NULL)
5451 break;
5452 }
5453 else
5454 {
5455 Elf32_Dyn *const dyn = (Elf32_Dyn *) buf;
5456 #ifdef DT_MIPS_RLD_MAP
5457 union
5458 {
5459 Elf32_Word map;
5460 unsigned char buf[sizeof (Elf32_Word)];
5461 }
5462 rld_map;
5463
5464 if (dyn->d_tag == DT_MIPS_RLD_MAP)
5465 {
5466 if (linux_read_memory (dyn->d_un.d_val,
5467 rld_map.buf, sizeof (rld_map.buf)) == 0)
5468 return rld_map.map;
5469 else
5470 break;
5471 }
5472 #endif /* DT_MIPS_RLD_MAP */
5473
5474 if (dyn->d_tag == DT_DEBUG && map == -1)
5475 map = dyn->d_un.d_val;
5476
5477 if (dyn->d_tag == DT_NULL)
5478 break;
5479 }
5480
5481 dynamic_memaddr += dyn_size;
5482 }
5483
5484 return map;
5485 }
5486
5487 /* Read one pointer from MEMADDR in the inferior. */
5488
5489 static int
5490 read_one_ptr (CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size)
5491 {
5492 int ret;
5493
5494 /* Go through a union so this works on either big or little endian
5495 hosts, when the inferior's pointer size is smaller than the size
5496 of CORE_ADDR. It is assumed the inferior's endianness is the
5497 same of the superior's. */
5498 union
5499 {
5500 CORE_ADDR core_addr;
5501 unsigned int ui;
5502 unsigned char uc;
5503 } addr;
5504
5505 ret = linux_read_memory (memaddr, &addr.uc, ptr_size);
5506 if (ret == 0)
5507 {
5508 if (ptr_size == sizeof (CORE_ADDR))
5509 *ptr = addr.core_addr;
5510 else if (ptr_size == sizeof (unsigned int))
5511 *ptr = addr.ui;
5512 else
5513 gdb_assert_not_reached ("unhandled pointer size");
5514 }
5515 return ret;
5516 }
5517
5518 struct link_map_offsets
5519 {
5520 /* Offset and size of r_debug.r_version. */
5521 int r_version_offset;
5522
5523 /* Offset and size of r_debug.r_map. */
5524 int r_map_offset;
5525
5526 /* Offset to l_addr field in struct link_map. */
5527 int l_addr_offset;
5528
5529 /* Offset to l_name field in struct link_map. */
5530 int l_name_offset;
5531
5532 /* Offset to l_ld field in struct link_map. */
5533 int l_ld_offset;
5534
5535 /* Offset to l_next field in struct link_map. */
5536 int l_next_offset;
5537
5538 /* Offset to l_prev field in struct link_map. */
5539 int l_prev_offset;
5540 };
5541
5542 /* Construct qXfer:libraries-svr4:read reply. */
5543
5544 static int
5545 linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf,
5546 unsigned const char *writebuf,
5547 CORE_ADDR offset, int len)
5548 {
5549 char *document;
5550 unsigned document_len;
5551 struct process_info_private *const priv = current_process ()->private;
5552 char filename[PATH_MAX];
5553 int pid, is_elf64;
5554
5555 static const struct link_map_offsets lmo_32bit_offsets =
5556 {
5557 0, /* r_version offset. */
5558 4, /* r_debug.r_map offset. */
5559 0, /* l_addr offset in link_map. */
5560 4, /* l_name offset in link_map. */
5561 8, /* l_ld offset in link_map. */
5562 12, /* l_next offset in link_map. */
5563 16 /* l_prev offset in link_map. */
5564 };
5565
5566 static const struct link_map_offsets lmo_64bit_offsets =
5567 {
5568 0, /* r_version offset. */
5569 8, /* r_debug.r_map offset. */
5570 0, /* l_addr offset in link_map. */
5571 8, /* l_name offset in link_map. */
5572 16, /* l_ld offset in link_map. */
5573 24, /* l_next offset in link_map. */
5574 32 /* l_prev offset in link_map. */
5575 };
5576 const struct link_map_offsets *lmo;
5577 unsigned int machine;
5578 int ptr_size;
5579 CORE_ADDR lm_addr = 0, lm_prev = 0;
5580 int allocated = 1024;
5581 char *p;
5582 CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
5583 int header_done = 0;
5584
5585 if (writebuf != NULL)
5586 return -2;
5587 if (readbuf == NULL)
5588 return -1;
5589
5590 pid = lwpid_of (current_inferior);
5591 xsnprintf (filename, sizeof filename, "/proc/%d/exe", pid);
5592 is_elf64 = elf_64_file_p (filename, &machine);
5593 lmo = is_elf64 ? &lmo_64bit_offsets : &lmo_32bit_offsets;
5594 ptr_size = is_elf64 ? 8 : 4;
5595
5596 while (annex[0] != '\0')
5597 {
5598 const char *sep;
5599 CORE_ADDR *addrp;
5600 int len;
5601
5602 sep = strchr (annex, '=');
5603 if (sep == NULL)
5604 break;
5605
5606 len = sep - annex;
5607 if (len == 5 && strncmp (annex, "start", 5) == 0)
5608 addrp = &lm_addr;
5609 else if (len == 4 && strncmp (annex, "prev", 4) == 0)
5610 addrp = &lm_prev;
5611 else
5612 {
5613 annex = strchr (sep, ';');
5614 if (annex == NULL)
5615 break;
5616 annex++;
5617 continue;
5618 }
5619
5620 annex = decode_address_to_semicolon (addrp, sep + 1);
5621 }
5622
5623 if (lm_addr == 0)
5624 {
5625 int r_version = 0;
5626
5627 if (priv->r_debug == 0)
5628 priv->r_debug = get_r_debug (pid, is_elf64);
5629
5630 /* We failed to find DT_DEBUG. Such situation will not change
5631 for this inferior - do not retry it. Report it to GDB as
5632 E01, see for the reasons at the GDB solib-svr4.c side. */
5633 if (priv->r_debug == (CORE_ADDR) -1)
5634 return -1;
5635
5636 if (priv->r_debug != 0)
5637 {
5638 if (linux_read_memory (priv->r_debug + lmo->r_version_offset,
5639 (unsigned char *) &r_version,
5640 sizeof (r_version)) != 0
5641 || r_version != 1)
5642 {
5643 warning ("unexpected r_debug version %d", r_version);
5644 }
5645 else if (read_one_ptr (priv->r_debug + lmo->r_map_offset,
5646 &lm_addr, ptr_size) != 0)
5647 {
5648 warning ("unable to read r_map from 0x%lx",
5649 (long) priv->r_debug + lmo->r_map_offset);
5650 }
5651 }
5652 }
5653
5654 document = xmalloc (allocated);
5655 strcpy (document, "<library-list-svr4 version=\"1.0\"");
5656 p = document + strlen (document);
5657
5658 while (lm_addr
5659 && read_one_ptr (lm_addr + lmo->l_name_offset,
5660 &l_name, ptr_size) == 0
5661 && read_one_ptr (lm_addr + lmo->l_addr_offset,
5662 &l_addr, ptr_size) == 0
5663 && read_one_ptr (lm_addr + lmo->l_ld_offset,
5664 &l_ld, ptr_size) == 0
5665 && read_one_ptr (lm_addr + lmo->l_prev_offset,
5666 &l_prev, ptr_size) == 0
5667 && read_one_ptr (lm_addr + lmo->l_next_offset,
5668 &l_next, ptr_size) == 0)
5669 {
5670 unsigned char libname[PATH_MAX];
5671
5672 if (lm_prev != l_prev)
5673 {
5674 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
5675 (long) lm_prev, (long) l_prev);
5676 break;
5677 }
5678
5679 /* Ignore the first entry even if it has valid name as the first entry
5680 corresponds to the main executable. The first entry should not be
5681 skipped if the dynamic loader was loaded late by a static executable
5682 (see solib-svr4.c parameter ignore_first). But in such case the main
5683 executable does not have PT_DYNAMIC present and this function already
5684 exited above due to failed get_r_debug. */
5685 if (lm_prev == 0)
5686 {
5687 sprintf (p, " main-lm=\"0x%lx\"", (unsigned long) lm_addr);
5688 p = p + strlen (p);
5689 }
5690 else
5691 {
5692 /* Not checking for error because reading may stop before
5693 we've got PATH_MAX worth of characters. */
5694 libname[0] = '\0';
5695 linux_read_memory (l_name, libname, sizeof (libname) - 1);
5696 libname[sizeof (libname) - 1] = '\0';
5697 if (libname[0] != '\0')
5698 {
5699 /* 6x the size for xml_escape_text below. */
5700 size_t len = 6 * strlen ((char *) libname);
5701 char *name;
5702
5703 if (!header_done)
5704 {
5705 /* Terminate `<library-list-svr4'. */
5706 *p++ = '>';
5707 header_done = 1;
5708 }
5709
5710 while (allocated < p - document + len + 200)
5711 {
5712 /* Expand to guarantee sufficient storage. */
5713 uintptr_t document_len = p - document;
5714
5715 document = xrealloc (document, 2 * allocated);
5716 allocated *= 2;
5717 p = document + document_len;
5718 }
5719
5720 name = xml_escape_text ((char *) libname);
5721 p += sprintf (p, "<library name=\"%s\" lm=\"0x%lx\" "
5722 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
5723 name, (unsigned long) lm_addr,
5724 (unsigned long) l_addr, (unsigned long) l_ld);
5725 free (name);
5726 }
5727 }
5728
5729 lm_prev = lm_addr;
5730 lm_addr = l_next;
5731 }
5732
5733 if (!header_done)
5734 {
5735 /* Empty list; terminate `<library-list-svr4'. */
5736 strcpy (p, "/>");
5737 }
5738 else
5739 strcpy (p, "</library-list-svr4>");
5740
5741 document_len = strlen (document);
5742 if (offset < document_len)
5743 document_len -= offset;
5744 else
5745 document_len = 0;
5746 if (len > document_len)
5747 len = document_len;
5748
5749 memcpy (readbuf, document + offset, len);
5750 xfree (document);
5751
5752 return len;
5753 }
5754
5755 #ifdef HAVE_LINUX_BTRACE
5756
5757 /* See to_enable_btrace target method. */
5758
5759 static struct btrace_target_info *
5760 linux_low_enable_btrace (ptid_t ptid)
5761 {
5762 struct btrace_target_info *tinfo;
5763
5764 tinfo = linux_enable_btrace (ptid);
5765
5766 if (tinfo != NULL)
5767 {
5768 struct thread_info *thread = find_thread_ptid (ptid);
5769 struct regcache *regcache = get_thread_regcache (thread, 0);
5770
5771 tinfo->ptr_bits = register_size (regcache->tdesc, 0) * 8;
5772 }
5773
5774 return tinfo;
5775 }
5776
5777 /* See to_disable_btrace target method. */
5778
5779 static int
5780 linux_low_disable_btrace (struct btrace_target_info *tinfo)
5781 {
5782 enum btrace_error err;
5783
5784 err = linux_disable_btrace (tinfo);
5785 return (err == BTRACE_ERR_NONE ? 0 : -1);
5786 }
5787
5788 /* See to_read_btrace target method. */
5789
5790 static int
5791 linux_low_read_btrace (struct btrace_target_info *tinfo, struct buffer *buffer,
5792 int type)
5793 {
5794 VEC (btrace_block_s) *btrace;
5795 struct btrace_block *block;
5796 enum btrace_error err;
5797 int i;
5798
5799 btrace = NULL;
5800 err = linux_read_btrace (&btrace, tinfo, type);
5801 if (err != BTRACE_ERR_NONE)
5802 {
5803 if (err == BTRACE_ERR_OVERFLOW)
5804 buffer_grow_str0 (buffer, "E.Overflow.");
5805 else
5806 buffer_grow_str0 (buffer, "E.Generic Error.");
5807
5808 return -1;
5809 }
5810
5811 buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
5812 buffer_grow_str (buffer, "<btrace version=\"1.0\">\n");
5813
5814 for (i = 0; VEC_iterate (btrace_block_s, btrace, i, block); i++)
5815 buffer_xml_printf (buffer, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
5816 paddress (block->begin), paddress (block->end));
5817
5818 buffer_grow_str0 (buffer, "</btrace>\n");
5819
5820 VEC_free (btrace_block_s, btrace);
5821
5822 return 0;
5823 }
5824 #endif /* HAVE_LINUX_BTRACE */
5825
5826 static struct target_ops linux_target_ops = {
5827 linux_create_inferior,
5828 linux_attach,
5829 linux_kill,
5830 linux_detach,
5831 linux_mourn,
5832 linux_join,
5833 linux_thread_alive,
5834 linux_resume,
5835 linux_wait,
5836 linux_fetch_registers,
5837 linux_store_registers,
5838 linux_prepare_to_access_memory,
5839 linux_done_accessing_memory,
5840 linux_read_memory,
5841 linux_write_memory,
5842 linux_look_up_symbols,
5843 linux_request_interrupt,
5844 linux_read_auxv,
5845 linux_insert_point,
5846 linux_remove_point,
5847 linux_stopped_by_watchpoint,
5848 linux_stopped_data_address,
5849 #if defined(__UCLIBC__) && defined(HAS_NOMMU) \
5850 && defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) \
5851 && defined(PT_TEXT_END_ADDR)
5852 linux_read_offsets,
5853 #else
5854 NULL,
5855 #endif
5856 #ifdef USE_THREAD_DB
5857 thread_db_get_tls_address,
5858 #else
5859 NULL,
5860 #endif
5861 linux_qxfer_spu,
5862 hostio_last_error_from_errno,
5863 linux_qxfer_osdata,
5864 linux_xfer_siginfo,
5865 linux_supports_non_stop,
5866 linux_async,
5867 linux_start_non_stop,
5868 linux_supports_multi_process,
5869 #ifdef USE_THREAD_DB
5870 thread_db_handle_monitor_command,
5871 #else
5872 NULL,
5873 #endif
5874 linux_common_core_of_thread,
5875 linux_read_loadmap,
5876 linux_process_qsupported,
5877 linux_supports_tracepoints,
5878 linux_read_pc,
5879 linux_write_pc,
5880 linux_thread_stopped,
5881 NULL,
5882 linux_pause_all,
5883 linux_unpause_all,
5884 linux_cancel_breakpoints,
5885 linux_stabilize_threads,
5886 linux_install_fast_tracepoint_jump_pad,
5887 linux_emit_ops,
5888 linux_supports_disable_randomization,
5889 linux_get_min_fast_tracepoint_insn_len,
5890 linux_qxfer_libraries_svr4,
5891 linux_supports_agent,
5892 #ifdef HAVE_LINUX_BTRACE
5893 linux_supports_btrace,
5894 linux_low_enable_btrace,
5895 linux_low_disable_btrace,
5896 linux_low_read_btrace,
5897 #else
5898 NULL,
5899 NULL,
5900 NULL,
5901 NULL,
5902 #endif
5903 linux_supports_range_stepping,
5904 };
5905
5906 static void
5907 linux_init_signals ()
5908 {
5909 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
5910 to find what the cancel signal actually is. */
5911 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
5912 signal (__SIGRTMIN+1, SIG_IGN);
5913 #endif
5914 }
5915
5916 #ifdef HAVE_LINUX_REGSETS
5917 void
5918 initialize_regsets_info (struct regsets_info *info)
5919 {
5920 for (info->num_regsets = 0;
5921 info->regsets[info->num_regsets].size >= 0;
5922 info->num_regsets++)
5923 ;
5924 }
5925 #endif
5926
5927 void
5928 initialize_low (void)
5929 {
5930 struct sigaction sigchld_action;
5931 memset (&sigchld_action, 0, sizeof (sigchld_action));
5932 set_target_ops (&linux_target_ops);
5933 set_breakpoint_data (the_low_target.breakpoint,
5934 the_low_target.breakpoint_len);
5935 linux_init_signals ();
5936 linux_ptrace_init_warnings ();
5937
5938 sigchld_action.sa_handler = sigchld_handler;
5939 sigemptyset (&sigchld_action.sa_mask);
5940 sigchld_action.sa_flags = SA_RESTART;
5941 sigaction (SIGCHLD, &sigchld_action, NULL);
5942
5943 initialize_low_arch ();
5944 }