GDB Debugging Python Process __python

Source: Internet
Author: User
Tags print print sleep function python script
debugging the Python process with gdbPosted in Python on 2011-10-12 10:08:50, tagged with debug, gdb, Python.

Sometimes we want to debug a running Python process, or a coredump of a python process. For example, a MOD_WSGI process is now dead and does not accept requests to see which line of Python code is running. This is the time to sacrifice the artifact of GDB. Ready to

1. Confirm that your gdb version is >=7,gdb from version 7 to support debug for Python. (ref)

2. Verify that the GDB connection Python is the debug Python, or recompile gdb.

Method:?

1 2 3 4 5 6 7 $ gdb (gdb) python > Import sys >print sys.version >end 2.4.3 (#1, Sep, 19:55:41) [GCC 4.1.2 20080704 ( Red Hat 4.1.2-51)]

In some stable distributions (such as CentOS),python versions will be lower, and they will compile a python use themselves. GDB, which is installed from the source, connects the Python version of the source. For example, in CentOS 5.4, the source Python is 2.4.3, and gdb from the source is connected to the Python 2.4.3.

Note at compile time that you want to add your own compiled Python path to the PATH environment variable so that the GDB configure will find the new Python and connect to it .

3. Download libpython.py how to debug

Suppose the process number you want to debug is 1000?

1 $ gdb-p 1000

Use this command to attach gdb to the process. Load Libpython Script If your gdb is modified by a vendor such as Redhat or fedora, you will have the--python option, which allows you to specify the Python extension script to load when GDB starts (this script is extended to GDB, not the script we need to debug). ?

1 $ gdb--python/path/to/libpython. Py-p 1000
If the GNU gdb is installed, you need to manually load the libpython.py script after you open GDB?
1 2 3 4 5 6 (gdb) python > Import sys >sys.path.insert (0, '/path/to/libpython.py ') > Import Libpython >end (GDB)

You can then use the PY-BT command to print the current thread's Python traceback.

Libpython also provides a number of commands, such as py-print print variables, py-locals print all local variables, and so on, and can open libpython.py view in detail. A little experience in gdb you can use the generate-core-file command to generate a coredump file. You can then use Gdb–core to open the Coredump file for debug. Avoid attach the process all the time, you can quickly restart the recovery service gdb-heap is an extension of gdb. Can print Python memory usage

Reference debuggingwithgdb easierpythondebugging debugging with GDB (GDB documentation)

using GDB to debug a python script

Debugging Python scripts can generally be accomplished by logging logs and using a python-led PDB module , but there are always exceptions to this, and in the following three cases the method is powerless.
1-Paragraph error
2 Daemon Program in operation
3 Core Dump
This time will need to sacrifice gdb for debugging. python2.6 's source code provides some predefined functions so that you can use GDB debugging, we just have to python-2.6/misc/the file The content included in the Gdbinit is added to the. gdbinit file in the user directory so that the macros are automatically defined each time GDB is started. But unfortunately Python2.6.2 Gdbini for the pylocals definition of the error, it appears that the code is not updated with the update. We simply modify the while $_i < f->f_nlocals to the while $_i < F->f_code->co_nlocals. Some of the macro suggestions that are appended to the article are also included in the. gdbinit file, where more macros can be referenced

http://web.archive.org/web/20070915134837/

Http://www.mashebali.com/?Python_GDB_macros:The_Macros.


We first need to construct a Python script that causes a segment error. To be honest , it's not easy to make a mistake with Python, but it's easy to call a library from outside . We named the file gdb_test.py
Import sys, OS, LIBXML2

Def segv_test ():
s = "Options = libxml2. Html_parse_recover + \
LIBXML2. Html_parse_noerror + \
LIBXML2. Html_parse_nowarning
Doc = Libxml2.htmlreaddoc (S, None, ' utf-8 ', options). doc
Ctxt = Doc.xpathnewcontext ()
nodes = Ctxt.xpatheval ('//body/node () ')
Nodes.reverse ()
For nodes:
nexts = Note.xpatheval (' node () ')
Note.unlinknode ()
Note.freenode ()//freenode will release the node and its child nodes
Nexts[0].unlinknode ()
Nexts[0].freenode ()//resource has been released, releasing again will cause a segment error

def main ():
Segv_test ()

if __name__ = = "__main__":
Main ()

Using GDB to run the script, we get a segment error message.
GDB python
R gdb_test.py

GLIBC detected * * * double free or corruption (fasttop): 0x08104570 * * *

Program received signal SIGABRT, aborted.
[Switching to Thread-1208260928 (LWP 26159)]
0X00B987A2 in _dl_sysinfo_int80 () from/lib/ld-linux.so.2

Type BT to get the following stack information:
(GDB)BT
#0 0x00b987a2 in _dl_sysinfo_int80 () from/lib/ld-linux.so.2
#1 0x00c00825 in Raise () from/lib/tls/libc.so.6
#2 0x00c02289 in Abort () from/lib/tls/libc.so.6
#3 0x00c34cda in __libc_message () from/lib/tls/libc.so.6
#4 0x00c3b56f in _int_free () from/lib/tls/libc.so.6
#5 0x00c3b94a in free () from/lib/tls/libc.so.6
#6 0x009812c6 in Xmlfreenode () from/opt/sohumc/lib/libxml2.so.2
#7 0x0029d7f3 in Libxml_xmlfreenode () from/opt/sohumc/lib/python2.6/site-packages/libxml2mod.so
#8 0x00780bae in Pycfunction_call (func=0x8104570, arg=0xd05820, kw=0x6) at objects/methodobject.c:116
#9 0x007d8c79 in Call_function (pp_stack=0xbff8c48c, oparg=0) at python/ceval.c:3679
#10 0x007d6d2b in Pyeval_evalframeex (F=0X8124EF4, throwflag=0) at python/ceval.c:2370
#11 0x007d8e36 in Fast_function (func=0x6, PP_STACK=0XBFF8C5DC, N=1, Na=1, nk=0) at python/ceval.c:3765
#12 0x007d89cd in Call_function (PP_STACK=0XBFF8C5DC, oparg=0) at python/ceval.c:3700
#13 0x007d6d2b in Pyeval_evalframeex (F=0X81242FC, throwflag=0) at python/ceval.c:2370
#14 0x007d8e36 in Fast_function (func=0x6, pp_stack=0xbff8c72c, n=0, na=0, nk=0) at python/ceval.c:3765
#15 0x007d89cd in Call_function (pp_stack=0xbff8c72c, oparg=0) at python/ceval.c:3700
#16 0x007d6d2b in Pyeval_evalframeex (F=0X810A7C4, throwflag=0) at python/ceval.c:2370
#17 0x007d8e36 in Fast_function (func=0x6, pp_stack=0xbff8c87c, n=0, na=0, nk=0) at python/ceval.c:3765
#18 0x007d89cd in Call_function (pp_stack=0xbff8c87c, oparg=0) at python/ceval.c:3700
#19 0x007d6d2b in Pyeval_evalframeex (f=0x8091d0c, throwflag=0) at python/ceval.c:2370
#20 0x007d76f9 in Pyeval_evalcodeex (co=0xb7fa3728, globals=0x6, locals=0xb7f9902c, args=0x0, argcount=0, kws=0x0, Kwcount=0, defs=0x0, defcount=0,
closure=0x0) at python/ceval.c:2942
#21 0X007D47CB in Pyeval_evalcode (co=0xb7fa3728, globals=0xb7f9902c, locals=0xb7f9902c) at python/ceval.c:515
#22 0X007FBBCE in Run_mod (mod=0x80ea780, Filename=0xbffc6be6 "gdb_test.py", globals=0xb7f9902c, locals=0xb7f9902c, FLAGS=0XBFF8CA8C, Arena=0x807ef28)
At python/pythonrun.c:1330
#23 0x007fbb58 in Pyrun_fileexflags (fp=0x8091d00, Filename=0xbffc6be6 "gdb_test.py", start=257, globals=0xb7f9902c, LOCALS=0XB7F9902C, Closeit=1,
FLAGS=0XBFF8CA8C) at python/pythonrun.c:1316
#24 0x007fb22d in Pyrun_simplefileexflags (fp=0x8091d00, Filename=0xbffc6be6 "gdb_test.py", Closeit=1, flags= 0XBFF8CA8C) at python/pythonrun.c:926
#25 0x007facc9 in Pyrun_anyfileexflags (fp=0x8091d00, Filename=0xbffc6be6 "gdb_test.py", Closeit=1, flags=0xbff8ca8c) At python/pythonrun.c:731
#26 0x00808fea in Py_main (Argc=1, Argv=0xbff8cbb4) at modules/main.c:597
#27 0x080486ae in Main (argc=2, Argv=0xbff8cbb4) at modules/python.c:23

