1 //===-- lib/DebugInfo/Symbolize/DIFetcher.cpp -----------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file defines the implementation of the local debug info fetcher, which
11 /// searches cache directories.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/DebugInfo/Symbolize/DIFetcher.h"
16 
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Path.h"
19 
20 namespace llvm {
21 namespace symbolize {
22 
23 Optional<std::string>
fetchBuildID(ArrayRef<uint8_t> BuildID) const24 LocalDIFetcher::fetchBuildID(ArrayRef<uint8_t> BuildID) const {
25   auto GetDebugPath = [&](StringRef Directory) {
26     SmallString<128> Path{Directory};
27     sys::path::append(Path, ".build-id",
28                       llvm::toHex(BuildID[0], /*LowerCase=*/true),
29                       llvm::toHex(BuildID.slice(1), /*LowerCase=*/true));
30     Path += ".debug";
31     return Path;
32   };
33   if (DebugFileDirectory.empty()) {
34     SmallString<128> Path = GetDebugPath(
35 #if defined(__NetBSD__)
36         // Try /usr/libdata/debug/.build-id/../...
37         "/usr/libdata/debug"
38 #else
39         // Try /usr/lib/debug/.build-id/../...
40         "/usr/lib/debug"
41 #endif
42     );
43     if (llvm::sys::fs::exists(Path))
44       return std::string(Path);
45   } else {
46     for (const auto &Directory : DebugFileDirectory) {
47       // Try <debug-file-directory>/.build-id/../...
48       SmallString<128> Path = GetDebugPath(Directory);
49       if (llvm::sys::fs::exists(Path))
50         return std::string(Path);
51     }
52   }
53   return None;
54 }
55 
56 } // namespace symbolize
57 } // namespace llvm
58