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_
20 // Other libraries and framework includes
22 #include "lldb/Core/Broadcaster.h"
23 #include "lldb/Core/FormatEntity.h"
24 #include "lldb/Core/IOHandler.h"
25 #include "lldb/Core/SourceManager.h"
26 #include "lldb/Core/UserSettingsController.h"
27 #include "lldb/Host/HostThread.h"
28 #include "lldb/Host/Terminal.h"
29 #include "lldb/Target/ExecutionContext.h" // for ExecutionContext
30 #include "lldb/Target/Platform.h"
31 #include "lldb/Target/TargetList.h"
32 #include "lldb/Utility/ConstString.h" // for ConstString
33 #include "lldb/Utility/FileSpec.h" // for FileSpec
34 #include "lldb/Utility/Status.h" // for Status
35 #include "lldb/Utility/UserID.h"
36 #include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN
37 #include "lldb/lldb-enumerations.h" // for ScriptLanguage, Langua...
38 #include "lldb/lldb-forward.h" // for StreamFileSP, DebuggerSP
39 #include "lldb/lldb-private-enumerations.h" // for VarSetOperationType
40 #include "lldb/lldb-private-types.h" // for LoadPluginCallbackType
41 #include "lldb/lldb-types.h" // for LogOutputCallback, thr...
43 #include "llvm/ADT/ArrayRef.h" // for ArrayRef
44 #include "llvm/ADT/StringMap.h" // for StringMap
45 #include "llvm/ADT/StringRef.h" // for StringRef
46 #include "llvm/Support/DynamicLibrary.h" // for DynamicLibrary
47 #include "llvm/Support/Threading.h"
49 #include <assert.h> // for assert
50 #include <stddef.h> // for size_t
53 namespace lldb_private {
56 namespace lldb_private {
57 class CommandInterpreter;
59 namespace lldb_private {
62 namespace lldb_private {
65 namespace lldb_private {
68 namespace lldb_private {
75 namespace lldb_private {
77 //----------------------------------------------------------------------
78 /// @class Debugger Debugger.h "lldb/Core/Debugger.h"
79 /// A class to manage flag bits.
81 /// Provides a global root objects for the debugger core.
82 //----------------------------------------------------------------------
84 class Debugger : public std::enable_shared_from_this<Debugger>,
87 friend class SourceManager; // For GetSourceFileCache.
92 static lldb::DebuggerSP
93 CreateInstance(lldb::LogOutputCallback log_callback = nullptr,
94 void *baton = nullptr);
96 static lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid);
98 static lldb::TargetSP FindTargetWithProcess(Process *process);
100 static void Initialize(LoadPluginCallbackType load_plugin_callback);
102 static void Terminate();
104 static void SettingsInitialize();
106 static void SettingsTerminate();
108 static void Destroy(lldb::DebuggerSP &debugger_sp);
110 static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id);
112 static lldb::DebuggerSP
113 FindDebuggerWithInstanceName(const ConstString &instance_name);
115 static size_t GetNumDebuggers();
117 static lldb::DebuggerSP GetDebuggerAtIndex(size_t index);
119 static bool FormatDisassemblerAddress(const FormatEntity::Entry *format,
120 const SymbolContext *sc,
121 const SymbolContext *prev_sc,
122 const ExecutionContext *exe_ctx,
123 const Address *addr, Stream &s);
127 bool GetAsyncExecution();
129 void SetAsyncExecution(bool async);
131 lldb::StreamFileSP GetInputFile() { return m_input_file_sp; }
133 lldb::StreamFileSP GetOutputFile() { return m_output_file_sp; }
135 lldb::StreamFileSP GetErrorFile() { return m_error_file_sp; }
137 void SetInputFileHandle(FILE *fh, bool tranfer_ownership);
139 void SetOutputFileHandle(FILE *fh, bool tranfer_ownership);
141 void SetErrorFileHandle(FILE *fh, bool tranfer_ownership);
143 void SaveInputTerminalState();
145 void RestoreInputTerminalState();
147 lldb::StreamSP GetAsyncOutputStream();
149 lldb::StreamSP GetAsyncErrorStream();
151 CommandInterpreter &GetCommandInterpreter() {
152 assert(m_command_interpreter_ap.get());
153 return *m_command_interpreter_ap;
156 lldb::ListenerSP GetListener() { return m_listener_sp; }
158 // This returns the Debugger's scratch source manager. It won't be able to
159 // look up files in debug information, but it can look up files by absolute
160 // path and display them to you. To get the target's source manager, call
161 // GetSourceManager on the target instead.
162 SourceManager &GetSourceManager();
164 lldb::TargetSP GetSelectedTarget() {
165 return m_target_list.GetSelectedTarget();
168 ExecutionContext GetSelectedExecutionContext();
169 //------------------------------------------------------------------
170 /// Get accessor for the target list.
172 /// The target list is part of the global debugger object. This the single
173 /// debugger shared instance to control where targets get created and to
174 /// allow for tracking and searching for targets based on certain criteria.
177 /// A global shared target list.
178 //------------------------------------------------------------------
179 TargetList &GetTargetList() { return m_target_list; }
181 PlatformList &GetPlatformList() { return m_platform_list; }
183 void DispatchInputInterrupt();
185 void DispatchInputEndOfFile();
187 //------------------------------------------------------------------
188 // If any of the streams are not set, set them to the in/out/err stream of
189 // the top most input reader to ensure they at least have something
190 //------------------------------------------------------------------
191 void AdoptTopIOHandlerFilesIfInvalid(lldb::StreamFileSP &in,
192 lldb::StreamFileSP &out,
193 lldb::StreamFileSP &err);
195 void PushIOHandler(const lldb::IOHandlerSP &reader_sp);
197 bool PopIOHandler(const lldb::IOHandlerSP &reader_sp);
199 // Synchronously run an input reader until it is done
200 void RunIOHandler(const lldb::IOHandlerSP &reader_sp);
202 bool IsTopIOHandler(const lldb::IOHandlerSP &reader_sp);
204 bool CheckTopIOHandlerTypes(IOHandler::Type top_type,
205 IOHandler::Type second_top_type);
207 void PrintAsync(const char *s, size_t len, bool is_stdout);
209 ConstString GetTopIOHandlerControlSequence(char ch);
211 const char *GetIOHandlerCommandPrefix();
213 const char *GetIOHandlerHelpPrologue();
215 void ClearIOHandlers();
217 bool GetCloseInputOnEOF() const;
219 void SetCloseInputOnEOF(bool b);
221 bool EnableLog(llvm::StringRef channel,
222 llvm::ArrayRef<const char *> categories,
223 llvm::StringRef log_file, uint32_t log_options,
224 llvm::raw_ostream &error_stream);
226 void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);
228 //----------------------------------------------------------------------
229 // Properties Functions
230 //----------------------------------------------------------------------
231 enum StopDisassemblyType {
232 eStopDisassemblyTypeNever = 0,
233 eStopDisassemblyTypeNoDebugInfo,
234 eStopDisassemblyTypeNoSource,
235 eStopDisassemblyTypeAlways
238 Status SetPropertyValue(const ExecutionContext *exe_ctx,
239 VarSetOperationType op, llvm::StringRef property_path,
240 llvm::StringRef value) override;
242 bool GetAutoConfirm() const;
244 const FormatEntity::Entry *GetDisassemblyFormat() const;
246 const FormatEntity::Entry *GetFrameFormat() const;
248 const FormatEntity::Entry *GetFrameFormatUnique() const;
250 const FormatEntity::Entry *GetThreadFormat() const;
252 const FormatEntity::Entry *GetThreadStopFormat() const;
254 lldb::ScriptLanguage GetScriptLanguage() const;
256 bool SetScriptLanguage(lldb::ScriptLanguage script_lang);
258 uint32_t GetTerminalWidth() const;
260 bool SetTerminalWidth(uint32_t term_width);
262 llvm::StringRef GetPrompt() const;
264 void SetPrompt(llvm::StringRef p);
265 void SetPrompt(const char *) = delete;
267 bool GetUseExternalEditor() const;
269 bool SetUseExternalEditor(bool use_external_editor_p);
271 bool GetUseColor() const;
273 bool SetUseColor(bool use_color);
275 lldb::StopShowColumn GetStopShowColumn() const;
277 const FormatEntity::Entry *GetStopShowColumnAnsiPrefix() const;
279 const FormatEntity::Entry *GetStopShowColumnAnsiSuffix() const;
281 uint32_t GetStopSourceLineCount(bool before) const;
283 StopDisassemblyType GetStopDisassemblyDisplay() const;
285 uint32_t GetDisassemblyLineCount() const;
287 bool GetAutoOneLineSummaries() const;
289 bool GetAutoIndent() const;
291 bool SetAutoIndent(bool b);
293 bool GetPrintDecls() const;
295 bool SetPrintDecls(bool b);
297 uint32_t GetTabSize() const;
299 bool SetTabSize(uint32_t tab_size);
301 bool GetEscapeNonPrintables() const;
303 bool GetNotifyVoid() const;
305 const ConstString &GetInstanceName() { return m_instance_name; }
307 bool LoadPlugin(const FileSpec &spec, Status &error);
309 void ExecuteIOHandlers();
311 bool IsForwardingEvents();
313 void EnableForwardEvents(const lldb::ListenerSP &listener_sp);
315 void CancelForwardEvents(const lldb::ListenerSP &listener_sp);
317 bool IsHandlingEvents() const { return m_event_handler_thread.IsJoinable(); }
319 Status RunREPL(lldb::LanguageType language, const char *repl_options);
321 // This is for use in the command interpreter, when you either want the
322 // selected target, or if no target is present you want to prime the dummy
323 // target with entities that will be copied over to new targets.
324 Target *GetSelectedOrDummyTarget(bool prefer_dummy = false);
325 Target *GetDummyTarget();
327 lldb::BroadcasterManagerSP GetBroadcasterManager() {
328 return m_broadcaster_manager_sp;
332 friend class CommandInterpreter;
335 bool StartEventHandlerThread();
337 void StopEventHandlerThread();
339 static lldb::thread_result_t EventHandlerThread(lldb::thread_arg_t arg);
341 bool HasIOHandlerThread();
343 bool StartIOHandlerThread();
345 void StopIOHandlerThread();
347 void JoinIOHandlerThread();
349 static lldb::thread_result_t IOHandlerThread(lldb::thread_arg_t arg);
351 void DefaultEventHandler();
353 void HandleBreakpointEvent(const lldb::EventSP &event_sp);
355 void HandleProcessEvent(const lldb::EventSP &event_sp);
357 void HandleThreadEvent(const lldb::EventSP &event_sp);
359 size_t GetProcessSTDOUT(Process *process, Stream *stream);
361 size_t GetProcessSTDERR(Process *process, Stream *stream);
363 SourceManager::SourceFileCache &GetSourceFileCache() {
364 return m_source_file_cache;
367 void InstanceInitialize();
369 lldb::StreamFileSP m_input_file_sp;
370 lldb::StreamFileSP m_output_file_sp;
371 lldb::StreamFileSP m_error_file_sp;
373 lldb::BroadcasterManagerSP m_broadcaster_manager_sp; // The debugger acts as a
374 // broadcaster manager of
376 // It needs to get constructed before the target_list or any other member
377 // that might want to broadcast through the debugger.
379 TerminalState m_terminal_state;
380 TargetList m_target_list;
382 PlatformList m_platform_list;
383 lldb::ListenerSP m_listener_sp;
384 std::unique_ptr<SourceManager> m_source_manager_ap; // This is a scratch
385 // source manager that we
386 // return if we have no
388 SourceManager::SourceFileCache m_source_file_cache; // All the source managers
389 // for targets created in
390 // this debugger used this
392 // source file cache.
393 std::unique_ptr<CommandInterpreter> m_command_interpreter_ap;
395 IOHandlerStack m_input_reader_stack;
396 llvm::StringMap<std::weak_ptr<llvm::raw_ostream>> m_log_streams;
397 std::shared_ptr<llvm::raw_ostream> m_log_callback_stream_sp;
398 ConstString m_instance_name;
399 static LoadPluginCallbackType g_load_plugin_callback;
400 typedef std::vector<llvm::sys::DynamicLibrary> LoadedPluginsList;
401 LoadedPluginsList m_loaded_plugins;
402 HostThread m_event_handler_thread;
403 HostThread m_io_handler_thread;
404 Broadcaster m_sync_broadcaster;
405 lldb::ListenerSP m_forward_listener_sp;
406 llvm::once_flag m_clear_once;
408 //----------------------------------------------------------------------
409 // Events for m_sync_broadcaster
410 //----------------------------------------------------------------------
412 eBroadcastBitEventThreadIsListening = (1 << 0),
416 // Use Debugger::CreateInstance() to get a shared pointer to a new debugger
418 Debugger(lldb::LogOutputCallback m_log_callback, void *baton);
420 DISALLOW_COPY_AND_ASSIGN(Debugger);
423 } // namespace lldb_private
425 #endif // liblldb_Debugger_h_