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