在进入Windbg后,是否有某种技巧可以在未命名的命名空间中查看变量的值?我找到了这样的变量:
0:000> x mod!ns::*myvar*
70f7afc6 mod!ns::`anonymous namespace'::`dynamic initializer for 'myvar'' (void)
717bb799 mod!ns::`anonymous namespace'::`dynamic atexit destructor for 'myvar'' (void)
71a00718 mod!ns::`anonymous namespace'::myvar = <no type information>
myvar
基本上是std::map
,并且有一个调试器可视化器(natvis)可供它使?? myvar
显示可用的东西。但是由于匿名命名空间介于两者之间,我似乎无法找出正确的语法来显示myvar
中的值。
有什么想法吗?
答案 0 :(得分:3)
说你的名字空间是嵌套的吗?
我假设是的,因为你在模块 mod!ns ::
之后有一个ns如果不是这种情况 x mymap 会产生可点击的dml链接 它会正确显示类型,如果它是嵌套的windbg会吐出没有可用的类型信息
在这种情况下,您可以使用像 mod!std :: map .......... xxx ...
这样的指针将它们转换为实际类型这是一个在windbg下编译和执行的人为源代码,它演示了一些可能性
第一个文件的来源包含未命名的ns和命名的嵌套未命名的ns,并且还引用包含匿名命名空间的另一个文件
#include "anonall.h"
namespace {
map<char, const char*> mymap;
const char *Greek_Alphabets[ALLOCSIZ] = { "Alp","Bet","Gam","Del","Eps" };
}
namespace ns {
namespace {
map<char, const char*> mymap;
const char *Greek_Alphabets[ALLOCSIZ]={
"Alpha","Beta","Gamma","Delta","Epsilon" };
}
}
void anonnsptwo(void);
void anonnsptre(void) {
cout << "\nfrom Anonymous NS Map in " << __FILE__ << " " << __LINE__ << "\n\n";
for (int i = 0; i < ALLOCSIZ; i++) {
mymap.insert(pair<char, const char*>(char('M' + i), Greek_Alphabets[i]));
}
map<char, const char*>::iterator iter = mymap.begin();
for (iter; iter != mymap.end(); iter++)
cout << iter->first << " = " << iter->second << "\n";
}
int main(void) {
anonnsptre();
cout << "\nfrom Anonymous NS Map in " << __FILE__ << " " << __LINE__ << "\n\n";
for (int i = 0; i < ALLOCSIZ; i++) {
ns::mymap.insert(pair<char, const char*>(char('A'+i),ns::Greek_Alphabets[i]));
}
map<char, const char*>::iterator iter = ns::mymap.begin();
for (iter; iter != ns::mymap.end(); iter++)
cout << iter->first << " = " << iter->second << "\n";
anonnsptwo();
return 0;
}
第一个文件引用的第二个文件的来源
#include "anonall.h"
namespace ns {
namespace {
map<char, const char*> mymap;
const char *Greek_Alphabets[ALLOCSIZ]={"Zeta","Eta","Theta","Iota","Kappa"};
}
}
void anonnsptwo(void) {
cout << "\nfrom Anonymous Namespace Map in " << __FILE__ << "\n\n";
for (int i = 0; i < ALLOCSIZ; i++) {
ns::mymap.insert(pair<char, const char*>(char('Z'-i),ns::Greek_Alphabets[i]));
}
map<char, const char*>::iterator iter = ns::mymap.begin();
for (iter; iter != ns::mymap.end(); iter++)
cout << iter->first << " = " << iter->second << "\n";
}
公共头文件内容
#pragma once
#include <iostream>
#include <map>
#define ALLOCSIZ 5
using namespace std;
使用vs社区2017(15.6.4)cmd提示编译并链接
:\>echo off
ls
anonall.h anonone.cpp anontwo.cpp
cl /Zi /W4 /analyze /EHsc /Od /nologo anonone.cpp anontwo.cpp /link /release /nologo
anonone.cpp
anontwo.cpp
Compiling...
Generating Code...
ls
anonall.h anonone.obj anontwo.obj
anonone.cpp anonone.pdb vc140.pdb
anonone.exe anontwo.cpp
anonone.nativecodeanalysis.xml anontwo.nativecodeanalysis.xml
执行二进制文件
anonone.exe
from Anonymous NS Map in anonone.cpp 15
M = Alp
N = Bet
O = Gam
P = Del
Q = Eps
from Anonymous NS Map in anonone.cpp 25
A = Alpha
B = Beta
C = Gamma
D = Delta
E = Epsilon
from Anonymous Namespace Map in anontwo.cpp
V = Kappa
W = Iota
X = Theta
Y = Eta
Z = Zeta
使用cdb只是为了在没有用户干预的情况下显示结果
如果使用windbg而不是cdb,x *!* mymap的结果将是可点击的链接
注意size = 5并注意另外两个没有类型信息的匿名mymaps
(关于如何打印这些mymaps的问题如下)
设置第一个文件的断点返回(程序结束)
grep -in ret。* anonone.cpp
33:返回0; &lt;&lt;行号是33
启用行无信息设置src行上的条件延迟中断,并按下该中断打印x命令和退出的结果
&gt; cdb -c“.lines; bu`anonone.cpp:33` \”x *!* mymap * \“; g; q”anonone.exe ë
cdb -c ".lines;bu `anonone.cpp:33` \"x *!*mymap*\";g;q" anonone.exe
Microsoft (R) Windows Debugger Version 10.0.16299.15 X86
0:000> cdb: Reading initial command '.lines;bu `anonone.cpp:33` "x *!*mymap*";g;q'
snipp======================
0118b938 anonone!mymap = { size=0x5 }
01175198 anonone!mymap$initializer$ = 0x01101040
011742c0 anonone!`anonymous namespace'::`dynamic atexit destructor for 'mymap'' (void)
01101040 anonone!`anonymous namespace'::`dynamic initializer for 'mymap'' (void)
011742e0 anonone!ns::`anonymous namespace'::`dynamic atexit destructor for 'mymap'' (void)
011742d0 anonone!ns::`anonymous namespace'::`dynamic atexit destructor for 'mymap'' (void)
01101080 anonone!ns::`anonymous namespace'::`dynamic initializer for 'mymap'' (void)
01101060 anonone!ns::`anonymous namespace'::`dynamic initializer for 'mymap'' (void)
0118b930 anonone!ns::`anonymous namespace'::mymap = <no type information>
0118b960 anonone!ns::`anonymous namespace'::mymap = <no type information>
quit:
你现在可以将mymap @ 0x0118b960和0x0118b930的地址同时转换为std :: map
(执行x anonone!std :: map命中选项卡选项卡以查找类型信息使其成为指针并粘贴地址并使用dx表达式求值程序获取显示)
就像 dx(anonone!std:map ...... xx..yy..zz *)0x0118b960
ntdll!LdrpDoDebuggerBreak+0x2c:
771d05a6 cc int 3
Processing initial command '$$>a< displayanon.txt'
0:000> $$>a< displayanon.txt
ModLoad: 6b450000 6b453000 C:\Windows\system32\api-ms-win-core-synch-l1-2-0.DLL
00d6b938 anonone!mymap = { size=0x5 }
00d55198 anonone!mymap$initializer$ = 0x00ce1040
00d542c0 anonone!`anonymous namespace'::`dynamic atexit destructor for 'mymap'' (void)
00ce1040 anonone!`anonymous namespace'::`dynamic initializer for 'mymap'' (void)
00d542e0 anonone!ns::`anonymous namespace'::`dynamic atexit destructor for 'mymap'' (void)
00d542d0 anonone!ns::`anonymous namespace'::`dynamic atexit destructor for 'mymap'' (void)
00ce1080 anonone!ns::`anonymous namespace'::`dynamic initializer for 'mymap'' (void)
00ce1060 anonone!ns::`anonymous namespace'::`dynamic initializer for 'mymap'' (void)
00d6b930 anonone!ns::`anonymous namespace'::mymap = <no type information>
00d6b960 anonone!ns::`anonymous namespace'::mymap = <no type information>
this is the type which you can get by using x {mod}!{std::map {wildcard}}
class std::map<char,char const *,std::less<char>,std::allocator<std::pair<char const ,char const *> > >
+0x000 _Mypair :
mymap : { size=0x5 }
snipped
[0x0] : 77 'M', "Alp" [Type:
[0x1] : 78 'N', "Bet" [Type:
[0x2] : 79 'O', "Gam" [Type:
[0x3] : 80 'P', "Del" [Type:
[0x4] : 81 'Q', "Eps" [Type:
0:000> dx ( std::map<char,char const *,std::less<char>,std::allocator<std::pair<char const ,char const *> > > *) 0x00d6b930
( std::map<char,char const *,std::less<char>,std::allocator<std::pair<char const ,char const *> > > *) 0x00d6b930
snipped
[0x1] : 65 'A', "Alpha" [Type:
[0x2] : 66 'B', "Beta" [Type:
[0x3] : 67 'C', "Gamma" [Type:
[0x4] : 68 'D', "Delta" [Type:
[0x5] : 69 'E', "Epsilon" [Type:
0:000> dx ( std::map<char,char const *,std::less<char>,std::allocator<std::pair<char const ,char const *> > > *) 0x00d6b960
( std::map<char,char const *,std::less<char>,std::allocator<std::pair<char const ,char const *> > > *) 0x00d6b960
snipped
[0x1] : 86 'V', "Kappa" [Type: std::pair<char const ,char const *>]
[0x2] : 87 'W', "Iota" [Type: std::pair<char const ,char const *>]
[0x3] : 88 'X', "Theta" [Type: std::pair<char const ,char const *>]
[0x4] : 89 'Y', "Eta" [Type: std::pair<char const ,char const *>]
[0x5] : 90 'Z', "Zeta" [Type: std::pair<char const ,char const *>]