Pystack and PYSTACKV Two macros can be used to view the stack inside Python, and you can see the end of the program when it executes to the Freenode function, which is in the 3141 rows of libxml2.py.
(GDB)Pystack
/opt/lib/python2.6/site-packages/libxml2.py (3141): freenode
Gdb_test.py (a): Segv_test
gdb_test.py (): Main
gdb_test.py (): <module>

we can see the invocation relationships of the functions within the script through the stack, so how do we look at the variables inside the function? As everyone, Python's internal stack and function calls are done by Pyeval_evalframeex, and one Pyeval_evalframeex means that a function call, like the above line 19,13,10 corresponds to Main, segv_test, freenode function, after positioning gdb to the corresponding row, use the Pylocals macro to view the details of the internal variables of the function.
(GDB) up 13
#13 0x007d6d2b in Pyeval_evalframeex (F=0X81242FC, throwflag=0) at python/ceval.c:2370
2370 in PYTHON/CEVAL.C
(GDB) Pylocals
S
Object: ' Type:str
Refcount:3
address:0xb7f64440
Options
object:97
Type:int
Refcount:7
Address:0x8082c20
Doc
Object: <xmldoc (None) object at 0xb7cc04ec>
Type:instance
Refcount:1
Address:0xb7cc04ec
Ctxt:
Object: <libxml2.xpathcontext instance at 0xb7f70ccc>
Type:instance
Refcount:1
Address:0xb7f70ccc
Nodes
object: [<xmlnode (儓 x object at 0xb7cc0cac>]
Type:list
Refcount:2
address:0xb7f70a8c
Note
Object: <xmlnode (? 圶. Object at 0xb7cc0cac>
Type:instance
Refcount:2
Address:0xb7cc0cac
Nexts:
object: [<xmlnode HHX object at 0xb7cc750c>, <xmlnode (HXX. Object at 0xb7cc76cc>, <xmlnode (@XX? object at 0xb7c9348c>]
Type:list
Refcount:1
address:0xb7f4ce4c

setting breakpoints When you debug a script is a very troublesome thing, I can think of two ways: 1 according to the function of the Python source of the breakpoint set, 2 use the Sleep function and CTRL + C to interrupt the operation of the program. No matter how it works, it's a pain in the way of debugging, because the Python interpreter itself does a lot of work
Because it is not very familiar with the Python source code, it is only a cursory understanding of how to use GDB for scripting the Python script, and it is a good idea to welcome people to share the experience.


Extra Macros:
Define PBT
Set $i = 0
Set $j = 0
While $i < 1000
Select $i
If $eip >= &am P Pyeval_evalframeex
If $eip < &pyeval_evalcodeex
Echo C frame #
P $i
echo py frame #
P $j br> Set $j = $j +1
x/s (pystringobject*) f->f_code->co_filename)->ob_sval
x/s ((pystringobject*) f-> F_code->co_name)->ob_sval
Echo Line #
P f->f_lineno
End
End
Set $i = $i +1
End
End
Document PBT
Show Python backtrace
End

Define Pyattrlist
Set $dict = * (pydictobject**) ((int) $arg 0) + $arg 0.ob_type.tp_dictoffset)
Set $i = 0
Set $ j = 0
while $i < $dict. Ma_mask
If 0!= $dict. ma_table[$i].me_value
   echo \nattr#:
&NBSP;&NB Sp P $j
   x/s (pystringobject*) $dict. ma_table[$i].me_key). Ob_sval
   pyobjinfo $dict. Ma_ table[$i].me_value
   Set $j = $j +1
End
Set $i = $i +1
End
End
Document Pyattrlist
SH OW pythonic Object Attributes list
End

Define Pyattr
Set $dict = * (pydictobject**) ((int) $arg 0) + $arg 0.ob_type.tp_dictoffset)
Set $i = 0
Set $j = 0
While $i < $dict. ma_mask
If 0!= $dict. ma_table[$i].me_value
if $j = = $arg 1
Set $attr = $dict. ma_table[$i].me_value
Echo $attr:
P $attr
End
Set $j = $j +1
End
Set $i = $i +1
End
End
Document Pyattr
Get Pythonic Object attribute
Usage:pyattr <pyobj> <attr#>
atr# correlates to those shown in Pyattrlist
End

