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