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