platform_bionic/libc/malloc_debug/backtrace.cpp
Christopher Ferris 63860cb8fd Malloc debug rewrite.
The major components of the rewrite:

- Completely remove the qemu shared library code. Nobody was using it
  and it appears to have broken at some point.
- Adds the ability to enable/disable different options independently.
- Adds a new option that can enable the backtrace on alloc/free when
  a process gets a specific signal.
- Adds a new way to enable malloc debug. If a special property is
  set, and the process has an environment variable set, then debug
  malloc will be enabled. This allows something that might be
  a derivative of app_process to be started with an environment variable
  being enabled.
- get_malloc_leak_info() used to return one element for each pointer that
  had the exact same backtrace. The new version returns information for
  every one of the pointers with same backtrace. It turns out ddms already
  automatically coalesces these, so the old method simply hid the fact
  that there where multiple pointers with the same amount of backtrace.
- Moved all of the malloc debug specific code into the library.
  Nothing related to the malloc debug data structures remains in libc.
- Removed the calls to the debug malloc cleanup routine. Instead, I
  added an atexit call with the debug malloc cleanup routine. This gets
  around most problems related to the timing of doing the cleanup.

The new properties and environment variables:

libc.debug.malloc.options
  Set by option name (such as "backtrace"). Setting this to a bad value
  will cause a usage statement to be printed to the log.

libc.debug.malloc.program
  Same as before. If this is set, then only the program named will
  be launched with malloc debug enabled. This is not a complete match,
  but if any part of the property is in the program name, malloc debug is
  enabled.

libc.debug.malloc.env_enabled
  If set, then malloc debug is only enabled if the running process has the
  environment variable LIBC_DEBUG_MALLOC_ENABLE set.

Bug: 19145921

Change-Id: I7b0e58cc85cc6d4118173fe1f8627a391b64c0d7
2016-01-25 10:54:21 -08:00

179 lines
5.7 KiB
C++

/*
* 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>
#include <errno.h>
#include <inttypes.h>
#include <malloc.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <unwind.h>
#include "backtrace.h"
#include "debug_disable.h"
#include "debug_log.h"
#include "MapData.h"
#if defined(__LP64__)
#define PAD_PTR "016" PRIxPTR
#else
#define PAD_PTR "08" PRIxPTR
#endif
typedef struct _Unwind_Context __unwind_context;
extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
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;
}
void backtrace_startup() {
ScopedDisableDebugCalls disable;
g_map_data = MapData::Create();
if (g_map_data) {
_Unwind_Backtrace(find_current_map, nullptr);
}
}
void backtrace_shutdown() {
ScopedDisableDebugCalls disable;
delete g_map_data;
g_map_data = nullptr;
}
struct stack_crawl_state_t {
uintptr_t* frames;
size_t frame_count;
size_t cur_frame = 0;
stack_crawl_state_t(uintptr_t* frames, size_t frame_count)
: frames(frames), frame_count(frame_count) {}
};
static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
uintptr_t ip = _Unwind_GetIP(context);
// The instruction pointer is pointing at the instruction after the return
// call on all architectures.
// Modify the pc to point at the real function.
if (ip != 0) {
#if defined(__arm__)
// 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;
}
}
#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--;
#endif
// 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;
}
}
state->frames[state->cur_frame++] = ip;
return (state->cur_frame >= state->frame_count) ? _URC_END_OF_STACK : _URC_NO_REASON;
}
size_t backtrace_get(uintptr_t* frames, size_t frame_count) {
ScopedDisableDebugCalls disable;
stack_crawl_state_t state(frames, frame_count);
_Unwind_Backtrace(trace_function, &state);
return state.cur_frame;
}
void backtrace_log(uintptr_t* frames, size_t frame_count) {
ScopedDisableDebugCalls disable;
for (size_t frame_num = 0; frame_num < frame_count; frame_num++) {
uintptr_t offset = 0;
const char* symbol = nullptr;
Dl_info info;
if (dladdr(reinterpret_cast<void*>(frames[frame_num]), &info) != 0) {
offset = reinterpret_cast<uintptr_t>(info.dli_saddr);
symbol = info.dli_sname;
}
uintptr_t rel_pc = offset;
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) {
soname = "<unknown>";
}
if (symbol != nullptr) {
char* demangled_symbol = __cxa_demangle(symbol, nullptr, nullptr, nullptr);
const char* best_name = (demangled_symbol != nullptr) ? demangled_symbol : symbol;
error_log(" #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")",
frame_num, rel_pc, soname, best_name, frames[frame_num] - offset);
free(demangled_symbol);
} else {
error_log(" #%02zd pc %" PAD_PTR " %s", frame_num, rel_pc, soname);
}
}
}