1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 | Author: Gerhard Haering (gerhard@bigfoot.de)
0. Scope of this HOWTO
----------------------
This howto describes how to debug *native* win32 Python extensions with open
source tools. See Appendix A for how to do this with Cygwin's Python (which is
much simpler).
1. Prepare Python for development with gcc
------------------------------------------
- Install native Python: http://www.python.org/
- Install Cygwin (you'll need gcc, gdb, ...) http://www.cygwin.com/
- Get the dll2def package from
http://home.trouwweb.nl/Jerry/packages.html#LIB2DEF unzip and put the
executable somewhere in your path (it put such stuff in c:\opt\tools)
- Open the Cygwin shell and chdir to the libs directory of your Python
installation
- Create a python21.def file
$ dll2def c:/winnt/system32/python21.dll >python21.def
(Adjust the path to the python dll if you use Windows 9x or don't have your
OS installed in c:/winnt)
- Create a libpython21.a file
$ dlltool --dllname python21.dll --def python21.def --output-lib \
libpython21.a
- Create a fake debugging environment for distutils
(the debugging files download from python.org is of no use, because Cygwin
gdb cannot understand the debugger symbol format used by Visual C++,
which is used by the Python crew to build the native win32 Python).
$ cp libpython21.a libpython21_d.a
- Steps to make your life easier:
-- Put the directory of the native Python in your PATH.
-- Copy the native python.exe to something like ntpython.exe to avoid
confusion with Cygwin's Python
2. Write the extension module
-----------------------------
Here's a buggy extension module:
/* BEGIN FILE crash.c ***********************************************/
#include "Python.h"
PyObject* crash()
{
char* s = "test!";
s[0] = 'T'; /* This is asking for trouble :-) */
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef crashMethods[] = {
{"crash", (PyCFunction)crash, NULL, NULL},
{NULL, NULL}
};
DL_EXPORT(void) initcrash(void)
{
PyObject *m;
m = Py_InitModule("crash", crashMethods);
}
/* END FILE crash.c ************************************************/
And the setup.py file to build it:
###### BEGIN setup.py ###############################################
import sys
from distutils.core import setup
from distutils.extension import Extension
setup (
name = "crash",
ext_modules = [Extension(
name="crash",
sources = [ "crash.c" ]
)],
)
###### END setup.py --###############################################
3. Build and debug the extension module
---------------------------------------
$ ntpython setup.py build --compiler=mingw32 --debug
$ cd build/lib.win32-2.1/
$ mv crash_d.pyd crash.pyd # workaround for distutils
$ gdb ntpython
- Run the application in gdb
- Then:
Python 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import crash
>>> crash.crash()
- Gdb will point out where the segmentation fault happened
APPENDIX A
----------
How to debug Python extensions on Cygwin.
- You'll need Cygwin installed (the full development environment, including
gcc, gdb, python, ...)
- write a setup.py file (see above)
- compile the extension with debugging options:
$ python setup.py build --debug
- $ cd build/lib.cygwin_nt-5.0-1.3.1-i686-2.1
- $ gdb python
- Run the application
- "import crash"
- "crash.crash()"
APPENDIX B
----------
Unresolved issues:
- How to set breakpoints in extension modules
|