[gdb/testsuite] Use unique portnum in parallel testing
[binutils-gdb.git] / gdb / darwin-nat.h
blob71aee57f61a52956baf335f777cd96e2201e10d9
1 /* Common things used by the various darwin files
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
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.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 #ifndef DARWIN_NAT_H
18 #define DARWIN_NAT_H
20 #include "inf-child.h"
21 #include <mach/mach.h>
22 #include "gdbthread.h"
24 struct darwin_exception_msg
26 mach_msg_header_t header;
28 /* Thread and task taking the exception. */
29 mach_port_t thread_port;
30 mach_port_t task_port;
32 /* Type of the exception. */
33 exception_type_t ex_type;
35 /* Machine dependent details. */
36 mach_msg_type_number_t data_count;
37 integer_t ex_data[2];
40 enum darwin_msg_state
42 /* The thread is running. */
43 DARWIN_RUNNING,
45 /* The thread is stopped. */
46 DARWIN_STOPPED,
48 /* The thread has sent a message and waits for a reply. */
49 DARWIN_MESSAGE
52 struct darwin_thread_info : public private_thread_info
54 /* The thread port from a GDB point of view. */
55 thread_t gdb_port = 0;
57 /* The thread port from the inferior point of view. Not to be used inside
58 gdb except for get_ada_task_ptid. */
59 thread_t inf_port = 0;
61 /* Current message state.
62 If the kernel has sent a message it expects a reply and the inferior
63 can't be killed before. */
64 enum darwin_msg_state msg_state = DARWIN_RUNNING;
66 /* True if this thread is single-stepped. */
67 bool single_step = false;
69 /* True if a signal was manually sent to the thread. */
70 bool signaled = false;
72 /* The last exception received. */
73 struct darwin_exception_msg event {};
75 typedef struct darwin_thread_info darwin_thread_t;
77 /* This needs to be overridden by the platform specific nat code. */
79 class darwin_nat_target : public inf_child_target
81 void create_inferior (const char *exec_file,
82 const std::string &allargs,
83 char **env, int from_tty) override;
85 void attach (const char *, int) override;
87 void detach (inferior *, int) override;
89 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
91 void mourn_inferior () override;
93 void kill () override;
95 void interrupt () override;
97 void resume (ptid_t, int , enum gdb_signal) override;
99 bool thread_alive (ptid_t ptid) override;
101 std::string pid_to_str (ptid_t) override;
103 const char *pid_to_exec_file (int pid) override;
105 enum target_xfer_status xfer_partial (enum target_object object,
106 const char *annex,
107 gdb_byte *readbuf,
108 const gdb_byte *writebuf,
109 ULONGEST offset, ULONGEST len,
110 ULONGEST *xfered_len) override;
112 bool supports_multi_process () override;
114 ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
116 private:
117 ptid_t wait_1 (ptid_t, struct target_waitstatus *);
118 void check_new_threads (inferior *inf);
119 int decode_exception_message (mach_msg_header_t *hdr,
120 inferior **pinf,
121 darwin_thread_t **pthread);
122 ptid_t decode_message (mach_msg_header_t *hdr,
123 darwin_thread_t **pthread,
124 inferior **pinf,
125 target_waitstatus *status);
126 void stop_inferior (inferior *inf);
127 void init_thread_list (inferior *inf);
128 void ptrace_him (int pid);
129 int cancel_breakpoint (inferior *inf, ptid_t ptid);
132 /* Describe the mach exception handling state for a task. This state is saved
133 before being changed and restored when a process is detached.
134 For more information on these fields see task_get_exception_ports manual
135 page. */
136 struct darwin_exception_info
138 /* Exceptions handled by the port. */
139 exception_mask_t masks[EXC_TYPES_COUNT] {};
141 /* Ports receiving exception messages. */
142 mach_port_t ports[EXC_TYPES_COUNT] {};
144 /* Type of messages sent. */
145 exception_behavior_t behaviors[EXC_TYPES_COUNT] {};
147 /* Type of state to be sent. */
148 thread_state_flavor_t flavors[EXC_TYPES_COUNT] {};
150 /* Number of elements set. */
151 mach_msg_type_number_t count = 0;
154 static inline darwin_thread_info *
155 get_darwin_thread_info (class thread_info *thread)
157 return gdb::checked_static_cast<darwin_thread_info *> (thread->priv.get ());
160 /* Describe an inferior. */
161 struct darwin_inferior : public private_inferior
163 /* Corresponding task port. */
164 task_t task = 0;
166 /* Port which will receive the dead-name notification for the task port.
167 This is used to detect the death of the task. */
168 mach_port_t notify_port = 0;
170 /* Initial exception handling. */
171 darwin_exception_info exception_info;
173 /* Number of messages that have been received but not yet replied. */
174 unsigned int pending_messages = 0;
176 /* Set if inferior is not controlled by ptrace(2) but through Mach. */
177 bool no_ptrace = false;
179 /* True if this task is suspended. */
180 bool suspended = false;
182 /* Sorted vector of known threads. */
183 std::vector<darwin_thread_t *> threads;
186 /* Return the darwin_inferior attached to INF. */
188 static inline darwin_inferior *
189 get_darwin_inferior (inferior *inf)
191 return gdb::checked_static_cast<darwin_inferior *> (inf->priv.get ());
194 /* Exception port. */
195 extern mach_port_t darwin_ex_port;
197 /* Port set. */
198 extern mach_port_t darwin_port_set;
200 /* A copy of mach_host_self (). */
201 extern mach_port_t darwin_host_self;
203 #define MACH_CHECK_ERROR(ret) \
204 mach_check_error (ret, __FILE__, __LINE__, __func__)
206 extern void mach_check_error (kern_return_t ret, const char *file,
207 unsigned int line, const char *func);
209 void darwin_set_sstep (thread_t thread, int enable);
211 void darwin_check_osabi (darwin_inferior *inf, thread_t thread);
213 #endif /* DARWIN_NAT_H */