1 //===-- StructuredDataDarwinLog.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 StructuredDataDarwinLog_h
11 #define StructuredDataDarwinLog_h
12 
13 #include "lldb/Target/StructuredDataPlugin.h"
14 
15 #include <mutex>
16 
17 // Forward declarations
18 namespace sddarwinlog_private {
19 class EnableCommand;
20 }
21 
22 namespace lldb_private {
23 
24 class StructuredDataDarwinLog : public StructuredDataPlugin {
25   friend sddarwinlog_private::EnableCommand;
26 
27 public:
28   // -------------------------------------------------------------------------
29   // Public static API
30   // -------------------------------------------------------------------------
31 
32   static void Initialize();
33 
34   static void Terminate();
35 
36   static const ConstString &GetStaticPluginName();
37 
38   // -------------------------------------------------------------------------
39   /// Return whether the DarwinLog functionality is enabled.
40   ///
41   /// The DarwinLog functionality is enabled if the user expicitly enabled
42   /// it with the enable command, or if the user has the setting set
43   /// that controls if we always enable it for newly created/attached
44   /// processes.
45   ///
46   /// @return
47   ///      True if DarwinLog support is/will be enabled for existing or
48   ///      newly launched/attached processes.
49   // -------------------------------------------------------------------------
50   static bool IsEnabled();
51 
52   // -------------------------------------------------------------------------
53   // PluginInterface API
54   // -------------------------------------------------------------------------
55 
56   ConstString GetPluginName() override;
57 
58   uint32_t GetPluginVersion() override;
59 
60   // -------------------------------------------------------------------------
61   // StructuredDataPlugin API
62   // -------------------------------------------------------------------------
63 
64   bool SupportsStructuredDataType(const ConstString &type_name) override;
65 
66   void HandleArrivalOfStructuredData(
67       Process &process, const ConstString &type_name,
68       const StructuredData::ObjectSP &object_sp) override;
69 
70   Error GetDescription(const StructuredData::ObjectSP &object_sp,
71                        lldb_private::Stream &stream) override;
72 
73   bool GetEnabled(const ConstString &type_name) const override;
74 
75   void ModulesDidLoad(Process &process, ModuleList &module_list) override;
76 
77   ~StructuredDataDarwinLog();
78 
79 private:
80   // -------------------------------------------------------------------------
81   // Private constructors
82   // -------------------------------------------------------------------------
83 
84   StructuredDataDarwinLog(const lldb::ProcessWP &process_wp);
85 
86   // -------------------------------------------------------------------------
87   // Private static methods
88   // -------------------------------------------------------------------------
89 
90   static lldb::StructuredDataPluginSP CreateInstance(Process &process);
91 
92   static void DebuggerInitialize(Debugger &debugger);
93 
94   static bool InitCompletionHookCallback(void *baton,
95                                          StoppointCallbackContext *context,
96                                          lldb::user_id_t break_id,
97                                          lldb::user_id_t break_loc_id);
98 
99   static Error FilterLaunchInfo(ProcessLaunchInfo &launch_info, Target *target);
100 
101   // -------------------------------------------------------------------------
102   // Internal helper methods used by friend classes
103   // -------------------------------------------------------------------------
104   void SetEnabled(bool enabled);
105 
106   void AddInitCompletionHook(Process &process);
107 
108   // -------------------------------------------------------------------------
109   // Private methods
110   // -------------------------------------------------------------------------
111 
112   void DumpTimestamp(Stream &stream, uint64_t timestamp);
113 
114   size_t DumpHeader(Stream &stream, const StructuredData::Dictionary &event);
115 
116   size_t HandleDisplayOfEvent(const StructuredData::Dictionary &event,
117                               Stream &stream);
118 
119   // -------------------------------------------------------------------------
120   /// Call the enable command again, using whatever settings were initially
121   /// made.
122   // -------------------------------------------------------------------------
123 
124   void EnableNow();
125 
126   // -------------------------------------------------------------------------
127   // Private data
128   // -------------------------------------------------------------------------
129   bool m_recorded_first_timestamp;
130   uint64_t m_first_timestamp_seen;
131   bool m_is_enabled;
132   std::mutex m_added_breakpoint_mutex;
133   bool m_added_breakpoint;
134   lldb::user_id_t m_breakpoint_id;
135 };
136 }
137 
138 #endif /* StructuredDataPluginDarwinLog_hpp */
139