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
37typedef bool (*DNBShouldCancelCallback)(void *);
38
39void DNBInitialize();
40void DNBTerminate();
41
42nub_bool_t DNBSetArchitecture(const char *arch);
43
44// Process control
45nub_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
53nub_process_t DNBProcessGetPIDByName(const char *name);
54nub_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);
59nub_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);
64nub_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.
79nub_bool_t DNBProcessResume(nub_process_t pid,
80 const DNBThreadResumeAction *actions,
81 size_t num_actions) DNB_EXPORT;
82nub_bool_t DNBProcessHalt(nub_process_t pid) DNB_EXPORT;
83nub_bool_t DNBProcessDetach(nub_process_t pid) DNB_EXPORT;
84nub_bool_t DNBProcessSignal(nub_process_t pid, int signal) DNB_EXPORT;
85nub_bool_t DNBProcessInterrupt(nub_process_t pid) DNB_EXPORT;
86nub_bool_t DNBProcessKill(nub_process_t pid) DNB_EXPORT;
87nub_bool_t DNBProcessSendEvent(nub_process_t pid, const char *event) DNB_EXPORT;
88nub_size_t DNBProcessMemoryRead(nub_process_t pid, nub_addr_t addr,
89 nub_size_t size, void *buf) DNB_EXPORT;
90uint64_t DNBProcessMemoryReadInteger(nub_process_t pid, nub_addr_t addr,
91 nub_size_t integer_size,
92 uint64_t fail_value) DNB_EXPORT;
93nub_addr_t DNBProcessMemoryReadPointer(nub_process_t pid,
94 nub_addr_t addr) DNB_EXPORT;
95std::string DNBProcessMemoryReadCString(nub_process_t pid,
96 nub_addr_t addr) DNB_EXPORT;
97std::string
98DNBProcessMemoryReadCStringFixed(nub_process_t pid, nub_addr_t addr,
99 nub_size_t fixed_length) DNB_EXPORT;
100nub_size_t DNBProcessMemoryWrite(nub_process_t pid, nub_addr_t addr,
101 nub_size_t size, const void *buf) DNB_EXPORT;
102nub_addr_t DNBProcessMemoryAllocate(nub_process_t pid, nub_size_t size,
103 uint32_t permissions) DNB_EXPORT;
104nub_bool_t DNBProcessMemoryDeallocate(nub_process_t pid,
105 nub_addr_t addr) DNB_EXPORT;
106int DNBProcessMemoryRegionInfo(nub_process_t pid, nub_addr_t addr,
107 DNBRegionInfo *region_info) DNB_EXPORT;
108std::string
109DNBProcessGetProfileData(nub_process_t pid,
110 DNBProfileDataScanType scanType) DNB_EXPORT;
111nub_bool_t
112DNBProcessSetEnableAsyncProfiling(nub_process_t pid, nub_bool_t enable,
113 uint64_t interval_usec,
114 DNBProfileDataScanType scan_type) DNB_EXPORT;
115
116// Process status
117nub_bool_t DNBProcessIsAlive(nub_process_t pid) DNB_EXPORT;
118nub_state_t DNBProcessGetState(nub_process_t pid) DNB_EXPORT;
119nub_bool_t DNBProcessGetExitStatus(nub_process_t pid, int *status) DNB_EXPORT;
120nub_bool_t DNBProcessSetExitStatus(nub_process_t pid, int status) DNB_EXPORT;
121const char *DNBProcessGetExitInfo(nub_process_t pid) DNB_EXPORT;
122nub_bool_t DNBProcessSetExitInfo(nub_process_t pid,
123 const char *info) DNB_EXPORT;
124nub_size_t DNBProcessGetNumThreads(nub_process_t pid) DNB_EXPORT;
125nub_thread_t DNBProcessGetCurrentThread(nub_process_t pid) DNB_EXPORT;
126nub_thread_t DNBProcessGetCurrentThreadMachPort(nub_process_t pid) DNB_EXPORT;
127nub_thread_t DNBProcessSetCurrentThread(nub_process_t pid,
128 nub_thread_t tid) DNB_EXPORT;
129nub_thread_t DNBProcessGetThreadAtIndex(nub_process_t pid,
130 nub_size_t thread_idx) DNB_EXPORT;
131nub_bool_t DNBProcessSyncThreadState(nub_process_t pid,
132 nub_thread_t tid) DNB_EXPORT;
133nub_addr_t DNBProcessGetSharedLibraryInfoAddress(nub_process_t pid) DNB_EXPORT;
134nub_bool_t DNBProcessSharedLibrariesUpdated(nub_process_t pid) DNB_EXPORT;
135nub_size_t
136DNBProcessGetSharedLibraryInfo(nub_process_t pid, nub_bool_t only_changed,
137 DNBExecutableImageInfo **image_infos) DNB_EXPORT;
138std::optional<std::string>
139DNBGetDeploymentInfo(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);
143nub_bool_t DNBProcessSetNameToAddressCallback(nub_process_t pid,
144 DNBCallbackNameToAddress callback,
145 void *baton) DNB_EXPORT;
146nub_bool_t DNBProcessSetSharedLibraryInfoCallback(
147 nub_process_t pid, DNBCallbackCopyExecutableImageInfos callback,
148 void *baton) DNB_EXPORT;
149nub_addr_t DNBProcessLookupAddress(nub_process_t pid, const char *name,
150 const char *shlib) DNB_EXPORT;
151nub_size_t DNBProcessGetAvailableSTDOUT(nub_process_t pid, char *buf,
152 nub_size_t buf_size) DNB_EXPORT;
153nub_size_t DNBProcessGetAvailableSTDERR(nub_process_t pid, char *buf,
154 nub_size_t buf_size) DNB_EXPORT;
155nub_size_t DNBProcessGetAvailableProfileData(nub_process_t pid, char *buf,
156 nub_size_t buf_size) DNB_EXPORT;
157nub_size_t DNBProcessGetStopCount(nub_process_t pid) DNB_EXPORT;
158uint32_t DNBProcessGetCPUType(nub_process_t pid) DNB_EXPORT;
159size_t DNBGetAllInfos(std::vector<struct kinfo_proc> &proc_infos);
160JSONGenerator::ObjectSP DNBGetDyldProcessState(nub_process_t pid);
161
162// Process executable and arguments
163const char *DNBProcessGetExecutablePath(nub_process_t pid);
164const char *DNBProcessGetArgumentAtIndex(nub_process_t pid, nub_size_t idx);
165nub_size_t DNBProcessGetArgumentCount(nub_process_t pid);
166
167// Process events
168nub_event_t DNBProcessWaitForEvents(nub_process_t pid, nub_event_t event_mask,
169 bool wait_for_set,
170 struct timespec *timeout);
171void DNBProcessResetEvents(nub_process_t pid, nub_event_t event_mask);
172
173// Thread functions
174const char *DNBThreadGetName(nub_process_t pid, nub_thread_t tid);
175nub_bool_t
176DNBThreadGetIdentifierInfo(nub_process_t pid, nub_thread_t tid,
177 thread_identifier_info_data_t *ident_info);
178nub_state_t DNBThreadGetState(nub_process_t pid, nub_thread_t tid);
179nub_bool_t DNBThreadGetRegisterValueByID(nub_process_t pid, nub_thread_t tid,
180 uint32_t set, uint32_t reg,
181 DNBRegisterValue *value);
182nub_bool_t DNBThreadSetRegisterValueByID(nub_process_t pid, nub_thread_t tid,
183 uint32_t set, uint32_t reg,
184 const DNBRegisterValue *value);
185nub_size_t DNBThreadGetRegisterContext(nub_process_t pid, nub_thread_t tid,
186 void *buf, size_t buf_len);
187nub_size_t DNBThreadSetRegisterContext(nub_process_t pid, nub_thread_t tid,
188 const void *buf, size_t buf_len);
189uint32_t DNBThreadSaveRegisterState(nub_process_t pid, nub_thread_t tid);
190nub_bool_t DNBThreadRestoreRegisterState(nub_process_t pid, nub_thread_t tid,
191 uint32_t save_id);
192nub_bool_t DNBThreadGetRegisterValueByName(nub_process_t pid, nub_thread_t tid,
193 uint32_t set, const char *name,
194 DNBRegisterValue *value);
195nub_bool_t DNBThreadGetStopReason(nub_process_t pid, nub_thread_t tid,
196 DNBThreadStopInfo *stop_info);
197const char *DNBThreadGetInfo(nub_process_t pid, nub_thread_t tid);
198Genealogy::ThreadActivitySP DNBGetGenealogyInfoForThread(nub_process_t pid,
199 nub_thread_t tid,
200 bool &timed_out);
201Genealogy::ProcessExecutableInfoSP DNBGetGenealogyImageInfo(nub_process_t pid,
202 size_t idx);
203ThreadInfo::QoS DNBGetRequestedQoSForThread(nub_process_t pid, nub_thread_t tid,
204 nub_addr_t tsd,
205 uint64_t dti_qos_class_index);
206nub_addr_t DNBGetPThreadT(nub_process_t pid, nub_thread_t tid);
207nub_addr_t DNBGetDispatchQueueT(nub_process_t pid, nub_thread_t tid);
208nub_addr_t
209DNBGetTSDAddressForThread(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);
213std::optional<std::pair<cpu_type_t, cpu_subtype_t>>
214DNBGetMainBinaryCPUTypes(nub_process_t pid);
215JSONGenerator::ObjectSP
216DNBGetAllLoadedLibrariesInfos(nub_process_t pid, bool report_load_commands);
217JSONGenerator::ObjectSP
218DNBGetLibrariesInfoForAddresses(nub_process_t pid,
219 std::vector<uint64_t> &macho_addresses);
220JSONGenerator::ObjectSP DNBGetSharedCacheInfo(nub_process_t pid);
221
222//
223// Breakpoint functions
224nub_bool_t DNBBreakpointSet(nub_process_t pid, nub_addr_t addr, nub_size_t size,
225 nub_bool_t hardware);
226nub_bool_t DNBBreakpointClear(nub_process_t pid, nub_addr_t addr);
227
228// Watchpoint functions
229nub_bool_t DNBWatchpointSet(nub_process_t pid, nub_addr_t addr, nub_size_t size,
230 uint32_t watch_flags, nub_bool_t hardware);
231nub_bool_t DNBWatchpointClear(nub_process_t pid, nub_addr_t addr);
232uint32_t DNBWatchpointGetNumSupportedHWP(nub_process_t pid);
233
234uint32_t DNBGetRegisterCPUType();
235const DNBRegisterSetInfo *DNBGetRegisterSetInfo(nub_size_t *num_reg_sets);
236nub_bool_t DNBGetRegisterInfoByName(const char *reg_name,
237 DNBRegisterInfo *info);
238
239// Other static nub information calls.
240const char *DNBStateAsString(nub_state_t state);
241nub_bool_t DNBResolveExecutablePath(const char *path, char *resolved_path,
242 size_t resolved_path_size);
243bool DNBGetOSVersionNumbers(uint64_t *major, uint64_t *minor, uint64_t *patch);
244/// \return the iOSSupportVersion of the host OS.
245std::string DNBGetMacCatalystVersionString();
246
247/// \return true if debugserver is running in translation
248/// (is an x86_64 process on arm64)
249bool DNBDebugserverIsTranslated();
250
251bool DNBGetAddressingBits(uint32_t &addressing_bits);
252
253nub_process_t DNBGetParentProcessID(nub_process_t child_pid);
254
255bool DNBProcessIsBeingDebugged(nub_process_t pid);
256
257#endif
258

source code of lldb/tools/debugserver/source/DNB.h