cyrus-and / gdb-dashboard

Modular visual interface for GDB in Python
MIT License
11.1k stars 780 forks source link

How to get syntax highlighting in the Source module when using venv? #318

Closed ipython3 closed 6 months ago

ipython3 commented 6 months ago

OS: Debian 12

I'm trying to use gdb-dashboard for the first time. I have installed Pygments as mentioned in the README.

In the Output/messages module, syntax highlighting looks good:

image

However, in the Source module, there is no syntax highlighting:

image (1)

Do I need to add any configuration to get syntax highlighting in the Source module?

Thank you so much for creating this amazing tool!

cyrus-and commented 6 months ago

Does the commands in this wiki page work? They need to be executed within GDB.

Also what's the output of the following?

gdb --batch -ex 'python import sys; print(sys.version)'
ipython3 commented 6 months ago

Hi @cyrus-and,

Here is the output:

(.venv) myname@debian12:~/learn/learn-gdb$ gdb --batch -ex 'python import sys; print(sys.version)'
3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]

I tried the commands in this wiki page and error ModuleNotFoundError: No module named 'pygments' occurs: 屏幕截图 2024-05-11 163603

To make sure that I didn't forget to install pygments, I created a test.py file:

from pygments.styles import *
for style in get_all_styles():
    print(style)

It runs successfully:

(.venv) myname@debian12:~/learn/learn-gdb$ which python3
/home/myname/.venv/bin/python3
(.venv) myname@debian12:~/learn/learn-gdb$ python3 test.py 
abap
algol
algol_nu
arduino
autumn
bw
borland
colorful
default
dracula
emacs
friendly_grayscale
friendly
fruity
github-dark
gruvbox-dark
gruvbox-light
igor
inkpot
lightbulb
lilypond
lovelace
manni
material
monokai
murphy
native
nord-darker
nord
one-dark
paraiso-dark
paraiso-light
pastie
perldoc
rainbow_dash
rrt
sas
solarized-dark
solarized-light
staroffice
stata-dark
stata-light
tango
trac
vim
vs
xcode
zenburn
(.venv) myname@debian12:~/learn/learn-gdb$
cyrus-and commented 6 months ago

In a nutshell, Pygments must be available for the version of Python that runs inside GDB (in your case 3.11.2).

ipython3 commented 6 months ago

Hi @cyrus-and,

Thank you so much! I was not familiar with Python integration in GDB and now I see where the problem is.

I didn't install Pygments system-wide, because the reason listed here.

In the Quickstart section of README.md of gdb-dashboard, it says we can install Pygments by pip install pygments, but I did some search and found this. It says that the Python interpreter that GDB uses is linked into GDB, in the form of libpython3.4.a or libpython3.4.so, etc.

I'm very confused. Even if I run pip install pygments system-wide, that would only install pygments in to the system Python, instead of the Python that runs inside GDB. How could I let Pygments be available for the Python that runs inside GDB?

Is there anything wrong with my understanding? I really appreciate it if you can correct me.

By the way, Python version in my system and in the venv are also 3.11.2:

myname@debian12:~/learn/learn-gdb$ which python3
/usr/bin/python3
myname@debian12:~/learn/learn-gdb$ python3
Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
myname@debian12:~/learn/learn-gdb$ source ~/.venv/bin/activate
(.venv) myname@debian12:~/learn/learn-gdb$ which python3
/home/myname/.venv/bin/python3
(.venv) myname@debian12:~/learn/learn-gdb$ python3
Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
(.venv) myname@debian12:~/learn/learn-gdb$
cyrus-and commented 6 months ago

OK so, the question is then, how to use Pygments in Python GDB using venv? What I said holds for packages installed systemwide. And in fact, installing Pygments systemwide will fix your issue.

But if you want to use venv, you just need to tell the GDB Python that you want to use that instead, apparently all you have to do is to add the venv path to the sys.path inside Python GDB, that is, in my case:

(.venv) user@cc547d5a0dfc:~$ python3 -c 'import sys; print(sys.path)'
['', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/user/.venv/lib/python3.10/site-packages']

So in GDB (this is the part that should be in your configuration file, something inside ~/.gdbinit.d/, either a .py file or a GDB file):

python import sys; sys.path += ['/home/user/.venv/lib/python3.10/site-packages']

After that, you should be able to import Pygments and all of that. Hope it helps!

ipython3 commented 6 months ago

Thank you for the informative answer! It really helps me a lot!

cyrus-and commented 6 months ago

Feel free to post your exact solution so to help others, if you want.