Important: Please read the Qt Code of Conduct -

Get stack trace

  • Hi.

    I have an error on my application, it doesn't crash, it just calls a function when it shouldn't, but this function is called from many places and I want to kwnow where is this function called, so I want to save the stack trace in a file.

    Is there any way to get the stack trace?


  • Hi and welcome to devnet.

    As often, that depends :-)
    If you are debugguing the application, you can just set a breakpoint.
    Otherwise it depends on the system you are on.

  • Hi Gerolf.

    I saw debuging tools in Qt creator are great!, but in this case I can't debug, it's an occasional error we can't reproduce any time we want, and it's a remote device and debuging does application go very slow and it needs to send frames in high frequency. I can execute my application compiled in debug mode.
    I would like to print stack trace like debuging does it.
    I'm using Linux (Debian 7.6).


  • AFAIK, that is not possible without instrumenting your application. A debugger would do that, but you could also create your own. It is a PITA though.

    We've build some code that we can use for similar cases. The idea is that you keep around a thread-global stack of function names. In every function that you wish to include in your stack, you make sure that at the start of that function (or scope, if you wish) you push the name of the function on to the stack, and you pop it off again on exit of the function. A RAII approach for making sure the pop actually happens is recommended.

    You'll need to put such a statement everywhere where you suspect code may pass that eventually leads to your wrong function call. At the location of the wrong function call, you can then dump the contents of the stack to a log file.

  • It is possible go backtrace the stack but it's not trivial. I have an open issue here to do that, and IIRC googleing for linux backtrace stackwalk gave good results on that. But it's not a trivial thing....

  • Interesting. But it sounds like something that is really dependent on the compiler and its settings. It might work for some very specific cases, but AFAIK, there are no provisions for this kind of introspection anywhere in the C++ standard...

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What about inspecting it with "GammaRay": ?

  • Ok.

    I'll create my own, using QVector/QList will be easy.

    Thank you

  • Better use QStack as your basis, I'd say.

Log in to reply