mac handler: Record the number of open files in the handler process

The "file-limit" annotation has shown that the system as a whole is not
likely to be out of file descriptors globally. It’s possible that a file
descriptor leak in crashpad_handler itself is responsible for certain
crashes. Add a count of the number of open files in the handler process
to this annotation to test this theory.

Bug: crashpad:180
Change-Id: If6f2304fdabddd29636ba4ac5a7d1e0fff7f4b61
Reviewed-on: https://chromium-review.googlesource.com/585852
Reviewed-by: Robert Sesek <rsesek@chromium.org>
This commit is contained in:
Mark Mentovai 2017-07-26 10:57:56 -04:00
parent 281be63d00
commit 7e6a0145b1

View File

@ -14,7 +14,9 @@
#include "handler/mac/file_limit_annotation.h"
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/types.h>
@ -26,6 +28,9 @@
#include "base/strings/stringprintf.h"
#include "client/crashpad_info.h"
#include "client/simple_string_dictionary.h"
#include "util/posix/scoped_dir.h"
namespace crashpad {
namespace {
@ -44,6 +49,38 @@ std::string FormatFromSysctl(int rv, const int* value, const size_t* size) {
return base::StringPrintf("%d", *value);
}
// Counts the number of open file descriptors in the process and returns it as a
// string. This /dev/fd and the value returned will include the open file
// descriptor for that directory. If opendir() fails, the returned string will
// be "E" followed by the error number. If readdir_r() fails, it will be "R"
// followed by the error number.
std::string CountOpenFileDescriptors() {
DIR* dir = opendir("/dev/fd");
if (!dir) {
return base::StringPrintf("E%d", errno);
}
ScopedDIR dir_owner(dir);
dirent entry;
dirent* result;
int count = 0;
while ((errno = readdir_r(dir, &entry, &result)) == 0 && result != nullptr) {
const char* entry_name = &(*result->d_name);
if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
continue;
}
++count;
}
if (errno != 0) {
return base::StringPrintf("R%d", errno);
}
return base::StringPrintf("%d", count);
}
// Returns a string for |limit|, or "inf" if |limit| is RLIM_INFINITY.
std::string StringForRLim(rlim_t limit) {
if (limit == RLIM_INFINITY) {
@ -55,8 +92,6 @@ std::string StringForRLim(rlim_t limit) {
} // namespace
namespace crashpad {
void RecordFileLimitAnnotation() {
CrashpadInfo* crashpad_info = CrashpadInfo::GetCrashpadInfo();
SimpleStringDictionary* simple_annotations =
@ -76,6 +111,8 @@ void RecordFileLimitAnnotation() {
std::string max_files = FormatFromSysctl(
sysctl(mib, arraysize(mib), &value, &size, nullptr, 0), &value, &size);
std::string open_files = CountOpenFileDescriptors();
rlimit limit;
std::string nofile;
if (getrlimit(RLIMIT_NOFILE, &limit) != 0) {
@ -85,8 +122,11 @@ void RecordFileLimitAnnotation() {
StringForRLim(limit.rlim_cur) + "," + StringForRLim(limit.rlim_max);
}
std::string annotation = base::StringPrintf(
"%s,%s,%s", num_files.c_str(), max_files.c_str(), nofile.c_str());
std::string annotation = base::StringPrintf("%s,%s,%s,%s",
num_files.c_str(),
max_files.c_str(),
open_files.c_str(),
nofile.c_str());
simple_annotations->SetKeyValue("file-limits", annotation.c_str());
}