1 //===-- MonitoringProcessLauncher.cpp ---------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/MonitoringProcessLauncher.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Host/HostProcess.h"
14 #include "lldb/Target/Platform.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/ProcessLaunchInfo.h"
17 #include "lldb/Utility/Log.h"
18 #include "lldb/Utility/Status.h"
20 #include "llvm/Support/FileSystem.h"
23 using namespace lldb_private;
25 MonitoringProcessLauncher::MonitoringProcessLauncher(
26 std::unique_ptr<ProcessLauncher> delegate_launcher)
27 : m_delegate_launcher(std::move(delegate_launcher)) {}
30 MonitoringProcessLauncher::LaunchProcess(const ProcessLaunchInfo &launch_info,
32 ProcessLaunchInfo resolved_info(launch_info);
35 char exe_path[PATH_MAX];
37 PlatformSP host_platform_sp(Platform::GetHostPlatform());
39 const ArchSpec &arch_spec = resolved_info.GetArchitecture();
41 FileSpec exe_spec(resolved_info.GetExecutableFile());
43 llvm::sys::fs::file_status stats;
44 status(exe_spec.GetPath(), stats);
45 if (!is_regular_file(stats)) {
46 ModuleSpec module_spec(exe_spec, arch_spec);
47 lldb::ModuleSP exe_module_sp;
49 host_platform_sp->ResolveExecutable(module_spec, exe_module_sp, NULL);
55 exe_spec = exe_module_sp->GetFileSpec();
56 status(exe_spec.GetPath(), stats);
61 exe_spec.GetPath(exe_path, sizeof(exe_path));
63 resolved_info.GetExecutableFile().GetPath(exe_path, sizeof(exe_path));
64 error.SetErrorStringWithFormat("executable doesn't exist: '%s'", exe_path);
68 resolved_info.SetExecutableFile(exe_spec, false);
69 assert(!resolved_info.GetFlags().Test(eLaunchFlagLaunchInTTY));
72 m_delegate_launcher->LaunchProcess(resolved_info, error);
74 if (process.GetProcessId() != LLDB_INVALID_PROCESS_ID) {
75 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
77 Host::MonitorChildProcessCallback callback =
78 launch_info.GetMonitorProcessCallback();
80 bool monitor_signals = false;
82 // If the ProcessLaunchInfo specified a callback, use that.
83 monitor_signals = launch_info.GetMonitorSignals();
85 callback = Process::SetProcessExitStatus;
88 process.StartMonitoring(callback, monitor_signals);
90 log->PutCString("started monitoring child process.");
92 // Invalid process ID, something didn't go well
94 error.SetErrorString("process launch failed for unknown reasons");