xref: /llvm-project/lldb/tools/debugserver/source/DNBDefs.h (revision 46e782300765eeac8026377bf30d5f08888c2b25)
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   eStopTypeWatchpoint
239 };
240 
241 enum DNBMemoryPermissions {
242   eMemoryPermissionsWritable = (1 << 0),
243   eMemoryPermissionsReadable = (1 << 1),
244   eMemoryPermissionsExecutable = (1 << 2)
245 };
246 
247 #define DNB_THREAD_STOP_INFO_MAX_DESC_LENGTH 256
248 #define DNB_THREAD_STOP_INFO_MAX_EXC_DATA 8
249 
250 // DNBThreadStopInfo
251 //
252 // Describes the reason a thread stopped.
253 struct DNBThreadStopInfo {
254   DNBThreadStopType reason;
255   char description[DNB_THREAD_STOP_INFO_MAX_DESC_LENGTH];
256   union {
257     // eStopTypeSignal
258     struct {
259       uint32_t signo;
260     } signal;
261 
262     // eStopTypeException
263     struct {
264       uint32_t type;
265       nub_size_t data_count;
266       nub_addr_t data[DNB_THREAD_STOP_INFO_MAX_EXC_DATA];
267     } exception;
268 
269     // eStopTypeWatchpoint
270     struct {
271       // The trigger address from the mach exception
272       // (likely the contents of the FAR register)
273       nub_addr_t mach_exception_addr;
274 
275       // The trigger address, adjusted to be the start
276       // address of one of the existing watchpoints for
277       // lldb's benefit.
278       nub_addr_t addr;
279 
280       // The watchpoint hardware index.
281       uint32_t hw_idx;
282 
283       // If the esr_fields bitfields have been filled in.
284       bool esr_fields_set;
285       struct {
286         uint32_t
287             iss; // "ISS encoding for an exception from a Watchpoint exception"
288         uint32_t wpt;  // Watchpoint number
289         bool wptv;     // Watchpoint number Valid
290         bool wpf;      // Watchpoint might be false-positive
291         bool fnp;      // FAR not Precise
292         bool vncr;     // watchpoint from use of VNCR_EL2 reg by EL1
293         bool fnv;      // FAR not Valid
294         bool cm;       // Cache maintenance
295         bool wnr;      // Write not Read
296         uint32_t dfsc; // Data Fault Status Code
297       } esr_fields;
298     } watchpoint;
299   } details;
300 };
301 
302 struct DNBRegisterValue {
303   struct DNBRegisterInfo info; // Register information for this register
304   union {
305     int8_t sint8;
306     int16_t sint16;
307     int32_t sint32;
308     int64_t sint64;
309     uint8_t uint8;
310     uint16_t uint16;
311     uint32_t uint32;
312     uint64_t uint64;
313     float float32;
314     double float64;
315     // AArch64 SME's ZA register max size is 64k, this object must be
316     // large enough to hold that much data.  The current Apple cores
317     // have a much smaller maximum ZA reg size, but there are not
318     // multiple copies of this object so increase the static size to
319     // maximum possible.
320     int8_t v_sint8[65536];
321     int16_t v_sint16[32768];
322     int32_t v_sint32[16384];
323     int64_t v_sint64[8192];
324     uint8_t v_uint8[65536];
325     uint16_t v_uint16[32768];
326     uint32_t v_uint32[16384];
327     uint64_t v_uint64[8192];
328     float v_float32[16384];
329     double v_float64[8192];
330     void *pointer;
331     char *c_str;
332   } value;
333 };
334 
335 enum DNBSharedLibraryState { eShlibStateUnloaded = 0, eShlibStateLoaded = 1 };
336 
337 #ifndef DNB_MAX_SEGMENT_NAME_LENGTH
338 #define DNB_MAX_SEGMENT_NAME_LENGTH 32
339 #endif
340 
341 struct DNBSegment {
342   char name[DNB_MAX_SEGMENT_NAME_LENGTH];
343   nub_addr_t addr;
344   nub_addr_t size;
345 };
346 
347 struct DNBExecutableImageInfo {
348   char name[PATH_MAX]; // Name of the executable image (usually a full path)
349   uint32_t
350       state; // State of the executable image (see enum DNBSharedLibraryState)
351   nub_addr_t header_addr; // Executable header address
352   uuid_t uuid;            // Unique identifier for matching with symbols
353   uint32_t
354       num_segments; // Number of contiguous memory segments to in SEGMENTS array
355   DNBSegment *segments; // Array of contiguous memory segments in executable
356 };
357 
358 struct DNBRegionInfo {
359 public:
360   DNBRegionInfo()
361       : addr(0), size(0), permissions(0), dirty_pages(), vm_types() {}
362   nub_addr_t addr;
363   nub_addr_t size;
364   uint32_t permissions;
365   std::vector<nub_addr_t> dirty_pages;
366   std::vector<std::string> vm_types;
367 };
368 
369 enum DNBProfileDataScanType {
370   eProfileHostCPU = (1 << 0),
371   eProfileCPU = (1 << 1),
372 
373   eProfileThreadsCPU =
374       (1 << 2), // By default excludes eProfileThreadName and eProfileQueueName.
375   eProfileThreadName =
376       (1 << 3), // Assume eProfileThreadsCPU, get thread name as well.
377   eProfileQueueName =
378       (1 << 4), // Assume eProfileThreadsCPU, get queue name as well.
379 
380   eProfileHostMemory = (1 << 5),
381 
382   eProfileMemory = (1 << 6),
383   eProfileMemoryAnonymous =
384       (1 << 8), // Assume eProfileMemory, get Anonymous memory as well.
385 
386   eProfileEnergy = (1 << 9),
387   eProfileEnergyCPUCap = (1 << 10),
388 
389   eProfileMemoryCap = (1 << 15),
390 
391   eProfileAll = 0xffffffff
392 };
393 
394 typedef nub_addr_t (*DNBCallbackNameToAddress)(nub_process_t pid,
395                                                const char *name,
396                                                const char *shlib_regex,
397                                                void *baton);
398 typedef nub_size_t (*DNBCallbackCopyExecutableImageInfos)(
399     nub_process_t pid, struct DNBExecutableImageInfo **image_infos,
400     nub_bool_t only_changed, void *baton);
401 typedef void (*DNBCallbackLog)(void *baton, uint32_t flags, const char *format,
402                                va_list args);
403 
404 #define UNUSED_IF_ASSERT_DISABLED(x) ((void)(x))
405 
406 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBDEFS_H
407