1 //===-- MachException.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Created by Greg Clayton on 6/18/07.
12 //===----------------------------------------------------------------------===//
14 #include "MachException.h"
18 #include <sys/ptrace.h>
19 #include <sys/types.h>
25 #include "lldb/Target/UnixSignals.h"
26 #include "lldb/Utility/LLDBAssert.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/Status.h"
29 #include "lldb/Utility/Stream.h"
32 using namespace lldb_private;
33 using namespace lldb_private::process_darwin;
35 // Routine mach_exception_raise
36 extern "C" kern_return_t
37 catch_mach_exception_raise(mach_port_t exception_port, mach_port_t thread,
38 mach_port_t task, exception_type_t exception,
39 mach_exception_data_t code,
40 mach_msg_type_number_t codeCnt);
42 extern "C" kern_return_t catch_mach_exception_raise_state(
43 mach_port_t exception_port, exception_type_t exception,
44 const mach_exception_data_t code, mach_msg_type_number_t codeCnt,
45 int *flavor, const thread_state_t old_state,
46 mach_msg_type_number_t old_stateCnt, thread_state_t new_state,
47 mach_msg_type_number_t *new_stateCnt);
49 // Routine mach_exception_raise_state_identity
50 extern "C" kern_return_t catch_mach_exception_raise_state_identity(
51 mach_port_t exception_port, mach_port_t thread, mach_port_t task,
52 exception_type_t exception, mach_exception_data_t code,
53 mach_msg_type_number_t codeCnt, int *flavor, thread_state_t old_state,
54 mach_msg_type_number_t old_stateCnt, thread_state_t new_state,
55 mach_msg_type_number_t *new_stateCnt);
57 extern "C" boolean_t mach_exc_server(mach_msg_header_t *InHeadP,
58 mach_msg_header_t *OutHeadP);
60 static MachException::Data *g_message = NULL;
62 extern "C" kern_return_t catch_mach_exception_raise_state(
63 mach_port_t exc_port, exception_type_t exc_type,
64 const mach_exception_data_t exc_data, mach_msg_type_number_t exc_data_count,
65 int *flavor, const thread_state_t old_state,
66 mach_msg_type_number_t old_stateCnt, thread_state_t new_state,
67 mach_msg_type_number_t *new_stateCnt) {
68 // TODO change to LIBLLDB_LOG_EXCEPTION
69 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
71 log->Printf("::%s(exc_port = 0x%4.4x, exc_type = %d (%s), "
72 "exc_data = 0x%llx, exc_data_count = %d)",
73 __FUNCTION__, exc_port, exc_type, MachException::Name(exc_type),
74 (uint64_t)exc_data, exc_data_count);
79 extern "C" kern_return_t catch_mach_exception_raise_state_identity(
80 mach_port_t exc_port, mach_port_t thread_port, mach_port_t task_port,
81 exception_type_t exc_type, mach_exception_data_t exc_data,
82 mach_msg_type_number_t exc_data_count, int *flavor,
83 thread_state_t old_state, mach_msg_type_number_t old_stateCnt,
84 thread_state_t new_state, mach_msg_type_number_t *new_stateCnt) {
85 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
87 log->Printf("::%s(exc_port = 0x%4.4x, thd_port = 0x%4.4x, "
88 "tsk_port = 0x%4.4x, exc_type = %d (%s), exc_data[%d] = "
89 "{ 0x%llx, 0x%llx })",
90 __FUNCTION__, exc_port, thread_port, task_port, exc_type,
91 MachException::Name(exc_type), exc_data_count,
92 (uint64_t)(exc_data_count > 0 ? exc_data[0] : 0xBADDBADD),
93 (uint64_t)(exc_data_count > 1 ? exc_data[1] : 0xBADDBADD));
95 mach_port_deallocate(mach_task_self(), task_port);
96 mach_port_deallocate(mach_task_self(), thread_port);
101 extern "C" kern_return_t
102 catch_mach_exception_raise(mach_port_t exc_port, mach_port_t thread_port,
103 mach_port_t task_port, exception_type_t exc_type,
104 mach_exception_data_t exc_data,
105 mach_msg_type_number_t exc_data_count) {
106 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
108 log->Printf("::%s(exc_port = 0x%4.4x, thd_port = 0x%4.4x, "
109 "tsk_port = 0x%4.4x, exc_type = %d (%s), exc_data[%d] "
110 "= { 0x%llx, 0x%llx })",
111 __FUNCTION__, exc_port, thread_port, task_port, exc_type,
112 MachException::Name(exc_type), exc_data_count,
113 (uint64_t)(exc_data_count > 0 ? exc_data[0] : 0xBADDBADD),
114 (uint64_t)(exc_data_count > 1 ? exc_data[1] : 0xBADDBADD));
117 if (task_port == g_message->task_port) {
118 g_message->task_port = task_port;
119 g_message->thread_port = thread_port;
120 g_message->exc_type = exc_type;
121 g_message->exc_data.resize(exc_data_count);
122 ::memcpy(&g_message->exc_data[0], exc_data,
123 g_message->exc_data.size() * sizeof(mach_exception_data_type_t));
129 bool MachException::Data::GetStopInfo(struct ThreadStopInfo *stop_info,
130 const UnixSignals &signals,
131 Stream &stream) const {
135 // Zero out the structure.
136 memset(stop_info, 0, sizeof(struct ThreadStopInfo));
139 stop_info->reason = eStopReasonInvalid;
143 // We always stop with a mach exception.
144 stop_info->reason = eStopReasonException;
145 // Save the EXC_XXXX exception type.
146 stop_info->details.exception.type = exc_type;
148 // Fill in a text description
149 const char *exc_name = MachException::Name(exc_type);
151 stream.Printf("%s", exc_name);
153 stream.Printf("%i", exc_type);
155 stop_info->details.exception.data_count = exc_data.size();
157 int soft_signal = SoftSignal();
159 const char *sig_str = signals.GetSignalAsCString(soft_signal);
160 stream.Printf(" EXC_SOFT_SIGNAL( %i ( %s ))", soft_signal,
161 sig_str ? sig_str : "unknown signal");
163 // No special disassembly for exception data, just print it.
165 stream.Printf(" data[%llu] = {",
166 (uint64_t)stop_info->details.exception.data_count);
168 for (idx = 0; idx < stop_info->details.exception.data_count; ++idx) {
170 "0x%llx%c", (uint64_t)exc_data[idx],
171 ((idx + 1 == stop_info->details.exception.data_count) ? '}' : ','));
175 // Copy the exception data
176 for (size_t i = 0; i < stop_info->details.exception.data_count; i++)
177 stop_info->details.exception.data[i] = exc_data[i];
182 Status MachException::Message::Receive(mach_port_t port,
183 mach_msg_option_t options,
184 mach_msg_timeout_t timeout,
185 mach_port_t notify_port) {
187 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
189 mach_msg_timeout_t mach_msg_timeout =
190 options & MACH_RCV_TIMEOUT ? timeout : 0;
191 if (log && ((options & MACH_RCV_TIMEOUT) == 0)) {
192 // Dump this log message if we have no timeout in case it never returns
193 log->Printf("::mach_msg(msg->{bits = %#x, size = %u remote_port = %#x, "
194 "local_port = %#x, reserved = 0x%x, id = 0x%x}, "
195 "option = %#x, send_size = 0, rcv_size = %llu, "
196 "rcv_name = %#x, timeout = %u, notify = %#x)",
197 exc_msg.hdr.msgh_bits, exc_msg.hdr.msgh_size,
198 exc_msg.hdr.msgh_remote_port, exc_msg.hdr.msgh_local_port,
199 exc_msg.hdr.msgh_reserved, exc_msg.hdr.msgh_id, options,
200 (uint64_t)sizeof(exc_msg.data), port, mach_msg_timeout,
204 mach_msg_return_t mach_err =
205 ::mach_msg(&exc_msg.hdr,
208 sizeof(exc_msg.data), // Receive size
209 port, // exception port to watch for
211 mach_msg_timeout, // timeout in msec (obeyed only
212 // if MACH_RCV_TIMEOUT is ORed
213 // into the options parameter)
215 error.SetError(mach_err, eErrorTypeMachKernel);
217 // Dump any errors we get
218 if (error.Fail() && log) {
219 log->Printf("::mach_msg(msg->{bits = %#x, size = %u remote_port = %#x, "
220 "local_port = %#x, reserved = 0x%x, id = 0x%x}, "
221 "option = %#x, send_size = %u, rcv_size = %lu, rcv_name "
222 "= %#x, timeout = %u, notify = %#x) failed: %s",
223 exc_msg.hdr.msgh_bits, exc_msg.hdr.msgh_size,
224 exc_msg.hdr.msgh_remote_port, exc_msg.hdr.msgh_local_port,
225 exc_msg.hdr.msgh_reserved, exc_msg.hdr.msgh_id, options, 0,
226 sizeof(exc_msg.data), port, mach_msg_timeout, notify_port,
232 void MachException::Message::Dump(Stream &stream) const {
233 stream.Printf(" exc_msg { bits = 0x%8.8x size = 0x%8.8x remote-port = "
234 "0x%8.8x local-port = 0x%8.8x reserved = 0x%8.8x id = "
236 exc_msg.hdr.msgh_bits, exc_msg.hdr.msgh_size,
237 exc_msg.hdr.msgh_remote_port, exc_msg.hdr.msgh_local_port,
238 exc_msg.hdr.msgh_reserved, exc_msg.hdr.msgh_id);
240 stream.Printf(" reply_msg { bits = 0x%8.8x size = 0x%8.8x remote-port = "
241 "0x%8.8x local-port = 0x%8.8x reserved = 0x%8.8x id = "
243 reply_msg.hdr.msgh_bits, reply_msg.hdr.msgh_size,
244 reply_msg.hdr.msgh_remote_port, reply_msg.hdr.msgh_local_port,
245 reply_msg.hdr.msgh_reserved, reply_msg.hdr.msgh_id);
248 bool MachException::Message::CatchExceptionRaise(task_t task) {
249 bool success = false;
250 state.task_port = task;
252 // The exc_server function is the MIG generated server handling function
253 // to handle messages from the kernel relating to the occurrence of an
254 // exception in a thread. Such messages are delivered to the exception port
255 // set via thread_set_exception_ports or task_set_exception_ports. When an
256 // exception occurs in a thread, the thread sends an exception message to
257 // its exception port, blocking in the kernel waiting for the receipt of a
258 // reply. The exc_server function performs all necessary argument handling
259 // for this kernel message and calls catch_exception_raise,
260 // catch_exception_raise_state or catch_exception_raise_state_identity,
261 // which should handle the exception. If the called routine returns
262 // KERN_SUCCESS, a reply message will be sent, allowing the thread to
263 // continue from the point of the exception; otherwise, no reply message
264 // is sent and the called routine must have dealt with the exception
266 if (mach_exc_server(&exc_msg.hdr, &reply_msg.hdr)) {
270 GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
272 log->Printf("MachException::Message::%s(): mach_exc_server "
280 Status MachException::Message::Reply(::pid_t inferior_pid, task_t inferior_task,
282 // Reply to the exception...
285 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
287 // If we had a soft signal, we need to update the thread first so it can
288 // continue without signaling
289 int soft_signal = state.SoftSignal();
292 if (inferior_task == state.task_port) {
293 // This is our task, so we can update the signal to send to it
294 state_pid = inferior_pid;
295 soft_signal = signal;
297 auto mach_err = ::pid_for_task(state.task_port, &state_pid);
299 error.SetError(mach_err, eErrorTypeMachKernel);
301 log->Printf("MachException::Message::%s(): pid_for_task() "
303 __FUNCTION__, error.AsCString());
308 lldbassert(state_pid != -1);
309 if (state_pid != -1) {
311 caddr_t thread_port_caddr = (caddr_t)(uintptr_t)state.thread_port;
312 if (::ptrace(PT_THUPDATE, state_pid, thread_port_caddr, soft_signal) != 0)
313 error.SetError(errno, eErrorTypePOSIX);
315 if (!error.Success()) {
317 log->Printf("::ptrace(request = PT_THUPDATE, pid = "
318 "0x%4.4x, tid = 0x%4.4x, signal = %i)",
319 state_pid, state.thread_port, soft_signal);
326 log->Printf("::mach_msg ( msg->{bits = %#x, size = %u, remote_port "
327 "= %#x, local_port = %#x, reserved = 0x%x, id = 0x%x}, "
328 "option = %#x, send_size = %u, rcv_size = %u, rcv_name "
329 "= %#x, timeout = %u, notify = %#x)",
330 reply_msg.hdr.msgh_bits, reply_msg.hdr.msgh_size,
331 reply_msg.hdr.msgh_remote_port, reply_msg.hdr.msgh_local_port,
332 reply_msg.hdr.msgh_reserved, reply_msg.hdr.msgh_id,
333 MACH_SEND_MSG | MACH_SEND_INTERRUPT, reply_msg.hdr.msgh_size, 0,
334 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
337 ::mach_msg(&reply_msg.hdr, MACH_SEND_MSG | MACH_SEND_INTERRUPT,
338 reply_msg.hdr.msgh_size, 0, MACH_PORT_NULL,
339 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
341 error.SetError(mach_err, eErrorTypeMachKernel);
343 // Log our error if we have one.
344 if (error.Fail() && log) {
345 if (error.GetError() == MACH_SEND_INTERRUPTED) {
346 log->PutCString("::mach_msg() - send interrupted");
347 // TODO: keep retrying to reply???
348 } else if (state.task_port == inferior_task) {
349 log->Printf("mach_msg(): returned an error when replying "
350 "to a mach exception: error = %u (%s)",
351 error.GetError(), error.AsCString());
353 log->Printf("::mach_msg() - failed (child of task): %u (%s)",
354 error.GetError(), error.AsCString());
361 #define PREV_EXC_MASK_ALL \
362 (EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION | EXC_MASK_ARITHMETIC | \
363 EXC_MASK_EMULATION | EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT | \
364 EXC_MASK_SYSCALL | EXC_MASK_MACH_SYSCALL | EXC_MASK_RPC_ALERT | \
367 // Don't listen for EXC_RESOURCE, it should really get handled by the system
371 #define EXC_RESOURCE 11
374 #ifndef EXC_MASK_RESOURCE
375 #define EXC_MASK_RESOURCE (1 << EXC_RESOURCE)
378 #define LLDB_EXC_MASK (EXC_MASK_ALL & ~EXC_MASK_RESOURCE)
380 Status MachException::PortInfo::Save(task_t task) {
382 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
385 log->Printf("MachException::PortInfo::%s(task = 0x%4.4x)", __FUNCTION__,
388 // Be careful to be able to have debugserver built on a newer OS than what
389 // it is currently running on by being able to start with all exceptions
390 // and back off to just what is supported on the current system
391 mask = LLDB_EXC_MASK;
393 count = (sizeof(ports) / sizeof(ports[0]));
394 auto mach_err = ::task_get_exception_ports(task, mask, masks, &count, ports,
397 error.SetError(mach_err, eErrorTypeMachKernel);
400 if (error.Success()) {
401 log->Printf("::task_get_exception_ports(task = 0x%4.4x, mask = "
402 "0x%x, maskCnt => %u, ports, behaviors, flavors)",
405 log->Printf("::task_get_exception_ports(task = 0x%4.4x, mask = 0x%x, "
406 "maskCnt => %u, ports, behaviors, flavors) error: %u (%s)",
407 task, mask, count, error.GetError(), error.AsCString());
411 if ((error.GetError() == KERN_INVALID_ARGUMENT) &&
412 (mask != PREV_EXC_MASK_ALL)) {
413 mask = PREV_EXC_MASK_ALL;
414 count = (sizeof(ports) / sizeof(ports[0]));
415 mach_err = ::task_get_exception_ports(task, mask, masks, &count, ports,
417 error.SetError(mach_err, eErrorTypeMachKernel);
419 if (error.Success()) {
420 log->Printf("::task_get_exception_ports(task = 0x%4.4x, "
421 "mask = 0x%x, maskCnt => %u, ports, behaviors, "
425 log->Printf("::task_get_exception_ports(task = 0x%4.4x, mask = "
426 "0x%x, maskCnt => %u, ports, behaviors, flavors) "
428 task, mask, count, error.GetError(), error.AsCString());
439 Status MachException::PortInfo::Restore(task_t task) {
442 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
445 log->Printf("MachException::PortInfo::Restore(task = 0x%4.4x)", task);
449 for (i = 0; i < count; i++) {
450 auto mach_err = ::task_set_exception_ports(task, masks[i], ports[i],
451 behaviors[i], flavors[i]);
453 error.SetError(mach_err, eErrorTypeMachKernel);
455 if (error.Success()) {
456 log->Printf("::task_set_exception_ports(task = 0x%4.4x, "
457 "exception_mask = 0x%8.8x, new_port = 0x%4.4x, "
458 "behavior = 0x%8.8x, new_flavor = 0x%8.8x)",
459 task, masks[i], ports[i], behaviors[i], flavors[i]);
461 log->Printf("::task_set_exception_ports(task = 0x%4.4x, "
462 "exception_mask = 0x%8.8x, new_port = 0x%4.4x, "
463 "behavior = 0x%8.8x, new_flavor = 0x%8.8x): "
465 task, masks[i], ports[i], behaviors[i], flavors[i],
466 error.GetError(), error.AsCString());
470 // Bail if we encounter any errors
480 const char *MachException::Name(exception_type_t exc_type) {
483 return "EXC_BAD_ACCESS";
484 case EXC_BAD_INSTRUCTION:
485 return "EXC_BAD_INSTRUCTION";
487 return "EXC_ARITHMETIC";
489 return "EXC_EMULATION";
491 return "EXC_SOFTWARE";
493 return "EXC_BREAKPOINT";
495 return "EXC_SYSCALL";
496 case EXC_MACH_SYSCALL:
497 return "EXC_MACH_SYSCALL";
499 return "EXC_RPC_ALERT";