Debugger.h revision 6c530f2201be4788dedf3d5970399220fbd50b11
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#if defined(__cplusplus) 13 14 15#include <stdint.h> 16#include <unistd.h> 17 18#include <stack> 19 20#include "lldb/lldb-public.h" 21#include "lldb/Core/Broadcaster.h" 22#include "lldb/Core/Communication.h" 23#include "lldb/Core/FormatManager.h" 24#include "lldb/Core/InputReaderStack.h" 25#include "lldb/Core/Listener.h" 26#include "lldb/Core/StreamFile.h" 27#include "lldb/Core/SourceManager.h" 28#include "lldb/Core/UserID.h" 29#include "lldb/Core/UserSettingsController.h" 30#include "lldb/Target/ExecutionContext.h" 31#include "lldb/Target/Platform.h" 32#include "lldb/Target/TargetList.h" 33 34namespace lldb_private { 35 36//---------------------------------------------------------------------- 37/// @class Debugger Debugger.h "lldb/Core/Debugger.h" 38/// @brief A class to manage flag bits. 39/// 40/// Provides a global root objects for the debugger core. 41//---------------------------------------------------------------------- 42 43 44class DebuggerInstanceSettings : public InstanceSettings 45{ 46public: 47 48 enum StopDisassemblyType 49 { 50 eStopDisassemblyTypeNever = 0, 51 eStopDisassemblyTypeNoSource, 52 eStopDisassemblyTypeAlways 53 }; 54 55 56 DebuggerInstanceSettings (const lldb::UserSettingsControllerSP &m_owner_sp, bool live_instance = true, const char *name = NULL); 57 58 DebuggerInstanceSettings (const DebuggerInstanceSettings &rhs); 59 60 virtual 61 ~DebuggerInstanceSettings (); 62 63 DebuggerInstanceSettings& 64 operator= (const DebuggerInstanceSettings &rhs); 65 66 void 67 UpdateInstanceSettingsVariable (const ConstString &var_name, 68 const char *index_value, 69 const char *value, 70 const ConstString &instance_name, 71 const SettingEntry &entry, 72 VarSetOperationType op, 73 Error &err, 74 bool pending); 75 76 bool 77 GetInstanceSettingsValue (const SettingEntry &entry, 78 const ConstString &var_name, 79 StringList &value, 80 Error *err); 81 82 uint32_t 83 GetTerminalWidth () const 84 { 85 return m_term_width; 86 } 87 88 void 89 SetTerminalWidth (uint32_t term_width) 90 { 91 m_term_width = term_width; 92 } 93 94 uint32_t 95 GetStopSourceLineCount (bool before) const 96 { 97 if (before) 98 return m_stop_source_before_count; 99 else 100 return m_stop_source_after_count; 101 } 102 103 104 void 105 SetStopSourceLineCount (bool before, uint32_t n) 106 { 107 if (before) 108 m_stop_source_before_count = n; 109 else 110 m_stop_source_after_count = n; 111 } 112 113 StopDisassemblyType 114 GetStopDisassemblyDisplay () const 115 { 116 return m_stop_disassembly_display; 117 } 118 119 120 uint32_t 121 GetDisassemblyLineCount () const 122 { 123 return m_stop_disassembly_count; 124 } 125 126 void 127 SetDisassemblyLineCount (uint32_t n) 128 { 129 m_stop_disassembly_count = n; 130 } 131 132 const char * 133 GetPrompt() const 134 { 135 return m_prompt.c_str(); 136 } 137 138 void 139 SetPrompt(const char *p) 140 { 141 if (p) 142 m_prompt.assign (p); 143 else 144 m_prompt.assign ("(lldb) "); 145 BroadcastPromptChange (m_instance_name, m_prompt.c_str()); 146 } 147 148 const char * 149 GetFrameFormat() const 150 { 151 return m_frame_format.c_str(); 152 } 153 154 bool 155 SetFrameFormat(const char *frame_format) 156 { 157 if (frame_format && frame_format[0]) 158 { 159 m_frame_format.assign (frame_format); 160 return true; 161 } 162 return false; 163 } 164 165 const char * 166 GetThreadFormat() const 167 { 168 return m_thread_format.c_str(); 169 } 170 171 bool 172 SetThreadFormat(const char *thread_format) 173 { 174 if (thread_format && thread_format[0]) 175 { 176 m_thread_format.assign (thread_format); 177 return true; 178 } 179 return false; 180 } 181 182 lldb::ScriptLanguage 183 GetScriptLanguage() const 184 { 185 return m_script_lang; 186 } 187 188 void 189 SetScriptLanguage (lldb::ScriptLanguage script_lang) 190 { 191 m_script_lang = script_lang; 192 } 193 194 bool 195 GetUseExternalEditor () const 196 { 197 return m_use_external_editor; 198 } 199 200 bool 201 SetUseExternalEditor (bool use_external_editor_p) 202 { 203 bool old_value = m_use_external_editor; 204 m_use_external_editor = use_external_editor_p; 205 return old_value; 206 } 207 208 bool 209 GetAutoConfirm () const 210 { 211 return m_auto_confirm_on; 212 } 213 214 void 215 SetAutoConfirm (bool auto_confirm_on) 216 { 217 m_auto_confirm_on = auto_confirm_on; 218 } 219 220protected: 221 222 void 223 CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, 224 bool pending); 225 226 bool 227 BroadcastPromptChange (const ConstString &instance_name, const char *new_prompt); 228 229 bool 230 ValidTermWidthValue (const char *value, Error err); 231 232 const ConstString 233 CreateInstanceName (); 234 235 static OptionEnumValueElement g_show_disassembly_enum_values[]; 236 237private: 238 239 uint32_t m_term_width; 240 uint32_t m_stop_source_before_count; 241 uint32_t m_stop_source_after_count; 242 uint32_t m_stop_disassembly_count; 243 StopDisassemblyType m_stop_disassembly_display; 244 std::string m_prompt; 245 std::string m_frame_format; 246 std::string m_thread_format; 247 lldb::ScriptLanguage m_script_lang; 248 bool m_use_external_editor; 249 bool m_auto_confirm_on; 250}; 251 252 253 254class Debugger : 255 public std::tr1::enable_shared_from_this<Debugger>, 256 public UserID, 257 public DebuggerInstanceSettings, 258 public BroadcasterManager 259{ 260friend class SourceManager; // For GetSourceFileCache. 261 262public: 263 264 class SettingsController : public UserSettingsController 265 { 266 public: 267 268 SettingsController (); 269 270 virtual 271 ~SettingsController (); 272 273 static SettingEntry global_settings_table[]; 274 static SettingEntry instance_settings_table[]; 275 276 protected: 277 278 lldb::InstanceSettingsSP 279 CreateInstanceSettings (const char *instance_name); 280 281 private: 282 283 // Class-wide settings. 284 285 DISALLOW_COPY_AND_ASSIGN (SettingsController); 286 }; 287 288 static lldb::UserSettingsControllerSP & 289 GetSettingsController (); 290 291 static lldb::DebuggerSP 292 CreateInstance (lldb::LogOutputCallback log_callback = NULL, void *baton = NULL); 293 294 static lldb::TargetSP 295 FindTargetWithProcessID (lldb::pid_t pid); 296 297 static lldb::TargetSP 298 FindTargetWithProcess (Process *process); 299 300 static void 301 Initialize (); 302 303 static void 304 Terminate (); 305 306 static void 307 SettingsInitialize (); 308 309 static void 310 SettingsTerminate (); 311 312 static void 313 Destroy (lldb::DebuggerSP &debugger_sp); 314 315 virtual 316 ~Debugger (); 317 318 void Clear(); 319 320 bool 321 GetAsyncExecution (); 322 323 void 324 SetAsyncExecution (bool async); 325 326 File & 327 GetInputFile () 328 { 329 return m_input_file.GetFile(); 330 } 331 332 File & 333 GetOutputFile () 334 { 335 return m_output_file.GetFile(); 336 } 337 338 File & 339 GetErrorFile () 340 { 341 return m_error_file.GetFile(); 342 } 343 344 void 345 SetInputFileHandle (FILE *fh, bool tranfer_ownership); 346 347 void 348 SetOutputFileHandle (FILE *fh, bool tranfer_ownership); 349 350 void 351 SetErrorFileHandle (FILE *fh, bool tranfer_ownership); 352 353 Stream& 354 GetOutputStream () 355 { 356 return m_output_file; 357 } 358 359 Stream& 360 GetErrorStream () 361 { 362 return m_error_file; 363 } 364 365 lldb::StreamSP 366 GetAsyncOutputStream (); 367 368 lldb::StreamSP 369 GetAsyncErrorStream (); 370 371 CommandInterpreter & 372 GetCommandInterpreter () 373 { 374 assert (m_command_interpreter_ap.get()); 375 return *m_command_interpreter_ap; 376 } 377 378 Listener & 379 GetListener () 380 { 381 return m_listener; 382 } 383 384 // This returns the Debugger's scratch source manager. It won't be able to look up files in debug 385 // information, but it can look up files by absolute path and display them to you. 386 // To get the target's source manager, call GetSourceManager on the target instead. 387 SourceManager & 388 GetSourceManager () 389 { 390 return m_source_manager; 391 } 392 393public: 394 395 lldb::TargetSP 396 GetSelectedTarget () 397 { 398 return m_target_list.GetSelectedTarget (); 399 } 400 401 ExecutionContext 402 GetSelectedExecutionContext(); 403 //------------------------------------------------------------------ 404 /// Get accessor for the target list. 405 /// 406 /// The target list is part of the global debugger object. This 407 /// the single debugger shared instance to control where targets 408 /// get created and to allow for tracking and searching for targets 409 /// based on certain criteria. 410 /// 411 /// @return 412 /// A global shared target list. 413 //------------------------------------------------------------------ 414 TargetList & 415 GetTargetList () 416 { 417 return m_target_list; 418 } 419 420 PlatformList & 421 GetPlatformList () 422 { 423 return m_platform_list; 424 } 425 426 void 427 DispatchInputInterrupt (); 428 429 void 430 DispatchInputEndOfFile (); 431 432 void 433 DispatchInput (const char *bytes, size_t bytes_len); 434 435 void 436 WriteToDefaultReader (const char *bytes, size_t bytes_len); 437 438 void 439 PushInputReader (const lldb::InputReaderSP& reader_sp); 440 441 bool 442 PopInputReader (const lldb::InputReaderSP& reader_sp); 443 444 void 445 NotifyTopInputReader (lldb::InputReaderAction notification); 446 447 bool 448 InputReaderIsTopReader (const lldb::InputReaderSP& reader_sp); 449 450 static lldb::DebuggerSP 451 FindDebuggerWithID (lldb::user_id_t id); 452 453 static lldb::DebuggerSP 454 FindDebuggerWithInstanceName (const ConstString &instance_name); 455 456 static uint32_t 457 GetNumDebuggers(); 458 459 static lldb::DebuggerSP 460 GetDebuggerAtIndex (uint32_t); 461 462 static bool 463 FormatPrompt (const char *format, 464 const SymbolContext *sc, 465 const ExecutionContext *exe_ctx, 466 const Address *addr, 467 Stream &s, 468 const char **end, 469 ValueObject* valobj = NULL); 470 471 472 void 473 CleanUpInputReaders (); 474 475 static int 476 TestDebuggerRefCount (); 477 478 bool 479 GetCloseInputOnEOF () const; 480 481 void 482 SetCloseInputOnEOF (bool b); 483 484 bool 485 EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream); 486 487protected: 488 489 void 490 SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton); 491 492 static void 493 DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len); 494 495 lldb::InputReaderSP 496 GetCurrentInputReader (); 497 498 void 499 ActivateInputReader (const lldb::InputReaderSP &reader_sp); 500 501 bool 502 CheckIfTopInputReaderIsDone (); 503 504 void 505 DisconnectInput() 506 { 507 m_input_comm.Clear (); 508 } 509 510 SourceManager::SourceFileCache & 511 GetSourceFileCache () 512 { 513 return m_source_file_cache; 514 } 515 516 Communication m_input_comm; 517 StreamFile m_input_file; 518 StreamFile m_output_file; 519 StreamFile m_error_file; 520 TargetList m_target_list; 521 PlatformList m_platform_list; 522 Listener m_listener; 523 SourceManager m_source_manager; // This is a scratch source manager that we return if we have no targets. 524 SourceManager::SourceFileCache m_source_file_cache; // All the source managers for targets created in this debugger used this shared 525 // source file cache. 526 std::auto_ptr<CommandInterpreter> m_command_interpreter_ap; 527 528 InputReaderStack m_input_reader_stack; 529 std::string m_input_reader_data; 530 typedef std::map<std::string, lldb::StreamSP> LogStreamMap; 531 LogStreamMap m_log_streams; 532 lldb::StreamSP m_log_callback_stream_sp; 533 534private: 535 536 // Use Debugger::CreateInstance() to get a shared pointer to a new 537 // debugger object 538 Debugger (lldb::LogOutputCallback m_log_callback, void *baton); 539 540 DISALLOW_COPY_AND_ASSIGN (Debugger); 541 542}; 543 544} // namespace lldb_private 545 546#endif // #if defined(__cplusplus) 547#endif // liblldb_Debugger_h_ 548