2013-01-18 03:36:06 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
2015-11-17 02:30:32 +01:00
|
|
|
#include <errno.h>
|
2013-10-09 02:02:26 +02:00
|
|
|
#include <inttypes.h>
|
2015-01-29 03:02:33 +01:00
|
|
|
#include <malloc.h>
|
2015-11-17 02:30:32 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2013-01-18 03:36:06 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <unwind.h>
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
#include "backtrace.h"
|
|
|
|
#include "debug_disable.h"
|
|
|
|
#include "debug_log.h"
|
|
|
|
#include "MapData.h"
|
2013-01-18 03:36:06 +01:00
|
|
|
|
2014-02-26 00:32:01 +01:00
|
|
|
#if defined(__LP64__)
|
|
|
|
#define PAD_PTR "016" PRIxPTR
|
|
|
|
#else
|
|
|
|
#define PAD_PTR "08" PRIxPTR
|
|
|
|
#endif
|
|
|
|
|
2013-01-18 03:36:06 +01:00
|
|
|
typedef struct _Unwind_Context __unwind_context;
|
|
|
|
|
2015-02-05 21:05:34 +01:00
|
|
|
extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
static MapData* g_map_data = nullptr;
|
|
|
|
static const MapEntry* g_current_code_map = nullptr;
|
|
|
|
|
|
|
|
static _Unwind_Reason_Code find_current_map(__unwind_context* context, void*) {
|
|
|
|
uintptr_t ip = _Unwind_GetIP(context);
|
|
|
|
|
|
|
|
if (ip == 0) {
|
|
|
|
return _URC_END_OF_STACK;
|
|
|
|
}
|
|
|
|
g_current_code_map = g_map_data->find(ip);
|
|
|
|
return _URC_END_OF_STACK;
|
|
|
|
}
|
2013-01-29 01:27:36 +01:00
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
void backtrace_startup() {
|
2014-07-25 02:52:23 +02:00
|
|
|
ScopedDisableDebugCalls disable;
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
g_map_data = MapData::Create();
|
|
|
|
if (g_map_data) {
|
|
|
|
_Unwind_Backtrace(find_current_map, nullptr);
|
|
|
|
}
|
2013-01-29 01:27:36 +01:00
|
|
|
}
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
void backtrace_shutdown() {
|
2014-07-25 02:52:23 +02:00
|
|
|
ScopedDisableDebugCalls disable;
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
delete g_map_data;
|
|
|
|
g_map_data = nullptr;
|
2013-01-29 01:27:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct stack_crawl_state_t {
|
|
|
|
uintptr_t* frames;
|
|
|
|
size_t frame_count;
|
2015-11-17 02:30:32 +01:00
|
|
|
size_t cur_frame = 0;
|
2013-01-29 01:27:36 +01:00
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
stack_crawl_state_t(uintptr_t* frames, size_t frame_count)
|
|
|
|
: frames(frames), frame_count(frame_count) {}
|
2013-01-29 01:27:36 +01:00
|
|
|
};
|
|
|
|
|
2013-01-18 03:36:06 +01:00
|
|
|
static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
|
|
|
|
stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
|
2013-01-29 01:27:36 +01:00
|
|
|
|
|
|
|
uintptr_t ip = _Unwind_GetIP(context);
|
|
|
|
|
2015-08-19 00:41:31 +02:00
|
|
|
// The instruction pointer is pointing at the instruction after the return
|
|
|
|
// call on all architectures.
|
|
|
|
// Modify the pc to point at the real function.
|
2013-05-07 23:22:43 +02:00
|
|
|
if (ip != 0) {
|
2015-08-19 00:41:31 +02:00
|
|
|
#if defined(__arm__)
|
2015-08-26 05:48:46 +02:00
|
|
|
// If the ip is suspiciously low, do nothing to avoid a segfault trying
|
|
|
|
// to access this memory.
|
|
|
|
if (ip >= 4096) {
|
|
|
|
// Check bits [15:11] of the first halfword assuming the instruction
|
|
|
|
// is 32 bits long. If the bits are any of these values, then our
|
|
|
|
// assumption was correct:
|
|
|
|
// b11101
|
|
|
|
// b11110
|
|
|
|
// b11111
|
|
|
|
// Otherwise, this is a 16 bit instruction.
|
|
|
|
uint16_t value = (*reinterpret_cast<uint16_t*>(ip - 2)) >> 11;
|
|
|
|
if (value == 0x1f || value == 0x1e || value == 0x1d) {
|
|
|
|
ip -= 4;
|
|
|
|
} else {
|
|
|
|
ip -= 2;
|
|
|
|
}
|
2013-05-07 23:22:43 +02:00
|
|
|
}
|
2015-08-19 00:41:31 +02:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
// All instructions are 4 bytes long, skip back one instruction.
|
|
|
|
ip -= 4;
|
|
|
|
#elif defined(__i386__) || defined(__x86_64__)
|
|
|
|
// It's difficult to decode exactly where the previous instruction is,
|
|
|
|
// so subtract 1 to estimate where the instruction lives.
|
|
|
|
ip--;
|
2013-05-07 23:22:43 +02:00
|
|
|
#endif
|
2015-11-17 02:30:32 +01:00
|
|
|
|
|
|
|
// Do not record the frames that fall in our own shared library.
|
|
|
|
if (g_current_code_map && (ip >= g_current_code_map->start) && ip < g_current_code_map->end) {
|
|
|
|
return _URC_NO_REASON;
|
|
|
|
}
|
2015-08-19 00:41:31 +02:00
|
|
|
}
|
2013-05-07 23:22:43 +02:00
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
state->frames[state->cur_frame++] = ip;
|
|
|
|
return (state->cur_frame >= state->frame_count) ? _URC_END_OF_STACK : _URC_NO_REASON;
|
2013-01-18 03:36:06 +01:00
|
|
|
}
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
size_t backtrace_get(uintptr_t* frames, size_t frame_count) {
|
2014-07-25 02:52:23 +02:00
|
|
|
ScopedDisableDebugCalls disable;
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
stack_crawl_state_t state(frames, frame_count);
|
2013-01-18 03:36:06 +01:00
|
|
|
_Unwind_Backtrace(trace_function, &state);
|
2015-11-17 02:30:32 +01:00
|
|
|
return state.cur_frame;
|
2013-01-18 03:36:06 +01:00
|
|
|
}
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
void backtrace_log(uintptr_t* frames, size_t frame_count) {
|
2014-07-25 02:52:23 +02:00
|
|
|
ScopedDisableDebugCalls disable;
|
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
for (size_t frame_num = 0; frame_num < frame_count; frame_num++) {
|
2013-10-09 02:02:26 +02:00
|
|
|
uintptr_t offset = 0;
|
2015-11-17 02:30:32 +01:00
|
|
|
const char* symbol = nullptr;
|
2013-01-18 03:36:06 +01:00
|
|
|
|
|
|
|
Dl_info info;
|
2015-11-17 02:30:32 +01:00
|
|
|
if (dladdr(reinterpret_cast<void*>(frames[frame_num]), &info) != 0) {
|
2013-10-09 02:02:26 +02:00
|
|
|
offset = reinterpret_cast<uintptr_t>(info.dli_saddr);
|
2013-01-18 03:36:06 +01:00
|
|
|
symbol = info.dli_sname;
|
|
|
|
}
|
|
|
|
|
2014-07-25 02:52:23 +02:00
|
|
|
uintptr_t rel_pc = offset;
|
2015-11-17 02:30:32 +01:00
|
|
|
const MapEntry* entry = nullptr;
|
|
|
|
if (g_map_data) {
|
|
|
|
entry = g_map_data->find(frames[frame_num], &rel_pc);
|
|
|
|
}
|
|
|
|
const char* soname = (entry != nullptr) ? entry->name.c_str() : info.dli_fname;
|
|
|
|
if (soname == nullptr) {
|
2013-01-29 01:27:36 +01:00
|
|
|
soname = "<unknown>";
|
|
|
|
}
|
2015-11-17 02:30:32 +01:00
|
|
|
if (symbol != nullptr) {
|
|
|
|
char* demangled_symbol = __cxa_demangle(symbol, nullptr, nullptr, nullptr);
|
|
|
|
const char* best_name = (demangled_symbol != nullptr) ? demangled_symbol : symbol;
|
2013-01-29 01:27:36 +01:00
|
|
|
|
2015-11-17 02:30:32 +01:00
|
|
|
error_log(" #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")",
|
|
|
|
frame_num, rel_pc, soname, best_name, frames[frame_num] - offset);
|
2013-01-29 01:27:36 +01:00
|
|
|
free(demangled_symbol);
|
|
|
|
} else {
|
2015-11-17 02:30:32 +01:00
|
|
|
error_log(" #%02zd pc %" PAD_PTR " %s", frame_num, rel_pc, soname);
|
2013-01-18 03:36:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|