1 /* Low-level child interface to ttrace.
3 Copyright (C) 2004-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* The ttrace(2) system call didn't exist before HP-UX 10.30. Don't
23 try to compile this code unless we have it. */
28 #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,
317 struct expression *cond)
319 const int pagesize = inf_ttrace_page_dict.pagesize;
320 pid_t pid = ptid_get_pid (inferior_ptid);
325 gdb_assert (type == hw_write);
327 page_addr = (addr / pagesize) * pagesize;
328 num_pages = (len + pagesize - 1) / pagesize;
330 for (page = 0; page < num_pages; page++, page_addr += pagesize)
331 inf_ttrace_insert_page (pid, page_addr);
336 /* Remove a "hardware" watchpoint for LEN bytes at address ADDR of
340 inf_ttrace_remove_watchpoint (CORE_ADDR addr, int len, int type,
341 struct expression *cond)
343 const int pagesize = inf_ttrace_page_dict.pagesize;
344 pid_t pid = ptid_get_pid (inferior_ptid);
349 gdb_assert (type == hw_write);
351 page_addr = (addr / pagesize) * pagesize;
352 num_pages = (len + pagesize - 1) / pagesize;
354 for (page = 0; page < num_pages; page++, page_addr += pagesize)
355 inf_ttrace_remove_page (pid, page_addr);
361 inf_ttrace_can_use_hw_breakpoint (int type, int len, int ot)
363 return (type == bp_hardware_watchpoint);
367 inf_ttrace_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
372 /* Return non-zero if the current inferior was (potentially) stopped
373 by hitting a "hardware" watchpoint. */
376 inf_ttrace_stopped_by_watchpoint (void)
378 pid_t pid = ptid_get_pid (inferior_ptid);
379 lwpid_t lwpid = ptid_get_lwp (inferior_ptid);
382 if (inf_ttrace_page_dict.count > 0)
384 if (ttrace (TT_LWP_GET_STATE, pid, lwpid,
385 (uintptr_t)&tts, sizeof tts, 0) == -1)
386 perror_with_name (("ttrace"));
388 if (tts.tts_event == TTEVT_SIGNAL
389 && tts.tts_u.tts_signal.tts_signo == SIGBUS)
391 const int pagesize = inf_ttrace_page_dict.pagesize;
392 void *addr = tts.tts_u.tts_signal.tts_siginfo.si_addr;
393 CORE_ADDR page_addr = ((uintptr_t)addr / pagesize) * pagesize;
395 if (inf_ttrace_get_page (pid, page_addr))
404 /* When tracking a vfork(2), we cannot detach from the parent until
405 after the child has called exec(3) or has exited. If we are still
406 attached to the parent, this variable will be set to the process ID
407 of the parent. Otherwise it will be set to zero. */
408 static pid_t inf_ttrace_vfork_ppid = -1;
411 inf_ttrace_follow_fork (struct target_ops *ops, int follow_child)
414 lwpid_t lwpid, flwpid;
416 struct thread_info *tp = inferior_thread ();
418 gdb_assert (tp->pending_follow.kind == TARGET_WAITKIND_FORKED
419 || tp->pending_follow.kind == TARGET_WAITKIND_VFORKED);
421 pid = ptid_get_pid (inferior_ptid);
422 lwpid = ptid_get_lwp (inferior_ptid);
424 /* Get all important details that core GDB doesn't (and shouldn't)
426 if (ttrace (TT_LWP_GET_STATE, pid, lwpid,
427 (uintptr_t)&tts, sizeof tts, 0) == -1)
428 perror_with_name (("ttrace"));
430 gdb_assert (tts.tts_event == TTEVT_FORK || tts.tts_event == TTEVT_VFORK);
432 if (tts.tts_u.tts_fork.tts_isparent)
435 lwpid = tts.tts_lwpid;
436 fpid = tts.tts_u.tts_fork.tts_fpid;
437 flwpid = tts.tts_u.tts_fork.tts_flwpid;
441 pid = tts.tts_u.tts_fork.tts_fpid;
442 lwpid = tts.tts_u.tts_fork.tts_flwpid;
444 flwpid = tts.tts_lwpid;
449 struct inferior *inf;
450 struct inferior *parent_inf;
452 parent_inf = find_inferior_pid (pid);
454 inferior_ptid = ptid_build (fpid, flwpid, 0);
455 inf = add_inferior (fpid);
456 inf->attach_flag = parent_inf->attach_flag;
457 inf->pspace = parent_inf->pspace;
458 inf->aspace = parent_inf->aspace;
459 copy_terminal_info (inf, parent_inf);
460 detach_breakpoints (pid);
462 target_terminal_ours ();
463 fprintf_unfiltered (gdb_stdlog,
464 _("Attaching after fork to child process %ld.\n"),
469 inferior_ptid = ptid_build (pid, lwpid, 0);
470 detach_breakpoints (fpid);
472 target_terminal_ours ();
473 fprintf_unfiltered (gdb_stdlog,
474 _("Detaching after fork from child process %ld.\n"),
478 if (tts.tts_event == TTEVT_VFORK)
480 gdb_assert (!tts.tts_u.tts_fork.tts_isparent);
484 /* We can't detach from the parent yet. */
485 inf_ttrace_vfork_ppid = pid;
487 reattach_breakpoints (fpid);
491 if (ttrace (TT_PROC_DETACH, fpid, 0, 0, 0, 0) == -1)
492 perror_with_name (("ttrace"));
494 /* Wait till we get the TTEVT_VFORK event in the parent.
495 This indicates that the child has called exec(3) or has
496 exited and that the parent is ready to be traced again. */
497 if (ttrace_wait (pid, lwpid, TTRACE_WAITOK, &tts, sizeof tts) == -1)
498 perror_with_name (("ttrace_wait"));
499 gdb_assert (tts.tts_event == TTEVT_VFORK);
500 gdb_assert (tts.tts_u.tts_fork.tts_isparent);
502 reattach_breakpoints (pid);
507 gdb_assert (tts.tts_u.tts_fork.tts_isparent);
511 if (ttrace (TT_PROC_DETACH, pid, 0, 0, 0, 0) == -1)
512 perror_with_name (("ttrace"));
516 if (ttrace (TT_PROC_DETACH, fpid, 0, 0, 0, 0) == -1)
517 perror_with_name (("ttrace"));
523 struct thread_info *ti;
525 /* The child will start out single-threaded. */
526 inf_ttrace_num_lwps = 1;
527 inf_ttrace_num_lwps_in_syscall = 0;
530 delete_thread_silent (ptid_build (pid, lwpid, 0));
531 detach_inferior (pid);
533 /* Add child thread. inferior_ptid was already set above. */
534 ti = add_thread_silent (inferior_ptid);
536 xmalloc (sizeof (struct inf_ttrace_private_thread_info));
537 memset (ti->private, 0,
538 sizeof (struct inf_ttrace_private_thread_info));
545 /* File descriptors for pipes used as semaphores during initial
546 startup of an inferior. */
547 static int inf_ttrace_pfd1[2];
548 static int inf_ttrace_pfd2[2];
551 do_cleanup_pfds (void *dummy)
553 close (inf_ttrace_pfd1[0]);
554 close (inf_ttrace_pfd1[1]);
555 close (inf_ttrace_pfd2[0]);
556 close (inf_ttrace_pfd2[1]);
560 inf_ttrace_prepare (void)
562 if (pipe (inf_ttrace_pfd1) == -1)
563 perror_with_name (("pipe"));
565 if (pipe (inf_ttrace_pfd2) == -1)
567 close (inf_ttrace_pfd1[0]);
568 close (inf_ttrace_pfd2[0]);
569 perror_with_name (("pipe"));
573 /* Prepare to be traced. */
578 struct cleanup *old_chain = make_cleanup (do_cleanup_pfds, 0);
581 /* "Trace me, Dr. Memory!" */
582 if (ttrace (TT_PROC_SETTRC, 0, 0, 0, TT_VERSION, 0) == -1)
583 perror_with_name (("ttrace"));
585 /* Tell our parent that we are ready to be traced. */
586 if (write (inf_ttrace_pfd1[1], &c, sizeof c) != sizeof c)
587 perror_with_name (("write"));
589 /* Wait until our parent has set the initial event mask. */
590 if (read (inf_ttrace_pfd2[0], &c, sizeof c) != sizeof c)
591 perror_with_name (("read"));
593 do_cleanups (old_chain);
596 /* Start tracing PID. */
599 inf_ttrace_him (struct target_ops *ops, int pid)
601 struct cleanup *old_chain = make_cleanup (do_cleanup_pfds, 0);
605 /* Wait until our child is ready to be traced. */
606 if (read (inf_ttrace_pfd1[0], &c, sizeof c) != sizeof c)
607 perror_with_name (("read"));
609 /* Set the initial event mask. */
610 memset (&tte, 0, sizeof (tte));
611 tte.tte_events |= TTEVT_EXEC | TTEVT_EXIT | TTEVT_FORK | TTEVT_VFORK;
612 tte.tte_events |= TTEVT_LWP_CREATE | TTEVT_LWP_EXIT | TTEVT_LWP_TERMINATE;
613 #ifdef TTEVT_BPT_SSTEP
614 tte.tte_events |= TTEVT_BPT_SSTEP;
616 tte.tte_opts |= TTEO_PROC_INHERIT;
617 if (ttrace (TT_PROC_SET_EVENT_MASK, pid, 0,
618 (uintptr_t)&tte, sizeof tte, 0) == -1)
619 perror_with_name (("ttrace"));
621 /* Tell our child that we have set the initial event mask. */
622 if (write (inf_ttrace_pfd2[1], &c, sizeof c) != sizeof c)
623 perror_with_name (("write"));
625 do_cleanups (old_chain);
629 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
630 be 1 or 2 depending on whether we're starting without or with a
632 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
634 /* On some targets, there must be some explicit actions taken after
635 the inferior has been started up. */
636 target_post_startup_inferior (pid_to_ptid (pid));
640 inf_ttrace_create_inferior (struct target_ops *ops, char *exec_file,
641 char *allargs, char **env, int from_tty)
645 gdb_assert (inf_ttrace_num_lwps == 0);
646 gdb_assert (inf_ttrace_num_lwps_in_syscall == 0);
647 gdb_assert (inf_ttrace_page_dict.count == 0);
648 gdb_assert (inf_ttrace_reenable_page_protections == 0);
649 gdb_assert (inf_ttrace_vfork_ppid == -1);
651 pid = fork_inferior (exec_file, allargs, env, inf_ttrace_me, NULL,
652 inf_ttrace_prepare, NULL, NULL);
654 inf_ttrace_him (ops, pid);
658 inf_ttrace_mourn_inferior (struct target_ops *ops)
660 const int num_buckets = ARRAY_SIZE (inf_ttrace_page_dict.buckets);
663 inf_ttrace_num_lwps = 0;
664 inf_ttrace_num_lwps_in_syscall = 0;
666 for (bucket = 0; bucket < num_buckets; bucket++)
668 struct inf_ttrace_page *page;
669 struct inf_ttrace_page *next;
671 page = inf_ttrace_page_dict.buckets[bucket].next;
679 inf_ttrace_page_dict.count = 0;
682 generic_mourn_inferior ();
685 /* Assuming we just attached the debugger to a new inferior, create
686 a new thread_info structure for each thread, and add it to our
690 inf_ttrace_create_threads_after_attach (int pid)
695 struct thread_info *ti;
697 status = ttrace (TT_PROC_GET_FIRST_LWP_STATE, pid, 0,
698 (uintptr_t) &tts, sizeof (ttstate_t), 0);
700 perror_with_name (_("TT_PROC_GET_FIRST_LWP_STATE ttrace call failed"));
701 gdb_assert (tts.tts_pid == pid);
703 /* Add the stopped thread. */
704 ptid = ptid_build (pid, tts.tts_lwpid, 0);
705 ti = add_thread (ptid);
706 ti->private = xzalloc (sizeof (struct inf_ttrace_private_thread_info));
707 inf_ttrace_num_lwps++;
709 /* We use the "first stopped thread" as the currently active thread. */
710 inferior_ptid = ptid;
712 /* Iterative over all the remaining threads. */
718 status = ttrace (TT_PROC_GET_NEXT_LWP_STATE, pid, 0,
719 (uintptr_t) &tts, sizeof (ttstate_t), 0);
721 perror_with_name (_("TT_PROC_GET_NEXT_LWP_STATE ttrace call failed"));
723 break; /* End of list. */
725 ptid = ptid_build (tts.tts_pid, tts.tts_lwpid, 0);
726 ti = add_thread (ptid);
727 ti->private = xzalloc (sizeof (struct inf_ttrace_private_thread_info));
728 inf_ttrace_num_lwps++;
733 inf_ttrace_attach (struct target_ops *ops, char *args, int from_tty)
738 struct inferior *inf;
740 pid = parse_pid_to_attach (args);
742 if (pid == getpid ()) /* Trying to masturbate? */
743 error (_("I refuse to debug myself!"));
747 exec_file = get_exec_file (0);
750 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
751 target_pid_to_str (pid_to_ptid (pid)));
753 printf_unfiltered (_("Attaching to %s\n"),
754 target_pid_to_str (pid_to_ptid (pid)));
756 gdb_flush (gdb_stdout);
759 gdb_assert (inf_ttrace_num_lwps == 0);
760 gdb_assert (inf_ttrace_num_lwps_in_syscall == 0);
761 gdb_assert (inf_ttrace_vfork_ppid == -1);
763 if (ttrace (TT_PROC_ATTACH, pid, 0, TT_KILL_ON_EXIT, TT_VERSION, 0) == -1)
764 perror_with_name (("ttrace"));
766 inf = current_inferior ();
767 inferior_appeared (inf, pid);
768 inf->attach_flag = 1;
770 /* Set the initial event mask. */
771 memset (&tte, 0, sizeof (tte));
772 tte.tte_events |= TTEVT_EXEC | TTEVT_EXIT | TTEVT_FORK | TTEVT_VFORK;
773 tte.tte_events |= TTEVT_LWP_CREATE | TTEVT_LWP_EXIT | TTEVT_LWP_TERMINATE;
774 #ifdef TTEVT_BPT_SSTEP
775 tte.tte_events |= TTEVT_BPT_SSTEP;
777 tte.tte_opts |= TTEO_PROC_INHERIT;
778 if (ttrace (TT_PROC_SET_EVENT_MASK, pid, 0,
779 (uintptr_t)&tte, sizeof tte, 0) == -1)
780 perror_with_name (("ttrace"));
784 inf_ttrace_create_threads_after_attach (pid);
788 inf_ttrace_detach (struct target_ops *ops, char *args, int from_tty)
790 pid_t pid = ptid_get_pid (inferior_ptid);
795 char *exec_file = get_exec_file (0);
798 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
799 target_pid_to_str (pid_to_ptid (pid)));
800 gdb_flush (gdb_stdout);
805 /* ??? The HP-UX 11.0 ttrace(2) manual page doesn't mention that we
806 can pass a signal number here. Does this really work? */
807 if (ttrace (TT_PROC_DETACH, pid, 0, 0, sig, 0) == -1)
808 perror_with_name (("ttrace"));
810 if (inf_ttrace_vfork_ppid != -1)
812 if (ttrace (TT_PROC_DETACH, inf_ttrace_vfork_ppid, 0, 0, 0, 0) == -1)
813 perror_with_name (("ttrace"));
814 inf_ttrace_vfork_ppid = -1;
817 inf_ttrace_num_lwps = 0;
818 inf_ttrace_num_lwps_in_syscall = 0;
820 inferior_ptid = null_ptid;
821 detach_inferior (pid);
827 inf_ttrace_kill (struct target_ops *ops)
829 pid_t pid = ptid_get_pid (inferior_ptid);
834 if (ttrace (TT_PROC_EXIT, pid, 0, 0, 0, 0) == -1)
835 perror_with_name (("ttrace"));
836 /* ??? Is it necessary to call ttrace_wait() here? */
838 if (inf_ttrace_vfork_ppid != -1)
840 if (ttrace (TT_PROC_DETACH, inf_ttrace_vfork_ppid, 0, 0, 0, 0) == -1)
841 perror_with_name (("ttrace"));
842 inf_ttrace_vfork_ppid = -1;
845 target_mourn_inferior ();
848 /* Check is a dying thread is dead by now, and delete it from GDBs
849 thread list if so. */
851 inf_ttrace_delete_dead_threads_callback (struct thread_info *info, void *arg)
854 struct inf_ttrace_private_thread_info *p;
856 if (is_exited (info->ptid))
859 lwpid = ptid_get_lwp (info->ptid);
860 p = (struct inf_ttrace_private_thread_info *) info->private;
862 /* Check if an lwp that was dying is still there or not. */
863 if (p->dying && (kill (lwpid, 0) == -1))
865 delete_thread (info->ptid);
870 /* Resume the lwp pointed to by INFO, with REQUEST, and pass it signal
874 inf_ttrace_resume_lwp (struct thread_info *info, ttreq_t request, int sig)
876 pid_t pid = ptid_get_pid (info->ptid);
877 lwpid_t lwpid = ptid_get_lwp (info->ptid);
879 if (ttrace (request, pid, lwpid, TT_NOPC, sig, 0) == -1)
881 struct inf_ttrace_private_thread_info *p
882 = (struct inf_ttrace_private_thread_info *) info->private;
883 if (p->dying && errno == EPROTO)
884 /* This is expected, it means the dying lwp is really gone
885 by now. If ttrace had an event to inform the debugger
886 the lwp is really gone, this wouldn't be needed. */
887 delete_thread (info->ptid);
889 /* This was really unexpected. */
890 perror_with_name (("ttrace"));
894 /* Callback for iterate_over_threads. */
897 inf_ttrace_resume_callback (struct thread_info *info, void *arg)
899 if (!ptid_equal (info->ptid, inferior_ptid) && !is_exited (info->ptid))
900 inf_ttrace_resume_lwp (info, TT_LWP_CONTINUE, 0);
906 inf_ttrace_resume (struct target_ops *ops,
907 ptid_t ptid, int step, enum gdb_signal signal)
910 ttreq_t request = step ? TT_LWP_SINGLE : TT_LWP_CONTINUE;
911 int sig = gdb_signal_to_host (signal);
912 struct thread_info *info;
914 /* A specific PTID means `step only this process id'. */
915 resume_all = (ptid_equal (ptid, minus_one_ptid));
917 /* If resuming all threads, it's the current thread that should be
918 handled specially. */
920 ptid = inferior_ptid;
922 info = find_thread_ptid (ptid);
923 inf_ttrace_resume_lwp (info, request, sig);
926 /* Let all the other threads run too. */
927 iterate_over_threads (inf_ttrace_resume_callback, NULL);
931 inf_ttrace_wait (struct target_ops *ops,
932 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
934 pid_t pid = ptid_get_pid (ptid);
935 lwpid_t lwpid = ptid_get_lwp (ptid);
937 struct thread_info *ti;
940 /* Until proven otherwise. */
941 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
946 gdb_assert (pid != 0 || lwpid == 0);
952 if (ttrace_wait (pid, lwpid, TTRACE_WAITOK, &tts, sizeof tts) == -1)
953 perror_with_name (("ttrace_wait"));
955 if (tts.tts_event == TTEVT_VFORK && tts.tts_u.tts_fork.tts_isparent)
957 if (inf_ttrace_vfork_ppid != -1)
959 gdb_assert (inf_ttrace_vfork_ppid == tts.tts_pid);
961 if (ttrace (TT_PROC_DETACH, tts.tts_pid, 0, 0, 0, 0) == -1)
962 perror_with_name (("ttrace"));
963 inf_ttrace_vfork_ppid = -1;
966 tts.tts_event = TTEVT_NONE;
969 clear_sigint_trap ();
971 while (tts.tts_event == TTEVT_NONE);
973 /* Now that we've waited, we can re-enable the page protections. */
974 if (inf_ttrace_reenable_page_protections)
976 gdb_assert (inf_ttrace_num_lwps_in_syscall == 0);
977 inf_ttrace_enable_page_protections (tts.tts_pid);
978 inf_ttrace_reenable_page_protections = 0;
981 ptid = ptid_build (tts.tts_pid, tts.tts_lwpid, 0);
983 if (inf_ttrace_num_lwps == 0)
985 struct thread_info *ti;
987 inf_ttrace_num_lwps = 1;
989 /* This is the earliest we hear about the lwp member of
990 INFERIOR_PTID, after an attach or fork_inferior. */
991 gdb_assert (ptid_get_lwp (inferior_ptid) == 0);
993 /* We haven't set the private member on the main thread yet. Do
995 ti = find_thread_ptid (inferior_ptid);
996 gdb_assert (ti != NULL && ti->private == NULL);
998 xmalloc (sizeof (struct inf_ttrace_private_thread_info));
999 memset (ti->private, 0,
1000 sizeof (struct inf_ttrace_private_thread_info));
1002 /* Notify the core that this ptid changed. This changes
1003 inferior_ptid as well. */
1004 thread_change_ptid (inferior_ptid, ptid);
1007 switch (tts.tts_event)
1009 #ifdef TTEVT_BPT_SSTEP
1010 case TTEVT_BPT_SSTEP:
1011 /* Make it look like a breakpoint. */
1012 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1013 ourstatus->value.sig = GDB_SIGNAL_TRAP;
1018 ourstatus->kind = TARGET_WAITKIND_EXECD;
1019 ourstatus->value.execd_pathname =
1020 xmalloc (tts.tts_u.tts_exec.tts_pathlen + 1);
1021 if (ttrace (TT_PROC_GET_PATHNAME, tts.tts_pid, 0,
1022 (uintptr_t)ourstatus->value.execd_pathname,
1023 tts.tts_u.tts_exec.tts_pathlen, 0) == -1)
1024 perror_with_name (("ttrace"));
1025 ourstatus->value.execd_pathname[tts.tts_u.tts_exec.tts_pathlen] = 0;
1027 /* At this point, all inserted breakpoints are gone. Doing this
1028 as soon as we detect an exec prevents the badness of deleting
1029 a breakpoint writing the current "shadow contents" to lift
1030 the bp. That shadow is NOT valid after an exec. */
1031 mark_breakpoints_out ();
1035 store_waitstatus (ourstatus, tts.tts_u.tts_exit.tts_exitcode);
1036 inf_ttrace_num_lwps = 0;
1040 related_ptid = ptid_build (tts.tts_u.tts_fork.tts_fpid,
1041 tts.tts_u.tts_fork.tts_flwpid, 0);
1043 ourstatus->kind = TARGET_WAITKIND_FORKED;
1044 ourstatus->value.related_pid = related_ptid;
1046 /* Make sure the other end of the fork is stopped too. */
1047 if (ttrace_wait (tts.tts_u.tts_fork.tts_fpid,
1048 tts.tts_u.tts_fork.tts_flwpid,
1049 TTRACE_WAITOK, &tts, sizeof tts) == -1)
1050 perror_with_name (("ttrace_wait"));
1052 gdb_assert (tts.tts_event == TTEVT_FORK);
1053 if (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);
1057 ptid = ptid_build (tts.tts_pid, tts.tts_lwpid, 0);
1058 ourstatus->value.related_pid = related_ptid;
1063 gdb_assert (!tts.tts_u.tts_fork.tts_isparent);
1065 related_ptid = ptid_build (tts.tts_u.tts_fork.tts_fpid,
1066 tts.tts_u.tts_fork.tts_flwpid, 0);
1068 ourstatus->kind = TARGET_WAITKIND_VFORKED;
1069 ourstatus->value.related_pid = related_ptid;
1071 /* HACK: To avoid touching the parent during the vfork, switch
1073 inferior_ptid = ptid;
1076 case TTEVT_LWP_CREATE:
1077 lwpid = tts.tts_u.tts_thread.tts_target_lwpid;
1078 ptid = ptid_build (tts.tts_pid, lwpid, 0);
1079 ti = add_thread (ptid);
1081 xmalloc (sizeof (struct inf_ttrace_private_thread_info));
1082 memset (ti->private, 0,
1083 sizeof (struct inf_ttrace_private_thread_info));
1084 inf_ttrace_num_lwps++;
1085 ptid = ptid_build (tts.tts_pid, tts.tts_lwpid, 0);
1086 /* Let the lwp_create-caller thread continue. */
1087 ttrace (TT_LWP_CONTINUE, ptid_get_pid (ptid),
1088 ptid_get_lwp (ptid), TT_NOPC, 0, 0);
1089 /* Return without stopping the whole process. */
1090 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1093 case TTEVT_LWP_EXIT:
1094 if (print_thread_events)
1095 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid));
1096 ti = find_thread_ptid (ptid);
1097 gdb_assert (ti != NULL);
1098 ((struct inf_ttrace_private_thread_info *)ti->private)->dying = 1;
1099 inf_ttrace_num_lwps--;
1100 /* Let the thread really exit. */
1101 ttrace (TT_LWP_CONTINUE, ptid_get_pid (ptid),
1102 ptid_get_lwp (ptid), TT_NOPC, 0, 0);
1103 /* Return without stopping the whole process. */
1104 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1107 case TTEVT_LWP_TERMINATE:
1108 lwpid = tts.tts_u.tts_thread.tts_target_lwpid;
1109 ptid = ptid_build (tts.tts_pid, lwpid, 0);
1110 if (print_thread_events)
1111 printf_unfiltered(_("[%s has been terminated]\n"),
1112 target_pid_to_str (ptid));
1113 ti = find_thread_ptid (ptid);
1114 gdb_assert (ti != NULL);
1115 ((struct inf_ttrace_private_thread_info *)ti->private)->dying = 1;
1116 inf_ttrace_num_lwps--;
1118 /* Resume the lwp_terminate-caller thread. */
1119 ptid = ptid_build (tts.tts_pid, tts.tts_lwpid, 0);
1120 ttrace (TT_LWP_CONTINUE, ptid_get_pid (ptid),
1121 ptid_get_lwp (ptid), TT_NOPC, 0, 0);
1122 /* Return without stopping the whole process. */
1123 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1127 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1128 ourstatus->value.sig =
1129 gdb_signal_from_host (tts.tts_u.tts_signal.tts_signo);
1132 case TTEVT_SYSCALL_ENTRY:
1133 gdb_assert (inf_ttrace_reenable_page_protections == 0);
1134 inf_ttrace_num_lwps_in_syscall++;
1135 if (inf_ttrace_num_lwps_in_syscall == 1)
1137 /* A thread has just entered a system call. Disable any
1138 page protections as the kernel can't deal with them. */
1139 inf_ttrace_disable_page_protections (tts.tts_pid);
1141 ourstatus->kind = TARGET_WAITKIND_SYSCALL_ENTRY;
1142 ourstatus->value.syscall_number = tts.tts_scno;
1145 case TTEVT_SYSCALL_RETURN:
1146 if (inf_ttrace_num_lwps_in_syscall > 0)
1148 /* If the last thread has just left the system call, this
1149 would be a logical place to re-enable the page
1150 protections, but that doesn't work. We can't re-enable
1151 them until we've done another wait. */
1152 inf_ttrace_reenable_page_protections =
1153 (inf_ttrace_num_lwps_in_syscall == 1);
1154 inf_ttrace_num_lwps_in_syscall--;
1156 ourstatus->kind = TARGET_WAITKIND_SYSCALL_RETURN;
1157 ourstatus->value.syscall_number = tts.tts_scno;
1161 gdb_assert (!"Unexpected ttrace event");
1165 /* Make sure all threads within the process are stopped. */
1166 if (ttrace (TT_PROC_STOP, tts.tts_pid, 0, 0, 0, 0) == -1)
1167 perror_with_name (("ttrace"));
1169 /* Now that the whole process is stopped, check if any dying thread
1170 is really dead by now. If a dying thread is still alive, it will
1171 be stopped too, and will still show up in `info threads', tagged
1172 with "(Exiting)". We could make `info threads' prune dead
1173 threads instead via inf_ttrace_thread_alive, but doing this here
1174 has the advantage that a frontend is notificed sooner of thread
1175 exits. Note that a dying lwp is still alive, it still has to be
1176 resumed, like any other lwp. */
1177 iterate_over_threads (inf_ttrace_delete_dead_threads_callback, NULL);
1182 /* Transfer LEN bytes from ADDR in the inferior's memory into READBUF,
1183 and transfer LEN bytes from WRITEBUF into the inferior's memory at
1184 ADDR. Either READBUF or WRITEBUF may be null, in which case the
1185 corresponding transfer doesn't happen. Return the number of bytes
1186 actually transferred (which may be zero if an error occurs). */
1189 inf_ttrace_xfer_memory (CORE_ADDR addr, ULONGEST len,
1190 void *readbuf, const void *writebuf)
1192 pid_t pid = ptid_get_pid (inferior_ptid);
1194 /* HP-UX treats text space and data space differently. GDB however,
1195 doesn't really know the difference. Therefore we try both. Try
1196 text space before data space though because when we're writing
1197 into text space the instruction cache might need to be flushed. */
1200 && ttrace (TT_PROC_RDTEXT, pid, 0, addr, len, (uintptr_t)readbuf) == -1
1201 && ttrace (TT_PROC_RDDATA, pid, 0, addr, len, (uintptr_t)readbuf) == -1)
1205 && ttrace (TT_PROC_WRTEXT, pid, 0, addr, len, (uintptr_t)writebuf) == -1
1206 && ttrace (TT_PROC_WRDATA, pid, 0, addr, len, (uintptr_t)writebuf) == -1)
1213 inf_ttrace_xfer_partial (struct target_ops *ops, enum target_object object,
1214 const char *annex, gdb_byte *readbuf,
1215 const gdb_byte *writebuf,
1216 ULONGEST offset, LONGEST len)
1220 case TARGET_OBJECT_MEMORY:
1221 return inf_ttrace_xfer_memory (offset, len, readbuf, writebuf);
1223 case TARGET_OBJECT_UNWIND_TABLE:
1226 case TARGET_OBJECT_AUXV:
1229 case TARGET_OBJECT_WCOOKIE:
1237 /* Print status information about what we're accessing. */
1240 inf_ttrace_files_info (struct target_ops *ignore)
1242 struct inferior *inf = current_inferior ();
1243 printf_filtered (_("\tUsing the running image of %s %s.\n"),
1244 inf->attach_flag ? "attached" : "child",
1245 target_pid_to_str (inferior_ptid));
1249 inf_ttrace_thread_alive (struct target_ops *ops, ptid_t ptid)
1254 /* Return a string describing the state of the thread specified by
1258 inf_ttrace_extra_thread_info (struct thread_info *info)
1260 struct inf_ttrace_private_thread_info* private =
1261 (struct inf_ttrace_private_thread_info *) info->private;
1263 if (private != NULL && private->dying)
1270 inf_ttrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
1272 pid_t pid = ptid_get_pid (ptid);
1273 lwpid_t lwpid = ptid_get_lwp (ptid);
1274 static char buf[128];
1277 xsnprintf (buf, sizeof buf, "process %ld",
1280 xsnprintf (buf, sizeof buf, "process %ld, lwp %ld",
1281 (long) pid, (long) lwpid);
1286 /* Implement the get_ada_task_ptid target_ops method. */
1289 inf_ttrace_get_ada_task_ptid (long lwp, long thread)
1291 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
1296 inf_ttrace_target (void)
1298 struct target_ops *t = inf_child_target ();
1300 t->to_attach = inf_ttrace_attach;
1301 t->to_detach = inf_ttrace_detach;
1302 t->to_resume = inf_ttrace_resume;
1303 t->to_wait = inf_ttrace_wait;
1304 t->to_files_info = inf_ttrace_files_info;
1305 t->to_can_use_hw_breakpoint = inf_ttrace_can_use_hw_breakpoint;
1306 t->to_insert_watchpoint = inf_ttrace_insert_watchpoint;
1307 t->to_remove_watchpoint = inf_ttrace_remove_watchpoint;
1308 t->to_stopped_by_watchpoint = inf_ttrace_stopped_by_watchpoint;
1309 t->to_region_ok_for_hw_watchpoint =
1310 inf_ttrace_region_ok_for_hw_watchpoint;
1311 t->to_kill = inf_ttrace_kill;
1312 t->to_create_inferior = inf_ttrace_create_inferior;
1313 t->to_follow_fork = inf_ttrace_follow_fork;
1314 t->to_mourn_inferior = inf_ttrace_mourn_inferior;
1315 t->to_thread_alive = inf_ttrace_thread_alive;
1316 t->to_extra_thread_info = inf_ttrace_extra_thread_info;
1317 t->to_pid_to_str = inf_ttrace_pid_to_str;
1318 t->to_xfer_partial = inf_ttrace_xfer_partial;
1319 t->to_get_ada_task_ptid = inf_ttrace_get_ada_task_ptid;
1326 /* Prevent warning from -Wmissing-prototypes. */
1327 void _initialize_inf_ttrace (void);
1330 _initialize_inf_ttrace (void)
1333 inf_ttrace_page_dict.pagesize = getpagesize();