Teach linux gdbserver to step-over-breakpoints.
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-low.c
CommitLineData
da6d8c04 1/* Low level interface to ptrace, for the remote server for GDB.
545587ee 2 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4c38e0a4 3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
da6d8c04
DJ
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
da6d8c04
DJ
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
da6d8c04
DJ
19
20#include "server.h"
58caa3dc 21#include "linux-low.h"
da6d8c04 22
58caa3dc 23#include <sys/wait.h>
da6d8c04
DJ
24#include <stdio.h>
25#include <sys/param.h>
da6d8c04 26#include <sys/ptrace.h>
da6d8c04
DJ
27#include <signal.h>
28#include <sys/ioctl.h>
29#include <fcntl.h>
d07c63e7 30#include <string.h>
0a30fbc4
DJ
31#include <stdlib.h>
32#include <unistd.h>
fa6a77dc 33#include <errno.h>
fd500816 34#include <sys/syscall.h>
f9387fc3 35#include <sched.h>
07e059b5
VP
36#include <ctype.h>
37#include <pwd.h>
38#include <sys/types.h>
39#include <dirent.h>
efcbbd14
UW
40#include <sys/stat.h>
41#include <sys/vfs.h>
957f3f49
DE
42#ifndef ELFMAG0
43/* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
44 then ELFMAG0 will have been defined. If it didn't get included by
45 gdb_proc_service.h then including it will likely introduce a duplicate
46 definition of elf_fpregset_t. */
47#include <elf.h>
48#endif
efcbbd14
UW
49
50#ifndef SPUFS_MAGIC
51#define SPUFS_MAGIC 0x23c9b64e
52#endif
da6d8c04 53
32ca6d61
DJ
54#ifndef PTRACE_GETSIGINFO
55# define PTRACE_GETSIGINFO 0x4202
56# define PTRACE_SETSIGINFO 0x4203
57#endif
58
fd462a61
DJ
59#ifndef O_LARGEFILE
60#define O_LARGEFILE 0
61#endif
62
24a09b5f
DJ
63/* If the system headers did not provide the constants, hard-code the normal
64 values. */
65#ifndef PTRACE_EVENT_FORK
66
67#define PTRACE_SETOPTIONS 0x4200
68#define PTRACE_GETEVENTMSG 0x4201
69
70/* options set using PTRACE_SETOPTIONS */
71#define PTRACE_O_TRACESYSGOOD 0x00000001
72#define PTRACE_O_TRACEFORK 0x00000002
73#define PTRACE_O_TRACEVFORK 0x00000004
74#define PTRACE_O_TRACECLONE 0x00000008
75#define PTRACE_O_TRACEEXEC 0x00000010
76#define PTRACE_O_TRACEVFORKDONE 0x00000020
77#define PTRACE_O_TRACEEXIT 0x00000040
78
79/* Wait extended result codes for the above trace options. */
80#define PTRACE_EVENT_FORK 1
81#define PTRACE_EVENT_VFORK 2
82#define PTRACE_EVENT_CLONE 3
83#define PTRACE_EVENT_EXEC 4
84#define PTRACE_EVENT_VFORK_DONE 5
85#define PTRACE_EVENT_EXIT 6
86
87#endif /* PTRACE_EVENT_FORK */
88
89/* We can't always assume that this flag is available, but all systems
90 with the ptrace event handlers also have __WALL, so it's safe to use
91 in some contexts. */
92#ifndef __WALL
93#define __WALL 0x40000000 /* Wait for any child. */
94#endif
95
ec8ebe72
DE
96#ifndef W_STOPCODE
97#define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
98#endif
99
42c81e2a
DJ
100#ifdef __UCLIBC__
101#if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
102#define HAS_NOMMU
103#endif
104#endif
105
24a09b5f
DJ
106/* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
107 representation of the thread ID.
611cb4a5 108
54a0b537 109 ``all_lwps'' is keyed by the process ID - which on Linux is (presently)
95954743
PA
110 the same as the LWP ID.
111
112 ``all_processes'' is keyed by the "overall process ID", which
113 GNU/Linux calls tgid, "thread group ID". */
0d62e5e8 114
54a0b537 115struct inferior_list all_lwps;
0d62e5e8 116
24a09b5f
DJ
117/* A list of all unknown processes which receive stop signals. Some other
118 process will presumably claim each of these as forked children
119 momentarily. */
120
121struct inferior_list stopped_pids;
122
0d62e5e8
DJ
123/* FIXME this is a bit of a hack, and could be removed. */
124int stopping_threads;
125
126/* FIXME make into a target method? */
24a09b5f 127int using_threads = 1;
24a09b5f 128
95954743
PA
129/* This flag is true iff we've just created or attached to our first
130 inferior but it has not stopped yet. As soon as it does, we need
131 to call the low target's arch_setup callback. Doing this only on
132 the first inferior avoids reinializing the architecture on every
133 inferior, and avoids messing with the register caches of the
134 already running inferiors. NOTE: this assumes all inferiors under
135 control of gdbserver have the same architecture. */
d61ddec4
UW
136static int new_inferior;
137
2acc282a 138static void linux_resume_one_lwp (struct lwp_info *lwp,
54a0b537 139 int step, int signal, siginfo_t *info);
2bd7c093 140static void linux_resume (struct thread_resume *resume_info, size_t n);
54a0b537 141static void stop_all_lwps (void);
95954743 142static int linux_wait_for_event (ptid_t ptid, int *wstat, int options);
95954743 143static void *add_lwp (ptid_t ptid);
c35fafde 144static int linux_stopped_by_watchpoint (void);
95954743 145static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
dc146f7c 146static int linux_core_of_thread (ptid_t ptid);
d50171e4
PA
147static void proceed_all_lwps (void);
148static void unstop_all_lwps (struct lwp_info *except);
149static void cancel_finished_single_steps (struct lwp_info *except);
150static int finish_step_over (struct lwp_info *lwp);
151static CORE_ADDR get_stop_pc (struct lwp_info *lwp);
152static int kill_lwp (unsigned long lwpid, int signo);
153
154/* True if the low target can hardware single-step. Such targets
155 don't need a BREAKPOINT_REINSERT_ADDR callback. */
156
157static int
158can_hardware_single_step (void)
159{
160 return (the_low_target.breakpoint_reinsert_addr == NULL);
161}
162
163/* True if the low target supports memory breakpoints. If so, we'll
164 have a GET_PC implementation. */
165
166static int
167supports_breakpoints (void)
168{
169 return (the_low_target.get_pc != NULL);
170}
0d62e5e8
DJ
171
172struct pending_signals
173{
174 int signal;
32ca6d61 175 siginfo_t info;
0d62e5e8
DJ
176 struct pending_signals *prev;
177};
611cb4a5 178
14ce3065
DE
179#define PTRACE_ARG3_TYPE void *
180#define PTRACE_ARG4_TYPE void *
c6ecbae5 181#define PTRACE_XFER_TYPE long
da6d8c04 182
58caa3dc 183#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
184static char *disabled_regsets;
185static int num_regsets;
58caa3dc
DJ
186#endif
187
bd99dc85
PA
188/* The read/write ends of the pipe registered as waitable file in the
189 event loop. */
190static int linux_event_pipe[2] = { -1, -1 };
191
192/* True if we're currently in async mode. */
193#define target_is_async_p() (linux_event_pipe[0] != -1)
194
195static void send_sigstop (struct inferior_list_entry *entry);
196static void wait_for_sigstop (struct inferior_list_entry *entry);
197
d0722149
DE
198/* Accepts an integer PID; Returns a string representing a file that
199 can be opened to get info for the child process.
200 Space for the result is malloc'd, caller must free. */
201
202char *
203linux_child_pid_to_exec_file (int pid)
204{
205 char *name1, *name2;
206
207 name1 = xmalloc (MAXPATHLEN);
208 name2 = xmalloc (MAXPATHLEN);
209 memset (name2, 0, MAXPATHLEN);
210
211 sprintf (name1, "/proc/%d/exe", pid);
212 if (readlink (name1, name2, MAXPATHLEN) > 0)
213 {
214 free (name1);
215 return name2;
216 }
217 else
218 {
219 free (name2);
220 return name1;
221 }
222}
223
224/* Return non-zero if HEADER is a 64-bit ELF file. */
225
226static int
957f3f49 227elf_64_header_p (const Elf64_Ehdr *header)
d0722149
DE
228{
229 return (header->e_ident[EI_MAG0] == ELFMAG0
230 && header->e_ident[EI_MAG1] == ELFMAG1
231 && header->e_ident[EI_MAG2] == ELFMAG2
232 && header->e_ident[EI_MAG3] == ELFMAG3
233 && header->e_ident[EI_CLASS] == ELFCLASS64);
234}
235
236/* Return non-zero if FILE is a 64-bit ELF file,
237 zero if the file is not a 64-bit ELF file,
238 and -1 if the file is not accessible or doesn't exist. */
239
240int
241elf_64_file_p (const char *file)
242{
957f3f49 243 Elf64_Ehdr header;
d0722149
DE
244 int fd;
245
246 fd = open (file, O_RDONLY);
247 if (fd < 0)
248 return -1;
249
250 if (read (fd, &header, sizeof (header)) != sizeof (header))
251 {
252 close (fd);
253 return 0;
254 }
255 close (fd);
256
257 return elf_64_header_p (&header);
258}
259
bd99dc85
PA
260static void
261delete_lwp (struct lwp_info *lwp)
262{
263 remove_thread (get_lwp_thread (lwp));
264 remove_inferior (&all_lwps, &lwp->head);
aa5ca48f 265 free (lwp->arch_private);
bd99dc85
PA
266 free (lwp);
267}
268
95954743
PA
269/* Add a process to the common process list, and set its private
270 data. */
271
272static struct process_info *
273linux_add_process (int pid, int attached)
274{
275 struct process_info *proc;
276
277 /* Is this the first process? If so, then set the arch. */
278 if (all_processes.head == NULL)
279 new_inferior = 1;
280
281 proc = add_process (pid, attached);
282 proc->private = xcalloc (1, sizeof (*proc->private));
283
aa5ca48f
DE
284 if (the_low_target.new_process != NULL)
285 proc->private->arch_private = the_low_target.new_process ();
286
95954743
PA
287 return proc;
288}
289
5091eb23
DE
290/* Remove a process from the common process list,
291 also freeing all private data. */
292
293static void
ca5c370d 294linux_remove_process (struct process_info *process)
5091eb23 295{
cdbfd419
PP
296 struct process_info_private *priv = process->private;
297
cdbfd419
PP
298 free (priv->arch_private);
299 free (priv);
5091eb23
DE
300 remove_process (process);
301}
302
07d4f67e
DE
303/* Wrapper function for waitpid which handles EINTR, and emulates
304 __WALL for systems where that is not available. */
305
306static int
307my_waitpid (int pid, int *status, int flags)
308{
309 int ret, out_errno;
310
311 if (debug_threads)
312 fprintf (stderr, "my_waitpid (%d, 0x%x)\n", pid, flags);
313
314 if (flags & __WALL)
315 {
316 sigset_t block_mask, org_mask, wake_mask;
317 int wnohang;
318
319 wnohang = (flags & WNOHANG) != 0;
320 flags &= ~(__WALL | __WCLONE);
321 flags |= WNOHANG;
322
323 /* Block all signals while here. This avoids knowing about
324 LinuxThread's signals. */
325 sigfillset (&block_mask);
326 sigprocmask (SIG_BLOCK, &block_mask, &org_mask);
327
328 /* ... except during the sigsuspend below. */
329 sigemptyset (&wake_mask);
330
331 while (1)
332 {
333 /* Since all signals are blocked, there's no need to check
334 for EINTR here. */
335 ret = waitpid (pid, status, flags);
336 out_errno = errno;
337
338 if (ret == -1 && out_errno != ECHILD)
339 break;
340 else if (ret > 0)
341 break;
342
343 if (flags & __WCLONE)
344 {
345 /* We've tried both flavors now. If WNOHANG is set,
346 there's nothing else to do, just bail out. */
347 if (wnohang)
348 break;
349
350 if (debug_threads)
351 fprintf (stderr, "blocking\n");
352
353 /* Block waiting for signals. */
354 sigsuspend (&wake_mask);
355 }
356
357 flags ^= __WCLONE;
358 }
359
360 sigprocmask (SIG_SETMASK, &org_mask, NULL);
361 }
362 else
363 {
364 do
365 ret = waitpid (pid, status, flags);
366 while (ret == -1 && errno == EINTR);
367 out_errno = errno;
368 }
369
370 if (debug_threads)
371 fprintf (stderr, "my_waitpid (%d, 0x%x): status(%x), %d\n",
372 pid, flags, status ? *status : -1, ret);
373
374 errno = out_errno;
375 return ret;
376}
377
bd99dc85
PA
378/* Handle a GNU/Linux extended wait response. If we see a clone
379 event, we need to add the new LWP to our list (and not report the
380 trap to higher layers). */
0d62e5e8 381
24a09b5f 382static void
54a0b537 383handle_extended_wait (struct lwp_info *event_child, int wstat)
24a09b5f
DJ
384{
385 int event = wstat >> 16;
54a0b537 386 struct lwp_info *new_lwp;
24a09b5f
DJ
387
388 if (event == PTRACE_EVENT_CLONE)
389 {
95954743 390 ptid_t ptid;
24a09b5f 391 unsigned long new_pid;
836acd6d 392 int ret, status = W_STOPCODE (SIGSTOP);
24a09b5f 393
bd99dc85 394 ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_child), 0, &new_pid);
24a09b5f
DJ
395
396 /* If we haven't already seen the new PID stop, wait for it now. */
397 if (! pull_pid_from_list (&stopped_pids, new_pid))
398 {
399 /* The new child has a pending SIGSTOP. We can't affect it until it
400 hits the SIGSTOP, but we're already attached. */
401
97438e3f 402 ret = my_waitpid (new_pid, &status, __WALL);
24a09b5f
DJ
403
404 if (ret == -1)
405 perror_with_name ("waiting for new child");
406 else if (ret != new_pid)
407 warning ("wait returned unexpected PID %d", ret);
da5898ce 408 else if (!WIFSTOPPED (status))
24a09b5f
DJ
409 warning ("wait returned unexpected status 0x%x", status);
410 }
411
14ce3065 412 ptrace (PTRACE_SETOPTIONS, new_pid, 0, (PTRACE_ARG4_TYPE) PTRACE_O_TRACECLONE);
24a09b5f 413
95954743
PA
414 ptid = ptid_build (pid_of (event_child), new_pid, 0);
415 new_lwp = (struct lwp_info *) add_lwp (ptid);
416 add_thread (ptid, new_lwp);
24a09b5f 417
e27d73f6
DE
418 /* Either we're going to immediately resume the new thread
419 or leave it stopped. linux_resume_one_lwp is a nop if it
420 thinks the thread is currently running, so set this first
421 before calling linux_resume_one_lwp. */
422 new_lwp->stopped = 1;
423
da5898ce
DJ
424 /* Normally we will get the pending SIGSTOP. But in some cases
425 we might get another signal delivered to the group first.
f21cc1a2 426 If we do get another signal, be sure not to lose it. */
da5898ce
DJ
427 if (WSTOPSIG (status) == SIGSTOP)
428 {
d50171e4
PA
429 if (stopping_threads)
430 new_lwp->stop_pc = get_stop_pc (new_lwp);
431 else
e27d73f6 432 linux_resume_one_lwp (new_lwp, 0, 0, NULL);
da5898ce 433 }
24a09b5f 434 else
da5898ce 435 {
54a0b537 436 new_lwp->stop_expected = 1;
d50171e4 437
da5898ce
DJ
438 if (stopping_threads)
439 {
d50171e4 440 new_lwp->stop_pc = get_stop_pc (new_lwp);
54a0b537
PA
441 new_lwp->status_pending_p = 1;
442 new_lwp->status_pending = status;
da5898ce
DJ
443 }
444 else
445 /* Pass the signal on. This is what GDB does - except
446 shouldn't we really report it instead? */
e27d73f6 447 linux_resume_one_lwp (new_lwp, 0, WSTOPSIG (status), NULL);
da5898ce 448 }
24a09b5f
DJ
449
450 /* Always resume the current thread. If we are stopping
451 threads, it will have a pending SIGSTOP; we may as well
452 collect it now. */
2acc282a 453 linux_resume_one_lwp (event_child, event_child->stepping, 0, NULL);
24a09b5f
DJ
454 }
455}
456
d50171e4
PA
457/* Return the PC as read from the regcache of LWP, without any
458 adjustment. */
459
460static CORE_ADDR
461get_pc (struct lwp_info *lwp)
462{
463 struct thread_info *saved_inferior;
464 struct regcache *regcache;
465 CORE_ADDR pc;
466
467 if (the_low_target.get_pc == NULL)
468 return 0;
469
470 saved_inferior = current_inferior;
471 current_inferior = get_lwp_thread (lwp);
472
473 regcache = get_thread_regcache (current_inferior, 1);
474 pc = (*the_low_target.get_pc) (regcache);
475
476 if (debug_threads)
477 fprintf (stderr, "pc is 0x%lx\n", (long) pc);
478
479 current_inferior = saved_inferior;
480 return pc;
481}
482
483/* This function should only be called if LWP got a SIGTRAP.
0d62e5e8
DJ
484 The SIGTRAP could mean several things.
485
486 On i386, where decr_pc_after_break is non-zero:
487 If we were single-stepping this process using PTRACE_SINGLESTEP,
488 we will get only the one SIGTRAP (even if the instruction we
489 stepped over was a breakpoint). The value of $eip will be the
490 next instruction.
491 If we continue the process using PTRACE_CONT, we will get a
492 SIGTRAP when we hit a breakpoint. The value of $eip will be
493 the instruction after the breakpoint (i.e. needs to be
494 decremented). If we report the SIGTRAP to GDB, we must also
495 report the undecremented PC. If we cancel the SIGTRAP, we
496 must resume at the decremented PC.
497
498 (Presumably, not yet tested) On a non-decr_pc_after_break machine
499 with hardware or kernel single-step:
500 If we single-step over a breakpoint instruction, our PC will
501 point at the following instruction. If we continue and hit a
502 breakpoint instruction, our PC will point at the breakpoint
503 instruction. */
504
505static CORE_ADDR
d50171e4 506get_stop_pc (struct lwp_info *lwp)
0d62e5e8 507{
d50171e4
PA
508 CORE_ADDR stop_pc;
509
510 if (the_low_target.get_pc == NULL)
511 return 0;
0d62e5e8 512
d50171e4
PA
513 stop_pc = get_pc (lwp);
514
515 if (WSTOPSIG (lwp->last_status) == SIGTRAP && !lwp->stepping)
47c0c975
DE
516 stop_pc -= the_low_target.decr_pc_after_break;
517
518 if (debug_threads)
519 fprintf (stderr, "stop pc is 0x%lx\n", (long) stop_pc);
520
521 return stop_pc;
0d62e5e8 522}
ce3a066d 523
0d62e5e8 524static void *
95954743 525add_lwp (ptid_t ptid)
611cb4a5 526{
54a0b537 527 struct lwp_info *lwp;
0d62e5e8 528
54a0b537
PA
529 lwp = (struct lwp_info *) xmalloc (sizeof (*lwp));
530 memset (lwp, 0, sizeof (*lwp));
0d62e5e8 531
95954743 532 lwp->head.id = ptid;
0d62e5e8 533
d50171e4
PA
534 lwp->last_resume_kind = resume_continue;
535
aa5ca48f
DE
536 if (the_low_target.new_thread != NULL)
537 lwp->arch_private = the_low_target.new_thread ();
538
54a0b537 539 add_inferior_to_list (&all_lwps, &lwp->head);
0d62e5e8 540
54a0b537 541 return lwp;
0d62e5e8 542}
611cb4a5 543
da6d8c04
DJ
544/* Start an inferior process and returns its pid.
545 ALLARGS is a vector of program-name and args. */
546
ce3a066d
DJ
547static int
548linux_create_inferior (char *program, char **allargs)
da6d8c04 549{
a6dbe5df 550 struct lwp_info *new_lwp;
da6d8c04 551 int pid;
95954743 552 ptid_t ptid;
da6d8c04 553
42c81e2a 554#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
555 pid = vfork ();
556#else
da6d8c04 557 pid = fork ();
52fb6437 558#endif
da6d8c04
DJ
559 if (pid < 0)
560 perror_with_name ("fork");
561
562 if (pid == 0)
563 {
564 ptrace (PTRACE_TRACEME, 0, 0, 0);
565
60c3d7b0 566#ifdef __SIGRTMIN /* Bionic doesn't use SIGRTMIN the way glibc does. */
254787d4 567 signal (__SIGRTMIN + 1, SIG_DFL);
60c3d7b0 568#endif
0d62e5e8 569
a9fa9f7d
DJ
570 setpgid (0, 0);
571
2b876972
DJ
572 execv (program, allargs);
573 if (errno == ENOENT)
574 execvp (program, allargs);
da6d8c04
DJ
575
576 fprintf (stderr, "Cannot exec %s: %s.\n", program,
d07c63e7 577 strerror (errno));
da6d8c04
DJ
578 fflush (stderr);
579 _exit (0177);
580 }
581
95954743
PA
582 linux_add_process (pid, 0);
583
584 ptid = ptid_build (pid, pid, 0);
585 new_lwp = add_lwp (ptid);
586 add_thread (ptid, new_lwp);
a6dbe5df 587 new_lwp->must_set_ptrace_flags = 1;
611cb4a5 588
a9fa9f7d 589 return pid;
da6d8c04
DJ
590}
591
592/* Attach to an inferior process. */
593
95954743
PA
594static void
595linux_attach_lwp_1 (unsigned long lwpid, int initial)
da6d8c04 596{
95954743 597 ptid_t ptid;
54a0b537 598 struct lwp_info *new_lwp;
611cb4a5 599
95954743 600 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) != 0)
da6d8c04 601 {
95954743 602 if (!initial)
2d717e4f
DJ
603 {
604 /* If we fail to attach to an LWP, just warn. */
95954743 605 fprintf (stderr, "Cannot attach to lwp %ld: %s (%d)\n", lwpid,
2d717e4f
DJ
606 strerror (errno), errno);
607 fflush (stderr);
608 return;
609 }
610 else
611 /* If we fail to attach to a process, report an error. */
95954743 612 error ("Cannot attach to lwp %ld: %s (%d)\n", lwpid,
43d5792c 613 strerror (errno), errno);
da6d8c04
DJ
614 }
615
95954743
PA
616 if (initial)
617 /* NOTE/FIXME: This lwp might have not been the tgid. */
618 ptid = ptid_build (lwpid, lwpid, 0);
619 else
620 {
621 /* Note that extracting the pid from the current inferior is
622 safe, since we're always called in the context of the same
623 process as this new thread. */
624 int pid = pid_of (get_thread_lwp (current_inferior));
625 ptid = ptid_build (pid, lwpid, 0);
626 }
24a09b5f 627
95954743
PA
628 new_lwp = (struct lwp_info *) add_lwp (ptid);
629 add_thread (ptid, new_lwp);
0d62e5e8 630
a6dbe5df
PA
631 /* We need to wait for SIGSTOP before being able to make the next
632 ptrace call on this LWP. */
633 new_lwp->must_set_ptrace_flags = 1;
634
0d62e5e8 635 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
0e21c1ec
DE
636 brings it to a halt.
637
638 There are several cases to consider here:
639
640 1) gdbserver has already attached to the process and is being notified
1b3f6016 641 of a new thread that is being created.
d50171e4
PA
642 In this case we should ignore that SIGSTOP and resume the
643 process. This is handled below by setting stop_expected = 1,
644 and the fact that add_lwp sets last_resume_kind ==
645 resume_continue.
0e21c1ec
DE
646
647 2) This is the first thread (the process thread), and we're attaching
1b3f6016
PA
648 to it via attach_inferior.
649 In this case we want the process thread to stop.
d50171e4
PA
650 This is handled by having linux_attach set last_resume_kind ==
651 resume_stop after we return.
1b3f6016
PA
652 ??? If the process already has several threads we leave the other
653 threads running.
0e21c1ec
DE
654
655 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1b3f6016
PA
656 existing threads.
657 In this case we want the thread to stop.
658 FIXME: This case is currently not properly handled.
659 We should wait for the SIGSTOP but don't. Things work apparently
660 because enough time passes between when we ptrace (ATTACH) and when
661 gdb makes the next ptrace call on the thread.
0d62e5e8
DJ
662
663 On the other hand, if we are currently trying to stop all threads, we
664 should treat the new thread as if we had sent it a SIGSTOP. This works
54a0b537 665 because we are guaranteed that the add_lwp call above added us to the
0e21c1ec
DE
666 end of the list, and so the new thread has not yet reached
667 wait_for_sigstop (but will). */
d50171e4 668 new_lwp->stop_expected = 1;
0d62e5e8
DJ
669}
670
95954743
PA
671void
672linux_attach_lwp (unsigned long lwpid)
673{
674 linux_attach_lwp_1 (lwpid, 0);
675}
676
0d62e5e8 677int
a1928bad 678linux_attach (unsigned long pid)
0d62e5e8 679{
54a0b537 680 struct lwp_info *lwp;
0d62e5e8 681
95954743
PA
682 linux_attach_lwp_1 (pid, 1);
683
684 linux_add_process (pid, 1);
0d62e5e8 685
bd99dc85
PA
686 if (!non_stop)
687 {
688 /* Don't ignore the initial SIGSTOP if we just attached to this
689 process. It will be collected by wait shortly. */
95954743
PA
690 lwp = (struct lwp_info *) find_inferior_id (&all_lwps,
691 ptid_build (pid, pid, 0));
d50171e4 692 lwp->last_resume_kind = resume_stop;
bd99dc85 693 }
0d62e5e8 694
95954743
PA
695 return 0;
696}
697
698struct counter
699{
700 int pid;
701 int count;
702};
703
704static int
705second_thread_of_pid_p (struct inferior_list_entry *entry, void *args)
706{
707 struct counter *counter = args;
708
709 if (ptid_get_pid (entry->id) == counter->pid)
710 {
711 if (++counter->count > 1)
712 return 1;
713 }
d61ddec4 714
da6d8c04
DJ
715 return 0;
716}
717
95954743
PA
718static int
719last_thread_of_process_p (struct thread_info *thread)
720{
721 ptid_t ptid = ((struct inferior_list_entry *)thread)->id;
722 int pid = ptid_get_pid (ptid);
723 struct counter counter = { pid , 0 };
da6d8c04 724
95954743
PA
725 return (find_inferior (&all_threads,
726 second_thread_of_pid_p, &counter) == NULL);
727}
728
729/* Kill the inferior lwp. */
730
731static int
732linux_kill_one_lwp (struct inferior_list_entry *entry, void *args)
da6d8c04 733{
0d62e5e8 734 struct thread_info *thread = (struct thread_info *) entry;
54a0b537 735 struct lwp_info *lwp = get_thread_lwp (thread);
0d62e5e8 736 int wstat;
95954743
PA
737 int pid = * (int *) args;
738
739 if (ptid_get_pid (entry->id) != pid)
740 return 0;
0d62e5e8 741
fd500816
DJ
742 /* We avoid killing the first thread here, because of a Linux kernel (at
743 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
744 the children get a chance to be reaped, it will remain a zombie
745 forever. */
95954743 746
12b42a12 747 if (lwpid_of (lwp) == pid)
95954743
PA
748 {
749 if (debug_threads)
750 fprintf (stderr, "lkop: is last of process %s\n",
751 target_pid_to_str (entry->id));
752 return 0;
753 }
fd500816 754
bd99dc85
PA
755 /* If we're killing a running inferior, make sure it is stopped
756 first, as PTRACE_KILL will not work otherwise. */
757 if (!lwp->stopped)
758 send_sigstop (&lwp->head);
759
0d62e5e8
DJ
760 do
761 {
bd99dc85 762 ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0);
0d62e5e8
DJ
763
764 /* Make sure it died. The loop is most likely unnecessary. */
95954743 765 pid = linux_wait_for_event (lwp->head.id, &wstat, __WALL);
bd99dc85 766 } while (pid > 0 && WIFSTOPPED (wstat));
95954743
PA
767
768 return 0;
da6d8c04
DJ
769}
770
95954743
PA
771static int
772linux_kill (int pid)
0d62e5e8 773{
95954743 774 struct process_info *process;
54a0b537 775 struct lwp_info *lwp;
95954743 776 struct thread_info *thread;
fd500816 777 int wstat;
95954743 778 int lwpid;
fd500816 779
95954743
PA
780 process = find_process_pid (pid);
781 if (process == NULL)
782 return -1;
9d606399 783
95954743 784 find_inferior (&all_threads, linux_kill_one_lwp, &pid);
fd500816 785
54a0b537 786 /* See the comment in linux_kill_one_lwp. We did not kill the first
fd500816 787 thread in the list, so do so now. */
95954743
PA
788 lwp = find_lwp_pid (pid_to_ptid (pid));
789 thread = get_lwp_thread (lwp);
bd99dc85
PA
790
791 if (debug_threads)
95954743
PA
792 fprintf (stderr, "lk_1: killing lwp %ld, for pid: %d\n",
793 lwpid_of (lwp), pid);
bd99dc85
PA
794
795 /* If we're killing a running inferior, make sure it is stopped
796 first, as PTRACE_KILL will not work otherwise. */
797 if (!lwp->stopped)
798 send_sigstop (&lwp->head);
799
fd500816
DJ
800 do
801 {
bd99dc85 802 ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0);
fd500816
DJ
803
804 /* Make sure it died. The loop is most likely unnecessary. */
95954743
PA
805 lwpid = linux_wait_for_event (lwp->head.id, &wstat, __WALL);
806 } while (lwpid > 0 && WIFSTOPPED (wstat));
2d717e4f 807
ca5c370d
PA
808#ifdef USE_THREAD_DB
809 thread_db_free (process, 0);
810#endif
bd99dc85 811 delete_lwp (lwp);
ca5c370d 812 linux_remove_process (process);
95954743 813 return 0;
0d62e5e8
DJ
814}
815
95954743
PA
816static int
817linux_detach_one_lwp (struct inferior_list_entry *entry, void *args)
6ad8ae5c
DJ
818{
819 struct thread_info *thread = (struct thread_info *) entry;
54a0b537 820 struct lwp_info *lwp = get_thread_lwp (thread);
95954743
PA
821 int pid = * (int *) args;
822
823 if (ptid_get_pid (entry->id) != pid)
824 return 0;
6ad8ae5c 825
bd99dc85
PA
826 /* If we're detaching from a running inferior, make sure it is
827 stopped first, as PTRACE_DETACH will not work otherwise. */
828 if (!lwp->stopped)
829 {
95954743 830 int lwpid = lwpid_of (lwp);
bd99dc85
PA
831
832 stopping_threads = 1;
833 send_sigstop (&lwp->head);
834
835 /* If this detects a new thread through a clone event, the new
836 thread is appended to the end of the lwp list, so we'll
837 eventually detach from it. */
838 wait_for_sigstop (&lwp->head);
839 stopping_threads = 0;
840
841 /* If LWP exits while we're trying to stop it, there's nothing
842 left to do. */
95954743 843 lwp = find_lwp_pid (pid_to_ptid (lwpid));
bd99dc85 844 if (lwp == NULL)
95954743 845 return 0;
bd99dc85
PA
846 }
847
ae13219e
DJ
848 /* If this process is stopped but is expecting a SIGSTOP, then make
849 sure we take care of that now. This isn't absolutely guaranteed
850 to collect the SIGSTOP, but is fairly likely to. */
54a0b537 851 if (lwp->stop_expected)
ae13219e 852 {
bd99dc85 853 int wstat;
ae13219e 854 /* Clear stop_expected, so that the SIGSTOP will be reported. */
54a0b537
PA
855 lwp->stop_expected = 0;
856 if (lwp->stopped)
2acc282a 857 linux_resume_one_lwp (lwp, 0, 0, NULL);
95954743 858 linux_wait_for_event (lwp->head.id, &wstat, __WALL);
ae13219e
DJ
859 }
860
861 /* Flush any pending changes to the process's registers. */
862 regcache_invalidate_one ((struct inferior_list_entry *)
54a0b537 863 get_lwp_thread (lwp));
ae13219e
DJ
864
865 /* Finally, let it resume. */
bd99dc85
PA
866 ptrace (PTRACE_DETACH, lwpid_of (lwp), 0, 0);
867
868 delete_lwp (lwp);
95954743 869 return 0;
6ad8ae5c
DJ
870}
871
dd6953e1 872static int
95954743 873any_thread_of (struct inferior_list_entry *entry, void *args)
6ad8ae5c 874{
95954743
PA
875 int *pid_p = args;
876
877 if (ptid_get_pid (entry->id) == *pid_p)
878 return 1;
879
880 return 0;
881}
882
883static int
884linux_detach (int pid)
885{
886 struct process_info *process;
887
888 process = find_process_pid (pid);
889 if (process == NULL)
890 return -1;
891
ca5c370d
PA
892#ifdef USE_THREAD_DB
893 thread_db_free (process, 1);
894#endif
895
95954743
PA
896 current_inferior =
897 (struct thread_info *) find_inferior (&all_threads, any_thread_of, &pid);
898
ae13219e 899 delete_all_breakpoints ();
95954743 900 find_inferior (&all_threads, linux_detach_one_lwp, &pid);
ca5c370d 901 linux_remove_process (process);
dd6953e1 902 return 0;
6ad8ae5c
DJ
903}
904
444d6139 905static void
95954743 906linux_join (int pid)
444d6139 907{
444d6139 908 int status, ret;
95954743 909 struct process_info *process;
bd99dc85 910
95954743
PA
911 process = find_process_pid (pid);
912 if (process == NULL)
913 return;
444d6139
PA
914
915 do {
95954743 916 ret = my_waitpid (pid, &status, 0);
444d6139
PA
917 if (WIFEXITED (status) || WIFSIGNALED (status))
918 break;
919 } while (ret != -1 || errno != ECHILD);
920}
921
6ad8ae5c 922/* Return nonzero if the given thread is still alive. */
0d62e5e8 923static int
95954743 924linux_thread_alive (ptid_t ptid)
0d62e5e8 925{
95954743
PA
926 struct lwp_info *lwp = find_lwp_pid (ptid);
927
928 /* We assume we always know if a thread exits. If a whole process
929 exited but we still haven't been able to report it to GDB, we'll
930 hold on to the last lwp of the dead process. */
931 if (lwp != NULL)
932 return !lwp->dead;
0d62e5e8
DJ
933 else
934 return 0;
935}
936
54a0b537
PA
937/* Return 1 if this lwp has an interesting status pending. This
938 function may silently resume an inferior lwp. */
611cb4a5 939static int
d50171e4 940status_pending_p_callback (struct inferior_list_entry *entry, void *arg)
0d62e5e8 941{
54a0b537 942 struct lwp_info *lwp = (struct lwp_info *) entry;
95954743 943 ptid_t ptid = * (ptid_t *) arg;
d50171e4 944 struct thread_info *thread = get_lwp_thread (lwp);
95954743
PA
945
946 /* Check if we're only interested in events from a specific process
947 or its lwps. */
948 if (!ptid_equal (minus_one_ptid, ptid)
949 && ptid_get_pid (ptid) != ptid_get_pid (lwp->head.id))
950 return 0;
0d62e5e8 951
d50171e4
PA
952 thread = get_lwp_thread (lwp);
953
954 /* If we got a `vCont;t', but we haven't reported a stop yet, do
955 report any status pending the LWP may have. */
956 if (lwp->last_resume_kind == resume_stop
957 && thread->last_status.kind == TARGET_WAITKIND_STOPPED)
958 return 0;
0d62e5e8 959
d50171e4 960 return lwp->status_pending_p;
0d62e5e8
DJ
961}
962
95954743
PA
963static int
964same_lwp (struct inferior_list_entry *entry, void *data)
965{
966 ptid_t ptid = *(ptid_t *) data;
967 int lwp;
968
969 if (ptid_get_lwp (ptid) != 0)
970 lwp = ptid_get_lwp (ptid);
971 else
972 lwp = ptid_get_pid (ptid);
973
974 if (ptid_get_lwp (entry->id) == lwp)
975 return 1;
976
977 return 0;
978}
979
980struct lwp_info *
981find_lwp_pid (ptid_t ptid)
982{
983 return (struct lwp_info*) find_inferior (&all_lwps, same_lwp, &ptid);
984}
985
bd99dc85 986static struct lwp_info *
95954743 987linux_wait_for_lwp (ptid_t ptid, int *wstatp, int options)
611cb4a5 988{
0d62e5e8 989 int ret;
95954743 990 int to_wait_for = -1;
bd99dc85 991 struct lwp_info *child = NULL;
0d62e5e8 992
bd99dc85 993 if (debug_threads)
95954743
PA
994 fprintf (stderr, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid));
995
996 if (ptid_equal (ptid, minus_one_ptid))
997 to_wait_for = -1; /* any child */
998 else
999 to_wait_for = ptid_get_lwp (ptid); /* this lwp only */
0d62e5e8 1000
bd99dc85 1001 options |= __WALL;
0d62e5e8 1002
bd99dc85 1003retry:
0d62e5e8 1004
bd99dc85
PA
1005 ret = my_waitpid (to_wait_for, wstatp, options);
1006 if (ret == 0 || (ret == -1 && errno == ECHILD && (options & WNOHANG)))
1007 return NULL;
1008 else if (ret == -1)
1009 perror_with_name ("waitpid");
0d62e5e8
DJ
1010
1011 if (debug_threads
1012 && (!WIFSTOPPED (*wstatp)
1013 || (WSTOPSIG (*wstatp) != 32
1014 && WSTOPSIG (*wstatp) != 33)))
1015 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
1016
95954743 1017 child = find_lwp_pid (pid_to_ptid (ret));
0d62e5e8 1018
24a09b5f
DJ
1019 /* If we didn't find a process, one of two things presumably happened:
1020 - A process we started and then detached from has exited. Ignore it.
1021 - A process we are controlling has forked and the new child's stop
1022 was reported to us by the kernel. Save its PID. */
bd99dc85 1023 if (child == NULL && WIFSTOPPED (*wstatp))
24a09b5f
DJ
1024 {
1025 add_pid_to_list (&stopped_pids, ret);
1026 goto retry;
1027 }
bd99dc85 1028 else if (child == NULL)
24a09b5f
DJ
1029 goto retry;
1030
bd99dc85 1031 child->stopped = 1;
0d62e5e8 1032
bd99dc85 1033 child->last_status = *wstatp;
32ca6d61 1034
d61ddec4
UW
1035 /* Architecture-specific setup after inferior is running.
1036 This needs to happen after we have attached to the inferior
1037 and it is stopped for the first time, but before we access
1038 any inferior registers. */
1039 if (new_inferior)
1040 {
1041 the_low_target.arch_setup ();
52fa2412
UW
1042#ifdef HAVE_LINUX_REGSETS
1043 memset (disabled_regsets, 0, num_regsets);
1044#endif
d61ddec4
UW
1045 new_inferior = 0;
1046 }
1047
c3adc08c
PA
1048 /* Fetch the possibly triggered data watchpoint info and store it in
1049 CHILD.
1050
1051 On some archs, like x86, that use debug registers to set
1052 watchpoints, it's possible that the way to know which watched
1053 address trapped, is to check the register that is used to select
1054 which address to watch. Problem is, between setting the
1055 watchpoint and reading back which data address trapped, the user
1056 may change the set of watchpoints, and, as a consequence, GDB
1057 changes the debug registers in the inferior. To avoid reading
1058 back a stale stopped-data-address when that happens, we cache in
1059 LP the fact that a watchpoint trapped, and the corresponding data
1060 address, as soon as we see CHILD stop with a SIGTRAP. If GDB
1061 changes the debug registers meanwhile, we have the cached data we
1062 can rely on. */
1063
1064 if (WIFSTOPPED (*wstatp) && WSTOPSIG (*wstatp) == SIGTRAP)
1065 {
1066 if (the_low_target.stopped_by_watchpoint == NULL)
1067 {
1068 child->stopped_by_watchpoint = 0;
1069 }
1070 else
1071 {
1072 struct thread_info *saved_inferior;
1073
1074 saved_inferior = current_inferior;
1075 current_inferior = get_lwp_thread (child);
1076
1077 child->stopped_by_watchpoint
1078 = the_low_target.stopped_by_watchpoint ();
1079
1080 if (child->stopped_by_watchpoint)
1081 {
1082 if (the_low_target.stopped_data_address != NULL)
1083 child->stopped_data_address
1084 = the_low_target.stopped_data_address ();
1085 else
1086 child->stopped_data_address = 0;
1087 }
1088
1089 current_inferior = saved_inferior;
1090 }
1091 }
1092
d50171e4
PA
1093 /* Store the STOP_PC, with adjustment applied. This depends on the
1094 architecture being defined already (so that CHILD has a valid
1095 regcache), and on LAST_STATUS being set (to check for SIGTRAP or
1096 not). */
1097 if (WIFSTOPPED (*wstatp))
1098 child->stop_pc = get_stop_pc (child);
1099
0d62e5e8 1100 if (debug_threads
47c0c975
DE
1101 && WIFSTOPPED (*wstatp)
1102 && the_low_target.get_pc != NULL)
0d62e5e8 1103 {
896c7fbb 1104 struct thread_info *saved_inferior = current_inferior;
bce522a2 1105 struct regcache *regcache;
47c0c975
DE
1106 CORE_ADDR pc;
1107
d50171e4 1108 current_inferior = get_lwp_thread (child);
bce522a2 1109 regcache = get_thread_regcache (current_inferior, 1);
442ea881 1110 pc = (*the_low_target.get_pc) (regcache);
47c0c975 1111 fprintf (stderr, "linux_wait_for_lwp: pc is 0x%lx\n", (long) pc);
896c7fbb 1112 current_inferior = saved_inferior;
0d62e5e8 1113 }
bd99dc85
PA
1114
1115 return child;
0d62e5e8 1116}
611cb4a5 1117
d50171e4
PA
1118/* Arrange for a breakpoint to be hit again later. We don't keep the
1119 SIGTRAP status and don't forward the SIGTRAP signal to the LWP. We
1120 will handle the current event, eventually we will resume this LWP,
1121 and this breakpoint will trap again. */
1122
1123static int
1124cancel_breakpoint (struct lwp_info *lwp)
1125{
1126 struct thread_info *saved_inferior;
1127 struct regcache *regcache;
1128
1129 /* There's nothing to do if we don't support breakpoints. */
1130 if (!supports_breakpoints ())
1131 return 0;
1132
1133 if (lwp->stepping)
1134 {
1135 if (debug_threads)
1136 fprintf (stderr,
1137 "CB: [%s] is stepping\n",
1138 target_pid_to_str (lwp->head.id));
1139 return 0;
1140 }
1141
1142 regcache = get_thread_regcache (get_lwp_thread (lwp), 1);
1143
1144 /* breakpoint_at reads from current inferior. */
1145 saved_inferior = current_inferior;
1146 current_inferior = get_lwp_thread (lwp);
1147
1148 if ((*the_low_target.breakpoint_at) (lwp->stop_pc))
1149 {
1150 if (debug_threads)
1151 fprintf (stderr,
1152 "CB: Push back breakpoint for %s\n",
1153 target_pid_to_str (lwp->head.id));
1154
1155 /* Back up the PC if necessary. */
1156 if (the_low_target.decr_pc_after_break)
1157 {
1158 struct regcache *regcache
1159 = get_thread_regcache (get_lwp_thread (lwp), 1);
1160 (*the_low_target.set_pc) (regcache, lwp->stop_pc);
1161 }
1162
1163 current_inferior = saved_inferior;
1164 return 1;
1165 }
1166 else
1167 {
1168 if (debug_threads)
1169 fprintf (stderr,
1170 "CB: No breakpoint found at %s for [%s]\n",
1171 paddress (lwp->stop_pc),
1172 target_pid_to_str (lwp->head.id));
1173 }
1174
1175 current_inferior = saved_inferior;
1176 return 0;
1177}
1178
1179/* When the event-loop is doing a step-over, this points at the thread
1180 being stepped. */
1181ptid_t step_over_bkpt;
1182
bd99dc85
PA
1183/* Wait for an event from child PID. If PID is -1, wait for any
1184 child. Store the stop status through the status pointer WSTAT.
1185 OPTIONS is passed to the waitpid call. Return 0 if no child stop
1186 event was found and OPTIONS contains WNOHANG. Return the PID of
1187 the stopped child otherwise. */
1188
0d62e5e8 1189static int
95954743 1190linux_wait_for_event_1 (ptid_t ptid, int *wstat, int options)
0d62e5e8
DJ
1191{
1192 CORE_ADDR stop_pc;
d50171e4
PA
1193 struct lwp_info *event_child, *requested_child;
1194
1195 again:
1196 event_child = NULL;
1197 requested_child = NULL;
0d62e5e8 1198
95954743 1199 /* Check for a lwp with a pending status. */
bd99dc85 1200
95954743
PA
1201 if (ptid_equal (ptid, minus_one_ptid)
1202 || ptid_equal (pid_to_ptid (ptid_get_pid (ptid)), ptid))
0d62e5e8 1203 {
54a0b537 1204 event_child = (struct lwp_info *)
d50171e4 1205 find_inferior (&all_lwps, status_pending_p_callback, &ptid);
0d62e5e8 1206 if (debug_threads && event_child)
bd99dc85 1207 fprintf (stderr, "Got a pending child %ld\n", lwpid_of (event_child));
0d62e5e8
DJ
1208 }
1209 else
1210 {
95954743 1211 requested_child = find_lwp_pid (ptid);
d50171e4
PA
1212
1213 if (requested_child->status_pending_p)
bd99dc85 1214 event_child = requested_child;
0d62e5e8 1215 }
611cb4a5 1216
0d62e5e8
DJ
1217 if (event_child != NULL)
1218 {
bd99dc85
PA
1219 if (debug_threads)
1220 fprintf (stderr, "Got an event from pending child %ld (%04x)\n",
1221 lwpid_of (event_child), event_child->status_pending);
1222 *wstat = event_child->status_pending;
1223 event_child->status_pending_p = 0;
1224 event_child->status_pending = 0;
1225 current_inferior = get_lwp_thread (event_child);
1226 return lwpid_of (event_child);
0d62e5e8
DJ
1227 }
1228
1229 /* We only enter this loop if no process has a pending wait status. Thus
1230 any action taken in response to a wait status inside this loop is
1231 responding as soon as we detect the status, not after any pending
1232 events. */
1233 while (1)
1234 {
d50171e4
PA
1235 int step_over_finished = 0;
1236 int bp_explains_trap = 0;
1237 int cancel_sigtrap;
1238
1239 if (ptid_equal (step_over_bkpt, null_ptid))
1240 event_child = linux_wait_for_lwp (ptid, wstat, options);
1241 else
1242 {
1243 if (debug_threads)
1244 fprintf (stderr, "step_over_bkpt set [%s], doing a blocking wait\n",
1245 target_pid_to_str (step_over_bkpt));
1246 event_child = linux_wait_for_lwp (step_over_bkpt,
1247 wstat, options & ~WNOHANG);
1248 }
0d62e5e8 1249
bd99dc85 1250 if ((options & WNOHANG) && event_child == NULL)
d50171e4
PA
1251 {
1252 if (debug_threads)
1253 fprintf (stderr, "WNOHANG set, no event found\n");
1254 return 0;
1255 }
0d62e5e8
DJ
1256
1257 if (event_child == NULL)
1258 error ("event from unknown child");
611cb4a5 1259
bd99dc85 1260 current_inferior = get_lwp_thread (event_child);
0d62e5e8 1261
89be2091 1262 /* Check for thread exit. */
bd99dc85 1263 if (! WIFSTOPPED (*wstat))
0d62e5e8 1264 {
89be2091 1265 if (debug_threads)
95954743 1266 fprintf (stderr, "LWP %ld exiting\n", lwpid_of (event_child));
89be2091
DJ
1267
1268 /* If the last thread is exiting, just return. */
95954743 1269 if (last_thread_of_process_p (current_inferior))
bd99dc85
PA
1270 {
1271 if (debug_threads)
95954743
PA
1272 fprintf (stderr, "LWP %ld is last lwp of process\n",
1273 lwpid_of (event_child));
bd99dc85
PA
1274 return lwpid_of (event_child);
1275 }
89be2091 1276
bd99dc85
PA
1277 if (!non_stop)
1278 {
1279 current_inferior = (struct thread_info *) all_threads.head;
1280 if (debug_threads)
1281 fprintf (stderr, "Current inferior is now %ld\n",
1282 lwpid_of (get_thread_lwp (current_inferior)));
1283 }
1284 else
1285 {
1286 current_inferior = NULL;
1287 if (debug_threads)
1288 fprintf (stderr, "Current inferior is now <NULL>\n");
1289 }
89be2091
DJ
1290
1291 /* If we were waiting for this particular child to do something...
1292 well, it did something. */
bd99dc85 1293 if (requested_child != NULL)
d50171e4
PA
1294 {
1295 int lwpid = lwpid_of (event_child);
1296
1297 /* Cancel the step-over operation --- the thread that
1298 started it is gone. */
1299 if (finish_step_over (event_child))
1300 unstop_all_lwps (event_child);
1301 delete_lwp (event_child);
1302 return lwpid;
1303 }
1304
1305 delete_lwp (event_child);
89be2091
DJ
1306
1307 /* Wait for a more interesting event. */
1308 continue;
1309 }
1310
a6dbe5df
PA
1311 if (event_child->must_set_ptrace_flags)
1312 {
1313 ptrace (PTRACE_SETOPTIONS, lwpid_of (event_child),
14ce3065 1314 0, (PTRACE_ARG4_TYPE) PTRACE_O_TRACECLONE);
a6dbe5df
PA
1315 event_child->must_set_ptrace_flags = 0;
1316 }
1317
bd99dc85
PA
1318 if (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) == SIGTRAP
1319 && *wstat >> 16 != 0)
24a09b5f 1320 {
bd99dc85 1321 handle_extended_wait (event_child, *wstat);
24a09b5f
DJ
1322 continue;
1323 }
1324
89be2091
DJ
1325 /* If GDB is not interested in this signal, don't stop other
1326 threads, and don't report it to GDB. Just resume the
1327 inferior right away. We do this for threading-related
69f223ed
DJ
1328 signals as well as any that GDB specifically requested we
1329 ignore. But never ignore SIGSTOP if we sent it ourselves,
1330 and do not ignore signals when stepping - they may require
1331 special handling to skip the signal handler. */
89be2091
DJ
1332 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
1333 thread library? */
bd99dc85 1334 if (WIFSTOPPED (*wstat)
69f223ed 1335 && !event_child->stepping
24a09b5f 1336 && (
60c3d7b0 1337#if defined (USE_THREAD_DB) && defined (__SIGRTMIN)
cdbfd419 1338 (current_process ()->private->thread_db != NULL
bd99dc85
PA
1339 && (WSTOPSIG (*wstat) == __SIGRTMIN
1340 || WSTOPSIG (*wstat) == __SIGRTMIN + 1))
24a09b5f
DJ
1341 ||
1342#endif
bd99dc85 1343 (pass_signals[target_signal_from_host (WSTOPSIG (*wstat))]
d50171e4
PA
1344 && !(WSTOPSIG (*wstat) == SIGSTOP
1345 && event_child->stop_expected))))
89be2091
DJ
1346 {
1347 siginfo_t info, *info_p;
1348
1349 if (debug_threads)
24a09b5f 1350 fprintf (stderr, "Ignored signal %d for LWP %ld.\n",
bd99dc85 1351 WSTOPSIG (*wstat), lwpid_of (event_child));
89be2091 1352
bd99dc85 1353 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (event_child), 0, &info) == 0)
89be2091
DJ
1354 info_p = &info;
1355 else
1356 info_p = NULL;
d50171e4 1357 linux_resume_one_lwp (event_child, event_child->stepping,
bd99dc85 1358 WSTOPSIG (*wstat), info_p);
89be2091 1359 continue;
0d62e5e8 1360 }
611cb4a5 1361
d50171e4
PA
1362 if (WIFSTOPPED (*wstat)
1363 && WSTOPSIG (*wstat) == SIGSTOP
1364 && event_child->stop_expected)
1365 {
1366 int should_stop;
1367
1368 if (debug_threads)
1369 fprintf (stderr, "Expected stop.\n");
1370 event_child->stop_expected = 0;
1371
1372 should_stop = (event_child->last_resume_kind == resume_stop
1373 || stopping_threads);
1374
1375 if (!should_stop)
1376 {
1377 linux_resume_one_lwp (event_child,
1378 event_child->stepping, 0, NULL);
1379 continue;
1380 }
1381 }
1382
1383 cancel_sigtrap = (stopping_threads
1384 || event_child->last_resume_kind == resume_stop);
1385
1386 /* Do not allow nested internal breakpoint handling, or leave
1387 the unadjusted PCs visible to GDB. Simply cancel the
1388 breakpoint now, and eventually when the thread is resumed, it
1389 will trap again, if the breakpoint is still there by
1390 then. */
1391 if (WIFSTOPPED (*wstat)
1392 && WSTOPSIG (*wstat) == SIGTRAP
1393 && cancel_sigtrap)
1394 {
1395 if (debug_threads)
1396 fprintf (stderr, "Got a nested SIGTRAP while stopping threads\n");
1397
1398 if (cancel_breakpoint (event_child))
1399 {
1400 if (debug_threads)
1401 fprintf (stderr, " bkpt canceled\n");
1402
1403 /* We don't resume immediately to collect the SIGSTOP,
1404 due to other reasons we may care for this SIGTRAP
1405 below. Care must be taken to not process anything
1406 breakpoint related though from this point on. */
1407 }
1408 }
1409
1410 /* If this event was not handled above, and is not a SIGTRAP, we
1411 report it. SIGILL and SIGSEGV are also treated as traps in
1412 case a breakpoint is inserted at the current PC. If this
1413 target does not support breakpoints, we also report the
1414 SIGTRAP without further processing; it's of no concern to
1415 us. */
2b009048 1416 if (!WIFSTOPPED (*wstat)
d50171e4
PA
1417 || !supports_breakpoints ()
1418 || (WSTOPSIG (*wstat) != SIGTRAP
1419 && WSTOPSIG (*wstat) != SIGILL
1420 && WSTOPSIG (*wstat) != SIGSEGV)
1421 /* Only handle SIGILL or SIGSEGV if we've hit a recognized
1422 breakpoint. */
1423 || (WSTOPSIG (*wstat) != SIGTRAP
1424 && !(*the_low_target.breakpoint_at) (event_child->stop_pc)))
1425 {
1426 if (debug_threads && WIFSTOPPED (*wstat))
1427 fprintf (stderr, "Reporting signal %d for LWP %ld.\n",
1428 WSTOPSIG (*wstat), lwpid_of (event_child));
611cb4a5 1429
d50171e4
PA
1430 /* If we were stepping over a breakpoint, this signal
1431 arriving means we didn't manage to move past the
1432 breakpoint location. Cancel the operation for now --- it
1433 will be handled again on the next resume, if required
1434 (the breakpoint may be removed meanwhile, for
1435 example). */
1436 if (finish_step_over (event_child))
1437 {
1438 event_child->need_step_over = 1;
1439 unstop_all_lwps (event_child);
1440 }
1441 return lwpid_of (event_child);
1442 }
0d62e5e8 1443
d50171e4 1444 stop_pc = event_child->stop_pc;
0d62e5e8 1445
d50171e4
PA
1446 /* Handle anything that requires bookkeeping before deciding to
1447 report the event or continue waiting. */
1448
1449 /* First check if we can explain the SIGTRAP with an internal
1450 breakpoint, or if we should possibly report the event to GDB.
1451 Do this before anything that may remove or insert a
2b009048 1452 breakpoint. */
d50171e4
PA
1453 bp_explains_trap = breakpoint_inserted_here (stop_pc);
1454
1455 /* We have a SIGTRAP, possibly a step-over dance has just
1456 finished. If so, tweak the state machine accordingly,
1457 reinsert breakpoints and delete any reinsert (software
1458 single-step) breakpoints. */
1459 step_over_finished = finish_step_over (event_child);
1460
1461 /* Now invoke the callbacks of any breakpoints there. */
1462 if (!cancel_sigtrap)
1463 check_breakpoints (stop_pc);
1464
1465 /* If we're stopping threads, resume once more to collect the
1466 SIGSTOP, and do nothing else. Note that we don't set
1467 need_step_over. If this predicate matches, then we've
1468 cancelled the SIGTRAP before reaching here, and we do want
1469 any breakpoint at STOP_PC to be re-hit on resume. */
1470 if (stopping_threads
1471 || event_child->last_resume_kind == resume_stop)
0d62e5e8 1472 {
d50171e4
PA
1473 gdb_assert (cancel_sigtrap);
1474
1475 if (step_over_finished)
1476 unstop_all_lwps (event_child);
1477
0d62e5e8 1478 if (debug_threads)
d50171e4
PA
1479 {
1480 if (event_child->last_resume_kind == resume_stop)
1481 fprintf (stderr, "Bailing out; GDB wanted the LWP to stop.\n");
1482 else if (stopping_threads)
1483 fprintf (stderr, "Bailing out; stopping threads.\n");
1484
1485 if (bp_explains_trap)
1486 fprintf (stderr, " Hit a breakpoint.\n");
1487 if (step_over_finished)
1488 fprintf (stderr, " Step-over finished.\n");
1489 if (event_child->stopped_by_watchpoint)
1490 fprintf (stderr, " Stopped by watchpoint.\n");
1491 }
1492
1493 /* Leave these pending. */
1494 if (event_child->stopped_by_watchpoint)
1495 return lwpid_of (event_child);
1496
1497 /* Otherwise, there may or not be a pending SIGSTOP. If
1498 there isn't one, queue one up. In any case, go back to
1499 the event loop to collect it. Don't return yet, as we
1500 don't want this SIGTRAP to be left pending. Note that
1501 since we cancelled the breakpoint above, the PC is
1502 already adjusted, and hence get_stop_pc returns the
1503 correct PC when we collect the SIGSTOP. */
1504
1505 if (!event_child->stop_expected)
1506 {
1507 event_child->stop_expected = 1;
1508 kill_lwp (lwpid_of (event_child), SIGSTOP);
1509 }
0d62e5e8
DJ
1510
1511 /* Clear the single-stepping flag and SIGTRAP as we resume. */
2acc282a 1512 linux_resume_one_lwp (event_child, 0, 0, NULL);
0d62e5e8
DJ
1513 continue;
1514 }
1515
d50171e4
PA
1516 /* We have all the data we need. Either report the event to
1517 GDB, or resume threads and keep waiting for more. */
0d62e5e8 1518
d50171e4
PA
1519 /* Check If GDB would be interested in this event. If GDB
1520 wanted this thread to single step, we always want to report
1521 the SIGTRAP, and let GDB handle it. */
1522 if ((event_child->last_resume_kind == resume_step)
1523 || event_child->stopped_by_watchpoint)
0d62e5e8 1524 {
d50171e4
PA
1525 if (step_over_finished)
1526 unstop_all_lwps (event_child);
bd99dc85 1527
d50171e4 1528 if (debug_threads)
0d62e5e8 1529 {
d50171e4
PA
1530 if (event_child->last_resume_kind == resume_step)
1531 fprintf (stderr, "GDB wanted to single-step, reporting event.\n");
1532 if (event_child->stopped_by_watchpoint)
1533 fprintf (stderr, "Stopped by watchpoint.\n");
0d62e5e8 1534 }
d50171e4
PA
1535 }
1536 /* We found no reason GDB would want us to stop. We either hit
1537 one of our own breakpoints, or finished an internal step GDB
1538 shouldn't know about. */
1539 else if (step_over_finished || bp_explains_trap)
1540 {
1541 if (debug_threads)
0d62e5e8 1542 {
d50171e4
PA
1543 if (bp_explains_trap)
1544 fprintf (stderr, "Hit a gdbserver breakpoint.\n");
1545 if (step_over_finished)
1546 fprintf (stderr, "Step-over finished.\n");
611cb4a5 1547 }
0d62e5e8 1548
d50171e4
PA
1549 /* If we stepped or ran into an internal breakpoint, we've
1550 already handled it. So next time we resume (from this
1551 PC), we should step over it. */
1552 if (breakpoint_here (stop_pc))
1553 event_child->need_step_over = 1;
0d62e5e8 1554
d50171e4
PA
1555 /* We're not reporting this breakpoint to GDB, so apply the
1556 decr_pc_after_break adjustment to the inferior's regcache
1557 ourselves. */
1558
1559 if (the_low_target.set_pc != NULL)
1560 {
1561 struct regcache *regcache
1562 = get_thread_regcache (get_lwp_thread (event_child), 1);
1563 (*the_low_target.set_pc) (regcache, stop_pc);
1564 }
1565
1566 /* We've finished stepping over a breakpoint. We've stopped
1567 all LWPs momentarily except the stepping one. This is
1568 where we resume them all again. We're going to keep
1569 waiting, so use proceed, which handles stepping over the
1570 next breakpoint. */
1571 if (debug_threads)
1572 fprintf (stderr, "proceeding all threads.\n");
1573 proceed_all_lwps ();
1574
1575 /* If we stopped threads momentarily, we may have threads
1576 with pending statuses now (except when we're going to
1577 force the next event out of a specific LWP, in which case
1578 don't want to handle the pending events of other LWPs
1579 yet. */
1580 if (ptid_equal (step_over_bkpt, null_ptid))
1581 goto again;
1582 else
1583 continue;
1584 }
1585 else
0d62e5e8 1586 {
d50171e4
PA
1587 /* GDB breakpoint or program trap, perhaps. */
1588 if (step_over_finished)
1589 unstop_all_lwps (event_child);
611cb4a5
DJ
1590 }
1591
d50171e4
PA
1592 if (debug_threads)
1593 fprintf (stderr, "Hit a non-gdbserver trap event.\n");
1594
bd99dc85 1595 return lwpid_of (event_child);
611cb4a5 1596 }
0d62e5e8 1597
611cb4a5
DJ
1598 /* NOTREACHED */
1599 return 0;
1600}
1601
95954743
PA
1602static int
1603linux_wait_for_event (ptid_t ptid, int *wstat, int options)
1604{
1605 ptid_t wait_ptid;
1606
1607 if (ptid_is_pid (ptid))
1608 {
1609 /* A request to wait for a specific tgid. This is not possible
1610 with waitpid, so instead, we wait for any child, and leave
1611 children we're not interested in right now with a pending
1612 status to report later. */
1613 wait_ptid = minus_one_ptid;
1614 }
1615 else
1616 wait_ptid = ptid;
1617
1618 while (1)
1619 {
1620 int event_pid;
1621
1622 event_pid = linux_wait_for_event_1 (wait_ptid, wstat, options);
1623
1624 if (event_pid > 0
1625 && ptid_is_pid (ptid) && ptid_get_pid (ptid) != event_pid)
1626 {
1627 struct lwp_info *event_child = find_lwp_pid (pid_to_ptid (event_pid));
1628
1629 if (! WIFSTOPPED (*wstat))
1630 mark_lwp_dead (event_child, *wstat);
1631 else
1632 {
1633 event_child->status_pending_p = 1;
1634 event_child->status_pending = *wstat;
1635 }
1636 }
1637 else
1638 return event_pid;
1639 }
1640}
1641
d50171e4
PA
1642/* Set this inferior LWP's state as "want-stopped". We won't resume
1643 this LWP until the client gives us another action for it. */
1644
1645static void
1646gdb_wants_lwp_stopped (struct inferior_list_entry *entry)
1647{
1648 struct lwp_info *lwp = (struct lwp_info *) entry;
1649 struct thread_info *thread = get_lwp_thread (lwp);
1650
1651 /* Most threads are stopped implicitly (all-stop); tag that with
1652 signal 0. The thread being explicitly reported stopped to the
1653 client, gets it's status fixed up afterwards. */
1654 thread->last_status.kind = TARGET_WAITKIND_STOPPED;
1655 thread->last_status.value.sig = TARGET_SIGNAL_0;
1656
1657 lwp->last_resume_kind = resume_stop;
1658}
1659
1660/* Set all LWP's states as "want-stopped". */
1661
1662static void
1663gdb_wants_all_stopped (void)
1664{
1665 for_each_inferior (&all_lwps, gdb_wants_lwp_stopped);
1666}
1667
0d62e5e8 1668/* Wait for process, returns status. */
da6d8c04 1669
95954743
PA
1670static ptid_t
1671linux_wait_1 (ptid_t ptid,
1672 struct target_waitstatus *ourstatus, int target_options)
da6d8c04 1673{
e5f1222d 1674 int w;
bd99dc85
PA
1675 struct thread_info *thread = NULL;
1676 struct lwp_info *lwp = NULL;
1677 int options;
bd99dc85
PA
1678 int pid;
1679
1680 /* Translate generic target options into linux options. */
1681 options = __WALL;
1682 if (target_options & TARGET_WNOHANG)
1683 options |= WNOHANG;
0d62e5e8
DJ
1684
1685retry:
bd99dc85
PA
1686 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1687
0d62e5e8
DJ
1688 /* If we were only supposed to resume one thread, only wait for
1689 that thread - if it's still alive. If it died, however - which
1690 can happen if we're coming from the thread death case below -
1691 then we need to make sure we restart the other threads. We could
1692 pick a thread at random or restart all; restarting all is less
1693 arbitrary. */
95954743
PA
1694 if (!non_stop
1695 && !ptid_equal (cont_thread, null_ptid)
1696 && !ptid_equal (cont_thread, minus_one_ptid))
0d62e5e8 1697 {
bd99dc85
PA
1698 thread = (struct thread_info *) find_inferior_id (&all_threads,
1699 cont_thread);
0d62e5e8
DJ
1700
1701 /* No stepping, no signal - unless one is pending already, of course. */
bd99dc85 1702 if (thread == NULL)
64386c31
DJ
1703 {
1704 struct thread_resume resume_info;
95954743 1705 resume_info.thread = minus_one_ptid;
bd99dc85
PA
1706 resume_info.kind = resume_continue;
1707 resume_info.sig = 0;
2bd7c093 1708 linux_resume (&resume_info, 1);
64386c31 1709 }
bd99dc85 1710 else
95954743 1711 ptid = cont_thread;
0d62e5e8 1712 }
da6d8c04 1713
95954743 1714 pid = linux_wait_for_event (ptid, &w, options);
bd99dc85 1715 if (pid == 0) /* only if TARGET_WNOHANG */
95954743 1716 return null_ptid;
bd99dc85
PA
1717
1718 lwp = get_thread_lwp (current_inferior);
da6d8c04 1719
0d62e5e8
DJ
1720 /* If we are waiting for a particular child, and it exited,
1721 linux_wait_for_event will return its exit status. Similarly if
1722 the last child exited. If this is not the last child, however,
1723 do not report it as exited until there is a 'thread exited' response
1724 available in the remote protocol. Instead, just wait for another event.
1725 This should be safe, because if the thread crashed we will already
1726 have reported the termination signal to GDB; that should stop any
1727 in-progress stepping operations, etc.
1728
1729 Report the exit status of the last thread to exit. This matches
1730 LinuxThreads' behavior. */
1731
95954743 1732 if (last_thread_of_process_p (current_inferior))
da6d8c04 1733 {
bd99dc85 1734 if (WIFEXITED (w) || WIFSIGNALED (w))
0d62e5e8 1735 {
95954743
PA
1736 int pid = pid_of (lwp);
1737 struct process_info *process = find_process_pid (pid);
5b1c542e 1738
ca5c370d
PA
1739#ifdef USE_THREAD_DB
1740 thread_db_free (process, 0);
1741#endif
bd99dc85 1742 delete_lwp (lwp);
ca5c370d 1743 linux_remove_process (process);
5b1c542e 1744
bd99dc85 1745 current_inferior = NULL;
5b1c542e 1746
bd99dc85
PA
1747 if (WIFEXITED (w))
1748 {
1749 ourstatus->kind = TARGET_WAITKIND_EXITED;
1750 ourstatus->value.integer = WEXITSTATUS (w);
1751
1752 if (debug_threads)
1753 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
1754 }
1755 else
1756 {
1757 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1758 ourstatus->value.sig = target_signal_from_host (WTERMSIG (w));
1759
1760 if (debug_threads)
1761 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
1762
1763 }
5b1c542e 1764
95954743 1765 return pid_to_ptid (pid);
0d62e5e8 1766 }
da6d8c04 1767 }
0d62e5e8 1768 else
da6d8c04 1769 {
0d62e5e8
DJ
1770 if (!WIFSTOPPED (w))
1771 goto retry;
da6d8c04
DJ
1772 }
1773
5b1c542e 1774 ourstatus->kind = TARGET_WAITKIND_STOPPED;
5b1c542e 1775
d50171e4
PA
1776 /* Do this before the gdb_wants_all_stopped calls below, since they
1777 always set last_resume_kind to resume_stop. */
1778 if (lwp->last_resume_kind == resume_stop && WSTOPSIG (w) == SIGSTOP)
bd99dc85
PA
1779 {
1780 /* A thread that has been requested to stop by GDB with vCont;t,
1781 and it stopped cleanly, so report as SIG0. The use of
1782 SIGSTOP is an implementation detail. */
1783 ourstatus->value.sig = TARGET_SIGNAL_0;
1784 }
d50171e4 1785 else if (lwp->last_resume_kind == resume_stop && WSTOPSIG (w) != SIGSTOP)
bd99dc85
PA
1786 {
1787 /* A thread that has been requested to stop by GDB with vCont;t,
d50171e4 1788 but, it stopped for other reasons. */
bd99dc85
PA
1789 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w));
1790 }
1791 else
1792 {
1793 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w));
1794 }
1795
d50171e4
PA
1796 gdb_assert (ptid_equal (step_over_bkpt, null_ptid));
1797
1798 if (!non_stop)
1799 {
1800 /* In all-stop, stop all threads, we're about to report an event
1801 to GDB. */
1802 stop_all_lwps ();
1803
1804 /* Do not leave a pending single-step finish to be reported to
1805 the client. The client will give us a new action for this
1806 thread, possibly a continue request --- otherwise, the client
1807 would consider this pending SIGTRAP reported later a spurious
1808 signal. */
1809 cancel_finished_single_steps (lwp);
1810
1811 /* From GDB's perspective, all-stop mode always stops all
1812 threads implicitly. Tag all threads as "want-stopped". */
1813 gdb_wants_all_stopped ();
1814 }
1815 else
1816 {
1817 /* We're reporting this LWP as stopped. Update it's
1818 "want-stopped" state to what the client wants, until it gets
1819 a new resume action. */
1820 gdb_wants_lwp_stopped (&lwp->head);
1821 }
1822
bd99dc85 1823 if (debug_threads)
95954743
PA
1824 fprintf (stderr, "linux_wait ret = %s, %d, %d\n",
1825 target_pid_to_str (lwp->head.id),
bd99dc85
PA
1826 ourstatus->kind,
1827 ourstatus->value.sig);
1828
d50171e4 1829 get_lwp_thread (lwp)->last_status = *ourstatus;
95954743 1830 return lwp->head.id;
bd99dc85
PA
1831}
1832
1833/* Get rid of any pending event in the pipe. */
1834static void
1835async_file_flush (void)
1836{
1837 int ret;
1838 char buf;
1839
1840 do
1841 ret = read (linux_event_pipe[0], &buf, 1);
1842 while (ret >= 0 || (ret == -1 && errno == EINTR));
1843}
1844
1845/* Put something in the pipe, so the event loop wakes up. */
1846static void
1847async_file_mark (void)
1848{
1849 int ret;
1850
1851 async_file_flush ();
1852
1853 do
1854 ret = write (linux_event_pipe[1], "+", 1);
1855 while (ret == 0 || (ret == -1 && errno == EINTR));
1856
1857 /* Ignore EAGAIN. If the pipe is full, the event loop will already
1858 be awakened anyway. */
1859}
1860
95954743
PA
1861static ptid_t
1862linux_wait (ptid_t ptid,
1863 struct target_waitstatus *ourstatus, int target_options)
bd99dc85 1864{
95954743 1865 ptid_t event_ptid;
bd99dc85
PA
1866
1867 if (debug_threads)
95954743 1868 fprintf (stderr, "linux_wait: [%s]\n", target_pid_to_str (ptid));
bd99dc85
PA
1869
1870 /* Flush the async file first. */
1871 if (target_is_async_p ())
1872 async_file_flush ();
1873
95954743 1874 event_ptid = linux_wait_1 (ptid, ourstatus, target_options);
bd99dc85
PA
1875
1876 /* If at least one stop was reported, there may be more. A single
1877 SIGCHLD can signal more than one child stop. */
1878 if (target_is_async_p ()
1879 && (target_options & TARGET_WNOHANG) != 0
95954743 1880 && !ptid_equal (event_ptid, null_ptid))
bd99dc85
PA
1881 async_file_mark ();
1882
1883 return event_ptid;
da6d8c04
DJ
1884}
1885
c5f62d5f 1886/* Send a signal to an LWP. */
fd500816
DJ
1887
1888static int
a1928bad 1889kill_lwp (unsigned long lwpid, int signo)
fd500816 1890{
c5f62d5f
DE
1891 /* Use tkill, if possible, in case we are using nptl threads. If tkill
1892 fails, then we are not using nptl threads and we should be using kill. */
fd500816 1893
c5f62d5f
DE
1894#ifdef __NR_tkill
1895 {
1896 static int tkill_failed;
fd500816 1897
c5f62d5f
DE
1898 if (!tkill_failed)
1899 {
1900 int ret;
1901
1902 errno = 0;
1903 ret = syscall (__NR_tkill, lwpid, signo);
1904 if (errno != ENOSYS)
1905 return ret;
1906 tkill_failed = 1;
1907 }
1908 }
fd500816
DJ
1909#endif
1910
1911 return kill (lwpid, signo);
1912}
1913
0d62e5e8
DJ
1914static void
1915send_sigstop (struct inferior_list_entry *entry)
1916{
54a0b537 1917 struct lwp_info *lwp = (struct lwp_info *) entry;
bd99dc85 1918 int pid;
0d62e5e8 1919
54a0b537 1920 if (lwp->stopped)
0d62e5e8
DJ
1921 return;
1922
bd99dc85
PA
1923 pid = lwpid_of (lwp);
1924
0d62e5e8
DJ
1925 /* If we already have a pending stop signal for this process, don't
1926 send another. */
54a0b537 1927 if (lwp->stop_expected)
0d62e5e8 1928 {
ae13219e 1929 if (debug_threads)
bd99dc85 1930 fprintf (stderr, "Have pending sigstop for lwp %d\n", pid);
ae13219e 1931
0d62e5e8
DJ
1932 return;
1933 }
1934
1935 if (debug_threads)
bd99dc85 1936 fprintf (stderr, "Sending sigstop to lwp %d\n", pid);
0d62e5e8 1937
d50171e4 1938 lwp->stop_expected = 1;
bd99dc85 1939 kill_lwp (pid, SIGSTOP);
0d62e5e8
DJ
1940}
1941
95954743
PA
1942static void
1943mark_lwp_dead (struct lwp_info *lwp, int wstat)
1944{
1945 /* It's dead, really. */
1946 lwp->dead = 1;
1947
1948 /* Store the exit status for later. */
1949 lwp->status_pending_p = 1;
1950 lwp->status_pending = wstat;
1951
95954743
PA
1952 /* Prevent trying to stop it. */
1953 lwp->stopped = 1;
1954
1955 /* No further stops are expected from a dead lwp. */
1956 lwp->stop_expected = 0;
1957}
1958
d50171e4
PA
1959static int
1960cancel_finished_single_step (struct inferior_list_entry *entry, void *except)
1961{
1962 struct lwp_info *lwp = (struct lwp_info *) entry;
1963 struct thread_info *saved_inferior;
1964
1965 if (lwp == except)
1966 return 0;
1967
1968 saved_inferior = current_inferior;
1969 current_inferior = get_lwp_thread (lwp);
1970
1971 /* Do not leave a pending single-step finish to be reported to the
1972 client. The client will give us a new action for this thread,
1973 possibly a continue request --- otherwise, the client would
1974 consider this pending SIGTRAP reported later a spurious
1975 signal. */
1976 if (lwp->status_pending_p
1977 && WSTOPSIG (lwp->status_pending) == SIGTRAP
1978 && lwp->stepping
1979 && !lwp->stopped_by_watchpoint)
1980 {
1981 if (debug_threads)
1982 fprintf (stderr, " single-step SIGTRAP cancelled\n");
1983
1984 lwp->status_pending_p = 0;
1985 lwp->status_pending = 0;
1986 }
1987
1988 current_inferior = saved_inferior;
1989 return 0;
1990}
1991
1992static void
1993cancel_finished_single_steps (struct lwp_info *except)
1994{
1995 find_inferior (&all_lwps, cancel_finished_single_step, except);
1996}
1997
0d62e5e8
DJ
1998static void
1999wait_for_sigstop (struct inferior_list_entry *entry)
2000{
54a0b537 2001 struct lwp_info *lwp = (struct lwp_info *) entry;
bd99dc85 2002 struct thread_info *saved_inferior;
a1928bad 2003 int wstat;
95954743
PA
2004 ptid_t saved_tid;
2005 ptid_t ptid;
d50171e4 2006 int pid;
0d62e5e8 2007
54a0b537 2008 if (lwp->stopped)
d50171e4
PA
2009 {
2010 if (debug_threads)
2011 fprintf (stderr, "wait_for_sigstop: LWP %ld already stopped\n",
2012 lwpid_of (lwp));
2013 return;
2014 }
0d62e5e8
DJ
2015
2016 saved_inferior = current_inferior;
bd99dc85
PA
2017 if (saved_inferior != NULL)
2018 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
2019 else
95954743 2020 saved_tid = null_ptid; /* avoid bogus unused warning */
bd99dc85 2021
95954743 2022 ptid = lwp->head.id;
bd99dc85 2023
d50171e4
PA
2024 if (debug_threads)
2025 fprintf (stderr, "wait_for_sigstop: pulling one event\n");
2026
2027 pid = linux_wait_for_event (ptid, &wstat, __WALL);
0d62e5e8
DJ
2028
2029 /* If we stopped with a non-SIGSTOP signal, save it for later
2030 and record the pending SIGSTOP. If the process exited, just
2031 return. */
d50171e4 2032 if (WIFSTOPPED (wstat))
0d62e5e8
DJ
2033 {
2034 if (debug_threads)
d50171e4
PA
2035 fprintf (stderr, "LWP %ld stopped with signal %d\n",
2036 lwpid_of (lwp), WSTOPSIG (wstat));
c35fafde 2037
d50171e4 2038 if (WSTOPSIG (wstat) != SIGSTOP)
c35fafde
PA
2039 {
2040 if (debug_threads)
d50171e4
PA
2041 fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n",
2042 lwpid_of (lwp), wstat);
2043
c35fafde
PA
2044 lwp->status_pending_p = 1;
2045 lwp->status_pending = wstat;
2046 }
0d62e5e8 2047 }
d50171e4 2048 else
95954743
PA
2049 {
2050 if (debug_threads)
d50171e4 2051 fprintf (stderr, "Process %d exited while stopping LWPs\n", pid);
95954743 2052
d50171e4
PA
2053 lwp = find_lwp_pid (pid_to_ptid (pid));
2054 if (lwp)
2055 {
2056 /* Leave this status pending for the next time we're able to
2057 report it. In the mean time, we'll report this lwp as
2058 dead to GDB, so GDB doesn't try to read registers and
2059 memory from it. This can only happen if this was the
2060 last thread of the process; otherwise, PID is removed
2061 from the thread tables before linux_wait_for_event
2062 returns. */
2063 mark_lwp_dead (lwp, wstat);
2064 }
95954743 2065 }
0d62e5e8 2066
bd99dc85 2067 if (saved_inferior == NULL || linux_thread_alive (saved_tid))
0d62e5e8
DJ
2068 current_inferior = saved_inferior;
2069 else
2070 {
2071 if (debug_threads)
2072 fprintf (stderr, "Previously current thread died.\n");
2073
bd99dc85
PA
2074 if (non_stop)
2075 {
2076 /* We can't change the current inferior behind GDB's back,
2077 otherwise, a subsequent command may apply to the wrong
2078 process. */
2079 current_inferior = NULL;
2080 }
2081 else
2082 {
2083 /* Set a valid thread as current. */
2084 set_desired_inferior (0);
2085 }
0d62e5e8
DJ
2086 }
2087}
2088
2089static void
54a0b537 2090stop_all_lwps (void)
0d62e5e8
DJ
2091{
2092 stopping_threads = 1;
54a0b537
PA
2093 for_each_inferior (&all_lwps, send_sigstop);
2094 for_each_inferior (&all_lwps, wait_for_sigstop);
0d62e5e8
DJ
2095 stopping_threads = 0;
2096}
2097
da6d8c04
DJ
2098/* Resume execution of the inferior process.
2099 If STEP is nonzero, single-step it.
2100 If SIGNAL is nonzero, give it that signal. */
2101
ce3a066d 2102static void
2acc282a 2103linux_resume_one_lwp (struct lwp_info *lwp,
54a0b537 2104 int step, int signal, siginfo_t *info)
da6d8c04 2105{
0d62e5e8
DJ
2106 struct thread_info *saved_inferior;
2107
54a0b537 2108 if (lwp->stopped == 0)
0d62e5e8
DJ
2109 return;
2110
2111 /* If we have pending signals or status, and a new signal, enqueue the
2112 signal. Also enqueue the signal if we are waiting to reinsert a
2113 breakpoint; it will be picked up again below. */
2114 if (signal != 0
54a0b537
PA
2115 && (lwp->status_pending_p || lwp->pending_signals != NULL
2116 || lwp->bp_reinsert != 0))
0d62e5e8
DJ
2117 {
2118 struct pending_signals *p_sig;
bca929d3 2119 p_sig = xmalloc (sizeof (*p_sig));
54a0b537 2120 p_sig->prev = lwp->pending_signals;
0d62e5e8 2121 p_sig->signal = signal;
32ca6d61
DJ
2122 if (info == NULL)
2123 memset (&p_sig->info, 0, sizeof (siginfo_t));
2124 else
2125 memcpy (&p_sig->info, info, sizeof (siginfo_t));
54a0b537 2126 lwp->pending_signals = p_sig;
0d62e5e8
DJ
2127 }
2128
d50171e4
PA
2129 if (lwp->status_pending_p)
2130 {
2131 if (debug_threads)
2132 fprintf (stderr, "Not resuming lwp %ld (%s, signal %d, stop %s);"
2133 " has pending status\n",
2134 lwpid_of (lwp), step ? "step" : "continue", signal,
2135 lwp->stop_expected ? "expected" : "not expected");
2136 return;
2137 }
0d62e5e8
DJ
2138
2139 saved_inferior = current_inferior;
54a0b537 2140 current_inferior = get_lwp_thread (lwp);
0d62e5e8
DJ
2141
2142 if (debug_threads)
1b3f6016 2143 fprintf (stderr, "Resuming lwp %ld (%s, signal %d, stop %s)\n",
bd99dc85 2144 lwpid_of (lwp), step ? "step" : "continue", signal,
54a0b537 2145 lwp->stop_expected ? "expected" : "not expected");
0d62e5e8
DJ
2146
2147 /* This bit needs some thinking about. If we get a signal that
2148 we must report while a single-step reinsert is still pending,
2149 we often end up resuming the thread. It might be better to
2150 (ew) allow a stack of pending events; then we could be sure that
2151 the reinsert happened right away and not lose any signals.
2152
2153 Making this stack would also shrink the window in which breakpoints are
54a0b537 2154 uninserted (see comment in linux_wait_for_lwp) but not enough for
0d62e5e8
DJ
2155 complete correctness, so it won't solve that problem. It may be
2156 worthwhile just to solve this one, however. */
54a0b537 2157 if (lwp->bp_reinsert != 0)
0d62e5e8
DJ
2158 {
2159 if (debug_threads)
d50171e4
PA
2160 fprintf (stderr, " pending reinsert at 0x%s\n",
2161 paddress (lwp->bp_reinsert));
2162
2163 if (lwp->bp_reinsert != 0 && can_hardware_single_step ())
2164 {
2165 if (step == 0)
2166 fprintf (stderr, "BAD - reinserting but not stepping.\n");
2167
2168 step = 1;
2169 }
0d62e5e8
DJ
2170
2171 /* Postpone any pending signal. It was enqueued above. */
2172 signal = 0;
2173 }
2174
aa691b87 2175 if (debug_threads && the_low_target.get_pc != NULL)
0d62e5e8 2176 {
442ea881
PA
2177 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
2178 CORE_ADDR pc = (*the_low_target.get_pc) (regcache);
47c0c975 2179 fprintf (stderr, " resuming from pc 0x%lx\n", (long) pc);
0d62e5e8
DJ
2180 }
2181
2182 /* If we have pending signals, consume one unless we are trying to reinsert
2183 a breakpoint. */
54a0b537 2184 if (lwp->pending_signals != NULL && lwp->bp_reinsert == 0)
0d62e5e8
DJ
2185 {
2186 struct pending_signals **p_sig;
2187
54a0b537 2188 p_sig = &lwp->pending_signals;
0d62e5e8
DJ
2189 while ((*p_sig)->prev != NULL)
2190 p_sig = &(*p_sig)->prev;
2191
2192 signal = (*p_sig)->signal;
32ca6d61 2193 if ((*p_sig)->info.si_signo != 0)
bd99dc85 2194 ptrace (PTRACE_SETSIGINFO, lwpid_of (lwp), 0, &(*p_sig)->info);
32ca6d61 2195
0d62e5e8
DJ
2196 free (*p_sig);
2197 *p_sig = NULL;
2198 }
2199
aa5ca48f
DE
2200 if (the_low_target.prepare_to_resume != NULL)
2201 the_low_target.prepare_to_resume (lwp);
2202
0d62e5e8 2203 regcache_invalidate_one ((struct inferior_list_entry *)
54a0b537 2204 get_lwp_thread (lwp));
da6d8c04 2205 errno = 0;
54a0b537 2206 lwp->stopped = 0;
c3adc08c 2207 lwp->stopped_by_watchpoint = 0;
54a0b537 2208 lwp->stepping = step;
14ce3065
DE
2209 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, lwpid_of (lwp), 0,
2210 /* Coerce to a uintptr_t first to avoid potential gcc warning
2211 of coercing an 8 byte integer to a 4 byte pointer. */
2212 (PTRACE_ARG4_TYPE) (uintptr_t) signal);
0d62e5e8
DJ
2213
2214 current_inferior = saved_inferior;
da6d8c04 2215 if (errno)
3221518c
UW
2216 {
2217 /* ESRCH from ptrace either means that the thread was already
2218 running (an error) or that it is gone (a race condition). If
2219 it's gone, we will get a notification the next time we wait,
2220 so we can ignore the error. We could differentiate these
2221 two, but it's tricky without waiting; the thread still exists
2222 as a zombie, so sending it signal 0 would succeed. So just
2223 ignore ESRCH. */
2224 if (errno == ESRCH)
2225 return;
2226
2227 perror_with_name ("ptrace");
2228 }
da6d8c04
DJ
2229}
2230
2bd7c093
PA
2231struct thread_resume_array
2232{
2233 struct thread_resume *resume;
2234 size_t n;
2235};
64386c31
DJ
2236
2237/* This function is called once per thread. We look up the thread
5544ad89
DJ
2238 in RESUME_PTR, and mark the thread with a pointer to the appropriate
2239 resume request.
2240
2241 This algorithm is O(threads * resume elements), but resume elements
2242 is small (and will remain small at least until GDB supports thread
2243 suspension). */
2bd7c093
PA
2244static int
2245linux_set_resume_request (struct inferior_list_entry *entry, void *arg)
0d62e5e8 2246{
54a0b537 2247 struct lwp_info *lwp;
64386c31 2248 struct thread_info *thread;
5544ad89 2249 int ndx;
2bd7c093 2250 struct thread_resume_array *r;
64386c31
DJ
2251
2252 thread = (struct thread_info *) entry;
54a0b537 2253 lwp = get_thread_lwp (thread);
2bd7c093 2254 r = arg;
64386c31 2255
2bd7c093 2256 for (ndx = 0; ndx < r->n; ndx++)
95954743
PA
2257 {
2258 ptid_t ptid = r->resume[ndx].thread;
2259 if (ptid_equal (ptid, minus_one_ptid)
2260 || ptid_equal (ptid, entry->id)
2261 || (ptid_is_pid (ptid)
2262 && (ptid_get_pid (ptid) == pid_of (lwp)))
2263 || (ptid_get_lwp (ptid) == -1
2264 && (ptid_get_pid (ptid) == pid_of (lwp))))
2265 {
d50171e4
PA
2266 if (r->resume[ndx].kind == resume_stop
2267 && lwp->last_resume_kind == resume_stop)
2268 {
2269 if (debug_threads)
2270 fprintf (stderr, "already %s LWP %ld at GDB's request\n",
2271 thread->last_status.kind == TARGET_WAITKIND_STOPPED
2272 ? "stopped"
2273 : "stopping",
2274 lwpid_of (lwp));
2275
2276 continue;
2277 }
2278
95954743 2279 lwp->resume = &r->resume[ndx];
d50171e4 2280 lwp->last_resume_kind = lwp->resume->kind;
95954743
PA
2281 return 0;
2282 }
2283 }
2bd7c093
PA
2284
2285 /* No resume action for this thread. */
2286 lwp->resume = NULL;
64386c31 2287
2bd7c093 2288 return 0;
5544ad89
DJ
2289}
2290
5544ad89 2291
bd99dc85
PA
2292/* Set *FLAG_P if this lwp has an interesting status pending. */
2293static int
2294resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
5544ad89 2295{
bd99dc85 2296 struct lwp_info *lwp = (struct lwp_info *) entry;
5544ad89 2297
bd99dc85
PA
2298 /* LWPs which will not be resumed are not interesting, because
2299 we might not wait for them next time through linux_wait. */
2bd7c093 2300 if (lwp->resume == NULL)
bd99dc85 2301 return 0;
64386c31 2302
bd99dc85 2303 if (lwp->status_pending_p)
d50171e4
PA
2304 * (int *) flag_p = 1;
2305
2306 return 0;
2307}
2308
2309/* Return 1 if this lwp that GDB wants running is stopped at an
2310 internal breakpoint that we need to step over. It assumes that any
2311 required STOP_PC adjustment has already been propagated to the
2312 inferior's regcache. */
2313
2314static int
2315need_step_over_p (struct inferior_list_entry *entry, void *dummy)
2316{
2317 struct lwp_info *lwp = (struct lwp_info *) entry;
2318 struct thread_info *saved_inferior;
2319 CORE_ADDR pc;
2320
2321 /* LWPs which will not be resumed are not interesting, because we
2322 might not wait for them next time through linux_wait. */
2323
2324 if (!lwp->stopped)
2325 {
2326 if (debug_threads)
2327 fprintf (stderr,
2328 "Need step over [LWP %ld]? Ignoring, not stopped\n",
2329 lwpid_of (lwp));
2330 return 0;
2331 }
2332
2333 if (lwp->last_resume_kind == resume_stop)
2334 {
2335 if (debug_threads)
2336 fprintf (stderr,
2337 "Need step over [LWP %ld]? Ignoring, should remain stopped\n",
2338 lwpid_of (lwp));
2339 return 0;
2340 }
2341
2342 if (!lwp->need_step_over)
2343 {
2344 if (debug_threads)
2345 fprintf (stderr,
2346 "Need step over [LWP %ld]? No\n", lwpid_of (lwp));
2347 }
5544ad89 2348
bd99dc85 2349 if (lwp->status_pending_p)
d50171e4
PA
2350 {
2351 if (debug_threads)
2352 fprintf (stderr,
2353 "Need step over [LWP %ld]? Ignoring, has pending status.\n",
2354 lwpid_of (lwp));
2355 return 0;
2356 }
2357
2358 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
2359 or we have. */
2360 pc = get_pc (lwp);
2361
2362 /* If the PC has changed since we stopped, then don't do anything,
2363 and let the breakpoint/tracepoint be hit. This happens if, for
2364 instance, GDB handled the decr_pc_after_break subtraction itself,
2365 GDB is OOL stepping this thread, or the user has issued a "jump"
2366 command, or poked thread's registers herself. */
2367 if (pc != lwp->stop_pc)
2368 {
2369 if (debug_threads)
2370 fprintf (stderr,
2371 "Need step over [LWP %ld]? Cancelling, PC was changed. "
2372 "Old stop_pc was 0x%s, PC is now 0x%s\n",
2373 lwpid_of (lwp), paddress (lwp->stop_pc), paddress (pc));
2374
2375 lwp->need_step_over = 0;
2376 return 0;
2377 }
2378
2379 saved_inferior = current_inferior;
2380 current_inferior = get_lwp_thread (lwp);
2381
2382 /* We only step over our breakpoints. */
2383 if (breakpoint_here (pc))
2384 {
2385 if (debug_threads)
2386 fprintf (stderr,
2387 "Need step over [LWP %ld]? yes, found breakpoint at 0x%s\n",
2388 lwpid_of (lwp), paddress (pc));
2389
2390 /* We've found an lwp that needs stepping over --- return 1 so
2391 that find_inferior stops looking. */
2392 current_inferior = saved_inferior;
2393
2394 /* If the step over is cancelled, this is set again. */
2395 lwp->need_step_over = 0;
2396 return 1;
2397 }
2398
2399 current_inferior = saved_inferior;
2400
2401 if (debug_threads)
2402 fprintf (stderr,
2403 "Need step over [LWP %ld]? No, no breakpoint found at 0x%s\n",
2404 lwpid_of (lwp), paddress (pc));
c6ecbae5 2405
bd99dc85 2406 return 0;
5544ad89
DJ
2407}
2408
d50171e4
PA
2409/* Start a step-over operation on LWP. When LWP stopped at a
2410 breakpoint, to make progress, we need to remove the breakpoint out
2411 of the way. If we let other threads run while we do that, they may
2412 pass by the breakpoint location and miss hitting it. To avoid
2413 that, a step-over momentarily stops all threads while LWP is
2414 single-stepped while the breakpoint is temporarily uninserted from
2415 the inferior. When the single-step finishes, we reinsert the
2416 breakpoint, and let all threads that are supposed to be running,
2417 run again.
2418
2419 On targets that don't support hardware single-step, we don't
2420 currently support full software single-stepping. Instead, we only
2421 support stepping over the thread event breakpoint, by asking the
2422 low target where to place a reinsert breakpoint. Since this
2423 routine assumes the breakpoint being stepped over is a thread event
2424 breakpoint, it usually assumes the return address of the current
2425 function is a good enough place to set the reinsert breakpoint. */
2426
2427static int
2428start_step_over (struct lwp_info *lwp)
2429{
2430 struct thread_info *saved_inferior;
2431 CORE_ADDR pc;
2432 int step;
2433
2434 if (debug_threads)
2435 fprintf (stderr,
2436 "Starting step-over on LWP %ld. Stopping all threads\n",
2437 lwpid_of (lwp));
2438
2439 stop_all_lwps ();
2440
2441 if (debug_threads)
2442 fprintf (stderr, "Done stopping all threads for step-over.\n");
2443
2444 /* Note, we should always reach here with an already adjusted PC,
2445 either by GDB (if we're resuming due to GDB's request), or by our
2446 caller, if we just finished handling an internal breakpoint GDB
2447 shouldn't care about. */
2448 pc = get_pc (lwp);
2449
2450 saved_inferior = current_inferior;
2451 current_inferior = get_lwp_thread (lwp);
2452
2453 lwp->bp_reinsert = pc;
2454 uninsert_breakpoints_at (pc);
2455
2456 if (can_hardware_single_step ())
2457 {
2458 step = 1;
2459 }
2460 else
2461 {
2462 CORE_ADDR raddr = (*the_low_target.breakpoint_reinsert_addr) ();
2463 set_reinsert_breakpoint (raddr);
2464 step = 0;
2465 }
2466
2467 current_inferior = saved_inferior;
2468
2469 linux_resume_one_lwp (lwp, step, 0, NULL);
2470
2471 /* Require next event from this LWP. */
2472 step_over_bkpt = lwp->head.id;
2473 return 1;
2474}
2475
2476/* Finish a step-over. Reinsert the breakpoint we had uninserted in
2477 start_step_over, if still there, and delete any reinsert
2478 breakpoints we've set, on non hardware single-step targets. */
2479
2480static int
2481finish_step_over (struct lwp_info *lwp)
2482{
2483 if (lwp->bp_reinsert != 0)
2484 {
2485 if (debug_threads)
2486 fprintf (stderr, "Finished step over.\n");
2487
2488 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
2489 may be no breakpoint to reinsert there by now. */
2490 reinsert_breakpoints_at (lwp->bp_reinsert);
2491
2492 lwp->bp_reinsert = 0;
2493
2494 /* Delete any software-single-step reinsert breakpoints. No
2495 longer needed. We don't have to worry about other threads
2496 hitting this trap, and later not being able to explain it,
2497 because we were stepping over a breakpoint, and we hold all
2498 threads but LWP stopped while doing that. */
2499 if (!can_hardware_single_step ())
2500 delete_reinsert_breakpoints ();
2501
2502 step_over_bkpt = null_ptid;
2503 return 1;
2504 }
2505 else
2506 return 0;
2507}
2508
5544ad89
DJ
2509/* This function is called once per thread. We check the thread's resume
2510 request, which will tell us whether to resume, step, or leave the thread
bd99dc85 2511 stopped; and what signal, if any, it should be sent.
5544ad89 2512
bd99dc85
PA
2513 For threads which we aren't explicitly told otherwise, we preserve
2514 the stepping flag; this is used for stepping over gdbserver-placed
2515 breakpoints.
2516
2517 If pending_flags was set in any thread, we queue any needed
2518 signals, since we won't actually resume. We already have a pending
2519 event to report, so we don't need to preserve any step requests;
2520 they should be re-issued if necessary. */
2521
2522static int
2523linux_resume_one_thread (struct inferior_list_entry *entry, void *arg)
5544ad89 2524{
54a0b537 2525 struct lwp_info *lwp;
5544ad89 2526 struct thread_info *thread;
bd99dc85 2527 int step;
d50171e4
PA
2528 int leave_all_stopped = * (int *) arg;
2529 int leave_pending;
5544ad89
DJ
2530
2531 thread = (struct thread_info *) entry;
54a0b537 2532 lwp = get_thread_lwp (thread);
5544ad89 2533
2bd7c093 2534 if (lwp->resume == NULL)
bd99dc85 2535 return 0;
5544ad89 2536
bd99dc85 2537 if (lwp->resume->kind == resume_stop)
5544ad89 2538 {
bd99dc85 2539 if (debug_threads)
d50171e4 2540 fprintf (stderr, "resume_stop request for LWP %ld\n", lwpid_of (lwp));
bd99dc85
PA
2541
2542 if (!lwp->stopped)
2543 {
2544 if (debug_threads)
d50171e4 2545 fprintf (stderr, "stopping LWP %ld\n", lwpid_of (lwp));
bd99dc85 2546
d50171e4
PA
2547 /* Stop the thread, and wait for the event asynchronously,
2548 through the event loop. */
bd99dc85
PA
2549 send_sigstop (&lwp->head);
2550 }
2551 else
2552 {
2553 if (debug_threads)
d50171e4
PA
2554 fprintf (stderr, "already stopped LWP %ld\n",
2555 lwpid_of (lwp));
2556
2557 /* The LWP may have been stopped in an internal event that
2558 was not meant to be notified back to GDB (e.g., gdbserver
2559 breakpoint), so we should be reporting a stop event in
2560 this case too. */
2561
2562 /* If the thread already has a pending SIGSTOP, this is a
2563 no-op. Otherwise, something later will presumably resume
2564 the thread and this will cause it to cancel any pending
2565 operation, due to last_resume_kind == resume_stop. If
2566 the thread already has a pending status to report, we
2567 will still report it the next time we wait - see
2568 status_pending_p_callback. */
2569 send_sigstop (&lwp->head);
bd99dc85 2570 }
32ca6d61 2571
bd99dc85
PA
2572 /* For stop requests, we're done. */
2573 lwp->resume = NULL;
d50171e4 2574 get_lwp_thread (lwp)->last_status.kind = TARGET_WAITKIND_IGNORE;
bd99dc85 2575 return 0;
5544ad89
DJ
2576 }
2577
bd99dc85
PA
2578 /* If this thread which is about to be resumed has a pending status,
2579 then don't resume any threads - we can just report the pending
2580 status. Make sure to queue any signals that would otherwise be
2581 sent. In all-stop mode, we do this decision based on if *any*
d50171e4
PA
2582 thread has a pending status. If there's a thread that needs the
2583 step-over-breakpoint dance, then don't resume any other thread
2584 but that particular one. */
2585 leave_pending = (lwp->status_pending_p || leave_all_stopped);
5544ad89 2586
d50171e4 2587 if (!leave_pending)
bd99dc85
PA
2588 {
2589 if (debug_threads)
2590 fprintf (stderr, "resuming LWP %ld\n", lwpid_of (lwp));
5544ad89 2591
d50171e4 2592 step = (lwp->resume->kind == resume_step);
2acc282a 2593 linux_resume_one_lwp (lwp, step, lwp->resume->sig, NULL);
d50171e4 2594 get_lwp_thread (lwp)->last_status.kind = TARGET_WAITKIND_IGNORE;
bd99dc85
PA
2595 }
2596 else
2597 {
2598 if (debug_threads)
2599 fprintf (stderr, "leaving LWP %ld stopped\n", lwpid_of (lwp));
5544ad89 2600
bd99dc85
PA
2601 /* If we have a new signal, enqueue the signal. */
2602 if (lwp->resume->sig != 0)
2603 {
2604 struct pending_signals *p_sig;
2605 p_sig = xmalloc (sizeof (*p_sig));
2606 p_sig->prev = lwp->pending_signals;
2607 p_sig->signal = lwp->resume->sig;
2608 memset (&p_sig->info, 0, sizeof (siginfo_t));
2609
2610 /* If this is the same signal we were previously stopped by,
2611 make sure to queue its siginfo. We can ignore the return
2612 value of ptrace; if it fails, we'll skip
2613 PTRACE_SETSIGINFO. */
2614 if (WIFSTOPPED (lwp->last_status)
2615 && WSTOPSIG (lwp->last_status) == lwp->resume->sig)
2616 ptrace (PTRACE_GETSIGINFO, lwpid_of (lwp), 0, &p_sig->info);
2617
2618 lwp->pending_signals = p_sig;
2619 }
2620 }
5544ad89 2621
bd99dc85 2622 lwp->resume = NULL;
5544ad89 2623 return 0;
0d62e5e8
DJ
2624}
2625
2626static void
2bd7c093 2627linux_resume (struct thread_resume *resume_info, size_t n)
0d62e5e8 2628{
2bd7c093 2629 struct thread_resume_array array = { resume_info, n };
d50171e4
PA
2630 struct lwp_info *need_step_over = NULL;
2631 int any_pending;
2632 int leave_all_stopped;
c6ecbae5 2633
2bd7c093 2634 find_inferior (&all_threads, linux_set_resume_request, &array);
5544ad89 2635
d50171e4
PA
2636 /* If there is a thread which would otherwise be resumed, which has
2637 a pending status, then don't resume any threads - we can just
2638 report the pending status. Make sure to queue any signals that
2639 would otherwise be sent. In non-stop mode, we'll apply this
2640 logic to each thread individually. We consume all pending events
2641 before considering to start a step-over (in all-stop). */
2642 any_pending = 0;
bd99dc85 2643 if (!non_stop)
d50171e4
PA
2644 find_inferior (&all_lwps, resume_status_pending_p, &any_pending);
2645
2646 /* If there is a thread which would otherwise be resumed, which is
2647 stopped at a breakpoint that needs stepping over, then don't
2648 resume any threads - have it step over the breakpoint with all
2649 other threads stopped, then resume all threads again. Make sure
2650 to queue any signals that would otherwise be delivered or
2651 queued. */
2652 if (!any_pending && supports_breakpoints ())
2653 need_step_over
2654 = (struct lwp_info *) find_inferior (&all_lwps,
2655 need_step_over_p, NULL);
2656
2657 leave_all_stopped = (need_step_over != NULL || any_pending);
2658
2659 if (debug_threads)
2660 {
2661 if (need_step_over != NULL)
2662 fprintf (stderr, "Not resuming all, need step over\n");
2663 else if (any_pending)
2664 fprintf (stderr,
2665 "Not resuming, all-stop and found "
2666 "an LWP with pending status\n");
2667 else
2668 fprintf (stderr, "Resuming, no pending status or step over needed\n");
2669 }
2670
2671 /* Even if we're leaving threads stopped, queue all signals we'd
2672 otherwise deliver. */
2673 find_inferior (&all_threads, linux_resume_one_thread, &leave_all_stopped);
2674
2675 if (need_step_over)
2676 start_step_over (need_step_over);
2677}
2678
2679/* This function is called once per thread. We check the thread's
2680 last resume request, which will tell us whether to resume, step, or
2681 leave the thread stopped. Any signal the client requested to be
2682 delivered has already been enqueued at this point.
2683
2684 If any thread that GDB wants running is stopped at an internal
2685 breakpoint that needs stepping over, we start a step-over operation
2686 on that particular thread, and leave all others stopped. */
2687
2688static void
2689proceed_one_lwp (struct inferior_list_entry *entry)
2690{
2691 struct lwp_info *lwp;
2692 int step;
2693
2694 lwp = (struct lwp_info *) entry;
2695
2696 if (debug_threads)
2697 fprintf (stderr,
2698 "proceed_one_lwp: lwp %ld\n", lwpid_of (lwp));
2699
2700 if (!lwp->stopped)
2701 {
2702 if (debug_threads)
2703 fprintf (stderr, " LWP %ld already running\n", lwpid_of (lwp));
2704 return;
2705 }
2706
2707 if (lwp->last_resume_kind == resume_stop)
2708 {
2709 if (debug_threads)
2710 fprintf (stderr, " client wants LWP %ld stopped\n", lwpid_of (lwp));
2711 return;
2712 }
2713
2714 if (lwp->status_pending_p)
2715 {
2716 if (debug_threads)
2717 fprintf (stderr, " LWP %ld has pending status, leaving stopped\n",
2718 lwpid_of (lwp));
2719 return;
2720 }
2721
2722 if (lwp->suspended)
2723 {
2724 if (debug_threads)
2725 fprintf (stderr, " LWP %ld is suspended\n", lwpid_of (lwp));
2726 return;
2727 }
2728
2729 step = lwp->last_resume_kind == resume_step;
2730 linux_resume_one_lwp (lwp, step, 0, NULL);
2731}
2732
2733/* When we finish a step-over, set threads running again. If there's
2734 another thread that may need a step-over, now's the time to start
2735 it. Eventually, we'll move all threads past their breakpoints. */
2736
2737static void
2738proceed_all_lwps (void)
2739{
2740 struct lwp_info *need_step_over;
2741
2742 /* If there is a thread which would otherwise be resumed, which is
2743 stopped at a breakpoint that needs stepping over, then don't
2744 resume any threads - have it step over the breakpoint with all
2745 other threads stopped, then resume all threads again. */
2746
2747 if (supports_breakpoints ())
2748 {
2749 need_step_over
2750 = (struct lwp_info *) find_inferior (&all_lwps,
2751 need_step_over_p, NULL);
2752
2753 if (need_step_over != NULL)
2754 {
2755 if (debug_threads)
2756 fprintf (stderr, "proceed_all_lwps: found "
2757 "thread %ld needing a step-over\n",
2758 lwpid_of (need_step_over));
2759
2760 start_step_over (need_step_over);
2761 return;
2762 }
2763 }
5544ad89 2764
d50171e4
PA
2765 if (debug_threads)
2766 fprintf (stderr, "Proceeding, no step-over needed\n");
2767
2768 for_each_inferior (&all_lwps, proceed_one_lwp);
2769}
2770
2771/* Stopped LWPs that the client wanted to be running, that don't have
2772 pending statuses, are set to run again, except for EXCEPT, if not
2773 NULL. This undoes a stop_all_lwps call. */
2774
2775static void
2776unstop_all_lwps (struct lwp_info *except)
2777{
5544ad89
DJ
2778 if (debug_threads)
2779 {
d50171e4
PA
2780 if (except)
2781 fprintf (stderr,
2782 "unstopping all lwps, except=(LWP %ld)\n", lwpid_of (except));
5544ad89 2783 else
d50171e4
PA
2784 fprintf (stderr,
2785 "unstopping all lwps\n");
5544ad89
DJ
2786 }
2787
d50171e4
PA
2788 /* Make sure proceed_one_lwp doesn't try to resume this thread. */
2789 if (except != NULL)
2790 ++except->suspended;
2791
2792 for_each_inferior (&all_lwps, proceed_one_lwp);
2793
2794 if (except != NULL)
2795 --except->suspended;
0d62e5e8
DJ
2796}
2797
2798#ifdef HAVE_LINUX_USRREGS
da6d8c04
DJ
2799
2800int
0a30fbc4 2801register_addr (int regnum)
da6d8c04
DJ
2802{
2803 int addr;
2804
2ec06d2e 2805 if (regnum < 0 || regnum >= the_low_target.num_regs)
da6d8c04
DJ
2806 error ("Invalid register number %d.", regnum);
2807
2ec06d2e 2808 addr = the_low_target.regmap[regnum];
da6d8c04
DJ
2809
2810 return addr;
2811}
2812
58caa3dc 2813/* Fetch one register. */
da6d8c04 2814static void
442ea881 2815fetch_register (struct regcache *regcache, int regno)
da6d8c04
DJ
2816{
2817 CORE_ADDR regaddr;
48d93c75 2818 int i, size;
0d62e5e8 2819 char *buf;
95954743 2820 int pid;
da6d8c04 2821
2ec06d2e 2822 if (regno >= the_low_target.num_regs)
0a30fbc4 2823 return;
2ec06d2e 2824 if ((*the_low_target.cannot_fetch_register) (regno))
0a30fbc4 2825 return;
da6d8c04 2826
0a30fbc4
DJ
2827 regaddr = register_addr (regno);
2828 if (regaddr == -1)
2829 return;
95954743
PA
2830
2831 pid = lwpid_of (get_thread_lwp (current_inferior));
1b3f6016
PA
2832 size = ((register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
2833 & - sizeof (PTRACE_XFER_TYPE));
48d93c75
UW
2834 buf = alloca (size);
2835 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04
DJ
2836 {
2837 errno = 0;
0d62e5e8 2838 *(PTRACE_XFER_TYPE *) (buf + i) =
14ce3065
DE
2839 ptrace (PTRACE_PEEKUSER, pid,
2840 /* Coerce to a uintptr_t first to avoid potential gcc warning
2841 of coercing an 8 byte integer to a 4 byte pointer. */
2842 (PTRACE_ARG3_TYPE) (uintptr_t) regaddr, 0);
da6d8c04
DJ
2843 regaddr += sizeof (PTRACE_XFER_TYPE);
2844 if (errno != 0)
f52cd8cd 2845 error ("reading register %d: %s", regno, strerror (errno));
da6d8c04 2846 }
ee1a7ae4
UW
2847
2848 if (the_low_target.supply_ptrace_register)
442ea881 2849 the_low_target.supply_ptrace_register (regcache, regno, buf);
5a1f5858 2850 else
442ea881 2851 supply_register (regcache, regno, buf);
da6d8c04
DJ
2852}
2853
2854/* Fetch all registers, or just one, from the child process. */
58caa3dc 2855static void
442ea881 2856usr_fetch_inferior_registers (struct regcache *regcache, int regno)
da6d8c04 2857{
4463ce24 2858 if (regno == -1)
2ec06d2e 2859 for (regno = 0; regno < the_low_target.num_regs; regno++)
442ea881 2860 fetch_register (regcache, regno);
da6d8c04 2861 else
442ea881 2862 fetch_register (regcache, regno);
da6d8c04
DJ
2863}
2864
2865/* Store our register values back into the inferior.
2866 If REGNO is -1, do this for all registers.
2867 Otherwise, REGNO specifies which register (so we can save time). */
58caa3dc 2868static void
442ea881 2869usr_store_inferior_registers (struct regcache *regcache, int regno)
da6d8c04
DJ
2870{
2871 CORE_ADDR regaddr;
48d93c75 2872 int i, size;
0d62e5e8 2873 char *buf;
55ac2b99 2874 int pid;
da6d8c04
DJ
2875
2876 if (regno >= 0)
2877 {
2ec06d2e 2878 if (regno >= the_low_target.num_regs)
0a30fbc4
DJ
2879 return;
2880
bc1e36ca 2881 if ((*the_low_target.cannot_store_register) (regno) == 1)
0a30fbc4
DJ
2882 return;
2883
2884 regaddr = register_addr (regno);
2885 if (regaddr == -1)
da6d8c04 2886 return;
da6d8c04 2887 errno = 0;
48d93c75
UW
2888 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
2889 & - sizeof (PTRACE_XFER_TYPE);
2890 buf = alloca (size);
2891 memset (buf, 0, size);
ee1a7ae4
UW
2892
2893 if (the_low_target.collect_ptrace_register)
442ea881 2894 the_low_target.collect_ptrace_register (regcache, regno, buf);
5a1f5858 2895 else
442ea881 2896 collect_register (regcache, regno, buf);
ee1a7ae4 2897
95954743 2898 pid = lwpid_of (get_thread_lwp (current_inferior));
48d93c75 2899 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04 2900 {
0a30fbc4 2901 errno = 0;
14ce3065
DE
2902 ptrace (PTRACE_POKEUSER, pid,
2903 /* Coerce to a uintptr_t first to avoid potential gcc warning
2904 about coercing an 8 byte integer to a 4 byte pointer. */
2905 (PTRACE_ARG3_TYPE) (uintptr_t) regaddr,
2906 (PTRACE_ARG4_TYPE) *(PTRACE_XFER_TYPE *) (buf + i));
da6d8c04
DJ
2907 if (errno != 0)
2908 {
1b3f6016
PA
2909 /* At this point, ESRCH should mean the process is
2910 already gone, in which case we simply ignore attempts
2911 to change its registers. See also the related
2912 comment in linux_resume_one_lwp. */
3221518c
UW
2913 if (errno == ESRCH)
2914 return;
2915
bc1e36ca 2916 if ((*the_low_target.cannot_store_register) (regno) == 0)
f52cd8cd 2917 error ("writing register %d: %s", regno, strerror (errno));
da6d8c04 2918 }
2ff29de4 2919 regaddr += sizeof (PTRACE_XFER_TYPE);
da6d8c04 2920 }
da6d8c04
DJ
2921 }
2922 else
2ec06d2e 2923 for (regno = 0; regno < the_low_target.num_regs; regno++)
442ea881 2924 usr_store_inferior_registers (regcache, regno);
da6d8c04 2925}
58caa3dc
DJ
2926#endif /* HAVE_LINUX_USRREGS */
2927
2928
2929
2930#ifdef HAVE_LINUX_REGSETS
2931
2932static int
442ea881 2933regsets_fetch_inferior_registers (struct regcache *regcache)
58caa3dc
DJ
2934{
2935 struct regset_info *regset;
e9d25b98 2936 int saw_general_regs = 0;
95954743 2937 int pid;
58caa3dc
DJ
2938
2939 regset = target_regsets;
2940
95954743 2941 pid = lwpid_of (get_thread_lwp (current_inferior));
58caa3dc
DJ
2942 while (regset->size >= 0)
2943 {
2944 void *buf;
2945 int res;
2946
52fa2412 2947 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
58caa3dc
DJ
2948 {
2949 regset ++;
2950 continue;
2951 }
2952
bca929d3 2953 buf = xmalloc (regset->size);
dfb64f85 2954#ifndef __sparc__
95954743 2955 res = ptrace (regset->get_request, pid, 0, buf);
dfb64f85 2956#else
95954743 2957 res = ptrace (regset->get_request, pid, buf, 0);
dfb64f85 2958#endif
58caa3dc
DJ
2959 if (res < 0)
2960 {
2961 if (errno == EIO)
2962 {
52fa2412
UW
2963 /* If we get EIO on a regset, do not try it again for
2964 this process. */
2965 disabled_regsets[regset - target_regsets] = 1;
fdeb2a12 2966 free (buf);
52fa2412 2967 continue;
58caa3dc
DJ
2968 }
2969 else
2970 {
0d62e5e8 2971 char s[256];
95954743
PA
2972 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
2973 pid);
0d62e5e8 2974 perror (s);
58caa3dc
DJ
2975 }
2976 }
e9d25b98
DJ
2977 else if (regset->type == GENERAL_REGS)
2978 saw_general_regs = 1;
442ea881 2979 regset->store_function (regcache, buf);
58caa3dc 2980 regset ++;
fdeb2a12 2981 free (buf);
58caa3dc 2982 }
e9d25b98
DJ
2983 if (saw_general_regs)
2984 return 0;
2985 else
2986 return 1;
58caa3dc
DJ
2987}
2988
2989static int
442ea881 2990regsets_store_inferior_registers (struct regcache *regcache)
58caa3dc
DJ
2991{
2992 struct regset_info *regset;
e9d25b98 2993 int saw_general_regs = 0;
95954743 2994 int pid;
58caa3dc
DJ
2995
2996 regset = target_regsets;
2997
95954743 2998 pid = lwpid_of (get_thread_lwp (current_inferior));
58caa3dc
DJ
2999 while (regset->size >= 0)
3000 {
3001 void *buf;
3002 int res;
3003
52fa2412 3004 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
58caa3dc
DJ
3005 {
3006 regset ++;
3007 continue;
3008 }
3009
bca929d3 3010 buf = xmalloc (regset->size);
545587ee
DJ
3011
3012 /* First fill the buffer with the current register set contents,
3013 in case there are any items in the kernel's regset that are
3014 not in gdbserver's regcache. */
dfb64f85 3015#ifndef __sparc__
95954743 3016 res = ptrace (regset->get_request, pid, 0, buf);
dfb64f85 3017#else
95954743 3018 res = ptrace (regset->get_request, pid, buf, 0);
dfb64f85 3019#endif
545587ee
DJ
3020
3021 if (res == 0)
3022 {
3023 /* Then overlay our cached registers on that. */
442ea881 3024 regset->fill_function (regcache, buf);
545587ee
DJ
3025
3026 /* Only now do we write the register set. */
dfb64f85 3027#ifndef __sparc__
95954743 3028 res = ptrace (regset->set_request, pid, 0, buf);
dfb64f85 3029#else
95954743 3030 res = ptrace (regset->set_request, pid, buf, 0);
dfb64f85 3031#endif
545587ee
DJ
3032 }
3033
58caa3dc
DJ
3034 if (res < 0)
3035 {
3036 if (errno == EIO)
3037 {
52fa2412
UW
3038 /* If we get EIO on a regset, do not try it again for
3039 this process. */
3040 disabled_regsets[regset - target_regsets] = 1;
fdeb2a12 3041 free (buf);
52fa2412 3042 continue;
58caa3dc 3043 }
3221518c
UW
3044 else if (errno == ESRCH)
3045 {
1b3f6016
PA
3046 /* At this point, ESRCH should mean the process is
3047 already gone, in which case we simply ignore attempts
3048 to change its registers. See also the related
3049 comment in linux_resume_one_lwp. */
fdeb2a12 3050 free (buf);
3221518c
UW
3051 return 0;
3052 }
58caa3dc
DJ
3053 else
3054 {
ce3a066d 3055 perror ("Warning: ptrace(regsets_store_inferior_registers)");
58caa3dc
DJ
3056 }
3057 }
e9d25b98
DJ
3058 else if (regset->type == GENERAL_REGS)
3059 saw_general_regs = 1;
58caa3dc 3060 regset ++;
09ec9b38 3061 free (buf);
58caa3dc 3062 }
e9d25b98
DJ
3063 if (saw_general_regs)
3064 return 0;
3065 else
3066 return 1;
ce3a066d 3067 return 0;
58caa3dc
DJ
3068}
3069
3070#endif /* HAVE_LINUX_REGSETS */
3071
3072
3073void
442ea881 3074linux_fetch_registers (struct regcache *regcache, int regno)
58caa3dc
DJ
3075{
3076#ifdef HAVE_LINUX_REGSETS
442ea881 3077 if (regsets_fetch_inferior_registers (regcache) == 0)
52fa2412 3078 return;
58caa3dc
DJ
3079#endif
3080#ifdef HAVE_LINUX_USRREGS
442ea881 3081 usr_fetch_inferior_registers (regcache, regno);
58caa3dc
DJ
3082#endif
3083}
3084
3085void
442ea881 3086linux_store_registers (struct regcache *regcache, int regno)
58caa3dc
DJ
3087{
3088#ifdef HAVE_LINUX_REGSETS
442ea881 3089 if (regsets_store_inferior_registers (regcache) == 0)
52fa2412 3090 return;
58caa3dc
DJ
3091#endif
3092#ifdef HAVE_LINUX_USRREGS
442ea881 3093 usr_store_inferior_registers (regcache, regno);
58caa3dc
DJ
3094#endif
3095}
3096
da6d8c04 3097
da6d8c04
DJ
3098/* Copy LEN bytes from inferior's memory starting at MEMADDR
3099 to debugger memory starting at MYADDR. */
3100
c3e735a6 3101static int
f450004a 3102linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
da6d8c04
DJ
3103{
3104 register int i;
3105 /* Round starting address down to longword boundary. */
3106 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
3107 /* Round ending address up; get number of longwords that makes. */
aa691b87
RM
3108 register int count
3109 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
da6d8c04
DJ
3110 / sizeof (PTRACE_XFER_TYPE);
3111 /* Allocate buffer of that many longwords. */
aa691b87 3112 register PTRACE_XFER_TYPE *buffer
da6d8c04 3113 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
fd462a61
DJ
3114 int fd;
3115 char filename[64];
95954743 3116 int pid = lwpid_of (get_thread_lwp (current_inferior));
fd462a61
DJ
3117
3118 /* Try using /proc. Don't bother for one word. */
3119 if (len >= 3 * sizeof (long))
3120 {
3121 /* We could keep this file open and cache it - possibly one per
3122 thread. That requires some juggling, but is even faster. */
95954743 3123 sprintf (filename, "/proc/%d/mem", pid);
fd462a61
DJ
3124 fd = open (filename, O_RDONLY | O_LARGEFILE);
3125 if (fd == -1)
3126 goto no_proc;
3127
3128 /* If pread64 is available, use it. It's faster if the kernel
3129 supports it (only one syscall), and it's 64-bit safe even on
3130 32-bit platforms (for instance, SPARC debugging a SPARC64
3131 application). */
3132#ifdef HAVE_PREAD64
3133 if (pread64 (fd, myaddr, len, memaddr) != len)
3134#else
1de1badb 3135 if (lseek (fd, memaddr, SEEK_SET) == -1 || read (fd, myaddr, len) != len)
fd462a61
DJ
3136#endif
3137 {
3138 close (fd);
3139 goto no_proc;
3140 }
3141
3142 close (fd);
3143 return 0;
3144 }
da6d8c04 3145
fd462a61 3146 no_proc:
da6d8c04
DJ
3147 /* Read all the longwords */
3148 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
3149 {
c3e735a6 3150 errno = 0;
14ce3065
DE
3151 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
3152 about coercing an 8 byte integer to a 4 byte pointer. */
3153 buffer[i] = ptrace (PTRACE_PEEKTEXT, pid,
3154 (PTRACE_ARG3_TYPE) (uintptr_t) addr, 0);
c3e735a6
DJ
3155 if (errno)
3156 return errno;
da6d8c04
DJ
3157 }
3158
3159 /* Copy appropriate bytes out of the buffer. */
1b3f6016
PA
3160 memcpy (myaddr,
3161 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
3162 len);
c3e735a6
DJ
3163
3164 return 0;
da6d8c04
DJ
3165}
3166
93ae6fdc
PA
3167/* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
3168 memory at MEMADDR. On failure (cannot write to the inferior)
da6d8c04
DJ
3169 returns the value of errno. */
3170
ce3a066d 3171static int
f450004a 3172linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
da6d8c04
DJ
3173{
3174 register int i;
3175 /* Round starting address down to longword boundary. */
3176 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
3177 /* Round ending address up; get number of longwords that makes. */
3178 register int count
3179 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
3180 /* Allocate buffer of that many longwords. */
3181 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
95954743 3182 int pid = lwpid_of (get_thread_lwp (current_inferior));
da6d8c04 3183
0d62e5e8
DJ
3184 if (debug_threads)
3185 {
58d6951d
DJ
3186 /* Dump up to four bytes. */
3187 unsigned int val = * (unsigned int *) myaddr;
3188 if (len == 1)
3189 val = val & 0xff;
3190 else if (len == 2)
3191 val = val & 0xffff;
3192 else if (len == 3)
3193 val = val & 0xffffff;
3194 fprintf (stderr, "Writing %0*x to 0x%08lx\n", 2 * ((len < 4) ? len : 4),
3195 val, (long)memaddr);
0d62e5e8
DJ
3196 }
3197
da6d8c04
DJ
3198 /* Fill start and end extra bytes of buffer with existing memory data. */
3199
93ae6fdc 3200 errno = 0;
14ce3065
DE
3201 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
3202 about coercing an 8 byte integer to a 4 byte pointer. */
3203 buffer[0] = ptrace (PTRACE_PEEKTEXT, pid,
3204 (PTRACE_ARG3_TYPE) (uintptr_t) addr, 0);
93ae6fdc
PA
3205 if (errno)
3206 return errno;
da6d8c04
DJ
3207
3208 if (count > 1)
3209 {
93ae6fdc 3210 errno = 0;
da6d8c04 3211 buffer[count - 1]
95954743 3212 = ptrace (PTRACE_PEEKTEXT, pid,
14ce3065
DE
3213 /* Coerce to a uintptr_t first to avoid potential gcc warning
3214 about coercing an 8 byte integer to a 4 byte pointer. */
3215 (PTRACE_ARG3_TYPE) (uintptr_t) (addr + (count - 1)
3216 * sizeof (PTRACE_XFER_TYPE)),
d844cde6 3217 0);
93ae6fdc
PA
3218 if (errno)
3219 return errno;
da6d8c04
DJ
3220 }
3221
93ae6fdc 3222 /* Copy data to be written over corresponding part of buffer. */
da6d8c04
DJ
3223
3224 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
3225
3226 /* Write the entire buffer. */
3227
3228 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
3229 {
3230 errno = 0;
14ce3065
DE
3231 ptrace (PTRACE_POKETEXT, pid,
3232 /* Coerce to a uintptr_t first to avoid potential gcc warning
3233 about coercing an 8 byte integer to a 4 byte pointer. */
3234 (PTRACE_ARG3_TYPE) (uintptr_t) addr,
3235 (PTRACE_ARG4_TYPE) buffer[i]);
da6d8c04
DJ
3236 if (errno)
3237 return errno;
3238 }
3239
3240 return 0;
3241}
2f2893d9 3242
6076632b 3243/* Non-zero if the kernel supports PTRACE_O_TRACEFORK. */
24a09b5f
DJ
3244static int linux_supports_tracefork_flag;
3245
51c2684e 3246/* Helper functions for linux_test_for_tracefork, called via clone (). */
24a09b5f 3247
51c2684e
DJ
3248static int
3249linux_tracefork_grandchild (void *arg)
3250{
3251 _exit (0);
3252}
3253
7407e2de
AS
3254#define STACK_SIZE 4096
3255
51c2684e
DJ
3256static int
3257linux_tracefork_child (void *arg)
24a09b5f
DJ
3258{
3259 ptrace (PTRACE_TRACEME, 0, 0, 0);
3260 kill (getpid (), SIGSTOP);
e4b7f41c
JK
3261
3262#if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
3263
3264 if (fork () == 0)
3265 linux_tracefork_grandchild (NULL);
3266
3267#else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3268
7407e2de
AS
3269#ifdef __ia64__
3270 __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE,
3271 CLONE_VM | SIGCHLD, NULL);
3272#else
3273 clone (linux_tracefork_grandchild, arg + STACK_SIZE,
3274 CLONE_VM | SIGCHLD, NULL);
3275#endif
e4b7f41c
JK
3276
3277#endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3278
24a09b5f
DJ
3279 _exit (0);
3280}
3281
24a09b5f
DJ
3282/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
3283 sure that we can enable the option, and that it had the desired
3284 effect. */
3285
3286static void
3287linux_test_for_tracefork (void)
3288{
3289 int child_pid, ret, status;
3290 long second_pid;
e4b7f41c 3291#if defined(__UCLIBC__) && defined(HAS_NOMMU)
bca929d3 3292 char *stack = xmalloc (STACK_SIZE * 4);
e4b7f41c 3293#endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
24a09b5f
DJ
3294
3295 linux_supports_tracefork_flag = 0;
3296
e4b7f41c
JK
3297#if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
3298
3299 child_pid = fork ();
3300 if (child_pid == 0)
3301 linux_tracefork_child (NULL);
3302
3303#else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3304
51c2684e 3305 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
7407e2de
AS
3306#ifdef __ia64__
3307 child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE,
3308 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
e4b7f41c 3309#else /* !__ia64__ */
7407e2de
AS
3310 child_pid = clone (linux_tracefork_child, stack + STACK_SIZE,
3311 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
e4b7f41c
JK
3312#endif /* !__ia64__ */
3313
3314#endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
3315
24a09b5f 3316 if (child_pid == -1)
51c2684e 3317 perror_with_name ("clone");
24a09b5f
DJ
3318
3319 ret = my_waitpid (child_pid, &status, 0);
3320 if (ret == -1)
3321 perror_with_name ("waitpid");
3322 else if (ret != child_pid)
3323 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
3324 if (! WIFSTOPPED (status))
3325 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
3326
14ce3065
DE
3327 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0,
3328 (PTRACE_ARG4_TYPE) PTRACE_O_TRACEFORK);
24a09b5f
DJ
3329 if (ret != 0)
3330 {
3331 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
3332 if (ret != 0)
3333 {
3334 warning ("linux_test_for_tracefork: failed to kill child");
3335 return;
3336 }
3337
3338 ret = my_waitpid (child_pid, &status, 0);
3339 if (ret != child_pid)
3340 warning ("linux_test_for_tracefork: failed to wait for killed child");
3341 else if (!WIFSIGNALED (status))
3342 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
3343 "killed child", status);
3344
3345 return;
3346 }
3347
3348 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
3349 if (ret != 0)
3350 warning ("linux_test_for_tracefork: failed to resume child");
3351
3352 ret = my_waitpid (child_pid, &status, 0);
3353
3354 if (ret == child_pid && WIFSTOPPED (status)
3355 && status >> 16 == PTRACE_EVENT_FORK)
3356 {
3357 second_pid = 0;
3358 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
3359 if (ret == 0 && second_pid != 0)
3360 {
3361 int second_status;
3362
3363 linux_supports_tracefork_flag = 1;
3364 my_waitpid (second_pid, &second_status, 0);
3365 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
3366 if (ret != 0)
3367 warning ("linux_test_for_tracefork: failed to kill second child");
3368 my_waitpid (second_pid, &status, 0);
3369 }
3370 }
3371 else
3372 warning ("linux_test_for_tracefork: unexpected result from waitpid "
3373 "(%d, status 0x%x)", ret, status);
3374
3375 do
3376 {
3377 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
3378 if (ret != 0)
3379 warning ("linux_test_for_tracefork: failed to kill child");
3380 my_waitpid (child_pid, &status, 0);
3381 }
3382 while (WIFSTOPPED (status));
51c2684e 3383
e4b7f41c 3384#if defined(__UCLIBC__) && defined(HAS_NOMMU)
51c2684e 3385 free (stack);
e4b7f41c 3386#endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
24a09b5f
DJ
3387}
3388
3389
2f2893d9
DJ
3390static void
3391linux_look_up_symbols (void)
3392{
0d62e5e8 3393#ifdef USE_THREAD_DB
95954743
PA
3394 struct process_info *proc = current_process ();
3395
cdbfd419 3396 if (proc->private->thread_db != NULL)
0d62e5e8
DJ
3397 return;
3398
6076632b
DE
3399 /* If the kernel supports tracing forks then it also supports tracing
3400 clones, and then we don't need to use the magic thread event breakpoint
3401 to learn about threads. */
cdbfd419 3402 thread_db_init (!linux_supports_tracefork_flag);
0d62e5e8
DJ
3403#endif
3404}
3405
e5379b03 3406static void
ef57601b 3407linux_request_interrupt (void)
e5379b03 3408{
a1928bad 3409 extern unsigned long signal_pid;
e5379b03 3410
95954743
PA
3411 if (!ptid_equal (cont_thread, null_ptid)
3412 && !ptid_equal (cont_thread, minus_one_ptid))
e5379b03 3413 {
54a0b537 3414 struct lwp_info *lwp;
bd99dc85 3415 int lwpid;
e5379b03 3416
54a0b537 3417 lwp = get_thread_lwp (current_inferior);
bd99dc85
PA
3418 lwpid = lwpid_of (lwp);
3419 kill_lwp (lwpid, SIGINT);
e5379b03
DJ
3420 }
3421 else
ef57601b 3422 kill_lwp (signal_pid, SIGINT);
e5379b03
DJ
3423}
3424
aa691b87
RM
3425/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
3426 to debugger memory starting at MYADDR. */
3427
3428static int
f450004a 3429linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
aa691b87
RM
3430{
3431 char filename[PATH_MAX];
3432 int fd, n;
95954743 3433 int pid = lwpid_of (get_thread_lwp (current_inferior));
aa691b87 3434
95954743 3435 snprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
aa691b87
RM
3436
3437 fd = open (filename, O_RDONLY);
3438 if (fd < 0)
3439 return -1;
3440
3441 if (offset != (CORE_ADDR) 0
3442 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
3443 n = -1;
3444 else
3445 n = read (fd, myaddr, len);
3446
3447 close (fd);
3448
3449 return n;
3450}
3451
d993e290
PA
3452/* These breakpoint and watchpoint related wrapper functions simply
3453 pass on the function call if the target has registered a
3454 corresponding function. */
e013ee27
OF
3455
3456static int
d993e290 3457linux_insert_point (char type, CORE_ADDR addr, int len)
e013ee27 3458{
d993e290
PA
3459 if (the_low_target.insert_point != NULL)
3460 return the_low_target.insert_point (type, addr, len);
e013ee27
OF
3461 else
3462 /* Unsupported (see target.h). */
3463 return 1;
3464}
3465
3466static int
d993e290 3467linux_remove_point (char type, CORE_ADDR addr, int len)
e013ee27 3468{
d993e290
PA
3469 if (the_low_target.remove_point != NULL)
3470 return the_low_target.remove_point (type, addr, len);
e013ee27
OF
3471 else
3472 /* Unsupported (see target.h). */
3473 return 1;
3474}
3475
3476static int
3477linux_stopped_by_watchpoint (void)
3478{
c3adc08c
PA
3479 struct lwp_info *lwp = get_thread_lwp (current_inferior);
3480
3481 return lwp->stopped_by_watchpoint;
e013ee27
OF
3482}
3483
3484static CORE_ADDR
3485linux_stopped_data_address (void)
3486{
c3adc08c
PA
3487 struct lwp_info *lwp = get_thread_lwp (current_inferior);
3488
3489 return lwp->stopped_data_address;
e013ee27
OF
3490}
3491
42c81e2a 3492#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
3493#if defined(__mcoldfire__)
3494/* These should really be defined in the kernel's ptrace.h header. */
3495#define PT_TEXT_ADDR 49*4
3496#define PT_DATA_ADDR 50*4
3497#define PT_TEXT_END_ADDR 51*4
3498#endif
3499
3500/* Under uClinux, programs are loaded at non-zero offsets, which we need
3501 to tell gdb about. */
3502
3503static int
3504linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
3505{
3506#if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
3507 unsigned long text, text_end, data;
bd99dc85 3508 int pid = lwpid_of (get_thread_lwp (current_inferior));
52fb6437
NS
3509
3510 errno = 0;
3511
3512 text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0);
3513 text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0);
3514 data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0);
3515
3516 if (errno == 0)
3517 {
3518 /* Both text and data offsets produced at compile-time (and so
1b3f6016
PA
3519 used by gdb) are relative to the beginning of the program,
3520 with the data segment immediately following the text segment.
3521 However, the actual runtime layout in memory may put the data
3522 somewhere else, so when we send gdb a data base-address, we
3523 use the real data base address and subtract the compile-time
3524 data base-address from it (which is just the length of the
3525 text segment). BSS immediately follows data in both
3526 cases. */
52fb6437
NS
3527 *text_p = text;
3528 *data_p = data - (text_end - text);
1b3f6016 3529
52fb6437
NS
3530 return 1;
3531 }
3532#endif
3533 return 0;
3534}
3535#endif
3536
dc146f7c
VP
3537static int
3538compare_ints (const void *xa, const void *xb)
3539{
3540 int a = *(const int *)xa;
3541 int b = *(const int *)xb;
3542
3543 return a - b;
3544}
3545
3546static int *
3547unique (int *b, int *e)
3548{
3549 int *d = b;
3550 while (++b != e)
3551 if (*d != *b)
3552 *++d = *b;
3553 return ++d;
3554}
3555
3556/* Given PID, iterates over all threads in that process.
3557
3558 Information about each thread, in a format suitable for qXfer:osdata:thread
3559 is printed to BUFFER, if it's not NULL. BUFFER is assumed to be already
3560 initialized, and the caller is responsible for finishing and appending '\0'
3561 to it.
3562
3563 The list of cores that threads are running on is assigned to *CORES, if it
3564 is not NULL. If no cores are found, *CORES will be set to NULL. Caller
3565 should free *CORES. */
3566
3567static void
3568list_threads (int pid, struct buffer *buffer, char **cores)
3569{
3570 int count = 0;
3571 int allocated = 10;
3572 int *core_numbers = xmalloc (sizeof (int) * allocated);
3573 char pathname[128];
3574 DIR *dir;
3575 struct dirent *dp;
3576 struct stat statbuf;
3577
3578 sprintf (pathname, "/proc/%d/task", pid);
3579 if (stat (pathname, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
3580 {
3581 dir = opendir (pathname);
3582 if (!dir)
3583 {
3584 free (core_numbers);
3585 return;
3586 }
3587
3588 while ((dp = readdir (dir)) != NULL)
3589 {
3590 unsigned long lwp = strtoul (dp->d_name, NULL, 10);
3591
3592 if (lwp != 0)
3593 {
3594 unsigned core = linux_core_of_thread (ptid_build (pid, lwp, 0));
3595
3596 if (core != -1)
3597 {
3598 char s[sizeof ("4294967295")];
3599 sprintf (s, "%u", core);
3600
3601 if (count == allocated)
3602 {
3603 allocated *= 2;
3604 core_numbers = realloc (core_numbers,
3605 sizeof (int) * allocated);
3606 }
3607 core_numbers[count++] = core;
3608 if (buffer)
3609 buffer_xml_printf (buffer,
3610 "<item>"
3611 "<column name=\"pid\">%d</column>"
3612 "<column name=\"tid\">%s</column>"
3613 "<column name=\"core\">%s</column>"
3614 "</item>", pid, dp->d_name, s);
3615 }
3616 else
3617 {
3618 if (buffer)
3619 buffer_xml_printf (buffer,
3620 "<item>"
3621 "<column name=\"pid\">%d</column>"
3622 "<column name=\"tid\">%s</column>"
3623 "</item>", pid, dp->d_name);
3624 }
3625 }
3626 }
3627 }
3628
3629 if (cores)
3630 {
3631 *cores = NULL;
3632 if (count > 0)
3633 {
3634 struct buffer buffer2;
3635 int *b;
3636 int *e;
3637 qsort (core_numbers, count, sizeof (int), compare_ints);
3638
3639 /* Remove duplicates. */
3640 b = core_numbers;
3641 e = unique (b, core_numbers + count);
3642
3643 buffer_init (&buffer2);
3644
3645 for (b = core_numbers; b != e; ++b)
3646 {
3647 char number[sizeof ("4294967295")];
3648 sprintf (number, "%u", *b);
3649 buffer_xml_printf (&buffer2, "%s%s",
3650 (b == core_numbers) ? "" : ",", number);
3651 }
3652 buffer_grow_str0 (&buffer2, "");
3653
3654 *cores = buffer_finish (&buffer2);
3655 }
3656 }
3657 free (core_numbers);
3658}
3659
3660static void
3661show_process (int pid, const char *username, struct buffer *buffer)
3662{
3663 char pathname[128];
3664 FILE *f;
3665 char cmd[MAXPATHLEN + 1];
3666
3667 sprintf (pathname, "/proc/%d/cmdline", pid);
3668
3669 if ((f = fopen (pathname, "r")) != NULL)
3670 {
3671 size_t len = fread (cmd, 1, sizeof (cmd) - 1, f);
3672 if (len > 0)
3673 {
3674 char *cores = 0;
3675 int i;
3676 for (i = 0; i < len; i++)
3677 if (cmd[i] == '\0')
3678 cmd[i] = ' ';
3679 cmd[len] = '\0';
3680
3681 buffer_xml_printf (buffer,
3682 "<item>"
3683 "<column name=\"pid\">%d</column>"
3684 "<column name=\"user\">%s</column>"
3685 "<column name=\"command\">%s</column>",
3686 pid,
3687 username,
3688 cmd);
3689
3690 /* This only collects core numbers, and does not print threads. */
3691 list_threads (pid, NULL, &cores);
3692
3693 if (cores)
3694 {
3695 buffer_xml_printf (buffer,
3696 "<column name=\"cores\">%s</column>", cores);
3697 free (cores);
3698 }
3699
3700 buffer_xml_printf (buffer, "</item>");
3701 }
3702 fclose (f);
3703 }
3704}
3705
07e059b5
VP
3706static int
3707linux_qxfer_osdata (const char *annex,
1b3f6016
PA
3708 unsigned char *readbuf, unsigned const char *writebuf,
3709 CORE_ADDR offset, int len)
07e059b5
VP
3710{
3711 /* We make the process list snapshot when the object starts to be
3712 read. */
3713 static const char *buf;
3714 static long len_avail = -1;
3715 static struct buffer buffer;
dc146f7c
VP
3716 int processes = 0;
3717 int threads = 0;
07e059b5
VP
3718
3719 DIR *dirp;
3720
dc146f7c
VP
3721 if (strcmp (annex, "processes") == 0)
3722 processes = 1;
3723 else if (strcmp (annex, "threads") == 0)
3724 threads = 1;
3725 else
07e059b5
VP
3726 return 0;
3727
3728 if (!readbuf || writebuf)
3729 return 0;
3730
3731 if (offset == 0)
3732 {
3733 if (len_avail != -1 && len_avail != 0)
3734 buffer_free (&buffer);
3735 len_avail = 0;
3736 buf = NULL;
3737 buffer_init (&buffer);
dc146f7c
VP
3738 if (processes)
3739 buffer_grow_str (&buffer, "<osdata type=\"processes\">");
3740 else if (threads)
3741 buffer_grow_str (&buffer, "<osdata type=\"threads\">");
07e059b5
VP
3742
3743 dirp = opendir ("/proc");
3744 if (dirp)
3745 {
1b3f6016
PA
3746 struct dirent *dp;
3747 while ((dp = readdir (dirp)) != NULL)
3748 {
3749 struct stat statbuf;
3750 char procentry[sizeof ("/proc/4294967295")];
3751
3752 if (!isdigit (dp->d_name[0])
3753 || strlen (dp->d_name) > sizeof ("4294967295") - 1)
3754 continue;
3755
3756 sprintf (procentry, "/proc/%s", dp->d_name);
3757 if (stat (procentry, &statbuf) == 0
3758 && S_ISDIR (statbuf.st_mode))
3759 {
dc146f7c 3760 int pid = (int) strtoul (dp->d_name, NULL, 10);
1b3f6016 3761
dc146f7c 3762 if (processes)
1b3f6016 3763 {
dc146f7c
VP
3764 struct passwd *entry = getpwuid (statbuf.st_uid);
3765 show_process (pid, entry ? entry->pw_name : "?", &buffer);
3766 }
3767 else if (threads)
3768 {
3769 list_threads (pid, &buffer, NULL);
1b3f6016
PA
3770 }
3771 }
3772 }
07e059b5 3773
1b3f6016 3774 closedir (dirp);
07e059b5
VP
3775 }
3776 buffer_grow_str0 (&buffer, "</osdata>\n");
3777 buf = buffer_finish (&buffer);
3778 len_avail = strlen (buf);
3779 }
3780
3781 if (offset >= len_avail)
3782 {
3783 /* Done. Get rid of the data. */
3784 buffer_free (&buffer);
3785 buf = NULL;
3786 len_avail = 0;
3787 return 0;
3788 }
3789
3790 if (len > len_avail - offset)
3791 len = len_avail - offset;
3792 memcpy (readbuf, buf + offset, len);
3793
3794 return len;
3795}
3796
d0722149
DE
3797/* Convert a native/host siginfo object, into/from the siginfo in the
3798 layout of the inferiors' architecture. */
3799
3800static void
3801siginfo_fixup (struct siginfo *siginfo, void *inf_siginfo, int direction)
3802{
3803 int done = 0;
3804
3805 if (the_low_target.siginfo_fixup != NULL)
3806 done = the_low_target.siginfo_fixup (siginfo, inf_siginfo, direction);
3807
3808 /* If there was no callback, or the callback didn't do anything,
3809 then just do a straight memcpy. */
3810 if (!done)
3811 {
3812 if (direction == 1)
3813 memcpy (siginfo, inf_siginfo, sizeof (struct siginfo));
3814 else
3815 memcpy (inf_siginfo, siginfo, sizeof (struct siginfo));
3816 }
3817}
3818
4aa995e1
PA
3819static int
3820linux_xfer_siginfo (const char *annex, unsigned char *readbuf,
3821 unsigned const char *writebuf, CORE_ADDR offset, int len)
3822{
d0722149 3823 int pid;
4aa995e1 3824 struct siginfo siginfo;
d0722149 3825 char inf_siginfo[sizeof (struct siginfo)];
4aa995e1
PA
3826
3827 if (current_inferior == NULL)
3828 return -1;
3829
bd99dc85 3830 pid = lwpid_of (get_thread_lwp (current_inferior));
4aa995e1
PA
3831
3832 if (debug_threads)
d0722149 3833 fprintf (stderr, "%s siginfo for lwp %d.\n",
4aa995e1
PA
3834 readbuf != NULL ? "Reading" : "Writing",
3835 pid);
3836
3837 if (offset > sizeof (siginfo))
3838 return -1;
3839
3840 if (ptrace (PTRACE_GETSIGINFO, pid, 0, &siginfo) != 0)
3841 return -1;
3842
d0722149
DE
3843 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
3844 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
3845 inferior with a 64-bit GDBSERVER should look the same as debugging it
3846 with a 32-bit GDBSERVER, we need to convert it. */
3847 siginfo_fixup (&siginfo, inf_siginfo, 0);
3848
4aa995e1
PA
3849 if (offset + len > sizeof (siginfo))
3850 len = sizeof (siginfo) - offset;
3851
3852 if (readbuf != NULL)
d0722149 3853 memcpy (readbuf, inf_siginfo + offset, len);
4aa995e1
PA
3854 else
3855 {
d0722149
DE
3856 memcpy (inf_siginfo + offset, writebuf, len);
3857
3858 /* Convert back to ptrace layout before flushing it out. */
3859 siginfo_fixup (&siginfo, inf_siginfo, 1);
3860
4aa995e1
PA
3861 if (ptrace (PTRACE_SETSIGINFO, pid, 0, &siginfo) != 0)
3862 return -1;
3863 }
3864
3865 return len;
3866}
3867
bd99dc85
PA
3868/* SIGCHLD handler that serves two purposes: In non-stop/async mode,
3869 so we notice when children change state; as the handler for the
3870 sigsuspend in my_waitpid. */
3871
3872static void
3873sigchld_handler (int signo)
3874{
3875 int old_errno = errno;
3876
3877 if (debug_threads)
3878 /* fprintf is not async-signal-safe, so call write directly. */
3879 write (2, "sigchld_handler\n", sizeof ("sigchld_handler\n") - 1);
3880
3881 if (target_is_async_p ())
3882 async_file_mark (); /* trigger a linux_wait */
3883
3884 errno = old_errno;
3885}
3886
3887static int
3888linux_supports_non_stop (void)
3889{
3890 return 1;
3891}
3892
3893static int
3894linux_async (int enable)
3895{
3896 int previous = (linux_event_pipe[0] != -1);
3897
3898 if (previous != enable)
3899 {
3900 sigset_t mask;
3901 sigemptyset (&mask);
3902 sigaddset (&mask, SIGCHLD);
3903
3904 sigprocmask (SIG_BLOCK, &mask, NULL);
3905
3906 if (enable)
3907 {
3908 if (pipe (linux_event_pipe) == -1)
3909 fatal ("creating event pipe failed.");
3910
3911 fcntl (linux_event_pipe[0], F_SETFL, O_NONBLOCK);
3912 fcntl (linux_event_pipe[1], F_SETFL, O_NONBLOCK);
3913
3914 /* Register the event loop handler. */
3915 add_file_handler (linux_event_pipe[0],
3916 handle_target_event, NULL);
3917
3918 /* Always trigger a linux_wait. */
3919 async_file_mark ();
3920 }
3921 else
3922 {
3923 delete_file_handler (linux_event_pipe[0]);
3924
3925 close (linux_event_pipe[0]);
3926 close (linux_event_pipe[1]);
3927 linux_event_pipe[0] = -1;
3928 linux_event_pipe[1] = -1;
3929 }
3930
3931 sigprocmask (SIG_UNBLOCK, &mask, NULL);
3932 }
3933
3934 return previous;
3935}
3936
3937static int
3938linux_start_non_stop (int nonstop)
3939{
3940 /* Register or unregister from event-loop accordingly. */
3941 linux_async (nonstop);
3942 return 0;
3943}
3944
cf8fd78b
PA
3945static int
3946linux_supports_multi_process (void)
3947{
3948 return 1;
3949}
3950
efcbbd14
UW
3951
3952/* Enumerate spufs IDs for process PID. */
3953static int
3954spu_enumerate_spu_ids (long pid, unsigned char *buf, CORE_ADDR offset, int len)
3955{
3956 int pos = 0;
3957 int written = 0;
3958 char path[128];
3959 DIR *dir;
3960 struct dirent *entry;
3961
3962 sprintf (path, "/proc/%ld/fd", pid);
3963 dir = opendir (path);
3964 if (!dir)
3965 return -1;
3966
3967 rewinddir (dir);
3968 while ((entry = readdir (dir)) != NULL)
3969 {
3970 struct stat st;
3971 struct statfs stfs;
3972 int fd;
3973
3974 fd = atoi (entry->d_name);
3975 if (!fd)
3976 continue;
3977
3978 sprintf (path, "/proc/%ld/fd/%d", pid, fd);
3979 if (stat (path, &st) != 0)
3980 continue;
3981 if (!S_ISDIR (st.st_mode))
3982 continue;
3983
3984 if (statfs (path, &stfs) != 0)
3985 continue;
3986 if (stfs.f_type != SPUFS_MAGIC)
3987 continue;
3988
3989 if (pos >= offset && pos + 4 <= offset + len)
3990 {
3991 *(unsigned int *)(buf + pos - offset) = fd;
3992 written += 4;
3993 }
3994 pos += 4;
3995 }
3996
3997 closedir (dir);
3998 return written;
3999}
4000
4001/* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
4002 object type, using the /proc file system. */
4003static int
4004linux_qxfer_spu (const char *annex, unsigned char *readbuf,
4005 unsigned const char *writebuf,
4006 CORE_ADDR offset, int len)
4007{
4008 long pid = lwpid_of (get_thread_lwp (current_inferior));
4009 char buf[128];
4010 int fd = 0;
4011 int ret = 0;
4012
4013 if (!writebuf && !readbuf)
4014 return -1;
4015
4016 if (!*annex)
4017 {
4018 if (!readbuf)
4019 return -1;
4020 else
4021 return spu_enumerate_spu_ids (pid, readbuf, offset, len);
4022 }
4023
4024 sprintf (buf, "/proc/%ld/fd/%s", pid, annex);
4025 fd = open (buf, writebuf? O_WRONLY : O_RDONLY);
4026 if (fd <= 0)
4027 return -1;
4028
4029 if (offset != 0
4030 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
4031 {
4032 close (fd);
4033 return 0;
4034 }
4035
4036 if (writebuf)
4037 ret = write (fd, writebuf, (size_t) len);
4038 else
4039 ret = read (fd, readbuf, (size_t) len);
4040
4041 close (fd);
4042 return ret;
4043}
4044
dc146f7c
VP
4045static int
4046linux_core_of_thread (ptid_t ptid)
4047{
4048 char filename[sizeof ("/proc//task//stat")
4049 + 2 * 20 /* decimal digits for 2 numbers, max 2^64 bit each */
4050 + 1];
4051 FILE *f;
4052 char *content = NULL;
4053 char *p;
4054 char *ts = 0;
4055 int content_read = 0;
4056 int i;
4057 int core;
4058
4059 sprintf (filename, "/proc/%d/task/%ld/stat",
4060 ptid_get_pid (ptid), ptid_get_lwp (ptid));
4061 f = fopen (filename, "r");
4062 if (!f)
4063 return -1;
4064
4065 for (;;)
4066 {
4067 int n;
4068 content = realloc (content, content_read + 1024);
4069 n = fread (content + content_read, 1, 1024, f);
4070 content_read += n;
4071 if (n < 1024)
4072 {
4073 content[content_read] = '\0';
4074 break;
4075 }
4076 }
4077
4078 p = strchr (content, '(');
4079 p = strchr (p, ')') + 2; /* skip ")" and a whitespace. */
4080
4081 p = strtok_r (p, " ", &ts);
4082 for (i = 0; i != 36; ++i)
4083 p = strtok_r (NULL, " ", &ts);
4084
4085 if (sscanf (p, "%d", &core) == 0)
4086 core = -1;
4087
4088 free (content);
4089 fclose (f);
4090
4091 return core;
4092}
4093
ce3a066d
DJ
4094static struct target_ops linux_target_ops = {
4095 linux_create_inferior,
4096 linux_attach,
4097 linux_kill,
6ad8ae5c 4098 linux_detach,
444d6139 4099 linux_join,
ce3a066d
DJ
4100 linux_thread_alive,
4101 linux_resume,
4102 linux_wait,
4103 linux_fetch_registers,
4104 linux_store_registers,
4105 linux_read_memory,
4106 linux_write_memory,
2f2893d9 4107 linux_look_up_symbols,
ef57601b 4108 linux_request_interrupt,
aa691b87 4109 linux_read_auxv,
d993e290
PA
4110 linux_insert_point,
4111 linux_remove_point,
e013ee27
OF
4112 linux_stopped_by_watchpoint,
4113 linux_stopped_data_address,
42c81e2a 4114#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437 4115 linux_read_offsets,
dae5f5cf
DJ
4116#else
4117 NULL,
4118#endif
4119#ifdef USE_THREAD_DB
4120 thread_db_get_tls_address,
4121#else
4122 NULL,
52fb6437 4123#endif
efcbbd14 4124 linux_qxfer_spu,
59a016f0 4125 hostio_last_error_from_errno,
07e059b5 4126 linux_qxfer_osdata,
4aa995e1 4127 linux_xfer_siginfo,
bd99dc85
PA
4128 linux_supports_non_stop,
4129 linux_async,
4130 linux_start_non_stop,
cdbfd419
PP
4131 linux_supports_multi_process,
4132#ifdef USE_THREAD_DB
dc146f7c 4133 thread_db_handle_monitor_command,
cdbfd419 4134#else
dc146f7c 4135 NULL,
cdbfd419 4136#endif
dc146f7c 4137 linux_core_of_thread
ce3a066d
DJ
4138};
4139
0d62e5e8
DJ
4140static void
4141linux_init_signals ()
4142{
4143 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
4144 to find what the cancel signal actually is. */
60c3d7b0 4145#ifdef __SIGRTMIN /* Bionic doesn't use SIGRTMIN the way glibc does. */
254787d4 4146 signal (__SIGRTMIN+1, SIG_IGN);
60c3d7b0 4147#endif
0d62e5e8
DJ
4148}
4149
da6d8c04
DJ
4150void
4151initialize_low (void)
4152{
bd99dc85
PA
4153 struct sigaction sigchld_action;
4154 memset (&sigchld_action, 0, sizeof (sigchld_action));
ce3a066d 4155 set_target_ops (&linux_target_ops);
611cb4a5
DJ
4156 set_breakpoint_data (the_low_target.breakpoint,
4157 the_low_target.breakpoint_len);
0d62e5e8 4158 linux_init_signals ();
24a09b5f 4159 linux_test_for_tracefork ();
52fa2412
UW
4160#ifdef HAVE_LINUX_REGSETS
4161 for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++)
4162 ;
bca929d3 4163 disabled_regsets = xmalloc (num_regsets);
52fa2412 4164#endif
bd99dc85
PA
4165
4166 sigchld_action.sa_handler = sigchld_handler;
4167 sigemptyset (&sigchld_action.sa_mask);
4168 sigchld_action.sa_flags = SA_RESTART;
4169 sigaction (SIGCHLD, &sigchld_action, NULL);
da6d8c04 4170}
This page took 0.911544 seconds and 4 git commands to generate.