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

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • 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: 3352
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: 6035
  • 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: 3352
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: 81
    • 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: 6035
  • 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: 81
    • 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: 6035
  • 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

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 679
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.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Hi, thanks for the response!

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.

I think this option should be "checked on", I mean we don't need the startup scripts.


Quote
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.
I don't do like that, I just shows what I did in this post:
Re: Debugger: use gdb python pretty printer for the libstdcxx under msys2
You don't need the "hard-coded path" in those settings.


Quote
3) Update it to change the "source F:\msys64\mingw64\etc\gdbinit" to "source $(TARGET_COMPILER_DIR)etc\gdbinit"
This is correct, I also do like this.


Quote
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.
Yes, I think msys2 already has configured the pretty printer for the libstdcxx library, what we need is to add some codes to the "gdbinit" file which support the wx's pretty printer.
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

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 679
Re: Debugger: use gdb python pretty printer for the libstdcxx under msys2
« Reply #10 on: June 20, 2021, 03:11:11 am »
Feedback on feedback:
1). "Disable startup scripts (-nx) (GDB only)" option.
Quote
In the Menu->Settings->Debugger settings. Open the debugger plugin setting dialog and uncheck the "Disable startup scripts (-nx) (GDB only)" option.
>> I think this option should be "checked on", I mean we don't need the startup scripts.

If I leave the option checked [X] then the pretty printer does not work and the watch looks like it does when no pretty printer is defined for wxWidget. If uncheck [ ] the option then the GDB pretty printer watch displays a wxString in a human read able format. See attachment showing this.

2.gdbinit file:
Quote
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.

>>I don't do like that, I just shows what I did in this post:
>>Re: Debugger: use gdb python pretty printer for the libstdcxx under msys2
>>You don't need the "hard-coded path" in those settings.

I think the page still needs an update to add the info so someone does not need to find in the forum post (like I had to do as it did not work due to item 1), but I also had to get the syntax correct in the gdbinit):
a) Copy the https://github.com/wxWidgets/wxWidgets/blob/master/misc/gdb/print.py into :
      Linux: /etc    (checked on Linux Mint 20.1 Virtual machine)
      Windows - MSYS2: x:\msys64\mingw64\etc or x:\msys64\mingw32\etc where x: is the drive you installed Msys2 onto and depending on if you are using the 32 or 64 bit compiler
      Windows - Cygwin: - Looks like it supports pretty printing, but by default no gdbinit file exists.
      Windows - Mingw64: TBA
      MacOSx: TBA
a) Add the following to gdbinit  in the directory in the step above:
Code
sys.path.insert(0, sys.path[0] + '/../../../etc')
import wxprint