How to know who kills my threads

后端 未结 15 702
南旧
南旧 2020-12-05 00:19

I got a thread that is just banishing.. i\'d like to know who is killing my thread and why.

It occurs to me my thread is being killed by the OS, but i\'d like to

15条回答
  •  失恋的感觉
    2020-12-05 00:52

    A potential way to get more information: attach a debugger and break on thread termination. Depending on how your thread is being terminated, this might not work.

    1. Download Debugging Tools for Windows if you don't already have it
    2. Run windbg.exe, attach to your process
    3. Break into windbg, type sxe et to enable breaking on thread exit
    4. When the debugger breaks, inspect the state of the system, other threads, etc.
    5. To get the managed stack, load sos.dll (.loadby sos mscorsvr, .loadby sos mscorwks, or .loadby sos clr should work), then run !clrstack (see !help for other sos commands)

    If you get a lot of noise from other threads exiting, script windbg to continue after breaking if it's not the thread ID you care about.

    Edit: If you think the thread is being terminated from within your process, you can also set a breakpoint on TerminateThread (bp kernel32!TerminateThread) and ExitThread (bp kernel32!ExitThread) to catch the stack of the killer.

提交回复
热议问题