我试图在Windows Performance Analyzer(WPA)(在Windows 8.1下)中显示线程的名称。此工具有一个名为"线程名称"的列。
我关注了著名的MSDN文章:
http://msdn.microsoft.com/en-US/library/xcb2z8hs(v=vs.110).aspx
但是,看起来它在 WPA 中不起作用。根据第三方文档,只有Microsoft的Visual Studio和WinDbg调试器支持此异常。
那么如何命名线程,使其名称可以在 WPA 中显示呢?
> 从 Windows 10 版本 1607 开始,您可以使用 SetThreadDescription() API,现在在 xperf/WPA 中受支持:
https://randomascii.wordpress.com/2015/10/26/thread-naming-in-windows-time-for-something-better/
您还可以在此处投票支持将其添加到其他Microsoft工具中:
https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/17608120-properly-support-native-thread-naming-via-the-sett
没有方便地安装 wpa,所以无法回答您的查询,但谢谢我从来没有想过这也可以使用本机代码的问题可以派上用场
#include <windows.h>
#include <stdio.h>
const DWORD MS_VC_EXCEPTION=0x406D1388;
//EmptyBlock,Constant in__except() and lpparam not used in ThreadProc
#pragma warning( disable : 6312 6322 4100 )
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO {
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
DWORD WINAPI ThreadProc( LPVOID lpParam ) {
int ch = 0; while(ch != 'y') { ch = getchar(); } return 0;}
void SetThreadName( DWORD dwThreadID, char* threadName) {
THREADNAME_INFO info; info.dwType = 0x1000; info.szName = threadName;
info.dwThreadID = dwThreadID; info.dwFlags = 0;
__try {
RaiseException( MS_VC_EXCEPTION, 0,
sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
}
__except( EXCEPTION_CONTINUE_EXECUTION) { }
}
void main (void) {
HANDLE hThread = NULL;
printf("nnn=======Creating Thread And Naming It================nnn");
if (( hThread = CreateThread(NULL,NULL,ThreadProc,NULL,NULL,NULL)) != NULL) {
SetThreadName(GetCurrentThreadId(), "nnMy New Shiny Threadnn");
WaitForSingleObject(hThread,INFINITE);
printf("Named Thread Terminated Main is terminatingn");
CloseHandle(hThread);
}
}
编译的链接和风袋 VCPP 事件似乎在 windBG 中处理此异常想知道 MAGIC Dword dbce 处理什么
dir /b
compile.bat
threadname.cpp
type compile.bat
@call "C:Program FilesMicrosoft Visual Studio 10.0VCvcvarsall.bat" x86
cl /Zi /nologo /W4 /analyze *.cpp /link /RELEASE
compile.bat
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
threadname.cpp
dir /b *.exe
threadname.exe
cdb -c "sxe -c "~*;gc;" vcpp;g;q" threadname.exe
0:000> cdb: Reading initial command 'sxe -c "~*;gc;" vcpp;g;q'
=================Creating Thread And Naming It===================
(f84.db4): Visual C++ exception - code 406d1388 (first chance)
. 0 Id: f84.db4 Suspend: 1 Teb: 7ffdf000 Unfrozen "
My New Shiny Thread
"
Start: threadname!mainCRTStartup (00401642)
Priority: 0 Priority class: 32 Affinity: 1
y
Named Thread Terminated Main is terminating
quit: