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