MachException.h revision 355940
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-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//  Created by Greg Clayton on 6/18/07.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef __MachException_h__
14#define __MachException_h__
15
16#include <mach/mach.h>
17#include <vector>
18
19#include "lldb/Host/Debug.h"
20#include "lldb/lldb-private-forward.h"
21#include "lldb/lldb-types.h"
22
23namespace lldb_private {
24namespace process_darwin {
25
26typedef union MachMessageTag {
27  mach_msg_header_t hdr;
28  char data[1024];
29} MachMessage;
30
31class MachException {
32public:
33  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;
41
42    Status Save(task_t task);
43
44    Status Restore(task_t task);
45  };
46
47  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() {}
55
56    void Clear() {
57      task_port = TASK_NULL;
58      thread_port = THREAD_NULL;
59      exc_type = 0;
60      exc_data.clear();
61    }
62
63    bool IsValid() const {
64      return task_port != TASK_NULL && thread_port != THREAD_NULL &&
65             exc_type != 0;
66    }
67
68    // 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    }
76
77    bool IsBreakpoint() const {
78      return (exc_type == EXC_BREAKPOINT ||
79              ((exc_type == EXC_SOFTWARE) && exc_data[0] == 1));
80    }
81
82    bool GetStopInfo(ThreadStopInfo *stop_info, const UnixSignals &signals,
83                     Stream &stream) const;
84  };
85
86  struct Message {
87    MachMessage exc_msg;
88    MachMessage reply_msg;
89    Data state;
90
91    Message() : state() {
92      memset(&exc_msg, 0, sizeof(exc_msg));
93      memset(&reply_msg, 0, sizeof(reply_msg));
94    }
95
96    bool CatchExceptionRaise(task_t task);
97
98    Status Reply(::pid_t inferior_pid, task_t inferior_task, int signal);
99
100    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);
103
104    void Dump(Stream &stream) const;
105
106    typedef std::vector<Message> collection;
107    typedef collection::iterator iterator;
108    typedef collection::const_iterator const_iterator;
109  };
110
111  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  };
132
133  static const char *Name(exception_type_t exc_type);
134};
135
136} // namespace process_darwin
137} // namespace lldb_private
138
139#endif
140