2019-07-29 22:41:33 +01:00
|
|
|
// Copyright 2019 - Omar Sandoval
|
|
|
|
// SPDX-License-Identifier: GPL-3.0+
|
|
|
|
|
2019-10-18 10:42:51 +01:00
|
|
|
#include <dwarf.h>
|
2019-07-29 22:41:33 +01:00
|
|
|
#include <elfutils/libdwfl.h>
|
|
|
|
#include <endian.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "program.h"
|
|
|
|
#include "string_builder.h"
|
|
|
|
#include "symbol.h"
|
|
|
|
|
|
|
|
struct drgn_stack_trace {
|
2019-10-18 10:42:02 +01:00
|
|
|
struct drgn_program *prog;
|
|
|
|
union {
|
|
|
|
size_t capacity;
|
|
|
|
Dwfl_Thread *thread;
|
|
|
|
};
|
2019-07-29 22:41:33 +01:00
|
|
|
size_t num_frames;
|
2019-10-18 10:42:02 +01:00
|
|
|
Dwfl_Frame *frames[];
|
2019-07-29 22:41:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC void drgn_stack_trace_destroy(struct drgn_stack_trace *trace)
|
|
|
|
{
|
2019-10-18 10:42:02 +01:00
|
|
|
dwfl_detach_thread(trace->thread);
|
2019-07-29 22:41:33 +01:00
|
|
|
free(trace);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC
|
|
|
|
size_t drgn_stack_trace_num_frames(struct drgn_stack_trace *trace)
|
|
|
|
{
|
|
|
|
return trace->num_frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
|
|
|
drgn_pretty_print_stack_trace(struct drgn_stack_trace *trace, char **ret)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
struct string_builder str = {};
|
2019-10-18 10:42:02 +01:00
|
|
|
struct drgn_stack_frame frame = { .trace = trace, };
|
2019-07-29 22:41:33 +01:00
|
|
|
|
2019-10-18 10:42:02 +01:00
|
|
|
for (; frame.i < trace->num_frames; frame.i++) {
|
2019-07-29 22:41:33 +01:00
|
|
|
uint64_t pc;
|
2019-10-18 10:42:02 +01:00
|
|
|
Dwfl_Module *module;
|
2019-07-29 22:41:33 +01:00
|
|
|
struct drgn_symbol sym;
|
|
|
|
|
|
|
|
pc = drgn_stack_frame_pc(frame);
|
2019-10-18 10:42:02 +01:00
|
|
|
module = dwfl_frame_module(trace->frames[frame.i]);
|
|
|
|
if (module) {
|
|
|
|
err = drgn_program_find_symbol_internal(trace->prog,
|
|
|
|
module, pc,
|
|
|
|
&sym);
|
|
|
|
} else {
|
|
|
|
err = &drgn_not_found;
|
|
|
|
}
|
2019-07-29 22:41:33 +01:00
|
|
|
if (err && err != &drgn_not_found)
|
|
|
|
goto err;
|
2019-10-18 10:42:02 +01:00
|
|
|
if (!string_builder_appendf(&str, "#%-2zu ", frame.i)) {
|
2019-07-29 22:41:33 +01:00
|
|
|
err = &drgn_enomem;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
if (!string_builder_appendf(&str, "0x%" PRIx64, pc)) {
|
|
|
|
err = &drgn_enomem;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!string_builder_appendf(&str,
|
|
|
|
"%s+0x%" PRIx64 "/0x%" PRIx64,
|
|
|
|
sym.name, pc - sym.address,
|
|
|
|
sym.size)) {
|
|
|
|
err = &drgn_enomem;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 10:42:02 +01:00
|
|
|
if (frame.i != trace->num_frames - 1 &&
|
2019-07-29 22:41:33 +01:00
|
|
|
!string_builder_appendc(&str, '\n')) {
|
|
|
|
err = &drgn_enomem;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!string_builder_finalize(&str, ret)) {
|
|
|
|
err = &drgn_enomem;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
err:
|
|
|
|
free(str.str);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-10-18 10:42:02 +01:00
|
|
|
LIBDRGN_PUBLIC uint64_t drgn_stack_frame_pc(struct drgn_stack_frame frame)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
2019-10-18 10:42:02 +01:00
|
|
|
Dwarf_Addr pc;
|
|
|
|
|
|
|
|
dwfl_frame_pc(frame.trace->frames[frame.i], &pc, NULL);
|
|
|
|
return pc;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
2019-10-18 10:42:02 +01:00
|
|
|
drgn_stack_frame_symbol(struct drgn_stack_frame frame, struct drgn_symbol **ret)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
2019-10-18 10:42:02 +01:00
|
|
|
Dwfl_Module *module;
|
|
|
|
|
|
|
|
module = dwfl_frame_module(frame.trace->frames[frame.i]);
|
|
|
|
return drgn_program_find_symbol_in_module(frame.trace->prog, module,
|
|
|
|
drgn_stack_frame_pc(frame),
|
|
|
|
ret);
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
2019-10-18 10:42:51 +01:00
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
|
|
|
drgn_stack_frame_register(struct drgn_stack_frame frame,
|
|
|
|
enum drgn_register_number regno, uint64_t *ret)
|
|
|
|
{
|
|
|
|
const Dwarf_Op op = { .atom = DW_OP_regx, .number = regno, };
|
|
|
|
Dwarf_Addr value;
|
|
|
|
|
|
|
|
if (!dwfl_frame_eval_expr(frame.trace->frames[frame.i], &op, 1, &value))
|
|
|
|
return drgn_error_libdwfl();
|
|
|
|
*ret = value;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
|
|
|
drgn_stack_frame_register_by_name(struct drgn_stack_frame frame,
|
|
|
|
const char *name, uint64_t *ret)
|
|
|
|
{
|
|
|
|
const struct drgn_register *reg;
|
|
|
|
|
|
|
|
reg = drgn_architecture_register_by_name(frame.trace->prog->platform.arch,
|
|
|
|
name);
|
|
|
|
if (!reg) {
|
|
|
|
return drgn_error_format(DRGN_ERROR_LOOKUP,
|
|
|
|
"unknown register '%s'", name);
|
|
|
|
}
|
|
|
|
return drgn_stack_frame_register(frame, reg->number, ret);
|
|
|
|
}
|
|
|
|
|
2019-07-29 22:41:33 +01:00
|
|
|
static bool drgn_thread_memory_read(Dwfl *dwfl, Dwarf_Addr addr,
|
|
|
|
Dwarf_Word *result, void *dwfl_arg)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
struct drgn_program *prog = dwfl_arg;
|
|
|
|
bool is_little_endian = drgn_program_is_little_endian(prog);
|
|
|
|
|
|
|
|
if (drgn_program_is_64_bit(prog)) {
|
|
|
|
uint64_t u64;
|
|
|
|
|
|
|
|
err = drgn_program_read_memory(prog, &u64, addr, sizeof(u64),
|
|
|
|
false);
|
|
|
|
if (err)
|
|
|
|
goto err;
|
|
|
|
*result = is_little_endian ? le64toh(u64) : be64toh(u64);
|
|
|
|
} else {
|
|
|
|
uint32_t u32;
|
|
|
|
|
|
|
|
err = drgn_program_read_memory(prog, &u32, addr, sizeof(u32),
|
|
|
|
false);
|
|
|
|
if (err)
|
|
|
|
goto err;
|
|
|
|
*result = is_little_endian ? le32toh(u32) : be32toh(u32);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (err->code == DRGN_ERROR_FAULT) {
|
|
|
|
/*
|
|
|
|
* This could be the end of the stack trace, so it shouldn't be
|
|
|
|
* fatal.
|
|
|
|
*/
|
|
|
|
drgn_error_destroy(err);
|
|
|
|
} else {
|
|
|
|
drgn_error_destroy(prog->stack_trace_err);
|
|
|
|
prog->stack_trace_err = err;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For drgn_object_stack_trace(), we only care about the thread
|
2019-10-18 10:42:02 +01:00
|
|
|
* prog->stack_trace_obj. We return it with an arbitrary TID.
|
2019-07-29 22:41:33 +01:00
|
|
|
*/
|
2019-10-18 10:42:02 +01:00
|
|
|
#define STACK_TRACE_OBJ_TID 1
|
2019-07-29 22:41:33 +01:00
|
|
|
static pid_t drgn_object_stack_trace_next_thread(Dwfl *dwfl, void *dwfl_arg,
|
|
|
|
void **thread_argp)
|
|
|
|
{
|
|
|
|
struct drgn_program *prog = dwfl_arg;
|
|
|
|
|
|
|
|
if (*thread_argp || !prog->stack_trace_obj)
|
|
|
|
return 0;
|
|
|
|
*thread_argp = (void *)prog->stack_trace_obj;
|
2019-10-18 10:42:02 +01:00
|
|
|
return STACK_TRACE_OBJ_TID;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool drgn_linux_kernel_set_initial_registers(Dwfl_Thread *thread,
|
|
|
|
void *thread_arg)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
struct drgn_object *task_obj = thread_arg;
|
|
|
|
struct drgn_program *prog = task_obj->prog;
|
|
|
|
|
|
|
|
err = prog->platform.arch->linux_kernel_set_initial_registers(thread,
|
|
|
|
task_obj);
|
|
|
|
if (err) {
|
|
|
|
drgn_error_destroy(prog->stack_trace_err);
|
|
|
|
prog->stack_trace_err = err;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-18 10:42:02 +01:00
|
|
|
static int drgn_append_stack_frame(Dwfl_Frame *state, void *arg)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
2019-10-18 10:42:02 +01:00
|
|
|
struct drgn_stack_trace **tracep = arg;
|
|
|
|
struct drgn_stack_trace *trace = *tracep;
|
2019-07-29 22:41:33 +01:00
|
|
|
|
2019-10-18 10:42:02 +01:00
|
|
|
if (trace->num_frames >= trace->capacity) {
|
|
|
|
struct drgn_stack_trace *tmp;
|
2019-07-29 22:41:33 +01:00
|
|
|
size_t new_capacity, bytes;
|
|
|
|
|
2019-10-18 10:42:02 +01:00
|
|
|
if (__builtin_mul_overflow(2U, trace->capacity,
|
2019-07-29 22:41:33 +01:00
|
|
|
&new_capacity) ||
|
|
|
|
__builtin_mul_overflow(new_capacity,
|
|
|
|
sizeof(trace->frames[0]), &bytes) ||
|
|
|
|
__builtin_add_overflow(bytes, sizeof(*trace), &bytes) ||
|
2019-10-18 10:42:02 +01:00
|
|
|
!(tmp = realloc(trace, bytes))) {
|
|
|
|
drgn_error_destroy(trace->prog->stack_trace_err);
|
|
|
|
trace->prog->stack_trace_err = &drgn_enomem;
|
|
|
|
return DWARF_CB_ABORT;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
2019-10-18 10:42:02 +01:00
|
|
|
*tracep = trace = tmp;
|
|
|
|
trace->capacity = new_capacity;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
2019-10-18 10:42:02 +01:00
|
|
|
trace->frames[trace->num_frames++] = state;
|
2019-07-29 22:41:33 +01:00
|
|
|
return DWARF_CB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Dwfl_Thread_Callbacks drgn_linux_kernel_thread_callbacks = {
|
|
|
|
.next_thread = drgn_object_stack_trace_next_thread,
|
|
|
|
.memory_read = drgn_thread_memory_read,
|
|
|
|
.set_initial_registers = drgn_linux_kernel_set_initial_registers,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct drgn_error *drgn_object_stack_trace(const struct drgn_object *obj,
|
|
|
|
struct drgn_stack_trace **ret)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
struct drgn_program *prog = obj->prog;
|
|
|
|
Dwfl *dwfl;
|
2019-10-18 10:42:02 +01:00
|
|
|
Dwfl_Thread *thread;
|
2019-07-29 22:41:33 +01:00
|
|
|
struct drgn_stack_trace *trace;
|
|
|
|
|
|
|
|
if (!prog->has_platform) {
|
|
|
|
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
|
|
|
|
"cannot unwind stack without platform");
|
|
|
|
}
|
|
|
|
if (!(prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL)) {
|
|
|
|
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
|
|
|
|
"stack unwinding is currently only supported for the Linux kernel");
|
|
|
|
}
|
|
|
|
if (!prog->platform.arch->linux_kernel_set_initial_registers) {
|
|
|
|
return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
|
|
|
|
"stack unwinding is not supported for %s architecture",
|
|
|
|
prog->platform.arch->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = drgn_program_get_dwfl(prog, &dwfl);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (!prog->attached_dwfl_state) {
|
|
|
|
if (!dwfl_attach_state(dwfl, NULL, 0,
|
|
|
|
&drgn_linux_kernel_thread_callbacks,
|
|
|
|
prog))
|
|
|
|
return drgn_error_libdwfl();
|
|
|
|
prog->attached_dwfl_state = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
prog->stack_trace_obj = obj;
|
2019-10-18 10:42:02 +01:00
|
|
|
thread = dwfl_attach_thread(dwfl, STACK_TRACE_OBJ_TID);
|
2019-07-29 22:41:33 +01:00
|
|
|
prog->stack_trace_obj = NULL;
|
2019-10-18 10:42:02 +01:00
|
|
|
if (prog->stack_trace_err)
|
|
|
|
goto stack_trace_err;
|
|
|
|
if (!thread) {
|
|
|
|
err = drgn_error_libdwfl();
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace = malloc(sizeof(*trace) + sizeof(trace->frames[0]));
|
|
|
|
if (!trace) {
|
|
|
|
err = &drgn_enomem;
|
2019-07-29 22:41:33 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2019-10-18 10:42:02 +01:00
|
|
|
trace->prog = prog;
|
|
|
|
trace->capacity = 1;
|
|
|
|
trace->num_frames = 0;
|
|
|
|
|
|
|
|
dwfl_thread_getframes(thread, drgn_append_stack_frame, &trace);
|
|
|
|
if (prog->stack_trace_err) {
|
|
|
|
free(trace);
|
|
|
|
goto stack_trace_err;
|
|
|
|
}
|
2019-07-29 22:41:33 +01:00
|
|
|
|
|
|
|
/* Shrink the trace to fit if we can, but don't fail if we can't. */
|
2019-10-18 10:42:02 +01:00
|
|
|
if (trace->capacity > trace->num_frames) {
|
|
|
|
struct drgn_stack_trace *tmp;
|
|
|
|
|
|
|
|
tmp = realloc(trace,
|
|
|
|
sizeof(*trace) +
|
|
|
|
trace->num_frames * sizeof(trace->frames[0]));
|
|
|
|
if (tmp)
|
|
|
|
trace = tmp;
|
|
|
|
}
|
|
|
|
trace->thread = thread;
|
2019-07-29 22:41:33 +01:00
|
|
|
*ret = trace;
|
|
|
|
return NULL;
|
|
|
|
|
2019-10-18 10:42:02 +01:00
|
|
|
stack_trace_err:
|
|
|
|
/*
|
|
|
|
* The error reporting for dwfl_getthread_frames() is not great. The
|
|
|
|
* documentation says that some of its unwinder implementations always
|
|
|
|
* return an error. So, we do our own error reporting for fatal errors
|
|
|
|
* through prog->stack_trace_err.
|
|
|
|
*/
|
|
|
|
err = prog->stack_trace_err;
|
|
|
|
prog->stack_trace_err = NULL;
|
2019-07-29 22:41:33 +01:00
|
|
|
err:
|
2019-10-18 10:42:02 +01:00
|
|
|
dwfl_detach_thread(thread);
|
2019-07-29 22:41:33 +01:00
|
|
|
return err;
|
|
|
|
}
|