| 1 | //===-- MonitoringProcessLauncher.cpp -------------------------------------===// |
|---|---|
| 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/MonitoringProcessLauncher.h" |
| 10 | #include "lldb/Host/FileSystem.h" |
| 11 | #include "lldb/Host/HostProcess.h" |
| 12 | #include "lldb/Host/ProcessLaunchInfo.h" |
| 13 | #include "lldb/Utility/LLDBLog.h" |
| 14 | #include "lldb/Utility/Log.h" |
| 15 | |
| 16 | #include "llvm/Support/FileSystem.h" |
| 17 | |
| 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
| 21 | MonitoringProcessLauncher::MonitoringProcessLauncher( |
| 22 | std::unique_ptr<ProcessLauncher> delegate_launcher) |
| 23 | : m_delegate_launcher(std::move(delegate_launcher)) {} |
| 24 | |
| 25 | HostProcess |
| 26 | MonitoringProcessLauncher::LaunchProcess(const ProcessLaunchInfo &launch_info, |
| 27 | Status &error) { |
| 28 | ProcessLaunchInfo resolved_info(launch_info); |
| 29 | |
| 30 | error.Clear(); |
| 31 | |
| 32 | FileSystem &fs = FileSystem::Instance(); |
| 33 | FileSpec exe_spec(resolved_info.GetExecutableFile()); |
| 34 | |
| 35 | if (!fs.Exists(file_spec: exe_spec)) |
| 36 | FileSystem::Instance().Resolve(file_spec&: exe_spec); |
| 37 | |
| 38 | if (!fs.Exists(file_spec: exe_spec)) |
| 39 | FileSystem::Instance().ResolveExecutableLocation(file_spec&: exe_spec); |
| 40 | |
| 41 | if (!fs.Exists(file_spec: exe_spec)) { |
| 42 | error = Status::FromErrorStringWithFormatv( |
| 43 | format: "executable doesn't exist: '{0}'", args&: exe_spec); |
| 44 | return HostProcess(); |
| 45 | } |
| 46 | |
| 47 | resolved_info.SetExecutableFile(exe_file: exe_spec, add_exe_file_as_first_arg: false); |
| 48 | assert(!resolved_info.GetFlags().Test(eLaunchFlagLaunchInTTY)); |
| 49 | |
| 50 | HostProcess process = |
| 51 | m_delegate_launcher->LaunchProcess(launch_info: resolved_info, error); |
| 52 | |
| 53 | if (process.GetProcessId() != LLDB_INVALID_PROCESS_ID) { |
| 54 | Log *log = GetLog(mask: LLDBLog::Process); |
| 55 | |
| 56 | assert(launch_info.GetMonitorProcessCallback()); |
| 57 | llvm::Expected<HostThread> maybe_thread = |
| 58 | process.StartMonitoring(callback: launch_info.GetMonitorProcessCallback()); |
| 59 | if (!maybe_thread) |
| 60 | error = Status::FromErrorStringWithFormatv( |
| 61 | format: "failed to launch host thread: {}", |
| 62 | args: llvm::toString(E: maybe_thread.takeError())); |
| 63 | if (log) |
| 64 | log->PutCString(cstr: "started monitoring child process."); |
| 65 | } else { |
| 66 | // Invalid process ID, something didn't go well |
| 67 | if (error.Success()) |
| 68 | error = |
| 69 | Status::FromErrorString(str: "process launch failed for unknown reasons"); |
| 70 | } |
| 71 | return process; |
| 72 | } |
| 73 |
