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