LLDB mainline
Host.mm
Go to the documentation of this file.
1//===-- Host.mm -------------------------------------------------*- 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#include "lldb/Host/Host.h"
11
12#include <AvailabilityMacros.h>
13#include <TargetConditionals.h>
14
15#if TARGET_OS_OSX
16#define __XPC_PRIVATE_H__
17#include <xpc/xpc.h>
18
19#define LaunchUsingXPCRightName "com.apple.lldb.RootDebuggingXPCService"
20
21// These XPC messaging keys are used for communication between Host.mm and the
22// XPC service.
23#define LauncherXPCServiceAuthKey "auth-key"
24#define LauncherXPCServiceArgPrefxKey "arg"
25#define LauncherXPCServiceEnvPrefxKey "env"
26#define LauncherXPCServiceCPUTypeKey "cpuType"
27#define LauncherXPCServicePosixspawnFlagsKey "posixspawnFlags"
28#define LauncherXPCServiceStdInPathKeyKey "stdInPath"
29#define LauncherXPCServiceStdOutPathKeyKey "stdOutPath"
30#define LauncherXPCServiceStdErrPathKeyKey "stdErrPath"
31#define LauncherXPCServiceChildPIDKey "childPID"
32#define LauncherXPCServiceErrorTypeKey "errorType"
33#define LauncherXPCServiceCodeTypeKey "errorCode"
34
35#include <bsm/audit.h>
36#include <bsm/audit_session.h>
37#endif
38
39#include "llvm/TargetParser/Host.h"
40
41#include <asl.h>
42#include <cassert>
43#include <cerrno>
44#include <crt_externs.h>
45#include <cstdio>
46#include <cstdlib>
47#include <dlfcn.h>
48#include <grp.h>
49#include <libproc.h>
50#include <pwd.h>
51#include <spawn.h>
52#include <sys/proc.h>
53#include <sys/stat.h>
54#include <sys/sysctl.h>
55#include <sys/types.h>
56#include <unistd.h>
57
60#include "lldb/Host/HostInfo.h"
67#include "lldb/Utility/Endian.h"
69#include "lldb/Utility/Log.h"
74#include "lldb/lldb-defines.h"
75
76#include "llvm/ADT/ScopeExit.h"
77#include "llvm/Support/Errno.h"
78#include "llvm/Support/Error.h"
79#include "llvm/Support/ErrorExtras.h"
80#include "llvm/Support/FileSystem.h"
81
82#include "../cfcpp/CFCBundle.h"
86#include "../cfcpp/CFCString.h"
87
88#include <objc/objc-auto.h>
89#include <os/log.h>
90
91#include <CoreFoundation/CoreFoundation.h>
92#include <Foundation/Foundation.h>
93
94#ifndef _POSIX_SPAWN_DISABLE_ASLR
95#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
96#endif
97
98extern "C" {
99int __pthread_chdir(const char *path);
100int __pthread_fchdir(int fildes);
101}
102
103using namespace lldb;
104using namespace lldb_private;
105
106static os_log_t g_os_log;
107static std::once_flag g_os_log_once;
108
109void Host::SystemLog(Severity severity, llvm::StringRef message) {
110 if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
111 std::call_once(g_os_log_once, []() {
112 g_os_log = os_log_create("com.apple.dt.lldb", "lldb");
113 });
114 switch (severity) {
117 os_log(g_os_log, "%{public}s", message.str().c_str());
118 break;
120 os_log_error(g_os_log, "%{public}s", message.str().c_str());
121 break;
122 }
123 } else {
124 llvm::errs() << message;
125 }
126}
127
128bool Host::GetBundleDirectory(const FileSpec &file,
129 FileSpec &bundle_directory) {
130#if defined(__APPLE__)
131 if (FileSystem::Instance().IsDirectory(file)) {
132 char path[PATH_MAX];
133 if (file.GetPath(path, sizeof(path))) {
134 CFCBundle bundle(path);
135 if (bundle.GetPath(path, sizeof(path))) {
136 bundle_directory.SetFile(path, FileSpec::Style::native);
137 return true;
138 }
139 }
140 }
141#endif
142 bundle_directory.Clear();
143 return false;
144}
145
147#if defined(__APPLE__)
148 if (FileSystem::Instance().IsDirectory(file)) {
149 char path[PATH_MAX];
150 if (file.GetPath(path, sizeof(path))) {
151 CFCBundle bundle(path);
152 CFCReleaser<CFURLRef> url(bundle.CopyExecutableURL());
153 if (url.get()) {
154 if (::CFURLGetFileSystemRepresentation(url.get(), YES, (UInt8 *)path,
155 sizeof(path))) {
156 file.SetFile(path, FileSpec::Style::native);
157 return true;
158 }
159 }
160 }
161 }
162#endif
163 return false;
164}
165
166#if TARGET_OS_OSX
167
168static void *AcceptPIDFromInferior(const char *connect_url) {
169 ConnectionFileDescriptor file_conn;
171 if (file_conn.Connect(connect_url, &error) == eConnectionStatusSuccess) {
172 char pid_str[256];
173 ::memset(pid_str, 0, sizeof(pid_str));
174 ConnectionStatus status;
175 const size_t pid_str_len = file_conn.Read(
176 pid_str, sizeof(pid_str), std::chrono::seconds(0), status, NULL);
177 if (pid_str_len > 0) {
178 int pid = atoi(pid_str);
179 return (void *)(intptr_t)pid;
180 }
181 }
182 return NULL;
183}
184
185const char *applscript_in_new_tty = "tell application \"Terminal\"\n"
186 " activate\n"
187 " do script \"/bin/bash -c '%s';exit\"\n"
188 "end tell\n";
189
190const char *applscript_in_existing_tty = "\
191set the_shell_script to \"/bin/bash -c '%s';exit\"\n\
192tell application \"Terminal\"\n\
193 repeat with the_window in (get windows)\n\
194 repeat with the_tab in tabs of the_window\n\
195 set the_tty to tty in the_tab\n\
196 if the_tty contains \"%s\" then\n\
197 if the_tab is not busy then\n\
198 set selected of the_tab to true\n\
199 set frontmost of the_window to true\n\
200 do script the_shell_script in the_tab\n\
201 return\n\
202 end if\n\
203 end if\n\
204 end repeat\n\
205 end repeat\n\
206 do script the_shell_script\n\
207end tell\n";
208
209static Status
210LaunchInNewTerminalWithAppleScript(const char *exe_path,
211 ProcessLaunchInfo &launch_info) {
213 char unix_socket_name[PATH_MAX] = "/tmp/XXXXXX";
214 if (::mktemp(unix_socket_name) == NULL) {
216 "failed to make temporary path for a unix socket");
217 return error;
218 }
219
220 StreamString command;
221 FileSpec darwin_debug_file_spec = HostInfo::GetSupportExeDir();
222 if (!darwin_debug_file_spec) {
223 error =
224 Status::FromErrorString("can't locate the 'darwin-debug' executable");
225 return error;
226 }
227
228 darwin_debug_file_spec.SetFilename("darwin-debug");
229
230 if (!FileSystem::Instance().Exists(darwin_debug_file_spec)) {
232 "the 'darwin-debug' executable doesn't exists at '%s'",
233 darwin_debug_file_spec.GetPath().c_str());
234 return error;
235 }
236
237 char launcher_path[PATH_MAX];
238 darwin_debug_file_spec.GetPath(launcher_path, sizeof(launcher_path));
239
240 const ArchSpec &arch_spec = launch_info.GetArchitecture();
241 // Only set the architecture if it is valid and if it isn't Haswell (x86_64h).
242 if (arch_spec.IsValid() &&
244 command.Printf("arch -arch %s ", arch_spec.GetArchitectureName());
245
246 command.Printf(R"(\"%s\" --unix-socket=%s)", launcher_path, unix_socket_name);
247
248 if (arch_spec.IsValid())
249 command.Printf(" --arch=%s", arch_spec.GetArchitectureName());
250
251 FileSpec working_dir{launch_info.GetWorkingDirectory()};
252 if (working_dir)
253 command.Printf(R"( --working-dir \"%s\")", working_dir.GetPath().c_str());
254 else {
255 char cwd[PATH_MAX];
256 if (getcwd(cwd, PATH_MAX))
257 command.Printf(R"( --working-dir \"%s\")", cwd);
258 }
259
260 if (launch_info.GetFlags().Test(eLaunchFlagDisableASLR))
261 command.PutCString(" --disable-aslr");
262
263 // We are launching on this host in a terminal. So compare the environment on
264 // the host to what is supplied in the launch_info. Any items that aren't in
265 // the host environment need to be sent to darwin-debug. If we send all
266 // environment entries, we might blow the max command line length, so we only
267 // send user modified entries.
269
270 for (const auto &KV : launch_info.GetEnvironment()) {
271 auto host_entry = host_env.find(KV.first());
272 if (host_entry == host_env.end() || host_entry->second != KV.second)
273 command.Format(R"( --env=\"{0}\")", Environment::compose(KV));
274 }
275
276 command.PutCString(" -- ");
277
278 const char **argv = launch_info.GetArguments().GetConstArgumentVector();
279 if (argv) {
280 for (size_t i = 0; argv[i] != NULL; ++i) {
281 if (i == 0)
282 command.Printf(R"( \"%s\")", exe_path);
283 else
284 command.Printf(R"( \"%s\")", argv[i]);
285 }
286 } else {
287 command.Printf(R"( \"%s\")", exe_path);
288 }
289 command.PutCString(" ; echo Process exited with status $?");
290 if (launch_info.GetFlags().Test(lldb::eLaunchFlagCloseTTYOnExit))
291 command.PutCString(" ; exit");
292
293 StreamString applescript_source;
294
295 applescript_source.Printf(applscript_in_new_tty,
296 command.GetString().str().c_str());
297
298 NSAppleScript *applescript = [[NSAppleScript alloc]
299 initWithSource:[NSString stringWithCString:applescript_source.GetString()
300 .str()
301 .c_str()
302 encoding:NSUTF8StringEncoding]];
303
305
306 Status lldb_error;
307 // Sleep and wait a bit for debugserver to start to listen...
308 ConnectionFileDescriptor file_conn;
309 char connect_url[128];
310 ::snprintf(connect_url, sizeof(connect_url), "unix-accept://%s",
311 unix_socket_name);
312
313 // Spawn a new thread to accept incoming connection on the connect_url
314 // so we can grab the pid from the inferior. We have to do this because we
315 // are sending an AppleScript that will launch a process in Terminal.app,
316 // in a shell and the shell will fork/exec a couple of times before we get
317 // to the process that we wanted to launch. So when our process actually
318 // gets launched, we will handshake with it and get the process ID for it.
319 llvm::Expected<HostThread> accept_thread = ThreadLauncher::LaunchThread(
320 unix_socket_name, [&] { return AcceptPIDFromInferior(connect_url); });
321
322 if (!accept_thread)
323 return Status::FromError(accept_thread.takeError());
324
325 [applescript executeAndReturnError:nil];
326
327 thread_result_t accept_thread_result = NULL;
328 lldb_error = accept_thread->Join(&accept_thread_result);
329 if (lldb_error.Success() && accept_thread_result) {
330 pid = (intptr_t)accept_thread_result;
331 }
332
333 llvm::sys::fs::remove(unix_socket_name);
334 [applescript release];
335 if (pid != LLDB_INVALID_PROCESS_ID)
336 launch_info.SetProcessID(pid);
337 return error;
338}
339
340#endif // TARGET_OS_OSX
341
342llvm::Error Host::OpenFileInExternalEditor(llvm::StringRef editor,
343 const FileSpec &file_spec,
344 uint32_t line_no) {
345#if !TARGET_OS_OSX
346 return llvm::errorCodeToError(
347 std::error_code(ENOTSUP, std::system_category()));
348#else // !TARGET_OS_OSX
349 Log *log = GetLog(LLDBLog::Host);
350
351 const std::string file_path = file_spec.GetPath();
352
353 LLDB_LOG(log, "Sending {0}:{1} to external editor",
354 file_path.empty() ? "<invalid>" : file_path, line_no);
355
356 if (file_path.empty())
357 return llvm::createStringError(llvm::inconvertibleErrorCode(),
358 "no file specified");
359
360 CFCString file_cfstr(file_path.c_str(), kCFStringEncodingUTF8);
361 CFCReleaser<CFURLRef> file_URL = ::CFURLCreateWithFileSystemPath(
362 /*allocator=*/NULL,
363 /*filePath*/ file_cfstr.get(),
364 /*pathStyle=*/kCFURLPOSIXPathStyle,
365 /*isDirectory=*/false);
366
367 if (!file_URL.get())
368 return llvm::createStringError(
369 llvm::inconvertibleErrorCode(),
370 llvm::formatv("could not create CFURL from path \"{0}\"", file_path));
371
372 // Create a new Apple Event descriptor.
373 typedef struct {
374 int16_t reserved0; // must be zero
375 int16_t fLineNumber;
376 int32_t fSelStart;
377 int32_t fSelEnd;
378 uint32_t reserved1; // must be zero
379 uint32_t reserved2; // must be zero
380 } BabelAESelInfo;
381
382 // We attach this to an 'odoc' event to specify a particular selection.
383 BabelAESelInfo file_and_line_info = {
384 0, // reserved0
385 (int16_t)(line_no - 1), // fLineNumber (zero based line number)
386 1, // fSelStart
387 1024, // fSelEnd
388 0, // reserved1
389 0 // reserved2
390 };
391
392 AEKeyDesc file_and_line_desc;
393 file_and_line_desc.descKey = keyAEPosition;
394 long error = ::AECreateDesc(/*typeCode=*/typeUTF8Text,
395 /*dataPtr=*/&file_and_line_info,
396 /*dataSize=*/sizeof(file_and_line_info),
397 /*result=*/&(file_and_line_desc.descContent));
398
399 if (error != noErr)
400 return llvm::createStringError(
401 llvm::inconvertibleErrorCode(),
402 llvm::formatv("creating Apple Event descriptor failed: error {0}",
403 error));
404
405 // Deallocate the descriptor on exit.
406 llvm::scope_exit on_exit(
407 [&]() { AEDisposeDesc(&(file_and_line_desc.descContent)); });
408
409 if (editor.empty()) {
410 if (const char *lldb_external_editor = ::getenv("LLDB_EXTERNAL_EDITOR"))
411 editor = lldb_external_editor;
412 }
413
414 std::optional<FSRef> app_fsref;
415 if (!editor.empty()) {
416 LLDB_LOG(log, "Looking for external editor: {0}", editor);
417
418 app_fsref.emplace();
419 CFCString editor_name(editor.data(), kCFStringEncodingUTF8);
420 long app_error = ::LSFindApplicationForInfo(
421 /*inCreator=*/kLSUnknownCreator, /*inBundleID=*/NULL,
422 /*inName=*/editor_name.get(), /*outAppRef=*/&(*app_fsref),
423 /*outAppURL=*/NULL);
424 if (app_error != noErr)
425 return llvm::createStringError(
426 llvm::inconvertibleErrorCode(),
427 llvm::formatv("could not find external editor \"{0}\": "
428 "LSFindApplicationForInfo returned error {1}",
429 editor, app_error));
430 }
431
432 // Build app launch parameters.
433 LSApplicationParameters app_params;
434 ::memset(&app_params, 0, sizeof(app_params));
435 app_params.flags =
436 kLSLaunchDefaults | kLSLaunchDontAddToRecents | kLSLaunchDontSwitch;
437 if (app_fsref)
438 app_params.application = &(*app_fsref);
439
440 ProcessSerialNumber psn;
441 std::array<CFURLRef, 1> file_array = {file_URL.get()};
442 CFCReleaser<CFArrayRef> cf_array(
443 CFArrayCreate(/*allocator=*/NULL, /*values=*/(const void **)&file_array,
444 /*numValues*/ 1, /*callBacks=*/NULL));
445 error = ::LSOpenURLsWithRole(
446 /*inURLs=*/cf_array.get(), /*inRole=*/kLSRolesEditor,
447 /*inAEParam=*/&file_and_line_desc,
448 /*inAppParams=*/&app_params, /*outPSNs=*/&psn, /*inMaxPSNCount=*/1);
449
450 if (error != noErr)
451 return llvm::createStringError(
452 llvm::inconvertibleErrorCode(),
453 llvm::formatv("LSOpenURLsWithRole failed: error {0}", error));
454
455 return llvm::Error::success();
456#endif // TARGET_OS_OSX
457}
458
459llvm::Error Host::OpenURL(llvm::StringRef url) {
460#if !TARGET_OS_OSX
461 return llvm::errorCodeToError(
462 std::error_code(ENOTSUP, std::system_category()));
463#else // !TARGET_OS_OSX
464 if (url.empty())
465 return llvm::createStringError("cannot open empty URL");
466
467 LLDB_LOG(GetLog(LLDBLog::Host), "Opening URL: {0}", url);
468
469 CFCString url_cfstr(url.data(), kCFStringEncodingUTF8);
470 CFCReleaser<CFURLRef> cfurl = ::CFURLCreateWithString(
471 /*allocator=*/NULL,
472 /*URLString*/ url_cfstr.get(),
473 /*baseURL=*/NULL);
474
475 if (!cfurl.get())
476 return llvm::createStringError(
477 llvm::formatv("could not create CFURL from URL \"{0}\"", url));
478
479 OSStatus error = ::LSOpenCFURLRef(
480 /*inURL=*/cfurl.get(),
481 /*outLaunchedURL=*/NULL);
482
483 if (error != noErr)
484 return llvm::createStringError(
485 llvm::formatv("LSOpenCFURLRef failed: error {0:x}", error));
486
487 return llvm::Error::success();
488#endif // TARGET_OS_OSX
489}
490
492#if !TARGET_OS_OSX
493 return false;
494#else
495 auditinfo_addr_t info;
496 getaudit_addr(&info, sizeof(info));
497 return info.ai_flags & AU_SESSION_FLAG_HAS_GRAPHIC_ACCESS;
498#endif
499}
500
501Environment Host::GetEnvironment() { return Environment(*_NSGetEnviron()); }
502
504 if (process_info.ProcessIDIsValid()) {
505 // Make a new mib to stay thread safe
506 int mib[CTL_MAXNAME] = {
507 0,
508 };
509 size_t mib_len = CTL_MAXNAME;
510 if (::sysctlnametomib("sysctl.proc_cputype", mib, &mib_len))
511 return false;
512
513 mib[mib_len] = process_info.GetProcessID();
514 mib_len++;
515
516 cpu_type_t cpu, sub = 0;
517 size_t len = sizeof(cpu);
518 if (::sysctl(mib, mib_len, &cpu, &len, 0, 0) == 0) {
519 switch (cpu) {
520 case CPU_TYPE_I386:
521 sub = CPU_SUBTYPE_I386_ALL;
522 break;
523 case CPU_TYPE_X86_64:
524 sub = CPU_SUBTYPE_X86_64_ALL;
525 break;
526
527#if defined(CPU_TYPE_ARM64) && defined(CPU_SUBTYPE_ARM64_ALL)
528 case CPU_TYPE_ARM64:
529 sub = CPU_SUBTYPE_ARM64_ALL;
530 break;
531#endif
532
533#if defined(CPU_TYPE_ARM64_32) && defined(CPU_SUBTYPE_ARM64_32_ALL)
535 sub = CPU_SUBTYPE_ARM64_32_ALL;
536 break;
537#endif
538
539 case CPU_TYPE_ARM: {
540 // Note that we fetched the cpu type from the PROCESS but we can't get a
541 // cpusubtype of the
542 // process -- we can only get the host's cpu subtype.
543 uint32_t cpusubtype = 0;
544 len = sizeof(cpusubtype);
545 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) == 0)
546 sub = cpusubtype;
547
548 bool host_cpu_is_64bit;
549 uint32_t is64bit_capable;
550 size_t is64bit_capable_len = sizeof(is64bit_capable);
551 host_cpu_is_64bit =
552 sysctlbyname("hw.cpu64bit_capable", &is64bit_capable,
553 &is64bit_capable_len, NULL, 0) == 0;
554
555 // if the host is an armv8 device, its cpusubtype will be in
556 // CPU_SUBTYPE_ARM64 numbering
557 // and we need to rewrite it to a reasonable CPU_SUBTYPE_ARM value
558 // instead.
559
560 if (host_cpu_is_64bit) {
561 sub = CPU_SUBTYPE_ARM_V7;
562 }
563 } break;
564
565 default:
566 break;
567 }
568 process_info.GetArchitecture().SetArchitecture(eArchTypeMachO, cpu, sub);
569 return true;
570 }
571 }
572 process_info.GetArchitecture().Clear();
573 return false;
574}
575
576static bool GetMacOSXProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr,
577 ProcessInstanceInfo &process_info) {
578 if (process_info.ProcessIDIsValid()) {
579 int proc_args_mib[3] = {CTL_KERN, KERN_PROCARGS2,
580 (int)process_info.GetProcessID()};
581
582 size_t arg_data_size = 0;
583 if (::sysctl(proc_args_mib, 3, nullptr, &arg_data_size, NULL, 0) ||
584 arg_data_size == 0)
585 arg_data_size = 8192;
586
587 // Add a few bytes to the calculated length, I know we need to add at least
588 // one byte
589 // to this number otherwise we get junk back, so add 128 just in case...
590 DataBufferHeap arg_data(arg_data_size + 128, 0);
591 arg_data_size = arg_data.GetByteSize();
592 if (::sysctl(proc_args_mib, 3, arg_data.GetBytes(), &arg_data_size, NULL,
593 0) == 0) {
594 DataExtractor data(arg_data.GetBytes(), arg_data_size,
595 endian::InlHostByteOrder(), sizeof(void *));
596 lldb::offset_t offset = 0;
597 uint32_t argc = data.GetU32(&offset);
598 llvm::Triple &triple = process_info.GetArchitecture().GetTriple();
599 const llvm::Triple::ArchType triple_arch = triple.getArch();
600 const bool check_for_ios_simulator =
601 (triple_arch == llvm::Triple::x86 ||
602 triple_arch == llvm::Triple::x86_64 ||
603 triple_arch == llvm::Triple::aarch64);
604
605 const char *cstr = data.GetCStr(&offset);
606 if (cstr) {
607 process_info.GetExecutableFile().SetFile(cstr, FileSpec::Style::native);
608
609 if (match_info_ptr == NULL ||
611 match_info_ptr->GetNameMatchType(),
612 match_info_ptr->GetProcessInfo().GetName())) {
613 // Skip NULLs
614 while (true) {
615 const uint8_t *p = data.PeekData(offset, 1);
616 if ((p == NULL) || (*p != '\0'))
617 break;
618 ++offset;
619 }
620
621 // Skip argv[0] as we already have the file name from the executable path.
622 if (argc > 0)
623 process_info.SetArg0(data.GetCStr(&offset));
624
625 // Now extract the rest of the arguments.
626 Args &proc_args = process_info.GetArguments();
627 for (int i = 1; i < static_cast<int>(argc); ++i) {
628 cstr = data.GetCStr(&offset);
629 if (cstr)
630 proc_args.AppendArgument(llvm::StringRef(cstr));
631 }
632
633 Environment &proc_env = process_info.GetEnvironment();
634 bool is_simulator = false;
635 llvm::StringRef env_var;
636 while (!(env_var = data.GetCStr(&offset)).empty()) {
637 if (check_for_ios_simulator &&
638 env_var.starts_with("SIMULATOR_UDID="))
639 is_simulator = true;
640 proc_env.insert(env_var);
641 }
642 llvm::Triple &triple = process_info.GetArchitecture().GetTriple();
643 if (is_simulator) {
644 triple.setOS(llvm::Triple::IOS);
645 triple.setEnvironment(llvm::Triple::Simulator);
646 } else {
647 triple.setOS(llvm::Triple::MacOSX);
648 }
649 return true;
650 }
651 }
652 }
653 }
654 return false;
655}
656
658 if (process_info.ProcessIDIsValid()) {
659 int mib[4];
660 mib[0] = CTL_KERN;
661 mib[1] = KERN_PROC;
662 mib[2] = KERN_PROC_PID;
663 mib[3] = process_info.GetProcessID();
664 struct kinfo_proc proc_kinfo;
665 size_t proc_kinfo_size = sizeof(struct kinfo_proc);
666
667 if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0) {
668 if (proc_kinfo_size > 0) {
669 process_info.SetParentProcessID(proc_kinfo.kp_eproc.e_ppid);
670 process_info.SetUserID(proc_kinfo.kp_eproc.e_pcred.p_ruid);
671 process_info.SetGroupID(proc_kinfo.kp_eproc.e_pcred.p_rgid);
672 process_info.SetEffectiveUserID(proc_kinfo.kp_eproc.e_ucred.cr_uid);
673 if (proc_kinfo.kp_eproc.e_ucred.cr_ngroups > 0)
674 process_info.SetEffectiveGroupID(
675 proc_kinfo.kp_eproc.e_ucred.cr_groups[0]);
676 else
677 process_info.SetEffectiveGroupID(UINT32_MAX);
678 return true;
679 }
680 }
681 }
683 process_info.SetUserID(UINT32_MAX);
684 process_info.SetGroupID(UINT32_MAX);
685 process_info.SetEffectiveUserID(UINT32_MAX);
686 process_info.SetEffectiveGroupID(UINT32_MAX);
687 return false;
688}
689
690/// Fetches the list of all processes into \p kinfos.
691static llvm::Error GetProcessList(std::vector<struct kinfo_proc> &kinfos) {
692 int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
693
694 // How often we retry fetching the process list.
695 static constexpr unsigned g_retry_count = 200;
696 // The rate at which we increase the adjusted buffer size to
697 // account for newly created processes between two sysctl calls.
698 static constexpr unsigned g_expected_new_pids = 500;
699 // We keep increasing the expected growth rate between the two
700 // sysctl calls. Check that the last attempt does not create an
701 // unreasonbly large buffer. It is unlikely we run on a system where
702 // 100k processes are repeatedly created between each attempted sysctl
703 // pair.
704 static_assert(
705 g_retry_count * g_expected_new_pids <= 100'000,
706 "Final retry attempt assumes an unlikely amount of new processes.");
707
708 // This is an inherently racy API. We have to first query the size for our
709 // buffer and then pass it back to sysctl. If more processes spawn between the
710 // size query and the actual call to fetch, then sysctl returns ENOMEM.
711 // We keep retrying until we get a passing result.
712 for (unsigned attempt = 1; attempt < g_retry_count; ++attempt) {
713 // Fetch the buffer size sysctl would return.
714 size_t current_pid_size = 0;
715 if (::sysctl(mib, 3, nullptr, &current_pid_size, nullptr, 0) != 0)
716 return llvm::errorCodeToError(
717 std::error_code(errno, std::generic_category()));
718
719 // Convert the byte length result to number of elements.
720 const size_t current_num_processes =
721 current_pid_size / sizeof(struct kinfo_proc);
722
723 // Adjust the buffer for new processes that spawned between the
724 // previous and next sysctl call. We increase this growth each attempt
725 // to account for systems where a lot of new processes spawn between
726 // these two calls.
727 const size_t expected_growth = attempt * g_expected_new_pids;
728
729 // Allocate the buffer for the sysctl result.
730 kinfos.resize(current_num_processes + expected_growth);
731
732 // Fetch the actual process list and let sysctl adjust actual_pid_size.
733 size_t actual_pid_size = kinfos.size() * sizeof(struct kinfo_proc);
734 if (::sysctl(mib, 3, &kinfos[0], &actual_pid_size, nullptr, 0) == 0) {
735 // Shrink the buffer to the actual number of processes returned.
736 kinfos.resize(actual_pid_size / sizeof(struct kinfo_proc));
737 return llvm::Error::success();
738 }
739
740 // Errno is set to ENOMEM if our estimated_pid_size is too small, in which
741 // case we retry with a bigger buffer. Any other error is unexpected.
742 if (errno != ENOMEM)
743 return llvm::errorCodeToError(
744 std::error_code(errno, std::generic_category()));
745 }
746
747 // The only way to exit the loop above is by repeatedly hitting ENOMEM. The
748 // only way this can happen is if the process list somehow grew extremely
749 // large between the two sysctl calls.
750 assert(errno == ENOMEM &&
751 "loop should only be left via the ENOMEM retry path");
752 return llvm::createStringErrorV(
753 "Failed to read process list: sysctl kept returning ENOMEM after {0} "
754 "attempts",
755 g_retry_count);
756}
757
758uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
759 ProcessInstanceInfoList &process_infos) {
760 std::vector<struct kinfo_proc> kinfos;
761 if (llvm::Error error = GetProcessList(kinfos)) {
763 "failed to read process list: {0}");
764 return 0;
765 }
766
767 bool all_users = match_info.GetMatchAllUsers();
768 const lldb::pid_t our_pid = getpid();
769 const uid_t our_uid = getuid();
770 for (const struct kinfo_proc &kinfo : kinfos) {
771 bool kinfo_user_matches = false;
772 if (all_users)
773 kinfo_user_matches = true;
774 else
775 kinfo_user_matches = kinfo.kp_eproc.e_pcred.p_ruid == our_uid;
776
777 // Special case, if lldb is being run as root we can attach to anything.
778 if (our_uid == 0)
779 kinfo_user_matches = true;
780
781 if (!kinfo_user_matches || // Make sure the user is acceptable
782 static_cast<lldb::pid_t>(kinfo.kp_proc.p_pid) ==
783 our_pid || // Skip this process
784 kinfo.kp_proc.p_pid == 0 || // Skip kernel (kernel pid is zero)
785 kinfo.kp_proc.p_stat == SZOMB || // Zombies are bad, they like brains...
786 kinfo.kp_proc.p_flag & P_TRACED || // Being debugged?
787 kinfo.kp_proc.p_flag & P_WEXIT)
788 continue;
789
790 ProcessInstanceInfo process_info;
791 process_info.SetProcessID(kinfo.kp_proc.p_pid);
792 process_info.SetParentProcessID(kinfo.kp_eproc.e_ppid);
793 process_info.SetUserID(kinfo.kp_eproc.e_pcred.p_ruid);
794 process_info.SetGroupID(kinfo.kp_eproc.e_pcred.p_rgid);
795 process_info.SetEffectiveUserID(kinfo.kp_eproc.e_ucred.cr_uid);
796 if (kinfo.kp_eproc.e_ucred.cr_ngroups > 0)
797 process_info.SetEffectiveGroupID(kinfo.kp_eproc.e_ucred.cr_groups[0]);
798 else
799 process_info.SetEffectiveGroupID(UINT32_MAX);
800
801 // Make sure our info matches before we go fetch the name and cpu type
802 if (!match_info.UserIDsMatch(process_info) ||
803 !match_info.ProcessIDsMatch(process_info))
804 continue;
805
806 // Get CPU type first so we can know to look for iOS simulator if we have
807 // a compatible type.
808 if (GetMacOSXProcessCPUType(process_info)) {
809 if (GetMacOSXProcessArgs(&match_info, process_info)) {
810 if (match_info.Matches(process_info))
811 process_infos.push_back(process_info);
812 }
813 }
814 }
815 return process_infos.size();
816}
817
819 process_info.SetProcessID(pid);
820 bool success = false;
821
822 // Get CPU type first so we can know to look for iOS simulator is we have x86
823 // or x86_64
824 if (GetMacOSXProcessCPUType(process_info))
825 success = true;
826
827 if (GetMacOSXProcessArgs(NULL, process_info))
828 success = true;
829
830 if (GetMacOSXProcessUserAndGroup(process_info))
831 success = true;
832
833 if (success)
834 return true;
835
836 process_info.Clear();
837 return false;
838}
839
840#if TARGET_OS_OSX
841static void PackageXPCArguments(xpc_object_t message, const char *prefix,
842 const Args &args) {
843 size_t count = args.GetArgumentCount();
844 char buf[50]; // long enough for 'argXXX'
845 memset(buf, 0, sizeof(buf));
846 snprintf(buf, sizeof(buf), "%sCount", prefix);
847 xpc_dictionary_set_int64(message, buf, count);
848 for (size_t i = 0; i < count; i++) {
849 memset(buf, 0, sizeof(buf));
850 snprintf(buf, sizeof(buf), "%s%zi", prefix, i);
851 xpc_dictionary_set_string(message, buf, args.GetArgumentAtIndex(i));
852 }
853}
854
855static void PackageXPCEnvironment(xpc_object_t message, llvm::StringRef prefix,
856 const Environment &env) {
857 xpc_dictionary_set_int64(message, (prefix + "Count").str().c_str(),
858 env.size());
859 size_t i = 0;
860 for (const auto &KV : env) {
861 xpc_dictionary_set_string(message, (prefix + llvm::Twine(i)).str().c_str(),
862 Environment::compose(KV).c_str());
863 }
864}
865
866/*
867 A valid authorizationRef means that
868 - there is the LaunchUsingXPCRightName rights in the /etc/authorization
869 - we have successfully copied the rights to be send over the XPC wire
870 Once obtained, it will be valid for as long as the process lives.
871 */
872static AuthorizationRef authorizationRef = NULL;
873static Status getXPCAuthorization(ProcessLaunchInfo &launch_info) {
876
877 if ((launch_info.GetUserID() == 0) && !authorizationRef) {
878 OSStatus createStatus =
879 AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
880 kAuthorizationFlagDefaults, &authorizationRef);
881 if (createStatus != errAuthorizationSuccess) {
883 error = Status::FromErrorString("Can't create authorizationRef.");
884 LLDB_LOG(log, "error: {0}", error);
885 return error;
886 }
887
888 OSStatus rightsStatus =
889 AuthorizationRightGet(LaunchUsingXPCRightName, NULL);
890 if (rightsStatus != errAuthorizationSuccess) {
891 // No rights in the security database, Create it with the right prompt.
892 CFStringRef prompt =
893 CFSTR("Xcode is trying to take control of a root process.");
894 CFStringRef keys[] = {CFSTR("en")};
895 CFTypeRef values[] = {prompt};
896 CFDictionaryRef promptDict = CFDictionaryCreate(
897 kCFAllocatorDefault, (const void **)keys, (const void **)values, 1,
898 &kCFCopyStringDictionaryKeyCallBacks,
899 &kCFTypeDictionaryValueCallBacks);
900
901 CFStringRef keys1[] = {CFSTR("class"), CFSTR("group"), CFSTR("comment"),
902 CFSTR("default-prompt"), CFSTR("shared")};
903 CFTypeRef values1[] = {CFSTR("user"), CFSTR("admin"),
904 CFSTR(LaunchUsingXPCRightName), promptDict,
905 kCFBooleanFalse};
906 CFDictionaryRef dict = CFDictionaryCreate(
907 kCFAllocatorDefault, (const void **)keys1, (const void **)values1, 5,
908 &kCFCopyStringDictionaryKeyCallBacks,
909 &kCFTypeDictionaryValueCallBacks);
910 rightsStatus = AuthorizationRightSet(
911 authorizationRef, LaunchUsingXPCRightName, dict, NULL, NULL, NULL);
912 CFRelease(promptDict);
913 CFRelease(dict);
914 }
915
916 OSStatus copyRightStatus = errAuthorizationDenied;
917 if (rightsStatus == errAuthorizationSuccess) {
918 AuthorizationItem item1 = {LaunchUsingXPCRightName, 0, NULL, 0};
919 AuthorizationItem items[] = {item1};
920 AuthorizationRights requestedRights = {1, items};
921 AuthorizationFlags authorizationFlags =
922 kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights;
923 copyRightStatus = AuthorizationCopyRights(
924 authorizationRef, &requestedRights, kAuthorizationEmptyEnvironment,
925 authorizationFlags, NULL);
926 }
927
928 if (copyRightStatus != errAuthorizationSuccess) {
929 // Eventually when the commandline supports running as root and the user
930 // is not
931 // logged in to the current audit session, we will need the trick in gdb
932 // where
933 // we ask the user to type in the root passwd in the terminal.
936 "Launching as root needs root authorization.");
937 LLDB_LOG(log, "error: {0}", error);
938
939 if (authorizationRef) {
940 AuthorizationFree(authorizationRef, kAuthorizationFlagDefaults);
941 authorizationRef = NULL;
942 }
943 }
944 }
945
946 return error;
947}
948#endif
949
950static short GetPosixspawnFlags(const ProcessLaunchInfo &launch_info) {
951 short flags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
952
953 if (launch_info.GetFlags().Test(eLaunchFlagExec))
954 flags |= POSIX_SPAWN_SETEXEC; // Darwin specific posix_spawn flag
955
956 if (launch_info.GetFlags().Test(eLaunchFlagDebug))
957 flags |= POSIX_SPAWN_START_SUSPENDED; // Darwin specific posix_spawn flag
958
959 if (launch_info.GetFlags().Test(eLaunchFlagDisableASLR))
960 flags |= _POSIX_SPAWN_DISABLE_ASLR; // Darwin specific posix_spawn flag
961
962 if (launch_info.GetLaunchInSeparateProcessGroup())
963 flags |= POSIX_SPAWN_SETPGROUP;
964
965#ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
966#if defined(__x86_64__) || defined(__i386__)
967 static LazyBool g_use_close_on_exec_flag = eLazyBoolCalculate;
968 if (g_use_close_on_exec_flag == eLazyBoolCalculate) {
969 g_use_close_on_exec_flag = eLazyBoolNo;
970
971 llvm::VersionTuple version = HostInfo::GetOSVersion();
972 if (version > llvm::VersionTuple(10, 7)) {
973 // Kernel panic if we use the POSIX_SPAWN_CLOEXEC_DEFAULT on 10.7 or
974 // earlier
975 g_use_close_on_exec_flag = eLazyBoolYes;
976 }
977 }
978#else
979 static LazyBool g_use_close_on_exec_flag = eLazyBoolYes;
980#endif // defined(__x86_64__) || defined(__i386__)
981 // Close all files exception those with file actions if this is supported.
982 if (g_use_close_on_exec_flag == eLazyBoolYes)
983 flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;
984#endif // ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
985 return flags;
986}
987
988static void finalize_xpc(void *xpc_object) {
989 xpc_release((xpc_object_t)xpc_object);
990}
991
992static Status LaunchProcessXPC(const char *exe_path,
993 ProcessLaunchInfo &launch_info,
994 lldb::pid_t &pid) {
995#if TARGET_OS_OSX
996 Status error = getXPCAuthorization(launch_info);
997 if (error.Fail())
998 return error;
999
1001
1002 uid_t requested_uid = launch_info.GetUserID();
1003 const char *xpc_service = nil;
1004 bool send_auth = false;
1005 AuthorizationExternalForm extForm;
1006 if (requested_uid == 0) {
1007 if (AuthorizationMakeExternalForm(authorizationRef, &extForm) ==
1008 errAuthorizationSuccess) {
1009 send_auth = true;
1010 } else {
1013 "Launching root via XPC needs to "
1014 "externalize authorization reference.");
1015 LLDB_LOG(log, "error: {0}", error);
1016 return error;
1017 }
1018 xpc_service = LaunchUsingXPCRightName;
1019 } else {
1022 "Launching via XPC is only currently available for root.");
1023 LLDB_LOG(log, "error: {0}", error);
1024 return error;
1025 }
1026
1027 xpc_connection_t conn = xpc_connection_create(xpc_service, NULL);
1028
1029 xpc_connection_set_event_handler(conn, ^(xpc_object_t event) {
1030 xpc_type_t type = xpc_get_type(event);
1031
1032 if (type == XPC_TYPE_ERROR) {
1033 if (event == XPC_ERROR_CONNECTION_INTERRUPTED) {
1034 // The service has either canceled itself, crashed, or been terminated.
1035 // The XPC connection is still valid and sending a message to it will
1036 // re-launch the service.
1037 // If the service is state-full, this is the time to initialize the new
1038 // service.
1039 return;
1040 } else if (event == XPC_ERROR_CONNECTION_INVALID) {
1041 // The service is invalid. Either the service name supplied to
1042 // xpc_connection_create() is incorrect
1043 // or we (this process) have canceled the service; we can do any cleanup
1044 // of application state at this point.
1045 // printf("Service disconnected");
1046 return;
1047 } else {
1048 // printf("Unexpected error from service: %s",
1049 // xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
1050 }
1051
1052 } else {
1053 // printf("Received unexpected event in handler");
1054 }
1055 });
1056
1057 xpc_connection_set_finalizer_f(conn, finalize_xpc);
1058 xpc_connection_resume(conn);
1059 xpc_object_t message = xpc_dictionary_create(nil, nil, 0);
1060
1061 if (send_auth) {
1062 xpc_dictionary_set_data(message, LauncherXPCServiceAuthKey, extForm.bytes,
1063 sizeof(AuthorizationExternalForm));
1064 }
1065
1066 PackageXPCArguments(message, LauncherXPCServiceArgPrefxKey,
1067 launch_info.GetArguments());
1068 PackageXPCEnvironment(message, LauncherXPCServiceEnvPrefxKey,
1069 launch_info.GetEnvironment());
1070
1071 // Posix spawn stuff.
1072 xpc_dictionary_set_int64(message, LauncherXPCServiceCPUTypeKey,
1073 launch_info.GetArchitecture().GetMachOCPUType());
1074 xpc_dictionary_set_int64(message, LauncherXPCServicePosixspawnFlagsKey,
1075 GetPosixspawnFlags(launch_info));
1076 const FileAction *file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
1077 std::string file_action_path;
1078 if (file_action)
1079 file_action_path = file_action->GetFileSpec().GetPath();
1080
1081 if (!file_action_path.empty())
1082 xpc_dictionary_set_string(message, LauncherXPCServiceStdInPathKeyKey,
1083 file_action_path.c_str());
1084
1085 file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
1086 if (file_action)
1087 file_action_path = file_action->GetFileSpec().GetPath();
1088
1089 if (!file_action_path.empty())
1090 xpc_dictionary_set_string(message, LauncherXPCServiceStdOutPathKeyKey,
1091 file_action_path.c_str());
1092
1093 file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
1094 if (file_action)
1095 file_action_path = file_action->GetFileSpec().GetPath();
1096
1097 if (!file_action_path.empty())
1098 xpc_dictionary_set_string(message, LauncherXPCServiceStdErrPathKeyKey,
1099 file_action_path.c_str());
1100
1101 xpc_object_t reply =
1102 xpc_connection_send_message_with_reply_sync(conn, message);
1103 xpc_type_t returnType = xpc_get_type(reply);
1104 if (returnType == XPC_TYPE_DICTIONARY) {
1105 pid = xpc_dictionary_get_int64(reply, LauncherXPCServiceChildPIDKey);
1106 if (pid == 0) {
1107 int errorType =
1108 xpc_dictionary_get_int64(reply, LauncherXPCServiceErrorTypeKey);
1109 int errorCode =
1110 xpc_dictionary_get_int64(reply, LauncherXPCServiceCodeTypeKey);
1111
1112 error = Status(errorCode, eErrorTypeGeneric);
1114 "Problems with launching via XPC. Error type : %i, code : %i",
1115 errorType, errorCode);
1116 LLDB_LOG(log, "error: {0}", error);
1117
1118 if (authorizationRef) {
1119 AuthorizationFree(authorizationRef, kAuthorizationFlagDefaults);
1120 authorizationRef = NULL;
1121 }
1122 }
1123 } else if (returnType == XPC_TYPE_ERROR) {
1126 "Problems with launching via XPC. XPC error : %s",
1127 xpc_dictionary_get_string(reply, XPC_ERROR_KEY_DESCRIPTION));
1128 LLDB_LOG(log, "error: {0}", error);
1129 }
1130
1131 return error;
1132#else
1133 Status error;
1134 return error;
1135#endif
1136}
1137
1138static bool AddPosixSpawnFileAction(void *_file_actions, const FileAction *info,
1139 Log *log, Status &error) {
1140 if (info == NULL)
1141 return false;
1142
1143 posix_spawn_file_actions_t *file_actions =
1144 static_cast<posix_spawn_file_actions_t *>(_file_actions);
1145
1146 switch (info->GetAction()) {
1148 error.Clear();
1149 break;
1150
1152 if (info->GetFD() == -1)
1154 "invalid fd for posix_spawn_file_actions_addclose(...)");
1155 else {
1156 error = Status(
1157 ::posix_spawn_file_actions_addclose(file_actions, info->GetFD()),
1159 if (error.Fail())
1160 LLDB_LOG(log,
1161 "error: {0}, posix_spawn_file_actions_addclose "
1162 "(action={1}, fd={2})",
1163 error, file_actions, info->GetFD());
1164 }
1165 break;
1166
1168 if (info->GetFD() == -1)
1170 "invalid fd for posix_spawn_file_actions_adddup2(...)");
1171 else if (info->GetActionArgument() == -1)
1173 "invalid duplicate fd for posix_spawn_file_actions_adddup2(...)");
1174 else if (info->GetFD() != info->GetActionArgument()) {
1175 error =
1176 Status(::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(),
1177 info->GetActionArgument()),
1179 if (error.Fail())
1180 LLDB_LOG(log,
1181 "error: {0}, posix_spawn_file_actions_adddup2 "
1182 "(action={1}, fd={2}, dup_fd={3})",
1183 error, file_actions, info->GetFD(), info->GetActionArgument());
1184 } else {
1185 error =
1186 Status(::posix_spawn_file_actions_addinherit_np(file_actions, info->GetFD()),
1188 if (error.Fail())
1189 LLDB_LOG(log,
1190 "error: {0}, posix_spawn_file_actions_addinherit_np "
1191 "(action={1}, fd={2})",
1192 error, file_actions, info->GetFD());
1193 }
1194 break;
1195
1197 if (info->GetFD() == -1)
1199 "invalid fd in posix_spawn_file_actions_addopen(...)");
1200 else {
1201 int oflag = info->GetActionArgument();
1202
1203 mode_t mode = 0;
1204
1205 if (oflag & O_CREAT)
1206 mode = 0640;
1207
1208 const std::string file_path(info->GetFileSpec().GetPath());
1209 error = Status(
1210 ::posix_spawn_file_actions_addopen(file_actions, info->GetFD(),
1211 file_path.c_str(), oflag, mode),
1213 if (error.Fail())
1214 LLDB_LOG(log,
1215 "error: {0}, posix_spawn_file_actions_addopen (action={1}, "
1216 "fd={2}, path='{3}', oflag={4}, mode={5})",
1217 error, file_actions, info->GetFD(), file_path, oflag, mode);
1218 }
1219 break;
1220 }
1221 return error.Success();
1222}
1223
1224static Status LaunchProcessPosixSpawn(const char *exe_path,
1225 const ProcessLaunchInfo &launch_info,
1226 lldb::pid_t &pid) {
1227 Status error;
1229
1230 posix_spawnattr_t attr;
1231 error = Status(::posix_spawnattr_init(&attr), eErrorTypePOSIX);
1232
1233 if (error.Fail()) {
1234 LLDB_LOG(log, "error: {0}, ::posix_spawnattr_init ( &attr )", error);
1235 return error;
1236 }
1237
1238 // Make sure we clean up the posix spawn attributes before exiting this scope.
1239 llvm::scope_exit cleanup_attr([&]() { posix_spawnattr_destroy(&attr); });
1240
1241 sigset_t no_signals;
1242 sigset_t all_signals;
1243 sigemptyset(&no_signals);
1244 sigfillset(&all_signals);
1245 ::posix_spawnattr_setsigmask(&attr, &no_signals);
1246 ::posix_spawnattr_setsigdefault(&attr, &all_signals);
1247
1248 short flags = GetPosixspawnFlags(launch_info);
1249
1250 error = Status(::posix_spawnattr_setflags(&attr, flags), eErrorTypePOSIX);
1251 if (error.Fail()) {
1252 LLDB_LOG(log,
1253 "error: {0}, ::posix_spawnattr_setflags ( &attr, flags={1:x} )",
1254 error, flags);
1255 return error;
1256 }
1257
1258 bool is_graphical = true;
1259
1260#if TARGET_OS_OSX
1261 SecuritySessionId session_id;
1262 SessionAttributeBits session_attributes;
1263 OSStatus status =
1264 SessionGetInfo(callerSecuritySession, &session_id, &session_attributes);
1265 if (status == errSessionSuccess)
1266 is_graphical = session_attributes & sessionHasGraphicAccess;
1267#endif
1268
1269 // When lldb is ran through a graphical session, make the debuggee process
1270 // responsible for its own TCC permissions instead of inheriting them from
1271 // its parent.
1272 if (is_graphical && launch_info.GetFlags().Test(eLaunchFlagDebug) &&
1273 !launch_info.GetFlags().Test(eLaunchFlagInheritTCCFromParent)) {
1275 if (error.Fail()) {
1276 LLDB_LOG(log, "error: {0}, setup_posix_spawn_responsible_flag(&attr)",
1277 error);
1278 return error;
1279 }
1280 }
1281
1282 if (launch_info.GetFlags().Test(eLaunchFlagMemoryTagging)) {
1283 // The following function configures the spawn attributes to launch the
1284 // process with memory tagging explicitly enabled. We look it up
1285 // dynamically since it is only available on newer OS. Does nothing on
1286 // hardware which does not support MTE.
1287 //
1288 // int posix_spawnattr_set_use_sec_transition_shims_np(
1289 // posix_spawnattr_t *attr, uint32_t flags);
1290 //
1291 using posix_spawnattr_set_use_sec_transition_shims_np_t =
1292 int (*)(posix_spawnattr_t *attr, uint32_t flags);
1293 auto posix_spawnattr_enable_memory_tagging_fn =
1294 (posix_spawnattr_set_use_sec_transition_shims_np_t)dlsym(
1295 RTLD_DEFAULT, "posix_spawnattr_set_use_sec_transition_shims_np");
1296 if (posix_spawnattr_enable_memory_tagging_fn) {
1297 error = Status(posix_spawnattr_enable_memory_tagging_fn(&attr, 0),
1299 if (error.Fail()) {
1300 LLDB_LOG(log,
1301 "error: {}, "
1302 "posix_spawnattr_set_use_sec_transition_shims_np(&attr, 0)",
1303 error);
1304 return error;
1305 }
1306 } else {
1307 LLDB_LOG(log,
1308 "error: {}, posix_spawnattr_set_use_sec_transition_shims_np "
1309 "not available",
1310 error);
1311 }
1312 }
1313
1314 // Don't set the binpref if a shell was provided. After all, that's only
1315 // going to affect what version of the shell is launched, not what fork of
1316 // the binary is launched. We insert "arch --arch <ARCH> as part of the
1317 // shell invocation to do that job on OSX.
1318 if (launch_info.GetShell() == FileSpec()) {
1319 const ArchSpec &arch_spec = launch_info.GetArchitecture();
1320 cpu_type_t cpu_type = arch_spec.GetMachOCPUType();
1321 cpu_type_t cpu_subtype = arch_spec.GetMachOCPUSubType();
1322 const bool set_cpu_type =
1323 cpu_type != 0 && cpu_type != static_cast<cpu_type_t>(UINT32_MAX) &&
1324 cpu_type != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE);
1325 const bool set_cpu_subtype =
1326 cpu_subtype != 0 &&
1327 cpu_subtype != static_cast<cpu_subtype_t>(UINT32_MAX) &&
1328 cpu_subtype != CPU_SUBTYPE_X86_64_H;
1329 if (set_cpu_type) {
1330 size_t ocount = 0;
1331 typedef int (*posix_spawnattr_setarchpref_np_t)(
1332 posix_spawnattr_t *, size_t, cpu_type_t *, cpu_subtype_t *, size_t *);
1333 posix_spawnattr_setarchpref_np_t posix_spawnattr_setarchpref_np_fn =
1334 (posix_spawnattr_setarchpref_np_t)dlsym(
1335 RTLD_DEFAULT, "posix_spawnattr_setarchpref_np");
1336 if (set_cpu_subtype && posix_spawnattr_setarchpref_np_fn) {
1337 error = Status((*posix_spawnattr_setarchpref_np_fn)(
1338 &attr, 1, &cpu_type, &cpu_subtype, &ocount),
1340 if (error.Fail())
1341 LLDB_LOG(log,
1342 "error: {}, ::posix_spawnattr_setarchpref_np ( &attr, 1, "
1343 "cpu_type = {:x}, cpu_subtype = {:x}, count => {} )",
1344 error, cpu_type, cpu_subtype, ocount);
1345
1346 if (error.Fail() || ocount != 1)
1347 return error;
1348 } else {
1349 error = Status(
1350 ::posix_spawnattr_setbinpref_np(&attr, 1, &cpu_type, &ocount),
1352 if (error.Fail())
1353 LLDB_LOG(log,
1354 "error: {0}, ::posix_spawnattr_setbinpref_np ( &attr, 1, "
1355 "cpu_type = {1:x}, count => {2} )",
1356 error, cpu_type, ocount);
1357 if (error.Fail() || ocount != 1)
1358 return error;
1359 }
1360 }
1361 }
1362
1363 const char *tmp_argv[2];
1364 char *const *argv = const_cast<char *const *>(
1365 launch_info.GetArguments().GetConstArgumentVector());
1366 Environment::Envp envp = launch_info.GetEnvironment().getEnvp();
1367 if (argv == NULL) {
1368 // posix_spawn gets very unhappy if it doesn't have at least the program
1369 // name in argv[0]. One of the side affects I have noticed is the
1370 // environment
1371 // variables don't make it into the child process if "argv == NULL"!!!
1372 tmp_argv[0] = exe_path;
1373 tmp_argv[1] = NULL;
1374 argv = const_cast<char *const *>(tmp_argv);
1375 }
1376
1377 FileSpec working_dir{launch_info.GetWorkingDirectory()};
1378 if (working_dir) {
1379 // Set the working directory on this thread only
1380 std::string working_dir_path = working_dir.GetPath();
1381 if (__pthread_chdir(working_dir_path.c_str()) < 0) {
1382 if (errno == ENOENT) {
1384 "No such file or directory: %s", working_dir_path.c_str());
1385 } else if (errno == ENOTDIR) {
1387 "Path doesn't name a directory: %s", working_dir_path.c_str());
1388 } else {
1389 error =
1390 Status::FromErrorStringWithFormat("An unknown error occurred when "
1391 "changing directory for process "
1392 "execution.");
1393 }
1394 return error;
1395 }
1396 }
1397
1398 ::pid_t result_pid = LLDB_INVALID_PROCESS_ID;
1399 const size_t num_file_actions = launch_info.GetNumFileActions();
1400 if (num_file_actions > 0) {
1401 posix_spawn_file_actions_t file_actions;
1402 error =
1403 Status(::posix_spawn_file_actions_init(&file_actions), eErrorTypePOSIX);
1404 if (error.Fail()) {
1405 LLDB_LOG(log,
1406 "error: {0}, ::posix_spawn_file_actions_init ( &file_actions )",
1407 error);
1408 return error;
1409 }
1410
1411 // Make sure we clean up the posix file actions before exiting this scope.
1412 llvm::scope_exit cleanup_fileact(
1413 [&]() { posix_spawn_file_actions_destroy(&file_actions); });
1414
1415 for (size_t i = 0; i < num_file_actions; ++i) {
1416 const FileAction *launch_file_action =
1417 launch_info.GetFileActionAtIndex(i);
1418 if (launch_file_action) {
1419 if (!AddPosixSpawnFileAction(&file_actions, launch_file_action, log,
1420 error))
1421 return error;
1422 }
1423 }
1424
1425 error = Status(
1426 ::posix_spawnp(&result_pid, exe_path, &file_actions, &attr, argv, envp),
1428
1429 if (error.Fail()) {
1430 LLDB_LOG(log,
1431 "error: {0}, ::posix_spawnp(pid => {1}, path = '{2}', "
1432 "file_actions = {3}, "
1433 "attr = {4}, argv = {5}, envp = {6} )",
1434 error, result_pid, exe_path, &file_actions, &attr, argv,
1435 envp.get());
1436 if (log) {
1437 for (int ii = 0; argv[ii]; ++ii)
1438 LLDB_LOG(log, "argv[{0}] = '{1}'", ii, argv[ii]);
1439 }
1440 }
1441
1442 } else {
1443 error =
1444 Status(::posix_spawnp(&result_pid, exe_path, NULL, &attr, argv, envp),
1446
1447 if (error.Fail()) {
1448 LLDB_LOG(log,
1449 "error: {0}, ::posix_spawnp ( pid => {1}, path = '{2}', "
1450 "file_actions = NULL, attr = {3}, argv = {4}, envp = {5} )",
1451 error, result_pid, exe_path, &attr, argv, envp.get());
1452 if (log) {
1453 for (int ii = 0; argv[ii]; ++ii)
1454 LLDB_LOG(log, "argv[{0}] = '{1}'", ii, argv[ii]);
1455 }
1456 }
1457 }
1458 pid = result_pid;
1459
1460 if (working_dir) {
1461 // No more thread specific current working directory
1462 __pthread_fchdir(-1);
1463 }
1464
1465 return error;
1466}
1467
1468static bool ShouldLaunchUsingXPC(ProcessLaunchInfo &launch_info) {
1469 bool result = false;
1470
1471#if TARGET_OS_OSX
1472 bool launchingAsRoot = launch_info.GetUserID() == 0;
1473 bool currentUserIsRoot = HostInfo::GetEffectiveUserID() == 0;
1474
1475 if (launchingAsRoot && !currentUserIsRoot) {
1476 // If current user is already root, we don't need XPC's help.
1477 result = true;
1478 }
1479#endif
1480
1481 return result;
1482}
1483
1485 Status error;
1486
1487 FileSystem &fs = FileSystem::Instance();
1488 FileSpec exe_spec(launch_info.GetExecutableFile());
1489
1490 if (!fs.Exists(exe_spec))
1491 FileSystem::Instance().Resolve(exe_spec);
1492
1493 if (!fs.Exists(exe_spec))
1495
1496 if (!fs.Exists(exe_spec)) {
1498 "executable doesn't exist: '{0}'", exe_spec);
1499 return error;
1500 }
1501
1502 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
1503#if TARGET_OS_OSX
1504 return LaunchInNewTerminalWithAppleScript(exe_spec.GetPath().c_str(),
1505 launch_info);
1506#else
1507 error =
1508 Status::FromErrorString("launching a process in a new terminal is not "
1509 "supported on iOS devices");
1510 return error;
1511#endif
1512 }
1513
1515
1516 auto exe_path = exe_spec.GetPath();
1517
1518 if (ShouldLaunchUsingXPC(launch_info))
1519 error = LaunchProcessXPC(exe_path.c_str(), launch_info, pid);
1520 else
1521 error = LaunchProcessPosixSpawn(exe_path.c_str(), launch_info, pid);
1522
1523 if (pid != LLDB_INVALID_PROCESS_ID) {
1524 // If all went well, then set the process ID into the launch info
1525 launch_info.SetProcessID(pid);
1526
1527 // Make sure we reap any processes we spawn or we will have zombies.
1528 bool monitoring = launch_info.MonitorProcess();
1529 UNUSED_IF_ASSERT_DISABLED(monitoring);
1530 assert(monitoring);
1531 } else {
1532 // Invalid process ID, something didn't go well
1533 if (error.Success())
1534 error =
1535 Status::FromErrorString("process launch failed for unknown reasons");
1536 }
1537 return error;
1538}
1539
1541 Status error;
1542 if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1543 FileSpec expand_tool_spec;
1544 Environment host_env = Host::GetEnvironment();
1545 std::string env_argdumper_path = host_env.lookup("LLDB_ARGDUMPER_PATH");
1546 if (!env_argdumper_path.empty()) {
1547 expand_tool_spec.SetFile(env_argdumper_path, FileSpec::Style::native);
1549 LLDB_LOGF(log,
1550 "lldb-argdumper exe path set from environment variable: %s",
1551 env_argdumper_path.c_str());
1552 }
1553 bool argdumper_exists = FileSystem::Instance().Exists(env_argdumper_path);
1554 if (!argdumper_exists) {
1555 expand_tool_spec = HostInfo::GetSupportExeDir();
1556 if (!expand_tool_spec) {
1558 "could not get support executable directory for "
1559 "lldb-argdumper tool");
1560 return error;
1561 }
1562 expand_tool_spec.AppendPathComponent("lldb-argdumper");
1563 if (!FileSystem::Instance().Exists(expand_tool_spec)) {
1565 "could not find the lldb-argdumper tool: %s",
1566 expand_tool_spec.GetPath().c_str());
1567 return error;
1568 }
1569 }
1570
1571 StreamString expand_tool_spec_stream;
1572 expand_tool_spec_stream.Printf("\"%s\"",
1573 expand_tool_spec.GetPath().c_str());
1574
1575 Args expand_command(expand_tool_spec_stream.GetData());
1576 expand_command.AppendArguments(launch_info.GetArguments());
1577
1578 int status;
1579 std::string output;
1580 FileSpec cwd(launch_info.GetWorkingDirectory());
1581 if (!FileSystem::Instance().Exists(cwd)) {
1582 char *wd = getcwd(nullptr, 0);
1583 if (wd == nullptr) {
1585 "cwd does not exist: Cannot launch with shell argument expansion");
1586 return error;
1587 } else {
1588 FileSpec working_dir(wd);
1589 free(wd);
1590 launch_info.SetWorkingDirectory(working_dir);
1591 }
1592 }
1593 bool run_in_shell = true;
1594 std::string error_output; // Pass stderr string arg so it is not mixed with
1595 // stdout.
1596 Status e =
1597 RunShellCommand(expand_command, cwd, &status, nullptr, &output,
1598 &error_output, std::chrono::seconds(10), run_in_shell);
1599
1600 if (e.Fail())
1601 return e;
1602
1603 if (status != 0) {
1605 "lldb-argdumper exited with error %d", status);
1606 return error;
1607 }
1608
1609 auto data_sp = StructuredData::ParseJSON(output);
1610 if (!data_sp) {
1611 error = Status::FromErrorString("invalid JSON");
1612 return error;
1613 }
1614
1615 auto dict_sp = data_sp->GetAsDictionary();
1616 if (!data_sp) {
1617 error = Status::FromErrorString("invalid JSON");
1618 return error;
1619 }
1620
1621 auto args_sp = dict_sp->GetObjectForDotSeparatedPath("arguments");
1622 if (!args_sp) {
1623 error = Status::FromErrorString("invalid JSON");
1624 return error;
1625 }
1626
1627 auto args_array_sp = args_sp->GetAsArray();
1628 if (!args_array_sp) {
1629 error = Status::FromErrorString("invalid JSON");
1630 return error;
1631 }
1632
1633 launch_info.GetArguments().Clear();
1634
1635 for (size_t i = 0; i < args_array_sp->GetSize(); i++) {
1636 auto item_sp = args_array_sp->GetItemAtIndex(i);
1637 if (!item_sp)
1638 continue;
1639 auto str_sp = item_sp->GetAsString();
1640 if (!str_sp)
1641 continue;
1642
1643 launch_info.GetArguments().AppendArgument(str_sp->GetValue());
1644 }
1645 }
1646
1647 return error;
1648}
1649
1650llvm::Expected<HostThread> Host::StartMonitoringChildProcess(
1651 const Host::MonitorChildProcessCallback &callback, lldb::pid_t pid) {
1652 unsigned long mask = DISPATCH_PROC_EXIT;
1653
1655
1656 dispatch_source_t source = ::dispatch_source_create(
1657 DISPATCH_SOURCE_TYPE_PROC, pid, mask,
1658 ::dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
1659
1660 LLDB_LOGF(log,
1661 "Host::StartMonitoringChildProcess(callback, pid=%i) source = %p\n",
1662 static_cast<int>(pid), static_cast<void *>(source));
1663
1664 if (source) {
1665 Host::MonitorChildProcessCallback callback_copy = callback;
1666 ::dispatch_source_set_cancel_handler(source, ^{
1667 dispatch_release(source);
1668 });
1669 ::dispatch_source_set_event_handler(source, ^{
1670
1671 int status = 0;
1672 int wait_pid = 0;
1673 wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &status, 0);
1674 if (wait_pid >= 0) {
1675 int signal = 0;
1676 int exit_status = 0;
1677 const char *status_cstr = NULL;
1678 if (WIFEXITED(status)) {
1679 exit_status = WEXITSTATUS(status);
1680 status_cstr = "EXITED";
1681 } else if (WIFSIGNALED(status)) {
1682 signal = WTERMSIG(status);
1683 status_cstr = "SIGNALED";
1684 exit_status = -1;
1685 } else {
1686 llvm_unreachable("Unknown status");
1687 }
1688
1689 LLDB_LOGF(log,
1690 "::waitpid (pid = %llu, &status, 0) => pid = %i, status "
1691 "= 0x%8.8x (%s), signal = %i, exit_status = %i",
1692 pid, wait_pid, status, status_cstr, signal, exit_status);
1693
1694 if (callback_copy)
1695 callback_copy(pid, signal, exit_status);
1696
1697 ::dispatch_source_cancel(source);
1698 }
1699 });
1700
1701 ::dispatch_resume(source);
1702 }
1703 return HostThread();
1704}
static llvm::raw_ostream & error(Stream &strm)
#define CPU_SUBTYPE_X86_64_H
#define CPU_TYPE_ARM64
#define CPU_TYPE_ARM64_32
static llvm::Error GetProcessList(std::vector< struct kinfo_proc > &kinfos)
Fetches the list of all processes into kinfos.
Definition Host.mm:691
static Status LaunchProcessPosixSpawn(const char *exe_path, const ProcessLaunchInfo &launch_info, lldb::pid_t &pid)
Definition Host.mm:1224
static os_log_t g_os_log
Definition Host.mm:106
static Status LaunchProcessXPC(const char *exe_path, ProcessLaunchInfo &launch_info, lldb::pid_t &pid)
Definition Host.mm:992
static short GetPosixspawnFlags(const ProcessLaunchInfo &launch_info)
Definition Host.mm:950
#define _POSIX_SPAWN_DISABLE_ASLR
Definition Host.mm:95
int __pthread_chdir(const char *path)
static bool GetMacOSXProcessUserAndGroup(ProcessInstanceInfo &process_info)
Definition Host.mm:657
static std::once_flag g_os_log_once
Definition Host.mm:107
static bool GetMacOSXProcessCPUType(ProcessInstanceInfo &process_info)
Definition Host.mm:503
static void finalize_xpc(void *xpc_object)
Definition Host.mm:988
int __pthread_fchdir(int fildes)
static bool ShouldLaunchUsingXPC(ProcessLaunchInfo &launch_info)
Definition Host.mm:1468
static bool AddPosixSpawnFileAction(void *_file_actions, const FileAction *info, Log *log, Status &error)
Definition Host.mm:1138
static bool GetMacOSXProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr, ProcessInstanceInfo &process_info)
Definition Host.mm:576
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:376
#define LLDB_LOGF(log,...)
Definition Log.h:390
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:406
static int setup_posix_spawn_responsible_flag(posix_spawnattr_t *attr)
int cpu_subtype_t
int cpu_type_t
An architecture specification class.
Definition ArchSpec.h:32
bool IsValid() const
Tests if this ArchSpec is valid.
Definition ArchSpec.h:452
void Clear()
Clears the object state.
Definition ArchSpec.cpp:730
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:544
bool SetArchitecture(ArchitectureType arch_type, uint32_t cpu, uint32_t sub, uint32_t os=0)
Change the architecture object type, CPU type and OS type.
uint32_t GetMachOCPUSubType() const
Definition ArchSpec.cpp:873
uint32_t GetMachOCPUType() const
Definition ArchSpec.cpp:869
Core GetCore() const
Definition ArchSpec.h:533
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition ArchSpec.cpp:740
A command line argument class.
Definition Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
void AppendArgument(llvm::StringRef arg_str, char quote_char='\0')
Appends a new argument to the end of the list argument list.
Definition Args.cpp:332
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition Args.cpp:273
const char ** GetConstArgumentVector() const
Gets the argument vector.
Definition Args.cpp:289
void Clear()
Clear the arguments.
Definition Args.cpp:388
lldb::ConnectionStatus Connect(llvm::StringRef url, Status *error_ptr) override
Connect using the connect string url.
size_t Read(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr) override
The read function that attempts to read from the connection.
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
An data extractor class.
const char * GetCStr(lldb::offset_t *offset_ptr) const
Extract a C string from *offset_ptr.
virtual const uint8_t * PeekData(lldb::offset_t offset, lldb::offset_t length) const
Peek at a bytes at offset.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
char *const * get() const
Definition Environment.h:27
static std::string compose(const value_type &KeyValue)
Definition Environment.h:80
std::pair< iterator, bool > insert(llvm::StringRef KeyEqValue)
Definition Environment.h:71
Represents a file descriptor action to be performed during process launch.
Definition FileAction.h:21
Action GetAction() const
Get the type of action.
Definition FileAction.h:59
int GetActionArgument() const
Get the action-specific argument.
Definition FileAction.h:65
const FileSpec & GetFileSpec() const
Get the file specification for open actions.
int GetFD() const
Get the file descriptor this action applies to.
Definition FileAction.h:56
A file utility class.
Definition FileSpec.h:57
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition FileSpec.cpp:174
void AppendPathComponent(llvm::StringRef component)
Definition FileSpec.cpp:452
void SetFilename(llvm::StringRef filename)
Filename string set accessor.
Definition FileSpec.cpp:359
llvm::StringRef GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:249
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:376
void Clear()
Clears the object state.
Definition FileSpec.cpp:261
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
bool Test(ValueType bit) const
Test a single flag bit.
Definition Flags.h:96
static Status LaunchProcess(ProcessLaunchInfo &launch_info)
Launch the process specified in launch_info.
static bool ResolveExecutableInBundle(FileSpec &file)
When executable files may live within a directory, where the directory represents an executable bundl...
static void SystemLog(lldb::Severity severity, llvm::StringRef message)
Emit the given message to the operating system log.
static llvm::Error OpenURL(llvm::StringRef url)
Open a URL with the host's default handler (Launch Services on macOS, xdg-open on other Unix).
static Status ShellExpandArguments(ProcessLaunchInfo &launch_info)
Perform expansion of the command-line for this launch info This can potentially involve wildcard expa...
Definition aix/Host.cpp:216
static Environment GetEnvironment()
static Status RunShellCommand(llvm::StringRef command, const FileSpec &working_dir, int *status_ptr, int *signo_ptr, std::string *command_output, std::string *error_output, const Timeout< std::micro > &timeout, bool run_in_shell=true)
Run a shell command.
static uint32_t FindProcessesImpl(const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &proc_infos)
Definition aix/Host.cpp:169
static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info)
Definition aix/Host.cpp:211
std::function< void(lldb::pid_t pid, int signal, int status)> MonitorChildProcessCallback
Definition Host.h:88
static llvm::Expected< HostThread > StartMonitoringChildProcess(const MonitorChildProcessCallback &callback, lldb::pid_t pid)
Start monitoring a child process.
static bool IsInteractiveGraphicSession()
Check if we're running in an interactive graphical session.
static bool GetBundleDirectory(const FileSpec &file, FileSpec &bundle_directory)
If you have an executable that is in a bundle and want to get back to the bundle directory from the p...
static llvm::Error OpenFileInExternalEditor(llvm::StringRef editor, const FileSpec &file_spec, uint32_t line_no)
void SetGroupID(uint32_t gid)
Definition ProcessInfo.h:58
bool ProcessIDIsValid() const
Definition ProcessInfo.h:70
void SetArg0(llvm::StringRef arg)
lldb::pid_t GetProcessID() const
Definition ProcessInfo.h:66
void SetProcessID(lldb::pid_t pid)
Definition ProcessInfo.h:68
FileSpec & GetExecutableFile()
Definition ProcessInfo.h:41
llvm::StringRef GetName() const
uint32_t GetUserID() const
Definition ProcessInfo.h:48
Environment & GetEnvironment()
Definition ProcessInfo.h:86
void SetUserID(uint32_t uid)
Definition ProcessInfo.h:56
ArchSpec & GetArchitecture()
Definition ProcessInfo.h:60
bool Matches(const ProcessInstanceInfo &proc_info) const
ProcessInstanceInfo & GetProcessInfo()
bool ProcessIDsMatch(const ProcessInstanceInfo &proc_info) const
Return true iff the process ID and parent process IDs in this object match the ones in proc_info.
bool UserIDsMatch(const ProcessInstanceInfo &proc_info) const
Return true iff the (both effective and real) user and group IDs in this object match the ones in pro...
void SetEffectiveGroupID(uint32_t gid)
void SetParentProcessID(lldb::pid_t pid)
void SetEffectiveUserID(uint32_t uid)
const FileSpec & GetShell() const
const FileAction * GetFileActionAtIndex(size_t idx) const
const FileAction * GetFileActionForFD(int fd) const
void SetWorkingDirectory(const FileSpec &working_dir)
const FileSpec & GetWorkingDirectory() const
An error handling class.
Definition Status.h:118
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Fail() const
Test for error condition.
Definition Status.cpp:293
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition Status.h:151
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:136
bool Success() const
Test for success condition.
Definition Status.cpp:303
const char * GetData() const
llvm::StringRef GetString() const
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:370
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
static ObjectSP ParseJSON(llvm::StringRef json_text)
static llvm::Expected< HostThread > LaunchThread(llvm::StringRef name, std::function< lldb::thread_result_t()> thread_function, size_t min_stack_byte_size=0)
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define LLDB_INVALID_CPUTYPE
#define UNUSED_IF_ASSERT_DISABLED(x)
#define UINT32_MAX
#define LLDB_INVALID_PROCESS_ID
lldb::ByteOrder InlHostByteOrder()
Definition Endian.h:25
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:339
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
Definition Host.h:32
Severity
Used for expressing severity in logs and diagnostics.
void * thread_result_t
Definition lldb-types.h:62
ConnectionStatus
Connection Status Types.
@ eConnectionStatusSuccess
Success.
uint64_t offset_t
Definition lldb-types.h:85
@ eErrorTypeGeneric
Generic errors that can be any value.
@ eErrorTypePOSIX
POSIX error codes.
uint64_t pid_t
Definition lldb-types.h:83
#define PATH_MAX