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