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/Core/Module.h" 12 #include "lldb/Core/ModuleSpec.h" 13 #include "lldb/Host/HostProcess.h" 14 #include "lldb/Target/Platform.h" 15 #include "lldb/Target/Process.h" 16 #include "lldb/Target/ProcessLaunchInfo.h" 17 #include "lldb/Utility/Error.h" 18 #include "lldb/Utility/Log.h" 19 20 #include "llvm/Support/FileSystem.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 MonitoringProcessLauncher::MonitoringProcessLauncher( 26 std::unique_ptr<ProcessLauncher> delegate_launcher) 27 : m_delegate_launcher(std::move(delegate_launcher)) {} 28 29 HostProcess 30 MonitoringProcessLauncher::LaunchProcess(const ProcessLaunchInfo &launch_info, 31 Error &error) { 32 ProcessLaunchInfo resolved_info(launch_info); 33 34 error.Clear(); 35 char exe_path[PATH_MAX]; 36 37 PlatformSP host_platform_sp(Platform::GetHostPlatform()); 38 39 const ArchSpec &arch_spec = resolved_info.GetArchitecture(); 40 41 FileSpec exe_spec(resolved_info.GetExecutableFile()); 42 43 llvm::sys::fs::file_status stats; 44 if (status(exe_spec.GetPath(), stats) || !is_regular_file(stats)) { 45 ModuleSpec module_spec(exe_spec, arch_spec); 46 lldb::ModuleSP exe_module_sp; 47 error = 48 host_platform_sp->ResolveExecutable(module_spec, exe_module_sp, NULL); 49 50 if (error.Fail()) 51 return HostProcess(); 52 53 if (exe_module_sp) 54 exe_spec = exe_module_sp->GetFileSpec(); 55 } 56 57 if (exists(stats)) { 58 exe_spec.GetPath(exe_path, sizeof(exe_path)); 59 } else { 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 HostProcess process = 69 m_delegate_launcher->LaunchProcess(resolved_info, error); 70 71 if (process.GetProcessId() != LLDB_INVALID_PROCESS_ID) { 72 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 73 74 Host::MonitorChildProcessCallback callback = 75 launch_info.GetMonitorProcessCallback(); 76 77 bool monitor_signals = false; 78 if (callback) { 79 // If the ProcessLaunchInfo specified a callback, use that. 80 monitor_signals = launch_info.GetMonitorSignals(); 81 } else { 82 callback = Process::SetProcessExitStatus; 83 } 84 85 process.StartMonitoring(callback, monitor_signals); 86 if (log) 87 log->PutCString("started monitoring child process."); 88 } else { 89 // Invalid process ID, something didn't go well 90 if (error.Success()) 91 error.SetErrorString("process launch failed for unknown reasons"); 92 } 93 return process; 94 } 95