Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does python do inside the gdb debugger?

Tags:

c++

python

gdb

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?

like image 597
Amir reza Riahi Avatar asked Oct 20 '25 16:10

Amir reza Riahi


1 Answers

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
like image 53
ks1322 Avatar answered Oct 23 '25 05:10

ks1322



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!