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_
22 // Other libraries and framework includes
24 #include "lldb/Core/Broadcaster.h"
25 #include "lldb/Core/FormatEntity.h"
26 #include "lldb/Core/IOHandler.h"
27 #include "lldb/Core/Listener.h"
28 #include "lldb/Core/SourceManager.h"
29 #include "lldb/Core/UserID.h"
30 #include "lldb/Core/UserSettingsController.h"
31 #include "lldb/Host/HostThread.h"
32 #include "lldb/Host/Terminal.h"
33 #include "lldb/Target/Platform.h"
34 #include "lldb/Target/TargetList.h"
35 #include "lldb/lldb-public.h"
43 namespace lldb_private {
45 //----------------------------------------------------------------------
46 /// @class Debugger Debugger.h "lldb/Core/Debugger.h"
47 /// @brief A class to manage flag bits.
49 /// Provides a global root objects for the debugger core.
50 //----------------------------------------------------------------------
52 class Debugger : public std::enable_shared_from_this<Debugger>,
55 friend class SourceManager; // For GetSourceFileCache.
60 static lldb::DebuggerSP
61 CreateInstance(lldb::LogOutputCallback log_callback = nullptr,
62 void *baton = nullptr);
64 static lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid);
66 static lldb::TargetSP FindTargetWithProcess(Process *process);
68 static void Initialize(LoadPluginCallbackType load_plugin_callback);
70 static void Terminate();
72 static void SettingsInitialize();
74 static void SettingsTerminate();
76 static void Destroy(lldb::DebuggerSP &debugger_sp);
78 static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id);
80 static lldb::DebuggerSP
81 FindDebuggerWithInstanceName(const ConstString &instance_name);
83 static size_t GetNumDebuggers();
85 static lldb::DebuggerSP GetDebuggerAtIndex(size_t index);
87 static bool FormatDisassemblerAddress(const FormatEntity::Entry *format,
88 const SymbolContext *sc,
89 const SymbolContext *prev_sc,
90 const ExecutionContext *exe_ctx,
91 const Address *addr, Stream &s);
95 bool GetAsyncExecution();
97 void SetAsyncExecution(bool async);
99 lldb::StreamFileSP GetInputFile() { return m_input_file_sp; }
101 lldb::StreamFileSP GetOutputFile() { return m_output_file_sp; }
103 lldb::StreamFileSP GetErrorFile() { return m_error_file_sp; }
105 void SetInputFileHandle(FILE *fh, bool tranfer_ownership);
107 void SetOutputFileHandle(FILE *fh, bool tranfer_ownership);
109 void SetErrorFileHandle(FILE *fh, bool tranfer_ownership);
111 void SaveInputTerminalState();
113 void RestoreInputTerminalState();
115 lldb::StreamSP GetAsyncOutputStream();
117 lldb::StreamSP GetAsyncErrorStream();
119 CommandInterpreter &GetCommandInterpreter() {
120 assert(m_command_interpreter_ap.get());
121 return *m_command_interpreter_ap;
124 lldb::ListenerSP GetListener() { return m_listener_sp; }
126 // This returns the Debugger's scratch source manager. It won't be able to
127 // look up files in debug
128 // information, but it can look up files by absolute path and display them to
130 // To get the target's source manager, call GetSourceManager on the target
132 SourceManager &GetSourceManager();
134 lldb::TargetSP GetSelectedTarget() {
135 return m_target_list.GetSelectedTarget();
138 ExecutionContext GetSelectedExecutionContext();
139 //------------------------------------------------------------------
140 /// Get accessor for the target list.
142 /// The target list is part of the global debugger object. This
143 /// the single debugger shared instance to control where targets
144 /// get created and to allow for tracking and searching for targets
145 /// based on certain criteria.
148 /// A global shared target list.
149 //------------------------------------------------------------------
150 TargetList &GetTargetList() { return m_target_list; }
152 PlatformList &GetPlatformList() { return m_platform_list; }
154 void DispatchInputInterrupt();
156 void DispatchInputEndOfFile();
158 //------------------------------------------------------------------
159 // If any of the streams are not set, set them to the in/out/err
160 // stream of the top most input reader to ensure they at least have
162 //------------------------------------------------------------------
163 void AdoptTopIOHandlerFilesIfInvalid(lldb::StreamFileSP &in,
164 lldb::StreamFileSP &out,
165 lldb::StreamFileSP &err);
167 void PushIOHandler(const lldb::IOHandlerSP &reader_sp);
169 bool PopIOHandler(const lldb::IOHandlerSP &reader_sp);
171 // Synchronously run an input reader until it is done
172 void RunIOHandler(const lldb::IOHandlerSP &reader_sp);
174 bool IsTopIOHandler(const lldb::IOHandlerSP &reader_sp);
176 bool CheckTopIOHandlerTypes(IOHandler::Type top_type,
177 IOHandler::Type second_top_type);
179 void PrintAsync(const char *s, size_t len, bool is_stdout);
181 ConstString GetTopIOHandlerControlSequence(char ch);
183 const char *GetIOHandlerCommandPrefix();
185 const char *GetIOHandlerHelpPrologue();
187 void ClearIOHandlers();
189 bool GetCloseInputOnEOF() const;
191 void SetCloseInputOnEOF(bool b);
193 bool EnableLog(const char *channel, const char **categories,
194 const char *log_file, uint32_t log_options,
195 Stream &error_stream);
197 void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);
199 //----------------------------------------------------------------------
200 // Properties Functions
201 //----------------------------------------------------------------------
202 enum StopDisassemblyType {
203 eStopDisassemblyTypeNever = 0,
204 eStopDisassemblyTypeNoDebugInfo,
205 eStopDisassemblyTypeNoSource,
206 eStopDisassemblyTypeAlways
209 Error SetPropertyValue(const ExecutionContext *exe_ctx,
210 VarSetOperationType op, llvm::StringRef property_path,
211 llvm::StringRef value) override;
213 bool GetAutoConfirm() const;
215 const FormatEntity::Entry *GetDisassemblyFormat() const;
217 const FormatEntity::Entry *GetFrameFormat() const;
219 const FormatEntity::Entry *GetThreadFormat() const;
221 const FormatEntity::Entry *GetThreadStopFormat() const;
223 lldb::ScriptLanguage GetScriptLanguage() const;
225 bool SetScriptLanguage(lldb::ScriptLanguage script_lang);
227 uint32_t GetTerminalWidth() const;
229 bool SetTerminalWidth(uint32_t term_width);
231 llvm::StringRef GetPrompt() const;
233 void SetPrompt(llvm::StringRef p);
234 void SetPrompt(const char *) = delete;
236 bool GetUseExternalEditor() const;
238 bool SetUseExternalEditor(bool use_external_editor_p);
240 bool GetUseColor() const;
242 bool SetUseColor(bool use_color);
244 lldb::StopShowColumn GetStopShowColumn() const;
246 const FormatEntity::Entry *GetStopShowColumnAnsiPrefix() const;
248 const FormatEntity::Entry *GetStopShowColumnAnsiSuffix() const;
250 uint32_t GetStopSourceLineCount(bool before) const;
252 StopDisassemblyType GetStopDisassemblyDisplay() const;
254 uint32_t GetDisassemblyLineCount() const;
256 bool GetAutoOneLineSummaries() const;
258 bool GetAutoIndent() const;
260 bool SetAutoIndent(bool b);
262 bool GetPrintDecls() const;
264 bool SetPrintDecls(bool b);
266 uint32_t GetTabSize() const;
268 bool SetTabSize(uint32_t tab_size);
270 bool GetEscapeNonPrintables() const;
272 bool GetNotifyVoid() const;
274 const ConstString &GetInstanceName() { return m_instance_name; }
276 bool LoadPlugin(const FileSpec &spec, Error &error);
278 void ExecuteIOHandlers();
280 bool IsForwardingEvents();
282 void EnableForwardEvents(const lldb::ListenerSP &listener_sp);
284 void CancelForwardEvents(const lldb::ListenerSP &listener_sp);
286 bool IsHandlingEvents() const { return m_event_handler_thread.IsJoinable(); }
288 Error RunREPL(lldb::LanguageType language, const char *repl_options);
290 // This is for use in the command interpreter, when you either want the
291 // selected target, or if no target
292 // is present you want to prime the dummy target with entities that will be
293 // copied over to new targets.
294 Target *GetSelectedOrDummyTarget(bool prefer_dummy = false);
295 Target *GetDummyTarget();
297 lldb::BroadcasterManagerSP GetBroadcasterManager() {
298 return m_broadcaster_manager_sp;
302 friend class CommandInterpreter;
305 bool StartEventHandlerThread();
307 void StopEventHandlerThread();
309 static lldb::thread_result_t EventHandlerThread(lldb::thread_arg_t arg);
311 bool HasIOHandlerThread();
313 bool StartIOHandlerThread();
315 void StopIOHandlerThread();
317 void JoinIOHandlerThread();
319 static lldb::thread_result_t IOHandlerThread(lldb::thread_arg_t arg);
321 void DefaultEventHandler();
323 void HandleBreakpointEvent(const lldb::EventSP &event_sp);
325 void HandleProcessEvent(const lldb::EventSP &event_sp);
327 void HandleThreadEvent(const lldb::EventSP &event_sp);
329 size_t GetProcessSTDOUT(Process *process, Stream *stream);
331 size_t GetProcessSTDERR(Process *process, Stream *stream);
333 SourceManager::SourceFileCache &GetSourceFileCache() {
334 return m_source_file_cache;
337 void InstanceInitialize();
339 lldb::StreamFileSP m_input_file_sp;
340 lldb::StreamFileSP m_output_file_sp;
341 lldb::StreamFileSP m_error_file_sp;
343 lldb::BroadcasterManagerSP m_broadcaster_manager_sp; // The debugger acts as a
344 // broadcaster manager of
346 // It needs to get constructed before the target_list or any other
347 // member that might want to broadcast through the debugger.
349 TerminalState m_terminal_state;
350 TargetList m_target_list;
352 PlatformList m_platform_list;
353 lldb::ListenerSP m_listener_sp;
354 std::unique_ptr<SourceManager> m_source_manager_ap; // This is a scratch
355 // source manager that we
356 // return if we have no
358 SourceManager::SourceFileCache m_source_file_cache; // All the source managers
359 // for targets created in
360 // this debugger used this
362 // source file cache.
363 std::unique_ptr<CommandInterpreter> m_command_interpreter_ap;
365 IOHandlerStack m_input_reader_stack;
366 typedef std::map<std::string, lldb::StreamWP> LogStreamMap;
367 LogStreamMap m_log_streams;
368 lldb::StreamSP m_log_callback_stream_sp;
369 ConstString m_instance_name;
370 static LoadPluginCallbackType g_load_plugin_callback;
371 typedef std::vector<llvm::sys::DynamicLibrary> LoadedPluginsList;
372 LoadedPluginsList m_loaded_plugins;
373 HostThread m_event_handler_thread;
374 HostThread m_io_handler_thread;
375 Broadcaster m_sync_broadcaster;
376 lldb::ListenerSP m_forward_listener_sp;
377 std::once_flag m_clear_once;
379 //----------------------------------------------------------------------
380 // Events for m_sync_broadcaster
381 //----------------------------------------------------------------------
383 eBroadcastBitEventThreadIsListening = (1 << 0),
387 // Use Debugger::CreateInstance() to get a shared pointer to a new
389 Debugger(lldb::LogOutputCallback m_log_callback, void *baton);
391 DISALLOW_COPY_AND_ASSIGN(Debugger);
394 } // namespace lldb_private
396 #endif // liblldb_Debugger_h_