]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/linux-low.c
* configure.ac: Switch license to GPLv3.
[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,
6aba47ca 3 2006, 2007 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
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
6f0f660e
EZ
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
da6d8c04
DJ
21
22#include "server.h"
58caa3dc 23#include "linux-low.h"
da6d8c04 24
58caa3dc 25#include <sys/wait.h>
da6d8c04
DJ
26#include <stdio.h>
27#include <sys/param.h>
28#include <sys/dir.h>
29#include <sys/ptrace.h>
30#include <sys/user.h>
31#include <signal.h>
32#include <sys/ioctl.h>
33#include <fcntl.h>
d07c63e7 34#include <string.h>
0a30fbc4
DJ
35#include <stdlib.h>
36#include <unistd.h>
fa6a77dc 37#include <errno.h>
fd500816 38#include <sys/syscall.h>
da6d8c04 39
32ca6d61
DJ
40#ifndef PTRACE_GETSIGINFO
41# define PTRACE_GETSIGINFO 0x4202
42# define PTRACE_SETSIGINFO 0x4203
43#endif
44
42c81e2a
DJ
45#ifdef __UCLIBC__
46#if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
47#define HAS_NOMMU
48#endif
49#endif
50
0d62e5e8
DJ
51/* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
52 however. This requires changing the ID in place when we go from !using_threads
53 to using_threads, immediately.
611cb4a5 54
0d62e5e8
DJ
55 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
56 the same as the LWP ID. */
57
58struct inferior_list all_processes;
59
60/* FIXME this is a bit of a hack, and could be removed. */
61int stopping_threads;
62
63/* FIXME make into a target method? */
64int using_threads;
65
66static void linux_resume_one_process (struct inferior_list_entry *entry,
32ca6d61 67 int step, int signal, siginfo_t *info);
64386c31 68static void linux_resume (struct thread_resume *resume_info);
0d62e5e8
DJ
69static void stop_all_processes (void);
70static int linux_wait_for_event (struct thread_info *child);
ae13219e 71static int check_removed_breakpoint (struct process_info *event_child);
0d62e5e8
DJ
72
73struct pending_signals
74{
75 int signal;
32ca6d61 76 siginfo_t info;
0d62e5e8
DJ
77 struct pending_signals *prev;
78};
611cb4a5 79
d844cde6 80#define PTRACE_ARG3_TYPE long
c6ecbae5 81#define PTRACE_XFER_TYPE long
da6d8c04 82
58caa3dc
DJ
83#ifdef HAVE_LINUX_REGSETS
84static int use_regsets_p = 1;
85#endif
86
0d62e5e8
DJ
87#define pid_of(proc) ((proc)->head.id)
88
89/* FIXME: Delete eventually. */
90#define inferior_pid (pid_of (get_thread_process (current_inferior)))
91
92/* This function should only be called if the process got a SIGTRAP.
93 The SIGTRAP could mean several things.
94
95 On i386, where decr_pc_after_break is non-zero:
96 If we were single-stepping this process using PTRACE_SINGLESTEP,
97 we will get only the one SIGTRAP (even if the instruction we
98 stepped over was a breakpoint). The value of $eip will be the
99 next instruction.
100 If we continue the process using PTRACE_CONT, we will get a
101 SIGTRAP when we hit a breakpoint. The value of $eip will be
102 the instruction after the breakpoint (i.e. needs to be
103 decremented). If we report the SIGTRAP to GDB, we must also
104 report the undecremented PC. If we cancel the SIGTRAP, we
105 must resume at the decremented PC.
106
107 (Presumably, not yet tested) On a non-decr_pc_after_break machine
108 with hardware or kernel single-step:
109 If we single-step over a breakpoint instruction, our PC will
110 point at the following instruction. If we continue and hit a
111 breakpoint instruction, our PC will point at the breakpoint
112 instruction. */
113
114static CORE_ADDR
115get_stop_pc (void)
116{
117 CORE_ADDR stop_pc = (*the_low_target.get_pc) ();
118
119 if (get_thread_process (current_inferior)->stepping)
120 return stop_pc;
121 else
122 return stop_pc - the_low_target.decr_pc_after_break;
123}
ce3a066d 124
0d62e5e8 125static void *
a1928bad 126add_process (unsigned long pid)
611cb4a5 127{
0d62e5e8
DJ
128 struct process_info *process;
129
130 process = (struct process_info *) malloc (sizeof (*process));
131 memset (process, 0, sizeof (*process));
132
133 process->head.id = pid;
134
135 /* Default to tid == lwpid == pid. */
136 process->tid = pid;
137 process->lwpid = pid;
138
139 add_inferior_to_list (&all_processes, &process->head);
140
141 return process;
142}
611cb4a5 143
da6d8c04
DJ
144/* Start an inferior process and returns its pid.
145 ALLARGS is a vector of program-name and args. */
146
ce3a066d
DJ
147static int
148linux_create_inferior (char *program, char **allargs)
da6d8c04 149{
0d62e5e8 150 void *new_process;
da6d8c04
DJ
151 int pid;
152
42c81e2a 153#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
154 pid = vfork ();
155#else
da6d8c04 156 pid = fork ();
52fb6437 157#endif
da6d8c04
DJ
158 if (pid < 0)
159 perror_with_name ("fork");
160
161 if (pid == 0)
162 {
163 ptrace (PTRACE_TRACEME, 0, 0, 0);
164
254787d4 165 signal (__SIGRTMIN + 1, SIG_DFL);
0d62e5e8 166
a9fa9f7d
DJ
167 setpgid (0, 0);
168
2b876972
DJ
169 execv (program, allargs);
170 if (errno == ENOENT)
171 execvp (program, allargs);
da6d8c04
DJ
172
173 fprintf (stderr, "Cannot exec %s: %s.\n", program,
d07c63e7 174 strerror (errno));
da6d8c04
DJ
175 fflush (stderr);
176 _exit (0177);
177 }
178
0d62e5e8 179 new_process = add_process (pid);
a06660f7 180 add_thread (pid, new_process, pid);
611cb4a5 181
a9fa9f7d 182 return pid;
da6d8c04
DJ
183}
184
185/* Attach to an inferior process. */
186
0d62e5e8 187void
a1928bad 188linux_attach_lwp (unsigned long pid, unsigned long tid)
da6d8c04 189{
0d62e5e8 190 struct process_info *new_process;
611cb4a5 191
da6d8c04
DJ
192 if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
193 {
a1928bad 194 fprintf (stderr, "Cannot attach to process %ld: %s (%d)\n", pid,
43d5792c 195 strerror (errno), errno);
da6d8c04 196 fflush (stderr);
0d62e5e8
DJ
197
198 /* If we fail to attach to an LWP, just return. */
199 if (!using_threads)
200 _exit (0177);
201 return;
da6d8c04
DJ
202 }
203
0d62e5e8 204 new_process = (struct process_info *) add_process (pid);
a06660f7 205 add_thread (tid, new_process, pid);
0d62e5e8
DJ
206
207 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
208 brings it to a halt. We should ignore that SIGSTOP and resume the process
209 (unless this is the first process, in which case the flag will be cleared
210 in linux_attach).
211
212 On the other hand, if we are currently trying to stop all threads, we
213 should treat the new thread as if we had sent it a SIGSTOP. This works
214 because we are guaranteed that add_process added us to the end of the
215 list, and so the new thread has not yet reached wait_for_sigstop (but
216 will). */
217 if (! stopping_threads)
218 new_process->stop_expected = 1;
219}
220
221int
a1928bad 222linux_attach (unsigned long pid)
0d62e5e8
DJ
223{
224 struct process_info *process;
225
226 linux_attach_lwp (pid, pid);
227
ae13219e
DJ
228 /* Don't ignore the initial SIGSTOP if we just attached to this process.
229 It will be collected by wait shortly. */
0d62e5e8
DJ
230 process = (struct process_info *) find_inferior_id (&all_processes, pid);
231 process->stop_expected = 0;
232
da6d8c04
DJ
233 return 0;
234}
235
236/* Kill the inferior process. Make us have no inferior. */
237
ce3a066d 238static void
0d62e5e8 239linux_kill_one_process (struct inferior_list_entry *entry)
da6d8c04 240{
0d62e5e8
DJ
241 struct thread_info *thread = (struct thread_info *) entry;
242 struct process_info *process = get_thread_process (thread);
243 int wstat;
244
fd500816
DJ
245 /* We avoid killing the first thread here, because of a Linux kernel (at
246 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
247 the children get a chance to be reaped, it will remain a zombie
248 forever. */
249 if (entry == all_threads.head)
250 return;
251
0d62e5e8
DJ
252 do
253 {
254 ptrace (PTRACE_KILL, pid_of (process), 0, 0);
255
256 /* Make sure it died. The loop is most likely unnecessary. */
257 wstat = linux_wait_for_event (thread);
258 } while (WIFSTOPPED (wstat));
da6d8c04
DJ
259}
260
0d62e5e8
DJ
261static void
262linux_kill (void)
263{
fd500816 264 struct thread_info *thread = (struct thread_info *) all_threads.head;
9d606399 265 struct process_info *process;
fd500816
DJ
266 int wstat;
267
9d606399
DJ
268 if (thread == NULL)
269 return;
270
0d62e5e8 271 for_each_inferior (&all_threads, linux_kill_one_process);
fd500816
DJ
272
273 /* See the comment in linux_kill_one_process. We did not kill the first
274 thread in the list, so do so now. */
9d606399 275 process = get_thread_process (thread);
fd500816
DJ
276 do
277 {
278 ptrace (PTRACE_KILL, pid_of (process), 0, 0);
279
280 /* Make sure it died. The loop is most likely unnecessary. */
281 wstat = linux_wait_for_event (thread);
282 } while (WIFSTOPPED (wstat));
0d62e5e8
DJ
283}
284
6ad8ae5c
DJ
285static void
286linux_detach_one_process (struct inferior_list_entry *entry)
287{
288 struct thread_info *thread = (struct thread_info *) entry;
289 struct process_info *process = get_thread_process (thread);
290
ae13219e
DJ
291 /* Make sure the process isn't stopped at a breakpoint that's
292 no longer there. */
293 check_removed_breakpoint (process);
294
295 /* If this process is stopped but is expecting a SIGSTOP, then make
296 sure we take care of that now. This isn't absolutely guaranteed
297 to collect the SIGSTOP, but is fairly likely to. */
298 if (process->stop_expected)
299 {
300 /* Clear stop_expected, so that the SIGSTOP will be reported. */
301 process->stop_expected = 0;
302 if (process->stopped)
303 linux_resume_one_process (&process->head, 0, 0, NULL);
304 linux_wait_for_event (thread);
305 }
306
307 /* Flush any pending changes to the process's registers. */
308 regcache_invalidate_one ((struct inferior_list_entry *)
309 get_process_thread (process));
310
311 /* Finally, let it resume. */
6ad8ae5c
DJ
312 ptrace (PTRACE_DETACH, pid_of (process), 0, 0);
313}
314
dd6953e1 315static int
6ad8ae5c
DJ
316linux_detach (void)
317{
ae13219e 318 delete_all_breakpoints ();
6ad8ae5c 319 for_each_inferior (&all_threads, linux_detach_one_process);
ae13219e 320 clear_inferiors ();
dd6953e1 321 return 0;
6ad8ae5c
DJ
322}
323
444d6139
PA
324static void
325linux_join (void)
326{
327 extern unsigned long signal_pid;
328 int status, ret;
329
330 do {
331 ret = waitpid (signal_pid, &status, 0);
332 if (WIFEXITED (status) || WIFSIGNALED (status))
333 break;
334 } while (ret != -1 || errno != ECHILD);
335}
336
6ad8ae5c 337/* Return nonzero if the given thread is still alive. */
0d62e5e8 338static int
a1928bad 339linux_thread_alive (unsigned long tid)
0d62e5e8
DJ
340{
341 if (find_inferior_id (&all_threads, tid) != NULL)
342 return 1;
343 else
344 return 0;
345}
346
347/* Return nonzero if this process stopped at a breakpoint which
348 no longer appears to be inserted. Also adjust the PC
349 appropriately to resume where the breakpoint used to be. */
ce3a066d 350static int
0d62e5e8 351check_removed_breakpoint (struct process_info *event_child)
da6d8c04 352{
0d62e5e8
DJ
353 CORE_ADDR stop_pc;
354 struct thread_info *saved_inferior;
355
356 if (event_child->pending_is_breakpoint == 0)
357 return 0;
358
359 if (debug_threads)
ae13219e
DJ
360 fprintf (stderr, "Checking for breakpoint in process %ld.\n",
361 event_child->lwpid);
0d62e5e8
DJ
362
363 saved_inferior = current_inferior;
364 current_inferior = get_process_thread (event_child);
365
366 stop_pc = get_stop_pc ();
367
368 /* If the PC has changed since we stopped, then we shouldn't do
369 anything. This happens if, for instance, GDB handled the
370 decr_pc_after_break subtraction itself. */
371 if (stop_pc != event_child->pending_stop_pc)
372 {
373 if (debug_threads)
ae13219e
DJ
374 fprintf (stderr, "Ignoring, PC was changed. Old PC was 0x%08llx\n",
375 event_child->pending_stop_pc);
0d62e5e8
DJ
376
377 event_child->pending_is_breakpoint = 0;
378 current_inferior = saved_inferior;
379 return 0;
380 }
381
382 /* If the breakpoint is still there, we will report hitting it. */
383 if ((*the_low_target.breakpoint_at) (stop_pc))
384 {
385 if (debug_threads)
386 fprintf (stderr, "Ignoring, breakpoint is still present.\n");
387 current_inferior = saved_inferior;
388 return 0;
389 }
390
391 if (debug_threads)
392 fprintf (stderr, "Removed breakpoint.\n");
393
394 /* For decr_pc_after_break targets, here is where we perform the
395 decrement. We go immediately from this function to resuming,
396 and can not safely call get_stop_pc () again. */
397 if (the_low_target.set_pc != NULL)
398 (*the_low_target.set_pc) (stop_pc);
399
400 /* We consumed the pending SIGTRAP. */
5544ad89 401 event_child->pending_is_breakpoint = 0;
0d62e5e8
DJ
402 event_child->status_pending_p = 0;
403 event_child->status_pending = 0;
404
405 current_inferior = saved_inferior;
da6d8c04
DJ
406 return 1;
407}
408
0d62e5e8
DJ
409/* Return 1 if this process has an interesting status pending. This function
410 may silently resume an inferior process. */
611cb4a5 411static int
0d62e5e8
DJ
412status_pending_p (struct inferior_list_entry *entry, void *dummy)
413{
414 struct process_info *process = (struct process_info *) entry;
415
416 if (process->status_pending_p)
417 if (check_removed_breakpoint (process))
418 {
419 /* This thread was stopped at a breakpoint, and the breakpoint
420 is now gone. We were told to continue (or step...) all threads,
421 so GDB isn't trying to single-step past this breakpoint.
422 So instead of reporting the old SIGTRAP, pretend we got to
423 the breakpoint just after it was removed instead of just
424 before; resume the process. */
32ca6d61 425 linux_resume_one_process (&process->head, 0, 0, NULL);
0d62e5e8
DJ
426 return 0;
427 }
428
429 return process->status_pending_p;
430}
431
432static void
433linux_wait_for_process (struct process_info **childp, int *wstatp)
611cb4a5 434{
0d62e5e8
DJ
435 int ret;
436 int to_wait_for = -1;
437
438 if (*childp != NULL)
439 to_wait_for = (*childp)->lwpid;
611cb4a5
DJ
440
441 while (1)
442 {
0d62e5e8
DJ
443 ret = waitpid (to_wait_for, wstatp, WNOHANG);
444
445 if (ret == -1)
446 {
447 if (errno != ECHILD)
448 perror_with_name ("waitpid");
449 }
450 else if (ret > 0)
451 break;
452
453 ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE);
454
455 if (ret == -1)
456 {
457 if (errno != ECHILD)
458 perror_with_name ("waitpid (WCLONE)");
459 }
460 else if (ret > 0)
461 break;
462
463 usleep (1000);
464 }
465
466 if (debug_threads
467 && (!WIFSTOPPED (*wstatp)
468 || (WSTOPSIG (*wstatp) != 32
469 && WSTOPSIG (*wstatp) != 33)))
470 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
471
472 if (to_wait_for == -1)
473 *childp = (struct process_info *) find_inferior_id (&all_processes, ret);
474
475 (*childp)->stopped = 1;
476 (*childp)->pending_is_breakpoint = 0;
477
32ca6d61
DJ
478 (*childp)->last_status = *wstatp;
479
0d62e5e8
DJ
480 if (debug_threads
481 && WIFSTOPPED (*wstatp))
482 {
483 current_inferior = (struct thread_info *)
484 find_inferior_id (&all_threads, (*childp)->tid);
485 /* For testing only; i386_stop_pc prints out a diagnostic. */
486 if (the_low_target.get_pc != NULL)
487 get_stop_pc ();
488 }
489}
611cb4a5 490
0d62e5e8
DJ
491static int
492linux_wait_for_event (struct thread_info *child)
493{
494 CORE_ADDR stop_pc;
495 struct process_info *event_child;
496 int wstat;
497
498 /* Check for a process with a pending status. */
499 /* It is possible that the user changed the pending task's registers since
500 it stopped. We correctly handle the change of PC if we hit a breakpoint
e5379b03 501 (in check_removed_breakpoint); signals should be reported anyway. */
0d62e5e8
DJ
502 if (child == NULL)
503 {
504 event_child = (struct process_info *)
505 find_inferior (&all_processes, status_pending_p, NULL);
506 if (debug_threads && event_child)
a1928bad 507 fprintf (stderr, "Got a pending child %ld\n", event_child->lwpid);
0d62e5e8
DJ
508 }
509 else
510 {
511 event_child = get_thread_process (child);
512 if (event_child->status_pending_p
513 && check_removed_breakpoint (event_child))
514 event_child = NULL;
515 }
611cb4a5 516
0d62e5e8
DJ
517 if (event_child != NULL)
518 {
519 if (event_child->status_pending_p)
611cb4a5 520 {
0d62e5e8 521 if (debug_threads)
a1928bad 522 fprintf (stderr, "Got an event from pending child %ld (%04x)\n",
0d62e5e8
DJ
523 event_child->lwpid, event_child->status_pending);
524 wstat = event_child->status_pending;
525 event_child->status_pending_p = 0;
526 event_child->status_pending = 0;
527 current_inferior = get_process_thread (event_child);
528 return wstat;
529 }
530 }
531
532 /* We only enter this loop if no process has a pending wait status. Thus
533 any action taken in response to a wait status inside this loop is
534 responding as soon as we detect the status, not after any pending
535 events. */
536 while (1)
537 {
538 if (child == NULL)
539 event_child = NULL;
540 else
541 event_child = get_thread_process (child);
542
543 linux_wait_for_process (&event_child, &wstat);
544
545 if (event_child == NULL)
546 error ("event from unknown child");
611cb4a5 547
0d62e5e8
DJ
548 current_inferior = (struct thread_info *)
549 find_inferior_id (&all_threads, event_child->tid);
550
89be2091
DJ
551 /* Check for thread exit. */
552 if (using_threads && ! WIFSTOPPED (wstat))
0d62e5e8 553 {
89be2091
DJ
554 if (debug_threads)
555 fprintf (stderr, "Thread %ld (LWP %ld) exiting\n",
556 event_child->tid, event_child->head.id);
557
558 /* If the last thread is exiting, just return. */
559 if (all_threads.head == all_threads.tail)
560 return wstat;
561
562 dead_thread_notify (event_child->tid);
563
564 remove_inferior (&all_processes, &event_child->head);
565 free (event_child);
566 remove_thread (current_inferior);
567 current_inferior = (struct thread_info *) all_threads.head;
568
569 /* If we were waiting for this particular child to do something...
570 well, it did something. */
571 if (child != NULL)
572 return wstat;
573
574 /* Wait for a more interesting event. */
575 continue;
576 }
577
578 if (using_threads
579 && WIFSTOPPED (wstat)
580 && WSTOPSIG (wstat) == SIGSTOP
581 && event_child->stop_expected)
582 {
583 if (debug_threads)
584 fprintf (stderr, "Expected stop.\n");
585 event_child->stop_expected = 0;
586 linux_resume_one_process (&event_child->head,
587 event_child->stepping, 0, NULL);
588 continue;
589 }
590
591 /* If GDB is not interested in this signal, don't stop other
592 threads, and don't report it to GDB. Just resume the
593 inferior right away. We do this for threading-related
594 signals as well as any that GDB specifically requested
595 we ignore. But never ignore SIGSTOP if we sent it
596 ourselves. */
597 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
598 thread library? */
599 if (WIFSTOPPED (wstat)
600 && ((using_threads && (WSTOPSIG (wstat) == __SIGRTMIN
601 || WSTOPSIG (wstat) == __SIGRTMIN + 1))
602 || (pass_signals[target_signal_from_host (WSTOPSIG (wstat))]
603 && (WSTOPSIG (wstat) != SIGSTOP
604 || !event_child->sigstop_sent))))
605 {
606 siginfo_t info, *info_p;
607
608 if (debug_threads)
609 fprintf (stderr, "Ignored signal %d for %ld (LWP %ld).\n",
610 WSTOPSIG (wstat), event_child->tid,
611 event_child->head.id);
612
613 if (ptrace (PTRACE_GETSIGINFO, event_child->lwpid, 0, &info) == 0)
614 info_p = &info;
615 else
616 info_p = NULL;
617 linux_resume_one_process (&event_child->head,
618 event_child->stepping,
619 WSTOPSIG (wstat), info_p);
620 continue;
0d62e5e8 621 }
611cb4a5 622
0d62e5e8
DJ
623 /* If this event was not handled above, and is not a SIGTRAP, report
624 it. */
625 if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP)
626 return wstat;
611cb4a5 627
0d62e5e8
DJ
628 /* If this target does not support breakpoints, we simply report the
629 SIGTRAP; it's of no concern to us. */
630 if (the_low_target.get_pc == NULL)
631 return wstat;
632
633 stop_pc = get_stop_pc ();
634
635 /* bp_reinsert will only be set if we were single-stepping.
636 Notice that we will resume the process after hitting
637 a gdbserver breakpoint; single-stepping to/over one
638 is not supported (yet). */
639 if (event_child->bp_reinsert != 0)
640 {
641 if (debug_threads)
642 fprintf (stderr, "Reinserted breakpoint.\n");
643 reinsert_breakpoint (event_child->bp_reinsert);
644 event_child->bp_reinsert = 0;
645
646 /* Clear the single-stepping flag and SIGTRAP as we resume. */
32ca6d61 647 linux_resume_one_process (&event_child->head, 0, 0, NULL);
0d62e5e8
DJ
648 continue;
649 }
650
651 if (debug_threads)
652 fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n");
653
654 if (check_breakpoints (stop_pc) != 0)
655 {
656 /* We hit one of our own breakpoints. We mark it as a pending
e5379b03 657 breakpoint, so that check_removed_breakpoint () will do the PC
0d62e5e8
DJ
658 adjustment for us at the appropriate time. */
659 event_child->pending_is_breakpoint = 1;
660 event_child->pending_stop_pc = stop_pc;
661
662 /* Now we need to put the breakpoint back. We continue in the event
663 loop instead of simply replacing the breakpoint right away,
664 in order to not lose signals sent to the thread that hit the
665 breakpoint. Unfortunately this increases the window where another
666 thread could sneak past the removed breakpoint. For the current
667 use of server-side breakpoints (thread creation) this is
668 acceptable; but it needs to be considered before this breakpoint
669 mechanism can be used in more general ways. For some breakpoints
670 it may be necessary to stop all other threads, but that should
671 be avoided where possible.
672
673 If breakpoint_reinsert_addr is NULL, that means that we can
674 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
675 mark it for reinsertion, and single-step.
676
677 Otherwise, call the target function to figure out where we need
678 our temporary breakpoint, create it, and continue executing this
679 process. */
680 if (the_low_target.breakpoint_reinsert_addr == NULL)
681 {
682 event_child->bp_reinsert = stop_pc;
683 uninsert_breakpoint (stop_pc);
32ca6d61 684 linux_resume_one_process (&event_child->head, 1, 0, NULL);
0d62e5e8
DJ
685 }
686 else
687 {
688 reinsert_breakpoint_by_bp
689 (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
32ca6d61 690 linux_resume_one_process (&event_child->head, 0, 0, NULL);
611cb4a5 691 }
0d62e5e8
DJ
692
693 continue;
694 }
695
696 /* If we were single-stepping, we definitely want to report the
697 SIGTRAP. The single-step operation has completed, so also
aa691b87 698 clear the stepping flag; in general this does not matter,
0d62e5e8
DJ
699 because the SIGTRAP will be reported to the client, which
700 will give us a new action for this thread, but clear it for
701 consistency anyway. It's safe to clear the stepping flag
702 because the only consumer of get_stop_pc () after this point
e5379b03 703 is check_removed_breakpoint, and pending_is_breakpoint is not
0d62e5e8
DJ
704 set. It might be wiser to use a step_completed flag instead. */
705 if (event_child->stepping)
706 {
707 event_child->stepping = 0;
708 return wstat;
709 }
710
711 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
712 Check if it is a breakpoint, and if so mark the process information
713 accordingly. This will handle both the necessary fiddling with the
714 PC on decr_pc_after_break targets and suppressing extra threads
715 hitting a breakpoint if two hit it at once and then GDB removes it
716 after the first is reported. Arguably it would be better to report
717 multiple threads hitting breakpoints simultaneously, but the current
718 remote protocol does not allow this. */
719 if ((*the_low_target.breakpoint_at) (stop_pc))
720 {
721 event_child->pending_is_breakpoint = 1;
722 event_child->pending_stop_pc = stop_pc;
611cb4a5
DJ
723 }
724
725 return wstat;
726 }
0d62e5e8 727
611cb4a5
DJ
728 /* NOTREACHED */
729 return 0;
730}
731
0d62e5e8 732/* Wait for process, returns status. */
da6d8c04 733
ce3a066d
DJ
734static unsigned char
735linux_wait (char *status)
da6d8c04 736{
e5f1222d 737 int w;
0d62e5e8
DJ
738 struct thread_info *child = NULL;
739
740retry:
741 /* If we were only supposed to resume one thread, only wait for
742 that thread - if it's still alive. If it died, however - which
743 can happen if we're coming from the thread death case below -
744 then we need to make sure we restart the other threads. We could
745 pick a thread at random or restart all; restarting all is less
746 arbitrary. */
d592fa2f 747 if (cont_thread != 0 && cont_thread != -1)
0d62e5e8
DJ
748 {
749 child = (struct thread_info *) find_inferior_id (&all_threads,
750 cont_thread);
751
752 /* No stepping, no signal - unless one is pending already, of course. */
753 if (child == NULL)
64386c31
DJ
754 {
755 struct thread_resume resume_info;
756 resume_info.thread = -1;
757 resume_info.step = resume_info.sig = resume_info.leave_stopped = 0;
758 linux_resume (&resume_info);
759 }
0d62e5e8 760 }
da6d8c04
DJ
761
762 enable_async_io ();
62ea82f5 763 unblock_async_io ();
0d62e5e8
DJ
764 w = linux_wait_for_event (child);
765 stop_all_processes ();
da6d8c04 766 disable_async_io ();
da6d8c04 767
0d62e5e8
DJ
768 /* If we are waiting for a particular child, and it exited,
769 linux_wait_for_event will return its exit status. Similarly if
770 the last child exited. If this is not the last child, however,
771 do not report it as exited until there is a 'thread exited' response
772 available in the remote protocol. Instead, just wait for another event.
773 This should be safe, because if the thread crashed we will already
774 have reported the termination signal to GDB; that should stop any
775 in-progress stepping operations, etc.
776
777 Report the exit status of the last thread to exit. This matches
778 LinuxThreads' behavior. */
779
780 if (all_threads.head == all_threads.tail)
da6d8c04 781 {
0d62e5e8
DJ
782 if (WIFEXITED (w))
783 {
784 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
785 *status = 'W';
786 clear_inferiors ();
075b3282
DJ
787 free (all_processes.head);
788 all_processes.head = all_processes.tail = NULL;
b80864fb 789 return WEXITSTATUS (w);
0d62e5e8
DJ
790 }
791 else if (!WIFSTOPPED (w))
792 {
793 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
0d62e5e8 794 *status = 'X';
075b3282
DJ
795 clear_inferiors ();
796 free (all_processes.head);
797 all_processes.head = all_processes.tail = NULL;
b80864fb 798 return target_signal_from_host (WTERMSIG (w));
0d62e5e8 799 }
da6d8c04 800 }
0d62e5e8 801 else
da6d8c04 802 {
0d62e5e8
DJ
803 if (!WIFSTOPPED (w))
804 goto retry;
da6d8c04
DJ
805 }
806
da6d8c04 807 *status = 'T';
b80864fb 808 return target_signal_from_host (WSTOPSIG (w));
da6d8c04
DJ
809}
810
fd500816
DJ
811/* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
812 thread groups are in use, we need to use tkill. */
813
814static int
a1928bad 815kill_lwp (unsigned long lwpid, int signo)
fd500816
DJ
816{
817 static int tkill_failed;
818
819 errno = 0;
820
821#ifdef SYS_tkill
822 if (!tkill_failed)
823 {
824 int ret = syscall (SYS_tkill, lwpid, signo);
825 if (errno != ENOSYS)
826 return ret;
827 errno = 0;
828 tkill_failed = 1;
829 }
830#endif
831
832 return kill (lwpid, signo);
833}
834
0d62e5e8
DJ
835static void
836send_sigstop (struct inferior_list_entry *entry)
837{
838 struct process_info *process = (struct process_info *) entry;
839
840 if (process->stopped)
841 return;
842
843 /* If we already have a pending stop signal for this process, don't
844 send another. */
845 if (process->stop_expected)
846 {
ae13219e
DJ
847 if (debug_threads)
848 fprintf (stderr, "Have pending sigstop for process %ld\n",
849 process->lwpid);
850
851 /* We clear the stop_expected flag so that wait_for_sigstop
852 will receive the SIGSTOP event (instead of silently resuming and
853 waiting again). It'll be reset below. */
0d62e5e8
DJ
854 process->stop_expected = 0;
855 return;
856 }
857
858 if (debug_threads)
a1928bad 859 fprintf (stderr, "Sending sigstop to process %ld\n", process->head.id);
0d62e5e8 860
fd500816 861 kill_lwp (process->head.id, SIGSTOP);
0d62e5e8
DJ
862 process->sigstop_sent = 1;
863}
864
865static void
866wait_for_sigstop (struct inferior_list_entry *entry)
867{
868 struct process_info *process = (struct process_info *) entry;
869 struct thread_info *saved_inferior, *thread;
a1928bad
DJ
870 int wstat;
871 unsigned long saved_tid;
0d62e5e8
DJ
872
873 if (process->stopped)
874 return;
875
876 saved_inferior = current_inferior;
877 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
878 thread = (struct thread_info *) find_inferior_id (&all_threads,
879 process->tid);
880 wstat = linux_wait_for_event (thread);
881
882 /* If we stopped with a non-SIGSTOP signal, save it for later
883 and record the pending SIGSTOP. If the process exited, just
884 return. */
885 if (WIFSTOPPED (wstat)
886 && WSTOPSIG (wstat) != SIGSTOP)
887 {
888 if (debug_threads)
ae13219e
DJ
889 fprintf (stderr, "Process %ld (thread %ld) "
890 "stopped with non-sigstop status %06x\n",
891 process->lwpid, process->tid, wstat);
0d62e5e8
DJ
892 process->status_pending_p = 1;
893 process->status_pending = wstat;
894 process->stop_expected = 1;
895 }
896
897 if (linux_thread_alive (saved_tid))
898 current_inferior = saved_inferior;
899 else
900 {
901 if (debug_threads)
902 fprintf (stderr, "Previously current thread died.\n");
903
904 /* Set a valid thread as current. */
905 set_desired_inferior (0);
906 }
907}
908
909static void
910stop_all_processes (void)
911{
912 stopping_threads = 1;
913 for_each_inferior (&all_processes, send_sigstop);
914 for_each_inferior (&all_processes, wait_for_sigstop);
915 stopping_threads = 0;
916}
917
da6d8c04
DJ
918/* Resume execution of the inferior process.
919 If STEP is nonzero, single-step it.
920 If SIGNAL is nonzero, give it that signal. */
921
ce3a066d 922static void
0d62e5e8 923linux_resume_one_process (struct inferior_list_entry *entry,
32ca6d61 924 int step, int signal, siginfo_t *info)
da6d8c04 925{
0d62e5e8
DJ
926 struct process_info *process = (struct process_info *) entry;
927 struct thread_info *saved_inferior;
928
929 if (process->stopped == 0)
930 return;
931
932 /* If we have pending signals or status, and a new signal, enqueue the
933 signal. Also enqueue the signal if we are waiting to reinsert a
934 breakpoint; it will be picked up again below. */
935 if (signal != 0
936 && (process->status_pending_p || process->pending_signals != NULL
937 || process->bp_reinsert != 0))
938 {
939 struct pending_signals *p_sig;
940 p_sig = malloc (sizeof (*p_sig));
941 p_sig->prev = process->pending_signals;
942 p_sig->signal = signal;
32ca6d61
DJ
943 if (info == NULL)
944 memset (&p_sig->info, 0, sizeof (siginfo_t));
945 else
946 memcpy (&p_sig->info, info, sizeof (siginfo_t));
0d62e5e8
DJ
947 process->pending_signals = p_sig;
948 }
949
e5379b03 950 if (process->status_pending_p && !check_removed_breakpoint (process))
0d62e5e8
DJ
951 return;
952
953 saved_inferior = current_inferior;
954 current_inferior = get_process_thread (process);
955
956 if (debug_threads)
a1928bad 957 fprintf (stderr, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid,
0d62e5e8
DJ
958 step ? "step" : "continue", signal,
959 process->stop_expected ? "expected" : "not expected");
960
961 /* This bit needs some thinking about. If we get a signal that
962 we must report while a single-step reinsert is still pending,
963 we often end up resuming the thread. It might be better to
964 (ew) allow a stack of pending events; then we could be sure that
965 the reinsert happened right away and not lose any signals.
966
967 Making this stack would also shrink the window in which breakpoints are
968 uninserted (see comment in linux_wait_for_process) but not enough for
969 complete correctness, so it won't solve that problem. It may be
970 worthwhile just to solve this one, however. */
971 if (process->bp_reinsert != 0)
972 {
973 if (debug_threads)
974 fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert);
975 if (step == 0)
976 fprintf (stderr, "BAD - reinserting but not stepping.\n");
977 step = 1;
978
979 /* Postpone any pending signal. It was enqueued above. */
980 signal = 0;
981 }
982
983 check_removed_breakpoint (process);
984
aa691b87 985 if (debug_threads && the_low_target.get_pc != NULL)
0d62e5e8
DJ
986 {
987 fprintf (stderr, " ");
52fb6437 988 (*the_low_target.get_pc) ();
0d62e5e8
DJ
989 }
990
991 /* If we have pending signals, consume one unless we are trying to reinsert
992 a breakpoint. */
993 if (process->pending_signals != NULL && process->bp_reinsert == 0)
994 {
995 struct pending_signals **p_sig;
996
997 p_sig = &process->pending_signals;
998 while ((*p_sig)->prev != NULL)
999 p_sig = &(*p_sig)->prev;
1000
1001 signal = (*p_sig)->signal;
32ca6d61
DJ
1002 if ((*p_sig)->info.si_signo != 0)
1003 ptrace (PTRACE_SETSIGINFO, process->lwpid, 0, &(*p_sig)->info);
1004
0d62e5e8
DJ
1005 free (*p_sig);
1006 *p_sig = NULL;
1007 }
1008
1009 regcache_invalidate_one ((struct inferior_list_entry *)
1010 get_process_thread (process));
da6d8c04 1011 errno = 0;
0d62e5e8
DJ
1012 process->stopped = 0;
1013 process->stepping = step;
1014 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal);
1015
1016 current_inferior = saved_inferior;
da6d8c04
DJ
1017 if (errno)
1018 perror_with_name ("ptrace");
1019}
1020
64386c31
DJ
1021static struct thread_resume *resume_ptr;
1022
1023/* This function is called once per thread. We look up the thread
5544ad89
DJ
1024 in RESUME_PTR, and mark the thread with a pointer to the appropriate
1025 resume request.
1026
1027 This algorithm is O(threads * resume elements), but resume elements
1028 is small (and will remain small at least until GDB supports thread
1029 suspension). */
0d62e5e8 1030static void
5544ad89 1031linux_set_resume_request (struct inferior_list_entry *entry)
0d62e5e8
DJ
1032{
1033 struct process_info *process;
64386c31 1034 struct thread_info *thread;
5544ad89 1035 int ndx;
64386c31
DJ
1036
1037 thread = (struct thread_info *) entry;
1038 process = get_thread_process (thread);
1039
1040 ndx = 0;
1041 while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id)
1042 ndx++;
1043
5544ad89
DJ
1044 process->resume = &resume_ptr[ndx];
1045}
1046
1047/* This function is called once per thread. We check the thread's resume
1048 request, which will tell us whether to resume, step, or leave the thread
1049 stopped; and what signal, if any, it should be sent. For threads which
1050 we aren't explicitly told otherwise, we preserve the stepping flag; this
1051 is used for stepping over gdbserver-placed breakpoints. */
1052
1053static void
1054linux_continue_one_thread (struct inferior_list_entry *entry)
1055{
1056 struct process_info *process;
1057 struct thread_info *thread;
1058 int step;
1059
1060 thread = (struct thread_info *) entry;
1061 process = get_thread_process (thread);
1062
1063 if (process->resume->leave_stopped)
64386c31
DJ
1064 return;
1065
5544ad89
DJ
1066 if (process->resume->thread == -1)
1067 step = process->stepping || process->resume->step;
64386c31 1068 else
5544ad89
DJ
1069 step = process->resume->step;
1070
32ca6d61 1071 linux_resume_one_process (&process->head, step, process->resume->sig, NULL);
c6ecbae5 1072
5544ad89
DJ
1073 process->resume = NULL;
1074}
1075
1076/* This function is called once per thread. We check the thread's resume
1077 request, which will tell us whether to resume, step, or leave the thread
1078 stopped; and what signal, if any, it should be sent. We queue any needed
1079 signals, since we won't actually resume. We already have a pending event
1080 to report, so we don't need to preserve any step requests; they should
1081 be re-issued if necessary. */
1082
1083static void
1084linux_queue_one_thread (struct inferior_list_entry *entry)
1085{
1086 struct process_info *process;
1087 struct thread_info *thread;
1088
1089 thread = (struct thread_info *) entry;
1090 process = get_thread_process (thread);
1091
1092 if (process->resume->leave_stopped)
1093 return;
1094
1095 /* If we have a new signal, enqueue the signal. */
1096 if (process->resume->sig != 0)
1097 {
1098 struct pending_signals *p_sig;
1099 p_sig = malloc (sizeof (*p_sig));
1100 p_sig->prev = process->pending_signals;
1101 p_sig->signal = process->resume->sig;
32ca6d61
DJ
1102 memset (&p_sig->info, 0, sizeof (siginfo_t));
1103
1104 /* If this is the same signal we were previously stopped by,
1105 make sure to queue its siginfo. We can ignore the return
1106 value of ptrace; if it fails, we'll skip
1107 PTRACE_SETSIGINFO. */
1108 if (WIFSTOPPED (process->last_status)
1109 && WSTOPSIG (process->last_status) == process->resume->sig)
1110 ptrace (PTRACE_GETSIGINFO, process->lwpid, 0, &p_sig->info);
1111
5544ad89
DJ
1112 process->pending_signals = p_sig;
1113 }
1114
1115 process->resume = NULL;
1116}
1117
1118/* Set DUMMY if this process has an interesting status pending. */
1119static int
1120resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
1121{
1122 struct process_info *process = (struct process_info *) entry;
1123
1124 /* Processes which will not be resumed are not interesting, because
1125 we might not wait for them next time through linux_wait. */
1126 if (process->resume->leave_stopped)
1127 return 0;
1128
1129 /* If this thread has a removed breakpoint, we won't have any
1130 events to report later, so check now. check_removed_breakpoint
1131 may clear status_pending_p. We avoid calling check_removed_breakpoint
1132 for any thread that we are not otherwise going to resume - this
1133 lets us preserve stopped status when two threads hit a breakpoint.
1134 GDB removes the breakpoint to single-step a particular thread
1135 past it, then re-inserts it and resumes all threads. We want
1136 to report the second thread without resuming it in the interim. */
1137 if (process->status_pending_p)
1138 check_removed_breakpoint (process);
1139
1140 if (process->status_pending_p)
1141 * (int *) flag_p = 1;
1142
1143 return 0;
0d62e5e8
DJ
1144}
1145
1146static void
64386c31 1147linux_resume (struct thread_resume *resume_info)
0d62e5e8 1148{
5544ad89 1149 int pending_flag;
c6ecbae5 1150
5544ad89 1151 /* Yes, the use of a global here is rather ugly. */
64386c31 1152 resume_ptr = resume_info;
5544ad89
DJ
1153
1154 for_each_inferior (&all_threads, linux_set_resume_request);
1155
1156 /* If there is a thread which would otherwise be resumed, which
1157 has a pending status, then don't resume any threads - we can just
1158 report the pending status. Make sure to queue any signals
1159 that would otherwise be sent. */
1160 pending_flag = 0;
1161 find_inferior (&all_processes, resume_status_pending_p, &pending_flag);
1162
1163 if (debug_threads)
1164 {
1165 if (pending_flag)
1166 fprintf (stderr, "Not resuming, pending status\n");
1167 else
1168 fprintf (stderr, "Resuming, no pending status\n");
1169 }
1170
1171 if (pending_flag)
1172 for_each_inferior (&all_threads, linux_queue_one_thread);
1173 else
62ea82f5
DJ
1174 {
1175 block_async_io ();
1176 enable_async_io ();
1177 for_each_inferior (&all_threads, linux_continue_one_thread);
1178 }
0d62e5e8
DJ
1179}
1180
1181#ifdef HAVE_LINUX_USRREGS
da6d8c04
DJ
1182
1183int
0a30fbc4 1184register_addr (int regnum)
da6d8c04
DJ
1185{
1186 int addr;
1187
2ec06d2e 1188 if (regnum < 0 || regnum >= the_low_target.num_regs)
da6d8c04
DJ
1189 error ("Invalid register number %d.", regnum);
1190
2ec06d2e 1191 addr = the_low_target.regmap[regnum];
da6d8c04
DJ
1192
1193 return addr;
1194}
1195
58caa3dc 1196/* Fetch one register. */
da6d8c04
DJ
1197static void
1198fetch_register (int regno)
1199{
1200 CORE_ADDR regaddr;
48d93c75 1201 int i, size;
0d62e5e8 1202 char *buf;
da6d8c04 1203
2ec06d2e 1204 if (regno >= the_low_target.num_regs)
0a30fbc4 1205 return;
2ec06d2e 1206 if ((*the_low_target.cannot_fetch_register) (regno))
0a30fbc4 1207 return;
da6d8c04 1208
0a30fbc4
DJ
1209 regaddr = register_addr (regno);
1210 if (regaddr == -1)
1211 return;
48d93c75
UW
1212 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
1213 & - sizeof (PTRACE_XFER_TYPE);
1214 buf = alloca (size);
1215 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04
DJ
1216 {
1217 errno = 0;
0d62e5e8 1218 *(PTRACE_XFER_TYPE *) (buf + i) =
da6d8c04
DJ
1219 ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
1220 regaddr += sizeof (PTRACE_XFER_TYPE);
1221 if (errno != 0)
1222 {
1223 /* Warning, not error, in case we are attached; sometimes the
1224 kernel doesn't let us at the registers. */
1225 char *err = strerror (errno);
1226 char *msg = alloca (strlen (err) + 128);
1227 sprintf (msg, "reading register %d: %s", regno, err);
1228 error (msg);
1229 goto error_exit;
1230 }
1231 }
5a1f5858
DJ
1232 if (the_low_target.left_pad_xfer
1233 && register_size (regno) < sizeof (PTRACE_XFER_TYPE))
1234 supply_register (regno, (buf + sizeof (PTRACE_XFER_TYPE)
1235 - register_size (regno)));
1236 else
1237 supply_register (regno, buf);
0d62e5e8 1238
da6d8c04
DJ
1239error_exit:;
1240}
1241
1242/* Fetch all registers, or just one, from the child process. */
58caa3dc
DJ
1243static void
1244usr_fetch_inferior_registers (int regno)
da6d8c04
DJ
1245{
1246 if (regno == -1 || regno == 0)
2ec06d2e 1247 for (regno = 0; regno < the_low_target.num_regs; regno++)
da6d8c04
DJ
1248 fetch_register (regno);
1249 else
1250 fetch_register (regno);
1251}
1252
1253/* Store our register values back into the inferior.
1254 If REGNO is -1, do this for all registers.
1255 Otherwise, REGNO specifies which register (so we can save time). */
58caa3dc
DJ
1256static void
1257usr_store_inferior_registers (int regno)
da6d8c04
DJ
1258{
1259 CORE_ADDR regaddr;
48d93c75 1260 int i, size;
0d62e5e8 1261 char *buf;
da6d8c04
DJ
1262
1263 if (regno >= 0)
1264 {
2ec06d2e 1265 if (regno >= the_low_target.num_regs)
0a30fbc4
DJ
1266 return;
1267
bc1e36ca 1268 if ((*the_low_target.cannot_store_register) (regno) == 1)
0a30fbc4
DJ
1269 return;
1270
1271 regaddr = register_addr (regno);
1272 if (regaddr == -1)
da6d8c04 1273 return;
da6d8c04 1274 errno = 0;
48d93c75
UW
1275 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
1276 & - sizeof (PTRACE_XFER_TYPE);
1277 buf = alloca (size);
1278 memset (buf, 0, size);
5a1f5858
DJ
1279 if (the_low_target.left_pad_xfer
1280 && register_size (regno) < sizeof (PTRACE_XFER_TYPE))
1281 collect_register (regno, (buf + sizeof (PTRACE_XFER_TYPE)
1282 - register_size (regno)));
1283 else
1284 collect_register (regno, buf);
48d93c75 1285 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04 1286 {
0a30fbc4
DJ
1287 errno = 0;
1288 ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
2ff29de4 1289 *(PTRACE_XFER_TYPE *) (buf + i));
da6d8c04
DJ
1290 if (errno != 0)
1291 {
bc1e36ca
DJ
1292 if ((*the_low_target.cannot_store_register) (regno) == 0)
1293 {
1294 char *err = strerror (errno);
1295 char *msg = alloca (strlen (err) + 128);
1296 sprintf (msg, "writing register %d: %s",
1297 regno, err);
1298 error (msg);
1299 return;
1300 }
da6d8c04 1301 }
2ff29de4 1302 regaddr += sizeof (PTRACE_XFER_TYPE);
da6d8c04 1303 }
da6d8c04
DJ
1304 }
1305 else
2ec06d2e 1306 for (regno = 0; regno < the_low_target.num_regs; regno++)
0d62e5e8 1307 usr_store_inferior_registers (regno);
da6d8c04 1308}
58caa3dc
DJ
1309#endif /* HAVE_LINUX_USRREGS */
1310
1311
1312
1313#ifdef HAVE_LINUX_REGSETS
1314
1315static int
0d62e5e8 1316regsets_fetch_inferior_registers ()
58caa3dc
DJ
1317{
1318 struct regset_info *regset;
e9d25b98 1319 int saw_general_regs = 0;
58caa3dc
DJ
1320
1321 regset = target_regsets;
1322
1323 while (regset->size >= 0)
1324 {
1325 void *buf;
1326 int res;
1327
1328 if (regset->size == 0)
1329 {
1330 regset ++;
1331 continue;
1332 }
1333
1334 buf = malloc (regset->size);
d06f167a 1335 res = ptrace (regset->get_request, inferior_pid, 0, buf);
58caa3dc
DJ
1336 if (res < 0)
1337 {
1338 if (errno == EIO)
1339 {
1340 /* If we get EIO on the first regset, do not try regsets again.
1341 If we get EIO on a later regset, disable that regset. */
1342 if (regset == target_regsets)
1343 {
1344 use_regsets_p = 0;
1345 return -1;
1346 }
1347 else
1348 {
1349 regset->size = 0;
1350 continue;
1351 }
1352 }
1353 else
1354 {
0d62e5e8 1355 char s[256];
a1928bad 1356 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
0d62e5e8
DJ
1357 inferior_pid);
1358 perror (s);
58caa3dc
DJ
1359 }
1360 }
e9d25b98
DJ
1361 else if (regset->type == GENERAL_REGS)
1362 saw_general_regs = 1;
58caa3dc
DJ
1363 regset->store_function (buf);
1364 regset ++;
1365 }
e9d25b98
DJ
1366 if (saw_general_regs)
1367 return 0;
1368 else
1369 return 1;
58caa3dc
DJ
1370}
1371
1372static int
0d62e5e8 1373regsets_store_inferior_registers ()
58caa3dc
DJ
1374{
1375 struct regset_info *regset;
e9d25b98 1376 int saw_general_regs = 0;
58caa3dc
DJ
1377
1378 regset = target_regsets;
1379
1380 while (regset->size >= 0)
1381 {
1382 void *buf;
1383 int res;
1384
1385 if (regset->size == 0)
1386 {
1387 regset ++;
1388 continue;
1389 }
1390
1391 buf = malloc (regset->size);
545587ee
DJ
1392
1393 /* First fill the buffer with the current register set contents,
1394 in case there are any items in the kernel's regset that are
1395 not in gdbserver's regcache. */
1396 res = ptrace (regset->get_request, inferior_pid, 0, buf);
1397
1398 if (res == 0)
1399 {
1400 /* Then overlay our cached registers on that. */
1401 regset->fill_function (buf);
1402
1403 /* Only now do we write the register set. */
1404 res = ptrace (regset->set_request, inferior_pid, 0, buf);
1405 }
1406
58caa3dc
DJ
1407 if (res < 0)
1408 {
1409 if (errno == EIO)
1410 {
1411 /* If we get EIO on the first regset, do not try regsets again.
1412 If we get EIO on a later regset, disable that regset. */
1413 if (regset == target_regsets)
1414 {
1415 use_regsets_p = 0;
1416 return -1;
1417 }
1418 else
1419 {
1420 regset->size = 0;
1421 continue;
1422 }
1423 }
1424 else
1425 {
ce3a066d 1426 perror ("Warning: ptrace(regsets_store_inferior_registers)");
58caa3dc
DJ
1427 }
1428 }
e9d25b98
DJ
1429 else if (regset->type == GENERAL_REGS)
1430 saw_general_regs = 1;
58caa3dc 1431 regset ++;
09ec9b38 1432 free (buf);
58caa3dc 1433 }
e9d25b98
DJ
1434 if (saw_general_regs)
1435 return 0;
1436 else
1437 return 1;
ce3a066d 1438 return 0;
58caa3dc
DJ
1439}
1440
1441#endif /* HAVE_LINUX_REGSETS */
1442
1443
1444void
ce3a066d 1445linux_fetch_registers (int regno)
58caa3dc
DJ
1446{
1447#ifdef HAVE_LINUX_REGSETS
1448 if (use_regsets_p)
1449 {
1450 if (regsets_fetch_inferior_registers () == 0)
1451 return;
1452 }
1453#endif
1454#ifdef HAVE_LINUX_USRREGS
1455 usr_fetch_inferior_registers (regno);
1456#endif
1457}
1458
1459void
ce3a066d 1460linux_store_registers (int regno)
58caa3dc
DJ
1461{
1462#ifdef HAVE_LINUX_REGSETS
1463 if (use_regsets_p)
1464 {
1465 if (regsets_store_inferior_registers () == 0)
1466 return;
1467 }
1468#endif
1469#ifdef HAVE_LINUX_USRREGS
1470 usr_store_inferior_registers (regno);
1471#endif
1472}
1473
da6d8c04 1474
da6d8c04
DJ
1475/* Copy LEN bytes from inferior's memory starting at MEMADDR
1476 to debugger memory starting at MYADDR. */
1477
c3e735a6 1478static int
f450004a 1479linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
da6d8c04
DJ
1480{
1481 register int i;
1482 /* Round starting address down to longword boundary. */
1483 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1484 /* Round ending address up; get number of longwords that makes. */
aa691b87
RM
1485 register int count
1486 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
da6d8c04
DJ
1487 / sizeof (PTRACE_XFER_TYPE);
1488 /* Allocate buffer of that many longwords. */
aa691b87 1489 register PTRACE_XFER_TYPE *buffer
da6d8c04
DJ
1490 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1491
1492 /* Read all the longwords */
1493 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1494 {
c3e735a6 1495 errno = 0;
d844cde6 1496 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
c3e735a6
DJ
1497 if (errno)
1498 return errno;
da6d8c04
DJ
1499 }
1500
1501 /* Copy appropriate bytes out of the buffer. */
1502 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
c3e735a6
DJ
1503
1504 return 0;
da6d8c04
DJ
1505}
1506
1507/* Copy LEN bytes of data from debugger memory at MYADDR
1508 to inferior's memory at MEMADDR.
1509 On failure (cannot write the inferior)
1510 returns the value of errno. */
1511
ce3a066d 1512static int
f450004a 1513linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
da6d8c04
DJ
1514{
1515 register int i;
1516 /* Round starting address down to longword boundary. */
1517 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1518 /* Round ending address up; get number of longwords that makes. */
1519 register int count
1520 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
1521 /* Allocate buffer of that many longwords. */
1522 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1523 extern int errno;
1524
0d62e5e8
DJ
1525 if (debug_threads)
1526 {
1527 fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
1528 }
1529
da6d8c04
DJ
1530 /* Fill start and end extra bytes of buffer with existing memory data. */
1531
d844cde6
DJ
1532 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
1533 (PTRACE_ARG3_TYPE) addr, 0);
da6d8c04
DJ
1534
1535 if (count > 1)
1536 {
1537 buffer[count - 1]
1538 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
d844cde6
DJ
1539 (PTRACE_ARG3_TYPE) (addr + (count - 1)
1540 * sizeof (PTRACE_XFER_TYPE)),
1541 0);
da6d8c04
DJ
1542 }
1543
1544 /* Copy data to be written over corresponding part of buffer */
1545
1546 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
1547
1548 /* Write the entire buffer. */
1549
1550 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1551 {
1552 errno = 0;
d844cde6 1553 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
da6d8c04
DJ
1554 if (errno)
1555 return errno;
1556 }
1557
1558 return 0;
1559}
2f2893d9
DJ
1560
1561static void
1562linux_look_up_symbols (void)
1563{
0d62e5e8
DJ
1564#ifdef USE_THREAD_DB
1565 if (using_threads)
1566 return;
1567
1568 using_threads = thread_db_init ();
1569#endif
1570}
1571
e5379b03 1572static void
ef57601b 1573linux_request_interrupt (void)
e5379b03 1574{
a1928bad 1575 extern unsigned long signal_pid;
e5379b03 1576
d592fa2f 1577 if (cont_thread != 0 && cont_thread != -1)
e5379b03
DJ
1578 {
1579 struct process_info *process;
1580
1581 process = get_thread_process (current_inferior);
ef57601b 1582 kill_lwp (process->lwpid, SIGINT);
e5379b03
DJ
1583 }
1584 else
ef57601b 1585 kill_lwp (signal_pid, SIGINT);
e5379b03
DJ
1586}
1587
aa691b87
RM
1588/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1589 to debugger memory starting at MYADDR. */
1590
1591static int
f450004a 1592linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
aa691b87
RM
1593{
1594 char filename[PATH_MAX];
1595 int fd, n;
1596
a1928bad 1597 snprintf (filename, sizeof filename, "/proc/%ld/auxv", inferior_pid);
aa691b87
RM
1598
1599 fd = open (filename, O_RDONLY);
1600 if (fd < 0)
1601 return -1;
1602
1603 if (offset != (CORE_ADDR) 0
1604 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
1605 n = -1;
1606 else
1607 n = read (fd, myaddr, len);
1608
1609 close (fd);
1610
1611 return n;
1612}
1613
e013ee27
OF
1614/* These watchpoint related wrapper functions simply pass on the function call
1615 if the target has registered a corresponding function. */
1616
1617static int
1618linux_insert_watchpoint (char type, CORE_ADDR addr, int len)
1619{
1620 if (the_low_target.insert_watchpoint != NULL)
1621 return the_low_target.insert_watchpoint (type, addr, len);
1622 else
1623 /* Unsupported (see target.h). */
1624 return 1;
1625}
1626
1627static int
1628linux_remove_watchpoint (char type, CORE_ADDR addr, int len)
1629{
1630 if (the_low_target.remove_watchpoint != NULL)
1631 return the_low_target.remove_watchpoint (type, addr, len);
1632 else
1633 /* Unsupported (see target.h). */
1634 return 1;
1635}
1636
1637static int
1638linux_stopped_by_watchpoint (void)
1639{
1640 if (the_low_target.stopped_by_watchpoint != NULL)
1641 return the_low_target.stopped_by_watchpoint ();
1642 else
1643 return 0;
1644}
1645
1646static CORE_ADDR
1647linux_stopped_data_address (void)
1648{
1649 if (the_low_target.stopped_data_address != NULL)
1650 return the_low_target.stopped_data_address ();
1651 else
1652 return 0;
1653}
1654
42c81e2a 1655#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
1656#if defined(__mcoldfire__)
1657/* These should really be defined in the kernel's ptrace.h header. */
1658#define PT_TEXT_ADDR 49*4
1659#define PT_DATA_ADDR 50*4
1660#define PT_TEXT_END_ADDR 51*4
1661#endif
1662
1663/* Under uClinux, programs are loaded at non-zero offsets, which we need
1664 to tell gdb about. */
1665
1666static int
1667linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
1668{
1669#if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1670 unsigned long text, text_end, data;
1671 int pid = get_thread_process (current_inferior)->head.id;
1672
1673 errno = 0;
1674
1675 text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0);
1676 text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0);
1677 data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0);
1678
1679 if (errno == 0)
1680 {
1681 /* Both text and data offsets produced at compile-time (and so
1682 used by gdb) are relative to the beginning of the program,
1683 with the data segment immediately following the text segment.
1684 However, the actual runtime layout in memory may put the data
1685 somewhere else, so when we send gdb a data base-address, we
1686 use the real data base address and subtract the compile-time
1687 data base-address from it (which is just the length of the
1688 text segment). BSS immediately follows data in both
1689 cases. */
1690 *text_p = text;
1691 *data_p = data - (text_end - text);
1692
1693 return 1;
1694 }
1695#endif
1696 return 0;
1697}
1698#endif
1699
23181151
DJ
1700static const char *
1701linux_arch_string (void)
1702{
1703 return the_low_target.arch_string;
1704}
1705
ce3a066d
DJ
1706static struct target_ops linux_target_ops = {
1707 linux_create_inferior,
1708 linux_attach,
1709 linux_kill,
6ad8ae5c 1710 linux_detach,
444d6139 1711 linux_join,
ce3a066d
DJ
1712 linux_thread_alive,
1713 linux_resume,
1714 linux_wait,
1715 linux_fetch_registers,
1716 linux_store_registers,
1717 linux_read_memory,
1718 linux_write_memory,
2f2893d9 1719 linux_look_up_symbols,
ef57601b 1720 linux_request_interrupt,
aa691b87 1721 linux_read_auxv,
e013ee27
OF
1722 linux_insert_watchpoint,
1723 linux_remove_watchpoint,
1724 linux_stopped_by_watchpoint,
1725 linux_stopped_data_address,
42c81e2a 1726#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437 1727 linux_read_offsets,
dae5f5cf
DJ
1728#else
1729 NULL,
1730#endif
1731#ifdef USE_THREAD_DB
1732 thread_db_get_tls_address,
1733#else
1734 NULL,
52fb6437 1735#endif
23181151 1736 linux_arch_string,
ce3a066d
DJ
1737};
1738
0d62e5e8
DJ
1739static void
1740linux_init_signals ()
1741{
1742 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1743 to find what the cancel signal actually is. */
254787d4 1744 signal (__SIGRTMIN+1, SIG_IGN);
0d62e5e8
DJ
1745}
1746
da6d8c04
DJ
1747void
1748initialize_low (void)
1749{
0d62e5e8 1750 using_threads = 0;
ce3a066d 1751 set_target_ops (&linux_target_ops);
611cb4a5
DJ
1752 set_breakpoint_data (the_low_target.breakpoint,
1753 the_low_target.breakpoint_len);
0a30fbc4 1754 init_registers ();
0d62e5e8 1755 linux_init_signals ();
da6d8c04 1756}