Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Native support code for HPUX PA-RISC. |
2 | Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1998 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | Contributed by the Center for Software Science at the | |
6 | University of Utah (pa-gdb-bugs@cs.utah.edu). | |
7 | ||
8 | This file is part of GDB. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with this program; if not, write to the Free Software | |
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | ||
25 | #include "defs.h" | |
26 | #include "inferior.h" | |
27 | #include "target.h" | |
28 | #include <sys/ptrace.h> | |
29 | #include "gdbcore.h" | |
30 | #include <wait.h> | |
31 | #include <signal.h> | |
32 | ||
33 | extern CORE_ADDR text_end; | |
34 | ||
35 | static void fetch_register PARAMS ((int)); | |
36 | ||
37 | void | |
38 | fetch_inferior_registers (regno) | |
39 | int regno; | |
40 | { | |
41 | if (regno == -1) | |
42 | for (regno = 0; regno < NUM_REGS; regno++) | |
43 | fetch_register (regno); | |
44 | else | |
45 | fetch_register (regno); | |
46 | } | |
47 | ||
48 | /* Store our register values back into the inferior. | |
49 | If REGNO is -1, do this for all registers. | |
50 | Otherwise, REGNO specifies which register (so we can save time). */ | |
51 | ||
52 | void | |
53 | store_inferior_registers (regno) | |
54 | int regno; | |
55 | { | |
56 | register unsigned int regaddr; | |
57 | char buf[80]; | |
58 | extern char registers[]; | |
59 | register int i; | |
60 | unsigned int offset = U_REGS_OFFSET; | |
61 | int scratch; | |
62 | ||
63 | if (regno >= 0) | |
64 | { | |
65 | if (CANNOT_STORE_REGISTER (regno)) | |
66 | return; | |
67 | regaddr = register_addr (regno, offset); | |
68 | errno = 0; | |
69 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM) | |
70 | { | |
71 | scratch = *(int *) ®isters[REGISTER_BYTE (regno)] | 0x3; | |
72 | call_ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
73 | scratch); | |
74 | if (errno != 0) | |
75 | { | |
76 | /* Error, even if attached. Failing to write these two | |
77 | registers is pretty serious. */ | |
78 | sprintf (buf, "writing register number %d", regno); | |
79 | perror_with_name (buf); | |
80 | } | |
81 | } | |
82 | else | |
83 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) | |
84 | { | |
85 | errno = 0; | |
86 | call_ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
87 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); | |
88 | if (errno != 0) | |
89 | { | |
90 | /* Warning, not error, in case we are attached; sometimes the | |
91 | kernel doesn't let us at the registers. */ | |
92 | char *err = safe_strerror (errno); | |
93 | char *msg = alloca (strlen (err) + 128); | |
94 | sprintf (msg, "writing register %s: %s", | |
95 | REGISTER_NAME (regno), err); | |
96 | warning (msg); | |
97 | return; | |
98 | } | |
99 | regaddr += sizeof(int); | |
100 | } | |
101 | } | |
102 | else | |
103 | for (regno = 0; regno < NUM_REGS; regno++) | |
104 | store_inferior_registers (regno); | |
105 | } | |
106 | ||
107 | /* Fetch one register. */ | |
108 | ||
109 | static void | |
110 | fetch_register (regno) | |
111 | int regno; | |
112 | { | |
113 | register unsigned int regaddr; | |
114 | char buf[MAX_REGISTER_RAW_SIZE]; | |
115 | register int i; | |
116 | ||
117 | /* Offset of registers within the u area. */ | |
118 | unsigned int offset; | |
119 | ||
120 | offset = U_REGS_OFFSET; | |
121 | ||
122 | regaddr = register_addr (regno, offset); | |
123 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) | |
124 | { | |
125 | errno = 0; | |
126 | *(int *) &buf[i] = call_ptrace (PT_RUREGS, inferior_pid, | |
127 | (PTRACE_ARG3_TYPE) regaddr, 0); | |
128 | regaddr += sizeof (int); | |
129 | if (errno != 0) | |
130 | { | |
131 | /* Warning, not error, in case we are attached; sometimes the | |
132 | * kernel doesn't let us at the registers. | |
133 | */ | |
134 | char *err = safe_strerror (errno); | |
135 | char *msg = alloca (strlen (err) + 128); | |
136 | sprintf (msg, "reading register %s: %s", REGISTER_NAME (regno), err); | |
137 | warning (msg); | |
138 | goto error_exit; | |
139 | } | |
140 | } | |
141 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM) | |
142 | buf[3] &= ~0x3; | |
143 | supply_register (regno, buf); | |
144 | error_exit:; | |
145 | } | |
146 | ||
147 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR | |
148 | to debugger memory starting at MYADDR. Copy to inferior if | |
149 | WRITE is nonzero. | |
150 | ||
151 | Returns the length copied, which is either the LEN argument or zero. | |
152 | This xfer function does not do partial moves, since child_ops | |
153 | doesn't allow memory operations to cross below us in the target stack | |
154 | anyway. */ | |
155 | ||
156 | int | |
157 | child_xfer_memory (memaddr, myaddr, len, write, target) | |
158 | CORE_ADDR memaddr; | |
159 | char *myaddr; | |
160 | int len; | |
161 | int write; | |
162 | struct target_ops *target; /* ignored */ | |
163 | { | |
164 | register int i; | |
165 | /* Round starting address down to longword boundary. */ | |
166 | register CORE_ADDR addr = memaddr & - sizeof (int); | |
167 | /* Round ending address up; get number of longwords that makes. */ | |
168 | register int count | |
169 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
170 | ||
171 | /* Allocate buffer of that many longwords. */ | |
172 | /* Note (RT) - This code formerly used alloca, which I have | |
173 | * replaced with xmalloc and a matching free() at the end. | |
174 | * The problem with alloca() is that there is no guarantee of | |
175 | * when it'll be freed, and we were seeing cases of memory | |
176 | * leaks on: | |
177 | * (gdb) watch x | |
178 | * (gdb) cont | |
179 | * where the piled-up alloca's for the child_xfer_memory buffers | |
180 | * were not getting freed. | |
181 | */ | |
182 | register int *buffer = (int *) xmalloc (count * sizeof (int)); | |
183 | ||
184 | if (write) | |
185 | { | |
186 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
187 | ||
188 | if (addr != memaddr || len < (int)sizeof (int)) { | |
189 | /* Need part of initial word -- fetch it. */ | |
190 | buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, | |
191 | inferior_pid, (PTRACE_ARG3_TYPE) addr, 0); | |
192 | } | |
193 | ||
194 | if (count > 1) /* FIXME, avoid if even boundary */ | |
195 | { | |
196 | buffer[count - 1] | |
197 | = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, inferior_pid, | |
198 | (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)), | |
199 | 0); | |
200 | } | |
201 | ||
202 | /* Copy data to be written over corresponding part of buffer */ | |
203 | ||
204 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); | |
205 | ||
206 | /* Write the entire buffer. */ | |
207 | ||
208 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
209 | { | |
210 | int pt_status; | |
211 | int pt_request; | |
212 | /* The HP-UX kernel crashes if you use PT_WDUSER to write into the text | |
213 | segment. FIXME -- does it work to write into the data segment using | |
214 | WIUSER, or do these idiots really expect us to figure out which segment | |
215 | the address is in, so we can use a separate system call for it??! */ | |
216 | errno = 0; | |
217 | pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER; | |
218 | pt_status = call_ptrace (pt_request, | |
219 | inferior_pid, | |
220 | (PTRACE_ARG3_TYPE) addr, | |
221 | buffer[i]); | |
222 | ||
223 | /* Did we fail? Might we've guessed wrong about which | |
224 | segment this address resides in? Try the other request, | |
225 | and see if that works... | |
226 | */ | |
227 | if ((pt_status == -1) && errno) { | |
228 | errno = 0; | |
229 | pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER; | |
230 | pt_status = call_ptrace (pt_request, | |
231 | inferior_pid, | |
232 | (PTRACE_ARG3_TYPE) addr, | |
233 | buffer[i]); | |
234 | ||
235 | /* No, we still fail. Okay, time to punt. */ | |
236 | if ((pt_status == -1) && errno) | |
237 | { | |
238 | free(buffer); | |
239 | return 0; | |
240 | } | |
241 | } | |
242 | } | |
243 | } | |
244 | else | |
245 | { | |
246 | /* Read all the longwords */ | |
247 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
248 | { | |
249 | errno = 0; | |
250 | buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, | |
251 | inferior_pid, (PTRACE_ARG3_TYPE) addr, 0); | |
252 | if (errno) { | |
253 | free(buffer); | |
254 | return 0; | |
255 | } | |
256 | QUIT; | |
257 | } | |
258 | ||
259 | /* Copy appropriate bytes out of the buffer. */ | |
260 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
261 | } | |
262 | free(buffer); | |
263 | return len; | |
264 | } | |
265 | ||
266 | ||
267 | void | |
268 | child_post_follow_inferior_by_clone () | |
269 | { | |
270 | int status; | |
271 | ||
272 | /* This function is used when following both the parent and child | |
273 | of a fork. In this case, the debugger clones itself. The original | |
274 | debugger follows the parent, the clone follows the child. The | |
275 | original detaches from the child, delivering a SIGSTOP to it to | |
276 | keep it from running away until the clone can attach itself. | |
277 | ||
278 | At this point, the clone has attached to the child. Because of | |
279 | the SIGSTOP, we must now deliver a SIGCONT to the child, or it | |
280 | won't behave properly. */ | |
281 | status = kill (inferior_pid, SIGCONT); | |
282 | } | |
283 | ||
284 | ||
285 | void | |
286 | child_post_follow_vfork (parent_pid, followed_parent, child_pid, followed_child) | |
287 | int parent_pid; | |
288 | int followed_parent; | |
289 | int child_pid; | |
290 | int followed_child; | |
291 | { | |
292 | ||
293 | /* Are we a debugger that followed the parent of a vfork? If so, | |
294 | then recall that the child's vfork event was delivered to us | |
295 | first. And, that the parent was suspended by the OS until the | |
296 | child's exec or exit events were received. | |
297 | ||
298 | Upon receiving that child vfork, then, we were forced to remove | |
299 | all breakpoints in the child and continue it so that it could | |
300 | reach the exec or exit point. | |
301 | ||
302 | But also recall that the parent and child of a vfork share the | |
303 | same address space. Thus, removing bp's in the child also | |
304 | removed them from the parent. | |
305 | ||
306 | Now that the child has safely exec'd or exited, we must restore | |
307 | the parent's breakpoints before we continue it. Else, we may | |
308 | cause it run past expected stopping points. */ | |
309 | if (followed_parent) | |
310 | { | |
311 | reattach_breakpoints (parent_pid); | |
312 | } | |
313 | ||
314 | /* Are we a debugger that followed the child of a vfork? If so, | |
315 | then recall that we don't actually acquire control of the child | |
316 | until after it has exec'd or exited. | |
317 | */ | |
318 | if (followed_child) | |
319 | { | |
320 | /* If the child has exited, then there's nothing for us to do. | |
321 | In the case of an exec event, we'll let that be handled by | |
322 | the normal mechanism that notices and handles exec events, in | |
323 | resume(). */ | |
324 | ||
325 | } | |
326 | } | |
327 | ||
328 | /* Format a process id, given a pid. Be sure to terminate | |
329 | * this with a null--it's going to be printed via a "%s". | |
330 | */ | |
331 | char * | |
332 | hppa_pid_to_str( pid ) | |
333 | pid_t pid; | |
334 | { | |
335 | static char buf[30]; /* Static because address returned */ | |
336 | ||
337 | sprintf( buf, "process %d\0\0\0\0", pid ); | |
338 | /* Extra NULLs for paranoia's sake */ | |
339 | ||
340 | return buf; | |
341 | } | |
342 | ||
343 | /* Format a thread id, given a tid. Be sure to terminate | |
344 | * this with a null--it's going to be printed via a "%s". | |
345 | * | |
346 | * Note: This is a core-gdb tid, not the actual system tid. | |
347 | * See infttrace.c for details. | |
348 | */ | |
349 | char * | |
350 | hppa_tid_to_str( tid ) | |
351 | pid_t tid; | |
352 | { | |
353 | static char buf[30]; /* Static because address returned */ | |
354 | ||
355 | sprintf( buf, "system thread %d\0\0\0\0", tid ); | |
356 | /* Extra NULLs for paranoia's sake */ | |
357 | ||
358 | return buf; | |
359 | } | |
360 | ||
361 | #if !defined (GDB_NATIVE_HPUX_11) | |
362 | ||
363 | /* The following code is a substitute for the infttrace.c versions used | |
364 | with ttrace() in HPUX 11. */ | |
365 | ||
366 | /* This value is an arbitrary integer. */ | |
367 | #define PT_VERSION 123456 | |
368 | ||
369 | /* This semaphore is used to coordinate the child and parent processes | |
370 | after a fork(), and before an exec() by the child. See | |
371 | parent_attach_all for details. */ | |
372 | ||
373 | typedef struct { | |
374 | int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */ | |
375 | int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */ | |
376 | } startup_semaphore_t; | |
377 | ||
378 | #define SEM_TALK (1) | |
379 | #define SEM_LISTEN (0) | |
380 | ||
381 | static startup_semaphore_t startup_semaphore; | |
382 | ||
383 | extern int parent_attach_all PARAMS ((int, PTRACE_ARG3_TYPE, int)); | |
384 | ||
385 | #ifdef PT_SETTRC | |
386 | /* This function causes the caller's process to be traced by its | |
387 | parent. This is intended to be called after GDB forks itself, | |
388 | and before the child execs the target. | |
389 | ||
390 | Note that HP-UX ptrace is rather funky in how this is done. | |
391 | If the parent wants to get the initial exec event of a child, | |
392 | it must set the ptrace event mask of the child to include execs. | |
393 | (The child cannot do this itself.) This must be done after the | |
394 | child is forked, but before it execs. | |
395 | ||
396 | To coordinate the parent and child, we implement a semaphore using | |
397 | pipes. After SETTRC'ing itself, the child tells the parent that | |
398 | it is now traceable by the parent, and waits for the parent's | |
399 | acknowledgement. The parent can then set the child's event mask, | |
400 | and notify the child that it can now exec. | |
401 | ||
402 | (The acknowledgement by parent happens as a result of a call to | |
403 | child_acknowledge_created_inferior.) */ | |
404 | ||
405 | int | |
406 | parent_attach_all (pid, addr, data) | |
407 | int pid; | |
408 | PTRACE_ARG3_TYPE addr; | |
409 | int data; | |
410 | { | |
411 | int pt_status = 0; | |
412 | ||
413 | /* We need a memory home for a constant. */ | |
414 | int tc_magic_child = PT_VERSION; | |
415 | int tc_magic_parent = 0; | |
416 | ||
417 | /* The remainder of this function is only useful for HPUX 10.0 and | |
418 | later, as it depends upon the ability to request notification | |
419 | of specific kinds of events by the kernel. */ | |
420 | #if defined(PT_SET_EVENT_MASK) | |
421 | ||
422 | /* Notify the parent that we're potentially ready to exec(). */ | |
423 | write (startup_semaphore.child_channel[SEM_TALK], | |
424 | &tc_magic_child, | |
425 | sizeof (tc_magic_child)); | |
426 | ||
427 | /* Wait for acknowledgement from the parent. */ | |
428 | read (startup_semaphore.parent_channel[SEM_LISTEN], | |
429 | &tc_magic_parent, | |
430 | sizeof (tc_magic_parent)); | |
431 | if (tc_magic_child != tc_magic_parent) | |
432 | warning ("mismatched semaphore magic"); | |
433 | ||
434 | /* Discard our copy of the semaphore. */ | |
435 | (void) close (startup_semaphore.parent_channel[SEM_LISTEN]); | |
436 | (void) close (startup_semaphore.parent_channel[SEM_TALK]); | |
437 | (void) close (startup_semaphore.child_channel[SEM_LISTEN]); | |
438 | (void) close (startup_semaphore.child_channel[SEM_TALK]); | |
439 | #endif | |
440 | ||
441 | return 0; | |
442 | } | |
443 | #endif | |
444 | ||
445 | int | |
446 | hppa_require_attach (pid) | |
447 | int pid; | |
448 | { | |
449 | int pt_status; | |
450 | CORE_ADDR pc; | |
451 | CORE_ADDR pc_addr; | |
452 | unsigned int regs_offset; | |
453 | ||
454 | /* Are we already attached? There appears to be no explicit way to | |
455 | answer this via ptrace, so we try something which should be | |
456 | innocuous if we are attached. If that fails, then we assume | |
457 | we're not attached, and so attempt to make it so. */ | |
458 | ||
459 | errno = 0; | |
460 | regs_offset = U_REGS_OFFSET; | |
461 | pc_addr = register_addr (PC_REGNUM, regs_offset); | |
462 | pc = call_ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE) pc_addr, 0); | |
463 | ||
464 | if (errno) | |
465 | { | |
466 | errno = 0; | |
467 | pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0); | |
468 | ||
469 | if (errno) | |
470 | return -1; | |
471 | ||
472 | /* Now we really are attached. */ | |
473 | errno = 0; | |
474 | } | |
475 | attach_flag = 1; | |
476 | return pid; | |
477 | } | |
478 | ||
479 | int | |
480 | hppa_require_detach (pid, signal) | |
481 | int pid; | |
482 | int signal; | |
483 | { | |
484 | errno = 0; | |
485 | call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal); | |
486 | errno = 0; /* Ignore any errors. */ | |
487 | return pid; | |
488 | } | |
489 | ||
490 | /* Since ptrace doesn't support memory page-protection events, which | |
491 | are used to implement "hardware" watchpoints on HP-UX, these are | |
492 | dummy versions, which perform no useful work. */ | |
493 | ||
494 | void | |
495 | hppa_enable_page_protection_events (pid) | |
496 | int pid; | |
497 | { | |
498 | } | |
499 | ||
500 | void | |
501 | hppa_disable_page_protection_events (pid) | |
502 | int pid; | |
503 | { | |
504 | } | |
505 | ||
506 | int | |
507 | hppa_insert_hw_watchpoint (pid, start, len, type) | |
508 | int pid; | |
509 | CORE_ADDR start; | |
510 | LONGEST len; | |
511 | int type; | |
512 | { | |
513 | error ("Hardware watchpoints not implemented on this platform."); | |
514 | } | |
515 | ||
516 | int | |
517 | hppa_remove_hw_watchpoint (pid, start, len, type) | |
518 | int pid; | |
519 | CORE_ADDR start; | |
520 | LONGEST len; | |
521 | enum bptype type; | |
522 | { | |
523 | error ("Hardware watchpoints not implemented on this platform."); | |
524 | } | |
525 | ||
526 | int | |
527 | hppa_can_use_hw_watchpoint (type, cnt, ot) | |
528 | enum bptype type; | |
529 | int cnt; | |
530 | enum bptype ot; | |
531 | { | |
532 | return 0; | |
533 | } | |
534 | ||
535 | int | |
536 | hppa_range_profitable_for_hw_watchpoint (pid, start, len) | |
537 | int pid; | |
538 | CORE_ADDR start; | |
539 | LONGEST len; | |
540 | { | |
541 | error ("Hardware watchpoints not implemented on this platform."); | |
542 | } | |
543 | ||
544 | char * | |
545 | hppa_pid_or_tid_to_str (id) | |
546 | pid_t id; | |
547 | { | |
548 | /* In the ptrace world, there are only processes. */ | |
549 | return hppa_pid_to_str (id); | |
550 | } | |
551 | ||
552 | /* This function has no meaning in a non-threaded world. Thus, we | |
553 | return 0 (FALSE). See the use of "hppa_prepare_to_proceed" in | |
554 | hppa-tdep.c. */ | |
555 | ||
556 | pid_t | |
557 | hppa_switched_threads (pid) | |
558 | pid_t pid; | |
559 | { | |
560 | return (pid_t) 0; | |
561 | } | |
562 | ||
563 | void | |
564 | hppa_ensure_vforking_parent_remains_stopped (pid) | |
565 | int pid; | |
566 | { | |
567 | /* This assumes that the vforked parent is presently stopped, and | |
568 | that the vforked child has just delivered its first exec event. | |
569 | Calling kill() this way will cause the SIGTRAP to be delivered as | |
570 | soon as the parent is resumed, which happens as soon as the | |
571 | vforked child is resumed. See wait_for_inferior for the use of | |
572 | this function. */ | |
573 | kill (pid, SIGTRAP); | |
574 | } | |
575 | ||
576 | int | |
577 | hppa_resume_execd_vforking_child_to_get_parent_vfork () | |
578 | { | |
579 | return 1; /* Yes, the child must be resumed. */ | |
580 | } | |
581 | ||
582 | void | |
583 | require_notification_of_events (pid) | |
584 | int pid; | |
585 | { | |
586 | #if defined(PT_SET_EVENT_MASK) | |
587 | int pt_status; | |
588 | ptrace_event_t ptrace_events; | |
589 | ||
590 | /* Instruct the kernel as to the set of events we wish to be | |
591 | informed of. (This support does not exist before HPUX 10.0. | |
592 | We'll assume if PT_SET_EVENT_MASK has not been defined by | |
593 | <sys/ptrace.h>, then we're being built on pre-10.0.) | |
594 | */ | |
595 | memset (&ptrace_events, 0, sizeof (ptrace_events)); | |
596 | ||
597 | /* Note: By default, all signals are visible to us. If we wish | |
598 | the kernel to keep certain signals hidden from us, we do it | |
599 | by calling sigdelset (ptrace_events.pe_signals, signal) for | |
600 | each such signal here, before doing PT_SET_EVENT_MASK. | |
601 | */ | |
602 | sigemptyset (&ptrace_events.pe_signals); | |
603 | ||
604 | ptrace_events.pe_set_event = 0; | |
605 | ||
606 | ptrace_events.pe_set_event |= PTRACE_SIGNAL; | |
607 | ptrace_events.pe_set_event |= PTRACE_EXEC; | |
608 | ptrace_events.pe_set_event |= PTRACE_FORK; | |
609 | ptrace_events.pe_set_event |= PTRACE_VFORK; | |
610 | /* ??rehrauer: Add this one when we're prepared to catch it... | |
611 | ptrace_events.pe_set_event |= PTRACE_EXIT; | |
612 | */ | |
613 | ||
614 | errno = 0; | |
615 | pt_status = call_ptrace (PT_SET_EVENT_MASK, | |
616 | pid, | |
617 | (PTRACE_ARG3_TYPE) &ptrace_events, | |
618 | sizeof (ptrace_events)); | |
619 | if (errno) | |
620 | perror_with_name ("ptrace"); | |
621 | if (pt_status < 0) | |
622 | return; | |
623 | #endif | |
624 | } | |
625 | ||
626 | void | |
627 | require_notification_of_exec_events (pid) | |
628 | int pid; | |
629 | { | |
630 | #if defined(PT_SET_EVENT_MASK) | |
631 | int pt_status; | |
632 | ptrace_event_t ptrace_events; | |
633 | ||
634 | /* Instruct the kernel as to the set of events we wish to be | |
635 | informed of. (This support does not exist before HPUX 10.0. | |
636 | We'll assume if PT_SET_EVENT_MASK has not been defined by | |
637 | <sys/ptrace.h>, then we're being built on pre-10.0.) | |
638 | */ | |
639 | memset (&ptrace_events, 0, sizeof (ptrace_events)); | |
640 | ||
641 | /* Note: By default, all signals are visible to us. If we wish | |
642 | the kernel to keep certain signals hidden from us, we do it | |
643 | by calling sigdelset (ptrace_events.pe_signals, signal) for | |
644 | each such signal here, before doing PT_SET_EVENT_MASK. | |
645 | */ | |
646 | sigemptyset (&ptrace_events.pe_signals); | |
647 | ||
648 | ptrace_events.pe_set_event = 0; | |
649 | ||
650 | ptrace_events.pe_set_event |= PTRACE_EXEC; | |
651 | /* ??rehrauer: Add this one when we're prepared to catch it... | |
652 | ptrace_events.pe_set_event |= PTRACE_EXIT; | |
653 | */ | |
654 | ||
655 | errno = 0; | |
656 | pt_status = call_ptrace (PT_SET_EVENT_MASK, | |
657 | pid, | |
658 | (PTRACE_ARG3_TYPE) &ptrace_events, | |
659 | sizeof (ptrace_events)); | |
660 | if (errno) | |
661 | perror_with_name ("ptrace"); | |
662 | if (pt_status < 0) | |
663 | return; | |
664 | #endif | |
665 | } | |
666 | ||
667 | /* This function is called by the parent process, with pid being the | |
668 | ID of the child process, after the debugger has forked. */ | |
669 | ||
670 | void | |
671 | child_acknowledge_created_inferior (pid) | |
672 | int pid; | |
673 | { | |
674 | /* We need a memory home for a constant. */ | |
675 | int tc_magic_parent = PT_VERSION; | |
676 | int tc_magic_child = 0; | |
677 | ||
678 | /* Wait for the child to tell us that it has forked. */ | |
679 | read (startup_semaphore.child_channel[SEM_LISTEN], | |
680 | &tc_magic_child, | |
681 | sizeof(tc_magic_child)); | |
682 | ||
683 | /* Notify the child that it can exec. | |
684 | ||
685 | In the infttrace.c variant of this function, we set the child's | |
686 | event mask after the fork but before the exec. In the ptrace | |
687 | world, it seems we can't set the event mask until after the exec. */ | |
688 | ||
689 | write (startup_semaphore.parent_channel[SEM_TALK], | |
690 | &tc_magic_parent, | |
691 | sizeof (tc_magic_parent)); | |
692 | ||
693 | /* We'd better pause a bit before trying to set the event mask, | |
694 | though, to ensure that the exec has happened. We don't want to | |
695 | wait() on the child, because that'll screw up the upper layers | |
696 | of gdb's execution control that expect to see the exec event. | |
697 | ||
698 | After an exec, the child is no longer executing gdb code. Hence, | |
699 | we can't have yet another synchronization via the pipes. We'll | |
700 | just sleep for a second, and hope that's enough delay... */ | |
701 | ||
702 | sleep (1); | |
703 | ||
704 | /* Instruct the kernel as to the set of events we wish to be | |
705 | informed of. */ | |
706 | ||
707 | require_notification_of_exec_events (pid); | |
708 | ||
709 | /* Discard our copy of the semaphore. */ | |
710 | (void) close (startup_semaphore.parent_channel[SEM_LISTEN]); | |
711 | (void) close (startup_semaphore.parent_channel[SEM_TALK]); | |
712 | (void) close (startup_semaphore.child_channel[SEM_LISTEN]); | |
713 | (void) close (startup_semaphore.child_channel[SEM_TALK]); | |
714 | } | |
715 | ||
716 | void | |
717 | child_post_startup_inferior (pid) | |
718 | int pid; | |
719 | ||
720 | { | |
721 | require_notification_of_events (pid); | |
722 | } | |
723 | ||
724 | void | |
725 | child_post_attach (pid) | |
726 | int pid; | |
727 | { | |
728 | require_notification_of_events (pid); | |
729 | } | |
730 | ||
731 | int | |
732 | child_insert_fork_catchpoint (pid) | |
733 | int pid; | |
734 | { | |
735 | /* This request is only available on HPUX 10.0 and later. */ | |
736 | #if !defined(PT_SET_EVENT_MASK) | |
737 | error ("Unable to catch forks prior to HPUX 10.0"); | |
738 | #else | |
739 | /* Enable reporting of fork events from the kernel. */ | |
740 | /* ??rehrauer: For the moment, we're always enabling these events, | |
741 | and just ignoring them if there's no catchpoint to catch them. | |
742 | */ | |
743 | return 0; | |
744 | #endif | |
745 | } | |
746 | ||
747 | int | |
748 | child_remove_fork_catchpoint (pid) | |
749 | int pid; | |
750 | { | |
751 | /* This request is only available on HPUX 10.0 and later. */ | |
752 | #if !defined(PT_SET_EVENT_MASK) | |
753 | error ("Unable to catch forks prior to HPUX 10.0"); | |
754 | #else | |
755 | /* Disable reporting of fork events from the kernel. */ | |
756 | /* ??rehrauer: For the moment, we're always enabling these events, | |
757 | and just ignoring them if there's no catchpoint to catch them. */ | |
758 | return 0; | |
759 | #endif | |
760 | } | |
761 | ||
762 | int | |
763 | child_insert_vfork_catchpoint (pid) | |
764 | int pid; | |
765 | { | |
766 | /* This request is only available on HPUX 10.0 and later. */ | |
767 | #if !defined(PT_SET_EVENT_MASK) | |
768 | error ("Unable to catch vforks prior to HPUX 10.0"); | |
769 | #else | |
770 | /* Enable reporting of vfork events from the kernel. */ | |
771 | /* ??rehrauer: For the moment, we're always enabling these events, | |
772 | and just ignoring them if there's no catchpoint to catch them. */ | |
773 | return 0; | |
774 | #endif | |
775 | } | |
776 | ||
777 | int | |
778 | child_remove_vfork_catchpoint (pid) | |
779 | int pid; | |
780 | { | |
781 | /* This request is only available on HPUX 10.0 and later. */ | |
782 | #if !defined(PT_SET_EVENT_MASK) | |
783 | error ("Unable to catch vforks prior to HPUX 10.0"); | |
784 | #else | |
785 | /* Disable reporting of vfork events from the kernel. */ | |
786 | /* ??rehrauer: For the moment, we're always enabling these events, | |
787 | and just ignoring them if there's no catchpoint to catch them. */ | |
788 | return 0; | |
789 | #endif | |
790 | } | |
791 | ||
792 | int | |
793 | child_has_forked (pid, childpid) | |
794 | int pid; | |
795 | int * childpid; | |
796 | { | |
797 | /* This request is only available on HPUX 10.0 and later. */ | |
798 | #if !defined(PT_GET_PROCESS_STATE) | |
799 | *childpid = 0; | |
800 | return 0; | |
801 | #else | |
802 | int pt_status; | |
803 | ptrace_state_t ptrace_state; | |
804 | ||
805 | errno = 0; | |
806 | pt_status = call_ptrace (PT_GET_PROCESS_STATE, | |
807 | pid, | |
808 | (PTRACE_ARG3_TYPE) &ptrace_state, | |
809 | sizeof (ptrace_state)); | |
810 | if (errno) | |
811 | perror_with_name ("ptrace"); | |
812 | if (pt_status < 0) | |
813 | return 0; | |
814 | ||
815 | if (ptrace_state.pe_report_event & PTRACE_FORK) | |
816 | { | |
817 | *childpid = ptrace_state.pe_other_pid; | |
818 | return 1; | |
819 | } | |
820 | ||
821 | return 0; | |
822 | #endif | |
823 | } | |
824 | ||
825 | int | |
826 | child_has_vforked (pid, childpid) | |
827 | int pid; | |
828 | int * childpid; | |
829 | { | |
830 | /* This request is only available on HPUX 10.0 and later. */ | |
831 | #if !defined(PT_GET_PROCESS_STATE) | |
832 | *childpid = 0; | |
833 | return 0; | |
834 | ||
835 | #else | |
836 | int pt_status; | |
837 | ptrace_state_t ptrace_state; | |
838 | ||
839 | errno = 0; | |
840 | pt_status = call_ptrace (PT_GET_PROCESS_STATE, | |
841 | pid, | |
842 | (PTRACE_ARG3_TYPE) &ptrace_state, | |
843 | sizeof (ptrace_state)); | |
844 | if (errno) | |
845 | perror_with_name ("ptrace"); | |
846 | if (pt_status < 0) | |
847 | return 0; | |
848 | ||
849 | if (ptrace_state.pe_report_event & PTRACE_VFORK) | |
850 | { | |
851 | *childpid = ptrace_state.pe_other_pid; | |
852 | return 1; | |
853 | } | |
854 | ||
855 | return 0; | |
856 | #endif | |
857 | } | |
858 | ||
859 | int | |
860 | child_can_follow_vfork_prior_to_exec () | |
861 | { | |
862 | /* ptrace doesn't allow this. */ | |
863 | return 0; | |
864 | } | |
865 | ||
866 | int | |
867 | child_insert_exec_catchpoint (pid) | |
868 | int pid; | |
869 | { | |
870 | /* This request is only available on HPUX 10.0 and later. | |
871 | */ | |
872 | #if !defined(PT_SET_EVENT_MASK) | |
873 | error ("Unable to catch execs prior to HPUX 10.0"); | |
874 | ||
875 | #else | |
876 | /* Enable reporting of exec events from the kernel. */ | |
877 | /* ??rehrauer: For the moment, we're always enabling these events, | |
878 | and just ignoring them if there's no catchpoint to catch them. | |
879 | */ | |
880 | return 0; | |
881 | #endif | |
882 | } | |
883 | ||
884 | int | |
885 | child_remove_exec_catchpoint (pid) | |
886 | int pid; | |
887 | { | |
888 | /* This request is only available on HPUX 10.0 and later. | |
889 | */ | |
890 | #if !defined(PT_SET_EVENT_MASK) | |
891 | error ("Unable to catch execs prior to HPUX 10.0"); | |
892 | ||
893 | #else | |
894 | /* Disable reporting of exec events from the kernel. */ | |
895 | /* ??rehrauer: For the moment, we're always enabling these events, | |
896 | and just ignoring them if there's no catchpoint to catch them. | |
897 | */ | |
898 | return 0; | |
899 | #endif | |
900 | } | |
901 | ||
902 | int | |
903 | child_has_execd (pid, execd_pathname) | |
904 | int pid; | |
905 | char ** execd_pathname; | |
906 | { | |
907 | ||
908 | /* This request is only available on HPUX 10.0 and later. | |
909 | */ | |
910 | #if !defined(PT_GET_PROCESS_STATE) | |
911 | *execd_pathname = NULL; | |
912 | return 0; | |
913 | ||
914 | #else | |
915 | int pt_status; | |
916 | ptrace_state_t ptrace_state; | |
917 | ||
918 | errno = 0; | |
919 | pt_status = call_ptrace (PT_GET_PROCESS_STATE, | |
920 | pid, | |
921 | (PTRACE_ARG3_TYPE) &ptrace_state, | |
922 | sizeof (ptrace_state)); | |
923 | if (errno) | |
924 | perror_with_name ("ptrace"); | |
925 | if (pt_status < 0) | |
926 | return 0; | |
927 | ||
928 | if (ptrace_state.pe_report_event & PTRACE_EXEC) | |
929 | { | |
930 | char * exec_file = target_pid_to_exec_file (pid); | |
931 | *execd_pathname = savestring (exec_file, strlen (exec_file)); | |
932 | return 1; | |
933 | } | |
934 | ||
935 | return 0; | |
936 | #endif | |
937 | } | |
938 | ||
939 | int | |
940 | child_reported_exec_events_per_exec_call () | |
941 | { | |
942 | return 2; /* ptrace reports the event twice per call. */ | |
943 | } | |
944 | ||
945 | int | |
946 | child_has_syscall_event (pid, kind, syscall_id) | |
947 | int pid; | |
948 | enum target_waitkind *kind; | |
949 | int *syscall_id; | |
950 | { | |
951 | /* This request is only available on HPUX 10.30 and later, via | |
952 | the ttrace interface. */ | |
953 | ||
954 | *kind = TARGET_WAITKIND_SPURIOUS; | |
955 | *syscall_id = -1; | |
956 | return 0; | |
957 | } | |
958 | ||
959 | char * | |
960 | child_pid_to_exec_file (pid) | |
961 | int pid; | |
962 | { | |
963 | static char exec_file_buffer[1024]; | |
964 | int pt_status; | |
965 | CORE_ADDR top_of_stack; | |
966 | char four_chars[4]; | |
967 | int name_index; | |
968 | int i; | |
969 | int saved_inferior_pid; | |
970 | boolean done; | |
971 | ||
972 | #ifdef PT_GET_PROCESS_PATHNAME | |
973 | /* As of 10.x HP-UX, there's an explicit request to get the pathname. */ | |
974 | pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME, | |
975 | pid, | |
976 | (PTRACE_ARG3_TYPE) exec_file_buffer, | |
977 | sizeof (exec_file_buffer) - 1); | |
978 | if (pt_status == 0) | |
979 | return exec_file_buffer; | |
980 | #endif | |
981 | ||
982 | /* It appears that this request is broken prior to 10.30. | |
983 | If it fails, try a really, truly amazingly gross hack | |
984 | that DDE uses, of pawing through the process' data | |
985 | segment to find the pathname. */ | |
986 | ||
987 | top_of_stack = 0x7b03a000; | |
988 | name_index = 0; | |
989 | done = 0; | |
990 | ||
991 | /* On the chance that pid != inferior_pid, set inferior_pid | |
992 | to pid, so that (grrrr!) implicit uses of inferior_pid get | |
993 | the right id. */ | |
994 | ||
995 | saved_inferior_pid = inferior_pid; | |
996 | inferior_pid = pid; | |
997 | ||
998 | /* Try to grab a null-terminated string. */ | |
999 | while (! done) | |
1000 | { | |
1001 | if (target_read_memory (top_of_stack, four_chars, 4) != 0) | |
1002 | { | |
1003 | inferior_pid = saved_inferior_pid; | |
1004 | return NULL; | |
1005 | } | |
1006 | for (i = 0; i < 4; i++) | |
1007 | { | |
1008 | exec_file_buffer[name_index++] = four_chars[i]; | |
1009 | done = (four_chars[i] == '\0'); | |
1010 | if (done) | |
1011 | break; | |
1012 | } | |
1013 | top_of_stack += 4; | |
1014 | } | |
1015 | ||
1016 | if (exec_file_buffer[0] == '\0') | |
1017 | { | |
1018 | inferior_pid = saved_inferior_pid; | |
1019 | return NULL; | |
1020 | } | |
1021 | ||
1022 | inferior_pid = saved_inferior_pid; | |
1023 | return exec_file_buffer; | |
1024 | } | |
1025 | ||
1026 | void | |
1027 | pre_fork_inferior () | |
1028 | { | |
1029 | int status; | |
1030 | ||
1031 | status = pipe (startup_semaphore.parent_channel); | |
1032 | if (status < 0) | |
1033 | { | |
1034 | warning ("error getting parent pipe for startup semaphore"); | |
1035 | return; | |
1036 | } | |
1037 | ||
1038 | status = pipe (startup_semaphore.child_channel); | |
1039 | if (status < 0) | |
1040 | { | |
1041 | warning ("error getting child pipe for startup semaphore"); | |
1042 | return; | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | \f | |
1047 | /* Check to see if the given thread is alive. | |
1048 | ||
1049 | This is a no-op, as ptrace doesn't support threads, so we just | |
1050 | return "TRUE". */ | |
1051 | ||
1052 | int | |
1053 | child_thread_alive (pid) | |
1054 | int pid; | |
1055 | { | |
1056 | return 1; | |
1057 | } | |
1058 | ||
1059 | #endif /* ! GDB_NATIVE_HPUX_11 */ |