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>
|
|
|
|
#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>
|
2016-10-19 03:17:52 +02:00
|
|
|
#include <debuggerd/util.h>
|
|
|
|
#include <selinux/selinux.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) {
|
|
|
|
fprintf(stderr, "usage: debuggerd [-b] 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");
|
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);
|
2017-03-03 04:01:20 +01:00
|
|
|
if (argc == 3 && strcmp(argv[1], "-b") != 0 && strcmp(argv[1], "--backtrace") != 0) usage(1);
|
|
|
|
bool backtrace_only = argc == 3;
|
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
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
if (!debuggerd_trigger_dump(pid, std::move(pipewrite),
|
2017-03-03 04:01:20 +01:00
|
|
|
backtrace_only ? kDebuggerdBacktrace : kDebuggerdTombstone, 0)) {
|
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
|
|
|
}
|