blob: 18e49173b020086472c6b707dd888da59382b56e [file] [log] [blame]
Todd Fialae77fce02016-09-04 00:18:56 +00001//===-- MachException.h -----------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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-exception
Todd Fialae77fce02016-09-04 00:18:56 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Created by Greg Clayton on 6/18/07.
10//
11//===----------------------------------------------------------------------===//
12
Todd Fialae77fce02016-09-04 00:18:56 +000013#ifndef __MachException_h__
14#define __MachException_h__
15
16#include <mach/mach.h>
17#include <vector>
18
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include "lldb/Host/Debug.h"
Todd Fialae77fce02016-09-04 00:18:56 +000020#include "lldb/lldb-private-forward.h"
21#include "lldb/lldb-types.h"
Todd Fialae77fce02016-09-04 00:18:56 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023namespace lldb_private {
24namespace process_darwin {
Todd Fialae77fce02016-09-04 00:18:56 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026typedef union MachMessageTag {
27 mach_msg_header_t hdr;
28 char data[1024];
Todd Fialae77fce02016-09-04 00:18:56 +000029} MachMessage;
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031class MachException {
Todd Fialae77fce02016-09-04 00:18:56 +000032public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 struct PortInfo {
34 exception_mask_t mask; // the exception mask for this device which may be a
35 // subset of EXC_MASK_ALL...
36 exception_mask_t masks[EXC_TYPES_COUNT];
37 mach_port_t ports[EXC_TYPES_COUNT];
38 exception_behavior_t behaviors[EXC_TYPES_COUNT];
39 thread_state_flavor_t flavors[EXC_TYPES_COUNT];
40 mach_msg_type_number_t count;
Todd Fialae77fce02016-09-04 00:18:56 +000041
Zachary Turner97206d52017-05-12 04:51:55 +000042 Status Save(task_t task);
Todd Fialae77fce02016-09-04 00:18:56 +000043
Zachary Turner97206d52017-05-12 04:51:55 +000044 Status Restore(task_t task);
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 };
Todd Fialae77fce02016-09-04 00:18:56 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 struct Data {
48 task_t task_port;
49 thread_t thread_port;
50 exception_type_t exc_type;
51 std::vector<mach_exception_data_type_t> exc_data;
52 Data()
53 : task_port(TASK_NULL), thread_port(THREAD_NULL), exc_type(0),
54 exc_data() {}
Todd Fialae77fce02016-09-04 00:18:56 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 void Clear() {
57 task_port = TASK_NULL;
58 thread_port = THREAD_NULL;
59 exc_type = 0;
60 exc_data.clear();
61 }
Todd Fialae77fce02016-09-04 00:18:56 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 bool IsValid() const {
64 return task_port != TASK_NULL && thread_port != THREAD_NULL &&
65 exc_type != 0;
66 }
Todd Fialae77fce02016-09-04 00:18:56 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 // Return the SoftSignal for this MachException data, or zero if there is
69 // none
70 int SoftSignal() const {
71 if (exc_type == EXC_SOFTWARE && exc_data.size() == 2 &&
72 exc_data[0] == EXC_SOFT_SIGNAL)
73 return static_cast<int>(exc_data[1]);
74 return 0;
75 }
Todd Fialae77fce02016-09-04 00:18:56 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 bool IsBreakpoint() const {
78 return (exc_type == EXC_BREAKPOINT ||
79 ((exc_type == EXC_SOFTWARE) && exc_data[0] == 1));
80 }
Todd Fialae77fce02016-09-04 00:18:56 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 bool GetStopInfo(ThreadStopInfo *stop_info, const UnixSignals &signals,
83 Stream &stream) const;
84 };
Todd Fialae77fce02016-09-04 00:18:56 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 struct Message {
87 MachMessage exc_msg;
88 MachMessage reply_msg;
89 Data state;
Todd Fialae77fce02016-09-04 00:18:56 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 Message() : state() {
92 memset(&exc_msg, 0, sizeof(exc_msg));
93 memset(&reply_msg, 0, sizeof(reply_msg));
94 }
Todd Fialae77fce02016-09-04 00:18:56 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 bool CatchExceptionRaise(task_t task);
Todd Fialae77fce02016-09-04 00:18:56 +000097
Zachary Turner97206d52017-05-12 04:51:55 +000098 Status Reply(::pid_t inferior_pid, task_t inferior_task, int signal);
Todd Fialae77fce02016-09-04 00:18:56 +000099
Zachary Turner97206d52017-05-12 04:51:55 +0000100 Status Receive(mach_port_t receive_port, mach_msg_option_t options,
101 mach_msg_timeout_t timeout,
102 mach_port_t notify_port = MACH_PORT_NULL);
Todd Fialae77fce02016-09-04 00:18:56 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 void Dump(Stream &stream) const;
Todd Fialae77fce02016-09-04 00:18:56 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 typedef std::vector<Message> collection;
107 typedef collection::iterator iterator;
108 typedef collection::const_iterator const_iterator;
109 };
Todd Fialae77fce02016-09-04 00:18:56 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 enum {
112 e_actionForward, // Forward signal to inferior process
113 e_actionStop, // Stop when this signal is received
114 };
115 struct Action {
116 task_t task_port; // Set to TASK_NULL for any TASK
117 thread_t thread_port; // Set to THREAD_NULL for any thread
118 exception_type_t exc_mask; // Mach exception mask to watch for
119 std::vector<mach_exception_data_type_t> exc_data_mask; // Mask to apply to
120 // exception data, or
121 // empty to ignore
122 // exc_data value for
123 // exception
124 std::vector<mach_exception_data_type_t> exc_data_value; // Value to compare
125 // to exception data
126 // after masking, or
127 // empty to ignore
128 // exc_data value
129 // for exception
130 uint8_t flags; // Action flags describing what to do with the exception
131 };
Todd Fialae77fce02016-09-04 00:18:56 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 static const char *Name(exception_type_t exc_type);
Todd Fialae77fce02016-09-04 00:18:56 +0000134};
135
136} // namespace process_darwin
137} // namespace lldb_private
138
139#endif