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 
22 #include "lldb/API/SBDefines.h"
23 
24 #include "lldb/Core/Broadcaster.h"
25 #include "lldb/Core/Communication.h"
26 #include "lldb/Core/InputReaderStack.h"
27 #include "lldb/Core/Listener.h"
28 #include "lldb/Core/StreamFile.h"
29 #include "lldb/Core/SourceManager.h"
30 #include "lldb/Core/UserID.h"
31 #include "lldb/Core/UserSettingsController.h"
32 #include "lldb/DataFormatters/FormatManager.h"
33 #include "lldb/Host/Terminal.h"
34 #include "lldb/Interpreter/OptionValueProperties.h"
35 #include "lldb/Target/ExecutionContext.h"
36 #include "lldb/Target/Platform.h"
37 #include "lldb/Target/TargetList.h"
38 
39 namespace lldb_private {
40 
41 //----------------------------------------------------------------------
42 /// @class Debugger Debugger.h "lldb/Core/Debugger.h"
43 /// @brief A class to manage flag bits.
44 ///
45 /// Provides a global root objects for the debugger core.
46 //----------------------------------------------------------------------
47 
48 
49 class Debugger :
50     public std::enable_shared_from_this<Debugger>,
51     public UserID,
52     public Properties,
53     public BroadcasterManager
54 {
55 friend class SourceManager;  // For GetSourceFileCache.
56 
57 public:
58 
59     static lldb::DebuggerSP
60     CreateInstance (lldb::LogOutputCallback log_callback = NULL, void *baton = NULL);
61 
62     static lldb::TargetSP
63     FindTargetWithProcessID (lldb::pid_t pid);
64 
65     static lldb::TargetSP
66     FindTargetWithProcess (Process *process);
67 
68     static void
69     Initialize ();
70 
71     static void
72     Terminate ();
73 
74     static void
75     SettingsInitialize ();
76 
77     static void
78     SettingsTerminate ();
79 
80     static void
81     Destroy (lldb::DebuggerSP &debugger_sp);
82 
83     virtual
84     ~Debugger ();
85 
86     void Clear();
87 
88     bool
89     GetAsyncExecution ();
90 
91     void
92     SetAsyncExecution (bool async);
93 
94     File &
GetInputFile()95     GetInputFile ()
96     {
97         return m_input_file.GetFile();
98     }
99 
100     File &
GetOutputFile()101     GetOutputFile ()
102     {
103         return m_output_file.GetFile();
104     }
105 
106     File &
GetErrorFile()107     GetErrorFile ()
108     {
109         return m_error_file.GetFile();
110     }
111 
112     void
113     SetInputFileHandle (FILE *fh, bool tranfer_ownership);
114 
115     void
116     SetOutputFileHandle (FILE *fh, bool tranfer_ownership);
117 
118     void
119     SetErrorFileHandle (FILE *fh, bool tranfer_ownership);
120 
121     void
122     SaveInputTerminalState();
123 
124     void
125     RestoreInputTerminalState();
126 
127     Stream&
GetOutputStream()128     GetOutputStream ()
129     {
130         return m_output_file;
131     }
132 
133     Stream&
GetErrorStream()134     GetErrorStream ()
135     {
136         return m_error_file;
137     }
138 
139     lldb::StreamSP
140     GetAsyncOutputStream ();
141 
142     lldb::StreamSP
143     GetAsyncErrorStream ();
144 
145     CommandInterpreter &
GetCommandInterpreter()146     GetCommandInterpreter ()
147     {
148         assert (m_command_interpreter_ap.get());
149         return *m_command_interpreter_ap;
150     }
151 
152     Listener &
GetListener()153     GetListener ()
154     {
155         return m_listener;
156     }
157 
158     // This returns the Debugger's scratch source manager.  It won't be able to look up files in debug
159     // information, but it can look up files by absolute path and display them to you.
160     // To get the target's source manager, call GetSourceManager on the target instead.
161     SourceManager &
162     GetSourceManager ();
163 
164 public:
165 
166     lldb::TargetSP
GetSelectedTarget()167     GetSelectedTarget ()
168     {
169         return m_target_list.GetSelectedTarget ();
170     }
171 
172     ExecutionContext
173     GetSelectedExecutionContext();
174     //------------------------------------------------------------------
175     /// Get accessor for the target list.
176     ///
177     /// The target list is part of the global debugger object. This
178     /// the single debugger shared instance to control where targets
179     /// get created and to allow for tracking and searching for targets
180     /// based on certain criteria.
181     ///
182     /// @return
183     ///     A global shared target list.
184     //------------------------------------------------------------------
185     TargetList &
GetTargetList()186     GetTargetList ()
187     {
188         return m_target_list;
189     }
190 
191     PlatformList &
GetPlatformList()192     GetPlatformList ()
193     {
194         return m_platform_list;
195     }
196 
197     void
198     DispatchInputInterrupt ();
199 
200     void
201     DispatchInputEndOfFile ();
202 
203     void
204     DispatchInput (const char *bytes, size_t bytes_len);
205 
206     void
207     WriteToDefaultReader (const char *bytes, size_t bytes_len);
208 
209     void
210     PushInputReader (const lldb::InputReaderSP& reader_sp);
211 
212     bool
213     PopInputReader (const lldb::InputReaderSP& reader_sp);
214 
215     void
216     NotifyTopInputReader (lldb::InputReaderAction notification);
217 
218     bool
219     InputReaderIsTopReader (const lldb::InputReaderSP& reader_sp);
220 
221     static lldb::DebuggerSP
222     FindDebuggerWithID (lldb::user_id_t id);
223 
224     static lldb::DebuggerSP
225     FindDebuggerWithInstanceName (const ConstString &instance_name);
226 
227     static size_t
228     GetNumDebuggers();
229 
230     static lldb::DebuggerSP
231     GetDebuggerAtIndex (size_t index);
232 
233     static bool
234     FormatPrompt (const char *format,
235                   const SymbolContext *sc,
236                   const ExecutionContext *exe_ctx,
237                   const Address *addr,
238                   Stream &s,
239                   ValueObject* valobj = NULL);
240 
241 
242     void
243     CleanUpInputReaders ();
244 
245     static int
246     TestDebuggerRefCount ();
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 char *
281     GetFrameFormat() const;
282 
283     const char *
284     GetThreadFormat() const;
285 
286     lldb::ScriptLanguage
287     GetScriptLanguage() const;
288 
289     bool
290     SetScriptLanguage (lldb::ScriptLanguage script_lang);
291 
292     uint32_t
293     GetTerminalWidth () const;
294 
295     bool
296     SetTerminalWidth (uint32_t term_width);
297 
298     const char *
299     GetPrompt() const;
300 
301     void
302     SetPrompt(const char *p);
303 
304     bool
305     GetUseExternalEditor () const;
306 
307     bool
308     SetUseExternalEditor (bool use_external_editor_p);
309 
310     bool
311     GetUseColor () const;
312 
313     bool
314     SetUseColor (bool use_color);
315 
316     uint32_t
317     GetStopSourceLineCount (bool before) const;
318 
319     StopDisassemblyType
320     GetStopDisassemblyDisplay () const;
321 
322     uint32_t
323     GetDisassemblyLineCount () const;
324 
325     bool
326     GetNotifyVoid () const;
327 
328 
329     const ConstString &
GetInstanceName()330     GetInstanceName()
331     {
332         return m_instance_name;
333     }
334 
335     typedef bool (*LLDBCommandPluginInit) (lldb::SBDebugger& debugger);
336 
337     bool
338     LoadPlugin (const FileSpec& spec, Error& error);
339 
340 protected:
341 
342     static void
343     DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len);
344 
345     lldb::InputReaderSP
346     GetCurrentInputReader ();
347 
348     void
349     ActivateInputReader (const lldb::InputReaderSP &reader_sp);
350 
351     bool
352     CheckIfTopInputReaderIsDone ();
353 
354     SourceManager::SourceFileCache &
GetSourceFileCache()355     GetSourceFileCache ()
356     {
357         return m_source_file_cache;
358     }
359     Communication m_input_comm;
360     StreamFile m_input_file;
361     StreamFile m_output_file;
362     StreamFile m_error_file;
363     TerminalState m_terminal_state;
364     TargetList m_target_list;
365     PlatformList m_platform_list;
366     Listener m_listener;
367     std::unique_ptr<SourceManager> m_source_manager_ap;    // This is a scratch source manager that we return if we have no targets.
368     SourceManager::SourceFileCache m_source_file_cache; // All the source managers for targets created in this debugger used this shared
369                                                         // source file cache.
370     std::unique_ptr<CommandInterpreter> m_command_interpreter_ap;
371 
372     InputReaderStack m_input_reader_stack;
373     std::string m_input_reader_data;
374     typedef std::map<std::string, lldb::StreamWP> LogStreamMap;
375     LogStreamMap m_log_streams;
376     lldb::StreamSP m_log_callback_stream_sp;
377     ConstString m_instance_name;
378     typedef std::vector<lldb::DynamicLibrarySP> LoadedPluginsList;
379     LoadedPluginsList m_loaded_plugins;
380 
381     void
382     InstanceInitialize ();
383 
384 private:
385 
386     // Use Debugger::CreateInstance() to get a shared pointer to a new
387     // debugger object
388     Debugger (lldb::LogOutputCallback m_log_callback, void *baton);
389 
390     DISALLOW_COPY_AND_ASSIGN (Debugger);
391 
392 };
393 
394 } // namespace lldb_private
395 
396 #endif  // #if defined(__cplusplus)
397 #endif  // liblldb_Debugger_h_
398