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