Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Native support code for HPUX PA-RISC. |
b6ba6518 KB |
2 | Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, |
3 | 1998, 1999, 2000, 2001 | |
c906108c SS |
4 | Free Software Foundation, Inc. |
5 | ||
6 | Contributed by the Center for Software Science at the | |
7 | University of Utah (pa-gdb-bugs@cs.utah.edu). | |
8 | ||
c5aa993b | 9 | This file is part of GDB. |
c906108c | 10 | |
c5aa993b JM |
11 | This program is free software; you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License as published by | |
13 | the Free Software Foundation; either version 2 of the License, or | |
14 | (at your option) any later version. | |
c906108c | 15 | |
c5aa993b JM |
16 | This program is distributed in the hope that it will be useful, |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
c906108c | 20 | |
c5aa993b JM |
21 | You should have received a copy of the GNU General Public License |
22 | along with this program; if not, write to the Free Software | |
23 | Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
25 | |
26 | ||
27 | #include "defs.h" | |
28 | #include "inferior.h" | |
29 | #include "target.h" | |
30 | #include <sys/ptrace.h> | |
31 | #include "gdbcore.h" | |
a526d713 | 32 | #include "gdb_wait.h" |
4e052eda | 33 | #include "regcache.h" |
65e82032 | 34 | #include "gdb_string.h" |
6d518969 | 35 | #include "infttrace.h" |
c906108c SS |
36 | #include <signal.h> |
37 | ||
47932f85 | 38 | |
b9fbf434 AC |
39 | static CORE_ADDR text_end; |
40 | ||
41 | void | |
42 | deprecated_hpux_text_end (struct target_ops *exec_ops) | |
43 | { | |
44 | struct section_table *p; | |
45 | ||
46 | /* Set text_end to the highest address of the end of any readonly | |
47 | code section. */ | |
48 | /* FIXME: The comment above does not match the code. The code | |
49 | checks for sections with are either code *or* readonly. */ | |
50 | text_end = (CORE_ADDR) 0; | |
51 | for (p = exec_ops->to_sections; p < exec_ops->to_sections_end; p++) | |
52 | if (bfd_get_section_flags (p->bfd, p->the_bfd_section) | |
53 | & (SEC_CODE | SEC_READONLY)) | |
54 | { | |
55 | if (text_end < p->endaddr) | |
56 | text_end = p->endaddr; | |
57 | } | |
58 | } | |
59 | ||
60 | ||
a14ed312 | 61 | static void fetch_register (int); |
c906108c SS |
62 | |
63 | void | |
fba45db2 | 64 | fetch_inferior_registers (int regno) |
c906108c SS |
65 | { |
66 | if (regno == -1) | |
67 | for (regno = 0; regno < NUM_REGS; regno++) | |
68 | fetch_register (regno); | |
69 | else | |
70 | fetch_register (regno); | |
71 | } | |
72 | ||
7be570e7 JM |
73 | /* Our own version of the offsetof macro, since we can't assume ANSI C. */ |
74 | #define HPPAH_OFFSETOF(type, member) ((int) (&((type *) 0)->member)) | |
75 | ||
c906108c SS |
76 | /* Store our register values back into the inferior. |
77 | If REGNO is -1, do this for all registers. | |
78 | Otherwise, REGNO specifies which register (so we can save time). */ | |
79 | ||
80 | void | |
fba45db2 | 81 | store_inferior_registers (int regno) |
c906108c | 82 | { |
52f0bd74 | 83 | unsigned int regaddr; |
c906108c | 84 | char buf[80]; |
52f0bd74 | 85 | int i; |
c906108c SS |
86 | unsigned int offset = U_REGS_OFFSET; |
87 | int scratch; | |
88 | ||
89 | if (regno >= 0) | |
90 | { | |
7be570e7 JM |
91 | unsigned int addr, len, offset; |
92 | ||
c906108c SS |
93 | if (CANNOT_STORE_REGISTER (regno)) |
94 | return; | |
7be570e7 JM |
95 | |
96 | offset = 0; | |
12c266ea | 97 | len = DEPRECATED_REGISTER_RAW_SIZE (regno); |
7be570e7 JM |
98 | |
99 | /* Requests for register zero actually want the save_state's | |
100 | ss_flags member. As RM says: "Oh, what a hack!" */ | |
101 | if (regno == 0) | |
b83266a0 | 102 | { |
7be570e7 JM |
103 | save_state_t ss; |
104 | addr = HPPAH_OFFSETOF (save_state_t, ss_flags); | |
105 | len = sizeof (ss.ss_flags); | |
106 | ||
107 | /* Note that ss_flags is always an int, no matter what | |
12c266ea AC |
108 | DEPRECATED_REGISTER_RAW_SIZE(0) says. Assuming all HP-UX |
109 | PA machines are big-endian, put it at the least | |
110 | significant end of the value, and zap the rest of the | |
111 | buffer. */ | |
112 | offset = DEPRECATED_REGISTER_RAW_SIZE (0) - len; | |
7be570e7 JM |
113 | } |
114 | ||
115 | /* Floating-point registers come from the ss_fpblock area. */ | |
116 | else if (regno >= FP0_REGNUM) | |
117 | addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock) | |
62700349 | 118 | + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (FP0_REGNUM))); |
7be570e7 JM |
119 | |
120 | /* Wide registers come from the ss_wide area. | |
121 | I think it's more PC to test (ss_flags & SS_WIDEREGS) to select | |
122 | between ss_wide and ss_narrow than to use the raw register size. | |
123 | But checking ss_flags would require an extra ptrace call for | |
124 | every register reference. Bleah. */ | |
125 | else if (len == 8) | |
126 | addr = (HPPAH_OFFSETOF (save_state_t, ss_wide) | |
62700349 | 127 | + DEPRECATED_REGISTER_BYTE (regno)); |
7be570e7 JM |
128 | |
129 | /* Narrow registers come from the ss_narrow area. Note that | |
130 | ss_narrow starts with gr1, not gr0. */ | |
131 | else if (len == 4) | |
132 | addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow) | |
62700349 | 133 | + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (1))); |
7be570e7 | 134 | else |
8e65ff28 AC |
135 | internal_error (__FILE__, __LINE__, |
136 | "hppah-nat.c (write_register): unexpected register size"); | |
7be570e7 JM |
137 | |
138 | #ifdef GDB_TARGET_IS_HPPA_20W | |
139 | /* Unbelieveable. The PC head and tail must be written in 64bit hunks | |
140 | or we will get an error. Worse yet, the oddball ptrace/ttrace | |
141 | layering will not allow us to perform a 64bit register store. | |
142 | ||
143 | What a crock. */ | |
144 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM && len == 8) | |
145 | { | |
146 | CORE_ADDR temp; | |
147 | ||
62700349 | 148 | temp = *(CORE_ADDR *)&deprecated_registers[DEPRECATED_REGISTER_BYTE (regno)]; |
7be570e7 JM |
149 | |
150 | /* Set the priv level (stored in the low two bits of the PC. */ | |
151 | temp |= 0x3; | |
152 | ||
39f77062 KB |
153 | ttrace_write_reg_64 (PIDGET (inferior_ptid), (CORE_ADDR)addr, |
154 | (CORE_ADDR)&temp); | |
7be570e7 JM |
155 | |
156 | /* If we fail to write the PC, give a true error instead of | |
157 | just a warning. */ | |
b83266a0 SS |
158 | if (errno != 0) |
159 | { | |
7be570e7 JM |
160 | char *err = safe_strerror (errno); |
161 | char *msg = alloca (strlen (err) + 128); | |
162 | sprintf (msg, "writing `%s' register: %s", | |
163 | REGISTER_NAME (regno), err); | |
164 | perror_with_name (msg); | |
b83266a0 | 165 | } |
7be570e7 | 166 | return; |
b83266a0 | 167 | } |
53a5351d JM |
168 | |
169 | /* Another crock. HPUX complains if you write a nonzero value to | |
170 | the high part of IPSW. What will it take for HP to catch a | |
171 | clue about building sensible interfaces? */ | |
172 | if (regno == IPSW_REGNUM && len == 8) | |
62700349 | 173 | *(int *)&deprecated_registers[DEPRECATED_REGISTER_BYTE (regno)] = 0; |
7be570e7 JM |
174 | #endif |
175 | ||
176 | for (i = 0; i < len; i += sizeof (int)) | |
177 | { | |
178 | errno = 0; | |
39f77062 KB |
179 | call_ptrace (PT_WUREGS, PIDGET (inferior_ptid), |
180 | (PTRACE_ARG3_TYPE) addr + i, | |
62700349 | 181 | *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (regno) + i]); |
7be570e7 JM |
182 | if (errno != 0) |
183 | { | |
184 | /* Warning, not error, in case we are attached; sometimes | |
185 | the kernel doesn't let us at the registers. */ | |
186 | char *err = safe_strerror (errno); | |
187 | char *msg = alloca (strlen (err) + 128); | |
53a5351d | 188 | sprintf (msg, "writing `%s' register: %s", |
7be570e7 JM |
189 | REGISTER_NAME (regno), err); |
190 | /* If we fail to write the PC, give a true error instead of | |
191 | just a warning. */ | |
192 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM) | |
193 | perror_with_name (msg); | |
194 | else | |
c906108c | 195 | warning (msg); |
7be570e7 JM |
196 | return; |
197 | } | |
198 | } | |
c906108c SS |
199 | } |
200 | else | |
201 | for (regno = 0; regno < NUM_REGS; regno++) | |
202 | store_inferior_registers (regno); | |
203 | } | |
204 | ||
c906108c | 205 | |
adf40b2e | 206 | /* Fetch a register's value from the process's U area. */ |
c906108c | 207 | static void |
fba45db2 | 208 | fetch_register (int regno) |
c906108c | 209 | { |
123a958e | 210 | char buf[MAX_REGISTER_SIZE]; |
adf40b2e JM |
211 | unsigned int addr, len, offset; |
212 | int i; | |
c906108c | 213 | |
adf40b2e | 214 | offset = 0; |
12c266ea | 215 | len = DEPRECATED_REGISTER_RAW_SIZE (regno); |
adf40b2e JM |
216 | |
217 | /* Requests for register zero actually want the save_state's | |
218 | ss_flags member. As RM says: "Oh, what a hack!" */ | |
219 | if (regno == 0) | |
220 | { | |
221 | save_state_t ss; | |
222 | addr = HPPAH_OFFSETOF (save_state_t, ss_flags); | |
223 | len = sizeof (ss.ss_flags); | |
224 | ||
225 | /* Note that ss_flags is always an int, no matter what | |
12c266ea AC |
226 | DEPRECATED_REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA |
227 | machines are big-endian, put it at the least significant end | |
228 | of the value, and zap the rest of the buffer. */ | |
229 | offset = DEPRECATED_REGISTER_RAW_SIZE (0) - len; | |
adf40b2e JM |
230 | memset (buf, 0, sizeof (buf)); |
231 | } | |
c906108c | 232 | |
adf40b2e JM |
233 | /* Floating-point registers come from the ss_fpblock area. */ |
234 | else if (regno >= FP0_REGNUM) | |
235 | addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock) | |
62700349 | 236 | + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (FP0_REGNUM))); |
adf40b2e JM |
237 | |
238 | /* Wide registers come from the ss_wide area. | |
239 | I think it's more PC to test (ss_flags & SS_WIDEREGS) to select | |
240 | between ss_wide and ss_narrow than to use the raw register size. | |
241 | But checking ss_flags would require an extra ptrace call for | |
242 | every register reference. Bleah. */ | |
243 | else if (len == 8) | |
244 | addr = (HPPAH_OFFSETOF (save_state_t, ss_wide) | |
62700349 | 245 | + DEPRECATED_REGISTER_BYTE (regno)); |
adf40b2e JM |
246 | |
247 | /* Narrow registers come from the ss_narrow area. Note that | |
248 | ss_narrow starts with gr1, not gr0. */ | |
249 | else if (len == 4) | |
250 | addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow) | |
62700349 | 251 | + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (1))); |
c906108c | 252 | |
adf40b2e | 253 | else |
8e65ff28 AC |
254 | internal_error (__FILE__, __LINE__, |
255 | "hppa-nat.c (fetch_register): unexpected register size"); | |
adf40b2e JM |
256 | |
257 | for (i = 0; i < len; i += sizeof (int)) | |
c906108c SS |
258 | { |
259 | errno = 0; | |
adf40b2e JM |
260 | /* Copy an int from the U area to buf. Fill the least |
261 | significant end if len != raw_size. */ | |
262 | * (int *) &buf[offset + i] = | |
39f77062 | 263 | call_ptrace (PT_RUREGS, PIDGET (inferior_ptid), |
adf40b2e | 264 | (PTRACE_ARG3_TYPE) addr + i, 0); |
c906108c SS |
265 | if (errno != 0) |
266 | { | |
adf40b2e JM |
267 | /* Warning, not error, in case we are attached; sometimes |
268 | the kernel doesn't let us at the registers. */ | |
c906108c SS |
269 | char *err = safe_strerror (errno); |
270 | char *msg = alloca (strlen (err) + 128); | |
adf40b2e JM |
271 | sprintf (msg, "reading `%s' register: %s", |
272 | REGISTER_NAME (regno), err); | |
c906108c | 273 | warning (msg); |
adf40b2e | 274 | return; |
c906108c SS |
275 | } |
276 | } | |
adf40b2e JM |
277 | |
278 | /* If we're reading an address from the instruction address queue, | |
279 | mask out the bottom two bits --- they contain the privilege | |
280 | level. */ | |
c906108c | 281 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM) |
adf40b2e JM |
282 | buf[len - 1] &= ~0x3; |
283 | ||
c906108c | 284 | supply_register (regno, buf); |
c906108c SS |
285 | } |
286 | ||
adf40b2e | 287 | |
c906108c SS |
288 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR |
289 | to debugger memory starting at MYADDR. Copy to inferior if | |
290 | WRITE is nonzero. | |
c5aa993b | 291 | |
c906108c SS |
292 | Returns the length copied, which is either the LEN argument or zero. |
293 | This xfer function does not do partial moves, since child_ops | |
294 | doesn't allow memory operations to cross below us in the target stack | |
8fef05cc | 295 | anyway. TARGET is ignored. */ |
c906108c SS |
296 | |
297 | int | |
8fef05cc | 298 | child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, |
240be855 | 299 | struct mem_attrib *mem, |
8fef05cc | 300 | struct target_ops *target) |
c906108c | 301 | { |
52f0bd74 | 302 | int i; |
c906108c | 303 | /* Round starting address down to longword boundary. */ |
52f0bd74 | 304 | CORE_ADDR addr = memaddr & - (CORE_ADDR)(sizeof (int)); |
c906108c | 305 | /* Round ending address up; get number of longwords that makes. */ |
52f0bd74 | 306 | int count |
c5aa993b | 307 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); |
c906108c | 308 | |
b83266a0 SS |
309 | /* Allocate buffer of that many longwords. |
310 | Note -- do not use alloca to allocate this buffer since there is no | |
311 | guarantee of when the buffer will actually be deallocated. | |
312 | ||
313 | This routine can be called over and over with the same call chain; | |
314 | this (in effect) would pile up all those alloca requests until a call | |
315 | to alloca was made from a point higher than this routine in the | |
316 | call chain. */ | |
52f0bd74 | 317 | int *buffer = (int *) xmalloc (count * sizeof (int)); |
c906108c SS |
318 | |
319 | if (write) | |
320 | { | |
321 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
c5aa993b | 322 | if (addr != memaddr || len < (int) sizeof (int)) |
b83266a0 SS |
323 | { |
324 | /* Need part of initial word -- fetch it. */ | |
c5aa993b | 325 | buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, |
39f77062 KB |
326 | PIDGET (inferior_ptid), |
327 | (PTRACE_ARG3_TYPE) addr, 0); | |
b83266a0 | 328 | } |
c906108c SS |
329 | |
330 | if (count > 1) /* FIXME, avoid if even boundary */ | |
331 | { | |
332 | buffer[count - 1] | |
b83266a0 | 333 | = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, |
39f77062 | 334 | PIDGET (inferior_ptid), |
b83266a0 SS |
335 | (PTRACE_ARG3_TYPE) (addr |
336 | + (count - 1) * sizeof (int)), | |
337 | 0); | |
c906108c SS |
338 | } |
339 | ||
340 | /* Copy data to be written over corresponding part of buffer */ | |
c906108c SS |
341 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); |
342 | ||
343 | /* Write the entire buffer. */ | |
c906108c SS |
344 | for (i = 0; i < count; i++, addr += sizeof (int)) |
345 | { | |
b83266a0 SS |
346 | int pt_status; |
347 | int pt_request; | |
348 | /* The HP-UX kernel crashes if you use PT_WDUSER to write into the | |
349 | text segment. FIXME -- does it work to write into the data | |
350 | segment using WIUSER, or do these idiots really expect us to | |
351 | figure out which segment the address is in, so we can use a | |
352 | separate system call for it??! */ | |
c906108c | 353 | errno = 0; |
b83266a0 | 354 | pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER; |
c906108c | 355 | pt_status = call_ptrace (pt_request, |
39f77062 | 356 | PIDGET (inferior_ptid), |
b83266a0 SS |
357 | (PTRACE_ARG3_TYPE) addr, |
358 | buffer[i]); | |
359 | ||
360 | /* Did we fail? Might we've guessed wrong about which | |
361 | segment this address resides in? Try the other request, | |
362 | and see if that works... */ | |
363 | if ((pt_status == -1) && errno) | |
364 | { | |
365 | errno = 0; | |
366 | pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER; | |
367 | pt_status = call_ptrace (pt_request, | |
39f77062 | 368 | PIDGET (inferior_ptid), |
b83266a0 SS |
369 | (PTRACE_ARG3_TYPE) addr, |
370 | buffer[i]); | |
371 | ||
372 | /* No, we still fail. Okay, time to punt. */ | |
373 | if ((pt_status == -1) && errno) | |
374 | { | |
b8c9b27d | 375 | xfree (buffer); |
b83266a0 SS |
376 | return 0; |
377 | } | |
378 | } | |
c906108c SS |
379 | } |
380 | } | |
381 | else | |
382 | { | |
383 | /* Read all the longwords */ | |
384 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
385 | { | |
386 | errno = 0; | |
c5aa993b | 387 | buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, |
39f77062 KB |
388 | PIDGET (inferior_ptid), |
389 | (PTRACE_ARG3_TYPE) addr, 0); | |
b83266a0 SS |
390 | if (errno) |
391 | { | |
b8c9b27d | 392 | xfree (buffer); |
b83266a0 SS |
393 | return 0; |
394 | } | |
c906108c SS |
395 | QUIT; |
396 | } | |
397 | ||
398 | /* Copy appropriate bytes out of the buffer. */ | |
399 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
400 | } | |
b8c9b27d | 401 | xfree (buffer); |
c906108c SS |
402 | return len; |
403 | } | |
404 | ||
7d2830a3 | 405 | char *saved_child_execd_pathname = NULL; |
6604731b | 406 | int saved_vfork_pid; |
7d2830a3 DJ |
407 | enum { |
408 | STATE_NONE, | |
409 | STATE_GOT_CHILD, | |
410 | STATE_GOT_EXEC, | |
411 | STATE_GOT_PARENT, | |
412 | STATE_FAKE_EXEC | |
413 | } saved_vfork_state = STATE_NONE; | |
c906108c | 414 | |
6604731b DJ |
415 | int |
416 | child_follow_fork (int follow_child) | |
c906108c | 417 | { |
6604731b DJ |
418 | ptid_t last_ptid; |
419 | struct target_waitstatus last_status; | |
420 | int has_vforked; | |
421 | int parent_pid, child_pid; | |
422 | ||
423 | get_last_target_status (&last_ptid, &last_status); | |
424 | has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED); | |
425 | parent_pid = ptid_get_pid (last_ptid); | |
426 | child_pid = last_status.value.related_pid; | |
427 | ||
428 | /* At this point, if we are vforking, breakpoints were already | |
429 | detached from the child in child_wait; and the child has already | |
430 | called execve(). If we are forking, both the parent and child | |
431 | have breakpoints inserted. */ | |
432 | ||
433 | if (! follow_child) | |
c906108c | 434 | { |
6604731b DJ |
435 | if (! has_vforked) |
436 | { | |
437 | detach_breakpoints (child_pid); | |
438 | #ifdef SOLIB_REMOVE_INFERIOR_HOOK | |
439 | SOLIB_REMOVE_INFERIOR_HOOK (child_pid); | |
440 | #endif | |
441 | } | |
442 | ||
443 | /* Detach from the child. */ | |
4c9ba7e0 DJ |
444 | printf_unfiltered ("Detaching after fork from %s\n", |
445 | target_pid_to_str (pid_to_ptid (child_pid))); | |
446 | hppa_require_detach (child_pid, 0); | |
6604731b DJ |
447 | |
448 | /* The parent and child of a vfork share the same address space. | |
449 | Also, on some targets the order in which vfork and exec events | |
450 | are received for parent in child requires some delicate handling | |
451 | of the events. | |
452 | ||
453 | For instance, on ptrace-based HPUX we receive the child's vfork | |
454 | event first, at which time the parent has been suspended by the | |
455 | OS and is essentially untouchable until the child's exit or second | |
456 | exec event arrives. At that time, the parent's vfork event is | |
457 | delivered to us, and that's when we see and decide how to follow | |
458 | the vfork. But to get to that point, we must continue the child | |
459 | until it execs or exits. To do that smoothly, all breakpoints | |
460 | must be removed from the child, in case there are any set between | |
461 | the vfork() and exec() calls. But removing them from the child | |
462 | also removes them from the parent, due to the shared-address-space | |
463 | nature of a vfork'd parent and child. On HPUX, therefore, we must | |
464 | take care to restore the bp's to the parent before we continue it. | |
465 | Else, it's likely that we may not stop in the expected place. (The | |
466 | worst scenario is when the user tries to step over a vfork() call; | |
467 | the step-resume bp must be restored for the step to properly stop | |
468 | in the parent after the call completes!) | |
469 | ||
470 | Sequence of events, as reported to gdb from HPUX: | |
471 | ||
472 | Parent Child Action for gdb to take | |
473 | ------------------------------------------------------- | |
474 | 1 VFORK Continue child | |
475 | 2 EXEC | |
476 | 3 EXEC or EXIT | |
477 | 4 VFORK | |
478 | ||
479 | Now that the child has safely exec'd or exited, we must restore | |
480 | the parent's breakpoints before we continue it. Else, we may | |
481 | cause it run past expected stopping points. */ | |
482 | ||
483 | if (has_vforked) | |
484 | reattach_breakpoints (parent_pid); | |
c906108c | 485 | } |
6604731b DJ |
486 | else |
487 | { | |
6604731b DJ |
488 | /* Needed to keep the breakpoint lists in sync. */ |
489 | if (! has_vforked) | |
490 | detach_breakpoints (child_pid); | |
7d2830a3 | 491 | |
6604731b DJ |
492 | /* Before detaching from the parent, remove all breakpoints from it. */ |
493 | remove_breakpoints (); | |
494 | ||
495 | /* Also reset the solib inferior hook from the parent. */ | |
496 | #ifdef SOLIB_REMOVE_INFERIOR_HOOK | |
497 | SOLIB_REMOVE_INFERIOR_HOOK (PIDGET (inferior_ptid)); | |
498 | #endif | |
7d2830a3 | 499 | |
6604731b DJ |
500 | /* Detach from the parent. */ |
501 | target_detach (NULL, 1); | |
502 | ||
503 | /* Attach to the child. */ | |
4c9ba7e0 DJ |
504 | printf_unfiltered ("Attaching after fork to %s\n", |
505 | target_pid_to_str (pid_to_ptid (child_pid))); | |
506 | hppa_require_attach (child_pid); | |
6604731b | 507 | inferior_ptid = pid_to_ptid (child_pid); |
6604731b DJ |
508 | |
509 | /* If we vforked, then we've also execed by now. The exec will be | |
510 | reported momentarily. follow_exec () will handle breakpoints, so | |
511 | we don't have to.. */ | |
512 | if (!has_vforked) | |
513 | follow_inferior_reset_breakpoints (); | |
514 | } | |
515 | ||
516 | if (has_vforked) | |
c906108c | 517 | { |
6604731b DJ |
518 | /* If we followed the parent, don't try to follow the child's exec. */ |
519 | if (saved_vfork_state != STATE_GOT_PARENT | |
520 | && saved_vfork_state != STATE_FAKE_EXEC) | |
521 | fprintf_unfiltered (gdb_stdout, | |
522 | "hppa: post follow vfork: confused state\n"); | |
523 | ||
524 | if (! follow_child || saved_vfork_state == STATE_GOT_PARENT) | |
525 | saved_vfork_state = STATE_NONE; | |
526 | else | |
527 | return 1; | |
c906108c | 528 | } |
6604731b | 529 | return 0; |
c906108c SS |
530 | } |
531 | ||
b83266a0 SS |
532 | /* Format a process id, given PID. Be sure to terminate |
533 | this with a null--it's going to be printed via a "%s". */ | |
c906108c | 534 | char * |
39f77062 | 535 | child_pid_to_str (ptid_t ptid) |
c906108c | 536 | { |
c5aa993b JM |
537 | /* Static because address returned */ |
538 | static char buf[30]; | |
39f77062 | 539 | pid_t pid = PIDGET (ptid); |
c906108c | 540 | |
ce414844 AC |
541 | /* Extra NUL for paranoia's sake */ |
542 | sprintf (buf, "process %d%c", pid, '\0'); | |
c5aa993b JM |
543 | |
544 | return buf; | |
c906108c SS |
545 | } |
546 | ||
b83266a0 SS |
547 | /* Format a thread id, given TID. Be sure to terminate |
548 | this with a null--it's going to be printed via a "%s". | |
549 | ||
550 | Note: This is a core-gdb tid, not the actual system tid. | |
c5aa993b | 551 | See infttrace.c for details. */ |
c906108c | 552 | char * |
39f77062 | 553 | hppa_tid_to_str (ptid_t ptid) |
c906108c | 554 | { |
c5aa993b JM |
555 | /* Static because address returned */ |
556 | static char buf[30]; | |
39f77062 KB |
557 | /* This seems strange, but when I did the ptid conversion, it looked |
558 | as though a pid was always being passed. - Kevin Buettner */ | |
559 | pid_t tid = PIDGET (ptid); | |
c5aa993b JM |
560 | |
561 | /* Extra NULLs for paranoia's sake */ | |
ce414844 | 562 | sprintf (buf, "system thread %d%c", tid, '\0'); |
c906108c | 563 | |
c5aa993b | 564 | return buf; |
c906108c SS |
565 | } |
566 | ||
47932f85 DJ |
567 | /*## */ |
568 | /* Enable HACK for ttrace work. In | |
569 | * infttrace.c/require_notification_of_events, | |
570 | * this is set to 0 so that the loop in child_wait | |
571 | * won't loop. | |
572 | */ | |
573 | int not_same_real_pid = 1; | |
574 | /*## */ | |
575 | ||
47932f85 DJ |
576 | /* Wait for child to do something. Return pid of child, or -1 in case |
577 | of error; store status through argument pointer OURSTATUS. */ | |
578 | ||
579 | ptid_t | |
580 | child_wait (ptid_t ptid, struct target_waitstatus *ourstatus) | |
581 | { | |
582 | int save_errno; | |
583 | int status; | |
584 | char *execd_pathname = NULL; | |
585 | int exit_status; | |
586 | int related_pid; | |
587 | int syscall_id; | |
588 | enum target_waitkind kind; | |
589 | int pid; | |
590 | ||
7d2830a3 DJ |
591 | if (saved_vfork_state == STATE_FAKE_EXEC) |
592 | { | |
593 | saved_vfork_state = STATE_NONE; | |
594 | ourstatus->kind = TARGET_WAITKIND_EXECD; | |
595 | ourstatus->value.execd_pathname = saved_child_execd_pathname; | |
596 | return inferior_ptid; | |
597 | } | |
598 | ||
47932f85 DJ |
599 | do |
600 | { | |
601 | set_sigint_trap (); /* Causes SIGINT to be passed on to the | |
602 | attached process. */ | |
603 | set_sigio_trap (); | |
604 | ||
605 | pid = ptrace_wait (inferior_ptid, &status); | |
606 | ||
607 | save_errno = errno; | |
608 | ||
609 | clear_sigio_trap (); | |
610 | ||
611 | clear_sigint_trap (); | |
612 | ||
613 | if (pid == -1) | |
614 | { | |
615 | if (save_errno == EINTR) | |
616 | continue; | |
617 | ||
618 | fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n", | |
619 | safe_strerror (save_errno)); | |
620 | ||
621 | /* Claim it exited with unknown signal. */ | |
622 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
623 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; | |
624 | return pid_to_ptid (-1); | |
625 | } | |
626 | ||
627 | /* Did it exit? | |
628 | */ | |
629 | if (target_has_exited (pid, status, &exit_status)) | |
630 | { | |
631 | /* ??rehrauer: For now, ignore this. */ | |
632 | continue; | |
633 | } | |
634 | ||
635 | if (!target_thread_alive (pid_to_ptid (pid))) | |
636 | { | |
637 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
638 | return pid_to_ptid (pid); | |
639 | } | |
640 | ||
8e7d2c16 | 641 | if (hpux_has_forked (pid, &related_pid)) |
47932f85 | 642 | { |
8e7d2c16 DJ |
643 | /* Ignore the parent's fork event. */ |
644 | if (pid == PIDGET (inferior_ptid)) | |
645 | { | |
646 | ourstatus->kind = TARGET_WAITKIND_IGNORE; | |
647 | return inferior_ptid; | |
648 | } | |
649 | ||
650 | /* If this is the child's fork event, report that the | |
651 | process has forked. */ | |
652 | if (related_pid == PIDGET (inferior_ptid)) | |
653 | { | |
654 | ourstatus->kind = TARGET_WAITKIND_FORKED; | |
655 | ourstatus->value.related_pid = pid; | |
656 | return inferior_ptid; | |
657 | } | |
47932f85 DJ |
658 | } |
659 | ||
7d2830a3 | 660 | if (hpux_has_vforked (pid, &related_pid)) |
47932f85 | 661 | { |
7d2830a3 DJ |
662 | if (pid == PIDGET (inferior_ptid)) |
663 | { | |
664 | if (saved_vfork_state == STATE_GOT_CHILD) | |
665 | saved_vfork_state = STATE_GOT_PARENT; | |
666 | else if (saved_vfork_state == STATE_GOT_EXEC) | |
667 | saved_vfork_state = STATE_FAKE_EXEC; | |
668 | else | |
669 | fprintf_unfiltered (gdb_stdout, | |
670 | "hppah: parent vfork: confused\n"); | |
671 | } | |
672 | else if (related_pid == PIDGET (inferior_ptid)) | |
673 | { | |
674 | if (saved_vfork_state == STATE_NONE) | |
675 | saved_vfork_state = STATE_GOT_CHILD; | |
676 | else | |
677 | fprintf_unfiltered (gdb_stdout, | |
678 | "hppah: child vfork: confused\n"); | |
679 | } | |
680 | else | |
681 | fprintf_unfiltered (gdb_stdout, | |
682 | "hppah: unknown vfork: confused\n"); | |
683 | ||
684 | if (saved_vfork_state == STATE_GOT_CHILD) | |
685 | { | |
686 | child_post_startup_inferior (pid_to_ptid (pid)); | |
6604731b DJ |
687 | detach_breakpoints (pid); |
688 | #ifdef SOLIB_REMOVE_INFERIOR_HOOK | |
689 | SOLIB_REMOVE_INFERIOR_HOOK (pid); | |
690 | #endif | |
691 | child_resume (pid_to_ptid (pid), 0, TARGET_SIGNAL_0); | |
692 | ourstatus->kind = TARGET_WAITKIND_IGNORE; | |
693 | return pid_to_ptid (related_pid); | |
7d2830a3 | 694 | } |
6604731b | 695 | else if (saved_vfork_state == STATE_FAKE_EXEC) |
7d2830a3 DJ |
696 | { |
697 | ourstatus->kind = TARGET_WAITKIND_VFORKED; | |
698 | ourstatus->value.related_pid = related_pid; | |
699 | return pid_to_ptid (pid); | |
700 | } | |
6604731b DJ |
701 | else |
702 | { | |
703 | /* We saw the parent's vfork, but we haven't seen the exec yet. | |
704 | Wait for it, for simplicity's sake. It should be pending. */ | |
705 | saved_vfork_pid = related_pid; | |
706 | ourstatus->kind = TARGET_WAITKIND_IGNORE; | |
707 | return pid_to_ptid (pid); | |
708 | } | |
47932f85 DJ |
709 | } |
710 | ||
711 | if (hpux_has_execd (pid, &execd_pathname)) | |
712 | { | |
7d2830a3 DJ |
713 | /* On HP-UX, events associated with a vforking inferior come in |
714 | threes: a vfork event for the child (always first), followed | |
715 | a vfork event for the parent and an exec event for the child. | |
6604731b DJ |
716 | The latter two can come in either order. Make sure we get |
717 | both. */ | |
718 | if (saved_vfork_state != STATE_NONE) | |
7d2830a3 | 719 | { |
6604731b DJ |
720 | if (saved_vfork_state == STATE_GOT_CHILD) |
721 | { | |
722 | saved_vfork_state = STATE_GOT_EXEC; | |
723 | /* On HP/UX with ptrace, the child must be resumed before | |
724 | the parent vfork event is delivered. A single-step | |
725 | suffices. */ | |
726 | if (RESUME_EXECD_VFORKING_CHILD_TO_GET_PARENT_VFORK ()) | |
727 | target_resume (pid_to_ptid (pid), 1, TARGET_SIGNAL_0); | |
728 | ourstatus->kind = TARGET_WAITKIND_IGNORE; | |
729 | } | |
730 | else if (saved_vfork_state == STATE_GOT_PARENT) | |
731 | { | |
732 | saved_vfork_state = STATE_FAKE_EXEC; | |
733 | ourstatus->kind = TARGET_WAITKIND_VFORKED; | |
734 | ourstatus->value.related_pid = saved_vfork_pid; | |
735 | } | |
736 | else | |
737 | fprintf_unfiltered (gdb_stdout, | |
738 | "hppa: exec: unexpected state\n"); | |
7d2830a3 | 739 | |
6604731b | 740 | saved_child_execd_pathname = execd_pathname; |
7d2830a3 | 741 | |
7d2830a3 DJ |
742 | return inferior_ptid; |
743 | } | |
744 | ||
47932f85 DJ |
745 | /* Are we ignoring initial exec events? (This is likely because |
746 | we're in the process of starting up the inferior, and another | |
747 | (older) mechanism handles those.) If so, we'll report this | |
748 | as a regular stop, not an exec. | |
749 | */ | |
750 | if (inferior_ignoring_startup_exec_events) | |
751 | { | |
752 | inferior_ignoring_startup_exec_events--; | |
753 | } | |
754 | else | |
755 | { | |
756 | ourstatus->kind = TARGET_WAITKIND_EXECD; | |
757 | ourstatus->value.execd_pathname = execd_pathname; | |
758 | return pid_to_ptid (pid); | |
759 | } | |
760 | } | |
761 | ||
762 | /* All we must do with these is communicate their occurrence | |
763 | to wait_for_inferior... | |
764 | */ | |
765 | if (hpux_has_syscall_event (pid, &kind, &syscall_id)) | |
766 | { | |
767 | ourstatus->kind = kind; | |
768 | ourstatus->value.syscall_id = syscall_id; | |
769 | return pid_to_ptid (pid); | |
770 | } | |
771 | ||
772 | /*## } while (pid != PIDGET (inferior_ptid)); ## *//* Some other child died or stopped */ | |
773 | /* hack for thread testing */ | |
774 | } | |
775 | while ((pid != PIDGET (inferior_ptid)) && not_same_real_pid); | |
776 | /*## */ | |
777 | ||
778 | store_waitstatus (ourstatus, status); | |
779 | return pid_to_ptid (pid); | |
780 | } | |
781 | ||
c906108c SS |
782 | #if !defined (GDB_NATIVE_HPUX_11) |
783 | ||
784 | /* The following code is a substitute for the infttrace.c versions used | |
785 | with ttrace() in HPUX 11. */ | |
786 | ||
787 | /* This value is an arbitrary integer. */ | |
788 | #define PT_VERSION 123456 | |
789 | ||
790 | /* This semaphore is used to coordinate the child and parent processes | |
791 | after a fork(), and before an exec() by the child. See | |
792 | parent_attach_all for details. */ | |
793 | ||
c5aa993b JM |
794 | typedef struct |
795 | { | |
796 | int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */ | |
797 | int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */ | |
798 | } | |
799 | startup_semaphore_t; | |
c906108c SS |
800 | |
801 | #define SEM_TALK (1) | |
802 | #define SEM_LISTEN (0) | |
803 | ||
c5aa993b | 804 | static startup_semaphore_t startup_semaphore; |
c906108c | 805 | |
c906108c SS |
806 | #ifdef PT_SETTRC |
807 | /* This function causes the caller's process to be traced by its | |
808 | parent. This is intended to be called after GDB forks itself, | |
809 | and before the child execs the target. | |
810 | ||
811 | Note that HP-UX ptrace is rather funky in how this is done. | |
812 | If the parent wants to get the initial exec event of a child, | |
813 | it must set the ptrace event mask of the child to include execs. | |
814 | (The child cannot do this itself.) This must be done after the | |
815 | child is forked, but before it execs. | |
816 | ||
817 | To coordinate the parent and child, we implement a semaphore using | |
818 | pipes. After SETTRC'ing itself, the child tells the parent that | |
819 | it is now traceable by the parent, and waits for the parent's | |
820 | acknowledgement. The parent can then set the child's event mask, | |
821 | and notify the child that it can now exec. | |
822 | ||
823 | (The acknowledgement by parent happens as a result of a call to | |
824 | child_acknowledge_created_inferior.) */ | |
825 | ||
826 | int | |
fba45db2 | 827 | parent_attach_all (int pid, PTRACE_ARG3_TYPE addr, int data) |
c906108c SS |
828 | { |
829 | int pt_status = 0; | |
830 | ||
831 | /* We need a memory home for a constant. */ | |
832 | int tc_magic_child = PT_VERSION; | |
833 | int tc_magic_parent = 0; | |
834 | ||
835 | /* The remainder of this function is only useful for HPUX 10.0 and | |
836 | later, as it depends upon the ability to request notification | |
837 | of specific kinds of events by the kernel. */ | |
838 | #if defined(PT_SET_EVENT_MASK) | |
839 | ||
840 | /* Notify the parent that we're potentially ready to exec(). */ | |
841 | write (startup_semaphore.child_channel[SEM_TALK], | |
b83266a0 SS |
842 | &tc_magic_child, |
843 | sizeof (tc_magic_child)); | |
c906108c SS |
844 | |
845 | /* Wait for acknowledgement from the parent. */ | |
846 | read (startup_semaphore.parent_channel[SEM_LISTEN], | |
b83266a0 SS |
847 | &tc_magic_parent, |
848 | sizeof (tc_magic_parent)); | |
c906108c | 849 | if (tc_magic_child != tc_magic_parent) |
c5aa993b | 850 | warning ("mismatched semaphore magic"); |
c906108c SS |
851 | |
852 | /* Discard our copy of the semaphore. */ | |
853 | (void) close (startup_semaphore.parent_channel[SEM_LISTEN]); | |
854 | (void) close (startup_semaphore.parent_channel[SEM_TALK]); | |
855 | (void) close (startup_semaphore.child_channel[SEM_LISTEN]); | |
856 | (void) close (startup_semaphore.child_channel[SEM_TALK]); | |
857 | #endif | |
c5aa993b | 858 | |
c906108c SS |
859 | return 0; |
860 | } | |
861 | #endif | |
862 | ||
863 | int | |
fba45db2 | 864 | hppa_require_attach (int pid) |
c906108c SS |
865 | { |
866 | int pt_status; | |
b83266a0 SS |
867 | CORE_ADDR pc; |
868 | CORE_ADDR pc_addr; | |
c906108c SS |
869 | unsigned int regs_offset; |
870 | ||
871 | /* Are we already attached? There appears to be no explicit way to | |
872 | answer this via ptrace, so we try something which should be | |
873 | innocuous if we are attached. If that fails, then we assume | |
874 | we're not attached, and so attempt to make it so. */ | |
875 | ||
876 | errno = 0; | |
877 | regs_offset = U_REGS_OFFSET; | |
878 | pc_addr = register_addr (PC_REGNUM, regs_offset); | |
879 | pc = call_ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE) pc_addr, 0); | |
880 | ||
881 | if (errno) | |
882 | { | |
883 | errno = 0; | |
884 | pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0); | |
885 | ||
886 | if (errno) | |
b83266a0 | 887 | return -1; |
c906108c SS |
888 | |
889 | /* Now we really are attached. */ | |
890 | errno = 0; | |
891 | } | |
892 | attach_flag = 1; | |
893 | return pid; | |
894 | } | |
895 | ||
896 | int | |
fba45db2 | 897 | hppa_require_detach (int pid, int signal) |
c906108c SS |
898 | { |
899 | errno = 0; | |
900 | call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal); | |
c5aa993b | 901 | errno = 0; /* Ignore any errors. */ |
c906108c SS |
902 | return pid; |
903 | } | |
904 | ||
905 | /* Since ptrace doesn't support memory page-protection events, which | |
906 | are used to implement "hardware" watchpoints on HP-UX, these are | |
907 | dummy versions, which perform no useful work. */ | |
908 | ||
909 | void | |
fba45db2 | 910 | hppa_enable_page_protection_events (int pid) |
c906108c SS |
911 | { |
912 | } | |
913 | ||
914 | void | |
fba45db2 | 915 | hppa_disable_page_protection_events (int pid) |
c906108c SS |
916 | { |
917 | } | |
918 | ||
919 | int | |
fba45db2 | 920 | hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type) |
c906108c SS |
921 | { |
922 | error ("Hardware watchpoints not implemented on this platform."); | |
923 | } | |
924 | ||
925 | int | |
65e82032 | 926 | hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type) |
c906108c SS |
927 | { |
928 | error ("Hardware watchpoints not implemented on this platform."); | |
929 | } | |
930 | ||
931 | int | |
65e82032 | 932 | hppa_can_use_hw_watchpoint (int type, int cnt, int ot) |
c906108c SS |
933 | { |
934 | return 0; | |
935 | } | |
936 | ||
937 | int | |
fba45db2 | 938 | hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len) |
c906108c SS |
939 | { |
940 | error ("Hardware watchpoints not implemented on this platform."); | |
941 | } | |
942 | ||
943 | char * | |
39f77062 | 944 | hppa_pid_or_tid_to_str (ptid_t id) |
c906108c SS |
945 | { |
946 | /* In the ptrace world, there are only processes. */ | |
ed9a39eb | 947 | return child_pid_to_str (id); |
c906108c SS |
948 | } |
949 | ||
c906108c | 950 | void |
fba45db2 | 951 | hppa_ensure_vforking_parent_remains_stopped (int pid) |
c906108c SS |
952 | { |
953 | /* This assumes that the vforked parent is presently stopped, and | |
954 | that the vforked child has just delivered its first exec event. | |
955 | Calling kill() this way will cause the SIGTRAP to be delivered as | |
956 | soon as the parent is resumed, which happens as soon as the | |
957 | vforked child is resumed. See wait_for_inferior for the use of | |
958 | this function. */ | |
959 | kill (pid, SIGTRAP); | |
960 | } | |
961 | ||
962 | int | |
fba45db2 | 963 | hppa_resume_execd_vforking_child_to_get_parent_vfork (void) |
c906108c | 964 | { |
c5aa993b | 965 | return 1; /* Yes, the child must be resumed. */ |
c906108c SS |
966 | } |
967 | ||
968 | void | |
fba45db2 | 969 | require_notification_of_events (int pid) |
c906108c SS |
970 | { |
971 | #if defined(PT_SET_EVENT_MASK) | |
972 | int pt_status; | |
973 | ptrace_event_t ptrace_events; | |
c2d11a7d JM |
974 | int nsigs; |
975 | int signum; | |
c906108c SS |
976 | |
977 | /* Instruct the kernel as to the set of events we wish to be | |
978 | informed of. (This support does not exist before HPUX 10.0. | |
979 | We'll assume if PT_SET_EVENT_MASK has not been defined by | |
b83266a0 | 980 | <sys/ptrace.h>, then we're being built on pre-10.0.) */ |
c906108c SS |
981 | memset (&ptrace_events, 0, sizeof (ptrace_events)); |
982 | ||
983 | /* Note: By default, all signals are visible to us. If we wish | |
984 | the kernel to keep certain signals hidden from us, we do it | |
985 | by calling sigdelset (ptrace_events.pe_signals, signal) for | |
b83266a0 | 986 | each such signal here, before doing PT_SET_EVENT_MASK. */ |
c2d11a7d JM |
987 | /* RM: The above comment is no longer true. We start with ignoring |
988 | all signals, and then add the ones we are interested in. We could | |
989 | do it the other way: start by looking at all signals and then | |
990 | deleting the ones that we aren't interested in, except that | |
991 | multiple gdb signals may be mapped to the same host signal | |
992 | (eg. TARGET_SIGNAL_IO and TARGET_SIGNAL_POLL both get mapped to | |
993 | signal 22 on HPUX 10.20) We want to be notified if we are | |
994 | interested in either signal. */ | |
995 | sigfillset (&ptrace_events.pe_signals); | |
996 | ||
997 | /* RM: Let's not bother with signals we don't care about */ | |
998 | nsigs = (int) TARGET_SIGNAL_LAST; | |
999 | for (signum = nsigs; signum > 0; signum--) | |
1000 | { | |
1001 | if ((signal_stop_state (signum)) || | |
1002 | (signal_print_state (signum)) || | |
1003 | (!signal_pass_state (signum))) | |
1004 | { | |
1005 | if (target_signal_to_host_p (signum)) | |
1006 | sigdelset (&ptrace_events.pe_signals, | |
1007 | target_signal_to_host (signum)); | |
1008 | } | |
1009 | } | |
c906108c SS |
1010 | |
1011 | ptrace_events.pe_set_event = 0; | |
1012 | ||
1013 | ptrace_events.pe_set_event |= PTRACE_SIGNAL; | |
1014 | ptrace_events.pe_set_event |= PTRACE_EXEC; | |
1015 | ptrace_events.pe_set_event |= PTRACE_FORK; | |
1016 | ptrace_events.pe_set_event |= PTRACE_VFORK; | |
1017 | /* ??rehrauer: Add this one when we're prepared to catch it... | |
c5aa993b JM |
1018 | ptrace_events.pe_set_event |= PTRACE_EXIT; |
1019 | */ | |
c906108c SS |
1020 | |
1021 | errno = 0; | |
1022 | pt_status = call_ptrace (PT_SET_EVENT_MASK, | |
c5aa993b JM |
1023 | pid, |
1024 | (PTRACE_ARG3_TYPE) & ptrace_events, | |
1025 | sizeof (ptrace_events)); | |
c906108c SS |
1026 | if (errno) |
1027 | perror_with_name ("ptrace"); | |
1028 | if (pt_status < 0) | |
1029 | return; | |
1030 | #endif | |
1031 | } | |
1032 | ||
1033 | void | |
fba45db2 | 1034 | require_notification_of_exec_events (int pid) |
c906108c SS |
1035 | { |
1036 | #if defined(PT_SET_EVENT_MASK) | |
1037 | int pt_status; | |
1038 | ptrace_event_t ptrace_events; | |
1039 | ||
1040 | /* Instruct the kernel as to the set of events we wish to be | |
1041 | informed of. (This support does not exist before HPUX 10.0. | |
1042 | We'll assume if PT_SET_EVENT_MASK has not been defined by | |
b83266a0 | 1043 | <sys/ptrace.h>, then we're being built on pre-10.0.) */ |
c906108c SS |
1044 | memset (&ptrace_events, 0, sizeof (ptrace_events)); |
1045 | ||
1046 | /* Note: By default, all signals are visible to us. If we wish | |
1047 | the kernel to keep certain signals hidden from us, we do it | |
1048 | by calling sigdelset (ptrace_events.pe_signals, signal) for | |
b83266a0 | 1049 | each such signal here, before doing PT_SET_EVENT_MASK. */ |
c906108c SS |
1050 | sigemptyset (&ptrace_events.pe_signals); |
1051 | ||
1052 | ptrace_events.pe_set_event = 0; | |
1053 | ||
1054 | ptrace_events.pe_set_event |= PTRACE_EXEC; | |
1055 | /* ??rehrauer: Add this one when we're prepared to catch it... | |
c5aa993b JM |
1056 | ptrace_events.pe_set_event |= PTRACE_EXIT; |
1057 | */ | |
c906108c SS |
1058 | |
1059 | errno = 0; | |
1060 | pt_status = call_ptrace (PT_SET_EVENT_MASK, | |
c5aa993b JM |
1061 | pid, |
1062 | (PTRACE_ARG3_TYPE) & ptrace_events, | |
1063 | sizeof (ptrace_events)); | |
c906108c SS |
1064 | if (errno) |
1065 | perror_with_name ("ptrace"); | |
1066 | if (pt_status < 0) | |
1067 | return; | |
1068 | #endif | |
1069 | } | |
1070 | ||
1071 | /* This function is called by the parent process, with pid being the | |
1072 | ID of the child process, after the debugger has forked. */ | |
1073 | ||
1074 | void | |
fba45db2 | 1075 | child_acknowledge_created_inferior (int pid) |
c906108c SS |
1076 | { |
1077 | /* We need a memory home for a constant. */ | |
1078 | int tc_magic_parent = PT_VERSION; | |
1079 | int tc_magic_child = 0; | |
1080 | ||
b83266a0 SS |
1081 | /* The remainder of this function is only useful for HPUX 10.0 and |
1082 | later, as it depends upon the ability to request notification | |
1083 | of specific kinds of events by the kernel. */ | |
1084 | #if defined(PT_SET_EVENT_MASK) | |
c906108c SS |
1085 | /* Wait for the child to tell us that it has forked. */ |
1086 | read (startup_semaphore.child_channel[SEM_LISTEN], | |
b83266a0 | 1087 | &tc_magic_child, |
c5aa993b | 1088 | sizeof (tc_magic_child)); |
c906108c SS |
1089 | |
1090 | /* Notify the child that it can exec. | |
1091 | ||
1092 | In the infttrace.c variant of this function, we set the child's | |
1093 | event mask after the fork but before the exec. In the ptrace | |
1094 | world, it seems we can't set the event mask until after the exec. */ | |
c906108c | 1095 | write (startup_semaphore.parent_channel[SEM_TALK], |
b83266a0 SS |
1096 | &tc_magic_parent, |
1097 | sizeof (tc_magic_parent)); | |
c906108c SS |
1098 | |
1099 | /* We'd better pause a bit before trying to set the event mask, | |
1100 | though, to ensure that the exec has happened. We don't want to | |
1101 | wait() on the child, because that'll screw up the upper layers | |
1102 | of gdb's execution control that expect to see the exec event. | |
1103 | ||
1104 | After an exec, the child is no longer executing gdb code. Hence, | |
1105 | we can't have yet another synchronization via the pipes. We'll | |
1106 | just sleep for a second, and hope that's enough delay... */ | |
c906108c SS |
1107 | sleep (1); |
1108 | ||
1109 | /* Instruct the kernel as to the set of events we wish to be | |
1110 | informed of. */ | |
c906108c SS |
1111 | require_notification_of_exec_events (pid); |
1112 | ||
1113 | /* Discard our copy of the semaphore. */ | |
1114 | (void) close (startup_semaphore.parent_channel[SEM_LISTEN]); | |
1115 | (void) close (startup_semaphore.parent_channel[SEM_TALK]); | |
1116 | (void) close (startup_semaphore.child_channel[SEM_LISTEN]); | |
1117 | (void) close (startup_semaphore.child_channel[SEM_TALK]); | |
b83266a0 | 1118 | #endif |
c906108c SS |
1119 | } |
1120 | ||
1121 | void | |
39f77062 | 1122 | child_post_startup_inferior (ptid_t ptid) |
c906108c | 1123 | { |
39f77062 | 1124 | require_notification_of_events (PIDGET (ptid)); |
c906108c SS |
1125 | } |
1126 | ||
1127 | void | |
fba45db2 | 1128 | child_post_attach (int pid) |
c906108c SS |
1129 | { |
1130 | require_notification_of_events (pid); | |
1131 | } | |
1132 | ||
1133 | int | |
fba45db2 | 1134 | child_insert_fork_catchpoint (int pid) |
c906108c SS |
1135 | { |
1136 | /* This request is only available on HPUX 10.0 and later. */ | |
1137 | #if !defined(PT_SET_EVENT_MASK) | |
1138 | error ("Unable to catch forks prior to HPUX 10.0"); | |
1139 | #else | |
1140 | /* Enable reporting of fork events from the kernel. */ | |
1141 | /* ??rehrauer: For the moment, we're always enabling these events, | |
b83266a0 | 1142 | and just ignoring them if there's no catchpoint to catch them. */ |
c906108c SS |
1143 | return 0; |
1144 | #endif | |
1145 | } | |
1146 | ||
1147 | int | |
fba45db2 | 1148 | child_remove_fork_catchpoint (int pid) |
c906108c SS |
1149 | { |
1150 | /* This request is only available on HPUX 10.0 and later. */ | |
1151 | #if !defined(PT_SET_EVENT_MASK) | |
1152 | error ("Unable to catch forks prior to HPUX 10.0"); | |
1153 | #else | |
1154 | /* Disable reporting of fork events from the kernel. */ | |
1155 | /* ??rehrauer: For the moment, we're always enabling these events, | |
1156 | and just ignoring them if there's no catchpoint to catch them. */ | |
1157 | return 0; | |
1158 | #endif | |
1159 | } | |
1160 | ||
1161 | int | |
fba45db2 | 1162 | child_insert_vfork_catchpoint (int pid) |
c906108c SS |
1163 | { |
1164 | /* This request is only available on HPUX 10.0 and later. */ | |
1165 | #if !defined(PT_SET_EVENT_MASK) | |
1166 | error ("Unable to catch vforks prior to HPUX 10.0"); | |
1167 | #else | |
1168 | /* Enable reporting of vfork events from the kernel. */ | |
1169 | /* ??rehrauer: For the moment, we're always enabling these events, | |
1170 | and just ignoring them if there's no catchpoint to catch them. */ | |
1171 | return 0; | |
1172 | #endif | |
1173 | } | |
1174 | ||
1175 | int | |
fba45db2 | 1176 | child_remove_vfork_catchpoint (int pid) |
c906108c SS |
1177 | { |
1178 | /* This request is only available on HPUX 10.0 and later. */ | |
1179 | #if !defined(PT_SET_EVENT_MASK) | |
1180 | error ("Unable to catch vforks prior to HPUX 10.0"); | |
1181 | #else | |
1182 | /* Disable reporting of vfork events from the kernel. */ | |
1183 | /* ??rehrauer: For the moment, we're always enabling these events, | |
1184 | and just ignoring them if there's no catchpoint to catch them. */ | |
1185 | return 0; | |
1186 | #endif | |
1187 | } | |
1188 | ||
1189 | int | |
47932f85 | 1190 | hpux_has_forked (int pid, int *childpid) |
c906108c SS |
1191 | { |
1192 | /* This request is only available on HPUX 10.0 and later. */ | |
1193 | #if !defined(PT_GET_PROCESS_STATE) | |
1194 | *childpid = 0; | |
1195 | return 0; | |
1196 | #else | |
1197 | int pt_status; | |
c5aa993b | 1198 | ptrace_state_t ptrace_state; |
c906108c SS |
1199 | |
1200 | errno = 0; | |
1201 | pt_status = call_ptrace (PT_GET_PROCESS_STATE, | |
b83266a0 | 1202 | pid, |
c5aa993b | 1203 | (PTRACE_ARG3_TYPE) & ptrace_state, |
b83266a0 | 1204 | sizeof (ptrace_state)); |
c906108c SS |
1205 | if (errno) |
1206 | perror_with_name ("ptrace"); | |
1207 | if (pt_status < 0) | |
1208 | return 0; | |
1209 | ||
1210 | if (ptrace_state.pe_report_event & PTRACE_FORK) | |
1211 | { | |
1212 | *childpid = ptrace_state.pe_other_pid; | |
1213 | return 1; | |
1214 | } | |
1215 | ||
1216 | return 0; | |
1217 | #endif | |
1218 | } | |
1219 | ||
1220 | int | |
47932f85 | 1221 | hpux_has_vforked (int pid, int *childpid) |
c906108c SS |
1222 | { |
1223 | /* This request is only available on HPUX 10.0 and later. */ | |
1224 | #if !defined(PT_GET_PROCESS_STATE) | |
1225 | *childpid = 0; | |
1226 | return 0; | |
1227 | ||
1228 | #else | |
1229 | int pt_status; | |
c5aa993b | 1230 | ptrace_state_t ptrace_state; |
c906108c SS |
1231 | |
1232 | errno = 0; | |
1233 | pt_status = call_ptrace (PT_GET_PROCESS_STATE, | |
b83266a0 | 1234 | pid, |
c5aa993b | 1235 | (PTRACE_ARG3_TYPE) & ptrace_state, |
b83266a0 | 1236 | sizeof (ptrace_state)); |
c906108c SS |
1237 | if (errno) |
1238 | perror_with_name ("ptrace"); | |
1239 | if (pt_status < 0) | |
1240 | return 0; | |
1241 | ||
1242 | if (ptrace_state.pe_report_event & PTRACE_VFORK) | |
1243 | { | |
1244 | *childpid = ptrace_state.pe_other_pid; | |
1245 | return 1; | |
1246 | } | |
1247 | ||
1248 | return 0; | |
1249 | #endif | |
1250 | } | |
1251 | ||
c906108c | 1252 | int |
fba45db2 | 1253 | child_insert_exec_catchpoint (int pid) |
c906108c | 1254 | { |
b83266a0 | 1255 | /* This request is only available on HPUX 10.0 and later. */ |
c906108c SS |
1256 | #if !defined(PT_SET_EVENT_MASK) |
1257 | error ("Unable to catch execs prior to HPUX 10.0"); | |
1258 | ||
1259 | #else | |
b83266a0 | 1260 | /* Enable reporting of exec events from the kernel. */ |
c906108c | 1261 | /* ??rehrauer: For the moment, we're always enabling these events, |
b83266a0 | 1262 | and just ignoring them if there's no catchpoint to catch them. */ |
c906108c SS |
1263 | return 0; |
1264 | #endif | |
1265 | } | |
1266 | ||
1267 | int | |
fba45db2 | 1268 | child_remove_exec_catchpoint (int pid) |
c906108c | 1269 | { |
b83266a0 | 1270 | /* This request is only available on HPUX 10.0 and later. */ |
c906108c SS |
1271 | #if !defined(PT_SET_EVENT_MASK) |
1272 | error ("Unable to catch execs prior to HPUX 10.0"); | |
1273 | ||
1274 | #else | |
1275 | /* Disable reporting of exec events from the kernel. */ | |
1276 | /* ??rehrauer: For the moment, we're always enabling these events, | |
b83266a0 | 1277 | and just ignoring them if there's no catchpoint to catch them. */ |
c906108c SS |
1278 | return 0; |
1279 | #endif | |
1280 | } | |
1281 | ||
1282 | int | |
47932f85 | 1283 | hpux_has_execd (int pid, char **execd_pathname) |
c906108c | 1284 | { |
b83266a0 | 1285 | /* This request is only available on HPUX 10.0 and later. */ |
c906108c SS |
1286 | #if !defined(PT_GET_PROCESS_STATE) |
1287 | *execd_pathname = NULL; | |
1288 | return 0; | |
1289 | ||
1290 | #else | |
1291 | int pt_status; | |
c5aa993b | 1292 | ptrace_state_t ptrace_state; |
c906108c SS |
1293 | |
1294 | errno = 0; | |
1295 | pt_status = call_ptrace (PT_GET_PROCESS_STATE, | |
b83266a0 | 1296 | pid, |
c5aa993b | 1297 | (PTRACE_ARG3_TYPE) & ptrace_state, |
b83266a0 | 1298 | sizeof (ptrace_state)); |
c906108c SS |
1299 | if (errno) |
1300 | perror_with_name ("ptrace"); | |
1301 | if (pt_status < 0) | |
1302 | return 0; | |
1303 | ||
1304 | if (ptrace_state.pe_report_event & PTRACE_EXEC) | |
1305 | { | |
c5aa993b | 1306 | char *exec_file = target_pid_to_exec_file (pid); |
c906108c SS |
1307 | *execd_pathname = savestring (exec_file, strlen (exec_file)); |
1308 | return 1; | |
1309 | } | |
1310 | ||
1311 | return 0; | |
1312 | #endif | |
1313 | } | |
1314 | ||
1315 | int | |
fba45db2 | 1316 | child_reported_exec_events_per_exec_call (void) |
c906108c | 1317 | { |
c5aa993b | 1318 | return 2; /* ptrace reports the event twice per call. */ |
c906108c SS |
1319 | } |
1320 | ||
1321 | int | |
47932f85 | 1322 | hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id) |
c906108c SS |
1323 | { |
1324 | /* This request is only available on HPUX 10.30 and later, via | |
1325 | the ttrace interface. */ | |
1326 | ||
1327 | *kind = TARGET_WAITKIND_SPURIOUS; | |
1328 | *syscall_id = -1; | |
1329 | return 0; | |
1330 | } | |
1331 | ||
1332 | char * | |
fba45db2 | 1333 | child_pid_to_exec_file (int pid) |
c906108c | 1334 | { |
b83266a0 | 1335 | static char exec_file_buffer[1024]; |
c906108c | 1336 | int pt_status; |
b83266a0 SS |
1337 | CORE_ADDR top_of_stack; |
1338 | char four_chars[4]; | |
c906108c SS |
1339 | int name_index; |
1340 | int i; | |
39f77062 | 1341 | ptid_t saved_inferior_ptid; |
52287340 | 1342 | int done; |
c5aa993b | 1343 | |
c906108c SS |
1344 | #ifdef PT_GET_PROCESS_PATHNAME |
1345 | /* As of 10.x HP-UX, there's an explicit request to get the pathname. */ | |
1346 | pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME, | |
b83266a0 SS |
1347 | pid, |
1348 | (PTRACE_ARG3_TYPE) exec_file_buffer, | |
1349 | sizeof (exec_file_buffer) - 1); | |
c906108c SS |
1350 | if (pt_status == 0) |
1351 | return exec_file_buffer; | |
1352 | #endif | |
1353 | ||
1354 | /* It appears that this request is broken prior to 10.30. | |
1355 | If it fails, try a really, truly amazingly gross hack | |
1356 | that DDE uses, of pawing through the process' data | |
1357 | segment to find the pathname. */ | |
1358 | ||
1359 | top_of_stack = 0x7b03a000; | |
1360 | name_index = 0; | |
1361 | done = 0; | |
1362 | ||
39f77062 KB |
1363 | /* On the chance that pid != inferior_ptid, set inferior_ptid |
1364 | to pid, so that (grrrr!) implicit uses of inferior_ptid get | |
c906108c SS |
1365 | the right id. */ |
1366 | ||
39f77062 KB |
1367 | saved_inferior_ptid = inferior_ptid; |
1368 | inferior_ptid = pid_to_ptid (pid); | |
c906108c SS |
1369 | |
1370 | /* Try to grab a null-terminated string. */ | |
c5aa993b | 1371 | while (!done) |
c906108c SS |
1372 | { |
1373 | if (target_read_memory (top_of_stack, four_chars, 4) != 0) | |
1374 | { | |
39f77062 | 1375 | inferior_ptid = saved_inferior_ptid; |
c906108c SS |
1376 | return NULL; |
1377 | } | |
1378 | for (i = 0; i < 4; i++) | |
1379 | { | |
1380 | exec_file_buffer[name_index++] = four_chars[i]; | |
1381 | done = (four_chars[i] == '\0'); | |
1382 | if (done) | |
1383 | break; | |
1384 | } | |
1385 | top_of_stack += 4; | |
1386 | } | |
1387 | ||
1388 | if (exec_file_buffer[0] == '\0') | |
1389 | { | |
39f77062 | 1390 | inferior_ptid = saved_inferior_ptid; |
c906108c SS |
1391 | return NULL; |
1392 | } | |
1393 | ||
39f77062 | 1394 | inferior_ptid = saved_inferior_ptid; |
c906108c SS |
1395 | return exec_file_buffer; |
1396 | } | |
1397 | ||
1398 | void | |
fba45db2 | 1399 | pre_fork_inferior (void) |
c906108c SS |
1400 | { |
1401 | int status; | |
1402 | ||
1403 | status = pipe (startup_semaphore.parent_channel); | |
1404 | if (status < 0) | |
1405 | { | |
1406 | warning ("error getting parent pipe for startup semaphore"); | |
1407 | return; | |
1408 | } | |
1409 | ||
1410 | status = pipe (startup_semaphore.child_channel); | |
1411 | if (status < 0) | |
1412 | { | |
1413 | warning ("error getting child pipe for startup semaphore"); | |
1414 | return; | |
1415 | } | |
1416 | } | |
c906108c | 1417 | \f |
c5aa993b | 1418 | |
c906108c SS |
1419 | /* Check to see if the given thread is alive. |
1420 | ||
1421 | This is a no-op, as ptrace doesn't support threads, so we just | |
1422 | return "TRUE". */ | |
1423 | ||
1424 | int | |
39f77062 | 1425 | child_thread_alive (ptid_t ptid) |
c906108c | 1426 | { |
c5aa993b | 1427 | return 1; |
c906108c SS |
1428 | } |
1429 | ||
1430 | #endif /* ! GDB_NATIVE_HPUX_11 */ |