Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Core/Debugger.h
Line
Count
Source (jump to first uncovered line)
1
//===-- Debugger.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_CORE_DEBUGGER_H
10
#define LLDB_CORE_DEBUGGER_H
11
12
#include <cstdint>
13
14
#include <memory>
15
#include <vector>
16
17
#include "lldb/Core/FormatEntity.h"
18
#include "lldb/Core/IOHandler.h"
19
#include "lldb/Core/SourceManager.h"
20
#include "lldb/Core/StreamFile.h"
21
#include "lldb/Core/UserSettingsController.h"
22
#include "lldb/Host/HostThread.h"
23
#include "lldb/Host/Terminal.h"
24
#include "lldb/Target/ExecutionContext.h"
25
#include "lldb/Target/Platform.h"
26
#include "lldb/Target/TargetList.h"
27
#include "lldb/Utility/Broadcaster.h"
28
#include "lldb/Utility/ConstString.h"
29
#include "lldb/Utility/FileSpec.h"
30
#include "lldb/Utility/Status.h"
31
#include "lldb/Utility/UserID.h"
32
#include "lldb/lldb-defines.h"
33
#include "lldb/lldb-enumerations.h"
34
#include "lldb/lldb-forward.h"
35
#include "lldb/lldb-private-enumerations.h"
36
#include "lldb/lldb-private-types.h"
37
#include "lldb/lldb-types.h"
38
39
#include "llvm/ADT/ArrayRef.h"
40
#include "llvm/ADT/StringMap.h"
41
#include "llvm/ADT/StringRef.h"
42
#include "llvm/Support/DynamicLibrary.h"
43
#include "llvm/Support/Threading.h"
44
45
#include <cassert>
46
#include <cstddef>
47
#include <cstdio>
48
49
namespace llvm {
50
class raw_ostream;
51
}
52
53
namespace lldb_private {
54
class Address;
55
class CommandInterpreter;
56
class Process;
57
class Stream;
58
class SymbolContext;
59
class Target;
60
61
namespace repro {
62
class DataRecorder;
63
}
64
65
/// \class Debugger Debugger.h "lldb/Core/Debugger.h"
66
/// A class to manage flag bits.
67
///
68
/// Provides a global root objects for the debugger core.
69
70
class Debugger : public std::enable_shared_from_this<Debugger>,
71
                 public UserID,
72
                 public Properties {
73
  friend class SourceManager; // For GetSourceFileCache.
74
75
public:
76
  /// Broadcaster event bits definitions.
77
  enum {
78
    eBroadcastBitProgress = (1 << 0),
79
  };
80
81
  static ConstString GetStaticBroadcasterClass();
82
83
  /// Get the public broadcaster for this debugger.
84
349k
  Broadcaster &GetBroadcaster() { return m_broadcaster; }
85
0
  const Broadcaster &GetBroadcaster() const { return m_broadcaster; }
86
87
  class ProgressEventData : public EventData {
88
89
  public:
90
    ProgressEventData(uint64_t progress_id, const std::string &message,
91
                      uint64_t completed, uint64_t total,
92
                      bool debugger_specific)
93
        : m_message(message), m_id(progress_id), m_completed(completed),
94
0
          m_total(total), m_debugger_specific(debugger_specific) {}
95
96
    static ConstString GetFlavorString();
97
98
    ConstString GetFlavor() const override;
99
100
    void Dump(Stream *s) const override;
101
102
    static const ProgressEventData *
103
    GetEventDataFromEvent(const Event *event_ptr);
104
0
    uint64_t GetID() const { return m_id; }
105
0
    uint64_t GetCompleted() const { return m_completed; }
106
0
    uint64_t GetTotal() const { return m_total; }
107
0
    const std::string &GetMessage() const { return m_message; }
108
0
    bool IsDebuggerSpecific() const { return m_debugger_specific; }
109
110
  private:
111
    std::string m_message;
112
    const uint64_t m_id;
113
    uint64_t m_completed;
114
    const uint64_t m_total;
115
    const bool m_debugger_specific;
116
    ProgressEventData(const ProgressEventData &) = delete;
117
    const ProgressEventData &operator=(const ProgressEventData &) = delete;
118
  };
119
120
  ~Debugger() override;
121
122
  static lldb::DebuggerSP
123
  CreateInstance(lldb::LogOutputCallback log_callback = nullptr,
124
                 void *baton = nullptr);
125
126
  static lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid);
127
128
  static lldb::TargetSP FindTargetWithProcess(Process *process);
129
130
  static void Initialize(LoadPluginCallbackType load_plugin_callback);
131
132
  static void Terminate();
133
134
  static void SettingsInitialize();
135
136
  static void SettingsTerminate();
137
138
  static void Destroy(lldb::DebuggerSP &debugger_sp);
139
140
  static lldb::DebuggerSP FindDebuggerWithID(lldb::user_id_t id);
141
142
  static lldb::DebuggerSP
143
  FindDebuggerWithInstanceName(ConstString instance_name);
144
145
  static size_t GetNumDebuggers();
146
147
  static lldb::DebuggerSP GetDebuggerAtIndex(size_t index);
148
149
  static bool FormatDisassemblerAddress(const FormatEntity::Entry *format,
150
                                        const SymbolContext *sc,
151
                                        const SymbolContext *prev_sc,
152
                                        const ExecutionContext *exe_ctx,
153
                                        const Address *addr, Stream &s);
154
155
  void Clear();
156
157
  bool GetAsyncExecution();
158
159
  void SetAsyncExecution(bool async);
160
161
6.84k
  lldb::FileSP GetInputFileSP() { return m_input_file_sp; }
162
163
6.77k
  lldb::StreamFileSP GetOutputStreamSP() { return m_output_stream_sp; }
164
165
6.77k
  lldb::StreamFileSP GetErrorStreamSP() { return m_error_stream_sp; }
166
167
6.76k
  File &GetInputFile() { return *m_input_file_sp; }
168
169
6.45k
  File &GetOutputFile() { return m_output_stream_sp->GetFile(); }
170
171
0
  File &GetErrorFile() { return m_error_stream_sp->GetFile(); }
172
173
906
  StreamFile &GetOutputStream() { return *m_output_stream_sp; }
174
175
723
  StreamFile &GetErrorStream() { return *m_error_stream_sp; }
176
177
  repro::DataRecorder *GetInputRecorder();
178
179
  Status SetInputString(const char *data);
180
181
  // This method will setup data recorder if reproducer enabled.
182
  // On reply mode this method should take instructions from reproducer file.
183
  Status SetInputFile(lldb::FileSP file);
184
185
  void SetInputFile(lldb::FileSP file, repro::DataRecorder *recorder);
186
187
  void SetOutputFile(lldb::FileSP file);
188
189
  void SetErrorFile(lldb::FileSP file);
190
191
  void SaveInputTerminalState();
192
193
  void RestoreInputTerminalState();
194
195
  lldb::StreamSP GetAsyncOutputStream();
196
197
  lldb::StreamSP GetAsyncErrorStream();
198
199
56.6k
  CommandInterpreter &GetCommandInterpreter() {
200
56.6k
    assert(m_command_interpreter_up.get());
201
0
    return *m_command_interpreter_up;
202
56.6k
  }
203
204
  ScriptInterpreter *
205
  GetScriptInterpreter(bool can_create = true,
206
                       llvm::Optional<lldb::ScriptLanguage> language = {});
207
208
3.43k
  lldb::ListenerSP GetListener() { return m_listener_sp; }
209
210
  // This returns the Debugger's scratch source manager.  It won't be able to
211
  // look up files in debug information, but it can look up files by absolute
212
  // path and display them to you. To get the target's source manager, call
213
  // GetSourceManager on the target instead.
214
  SourceManager &GetSourceManager();
215
216
434k
  lldb::TargetSP GetSelectedTarget() {
217
434k
    return m_target_list.GetSelectedTarget();
218
434k
  }
219
220
  ExecutionContext GetSelectedExecutionContext();
221
  /// Get accessor for the target list.
222
  ///
223
  /// The target list is part of the global debugger object. This the single
224
  /// debugger shared instance to control where targets get created and to
225
  /// allow for tracking and searching for targets based on certain criteria.
226
  ///
227
  /// \return
228
  ///     A global shared target list.
229
27.3k
  TargetList &GetTargetList() { return m_target_list; }
230
231
43.4k
  PlatformList &GetPlatformList() { return m_platform_list; }
232
233
  void DispatchInputInterrupt();
234
235
  void DispatchInputEndOfFile();
236
237
  // If any of the streams are not set, set them to the in/out/err stream of
238
  // the top most input reader to ensure they at least have something
239
  void AdoptTopIOHandlerFilesIfInvalid(lldb::FileSP &in,
240
                                       lldb::StreamFileSP &out,
241
                                       lldb::StreamFileSP &err);
242
243
  /// Run the given IO handler and return immediately.
244
  void RunIOHandlerAsync(const lldb::IOHandlerSP &reader_sp,
245
                         bool cancel_top_handler = true);
246
247
  /// Run the given IO handler and block until it's complete.
248
  void RunIOHandlerSync(const lldb::IOHandlerSP &reader_sp);
249
250
  ///  Remove the given IO handler if it's currently active.
251
  bool RemoveIOHandler(const lldb::IOHandlerSP &reader_sp);
252
253
  bool IsTopIOHandler(const lldb::IOHandlerSP &reader_sp);
254
255
  bool CheckTopIOHandlerTypes(IOHandler::Type top_type,
256
                              IOHandler::Type second_top_type);
257
258
  void PrintAsync(const char *s, size_t len, bool is_stdout);
259
260
  ConstString GetTopIOHandlerControlSequence(char ch);
261
262
  const char *GetIOHandlerCommandPrefix();
263
264
  const char *GetIOHandlerHelpPrologue();
265
266
  void ClearIOHandlers();
267
268
  bool GetCloseInputOnEOF() const;
269
270
  void SetCloseInputOnEOF(bool b);
271
272
  bool EnableLog(llvm::StringRef channel,
273
                 llvm::ArrayRef<const char *> categories,
274
                 llvm::StringRef log_file, uint32_t log_options,
275
                 llvm::raw_ostream &error_stream);
276
277
  void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);
