xref: /llvm-project/lldb/source/Host/common/MonitoringProcessLauncher.cpp (revision 172d37d3b92ca70e65f11f9a7caf7af4c8a5e44f)
1 //===-- ProcessLauncherWindows.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/Core/Error.h"
11 #include "lldb/Core/Log.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Host/HostProcess.h"
14 #include "lldb/Host/MonitoringProcessLauncher.h"
15 #include "lldb/Target/Platform.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Target/ProcessLaunchInfo.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 MonitoringProcessLauncher::MonitoringProcessLauncher(std::unique_ptr<ProcessLauncher> delegate_launcher)
23     : m_delegate_launcher(std::move(delegate_launcher))
24 {
25 }
26 
27 HostProcess
28 MonitoringProcessLauncher::LaunchProcess(const ProcessLaunchInfo &launch_info, Error &error)
29 {
30     ProcessLaunchInfo resolved_info(launch_info);
31 
32     error.Clear();
33     char exe_path[PATH_MAX];
34 
35     PlatformSP host_platform_sp(Platform::GetHostPlatform());
36 
37     const ArchSpec &arch_spec = resolved_info.GetArchitecture();
38 
39     FileSpec exe_spec(resolved_info.GetExecutableFile());
40 
41     FileSpec::FileType file_type = exe_spec.GetFileType();
42     if (file_type != FileSpec::eFileTypeRegular)
43     {
44         lldb::ModuleSP exe_module_sp;
45         error = host_platform_sp->ResolveExecutable(exe_spec, arch_spec, exe_module_sp, NULL);
46 
47         if (error.Fail())
48             return HostProcess();
49 
50         if (exe_module_sp)
51             exe_spec = exe_module_sp->GetFileSpec();
52     }
53 
54     if (exe_spec.Exists())
55     {
56         exe_spec.GetPath(exe_path, sizeof(exe_path));
57     }
58     else
59     {
60         resolved_info.GetExecutableFile().GetPath(exe_path, sizeof(exe_path));
61         error.SetErrorStringWithFormat("executable doesn't exist: '%s'", exe_path);
62         return HostProcess();
63     }
64 
65     resolved_info.SetExecutableFile(exe_spec, false);
66     assert(!resolved_info.GetFlags().Test(eLaunchFlagLaunchInTTY));
67 
68     ::pid_t pid = LLDB_INVALID_PROCESS_ID;
69 
70     HostProcess process = m_delegate_launcher->LaunchProcess(resolved_info, error);
71 
72     if (process.GetProcessId() != LLDB_INVALID_PROCESS_ID)
73     {
74         Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
75 
76         Host::MonitorChildProcessCallback callback = launch_info.GetMonitorProcessCallback();
77 
78         void *baton = nullptr;
79         bool monitor_signals = false;
80         if (callback)
81         {
82             // If the ProcessLaunchInfo specified a callback, use that.
83             baton = launch_info.GetMonitorProcessBaton();
84             monitor_signals = launch_info.GetMonitorSignals();
85         }
86         else
87         {
88             callback = Process::SetProcessExitStatus;
89         }
90 
91         process.StartMonitoring(callback, baton, monitor_signals);
92         if (log)
93             log->PutCString("started monitoring child process.");
94     }
95     else
96     {
97         // Invalid process ID, something didn't go well
98         if (error.Success())
99             error.SetErrorString("process launch failed for unknown reasons");
100     }
101     return process;
102 }
103