1*dda28197Spatrick //===-- HostInfoAndroid.cpp -----------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "lldb/Host/android/HostInfoAndroid.h"
10061da546Spatrick #include "lldb/Host/FileSystem.h"
11061da546Spatrick #include "lldb/Host/linux/HostInfoLinux.h"
12061da546Spatrick #include "llvm/ADT/SmallVector.h"
13061da546Spatrick #include "llvm/ADT/StringRef.h"
14061da546Spatrick
15061da546Spatrick using namespace lldb_private;
16061da546Spatrick using namespace llvm;
17061da546Spatrick
ComputeHostArchitectureSupport(ArchSpec & arch_32,ArchSpec & arch_64)18061da546Spatrick void HostInfoAndroid::ComputeHostArchitectureSupport(ArchSpec &arch_32,
19061da546Spatrick ArchSpec &arch_64) {
20061da546Spatrick HostInfoLinux::ComputeHostArchitectureSupport(arch_32, arch_64);
21061da546Spatrick
22061da546Spatrick if (arch_32.IsValid()) {
23061da546Spatrick arch_32.GetTriple().setEnvironment(llvm::Triple::Android);
24061da546Spatrick }
25061da546Spatrick if (arch_64.IsValid()) {
26061da546Spatrick arch_64.GetTriple().setEnvironment(llvm::Triple::Android);
27061da546Spatrick }
28061da546Spatrick }
29061da546Spatrick
GetDefaultShell()30061da546Spatrick FileSpec HostInfoAndroid::GetDefaultShell() {
31061da546Spatrick return FileSpec("/system/bin/sh");
32061da546Spatrick }
33061da546Spatrick
ResolveLibraryPath(const std::string & module_path,const ArchSpec & arch)34061da546Spatrick FileSpec HostInfoAndroid::ResolveLibraryPath(const std::string &module_path,
35061da546Spatrick const ArchSpec &arch) {
36061da546Spatrick static const char *const ld_library_path_separator = ":";
37061da546Spatrick static const char *const default_lib32_path[] = {"/vendor/lib", "/system/lib",
38061da546Spatrick nullptr};
39061da546Spatrick static const char *const default_lib64_path[] = {"/vendor/lib64",
40061da546Spatrick "/system/lib64", nullptr};
41061da546Spatrick
42061da546Spatrick if (module_path.empty() || module_path[0] == '/') {
43061da546Spatrick FileSpec file_spec(module_path.c_str());
44061da546Spatrick FileSystem::Instance().Resolve(file_spec);
45061da546Spatrick return file_spec;
46061da546Spatrick }
47061da546Spatrick
48061da546Spatrick SmallVector<StringRef, 4> ld_paths;
49061da546Spatrick
50061da546Spatrick if (const char *ld_library_path = ::getenv("LD_LIBRARY_PATH"))
51061da546Spatrick StringRef(ld_library_path)
52061da546Spatrick .split(ld_paths, StringRef(ld_library_path_separator), -1, false);
53061da546Spatrick
54061da546Spatrick const char *const *default_lib_path = nullptr;
55061da546Spatrick switch (arch.GetAddressByteSize()) {
56061da546Spatrick case 4:
57061da546Spatrick default_lib_path = default_lib32_path;
58061da546Spatrick break;
59061da546Spatrick case 8:
60061da546Spatrick default_lib_path = default_lib64_path;
61061da546Spatrick break;
62061da546Spatrick default:
63061da546Spatrick assert(false && "Unknown address byte size");
64061da546Spatrick return FileSpec();
65061da546Spatrick }
66061da546Spatrick
67061da546Spatrick for (const char *const *it = default_lib_path; *it; ++it)
68061da546Spatrick ld_paths.push_back(StringRef(*it));
69061da546Spatrick
70061da546Spatrick for (const StringRef &path : ld_paths) {
71061da546Spatrick FileSpec file_candidate(path.str().c_str());
72061da546Spatrick FileSystem::Instance().Resolve(file_candidate);
73061da546Spatrick file_candidate.AppendPathComponent(module_path.c_str());
74061da546Spatrick
75061da546Spatrick if (FileSystem::Instance().Exists(file_candidate))
76061da546Spatrick return file_candidate;
77061da546Spatrick }
78061da546Spatrick
79061da546Spatrick return FileSpec();
80061da546Spatrick }
81061da546Spatrick
ComputeTempFileBaseDirectory(FileSpec & file_spec)82061da546Spatrick bool HostInfoAndroid::ComputeTempFileBaseDirectory(FileSpec &file_spec) {
83061da546Spatrick bool success = HostInfoLinux::ComputeTempFileBaseDirectory(file_spec);
84061da546Spatrick
85061da546Spatrick // On Android, there is no path which is guaranteed to be writable. If the
86061da546Spatrick // user has not provided a path via an environment variable, the generic
87061da546Spatrick // algorithm will deduce /tmp, which is plain wrong. In that case we have an
88061da546Spatrick // invalid directory, we substitute the path with /data/local/tmp, which is
89061da546Spatrick // correct at least in some cases (i.e., when running as shell user).
90061da546Spatrick if (!success || !FileSystem::Instance().Exists(file_spec))
91061da546Spatrick file_spec = FileSpec("/data/local/tmp");
92061da546Spatrick
93061da546Spatrick return FileSystem::Instance().Exists(file_spec);
94061da546Spatrick }
95