278
279
  // Properties Functions
280
  enum StopDisassemblyType {
281
    eStopDisassemblyTypeNever = 0,
282
    eStopDisassemblyTypeNoDebugInfo,
283
    eStopDisassemblyTypeNoSource,
284
    eStopDisassemblyTypeAlways
285
  };
286
287
  Status SetPropertyValue(const ExecutionContext *exe_ctx,
288
                          VarSetOperationType op, llvm::StringRef property_path,
289
                          llvm::StringRef value) override;
290
291
  bool GetAutoConfirm() const;
292
293
  const FormatEntity::Entry *GetDisassemblyFormat() const;
294
295
  const FormatEntity::Entry *GetFrameFormat() const;
296
297
  const FormatEntity::Entry *GetFrameFormatUnique() const;
298
299
  uint32_t GetStopDisassemblyMaxSize() const;
300
301
  const FormatEntity::Entry *GetThreadFormat() const;
302
303
  const FormatEntity::Entry *GetThreadStopFormat() const;
304
305
  lldb::ScriptLanguage GetScriptLanguage() const;
306
307
  bool SetScriptLanguage(lldb::ScriptLanguage script_lang);
308
309
  lldb::LanguageType GetREPLLanguage() const;
310
311
  bool SetREPLLanguage(lldb::LanguageType repl_lang);
