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