• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include "lldb/Host/FileAction.h"
19 #include "lldb/Host/Host.h"
20 #include "lldb/Host/PseudoTerminal.h"
21 #include "lldb/Utility/FileSpec.h"
22 #include "lldb/Utility/ProcessInfo.h"
23 
24 namespace lldb_private {
25 
26 // ProcessLaunchInfo
27 //
28 // Describes any information that is required to launch a process.
29 
30 class ProcessLaunchInfo : public ProcessInfo {
31 public:
32   ProcessLaunchInfo();
33 
34   ProcessLaunchInfo(const FileSpec &stdin_file_spec,
35                     const FileSpec &stdout_file_spec,
36                     const FileSpec &stderr_file_spec,
37                     const FileSpec &working_dir, uint32_t launch_flags);
38 
AppendFileAction(const FileAction & info)39   void AppendFileAction(const FileAction &info) {
40     m_file_actions.push_back(info);
41   }
42 
43   bool AppendCloseFileAction(int fd);
44 
45   bool AppendDuplicateFileAction(int fd, int dup_fd);
46 
47   bool AppendOpenFileAction(int fd, const FileSpec &file_spec, bool read,
48                             bool write);
49 
50   bool AppendSuppressFileAction(int fd, bool read, bool write);
51 
52   // Redirect stdin/stdout/stderr to a pty, if no action for the respective file
53   // descriptor is specified. (So if stdin and stdout already have file actions,
54   // but stderr doesn't, then only stderr will be redirected to a pty.)
55   llvm::Error SetUpPtyRedirection();
56 
GetNumFileActions()57   size_t GetNumFileActions() const { return m_file_actions.size(); }
58 
59   const FileAction *GetFileActionAtIndex(size_t idx) const;
60 
61   const FileAction *GetFileActionForFD(int fd) const;
62 
GetFlags()63   Flags &GetFlags() { return m_flags; }
64 
GetFlags()65   const Flags &GetFlags() const { return m_flags; }
66 
67   const FileSpec &GetWorkingDirectory() const;
68 
69   void SetWorkingDirectory(const FileSpec &working_dir);
70 
71   const char *GetProcessPluginName() const;
72 
73   void SetProcessPluginName(llvm::StringRef plugin);
74 
75   const FileSpec &GetShell() const;
76 
77   void SetShell(const FileSpec &shell);
78 
GetResumeCount()79   uint32_t GetResumeCount() const { return m_resume_count; }
80 
SetResumeCount(uint32_t c)81   void SetResumeCount(uint32_t c) { m_resume_count = c; }
82 
GetLaunchInSeparateProcessGroup()83   bool GetLaunchInSeparateProcessGroup() const {
84     return m_flags.Test(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
85   }
86 
87   void SetLaunchInSeparateProcessGroup(bool separate);
88 
GetShellExpandArguments()89   bool GetShellExpandArguments() const {
90     return m_flags.Test(lldb::eLaunchFlagShellExpandArguments);
91   }
92 
93   void SetShellExpandArguments(bool expand);
94 
95   void Clear();
96 
97   bool ConvertArgumentsForLaunchingInShell(Status &error, bool will_debug,
98                                            bool first_arg_is_full_shell_command,
99                                            uint32_t num_resumes);
100 
101   void
102   SetMonitorProcessCallback(const Host::MonitorChildProcessCallback &callback,
103                             bool monitor_signals);
104 
GetMonitorProcessCallback()105   Host::MonitorChildProcessCallback GetMonitorProcessCallback() const {
106     return m_monitor_callback;
107   }
108 
109   /// A Monitor callback which does not take any action on process events. Use
110   /// this if you don't need to take any particular action when the process
111   /// terminates, but you still need to reap it.
112   static bool NoOpMonitorCallback(lldb::pid_t pid, bool exited, int signal,
113                                   int status);
114 
GetMonitorSignals()115   bool GetMonitorSignals() const { return m_monitor_signals; }
116 
117   // If the LaunchInfo has a monitor callback, then arrange to monitor the
118   // process. Return true if the LaunchInfo has taken care of monitoring the
119   // process, and false if the caller might want to monitor the process
120   // themselves.
121 
122   bool MonitorProcess() const;
123 
GetPTY()124   PseudoTerminal &GetPTY() { return *m_pty; }
125 
126   // Get and set the actual listener that will be used for the process events
GetListener()127   lldb::ListenerSP GetListener() const { return m_listener_sp; }
128 
SetListener(const lldb::ListenerSP & listener_sp)129   void SetListener(const lldb::ListenerSP &listener_sp) {
130     m_listener_sp = listener_sp;
131   }
132 
GetHijackListener()133   lldb::ListenerSP GetHijackListener() const { return m_hijack_listener_sp; }
134 
SetHijackListener(const lldb::ListenerSP & listener_sp)135   void SetHijackListener(const lldb::ListenerSP &listener_sp) {
136     m_hijack_listener_sp = listener_sp;
137   }
138 
SetLaunchEventData(const char * data)139   void SetLaunchEventData(const char *data) { m_event_data.assign(data); }
140 
GetLaunchEventData()141   const char *GetLaunchEventData() const { return m_event_data.c_str(); }
142 
143   void SetDetachOnError(bool enable);
144 
GetDetachOnError()145   bool GetDetachOnError() const {
146     return m_flags.Test(lldb::eLaunchFlagDetachOnError);
147   }
148 
149 protected:
150   FileSpec m_working_dir;
151   std::string m_plugin_name;
152   FileSpec m_shell;
153   Flags m_flags; // Bitwise OR of bits from lldb::LaunchFlags
154   std::vector<FileAction> m_file_actions; // File actions for any other files
155   std::shared_ptr<PseudoTerminal> m_pty;
156   uint32_t m_resume_count; // How many times do we resume after launching
157   Host::MonitorChildProcessCallback m_monitor_callback;
158   void *m_monitor_callback_baton;
159   bool m_monitor_signals;
160   std::string m_event_data; // A string passed to the plugin launch, having no
161                             // meaning to the upper levels of lldb.
162   lldb::ListenerSP m_listener_sp;
163   lldb::ListenerSP m_hijack_listener_sp;
164 };
165 }
166 
167 #endif // LLDB_HOST_PROCESSLAUNCHINFO_H
168