1 //===-- Debugger.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_Debugger_h_
11 #define liblldb_Debugger_h_
18 #include "lldb/Core/FormatEntity.h"
19 #include "lldb/Core/IOHandler.h"
20 #include "lldb/Core/SourceManager.h"
21 #include "lldb/Core/UserSettingsController.h"
22 #include "lldb/Host/HostThread.h"
23 #include "lldb/Host/Terminal.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/Platform.h"
26 #include "lldb/Target/TargetList.h"
27 #include "lldb/Utility/Broadcaster.h"
28 #include "lldb/Utility/ConstString.h"
29 #include "lldb/Utility/FileSpec.h"
30 #include "lldb/Utility/Status.h"
31 #include "lldb/Utility/UserID.h"
32 #include "lldb/lldb-defines.h"
33 #include "lldb/lldb-enumerations.h"
34 #include "lldb/lldb-forward.h"
35 #include "lldb/lldb-private-enumerations.h"
36 #include "lldb/lldb-private-types.h"
37 #include "lldb/lldb-types.h"
39 #include "llvm/ADT/ArrayRef.h"
40 #include "llvm/ADT/StringMap.h"
41 #include "llvm/ADT/StringRef.h"
42 #include "llvm/Support/DynamicLibrary.h"
43 #include "llvm/Support/Threading.h"
49 namespace lldb_private {
52 namespace lldb_private {
53 class CommandInterpreter;
55 namespace lldb_private {
58 namespace lldb_private {
61 namespace lldb_private {
64 namespace lldb_private {
71 namespace lldb_private {
73 //----------------------------------------------------------------------
74 /// @class Debugger Debugger.h "lldb/Core/Debugger.h"
75 /// A class to manage flag bits.
77 /// Provides a global root objects for the debugger core.
78 //----------------------------------------------------------------------
80 class Debugger : public std::enable_shared_from_this<Debugger>,
83 friend class SourceManager; // For GetSourceFileCache.
88 static lldb::DebuggerSP
89 CreateInstance(lldb::LogOutputCallback log_callback = nullptr,
90 void *baton = nullptr);
92 static lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid);
94 static lldb::TargetSP FindTargetWithProcess(Process *process);
96 static void Initialize(LoadPluginCallbackType load_plugin_callback);
98 static void Terminate();
100 static void SettingsInitialize();
102 static void SettingsTerminate();
104 static void Destroy(lldb::DebuggerSP &debugger_sp);
106 static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id);
108 static lldb::DebuggerSP
109 FindDebuggerWithInstanceName(const ConstString &instance_name);
111 static size_t GetNumDebuggers();
113 static lldb::DebuggerSP GetDebuggerAtIndex(size_t index);
115 static bool FormatDisassemblerAddress(const FormatEntity::Entry *format,
116 const SymbolContext *sc,
117 const SymbolContext *prev_sc,
118 const ExecutionContext *exe_ctx,
119 const Address *addr, Stream &s);
123 bool GetAsyncExecution();
125 void SetAsyncExecution(bool async);
127 lldb::StreamFileSP GetInputFile() { return m_input_file_sp; }
129 lldb::StreamFileSP GetOutputFile() { return m_output_file_sp; }
131 lldb::StreamFileSP GetErrorFile() { return m_error_file_sp; }
133 void SetInputFileHandle(FILE *fh, bool tranfer_ownership);
135 void SetOutputFileHandle(FILE *fh, bool tranfer_ownership);
137 void SetErrorFileHandle(FILE *fh, bool tranfer_ownership);
139 void SaveInputTerminalState();
141 void RestoreInputTerminalState();
143 lldb::StreamSP GetAsyncOutputStream();
145 lldb::StreamSP GetAsyncErrorStream();
147 CommandInterpreter &GetCommandInterpreter() {
148 assert(m_command_interpreter_ap.get());
149 return *m_command_interpreter_ap;
152 lldb::ListenerSP GetListener() { return m_listener_sp; }
154 // This returns the Debugger's scratch source manager. It won't be able to
155 // look up files in debug information, but it can look up files by absolute
156 // path and display them to you. To get the target's source manager, call
157 // GetSourceManager on the target instead.
158 SourceManager &GetSourceManager();
160 lldb::TargetSP GetSelectedTarget() {
161 return m_target_list.GetSelectedTarget();
164 ExecutionContext GetSelectedExecutionContext();
165 //------------------------------------------------------------------
166 /// Get accessor for the target list.
168 /// The target list is part of the global debugger object. This the single
169 /// debugger shared instance to control where targets get created and to
170 /// allow for tracking and searching for targets based on certain criteria.
173 /// A global shared target list.
174 //------------------------------------------------------------------
175 TargetList &GetTargetList() { return m_target_list; }
177 PlatformList &GetPlatformList() { return m_platform_list; }
179 void DispatchInputInterrupt();
181 void DispatchInputEndOfFile();
183 //------------------------------------------------------------------
184 // If any of the streams are not set, set them to the in/out/err stream of
185 // the top most input reader to ensure they at least have something
186 //------------------------------------------------------------------
187 void AdoptTopIOHandlerFilesIfInvalid(lldb::StreamFileSP &in,
188 lldb::StreamFileSP &out,
189 lldb::StreamFileSP &err);
191 void PushIOHandler(const lldb::IOHandlerSP &reader_sp,
192 bool cancel_top_handler = true);
194 bool PopIOHandler(const lldb::IOHandlerSP &reader_sp);
196 // Synchronously run an input reader until it is done
197 void RunIOHandler(const lldb::IOHandlerSP &reader_sp);
199 bool IsTopIOHandler(const lldb::IOHandlerSP &reader_sp);
201 bool CheckTopIOHandlerTypes(IOHandler::Type top_type,
202 IOHandler::Type second_top_type);
204 void PrintAsync(const char *s, size_t len, bool is_stdout);
206 ConstString GetTopIOHandlerControlSequence(char ch);
208 const char *GetIOHandlerCommandPrefix();
210 const char *GetIOHandlerHelpPrologue();
212 void ClearIOHandlers();
214 bool GetCloseInputOnEOF() const;
216 void SetCloseInputOnEOF(bool b);
218 bool EnableLog(llvm::StringRef channel,
219 llvm::ArrayRef<const char *> categories,
220 llvm::StringRef log_file, uint32_t log_options,
221 llvm::raw_ostream &error_stream);
223 void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);
225 //----------------------------------------------------------------------
226 // Properties Functions
227 //----------------------------------------------------------------------
228 enum StopDisassemblyType {
229 eStopDisassemblyTypeNever = 0,
230 eStopDisassemblyTypeNoDebugInfo,
231 eStopDisassemblyTypeNoSource,
232 eStopDisassemblyTypeAlways
235 Status SetPropertyValue(const ExecutionContext *exe_ctx,
236 VarSetOperationType op, llvm::StringRef property_path,
237 llvm::StringRef value) override;
239 bool GetAutoConfirm() const;
241 const FormatEntity::Entry *GetDisassemblyFormat() const;
243 const FormatEntity::Entry *GetFrameFormat() const;
245 const FormatEntity::Entry *GetFrameFormatUnique() const;
247 const FormatEntity::Entry *GetThreadFormat() const;
249 const FormatEntity::Entry *GetThreadStopFormat() const;
251 lldb::ScriptLanguage GetScriptLanguage() const;
253 bool SetScriptLanguage(lldb::ScriptLanguage script_lang);
255 uint32_t GetTerminalWidth() const;
257 bool SetTerminalWidth(uint32_t term_width);
259 llvm::StringRef GetPrompt() const;
261 void SetPrompt(llvm::StringRef p);
262 void SetPrompt(const char *) = delete;
264 llvm::StringRef GetReproducerPath() const;
266 bool GetUseExternalEditor() const;
268 bool SetUseExternalEditor(bool use_external_editor_p);
270 bool GetUseColor() const;
272 bool SetUseColor(bool use_color);
274 bool GetHighlightSource() const;
276 lldb::StopShowColumn GetStopShowColumn() const;
278 llvm::StringRef GetStopShowColumnAnsiPrefix() const;
280 llvm::StringRef GetStopShowColumnAnsiSuffix() const;
282 uint32_t GetStopSourceLineCount(bool before) const;
284 StopDisassemblyType GetStopDisassemblyDisplay() const;
286 uint32_t GetDisassemblyLineCount() const;
288 bool GetAutoOneLineSummaries() const;
290 bool GetAutoIndent() const;
292 bool SetAutoIndent(bool b);
294 bool GetPrintDecls() const;
296 bool SetPrintDecls(bool b);
298 uint32_t GetTabSize() const;
300 bool SetTabSize(uint32_t tab_size);
302 bool GetEscapeNonPrintables() const;
304 bool GetNotifyVoid() const;
306 const ConstString &GetInstanceName() { return m_instance_name; }
308 bool LoadPlugin(const FileSpec &spec, Status &error);
310 void ExecuteIOHandlers();
312 bool IsForwardingEvents();
314 void EnableForwardEvents(const lldb::ListenerSP &listener_sp);
316 void CancelForwardEvents(const lldb::ListenerSP &listener_sp);
318 bool IsHandlingEvents() const { return m_event_handler_thread.IsJoinable(); }
320 Status RunREPL(lldb::LanguageType language, const char *repl_options);
322 // This is for use in the command interpreter, when you either want the
323 // selected target, or if no target is present you want to prime the dummy
324 // target with entities that will be copied over to new targets.
325 Target *GetSelectedOrDummyTarget(bool prefer_dummy = false);
326 Target *GetDummyTarget();
328 lldb::BroadcasterManagerSP GetBroadcasterManager() {
329 return m_broadcaster_manager_sp;
333 friend class CommandInterpreter;
336 bool StartEventHandlerThread();
338 void StopEventHandlerThread();
340 static lldb::thread_result_t EventHandlerThread(lldb::thread_arg_t arg);
342 bool HasIOHandlerThread();
344 bool StartIOHandlerThread();
346 void StopIOHandlerThread();
348 void JoinIOHandlerThread();
350 static lldb::thread_result_t IOHandlerThread(lldb::thread_arg_t arg);
352 void DefaultEventHandler();
354 void HandleBreakpointEvent(const lldb::EventSP &event_sp);
356 void HandleProcessEvent(const lldb::EventSP &event_sp);
358 void HandleThreadEvent(const lldb::EventSP &event_sp);
360 size_t GetProcessSTDOUT(Process *process, Stream *stream);
362 size_t GetProcessSTDERR(Process *process, Stream *stream);
364 SourceManager::SourceFileCache &GetSourceFileCache() {
365 return m_source_file_cache;
368 void InstanceInitialize();
370 lldb::StreamFileSP m_input_file_sp;
371 lldb::StreamFileSP m_output_file_sp;
372 lldb::StreamFileSP m_error_file_sp;
374 lldb::BroadcasterManagerSP m_broadcaster_manager_sp; // The debugger acts as a
375 // broadcaster manager of
377 // It needs to get constructed before the target_list or any other member
378 // that might want to broadcast through the debugger.
380 TerminalState m_terminal_state;
381 TargetList m_target_list;
383 PlatformList m_platform_list;
384 lldb::ListenerSP m_listener_sp;
385 std::unique_ptr<SourceManager> m_source_manager_ap; // This is a scratch
386 // source manager that we
387 // return if we have no
389 SourceManager::SourceFileCache m_source_file_cache; // All the source managers
390 // for targets created in
391 // this debugger used this
393 // source file cache.
394 std::unique_ptr<CommandInterpreter> m_command_interpreter_ap;
396 IOHandlerStack m_input_reader_stack;
397 llvm::StringMap<std::weak_ptr<llvm::raw_ostream>> m_log_streams;
398 std::shared_ptr<llvm::raw_ostream> m_log_callback_stream_sp;
399 ConstString m_instance_name;
400 static LoadPluginCallbackType g_load_plugin_callback;
401 typedef std::vector<llvm::sys::DynamicLibrary> LoadedPluginsList;
402 LoadedPluginsList m_loaded_plugins;
403 HostThread m_event_handler_thread;
404 HostThread m_io_handler_thread;
405 Broadcaster m_sync_broadcaster;
406 lldb::ListenerSP m_forward_listener_sp;
407 llvm::once_flag m_clear_once;
409 //----------------------------------------------------------------------
410 // Events for m_sync_broadcaster
411 //----------------------------------------------------------------------
413 eBroadcastBitEventThreadIsListening = (1 << 0),
417 // Use Debugger::CreateInstance() to get a shared pointer to a new debugger
419 Debugger(lldb::LogOutputCallback m_log_callback, void *baton);
421 DISALLOW_COPY_AND_ASSIGN(Debugger);
424 } // namespace lldb_private
426 #endif // liblldb_Debugger_h_