1 //===-- Host.h --------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_HOST_HOST_H
11 #define LLDB_HOST_HOST_H
13 #include "lldb/Host/File.h"
14 #include "lldb/Host/HostThread.h"
15 #include "lldb/Utility/FileSpec.h"
16 #include "lldb/Utility/StringList.h"
17 #include "lldb/lldb-private-forward.h"
18 #include "lldb/lldb-private.h"
23 #include <type_traits>
25 namespace lldb_private {
28 class ProcessLaunchInfo;
30 //----------------------------------------------------------------------
31 // Exit Type for inferior processes
32 //----------------------------------------------------------------------
35 Exit, // The status represents the return code from normal
36 // program exit (i.e. WIFEXITED() was true)
37 Signal, // The status represents the signal number that caused
38 // the program to exit (i.e. WIFSIGNALED() was true)
39 Stop, // The status represents the signal number that caused the
40 // program to stop (i.e. WIFSTOPPED() was true)
46 WaitStatus(Type type, uint8_t status) : type(type), status(status) {}
48 static WaitStatus Decode(int wstatus);
51 inline bool operator==(WaitStatus a, WaitStatus b) {
52 return a.type == b.type && a.status == b.status;
55 inline bool operator!=(WaitStatus a, WaitStatus b) { return !(a == b); }
57 //----------------------------------------------------------------------
58 /// @class Host Host.h "lldb/Host/Host.h"
59 /// @brief A class that provides host computer information.
61 /// Host is a class that answers information about the host operating
63 //----------------------------------------------------------------------
66 typedef std::function<bool(
67 lldb::pid_t pid, bool exited,
68 int signal, // Zero for no signal
69 int status)> // Exit value of process if signal is zero
70 MonitorChildProcessCallback;
72 //------------------------------------------------------------------
73 /// Start monitoring a child process.
75 /// Allows easy monitoring of child processes. \a callback will be
76 /// called when the child process exits or if it gets a signal. The
77 /// callback will only be called with signals if \a monitor_signals
78 /// is \b true. \a callback will usually be called from another
79 /// thread so the callback function must be thread safe.
81 /// When the callback gets called, the return value indicates if
82 /// monitoring should stop. If \b true is returned from \a callback
83 /// the information will be removed. If \b false is returned then
84 /// monitoring will continue. If the child process exits, the
85 /// monitoring will automatically stop after the callback returned
86 /// regardless of the callback return value.
88 /// @param[in] callback
89 /// A function callback to call when a child receives a signal
90 /// (if \a monitor_signals is true) or a child exits.
93 /// The process ID of a child process to monitor, -1 for all
96 /// @param[in] monitor_signals
97 /// If \b true the callback will get called when the child
98 /// process gets a signal. If \b false, the callback will only
99 /// get called if the child process exits.
102 /// A thread handle that can be used to cancel the thread that
103 /// was spawned to monitor \a pid.
105 /// @see static void Host::StopMonitoringChildProcess (uint32_t)
106 //------------------------------------------------------------------
108 StartMonitoringChildProcess(const MonitorChildProcessCallback &callback,
109 lldb::pid_t pid, bool monitor_signals);
111 enum SystemLogType { eSystemLogWarning, eSystemLogError };
113 static void SystemLog(SystemLogType type, const char *format, ...)
114 __attribute__((format(printf, 2, 3)));
116 static void SystemLog(SystemLogType type, const char *format, va_list args);
118 //------------------------------------------------------------------
119 /// Get the process ID for the calling process.
122 /// The process ID for the current process.
123 //------------------------------------------------------------------
124 static lldb::pid_t GetCurrentProcessID();
126 static void Kill(lldb::pid_t pid, int signo);
128 //------------------------------------------------------------------
129 /// Get the thread token (the one returned by ThreadCreate when the thread was
131 /// calling thread in the current process.
134 /// The thread token for the calling thread in the current process.
135 //------------------------------------------------------------------
136 static lldb::thread_t GetCurrentThread();
138 static const char *GetSignalAsCString(int signo);
140 //------------------------------------------------------------------
141 /// Given an address in the current process (the process that
142 /// is running the LLDB code), return the name of the module that
143 /// it comes from. This can be useful when you need to know the
144 /// path to the shared library that your code is running in for
145 /// loading resources that are relative to your binary.
147 /// @param[in] host_addr
148 /// The pointer to some code in the current process.
151 /// \b A file spec with the module that contains \a host_addr,
152 /// which may be invalid if \a host_addr doesn't fall into
153 /// any valid module address range.
154 //------------------------------------------------------------------
155 static FileSpec GetModuleFileSpecForHostAddress(const void *host_addr);
157 //------------------------------------------------------------------
158 /// If you have an executable that is in a bundle and want to get
159 /// back to the bundle directory from the path itself, this
160 /// function will change a path to a file within a bundle to the
161 /// bundle directory itself.
164 /// A file spec that might point to a file in a bundle.
166 /// @param[out] bundle_directory
167 /// An object will be filled in with the bundle directory for
168 /// the bundle when \b true is returned. Otherwise \a file is
169 /// left untouched and \b false is returned.
172 /// \b true if \a file was resolved in \a bundle_directory,
173 /// \b false otherwise.
174 //------------------------------------------------------------------
175 static bool GetBundleDirectory(const FileSpec &file,
176 FileSpec &bundle_directory);
178 //------------------------------------------------------------------
179 /// When executable files may live within a directory, where the
180 /// directory represents an executable bundle (like the MacOSX
181 /// app bundles), then locate the executable within the containing
184 /// @param[in,out] file
185 /// A file spec that currently points to the bundle that will
186 /// be filled in with the executable path within the bundle
187 /// if \b true is returned. Otherwise \a file is left untouched.
190 /// \b true if \a file was resolved, \b false if this function
191 /// was not able to resolve the path.
192 //------------------------------------------------------------------
193 static bool ResolveExecutableInBundle(FileSpec &file);
195 static uint32_t FindProcesses(const ProcessInstanceInfoMatch &match_info,
196 ProcessInstanceInfoList &proc_infos);
198 typedef std::map<lldb::pid_t, bool> TidMap;
199 typedef std::pair<lldb::pid_t, bool> TidPair;
200 static bool FindProcessThreads(const lldb::pid_t pid, TidMap &tids_to_attach);
202 static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info);
204 static const lldb::UnixSignalsSP &GetUnixSignals();
206 static Status LaunchProcess(ProcessLaunchInfo &launch_info);
208 //------------------------------------------------------------------
209 /// Perform expansion of the command-line for this launch info
210 /// This can potentially involve wildcard expansion
211 // environment variable replacement, and whatever other
212 // argument magic the platform defines as part of its typical
214 //------------------------------------------------------------------
215 static Status ShellExpandArguments(ProcessLaunchInfo &launch_info);
217 // TODO: Convert this function to take a StringRef.
218 static Status RunShellCommand(
219 const char *command, // Shouldn't be NULL
220 const FileSpec &working_dir, // Pass empty FileSpec to use the current
222 int *status_ptr, // Pass NULL if you don't want the process exit status
223 int *signo_ptr, // Pass NULL if you don't want the signal that caused the
226 *command_output, // Pass NULL if you don't want the command output
227 uint32_t timeout_sec,
228 bool run_in_default_shell = true);
230 static Status RunShellCommand(
232 const FileSpec &working_dir, // Pass empty FileSpec to use the current
234 int *status_ptr, // Pass NULL if you don't want the process exit status
235 int *signo_ptr, // Pass NULL if you don't want the signal that caused the
238 *command_output, // Pass NULL if you don't want the command output
239 uint32_t timeout_sec,
240 bool run_in_default_shell = true);
242 static bool OpenFileInExternalEditor(const FileSpec &file_spec,
245 static size_t GetEnvironment(StringList &env);
247 static std::unique_ptr<Connection>
248 CreateDefaultConnection(llvm::StringRef url);
251 } // namespace lldb_private
254 template <> struct format_provider<lldb_private::WaitStatus> {
255 /// Options = "" gives a human readable description of the status
256 /// Options = "g" gives a gdb-remote protocol status (e.g., X09)
257 static void format(const lldb_private::WaitStatus &WS, raw_ostream &OS,
258 llvm::StringRef Options);
262 #endif // LLDB_HOST_HOST_H