312
313
  uint32_t GetTerminalWidth() const;
314
315
  bool SetTerminalWidth(uint32_t term_width);
316
317
  llvm::StringRef GetPrompt() const;
318
319
  void SetPrompt(llvm::StringRef p);
320
  void SetPrompt(const char *) = delete;
321
322
  llvm::StringRef GetReproducerPath() const;
323
324
  bool GetUseExternalEditor() const;
325
326
  bool SetUseExternalEditor(bool use_external_editor_p);
327
328
  bool GetUseColor() const;
329
330
  bool SetUseColor(bool use_color);
331
332
  bool GetUseAutosuggestion() const;
333
334
  bool GetUseSourceCache() const;
335
336
  bool SetUseSourceCache(bool use_source_cache);
337
338
  bool GetHighlightSource() const;
339
340
  lldb::StopShowColumn GetStopShowColumn() const;
341
342
  llvm::StringRef GetStopShowColumnAnsiPrefix() const;
343
344
  llvm::StringRef GetStopShowColumnAnsiSuffix() const;
345
346
  uint32_t GetStopSourceLineCount(bool before) const;
347
348
  StopDisassemblyType GetStopDisassemblyDisplay() const;
349
350
  uint32_t GetDisassemblyLineCount() const;
351
352
  llvm::StringRef GetStopShowLineMarkerAnsiPrefix() const;
