Commit | Line | Data |
---|---|---|
da6d8c04 | 1 | /* Low level interface to ptrace, for the remote server for GDB. |
545587ee | 2 | Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
9b254dd1 | 3 | 2006, 2007, 2008 Free Software Foundation, Inc. |
da6d8c04 DJ |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
da6d8c04 DJ |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
da6d8c04 DJ |
19 | |
20 | #include "server.h" | |
58caa3dc | 21 | #include "linux-low.h" |
da6d8c04 | 22 | |
58caa3dc | 23 | #include <sys/wait.h> |
da6d8c04 DJ |
24 | #include <stdio.h> |
25 | #include <sys/param.h> | |
da6d8c04 | 26 | #include <sys/ptrace.h> |
da6d8c04 DJ |
27 | #include <signal.h> |
28 | #include <sys/ioctl.h> | |
29 | #include <fcntl.h> | |
d07c63e7 | 30 | #include <string.h> |
0a30fbc4 DJ |
31 | #include <stdlib.h> |
32 | #include <unistd.h> | |
fa6a77dc | 33 | #include <errno.h> |
fd500816 | 34 | #include <sys/syscall.h> |
f9387fc3 | 35 | #include <sched.h> |
07e059b5 VP |
36 | #include <ctype.h> |
37 | #include <pwd.h> | |
38 | #include <sys/types.h> | |
39 | #include <dirent.h> | |
da6d8c04 | 40 | |
32ca6d61 DJ |
41 | #ifndef PTRACE_GETSIGINFO |
42 | # define PTRACE_GETSIGINFO 0x4202 | |
43 | # define PTRACE_SETSIGINFO 0x4203 | |
44 | #endif | |
45 | ||
fd462a61 DJ |
46 | #ifndef O_LARGEFILE |
47 | #define O_LARGEFILE 0 | |
48 | #endif | |
49 | ||
24a09b5f DJ |
50 | /* If the system headers did not provide the constants, hard-code the normal |
51 | values. */ | |
52 | #ifndef PTRACE_EVENT_FORK | |
53 | ||
54 | #define PTRACE_SETOPTIONS 0x4200 | |
55 | #define PTRACE_GETEVENTMSG 0x4201 | |
56 | ||
57 | /* options set using PTRACE_SETOPTIONS */ | |
58 | #define PTRACE_O_TRACESYSGOOD 0x00000001 | |
59 | #define PTRACE_O_TRACEFORK 0x00000002 | |
60 | #define PTRACE_O_TRACEVFORK 0x00000004 | |
61 | #define PTRACE_O_TRACECLONE 0x00000008 | |
62 | #define PTRACE_O_TRACEEXEC 0x00000010 | |
63 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 | |
64 | #define PTRACE_O_TRACEEXIT 0x00000040 | |
65 | ||
66 | /* Wait extended result codes for the above trace options. */ | |
67 | #define PTRACE_EVENT_FORK 1 | |
68 | #define PTRACE_EVENT_VFORK 2 | |
69 | #define PTRACE_EVENT_CLONE 3 | |
70 | #define PTRACE_EVENT_EXEC 4 | |
71 | #define PTRACE_EVENT_VFORK_DONE 5 | |
72 | #define PTRACE_EVENT_EXIT 6 | |
73 | ||
74 | #endif /* PTRACE_EVENT_FORK */ | |
75 | ||
76 | /* We can't always assume that this flag is available, but all systems | |
77 | with the ptrace event handlers also have __WALL, so it's safe to use | |
78 | in some contexts. */ | |
79 | #ifndef __WALL | |
80 | #define __WALL 0x40000000 /* Wait for any child. */ | |
81 | #endif | |
82 | ||
42c81e2a DJ |
83 | #ifdef __UCLIBC__ |
84 | #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__)) | |
85 | #define HAS_NOMMU | |
86 | #endif | |
87 | #endif | |
88 | ||
24a09b5f DJ |
89 | /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol |
90 | representation of the thread ID. | |
611cb4a5 | 91 | |
0d62e5e8 DJ |
92 | ``all_processes'' is keyed by the process ID - which on Linux is (presently) |
93 | the same as the LWP ID. */ | |
94 | ||
95 | struct inferior_list all_processes; | |
96 | ||
24a09b5f DJ |
97 | /* A list of all unknown processes which receive stop signals. Some other |
98 | process will presumably claim each of these as forked children | |
99 | momentarily. */ | |
100 | ||
101 | struct inferior_list stopped_pids; | |
102 | ||
0d62e5e8 DJ |
103 | /* FIXME this is a bit of a hack, and could be removed. */ |
104 | int stopping_threads; | |
105 | ||
106 | /* FIXME make into a target method? */ | |
24a09b5f DJ |
107 | int using_threads = 1; |
108 | static int thread_db_active; | |
109 | ||
110 | static int must_set_ptrace_flags; | |
0d62e5e8 | 111 | |
d61ddec4 UW |
112 | /* This flag is true iff we've just created or attached to a new inferior |
113 | but it has not stopped yet. As soon as it does, we need to call the | |
114 | low target's arch_setup callback. */ | |
115 | static int new_inferior; | |
116 | ||
0d62e5e8 | 117 | static void linux_resume_one_process (struct inferior_list_entry *entry, |
32ca6d61 | 118 | int step, int signal, siginfo_t *info); |
64386c31 | 119 | static void linux_resume (struct thread_resume *resume_info); |
0d62e5e8 DJ |
120 | static void stop_all_processes (void); |
121 | static int linux_wait_for_event (struct thread_info *child); | |
ae13219e | 122 | static int check_removed_breakpoint (struct process_info *event_child); |
24a09b5f | 123 | static void *add_process (unsigned long pid); |
97438e3f | 124 | static int my_waitpid (int pid, int *status, int flags); |
0d62e5e8 DJ |
125 | |
126 | struct pending_signals | |
127 | { | |
128 | int signal; | |
32ca6d61 | 129 | siginfo_t info; |
0d62e5e8 DJ |
130 | struct pending_signals *prev; |
131 | }; | |
611cb4a5 | 132 | |
d844cde6 | 133 | #define PTRACE_ARG3_TYPE long |
c6ecbae5 | 134 | #define PTRACE_XFER_TYPE long |
da6d8c04 | 135 | |
58caa3dc | 136 | #ifdef HAVE_LINUX_REGSETS |
52fa2412 UW |
137 | static char *disabled_regsets; |
138 | static int num_regsets; | |
58caa3dc DJ |
139 | #endif |
140 | ||
0d62e5e8 DJ |
141 | #define pid_of(proc) ((proc)->head.id) |
142 | ||
143 | /* FIXME: Delete eventually. */ | |
144 | #define inferior_pid (pid_of (get_thread_process (current_inferior))) | |
145 | ||
24a09b5f DJ |
146 | static void |
147 | handle_extended_wait (struct process_info *event_child, int wstat) | |
148 | { | |
149 | int event = wstat >> 16; | |
150 | struct process_info *new_process; | |
151 | ||
152 | if (event == PTRACE_EVENT_CLONE) | |
153 | { | |
154 | unsigned long new_pid; | |
836acd6d | 155 | int ret, status = W_STOPCODE (SIGSTOP); |
24a09b5f DJ |
156 | |
157 | ptrace (PTRACE_GETEVENTMSG, inferior_pid, 0, &new_pid); | |
158 | ||
159 | /* If we haven't already seen the new PID stop, wait for it now. */ | |
160 | if (! pull_pid_from_list (&stopped_pids, new_pid)) | |
161 | { | |
162 | /* The new child has a pending SIGSTOP. We can't affect it until it | |
163 | hits the SIGSTOP, but we're already attached. */ | |
164 | ||
97438e3f | 165 | ret = my_waitpid (new_pid, &status, __WALL); |
24a09b5f DJ |
166 | |
167 | if (ret == -1) | |
168 | perror_with_name ("waiting for new child"); | |
169 | else if (ret != new_pid) | |
170 | warning ("wait returned unexpected PID %d", ret); | |
da5898ce | 171 | else if (!WIFSTOPPED (status)) |
24a09b5f DJ |
172 | warning ("wait returned unexpected status 0x%x", status); |
173 | } | |
174 | ||
175 | ptrace (PTRACE_SETOPTIONS, new_pid, 0, PTRACE_O_TRACECLONE); | |
176 | ||
177 | new_process = (struct process_info *) add_process (new_pid); | |
178 | add_thread (new_pid, new_process, new_pid); | |
179 | new_thread_notify (thread_id_to_gdb_id (new_process->lwpid)); | |
180 | ||
da5898ce DJ |
181 | /* Normally we will get the pending SIGSTOP. But in some cases |
182 | we might get another signal delivered to the group first. | |
183 | If we do, be sure not to lose it. */ | |
184 | if (WSTOPSIG (status) == SIGSTOP) | |
185 | { | |
186 | if (stopping_threads) | |
187 | new_process->stopped = 1; | |
188 | else | |
189 | ptrace (PTRACE_CONT, new_pid, 0, 0); | |
190 | } | |
24a09b5f | 191 | else |
da5898ce DJ |
192 | { |
193 | new_process->stop_expected = 1; | |
194 | if (stopping_threads) | |
195 | { | |
196 | new_process->stopped = 1; | |
197 | new_process->status_pending_p = 1; | |
198 | new_process->status_pending = status; | |
199 | } | |
200 | else | |
201 | /* Pass the signal on. This is what GDB does - except | |
202 | shouldn't we really report it instead? */ | |
203 | ptrace (PTRACE_CONT, new_pid, 0, WSTOPSIG (status)); | |
204 | } | |
24a09b5f DJ |
205 | |
206 | /* Always resume the current thread. If we are stopping | |
207 | threads, it will have a pending SIGSTOP; we may as well | |
208 | collect it now. */ | |
209 | linux_resume_one_process (&event_child->head, | |
210 | event_child->stepping, 0, NULL); | |
211 | } | |
212 | } | |
213 | ||
0d62e5e8 DJ |
214 | /* This function should only be called if the process got a SIGTRAP. |
215 | The SIGTRAP could mean several things. | |
216 | ||
217 | On i386, where decr_pc_after_break is non-zero: | |
218 | If we were single-stepping this process using PTRACE_SINGLESTEP, | |
219 | we will get only the one SIGTRAP (even if the instruction we | |
220 | stepped over was a breakpoint). The value of $eip will be the | |
221 | next instruction. | |
222 | If we continue the process using PTRACE_CONT, we will get a | |
223 | SIGTRAP when we hit a breakpoint. The value of $eip will be | |
224 | the instruction after the breakpoint (i.e. needs to be | |
225 | decremented). If we report the SIGTRAP to GDB, we must also | |
226 | report the undecremented PC. If we cancel the SIGTRAP, we | |
227 | must resume at the decremented PC. | |
228 | ||
229 | (Presumably, not yet tested) On a non-decr_pc_after_break machine | |
230 | with hardware or kernel single-step: | |
231 | If we single-step over a breakpoint instruction, our PC will | |
232 | point at the following instruction. If we continue and hit a | |
233 | breakpoint instruction, our PC will point at the breakpoint | |
234 | instruction. */ | |
235 | ||
236 | static CORE_ADDR | |
237 | get_stop_pc (void) | |
238 | { | |
239 | CORE_ADDR stop_pc = (*the_low_target.get_pc) (); | |
240 | ||
241 | if (get_thread_process (current_inferior)->stepping) | |
242 | return stop_pc; | |
243 | else | |
244 | return stop_pc - the_low_target.decr_pc_after_break; | |
245 | } | |
ce3a066d | 246 | |
0d62e5e8 | 247 | static void * |
a1928bad | 248 | add_process (unsigned long pid) |
611cb4a5 | 249 | { |
0d62e5e8 DJ |
250 | struct process_info *process; |
251 | ||
252 | process = (struct process_info *) malloc (sizeof (*process)); | |
253 | memset (process, 0, sizeof (*process)); | |
254 | ||
255 | process->head.id = pid; | |
0d62e5e8 DJ |
256 | process->lwpid = pid; |
257 | ||
258 | add_inferior_to_list (&all_processes, &process->head); | |
259 | ||
260 | return process; | |
261 | } | |
611cb4a5 | 262 | |
da6d8c04 DJ |
263 | /* Start an inferior process and returns its pid. |
264 | ALLARGS is a vector of program-name and args. */ | |
265 | ||
ce3a066d DJ |
266 | static int |
267 | linux_create_inferior (char *program, char **allargs) | |
da6d8c04 | 268 | { |
0d62e5e8 | 269 | void *new_process; |
da6d8c04 DJ |
270 | int pid; |
271 | ||
42c81e2a | 272 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) |
52fb6437 NS |
273 | pid = vfork (); |
274 | #else | |
da6d8c04 | 275 | pid = fork (); |
52fb6437 | 276 | #endif |
da6d8c04 DJ |
277 | if (pid < 0) |
278 | perror_with_name ("fork"); | |
279 | ||
280 | if (pid == 0) | |
281 | { | |
282 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
283 | ||
254787d4 | 284 | signal (__SIGRTMIN + 1, SIG_DFL); |
0d62e5e8 | 285 | |
a9fa9f7d DJ |
286 | setpgid (0, 0); |
287 | ||
2b876972 DJ |
288 | execv (program, allargs); |
289 | if (errno == ENOENT) | |
290 | execvp (program, allargs); | |
da6d8c04 DJ |
291 | |
292 | fprintf (stderr, "Cannot exec %s: %s.\n", program, | |
d07c63e7 | 293 | strerror (errno)); |
da6d8c04 DJ |
294 | fflush (stderr); |
295 | _exit (0177); | |
296 | } | |
297 | ||
0d62e5e8 | 298 | new_process = add_process (pid); |
a06660f7 | 299 | add_thread (pid, new_process, pid); |
24a09b5f | 300 | must_set_ptrace_flags = 1; |
d61ddec4 | 301 | new_inferior = 1; |
611cb4a5 | 302 | |
a9fa9f7d | 303 | return pid; |
da6d8c04 DJ |
304 | } |
305 | ||
306 | /* Attach to an inferior process. */ | |
307 | ||
0d62e5e8 | 308 | void |
24a09b5f | 309 | linux_attach_lwp (unsigned long pid) |
da6d8c04 | 310 | { |
0d62e5e8 | 311 | struct process_info *new_process; |
611cb4a5 | 312 | |
da6d8c04 DJ |
313 | if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0) |
314 | { | |
2d717e4f DJ |
315 | if (all_threads.head != NULL) |
316 | { | |
317 | /* If we fail to attach to an LWP, just warn. */ | |
318 | fprintf (stderr, "Cannot attach to process %ld: %s (%d)\n", pid, | |
319 | strerror (errno), errno); | |
320 | fflush (stderr); | |
321 | return; | |
322 | } | |
323 | else | |
324 | /* If we fail to attach to a process, report an error. */ | |
325 | error ("Cannot attach to process %ld: %s (%d)\n", pid, | |
43d5792c | 326 | strerror (errno), errno); |
da6d8c04 DJ |
327 | } |
328 | ||
24a09b5f DJ |
329 | ptrace (PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACECLONE); |
330 | ||
0d62e5e8 | 331 | new_process = (struct process_info *) add_process (pid); |
24a09b5f DJ |
332 | add_thread (pid, new_process, pid); |
333 | new_thread_notify (thread_id_to_gdb_id (new_process->lwpid)); | |
0d62e5e8 DJ |
334 | |
335 | /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH | |
336 | brings it to a halt. We should ignore that SIGSTOP and resume the process | |
337 | (unless this is the first process, in which case the flag will be cleared | |
338 | in linux_attach). | |
339 | ||
340 | On the other hand, if we are currently trying to stop all threads, we | |
341 | should treat the new thread as if we had sent it a SIGSTOP. This works | |
342 | because we are guaranteed that add_process added us to the end of the | |
343 | list, and so the new thread has not yet reached wait_for_sigstop (but | |
344 | will). */ | |
345 | if (! stopping_threads) | |
346 | new_process->stop_expected = 1; | |
347 | } | |
348 | ||
349 | int | |
a1928bad | 350 | linux_attach (unsigned long pid) |
0d62e5e8 DJ |
351 | { |
352 | struct process_info *process; | |
353 | ||
24a09b5f | 354 | linux_attach_lwp (pid); |
0d62e5e8 | 355 | |
ae13219e DJ |
356 | /* Don't ignore the initial SIGSTOP if we just attached to this process. |
357 | It will be collected by wait shortly. */ | |
0d62e5e8 DJ |
358 | process = (struct process_info *) find_inferior_id (&all_processes, pid); |
359 | process->stop_expected = 0; | |
360 | ||
d61ddec4 UW |
361 | new_inferior = 1; |
362 | ||
da6d8c04 DJ |
363 | return 0; |
364 | } | |
365 | ||
366 | /* Kill the inferior process. Make us have no inferior. */ | |
367 | ||
ce3a066d | 368 | static void |
0d62e5e8 | 369 | linux_kill_one_process (struct inferior_list_entry *entry) |
da6d8c04 | 370 | { |
0d62e5e8 DJ |
371 | struct thread_info *thread = (struct thread_info *) entry; |
372 | struct process_info *process = get_thread_process (thread); | |
373 | int wstat; | |
374 | ||
fd500816 DJ |
375 | /* We avoid killing the first thread here, because of a Linux kernel (at |
376 | least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before | |
377 | the children get a chance to be reaped, it will remain a zombie | |
378 | forever. */ | |
379 | if (entry == all_threads.head) | |
380 | return; | |
381 | ||
0d62e5e8 DJ |
382 | do |
383 | { | |
384 | ptrace (PTRACE_KILL, pid_of (process), 0, 0); | |
385 | ||
386 | /* Make sure it died. The loop is most likely unnecessary. */ | |
387 | wstat = linux_wait_for_event (thread); | |
388 | } while (WIFSTOPPED (wstat)); | |
da6d8c04 DJ |
389 | } |
390 | ||
0d62e5e8 DJ |
391 | static void |
392 | linux_kill (void) | |
393 | { | |
fd500816 | 394 | struct thread_info *thread = (struct thread_info *) all_threads.head; |
9d606399 | 395 | struct process_info *process; |
fd500816 DJ |
396 | int wstat; |
397 | ||
9d606399 DJ |
398 | if (thread == NULL) |
399 | return; | |
400 | ||
0d62e5e8 | 401 | for_each_inferior (&all_threads, linux_kill_one_process); |
fd500816 DJ |
402 | |
403 | /* See the comment in linux_kill_one_process. We did not kill the first | |
404 | thread in the list, so do so now. */ | |
9d606399 | 405 | process = get_thread_process (thread); |
fd500816 DJ |
406 | do |
407 | { | |
408 | ptrace (PTRACE_KILL, pid_of (process), 0, 0); | |
409 | ||
410 | /* Make sure it died. The loop is most likely unnecessary. */ | |
411 | wstat = linux_wait_for_event (thread); | |
412 | } while (WIFSTOPPED (wstat)); | |
2d717e4f DJ |
413 | |
414 | clear_inferiors (); | |
415 | free (all_processes.head); | |
416 | all_processes.head = all_processes.tail = NULL; | |
0d62e5e8 DJ |
417 | } |
418 | ||
6ad8ae5c DJ |
419 | static void |
420 | linux_detach_one_process (struct inferior_list_entry *entry) | |
421 | { | |
422 | struct thread_info *thread = (struct thread_info *) entry; | |
423 | struct process_info *process = get_thread_process (thread); | |
424 | ||
ae13219e DJ |
425 | /* Make sure the process isn't stopped at a breakpoint that's |
426 | no longer there. */ | |
427 | check_removed_breakpoint (process); | |
428 | ||
429 | /* If this process is stopped but is expecting a SIGSTOP, then make | |
430 | sure we take care of that now. This isn't absolutely guaranteed | |
431 | to collect the SIGSTOP, but is fairly likely to. */ | |
432 | if (process->stop_expected) | |
433 | { | |
434 | /* Clear stop_expected, so that the SIGSTOP will be reported. */ | |
435 | process->stop_expected = 0; | |
436 | if (process->stopped) | |
437 | linux_resume_one_process (&process->head, 0, 0, NULL); | |
438 | linux_wait_for_event (thread); | |
439 | } | |
440 | ||
441 | /* Flush any pending changes to the process's registers. */ | |
442 | regcache_invalidate_one ((struct inferior_list_entry *) | |
443 | get_process_thread (process)); | |
444 | ||
445 | /* Finally, let it resume. */ | |
6ad8ae5c DJ |
446 | ptrace (PTRACE_DETACH, pid_of (process), 0, 0); |
447 | } | |
448 | ||
dd6953e1 | 449 | static int |
6ad8ae5c DJ |
450 | linux_detach (void) |
451 | { | |
ae13219e | 452 | delete_all_breakpoints (); |
6ad8ae5c | 453 | for_each_inferior (&all_threads, linux_detach_one_process); |
ae13219e | 454 | clear_inferiors (); |
2d717e4f DJ |
455 | free (all_processes.head); |
456 | all_processes.head = all_processes.tail = NULL; | |
dd6953e1 | 457 | return 0; |
6ad8ae5c DJ |
458 | } |
459 | ||
444d6139 PA |
460 | static void |
461 | linux_join (void) | |
462 | { | |
463 | extern unsigned long signal_pid; | |
464 | int status, ret; | |
465 | ||
466 | do { | |
467 | ret = waitpid (signal_pid, &status, 0); | |
468 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
469 | break; | |
470 | } while (ret != -1 || errno != ECHILD); | |
471 | } | |
472 | ||
6ad8ae5c | 473 | /* Return nonzero if the given thread is still alive. */ |
0d62e5e8 | 474 | static int |
24a09b5f | 475 | linux_thread_alive (unsigned long lwpid) |
0d62e5e8 | 476 | { |
24a09b5f | 477 | if (find_inferior_id (&all_threads, lwpid) != NULL) |
0d62e5e8 DJ |
478 | return 1; |
479 | else | |
480 | return 0; | |
481 | } | |
482 | ||
483 | /* Return nonzero if this process stopped at a breakpoint which | |
484 | no longer appears to be inserted. Also adjust the PC | |
485 | appropriately to resume where the breakpoint used to be. */ | |
ce3a066d | 486 | static int |
0d62e5e8 | 487 | check_removed_breakpoint (struct process_info *event_child) |
da6d8c04 | 488 | { |
0d62e5e8 DJ |
489 | CORE_ADDR stop_pc; |
490 | struct thread_info *saved_inferior; | |
491 | ||
492 | if (event_child->pending_is_breakpoint == 0) | |
493 | return 0; | |
494 | ||
495 | if (debug_threads) | |
ae13219e DJ |
496 | fprintf (stderr, "Checking for breakpoint in process %ld.\n", |
497 | event_child->lwpid); | |
0d62e5e8 DJ |
498 | |
499 | saved_inferior = current_inferior; | |
500 | current_inferior = get_process_thread (event_child); | |
501 | ||
502 | stop_pc = get_stop_pc (); | |
503 | ||
504 | /* If the PC has changed since we stopped, then we shouldn't do | |
505 | anything. This happens if, for instance, GDB handled the | |
506 | decr_pc_after_break subtraction itself. */ | |
507 | if (stop_pc != event_child->pending_stop_pc) | |
508 | { | |
509 | if (debug_threads) | |
ae13219e DJ |
510 | fprintf (stderr, "Ignoring, PC was changed. Old PC was 0x%08llx\n", |
511 | event_child->pending_stop_pc); | |
0d62e5e8 DJ |
512 | |
513 | event_child->pending_is_breakpoint = 0; | |
514 | current_inferior = saved_inferior; | |
515 | return 0; | |
516 | } | |
517 | ||
518 | /* If the breakpoint is still there, we will report hitting it. */ | |
519 | if ((*the_low_target.breakpoint_at) (stop_pc)) | |
520 | { | |
521 | if (debug_threads) | |
522 | fprintf (stderr, "Ignoring, breakpoint is still present.\n"); | |
523 | current_inferior = saved_inferior; | |
524 | return 0; | |
525 | } | |
526 | ||
527 | if (debug_threads) | |
528 | fprintf (stderr, "Removed breakpoint.\n"); | |
529 | ||
530 | /* For decr_pc_after_break targets, here is where we perform the | |
531 | decrement. We go immediately from this function to resuming, | |
532 | and can not safely call get_stop_pc () again. */ | |
533 | if (the_low_target.set_pc != NULL) | |
534 | (*the_low_target.set_pc) (stop_pc); | |
535 | ||
536 | /* We consumed the pending SIGTRAP. */ | |
5544ad89 | 537 | event_child->pending_is_breakpoint = 0; |
0d62e5e8 DJ |
538 | event_child->status_pending_p = 0; |
539 | event_child->status_pending = 0; | |
540 | ||
541 | current_inferior = saved_inferior; | |
da6d8c04 DJ |
542 | return 1; |
543 | } | |
544 | ||
0d62e5e8 DJ |
545 | /* Return 1 if this process has an interesting status pending. This function |
546 | may silently resume an inferior process. */ | |
611cb4a5 | 547 | static int |
0d62e5e8 DJ |
548 | status_pending_p (struct inferior_list_entry *entry, void *dummy) |
549 | { | |
550 | struct process_info *process = (struct process_info *) entry; | |
551 | ||
552 | if (process->status_pending_p) | |
553 | if (check_removed_breakpoint (process)) | |
554 | { | |
555 | /* This thread was stopped at a breakpoint, and the breakpoint | |
556 | is now gone. We were told to continue (or step...) all threads, | |
557 | so GDB isn't trying to single-step past this breakpoint. | |
558 | So instead of reporting the old SIGTRAP, pretend we got to | |
559 | the breakpoint just after it was removed instead of just | |
560 | before; resume the process. */ | |
32ca6d61 | 561 | linux_resume_one_process (&process->head, 0, 0, NULL); |
0d62e5e8 DJ |
562 | return 0; |
563 | } | |
564 | ||
565 | return process->status_pending_p; | |
566 | } | |
567 | ||
568 | static void | |
569 | linux_wait_for_process (struct process_info **childp, int *wstatp) | |
611cb4a5 | 570 | { |
0d62e5e8 DJ |
571 | int ret; |
572 | int to_wait_for = -1; | |
573 | ||
574 | if (*childp != NULL) | |
575 | to_wait_for = (*childp)->lwpid; | |
611cb4a5 | 576 | |
24a09b5f | 577 | retry: |
611cb4a5 DJ |
578 | while (1) |
579 | { | |
0d62e5e8 DJ |
580 | ret = waitpid (to_wait_for, wstatp, WNOHANG); |
581 | ||
582 | if (ret == -1) | |
583 | { | |
584 | if (errno != ECHILD) | |
585 | perror_with_name ("waitpid"); | |
586 | } | |
587 | else if (ret > 0) | |
588 | break; | |
589 | ||
590 | ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE); | |
591 | ||
592 | if (ret == -1) | |
593 | { | |
594 | if (errno != ECHILD) | |
595 | perror_with_name ("waitpid (WCLONE)"); | |
596 | } | |
597 | else if (ret > 0) | |
598 | break; | |
599 | ||
600 | usleep (1000); | |
601 | } | |
602 | ||
603 | if (debug_threads | |
604 | && (!WIFSTOPPED (*wstatp) | |
605 | || (WSTOPSIG (*wstatp) != 32 | |
606 | && WSTOPSIG (*wstatp) != 33))) | |
607 | fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp); | |
608 | ||
609 | if (to_wait_for == -1) | |
610 | *childp = (struct process_info *) find_inferior_id (&all_processes, ret); | |
611 | ||
24a09b5f DJ |
612 | /* If we didn't find a process, one of two things presumably happened: |
613 | - A process we started and then detached from has exited. Ignore it. | |
614 | - A process we are controlling has forked and the new child's stop | |
615 | was reported to us by the kernel. Save its PID. */ | |
616 | if (*childp == NULL && WIFSTOPPED (*wstatp)) | |
617 | { | |
618 | add_pid_to_list (&stopped_pids, ret); | |
619 | goto retry; | |
620 | } | |
621 | else if (*childp == NULL) | |
622 | goto retry; | |
623 | ||
0d62e5e8 DJ |
624 | (*childp)->stopped = 1; |
625 | (*childp)->pending_is_breakpoint = 0; | |
626 | ||
32ca6d61 DJ |
627 | (*childp)->last_status = *wstatp; |
628 | ||
d61ddec4 UW |
629 | /* Architecture-specific setup after inferior is running. |
630 | This needs to happen after we have attached to the inferior | |
631 | and it is stopped for the first time, but before we access | |
632 | any inferior registers. */ | |
633 | if (new_inferior) | |
634 | { | |
635 | the_low_target.arch_setup (); | |
52fa2412 UW |
636 | #ifdef HAVE_LINUX_REGSETS |
637 | memset (disabled_regsets, 0, num_regsets); | |
638 | #endif | |
d61ddec4 UW |
639 | new_inferior = 0; |
640 | } | |
641 | ||
0d62e5e8 DJ |
642 | if (debug_threads |
643 | && WIFSTOPPED (*wstatp)) | |
644 | { | |
645 | current_inferior = (struct thread_info *) | |
24a09b5f | 646 | find_inferior_id (&all_threads, (*childp)->lwpid); |
0d62e5e8 DJ |
647 | /* For testing only; i386_stop_pc prints out a diagnostic. */ |
648 | if (the_low_target.get_pc != NULL) | |
649 | get_stop_pc (); | |
650 | } | |
651 | } | |
611cb4a5 | 652 | |
0d62e5e8 DJ |
653 | static int |
654 | linux_wait_for_event (struct thread_info *child) | |
655 | { | |
656 | CORE_ADDR stop_pc; | |
657 | struct process_info *event_child; | |
658 | int wstat; | |
b65d95c5 | 659 | int bp_status; |
0d62e5e8 DJ |
660 | |
661 | /* Check for a process with a pending status. */ | |
662 | /* It is possible that the user changed the pending task's registers since | |
663 | it stopped. We correctly handle the change of PC if we hit a breakpoint | |
e5379b03 | 664 | (in check_removed_breakpoint); signals should be reported anyway. */ |
0d62e5e8 DJ |
665 | if (child == NULL) |
666 | { | |
667 | event_child = (struct process_info *) | |
668 | find_inferior (&all_processes, status_pending_p, NULL); | |
669 | if (debug_threads && event_child) | |
a1928bad | 670 | fprintf (stderr, "Got a pending child %ld\n", event_child->lwpid); |
0d62e5e8 DJ |
671 | } |
672 | else | |
673 | { | |
674 | event_child = get_thread_process (child); | |
675 | if (event_child->status_pending_p | |
676 | && check_removed_breakpoint (event_child)) | |
677 | event_child = NULL; | |
678 | } | |
611cb4a5 | 679 | |
0d62e5e8 DJ |
680 | if (event_child != NULL) |
681 | { | |
682 | if (event_child->status_pending_p) | |
611cb4a5 | 683 | { |
0d62e5e8 | 684 | if (debug_threads) |
a1928bad | 685 | fprintf (stderr, "Got an event from pending child %ld (%04x)\n", |
0d62e5e8 DJ |
686 | event_child->lwpid, event_child->status_pending); |
687 | wstat = event_child->status_pending; | |
688 | event_child->status_pending_p = 0; | |
689 | event_child->status_pending = 0; | |
690 | current_inferior = get_process_thread (event_child); | |
691 | return wstat; | |
692 | } | |
693 | } | |
694 | ||
695 | /* We only enter this loop if no process has a pending wait status. Thus | |
696 | any action taken in response to a wait status inside this loop is | |
697 | responding as soon as we detect the status, not after any pending | |
698 | events. */ | |
699 | while (1) | |
700 | { | |
701 | if (child == NULL) | |
702 | event_child = NULL; | |
703 | else | |
704 | event_child = get_thread_process (child); | |
705 | ||
706 | linux_wait_for_process (&event_child, &wstat); | |
707 | ||
708 | if (event_child == NULL) | |
709 | error ("event from unknown child"); | |
611cb4a5 | 710 | |
0d62e5e8 | 711 | current_inferior = (struct thread_info *) |
24a09b5f | 712 | find_inferior_id (&all_threads, event_child->lwpid); |
0d62e5e8 | 713 | |
89be2091 | 714 | /* Check for thread exit. */ |
24a09b5f | 715 | if (! WIFSTOPPED (wstat)) |
0d62e5e8 | 716 | { |
89be2091 | 717 | if (debug_threads) |
24a09b5f | 718 | fprintf (stderr, "LWP %ld exiting\n", event_child->head.id); |
89be2091 DJ |
719 | |
720 | /* If the last thread is exiting, just return. */ | |
721 | if (all_threads.head == all_threads.tail) | |
722 | return wstat; | |
723 | ||
24a09b5f | 724 | dead_thread_notify (thread_id_to_gdb_id (event_child->lwpid)); |
89be2091 DJ |
725 | |
726 | remove_inferior (&all_processes, &event_child->head); | |
727 | free (event_child); | |
728 | remove_thread (current_inferior); | |
729 | current_inferior = (struct thread_info *) all_threads.head; | |
730 | ||
731 | /* If we were waiting for this particular child to do something... | |
732 | well, it did something. */ | |
733 | if (child != NULL) | |
734 | return wstat; | |
735 | ||
736 | /* Wait for a more interesting event. */ | |
737 | continue; | |
738 | } | |
739 | ||
24a09b5f | 740 | if (WIFSTOPPED (wstat) |
89be2091 DJ |
741 | && WSTOPSIG (wstat) == SIGSTOP |
742 | && event_child->stop_expected) | |
743 | { | |
744 | if (debug_threads) | |
745 | fprintf (stderr, "Expected stop.\n"); | |
746 | event_child->stop_expected = 0; | |
747 | linux_resume_one_process (&event_child->head, | |
748 | event_child->stepping, 0, NULL); | |
749 | continue; | |
750 | } | |
751 | ||
24a09b5f DJ |
752 | if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGTRAP |
753 | && wstat >> 16 != 0) | |
754 | { | |
755 | handle_extended_wait (event_child, wstat); | |
756 | continue; | |
757 | } | |
758 | ||
89be2091 DJ |
759 | /* If GDB is not interested in this signal, don't stop other |
760 | threads, and don't report it to GDB. Just resume the | |
761 | inferior right away. We do this for threading-related | |
69f223ed DJ |
762 | signals as well as any that GDB specifically requested we |
763 | ignore. But never ignore SIGSTOP if we sent it ourselves, | |
764 | and do not ignore signals when stepping - they may require | |
765 | special handling to skip the signal handler. */ | |
89be2091 DJ |
766 | /* FIXME drow/2002-06-09: Get signal numbers from the inferior's |
767 | thread library? */ | |
768 | if (WIFSTOPPED (wstat) | |
69f223ed | 769 | && !event_child->stepping |
24a09b5f DJ |
770 | && ( |
771 | #ifdef USE_THREAD_DB | |
772 | (thread_db_active && (WSTOPSIG (wstat) == __SIGRTMIN | |
773 | || WSTOPSIG (wstat) == __SIGRTMIN + 1)) | |
774 | || | |
775 | #endif | |
776 | (pass_signals[target_signal_from_host (WSTOPSIG (wstat))] | |
777 | && (WSTOPSIG (wstat) != SIGSTOP || !stopping_threads)))) | |
89be2091 DJ |
778 | { |
779 | siginfo_t info, *info_p; | |
780 | ||
781 | if (debug_threads) | |
24a09b5f DJ |
782 | fprintf (stderr, "Ignored signal %d for LWP %ld.\n", |
783 | WSTOPSIG (wstat), event_child->head.id); | |
89be2091 DJ |
784 | |
785 | if (ptrace (PTRACE_GETSIGINFO, event_child->lwpid, 0, &info) == 0) | |
786 | info_p = &info; | |
787 | else | |
788 | info_p = NULL; | |
789 | linux_resume_one_process (&event_child->head, | |
790 | event_child->stepping, | |
791 | WSTOPSIG (wstat), info_p); | |
792 | continue; | |
0d62e5e8 | 793 | } |
611cb4a5 | 794 | |
0d62e5e8 DJ |
795 | /* If this event was not handled above, and is not a SIGTRAP, report |
796 | it. */ | |
797 | if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP) | |
798 | return wstat; | |
611cb4a5 | 799 | |
0d62e5e8 DJ |
800 | /* If this target does not support breakpoints, we simply report the |
801 | SIGTRAP; it's of no concern to us. */ | |
802 | if (the_low_target.get_pc == NULL) | |
803 | return wstat; | |
804 | ||
805 | stop_pc = get_stop_pc (); | |
806 | ||
807 | /* bp_reinsert will only be set if we were single-stepping. | |
808 | Notice that we will resume the process after hitting | |
809 | a gdbserver breakpoint; single-stepping to/over one | |
810 | is not supported (yet). */ | |
811 | if (event_child->bp_reinsert != 0) | |
812 | { | |
813 | if (debug_threads) | |
814 | fprintf (stderr, "Reinserted breakpoint.\n"); | |
815 | reinsert_breakpoint (event_child->bp_reinsert); | |
816 | event_child->bp_reinsert = 0; | |
817 | ||
818 | /* Clear the single-stepping flag and SIGTRAP as we resume. */ | |
32ca6d61 | 819 | linux_resume_one_process (&event_child->head, 0, 0, NULL); |
0d62e5e8 DJ |
820 | continue; |
821 | } | |
822 | ||
b65d95c5 | 823 | bp_status = check_breakpoints (stop_pc); |
0d62e5e8 | 824 | |
b65d95c5 | 825 | if (bp_status != 0) |
0d62e5e8 | 826 | { |
b65d95c5 DJ |
827 | if (debug_threads) |
828 | fprintf (stderr, "Hit a gdbserver breakpoint.\n"); | |
829 | ||
0d62e5e8 | 830 | /* We hit one of our own breakpoints. We mark it as a pending |
e5379b03 | 831 | breakpoint, so that check_removed_breakpoint () will do the PC |
0d62e5e8 DJ |
832 | adjustment for us at the appropriate time. */ |
833 | event_child->pending_is_breakpoint = 1; | |
834 | event_child->pending_stop_pc = stop_pc; | |
835 | ||
b65d95c5 | 836 | /* We may need to put the breakpoint back. We continue in the event |
0d62e5e8 DJ |
837 | loop instead of simply replacing the breakpoint right away, |
838 | in order to not lose signals sent to the thread that hit the | |
839 | breakpoint. Unfortunately this increases the window where another | |
840 | thread could sneak past the removed breakpoint. For the current | |
841 | use of server-side breakpoints (thread creation) this is | |
842 | acceptable; but it needs to be considered before this breakpoint | |
843 | mechanism can be used in more general ways. For some breakpoints | |
844 | it may be necessary to stop all other threads, but that should | |
845 | be avoided where possible. | |
846 | ||
847 | If breakpoint_reinsert_addr is NULL, that means that we can | |
848 | use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint, | |
849 | mark it for reinsertion, and single-step. | |
850 | ||
851 | Otherwise, call the target function to figure out where we need | |
852 | our temporary breakpoint, create it, and continue executing this | |
853 | process. */ | |
b65d95c5 DJ |
854 | if (bp_status == 2) |
855 | /* No need to reinsert. */ | |
856 | linux_resume_one_process (&event_child->head, 0, 0, NULL); | |
857 | else if (the_low_target.breakpoint_reinsert_addr == NULL) | |
0d62e5e8 DJ |
858 | { |
859 | event_child->bp_reinsert = stop_pc; | |
860 | uninsert_breakpoint (stop_pc); | |
32ca6d61 | 861 | linux_resume_one_process (&event_child->head, 1, 0, NULL); |
0d62e5e8 DJ |
862 | } |
863 | else | |
864 | { | |
865 | reinsert_breakpoint_by_bp | |
866 | (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ()); | |
32ca6d61 | 867 | linux_resume_one_process (&event_child->head, 0, 0, NULL); |
611cb4a5 | 868 | } |
0d62e5e8 DJ |
869 | |
870 | continue; | |
871 | } | |
872 | ||
b65d95c5 DJ |
873 | if (debug_threads) |
874 | fprintf (stderr, "Hit a non-gdbserver breakpoint.\n"); | |
875 | ||
0d62e5e8 DJ |
876 | /* If we were single-stepping, we definitely want to report the |
877 | SIGTRAP. The single-step operation has completed, so also | |
aa691b87 | 878 | clear the stepping flag; in general this does not matter, |
0d62e5e8 DJ |
879 | because the SIGTRAP will be reported to the client, which |
880 | will give us a new action for this thread, but clear it for | |
881 | consistency anyway. It's safe to clear the stepping flag | |
882 | because the only consumer of get_stop_pc () after this point | |
e5379b03 | 883 | is check_removed_breakpoint, and pending_is_breakpoint is not |
0d62e5e8 DJ |
884 | set. It might be wiser to use a step_completed flag instead. */ |
885 | if (event_child->stepping) | |
886 | { | |
887 | event_child->stepping = 0; | |
888 | return wstat; | |
889 | } | |
890 | ||
891 | /* A SIGTRAP that we can't explain. It may have been a breakpoint. | |
892 | Check if it is a breakpoint, and if so mark the process information | |
893 | accordingly. This will handle both the necessary fiddling with the | |
894 | PC on decr_pc_after_break targets and suppressing extra threads | |
895 | hitting a breakpoint if two hit it at once and then GDB removes it | |
896 | after the first is reported. Arguably it would be better to report | |
897 | multiple threads hitting breakpoints simultaneously, but the current | |
898 | remote protocol does not allow this. */ | |
899 | if ((*the_low_target.breakpoint_at) (stop_pc)) | |
900 | { | |
901 | event_child->pending_is_breakpoint = 1; | |
902 | event_child->pending_stop_pc = stop_pc; | |
611cb4a5 DJ |
903 | } |
904 | ||
905 | return wstat; | |
906 | } | |
0d62e5e8 | 907 | |
611cb4a5 DJ |
908 | /* NOTREACHED */ |
909 | return 0; | |
910 | } | |
911 | ||
0d62e5e8 | 912 | /* Wait for process, returns status. */ |
da6d8c04 | 913 | |
ce3a066d DJ |
914 | static unsigned char |
915 | linux_wait (char *status) | |
da6d8c04 | 916 | { |
e5f1222d | 917 | int w; |
0d62e5e8 DJ |
918 | struct thread_info *child = NULL; |
919 | ||
920 | retry: | |
921 | /* If we were only supposed to resume one thread, only wait for | |
922 | that thread - if it's still alive. If it died, however - which | |
923 | can happen if we're coming from the thread death case below - | |
924 | then we need to make sure we restart the other threads. We could | |
925 | pick a thread at random or restart all; restarting all is less | |
926 | arbitrary. */ | |
d592fa2f | 927 | if (cont_thread != 0 && cont_thread != -1) |
0d62e5e8 DJ |
928 | { |
929 | child = (struct thread_info *) find_inferior_id (&all_threads, | |
930 | cont_thread); | |
931 | ||
932 | /* No stepping, no signal - unless one is pending already, of course. */ | |
933 | if (child == NULL) | |
64386c31 DJ |
934 | { |
935 | struct thread_resume resume_info; | |
936 | resume_info.thread = -1; | |
937 | resume_info.step = resume_info.sig = resume_info.leave_stopped = 0; | |
938 | linux_resume (&resume_info); | |
939 | } | |
0d62e5e8 | 940 | } |
da6d8c04 | 941 | |
0d62e5e8 DJ |
942 | w = linux_wait_for_event (child); |
943 | stop_all_processes (); | |
da6d8c04 | 944 | |
24a09b5f DJ |
945 | if (must_set_ptrace_flags) |
946 | { | |
947 | ptrace (PTRACE_SETOPTIONS, inferior_pid, 0, PTRACE_O_TRACECLONE); | |
948 | must_set_ptrace_flags = 0; | |
949 | } | |
950 | ||
0d62e5e8 DJ |
951 | /* If we are waiting for a particular child, and it exited, |
952 | linux_wait_for_event will return its exit status. Similarly if | |
953 | the last child exited. If this is not the last child, however, | |
954 | do not report it as exited until there is a 'thread exited' response | |
955 | available in the remote protocol. Instead, just wait for another event. | |
956 | This should be safe, because if the thread crashed we will already | |
957 | have reported the termination signal to GDB; that should stop any | |
958 | in-progress stepping operations, etc. | |
959 | ||
960 | Report the exit status of the last thread to exit. This matches | |
961 | LinuxThreads' behavior. */ | |
962 | ||
963 | if (all_threads.head == all_threads.tail) | |
da6d8c04 | 964 | { |
0d62e5e8 DJ |
965 | if (WIFEXITED (w)) |
966 | { | |
967 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
968 | *status = 'W'; | |
969 | clear_inferiors (); | |
075b3282 DJ |
970 | free (all_processes.head); |
971 | all_processes.head = all_processes.tail = NULL; | |
b80864fb | 972 | return WEXITSTATUS (w); |
0d62e5e8 DJ |
973 | } |
974 | else if (!WIFSTOPPED (w)) | |
975 | { | |
976 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
0d62e5e8 | 977 | *status = 'X'; |
075b3282 DJ |
978 | clear_inferiors (); |
979 | free (all_processes.head); | |
980 | all_processes.head = all_processes.tail = NULL; | |
b80864fb | 981 | return target_signal_from_host (WTERMSIG (w)); |
0d62e5e8 | 982 | } |
da6d8c04 | 983 | } |
0d62e5e8 | 984 | else |
da6d8c04 | 985 | { |
0d62e5e8 DJ |
986 | if (!WIFSTOPPED (w)) |
987 | goto retry; | |
da6d8c04 DJ |
988 | } |
989 | ||
da6d8c04 | 990 | *status = 'T'; |
b80864fb | 991 | return target_signal_from_host (WSTOPSIG (w)); |
da6d8c04 DJ |
992 | } |
993 | ||
fd500816 DJ |
994 | /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if |
995 | thread groups are in use, we need to use tkill. */ | |
996 | ||
997 | static int | |
a1928bad | 998 | kill_lwp (unsigned long lwpid, int signo) |
fd500816 DJ |
999 | { |
1000 | static int tkill_failed; | |
1001 | ||
1002 | errno = 0; | |
1003 | ||
1004 | #ifdef SYS_tkill | |
1005 | if (!tkill_failed) | |
1006 | { | |
1007 | int ret = syscall (SYS_tkill, lwpid, signo); | |
1008 | if (errno != ENOSYS) | |
1009 | return ret; | |
1010 | errno = 0; | |
1011 | tkill_failed = 1; | |
1012 | } | |
1013 | #endif | |
1014 | ||
1015 | return kill (lwpid, signo); | |
1016 | } | |
1017 | ||
0d62e5e8 DJ |
1018 | static void |
1019 | send_sigstop (struct inferior_list_entry *entry) | |
1020 | { | |
1021 | struct process_info *process = (struct process_info *) entry; | |
1022 | ||
1023 | if (process->stopped) | |
1024 | return; | |
1025 | ||
1026 | /* If we already have a pending stop signal for this process, don't | |
1027 | send another. */ | |
1028 | if (process->stop_expected) | |
1029 | { | |
ae13219e DJ |
1030 | if (debug_threads) |
1031 | fprintf (stderr, "Have pending sigstop for process %ld\n", | |
1032 | process->lwpid); | |
1033 | ||
1034 | /* We clear the stop_expected flag so that wait_for_sigstop | |
1035 | will receive the SIGSTOP event (instead of silently resuming and | |
1036 | waiting again). It'll be reset below. */ | |
0d62e5e8 DJ |
1037 | process->stop_expected = 0; |
1038 | return; | |
1039 | } | |
1040 | ||
1041 | if (debug_threads) | |
a1928bad | 1042 | fprintf (stderr, "Sending sigstop to process %ld\n", process->head.id); |
0d62e5e8 | 1043 | |
fd500816 | 1044 | kill_lwp (process->head.id, SIGSTOP); |
0d62e5e8 DJ |
1045 | } |
1046 | ||
1047 | static void | |
1048 | wait_for_sigstop (struct inferior_list_entry *entry) | |
1049 | { | |
1050 | struct process_info *process = (struct process_info *) entry; | |
1051 | struct thread_info *saved_inferior, *thread; | |
a1928bad DJ |
1052 | int wstat; |
1053 | unsigned long saved_tid; | |
0d62e5e8 DJ |
1054 | |
1055 | if (process->stopped) | |
1056 | return; | |
1057 | ||
1058 | saved_inferior = current_inferior; | |
1059 | saved_tid = ((struct inferior_list_entry *) saved_inferior)->id; | |
1060 | thread = (struct thread_info *) find_inferior_id (&all_threads, | |
24a09b5f | 1061 | process->lwpid); |
0d62e5e8 DJ |
1062 | wstat = linux_wait_for_event (thread); |
1063 | ||
1064 | /* If we stopped with a non-SIGSTOP signal, save it for later | |
1065 | and record the pending SIGSTOP. If the process exited, just | |
1066 | return. */ | |
1067 | if (WIFSTOPPED (wstat) | |
1068 | && WSTOPSIG (wstat) != SIGSTOP) | |
1069 | { | |
1070 | if (debug_threads) | |
24a09b5f DJ |
1071 | fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n", |
1072 | process->lwpid, wstat); | |
0d62e5e8 DJ |
1073 | process->status_pending_p = 1; |
1074 | process->status_pending = wstat; | |
1075 | process->stop_expected = 1; | |
1076 | } | |
1077 | ||
1078 | if (linux_thread_alive (saved_tid)) | |
1079 | current_inferior = saved_inferior; | |
1080 | else | |
1081 | { | |
1082 | if (debug_threads) | |
1083 | fprintf (stderr, "Previously current thread died.\n"); | |
1084 | ||
1085 | /* Set a valid thread as current. */ | |
1086 | set_desired_inferior (0); | |
1087 | } | |
1088 | } | |
1089 | ||
1090 | static void | |
1091 | stop_all_processes (void) | |
1092 | { | |
1093 | stopping_threads = 1; | |
1094 | for_each_inferior (&all_processes, send_sigstop); | |
1095 | for_each_inferior (&all_processes, wait_for_sigstop); | |
1096 | stopping_threads = 0; | |
1097 | } | |
1098 | ||
da6d8c04 DJ |
1099 | /* Resume execution of the inferior process. |
1100 | If STEP is nonzero, single-step it. | |
1101 | If SIGNAL is nonzero, give it that signal. */ | |
1102 | ||
ce3a066d | 1103 | static void |
0d62e5e8 | 1104 | linux_resume_one_process (struct inferior_list_entry *entry, |
32ca6d61 | 1105 | int step, int signal, siginfo_t *info) |
da6d8c04 | 1106 | { |
0d62e5e8 DJ |
1107 | struct process_info *process = (struct process_info *) entry; |
1108 | struct thread_info *saved_inferior; | |
1109 | ||
1110 | if (process->stopped == 0) | |
1111 | return; | |
1112 | ||
1113 | /* If we have pending signals or status, and a new signal, enqueue the | |
1114 | signal. Also enqueue the signal if we are waiting to reinsert a | |
1115 | breakpoint; it will be picked up again below. */ | |
1116 | if (signal != 0 | |
1117 | && (process->status_pending_p || process->pending_signals != NULL | |
1118 | || process->bp_reinsert != 0)) | |
1119 | { | |
1120 | struct pending_signals *p_sig; | |
1121 | p_sig = malloc (sizeof (*p_sig)); | |
1122 | p_sig->prev = process->pending_signals; | |
1123 | p_sig->signal = signal; | |
32ca6d61 DJ |
1124 | if (info == NULL) |
1125 | memset (&p_sig->info, 0, sizeof (siginfo_t)); | |
1126 | else | |
1127 | memcpy (&p_sig->info, info, sizeof (siginfo_t)); | |
0d62e5e8 DJ |
1128 | process->pending_signals = p_sig; |
1129 | } | |
1130 | ||
e5379b03 | 1131 | if (process->status_pending_p && !check_removed_breakpoint (process)) |
0d62e5e8 DJ |
1132 | return; |
1133 | ||
1134 | saved_inferior = current_inferior; | |
1135 | current_inferior = get_process_thread (process); | |
1136 | ||
1137 | if (debug_threads) | |
a1928bad | 1138 | fprintf (stderr, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid, |
0d62e5e8 DJ |
1139 | step ? "step" : "continue", signal, |
1140 | process->stop_expected ? "expected" : "not expected"); | |
1141 | ||
1142 | /* This bit needs some thinking about. If we get a signal that | |
1143 | we must report while a single-step reinsert is still pending, | |
1144 | we often end up resuming the thread. It might be better to | |
1145 | (ew) allow a stack of pending events; then we could be sure that | |
1146 | the reinsert happened right away and not lose any signals. | |
1147 | ||
1148 | Making this stack would also shrink the window in which breakpoints are | |
1149 | uninserted (see comment in linux_wait_for_process) but not enough for | |
1150 | complete correctness, so it won't solve that problem. It may be | |
1151 | worthwhile just to solve this one, however. */ | |
1152 | if (process->bp_reinsert != 0) | |
1153 | { | |
1154 | if (debug_threads) | |
1155 | fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert); | |
1156 | if (step == 0) | |
1157 | fprintf (stderr, "BAD - reinserting but not stepping.\n"); | |
1158 | step = 1; | |
1159 | ||
1160 | /* Postpone any pending signal. It was enqueued above. */ | |
1161 | signal = 0; | |
1162 | } | |
1163 | ||
1164 | check_removed_breakpoint (process); | |
1165 | ||
aa691b87 | 1166 | if (debug_threads && the_low_target.get_pc != NULL) |
0d62e5e8 DJ |
1167 | { |
1168 | fprintf (stderr, " "); | |
52fb6437 | 1169 | (*the_low_target.get_pc) (); |
0d62e5e8 DJ |
1170 | } |
1171 | ||
1172 | /* If we have pending signals, consume one unless we are trying to reinsert | |
1173 | a breakpoint. */ | |
1174 | if (process->pending_signals != NULL && process->bp_reinsert == 0) | |
1175 | { | |
1176 | struct pending_signals **p_sig; | |
1177 | ||
1178 | p_sig = &process->pending_signals; | |
1179 | while ((*p_sig)->prev != NULL) | |
1180 | p_sig = &(*p_sig)->prev; | |
1181 | ||
1182 | signal = (*p_sig)->signal; | |
32ca6d61 DJ |
1183 | if ((*p_sig)->info.si_signo != 0) |
1184 | ptrace (PTRACE_SETSIGINFO, process->lwpid, 0, &(*p_sig)->info); | |
1185 | ||
0d62e5e8 DJ |
1186 | free (*p_sig); |
1187 | *p_sig = NULL; | |
1188 | } | |
1189 | ||
1190 | regcache_invalidate_one ((struct inferior_list_entry *) | |
1191 | get_process_thread (process)); | |
da6d8c04 | 1192 | errno = 0; |
0d62e5e8 DJ |
1193 | process->stopped = 0; |
1194 | process->stepping = step; | |
1195 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal); | |
1196 | ||
1197 | current_inferior = saved_inferior; | |
da6d8c04 | 1198 | if (errno) |
3221518c UW |
1199 | { |
1200 | /* ESRCH from ptrace either means that the thread was already | |
1201 | running (an error) or that it is gone (a race condition). If | |
1202 | it's gone, we will get a notification the next time we wait, | |
1203 | so we can ignore the error. We could differentiate these | |
1204 | two, but it's tricky without waiting; the thread still exists | |
1205 | as a zombie, so sending it signal 0 would succeed. So just | |
1206 | ignore ESRCH. */ | |
1207 | if (errno == ESRCH) | |
1208 | return; | |
1209 | ||
1210 | perror_with_name ("ptrace"); | |
1211 | } | |
da6d8c04 DJ |
1212 | } |
1213 | ||
64386c31 DJ |
1214 | static struct thread_resume *resume_ptr; |
1215 | ||
1216 | /* This function is called once per thread. We look up the thread | |
5544ad89 DJ |
1217 | in RESUME_PTR, and mark the thread with a pointer to the appropriate |
1218 | resume request. | |
1219 | ||
1220 | This algorithm is O(threads * resume elements), but resume elements | |
1221 | is small (and will remain small at least until GDB supports thread | |
1222 | suspension). */ | |
0d62e5e8 | 1223 | static void |
5544ad89 | 1224 | linux_set_resume_request (struct inferior_list_entry *entry) |
0d62e5e8 DJ |
1225 | { |
1226 | struct process_info *process; | |
64386c31 | 1227 | struct thread_info *thread; |
5544ad89 | 1228 | int ndx; |
64386c31 DJ |
1229 | |
1230 | thread = (struct thread_info *) entry; | |
1231 | process = get_thread_process (thread); | |
1232 | ||
1233 | ndx = 0; | |
1234 | while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id) | |
1235 | ndx++; | |
1236 | ||
5544ad89 DJ |
1237 | process->resume = &resume_ptr[ndx]; |
1238 | } | |
1239 | ||
1240 | /* This function is called once per thread. We check the thread's resume | |
1241 | request, which will tell us whether to resume, step, or leave the thread | |
1242 | stopped; and what signal, if any, it should be sent. For threads which | |
1243 | we aren't explicitly told otherwise, we preserve the stepping flag; this | |
1244 | is used for stepping over gdbserver-placed breakpoints. */ | |
1245 | ||
1246 | static void | |
1247 | linux_continue_one_thread (struct inferior_list_entry *entry) | |
1248 | { | |
1249 | struct process_info *process; | |
1250 | struct thread_info *thread; | |
1251 | int step; | |
1252 | ||
1253 | thread = (struct thread_info *) entry; | |
1254 | process = get_thread_process (thread); | |
1255 | ||
1256 | if (process->resume->leave_stopped) | |
64386c31 DJ |
1257 | return; |
1258 | ||
5544ad89 DJ |
1259 | if (process->resume->thread == -1) |
1260 | step = process->stepping || process->resume->step; | |
64386c31 | 1261 | else |
5544ad89 DJ |
1262 | step = process->resume->step; |
1263 | ||
32ca6d61 | 1264 | linux_resume_one_process (&process->head, step, process->resume->sig, NULL); |
c6ecbae5 | 1265 | |
5544ad89 DJ |
1266 | process->resume = NULL; |
1267 | } | |
1268 | ||
1269 | /* This function is called once per thread. We check the thread's resume | |
1270 | request, which will tell us whether to resume, step, or leave the thread | |
1271 | stopped; and what signal, if any, it should be sent. We queue any needed | |
1272 | signals, since we won't actually resume. We already have a pending event | |
1273 | to report, so we don't need to preserve any step requests; they should | |
1274 | be re-issued if necessary. */ | |
1275 | ||
1276 | static void | |
1277 | linux_queue_one_thread (struct inferior_list_entry *entry) | |
1278 | { | |
1279 | struct process_info *process; | |
1280 | struct thread_info *thread; | |
1281 | ||
1282 | thread = (struct thread_info *) entry; | |
1283 | process = get_thread_process (thread); | |
1284 | ||
1285 | if (process->resume->leave_stopped) | |
1286 | return; | |
1287 | ||
1288 | /* If we have a new signal, enqueue the signal. */ | |
1289 | if (process->resume->sig != 0) | |
1290 | { | |
1291 | struct pending_signals *p_sig; | |
1292 | p_sig = malloc (sizeof (*p_sig)); | |
1293 | p_sig->prev = process->pending_signals; | |
1294 | p_sig->signal = process->resume->sig; | |
32ca6d61 DJ |
1295 | memset (&p_sig->info, 0, sizeof (siginfo_t)); |
1296 | ||
1297 | /* If this is the same signal we were previously stopped by, | |
1298 | make sure to queue its siginfo. We can ignore the return | |
1299 | value of ptrace; if it fails, we'll skip | |
1300 | PTRACE_SETSIGINFO. */ | |
1301 | if (WIFSTOPPED (process->last_status) | |
1302 | && WSTOPSIG (process->last_status) == process->resume->sig) | |
1303 | ptrace (PTRACE_GETSIGINFO, process->lwpid, 0, &p_sig->info); | |
1304 | ||
5544ad89 DJ |
1305 | process->pending_signals = p_sig; |
1306 | } | |
1307 | ||
1308 | process->resume = NULL; | |
1309 | } | |
1310 | ||
1311 | /* Set DUMMY if this process has an interesting status pending. */ | |
1312 | static int | |
1313 | resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p) | |
1314 | { | |
1315 | struct process_info *process = (struct process_info *) entry; | |
1316 | ||
1317 | /* Processes which will not be resumed are not interesting, because | |
1318 | we might not wait for them next time through linux_wait. */ | |
1319 | if (process->resume->leave_stopped) | |
1320 | return 0; | |
1321 | ||
1322 | /* If this thread has a removed breakpoint, we won't have any | |
1323 | events to report later, so check now. check_removed_breakpoint | |
1324 | may clear status_pending_p. We avoid calling check_removed_breakpoint | |
1325 | for any thread that we are not otherwise going to resume - this | |
1326 | lets us preserve stopped status when two threads hit a breakpoint. | |
1327 | GDB removes the breakpoint to single-step a particular thread | |
1328 | past it, then re-inserts it and resumes all threads. We want | |
1329 | to report the second thread without resuming it in the interim. */ | |
1330 | if (process->status_pending_p) | |
1331 | check_removed_breakpoint (process); | |
1332 | ||
1333 | if (process->status_pending_p) | |
1334 | * (int *) flag_p = 1; | |
1335 | ||
1336 | return 0; | |
0d62e5e8 DJ |
1337 | } |
1338 | ||
1339 | static void | |
64386c31 | 1340 | linux_resume (struct thread_resume *resume_info) |
0d62e5e8 | 1341 | { |
5544ad89 | 1342 | int pending_flag; |
c6ecbae5 | 1343 | |
5544ad89 | 1344 | /* Yes, the use of a global here is rather ugly. */ |
64386c31 | 1345 | resume_ptr = resume_info; |
5544ad89 DJ |
1346 | |
1347 | for_each_inferior (&all_threads, linux_set_resume_request); | |
1348 | ||
1349 | /* If there is a thread which would otherwise be resumed, which | |
1350 | has a pending status, then don't resume any threads - we can just | |
1351 | report the pending status. Make sure to queue any signals | |
1352 | that would otherwise be sent. */ | |
1353 | pending_flag = 0; | |
1354 | find_inferior (&all_processes, resume_status_pending_p, &pending_flag); | |
1355 | ||
1356 | if (debug_threads) | |
1357 | { | |
1358 | if (pending_flag) | |
1359 | fprintf (stderr, "Not resuming, pending status\n"); | |
1360 | else | |
1361 | fprintf (stderr, "Resuming, no pending status\n"); | |
1362 | } | |
1363 | ||
1364 | if (pending_flag) | |
1365 | for_each_inferior (&all_threads, linux_queue_one_thread); | |
1366 | else | |
a20d5e98 | 1367 | for_each_inferior (&all_threads, linux_continue_one_thread); |
0d62e5e8 DJ |
1368 | } |
1369 | ||
1370 | #ifdef HAVE_LINUX_USRREGS | |
da6d8c04 DJ |
1371 | |
1372 | int | |
0a30fbc4 | 1373 | register_addr (int regnum) |
da6d8c04 DJ |
1374 | { |
1375 | int addr; | |
1376 | ||
2ec06d2e | 1377 | if (regnum < 0 || regnum >= the_low_target.num_regs) |
da6d8c04 DJ |
1378 | error ("Invalid register number %d.", regnum); |
1379 | ||
2ec06d2e | 1380 | addr = the_low_target.regmap[regnum]; |
da6d8c04 DJ |
1381 | |
1382 | return addr; | |
1383 | } | |
1384 | ||
58caa3dc | 1385 | /* Fetch one register. */ |
da6d8c04 DJ |
1386 | static void |
1387 | fetch_register (int regno) | |
1388 | { | |
1389 | CORE_ADDR regaddr; | |
48d93c75 | 1390 | int i, size; |
0d62e5e8 | 1391 | char *buf; |
da6d8c04 | 1392 | |
2ec06d2e | 1393 | if (regno >= the_low_target.num_regs) |
0a30fbc4 | 1394 | return; |
2ec06d2e | 1395 | if ((*the_low_target.cannot_fetch_register) (regno)) |
0a30fbc4 | 1396 | return; |
da6d8c04 | 1397 | |
0a30fbc4 DJ |
1398 | regaddr = register_addr (regno); |
1399 | if (regaddr == -1) | |
1400 | return; | |
48d93c75 UW |
1401 | size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1) |
1402 | & - sizeof (PTRACE_XFER_TYPE); | |
1403 | buf = alloca (size); | |
1404 | for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE)) | |
da6d8c04 DJ |
1405 | { |
1406 | errno = 0; | |
0d62e5e8 | 1407 | *(PTRACE_XFER_TYPE *) (buf + i) = |
da6d8c04 DJ |
1408 | ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0); |
1409 | regaddr += sizeof (PTRACE_XFER_TYPE); | |
1410 | if (errno != 0) | |
1411 | { | |
1412 | /* Warning, not error, in case we are attached; sometimes the | |
1413 | kernel doesn't let us at the registers. */ | |
1414 | char *err = strerror (errno); | |
1415 | char *msg = alloca (strlen (err) + 128); | |
1416 | sprintf (msg, "reading register %d: %s", regno, err); | |
1417 | error (msg); | |
1418 | goto error_exit; | |
1419 | } | |
1420 | } | |
ee1a7ae4 UW |
1421 | |
1422 | if (the_low_target.supply_ptrace_register) | |
1423 | the_low_target.supply_ptrace_register (regno, buf); | |
5a1f5858 DJ |
1424 | else |
1425 | supply_register (regno, buf); | |
0d62e5e8 | 1426 | |
da6d8c04 DJ |
1427 | error_exit:; |
1428 | } | |
1429 | ||
1430 | /* Fetch all registers, or just one, from the child process. */ | |
58caa3dc DJ |
1431 | static void |
1432 | usr_fetch_inferior_registers (int regno) | |
da6d8c04 DJ |
1433 | { |
1434 | if (regno == -1 || regno == 0) | |
2ec06d2e | 1435 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
da6d8c04 DJ |
1436 | fetch_register (regno); |
1437 | else | |
1438 | fetch_register (regno); | |
1439 | } | |
1440 | ||
1441 | /* Store our register values back into the inferior. | |
1442 | If REGNO is -1, do this for all registers. | |
1443 | Otherwise, REGNO specifies which register (so we can save time). */ | |
58caa3dc DJ |
1444 | static void |
1445 | usr_store_inferior_registers (int regno) | |
da6d8c04 DJ |
1446 | { |
1447 | CORE_ADDR regaddr; | |
48d93c75 | 1448 | int i, size; |
0d62e5e8 | 1449 | char *buf; |
da6d8c04 DJ |
1450 | |
1451 | if (regno >= 0) | |
1452 | { | |
2ec06d2e | 1453 | if (regno >= the_low_target.num_regs) |
0a30fbc4 DJ |
1454 | return; |
1455 | ||
bc1e36ca | 1456 | if ((*the_low_target.cannot_store_register) (regno) == 1) |
0a30fbc4 DJ |
1457 | return; |
1458 | ||
1459 | regaddr = register_addr (regno); | |
1460 | if (regaddr == -1) | |
da6d8c04 | 1461 | return; |
da6d8c04 | 1462 | errno = 0; |
48d93c75 UW |
1463 | size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1) |
1464 | & - sizeof (PTRACE_XFER_TYPE); | |
1465 | buf = alloca (size); | |
1466 | memset (buf, 0, size); | |
ee1a7ae4 UW |
1467 | |
1468 | if (the_low_target.collect_ptrace_register) | |
1469 | the_low_target.collect_ptrace_register (regno, buf); | |
5a1f5858 DJ |
1470 | else |
1471 | collect_register (regno, buf); | |
ee1a7ae4 | 1472 | |
48d93c75 | 1473 | for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE)) |
da6d8c04 | 1474 | { |
0a30fbc4 DJ |
1475 | errno = 0; |
1476 | ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
2ff29de4 | 1477 | *(PTRACE_XFER_TYPE *) (buf + i)); |
da6d8c04 DJ |
1478 | if (errno != 0) |
1479 | { | |
3221518c UW |
1480 | /* At this point, ESRCH should mean the process is already gone, |
1481 | in which case we simply ignore attempts to change its registers. | |
1482 | See also the related comment in linux_resume_one_process. */ | |
1483 | if (errno == ESRCH) | |
1484 | return; | |
1485 | ||
bc1e36ca DJ |
1486 | if ((*the_low_target.cannot_store_register) (regno) == 0) |
1487 | { | |
1488 | char *err = strerror (errno); | |
1489 | char *msg = alloca (strlen (err) + 128); | |
1490 | sprintf (msg, "writing register %d: %s", | |
1491 | regno, err); | |
1492 | error (msg); | |
1493 | return; | |
1494 | } | |
da6d8c04 | 1495 | } |
2ff29de4 | 1496 | regaddr += sizeof (PTRACE_XFER_TYPE); |
da6d8c04 | 1497 | } |
da6d8c04 DJ |
1498 | } |
1499 | else | |
2ec06d2e | 1500 | for (regno = 0; regno < the_low_target.num_regs; regno++) |
0d62e5e8 | 1501 | usr_store_inferior_registers (regno); |
da6d8c04 | 1502 | } |
58caa3dc DJ |
1503 | #endif /* HAVE_LINUX_USRREGS */ |
1504 | ||
1505 | ||
1506 | ||
1507 | #ifdef HAVE_LINUX_REGSETS | |
1508 | ||
1509 | static int | |
0d62e5e8 | 1510 | regsets_fetch_inferior_registers () |
58caa3dc DJ |
1511 | { |
1512 | struct regset_info *regset; | |
e9d25b98 | 1513 | int saw_general_regs = 0; |
58caa3dc DJ |
1514 | |
1515 | regset = target_regsets; | |
1516 | ||
1517 | while (regset->size >= 0) | |
1518 | { | |
1519 | void *buf; | |
1520 | int res; | |
1521 | ||
52fa2412 | 1522 | if (regset->size == 0 || disabled_regsets[regset - target_regsets]) |
58caa3dc DJ |
1523 | { |
1524 | regset ++; | |
1525 | continue; | |
1526 | } | |
1527 | ||
1528 | buf = malloc (regset->size); | |
dfb64f85 | 1529 | #ifndef __sparc__ |
d06f167a | 1530 | res = ptrace (regset->get_request, inferior_pid, 0, buf); |
dfb64f85 DJ |
1531 | #else |
1532 | res = ptrace (regset->get_request, inferior_pid, buf, 0); | |
1533 | #endif | |
58caa3dc DJ |
1534 | if (res < 0) |
1535 | { | |
1536 | if (errno == EIO) | |
1537 | { | |
52fa2412 UW |
1538 | /* If we get EIO on a regset, do not try it again for |
1539 | this process. */ | |
1540 | disabled_regsets[regset - target_regsets] = 1; | |
1541 | continue; | |
58caa3dc DJ |
1542 | } |
1543 | else | |
1544 | { | |
0d62e5e8 | 1545 | char s[256]; |
a1928bad | 1546 | sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%ld", |
0d62e5e8 DJ |
1547 | inferior_pid); |
1548 | perror (s); | |
58caa3dc DJ |
1549 | } |
1550 | } | |
e9d25b98 DJ |
1551 | else if (regset->type == GENERAL_REGS) |
1552 | saw_general_regs = 1; | |
58caa3dc DJ |
1553 | regset->store_function (buf); |
1554 | regset ++; | |
1555 | } | |
e9d25b98 DJ |
1556 | if (saw_general_regs) |
1557 | return 0; | |
1558 | else | |
1559 | return 1; | |
58caa3dc DJ |
1560 | } |
1561 | ||
1562 | static int | |
0d62e5e8 | 1563 | regsets_store_inferior_registers () |
58caa3dc DJ |
1564 | { |
1565 | struct regset_info *regset; | |
e9d25b98 | 1566 | int saw_general_regs = 0; |
58caa3dc DJ |
1567 | |
1568 | regset = target_regsets; | |
1569 | ||
1570 | while (regset->size >= 0) | |
1571 | { | |
1572 | void *buf; | |
1573 | int res; | |
1574 | ||
52fa2412 | 1575 | if (regset->size == 0 || disabled_regsets[regset - target_regsets]) |
58caa3dc DJ |
1576 | { |
1577 | regset ++; | |
1578 | continue; | |
1579 | } | |
1580 | ||
1581 | buf = malloc (regset->size); | |
545587ee DJ |
1582 | |
1583 | /* First fill the buffer with the current register set contents, | |
1584 | in case there are any items in the kernel's regset that are | |
1585 | not in gdbserver's regcache. */ | |
dfb64f85 | 1586 | #ifndef __sparc__ |
545587ee | 1587 | res = ptrace (regset->get_request, inferior_pid, 0, buf); |
dfb64f85 DJ |
1588 | #else |
1589 | res = ptrace (regset->get_request, inferior_pid, buf, 0); | |
1590 | #endif | |
545587ee DJ |
1591 | |
1592 | if (res == 0) | |
1593 | { | |
1594 | /* Then overlay our cached registers on that. */ | |
1595 | regset->fill_function (buf); | |
1596 | ||
1597 | /* Only now do we write the register set. */ | |
dfb64f85 DJ |
1598 | #ifndef __sparc__ |
1599 | res = ptrace (regset->set_request, inferior_pid, 0, buf); | |
1600 | #else | |
1601 | res = ptrace (regset->set_request, inferior_pid, buf, 0); | |
1602 | #endif | |
545587ee DJ |
1603 | } |
1604 | ||
58caa3dc DJ |
1605 | if (res < 0) |
1606 | { | |
1607 | if (errno == EIO) | |
1608 | { | |
52fa2412 UW |
1609 | /* If we get EIO on a regset, do not try it again for |
1610 | this process. */ | |
1611 | disabled_regsets[regset - target_regsets] = 1; | |
1612 | continue; | |
58caa3dc | 1613 | } |
3221518c UW |
1614 | else if (errno == ESRCH) |
1615 | { | |
1616 | /* At this point, ESRCH should mean the process is already gone, | |
1617 | in which case we simply ignore attempts to change its registers. | |
1618 | See also the related comment in linux_resume_one_process. */ | |
1619 | return 0; | |
1620 | } | |
58caa3dc DJ |
1621 | else |
1622 | { | |
ce3a066d | 1623 | perror ("Warning: ptrace(regsets_store_inferior_registers)"); |
58caa3dc DJ |
1624 | } |
1625 | } | |
e9d25b98 DJ |
1626 | else if (regset->type == GENERAL_REGS) |
1627 | saw_general_regs = 1; | |
58caa3dc | 1628 | regset ++; |
09ec9b38 | 1629 | free (buf); |
58caa3dc | 1630 | } |
e9d25b98 DJ |
1631 | if (saw_general_regs) |
1632 | return 0; | |
1633 | else | |
1634 | return 1; | |
ce3a066d | 1635 | return 0; |
58caa3dc DJ |
1636 | } |
1637 | ||
1638 | #endif /* HAVE_LINUX_REGSETS */ | |
1639 | ||
1640 | ||
1641 | void | |
ce3a066d | 1642 | linux_fetch_registers (int regno) |
58caa3dc DJ |
1643 | { |
1644 | #ifdef HAVE_LINUX_REGSETS | |
52fa2412 UW |
1645 | if (regsets_fetch_inferior_registers () == 0) |
1646 | return; | |
58caa3dc DJ |
1647 | #endif |
1648 | #ifdef HAVE_LINUX_USRREGS | |
1649 | usr_fetch_inferior_registers (regno); | |
1650 | #endif | |
1651 | } | |
1652 | ||
1653 | void | |
ce3a066d | 1654 | linux_store_registers (int regno) |
58caa3dc DJ |
1655 | { |
1656 | #ifdef HAVE_LINUX_REGSETS | |
52fa2412 UW |
1657 | if (regsets_store_inferior_registers () == 0) |
1658 | return; | |
58caa3dc DJ |
1659 | #endif |
1660 | #ifdef HAVE_LINUX_USRREGS | |
1661 | usr_store_inferior_registers (regno); | |
1662 | #endif | |
1663 | } | |
1664 | ||
da6d8c04 | 1665 | |
da6d8c04 DJ |
1666 | /* Copy LEN bytes from inferior's memory starting at MEMADDR |
1667 | to debugger memory starting at MYADDR. */ | |
1668 | ||
c3e735a6 | 1669 | static int |
f450004a | 1670 | linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
da6d8c04 DJ |
1671 | { |
1672 | register int i; | |
1673 | /* Round starting address down to longword boundary. */ | |
1674 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
1675 | /* Round ending address up; get number of longwords that makes. */ | |
aa691b87 RM |
1676 | register int count |
1677 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) | |
da6d8c04 DJ |
1678 | / sizeof (PTRACE_XFER_TYPE); |
1679 | /* Allocate buffer of that many longwords. */ | |
aa691b87 | 1680 | register PTRACE_XFER_TYPE *buffer |
da6d8c04 | 1681 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); |
fd462a61 DJ |
1682 | int fd; |
1683 | char filename[64]; | |
1684 | ||
1685 | /* Try using /proc. Don't bother for one word. */ | |
1686 | if (len >= 3 * sizeof (long)) | |
1687 | { | |
1688 | /* We could keep this file open and cache it - possibly one per | |
1689 | thread. That requires some juggling, but is even faster. */ | |
1690 | sprintf (filename, "/proc/%ld/mem", inferior_pid); | |
1691 | fd = open (filename, O_RDONLY | O_LARGEFILE); | |
1692 | if (fd == -1) | |
1693 | goto no_proc; | |
1694 | ||
1695 | /* If pread64 is available, use it. It's faster if the kernel | |
1696 | supports it (only one syscall), and it's 64-bit safe even on | |
1697 | 32-bit platforms (for instance, SPARC debugging a SPARC64 | |
1698 | application). */ | |
1699 | #ifdef HAVE_PREAD64 | |
1700 | if (pread64 (fd, myaddr, len, memaddr) != len) | |
1701 | #else | |
1702 | if (lseek (fd, memaddr, SEEK_SET) == -1 || read (fd, memaddr, len) != len) | |
1703 | #endif | |
1704 | { | |
1705 | close (fd); | |
1706 | goto no_proc; | |
1707 | } | |
1708 | ||
1709 | close (fd); | |
1710 | return 0; | |
1711 | } | |
da6d8c04 | 1712 | |
fd462a61 | 1713 | no_proc: |
da6d8c04 DJ |
1714 | /* Read all the longwords */ |
1715 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
1716 | { | |
c3e735a6 | 1717 | errno = 0; |
d844cde6 | 1718 | buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0); |
c3e735a6 DJ |
1719 | if (errno) |
1720 | return errno; | |
da6d8c04 DJ |
1721 | } |
1722 | ||
1723 | /* Copy appropriate bytes out of the buffer. */ | |
1724 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len); | |
c3e735a6 DJ |
1725 | |
1726 | return 0; | |
da6d8c04 DJ |
1727 | } |
1728 | ||
1729 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
1730 | to inferior's memory at MEMADDR. | |
1731 | On failure (cannot write the inferior) | |
1732 | returns the value of errno. */ | |
1733 | ||
ce3a066d | 1734 | static int |
f450004a | 1735 | linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) |
da6d8c04 DJ |
1736 | { |
1737 | register int i; | |
1738 | /* Round starting address down to longword boundary. */ | |
1739 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
1740 | /* Round ending address up; get number of longwords that makes. */ | |
1741 | register int count | |
1742 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE); | |
1743 | /* Allocate buffer of that many longwords. */ | |
1744 | register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); | |
da6d8c04 | 1745 | |
0d62e5e8 DJ |
1746 | if (debug_threads) |
1747 | { | |
1748 | fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr); | |
1749 | } | |
1750 | ||
da6d8c04 DJ |
1751 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
1752 | ||
d844cde6 DJ |
1753 | buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, |
1754 | (PTRACE_ARG3_TYPE) addr, 0); | |
da6d8c04 DJ |
1755 | |
1756 | if (count > 1) | |
1757 | { | |
1758 | buffer[count - 1] | |
1759 | = ptrace (PTRACE_PEEKTEXT, inferior_pid, | |
d844cde6 DJ |
1760 | (PTRACE_ARG3_TYPE) (addr + (count - 1) |
1761 | * sizeof (PTRACE_XFER_TYPE)), | |
1762 | 0); | |
da6d8c04 DJ |
1763 | } |
1764 | ||
1765 | /* Copy data to be written over corresponding part of buffer */ | |
1766 | ||
1767 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len); | |
1768 | ||
1769 | /* Write the entire buffer. */ | |
1770 | ||
1771 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
1772 | { | |
1773 | errno = 0; | |
d844cde6 | 1774 | ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]); |
da6d8c04 DJ |
1775 | if (errno) |
1776 | return errno; | |
1777 | } | |
1778 | ||
1779 | return 0; | |
1780 | } | |
2f2893d9 | 1781 | |
24a09b5f DJ |
1782 | static int linux_supports_tracefork_flag; |
1783 | ||
51c2684e | 1784 | /* Helper functions for linux_test_for_tracefork, called via clone (). */ |
24a09b5f | 1785 | |
51c2684e DJ |
1786 | static int |
1787 | linux_tracefork_grandchild (void *arg) | |
1788 | { | |
1789 | _exit (0); | |
1790 | } | |
1791 | ||
7407e2de AS |
1792 | #define STACK_SIZE 4096 |
1793 | ||
51c2684e DJ |
1794 | static int |
1795 | linux_tracefork_child (void *arg) | |
24a09b5f DJ |
1796 | { |
1797 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
1798 | kill (getpid (), SIGSTOP); | |
7407e2de AS |
1799 | #ifdef __ia64__ |
1800 | __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE, | |
1801 | CLONE_VM | SIGCHLD, NULL); | |
1802 | #else | |
1803 | clone (linux_tracefork_grandchild, arg + STACK_SIZE, | |
1804 | CLONE_VM | SIGCHLD, NULL); | |
1805 | #endif | |
24a09b5f DJ |
1806 | _exit (0); |
1807 | } | |
1808 | ||
1809 | /* Wrapper function for waitpid which handles EINTR. */ | |
1810 | ||
1811 | static int | |
1812 | my_waitpid (int pid, int *status, int flags) | |
1813 | { | |
1814 | int ret; | |
1815 | do | |
1816 | { | |
1817 | ret = waitpid (pid, status, flags); | |
1818 | } | |
1819 | while (ret == -1 && errno == EINTR); | |
1820 | ||
1821 | return ret; | |
1822 | } | |
1823 | ||
1824 | /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make | |
1825 | sure that we can enable the option, and that it had the desired | |
1826 | effect. */ | |
1827 | ||
1828 | static void | |
1829 | linux_test_for_tracefork (void) | |
1830 | { | |
1831 | int child_pid, ret, status; | |
1832 | long second_pid; | |
7407e2de | 1833 | char *stack = malloc (STACK_SIZE * 4); |
24a09b5f DJ |
1834 | |
1835 | linux_supports_tracefork_flag = 0; | |
1836 | ||
51c2684e | 1837 | /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */ |
7407e2de AS |
1838 | #ifdef __ia64__ |
1839 | child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE, | |
1840 | CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2); | |
1841 | #else | |
1842 | child_pid = clone (linux_tracefork_child, stack + STACK_SIZE, | |
1843 | CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2); | |
1844 | #endif | |
24a09b5f | 1845 | if (child_pid == -1) |
51c2684e | 1846 | perror_with_name ("clone"); |
24a09b5f DJ |
1847 | |
1848 | ret = my_waitpid (child_pid, &status, 0); | |
1849 | if (ret == -1) | |
1850 | perror_with_name ("waitpid"); | |
1851 | else if (ret != child_pid) | |
1852 | error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret); | |
1853 | if (! WIFSTOPPED (status)) | |
1854 | error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status); | |
1855 | ||
1856 | ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK); | |
1857 | if (ret != 0) | |
1858 | { | |
1859 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); | |
1860 | if (ret != 0) | |
1861 | { | |
1862 | warning ("linux_test_for_tracefork: failed to kill child"); | |
1863 | return; | |
1864 | } | |
1865 | ||
1866 | ret = my_waitpid (child_pid, &status, 0); | |
1867 | if (ret != child_pid) | |
1868 | warning ("linux_test_for_tracefork: failed to wait for killed child"); | |
1869 | else if (!WIFSIGNALED (status)) | |
1870 | warning ("linux_test_for_tracefork: unexpected wait status 0x%x from " | |
1871 | "killed child", status); | |
1872 | ||
1873 | return; | |
1874 | } | |
1875 | ||
1876 | ret = ptrace (PTRACE_CONT, child_pid, 0, 0); | |
1877 | if (ret != 0) | |
1878 | warning ("linux_test_for_tracefork: failed to resume child"); | |
1879 | ||
1880 | ret = my_waitpid (child_pid, &status, 0); | |
1881 | ||
1882 | if (ret == child_pid && WIFSTOPPED (status) | |
1883 | && status >> 16 == PTRACE_EVENT_FORK) | |
1884 | { | |
1885 | second_pid = 0; | |
1886 | ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid); | |
1887 | if (ret == 0 && second_pid != 0) | |
1888 | { | |
1889 | int second_status; | |
1890 | ||
1891 | linux_supports_tracefork_flag = 1; | |
1892 | my_waitpid (second_pid, &second_status, 0); | |
1893 | ret = ptrace (PTRACE_KILL, second_pid, 0, 0); | |
1894 | if (ret != 0) | |
1895 | warning ("linux_test_for_tracefork: failed to kill second child"); | |
1896 | my_waitpid (second_pid, &status, 0); | |
1897 | } | |
1898 | } | |
1899 | else | |
1900 | warning ("linux_test_for_tracefork: unexpected result from waitpid " | |
1901 | "(%d, status 0x%x)", ret, status); | |
1902 | ||
1903 | do | |
1904 | { | |
1905 | ret = ptrace (PTRACE_KILL, child_pid, 0, 0); | |
1906 | if (ret != 0) | |
1907 | warning ("linux_test_for_tracefork: failed to kill child"); | |
1908 | my_waitpid (child_pid, &status, 0); | |
1909 | } | |
1910 | while (WIFSTOPPED (status)); | |
51c2684e DJ |
1911 | |
1912 | free (stack); | |
24a09b5f DJ |
1913 | } |
1914 | ||
1915 | ||
2f2893d9 DJ |
1916 | static void |
1917 | linux_look_up_symbols (void) | |
1918 | { | |
0d62e5e8 | 1919 | #ifdef USE_THREAD_DB |
24a09b5f | 1920 | if (thread_db_active) |
0d62e5e8 DJ |
1921 | return; |
1922 | ||
24a09b5f | 1923 | thread_db_active = thread_db_init (!linux_supports_tracefork_flag); |
0d62e5e8 DJ |
1924 | #endif |
1925 | } | |
1926 | ||
e5379b03 | 1927 | static void |
ef57601b | 1928 | linux_request_interrupt (void) |
e5379b03 | 1929 | { |
a1928bad | 1930 | extern unsigned long signal_pid; |
e5379b03 | 1931 | |
d592fa2f | 1932 | if (cont_thread != 0 && cont_thread != -1) |
e5379b03 DJ |
1933 | { |
1934 | struct process_info *process; | |
1935 | ||
1936 | process = get_thread_process (current_inferior); | |
ef57601b | 1937 | kill_lwp (process->lwpid, SIGINT); |
e5379b03 DJ |
1938 | } |
1939 | else | |
ef57601b | 1940 | kill_lwp (signal_pid, SIGINT); |
e5379b03 DJ |
1941 | } |
1942 | ||
aa691b87 RM |
1943 | /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET |
1944 | to debugger memory starting at MYADDR. */ | |
1945 | ||
1946 | static int | |
f450004a | 1947 | linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len) |
aa691b87 RM |
1948 | { |
1949 | char filename[PATH_MAX]; | |
1950 | int fd, n; | |
1951 | ||
a1928bad | 1952 | snprintf (filename, sizeof filename, "/proc/%ld/auxv", inferior_pid); |
aa691b87 RM |
1953 | |
1954 | fd = open (filename, O_RDONLY); | |
1955 | if (fd < 0) | |
1956 | return -1; | |
1957 | ||
1958 | if (offset != (CORE_ADDR) 0 | |
1959 | && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset) | |
1960 | n = -1; | |
1961 | else | |
1962 | n = read (fd, myaddr, len); | |
1963 | ||
1964 | close (fd); | |
1965 | ||
1966 | return n; | |
1967 | } | |
1968 | ||
e013ee27 OF |
1969 | /* These watchpoint related wrapper functions simply pass on the function call |
1970 | if the target has registered a corresponding function. */ | |
1971 | ||
1972 | static int | |
1973 | linux_insert_watchpoint (char type, CORE_ADDR addr, int len) | |
1974 | { | |
1975 | if (the_low_target.insert_watchpoint != NULL) | |
1976 | return the_low_target.insert_watchpoint (type, addr, len); | |
1977 | else | |
1978 | /* Unsupported (see target.h). */ | |
1979 | return 1; | |
1980 | } | |
1981 | ||
1982 | static int | |
1983 | linux_remove_watchpoint (char type, CORE_ADDR addr, int len) | |
1984 | { | |
1985 | if (the_low_target.remove_watchpoint != NULL) | |
1986 | return the_low_target.remove_watchpoint (type, addr, len); | |
1987 | else | |
1988 | /* Unsupported (see target.h). */ | |
1989 | return 1; | |
1990 | } | |
1991 | ||
1992 | static int | |
1993 | linux_stopped_by_watchpoint (void) | |
1994 | { | |
1995 | if (the_low_target.stopped_by_watchpoint != NULL) | |
1996 | return the_low_target.stopped_by_watchpoint (); | |
1997 | else | |
1998 | return 0; | |
1999 | } | |
2000 | ||
2001 | static CORE_ADDR | |
2002 | linux_stopped_data_address (void) | |
2003 | { | |
2004 | if (the_low_target.stopped_data_address != NULL) | |
2005 | return the_low_target.stopped_data_address (); | |
2006 | else | |
2007 | return 0; | |
2008 | } | |
2009 | ||
42c81e2a | 2010 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) |
52fb6437 NS |
2011 | #if defined(__mcoldfire__) |
2012 | /* These should really be defined in the kernel's ptrace.h header. */ | |
2013 | #define PT_TEXT_ADDR 49*4 | |
2014 | #define PT_DATA_ADDR 50*4 | |
2015 | #define PT_TEXT_END_ADDR 51*4 | |
2016 | #endif | |
2017 | ||
2018 | /* Under uClinux, programs are loaded at non-zero offsets, which we need | |
2019 | to tell gdb about. */ | |
2020 | ||
2021 | static int | |
2022 | linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p) | |
2023 | { | |
2024 | #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR) | |
2025 | unsigned long text, text_end, data; | |
2026 | int pid = get_thread_process (current_inferior)->head.id; | |
2027 | ||
2028 | errno = 0; | |
2029 | ||
2030 | text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0); | |
2031 | text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0); | |
2032 | data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0); | |
2033 | ||
2034 | if (errno == 0) | |
2035 | { | |
2036 | /* Both text and data offsets produced at compile-time (and so | |
2037 | used by gdb) are relative to the beginning of the program, | |
2038 | with the data segment immediately following the text segment. | |
2039 | However, the actual runtime layout in memory may put the data | |
2040 | somewhere else, so when we send gdb a data base-address, we | |
2041 | use the real data base address and subtract the compile-time | |
2042 | data base-address from it (which is just the length of the | |
2043 | text segment). BSS immediately follows data in both | |
2044 | cases. */ | |
2045 | *text_p = text; | |
2046 | *data_p = data - (text_end - text); | |
2047 | ||
2048 | return 1; | |
2049 | } | |
2050 | #endif | |
2051 | return 0; | |
2052 | } | |
2053 | #endif | |
2054 | ||
07e059b5 VP |
2055 | static int |
2056 | linux_qxfer_osdata (const char *annex, | |
2057 | unsigned char *readbuf, unsigned const char *writebuf, | |
2058 | CORE_ADDR offset, int len) | |
2059 | { | |
2060 | /* We make the process list snapshot when the object starts to be | |
2061 | read. */ | |
2062 | static const char *buf; | |
2063 | static long len_avail = -1; | |
2064 | static struct buffer buffer; | |
2065 | ||
2066 | DIR *dirp; | |
2067 | ||
2068 | if (strcmp (annex, "processes") != 0) | |
2069 | return 0; | |
2070 | ||
2071 | if (!readbuf || writebuf) | |
2072 | return 0; | |
2073 | ||
2074 | if (offset == 0) | |
2075 | { | |
2076 | if (len_avail != -1 && len_avail != 0) | |
2077 | buffer_free (&buffer); | |
2078 | len_avail = 0; | |
2079 | buf = NULL; | |
2080 | buffer_init (&buffer); | |
2081 | buffer_grow_str (&buffer, "<osdata type=\"processes\">"); | |
2082 | ||
2083 | dirp = opendir ("/proc"); | |
2084 | if (dirp) | |
2085 | { | |
2086 | struct dirent *dp; | |
2087 | while ((dp = readdir (dirp)) != NULL) | |
2088 | { | |
2089 | struct stat statbuf; | |
2090 | char procentry[sizeof ("/proc/4294967295")]; | |
2091 | ||
2092 | if (!isdigit (dp->d_name[0]) | |
2093 | || strlen (dp->d_name) > sizeof ("4294967295") - 1) | |
2094 | continue; | |
2095 | ||
2096 | sprintf (procentry, "/proc/%s", dp->d_name); | |
2097 | if (stat (procentry, &statbuf) == 0 | |
2098 | && S_ISDIR (statbuf.st_mode)) | |
2099 | { | |
2100 | char pathname[128]; | |
2101 | FILE *f; | |
2102 | char cmd[MAXPATHLEN + 1]; | |
2103 | struct passwd *entry; | |
2104 | ||
2105 | sprintf (pathname, "/proc/%s/cmdline", dp->d_name); | |
2106 | entry = getpwuid (statbuf.st_uid); | |
2107 | ||
2108 | if ((f = fopen (pathname, "r")) != NULL) | |
2109 | { | |
2110 | size_t len = fread (cmd, 1, sizeof (cmd) - 1, f); | |
2111 | if (len > 0) | |
2112 | { | |
2113 | int i; | |
2114 | for (i = 0; i < len; i++) | |
2115 | if (cmd[i] == '\0') | |
2116 | cmd[i] = ' '; | |
2117 | cmd[len] = '\0'; | |
2118 | ||
2119 | buffer_xml_printf ( | |
2120 | &buffer, | |
2121 | "<item>" | |
2122 | "<column name=\"pid\">%s</column>" | |
2123 | "<column name=\"user\">%s</column>" | |
2124 | "<column name=\"command\">%s</column>" | |
2125 | "</item>", | |
2126 | dp->d_name, | |
2127 | entry ? entry->pw_name : "?", | |
2128 | cmd); | |
2129 | } | |
2130 | fclose (f); | |
2131 | } | |
2132 | } | |
2133 | } | |
2134 | ||
2135 | closedir (dirp); | |
2136 | } | |
2137 | buffer_grow_str0 (&buffer, "</osdata>\n"); | |
2138 | buf = buffer_finish (&buffer); | |
2139 | len_avail = strlen (buf); | |
2140 | } | |
2141 | ||
2142 | if (offset >= len_avail) | |
2143 | { | |
2144 | /* Done. Get rid of the data. */ | |
2145 | buffer_free (&buffer); | |
2146 | buf = NULL; | |
2147 | len_avail = 0; | |
2148 | return 0; | |
2149 | } | |
2150 | ||
2151 | if (len > len_avail - offset) | |
2152 | len = len_avail - offset; | |
2153 | memcpy (readbuf, buf + offset, len); | |
2154 | ||
2155 | return len; | |
2156 | } | |
2157 | ||
ce3a066d DJ |
2158 | static struct target_ops linux_target_ops = { |
2159 | linux_create_inferior, | |
2160 | linux_attach, | |
2161 | linux_kill, | |
6ad8ae5c | 2162 | linux_detach, |
444d6139 | 2163 | linux_join, |
ce3a066d DJ |
2164 | linux_thread_alive, |
2165 | linux_resume, | |
2166 | linux_wait, | |
2167 | linux_fetch_registers, | |
2168 | linux_store_registers, | |
2169 | linux_read_memory, | |
2170 | linux_write_memory, | |
2f2893d9 | 2171 | linux_look_up_symbols, |
ef57601b | 2172 | linux_request_interrupt, |
aa691b87 | 2173 | linux_read_auxv, |
e013ee27 OF |
2174 | linux_insert_watchpoint, |
2175 | linux_remove_watchpoint, | |
2176 | linux_stopped_by_watchpoint, | |
2177 | linux_stopped_data_address, | |
42c81e2a | 2178 | #if defined(__UCLIBC__) && defined(HAS_NOMMU) |
52fb6437 | 2179 | linux_read_offsets, |
dae5f5cf DJ |
2180 | #else |
2181 | NULL, | |
2182 | #endif | |
2183 | #ifdef USE_THREAD_DB | |
2184 | thread_db_get_tls_address, | |
2185 | #else | |
2186 | NULL, | |
52fb6437 | 2187 | #endif |
59a016f0 PA |
2188 | NULL, |
2189 | hostio_last_error_from_errno, | |
07e059b5 | 2190 | linux_qxfer_osdata, |
ce3a066d DJ |
2191 | }; |
2192 | ||
0d62e5e8 DJ |
2193 | static void |
2194 | linux_init_signals () | |
2195 | { | |
2196 | /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads | |
2197 | to find what the cancel signal actually is. */ | |
254787d4 | 2198 | signal (__SIGRTMIN+1, SIG_IGN); |
0d62e5e8 DJ |
2199 | } |
2200 | ||
da6d8c04 DJ |
2201 | void |
2202 | initialize_low (void) | |
2203 | { | |
24a09b5f | 2204 | thread_db_active = 0; |
ce3a066d | 2205 | set_target_ops (&linux_target_ops); |
611cb4a5 DJ |
2206 | set_breakpoint_data (the_low_target.breakpoint, |
2207 | the_low_target.breakpoint_len); | |
0d62e5e8 | 2208 | linux_init_signals (); |
24a09b5f | 2209 | linux_test_for_tracefork (); |
52fa2412 UW |
2210 | #ifdef HAVE_LINUX_REGSETS |
2211 | for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++) | |
2212 | ; | |
2213 | disabled_regsets = malloc (num_regsets); | |
2214 | #endif | |
da6d8c04 | 2215 | } |