Is it possible to use GDB's reverse debugging with Python? How? - python

I am trying to use GDB's reverse debugging with a Django application. I get it running in GDB, but I can't make it run backwards.
I stopped my Django app with Ctrl-Z and then entered reverse-next at the gdb prompt, getting the error message "Target multi-thread does not support this command."
Am I doing it wrong? Isn't this possible? Both?

Before you can use GDB for reverse debugging, you must tell it to record your program execution (so it can play it back) via target record command, as documented here.
I am not sure this will help you debug your Django application though -- GDB is well suited for debugging "native" code (compiled C/C++), and is not well suited for debugging "interpreted" code (in either forward or reverse direction).

RevDB
https://bitbucket.org/pypy/revdb
https://morepypy.blogspot.co.uk/2016/07/reverse-debugging-for-python.html
This project aims to allow pdb-like reverse debugging, which is likely what you want unless you are debugging the Python interpreter itself.
It is still in early stages as of 2017, and you must build from source.
How to question that does not mention GDB: Is it possible to step backwards in pdb?
Finally, GDB reverse debugging is quite immature, e.g. does not deal with AVX extensions Disable AVX-optimized functions in glibc (LD_HWCAP_MASK, /etc/ld.so.nohwcap) for valgrind & gdb record , so I strongly recommend using rr instead: https://github.com/mozilla/rr (on which RevDB claims to take inspiration from).
Related for JavaScript: How to go backwards while debugging Javascript in Chrome sources debugging?

That's an amazingly good question.
My first impulse would be to ensure I was using IPython as my shell for django and see if it's pdb support would help in this case. Pdb should have a very similar interface to gdb. As I recall, gdb is what's used to debug C/C++ programs, while django is being executed by a python interpreter. Using Pdb is here:
http://ericholscher.com/blog/2008/aug/31/using-pdb-python-debugger-django-debugging-series-/
Also you might want to try using django-extensions, for access to the werkzeug debugging view.

Related

SPSS Python Custom Extension Command Debugging

I have created a Custom Extension Command in Python. I installed it, but as expected I am getting errors (quote from SPSS log output - the only way I know for debugging Python programs in SPSS):
Extension command TEST_EXTENSION could not be loaded. The module or a module that it requires may be missing, or there may be syntax errors in it.
The error is probably from the .xmlor from the Run(args) function. The CustomFunction() I am implementing was tested thoroughly.
What would be a good practice for debugging this, and the other potential errors ? The official IBM-SPSS-Statistics-Extension-Command says to
set the
SPSS_EXTENSIONS_RAISE
environment variable to "true"
but I don't know how to do that, nor of this will work regardless of the source of the error.
#horace
You set the environment variable on Windows via the Control Panel > System > Advanced system settings > Environment Variables. The exact wording varies with different Windows versions. I usually choose System variables, although either will usually work. You need to restart Statistics after that. Once you have set this variable, errors in the Python code will produce a traceback. The traceback is ordinarily suppressed as it is of no use to users, but it is very helpful for developers.
The traceback only appears for errors in the Python code. The "could not be loaded" error you reported happens before Python gets control, so no traceback would be produced. There are two common causes for this error. The first is that the xml file defining the extension command or the corresponding Python module was not found by Statistics. The extension command definitions are loaded at Statistics startup or by running the EXTENSION command. Execute SHOW EXT. from the Syntax Editor to see the places where Statistics looks for extension files.
The second cause is a syntax error in the Python code. Run
begin program.
import yourmodule
end program.
to see if any errors are reported.
More generally, there are two useful strategies for debugging. The first is to run the code in external mode, where you run the code from Python. That way you can step through the code using your IDE or the plain Python debugger. See the programmability documentation for details. There are some limitations on what can be done in external mode, but it is often a good solution.
The second is to use an IDE that supports remote debugging. I use Wing IDE, but there are other IDEs that can do this. That lets me jump into the debugger from within Statistics, step through the Python code, and do all the other things you want in a debugger.
HTh

How to debug python tornado

I already set the debug=Truethen what is the next ? I use eclipse + pydev for develop environment
Give me some details about tornado debugging will be very appreciate
Use Eclipse, PyDev, PyCharm, or whatever to set a breakpoint at the misbehaving line of code and step through your code from there. Tornado applications are relatively difficult to debug because the stack trace is less clear than in multithreaded code. Step through your code carefully. If you use coroutines, you should become familiar with the implementation of gen.Runner so you can understand what your code does during a "yield".

Debugging pyjamas python code

I am using pyjamas to develop a web page. I write python code which is translated to javascript by pyjamas. This way debugging programming error is caught only when testing the web page. It would be much more efficient if we can run the code first in the Python interpreter before compiling to javascript. This seems would require pyjamas-desktop be installed. I tried installing pyjamas-desktop but it was not straight forward for me and I could not succeed in installing pyjamas-desktop.
I am wondering if there is anyway to run the code to just check for errors i.e. I do not want to see the widgets popped up. But just running in the interpreter so that errors like missing to import a module that is used in the program are caught.
Pylint is probably the first place to start - it can be installed with easy_install then run as
$ pylint my_python.py
The verbosity of errors can be easily edited - but any major problems will be highlighted immediately - and you don't need those extra pyjamas-desktop modules installed! Other similar tools (that do the same sort of thing) are PyCheckMate (syntax check only), PyChecker, and PyFlakes.