Debug module debugging with PDB
First you choose to run the PY
python-m pdb myscript.py
(PDB) willautomatically stops at the first line and waits for debugging, then you can look at the Help
(PDB)H
Description of these key commands

> Breakpoint Settings
(Pdb) B #断点设置在本py的第10行
or (Pdb) b ots.py:20 #断点设置到 ots.py line 20th
Remove Breakpoint (Pdb) b #查看断点编号
(PDB) CL 2 #删除第2个断点

> Operation
(PDB) n #单步运行
(PDB) s #细点运行 is the next, method
(PDB) C #跳到下个断点
> View
(Pdb) P param #View current variable values
(PDB) L #查看运行到某处代码
(PDB) A #查看全部栈内变量

> If it is indebugging in the command lineFor:
Import PDB def TT ():
Pdb.set_trace ()
For I in range (1, 5):
Print I

>>> TT ()
# it supports n P C.
> < stdin > (3) TT ()
(PDB) n
.

PyDev for Eclipse PyDev for Eclipse profile Zheng Weifang (zhengwf@cn.ibm.com), software engineer, EMC

Introduction: PyDev for Eclipse is a powerful and easy-to-use Eclipse Python IDE plug-in. This article introduces readers to the PyDev Open source project and its installation configuration method, and on this basis, details how to use the PyDev plug-in to transform Eclipse into a powerful and easy-to-use python IDE, using it for development and debugging of Python programs. through this article, readers can not only understand PyDev this open source project, but also in-depth understanding of how to apply the PyDev plug-ins to use Eclipse as the Python IDE for the development and debugging of Python applications.

