]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/linux-low.c
gdb/
[thirdparty/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,
0fb0cc75 3 2006, 2007, 2008, 2009 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>
da6d8c04 40
32ca6d61
DJ
41#ifndef PTRACE_GETSIGINFO
42# define PTRACE_GETSIGINFO 0x4202
43# define PTRACE_SETSIGINFO 0x4203
44#endif
45
fd462a61
DJ
46#ifndef O_LARGEFILE
47#define O_LARGEFILE 0
48#endif
49
24a09b5f
DJ
50/* If the system headers did not provide the constants, hard-code the normal
51 values. */
52#ifndef PTRACE_EVENT_FORK
53
54#define PTRACE_SETOPTIONS 0x4200
55#define PTRACE_GETEVENTMSG 0x4201
56
57/* options set using PTRACE_SETOPTIONS */
58#define PTRACE_O_TRACESYSGOOD 0x00000001
59#define PTRACE_O_TRACEFORK 0x00000002
60#define PTRACE_O_TRACEVFORK 0x00000004
61#define PTRACE_O_TRACECLONE 0x00000008
62#define PTRACE_O_TRACEEXEC 0x00000010
63#define PTRACE_O_TRACEVFORKDONE 0x00000020
64#define PTRACE_O_TRACEEXIT 0x00000040
65
66/* Wait extended result codes for the above trace options. */
67#define PTRACE_EVENT_FORK 1
68#define PTRACE_EVENT_VFORK 2
69#define PTRACE_EVENT_CLONE 3
70#define PTRACE_EVENT_EXEC 4
71#define PTRACE_EVENT_VFORK_DONE 5
72#define PTRACE_EVENT_EXIT 6
73
74#endif /* PTRACE_EVENT_FORK */
75
76/* We can't always assume that this flag is available, but all systems
77 with the ptrace event handlers also have __WALL, so it's safe to use
78 in some contexts. */
79#ifndef __WALL
80#define __WALL 0x40000000 /* Wait for any child. */
81#endif
82
42c81e2a
DJ
83#ifdef __UCLIBC__
84#if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
85#define HAS_NOMMU
86#endif
87#endif
88
24a09b5f
DJ
89/* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
90 representation of the thread ID.
611cb4a5 91
0d62e5e8
DJ
92 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
93 the same as the LWP ID. */
94
95struct inferior_list all_processes;
96
24a09b5f
DJ
97/* A list of all unknown processes which receive stop signals. Some other
98 process will presumably claim each of these as forked children
99 momentarily. */
100
101struct inferior_list stopped_pids;
102
0d62e5e8
DJ
103/* FIXME this is a bit of a hack, and could be removed. */
104int stopping_threads;
105
106/* FIXME make into a target method? */
24a09b5f
DJ
107int using_threads = 1;
108static int thread_db_active;
109
110static int must_set_ptrace_flags;
0d62e5e8 111
d61ddec4
UW
112/* This flag is true iff we've just created or attached to a new inferior
113 but it has not stopped yet. As soon as it does, we need to call the
114 low target's arch_setup callback. */
115static int new_inferior;
116
0d62e5e8 117static void linux_resume_one_process (struct inferior_list_entry *entry,
32ca6d61 118 int step, int signal, siginfo_t *info);
64386c31 119static void linux_resume (struct thread_resume *resume_info);
0d62e5e8
DJ
120static void stop_all_processes (void);
121static int linux_wait_for_event (struct thread_info *child);
ae13219e 122static int check_removed_breakpoint (struct process_info *event_child);
24a09b5f 123static void *add_process (unsigned long pid);
97438e3f 124static int my_waitpid (int pid, int *status, int flags);
0d62e5e8
DJ
125
126struct pending_signals
127{
128 int signal;
32ca6d61 129 siginfo_t info;
0d62e5e8
DJ
130 struct pending_signals *prev;
131};
611cb4a5 132
d844cde6 133#define PTRACE_ARG3_TYPE long
c6ecbae5 134#define PTRACE_XFER_TYPE long
da6d8c04 135
58caa3dc 136#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
137static char *disabled_regsets;
138static int num_regsets;
58caa3dc
DJ
139#endif
140
0d62e5e8
DJ
141#define pid_of(proc) ((proc)->head.id)
142
143/* FIXME: Delete eventually. */
144#define inferior_pid (pid_of (get_thread_process (current_inferior)))
145
24a09b5f
DJ
146static void
147handle_extended_wait (struct process_info *event_child, int wstat)
148{
149 int event = wstat >> 16;
150 struct process_info *new_process;
151
152 if (event == PTRACE_EVENT_CLONE)
153 {
154 unsigned long new_pid;
836acd6d 155 int ret, status = W_STOPCODE (SIGSTOP);
24a09b5f
DJ
156
157 ptrace (PTRACE_GETEVENTMSG, inferior_pid, 0, &new_pid);
158
159 /* If we haven't already seen the new PID stop, wait for it now. */
160 if (! pull_pid_from_list (&stopped_pids, new_pid))
161 {
162 /* The new child has a pending SIGSTOP. We can't affect it until it
163 hits the SIGSTOP, but we're already attached. */
164
97438e3f 165 ret = my_waitpid (new_pid, &status, __WALL);
24a09b5f
DJ
166
167 if (ret == -1)
168 perror_with_name ("waiting for new child");
169 else if (ret != new_pid)
170 warning ("wait returned unexpected PID %d", ret);
da5898ce 171 else if (!WIFSTOPPED (status))
24a09b5f
DJ
172 warning ("wait returned unexpected status 0x%x", status);
173 }
174
175 ptrace (PTRACE_SETOPTIONS, new_pid, 0, PTRACE_O_TRACECLONE);
176
177 new_process = (struct process_info *) add_process (new_pid);
178 add_thread (new_pid, new_process, new_pid);
179 new_thread_notify (thread_id_to_gdb_id (new_process->lwpid));
180
da5898ce
DJ
181 /* Normally we will get the pending SIGSTOP. But in some cases
182 we might get another signal delivered to the group first.
f21cc1a2 183 If we do get another signal, be sure not to lose it. */
da5898ce
DJ
184 if (WSTOPSIG (status) == SIGSTOP)
185 {
186 if (stopping_threads)
187 new_process->stopped = 1;
188 else
189 ptrace (PTRACE_CONT, new_pid, 0, 0);
190 }
24a09b5f 191 else
da5898ce
DJ
192 {
193 new_process->stop_expected = 1;
194 if (stopping_threads)
195 {
196 new_process->stopped = 1;
197 new_process->status_pending_p = 1;
198 new_process->status_pending = status;
199 }
200 else
201 /* Pass the signal on. This is what GDB does - except
202 shouldn't we really report it instead? */
203 ptrace (PTRACE_CONT, new_pid, 0, WSTOPSIG (status));
204 }
24a09b5f
DJ
205
206 /* Always resume the current thread. If we are stopping
207 threads, it will have a pending SIGSTOP; we may as well
208 collect it now. */
209 linux_resume_one_process (&event_child->head,
210 event_child->stepping, 0, NULL);
211 }
212}
213
0d62e5e8
DJ
214/* This function should only be called if the process got a SIGTRAP.
215 The SIGTRAP could mean several things.
216
217 On i386, where decr_pc_after_break is non-zero:
218 If we were single-stepping this process using PTRACE_SINGLESTEP,
219 we will get only the one SIGTRAP (even if the instruction we
220 stepped over was a breakpoint). The value of $eip will be the
221 next instruction.
222 If we continue the process using PTRACE_CONT, we will get a
223 SIGTRAP when we hit a breakpoint. The value of $eip will be
224 the instruction after the breakpoint (i.e. needs to be
225 decremented). If we report the SIGTRAP to GDB, we must also
226 report the undecremented PC. If we cancel the SIGTRAP, we
227 must resume at the decremented PC.
228
229 (Presumably, not yet tested) On a non-decr_pc_after_break machine
230 with hardware or kernel single-step:
231 If we single-step over a breakpoint instruction, our PC will
232 point at the following instruction. If we continue and hit a
233 breakpoint instruction, our PC will point at the breakpoint
234 instruction. */
235
236static CORE_ADDR
237get_stop_pc (void)
238{
239 CORE_ADDR stop_pc = (*the_low_target.get_pc) ();
240
241 if (get_thread_process (current_inferior)->stepping)
242 return stop_pc;
243 else
244 return stop_pc - the_low_target.decr_pc_after_break;
245}
ce3a066d 246
0d62e5e8 247static void *
a1928bad 248add_process (unsigned long pid)
611cb4a5 249{
0d62e5e8
DJ
250 struct process_info *process;
251
bca929d3 252 process = (struct process_info *) xmalloc (sizeof (*process));
0d62e5e8
DJ
253 memset (process, 0, sizeof (*process));
254
255 process->head.id = pid;
0d62e5e8
DJ
256 process->lwpid = pid;
257
258 add_inferior_to_list (&all_processes, &process->head);
259
260 return process;
261}
611cb4a5 262
da6d8c04
DJ
263/* Start an inferior process and returns its pid.
264 ALLARGS is a vector of program-name and args. */
265
ce3a066d
DJ
266static int
267linux_create_inferior (char *program, char **allargs)
da6d8c04 268{
0d62e5e8 269 void *new_process;
da6d8c04
DJ
270 int pid;
271
42c81e2a 272#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
273 pid = vfork ();
274#else
da6d8c04 275 pid = fork ();
52fb6437 276#endif
da6d8c04
DJ
277 if (pid < 0)
278 perror_with_name ("fork");
279
280 if (pid == 0)
281 {
282 ptrace (PTRACE_TRACEME, 0, 0, 0);
283
254787d4 284 signal (__SIGRTMIN + 1, SIG_DFL);
0d62e5e8 285
a9fa9f7d
DJ
286 setpgid (0, 0);
287
2b876972
DJ
288 execv (program, allargs);
289 if (errno == ENOENT)
290 execvp (program, allargs);
da6d8c04
DJ
291
292 fprintf (stderr, "Cannot exec %s: %s.\n", program,
d07c63e7 293 strerror (errno));
da6d8c04
DJ
294 fflush (stderr);
295 _exit (0177);
296 }
297
0d62e5e8 298 new_process = add_process (pid);
a06660f7 299 add_thread (pid, new_process, pid);
24a09b5f 300 must_set_ptrace_flags = 1;
d61ddec4 301 new_inferior = 1;
611cb4a5 302
a9fa9f7d 303 return pid;
da6d8c04
DJ
304}
305
306/* Attach to an inferior process. */
307
0d62e5e8 308void
24a09b5f 309linux_attach_lwp (unsigned long pid)
da6d8c04 310{
0d62e5e8 311 struct process_info *new_process;
611cb4a5 312
da6d8c04
DJ
313 if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
314 {
2d717e4f
DJ
315 if (all_threads.head != NULL)
316 {
317 /* If we fail to attach to an LWP, just warn. */
318 fprintf (stderr, "Cannot attach to process %ld: %s (%d)\n", pid,
319 strerror (errno), errno);
320 fflush (stderr);
321 return;
322 }
323 else
324 /* If we fail to attach to a process, report an error. */
325 error ("Cannot attach to process %ld: %s (%d)\n", pid,
43d5792c 326 strerror (errno), errno);
da6d8c04
DJ
327 }
328
0e21c1ec
DE
329 /* FIXME: This intermittently fails.
330 We need to wait for SIGSTOP first. */
24a09b5f
DJ
331 ptrace (PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACECLONE);
332
0d62e5e8 333 new_process = (struct process_info *) add_process (pid);
24a09b5f
DJ
334 add_thread (pid, new_process, pid);
335 new_thread_notify (thread_id_to_gdb_id (new_process->lwpid));
0d62e5e8
DJ
336
337 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
0e21c1ec
DE
338 brings it to a halt.
339
340 There are several cases to consider here:
341
342 1) gdbserver has already attached to the process and is being notified
343 of a new thread that is being created.
344 In this case we should ignore that SIGSTOP and resume the process.
345 This is handled below by setting stop_expected = 1.
346
347 2) This is the first thread (the process thread), and we're attaching
348 to it via attach_inferior.
349 In this case we want the process thread to stop.
350 This is handled by having linux_attach clear stop_expected after
351 we return.
352 ??? If the process already has several threads we leave the other
353 threads running.
354
355 3) GDB is connecting to gdbserver and is requesting an enumeration of all
356 existing threads.
357 In this case we want the thread to stop.
358 FIXME: This case is currently not properly handled.
359 We should wait for the SIGSTOP but don't. Things work apparently
360 because enough time passes between when we ptrace (ATTACH) and when
361 gdb makes the next ptrace call on the thread.
0d62e5e8
DJ
362
363 On the other hand, if we are currently trying to stop all threads, we
364 should treat the new thread as if we had sent it a SIGSTOP. This works
0e21c1ec
DE
365 because we are guaranteed that the add_process call above added us to the
366 end of the list, and so the new thread has not yet reached
367 wait_for_sigstop (but will). */
0d62e5e8
DJ
368 if (! stopping_threads)
369 new_process->stop_expected = 1;
370}
371
372int
a1928bad 373linux_attach (unsigned long pid)
0d62e5e8
DJ
374{
375 struct process_info *process;
376
24a09b5f 377 linux_attach_lwp (pid);
0d62e5e8 378
ae13219e
DJ
379 /* Don't ignore the initial SIGSTOP if we just attached to this process.
380 It will be collected by wait shortly. */
0d62e5e8
DJ
381 process = (struct process_info *) find_inferior_id (&all_processes, pid);
382 process->stop_expected = 0;
383
d61ddec4
UW
384 new_inferior = 1;
385
da6d8c04
DJ
386 return 0;
387}
388
389/* Kill the inferior process. Make us have no inferior. */
390
ce3a066d 391static void
0d62e5e8 392linux_kill_one_process (struct inferior_list_entry *entry)
da6d8c04 393{
0d62e5e8
DJ
394 struct thread_info *thread = (struct thread_info *) entry;
395 struct process_info *process = get_thread_process (thread);
396 int wstat;
397
fd500816
DJ
398 /* We avoid killing the first thread here, because of a Linux kernel (at
399 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
400 the children get a chance to be reaped, it will remain a zombie
401 forever. */
402 if (entry == all_threads.head)
403 return;
404
0d62e5e8
DJ
405 do
406 {
407 ptrace (PTRACE_KILL, pid_of (process), 0, 0);
408
409 /* Make sure it died. The loop is most likely unnecessary. */
410 wstat = linux_wait_for_event (thread);
411 } while (WIFSTOPPED (wstat));
da6d8c04
DJ
412}
413
0d62e5e8
DJ
414static void
415linux_kill (void)
416{
fd500816 417 struct thread_info *thread = (struct thread_info *) all_threads.head;
9d606399 418 struct process_info *process;
fd500816
DJ
419 int wstat;
420
9d606399
DJ
421 if (thread == NULL)
422 return;
423
0d62e5e8 424 for_each_inferior (&all_threads, linux_kill_one_process);
fd500816
DJ
425
426 /* See the comment in linux_kill_one_process. We did not kill the first
427 thread in the list, so do so now. */
9d606399 428 process = get_thread_process (thread);
fd500816
DJ
429 do
430 {
431 ptrace (PTRACE_KILL, pid_of (process), 0, 0);
432
433 /* Make sure it died. The loop is most likely unnecessary. */
434 wstat = linux_wait_for_event (thread);
435 } while (WIFSTOPPED (wstat));
2d717e4f
DJ
436
437 clear_inferiors ();
438 free (all_processes.head);
439 all_processes.head = all_processes.tail = NULL;
0d62e5e8
DJ
440}
441
6ad8ae5c
DJ
442static void
443linux_detach_one_process (struct inferior_list_entry *entry)
444{
445 struct thread_info *thread = (struct thread_info *) entry;
446 struct process_info *process = get_thread_process (thread);
447
ae13219e
DJ
448 /* Make sure the process isn't stopped at a breakpoint that's
449 no longer there. */
450 check_removed_breakpoint (process);
451
452 /* If this process is stopped but is expecting a SIGSTOP, then make
453 sure we take care of that now. This isn't absolutely guaranteed
454 to collect the SIGSTOP, but is fairly likely to. */
455 if (process->stop_expected)
456 {
457 /* Clear stop_expected, so that the SIGSTOP will be reported. */
458 process->stop_expected = 0;
459 if (process->stopped)
460 linux_resume_one_process (&process->head, 0, 0, NULL);
461 linux_wait_for_event (thread);
462 }
463
464 /* Flush any pending changes to the process's registers. */
465 regcache_invalidate_one ((struct inferior_list_entry *)
466 get_process_thread (process));
467
468 /* Finally, let it resume. */
6ad8ae5c
DJ
469 ptrace (PTRACE_DETACH, pid_of (process), 0, 0);
470}
471
dd6953e1 472static int
6ad8ae5c
DJ
473linux_detach (void)
474{
ae13219e 475 delete_all_breakpoints ();
6ad8ae5c 476 for_each_inferior (&all_threads, linux_detach_one_process);
ae13219e 477 clear_inferiors ();
2d717e4f
DJ
478 free (all_processes.head);
479 all_processes.head = all_processes.tail = NULL;
dd6953e1 480 return 0;
6ad8ae5c
DJ
481}
482
444d6139
PA
483static void
484linux_join (void)
485{
486 extern unsigned long signal_pid;
487 int status, ret;
488
489 do {
490 ret = waitpid (signal_pid, &status, 0);
491 if (WIFEXITED (status) || WIFSIGNALED (status))
492 break;
493 } while (ret != -1 || errno != ECHILD);
494}
495
6ad8ae5c 496/* Return nonzero if the given thread is still alive. */
0d62e5e8 497static int
24a09b5f 498linux_thread_alive (unsigned long lwpid)
0d62e5e8 499{
24a09b5f 500 if (find_inferior_id (&all_threads, lwpid) != NULL)
0d62e5e8
DJ
501 return 1;
502 else
503 return 0;
504}
505
506/* Return nonzero if this process stopped at a breakpoint which
507 no longer appears to be inserted. Also adjust the PC
508 appropriately to resume where the breakpoint used to be. */
ce3a066d 509static int
0d62e5e8 510check_removed_breakpoint (struct process_info *event_child)
da6d8c04 511{
0d62e5e8
DJ
512 CORE_ADDR stop_pc;
513 struct thread_info *saved_inferior;
514
515 if (event_child->pending_is_breakpoint == 0)
516 return 0;
517
518 if (debug_threads)
ae13219e
DJ
519 fprintf (stderr, "Checking for breakpoint in process %ld.\n",
520 event_child->lwpid);
0d62e5e8
DJ
521
522 saved_inferior = current_inferior;
523 current_inferior = get_process_thread (event_child);
524
525 stop_pc = get_stop_pc ();
526
527 /* If the PC has changed since we stopped, then we shouldn't do
528 anything. This happens if, for instance, GDB handled the
529 decr_pc_after_break subtraction itself. */
530 if (stop_pc != event_child->pending_stop_pc)
531 {
532 if (debug_threads)
ae13219e
DJ
533 fprintf (stderr, "Ignoring, PC was changed. Old PC was 0x%08llx\n",
534 event_child->pending_stop_pc);
0d62e5e8
DJ
535
536 event_child->pending_is_breakpoint = 0;
537 current_inferior = saved_inferior;
538 return 0;
539 }
540
541 /* If the breakpoint is still there, we will report hitting it. */
542 if ((*the_low_target.breakpoint_at) (stop_pc))
543 {
544 if (debug_threads)
545 fprintf (stderr, "Ignoring, breakpoint is still present.\n");
546 current_inferior = saved_inferior;
547 return 0;
548 }
549
550 if (debug_threads)
551 fprintf (stderr, "Removed breakpoint.\n");
552
553 /* For decr_pc_after_break targets, here is where we perform the
554 decrement. We go immediately from this function to resuming,
555 and can not safely call get_stop_pc () again. */
556 if (the_low_target.set_pc != NULL)
557 (*the_low_target.set_pc) (stop_pc);
558
559 /* We consumed the pending SIGTRAP. */
5544ad89 560 event_child->pending_is_breakpoint = 0;
0d62e5e8
DJ
561 event_child->status_pending_p = 0;
562 event_child->status_pending = 0;
563
564 current_inferior = saved_inferior;
da6d8c04
DJ
565 return 1;
566}
567
0d62e5e8
DJ
568/* Return 1 if this process has an interesting status pending. This function
569 may silently resume an inferior process. */
611cb4a5 570static int
0d62e5e8
DJ
571status_pending_p (struct inferior_list_entry *entry, void *dummy)
572{
573 struct process_info *process = (struct process_info *) entry;
574
575 if (process->status_pending_p)
576 if (check_removed_breakpoint (process))
577 {
578 /* This thread was stopped at a breakpoint, and the breakpoint
579 is now gone. We were told to continue (or step...) all threads,
580 so GDB isn't trying to single-step past this breakpoint.
581 So instead of reporting the old SIGTRAP, pretend we got to
582 the breakpoint just after it was removed instead of just
583 before; resume the process. */
32ca6d61 584 linux_resume_one_process (&process->head, 0, 0, NULL);
0d62e5e8
DJ
585 return 0;
586 }
587
588 return process->status_pending_p;
589}
590
591static void
592linux_wait_for_process (struct process_info **childp, int *wstatp)
611cb4a5 593{
0d62e5e8
DJ
594 int ret;
595 int to_wait_for = -1;
596
597 if (*childp != NULL)
598 to_wait_for = (*childp)->lwpid;
611cb4a5 599
24a09b5f 600retry:
611cb4a5
DJ
601 while (1)
602 {
0d62e5e8
DJ
603 ret = waitpid (to_wait_for, wstatp, WNOHANG);
604
605 if (ret == -1)
606 {
607 if (errno != ECHILD)
608 perror_with_name ("waitpid");
609 }
610 else if (ret > 0)
611 break;
612
613 ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE);
614
615 if (ret == -1)
616 {
617 if (errno != ECHILD)
618 perror_with_name ("waitpid (WCLONE)");
619 }
620 else if (ret > 0)
621 break;
622
623 usleep (1000);
624 }
625
626 if (debug_threads
627 && (!WIFSTOPPED (*wstatp)
628 || (WSTOPSIG (*wstatp) != 32
629 && WSTOPSIG (*wstatp) != 33)))
630 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
631
632 if (to_wait_for == -1)
633 *childp = (struct process_info *) find_inferior_id (&all_processes, ret);
634
24a09b5f
DJ
635 /* If we didn't find a process, one of two things presumably happened:
636 - A process we started and then detached from has exited. Ignore it.
637 - A process we are controlling has forked and the new child's stop
638 was reported to us by the kernel. Save its PID. */
639 if (*childp == NULL && WIFSTOPPED (*wstatp))
640 {
641 add_pid_to_list (&stopped_pids, ret);
642 goto retry;
643 }
644 else if (*childp == NULL)
645 goto retry;
646
0d62e5e8
DJ
647 (*childp)->stopped = 1;
648 (*childp)->pending_is_breakpoint = 0;
649
32ca6d61
DJ
650 (*childp)->last_status = *wstatp;
651
d61ddec4
UW
652 /* Architecture-specific setup after inferior is running.
653 This needs to happen after we have attached to the inferior
654 and it is stopped for the first time, but before we access
655 any inferior registers. */
656 if (new_inferior)
657 {
658 the_low_target.arch_setup ();
52fa2412
UW
659#ifdef HAVE_LINUX_REGSETS
660 memset (disabled_regsets, 0, num_regsets);
661#endif
d61ddec4
UW
662 new_inferior = 0;
663 }
664
0d62e5e8
DJ
665 if (debug_threads
666 && WIFSTOPPED (*wstatp))
667 {
896c7fbb 668 struct thread_info *saved_inferior = current_inferior;
0d62e5e8 669 current_inferior = (struct thread_info *)
24a09b5f 670 find_inferior_id (&all_threads, (*childp)->lwpid);
0d62e5e8
DJ
671 /* For testing only; i386_stop_pc prints out a diagnostic. */
672 if (the_low_target.get_pc != NULL)
673 get_stop_pc ();
896c7fbb 674 current_inferior = saved_inferior;
0d62e5e8
DJ
675 }
676}
611cb4a5 677
0d62e5e8
DJ
678static int
679linux_wait_for_event (struct thread_info *child)
680{
681 CORE_ADDR stop_pc;
682 struct process_info *event_child;
683 int wstat;
b65d95c5 684 int bp_status;
0d62e5e8
DJ
685
686 /* Check for a process with a pending status. */
687 /* It is possible that the user changed the pending task's registers since
688 it stopped. We correctly handle the change of PC if we hit a breakpoint
e5379b03 689 (in check_removed_breakpoint); signals should be reported anyway. */
0d62e5e8
DJ
690 if (child == NULL)
691 {
692 event_child = (struct process_info *)
693 find_inferior (&all_processes, status_pending_p, NULL);
694 if (debug_threads && event_child)
a1928bad 695 fprintf (stderr, "Got a pending child %ld\n", event_child->lwpid);
0d62e5e8
DJ
696 }
697 else
698 {
699 event_child = get_thread_process (child);
700 if (event_child->status_pending_p
701 && check_removed_breakpoint (event_child))
702 event_child = NULL;
703 }
611cb4a5 704
0d62e5e8
DJ
705 if (event_child != NULL)
706 {
707 if (event_child->status_pending_p)
611cb4a5 708 {
0d62e5e8 709 if (debug_threads)
a1928bad 710 fprintf (stderr, "Got an event from pending child %ld (%04x)\n",
0d62e5e8
DJ
711 event_child->lwpid, event_child->status_pending);
712 wstat = event_child->status_pending;
713 event_child->status_pending_p = 0;
714 event_child->status_pending = 0;
715 current_inferior = get_process_thread (event_child);
716 return wstat;
717 }
718 }
719
720 /* We only enter this loop if no process has a pending wait status. Thus
721 any action taken in response to a wait status inside this loop is
722 responding as soon as we detect the status, not after any pending
723 events. */
724 while (1)
725 {
726 if (child == NULL)
727 event_child = NULL;
728 else
729 event_child = get_thread_process (child);
730
731 linux_wait_for_process (&event_child, &wstat);
732
733 if (event_child == NULL)
734 error ("event from unknown child");
611cb4a5 735
0d62e5e8 736 current_inferior = (struct thread_info *)
24a09b5f 737 find_inferior_id (&all_threads, event_child->lwpid);
0d62e5e8 738
89be2091 739 /* Check for thread exit. */
24a09b5f 740 if (! WIFSTOPPED (wstat))
0d62e5e8 741 {
89be2091 742 if (debug_threads)
24a09b5f 743 fprintf (stderr, "LWP %ld exiting\n", event_child->head.id);
89be2091
DJ
744
745 /* If the last thread is exiting, just return. */
746 if (all_threads.head == all_threads.tail)
747 return wstat;
748
24a09b5f 749 dead_thread_notify (thread_id_to_gdb_id (event_child->lwpid));
89be2091
DJ
750
751 remove_inferior (&all_processes, &event_child->head);
752 free (event_child);
753 remove_thread (current_inferior);
754 current_inferior = (struct thread_info *) all_threads.head;
755
756 /* If we were waiting for this particular child to do something...
757 well, it did something. */
758 if (child != NULL)
759 return wstat;
760
761 /* Wait for a more interesting event. */
762 continue;
763 }
764
24a09b5f 765 if (WIFSTOPPED (wstat)
89be2091
DJ
766 && WSTOPSIG (wstat) == SIGSTOP
767 && event_child->stop_expected)
768 {
769 if (debug_threads)
770 fprintf (stderr, "Expected stop.\n");
771 event_child->stop_expected = 0;
772 linux_resume_one_process (&event_child->head,
773 event_child->stepping, 0, NULL);
774 continue;
775 }
776
24a09b5f
DJ
777 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGTRAP
778 && wstat >> 16 != 0)
779 {
780 handle_extended_wait (event_child, wstat);
781 continue;
782 }
783
89be2091
DJ
784 /* If GDB is not interested in this signal, don't stop other
785 threads, and don't report it to GDB. Just resume the
786 inferior right away. We do this for threading-related
69f223ed
DJ
787 signals as well as any that GDB specifically requested we
788 ignore. But never ignore SIGSTOP if we sent it ourselves,
789 and do not ignore signals when stepping - they may require
790 special handling to skip the signal handler. */
89be2091
DJ
791 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
792 thread library? */
793 if (WIFSTOPPED (wstat)
69f223ed 794 && !event_child->stepping
24a09b5f
DJ
795 && (
796#ifdef USE_THREAD_DB
797 (thread_db_active && (WSTOPSIG (wstat) == __SIGRTMIN
798 || WSTOPSIG (wstat) == __SIGRTMIN + 1))
799 ||
800#endif
801 (pass_signals[target_signal_from_host (WSTOPSIG (wstat))]
802 && (WSTOPSIG (wstat) != SIGSTOP || !stopping_threads))))
89be2091
DJ
803 {
804 siginfo_t info, *info_p;
805
806 if (debug_threads)
24a09b5f
DJ
807 fprintf (stderr, "Ignored signal %d for LWP %ld.\n",
808 WSTOPSIG (wstat), event_child->head.id);
89be2091
DJ
809
810 if (ptrace (PTRACE_GETSIGINFO, event_child->lwpid, 0, &info) == 0)
811 info_p = &info;
812 else
813 info_p = NULL;
814 linux_resume_one_process (&event_child->head,
815 event_child->stepping,
816 WSTOPSIG (wstat), info_p);
817 continue;
0d62e5e8 818 }
611cb4a5 819
0d62e5e8
DJ
820 /* If this event was not handled above, and is not a SIGTRAP, report
821 it. */
822 if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP)
823 return wstat;
611cb4a5 824
0d62e5e8
DJ
825 /* If this target does not support breakpoints, we simply report the
826 SIGTRAP; it's of no concern to us. */
827 if (the_low_target.get_pc == NULL)
828 return wstat;
829
830 stop_pc = get_stop_pc ();
831
832 /* bp_reinsert will only be set if we were single-stepping.
833 Notice that we will resume the process after hitting
834 a gdbserver breakpoint; single-stepping to/over one
835 is not supported (yet). */
836 if (event_child->bp_reinsert != 0)
837 {
838 if (debug_threads)
839 fprintf (stderr, "Reinserted breakpoint.\n");
840 reinsert_breakpoint (event_child->bp_reinsert);
841 event_child->bp_reinsert = 0;
842
843 /* Clear the single-stepping flag and SIGTRAP as we resume. */
32ca6d61 844 linux_resume_one_process (&event_child->head, 0, 0, NULL);
0d62e5e8
DJ
845 continue;
846 }
847
b65d95c5 848 bp_status = check_breakpoints (stop_pc);
0d62e5e8 849
b65d95c5 850 if (bp_status != 0)
0d62e5e8 851 {
b65d95c5
DJ
852 if (debug_threads)
853 fprintf (stderr, "Hit a gdbserver breakpoint.\n");
854
0d62e5e8 855 /* We hit one of our own breakpoints. We mark it as a pending
e5379b03 856 breakpoint, so that check_removed_breakpoint () will do the PC
0d62e5e8
DJ
857 adjustment for us at the appropriate time. */
858 event_child->pending_is_breakpoint = 1;
859 event_child->pending_stop_pc = stop_pc;
860
b65d95c5 861 /* We may need to put the breakpoint back. We continue in the event
0d62e5e8
DJ
862 loop instead of simply replacing the breakpoint right away,
863 in order to not lose signals sent to the thread that hit the
864 breakpoint. Unfortunately this increases the window where another
865 thread could sneak past the removed breakpoint. For the current
866 use of server-side breakpoints (thread creation) this is
867 acceptable; but it needs to be considered before this breakpoint
868 mechanism can be used in more general ways. For some breakpoints
869 it may be necessary to stop all other threads, but that should
870 be avoided where possible.
871
872 If breakpoint_reinsert_addr is NULL, that means that we can
873 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
874 mark it for reinsertion, and single-step.
875
876 Otherwise, call the target function to figure out where we need
877 our temporary breakpoint, create it, and continue executing this
878 process. */
b65d95c5
DJ
879 if (bp_status == 2)
880 /* No need to reinsert. */
881 linux_resume_one_process (&event_child->head, 0, 0, NULL);
882 else if (the_low_target.breakpoint_reinsert_addr == NULL)
0d62e5e8
DJ
883 {
884 event_child->bp_reinsert = stop_pc;
885 uninsert_breakpoint (stop_pc);
32ca6d61 886 linux_resume_one_process (&event_child->head, 1, 0, NULL);
0d62e5e8
DJ
887 }
888 else
889 {
890 reinsert_breakpoint_by_bp
891 (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
32ca6d61 892 linux_resume_one_process (&event_child->head, 0, 0, NULL);
611cb4a5 893 }
0d62e5e8
DJ
894
895 continue;
896 }
897
b65d95c5
DJ
898 if (debug_threads)
899 fprintf (stderr, "Hit a non-gdbserver breakpoint.\n");
900
0d62e5e8
DJ
901 /* If we were single-stepping, we definitely want to report the
902 SIGTRAP. The single-step operation has completed, so also
aa691b87 903 clear the stepping flag; in general this does not matter,
0d62e5e8
DJ
904 because the SIGTRAP will be reported to the client, which
905 will give us a new action for this thread, but clear it for
906 consistency anyway. It's safe to clear the stepping flag
907 because the only consumer of get_stop_pc () after this point
e5379b03 908 is check_removed_breakpoint, and pending_is_breakpoint is not
0d62e5e8
DJ
909 set. It might be wiser to use a step_completed flag instead. */
910 if (event_child->stepping)
911 {
912 event_child->stepping = 0;
913 return wstat;
914 }
915
916 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
917 Check if it is a breakpoint, and if so mark the process information
918 accordingly. This will handle both the necessary fiddling with the
919 PC on decr_pc_after_break targets and suppressing extra threads
920 hitting a breakpoint if two hit it at once and then GDB removes it
921 after the first is reported. Arguably it would be better to report
922 multiple threads hitting breakpoints simultaneously, but the current
923 remote protocol does not allow this. */
924 if ((*the_low_target.breakpoint_at) (stop_pc))
925 {
926 event_child->pending_is_breakpoint = 1;
927 event_child->pending_stop_pc = stop_pc;
611cb4a5
DJ
928 }
929
930 return wstat;
931 }
0d62e5e8 932
611cb4a5
DJ
933 /* NOTREACHED */
934 return 0;
935}
936
0d62e5e8 937/* Wait for process, returns status. */
da6d8c04 938
ce3a066d
DJ
939static unsigned char
940linux_wait (char *status)
da6d8c04 941{
e5f1222d 942 int w;
0d62e5e8
DJ
943 struct thread_info *child = NULL;
944
945retry:
946 /* If we were only supposed to resume one thread, only wait for
947 that thread - if it's still alive. If it died, however - which
948 can happen if we're coming from the thread death case below -
949 then we need to make sure we restart the other threads. We could
950 pick a thread at random or restart all; restarting all is less
951 arbitrary. */
d592fa2f 952 if (cont_thread != 0 && cont_thread != -1)
0d62e5e8
DJ
953 {
954 child = (struct thread_info *) find_inferior_id (&all_threads,
955 cont_thread);
956
957 /* No stepping, no signal - unless one is pending already, of course. */
958 if (child == NULL)
64386c31
DJ
959 {
960 struct thread_resume resume_info;
961 resume_info.thread = -1;
962 resume_info.step = resume_info.sig = resume_info.leave_stopped = 0;
963 linux_resume (&resume_info);
964 }
0d62e5e8 965 }
da6d8c04 966
0d62e5e8
DJ
967 w = linux_wait_for_event (child);
968 stop_all_processes ();
da6d8c04 969
24a09b5f
DJ
970 if (must_set_ptrace_flags)
971 {
972 ptrace (PTRACE_SETOPTIONS, inferior_pid, 0, PTRACE_O_TRACECLONE);
973 must_set_ptrace_flags = 0;
974 }
975
0d62e5e8
DJ
976 /* If we are waiting for a particular child, and it exited,
977 linux_wait_for_event will return its exit status. Similarly if
978 the last child exited. If this is not the last child, however,
979 do not report it as exited until there is a 'thread exited' response
980 available in the remote protocol. Instead, just wait for another event.
981 This should be safe, because if the thread crashed we will already
982 have reported the termination signal to GDB; that should stop any
983 in-progress stepping operations, etc.
984
985 Report the exit status of the last thread to exit. This matches
986 LinuxThreads' behavior. */
987
988 if (all_threads.head == all_threads.tail)
da6d8c04 989 {
0d62e5e8
DJ
990 if (WIFEXITED (w))
991 {
992 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
993 *status = 'W';
994 clear_inferiors ();
075b3282
DJ
995 free (all_processes.head);
996 all_processes.head = all_processes.tail = NULL;
b80864fb 997 return WEXITSTATUS (w);
0d62e5e8
DJ
998 }
999 else if (!WIFSTOPPED (w))
1000 {
1001 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
0d62e5e8 1002 *status = 'X';
075b3282
DJ
1003 clear_inferiors ();
1004 free (all_processes.head);
1005 all_processes.head = all_processes.tail = NULL;
b80864fb 1006 return target_signal_from_host (WTERMSIG (w));
0d62e5e8 1007 }
da6d8c04 1008 }
0d62e5e8 1009 else
da6d8c04 1010 {
0d62e5e8
DJ
1011 if (!WIFSTOPPED (w))
1012 goto retry;
da6d8c04
DJ
1013 }
1014
da6d8c04 1015 *status = 'T';
b80864fb 1016 return target_signal_from_host (WSTOPSIG (w));
da6d8c04
DJ
1017}
1018
fd500816
DJ
1019/* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
1020 thread groups are in use, we need to use tkill. */
1021
1022static int
a1928bad 1023kill_lwp (unsigned long lwpid, int signo)
fd500816
DJ
1024{
1025 static int tkill_failed;
1026
1027 errno = 0;
1028
1029#ifdef SYS_tkill
1030 if (!tkill_failed)
1031 {
1032 int ret = syscall (SYS_tkill, lwpid, signo);
1033 if (errno != ENOSYS)
1034 return ret;
1035 errno = 0;
1036 tkill_failed = 1;
1037 }
1038#endif
1039
1040 return kill (lwpid, signo);
1041}
1042
0d62e5e8
DJ
1043static void
1044send_sigstop (struct inferior_list_entry *entry)
1045{
1046 struct process_info *process = (struct process_info *) entry;
1047
1048 if (process->stopped)
1049 return;
1050
1051 /* If we already have a pending stop signal for this process, don't
1052 send another. */
1053 if (process->stop_expected)
1054 {
ae13219e
DJ
1055 if (debug_threads)
1056 fprintf (stderr, "Have pending sigstop for process %ld\n",
1057 process->lwpid);
1058
1059 /* We clear the stop_expected flag so that wait_for_sigstop
1060 will receive the SIGSTOP event (instead of silently resuming and
1061 waiting again). It'll be reset below. */
0d62e5e8
DJ
1062 process->stop_expected = 0;
1063 return;
1064 }
1065
1066 if (debug_threads)
a1928bad 1067 fprintf (stderr, "Sending sigstop to process %ld\n", process->head.id);
0d62e5e8 1068
fd500816 1069 kill_lwp (process->head.id, SIGSTOP);
0d62e5e8
DJ
1070}
1071
1072static void
1073wait_for_sigstop (struct inferior_list_entry *entry)
1074{
1075 struct process_info *process = (struct process_info *) entry;
1076 struct thread_info *saved_inferior, *thread;
a1928bad
DJ
1077 int wstat;
1078 unsigned long saved_tid;
0d62e5e8
DJ
1079
1080 if (process->stopped)
1081 return;
1082
1083 saved_inferior = current_inferior;
1084 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
1085 thread = (struct thread_info *) find_inferior_id (&all_threads,
24a09b5f 1086 process->lwpid);
0d62e5e8
DJ
1087 wstat = linux_wait_for_event (thread);
1088
1089 /* If we stopped with a non-SIGSTOP signal, save it for later
1090 and record the pending SIGSTOP. If the process exited, just
1091 return. */
1092 if (WIFSTOPPED (wstat)
1093 && WSTOPSIG (wstat) != SIGSTOP)
1094 {
1095 if (debug_threads)
24a09b5f
DJ
1096 fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n",
1097 process->lwpid, wstat);
0d62e5e8
DJ
1098 process->status_pending_p = 1;
1099 process->status_pending = wstat;
1100 process->stop_expected = 1;
1101 }
1102
1103 if (linux_thread_alive (saved_tid))
1104 current_inferior = saved_inferior;
1105 else
1106 {
1107 if (debug_threads)
1108 fprintf (stderr, "Previously current thread died.\n");
1109
1110 /* Set a valid thread as current. */
1111 set_desired_inferior (0);
1112 }
1113}
1114
1115static void
1116stop_all_processes (void)
1117{
1118 stopping_threads = 1;
1119 for_each_inferior (&all_processes, send_sigstop);
1120 for_each_inferior (&all_processes, wait_for_sigstop);
1121 stopping_threads = 0;
1122}
1123
da6d8c04
DJ
1124/* Resume execution of the inferior process.
1125 If STEP is nonzero, single-step it.
1126 If SIGNAL is nonzero, give it that signal. */
1127
ce3a066d 1128static void
0d62e5e8 1129linux_resume_one_process (struct inferior_list_entry *entry,
32ca6d61 1130 int step, int signal, siginfo_t *info)
da6d8c04 1131{
0d62e5e8
DJ
1132 struct process_info *process = (struct process_info *) entry;
1133 struct thread_info *saved_inferior;
1134
1135 if (process->stopped == 0)
1136 return;
1137
1138 /* If we have pending signals or status, and a new signal, enqueue the
1139 signal. Also enqueue the signal if we are waiting to reinsert a
1140 breakpoint; it will be picked up again below. */
1141 if (signal != 0
1142 && (process->status_pending_p || process->pending_signals != NULL
1143 || process->bp_reinsert != 0))
1144 {
1145 struct pending_signals *p_sig;
bca929d3 1146 p_sig = xmalloc (sizeof (*p_sig));
0d62e5e8
DJ
1147 p_sig->prev = process->pending_signals;
1148 p_sig->signal = signal;
32ca6d61
DJ
1149 if (info == NULL)
1150 memset (&p_sig->info, 0, sizeof (siginfo_t));
1151 else
1152 memcpy (&p_sig->info, info, sizeof (siginfo_t));
0d62e5e8
DJ
1153 process->pending_signals = p_sig;
1154 }
1155
e5379b03 1156 if (process->status_pending_p && !check_removed_breakpoint (process))
0d62e5e8
DJ
1157 return;
1158
1159 saved_inferior = current_inferior;
1160 current_inferior = get_process_thread (process);
1161
1162 if (debug_threads)
a1928bad 1163 fprintf (stderr, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid,
0d62e5e8
DJ
1164 step ? "step" : "continue", signal,
1165 process->stop_expected ? "expected" : "not expected");
1166
1167 /* This bit needs some thinking about. If we get a signal that
1168 we must report while a single-step reinsert is still pending,
1169 we often end up resuming the thread. It might be better to
1170 (ew) allow a stack of pending events; then we could be sure that
1171 the reinsert happened right away and not lose any signals.
1172
1173 Making this stack would also shrink the window in which breakpoints are
1174 uninserted (see comment in linux_wait_for_process) but not enough for
1175 complete correctness, so it won't solve that problem. It may be
1176 worthwhile just to solve this one, however. */
1177 if (process->bp_reinsert != 0)
1178 {
1179 if (debug_threads)
1180 fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert);
1181 if (step == 0)
1182 fprintf (stderr, "BAD - reinserting but not stepping.\n");
1183 step = 1;
1184
1185 /* Postpone any pending signal. It was enqueued above. */
1186 signal = 0;
1187 }
1188
1189 check_removed_breakpoint (process);
1190
aa691b87 1191 if (debug_threads && the_low_target.get_pc != NULL)
0d62e5e8
DJ
1192 {
1193 fprintf (stderr, " ");
52fb6437 1194 (*the_low_target.get_pc) ();
0d62e5e8
DJ
1195 }
1196
1197 /* If we have pending signals, consume one unless we are trying to reinsert
1198 a breakpoint. */
1199 if (process->pending_signals != NULL && process->bp_reinsert == 0)
1200 {
1201 struct pending_signals **p_sig;
1202
1203 p_sig = &process->pending_signals;
1204 while ((*p_sig)->prev != NULL)
1205 p_sig = &(*p_sig)->prev;
1206
1207 signal = (*p_sig)->signal;
32ca6d61
DJ
1208 if ((*p_sig)->info.si_signo != 0)
1209 ptrace (PTRACE_SETSIGINFO, process->lwpid, 0, &(*p_sig)->info);
1210
0d62e5e8
DJ
1211 free (*p_sig);
1212 *p_sig = NULL;
1213 }
1214
1215 regcache_invalidate_one ((struct inferior_list_entry *)
1216 get_process_thread (process));
da6d8c04 1217 errno = 0;
0d62e5e8
DJ
1218 process->stopped = 0;
1219 process->stepping = step;
1220 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal);
1221
1222 current_inferior = saved_inferior;
da6d8c04 1223 if (errno)
3221518c
UW
1224 {
1225 /* ESRCH from ptrace either means that the thread was already
1226 running (an error) or that it is gone (a race condition). If
1227 it's gone, we will get a notification the next time we wait,
1228 so we can ignore the error. We could differentiate these
1229 two, but it's tricky without waiting; the thread still exists
1230 as a zombie, so sending it signal 0 would succeed. So just
1231 ignore ESRCH. */
1232 if (errno == ESRCH)
1233 return;
1234
1235 perror_with_name ("ptrace");
1236 }
da6d8c04
DJ
1237}
1238
64386c31
DJ
1239static struct thread_resume *resume_ptr;
1240
1241/* This function is called once per thread. We look up the thread
5544ad89
DJ
1242 in RESUME_PTR, and mark the thread with a pointer to the appropriate
1243 resume request.
1244
1245 This algorithm is O(threads * resume elements), but resume elements
1246 is small (and will remain small at least until GDB supports thread
1247 suspension). */
0d62e5e8 1248static void
5544ad89 1249linux_set_resume_request (struct inferior_list_entry *entry)
0d62e5e8
DJ
1250{
1251 struct process_info *process;
64386c31 1252 struct thread_info *thread;
5544ad89 1253 int ndx;
64386c31
DJ
1254
1255 thread = (struct thread_info *) entry;
1256 process = get_thread_process (thread);
1257
1258 ndx = 0;
1259 while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id)
1260 ndx++;
1261
5544ad89
DJ
1262 process->resume = &resume_ptr[ndx];
1263}
1264
1265/* This function is called once per thread. We check the thread's resume
1266 request, which will tell us whether to resume, step, or leave the thread
1267 stopped; and what signal, if any, it should be sent. For threads which
1268 we aren't explicitly told otherwise, we preserve the stepping flag; this
1269 is used for stepping over gdbserver-placed breakpoints. */
1270
1271static void
1272linux_continue_one_thread (struct inferior_list_entry *entry)
1273{
1274 struct process_info *process;
1275 struct thread_info *thread;
1276 int step;
1277
1278 thread = (struct thread_info *) entry;
1279 process = get_thread_process (thread);
1280
1281 if (process->resume->leave_stopped)
64386c31
DJ
1282 return;
1283
5544ad89
DJ
1284 if (process->resume->thread == -1)
1285 step = process->stepping || process->resume->step;
64386c31 1286 else
5544ad89
DJ
1287 step = process->resume->step;
1288
32ca6d61 1289 linux_resume_one_process (&process->head, step, process->resume->sig, NULL);
c6ecbae5 1290
5544ad89
DJ
1291 process->resume = NULL;
1292}
1293
1294/* This function is called once per thread. We check the thread's resume
1295 request, which will tell us whether to resume, step, or leave the thread
1296 stopped; and what signal, if any, it should be sent. We queue any needed
1297 signals, since we won't actually resume. We already have a pending event
1298 to report, so we don't need to preserve any step requests; they should
1299 be re-issued if necessary. */
1300
1301static void
1302linux_queue_one_thread (struct inferior_list_entry *entry)
1303{
1304 struct process_info *process;
1305 struct thread_info *thread;
1306
1307 thread = (struct thread_info *) entry;
1308 process = get_thread_process (thread);
1309
1310 if (process->resume->leave_stopped)
1311 return;
1312
1313 /* If we have a new signal, enqueue the signal. */
1314 if (process->resume->sig != 0)
1315 {
1316 struct pending_signals *p_sig;
bca929d3 1317 p_sig = xmalloc (sizeof (*p_sig));
5544ad89
DJ
1318 p_sig->prev = process->pending_signals;
1319 p_sig->signal = process->resume->sig;
32ca6d61
DJ
1320 memset (&p_sig->info, 0, sizeof (siginfo_t));
1321
1322 /* If this is the same signal we were previously stopped by,
1323 make sure to queue its siginfo. We can ignore the return
1324 value of ptrace; if it fails, we'll skip
1325 PTRACE_SETSIGINFO. */
1326 if (WIFSTOPPED (process->last_status)
1327 && WSTOPSIG (process->last_status) == process->resume->sig)
1328 ptrace (PTRACE_GETSIGINFO, process->lwpid, 0, &p_sig->info);
1329
5544ad89
DJ
1330 process->pending_signals = p_sig;
1331 }
1332
1333 process->resume = NULL;
1334}
1335
1336/* Set DUMMY if this process has an interesting status pending. */
1337static int
1338resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
1339{
1340 struct process_info *process = (struct process_info *) entry;
1341
1342 /* Processes which will not be resumed are not interesting, because
1343 we might not wait for them next time through linux_wait. */
1344 if (process->resume->leave_stopped)
1345 return 0;
1346
1347 /* If this thread has a removed breakpoint, we won't have any
1348 events to report later, so check now. check_removed_breakpoint
1349 may clear status_pending_p. We avoid calling check_removed_breakpoint
1350 for any thread that we are not otherwise going to resume - this
1351 lets us preserve stopped status when two threads hit a breakpoint.
1352 GDB removes the breakpoint to single-step a particular thread
1353 past it, then re-inserts it and resumes all threads. We want
1354 to report the second thread without resuming it in the interim. */
1355 if (process->status_pending_p)
1356 check_removed_breakpoint (process);
1357
1358 if (process->status_pending_p)
1359 * (int *) flag_p = 1;
1360
1361 return 0;
0d62e5e8
DJ
1362}
1363
1364static void
64386c31 1365linux_resume (struct thread_resume *resume_info)
0d62e5e8 1366{
5544ad89 1367 int pending_flag;
c6ecbae5 1368
5544ad89 1369 /* Yes, the use of a global here is rather ugly. */
64386c31 1370 resume_ptr = resume_info;
5544ad89
DJ
1371
1372 for_each_inferior (&all_threads, linux_set_resume_request);
1373
1374 /* If there is a thread which would otherwise be resumed, which
1375 has a pending status, then don't resume any threads - we can just
1376 report the pending status. Make sure to queue any signals
1377 that would otherwise be sent. */
1378 pending_flag = 0;
1379 find_inferior (&all_processes, resume_status_pending_p, &pending_flag);
1380
1381 if (debug_threads)
1382 {
1383 if (pending_flag)
1384 fprintf (stderr, "Not resuming, pending status\n");
1385 else
1386 fprintf (stderr, "Resuming, no pending status\n");
1387 }
1388
1389 if (pending_flag)
1390 for_each_inferior (&all_threads, linux_queue_one_thread);
1391 else
a20d5e98 1392 for_each_inferior (&all_threads, linux_continue_one_thread);
0d62e5e8
DJ
1393}
1394
1395#ifdef HAVE_LINUX_USRREGS
da6d8c04
DJ
1396
1397int
0a30fbc4 1398register_addr (int regnum)
da6d8c04
DJ
1399{
1400 int addr;
1401
2ec06d2e 1402 if (regnum < 0 || regnum >= the_low_target.num_regs)
da6d8c04
DJ
1403 error ("Invalid register number %d.", regnum);
1404
2ec06d2e 1405 addr = the_low_target.regmap[regnum];
da6d8c04
DJ
1406
1407 return addr;
1408}
1409
58caa3dc 1410/* Fetch one register. */
da6d8c04
DJ
1411static void
1412fetch_register (int regno)
1413{
1414 CORE_ADDR regaddr;
48d93c75 1415 int i, size;
0d62e5e8 1416 char *buf;
da6d8c04 1417
2ec06d2e 1418 if (regno >= the_low_target.num_regs)
0a30fbc4 1419 return;
2ec06d2e 1420 if ((*the_low_target.cannot_fetch_register) (regno))
0a30fbc4 1421 return;
da6d8c04 1422
0a30fbc4
DJ
1423 regaddr = register_addr (regno);
1424 if (regaddr == -1)
1425 return;
48d93c75
UW
1426 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
1427 & - sizeof (PTRACE_XFER_TYPE);
1428 buf = alloca (size);
1429 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04
DJ
1430 {
1431 errno = 0;
0d62e5e8 1432 *(PTRACE_XFER_TYPE *) (buf + i) =
da6d8c04
DJ
1433 ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
1434 regaddr += sizeof (PTRACE_XFER_TYPE);
1435 if (errno != 0)
1436 {
1437 /* Warning, not error, in case we are attached; sometimes the
1438 kernel doesn't let us at the registers. */
1439 char *err = strerror (errno);
1440 char *msg = alloca (strlen (err) + 128);
1441 sprintf (msg, "reading register %d: %s", regno, err);
1442 error (msg);
1443 goto error_exit;
1444 }
1445 }
ee1a7ae4
UW
1446
1447 if (the_low_target.supply_ptrace_register)
1448 the_low_target.supply_ptrace_register (regno, buf);
5a1f5858
DJ
1449 else
1450 supply_register (regno, buf);
0d62e5e8 1451
da6d8c04
DJ
1452error_exit:;
1453}
1454
1455/* Fetch all registers, or just one, from the child process. */
58caa3dc
DJ
1456static void
1457usr_fetch_inferior_registers (int regno)
da6d8c04
DJ
1458{
1459 if (regno == -1 || regno == 0)
2ec06d2e 1460 for (regno = 0; regno < the_low_target.num_regs; regno++)
da6d8c04
DJ
1461 fetch_register (regno);
1462 else
1463 fetch_register (regno);
1464}
1465
1466/* Store our register values back into the inferior.
1467 If REGNO is -1, do this for all registers.
1468 Otherwise, REGNO specifies which register (so we can save time). */
58caa3dc
DJ
1469static void
1470usr_store_inferior_registers (int regno)
da6d8c04
DJ
1471{
1472 CORE_ADDR regaddr;
48d93c75 1473 int i, size;
0d62e5e8 1474 char *buf;
da6d8c04
DJ
1475
1476 if (regno >= 0)
1477 {
2ec06d2e 1478 if (regno >= the_low_target.num_regs)
0a30fbc4
DJ
1479 return;
1480
bc1e36ca 1481 if ((*the_low_target.cannot_store_register) (regno) == 1)
0a30fbc4
DJ
1482 return;
1483
1484 regaddr = register_addr (regno);
1485 if (regaddr == -1)
da6d8c04 1486 return;
da6d8c04 1487 errno = 0;
48d93c75
UW
1488 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
1489 & - sizeof (PTRACE_XFER_TYPE);
1490 buf = alloca (size);
1491 memset (buf, 0, size);
ee1a7ae4
UW
1492
1493 if (the_low_target.collect_ptrace_register)
1494 the_low_target.collect_ptrace_register (regno, buf);
5a1f5858
DJ
1495 else
1496 collect_register (regno, buf);
ee1a7ae4 1497
48d93c75 1498 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04 1499 {
0a30fbc4
DJ
1500 errno = 0;
1501 ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
2ff29de4 1502 *(PTRACE_XFER_TYPE *) (buf + i));
da6d8c04
DJ
1503 if (errno != 0)
1504 {
3221518c
UW
1505 /* At this point, ESRCH should mean the process is already gone,
1506 in which case we simply ignore attempts to change its registers.
1507 See also the related comment in linux_resume_one_process. */
1508 if (errno == ESRCH)
1509 return;
1510
bc1e36ca
DJ
1511 if ((*the_low_target.cannot_store_register) (regno) == 0)
1512 {
1513 char *err = strerror (errno);
1514 char *msg = alloca (strlen (err) + 128);
1515 sprintf (msg, "writing register %d: %s",
1516 regno, err);
1517 error (msg);
1518 return;
1519 }
da6d8c04 1520 }
2ff29de4 1521 regaddr += sizeof (PTRACE_XFER_TYPE);
da6d8c04 1522 }
da6d8c04
DJ
1523 }
1524 else
2ec06d2e 1525 for (regno = 0; regno < the_low_target.num_regs; regno++)
0d62e5e8 1526 usr_store_inferior_registers (regno);
da6d8c04 1527}
58caa3dc
DJ
1528#endif /* HAVE_LINUX_USRREGS */
1529
1530
1531
1532#ifdef HAVE_LINUX_REGSETS
1533
1534static int
0d62e5e8 1535regsets_fetch_inferior_registers ()
58caa3dc
DJ
1536{
1537 struct regset_info *regset;
e9d25b98 1538 int saw_general_regs = 0;
58caa3dc
DJ
1539
1540 regset = target_regsets;
1541
1542 while (regset->size >= 0)
1543 {
1544 void *buf;
1545 int res;
1546
52fa2412 1547 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
58caa3dc
DJ
1548 {
1549 regset ++;
1550 continue;
1551 }
1552
bca929d3 1553 buf = xmalloc (regset->size);
dfb64f85 1554#ifndef __sparc__
d06f167a 1555 res = ptrace (regset->get_request, inferior_pid, 0, buf);
dfb64f85
DJ
1556#else
1557 res = ptrace (regset->get_request, inferior_pid, buf, 0);
1558#endif
58caa3dc
DJ
1559 if (res < 0)
1560 {
1561 if (errno == EIO)
1562 {
52fa2412
UW
1563 /* If we get EIO on a regset, do not try it again for
1564 this process. */
1565 disabled_regsets[regset - target_regsets] = 1;
1566 continue;
58caa3dc
DJ
1567 }
1568 else
1569 {
0d62e5e8 1570 char s[256];
a1928bad 1571 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
0d62e5e8
DJ
1572 inferior_pid);
1573 perror (s);
58caa3dc
DJ
1574 }
1575 }
e9d25b98
DJ
1576 else if (regset->type == GENERAL_REGS)
1577 saw_general_regs = 1;
58caa3dc
DJ
1578 regset->store_function (buf);
1579 regset ++;
1580 }
e9d25b98
DJ
1581 if (saw_general_regs)
1582 return 0;
1583 else
1584 return 1;
58caa3dc
DJ
1585}
1586
1587static int
0d62e5e8 1588regsets_store_inferior_registers ()
58caa3dc
DJ
1589{
1590 struct regset_info *regset;
e9d25b98 1591 int saw_general_regs = 0;
58caa3dc
DJ
1592
1593 regset = target_regsets;
1594
1595 while (regset->size >= 0)
1596 {
1597 void *buf;
1598 int res;
1599
52fa2412 1600 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
58caa3dc
DJ
1601 {
1602 regset ++;
1603 continue;
1604 }
1605
bca929d3 1606 buf = xmalloc (regset->size);
545587ee
DJ
1607
1608 /* First fill the buffer with the current register set contents,
1609 in case there are any items in the kernel's regset that are
1610 not in gdbserver's regcache. */
dfb64f85 1611#ifndef __sparc__
545587ee 1612 res = ptrace (regset->get_request, inferior_pid, 0, buf);
dfb64f85
DJ
1613#else
1614 res = ptrace (regset->get_request, inferior_pid, buf, 0);
1615#endif
545587ee
DJ
1616
1617 if (res == 0)
1618 {
1619 /* Then overlay our cached registers on that. */
1620 regset->fill_function (buf);
1621
1622 /* Only now do we write the register set. */
dfb64f85
DJ
1623#ifndef __sparc__
1624 res = ptrace (regset->set_request, inferior_pid, 0, buf);
1625#else
1626 res = ptrace (regset->set_request, inferior_pid, buf, 0);
1627#endif
545587ee
DJ
1628 }
1629
58caa3dc
DJ
1630 if (res < 0)
1631 {
1632 if (errno == EIO)
1633 {
52fa2412
UW
1634 /* If we get EIO on a regset, do not try it again for
1635 this process. */
1636 disabled_regsets[regset - target_regsets] = 1;
1637 continue;
58caa3dc 1638 }
3221518c
UW
1639 else if (errno == ESRCH)
1640 {
1641 /* At this point, ESRCH should mean the process is already gone,
1642 in which case we simply ignore attempts to change its registers.
1643 See also the related comment in linux_resume_one_process. */
1644 return 0;
1645 }
58caa3dc
DJ
1646 else
1647 {
ce3a066d 1648 perror ("Warning: ptrace(regsets_store_inferior_registers)");
58caa3dc
DJ
1649 }
1650 }
e9d25b98
DJ
1651 else if (regset->type == GENERAL_REGS)
1652 saw_general_regs = 1;
58caa3dc 1653 regset ++;
09ec9b38 1654 free (buf);
58caa3dc 1655 }
e9d25b98
DJ
1656 if (saw_general_regs)
1657 return 0;
1658 else
1659 return 1;
ce3a066d 1660 return 0;
58caa3dc
DJ
1661}
1662
1663#endif /* HAVE_LINUX_REGSETS */
1664
1665
1666void
ce3a066d 1667linux_fetch_registers (int regno)
58caa3dc
DJ
1668{
1669#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
1670 if (regsets_fetch_inferior_registers () == 0)
1671 return;
58caa3dc
DJ
1672#endif
1673#ifdef HAVE_LINUX_USRREGS
1674 usr_fetch_inferior_registers (regno);
1675#endif
1676}
1677
1678void
ce3a066d 1679linux_store_registers (int regno)
58caa3dc
DJ
1680{
1681#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
1682 if (regsets_store_inferior_registers () == 0)
1683 return;
58caa3dc
DJ
1684#endif
1685#ifdef HAVE_LINUX_USRREGS
1686 usr_store_inferior_registers (regno);
1687#endif
1688}
1689
da6d8c04 1690
da6d8c04
DJ
1691/* Copy LEN bytes from inferior's memory starting at MEMADDR
1692 to debugger memory starting at MYADDR. */
1693
c3e735a6 1694static int
f450004a 1695linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
da6d8c04
DJ
1696{
1697 register int i;
1698 /* Round starting address down to longword boundary. */
1699 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1700 /* Round ending address up; get number of longwords that makes. */
aa691b87
RM
1701 register int count
1702 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
da6d8c04
DJ
1703 / sizeof (PTRACE_XFER_TYPE);
1704 /* Allocate buffer of that many longwords. */
aa691b87 1705 register PTRACE_XFER_TYPE *buffer
da6d8c04 1706 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
fd462a61
DJ
1707 int fd;
1708 char filename[64];
1709
1710 /* Try using /proc. Don't bother for one word. */
1711 if (len >= 3 * sizeof (long))
1712 {
1713 /* We could keep this file open and cache it - possibly one per
1714 thread. That requires some juggling, but is even faster. */
1715 sprintf (filename, "/proc/%ld/mem", inferior_pid);
1716 fd = open (filename, O_RDONLY | O_LARGEFILE);
1717 if (fd == -1)
1718 goto no_proc;
1719
1720 /* If pread64 is available, use it. It's faster if the kernel
1721 supports it (only one syscall), and it's 64-bit safe even on
1722 32-bit platforms (for instance, SPARC debugging a SPARC64
1723 application). */
1724#ifdef HAVE_PREAD64
1725 if (pread64 (fd, myaddr, len, memaddr) != len)
1726#else
1727 if (lseek (fd, memaddr, SEEK_SET) == -1 || read (fd, memaddr, len) != len)
1728#endif
1729 {
1730 close (fd);
1731 goto no_proc;
1732 }
1733
1734 close (fd);
1735 return 0;
1736 }
da6d8c04 1737
fd462a61 1738 no_proc:
da6d8c04
DJ
1739 /* Read all the longwords */
1740 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1741 {
c3e735a6 1742 errno = 0;
d844cde6 1743 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
c3e735a6
DJ
1744 if (errno)
1745 return errno;
da6d8c04
DJ
1746 }
1747
1748 /* Copy appropriate bytes out of the buffer. */
1749 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
c3e735a6
DJ
1750
1751 return 0;
da6d8c04
DJ
1752}
1753
1754/* Copy LEN bytes of data from debugger memory at MYADDR
1755 to inferior's memory at MEMADDR.
1756 On failure (cannot write the inferior)
1757 returns the value of errno. */
1758
ce3a066d 1759static int
f450004a 1760linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
da6d8c04
DJ
1761{
1762 register int i;
1763 /* Round starting address down to longword boundary. */
1764 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1765 /* Round ending address up; get number of longwords that makes. */
1766 register int count
1767 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
1768 /* Allocate buffer of that many longwords. */
1769 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
da6d8c04 1770
0d62e5e8
DJ
1771 if (debug_threads)
1772 {
1773 fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
1774 }
1775
da6d8c04
DJ
1776 /* Fill start and end extra bytes of buffer with existing memory data. */
1777
d844cde6
DJ
1778 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
1779 (PTRACE_ARG3_TYPE) addr, 0);
da6d8c04
DJ
1780
1781 if (count > 1)
1782 {
1783 buffer[count - 1]
1784 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
d844cde6
DJ
1785 (PTRACE_ARG3_TYPE) (addr + (count - 1)
1786 * sizeof (PTRACE_XFER_TYPE)),
1787 0);
da6d8c04
DJ
1788 }
1789
1790 /* Copy data to be written over corresponding part of buffer */
1791
1792 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
1793
1794 /* Write the entire buffer. */
1795
1796 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1797 {
1798 errno = 0;
d844cde6 1799 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
da6d8c04
DJ
1800 if (errno)
1801 return errno;
1802 }
1803
1804 return 0;
1805}
2f2893d9 1806
24a09b5f
DJ
1807static int linux_supports_tracefork_flag;
1808
51c2684e 1809/* Helper functions for linux_test_for_tracefork, called via clone (). */
24a09b5f 1810
51c2684e
DJ
1811static int
1812linux_tracefork_grandchild (void *arg)
1813{
1814 _exit (0);
1815}
1816
7407e2de
AS
1817#define STACK_SIZE 4096
1818
51c2684e
DJ
1819static int
1820linux_tracefork_child (void *arg)
24a09b5f
DJ
1821{
1822 ptrace (PTRACE_TRACEME, 0, 0, 0);
1823 kill (getpid (), SIGSTOP);
7407e2de
AS
1824#ifdef __ia64__
1825 __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE,
1826 CLONE_VM | SIGCHLD, NULL);
1827#else
1828 clone (linux_tracefork_grandchild, arg + STACK_SIZE,
1829 CLONE_VM | SIGCHLD, NULL);
1830#endif
24a09b5f
DJ
1831 _exit (0);
1832}
1833
1834/* Wrapper function for waitpid which handles EINTR. */
1835
1836static int
1837my_waitpid (int pid, int *status, int flags)
1838{
1839 int ret;
1840 do
1841 {
1842 ret = waitpid (pid, status, flags);
1843 }
1844 while (ret == -1 && errno == EINTR);
1845
1846 return ret;
1847}
1848
1849/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
1850 sure that we can enable the option, and that it had the desired
1851 effect. */
1852
1853static void
1854linux_test_for_tracefork (void)
1855{
1856 int child_pid, ret, status;
1857 long second_pid;
bca929d3 1858 char *stack = xmalloc (STACK_SIZE * 4);
24a09b5f
DJ
1859
1860 linux_supports_tracefork_flag = 0;
1861
51c2684e 1862 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
7407e2de
AS
1863#ifdef __ia64__
1864 child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE,
1865 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
1866#else
1867 child_pid = clone (linux_tracefork_child, stack + STACK_SIZE,
1868 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
1869#endif
24a09b5f 1870 if (child_pid == -1)
51c2684e 1871 perror_with_name ("clone");
24a09b5f
DJ
1872
1873 ret = my_waitpid (child_pid, &status, 0);
1874 if (ret == -1)
1875 perror_with_name ("waitpid");
1876 else if (ret != child_pid)
1877 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
1878 if (! WIFSTOPPED (status))
1879 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
1880
1881 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
1882 if (ret != 0)
1883 {
1884 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
1885 if (ret != 0)
1886 {
1887 warning ("linux_test_for_tracefork: failed to kill child");
1888 return;
1889 }
1890
1891 ret = my_waitpid (child_pid, &status, 0);
1892 if (ret != child_pid)
1893 warning ("linux_test_for_tracefork: failed to wait for killed child");
1894 else if (!WIFSIGNALED (status))
1895 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
1896 "killed child", status);
1897
1898 return;
1899 }
1900
1901 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
1902 if (ret != 0)
1903 warning ("linux_test_for_tracefork: failed to resume child");
1904
1905 ret = my_waitpid (child_pid, &status, 0);
1906
1907 if (ret == child_pid && WIFSTOPPED (status)
1908 && status >> 16 == PTRACE_EVENT_FORK)
1909 {
1910 second_pid = 0;
1911 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
1912 if (ret == 0 && second_pid != 0)
1913 {
1914 int second_status;
1915
1916 linux_supports_tracefork_flag = 1;
1917 my_waitpid (second_pid, &second_status, 0);
1918 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
1919 if (ret != 0)
1920 warning ("linux_test_for_tracefork: failed to kill second child");
1921 my_waitpid (second_pid, &status, 0);
1922 }
1923 }
1924 else
1925 warning ("linux_test_for_tracefork: unexpected result from waitpid "
1926 "(%d, status 0x%x)", ret, status);
1927
1928 do
1929 {
1930 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
1931 if (ret != 0)
1932 warning ("linux_test_for_tracefork: failed to kill child");
1933 my_waitpid (child_pid, &status, 0);
1934 }
1935 while (WIFSTOPPED (status));
51c2684e
DJ
1936
1937 free (stack);
24a09b5f
DJ
1938}
1939
1940
2f2893d9
DJ
1941static void
1942linux_look_up_symbols (void)
1943{
0d62e5e8 1944#ifdef USE_THREAD_DB
24a09b5f 1945 if (thread_db_active)
0d62e5e8
DJ
1946 return;
1947
24a09b5f 1948 thread_db_active = thread_db_init (!linux_supports_tracefork_flag);
0d62e5e8
DJ
1949#endif
1950}
1951
e5379b03 1952static void
ef57601b 1953linux_request_interrupt (void)
e5379b03 1954{
a1928bad 1955 extern unsigned long signal_pid;
e5379b03 1956
d592fa2f 1957 if (cont_thread != 0 && cont_thread != -1)
e5379b03
DJ
1958 {
1959 struct process_info *process;
1960
1961 process = get_thread_process (current_inferior);
ef57601b 1962 kill_lwp (process->lwpid, SIGINT);
e5379b03
DJ
1963 }
1964 else
ef57601b 1965 kill_lwp (signal_pid, SIGINT);
e5379b03
DJ
1966}
1967
aa691b87
RM
1968/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1969 to debugger memory starting at MYADDR. */
1970
1971static int
f450004a 1972linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
aa691b87
RM
1973{
1974 char filename[PATH_MAX];
1975 int fd, n;
1976
a1928bad 1977 snprintf (filename, sizeof filename, "/proc/%ld/auxv", inferior_pid);
aa691b87
RM
1978
1979 fd = open (filename, O_RDONLY);
1980 if (fd < 0)
1981 return -1;
1982
1983 if (offset != (CORE_ADDR) 0
1984 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
1985 n = -1;
1986 else
1987 n = read (fd, myaddr, len);
1988
1989 close (fd);
1990
1991 return n;
1992}
1993
e013ee27
OF
1994/* These watchpoint related wrapper functions simply pass on the function call
1995 if the target has registered a corresponding function. */
1996
1997static int
1998linux_insert_watchpoint (char type, CORE_ADDR addr, int len)
1999{
2000 if (the_low_target.insert_watchpoint != NULL)
2001 return the_low_target.insert_watchpoint (type, addr, len);
2002 else
2003 /* Unsupported (see target.h). */
2004 return 1;
2005}
2006
2007static int
2008linux_remove_watchpoint (char type, CORE_ADDR addr, int len)
2009{
2010 if (the_low_target.remove_watchpoint != NULL)
2011 return the_low_target.remove_watchpoint (type, addr, len);
2012 else
2013 /* Unsupported (see target.h). */
2014 return 1;
2015}
2016
2017static int
2018linux_stopped_by_watchpoint (void)
2019{
2020 if (the_low_target.stopped_by_watchpoint != NULL)
2021 return the_low_target.stopped_by_watchpoint ();
2022 else
2023 return 0;
2024}
2025
2026static CORE_ADDR
2027linux_stopped_data_address (void)
2028{
2029 if (the_low_target.stopped_data_address != NULL)
2030 return the_low_target.stopped_data_address ();
2031 else
2032 return 0;
2033}
2034
42c81e2a 2035#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
2036#if defined(__mcoldfire__)
2037/* These should really be defined in the kernel's ptrace.h header. */
2038#define PT_TEXT_ADDR 49*4
2039#define PT_DATA_ADDR 50*4
2040#define PT_TEXT_END_ADDR 51*4
2041#endif
2042
2043/* Under uClinux, programs are loaded at non-zero offsets, which we need
2044 to tell gdb about. */
2045
2046static int
2047linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
2048{
2049#if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
2050 unsigned long text, text_end, data;
2051 int pid = get_thread_process (current_inferior)->head.id;
2052
2053 errno = 0;
2054
2055 text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0);
2056 text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0);
2057 data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0);
2058
2059 if (errno == 0)
2060 {
2061 /* Both text and data offsets produced at compile-time (and so
2062 used by gdb) are relative to the beginning of the program,
2063 with the data segment immediately following the text segment.
2064 However, the actual runtime layout in memory may put the data
2065 somewhere else, so when we send gdb a data base-address, we
2066 use the real data base address and subtract the compile-time
2067 data base-address from it (which is just the length of the
2068 text segment). BSS immediately follows data in both
2069 cases. */
2070 *text_p = text;
2071 *data_p = data - (text_end - text);
2072
2073 return 1;
2074 }
2075#endif
2076 return 0;
2077}
2078#endif
2079
07e059b5
VP
2080static int
2081linux_qxfer_osdata (const char *annex,
2082 unsigned char *readbuf, unsigned const char *writebuf,
2083 CORE_ADDR offset, int len)
2084{
2085 /* We make the process list snapshot when the object starts to be
2086 read. */
2087 static const char *buf;
2088 static long len_avail = -1;
2089 static struct buffer buffer;
2090
2091 DIR *dirp;
2092
2093 if (strcmp (annex, "processes") != 0)
2094 return 0;
2095
2096 if (!readbuf || writebuf)
2097 return 0;
2098
2099 if (offset == 0)
2100 {
2101 if (len_avail != -1 && len_avail != 0)
2102 buffer_free (&buffer);
2103 len_avail = 0;
2104 buf = NULL;
2105 buffer_init (&buffer);
2106 buffer_grow_str (&buffer, "<osdata type=\"processes\">");
2107
2108 dirp = opendir ("/proc");
2109 if (dirp)
2110 {
2111 struct dirent *dp;
2112 while ((dp = readdir (dirp)) != NULL)
2113 {
2114 struct stat statbuf;
2115 char procentry[sizeof ("/proc/4294967295")];
2116
2117 if (!isdigit (dp->d_name[0])
2118 || strlen (dp->d_name) > sizeof ("4294967295") - 1)
2119 continue;
2120
2121 sprintf (procentry, "/proc/%s", dp->d_name);
2122 if (stat (procentry, &statbuf) == 0
2123 && S_ISDIR (statbuf.st_mode))
2124 {
2125 char pathname[128];
2126 FILE *f;
2127 char cmd[MAXPATHLEN + 1];
2128 struct passwd *entry;
2129
2130 sprintf (pathname, "/proc/%s/cmdline", dp->d_name);
2131 entry = getpwuid (statbuf.st_uid);
2132
2133 if ((f = fopen (pathname, "r")) != NULL)
2134 {
2135 size_t len = fread (cmd, 1, sizeof (cmd) - 1, f);
2136 if (len > 0)
2137 {
2138 int i;
2139 for (i = 0; i < len; i++)
2140 if (cmd[i] == '\0')
2141 cmd[i] = ' ';
2142 cmd[len] = '\0';
2143
2144 buffer_xml_printf (
2145 &buffer,
2146 "<item>"
2147 "<column name=\"pid\">%s</column>"
2148 "<column name=\"user\">%s</column>"
2149 "<column name=\"command\">%s</column>"
2150 "</item>",
2151 dp->d_name,
2152 entry ? entry->pw_name : "?",
2153 cmd);
2154 }
2155 fclose (f);
2156 }
2157 }
2158 }
2159
2160 closedir (dirp);
2161 }
2162 buffer_grow_str0 (&buffer, "</osdata>\n");
2163 buf = buffer_finish (&buffer);
2164 len_avail = strlen (buf);
2165 }
2166
2167 if (offset >= len_avail)
2168 {
2169 /* Done. Get rid of the data. */
2170 buffer_free (&buffer);
2171 buf = NULL;
2172 len_avail = 0;
2173 return 0;
2174 }
2175
2176 if (len > len_avail - offset)
2177 len = len_avail - offset;
2178 memcpy (readbuf, buf + offset, len);
2179
2180 return len;
2181}
2182
ce3a066d
DJ
2183static struct target_ops linux_target_ops = {
2184 linux_create_inferior,
2185 linux_attach,
2186 linux_kill,
6ad8ae5c 2187 linux_detach,
444d6139 2188 linux_join,
ce3a066d
DJ
2189 linux_thread_alive,
2190 linux_resume,
2191 linux_wait,
2192 linux_fetch_registers,
2193 linux_store_registers,
2194 linux_read_memory,
2195 linux_write_memory,
2f2893d9 2196 linux_look_up_symbols,
ef57601b 2197 linux_request_interrupt,
aa691b87 2198 linux_read_auxv,
e013ee27
OF
2199 linux_insert_watchpoint,
2200 linux_remove_watchpoint,
2201 linux_stopped_by_watchpoint,
2202 linux_stopped_data_address,
42c81e2a 2203#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437 2204 linux_read_offsets,
dae5f5cf
DJ
2205#else
2206 NULL,
2207#endif
2208#ifdef USE_THREAD_DB
2209 thread_db_get_tls_address,
2210#else
2211 NULL,
52fb6437 2212#endif
59a016f0
PA
2213 NULL,
2214 hostio_last_error_from_errno,
07e059b5 2215 linux_qxfer_osdata,
ce3a066d
DJ
2216};
2217
0d62e5e8
DJ
2218static void
2219linux_init_signals ()
2220{
2221 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
2222 to find what the cancel signal actually is. */
254787d4 2223 signal (__SIGRTMIN+1, SIG_IGN);
0d62e5e8
DJ
2224}
2225
da6d8c04
DJ
2226void
2227initialize_low (void)
2228{
24a09b5f 2229 thread_db_active = 0;
ce3a066d 2230 set_target_ops (&linux_target_ops);
611cb4a5
DJ
2231 set_breakpoint_data (the_low_target.breakpoint,
2232 the_low_target.breakpoint_len);
0d62e5e8 2233 linux_init_signals ();
24a09b5f 2234 linux_test_for_tracefork ();
52fa2412
UW
2235#ifdef HAVE_LINUX_REGSETS
2236 for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++)
2237 ;
bca929d3 2238 disabled_regsets = xmalloc (num_regsets);
52fa2412 2239#endif
da6d8c04 2240}