Merge "debuggerd: Dump list of open files on process crash."

am: b675f7cd32

Change-Id: Iec231fc7eb57fa2c51aabc0fb75b4a09c47004cd
This commit is contained in:
Richard Uhler 2016-11-30 09:18:57 +00:00 committed by android-build-merger
commit 3ff028c7db
8 changed files with 183 additions and 10 deletions

View file

@ -18,6 +18,7 @@ LOCAL_SRC_FILES:= \
debuggerd.cpp \ debuggerd.cpp \
elf_utils.cpp \ elf_utils.cpp \
getevent.cpp \ getevent.cpp \
open_files_list.cpp \
signal_sender.cpp \ signal_sender.cpp \
tombstone.cpp \ tombstone.cpp \
utility.cpp \ utility.cpp \
@ -108,9 +109,11 @@ include $(BUILD_EXECUTABLE)
debuggerd_test_src_files := \ debuggerd_test_src_files := \
utility.cpp \ utility.cpp \
open_files_list.cpp \
test/dump_memory_test.cpp \ test/dump_memory_test.cpp \
test/elf_fake.cpp \ test/elf_fake.cpp \
test/log_fake.cpp \ test/log_fake.cpp \
test/open_files_list_test.cpp \
test/property_fake.cpp \ test/property_fake.cpp \
test/ptrace_fake.cpp \ test/ptrace_fake.cpp \
test/tombstone_test.cpp \ test/tombstone_test.cpp \

View file

