Author Topic: Debugger: use gdb python pretty printer for the libstdcxx under msys2  (Read 2451 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5346
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Hi, this is my way to use gdb python pretty printer for the libstdcxx under msys2.
Suppose you use 64bit gcc compiler. My msys2 is installed under F:\msys64

In the Menu->Settings->Debugger settings. Open the debugger plugin setting dialog.

Then, in the "Executable path" field, select "F:\msys64\mingw64\bin\gdb.exe"
in the "Debugger initialization commands" field, put the following text in the edit control.
Code
source F:\msys64\mingw64\etc\gdbinit

I see that I have to modify the file "F:\msys64\mingw64\etc\gdbinit" file: below is the original code

Code
python
import sys
sys.path.insert(0, sys.path[0] + '/../../gcc-9.2.0/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
end

But you have to add one line before the "end" statement like below to let the register_libstdcxx_printers function get executed.

Code
python
import sys
sys.path.insert(0, sys.path[0] + '/../../gcc-9.2.0/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers(None)
end

BTW: It is the same thing to add the python pretty printer for wxWidgets. You can use this file:
Code
https://github.com/wxWidgets/wxWidgets/blob/master/misc/gdb/print.py
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2974
Re: Debugger: use gdb python pretty printer for the libstdcxx under msys2
« Reply #1 on: November 30, 2019, 01:50:03 pm »
a wiki article would be nice :)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5346
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2974
Re: Debugger: use gdb python pretty printer for the libstdcxx under msys2
« Reply #3 on: March 20, 2021, 11:01:26 pm »
Great!

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 80
    • Suryavarman
Re: Debugger: use gdb python pretty printer for the libstdcxx under msys2
« Reply #4 on: March 21, 2021, 12:40:18 am »
It doesn't work for the wx print.
When i find some time i will use "source -p" like wxWidgets team say:

Quote
this file is meant to
#               be sourced from gdb using "source -p" (or, better, autoloaded
#               in the future...)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5346
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Debugger: use gdb python pretty printer for the libstdcxx under msys2
« Reply #5 on: March 21, 2021, 01:55:34 am »
It doesn't work for the wx print.
When i find some time i will use "source -p" like wxWidgets team say:

Quote
this file is meant to
#               be sourced from gdb using "source -p" (or, better, autoloaded
#               in the future...)

OK, I understand your problem. Here is the method to solve this problem.

First, you download the file
Code
https://github.com/wxWidgets/wxWidgets/blob/master/misc/gdb/print.py

For me, I renamed this file to wxprint.py, and I saved it to the same folder as the gdbinit file locates.

Then, I modify the gdbinit file as below:

Code
python
import sys
sys.path.insert(0, sys.path[0] + '/../../gcc-10.2.0/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)

sys.path.insert(0, sys.path[0] + '/../../../etc')
import wxprint
end

Look at the two lines:
Code
sys.path.insert(0, sys.path[0] + '/../../../etc')
import wxprint

The first line adds the current directory of the wxprint.py, and the second line just imports the wxprint.py file. I think the "import" directive us just like the "source" directive in Python, am I correct?

Anyway, this works OK in my msys2. I can have both wxWidgets and C++ std library's pretty printers working when debugging. :)

BTW: I haven't find a simple way to load the wxprint.py file, I just want to avoid the first line, but that line is always needed :(.  Maybe, some guys can improve the loading code.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 80
    • Suryavarman
Re: Debugger: use gdb python pretty printer for the libstdcxx under msys2
« Reply #6 on: March 21, 2021, 08:24:50 pm »
It's works. Thank you BlueHazzard  ;D

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5346
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
This is further simplified config when using msys2 based gdb. We can use the macro variables

For the gdb.exe path, just use this string:

Code
$(TARGET_COMPILER_DIR)bin\gdb.exe

and to use the gdb initialization script, use this string:

Code
source $(TARGET_COMPILER_DIR)etc\gdbinit

See the image shot below:

This make C::B more portable


If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline AndrewCot

  • Multiple posting newcomer
  • *
  • Posts: 23
ollydbg. Thanks for the Page and info as I was able to get the wxwidget pretty printing to work with the help form the page. I fell into a few minor holes, which could filed in by updating the page so other people do not fall into the same holes, which are/were:
1) In the Menu->Settings->Debugger settings. Open the debugger plugin setting dialog and uncheck the "Disable startup scripts (-nx) (GDB only)" option.

2) If you are using the https://github.com/wxWidgets/wxWidgets/blob/master/misc/gdb/print.py file then add the following to the gdbinit file:
sys.path.insert(0, 'D:/temp')         
from wxprint import register_wxwidgets_printers
register_wxwidgets_printers (None)

Rember to change the directory to where you have saved the print.py file.

3) Update it to change the "source F:\msys64\mingw64\etc\gdbinit" to "source $(TARGET_COMPILER_DIR)etc\gdbinit"

4) You may want to change the note about the "register_libstdcxx_printers(None)" additional line was added to MSYS in https://github.com/msys2/MINGW-packages/pull/6351 (6-Apr-2020) and if people are using a later version then they do not need to do anything.