10 #include "lldb/Host/Config.h"
15 #include "llvm/Support/Threading.h"
20 #include <sys/utsname.h>
29 struct HostInfoLinuxFields {
30 llvm::once_flag m_distribution_once_flag;
32 llvm::once_flag m_os_version_once_flag;
33 llvm::VersionTuple m_os_version;
37 static HostInfoLinuxFields *
g_fields =
nullptr;
42 g_fields =
new HostInfoLinuxFields();
46 assert(
g_fields &&
"Missing call to Initialize?");
53 assert(
g_fields &&
"Missing call to Initialize?");
54 llvm::call_once(
g_fields->m_os_version_once_flag, []() {
59 llvm::StringRef release = un.release;
62 release = release.substr(0, release.find_first_not_of(
"0123456789."));
63 g_fields->m_os_version.tryParse(release);
71 ::memset(&un, 0,
sizeof(utsname));
80 assert(
g_fields &&
"Missing call to Initialize?");
83 llvm::call_once(
g_fields->m_distribution_once_flag, []() {
84 Log *log = GetLog(LLDBLog::Host);
85 LLDB_LOGF(log,
"attempting to determine Linux distribution...");
88 const char *const exe_paths[] = {
"/bin/lsb_release",
89 "/usr/bin/lsb_release"};
91 for (
size_t exe_index = 0;
92 exe_index <
sizeof(exe_paths) /
sizeof(exe_paths[0]); ++exe_index) {
93 const char *
const get_distribution_info_exe = exe_paths[exe_index];
94 if (access(get_distribution_info_exe, F_OK)) {
96 LLDB_LOGF(log,
"executable doesn't exist: %s",
97 get_distribution_info_exe);
102 std::string get_distribution_id_command(get_distribution_info_exe);
103 get_distribution_id_command +=
" -i";
105 FILE *file = popen(get_distribution_id_command.c_str(),
"r");
108 "failed to run command: \"%s\", cannot retrieve "
109 "platform information",
110 get_distribution_id_command.c_str());
115 char distribution_id[256] = {
'\0'};
116 if (fgets(distribution_id,
sizeof(distribution_id) - 1, file) !=
118 LLDB_LOGF(log,
"distribution id command returned \"%s\"",
121 const char *
const distributor_id_key =
"Distributor ID:\t";
122 if (strstr(distribution_id, distributor_id_key)) {
124 std::string id_string(distribution_id + strlen(distributor_id_key));
125 id_string.erase(std::remove(id_string.begin(), id_string.end(),
'\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/lib" LLDB_LIBDIR_SUFFIX
"/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());