Is there a way to get the stacktraces for all threads in c#, like java.lang.Thread.getAllStackTraces()?

后端 未结 5 632
遥遥无期
遥遥无期 2020-12-08 10:20

In java it is possible to get a snapshot of the stacktraces of all running threads. This is done with java.lang.Thread.getAllStackTraces() (it returns Map

5条回答
  •  离开以前
    2020-12-08 10:29

    If you want this for debugging purposes alone, the SOS extensions to WinDbg can give you this information.

    The command to run is "*~e !clrstack".

    Inside of a running C# program, there is no public way to enumerate managed threads or look them up by ID. Even if you could, getting a stack trace on a different thread would likely require it to be suspended, which has some risks of side effects (see why this is obsolete).

    The other alternative is to enlist threads as they are known, and scan them at your leisure. This is probably only possible if you're explicitly creating thread objects rather than using the thread pool.

    That said, it is also hard for me to see what purpose this approach would serve. If it is for debugging, there are far more powerful techniques that can be done in-memory or on mini-dumps. If it is for logging, then it might make sense to have logging calls contribute their own stacks.

提交回复
热议问题