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