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