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"
21 #include "lldb/Host/File.h"
23 namespace lldb_private {
25 //----------------------------------------------------------------------
26 /// @class Host Host.h "lldb/Host/Host.h"
27 /// @brief A class that provides host computer information.
29 /// Host is a class that answers information about the host operating
31 //----------------------------------------------------------------------
35 typedef bool (*MonitorChildProcessCallback) (void *callback_baton,
38 int signal, // Zero for no signal
39 int status); // Exit value of process if signal is zero
41 //------------------------------------------------------------------
42 /// Start monitoring a child process.
44 /// Allows easy monitoring of child processes. \a callback will be
45 /// called when the child process exits or if it gets a signal. The
46 /// callback will only be called with signals if \a monitor_signals
47 /// is \b true. \a callback will usually be called from another
48 /// thread so the callback function must be thread safe.
50 /// When the callback gets called, the return value indicates if
51 /// minotoring should stop. If \b true is returned from \a callback
52 /// the information will be removed. If \b false is returned then
53 /// monitoring will continue. If the child process exits, the
54 /// monitoring will automatically stop after the callback returned
55 /// ragardless of the callback return value.
57 /// @param[in] callback
58 /// A function callback to call when a child receives a signal
59 /// (if \a monitor_signals is true) or a child exits.
61 /// @param[in] callback_baton
62 /// A void * of user data that will be pass back when
63 /// \a callback is called.
66 /// The process ID of a child process to monitor, -1 for all
69 /// @param[in] monitor_signals
70 /// If \b true the callback will get called when the child
71 /// process gets a signal. If \b false, the callback will only
72 /// get called if the child process exits.
75 /// A thread handle that can be used to cancel the thread that
76 /// was spawned to monitor \a pid.
78 /// @see static void Host::StopMonitoringChildProcess (uint32_t)
79 //------------------------------------------------------------------
81 StartMonitoringChildProcess (MonitorChildProcessCallback callback,
84 bool monitor_signals);
86 //------------------------------------------------------------------
87 /// Get the host page size.
90 /// The size in bytes of a VM page on the host system.
91 //------------------------------------------------------------------
95 //------------------------------------------------------------------
96 /// Returns the endianness of the host system.
99 /// Returns the endianness of the host system as a lldb::ByteOrder
101 //------------------------------------------------------------------
102 static lldb::ByteOrder
105 //------------------------------------------------------------------
106 /// Returns the number of CPUs on this current host.
109 /// Number of CPUs on this current host, or zero if the number
110 /// of CPUs can't be determined on this host.
111 //------------------------------------------------------------------
116 GetOSVersion (uint32_t &major,
121 GetOSBuildString (std::string &s);
124 GetOSKernelDescription (std::string &s);
127 GetHostname (std::string &s);
130 GetUserName (uint32_t uid, std::string &user_name);
133 GetGroupName (uint32_t gid, std::string &group_name);
142 GetEffectiveUserID ();
145 GetEffectiveGroupID ();
155 SystemLog (SystemLogType type, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
158 SystemLog (SystemLogType type, const char *format, va_list args);
160 //------------------------------------------------------------------
161 /// Gets the host architecture.
164 /// A const architecture object that represents the host
166 //------------------------------------------------------------------
167 enum SystemDefaultArchitecture
169 eSystemDefaultArchitecture, // The overall default architecture that applications will run on this host
170 eSystemDefaultArchitecture32, // If this host supports 32 bit programs, return the default 32 bit arch
171 eSystemDefaultArchitecture64 // If this host supports 64 bit programs, return the default 64 bit arch
174 static const ArchSpec &
175 GetArchitecture (SystemDefaultArchitecture arch_kind = eSystemDefaultArchitecture);
177 //------------------------------------------------------------------
178 /// Gets the host vendor string.
181 /// A const string object containing the host vendor name.
182 //------------------------------------------------------------------
183 static const ConstString &
186 //------------------------------------------------------------------
187 /// Gets the host Operating System (OS) string.
190 /// A const string object containing the host OS name.
191 //------------------------------------------------------------------
192 static const ConstString &
195 //------------------------------------------------------------------
196 /// Gets the host target triple as a const string.
199 /// A const string object containing the host target triple.
200 //------------------------------------------------------------------
201 static const ConstString &
204 //------------------------------------------------------------------
205 /// Get the process ID for the calling process.
208 /// The process ID for the current process.
209 //------------------------------------------------------------------
211 GetCurrentProcessID ();
214 Kill(lldb::pid_t pid, int signo);
216 //------------------------------------------------------------------
217 /// Get the thread ID for the calling thread in the current process.
220 /// The thread ID for the calling thread in the current process.
221 //------------------------------------------------------------------
223 GetCurrentThreadID ();
225 //------------------------------------------------------------------
226 /// Get the thread token (the one returned by ThreadCreate when the thread was created) for the
227 /// calling thread in the current process.
230 /// The thread token for the calling thread in the current process.
231 //------------------------------------------------------------------
232 static lldb::thread_t
236 GetSignalAsCString (int signo);
240 //------------------------------------------------------------------
241 /// Host specific thread created function call.
243 /// This function call lets the current host OS do any thread
244 /// specific initialization that it needs, including naming the
245 /// thread. No cleanup routine is exptected to be called
248 /// The current thread's name in the current process.
249 //------------------------------------------------------------------
251 ThreadCreated (const char *name);
253 static lldb::thread_t
254 ThreadCreate (const char *name,
255 lldb::thread_func_t function,
256 lldb::thread_arg_t thread_arg,
260 ThreadCancel (lldb::thread_t thread,
264 ThreadDetach (lldb::thread_t thread,
267 ThreadJoin (lldb::thread_t thread,
268 lldb::thread_result_t *thread_result_ptr,
271 typedef void (*ThreadLocalStorageCleanupCallback) (void *p);
273 static lldb::thread_key_t
274 ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback);
277 ThreadLocalStorageGet(lldb::thread_key_t key);
280 ThreadLocalStorageSet(lldb::thread_key_t key, void *value);
282 //------------------------------------------------------------------
283 /// Gets the name of a thread in a process.
285 /// This function will name a thread in a process using it's own
286 /// thread name pool, and also will attempt to set a thread name
287 /// using any supported host OS APIs.
290 /// The process ID in which we are trying to get the name of
294 /// The thread ID for which we are trying retrieve the name of.
297 /// A std::string containing the thread name.
298 //------------------------------------------------------------------
300 GetThreadName (lldb::pid_t pid, lldb::tid_t tid);
302 //------------------------------------------------------------------
303 /// Sets the name of a thread in the current process.
306 /// The process ID in which we are trying to name a thread.
309 /// The thread ID which we are trying to name.
312 /// The current thread's name in the current process to \a name.
315 /// \b true if the thread name was able to be set, \b false
317 //------------------------------------------------------------------
319 SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name);
321 //------------------------------------------------------------------
322 /// Sets a shortened name of a thread in the current process.
325 /// The process ID in which we are trying to name a thread.
328 /// The thread ID which we are trying to name.
331 /// The current thread's name in the current process to \a name.
334 /// The maximum length for the thread's shortened name.
337 /// \b true if the thread name was able to be set, \b false
340 SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name, size_t len);
342 //------------------------------------------------------------------
343 /// Gets the FileSpec of the current process (the process that
344 /// that is running the LLDB code).
347 /// \b A file spec with the program name.
348 //------------------------------------------------------------------
350 GetProgramFileSpec ();
352 //------------------------------------------------------------------
353 /// Given an address in the current process (the process that
354 /// is running the LLDB code), return the name of the module that
355 /// it comes from. This can be useful when you need to know the
356 /// path to the shared library that your code is running in for
357 /// loading resources that are relative to your binary.
359 /// @param[in] host_addr
360 /// The pointer to some code in the current process.
363 /// \b A file spec with the module that contains \a host_addr,
364 /// which may be invalid if \a host_addr doesn't fall into
365 /// any valid module address range.
366 //------------------------------------------------------------------
368 GetModuleFileSpecForHostAddress (const void *host_addr);
372 //------------------------------------------------------------------
373 /// If you have an executable that is in a bundle and want to get
374 /// back to the bundle directory from the path itself, this
375 /// function will change a path to a file within a bundle to the
376 /// bundle directory itself.
379 /// A file spec that might point to a file in a bundle.
381 /// @param[out] bundle_directory
382 /// An object will be filled in with the bundle directory for
383 /// the bundle when \b true is returned. Otherwise \a file is
384 /// left untouched and \b false is returned.
387 /// \b true if \a file was resolved in \a bundle_directory,
388 /// \b false otherwise.
389 //------------------------------------------------------------------
391 GetBundleDirectory (const FileSpec &file, FileSpec &bundle_directory);
393 //------------------------------------------------------------------
394 /// When executable files may live within a directory, where the
395 /// directory represents an executable bundle (like the MacOSX
396 /// app bundles), the locate the executable within the containing
399 /// @param[in,out] file
400 /// A file spec that currently points to the bundle that will
401 /// be filled in with the executable path within the bundle
402 /// if \b true is returned. Otherwise \a file is left untouched.
405 /// \b true if \a file was resolved, \b false if this function
406 /// was not able to resolve the path.
407 //------------------------------------------------------------------
409 ResolveExecutableInBundle (FileSpec &file);
411 //------------------------------------------------------------------
412 /// Find a resource files that are related to LLDB.
414 /// Operating systems have different ways of storing shared
415 /// libraries and related resources. This function abstracts the
416 /// access to these paths.
418 /// @param[in] path_type
419 /// The type of LLDB resource path you are looking for. If the
420 /// enumeration ends with "Dir", then only the \a file_spec's
421 /// directory member gets filled in.
423 /// @param[in] file_spec
424 /// A file spec that gets filled in with the appriopriate path.
427 /// \b true if \a resource_path was resolved, \a false otherwise.
428 //------------------------------------------------------------------
430 GetLLDBPath (PathType path_type,
431 FileSpec &file_spec);
433 //------------------------------------------------------------------
434 /// Set a string that can be displayed if host application crashes.
436 /// Some operating systems have the ability to print a description
437 /// for shared libraries when a program crashes. If the host OS
438 /// supports such a mechanism, it should be implemented to help
439 /// with crash triage.
441 /// @param[in] format
442 /// A printf format that will be used to form a new crash
443 /// description string.
444 //------------------------------------------------------------------
446 SetCrashDescriptionWithFormat (const char *format, ...) __attribute__ ((format (printf, 1, 2)));
449 SetCrashDescription (const char *description);
452 FindProcesses (const ProcessInstanceInfoMatch &match_info,
453 ProcessInstanceInfoList &proc_infos);
455 typedef std::map<lldb::pid_t, bool> TidMap;
456 typedef std::pair<lldb::pid_t, bool> TidPair;
458 FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach);
461 GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &proc_info);
464 LaunchApplication (const FileSpec &app_file_spec);
467 LaunchProcess (ProcessLaunchInfo &launch_info);
470 RunShellCommand (const char *command, // Shouldn't be NULL
471 const char *working_dir, // Pass NULL to use the current working directory
472 int *status_ptr, // Pass NULL if you don't want the process exit status
473 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
474 std::string *command_output, // Pass NULL if you don't want the command output
475 uint32_t timeout_sec,
476 const char *shell = LLDB_DEFAULT_SHELL);
478 static lldb::DataBufferSP
479 GetAuxvData (lldb_private::Process *process);
481 static lldb::TargetSP
482 GetDummyTarget (Debugger &debugger);
485 OpenFileInExternalEditor (const FileSpec &file_spec,
489 Backtrace (Stream &strm, uint32_t max_frames);
492 GetEnvironment (StringList &env);
494 enum DynamicLibraryOpenOptions
496 eDynamicLibraryOpenOptionLazy = (1u << 0), // Lazily resolve symbols in this dynamic library
497 eDynamicLibraryOpenOptionLocal = (1u << 1), // Only open a shared library with local access (hide it from the global symbol namespace)
498 eDynamicLibraryOpenOptionLimitGetSymbol = (1u << 2) // DynamicLibraryGetSymbol calls on this handle will only return matches from this shared library
501 DynamicLibraryOpen (const FileSpec &file_spec,
506 DynamicLibraryClose (void *dynamic_library_handle);
509 DynamicLibraryGetSymbol (void *dynamic_library_handle,
510 const char *symbol_name,
514 MakeDirectory (const char* path, mode_t mode);
516 static lldb::user_id_t
517 OpenFile (const FileSpec& file_spec,
523 CloseFile (lldb::user_id_t fd,
527 WriteFile (lldb::user_id_t fd,
534 ReadFile (lldb::user_id_t fd,
540 static lldb::user_id_t
541 GetFileSize (const FileSpec& file_spec);
544 GetFileExists (const FileSpec& file_spec);
547 CalculateMD5 (const FileSpec& file_spec,
553 } // namespace lldb_private
555 #endif // #if defined(__cplusplus)
556 #endif // liblldb_Host_h_