检查GDB上层范围中的班级成员
examine class members in an upper scope in gdb
有一个类方法,其中包含开关/情况。为了表示,
之类的东西class class_name {
int k;
public:
int method();
class_name():k(0) {}
};
int class_name::method() {
k = 2; // class private data member, an integer
switch(k){
case 0:
// to examine k at this point
// perform path A
break;
case 1:
// perform path B
break;
} // switch(k)
return 0;
}
问题是:如果我想从开关/情况范围内从上部范围(例如K)中检查数据。我可以总是在上部范围中的某个地方放置一个断点,例如在分配k = 2的行上?
#include <stdio.h>
struct z
{
z() : k(42) {}
int k;
int mm ();
};
int z::mm()
{
int k = 0;
{
int k = 1;
{
int k = 2;
printf ("%dn", k);
}
}
}
int main()
{
z zp;
zp.mm();
}
当您在printf
上停止时,很难在外部范围中打印变量。 up
在帧之间移动时不起作用,而不是 scopes 。我不知道一种简单的方法,但是有解决方法。
info locals
将打印所有变量本地为函数。
(gdb) info locals
k = 2
k = 1
k = 0
这可能足以容纳简单的整数变量,但是如果我们有指针并想解释它们怎么办?
(gdb) where
#0 z::mm (this=0xbfffec8c) at q.C:18
#1 0x080484a5 in main () at q.C:26
好吧,我们在第18行,我们知道什么?
(gdb) info scope 18
Scope for 18:
Symbol k is a variable at frame base reg $ebp offset 8+-28, length 4.
Symbol k is a variable at frame base reg $ebp offset 8+-24, length 4.
Symbol k is a variable at frame base reg $ebp offset 8+-20, length 4.
Symbol this is a variable at frame base reg $esp offset 4+0, length 4.
aha,有三个名为 k
的符号(无论是什么意思), $ebp
看起来像寄存器名称,旁边的数字必须是偏移。
(gdb) p *(int*)($ebp+8-20)
$1 = 0
(gdb) p *(int*)($ebp+8-24)
$2 = 1
(gdb) p *(int*)($ebp+8-28)
$3 = 2
看起来我们在这里有赢家。
哦,总是可以说
(gdb) p this->k
$4 = 42
您输入以'向上'调用堆栈。呼叫将进行反向。那是你要的吗?
相关文章:
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 迭代嵌套映射与范围为循环:没有名为"first"的成员
- 检查私有成员变量是否在一定范围内,如果没有调整
- 立即传递其成员时的右值范围
- 成员变量在超出BeginPlay函数虚幻引擎的范围时丢失值c++
- 越界成员函数定义是否需要一个完全限定的类名,直到全局范围
- 对象超出范围后,引用成员设置为 0
- pImpl、范围和隐藏数据成员
- 在命名成员函数重载解析期间,"this"何时不在范围内?
- 当在另一个范围中,将SelfAdjointeigensolver保存为成员的结果被重新引入
- 成员的结构与命名空间的潜在范围
- C++奇怪的问题.数据成员超出范围
- 范围视图作为数据成员
- 基于循环的C ISTRINGSTREAM范围没有开始成员
- 为 具有"end"成员变量的类型启用基于范围的
- 如何将成员函数引入范围
- 如何基于每个成员覆盖类范围的__declspec(dllexport)注释?
- 从对象本身访问成员或成员范围
- c++单元测试:改变成员范围
- 在不更改成员范围的情况下有条件地启用静态成员