Can I find out where a Python application crashed using the data dump?

My application crashed recently on a client's computer. I suspect it's because of PyQt's own memory management, which can lead to a invalid memory accesses when not properly handled.
When Python crashes like this, no backtrace is printed, only a data dump is written to the disk.
Is there a possibility to find out where in the Python code the crash occured?
Here's the dump: http://pastie.org/768550
Is this a linux core dump? If so you can examine it with gdb. You will need to run in on a system with an identical OS and version of Python, including 3rd party libraries. Run gdb -c /path/to/core/file. Once gdb has loaded then the command bt will list the stack trace for the main thread, and thread apply all bt will list the stack trace for all threads.
How useful this will be depends on whether the version of Python includes the full symbol table (i.e. is a debug build of Python) - if it is not, then you will only see addresses as offsets to the main C entry points. However this can still be of some use in diagnosing what went wrong.
If it is some other OS that does not support gdb then you are on your own - presumably the OS will have its own debugging tools.
Edit:
There is a page on the Python wiki describing how to get a python stack trace with gdb.
However a quick look at the link in the question shows that the OS is Windows, so gdb is of no use. The information in the Windows dump is minimal, so I think you are out of luck.
My only suggestions are:
try to reproduce the crash in-house.
get the user to reproduce the bug while running a tool that will catch the crash and do a proper memory dump. It is about a decade since I have done serious windows debugging so I don't know what tools are available now - there used to be one called Dr.Watson, but it may be obsolete.
If the user can't reproduce the crash then you are out of luck, on the other hand if it never happens again it is not really that big a problem. ;-)
Update:
Google tells me that Dr Watson is still the default crash handler on Windows XP (and presumably other versions of Windows) - the stack dump that was linked in the question probably came from it. However the default data saved by Dr Watson is fairly minimal, but you can configure it to save more - see this article. In short, if you run drwtsn32 -i it will pop up a dialog to let you set the options.
There's a file named gdbinit in the Python source tree (in Misc/gdbinit) which provides a set of macros for gdb so as to display the current interpreter context. Just type source gdbinit in gdb and then you can execute macros such as pystack. The list of available macros can be obtained simply by reading the file's source code.
(you can find it directly here: http://svn.python.org/view/python/trunk/Misc/gdbinit?view=log).
Of course, if the crash is so severe that it has corrupted the interpreter's internal structures, the macros may fail or crash. Also, it is better to compile the interpreter in debug mode, otherwise gdb may fail to locate the required symbols and variables.
Not sure if it helps, but if you can catch the exception, you could use http://github.com/gooli/pydump to store a dump and load it later in a Python debugger.
Does your application produce a log? If so, you can have logging produce an in-memory log which you might be able to find within the core dump. Also, you can have them send you the log file itself instead of the core dump.

Python object inspector?

besides from using a completely integrated IDE with debugger for python (like with Eclipse), is there any little tool for achieving this:
when running a program, i want to be able to hook somewhere into it (similar to inserting a print statement) and call a window with an object inspector (a tree view)
after closing the window, the program should resume
It doesnt need to be polished, not even absolutely stable, it could be introspection example code for some widget library like wx. Platform independent would be nice though (not a PyObjC program, or something like that on Windows).
Any Ideas ?
Edit:
Yes, i know about pdb, but I'm looking for a graphical tree of all the current objects.
Nevertheless, here is a nice introduction on how to use pdb (in this case in Django):
pdb + Django
Winpdb is a platform independent graphical GPL Python debugger with an object inspector.
It supports remote debugging over a network, multiple threads, namespace modification, embedded debugging, encrypted communication and is up to 20 times faster than pdb.
Some other features:
GPL license. Winpdb is Free Software.
Compatible with CPython 2.3 through 2.6 and Python 3000
Compatible with wxPython 2.6 through 2.8
Platform independent, and tested on Ubuntu Jaunty and Windows XP.
User Interfaces: rpdb2 is console based, while winpdb requires wxPython 2.6 or later.
Here's a screenshot that shows the local object tree at the top-left.
(source: winpdb.org)
pdb isn't windowed, it runs in a console, but it's the standard way to debug in Python programs.
Insert this where you want to stop:
import pdb;pdb.set_trace()
you'll get a prompt on stdout.
If a commercial solution is acceptable, Wingware may be the answer to the OP's desires (Wingware does have free versions, but I don't think they have the full debugging power he requires, which the for-pay versions do provide).
Python Debugging Techniques is worth reading. and it's Reddit's comment is worth reading too. I have really find some nice debug tricks from Brian's comment. such as this comment and this comment.
Of course, WingIDE is cool (for general Python coding and Python code debugging) and I use it everyday. unlucky for WingIDE still can't embedded a IPython at now.
You can use ipython, with the %debug statement. Once your code crashes, you can add breakpoints, see objects etc. A very crude way to kickoff the debugger is to raise Exception at some line of your code, run it in ipython, the type %debug when it crashes.

Categories