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