Skip to main content

Python post-mortem debugger

Project description

Python post-mortem debugging

English | 简体中文

It's a fork/optimized version from elifiner/pydump.The main optimization points are:

  • Save the Python traceback anywhere, not just when it's an exception.
  • Optimize code structure && remove redundant code
  • fix bug in python3.10+
  • supported more pdb commnd
  • a useful command line tool for debug

Pydumpling writes the python current traceback into a file and can later load it in a Python debugger. It works with the built-in pdb and with other popular debuggers (pudb, ipdb and pdbpp).

Why use pydumpling?

  • We usually use try... except ... to catch exceptions that occur in our programs, but do we really know why they occur?
  • When your project is running online, you suddenly get an unexpected exception that causes the process to exit. How do you reproduce this problem?
  • Not enough information in the logs to help us pinpoint online issues?
  • If we were able to save the exception error and then use the debugger to recover the traceback at that time, we could see the entire stack variables along the traceback as if you had caught the exception at the local breakpoint.

Install pydumpling

Python version:>=3.7

pip install pydumpling

How to use pydumpling

Save the python traceback anywhere.

from pydumpling import dump_current_traceback
from inspect import currentframe


def inner():
    a = 1
    b = "2"
    dump_current_traceback("test.dump")
    c = str(a) + b


def outer():
    d = 4
    inner()

Save the exception traceback.

In the code, find the place where we need to do the try ... except ... and use save_dumpling(). When we save the dump file, it will default to ${exception filename}:${error lineno}.dump.

from pydumpling import save_dumping

def inner():
    a = 1
    b = "2"
    c = a + b


def outer():
    inner()


if __name__ == "__main__":
    try:
        outer()
    except Exception:
        save_dumping("test.dump")

Now we have the test.dump file, which we can use debub_dumpling to do pdb debug:

Python 3.10.6 (main, Aug  1 2022, 20:38:21) [GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pydumpling import debug_dumpling
>>> debug_dumpling("test.dump")
> /home/loyd/vscodeFiles/pydumpling/test.py(6)inner()
-> c = a + b
(Pdb) list 1,17
  1     from pydumpling import save_dumping
  2  
  3     def inner():
  4  >>     a = 1
  5         b = "2"
  6  ->     c = a + b
  7  
  8  
  9     def outer():
 10         inner()
 11  
 12  
 13     if __name__ == "__main__":
 14         try:
 15             outer()
 16         except Exception:
 17             save_dumping("test.dump")
(Pdb) ll
  3     def inner():
  4  >>     a = 1
  5         b = "2"
  6  ->     c = a + b
(Pdb) bt
  /home/loyd/vscodeFiles/pydumpling/test.py(15)<module>()
-> outer()
  /home/loyd/vscodeFiles/pydumpling/test.py(10)outer()
-> inner()
> /home/loyd/vscodeFiles/pydumpling/test.py(6)inner()
-> c = a + b
(Pdb) pp a
1
(Pdb) pp b
'2'
(Pdb) u
> /home/loyd/vscodeFiles/pydumpling/test.py(10)outer()
-> inner()
(Pdb) ll
  9     def outer():
 10  ->     inner()
(Pdb) 

Use Command Line

Use command line to print the traceback: python -m pydumpling --print test.deump

It will print:

Traceback (most recent call last):
  File "/workspaces/pydumpling/tests/test_dump.py", line 20, in test_dumpling
    outer()
  File "/workspaces/pydumpling/tests/test_dump.py", line 14, in outer
    inner()
  File "/workspaces/pydumpling/tests/test_dump.py", line 10, in inner
    c = a + b  # noqa: F841
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Use command line to do pdb debug: python -m pydumpling --debug test.deump

It will open the pdb window:

-> c = a + b
(Pdb) 

TODO

  • []

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydumpling-0.1.3.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydumpling-0.1.3-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file pydumpling-0.1.3.tar.gz.

File metadata

  • Download URL: pydumpling-0.1.3.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for pydumpling-0.1.3.tar.gz
Algorithm Hash digest
SHA256 47fda2983533c937f25d6dc75a3c318a734b49f4a822b3465c904b94c9842ed5
MD5 764e7d492aa7d11df288ebc7602228d2
BLAKE2b-256 13c5f81aedfbd4dbe812e077d6b5565a06ad4f1f992ac5b4b48b9a4477019555

See more details on using hashes here.

File details

Details for the file pydumpling-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pydumpling-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for pydumpling-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6653f004c000e9461ef9f62e0e017f5375f1a7c8a9692cb7b548b9500b1f366c
MD5 91b2888f372c3bc5d1d865a37fa897b1
BLAKE2b-256 f48a29c86174c4d83064b79143eb837d5b35ff215a98bbcd7a5c4793d21e4afd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page