xref: /llvm-project/lldb/source/Host/common/MonitoringProcessLauncher.cpp (revision 0bbe9a7a98083f9ea036f602e0bed65e2f3ccc6d)
1 //===-- MonitoringProcessLauncher.cpp ---------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Host/MonitoringProcessLauncher.h"
11 #include "lldb/Host/FileSystem.h"
12 #include "lldb/Host/HostProcess.h"
13 #include "lldb/Target/ProcessLaunchInfo.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(exe_spec))
36     FileSystem::Instance().Resolve(exe_spec);
37 
38   if (!fs.Exists(exe_spec))
39     FileSystem::Instance().ResolveExecutableLocation(exe_spec);
40 
41   if (!fs.Exists(exe_spec)) {
42     error.SetErrorStringWithFormatv("executable doesn't exist: '{0}'",
43                                     exe_spec);
44     return HostProcess();
45   }
46 
47   resolved_info.SetExecutableFile(exe_spec, false);
48   assert(!resolved_info.GetFlags().Test(eLaunchFlagLaunchInTTY));
49 
50   HostProcess process =
51       m_delegate_launcher->LaunchProcess(resolved_info, error);
52 
53   if (process.GetProcessId() != LLDB_INVALID_PROCESS_ID) {
54     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
55 
56     assert(launch_info.GetMonitorProcessCallback());
57     process.StartMonitoring(launch_info.GetMonitorProcessCallback(),
58                             launch_info.GetMonitorSignals());
59     if (log)
60       log->PutCString("started monitoring child process.");
61   } else {
62     // Invalid process ID, something didn't go well
63     if (error.Success())
64       error.SetErrorString("process launch failed for unknown reasons");
65   }
66   return process;
67 }
68