353
354
  llvm::StringRef GetStopShowLineMarkerAnsiSuffix() const;
355
356
  bool GetAutoOneLineSummaries() const;
357
358
  bool GetAutoIndent() const;
359
360
  bool SetAutoIndent(bool b);
361
362
  bool GetPrintDecls() const;
363
364
  bool SetPrintDecls(bool b);
365
366
  uint32_t GetTabSize() const;
367
368
  bool SetTabSize(uint32_t tab_size);
369
370
  bool GetEscapeNonPrintables() const;
371
372
  bool GetNotifyVoid() const;
373
374
1.44k
  ConstString GetInstanceName() { return m_instance_name; }
375
376
  bool LoadPlugin(const FileSpec &spec, Status &error);
377
378
  void RunIOHandlers();
379
380
  bool IsForwardingEvents();
381
382
  void EnableForwardEvents(const lldb::ListenerSP &listener_sp);
383
384
  void CancelForwardEvents(const lldb::ListenerSP &listener_sp);
385
386
2.11k
  bool IsHandlingEvents() const { return m_event_handler_thread.IsJoinable(); }
387
388
  Status RunREPL(lldb::LanguageType language, const char *repl_options);
389
390
  // This is for use in the command interpreter, when you either want the
391
  // selected target, or if no target is present you want to prime the dummy
392
  // target with entities that will be copied over to new targets.
393
  Target &GetSelectedOrDummyTarget(bool prefer_dummy = false);
394
4.59k
  Target &GetDummyTarget() { return *m_dummy_target_sp; }
395
396
27.3k
  lldb::BroadcasterManagerSP GetBroadcasterManager() {
397
27.3k
    return m_broadcaster_manager_sp;
398
27.3k
  }
399
400
protected:
401
  friend class CommandInterpreter;
402
  friend class REPL;
403
  friend class Progress;
404
405
  /// Report progress events.
406
  ///
407
  /// Progress events will be delivered to any debuggers that have listeners
408
  /// for the eBroadcastBitProgress. This function is called by the
409
  /// lldb_private::Progress class to deliver the events to any debuggers that
410
  /// qualify.
411
  ///
412
  /// \param [in] progress_id
413
  ///   The unique integer identifier for the progress to report.
414
  ///
415
  /// \param[in] message
416
  ///   The title of the progress dialog to display in the UI.
417
  ///
418
  /// \param [in] completed
419
  ///   The amount of work completed. If \a completed is zero, then this event
420
  ///   is a progress started event. If \a completed is equal to \a total, then
421
  ///   this event is a progress end event. Otherwise completed indicates the
422
  ///   current progress compare to the total value.
423
  ///
424
  /// \param [in] total
425
  ///   The total amount of work units that need to be completed. If this value
426
  ///   is UINT64_MAX, then an indeterminate progress indicator should be
427
  ///   displayed.
428
  ///
429
  /// \param [in] debugger_id
430
  ///   If this optional parameter has a value, it indicates the unique
431
  ///   debugger identifier that this progress should be delivered to. If this
432
  ///   optional parameter does not have a value, the progress will be
433
  ///   delivered to all debuggers.
434
  static void ReportProgress(uint64_t progress_id, const std::string &message,
435
                             uint64_t completed, uint64_t total,
436
                             llvm::Optional<lldb::user_id_t> debugger_id);
437
438
  bool StartEventHandlerThread();
439
440
  void StopEventHandlerThread();
441
442
  static lldb::thread_result_t EventHandlerThread(lldb::thread_arg_t arg);
443
444
  void PushIOHandler(const lldb::IOHandlerSP &reader_sp,
445
                     bool cancel_top_handler = true);
