From b0c779458221147ea55b1e60d5f06e593fb0931b Mon Sep 17 00:00:00 2001 From: Tim Haines Date: Thu, 25 Jan 2024 00:02:41 -0600 Subject: [PATCH] Add stackwalker snippets from docs --- stackwalker/CMakeLists.txt | 12 ++++++++ stackwalker/complex3rdPartyWalk.cpp | 43 ++++++++++++++++++++++++++++ stackwalker/determineWalkerParty.cpp | 17 +++++++++++ stackwalker/simple3rdPartyWalk.cpp | 20 +++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 stackwalker/complex3rdPartyWalk.cpp create mode 100644 stackwalker/determineWalkerParty.cpp create mode 100644 stackwalker/simple3rdPartyWalk.cpp diff --git a/stackwalker/CMakeLists.txt b/stackwalker/CMakeLists.txt index 253e1c5..b515cb7 100644 --- a/stackwalker/CMakeLists.txt +++ b/stackwalker/CMakeLists.txt @@ -3,3 +3,15 @@ project(stackwalker LANGUAGES CXX) add_executable(this_thread this_thread.cpp) target_compile_options(this_thread PRIVATE ${EXAMPLES_WARNING_FLAGS}) target_link_libraries(this_thread Dyninst::stackwalk) + +add_executable(determineWalkerParty determineWalkerParty.cpp) +target_compile_options(determineWalkerParty PRIVATE ${EXAMPLES_WARNING_FLAGS}) +target_link_libraries(determineWalkerParty Dyninst::stackwalk) + +add_library(simple3rdPartyWalk SHARED simple3rdPartyWalk.cpp) +target_compile_options(simple3rdPartyWalk PRIVATE ${EXAMPLES_WARNING_FLAGS}) +target_link_libraries(simple3rdPartyWalk Dyninst::stackwalk) + +add_library(complex3rdPartyWalk SHARED complex3rdPartyWalk.cpp) +target_compile_options(complex3rdPartyWalk PRIVATE ${EXAMPLES_WARNING_FLAGS}) +target_link_libraries(complex3rdPartyWalk Dyninst::stackwalk) diff --git a/stackwalker/complex3rdPartyWalk.cpp b/stackwalker/complex3rdPartyWalk.cpp new file mode 100644 index 0000000..ca04fa9 --- /dev/null +++ b/stackwalker/complex3rdPartyWalk.cpp @@ -0,0 +1,43 @@ +#include "frame.h" +#include "procstate.h" +#include "walker.h" + +#include +#include + +#include + +// Collect a stack walk every five seconds. + +namespace sw = Dyninst::Stackwalker; + +void walk(Dyninst::PID pid) { + auto* walker = sw::Walker::newWalker(pid); + std::vector swalk; + + struct timeval timeout; + timeout.tv_sec = 5; + timeout.tv_usec = 0; + + for(;;) { + walker->walkStack(swalk); + int max = 1; + fd_set readfds, writefds, exceptfds; + FD_ZERO(&readfds); + FD_ZERO(&writefds); + FD_ZERO(&exceptfds); + FD_SET(sw::ProcDebug::getNotificationFD(), &readfds); + + for(;;) { + int result = select(max, &readfds, &writefds, &exceptfds, &timeout); + if(FD_ISSET(sw::ProcDebug::getNotificationFD(), &readfds)) { + // Debug event + sw::ProcDebug::handleDebugEvent(); + } + if(result == 0) { + // Timeout + break; + } + } + } +} diff --git a/stackwalker/determineWalkerParty.cpp b/stackwalker/determineWalkerParty.cpp new file mode 100644 index 0000000..b7a9d3f --- /dev/null +++ b/stackwalker/determineWalkerParty.cpp @@ -0,0 +1,17 @@ +#include "walker.h" +#include "procstate.h" + +// How to determine if first-party or third-party walker + +namespace sw = Dyninst::Stackwalker; + +int main() { + auto* walker = sw::Walker::newWalker(); + auto* debugger = dynamic_cast(walker->getProcessState()); + + if(debugger) { + //3rd party walker + } else { + //1st party walker + } +} diff --git a/stackwalker/simple3rdPartyWalk.cpp b/stackwalker/simple3rdPartyWalk.cpp new file mode 100644 index 0000000..f6a4e1f --- /dev/null +++ b/stackwalker/simple3rdPartyWalk.cpp @@ -0,0 +1,20 @@ +#include "frame.h" +#include "walker.h" + +#include +#include + +// Collect a stack walk every five seconds. + +namespace sw = Dyninst::Stackwalker; + +void walk(Dyninst::PID pid) { + auto* walker = sw::Walker::newWalker(pid); + std::vector swalk; + for(;;) { + walker->walkStack(swalk); + + using namespace std::chrono_literals; + std::this_thread::sleep_for(5s); + } +}