1 //===-- DNB.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 3/23/07. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef __DNB_h__ 14 #define __DNB_h__ 15 16 #include "DNBDefs.h" 17 #include "JSONGenerator.h" 18 #include "MacOSX/DarwinLog/DarwinLogEvent.h" 19 #include "MacOSX/Genealogy.h" 20 #include "MacOSX/ThreadInfo.h" 21 #include <mach/thread_info.h> 22 #include <string> 23 #include <Availability.h> 24 #include <mach/machine.h> 25 26 #define DNB_EXPORT __attribute__((visibility("default"))) 27 28 #ifndef CPU_TYPE_ARM64 29 #define CPU_TYPE_ARM64 ((cpu_type_t)12 | 0x01000000) 30 #endif 31 32 #ifndef CPU_TYPE_ARM64_32 33 #define CPU_TYPE_ARM64_32 ((cpu_type_t)12 | 0x02000000) 34 #endif 35 36 typedef bool (*DNBShouldCancelCallback)(void *); 37 38 void DNBInitialize(); 39 void DNBTerminate(); 40 41 nub_bool_t DNBSetArchitecture(const char *arch); 42 43 // Process control 44 nub_process_t DNBProcessLaunch( 45 const char *path, char const *argv[], const char *envp[], 46 const char *working_directory, // NULL => don't change, non-NULL => set 47 // working directory for inferior to this 48 const char *stdin_path, const char *stdout_path, const char *stderr_path, 49 bool no_stdio, nub_launch_flavor_t launch_flavor, int disable_aslr, 50 const char *event_data, char *err_str, size_t err_len); 51 52 nub_process_t DNBProcessGetPIDByName(const char *name); 53 nub_process_t DNBProcessAttach(nub_process_t pid, struct timespec *timeout, 54 char *err_str, size_t err_len); 55 nub_process_t DNBProcessAttachByName(const char *name, struct timespec *timeout, 56 char *err_str, size_t err_len); 57 nub_process_t 58 DNBProcessAttachWait(const char *wait_name, nub_launch_flavor_t launch_flavor, 59 bool ignore_existing, struct timespec *timeout, 60 useconds_t interval, char *err_str, size_t err_len, 61 DNBShouldCancelCallback should_cancel = NULL, 62 void *callback_data = NULL); 63 // Resume a process with exact instructions on what to do with each thread: 64 // - If no thread actions are supplied (actions is NULL or num_actions is zero), 65 // then all threads are continued. 66 // - If any thread actions are supplied, then each thread will do as it is told 67 // by the action. A default actions for any threads that don't have an 68 // explicit thread action can be made by making a thread action with a tid of 69 // INVALID_NUB_THREAD. If there is no default action, those threads will 70 // remain stopped. 71 nub_bool_t DNBProcessResume(nub_process_t pid, 72 const DNBThreadResumeAction *actions, 73 size_t num_actions) DNB_EXPORT; 74 nub_bool_t DNBProcessHalt(nub_process_t pid) DNB_EXPORT; 75 nub_bool_t DNBProcessDetach(nub_process_t pid) DNB_EXPORT; 76 nub_bool_t DNBProcessSignal(nub_process_t pid, int signal) DNB_EXPORT; 77 nub_bool_t DNBProcessInterrupt(nub_process_t pid) DNB_EXPORT; 78 nub_bool_t DNBProcessKill(nub_process_t pid) DNB_EXPORT; 79 nub_bool_t DNBProcessSendEvent(nub_process_t pid, const char *event) DNB_EXPORT; 80 nub_size_t DNBProcessMemoryRead(nub_process_t pid, nub_addr_t addr, 81 nub_size_t size, void *buf) DNB_EXPORT; 82 uint64_t DNBProcessMemoryReadInteger(nub_process_t pid, nub_addr_t addr, 83 nub_size_t integer_size, 84 uint64_t fail_value) DNB_EXPORT; 85 nub_addr_t DNBProcessMemoryReadPointer(nub_process_t pid, 86 nub_addr_t addr) DNB_EXPORT; 87 std::string DNBProcessMemoryReadCString(nub_process_t pid, 88 nub_addr_t addr) DNB_EXPORT; 89 std::string 90 DNBProcessMemoryReadCStringFixed(nub_process_t pid, nub_addr_t addr, 91 nub_size_t fixed_length) DNB_EXPORT; 92 nub_size_t DNBProcessMemoryWrite(nub_process_t pid, nub_addr_t addr, 93 nub_size_t size, const void *buf) DNB_EXPORT; 94 nub_addr_t DNBProcessMemoryAllocate(nub_process_t pid, nub_size_t size, 95 uint32_t permissions) DNB_EXPORT; 96 nub_bool_t DNBProcessMemoryDeallocate(nub_process_t pid, 97 nub_addr_t addr) DNB_EXPORT; 98 int DNBProcessMemoryRegionInfo(nub_process_t pid, nub_addr_t addr, 99 DNBRegionInfo *region_info) DNB_EXPORT; 100 std::string 101 DNBProcessGetProfileData(nub_process_t pid, 102 DNBProfileDataScanType scanType) DNB_EXPORT; 103 nub_bool_t 104 DNBProcessSetEnableAsyncProfiling(nub_process_t pid, nub_bool_t enable, 105 uint64_t interval_usec, 106 DNBProfileDataScanType scan_type) DNB_EXPORT; 107 DarwinLogEventVector DNBProcessGetAvailableDarwinLogEvents(nub_process_t pid); 108 109 // Process status 110 nub_bool_t DNBProcessIsAlive(nub_process_t pid) DNB_EXPORT; 111 nub_state_t DNBProcessGetState(nub_process_t pid) DNB_EXPORT; 112 nub_bool_t DNBProcessGetExitStatus(nub_process_t pid, int *status) DNB_EXPORT; 113 nub_bool_t DNBProcessSetExitStatus(nub_process_t pid, int status) DNB_EXPORT; 114 const char *DNBProcessGetExitInfo(nub_process_t pid) DNB_EXPORT; 115 nub_bool_t DNBProcessSetExitInfo(nub_process_t pid, 116 const char *info) DNB_EXPORT; 117 nub_size_t DNBProcessGetNumThreads(nub_process_t pid) DNB_EXPORT; 118 nub_thread_t DNBProcessGetCurrentThread(nub_process_t pid) DNB_EXPORT; 119 nub_thread_t DNBProcessGetCurrentThreadMachPort(nub_process_t pid) DNB_EXPORT; 120 nub_thread_t DNBProcessSetCurrentThread(nub_process_t pid, 121 nub_thread_t tid) DNB_EXPORT; 122 nub_thread_t DNBProcessGetThreadAtIndex(nub_process_t pid, 123 nub_size_t thread_idx) DNB_EXPORT; 124 nub_bool_t DNBProcessSyncThreadState(nub_process_t pid, 125 nub_thread_t tid) DNB_EXPORT; 126 nub_addr_t DNBProcessGetSharedLibraryInfoAddress(nub_process_t pid) DNB_EXPORT; 127 nub_bool_t DNBProcessSharedLibrariesUpdated(nub_process_t pid) DNB_EXPORT; 128 nub_size_t 129 DNBProcessGetSharedLibraryInfo(nub_process_t pid, nub_bool_t only_changed, 130 DNBExecutableImageInfo **image_infos) DNB_EXPORT; 131 const char *DNBGetDeploymentInfo(nub_process_t pid, 132 const struct load_command& lc, 133 uint64_t load_command_address, 134 uint32_t& major_version, 135 uint32_t& minor_version, 136 uint32_t& patch_version); 137 nub_bool_t DNBProcessSetNameToAddressCallback(nub_process_t pid, 138 DNBCallbackNameToAddress callback, 139 void *baton) DNB_EXPORT; 140 nub_bool_t DNBProcessSetSharedLibraryInfoCallback( 141 nub_process_t pid, DNBCallbackCopyExecutableImageInfos callback, 142 void *baton) DNB_EXPORT; 143 nub_addr_t DNBProcessLookupAddress(nub_process_t pid, const char *name, 144 const char *shlib) DNB_EXPORT; 145 nub_size_t DNBProcessGetAvailableSTDOUT(nub_process_t pid, char *buf, 146 nub_size_t buf_size) DNB_EXPORT; 147 nub_size_t DNBProcessGetAvailableSTDERR(nub_process_t pid, char *buf, 148 nub_size_t buf_size) DNB_EXPORT; 149 nub_size_t DNBProcessGetAvailableProfileData(nub_process_t pid, char *buf, 150 nub_size_t buf_size) DNB_EXPORT; 151 nub_size_t DNBProcessGetStopCount(nub_process_t pid) DNB_EXPORT; 152 uint32_t DNBProcessGetCPUType(nub_process_t pid) DNB_EXPORT; 153 154 // Process executable and arguments 155 const char *DNBProcessGetExecutablePath(nub_process_t pid); 156 const char *DNBProcessGetArgumentAtIndex(nub_process_t pid, nub_size_t idx); 157 nub_size_t DNBProcessGetArgumentCount(nub_process_t pid); 158 159 // Process events 160 nub_event_t DNBProcessWaitForEvents(nub_process_t pid, nub_event_t event_mask, 161 bool wait_for_set, 162 struct timespec *timeout); 163 void DNBProcessResetEvents(nub_process_t pid, nub_event_t event_mask); 164 165 // Thread functions 166 const char *DNBThreadGetName(nub_process_t pid, nub_thread_t tid); 167 nub_bool_t 168 DNBThreadGetIdentifierInfo(nub_process_t pid, nub_thread_t tid, 169 thread_identifier_info_data_t *ident_info); 170 nub_state_t DNBThreadGetState(nub_process_t pid, nub_thread_t tid); 171 nub_bool_t DNBThreadGetRegisterValueByID(nub_process_t pid, nub_thread_t tid, 172 uint32_t set, uint32_t reg, 173 DNBRegisterValue *value); 174 nub_bool_t DNBThreadSetRegisterValueByID(nub_process_t pid, nub_thread_t tid, 175 uint32_t set, uint32_t reg, 176 const DNBRegisterValue *value); 177 nub_size_t DNBThreadGetRegisterContext(nub_process_t pid, nub_thread_t tid, 178 void *buf, size_t buf_len); 179 nub_size_t DNBThreadSetRegisterContext(nub_process_t pid, nub_thread_t tid, 180 const void *buf, size_t buf_len); 181 uint32_t DNBThreadSaveRegisterState(nub_process_t pid, nub_thread_t tid); 182 nub_bool_t DNBThreadRestoreRegisterState(nub_process_t pid, nub_thread_t tid, 183 uint32_t save_id); 184 nub_bool_t DNBThreadGetRegisterValueByName(nub_process_t pid, nub_thread_t tid, 185 uint32_t set, const char *name, 186 DNBRegisterValue *value); 187 nub_bool_t DNBThreadGetStopReason(nub_process_t pid, nub_thread_t tid, 188 DNBThreadStopInfo *stop_info); 189 const char *DNBThreadGetInfo(nub_process_t pid, nub_thread_t tid); 190 Genealogy::ThreadActivitySP DNBGetGenealogyInfoForThread(nub_process_t pid, 191 nub_thread_t tid, 192 bool &timed_out); 193 Genealogy::ProcessExecutableInfoSP DNBGetGenealogyImageInfo(nub_process_t pid, 194 size_t idx); 195 ThreadInfo::QoS DNBGetRequestedQoSForThread(nub_process_t pid, nub_thread_t tid, 196 nub_addr_t tsd, 197 uint64_t dti_qos_class_index); 198 nub_addr_t DNBGetPThreadT(nub_process_t pid, nub_thread_t tid); 199 nub_addr_t DNBGetDispatchQueueT(nub_process_t pid, nub_thread_t tid); 200 nub_addr_t 201 DNBGetTSDAddressForThread(nub_process_t pid, nub_thread_t tid, 202 uint64_t plo_pthread_tsd_base_address_offset, 203 uint64_t plo_pthread_tsd_base_offset, 204 uint64_t plo_pthread_tsd_entry_size); 205 JSONGenerator::ObjectSP DNBGetLoadedDynamicLibrariesInfos( 206 nub_process_t pid, nub_addr_t image_list_address, nub_addr_t image_count); 207 JSONGenerator::ObjectSP DNBGetAllLoadedLibrariesInfos(nub_process_t pid); 208 JSONGenerator::ObjectSP 209 DNBGetLibrariesInfoForAddresses(nub_process_t pid, 210 std::vector<uint64_t> &macho_addresses); 211 JSONGenerator::ObjectSP DNBGetSharedCacheInfo(nub_process_t pid); 212 213 // 214 // Breakpoint functions 215 nub_bool_t DNBBreakpointSet(nub_process_t pid, nub_addr_t addr, nub_size_t size, 216 nub_bool_t hardware); 217 nub_bool_t DNBBreakpointClear(nub_process_t pid, nub_addr_t addr); 218 219 // Watchpoint functions 220 nub_bool_t DNBWatchpointSet(nub_process_t pid, nub_addr_t addr, nub_size_t size, 221 uint32_t watch_flags, nub_bool_t hardware); 222 nub_bool_t DNBWatchpointClear(nub_process_t pid, nub_addr_t addr); 223 uint32_t DNBWatchpointGetNumSupportedHWP(nub_process_t pid); 224 225 uint32_t DNBGetRegisterCPUType(); 226 const DNBRegisterSetInfo *DNBGetRegisterSetInfo(nub_size_t *num_reg_sets); 227 nub_bool_t DNBGetRegisterInfoByName(const char *reg_name, 228 DNBRegisterInfo *info); 229 230 // Other static nub information calls. 231 const char *DNBStateAsString(nub_state_t state); 232 nub_bool_t DNBResolveExecutablePath(const char *path, char *resolved_path, 233 size_t resolved_path_size); 234 bool DNBGetOSVersionNumbers(uint64_t *major, uint64_t *minor, uint64_t *patch); 235 /// \return the iOSSupportVersion of the host OS. 236 std::string DNBGetMacCatalystVersionString(); 237 #endif 238