How to use pdb?

T

tron.thomas

I am trying to figure out how to use the pdb module to debug Python
programs, and I'm having difficulties. I am pretty familiar with GDB
which seems to be similar, however I cannot even get the Python
debugger to break into a program so I can step through it. I wrote a
simple program and below is an example of the steps I tried, as well as
the results I got:
<string>(1)?()
(Pdb) b MyProgram.MyClass.method
Breakpoint 1 at MyProgram.py:8
(Pdb) cont
The program should have printed a message to the screen when execution
was continued, and it should have hit the breakpoint during that
execution. Instead the debugger exitted to the main prompt.

What is the reason the dubugger behaves like this?
What is the correct way to accomplish what I am trying to do?

I have tried this process on Python 2.3.5 and 2.4.3 with the same
results on each version.
 
R

R. Bernstein

I am trying to figure out how to use the pdb module to debug Python
programs, and I'm having difficulties. I am pretty familiar with GDB
which seems to be similar,

If you are pretty familiar with gdb, try
http://bashdb.sourceforge.net/pydb. It is a great deal more similar. ;-)

however I cannot even get the Python
debugger to break into a program so I can step through it. I wrote a
simple program and below is an example of the steps I tried, as well as
the results I got:

(Pdb) b MyProgram.MyClass.method
Breakpoint 1 at MyProgram.py:8
(Pdb) cont

The program should have printed a message to the screen when execution
was continued, and it should have hit the breakpoint during that
execution. Instead the debugger exitted to the main prompt.

What is the reason the dubugger behaves like this?

I think you misunderstand what pdb.run is supposed to do. It's not at
all like gdb's "run" command. In fact pdb doesn't even have a
debugger *command* called "run", although it does have top-level
function called "run".

pdb.run is evaluating MyProgram.mainfunction the same as if you were
to type it at Python's >>> prompt.

Except it will call the debugger control WHEN IT ENCOUNTERS THE FIRST
STATEMENT. I think if you were issu MyProgram.mainFunction at the
python prompt you'd get back something like: <function mainFunction at
0xb7f5c304>.

It wouldn't *call* the routine. To call it, you'd type something like
Myprogram.mainFunction()

But before you try to issue pdb.run('Myprogram.mainFunction()'), read
on. The way to use pdb.run is to insert it somewhere in your program
like MyProgram and have the debugger kick in, not something you issue
from a Python prompt.
What is the correct way to accomplish what I am trying to do?

I have tried this process on Python 2.3.5 and 2.4.3 with the same
results on each version.

Perhaps what you are looking for is:
python /usr/lib/python2.4/pdb.py Myprogram.py

(Subtitute the correct path name for pdb.py.)

If you have pydb installed it adds a symbolic link to pydb.py. So here
you should be able to issue:

pydb Myprogram.py
 
T

tron.thomas

R. Bernstein said:
Perhaps what you are looking for is:
python /usr/lib/python2.4/pdb.py Myprogram.py

I tried this and it did not work. pdb did not load the file so it
could be debugged.
 
R

R. Bernstein

I tried this and it did not work. pdb did not load the file so it
could be debugged.

lol. Yes, if you are not in the same directory as Myprogram.py you may
have to add be more explicit about where Myprogram.py is.

Reminds me of the story of the guy who was so lazy that when he got an
award for "laziest person alive" he said, "roll me over and put it in
my back pocket." :)

Glad you were able to solve your problem though.

For other similarly confused folks I have updated pydb's
documentation (in CVS):

In contrast to running a program from a shell (or gdb), no path
searching is performed on the python-script. Therefore python-script
should be explicit enough (include relative or absolute file paths)
so that the debugger can read it as a file name. Similarly, the
location of the Python interpeter used for the script will not
necessarily be the one specified in the magic field (the first line
of the file), but will be the Python interpreter that the debugger
specifies. (In most cases they'll be the same and/or it won't
matter.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top