Search code examples
linuxmultithreadingstack-tracecallstackpstack

how to see stack trace for a particular thread within a hanged process in Linux


I have a multi-threaded process that got stuck while running under Linux. But I don't have multi-threaded version to pstack (made alias with gstack). As such, gstack <pid> does not provide me anything. How can I (a) attach gdb to an already running process (b) get stack trace of individual threads (c) know threadids of a running process (d) from threadid (if known) how can I see the stack trace / call stack for a running thread under stuck process?


Solution

  • At first, compile it with options -O0 -ggdb for best debugging.

    If you can run in under gdb, just run

       # gdb ./yourbinary
    

    And just wait problem. When process get stuck, just press ^C and you in.

    If you can't run under gdb, just:

       # gdb --pid=<YOUR PID>
    

    At both cases, type info threads and you will see all your threads. Then select one and you can see bt or anything else.