Remove necessity to copy the stack

This commit is contained in:
Théophile Bastian 2018-04-06 16:38:24 +02:00
parent c4895ed0d1
commit 36096575ed
6 changed files with 6 additions and 82 deletions

View file

@ -1,6 +1,6 @@
LIB_DIR=lib LIB_DIR=lib
TARGET=$(LIB_DIR)/libdwarfinterpret.so TARGET=$(LIB_DIR)/libdwarfinterpret.so
SRC=src/DwarfInterpret.cpp src/MemoryMap.cpp src/StackDump.cpp SRC=src/DwarfInterpret.cpp src/MemoryMap.cpp
INCLUDE_DIR=include INCLUDE_DIR=include

View file

@ -12,7 +12,6 @@
#include <dwarfpp/root.hpp> #include <dwarfpp/root.hpp>
#include "MemoryMap.hpp" #include "MemoryMap.hpp"
#include "StackDump.hpp"
#define OF_WHAT_EXCEPTION(cl_name) \ #define OF_WHAT_EXCEPTION(cl_name) \
cl_name: public WhatException { \ cl_name: public WhatException { \
@ -95,10 +94,7 @@ class DwarfInterpret {
/// An unwind context, holding registers /// An unwind context, holding registers
struct UnwindContext { struct UnwindContext {
UnwindContext(const StackDump& dump): stack(dump) {}
// Let's pretend this is enough // Let's pretend this is enough
StackDump stack;
uintptr_t rip; uintptr_t rip;
uintptr_t rsp; uintptr_t rsp;
uintptr_t rbp; uintptr_t rbp;

View file

@ -1,28 +0,0 @@
#pragma once
#include <memory>
#include <cstdint>
#include <dwarfinterpret/MemoryMap.hpp>
class StackDump {
public:
static StackDump snapshot(uintptr_t rsp); ///< Take an instant snapshot
StackDump(const StackDump& oth); ///< copy
StackDump& operator=(const StackDump& oth); ///< copy
template <typename T> T deref(uintptr_t pos) const {
return *((T*)(stack.get() + pos - offset));
}
uintptr_t at(uintptr_t pos) const {
return deref<uintptr_t>(pos);
}
private:
StackDump();
typedef char cell_t;
std::shared_ptr<cell_t> stack;
uintptr_t offset; ///< such that stack[stack_addr - offset] is ok
};

View file

@ -159,7 +159,7 @@ DwarfInterpret::reg_content_t DwarfInterpret::interpret_dw_register(
interpret_dw_register(row, DW_FRAME_CFA_COL3, ctx); interpret_dw_register(row, DW_FRAME_CFA_COL3, ctx);
int cfa_offset = reg.saved_at_offset_from_cfa_r(); int cfa_offset = reg.saved_at_offset_from_cfa_r();
reg_content_t addr = cfa_loc + cfa_offset; reg_content_t addr = cfa_loc + cfa_offset;
reg_content_t value = ctx.stack.deref<reg_content_t>(addr); reg_content_t value = *((reg_content_t*) addr);
cerr << "@@ Interpreting CFA offset: CFA is " << hex cerr << "@@ Interpreting CFA offset: CFA is " << hex
<< cfa_loc << " + offset " << dec << cfa_offset << hex << cfa_loc << " + offset " << dec << cfa_offset << hex
<< " = " << addr << " = " << addr
@ -193,12 +193,10 @@ DwarfInterpret::UnwindContext DwarfInterpret::get_current_unwind_context() {
// context snapshot naively taken from inside this function). Unwinding // context snapshot naively taken from inside this function). Unwinding
// it some number of times should yield the expected context // it some number of times should yield the expected context
uintptr_t rsp;
get_cpu_register(REG_RSP, rsp);
UnwindContext ctx(StackDump::snapshot(rsp)); UnwindContext ctx;
get_cpu_register(REG_RIP, ctx.rip); get_cpu_register(REG_RIP, ctx.rip);
ctx.rsp = rsp; get_cpu_register(REG_RSP, ctx.rsp);
get_cpu_register(REG_RBP, ctx.rbp); get_cpu_register(REG_RBP, ctx.rbp);
cerr << "CREATING CONTEXT. %rsp=0x" << hex cerr << "CREATING CONTEXT. %rsp=0x" << hex
@ -222,7 +220,7 @@ DwarfInterpret::UnwindContext DwarfInterpret::unwind_context(
DwarfRow cur_row = dwarf_row_at(ctx.rip); DwarfRow cur_row = dwarf_row_at(ctx.rip);
const core::Cie& cie = *cie_at(ctx.rip); const core::Cie& cie = *cie_at(ctx.rip);
UnwindContext new_context(ctx.stack); UnwindContext new_context;
cerr << "Obtaining previous context as reg " cerr << "Obtaining previous context as reg "
<< cie.get_return_address_register_rule() << cie.get_return_address_register_rule()
<< " at current IP = " << " at current IP = "

View file

@ -1,40 +0,0 @@
#include <dwarfinterpret/StackDump.hpp>
#include <dwarfinterpret/MemoryMap.hpp>
#include <cassert>
#include <iostream> // FIXME
#include <cstring>
using namespace std;
StackDump StackDump::snapshot(uintptr_t rsp) {
StackDump stack;
MemoryMap memory_map;
const MemoryMap::MapEntry& stack_region =
memory_map[memory_map.id_of_address(rsp)];
assert(stack_region.pathname == "[stack]");
size_t stack_size = stack_region.mem_region.end - rsp;
stack.stack = std::shared_ptr<cell_t>(new cell_t[stack_size]);
cerr << "memcpy'ing " << stack_size << " bytes" << endl;
memcpy(stack.stack.get(), (void*)rsp, stack_size); // FIXME way too brutal
stack.offset = rsp;
return stack;
}
StackDump::StackDump()
: stack(nullptr), offset(0)
{}
StackDump::StackDump(const StackDump& oth) {
this->operator=(oth);
}
StackDump& StackDump::operator=(const StackDump& oth) {
stack = oth.stack;
offset = oth.offset;
return *this;
}

View file

@ -4,8 +4,6 @@ CXXLIBS=-ldwarfpp -ldwarf -lelf -lc++fileno -ldwarfinterpret
CXXDIRS=-I../include -L../lib CXXDIRS=-I../include -L../lib
OBJS = $(SRC:.cpp=.o)
############################################################################### ###############################################################################
all: $(TARGET) all: $(TARGET)
@ -22,4 +20,4 @@ dump_my_stack.bin: dump_my_stack.o
.PRECIOUS: %.o .PRECIOUS: %.o
clean: clean:
rm -f $(OBJS) $(TARGET) rm -f *.o *.bin