Amir reza Riahi
Amir reza Riahi

Reputation: 2450

What does python do inside the gdb debugger?

I was debugging a C++ program in the gdb debugger and tried to access the 5th element of vector which only contains 4 element. After trying it, this error was on the screen:

(gdb) list main
1   #include <memory>
2   #include <vector>
3   
4   int main(int argc, char *argv[]){
5   
6   
7       std::vector<int> v_num = {1, 3, 5, 67};
8       std::unique_ptr<std::vector<int>> p(&v_num);
9   
10  
(gdb) p v_num.at (5)
Python Exception <class 'IndexError'> Vector index "5" should not be >= 4.: 
Error while executing Python code.
(gdb) 

I didn't expect to see a Python exception inside the the gdb. Can someone explain why I encountered such error? Does gdb uses python internally?

Upvotes: 4

Views: 526

Answers (1)

ks1322
ks1322

Reputation: 35716

Does gdb uses python internally?

Yes, it uses Python a lot to extend itself in many ways, see https://sourceware.org/gdb/onlinedocs/gdb/Python.html#Python.

What you discovered is called Python Xmethods, see https://sourceware.org/gdb/onlinedocs/gdb/Xmethods-In-Python.html. Xmethods are used as a replacement of inlined or optimized out method defined in C++ source code. libstdc++ has a number of xmethods for standard containers. You can see them with info xmethod command. For std::vector there are 6 xmethods defined on my box:

  libstdc++::vector
    size
    empty
    front
    back
    at
    operator[]

You can disable all xmethods with disable xmethod command. If you do it, GDB will unable to call at method:

(gdb) p v_num.at (5)
Cannot evaluate function -- may be inlined

Upvotes: 4

Related Questions