10 #include "lldb/Host/Config.h"
15 #include "llvm/Support/Threading.h"
20 #include <sys/utsname.h>
30 struct HostInfoLinuxFields {
31 llvm::once_flag m_distribution_once_flag;
33 llvm::once_flag m_os_version_once_flag;
34 llvm::VersionTuple m_os_version;
38 static HostInfoLinuxFields *
g_fields =
nullptr;
43 g_fields =
new HostInfoLinuxFields();
47 assert(
g_fields &&
"Missing call to Initialize?");
54 assert(
g_fields &&
"Missing call to Initialize?");
55 llvm::call_once(
g_fields->m_os_version_once_flag, []() {
60 llvm::StringRef release = un.release;
63 release = release.substr(0, release.find_first_not_of(
"0123456789."));
64 g_fields->m_os_version.tryParse(release);
72 ::memset(&un, 0,
sizeof(utsname));
81 assert(
g_fields &&
"Missing call to Initialize?");
84 llvm::call_once(
g_fields->m_distribution_once_flag, []() {
85 Log *log = GetLog(LLDBLog::Host);
86 LLDB_LOGF(log,
"attempting to determine Linux distribution...");
89 const char *const exe_paths[] = {
"/bin/lsb_release",
90 "/usr/bin/lsb_release"};
92 for (
size_t exe_index = 0;
93 exe_index <
sizeof(exe_paths) /
sizeof(exe_paths[0]); ++exe_index) {
94 const char *
const get_distribution_info_exe = exe_paths[exe_index];
95 if (access(get_distribution_info_exe, F_OK)) {
97 LLDB_LOGF(log,
"executable doesn't exist: %s",
98 get_distribution_info_exe);
103 std::string get_distribution_id_command(get_distribution_info_exe);
104 get_distribution_id_command +=
" -i";
106 FILE *file = popen(get_distribution_id_command.c_str(),
"r");
109 "failed to run command: \"%s\", cannot retrieve "
110 "platform information",
111 get_distribution_id_command.c_str());
116 char distribution_id[256] = {
'\0'};
117 if (fgets(distribution_id,
sizeof(distribution_id) - 1, file) !=
119 LLDB_LOGF(log,
"distribution id command returned \"%s\"",
122 const char *
const distributor_id_key =
"Distributor ID:\t";
123 if (strstr(distribution_id, distributor_id_key)) {
125 std::string id_string(distribution_id + strlen(distributor_id_key));
126 llvm::erase_value(id_string,
'\n');
130 id_string.begin(), id_string.end(), id_string.begin(),
131 [](
char ch) { return tolower(isspace(ch) ?
'_' : ch); });
133 g_fields->m_distribution_id = id_string;
134 LLDB_LOGF(log,
"distribution id set to \"%s\"",
135 g_fields->m_distribution_id.c_str());
137 LLDB_LOGF(log,
"failed to find \"%s\" field in \"%s\"",
138 distributor_id_key, distribution_id);
142 "failed to retrieve distribution id, \"%s\" returned no"
144 get_distribution_id_command.c_str());
158 if (!g_program_filespec) {
160 ssize_t len = readlink(
"/proc/self/exe", exe_path,
sizeof(exe_path) - 1);
163 g_program_filespec.
SetFile(exe_path, FileSpec::Style::native);
167 return g_program_filespec;
179 FileSpec temp_file(
"/usr/" LLDB_INSTALL_LIBDIR_BASENAME
"/lldb/plugins");
189 const char *xdg_data_home = getenv(
"XDG_DATA_HOME");
190 if (xdg_data_home && xdg_data_home[0]) {
192 user_plugin_dir +=
"/lldb";
209 if (arch_32.
GetTriple().getVendor() == llvm::Triple::UnknownVendor)
210 arch_32.
GetTriple().setVendorName(llvm::StringRef());
214 if (arch_64.
GetTriple().getVendor() == llvm::Triple::UnknownVendor)
215 arch_64.
GetTriple().setVendorName(llvm::StringRef());