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