xref: /openbsd-src/gnu/llvm/lldb/tools/debugserver/source/DNB.h (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
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/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 size_t DNBGetAllInfos(std::vector<struct kinfo_proc> &proc_infos);
154 
155 // Process executable and arguments
156 const char *DNBProcessGetExecutablePath(nub_process_t pid);
157 const char *DNBProcessGetArgumentAtIndex(nub_process_t pid, nub_size_t idx);
158 nub_size_t DNBProcessGetArgumentCount(nub_process_t pid);
159 
160 // Process events
161 nub_event_t DNBProcessWaitForEvents(nub_process_t pid, nub_event_t event_mask,
162                                     bool wait_for_set,
163                                     struct timespec *timeout);
164 void DNBProcessResetEvents(nub_process_t pid, nub_event_t event_mask);
165 
166 // Thread functions
167 const char *DNBThreadGetName(nub_process_t pid, nub_thread_t tid);
168 nub_bool_t
169 DNBThreadGetIdentifierInfo(nub_process_t pid, nub_thread_t tid,
170                            thread_identifier_info_data_t *ident_info);
171 nub_state_t DNBThreadGetState(nub_process_t pid, nub_thread_t tid);
172 nub_bool_t DNBThreadGetRegisterValueByID(nub_process_t pid, nub_thread_t tid,
173                                          uint32_t set, uint32_t reg,
174                                          DNBRegisterValue *value);
175 nub_bool_t DNBThreadSetRegisterValueByID(nub_process_t pid, nub_thread_t tid,
176                                          uint32_t set, uint32_t reg,
177                                          const DNBRegisterValue *value);
178 nub_size_t DNBThreadGetRegisterContext(nub_process_t pid, nub_thread_t tid,
179                                        void *buf, size_t buf_len);
180 nub_size_t DNBThreadSetRegisterContext(nub_process_t pid, nub_thread_t tid,
181                                        const void *buf, size_t buf_len);
182 uint32_t DNBThreadSaveRegisterState(nub_process_t pid, nub_thread_t tid);
183 nub_bool_t DNBThreadRestoreRegisterState(nub_process_t pid, nub_thread_t tid,
184                                          uint32_t save_id);
185 nub_bool_t DNBThreadGetRegisterValueByName(nub_process_t pid, nub_thread_t tid,
186                                            uint32_t set, const char *name,
187                                            DNBRegisterValue *value);
188 nub_bool_t DNBThreadGetStopReason(nub_process_t pid, nub_thread_t tid,
189                                   DNBThreadStopInfo *stop_info);
190 const char *DNBThreadGetInfo(nub_process_t pid, nub_thread_t tid);
191 Genealogy::ThreadActivitySP DNBGetGenealogyInfoForThread(nub_process_t pid,
192                                                          nub_thread_t tid,
193                                                          bool &timed_out);
194 Genealogy::ProcessExecutableInfoSP DNBGetGenealogyImageInfo(nub_process_t pid,
195                                                             size_t idx);
196 ThreadInfo::QoS DNBGetRequestedQoSForThread(nub_process_t pid, nub_thread_t tid,
197                                             nub_addr_t tsd,
198                                             uint64_t dti_qos_class_index);
199 nub_addr_t DNBGetPThreadT(nub_process_t pid, nub_thread_t tid);
200 nub_addr_t DNBGetDispatchQueueT(nub_process_t pid, nub_thread_t tid);
201 nub_addr_t
202 DNBGetTSDAddressForThread(nub_process_t pid, nub_thread_t tid,
203                           uint64_t plo_pthread_tsd_base_address_offset,
204                           uint64_t plo_pthread_tsd_base_offset,
205                           uint64_t plo_pthread_tsd_entry_size);
206 JSONGenerator::ObjectSP DNBGetLoadedDynamicLibrariesInfos(
207     nub_process_t pid, nub_addr_t image_list_address, nub_addr_t image_count);
208 JSONGenerator::ObjectSP DNBGetAllLoadedLibrariesInfos(nub_process_t pid);
209 JSONGenerator::ObjectSP
210 DNBGetLibrariesInfoForAddresses(nub_process_t pid,
211                                 std::vector<uint64_t> &macho_addresses);
212 JSONGenerator::ObjectSP DNBGetSharedCacheInfo(nub_process_t pid);
213 
214 //
215 // Breakpoint functions
216 nub_bool_t DNBBreakpointSet(nub_process_t pid, nub_addr_t addr, nub_size_t size,
217                             nub_bool_t hardware);
218 nub_bool_t DNBBreakpointClear(nub_process_t pid, nub_addr_t addr);
219 
220 // Watchpoint functions
221 nub_bool_t DNBWatchpointSet(nub_process_t pid, nub_addr_t addr, nub_size_t size,
222                             uint32_t watch_flags, nub_bool_t hardware);
223 nub_bool_t DNBWatchpointClear(nub_process_t pid, nub_addr_t addr);
224 uint32_t DNBWatchpointGetNumSupportedHWP(nub_process_t pid);
225 
226 uint32_t DNBGetRegisterCPUType();
227 const DNBRegisterSetInfo *DNBGetRegisterSetInfo(nub_size_t *num_reg_sets);
228 nub_bool_t DNBGetRegisterInfoByName(const char *reg_name,
229                                     DNBRegisterInfo *info);
230 
231 // Other static nub information calls.
232 const char *DNBStateAsString(nub_state_t state);
233 nub_bool_t DNBResolveExecutablePath(const char *path, char *resolved_path,
234                                     size_t resolved_path_size);
235 bool DNBGetOSVersionNumbers(uint64_t *major, uint64_t *minor, uint64_t *patch);
236 /// \return the iOSSupportVersion of the host OS.
237 std::string DNBGetMacCatalystVersionString();
238 #endif
239