在调试器中创建 std::string
Create std::string inside debugger
我正在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 *
作为论据。它用于初始化新创建的字符串,并且非常容易提供。因此,完成这项工作的步骤是:
为对象分配内存 (
std::string
(。我们使用 1000,因为它是最小分配大小:0:047> .dvalloc 1000 Allocated 1000 bytes starting at 03fe0000
为
char *
参数分配缓冲区:0:047> .dvalloc 1000 Allocated 1000 bytes starting at 03ff0000
我们可以用以下方法初始化缓冲区:
0:047> ea 0x03ff0000 "my string here"
放置一个传递两个参数的
.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"
.
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 如何更改大小(std::string)
- std::string 的对象真的可以移动吗?
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- C++:考虑C++ std::string 中双引号 (") 的字面含义,而不使用反斜杠 (\)
- 'int main(int, int, std::__cxx11::string, std::__cxx11::string)'只需要零或两个参数 [-Wmain]
- C++ 在列表和列表之间选择返回类型<<string>std::p air<string,string>>
- std::string, std::wstring and UTF8
- uan inplace replacement of std::string/std::wstring?
- #define Glib::string std:wstring
- 从 std::string & std::wstring 中获取 char 整数值
- BSTR转换为std::string(std::wstring),反之亦然