2012-06-07 01:25:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 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.
|
|
|
|
*/
|
|
|
|
|
2015-05-15 00:39:52 +02:00
|
|
|
#define LOG_TAG "DEBUG"
|
|
|
|
|
2017-08-29 22:08:32 +02:00
|
|
|
#include "libdebuggerd/backtrace.h"
|
|
|
|
|
2016-09-29 00:54:45 +02:00
|
|
|
#include <dirent.h>
|
2020-09-24 00:51:46 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2016-09-29 00:54:45 +02:00
|
|
|
#include <limits.h>
|
2012-06-07 01:25:03 +02:00
|
|
|
#include <stddef.h>
|
2016-09-29 00:54:45 +02:00
|
|
|
#include <stdio.h>
|
2012-06-07 01:25:03 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-09-29 00:54:45 +02:00
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/types.h>
|
2012-06-07 01:25:03 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
#include <map>
|
2015-05-16 02:30:21 +02:00
|
|
|
#include <memory>
|
2016-05-04 01:32:13 +02:00
|
|
|
#include <string>
|
2015-05-16 02:30:21 +02:00
|
|
|
|
2021-03-30 06:53:42 +02:00
|
|
|
#include <android-base/strings.h>
|
2017-08-21 23:31:17 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2017-01-10 22:19:54 +01:00
|
|
|
#include <log/log.h>
|
2022-03-16 00:56:09 +01:00
|
|
|
#include <unwindstack/AndroidUnwinder.h>
|
2019-01-16 00:18:43 +01:00
|
|
|
#include <unwindstack/Unwinder.h>
|
2015-05-15 00:39:52 +02:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
#include "libdebuggerd/types.h"
|
2017-08-29 22:08:32 +02:00
|
|
|
#include "libdebuggerd/utility.h"
|
2020-07-24 00:26:10 +02:00
|
|
|
#include "util.h"
|
2012-06-07 01:25:03 +02:00
|
|
|
|
2021-03-30 06:53:42 +02:00
|
|
|
static void dump_process_header(log_t* log, pid_t pid,
|
|
|
|
const std::vector<std::string>& command_line) {
|
2020-07-24 00:26:10 +02:00
|
|
|
_LOG(log, logtype::BACKTRACE, "\n\n----- pid %d at %s -----\n", pid, get_timestamp().c_str());
|
2014-01-11 01:33:16 +01:00
|
|
|
|
2021-03-30 06:53:42 +02:00
|
|
|
if (!command_line.empty()) {
|
|
|
|
_LOG(log, logtype::BACKTRACE, "Cmd line: %s\n", android::base::Join(command_line, " ").c_str());
|
2014-01-11 01:33:16 +01:00
|
|
|
}
|
2014-09-11 00:47:49 +02:00
|
|
|
_LOG(log, logtype::BACKTRACE, "ABI: '%s'\n", ABI_STRING);
|
2012-06-07 01:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_process_footer(log_t* log, pid_t pid) {
|
2014-06-10 20:53:08 +02:00
|
|
|
_LOG(log, logtype::BACKTRACE, "\n----- end %d -----\n", pid);
|
2012-06-07 01:25:03 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 00:56:09 +01:00
|
|
|
void dump_backtrace_thread(int output_fd, unwindstack::AndroidUnwinder* unwinder,
|
2019-01-16 00:18:43 +01:00
|
|
|
const ThreadInfo& thread) {
|
2014-01-11 01:33:16 +01:00
|
|
|
log_t log;
|
2017-08-21 23:31:17 +02:00
|
|
|
log.tfd = output_fd;
|
|
|
|
log.amfd_data = nullptr;
|
2014-01-11 01:33:16 +01:00
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
_LOG(&log, logtype::BACKTRACE, "\n\"%s\" sysTid=%d\n", thread.thread_name.c_str(), thread.tid);
|
2014-01-11 01:33:16 +01:00
|
|
|
|
2022-03-16 00:56:09 +01:00
|
|
|
unwindstack::AndroidUnwinderData data;
|
|
|
|
if (!unwinder->Unwind(thread.registers.get(), data)) {
|
|
|
|
_LOG(&log, logtype::THREAD, "Unwind failed: tid = %d: Error %s\n", thread.tid,
|
|
|
|
data.GetErrorString().c_str());
|
2017-08-21 23:31:17 +02:00
|
|
|
return;
|
2014-01-11 01:33:16 +01:00
|
|
|
}
|
2012-06-07 01:25:03 +02:00
|
|
|
|
2022-03-16 00:56:09 +01:00
|
|
|
log_backtrace(&log, unwinder, data, " ");
|
2013-10-02 21:26:48 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 00:56:09 +01:00
|
|
|
void dump_backtrace(android::base::unique_fd output_fd, unwindstack::AndroidUnwinder* unwinder,
|
2017-08-21 23:31:17 +02:00
|
|
|
const std::map<pid_t, ThreadInfo>& thread_info, pid_t target_thread) {
|
2017-03-02 02:23:22 +01:00
|
|
|
log_t log;
|
2017-08-21 23:31:17 +02:00
|
|
|
log.tfd = output_fd.get();
|
2017-03-02 02:23:22 +01:00
|
|
|
log.amfd_data = nullptr;
|
|
|
|
|
2017-08-21 23:31:17 +02:00
|
|
|
auto target = thread_info.find(target_thread);
|
|
|
|
if (target == thread_info.end()) {
|
|
|
|
ALOGE("failed to find target thread in thread info");
|
|
|
|
return;
|
|
|
|
}
|
2017-03-02 02:23:22 +01:00
|
|
|
|
2021-03-30 06:53:42 +02:00
|
|
|
dump_process_header(&log, target->second.pid, target->second.command_line);
|
2017-08-21 23:31:17 +02:00
|
|
|
|
2019-01-16 00:18:43 +01:00
|
|
|
dump_backtrace_thread(output_fd.get(), unwinder, target->second);
|
2017-08-21 23:31:17 +02:00
|
|
|
for (const auto& [tid, info] : thread_info) {
|
|
|
|
if (tid != target_thread) {
|
2019-01-16 00:18:43 +01:00
|
|
|
dump_backtrace_thread(output_fd.get(), unwinder, info);
|
2017-08-21 23:31:17 +02:00
|
|
|
}
|
2017-03-02 02:23:22 +01:00
|
|
|
}
|
2017-08-21 23:31:17 +02:00
|
|
|
|
|
|
|
dump_process_footer(&log, target->second.pid);
|
2017-03-02 02:23:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void dump_backtrace_header(int output_fd) {
|
|
|
|
log_t log;
|
|
|
|
log.tfd = output_fd;
|
|
|
|
log.amfd_data = nullptr;
|
|
|
|
|
2020-07-24 00:26:10 +02:00
|
|
|
pid_t pid = getpid();
|
2021-03-30 06:53:42 +02:00
|
|
|
dump_process_header(&log, pid, get_command_line(pid));
|
2017-03-02 02:23:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void dump_backtrace_footer(int output_fd) {
|
|
|
|
log_t log;
|
|
|
|
log.tfd = output_fd;
|
|
|
|
log.amfd_data = nullptr;
|
|
|
|
|
|
|
|
dump_process_footer(&log, getpid());
|
|
|
|
}
|