diff --git a/snapshot/linux/process_reader_linux.cc b/snapshot/linux/process_reader_linux.cc index 33d4f92b..43590116 100644 --- a/snapshot/linux/process_reader_linux.cc +++ b/snapshot/linux/process_reader_linux.cc @@ -236,7 +236,7 @@ bool ProcessReaderLinux::CPUTimes(timeval* user_time, for (const Thread& thread : threads_) { ProcStatReader stat; - if (!stat.Initialize(thread.tid)) { + if (!stat.Initialize(connection_, thread.tid)) { return false; } diff --git a/util/linux/proc_stat_reader.cc b/util/linux/proc_stat_reader.cc index b1dfe94f..795e5139 100644 --- a/util/linux/proc_stat_reader.cc +++ b/util/linux/proc_stat_reader.cc @@ -43,9 +43,12 @@ ProcStatReader::ProcStatReader() ProcStatReader::~ProcStatReader() {} -bool ProcStatReader::Initialize(pid_t tid) { +bool ProcStatReader::Initialize(PtraceConnection* connection, pid_t tid) { INITIALIZATION_STATE_SET_INITIALIZING(initialized_); - if (!ReadFile(tid)) { + + char path[32]; + snprintf(path, arraysize(path), "/proc/%d/stat", tid); + if (!connection->ReadFileContents(base::FilePath(path), &contents_)) { return false; } @@ -110,15 +113,6 @@ bool ProcStatReader::StartTime(timeval* start_time) const { return true; } -bool ProcStatReader::ReadFile(pid_t tid) { - char path[32]; - snprintf(path, arraysize(path), "/proc/%d/stat", tid); - if (!LoggingReadEntireFile(base::FilePath(path), &contents_)) { - return false; - } - return true; -} - bool ProcStatReader::FindColumn(int col_index, const char** column) const { size_t position = third_column_position_; for (int index = 2; index < col_index; ++index) { diff --git a/util/linux/proc_stat_reader.h b/util/linux/proc_stat_reader.h index 4cad97c5..c0b30520 100644 --- a/util/linux/proc_stat_reader.h +++ b/util/linux/proc_stat_reader.h @@ -22,6 +22,7 @@ #include #include "base/macros.h" +#include "util/linux/ptrace_connection.h" #include "util/misc/initialization_state_dcheck.h" namespace crashpad { @@ -36,8 +37,10 @@ class ProcStatReader { //! //! This method must be successfully called before calling any other. //! + //! \param[in] connection A connection to the process to which the target + //! thread belongs. //! \param[in] tid The thread ID to read the stat file for. - bool Initialize(pid_t tid); + bool Initialize(PtraceConnection* connection, pid_t tid); //! \brief Determines the time the thread has spent executing in user mode. //! @@ -64,7 +67,6 @@ class ProcStatReader { bool StartTime(timeval* start_time) const; private: - bool ReadFile(pid_t tid); bool FindColumn(int index, const char** column) const; bool ReadTimeAtIndex(int index, timeval* time_val) const; diff --git a/util/linux/proc_stat_reader_test.cc b/util/linux/proc_stat_reader_test.cc index 8189c2ea..bc0d65bc 100644 --- a/util/linux/proc_stat_reader_test.cc +++ b/util/linux/proc_stat_reader_test.cc @@ -20,6 +20,7 @@ #include "base/logging.h" #include "gtest/gtest.h" +#include "test/linux/fake_ptrace_connection.h" #include "util/thread/thread.h" namespace crashpad { @@ -27,8 +28,11 @@ namespace test { namespace { TEST(ProcStatReader, Basic) { + FakePtraceConnection connection; + ASSERT_TRUE(connection.Initialize(getpid())); + ProcStatReader stat; - ASSERT_TRUE(stat.Initialize(getpid())); + ASSERT_TRUE(stat.Initialize(&connection, getpid())); timeval start_time; ASSERT_TRUE(stat.StartTime(&start_time)); @@ -53,8 +57,11 @@ pid_t gettid() { } void GetStartTime(timeval* start_time) { + FakePtraceConnection connection; + ASSERT_TRUE(connection.Initialize(getpid())); + ProcStatReader stat; - ASSERT_TRUE(stat.Initialize(gettid())); + ASSERT_TRUE(stat.Initialize(&connection, gettid())); ASSERT_TRUE(stat.StartTime(start_time)); } diff --git a/util/posix/process_info.h b/util/posix/process_info.h index 18a9cd93..7017cc64 100644 --- a/util/posix/process_info.h +++ b/util/posix/process_info.h @@ -174,6 +174,7 @@ class ProcessInfo { // multiple successive calls will always produce the same return value and out // parameters. This is necessary for intergration with the Snapshot interface. // See https://crashpad.chromium.org/bug/9. + PtraceConnection* connection_; std::set supplementary_groups_; mutable timeval start_time_; pid_t pid_; diff --git a/util/posix/process_info_linux.cc b/util/posix/process_info_linux.cc index 10c94b36..72a2bd70 100644 --- a/util/posix/process_info_linux.cc +++ b/util/posix/process_info_linux.cc @@ -20,13 +20,15 @@ #include "base/logging.h" #include "util/file/delimited_file_reader.h" #include "util/file/file_reader.h" +#include "util/file/string_file.h" #include "util/linux/proc_stat_reader.h" #include "util/misc/lexing.h" namespace crashpad { ProcessInfo::ProcessInfo() - : supplementary_groups_(), + : connection_(), + supplementary_groups_(), start_time_(), pid_(-1), ppid_(-1), @@ -45,16 +47,19 @@ bool ProcessInfo::InitializeWithPtrace(PtraceConnection* connection) { INITIALIZATION_STATE_SET_INITIALIZING(initialized_); DCHECK(connection); + connection_ = connection; pid_ = connection->GetProcessID(); is_64_bit_ = connection->Is64Bit(); { char path[32]; snprintf(path, sizeof(path), "/proc/%d/status", pid_); - FileReader status_file; - if (!status_file.Open(base::FilePath(path))) { + std::string contents; + if (!connection->ReadFileContents(base::FilePath(path), &contents)) { return false; } + StringFile status_file; + status_file.SetString(contents); DelimitedFileReader status_file_line_reader(&status_file); @@ -230,7 +235,7 @@ bool ProcessInfo::StartTime(timeval* start_time) const { if (start_time_initialized_.is_uninitialized()) { start_time_initialized_.set_invalid(); ProcStatReader reader; - if (!reader.Initialize(pid_)) { + if (!reader.Initialize(connection_, pid_)) { return false; } if (!reader.StartTime(&start_time_)) { @@ -252,10 +257,12 @@ bool ProcessInfo::Arguments(std::vector* argv) const { char path[32]; snprintf(path, sizeof(path), "/proc/%d/cmdline", pid_); - FileReader cmdline_file; - if (!cmdline_file.Open(base::FilePath(path))) { + std::string contents; + if (!connection_->ReadFileContents(base::FilePath(path), &contents)) { return false; } + StringFile cmdline_file; + cmdline_file.SetString(contents); DelimitedFileReader cmdline_file_field_reader(&cmdline_file);