android_system_core/include/corkscrew/backtrace.h

103 lines
3.6 KiB
C
Raw Normal View History

/*
* Copyright (C) 2011 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.
*/
/* A stack unwinder. */
#ifndef _CORKSCREW_BACKTRACE_H
#define _CORKSCREW_BACKTRACE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
#include <corkscrew/ptrace.h>
#include <corkscrew/map_info.h>
#include <corkscrew/symbol_table.h>
/*
* Describes a single frame of a backtrace.
*/
typedef struct {
uintptr_t absolute_pc; /* absolute PC offset */
uintptr_t stack_top; /* top of stack for this frame */
size_t stack_size; /* size of this stack frame */
} backtrace_frame_t;
/*
* Describes the symbols associated with a backtrace frame.
*/
typedef struct {
uintptr_t relative_pc; /* relative PC offset from the start of the library,
or the absolute PC if the library is unknown */
Improve stack unwinder robustness. Keep track of whether memory maps are readable. Use the information in try_get_word to try to avoid accidentally dereferencing an invalid pointer within the current process. (Note that I haven't ever seen that happen during normal unwinding, but it pays to be a little more careful.) Refactored try_get_word a little to make it easier to pass it the needed state for validation checks by way of a little memory_t struct. Improved how the memory map for the current process is cached. This is important because we need up to date information about readable maps. Use a 5 second cache expiration. Improved the PC -> LR fallback logic in the unwinder so we can eke out an extra frame sometimes. Fixed a bug reading ELF program headers. The phnum & phentsize fields are half-words. We were incorrectly interpreting phnum as a whole word. Used android_atomic_* operations carefully in the unwinder to prevent possible memory races between the dumper and the dumpee. This was highly unlikely (or even impossible due to the presence of other barriers along the way) but the code is clearer now about its invariants. Fixed a bug in debuggerd where the pid was being passed to have its stack dump taken instead of the tid, resulting in short stacks because ptrace couldn't read the data if pid != tid. Did a full sweep to ensure that we use pid / tid correctly everywhere. Ported old code from debuggerd to rewind the program counter back one instruction so that it points to the branch instruction itself instead of the return address. Change-Id: Icc4eb08320052975a4ae7f0f5f0ac9308a2d33d7
2011-11-04 00:58:44 +00:00
char* map_name; /* executable or library name, or NULL if unknown */
char* name; /* symbol name, or NULL if unknown */
char* demangled_name; /* demangled symbol name, or NULL if unknown */
} backtrace_symbol_t;
/*
* Unwinds the call stack for the current thread of execution.
* Populates the backtrace array with the program counters from the call stack.
* Returns the number of frames collected, or -1 if an error occurred.
*/
ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
/*
* Unwinds the call stack for a thread within this process.
* Populates the backtrace array with the program counters from the call stack.
* Returns the number of frames collected, or -1 if an error occurred.
*
* The task is briefly suspended while the backtrace is being collected.
*/
ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
size_t ignore_depth, size_t max_depth);
/*
* Unwinds the call stack of a task within a remote process using ptrace().
* Populates the backtrace array with the program counters from the call stack.
* Returns the number of frames collected, or -1 if an error occurred.
*/
ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
/*
* Gets the symbols for each frame of a backtrace.
* The symbols array must be big enough to hold one symbol record per frame.
* The symbols must later be freed using free_backtrace_symbols.
*/
void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
backtrace_symbol_t* backtrace_symbols);
/*
* Gets the symbols for each frame of a backtrace from a remote process.
* The symbols array must be big enough to hold one symbol record per frame.
* The symbols must later be freed using free_backtrace_symbols.
*/
void get_backtrace_symbols_ptrace(const ptrace_context_t* context,
const backtrace_frame_t* backtrace, size_t frames,
backtrace_symbol_t* backtrace_symbols);
/*
* Frees the storage associated with backtrace symbols.
*/
void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames);
#ifdef __cplusplus
}
#endif
#endif // _CORKSCREW_BACKTRACE_H