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