* linux-low.c (handle_extended_wait): Use linux_resume_one_lwp
[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, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "server.h"
21 #include "linux-low.h"
22 #include "ansidecl.h" /* For ATTRIBUTE_PACKED, must be bug in external.h. */
23 #include "elf/common.h"
24 #include "elf/external.h"
25
26 #include <sys/wait.h>
27 #include <stdio.h>
28 #include <sys/param.h>
29 #include <sys/ptrace.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
44 #ifndef PTRACE_GETSIGINFO
45 # define PTRACE_GETSIGINFO 0x4202
46 # define PTRACE_SETSIGINFO 0x4203
47 #endif
48
49 #ifndef O_LARGEFILE
50 #define O_LARGEFILE 0
51 #endif
52
53 /* If the system headers did not provide the constants, hard-code the normal
54 values. */
55 #ifndef PTRACE_EVENT_FORK
56
57 #define PTRACE_SETOPTIONS 0x4200
58 #define PTRACE_GETEVENTMSG 0x4201
59
60 /* options set using PTRACE_SETOPTIONS */
61 #define PTRACE_O_TRACESYSGOOD 0x00000001
62 #define PTRACE_O_TRACEFORK 0x00000002
63 #define PTRACE_O_TRACEVFORK 0x00000004
64 #define PTRACE_O_TRACECLONE 0x00000008
65 #define PTRACE_O_TRACEEXEC 0x00000010
66 #define PTRACE_O_TRACEVFORKDONE 0x00000020
67 #define PTRACE_O_TRACEEXIT 0x00000040
68
69 /* Wait extended result codes for the above trace options. */
70 #define PTRACE_EVENT_FORK 1
71 #define PTRACE_EVENT_VFORK 2
72 #define PTRACE_EVENT_CLONE 3
73 #define PTRACE_EVENT_EXEC 4
74 #define PTRACE_EVENT_VFORK_DONE 5
75 #define PTRACE_EVENT_EXIT 6
76
77 #endif /* PTRACE_EVENT_FORK */
78
79 /* We can't always assume that this flag is available, but all systems
80 with the ptrace event handlers also have __WALL, so it's safe to use
81 in some contexts. */
82 #ifndef __WALL
83 #define __WALL 0x40000000 /* Wait for any child. */
84 #endif
85
86 #ifdef __UCLIBC__
87 #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
88 #define HAS_NOMMU
89 #endif
90 #endif
91
92 /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
93 representation of the thread ID.
94
95 ``all_lwps'' is keyed by the process ID - which on Linux is (presently)
96 the same as the LWP ID.
97
98 ``all_processes'' is keyed by the "overall process ID", which
99 GNU/Linux calls tgid, "thread group ID". */
100
101 struct inferior_list all_lwps;
102
103 /* A list of all unknown processes which receive stop signals. Some other
104 process will presumably claim each of these as forked children
105 momentarily. */
106
107 struct inferior_list stopped_pids;
108
109 /* FIXME this is a bit of a hack, and could be removed. */
110 int stopping_threads;
111
112 /* FIXME make into a target method? */
113 int using_threads = 1;
114
115 /* This flag is true iff we've just created or attached to our first
116 inferior but it has not stopped yet. As soon as it does, we need
117 to call the low target's arch_setup callback. Doing this only on
118 the first inferior avoids reinializing the architecture on every
119 inferior, and avoids messing with the register caches of the
120 already running inferiors. NOTE: this assumes all inferiors under
121 control of gdbserver have the same architecture. */
122 static int new_inferior;
123
124 static void linux_resume_one_lwp (struct lwp_info *lwp,
125 int step, int signal, siginfo_t *info);
126 static void linux_resume (struct thread_resume *resume_info, size_t n);
127 static void stop_all_lwps (void);
128 static int linux_wait_for_event (ptid_t ptid, int *wstat, int options);
129 static int check_removed_breakpoint (struct lwp_info *event_child);
130 static void *add_lwp (ptid_t ptid);
131 static int my_waitpid (int pid, int *status, int flags);
132 static int linux_stopped_by_watchpoint (void);
133 static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
134
135 struct pending_signals
136 {
137 int signal;
138 siginfo_t info;
139 struct pending_signals *prev;
140 };
141
142 #define PTRACE_ARG3_TYPE long
143 #define PTRACE_XFER_TYPE long
144
145 #ifdef HAVE_LINUX_REGSETS
146 static char *disabled_regsets;
147 static int num_regsets;
148 #endif
149
150 /* The read/write ends of the pipe registered as waitable file in the
151 event loop. */
152 static int linux_event_pipe[2] = { -1, -1 };
153
154 /* True if we're currently in async mode. */
155 #define target_is_async_p() (linux_event_pipe[0] != -1)
156
157 static void send_sigstop (struct inferior_list_entry *entry);
158 static void wait_for_sigstop (struct inferior_list_entry *entry);
159
160 /* Accepts an integer PID; Returns a string representing a file that
161 can be opened to get info for the child process.
162 Space for the result is malloc'd, caller must free. */
163
164 char *
165 linux_child_pid_to_exec_file (int pid)
166 {
167 char *name1, *name2;
168
169 name1 = xmalloc (MAXPATHLEN);
170 name2 = xmalloc (MAXPATHLEN);
171 memset (name2, 0, MAXPATHLEN);
172
173 sprintf (name1, "/proc/%d/exe", pid);
174 if (readlink (name1, name2, MAXPATHLEN) > 0)
175 {
176 free (name1);
177 return name2;
178 }
179 else
180 {
181 free (name2);
182 return name1;
183 }
184 }
185
186 /* Return non-zero if HEADER is a 64-bit ELF file. */
187
188 static int
189 elf_64_header_p (const Elf64_External_Ehdr *header)
190 {
191 return (header->e_ident[EI_MAG0] == ELFMAG0
192 && header->e_ident[EI_MAG1] == ELFMAG1
193 && header->e_ident[EI_MAG2] == ELFMAG2
194 && header->e_ident[EI_MAG3] == ELFMAG3
195 && header->e_ident[EI_CLASS] == ELFCLASS64);
196 }
197
198 /* Return non-zero if FILE is a 64-bit ELF file,
199 zero if the file is not a 64-bit ELF file,
200 and -1 if the file is not accessible or doesn't exist. */
201
202 int
203 elf_64_file_p (const char *file)
204 {
205 Elf64_External_Ehdr header;
206 int fd;
207
208 fd = open (file, O_RDONLY);
209 if (fd < 0)
210 return -1;
211
212 if (read (fd, &header, sizeof (header)) != sizeof (header))
213 {
214 close (fd);
215 return 0;
216 }
217 close (fd);
218
219 return elf_64_header_p (&header);
220 }
221
222 static void
223 delete_lwp (struct lwp_info *lwp)
224 {
225 remove_thread (get_lwp_thread (lwp));
226 remove_inferior (&all_lwps, &lwp->head);
227 free (lwp);
228 }
229
230 /* Add a process to the common process list, and set its private
231 data. */
232
233 static struct process_info *
234 linux_add_process (int pid, int attached)
235 {
236 struct process_info *proc;
237
238 /* Is this the first process? If so, then set the arch. */
239 if (all_processes.head == NULL)
240 new_inferior = 1;
241
242 proc = add_process (pid, attached);
243 proc->private = xcalloc (1, sizeof (*proc->private));
244
245 return proc;
246 }
247
248 /* Remove a process from the common process list,
249 also freeing all private data. */
250
251 static void
252 linux_remove_process (struct process_info *process)
253 {
254 free (process->private);
255 remove_process (process);
256 }
257
258 /* Handle a GNU/Linux extended wait response. If we see a clone
259 event, we need to add the new LWP to our list (and not report the
260 trap to higher layers). */
261
262 static void
263 handle_extended_wait (struct lwp_info *event_child, int wstat)
264 {
265 int event = wstat >> 16;
266 struct lwp_info *new_lwp;
267
268 if (event == PTRACE_EVENT_CLONE)
269 {
270 ptid_t ptid;
271 unsigned long new_pid;
272 int ret, status = W_STOPCODE (SIGSTOP);
273
274 ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_child), 0, &new_pid);
275
276 /* If we haven't already seen the new PID stop, wait for it now. */
277 if (! pull_pid_from_list (&stopped_pids, new_pid))
278 {
279 /* The new child has a pending SIGSTOP. We can't affect it until it
280 hits the SIGSTOP, but we're already attached. */
281
282 ret = my_waitpid (new_pid, &status, __WALL);
283
284 if (ret == -1)
285 perror_with_name ("waiting for new child");
286 else if (ret != new_pid)
287 warning ("wait returned unexpected PID %d", ret);
288 else if (!WIFSTOPPED (status))
289 warning ("wait returned unexpected status 0x%x", status);
290 }
291
292 ptrace (PTRACE_SETOPTIONS, new_pid, 0, PTRACE_O_TRACECLONE);
293
294 ptid = ptid_build (pid_of (event_child), new_pid, 0);
295 new_lwp = (struct lwp_info *) add_lwp (ptid);
296 add_thread (ptid, new_lwp);
297
298 /* Either we're going to immediately resume the new thread
299 or leave it stopped. linux_resume_one_lwp is a nop if it
300 thinks the thread is currently running, so set this first
301 before calling linux_resume_one_lwp. */
302 new_lwp->stopped = 1;
303
304 /* Normally we will get the pending SIGSTOP. But in some cases
305 we might get another signal delivered to the group first.
306 If we do get another signal, be sure not to lose it. */
307 if (WSTOPSIG (status) == SIGSTOP)
308 {
309 if (! stopping_threads)
310 linux_resume_one_lwp (new_lwp, 0, 0, NULL);
311 }
312 else
313 {
314 new_lwp->stop_expected = 1;
315 if (stopping_threads)
316 {
317 new_lwp->status_pending_p = 1;
318 new_lwp->status_pending = status;
319 }
320 else
321 /* Pass the signal on. This is what GDB does - except
322 shouldn't we really report it instead? */
323 linux_resume_one_lwp (new_lwp, 0, WSTOPSIG (status), NULL);
324 }
325
326 /* Always resume the current thread. If we are stopping
327 threads, it will have a pending SIGSTOP; we may as well
328 collect it now. */
329 linux_resume_one_lwp (event_child, event_child->stepping, 0, NULL);
330 }
331 }
332
333 /* This function should only be called if the process got a SIGTRAP.
334 The SIGTRAP could mean several things.
335
336 On i386, where decr_pc_after_break is non-zero:
337 If we were single-stepping this process using PTRACE_SINGLESTEP,
338 we will get only the one SIGTRAP (even if the instruction we
339 stepped over was a breakpoint). The value of $eip will be the
340 next instruction.
341 If we continue the process using PTRACE_CONT, we will get a
342 SIGTRAP when we hit a breakpoint. The value of $eip will be
343 the instruction after the breakpoint (i.e. needs to be
344 decremented). If we report the SIGTRAP to GDB, we must also
345 report the undecremented PC. If we cancel the SIGTRAP, we
346 must resume at the decremented PC.
347
348 (Presumably, not yet tested) On a non-decr_pc_after_break machine
349 with hardware or kernel single-step:
350 If we single-step over a breakpoint instruction, our PC will
351 point at the following instruction. If we continue and hit a
352 breakpoint instruction, our PC will point at the breakpoint
353 instruction. */
354
355 static CORE_ADDR
356 get_stop_pc (void)
357 {
358 CORE_ADDR stop_pc = (*the_low_target.get_pc) ();
359
360 if (! get_thread_lwp (current_inferior)->stepping)
361 stop_pc -= the_low_target.decr_pc_after_break;
362
363 if (debug_threads)
364 fprintf (stderr, "stop pc is 0x%lx\n", (long) stop_pc);
365
366 return stop_pc;
367 }
368
369 static void *
370 add_lwp (ptid_t ptid)
371 {
372 struct lwp_info *lwp;
373
374 lwp = (struct lwp_info *) xmalloc (sizeof (*lwp));
375 memset (lwp, 0, sizeof (*lwp));
376
377 lwp->head.id = ptid;
378
379 add_inferior_to_list (&all_lwps, &lwp->head);
380
381 return lwp;
382 }
383
384 /* Start an inferior process and returns its pid.
385 ALLARGS is a vector of program-name and args. */
386
387 static int
388 linux_create_inferior (char *program, char **allargs)
389 {
390 struct lwp_info *new_lwp;
391 int pid;
392 ptid_t ptid;
393
394 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
395 pid = vfork ();
396 #else
397 pid = fork ();
398 #endif
399 if (pid < 0)
400 perror_with_name ("fork");
401
402 if (pid == 0)
403 {
404 ptrace (PTRACE_TRACEME, 0, 0, 0);
405
406 signal (__SIGRTMIN + 1, SIG_DFL);
407
408 setpgid (0, 0);
409
410 execv (program, allargs);
411 if (errno == ENOENT)
412 execvp (program, allargs);
413
414 fprintf (stderr, "Cannot exec %s: %s.\n", program,
415 strerror (errno));
416 fflush (stderr);
417 _exit (0177);
418 }
419
420 linux_add_process (pid, 0);
421
422 ptid = ptid_build (pid, pid, 0);
423 new_lwp = add_lwp (ptid);
424 add_thread (ptid, new_lwp);
425 new_lwp->must_set_ptrace_flags = 1;
426
427 return pid;
428 }
429
430 /* Attach to an inferior process. */
431
432 static void
433 linux_attach_lwp_1 (unsigned long lwpid, int initial)
434 {
435 ptid_t ptid;
436 struct lwp_info *new_lwp;
437
438 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) != 0)
439 {
440 if (!initial)
441 {
442 /* If we fail to attach to an LWP, just warn. */
443 fprintf (stderr, "Cannot attach to lwp %ld: %s (%d)\n", lwpid,
444 strerror (errno), errno);
445 fflush (stderr);
446 return;
447 }
448 else
449 /* If we fail to attach to a process, report an error. */
450 error ("Cannot attach to lwp %ld: %s (%d)\n", lwpid,
451 strerror (errno), errno);
452 }
453
454 if (initial)
455 /* NOTE/FIXME: This lwp might have not been the tgid. */
456 ptid = ptid_build (lwpid, lwpid, 0);
457 else
458 {
459 /* Note that extracting the pid from the current inferior is
460 safe, since we're always called in the context of the same
461 process as this new thread. */
462 int pid = pid_of (get_thread_lwp (current_inferior));
463 ptid = ptid_build (pid, lwpid, 0);
464 }
465
466 new_lwp = (struct lwp_info *) add_lwp (ptid);
467 add_thread (ptid, new_lwp);
468
469
470 /* We need to wait for SIGSTOP before being able to make the next
471 ptrace call on this LWP. */
472 new_lwp->must_set_ptrace_flags = 1;
473
474 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
475 brings it to a halt.
476
477 There are several cases to consider here:
478
479 1) gdbserver has already attached to the process and is being notified
480 of a new thread that is being created.
481 In this case we should ignore that SIGSTOP and resume the process.
482 This is handled below by setting stop_expected = 1.
483
484 2) This is the first thread (the process thread), and we're attaching
485 to it via attach_inferior.
486 In this case we want the process thread to stop.
487 This is handled by having linux_attach clear stop_expected after
488 we return.
489 ??? If the process already has several threads we leave the other
490 threads running.
491
492 3) GDB is connecting to gdbserver and is requesting an enumeration of all
493 existing threads.
494 In this case we want the thread to stop.
495 FIXME: This case is currently not properly handled.
496 We should wait for the SIGSTOP but don't. Things work apparently
497 because enough time passes between when we ptrace (ATTACH) and when
498 gdb makes the next ptrace call on the thread.
499
500 On the other hand, if we are currently trying to stop all threads, we
501 should treat the new thread as if we had sent it a SIGSTOP. This works
502 because we are guaranteed that the add_lwp call above added us to the
503 end of the list, and so the new thread has not yet reached
504 wait_for_sigstop (but will). */
505 if (! stopping_threads)
506 new_lwp->stop_expected = 1;
507 }
508
509 void
510 linux_attach_lwp (unsigned long lwpid)
511 {
512 linux_attach_lwp_1 (lwpid, 0);
513 }
514
515 int
516 linux_attach (unsigned long pid)
517 {
518 struct lwp_info *lwp;
519
520 linux_attach_lwp_1 (pid, 1);
521
522 linux_add_process (pid, 1);
523
524 if (!non_stop)
525 {
526 /* Don't ignore the initial SIGSTOP if we just attached to this
527 process. It will be collected by wait shortly. */
528 lwp = (struct lwp_info *) find_inferior_id (&all_lwps,
529 ptid_build (pid, pid, 0));
530 lwp->stop_expected = 0;
531 }
532
533 return 0;
534 }
535
536 struct counter
537 {
538 int pid;
539 int count;
540 };
541
542 static int
543 second_thread_of_pid_p (struct inferior_list_entry *entry, void *args)
544 {
545 struct counter *counter = args;
546
547 if (ptid_get_pid (entry->id) == counter->pid)
548 {
549 if (++counter->count > 1)
550 return 1;
551 }
552
553 return 0;
554 }
555
556 static int
557 last_thread_of_process_p (struct thread_info *thread)
558 {
559 ptid_t ptid = ((struct inferior_list_entry *)thread)->id;
560 int pid = ptid_get_pid (ptid);
561 struct counter counter = { pid , 0 };
562
563 return (find_inferior (&all_threads,
564 second_thread_of_pid_p, &counter) == NULL);
565 }
566
567 /* Kill the inferior lwp. */
568
569 static int
570 linux_kill_one_lwp (struct inferior_list_entry *entry, void *args)
571 {
572 struct thread_info *thread = (struct thread_info *) entry;
573 struct lwp_info *lwp = get_thread_lwp (thread);
574 int wstat;
575 int pid = * (int *) args;
576
577 if (ptid_get_pid (entry->id) != pid)
578 return 0;
579
580 /* We avoid killing the first thread here, because of a Linux kernel (at
581 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
582 the children get a chance to be reaped, it will remain a zombie
583 forever. */
584
585 if (last_thread_of_process_p (thread))
586 {
587 if (debug_threads)
588 fprintf (stderr, "lkop: is last of process %s\n",
589 target_pid_to_str (entry->id));
590 return 0;
591 }
592
593 /* If we're killing a running inferior, make sure it is stopped
594 first, as PTRACE_KILL will not work otherwise. */
595 if (!lwp->stopped)
596 send_sigstop (&lwp->head);
597
598 do
599 {
600 ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0);
601
602 /* Make sure it died. The loop is most likely unnecessary. */
603 pid = linux_wait_for_event (lwp->head.id, &wstat, __WALL);
604 } while (pid > 0 && WIFSTOPPED (wstat));
605
606 return 0;
607 }
608
609 static int
610 linux_kill (int pid)
611 {
612 struct process_info *process;
613 struct lwp_info *lwp;
614 struct thread_info *thread;
615 int wstat;
616 int lwpid;
617
618 process = find_process_pid (pid);
619 if (process == NULL)
620 return -1;
621
622 find_inferior (&all_threads, linux_kill_one_lwp, &pid);
623
624 /* See the comment in linux_kill_one_lwp. We did not kill the first
625 thread in the list, so do so now. */
626 lwp = find_lwp_pid (pid_to_ptid (pid));
627 thread = get_lwp_thread (lwp);
628
629 if (debug_threads)
630 fprintf (stderr, "lk_1: killing lwp %ld, for pid: %d\n",
631 lwpid_of (lwp), pid);
632
633 /* If we're killing a running inferior, make sure it is stopped
634 first, as PTRACE_KILL will not work otherwise. */
635 if (!lwp->stopped)
636 send_sigstop (&lwp->head);
637
638 do
639 {
640 ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0);
641
642 /* Make sure it died. The loop is most likely unnecessary. */
643 lwpid = linux_wait_for_event (lwp->head.id, &wstat, __WALL);
644 } while (lwpid > 0 && WIFSTOPPED (wstat));
645
646 delete_lwp (lwp);
647 linux_remove_process (process);
648 return 0;
649 }
650
651 static int
652 linux_detach_one_lwp (struct inferior_list_entry *entry, void *args)
653 {
654 struct thread_info *thread = (struct thread_info *) entry;
655 struct lwp_info *lwp = get_thread_lwp (thread);
656 int pid = * (int *) args;
657
658 if (ptid_get_pid (entry->id) != pid)
659 return 0;
660
661 /* If we're detaching from a running inferior, make sure it is
662 stopped first, as PTRACE_DETACH will not work otherwise. */
663 if (!lwp->stopped)
664 {
665 int lwpid = lwpid_of (lwp);
666
667 stopping_threads = 1;
668 send_sigstop (&lwp->head);
669
670 /* If this detects a new thread through a clone event, the new
671 thread is appended to the end of the lwp list, so we'll
672 eventually detach from it. */
673 wait_for_sigstop (&lwp->head);
674 stopping_threads = 0;
675
676 /* If LWP exits while we're trying to stop it, there's nothing
677 left to do. */
678 lwp = find_lwp_pid (pid_to_ptid (lwpid));
679 if (lwp == NULL)
680 return 0;
681 }
682
683 /* Make sure the process isn't stopped at a breakpoint that's
684 no longer there. */
685 check_removed_breakpoint (lwp);
686
687 /* If this process is stopped but is expecting a SIGSTOP, then make
688 sure we take care of that now. This isn't absolutely guaranteed
689 to collect the SIGSTOP, but is fairly likely to. */
690 if (lwp->stop_expected)
691 {
692 int wstat;
693 /* Clear stop_expected, so that the SIGSTOP will be reported. */
694 lwp->stop_expected = 0;
695 if (lwp->stopped)
696 linux_resume_one_lwp (lwp, 0, 0, NULL);
697 linux_wait_for_event (lwp->head.id, &wstat, __WALL);
698 }
699
700 /* Flush any pending changes to the process's registers. */
701 regcache_invalidate_one ((struct inferior_list_entry *)
702 get_lwp_thread (lwp));
703
704 /* Finally, let it resume. */
705 ptrace (PTRACE_DETACH, lwpid_of (lwp), 0, 0);
706
707 delete_lwp (lwp);
708 return 0;
709 }
710
711 static int
712 any_thread_of (struct inferior_list_entry *entry, void *args)
713 {
714 int *pid_p = args;
715
716 if (ptid_get_pid (entry->id) == *pid_p)
717 return 1;
718
719 return 0;
720 }
721
722 static int
723 linux_detach (int pid)
724 {
725 struct process_info *process;
726
727 process = find_process_pid (pid);
728 if (process == NULL)
729 return -1;
730
731 current_inferior =
732 (struct thread_info *) find_inferior (&all_threads, any_thread_of, &pid);
733
734 delete_all_breakpoints ();
735 find_inferior (&all_threads, linux_detach_one_lwp, &pid);
736 linux_remove_process (process);
737 return 0;
738 }
739
740 static void
741 linux_join (int pid)
742 {
743 int status, ret;
744 struct process_info *process;
745
746 process = find_process_pid (pid);
747 if (process == NULL)
748 return;
749
750 do {
751 ret = my_waitpid (pid, &status, 0);
752 if (WIFEXITED (status) || WIFSIGNALED (status))
753 break;
754 } while (ret != -1 || errno != ECHILD);
755 }
756
757 /* Return nonzero if the given thread is still alive. */
758 static int
759 linux_thread_alive (ptid_t ptid)
760 {
761 struct lwp_info *lwp = find_lwp_pid (ptid);
762
763 /* We assume we always know if a thread exits. If a whole process
764 exited but we still haven't been able to report it to GDB, we'll
765 hold on to the last lwp of the dead process. */
766 if (lwp != NULL)
767 return !lwp->dead;
768 else
769 return 0;
770 }
771
772 /* Return nonzero if this process stopped at a breakpoint which
773 no longer appears to be inserted. Also adjust the PC
774 appropriately to resume where the breakpoint used to be. */
775 static int
776 check_removed_breakpoint (struct lwp_info *event_child)
777 {
778 CORE_ADDR stop_pc;
779 struct thread_info *saved_inferior;
780
781 if (event_child->pending_is_breakpoint == 0)
782 return 0;
783
784 if (debug_threads)
785 fprintf (stderr, "Checking for breakpoint in lwp %ld.\n",
786 lwpid_of (event_child));
787
788 saved_inferior = current_inferior;
789 current_inferior = get_lwp_thread (event_child);
790
791 stop_pc = get_stop_pc ();
792
793 /* If the PC has changed since we stopped, then we shouldn't do
794 anything. This happens if, for instance, GDB handled the
795 decr_pc_after_break subtraction itself. */
796 if (stop_pc != event_child->pending_stop_pc)
797 {
798 if (debug_threads)
799 fprintf (stderr, "Ignoring, PC was changed. Old PC was 0x%08llx\n",
800 event_child->pending_stop_pc);
801
802 event_child->pending_is_breakpoint = 0;
803 current_inferior = saved_inferior;
804 return 0;
805 }
806
807 /* If the breakpoint is still there, we will report hitting it. */
808 if ((*the_low_target.breakpoint_at) (stop_pc))
809 {
810 if (debug_threads)
811 fprintf (stderr, "Ignoring, breakpoint is still present.\n");
812 current_inferior = saved_inferior;
813 return 0;
814 }
815
816 if (debug_threads)
817 fprintf (stderr, "Removed breakpoint.\n");
818
819 /* For decr_pc_after_break targets, here is where we perform the
820 decrement. We go immediately from this function to resuming,
821 and can not safely call get_stop_pc () again. */
822 if (the_low_target.set_pc != NULL)
823 {
824 if (debug_threads)
825 fprintf (stderr, "Set pc to 0x%lx\n", (long) stop_pc);
826 (*the_low_target.set_pc) (stop_pc);
827 }
828
829 /* We consumed the pending SIGTRAP. */
830 event_child->pending_is_breakpoint = 0;
831 event_child->status_pending_p = 0;
832 event_child->status_pending = 0;
833
834 current_inferior = saved_inferior;
835 return 1;
836 }
837
838 /* Return 1 if this lwp has an interesting status pending. This
839 function may silently resume an inferior lwp. */
840 static int
841 status_pending_p (struct inferior_list_entry *entry, void *arg)
842 {
843 struct lwp_info *lwp = (struct lwp_info *) entry;
844 ptid_t ptid = * (ptid_t *) arg;
845
846 /* Check if we're only interested in events from a specific process
847 or its lwps. */
848 if (!ptid_equal (minus_one_ptid, ptid)
849 && ptid_get_pid (ptid) != ptid_get_pid (lwp->head.id))
850 return 0;
851
852 if (lwp->status_pending_p && !lwp->suspended)
853 if (check_removed_breakpoint (lwp))
854 {
855 /* This thread was stopped at a breakpoint, and the breakpoint
856 is now gone. We were told to continue (or step...) all threads,
857 so GDB isn't trying to single-step past this breakpoint.
858 So instead of reporting the old SIGTRAP, pretend we got to
859 the breakpoint just after it was removed instead of just
860 before; resume the process. */
861 linux_resume_one_lwp (lwp, 0, 0, NULL);
862 return 0;
863 }
864
865 return (lwp->status_pending_p && !lwp->suspended);
866 }
867
868 static int
869 same_lwp (struct inferior_list_entry *entry, void *data)
870 {
871 ptid_t ptid = *(ptid_t *) data;
872 int lwp;
873
874 if (ptid_get_lwp (ptid) != 0)
875 lwp = ptid_get_lwp (ptid);
876 else
877 lwp = ptid_get_pid (ptid);
878
879 if (ptid_get_lwp (entry->id) == lwp)
880 return 1;
881
882 return 0;
883 }
884
885 struct lwp_info *
886 find_lwp_pid (ptid_t ptid)
887 {
888 return (struct lwp_info*) find_inferior (&all_lwps, same_lwp, &ptid);
889 }
890
891 static struct lwp_info *
892 linux_wait_for_lwp (ptid_t ptid, int *wstatp, int options)
893 {
894 int ret;
895 int to_wait_for = -1;
896 struct lwp_info *child = NULL;
897
898 if (debug_threads)
899 fprintf (stderr, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid));
900
901 if (ptid_equal (ptid, minus_one_ptid))
902 to_wait_for = -1; /* any child */
903 else
904 to_wait_for = ptid_get_lwp (ptid); /* this lwp only */
905
906 options |= __WALL;
907
908 retry:
909
910 ret = my_waitpid (to_wait_for, wstatp, options);
911 if (ret == 0 || (ret == -1 && errno == ECHILD && (options & WNOHANG)))
912 return NULL;
913 else if (ret == -1)
914 perror_with_name ("waitpid");
915
916 if (debug_threads
917 && (!WIFSTOPPED (*wstatp)
918 || (WSTOPSIG (*wstatp) != 32
919 && WSTOPSIG (*wstatp) != 33)))
920 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
921
922 child = find_lwp_pid (pid_to_ptid (ret));
923
924 /* If we didn't find a process, one of two things presumably happened:
925 - A process we started and then detached from has exited. Ignore it.
926 - A process we are controlling has forked and the new child's stop
927 was reported to us by the kernel. Save its PID. */
928 if (child == NULL && WIFSTOPPED (*wstatp))
929 {
930 add_pid_to_list (&stopped_pids, ret);
931 goto retry;
932 }
933 else if (child == NULL)
934 goto retry;
935
936 child->stopped = 1;
937 child->pending_is_breakpoint = 0;
938
939 child->last_status = *wstatp;
940
941 /* Architecture-specific setup after inferior is running.
942 This needs to happen after we have attached to the inferior
943 and it is stopped for the first time, but before we access
944 any inferior registers. */
945 if (new_inferior)
946 {
947 the_low_target.arch_setup ();
948 #ifdef HAVE_LINUX_REGSETS
949 memset (disabled_regsets, 0, num_regsets);
950 #endif
951 new_inferior = 0;
952 }
953
954 if (debug_threads
955 && WIFSTOPPED (*wstatp)
956 && the_low_target.get_pc != NULL)
957 {
958 struct thread_info *saved_inferior = current_inferior;
959 CORE_ADDR pc;
960
961 current_inferior = (struct thread_info *)
962 find_inferior_id (&all_threads, child->head.id);
963 pc = (*the_low_target.get_pc) ();
964 fprintf (stderr, "linux_wait_for_lwp: pc is 0x%lx\n", (long) pc);
965 current_inferior = saved_inferior;
966 }
967
968 return child;
969 }
970
971 /* Wait for an event from child PID. If PID is -1, wait for any
972 child. Store the stop status through the status pointer WSTAT.
973 OPTIONS is passed to the waitpid call. Return 0 if no child stop
974 event was found and OPTIONS contains WNOHANG. Return the PID of
975 the stopped child otherwise. */
976
977 static int
978 linux_wait_for_event_1 (ptid_t ptid, int *wstat, int options)
979 {
980 CORE_ADDR stop_pc;
981 struct lwp_info *event_child = NULL;
982 int bp_status;
983 struct lwp_info *requested_child = NULL;
984
985 /* Check for a lwp with a pending status. */
986 /* It is possible that the user changed the pending task's registers since
987 it stopped. We correctly handle the change of PC if we hit a breakpoint
988 (in check_removed_breakpoint); signals should be reported anyway. */
989
990 if (ptid_equal (ptid, minus_one_ptid)
991 || ptid_equal (pid_to_ptid (ptid_get_pid (ptid)), ptid))
992 {
993 event_child = (struct lwp_info *)
994 find_inferior (&all_lwps, status_pending_p, &ptid);
995 if (debug_threads && event_child)
996 fprintf (stderr, "Got a pending child %ld\n", lwpid_of (event_child));
997 }
998 else
999 {
1000 requested_child = find_lwp_pid (ptid);
1001 if (requested_child->status_pending_p
1002 && !check_removed_breakpoint (requested_child))
1003 event_child = requested_child;
1004 }
1005
1006 if (event_child != NULL)
1007 {
1008 if (debug_threads)
1009 fprintf (stderr, "Got an event from pending child %ld (%04x)\n",
1010 lwpid_of (event_child), event_child->status_pending);
1011 *wstat = event_child->status_pending;
1012 event_child->status_pending_p = 0;
1013 event_child->status_pending = 0;
1014 current_inferior = get_lwp_thread (event_child);
1015 return lwpid_of (event_child);
1016 }
1017
1018 /* We only enter this loop if no process has a pending wait status. Thus
1019 any action taken in response to a wait status inside this loop is
1020 responding as soon as we detect the status, not after any pending
1021 events. */
1022 while (1)
1023 {
1024 event_child = linux_wait_for_lwp (ptid, wstat, options);
1025
1026 if ((options & WNOHANG) && event_child == NULL)
1027 return 0;
1028
1029 if (event_child == NULL)
1030 error ("event from unknown child");
1031
1032 current_inferior = get_lwp_thread (event_child);
1033
1034 /* Check for thread exit. */
1035 if (! WIFSTOPPED (*wstat))
1036 {
1037 if (debug_threads)
1038 fprintf (stderr, "LWP %ld exiting\n", lwpid_of (event_child));
1039
1040 /* If the last thread is exiting, just return. */
1041 if (last_thread_of_process_p (current_inferior))
1042 {
1043 if (debug_threads)
1044 fprintf (stderr, "LWP %ld is last lwp of process\n",
1045 lwpid_of (event_child));
1046 return lwpid_of (event_child);
1047 }
1048
1049 delete_lwp (event_child);
1050
1051 if (!non_stop)
1052 {
1053 current_inferior = (struct thread_info *) all_threads.head;
1054 if (debug_threads)
1055 fprintf (stderr, "Current inferior is now %ld\n",
1056 lwpid_of (get_thread_lwp (current_inferior)));
1057 }
1058 else
1059 {
1060 current_inferior = NULL;
1061 if (debug_threads)
1062 fprintf (stderr, "Current inferior is now <NULL>\n");
1063 }
1064
1065 /* If we were waiting for this particular child to do something...
1066 well, it did something. */
1067 if (requested_child != NULL)
1068 return lwpid_of (event_child);
1069
1070 /* Wait for a more interesting event. */
1071 continue;
1072 }
1073
1074 if (event_child->must_set_ptrace_flags)
1075 {
1076 ptrace (PTRACE_SETOPTIONS, lwpid_of (event_child),
1077 0, PTRACE_O_TRACECLONE);
1078 event_child->must_set_ptrace_flags = 0;
1079 }
1080
1081 if (WIFSTOPPED (*wstat)
1082 && WSTOPSIG (*wstat) == SIGSTOP
1083 && event_child->stop_expected)
1084 {
1085 if (debug_threads)
1086 fprintf (stderr, "Expected stop.\n");
1087 event_child->stop_expected = 0;
1088 linux_resume_one_lwp (event_child, event_child->stepping, 0, NULL);
1089 continue;
1090 }
1091
1092 if (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) == SIGTRAP
1093 && *wstat >> 16 != 0)
1094 {
1095 handle_extended_wait (event_child, *wstat);
1096 continue;
1097 }
1098
1099 /* If GDB is not interested in this signal, don't stop other
1100 threads, and don't report it to GDB. Just resume the
1101 inferior right away. We do this for threading-related
1102 signals as well as any that GDB specifically requested we
1103 ignore. But never ignore SIGSTOP if we sent it ourselves,
1104 and do not ignore signals when stepping - they may require
1105 special handling to skip the signal handler. */
1106 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
1107 thread library? */
1108 if (WIFSTOPPED (*wstat)
1109 && !event_child->stepping
1110 && (
1111 #ifdef USE_THREAD_DB
1112 (current_process ()->private->thread_db_active
1113 && (WSTOPSIG (*wstat) == __SIGRTMIN
1114 || WSTOPSIG (*wstat) == __SIGRTMIN + 1))
1115 ||
1116 #endif
1117 (pass_signals[target_signal_from_host (WSTOPSIG (*wstat))]
1118 && (WSTOPSIG (*wstat) != SIGSTOP || !stopping_threads))))
1119 {
1120 siginfo_t info, *info_p;
1121
1122 if (debug_threads)
1123 fprintf (stderr, "Ignored signal %d for LWP %ld.\n",
1124 WSTOPSIG (*wstat), lwpid_of (event_child));
1125
1126 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (event_child), 0, &info) == 0)
1127 info_p = &info;
1128 else
1129 info_p = NULL;
1130 linux_resume_one_lwp (event_child,
1131 event_child->stepping,
1132 WSTOPSIG (*wstat), info_p);
1133 continue;
1134 }
1135
1136 /* If this event was not handled above, and is not a SIGTRAP, report
1137 it. */
1138 if (!WIFSTOPPED (*wstat) || WSTOPSIG (*wstat) != SIGTRAP)
1139 return lwpid_of (event_child);
1140
1141 /* If this target does not support breakpoints, we simply report the
1142 SIGTRAP; it's of no concern to us. */
1143 if (the_low_target.get_pc == NULL)
1144 return lwpid_of (event_child);
1145
1146 stop_pc = get_stop_pc ();
1147
1148 /* bp_reinsert will only be set if we were single-stepping.
1149 Notice that we will resume the process after hitting
1150 a gdbserver breakpoint; single-stepping to/over one
1151 is not supported (yet). */
1152 if (event_child->bp_reinsert != 0)
1153 {
1154 if (debug_threads)
1155 fprintf (stderr, "Reinserted breakpoint.\n");
1156 reinsert_breakpoint (event_child->bp_reinsert);
1157 event_child->bp_reinsert = 0;
1158
1159 /* Clear the single-stepping flag and SIGTRAP as we resume. */
1160 linux_resume_one_lwp (event_child, 0, 0, NULL);
1161 continue;
1162 }
1163
1164 bp_status = check_breakpoints (stop_pc);
1165
1166 if (bp_status != 0)
1167 {
1168 if (debug_threads)
1169 fprintf (stderr, "Hit a gdbserver breakpoint.\n");
1170
1171 /* We hit one of our own breakpoints. We mark it as a pending
1172 breakpoint, so that check_removed_breakpoint () will do the PC
1173 adjustment for us at the appropriate time. */
1174 event_child->pending_is_breakpoint = 1;
1175 event_child->pending_stop_pc = stop_pc;
1176
1177 /* We may need to put the breakpoint back. We continue in the event
1178 loop instead of simply replacing the breakpoint right away,
1179 in order to not lose signals sent to the thread that hit the
1180 breakpoint. Unfortunately this increases the window where another
1181 thread could sneak past the removed breakpoint. For the current
1182 use of server-side breakpoints (thread creation) this is
1183 acceptable; but it needs to be considered before this breakpoint
1184 mechanism can be used in more general ways. For some breakpoints
1185 it may be necessary to stop all other threads, but that should
1186 be avoided where possible.
1187
1188 If breakpoint_reinsert_addr is NULL, that means that we can
1189 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
1190 mark it for reinsertion, and single-step.
1191
1192 Otherwise, call the target function to figure out where we need
1193 our temporary breakpoint, create it, and continue executing this
1194 process. */
1195
1196 /* NOTE: we're lifting breakpoints in non-stop mode. This
1197 is currently only used for thread event breakpoints, so
1198 it isn't that bad as long as we have PTRACE_EVENT_CLONE
1199 events. */
1200 if (bp_status == 2)
1201 /* No need to reinsert. */
1202 linux_resume_one_lwp (event_child, 0, 0, NULL);
1203 else if (the_low_target.breakpoint_reinsert_addr == NULL)
1204 {
1205 event_child->bp_reinsert = stop_pc;
1206 uninsert_breakpoint (stop_pc);
1207 linux_resume_one_lwp (event_child, 1, 0, NULL);
1208 }
1209 else
1210 {
1211 reinsert_breakpoint_by_bp
1212 (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
1213 linux_resume_one_lwp (event_child, 0, 0, NULL);
1214 }
1215
1216 continue;
1217 }
1218
1219 if (debug_threads)
1220 fprintf (stderr, "Hit a non-gdbserver breakpoint.\n");
1221
1222 /* If we were single-stepping, we definitely want to report the
1223 SIGTRAP. Although the single-step operation has completed,
1224 do not clear clear the stepping flag yet; we need to check it
1225 in wait_for_sigstop. */
1226 if (event_child->stepping)
1227 return lwpid_of (event_child);
1228
1229 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
1230 Check if it is a breakpoint, and if so mark the process information
1231 accordingly. This will handle both the necessary fiddling with the
1232 PC on decr_pc_after_break targets and suppressing extra threads
1233 hitting a breakpoint if two hit it at once and then GDB removes it
1234 after the first is reported. Arguably it would be better to report
1235 multiple threads hitting breakpoints simultaneously, but the current
1236 remote protocol does not allow this. */
1237 if ((*the_low_target.breakpoint_at) (stop_pc))
1238 {
1239 event_child->pending_is_breakpoint = 1;
1240 event_child->pending_stop_pc = stop_pc;
1241 }
1242
1243 return lwpid_of (event_child);
1244 }
1245
1246 /* NOTREACHED */
1247 return 0;
1248 }
1249
1250 static int
1251 linux_wait_for_event (ptid_t ptid, int *wstat, int options)
1252 {
1253 ptid_t wait_ptid;
1254
1255 if (ptid_is_pid (ptid))
1256 {
1257 /* A request to wait for a specific tgid. This is not possible
1258 with waitpid, so instead, we wait for any child, and leave
1259 children we're not interested in right now with a pending
1260 status to report later. */
1261 wait_ptid = minus_one_ptid;
1262 }
1263 else
1264 wait_ptid = ptid;
1265
1266 while (1)
1267 {
1268 int event_pid;
1269
1270 event_pid = linux_wait_for_event_1 (wait_ptid, wstat, options);
1271
1272 if (event_pid > 0
1273 && ptid_is_pid (ptid) && ptid_get_pid (ptid) != event_pid)
1274 {
1275 struct lwp_info *event_child = find_lwp_pid (pid_to_ptid (event_pid));
1276
1277 if (! WIFSTOPPED (*wstat))
1278 mark_lwp_dead (event_child, *wstat);
1279 else
1280 {
1281 event_child->status_pending_p = 1;
1282 event_child->status_pending = *wstat;
1283 }
1284 }
1285 else
1286 return event_pid;
1287 }
1288 }
1289
1290 /* Wait for process, returns status. */
1291
1292 static ptid_t
1293 linux_wait_1 (ptid_t ptid,
1294 struct target_waitstatus *ourstatus, int target_options)
1295 {
1296 int w;
1297 struct thread_info *thread = NULL;
1298 struct lwp_info *lwp = NULL;
1299 int options;
1300 int pid;
1301
1302 /* Translate generic target options into linux options. */
1303 options = __WALL;
1304 if (target_options & TARGET_WNOHANG)
1305 options |= WNOHANG;
1306
1307 retry:
1308 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1309
1310 /* If we were only supposed to resume one thread, only wait for
1311 that thread - if it's still alive. If it died, however - which
1312 can happen if we're coming from the thread death case below -
1313 then we need to make sure we restart the other threads. We could
1314 pick a thread at random or restart all; restarting all is less
1315 arbitrary. */
1316 if (!non_stop
1317 && !ptid_equal (cont_thread, null_ptid)
1318 && !ptid_equal (cont_thread, minus_one_ptid))
1319 {
1320 thread = (struct thread_info *) find_inferior_id (&all_threads,
1321 cont_thread);
1322
1323 /* No stepping, no signal - unless one is pending already, of course. */
1324 if (thread == NULL)
1325 {
1326 struct thread_resume resume_info;
1327 resume_info.thread = minus_one_ptid;
1328 resume_info.kind = resume_continue;
1329 resume_info.sig = 0;
1330 linux_resume (&resume_info, 1);
1331 }
1332 else
1333 ptid = cont_thread;
1334 }
1335
1336 pid = linux_wait_for_event (ptid, &w, options);
1337 if (pid == 0) /* only if TARGET_WNOHANG */
1338 return null_ptid;
1339
1340 lwp = get_thread_lwp (current_inferior);
1341
1342 /* If we are waiting for a particular child, and it exited,
1343 linux_wait_for_event will return its exit status. Similarly if
1344 the last child exited. If this is not the last child, however,
1345 do not report it as exited until there is a 'thread exited' response
1346 available in the remote protocol. Instead, just wait for another event.
1347 This should be safe, because if the thread crashed we will already
1348 have reported the termination signal to GDB; that should stop any
1349 in-progress stepping operations, etc.
1350
1351 Report the exit status of the last thread to exit. This matches
1352 LinuxThreads' behavior. */
1353
1354 if (last_thread_of_process_p (current_inferior))
1355 {
1356 if (WIFEXITED (w) || WIFSIGNALED (w))
1357 {
1358 int pid = pid_of (lwp);
1359 struct process_info *process = find_process_pid (pid);
1360
1361 delete_lwp (lwp);
1362 linux_remove_process (process);
1363
1364 current_inferior = NULL;
1365
1366 if (WIFEXITED (w))
1367 {
1368 ourstatus->kind = TARGET_WAITKIND_EXITED;
1369 ourstatus->value.integer = WEXITSTATUS (w);
1370
1371 if (debug_threads)
1372 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
1373 }
1374 else
1375 {
1376 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1377 ourstatus->value.sig = target_signal_from_host (WTERMSIG (w));
1378
1379 if (debug_threads)
1380 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
1381
1382 }
1383
1384 return pid_to_ptid (pid);
1385 }
1386 }
1387 else
1388 {
1389 if (!WIFSTOPPED (w))
1390 goto retry;
1391 }
1392
1393 /* In all-stop, stop all threads. Be careful to only do this if
1394 we're about to report an event to GDB. */
1395 if (!non_stop)
1396 stop_all_lwps ();
1397
1398 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1399
1400 if (lwp->suspended && WSTOPSIG (w) == SIGSTOP)
1401 {
1402 /* A thread that has been requested to stop by GDB with vCont;t,
1403 and it stopped cleanly, so report as SIG0. The use of
1404 SIGSTOP is an implementation detail. */
1405 ourstatus->value.sig = TARGET_SIGNAL_0;
1406 }
1407 else if (lwp->suspended && WSTOPSIG (w) != SIGSTOP)
1408 {
1409 /* A thread that has been requested to stop by GDB with vCont;t,
1410 but, it stopped for other reasons. Set stop_expected so the
1411 pending SIGSTOP is ignored and the LWP is resumed. */
1412 lwp->stop_expected = 1;
1413 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w));
1414 }
1415 else
1416 {
1417 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w));
1418 }
1419
1420 if (debug_threads)
1421 fprintf (stderr, "linux_wait ret = %s, %d, %d\n",
1422 target_pid_to_str (lwp->head.id),
1423 ourstatus->kind,
1424 ourstatus->value.sig);
1425
1426 return lwp->head.id;
1427 }
1428
1429 /* Get rid of any pending event in the pipe. */
1430 static void
1431 async_file_flush (void)
1432 {
1433 int ret;
1434 char buf;
1435
1436 do
1437 ret = read (linux_event_pipe[0], &buf, 1);
1438 while (ret >= 0 || (ret == -1 && errno == EINTR));
1439 }
1440
1441 /* Put something in the pipe, so the event loop wakes up. */
1442 static void
1443 async_file_mark (void)
1444 {
1445 int ret;
1446
1447 async_file_flush ();
1448
1449 do
1450 ret = write (linux_event_pipe[1], "+", 1);
1451 while (ret == 0 || (ret == -1 && errno == EINTR));
1452
1453 /* Ignore EAGAIN. If the pipe is full, the event loop will already
1454 be awakened anyway. */
1455 }
1456
1457 static ptid_t
1458 linux_wait (ptid_t ptid,
1459 struct target_waitstatus *ourstatus, int target_options)
1460 {
1461 ptid_t event_ptid;
1462
1463 if (debug_threads)
1464 fprintf (stderr, "linux_wait: [%s]\n", target_pid_to_str (ptid));
1465
1466 /* Flush the async file first. */
1467 if (target_is_async_p ())
1468 async_file_flush ();
1469
1470 event_ptid = linux_wait_1 (ptid, ourstatus, target_options);
1471
1472 /* If at least one stop was reported, there may be more. A single
1473 SIGCHLD can signal more than one child stop. */
1474 if (target_is_async_p ()
1475 && (target_options & TARGET_WNOHANG) != 0
1476 && !ptid_equal (event_ptid, null_ptid))
1477 async_file_mark ();
1478
1479 return event_ptid;
1480 }
1481
1482 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
1483 thread groups are in use, we need to use tkill. */
1484
1485 static int
1486 kill_lwp (unsigned long lwpid, int signo)
1487 {
1488 static int tkill_failed;
1489
1490 errno = 0;
1491
1492 #ifdef SYS_tkill
1493 if (!tkill_failed)
1494 {
1495 int ret = syscall (SYS_tkill, lwpid, signo);
1496 if (errno != ENOSYS)
1497 return ret;
1498 errno = 0;
1499 tkill_failed = 1;
1500 }
1501 #endif
1502
1503 return kill (lwpid, signo);
1504 }
1505
1506 static void
1507 send_sigstop (struct inferior_list_entry *entry)
1508 {
1509 struct lwp_info *lwp = (struct lwp_info *) entry;
1510 int pid;
1511
1512 if (lwp->stopped)
1513 return;
1514
1515 pid = lwpid_of (lwp);
1516
1517 /* If we already have a pending stop signal for this process, don't
1518 send another. */
1519 if (lwp->stop_expected)
1520 {
1521 if (debug_threads)
1522 fprintf (stderr, "Have pending sigstop for lwp %d\n", pid);
1523
1524 /* We clear the stop_expected flag so that wait_for_sigstop
1525 will receive the SIGSTOP event (instead of silently resuming and
1526 waiting again). It'll be reset below. */
1527 lwp->stop_expected = 0;
1528 return;
1529 }
1530
1531 if (debug_threads)
1532 fprintf (stderr, "Sending sigstop to lwp %d\n", pid);
1533
1534 kill_lwp (pid, SIGSTOP);
1535 }
1536
1537 static void
1538 mark_lwp_dead (struct lwp_info *lwp, int wstat)
1539 {
1540 /* It's dead, really. */
1541 lwp->dead = 1;
1542
1543 /* Store the exit status for later. */
1544 lwp->status_pending_p = 1;
1545 lwp->status_pending = wstat;
1546
1547 /* So that check_removed_breakpoint doesn't try to figure out if
1548 this is stopped at a breakpoint. */
1549 lwp->pending_is_breakpoint = 0;
1550
1551 /* Prevent trying to stop it. */
1552 lwp->stopped = 1;
1553
1554 /* No further stops are expected from a dead lwp. */
1555 lwp->stop_expected = 0;
1556 }
1557
1558 static void
1559 wait_for_sigstop (struct inferior_list_entry *entry)
1560 {
1561 struct lwp_info *lwp = (struct lwp_info *) entry;
1562 struct thread_info *saved_inferior;
1563 int wstat;
1564 ptid_t saved_tid;
1565 ptid_t ptid;
1566
1567 if (lwp->stopped)
1568 return;
1569
1570 saved_inferior = current_inferior;
1571 if (saved_inferior != NULL)
1572 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
1573 else
1574 saved_tid = null_ptid; /* avoid bogus unused warning */
1575
1576 ptid = lwp->head.id;
1577
1578 linux_wait_for_event (ptid, &wstat, __WALL);
1579
1580 /* If we stopped with a non-SIGSTOP signal, save it for later
1581 and record the pending SIGSTOP. If the process exited, just
1582 return. */
1583 if (WIFSTOPPED (wstat)
1584 && WSTOPSIG (wstat) != SIGSTOP)
1585 {
1586 if (debug_threads)
1587 fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n",
1588 lwpid_of (lwp), wstat);
1589
1590 /* Do not leave a pending single-step finish to be reported to
1591 the client. The client will give us a new action for this
1592 thread, possibly a continue request --- otherwise, the client
1593 would consider this pending SIGTRAP reported later a spurious
1594 signal. */
1595 if (WSTOPSIG (wstat) == SIGTRAP
1596 && lwp->stepping
1597 && !linux_stopped_by_watchpoint ())
1598 {
1599 if (debug_threads)
1600 fprintf (stderr, " single-step SIGTRAP ignored\n");
1601 }
1602 else
1603 {
1604 lwp->status_pending_p = 1;
1605 lwp->status_pending = wstat;
1606 }
1607 lwp->stop_expected = 1;
1608 }
1609 else if (!WIFSTOPPED (wstat))
1610 {
1611 if (debug_threads)
1612 fprintf (stderr, "Process %ld exited while stopping LWPs\n",
1613 lwpid_of (lwp));
1614
1615 /* Leave this status pending for the next time we're able to
1616 report it. In the mean time, we'll report this lwp as dead
1617 to GDB, so GDB doesn't try to read registers and memory from
1618 it. */
1619 mark_lwp_dead (lwp, wstat);
1620 }
1621
1622 if (saved_inferior == NULL || linux_thread_alive (saved_tid))
1623 current_inferior = saved_inferior;
1624 else
1625 {
1626 if (debug_threads)
1627 fprintf (stderr, "Previously current thread died.\n");
1628
1629 if (non_stop)
1630 {
1631 /* We can't change the current inferior behind GDB's back,
1632 otherwise, a subsequent command may apply to the wrong
1633 process. */
1634 current_inferior = NULL;
1635 }
1636 else
1637 {
1638 /* Set a valid thread as current. */
1639 set_desired_inferior (0);
1640 }
1641 }
1642 }
1643
1644 static void
1645 stop_all_lwps (void)
1646 {
1647 stopping_threads = 1;
1648 for_each_inferior (&all_lwps, send_sigstop);
1649 for_each_inferior (&all_lwps, wait_for_sigstop);
1650 stopping_threads = 0;
1651 }
1652
1653 /* Resume execution of the inferior process.
1654 If STEP is nonzero, single-step it.
1655 If SIGNAL is nonzero, give it that signal. */
1656
1657 static void
1658 linux_resume_one_lwp (struct lwp_info *lwp,
1659 int step, int signal, siginfo_t *info)
1660 {
1661 struct thread_info *saved_inferior;
1662
1663 if (lwp->stopped == 0)
1664 return;
1665
1666 /* If we have pending signals or status, and a new signal, enqueue the
1667 signal. Also enqueue the signal if we are waiting to reinsert a
1668 breakpoint; it will be picked up again below. */
1669 if (signal != 0
1670 && (lwp->status_pending_p || lwp->pending_signals != NULL
1671 || lwp->bp_reinsert != 0))
1672 {
1673 struct pending_signals *p_sig;
1674 p_sig = xmalloc (sizeof (*p_sig));
1675 p_sig->prev = lwp->pending_signals;
1676 p_sig->signal = signal;
1677 if (info == NULL)
1678 memset (&p_sig->info, 0, sizeof (siginfo_t));
1679 else
1680 memcpy (&p_sig->info, info, sizeof (siginfo_t));
1681 lwp->pending_signals = p_sig;
1682 }
1683
1684 if (lwp->status_pending_p && !check_removed_breakpoint (lwp))
1685 return;
1686
1687 saved_inferior = current_inferior;
1688 current_inferior = get_lwp_thread (lwp);
1689
1690 if (debug_threads)
1691 fprintf (stderr, "Resuming lwp %ld (%s, signal %d, stop %s)\n",
1692 lwpid_of (lwp), step ? "step" : "continue", signal,
1693 lwp->stop_expected ? "expected" : "not expected");
1694
1695 /* This bit needs some thinking about. If we get a signal that
1696 we must report while a single-step reinsert is still pending,
1697 we often end up resuming the thread. It might be better to
1698 (ew) allow a stack of pending events; then we could be sure that
1699 the reinsert happened right away and not lose any signals.
1700
1701 Making this stack would also shrink the window in which breakpoints are
1702 uninserted (see comment in linux_wait_for_lwp) but not enough for
1703 complete correctness, so it won't solve that problem. It may be
1704 worthwhile just to solve this one, however. */
1705 if (lwp->bp_reinsert != 0)
1706 {
1707 if (debug_threads)
1708 fprintf (stderr, " pending reinsert at %08lx", (long)lwp->bp_reinsert);
1709 if (step == 0)
1710 fprintf (stderr, "BAD - reinserting but not stepping.\n");
1711 step = 1;
1712
1713 /* Postpone any pending signal. It was enqueued above. */
1714 signal = 0;
1715 }
1716
1717 check_removed_breakpoint (lwp);
1718
1719 if (debug_threads && the_low_target.get_pc != NULL)
1720 {
1721 CORE_ADDR pc = (*the_low_target.get_pc) ();
1722 fprintf (stderr, " resuming from pc 0x%lx\n", (long) pc);
1723 }
1724
1725 /* If we have pending signals, consume one unless we are trying to reinsert
1726 a breakpoint. */
1727 if (lwp->pending_signals != NULL && lwp->bp_reinsert == 0)
1728 {
1729 struct pending_signals **p_sig;
1730
1731 p_sig = &lwp->pending_signals;
1732 while ((*p_sig)->prev != NULL)
1733 p_sig = &(*p_sig)->prev;
1734
1735 signal = (*p_sig)->signal;
1736 if ((*p_sig)->info.si_signo != 0)
1737 ptrace (PTRACE_SETSIGINFO, lwpid_of (lwp), 0, &(*p_sig)->info);
1738
1739 free (*p_sig);
1740 *p_sig = NULL;
1741 }
1742
1743 regcache_invalidate_one ((struct inferior_list_entry *)
1744 get_lwp_thread (lwp));
1745 errno = 0;
1746 lwp->stopped = 0;
1747 lwp->stepping = step;
1748 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, lwpid_of (lwp), 0, signal);
1749
1750 current_inferior = saved_inferior;
1751 if (errno)
1752 {
1753 /* ESRCH from ptrace either means that the thread was already
1754 running (an error) or that it is gone (a race condition). If
1755 it's gone, we will get a notification the next time we wait,
1756 so we can ignore the error. We could differentiate these
1757 two, but it's tricky without waiting; the thread still exists
1758 as a zombie, so sending it signal 0 would succeed. So just
1759 ignore ESRCH. */
1760 if (errno == ESRCH)
1761 return;
1762
1763 perror_with_name ("ptrace");
1764 }
1765 }
1766
1767 struct thread_resume_array
1768 {
1769 struct thread_resume *resume;
1770 size_t n;
1771 };
1772
1773 /* This function is called once per thread. We look up the thread
1774 in RESUME_PTR, and mark the thread with a pointer to the appropriate
1775 resume request.
1776
1777 This algorithm is O(threads * resume elements), but resume elements
1778 is small (and will remain small at least until GDB supports thread
1779 suspension). */
1780 static int
1781 linux_set_resume_request (struct inferior_list_entry *entry, void *arg)
1782 {
1783 struct lwp_info *lwp;
1784 struct thread_info *thread;
1785 int ndx;
1786 struct thread_resume_array *r;
1787
1788 thread = (struct thread_info *) entry;
1789 lwp = get_thread_lwp (thread);
1790 r = arg;
1791
1792 for (ndx = 0; ndx < r->n; ndx++)
1793 {
1794 ptid_t ptid = r->resume[ndx].thread;
1795 if (ptid_equal (ptid, minus_one_ptid)
1796 || ptid_equal (ptid, entry->id)
1797 || (ptid_is_pid (ptid)
1798 && (ptid_get_pid (ptid) == pid_of (lwp)))
1799 || (ptid_get_lwp (ptid) == -1
1800 && (ptid_get_pid (ptid) == pid_of (lwp))))
1801 {
1802 lwp->resume = &r->resume[ndx];
1803 return 0;
1804 }
1805 }
1806
1807 /* No resume action for this thread. */
1808 lwp->resume = NULL;
1809
1810 return 0;
1811 }
1812
1813
1814 /* Set *FLAG_P if this lwp has an interesting status pending. */
1815 static int
1816 resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
1817 {
1818 struct lwp_info *lwp = (struct lwp_info *) entry;
1819
1820 /* LWPs which will not be resumed are not interesting, because
1821 we might not wait for them next time through linux_wait. */
1822 if (lwp->resume == NULL)
1823 return 0;
1824
1825 /* If this thread has a removed breakpoint, we won't have any
1826 events to report later, so check now. check_removed_breakpoint
1827 may clear status_pending_p. We avoid calling check_removed_breakpoint
1828 for any thread that we are not otherwise going to resume - this
1829 lets us preserve stopped status when two threads hit a breakpoint.
1830 GDB removes the breakpoint to single-step a particular thread
1831 past it, then re-inserts it and resumes all threads. We want
1832 to report the second thread without resuming it in the interim. */
1833 if (lwp->status_pending_p)
1834 check_removed_breakpoint (lwp);
1835
1836 if (lwp->status_pending_p)
1837 * (int *) flag_p = 1;
1838
1839 return 0;
1840 }
1841
1842 /* This function is called once per thread. We check the thread's resume
1843 request, which will tell us whether to resume, step, or leave the thread
1844 stopped; and what signal, if any, it should be sent.
1845
1846 For threads which we aren't explicitly told otherwise, we preserve
1847 the stepping flag; this is used for stepping over gdbserver-placed
1848 breakpoints.
1849
1850 If pending_flags was set in any thread, we queue any needed
1851 signals, since we won't actually resume. We already have a pending
1852 event to report, so we don't need to preserve any step requests;
1853 they should be re-issued if necessary. */
1854
1855 static int
1856 linux_resume_one_thread (struct inferior_list_entry *entry, void *arg)
1857 {
1858 struct lwp_info *lwp;
1859 struct thread_info *thread;
1860 int step;
1861 int pending_flag = * (int *) arg;
1862
1863 thread = (struct thread_info *) entry;
1864 lwp = get_thread_lwp (thread);
1865
1866 if (lwp->resume == NULL)
1867 return 0;
1868
1869 if (lwp->resume->kind == resume_stop)
1870 {
1871 if (debug_threads)
1872 fprintf (stderr, "suspending LWP %ld\n", lwpid_of (lwp));
1873
1874 if (!lwp->stopped)
1875 {
1876 if (debug_threads)
1877 fprintf (stderr, "running -> suspending LWP %ld\n", lwpid_of (lwp));
1878
1879 lwp->suspended = 1;
1880 send_sigstop (&lwp->head);
1881 }
1882 else
1883 {
1884 if (debug_threads)
1885 {
1886 if (lwp->suspended)
1887 fprintf (stderr, "already stopped/suspended LWP %ld\n",
1888 lwpid_of (lwp));
1889 else
1890 fprintf (stderr, "already stopped/not suspended LWP %ld\n",
1891 lwpid_of (lwp));
1892 }
1893
1894 /* Make sure we leave the LWP suspended, so we don't try to
1895 resume it without GDB telling us to. FIXME: The LWP may
1896 have been stopped in an internal event that was not meant
1897 to be notified back to GDB (e.g., gdbserver breakpoint),
1898 so we should be reporting a stop event in that case
1899 too. */
1900 lwp->suspended = 1;
1901 }
1902
1903 /* For stop requests, we're done. */
1904 lwp->resume = NULL;
1905 return 0;
1906 }
1907 else
1908 lwp->suspended = 0;
1909
1910 /* If this thread which is about to be resumed has a pending status,
1911 then don't resume any threads - we can just report the pending
1912 status. Make sure to queue any signals that would otherwise be
1913 sent. In all-stop mode, we do this decision based on if *any*
1914 thread has a pending status. */
1915 if (non_stop)
1916 resume_status_pending_p (&lwp->head, &pending_flag);
1917
1918 if (!pending_flag)
1919 {
1920 if (debug_threads)
1921 fprintf (stderr, "resuming LWP %ld\n", lwpid_of (lwp));
1922
1923 if (ptid_equal (lwp->resume->thread, minus_one_ptid)
1924 && lwp->stepping
1925 && lwp->pending_is_breakpoint)
1926 step = 1;
1927 else
1928 step = (lwp->resume->kind == resume_step);
1929
1930 linux_resume_one_lwp (lwp, step, lwp->resume->sig, NULL);
1931 }
1932 else
1933 {
1934 if (debug_threads)
1935 fprintf (stderr, "leaving LWP %ld stopped\n", lwpid_of (lwp));
1936
1937 /* If we have a new signal, enqueue the signal. */
1938 if (lwp->resume->sig != 0)
1939 {
1940 struct pending_signals *p_sig;
1941 p_sig = xmalloc (sizeof (*p_sig));
1942 p_sig->prev = lwp->pending_signals;
1943 p_sig->signal = lwp->resume->sig;
1944 memset (&p_sig->info, 0, sizeof (siginfo_t));
1945
1946 /* If this is the same signal we were previously stopped by,
1947 make sure to queue its siginfo. We can ignore the return
1948 value of ptrace; if it fails, we'll skip
1949 PTRACE_SETSIGINFO. */
1950 if (WIFSTOPPED (lwp->last_status)
1951 && WSTOPSIG (lwp->last_status) == lwp->resume->sig)
1952 ptrace (PTRACE_GETSIGINFO, lwpid_of (lwp), 0, &p_sig->info);
1953
1954 lwp->pending_signals = p_sig;
1955 }
1956 }
1957
1958 lwp->resume = NULL;
1959 return 0;
1960 }
1961
1962 static void
1963 linux_resume (struct thread_resume *resume_info, size_t n)
1964 {
1965 int pending_flag;
1966 struct thread_resume_array array = { resume_info, n };
1967
1968 find_inferior (&all_threads, linux_set_resume_request, &array);
1969
1970 /* If there is a thread which would otherwise be resumed, which
1971 has a pending status, then don't resume any threads - we can just
1972 report the pending status. Make sure to queue any signals
1973 that would otherwise be sent. In non-stop mode, we'll apply this
1974 logic to each thread individually. */
1975 pending_flag = 0;
1976 if (!non_stop)
1977 find_inferior (&all_lwps, resume_status_pending_p, &pending_flag);
1978
1979 if (debug_threads)
1980 {
1981 if (pending_flag)
1982 fprintf (stderr, "Not resuming, pending status\n");
1983 else
1984 fprintf (stderr, "Resuming, no pending status\n");
1985 }
1986
1987 find_inferior (&all_threads, linux_resume_one_thread, &pending_flag);
1988 }
1989
1990 #ifdef HAVE_LINUX_USRREGS
1991
1992 int
1993 register_addr (int regnum)
1994 {
1995 int addr;
1996
1997 if (regnum < 0 || regnum >= the_low_target.num_regs)
1998 error ("Invalid register number %d.", regnum);
1999
2000 addr = the_low_target.regmap[regnum];
2001
2002 return addr;
2003 }
2004
2005 /* Fetch one register. */
2006 static void
2007 fetch_register (int regno)
2008 {
2009 CORE_ADDR regaddr;
2010 int i, size;
2011 char *buf;
2012 int pid;
2013
2014 if (regno >= the_low_target.num_regs)
2015 return;
2016 if ((*the_low_target.cannot_fetch_register) (regno))
2017 return;
2018
2019 regaddr = register_addr (regno);
2020 if (regaddr == -1)
2021 return;
2022
2023 pid = lwpid_of (get_thread_lwp (current_inferior));
2024 size = ((register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
2025 & - sizeof (PTRACE_XFER_TYPE));
2026 buf = alloca (size);
2027 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
2028 {
2029 errno = 0;
2030 *(PTRACE_XFER_TYPE *) (buf + i) =
2031 ptrace (PTRACE_PEEKUSER, pid, (PTRACE_ARG3_TYPE) regaddr, 0);
2032 regaddr += sizeof (PTRACE_XFER_TYPE);
2033 if (errno != 0)
2034 {
2035 /* Warning, not error, in case we are attached; sometimes the
2036 kernel doesn't let us at the registers. */
2037 char *err = strerror (errno);
2038 char *msg = alloca (strlen (err) + 128);
2039 sprintf (msg, "reading register %d: %s", regno, err);
2040 error (msg);
2041 goto error_exit;
2042 }
2043 }
2044
2045 if (the_low_target.supply_ptrace_register)
2046 the_low_target.supply_ptrace_register (regno, buf);
2047 else
2048 supply_register (regno, buf);
2049
2050 error_exit:;
2051 }
2052
2053 /* Fetch all registers, or just one, from the child process. */
2054 static void
2055 usr_fetch_inferior_registers (int regno)
2056 {
2057 if (regno == -1 || regno == 0)
2058 for (regno = 0; regno < the_low_target.num_regs; regno++)
2059 fetch_register (regno);
2060 else
2061 fetch_register (regno);
2062 }
2063
2064 /* Store our register values back into the inferior.
2065 If REGNO is -1, do this for all registers.
2066 Otherwise, REGNO specifies which register (so we can save time). */
2067 static void
2068 usr_store_inferior_registers (int regno)
2069 {
2070 CORE_ADDR regaddr;
2071 int i, size;
2072 char *buf;
2073 int pid;
2074
2075 if (regno >= 0)
2076 {
2077 if (regno >= the_low_target.num_regs)
2078 return;
2079
2080 if ((*the_low_target.cannot_store_register) (regno) == 1)
2081 return;
2082
2083 regaddr = register_addr (regno);
2084 if (regaddr == -1)
2085 return;
2086 errno = 0;
2087 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
2088 & - sizeof (PTRACE_XFER_TYPE);
2089 buf = alloca (size);
2090 memset (buf, 0, size);
2091
2092 if (the_low_target.collect_ptrace_register)
2093 the_low_target.collect_ptrace_register (regno, buf);
2094 else
2095 collect_register (regno, buf);
2096
2097 pid = lwpid_of (get_thread_lwp (current_inferior));
2098 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
2099 {
2100 errno = 0;
2101 ptrace (PTRACE_POKEUSER, pid, (PTRACE_ARG3_TYPE) regaddr,
2102 *(PTRACE_XFER_TYPE *) (buf + i));
2103 if (errno != 0)
2104 {
2105 /* At this point, ESRCH should mean the process is
2106 already gone, in which case we simply ignore attempts
2107 to change its registers. See also the related
2108 comment in linux_resume_one_lwp. */
2109 if (errno == ESRCH)
2110 return;
2111
2112 if ((*the_low_target.cannot_store_register) (regno) == 0)
2113 {
2114 char *err = strerror (errno);
2115 char *msg = alloca (strlen (err) + 128);
2116 sprintf (msg, "writing register %d: %s",
2117 regno, err);
2118 error (msg);
2119 return;
2120 }
2121 }
2122 regaddr += sizeof (PTRACE_XFER_TYPE);
2123 }
2124 }
2125 else
2126 for (regno = 0; regno < the_low_target.num_regs; regno++)
2127 usr_store_inferior_registers (regno);
2128 }
2129 #endif /* HAVE_LINUX_USRREGS */
2130
2131
2132
2133 #ifdef HAVE_LINUX_REGSETS
2134
2135 static int
2136 regsets_fetch_inferior_registers ()
2137 {
2138 struct regset_info *regset;
2139 int saw_general_regs = 0;
2140 int pid;
2141
2142 regset = target_regsets;
2143
2144 pid = lwpid_of (get_thread_lwp (current_inferior));
2145 while (regset->size >= 0)
2146 {
2147 void *buf;
2148 int res;
2149
2150 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
2151 {
2152 regset ++;
2153 continue;
2154 }
2155
2156 buf = xmalloc (regset->size);
2157 #ifndef __sparc__
2158 res = ptrace (regset->get_request, pid, 0, buf);
2159 #else
2160 res = ptrace (regset->get_request, pid, buf, 0);
2161 #endif
2162 if (res < 0)
2163 {
2164 if (errno == EIO)
2165 {
2166 /* If we get EIO on a regset, do not try it again for
2167 this process. */
2168 disabled_regsets[regset - target_regsets] = 1;
2169 free (buf);
2170 continue;
2171 }
2172 else
2173 {
2174 char s[256];
2175 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
2176 pid);
2177 perror (s);
2178 }
2179 }
2180 else if (regset->type == GENERAL_REGS)
2181 saw_general_regs = 1;
2182 regset->store_function (buf);
2183 regset ++;
2184 free (buf);
2185 }
2186 if (saw_general_regs)
2187 return 0;
2188 else
2189 return 1;
2190 }
2191
2192 static int
2193 regsets_store_inferior_registers ()
2194 {
2195 struct regset_info *regset;
2196 int saw_general_regs = 0;
2197 int pid;
2198
2199 regset = target_regsets;
2200
2201 pid = lwpid_of (get_thread_lwp (current_inferior));
2202 while (regset->size >= 0)
2203 {
2204 void *buf;
2205 int res;
2206
2207 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
2208 {
2209 regset ++;
2210 continue;
2211 }
2212
2213 buf = xmalloc (regset->size);
2214
2215 /* First fill the buffer with the current register set contents,
2216 in case there are any items in the kernel's regset that are
2217 not in gdbserver's regcache. */
2218 #ifndef __sparc__
2219 res = ptrace (regset->get_request, pid, 0, buf);
2220 #else
2221 res = ptrace (regset->get_request, pid, buf, 0);
2222 #endif
2223
2224 if (res == 0)
2225 {
2226 /* Then overlay our cached registers on that. */
2227 regset->fill_function (buf);
2228
2229 /* Only now do we write the register set. */
2230 #ifndef __sparc__
2231 res = ptrace (regset->set_request, pid, 0, buf);
2232 #else
2233 res = ptrace (regset->set_request, pid, buf, 0);
2234 #endif
2235 }
2236
2237 if (res < 0)
2238 {
2239 if (errno == EIO)
2240 {
2241 /* If we get EIO on a regset, do not try it again for
2242 this process. */
2243 disabled_regsets[regset - target_regsets] = 1;
2244 free (buf);
2245 continue;
2246 }
2247 else if (errno == ESRCH)
2248 {
2249 /* At this point, ESRCH should mean the process is
2250 already gone, in which case we simply ignore attempts
2251 to change its registers. See also the related
2252 comment in linux_resume_one_lwp. */
2253 free (buf);
2254 return 0;
2255 }
2256 else
2257 {
2258 perror ("Warning: ptrace(regsets_store_inferior_registers)");
2259 }
2260 }
2261 else if (regset->type == GENERAL_REGS)
2262 saw_general_regs = 1;
2263 regset ++;
2264 free (buf);
2265 }
2266 if (saw_general_regs)
2267 return 0;
2268 else
2269 return 1;
2270 return 0;
2271 }
2272
2273 #endif /* HAVE_LINUX_REGSETS */
2274
2275
2276 void
2277 linux_fetch_registers (int regno)
2278 {
2279 #ifdef HAVE_LINUX_REGSETS
2280 if (regsets_fetch_inferior_registers () == 0)
2281 return;
2282 #endif
2283 #ifdef HAVE_LINUX_USRREGS
2284 usr_fetch_inferior_registers (regno);
2285 #endif
2286 }
2287
2288 void
2289 linux_store_registers (int regno)
2290 {
2291 #ifdef HAVE_LINUX_REGSETS
2292 if (regsets_store_inferior_registers () == 0)
2293 return;
2294 #endif
2295 #ifdef HAVE_LINUX_USRREGS
2296 usr_store_inferior_registers (regno);
2297 #endif
2298 }
2299
2300
2301 /* Copy LEN bytes from inferior's memory starting at MEMADDR
2302 to debugger memory starting at MYADDR. */
2303
2304 static int
2305 linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
2306 {
2307 register int i;
2308 /* Round starting address down to longword boundary. */
2309 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
2310 /* Round ending address up; get number of longwords that makes. */
2311 register int count
2312 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
2313 / sizeof (PTRACE_XFER_TYPE);
2314 /* Allocate buffer of that many longwords. */
2315 register PTRACE_XFER_TYPE *buffer
2316 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
2317 int fd;
2318 char filename[64];
2319 int pid = lwpid_of (get_thread_lwp (current_inferior));
2320
2321 /* Try using /proc. Don't bother for one word. */
2322 if (len >= 3 * sizeof (long))
2323 {
2324 /* We could keep this file open and cache it - possibly one per
2325 thread. That requires some juggling, but is even faster. */
2326 sprintf (filename, "/proc/%d/mem", pid);
2327 fd = open (filename, O_RDONLY | O_LARGEFILE);
2328 if (fd == -1)
2329 goto no_proc;
2330
2331 /* If pread64 is available, use it. It's faster if the kernel
2332 supports it (only one syscall), and it's 64-bit safe even on
2333 32-bit platforms (for instance, SPARC debugging a SPARC64
2334 application). */
2335 #ifdef HAVE_PREAD64
2336 if (pread64 (fd, myaddr, len, memaddr) != len)
2337 #else
2338 if (lseek (fd, memaddr, SEEK_SET) == -1 || read (fd, memaddr, len) != len)
2339 #endif
2340 {
2341 close (fd);
2342 goto no_proc;
2343 }
2344
2345 close (fd);
2346 return 0;
2347 }
2348
2349 no_proc:
2350 /* Read all the longwords */
2351 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
2352 {
2353 errno = 0;
2354 buffer[i] = ptrace (PTRACE_PEEKTEXT, pid, (PTRACE_ARG3_TYPE) addr, 0);
2355 if (errno)
2356 return errno;
2357 }
2358
2359 /* Copy appropriate bytes out of the buffer. */
2360 memcpy (myaddr,
2361 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
2362 len);
2363
2364 return 0;
2365 }
2366
2367 /* Copy LEN bytes of data from debugger memory at MYADDR
2368 to inferior's memory at MEMADDR.
2369 On failure (cannot write the inferior)
2370 returns the value of errno. */
2371
2372 static int
2373 linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
2374 {
2375 register int i;
2376 /* Round starting address down to longword boundary. */
2377 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
2378 /* Round ending address up; get number of longwords that makes. */
2379 register int count
2380 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
2381 /* Allocate buffer of that many longwords. */
2382 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
2383 int pid = lwpid_of (get_thread_lwp (current_inferior));
2384
2385 if (debug_threads)
2386 {
2387 fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
2388 }
2389
2390 /* Fill start and end extra bytes of buffer with existing memory data. */
2391
2392 buffer[0] = ptrace (PTRACE_PEEKTEXT, pid, (PTRACE_ARG3_TYPE) addr, 0);
2393
2394 if (count > 1)
2395 {
2396 buffer[count - 1]
2397 = ptrace (PTRACE_PEEKTEXT, pid,
2398 (PTRACE_ARG3_TYPE) (addr + (count - 1)
2399 * sizeof (PTRACE_XFER_TYPE)),
2400 0);
2401 }
2402
2403 /* Copy data to be written over corresponding part of buffer */
2404
2405 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
2406
2407 /* Write the entire buffer. */
2408
2409 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
2410 {
2411 errno = 0;
2412 ptrace (PTRACE_POKETEXT, pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
2413 if (errno)
2414 return errno;
2415 }
2416
2417 return 0;
2418 }
2419
2420 static int linux_supports_tracefork_flag;
2421
2422 /* Helper functions for linux_test_for_tracefork, called via clone (). */
2423
2424 static int
2425 linux_tracefork_grandchild (void *arg)
2426 {
2427 _exit (0);
2428 }
2429
2430 #define STACK_SIZE 4096
2431
2432 static int
2433 linux_tracefork_child (void *arg)
2434 {
2435 ptrace (PTRACE_TRACEME, 0, 0, 0);
2436 kill (getpid (), SIGSTOP);
2437 #ifdef __ia64__
2438 __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE,
2439 CLONE_VM | SIGCHLD, NULL);
2440 #else
2441 clone (linux_tracefork_grandchild, arg + STACK_SIZE,
2442 CLONE_VM | SIGCHLD, NULL);
2443 #endif
2444 _exit (0);
2445 }
2446
2447 /* Wrapper function for waitpid which handles EINTR, and emulates
2448 __WALL for systems where that is not available. */
2449
2450 static int
2451 my_waitpid (int pid, int *status, int flags)
2452 {
2453 int ret, out_errno;
2454
2455 if (debug_threads)
2456 fprintf (stderr, "my_waitpid (%d, 0x%x)\n", pid, flags);
2457
2458 if (flags & __WALL)
2459 {
2460 sigset_t block_mask, org_mask, wake_mask;
2461 int wnohang;
2462
2463 wnohang = (flags & WNOHANG) != 0;
2464 flags &= ~(__WALL | __WCLONE);
2465 flags |= WNOHANG;
2466
2467 /* Block all signals while here. This avoids knowing about
2468 LinuxThread's signals. */
2469 sigfillset (&block_mask);
2470 sigprocmask (SIG_BLOCK, &block_mask, &org_mask);
2471
2472 /* ... except during the sigsuspend below. */
2473 sigemptyset (&wake_mask);
2474
2475 while (1)
2476 {
2477 /* Since all signals are blocked, there's no need to check
2478 for EINTR here. */
2479 ret = waitpid (pid, status, flags);
2480 out_errno = errno;
2481
2482 if (ret == -1 && out_errno != ECHILD)
2483 break;
2484 else if (ret > 0)
2485 break;
2486
2487 if (flags & __WCLONE)
2488 {
2489 /* We've tried both flavors now. If WNOHANG is set,
2490 there's nothing else to do, just bail out. */
2491 if (wnohang)
2492 break;
2493
2494 if (debug_threads)
2495 fprintf (stderr, "blocking\n");
2496
2497 /* Block waiting for signals. */
2498 sigsuspend (&wake_mask);
2499 }
2500
2501 flags ^= __WCLONE;
2502 }
2503
2504 sigprocmask (SIG_SETMASK, &org_mask, NULL);
2505 }
2506 else
2507 {
2508 do
2509 ret = waitpid (pid, status, flags);
2510 while (ret == -1 && errno == EINTR);
2511 out_errno = errno;
2512 }
2513
2514 if (debug_threads)
2515 fprintf (stderr, "my_waitpid (%d, 0x%x): status(%x), %d\n",
2516 pid, flags, status ? *status : -1, ret);
2517
2518 errno = out_errno;
2519 return ret;
2520 }
2521
2522 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
2523 sure that we can enable the option, and that it had the desired
2524 effect. */
2525
2526 static void
2527 linux_test_for_tracefork (void)
2528 {
2529 int child_pid, ret, status;
2530 long second_pid;
2531 char *stack = xmalloc (STACK_SIZE * 4);
2532
2533 linux_supports_tracefork_flag = 0;
2534
2535 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
2536 #ifdef __ia64__
2537 child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE,
2538 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
2539 #else
2540 child_pid = clone (linux_tracefork_child, stack + STACK_SIZE,
2541 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
2542 #endif
2543 if (child_pid == -1)
2544 perror_with_name ("clone");
2545
2546 ret = my_waitpid (child_pid, &status, 0);
2547 if (ret == -1)
2548 perror_with_name ("waitpid");
2549 else if (ret != child_pid)
2550 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
2551 if (! WIFSTOPPED (status))
2552 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
2553
2554 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
2555 if (ret != 0)
2556 {
2557 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
2558 if (ret != 0)
2559 {
2560 warning ("linux_test_for_tracefork: failed to kill child");
2561 return;
2562 }
2563
2564 ret = my_waitpid (child_pid, &status, 0);
2565 if (ret != child_pid)
2566 warning ("linux_test_for_tracefork: failed to wait for killed child");
2567 else if (!WIFSIGNALED (status))
2568 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
2569 "killed child", status);
2570
2571 return;
2572 }
2573
2574 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
2575 if (ret != 0)
2576 warning ("linux_test_for_tracefork: failed to resume child");
2577
2578 ret = my_waitpid (child_pid, &status, 0);
2579
2580 if (ret == child_pid && WIFSTOPPED (status)
2581 && status >> 16 == PTRACE_EVENT_FORK)
2582 {
2583 second_pid = 0;
2584 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
2585 if (ret == 0 && second_pid != 0)
2586 {
2587 int second_status;
2588
2589 linux_supports_tracefork_flag = 1;
2590 my_waitpid (second_pid, &second_status, 0);
2591 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
2592 if (ret != 0)
2593 warning ("linux_test_for_tracefork: failed to kill second child");
2594 my_waitpid (second_pid, &status, 0);
2595 }
2596 }
2597 else
2598 warning ("linux_test_for_tracefork: unexpected result from waitpid "
2599 "(%d, status 0x%x)", ret, status);
2600
2601 do
2602 {
2603 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
2604 if (ret != 0)
2605 warning ("linux_test_for_tracefork: failed to kill child");
2606 my_waitpid (child_pid, &status, 0);
2607 }
2608 while (WIFSTOPPED (status));
2609
2610 free (stack);
2611 }
2612
2613
2614 static void
2615 linux_look_up_symbols (void)
2616 {
2617 #ifdef USE_THREAD_DB
2618 struct process_info *proc = current_process ();
2619
2620 if (proc->private->thread_db_active)
2621 return;
2622
2623 proc->private->thread_db_active
2624 = thread_db_init (!linux_supports_tracefork_flag);
2625 #endif
2626 }
2627
2628 static void
2629 linux_request_interrupt (void)
2630 {
2631 extern unsigned long signal_pid;
2632
2633 if (!ptid_equal (cont_thread, null_ptid)
2634 && !ptid_equal (cont_thread, minus_one_ptid))
2635 {
2636 struct lwp_info *lwp;
2637 int lwpid;
2638
2639 lwp = get_thread_lwp (current_inferior);
2640 lwpid = lwpid_of (lwp);
2641 kill_lwp (lwpid, SIGINT);
2642 }
2643 else
2644 kill_lwp (signal_pid, SIGINT);
2645 }
2646
2647 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
2648 to debugger memory starting at MYADDR. */
2649
2650 static int
2651 linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
2652 {
2653 char filename[PATH_MAX];
2654 int fd, n;
2655 int pid = lwpid_of (get_thread_lwp (current_inferior));
2656
2657 snprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
2658
2659 fd = open (filename, O_RDONLY);
2660 if (fd < 0)
2661 return -1;
2662
2663 if (offset != (CORE_ADDR) 0
2664 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
2665 n = -1;
2666 else
2667 n = read (fd, myaddr, len);
2668
2669 close (fd);
2670
2671 return n;
2672 }
2673
2674 /* These watchpoint related wrapper functions simply pass on the function call
2675 if the target has registered a corresponding function. */
2676
2677 static int
2678 linux_insert_watchpoint (char type, CORE_ADDR addr, int len)
2679 {
2680 if (the_low_target.insert_watchpoint != NULL)
2681 return the_low_target.insert_watchpoint (type, addr, len);
2682 else
2683 /* Unsupported (see target.h). */
2684 return 1;
2685 }
2686
2687 static int
2688 linux_remove_watchpoint (char type, CORE_ADDR addr, int len)
2689 {
2690 if (the_low_target.remove_watchpoint != NULL)
2691 return the_low_target.remove_watchpoint (type, addr, len);
2692 else
2693 /* Unsupported (see target.h). */
2694 return 1;
2695 }
2696
2697 static int
2698 linux_stopped_by_watchpoint (void)
2699 {
2700 if (the_low_target.stopped_by_watchpoint != NULL)
2701 return the_low_target.stopped_by_watchpoint ();
2702 else
2703 return 0;
2704 }
2705
2706 static CORE_ADDR
2707 linux_stopped_data_address (void)
2708 {
2709 if (the_low_target.stopped_data_address != NULL)
2710 return the_low_target.stopped_data_address ();
2711 else
2712 return 0;
2713 }
2714
2715 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
2716 #if defined(__mcoldfire__)
2717 /* These should really be defined in the kernel's ptrace.h header. */
2718 #define PT_TEXT_ADDR 49*4
2719 #define PT_DATA_ADDR 50*4
2720 #define PT_TEXT_END_ADDR 51*4
2721 #endif
2722
2723 /* Under uClinux, programs are loaded at non-zero offsets, which we need
2724 to tell gdb about. */
2725
2726 static int
2727 linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
2728 {
2729 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
2730 unsigned long text, text_end, data;
2731 int pid = lwpid_of (get_thread_lwp (current_inferior));
2732
2733 errno = 0;
2734
2735 text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0);
2736 text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0);
2737 data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0);
2738
2739 if (errno == 0)
2740 {
2741 /* Both text and data offsets produced at compile-time (and so
2742 used by gdb) are relative to the beginning of the program,
2743 with the data segment immediately following the text segment.
2744 However, the actual runtime layout in memory may put the data
2745 somewhere else, so when we send gdb a data base-address, we
2746 use the real data base address and subtract the compile-time
2747 data base-address from it (which is just the length of the
2748 text segment). BSS immediately follows data in both
2749 cases. */
2750 *text_p = text;
2751 *data_p = data - (text_end - text);
2752
2753 return 1;
2754 }
2755 #endif
2756 return 0;
2757 }
2758 #endif
2759
2760 static int
2761 linux_qxfer_osdata (const char *annex,
2762 unsigned char *readbuf, unsigned const char *writebuf,
2763 CORE_ADDR offset, int len)
2764 {
2765 /* We make the process list snapshot when the object starts to be
2766 read. */
2767 static const char *buf;
2768 static long len_avail = -1;
2769 static struct buffer buffer;
2770
2771 DIR *dirp;
2772
2773 if (strcmp (annex, "processes") != 0)
2774 return 0;
2775
2776 if (!readbuf || writebuf)
2777 return 0;
2778
2779 if (offset == 0)
2780 {
2781 if (len_avail != -1 && len_avail != 0)
2782 buffer_free (&buffer);
2783 len_avail = 0;
2784 buf = NULL;
2785 buffer_init (&buffer);
2786 buffer_grow_str (&buffer, "<osdata type=\"processes\">");
2787
2788 dirp = opendir ("/proc");
2789 if (dirp)
2790 {
2791 struct dirent *dp;
2792 while ((dp = readdir (dirp)) != NULL)
2793 {
2794 struct stat statbuf;
2795 char procentry[sizeof ("/proc/4294967295")];
2796
2797 if (!isdigit (dp->d_name[0])
2798 || strlen (dp->d_name) > sizeof ("4294967295") - 1)
2799 continue;
2800
2801 sprintf (procentry, "/proc/%s", dp->d_name);
2802 if (stat (procentry, &statbuf) == 0
2803 && S_ISDIR (statbuf.st_mode))
2804 {
2805 char pathname[128];
2806 FILE *f;
2807 char cmd[MAXPATHLEN + 1];
2808 struct passwd *entry;
2809
2810 sprintf (pathname, "/proc/%s/cmdline", dp->d_name);
2811 entry = getpwuid (statbuf.st_uid);
2812
2813 if ((f = fopen (pathname, "r")) != NULL)
2814 {
2815 size_t len = fread (cmd, 1, sizeof (cmd) - 1, f);
2816 if (len > 0)
2817 {
2818 int i;
2819 for (i = 0; i < len; i++)
2820 if (cmd[i] == '\0')
2821 cmd[i] = ' ';
2822 cmd[len] = '\0';
2823
2824 buffer_xml_printf (
2825 &buffer,
2826 "<item>"
2827 "<column name=\"pid\">%s</column>"
2828 "<column name=\"user\">%s</column>"
2829 "<column name=\"command\">%s</column>"
2830 "</item>",
2831 dp->d_name,
2832 entry ? entry->pw_name : "?",
2833 cmd);
2834 }
2835 fclose (f);
2836 }
2837 }
2838 }
2839
2840 closedir (dirp);
2841 }
2842 buffer_grow_str0 (&buffer, "</osdata>\n");
2843 buf = buffer_finish (&buffer);
2844 len_avail = strlen (buf);
2845 }
2846
2847 if (offset >= len_avail)
2848 {
2849 /* Done. Get rid of the data. */
2850 buffer_free (&buffer);
2851 buf = NULL;
2852 len_avail = 0;
2853 return 0;
2854 }
2855
2856 if (len > len_avail - offset)
2857 len = len_avail - offset;
2858 memcpy (readbuf, buf + offset, len);
2859
2860 return len;
2861 }
2862
2863 /* Convert a native/host siginfo object, into/from the siginfo in the
2864 layout of the inferiors' architecture. */
2865
2866 static void
2867 siginfo_fixup (struct siginfo *siginfo, void *inf_siginfo, int direction)
2868 {
2869 int done = 0;
2870
2871 if (the_low_target.siginfo_fixup != NULL)
2872 done = the_low_target.siginfo_fixup (siginfo, inf_siginfo, direction);
2873
2874 /* If there was no callback, or the callback didn't do anything,
2875 then just do a straight memcpy. */
2876 if (!done)
2877 {
2878 if (direction == 1)
2879 memcpy (siginfo, inf_siginfo, sizeof (struct siginfo));
2880 else
2881 memcpy (inf_siginfo, siginfo, sizeof (struct siginfo));
2882 }
2883 }
2884
2885 static int
2886 linux_xfer_siginfo (const char *annex, unsigned char *readbuf,
2887 unsigned const char *writebuf, CORE_ADDR offset, int len)
2888 {
2889 int pid;
2890 struct siginfo siginfo;
2891 char inf_siginfo[sizeof (struct siginfo)];
2892
2893 if (current_inferior == NULL)
2894 return -1;
2895
2896 pid = lwpid_of (get_thread_lwp (current_inferior));
2897
2898 if (debug_threads)
2899 fprintf (stderr, "%s siginfo for lwp %d.\n",
2900 readbuf != NULL ? "Reading" : "Writing",
2901 pid);
2902
2903 if (offset > sizeof (siginfo))
2904 return -1;
2905
2906 if (ptrace (PTRACE_GETSIGINFO, pid, 0, &siginfo) != 0)
2907 return -1;
2908
2909 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
2910 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
2911 inferior with a 64-bit GDBSERVER should look the same as debugging it
2912 with a 32-bit GDBSERVER, we need to convert it. */
2913 siginfo_fixup (&siginfo, inf_siginfo, 0);
2914
2915 if (offset + len > sizeof (siginfo))
2916 len = sizeof (siginfo) - offset;
2917
2918 if (readbuf != NULL)
2919 memcpy (readbuf, inf_siginfo + offset, len);
2920 else
2921 {
2922 memcpy (inf_siginfo + offset, writebuf, len);
2923
2924 /* Convert back to ptrace layout before flushing it out. */
2925 siginfo_fixup (&siginfo, inf_siginfo, 1);
2926
2927 if (ptrace (PTRACE_SETSIGINFO, pid, 0, &siginfo) != 0)
2928 return -1;
2929 }
2930
2931 return len;
2932 }
2933
2934 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
2935 so we notice when children change state; as the handler for the
2936 sigsuspend in my_waitpid. */
2937
2938 static void
2939 sigchld_handler (int signo)
2940 {
2941 int old_errno = errno;
2942
2943 if (debug_threads)
2944 /* fprintf is not async-signal-safe, so call write directly. */
2945 write (2, "sigchld_handler\n", sizeof ("sigchld_handler\n") - 1);
2946
2947 if (target_is_async_p ())
2948 async_file_mark (); /* trigger a linux_wait */
2949
2950 errno = old_errno;
2951 }
2952
2953 static int
2954 linux_supports_non_stop (void)
2955 {
2956 return 1;
2957 }
2958
2959 static int
2960 linux_async (int enable)
2961 {
2962 int previous = (linux_event_pipe[0] != -1);
2963
2964 if (previous != enable)
2965 {
2966 sigset_t mask;
2967 sigemptyset (&mask);
2968 sigaddset (&mask, SIGCHLD);
2969
2970 sigprocmask (SIG_BLOCK, &mask, NULL);
2971
2972 if (enable)
2973 {
2974 if (pipe (linux_event_pipe) == -1)
2975 fatal ("creating event pipe failed.");
2976
2977 fcntl (linux_event_pipe[0], F_SETFL, O_NONBLOCK);
2978 fcntl (linux_event_pipe[1], F_SETFL, O_NONBLOCK);
2979
2980 /* Register the event loop handler. */
2981 add_file_handler (linux_event_pipe[0],
2982 handle_target_event, NULL);
2983
2984 /* Always trigger a linux_wait. */
2985 async_file_mark ();
2986 }
2987 else
2988 {
2989 delete_file_handler (linux_event_pipe[0]);
2990
2991 close (linux_event_pipe[0]);
2992 close (linux_event_pipe[1]);
2993 linux_event_pipe[0] = -1;
2994 linux_event_pipe[1] = -1;
2995 }
2996
2997 sigprocmask (SIG_UNBLOCK, &mask, NULL);
2998 }
2999
3000 return previous;
3001 }
3002
3003 static int
3004 linux_start_non_stop (int nonstop)
3005 {
3006 /* Register or unregister from event-loop accordingly. */
3007 linux_async (nonstop);
3008 return 0;
3009 }
3010
3011 static struct target_ops linux_target_ops = {
3012 linux_create_inferior,
3013 linux_attach,
3014 linux_kill,
3015 linux_detach,
3016 linux_join,
3017 linux_thread_alive,
3018 linux_resume,
3019 linux_wait,
3020 linux_fetch_registers,
3021 linux_store_registers,
3022 linux_read_memory,
3023 linux_write_memory,
3024 linux_look_up_symbols,
3025 linux_request_interrupt,
3026 linux_read_auxv,
3027 linux_insert_watchpoint,
3028 linux_remove_watchpoint,
3029 linux_stopped_by_watchpoint,
3030 linux_stopped_data_address,
3031 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
3032 linux_read_offsets,
3033 #else
3034 NULL,
3035 #endif
3036 #ifdef USE_THREAD_DB
3037 thread_db_get_tls_address,
3038 #else
3039 NULL,
3040 #endif
3041 NULL,
3042 hostio_last_error_from_errno,
3043 linux_qxfer_osdata,
3044 linux_xfer_siginfo,
3045 linux_supports_non_stop,
3046 linux_async,
3047 linux_start_non_stop,
3048 };
3049
3050 static void
3051 linux_init_signals ()
3052 {
3053 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
3054 to find what the cancel signal actually is. */
3055 signal (__SIGRTMIN+1, SIG_IGN);
3056 }
3057
3058 void
3059 initialize_low (void)
3060 {
3061 struct sigaction sigchld_action;
3062 memset (&sigchld_action, 0, sizeof (sigchld_action));
3063 set_target_ops (&linux_target_ops);
3064 set_breakpoint_data (the_low_target.breakpoint,
3065 the_low_target.breakpoint_len);
3066 linux_init_signals ();
3067 linux_test_for_tracefork ();
3068 #ifdef HAVE_LINUX_REGSETS
3069 for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++)
3070 ;
3071 disabled_regsets = xmalloc (num_regsets);
3072 #endif
3073
3074 sigchld_action.sa_handler = sigchld_handler;
3075 sigemptyset (&sigchld_action.sa_mask);
3076 sigchld_action.sa_flags = SA_RESTART;
3077 sigaction (SIGCHLD, &sigchld_action, NULL);
3078 }
This page took 0.135489 seconds and 4 git commands to generate.