1 //===-- Debugger.h ----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_Debugger_h_ 11 #define liblldb_Debugger_h_ 12 13 #include <stdint.h> 14 15 #include <memory> 16 #include <vector> 17 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" 38 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" 44 45 #include <assert.h> 46 #include <stddef.h> 47 #include <stdio.h> 48 49 namespace lldb_private { 50 class Address; 51 } 52 namespace lldb_private { 53 class CommandInterpreter; 54 } 55 namespace lldb_private { 56 class Process; 57 } 58 namespace lldb_private { 59 class Stream; 60 } 61 namespace lldb_private { 62 class SymbolContext; 63 } 64 namespace lldb_private { 65 class Target; 66 } 67 namespace llvm { 68 class raw_ostream; 69 } 70 71 namespace lldb_private { 72 73 //---------------------------------------------------------------------- 74 /// @class Debugger Debugger.h "lldb/Core/Debugger.h" 75 /// A class to manage flag bits. 76 /// 77 /// Provides a global root objects for the debugger core. 78 //---------------------------------------------------------------------- 79 80 class Debugger : public std::enable_shared_from_this<Debugger>, 81 public UserID, 82 public Properties { 83 friend class SourceManager; // For GetSourceFileCache. 84 85 public: 86 ~Debugger() override; 87 88 static lldb::DebuggerSP 89 CreateInstance(lldb::LogOutputCallback log_callback = nullptr, 90 void *baton = nullptr); 91 92 static lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid); 93 94 static lldb::TargetSP FindTargetWithProcess(Process *process); 95 96 static void Initialize(LoadPluginCallbackType load_plugin_callback); 97 98 static void Terminate(); 99 100 static void SettingsInitialize(); 101 102 static void SettingsTerminate(); 103 104 static void Destroy(lldb::DebuggerSP &debugger_sp); 105 106 static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id); 107 108 static lldb::DebuggerSP 109 FindDebuggerWithInstanceName(const ConstString &instance_name); 110 111 static size_t GetNumDebuggers(); 112 113 static lldb::DebuggerSP GetDebuggerAtIndex(size_t index); 114 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); 120 121 void Clear(); 122 123 bool GetAsyncExecution(); 124 125 void SetAsyncExecution(bool async); 126 GetInputFile()127 lldb::StreamFileSP GetInputFile() { return m_input_file_sp; } 128 GetOutputFile()129 lldb::StreamFileSP GetOutputFile() { return m_output_file_sp; } 130 GetErrorFile()131 lldb::StreamFileSP GetErrorFile() { return m_error_file_sp; } 132 133 void SetInputFileHandle(FILE *fh, bool tranfer_ownership); 134 135 void SetOutputFileHandle(FILE *fh, bool tranfer_ownership); 136 137 void SetErrorFileHandle(FILE *fh, bool tranfer_ownership); 138 139 void SaveInputTerminalState(); 140 141 void RestoreInputTerminalState(); 142 143 lldb::StreamSP GetAsyncOutputStream(); 144 145 lldb::StreamSP GetAsyncErrorStream(); 146 GetCommandInterpreter()147 CommandInterpreter &GetCommandInterpreter() { 148 assert(m_command_interpreter_ap.get()); 149 return *m_command_interpreter_ap; 150 } 151 GetListener()152 lldb::ListenerSP GetListener() { return m_listener_sp; } 153 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(); 159 GetSelectedTarget()160 lldb::TargetSP GetSelectedTarget() { 161 return m_target_list.GetSelectedTarget(); 162 } 163 164 ExecutionContext GetSelectedExecutionContext(); 165 //------------------------------------------------------------------ 166 /// Get accessor for the target list. 167 /// 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. 171 /// 172 /// @return 173 /// A global shared target list. 174 //------------------------------------------------------------------ GetTargetList()175 TargetList &GetTargetList() { return m_target_list; } 176 GetPlatformList()177 PlatformList &GetPlatformList() { return m_platform_list; } 178 179 void DispatchInputInterrupt(); 180 181 void DispatchInputEndOfFile(); 182 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); 190 191 void PushIOHandler(const lldb::IOHandlerSP &reader_sp, 192 bool cancel_top_handler = true); 193 194 bool PopIOHandler(const lldb::IOHandlerSP &reader_sp); 195 196 // Synchronously run an input reader until it is done 197 void RunIOHandler(const lldb::IOHandlerSP &reader_sp); 198 199 bool IsTopIOHandler(const lldb::IOHandlerSP &reader_sp); 200 201 bool CheckTopIOHandlerTypes(IOHandler::Type top_type, 202 IOHandler::Type second_top_type); 203 204 void PrintAsync(const char *s, size_t len, bool is_stdout); 205 206 ConstString GetTopIOHandlerControlSequence(char ch); 207 208 const char *GetIOHandlerCommandPrefix(); 209 210 const char *GetIOHandlerHelpPrologue(); 211 212 void ClearIOHandlers(); 213 214 bool GetCloseInputOnEOF() const; 215 216 void SetCloseInputOnEOF(bool b); 217 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); 222 223 void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton); 224 225 //---------------------------------------------------------------------- 226 // Properties Functions 227 //---------------------------------------------------------------------- 228 enum StopDisassemblyType { 229 eStopDisassemblyTypeNever = 0, 230 eStopDisassemblyTypeNoDebugInfo, 231 eStopDisassemblyTypeNoSource, 232 eStopDisassemblyTypeAlways 233 }; 234 235 Status SetPropertyValue(const ExecutionContext *exe_ctx, 236 VarSetOperationType op, llvm::StringRef property_path, 237 llvm::StringRef value) override; 238 239 bool GetAutoConfirm() const; 240 241 const FormatEntity::Entry *GetDisassemblyFormat() const; 242 243 const FormatEntity::Entry *GetFrameFormat() const; 244 245 const FormatEntity::Entry *GetFrameFormatUnique() const; 246 247 const FormatEntity::Entry *GetThreadFormat() const; 248 249 const FormatEntity::Entry *GetThreadStopFormat() const; 250 251 lldb::ScriptLanguage GetScriptLanguage() const; 252 253 bool SetScriptLanguage(lldb::ScriptLanguage script_lang); 254 255 uint32_t GetTerminalWidth() const; 256 257 bool SetTerminalWidth(uint32_t term_width); 258 259 llvm::StringRef GetPrompt() const; 260 261 void SetPrompt(llvm::StringRef p); 262 void SetPrompt(const char *) = delete; 263 264 llvm::StringRef GetReproducerPath() const; 265 266 bool GetUseExternalEditor() const; 267 268 bool SetUseExternalEditor(bool use_external_editor_p); 269 270 bool GetUseColor() const; 271 272 bool SetUseColor(bool use_color); 273 274 bool GetHighlightSource() const; 275 276 lldb::StopShowColumn GetStopShowColumn() const; 277 278 llvm::StringRef GetStopShowColumnAnsiPrefix() const; 279 280 llvm::StringRef GetStopShowColumnAnsiSuffix() const; 281 282 uint32_t GetStopSourceLineCount(bool before) const; 283 284 StopDisassemblyType GetStopDisassemblyDisplay() const; 285 286 uint32_t GetDisassemblyLineCount() const; 287 288 bool GetAutoOneLineSummaries() const; 289 290 bool GetAutoIndent() const; 291 292 bool SetAutoIndent(bool b); 293 294 bool GetPrintDecls() const; 295 296 bool SetPrintDecls(bool b); 297 298 uint32_t GetTabSize() const; 299 300 bool SetTabSize(uint32_t tab_size); 301 302 bool GetEscapeNonPrintables() const; 303 304 bool GetNotifyVoid() const; 305 GetInstanceName()306 const ConstString &GetInstanceName() { return m_instance_name; } 307 308 bool LoadPlugin(const FileSpec &spec, Status &error); 309 310 void ExecuteIOHandlers(); 311 312 bool IsForwardingEvents(); 313 314 void EnableForwardEvents(const lldb::ListenerSP &listener_sp); 315 316 void CancelForwardEvents(const lldb::ListenerSP &listener_sp); 317 IsHandlingEvents()318 bool IsHandlingEvents() const { return m_event_handler_thread.IsJoinable(); } 319 320 Status RunREPL(lldb::LanguageType language, const char *repl_options); 321 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(); 327 GetBroadcasterManager()328 lldb::BroadcasterManagerSP GetBroadcasterManager() { 329 return m_broadcaster_manager_sp; 330 } 331 332 protected: 333 friend class CommandInterpreter; 334 friend class REPL; 335 336 bool StartEventHandlerThread(); 337 338 void StopEventHandlerThread(); 339 340 static lldb::thread_result_t EventHandlerThread(lldb::thread_arg_t arg); 341 342 bool HasIOHandlerThread(); 343 344 bool StartIOHandlerThread(); 345 346 void StopIOHandlerThread(); 347 348 void JoinIOHandlerThread(); 349 350 static lldb::thread_result_t IOHandlerThread(lldb::thread_arg_t arg); 351 352 void DefaultEventHandler(); 353 354 void HandleBreakpointEvent(const lldb::EventSP &event_sp); 355 356 void HandleProcessEvent(const lldb::EventSP &event_sp); 357 358 void HandleThreadEvent(const lldb::EventSP &event_sp); 359 360 size_t GetProcessSTDOUT(Process *process, Stream *stream); 361 362 size_t GetProcessSTDERR(Process *process, Stream *stream); 363 GetSourceFileCache()364 SourceManager::SourceFileCache &GetSourceFileCache() { 365 return m_source_file_cache; 366 } 367 368 void InstanceInitialize(); 369 370 lldb::StreamFileSP m_input_file_sp; 371 lldb::StreamFileSP m_output_file_sp; 372 lldb::StreamFileSP m_error_file_sp; 373 374 lldb::BroadcasterManagerSP m_broadcaster_manager_sp; // The debugger acts as a 375 // broadcaster manager of 376 // last resort. 377 // It needs to get constructed before the target_list or any other member 378 // that might want to broadcast through the debugger. 379 380 TerminalState m_terminal_state; 381 TargetList m_target_list; 382 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 388 // targets. 389 SourceManager::SourceFileCache m_source_file_cache; // All the source managers 390 // for targets created in 391 // this debugger used this 392 // shared 393 // source file cache. 394 std::unique_ptr<CommandInterpreter> m_command_interpreter_ap; 395 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; 408 409 //---------------------------------------------------------------------- 410 // Events for m_sync_broadcaster 411 //---------------------------------------------------------------------- 412 enum { 413 eBroadcastBitEventThreadIsListening = (1 << 0), 414 }; 415 416 private: 417 // Use Debugger::CreateInstance() to get a shared pointer to a new debugger 418 // object 419 Debugger(lldb::LogOutputCallback m_log_callback, void *baton); 420 421 DISALLOW_COPY_AND_ASSIGN(Debugger); 422 }; 423 424 } // namespace lldb_private 425 426 #endif // liblldb_Debugger_h_ 427