]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Low level Unix child interface to ttrace, for GDB when running under HP-UX. |
f83f82bc AC |
2 | |
3 | Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, | |
4 | 1998, 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
24 | #include "frame.h" | |
25 | #include "inferior.h" | |
26 | #include "target.h" | |
27 | #include "gdb_string.h" | |
03f2053f | 28 | #include "gdb_wait.h" |
c906108c | 29 | #include "command.h" |
f7dd6af2 | 30 | #include "gdbthread.h" |
6339dc9e | 31 | #include "infttrace.h" |
c906108c | 32 | |
6aaea291 AC |
33 | /* We need pstat functionality so that we can get the exec file |
34 | for a process we attach to. | |
35 | ||
36 | According to HP, we should use the 64bit interfaces, so we | |
37 | define _PSTAT64 to achieve this. */ | |
38 | #define _PSTAT64 | |
39 | #include <sys/pstat.h> | |
40 | ||
c906108c SS |
41 | /* Some hackery to work around a use of the #define name NO_FLAGS |
42 | * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h). | |
43 | */ | |
44 | #ifdef NO_FLAGS | |
45 | #define INFTTRACE_TEMP_HACK NO_FLAGS | |
46 | #undef NO_FLAGS | |
47 | #endif | |
48 | ||
49 | #ifdef USG | |
50 | #include <sys/types.h> | |
51 | #endif | |
52 | ||
53 | #include <sys/param.h> | |
54 | #include <sys/dir.h> | |
55 | #include <signal.h> | |
56 | #include <sys/ioctl.h> | |
57 | ||
58 | #include <sys/ttrace.h> | |
c906108c SS |
59 | #include <sys/mman.h> |
60 | ||
61 | #ifndef NO_PTRACE_H | |
62 | #ifdef PTRACE_IN_WRONG_PLACE | |
63 | #include <ptrace.h> | |
64 | #else | |
65 | #include <sys/ptrace.h> | |
66 | #endif | |
67 | #endif /* NO_PTRACE_H */ | |
68 | ||
69 | /* Second half of the hackery above. Non-ANSI C, so | |
70 | * we can't use "#error", alas. | |
71 | */ | |
72 | #ifdef NO_FLAGS | |
73 | #if (NO_FLAGS != INFTTRACE_TEMP_HACK ) | |
74 | /* #error "Hackery to remove warning didn't work right" */ | |
75 | #else | |
76 | /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */ | |
77 | #endif | |
78 | #else | |
79 | /* #error "Didn't get expected re-definition of NO_FLAGS" */ | |
80 | #define NO_FLAGS INFTTRACE_TEMP_HACK | |
81 | #endif | |
82 | ||
83 | #if !defined (PT_SETTRC) | |
84 | #define PT_SETTRC 0 /* Make process traceable by parent */ | |
85 | #endif | |
86 | #if !defined (PT_READ_I) | |
87 | #define PT_READ_I 1 /* Read word from text space */ | |
88 | #endif | |
89 | #if !defined (PT_READ_D) | |
90 | #define PT_READ_D 2 /* Read word from data space */ | |
91 | #endif | |
92 | #if !defined (PT_READ_U) | |
93 | #define PT_READ_U 3 /* Read word from kernel user struct */ | |
94 | #endif | |
95 | #if !defined (PT_WRITE_I) | |
96 | #define PT_WRITE_I 4 /* Write word to text space */ | |
97 | #endif | |
98 | #if !defined (PT_WRITE_D) | |
99 | #define PT_WRITE_D 5 /* Write word to data space */ | |
100 | #endif | |
101 | #if !defined (PT_WRITE_U) | |
102 | #define PT_WRITE_U 6 /* Write word to kernel user struct */ | |
103 | #endif | |
104 | #if !defined (PT_CONTINUE) | |
105 | #define PT_CONTINUE 7 /* Continue after signal */ | |
106 | #endif | |
107 | #if !defined (PT_STEP) | |
108 | #define PT_STEP 9 /* Set flag for single stepping */ | |
109 | #endif | |
110 | #if !defined (PT_KILL) | |
111 | #define PT_KILL 8 /* Send child a SIGKILL signal */ | |
112 | #endif | |
113 | ||
114 | #ifndef PT_ATTACH | |
115 | #define PT_ATTACH PTRACE_ATTACH | |
116 | #endif | |
117 | #ifndef PT_DETACH | |
118 | #define PT_DETACH PTRACE_DETACH | |
119 | #endif | |
120 | ||
121 | #include "gdbcore.h" | |
122 | #ifndef NO_SYS_FILE | |
123 | #include <sys/file.h> | |
124 | #endif | |
125 | ||
126 | /* This semaphore is used to coordinate the child and parent processes | |
127 | after a fork(), and before an exec() by the child. See parent_attach_all | |
128 | for details. | |
c5aa993b JM |
129 | */ |
130 | typedef struct | |
131 | { | |
132 | int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */ | |
133 | int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */ | |
134 | } | |
135 | startup_semaphore_t; | |
c906108c SS |
136 | |
137 | #define SEM_TALK (1) | |
138 | #define SEM_LISTEN (0) | |
139 | ||
c5aa993b | 140 | static startup_semaphore_t startup_semaphore; |
c906108c SS |
141 | |
142 | /* See can_touch_threads_of_process for details. */ | |
c5aa993b JM |
143 | static int vforking_child_pid = 0; |
144 | static int vfork_in_flight = 0; | |
c906108c | 145 | |
c906108c SS |
146 | /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise. |
147 | */ | |
148 | #define TT_OK( _status, _errno ) \ | |
149 | (((_status) == 1) && ((_errno) == 0)) | |
150 | ||
151 | #define TTRACE_ARG_TYPE uint64_t | |
152 | ||
153 | /* When supplied as the "addr" operand, ttrace interprets this | |
154 | to mean, "from the current address". | |
c5aa993b | 155 | */ |
c906108c SS |
156 | #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC) |
157 | ||
158 | /* When supplied as the "addr", "data" or "addr2" operand for most | |
159 | requests, ttrace interprets this to mean, "pay no heed to this | |
160 | argument". | |
c5aa993b | 161 | */ |
c906108c SS |
162 | #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG) |
163 | ||
164 | /* This is capable of holding the value of a 32-bit register. The | |
165 | value is always left-aligned in the buffer; i.e., [0] contains | |
166 | the most-significant byte of the register's value, and [sizeof(reg)] | |
167 | contains the least-significant value. | |
168 | ||
169 | ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and | |
170 | that registers are 32-bits on HP-UX. The latter assumption changes | |
171 | with PA2.0. | |
c5aa993b JM |
172 | */ |
173 | typedef int register_value_t; | |
c906108c SS |
174 | |
175 | /******************************************************************** | |
176 | ||
177 | How this works: | |
178 | ||
179 | 1. Thread numbers | |
180 | ||
181 | The rest of GDB sees threads as being things with different | |
182 | "pid" (process id) values. See "thread.c" for details. The | |
183 | separate threads will be seen and reacted to if infttrace passes | |
184 | back different pid values (for _events_). See wait_for_inferior | |
185 | in inftarg.c. | |
186 | ||
187 | So infttrace is going to use thread ids externally, pretending | |
188 | they are process ids, and keep track internally so that it can | |
189 | use the real process id (and thread id) when calling ttrace. | |
190 | ||
191 | The data structure that supports this is a linked list of the | |
192 | current threads. Since at some date infttrace will have to | |
193 | deal with multiple processes, each list element records its | |
194 | corresponding pid, rather than having a single global. | |
195 | ||
196 | Note that the list is only approximately current; that's ok, as | |
197 | it's up to date when we need it (we hope!). Also, it can contain | |
198 | dead threads, as there's no harm if it does. | |
199 | ||
200 | The approach taken here is to bury the translation from external | |
201 | to internal inside "call_ttrace" and a few other places. | |
202 | ||
203 | There are some wrinkles: | |
204 | ||
205 | o When GDB forks itself to create the debug target process, | |
206 | there's only a pid of 0 around in the child, so the | |
207 | TT_PROC_SETTRC operation uses a more direct call to ttrace; | |
208 | Similiarly, the initial setting of the event mask happens | |
209 | early as well, and so is also special-cased, and an attach | |
210 | uses a real pid; | |
211 | ||
212 | o We define an unthreaded application as having a "pseudo" | |
213 | thread; | |
214 | ||
215 | o To keep from confusing the rest of GDB, we don't switch | |
216 | the PID for the pseudo thread to a TID. A table will help: | |
217 | ||
218 | Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ... | |
219 | ||
220 | Our thread list stores: pid pid pid pid ... | |
221 | tid0 tid1 tid2 tid3 | |
222 | ||
223 | Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ... | |
224 | ||
225 | Both pid and tid0 will map to tid0, as there are infttrace.c-internal | |
226 | calls to ttrace using tid0. | |
227 | ||
228 | 2. Step and Continue | |
229 | ||
230 | Since we're implementing the "stop the world" model, sub-model | |
231 | "other threads run during step", we have some stuff to do: | |
232 | ||
233 | o User steps require continuing all threads other than the | |
234 | one the user is stepping; | |
235 | ||
236 | o Internal debugger steps (such as over a breakpoint or watchpoint, | |
237 | but not out of a library load thunk) require stepping only | |
238 | the selected thread; this means that we have to report the | |
239 | step finish on that thread, which can lead to complications; | |
240 | ||
241 | o When a thread is created, it is created running, rather | |
242 | than stopped--so we have to stop it. | |
243 | ||
244 | The OS doesn't guarantee the stopped thread list will be stable, | |
245 | no does it guarantee where on the stopped thread list a thread | |
246 | that is single-stepped will wind up: it's possible that it will | |
247 | be off the list for a while, it's possible the step will complete | |
248 | and it will be re-posted to the end... | |
249 | ||
250 | This means we have to scan the stopped thread list, build up | |
251 | a work-list, and then run down the work list; we can't do the | |
252 | step/continue during the scan. | |
253 | ||
254 | 3. Buffering events | |
255 | ||
256 | Then there's the issue of waiting for an event. We do this by | |
257 | noticing how many events are reported at the end of each wait. | |
258 | From then on, we "fake" all resumes and steps, returning instantly, | |
259 | and don't do another wait. Once all pending events are reported, | |
260 | we can really resume again. | |
261 | ||
262 | To keep this hidden, all the routines which know about tids and | |
263 | pids or real events and simulated ones are static (file-local). | |
264 | ||
265 | This code can make lots of calls to ttrace, in particular it | |
266 | can spin down the list of thread states more than once. If this | |
267 | becomes a performance hit, the spin could be done once and the | |
268 | various "tsp" blocks saved, keeping all later spins in this | |
269 | process. | |
270 | ||
271 | The O/S doesn't promise to keep the list straight, and so we must | |
272 | re-scan a lot. By observation, it looks like a single-step/wait | |
273 | puts the stepped thread at the end of the list but doesn't change | |
274 | it otherwise. | |
275 | ||
276 | **************************************************************** | |
277 | */ | |
278 | ||
279 | /* Uncomment these to turn on various debugging output */ | |
280 | /* #define THREAD_DEBUG */ | |
281 | /* #define WAIT_BUFFER_DEBUG */ | |
282 | /* #define PARANOIA */ | |
283 | ||
284 | ||
285 | #define INFTTRACE_ALL_THREADS (-1) | |
286 | #define INFTTRACE_STEP (1) | |
287 | #define INFTTRACE_CONTINUE (0) | |
288 | ||
289 | /* FIX: this is used in inftarg.c/child_wait, in a hack. | |
290 | */ | |
291 | extern int not_same_real_pid; | |
292 | ||
293 | /* This is used to count buffered events. | |
294 | */ | |
295 | static unsigned int more_events_left = 0; | |
296 | ||
297 | /* Process state. | |
298 | */ | |
c5aa993b JM |
299 | typedef enum process_state_enum |
300 | { | |
c906108c SS |
301 | STOPPED, |
302 | FAKE_STEPPING, | |
c5aa993b | 303 | FAKE_CONTINUE, /* For later use */ |
c906108c SS |
304 | RUNNING, |
305 | FORKING, | |
306 | VFORKING | |
c5aa993b JM |
307 | } |
308 | process_state_t; | |
c906108c SS |
309 | |
310 | static process_state_t process_state = STOPPED; | |
311 | ||
312 | /* User-specified stepping modality. | |
313 | */ | |
c5aa993b JM |
314 | typedef enum stepping_mode_enum |
315 | { | |
316 | DO_DEFAULT, /* ...which is a continue! */ | |
c906108c SS |
317 | DO_STEP, |
318 | DO_CONTINUE | |
c5aa993b JM |
319 | } |
320 | stepping_mode_t; | |
321 | ||
c906108c SS |
322 | /* Action to take on an attach, depends on |
323 | * what kind (user command, fork, vfork). | |
324 | * | |
325 | * At the moment, this is either: | |
326 | * | |
327 | * o continue with a SIGTRAP signal, or | |
328 | * | |
329 | * o leave stopped. | |
330 | */ | |
c5aa993b JM |
331 | typedef enum attach_continue_enum |
332 | { | |
333 | DO_ATTACH_CONTINUE, | |
334 | DONT_ATTACH_CONTINUE | |
335 | } | |
336 | attach_continue_t; | |
c906108c SS |
337 | |
338 | /* This flag is true if we are doing a step-over-bpt | |
339 | * with buffered events. We will have to be sure to | |
340 | * report the right thread, as otherwise the spaghetti | |
341 | * code in "infrun.c/wait_for_inferior" will get | |
342 | * confused. | |
343 | */ | |
c5aa993b JM |
344 | static int doing_fake_step = 0; |
345 | static lwpid_t fake_step_tid = 0; | |
c906108c | 346 | \f |
c5aa993b | 347 | |
c906108c SS |
348 | /**************************************************** |
349 | * Thread information structure routines and types. * | |
350 | **************************************************** | |
351 | */ | |
c5aa993b | 352 | typedef |
c906108c | 353 | struct thread_info_struct |
c5aa993b JM |
354 | { |
355 | int am_pseudo; /* This is a pseudo-thread for the process. */ | |
356 | int pid; /* Process ID */ | |
357 | lwpid_t tid; /* Thread ID */ | |
358 | int handled; /* 1 if a buffered event was handled. */ | |
359 | int seen; /* 1 if this thread was seen on a traverse. */ | |
360 | int terminated; /* 1 if thread has terminated. */ | |
361 | int have_signal; /* 1 if signal to be sent */ | |
362 | enum target_signal signal_value; /* Signal to send */ | |
363 | int have_start; /* 1 if alternate starting address */ | |
364 | stepping_mode_t stepping_mode; /* Whether to step or continue */ | |
365 | CORE_ADDR start; /* Where to start */ | |
366 | int have_state; /* 1 if the event state has been set */ | |
367 | ttstate_t last_stop_state; /* The most recently-waited event for this thread. */ | |
c906108c | 368 | struct thread_info_struct |
c5aa993b | 369 | *next; /* All threads are linked via this field. */ |
c906108c | 370 | struct thread_info_struct |
c5aa993b JM |
371 | *next_pseudo; /* All pseudo-threads are linked via this field. */ |
372 | } | |
373 | thread_info; | |
c906108c SS |
374 | |
375 | typedef | |
376 | struct thread_info_header_struct | |
c5aa993b JM |
377 | { |
378 | int count; | |
c906108c SS |
379 | thread_info *head; |
380 | thread_info *head_pseudo; | |
c906108c | 381 | |
c5aa993b JM |
382 | } |
383 | thread_info_header; | |
c906108c | 384 | |
c5aa993b JM |
385 | static thread_info_header thread_head = |
386 | {0, NULL, NULL}; | |
387 | static thread_info_header deleted_threads = | |
388 | {0, NULL, NULL}; | |
c906108c | 389 | |
39f77062 | 390 | static ptid_t saved_real_ptid; |
c906108c | 391 | \f |
c5aa993b | 392 | |
c906108c SS |
393 | /************************************************* |
394 | * Debugging support functions * | |
395 | ************************************************* | |
396 | */ | |
397 | CORE_ADDR | |
fba45db2 | 398 | get_raw_pc (lwpid_t ttid) |
c906108c | 399 | { |
c5aa993b JM |
400 | unsigned long pc_val; |
401 | int offset; | |
402 | int res; | |
403 | ||
404 | offset = register_addr (PC_REGNUM, U_REGS_OFFSET); | |
405 | res = read_from_register_save_state ( | |
406 | ttid, | |
407 | (TTRACE_ARG_TYPE) offset, | |
408 | (char *) &pc_val, | |
409 | sizeof (pc_val)); | |
410 | if (res <= 0) | |
411 | { | |
412 | return (CORE_ADDR) pc_val; | |
413 | } | |
414 | else | |
415 | { | |
416 | return (CORE_ADDR) 0; | |
417 | } | |
418 | } | |
c906108c SS |
419 | |
420 | static char * | |
fba45db2 | 421 | get_printable_name_of_stepping_mode (stepping_mode_t mode) |
c906108c | 422 | { |
c5aa993b JM |
423 | switch (mode) |
424 | { | |
425 | case DO_DEFAULT: | |
426 | return "DO_DEFAULT"; | |
427 | case DO_STEP: | |
428 | return "DO_STEP"; | |
429 | case DO_CONTINUE: | |
430 | return "DO_CONTINUE"; | |
431 | default: | |
432 | return "?unknown mode?"; | |
433 | } | |
c906108c SS |
434 | } |
435 | ||
436 | /* This function returns a pointer to a string describing the | |
437 | * ttrace event being reported. | |
438 | */ | |
439 | char * | |
fba45db2 | 440 | get_printable_name_of_ttrace_event (ttevents_t event) |
c906108c SS |
441 | { |
442 | /* This enumeration is "gappy", so don't use a table. */ | |
c5aa993b JM |
443 | switch (event) |
444 | { | |
c906108c SS |
445 | |
446 | case TTEVT_NONE: | |
c5aa993b | 447 | return "TTEVT_NONE"; |
c906108c | 448 | case TTEVT_SIGNAL: |
c5aa993b | 449 | return "TTEVT_SIGNAL"; |
c906108c | 450 | case TTEVT_FORK: |
c5aa993b | 451 | return "TTEVT_FORK"; |
c906108c | 452 | case TTEVT_EXEC: |
c5aa993b | 453 | return "TTEVT_EXEC"; |
c906108c | 454 | case TTEVT_EXIT: |
c5aa993b | 455 | return "TTEVT_EXIT"; |
c906108c | 456 | case TTEVT_VFORK: |
c5aa993b | 457 | return "TTEVT_VFORK"; |
c906108c | 458 | case TTEVT_SYSCALL_RETURN: |
c5aa993b | 459 | return "TTEVT_SYSCALL_RETURN"; |
c906108c | 460 | case TTEVT_LWP_CREATE: |
c5aa993b | 461 | return "TTEVT_LWP_CREATE"; |
c906108c | 462 | case TTEVT_LWP_TERMINATE: |
c5aa993b | 463 | return "TTEVT_LWP_TERMINATE"; |
c906108c | 464 | case TTEVT_LWP_EXIT: |
c5aa993b | 465 | return "TTEVT_LWP_EXIT"; |
c906108c | 466 | case TTEVT_LWP_ABORT_SYSCALL: |
c5aa993b | 467 | return "TTEVT_LWP_ABORT_SYSCALL"; |
c906108c | 468 | case TTEVT_SYSCALL_ENTRY: |
c5aa993b JM |
469 | return "TTEVT_SYSCALL_ENTRY"; |
470 | case TTEVT_SYSCALL_RESTART: | |
471 | return "TTEVT_SYSCALL_RESTART"; | |
472 | default: | |
c906108c | 473 | return "?new event?"; |
c5aa993b | 474 | } |
c906108c | 475 | } |
c906108c | 476 | \f |
c5aa993b | 477 | |
c906108c SS |
478 | /* This function translates the ttrace request enumeration into |
479 | * a character string that is its printable (aka "human readable") | |
480 | * name. | |
481 | */ | |
482 | char * | |
fba45db2 | 483 | get_printable_name_of_ttrace_request (ttreq_t request) |
c906108c SS |
484 | { |
485 | if (!IS_TTRACE_REQ (request)) | |
486 | return "?bad req?"; | |
487 | ||
488 | /* This enumeration is "gappy", so don't use a table. */ | |
c5aa993b JM |
489 | switch (request) |
490 | { | |
491 | case TT_PROC_SETTRC: | |
c906108c | 492 | return "TT_PROC_SETTRC"; |
c5aa993b | 493 | case TT_PROC_ATTACH: |
c906108c | 494 | return "TT_PROC_ATTACH"; |
c5aa993b | 495 | case TT_PROC_DETACH: |
c906108c | 496 | return "TT_PROC_DETACH"; |
c5aa993b | 497 | case TT_PROC_RDTEXT: |
c906108c | 498 | return "TT_PROC_RDTEXT"; |
c5aa993b | 499 | case TT_PROC_WRTEXT: |
c906108c | 500 | return "TT_PROC_WRTEXT"; |
c5aa993b | 501 | case TT_PROC_RDDATA: |
c906108c | 502 | return "TT_PROC_RDDATA"; |
c5aa993b | 503 | case TT_PROC_WRDATA: |
c906108c | 504 | return "TT_PROC_WRDATA"; |
c5aa993b | 505 | case TT_PROC_STOP: |
c906108c | 506 | return "TT_PROC_STOP"; |
c5aa993b | 507 | case TT_PROC_CONTINUE: |
c906108c | 508 | return "TT_PROC_CONTINUE"; |
c5aa993b | 509 | case TT_PROC_GET_PATHNAME: |
c906108c | 510 | return "TT_PROC_GET_PATHNAME"; |
c5aa993b | 511 | case TT_PROC_GET_EVENT_MASK: |
c906108c | 512 | return "TT_PROC_GET_EVENT_MASK"; |
c5aa993b | 513 | case TT_PROC_SET_EVENT_MASK: |
c906108c | 514 | return "TT_PROC_SET_EVENT_MASK"; |
c5aa993b | 515 | case TT_PROC_GET_FIRST_LWP_STATE: |
c906108c | 516 | return "TT_PROC_GET_FIRST_LWP_STATE"; |
c5aa993b | 517 | case TT_PROC_GET_NEXT_LWP_STATE: |
c906108c | 518 | return "TT_PROC_GET_NEXT_LWP_STATE"; |
c5aa993b | 519 | case TT_PROC_EXIT: |
c906108c | 520 | return "TT_PROC_EXIT"; |
c5aa993b | 521 | case TT_PROC_GET_MPROTECT: |
c906108c | 522 | return "TT_PROC_GET_MPROTECT"; |
c5aa993b | 523 | case TT_PROC_SET_MPROTECT: |
c906108c | 524 | return "TT_PROC_SET_MPROTECT"; |
c5aa993b | 525 | case TT_PROC_SET_SCBM: |
c906108c | 526 | return "TT_PROC_SET_SCBM"; |
c5aa993b | 527 | case TT_LWP_STOP: |
c906108c | 528 | return "TT_LWP_STOP"; |
c5aa993b | 529 | case TT_LWP_CONTINUE: |
c906108c | 530 | return "TT_LWP_CONTINUE"; |
c5aa993b | 531 | case TT_LWP_SINGLE: |
c906108c | 532 | return "TT_LWP_SINGLE"; |
c5aa993b | 533 | case TT_LWP_RUREGS: |
c906108c | 534 | return "TT_LWP_RUREGS"; |
c5aa993b | 535 | case TT_LWP_WUREGS: |
c906108c | 536 | return "TT_LWP_WUREGS"; |
c5aa993b | 537 | case TT_LWP_GET_EVENT_MASK: |
c906108c | 538 | return "TT_LWP_GET_EVENT_MASK"; |
c5aa993b | 539 | case TT_LWP_SET_EVENT_MASK: |
c906108c | 540 | return "TT_LWP_SET_EVENT_MASK"; |
c5aa993b | 541 | case TT_LWP_GET_STATE: |
c906108c | 542 | return "TT_LWP_GET_STATE"; |
c5aa993b | 543 | default: |
c906108c | 544 | return "?new req?"; |
c5aa993b | 545 | } |
c906108c | 546 | } |
c906108c | 547 | \f |
c5aa993b | 548 | |
c906108c SS |
549 | /* This function translates the process state enumeration into |
550 | * a character string that is its printable (aka "human readable") | |
551 | * name. | |
552 | */ | |
553 | static char * | |
fba45db2 | 554 | get_printable_name_of_process_state (process_state_t process_state) |
c906108c | 555 | { |
c5aa993b JM |
556 | switch (process_state) |
557 | { | |
c906108c SS |
558 | case STOPPED: |
559 | return "STOPPED"; | |
560 | case FAKE_STEPPING: | |
561 | return "FAKE_STEPPING"; | |
562 | case RUNNING: | |
563 | return "RUNNING"; | |
564 | case FORKING: | |
565 | return "FORKING"; | |
566 | case VFORKING: | |
567 | return "VFORKING"; | |
568 | default: | |
569 | return "?some unknown state?"; | |
c5aa993b | 570 | } |
c906108c SS |
571 | } |
572 | ||
573 | /* Set a ttrace thread state to a safe, initial state. | |
574 | */ | |
575 | static void | |
fba45db2 | 576 | clear_ttstate_t (ttstate_t *tts) |
c906108c | 577 | { |
c5aa993b JM |
578 | tts->tts_pid = 0; |
579 | tts->tts_lwpid = 0; | |
580 | tts->tts_user_tid = 0; | |
581 | tts->tts_event = TTEVT_NONE; | |
c906108c SS |
582 | } |
583 | ||
584 | /* Copy ttrace thread state TTS_FROM into TTS_TO. | |
585 | */ | |
586 | static void | |
fba45db2 | 587 | copy_ttstate_t (ttstate_t *tts_to, ttstate_t *tts_from) |
c906108c | 588 | { |
c5aa993b | 589 | memcpy ((char *) tts_to, (char *) tts_from, sizeof (*tts_to)); |
c906108c SS |
590 | } |
591 | ||
592 | /* Are there any live threads we know about? | |
593 | */ | |
c5aa993b | 594 | static int |
fba45db2 | 595 | any_thread_records (void) |
c906108c | 596 | { |
c5aa993b | 597 | return (thread_head.count > 0); |
c906108c SS |
598 | } |
599 | ||
600 | /* Create, fill in and link in a thread descriptor. | |
601 | */ | |
602 | static thread_info * | |
fba45db2 | 603 | create_thread_info (int pid, lwpid_t tid) |
c906108c | 604 | { |
c5aa993b JM |
605 | thread_info *new_p; |
606 | thread_info *p; | |
607 | int thread_count_of_pid; | |
608 | ||
3c37485b | 609 | new_p = xmalloc (sizeof (thread_info)); |
c5aa993b JM |
610 | new_p->pid = pid; |
611 | new_p->tid = tid; | |
612 | new_p->have_signal = 0; | |
613 | new_p->have_start = 0; | |
614 | new_p->have_state = 0; | |
615 | clear_ttstate_t (&new_p->last_stop_state); | |
616 | new_p->am_pseudo = 0; | |
617 | new_p->handled = 0; | |
618 | new_p->seen = 0; | |
619 | new_p->terminated = 0; | |
620 | new_p->next = NULL; | |
621 | new_p->next_pseudo = NULL; | |
622 | new_p->stepping_mode = DO_DEFAULT; | |
623 | ||
624 | if (0 == thread_head.count) | |
625 | { | |
c906108c | 626 | #ifdef THREAD_DEBUG |
c5aa993b JM |
627 | if (debug_on) |
628 | printf ("First thread, pid %d tid %d!\n", pid, tid); | |
c906108c | 629 | #endif |
39f77062 | 630 | saved_real_ptid = inferior_ptid; |
c906108c | 631 | } |
c5aa993b JM |
632 | else |
633 | { | |
c906108c | 634 | #ifdef THREAD_DEBUG |
c5aa993b JM |
635 | if (debug_on) |
636 | printf ("Subsequent thread, pid %d tid %d\n", pid, tid); | |
c906108c SS |
637 | #endif |
638 | } | |
639 | ||
c5aa993b JM |
640 | /* Another day, another thread... |
641 | */ | |
642 | thread_head.count++; | |
c906108c | 643 | |
c5aa993b JM |
644 | /* The new thread always goes at the head of the list. |
645 | */ | |
646 | new_p->next = thread_head.head; | |
647 | thread_head.head = new_p; | |
c906108c | 648 | |
c5aa993b JM |
649 | /* Is this the "pseudo" thread of a process? It is if there's |
650 | * no other thread for this process on the list. (Note that this | |
651 | * accomodates multiple processes, such as we see even for simple | |
652 | * cases like forking "non-threaded" programs.) | |
653 | */ | |
654 | p = thread_head.head; | |
655 | thread_count_of_pid = 0; | |
656 | while (p) | |
657 | { | |
658 | if (p->pid == new_p->pid) | |
659 | thread_count_of_pid++; | |
660 | p = p->next; | |
661 | } | |
662 | ||
663 | /* Did we see any other threads for this pid? (Recall that we just | |
664 | * added this thread to the list...) | |
665 | */ | |
666 | if (thread_count_of_pid == 1) | |
667 | { | |
668 | new_p->am_pseudo = 1; | |
669 | new_p->next_pseudo = thread_head.head_pseudo; | |
670 | thread_head.head_pseudo = new_p; | |
671 | } | |
672 | ||
673 | return new_p; | |
c906108c SS |
674 | } |
675 | ||
676 | /* Get rid of our thread info. | |
677 | */ | |
678 | static void | |
fba45db2 | 679 | clear_thread_info (void) |
c906108c | 680 | { |
c5aa993b JM |
681 | thread_info *p; |
682 | thread_info *q; | |
c906108c SS |
683 | |
684 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
685 | if (debug_on) |
686 | printf ("Clearing all thread info\n"); | |
c906108c SS |
687 | #endif |
688 | ||
c5aa993b JM |
689 | p = thread_head.head; |
690 | while (p) | |
691 | { | |
692 | q = p; | |
693 | p = p->next; | |
b8c9b27d | 694 | xfree (q); |
c906108c SS |
695 | } |
696 | ||
c5aa993b JM |
697 | thread_head.head = NULL; |
698 | thread_head.head_pseudo = NULL; | |
699 | thread_head.count = 0; | |
c906108c | 700 | |
c5aa993b JM |
701 | p = deleted_threads.head; |
702 | while (p) | |
703 | { | |
704 | q = p; | |
705 | p = p->next; | |
b8c9b27d | 706 | xfree (q); |
c906108c SS |
707 | } |
708 | ||
c5aa993b JM |
709 | deleted_threads.head = NULL; |
710 | deleted_threads.head_pseudo = NULL; | |
711 | deleted_threads.count = 0; | |
c906108c | 712 | |
c5aa993b JM |
713 | /* No threads, so can't have pending events. |
714 | */ | |
715 | more_events_left = 0; | |
c906108c SS |
716 | } |
717 | ||
718 | /* Given a tid, find the thread block for it. | |
719 | */ | |
720 | static thread_info * | |
fba45db2 | 721 | find_thread_info (lwpid_t tid) |
c906108c | 722 | { |
c5aa993b | 723 | thread_info *p; |
c906108c | 724 | |
c5aa993b JM |
725 | for (p = thread_head.head; p; p = p->next) |
726 | { | |
727 | if (p->tid == tid) | |
728 | { | |
729 | return p; | |
730 | } | |
c906108c SS |
731 | } |
732 | ||
c5aa993b JM |
733 | for (p = deleted_threads.head; p; p = p->next) |
734 | { | |
735 | if (p->tid == tid) | |
736 | { | |
737 | return p; | |
738 | } | |
c906108c | 739 | } |
c5aa993b JM |
740 | |
741 | return NULL; | |
c906108c SS |
742 | } |
743 | ||
744 | /* For any but the pseudo thread, this maps to the | |
745 | * thread ID. For the pseudo thread, if you pass either | |
746 | * the thread id or the PID, you get the pseudo thread ID. | |
747 | * | |
748 | * We have to be prepared for core gdb to ask about | |
749 | * deleted threads. We do the map, but we don't like it. | |
750 | */ | |
751 | static lwpid_t | |
fba45db2 | 752 | map_from_gdb_tid (lwpid_t gdb_tid) |
c906108c | 753 | { |
c5aa993b | 754 | thread_info *p; |
c906108c | 755 | |
c5aa993b JM |
756 | /* First assume gdb_tid really is a tid, and try to find a |
757 | * matching entry on the threads list. | |
758 | */ | |
759 | for (p = thread_head.head; p; p = p->next) | |
760 | { | |
761 | if (p->tid == gdb_tid) | |
762 | return gdb_tid; | |
c906108c SS |
763 | } |
764 | ||
c5aa993b JM |
765 | /* It doesn't appear to be a tid; perhaps it's really a pid? |
766 | * Try to find a "pseudo" thread entry on the threads list. | |
767 | */ | |
768 | for (p = thread_head.head_pseudo; p != NULL; p = p->next_pseudo) | |
769 | { | |
770 | if (p->pid == gdb_tid) | |
771 | return p->tid; | |
c906108c SS |
772 | } |
773 | ||
c5aa993b JM |
774 | /* Perhaps it's the tid of a deleted thread we may still |
775 | * have some knowledge of? | |
776 | */ | |
777 | for (p = deleted_threads.head; p; p = p->next) | |
778 | { | |
779 | if (p->tid == gdb_tid) | |
780 | return gdb_tid; | |
781 | } | |
c906108c | 782 | |
c5aa993b JM |
783 | /* Or perhaps it's the pid of a deleted process we may still |
784 | * have knowledge of? | |
785 | */ | |
786 | for (p = deleted_threads.head_pseudo; p != NULL; p = p->next_pseudo) | |
787 | { | |
788 | if (p->pid == gdb_tid) | |
789 | return p->tid; | |
790 | } | |
791 | ||
792 | return 0; /* Error? */ | |
c906108c SS |
793 | } |
794 | ||
795 | /* Map the other way: from a real tid to the | |
796 | * "pid" known by core gdb. This tid may be | |
797 | * for a thread that just got deleted, so we | |
798 | * also need to consider deleted threads. | |
799 | */ | |
800 | static lwpid_t | |
fba45db2 | 801 | map_to_gdb_tid (lwpid_t real_tid) |
c906108c | 802 | { |
c5aa993b | 803 | thread_info *p; |
c906108c | 804 | |
c5aa993b JM |
805 | for (p = thread_head.head; p; p = p->next) |
806 | { | |
807 | if (p->tid == real_tid) | |
808 | { | |
809 | if (p->am_pseudo) | |
810 | return p->pid; | |
811 | else | |
812 | return real_tid; | |
813 | } | |
c906108c SS |
814 | } |
815 | ||
c5aa993b JM |
816 | for (p = deleted_threads.head; p; p = p->next) |
817 | { | |
818 | if (p->tid == real_tid) | |
819 | if (p->am_pseudo) | |
820 | return p->pid; /* Error? */ | |
821 | else | |
822 | return real_tid; | |
c906108c SS |
823 | } |
824 | ||
c5aa993b | 825 | return 0; /* Error? Never heard of this thread! */ |
c906108c SS |
826 | } |
827 | ||
828 | /* Do any threads have saved signals? | |
829 | */ | |
c5aa993b | 830 | static int |
fba45db2 | 831 | saved_signals_exist (void) |
c906108c | 832 | { |
c5aa993b JM |
833 | thread_info *p; |
834 | ||
835 | for (p = thread_head.head; p; p = p->next) | |
836 | { | |
837 | if (p->have_signal) | |
838 | { | |
839 | return 1; | |
840 | } | |
c906108c SS |
841 | } |
842 | ||
c5aa993b | 843 | return 0; |
c906108c SS |
844 | } |
845 | ||
846 | /* Is this the tid for the zero-th thread? | |
847 | */ | |
c5aa993b | 848 | static int |
fba45db2 | 849 | is_pseudo_thread (lwpid_t tid) |
c906108c | 850 | { |
c5aa993b JM |
851 | thread_info *p = find_thread_info (tid); |
852 | if (NULL == p || p->terminated) | |
853 | return 0; | |
854 | else | |
855 | return p->am_pseudo; | |
c906108c SS |
856 | } |
857 | ||
858 | /* Is this thread terminated? | |
859 | */ | |
c5aa993b | 860 | static int |
fba45db2 | 861 | is_terminated (lwpid_t tid) |
c906108c | 862 | { |
c5aa993b | 863 | thread_info *p = find_thread_info (tid); |
c906108c | 864 | |
c5aa993b JM |
865 | if (NULL != p) |
866 | return p->terminated; | |
c906108c | 867 | |
c5aa993b | 868 | return 0; |
c906108c SS |
869 | } |
870 | ||
871 | /* Is this pid a real PID or a TID? | |
872 | */ | |
c5aa993b | 873 | static int |
fba45db2 | 874 | is_process_id (int pid) |
c906108c | 875 | { |
c5aa993b JM |
876 | lwpid_t tid; |
877 | thread_info *tinfo; | |
878 | pid_t this_pid; | |
879 | int this_pid_count; | |
c906108c SS |
880 | |
881 | /* What does PID really represent? | |
882 | */ | |
883 | tid = map_from_gdb_tid (pid); | |
884 | if (tid <= 0) | |
c5aa993b | 885 | return 0; /* Actually, is probably an error... */ |
c906108c SS |
886 | |
887 | tinfo = find_thread_info (tid); | |
888 | ||
889 | /* Does it appear to be a true thread? | |
890 | */ | |
c5aa993b | 891 | if (!tinfo->am_pseudo) |
c906108c SS |
892 | return 0; |
893 | ||
894 | /* Else, it looks like it may be a process. See if there's any other | |
895 | * threads with the same process ID, though. If there are, then TID | |
896 | * just happens to be the first thread of several for this process. | |
897 | */ | |
898 | this_pid = tinfo->pid; | |
899 | this_pid_count = 0; | |
900 | for (tinfo = thread_head.head; tinfo; tinfo = tinfo->next) | |
901 | { | |
902 | if (tinfo->pid == this_pid) | |
c5aa993b | 903 | this_pid_count++; |
c906108c SS |
904 | } |
905 | ||
906 | return (this_pid_count == 1); | |
907 | } | |
908 | ||
909 | ||
910 | /* Add a thread to our info. Prevent duplicate entries. | |
911 | */ | |
912 | static thread_info * | |
fba45db2 | 913 | add_tthread (int pid, lwpid_t tid) |
c906108c | 914 | { |
c5aa993b | 915 | thread_info *p; |
c906108c | 916 | |
c5aa993b JM |
917 | p = find_thread_info (tid); |
918 | if (NULL == p) | |
919 | p = create_thread_info (pid, tid); | |
c906108c | 920 | |
c5aa993b | 921 | return p; |
c906108c SS |
922 | } |
923 | ||
924 | /* Notice that a thread was deleted. | |
925 | */ | |
926 | static void | |
fba45db2 | 927 | del_tthread (lwpid_t tid) |
c906108c | 928 | { |
c5aa993b JM |
929 | thread_info *p; |
930 | thread_info *chase; | |
c906108c | 931 | |
c5aa993b JM |
932 | if (thread_head.count <= 0) |
933 | { | |
934 | error ("Internal error in thread database."); | |
935 | return; | |
c906108c SS |
936 | } |
937 | ||
c5aa993b JM |
938 | chase = NULL; |
939 | for (p = thread_head.head; p; p = p->next) | |
940 | { | |
941 | if (p->tid == tid) | |
942 | { | |
c906108c SS |
943 | |
944 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
945 | if (debug_on) |
946 | printf ("Delete here: %d \n", tid); | |
c906108c SS |
947 | #endif |
948 | ||
c5aa993b JM |
949 | if (p->am_pseudo) |
950 | { | |
951 | /* | |
952 | * Deleting a main thread is ok if we're doing | |
953 | * a parent-follow on a child; this is odd but | |
954 | * not wrong. It apparently _doesn't_ happen | |
955 | * on the child-follow, as we don't just delete | |
956 | * the pseudo while keeping the rest of the | |
957 | * threads around--instead, we clear out the whole | |
958 | * thread list at once. | |
959 | */ | |
960 | thread_info *q; | |
961 | thread_info *q_chase; | |
962 | ||
963 | q_chase = NULL; | |
964 | for (q = thread_head.head_pseudo; q; q = q->next) | |
965 | { | |
966 | if (q == p) | |
967 | { | |
968 | /* Remove from pseudo list. | |
969 | */ | |
970 | if (q_chase == NULL) | |
971 | thread_head.head_pseudo = p->next_pseudo; | |
972 | else | |
973 | q_chase->next = p->next_pseudo; | |
974 | } | |
975 | else | |
976 | q_chase = q; | |
977 | } | |
978 | } | |
979 | ||
980 | /* Remove from live list. | |
981 | */ | |
982 | thread_head.count--; | |
983 | ||
984 | if (NULL == chase) | |
985 | thread_head.head = p->next; | |
986 | else | |
987 | chase->next = p->next; | |
988 | ||
989 | /* Add to deleted thread list. | |
990 | */ | |
991 | p->next = deleted_threads.head; | |
992 | deleted_threads.head = p; | |
993 | deleted_threads.count++; | |
994 | if (p->am_pseudo) | |
995 | { | |
996 | p->next_pseudo = deleted_threads.head_pseudo; | |
997 | deleted_threads.head_pseudo = p; | |
998 | } | |
999 | p->terminated = 1; | |
1000 | ||
1001 | return; | |
1002 | } | |
1003 | ||
1004 | else | |
1005 | chase = p; | |
c906108c SS |
1006 | } |
1007 | } | |
1008 | ||
1009 | /* Get the pid for this tid. (Has to be a real TID!). | |
1010 | */ | |
1011 | static int | |
fba45db2 | 1012 | get_pid_for (lwpid_t tid) |
c906108c | 1013 | { |
c5aa993b | 1014 | thread_info *p; |
c906108c | 1015 | |
c5aa993b JM |
1016 | for (p = thread_head.head; p; p = p->next) |
1017 | { | |
1018 | if (p->tid == tid) | |
1019 | { | |
1020 | return p->pid; | |
1021 | } | |
c906108c SS |
1022 | } |
1023 | ||
c5aa993b JM |
1024 | for (p = deleted_threads.head; p; p = p->next) |
1025 | { | |
1026 | if (p->tid == tid) | |
1027 | { | |
1028 | return p->pid; | |
1029 | } | |
c906108c | 1030 | } |
c5aa993b JM |
1031 | |
1032 | return 0; | |
c906108c SS |
1033 | } |
1034 | ||
1035 | /* Note that this thread's current event has been handled. | |
1036 | */ | |
1037 | static void | |
fba45db2 | 1038 | set_handled (int pid, lwpid_t tid) |
c906108c | 1039 | { |
c5aa993b JM |
1040 | thread_info *p; |
1041 | ||
1042 | p = find_thread_info (tid); | |
1043 | if (NULL == p) | |
1044 | p = add_tthread (pid, tid); | |
c906108c | 1045 | |
c5aa993b | 1046 | p->handled = 1; |
c906108c SS |
1047 | } |
1048 | ||
1049 | /* Was this thread's current event handled? | |
1050 | */ | |
c5aa993b | 1051 | static int |
fba45db2 | 1052 | was_handled (lwpid_t tid) |
c906108c | 1053 | { |
c5aa993b JM |
1054 | thread_info *p; |
1055 | ||
1056 | p = find_thread_info (tid); | |
1057 | if (NULL != p) | |
1058 | return p->handled; | |
c906108c | 1059 | |
c5aa993b | 1060 | return 0; /* New threads have not been handled */ |
c906108c SS |
1061 | } |
1062 | ||
1063 | /* Set this thread to unhandled. | |
1064 | */ | |
1065 | static void | |
fba45db2 | 1066 | clear_handled (lwpid_t tid) |
c906108c | 1067 | { |
c5aa993b JM |
1068 | thread_info *p; |
1069 | ||
c906108c | 1070 | #ifdef WAIT_BUFFER_DEBUG |
c5aa993b JM |
1071 | if (debug_on) |
1072 | printf ("clear_handled %d\n", (int) tid); | |
c906108c SS |
1073 | #endif |
1074 | ||
1075 | p = find_thread_info (tid); | |
1076 | if (p == NULL) | |
1077 | error ("Internal error: No thread state to clear?"); | |
1078 | ||
1079 | p->handled = 0; | |
1080 | } | |
1081 | ||
1082 | /* Set all threads to unhandled. | |
1083 | */ | |
1084 | static void | |
fba45db2 | 1085 | clear_all_handled (void) |
c906108c | 1086 | { |
c5aa993b | 1087 | thread_info *p; |
c906108c SS |
1088 | |
1089 | #ifdef WAIT_BUFFER_DEBUG | |
c5aa993b JM |
1090 | if (debug_on) |
1091 | printf ("clear_all_handled\n"); | |
c906108c SS |
1092 | #endif |
1093 | ||
c5aa993b JM |
1094 | for (p = thread_head.head; p; p = p->next) |
1095 | { | |
1096 | p->handled = 0; | |
c906108c SS |
1097 | } |
1098 | ||
c5aa993b JM |
1099 | for (p = deleted_threads.head; p; p = p->next) |
1100 | { | |
1101 | p->handled = 0; | |
c906108c SS |
1102 | } |
1103 | } | |
1104 | ||
1105 | /* Set this thread to default stepping mode. | |
1106 | */ | |
1107 | static void | |
fba45db2 | 1108 | clear_stepping_mode (lwpid_t tid) |
c906108c | 1109 | { |
c5aa993b JM |
1110 | thread_info *p; |
1111 | ||
c906108c | 1112 | #ifdef WAIT_BUFFER_DEBUG |
c5aa993b JM |
1113 | if (debug_on) |
1114 | printf ("clear_stepping_mode %d\n", (int) tid); | |
c906108c SS |
1115 | #endif |
1116 | ||
1117 | p = find_thread_info (tid); | |
1118 | if (p == NULL) | |
1119 | error ("Internal error: No thread state to clear?"); | |
1120 | ||
1121 | p->stepping_mode = DO_DEFAULT; | |
1122 | } | |
1123 | ||
1124 | /* Set all threads to do default continue on resume. | |
1125 | */ | |
1126 | static void | |
fba45db2 | 1127 | clear_all_stepping_mode (void) |
c906108c | 1128 | { |
c5aa993b | 1129 | thread_info *p; |
c906108c SS |
1130 | |
1131 | #ifdef WAIT_BUFFER_DEBUG | |
c5aa993b JM |
1132 | if (debug_on) |
1133 | printf ("clear_all_stepping_mode\n"); | |
c906108c SS |
1134 | #endif |
1135 | ||
c5aa993b JM |
1136 | for (p = thread_head.head; p; p = p->next) |
1137 | { | |
1138 | p->stepping_mode = DO_DEFAULT; | |
c906108c SS |
1139 | } |
1140 | ||
c5aa993b JM |
1141 | for (p = deleted_threads.head; p; p = p->next) |
1142 | { | |
1143 | p->stepping_mode = DO_DEFAULT; | |
c906108c SS |
1144 | } |
1145 | } | |
1146 | ||
1147 | /* Set all threads to unseen on this pass. | |
c5aa993b | 1148 | */ |
c906108c | 1149 | static void |
fba45db2 | 1150 | set_all_unseen (void) |
c906108c | 1151 | { |
c5aa993b | 1152 | thread_info *p; |
c906108c | 1153 | |
c5aa993b JM |
1154 | for (p = thread_head.head; p; p = p->next) |
1155 | { | |
1156 | p->seen = 0; | |
c906108c SS |
1157 | } |
1158 | } | |
1159 | ||
1160 | #if (defined( THREAD_DEBUG ) || defined( PARANOIA )) | |
1161 | /* debugging routine. | |
1162 | */ | |
1163 | static void | |
fba45db2 | 1164 | print_tthread (thread_info *p) |
c906108c | 1165 | { |
c5aa993b JM |
1166 | printf (" Thread pid %d, tid %d", p->pid, p->tid); |
1167 | if (p->have_state) | |
1168 | printf (", event is %s", | |
1169 | get_printable_name_of_ttrace_event (p->last_stop_state.tts_event)); | |
1170 | ||
1171 | if (p->am_pseudo) | |
1172 | printf (", pseudo thread"); | |
1173 | ||
1174 | if (p->have_signal) | |
1175 | printf (", have signal 0x%x", p->signal_value); | |
1176 | ||
1177 | if (p->have_start) | |
1178 | printf (", have start at 0x%x", p->start); | |
1179 | ||
1180 | printf (", step is %s", get_printable_name_of_stepping_mode (p->stepping_mode)); | |
1181 | ||
1182 | if (p->handled) | |
1183 | printf (", handled"); | |
1184 | else | |
1185 | printf (", not handled"); | |
1186 | ||
1187 | if (p->seen) | |
1188 | printf (", seen"); | |
1189 | else | |
1190 | printf (", not seen"); | |
1191 | ||
1192 | printf ("\n"); | |
c906108c SS |
1193 | } |
1194 | ||
1195 | static void | |
fba45db2 | 1196 | print_tthreads (void) |
c906108c | 1197 | { |
c5aa993b JM |
1198 | thread_info *p; |
1199 | ||
1200 | if (thread_head.count == 0) | |
1201 | printf ("Thread list is empty\n"); | |
1202 | else | |
1203 | { | |
1204 | printf ("Thread list has "); | |
1205 | if (thread_head.count == 1) | |
1206 | printf ("1 entry:\n"); | |
1207 | else | |
1208 | printf ("%d entries:\n", thread_head.count); | |
1209 | for (p = thread_head.head; p; p = p->next) | |
1210 | { | |
1211 | print_tthread (p); | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | if (deleted_threads.count == 0) | |
1216 | printf ("Deleted thread list is empty\n"); | |
1217 | else | |
1218 | { | |
1219 | printf ("Deleted thread list has "); | |
1220 | if (deleted_threads.count == 1) | |
1221 | printf ("1 entry:\n"); | |
1222 | else | |
1223 | printf ("%d entries:\n", deleted_threads.count); | |
1224 | ||
1225 | for (p = deleted_threads.head; p; p = p->next) | |
1226 | { | |
1227 | print_tthread (p); | |
1228 | } | |
c906108c SS |
1229 | } |
1230 | } | |
1231 | #endif | |
1232 | ||
1233 | /* Update the thread list based on the "seen" bits. | |
1234 | */ | |
1235 | static void | |
fba45db2 | 1236 | update_thread_list (void) |
c906108c | 1237 | { |
c5aa993b JM |
1238 | thread_info *p; |
1239 | thread_info *chase; | |
c906108c | 1240 | |
c5aa993b JM |
1241 | chase = NULL; |
1242 | for (p = thread_head.head; p; p = p->next) | |
1243 | { | |
c906108c | 1244 | /* Is this an "unseen" thread which really happens to be a process? |
39f77062 | 1245 | If so, is it inferior_ptid and is a vfork in flight? If yes to |
c906108c SS |
1246 | all, then DON'T REMOVE IT! We're in the midst of moving a vfork |
1247 | operation, which is a multiple step thing, to the point where we | |
1248 | can touch the parent again. We've most likely stopped to examine | |
1249 | the child at a late stage in the vfork, and if we're not following | |
1250 | the child, we'd best not treat the parent as a dead "thread"... | |
c5aa993b JM |
1251 | */ |
1252 | if ((!p->seen) && p->am_pseudo && vfork_in_flight | |
1253 | && (p->pid != vforking_child_pid)) | |
1254 | p->seen = 1; | |
c906108c | 1255 | |
c5aa993b JM |
1256 | if (!p->seen) |
1257 | { | |
1258 | /* Remove this one | |
1259 | */ | |
c906108c SS |
1260 | |
1261 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
1262 | if (debug_on) |
1263 | printf ("Delete unseen thread: %d \n", p->tid); | |
c906108c | 1264 | #endif |
c5aa993b JM |
1265 | del_tthread (p->tid); |
1266 | } | |
c906108c SS |
1267 | } |
1268 | } | |
c906108c | 1269 | \f |
c5aa993b JM |
1270 | |
1271 | ||
c906108c SS |
1272 | /************************************************ |
1273 | * O/S call wrappers * | |
1274 | ************************************************ | |
1275 | */ | |
1276 | ||
1277 | /* This function simply calls ttrace with the given arguments. | |
1278 | * It exists so that all calls to ttrace are isolated. All | |
1279 | * parameters should be as specified by "man 2 ttrace". | |
1280 | * | |
1281 | * No other "raw" calls to ttrace should exist in this module. | |
1282 | */ | |
1283 | static int | |
fba45db2 KB |
1284 | call_real_ttrace (ttreq_t request, pid_t pid, lwpid_t tid, TTRACE_ARG_TYPE addr, |
1285 | TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2) | |
c906108c | 1286 | { |
c5aa993b | 1287 | int tt_status; |
c906108c SS |
1288 | |
1289 | errno = 0; | |
c5aa993b | 1290 | tt_status = ttrace (request, pid, tid, addr, data, addr2); |
c906108c SS |
1291 | |
1292 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
1293 | if (errno) |
1294 | { | |
1295 | /* Don't bother for a known benign error: if you ask for the | |
1296 | * first thread state, but there is only one thread and it's | |
1297 | * not stopped, ttrace complains. | |
1298 | * | |
1299 | * We have this inside the #ifdef because our caller will do | |
1300 | * this check for real. | |
1301 | */ | |
1302 | if (request != TT_PROC_GET_FIRST_LWP_STATE | |
1303 | || errno != EPROTO) | |
1304 | { | |
1305 | if (debug_on) | |
1306 | printf ("TT fail for %s, with pid %d, tid %d, status %d \n", | |
1307 | get_printable_name_of_ttrace_request (request), | |
1308 | pid, tid, tt_status); | |
1309 | } | |
c906108c | 1310 | } |
c906108c SS |
1311 | #endif |
1312 | ||
1313 | #if 0 | |
1314 | /* ??rehrauer: It would probably be most robust to catch and report | |
1315 | * failed requests here. However, some clients of this interface | |
1316 | * seem to expect to catch & deal with them, so we'd best not. | |
1317 | */ | |
c5aa993b JM |
1318 | if (errno) |
1319 | { | |
1320 | strcpy (reason_for_failure, "ttrace ("); | |
1321 | strcat (reason_for_failure, get_printable_name_of_ttrace_request (request)); | |
1322 | strcat (reason_for_failure, ")"); | |
1323 | printf ("ttrace error, errno = %d\n", errno); | |
1324 | perror_with_name (reason_for_failure); | |
1325 | } | |
c906108c SS |
1326 | #endif |
1327 | ||
1328 | return tt_status; | |
1329 | } | |
c906108c | 1330 | \f |
c5aa993b | 1331 | |
c906108c SS |
1332 | /* This function simply calls ttrace_wait with the given arguments. |
1333 | * It exists so that all calls to ttrace_wait are isolated. | |
1334 | * | |
1335 | * No "raw" calls to ttrace_wait should exist elsewhere. | |
1336 | */ | |
1337 | static int | |
fba45db2 KB |
1338 | call_real_ttrace_wait (int pid, lwpid_t tid, ttwopt_t option, ttstate_t *tsp, |
1339 | size_t tsp_size) | |
c906108c | 1340 | { |
c5aa993b JM |
1341 | int ttw_status; |
1342 | thread_info *tinfo = NULL; | |
c906108c SS |
1343 | |
1344 | errno = 0; | |
1345 | ttw_status = ttrace_wait (pid, tid, option, tsp, tsp_size); | |
c5aa993b JM |
1346 | |
1347 | if (errno) | |
1348 | { | |
c906108c | 1349 | #ifdef THREAD_DEBUG |
c5aa993b JM |
1350 | if (debug_on) |
1351 | printf ("TW fail with pid %d, tid %d \n", pid, tid); | |
c906108c SS |
1352 | #endif |
1353 | ||
1354 | perror_with_name ("ttrace wait"); | |
c5aa993b | 1355 | } |
c906108c SS |
1356 | |
1357 | return ttw_status; | |
1358 | } | |
c906108c | 1359 | \f |
c5aa993b | 1360 | |
c906108c SS |
1361 | /* A process may have one or more kernel threads, of which all or |
1362 | none may be stopped. This function returns the ID of the first | |
1363 | kernel thread in a stopped state, or 0 if none are stopped. | |
1364 | ||
1365 | This function can be used with get_process_next_stopped_thread_id | |
1366 | to iterate over the IDs of all stopped threads of this process. | |
1367 | */ | |
1368 | static lwpid_t | |
fba45db2 | 1369 | get_process_first_stopped_thread_id (int pid, ttstate_t *thread_state) |
c906108c | 1370 | { |
c5aa993b | 1371 | int tt_status; |
c906108c | 1372 | |
a0b3c4fd JM |
1373 | tt_status = call_real_ttrace (TT_PROC_GET_FIRST_LWP_STATE, |
1374 | (pid_t) pid, | |
1375 | (lwpid_t) TT_NIL, | |
1376 | (TTRACE_ARG_TYPE) thread_state, | |
1377 | (TTRACE_ARG_TYPE) sizeof (*thread_state), | |
1378 | TT_NIL); | |
c5aa993b JM |
1379 | |
1380 | if (errno) | |
1381 | { | |
1382 | if (errno == EPROTO) | |
1383 | { | |
1384 | /* This is an error we can handle: there isn't any stopped | |
1385 | * thread. This happens when we're re-starting the application | |
1386 | * and it has only one thread. GET_NEXT handles the case of | |
1387 | * no more stopped threads well; GET_FIRST doesn't. (A ttrace | |
1388 | * "feature".) | |
1389 | */ | |
1390 | tt_status = 1; | |
1391 | errno = 0; | |
1392 | return 0; | |
1393 | } | |
1394 | else | |
1395 | perror_with_name ("ttrace"); | |
1396 | } | |
1397 | ||
1398 | if (tt_status < 0) | |
c906108c SS |
1399 | /* Failed somehow. |
1400 | */ | |
1401 | return 0; | |
1402 | ||
1403 | return thread_state->tts_lwpid; | |
1404 | } | |
c906108c | 1405 | \f |
c5aa993b | 1406 | |
c906108c SS |
1407 | /* This function returns the ID of the "next" kernel thread in a |
1408 | stopped state, or 0 if there are none. "Next" refers to the | |
1409 | thread following that of the last successful call to this | |
1410 | function or to get_process_first_stopped_thread_id, using | |
1411 | the value of thread_state returned by that call. | |
1412 | ||
1413 | This function can be used with get_process_first_stopped_thread_id | |
1414 | to iterate over the IDs of all stopped threads of this process. | |
1415 | */ | |
1416 | static lwpid_t | |
fba45db2 | 1417 | get_process_next_stopped_thread_id (int pid, ttstate_t *thread_state) |
c906108c | 1418 | { |
c5aa993b | 1419 | int tt_status; |
c906108c SS |
1420 | |
1421 | tt_status = call_real_ttrace ( | |
c5aa993b JM |
1422 | TT_PROC_GET_NEXT_LWP_STATE, |
1423 | (pid_t) pid, | |
1424 | (lwpid_t) TT_NIL, | |
1425 | (TTRACE_ARG_TYPE) thread_state, | |
1426 | (TTRACE_ARG_TYPE) sizeof (*thread_state), | |
1427 | TT_NIL); | |
c906108c SS |
1428 | if (errno) |
1429 | perror_with_name ("ttrace"); | |
1430 | ||
1431 | if (tt_status < 0) | |
1432 | /* Failed | |
1433 | */ | |
1434 | return 0; | |
1435 | ||
c5aa993b JM |
1436 | else if (tt_status == 0) |
1437 | { | |
1438 | /* End of list, no next state. Don't return the | |
1439 | * tts_lwpid, as it's a meaningless "240". | |
1440 | * | |
1441 | * This is an HPUX "feature". | |
1442 | */ | |
1443 | return 0; | |
1444 | } | |
1445 | ||
c906108c SS |
1446 | return thread_state->tts_lwpid; |
1447 | } | |
1448 | ||
1449 | /* ??rehrauer: Eventually this function perhaps should be calling | |
1450 | pid_to_thread_id. However, that function currently does nothing | |
1451 | for HP-UX. Even then, I'm not clear whether that function | |
1452 | will return a "kernel" thread ID, or a "user" thread ID. If | |
1453 | the former, we can just call it here. If the latter, we must | |
1454 | map from the "user" tid to a "kernel" tid. | |
1455 | ||
1456 | NOTE: currently not called. | |
1457 | */ | |
1458 | static lwpid_t | |
fba45db2 | 1459 | get_active_tid_of_pid (int pid) |
c906108c | 1460 | { |
c5aa993b | 1461 | ttstate_t thread_state; |
c906108c SS |
1462 | |
1463 | return get_process_first_stopped_thread_id (pid, &thread_state); | |
1464 | } | |
1465 | ||
1466 | /* This function returns 1 if tt_request is a ttrace request that | |
1467 | * operates upon all threads of a (i.e., the entire) process. | |
1468 | */ | |
1469 | int | |
fba45db2 | 1470 | is_process_ttrace_request (ttreq_t tt_request) |
c906108c SS |
1471 | { |
1472 | return IS_TTRACE_PROCREQ (tt_request); | |
1473 | } | |
c906108c | 1474 | \f |
c5aa993b | 1475 | |
c906108c SS |
1476 | /* This function translates a thread ttrace request into |
1477 | * the equivalent process request for a one-thread process. | |
1478 | */ | |
1479 | static ttreq_t | |
fba45db2 | 1480 | make_process_version (ttreq_t request) |
c906108c | 1481 | { |
c5aa993b JM |
1482 | if (!IS_TTRACE_REQ (request)) |
1483 | { | |
1484 | error ("Internal error, bad ttrace request made\n"); | |
1485 | return -1; | |
1486 | } | |
c906108c | 1487 | |
c5aa993b JM |
1488 | switch (request) |
1489 | { | |
1490 | case TT_LWP_STOP: | |
c906108c SS |
1491 | return TT_PROC_STOP; |
1492 | ||
c5aa993b | 1493 | case TT_LWP_CONTINUE: |
c906108c SS |
1494 | return TT_PROC_CONTINUE; |
1495 | ||
c5aa993b | 1496 | case TT_LWP_GET_EVENT_MASK: |
c906108c SS |
1497 | return TT_PROC_GET_EVENT_MASK; |
1498 | ||
c5aa993b | 1499 | case TT_LWP_SET_EVENT_MASK: |
c906108c SS |
1500 | return TT_PROC_SET_EVENT_MASK; |
1501 | ||
c5aa993b JM |
1502 | case TT_LWP_SINGLE: |
1503 | case TT_LWP_RUREGS: | |
1504 | case TT_LWP_WUREGS: | |
1505 | case TT_LWP_GET_STATE: | |
1506 | return -1; /* No equivalent */ | |
c906108c | 1507 | |
c5aa993b | 1508 | default: |
c906108c | 1509 | return request; |
c5aa993b | 1510 | } |
c906108c | 1511 | } |
c906108c | 1512 | \f |
c5aa993b | 1513 | |
c906108c SS |
1514 | /* This function translates the "pid" used by the rest of |
1515 | * gdb to a real pid and a tid. It then calls "call_real_ttrace" | |
1516 | * with the given arguments. | |
1517 | * | |
1518 | * In general, other parts of this module should call this | |
1519 | * function when they are dealing with external users, who only | |
1520 | * have tids to pass (but they call it "pid" for historical | |
1521 | * reasons). | |
1522 | */ | |
1523 | static int | |
fba45db2 KB |
1524 | call_ttrace (ttreq_t request, int gdb_tid, TTRACE_ARG_TYPE addr, |
1525 | TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2) | |
c906108c | 1526 | { |
c5aa993b JM |
1527 | lwpid_t real_tid; |
1528 | int real_pid; | |
1529 | ttreq_t new_request; | |
1530 | int tt_status; | |
1531 | char reason_for_failure[100]; /* Arbitrary size, should be big enough. */ | |
1532 | ||
c906108c | 1533 | #ifdef THREAD_DEBUG |
c5aa993b | 1534 | int is_interesting = 0; |
c906108c | 1535 | |
c5aa993b JM |
1536 | if (TT_LWP_RUREGS == request) |
1537 | { | |
1538 | is_interesting = 1; /* Adjust code here as desired */ | |
1539 | } | |
1540 | ||
1541 | if (is_interesting && 0 && debug_on) | |
1542 | { | |
1543 | if (!is_process_ttrace_request (request)) | |
1544 | { | |
1545 | printf ("TT: Thread request, tid is %d", gdb_tid); | |
1546 | printf ("== SINGLE at %x", addr); | |
1547 | } | |
1548 | else | |
1549 | { | |
1550 | printf ("TT: Process request, tid is %d\n", gdb_tid); | |
1551 | printf ("==! SINGLE at %x", addr); | |
1552 | } | |
1553 | } | |
c906108c SS |
1554 | #endif |
1555 | ||
1556 | /* The initial SETTRC and SET_EVENT_MASK calls (and all others | |
1557 | * which happen before any threads get set up) should go | |
1558 | * directly to "call_real_ttrace", so they don't happen here. | |
1559 | * | |
1560 | * But hardware watchpoints do a SET_EVENT_MASK, so we can't | |
1561 | * rule them out.... | |
1562 | */ | |
1563 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
1564 | if (request == TT_PROC_SETTRC && debug_on) |
1565 | printf ("Unexpected call for TT_PROC_SETTRC\n"); | |
c906108c SS |
1566 | #endif |
1567 | ||
1568 | /* Sometimes we get called with a bogus tid (e.g., if a | |
1569 | * thread has terminated, we return 0; inftarg later asks | |
1570 | * whether the thread has exited/forked/vforked). | |
1571 | */ | |
c5aa993b | 1572 | if (gdb_tid == 0) |
c906108c | 1573 | { |
c5aa993b | 1574 | errno = ESRCH; /* ttrace's response would probably be "No such process". */ |
c906108c SS |
1575 | return -1; |
1576 | } | |
1577 | ||
1578 | /* All other cases should be able to expect that there are | |
1579 | * thread records. | |
1580 | */ | |
c5aa993b JM |
1581 | if (!any_thread_records ()) |
1582 | { | |
c906108c | 1583 | #ifdef THREAD_DEBUG |
c5aa993b JM |
1584 | if (debug_on) |
1585 | warning ("No thread records for ttrace call"); | |
c906108c | 1586 | #endif |
c5aa993b | 1587 | errno = ESRCH; /* ttrace's response would be "No such process". */ |
c906108c | 1588 | return -1; |
c5aa993b | 1589 | } |
c906108c SS |
1590 | |
1591 | /* OK, now the task is to translate the incoming tid into | |
1592 | * a pid/tid pair. | |
1593 | */ | |
c5aa993b JM |
1594 | real_tid = map_from_gdb_tid (gdb_tid); |
1595 | real_pid = get_pid_for (real_tid); | |
c906108c SS |
1596 | |
1597 | /* Now check the result. "Real_pid" is NULL if our list | |
1598 | * didn't find it. We have some tricks we can play to fix | |
1599 | * this, however. | |
1600 | */ | |
c5aa993b JM |
1601 | if (0 == real_pid) |
1602 | { | |
1603 | ttstate_t thread_state; | |
c906108c SS |
1604 | |
1605 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
1606 | if (debug_on) |
1607 | printf ("No saved pid for tid %d\n", gdb_tid); | |
c906108c SS |
1608 | #endif |
1609 | ||
c5aa993b JM |
1610 | if (is_process_ttrace_request (request)) |
1611 | { | |
1612 | ||
1613 | /* Ok, we couldn't get a tid. Try to translate to | |
1614 | * the equivalent process operation. We expect this | |
1615 | * NOT to happen, so this is a desparation-type | |
1616 | * move. It can happen if there is an internal | |
1617 | * error and so no "wait()" call is ever done. | |
1618 | */ | |
1619 | new_request = make_process_version (request); | |
1620 | if (new_request == -1) | |
1621 | { | |
1622 | ||
c906108c | 1623 | #ifdef THREAD_DEBUG |
c5aa993b JM |
1624 | if (debug_on) |
1625 | printf ("...and couldn't make process version of thread operation\n"); | |
c906108c SS |
1626 | #endif |
1627 | ||
c5aa993b JM |
1628 | /* Use hacky saved pid, which won't always be correct |
1629 | * in the multi-process future. Use tid as thread, | |
1630 | * probably dooming this to failure. FIX! | |
1631 | */ | |
39f77062 | 1632 | if (! ptid_equal (saved_real_ptid, null_ptid)) |
c5aa993b | 1633 | { |
c906108c | 1634 | #ifdef THREAD_DEBUG |
c5aa993b | 1635 | if (debug_on) |
39f77062 KB |
1636 | printf ("...using saved pid %d\n", |
1637 | PIDGET (saved_real_ptid)); | |
c906108c SS |
1638 | #endif |
1639 | ||
39f77062 | 1640 | real_pid = PIDGET (saved_real_ptid); |
c5aa993b JM |
1641 | real_tid = gdb_tid; |
1642 | } | |
c906108c | 1643 | |
c5aa993b JM |
1644 | else |
1645 | error ("Unable to perform thread operation"); | |
1646 | } | |
c906108c | 1647 | |
c5aa993b JM |
1648 | else |
1649 | { | |
1650 | /* Sucessfully translated this to a process request, | |
1651 | * which needs no thread value. | |
1652 | */ | |
1653 | real_pid = gdb_tid; | |
1654 | real_tid = 0; | |
1655 | request = new_request; | |
c906108c SS |
1656 | |
1657 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
1658 | if (debug_on) |
1659 | { | |
1660 | printf ("Translated thread request to process request\n"); | |
39f77062 | 1661 | if (ptid_equal (saved_real_ptid, null_ptid)) |
c5aa993b JM |
1662 | printf ("...but there's no saved pid\n"); |
1663 | ||
1664 | else | |
1665 | { | |
39f77062 | 1666 | if (gdb_tid != PIDGET (saved_real_ptid)) |
c5aa993b | 1667 | printf ("...but have the wrong pid (%d rather than %d)\n", |
39f77062 | 1668 | gdb_tid, PIDGET (saved_real_ptid)); |
c5aa993b JM |
1669 | } |
1670 | } | |
c906108c | 1671 | #endif |
c5aa993b JM |
1672 | } /* Translated to a process request */ |
1673 | } /* Is a process request */ | |
c906108c | 1674 | |
c5aa993b JM |
1675 | else |
1676 | { | |
1677 | /* We have to have a thread. Ooops. | |
1678 | */ | |
1679 | error ("Thread request with no threads (%s)", | |
1680 | get_printable_name_of_ttrace_request (request)); | |
1681 | } | |
c906108c | 1682 | } |
c906108c SS |
1683 | |
1684 | /* Ttrace doesn't like to see tid values on process requests, | |
1685 | * even if we have the right one. | |
1686 | */ | |
c5aa993b JM |
1687 | if (is_process_ttrace_request (request)) |
1688 | { | |
c906108c | 1689 | real_tid = 0; |
c5aa993b JM |
1690 | } |
1691 | ||
c906108c | 1692 | #ifdef THREAD_DEBUG |
c5aa993b JM |
1693 | if (is_interesting && 0 && debug_on) |
1694 | { | |
1695 | printf (" now tid %d, pid %d\n", real_tid, real_pid); | |
1696 | printf (" request is %s\n", get_printable_name_of_ttrace_request (request)); | |
1697 | } | |
c906108c SS |
1698 | #endif |
1699 | ||
1700 | /* Finally, the (almost) real call. | |
1701 | */ | |
1702 | tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2); | |
1703 | ||
1704 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
1705 | if (is_interesting && debug_on) |
1706 | { | |
1707 | if (!TT_OK (tt_status, errno) | |
1708 | && !(tt_status == 0 & errno == 0)) | |
1709 | printf (" got error (errno==%d, status==%d)\n", errno, tt_status); | |
1710 | } | |
c906108c SS |
1711 | #endif |
1712 | ||
1713 | return tt_status; | |
1714 | } | |
1715 | ||
1716 | ||
1717 | /* Stop all the threads of a process. | |
c5aa993b | 1718 | |
c906108c SS |
1719 | * NOTE: use of TT_PROC_STOP can cause a thread with a real event |
1720 | * to get a TTEVT_NONE event, discarding the old event. Be | |
1721 | * very careful, and only call TT_PROC_STOP when you mean it! | |
1722 | */ | |
1723 | static void | |
fba45db2 | 1724 | stop_all_threads_of_process (pid_t real_pid) |
c906108c | 1725 | { |
c5aa993b | 1726 | int ttw_status; |
c906108c SS |
1727 | |
1728 | ttw_status = call_real_ttrace (TT_PROC_STOP, | |
c5aa993b JM |
1729 | (pid_t) real_pid, |
1730 | (lwpid_t) TT_NIL, | |
1731 | (TTRACE_ARG_TYPE) TT_NIL, | |
1732 | (TTRACE_ARG_TYPE) TT_NIL, | |
1733 | TT_NIL); | |
c906108c SS |
1734 | if (errno) |
1735 | perror_with_name ("ttrace stop of other threads"); | |
1736 | } | |
1737 | ||
1738 | ||
1739 | /* Under some circumstances, it's unsafe to attempt to stop, or even | |
1740 | query the state of, a process' threads. | |
1741 | ||
1742 | In ttrace-based HP-UX, an example is a vforking child process. The | |
1743 | vforking parent and child are somewhat fragile, w/r/t what we can do | |
1744 | what we can do to them with ttrace, until after the child exits or | |
1745 | execs, or until the parent's vfork event is delivered. Until that | |
1746 | time, we must not try to stop the process' threads, or inquire how | |
1747 | many there are, or even alter its data segments, or it typically dies | |
1748 | with a SIGILL. Sigh. | |
1749 | ||
1750 | This function returns 1 if this stopped process, and the event that | |
1751 | we're told was responsible for its current stopped state, cannot safely | |
1752 | have its threads examined. | |
c5aa993b | 1753 | */ |
c906108c | 1754 | #define CHILD_VFORKED(evt,pid) \ |
39f77062 | 1755 | (((evt) == TTEVT_VFORK) && ((pid) != PIDGET (inferior_ptid))) |
c906108c SS |
1756 | #define CHILD_URPED(evt,pid) \ |
1757 | ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid)) | |
1758 | #define PARENT_VFORKED(evt,pid) \ | |
39f77062 | 1759 | (((evt) == TTEVT_VFORK) && ((pid) == PIDGET (inferior_ptid))) |
c906108c SS |
1760 | |
1761 | static int | |
fba45db2 | 1762 | can_touch_threads_of_process (int pid, ttevents_t stopping_event) |
c906108c SS |
1763 | { |
1764 | if (CHILD_VFORKED (stopping_event, pid)) | |
1765 | { | |
1766 | vforking_child_pid = pid; | |
1767 | vfork_in_flight = 1; | |
1768 | } | |
1769 | ||
1770 | else if (vfork_in_flight && | |
c5aa993b JM |
1771 | (PARENT_VFORKED (stopping_event, pid) || |
1772 | CHILD_URPED (stopping_event, pid))) | |
c906108c SS |
1773 | { |
1774 | vfork_in_flight = 0; | |
1775 | vforking_child_pid = 0; | |
1776 | } | |
1777 | ||
c5aa993b | 1778 | return !vfork_in_flight; |
c906108c SS |
1779 | } |
1780 | ||
1781 | ||
1782 | /* If we can find an as-yet-unhandled thread state of a | |
1783 | * stopped thread of this process return 1 and set "tsp". | |
1784 | * Return 0 if we can't. | |
1785 | * | |
1786 | * If this function is used when the threads of PIS haven't | |
1787 | * been stopped, undefined behaviour is guaranteed! | |
1788 | */ | |
c5aa993b | 1789 | static int |
fba45db2 | 1790 | select_stopped_thread_of_process (int pid, ttstate_t *tsp) |
c906108c | 1791 | { |
c5aa993b JM |
1792 | lwpid_t candidate_tid, tid; |
1793 | ttstate_t candidate_tstate, tstate; | |
c906108c SS |
1794 | |
1795 | /* If we're not allowed to touch the process now, then just | |
1796 | * return the current value of *TSP. | |
1797 | * | |
1798 | * This supports "vfork". It's ok, really, to double the | |
1799 | * current event (the child EXEC, we hope!). | |
1800 | */ | |
c5aa993b | 1801 | if (!can_touch_threads_of_process (pid, tsp->tts_event)) |
c906108c SS |
1802 | return 1; |
1803 | ||
1804 | /* Decide which of (possibly more than one) events to | |
1805 | * return as the first one. We scan them all so that | |
1806 | * we always return the result of a fake-step first. | |
1807 | */ | |
1808 | candidate_tid = 0; | |
1809 | for (tid = get_process_first_stopped_thread_id (pid, &tstate); | |
1810 | tid != 0; | |
1811 | tid = get_process_next_stopped_thread_id (pid, &tstate)) | |
1812 | { | |
1813 | /* TTEVT_NONE events are uninteresting to our clients. They're | |
1814 | * an artifact of our "stop the world" model--the thread is | |
1815 | * stopped because we stopped it. | |
1816 | */ | |
c5aa993b JM |
1817 | if (tstate.tts_event == TTEVT_NONE) |
1818 | { | |
1819 | set_handled (pid, tstate.tts_lwpid); | |
1820 | } | |
c906108c SS |
1821 | |
1822 | /* Did we just single-step a single thread, without letting any | |
1823 | * of the others run? Is this an event for that thread? | |
1824 | * | |
1825 | * If so, we believe our client would prefer to see this event | |
1826 | * over any others. (Typically the client wants to just push | |
1827 | * one thread a little farther forward, and then go around | |
1828 | * checking for what all threads are doing.) | |
1829 | */ | |
1830 | else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid)) | |
c5aa993b | 1831 | { |
c906108c | 1832 | #ifdef WAIT_BUFFER_DEBUG |
c5aa993b JM |
1833 | /* It's possible here to see either a SIGTRAP (due to |
1834 | * successful completion of a step) or a SYSCALL_ENTRY | |
1835 | * (due to a step completion with active hardware | |
1836 | * watchpoints). | |
1837 | */ | |
1838 | if (debug_on) | |
1839 | printf ("Ending fake step with tid %d, state %s\n", | |
1840 | tstate.tts_lwpid, | |
1841 | get_printable_name_of_ttrace_event (tstate.tts_event)); | |
1842 | #endif | |
1843 | ||
1844 | /* Remember this one, and throw away any previous | |
1845 | * candidate. | |
1846 | */ | |
1847 | candidate_tid = tstate.tts_lwpid; | |
1848 | candidate_tstate = tstate; | |
1849 | } | |
c906108c SS |
1850 | |
1851 | #ifdef FORGET_DELETED_BPTS | |
1852 | ||
1853 | /* We can't just do this, as if we do, and then wind | |
1854 | * up the loop with no unhandled events, we need to | |
1855 | * handle that case--the appropriate reaction is to | |
1856 | * just continue, but there's no easy way to do that. | |
1857 | * | |
1858 | * Better to put this in the ttrace_wait call--if, when | |
1859 | * we fake a wait, we update our events based on the | |
1860 | * breakpoint_here_pc call and find there are no more events, | |
1861 | * then we better continue and so on. | |
1862 | * | |
1863 | * Or we could put it in the next/continue fake. | |
1864 | * But it has to go in the buffering code, not in the | |
1865 | * real go/wait code. | |
1866 | */ | |
c5aa993b JM |
1867 | else if ((TTEVT_SIGNAL == tstate.tts_event) |
1868 | && (5 == tstate.tts_u.tts_signal.tts_signo) | |
1869 | && (0 != get_raw_pc (tstate.tts_lwpid)) | |
1870 | && !breakpoint_here_p (get_raw_pc (tstate.tts_lwpid))) | |
1871 | { | |
1872 | /* | |
1873 | * If the user deleted a breakpoint while this | |
1874 | * breakpoint-hit event was buffered, we can forget | |
1875 | * it now. | |
1876 | */ | |
c906108c | 1877 | #ifdef WAIT_BUFFER_DEBUG |
c5aa993b JM |
1878 | if (debug_on) |
1879 | printf ("Forgetting deleted bp hit for thread %d\n", | |
1880 | tstate.tts_lwpid); | |
1881 | #endif | |
c906108c | 1882 | |
c5aa993b JM |
1883 | set_handled (pid, tstate.tts_lwpid); |
1884 | } | |
c906108c SS |
1885 | #endif |
1886 | ||
1887 | /* Else, is this the first "unhandled" event? If so, | |
1888 | * we believe our client wants to see it (if we don't | |
1889 | * see a fake-step later on in the scan). | |
1890 | */ | |
c5aa993b JM |
1891 | else if (!was_handled (tstate.tts_lwpid) && candidate_tid == 0) |
1892 | { | |
1893 | candidate_tid = tstate.tts_lwpid; | |
1894 | candidate_tstate = tstate; | |
1895 | } | |
c906108c SS |
1896 | |
1897 | /* This is either an event that has already been "handled", | |
1898 | * and thus we believe is uninteresting to our client, or we | |
1899 | * already have a candidate event. Ignore it... | |
1900 | */ | |
1901 | } | |
1902 | ||
1903 | /* What do we report? | |
1904 | */ | |
c5aa993b JM |
1905 | if (doing_fake_step) |
1906 | { | |
1907 | if (candidate_tid == fake_step_tid) | |
1908 | { | |
1909 | /* Fake step. | |
1910 | */ | |
1911 | tstate = candidate_tstate; | |
1912 | } | |
1913 | else | |
1914 | { | |
1915 | warning ("Internal error: fake-step failed to complete."); | |
1916 | return 0; | |
1917 | } | |
1918 | } | |
1919 | else if (candidate_tid != 0) | |
1920 | { | |
c906108c SS |
1921 | /* Found a candidate unhandled event. |
1922 | */ | |
1923 | tstate = candidate_tstate; | |
c5aa993b JM |
1924 | } |
1925 | else if (tid != 0) | |
1926 | { | |
1927 | warning ("Internal error in call of ttrace_wait."); | |
c906108c | 1928 | return 0; |
c5aa993b JM |
1929 | } |
1930 | else | |
1931 | { | |
c906108c SS |
1932 | warning ("Internal error: no unhandled thread event to select"); |
1933 | return 0; | |
c5aa993b | 1934 | } |
c906108c SS |
1935 | |
1936 | copy_ttstate_t (tsp, &tstate); | |
1937 | return 1; | |
c5aa993b | 1938 | } /* End of select_stopped_thread_of_process */ |
c906108c SS |
1939 | |
1940 | #ifdef PARANOIA | |
1941 | /* Check our internal thread data against the real thing. | |
1942 | */ | |
1943 | static void | |
fba45db2 | 1944 | check_thread_consistency (pid_t real_pid) |
c906108c | 1945 | { |
c5aa993b JM |
1946 | int tid; /* really lwpid_t */ |
1947 | ttstate_t tstate; | |
1948 | thread_info *p; | |
c906108c | 1949 | |
c5aa993b JM |
1950 | /* Spin down the O/S list of threads, checking that they |
1951 | * match what we've got. | |
1952 | */ | |
1953 | for (tid = get_process_first_stopped_thread_id (real_pid, &tstate); | |
1954 | tid != 0; | |
1955 | tid = get_process_next_stopped_thread_id (real_pid, &tstate)) | |
1956 | { | |
c906108c | 1957 | |
c5aa993b | 1958 | p = find_thread_info (tid); |
c906108c | 1959 | |
c5aa993b JM |
1960 | if (NULL == p) |
1961 | { | |
1962 | warning ("No internal thread data for thread %d.", tid); | |
1963 | continue; | |
1964 | } | |
1965 | ||
1966 | if (!p->seen) | |
1967 | { | |
1968 | warning ("Inconsistent internal thread data for thread %d.", tid); | |
1969 | } | |
1970 | ||
1971 | if (p->terminated) | |
1972 | { | |
1973 | warning ("Thread %d is not terminated, internal error.", tid); | |
1974 | continue; | |
1975 | } | |
c906108c SS |
1976 | |
1977 | ||
1978 | #define TT_COMPARE( fld ) \ | |
1979 | tstate.fld != p->last_stop_state.fld | |
c5aa993b JM |
1980 | |
1981 | if (p->have_state) | |
1982 | { | |
1983 | if (TT_COMPARE (tts_pid) | |
1984 | || TT_COMPARE (tts_lwpid) | |
1985 | || TT_COMPARE (tts_user_tid) | |
1986 | || TT_COMPARE (tts_event) | |
1987 | || TT_COMPARE (tts_flags) | |
1988 | || TT_COMPARE (tts_scno) | |
1989 | || TT_COMPARE (tts_scnargs)) | |
1990 | { | |
1991 | warning ("Internal thread data for thread %d is wrong.", tid); | |
1992 | continue; | |
1993 | } | |
1994 | } | |
c906108c SS |
1995 | } |
1996 | } | |
c5aa993b | 1997 | #endif /* PARANOIA */ |
c906108c | 1998 | \f |
c5aa993b | 1999 | |
c906108c SS |
2000 | /* This function wraps calls to "call_real_ttrace_wait" so |
2001 | * that a actual wait is only done when all pending events | |
2002 | * have been reported. | |
2003 | * | |
2004 | * Note that typically it is called with a pid of "0", i.e. | |
2005 | * the "don't care" value. | |
2006 | * | |
2007 | * Return value is the status of the pseudo wait. | |
2008 | */ | |
2009 | static int | |
fba45db2 | 2010 | call_ttrace_wait (int pid, ttwopt_t option, ttstate_t *tsp, size_t tsp_size) |
c906108c SS |
2011 | { |
2012 | /* This holds the actual, for-real, true process ID. | |
2013 | */ | |
2014 | static int real_pid; | |
2015 | ||
2016 | /* As an argument to ttrace_wait, zero pid | |
2017 | * means "Any process", and zero tid means | |
2018 | * "Any thread of the specified process". | |
2019 | */ | |
c5aa993b JM |
2020 | int wait_pid = 0; |
2021 | lwpid_t wait_tid = 0; | |
2022 | lwpid_t real_tid; | |
c906108c | 2023 | |
c5aa993b | 2024 | int ttw_status = 0; /* To be returned */ |
c906108c | 2025 | |
c5aa993b | 2026 | thread_info *tinfo = NULL; |
c906108c | 2027 | |
c5aa993b JM |
2028 | if (pid != 0) |
2029 | { | |
c906108c SS |
2030 | /* Unexpected case. |
2031 | */ | |
2032 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
2033 | if (debug_on) |
2034 | printf ("TW: Pid to wait on is %d\n", pid); | |
c906108c SS |
2035 | #endif |
2036 | ||
c5aa993b JM |
2037 | if (!any_thread_records ()) |
2038 | error ("No thread records for ttrace call w. specific pid"); | |
c906108c SS |
2039 | |
2040 | /* OK, now the task is to translate the incoming tid into | |
2041 | * a pid/tid pair. | |
2042 | */ | |
c5aa993b JM |
2043 | real_tid = map_from_gdb_tid (pid); |
2044 | real_pid = get_pid_for (real_tid); | |
c906108c | 2045 | #ifdef THREAD_DEBUG |
c5aa993b JM |
2046 | if (debug_on) |
2047 | printf ("==TW: real pid %d, real tid %d\n", real_pid, real_tid); | |
c906108c | 2048 | #endif |
c5aa993b | 2049 | } |
c906108c SS |
2050 | |
2051 | ||
2052 | /* Sanity checks and set-up. | |
2053 | * Process State | |
2054 | * | |
2055 | * Stopped Running Fake-step (v)Fork | |
2056 | * \________________________________________ | |
2057 | * | | |
2058 | * No buffered events | error wait wait wait | |
2059 | * | | |
2060 | * Buffered events | debuffer error wait debuffer (?) | |
2061 | * | |
2062 | */ | |
c5aa993b JM |
2063 | if (more_events_left == 0) |
2064 | { | |
2065 | ||
2066 | if (process_state == RUNNING) | |
2067 | { | |
2068 | /* OK--normal call of ttrace_wait with no buffered events. | |
2069 | */ | |
2070 | ; | |
2071 | } | |
2072 | else if (process_state == FAKE_STEPPING) | |
2073 | { | |
2074 | /* Ok--call of ttrace_wait to support | |
2075 | * fake stepping with no buffered events. | |
2076 | * | |
2077 | * But we better be fake-stepping! | |
2078 | */ | |
2079 | if (!doing_fake_step) | |
2080 | { | |
2081 | warning ("Inconsistent thread state."); | |
2082 | } | |
2083 | } | |
2084 | else if ((process_state == FORKING) | |
2085 | || (process_state == VFORKING)) | |
2086 | { | |
2087 | /* Ok--there are two processes, so waiting | |
2088 | * for the second while the first is stopped | |
2089 | * is ok. Handled bits stay as they were. | |
2090 | */ | |
2091 | ; | |
2092 | } | |
2093 | else if (process_state == STOPPED) | |
2094 | { | |
2095 | warning ("Process not running at wait call."); | |
2096 | } | |
c906108c | 2097 | else |
c5aa993b JM |
2098 | /* No known state. |
2099 | */ | |
2100 | warning ("Inconsistent process state."); | |
2101 | } | |
2102 | ||
2103 | else | |
2104 | { | |
c906108c SS |
2105 | /* More events left |
2106 | */ | |
c5aa993b JM |
2107 | if (process_state == STOPPED) |
2108 | { | |
2109 | /* OK--buffered events being unbuffered. | |
2110 | */ | |
2111 | ; | |
2112 | } | |
2113 | else if (process_state == RUNNING) | |
2114 | { | |
2115 | /* An error--shouldn't have buffered events | |
2116 | * when running. | |
2117 | */ | |
2118 | warning ("Trying to continue with buffered events:"); | |
2119 | } | |
2120 | else if (process_state == FAKE_STEPPING) | |
2121 | { | |
2122 | /* | |
2123 | * Better be fake-stepping! | |
2124 | */ | |
2125 | if (!doing_fake_step) | |
2126 | { | |
2127 | warning ("Losing buffered thread events!\n"); | |
2128 | } | |
2129 | } | |
2130 | else if ((process_state == FORKING) | |
2131 | || (process_state == VFORKING)) | |
2132 | { | |
2133 | /* Ok--there are two processes, so waiting | |
2134 | * for the second while the first is stopped | |
2135 | * is ok. Handled bits stay as they were. | |
2136 | */ | |
2137 | ; | |
2138 | } | |
c906108c | 2139 | else |
c5aa993b JM |
2140 | warning ("Process in unknown state with buffered events."); |
2141 | } | |
c906108c SS |
2142 | |
2143 | /* Sometimes we have to wait for a particular thread | |
2144 | * (if we're stepping over a bpt). In that case, we | |
2145 | * _know_ it's going to complete the single-step we | |
2146 | * asked for (because we're only doing the step under | |
2147 | * certain very well-understood circumstances), so it | |
2148 | * can't block. | |
2149 | */ | |
c5aa993b JM |
2150 | if (doing_fake_step) |
2151 | { | |
c906108c | 2152 | wait_tid = fake_step_tid; |
c5aa993b | 2153 | wait_pid = get_pid_for (fake_step_tid); |
c906108c SS |
2154 | |
2155 | #ifdef WAIT_BUFFER_DEBUG | |
c5aa993b JM |
2156 | if (debug_on) |
2157 | printf ("Doing a wait after a fake-step for %d, pid %d\n", | |
2158 | wait_tid, wait_pid); | |
c906108c | 2159 | #endif |
c5aa993b | 2160 | } |
c906108c | 2161 | |
c5aa993b JM |
2162 | if (more_events_left == 0 /* No buffered events, need real ones. */ |
2163 | || process_state != STOPPED) | |
2164 | { | |
c906108c SS |
2165 | /* If there are no buffered events, and so we need |
2166 | * real ones, or if we are FORKING, VFORKING, | |
2167 | * FAKE_STEPPING or RUNNING, and thus have to do | |
2168 | * a real wait, then do a real wait. | |
2169 | */ | |
2170 | ||
2171 | #ifdef WAIT_BUFFER_DEBUG | |
2172 | /* Normal case... */ | |
c5aa993b JM |
2173 | if (debug_on) |
2174 | printf ("TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid); | |
c906108c SS |
2175 | #endif |
2176 | ||
2177 | /* The actual wait call. | |
2178 | */ | |
c5aa993b | 2179 | ttw_status = call_real_ttrace_wait (wait_pid, wait_tid, option, tsp, tsp_size); |
c906108c SS |
2180 | |
2181 | /* Note that the routines we'll call will be using "call_real_ttrace", | |
2182 | * not "call_ttrace", and thus need the real pid rather than the pseudo-tid | |
2183 | * the rest of the world uses (which is actually the tid). | |
2184 | */ | |
2185 | real_pid = tsp->tts_pid; | |
2186 | ||
2187 | /* For most events: Stop the world! | |
c5aa993b | 2188 | |
c906108c SS |
2189 | * It's sometimes not safe to stop all threads of a process. |
2190 | * Sometimes it's not even safe to ask for the thread state | |
2191 | * of a process! | |
2192 | */ | |
2193 | if (can_touch_threads_of_process (real_pid, tsp->tts_event)) | |
c5aa993b JM |
2194 | { |
2195 | /* If we're really only stepping a single thread, then don't | |
2196 | * try to stop all the others -- we only do this single-stepping | |
2197 | * business when all others were already stopped...and the stop | |
2198 | * would mess up other threads' events. | |
2199 | * | |
2200 | * Similiarly, if there are other threads with events, | |
2201 | * don't do the stop. | |
2202 | */ | |
2203 | if (!doing_fake_step) | |
2204 | { | |
2205 | if (more_events_left > 0) | |
2206 | warning ("Internal error in stopping process"); | |
2207 | ||
2208 | stop_all_threads_of_process (real_pid); | |
2209 | ||
2210 | /* At this point, we could scan and update_thread_list(), | |
2211 | * and only use the local list for the rest of the | |
2212 | * module! We'd get rid of the scans in the various | |
2213 | * continue routines (adding one in attach). It'd | |
2214 | * be great--UPGRADE ME! | |
2215 | */ | |
2216 | } | |
2217 | } | |
2218 | ||
c906108c | 2219 | #ifdef PARANOIA |
c5aa993b JM |
2220 | else if (debug_on) |
2221 | { | |
2222 | if (more_events_left > 0) | |
2223 | printf ("== Can't stop process; more events!\n"); | |
2224 | else | |
2225 | printf ("== Can't stop process!\n"); | |
2226 | } | |
c906108c SS |
2227 | #endif |
2228 | ||
c5aa993b | 2229 | process_state = STOPPED; |
c906108c SS |
2230 | |
2231 | #ifdef WAIT_BUFFER_DEBUG | |
c5aa993b JM |
2232 | if (debug_on) |
2233 | printf ("Process set to STOPPED\n"); | |
c906108c | 2234 | #endif |
c5aa993b JM |
2235 | } |
2236 | ||
2237 | else | |
2238 | { | |
c906108c SS |
2239 | /* Fake a call to ttrace_wait. The process must be |
2240 | * STOPPED, as we aren't going to do any wait. | |
2241 | */ | |
2242 | #ifdef WAIT_BUFFER_DEBUG | |
c5aa993b JM |
2243 | if (debug_on) |
2244 | printf ("TW: fake it\n"); | |
c906108c SS |
2245 | #endif |
2246 | ||
c5aa993b JM |
2247 | if (process_state != STOPPED) |
2248 | { | |
2249 | warning ("Process not stopped at wait call, in state '%s'.\n", | |
2250 | get_printable_name_of_process_state (process_state)); | |
2251 | } | |
2252 | ||
2253 | if (doing_fake_step) | |
2254 | error ("Internal error in stepping over breakpoint"); | |
c906108c | 2255 | |
c5aa993b JM |
2256 | ttw_status = 0; /* Faking it is always successful! */ |
2257 | } /* End of fake or not? if */ | |
c906108c SS |
2258 | |
2259 | /* Pick an event to pass to our caller. Be paranoid. | |
2260 | */ | |
c5aa993b JM |
2261 | if (!select_stopped_thread_of_process (real_pid, tsp)) |
2262 | warning ("Can't find event, using previous event."); | |
2263 | ||
2264 | else if (tsp->tts_event == TTEVT_NONE) | |
2265 | warning ("Internal error: no thread has a real event."); | |
c906108c | 2266 | |
c5aa993b JM |
2267 | else if (doing_fake_step) |
2268 | { | |
2269 | if (fake_step_tid != tsp->tts_lwpid) | |
2270 | warning ("Internal error in stepping over breakpoint."); | |
c906108c | 2271 | |
c906108c SS |
2272 | /* This wait clears the (current) fake-step if there was one. |
2273 | */ | |
2274 | doing_fake_step = 0; | |
c5aa993b JM |
2275 | fake_step_tid = 0; |
2276 | } | |
c906108c SS |
2277 | |
2278 | /* We now have a correct tsp and ttw_status for the thread | |
2279 | * which we want to report. So it's "handled"! This call | |
2280 | * will add it to our list if it's not there already. | |
2281 | */ | |
c5aa993b | 2282 | set_handled (real_pid, tsp->tts_lwpid); |
c906108c SS |
2283 | |
2284 | /* Save a copy of the ttrace state of this thread, in our local | |
2285 | thread descriptor. | |
2286 | ||
2287 | This caches the state. The implementation of queries like | |
47932f85 | 2288 | hpux_has_execd can then use this cached state, rather than |
c906108c SS |
2289 | be forced to make an explicit ttrace call to get it. |
2290 | ||
2291 | (Guard against the condition that this is the first time we've | |
2292 | waited on, i.e., seen this thread, and so haven't yet entered | |
2293 | it into our list of threads.) | |
2294 | */ | |
2295 | tinfo = find_thread_info (tsp->tts_lwpid); | |
c5aa993b JM |
2296 | if (tinfo != NULL) |
2297 | { | |
2298 | copy_ttstate_t (&tinfo->last_stop_state, tsp); | |
2299 | tinfo->have_state = 1; | |
2300 | } | |
2301 | ||
c906108c | 2302 | return ttw_status; |
c5aa993b | 2303 | } /* call_ttrace_wait */ |
c906108c SS |
2304 | |
2305 | #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL) | |
2306 | int | |
fba45db2 | 2307 | child_reported_exec_events_per_exec_call (void) |
c906108c | 2308 | { |
c5aa993b | 2309 | return 1; /* ttrace reports the event once per call. */ |
c906108c SS |
2310 | } |
2311 | #endif | |
c5aa993b | 2312 | \f |
c906108c SS |
2313 | |
2314 | ||
c906108c SS |
2315 | /* Our implementation of hardware watchpoints involves making memory |
2316 | pages write-protected. We must remember a page's original permissions, | |
2317 | and we must also know when it is appropriate to restore a page's | |
2318 | permissions to its original state. | |
2319 | ||
2320 | We use a "dictionary" of hardware-watched pages to do this. Each | |
2321 | hardware-watched page is recorded in the dictionary. Each page's | |
2322 | dictionary entry contains the original permissions and a reference | |
2323 | count. Pages are hashed into the dictionary by their start address. | |
2324 | ||
2325 | When hardware watchpoint is set on page X for the first time, page X | |
2326 | is added to the dictionary with a reference count of 1. If other | |
2327 | hardware watchpoints are subsequently set on page X, its reference | |
2328 | count is incremented. When hardware watchpoints are removed from | |
2329 | page X, its reference count is decremented. If a page's reference | |
2330 | count drops to 0, it's permissions are restored and the page's entry | |
2331 | is thrown out of the dictionary. | |
c5aa993b JM |
2332 | */ |
2333 | typedef struct memory_page | |
2334 | { | |
2335 | CORE_ADDR page_start; | |
2336 | int reference_count; | |
2337 | int original_permissions; | |
2338 | struct memory_page *next; | |
2339 | struct memory_page *previous; | |
2340 | } | |
2341 | memory_page_t; | |
c906108c SS |
2342 | |
2343 | #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128 | |
2344 | ||
c5aa993b JM |
2345 | static struct |
2346 | { | |
2347 | LONGEST page_count; | |
2348 | int page_size; | |
2349 | int page_protections_allowed; | |
2350 | /* These are just the heads of chains of actual page descriptors. */ | |
2351 | memory_page_t buckets[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT]; | |
2352 | } | |
2353 | memory_page_dictionary; | |
c906108c SS |
2354 | |
2355 | ||
2356 | static void | |
fba45db2 | 2357 | require_memory_page_dictionary (void) |
c906108c | 2358 | { |
c5aa993b | 2359 | int i; |
c906108c SS |
2360 | |
2361 | /* Is the memory page dictionary ready for use? If so, we're done. */ | |
2362 | if (memory_page_dictionary.page_count >= (LONGEST) 0) | |
2363 | return; | |
2364 | ||
2365 | /* Else, initialize it. */ | |
2366 | memory_page_dictionary.page_count = (LONGEST) 0; | |
2367 | ||
c5aa993b | 2368 | for (i = 0; i < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++) |
c906108c SS |
2369 | { |
2370 | memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0; | |
2371 | memory_page_dictionary.buckets[i].reference_count = 0; | |
2372 | memory_page_dictionary.buckets[i].next = NULL; | |
2373 | memory_page_dictionary.buckets[i].previous = NULL; | |
2374 | } | |
2375 | } | |
2376 | ||
2377 | ||
2378 | static void | |
fba45db2 | 2379 | retire_memory_page_dictionary (void) |
c906108c | 2380 | { |
c5aa993b | 2381 | memory_page_dictionary.page_count = (LONGEST) - 1; |
c906108c SS |
2382 | } |
2383 | ||
2384 | ||
2385 | /* Write-protect the memory page that starts at this address. | |
2386 | ||
2387 | Returns the original permissions of the page. | |
2388 | */ | |
2389 | static int | |
fba45db2 | 2390 | write_protect_page (int pid, CORE_ADDR page_start) |
c906108c | 2391 | { |
c5aa993b JM |
2392 | int tt_status; |
2393 | int original_permissions; | |
2394 | int new_permissions; | |
c906108c SS |
2395 | |
2396 | tt_status = call_ttrace (TT_PROC_GET_MPROTECT, | |
c5aa993b JM |
2397 | pid, |
2398 | (TTRACE_ARG_TYPE) page_start, | |
2399 | TT_NIL, | |
2400 | (TTRACE_ARG_TYPE) & original_permissions); | |
c906108c SS |
2401 | if (errno || (tt_status < 0)) |
2402 | { | |
c5aa993b | 2403 | return 0; /* What else can we do? */ |
c906108c SS |
2404 | } |
2405 | ||
2406 | /* We'll also write-protect the page now, if that's allowed. */ | |
2407 | if (memory_page_dictionary.page_protections_allowed) | |
2408 | { | |
2409 | new_permissions = original_permissions & ~PROT_WRITE; | |
2410 | tt_status = call_ttrace (TT_PROC_SET_MPROTECT, | |
c5aa993b JM |
2411 | pid, |
2412 | (TTRACE_ARG_TYPE) page_start, | |
2413 | (TTRACE_ARG_TYPE) memory_page_dictionary.page_size, | |
2414 | (TTRACE_ARG_TYPE) new_permissions); | |
c906108c | 2415 | if (errno || (tt_status < 0)) |
c5aa993b JM |
2416 | { |
2417 | return 0; /* What else can we do? */ | |
2418 | } | |
c906108c SS |
2419 | } |
2420 | ||
2421 | return original_permissions; | |
2422 | } | |
2423 | ||
2424 | ||
2425 | /* Unwrite-protect the memory page that starts at this address, restoring | |
2426 | (what we must assume are) its original permissions. | |
c5aa993b | 2427 | */ |
c906108c | 2428 | static void |
fba45db2 | 2429 | unwrite_protect_page (int pid, CORE_ADDR page_start, int original_permissions) |
c906108c | 2430 | { |
c5aa993b | 2431 | int tt_status; |
c906108c SS |
2432 | |
2433 | tt_status = call_ttrace (TT_PROC_SET_MPROTECT, | |
c5aa993b JM |
2434 | pid, |
2435 | (TTRACE_ARG_TYPE) page_start, | |
2436 | (TTRACE_ARG_TYPE) memory_page_dictionary.page_size, | |
2437 | (TTRACE_ARG_TYPE) original_permissions); | |
c906108c SS |
2438 | if (errno || (tt_status < 0)) |
2439 | { | |
c5aa993b | 2440 | return; /* What else can we do? */ |
c906108c SS |
2441 | } |
2442 | } | |
2443 | ||
2444 | ||
2445 | /* Memory page-protections are used to implement "hardware" watchpoints | |
2446 | on HP-UX. | |
2447 | ||
2448 | For every memory page that is currently being watched (i.e., that | |
2449 | presently should be write-protected), write-protect it. | |
c5aa993b | 2450 | */ |
c906108c | 2451 | void |
fba45db2 | 2452 | hppa_enable_page_protection_events (int pid) |
c906108c | 2453 | { |
c5aa993b | 2454 | int bucket; |
c906108c SS |
2455 | |
2456 | memory_page_dictionary.page_protections_allowed = 1; | |
2457 | ||
c5aa993b | 2458 | for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++) |
c906108c | 2459 | { |
c5aa993b | 2460 | memory_page_t *page; |
c906108c SS |
2461 | |
2462 | page = memory_page_dictionary.buckets[bucket].next; | |
2463 | while (page != NULL) | |
c5aa993b JM |
2464 | { |
2465 | page->original_permissions = write_protect_page (pid, page->page_start); | |
2466 | page = page->next; | |
2467 | } | |
c906108c SS |
2468 | } |
2469 | } | |
2470 | ||
2471 | ||
2472 | /* Memory page-protections are used to implement "hardware" watchpoints | |
2473 | on HP-UX. | |
2474 | ||
2475 | For every memory page that is currently being watched (i.e., that | |
2476 | presently is or should be write-protected), un-write-protect it. | |
c5aa993b | 2477 | */ |
c906108c | 2478 | void |
fba45db2 | 2479 | hppa_disable_page_protection_events (int pid) |
c906108c | 2480 | { |
c5aa993b | 2481 | int bucket; |
c906108c | 2482 | |
c5aa993b | 2483 | for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++) |
c906108c | 2484 | { |
c5aa993b | 2485 | memory_page_t *page; |
c906108c SS |
2486 | |
2487 | page = memory_page_dictionary.buckets[bucket].next; | |
2488 | while (page != NULL) | |
c5aa993b JM |
2489 | { |
2490 | unwrite_protect_page (pid, page->page_start, page->original_permissions); | |
2491 | page = page->next; | |
2492 | } | |
c906108c SS |
2493 | } |
2494 | ||
2495 | memory_page_dictionary.page_protections_allowed = 0; | |
2496 | } | |
2497 | ||
2498 | /* Count the number of outstanding events. At this | |
2499 | * point, we have selected one thread and its event | |
2500 | * as the one to be "reported" upwards to core gdb. | |
2501 | * That thread is already marked as "handled". | |
2502 | * | |
2503 | * Note: we could just scan our own thread list. FIXME! | |
2504 | */ | |
2505 | static int | |
fba45db2 | 2506 | count_unhandled_events (int real_pid, lwpid_t real_tid) |
c906108c | 2507 | { |
c5aa993b JM |
2508 | ttstate_t tstate; |
2509 | lwpid_t ttid; | |
2510 | int events_left; | |
2511 | ||
c906108c SS |
2512 | /* Ok, find out how many threads have real events to report. |
2513 | */ | |
2514 | events_left = 0; | |
c5aa993b | 2515 | ttid = get_process_first_stopped_thread_id (real_pid, &tstate); |
c906108c SS |
2516 | |
2517 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
2518 | if (debug_on) |
2519 | { | |
2520 | if (ttid == 0) | |
2521 | printf ("Process %d has no threads\n", real_pid); | |
c906108c | 2522 | else |
c5aa993b JM |
2523 | printf ("Process %d has these threads:\n", real_pid); |
2524 | } | |
c906108c SS |
2525 | #endif |
2526 | ||
c5aa993b JM |
2527 | while (ttid > 0) |
2528 | { | |
2529 | if (tstate.tts_event != TTEVT_NONE | |
2530 | && !was_handled (ttid)) | |
2531 | { | |
2532 | /* TTEVT_NONE implies we just stopped it ourselves | |
2533 | * because we're the stop-the-world guys, so it's | |
2534 | * not an event from our point of view. | |
2535 | * | |
2536 | * If "was_handled" is true, this is an event we | |
2537 | * already handled, so don't count it. | |
2538 | * | |
2539 | * Note that we don't count the thread with the | |
2540 | * currently-reported event, as it's already marked | |
2541 | * as handled. | |
2542 | */ | |
2543 | events_left++; | |
2544 | } | |
2545 | ||
c906108c | 2546 | #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG ) |
c5aa993b JM |
2547 | if (debug_on) |
2548 | { | |
2549 | if (ttid == real_tid) | |
2550 | printf ("*"); /* Thread we're reporting */ | |
2551 | else | |
2552 | printf (" "); | |
2553 | ||
2554 | if (tstate.tts_event != TTEVT_NONE) | |
2555 | printf ("+"); /* Thread with a real event */ | |
2556 | else | |
2557 | printf (" "); | |
2558 | ||
2559 | if (was_handled (ttid)) | |
2560 | printf ("h"); /* Thread has been handled */ | |
2561 | else | |
2562 | printf (" "); | |
2563 | ||
2564 | printf (" %d, with event %s", ttid, | |
2565 | get_printable_name_of_ttrace_event (tstate.tts_event)); | |
2566 | ||
2567 | if (tstate.tts_event == TTEVT_SIGNAL | |
2568 | && 5 == tstate.tts_u.tts_signal.tts_signo) | |
2569 | { | |
2570 | CORE_ADDR pc_val; | |
c906108c | 2571 | |
c5aa993b JM |
2572 | pc_val = get_raw_pc (ttid); |
2573 | ||
2574 | if (pc_val > 0) | |
2575 | printf (" breakpoint at 0x%x\n", pc_val); | |
2576 | else | |
2577 | printf (" bpt, can't fetch pc.\n"); | |
2578 | } | |
2579 | else | |
2580 | printf ("\n"); | |
2581 | } | |
c906108c SS |
2582 | #endif |
2583 | ||
2584 | ttid = get_process_next_stopped_thread_id (real_pid, &tstate); | |
c5aa993b | 2585 | } |
c906108c SS |
2586 | |
2587 | #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG ) | |
c5aa993b JM |
2588 | if (debug_on) |
2589 | if (events_left > 0) | |
2590 | printf ("There are thus %d pending events\n", events_left); | |
c906108c SS |
2591 | #endif |
2592 | ||
2593 | return events_left; | |
2594 | } | |
2595 | ||
2596 | /* This function is provided as a sop to clients that are calling | |
2597 | * ptrace_wait to wait for a process to stop. (see the | |
2598 | * implementation of child_wait.) Return value is the pid for | |
2599 | * the event that ended the wait. | |
2600 | * | |
2601 | * Note: used by core gdb and so uses the pseudo-pid (really tid). | |
2602 | */ | |
de6ee558 | 2603 | int |
39f77062 | 2604 | ptrace_wait (ptid_t ptid, int *status) |
c906108c | 2605 | { |
c5aa993b JM |
2606 | ttstate_t tsp; |
2607 | int ttwait_return; | |
2608 | int real_pid; | |
2609 | ttstate_t state; | |
2610 | lwpid_t real_tid; | |
2611 | int return_pid; | |
c906108c SS |
2612 | |
2613 | /* The ptrace implementation of this also ignores pid. | |
2614 | */ | |
2615 | *status = 0; | |
2616 | ||
c5aa993b | 2617 | ttwait_return = call_ttrace_wait (0, TTRACE_WAITOK, &tsp, sizeof (tsp)); |
c906108c SS |
2618 | if (ttwait_return < 0) |
2619 | { | |
2620 | /* ??rehrauer: It appears that if our inferior exits and we | |
2621 | haven't asked for exit events, that we're not getting any | |
2622 | indication save a negative return from ttrace_wait and an | |
2623 | errno set to ESRCH? | |
c5aa993b | 2624 | */ |
c906108c | 2625 | if (errno == ESRCH) |
c5aa993b JM |
2626 | { |
2627 | *status = 0; /* WIFEXITED */ | |
de6ee558 | 2628 | return PIDGET (inferior_ptid); |
c5aa993b | 2629 | } |
c906108c | 2630 | |
c5aa993b JM |
2631 | warning ("Call of ttrace_wait returned with errno %d.", |
2632 | errno); | |
c906108c | 2633 | *status = ttwait_return; |
de6ee558 | 2634 | return PIDGET (inferior_ptid); |
c906108c SS |
2635 | } |
2636 | ||
2637 | real_pid = tsp.tts_pid; | |
2638 | real_tid = tsp.tts_lwpid; | |
2639 | ||
2640 | /* One complication is that the "tts_event" structure has | |
2641 | * a set of flags, and more than one can be set. So we | |
2642 | * either have to force an order (as we do here), or handle | |
2643 | * more than one flag at a time. | |
2644 | */ | |
c5aa993b JM |
2645 | if (tsp.tts_event & TTEVT_LWP_CREATE) |
2646 | { | |
2647 | ||
2648 | /* Unlike what you might expect, this event is reported in | |
2649 | * the _creating_ thread, and the _created_ thread (whose tid | |
2650 | * we have) is still running. So we have to stop it. This | |
2651 | * has already been done in "call_ttrace_wait", but should we | |
2652 | * ever abandon the "stop-the-world" model, here's the command | |
2653 | * to use: | |
2654 | * | |
2655 | * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL ); | |
2656 | * | |
2657 | * Note that this would depend on being called _after_ "add_tthread" | |
2658 | * below for the tid-to-pid translation to be done in "call_ttrace". | |
2659 | */ | |
c906108c SS |
2660 | |
2661 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
2662 | if (debug_on) |
2663 | printf ("New thread: pid %d, tid %d, creator tid %d\n", | |
2664 | real_pid, tsp.tts_u.tts_thread.tts_target_lwpid, | |
2665 | real_tid); | |
c906108c SS |
2666 | #endif |
2667 | ||
c5aa993b JM |
2668 | /* Now we have to return the tid of the created thread, not |
2669 | * the creating thread, or "wait_for_inferior" won't know we | |
2670 | * have a new "process" (thread). Plus we should record it | |
2671 | * right, too. | |
2672 | */ | |
c906108c SS |
2673 | real_tid = tsp.tts_u.tts_thread.tts_target_lwpid; |
2674 | ||
c5aa993b JM |
2675 | add_tthread (real_pid, real_tid); |
2676 | } | |
c906108c | 2677 | |
c5aa993b JM |
2678 | else if ((tsp.tts_event & TTEVT_LWP_TERMINATE) |
2679 | || (tsp.tts_event & TTEVT_LWP_EXIT)) | |
2680 | { | |
c906108c SS |
2681 | |
2682 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
2683 | if (debug_on) |
2684 | printf ("Thread dies: %d\n", real_tid); | |
c906108c SS |
2685 | #endif |
2686 | ||
c5aa993b JM |
2687 | del_tthread (real_tid); |
2688 | } | |
c906108c | 2689 | |
c5aa993b JM |
2690 | else if (tsp.tts_event & TTEVT_EXEC) |
2691 | { | |
c906108c | 2692 | |
c5aa993b JM |
2693 | #ifdef THREAD_DEBUG |
2694 | if (debug_on) | |
2695 | printf ("Pid %d has zero'th thread %d; inferior pid is %d\n", | |
39f77062 | 2696 | real_pid, real_tid, PIDGET (inferior_ptid)); |
c906108c SS |
2697 | #endif |
2698 | ||
c5aa993b JM |
2699 | add_tthread (real_pid, real_tid); |
2700 | } | |
c906108c SS |
2701 | |
2702 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
2703 | else if (debug_on) |
2704 | { | |
2705 | printf ("Process-level event %s, using tid %d\n", | |
2706 | get_printable_name_of_ttrace_event (tsp.tts_event), | |
2707 | real_tid); | |
2708 | ||
2709 | /* OK to do this, as "add_tthread" won't add | |
2710 | * duplicate entries. Also OK not to do it, | |
2711 | * as this event isn't one which can change the | |
2712 | * thread state. | |
2713 | */ | |
2714 | add_tthread (real_pid, real_tid); | |
2715 | } | |
c906108c SS |
2716 | #endif |
2717 | ||
2718 | ||
2719 | /* How many events are left to report later? | |
2720 | * In a non-stop-the-world model, this isn't needed. | |
2721 | * | |
2722 | * Note that it's not always safe to query the thread state of a process, | |
2723 | * which is what count_unhandled_events does. (If unsafe, we're left with | |
2724 | * no other resort than to assume that no more events remain...) | |
2725 | */ | |
2726 | if (can_touch_threads_of_process (real_pid, tsp.tts_event)) | |
c5aa993b JM |
2727 | more_events_left = count_unhandled_events (real_pid, real_tid); |
2728 | ||
2729 | else | |
2730 | { | |
2731 | if (more_events_left > 0) | |
2732 | warning ("Vfork or fork causing loss of %d buffered events.", | |
2733 | more_events_left); | |
2734 | ||
c906108c | 2735 | more_events_left = 0; |
c5aa993b | 2736 | } |
c906108c SS |
2737 | |
2738 | /* Attempt to translate the ttrace_wait-returned status into the | |
2739 | ptrace equivalent. | |
2740 | ||
2741 | ??rehrauer: This is somewhat fragile. We really ought to rewrite | |
2742 | clients that expect to pick apart a ptrace wait status, to use | |
2743 | something a little more abstract. | |
c5aa993b JM |
2744 | */ |
2745 | if ((tsp.tts_event & TTEVT_EXEC) | |
c906108c SS |
2746 | || (tsp.tts_event & TTEVT_FORK) |
2747 | || (tsp.tts_event & TTEVT_VFORK)) | |
2748 | { | |
2749 | /* Forks come in pairs (parent and child), so core gdb | |
2750 | * will do two waits. Be ready to notice this. | |
2751 | */ | |
2752 | if (tsp.tts_event & TTEVT_FORK) | |
c5aa993b JM |
2753 | { |
2754 | process_state = FORKING; | |
2755 | ||
c906108c | 2756 | #ifdef WAIT_BUFFER_DEBUG |
c5aa993b JM |
2757 | if (debug_on) |
2758 | printf ("Process set to FORKING\n"); | |
c906108c | 2759 | #endif |
c5aa993b | 2760 | } |
c906108c | 2761 | else if (tsp.tts_event & TTEVT_VFORK) |
c5aa993b JM |
2762 | { |
2763 | process_state = VFORKING; | |
2764 | ||
c906108c | 2765 | #ifdef WAIT_BUFFER_DEBUG |
c5aa993b JM |
2766 | if (debug_on) |
2767 | printf ("Process set to VFORKING\n"); | |
c906108c | 2768 | #endif |
c5aa993b | 2769 | } |
c906108c SS |
2770 | |
2771 | /* Make an exec or fork look like a breakpoint. Definitely a hack, | |
2772 | but I don't think non HP-UX-specific clients really carefully | |
2773 | inspect the first events they get after inferior startup, so | |
2774 | it probably almost doesn't matter what we claim this is. | |
c5aa993b | 2775 | */ |
c906108c SS |
2776 | |
2777 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
2778 | if (debug_on) |
2779 | printf ("..a process 'event'\n"); | |
c906108c SS |
2780 | #endif |
2781 | ||
2782 | /* Also make fork and exec events look like bpts, so they can be caught. | |
c5aa993b | 2783 | */ |
c906108c SS |
2784 | *status = 0177 | (_SIGTRAP << 8); |
2785 | } | |
2786 | ||
2787 | /* Special-cases: We ask for syscall entry and exit events to implement | |
2788 | "fast" (aka "hardware") watchpoints. | |
2789 | ||
2790 | When we get a syscall entry, we want to disable page-protections, | |
2791 | and resume the inferior; this isn't an event we wish for | |
2792 | wait_for_inferior to see. Note that we must resume ONLY the | |
2793 | thread that reported the syscall entry; we don't want to allow | |
2794 | other threads to run with the page protections off, as they might | |
2795 | then be able to write to watch memory without it being caught. | |
2796 | ||
2797 | When we get a syscall exit, we want to reenable page-protections, | |
2798 | but we don't want to resume the inferior; this is an event we wish | |
2799 | wait_for_inferior to see. Make it look like the signal we normally | |
2800 | get for a single-step completion. This should cause wait_for_inferior | |
2801 | to evaluate whether any watchpoint triggered. | |
2802 | ||
2803 | Or rather, that's what we'd LIKE to do for syscall exit; we can't, | |
2804 | due to some HP-UX "features". Some syscalls have problems with | |
2805 | write-protections on some pages, and some syscalls seem to have | |
2806 | pending writes to those pages at the time we're getting the return | |
2807 | event. So, we'll single-step the inferior to get out of the syscall, | |
2808 | and then reenable protections. | |
2809 | ||
2810 | Note that we're intentionally allowing the syscall exit case to | |
2811 | fall through into the succeeding cases, as sometimes we single- | |
2812 | step out of one syscall only to immediately enter another... | |
2813 | */ | |
2814 | else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY) | |
c5aa993b | 2815 | || (tsp.tts_event & TTEVT_SYSCALL_RETURN)) |
c906108c SS |
2816 | { |
2817 | /* Make a syscall event look like a breakpoint. Same comments | |
2818 | as for exec & fork events. | |
c5aa993b | 2819 | */ |
c906108c | 2820 | #ifdef THREAD_DEBUG |
c5aa993b JM |
2821 | if (debug_on) |
2822 | printf ("..a syscall 'event'\n"); | |
c906108c SS |
2823 | #endif |
2824 | ||
2825 | /* Also make syscall events look like bpts, so they can be caught. | |
c5aa993b | 2826 | */ |
c906108c SS |
2827 | *status = 0177 | (_SIGTRAP << 8); |
2828 | } | |
2829 | ||
2830 | else if ((tsp.tts_event & TTEVT_LWP_CREATE) | |
c5aa993b JM |
2831 | || (tsp.tts_event & TTEVT_LWP_TERMINATE) |
2832 | || (tsp.tts_event & TTEVT_LWP_EXIT)) | |
c906108c SS |
2833 | { |
2834 | /* Make a thread event look like a breakpoint. Same comments | |
2835 | * as for exec & fork events. | |
2836 | */ | |
2837 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
2838 | if (debug_on) |
2839 | printf ("..a thread 'event'\n"); | |
c906108c SS |
2840 | #endif |
2841 | ||
2842 | /* Also make thread events look like bpts, so they can be caught. | |
c5aa993b | 2843 | */ |
c906108c SS |
2844 | *status = 0177 | (_SIGTRAP << 8); |
2845 | } | |
c5aa993b | 2846 | |
c906108c | 2847 | else if ((tsp.tts_event & TTEVT_EXIT)) |
c5aa993b JM |
2848 | { /* WIFEXITED */ |
2849 | ||
c906108c | 2850 | #ifdef THREAD_DEBUG |
c5aa993b JM |
2851 | if (debug_on) |
2852 | printf ("..an exit\n"); | |
c906108c SS |
2853 | #endif |
2854 | ||
2855 | /* Prevent rest of gdb from thinking this is | |
2856 | * a new thread if for some reason it's never | |
2857 | * seen the main thread before. | |
2858 | */ | |
39f77062 | 2859 | inferior_ptid = pid_to_ptid (map_to_gdb_tid (real_tid)); /* HACK, FIX */ |
c5aa993b | 2860 | |
c906108c SS |
2861 | *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode); |
2862 | } | |
c5aa993b | 2863 | |
c906108c | 2864 | else if (tsp.tts_event & TTEVT_SIGNAL) |
c5aa993b | 2865 | { /* WIFSTOPPED */ |
c906108c | 2866 | #ifdef THREAD_DEBUG |
c5aa993b JM |
2867 | if (debug_on) |
2868 | printf ("..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo); | |
c906108c SS |
2869 | #endif |
2870 | ||
2871 | *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8); | |
2872 | } | |
2873 | ||
2874 | else | |
c5aa993b | 2875 | { /* !WIFSTOPPED */ |
c906108c SS |
2876 | |
2877 | /* This means the process or thread terminated. But we should've | |
2878 | caught an explicit exit/termination above. So warn (this is | |
2879 | really an internal error) and claim the process or thread | |
2880 | terminated with a SIGTRAP. | |
2881 | */ | |
2882 | ||
2883 | warning ("process_wait: unknown process state"); | |
2884 | ||
2885 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
2886 | if (debug_on) |
2887 | printf ("Process-level event %s, using tid %d\n", | |
2888 | get_printable_name_of_ttrace_event (tsp.tts_event), | |
2889 | real_tid); | |
c906108c SS |
2890 | #endif |
2891 | ||
2892 | *status = _SIGTRAP; | |
2893 | } | |
2894 | ||
de6ee558 | 2895 | target_post_wait (pid_to_ptid (tsp.tts_pid), *status); |
c906108c SS |
2896 | |
2897 | ||
2898 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
2899 | if (debug_on) |
2900 | printf ("Done waiting, pid is %d, tid %d\n", real_pid, real_tid); | |
c906108c SS |
2901 | #endif |
2902 | ||
2903 | /* All code external to this module uses the tid, but calls | |
2904 | * it "pid". There's some tweaking so that the outside sees | |
2905 | * the first thread as having the same number as the starting | |
2906 | * pid. | |
2907 | */ | |
c5aa993b | 2908 | return_pid = map_to_gdb_tid (real_tid); |
c906108c | 2909 | |
c5aa993b JM |
2910 | if (real_tid == 0 || return_pid == 0) |
2911 | { | |
2912 | warning ("Internal error: process-wait failed."); | |
2913 | } | |
2914 | ||
de6ee558 | 2915 | return return_pid; |
c906108c | 2916 | } |
c906108c | 2917 | \f |
c5aa993b | 2918 | |
c906108c SS |
2919 | /* This function causes the caller's process to be traced by its |
2920 | parent. This is intended to be called after GDB forks itself, | |
2921 | and before the child execs the target. Despite the name, it | |
2922 | is called by the child. | |
2923 | ||
2924 | Note that HP-UX ttrace is rather funky in how this is done. | |
2925 | If the parent wants to get the initial exec event of a child, | |
2926 | it must set the ttrace event mask of the child to include execs. | |
2927 | (The child cannot do this itself.) This must be done after the | |
2928 | child is forked, but before it execs. | |
2929 | ||
2930 | To coordinate the parent and child, we implement a semaphore using | |
2931 | pipes. After SETTRC'ing itself, the child tells the parent that | |
2932 | it is now traceable by the parent, and waits for the parent's | |
2933 | acknowledgement. The parent can then set the child's event mask, | |
2934 | and notify the child that it can now exec. | |
2935 | ||
2936 | (The acknowledgement by parent happens as a result of a call to | |
2937 | child_acknowledge_created_inferior.) | |
2938 | */ | |
2939 | int | |
f7dd6af2 | 2940 | parent_attach_all (int p1, PTRACE_ARG3_TYPE p2, int p3) |
c906108c | 2941 | { |
c5aa993b | 2942 | int tt_status; |
c906108c SS |
2943 | |
2944 | /* We need a memory home for a constant, to pass it to ttrace. | |
2945 | The value of the constant is arbitrary, so long as both | |
2946 | parent and child use the same value. Might as well use the | |
2947 | "magic" constant provided by ttrace... | |
2948 | */ | |
c5aa993b JM |
2949 | uint64_t tc_magic_child = TT_VERSION; |
2950 | uint64_t tc_magic_parent = 0; | |
c906108c SS |
2951 | |
2952 | tt_status = call_real_ttrace ( | |
c5aa993b JM |
2953 | TT_PROC_SETTRC, |
2954 | (int) TT_NIL, | |
2955 | (lwpid_t) TT_NIL, | |
2956 | TT_NIL, | |
2957 | (TTRACE_ARG_TYPE) TT_VERSION, | |
2958 | TT_NIL); | |
c906108c SS |
2959 | |
2960 | if (tt_status < 0) | |
2961 | return tt_status; | |
2962 | ||
2963 | /* Notify the parent that we're potentially ready to exec(). */ | |
2964 | write (startup_semaphore.child_channel[SEM_TALK], | |
c5aa993b JM |
2965 | &tc_magic_child, |
2966 | sizeof (tc_magic_child)); | |
c906108c SS |
2967 | |
2968 | /* Wait for acknowledgement from the parent. */ | |
2969 | read (startup_semaphore.parent_channel[SEM_LISTEN], | |
c5aa993b JM |
2970 | &tc_magic_parent, |
2971 | sizeof (tc_magic_parent)); | |
2972 | ||
c906108c SS |
2973 | if (tc_magic_child != tc_magic_parent) |
2974 | warning ("mismatched semaphore magic"); | |
2975 | ||
2976 | /* Discard our copy of the semaphore. */ | |
2977 | (void) close (startup_semaphore.parent_channel[SEM_LISTEN]); | |
2978 | (void) close (startup_semaphore.parent_channel[SEM_TALK]); | |
2979 | (void) close (startup_semaphore.child_channel[SEM_LISTEN]); | |
2980 | (void) close (startup_semaphore.child_channel[SEM_TALK]); | |
c5aa993b | 2981 | |
c906108c SS |
2982 | return tt_status; |
2983 | } | |
2984 | ||
2985 | /* Despite being file-local, this routine is dealing with | |
2986 | * actual process IDs, not thread ids. That's because it's | |
2987 | * called before the first "wait" call, and there's no map | |
2988 | * yet from tids to pids. | |
2989 | * | |
2990 | * When it is called, a forked child is running, but waiting on | |
2991 | * the semaphore. If you stop the child and re-start it, | |
2992 | * things get confused, so don't do that! An attached child is | |
2993 | * stopped. | |
2994 | * | |
2995 | * Since this is called after either attach or run, we | |
2996 | * have to be the common part of both. | |
2997 | */ | |
2998 | static void | |
fba45db2 | 2999 | require_notification_of_events (int real_pid) |
c906108c | 3000 | { |
c5aa993b JM |
3001 | int tt_status; |
3002 | ttevent_t notifiable_events; | |
c906108c | 3003 | |
c5aa993b JM |
3004 | lwpid_t tid; |
3005 | ttstate_t thread_state; | |
c906108c SS |
3006 | |
3007 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
3008 | if (debug_on) |
3009 | printf ("Require notif, pid is %d\n", real_pid); | |
c906108c SS |
3010 | #endif |
3011 | ||
3012 | /* Temporary HACK: tell inftarg.c/child_wait to not | |
3013 | * loop until pids are the same. | |
3014 | */ | |
3015 | not_same_real_pid = 0; | |
3016 | ||
3017 | sigemptyset (¬ifiable_events.tte_signals); | |
3018 | notifiable_events.tte_opts = TTEO_NONE; | |
3019 | ||
3020 | /* This ensures that forked children inherit their parent's | |
3021 | * event mask, which we're setting here. | |
3022 | * | |
3023 | * NOTE: if you debug gdb with itself, then the ultimate | |
3024 | * debuggee gets flags set by the outermost gdb, as | |
3025 | * a child of a child will still inherit. | |
3026 | */ | |
3027 | notifiable_events.tte_opts |= TTEO_PROC_INHERIT; | |
3028 | ||
c5aa993b | 3029 | notifiable_events.tte_events = TTEVT_DEFAULT; |
c906108c SS |
3030 | notifiable_events.tte_events |= TTEVT_SIGNAL; |
3031 | notifiable_events.tte_events |= TTEVT_EXEC; | |
3032 | notifiable_events.tte_events |= TTEVT_EXIT; | |
3033 | notifiable_events.tte_events |= TTEVT_FORK; | |
3034 | notifiable_events.tte_events |= TTEVT_VFORK; | |
3035 | notifiable_events.tte_events |= TTEVT_LWP_CREATE; | |
3036 | notifiable_events.tte_events |= TTEVT_LWP_EXIT; | |
3037 | notifiable_events.tte_events |= TTEVT_LWP_TERMINATE; | |
3038 | ||
3039 | tt_status = call_real_ttrace ( | |
c5aa993b JM |
3040 | TT_PROC_SET_EVENT_MASK, |
3041 | real_pid, | |
3042 | (lwpid_t) TT_NIL, | |
3043 | (TTRACE_ARG_TYPE) & notifiable_events, | |
3044 | (TTRACE_ARG_TYPE) sizeof (notifiable_events), | |
3045 | TT_NIL); | |
c906108c SS |
3046 | } |
3047 | ||
3048 | static void | |
fba45db2 | 3049 | require_notification_of_exec_events (int real_pid) |
c906108c | 3050 | { |
c5aa993b JM |
3051 | int tt_status; |
3052 | ttevent_t notifiable_events; | |
c906108c | 3053 | |
c5aa993b JM |
3054 | lwpid_t tid; |
3055 | ttstate_t thread_state; | |
c906108c SS |
3056 | |
3057 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
3058 | if (debug_on) |
3059 | printf ("Require notif, pid is %d\n", real_pid); | |
c906108c SS |
3060 | #endif |
3061 | ||
3062 | /* Temporary HACK: tell inftarg.c/child_wait to not | |
3063 | * loop until pids are the same. | |
3064 | */ | |
3065 | not_same_real_pid = 0; | |
3066 | ||
3067 | sigemptyset (¬ifiable_events.tte_signals); | |
3068 | notifiable_events.tte_opts = TTEO_NOSTRCCHLD; | |
3069 | ||
3070 | /* This ensures that forked children don't inherit their parent's | |
3071 | * event mask, which we're setting here. | |
3072 | */ | |
3073 | notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT; | |
3074 | ||
c5aa993b | 3075 | notifiable_events.tte_events = TTEVT_DEFAULT; |
c906108c SS |
3076 | notifiable_events.tte_events |= TTEVT_EXEC; |
3077 | notifiable_events.tte_events |= TTEVT_EXIT; | |
3078 | ||
3079 | tt_status = call_real_ttrace ( | |
c5aa993b JM |
3080 | TT_PROC_SET_EVENT_MASK, |
3081 | real_pid, | |
3082 | (lwpid_t) TT_NIL, | |
3083 | (TTRACE_ARG_TYPE) & notifiable_events, | |
3084 | (TTRACE_ARG_TYPE) sizeof (notifiable_events), | |
3085 | TT_NIL); | |
c906108c | 3086 | } |
c906108c | 3087 | \f |
c5aa993b | 3088 | |
c906108c SS |
3089 | /* This function is called by the parent process, with pid being the |
3090 | * ID of the child process, after the debugger has forked. | |
3091 | */ | |
3092 | void | |
fba45db2 | 3093 | child_acknowledge_created_inferior (int pid) |
c906108c SS |
3094 | { |
3095 | /* We need a memory home for a constant, to pass it to ttrace. | |
3096 | The value of the constant is arbitrary, so long as both | |
3097 | parent and child use the same value. Might as well use the | |
3098 | "magic" constant provided by ttrace... | |
c5aa993b JM |
3099 | */ |
3100 | uint64_t tc_magic_parent = TT_VERSION; | |
3101 | uint64_t tc_magic_child = 0; | |
c906108c SS |
3102 | |
3103 | /* Wait for the child to tell us that it has forked. */ | |
3104 | read (startup_semaphore.child_channel[SEM_LISTEN], | |
c5aa993b JM |
3105 | &tc_magic_child, |
3106 | sizeof (tc_magic_child)); | |
c906108c SS |
3107 | |
3108 | /* Clear thread info now. We'd like to do this in | |
3109 | * "require...", but that messes up attach. | |
3110 | */ | |
c5aa993b | 3111 | clear_thread_info (); |
c906108c SS |
3112 | |
3113 | /* Tell the "rest of gdb" that the initial thread exists. | |
3114 | * This isn't really a hack. Other thread-based versions | |
3115 | * of gdb (e.g. gnu-nat.c) seem to do the same thing. | |
3116 | * | |
3117 | * Q: Why don't we also add this thread to the local | |
3118 | * list via "add_tthread"? | |
3119 | * | |
3120 | * A: Because we don't know the tid, and can't stop the | |
3121 | * the process safely to ask what it is. Anyway, we'll | |
3122 | * add it when it gets the EXEC event. | |
3123 | */ | |
6c482b87 | 3124 | add_thread (pid_to_ptid (pid)); /* in thread.c */ |
c906108c SS |
3125 | |
3126 | /* We can now set the child's ttrace event mask. | |
3127 | */ | |
3128 | require_notification_of_exec_events (pid); | |
3129 | ||
3130 | /* Tell ourselves that the process is running. | |
3131 | */ | |
3132 | process_state = RUNNING; | |
3133 | ||
3134 | /* Notify the child that it can exec. */ | |
3135 | write (startup_semaphore.parent_channel[SEM_TALK], | |
c5aa993b JM |
3136 | &tc_magic_parent, |
3137 | sizeof (tc_magic_parent)); | |
c906108c SS |
3138 | |
3139 | /* Discard our copy of the semaphore. */ | |
3140 | (void) close (startup_semaphore.parent_channel[SEM_LISTEN]); | |
3141 | (void) close (startup_semaphore.parent_channel[SEM_TALK]); | |
3142 | (void) close (startup_semaphore.child_channel[SEM_LISTEN]); | |
3143 | (void) close (startup_semaphore.child_channel[SEM_TALK]); | |
3144 | } | |
3145 | ||
3146 | ||
3147 | /* | |
3148 | * arrange for notification of all events by | |
3149 | * calling require_notification_of_events. | |
3150 | */ | |
3151 | void | |
39f77062 | 3152 | child_post_startup_inferior (ptid_t ptid) |
c906108c | 3153 | { |
39f77062 | 3154 | require_notification_of_events (PIDGET (ptid)); |
c906108c SS |
3155 | } |
3156 | ||
3157 | /* From here on, we should expect tids rather than pids. | |
3158 | */ | |
3159 | static void | |
fba45db2 | 3160 | hppa_enable_catch_fork (int tid) |
c906108c | 3161 | { |
c5aa993b JM |
3162 | int tt_status; |
3163 | ttevent_t ttrace_events; | |
c906108c SS |
3164 | |
3165 | /* Get the set of events that are currently enabled. | |
3166 | */ | |
3167 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, | |
c5aa993b JM |
3168 | tid, |
3169 | (TTRACE_ARG_TYPE) & ttrace_events, | |
3170 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
3171 | TT_NIL); | |
c906108c SS |
3172 | if (errno) |
3173 | perror_with_name ("ttrace"); | |
3174 | ||
3175 | /* Add forks to that set. */ | |
3176 | ttrace_events.tte_events |= TTEVT_FORK; | |
3177 | ||
3178 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
3179 | if (debug_on) |
3180 | printf ("enable fork, tid is %d\n", tid); | |
c906108c SS |
3181 | #endif |
3182 | ||
3183 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, | |
c5aa993b JM |
3184 | tid, |
3185 | (TTRACE_ARG_TYPE) & ttrace_events, | |
3186 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
3187 | TT_NIL); | |
c906108c SS |
3188 | if (errno) |
3189 | perror_with_name ("ttrace"); | |
3190 | } | |
3191 | ||
3192 | ||
3193 | static void | |
fba45db2 | 3194 | hppa_disable_catch_fork (int tid) |
c906108c | 3195 | { |
c5aa993b JM |
3196 | int tt_status; |
3197 | ttevent_t ttrace_events; | |
c906108c SS |
3198 | |
3199 | /* Get the set of events that are currently enabled. | |
3200 | */ | |
3201 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, | |
c5aa993b JM |
3202 | tid, |
3203 | (TTRACE_ARG_TYPE) & ttrace_events, | |
3204 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
3205 | TT_NIL); | |
c906108c SS |
3206 | |
3207 | if (errno) | |
3208 | perror_with_name ("ttrace"); | |
3209 | ||
3210 | /* Remove forks from that set. */ | |
3211 | ttrace_events.tte_events &= ~TTEVT_FORK; | |
3212 | ||
3213 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
3214 | if (debug_on) |
3215 | printf ("disable fork, tid is %d\n", tid); | |
c906108c SS |
3216 | #endif |
3217 | ||
3218 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, | |
c5aa993b JM |
3219 | tid, |
3220 | (TTRACE_ARG_TYPE) & ttrace_events, | |
3221 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
3222 | TT_NIL); | |
c906108c SS |
3223 | |
3224 | if (errno) | |
3225 | perror_with_name ("ttrace"); | |
3226 | } | |
3227 | ||
3228 | ||
3229 | #if defined(CHILD_INSERT_FORK_CATCHPOINT) | |
3230 | int | |
fba45db2 | 3231 | child_insert_fork_catchpoint (int tid) |
c906108c SS |
3232 | { |
3233 | /* Enable reporting of fork events from the kernel. */ | |
3234 | /* ??rehrauer: For the moment, we're always enabling these events, | |
3235 | and just ignoring them if there's no catchpoint to catch them. | |
c5aa993b | 3236 | */ |
c906108c SS |
3237 | return 0; |
3238 | } | |
3239 | #endif | |
3240 | ||
3241 | ||
3242 | #if defined(CHILD_REMOVE_FORK_CATCHPOINT) | |
3243 | int | |
fba45db2 | 3244 | child_remove_fork_catchpoint (int tid) |
c906108c SS |
3245 | { |
3246 | /* Disable reporting of fork events from the kernel. */ | |
3247 | /* ??rehrauer: For the moment, we're always enabling these events, | |
3248 | and just ignoring them if there's no catchpoint to catch them. | |
c5aa993b | 3249 | */ |
c906108c SS |
3250 | return 0; |
3251 | } | |
3252 | #endif | |
3253 | ||
3254 | ||
3255 | static void | |
fba45db2 | 3256 | hppa_enable_catch_vfork (int tid) |
c906108c | 3257 | { |
c5aa993b JM |
3258 | int tt_status; |
3259 | ttevent_t ttrace_events; | |
c906108c SS |
3260 | |
3261 | /* Get the set of events that are currently enabled. | |
3262 | */ | |
3263 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, | |
c5aa993b JM |
3264 | tid, |
3265 | (TTRACE_ARG_TYPE) & ttrace_events, | |
3266 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
3267 | TT_NIL); | |
c906108c SS |
3268 | |
3269 | if (errno) | |
3270 | perror_with_name ("ttrace"); | |
3271 | ||
3272 | /* Add vforks to that set. */ | |
3273 | ttrace_events.tte_events |= TTEVT_VFORK; | |
3274 | ||
3275 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
3276 | if (debug_on) |
3277 | printf ("enable vfork, tid is %d\n", tid); | |
c906108c SS |
3278 | #endif |
3279 | ||
3280 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, | |
c5aa993b JM |
3281 | tid, |
3282 | (TTRACE_ARG_TYPE) & ttrace_events, | |
3283 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
3284 | TT_NIL); | |
c906108c SS |
3285 | |
3286 | if (errno) | |
3287 | perror_with_name ("ttrace"); | |
3288 | } | |
3289 | ||
3290 | ||
3291 | static void | |
fba45db2 | 3292 | hppa_disable_catch_vfork (int tid) |
c906108c | 3293 | { |
c5aa993b JM |
3294 | int tt_status; |
3295 | ttevent_t ttrace_events; | |
c906108c SS |
3296 | |
3297 | /* Get the set of events that are currently enabled. */ | |
3298 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, | |
c5aa993b JM |
3299 | tid, |
3300 | (TTRACE_ARG_TYPE) & ttrace_events, | |
3301 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
3302 | TT_NIL); | |
c906108c SS |
3303 | |
3304 | if (errno) | |
3305 | perror_with_name ("ttrace"); | |
3306 | ||
3307 | /* Remove vforks from that set. */ | |
3308 | ttrace_events.tte_events &= ~TTEVT_VFORK; | |
3309 | ||
3310 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
3311 | if (debug_on) |
3312 | printf ("disable vfork, tid is %d\n", tid); | |
c906108c SS |
3313 | #endif |
3314 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, | |
c5aa993b JM |
3315 | tid, |
3316 | (TTRACE_ARG_TYPE) & ttrace_events, | |
3317 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
3318 | TT_NIL); | |
c906108c SS |
3319 | |
3320 | if (errno) | |
3321 | perror_with_name ("ttrace"); | |
3322 | } | |
3323 | ||
3324 | ||
3325 | #if defined(CHILD_INSERT_VFORK_CATCHPOINT) | |
3326 | int | |
fba45db2 | 3327 | child_insert_vfork_catchpoint (int tid) |
c906108c SS |
3328 | { |
3329 | /* Enable reporting of vfork events from the kernel. */ | |
3330 | /* ??rehrauer: For the moment, we're always enabling these events, | |
3331 | and just ignoring them if there's no catchpoint to catch them. | |
c5aa993b | 3332 | */ |
c906108c SS |
3333 | return 0; |
3334 | } | |
3335 | #endif | |
3336 | ||
3337 | ||
3338 | #if defined(CHILD_REMOVE_VFORK_CATCHPOINT) | |
3339 | int | |
fba45db2 | 3340 | child_remove_vfork_catchpoint (int tid) |
c906108c SS |
3341 | { |
3342 | /* Disable reporting of vfork events from the kernel. */ | |
3343 | /* ??rehrauer: For the moment, we're always enabling these events, | |
3344 | and just ignoring them if there's no catchpoint to catch them. | |
c5aa993b | 3345 | */ |
c906108c SS |
3346 | return 0; |
3347 | } | |
3348 | #endif | |
3349 | ||
c906108c | 3350 | /* Q: Do we need to map the returned process ID to a thread ID? |
c5aa993b | 3351 | |
c906108c SS |
3352 | * A: I don't think so--here we want a _real_ pid. Any later |
3353 | * operations will call "require_notification_of_events" and | |
3354 | * start the mapping. | |
3355 | */ | |
3356 | int | |
47932f85 | 3357 | hpux_has_forked (int tid, int *childpid) |
c906108c | 3358 | { |
c5aa993b JM |
3359 | int tt_status; |
3360 | ttstate_t ttrace_state; | |
3361 | thread_info *tinfo; | |
c906108c SS |
3362 | |
3363 | /* Do we have cached thread state that we can consult? If so, use it. */ | |
3364 | tinfo = find_thread_info (map_from_gdb_tid (tid)); | |
c5aa993b JM |
3365 | if (tinfo != NULL) |
3366 | { | |
3367 | copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state); | |
3368 | } | |
c906108c SS |
3369 | |
3370 | /* Nope, must read the thread's current state */ | |
3371 | else | |
3372 | { | |
3373 | tt_status = call_ttrace (TT_LWP_GET_STATE, | |
c5aa993b JM |
3374 | tid, |
3375 | (TTRACE_ARG_TYPE) & ttrace_state, | |
3376 | (TTRACE_ARG_TYPE) sizeof (ttrace_state), | |
3377 | TT_NIL); | |
c906108c SS |
3378 | |
3379 | if (errno) | |
c5aa993b JM |
3380 | perror_with_name ("ttrace"); |
3381 | ||
c906108c | 3382 | if (tt_status < 0) |
c5aa993b | 3383 | return 0; |
c906108c SS |
3384 | } |
3385 | ||
3386 | if (ttrace_state.tts_event & TTEVT_FORK) | |
3387 | { | |
3388 | *childpid = ttrace_state.tts_u.tts_fork.tts_fpid; | |
3389 | return 1; | |
3390 | } | |
3391 | ||
3392 | return 0; | |
3393 | } | |
c906108c | 3394 | |
47932f85 | 3395 | /* See hpux_has_forked for pid discussion. |
c906108c SS |
3396 | */ |
3397 | int | |
47932f85 | 3398 | hpux_has_vforked (int tid, int *childpid) |
c906108c | 3399 | { |
c5aa993b JM |
3400 | int tt_status; |
3401 | ttstate_t ttrace_state; | |
3402 | thread_info *tinfo; | |
c906108c SS |
3403 | |
3404 | /* Do we have cached thread state that we can consult? If so, use it. */ | |
3405 | tinfo = find_thread_info (map_from_gdb_tid (tid)); | |
3406 | if (tinfo != NULL) | |
3407 | copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state); | |
3408 | ||
3409 | /* Nope, must read the thread's current state */ | |
3410 | else | |
3411 | { | |
3412 | tt_status = call_ttrace (TT_LWP_GET_STATE, | |
c5aa993b JM |
3413 | tid, |
3414 | (TTRACE_ARG_TYPE) & ttrace_state, | |
3415 | (TTRACE_ARG_TYPE) sizeof (ttrace_state), | |
3416 | TT_NIL); | |
c906108c SS |
3417 | |
3418 | if (errno) | |
c5aa993b JM |
3419 | perror_with_name ("ttrace"); |
3420 | ||
c906108c | 3421 | if (tt_status < 0) |
c5aa993b | 3422 | return 0; |
c906108c SS |
3423 | } |
3424 | ||
3425 | if (ttrace_state.tts_event & TTEVT_VFORK) | |
3426 | { | |
3427 | *childpid = ttrace_state.tts_u.tts_fork.tts_fpid; | |
3428 | return 1; | |
3429 | } | |
3430 | ||
3431 | return 0; | |
3432 | } | |
c906108c SS |
3433 | |
3434 | ||
c906108c SS |
3435 | #if defined(CHILD_INSERT_EXEC_CATCHPOINT) |
3436 | int | |
fba45db2 | 3437 | child_insert_exec_catchpoint (int tid) |
c906108c SS |
3438 | { |
3439 | /* Enable reporting of exec events from the kernel. */ | |
3440 | /* ??rehrauer: For the moment, we're always enabling these events, | |
3441 | and just ignoring them if there's no catchpoint to catch them. | |
c5aa993b | 3442 | */ |
c906108c SS |
3443 | return 0; |
3444 | } | |
3445 | #endif | |
3446 | ||
3447 | ||
3448 | #if defined(CHILD_REMOVE_EXEC_CATCHPOINT) | |
3449 | int | |
fba45db2 | 3450 | child_remove_exec_catchpoint (int tid) |
c906108c SS |
3451 | { |
3452 | /* Disable reporting of execevents from the kernel. */ | |
3453 | /* ??rehrauer: For the moment, we're always enabling these events, | |
3454 | and just ignoring them if there's no catchpoint to catch them. | |
c5aa993b | 3455 | */ |
c906108c SS |
3456 | return 0; |
3457 | } | |
3458 | #endif | |
3459 | ||
3460 | ||
c906108c | 3461 | int |
47932f85 | 3462 | hpux_has_execd (int tid, char **execd_pathname) |
c906108c | 3463 | { |
c5aa993b JM |
3464 | int tt_status; |
3465 | ttstate_t ttrace_state; | |
3466 | thread_info *tinfo; | |
c906108c SS |
3467 | |
3468 | /* Do we have cached thread state that we can consult? If so, use it. */ | |
3469 | tinfo = find_thread_info (map_from_gdb_tid (tid)); | |
3470 | if (tinfo != NULL) | |
3471 | copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state); | |
3472 | ||
3473 | /* Nope, must read the thread's current state */ | |
3474 | else | |
3475 | { | |
3476 | tt_status = call_ttrace (TT_LWP_GET_STATE, | |
c5aa993b JM |
3477 | tid, |
3478 | (TTRACE_ARG_TYPE) & ttrace_state, | |
3479 | (TTRACE_ARG_TYPE) sizeof (ttrace_state), | |
3480 | TT_NIL); | |
c906108c SS |
3481 | |
3482 | if (errno) | |
c5aa993b JM |
3483 | perror_with_name ("ttrace"); |
3484 | ||
c906108c | 3485 | if (tt_status < 0) |
c5aa993b | 3486 | return 0; |
c906108c SS |
3487 | } |
3488 | ||
3489 | if (ttrace_state.tts_event & TTEVT_EXEC) | |
3490 | { | |
3491 | /* See child_pid_to_exec_file in this file: this is a macro. | |
3492 | */ | |
c5aa993b JM |
3493 | char *exec_file = target_pid_to_exec_file (tid); |
3494 | ||
c906108c SS |
3495 | *execd_pathname = savestring (exec_file, strlen (exec_file)); |
3496 | return 1; | |
3497 | } | |
3498 | ||
3499 | return 0; | |
3500 | } | |
c906108c SS |
3501 | |
3502 | ||
c906108c | 3503 | int |
47932f85 | 3504 | hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id) |
c906108c | 3505 | { |
c5aa993b JM |
3506 | int tt_status; |
3507 | ttstate_t ttrace_state; | |
3508 | thread_info *tinfo; | |
c906108c SS |
3509 | |
3510 | /* Do we have cached thread state that we can consult? If so, use it. */ | |
3511 | tinfo = find_thread_info (map_from_gdb_tid (pid)); | |
3512 | if (tinfo != NULL) | |
3513 | copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state); | |
3514 | ||
3515 | /* Nope, must read the thread's current state */ | |
3516 | else | |
3517 | { | |
c5aa993b JM |
3518 | tt_status = call_ttrace (TT_LWP_GET_STATE, |
3519 | pid, | |
3520 | (TTRACE_ARG_TYPE) & ttrace_state, | |
3521 | (TTRACE_ARG_TYPE) sizeof (ttrace_state), | |
3522 | TT_NIL); | |
c906108c SS |
3523 | |
3524 | if (errno) | |
c5aa993b JM |
3525 | perror_with_name ("ttrace"); |
3526 | ||
c906108c | 3527 | if (tt_status < 0) |
c5aa993b | 3528 | return 0; |
c906108c SS |
3529 | } |
3530 | ||
c5aa993b | 3531 | *kind = TARGET_WAITKIND_SPURIOUS; /* Until proven otherwise... */ |
c906108c SS |
3532 | *syscall_id = -1; |
3533 | ||
3534 | if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY) | |
3535 | *kind = TARGET_WAITKIND_SYSCALL_ENTRY; | |
3536 | else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN) | |
3537 | *kind = TARGET_WAITKIND_SYSCALL_RETURN; | |
3538 | else | |
3539 | return 0; | |
3540 | ||
3541 | *syscall_id = ttrace_state.tts_scno; | |
3542 | return 1; | |
3543 | } | |
c5aa993b | 3544 | \f |
c906108c SS |
3545 | |
3546 | ||
c906108c SS |
3547 | #if defined(CHILD_THREAD_ALIVE) |
3548 | ||
3549 | /* Check to see if the given thread is alive. | |
c5aa993b | 3550 | |
c906108c SS |
3551 | * We'll trust the thread list, as the more correct |
3552 | * approach of stopping the process and spinning down | |
3553 | * the OS's thread list is _very_ expensive. | |
3554 | * | |
3555 | * May need a FIXME for that reason. | |
3556 | */ | |
3557 | int | |
39f77062 | 3558 | child_thread_alive (ptid_t ptid) |
c906108c | 3559 | { |
4b048bc0 | 3560 | lwpid_t gdb_tid = PIDGET (ptid); |
c5aa993b | 3561 | lwpid_t tid; |
c906108c | 3562 | |
c5aa993b JM |
3563 | /* This spins down the lists twice. |
3564 | * Possible peformance improvement here! | |
3565 | */ | |
3566 | tid = map_from_gdb_tid (gdb_tid); | |
3567 | return !is_terminated (tid); | |
c906108c SS |
3568 | } |
3569 | ||
3570 | #endif | |
c5aa993b | 3571 | \f |
c906108c SS |
3572 | |
3573 | ||
c906108c SS |
3574 | /* This function attempts to read the specified number of bytes from the |
3575 | save_state_t that is our view into the hardware registers, starting at | |
3576 | ss_offset, and ending at ss_offset + sizeof_buf - 1 | |
3577 | ||
3578 | If this function succeeds, it deposits the fetched bytes into buf, | |
3579 | and returns 0. | |
3580 | ||
3581 | If it fails, it returns a negative result. The contents of buf are | |
3582 | undefined it this function fails. | |
c5aa993b | 3583 | */ |
c906108c | 3584 | int |
fba45db2 KB |
3585 | read_from_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf, |
3586 | int sizeof_buf) | |
c906108c | 3587 | { |
c5aa993b JM |
3588 | int tt_status; |
3589 | register_value_t register_value = 0; | |
c906108c SS |
3590 | |
3591 | tt_status = call_ttrace (TT_LWP_RUREGS, | |
c5aa993b JM |
3592 | tid, |
3593 | ss_offset, | |
3594 | (TTRACE_ARG_TYPE) sizeof_buf, | |
3595 | (TTRACE_ARG_TYPE) buf); | |
3596 | ||
3597 | if (tt_status == 1) | |
3598 | /* Map ttrace's version of success to our version. | |
3599 | * Sometime ttrace returns 0, but that's ok here. | |
3600 | */ | |
3601 | return 0; | |
3602 | ||
c906108c SS |
3603 | return tt_status; |
3604 | } | |
c906108c | 3605 | \f |
c5aa993b | 3606 | |
c906108c SS |
3607 | /* This function attempts to write the specified number of bytes to the |
3608 | save_state_t that is our view into the hardware registers, starting at | |
3609 | ss_offset, and ending at ss_offset + sizeof_buf - 1 | |
3610 | ||
3611 | If this function succeeds, it deposits the bytes in buf, and returns 0. | |
3612 | ||
3613 | If it fails, it returns a negative result. The contents of the save_state_t | |
3614 | are undefined it this function fails. | |
c5aa993b | 3615 | */ |
c906108c | 3616 | int |
fba45db2 KB |
3617 | write_to_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf, |
3618 | int sizeof_buf) | |
c906108c | 3619 | { |
c5aa993b JM |
3620 | int tt_status; |
3621 | register_value_t register_value = 0; | |
c906108c SS |
3622 | |
3623 | tt_status = call_ttrace (TT_LWP_WUREGS, | |
c5aa993b JM |
3624 | tid, |
3625 | ss_offset, | |
3626 | (TTRACE_ARG_TYPE) sizeof_buf, | |
3627 | (TTRACE_ARG_TYPE) buf); | |
c906108c SS |
3628 | return tt_status; |
3629 | } | |
c906108c | 3630 | \f |
c5aa993b | 3631 | |
c906108c SS |
3632 | /* This function is a sop to the largeish number of direct calls |
3633 | to call_ptrace that exist in other files. Rather than create | |
3634 | functions whose name abstracts away from ptrace, and change all | |
3635 | the present callers of call_ptrace, we'll do the expedient (and | |
3636 | perhaps only practical) thing. | |
3637 | ||
3638 | Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced | |
3639 | process. Thus, we must translate all ptrace requests into their | |
3640 | process-specific, ttrace equivalents. | |
c5aa993b | 3641 | */ |
c906108c | 3642 | int |
fba45db2 | 3643 | call_ptrace (int pt_request, int gdb_tid, PTRACE_ARG3_TYPE addr, int data) |
c906108c | 3644 | { |
c5aa993b JM |
3645 | ttreq_t tt_request; |
3646 | TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr; | |
3647 | TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data; | |
3648 | TTRACE_ARG_TYPE tt_addr2 = TT_NIL; | |
3649 | int tt_status; | |
3650 | register_value_t register_value; | |
3651 | int read_buf; | |
c906108c SS |
3652 | |
3653 | /* Perform the necessary argument translation. Note that some | |
3654 | cases are funky enough in the ttrace realm that we handle them | |
3655 | very specially. | |
3656 | */ | |
c5aa993b JM |
3657 | switch (pt_request) |
3658 | { | |
c906108c SS |
3659 | /* The following cases cannot conveniently be handled conveniently |
3660 | by merely adjusting the ptrace arguments and feeding into the | |
3661 | generic call to ttrace at the bottom of this function. | |
3662 | ||
3663 | Note that because all branches of this switch end in "return", | |
3664 | there's no need for any "break" statements. | |
c5aa993b JM |
3665 | */ |
3666 | case PT_SETTRC: | |
f7dd6af2 | 3667 | return parent_attach_all (0, 0, 0); |
c5aa993b JM |
3668 | |
3669 | case PT_RUREGS: | |
3670 | tt_status = read_from_register_save_state (gdb_tid, | |
3671 | tt_addr, | |
3672 | ®ister_value, | |
3673 | sizeof (register_value)); | |
3674 | if (tt_status < 0) | |
3675 | return tt_status; | |
3676 | return register_value; | |
3677 | ||
3678 | case PT_WUREGS: | |
3679 | register_value = (int) tt_data; | |
3680 | tt_status = write_to_register_save_state (gdb_tid, | |
3681 | tt_addr, | |
3682 | ®ister_value, | |
3683 | sizeof (register_value)); | |
3684 | return tt_status; | |
3685 | break; | |
3686 | ||
3687 | case PT_READ_I: | |
3688 | tt_status = call_ttrace (TT_PROC_RDTEXT, /* Implicit 4-byte xfer becomes block-xfer. */ | |
3689 | gdb_tid, | |
3690 | tt_addr, | |
3691 | (TTRACE_ARG_TYPE) 4, | |
3692 | (TTRACE_ARG_TYPE) & read_buf); | |
3693 | if (tt_status < 0) | |
3694 | return tt_status; | |
3695 | return read_buf; | |
3696 | ||
3697 | case PT_READ_D: | |
3698 | tt_status = call_ttrace (TT_PROC_RDDATA, /* Implicit 4-byte xfer becomes block-xfer. */ | |
3699 | gdb_tid, | |
3700 | tt_addr, | |
3701 | (TTRACE_ARG_TYPE) 4, | |
3702 | (TTRACE_ARG_TYPE) & read_buf); | |
3703 | if (tt_status < 0) | |
3704 | return tt_status; | |
3705 | return read_buf; | |
3706 | ||
3707 | case PT_ATTACH: | |
3708 | tt_status = call_real_ttrace (TT_PROC_ATTACH, | |
3709 | map_from_gdb_tid (gdb_tid), | |
3710 | (lwpid_t) TT_NIL, | |
3711 | tt_addr, | |
3712 | (TTRACE_ARG_TYPE) TT_VERSION, | |
3713 | tt_addr2); | |
3714 | if (tt_status < 0) | |
3715 | return tt_status; | |
3716 | return tt_status; | |
c906108c SS |
3717 | |
3718 | /* The following cases are handled by merely adjusting the ptrace | |
3719 | arguments and feeding into the generic call to ttrace. | |
c5aa993b JM |
3720 | */ |
3721 | case PT_DETACH: | |
3722 | tt_request = TT_PROC_DETACH; | |
3723 | break; | |
3724 | ||
3725 | case PT_WRITE_I: | |
3726 | tt_request = TT_PROC_WRTEXT; /* Translates 4-byte xfer to block-xfer. */ | |
3727 | tt_data = 4; /* This many bytes. */ | |
3728 | tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */ | |
3729 | break; | |
3730 | ||
3731 | case PT_WRITE_D: | |
3732 | tt_request = TT_PROC_WRDATA; /* Translates 4-byte xfer to block-xfer. */ | |
3733 | tt_data = 4; /* This many bytes. */ | |
3734 | tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */ | |
3735 | break; | |
3736 | ||
3737 | case PT_RDTEXT: | |
3738 | tt_request = TT_PROC_RDTEXT; | |
3739 | break; | |
3740 | ||
3741 | case PT_RDDATA: | |
3742 | tt_request = TT_PROC_RDDATA; | |
3743 | break; | |
3744 | ||
3745 | case PT_WRTEXT: | |
3746 | tt_request = TT_PROC_WRTEXT; | |
3747 | break; | |
3748 | ||
3749 | case PT_WRDATA: | |
3750 | tt_request = TT_PROC_WRDATA; | |
3751 | break; | |
3752 | ||
3753 | case PT_CONTINUE: | |
3754 | tt_request = TT_PROC_CONTINUE; | |
3755 | break; | |
3756 | ||
3757 | case PT_STEP: | |
3758 | tt_request = TT_LWP_SINGLE; /* Should not be making this request? */ | |
3759 | break; | |
3760 | ||
3761 | case PT_KILL: | |
3762 | tt_request = TT_PROC_EXIT; | |
3763 | break; | |
3764 | ||
3765 | case PT_GET_PROCESS_PATHNAME: | |
3766 | tt_request = TT_PROC_GET_PATHNAME; | |
3767 | break; | |
3768 | ||
3769 | default: | |
3770 | tt_request = pt_request; /* Let ttrace be the one to complain. */ | |
3771 | break; | |
3772 | } | |
c906108c SS |
3773 | |
3774 | return call_ttrace (tt_request, | |
c5aa993b JM |
3775 | gdb_tid, |
3776 | tt_addr, | |
3777 | tt_data, | |
3778 | tt_addr2); | |
c906108c SS |
3779 | } |
3780 | ||
3781 | /* Kill that pesky process! | |
3782 | */ | |
3783 | void | |
fba45db2 | 3784 | kill_inferior (void) |
c906108c | 3785 | { |
c5aa993b JM |
3786 | int tid; |
3787 | int wait_status; | |
3788 | thread_info *t; | |
c906108c | 3789 | thread_info **paranoia; |
c5aa993b | 3790 | int para_count, i; |
c906108c | 3791 | |
39f77062 | 3792 | if (PIDGET (inferior_ptid) == 0) |
c906108c SS |
3793 | return; |
3794 | ||
3795 | /* Walk the list of "threads", some of which are "pseudo threads", | |
39f77062 | 3796 | aka "processes". For each that is NOT inferior_ptid, stop it, |
c906108c SS |
3797 | and detach it. |
3798 | ||
3799 | You see, we may not have just a single process to kill. If we're | |
3800 | restarting or quitting or detaching just after the inferior has | |
3801 | forked, then we've actually two processes to clean up. | |
3802 | ||
3803 | But we can't just call target_mourn_inferior() for each, since that | |
3804 | zaps the target vector. | |
c5aa993b | 3805 | */ |
c906108c | 3806 | |
3c37485b AC |
3807 | paranoia = (thread_info **) xmalloc (thread_head.count * |
3808 | sizeof (thread_info *)); | |
c906108c | 3809 | para_count = 0; |
c5aa993b | 3810 | |
c906108c | 3811 | t = thread_head.head; |
c5aa993b JM |
3812 | while (t) |
3813 | { | |
3814 | ||
3815 | paranoia[para_count] = t; | |
3816 | for (i = 0; i < para_count; i++) | |
3817 | { | |
3818 | if (t->next == paranoia[i]) | |
3819 | { | |
3820 | warning ("Bad data in gdb's thread data; repairing."); | |
3821 | t->next = 0; | |
3822 | } | |
3823 | } | |
3824 | para_count++; | |
3825 | ||
39f77062 | 3826 | if (t->am_pseudo && (t->pid != PIDGET (inferior_ptid))) |
c5aa993b | 3827 | { |
d3340a53 | 3828 | call_ttrace (TT_PROC_EXIT, |
c5aa993b JM |
3829 | t->pid, |
3830 | TT_NIL, | |
3831 | TT_NIL, | |
3832 | TT_NIL); | |
c5aa993b JM |
3833 | } |
3834 | t = t->next; | |
3835 | } | |
3836 | ||
b8c9b27d | 3837 | xfree (paranoia); |
c906108c | 3838 | |
d3340a53 | 3839 | call_ttrace (TT_PROC_EXIT, |
39f77062 | 3840 | PIDGET (inferior_ptid), |
c5aa993b JM |
3841 | TT_NIL, |
3842 | TT_NIL, | |
3843 | TT_NIL); | |
c906108c | 3844 | target_mourn_inferior (); |
c5aa993b | 3845 | clear_thread_info (); |
c906108c SS |
3846 | } |
3847 | ||
3848 | ||
3849 | #ifndef CHILD_RESUME | |
3850 | ||
3851 | /* Sanity check a thread about to be continued. | |
3852 | */ | |
3853 | static void | |
fba45db2 | 3854 | thread_dropping_event_check (thread_info *p) |
c906108c | 3855 | { |
c5aa993b JM |
3856 | if (!p->handled) |
3857 | { | |
3858 | /* | |
3859 | * This seems to happen when we "next" over a | |
3860 | * "fork()" while following the parent. If it's | |
3861 | * the FORK event, that's ok. If it's a SIGNAL | |
3862 | * in the unfollowed child, that's ok to--but | |
3863 | * how can we know that's what's going on? | |
3864 | * | |
3865 | * FIXME! | |
3866 | */ | |
3867 | if (p->have_state) | |
3868 | { | |
3869 | if (p->last_stop_state.tts_event == TTEVT_FORK) | |
3870 | { | |
3871 | /* Ok */ | |
3872 | ; | |
3873 | } | |
3874 | else if (p->last_stop_state.tts_event == TTEVT_SIGNAL) | |
3875 | { | |
3876 | /* Ok, close eyes and let it happen. | |
3877 | */ | |
3878 | ; | |
3879 | } | |
3880 | else | |
3881 | { | |
3882 | /* This shouldn't happen--we're dropping a | |
3883 | * real event. | |
3884 | */ | |
3885 | warning ("About to continue process %d, thread %d with unhandled event %s.", | |
3886 | p->pid, p->tid, | |
3887 | get_printable_name_of_ttrace_event ( | |
3888 | p->last_stop_state.tts_event)); | |
c906108c SS |
3889 | |
3890 | #ifdef PARANOIA | |
c5aa993b JM |
3891 | if (debug_on) |
3892 | print_tthread (p); | |
c906108c | 3893 | #endif |
c5aa993b JM |
3894 | } |
3895 | } | |
3896 | else | |
3897 | { | |
3898 | /* No saved state, have to assume it failed. | |
3899 | */ | |
3900 | warning ("About to continue process %d, thread %d with unhandled event.", | |
3901 | p->pid, p->tid); | |
c906108c | 3902 | #ifdef PARANOIA |
c5aa993b JM |
3903 | if (debug_on) |
3904 | print_tthread (p); | |
c906108c | 3905 | #endif |
c5aa993b | 3906 | } |
c906108c | 3907 | } |
c5aa993b JM |
3908 | |
3909 | } /* thread_dropping_event_check */ | |
c906108c SS |
3910 | |
3911 | /* Use a loop over the threads to continue all the threads but | |
3912 | * the one specified, which is to be stepped. | |
3913 | */ | |
3914 | static void | |
fba45db2 | 3915 | threads_continue_all_but_one (lwpid_t gdb_tid, int signal) |
c906108c | 3916 | { |
c5aa993b JM |
3917 | thread_info *p; |
3918 | int thread_signal; | |
3919 | lwpid_t real_tid; | |
3920 | lwpid_t scan_tid; | |
3921 | ttstate_t state; | |
3922 | int real_pid; | |
3923 | ||
c906108c | 3924 | #ifdef THREAD_DEBUG |
c5aa993b JM |
3925 | if (debug_on) |
3926 | printf ("Using loop over threads to step/resume with signals\n"); | |
c906108c SS |
3927 | #endif |
3928 | ||
c5aa993b JM |
3929 | /* First update the thread list. |
3930 | */ | |
3931 | set_all_unseen (); | |
3932 | real_tid = map_from_gdb_tid (gdb_tid); | |
3933 | real_pid = get_pid_for (real_tid); | |
3934 | ||
3935 | scan_tid = get_process_first_stopped_thread_id (real_pid, &state); | |
3936 | while (0 != scan_tid) | |
3937 | { | |
3938 | ||
c906108c | 3939 | #ifdef THREAD_DEBUG |
c5aa993b JM |
3940 | /* FIX: later should check state is stopped; |
3941 | * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED | |
3942 | */ | |
3943 | if (debug_on) | |
b871e4ec | 3944 | if ((state.tts_flags & TTS_STATEMASK) != TTS_WASSUSPENDED) |
c5aa993b | 3945 | printf ("About to continue non-stopped thread %d\n", scan_tid); |
c906108c SS |
3946 | #endif |
3947 | ||
c5aa993b JM |
3948 | p = find_thread_info (scan_tid); |
3949 | if (NULL == p) | |
3950 | { | |
3951 | add_tthread (real_pid, scan_tid); | |
3952 | p = find_thread_info (scan_tid); | |
3953 | ||
3954 | /* This is either a newly-created thread or the | |
3955 | * result of a fork; in either case there's no | |
3956 | * actual event to worry about. | |
3957 | */ | |
3958 | p->handled = 1; | |
3959 | ||
3960 | if (state.tts_event != TTEVT_NONE) | |
3961 | { | |
3962 | /* Oops, do need to worry! | |
3963 | */ | |
3964 | warning ("Unexpected thread with \"%s\" event.", | |
3965 | get_printable_name_of_ttrace_event (state.tts_event)); | |
3966 | } | |
3967 | } | |
3968 | else if (scan_tid != p->tid) | |
3969 | error ("Bad data in thread database."); | |
c906108c SS |
3970 | |
3971 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
3972 | if (debug_on) |
3973 | if (p->terminated) | |
3974 | printf ("Why are we continuing a dead thread?\n"); | |
c906108c SS |
3975 | #endif |
3976 | ||
c5aa993b JM |
3977 | p->seen = 1; |
3978 | ||
3979 | scan_tid = get_process_next_stopped_thread_id (real_pid, &state); | |
c906108c SS |
3980 | } |
3981 | ||
c5aa993b JM |
3982 | /* Remove unseen threads. |
3983 | */ | |
3984 | update_thread_list (); | |
c906108c | 3985 | |
c5aa993b JM |
3986 | /* Now run down the thread list and continue or step. |
3987 | */ | |
3988 | for (p = thread_head.head; p; p = p->next) | |
3989 | { | |
3990 | ||
3991 | /* Sanity check. | |
3992 | */ | |
3993 | thread_dropping_event_check (p); | |
3994 | ||
3995 | /* Pass the correct signals along. | |
3996 | */ | |
3997 | if (p->have_signal) | |
3998 | { | |
3999 | thread_signal = p->signal_value; | |
4000 | p->have_signal = 0; | |
4001 | } | |
4002 | else | |
4003 | thread_signal = 0; | |
4004 | ||
4005 | if (p->tid != real_tid) | |
4006 | { | |
4007 | /* | |
4008 | * Not the thread of interest, so continue it | |
4009 | * as the user expects. | |
4010 | */ | |
4011 | if (p->stepping_mode == DO_STEP) | |
4012 | { | |
4013 | /* Just step this thread. | |
4014 | */ | |
4015 | call_ttrace ( | |
4016 | TT_LWP_SINGLE, | |
4017 | p->tid, | |
4018 | TT_USE_CURRENT_PC, | |
4019 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), | |
4020 | TT_NIL); | |
4021 | } | |
4022 | else | |
4023 | { | |
4024 | /* Regular continue (default case). | |
4025 | */ | |
4026 | call_ttrace ( | |
4027 | TT_LWP_CONTINUE, | |
4028 | p->tid, | |
4029 | TT_USE_CURRENT_PC, | |
4030 | (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal), | |
4031 | TT_NIL); | |
4032 | } | |
4033 | } | |
4034 | else | |
4035 | { | |
4036 | /* Step the thread of interest. | |
4037 | */ | |
4038 | call_ttrace ( | |
4039 | TT_LWP_SINGLE, | |
4040 | real_tid, | |
4041 | TT_USE_CURRENT_PC, | |
4042 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), | |
4043 | TT_NIL); | |
4044 | } | |
4045 | } /* Loop over threads */ | |
4046 | } /* End threads_continue_all_but_one */ | |
c906108c SS |
4047 | |
4048 | /* Use a loop over the threads to continue all the threads. | |
4049 | * This is done when a signal must be sent to any of the threads. | |
4050 | */ | |
4051 | static void | |
fba45db2 | 4052 | threads_continue_all_with_signals (lwpid_t gdb_tid, int signal) |
c906108c | 4053 | { |
c5aa993b JM |
4054 | thread_info *p; |
4055 | int thread_signal; | |
4056 | lwpid_t real_tid; | |
4057 | lwpid_t scan_tid; | |
4058 | ttstate_t state; | |
4059 | int real_pid; | |
c906108c SS |
4060 | |
4061 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
4062 | if (debug_on) |
4063 | printf ("Using loop over threads to resume with signals\n"); | |
c906108c SS |
4064 | #endif |
4065 | ||
c5aa993b JM |
4066 | /* Scan and update thread list. |
4067 | */ | |
4068 | set_all_unseen (); | |
4069 | real_tid = map_from_gdb_tid (gdb_tid); | |
4070 | real_pid = get_pid_for (real_tid); | |
4071 | ||
4072 | scan_tid = get_process_first_stopped_thread_id (real_pid, &state); | |
4073 | while (0 != scan_tid) | |
4074 | { | |
4075 | ||
4076 | #ifdef THREAD_DEBUG | |
4077 | if (debug_on) | |
b871e4ec | 4078 | if ((state.tts_flags & TTS_STATEMASK) != TTS_WASSUSPENDED) |
c5aa993b JM |
4079 | warning ("About to continue non-stopped thread %d\n", scan_tid); |
4080 | #endif | |
4081 | ||
4082 | p = find_thread_info (scan_tid); | |
4083 | if (NULL == p) | |
4084 | { | |
4085 | add_tthread (real_pid, scan_tid); | |
4086 | p = find_thread_info (scan_tid); | |
4087 | ||
4088 | /* This is either a newly-created thread or the | |
4089 | * result of a fork; in either case there's no | |
4090 | * actual event to worry about. | |
4091 | */ | |
4092 | p->handled = 1; | |
4093 | ||
4094 | if (state.tts_event != TTEVT_NONE) | |
4095 | { | |
4096 | /* Oops, do need to worry! | |
4097 | */ | |
4098 | warning ("Unexpected thread with \"%s\" event.", | |
4099 | get_printable_name_of_ttrace_event (state.tts_event)); | |
4100 | } | |
4101 | } | |
c906108c | 4102 | |
c906108c | 4103 | #ifdef THREAD_DEBUG |
c5aa993b JM |
4104 | if (debug_on) |
4105 | if (p->terminated) | |
4106 | printf ("Why are we continuing a dead thread? (1)\n"); | |
c906108c SS |
4107 | #endif |
4108 | ||
c5aa993b | 4109 | p->seen = 1; |
c906108c | 4110 | |
c5aa993b JM |
4111 | scan_tid = get_process_next_stopped_thread_id (real_pid, &state); |
4112 | } | |
c906108c | 4113 | |
c5aa993b JM |
4114 | /* Remove unseen threads from our list. |
4115 | */ | |
4116 | update_thread_list (); | |
c906108c | 4117 | |
c5aa993b JM |
4118 | /* Continue the threads. |
4119 | */ | |
4120 | for (p = thread_head.head; p; p = p->next) | |
4121 | { | |
c906108c | 4122 | |
c5aa993b JM |
4123 | /* Sanity check. |
4124 | */ | |
4125 | thread_dropping_event_check (p); | |
c906108c | 4126 | |
c5aa993b JM |
4127 | /* Pass the correct signals along. |
4128 | */ | |
4129 | if (p->tid == real_tid) | |
4130 | { | |
4131 | thread_signal = signal; | |
4132 | p->have_signal = 0; | |
4133 | } | |
4134 | else if (p->have_signal) | |
4135 | { | |
4136 | thread_signal = p->signal_value; | |
4137 | p->have_signal = 0; | |
4138 | } | |
4139 | else | |
4140 | thread_signal = 0; | |
4141 | ||
4142 | if (p->stepping_mode == DO_STEP) | |
4143 | { | |
4144 | call_ttrace ( | |
4145 | TT_LWP_SINGLE, | |
4146 | p->tid, | |
4147 | TT_USE_CURRENT_PC, | |
4148 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), | |
4149 | TT_NIL); | |
4150 | } | |
4151 | else | |
4152 | { | |
4153 | /* Continue this thread (default case). | |
4154 | */ | |
4155 | call_ttrace ( | |
4156 | TT_LWP_CONTINUE, | |
4157 | p->tid, | |
4158 | TT_USE_CURRENT_PC, | |
4159 | (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal), | |
4160 | TT_NIL); | |
4161 | } | |
4162 | } | |
4163 | } /* End threads_continue_all_with_signals */ | |
c906108c SS |
4164 | |
4165 | /* Step one thread only. | |
4166 | */ | |
4167 | static void | |
fba45db2 | 4168 | thread_fake_step (lwpid_t tid, enum target_signal signal) |
c906108c | 4169 | { |
c5aa993b | 4170 | thread_info *p; |
c906108c SS |
4171 | |
4172 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
4173 | if (debug_on) |
4174 | { | |
4175 | printf ("Doing a fake-step over a bpt, etc. for %d\n", tid); | |
c906108c | 4176 | |
c5aa993b JM |
4177 | if (is_terminated (tid)) |
4178 | printf ("Why are we continuing a dead thread? (4)\n"); | |
c906108c SS |
4179 | } |
4180 | #endif | |
c906108c | 4181 | |
c5aa993b JM |
4182 | if (doing_fake_step) |
4183 | warning ("Step while step already in progress."); | |
4184 | ||
4185 | /* See if there's a saved signal value for this | |
4186 | * thread to be passed on, but no current signal. | |
4187 | */ | |
4188 | p = find_thread_info (tid); | |
4189 | if (p != NULL) | |
4190 | { | |
a0b3c4fd | 4191 | if (p->have_signal && signal == TARGET_SIGNAL_0) |
c5aa993b JM |
4192 | { |
4193 | /* Pass on a saved signal. | |
4194 | */ | |
4195 | signal = p->signal_value; | |
4196 | } | |
4197 | ||
4198 | p->have_signal = 0; | |
4199 | } | |
4200 | ||
4201 | if (!p->handled) | |
4202 | warning ("Internal error: continuing unhandled thread."); | |
c906108c | 4203 | |
c5aa993b JM |
4204 | call_ttrace (TT_LWP_SINGLE, |
4205 | tid, | |
4206 | TT_USE_CURRENT_PC, | |
4207 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), | |
4208 | TT_NIL); | |
4209 | ||
4210 | /* Do bookkeeping so "call_ttrace_wait" knows it has to wait | |
4211 | * for this thread only, and clear any saved signal info. | |
4212 | */ | |
4213 | doing_fake_step = 1; | |
4214 | fake_step_tid = tid; | |
4215 | ||
4216 | } /* End thread_fake_step */ | |
c906108c SS |
4217 | |
4218 | /* Continue one thread when a signal must be sent to it. | |
4219 | */ | |
4220 | static void | |
fba45db2 | 4221 | threads_continue_one_with_signal (lwpid_t gdb_tid, int signal) |
c906108c | 4222 | { |
c5aa993b JM |
4223 | thread_info *p; |
4224 | lwpid_t real_tid; | |
4225 | int real_pid; | |
4226 | ||
c906108c | 4227 | #ifdef THREAD_DEBUG |
c5aa993b JM |
4228 | if (debug_on) |
4229 | printf ("Continuing one thread with a signal\n"); | |
c906108c SS |
4230 | #endif |
4231 | ||
c5aa993b JM |
4232 | real_tid = map_from_gdb_tid (gdb_tid); |
4233 | real_pid = get_pid_for (real_tid); | |
c906108c | 4234 | |
c5aa993b JM |
4235 | p = find_thread_info (real_tid); |
4236 | if (NULL == p) | |
4237 | { | |
4238 | add_tthread (real_pid, real_tid); | |
c906108c SS |
4239 | } |
4240 | ||
4241 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
4242 | if (debug_on) |
4243 | if (p->terminated) | |
4244 | printf ("Why are we continuing a dead thread? (2)\n"); | |
c906108c SS |
4245 | #endif |
4246 | ||
c5aa993b JM |
4247 | if (!p->handled) |
4248 | warning ("Internal error: continuing unhandled thread."); | |
4249 | ||
4250 | p->have_signal = 0; | |
4251 | ||
4252 | call_ttrace (TT_LWP_CONTINUE, | |
4253 | gdb_tid, | |
4254 | TT_USE_CURRENT_PC, | |
4255 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), | |
4256 | TT_NIL); | |
c906108c SS |
4257 | } |
4258 | #endif | |
4259 | ||
4260 | #ifndef CHILD_RESUME | |
4261 | ||
4262 | /* Resume execution of the inferior process. | |
c5aa993b | 4263 | |
c906108c SS |
4264 | * This routine is in charge of setting the "handled" bits. |
4265 | * | |
4266 | * If STEP is zero, continue it. | |
4267 | * If STEP is nonzero, single-step it. | |
4268 | * | |
4269 | * If SIGNAL is nonzero, give it that signal. | |
4270 | * | |
4271 | * If TID is -1, apply to all threads. | |
4272 | * If TID is not -1, apply to specified thread. | |
4273 | * | |
4274 | * STEP | |
4275 | * \ !0 0 | |
4276 | * TID \________________________________________________ | |
4277 | * | | |
4278 | * -1 | Step current Continue all threads | |
4279 | * | thread and (but which gets any | |
4280 | * | continue others signal?--We look at | |
39f77062 | 4281 | * | "inferior_ptid") |
c906108c SS |
4282 | * | |
4283 | * N | Step _this_ thread Continue _this_ thread | |
4284 | * | and leave others and leave others | |
4285 | * | stopped; internally stopped; used only for | |
4286 | * | used by gdb, never hardware watchpoints | |
4287 | * | a user command. and attach, never a | |
4288 | * | user command. | |
4289 | */ | |
4290 | void | |
39f77062 | 4291 | child_resume (ptid_t ptid, int step, enum target_signal signal) |
c906108c | 4292 | { |
c5aa993b | 4293 | int resume_all_threads; |
c906108c | 4294 | lwpid_t tid; |
c5aa993b | 4295 | process_state_t new_process_state; |
39f77062 | 4296 | lwpid_t gdb_tid = PIDGET (ptid); |
c906108c SS |
4297 | |
4298 | resume_all_threads = | |
4299 | (gdb_tid == INFTTRACE_ALL_THREADS) || | |
4300 | (vfork_in_flight); | |
4301 | ||
c5aa993b JM |
4302 | if (resume_all_threads) |
4303 | { | |
4304 | /* Resume all threads, but first pick a tid value | |
4305 | * so we can get the pid when in call_ttrace doing | |
4306 | * the map. | |
4307 | */ | |
4308 | if (vfork_in_flight) | |
4309 | tid = vforking_child_pid; | |
4310 | else | |
39f77062 | 4311 | tid = map_from_gdb_tid (PIDGET (inferior_ptid)); |
c5aa993b | 4312 | } |
c906108c | 4313 | else |
c5aa993b | 4314 | tid = map_from_gdb_tid (gdb_tid); |
c906108c SS |
4315 | |
4316 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
4317 | if (debug_on) |
4318 | { | |
4319 | if (more_events_left) | |
4320 | printf ("More events; "); | |
c906108c | 4321 | |
c5aa993b JM |
4322 | if (signal != 0) |
4323 | printf ("Sending signal %d; ", signal); | |
4324 | ||
4325 | if (resume_all_threads) | |
4326 | { | |
4327 | if (step == 0) | |
4328 | printf ("Continue process %d\n", tid); | |
4329 | else | |
4330 | printf ("Step/continue thread %d\n", tid); | |
4331 | } | |
4332 | else | |
4333 | { | |
4334 | if (step == 0) | |
4335 | printf ("Continue thread %d\n", tid); | |
4336 | else | |
4337 | printf ("Step just thread %d\n", tid); | |
4338 | } | |
4339 | ||
4340 | if (vfork_in_flight) | |
4341 | printf ("Vfork in flight\n"); | |
4342 | } | |
c906108c SS |
4343 | #endif |
4344 | ||
c5aa993b JM |
4345 | if (process_state == RUNNING) |
4346 | warning ("Internal error in resume logic; doing resume or step anyway."); | |
4347 | ||
4348 | if (!step /* Asked to continue... */ | |
4349 | && resume_all_threads /* whole process.. */ | |
4350 | && signal != 0 /* with a signal... */ | |
4351 | && more_events_left > 0) | |
4352 | { /* but we can't yet--save it! */ | |
c906108c SS |
4353 | |
4354 | /* Continue with signal means we have to set the pending | |
4355 | * signal value for this thread. | |
4356 | */ | |
4357 | thread_info *k; | |
c5aa993b | 4358 | |
c906108c | 4359 | #ifdef THREAD_DEBUG |
c5aa993b JM |
4360 | if (debug_on) |
4361 | printf ("Saving signal %d for thread %d\n", signal, tid); | |
c906108c SS |
4362 | #endif |
4363 | ||
c5aa993b JM |
4364 | k = find_thread_info (tid); |
4365 | if (k != NULL) | |
4366 | { | |
4367 | k->have_signal = 1; | |
4368 | k->signal_value = signal; | |
c906108c SS |
4369 | |
4370 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
4371 | if (debug_on) |
4372 | if (k->terminated) | |
4373 | printf ("Why are we continuing a dead thread? (3)\n"); | |
c906108c SS |
4374 | #endif |
4375 | ||
c5aa993b | 4376 | } |
c906108c SS |
4377 | |
4378 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
4379 | else if (debug_on) |
4380 | { | |
4381 | printf ("No thread info for tid %d\n", tid); | |
4382 | } | |
c906108c | 4383 | #endif |
c5aa993b | 4384 | } |
c906108c SS |
4385 | |
4386 | /* Are we faking this "continue" or "step"? | |
c5aa993b | 4387 | |
c906108c SS |
4388 | * We used to do steps by continuing all the threads for |
4389 | * which the events had been handled already. While | |
4390 | * conceptually nicer (hides it all in a lower level), this | |
4391 | * can lead to starvation and a hang (e.g. all but one thread | |
4392 | * are unhandled at a breakpoint just before a "join" operation, | |
4393 | * and one thread is in the join, and the user wants to step that | |
4394 | * thread). | |
4395 | */ | |
c5aa993b JM |
4396 | if (resume_all_threads /* Whole process, therefore user command */ |
4397 | && more_events_left > 0) | |
4398 | { /* But we can't do this yet--fake it! */ | |
c906108c | 4399 | thread_info *p; |
c5aa993b JM |
4400 | |
4401 | if (!step) | |
4402 | { | |
4403 | /* No need to do any notes on a per-thread | |
4404 | * basis--we're done! | |
4405 | */ | |
c906108c | 4406 | #ifdef WAIT_BUFFER_DEBUG |
c5aa993b JM |
4407 | if (debug_on) |
4408 | printf ("Faking a process resume.\n"); | |
c906108c SS |
4409 | #endif |
4410 | ||
c5aa993b JM |
4411 | return; |
4412 | } | |
4413 | else | |
4414 | { | |
c906108c SS |
4415 | |
4416 | #ifdef WAIT_BUFFER_DEBUG | |
c5aa993b JM |
4417 | if (debug_on) |
4418 | printf ("Faking a process step.\n"); | |
c906108c SS |
4419 | #endif |
4420 | ||
c5aa993b JM |
4421 | } |
4422 | ||
4423 | p = find_thread_info (tid); | |
4424 | if (p == NULL) | |
4425 | { | |
4426 | warning ("No thread information for tid %d, 'next' command ignored.\n", tid); | |
4427 | return; | |
4428 | } | |
4429 | else | |
4430 | { | |
c906108c SS |
4431 | |
4432 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
4433 | if (debug_on) |
4434 | if (p->terminated) | |
4435 | printf ("Why are we continuing a dead thread? (3.5)\n"); | |
c906108c SS |
4436 | #endif |
4437 | ||
c5aa993b JM |
4438 | if (p->stepping_mode != DO_DEFAULT) |
4439 | { | |
4440 | warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored."); | |
c906108c | 4441 | |
c5aa993b JM |
4442 | return; |
4443 | } | |
c906108c | 4444 | |
c5aa993b JM |
4445 | if (step) |
4446 | p->stepping_mode = DO_STEP; | |
4447 | else | |
4448 | p->stepping_mode = DO_CONTINUE; | |
c906108c | 4449 | |
c5aa993b JM |
4450 | return; |
4451 | } /* Have thread info */ | |
4452 | } /* Must fake step or go */ | |
c906108c SS |
4453 | |
4454 | /* Execept for fake-steps, from here on we know we are | |
4455 | * going to wind up with a running process which will | |
4456 | * need a real wait. | |
4457 | */ | |
4458 | new_process_state = RUNNING; | |
4459 | ||
4460 | /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where | |
4461 | * it was. (If GDB wanted it to start some other way, we have already | |
4462 | * written a new PC value to the child.) | |
4463 | * | |
4464 | * If this system does not support PT_STEP, a higher level function will | |
4465 | * have called single_step() to transmute the step request into a | |
4466 | * continue request (by setting breakpoints on all possible successor | |
4467 | * instructions), so we don't have to worry about that here. | |
4468 | */ | |
c5aa993b JM |
4469 | if (step) |
4470 | { | |
4471 | if (resume_all_threads) | |
4472 | { | |
4473 | /* | |
4474 | * Regular user step: other threads get a "continue". | |
4475 | */ | |
4476 | threads_continue_all_but_one (tid, signal); | |
4477 | clear_all_handled (); | |
4478 | clear_all_stepping_mode (); | |
4479 | } | |
4480 | ||
4481 | else | |
4482 | { | |
4483 | /* "Fake step": gdb is stepping one thread over a | |
4484 | * breakpoint, watchpoint, or out of a library load | |
4485 | * event, etc. The rest just stay where they are. | |
4486 | * | |
4487 | * Also used when there are pending events: we really | |
4488 | * step the current thread, but leave the rest stopped. | |
4489 | * Users can't request this, but "wait_for_inferior" | |
4490 | * does--a lot! | |
4491 | */ | |
4492 | thread_fake_step (tid, signal); | |
4493 | ||
4494 | /* Clear the "handled" state of this thread, because | |
4495 | * we'll soon get a new event for it. Other events | |
4496 | * stay as they were. | |
4497 | */ | |
4498 | clear_handled (tid); | |
4499 | clear_stepping_mode (tid); | |
4500 | new_process_state = FAKE_STEPPING; | |
4501 | } | |
4502 | } | |
4503 | ||
4504 | else | |
4505 | { | |
da12f4d8 JL |
4506 | /* TT_LWP_CONTINUE can pass signals to threads, TT_PROC_CONTINUE can't. |
4507 | Therefore, we really can't use TT_PROC_CONTINUE here. | |
4508 | ||
4509 | Consider a process which stopped due to signal which gdb decides | |
4510 | to handle and not pass on to the inferior. In that case we must | |
4511 | clear the pending signal by restarting the inferior using | |
4512 | TT_LWP_CONTINUE and pass zero as the signal number. Else the | |
4513 | pending signal will be passed to the inferior. interrupt.exp | |
4514 | in the testsuite does this precise thing and fails due to the | |
4515 | unwanted signal delivery to the inferior. */ | |
7d2830a3 DJ |
4516 | /* drow/2002-12-05: However, note that we must use TT_PROC_CONTINUE |
4517 | if we are tracing a vfork. */ | |
4518 | if (vfork_in_flight) | |
4519 | { | |
4520 | call_ttrace (TT_PROC_CONTINUE, tid, TT_NIL, TT_NIL, TT_NIL); | |
4521 | clear_all_handled (); | |
4522 | clear_all_stepping_mode (); | |
4523 | } | |
4524 | else if (resume_all_threads) | |
c5aa993b | 4525 | { |
c906108c | 4526 | #ifdef THREAD_DEBUG |
da12f4d8 JL |
4527 | if (debug_on) |
4528 | printf ("Doing a continue by loop of all threads\n"); | |
c906108c SS |
4529 | #endif |
4530 | ||
da12f4d8 | 4531 | threads_continue_all_with_signals (tid, signal); |
c5aa993b | 4532 | |
da12f4d8 JL |
4533 | clear_all_handled (); |
4534 | clear_all_stepping_mode (); | |
c5aa993b | 4535 | } |
c5aa993b JM |
4536 | else |
4537 | { | |
c906108c | 4538 | #ifdef THREAD_DEBUG |
da12f4d8 | 4539 | printf ("Doing a continue w/signal of just thread %d\n", tid); |
c906108c SS |
4540 | #endif |
4541 | ||
da12f4d8 | 4542 | threads_continue_one_with_signal (tid, signal); |
c5aa993b | 4543 | |
da12f4d8 JL |
4544 | /* Clear the "handled" state of this thread, because we |
4545 | will soon get a new event for it. Other events can | |
4546 | stay as they were. */ | |
4547 | clear_handled (tid); | |
4548 | clear_stepping_mode (tid); | |
c5aa993b JM |
4549 | } |
4550 | } | |
c906108c SS |
4551 | |
4552 | process_state = new_process_state; | |
4553 | ||
4554 | #ifdef WAIT_BUFFER_DEBUG | |
c5aa993b JM |
4555 | if (debug_on) |
4556 | printf ("Process set to %s\n", | |
4557 | get_printable_name_of_process_state (process_state)); | |
c906108c SS |
4558 | #endif |
4559 | ||
4560 | } | |
4561 | #endif /* CHILD_RESUME */ | |
c906108c | 4562 | \f |
c906108c SS |
4563 | /* |
4564 | * Like it says. | |
4565 | * | |
39f77062 | 4566 | * One worry is that we may not be attaching to "inferior_ptid" |
c906108c SS |
4567 | * and thus may not want to clear out our data. FIXME? |
4568 | * | |
4569 | */ | |
4570 | static void | |
fba45db2 | 4571 | update_thread_state_after_attach (int pid, attach_continue_t kind_of_go) |
c906108c | 4572 | { |
c5aa993b JM |
4573 | int tt_status; |
4574 | ttstate_t thread_state; | |
4575 | lwpid_t a_thread; | |
4576 | lwpid_t tid; | |
c906108c SS |
4577 | |
4578 | /* The process better be stopped. | |
4579 | */ | |
c5aa993b JM |
4580 | if (process_state != STOPPED |
4581 | && process_state != VFORKING) | |
4582 | warning ("Internal error attaching."); | |
c906108c SS |
4583 | |
4584 | /* Clear out old tthread info and start over. This has the | |
4585 | * side effect of ensuring that the TRAP is reported as being | |
4586 | * in the right thread (re-mapped from tid to pid). | |
4587 | * | |
4588 | * It's because we need to add the tthread _now_ that we | |
4589 | * need to call "clear_thread_info" _now_, and that's why | |
4590 | * "require_notification_of_events" doesn't clear the thread | |
4591 | * info (it's called later than this routine). | |
4592 | */ | |
c5aa993b | 4593 | clear_thread_info (); |
c906108c SS |
4594 | a_thread = 0; |
4595 | ||
4596 | for (tid = get_process_first_stopped_thread_id (pid, &thread_state); | |
4597 | tid != 0; | |
4598 | tid = get_process_next_stopped_thread_id (pid, &thread_state)) | |
4599 | { | |
4600 | thread_info *p; | |
c5aa993b | 4601 | |
c906108c | 4602 | if (a_thread == 0) |
c5aa993b JM |
4603 | { |
4604 | a_thread = tid; | |
c906108c | 4605 | #ifdef THREAD_DEBUG |
c5aa993b JM |
4606 | if (debug_on) |
4607 | printf ("Attaching to process %d, thread %d\n", | |
4608 | pid, a_thread); | |
c906108c | 4609 | #endif |
c5aa993b | 4610 | } |
c906108c SS |
4611 | |
4612 | /* Tell ourselves and the "rest of gdb" that this thread | |
4613 | * exists. | |
4614 | * | |
4615 | * This isn't really a hack. Other thread-based versions | |
4616 | * of gdb (e.g. gnu-nat.c) seem to do the same thing. | |
4617 | * | |
4618 | * We don't need to do mapping here, as we know this | |
4619 | * is the first thread and thus gets the real pid | |
39f77062 | 4620 | * (and is "inferior_ptid"). |
c906108c SS |
4621 | * |
4622 | * NOTE: it probably isn't the originating thread, | |
4623 | * but that doesn't matter (we hope!). | |
4624 | */ | |
c5aa993b JM |
4625 | add_tthread (pid, tid); |
4626 | p = find_thread_info (tid); | |
4627 | if (NULL == p) /* ?We just added it! */ | |
4628 | error ("Internal error adding a thread on attach."); | |
4629 | ||
8c6b089e | 4630 | copy_ttstate_t (&p->last_stop_state, &thread_state); |
c906108c | 4631 | p->have_state = 1; |
c5aa993b JM |
4632 | |
4633 | if (DO_ATTACH_CONTINUE == kind_of_go) | |
4634 | { | |
4635 | /* | |
4636 | * If we are going to CONTINUE afterwards, | |
4637 | * raising a SIGTRAP, don't bother trying to | |
4638 | * handle this event. But check first! | |
4639 | */ | |
4640 | switch (p->last_stop_state.tts_event) | |
4641 | { | |
4642 | ||
4643 | case TTEVT_NONE: | |
4644 | /* Ok to set this handled. | |
4645 | */ | |
4646 | break; | |
4647 | ||
4648 | default: | |
4649 | warning ("Internal error; skipping event %s on process %d, thread %d.", | |
4650 | get_printable_name_of_ttrace_event ( | |
4651 | p->last_stop_state.tts_event), | |
4652 | p->pid, p->tid); | |
4653 | } | |
4654 | ||
4655 | set_handled (pid, tid); | |
4656 | ||
4657 | } | |
4658 | else | |
4659 | { | |
4660 | /* There will be no "continue" opertion, so the | |
4661 | * process remains stopped. Don't set any events | |
4662 | * handled except the "gimmies". | |
4663 | */ | |
4664 | switch (p->last_stop_state.tts_event) | |
4665 | { | |
4666 | ||
4667 | case TTEVT_NONE: | |
4668 | /* Ok to ignore this. | |
4669 | */ | |
4670 | set_handled (pid, tid); | |
4671 | break; | |
4672 | ||
4673 | case TTEVT_EXEC: | |
4674 | case TTEVT_FORK: | |
4675 | /* Expected "other" FORK or EXEC event from a | |
4676 | * fork or vfork. | |
4677 | */ | |
4678 | break; | |
4679 | ||
4680 | default: | |
4681 | printf ("Internal error: failed to handle event %s on process %d, thread %d.", | |
4682 | get_printable_name_of_ttrace_event ( | |
4683 | p->last_stop_state.tts_event), | |
4684 | p->pid, p->tid); | |
4685 | } | |
4686 | } | |
4687 | ||
6c482b87 | 4688 | add_thread (pid_to_ptid (pid)); /* in thread.c */ |
c906108c | 4689 | } |
c5aa993b | 4690 | |
c906108c | 4691 | #ifdef PARANOIA |
c5aa993b JM |
4692 | if (debug_on) |
4693 | print_tthreads (); | |
c906108c SS |
4694 | #endif |
4695 | ||
4696 | /* One mustn't call ttrace_wait() after attaching via ttrace, | |
4697 | 'cause the process is stopped already. | |
c5aa993b | 4698 | |
c906108c SS |
4699 | However, the upper layers of gdb's execution control will |
4700 | want to wait after attaching (but not after forks, in | |
4701 | which case they will be doing a "target_resume", anticipating | |
4702 | a later TTEVT_EXEC or TTEVT_FORK event). | |
4703 | ||
4704 | To make this attach() implementation more compatible with | |
4705 | others, we'll make the attached-to process raise a SIGTRAP. | |
4706 | ||
4707 | Issue: this continues only one thread. That could be | |
4708 | dangerous if the thread is blocked--the process won't run | |
4709 | and no trap will be raised. FIX! (check state.tts_flags? | |
4710 | need one that's either TTS_WASRUNNING--but we've stopped | |
4711 | it and made it TTS_WASSUSPENDED. Hum...FIXME!) | |
4712 | */ | |
c5aa993b JM |
4713 | if (DO_ATTACH_CONTINUE == kind_of_go) |
4714 | { | |
4715 | tt_status = call_real_ttrace ( | |
4716 | TT_LWP_CONTINUE, | |
4717 | pid, | |
4718 | a_thread, | |
4719 | TT_USE_CURRENT_PC, | |
4720 | (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP), | |
4721 | TT_NIL); | |
c906108c | 4722 | if (errno) |
c5aa993b | 4723 | perror_with_name ("ttrace"); |
c906108c | 4724 | |
c5aa993b | 4725 | clear_handled (a_thread); /* So TRAP will be reported. */ |
c906108c SS |
4726 | |
4727 | /* Now running. | |
4728 | */ | |
4729 | process_state = RUNNING; | |
c5aa993b | 4730 | } |
c906108c SS |
4731 | |
4732 | attach_flag = 1; | |
4733 | } | |
c906108c | 4734 | \f |
c5aa993b | 4735 | |
c906108c SS |
4736 | /* Start debugging the process whose number is PID. |
4737 | * (A _real_ pid). | |
4738 | */ | |
4739 | int | |
fba45db2 | 4740 | attach (int pid) |
c906108c | 4741 | { |
c5aa993b JM |
4742 | int tt_status; |
4743 | ||
c906108c | 4744 | tt_status = call_real_ttrace ( |
c5aa993b JM |
4745 | TT_PROC_ATTACH, |
4746 | pid, | |
4747 | (lwpid_t) TT_NIL, | |
4748 | TT_NIL, | |
4749 | (TTRACE_ARG_TYPE) TT_VERSION, | |
4750 | TT_NIL); | |
c906108c SS |
4751 | if (errno) |
4752 | perror_with_name ("ttrace attach"); | |
4753 | ||
4754 | /* If successful, the process is now stopped. | |
4755 | */ | |
4756 | process_state = STOPPED; | |
4757 | ||
4758 | /* Our caller ("attach_command" in "infcmd.c") | |
4759 | * expects to do a "wait_for_inferior" after | |
4760 | * the attach, so make sure the inferior is | |
4761 | * running when we're done. | |
4762 | */ | |
c5aa993b | 4763 | update_thread_state_after_attach (pid, DO_ATTACH_CONTINUE); |
c906108c SS |
4764 | |
4765 | return pid; | |
4766 | } | |
4767 | ||
4768 | ||
4769 | #if defined(CHILD_POST_ATTACH) | |
4770 | void | |
fba45db2 | 4771 | child_post_attach (int pid) |
c906108c SS |
4772 | { |
4773 | #ifdef THREAD_DEBUG | |
c5aa993b JM |
4774 | if (debug_on) |
4775 | printf ("child-post-attach call\n"); | |
c906108c SS |
4776 | #endif |
4777 | ||
4778 | require_notification_of_events (pid); | |
4779 | } | |
4780 | #endif | |
4781 | ||
4782 | ||
4783 | /* Stop debugging the process whose number is PID | |
4784 | and continue it with signal number SIGNAL. | |
4785 | SIGNAL = 0 means just continue it. | |
4786 | */ | |
4787 | void | |
fba45db2 | 4788 | detach (int signal) |
c906108c SS |
4789 | { |
4790 | errno = 0; | |
4791 | call_ttrace (TT_PROC_DETACH, | |
39f77062 | 4792 | PIDGET (inferior_ptid), |
c5aa993b JM |
4793 | TT_NIL, |
4794 | (TTRACE_ARG_TYPE) signal, | |
4795 | TT_NIL); | |
c906108c SS |
4796 | attach_flag = 0; |
4797 | ||
c5aa993b | 4798 | clear_thread_info (); |
c906108c SS |
4799 | |
4800 | /* Process-state? */ | |
4801 | } | |
c906108c | 4802 | \f |
c5aa993b | 4803 | |
c906108c SS |
4804 | /* Default the type of the ttrace transfer to int. */ |
4805 | #ifndef TTRACE_XFER_TYPE | |
4806 | #define TTRACE_XFER_TYPE int | |
4807 | #endif | |
4808 | ||
4809 | void | |
fba45db2 | 4810 | _initialize_kernel_u_addr (void) |
c906108c SS |
4811 | { |
4812 | } | |
4813 | ||
4814 | #if !defined (CHILD_XFER_MEMORY) | |
4815 | /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory | |
4816 | in the NEW_SUN_TTRACE case. | |
4817 | It ought to be straightforward. But it appears that writing did | |
4818 | not write the data that I specified. I cannot understand where | |
4819 | it got the data that it actually did write. */ | |
4820 | ||
4821 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR | |
4822 | to debugger memory starting at MYADDR. Copy to inferior if | |
73186089 | 4823 | WRITE is nonzero. TARGET is ignored. |
c5aa993b | 4824 | |
c906108c SS |
4825 | Returns the length copied, which is either the LEN argument or zero. |
4826 | This xfer function does not do partial moves, since child_ops | |
4827 | doesn't allow memory operations to cross below us in the target stack | |
4828 | anyway. */ | |
4829 | ||
4830 | int | |
73186089 | 4831 | child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, |
043780a1 | 4832 | struct mem_attrib *attrib, |
73186089 | 4833 | struct target_ops *target) |
c906108c | 4834 | { |
52f0bd74 | 4835 | int i; |
c906108c | 4836 | /* Round starting address down to longword boundary. */ |
52f0bd74 | 4837 | CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (TTRACE_XFER_TYPE); |
c906108c | 4838 | /* Round ending address up; get number of longwords that makes. */ |
52f0bd74 | 4839 | int count |
c5aa993b JM |
4840 | = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1) |
4841 | / sizeof (TTRACE_XFER_TYPE); | |
c906108c | 4842 | /* Allocate buffer of that many longwords. */ |
94cd915f MS |
4843 | /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe |
4844 | because it uses alloca to allocate a buffer of arbitrary size. | |
4845 | For very large xfers, this could crash GDB's stack. */ | |
52f0bd74 | 4846 | TTRACE_XFER_TYPE *buffer |
94cd915f | 4847 | = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE)); |
c906108c SS |
4848 | |
4849 | if (write) | |
4850 | { | |
4851 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
4852 | ||
c5aa993b JM |
4853 | if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE)) |
4854 | { | |
4855 | /* Need part of initial word -- fetch it. */ | |
4856 | buffer[0] = call_ttrace (TT_LWP_RDTEXT, | |
39f77062 | 4857 | PIDGET (inferior_ptid), |
c5aa993b JM |
4858 | (TTRACE_ARG_TYPE) addr, |
4859 | TT_NIL, | |
4860 | TT_NIL); | |
4861 | } | |
c906108c SS |
4862 | |
4863 | if (count > 1) /* FIXME, avoid if even boundary */ | |
4864 | { | |
4865 | buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT, | |
39f77062 | 4866 | PIDGET (inferior_ptid), |
c5aa993b JM |
4867 | ((TTRACE_ARG_TYPE) |
4868 | (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))), | |
4869 | TT_NIL, | |
4870 | TT_NIL); | |
c906108c SS |
4871 | } |
4872 | ||
4873 | /* Copy data to be written over corresponding part of buffer */ | |
4874 | ||
4875 | memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)), | |
4876 | myaddr, | |
4877 | len); | |
4878 | ||
4879 | /* Write the entire buffer. */ | |
4880 | ||
4881 | for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE)) | |
4882 | { | |
4883 | errno = 0; | |
4884 | call_ttrace (TT_LWP_WRDATA, | |
39f77062 | 4885 | PIDGET (inferior_ptid), |
c5aa993b JM |
4886 | (TTRACE_ARG_TYPE) addr, |
4887 | (TTRACE_ARG_TYPE) buffer[i], | |
4888 | TT_NIL); | |
c906108c SS |
4889 | if (errno) |
4890 | { | |
4891 | /* Using the appropriate one (I or D) is necessary for | |
c5aa993b | 4892 | Gould NP1, at least. */ |
c906108c SS |
4893 | errno = 0; |
4894 | call_ttrace (TT_LWP_WRTEXT, | |
39f77062 | 4895 | PIDGET (inferior_ptid), |
c5aa993b JM |
4896 | (TTRACE_ARG_TYPE) addr, |
4897 | (TTRACE_ARG_TYPE) buffer[i], | |
4898 | TT_NIL); | |
c906108c SS |
4899 | } |
4900 | if (errno) | |
4901 | return 0; | |
4902 | } | |
4903 | } | |
4904 | else | |
4905 | { | |
4906 | /* Read all the longwords */ | |
4907 | for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE)) | |
4908 | { | |
4909 | errno = 0; | |
4910 | buffer[i] = call_ttrace (TT_LWP_RDTEXT, | |
39f77062 | 4911 | PIDGET (inferior_ptid), |
c5aa993b JM |
4912 | (TTRACE_ARG_TYPE) addr, |
4913 | TT_NIL, | |
4914 | TT_NIL); | |
c906108c SS |
4915 | if (errno) |
4916 | return 0; | |
4917 | QUIT; | |
4918 | } | |
4919 | ||
4920 | /* Copy appropriate bytes out of the buffer. */ | |
4921 | memcpy (myaddr, | |
4922 | (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)), | |
4923 | len); | |
4924 | } | |
4925 | return len; | |
4926 | } | |
c906108c | 4927 | \f |
c5aa993b | 4928 | |
c906108c | 4929 | static void |
fba45db2 | 4930 | udot_info (void) |
c906108c | 4931 | { |
c5aa993b JM |
4932 | int udot_off; /* Offset into user struct */ |
4933 | int udot_val; /* Value from user struct at udot_off */ | |
4934 | char mess[128]; /* For messages */ | |
c906108c | 4935 | |
c5aa993b JM |
4936 | if (!target_has_execution) |
4937 | { | |
4938 | error ("The program is not being run."); | |
4939 | } | |
c906108c SS |
4940 | |
4941 | #if !defined (KERNEL_U_SIZE) | |
4942 | ||
4943 | /* Adding support for this command is easy. Typically you just add a | |
4944 | routine, called "kernel_u_size" that returns the size of the user | |
4945 | struct, to the appropriate *-nat.c file and then add to the native | |
4946 | config file "#define KERNEL_U_SIZE kernel_u_size()" */ | |
4947 | error ("Don't know how large ``struct user'' is in this version of gdb."); | |
4948 | ||
4949 | #else | |
4950 | ||
4951 | for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val)) | |
4952 | { | |
4953 | if ((udot_off % 24) == 0) | |
4954 | { | |
4955 | if (udot_off > 0) | |
4956 | { | |
4957 | printf_filtered ("\n"); | |
4958 | } | |
4959 | printf_filtered ("%04x:", udot_off); | |
4960 | } | |
4961 | udot_val = call_ttrace (TT_LWP_RUREGS, | |
39f77062 | 4962 | PIDGET (inferior_ptid), |
c5aa993b JM |
4963 | (TTRACE_ARG_TYPE) udot_off, |
4964 | TT_NIL, | |
4965 | TT_NIL); | |
c906108c SS |
4966 | if (errno != 0) |
4967 | { | |
4968 | sprintf (mess, "\nreading user struct at offset 0x%x", udot_off); | |
4969 | perror_with_name (mess); | |
4970 | } | |
4971 | /* Avoid using nonportable (?) "*" in print specs */ | |
4972 | printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val); | |
4973 | } | |
4974 | printf_filtered ("\n"); | |
4975 | ||
4976 | #endif | |
4977 | } | |
4978 | #endif /* !defined (CHILD_XFER_MEMORY). */ | |
4979 | ||
6aaea291 | 4980 | |
c906108c SS |
4981 | /* TTrace version of "target_pid_to_exec_file" |
4982 | */ | |
4983 | char * | |
fba45db2 | 4984 | child_pid_to_exec_file (int tid) |
c906108c | 4985 | { |
c5aa993b | 4986 | int tt_status; |
6aaea291 AC |
4987 | static char exec_file_buffer[1024]; |
4988 | pid_t pid; | |
4989 | static struct pst_status buf; | |
c5aa993b | 4990 | |
6aaea291 AC |
4991 | /* On various versions of hpux11, this may fail due to a supposed |
4992 | kernel bug. We have alternate methods to get this information | |
4993 | (ie pstat). */ | |
c906108c | 4994 | tt_status = call_ttrace (TT_PROC_GET_PATHNAME, |
c5aa993b | 4995 | tid, |
6aaea291 AC |
4996 | (uint64_t) exec_file_buffer, |
4997 | sizeof (exec_file_buffer) - 1, | |
4998 | 0); | |
c906108c SS |
4999 | if (tt_status >= 0) |
5000 | return exec_file_buffer; | |
5001 | ||
6aaea291 AC |
5002 | /* Try to get process information via pstat and extract the filename |
5003 | from the pst_cmd field within the pst_status structure. */ | |
5004 | if (pstat_getproc (&buf, sizeof (struct pst_status), 0, tid) != -1) | |
c5aa993b | 5005 | { |
6aaea291 | 5006 | char *p = buf.pst_cmd; |
c906108c | 5007 | |
6aaea291 AC |
5008 | while (*p && *p != ' ') |
5009 | p++; | |
5010 | *p = 0; | |
5011 | ||
5012 | return (buf.pst_cmd); | |
c906108c SS |
5013 | } |
5014 | ||
6aaea291 | 5015 | return (NULL); |
c906108c SS |
5016 | } |
5017 | ||
c906108c | 5018 | void |
fba45db2 | 5019 | pre_fork_inferior (void) |
c906108c | 5020 | { |
c5aa993b | 5021 | int status; |
c906108c SS |
5022 | |
5023 | status = pipe (startup_semaphore.parent_channel); | |
c5aa993b JM |
5024 | if (status < 0) |
5025 | { | |
c906108c SS |
5026 | warning ("error getting parent pipe for startup semaphore"); |
5027 | return; | |
c5aa993b | 5028 | } |
c906108c SS |
5029 | |
5030 | status = pipe (startup_semaphore.child_channel); | |
c5aa993b JM |
5031 | if (status < 0) |
5032 | { | |
c906108c SS |
5033 | warning ("error getting child pipe for startup semaphore"); |
5034 | return; | |
c5aa993b | 5035 | } |
c906108c SS |
5036 | } |
5037 | ||
4c9ba7e0 | 5038 | /* Called from child_follow_fork in hppah-nat.c. |
c906108c SS |
5039 | * |
5040 | * This seems to be intended to attach after a fork or | |
5041 | * vfork, while "attach" is used to attach to a pid | |
5042 | * given by the user. The check for an existing attach | |
5043 | * seems odd--it always fails in our test system. | |
5044 | */ | |
5045 | int | |
fba45db2 | 5046 | hppa_require_attach (int pid) |
c906108c | 5047 | { |
c5aa993b JM |
5048 | int tt_status; |
5049 | CORE_ADDR pc; | |
5050 | CORE_ADDR pc_addr; | |
5051 | unsigned int regs_offset; | |
c906108c | 5052 | process_state_t old_process_state = process_state; |
c5aa993b | 5053 | |
c906108c SS |
5054 | /* Are we already attached? There appears to be no explicit |
5055 | * way to answer this via ttrace, so we try something which | |
5056 | * should be innocuous if we are attached. If that fails, | |
5057 | * then we assume we're not attached, and so attempt to make | |
5058 | * it so. | |
5059 | */ | |
5060 | errno = 0; | |
5061 | tt_status = call_real_ttrace (TT_PROC_STOP, | |
c5aa993b JM |
5062 | pid, |
5063 | (lwpid_t) TT_NIL, | |
5064 | (TTRACE_ARG_TYPE) TT_NIL, | |
5065 | (TTRACE_ARG_TYPE) TT_NIL, | |
5066 | TT_NIL); | |
5067 | ||
c906108c SS |
5068 | if (errno) |
5069 | { | |
5070 | /* No change to process-state! | |
5071 | */ | |
5072 | errno = 0; | |
c5aa993b | 5073 | pid = attach (pid); |
c906108c SS |
5074 | } |
5075 | else | |
5076 | { | |
c5aa993b JM |
5077 | /* If successful, the process is now stopped. But if |
5078 | * we're VFORKING, the parent is still running, so don't | |
5079 | * change the process state. | |
5080 | */ | |
5081 | if (process_state != VFORKING) | |
5082 | process_state = STOPPED; | |
5083 | ||
5084 | /* If we were already attached, you'd think that we | |
5085 | * would need to start going again--but you'd be wrong, | |
5086 | * as the fork-following code is actually in the middle | |
5087 | * of the "resume" routine in in "infrun.c" and so | |
5088 | * will (almost) immediately do a resume. | |
5089 | * | |
5090 | * On the other hand, if we are VFORKING, which means | |
5091 | * that the child and the parent share a process for a | |
5092 | * while, we know that "resume" won't be resuming | |
5093 | * until the child EXEC event is seen. But we still | |
5094 | * don't want to continue, as the event is already | |
5095 | * there waiting. | |
5096 | */ | |
5097 | update_thread_state_after_attach (pid, DONT_ATTACH_CONTINUE); | |
5098 | } /* STOP succeeded */ | |
5099 | ||
c906108c SS |
5100 | return pid; |
5101 | } | |
5102 | ||
5103 | int | |
fba45db2 | 5104 | hppa_require_detach (int pid, int signal) |
c906108c | 5105 | { |
c5aa993b | 5106 | int tt_status; |
c906108c SS |
5107 | |
5108 | /* If signal is non-zero, we must pass the signal on to the active | |
5109 | thread prior to detaching. We do this by continuing the threads | |
5110 | with the signal. | |
5111 | */ | |
5112 | if (signal != 0) | |
5113 | { | |
5114 | errno = 0; | |
c5aa993b | 5115 | threads_continue_all_with_signals (pid, signal); |
c906108c SS |
5116 | } |
5117 | ||
5118 | errno = 0; | |
5119 | tt_status = call_ttrace (TT_PROC_DETACH, | |
c5aa993b JM |
5120 | pid, |
5121 | TT_NIL, | |
5122 | TT_NIL, | |
5123 | TT_NIL); | |
c906108c | 5124 | |
c5aa993b | 5125 | errno = 0; /* Ignore any errors. */ |
c906108c SS |
5126 | |
5127 | /* process_state? */ | |
c5aa993b | 5128 | |
c906108c SS |
5129 | return pid; |
5130 | } | |
5131 | ||
5132 | /* Given the starting address of a memory page, hash it to a bucket in | |
5133 | the memory page dictionary. | |
c5aa993b | 5134 | */ |
c906108c | 5135 | static int |
fba45db2 | 5136 | get_dictionary_bucket_of_page (CORE_ADDR page_start) |
c906108c | 5137 | { |
c5aa993b | 5138 | int hash; |
c906108c SS |
5139 | |
5140 | hash = (page_start / memory_page_dictionary.page_size); | |
5141 | hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; | |
5142 | ||
5143 | return hash; | |
5144 | } | |
5145 | ||
5146 | ||
5147 | /* Given a memory page's starting address, get (i.e., find an existing | |
5148 | or create a new) dictionary entry for the page. The page will be | |
5149 | write-protected when this function returns, but may have a reference | |
5150 | count of 0 (if the page was newly-added to the dictionary). | |
c5aa993b | 5151 | */ |
c906108c | 5152 | static memory_page_t * |
fba45db2 | 5153 | get_dictionary_entry_of_page (int pid, CORE_ADDR page_start) |
c906108c | 5154 | { |
c5aa993b JM |
5155 | int bucket; |
5156 | memory_page_t *page = NULL; | |
5157 | memory_page_t *previous_page = NULL; | |
c906108c SS |
5158 | |
5159 | /* We're going to be using the dictionary now, than-kew. */ | |
3731b38a | 5160 | require_memory_page_dictionary (); |
c906108c SS |
5161 | |
5162 | /* Try to find an existing dictionary entry for this page. Hash | |
5163 | on the page's starting address. | |
c5aa993b | 5164 | */ |
c906108c SS |
5165 | bucket = get_dictionary_bucket_of_page (page_start); |
5166 | page = &memory_page_dictionary.buckets[bucket]; | |
5167 | while (page != NULL) | |
5168 | { | |
5169 | if (page->page_start == page_start) | |
c5aa993b | 5170 | break; |
c906108c SS |
5171 | previous_page = page; |
5172 | page = page->next; | |
5173 | } | |
5174 | ||
5175 | /* Did we find a dictionary entry for this page? If not, then | |
5176 | add it to the dictionary now. | |
c5aa993b | 5177 | */ |
c906108c SS |
5178 | if (page == NULL) |
5179 | { | |
5180 | /* Create a new entry. */ | |
5181 | page = (memory_page_t *) xmalloc (sizeof (memory_page_t)); | |
5182 | page->page_start = page_start; | |
5183 | page->reference_count = 0; | |
5184 | page->next = NULL; | |
5185 | page->previous = NULL; | |
5186 | ||
5187 | /* We'll write-protect the page now, if that's allowed. */ | |
5188 | page->original_permissions = write_protect_page (pid, page_start); | |
5189 | ||
5190 | /* Add the new entry to the dictionary. */ | |
5191 | page->previous = previous_page; | |
5192 | previous_page->next = page; | |
5193 | ||
5194 | memory_page_dictionary.page_count++; | |
5195 | } | |
5196 | ||
5197 | return page; | |
5198 | } | |
5199 | ||
5200 | ||
5201 | static void | |
fba45db2 | 5202 | remove_dictionary_entry_of_page (int pid, memory_page_t *page) |
c906108c SS |
5203 | { |
5204 | /* Restore the page's original permissions. */ | |
5205 | unwrite_protect_page (pid, page->page_start, page->original_permissions); | |
5206 | ||
5207 | /* Kick the page out of the dictionary. */ | |
5208 | if (page->previous != NULL) | |
5209 | page->previous->next = page->next; | |
5210 | if (page->next != NULL) | |
5211 | page->next->previous = page->previous; | |
5212 | ||
5213 | /* Just in case someone retains a handle to this after it's freed. */ | |
5214 | page->page_start = (CORE_ADDR) 0; | |
5215 | ||
5216 | memory_page_dictionary.page_count--; | |
5217 | ||
b8c9b27d | 5218 | xfree (page); |
c906108c SS |
5219 | } |
5220 | ||
5221 | ||
5222 | static void | |
fba45db2 | 5223 | hppa_enable_syscall_events (int pid) |
c906108c | 5224 | { |
c5aa993b JM |
5225 | int tt_status; |
5226 | ttevent_t ttrace_events; | |
c906108c SS |
5227 | |
5228 | /* Get the set of events that are currently enabled. */ | |
5229 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, | |
c5aa993b JM |
5230 | pid, |
5231 | (TTRACE_ARG_TYPE) & ttrace_events, | |
5232 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
5233 | TT_NIL); | |
c906108c SS |
5234 | if (errno) |
5235 | perror_with_name ("ttrace"); | |
5236 | ||
5237 | /* Add syscall events to that set. */ | |
5238 | ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY; | |
5239 | ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN; | |
5240 | ||
5241 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, | |
c5aa993b JM |
5242 | pid, |
5243 | (TTRACE_ARG_TYPE) & ttrace_events, | |
5244 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
5245 | TT_NIL); | |
c906108c SS |
5246 | if (errno) |
5247 | perror_with_name ("ttrace"); | |
5248 | } | |
5249 | ||
5250 | ||
5251 | static void | |
fba45db2 | 5252 | hppa_disable_syscall_events (int pid) |
c906108c | 5253 | { |
c5aa993b JM |
5254 | int tt_status; |
5255 | ttevent_t ttrace_events; | |
c906108c SS |
5256 | |
5257 | /* Get the set of events that are currently enabled. */ | |
5258 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, | |
c5aa993b JM |
5259 | pid, |
5260 | (TTRACE_ARG_TYPE) & ttrace_events, | |
5261 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
5262 | TT_NIL); | |
c906108c SS |
5263 | if (errno) |
5264 | perror_with_name ("ttrace"); | |
5265 | ||
5266 | /* Remove syscall events from that set. */ | |
5267 | ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY; | |
5268 | ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN; | |
5269 | ||
5270 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, | |
c5aa993b JM |
5271 | pid, |
5272 | (TTRACE_ARG_TYPE) & ttrace_events, | |
5273 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), | |
5274 | TT_NIL); | |
c906108c SS |
5275 | if (errno) |
5276 | perror_with_name ("ttrace"); | |
5277 | } | |
5278 | ||
5279 | ||
5280 | /* The address range beginning with START and ending with START+LEN-1 | |
5281 | (inclusive) is to be watched via page-protection by a new watchpoint. | |
5282 | Set protection for all pages that overlap that range. | |
5283 | ||
5284 | Note that our caller sets TYPE to: | |
c5aa993b JM |
5285 | 0 for a bp_hardware_watchpoint, |
5286 | 1 for a bp_read_watchpoint, | |
5287 | 2 for a bp_access_watchpoint | |
c906108c SS |
5288 | |
5289 | (Yes, this is intentionally (though lord only knows why) different | |
5290 | from the TYPE that is passed to hppa_remove_hw_watchpoint.) | |
c5aa993b | 5291 | */ |
c906108c | 5292 | int |
fba45db2 | 5293 | hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type) |
c906108c | 5294 | { |
c5aa993b JM |
5295 | CORE_ADDR page_start; |
5296 | int dictionary_was_empty; | |
5297 | int page_size; | |
5298 | int page_id; | |
5299 | LONGEST range_size_in_pages; | |
c906108c SS |
5300 | |
5301 | if (type != 0) | |
5302 | error ("read or access hardware watchpoints not supported on HP-UX"); | |
5303 | ||
5304 | /* Examine all pages in the address range. */ | |
5305 | require_memory_page_dictionary (); | |
5306 | ||
5307 | dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0); | |
5308 | ||
5309 | page_size = memory_page_dictionary.page_size; | |
5310 | page_start = (start / page_size) * page_size; | |
5311 | range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size; | |
5312 | ||
c5aa993b | 5313 | for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size) |
c906108c | 5314 | { |
c5aa993b | 5315 | memory_page_t *page; |
c906108c SS |
5316 | |
5317 | /* This gets the page entered into the dictionary if it was | |
5318 | not already entered. | |
c5aa993b | 5319 | */ |
c906108c SS |
5320 | page = get_dictionary_entry_of_page (pid, page_start); |
5321 | page->reference_count++; | |
5322 | } | |
5323 | ||
5324 | /* Our implementation depends on seeing calls to kernel code, for the | |
5325 | following reason. Here we ask to be notified of syscalls. | |
5326 | ||
5327 | When a protected page is accessed by user code, HP-UX raises a SIGBUS. | |
5328 | Fine. | |
5329 | ||
5330 | But when kernel code accesses the page, it doesn't give a SIGBUS. | |
5331 | Rather, the system call that touched the page fails, with errno=EFAULT. | |
5332 | Not good for us. | |
5333 | ||
5334 | We could accomodate this "feature" by asking to be notified of syscall | |
5335 | entries & exits; upon getting an entry event, disabling page-protections; | |
5336 | upon getting an exit event, reenabling page-protections and then checking | |
5337 | if any watchpoints triggered. | |
5338 | ||
5339 | However, this turns out to be a real performance loser. syscalls are | |
5340 | usually a frequent occurrence. Having to unprotect-reprotect all watched | |
5341 | pages, and also to then read all watched memory locations and compare for | |
5342 | triggers, can be quite expensive. | |
5343 | ||
5344 | Instead, we'll only ask to be notified of syscall exits. When we get | |
5345 | one, we'll check whether errno is set. If not, or if it's not EFAULT, | |
5346 | we can just continue the inferior. | |
5347 | ||
5348 | If errno is set upon syscall exit to EFAULT, we must perform some fairly | |
5349 | hackish stuff to determine whether the failure really was due to a | |
5350 | page-protect trap on a watched location. | |
c5aa993b | 5351 | */ |
c906108c SS |
5352 | if (dictionary_was_empty) |
5353 | hppa_enable_syscall_events (pid); | |
5354 | ||
5355 | return 1; | |
5356 | } | |
5357 | ||
5358 | ||
5359 | /* The address range beginning with START and ending with START+LEN-1 | |
5360 | (inclusive) was being watched via page-protection by a watchpoint | |
5361 | which has been removed. Remove protection for all pages that | |
5362 | overlap that range, which are not also being watched by other | |
5363 | watchpoints. | |
c5aa993b | 5364 | */ |
c906108c | 5365 | int |
65e82032 | 5366 | hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type) |
c906108c | 5367 | { |
c5aa993b JM |
5368 | CORE_ADDR page_start; |
5369 | int dictionary_is_empty; | |
5370 | int page_size; | |
5371 | int page_id; | |
5372 | LONGEST range_size_in_pages; | |
c906108c SS |
5373 | |
5374 | if (type != 0) | |
5375 | error ("read or access hardware watchpoints not supported on HP-UX"); | |
5376 | ||
5377 | /* Examine all pages in the address range. */ | |
5378 | require_memory_page_dictionary (); | |
5379 | ||
5380 | page_size = memory_page_dictionary.page_size; | |
5381 | page_start = (start / page_size) * page_size; | |
5382 | range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size; | |
5383 | ||
c5aa993b | 5384 | for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size) |
c906108c | 5385 | { |
c5aa993b | 5386 | memory_page_t *page; |
c906108c SS |
5387 | |
5388 | page = get_dictionary_entry_of_page (pid, page_start); | |
5389 | page->reference_count--; | |
5390 | ||
5391 | /* Was this the last reference of this page? If so, then we | |
5392 | must scrub the entry from the dictionary, and also restore | |
5393 | the page's original permissions. | |
c5aa993b | 5394 | */ |
c906108c | 5395 | if (page->reference_count == 0) |
c5aa993b | 5396 | remove_dictionary_entry_of_page (pid, page); |
c906108c SS |
5397 | } |
5398 | ||
5399 | dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0); | |
5400 | ||
5401 | /* If write protections are currently disallowed, then that implies that | |
5402 | wait_for_inferior believes that the inferior is within a system call. | |
5403 | Since we want to see both syscall entry and return, it's clearly not | |
5404 | good to disable syscall events in this state! | |
5405 | ||
5406 | ??rehrauer: Yeah, it'd be better if we had a specific flag that said, | |
5407 | "inferior is between syscall events now". Oh well. | |
c5aa993b | 5408 | */ |
c906108c SS |
5409 | if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed) |
5410 | hppa_disable_syscall_events (pid); | |
5411 | ||
5412 | return 1; | |
5413 | } | |
5414 | ||
5415 | ||
5416 | /* Could we implement a watchpoint of this type via our available | |
5417 | hardware support? | |
5418 | ||
5419 | This query does not consider whether a particular address range | |
5420 | could be so watched, but just whether support is generally available | |
5421 | for such things. See hppa_range_profitable_for_hw_watchpoint for a | |
5422 | query that answers whether a particular range should be watched via | |
5423 | hardware support. | |
c5aa993b | 5424 | */ |
c906108c | 5425 | int |
65e82032 | 5426 | hppa_can_use_hw_watchpoint (int type, int cnt, int ot) |
c906108c SS |
5427 | { |
5428 | return (type == bp_hardware_watchpoint); | |
5429 | } | |
5430 | ||
5431 | ||
5432 | /* Assuming we could set a hardware watchpoint on this address, do | |
5433 | we think it would be profitable ("a good idea") to do so? If not, | |
5434 | we can always set a regular (aka single-step & test) watchpoint | |
5435 | on the address... | |
c5aa993b | 5436 | */ |
c906108c | 5437 | int |
fba45db2 | 5438 | hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len) |
c906108c | 5439 | { |
c5aa993b JM |
5440 | int range_is_stack_based; |
5441 | int range_is_accessible; | |
5442 | CORE_ADDR page_start; | |
5443 | int page_size; | |
5444 | int page; | |
5445 | LONGEST range_size_in_pages; | |
c906108c SS |
5446 | |
5447 | /* ??rehrauer: For now, say that all addresses are potentially | |
5448 | profitable. Possibly later we'll want to test the address | |
5449 | for "stackness"? | |
c5aa993b | 5450 | */ |
c906108c SS |
5451 | range_is_stack_based = 0; |
5452 | ||
5453 | /* If any page in the range is inaccessible, then we cannot | |
5454 | really use hardware watchpointing, even though our client | |
5455 | thinks we can. In that case, it's actually an error to | |
5456 | attempt to use hw watchpoints, so we'll tell our client | |
5457 | that the range is "unprofitable", and hope that they listen... | |
c5aa993b JM |
5458 | */ |
5459 | range_is_accessible = 1; /* Until proven otherwise. */ | |
c906108c SS |
5460 | |
5461 | /* Examine all pages in the address range. */ | |
5462 | errno = 0; | |
5463 | page_size = sysconf (_SC_PAGE_SIZE); | |
5464 | ||
5465 | /* If we can't determine page size, we're hosed. Tell our | |
5466 | client it's unprofitable to use hw watchpoints for this | |
5467 | range. | |
c5aa993b | 5468 | */ |
c906108c SS |
5469 | if (errno || (page_size <= 0)) |
5470 | { | |
5471 | errno = 0; | |
5472 | return 0; | |
5473 | } | |
5474 | ||
5475 | page_start = (start / page_size) * page_size; | |
c5aa993b | 5476 | range_size_in_pages = len / (LONGEST) page_size; |
c906108c | 5477 | |
c5aa993b | 5478 | for (page = 0; page < range_size_in_pages; page++, page_start += page_size) |
c906108c | 5479 | { |
c5aa993b JM |
5480 | int tt_status; |
5481 | int page_permissions; | |
c906108c SS |
5482 | |
5483 | /* Is this page accessible? */ | |
5484 | errno = 0; | |
5485 | tt_status = call_ttrace (TT_PROC_GET_MPROTECT, | |
c5aa993b JM |
5486 | pid, |
5487 | (TTRACE_ARG_TYPE) page_start, | |
5488 | TT_NIL, | |
5489 | (TTRACE_ARG_TYPE) & page_permissions); | |
c906108c | 5490 | if (errno || (tt_status < 0)) |
c5aa993b JM |
5491 | { |
5492 | errno = 0; | |
5493 | range_is_accessible = 0; | |
5494 | break; | |
5495 | } | |
c906108c SS |
5496 | |
5497 | /* Yes, go for another... */ | |
5498 | } | |
5499 | ||
c5aa993b | 5500 | return (!range_is_stack_based && range_is_accessible); |
c906108c SS |
5501 | } |
5502 | ||
5503 | ||
5504 | char * | |
39f77062 | 5505 | hppa_pid_or_tid_to_str (ptid_t ptid) |
c906108c | 5506 | { |
c5aa993b | 5507 | static char buf[100]; /* Static because address returned. */ |
39f77062 | 5508 | pid_t id = PIDGET (ptid); |
c906108c SS |
5509 | |
5510 | /* Does this appear to be a process? If so, print it that way. */ | |
5511 | if (is_process_id (id)) | |
39f77062 | 5512 | return child_pid_to_str (ptid); |
c906108c SS |
5513 | |
5514 | /* Else, print both the GDB thread number and the system thread id. */ | |
39f77062 KB |
5515 | sprintf (buf, "thread %d (", pid_to_thread_id (ptid)); |
5516 | strcat (buf, hppa_tid_to_str (ptid)); | |
c906108c SS |
5517 | strcat (buf, ")\0"); |
5518 | ||
5519 | return buf; | |
5520 | } | |
c906108c | 5521 | \f |
c5aa993b | 5522 | |
c906108c | 5523 | void |
fba45db2 | 5524 | hppa_ensure_vforking_parent_remains_stopped (int pid) |
c906108c SS |
5525 | { |
5526 | /* Nothing to do when using ttrace. Only the ptrace-based implementation | |
5527 | must do real work. | |
5528 | */ | |
5529 | } | |
5530 | ||
5531 | ||
5532 | int | |
fba45db2 | 5533 | hppa_resume_execd_vforking_child_to_get_parent_vfork (void) |
c906108c | 5534 | { |
c5aa993b | 5535 | return 0; /* No, the parent vfork is available now. */ |
c906108c | 5536 | } |
c5aa993b | 5537 | \f |
c906108c | 5538 | |
7be570e7 JM |
5539 | /* Write a register as a 64bit value. This may be necessary if the |
5540 | native OS is too braindamaged to allow some (or all) registers to | |
5541 | be written in 32bit hunks such as hpux11 and the PC queue registers. | |
5542 | ||
5543 | This is horribly gross and disgusting. */ | |
5544 | ||
5545 | int | |
fba45db2 | 5546 | ttrace_write_reg_64 (int gdb_tid, CORE_ADDR dest_addr, CORE_ADDR src_addr) |
7be570e7 JM |
5547 | { |
5548 | pid_t pid; | |
5549 | lwpid_t tid; | |
5550 | int tt_status; | |
5551 | ||
5552 | tid = map_from_gdb_tid (gdb_tid); | |
5553 | pid = get_pid_for (tid); | |
5554 | ||
5555 | errno = 0; | |
5556 | tt_status = ttrace (TT_LWP_WUREGS, | |
5557 | pid, | |
5558 | tid, | |
5559 | (TTRACE_ARG_TYPE) dest_addr, | |
5560 | 8, | |
5561 | (TTRACE_ARG_TYPE) src_addr ); | |
5562 | ||
5563 | #ifdef THREAD_DEBUG | |
5564 | if (errno) | |
5565 | { | |
5566 | /* Don't bother for a known benign error: if you ask for the | |
5567 | first thread state, but there is only one thread and it's | |
5568 | not stopped, ttrace complains. | |
5569 | ||
5570 | We have this inside the #ifdef because our caller will do | |
5571 | this check for real. */ | |
5572 | if( request != TT_PROC_GET_FIRST_LWP_STATE | |
5573 | || errno != EPROTO ) | |
5574 | { | |
5575 | if( debug_on ) | |
5576 | printf( "TT fail for %s, with pid %d, tid %d, status %d \n", | |
5577 | get_printable_name_of_ttrace_request (TT_LWP_WUREGS), | |
5578 | pid, tid, tt_status ); | |
5579 | } | |
5580 | } | |
5581 | #endif | |
5582 | ||
5583 | return tt_status; | |
5584 | } | |
c906108c | 5585 | |
c906108c | 5586 | void |
fba45db2 | 5587 | _initialize_infttrace (void) |
c906108c SS |
5588 | { |
5589 | /* Initialize the ttrace-based hardware watchpoint implementation. */ | |
c5aa993b | 5590 | memory_page_dictionary.page_count = (LONGEST) - 1; |
c906108c SS |
5591 | memory_page_dictionary.page_protections_allowed = 1; |
5592 | ||
5593 | errno = 0; | |
5594 | memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE); | |
5595 | ||
a0b3c4fd JM |
5596 | /* We do a lot of casts from pointers to TTRACE_ARG_TYPE; make sure |
5597 | this is okay. */ | |
5598 | if (sizeof (TTRACE_ARG_TYPE) < sizeof (void *)) | |
e1e9e218 | 5599 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
a0b3c4fd | 5600 | |
c906108c SS |
5601 | if (errno || (memory_page_dictionary.page_size <= 0)) |
5602 | perror_with_name ("sysconf"); | |
5603 | } |