2424#include < optional>
2525#include < string>
2626#include < unordered_map>
27+ #include < type_traits>
28+ #include < utility>
29+ #include < functional>
30+ #include < stack>
2731
2832namespace lldb_private {
2933namespace telemetry {
3034
3135struct LLDBEntryKind : public ::llvm::telemetry::EntryKind {
3236 static const llvm::telemetry::KindType BaseInfo = 0b11000 ;
33- static const KindType TargetInfo = 0b11010 ;
37+ static const llvm::telemetry:: KindType TargetInfo = 0b11010 ;
3438 // There are other entries in between (added in separate PRs)
3539 static const llvm::telemetry::KindType MiscInfo = 0b11110 ;
3640};
3741
42+
3843// / Defines a convenient type for timestamp of various events.
3944using SteadyTimePoint = std::chrono::time_point<std::chrono::steady_clock,
4045 std::chrono::nanoseconds>;
@@ -66,64 +71,26 @@ struct ExitDescription {
6671 std::string description;
6772};
6873
69- struct TargetTelemetryInfo : public LldbBaseTelemetryInfo {
74+
75+ struct TargetInfo : public LLDBBaseTelemetryInfo {
7076 lldb::ModuleSP exec_mod;
7177 Target *target_ptr;
7278
7379 // The same as the executable-module's UUID.
7480 std::string target_uuid;
75- std::string file_format;
76-
77- std::string binary_path;
78- size_t binary_size;
81+ std::string executable_path;
82+ size_t executable_size;
83+ std::string arch_name;
7984
8085 std::optional<ExitDescription> exit_desc;
81- TargetTelemetryInfo () = default ;
82-
83- TargetTelemetryInfo (const TargetTelemetryInfo &other) {
84- exec_mod = other.exec_mod ;
85- target_uuid = other.target_uuid ;
86- file_format = other.file_format ;
87- binary_path = other.binary_path ;
88- binary_size = other.binary_size ;
89- exit_desc = other.exit_desc ;
90- }
86+ TargetInfo () = default ;
9187
92- KindType getKind () const override { return LldbEntryKind ::TargetInfo; }
88+ llvm::telemetry:: KindType getKind () const override { return LLDBEntryKind ::TargetInfo; }
9389
9490 static bool classof (const TelemetryInfo *T) {
9591 if (T == nullptr )
9692 return false ;
97- return T->getKind () == LldbEntryKind::TargetInfo;
98- }
99-
100- void serialize (Serializer &serializer) const override ;
101- };
102-
103- // / The "catch-all" entry to store a set of non-standard data, such as
104- // / error-messages, etc.
105- struct MiscTelemetryInfo : public LLDBBaseTelemetryInfo {
106- // / If the event is/can be associated with a target entry,
107- // / this field contains that target's UUID.
108- // / <EMPTY> otherwise.
109- std::string target_uuid;
110-
111- // / Set of key-value pairs for any optional (or impl-specific) data
112- std::map<std::string, std::string> meta_data;
113-
114- MiscTelemetryInfo () = default ;
115-
116- MiscTelemetryInfo (const MiscTelemetryInfo &other) {
117- target_uuid = other.target_uuid ;
118- meta_data = other.meta_data ;
119- }
120-
121- llvm::telemetry::KindType getKind () const override {
122- return LLDBEntryKind::MiscInfo;
123- }
124-
125- static bool classof (const llvm::telemetry::TelemetryInfo *T) {
126- return T->getKind () == LLDBEntryKind::MiscInfo;
93+ return T->getKind () == LLDBEntryKind::TargetInfo;
12794 }
12895
12996 void serialize (llvm::telemetry::Serializer &serializer) const override ;
@@ -132,32 +99,68 @@ struct MiscTelemetryInfo : public LLDBBaseTelemetryInfo {
13299// / The base Telemetry manager instance in LLDB.
133100// / This class declares additional instrumentation points
134101// / applicable to LLDB.
135- class TelemetryMager : public llvm ::telemetry::Manager {
102+ class TelemetryManager : public llvm ::telemetry::Manager {
136103public:
137104 llvm::Error preDispatch (llvm::telemetry::TelemetryInfo *entry) override ;
138105
139- const llvm::telemetry::Config *getConfig ();
106+ const llvm::telemetry::Config *GetConfig ();
140107
108+ // / The following methods are for reporting the load of an executable.
109+ // / One is invoked at the beginning of the process and the other at
110+ // / the end.
111+ // / This is done in two passes to avoid losing date in case of any error/crash
112+ // / during the action.
113+ // /
114+ // / Invoked at the begining of the load of the main-executable.
141115 virtual void AtMainExecutableLoadStart (TargetInfo * entry);
116+ // / Invoked at the end of the load.
142117 virtual void AtMainExecutableLoadEnd (TargetInfo *entry);
143118
144- virtual llvm::StringRef GetInstanceName () const = 0;
145- static TelemetryManager *getInstance ();
119+ virtual llvm::StringRef GetInstanceName () const = 0;
120+
121+ static TelemetryManager *GetInstance ();
146122
147123protected:
148124 TelemetryManager (std::unique_ptr<llvm::telemetry::Config> config);
149125
150- static void setInstance (std::unique_ptr<TelemetryManager> manger);
126+ static void SetInstance (std::unique_ptr<TelemetryManager> manger);
151127
152128private:
153129 std::unique_ptr<llvm::telemetry::Config> m_config;
154- // Each debugger is assigned a unique ID (session_id).
155- // All TelemetryInfo entries emitted for the same debugger instance
156- // will get the same session_id.
157- llvm::DenseMap<Debugger *, std::string> session_ids;
130+
158131 static std::unique_ptr<TelemetryManager> g_instance;
159132};
160133
134+ class Helper {
135+ public:
136+ Helper () : m_start(std::chrono::steady_clock::now()) {}
137+ ~Helper () {
138+ while (! m_exit_funcs.empty ()) {
139+ (m_exit_funcs.top ())();
140+ m_exit_funcs.pop ();
141+ }
142+ }
143+
144+ bool TelemetryEnabled () {
145+ TelemetryManager* instance = TelemetryManager::GetInstance ();
146+ return instance != nullptr && instance->GetConfig ()->EnableTelemetry ;
147+ }
148+
149+
150+ SteadyTimePoint GetStartTime () {return m_start;}
151+ SteadyTimePoint GetCurrentTime () { return std::chrono::steady_clock::now (); }
152+
153+ template <typename Fp>
154+ void RunAtScopeExit (Fp&& F){
155+ m_exit_funcs.push (std::forward<Fp>(F));
156+ }
157+
158+ private:
159+ const SteadyTimePoint m_start;
160+ std::stack<std::function<void ()>> m_exit_funcs;
161+
162+ };
163+
161164} // namespace telemetry
162165} // namespace lldb_private
163166#endif // LLDB_CORE_TELEMETRY_H
0 commit comments