1010
1111#ifdef LLVM_BUILD_TELEMETRY
1212
13- #include " lldb/Core/Telemetry.h"
1413#include " lldb/Core/Debugger.h"
14+ #include " lldb/Core/Telemetry.h"
15+ #include " lldb/Host/FileSystem.h"
16+ #include " lldb/Host/HostInfo.h"
17+ #include " lldb/Target/Process.h"
18+ #include " lldb/Target/Statistics.h"
1519#include " lldb/Utility/LLDBLog.h"
1620#include " lldb/Utility/UUID.h"
21+ #include " lldb/Version/Version.h"
1722#include " lldb/lldb-enumerations.h"
1823#include " lldb/lldb-forward.h"
1924#include " llvm/ADT/StringRef.h"
2025#include " llvm/Support/Error.h"
26+ #include " llvm/Support/Path.h"
2127#include " llvm/Support/RandomNumberGenerator.h"
2228#include " llvm/Telemetry/Telemetry.h"
2329#include < chrono>
@@ -35,15 +41,7 @@ static uint64_t ToNanosec(const SteadyTimePoint Point) {
3541 return std::chrono::nanoseconds (Point.time_since_epoch ()).count ();
3642}
3743
38- void LLDBBaseTelemetryInfo::serialize (Serializer &serializer) const {
39- serializer.write (" entry_kind" , getKind ());
40- serializer.write (" session_id" , SessionId);
41- serializer.write (" start_time" , ToNanosec (start_time));
42- if (end_time.has_value ())
43- serializer.write (" end_time" , ToNanosec (end_time.value ()));
44- }
45-
46- [[maybe_unused]] static std::string MakeUUID (Debugger *debugger) {
44+ static std::string MakeUUID (Debugger *debugger) {
4745 uint8_t random_bytes[16 ];
4846 if (auto ec = llvm::getRandomBytes (random_bytes, 16 )) {
4947 LLDB_LOG (GetLog (LLDBLog::Object),
@@ -56,16 +54,91 @@ void LLDBBaseTelemetryInfo::serialize(Serializer &serializer) const {
5654 return UUID (random_bytes).GetAsString ();
5755}
5856
57+ void LLDBBaseTelemetryInfo::serialize (Serializer &serializer) const {
58+ serializer.write (" entry_kind" , getKind ());
59+ serializer.write (" session_id" , SessionId);
60+ serializer.write (" start_time" , ToNanosec (start_time));
61+ if (end_time.has_value ())
62+ serializer.write (" end_time" , ToNanosec (end_time.value ()));
63+ }
64+
65+ void TargetInfo::serialize (Serializer &serializer) const {
66+ LLDBBaseTelemetryInfo::serialize (serializer);
67+
68+ serializer.write (" username" , username);
69+ serializer.write (" lldb_git_sha" , lldb_git_sha);
70+ serializer.write (" lldb_path" , lldb_path);
71+ serializer.write (" cwd" , cwd);
72+ if (exit_desc.has_value ()) {
73+ serializer.write (" exit_code" , exit_desc->exit_code );
74+ serializer.write (" exit_desc" , exit_desc->description );
75+ }
76+ }
77+
78+ void MiscTelemetryInfo::serialize (Serializer &serializer) const {
79+ LLDBBaseTelemetryInfo::serialize (serializer);
80+ serializer.write (" target_uuid" , target_uuid);
81+ serializer.beginObject (" meta_data" );
82+ for (const auto &kv : meta_data)
83+ serializer.write (kv.first , kv.second );
84+ serializer.endObject ();
85+ }
86+
5987TelemetryManager::TelemetryManager (std::unique_ptr<Config> config)
6088 : m_config(std::move(config)) {}
6189
6290llvm::Error TelemetryManager::preDispatch (TelemetryInfo *entry) {
63- // Do nothing for now.
64- // In up-coming patch, this would be where the manager
65- // attach the session_uuid to the entry.
91+ LLDBBaseTelemetryInfo *lldb_entry =
92+ llvm::dyn_cast<LLDBBaseTelemetryInfo>(entry);
93+ std::string session_id = " " ;
94+ if (Debugger *debugger = lldb_entry->debugger ) {
95+ auto session_id_pos = session_ids.find (debugger);
96+ if (session_id_pos != session_ids.end ())
97+ session_id = session_id_pos->second ;
98+ else
99+ session_id_pos->second = session_id = MakeUUID (debugger);
100+ }
101+ lldb_entry->SessionId = session_id;
102+
66103 return llvm::Error::success ();
67104}
68105
106+ const Config *getConfig () { return m_config.get (); }
107+
108+ void TelemetryManager::AtMainExecutableLoadStart (TargetInfo *entry) {
109+ UserIDResolver &resolver = lldb_private::HostInfo::GetUserIDResolver ();
110+ std::optional<llvm::StringRef> opt_username =
111+ resolver.GetUserName (lldb_private::HostInfo::GetUserID ());
112+ if (opt_username)
113+ entry->username = *opt_username;
114+
115+ entry->lldb_git_sha =
116+ lldb_private::GetVersion (); // TODO: find the real git sha?
117+
118+ entry->lldb_path = HostInfo::GetProgramFileSpec ().GetPath ();
119+
120+ llvm::SmallString<64 > cwd;
121+ if (!llvm::sys::fs::current_path (cwd)) {
122+ entry->cwd = cwd.c_str ();
123+ } else {
124+ MiscTelemetryInfo misc_info;
125+ misc_info.meta_data [" internal_errors" ] = " Cannot determine CWD" ;
126+ if (auto er = dispatch (&misc_info)) {
127+ LLDB_LOG (GetLog (LLDBLog::Object),
128+ " Failed to dispatch misc-info at startup" );
129+ }
130+ }
131+
132+ if (auto er = dispatch (entry)) {
133+ LLDB_LOG (GetLog (LLDBLog::Object), " Failed to dispatch entry at startup" );
134+ }
135+ }
136+
137+ void TelemetryManager::AtMainExecutableLoadEnd (TargetInfo *entry) {
138+ // ....
139+ dispatch (entry);
140+ }
141+
69142std::unique_ptr<TelemetryManager> TelemetryManager::g_instance = nullptr ;
70143TelemetryManager *TelemetryManager::getInstance () { return g_instance.get (); }
71144
0 commit comments