@ -55,6 +55,7 @@
#include "backtrace.h" #include "backtrace.h"
#include "getevent.h" #include "getevent.h"
#include "open_files_list.h"
#include "signal_sender.h" #include "signal_sender.h"
#include "tombstone.h" #include "tombstone.h"
#include "utility.h" #include "utility.h"
@ -480,7 +481,8 @@ static void ptrace_siblings(pid_t pid, pid_t main_tid, pid_t ignore_tid, std::se
} }
static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd, static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
BacktraceMap* backtrace_map, const std::set<pid_t>& siblings, BacktraceMap* backtrace_map, const OpenFilesList& open_files,
const std::set<pid_t>& siblings,
int* crash_signal, std::string* amfd_data) { int* crash_signal, std::string* amfd_data) {
if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) { if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno)); ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
@ -499,7 +501,8 @@ static bool perform_dump(const debugger_request_t& request, int fd, int tombston
case SIGSTOP: case SIGSTOP:
if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
ALOGV("debuggerd: stopped -- dumping to tombstone"); ALOGV("debuggerd: stopped -- dumping to tombstone");
engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, engrave_tombstone(tombstone_fd, backtrace_map, open_files,
request.pid, request.tid, siblings,
request.abort_msg_address, amfd_data); request.abort_msg_address, amfd_data);
} else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) { } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
ALOGV("debuggerd: stopped -- dumping to fd"); ALOGV("debuggerd: stopped -- dumping to fd");
@ -526,7 +529,8 @@ static bool perform_dump(const debugger_request_t& request, int fd, int tombston
case SIGTRAP: case SIGTRAP:
ALOGV("stopped -- fatal signal\n"); ALOGV("stopped -- fatal signal\n");
*crash_signal = signal; *crash_signal = signal;
engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings, engrave_tombstone(tombstone_fd, backtrace_map, open_files,
request.pid, request.tid, siblings,
request.abort_msg_address, amfd_data); request.abort_msg_address, amfd_data);
break; break;
@ -643,6 +647,10 @@ static void worker_process(int fd, debugger_request_t& request) {
// Generate the backtrace map before dropping privileges. // Generate the backtrace map before dropping privileges.
std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid)); std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
// Collect the list of open files before dropping privileges.
OpenFilesList open_files;
populate_open_files_list(request.pid, &open_files);
int amfd = -1; int amfd = -1;
std::unique_ptr<std::string> amfd_data; std::unique_ptr<std::string> amfd_data;
if (request.action == DEBUGGER_ACTION_CRASH) { if (request.action == DEBUGGER_ACTION_CRASH) {
@ -660,8 +668,8 @@ static void worker_process(int fd, debugger_request_t& request) {
} }
int crash_signal = SIGKILL; int crash_signal = SIGKILL;
succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings, succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), open_files,
&crash_signal, amfd_data.get()); siblings, &crash_signal, amfd_data.get());
if (succeeded) { if (succeeded) {
if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) { if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
if (!tombstone_path.empty()) { if (!tombstone_path.empty()) {

View file

@ -0,0 +1,69 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "DEBUG"
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <utility>
#include <vector>
#include <android-base/file.h>
#include <android/log.h>
#include "open_files_list.h"
#include "utility.h"
void populate_open_files_list(pid_t pid, OpenFilesList* list) {
std::string fd_dir_name = "/proc/" + std::to_string(pid) + "/fd";
std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(fd_dir_name.c_str()), closedir);
if (dir == nullptr) {
ALOGE("failed to open directory %s: %s", fd_dir_name.c_str(), strerror(errno));
return;
}
struct dirent* de;
while ((de = readdir(dir.get())) != nullptr) {
if (*de->d_name == '.') {
continue;
}
int fd = atoi(de->d_name);
std::string path = fd_dir_name + "/" + std::string(de->d_name);
std::string target;
if (android::base::Readlink(path, &target)) {
list->emplace_back(fd, target);
} else {
ALOGE("failed to readlink %s: %s", path.c_str(), strerror(errno));
list->emplace_back(fd, "???");
}
}
}
void dump_open_files_list_to_log(const OpenFilesList& files, log_t* log, const char* prefix) {
for (auto& file : files) {
_LOG(log, logtype::OPEN_FILES, "%sfd %i: %s\n", prefix, file.first, file.second.c_str());
}
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _DEBUGGERD_OPEN_FILES_LIST_H
#define _DEBUGGERD_OPEN_FILES_LIST_H
#include <sys/types.h>
#include <string>
#include <utility>
#include <vector>
#include "utility.h"
typedef std::vector<std::pair<int, std::string>> OpenFilesList;
/* Populates the given list with open files for the given process. */
void populate_open_files_list(pid_t pid, OpenFilesList* list);
/* Dumps the open files list to the log. */
void dump_open_files_list_to_log(const OpenFilesList& files, log_t* log, const char* prefix);
#endif // _DEBUGGERD_OPEN_FILES_LIST_H

View file

@ -0,0 +1,49 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include <gtest/gtest.h>
#include "android-base/test_utils.h"
#include "open_files_list.h"
// Check that we can produce a list of open files for the current process, and
// that it includes a known open file.
TEST(OpenFilesListTest, BasicTest) {
// Open a temporary file that we can check for in the list of open files.
TemporaryFile tf;
// Get the list of open files for this process.
OpenFilesList list;
populate_open_files_list(getpid(), &list);
// Verify our open file is in the list.
bool found = false;
for (auto& file : list) {
if (file.first == tf.fd) {
EXPECT_EQ(file.second, std::string(tf.path));
found = true;
break;
}
}
EXPECT_TRUE(found);
}

View file

@ -46,6 +46,7 @@
#include "backtrace.h" #include "backtrace.h"
#include "elf_utils.h" #include "elf_utils.h"
#include "machine.h" #include "machine.h"
#include "open_files_list.h"
#include "tombstone.h" #include "tombstone.h"
#define STACK_WORDS 16 #define STACK_WORDS 16
@ -620,7 +621,8 @@ static void dump_logs(log_t* log, pid_t pid, unsigned int tail) {
} }
// Dumps all information about the specified pid to the tombstone. // Dumps all information about the specified pid to the tombstone.
static void dump_crash(log_t* log, BacktraceMap* map, pid_t pid, pid_t tid, static void dump_crash(log_t* log, BacktraceMap* map,
const OpenFilesList& open_files, pid_t pid, pid_t tid,
const std::set<pid_t>& siblings, uintptr_t abort_msg_address) { const std::set<pid_t>& siblings, uintptr_t abort_msg_address) {
// don't copy log messages to tombstone unless this is a dev device // don't copy log messages to tombstone unless this is a dev device
bool want_logs = __android_log_is_debuggable(); bool want_logs = __android_log_is_debuggable();
@ -639,6 +641,9 @@ static void dump_crash(log_t* log, BacktraceMap* map, pid_t pid, pid_t tid,
} }
} }
_LOG(log, logtype::OPEN_FILES, "\nopen files:\n");
dump_open_files_list_to_log(open_files, log, " ");
if (want_logs) { if (want_logs) {
dump_logs(log, pid, 0); dump_logs(log, pid, 0);
} }
@ -697,7 +702,8 @@ int open_tombstone(std::string* out_path) {
return fd; return fd;
} }
void engrave_tombstone(int tombstone_fd, BacktraceMap* map, pid_t pid, pid_t tid, void engrave_tombstone(int tombstone_fd, BacktraceMap* map,
const OpenFilesList& open_files, pid_t pid, pid_t tid,
const std::set<pid_t>& siblings, uintptr_t abort_msg_address, const std::set<pid_t>& siblings, uintptr_t abort_msg_address,
std::string* amfd_data) { std::string* amfd_data) {
log_t log; log_t log;
@ -711,5 +717,5 @@ void engrave_tombstone(int tombstone_fd, BacktraceMap* map, pid_t pid, pid_t tid
log.tfd = tombstone_fd; log.tfd = tombstone_fd;
log.amfd_data = amfd_data; log.amfd_data = amfd_data;
dump_crash(&log, map, pid, tid, siblings, abort_msg_address); dump_crash(&log, map, open_files, pid, tid, siblings, abort_msg_address);
} }

View file

@ -32,7 +32,8 @@ class BacktraceMap;
int open_tombstone(std::string* path); int open_tombstone(std::string* path);
/* Creates a tombstone file and writes the crash dump to it. */ /* Creates a tombstone file and writes the crash dump to it. */
void engrave_tombstone(int tombstone_fd, BacktraceMap* map, pid_t pid, pid_t tid, void engrave_tombstone(int tombstone_fd, BacktraceMap* map,
const OpenFilesList& open_files, pid_t pid, pid_t tid,
const std::set<pid_t>& siblings, uintptr_t abort_msg_address, const std::set<pid_t>& siblings, uintptr_t abort_msg_address,
std::string* amfd_data); std::string* amfd_data);

View file

@ -70,7 +70,8 @@ enum logtype {
MAPS, MAPS,
MEMORY, MEMORY,
STACK, STACK,
LOGS LOGS,
OPEN_FILES
}; };
// Log information onto the tombstone. // Log information onto the tombstone.