1 | //===-- MachProcess.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 6/15/07. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHPROCESS_H |
14 | #define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHPROCESS_H |
15 | |
16 | #include <CoreFoundation/CoreFoundation.h> |
17 | #include <mach-o/loader.h> |
18 | #include <mach/mach.h> |
19 | #include <optional> |
20 | #include <pthread.h> |
21 | #include <sys/signal.h> |
22 | #include <uuid/uuid.h> |
23 | #include <vector> |
24 | |
25 | #include "DNBBreakpoint.h" |
26 | #include "DNBDefs.h" |
27 | #include "DNBError.h" |
28 | #include "DNBThreadResumeActions.h" |
29 | #include "Genealogy.h" |
30 | #include "JSONGenerator.h" |
31 | #include "MachException.h" |
32 | #include "MachTask.h" |
33 | #include "MachThreadList.h" |
34 | #include "MachVMMemory.h" |
35 | #include "PThreadCondition.h" |
36 | #include "PThreadEvent.h" |
37 | #include "RNBContext.h" |
38 | #include "ThreadInfo.h" |
39 | |
40 | class DNBThreadResumeActions; |
41 | |
42 | class MachProcess { |
43 | public: |
44 | // Constructors and Destructors |
45 | MachProcess(); |
46 | ~MachProcess(); |
47 | |
48 | // A structure that can hold everything debugserver needs to know from |
49 | // a binary's Mach-O header / load commands. |
50 | |
51 | struct mach_o_segment { |
52 | std::string name; |
53 | uint64_t vmaddr; |
54 | uint64_t vmsize; |
55 | uint64_t fileoff; |
56 | uint64_t filesize; |
57 | uint64_t maxprot; |
58 | uint64_t initprot; |
59 | uint64_t nsects; |
60 | uint64_t flags; |
61 | }; |
62 | |
63 | struct mach_o_information { |
64 | struct ; |
65 | std::vector<struct mach_o_segment> segments; |
66 | uuid_t uuid; |
67 | std::string min_version_os_name; |
68 | std::string min_version_os_version; |
69 | }; |
70 | |
71 | struct binary_image_information { |
72 | std::string filename; |
73 | uint64_t load_address; |
74 | struct mach_o_information macho_info; |
75 | bool ; |
76 | |
77 | binary_image_information() |
78 | : filename(), load_address(INVALID_NUB_ADDRESS), |
79 | is_valid_mach_header(false) {} |
80 | }; |
81 | |
82 | // Child process control |
83 | pid_t AttachForDebug(pid_t pid, |
84 | const RNBContext::IgnoredExceptions &ignored_exceptions, |
85 | char *err_str, |
86 | size_t err_len); |
87 | pid_t LaunchForDebug(const char *path, char const *argv[], char const *envp[], |
88 | const char *working_directory, const char *stdin_path, |
89 | const char *stdout_path, const char *stderr_path, |
90 | bool no_stdio, nub_launch_flavor_t launch_flavor, |
91 | int disable_aslr, const char *event_data, |
92 | const RNBContext::IgnoredExceptions &ignored_exceptions, |
93 | DNBError &err); |
94 | |
95 | static uint32_t GetCPUTypeForLocalProcess(pid_t pid); |
96 | static pid_t ForkChildForPTraceDebugging(const char *path, char const *argv[], |
97 | char const *envp[], |
98 | MachProcess *process, DNBError &err); |
99 | static pid_t PosixSpawnChildForPTraceDebugging( |
100 | const char *path, cpu_type_t cpu_type, cpu_subtype_t cpu_subtype, |
101 | char const *argv[], char const *envp[], const char *working_directory, |
102 | const char *stdin_path, const char *stdout_path, const char *stderr_path, |
103 | bool no_stdio, MachProcess *process, int disable_aslr, DNBError &err); |
104 | nub_addr_t GetDYLDAllImageInfosAddress(); |
105 | std::optional<std::pair<cpu_type_t, cpu_subtype_t>> |
106 | GetMainBinaryCPUTypes(nub_process_t pid); |
107 | static const void *PrepareForAttach(const char *path, |
108 | nub_launch_flavor_t launch_flavor, |
109 | bool waitfor, DNBError &err_str); |
110 | static void CleanupAfterAttach(const void *attach_token, |
111 | nub_launch_flavor_t launch_flavor, |
112 | bool success, DNBError &err_str); |
113 | static nub_process_t CheckForProcess(const void *attach_token, |
114 | nub_launch_flavor_t launch_flavor); |
115 | #if defined(WITH_BKS) || defined(WITH_FBS) |
116 | pid_t BoardServiceLaunchForDebug(const char *app_bundle_path, |
117 | char const *argv[], char const *envp[], |
118 | bool no_stdio, bool disable_aslr, |
119 | const char *event_data, |
120 | const RNBContext::IgnoredExceptions &ignored_exceptions, |
121 | DNBError &launch_err); |
122 | pid_t BoardServiceForkChildForPTraceDebugging( |
123 | const char *path, char const *argv[], char const *envp[], bool no_stdio, |
124 | bool disable_aslr, const char *event_data, DNBError &launch_err); |
125 | bool BoardServiceSendEvent(const char *event, DNBError &error); |
126 | #endif |
127 | static bool GetOSVersionNumbers(uint64_t *major, uint64_t *minor, |
128 | uint64_t *patch); |
129 | static std::string GetMacCatalystVersionString(); |
130 | |
131 | static nub_process_t GetParentProcessID(nub_process_t child_pid); |
132 | |
133 | static bool ProcessIsBeingDebugged(nub_process_t pid); |
134 | |
135 | #ifdef WITH_BKS |
136 | static void BKSCleanupAfterAttach(const void *attach_token, |
137 | DNBError &err_str); |
138 | #endif // WITH_BKS |
139 | #ifdef WITH_FBS |
140 | static void FBSCleanupAfterAttach(const void *attach_token, |
141 | DNBError &err_str); |
142 | #endif // WITH_FBS |
143 | #ifdef WITH_SPRINGBOARD |
144 | pid_t SBLaunchForDebug(const char *app_bundle_path, char const *argv[], |
145 | char const *envp[], bool no_stdio, bool disable_aslr, |
146 | bool unmask_signals, DNBError &launch_err); |
147 | static pid_t SBForkChildForPTraceDebugging(const char *path, |
148 | char const *argv[], |
149 | char const *envp[], bool no_stdio, |
150 | MachProcess *process, |
151 | DNBError &launch_err); |
152 | #endif // WITH_SPRINGBOARD |
153 | nub_addr_t LookupSymbol(const char *name, const char *shlib); |
154 | void SetNameToAddressCallback(DNBCallbackNameToAddress callback, |
155 | void *baton) { |
156 | m_name_to_addr_callback = callback; |
157 | m_name_to_addr_baton = baton; |
158 | } |
159 | void |
160 | SetSharedLibraryInfoCallback(DNBCallbackCopyExecutableImageInfos callback, |
161 | void *baton) { |
162 | m_image_infos_callback = callback; |
163 | m_image_infos_baton = baton; |
164 | } |
165 | |
166 | bool Resume(const DNBThreadResumeActions &thread_actions); |
167 | bool Signal(int signal, const struct timespec *timeout_abstime = NULL); |
168 | bool Interrupt(); |
169 | bool SendEvent(const char *event, DNBError &send_err); |
170 | bool Kill(const struct timespec *timeout_abstime = NULL); |
171 | bool Detach(); |
172 | nub_size_t ReadMemory(nub_addr_t addr, nub_size_t size, void *buf); |
173 | nub_size_t WriteMemory(nub_addr_t addr, nub_size_t size, const void *buf); |
174 | |
175 | // Path and arg accessors |
176 | const char *Path() const { return m_path.c_str(); } |
177 | size_t ArgumentCount() const { return m_args.size(); } |
178 | const char *ArgumentAtIndex(size_t arg_idx) const { |
179 | if (arg_idx < m_args.size()) |
180 | return m_args[arg_idx].c_str(); |
181 | return NULL; |
182 | } |
183 | |
184 | // Breakpoint functions |
185 | DNBBreakpoint *CreateBreakpoint(nub_addr_t addr, nub_size_t length, |
186 | bool hardware); |
187 | bool DisableBreakpoint(nub_addr_t addr, bool remove); |
188 | void DisableAllBreakpoints(bool remove); |
189 | bool EnableBreakpoint(nub_addr_t addr); |
190 | DNBBreakpointList &Breakpoints() { return m_breakpoints; } |
191 | const DNBBreakpointList &Breakpoints() const { return m_breakpoints; } |
192 | |
193 | // Watchpoint functions |
194 | DNBBreakpoint *CreateWatchpoint(nub_addr_t addr, nub_size_t length, |
195 | uint32_t watch_type, bool hardware); |
196 | bool DisableWatchpoint(nub_addr_t addr, bool remove); |
197 | void DisableAllWatchpoints(bool remove); |
198 | bool EnableWatchpoint(nub_addr_t addr); |
199 | uint32_t GetNumSupportedHardwareWatchpoints() const; |
200 | DNBBreakpointList &Watchpoints() { return m_watchpoints; } |
201 | const DNBBreakpointList &Watchpoints() const { return m_watchpoints; } |
202 | |
203 | // Exception thread functions |
204 | bool StartSTDIOThread(); |
205 | static void *STDIOThread(void *arg); |
206 | void ExceptionMessageReceived(const MachException::Message &exceptionMessage); |
207 | task_t ExceptionMessageBundleComplete(); |
208 | void SharedLibrariesUpdated(); |
209 | nub_size_t CopyImageInfos(struct DNBExecutableImageInfo **image_infos, |
210 | bool only_changed); |
211 | |
212 | // Profile functions |
213 | void SetEnableAsyncProfiling(bool enable, uint64_t internal_usec, |
214 | DNBProfileDataScanType scan_type); |
215 | bool IsProfilingEnabled() { return m_profile_enabled; } |
216 | useconds_t ProfileInterval() { return m_profile_interval_usec; } |
217 | bool StartProfileThread(); |
218 | static void *ProfileThread(void *arg); |
219 | void SignalAsyncProfileData(const char *info); |
220 | size_t GetAsyncProfileData(char *buf, size_t buf_size); |
221 | |
222 | // Accessors |
223 | pid_t ProcessID() const { return m_pid; } |
224 | bool ProcessIDIsValid() const { return m_pid > 0; } |
225 | pid_t SetProcessID(pid_t pid); |
226 | MachTask &Task() { return m_task; } |
227 | const MachTask &Task() const { return m_task; } |
228 | |
229 | PThreadEvent &Events() { return m_events; } |
230 | const DNBRegisterSetInfo *GetRegisterSetInfo(nub_thread_t tid, |
231 | nub_size_t *num_reg_sets) const; |
232 | bool GetRegisterValue(nub_thread_t tid, uint32_t set, uint32_t reg, |
233 | DNBRegisterValue *reg_value) const; |
234 | bool SetRegisterValue(nub_thread_t tid, uint32_t set, uint32_t reg, |
235 | const DNBRegisterValue *value) const; |
236 | nub_bool_t SyncThreadState(nub_thread_t tid); |
237 | const char *ThreadGetName(nub_thread_t tid); |
238 | nub_state_t ThreadGetState(nub_thread_t tid); |
239 | ThreadInfo::QoS GetRequestedQoS(nub_thread_t tid, nub_addr_t tsd, |
240 | uint64_t dti_qos_class_index); |
241 | nub_addr_t GetPThreadT(nub_thread_t tid); |
242 | nub_addr_t GetDispatchQueueT(nub_thread_t tid); |
243 | nub_addr_t |
244 | GetTSDAddressForThread(nub_thread_t tid, |
245 | uint64_t plo_pthread_tsd_base_address_offset, |
246 | uint64_t plo_pthread_tsd_base_offset, |
247 | uint64_t plo_pthread_tsd_entry_size); |
248 | |
249 | struct DeploymentInfo { |
250 | DeploymentInfo() = default; |
251 | operator bool() { return platform > 0; } |
252 | /// The Mach-O platform type; |
253 | unsigned char platform = 0; |
254 | uint32_t major_version = 0; |
255 | uint32_t minor_version = 0; |
256 | uint32_t patch_version = 0; |
257 | }; |
258 | DeploymentInfo GetDeploymentInfo(const struct load_command &, |
259 | uint64_t load_command_address, |
260 | bool is_executable); |
261 | static std::optional<std::string> GetPlatformString(unsigned char platform); |
262 | bool GetMachOInformationFromMemory(uint32_t platform, |
263 | nub_addr_t , |
264 | int wordsize, |
265 | struct mach_o_information &inf); |
266 | JSONGenerator::ObjectSP FormatDynamicLibrariesIntoJSON( |
267 | const std::vector<struct binary_image_information> &image_infos, |
268 | bool report_load_commands); |
269 | uint32_t GetPlatform(); |
270 | /// Get the runtime platform from DYLD via SPI. |
271 | uint32_t GetProcessPlatformViaDYLDSPI(); |
272 | /// Use the dyld SPI present in macOS 10.12, iOS 10, tvOS 10, |
273 | /// watchOS 3 and newer to get the load address, uuid, and filenames |
274 | /// of all the libraries. This only fills in those three fields in |
275 | /// the 'struct binary_image_information' - call |
276 | /// GetMachOInformationFromMemory to fill in the mach-o header/load |
277 | /// command details. |
278 | void GetAllLoadedBinariesViaDYLDSPI( |
279 | std::vector<struct binary_image_information> &image_infos); |
280 | JSONGenerator::ObjectSP |
281 | GetLibrariesInfoForAddresses(nub_process_t pid, |
282 | std::vector<uint64_t> &macho_addresses); |
283 | JSONGenerator::ObjectSP |
284 | GetAllLoadedLibrariesInfos(nub_process_t pid, |
285 | bool fetch_report_load_commands); |
286 | JSONGenerator::ObjectSP GetSharedCacheInfo(nub_process_t pid); |
287 | |
288 | nub_size_t GetNumThreads() const; |
289 | nub_thread_t GetThreadAtIndex(nub_size_t thread_idx) const; |
290 | nub_thread_t GetCurrentThread(); |
291 | nub_thread_t GetCurrentThreadMachPort(); |
292 | nub_thread_t SetCurrentThread(nub_thread_t tid); |
293 | MachThreadList &GetThreadList() { return m_thread_list; } |
294 | bool GetThreadStoppedReason(nub_thread_t tid, |
295 | struct DNBThreadStopInfo *stop_info); |
296 | void DumpThreadStoppedReason(nub_thread_t tid) const; |
297 | const char *GetThreadInfo(nub_thread_t tid) const; |
298 | |
299 | nub_thread_t GetThreadIDForMachPortNumber(thread_t mach_port_number) const; |
300 | |
301 | uint32_t GetCPUType(); |
302 | nub_state_t GetState(); |
303 | void SetState(nub_state_t state); |
304 | bool IsRunning(nub_state_t state) { |
305 | return state == eStateRunning || IsStepping(state); |
306 | } |
307 | bool IsStepping(nub_state_t state) { return state == eStateStepping; } |
308 | bool CanResume(nub_state_t state) { return state == eStateStopped; } |
309 | |
310 | bool GetExitStatus(int *status) { |
311 | if (GetState() == eStateExited) { |
312 | if (status) |
313 | *status = m_exit_status; |
314 | return true; |
315 | } |
316 | return false; |
317 | } |
318 | void SetExitStatus(int status) { |
319 | m_exit_status = status; |
320 | SetState(eStateExited); |
321 | } |
322 | const char *GetExitInfo() { return m_exit_info.c_str(); } |
323 | |
324 | void SetExitInfo(const char *info); |
325 | |
326 | uint32_t StopCount() const { return m_stop_count; } |
327 | void SetChildFileDescriptors(int stdin_fileno, int stdout_fileno, |
328 | int stderr_fileno) { |
329 | m_child_stdin = stdin_fileno; |
330 | m_child_stdout = stdout_fileno; |
331 | m_child_stderr = stderr_fileno; |
332 | } |
333 | |
334 | int GetStdinFileDescriptor() const { return m_child_stdin; } |
335 | int GetStdoutFileDescriptor() const { return m_child_stdout; } |
336 | int GetStderrFileDescriptor() const { return m_child_stderr; } |
337 | void AppendSTDOUT(char *s, size_t len); |
338 | size_t GetAvailableSTDOUT(char *buf, size_t buf_size); |
339 | size_t GetAvailableSTDERR(char *buf, size_t buf_size); |
340 | void CloseChildFileDescriptors() { |
341 | if (m_child_stdin >= 0) { |
342 | ::close(fd: m_child_stdin); |
343 | m_child_stdin = -1; |
344 | } |
345 | if (m_child_stdout >= 0) { |
346 | ::close(fd: m_child_stdout); |
347 | m_child_stdout = -1; |
348 | } |
349 | if (m_child_stderr >= 0) { |
350 | ::close(fd: m_child_stderr); |
351 | m_child_stderr = -1; |
352 | } |
353 | } |
354 | |
355 | void CalculateBoardStatus(); |
356 | |
357 | bool ProcessUsingBackBoard(); |
358 | |
359 | bool ProcessUsingFrontBoard(); |
360 | |
361 | // Size of addresses in the inferior process (4 or 8). |
362 | int GetInferiorAddrSize(pid_t pid); |
363 | |
364 | Genealogy::ThreadActivitySP GetGenealogyInfoForThread(nub_thread_t tid, |
365 | bool &timed_out); |
366 | |
367 | Genealogy::ProcessExecutableInfoSP GetGenealogyImageInfo(size_t idx); |
368 | |
369 | DNBProfileDataScanType GetProfileScanType() { return m_profile_scan_type; } |
370 | |
371 | JSONGenerator::ObjectSP GetDyldProcessState(); |
372 | |
373 | private: |
374 | enum { |
375 | eMachProcessFlagsNone = 0, |
376 | eMachProcessFlagsAttached = (1 << 0), |
377 | eMachProcessFlagsUsingBKS = (1 << 2), // only read via ProcessUsingBackBoard() |
378 | eMachProcessFlagsUsingFBS = (1 << 3), // only read via ProcessUsingFrontBoard() |
379 | eMachProcessFlagsBoardCalculated = (1 << 4) |
380 | }; |
381 | |
382 | enum { |
383 | eMachProcessProfileNone = 0, |
384 | eMachProcessProfileCancel = (1 << 0) |
385 | }; |
386 | |
387 | void Clear(bool detaching = false); |
388 | void ReplyToAllExceptions(); |
389 | void PrivateResume(); |
390 | void StopProfileThread(); |
391 | |
392 | void RefineWatchpointStopInfo(nub_thread_t tid, |
393 | struct DNBThreadStopInfo *stop_info); |
394 | |
395 | uint32_t Flags() const { return m_flags; } |
396 | nub_state_t DoSIGSTOP(bool clear_bps_and_wps, bool allow_running, |
397 | uint32_t *thread_idx_ptr); |
398 | |
399 | pid_t m_pid; // Process ID of child process |
400 | cpu_type_t m_cpu_type; // The CPU type of this process |
401 | uint32_t m_platform; // The platform of this process |
402 | int m_child_stdin; |
403 | int m_child_stdout; |
404 | int m_child_stderr; |
405 | std::string m_path; // A path to the executable if we have one |
406 | std::vector<std::string> |
407 | m_args; // The arguments with which the process was lauched |
408 | int m_exit_status; // The exit status for the process |
409 | std::string m_exit_info; // Any extra info that we may have about the exit |
410 | MachTask m_task; // The mach task for this process |
411 | uint32_t m_flags; // Process specific flags (see eMachProcessFlags enums) |
412 | uint32_t m_stop_count; // A count of many times have we stopped |
413 | pthread_t m_stdio_thread; // Thread ID for the thread that watches for child |
414 | // process stdio |
415 | std::recursive_mutex m_stdio_mutex; // Multithreaded protection for stdio |
416 | std::string m_stdout_data; |
417 | |
418 | bool m_profile_enabled; // A flag to indicate if profiling is enabled |
419 | useconds_t m_profile_interval_usec; // If enable, the profiling interval in |
420 | // microseconds |
421 | DNBProfileDataScanType |
422 | m_profile_scan_type; // Indicates what needs to be profiled |
423 | pthread_t |
424 | m_profile_thread; // Thread ID for the thread that profiles the inferior |
425 | std::recursive_mutex |
426 | m_profile_data_mutex; // Multithreaded protection for profile info data |
427 | std::vector<std::string> |
428 | m_profile_data; // Profile data, must be protected by m_profile_data_mutex |
429 | PThreadEvent m_profile_events; // Used for the profile thread cancellable wait |
430 | DNBThreadResumeActions m_thread_actions; // The thread actions for the current |
431 | // MachProcess::Resume() call |
432 | MachException::Message::collection m_exception_messages; // A collection of |
433 | // exception messages |
434 | // caught when |
435 | // listening to the |
436 | // exception port |
437 | std::recursive_mutex |
438 | m_exception_and_signal_mutex; // Multithreaded protection for |
439 | // exceptions and signals. |
440 | |
441 | MachThreadList m_thread_list; // A list of threads that is maintained/updated |
442 | // after each stop |
443 | Genealogy m_activities; // A list of activities that is updated after every |
444 | // stop lazily |
445 | nub_state_t m_state; // The state of our process |
446 | std::recursive_mutex m_state_mutex; // Multithreaded protection for m_state |
447 | PThreadEvent m_events; // Process related events in the child processes |
448 | // lifetime can be waited upon |
449 | PThreadEvent m_private_events; // Used to coordinate running and stopping the |
450 | // process without affecting m_events |
451 | DNBBreakpointList m_breakpoints; // Breakpoint list for this process |
452 | DNBBreakpointList m_watchpoints; // Watchpoint list for this process |
453 | DNBCallbackNameToAddress m_name_to_addr_callback; |
454 | void *m_name_to_addr_baton; |
455 | DNBCallbackCopyExecutableImageInfos m_image_infos_callback; |
456 | void *m_image_infos_baton; |
457 | std::string |
458 | m_bundle_id; // If we are a SB or BKS process, this will be our bundle ID. |
459 | int m_sent_interrupt_signo; // When we call MachProcess::Interrupt(), we want |
460 | // to send a single signal |
461 | // to the inferior and only send the signal if we aren't already stopped. |
462 | // If we end up sending a signal to stop the process we store it until we |
463 | // receive an exception with this signal. This helps us to verify we got |
464 | // the signal that interrupted the process. We might stop due to another |
465 | // reason after an interrupt signal is sent, so this helps us ensure that |
466 | // we don't report a spurious stop on the next resume. |
467 | int m_auto_resume_signo; // If we resume the process and still haven't |
468 | // received our interrupt signal |
469 | // acknowledgement, we will shortly after the next resume. We store the |
470 | // interrupt signal in this variable so when we get the interrupt signal |
471 | // as the sole reason for the process being stopped, we can auto resume |
472 | // the process. |
473 | bool m_did_exec; |
474 | |
475 | void *(*m_dyld_process_info_create)(task_t task, uint64_t timestamp, |
476 | kern_return_t *kernelError); |
477 | void (*m_dyld_process_info_for_each_image)( |
478 | void *info, void (^callback)(uint64_t , |
479 | const uuid_t uuid, const char *path)); |
480 | void (*m_dyld_process_info_release)(void *info); |
481 | void (*m_dyld_process_info_get_cache)(void *info, void *cacheInfo); |
482 | uint32_t (*m_dyld_process_info_get_platform)(void *info); |
483 | void (*m_dyld_process_info_get_state)(void *info, void *stateInfo); |
484 | }; |
485 | |
486 | #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHPROCESS_H |
487 | |