Call the python debugger at any time

Often I'm writing Python code and want to call the debugger at some point. If you know the situation you want to call in advance, you can simply specify a breakpoint in pdb or embed "import pdb; pdb.set_trace ()" in your code, but it's completely "arbitrary" timing. That is not the case. When I investigated whether it was possible to attach to a running process in gdb, StackOverflow had an elegant method, so I will record it here.

python - Attaching a process with pdb - Stack Overflow

python


import signal
def handle_pdb(sig, frame):
    import pdb
    pdb.Pdb().set_trace(frame)
if __name__ == '__main__':
    signal.signal(signal.SIGUSR1, handle_pdb)

What we're doing is simply calling the pdb when the signal SIGUSR1 is called. You can use it unless you are already using SIGUSR1 for other purposes. You may use other available signals.

cf. use gdb directly

Python is integrated in gdb nowadays, and advanced operations are possible. If you have a python debug symbol running, I think it's possible to attach it directly with gdb and trace the source code at the python level. The Material presented by Mr. Nojima at the Grand Unified Debian Study Group in 2013 will be helpful. Here we are doing source level debugging of php. The handling of debug symbols differs depending on the OS, distribution, and installation method, so please read that point as appropriate.

Recommended Posts

Call the python debugger at any time
At the time of python update on ubuntu
Get a datetime instance at any time of the day in Python
Call the API with python3.
See python for the first time
[Python 3.8 ~] Rewrite arrays etc. at the same time as definition [tips]
Python Master RTA for the time being
MongoDB for the first time in Python
Python Basic Course (at the end of 15)
Install the Python module in any directory
[Python] [Machine learning] Beginners without any knowledge try machine learning for the time being
[Python] How to open two or more files at the same time
First time python
[Python] Colored map plot at the city level
[python] Check the elements of the list all, any
33 selections that the soshage engineer emphasizes during code review [added at any time]
Use logger with Python for the time being
python time measurement
I tried python programming for the first time.
Send Gmail at the end of the process [Python]
First time python
python at docker
Loop variables at the same time in the template
Turn multiple lists with a for statement at the same time in Python
Remove specific strings at the end of python
Call Polly from the AWS SDK for Python
What I got into Python for the first time
I tried Python on Mac for the first time.
Run with CentOS7 + Apache2.4 + Python3.6 for the time being
[Python] Measures and displays the time required for processing
Output the time from the time the program was started in python
I tried python on heroku for the first time
Visualize data and understand correlation at the same time
This time I learned Python I and II at Progate.
Call your own python module from the ROS package
Tasks at the start of a new python project
Python: Time Series Analysis
Find the maximum Python
Function execution time (Python)
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ②
the zen of Python
How to start the PC at a fixed time every morning and execute the python program
Python time series question
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ①
Output python execution time
Time floor function (Python)
[Python] Split the date
python get current time
Python built-in function ~ divmod ~ Let's get the quotient and remainder of division at the same time
I was hooked for 2 minutes with the Python debugger pdb
Try using the Python web framework Django (2) --Look at setting.py
wxPython: Draw animation and graph drawing at the same time
How to use the __call__ method in a Python class
Grep so that grep does not appear at the time of grep
Introducing yourself at Qiita for the first time (test post)
[Python3] Call by dynamically specifying the keyword argument of the function
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Get the current date and time in Python, considering the time difference
Take a look at the Python built-in exception tree structure
Automatically work on the python project specified at zsh login
Reverse lookup Numpy / Pandas (will be updated at any time)