1 //===-- SBDebugger.h --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_API_SBDEBUGGER_H
10 #define LLDB_API_SBDEBUGGER_H
11 
12 #include <cstdio>
13 
14 #include "lldb/API/SBDefines.h"
15 #include "lldb/API/SBPlatform.h"
16 
17 namespace lldb {
18 
19 class LLDB_API SBInputReader {
20 public:
21   SBInputReader() = default;
22   ~SBInputReader() = default;
23 
24   SBError Initialize(lldb::SBDebugger &sb_debugger,
25                      unsigned long (*callback)(void *, lldb::SBInputReader *,
26                                                lldb::InputReaderAction,
27                                                char const *, unsigned long),
28                      void *a, lldb::InputReaderGranularity b, char const *c,
29                      char const *d, bool e);
30   void SetIsDone(bool);
31   bool IsActive() const;
32 };
33 
34 class LLDB_API SBDebugger {
35 public:
36   FLAGS_ANONYMOUS_ENUM(){
37       eBroadcastBitProgress = (1 << 0),
38       eBroadcastBitWarning = (1 << 1),
39       eBroadcastBitError = (1 << 2),
40   };
41 
42   SBDebugger();
43 
44   SBDebugger(const lldb::SBDebugger &rhs);
45 
46   SBDebugger(const lldb::DebuggerSP &debugger_sp);
47 
48   ~SBDebugger();
49 
50   static const char *GetBroadcasterClass();
51 
52   lldb::SBBroadcaster GetBroadcaster();
53 
54   /// Get progress data from a SBEvent whose type is eBroadcastBitProgress.
55   ///
56   /// \param [in] event
57   ///   The event to extract the progress information from.
58   ///
59   /// \param [out] progress_id
60   ///   The unique integer identifier for the progress to report.
61   ///
62   /// \param [out] completed
63   ///   The amount of work completed. If \a completed is zero, then this event
64   ///   is a progress started event. If \a completed is equal to \a total, then
65   ///   this event is a progress end event. Otherwise completed indicates the
66   ///   current progress update.
67   ///
68   /// \param [out] total
69   ///   The total amount of work units that need to be completed. If this value
70   ///   is UINT64_MAX, then an indeterminate progress indicator should be
71   ///   displayed.
72   ///
73   /// \param [out] is_debugger_specific
74   ///   Set to true if this progress is specific to this debugger only. Many
75   ///   progress events are not specific to a debugger instance, like any
76   ///   progress events for loading information in modules since LLDB has a
77   ///   global module cache that all debuggers use.
78   ///
79   /// \return The message for the progress. If the returned value is NULL, then
80   ///   \a event was not a eBroadcastBitProgress event.
81   static const char *GetProgressFromEvent(const lldb::SBEvent &event,
82                                           uint64_t &progress_id,
83                                           uint64_t &completed, uint64_t &total,
84                                           bool &is_debugger_specific);
85 
86   static lldb::SBStructuredData
87   GetDiagnosticFromEvent(const lldb::SBEvent &event);
88 
89   lldb::SBDebugger &operator=(const lldb::SBDebugger &rhs);
90 
91   static void Initialize();
92 
93   static lldb::SBError InitializeWithErrorHandling();
94 
95   static void Terminate();
96 
97   // Deprecated, use the one that takes a source_init_files bool.
98   static lldb::SBDebugger Create();
99 
100   static lldb::SBDebugger Create(bool source_init_files);
101 
102   static lldb::SBDebugger Create(bool source_init_files,
103                                  lldb::LogOutputCallback log_callback,
104                                  void *baton);
105 
106   static void Destroy(lldb::SBDebugger &debugger);
107 
108   static void MemoryPressureDetected();
109 
110   explicit operator bool() const;
111 
112   bool IsValid() const;
113 
114   void Clear();
115 
116   void SetAsync(bool b);
117 
118   bool GetAsync();
119 
120   void SkipLLDBInitFiles(bool b);
121 
122   void SkipAppInitFiles(bool b);
123 
124   void SetInputFileHandle(FILE *f, bool transfer_ownership);
125 
126   void SetOutputFileHandle(FILE *f, bool transfer_ownership);
127 
128   void SetErrorFileHandle(FILE *f, bool transfer_ownership);
129 
130   FILE *GetInputFileHandle();
131 
132   FILE *GetOutputFileHandle();
133 
134   FILE *GetErrorFileHandle();
135 
136   SBError SetInputString(const char *data);
137 
138   SBError SetInputFile(SBFile file);
139 
140   SBError SetOutputFile(SBFile file);
141 
142   SBError SetErrorFile(SBFile file);
143 
144   SBError SetInputFile(FileSP file);
145 
146   SBError SetOutputFile(FileSP file);
147 
148   SBError SetErrorFile(FileSP file);
149 
150   SBFile GetInputFile();
151 
152   SBFile GetOutputFile();
153 
154   SBFile GetErrorFile();
155 
156   void SaveInputTerminalState();
157 
158   void RestoreInputTerminalState();
159 
160   lldb::SBCommandInterpreter GetCommandInterpreter();
161 
162   void HandleCommand(const char *command);
163 
164   lldb::SBListener GetListener();
165 
166   void HandleProcessEvent(const lldb::SBProcess &process,
167                           const lldb::SBEvent &event, FILE *out,
168                           FILE *err); // DEPRECATED
169 
170   void HandleProcessEvent(const lldb::SBProcess &process,
171                           const lldb::SBEvent &event, SBFile out, SBFile err);
172 
173   void HandleProcessEvent(const lldb::SBProcess &process,
174                           const lldb::SBEvent &event, FileSP out, FileSP err);
175 
176   lldb::SBTarget CreateTarget(const char *filename, const char *target_triple,
177                               const char *platform_name,
178                               bool add_dependent_modules, lldb::SBError &error);
179 
180   lldb::SBTarget CreateTargetWithFileAndTargetTriple(const char *filename,
181                                                      const char *target_triple);
182 
183   lldb::SBTarget CreateTargetWithFileAndArch(const char *filename,
184                                              const char *archname);
185 
186   lldb::SBTarget CreateTarget(const char *filename);
187 
188   lldb::SBTarget GetDummyTarget();
189 
190   // Return true if target is deleted from the target list of the debugger.
191   bool DeleteTarget(lldb::SBTarget &target);
192 
193   lldb::SBTarget GetTargetAtIndex(uint32_t idx);
194 
195   uint32_t GetIndexOfTarget(lldb::SBTarget target);
196 
197   lldb::SBTarget FindTargetWithProcessID(pid_t pid);
198 
199   lldb::SBTarget FindTargetWithFileAndArch(const char *filename,
200                                            const char *arch);
201 
202   uint32_t GetNumTargets();
203 
204   lldb::SBTarget GetSelectedTarget();
205 
206   void SetSelectedTarget(SBTarget &target);
207 
208   lldb::SBPlatform GetSelectedPlatform();
209 
210   void SetSelectedPlatform(lldb::SBPlatform &platform);
211 
212   /// Get the number of currently active platforms.
213   uint32_t GetNumPlatforms();
214 
215   /// Get one of the currently active platforms.
216   lldb::SBPlatform GetPlatformAtIndex(uint32_t idx);
217 
218   /// Get the number of available platforms.
219   ///
220   /// The return value should match the number of entries output by the
221   /// "platform list" command.
222   uint32_t GetNumAvailablePlatforms();
223 
224   /// Get the name and description of one of the available platforms.
225   ///
226   /// \param[in] idx
227   ///     Zero-based index of the platform for which info should be retrieved,
228   ///     must be less than the value returned by GetNumAvailablePlatforms().
229   lldb::SBStructuredData GetAvailablePlatformInfoAtIndex(uint32_t idx);
230 
231   lldb::SBSourceManager GetSourceManager();
232 
233   // REMOVE: just for a quick fix, need to expose platforms through
234   // SBPlatform from this class.
235   lldb::SBError SetCurrentPlatform(const char *platform_name);
236 
237   bool SetCurrentPlatformSDKRoot(const char *sysroot);
238 
239   // FIXME: Once we get the set show stuff in place, the driver won't need
240   // an interface to the Set/Get UseExternalEditor.
241   bool SetUseExternalEditor(bool input);
242 
243   bool GetUseExternalEditor();
244 
245   bool SetUseColor(bool use_color);
246 
247   bool GetUseColor() const;
248 
249   bool SetUseSourceCache(bool use_source_cache);
250 
251   bool GetUseSourceCache() const;
252 
253   static bool GetDefaultArchitecture(char *arch_name, size_t arch_name_len);
254 
255   static bool SetDefaultArchitecture(const char *arch_name);
256 
257   lldb::ScriptLanguage GetScriptingLanguage(const char *script_language_name);
258 
259   SBStructuredData GetScriptInterpreterInfo(ScriptLanguage);
260 
261   static const char *GetVersionString();
262 
263   static const char *StateAsCString(lldb::StateType state);
264 
265   static SBStructuredData GetBuildConfiguration();
266 
267   static bool StateIsRunningState(lldb::StateType state);
268 
269   static bool StateIsStoppedState(lldb::StateType state);
270 
271   bool EnableLog(const char *channel, const char **categories);
272 
273   void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);
274 
275   // DEPRECATED
276   void DispatchInput(void *baton, const void *data, size_t data_len);
277 
278   void DispatchInput(const void *data, size_t data_len);
279 
280   void DispatchInputInterrupt();
281 
282   void DispatchInputEndOfFile();
283 
284   void PushInputReader(lldb::SBInputReader &reader);
285 
286   const char *GetInstanceName();
287 
288   static SBDebugger FindDebuggerWithID(int id);
289 
290   static lldb::SBError SetInternalVariable(const char *var_name,
291                                            const char *value,
292                                            const char *debugger_instance_name);
293 
294   static lldb::SBStringList
295   GetInternalVariableValue(const char *var_name,
296                            const char *debugger_instance_name);
297 
298   bool GetDescription(lldb::SBStream &description);
299 
300   uint32_t GetTerminalWidth() const;
301 
302   void SetTerminalWidth(uint32_t term_width);
303 
304   lldb::user_id_t GetID();
305 
306   const char *GetPrompt() const;
307 
308   void SetPrompt(const char *prompt);
309 
310   const char *GetReproducerPath() const;
311 
312   lldb::ScriptLanguage GetScriptLanguage() const;
313 
314   void SetScriptLanguage(lldb::ScriptLanguage script_lang);
315 
316   lldb::LanguageType GetREPLLanguage() const;
317 
318   void SetREPLLanguage(lldb::LanguageType repl_lang);
319 
320   bool GetCloseInputOnEOF() const;
321 
322   void SetCloseInputOnEOF(bool b);
323 
324   SBTypeCategory GetCategory(const char *category_name);
325 
326   SBTypeCategory GetCategory(lldb::LanguageType lang_type);
327 
328   SBTypeCategory CreateCategory(const char *category_name);
329 
330   bool DeleteCategory(const char *category_name);
331 
332   uint32_t GetNumCategories();
333 
334   SBTypeCategory GetCategoryAtIndex(uint32_t);
335 
336   SBTypeCategory GetDefaultCategory();
337 
338   SBTypeFormat GetFormatForType(SBTypeNameSpecifier);
339 
340   SBTypeSummary GetSummaryForType(SBTypeNameSpecifier);
341 
342   SBTypeFilter GetFilterForType(SBTypeNameSpecifier);
343 
344   SBTypeSynthetic GetSyntheticForType(SBTypeNameSpecifier);
345 
346   /// Run the command interpreter.
347   ///
348   /// \param[in] auto_handle_events
349   ///     If true, automatically handle resulting events. This takes precedence
350   ///     and overrides the corresponding option in
351   ///     SBCommandInterpreterRunOptions.
352   ///
353   /// \param[in] spawn_thread
354   ///     If true, start a new thread for IO handling. This takes precedence
355   ///     and overrides the corresponding option in
356   ///     SBCommandInterpreterRunOptions.
357   void RunCommandInterpreter(bool auto_handle_events, bool spawn_thread);
358 
359   /// Run the command interpreter.
360   ///
361   /// \param[in] auto_handle_events
362   ///     If true, automatically handle resulting events. This takes precedence
363   ///     and overrides the corresponding option in
364   ///     SBCommandInterpreterRunOptions.
365   ///
366   /// \param[in] spawn_thread
367   ///     If true, start a new thread for IO handling. This takes precedence
368   ///     and overrides the corresponding option in
369   ///     SBCommandInterpreterRunOptions.
370   ///
371   /// \param[in] options
372   ///     Parameter collection of type SBCommandInterpreterRunOptions.
373   ///
374   /// \param[out] num_errors
375   ///     The number of errors.
376   ///
377   /// \param[out] quit_requested
378   ///     Whether a quit was requested.
379   ///
380   /// \param[out] stopped_for_crash
381   ///     Whether the interpreter stopped for a crash.
382   void RunCommandInterpreter(bool auto_handle_events, bool spawn_thread,
383                              SBCommandInterpreterRunOptions &options,
384                              int &num_errors, bool &quit_requested,
385                              bool &stopped_for_crash);
386 
387   SBCommandInterpreterRunResult
388   RunCommandInterpreter(const SBCommandInterpreterRunOptions &options);
389 
390   SBError RunREPL(lldb::LanguageType language, const char *repl_options);
391 
392 private:
393   friend class SBCommandInterpreter;
394   friend class SBInputReader;
395   friend class SBListener;
396   friend class SBProcess;
397   friend class SBSourceManager;
398   friend class SBTarget;
399 
400   lldb::SBTarget FindTargetWithLLDBProcess(const lldb::ProcessSP &processSP);
401 
402   void reset(const lldb::DebuggerSP &debugger_sp);
403 
404   lldb_private::Debugger *get() const;
405 
406   lldb_private::Debugger &ref() const;
407 
408   const lldb::DebuggerSP &get_sp() const;
409 
410   lldb::DebuggerSP m_opaque_sp;
411 
412 }; // class SBDebugger
413 
414 } // namespace lldb
415 
416 #endif // LLDB_API_SBDEBUGGER_H
417