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