2021-11-21 23:59:44 +00:00
|
|
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
2022-11-02 00:05:16 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
2019-07-29 22:41:33 +01:00
|
|
|
|
2020-09-24 00:02:02 +01:00
|
|
|
#include <assert.h>
|
2020-12-02 19:24:05 +00:00
|
|
|
#include <elfutils/libdw.h>
|
2019-07-29 22:41:33 +01:00
|
|
|
#include <elfutils/libdwfl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdlib.h>
|
2020-09-24 00:02:02 +01:00
|
|
|
#include <string.h>
|
2019-07-29 22:41:33 +01:00
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
#include "cfi.h"
|
2020-09-16 01:42:53 +01:00
|
|
|
#include "debug_info.h"
|
2020-09-24 00:02:02 +01:00
|
|
|
#include "drgn.h"
|
2022-11-01 22:06:37 +00:00
|
|
|
#include "dwarf_constants.h"
|
2021-11-09 01:05:33 +00:00
|
|
|
#include "dwarf_info.h"
|
2022-10-20 21:22:50 +01:00
|
|
|
#include "elf_file.h"
|
2020-09-24 00:02:02 +01:00
|
|
|
#include "error.h"
|
2019-10-26 00:11:14 +01:00
|
|
|
#include "helpers.h"
|
2021-03-10 09:51:33 +00:00
|
|
|
#include "minmax.h"
|
2021-11-10 23:09:29 +00:00
|
|
|
#include "nstring.h"
|
2020-09-24 00:02:02 +01:00
|
|
|
#include "platform.h"
|
2019-07-29 22:41:33 +01:00
|
|
|
#include "program.h"
|
2021-03-10 09:51:33 +00:00
|
|
|
#include "register_state.h"
|
|
|
|
#include "serialize.h"
|
2020-05-13 07:21:50 +01:00
|
|
|
#include "stack_trace.h"
|
2019-07-29 22:41:33 +01:00
|
|
|
#include "string_builder.h"
|
|
|
|
#include "symbol.h"
|
2020-09-24 00:02:02 +01:00
|
|
|
#include "type.h"
|
|
|
|
#include "util.h"
|
2019-07-29 22:41:33 +01:00
|
|
|
|
2020-12-02 19:24:05 +00:00
|
|
|
static struct drgn_error *
|
|
|
|
drgn_stack_trace_append_frame(struct drgn_stack_trace **trace, size_t *capacity,
|
|
|
|
struct drgn_register_state *regs,
|
|
|
|
Dwarf_Die *scopes, size_t num_scopes,
|
|
|
|
size_t function_scope)
|
2021-03-10 09:51:33 +00:00
|
|
|
{
|
|
|
|
if ((*trace)->num_frames == *capacity) {
|
|
|
|
static const size_t max_capacity =
|
|
|
|
(SIZE_MAX - sizeof(struct drgn_stack_trace)) /
|
|
|
|
sizeof(struct drgn_stack_frame);
|
|
|
|
if (*capacity == max_capacity)
|
2020-12-02 19:24:05 +00:00
|
|
|
return &drgn_enomem;
|
2021-03-10 09:51:33 +00:00
|
|
|
size_t new_capacity;
|
|
|
|
if (*capacity > max_capacity / 2)
|
|
|
|
new_capacity = max_capacity;
|
|
|
|
else
|
|
|
|
new_capacity = 2 * (*capacity);
|
|
|
|
struct drgn_stack_trace *new_trace =
|
|
|
|
realloc((*trace),
|
|
|
|
offsetof(struct drgn_stack_trace,
|
|
|
|
frames[new_capacity]));
|
|
|
|
if (!new_trace)
|
2020-12-02 19:24:05 +00:00
|
|
|
return &drgn_enomem;
|
2021-03-10 09:51:33 +00:00
|
|
|
*trace = new_trace;
|
|
|
|
*capacity = new_capacity;
|
|
|
|
}
|
|
|
|
struct drgn_stack_frame *frame =
|
|
|
|
&(*trace)->frames[(*trace)->num_frames++];
|
|
|
|
frame->regs = regs;
|
2020-12-02 19:24:05 +00:00
|
|
|
frame->scopes = scopes;
|
|
|
|
frame->num_scopes = num_scopes;
|
|
|
|
frame->function_scope = function_scope;
|
|
|
|
return NULL;
|
2021-03-10 09:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void drgn_stack_trace_shrink_to_fit(struct drgn_stack_trace **trace,
|
|
|
|
size_t capacity)
|
|
|
|
{
|
|
|
|
size_t num_frames = (*trace)->num_frames;
|
|
|
|
if (capacity > num_frames) {
|
|
|
|
struct drgn_stack_trace *new_trace =
|
|
|
|
realloc((*trace),
|
|
|
|
offsetof(struct drgn_stack_trace,
|
|
|
|
frames[num_frames]));
|
|
|
|
if (new_trace)
|
|
|
|
*trace = new_trace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 22:41:33 +01:00
|
|
|
LIBDRGN_PUBLIC void drgn_stack_trace_destroy(struct drgn_stack_trace *trace)
|
|
|
|
{
|
2022-01-07 02:23:27 +00:00
|
|
|
if (trace) {
|
|
|
|
struct drgn_register_state *regs = NULL;
|
|
|
|
for (size_t i = 0; i < trace->num_frames; i++) {
|
|
|
|
if (trace->frames[i].regs != regs) {
|
|
|
|
drgn_register_state_destroy(regs);
|
|
|
|
regs = trace->frames[i].regs;
|
|
|
|
}
|
|
|
|
free(trace->frames[i].scopes);
|
2020-12-02 19:24:05 +00:00
|
|
|
}
|
2022-01-07 02:23:27 +00:00
|
|
|
drgn_register_state_destroy(regs);
|
|
|
|
free(trace);
|
2020-12-02 19:24:05 +00:00
|
|
|
}
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
2022-08-11 22:42:21 +01:00
|
|
|
LIBDRGN_PUBLIC
|
|
|
|
struct drgn_program *drgn_stack_trace_program(struct drgn_stack_trace *trace)
|
|
|
|
{
|
|
|
|
return trace->prog;
|
|
|
|
}
|
|
|
|
|
2020-05-13 07:21:50 +01:00
|
|
|
LIBDRGN_PUBLIC size_t
|
|
|
|
drgn_stack_trace_num_frames(struct drgn_stack_trace *trace)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
|
|
|
return trace->num_frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
2019-11-28 03:27:14 +00:00
|
|
|
drgn_format_stack_trace(struct drgn_stack_trace *trace, char **ret)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
2022-10-05 22:41:12 +01:00
|
|
|
struct string_builder str = STRING_BUILDER_INIT;
|
2020-05-13 07:21:50 +01:00
|
|
|
for (size_t frame = 0; frame < trace->num_frames; frame++) {
|
2021-03-10 09:51:33 +00:00
|
|
|
if (!string_builder_appendf(&str, "#%-2zu ", frame))
|
|
|
|
goto enomem;
|
|
|
|
|
|
|
|
struct drgn_register_state *regs = trace->frames[frame].regs;
|
2020-12-02 19:24:05 +00:00
|
|
|
struct optional_uint64 pc;
|
|
|
|
const char *name = drgn_stack_frame_name(trace, frame);
|
|
|
|
if (name) {
|
|
|
|
if (!string_builder_append(&str, name))
|
|
|
|
goto enomem;
|
|
|
|
} else if ((pc = drgn_register_state_get_pc(regs)).has_value) {
|
2021-03-10 09:51:33 +00:00
|
|
|
Dwfl_Module *dwfl_module =
|
|
|
|
regs->module ? regs->module->dwfl_module : NULL;
|
|
|
|
struct drgn_symbol sym;
|
|
|
|
if (dwfl_module &&
|
|
|
|
drgn_program_find_symbol_by_address_internal(trace->prog,
|
|
|
|
pc.value - !regs->interrupted,
|
|
|
|
dwfl_module,
|
|
|
|
&sym)) {
|
|
|
|
if (!string_builder_appendf(&str,
|
|
|
|
"%s+0x%" PRIx64 "/0x%" PRIx64,
|
|
|
|
sym.name,
|
|
|
|
pc.value - sym.address,
|
|
|
|
sym.size))
|
|
|
|
goto enomem;
|
|
|
|
} else {
|
|
|
|
if (!string_builder_appendf(&str, "0x%" PRIx64,
|
|
|
|
pc.value))
|
|
|
|
goto enomem;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
2019-12-10 19:02:55 +00:00
|
|
|
} else {
|
2021-03-10 09:51:33 +00:00
|
|
|
if (!string_builder_append(&str, "???"))
|
|
|
|
goto enomem;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
2019-12-10 19:02:55 +00:00
|
|
|
|
2020-12-02 19:24:05 +00:00
|
|
|
int line, column;
|
|
|
|
const char *filename = drgn_stack_frame_source(trace, frame,
|
|
|
|
&line, &column);
|
|
|
|
if (filename && column) {
|
|
|
|
if (!string_builder_appendf(&str, " (%s:%d:%d)",
|
|
|
|
filename, line, column))
|
|
|
|
goto enomem;
|
|
|
|
} else if (filename) {
|
|
|
|
if (!string_builder_appendf(&str, " (%s:%d)", filename,
|
|
|
|
line))
|
|
|
|
goto enomem;
|
|
|
|
}
|
|
|
|
|
2020-05-13 07:21:50 +01:00
|
|
|
if (frame != trace->num_frames - 1 &&
|
2021-03-10 09:51:33 +00:00
|
|
|
!string_builder_appendc(&str, '\n'))
|
|
|
|
goto enomem;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
2022-09-21 18:43:31 +01:00
|
|
|
if (!(*ret = string_builder_null_terminate(&str)))
|
2021-03-10 09:51:33 +00:00
|
|
|
goto enomem;
|
2019-07-29 22:41:33 +01:00
|
|
|
return NULL;
|
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
enomem:
|
2019-07-29 22:41:33 +01:00
|
|
|
free(str.str);
|
2021-03-10 09:51:33 +00:00
|
|
|
return &drgn_enomem;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 19:24:05 +00:00
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
|
|
|
drgn_format_stack_frame(struct drgn_stack_trace *trace, size_t frame, char **ret)
|
|
|
|
{
|
2022-10-05 22:41:12 +01:00
|
|
|
struct string_builder str = STRING_BUILDER_INIT;
|
2020-12-02 19:24:05 +00:00
|
|
|
struct drgn_register_state *regs = trace->frames[frame].regs;
|
|
|
|
if (!string_builder_appendf(&str, "#%zu at ", frame))
|
|
|
|
goto enomem;
|
|
|
|
|
|
|
|
struct optional_uint64 pc = drgn_register_state_get_pc(regs);
|
|
|
|
if (pc.has_value) {
|
|
|
|
if (!string_builder_appendf(&str, "%#" PRIx64, pc.value))
|
|
|
|
goto enomem;
|
|
|
|
|
|
|
|
Dwfl_Module *dwfl_module =
|
|
|
|
regs->module ? regs->module->dwfl_module : NULL;
|
|
|
|
struct drgn_symbol sym;
|
|
|
|
if (dwfl_module &&
|
|
|
|
drgn_program_find_symbol_by_address_internal(trace->prog,
|
|
|
|
pc.value - !regs->interrupted,
|
|
|
|
dwfl_module,
|
|
|
|
&sym) &&
|
|
|
|
!string_builder_appendf(&str, " (%s+0x%" PRIx64 "/0x%" PRIx64 ")",
|
|
|
|
sym.name, pc.value - sym.address,
|
|
|
|
sym.size))
|
|
|
|
goto enomem;
|
|
|
|
} else {
|
|
|
|
if (!string_builder_append(&str, "???"))
|
|
|
|
goto enomem;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *name = drgn_stack_frame_name(trace, frame);
|
|
|
|
if (name && !string_builder_appendf(&str, " in %s", name))
|
|
|
|
goto enomem;
|
|
|
|
|
|
|
|
int line, column;
|
|
|
|
const char *filename = drgn_stack_frame_source(trace, frame, &line,
|
|
|
|
&column);
|
|
|
|
if (filename && column) {
|
|
|
|
if (!string_builder_appendf(&str, " at %s:%d:%d", filename,
|
|
|
|
line, column))
|
|
|
|
goto enomem;
|
|
|
|
} else if (filename) {
|
|
|
|
if (!string_builder_appendf(&str, " at %s:%d", filename, line))
|
|
|
|
goto enomem;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drgn_stack_frame_is_inline(trace, frame) &&
|
|
|
|
!string_builder_append(&str, " (inlined)"))
|
|
|
|
goto enomem;
|
|
|
|
|
2022-09-21 18:43:31 +01:00
|
|
|
if (!(*ret = string_builder_null_terminate(&str)))
|
2020-12-02 19:24:05 +00:00
|
|
|
goto enomem;
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
enomem:
|
|
|
|
free(str.str);
|
|
|
|
return &drgn_enomem;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC const char *drgn_stack_frame_name(struct drgn_stack_trace *trace,
|
|
|
|
size_t frame)
|
|
|
|
{
|
|
|
|
Dwarf_Die *scopes = trace->frames[frame].scopes;
|
|
|
|
size_t num_scopes = trace->frames[frame].num_scopes;
|
|
|
|
size_t function_scope = trace->frames[frame].function_scope;
|
|
|
|
if (function_scope >= num_scopes)
|
|
|
|
return NULL;
|
|
|
|
return dwarf_diename(&scopes[function_scope]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC bool drgn_stack_frame_is_inline(struct drgn_stack_trace *trace,
|
|
|
|
size_t frame)
|
|
|
|
{
|
|
|
|
Dwarf_Die *scopes = trace->frames[frame].scopes;
|
|
|
|
size_t num_scopes = trace->frames[frame].num_scopes;
|
|
|
|
size_t function_scope = trace->frames[frame].function_scope;
|
|
|
|
return (function_scope < num_scopes &&
|
|
|
|
dwarf_tag(&scopes[function_scope]) ==
|
|
|
|
DW_TAG_inlined_subroutine);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC const char *
|
|
|
|
drgn_stack_frame_source(struct drgn_stack_trace *trace, size_t frame,
|
|
|
|
int *line_ret, int *column_ret)
|
|
|
|
{
|
|
|
|
if (frame > 0 &&
|
|
|
|
trace->frames[frame].regs == trace->frames[frame - 1].regs) {
|
|
|
|
/*
|
|
|
|
* This frame is the caller of an inline frame. Get the call
|
|
|
|
* location from the inlined_subroutine of the callee.
|
|
|
|
*/
|
|
|
|
Dwarf_Die *inlined_scopes = trace->frames[frame - 1].scopes;
|
|
|
|
size_t inlined_num_scopes = trace->frames[frame - 1].num_scopes;
|
|
|
|
size_t inlined_function_scope =
|
|
|
|
trace->frames[frame - 1].function_scope;
|
|
|
|
if (inlined_function_scope >= inlined_num_scopes)
|
|
|
|
return NULL;
|
|
|
|
Dwarf_Die *inlined = &inlined_scopes[inlined_function_scope];
|
|
|
|
|
|
|
|
Dwarf_Die inlined_cu;
|
|
|
|
Dwarf_Files *files;
|
|
|
|
if (!dwarf_diecu(inlined, &inlined_cu, NULL, NULL) ||
|
|
|
|
dwarf_getsrcfiles(&inlined_cu, &files, NULL))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Dwarf_Attribute attr;
|
|
|
|
Dwarf_Word value;
|
|
|
|
if (dwarf_formudata(dwarf_attr(inlined, DW_AT_call_file, &attr),
|
|
|
|
&value))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
const char *filename = dwarf_filesrc(files, value, NULL, NULL);
|
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
|
|
|
if (line_ret) {
|
|
|
|
if (dwarf_formudata(dwarf_attr(inlined, DW_AT_call_line,
|
|
|
|
&attr), &value))
|
|
|
|
*line_ret = 0;
|
|
|
|
else
|
|
|
|
*line_ret = value;
|
|
|
|
}
|
|
|
|
if (column_ret) {
|
|
|
|
if (dwarf_formudata(dwarf_attr(inlined,
|
|
|
|
DW_AT_call_column,
|
|
|
|
&attr), &value))
|
|
|
|
*column_ret = 0;
|
|
|
|
else
|
|
|
|
*column_ret = value;
|
|
|
|
}
|
|
|
|
return filename;
|
2021-07-07 21:35:54 +01:00
|
|
|
} else if (trace->frames[frame].num_scopes > 0) {
|
2020-12-02 19:24:05 +00:00
|
|
|
struct drgn_register_state *regs = trace->frames[frame].regs;
|
2022-10-18 18:24:56 +01:00
|
|
|
if (!regs->module)
|
2020-12-02 19:24:05 +00:00
|
|
|
return NULL;
|
2021-07-07 21:35:54 +01:00
|
|
|
|
2020-12-02 19:24:05 +00:00
|
|
|
struct optional_uint64 pc = drgn_register_state_get_pc(regs);
|
|
|
|
if (!pc.has_value)
|
|
|
|
return NULL;
|
2022-10-18 18:24:56 +01:00
|
|
|
pc.value -= !regs->interrupted + regs->module->debug_file_bias;
|
2021-07-07 21:35:54 +01:00
|
|
|
|
|
|
|
Dwarf_Die *scopes = trace->frames[frame].scopes;
|
|
|
|
size_t num_scopes = trace->frames[frame].num_scopes;
|
|
|
|
Dwarf_Die cu_die;
|
|
|
|
if (!dwarf_cu_die(scopes[num_scopes - 1].cu, &cu_die, NULL,
|
|
|
|
NULL, NULL, NULL, NULL, NULL))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Dwarf_Line *line = dwarf_getsrc_die(&cu_die, pc.value);
|
2020-12-02 19:24:05 +00:00
|
|
|
if (!line)
|
|
|
|
return NULL;
|
2021-07-07 21:35:54 +01:00
|
|
|
if (line_ret)
|
|
|
|
dwarf_lineno(line, line_ret);
|
|
|
|
if (column_ret)
|
|
|
|
dwarf_linecol(line, column_ret);
|
|
|
|
return dwarf_linesrc(line, NULL, NULL);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
2020-12-02 19:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
LIBDRGN_PUBLIC bool drgn_stack_frame_interrupted(struct drgn_stack_trace *trace,
|
|
|
|
size_t frame)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
2021-03-10 09:51:33 +00:00
|
|
|
return trace->frames[frame].regs->interrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC bool drgn_stack_frame_pc(struct drgn_stack_trace *trace,
|
|
|
|
size_t frame, uint64_t *ret)
|
|
|
|
{
|
|
|
|
struct optional_uint64 pc =
|
|
|
|
drgn_register_state_get_pc(trace->frames[frame].regs);
|
|
|
|
if (pc.has_value)
|
|
|
|
*ret = pc.value;
|
|
|
|
return pc.has_value;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 00:51:44 +00:00
|
|
|
LIBDRGN_PUBLIC bool drgn_stack_frame_sp(struct drgn_stack_trace *trace,
|
|
|
|
size_t frame, uint64_t *ret)
|
|
|
|
{
|
|
|
|
struct drgn_program *prog = trace->prog;
|
|
|
|
drgn_register_number regno = prog->platform.arch->stack_pointer_regno;
|
|
|
|
struct drgn_register_state *regs = trace->frames[frame].regs;
|
|
|
|
if (!drgn_register_state_has_register(regs, regno))
|
|
|
|
return false;
|
|
|
|
const struct drgn_register_layout *layout =
|
|
|
|
&prog->platform.arch->register_layout[regno];
|
|
|
|
copy_lsbytes(ret, sizeof(*ret), HOST_LITTLE_ENDIAN,
|
|
|
|
®s->buf[layout->offset], layout->size,
|
|
|
|
drgn_platform_is_little_endian(&prog->platform));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-29 22:41:33 +01:00
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
2020-05-13 07:21:50 +01:00
|
|
|
drgn_stack_frame_symbol(struct drgn_stack_trace *trace, size_t frame,
|
|
|
|
struct drgn_symbol **ret)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
2021-03-10 09:51:33 +00:00
|
|
|
struct drgn_register_state *regs = trace->frames[frame].regs;
|
|
|
|
struct optional_uint64 pc = drgn_register_state_get_pc(regs);
|
|
|
|
if (!pc.has_value) {
|
|
|
|
return drgn_error_create(DRGN_ERROR_LOOKUP,
|
|
|
|
"program counter is not known at stack frame");
|
|
|
|
}
|
|
|
|
pc.value -= !regs->interrupted;
|
|
|
|
Dwfl_Module *dwfl_module =
|
|
|
|
regs->module ? regs->module->dwfl_module : NULL;
|
|
|
|
if (!dwfl_module)
|
|
|
|
return drgn_error_symbol_not_found(pc.value);
|
2020-05-13 07:21:50 +01:00
|
|
|
struct drgn_symbol *sym = malloc(sizeof(*sym));
|
2019-12-10 19:02:55 +00:00
|
|
|
if (!sym)
|
|
|
|
return &drgn_enomem;
|
2021-03-10 09:51:33 +00:00
|
|
|
if (!drgn_program_find_symbol_by_address_internal(trace->prog, pc.value,
|
|
|
|
dwfl_module, sym)) {
|
2019-12-10 19:02:55 +00:00
|
|
|
free(sym);
|
2021-03-10 09:51:33 +00:00
|
|
|
return drgn_error_symbol_not_found(pc.value);
|
2019-12-10 19:02:55 +00:00
|
|
|
}
|
|
|
|
*ret = sym;
|
|
|
|
return NULL;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 00:42:20 +01:00
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
|
|
|
drgn_stack_frame_locals(struct drgn_stack_trace *trace, size_t frame_i,
|
|
|
|
const char ***names_ret, size_t *count_ret)
|
|
|
|
{
|
|
|
|
struct drgn_stack_frame *frame = &trace->frames[frame_i];
|
|
|
|
if (frame->function_scope >= frame->num_scopes) {
|
|
|
|
*names_ret = NULL;
|
|
|
|
*count_ret = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return drgn_dwarf_scopes_names(frame->scopes + frame->function_scope,
|
|
|
|
frame->num_scopes - frame->function_scope,
|
|
|
|
names_ret, count_ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC
|
|
|
|
void drgn_stack_frame_locals_destroy(const char **names, size_t count)
|
|
|
|
{
|
|
|
|
free(names);
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:17:41 +01:00
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
|
|
|
drgn_stack_frame_find_object(struct drgn_stack_trace *trace, size_t frame_i,
|
|
|
|
const char *name, struct drgn_object *ret)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
struct drgn_stack_frame *frame = &trace->frames[frame_i];
|
|
|
|
|
2021-07-07 21:35:54 +01:00
|
|
|
if (frame->function_scope >= frame->num_scopes)
|
2021-06-02 21:17:41 +01:00
|
|
|
goto not_found;
|
|
|
|
|
|
|
|
Dwarf_Die die, type_die;
|
|
|
|
err = drgn_find_in_dwarf_scopes(frame->scopes, frame->num_scopes, name,
|
|
|
|
&die, &type_die);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (!die.addr && frame->function_scope == 0) {
|
|
|
|
/*
|
|
|
|
* Scope 0 must be a DW_TAG_inlined_subroutine, and we didn't
|
|
|
|
* find the name in the concrete inlined instance tree. We need
|
|
|
|
* to find the scopes that contain the the abstract instance
|
|
|
|
* root (i.e, the DW_TAG_subprogram definition). (We could do
|
|
|
|
* this ahead of time when unwinding the stack, but for
|
|
|
|
* efficiency we do it lazily.)
|
|
|
|
*/
|
|
|
|
Dwarf_Attribute attr_mem, *attr;
|
|
|
|
if (!(attr = dwarf_attr(frame->scopes, DW_AT_abstract_origin,
|
|
|
|
&attr_mem)))
|
|
|
|
goto not_found;
|
|
|
|
Dwarf_Die abstract_origin;
|
|
|
|
if (!dwarf_formref_die(attr, &abstract_origin))
|
|
|
|
return drgn_error_libdw();
|
|
|
|
|
|
|
|
Dwarf_Die *ancestors;
|
|
|
|
size_t num_ancestors;
|
|
|
|
err = drgn_find_die_ancestors(&abstract_origin, &ancestors,
|
|
|
|
&num_ancestors);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
size_t new_num_scopes = num_ancestors + frame->num_scopes;
|
|
|
|
Dwarf_Die *new_scopes = realloc(ancestors,
|
|
|
|
new_num_scopes *
|
|
|
|
sizeof(*new_scopes));
|
|
|
|
if (!new_scopes) {
|
|
|
|
free(ancestors);
|
|
|
|
return &drgn_enomem;
|
|
|
|
}
|
|
|
|
memcpy(&new_scopes[num_ancestors], frame->scopes,
|
|
|
|
frame->num_scopes * sizeof(*new_scopes));
|
|
|
|
free(frame->scopes);
|
|
|
|
frame->scopes = new_scopes;
|
|
|
|
frame->num_scopes = new_num_scopes;
|
|
|
|
frame->function_scope = num_ancestors;
|
|
|
|
|
|
|
|
/* Look for the name in the new scopes. */
|
|
|
|
err = drgn_find_in_dwarf_scopes(frame->scopes, num_ancestors,
|
|
|
|
name, &die, &type_die);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (!die.addr) {
|
|
|
|
not_found:;
|
|
|
|
const char *frame_name = drgn_stack_frame_name(trace, frame_i);
|
|
|
|
if (frame_name) {
|
|
|
|
return drgn_error_format(DRGN_ERROR_LOOKUP,
|
|
|
|
"could not find '%s' in '%s'",
|
|
|
|
name, frame_name);
|
|
|
|
} else {
|
|
|
|
return drgn_error_format(DRGN_ERROR_LOOKUP,
|
|
|
|
"could not find '%s'", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 21:22:50 +01:00
|
|
|
// It doesn't make sense to use the registers if the file has a
|
2022-10-20 20:36:29 +01:00
|
|
|
// different platform than the program.
|
|
|
|
const struct drgn_register_state *regs = frame->regs;
|
2022-10-20 21:22:50 +01:00
|
|
|
struct drgn_elf_file *file = regs->module->debug_file;
|
|
|
|
if (!drgn_platforms_equal(&file->platform, &trace->prog->platform))
|
2022-10-20 20:36:29 +01:00
|
|
|
regs = NULL;
|
2021-06-02 21:17:41 +01:00
|
|
|
Dwarf_Die function_die = frame->scopes[frame->function_scope];
|
2022-10-20 21:22:50 +01:00
|
|
|
return drgn_object_from_dwarf(trace->prog->dbinfo, file, &die,
|
2021-06-02 21:17:41 +01:00
|
|
|
dwarf_tag(&die) == DW_TAG_enumerator ?
|
|
|
|
&type_die : NULL,
|
2022-10-20 20:36:29 +01:00
|
|
|
&function_die, regs, ret);
|
2021-06-02 21:17:41 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 23:49:21 +00:00
|
|
|
LIBDRGN_PUBLIC bool drgn_stack_frame_register(struct drgn_stack_trace *trace,
|
|
|
|
size_t frame,
|
|
|
|
const struct drgn_register *reg,
|
|
|
|
uint64_t *ret)
|
2019-10-18 10:42:51 +01:00
|
|
|
{
|
2021-03-10 09:51:33 +00:00
|
|
|
struct drgn_program *prog = trace->prog;
|
|
|
|
struct drgn_register_state *regs = trace->frames[frame].regs;
|
|
|
|
if (!drgn_register_state_has_register(regs, reg->regno))
|
2021-01-28 23:49:21 +00:00
|
|
|
return false;
|
2021-03-10 09:51:33 +00:00
|
|
|
const struct drgn_register_layout *layout =
|
|
|
|
&prog->platform.arch->register_layout[reg->regno];
|
|
|
|
if (layout->size > sizeof(*ret))
|
2020-04-27 08:34:04 +01:00
|
|
|
return false;
|
2021-03-10 09:51:33 +00:00
|
|
|
copy_lsbytes(ret, sizeof(*ret), HOST_LITTLE_ENDIAN,
|
|
|
|
®s->buf[layout->offset], layout->size,
|
|
|
|
drgn_platform_is_little_endian(&prog->platform));
|
2019-07-29 22:41:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:36:18 +00:00
|
|
|
static struct drgn_error *
|
2021-03-10 09:51:33 +00:00
|
|
|
drgn_get_stack_trace_obj(struct drgn_object *res,
|
|
|
|
const struct drgn_object *thread_obj,
|
2019-10-28 18:36:18 +00:00
|
|
|
bool *is_pt_regs_ret)
|
|
|
|
{
|
2021-03-10 09:51:33 +00:00
|
|
|
struct drgn_program *prog = drgn_object_program(res);
|
2019-10-28 18:36:18 +00:00
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
struct drgn_type *type = drgn_underlying_type(thread_obj->type);
|
2019-10-28 18:36:18 +00:00
|
|
|
if (drgn_type_kind(type) == DRGN_TYPE_STRUCT &&
|
|
|
|
strcmp(drgn_type_tag(type), "pt_regs") == 0) {
|
|
|
|
*is_pt_regs_ret = true;
|
2021-03-10 09:51:33 +00:00
|
|
|
return drgn_object_read(res, thread_obj);
|
2019-10-28 18:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (drgn_type_kind(type) != DRGN_TYPE_POINTER)
|
|
|
|
goto type_error;
|
|
|
|
type = drgn_underlying_type(drgn_type_type(type).type);
|
|
|
|
if (drgn_type_kind(type) != DRGN_TYPE_STRUCT)
|
|
|
|
goto type_error;
|
|
|
|
|
|
|
|
if ((prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) &&
|
|
|
|
strcmp(drgn_type_tag(type), "task_struct") == 0) {
|
|
|
|
*is_pt_regs_ret = false;
|
2021-03-10 09:51:33 +00:00
|
|
|
return drgn_object_read(res, thread_obj);
|
2019-10-28 18:36:18 +00:00
|
|
|
} else if (strcmp(drgn_type_tag(type), "pt_regs") == 0) {
|
|
|
|
*is_pt_regs_ret = true;
|
|
|
|
/*
|
|
|
|
* If the drgn_object_read() call fails, we're breaking
|
|
|
|
* the rule of not modifying the result on error, but we
|
|
|
|
* don't care in this context.
|
|
|
|
*/
|
2021-03-10 09:51:33 +00:00
|
|
|
struct drgn_error *err = drgn_object_dereference(res,
|
|
|
|
thread_obj);
|
2019-10-28 18:36:18 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
return drgn_object_read(res, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_error:
|
|
|
|
return drgn_error_format(DRGN_ERROR_TYPE,
|
|
|
|
"expected struct pt_regs, struct pt_regs *%s, or int",
|
|
|
|
(prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) ?
|
|
|
|
", struct task_struct *" : "");
|
|
|
|
}
|
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
static struct drgn_error *
|
|
|
|
drgn_get_initial_registers(struct drgn_program *prog, uint32_t tid,
|
|
|
|
const struct drgn_object *thread_obj,
|
2021-11-19 00:46:59 +00:00
|
|
|
struct nstring *prstatus,
|
2021-03-10 09:51:33 +00:00
|
|
|
struct drgn_register_state **ret)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
2019-10-26 00:11:14 +01:00
|
|
|
struct drgn_object obj;
|
2020-05-20 19:30:00 +01:00
|
|
|
struct drgn_object tmp;
|
2021-11-19 00:46:59 +00:00
|
|
|
struct nstring _prstatus;
|
2019-10-26 00:11:14 +01:00
|
|
|
|
|
|
|
drgn_object_init(&obj, prog);
|
2020-05-20 19:30:00 +01:00
|
|
|
drgn_object_init(&tmp, prog);
|
2019-10-24 22:26:45 +01:00
|
|
|
|
2019-10-28 18:36:18 +00:00
|
|
|
/* First, try pt_regs. */
|
2021-03-10 09:51:33 +00:00
|
|
|
if (thread_obj) {
|
2019-10-28 18:36:18 +00:00
|
|
|
bool is_pt_regs;
|
2021-03-10 09:51:33 +00:00
|
|
|
err = drgn_get_stack_trace_obj(&obj, thread_obj, &is_pt_regs);
|
2019-10-26 00:11:14 +01:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2019-10-28 18:36:18 +00:00
|
|
|
|
|
|
|
if (is_pt_regs) {
|
2020-12-04 20:01:29 +00:00
|
|
|
assert(obj.encoding == DRGN_OBJECT_ENCODING_BUFFER);
|
2020-12-04 21:20:09 +00:00
|
|
|
assert(obj.kind == DRGN_OBJECT_VALUE);
|
2021-03-10 09:51:33 +00:00
|
|
|
if (!prog->platform.arch->pt_regs_get_initial_registers) {
|
2020-05-19 21:28:49 +01:00
|
|
|
err = drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
|
|
|
|
"pt_regs stack unwinding is not supported for %s architecture",
|
|
|
|
prog->platform.arch->name);
|
|
|
|
goto out;
|
|
|
|
}
|
2021-03-10 09:51:33 +00:00
|
|
|
err = prog->platform.arch->pt_regs_get_initial_registers(&obj,
|
|
|
|
ret);
|
2019-10-28 18:36:18 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2020-05-20 19:30:00 +01:00
|
|
|
} else if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
|
|
|
|
err = drgn_program_find_object(prog, "init_pid_ns", NULL,
|
|
|
|
DRGN_FIND_OBJECT_ANY, &tmp);
|
2019-10-24 22:26:45 +01:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2020-05-20 19:30:00 +01:00
|
|
|
err = drgn_object_address_of(&tmp, &tmp);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
2021-03-10 09:51:33 +00:00
|
|
|
err = linux_helper_find_task(&obj, &tmp, tid);
|
2020-05-20 19:30:00 +01:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2020-07-09 01:50:33 +01:00
|
|
|
bool found;
|
2020-05-20 19:30:00 +01:00
|
|
|
err = drgn_object_bool(&obj, &found);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
if (!found) {
|
|
|
|
err = drgn_error_create(DRGN_ERROR_LOOKUP, "task not found");
|
2019-10-24 22:26:45 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 00:11:14 +01:00
|
|
|
|
2020-05-20 19:30:00 +01:00
|
|
|
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
|
|
|
|
if (prog->flags & DRGN_PROGRAM_IS_LIVE) {
|
|
|
|
err = drgn_object_member_dereference(&tmp, &obj, "on_cpu");
|
|
|
|
if (!err) {
|
|
|
|
bool on_cpu;
|
|
|
|
err = drgn_object_bool(&tmp, &on_cpu);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
if (on_cpu) {
|
|
|
|
err = drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
|
|
|
|
"cannot unwind stack of running task");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else if (err->code == DRGN_ERROR_LOOKUP) {
|
|
|
|
/*
|
|
|
|
* The running kernel is !SMP. Assume that the
|
|
|
|
* task isn't running (which can only be wrong
|
|
|
|
* for this thread itself).
|
|
|
|
*/
|
|
|
|
drgn_error_destroy(err);
|
|
|
|
} else {
|
|
|
|
goto out;
|
|
|
|
}
|
2021-11-19 00:46:59 +00:00
|
|
|
} else if (prstatus) {
|
|
|
|
goto prstatus;
|
2020-05-20 19:30:00 +01:00
|
|
|
} else {
|
2021-11-19 00:46:59 +00:00
|
|
|
prstatus = &_prstatus;
|
2020-07-09 01:50:33 +01:00
|
|
|
/*
|
|
|
|
* For kernel core dumps, we look up the PRSTATUS note
|
|
|
|
* by CPU rather than by PID. This is because there is
|
|
|
|
* an idle task with PID 0 for each CPU, so we must find
|
|
|
|
* the idle task by CPU. Rather than making PID 0 a
|
|
|
|
* special case, we handle all tasks this way.
|
|
|
|
*/
|
2022-10-04 00:08:53 +01:00
|
|
|
uint64_t cpu;
|
|
|
|
err = linux_helper_task_cpu(&obj, &cpu);
|
|
|
|
if (err)
|
2020-05-20 19:30:00 +01:00
|
|
|
goto out;
|
2020-07-09 01:50:33 +01:00
|
|
|
uint32_t prstatus_tid;
|
2022-10-04 00:08:53 +01:00
|
|
|
err = drgn_program_find_prstatus_by_cpu(prog, cpu,
|
2021-11-19 00:46:59 +00:00
|
|
|
prstatus,
|
2020-07-09 01:50:33 +01:00
|
|
|
&prstatus_tid);
|
2020-05-20 19:30:00 +01:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2021-11-19 00:46:59 +00:00
|
|
|
if (prstatus->str) {
|
2020-07-09 01:50:33 +01:00
|
|
|
/*
|
|
|
|
* The PRSTATUS note is for the CPU that the
|
|
|
|
* task is assigned to, but it is not
|
|
|
|
* necessarily for this task. Only use it if the
|
|
|
|
* PID matches.
|
|
|
|
*
|
|
|
|
* Note that this isn't perfect: the PID is
|
|
|
|
* populated by the kernel from "current" (the
|
|
|
|
* current task) via a non-maskable interrupt
|
|
|
|
* (NMI). During a context switch, the stack
|
|
|
|
* pointer and current are not updated
|
|
|
|
* atomically, so if the NMI arrives in the
|
|
|
|
* middle of a context switch, the stack pointer
|
|
|
|
* may not actually be that of current.
|
|
|
|
* Therefore, the stack pointer in PRSTATUS may
|
|
|
|
* not actually be for the PID in PRSTATUS.
|
|
|
|
* Unfortunately, we can't easily fix this.
|
|
|
|
*/
|
|
|
|
err = drgn_object_member_dereference(&tmp, &obj, "pid");
|
|
|
|
if (err)
|
|
|
|
goto out;
|
2022-10-04 00:08:53 +01:00
|
|
|
union drgn_value value;
|
2020-07-09 01:50:33 +01:00
|
|
|
err = drgn_object_read_integer(&tmp, &value);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
if (prstatus_tid == value.uvalue)
|
|
|
|
goto prstatus;
|
|
|
|
}
|
2020-05-20 19:30:00 +01:00
|
|
|
}
|
2021-03-10 09:51:33 +00:00
|
|
|
if (!prog->platform.arch->linux_kernel_get_initial_registers) {
|
2020-05-20 19:30:00 +01:00
|
|
|
err = drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
|
|
|
|
"Linux kernel stack unwinding is not supported for %s architecture",
|
|
|
|
prog->platform.arch->name);
|
|
|
|
goto out;
|
2019-10-26 00:11:14 +01:00
|
|
|
}
|
2021-03-10 09:51:33 +00:00
|
|
|
err = prog->platform.arch->linux_kernel_get_initial_registers(&obj,
|
|
|
|
ret);
|
2020-05-20 19:30:00 +01:00
|
|
|
} else {
|
2021-11-19 00:46:59 +00:00
|
|
|
if (!prstatus) {
|
|
|
|
prstatus = &_prstatus;
|
|
|
|
err = drgn_program_find_prstatus_by_tid(prog, tid, prstatus);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
if (!prstatus->str) {
|
|
|
|
err = drgn_error_create(DRGN_ERROR_LOOKUP,
|
|
|
|
"thread not found");
|
|
|
|
goto out;
|
|
|
|
}
|
2020-05-20 19:30:00 +01:00
|
|
|
}
|
2020-07-09 01:50:33 +01:00
|
|
|
prstatus:
|
2021-03-10 09:51:33 +00:00
|
|
|
if (!prog->platform.arch->prstatus_get_initial_registers) {
|
2020-05-20 19:30:00 +01:00
|
|
|
err = drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
|
|
|
|
"core dump stack unwinding is not supported for %s architecture",
|
|
|
|
prog->platform.arch->name);
|
|
|
|
goto out;
|
|
|
|
}
|
2021-03-10 09:51:33 +00:00
|
|
|
err = prog->platform.arch->prstatus_get_initial_registers(prog,
|
2021-11-19 00:46:59 +00:00
|
|
|
prstatus->str,
|
|
|
|
prstatus->len,
|
2021-03-10 09:51:33 +00:00
|
|
|
ret);
|
2019-10-28 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 22:26:45 +01:00
|
|
|
out:
|
2020-05-20 19:30:00 +01:00
|
|
|
drgn_object_deinit(&tmp);
|
2019-10-26 00:11:14 +01:00
|
|
|
drgn_object_deinit(&obj);
|
2021-03-10 09:51:33 +00:00
|
|
|
return err;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 22:39:37 +00:00
|
|
|
static void drgn_add_to_register(void *dst, size_t dst_size, const void *src,
|
|
|
|
size_t src_size, int64_t addend,
|
|
|
|
bool little_endian)
|
|
|
|
{
|
|
|
|
while (addend && dst_size && src_size) {
|
|
|
|
uint64_t uvalue;
|
|
|
|
copy_lsbytes(&uvalue, sizeof(uvalue), HOST_LITTLE_ENDIAN, src,
|
|
|
|
src_size, little_endian);
|
|
|
|
size_t n = min(sizeof(uvalue), src_size);
|
|
|
|
if (little_endian)
|
|
|
|
src = (char *)src + n;
|
|
|
|
src_size -= n;
|
|
|
|
|
|
|
|
bool carry = __builtin_add_overflow(uvalue, (uint64_t)addend,
|
|
|
|
&uvalue);
|
|
|
|
addend = (addend < 0 ? -1 : 0) + carry;
|
|
|
|
|
|
|
|
copy_lsbytes(dst, dst_size, little_endian, &uvalue,
|
|
|
|
sizeof(uvalue), HOST_LITTLE_ENDIAN);
|
|
|
|
n = min(sizeof(uvalue), dst_size);
|
|
|
|
if (little_endian)
|
|
|
|
dst = (char *)dst + n;
|
|
|
|
dst_size -= n;
|
|
|
|
}
|
|
|
|
if (dst != src) {
|
|
|
|
copy_lsbytes(dst, dst_size, little_endian, src, src_size,
|
|
|
|
little_endian);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-02 19:24:05 +00:00
|
|
|
static struct drgn_error *
|
|
|
|
drgn_stack_trace_add_frames(struct drgn_stack_trace **trace,
|
|
|
|
size_t *trace_capacity,
|
|
|
|
struct drgn_register_state *regs)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
|
|
|
|
if (!regs->module) {
|
|
|
|
err = drgn_stack_trace_append_frame(trace, trace_capacity, regs,
|
|
|
|
NULL, 0, 0);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t pc = regs->_pc - !regs->interrupted;
|
|
|
|
uint64_t bias;
|
|
|
|
Dwarf_Die *scopes;
|
|
|
|
size_t num_scopes;
|
2022-04-26 22:30:08 +01:00
|
|
|
err = drgn_module_find_dwarf_scopes(regs->module, pc, &bias, &scopes,
|
|
|
|
&num_scopes);
|
2020-12-02 19:24:05 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
pc -= bias;
|
|
|
|
|
|
|
|
size_t orig_num_frames = (*trace)->num_frames;
|
|
|
|
/*
|
|
|
|
* Walk backwards through scopes, splitting into frames. Stop at index 1
|
|
|
|
* because 0 must be a unit DIE.
|
|
|
|
*/
|
|
|
|
size_t frame_end = num_scopes;
|
|
|
|
for (size_t i = num_scopes; i-- > 1;) {
|
|
|
|
bool has_pc;
|
|
|
|
if (i == num_scopes - 1) {
|
|
|
|
/*
|
|
|
|
* The last scope is guaranteed to contain PC, so avoid
|
|
|
|
* a call to dwarf_haspc().
|
|
|
|
*/
|
|
|
|
has_pc = true;
|
|
|
|
} else {
|
|
|
|
int r = dwarf_haspc(&scopes[i], pc);
|
|
|
|
if (r < 0) {
|
|
|
|
err = drgn_error_libdw();
|
|
|
|
goto out_scopes;
|
|
|
|
}
|
|
|
|
has_pc = r > 0;
|
|
|
|
}
|
|
|
|
if (has_pc) {
|
|
|
|
Dwarf_Die *frame_scopes;
|
|
|
|
switch (dwarf_tag(&scopes[i])) {
|
|
|
|
case DW_TAG_subprogram:
|
|
|
|
/*
|
|
|
|
* Reuse the original scopes array (shrinking it
|
|
|
|
* if necessary).
|
|
|
|
*/
|
|
|
|
if (frame_end == num_scopes ||
|
|
|
|
!(frame_scopes = realloc(scopes,
|
|
|
|
frame_end *
|
|
|
|
sizeof(scopes[i]))))
|
|
|
|
frame_scopes = scopes;
|
|
|
|
err = drgn_stack_trace_append_frame(trace,
|
|
|
|
trace_capacity,
|
|
|
|
regs,
|
|
|
|
frame_scopes,
|
|
|
|
frame_end,
|
|
|
|
i);
|
|
|
|
if (err) {
|
|
|
|
free(frame_scopes);
|
|
|
|
/*
|
|
|
|
* We stole scopes for frame_scopes, so
|
|
|
|
* not out_scopes.
|
|
|
|
*/
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Added the DW_TAG_subprogram frame. We're
|
|
|
|
* done.
|
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
case DW_TAG_inlined_subroutine:
|
|
|
|
frame_scopes = memdup(&scopes[i],
|
|
|
|
(frame_end - i) *
|
|
|
|
sizeof(scopes[i]));
|
|
|
|
if (!frame_scopes) {
|
|
|
|
err = &drgn_enomem;
|
|
|
|
goto out_scopes;
|
|
|
|
}
|
|
|
|
err = drgn_stack_trace_append_frame(trace,
|
|
|
|
trace_capacity,
|
|
|
|
regs,
|
|
|
|
frame_scopes,
|
|
|
|
frame_end - i,
|
|
|
|
0);
|
|
|
|
if (err) {
|
|
|
|
free(frame_scopes);
|
|
|
|
goto out_scopes;
|
|
|
|
}
|
|
|
|
frame_end = i;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* This DIE doesn't contain PC. Ignore it and everything
|
|
|
|
* after it.
|
|
|
|
*/
|
|
|
|
frame_end = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We didn't find a matching DW_TAG_subprogram. Free any matching
|
2021-07-07 21:35:54 +01:00
|
|
|
* DW_TAG_inlined_subroutine frames we found.
|
2020-12-02 19:24:05 +00:00
|
|
|
*/
|
|
|
|
for (size_t i = orig_num_frames; i < (*trace)->num_frames; i++)
|
|
|
|
free((*trace)->frames[i].scopes);
|
|
|
|
(*trace)->num_frames = orig_num_frames;
|
2021-07-07 21:35:54 +01:00
|
|
|
/* If we at least found the unit DIE, keep it. */
|
|
|
|
if (num_scopes > 0) {
|
|
|
|
Dwarf_Die *frame_scopes;
|
|
|
|
if (!(frame_scopes = realloc(scopes, sizeof(scopes[0]))))
|
|
|
|
frame_scopes = scopes;
|
|
|
|
err = drgn_stack_trace_append_frame(trace, trace_capacity, regs,
|
|
|
|
frame_scopes, 1, 1);
|
|
|
|
if (err) {
|
|
|
|
free(frame_scopes);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Otherwise, add a scopeless frame. */
|
2020-12-02 19:24:05 +00:00
|
|
|
err = drgn_stack_trace_append_frame(trace, trace_capacity, regs, NULL,
|
|
|
|
0, 0);
|
|
|
|
out_scopes:
|
|
|
|
free(scopes);
|
|
|
|
out:
|
|
|
|
if (err)
|
|
|
|
drgn_register_state_destroy(regs);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
static struct drgn_error *
|
2022-10-18 22:34:03 +01:00
|
|
|
drgn_unwind_one_register(struct drgn_program *prog, struct drgn_elf_file *file,
|
2021-03-10 09:51:33 +00:00
|
|
|
const struct drgn_cfi_rule *rule,
|
|
|
|
const struct drgn_register_state *regs, void *buf,
|
|
|
|
size_t size)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
2021-03-10 09:51:33 +00:00
|
|
|
struct drgn_error *err;
|
|
|
|
bool little_endian = drgn_platform_is_little_endian(&prog->platform);
|
|
|
|
SWITCH_ENUM(rule->kind,
|
|
|
|
case DRGN_CFI_RULE_UNDEFINED:
|
|
|
|
return &drgn_not_found;
|
|
|
|
case DRGN_CFI_RULE_AT_CFA_PLUS_OFFSET: {
|
|
|
|
struct optional_uint64 cfa = drgn_register_state_get_cfa(regs);
|
|
|
|
if (!cfa.has_value)
|
|
|
|
return &drgn_not_found;
|
|
|
|
err = drgn_program_read_memory(prog, buf,
|
|
|
|
cfa.value + rule->offset, size,
|
|
|
|
false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DRGN_CFI_RULE_CFA_PLUS_OFFSET: {
|
|
|
|
struct optional_uint64 cfa = drgn_register_state_get_cfa(regs);
|
|
|
|
if (!cfa.has_value)
|
|
|
|
return &drgn_not_found;
|
|
|
|
cfa.value += rule->offset;
|
|
|
|
copy_lsbytes(buf, size, little_endian, &cfa.value,
|
|
|
|
sizeof(cfa.value), HOST_LITTLE_ENDIAN);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-03-16 22:39:37 +00:00
|
|
|
case DRGN_CFI_RULE_AT_REGISTER_PLUS_OFFSET:
|
|
|
|
case DRGN_CFI_RULE_AT_REGISTER_ADD_OFFSET: {
|
2021-03-10 09:51:33 +00:00
|
|
|
if (!drgn_register_state_has_register(regs, rule->regno))
|
|
|
|
return &drgn_not_found;
|
|
|
|
const struct drgn_register_layout *layout =
|
|
|
|
&prog->platform.arch->register_layout[rule->regno];
|
2021-03-16 22:39:37 +00:00
|
|
|
uint64_t address;
|
|
|
|
copy_lsbytes(&address, sizeof(address), HOST_LITTLE_ENDIAN,
|
|
|
|
®s->buf[layout->offset], layout->size,
|
2021-03-10 09:51:33 +00:00
|
|
|
little_endian);
|
2021-03-16 22:39:37 +00:00
|
|
|
if (rule->kind == DRGN_CFI_RULE_AT_REGISTER_PLUS_OFFSET)
|
|
|
|
address += rule->offset;
|
|
|
|
address &= drgn_platform_address_mask(&prog->platform);
|
|
|
|
err = drgn_program_read_memory(prog, buf, address, size, false);
|
|
|
|
if (!err && rule->kind == DRGN_CFI_RULE_AT_REGISTER_ADD_OFFSET) {
|
|
|
|
drgn_add_to_register(buf, size, buf, size, rule->offset,
|
|
|
|
little_endian);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DRGN_CFI_RULE_REGISTER_PLUS_OFFSET: {
|
|
|
|
if (!drgn_register_state_has_register(regs, rule->regno))
|
|
|
|
return &drgn_not_found;
|
|
|
|
const struct drgn_register_layout *layout =
|
|
|
|
&prog->platform.arch->register_layout[rule->regno];
|
|
|
|
drgn_add_to_register(buf, size, ®s->buf[layout->offset],
|
|
|
|
layout->size, rule->offset, little_endian);
|
2021-03-10 09:51:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
case DRGN_CFI_RULE_AT_DWARF_EXPRESSION:
|
|
|
|
case DRGN_CFI_RULE_DWARF_EXPRESSION:
|
2022-10-18 22:34:03 +01:00
|
|
|
err = drgn_eval_cfi_dwarf_expression(prog, file, rule, regs,
|
|
|
|
buf, size);
|
2021-03-10 09:51:33 +00:00
|
|
|
break;
|
2022-06-22 02:59:30 +01:00
|
|
|
case DRGN_CFI_RULE_CONSTANT:
|
|
|
|
copy_lsbytes(buf, size, little_endian, &rule->constant,
|
|
|
|
sizeof(rule->constant), HOST_LITTLE_ENDIAN);
|
|
|
|
return NULL;
|
2021-03-10 09:51:33 +00:00
|
|
|
)
|
|
|
|
/*
|
|
|
|
* If we couldn't read from memory, leave the register unknown instead
|
|
|
|
* of failing hard.
|
|
|
|
*/
|
|
|
|
if (err && err->code == DRGN_ERROR_FAULT) {
|
|
|
|
drgn_error_destroy(err);
|
|
|
|
err = &drgn_not_found;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
2021-03-10 09:51:33 +00:00
|
|
|
return err;
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
static struct drgn_error *drgn_unwind_cfa(struct drgn_program *prog,
|
2022-10-18 22:34:03 +01:00
|
|
|
struct drgn_elf_file *file,
|
2021-03-10 09:51:33 +00:00
|
|
|
const struct drgn_cfi_row *row,
|
|
|
|
struct drgn_register_state *regs)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
struct drgn_cfi_rule rule;
|
|
|
|
drgn_cfi_row_get_cfa(row, &rule);
|
|
|
|
uint8_t address_size = drgn_platform_address_size(&prog->platform);
|
|
|
|
char buf[8];
|
2022-10-18 22:34:03 +01:00
|
|
|
err = drgn_unwind_one_register(prog, file, &rule, regs, buf,
|
|
|
|
address_size);
|
2021-03-10 09:51:33 +00:00
|
|
|
if (!err) {
|
|
|
|
uint64_t cfa;
|
|
|
|
copy_lsbytes(&cfa, sizeof(cfa), HOST_LITTLE_ENDIAN, buf,
|
|
|
|
address_size,
|
|
|
|
drgn_platform_is_little_endian(&prog->platform));
|
|
|
|
drgn_register_state_set_cfa(prog, regs, cfa);
|
|
|
|
} else if (err == &drgn_not_found) {
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct drgn_error *
|
|
|
|
drgn_unwind_with_cfi(struct drgn_program *prog, struct drgn_cfi_row **row,
|
|
|
|
struct drgn_register_state *regs,
|
|
|
|
struct drgn_register_state **ret)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
|
|
|
|
if (!regs->module)
|
|
|
|
return &drgn_not_found;
|
|
|
|
|
2022-10-18 22:34:03 +01:00
|
|
|
struct drgn_elf_file *file;
|
2021-03-10 09:51:33 +00:00
|
|
|
bool interrupted;
|
|
|
|
drgn_register_number ret_addr_regno;
|
|
|
|
/* If we found the module, then we must have the PC. */
|
2022-04-26 22:30:08 +01:00
|
|
|
err = drgn_module_find_cfi(prog, regs->module,
|
2022-10-18 22:34:03 +01:00
|
|
|
regs->_pc - !regs->interrupted, &file, row,
|
|
|
|
&interrupted, &ret_addr_regno);
|
2021-03-10 09:51:33 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2022-10-18 22:34:03 +01:00
|
|
|
err = drgn_unwind_cfa(prog, file, *row, regs);
|
2021-03-10 09:51:33 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
size_t num_regs = (*row)->num_regs;
|
|
|
|
if (num_regs == 0)
|
|
|
|
return &drgn_stop;
|
|
|
|
|
|
|
|
const struct drgn_register_layout *layout =
|
|
|
|
&prog->platform.arch->register_layout[num_regs - 1];
|
|
|
|
struct drgn_register_state *unwound =
|
|
|
|
drgn_register_state_create_impl(layout->offset + layout->size,
|
|
|
|
num_regs, interrupted);
|
|
|
|
if (!unwound)
|
|
|
|
return &drgn_enomem;
|
|
|
|
|
|
|
|
bool has_any_register = false;
|
|
|
|
for (drgn_register_number regno = 0; regno < num_regs; regno++) {
|
|
|
|
struct drgn_cfi_rule rule;
|
|
|
|
drgn_cfi_row_get_register(*row, regno, &rule);
|
|
|
|
layout = &prog->platform.arch->register_layout[regno];
|
2022-10-18 22:34:03 +01:00
|
|
|
err = drgn_unwind_one_register(prog, file, &rule, regs,
|
2021-03-10 09:51:33 +00:00
|
|
|
&unwound->buf[layout->offset],
|
|
|
|
layout->size);
|
|
|
|
if (!err) {
|
|
|
|
drgn_register_state_set_has_register(unwound, regno);
|
|
|
|
has_any_register = true;
|
|
|
|
} else if (err != &drgn_not_found) {
|
|
|
|
drgn_register_state_destroy(unwound);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!has_any_register) {
|
|
|
|
/* Couldn't unwind any registers. We're done. */
|
|
|
|
drgn_register_state_destroy(unwound);
|
|
|
|
return &drgn_stop;
|
|
|
|
}
|
2022-12-02 21:52:06 +00:00
|
|
|
if (prog->platform.arch->demangle_cfi_registers)
|
|
|
|
prog->platform.arch->demangle_cfi_registers(prog, unwound);
|
2021-03-10 09:51:33 +00:00
|
|
|
if (drgn_register_state_has_register(unwound, ret_addr_regno)) {
|
|
|
|
layout = &prog->platform.arch->register_layout[ret_addr_regno];
|
|
|
|
drgn_register_state_set_pc_from_register_impl(prog, unwound,
|
|
|
|
ret_addr_regno,
|
|
|
|
layout->offset,
|
|
|
|
layout->size);
|
|
|
|
}
|
|
|
|
*ret = unwound;
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-29 22:41:33 +01:00
|
|
|
|
2019-10-26 00:11:14 +01:00
|
|
|
static struct drgn_error *drgn_get_stack_trace(struct drgn_program *prog,
|
|
|
|
uint32_t tid,
|
|
|
|
const struct drgn_object *obj,
|
2021-11-19 00:46:59 +00:00
|
|
|
struct nstring *prstatus,
|
2019-10-26 00:11:14 +01:00
|
|
|
struct drgn_stack_trace **ret)
|
2019-07-29 22:41:33 +01:00
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
|
|
|
|
if (!prog->has_platform) {
|
|
|
|
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
|
|
|
|
"cannot unwind stack without platform");
|
|
|
|
}
|
2019-10-26 00:11:14 +01:00
|
|
|
if ((prog->flags & (DRGN_PROGRAM_IS_LINUX_KERNEL |
|
|
|
|
DRGN_PROGRAM_IS_LIVE)) == DRGN_PROGRAM_IS_LIVE) {
|
2019-07-29 22:41:33 +01:00
|
|
|
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
|
2019-10-26 00:11:14 +01:00
|
|
|
"stack unwinding is not yet supported for live processes");
|
2019-07-29 22:41:33 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
size_t trace_capacity = 1;
|
|
|
|
struct drgn_stack_trace *trace =
|
|
|
|
malloc(offsetof(struct drgn_stack_trace,
|
|
|
|
frames[trace_capacity]));
|
|
|
|
if (!trace)
|
|
|
|
return &drgn_enomem;
|
2019-10-18 10:42:02 +01:00
|
|
|
trace->prog = prog;
|
|
|
|
trace->num_frames = 0;
|
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
struct drgn_cfi_row *row = drgn_empty_cfi_row;
|
|
|
|
|
|
|
|
struct drgn_register_state *regs;
|
2021-11-19 00:46:59 +00:00
|
|
|
err = drgn_get_initial_registers(prog, tid, obj, prstatus, ®s);
|
2021-03-10 09:51:33 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2019-07-29 22:41:33 +01:00
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
/* Limit iterations so we don't get caught in a loop. */
|
|
|
|
for (int i = 0; i < 1024; i++) {
|
2020-12-02 19:24:05 +00:00
|
|
|
err = drgn_stack_trace_add_frames(&trace, &trace_capacity,
|
|
|
|
regs);
|
|
|
|
if (err)
|
2021-03-10 09:51:33 +00:00
|
|
|
goto out;
|
2019-10-18 10:42:02 +01:00
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
err = drgn_unwind_with_cfi(prog, &row, regs, ®s);
|
|
|
|
if (err == &drgn_not_found) {
|
|
|
|
err = prog->platform.arch->fallback_unwind(prog, regs,
|
|
|
|
®s);
|
|
|
|
}
|
|
|
|
if (err == &drgn_stop)
|
|
|
|
break;
|
|
|
|
else if (err)
|
|
|
|
goto out;
|
2019-10-18 10:42:02 +01:00
|
|
|
}
|
2019-07-29 22:41:33 +01:00
|
|
|
|
2021-03-10 09:51:33 +00:00
|
|
|
err = NULL;
|
|
|
|
out:
|
|
|
|
drgn_cfi_row_destroy(row);
|
|
|
|
if (err) {
|
|
|
|
drgn_stack_trace_destroy(trace);
|
|
|
|
} else {
|
|
|
|
drgn_stack_trace_shrink_to_fit(&trace, trace_capacity);
|
|
|
|
*ret = trace;
|
|
|
|
}
|
2019-07-29 22:41:33 +01:00
|
|
|
return err;
|
|
|
|
}
|
2019-10-26 00:11:14 +01:00
|
|
|
|
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
|
|
|
drgn_program_stack_trace(struct drgn_program *prog, uint32_t tid,
|
|
|
|
struct drgn_stack_trace **ret)
|
|
|
|
{
|
2021-11-19 00:46:59 +00:00
|
|
|
return drgn_get_stack_trace(prog, tid, NULL, NULL, ret);
|
2019-10-26 00:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
|
|
|
drgn_object_stack_trace(const struct drgn_object *obj,
|
|
|
|
struct drgn_stack_trace **ret)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
|
|
|
|
if (drgn_type_kind(drgn_underlying_type(obj->type)) == DRGN_TYPE_INT) {
|
|
|
|
union drgn_value value;
|
|
|
|
|
|
|
|
err = drgn_object_read_integer(obj, &value);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2020-08-27 19:26:39 +01:00
|
|
|
return drgn_get_stack_trace(drgn_object_program(obj),
|
2021-11-19 00:46:59 +00:00
|
|
|
value.uvalue, NULL, NULL, ret);
|
2019-10-26 00:11:14 +01:00
|
|
|
} else {
|
2020-08-27 19:26:39 +01:00
|
|
|
return drgn_get_stack_trace(drgn_object_program(obj), 0, obj,
|
2021-11-19 00:46:59 +00:00
|
|
|
NULL, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBDRGN_PUBLIC struct drgn_error *
|
|
|
|
drgn_thread_stack_trace(struct drgn_thread *thread,
|
|
|
|
struct drgn_stack_trace **ret)
|
|
|
|
{
|
|
|
|
if (thread->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
|
|
|
|
struct nstring *prstatus =
|
|
|
|
thread->prstatus.str ? &thread->prstatus : NULL;
|
|
|
|
return drgn_get_stack_trace(thread->prog, thread->tid,
|
|
|
|
&thread->object, prstatus, ret);
|
|
|
|
} else {
|
|
|
|
return drgn_get_stack_trace(thread->prog, thread->tid, NULL,
|
|
|
|
&thread->prstatus, ret);
|
2019-10-26 00:11:14 +01:00
|
|
|
}
|
|
|
|
}
|