This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
Symbolic constants inside classes
- From: Jonas Maebe <jonas dot maebe at elis dot ugent dot be>
- To: gdb at sourceware dot org
- Date: Sat, 18 Dec 2010 21:17:28 +0100
- Subject: Symbolic constants inside classes
Hi,
Consider this C++ program:
***
class Xyz {
public:
static const int MAGIC_NUM = 42;
private:
char mArray[MAGIC_NUM];
};
class Xyz2 {
public:
static const int MAGIC_NUM = 43;
private:
char mArray2[MAGIC_NUM];
};
Xyz C;
Xyz2 C2;
int main()
{
return 0;
}
***
g++ -gdwarf-2 generates a definition for both MAGIC_NUM constants both in the respective class scopes, and also in the global namespace:
in the class:
<1><78>: Abbrev Number: 2 (DW_TAG_class_type) -- Xyz declaration
DW_AT_sibling : <a9>
DW_AT_name : Xyz
DW_AT_byte_size : 42
DW_AT_decl_file : 1
DW_AT_decl_line : 1
<2><84>: Abbrev Number: 3 (DW_TAG_variable) -- MAGIC_NUM field
DW_AT_name : (indirect string, offset: 0x14): MAGIC_NUM
DW_AT_decl_file : 1
DW_AT_decl_line : 3
DW_AT_MIPS_linkage_name: (indirect string, offset: 0x1e): _ZN3Xyz9MAGIC_NUME
DW_AT_type : <a9>
DW_AT_external : 1
DW_AT_declaration : 1
DW_AT_const_value : 42
...
in the global namespace:
<1><146>: Abbrev Number: 10 (DW_TAG_namespace) -- global namespace
DW_AT_sibling : <168>
DW_AT_name : ::
DW_AT_decl_file : 2
DW_AT_decl_line : 0
...
<1><168>: Abbrev Number: 12 (DW_TAG_variable) -- "C" global variable
DW_AT_specification: <150>
DW_AT_location : 9 byte block: 3 0 0 0 0 0 0 0 0 (DW_OP_addr: 0)
<1><177>: Abbrev Number: 12 (DW_TAG_variable) -- "C2" global variable
DW_AT_specification: <15b>
DW_AT_location : 9 byte block: 3 0 0 0 0 0 0 0 0 (DW_OP_addr: 0)
<1><186>: Abbrev Number: 3 (DW_TAG_variable) -- Xyz::MAGIC_NUM
DW_AT_name : (indirect string, offset: 0x14): MAGIC_NUM
DW_AT_decl_file : 1
DW_AT_decl_line : 3
DW_AT_MIPS_linkage_name: (indirect string, offset: 0x1e): _ZN3Xyz9MAGIC_NUME
DW_AT_type : <a9>
DW_AT_external : 1
DW_AT_declaration : 1
DW_AT_const_value : 42
<1><198>: Abbrev Number: 3 (DW_TAG_variable) -- Xyz2::MAGIC_NUM
DW_AT_name : (indirect string, offset: 0x14): MAGIC_NUM
DW_AT_decl_file : 1
DW_AT_decl_line : 10
DW_AT_MIPS_linkage_name: (indirect string, offset: 0x0): _ZN4Xyz29MAGIC_NUME
DW_AT_type : <a9>
DW_AT_external : 1
DW_AT_declaration : 1
DW_AT_const_value : 43
In gdb (7.0),
1) p MAGIC_NUM prints 42
2) "p Xyz::MAGIC_NUM" and "p Xyz2::MAGIC_NUM" print "static field MAGIC_NUM has been optimized out"
3) "p C.MAGIC_NUM" and "p C2.MAGIC_NUM" print "field MAGIC_NUM is nonexistent or has been optimised out"
4) ptype Xyz returns:
type = class Xyz {
public:
static const int MAGIC_NUM;
private:
char mArray[42];
}
My questions:
a) why does GDB not know the value of MAGIC_NUM inside the class scope? It is there in the debug info (see above)
b) does g++ possibly add the constants to the global scope because gdb cannot show them in the class scope (although it obviously can still only show one of them in case of a name conflict), or is this required by a DWARF rule I missed?
Jonas