1 /* Low-level child interface to ttrace.
3 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* The ttrace(2) system call didn't exist before HP-UX 10.30. Don't
24 try to compile this code unless we have it. */
29 #include "gdbthread.h"
33 #include "gdb_assert.h"
34 #include "gdb_string.h"
36 #include <sys/ttrace.h>
39 #include "inf-child.h"
40 #include "inf-ttrace.h"
44 /* HP-UX uses a threading model where each user-space thread
45 corresponds to a kernel thread. These kernel threads are called
46 lwps. The ttrace(2) interface gives us almost full control over
47 the threads, which makes it very easy to support them in GDB. We
48 identify the threads by process ID and lwp ID. The ttrace(2) also
49 provides us with a thread's user ID (in the `tts_user_tid' member
50 of `ttstate_t') but we don't use that (yet) as it isn't necessary
51 to uniquely label the thread. */
53 /* Number of active lwps. */
54 static int inf_ttrace_num_lwps
;
57 /* On HP-UX versions that have the ttrace(2) system call, we can
58 implement "hardware" watchpoints by fiddling with the protection of
59 pages in the address space that contain the variable being watched.
60 In order to implement this, we keep a dictionary of pages for which
61 we have changed the protection. */
63 struct inf_ttrace_page
65 CORE_ADDR addr
; /* Page address. */
66 int prot
; /* Protection. */
67 int refcount
; /* Reference count. */
68 struct inf_ttrace_page
*next
;
69 struct inf_ttrace_page
*prev
;
72 struct inf_ttrace_page_dict
74 struct inf_ttrace_page buckets
[128];
75 int pagesize
; /* Page size. */
76 int count
; /* Number of pages in this dictionary. */
77 } inf_ttrace_page_dict
;
79 struct inf_ttrace_private_thread_info
84 /* Number of lwps that are currently in a system call. */
85 static int inf_ttrace_num_lwps_in_syscall
;
87 /* Flag to indicate whether we should re-enable page protections after
89 static int inf_ttrace_reenable_page_protections
;
91 /* Enable system call events for process PID. */
94 inf_ttrace_enable_syscall_events (pid_t pid
)
99 gdb_assert (inf_ttrace_num_lwps_in_syscall
== 0);
101 if (ttrace (TT_PROC_GET_EVENT_MASK
, pid
, 0,
102 (uintptr_t)&tte
, sizeof tte
, 0) == -1)
103 perror_with_name (("ttrace"));
105 tte
.tte_events
|= (TTEVT_SYSCALL_ENTRY
| TTEVT_SYSCALL_RETURN
);
107 if (ttrace (TT_PROC_SET_EVENT_MASK
, pid
, 0,
108 (uintptr_t)&tte
, sizeof tte
, 0) == -1)
109 perror_with_name (("ttrace"));
111 if (ttrace (TT_PROC_GET_FIRST_LWP_STATE
, pid
, 0,
112 (uintptr_t)&tts
, sizeof tts
, 0) == -1)
113 perror_with_name (("ttrace"));
115 if (tts
.tts_flags
& TTS_INSYSCALL
)
116 inf_ttrace_num_lwps_in_syscall
++;
118 /* FIXME: Handle multiple threads. */
121 /* Disable system call events for process PID. */
124 inf_ttrace_disable_syscall_events (pid_t pid
)
128 gdb_assert (inf_ttrace_page_dict
.count
== 0);
130 if (ttrace (TT_PROC_GET_EVENT_MASK
, pid
, 0,
131 (uintptr_t)&tte
, sizeof tte
, 0) == -1)
132 perror_with_name (("ttrace"));
134 tte
.tte_events
&= ~(TTEVT_SYSCALL_ENTRY
| TTEVT_SYSCALL_RETURN
);
136 if (ttrace (TT_PROC_SET_EVENT_MASK
, pid
, 0,
137 (uintptr_t)&tte
, sizeof tte
, 0) == -1)
138 perror_with_name (("ttrace"));
140 inf_ttrace_num_lwps_in_syscall
= 0;
143 /* Get information about the page at address ADDR for process PID from
146 static struct inf_ttrace_page
*
147 inf_ttrace_get_page (pid_t pid
, CORE_ADDR addr
)
149 const int num_buckets
= ARRAY_SIZE (inf_ttrace_page_dict
.buckets
);
150 const int pagesize
= inf_ttrace_page_dict
.pagesize
;
152 struct inf_ttrace_page
*page
;
154 bucket
= (addr
/ pagesize
) % num_buckets
;
155 page
= &inf_ttrace_page_dict
.buckets
[bucket
];
158 if (page
->addr
== addr
)
167 /* Add the page at address ADDR for process PID to the dictionary. */
169 static struct inf_ttrace_page
*
170 inf_ttrace_add_page (pid_t pid
, CORE_ADDR addr
)
172 const int num_buckets
= ARRAY_SIZE (inf_ttrace_page_dict
.buckets
);
173 const int pagesize
= inf_ttrace_page_dict
.pagesize
;
175 struct inf_ttrace_page
*page
;
176 struct inf_ttrace_page
*prev
= NULL
;
178 bucket
= (addr
/ pagesize
) % num_buckets
;
179 page
= &inf_ttrace_page_dict
.buckets
[bucket
];
182 if (page
->addr
== addr
)
193 if (ttrace (TT_PROC_GET_MPROTECT
, pid
, 0,
194 addr
, 0, (uintptr_t)&prot
) == -1)
195 perror_with_name (("ttrace"));
197 page
= XMALLOC (struct inf_ttrace_page
);
206 inf_ttrace_page_dict
.count
++;
207 if (inf_ttrace_page_dict
.count
== 1)
208 inf_ttrace_enable_syscall_events (pid
);
210 if (inf_ttrace_num_lwps_in_syscall
== 0)
212 if (ttrace (TT_PROC_SET_MPROTECT
, pid
, 0,
213 addr
, pagesize
, prot
& ~PROT_WRITE
) == -1)
214 perror_with_name (("ttrace"));
221 /* Insert the page at address ADDR of process PID to the dictionary. */
224 inf_ttrace_insert_page (pid_t pid
, CORE_ADDR addr
)
226 struct inf_ttrace_page
*page
;
228 page
= inf_ttrace_get_page (pid
, addr
);
230 page
= inf_ttrace_add_page (pid
, addr
);
235 /* Remove the page at address ADDR of process PID from the dictionary. */
238 inf_ttrace_remove_page (pid_t pid
, CORE_ADDR addr
)
240 const int pagesize
= inf_ttrace_page_dict
.pagesize
;
241 struct inf_ttrace_page
*page
;
243 page
= inf_ttrace_get_page (pid
, addr
);
246 gdb_assert (page
->refcount
>= 0);
248 if (page
->refcount
== 0)
250 if (inf_ttrace_num_lwps_in_syscall
== 0)
252 if (ttrace (TT_PROC_SET_MPROTECT
, pid
, 0,
253 addr
, pagesize
, page
->prot
) == -1)
254 perror_with_name (("ttrace"));
257 inf_ttrace_page_dict
.count
--;
258 if (inf_ttrace_page_dict
.count
== 0)
259 inf_ttrace_disable_syscall_events (pid
);
261 page
->prev
->next
= page
->next
;
263 page
->next
->prev
= page
->prev
;
269 /* Mask the bits in PROT from the page protections that are currently
270 in the dictionary for process PID. */
273 inf_ttrace_mask_page_protections (pid_t pid
, int prot
)
275 const int num_buckets
= ARRAY_SIZE (inf_ttrace_page_dict
.buckets
);
276 const int pagesize
= inf_ttrace_page_dict
.pagesize
;
279 for (bucket
= 0; bucket
< num_buckets
; bucket
++)
281 struct inf_ttrace_page
*page
;
283 page
= inf_ttrace_page_dict
.buckets
[bucket
].next
;
286 if (ttrace (TT_PROC_SET_MPROTECT
, pid
, 0,
287 page
->addr
, pagesize
, page
->prot
& ~prot
) == -1)
288 perror_with_name (("ttrace"));
295 /* Write-protect the pages in the dictionary for process PID. */
298 inf_ttrace_enable_page_protections (pid_t pid
)
300 inf_ttrace_mask_page_protections (pid
, PROT_WRITE
);
303 /* Restore the protection of the pages in the dictionary for process
307 inf_ttrace_disable_page_protections (pid_t pid
)
309 inf_ttrace_mask_page_protections (pid
, 0);
312 /* Insert a "hardware" watchpoint for LEN bytes at address ADDR of
316 inf_ttrace_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
318 const int pagesize
= inf_ttrace_page_dict
.pagesize
;
319 pid_t pid
= ptid_get_pid (inferior_ptid
);
324 gdb_assert (type
== hw_write
);
326 page_addr
= (addr
/ pagesize
) * pagesize
;
327 num_pages
= (len
+ pagesize
- 1) / pagesize
;
329 for (page
= 0; page
< num_pages
; page
++, page_addr
+= pagesize
)
330 inf_ttrace_insert_page (pid
, page_addr
);
335 /* Remove a "hardware" watchpoint for LEN bytes at address ADDR of
339 inf_ttrace_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
341 const int pagesize
= inf_ttrace_page_dict
.pagesize
;
342 pid_t pid
= ptid_get_pid (inferior_ptid
);
347 gdb_assert (type
== hw_write
);
349 page_addr
= (addr
/ pagesize
) * pagesize
;
350 num_pages
= (len
+ pagesize
- 1) / pagesize
;
352 for (page
= 0; page
< num_pages
; page
++, page_addr
+= pagesize
)
353 inf_ttrace_remove_page (pid
, page_addr
);
359 inf_ttrace_can_use_hw_breakpoint (int type
, int len
, int ot
)
361 return (type
== bp_hardware_watchpoint
);
365 inf_ttrace_region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
370 /* Return non-zero if the current inferior was (potentially) stopped
371 by hitting a "hardware" watchpoint. */
374 inf_ttrace_stopped_by_watchpoint (void)
376 pid_t pid
= ptid_get_pid (inferior_ptid
);
377 lwpid_t lwpid
= ptid_get_lwp (inferior_ptid
);
380 if (inf_ttrace_page_dict
.count
> 0)
382 if (ttrace (TT_LWP_GET_STATE
, pid
, lwpid
,
383 (uintptr_t)&tts
, sizeof tts
, 0) == -1)
384 perror_with_name (("ttrace"));
386 if (tts
.tts_event
== TTEVT_SIGNAL
387 && tts
.tts_u
.tts_signal
.tts_signo
== SIGBUS
)
389 const int pagesize
= inf_ttrace_page_dict
.pagesize
;
390 void *addr
= tts
.tts_u
.tts_signal
.tts_siginfo
.si_addr
;
391 CORE_ADDR page_addr
= ((uintptr_t)addr
/ pagesize
) * pagesize
;
393 if (inf_ttrace_get_page (pid
, page_addr
))
402 /* When tracking a vfork(2), we cannot detach from the parent until
403 after the child has called exec(3) or has exited. If we are still
404 attached to the parent, this variable will be set to the process ID
405 of the parent. Otherwise it will be set to zero. */
406 static pid_t inf_ttrace_vfork_ppid
= -1;
409 inf_ttrace_follow_fork (struct target_ops
*ops
, int follow_child
)
412 lwpid_t lwpid
, flwpid
;
414 struct thread_info
*last_tp
= NULL
;
415 struct breakpoint
*step_resume_breakpoint
= NULL
;
416 CORE_ADDR step_range_start
= 0, step_range_end
= 0;
417 struct frame_id step_frame_id
= null_frame_id
;
419 /* FIXME: kettenis/20050720: This stuff should really be passed as
420 an argument by our caller. */
423 struct target_waitstatus status
;
425 get_last_target_status (&ptid
, &status
);
426 gdb_assert (status
.kind
== TARGET_WAITKIND_FORKED
427 || status
.kind
== TARGET_WAITKIND_VFORKED
);
429 pid
= ptid_get_pid (ptid
);
430 lwpid
= ptid_get_lwp (ptid
);
431 last_tp
= find_thread_pid (ptid
);
434 /* Get all important details that core GDB doesn't (and shouldn't)
436 if (ttrace (TT_LWP_GET_STATE
, pid
, lwpid
,
437 (uintptr_t)&tts
, sizeof tts
, 0) == -1)
438 perror_with_name (("ttrace"));
440 gdb_assert (tts
.tts_event
== TTEVT_FORK
|| tts
.tts_event
== TTEVT_VFORK
);
442 if (tts
.tts_u
.tts_fork
.tts_isparent
)
445 lwpid
= tts
.tts_lwpid
;
446 fpid
= tts
.tts_u
.tts_fork
.tts_fpid
;
447 flwpid
= tts
.tts_u
.tts_fork
.tts_flwpid
;
451 pid
= tts
.tts_u
.tts_fork
.tts_fpid
;
452 lwpid
= tts
.tts_u
.tts_fork
.tts_flwpid
;
454 flwpid
= tts
.tts_lwpid
;
459 struct inferior
*inf
;
461 /* Copy user stepping state to the new inferior thread. */
462 step_resume_breakpoint
= last_tp
->step_resume_breakpoint
;
463 step_range_start
= last_tp
->step_range_start
;
464 step_range_end
= last_tp
->step_range_end
;
465 step_frame_id
= last_tp
->step_frame_id
;
467 /* Otherwise, deleting the parent would get rid of this
469 last_tp
->step_resume_breakpoint
= NULL
;
471 inferior_ptid
= ptid_build (fpid
, flwpid
, 0);
472 inf
= add_inferior (fpid
);
473 inf
->attach_flag
= find_inferior_pid (pid
)->attach_flag
;
474 detach_breakpoints (pid
);
476 target_terminal_ours ();
477 fprintf_unfiltered (gdb_stdlog
, _("\
478 Attaching after fork to child process %ld.\n"), (long)fpid
);
482 inferior_ptid
= ptid_build (pid
, lwpid
, 0);
483 detach_breakpoints (fpid
);
485 target_terminal_ours ();
486 fprintf_unfiltered (gdb_stdlog
, _("\
487 Detaching after fork from child process %ld.\n"), (long)fpid
);
490 if (tts
.tts_event
== TTEVT_VFORK
)
492 gdb_assert (!tts
.tts_u
.tts_fork
.tts_isparent
);
496 /* We can't detach from the parent yet. */
497 inf_ttrace_vfork_ppid
= pid
;
499 reattach_breakpoints (fpid
);
503 if (ttrace (TT_PROC_DETACH
, fpid
, 0, 0, 0, 0) == -1)
504 perror_with_name (("ttrace"));
506 /* Wait till we get the TTEVT_VFORK event in the parent.
507 This indicates that the child has called exec(3) or has
508 exited and that the parent is ready to be traced again. */
509 if (ttrace_wait (pid
, lwpid
, TTRACE_WAITOK
, &tts
, sizeof tts
) == -1)
510 perror_with_name (("ttrace_wait"));
511 gdb_assert (tts
.tts_event
== TTEVT_VFORK
);
512 gdb_assert (tts
.tts_u
.tts_fork
.tts_isparent
);
514 reattach_breakpoints (pid
);
519 gdb_assert (tts
.tts_u
.tts_fork
.tts_isparent
);
523 if (ttrace (TT_PROC_DETACH
, pid
, 0, 0, 0, 0) == -1)
524 perror_with_name (("ttrace"));
528 if (ttrace (TT_PROC_DETACH
, fpid
, 0, 0, 0, 0) == -1)
529 perror_with_name (("ttrace"));
535 struct thread_info
*ti
;
537 /* The child will start out single-threaded. */
538 inf_ttrace_num_lwps
= 1;
539 inf_ttrace_num_lwps_in_syscall
= 0;
542 delete_thread_silent (ptid_build (pid
, lwpid
, 0));
543 detach_inferior (pid
);
545 /* Add child thread. inferior_ptid was already set above. */
546 ti
= add_thread_silent (inferior_ptid
);
548 xmalloc (sizeof (struct inf_ttrace_private_thread_info
));
549 memset (ti
->private, 0,
550 sizeof (struct inf_ttrace_private_thread_info
));
552 ti
->step_resume_breakpoint
= step_resume_breakpoint
;
553 ti
->step_range_start
= step_range_start
;
554 ti
->step_range_end
= step_range_end
;
555 ti
->step_frame_id
= step_frame_id
;
557 /* Reset breakpoints in the child as appropriate. */
558 follow_inferior_reset_breakpoints ();
565 /* File descriptors for pipes used as semaphores during initial
566 startup of an inferior. */
567 static int inf_ttrace_pfd1
[2];
568 static int inf_ttrace_pfd2
[2];
571 do_cleanup_pfds (void *dummy
)
573 close (inf_ttrace_pfd1
[0]);
574 close (inf_ttrace_pfd1
[1]);
575 close (inf_ttrace_pfd2
[0]);
576 close (inf_ttrace_pfd2
[1]);
580 inf_ttrace_prepare (void)
582 if (pipe (inf_ttrace_pfd1
) == -1)
583 perror_with_name (("pipe"));
585 if (pipe (inf_ttrace_pfd2
) == -1)
587 close (inf_ttrace_pfd1
[0]);
588 close (inf_ttrace_pfd2
[0]);
589 perror_with_name (("pipe"));
593 /* Prepare to be traced. */
598 struct cleanup
*old_chain
= make_cleanup (do_cleanup_pfds
, 0);
601 /* "Trace me, Dr. Memory!" */
602 if (ttrace (TT_PROC_SETTRC
, 0, 0, 0, TT_VERSION
, 0) == -1)
603 perror_with_name (("ttrace"));
605 /* Tell our parent that we are ready to be traced. */
606 if (write (inf_ttrace_pfd1
[1], &c
, sizeof c
) != sizeof c
)
607 perror_with_name (("write"));
609 /* Wait until our parent has set the initial event mask. */
610 if (read (inf_ttrace_pfd2
[0], &c
, sizeof c
) != sizeof c
)
611 perror_with_name (("read"));
613 do_cleanups (old_chain
);
616 /* Start tracing PID. */
619 inf_ttrace_him (struct target_ops
*ops
, int pid
)
621 struct cleanup
*old_chain
= make_cleanup (do_cleanup_pfds
, 0);
625 /* Wait until our child is ready to be traced. */
626 if (read (inf_ttrace_pfd1
[0], &c
, sizeof c
) != sizeof c
)
627 perror_with_name (("read"));
629 /* Set the initial event mask. */
630 memset (&tte
, 0, sizeof (tte
));
631 tte
.tte_events
|= TTEVT_EXEC
| TTEVT_EXIT
| TTEVT_FORK
| TTEVT_VFORK
;
632 tte
.tte_events
|= TTEVT_LWP_CREATE
| TTEVT_LWP_EXIT
| TTEVT_LWP_TERMINATE
;
633 #ifdef TTEVT_BPT_SSTEP
634 tte
.tte_events
|= TTEVT_BPT_SSTEP
;
636 tte
.tte_opts
|= TTEO_PROC_INHERIT
;
637 if (ttrace (TT_PROC_SET_EVENT_MASK
, pid
, 0,
638 (uintptr_t)&tte
, sizeof tte
, 0) == -1)
639 perror_with_name (("ttrace"));
641 /* Tell our child that we have set the initial event mask. */
642 if (write (inf_ttrace_pfd2
[1], &c
, sizeof c
) != sizeof c
)
643 perror_with_name (("write"));
645 do_cleanups (old_chain
);
649 /* On some targets, there must be some explicit synchronization
650 between the parent and child processes after the debugger forks,
651 and before the child execs the debuggee program. This call
652 basically gives permission for the child to exec. */
654 target_acknowledge_created_inferior (pid
);
656 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
657 be 1 or 2 depending on whether we're starting without or with a
659 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
661 /* On some targets, there must be some explicit actions taken after
662 the inferior has been started up. */
663 target_post_startup_inferior (pid_to_ptid (pid
));
667 inf_ttrace_create_inferior (struct target_ops
*ops
, char *exec_file
,
668 char *allargs
, char **env
, int from_tty
)
672 gdb_assert (inf_ttrace_num_lwps
== 0);
673 gdb_assert (inf_ttrace_num_lwps_in_syscall
== 0);
674 gdb_assert (inf_ttrace_page_dict
.count
== 0);
675 gdb_assert (inf_ttrace_reenable_page_protections
== 0);
676 gdb_assert (inf_ttrace_vfork_ppid
== -1);
678 pid
= fork_inferior (exec_file
, allargs
, env
, inf_ttrace_me
, NULL
,
679 inf_ttrace_prepare
, NULL
);
681 inf_ttrace_him (ops
, pid
);
685 inf_ttrace_mourn_inferior (struct target_ops
*ops
)
687 const int num_buckets
= ARRAY_SIZE (inf_ttrace_page_dict
.buckets
);
690 inf_ttrace_num_lwps
= 0;
691 inf_ttrace_num_lwps_in_syscall
= 0;
693 for (bucket
= 0; bucket
< num_buckets
; bucket
++)
695 struct inf_ttrace_page
*page
;
696 struct inf_ttrace_page
*next
;
698 page
= inf_ttrace_page_dict
.buckets
[bucket
].next
;
706 inf_ttrace_page_dict
.count
= 0;
709 generic_mourn_inferior ();
713 inf_ttrace_attach (struct target_ops
*ops
, char *args
, int from_tty
)
719 struct inferior
*inf
;
722 error_no_arg (_("process-id to attach"));
725 pid
= strtol (args
, &dummy
, 0);
726 if (pid
== 0 && args
== dummy
)
727 error (_("Illegal process-id: %s."), args
);
729 if (pid
== getpid ()) /* Trying to masturbate? */
730 error (_("I refuse to debug myself!"));
734 exec_file
= get_exec_file (0);
737 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
738 target_pid_to_str (pid_to_ptid (pid
)));
740 printf_unfiltered (_("Attaching to %s\n"),
741 target_pid_to_str (pid_to_ptid (pid
)));
743 gdb_flush (gdb_stdout
);
746 gdb_assert (inf_ttrace_num_lwps
== 0);
747 gdb_assert (inf_ttrace_num_lwps_in_syscall
== 0);
748 gdb_assert (inf_ttrace_vfork_ppid
== -1);
750 if (ttrace (TT_PROC_ATTACH
, pid
, 0, TT_KILL_ON_EXIT
, TT_VERSION
, 0) == -1)
751 perror_with_name (("ttrace"));
753 inf
= add_inferior (pid
);
754 inf
->attach_flag
= 1;
756 /* Set the initial event mask. */
757 memset (&tte
, 0, sizeof (tte
));
758 tte
.tte_events
|= TTEVT_EXEC
| TTEVT_EXIT
| TTEVT_FORK
| TTEVT_VFORK
;
759 tte
.tte_events
|= TTEVT_LWP_CREATE
| TTEVT_LWP_EXIT
| TTEVT_LWP_TERMINATE
;
760 #ifdef TTEVT_BPT_SSTEP
761 tte
.tte_events
|= TTEVT_BPT_SSTEP
;
763 tte
.tte_opts
|= TTEO_PROC_INHERIT
;
764 if (ttrace (TT_PROC_SET_EVENT_MASK
, pid
, 0,
765 (uintptr_t)&tte
, sizeof tte
, 0) == -1)
766 perror_with_name (("ttrace"));
770 /* We'll bump inf_ttrace_num_lwps up and add the private data to the
771 thread as soon as we get to inf_ttrace_wait. At this point, we
772 don't have lwpid info yet. */
773 inferior_ptid
= pid_to_ptid (pid
);
774 add_thread_silent (inferior_ptid
);
778 inf_ttrace_detach (struct target_ops
*ops
, char *args
, int from_tty
)
780 pid_t pid
= ptid_get_pid (inferior_ptid
);
785 char *exec_file
= get_exec_file (0);
788 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
789 target_pid_to_str (pid_to_ptid (pid
)));
790 gdb_flush (gdb_stdout
);
795 /* ??? The HP-UX 11.0 ttrace(2) manual page doesn't mention that we
796 can pass a signal number here. Does this really work? */
797 if (ttrace (TT_PROC_DETACH
, pid
, 0, 0, sig
, 0) == -1)
798 perror_with_name (("ttrace"));
800 if (inf_ttrace_vfork_ppid
!= -1)
802 if (ttrace (TT_PROC_DETACH
, inf_ttrace_vfork_ppid
, 0, 0, 0, 0) == -1)
803 perror_with_name (("ttrace"));
804 inf_ttrace_vfork_ppid
= -1;
807 inf_ttrace_num_lwps
= 0;
808 inf_ttrace_num_lwps_in_syscall
= 0;
810 inferior_ptid
= null_ptid
;
811 detach_inferior (pid
);
817 inf_ttrace_kill (struct target_ops
*ops
)
819 pid_t pid
= ptid_get_pid (inferior_ptid
);
824 if (ttrace (TT_PROC_EXIT
, pid
, 0, 0, 0, 0) == -1)
825 perror_with_name (("ttrace"));
826 /* ??? Is it necessary to call ttrace_wait() here? */
828 if (inf_ttrace_vfork_ppid
!= -1)
830 if (ttrace (TT_PROC_DETACH
, inf_ttrace_vfork_ppid
, 0, 0, 0, 0) == -1)
831 perror_with_name (("ttrace"));
832 inf_ttrace_vfork_ppid
= -1;
835 target_mourn_inferior ();
838 /* Check is a dying thread is dead by now, and delete it from GDBs
839 thread list if so. */
841 inf_ttrace_delete_dead_threads_callback (struct thread_info
*info
, void *arg
)
844 struct inf_ttrace_private_thread_info
*p
;
846 if (is_exited (info
->ptid
))
849 lwpid
= ptid_get_lwp (info
->ptid
);
850 p
= (struct inf_ttrace_private_thread_info
*) info
->private;
852 /* Check if an lwp that was dying is still there or not. */
853 if (p
->dying
&& (kill (lwpid
, 0) == -1))
855 delete_thread (info
->ptid
);
860 /* Resume the lwp pointed to by INFO, with REQUEST, and pass it signal
864 inf_ttrace_resume_lwp (struct thread_info
*info
, ttreq_t request
, int sig
)
866 pid_t pid
= ptid_get_pid (info
->ptid
);
867 lwpid_t lwpid
= ptid_get_lwp (info
->ptid
);
869 if (ttrace (request
, pid
, lwpid
, TT_NOPC
, sig
, 0) == -1)
871 struct inf_ttrace_private_thread_info
*p
872 = (struct inf_ttrace_private_thread_info
*) info
->private;
873 if (p
->dying
&& errno
== EPROTO
)
874 /* This is expected, it means the dying lwp is really gone
875 by now. If ttrace had an event to inform the debugger
876 the lwp is really gone, this wouldn't be needed. */
877 delete_thread (info
->ptid
);
879 /* This was really unexpected. */
880 perror_with_name (("ttrace"));
884 /* Callback for iterate_over_threads. */
887 inf_ttrace_resume_callback (struct thread_info
*info
, void *arg
)
889 if (!ptid_equal (info
->ptid
, inferior_ptid
) && !is_exited (info
->ptid
))
890 inf_ttrace_resume_lwp (info
, TT_LWP_CONTINUE
, 0);
896 inf_ttrace_resume (struct target_ops
*ops
,
897 ptid_t ptid
, int step
, enum target_signal signal
)
900 ttreq_t request
= step
? TT_LWP_SINGLE
: TT_LWP_CONTINUE
;
901 int sig
= target_signal_to_host (signal
);
902 struct thread_info
*info
;
904 /* A specific PTID means `step only this process id'. */
905 resume_all
= (ptid_equal (ptid
, minus_one_ptid
));
907 /* If resuming all threads, it's the current thread that should be
908 handled specially. */
910 ptid
= inferior_ptid
;
912 info
= find_thread_pid (ptid
);
913 inf_ttrace_resume_lwp (info
, request
, sig
);
916 /* Let all the other threads run too. */
917 iterate_over_threads (inf_ttrace_resume_callback
, NULL
);
921 inf_ttrace_wait (struct target_ops
*ops
,
922 ptid_t ptid
, struct target_waitstatus
*ourstatus
)
924 pid_t pid
= ptid_get_pid (ptid
);
925 lwpid_t lwpid
= ptid_get_lwp (ptid
);
927 struct thread_info
*ti
;
930 /* Until proven otherwise. */
931 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
936 gdb_assert (pid
!= 0 || lwpid
== 0);
942 if (ttrace_wait (pid
, lwpid
, TTRACE_WAITOK
, &tts
, sizeof tts
) == -1)
943 perror_with_name (("ttrace_wait"));
945 if (tts
.tts_event
== TTEVT_VFORK
&& tts
.tts_u
.tts_fork
.tts_isparent
)
947 if (inf_ttrace_vfork_ppid
!= -1)
949 gdb_assert (inf_ttrace_vfork_ppid
== tts
.tts_pid
);
951 if (ttrace (TT_PROC_DETACH
, tts
.tts_pid
, 0, 0, 0, 0) == -1)
952 perror_with_name (("ttrace"));
953 inf_ttrace_vfork_ppid
= -1;
956 tts
.tts_event
= TTEVT_NONE
;
959 clear_sigint_trap ();
961 while (tts
.tts_event
== TTEVT_NONE
);
963 /* Now that we've waited, we can re-enable the page protections. */
964 if (inf_ttrace_reenable_page_protections
)
966 gdb_assert (inf_ttrace_num_lwps_in_syscall
== 0);
967 inf_ttrace_enable_page_protections (tts
.tts_pid
);
968 inf_ttrace_reenable_page_protections
= 0;
971 ptid
= ptid_build (tts
.tts_pid
, tts
.tts_lwpid
, 0);
973 if (inf_ttrace_num_lwps
== 0)
975 struct thread_info
*ti
;
977 inf_ttrace_num_lwps
= 1;
979 /* This is the earliest we hear about the lwp member of
980 INFERIOR_PTID, after an attach or fork_inferior. */
981 gdb_assert (ptid_get_lwp (inferior_ptid
) == 0);
983 /* We haven't set the private member on the main thread yet. Do
985 ti
= find_thread_pid (inferior_ptid
);
986 gdb_assert (ti
!= NULL
&& ti
->private == NULL
);
988 xmalloc (sizeof (struct inf_ttrace_private_thread_info
));
989 memset (ti
->private, 0,
990 sizeof (struct inf_ttrace_private_thread_info
));
992 /* Notify the core that this ptid changed. This changes
993 inferior_ptid as well. */
994 thread_change_ptid (inferior_ptid
, ptid
);
997 switch (tts
.tts_event
)
999 #ifdef TTEVT_BPT_SSTEP
1000 case TTEVT_BPT_SSTEP
:
1001 /* Make it look like a breakpoint. */
1002 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1003 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1008 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
1009 ourstatus
->value
.execd_pathname
=
1010 xmalloc (tts
.tts_u
.tts_exec
.tts_pathlen
+ 1);
1011 if (ttrace (TT_PROC_GET_PATHNAME
, tts
.tts_pid
, 0,
1012 (uintptr_t)ourstatus
->value
.execd_pathname
,
1013 tts
.tts_u
.tts_exec
.tts_pathlen
, 0) == -1)
1014 perror_with_name (("ttrace"));
1015 ourstatus
->value
.execd_pathname
[tts
.tts_u
.tts_exec
.tts_pathlen
] = 0;
1017 /* At this point, all inserted breakpoints are gone. Doing this
1018 as soon as we detect an exec prevents the badness of deleting
1019 a breakpoint writing the current "shadow contents" to lift
1020 the bp. That shadow is NOT valid after an exec. */
1021 mark_breakpoints_out ();
1025 store_waitstatus (ourstatus
, tts
.tts_u
.tts_exit
.tts_exitcode
);
1026 inf_ttrace_num_lwps
= 0;
1030 related_ptid
= ptid_build (tts
.tts_u
.tts_fork
.tts_fpid
,
1031 tts
.tts_u
.tts_fork
.tts_flwpid
, 0);
1033 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
1034 ourstatus
->value
.related_pid
= related_ptid
;
1036 /* Make sure the other end of the fork is stopped too. */
1037 if (ttrace_wait (tts
.tts_u
.tts_fork
.tts_fpid
,
1038 tts
.tts_u
.tts_fork
.tts_flwpid
,
1039 TTRACE_WAITOK
, &tts
, sizeof tts
) == -1)
1040 perror_with_name (("ttrace_wait"));
1042 gdb_assert (tts
.tts_event
== TTEVT_FORK
);
1043 if (tts
.tts_u
.tts_fork
.tts_isparent
)
1045 related_ptid
= ptid_build (tts
.tts_u
.tts_fork
.tts_fpid
,
1046 tts
.tts_u
.tts_fork
.tts_flwpid
, 0);
1047 ptid
= ptid_build (tts
.tts_pid
, tts
.tts_lwpid
, 0);
1048 ourstatus
->value
.related_pid
= related_ptid
;
1053 gdb_assert (!tts
.tts_u
.tts_fork
.tts_isparent
);
1055 related_ptid
= ptid_build (tts
.tts_u
.tts_fork
.tts_fpid
,
1056 tts
.tts_u
.tts_fork
.tts_flwpid
, 0);
1058 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
1059 ourstatus
->value
.related_pid
= related_ptid
;
1061 /* HACK: To avoid touching the parent during the vfork, switch
1063 inferior_ptid
= ptid
;
1066 case TTEVT_LWP_CREATE
:
1067 lwpid
= tts
.tts_u
.tts_thread
.tts_target_lwpid
;
1068 ptid
= ptid_build (tts
.tts_pid
, lwpid
, 0);
1069 ti
= add_thread (ptid
);
1071 xmalloc (sizeof (struct inf_ttrace_private_thread_info
));
1072 memset (ti
->private, 0,
1073 sizeof (struct inf_ttrace_private_thread_info
));
1074 inf_ttrace_num_lwps
++;
1075 ptid
= ptid_build (tts
.tts_pid
, tts
.tts_lwpid
, 0);
1076 /* Let the lwp_create-caller thread continue. */
1077 ttrace (TT_LWP_CONTINUE
, ptid_get_pid (ptid
),
1078 ptid_get_lwp (ptid
), TT_NOPC
, 0, 0);
1079 /* Return without stopping the whole process. */
1080 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1083 case TTEVT_LWP_EXIT
:
1084 if (print_thread_events
)
1085 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid
));
1086 ti
= find_thread_pid (ptid
);
1087 gdb_assert (ti
!= NULL
);
1088 ((struct inf_ttrace_private_thread_info
*)ti
->private)->dying
= 1;
1089 inf_ttrace_num_lwps
--;
1090 /* Let the thread really exit. */
1091 ttrace (TT_LWP_CONTINUE
, ptid_get_pid (ptid
),
1092 ptid_get_lwp (ptid
), TT_NOPC
, 0, 0);
1093 /* Return without stopping the whole process. */
1094 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1097 case TTEVT_LWP_TERMINATE
:
1098 lwpid
= tts
.tts_u
.tts_thread
.tts_target_lwpid
;
1099 ptid
= ptid_build (tts
.tts_pid
, lwpid
, 0);
1100 if (print_thread_events
)
1101 printf_unfiltered(_("[%s has been terminated]\n"),
1102 target_pid_to_str (ptid
));
1103 ti
= find_thread_pid (ptid
);
1104 gdb_assert (ti
!= NULL
);
1105 ((struct inf_ttrace_private_thread_info
*)ti
->private)->dying
= 1;
1106 inf_ttrace_num_lwps
--;
1108 /* Resume the lwp_terminate-caller thread. */
1109 ptid
= ptid_build (tts
.tts_pid
, tts
.tts_lwpid
, 0);
1110 ttrace (TT_LWP_CONTINUE
, ptid_get_pid (ptid
),
1111 ptid_get_lwp (ptid
), TT_NOPC
, 0, 0);
1112 /* Return without stopping the whole process. */
1113 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
1117 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1118 ourstatus
->value
.sig
=
1119 target_signal_from_host (tts
.tts_u
.tts_signal
.tts_signo
);
1122 case TTEVT_SYSCALL_ENTRY
:
1123 gdb_assert (inf_ttrace_reenable_page_protections
== 0);
1124 inf_ttrace_num_lwps_in_syscall
++;
1125 if (inf_ttrace_num_lwps_in_syscall
== 1)
1127 /* A thread has just entered a system call. Disable any
1128 page protections as the kernel can't deal with them. */
1129 inf_ttrace_disable_page_protections (tts
.tts_pid
);
1131 ourstatus
->kind
= TARGET_WAITKIND_SYSCALL_ENTRY
;
1132 ourstatus
->value
.syscall_id
= tts
.tts_scno
;
1135 case TTEVT_SYSCALL_RETURN
:
1136 if (inf_ttrace_num_lwps_in_syscall
> 0)
1138 /* If the last thread has just left the system call, this
1139 would be a logical place to re-enable the page
1140 protections, but that doesn't work. We can't re-enable
1141 them until we've done another wait. */
1142 inf_ttrace_reenable_page_protections
=
1143 (inf_ttrace_num_lwps_in_syscall
== 1);
1144 inf_ttrace_num_lwps_in_syscall
--;
1146 ourstatus
->kind
= TARGET_WAITKIND_SYSCALL_RETURN
;
1147 ourstatus
->value
.syscall_id
= tts
.tts_scno
;
1151 gdb_assert (!"Unexpected ttrace event");
1155 /* Make sure all threads within the process are stopped. */
1156 if (ttrace (TT_PROC_STOP
, tts
.tts_pid
, 0, 0, 0, 0) == -1)
1157 perror_with_name (("ttrace"));
1159 /* Now that the whole process is stopped, check if any dying thread
1160 is really dead by now. If a dying thread is still alive, it will
1161 be stopped too, and will still show up in `info threads', tagged
1162 with "(Exiting)". We could make `info threads' prune dead
1163 threads instead via inf_ttrace_thread_alive, but doing this here
1164 has the advantage that a frontend is notificed sooner of thread
1165 exits. Note that a dying lwp is still alive, it still has to be
1166 resumed, like any other lwp. */
1167 iterate_over_threads (inf_ttrace_delete_dead_threads_callback
, NULL
);
1172 /* Transfer LEN bytes from ADDR in the inferior's memory into READBUF,
1173 and transfer LEN bytes from WRITEBUF into the inferior's memory at
1174 ADDR. Either READBUF or WRITEBUF may be null, in which case the
1175 corresponding transfer doesn't happen. Return the number of bytes
1176 actually transferred (which may be zero if an error occurs). */
1179 inf_ttrace_xfer_memory (CORE_ADDR addr
, ULONGEST len
,
1180 void *readbuf
, const void *writebuf
)
1182 pid_t pid
= ptid_get_pid (inferior_ptid
);
1184 /* HP-UX treats text space and data space differently. GDB however,
1185 doesn't really know the difference. Therefore we try both. Try
1186 text space before data space though because when we're writing
1187 into text space the instruction cache might need to be flushed. */
1190 && ttrace (TT_PROC_RDTEXT
, pid
, 0, addr
, len
, (uintptr_t)readbuf
) == -1
1191 && ttrace (TT_PROC_RDDATA
, pid
, 0, addr
, len
, (uintptr_t)readbuf
) == -1)
1195 && ttrace (TT_PROC_WRTEXT
, pid
, 0, addr
, len
, (uintptr_t)writebuf
) == -1
1196 && ttrace (TT_PROC_WRDATA
, pid
, 0, addr
, len
, (uintptr_t)writebuf
) == -1)
1203 inf_ttrace_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1204 const char *annex
, gdb_byte
*readbuf
,
1205 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1209 case TARGET_OBJECT_MEMORY
:
1210 return inf_ttrace_xfer_memory (offset
, len
, readbuf
, writebuf
);
1212 case TARGET_OBJECT_UNWIND_TABLE
:
1215 case TARGET_OBJECT_AUXV
:
1218 case TARGET_OBJECT_WCOOKIE
:
1226 /* Print status information about what we're accessing. */
1229 inf_ttrace_files_info (struct target_ops
*ignore
)
1231 struct inferior
*inf
= current_inferior ();
1232 printf_filtered (_("\tUsing the running image of %s %s.\n"),
1233 inf
->attach_flag
? "attached" : "child",
1234 target_pid_to_str (inferior_ptid
));
1238 inf_ttrace_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1243 /* Return a string describing the state of the thread specified by
1247 inf_ttrace_extra_thread_info (struct thread_info
*info
)
1249 struct inf_ttrace_private_thread_info
* private =
1250 (struct inf_ttrace_private_thread_info
*) info
->private;
1252 if (private != NULL
&& private->dying
)
1259 inf_ttrace_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1261 pid_t pid
= ptid_get_pid (ptid
);
1262 lwpid_t lwpid
= ptid_get_lwp (ptid
);
1263 static char buf
[128];
1266 xsnprintf (buf
, sizeof buf
, "process %ld",
1269 xsnprintf (buf
, sizeof buf
, "process %ld, lwp %ld",
1270 (long) pid
, (long) lwpid
);
1276 inf_ttrace_target (void)
1278 struct target_ops
*t
= inf_child_target ();
1280 t
->to_attach
= inf_ttrace_attach
;
1281 t
->to_detach
= inf_ttrace_detach
;
1282 t
->to_resume
= inf_ttrace_resume
;
1283 t
->to_wait
= inf_ttrace_wait
;
1284 t
->to_files_info
= inf_ttrace_files_info
;
1285 t
->to_can_use_hw_breakpoint
= inf_ttrace_can_use_hw_breakpoint
;
1286 t
->to_insert_watchpoint
= inf_ttrace_insert_watchpoint
;
1287 t
->to_remove_watchpoint
= inf_ttrace_remove_watchpoint
;
1288 t
->to_stopped_by_watchpoint
= inf_ttrace_stopped_by_watchpoint
;
1289 t
->to_region_ok_for_hw_watchpoint
=
1290 inf_ttrace_region_ok_for_hw_watchpoint
;
1291 t
->to_kill
= inf_ttrace_kill
;
1292 t
->to_create_inferior
= inf_ttrace_create_inferior
;
1293 t
->to_follow_fork
= inf_ttrace_follow_fork
;
1294 t
->to_mourn_inferior
= inf_ttrace_mourn_inferior
;
1295 t
->to_thread_alive
= inf_ttrace_thread_alive
;
1296 t
->to_extra_thread_info
= inf_ttrace_extra_thread_info
;
1297 t
->to_pid_to_str
= inf_ttrace_pid_to_str
;
1298 t
->to_xfer_partial
= inf_ttrace_xfer_partial
;
1305 /* Prevent warning from -Wmissing-prototypes. */
1306 void _initialize_hppa_hpux_nat (void);
1309 _initialize_inf_ttrace (void)
1312 inf_ttrace_page_dict
.pagesize
= getpagesize();