在调试器中创建 std::string

Create std::string inside debugger

本文关键字:string std 创建 调试器      更新时间:2023-10-16

我正在WinDbg中调试一个x86程序(用C++/VS2012/静态链接编写(,我有它的目标文件。我的兴趣点是这个功能:

static bool isValidToken(const std::string& token)

此函数接收字符串令牌以验证客户端。

我希望能够在调试器中对其进行测试,但要做到这一点,我必须创建一个std::string,以便我可以执行命令:.call isValidToken(<addr_of_string>) .

在 WinDbg 中转储和操作std::string相对容易,但是否可以创建它?

我可以劫持其他字符串并更改它以便我可以进行测试,但它有时会使程序崩溃。我正在尝试为该类找到一个静态构造函数,但这真的很难,因为它在很大程度上基于模板。

通过在Visual Studio中调试测试程序(@cdonts在注释中建议(,我可以找到std::string的构造函数原型。它显示在下面的命令中。

回到 WinDbg 我发出以下命令来查找具有该签名的符号(请注意,*用作通配符来替换空格(:

0:047> x Manager!std::basic_string<char,std::char_traits<char>,std::allocator<char>*>::basic_string<char,std::char_traits<char>,std::allocator<char>*>

找到以下构造函数:

6e36bf96 Manager!std::basic_string<...PROTOTYPE...> (char *, char *)
6e67fa65 Manager!std::basic_string<...PROTOTYPE...> (class std::basic_string<...PROTOTYPE...> *, int, int)
6d519218 Manager!std::basic_string<...PROTOTYPE...> (class std::_String_const_iterator<...PROTOTYPE...>)
6d54c745 Manager!std::basic_string<...PROTOTYPE...> (char *, unsigned int)
6d0c2666 Manager!std::basic_string<...PROTOTYPE...> (char *)
6d1f2a43 Manager!std::basic_string<...PROTOTYPE...> (class std::basic_string<...PROTOTYPE...> *)
6d151eb8 Manager!std::basic_string<...PROTOTYPE...> (class std::basic_string<...PROTOTYPE...> *)

我省略了原型的某些部分,但我们感兴趣的是:

6d0c2666 Manager!std::basic_string<...PROTOTYPE...> (char *)

这个只用一个char *作为论据。它用于初始化新创建的字符串,并且非常容易提供。因此,完成这项工作的步骤是:

  1. 为对象分配内存 ( std::string (。我们使用 1000,因为它是最小分配大小:

    0:047> .dvalloc 1000
    Allocated 1000 bytes starting at 03fe0000
    
  2. char * 参数分配缓冲区:

    0:047> .dvalloc 1000
    Allocated 1000 bytes starting at 03ff0000
    

    我们可以用以下方法初始化缓冲区:

    0:047> ea 0x03ff0000 "my string here"
    
  3. 放置一个传递两个参数的.call命令:第一个是我们为对象本身分配的内存地址,这实际上恰好是一个this参数,因为函数使用调用约定thiscall(WinDbg 知道它并将其放在 ecx 中(。第二个是构造函数的char *参数:

    0:048> .call 6d0c2666(0x03fe0000, 0x03ff0000)
    Thread is set up for call, 'g' will execute.
    WARNING: This can have serious side-effects,
    including deadlocks and corruption of the debuggee.
    0:048> g
    

之后,我们有一个很好的std::string对象(在 0x03fe0000 (可以使用,包含文本"my string here" .