Skip to content

Commit

Permalink
Add stackwalker snippets from docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hainest committed Jan 25, 2024
1 parent 1d38649 commit b0c7794
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
12 changes: 12 additions & 0 deletions stackwalker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
43 changes: 43 additions & 0 deletions stackwalker/complex3rdPartyWalk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "frame.h"
#include "procstate.h"
#include "walker.h"

#include <thread>
#include <vector>

#include <sys/select.h>

// Collect a stack walk every five seconds.

namespace sw = Dyninst::Stackwalker;

void walk(Dyninst::PID pid) {
auto* walker = sw::Walker::newWalker(pid);
std::vector<sw::Frame> 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;
}
}
}
}
17 changes: 17 additions & 0 deletions stackwalker/determineWalkerParty.cpp
Original file line number Diff line number Diff line change
@@ -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<sw::ProcDebug*>(walker->getProcessState());

if(debugger) {
//3rd party walker
} else {
//1st party walker
}
}
20 changes: 20 additions & 0 deletions stackwalker/simple3rdPartyWalk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "frame.h"
#include "walker.h"

#include <vector>
#include <thread>

// Collect a stack walk every five seconds.

namespace sw = Dyninst::Stackwalker;

void walk(Dyninst::PID pid) {
auto* walker = sw::Walker::newWalker(pid);
std::vector<sw::Frame> swalk;
for(;;) {
walker->walkStack(swalk);

using namespace std::chrono_literals;
std::this_thread::sleep_for(5s);
}
}

0 comments on commit b0c7794

Please sign in to comment.