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 #include "DNBConfig.h"
21 
22 class MachProcess;
23 class PThreadMutex;
24 
25 typedef union MachMessageTag
26 {
27     mach_msg_header_t hdr;
28     char data[1024];
29 } MachMessage;
30 
31 
32 class MachException
33 {
34 public:
35 
36     struct PortInfo
37     {
38         exception_mask_t        mask; // the exception mask for this device which may be a subset of EXC_MASK_ALL...
39         exception_mask_t        masks[EXC_TYPES_COUNT];
40         mach_port_t             ports[EXC_TYPES_COUNT];
41         exception_behavior_t    behaviors[EXC_TYPES_COUNT];
42         thread_state_flavor_t   flavors[EXC_TYPES_COUNT];
43         mach_msg_type_number_t  count;
44 
45         kern_return_t   Save(task_t task);
46         kern_return_t   Restore(task_t task);
47     };
48 
49     struct Data
50     {
51         task_t task_port;
52         thread_t thread_port;
53         exception_type_t exc_type;
54         std::vector<mach_exception_data_type_t> exc_data;
DataData55         Data() :
56             task_port(TASK_NULL),
57             thread_port(THREAD_NULL),
58             exc_type(0),
59             exc_data()
60             {
61             }
62 
ClearData63         void Clear()
64         {
65             task_port = TASK_NULL;
66             thread_port = THREAD_NULL;
67             exc_type = 0;
68             exc_data.clear();
69         }
IsValidData70         bool IsValid() const
71         {
72             return  task_port != TASK_NULL &&
73                     thread_port != THREAD_NULL &&
74                     exc_type != 0;
75         }
76         // Return the SoftSignal for this MachException data, or zero if there is none
SoftSignalData77         int SoftSignal() const
78         {
79             if (exc_type == EXC_SOFTWARE && exc_data.size() == 2 && exc_data[0] == EXC_SOFT_SIGNAL)
80                 return exc_data[1];
81             return 0;
82         }
IsBreakpointData83         bool IsBreakpoint() const
84         {
85             return (exc_type == EXC_BREAKPOINT) || ((exc_type == EXC_SOFTWARE) && exc_data[0] == 1);
86         }
87         void Dump() const;
88         void DumpStopReason() const;
89         bool GetStopInfo(struct DNBThreadStopInfo *stop_info) const;
90     };
91 
92     struct Message
93     {
94         MachMessage exc_msg;
95         MachMessage reply_msg;
96         Data state;
97 
MessageMessage98         Message() :
99             state()
100         {
101             memset(&exc_msg,   0, sizeof(exc_msg));
102             memset(&reply_msg, 0, sizeof(reply_msg));
103         }
104         bool CatchExceptionRaise(task_t task);
105         void Dump() const;
106         kern_return_t Reply (MachProcess *process, int signal);
107         kern_return_t Receive( mach_port_t receive_port,
108                                mach_msg_option_t options,
109                                mach_msg_timeout_t timeout,
110                                mach_port_t notify_port = MACH_PORT_NULL);
111 
112         typedef std::vector<Message>        collection;
113         typedef collection::iterator        iterator;
114         typedef collection::const_iterator    const_iterator;
115     };
116 
117     enum
118     {
119         e_actionForward,    // Forward signal to inferior process
120         e_actionStop,        // Stop when this signal is received
121     };
122     struct Action
123     {
124         task_t task_port;            // Set to TASK_NULL for any TASK
125         thread_t thread_port;        // Set to THREAD_NULL for any thread
126         exception_type_t exc_mask;    // Mach exception mask to watch for
127         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
128         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
129         uint8_t flags;                // Action flags describing what to do with the exception
130     };
131     static const char *Name(exception_type_t exc_type);
132 };
133 
134 #endif
135