This article's tags: pydev_introduct, python tag this article.

PyDev Introduction

July 16, 2003, the three-person development team led by Fabio Zadrozny registered a new project in the world's largest open source software development platform and warehouse SourceForge, a powerful eclipse plug-in that enables users to fully leverage Eclipse for development and debugging of Python applications. This project, which can use eclipse as the Python IDE, is PyDev.

The advent of the PyDev plug-in facilitates a large number of Python developers, providing some good features such as syntax error prompts, source Editor assistant, Quick Outline, Globals Browser, Hierarchy View, run and debug, and so on. based on the Eclipse platform, which has many powerful features and is very easy to use, PyDev's features make it more and more popular.

Today, the project is continuing to push ahead with a new release, the latest version is the October 3, 2008 release of 1.3.22. This article describes the installation configuration method of PyDev and, on this basis, describes in detail how to use PyDev to develop and debug the Python IDE as a python.

Back to the top of the page

PyDev Installation and Configuration

Install PyDev

Before installing PyDev, make sure that you have installed Java 1.4 or later, Eclipse, and Python. Next, start installing the PyDev plugin. Start Eclipse and install PyDev with Eclipse Update Manager. Locate the Help bar in the Eclipse menu bar and select Help > Software Updates > Find and Install. Select Search for new features for install, and then click Next. In the displayed window, select New remote site. A dialog box pops up asking for the name and link of the new update site. Here, the name entry PyDev, of course, you can also enter a different name, the link input http://www.fabioz.com/pydev/updates, you can also fill in the http://pydev.sourceforge.net/updates. Then, click Ok.
Figure 1. New Update site

