句柄分配在哪里?

发布于 2024-08-02 06:19:08 字数 943 浏览 4 评论 0原文

我想知道是否可以使用 WinDbg 来了解导致分配句柄的调用堆栈。

例如:

#include <windows.h>
#include <conio.h>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Press ENTER to leak handles." << endl;

    _getch();

    cout << "Leaking handles" << endl;

    for (int i = 0; i < 100; ++i)
    {
        HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL);
        if (h != NULL)
        {
            cout << ".";
        }
    }

    cout << "Handles leaked. Press ENTER to exit." << endl;

    _getch();

    return 0;
}

在构建此示例并在 WinDbg 中启动它之后,是否可以在行上方的示例中获取分配句柄的调用堆栈:

HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL);

我正在使用 !handle 命令进行探索,但没有到目前为止的进展。

这与处理泄漏分析有关。我知道 !htrace -enable!htrace -diff 但这是一个不同的使用场景(除非有某种方法来组合或其他使用向量,请提供资料)。

I am wondering if it is possible to use WinDbg to kwown the callstack that lead to the allocation of a handle.

For example:

#include <windows.h>
#include <conio.h>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Press ENTER to leak handles." << endl;

    _getch();

    cout << "Leaking handles" << endl;

    for (int i = 0; i < 100; ++i)
    {
        HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL);
        if (h != NULL)
        {
            cout << ".";
        }
    }

    cout << "Handles leaked. Press ENTER to exit." << endl;

    _getch();

    return 0;
}

After building this sample and firing it up in WinDbg is it possible to get the callstack that allocated the handles, in the sample above the line:

HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL);

I am poking around with the !handle command but no progress so far.

This is pertinent to handle leak analysis. I am aware of !htrace -enable and !htrace -diff but this is a different usage scenario (unless there is some way to combine or other usage vector for it, please provide information).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

邮友 2024-08-09 06:19:08

找到了似乎是解决方案:

  1. 使用 !htrace -enable 启用跟踪
  2. 运行程序并等待句柄泄漏
  3. 检查程序的句柄并使用 !htrace 进行分析的峰值之一 操作:
0:001> !htrace -enable
Handle tracing enabled.
Handle tracing information snapshot successfully taken.
0:001> g
0:001> !handle
...

Handle 7d8
  Type          Event
...
111 Handles
Type            Count
Event           103
File            3
Port            1
Directory       2
WindowStation   1
KeyedEvent      1
0:001> !htrace 7d8
--------------------------------------
Handle = 0x000007d8 - OPEN
Thread ID = 0x00000fc4, Process ID = 0x000017a8

0x0040106d: TestMemHandleLeak!wmain+0x0000006d
0x0040151b: TestMemHandleLeak!__tmainCRTStartup+0x0000010f
0x7c817077: kernel32!BaseProcessStart+0x00000023

--------------------------------------
Parsed 0x64 stack traces.
Dumped 0x1 stack traces.

要获取该地址处的代码行,我执行了以下

0:001> ln TestMemHandleLeak!wmain+0x0000006d
f:\temp\windowsapplication3\testmemhandleleak\testmemhandleleak.cpp(22)

Found what seems to be a solution:

  1. Enable traces by using !htrace -enable
  2. Run the program and wait for handle leaks
  3. Check the handles of the program and peak one for analysis with !htrace <handle>
0:001> !htrace -enable
Handle tracing enabled.
Handle tracing information snapshot successfully taken.
0:001> g
0:001> !handle
...

Handle 7d8
  Type          Event
...
111 Handles
Type            Count
Event           103
File            3
Port            1
Directory       2
WindowStation   1
KeyedEvent      1
0:001> !htrace 7d8
--------------------------------------
Handle = 0x000007d8 - OPEN
Thread ID = 0x00000fc4, Process ID = 0x000017a8

0x0040106d: TestMemHandleLeak!wmain+0x0000006d
0x0040151b: TestMemHandleLeak!__tmainCRTStartup+0x0000010f
0x7c817077: kernel32!BaseProcessStart+0x00000023

--------------------------------------
Parsed 0x64 stack traces.
Dumped 0x1 stack traces.

And to get the line of code at that address I did:

0:001> ln TestMemHandleLeak!wmain+0x0000006d
f:\temp\windowsapplication3\testmemhandleleak\testmemhandleleak.cpp(22)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文