1 //===-- MachException.h -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  Created by Greg Clayton on 6/18/07.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 
15 #ifndef __MachException_h__
16 #define __MachException_h__
17 
18 #include <mach/mach.h>
19 #include <vector>
20 
21 class MachProcess;
22 class PThreadMutex;
23 
24 typedef union MachMessageTag
25 {
26     mach_msg_header_t hdr;
27     char data[1024];
28 } MachMessage;
29 
30 
31 class MachException
32 {
33 public:
34 
35     struct PortInfo
36     {
37         exception_mask_t        mask; // the exception mask for this device which may be a subset of EXC_MASK_ALL...
38         exception_mask_t        masks[EXC_TYPES_COUNT];
39         mach_port_t             ports[EXC_TYPES_COUNT];
40         exception_behavior_t    behaviors[EXC_TYPES_COUNT];
41         thread_state_flavor_t   flavors[EXC_TYPES_COUNT];
42         mach_msg_type_number_t  count;
43 
44         kern_return_t   Save(task_t task);
45         kern_return_t   Restore(task_t task);
46     };
47 
48     struct Data
49     {
50         task_t task_port;
51         thread_t thread_port;
52         exception_type_t exc_type;
53         std::vector<mach_exception_data_type_t> exc_data;
54         Data() :
55             task_port(TASK_NULL),
56             thread_port(THREAD_NULL),
57             exc_type(0),
58             exc_data()
59             {
60             }
61 
62         void Clear()
63         {
64             task_port = TASK_NULL;
65             thread_port = THREAD_NULL;
66             exc_type = 0;
67             exc_data.clear();
68         }
69         bool IsValid() const
70         {
71             return  task_port != TASK_NULL &&
72                     thread_port != THREAD_NULL &&
73                     exc_type != 0;
74         }
75         // Return the SoftSignal for this MachException data, or zero if there is none
76         int SoftSignal() const
77         {
78             if (exc_type == EXC_SOFTWARE && exc_data.size() == 2 && exc_data[0] == EXC_SOFT_SIGNAL)
79                 return static_cast<int>(exc_data[1]);
80             return 0;
81         }
82         bool IsBreakpoint() const
83         {
84             return (exc_type == EXC_BREAKPOINT || ((exc_type == EXC_SOFTWARE) && exc_data[0] == 1));
85         }
86         void Dump() const;
87         void DumpStopReason() const;
88         bool GetStopInfo(struct DNBThreadStopInfo *stop_info) const;
89     };
90 
91     struct Message
92     {
93         MachMessage exc_msg;
94         MachMessage reply_msg;
95         Data state;
96 
97         Message() :
98             state()
99         {
100             memset(&exc_msg,   0, sizeof(exc_msg));
101             memset(&reply_msg, 0, sizeof(reply_msg));
102         }
103         bool CatchExceptionRaise(task_t task);
104         void Dump() const;
105         kern_return_t Reply (MachProcess *process, int signal);
106         kern_return_t Receive( mach_port_t receive_port,
107                                mach_msg_option_t options,
108                                mach_msg_timeout_t timeout,
109                                mach_port_t notify_port = MACH_PORT_NULL);
110 
111         typedef std::vector<Message>        collection;
112         typedef collection::iterator        iterator;
113         typedef collection::const_iterator    const_iterator;
114     };
115 
116     enum
117     {
118         e_actionForward,    // Forward signal to inferior process
119         e_actionStop,        // Stop when this signal is received
120     };
121     struct Action
122     {
123         task_t task_port;            // Set to TASK_NULL for any TASK
124         thread_t thread_port;        // Set to THREAD_NULL for any thread
125         exception_type_t exc_mask;    // Mach exception mask to watch for
126         std::vector<mach_exception_data_type_t> exc_data_mask;    // Mask to apply to exception data, or empty to ignore exc_data value for exception
127         std::vector<mach_exception_data_type_t> exc_data_value;    // Value to compare to exception data after masking, or empty to ignore exc_data value for exception
128         uint8_t flags;                // Action flags describing what to do with the exception
129     };
130     static const char *Name(exception_type_t exc_type);
131 };
132 
133 #endif
134