Commit | Line | Data |
---|---|---|
da6d8c04 DJ |
1 | /* Low level interface to ptrace, for the remote server for GDB. |
2 | Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002 | |
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); | |
611cb4a5 | 55 | static void linux_resume (int step, int signal); |
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. */ | |
319 | event_child->status_pending_p = 0; | |
320 | event_child->status_pending = 0; | |
321 | ||
322 | current_inferior = saved_inferior; | |
da6d8c04 DJ |
323 | return 1; |
324 | } | |
325 | ||
0d62e5e8 DJ |
326 | /* Return 1 if this process has an interesting status pending. This function |
327 | may silently resume an inferior process. */ | |
611cb4a5 | 328 | static int |
0d62e5e8 DJ |
329 | status_pending_p (struct inferior_list_entry *entry, void *dummy) |
330 | { | |
331 | struct process_info *process = (struct process_info *) entry; | |
332 | ||
333 | if (process->status_pending_p) | |
334 | if (check_removed_breakpoint (process)) | |
335 | { | |
336 | /* This thread was stopped at a breakpoint, and the breakpoint | |
337 | is now gone. We were told to continue (or step...) all threads, | |
338 | so GDB isn't trying to single-step past this breakpoint. | |
339 | So instead of reporting the old SIGTRAP, pretend we got to | |
340 | the breakpoint just after it was removed instead of just | |
341 | before; resume the process. */ | |
342 | linux_resume_one_process (&process->head, 0, 0); | |
343 | return 0; | |
344 | } | |
345 | ||
346 | return process->status_pending_p; | |
347 | } | |
348 | ||
349 | static void | |
350 | linux_wait_for_process (struct process_info **childp, int *wstatp) | |
611cb4a5 | 351 | { |
0d62e5e8 DJ |
352 | int ret; |
353 | int to_wait_for = -1; | |
354 | ||
355 | if (*childp != NULL) | |
356 | to_wait_for = (*childp)->lwpid; | |
611cb4a5 DJ |
357 | |
358 | while (1) | |
359 | { | |
0d62e5e8 DJ |
360 | ret = waitpid (to_wait_for, wstatp, WNOHANG); |
361 | ||
362 | if (ret == -1) | |
363 | { | |
364 | if (errno != ECHILD) | |
365 | perror_with_name ("waitpid"); | |
366 | } | |
367 | else if (ret > 0) | |
368 | break; | |
369 | ||
370 | ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE); | |
371 | ||
372 | if (ret == -1) | |
373 | { | |
374 | if (errno != ECHILD) | |
375 | perror_with_name ("waitpid (WCLONE)"); | |
376 | } | |
377 | else if (ret > 0) | |
378 | break; | |
379 | ||
380 | usleep (1000); | |
381 | } | |
382 | ||
383 | if (debug_threads | |
384 | && (!WIFSTOPPED (*wstatp) | |
385 | || (WSTOPSIG (*wstatp) != 32 | |
386 | && WSTOPSIG (*wstatp) != 33))) | |
387 | fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp); | |
388 | ||
389 | if (to_wait_for == -1) | |
390 | *childp = (struct process_info *) find_inferior_id (&all_processes, ret); | |
391 | ||
392 | (*childp)->stopped = 1; | |
393 | (*childp)->pending_is_breakpoint = 0; | |
394 | ||
395 | if (debug_threads | |
396 | && WIFSTOPPED (*wstatp)) | |
397 | { | |
398 | current_inferior = (struct thread_info *) | |
399 | find_inferior_id (&all_threads, (*childp)->tid); | |
400 | /* For testing only; i386_stop_pc prints out a diagnostic. */ | |
401 | if (the_low_target.get_pc != NULL) | |
402 | get_stop_pc (); | |
403 | } | |
404 | } | |
611cb4a5 | 405 | |
0d62e5e8 DJ |
406 | static int |
407 | linux_wait_for_event (struct thread_info *child) | |
408 | { | |
409 | CORE_ADDR stop_pc; | |
410 | struct process_info *event_child; | |
411 | int wstat; | |
412 | ||
413 | /* Check for a process with a pending status. */ | |
414 | /* It is possible that the user changed the pending task's registers since | |
415 | it stopped. We correctly handle the change of PC if we hit a breakpoint | |
e5379b03 | 416 | (in check_removed_breakpoint); signals should be reported anyway. */ |
0d62e5e8 DJ |
417 | if (child == NULL) |
418 | { | |
419 | event_child = (struct process_info *) | |
420 | find_inferior (&all_processes, status_pending_p, NULL); | |
421 | if (debug_threads && event_child) | |
422 | fprintf (stderr, "Got a pending child %d\n", event_child->lwpid); | |
423 | } | |
424 | else | |
425 | { | |
426 | event_child = get_thread_process (child); | |
427 | if (event_child->status_pending_p | |
428 | && check_removed_breakpoint (event_child)) | |
429 | event_child = NULL; | |
430 | } | |
611cb4a5 | 431 | |
0d62e5e8 DJ |
432 | if (event_child != NULL) |
433 | { | |
434 | if (event_child->status_pending_p) | |
611cb4a5 | 435 | { |
0d62e5e8 DJ |
436 | if (debug_threads) |
437 | fprintf (stderr, "Got an event from pending child %d (%04x)\n", | |
438 | event_child->lwpid, event_child->status_pending); | |
439 | wstat = event_child->status_pending; | |
440 | event_child->status_pending_p = 0; | |
441 | event_child->status_pending = 0; | |
442 | current_inferior = get_process_thread (event_child); | |
443 | return wstat; | |
444 | } | |
445 | } | |
446 | ||
447 | /* We only enter this loop if no process has a pending wait status. Thus | |
448 | any action taken in response to a wait status inside this loop is | |
449 | responding as soon as we detect the status, not after any pending | |
450 | events. */ | |
451 | while (1) | |
452 | { | |
453 | if (child == NULL) | |
454 | event_child = NULL; | |
455 | else | |
456 | event_child = get_thread_process (child); | |
457 | ||
458 | linux_wait_for_process (&event_child, &wstat); | |
459 | ||
460 | if (event_child == NULL) | |
461 | error ("event from unknown child"); | |
611cb4a5 | 462 | |
0d62e5e8 DJ |
463 | current_inferior = (struct thread_info *) |
464 | find_inferior_id (&all_threads, event_child->tid); | |
465 | ||
466 | if (using_threads) | |
467 | { | |
468 | /* Check for thread exit. */ | |
469 | if (! WIFSTOPPED (wstat)) | |
611cb4a5 | 470 | { |
0d62e5e8 DJ |
471 | if (debug_threads) |
472 | fprintf (stderr, "Thread %d (LWP %d) exiting\n", | |
473 | event_child->tid, event_child->head.id); | |
474 | ||
475 | /* If the last thread is exiting, just return. */ | |
476 | if (all_threads.head == all_threads.tail) | |
477 | return wstat; | |
478 | ||
479 | dead_thread_notify (event_child->tid); | |
480 | ||
481 | remove_inferior (&all_processes, &event_child->head); | |
482 | free (event_child); | |
483 | remove_thread (current_inferior); | |
484 | current_inferior = (struct thread_info *) all_threads.head; | |
485 | ||
486 | /* If we were waiting for this particular child to do something... | |
487 | well, it did something. */ | |
488 | if (child != NULL) | |
489 | return wstat; | |
490 | ||
491 | /* Wait for a more interesting event. */ | |
611cb4a5 DJ |
492 | continue; |
493 | } | |
494 | ||
0d62e5e8 DJ |
495 | if (WIFSTOPPED (wstat) |
496 | && WSTOPSIG (wstat) == SIGSTOP | |
497 | && event_child->stop_expected) | |
498 | { | |
499 | if (debug_threads) | |
500 | fprintf (stderr, "Expected stop.\n"); | |
501 | event_child->stop_expected = 0; | |
502 | linux_resume_one_process (&event_child->head, | |
503 | event_child->stepping, 0); | |
504 | continue; | |
505 | } | |
611cb4a5 | 506 | |
0d62e5e8 DJ |
507 | /* FIXME drow/2002-06-09: Get signal numbers from the inferior's |
508 | thread library? */ | |
509 | if (WIFSTOPPED (wstat) | |
254787d4 DJ |
510 | && (WSTOPSIG (wstat) == __SIGRTMIN |
511 | || WSTOPSIG (wstat) == __SIGRTMIN + 1)) | |
611cb4a5 | 512 | { |
0d62e5e8 DJ |
513 | if (debug_threads) |
514 | fprintf (stderr, "Ignored signal %d for %d (LWP %d).\n", | |
515 | WSTOPSIG (wstat), event_child->tid, | |
516 | event_child->head.id); | |
517 | linux_resume_one_process (&event_child->head, | |
518 | event_child->stepping, | |
519 | WSTOPSIG (wstat)); | |
520 | continue; | |
521 | } | |
522 | } | |
611cb4a5 | 523 | |
0d62e5e8 DJ |
524 | /* If this event was not handled above, and is not a SIGTRAP, report |
525 | it. */ | |
526 | if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP) | |
527 | return wstat; | |
611cb4a5 | 528 | |
0d62e5e8 DJ |
529 | /* If this target does not support breakpoints, we simply report the |
530 | SIGTRAP; it's of no concern to us. */ | |
531 | if (the_low_target.get_pc == NULL) | |
532 | return wstat; | |
533 | ||
534 | stop_pc = get_stop_pc (); | |
535 | ||
536 | /* bp_reinsert will only be set if we were single-stepping. | |
537 | Notice that we will resume the process after hitting | |
538 | a gdbserver breakpoint; single-stepping to/over one | |
539 | is not supported (yet). */ | |
540 | if (event_child->bp_reinsert != 0) | |
541 | { | |
542 | if (debug_threads) | |
543 | fprintf (stderr, "Reinserted breakpoint.\n"); | |
544 | reinsert_breakpoint (event_child->bp_reinsert); | |
545 | event_child->bp_reinsert = 0; | |
546 | ||
547 | /* Clear the single-stepping flag and SIGTRAP as we resume. */ | |
548 | linux_resume_one_process (&event_child->head, 0, 0); | |
549 | continue; | |
550 | } | |
551 | ||
552 | if (debug_threads) | |
553 | fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n"); | |
554 | ||
555 | if (check_breakpoints (stop_pc) != 0) | |
556 | { | |
557 | /* We hit one of our own breakpoints. We mark it as a pending | |
e5379b03 | 558 | breakpoint, so that check_removed_breakpoint () will do the PC |
0d62e5e8 DJ |
559 | adjustment for us at the appropriate time. */ |
560 | event_child->pending_is_breakpoint = 1; | |
561 | event_child->pending_stop_pc = stop_pc; | |
562 | ||
563 | /* Now we need to put the breakpoint back. We continue in the event | |
564 | loop instead of simply replacing the breakpoint right away, | |
565 | in order to not lose signals sent to the thread that hit the | |
566 | breakpoint. Unfortunately this increases the window where another | |
567 | thread could sneak past the removed breakpoint. For the current | |
568 | use of server-side breakpoints (thread creation) this is | |
569 | acceptable; but it needs to be considered before this breakpoint | |
570 | mechanism can be used in more general ways. For some breakpoints | |
571 | it may be necessary to stop all other threads, but that should | |
572 | be avoided where possible. | |
573 | ||
574 | If breakpoint_reinsert_addr is NULL, that means that we can | |
575 | use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint, | |
576 | mark it for reinsertion, and single-step. | |
577 | ||
578 | Otherwise, call the target function to figure out where we need | |
579 | our temporary breakpoint, create it, and continue executing this | |
580 | process. */ | |
581 | if (the_low_target.breakpoint_reinsert_addr == NULL) | |
582 | { | |
583 | event_child->bp_reinsert = stop_pc; | |
584 | uninsert_breakpoint (stop_pc); | |
585 | linux_resume_one_process (&event_child->head, 1, 0); | |
586 | } | |
587 | else | |
588 | { | |
589 | reinsert_breakpoint_by_bp | |
590 | (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ()); | |
591 | linux_resume_one_process (&event_child->head, 0, 0); | |
611cb4a5 | 592 | } |
0d62e5e8 DJ |
593 | |
594 | continue; | |
595 | } | |
596 | ||
597 | /* If we were single-stepping, we definitely want to report the | |
598 | SIGTRAP. The single-step operation has completed, so also | |
599 | clear the stepping flag; in general this does not matter, | |
600 | because the SIGTRAP will be reported to the client, which | |
601 | will give us a new action for this thread, but clear it for | |
602 | consistency anyway. It's safe to clear the stepping flag | |
603 | because the only consumer of get_stop_pc () after this point | |
e5379b03 | 604 | is check_removed_breakpoint, and pending_is_breakpoint is not |
0d62e5e8 DJ |
605 | set. It might be wiser to use a step_completed flag instead. */ |
606 | if (event_child->stepping) | |
607 | { | |
608 | event_child->stepping = 0; | |
609 | return wstat; | |
610 | } | |
611 | ||
612 | /* A SIGTRAP that we can't explain. It may have been a breakpoint. | |
613 | Check if it is a breakpoint, and if so mark the process information | |
614 | accordingly. This will handle both the necessary fiddling with the | |
615 | PC on decr_pc_after_break targets and suppressing extra threads | |
616 | hitting a breakpoint if two hit it at once and then GDB removes it | |
617 | after the first is reported. Arguably it would be better to report | |
618 | multiple threads hitting breakpoints simultaneously, but the current | |
619 | remote protocol does not allow this. */ | |
620 | if ((*the_low_target.breakpoint_at) (stop_pc)) | |
621 | { | |
622 | event_child->pending_is_breakpoint = 1; | |
623 | event_child->pending_stop_pc = stop_pc; | |
611cb4a5 DJ |
624 | } |
625 | ||
626 | return wstat; | |
627 | } | |
0d62e5e8 | 628 | |
611cb4a5 DJ |
629 | /* NOTREACHED */ |
630 | return 0; | |
631 | } | |
632 | ||
0d62e5e8 | 633 | /* Wait for process, returns status. */ |
da6d8c04 | 634 | |
ce3a066d DJ |
635 | static unsigned char |
636 | linux_wait (char *status) | |
da6d8c04 | 637 | { |
e5f1222d | 638 | int w; |
0d62e5e8 DJ |
639 | struct thread_info *child = NULL; |
640 | ||
641 | retry: | |
642 | /* If we were only supposed to resume one thread, only wait for | |
643 | that thread - if it's still alive. If it died, however - which | |
644 | can happen if we're coming from the thread death case below - | |
645 | then we need to make sure we restart the other threads. We could | |
646 | pick a thread at random or restart all; restarting all is less | |
647 | arbitrary. */ | |
648 | if (cont_thread > 0) | |
649 | { | |
650 | child = (struct thread_info *) find_inferior_id (&all_threads, | |
651 | cont_thread); | |
652 | ||
653 | /* No stepping, no signal - unless one is pending already, of course. */ | |
654 | if (child == NULL) | |
655 | linux_resume (0, 0); | |
656 | } | |
da6d8c04 DJ |
657 | |
658 | enable_async_io (); | |
0d62e5e8 DJ |
659 | w = linux_wait_for_event (child); |
660 | stop_all_processes (); | |
da6d8c04 | 661 | disable_async_io (); |
da6d8c04 | 662 | |
0d62e5e8 DJ |
663 | /* If we are waiting for a particular child, and it exited, |
664 | linux_wait_for_event will return its exit status. Similarly if | |
665 | the last child exited. If this is not the last child, however, | |
666 | do not report it as exited until there is a 'thread exited' response | |
667 | available in the remote protocol. Instead, just wait for another event. | |
668 | This should be safe, because if the thread crashed we will already | |
669 | have reported the termination signal to GDB; that should stop any | |
670 | in-progress stepping operations, etc. | |
671 | ||
672 | Report the exit status of the last thread to exit. This matches | |
673 | LinuxThreads' behavior. */ | |
674 | ||
675 | if (all_threads.head == all_threads.tail) | |
da6d8c04 | 676 | { |
0d62e5e8 DJ |
677 | if (WIFEXITED (w)) |
678 | { | |
679 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
680 | *status = 'W'; | |
681 | clear_inferiors (); | |
682 | return ((unsigned char) WEXITSTATUS (w)); | |
683 | } | |
684 | else if (!WIFSTOPPED (w)) | |
685 | { | |
686 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
687 | clear_inferiors (); | |
688 | *status = 'X'; | |
689 | return ((unsigned char) WTERMSIG (w)); | |
690 | } | |
da6d8c04 | 691 | } |
0d62e5e8 | 692 | else |
da6d8c04 | 693 | { |
0d62e5e8 DJ |
694 | if (!WIFSTOPPED (w)) |
695 | goto retry; | |
da6d8c04 DJ |
696 | } |
697 | ||
da6d8c04 DJ |
698 | *status = 'T'; |
699 | return ((unsigned char) WSTOPSIG (w)); | |
700 | } | |
701 | ||
0d62e5e8 DJ |
702 | static void |
703 | send_sigstop (struct inferior_list_entry *entry) | |
704 | { | |
705 | struct process_info *process = (struct process_info *) entry; | |
706 | ||
707 | if (process->stopped) | |
708 | return; | |
709 | ||
710 | /* If we already have a pending stop signal for this process, don't | |
711 | send another. */ | |
712 | if (process->stop_expected) | |
713 | { | |
714 | process->stop_expected = 0; | |
715 | return; | |
716 | } | |
717 | ||
718 | if (debug_threads) | |
719 | fprintf (stderr, "Sending sigstop to process %d\n", process->head.id); | |
720 | ||
721 | kill (process->head.id, SIGSTOP); | |
722 | process->sigstop_sent = 1; | |
723 | } | |
724 | ||
725 | static void | |
726 | wait_for_sigstop (struct inferior_list_entry *entry) | |
727 | { | |
728 | struct process_info *process = (struct process_info *) entry; | |
729 | struct thread_info *saved_inferior, *thread; | |
730 | int wstat, saved_tid; | |
731 | ||
732 | if (process->stopped) | |
733 | return; | |
734 | ||
735 | saved_inferior = current_inferior; | |
736 | saved_tid = ((struct inferior_list_entry *) saved_inferior)->id; | |
737 | thread = (struct thread_info *) find_inferior_id (&all_threads, | |
738 | process->tid); | |
739 | wstat = linux_wait_for_event (thread); | |
740 | ||
741 | /* If we stopped with a non-SIGSTOP signal, save it for later | |
742 | and record the pending SIGSTOP. If the process exited, just | |
743 | return. */ | |
744 | if (WIFSTOPPED (wstat) | |
745 | && WSTOPSIG (wstat) != SIGSTOP) | |
746 | { | |
747 | if (debug_threads) | |
748 | fprintf (stderr, "Stopped with non-sigstop signal\n"); | |
749 | process->status_pending_p = 1; | |
750 | process->status_pending = wstat; | |
751 | process->stop_expected = 1; | |
752 | } | |
753 | ||
754 | if (linux_thread_alive (saved_tid)) | |
755 | current_inferior = saved_inferior; | |
756 | else | |
757 | { | |
758 | if (debug_threads) | |
759 | fprintf (stderr, "Previously current thread died.\n"); | |
760 | ||
761 | /* Set a valid thread as current. */ | |
762 | set_desired_inferior (0); | |
763 | } | |
764 | } | |
765 | ||
766 | static void | |
767 | stop_all_processes (void) | |
768 | { | |
769 | stopping_threads = 1; | |
770 | for_each_inferior (&all_processes, send_sigstop); | |
771 | for_each_inferior (&all_processes, wait_for_sigstop); | |
772 | stopping_threads = 0; | |
773 | } | |
774 | ||
da6d8c04 DJ |
775 | /* Resume execution of the inferior process. |
776 | If STEP is nonzero, single-step it. | |
777 | If SIGNAL is nonzero, give it that signal. */ | |
778 | ||
ce3a066d | 779 | static void |
0d62e5e8 DJ |
780 | linux_resume_one_process (struct inferior_list_entry *entry, |
781 | int step, int signal) | |
da6d8c04 | 782 | { |
0d62e5e8 DJ |
783 | struct process_info *process = (struct process_info *) entry; |
784 | struct thread_info *saved_inferior; | |
785 | ||
786 | if (process->stopped == 0) | |
787 | return; | |
788 | ||
789 | /* If we have pending signals or status, and a new signal, enqueue the | |
790 | signal. Also enqueue the signal if we are waiting to reinsert a | |
791 | breakpoint; it will be picked up again below. */ | |
792 | if (signal != 0 | |
793 | && (process->status_pending_p || process->pending_signals != NULL | |
794 | || process->bp_reinsert != 0)) | |
795 | { | |
796 | struct pending_signals *p_sig; | |
797 | p_sig = malloc (sizeof (*p_sig)); | |
798 | p_sig->prev = process->pending_signals; | |
799 | p_sig->signal = signal; | |
800 | process->pending_signals = p_sig; | |
801 | } | |
802 | ||
e5379b03 | 803 | if (process->status_pending_p && !check_removed_breakpoint (process)) |
0d62e5e8 DJ |
804 | return; |
805 | ||
806 | saved_inferior = current_inferior; | |
807 | current_inferior = get_process_thread (process); | |
808 | ||
809 | if (debug_threads) | |
810 | fprintf (stderr, "Resuming process %d (%s, signal %d, stop %s)\n", inferior_pid, | |
811 | step ? "step" : "continue", signal, | |
812 | process->stop_expected ? "expected" : "not expected"); | |
813 | ||
814 | /* This bit needs some thinking about. If we get a signal that | |
815 | we must report while a single-step reinsert is still pending, | |
816 | we often end up resuming the thread. It might be better to | |
817 | (ew) allow a stack of pending events; then we could be sure that | |
818 | the reinsert happened right away and not lose any signals. | |
819 | ||
820 | Making this stack would also shrink the window in which breakpoints are | |
821 | uninserted (see comment in linux_wait_for_process) but not enough for | |
822 | complete correctness, so it won't solve that problem. It may be | |
823 | worthwhile just to solve this one, however. */ | |
824 | if (process->bp_reinsert != 0) | |
825 | { | |
826 | if (debug_threads) | |
827 | fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert); | |
828 | if (step == 0) | |
829 | fprintf (stderr, "BAD - reinserting but not stepping.\n"); | |
830 | step = 1; | |
831 | ||
832 | /* Postpone any pending signal. It was enqueued above. */ | |
833 | signal = 0; | |
834 | } | |
835 | ||
836 | check_removed_breakpoint (process); | |
837 | ||
838 | if (debug_threads && the_low_target.get_pc != NULL) | |
839 | { | |
840 | fprintf (stderr, " "); | |
841 | (long) (*the_low_target.get_pc) (); | |
842 | } | |
843 | ||
844 | /* If we have pending signals, consume one unless we are trying to reinsert | |
845 | a breakpoint. */ | |
846 | if (process->pending_signals != NULL && process->bp_reinsert == 0) | |
847 | { | |
848 | struct pending_signals **p_sig; | |
849 | ||
850 | p_sig = &process->pending_signals; | |
851 | while ((*p_sig)->prev != NULL) | |
852 | p_sig = &(*p_sig)->prev; | |
853 | ||
854 | signal = (*p_sig)->signal; | |
855 | free (*p_sig); | |
856 | *p_sig = NULL; | |
857 | } | |
858 | ||
859 | regcache_invalidate_one ((struct inferior_list_entry *) | |
860 | get_process_thread (process)); | |
da6d8c04 | 861 | errno = 0; |
0d62e5e8 DJ |
862 | process->stopped = 0; |
863 | process->stepping = step; | |
864 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal); | |
865 | ||
866 | current_inferior = saved_inferior; | |
da6d8c04 DJ |
867 | if (errno) |
868 | perror_with_name ("ptrace"); | |
869 | } | |
870 | ||
0d62e5e8 DJ |
871 | /* This function is called once per process other than the first |
872 | one. The first process we are told the signal to continue | |
873 | with, and whether to step or continue; for all others, any | |
874 | existing signals will be marked in status_pending_p to be | |
875 | reported momentarily, and we preserve the stepping flag. */ | |
876 | static void | |
877 | linux_continue_one_process (struct inferior_list_entry *entry) | |
878 | { | |
879 | struct process_info *process; | |
c6ecbae5 | 880 | |
0d62e5e8 DJ |
881 | process = (struct process_info *) entry; |
882 | linux_resume_one_process (entry, process->stepping, 0); | |
883 | } | |
884 | ||
885 | static void | |
886 | linux_resume (int step, int signal) | |
887 | { | |
888 | struct process_info *process; | |
889 | ||
890 | process = get_thread_process (current_inferior); | |
891 | ||
892 | /* If the current process has a status pending, this signal will | |
893 | be enqueued and sent later. */ | |
894 | linux_resume_one_process (&process->head, step, signal); | |
c6ecbae5 | 895 | |
0d62e5e8 DJ |
896 | if (cont_thread == 0 || cont_thread == -1) |
897 | for_each_inferior (&all_processes, linux_continue_one_process); | |
898 | } | |
899 | ||
900 | #ifdef HAVE_LINUX_USRREGS | |
da6d8c04 DJ |
901 | |
902 | int | |
0a30fbc4 | 903 | register_addr (int regnum) |
da6d8c04 DJ |
904 | { |
905 | int addr; | |
906 | ||
2ec06d2e | 907 | if (regnum < 0 || regnum >= the_low_target.num_regs) |
da6d8c04 DJ |
908 | error ("Invalid register number %d.", regnum); |
909 | ||
2ec06d2e | 910 | addr = the_low_target.regmap[regnum]; |
da6d8c04 DJ |
911 | |
912 | return addr; | |
913 | } | |
914 | ||
58caa3dc | 915 | /* Fetch one register. */ |
da6d8c04 DJ |
916 | static void |
917 | fetch_register (int regno) | |
918 | { | |
919 | CORE_ADDR regaddr; | |
920 | register int i; | |
0d62e5e8 | 921 | char *buf; |
da6d8c04 | 922 | |
2ec06d2e | 923 | if (regno >= the_low_target.num_regs) |
0a30fbc4 | 924 | return; |
2ec06d2e | 925 | if ((*the_low_target.cannot_fetch_register) (regno)) |
0a30fbc4 | 926 | return; |
da6d8c04 | 927 | |
0a30fbc4 DJ |
928 | regaddr = register_addr (regno); |
929 | if (regaddr == -1) | |
930 | return; | |
0d62e5e8 DJ |
931 | buf = alloca (register_size (regno)); |
932 | for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE)) | |
da6d8c04 DJ |
933 | { |
934 | errno = 0; | |
0d62e5e8 | 935 | *(PTRACE_XFER_TYPE *) (buf + i) = |
da6d8c04 DJ |
936 | ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0); |
937 | regaddr += sizeof (PTRACE_XFER_TYPE); | |
938 | if (errno != 0) | |
939 | { | |
940 | /* Warning, not error, in case we are attached; sometimes the | |
941 | kernel doesn't let us at the registers. */ | |
942 | char *err = strerror (errno); | |
943 | char *msg = alloca (strlen (err) + 128); | |
944 | sprintf (msg, "reading register %d: %s", regno, err); | |
945 | error (msg); | |
946 | goto error_exit; | |
947 | } | |
948 | } | |
0d62e5e8 DJ |
949 | supply_register (regno, buf); |
950 | ||
da6d8c04 DJ |
951 | error_exit:; |
952 | } | |
953 | ||
954 | /* Fetch all registers, or just one, from the child process. */ | |
58caa3dc DJ |
955 | static void |
956 | usr_fetch_inferior_registers (int regno) | |
da6d8c04 DJ |
957 | { |
958 | if (regno == -1 || regno == 0) | |
2ec06d2e | 959 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
da6d8c04 DJ |
960 | fetch_register (regno); |
961 | else | |
962 | fetch_register (regno); | |
963 | } | |
964 | ||
965 | /* Store our register values back into the inferior. | |
966 | If REGNO is -1, do this for all registers. | |
967 | Otherwise, REGNO specifies which register (so we can save time). */ | |
58caa3dc DJ |
968 | static void |
969 | usr_store_inferior_registers (int regno) | |
da6d8c04 DJ |
970 | { |
971 | CORE_ADDR regaddr; | |
972 | int i; | |
0d62e5e8 | 973 | char *buf; |
da6d8c04 DJ |
974 | |
975 | if (regno >= 0) | |
976 | { | |
2ec06d2e | 977 | if (regno >= the_low_target.num_regs) |
0a30fbc4 DJ |
978 | return; |
979 | ||
bc1e36ca | 980 | if ((*the_low_target.cannot_store_register) (regno) == 1) |
0a30fbc4 DJ |
981 | return; |
982 | ||
983 | regaddr = register_addr (regno); | |
984 | if (regaddr == -1) | |
da6d8c04 | 985 | return; |
da6d8c04 | 986 | errno = 0; |
0d62e5e8 DJ |
987 | buf = alloca (register_size (regno)); |
988 | collect_register (regno, buf); | |
989 | for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE)) | |
da6d8c04 | 990 | { |
0a30fbc4 DJ |
991 | errno = 0; |
992 | ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
2ff29de4 | 993 | *(PTRACE_XFER_TYPE *) (buf + i)); |
da6d8c04 DJ |
994 | if (errno != 0) |
995 | { | |
bc1e36ca DJ |
996 | if ((*the_low_target.cannot_store_register) (regno) == 0) |
997 | { | |
998 | char *err = strerror (errno); | |
999 | char *msg = alloca (strlen (err) + 128); | |
1000 | sprintf (msg, "writing register %d: %s", | |
1001 | regno, err); | |
1002 | error (msg); | |
1003 | return; | |
1004 | } | |
da6d8c04 | 1005 | } |
2ff29de4 | 1006 | regaddr += sizeof (PTRACE_XFER_TYPE); |
da6d8c04 | 1007 | } |
da6d8c04 DJ |
1008 | } |
1009 | else | |
2ec06d2e | 1010 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
0d62e5e8 | 1011 | usr_store_inferior_registers (regno); |
da6d8c04 | 1012 | } |
58caa3dc DJ |
1013 | #endif /* HAVE_LINUX_USRREGS */ |
1014 | ||
1015 | ||
1016 | ||
1017 | #ifdef HAVE_LINUX_REGSETS | |
1018 | ||
1019 | static int | |
0d62e5e8 | 1020 | regsets_fetch_inferior_registers () |
58caa3dc DJ |
1021 | { |
1022 | struct regset_info *regset; | |
1023 | ||
1024 | regset = target_regsets; | |
1025 | ||
1026 | while (regset->size >= 0) | |
1027 | { | |
1028 | void *buf; | |
1029 | int res; | |
1030 | ||
1031 | if (regset->size == 0) | |
1032 | { | |
1033 | regset ++; | |
1034 | continue; | |
1035 | } | |
1036 | ||
1037 | buf = malloc (regset->size); | |
d06f167a | 1038 | res = ptrace (regset->get_request, inferior_pid, 0, buf); |
58caa3dc DJ |
1039 | if (res < 0) |
1040 | { | |
1041 | if (errno == EIO) | |
1042 | { | |
1043 | /* If we get EIO on the first regset, do not try regsets again. | |
1044 | If we get EIO on a later regset, disable that regset. */ | |
1045 | if (regset == target_regsets) | |
1046 | { | |
1047 | use_regsets_p = 0; | |
1048 | return -1; | |
1049 | } | |
1050 | else | |
1051 | { | |
1052 | regset->size = 0; | |
1053 | continue; | |
1054 | } | |
1055 | } | |
1056 | else | |
1057 | { | |
0d62e5e8 DJ |
1058 | char s[256]; |
1059 | sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d", | |
1060 | inferior_pid); | |
1061 | perror (s); | |
58caa3dc DJ |
1062 | } |
1063 | } | |
1064 | regset->store_function (buf); | |
1065 | regset ++; | |
1066 | } | |
ce3a066d | 1067 | return 0; |
58caa3dc DJ |
1068 | } |
1069 | ||
1070 | static int | |
0d62e5e8 | 1071 | regsets_store_inferior_registers () |
58caa3dc DJ |
1072 | { |
1073 | struct regset_info *regset; | |
1074 | ||
1075 | regset = target_regsets; | |
1076 | ||
1077 | while (regset->size >= 0) | |
1078 | { | |
1079 | void *buf; | |
1080 | int res; | |
1081 | ||
1082 | if (regset->size == 0) | |
1083 | { | |
1084 | regset ++; | |
1085 | continue; | |
1086 | } | |
1087 | ||
1088 | buf = malloc (regset->size); | |
1089 | regset->fill_function (buf); | |
d06f167a | 1090 | res = ptrace (regset->set_request, inferior_pid, 0, buf); |
58caa3dc DJ |
1091 | if (res < 0) |
1092 | { | |
1093 | if (errno == EIO) | |
1094 | { | |
1095 | /* If we get EIO on the first regset, do not try regsets again. | |
1096 | If we get EIO on a later regset, disable that regset. */ | |
1097 | if (regset == target_regsets) | |
1098 | { | |
1099 | use_regsets_p = 0; | |
1100 | return -1; | |
1101 | } | |
1102 | else | |
1103 | { | |
1104 | regset->size = 0; | |
1105 | continue; | |
1106 | } | |
1107 | } | |
1108 | else | |
1109 | { | |
ce3a066d | 1110 | perror ("Warning: ptrace(regsets_store_inferior_registers)"); |
58caa3dc DJ |
1111 | } |
1112 | } | |
1113 | regset ++; | |
09ec9b38 | 1114 | free (buf); |
58caa3dc | 1115 | } |
ce3a066d | 1116 | return 0; |
58caa3dc DJ |
1117 | } |
1118 | ||
1119 | #endif /* HAVE_LINUX_REGSETS */ | |
1120 | ||
1121 | ||
1122 | void | |
ce3a066d | 1123 | linux_fetch_registers (int regno) |
58caa3dc DJ |
1124 | { |
1125 | #ifdef HAVE_LINUX_REGSETS | |
1126 | if (use_regsets_p) | |
1127 | { | |
1128 | if (regsets_fetch_inferior_registers () == 0) | |
1129 | return; | |
1130 | } | |
1131 | #endif | |
1132 | #ifdef HAVE_LINUX_USRREGS | |
1133 | usr_fetch_inferior_registers (regno); | |
1134 | #endif | |
1135 | } | |
1136 | ||
1137 | void | |
ce3a066d | 1138 | linux_store_registers (int regno) |
58caa3dc DJ |
1139 | { |
1140 | #ifdef HAVE_LINUX_REGSETS | |
1141 | if (use_regsets_p) | |
1142 | { | |
1143 | if (regsets_store_inferior_registers () == 0) | |
1144 | return; | |
1145 | } | |
1146 | #endif | |
1147 | #ifdef HAVE_LINUX_USRREGS | |
1148 | usr_store_inferior_registers (regno); | |
1149 | #endif | |
1150 | } | |
1151 | ||
da6d8c04 | 1152 | |
da6d8c04 DJ |
1153 | /* Copy LEN bytes from inferior's memory starting at MEMADDR |
1154 | to debugger memory starting at MYADDR. */ | |
1155 | ||
ce3a066d DJ |
1156 | static void |
1157 | linux_read_memory (CORE_ADDR memaddr, char *myaddr, int len) | |
da6d8c04 DJ |
1158 | { |
1159 | register int i; | |
1160 | /* Round starting address down to longword boundary. */ | |
1161 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
1162 | /* Round ending address up; get number of longwords that makes. */ | |
1163 | register int count | |
1164 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) | |
1165 | / sizeof (PTRACE_XFER_TYPE); | |
1166 | /* Allocate buffer of that many longwords. */ | |
1167 | register PTRACE_XFER_TYPE *buffer | |
1168 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); | |
1169 | ||
1170 | /* Read all the longwords */ | |
1171 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
1172 | { | |
d844cde6 | 1173 | buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0); |
da6d8c04 DJ |
1174 | } |
1175 | ||
1176 | /* Copy appropriate bytes out of the buffer. */ | |
1177 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len); | |
1178 | } | |
1179 | ||
1180 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
1181 | to inferior's memory at MEMADDR. | |
1182 | On failure (cannot write the inferior) | |
1183 | returns the value of errno. */ | |
1184 | ||
ce3a066d | 1185 | static int |
611cb4a5 | 1186 | linux_write_memory (CORE_ADDR memaddr, const char *myaddr, int len) |
da6d8c04 DJ |
1187 | { |
1188 | register int i; | |
1189 | /* Round starting address down to longword boundary. */ | |
1190 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
1191 | /* Round ending address up; get number of longwords that makes. */ | |
1192 | register int count | |
1193 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE); | |
1194 | /* Allocate buffer of that many longwords. */ | |
1195 | register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); | |
1196 | extern int errno; | |
1197 | ||
0d62e5e8 DJ |
1198 | if (debug_threads) |
1199 | { | |
1200 | fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr); | |
1201 | } | |
1202 | ||
da6d8c04 DJ |
1203 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
1204 | ||
d844cde6 DJ |
1205 | buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, |
1206 | (PTRACE_ARG3_TYPE) addr, 0); | |
da6d8c04 DJ |
1207 | |
1208 | if (count > 1) | |
1209 | { | |
1210 | buffer[count - 1] | |
1211 | = ptrace (PTRACE_PEEKTEXT, inferior_pid, | |
d844cde6 DJ |
1212 | (PTRACE_ARG3_TYPE) (addr + (count - 1) |
1213 | * sizeof (PTRACE_XFER_TYPE)), | |
1214 | 0); | |
da6d8c04 DJ |
1215 | } |
1216 | ||
1217 | /* Copy data to be written over corresponding part of buffer */ | |
1218 | ||
1219 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len); | |
1220 | ||
1221 | /* Write the entire buffer. */ | |
1222 | ||
1223 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
1224 | { | |
1225 | errno = 0; | |
d844cde6 | 1226 | ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]); |
da6d8c04 DJ |
1227 | if (errno) |
1228 | return errno; | |
1229 | } | |
1230 | ||
1231 | return 0; | |
1232 | } | |
2f2893d9 DJ |
1233 | |
1234 | static void | |
1235 | linux_look_up_symbols (void) | |
1236 | { | |
0d62e5e8 DJ |
1237 | #ifdef USE_THREAD_DB |
1238 | if (using_threads) | |
1239 | return; | |
1240 | ||
1241 | using_threads = thread_db_init (); | |
1242 | #endif | |
1243 | } | |
1244 | ||
e5379b03 DJ |
1245 | static void |
1246 | linux_send_signal (int signum) | |
1247 | { | |
1248 | extern int signal_pid; | |
1249 | ||
1250 | if (cont_thread > 0) | |
1251 | { | |
1252 | struct process_info *process; | |
1253 | ||
1254 | process = get_thread_process (current_inferior); | |
1255 | kill (process->lwpid, signum); | |
1256 | } | |
1257 | else | |
1258 | kill (signal_pid, signum); | |
1259 | } | |
1260 | ||
da6d8c04 | 1261 | \f |
ce3a066d DJ |
1262 | static struct target_ops linux_target_ops = { |
1263 | linux_create_inferior, | |
1264 | linux_attach, | |
1265 | linux_kill, | |
6ad8ae5c | 1266 | linux_detach, |
ce3a066d DJ |
1267 | linux_thread_alive, |
1268 | linux_resume, | |
1269 | linux_wait, | |
1270 | linux_fetch_registers, | |
1271 | linux_store_registers, | |
1272 | linux_read_memory, | |
1273 | linux_write_memory, | |
2f2893d9 | 1274 | linux_look_up_symbols, |
e5379b03 | 1275 | linux_send_signal, |
ce3a066d DJ |
1276 | }; |
1277 | ||
0d62e5e8 DJ |
1278 | static void |
1279 | linux_init_signals () | |
1280 | { | |
1281 | /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads | |
1282 | to find what the cancel signal actually is. */ | |
254787d4 | 1283 | signal (__SIGRTMIN+1, SIG_IGN); |
0d62e5e8 DJ |
1284 | } |
1285 | ||
da6d8c04 DJ |
1286 | void |
1287 | initialize_low (void) | |
1288 | { | |
0d62e5e8 | 1289 | using_threads = 0; |
ce3a066d | 1290 | set_target_ops (&linux_target_ops); |
611cb4a5 DJ |
1291 | set_breakpoint_data (the_low_target.breakpoint, |
1292 | the_low_target.breakpoint_len); | |
0a30fbc4 | 1293 | init_registers (); |
0d62e5e8 | 1294 | linux_init_signals (); |
da6d8c04 | 1295 | } |