在调试器中创建std :: string

时间:2016-02-08 18:58:07

标签: c++ windbg

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

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

此函数接收字符串标记以验证客户端。

我希望能够在调试器中测试它,但为了这样做,我必须创建一个std::string所以我可以执行命令:.call isValidToken(<addr_of_string>)

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

我能够劫持其他字符串并对其进行更改以便我可以进行测试,但它有时会使程序崩溃。我正在尝试为类找到一个静态构造函数,但它确实很难,因为它主要基于模板。

1 个答案:

答案 0 :(得分:4)

通过在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
    
  4. 之后我们有一个好的std::string对象(0x03fe0000)可以使用,其中包含文字"my string here"