From e56703922770203b7854c3389b4e1f8a8b2027eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Fri, 6 Apr 2018 17:36:16 +0200 Subject: [PATCH] Add a threaded test (harder to check unwinding end) --- test/Makefile | 12 +++---- test/dump_my_threads.cpp | 70 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 test/dump_my_threads.cpp diff --git a/test/Makefile b/test/Makefile index 6d7c41b..ac34468 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,19 +1,19 @@ CXX=g++ -CXXFLAGS=-Wall -Wextra -O0 -g --std=c++14 -CXXLIBS=-ldwarfpp -ldwarf -lelf -lc++fileno -ldwarfinterpret +CXXFLAGS=-Wall -Wextra -O0 -g --std=c++14 -rdynamic +CXXLIBS=-ldwarfpp -ldwarf -lelf -lc++fileno -ldwarfinterpret -ldl -lpthread CXXDIRS=-I../include -L../lib +CPP_FILES := $(shell ls *.cpp) +BINS := $(CPP_FILES:.cpp=.bin) + ############################################################################### -all: $(TARGET) +all: $(BINS) %.bin: %.o $(CXX) $(CXXFLAGS) $(CXXDIRS) $(CXXLIBS) $< -o "$@" -dump_my_stack.bin: dump_my_stack.o - $(CXX) $(CXXFLAGS) $(CXXDIRS) $(CXXLIBS) -ldl -rdynamic $< -o "$@" - %.o: %.cpp $(CXX) $(CXXFLAGS) $(CXXDIRS) -c "$<" -o "$@" diff --git a/test/dump_my_threads.cpp b/test/dump_my_threads.cpp new file mode 100644 index 0000000..ffb201f --- /dev/null +++ b/test/dump_my_threads.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +using namespace std; + +volatile int optim_stopper = 0; + +void dump_my_stack(int thread_id) { + DwarfInterpret& dw = DwarfInterpret::acquire(); + + DwarfInterpret::UnwindContext unw_context = + DwarfInterpret::get_current_unwind_context(); + MemoryMap mmap; + + while(true) { + Dl_info dl_inf; + int dl_rc = dladdr((void *) unw_context.rip, &dl_inf); + + printf("[%d] >> PC = %lX ", thread_id, unw_context.rip); + MemoryMap::MapEntry cur_map_entry = + mmap[mmap.id_of_address(unw_context.rip)]; + uintptr_t inelf_pc = unw_context.rip + - cur_map_entry.mem_region.begin + cur_map_entry.offset; + + printf("(in ELF: 0x%lX, func %s, path %s) <<\n", + inelf_pc, + (dl_rc && dl_inf.dli_sname) ? dl_inf.dli_sname : "(no symbol)", + cur_map_entry.pathname.c_str()); + fflush(stdout); + try { + unw_context = dw.unwind_context(unw_context); + } catch(...) { + // Assume we're reached _start + return; + } + } +} + +void fill_my_stack(int stack_depth, int thread_id) { + if(stack_depth == 0) { + dump_my_stack(thread_id); + return; + } + + std::this_thread::sleep_for(100ms); + fill_my_stack(stack_depth - 1, thread_id); +} + +int main(int argc, char** argv) { + MemoryMap mmap; + cout << "Dumping memory map… (" << mmap.size() << " entries)" << endl; + for(const auto& entry: mmap) { + entry.dump(cout); + } + cout << "End memory map" << endl; + int stack_depth = 5; + if(argc >= 2) + stack_depth = atoi(argv[1]); + + fill_my_stack(1, 0); + + std::thread th_stackwalk1(fill_my_stack, stack_depth, 1); + std::thread th_stackwalk2(fill_my_stack, stack_depth - 1, 2); + th_stackwalk1.join(); + th_stackwalk2.join(); + + return 0; +}