obsd-nat.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Native-dependent code for OpenBSD.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbthread.h"
  16. #include "inferior.h"
  17. #include "target.h"
  18. #include <sys/types.h>
  19. #include <sys/ptrace.h>
  20. #include "gdbsupport/gdb_wait.h"
  21. #include "inf-ptrace.h"
  22. #include "obsd-nat.h"
  23. /* OpenBSD 5.2 and later include rthreads which uses a thread model
  24. that maps userland threads directly onto kernel threads in a 1:1
  25. fashion. */
  26. std::string
  27. obsd_nat_target::pid_to_str (ptid_t ptid)
  28. {
  29. if (ptid.lwp () != 0)
  30. return string_printf ("thread %ld of process %d", ptid.lwp (), ptid.pid ());
  31. return normal_pid_to_str (ptid);
  32. }
  33. void
  34. obsd_nat_target::update_thread_list ()
  35. {
  36. pid_t pid = inferior_ptid.pid ();
  37. struct ptrace_thread_state pts;
  38. prune_threads ();
  39. if (ptrace (PT_GET_THREAD_FIRST, pid, (caddr_t)&pts, sizeof pts) == -1)
  40. perror_with_name (("ptrace"));
  41. while (pts.pts_tid != -1)
  42. {
  43. ptid_t ptid = ptid_t (pid, pts.pts_tid, 0);
  44. if (!in_thread_list (this, ptid))
  45. {
  46. if (inferior_ptid.lwp () == 0)
  47. thread_change_ptid (this, inferior_ptid, ptid);
  48. else
  49. add_thread (this, ptid);
  50. }
  51. if (ptrace (PT_GET_THREAD_NEXT, pid, (caddr_t)&pts, sizeof pts) == -1)
  52. perror_with_name (("ptrace"));
  53. }
  54. }
  55. /* Enable additional event reporting on a new or existing process. */
  56. static void
  57. obsd_enable_proc_events (pid_t pid)
  58. {
  59. ptrace_event_t pe;
  60. /* Set the initial event mask. */
  61. memset (&pe, 0, sizeof pe);
  62. pe.pe_set_event |= PTRACE_FORK;
  63. if (ptrace (PT_SET_EVENT_MASK, pid,
  64. (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
  65. perror_with_name (("ptrace"));
  66. }
  67. ptid_t
  68. obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
  69. target_wait_flags options)
  70. {
  71. ptid_t wptid = inf_ptrace_target::wait (ptid, ourstatus, options);
  72. if (ourstatus->kind () == TARGET_WAITKIND_STOPPED)
  73. {
  74. ptrace_state_t pe;
  75. pid_t pid = wptid.pid ();
  76. if (ptrace (PT_GET_PROCESS_STATE, pid, (caddr_t)&pe, sizeof pe) == -1)
  77. perror_with_name (("ptrace"));
  78. wptid = ptid_t (pid, pe.pe_tid, 0);
  79. switch (pe.pe_report_event)
  80. {
  81. case PTRACE_FORK:
  82. ourstatus->set_forked (ptid_t (pe.pe_other_pid));
  83. /* Make sure the other end of the fork is stopped too. */
  84. pid_t fpid = waitpid (pe.pe_other_pid, nullptr, 0);
  85. if (fpid == -1)
  86. perror_with_name (("waitpid"));
  87. if (ptrace (PT_GET_PROCESS_STATE, fpid,
  88. (caddr_t)&pe, sizeof pe) == -1)
  89. perror_with_name (("ptrace"));
  90. gdb_assert (pe.pe_report_event == PTRACE_FORK);
  91. gdb_assert (pe.pe_other_pid == pid);
  92. if (find_inferior_pid (this, fpid) != nullptr)
  93. {
  94. ourstatus->set_forked (ptid_t (pe.pe_other_pid));
  95. wptid = ptid_t (fpid, pe.pe_tid, 0);
  96. }
  97. obsd_enable_proc_events (ourstatus->child_ptid ().pid ());
  98. break;
  99. }
  100. /* Ensure the ptid is updated with an LWP id on the first stop
  101. of a process. */
  102. if (!in_thread_list (this, wptid))
  103. {
  104. if (in_thread_list (this, ptid_t (pid)))
  105. thread_change_ptid (this, ptid_t (pid), wptid);
  106. else
  107. add_thread (this, wptid);
  108. }
  109. }
  110. return wptid;
  111. }
  112. void
  113. obsd_nat_target::post_attach (int pid)
  114. {
  115. obsd_enable_proc_events (pid);
  116. }
  117. /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
  118. void
  119. obsd_nat_target::post_startup_inferior (ptid_t pid)
  120. {
  121. obsd_enable_proc_events (pid.pid ());
  122. }
  123. /* Target hook for follow_fork. */
  124. void
  125. obsd_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
  126. target_waitkind fork_kind,
  127. bool follow_child, bool detach_fork)
  128. {
  129. inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
  130. follow_child, detach_fork);
  131. if (!follow_child && detach_fork)
  132. {
  133. /* Breakpoints have already been detached from the child by
  134. infrun.c. */
  135. if (ptrace (PT_DETACH, child_ptid.pid (), (PTRACE_TYPE_ARG3)1, 0) == -1)
  136. perror_with_name (("ptrace"));
  137. }
  138. }
  139. int
  140. obsd_nat_target::insert_fork_catchpoint (int pid)
  141. {
  142. return 0;
  143. }
  144. int
  145. obsd_nat_target::remove_fork_catchpoint (int pid)
  146. {
  147. return 0;
  148. }