xref: /openbsd-src/gnu/llvm/lldb/source/Host/common/MonitoringProcessLauncher.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- MonitoringProcessLauncher.cpp -------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Host/MonitoringProcessLauncher.h"
10061da546Spatrick #include "lldb/Host/FileSystem.h"
11061da546Spatrick #include "lldb/Host/HostProcess.h"
12061da546Spatrick #include "lldb/Host/ProcessLaunchInfo.h"
13*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
14061da546Spatrick #include "lldb/Utility/Log.h"
15061da546Spatrick 
16061da546Spatrick #include "llvm/Support/FileSystem.h"
17061da546Spatrick 
18061da546Spatrick using namespace lldb;
19061da546Spatrick using namespace lldb_private;
20061da546Spatrick 
MonitoringProcessLauncher(std::unique_ptr<ProcessLauncher> delegate_launcher)21061da546Spatrick MonitoringProcessLauncher::MonitoringProcessLauncher(
22061da546Spatrick     std::unique_ptr<ProcessLauncher> delegate_launcher)
23061da546Spatrick     : m_delegate_launcher(std::move(delegate_launcher)) {}
24061da546Spatrick 
25061da546Spatrick HostProcess
LaunchProcess(const ProcessLaunchInfo & launch_info,Status & error)26061da546Spatrick MonitoringProcessLauncher::LaunchProcess(const ProcessLaunchInfo &launch_info,
27061da546Spatrick                                          Status &error) {
28061da546Spatrick   ProcessLaunchInfo resolved_info(launch_info);
29061da546Spatrick 
30061da546Spatrick   error.Clear();
31061da546Spatrick 
32061da546Spatrick   FileSystem &fs = FileSystem::Instance();
33061da546Spatrick   FileSpec exe_spec(resolved_info.GetExecutableFile());
34061da546Spatrick 
35061da546Spatrick   if (!fs.Exists(exe_spec))
36061da546Spatrick     FileSystem::Instance().Resolve(exe_spec);
37061da546Spatrick 
38061da546Spatrick   if (!fs.Exists(exe_spec))
39061da546Spatrick     FileSystem::Instance().ResolveExecutableLocation(exe_spec);
40061da546Spatrick 
41061da546Spatrick   if (!fs.Exists(exe_spec)) {
42061da546Spatrick     error.SetErrorStringWithFormatv("executable doesn't exist: '{0}'",
43061da546Spatrick                                     exe_spec);
44061da546Spatrick     return HostProcess();
45061da546Spatrick   }
46061da546Spatrick 
47061da546Spatrick   resolved_info.SetExecutableFile(exe_spec, false);
48061da546Spatrick   assert(!resolved_info.GetFlags().Test(eLaunchFlagLaunchInTTY));
49061da546Spatrick 
50061da546Spatrick   HostProcess process =
51061da546Spatrick       m_delegate_launcher->LaunchProcess(resolved_info, error);
52061da546Spatrick 
53061da546Spatrick   if (process.GetProcessId() != LLDB_INVALID_PROCESS_ID) {
54*f6aab3d8Srobert     Log *log = GetLog(LLDBLog::Process);
55061da546Spatrick 
56061da546Spatrick     assert(launch_info.GetMonitorProcessCallback());
57061da546Spatrick     llvm::Expected<HostThread> maybe_thread =
58*f6aab3d8Srobert         process.StartMonitoring(launch_info.GetMonitorProcessCallback());
59061da546Spatrick     if (!maybe_thread)
60061da546Spatrick       error.SetErrorStringWithFormatv("failed to launch host thread: {}",
61061da546Spatrick                                       llvm::toString(maybe_thread.takeError()));
62061da546Spatrick     if (log)
63061da546Spatrick       log->PutCString("started monitoring child process.");
64061da546Spatrick   } else {
65061da546Spatrick     // Invalid process ID, something didn't go well
66061da546Spatrick     if (error.Success())
67061da546Spatrick       error.SetErrorString("process launch failed for unknown reasons");
68061da546Spatrick   }
69061da546Spatrick   return process;
70061da546Spatrick }
71