Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examining static const class members in gdb

Tags:

c++

gdb

How can I print the value of static const class members in gdb?

Say I have:

#include <iostream>

struct foo {
    static const int bar = 5;
};

int main() { 
    std::cout << foo::bar;
    return 0; 
}

How do I examine the contents foo::bar in gdb?

I tried:

(gdb) p foo::bar
No symbol "foo" in current context.
(gdb) p 'foo::bar'
No symbol "foo::bar" in current context.
like image 282
Gillespie Avatar asked Aug 05 '21 20:08

Gillespie


People also ask

What are static data members in C++?

Static data members in C++. Static data member are class members that are declared using static keyword A static member has certain special characteristics These are: Only one copy of that member is created for the entire class and is shared by all the objects of that class , no matter how many objects are created.

What are the characteristics of static members of a class?

A static member has certain special characteristics. These are: Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created. It is initialized to zero when the first object of its class is created.

What are the different register names used in gdb?

GDB has four "standard" register names that are available (in expressions) on most machines--whenever they do not conflict with an architecture's canonical mnemonics for registers. The register names $pcand $spare used for the program counter register and the stack pointer.

How to see the true contents of hardware registers in gdb?

In order to see the true contents of hardware registers, you must select the innermost frame (with `frame 0'). However, GDB must deduce where registers are saved, from the machine code generated by your compiler. If some registers are not saved, or if GDB is unable to locate the saved registers, the selected stack frame makes no difference.


Video Answer


1 Answers

You can't because gcc does not resolve this to a symbol but to an actual value in the assembly so gdb has nothing to look at. If you needed to you might be able to add the volatile keyword to prevent the compiler from performing this optimization.

like image 199
Samuel Comeau Avatar answered Oct 24 '22 09:10

Samuel Comeau