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
  • supported remote debug (rpdb)

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) 

Use command line to do remote pdb debug:

python -m pydumpling --rdebug test.deump It will open the debugger on port 4444, then we can access pdb using telnet、netcat... : nc 127.0.0.1 4444 alt text

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.4.tar.gz (9.2 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.4-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydumpling-0.1.4.tar.gz
  • Upload date:
  • Size: 9.2 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.4.tar.gz
Algorithm Hash digest
SHA256 8d1bea9a1546547b4b5cd2c5e1fb360057a31d903d2e3a2d0537f7a5307915cc
MD5 315cf8964e22ffaf9dc1b3f79f159431
BLAKE2b-256 83b185d8978e2c6b9c8c4a073b2799382d04915ddc6e8f9ed6d687b3c481aacd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydumpling-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 9.4 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 32571b07e5d3e155d657d29ba91ffaf5cc3cfd3885e2b29a644486297fc3df47
MD5 80d7d12ec6cc4334d99d41c2fe76d299
BLAKE2b-256 47afe5042c026794b81bef27e6a404d7a8d5809ae5ea94f858cc4a6c7801d4e5

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