2014-01-11 01:33:16 +01:00
|
|
|
/*
|
2016-10-19 03:17:52 +02:00
|
|
|
* Copyright 2016, The Android Open Source Project
|
2014-01-11 01:33:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <err.h>
|
2016-01-14 02:57:14 +01:00
|
|
|
#include <stdio.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-01-14 02:57:14 +01:00
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <limits>
|
2018-06-15 00:03:12 +02:00
|
|
|
#include <string_view>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <thread>
|
2014-07-24 21:23:05 +02:00
|
|
|
|
2016-05-04 01:32:13 +02:00
|
|
|
#include <android-base/file.h>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/parseint.h>
|
2016-03-23 04:03:13 +01:00
|
|
|
#include <android-base/unique_fd.h>
|
2016-08-11 21:50:32 +02:00
|
|
|
#include <debuggerd/client.h>
|
2017-06-27 23:08:05 +02:00
|
|
|
#include <procinfo/process.h>
|
2017-05-10 11:58:59 +02:00
|
|
|
#include "util.h"
|
2016-08-11 21:50:32 +02:00
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
using android::base::unique_fd;
|
2014-01-11 01:33:16 +01:00
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
static void usage(int exit_code) {
|
2018-06-15 00:03:12 +02:00
|
|
|
fprintf(stderr, "usage: debuggerd [-bj] PID\n");
|
2017-03-03 04:01:20 +01:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "-b, --backtrace just a backtrace rather than a full tombstone\n");
|
2018-06-15 00:03:12 +02:00
|
|
|
fprintf(stderr, "-j collect java traces\n");
|
2016-10-19 03:17:52 +02:00
|
|
|
_exit(exit_code);
|
2011-10-24 20:10:16 +02:00
|
|
|
}
|
2011-10-21 21:14:56 +02:00
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
static std::thread spawn_redirect_thread(unique_fd fd) {
|
|
|
|
return std::thread([fd{ std::move(fd) }]() {
|
|
|
|
while (true) {
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, sizeof(buf)));
|
|
|
|
if (rc <= 0) {
|
|
|
|
return;
|
2015-01-15 23:47:36 +01:00
|
|
|
}
|
2016-01-14 02:57:14 +01:00
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
if (!android::base::WriteFully(STDOUT_FILENO, buf, rc)) {
|
|
|
|
return;
|
2016-03-23 22:02:52 +01:00
|
|
|
}
|
2016-03-17 04:19:44 +01:00
|
|
|
}
|
2016-10-19 03:17:52 +02:00
|
|
|
});
|
2016-03-17 04:19:44 +01:00
|
|
|
}
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
if (argc <= 1) usage(0);
|
|
|
|
if (argc > 3) usage(1);
|
2018-06-15 00:03:12 +02:00
|
|
|
|
|
|
|
DebuggerdDumpType dump_type = kDebuggerdTombstone;
|
|
|
|
|
|
|
|
if (argc == 3) {
|
|
|
|
std::string_view flag = argv[1];
|
|
|
|
if (flag == "-b" || flag == "--backtrace") {
|
|
|
|
dump_type = kDebuggerdNativeBacktrace;
|
|
|
|
} else if (flag == "-j") {
|
|
|
|
dump_type = kDebuggerdJavaBacktrace;
|
|
|
|
} else {
|
|
|
|
usage(1);
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 04:19:44 +01:00
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
pid_t pid;
|
|
|
|
if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) {
|
|
|
|
usage(1);
|
2016-03-17 04:19:44 +01:00
|
|
|
}
|
|
|
|
|
2017-06-27 23:08:05 +02:00
|
|
|
if (getuid() != 0) {
|
|
|
|
errx(1, "root is required");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if the process exists and that we can actually send a signal to it.
|
|
|
|
android::procinfo::ProcessInfo proc_info;
|
|
|
|
if (!android::procinfo::GetProcessInfo(pid, &proc_info)) {
|
|
|
|
err(1, "failed to fetch info for process %d", pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proc_info.state == android::procinfo::kProcessStateZombie) {
|
|
|
|
errx(1, "process %d is a zombie", pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kill(pid, 0) != 0) {
|
|
|
|
err(1, "cannot send signal to process %d", pid);
|
|
|
|
}
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
unique_fd piperead, pipewrite;
|
|
|
|
if (!Pipe(&piperead, &pipewrite)) {
|
|
|
|
err(1, "failed to create pipe");
|
2016-03-17 04:19:44 +01:00
|
|
|
}
|
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
std::thread redirect_thread = spawn_redirect_thread(std::move(piperead));
|
2018-06-15 00:03:12 +02:00
|
|
|
if (!debuggerd_trigger_dump(pid, dump_type, 0, std::move(pipewrite))) {
|
2016-10-19 03:17:52 +02:00
|
|
|
redirect_thread.join();
|
|
|
|
errx(1, "failed to dump process %d", pid);
|
2016-03-17 04:19:44 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-10-19 03:17:52 +02:00
|
|
|
redirect_thread.join();
|
2014-01-11 01:33:16 +01:00
|
|
|
return 0;
|
2011-10-24 20:10:16 +02:00
|
|
|
}
|