446
447
  bool PopIOHandler(const lldb::IOHandlerSP &reader_sp);
448
449
  bool HasIOHandlerThread();
450
451
  bool StartIOHandlerThread();
452
453
  void StopIOHandlerThread();
454
455
  void JoinIOHandlerThread();
456
457
  static lldb::thread_result_t IOHandlerThread(lldb::thread_arg_t arg);
458
459
  void DefaultEventHandler();
460
461
  void HandleBreakpointEvent(const lldb::EventSP &event_sp);
462
463
  void HandleProcessEvent(const lldb::EventSP &event_sp);
464
465
  void HandleThreadEvent(const lldb::EventSP &event_sp);
466
467
  // Ensures two threads don't attempt to flush process output in parallel.
468
  std::mutex m_output_flush_mutex;
469
  void FlushProcessOutput(Process &process, bool flush_stdout,
470
                          bool flush_stderr);
471
472
20.2k
  SourceManager::SourceFileCache &GetSourceFileCache() {
473
20.2k
    return m_source_file_cache;
474
20.2k
  }
475
476
  void InstanceInitialize();
477
478
  // these should never be NULL
479
  lldb::FileSP m_input_file_sp;
480
  lldb::StreamFileSP m_output_stream_sp;
481
  lldb::StreamFileSP m_error_stream_sp;
482
483
  /// Used for shadowing the input file when capturing a reproducer.
484
  repro::DataRecorder *m_input_recorder;
485
486
  lldb::BroadcasterManagerSP m_broadcaster_manager_sp; // The debugger acts as a
487
                                                       // broadcaster manager of
488
                                                       // last resort.
489
  // It needs to get constructed before the target_list or any other member
490
  // that might want to broadcast through the debugger.
491
492
  TerminalState m_terminal_state;
493
  TargetList m_target_list;
494
495
  PlatformList m_platform_list;
496
  lldb::ListenerSP m_listener_sp;
497
  std::unique_ptr<SourceManager> m_source_manager_up; // This is a scratch
498
                                                      // source manager that we
499
                                                      // return if we have no
500
                                                      // targets.
501
  SourceManager::SourceFileCache m_source_file_cache; // All the source managers
502
                                                      // for targets created in
503
                                                      // this debugger used this
504
                                                      // shared
505
                                                      // source file cache.
506
  std::unique_ptr<CommandInterpreter> m_command_interpreter_up;
507
508
  std::recursive_mutex m_script_interpreter_mutex;
509
  std::array<lldb::ScriptInterpreterSP, lldb::eScriptLanguageUnknown>
510
      m_script_interpreters;
511
512
  IOHandlerStack m_io_handler_stack;
513
  std::recursive_mutex m_io_handler_synchronous_mutex;
514
515
  llvm::StringMap<std::weak_ptr<llvm::raw_ostream>> m_log_streams;
516
  std::shared_ptr<llvm::raw_ostream> m_log_callback_stream_sp;
517
  ConstString m_instance_name;
518
  static LoadPluginCallbackType g_load_plugin_callback;
519
  typedef std::vector<llvm::sys::DynamicLibrary> LoadedPluginsList;
520
  LoadedPluginsList m_loaded_plugins;
521
  HostThread m_event_handler_thread;
522
  HostThread m_io_handler_thread;
523
  Broadcaster m_sync_broadcaster; ///< Private debugger synchronization.
524
  Broadcaster m_broadcaster;      ///< Public Debugger event broadcaster.
525
  lldb::ListenerSP m_forward_listener_sp;
526
  llvm::once_flag m_clear_once;
527
  lldb::TargetSP m_dummy_target_sp;
528
529
  // Events for m_sync_broadcaster
530
  enum {
531
    eBroadcastBitEventThreadIsListening = (1 << 0),
532
  };
533
534
private:
535
  // Use Debugger::CreateInstance() to get a shared pointer to a new debugger
536
  // object
537
  Debugger(lldb::LogOutputCallback m_log_callback, void *baton);
538
539
  Debugger(const Debugger &) = delete;
540
  const Debugger &operator=(const Debugger &) = delete;
541
};
542
543
} // namespace lldb_private
544
545
#endif // LLDB_CORE_DEBUGGER_H