@@ -41,17 +41,32 @@ static uint64_t ToNanosec(const SteadyTimePoint Point) {
4141 return std::chrono::nanoseconds (Point.time_since_epoch ()).count ();
4242}
4343
44- static std::string MakeUUID (Debugger *debugger) {
44+ // Generate a unique string. This should be unique across different runs.
45+ // We build such string by combining three parts:
46+ // <16 random bytes>_<timestamp>_<hash of username>
47+ // This reduces the chances of getting the same UUID, even when the same
48+ // user runs the two copies of binary at the same time.
49+ static std::string MakeUUID () {
4550 uint8_t random_bytes[16 ];
51+ std::string randomString = " _" ;
4652 if (auto ec = llvm::getRandomBytes (random_bytes, 16 )) {
4753 LLDB_LOG (GetLog (LLDBLog::Object),
4854 " Failed to generate random bytes for UUID: {0}" , ec.message ());
49- // Fallback to using timestamp + debugger ID.
50- return llvm::formatv (
51- " {0}_{1}" , std::chrono::steady_clock::now ().time_since_epoch ().count (),
52- debugger->GetID ());
55+ } else {
56+ randomString = UUID (random_bytes).GetAsString ();
5357 }
54- return UUID (random_bytes).GetAsString ();
58+
59+ std::string username = " _" ;
60+ UserIDResolver &resolver = lldb_private::HostInfo::GetUserIDResolver ();
61+ std::optional<llvm::StringRef> opt_username =
62+ resolver.GetUserName (lldb_private::HostInfo::GetUserID ());
63+ if (opt_username)
64+ username = *opt_username;
65+
66+ return llvm::formatv (
67+ " {0}_{1}_{2}" , randomString,
68+ std::chrono::steady_clock::now ().time_since_epoch ().count (),
69+ llvm::MD5Hash (username));
5570}
5671
5772void LLDBBaseTelemetryInfo::serialize (Serializer &serializer) const {
@@ -62,7 +77,7 @@ void LLDBBaseTelemetryInfo::serialize(Serializer &serializer) const {
6277 serializer.write (" end_time" , ToNanosec (end_time.value ()));
6378}
6479
65- void DebuggerTelemetryInfo ::serialize (Serializer &serializer) const {
80+ void DebuggerInfo ::serialize (Serializer &serializer) const {
6681 LLDBBaseTelemetryInfo::serialize (serializer);
6782
6883 serializer.write (" username" , username);
@@ -85,18 +100,21 @@ void MiscTelemetryInfo::serialize(Serializer &serializer) const {
85100}
86101
87102TelemetryManager::TelemetryManager (std::unique_ptr<Config> config)
88- : m_config(std::move(config)) {}
103+ : m_config(std::move(config)), m_id(MakeUUID) {}
89104
90105llvm::Error TelemetryManager::preDispatch (TelemetryInfo *entry) {
106+ // Look up the session_id to assign to this entry or make one
107+ // if none had been computed for this debugger.
91108 LLDBBaseTelemetryInfo *lldb_entry =
92109 llvm::dyn_cast<LLDBBaseTelemetryInfo>(entry);
93- std::string session_id = " " ;
110+ std::string session_id = m_id ;
94111 if (Debugger *debugger = lldb_entry->debugger ) {
95- auto session_id_pos = session_ids.find (debugger);
112+ auto session_id_pos = session_ids.find (debugger-> getID () );
96113 if (session_id_pos != session_ids.end ())
97114 session_id = session_id_pos->second ;
98115 else
99- session_id_pos->second = session_id = MakeUUID (debugger);
116+ session_id_pos->second = session_id =
117+ llvm::formatv (" {0}_{1}" , m_id, debugger->getID ());
100118 }
101119 lldb_entry->SessionId = session_id;
102120
@@ -105,36 +123,13 @@ llvm::Error TelemetryManager::preDispatch(TelemetryInfo *entry) {
105123
106124const Config *getConfig () { return m_config.get (); }
107125
108- void TelemetryManager::atDebuggerStartup (DebuggerTelemetryInfo *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 (llvm::Error er = dispatch (&misc_info)) {
127- LLDB_LOG_ERROR (GetLog (LLDBLog::Object), std::move (er),
128- " Failed to dispatch misc-info at startup: {0}" );
129- }
130- }
131-
126+ void TelemetryManager::AtDebuggerStartup (DebuggerInfo *entry) {
132127 if (auto er = dispatch (entry)) {
133128 LLDB_LOG (GetLog (LLDBLog::Object), " Failed to dispatch entry at startup" );
134129 }
135130}
136131
137- void TelemetryManager::atDebuggerExit (DebuggerTelemetryInfo *entry) {
132+ void TelemetryManager::AtDebuggerExit (DebuggerInfo *entry) {
138133 // There must be a reference to the debugger at this point.
139134 assert (entry->debugger != nullptr );
140135
0 commit comments