Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB/C++ - How to hide const static fields when backtracing?

Tags:

c++

gdb

I'm debugging c++ code with gdb. Something is throwing a wobbly, so I hit "backtrace full" to inspect. Unfortunately some of the member variables are instances of class that contain const static fields. There are a lot of const static fields. In addition, these const static fields are themselves instances of said class:

class foo {
public:
   int val;
   foo(int _val) : val(_val) {}

   static const foo FOO_5 = foo(5);
   static const foo FOO_6 = foo(6);
   static const foo FOO_7 = foo(7);
   ...
   }

(My real code is a font formatting class with presets for bold, italic, black, red, etc)

The result is gdb feeling the need to spit out every const static field of every member of said class (and every member of each memeber, etc). This results in many screens full of the same const static fields being displayed:

Wall of gdb text:

 mItems = std::vector of length 0, capacity 0, activeFormat = {
            _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 255 '\377', g = 255 '\377', b = 255 '\377', 
            a = 255 '\377', format = 0 '\000', flags = 0 '\000', padding = "\000", static FORMAT_NONE = 0 '\000', 
            static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', 
            static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', 
            static FORMAT_FLAGS_COLOUR_SET = 1 '\001', static bold = {
              _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 255 '\377', g = 255 '\377', b = 255 '\377', 
              a = 255 '\377', format = 1 '\001', flags = 0 '\000', padding = "\000", static FORMAT_NONE = 0 '\000', 
              static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', 
              static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', 
              static FORMAT_FLAGS_COLOUR_SET = 1 '\001', static bold = <same as static member of an already seen type>, static italic = {
                _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 255 '\377', g = 255 '\377', b = 255 '\377', 
                a = 255 '\377', format = 2 '\002', flags = 0 '\000', padding = "\000", static FORMAT_NONE = 0 '\000', 
                static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', 
                static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', 
                static FORMAT_FLAGS_COLOUR_SET = 1 '\001', static bold = <same as static member of an already seen type>, 
                static italic = <same as static member of an already seen type>, static underline = {
                  _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 255 '\377', g = 255 '\377', b = 255 '\377', 
                  a = 255 '\377', format = 4 '\004', flags = 0 '\000', padding = "\000", static FORMAT_NONE = 0 '\000', 
                  static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', 
                  static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', 
                  static FORMAT_FLAGS_COLOUR_SET = 1 '\001', static bold = <same as static member of an already seen type>, 
                  static italic = <same as static member of an already seen type>, 
                  static underline = <same as static member of an already seen type>, static strikethrough = {
                    _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 255 '\377', g = 255 '\377', b = 255 '\377', 
                    a = 255 '\377', format = 8 '\b', flags = 0 '\000', padding = "\000", static FORMAT_NONE = 0 '\000', 
                    static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', 
                    static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', 
                    static FORMAT_FLAGS_COLOUR_SET = 1 '\001', static bold = <same as static member of an already seen type>, 
                    static italic = <same as static member of an already seen type>, 
                    static underline = <same as static member of an already seen type>, 
                    static strikethrough = <same as static member of an already seen type>, static red = {
                      _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 255 '\377', g = 0 '\000', b = 0 '\000', 
                      a = 255 '\377', format = 0 '\000', flags = 1 '\001', padding = "\000", static FORMAT_NONE = 0 '\000', 
                      static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', 
                      static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', 
                      static FORMAT_FLAGS_COLOUR_SET = 1 '\001', static bold = <same as static member of an already seen type>, 
                      static italic = <same as static member of an already seen type>, 
                      static underline = <same as static member of an already seen type>, 
                      static strikethrough = <same as static member of an already seen type>, 
                      static red = <same as static member of an already seen type>, static green = {
                        _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 0 '\000', g = 255 '\377', b = 0 '\000', 
                        a = 255 '\377', format = 0 '\000', flags = 1 '\001', padding = "\000", static FORMAT_NONE = 0 '\000', 
                        static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', 
                        static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', 
                        static FORMAT_FLAGS_COLOUR_SET = 1 '\001', static bold = <same as static member of an already seen type>, 
                        static italic = <same as static member of an already seen type>, 
                        static underline = <same as static member of an already seen type>, 
                        static strikethrough = <same as static member of an already seen type>, 
                        static red = <same as static member of an already seen type>, 
                        static green = <same as static member of an already seen type>, static blue = {
                          _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 0 '\000', g = 0 '\000', 
                          b = 255 '\377', a = 255 '\377', format = 0 '\000', flags = 1 '\001', padding = "\000", 
                          static FORMAT_NONE = 0 '\000', static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', 
                          static FORMAT_UNDERLINE = 4 '\004', static FORMAT_STRIKETHROUGH = 8 '\b', 
                          static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', static FORMAT_FLAGS_COLOUR_SET = 1 '\001', 
                          static bold = <same as static member of an already seen type>, 
                          static italic = <same as static member of an already seen type>, 
                          static underline = <same as static member of an already seen type>, 
                          static strikethrough = <same as static member of an already seen type>, 
                          static red = <same as static member of an already seen type>, 
                          static green = <same as static member of an already seen type>, 
                          static blue = <same as static member of an already seen type>, static yellow = {
                            _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 255 '\377', g = 255 '\377', 
                            b = 0 '\000', a = 255 '\377', format = 0 '\000', flags = 1 '\001', padding = "\000", 
                            static FORMAT_NONE = 0 '\000', static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', 
                            static FORMAT_UNDERLINE = 4 '\004', static FORMAT_STRIKETHROUGH = 8 '\b', 
                            static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', static FORMAT_FLAGS_COLOUR_SET = 1 '\001', 
                            static bold = <same as static member of an already seen type>, 
                            static italic = <same as static member of an already seen type>, 
                            static underline = <same as static member of an already seen type>, 
                            static strikethrough = <same as static member of an already seen type>, 
                            static red = <same as static member of an already seen type>, 
--Type <RET> for more, q to quit, c to continue without paging--
                            y seen type>, static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, static black = {
                              _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 0 '\000', g = 0 '\000', b = 0 '\000', a = 255 '\377', format = 0 '\000', flags = 1 '\001', padding = "\000", static FORMAT_NONE = 0 '\000', static FORMAT_BOLD = 1 '\001', 
                              static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', static FORMAT_FLAGS_COLOUR_SET = 1 '\001', 
                              static bold = <same as static member of an already seen type>, static italic = <same as static member of an already seen type>, static underline = <same as static member of an already seen type>, 
                              static strikethrough = <same as static member of an already seen type>, static red = <same as static member of an already seen type>, static green = <same as static member of an already seen type>, 
                              static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, static white = {
                                _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 255 '\377', g = 255 '\377', b = 255 '\377', a = 255 '\377', format = 0 '\000', flags = 1 '\001', padding = "\000", static FORMAT_NONE = 0 '\000', 
                                static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', static FORMAT_FLAGS_COLOUR_SET = 1 '\001', 
                                static bold = <same as static member of an already seen type>, static italic = <same as static member of an already seen type>, static underline = <same as static member of an already seen type>, 
                                static strikethrough = <same as static member of an already seen type>, static red = <same as static member of an already seen type>, static green = <same as static member of an already seen type>, 
                                static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, 
                                static white = <same as static member of an already seen type>, static magenta = {_vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 255 '\377', g = 0 '\000', b = 255 '\377', a = 255 '\377', format = 0 '\000', 
                                  flags = 1 '\001', padding = "\000", static FORMAT_NONE = 0 '\000', static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', static FORMAT_STRIKETHROUGH = 8 '\b', 
                                  static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', static FORMAT_FLAGS_COLOUR_SET = 1 '\001', static bold = <same as static member of an already seen type>, static italic = <same as static member of an already seen type>, 
                                  static underline = <same as static member of an already seen type>, static strikethrough = <same as static member of an already seen type>, static red = <same as static member of an already seen type>, 
                                  static green = <same as static member of an already seen type>, static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, 
                                  static black = <same as static member of an already seen type>, static white = <same as static member of an already seen type>, static magenta = <same as static member of an already seen type>, static cyan = {
                                    _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 0 '\000', g = 255 '\377', b = 255 '\377', a = 255 '\377', format = 0 '\000', flags = 1 '\001', padding = "\000", static FORMAT_NONE = 0 '\000', 
                                    static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', static FORMAT_FLAGS_COLOUR_SET = 1 '\001', 
                                    static bold = <same as static member of an already seen type>, static italic = <same as static member of an already seen type>, static underline = <same as static member of an already seen type>, 
                                    static strikethrough = <same as static member of an already seen type>, static red = <same as static member of an already seen type>, static green = <same as static member of an already seen type>, 
                                    static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, 
                                    static white = <same as static member of an already seen type>, static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, static grey = {
                                      _vptr.sttfont_format = 0x555555590898 <vtable for sttfont_format+16>, r = 128 '\200', g = 128 '\200', b = 128 '\200', a = 255 '\377', format = 0 '\000', flags = 1 '\001', padding = "\000", static FORMAT_NONE = 0 '\000', 
                                      static FORMAT_BOLD = 1 '\001', static FORMAT_ITALIC = 2 '\002', static FORMAT_UNDERLINE = 4 '\004', static FORMAT_STRIKETHROUGH = 8 '\b', static FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 128 '\200', static FORMAT_FLAGS_COLOUR_SET = 1 '\001', 
                                      static bold = <same as static member of an already seen type>, static italic = <same as static member of an already seen type>, static underline = <same as static member of an already seen type>, 
                                      static strikethrough = <same as static member of an already seen type>, static red = <same as static member of an already seen type>, static green = <same as static member of an already seen type>, 
                                      static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, 
                                      static white = <same as static member of an already seen type>, static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, 
                                      static grey = <same as static member of an already seen type>, static reset = {<No data fields>}}, static reset = <same as static member of an already seen type>}, static grey = <same as static member of an already seen type>, 
                                  static reset = <same as static member of an already seen type>}, static cyan = <same as static member of an already seen type>, static grey = <same as static member of an already seen type>, 
                                static reset = <same as static member of an already seen type>}, static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, 
                              static grey = <same as static member of an already seen type>, static reset = <same as static member of an already seen type>}, static white = <same as static member of an already seen type>, 
                            static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, static grey = <same as static member of an already seen type>, 
                            static reset = <same as static member of an already seen type>}, static black = <same as static member of an already seen type>, static white = <same as static member of an already seen type>, 
                          static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, static grey = <same as static member of an already seen type>, 
                          static reset = <same as static member of an already seen type>}, static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, 
                        static white = <same as static member of an already seen type>, static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, static grey = <same as static member of an already seen type>, 
                        static reset = <same as static member of an already seen type>}, static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, 
                      static white = <same as static member of an already seen type>, static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, static grey = <same as static member of an already seen type>, 
                      static reset = <same as static member of an already seen type>}, static green = <same as static member of an already seen type>, static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, 
                    static black = <same as static member of an already seen type>, static white = <same as static member of an already seen type>, static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, 
                    static grey = <same as static member of an already seen type>, static reset = <same as static member of an already seen type>}, static red = <same as static member of an already seen type>, static green = <same as static member of an already seen type>, 
                  static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, static white = <same as static member of an already seen type>, 
                  static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, static grey = <same as static member of an already seen type>, static reset = <same as static member of an already seen type>}, 
                static strikethrough = <same as static member of an already seen type>, static red = <same as static member of an already seen type>, static green = <same as static member of an already seen type>, static blue = <same as static member of an already seen type>, 
                static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, static white = <same as static member of an already seen type>, static magenta = <same as static member of an already seen type>, 
                static cyan = <same as static member of an already seen type>, static grey = <same as static member of an already seen type>, static reset = <same as static member of an already seen type>}, static underline = <same as static member of an already seen type>, 
              static strikethrough = <same as static member of an already seen type>, static red = <same as static member of an already seen type>, static green = <same as static member of an already seen type>, static blue = <same as static member of an already seen type>, 
              static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, static white = <same as static member of an already seen type>, static magenta = <same as static member of an already seen type>, 
              static cyan = <same as static member of an already seen type>, static grey = <same as static member of an already seen type>, static reset = <same as static member of an already seen type>}, static italic = <same as static member of an already seen type>, 
            static underline = <same as static member of an already seen type>, static strikethrough = <same as static member of an already seen type>, static red = <same as static member of an already seen type>, static green = <same as static member of an already seen type>, 
            static blue = <same as static member of an already seen type>, static yellow = <same as static member of an already seen type>, static black = <same as static member of an already seen type>, static white = <same as static member of an already seen type>, 
            static magenta = <same as static member of an already seen type>, static cyan = <same as static member of an already seen type>, static grey = <same as static member of an already seen type>, static reset = <same as static member of an already seen type>}}

My question is simple: How do I suppress const static fields from gdb's backtrace full command? Or even just remove all static fields. Cheers!

like image 386
SnapperTT Avatar asked Oct 25 '25 05:10

SnapperTT


2 Answers

How do I suppress const static fields from gdb's backtrace full command?

(gdb) set print static off

like image 102
Employed Russian Avatar answered Oct 26 '25 19:10

Employed Russian


The full command is:

set print static-members off

It can be abbreviated to:

set pri st off

If not totally sure, when typing it from memory, use the key to autocomplete each value:

set pri<TAB> st<TAB> off

and you will get back the full version to verify, that you are setting off the right thing. Instead of typing it in each session, you can also put it into the file .gdbinit in your home directory.

Unfortunately, gdb doesn't seem to distinguish, whether values are just static or const static. The above command turns printing of both of them off. So, if you have important state in static fields, you'll need to print that manually. That can easily be done with:

print typename::fieldname

even, if you don't have an object of that type handy. Unfortunately, there doesn't seem to be a wildcard like print typename::* or print typename::static.

like image 25
Kai Petzke Avatar answered Oct 26 '25 19:10

Kai Petzke