In this way, a new PyDev site is built, select the site, and then Finish. Next, Eclipse's update Manager will search for the installation package in the site just entered, select the results PyDev, and click Next.
Figure 2. Install Pydev

Read the license Terms and, if accepted, click Next. Go to the Installation path selection interface, use the default settings, and Finish. Eclipse Update Manager Downloads PyDev, and you can see the progress of the download from the Eclipse taskbar. After downloading, display an interface that requires you to confirm the installation, click Install All to start the installation.

After installation, you will need to restart Eclipse for the installation to take effect.

Verify that the PyDev is installed successfully

How can I verify that the Eclipse Update Manager has successfully installed the required PyDev plug-in?

Select Help->about Eclipse sdk->plug-in Details, which will appear in the About Eclipse SDK plug-ins window, which lists all installed Eclipse Plug-ins. Check to see if there are at least five plug-ins in the Plug-in Id column that begin with Com.python.pydev and Org.python.pydev respectively. If it is, then Pydev has been successfully installed, otherwise, installed some problems, need to do specific analysis according to the specific problems.
Figure 3. Verifying PyDev Plug-ins

Configure PyDev

After installing the PyDev, you need to configure the Python/jython interpreter, which is simple to configure.

In the Eclipse menu bar, select Window > Preferences > Pydev > Interpreter-(Python/jython), where you configure the Python/jython interpreter, following the Python Provides an example of how to configure.

You first need to add an installed interpreter. Here, Python is installed under the C:\Python25 path. Click New, select the Python interpreter Python.exe, open A window that contains a lot of check boxes, select the path you want to add to the system Pythonpath, and click Ok.
Figure 4. Configure PyDev

Next, check to see if the results of the configuration are correct.

In System Pythonpath, check to see if those paths that were added to the configuration process are included. Here is a list of all the library folders required by the system.

In addition, in forced Builtin Libs, a built-in library of Python is listed. For Python, there are about 50 such built-in libraries, and for Jython, there are about 30.

In this way, the Python interpreter is configured.

Back to the top of the page

PyDev Package Explorer

Create a project

Before you start your work, you need to create a new project. In the Eclipse menu bar, select File > New > Project > Pydev > Pydev Project, click Next.
Figure 5. Create a Pydev Project

At this point, display the Pydev Project window, enter the project name, select the work path, select the version type of the Python interpreter, select the check box, click Next, go to the window of the associated project, or click Finish to finish creating the project if you don't need to associate other items.

Creating Python Packages and modules

Next, start creating Python packages and modules in the project you just created. Into the Pydev perspective, in Python Package Explorer, right-click SRC, select New->pydev Package, enter the Package name and click Finish,python Package to create it, at this point, automatically generate __ init__.py file, which does not contain any content.

Note: If the CREATE default SRC folder and add it to the Pythonpath check box is not selected when the project is created, you need to pass File > New > Other > Source Fo Lder manually create a source code folder. After you create the Pydev Package, right-click the created package, select New->pydev module, enter the module name, and click Finish. In this way, the Python module is built.

Edit source Program

Some basic editing methods for the source program are not introduced. The following are some of the most useful editing features provided by Pydev. syntax error hints

In the process of creating a modified program, the Python developer can find the grammatical errors in the editing process in time, which is very important to the quality and progress of the whole project development. In the Python perspective, the source code for the project is listed in Pydev Package Explorer, one of the Python files is double-clicked, and if the file contains syntax errors, the error is displayed in a very eye-catching way.
Figure 6. Pydev file Syntax error hint

If you want to show all the files that contain grammatical errors throughout your project, you can switch from the Python perspective to the Java perspective. In the Java Package, an eye-catching little red fork marks all python files that contain grammatical errors.
Figure 7. Pydev Project syntax error hint

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.