如何在Windows的CDB中查看std::filesystem::path变量的值?
在Windows CDB中查看std::filesystem::path变量值的方法
我正在为Descent3项目贡献代码,当前处理的PR存在Windows专属bug,调试时需要查看std::filesystem::path变量的值。Linux下使用GDB可以直接查看,但Windows下使用CDB时,尝试了dv命令、??命令、调用string()方法以及查看_Text内部成员均未成功获取有效信息,以下是可行的解决方法:
Linux下GDB操作示例
(gdb) p example_path $1 = filesystem::path "foo/bar/baz" = {[0] = "foo", [1] = "bar", [2] = "baz"} (gdb)
已尝试的CDB操作
0:000> dv example_path example_path = class std::filesystem::path 0:000>
0:000> ?? example_path class std::filesystem::path +0x000 _Text : std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > 0:000>
0:000> ?? example_path.string() Type does not have given member error at 'string()' 0:000>
0:000> ?? example_path class std::filesystem::path +0x000 _Text : std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > 0:000> ?? example_path._Text class std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > +0x000 _Mypair : std::_Compressed_pair<std::allocator<wchar_t>,std::_String_val<std::_Simple_types<wchar_t> >,1> 0:000> ?? example_path._Text._MyPair Type does not have given member error at '_MyPair' 0:000>
可行的解决方法
1. 正确访问std::wstring的底层成员
MSVC编译的std::wstring成员名是大小写敏感的,你之前输入的_MyPair应为_Mypair(小写p)。通过以下命令逐层解析:
0:000> ?? example_path._Text._Mypair._Myval2._Bx._Buf
如果是长字符串,内容会存在_Ptr中:
0:000> ?? example_path._Text._Mypair._Myval2._Bx._Ptr
得到地址后,用du命令查看宽字符串内容:
0:000> du 0xXXXXXXXX // 替换为上述命令得到的内存地址
2. 调用wstring()方法
Windows下std::filesystem::path默认使用宽字符,应调用wstring()而非string():
0:000> ?? example_path.wstring()
注意:确保编译时生成了完整调试符号(如Debug模式,或Release模式带符号),否则可能无法识别成员函数。
3. 使用dx命令解析对象
CDB的dx命令可以更友好地解析C++对象结构,直接尝试:
0:000> dx example_path
或者展开_Text成员:
0:000> dx example_path._Text
该命令会自动解析对象,大概率直接显示字符串内容。
4. 手动解析内存布局
如果以上方法失效,可直接查看_Text的内存。MSVC的std::wstring内存布局通常包含:
- 前4字节:字符串长度(不含终止符)
- 接下来4字节:字符串容量
- 后续:短字符串存在栈缓冲区
_Buf,长字符串存在堆指针_Ptr
先查看_Text的内存:
0:000> dd example_path._Text L10
找到宽字符串的起始地址后,用du命令查看内容。
内容的提问来源于stack exchange,提问作者Ginger Jesus
相关产品推荐
相关产品推荐

