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 liblldb_Host_h_
11 #define liblldb_Host_h_
12 #if defined(__cplusplus)
19 #include "lldb/lldb-private.h"
20 #include "lldb/lldb-private-forward.h"
21 #include "lldb/Core/StringList.h"
22 #include "lldb/Host/File.h"
23 #include "lldb/Host/FileSpec.h"
25 namespace lldb_private {
28 class ProcessLaunchInfo;
30 //----------------------------------------------------------------------
31 /// @class Host Host.h "lldb/Host/Host.h"
32 /// @brief A class that provides host computer information.
34 /// Host is a class that answers information about the host operating
36 //----------------------------------------------------------------------
41 /// A value of std::numeric_limits<uint32_t>::max() is used if there is no practical limit.
42 static const uint32_t MAX_THREAD_NAME_LENGTH;
44 typedef bool (*MonitorChildProcessCallback) (void *callback_baton,
47 int signal, // Zero for no signal
48 int status); // Exit value of process if signal is zero
50 //------------------------------------------------------------------
51 /// Start monitoring a child process.
53 /// Allows easy monitoring of child processes. \a callback will be
54 /// called when the child process exits or if it gets a signal. The
55 /// callback will only be called with signals if \a monitor_signals
56 /// is \b true. \a callback will usually be called from another
57 /// thread so the callback function must be thread safe.
59 /// When the callback gets called, the return value indicates if
60 /// monitoring should stop. If \b true is returned from \a callback
61 /// the information will be removed. If \b false is returned then
62 /// monitoring will continue. If the child process exits, the
63 /// monitoring will automatically stop after the callback returned
64 /// regardless of the callback return value.
66 /// @param[in] callback
67 /// A function callback to call when a child receives a signal
68 /// (if \a monitor_signals is true) or a child exits.
70 /// @param[in] callback_baton
71 /// A void * of user data that will be pass back when
72 /// \a callback is called.
75 /// The process ID of a child process to monitor, -1 for all
78 /// @param[in] monitor_signals
79 /// If \b true the callback will get called when the child
80 /// process gets a signal. If \b false, the callback will only
81 /// get called if the child process exits.
84 /// A thread handle that can be used to cancel the thread that
85 /// was spawned to monitor \a pid.
87 /// @see static void Host::StopMonitoringChildProcess (uint32_t)
88 //------------------------------------------------------------------
90 StartMonitoringChildProcess (MonitorChildProcessCallback callback,
93 bool monitor_signals);
102 SystemLog (SystemLogType type, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
105 SystemLog (SystemLogType type, const char *format, va_list args);
107 //------------------------------------------------------------------
108 /// Get the process ID for the calling process.
111 /// The process ID for the current process.
112 //------------------------------------------------------------------
114 GetCurrentProcessID ();
117 Kill(lldb::pid_t pid, int signo);
119 //------------------------------------------------------------------
120 /// Get the thread ID for the calling thread in the current process.
123 /// The thread ID for the calling thread in the current process.
124 //------------------------------------------------------------------
126 GetCurrentThreadID ();
128 //------------------------------------------------------------------
129 /// Get the thread token (the one returned by ThreadCreate when the thread was created) for the
130 /// calling thread in the current process.
133 /// The thread token for the calling thread in the current process.
134 //------------------------------------------------------------------
135 static lldb::thread_t
139 GetSignalAsCString (int signo);
143 //------------------------------------------------------------------
144 /// Host specific thread created function call.
146 /// This function call lets the current host OS do any thread
147 /// specific initialization that it needs, including naming the
148 /// thread. No cleanup routine is expected to be called
151 /// The current thread's name in the current process.
152 //------------------------------------------------------------------
154 ThreadCreated (const char *name);
156 static lldb::thread_t
157 ThreadCreate (const char *name,
158 lldb::thread_func_t function,
159 lldb::thread_arg_t thread_arg,
163 ThreadCancel (lldb::thread_t thread,
167 ThreadDetach (lldb::thread_t thread,
170 ThreadJoin (lldb::thread_t thread,
171 lldb::thread_result_t *thread_result_ptr,
174 typedef void (*ThreadLocalStorageCleanupCallback) (void *p);
176 static lldb::thread_key_t
177 ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback);
180 ThreadLocalStorageGet(lldb::thread_key_t key);
183 ThreadLocalStorageSet(lldb::thread_key_t key, void *value);
185 //------------------------------------------------------------------
186 /// Gets the name of a thread in a process.
188 /// This function will name a thread in a process using it's own
189 /// thread name pool, and also will attempt to set a thread name
190 /// using any supported host OS APIs.
193 /// The process ID in which we are trying to get the name of
197 /// The thread ID for which we are trying retrieve the name of.
200 /// A std::string containing the thread name.
201 //------------------------------------------------------------------
203 GetThreadName (lldb::pid_t pid, lldb::tid_t tid);
205 //------------------------------------------------------------------
206 /// Sets the name of a thread in the current process.
209 /// The process ID in which we are trying to name a thread.
212 /// The thread ID which we are trying to name.
215 /// The current thread's name in the current process to \a name.
218 /// \b true if the thread name was able to be set, \b false
220 //------------------------------------------------------------------
222 SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name);
224 //------------------------------------------------------------------
225 /// Sets a shortened name of a thread in the current process.
228 /// The process ID in which we are trying to name a thread.
231 /// The thread ID which we are trying to name.
234 /// The current thread's name in the current process to \a name.
237 /// The maximum length for the thread's shortened name.
240 /// \b true if the thread name was able to be set, \b false
243 SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name, size_t len);
245 //------------------------------------------------------------------
246 /// Given an address in the current process (the process that
247 /// is running the LLDB code), return the name of the module that
248 /// it comes from. This can be useful when you need to know the
249 /// path to the shared library that your code is running in for
250 /// loading resources that are relative to your binary.
252 /// @param[in] host_addr
253 /// The pointer to some code in the current process.
256 /// \b A file spec with the module that contains \a host_addr,
257 /// which may be invalid if \a host_addr doesn't fall into
258 /// any valid module address range.
259 //------------------------------------------------------------------
261 GetModuleFileSpecForHostAddress (const void *host_addr);
263 //------------------------------------------------------------------
264 /// If you have an executable that is in a bundle and want to get
265 /// back to the bundle directory from the path itself, this
266 /// function will change a path to a file within a bundle to the
267 /// bundle directory itself.
270 /// A file spec that might point to a file in a bundle.
272 /// @param[out] bundle_directory
273 /// An object will be filled in with the bundle directory for
274 /// the bundle when \b true is returned. Otherwise \a file is
275 /// left untouched and \b false is returned.
278 /// \b true if \a file was resolved in \a bundle_directory,
279 /// \b false otherwise.
280 //------------------------------------------------------------------
282 GetBundleDirectory (const FileSpec &file, FileSpec &bundle_directory);
284 //------------------------------------------------------------------
285 /// When executable files may live within a directory, where the
286 /// directory represents an executable bundle (like the MacOSX
287 /// app bundles), then locate the executable within the containing
290 /// @param[in,out] file
291 /// A file spec that currently points to the bundle that will
292 /// be filled in with the executable path within the bundle
293 /// if \b true is returned. Otherwise \a file is left untouched.
296 /// \b true if \a file was resolved, \b false if this function
297 /// was not able to resolve the path.
298 //------------------------------------------------------------------
300 ResolveExecutableInBundle (FileSpec &file);
302 //------------------------------------------------------------------
303 /// Set a string that can be displayed if host application crashes.
305 /// Some operating systems have the ability to print a description
306 /// for shared libraries when a program crashes. If the host OS
307 /// supports such a mechanism, it should be implemented to help
308 /// with crash triage.
310 /// @param[in] format
311 /// A printf format that will be used to form a new crash
312 /// description string.
313 //------------------------------------------------------------------
315 SetCrashDescriptionWithFormat (const char *format, ...) __attribute__ ((format (printf, 1, 2)));
318 SetCrashDescription (const char *description);
321 FindProcesses (const ProcessInstanceInfoMatch &match_info,
322 ProcessInstanceInfoList &proc_infos);
324 typedef std::map<lldb::pid_t, bool> TidMap;
325 typedef std::pair<lldb::pid_t, bool> TidPair;
327 FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach);
330 GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &proc_info);
332 #if defined (__APPLE__) || defined (__linux__) || defined (__FreeBSD__) || defined (__GLIBC__) || defined (__NetBSD__)
334 GetPosixspawnFlags (ProcessLaunchInfo &launch_info);
337 LaunchProcessPosixSpawn (const char *exe_path, ProcessLaunchInfo &launch_info, ::pid_t &pid);
339 static bool AddPosixSpawnFileAction(void *file_actions, const FileAction *info, Log *log, Error &error);
342 static const lldb_private::UnixSignalsSP&
346 LaunchApplication (const FileSpec &app_file_spec);
349 LaunchProcess (ProcessLaunchInfo &launch_info);
352 RunShellCommand (const char *command, // Shouldn't be NULL
353 const char *working_dir, // Pass NULL to use the current working directory
354 int *status_ptr, // Pass NULL if you don't want the process exit status
355 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
356 std::string *command_output, // Pass NULL if you don't want the command output
357 uint32_t timeout_sec,
358 const char *shell = LLDB_DEFAULT_SHELL);
360 static lldb::DataBufferSP
361 GetAuxvData (lldb_private::Process *process);
363 static lldb::DataBufferSP
364 GetAuxvData (lldb::pid_t pid);
366 static lldb::TargetSP
367 GetDummyTarget (Debugger &debugger);
370 OpenFileInExternalEditor (const FileSpec &file_spec,
374 Backtrace (Stream &strm, uint32_t max_frames);
377 GetEnvironment (StringList &env);
380 } // namespace lldb_private
382 #endif // #if defined(__cplusplus)
383 #endif // liblldb_Host_h_