Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain thread stack information WITHOUT pstack and gdb

Is there a way of obtaining information about why program got stuck, when not being able to insert any logs and not having pstack and gdb, on some very tiny linux?

like image 827
YotKay Avatar asked Aug 07 '26 03:08

YotKay


1 Answers

My suggestion is to do the following:

  1. Be able to retrieve manipulate and print a stack trace within your own program
  2. Install a signal handler (say, for SIGUSR1) for your process which would do that
  3. Send the signal to your program when it's stuck - control will be moved to the signal handler (assuming you haven't masked out signals).

Now (2.) and (3.) and pretty simple. For (3.) it's kill -USR1 1234 (for a process with ID 1234). For (2.), it's:

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t sigset(int sig, sighandler_t disp);

(read man sigset for details).

As for (1.), this used to be quite difficult and tricky, but now it's been essentially resolved:

Boost StackTrace: Code | Documentation

It will do the best possible to get as much stack trace info as is possible. A simple example of use:

#include <iostream>
#include <boost/stacktrace.hpp>

// ... somewhere inside the `bar(int)` function that is called recursively:
std::cout << boost::stacktrace::stacktrace();

may result in the following output:

0# bar(int) at /path/to/source/file.cpp:70
1# bar(int) at /path/to/source/file.cpp:70
2# bar(int) at /path/to/source/file.cpp:70
3# bar(int) at /path/to/source/file.cpp:70
4# main at /path/to/main.cpp:93
5# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
6# _start

Note: StackTrace is a very recent addition to Boost (as of August 2017); you might consider getting it independently if your Boost version is not recent enough.

like image 141
einpoklum Avatar answered Aug 09 '26 23:08

einpoklum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!