49

I'm trying to debug a module "main", which calls a function "broken_function" at line 356 of "another_module". I'm having an error in that function and want to put a breakpoint at its start. Below is the listing. Am I doing something wrong? Cause, the breakpoint doesn't work:

$ python -m pdb main
(Pdb) import sys
(Pdb) sys.path.append("/home/user/path/to/another/module")
(Pdb) import another_module
(Pdb) b another_module:356
Breakpoint 1 at /home/user/path/to/another/module/another_module.py:356
(Pdb) c
Traceback (most recent call last):
...
File "/home/user/path/to/another/module/another_module.py", line 383, in broken_function
f=open("../jobs/temptree.tre", "r")
IOError: [Errno 2] No such file or directory: '../jobs/temptree.tre'
Uncaught exception. Entering post mortem debugging
...
Boris Burkov
  • 13,420
  • 17
  • 74
  • 109

2 Answers2

28

You are setting the breakpoint correctly. I imagine it is not stopping because the line of code you are breaking on is not called. Put the break on line 383.

Rian Rizvi
  • 9,989
  • 2
  • 37
  • 33
  • 2
    btw, you can also try a hard breakpoint to make sure. On line 383 insert "import pdb;pdb.set_trace()" – Rian Rizvi Nov 27 '12 at 18:01
  • Your advice works perfectly, but pdb's behaviour looks odd to me. The line 356 is the first line of the function "def broken_function():". If I put the breakpoint at the next line, where comment resides, pdb says "*** Blank or comment", which means that the numeration of lines isn't broken. Breakpoints within the function work properly. Strange... Thanks for your solution! – Boris Burkov Nov 27 '12 at 18:17
  • That's normal behavior. You can only suspend a line of code. Empty lines and comment lines are not executed by the Python interpreter, so you can't ask the interpreter to break before it runs them. – Rian Rizvi Nov 27 '12 at 18:44
  • But it seems, that when debugger executes the function it doesn't step on its definition line at all (I've checked it on a separate example). Gawd, am I going to have a day of python programming without dis.dis()? :) – Boris Burkov Nov 27 '12 at 19:12
  • 4
    The function definition isn't an executable line in Python. You can break on the first executable line in the function but not the function name itself. If you set the breakpoint with the alternative syntax "b another_module:broken_function", ie use the function name instead of a line number, then the debugger will find the first line for you and break there. That might be the better way to do it. – Rian Rizvi Nov 27 '12 at 19:17
  • 2
    `b another_module:your_function_name`. Much clearer and also less brittle than raw line numbers. – smci Nov 21 '17 at 18:24
18

You can also set the breakpoint directly with the file and line number, without having to import either sys or another_module.

(Pdb) b /home/user/path/to/another/module/another_module.py:383
Breakpoint 1 at /home/user/path/to/another/module/another_module.py:383

Note that /home/user/path/to/another/module/another_module.py needs to be imported and line 383 needs executable and in the path of execution for it to break, as others have pointed out.

For more help, type help b (or for that matter help followed by any other command) to get more information on that command.

user650654
  • 5,630
  • 3
  • 41
  • 44