Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1 | //===--------------------- ModuleCache.cpp ----------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Zachary Turner | 01c3243 | 2017-02-14 19:06:07 +0000 | [diff] [blame] | 9 | #include "lldb/Target/ModuleCache.h" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 10 | |
| 11 | #include "lldb/Core/Module.h" |
Tamas Berghammer | 257e13a | 2015-12-10 17:08:23 +0000 | [diff] [blame] | 12 | #include "lldb/Core/ModuleList.h" |
Oleksiy Vyalov | eda270e | 2015-03-12 18:18:03 +0000 | [diff] [blame] | 13 | #include "lldb/Core/ModuleSpec.h" |
Oleksiy Vyalov | 919ef9d | 2015-05-07 15:28:49 +0000 | [diff] [blame] | 14 | #include "lldb/Host/File.h" |
Oleksiy Vyalov | 919ef9d | 2015-05-07 15:28:49 +0000 | [diff] [blame] | 15 | #include "lldb/Host/LockFile.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/Log.h" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileUtilities.h" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 19 | |
| 20 | #include <assert.h> |
| 21 | |
| 22 | #include <cstdio> |
Oleksiy Vyalov | 919ef9d | 2015-05-07 15:28:49 +0000 | [diff] [blame] | 23 | |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | namespace { |
| 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | const char *kModulesSubdir = ".cache"; |
| 30 | const char *kLockDirName = ".lock"; |
| 31 | const char *kTempFileName = ".temp"; |
| 32 | const char *kTempSymFileName = ".symtemp"; |
| 33 | const char *kSymFileExtension = ".sym"; |
| 34 | const char *kFSIllegalChars = "\\/:*?\"<>|"; |
Oleksiy Vyalov | 3154e77 | 2016-05-24 18:09:05 +0000 | [diff] [blame] | 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | std::string GetEscapedHostname(const char *hostname) { |
Jason Molenda | 14699cf | 2016-10-21 02:32:08 +0000 | [diff] [blame] | 37 | if (hostname == nullptr) |
| 38 | hostname = "unknown"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | std::string result(hostname); |
| 40 | size_t size = result.size(); |
| 41 | for (size_t i = 0; i < size; ++i) { |
| 42 | if ((result[i] >= 1 && result[i] <= 31) || |
| 43 | strchr(kFSIllegalChars, result[i]) != nullptr) |
| 44 | result[i] = '_'; |
| 45 | } |
| 46 | return result; |
Oleksiy Vyalov | 3154e77 | 2016-05-24 18:09:05 +0000 | [diff] [blame] | 47 | } |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | class ModuleLock { |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 50 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | File m_file; |
| 52 | std::unique_ptr<lldb_private::LockFile> m_lock; |
| 53 | FileSpec m_file_spec; |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 54 | |
| 55 | public: |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 56 | ModuleLock(const FileSpec &root_dir_spec, const UUID &uuid, Status &error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | void Delete(); |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 60 | static FileSpec JoinPath(const FileSpec &path1, const char *path2) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | FileSpec result_spec(path1); |
| 62 | result_spec.AppendPathComponent(path2); |
| 63 | return result_spec; |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 66 | static Status MakeDirectory(const FileSpec &dir_path) { |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 67 | namespace fs = llvm::sys::fs; |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 68 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 69 | return fs::create_directories(dir_path.GetPath(), true, fs::perms::owner_all); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | FileSpec GetModuleDirectory(const FileSpec &root_dir_spec, const UUID &uuid) { |
| 73 | const auto modules_dir_spec = JoinPath(root_dir_spec, kModulesSubdir); |
| 74 | return JoinPath(modules_dir_spec, uuid.GetAsString().c_str()); |
Oleksiy Vyalov | a9ea071 | 2015-05-08 23:54:34 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | FileSpec GetSymbolFileSpec(const FileSpec &module_file_spec) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 78 | return FileSpec(module_file_spec.GetPath() + kSymFileExtension); |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | void DeleteExistingModule(const FileSpec &root_dir_spec, |
| 82 | const FileSpec &sysroot_module_path_spec) { |
| 83 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); |
| 84 | UUID module_uuid; |
| 85 | { |
| 86 | auto module_sp = |
| 87 | std::make_shared<Module>(ModuleSpec(sysroot_module_path_spec)); |
| 88 | module_uuid = module_sp->GetUUID(); |
| 89 | } |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | if (!module_uuid.IsValid()) |
| 92 | return; |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 93 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 94 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | ModuleLock lock(root_dir_spec, module_uuid, error); |
| 96 | if (error.Fail()) { |
| 97 | if (log) |
| 98 | log->Printf("Failed to lock module %s: %s", |
| 99 | module_uuid.GetAsString().c_str(), error.AsCString()); |
| 100 | } |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 101 | |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 102 | namespace fs = llvm::sys::fs; |
| 103 | fs::file_status st; |
| 104 | if (status(sysroot_module_path_spec.GetPath(), st)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | return; |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 106 | |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 107 | if (st.getLinkCount() > 2) // module is referred by other hosts. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | return; |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | const auto module_spec_dir = GetModuleDirectory(root_dir_spec, module_uuid); |
Zachary Turner | d82067f | 2017-03-09 05:12:36 +0000 | [diff] [blame] | 111 | llvm::sys::fs::remove_directories(module_spec_dir.GetPath()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | lock.Delete(); |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | void DecrementRefExistingModule(const FileSpec &root_dir_spec, |
| 116 | const FileSpec &sysroot_module_path_spec) { |
| 117 | // Remove $platform/.cache/$uuid folder if nobody else references it. |
| 118 | DeleteExistingModule(root_dir_spec, sysroot_module_path_spec); |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | // Remove sysroot link. |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 121 | llvm::sys::fs::remove(sysroot_module_path_spec.GetPath()); |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | FileSpec symfile_spec = GetSymbolFileSpec(sysroot_module_path_spec); |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 124 | llvm::sys::fs::remove(symfile_spec.GetPath()); |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 127 | Status CreateHostSysRootModuleLink(const FileSpec &root_dir_spec, |
| 128 | const char *hostname, |
| 129 | const FileSpec &platform_module_spec, |
| 130 | const FileSpec &local_module_spec, |
| 131 | bool delete_existing) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | const auto sysroot_module_path_spec = |
| 133 | JoinPath(JoinPath(root_dir_spec, hostname), |
| 134 | platform_module_spec.GetPath().c_str()); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 135 | if (FileSystem::Instance().Exists(sysroot_module_path_spec)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | if (!delete_existing) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 137 | return Status(); |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | DecrementRefExistingModule(root_dir_spec, sysroot_module_path_spec); |
| 140 | } |
Oleksiy Vyalov | a9ea071 | 2015-05-08 23:54:34 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | const auto error = MakeDirectory( |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 143 | FileSpec(sysroot_module_path_spec.GetDirectory().AsCString())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | if (error.Fail()) |
| 145 | return error; |
Oleksiy Vyalov | a9ea071 | 2015-05-08 23:54:34 +0000 | [diff] [blame] | 146 | |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 147 | return llvm::sys::fs::create_hard_link(local_module_spec.GetPath(), |
| 148 | sysroot_module_path_spec.GetPath()); |
Oleksiy Vyalov | a9ea071 | 2015-05-08 23:54:34 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | } // namespace |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | ModuleLock::ModuleLock(const FileSpec &root_dir_spec, const UUID &uuid, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 154 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | const auto lock_dir_spec = JoinPath(root_dir_spec, kLockDirName); |
| 156 | error = MakeDirectory(lock_dir_spec); |
| 157 | if (error.Fail()) |
| 158 | return; |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | m_file_spec = JoinPath(lock_dir_spec, uuid.GetAsString().c_str()); |
Jonas Devlieghere | 50bc1ed | 2018-11-02 22:34:51 +0000 | [diff] [blame] | 161 | FileSystem::Instance().Open(m_file, m_file_spec, |
| 162 | File::eOpenOptionWrite | |
| 163 | File::eOpenOptionCanCreate | |
| 164 | File::eOpenOptionCloseOnExec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | if (!m_file) { |
| 166 | error.SetErrorToErrno(); |
| 167 | return; |
| 168 | } |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | m_lock.reset(new lldb_private::LockFile(m_file.GetDescriptor())); |
| 171 | error = m_lock->WriteLock(0, 1); |
| 172 | if (error.Fail()) |
| 173 | error.SetErrorStringWithFormat("Failed to lock file: %s", |
| 174 | error.AsCString()); |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | void ModuleLock::Delete() { |
| 178 | if (!m_file) |
| 179 | return; |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | m_file.Close(); |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 182 | llvm::sys::fs::remove(m_file_spec.GetPath()); |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | ///////////////////////////////////////////////////////////////////////// |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 186 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 187 | Status ModuleCache::Put(const FileSpec &root_dir_spec, const char *hostname, |
| 188 | const ModuleSpec &module_spec, const FileSpec &tmp_file, |
| 189 | const FileSpec &target_file) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | const auto module_spec_dir = |
| 191 | GetModuleDirectory(root_dir_spec, module_spec.GetUUID()); |
| 192 | const auto module_file_path = |
| 193 | JoinPath(module_spec_dir, target_file.GetFilename().AsCString()); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | const auto tmp_file_path = tmp_file.GetPath(); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 196 | const auto err_code = |
| 197 | llvm::sys::fs::rename(tmp_file_path, module_file_path.GetPath()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | if (err_code) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 199 | return Status("Failed to rename file %s to %s: %s", tmp_file_path.c_str(), |
| 200 | module_file_path.GetPath().c_str(), |
| 201 | err_code.message().c_str()); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 202 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 203 | const auto error = CreateHostSysRootModuleLink( |
| 204 | root_dir_spec, hostname, target_file, module_file_path, true); |
| 205 | if (error.Fail()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 206 | return Status("Failed to create link to %s: %s", |
| 207 | module_file_path.GetPath().c_str(), error.AsCString()); |
| 208 | return Status(); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 211 | Status ModuleCache::Get(const FileSpec &root_dir_spec, const char *hostname, |
| 212 | const ModuleSpec &module_spec, |
| 213 | ModuleSP &cached_module_sp, bool *did_create_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | const auto find_it = |
| 215 | m_loaded_modules.find(module_spec.GetUUID().GetAsString()); |
| 216 | if (find_it != m_loaded_modules.end()) { |
| 217 | cached_module_sp = (*find_it).second.lock(); |
| 218 | if (cached_module_sp) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 219 | return Status(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | m_loaded_modules.erase(find_it); |
| 221 | } |
Oleksiy Vyalov | eda270e | 2015-03-12 18:18:03 +0000 | [diff] [blame] | 222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | const auto module_spec_dir = |
| 224 | GetModuleDirectory(root_dir_spec, module_spec.GetUUID()); |
| 225 | const auto module_file_path = JoinPath( |
| 226 | module_spec_dir, module_spec.GetFileSpec().GetFilename().AsCString()); |
Oleksiy Vyalov | eda270e | 2015-03-12 18:18:03 +0000 | [diff] [blame] | 227 | |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 228 | if (!FileSystem::Instance().Exists(module_file_path)) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 229 | return Status("Module %s not found", module_file_path.GetPath().c_str()); |
Jonas Devlieghere | 59b78bc | 2018-11-01 04:45:28 +0000 | [diff] [blame] | 230 | if (FileSystem::Instance().GetByteSize(module_file_path) != |
| 231 | module_spec.GetObjectSize()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 232 | return Status("Module %s has invalid file size", |
| 233 | module_file_path.GetPath().c_str()); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 234 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | // We may have already cached module but downloaded from an another host - in |
| 236 | // this case let's create a link to it. |
| 237 | auto error = CreateHostSysRootModuleLink(root_dir_spec, hostname, |
| 238 | module_spec.GetFileSpec(), |
| 239 | module_file_path, false); |
| 240 | if (error.Fail()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 241 | return Status("Failed to create link to %s: %s", |
| 242 | module_file_path.GetPath().c_str(), error.AsCString()); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 243 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 244 | auto cached_module_spec(module_spec); |
| 245 | cached_module_spec.GetUUID().Clear(); // Clear UUID since it may contain md5 |
| 246 | // content hash instead of real UUID. |
| 247 | cached_module_spec.GetFileSpec() = module_file_path; |
| 248 | cached_module_spec.GetPlatformFileSpec() = module_spec.GetFileSpec(); |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame] | 249 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | error = ModuleList::GetSharedModule(cached_module_spec, cached_module_sp, |
| 251 | nullptr, nullptr, did_create_ptr, false); |
| 252 | if (error.Fail()) |
| 253 | return error; |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame] | 254 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 255 | FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec()); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 256 | if (FileSystem::Instance().Exists(symfile_spec)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | cached_module_sp->SetSymbolFileFileSpec(symfile_spec); |
Oleksiy Vyalov | eda270e | 2015-03-12 18:18:03 +0000 | [diff] [blame] | 258 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 259 | m_loaded_modules.insert( |
| 260 | std::make_pair(module_spec.GetUUID().GetAsString(), cached_module_sp)); |
| 261 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 262 | return Status(); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 265 | Status ModuleCache::GetAndPut(const FileSpec &root_dir_spec, |
| 266 | const char *hostname, |
| 267 | const ModuleSpec &module_spec, |
| 268 | const ModuleDownloader &module_downloader, |
| 269 | const SymfileDownloader &symfile_downloader, |
| 270 | lldb::ModuleSP &cached_module_sp, |
| 271 | bool *did_create_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | const auto module_spec_dir = |
| 273 | GetModuleDirectory(root_dir_spec, module_spec.GetUUID()); |
| 274 | auto error = MakeDirectory(module_spec_dir); |
| 275 | if (error.Fail()) |
| 276 | return error; |
Oleksiy Vyalov | 919ef9d | 2015-05-07 15:28:49 +0000 | [diff] [blame] | 277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | ModuleLock lock(root_dir_spec, module_spec.GetUUID(), error); |
| 279 | if (error.Fail()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 280 | return Status("Failed to lock module %s: %s", |
| 281 | module_spec.GetUUID().GetAsString().c_str(), |
| 282 | error.AsCString()); |
Oleksiy Vyalov | 919ef9d | 2015-05-07 15:28:49 +0000 | [diff] [blame] | 283 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | const auto escaped_hostname(GetEscapedHostname(hostname)); |
| 285 | // Check local cache for a module. |
| 286 | error = Get(root_dir_spec, escaped_hostname.c_str(), module_spec, |
| 287 | cached_module_sp, did_create_ptr); |
| 288 | if (error.Success()) |
| 289 | return error; |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | const auto tmp_download_file_spec = JoinPath(module_spec_dir, kTempFileName); |
| 292 | error = module_downloader(module_spec, tmp_download_file_spec); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 293 | llvm::FileRemover tmp_file_remover(tmp_download_file_spec.GetPath()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | if (error.Fail()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 295 | return Status("Failed to download module: %s", error.AsCString()); |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 296 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 297 | // Put downloaded file into local module cache. |
| 298 | error = Put(root_dir_spec, escaped_hostname.c_str(), module_spec, |
| 299 | tmp_download_file_spec, module_spec.GetFileSpec()); |
| 300 | if (error.Fail()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 301 | return Status("Failed to put module into cache: %s", error.AsCString()); |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 302 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 303 | tmp_file_remover.releaseFile(); |
| 304 | error = Get(root_dir_spec, escaped_hostname.c_str(), module_spec, |
| 305 | cached_module_sp, did_create_ptr); |
| 306 | if (error.Fail()) |
| 307 | return error; |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame] | 308 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 309 | // Fetching a symbol file for the module |
| 310 | const auto tmp_download_sym_file_spec = |
| 311 | JoinPath(module_spec_dir, kTempSymFileName); |
| 312 | error = symfile_downloader(cached_module_sp, tmp_download_sym_file_spec); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 313 | llvm::FileRemover tmp_symfile_remover(tmp_download_sym_file_spec.GetPath()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | if (error.Fail()) |
| 315 | // Failed to download a symfile but fetching the module was successful. The |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 316 | // module might contain the necessary symbols and the debugging is also |
| 317 | // possible without a symfile. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 318 | return Status(); |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame] | 319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | error = Put(root_dir_spec, escaped_hostname.c_str(), module_spec, |
| 321 | tmp_download_sym_file_spec, |
| 322 | GetSymbolFileSpec(module_spec.GetFileSpec())); |
| 323 | if (error.Fail()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 324 | return Status("Failed to put symbol file into cache: %s", |
| 325 | error.AsCString()); |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame] | 326 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 327 | tmp_symfile_remover.releaseFile(); |
| 328 | |
| 329 | FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec()); |
| 330 | cached_module_sp->SetSymbolFileFileSpec(symfile_spec); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 331 | return Status(); |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 332 | } |