Skip to content

Commit 75cfc48

Browse files
clean up some declaration in header file
Created using spr 1.3.7
2 parents fbca588 + a19c9a8 commit 75cfc48

File tree

12 files changed

+315
-52
lines changed

12 files changed

+315
-52
lines changed

libc/src/__support/OSUtil/linux/auxv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <linux/auxvec.h> // For AT_ macros
1818
#include <linux/mman.h> // For mmap flags
19+
#include <linux/param.h> // For EXEC_PAGESIZE
1920
#include <linux/prctl.h> // For prctl
2021
#include <sys/syscall.h> // For syscall numbers
2122

@@ -90,7 +91,7 @@ LIBC_INLINE void Vector::fallback_initialize_unsync() {
9091
PROT_READ | PROT_WRITE,
9192
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
9293
// We do not proceed if mmap fails.
93-
if (mmap_ret <= 0)
94+
if (!linux_utils::is_valid_mmap(mmap_ret))
9495
return;
9596

9697
// Initialize the auxv array with AT_NULL entries.

libc/src/__support/OSUtil/linux/syscall.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/CPP/bit.h"
1313
#include "src/__support/common.h"
1414
#include "src/__support/macros/config.h"
15+
#include "src/__support/macros/optimization.h"
1516
#include "src/__support/macros/properties/architectures.h"
1617

1718
#ifdef LIBC_TARGET_ARCH_IS_X86_32
@@ -34,6 +35,19 @@ LIBC_INLINE R syscall_impl(long __number, Ts... ts) {
3435
return cpp::bit_or_static_cast<R>(syscall_impl(__number, (long)ts...));
3536
}
3637

38+
// Linux-specific function for checking
39+
namespace linux_utils {
40+
LIBC_INLINE_VAR constexpr unsigned long MAX_ERRNO = 4095;
41+
// Ideally, this should be defined using PAGE_OFFSET
42+
// However, that is a configurable parameter. We mimic kernel's behavior
43+
// by checking against MAX_ERRNO.
44+
template <typename PointerLike>
45+
LIBC_INLINE constexpr bool is_valid_mmap(PointerLike ptr) {
46+
long addr = cpp::bit_cast<long>(ptr);
47+
return LIBC_LIKELY(addr > 0 || addr < -static_cast<long>(MAX_ERRNO));
48+
}
49+
} // namespace linux_utils
50+
3751
} // namespace LIBC_NAMESPACE_DECL
3852

3953
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_H

libc/src/__support/threads/linux/thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ LIBC_INLINE ErrorOr<void *> alloc_stack(size_t stacksize, size_t guardsize) {
100100
-1, // Not backed by any file
101101
0 // No offset
102102
);
103-
if (mmap_result < 0 && (uintptr_t(mmap_result) >= UINTPTR_MAX - size))
103+
if (!linux_utils::is_valid_mmap(mmap_result))
104104
return Error{int(-mmap_result)};
105105

