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