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