LLDB  mainline
MonitoringProcessLauncher.cpp
Go to the documentation of this file.
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 
10 #include "lldb/Host/FileSystem.h"
11 #include "lldb/Host/HostProcess.h"
13 #include "lldb/Utility/Log.h"
14 
15 #include "llvm/Support/FileSystem.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 MonitoringProcessLauncher::MonitoringProcessLauncher(
21  std::unique_ptr<ProcessLauncher> delegate_launcher)
22  : m_delegate_launcher(std::move(delegate_launcher)) {}
23 
26  Status &error) {
27  ProcessLaunchInfo resolved_info(launch_info);
28 
29  error.Clear();
30 
32  FileSpec exe_spec(resolved_info.GetExecutableFile());
33 
34  if (!fs.Exists(exe_spec))
35  FileSystem::Instance().Resolve(exe_spec);
36 
37  if (!fs.Exists(exe_spec))
39 
40  if (!fs.Exists(exe_spec)) {
41  error.SetErrorStringWithFormatv("executable doesn't exist: '{0}'",
42  exe_spec);
43  return HostProcess();
44  }
45 
46  resolved_info.SetExecutableFile(exe_spec, false);
47  assert(!resolved_info.GetFlags().Test(eLaunchFlagLaunchInTTY));
48 
49  HostProcess process =
50  m_delegate_launcher->LaunchProcess(resolved_info, error);
51 
52  if (process.GetProcessId() != LLDB_INVALID_PROCESS_ID) {
54 
55  assert(launch_info.GetMonitorProcessCallback());
56  llvm::Expected<HostThread> maybe_thread =
57  process.StartMonitoring(launch_info.GetMonitorProcessCallback(),
58  launch_info.GetMonitorSignals());
59  if (!maybe_thread)
60  error.SetErrorStringWithFormatv("failed to launch host thread: {}",
61  llvm::toString(maybe_thread.takeError()));
62  if (log)
63  log->PutCString("started monitoring child process.");
64  } else {
65  // Invalid process ID, something didn't go well
66  if (error.Success())
67  error.SetErrorString("process launch failed for unknown reasons");
68  }
69  return process;
70 }
lldb_private::toString
const char * toString(AppleArm64ExceptionClass EC)
Definition: AppleArm64ExceptionClass.h:38
lldb_private::HostProcess
Definition: HostProcess.h:33
FileSystem.h
lldb_private::ProcessInfo::SetExecutableFile
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:62
LLDB_INVALID_PROCESS_ID
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:93
lldb_private::ProcessLaunchInfo::GetMonitorProcessCallback
Host::MonitorChildProcessCallback GetMonitorProcessCallback() const
Definition: ProcessLaunchInfo.h:106
ProcessLaunchInfo.h
HostProcess.h
lldb_private::FileSystem::ResolveExecutableLocation
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
Definition: common/FileSystem.cpp:330
lldb_private::Flags::Test
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
lldb_private::FileSystem::Exists
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
Definition: common/FileSystem.cpp:170
lldb_private::ProcessLaunchInfo::GetFlags
Flags & GetFlags()
Definition: ProcessLaunchInfo.h:64
lldb_private::FileSystem
Definition: FileSystem.h:29
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ProcessLaunchInfo
Definition: ProcessLaunchInfo.h:31
Log.h
lldb_private::HostProcess::StartMonitoring
llvm::Expected< HostThread > StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals)
Definition: HostProcess.cpp:36
lldb_private::MonitoringProcessLauncher::LaunchProcess
HostProcess LaunchProcess(const ProcessLaunchInfo &launch_info, Status &error) override
Launch the process specified in launch_info.
Definition: MonitoringProcessLauncher.cpp:25
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::FileSystem::Resolve
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
Definition: common/FileSystem.cpp:255
lldb_private::HostProcess::GetProcessId
lldb::pid_t GetProcessId() const
Definition: HostProcess.cpp:29
MonitoringProcessLauncher.h
lldb_private::ProcessLaunchInfo::GetMonitorSignals
bool GetMonitorSignals() const
Definition: ProcessLaunchInfo.h:116
lldb_private::Status
Definition: Status.h:44
lldb_private::MonitoringProcessLauncher::m_delegate_launcher
std::unique_ptr< ProcessLauncher > m_delegate_launcher
Definition: MonitoringProcessLauncher.h:29
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:45
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ProcessInfo::GetExecutableFile
FileSpec & GetExecutableFile()
Definition: ProcessInfo.h:42
lldb_private::Log
Definition: Log.h:49
lldb
Definition: SBAddress.h:15
LIBLLDB_LOG_PROCESS
#define LIBLLDB_LOG_PROCESS
Definition: Logging.h:15
lldb_private::Log::PutCString
void PutCString(const char *cstr)
Definition: Log.cpp:118