Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set breakpoint in another module (don't set it on function definition line, if you want to break when function starts being executed)

Tags:

python

pdb

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 ... 
like image 961
Boris Burkov Avatar asked Nov 27 '12 17:11

Boris Burkov


2 Answers

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.

like image 178
Riaz Rizvi Avatar answered Sep 24 '22 22:09

Riaz Rizvi


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.

like image 33
user650654 Avatar answered Sep 22 '22 22:09

user650654