darwin-nat.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Common things used by the various darwin files
  2. Copyright (C) 1995-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef DARWIN_NAT_H
  14. #define DARWIN_NAT_H
  15. #include "inf-child.h"
  16. #include <mach/mach.h>
  17. #include "gdbthread.h"
  18. struct darwin_exception_msg
  19. {
  20. mach_msg_header_t header;
  21. /* Thread and task taking the exception. */
  22. mach_port_t thread_port;
  23. mach_port_t task_port;
  24. /* Type of the exception. */
  25. exception_type_t ex_type;
  26. /* Machine dependent details. */
  27. mach_msg_type_number_t data_count;
  28. integer_t ex_data[2];
  29. };
  30. enum darwin_msg_state
  31. {
  32. /* The thread is running. */
  33. DARWIN_RUNNING,
  34. /* The thread is stopped. */
  35. DARWIN_STOPPED,
  36. /* The thread has sent a message and waits for a reply. */
  37. DARWIN_MESSAGE
  38. };
  39. struct darwin_thread_info : public private_thread_info
  40. {
  41. /* The thread port from a GDB point of view. */
  42. thread_t gdb_port = 0;
  43. /* The thread port from the inferior point of view. Not to be used inside
  44. gdb except for get_ada_task_ptid. */
  45. thread_t inf_port = 0;
  46. /* Current message state.
  47. If the kernel has sent a message it expects a reply and the inferior
  48. can't be killed before. */
  49. enum darwin_msg_state msg_state = DARWIN_RUNNING;
  50. /* True if this thread is single-stepped. */
  51. bool single_step = false;
  52. /* True if a signal was manually sent to the thread. */
  53. bool signaled = false;
  54. /* The last exception received. */
  55. struct darwin_exception_msg event {};
  56. };
  57. typedef struct darwin_thread_info darwin_thread_t;
  58. /* This needs to be overridden by the platform specific nat code. */
  59. class darwin_nat_target : public inf_child_target
  60. {
  61. void create_inferior (const char *exec_file,
  62. const std::string &allargs,
  63. char **env, int from_tty) override;
  64. void attach (const char *, int) override;
  65. void detach (inferior *, int) override;
  66. ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
  67. void mourn_inferior () override;
  68. void kill () override;
  69. void interrupt () override;
  70. void resume (ptid_t, int , enum gdb_signal) override;
  71. bool thread_alive (ptid_t ptid) override;
  72. std::string pid_to_str (ptid_t) override;
  73. char *pid_to_exec_file (int pid) override;
  74. enum target_xfer_status xfer_partial (enum target_object object,
  75. const char *annex,
  76. gdb_byte *readbuf,
  77. const gdb_byte *writebuf,
  78. ULONGEST offset, ULONGEST len,
  79. ULONGEST *xfered_len) override;
  80. bool supports_multi_process () override;
  81. ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
  82. private:
  83. ptid_t wait_1 (ptid_t, struct target_waitstatus *);
  84. void check_new_threads (inferior *inf);
  85. int decode_exception_message (mach_msg_header_t *hdr,
  86. inferior **pinf,
  87. darwin_thread_t **pthread);
  88. ptid_t decode_message (mach_msg_header_t *hdr,
  89. darwin_thread_t **pthread,
  90. inferior **pinf,
  91. target_waitstatus *status);
  92. void stop_inferior (inferior *inf);
  93. void init_thread_list (inferior *inf);
  94. void ptrace_him (int pid);
  95. int cancel_breakpoint (ptid_t ptid);
  96. };
  97. /* Describe the mach exception handling state for a task. This state is saved
  98. before being changed and restored when a process is detached.
  99. For more information on these fields see task_get_exception_ports manual
  100. page. */
  101. struct darwin_exception_info
  102. {
  103. /* Exceptions handled by the port. */
  104. exception_mask_t masks[EXC_TYPES_COUNT] {};
  105. /* Ports receiving exception messages. */
  106. mach_port_t ports[EXC_TYPES_COUNT] {};
  107. /* Type of messages sent. */
  108. exception_behavior_t behaviors[EXC_TYPES_COUNT] {};
  109. /* Type of state to be sent. */
  110. thread_state_flavor_t flavors[EXC_TYPES_COUNT] {};
  111. /* Number of elements set. */
  112. mach_msg_type_number_t count = 0;
  113. };
  114. static inline darwin_thread_info *
  115. get_darwin_thread_info (class thread_info *thread)
  116. {
  117. return static_cast<darwin_thread_info *> (thread->priv.get ());
  118. }
  119. /* Describe an inferior. */
  120. struct darwin_inferior : public private_inferior
  121. {
  122. /* Corresponding task port. */
  123. task_t task = 0;
  124. /* Port which will receive the dead-name notification for the task port.
  125. This is used to detect the death of the task. */
  126. mach_port_t notify_port = 0;
  127. /* Initial exception handling. */
  128. darwin_exception_info exception_info;
  129. /* Number of messages that have been received but not yet replied. */
  130. unsigned int pending_messages = 0;
  131. /* Set if inferior is not controlled by ptrace(2) but through Mach. */
  132. bool no_ptrace = false;
  133. /* True if this task is suspended. */
  134. bool suspended = false;
  135. /* Sorted vector of known threads. */
  136. std::vector<darwin_thread_t *> threads;
  137. };
  138. /* Return the darwin_inferior attached to INF. */
  139. static inline darwin_inferior *
  140. get_darwin_inferior (inferior *inf)
  141. {
  142. return static_cast<darwin_inferior *> (inf->priv.get ());
  143. }
  144. /* Exception port. */
  145. extern mach_port_t darwin_ex_port;
  146. /* Port set. */
  147. extern mach_port_t darwin_port_set;
  148. /* A copy of mach_host_self (). */
  149. extern mach_port_t darwin_host_self;
  150. #define MACH_CHECK_ERROR(ret) \
  151. mach_check_error (ret, __FILE__, __LINE__, __func__)
  152. extern void mach_check_error (kern_return_t ret, const char *file,
  153. unsigned int line, const char *func);
  154. void darwin_set_sstep (thread_t thread, int enable);
  155. void darwin_check_osabi (darwin_inferior *inf, thread_t thread);
  156. #endif /* DARWIN_NAT_H */