LLDB mainline
ProcessLaunchInfo.h
Go to the documentation of this file.
1//===-- ProcessLaunchInfo.h -------------------------------------*- C++ -*-===//
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
9#ifndef LLDB_HOST_PROCESSLAUNCHINFO_H
10#define LLDB_HOST_PROCESSLAUNCHINFO_H
11
12// C++ Headers
13#include <string>
14
15// LLDB Headers
16#include "lldb/Utility/Flags.h"
17
19#include "lldb/Host/Host.h"
20#ifdef _WIN32
23#else
25#endif
28
29namespace lldb_private {
30
31#if defined(_WIN32)
32using PTY = PseudoConsole;
34#else
37#endif
38
39// ProcessLaunchInfo
40//
41// Describes any information that is required to launch a process.
42
44public:
46
47 ProcessLaunchInfo(const FileSpec &stdin_file_spec,
48 const FileSpec &stdout_file_spec,
49 const FileSpec &stderr_file_spec,
50 const FileSpec &working_dir, uint32_t launch_flags);
51
52 void AppendFileAction(const FileActionImpl &info) {
53 m_file_actions.push_back(info);
54 }
55
56 bool AppendCloseFileAction(int fd);
57
58 bool AppendDuplicateFileAction(int fd, int dup_fd);
59
60#ifdef _WIN32
62#endif
63
64 bool AppendOpenFileAction(int fd, const FileSpec &file_spec, bool read,
65 bool write);
66
67 bool AppendSuppressFileAction(int fd, bool read, bool write);
68
69 // Redirect stdin/stdout/stderr to a pty, if no action for the respective file
70 // descriptor is specified. (So if stdin and stdout already have file actions,
71 // but stderr doesn't, then only stderr will be redirected to a pty.)
72 llvm::Error SetUpPtyRedirection();
73
74#ifdef _WIN32
75 // Redirect stdin/stdout/stderr to anonymous pipes instead of a ConPTY.
76 // Used when terminal emulation is not needed (e.g. lldb-dap internalConsole).
77 llvm::Error SetUpPipeRedirection();
78#endif
79
80 bool HasPTY() const { return m_pty != nullptr; }
81
82 size_t GetNumFileActions() const { return m_file_actions.size(); }
83
84 const FileAction *GetFileActionAtIndex(size_t idx) const;
85
86 const FileAction *GetFileActionForFD(int fd) const;
87
88 Flags &GetFlags() { return m_flags; }
89
90 const Flags &GetFlags() const { return m_flags; }
91
92 const FileSpec &GetWorkingDirectory() const;
93
94 void SetWorkingDirectory(const FileSpec &working_dir);
95
96 llvm::StringRef GetProcessPluginName() const;
97
98 void SetProcessPluginName(llvm::StringRef plugin);
99
100 const FileSpec &GetShell() const;
101
102 void SetShell(const FileSpec &shell);
103
104 uint32_t GetResumeCount() const { return m_resume_count; }
105
106 void SetResumeCount(uint32_t c) { m_resume_count = c; }
107
109 return m_flags.Test(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
110 }
111
113
115 return m_flags.Test(lldb::eLaunchFlagShellExpandArguments);
116 }
117
118 void SetShellExpandArguments(bool expand);
119
120 void Clear();
121
122 bool ConvertArgumentsForLaunchingInShell(Status &error, bool will_debug,
123 bool first_arg_is_full_shell_command,
124 uint32_t num_resumes);
125
127 m_monitor_callback = std::move(callback);
128 }
129
133
134 /// A Monitor callback which does not take any action on process events. Use
135 /// this if you don't need to take any particular action when the process
136 /// terminates, but you still need to reap it.
137 static void NoOpMonitorCallback(lldb::pid_t pid, int signal, int status);
138
139 // If the LaunchInfo has a monitor callback, then arrange to monitor the
140 // process. Return true if the LaunchInfo has taken care of monitoring the
141 // process, and false if the caller might want to monitor the process
142 // themselves.
143
144 bool MonitorProcess() const;
145
146 PTY &GetPTY() const { return *m_pty; }
147
148 std::shared_ptr<PTY> TakePTY() { return std::move(m_pty); }
149
150 /// Returns whether if lldb should read information from the PTY. This is
151 /// always true on non Windows.
152 bool ShouldUsePTY() const {
153#ifdef _WIN32
154 if (!m_pty)
155 return false;
156 return GetPTY().GetMode() != PseudoConsole::Mode::None &&
157 GetNumFileActions() == 0;
158#else
159 return true;
160#endif
161 }
162
163 void SetLaunchEventData(const char *data) { m_event_data.assign(data); }
164
165 const char *GetLaunchEventData() const { return m_event_data.c_str(); }
166
167 void SetDetachOnError(bool enable);
168
169 bool GetDetachOnError() const {
170 return m_flags.Test(lldb::eLaunchFlagDetachOnError);
171 }
172
173protected:
175 std::string m_plugin_name;
177 Flags m_flags; // Bitwise OR of bits from lldb::LaunchFlags
178 std::vector<FileActionImpl>
179 m_file_actions; // File actions for any other files
180 std::shared_ptr<PTY> m_pty;
181 uint32_t m_resume_count = 0; // How many times do we resume after launching
183 std::string m_event_data; // A string passed to the plugin launch, having no
184 // meaning to the upper levels of lldb.
185};
186}
187
188#endif // LLDB_HOST_PROCESSLAUNCHINFO_H
static llvm::raw_ostream & error(Stream &strm)
void * HANDLE
static bool separate(size_t count)
Definition UUID.cpp:29
Represents a file descriptor action to be performed during process launch.
Definition FileAction.h:21
A file utility class.
Definition FileSpec.h:57
A class to manage flags.
Definition Flags.h:22
std::function< void(lldb::pid_t pid, int signal, int status)> MonitorChildProcessCallback
Definition Host.h:88
llvm::StringRef GetProcessPluginName() const
const FileSpec & GetShell() const
void AppendFileAction(const FileActionImpl &info)
const char * GetLaunchEventData() const
bool AppendOpenFileAction(int fd, const FileSpec &file_spec, bool read, bool write)
void SetLaunchEventData(const char *data)
bool AppendSuppressFileAction(int fd, bool read, bool write)
static void NoOpMonitorCallback(lldb::pid_t pid, int signal, int status)
A Monitor callback which does not take any action on process events.
const FileAction * GetFileActionAtIndex(size_t idx) const
std::vector< FileActionImpl > m_file_actions
void SetShell(const FileSpec &shell)
const Host::MonitorChildProcessCallback & GetMonitorProcessCallback() const
const FileAction * GetFileActionForFD(int fd) const
void SetProcessPluginName(llvm::StringRef plugin)
void SetMonitorProcessCallback(Host::MonitorChildProcessCallback callback)
bool ConvertArgumentsForLaunchingInShell(Status &error, bool will_debug, bool first_arg_is_full_shell_command, uint32_t num_resumes)
bool AppendDuplicateFileAction(int fd, int dup_fd)
void SetLaunchInSeparateProcessGroup(bool separate)
std::shared_ptr< PTY > TakePTY()
bool ShouldUsePTY() const
Returns whether if lldb should read information from the PTY.
void SetWorkingDirectory(const FileSpec &working_dir)
Host::MonitorChildProcessCallback m_monitor_callback
const FileSpec & GetWorkingDirectory() const
A pseudo terminal helper class.
An error handling class.
Definition Status.h:118
A Windows-specific extension of FileAction that supports HANDLE-based file operations in addition to ...
A class that represents a running process on the host machine.
PseudoTerminal PTY
FileAction FileActionImpl
uint64_t pid_t
Definition lldb-types.h:83