1

I am trying to use gdb to debug a program that includes a module written in C++. I downloaded the latest version of gdb using homebrew.

I tried to run the following command:

(gdb) run Pyrh_test.py

However, I get the following error:

Starting program:  Pyrh_test.py
No executable file specified.
Use the "file" or "exec-file" command.

Then I tried using the "file" and "exec-file" commands like the following (outputs also included):

(gdb) file Pyrh_test.py
"/Users/danielribeiro/opt/Python/Spyder/Pyrh_test.py": not in executable format: file format not recognized
(gdb) exec-file Pyrh_test.py
"/Users/danielribeiro/opt/Python/Spyder/Pyrh_test.py": not in executable format: file format not recognized

How can I use gdb to run Pyrh_test.py?

Red
  • 26,798
  • 7
  • 36
  • 58

2 Answers2

1

What the OP has almost works for me - just needs one tweak which is adding python to the beginning gdb command:

1. $ gdb python3

Then the run command works

2. (gdb) run py.py 
Starting program: /usr/bin/python3 py.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
main started
[ ... ]

You can also test if gdb is running python by invoking directly instead of attempting to run a .py file:

$ gdb python3
GNU gdb (Ubuntu 12.0.90-0ubuntu1) 12.0.90
Copyright (C) 2022 Free Software Foundation, Inc.
[ ... ]

(gdb) run
>>> print("This is the python prompt.")
This is the python prompt.

System specs

And for reference:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:    22.04
Codename:   jammy

$ gdb --version
GNU gdb (Ubuntu 12.0.90-0ubuntu1) 12.0.90
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ python3 --version
Python 3.10.6
0

How can I use gdb to run Pyrh_test.py?

You don't run Pyrh_test.py. You run Python, which interprets the .py file.

The way to debug this is to run Python under GDB, like so:

gdb --args python Pyrh_test.py
(gdb) run
Employed Russian
  • 199,314
  • 34
  • 295
  • 362