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