Skip to content

Commit

Permalink
[NFC][sanitizer] Extract LoadStatus (#111909)
Browse files Browse the repository at this point in the history
For #111901
  • Loading branch information
vitalybuka authored Oct 10, 2024
1 parent c99b365 commit 5deadc6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
21 changes: 15 additions & 6 deletions compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,6 @@ ThreadLister::Result ThreadLister::ListThreads(

Result result = Ok;
for (bool first_read = true;; first_read = false) {
// Resize to max capacity if it was downsized by IsAlive.
buffer_.resize(buffer_.capacity());
CHECK_GE(buffer_.size(), 4096);
uptr read = internal_getdents(
descriptor, (struct linux_dirent *)buffer_.data(), buffer_.size());
Expand Down Expand Up @@ -1088,14 +1086,25 @@ ThreadLister::Result ThreadLister::ListThreads(
}
}

const char *ThreadLister::LoadStatus(int tid) {
auto cleanup = at_scope_exit([&] {
// Resize back to capacity if it is downsized by `ReadFileToVector`.
buffer_.resize(buffer_.capacity());
});
if (!ReadFileToVector(status_path_.data(), &buffer_) || buffer_.empty())
return nullptr;
buffer_.push_back('\0');
return buffer_.data();
}

bool ThreadLister::IsAlive(int tid) {
// /proc/%d/task/%d/status uses same call to detect alive threads as
// proc_task_readdir. See task_state implementation in Linux.
if (!ReadFileToVector(status_path_.data(), &buffer_) || buffer_.empty())
return false;
buffer_.push_back(0);
static const char kPrefix[] = "\nPPid:";
const char *field = internal_strstr(buffer_.data(), kPrefix);
const char *status = LoadStatus(tid);
if (!status)
return false;
const char *field = internal_strstr(status, kPrefix);
if (!field)
return false;
field += internal_strlen(kPrefix);
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ThreadLister {
Ok,
};
Result ListThreads(InternalMmapVector<tid_t> *threads);
const char *LoadStatus(int tid);

private:
bool IsAlive(int tid);
Expand Down

0 comments on commit 5deadc6

Please sign in to comment.