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