From 36096575ed213a491e60b822ec84e2365a2de0d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Fri, 6 Apr 2018 16:38:24 +0200 Subject: [PATCH] Remove necessity to copy the stack --- Makefile | 2 +- include/dwarfinterpret/DwarfInterpret.hpp | 4 --- include/dwarfinterpret/StackDump.hpp | 28 ---------------- src/DwarfInterpret.cpp | 10 +++--- src/StackDump.cpp | 40 ----------------------- test/Makefile | 4 +-- 6 files changed, 6 insertions(+), 82 deletions(-) delete mode 100644 include/dwarfinterpret/StackDump.hpp delete mode 100644 src/StackDump.cpp diff --git a/Makefile b/Makefile index b500bbc..e9220e2 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ LIB_DIR=lib 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 diff --git a/include/dwarfinterpret/DwarfInterpret.hpp b/include/dwarfinterpret/DwarfInterpret.hpp index 13122ff..1a9bd44 100644 --- a/include/dwarfinterpret/DwarfInterpret.hpp +++ b/include/dwarfinterpret/DwarfInterpret.hpp @@ -12,7 +12,6 @@ #include #include "MemoryMap.hpp" -#include "StackDump.hpp" #define OF_WHAT_EXCEPTION(cl_name) \ cl_name: public WhatException { \ @@ -95,10 +94,7 @@ class DwarfInterpret { /// An unwind context, holding registers struct UnwindContext { - UnwindContext(const StackDump& dump): stack(dump) {} - // Let's pretend this is enough - StackDump stack; uintptr_t rip; uintptr_t rsp; uintptr_t rbp; diff --git a/include/dwarfinterpret/StackDump.hpp b/include/dwarfinterpret/StackDump.hpp deleted file mode 100644 index 2686378..0000000 --- a/include/dwarfinterpret/StackDump.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include - -#include - -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 T deref(uintptr_t pos) const { - return *((T*)(stack.get() + pos - offset)); - } - uintptr_t at(uintptr_t pos) const { - return deref(pos); - } - - private: - StackDump(); - - typedef char cell_t; - std::shared_ptr stack; - uintptr_t offset; ///< such that stack[stack_addr - offset] is ok -}; diff --git a/src/DwarfInterpret.cpp b/src/DwarfInterpret.cpp index fceaa9e..a2a733f 100644 --- a/src/DwarfInterpret.cpp +++ b/src/DwarfInterpret.cpp @@ -159,7 +159,7 @@ DwarfInterpret::reg_content_t DwarfInterpret::interpret_dw_register( interpret_dw_register(row, DW_FRAME_CFA_COL3, ctx); int cfa_offset = reg.saved_at_offset_from_cfa_r(); reg_content_t addr = cfa_loc + cfa_offset; - reg_content_t value = ctx.stack.deref(addr); + reg_content_t value = *((reg_content_t*) addr); cerr << "@@ Interpreting CFA offset: CFA is " << hex << cfa_loc << " + offset " << dec << cfa_offset << hex << " = " << addr @@ -193,12 +193,10 @@ DwarfInterpret::UnwindContext DwarfInterpret::get_current_unwind_context() { // context snapshot naively taken from inside this function). Unwinding // 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); - ctx.rsp = rsp; + get_cpu_register(REG_RSP, ctx.rsp); get_cpu_register(REG_RBP, ctx.rbp); cerr << "CREATING CONTEXT. %rsp=0x" << hex @@ -222,7 +220,7 @@ DwarfInterpret::UnwindContext DwarfInterpret::unwind_context( DwarfRow cur_row = dwarf_row_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 " << cie.get_return_address_register_rule() << " at current IP = " diff --git a/src/StackDump.cpp b/src/StackDump.cpp deleted file mode 100644 index 6e3bbce..0000000 --- a/src/StackDump.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include - -#include -#include -#include // FIXME -#include - -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(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; -} diff --git a/test/Makefile b/test/Makefile index 56c9fa0..6d7c41b 100644 --- a/test/Makefile +++ b/test/Makefile @@ -4,8 +4,6 @@ CXXLIBS=-ldwarfpp -ldwarf -lelf -lc++fileno -ldwarfinterpret CXXDIRS=-I../include -L../lib -OBJS = $(SRC:.cpp=.o) - ############################################################################### all: $(TARGET) @@ -22,4 +20,4 @@ dump_my_stack.bin: dump_my_stack.o .PRECIOUS: %.o clean: - rm -f $(OBJS) $(TARGET) + rm -f *.o *.bin