MachException.h revision 0e8147bd867e4cdaae9400f56d02c7aacd40a9b3
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
22class MachProcess;
23class PThreadMutex;
24
25typedef union MachMessageTag
26{
27    mach_msg_header_t hdr;
28    char data[1024];
29} MachMessage;
30
31
32class MachException
33{
34public:
35
36    struct PortInfo
37    {
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 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();
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