1 //===-- DNBDefs.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/26/07.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef __DNBDefs_h__
15 #define __DNBDefs_h__
16 
17 #include <stdint.h>
18 #include <signal.h>
19 #include <stdio.h>
20 #include <sys/syslimits.h>
21 #include <unistd.h>
22 
23 #include <ciso646>  // detect C++ lib
24 
25 #ifdef _LIBCPP_VERSION
26 #include <memory>
27 #define STD_SHARED_PTR(T) std::shared_ptr<T>
28 #define STD_WEAK_PTR(T) std::weak_ptr<T>
29 #define STD_ENABLE_SHARED_FROM_THIS(T) std::enable_shared_from_this<T>
30 #else
31 #include <tr1/memory>
32 #define STD_SHARED_PTR(T) std::tr1::shared_ptr<T>
33 #define STD_WEAK_PTR(T) std::tr1::weak_ptr<T>
34 #define STD_ENABLE_SHARED_FROM_THIS(T) std::tr1::enable_shared_from_this<T>
35 #endif
36 
37 //----------------------------------------------------------------------
38 // Define nub_addr_t and the invalid address value from the architecture
39 //----------------------------------------------------------------------
40 #if defined (__x86_64__) || defined (__ppc64__)
41 
42 //----------------------------------------------------------------------
43 // 64 bit address architectures
44 //----------------------------------------------------------------------
45 typedef uint64_t        nub_addr_t;
46 #define INVALID_NUB_ADDRESS     ((nub_addr_t)~0ull)
47 
48 #elif defined (__i386__) || defined (__powerpc__) || defined (__ppc__) || defined (__arm__)
49 
50 //----------------------------------------------------------------------
51 // 32 bit address architectures
52 //----------------------------------------------------------------------
53 
54 typedef uint32_t        nub_addr_t;
55 #define INVALID_NUB_ADDRESS     ((nub_addr_t)~0ul)
56 
57 #else
58 
59 //----------------------------------------------------------------------
60 // Default to 64 bit address for unrecognized architectures.
61 //----------------------------------------------------------------------
62 
63 #warning undefined architecture, defaulting to 8 byte addresses
64 typedef uint64_t        nub_addr_t;
65 #define INVALID_NUB_ADDRESS     ((nub_addr_t)~0ull)
66 
67 
68 #endif
69 
70 typedef size_t          nub_size_t;
71 typedef ssize_t         nub_ssize_t;
72 typedef uint32_t        nub_break_t;
73 typedef uint32_t        nub_watch_t;
74 typedef uint32_t        nub_index_t;
75 typedef pid_t           nub_process_t;
76 typedef uint64_t        nub_thread_t;
77 typedef uint32_t        nub_event_t;
78 typedef uint32_t        nub_bool_t;
79 
80 #define INVALID_NUB_BREAK_ID    ((nub_break_t)0)
81 #define INVALID_NUB_PROCESS     ((nub_process_t)0)
82 #define INVALID_NUB_THREAD      ((nub_thread_t)0)
83 #define INVALID_NUB_WATCH_ID    ((nub_watch_t)0)
84 #define INVALID_NUB_HW_INDEX    UINT32_MAX
85 #define INVALID_NUB_REGNUM      UINT32_MAX
86 #define NUB_GENERIC_ERROR       UINT32_MAX
87 
88 #define NUB_BREAK_ID_IS_VALID(breakID)    ((breakID) != (INVALID_NUB_BREAK_ID))
89 #define NUB_WATCH_ID_IS_VALID(watchID)    ((watchID) != (INVALID_NUB_WATCH_ID))
90 
91 // Watchpoint types
92 #define WATCH_TYPE_READ     (1u << 0)
93 #define WATCH_TYPE_WRITE    (1u << 1)
94 
95 typedef enum
96 {
97     eStateInvalid = 0,
98     eStateUnloaded,
99     eStateAttaching,
100     eStateLaunching,
101     eStateStopped,
102     eStateRunning,
103     eStateStepping,
104     eStateCrashed,
105     eStateDetached,
106     eStateExited,
107     eStateSuspended
108 } nub_state_t;
109 
110 typedef enum
111 {
112     eLaunchFlavorDefault = 0,
113     eLaunchFlavorPosixSpawn,
114     eLaunchFlavorForkExec,
115 #ifdef WITH_SPRINGBOARD
116     eLaunchFlavorSpringBoard,
117 #endif
118 } nub_launch_flavor_t;
119 
120 #define NUB_STATE_IS_RUNNING(s) ((s) == eStateAttaching ||\
121                                  (s) == eStateLaunching ||\
122                                  (s) == eStateRunning ||\
123                                  (s) == eStateStepping ||\
124                                  (s) == eStateDetached)
125 
126 #define NUB_STATE_IS_STOPPED(s) ((s) == eStateUnloaded ||\
127                                  (s) == eStateStopped ||\
128                                  (s) == eStateCrashed ||\
129                                  (s) == eStateExited)
130 
131 enum
132 {
133     eEventProcessRunningStateChanged = 1 << 0,  // The process has changed state to running
134     eEventProcessStoppedStateChanged = 1 << 1,  // The process has changed state to stopped
135     eEventSharedLibsStateChange = 1 << 2,       // Shared libraries loaded/unloaded state has changed
136     eEventStdioAvailable = 1 << 3,              // Something is available on stdout/stderr
137     eEventProfileDataAvailable = 1 << 4,        // Profile data ready for retrieval
138     eEventProcessAsyncInterrupt = 1 << 5,       // Gives the ability for any infinite wait calls to be interrupted
139     kAllEventsMask = eEventProcessRunningStateChanged |
140                      eEventProcessStoppedStateChanged |
141                      eEventSharedLibsStateChange |
142                      eEventStdioAvailable |
143                      eEventProfileDataAvailable |
144                      eEventProcessAsyncInterrupt
145 };
146 
147 #define LOG_VERBOSE             (1u << 0)
148 #define LOG_PROCESS             (1u << 1)
149 #define LOG_THREAD              (1u << 2)
150 #define LOG_EXCEPTIONS          (1u << 3)
151 #define LOG_SHLIB               (1u << 4)
152 #define LOG_MEMORY              (1u << 5)    // Log memory reads/writes calls
153 #define LOG_MEMORY_DATA_SHORT   (1u << 6)    // Log short memory reads/writes bytes
154 #define LOG_MEMORY_DATA_LONG    (1u << 7)    // Log all memory reads/writes bytes
155 #define LOG_MEMORY_PROTECTIONS  (1u << 8)    // Log memory protection changes
156 #define LOG_BREAKPOINTS         (1u << 9)
157 #define LOG_EVENTS              (1u << 10)
158 #define LOG_WATCHPOINTS         (1u << 11)
159 #define LOG_STEP                (1u << 12)
160 #define LOG_TASK                (1u << 13)
161 #define LOG_LO_USER             (1u << 16)
162 #define LOG_HI_USER             (1u << 31)
163 #define LOG_ALL                 0xFFFFFFFFu
164 #define LOG_DEFAULT             ((LOG_PROCESS) |\
165                                  (LOG_TASK) |\
166                                  (LOG_THREAD) |\
167                                  (LOG_EXCEPTIONS) |\
168                                  (LOG_SHLIB) |\
169                                  (LOG_MEMORY) |\
170                                  (LOG_BREAKPOINTS) |\
171                                  (LOG_WATCHPOINTS) |\
172                                  (LOG_STEP))
173 
174 
175 #define REGISTER_SET_ALL        0
176 // Generic Register set to be defined by each architecture for access to common
177 // register values.
178 #define REGISTER_SET_GENERIC    ((uint32_t)0xFFFFFFFFu)
179 #define GENERIC_REGNUM_PC       0   // Program Counter
180 #define GENERIC_REGNUM_SP       1   // Stack Pointer
181 #define GENERIC_REGNUM_FP       2   // Frame Pointer
182 #define GENERIC_REGNUM_RA       3   // Return Address
183 #define GENERIC_REGNUM_FLAGS    4   // Processor flags register
184 #define GENERIC_REGNUM_ARG1     5   // The register that would contain pointer size or less argument 1 (if any)
185 #define GENERIC_REGNUM_ARG2     6   // The register that would contain pointer size or less argument 2 (if any)
186 #define GENERIC_REGNUM_ARG3     7   // The register that would contain pointer size or less argument 3 (if any)
187 #define GENERIC_REGNUM_ARG4     8   // The register that would contain pointer size or less argument 4 (if any)
188 #define GENERIC_REGNUM_ARG5     9   // The register that would contain pointer size or less argument 5 (if any)
189 #define GENERIC_REGNUM_ARG6     10  // The register that would contain pointer size or less argument 6 (if any)
190 #define GENERIC_REGNUM_ARG7     11  // The register that would contain pointer size or less argument 7 (if any)
191 #define GENERIC_REGNUM_ARG8     12  // The register that would contain pointer size or less argument 8 (if any)
192 
193 enum DNBRegisterType
194 {
195     InvalidRegType = 0,
196     Uint,               // unsigned integer
197     Sint,               // signed integer
198     IEEE754,            // float
199     Vector              // vector registers
200 };
201 
202 enum DNBRegisterFormat
203 {
204     InvalidRegFormat = 0,
205     Binary,
206     Decimal,
207     Hex,
208     Float,
209     VectorOfSInt8,
210     VectorOfUInt8,
211     VectorOfSInt16,
212     VectorOfUInt16,
213     VectorOfSInt32,
214     VectorOfUInt32,
215     VectorOfFloat32,
216     VectorOfUInt128
217 };
218 
219 struct DNBRegisterInfo
220 {
221     uint32_t    set;            // Register set
222     uint32_t    reg;            // Register number
223     const char *name;           // Name of this register
224     const char *alt;            // Alternate name
225     uint16_t    type;           // Type of the register bits (DNBRegisterType)
226     uint16_t    format;         // Default format for display (DNBRegisterFormat),
227     uint32_t    size;           // Size in bytes of the register
228     uint32_t    offset;         // Offset from the beginning of the register context
229     uint32_t    reg_gcc;        // GCC register number (INVALID_NUB_REGNUM when none)
230     uint32_t    reg_dwarf;      // DWARF register number (INVALID_NUB_REGNUM when none)
231     uint32_t    reg_generic;    // Generic register number (INVALID_NUB_REGNUM when none)
232     uint32_t    reg_gdb;        // The GDB register number (INVALID_NUB_REGNUM when none)
233     uint32_t    *pseudo_regs;   // If this register is a part of another register, list the one or more registers
234     uint32_t    *update_regs;   // If modifying this register will invalidate other registers, list them here
235 };
236 
237 struct DNBRegisterSetInfo
238 {
239     const char *name;                           // Name of this register set
240     const struct DNBRegisterInfo *registers;    // An array of register descriptions
241     nub_size_t num_registers;                   // The number of registers in REGISTERS array above
242 };
243 
244 struct DNBThreadResumeAction
245 {
246     nub_thread_t tid;   // The thread ID that this action applies to, INVALID_NUB_THREAD for the default thread action
247     nub_state_t state;  // Valid values are eStateStopped/eStateSuspended, eStateRunning, and eStateStepping.
248     int signal;         // When resuming this thread, resume it with this signal
249     nub_addr_t addr;    // If not INVALID_NUB_ADDRESS, then set the PC for the thread to ADDR before resuming/stepping
250 };
251 
252 enum DNBThreadStopType
253 {
254     eStopTypeInvalid = 0,
255     eStopTypeSignal,
256     eStopTypeException
257 };
258 
259 enum DNBMemoryPermissions
260 {
261     eMemoryPermissionsWritable    = (1 << 0),
262     eMemoryPermissionsReadable    = (1 << 1),
263     eMemoryPermissionsExecutable  = (1 << 2)
264 };
265 
266 #define DNB_THREAD_STOP_INFO_MAX_DESC_LENGTH    256
267 #define DNB_THREAD_STOP_INFO_MAX_EXC_DATA       8
268 
269 //----------------------------------------------------------------------
270 // DNBThreadStopInfo
271 //
272 // Describes the reason a thread stopped.
273 //----------------------------------------------------------------------
274 struct DNBThreadStopInfo
275 {
276     DNBThreadStopType reason;
277     char description[DNB_THREAD_STOP_INFO_MAX_DESC_LENGTH];
278     union
279     {
280         // eStopTypeSignal
281         struct
282         {
283             uint32_t signo;
284         } signal;
285 
286         // eStopTypeException
287         struct
288         {
289             uint32_t type;
290             nub_size_t data_count;
291             nub_addr_t data[DNB_THREAD_STOP_INFO_MAX_EXC_DATA];
292         } exception;
293     } details;
294 };
295 
296 
297 struct DNBRegisterValue
298 {
299     struct DNBRegisterInfo info;    // Register information for this register
300     union
301     {
302         int8_t      sint8;
303         int16_t     sint16;
304         int32_t     sint32;
305         int64_t     sint64;
306         uint8_t     uint8;
307         uint16_t    uint16;
308         uint32_t    uint32;
309         uint64_t    uint64;
310         float       float32;
311         double      float64;
312         int8_t      v_sint8[32];
313         int16_t     v_sint16[16];
314         int32_t     v_sint32[8];
315         int64_t     v_sint64[4];
316         uint8_t     v_uint8[32];
317         uint16_t    v_uint16[16];
318         uint32_t    v_uint32[8];
319         uint64_t    v_uint64[4];
320         float       v_float32[8];
321         double      v_float64[4];
322         void        *pointer;
323         char        *c_str;
324     } value;
325 };
326 
327 enum DNBSharedLibraryState
328 {
329     eShlibStateUnloaded    = 0,
330     eShlibStateLoaded    = 1
331 };
332 
333 #ifndef DNB_MAX_SEGMENT_NAME_LENGTH
334 #define DNB_MAX_SEGMENT_NAME_LENGTH    32
335 #endif
336 
337 struct DNBSegment
338 {
339     char        name[DNB_MAX_SEGMENT_NAME_LENGTH];
340     nub_addr_t  addr;
341     nub_addr_t  size;
342 };
343 
344 struct DNBExecutableImageInfo
345 {
346     char        name[PATH_MAX]; // Name of the executable image (usually a full path)
347     uint32_t    state;          // State of the executable image (see enum DNBSharedLibraryState)
348     nub_addr_t  header_addr;    // Executable header address
349     uuid_t      uuid;           // Unique indentifier for matching with symbols
350     uint32_t    num_segments;   // Number of contiguous memory segments to in SEGMENTS array
351     DNBSegment  *segments;      // Array of contiguous memory segments in executable
352 };
353 
354 struct DNBRegionInfo
355 {
356     nub_addr_t addr;
357     nub_addr_t size;
358     uint32_t permissions;
359 };
360 
361 enum DNBProfileDataScanType
362 {
363     eProfileHostCPU             = (1 << 0),
364     eProfileCPU                 = (1 << 1),
365 
366     eProfileThreadsCPU          = (1 << 2), // By default excludes eProfileThreadName and eProfileQueueName.
367     eProfileThreadName          = (1 << 3), // Assume eProfileThreadsCPU, get thread name as well.
368     eProfileQueueName           = (1 << 4), // Assume eProfileThreadsCPU, get queue name as well.
369 
370     eProfileHostMemory          = (1 << 5),
371 
372     eProfileMemory              = (1 << 6), // By default, excludes eProfileMemoryDirtyPage.
373     eProfileMemoryDirtyPage     = (1 << 7), // Assume eProfileMemory, get Dirty Page size as well.
374 
375     eProfileAll                 = 0xffffffff
376 };
377 
378 typedef nub_bool_t (*DNBCallbackBreakpointHit)(nub_process_t pid, nub_thread_t tid, nub_break_t breakID, void *baton);
379 typedef nub_addr_t (*DNBCallbackNameToAddress)(nub_process_t pid, const char *name, const char *shlib_regex, void *baton);
380 typedef nub_size_t (*DNBCallbackCopyExecutableImageInfos)(nub_process_t pid, struct DNBExecutableImageInfo **image_infos, nub_bool_t only_changed, void *baton);
381 typedef void (*DNBCallbackLog)(void *baton, uint32_t flags, const char *format, va_list args);
382 
383 #endif    // #ifndef __DNBDefs_h__
384