1 //===-- Debugger.h ----------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_Debugger_h_
10 #define liblldb_Debugger_h_
17 #include "lldb/Core/FormatEntity.h"
18 #include "lldb/Core/IOHandler.h"
19 #include "lldb/Core/SourceManager.h"
20 #include "lldb/Core/UserSettingsController.h"
21 #include "lldb/Host/HostThread.h"
22 #include "lldb/Host/Terminal.h"
23 #include "lldb/Target/ExecutionContext.h"
24 #include "lldb/Target/Platform.h"
25 #include "lldb/Target/TargetList.h"
26 #include "lldb/Utility/Broadcaster.h"
27 #include "lldb/Utility/ConstString.h"
28 #include "lldb/Utility/FileSpec.h"
29 #include "lldb/Utility/Status.h"
30 #include "lldb/Utility/UserID.h"
31 #include "lldb/lldb-defines.h"
32 #include "lldb/lldb-enumerations.h"
33 #include "lldb/lldb-forward.h"
34 #include "lldb/lldb-private-enumerations.h"
35 #include "lldb/lldb-private-types.h"
36 #include "lldb/lldb-types.h"
38 #include "llvm/ADT/ArrayRef.h"
39 #include "llvm/ADT/StringMap.h"
40 #include "llvm/ADT/StringRef.h"
41 #include "llvm/Support/DynamicLibrary.h"
42 #include "llvm/Support/Threading.h"
52 namespace lldb_private {
54 class CommandInterpreter;
64 /// \class Debugger Debugger.h "lldb/Core/Debugger.h"
65 /// A class to manage flag bits.
67 /// Provides a global root objects for the debugger core.
69 class Debugger : public std::enable_shared_from_this<Debugger>,
72 friend class SourceManager; // For GetSourceFileCache.
77 static lldb::DebuggerSP
78 CreateInstance(lldb::LogOutputCallback log_callback = nullptr,
79 void *baton = nullptr);
81 static lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid);
83 static lldb::TargetSP FindTargetWithProcess(Process *process);
85 static void Initialize(LoadPluginCallbackType load_plugin_callback);
87 static void Terminate();
89 static void SettingsInitialize();
91 static void SettingsTerminate();
93 static void Destroy(lldb::DebuggerSP &debugger_sp);
95 static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id);
97 static lldb::DebuggerSP
98 FindDebuggerWithInstanceName(ConstString instance_name);
100 static size_t GetNumDebuggers();
102 static lldb::DebuggerSP GetDebuggerAtIndex(size_t index);
104 static bool FormatDisassemblerAddress(const FormatEntity::Entry *format,
105 const SymbolContext *sc,
106 const SymbolContext *prev_sc,
107 const ExecutionContext *exe_ctx,
108 const Address *addr, Stream &s);
112 bool GetAsyncExecution();
114 void SetAsyncExecution(bool async);
116 lldb::StreamFileSP GetInputFile() { return m_input_file_sp; }
118 lldb::StreamFileSP GetOutputFile() { return m_output_file_sp; }
120 lldb::StreamFileSP GetErrorFile() { return m_error_file_sp; }
122 repro::DataRecorder *GetInputRecorder();
124 void SetInputFileHandle(FILE *fh, bool tranfer_ownership,
125 repro::DataRecorder *recorder = nullptr);
127 void SetOutputFileHandle(FILE *fh, bool tranfer_ownership);
129 void SetErrorFileHandle(FILE *fh, bool tranfer_ownership);
131 void SaveInputTerminalState();
133 void RestoreInputTerminalState();
135 lldb::StreamSP GetAsyncOutputStream();
137 lldb::StreamSP GetAsyncErrorStream();
139 CommandInterpreter &GetCommandInterpreter() {
140 assert(m_command_interpreter_up.get());
141 return *m_command_interpreter_up;
144 ScriptInterpreter *GetScriptInterpreter(bool can_create = true);
146 lldb::ListenerSP GetListener() { return m_listener_sp; }
148 // This returns the Debugger's scratch source manager. It won't be able to
149 // look up files in debug information, but it can look up files by absolute
150 // path and display them to you. To get the target's source manager, call
151 // GetSourceManager on the target instead.
152 SourceManager &GetSourceManager();
154 lldb::TargetSP GetSelectedTarget() {
155 return m_target_list.GetSelectedTarget();
158 ExecutionContext GetSelectedExecutionContext();
159 /// Get accessor for the target list.
161 /// The target list is part of the global debugger object. This the single
162 /// debugger shared instance to control where targets get created and to
163 /// allow for tracking and searching for targets based on certain criteria.
166 /// A global shared target list.
167 TargetList &GetTargetList() { return m_target_list; }
169 PlatformList &GetPlatformList() { return m_platform_list; }
171 void DispatchInputInterrupt();
173 void DispatchInputEndOfFile();
175 // If any of the streams are not set, set them to the in/out/err stream of
176 // the top most input reader to ensure they at least have something
177 void AdoptTopIOHandlerFilesIfInvalid(lldb::StreamFileSP &in,
178 lldb::StreamFileSP &out,
179 lldb::StreamFileSP &err);
181 void PushIOHandler(const lldb::IOHandlerSP &reader_sp,
182 bool cancel_top_handler = true);
184 bool PopIOHandler(const lldb::IOHandlerSP &reader_sp);
186 // Synchronously run an input reader until it is done
187 void RunIOHandler(const lldb::IOHandlerSP &reader_sp);
189 bool IsTopIOHandler(const lldb::IOHandlerSP &reader_sp);
191 bool CheckTopIOHandlerTypes(IOHandler::Type top_type,
192 IOHandler::Type second_top_type);
194 void PrintAsync(const char *s, size_t len, bool is_stdout);
196 ConstString GetTopIOHandlerControlSequence(char ch);
198 const char *GetIOHandlerCommandPrefix();
200 const char *GetIOHandlerHelpPrologue();
202 void ClearIOHandlers();
204 bool GetCloseInputOnEOF() const;
206 void SetCloseInputOnEOF(bool b);
208 bool EnableLog(llvm::StringRef channel,
209 llvm::ArrayRef<const char *> categories,
210 llvm::StringRef log_file, uint32_t log_options,
211 llvm::raw_ostream &error_stream);
213 void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);
215 // Properties Functions
216 enum StopDisassemblyType {
217 eStopDisassemblyTypeNever = 0,
218 eStopDisassemblyTypeNoDebugInfo,
219 eStopDisassemblyTypeNoSource,
220 eStopDisassemblyTypeAlways
223 Status SetPropertyValue(const ExecutionContext *exe_ctx,
224 VarSetOperationType op, llvm::StringRef property_path,
225 llvm::StringRef value) override;
227 bool GetAutoConfirm() const;
229 const FormatEntity::Entry *GetDisassemblyFormat() const;
231 const FormatEntity::Entry *GetFrameFormat() const;
233 const FormatEntity::Entry *GetFrameFormatUnique() const;
235 const FormatEntity::Entry *GetThreadFormat() const;
237 const FormatEntity::Entry *GetThreadStopFormat() const;
239 lldb::ScriptLanguage GetScriptLanguage() const;
241 bool SetScriptLanguage(lldb::ScriptLanguage script_lang);
243 uint32_t GetTerminalWidth() const;
245 bool SetTerminalWidth(uint32_t term_width);
247 llvm::StringRef GetPrompt() const;
249 void SetPrompt(llvm::StringRef p);
250 void SetPrompt(const char *) = delete;
252 llvm::StringRef GetReproducerPath() const;
254 bool GetUseExternalEditor() const;
256 bool SetUseExternalEditor(bool use_external_editor_p);
258 bool GetUseColor() const;
260 bool SetUseColor(bool use_color);
262 bool GetHighlightSource() const;
264 lldb::StopShowColumn GetStopShowColumn() const;
266 llvm::StringRef GetStopShowColumnAnsiPrefix() const;
268 llvm::StringRef GetStopShowColumnAnsiSuffix() const;
270 uint32_t GetStopSourceLineCount(bool before) const;
272 StopDisassemblyType GetStopDisassemblyDisplay() const;
274 uint32_t GetDisassemblyLineCount() const;
276 bool GetAutoOneLineSummaries() const;
278 bool GetAutoIndent() const;
280 bool SetAutoIndent(bool b);
282 bool GetPrintDecls() const;
284 bool SetPrintDecls(bool b);
286 uint32_t GetTabSize() const;
288 bool SetTabSize(uint32_t tab_size);
290 bool GetEscapeNonPrintables() const;
292 bool GetNotifyVoid() const;
294 ConstString GetInstanceName() { return m_instance_name; }
296 bool LoadPlugin(const FileSpec &spec, Status &error);
298 void ExecuteIOHandlers();
300 bool IsForwardingEvents();
302 void EnableForwardEvents(const lldb::ListenerSP &listener_sp);
304 void CancelForwardEvents(const lldb::ListenerSP &listener_sp);
306 bool IsHandlingEvents() const { return m_event_handler_thread.IsJoinable(); }
308 Status RunREPL(lldb::LanguageType language, const char *repl_options);
310 // This is for use in the command interpreter, when you either want the
311 // selected target, or if no target is present you want to prime the dummy
312 // target with entities that will be copied over to new targets.
313 Target *GetSelectedOrDummyTarget(bool prefer_dummy = false);
314 Target *GetDummyTarget();
316 lldb::BroadcasterManagerSP GetBroadcasterManager() {
317 return m_broadcaster_manager_sp;
321 friend class CommandInterpreter;
324 bool StartEventHandlerThread();
326 void StopEventHandlerThread();
328 static lldb::thread_result_t EventHandlerThread(lldb::thread_arg_t arg);
330 bool HasIOHandlerThread();
332 bool StartIOHandlerThread();
334 void StopIOHandlerThread();
336 void JoinIOHandlerThread();
338 static lldb::thread_result_t IOHandlerThread(lldb::thread_arg_t arg);
340 void DefaultEventHandler();
342 void HandleBreakpointEvent(const lldb::EventSP &event_sp);
344 void HandleProcessEvent(const lldb::EventSP &event_sp);
346 void HandleThreadEvent(const lldb::EventSP &event_sp);
348 size_t GetProcessSTDOUT(Process *process, Stream *stream);
350 size_t GetProcessSTDERR(Process *process, Stream *stream);
352 SourceManager::SourceFileCache &GetSourceFileCache() {
353 return m_source_file_cache;
356 void InstanceInitialize();
358 lldb::StreamFileSP m_input_file_sp;
359 lldb::StreamFileSP m_output_file_sp;
360 lldb::StreamFileSP m_error_file_sp;
362 /// Used for shadowing the input file when capturing a reproducer.
363 repro::DataRecorder *m_input_recorder;
365 lldb::BroadcasterManagerSP m_broadcaster_manager_sp; // The debugger acts as a
366 // broadcaster manager of
368 // It needs to get constructed before the target_list or any other member
369 // that might want to broadcast through the debugger.
371 TerminalState m_terminal_state;
372 TargetList m_target_list;
374 PlatformList m_platform_list;
375 lldb::ListenerSP m_listener_sp;
376 std::unique_ptr<SourceManager> m_source_manager_up; // This is a scratch
377 // source manager that we
378 // return if we have no
380 SourceManager::SourceFileCache m_source_file_cache; // All the source managers
381 // for targets created in
382 // this debugger used this
384 // source file cache.
385 std::unique_ptr<CommandInterpreter> m_command_interpreter_up;
387 lldb::ScriptInterpreterSP m_script_interpreter_sp;
388 std::recursive_mutex m_script_interpreter_mutex;
390 IOHandlerStack m_input_reader_stack;
391 llvm::StringMap<std::weak_ptr<llvm::raw_ostream>> m_log_streams;
392 std::shared_ptr<llvm::raw_ostream> m_log_callback_stream_sp;
393 ConstString m_instance_name;
394 static LoadPluginCallbackType g_load_plugin_callback;
395 typedef std::vector<llvm::sys::DynamicLibrary> LoadedPluginsList;
396 LoadedPluginsList m_loaded_plugins;
397 HostThread m_event_handler_thread;
398 HostThread m_io_handler_thread;
399 Broadcaster m_sync_broadcaster;
400 lldb::ListenerSP m_forward_listener_sp;
401 llvm::once_flag m_clear_once;
403 // Events for m_sync_broadcaster
405 eBroadcastBitEventThreadIsListening = (1 << 0),
409 // Use Debugger::CreateInstance() to get a shared pointer to a new debugger
411 Debugger(lldb::LogOutputCallback m_log_callback, void *baton);
413 DISALLOW_COPY_AND_ASSIGN(Debugger);
416 } // namespace lldb_private
418 #endif // liblldb_Debugger_h_