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/Core/StringList.h"
22 namespace lldb_private {
24 //----------------------------------------------------------------------
25 /// @class Host Host.h "lldb/Host/Host.h"
26 /// @brief A class that provides host computer information.
28 /// Host is a class that answers information about the host operating
30 //----------------------------------------------------------------------
34 typedef bool (*MonitorChildProcessCallback) (void *callback_baton,
37 int signal, // Zero for no signal
38 int status); // Exit value of process if signal is zero
40 //------------------------------------------------------------------
41 /// Start monitoring a child process.
43 /// Allows easy monitoring of child processes. \a callback will be
44 /// called when the child process exits or if it gets a signal. The
45 /// callback will only be called with signals if \a monitor_signals
46 /// is \b true. \a callback will usually be called from another
47 /// thread so the callback function must be thread safe.
49 /// When the callback gets called, the return value indicates if
50 /// minotoring should stop. If \b true is returned from \a callback
51 /// the information will be removed. If \b false is returned then
52 /// monitoring will continue. If the child process exits, the
53 /// monitoring will automatically stop after the callback returned
54 /// ragardless of the callback return value.
56 /// @param[in] callback
57 /// A function callback to call when a child receives a signal
58 /// (if \a monitor_signals is true) or a child exits.
60 /// @param[in] callback_baton
61 /// A void * of user data that will be pass back when
62 /// \a callback is called.
65 /// The process ID of a child process to monitor, -1 for all
68 /// @param[in] monitor_signals
69 /// If \b true the callback will get called when the child
70 /// process gets a signal. If \b false, the callback will only
71 /// get called if the child process exits.
74 /// A thread handle that can be used to cancel the thread that
75 /// was spawned to monitor \a pid.
77 /// @see static void Host::StopMonitoringChildProcess (uint32_t)
78 //------------------------------------------------------------------
80 StartMonitoringChildProcess (MonitorChildProcessCallback callback,
83 bool monitor_signals);
85 //------------------------------------------------------------------
86 /// Get the host page size.
89 /// The size in bytes of a VM page on the host system.
90 //------------------------------------------------------------------
94 //------------------------------------------------------------------
95 /// Returns the endianness of the host system.
98 /// Returns the endianness of the host system as a lldb::ByteOrder
100 //------------------------------------------------------------------
101 static lldb::ByteOrder
104 //------------------------------------------------------------------
105 /// Returns the number of CPUs on this current host.
108 /// Number of CPUs on this current host, or zero if the number
109 /// of CPUs can't be determined on this host.
110 //------------------------------------------------------------------
115 GetOSVersion (uint32_t &major,
120 GetOSBuildString (std::string &s);
123 GetOSKernelDescription (std::string &s);
126 GetHostname (std::string &s);
129 GetUserName (uint32_t uid, std::string &user_name);
132 GetGroupName (uint32_t gid, std::string &group_name);
141 GetEffectiveUserID ();
144 GetEffectiveGroupID ();
154 SystemLog (SystemLogType type, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
157 SystemLog (SystemLogType type, const char *format, va_list args);
159 //------------------------------------------------------------------
160 /// Gets the host architecture.
163 /// A const architecture object that represents the host
165 //------------------------------------------------------------------
166 enum SystemDefaultArchitecture
168 eSystemDefaultArchitecture, // The overall default architecture that applications will run on this host
169 eSystemDefaultArchitecture32, // If this host supports 32 bit programs, return the default 32 bit arch
170 eSystemDefaultArchitecture64 // If this host supports 64 bit programs, return the default 64 bit arch
173 static const ArchSpec &
174 GetArchitecture (SystemDefaultArchitecture arch_kind = eSystemDefaultArchitecture);
176 //------------------------------------------------------------------
177 /// Gets the host vendor string.
180 /// A const string object containing the host vendor name.
181 //------------------------------------------------------------------
182 static const ConstString &
185 //------------------------------------------------------------------
186 /// Gets the host Operating System (OS) string.
189 /// A const string object containing the host OS name.
190 //------------------------------------------------------------------
191 static const ConstString &
194 //------------------------------------------------------------------
195 /// Gets the host target triple as a const string.
198 /// A const string object containing the host target triple.
199 //------------------------------------------------------------------
200 static const ConstString &
203 //------------------------------------------------------------------
204 /// Get the process ID for the calling process.
207 /// The process ID for the current process.
208 //------------------------------------------------------------------
210 GetCurrentProcessID ();
212 //------------------------------------------------------------------
213 /// Get the thread ID for the calling thread in the current process.
216 /// The thread ID for the calling thread in the current process.
217 //------------------------------------------------------------------
219 GetCurrentThreadID ();
221 //------------------------------------------------------------------
222 /// Get the thread token (the one returned by ThreadCreate when the thread was created) for the
223 /// calling thread in the current process.
226 /// The thread token for the calling thread in the current process.
227 //------------------------------------------------------------------
228 static lldb::thread_t
232 GetSignalAsCString (int signo);
236 //------------------------------------------------------------------
237 /// Host specific thread created function call.
239 /// This function call lets the current host OS do any thread
240 /// specific initialization that it needs, including naming the
241 /// thread. No cleanup routine is exptected to be called
244 /// The current thread's name in the current process.
245 //------------------------------------------------------------------
247 ThreadCreated (const char *name);
249 static lldb::thread_t
250 ThreadCreate (const char *name,
251 lldb::thread_func_t function,
252 lldb::thread_arg_t thread_arg,
256 ThreadCancel (lldb::thread_t thread,
260 ThreadDetach (lldb::thread_t thread,
263 ThreadJoin (lldb::thread_t thread,
264 lldb::thread_result_t *thread_result_ptr,
267 //------------------------------------------------------------------
268 /// Gets the name of a thread in a process.
270 /// This function will name a thread in a process using it's own
271 /// thread name pool, and also will attempt to set a thread name
272 /// using any supported host OS APIs.
275 /// The process ID in which we are trying to get the name of
279 /// The thread ID for which we are trying retrieve the name of.
282 /// A std::string containing the thread name.
283 //------------------------------------------------------------------
285 GetThreadName (lldb::pid_t pid, lldb::tid_t tid);
287 //------------------------------------------------------------------
288 /// Sets the name of a thread in the current process.
291 /// The process ID in which we are trying to name a thread.
294 /// The thread ID which we are trying to name.
297 /// The current thread's name in the current process to \a name.
300 /// \b true if the thread name was able to be set, \b false
302 //------------------------------------------------------------------
304 SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name);
306 //------------------------------------------------------------------
307 /// Sets a shortened name of a thread in the current process.
310 /// The process ID in which we are trying to name a thread.
313 /// The thread ID which we are trying to name.
316 /// The current thread's name in the current process to \a name.
319 /// The maximum length for the thread's shortened name.
322 /// \b true if the thread name was able to be set, \b false
325 SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name, size_t len);
327 //------------------------------------------------------------------
328 /// Gets the FileSpec of the current process (the process that
329 /// that is running the LLDB code).
332 /// \b A file spec with the program name.
333 //------------------------------------------------------------------
335 GetProgramFileSpec ();
337 //------------------------------------------------------------------
338 /// Given an address in the current process (the process that
339 /// is running the LLDB code), return the name of the module that
340 /// it comes from. This can be useful when you need to know the
341 /// path to the shared library that your code is running in for
342 /// loading resources that are relative to your binary.
344 /// @param[in] host_addr
345 /// The pointer to some code in the current process.
348 /// \b A file spec with the module that contains \a host_addr,
349 /// which may be invalid if \a host_addr doesn't fall into
350 /// any valid module address range.
351 //------------------------------------------------------------------
353 GetModuleFileSpecForHostAddress (const void *host_addr);
357 //------------------------------------------------------------------
358 /// If you have an executable that is in a bundle and want to get
359 /// back to the bundle directory from the path itself, this
360 /// function will change a path to a file within a bundle to the
361 /// bundle directory itself.
364 /// A file spec that might point to a file in a bundle.
366 /// @param[out] bundle_directory
367 /// An object will be filled in with the bundle directory for
368 /// the bundle when \b true is returned. Otherwise \a file is
369 /// left untouched and \b false is returned.
372 /// \b true if \a file was resolved in \a bundle_directory,
373 /// \b false otherwise.
374 //------------------------------------------------------------------
376 GetBundleDirectory (const FileSpec &file, FileSpec &bundle_directory);
378 //------------------------------------------------------------------
379 /// When executable files may live within a directory, where the
380 /// directory represents an executable bundle (like the MacOSX
381 /// app bundles), the locate the executable within the containing
384 /// @param[in,out] file
385 /// A file spec that currently points to the bundle that will
386 /// be filled in with the executable path within the bundle
387 /// if \b true is returned. Otherwise \a file is left untouched.
390 /// \b true if \a file was resolved, \b false if this function
391 /// was not able to resolve the path.
392 //------------------------------------------------------------------
394 ResolveExecutableInBundle (FileSpec &file);
396 //------------------------------------------------------------------
397 /// Find a resource files that are related to LLDB.
399 /// Operating systems have different ways of storing shared
400 /// libraries and related resources. This function abstracts the
401 /// access to these paths.
403 /// @param[in] path_type
404 /// The type of LLDB resource path you are looking for. If the
405 /// enumeration ends with "Dir", then only the \a file_spec's
406 /// directory member gets filled in.
408 /// @param[in] file_spec
409 /// A file spec that gets filled in with the appriopriate path.
412 /// \b true if \a resource_path was resolved, \a false otherwise.
413 //------------------------------------------------------------------
415 GetLLDBPath (PathType path_type,
416 FileSpec &file_spec);
418 //------------------------------------------------------------------
419 /// Set a string that can be displayed if host application crashes.
421 /// Some operating systems have the ability to print a description
422 /// for shared libraries when a program crashes. If the host OS
423 /// supports such a mechanism, it should be implemented to help
424 /// with crash triage.
426 /// @param[in] format
427 /// A printf format that will be used to form a new crash
428 /// description string.
429 //------------------------------------------------------------------
431 SetCrashDescriptionWithFormat (const char *format, ...) __attribute__ ((format (printf, 1, 2)));
434 SetCrashDescription (const char *description);
437 FindProcesses (const ProcessInstanceInfoMatch &match_info,
438 ProcessInstanceInfoList &proc_infos);
440 typedef std::map<lldb::pid_t, bool> TidMap;
441 typedef std::pair<lldb::pid_t, bool> TidPair;
443 FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach);
446 GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &proc_info);
449 LaunchApplication (const FileSpec &app_file_spec);
452 LaunchProcess (ProcessLaunchInfo &launch_info);
455 RunShellCommand (const char *command, // Shouldn't be NULL
456 const char *working_dir, // Pass NULL to use the current working directory
457 int *status_ptr, // Pass NULL if you don't want the process exit status
458 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
459 std::string *command_output, // Pass NULL if you don't want the command output
460 uint32_t timeout_sec,
461 const char *shell = "/bin/bash");
463 static lldb::DataBufferSP
464 GetAuxvData (lldb_private::Process *process);
466 static lldb::TargetSP
467 GetDummyTarget (Debugger &debugger);
470 OpenFileInExternalEditor (const FileSpec &file_spec,
474 Backtrace (Stream &strm, uint32_t max_frames);
477 GetEnvironment (StringList &env);
479 enum DynamicLibraryOpenOptions
481 eDynamicLibraryOpenOptionLazy = (1u << 0), // Lazily resolve symbols in this dynamic library
482 eDynamicLibraryOpenOptionLocal = (1u << 1), // Only open a shared library with local access (hide it from the global symbol namespace)
483 eDynamicLibraryOpenOptionLimitGetSymbol = (1u << 2) // DynamicLibraryGetSymbol calls on this handle will only return matches from this shared library
486 DynamicLibraryOpen (const FileSpec &file_spec,
491 DynamicLibraryClose (void *dynamic_library_handle);
494 DynamicLibraryGetSymbol (void *dynamic_library_handle,
495 const char *symbol_name,
499 } // namespace lldb_private
501 #endif // #if defined(__cplusplus)
502 #endif // liblldb_Host_h_