106106
if (guardsize) {

libc/src/sys/mman/linux/mmap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ LLVM_LIBC_FUNCTION(void *, mmap,
5656
// However, since a valid return address cannot be within the last page, a
5757
// return value corresponding to a location in the last page is an error
5858
// value.
59-
if (ret < 0 && ret > -EXEC_PAGESIZE) {
59+
if (!linux_utils::is_valid_mmap(ret)) {
6060
libc_errno = static_cast<int>(-ret);
6161
return MAP_FAILED;
6262
}

libc/startup/linux/aarch64/tls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void init_tls(TLSDescriptor &tls_descriptor) {
6262
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
6363
// We cannot check the return value with MAP_FAILED as that is the return
6464
// of the mmap function and not the mmap syscall.
65-
if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.page_size)
65+
if (!linux_utils::is_valid_mmap(mmap_ret_val))
6666
syscall_impl<long>(SYS_exit, 1);
6767
uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
6868
uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;

libc/startup/linux/riscv/tls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void init_tls(TLSDescriptor &tls_descriptor) {
5050
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
5151
// We cannot check the return value with MAP_FAILED as that is the return
5252
// of the mmap function and not the mmap syscall.
53-
if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.page_size)
53+
if (!linux_utils::is_valid_mmap(mmap_ret_val))
5454
syscall_impl<long>(SYS_exit, 1);
5555
uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
5656
uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;

libc/startup/linux/x86_64/tls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void init_tls(TLSDescriptor &tls_descriptor) {
5353
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
5454
// We cannot check the return value with MAP_FAILED as that is the return
5555
// of the mmap function and not the mmap syscall.
56-
if (mmap_retval < 0 && static_cast<uintptr_t>(mmap_retval) > -app.page_size)
56+
if (!linux_utils::is_valid_mmap(mmap_retval))
5757
syscall_impl<long>(SYS_exit, 1);
5858
uintptr_t *tls_addr = reinterpret_cast<uintptr_t *>(mmap_retval);
5959

lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,249 @@ std::string PlatformAndroid::GetRunAs() {
477477
}
478478
return run_as.str();
479479
}
480+
481+
// Helper function to populate process status information from
482+
// /proc/[pid]/status
483+
void PlatformAndroid::PopulateProcessStatusInfo(
484+
lldb::pid_t pid, ProcessInstanceInfo &process_info) {
485+
// Read /proc/[pid]/status to get parent PID, UIDs, and GIDs
486+
Status error;
487+
AdbClientUP status_adb = GetAdbClient(error);
488+
if (error.Fail())
489+
return;
490+
491+
std::string status_output;
492+
StreamString status_cmd;
493+
status_cmd.Printf(
494+
"cat /proc/%llu/status 2>/dev/null | grep -E '^(PPid|Uid|Gid):'",
495+
static_cast<unsigned long long>(pid));
496+
Status status_error =
497+
status_adb->Shell(status_cmd.GetData(), seconds(5), &status_output);
498+
499+
if (status_error.Fail() || status_output.empty())
500+
return;
501+
502+
llvm::SmallVector<llvm::StringRef, 16> lines;
503+
llvm::StringRef(status_output).split(lines, '\n');
504+
505+
for (llvm::StringRef line : lines) {
506+
line = line.trim();
507+
if (line.starts_with("PPid:")) {
508+
llvm::StringRef ppid_str = line.substr(5).trim();
509+
lldb::pid_t ppid;
510+
if (llvm::to_integer(ppid_str, ppid))
511+
process_info.SetParentProcessID(ppid);
512+
} else if (line.starts_with("Uid:")) {
513+
llvm::SmallVector<llvm::StringRef, 4> uid_parts;
514+
line.substr(4).trim().split(uid_parts, '\t', -1, false);
515+
if (uid_parts.size() >= 2) {
516+
uint32_t uid, euid;
517+
if (llvm::to_integer(uid_parts[0].trim(), uid))
518+
process_info.SetUserID(uid);
519+
if (llvm::to_integer(uid_parts[1].trim(), euid))
520+
process_info.SetEffectiveUserID(euid);
521+
}
522+
} else if (line.starts_with("Gid:")) {
523+
llvm::SmallVector<llvm::StringRef, 4> gid_parts;
524+
line.substr(4).trim().split(gid_parts, '\t', -1, false);
525+
if (gid_parts.size() >= 2) {
526+
uint32_t gid, egid;
527+
if (llvm::to_integer(gid_parts[0].trim(), gid))
528+
process_info.SetGroupID(gid);
529+
if (llvm::to_integer(gid_parts[1].trim(), egid))
530+
process_info.SetEffectiveGroupID(egid);
531+
}
532+
}
533+
}
534+
}
535+
536+
// Helper function to populate command line arguments from /proc/[pid]/cmdline
537+
void PlatformAndroid::PopulateProcessCommandLine(
538+
lldb::pid_t pid, ProcessInstanceInfo &process_info) {
539+
// Read /proc/[pid]/cmdline to get command line arguments
540+
Status error;
541+
AdbClientUP cmdline_adb = GetAdbClient(error);
542+
if (error.Fail())
543+
return;
544+
545+
std::string cmdline_output;
546+
StreamString cmdline_cmd;
547+
cmdline_cmd.Printf("cat /proc/%llu/cmdline 2>/dev/null | tr '\\000' ' '",
548+
static_cast<unsigned long long>(pid));
549+
Status cmdline_error =
550+
cmdline_adb->Shell(cmdline_cmd.GetData(), seconds(5), &cmdline_output);
551+
552+
if (cmdline_error.Fail() || cmdline_output.empty())
553+
return;
554+
555+
cmdline_output = llvm::StringRef(cmdline_output).trim().str();
556+
if (cmdline_output.empty())
557+
return;
558+
559+
llvm::SmallVector<llvm::StringRef, 16> args;
560+
llvm::StringRef(cmdline_output).split(args, ' ', -1, false);
561+
if (args.empty())
562+
return;
563+
564+
process_info.SetArg0(args[0]);
565+
Args process_args;
566+
for (size_t i = 1; i < args.size(); i++) {
567+
if (!args[i].empty())
568+
process_args.AppendArgument(args[i]);
569+
}
570+
process_info.SetArguments(process_args, false);
571+
}
572+
573+
// Helper function to populate architecture from /proc/[pid]/exe
574+
void PlatformAndroid::PopulateProcessArchitecture(
575+
lldb::pid_t pid, ProcessInstanceInfo &process_info) {
576+
// Read /proc/[pid]/exe to get executable path for architecture detection
577+
Status error;
578+
AdbClientUP exe_adb = GetAdbClient(error);
579+
if (error.Fail())
580+
return;
581+
582+
std::string exe_output;
583+
StreamString exe_cmd;
584+
exe_cmd.Printf("readlink /proc/%llu/exe 2>/dev/null",
585+
static_cast<unsigned long long>(pid));
586+
Status exe_error = exe_adb->Shell(exe_cmd.GetData(), seconds(5), &exe_output);
587+
588+
if (exe_error.Fail() || exe_output.empty())
589+
return;
590+
591+
exe_output = llvm::StringRef(exe_output).trim().str();
592+
593+
// Determine architecture from exe path
594+
ArchSpec arch;
595+
if (exe_output.find("64") != std::string::npos ||
596+
exe_output.find("arm64") != std::string::npos ||
597+
exe_output.find("aarch64") != std::string::npos) {
598+
arch.SetTriple("aarch64-unknown-linux-android");
599+
} else if (exe_output.find("x86_64") != std::string::npos) {
600+
arch.SetTriple("x86_64-unknown-linux-android");
601+
} else if (exe_output.find("x86") != std::string::npos ||
602+
exe_output.find("i686") != std::string::npos) {
603+
arch.SetTriple("i686-unknown-linux-android");
604+
} else {
605+
// Default to armv7 for 32-bit ARM (most common on Android)
606+
arch.SetTriple("armv7-unknown-linux-android");
607+
}
608+
609+
if (arch.IsValid())
610+
process_info.SetArchitecture(arch);
611+
}
612+
613+
uint32_t
614+
PlatformAndroid::FindProcesses(const ProcessInstanceInfoMatch &match_info,
615+
ProcessInstanceInfoList &proc_infos) {
616+
proc_infos.clear();
617+
618+
// When LLDB is running natively on an Android device (IsHost() == true),
619+
// use the parent class's standard Linux /proc enumeration. IsHost() is only
620+
// true when compiled for Android (#if defined(__ANDROID__)), so calling
621+
// PlatformLinux methods is safe (Android is Linux-based).
622+
if (IsHost())
623+
return PlatformLinux::FindProcesses(match_info, proc_infos);
624+
625+
// Remote Android platform: implement process name lookup using 'pidof' over
626+
// adb.
627+
628+
// LLDB stores the search name in GetExecutableFile() (even though it's
629+
// actually a process name like "com.android.chrome" rather than an
630+
// executable path). If no search name is provided, we can't use
631+
// 'pidof', so return early with no results.
632+
const ProcessInstanceInfo &match_process_info = match_info.GetProcessInfo();
633+
if (!match_process_info.GetExecutableFile() ||
634+
match_info.GetNameMatchType() == NameMatch::Ignore) {
635+
return 0;
636+
}
637+
638+
// Extract the process name to search for (typically an Android package name
639+
// like "com.example.app" or binary name like "app_process64")
640+
std::string process_name = match_process_info.GetExecutableFile().GetPath();
641+
if (process_name.empty())
642+
return 0;
643+
644+
// Use adb to find the process by name
645+
Status error;
646+
AdbClientUP adb(GetAdbClient(error));
647+
if (error.Fail()) {
648+
Log *log = GetLog(LLDBLog::Platform);
649+
LLDB_LOGF(log, "PlatformAndroid::%s failed to get ADB client: %s",
650+
__FUNCTION__, error.AsCString());
651+
return 0;
652+
}
653+
654+
// Use 'pidof' command to get PIDs for the process name.
655+
// Quote the process name to handle special characters (spaces, etc.)
656+
std::string pidof_output;
657+
StreamString command;
658+
command.Printf("pidof '%s'", process_name.c_str());
659+
error = adb->Shell(command.GetData(), seconds(5), &pidof_output);
660+
661+
if (error.Fail()) {
662+
Log *log = GetLog(LLDBLog::Platform);
663+
LLDB_LOG(log, "PlatformAndroid::{} 'pidof {}' failed: {}", __FUNCTION__,
664+
process_name.c_str(), error.AsCString());
665+
return 0;
666+
}
667+
668+
// Parse PIDs from pidof output.
669+
// Note: pidof can return multiple PIDs (space-separated) if multiple
670+
// instances of the same executable are running.
671+
pidof_output = llvm::StringRef(pidof_output).trim().str();
672+
if (pidof_output.empty()) {
673+
Log *log = GetLog(LLDBLog::Platform);
674+
LLDB_LOGF(log, "PlatformAndroid::%s no process found with name '%s'",
675+
__FUNCTION__, process_name.c_str());
676+
return 0;
677+
}
678+
679+
// Split the output by whitespace to handle multiple PIDs
680+
llvm::SmallVector<llvm::StringRef, 8> pid_strings;
681+
llvm::StringRef(pidof_output).split(pid_strings, ' ', -1, false);
682+
683+
Log *log = GetLog(LLDBLog::Platform);
684+
685+
// Process each PID and gather information
686+
uint32_t num_matches = 0;
687+
for (llvm::StringRef pid_str : pid_strings) {
688+
pid_str = pid_str.trim();
689+
if (pid_str.empty())
690+
continue;
691+
692+
lldb::pid_t pid;
693+
if (!llvm::to_integer(pid_str, pid)) {
694+
LLDB_LOGF(log, "PlatformAndroid::%s failed to parse PID from: '%s'",
695+
__FUNCTION__, pid_str.str().c_str());
696+
continue;
697+
}
698+
699+
ProcessInstanceInfo process_info;
700+
process_info.SetProcessID(pid);
701+
process_info.GetExecutableFile().SetFile(process_name,
702+
FileSpec::Style::posix);
703+
704+
// Populate additional process information
705+
PopulateProcessStatusInfo(pid, process_info);
706+
PopulateProcessCommandLine(pid, process_info);
707+
PopulateProcessArchitecture(pid, process_info);
708+
709+
// Check if this process matches the criteria
710+
if (match_info.Matches(process_info)) {
711+
proc_infos.push_back(process_info);
712+
num_matches++;
713+
714+
LLDB_LOGF(log, "PlatformAndroid::%s found process '%s' with PID %llu",
715+
__FUNCTION__, process_name.c_str(),
716+
static_cast<unsigned long long>(pid));
717+
}
718+
}
719+
720+
return num_matches;
721+
}
722+
480723
std::unique_ptr<AdbSyncService> PlatformAndroid::GetSyncService(Status &error) {
481724
auto sync_service = std::make_unique<AdbSyncService>(m_device_id);
482725
error = sync_service->SetupSyncConnection();

lldb/source/Plugins/Platform/Android/PlatformAndroid.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class PlatformAndroid : public platform_linux::PlatformLinux {
5959

6060
uint32_t GetDefaultMemoryCacheLineSize() override;
6161

62+
uint32_t FindProcesses(const ProcessInstanceInfoMatch &match_info,
63+
ProcessInstanceInfoList &proc_infos) override;
64+
6265
protected:
6366
const char *GetCacheHostname() override;
6467

@@ -86,6 +89,14 @@ class PlatformAndroid : public platform_linux::PlatformLinux {
8689
private:
8790
std::string m_device_id;
8891
uint32_t m_sdk_version;
92+
93+
// Helper functions for process information gathering
94+
void PopulateProcessStatusInfo(lldb::pid_t pid,
95+
ProcessInstanceInfo &process_info);
96+
void PopulateProcessCommandLine(lldb::pid_t pid,
97+
ProcessInstanceInfo &process_info);
98+
void PopulateProcessArchitecture(lldb::pid_t pid,
99+
ProcessInstanceInfo &process_info);
89100
};
90101

91102
} // namespace platform_android

0 commit comments

Comments
 (0)