LLDB  mainline
HostInfoAndroid.cpp
Go to the documentation of this file.
1 //===-- HostInfoAndroid.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 
10 #include "lldb/Host/FileSystem.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 
15 using namespace lldb_private;
16 using namespace llvm;
17 
19  ArchSpec &arch_64) {
21 
22  if (arch_32.IsValid()) {
23  arch_32.GetTriple().setEnvironment(llvm::Triple::Android);
24  }
25  if (arch_64.IsValid()) {
26  arch_64.GetTriple().setEnvironment(llvm::Triple::Android);
27  }
28 }
29 
31  return FileSpec("/system/bin/sh");
32 }
33 
35  const ArchSpec &arch) {
36  static const char *const ld_library_path_separator = ":";
37  static const char *const default_lib32_path[] = {"/vendor/lib", "/system/lib",
38  nullptr};
39  static const char *const default_lib64_path[] = {"/vendor/lib64",
40  "/system/lib64", nullptr};
41 
42  if (module_path.empty() || module_path[0] == '/') {
43  FileSpec file_spec(module_path.c_str());
44  FileSystem::Instance().Resolve(file_spec);
45  return file_spec;
46  }
47 
48  SmallVector<StringRef, 4> ld_paths;
49 
50  if (const char *ld_library_path = ::getenv("LD_LIBRARY_PATH"))
51  StringRef(ld_library_path)
52  .split(ld_paths, StringRef(ld_library_path_separator), -1, false);
53 
54  const char *const *default_lib_path = nullptr;
55  switch (arch.GetAddressByteSize()) {
56  case 4:
57  default_lib_path = default_lib32_path;
58  break;
59  case 8:
60  default_lib_path = default_lib64_path;
61  break;
62  default:
63  assert(false && "Unknown address byte size");
64  return FileSpec();
65  }
66 
67  for (const char *const *it = default_lib_path; *it; ++it)
68  ld_paths.push_back(StringRef(*it));
69 
70  for (const StringRef &path : ld_paths) {
71  FileSpec file_candidate(path.str().c_str());
72  FileSystem::Instance().Resolve(file_candidate);
73  file_candidate.AppendPathComponent(module_path.c_str());
74 
75  if (FileSystem::Instance().Exists(file_candidate))
76  return file_candidate;
77  }
78 
79  return FileSpec();
80 }
81 
83  bool success = HostInfoLinux::ComputeTempFileBaseDirectory(file_spec);
84 
85  // On Android, there is no path which is guaranteed to be writable. If the
86  // user has not provided a path via an environment variable, the generic
87  // algorithm will deduce /tmp, which is plain wrong. In that case we have an
88  // invalid directory, we substitute the path with /data/local/tmp, which is
89  // correct at least in some cases (i.e., when running as shell user).
90  if (!success || !FileSystem::Instance().Exists(file_spec))
91  file_spec = FileSpec("/data/local/tmp");
92 
93  return FileSystem::Instance().Exists(file_spec);
94 }
lldb_private::HostInfoAndroid::ComputeHostArchitectureSupport
static void ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
Definition: HostInfoAndroid.cpp:18
llvm
Definition: Debugger.h:49
lldb_private::ArchSpec
Definition: ArchSpec.h:33
FileSystem.h
lldb_private::HostInfoLinux::ComputeHostArchitectureSupport
static void ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
Definition: HostInfoLinux.cpp:199
lldb_private::FileSystem::Exists
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
Definition: common/FileSystem.cpp:170
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:444
lldb_private::FileSpec
Definition: FileSpec.h:56
HostInfoLinux.h
lldb_private::HostInfoBase::ComputeTempFileBaseDirectory
static bool ComputeTempFileBaseDirectory(FileSpec &file_spec)
Definition: HostInfoBase.cpp:280
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::FileSystem::Resolve
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
Definition: common/FileSystem.cpp:255
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:342
lldb_private::ArchSpec::GetAddressByteSize
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition: ArchSpec.cpp:683
lldb_private::HostInfoAndroid::ResolveLibraryPath
static FileSpec ResolveLibraryPath(const std::string &path, const ArchSpec &arch)
Definition: HostInfoAndroid.cpp:34
lldb_private::HostInfoAndroid::GetDefaultShell
static FileSpec GetDefaultShell()
Definition: HostInfoAndroid.cpp:30
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:45
lldb_private::HostInfoAndroid::ComputeTempFileBaseDirectory
static bool ComputeTempFileBaseDirectory(FileSpec &file_spec)
Definition: HostInfoAndroid.cpp:82
lldb_private::FileSpec::AppendPathComponent
void AppendPathComponent(llvm::StringRef component)
Definition: FileSpec.cpp:435
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
HostInfoAndroid.h