124 lines · c
1//===-- MachException.h -----------------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Created by Greg Clayton on 6/18/07.10//11//===----------------------------------------------------------------------===//12 13#ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHEXCEPTION_H14#define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHEXCEPTION_H15 16#include <mach/mach.h>17#include <vector>18 19class MachProcess;20 21typedef union MachMessageTag {22 mach_msg_header_t hdr;23 char data[1024];24} MachMessage;25 26class MachException {27public:28 struct PortInfo {29 exception_mask_t mask; // the exception mask for this device which may be a30 // subset of EXC_MASK_ALL...31 exception_mask_t masks[EXC_TYPES_COUNT];32 mach_port_t ports[EXC_TYPES_COUNT];33 exception_behavior_t behaviors[EXC_TYPES_COUNT];34 thread_state_flavor_t flavors[EXC_TYPES_COUNT];35 mach_msg_type_number_t count;36 37 kern_return_t Save(task_t task);38 kern_return_t Restore(task_t task);39 };40 41 struct Data {42 task_t task_port;43 thread_t thread_port;44 exception_type_t exc_type;45 std::vector<mach_exception_data_type_t> exc_data;46 Data()47 : task_port(TASK_NULL), thread_port(THREAD_NULL), exc_type(0),48 exc_data() {}49 50 void Clear() {51 task_port = TASK_NULL;52 thread_port = THREAD_NULL;53 exc_type = 0;54 exc_data.clear();55 }56 bool IsValid() const {57 return task_port != TASK_NULL && thread_port != THREAD_NULL &&58 exc_type != 0;59 }60 // Return the SoftSignal for this MachException data, or zero if there is61 // none62 int SoftSignal() const {63 if (exc_type == EXC_SOFTWARE && exc_data.size() == 2 &&64 exc_data[0] == EXC_SOFT_SIGNAL)65 return static_cast<int>(exc_data[1]);66 return 0;67 }68 bool IsBreakpoint() const {69 return (exc_type == EXC_BREAKPOINT ||70 ((exc_type == EXC_SOFTWARE) && exc_data[0] == 1));71 }72 void Dump() const;73 void DumpStopReason() const;74 bool GetStopInfo(struct DNBThreadStopInfo *stop_info) const;75 };76 77 struct Message {78 MachMessage exc_msg;79 MachMessage reply_msg;80 Data state;81 82 Message() : state() {83 memset(&exc_msg, 0, sizeof(exc_msg));84 memset(&reply_msg, 0, sizeof(reply_msg));85 }86 bool CatchExceptionRaise(task_t task);87 void Dump() const;88 kern_return_t Reply(MachProcess *process, int signal);89 kern_return_t Receive(mach_port_t receive_port, mach_msg_option_t options,90 mach_msg_timeout_t timeout,91 mach_port_t notify_port = MACH_PORT_NULL);92 93 typedef std::vector<Message> collection;94 typedef collection::iterator iterator;95 typedef collection::const_iterator const_iterator;96 };97 98 enum {99 e_actionForward, // Forward signal to inferior process100 e_actionStop, // Stop when this signal is received101 };102 struct Action {103 task_t task_port; // Set to TASK_NULL for any TASK104 thread_t thread_port; // Set to THREAD_NULL for any thread105 exception_type_t exc_mask; // Mach exception mask to watch for106 std::vector<mach_exception_data_type_t> exc_data_mask; // Mask to apply to107 // exception data, or108 // empty to ignore109 // exc_data value for110 // exception111 std::vector<mach_exception_data_type_t> exc_data_value; // Value to compare112 // to exception data113 // after masking, or114 // empty to ignore115 // exc_data value116 // for exception117 uint8_t flags; // Action flags describing what to do with the exception118 };119 static const char *Name(exception_type_t exc_type);120 static exception_mask_t ExceptionMask(const char *name);121};122 123#endif124