]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/Debugger.h
Pull down pjdfstest 0.1
[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 <map>
18 #include <memory>
19 #include <mutex>
20 #include <vector>
21
22 // Other libraries and framework includes
23 // Project includes
24 #include "lldb/Core/Broadcaster.h"
25 #include "lldb/Core/FormatEntity.h"
26 #include "lldb/Core/IOHandler.h"
27 #include "lldb/Core/Listener.h"
28 #include "lldb/Core/SourceManager.h"
29 #include "lldb/Core/UserID.h"
30 #include "lldb/Core/UserSettingsController.h"
31 #include "lldb/Host/HostThread.h"
32 #include "lldb/Host/Terminal.h"
33 #include "lldb/Target/Platform.h"
34 #include "lldb/Target/TargetList.h"
35 #include "lldb/lldb-public.h"
36
37 namespace llvm {
38 namespace sys {
39 class DynamicLibrary;
40 } // namespace sys
41 } // namespace llvm
42
43 namespace lldb_private {
44
45 //----------------------------------------------------------------------
46 /// @class Debugger Debugger.h "lldb/Core/Debugger.h"
47 /// @brief A class to manage flag bits.
48 ///
49 /// Provides a global root objects for the debugger core.
50 //----------------------------------------------------------------------
51
52 class Debugger : public std::enable_shared_from_this<Debugger>,
53                  public UserID,
54                  public Properties {
55   friend class SourceManager; // For GetSourceFileCache.
56
57 public:
58   ~Debugger() override;
59
60   static lldb::DebuggerSP
61   CreateInstance(lldb::LogOutputCallback log_callback = nullptr,
62                  void *baton = nullptr);
63
64   static lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid);
65
66   static lldb::TargetSP FindTargetWithProcess(Process *process);
67
68   static void Initialize(LoadPluginCallbackType load_plugin_callback);
69
70   static void Terminate();
71
72   static void SettingsInitialize();
73
74   static void SettingsTerminate();
75
76   static void Destroy(lldb::DebuggerSP &debugger_sp);
77
78   static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id);
79
80   static lldb::DebuggerSP
81   FindDebuggerWithInstanceName(const ConstString &instance_name);
82
83   static size_t GetNumDebuggers();
84
85   static lldb::DebuggerSP GetDebuggerAtIndex(size_t index);
86
87   static bool FormatDisassemblerAddress(const FormatEntity::Entry *format,
88                                         const SymbolContext *sc,
89                                         const SymbolContext *prev_sc,
90                                         const ExecutionContext *exe_ctx,
91                                         const Address *addr, Stream &s);
92
93   void Clear();
94
95   bool GetAsyncExecution();
96
97   void SetAsyncExecution(bool async);
98
99   lldb::StreamFileSP GetInputFile() { return m_input_file_sp; }
100
101   lldb::StreamFileSP GetOutputFile() { return m_output_file_sp; }
102
103   lldb::StreamFileSP GetErrorFile() { return m_error_file_sp; }
104
105   void SetInputFileHandle(FILE *fh, bool tranfer_ownership);
106
107   void SetOutputFileHandle(FILE *fh, bool tranfer_ownership);
108
109   void SetErrorFileHandle(FILE *fh, bool tranfer_ownership);
110
111   void SaveInputTerminalState();
112
113   void RestoreInputTerminalState();
114
115   lldb::StreamSP GetAsyncOutputStream();
116
117   lldb::StreamSP GetAsyncErrorStream();
118
119   CommandInterpreter &GetCommandInterpreter() {
120     assert(m_command_interpreter_ap.get());
121     return *m_command_interpreter_ap;
122   }
123
124   lldb::ListenerSP GetListener() { return m_listener_sp; }
125
126   // This returns the Debugger's scratch source manager.  It won't be able to
127   // look up files in debug
128   // information, but it can look up files by absolute path and display them to
129   // you.
130   // To get the target's source manager, call GetSourceManager on the target
131   // instead.
132   SourceManager &GetSourceManager();
133
134   lldb::TargetSP GetSelectedTarget() {
135     return m_target_list.GetSelectedTarget();
136   }
137
138   ExecutionContext GetSelectedExecutionContext();
139   //------------------------------------------------------------------
140   /// Get accessor for the target list.
141   ///
142   /// The target list is part of the global debugger object. This
143   /// the single debugger shared instance to control where targets
144   /// get created and to allow for tracking and searching for targets
145   /// based on certain criteria.
146   ///
147   /// @return
148   ///     A global shared target list.
149   //------------------------------------------------------------------
150   TargetList &GetTargetList() { return m_target_list; }
151
152   PlatformList &GetPlatformList() { return m_platform_list; }
153
154   void DispatchInputInterrupt();
155
156   void DispatchInputEndOfFile();
157
158   //------------------------------------------------------------------
159   // If any of the streams are not set, set them to the in/out/err
160   // stream of the top most input reader to ensure they at least have
161   // something
162   //------------------------------------------------------------------
163   void AdoptTopIOHandlerFilesIfInvalid(lldb::StreamFileSP &in,
164                                        lldb::StreamFileSP &out,
165                                        lldb::StreamFileSP &err);
166
167   void PushIOHandler(const lldb::IOHandlerSP &reader_sp);
168
169   bool PopIOHandler(const lldb::IOHandlerSP &reader_sp);
170
171   // Synchronously run an input reader until it is done
172   void RunIOHandler(const lldb::IOHandlerSP &reader_sp);
173
174   bool IsTopIOHandler(const lldb::IOHandlerSP &reader_sp);
175
176   bool CheckTopIOHandlerTypes(IOHandler::Type top_type,
177                               IOHandler::Type second_top_type);
178
179   void PrintAsync(const char *s, size_t len, bool is_stdout);
180
181   ConstString GetTopIOHandlerControlSequence(char ch);
182
183   const char *GetIOHandlerCommandPrefix();
184
185   const char *GetIOHandlerHelpPrologue();
186
187   void ClearIOHandlers();
188
189   bool GetCloseInputOnEOF() const;
190
191   void SetCloseInputOnEOF(bool b);
192
193   bool EnableLog(const char *channel, const char **categories,
194                  const char *log_file, uint32_t log_options,
195                  Stream &error_stream);
196
197   void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);
198
199   //----------------------------------------------------------------------
200   // Properties Functions
201   //----------------------------------------------------------------------
202   enum StopDisassemblyType {
203     eStopDisassemblyTypeNever = 0,
204     eStopDisassemblyTypeNoDebugInfo,
205     eStopDisassemblyTypeNoSource,
206     eStopDisassemblyTypeAlways
207   };
208
209   Error SetPropertyValue(const ExecutionContext *exe_ctx,
210                          VarSetOperationType op, llvm::StringRef property_path,
211     llvm::StringRef value) override;
212
213   bool GetAutoConfirm() const;
214
215   const FormatEntity::Entry *GetDisassemblyFormat() const;
216
217   const FormatEntity::Entry *GetFrameFormat() const;
218
219   const FormatEntity::Entry *GetThreadFormat() const;
220
221   const FormatEntity::Entry *GetThreadStopFormat() const;
222
223   lldb::ScriptLanguage GetScriptLanguage() const;
224
225   bool SetScriptLanguage(lldb::ScriptLanguage script_lang);
226
227   uint32_t GetTerminalWidth() const;
228
229   bool SetTerminalWidth(uint32_t term_width);
230
231   llvm::StringRef GetPrompt() const;
232
233   void SetPrompt(llvm::StringRef p);
234   void SetPrompt(const char *) = delete;
235
236   bool GetUseExternalEditor() const;
237
238   bool SetUseExternalEditor(bool use_external_editor_p);
239
240   bool GetUseColor() const;
241
242   bool SetUseColor(bool use_color);
243
244   lldb::StopShowColumn GetStopShowColumn() const;
245
246   const FormatEntity::Entry *GetStopShowColumnAnsiPrefix() const;
247
248   const FormatEntity::Entry *GetStopShowColumnAnsiSuffix() const;
249
250   uint32_t GetStopSourceLineCount(bool before) const;
251
252   StopDisassemblyType GetStopDisassemblyDisplay() const;
253
254   uint32_t GetDisassemblyLineCount() const;
255
256   bool GetAutoOneLineSummaries() const;
257
258   bool GetAutoIndent() const;
259
260   bool SetAutoIndent(bool b);
261
262   bool GetPrintDecls() const;
263
264   bool SetPrintDecls(bool b);
265
266   uint32_t GetTabSize() const;
267
268   bool SetTabSize(uint32_t tab_size);
269
270   bool GetEscapeNonPrintables() const;
271
272   bool GetNotifyVoid() const;
273
274   const ConstString &GetInstanceName() { return m_instance_name; }
275
276   bool LoadPlugin(const FileSpec &spec, Error &error);
277
278   void ExecuteIOHandlers();
279
280   bool IsForwardingEvents();
281
282   void EnableForwardEvents(const lldb::ListenerSP &listener_sp);
283
284   void CancelForwardEvents(const lldb::ListenerSP &listener_sp);
285
286   bool IsHandlingEvents() const { return m_event_handler_thread.IsJoinable(); }
287
288   Error RunREPL(lldb::LanguageType language, const char *repl_options);
289
290   // This is for use in the command interpreter, when you either want the
291   // selected target, or if no target
292   // is present you want to prime the dummy target with entities that will be
293   // copied over to new targets.
294   Target *GetSelectedOrDummyTarget(bool prefer_dummy = false);
295   Target *GetDummyTarget();
296
297   lldb::BroadcasterManagerSP GetBroadcasterManager() {
298     return m_broadcaster_manager_sp;
299   }
300
301 protected:
302   friend class CommandInterpreter;
303   friend class REPL;
304
305   bool StartEventHandlerThread();
306
307   void StopEventHandlerThread();
308
309   static lldb::thread_result_t EventHandlerThread(lldb::thread_arg_t arg);
310
311   bool HasIOHandlerThread();
312
313   bool StartIOHandlerThread();
314
315   void StopIOHandlerThread();
316
317   void JoinIOHandlerThread();
318
319   static lldb::thread_result_t IOHandlerThread(lldb::thread_arg_t arg);
320
321   void DefaultEventHandler();
322
323   void HandleBreakpointEvent(const lldb::EventSP &event_sp);
324
325   void HandleProcessEvent(const lldb::EventSP &event_sp);
326
327   void HandleThreadEvent(const lldb::EventSP &event_sp);
328
329   size_t GetProcessSTDOUT(Process *process, Stream *stream);
330
331   size_t GetProcessSTDERR(Process *process, Stream *stream);
332
333   SourceManager::SourceFileCache &GetSourceFileCache() {
334     return m_source_file_cache;
335   }
336
337   void InstanceInitialize();
338
339   lldb::StreamFileSP m_input_file_sp;
340   lldb::StreamFileSP m_output_file_sp;
341   lldb::StreamFileSP m_error_file_sp;
342
343   lldb::BroadcasterManagerSP m_broadcaster_manager_sp; // The debugger acts as a
344                                                        // broadcaster manager of
345                                                        // last resort.
346   // It needs to get constructed before the target_list or any other
347   // member that might want to broadcast through the debugger.
348
349   TerminalState m_terminal_state;
350   TargetList m_target_list;
351
352   PlatformList m_platform_list;
353   lldb::ListenerSP m_listener_sp;
354   std::unique_ptr<SourceManager> m_source_manager_ap; // This is a scratch
355                                                       // source manager that we
356                                                       // return if we have no
357                                                       // targets.
358   SourceManager::SourceFileCache m_source_file_cache; // All the source managers
359                                                       // for targets created in
360                                                       // this debugger used this
361                                                       // shared
362                                                       // source file cache.
363   std::unique_ptr<CommandInterpreter> m_command_interpreter_ap;
364
365   IOHandlerStack m_input_reader_stack;
366   typedef std::map<std::string, lldb::StreamWP> LogStreamMap;
367   LogStreamMap m_log_streams;
368   lldb::StreamSP m_log_callback_stream_sp;
369   ConstString m_instance_name;
370   static LoadPluginCallbackType g_load_plugin_callback;
371   typedef std::vector<llvm::sys::DynamicLibrary> LoadedPluginsList;
372   LoadedPluginsList m_loaded_plugins;
373   HostThread m_event_handler_thread;
374   HostThread m_io_handler_thread;
375   Broadcaster m_sync_broadcaster;
376   lldb::ListenerSP m_forward_listener_sp;
377   std::once_flag m_clear_once;
378
379   //----------------------------------------------------------------------
380   // Events for m_sync_broadcaster
381   //----------------------------------------------------------------------
382   enum {
383     eBroadcastBitEventThreadIsListening = (1 << 0),
384   };
385
386 private:
387   // Use Debugger::CreateInstance() to get a shared pointer to a new
388   // debugger object
389   Debugger(lldb::LogOutputCallback m_log_callback, void *baton);
390
391   DISALLOW_COPY_AND_ASSIGN(Debugger);
392 };
393
394 } // namespace lldb_private
395
396 #endif // liblldb_Debugger_h_