简体   繁体   中英

Why does "gdb" print the class methods of specific classes?

The context

I recently found this question since I was searching a way to list all the available methods present in a class, so I started experimenting with the ptype command which made me post this question

Consider the following file

#include <array>
#include <vector>
#include <list>

int main() {
  std::array <int, 1> a;
  std::vector <int> v;
  std::list <int> l;

  return 0;
}

I can list all methods present in the array class by doing the following

g++ -g main.cpp && \
  gdb -q -batch -ex 'break 10' -ex 'run' -ex 'ptype a' ./a.out
Breakpoint 1 at 0x1179: file main.cpp, line 10.

Breakpoint 1, main () at main.cpp:10
10    return 0;
type = struct std::array<int, 1> [with _Tp = int] {
    std::__array_traits<_Tp, 1>::_Type _M_elems;
  public:
    void fill(reference);
    void swap(std::array<_Tp, 1> &);
    iterator begin(void);
    iterator begin(void) const;
    iterator end(void);
(... more methods ...)

However, this doesn't happen with the vector and list classes (see below).

Executing ptype on an instance of the vector class

g++ -g main.cpp && \
  gdb -q -batch -ex 'break 10' -ex 'run' -ex 'ptype v' ./a.out
Breakpoint 1 at 0x1179: file main.cpp, line 10.

Breakpoint 1, main () at main.cpp:10
10    return 0;
type = std::vector<int>

Executing ptype on an instance of the list class

g++ -g main.cpp && \
  gdb -q -batch -ex 'break 10' -ex 'run' -ex 'ptype l' ./a.out
Breakpoint 1 at 0x1179: file main.cpp, line 10.

Breakpoint 1, main () at main.cpp:10
10    return 0;
type = std::list<int>

The question

Why does ptype prints all the methods present in the array class but doesn't do the same with the vector and list classes?

I think it's gdb bug. I have reproduced this behavior for std::vector on my box, ptype v does not output std::vector members:

(gdb) ptype v
type = std::vector<int>

On the other hand ptype/rv does output members:

(gdb) ptype/r v
type = class std::vector<int, std::allocator<int> > : protected std::_Vector_base<int, std::allocator<int> > {
  private:
    static bool _S_nothrow_relocate(std::true_type);
    static bool _S_nothrow_relocate(std::false_type);
    static bool _S_use_relocate(void);
    ...

ptype/r should only display members in more verbose raw form compared to ordinary ptype , see in documentation :

Display in “raw” form. Normally, GDB substitutes template parameters and typedefs defined in a class when printing the class' members. The /r flag disables this.

But ordinary ptype does not display anything. As a workaround you can try to use ptype/r .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM