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 /// Returns true if fd has an explicit file action, or is the destination of a
89 /// duplicate action.
90 bool IsFDRedirected(int fd) const;
91
92 Flags &GetFlags() { return m_flags; }
93
94 const Flags &GetFlags() const { return m_flags; }
95
96 const FileSpec &GetWorkingDirectory() const;
97
98 void SetWorkingDirectory(const FileSpec &working_dir);
99
100 llvm::StringRef GetProcessPluginName() const;
101
102 void SetProcessPluginName(llvm::StringRef plugin);
103
104 const FileSpec &GetShell() const;
105
106 void SetShell(const FileSpec &shell);
107
108 uint32_t GetResumeCount() const { return m_resume_count; }
109
110 void SetResumeCount(uint32_t c) { m_resume_count = c; }
111
113 return m_flags.Test(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
114 }
115
117
119 return m_flags.Test(lldb::eLaunchFlagShellExpandArguments);
120 }
121
122 void SetShellExpandArguments(bool expand);
123
124 void Clear();
125
126 bool ConvertArgumentsForLaunchingInShell(Status &error, bool will_debug,
127 bool first_arg_is_full_shell_command,
128 uint32_t num_resumes);
129
131 m_monitor_callback = std::move(callback);
132 }
133
137
138 /// A Monitor callback which does not take any action on process events. Use
139 /// this if you don't need to take any particular action when the process
140 /// terminates, but you still need to reap it.
141 static void NoOpMonitorCallback(lldb::pid_t pid, int signal, int status);
142
143 // If the LaunchInfo has a monitor callback, then arrange to monitor the
144 // process. Return true if the LaunchInfo has taken care of monitoring the
145 // process, and false if the caller might want to monitor the process
146 // themselves.
147
148 bool MonitorProcess() const;
149
150 PTY &GetPTY() const { return *m_pty; }
151
152 std::shared_ptr<PTY> TakePTY() { return std::move(m_pty); }
153
154 /// Returns whether if lldb should read information from the PTY. This is
155 /// always true on non Windows.
156 bool ShouldUsePTY() const {
157#ifdef _WIN32
158 if (!m_pty)
159 return false;
160 return GetPTY().GetMode() != PseudoConsole::Mode::None &&
161 GetNumFileActions() == 0;
162#else
163 return true;
164#endif
165 }
166
167 void SetLaunchEventData(const char *data) { m_event_data.assign(data); }
168
169 const char *GetLaunchEventData() const { return m_event_data.c_str(); }
170
171 void SetDetachOnError(bool enable);
172
173 bool GetDetachOnError() const {
174 return m_flags.Test(lldb::eLaunchFlagDetachOnError);
175 }
176
177protected:
179 std::string m_plugin_name;
181 Flags m_flags; // Bitwise OR of bits from lldb::LaunchFlags
182 std::vector<FileActionImpl>
183 m_file_actions; // File actions for any other files
184 std::shared_ptr<PTY> m_pty;
185 uint32_t m_resume_count = 0; // How many times do we resume after launching
187 std::string m_event_data; // A string passed to the plugin launch, having no
188 // meaning to the upper levels of lldb.
189};
190}
191
192#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)
bool IsFDRedirected(int fd) const
Returns true if fd has an explicit file action, or is the destination of a duplicate action.
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