1 This patch adds Python dtrace support. Note it is necessary to modify
2 test_sys.py to add an integer to the frameobject structure size since this
3 patch adds "int f_calllineno" to the structure, so this test does not fail.
5 --- Python-2.7.6/Makefile.pre.in.~1~ 2013-11-09 23:36:41.000000000 -0800
6 +++ Python-2.7.6/Makefile.pre.in 2014-05-14 12:54:43.824219677 -0700
8 # Used of signalmodule.o is not available
9 SIGNAL_OBJS= @SIGNAL_OBJS@
11 +DTRACE_OBJS=Python/dtrace.o Python/phelper.o
13 ##########################################################################
16 Python/formatter_unicode.o \
17 Python/formatter_string.o \
18 Python/$(DYNLOADFILE) \
24 Python/formatter_string.o: $(srcdir)/Python/formatter_string.c \
27 +Python/phelper.o: $(srcdir)/Python/phelper.d
28 + dtrace -o $@ -DPHELPER $(DFLAGS) $(CPPFLAGS) -C -G -s $(srcdir)/Python/phelper.d
30 +Python/python.h: $(srcdir)/Python/python.d
31 + dtrace -o $@ $(DFLAGS) -C -h -s $(srcdir)/Python/python.d
33 +Python/ceval.o: Python/ceval.c Python/python.h
34 + $(CC) -c -I. -IPython $(BASECFLAGS) $(EXTRA_CFLAGS) $(CPPFLAGS) $(CFLAGSFORSHARED) $(CFLAGS) -DPy_BUILD_CORE -o $@ $<
36 +Python/dtrace.o: $(srcdir)/Python/python.d Python/ceval.o
37 + dtrace -o $@ $(DFLAGS) -C -G -s $(srcdir)/Python/python.d Python/ceval.o
39 ############################################################################
42 --- Python-2.7.6/Include/frameobject.h.~1~ 2013-11-09 23:36:39.000000000 -0800
43 +++ Python-2.7.6/Include/frameobject.h 2014-05-14 13:03:19.938777249 -0700
45 PyCode_Addr2Line to calculate the line from the current
47 int f_lineno; /* Current line number */
48 + int f_calllineno; /* line number of call site */
49 int f_iblock; /* index in f_blockstack */
50 PyTryBlock f_blockstack[CO_MAXBLOCKS]; /* for try and loop blocks */
51 PyObject *f_localsplus[1]; /* locals+stack, dynamically sized */
52 --- Python-2.7.6/Objects/frameobject.c.~1~ 2013-11-09 23:36:41.000000000 -0800
53 +++ Python-2.7.6/Objects/frameobject.c 2014-05-14 12:56:06.970076859 -0700
58 + f->f_calllineno = code->co_firstlineno;
59 f->f_lineno = code->co_firstlineno;
62 --- Python-2.7.6/Python/ceval.c.~1~ 2013-11-09 23:36:41.000000000 -0800
63 +++ Python-2.7.6/Python/ceval.c 2014-05-14 13:04:01.334853206 -0700
75 #define READ_TIMESTAMP(var)
82 +dtrace_entry(PyFrameObject *f)
84 + const char *filename;
88 + filename = PyString_AsString(f->f_code->co_filename);
89 + fname = PyString_AsString(f->f_code->co_name);
90 + lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
92 + PYTHON_FUNCTION_ENTRY((char *)filename, (char *)fname, lineno);
95 + * Currently a USDT tail-call will not receive the correct arguments.
96 + * Disable the tail call here.
104 +dtrace_return(PyFrameObject *f)
106 + const char *filename;
110 + filename = PyString_AsString(f->f_code->co_filename);
111 + fname = PyString_AsString(f->f_code->co_name);
112 + lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
113 + PYTHON_FUNCTION_RETURN((char *)filename, (char *)fname, lineno);
116 + * Currently a USDT tail-call will not receive the correct arguments.
117 + * Disable the tail call here.
119 +#if defined(__sparc)
124 +#define PYTHON_FUNCTION_ENTRY_ENABLED 0
125 +#define PYTHON_FUNCTION_RETURN_ENABLED 0
126 +#define dtrace_entry()
127 +#define dtrace_return()
130 /* Interpreter main loop */
133 return PyEval_EvalFrameEx(f, 0);
137 + * These shenanigans look like utter madness, but what we're actually doing is
138 + * making sure that the ustack helper will see the PyFrameObject pointer on the
139 + * stack. We have two tricky cases:
143 + * We use up the six registers for passing arguments, meaning the call can't
144 + * use a register for passing 'f', and has to push it onto the stack in a known
147 + * And how does "throwflag" figure in to this? -PN
151 + * Here the problem is that (on 32-bit) the compiler is re-using %i0 before
152 + * some calls inside PyEval_EvalFrameReal(), which means that when it's saved,
153 + * it's just some junk value rather than the real first argument. So, instead,
154 + * we trace our proxy PyEval_EvalFrame(), where we 'know' the compiler won't
155 + * decide to re-use %i0. We also need to defeat optimization of our proxy.
158 +#if defined(HAVE_DTRACE)
160 +#if defined(__amd64)
161 +PyObject *PyEval_EvalFrameExReal(long, long, long, long, long, long,
162 + PyFrameObject *, int throwflag);
167 +PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
169 + volatile PyObject *f2;
170 + f2 = PyEval_EvalFrameExReal(0, 0, 0, 0, 0, 0, f, throwflag);
171 + return (PyObject *)f2;
175 +PyEval_EvalFrameExReal(long a1, long a2, long a3, long a4, long a5, long a6,
176 + PyFrameObject *f, int throwflag)
179 +#elif defined(__sparc)
181 +PyObject *PyEval_EvalFrameExReal(PyFrameObject *f, int throwflag);
186 +PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
188 + volatile PyObject *f2;
189 + f2 = PyEval_EvalFrameExReal(f, throwflag);
190 + dummy = f->ob_refcnt;
191 + return (PyObject *)f2;
195 +PyEval_EvalFrameExReal(PyFrameObject *f, int throwflag)
198 +#else /* __amd64 || __sparc */
201 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
204 +#endif /* __amd64 || __sparc */
206 +#else /* don't HAVE_DTRACE */
209 +PyEval_EvalFrameexEx(PyFrameObject *f, int throwflag))
212 +#endif /* HAVE_DTRACE */
214 #ifdef DYNAMIC_EXECUTION_PROFILE
215 #undef USE_COMPUTED_GOTOS
217 @@ -910,6 +1039,11 @@
222 + if (PYTHON_FUNCTION_ENTRY_ENABLED())
227 names = co->co_names;
228 consts = co->co_consts;
229 @@ -2660,6 +2794,9 @@
234 + f->f_calllineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
237 x = call_function(&sp, oparg, &intr0, &intr1);
239 @@ -2701,6 +2838,9 @@
244 + f->f_calllineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
246 READ_TIMESTAMP(intr0);
247 x = ext_do_call(func, &sp, flags, na, nk);
248 READ_TIMESTAMP(intr1);
249 @@ -3001,6 +3141,10 @@
254 + if (PYTHON_FUNCTION_RETURN_ENABLED())
257 Py_LeaveRecursiveCall();
258 tstate->frame = f->f_back;
261 +++ Python-2.6.4/Python/phelper.d
265 + * Python ustack helper. This relies on the first argument (PyFrame *) being
266 + * on the stack; see Python/ceval.c for the contortions we go through to ensure
267 + * this is the case.
269 + * On x86, the PyFrame * is two slots up from the frame pointer; on SPARC, it's
274 + * Yes, this is as gross as it looks. DTrace cannot handle static functions,
275 + * and our stat_impl.h has them in ILP32.
280 +#include <sys/types.h>
284 +#include "pystate.h"
285 +#include "pyarena.h"
286 +#include "pythonrun.h"
287 +#include "compile.h"
288 +#include "frameobject.h"
289 +#include "stringobject.h"
292 +#define startframe PyEval_EvalFrameEx
293 +#define endframe PyEval_EvalCodeEx
294 +#elif defined(__amd64)
295 +#define PyEval_EvalFrameEx PyEval_EvalFrameExReal
296 +#define startframe PyEval_EvalFrameExReal
297 +#define endframe PyEval_EvalCodeEx
298 +#elif defined(__sparc)
299 +#define PyEval_EvalFrameEx PyEval_EvalFrameExReal
300 +#define startframe PyEval_EvalFrameEx
301 +#define endframe PyEval_EvalFrameExReal
305 +#define STACK_BIAS (2048-1)
307 +#define STACK_BIAS 0
311 + * Not defining PHELPER lets us test this code as a normal D script.
315 +#define at_evalframe(addr) \
316 + ((uintptr_t)addr >= ((uintptr_t)&``startframe) && \
317 + (uintptr_t)addr < ((uintptr_t)&``endframe))
318 +#define probe dtrace:helper:ustack:
319 +#define print_result(r) (r)
321 +#if defined(__i386) || defined(__amd64)
322 +#define frame_ptr_addr ((uintptr_t)arg1 + sizeof(uintptr_t) * 2)
323 +#elif defined(__sparc)
324 +#define frame_ptr_addr ((uintptr_t)arg1 + STACK_BIAS + sizeof(uintptr_t) * 8)
326 +#error unknown architecture
331 +#define at_evalframe(addr) (1)
332 +#define probe pid$target::PyEval_EvalFrame:entry
333 +#define print_result(r) (trace(r))
335 +#if defined(__i386) || defined(__amd64)
336 +#define frame_ptr_addr ((uintptr_t)uregs[R_SP] + sizeof(uintptr_t))
337 +#elif defined(__sparc)
339 + * Not implemented: we could just use R_I0, but what's the point?
342 +#error unknown architecture
345 +#endif /* PHELPER */
347 +extern uintptr_t PyEval_EvalFrameEx;
348 +extern uintptr_t PyEval_EvalCodeEx;
350 +#define copyin_obj(addr, obj) ((obj *)copyin((uintptr_t)addr, sizeof(obj)))
351 +#define pystr_addr(addr) ((char *)addr + offsetof(PyStringObject, ob_sval))
352 +#define copyin_str(dest, addr, obj) \
353 + (copyinto((uintptr_t)pystr_addr(addr), obj->ob_size, (dest)))
354 +#define add_str(addr, obj) \
355 + copyin_str(this->result + this->pos, addr, obj); \
356 + this->pos += obj->ob_size; \
357 + this->result[this->pos] = '\0';
358 +#define add_digit(nr, div) ((nr / div) ? \
359 + (this->result[this->pos++] = '0' + ((nr / div) % 10)) : \
360 + (this->result[this->pos] = '\0'))
361 +#define add_char(c) (this->result[this->pos++] = c)
363 +probe /at_evalframe(arg0)/
365 + this->framep = *(uintptr_t *)copyin(frame_ptr_addr, sizeof(uintptr_t));
366 + this->frameo = copyin_obj(this->framep, PyFrameObject);
367 + this->codep = this->frameo->f_code;
368 + this->lineno = this->frameo->f_calllineno;
369 + this->codeo = copyin_obj(this->codep, PyCodeObject);
370 + this->filenamep = this->codeo->co_filename;
371 + this->fnamep = this->codeo->co_name;
372 + this->filenameo = copyin_obj(this->filenamep, PyStringObject);
373 + this->fnameo = copyin_obj(this->fnamep, PyStringObject);
375 + this->len = 1 + this->filenameo->ob_size + 1 + 5 + 2 +
376 + this->fnameo->ob_size + 1 + 1;
378 + this->result = (char *)alloca(this->len);
382 + add_str(this->filenamep, this->filenameo);
384 + add_digit(this->lineno, 10000);
385 + add_digit(this->lineno, 1000);
386 + add_digit(this->lineno, 100);
387 + add_digit(this->lineno, 10);
388 + add_digit(this->lineno, 1);
391 + add_str(this->fnamep, this->fnameo);
393 + this->result[this->pos] = '\0';
395 + print_result(stringof(this->result));
398 +probe /!at_evalframe(arg0)/
402 diff --git Python-2.6.4/Python/python.d Python-2.6.4/Python/python.d
405 +++ Python-2.6.4/Python/python.d
408 + probe function__entry(const char *, const char *, int);
409 + probe function__return(const char *, const char *, int);
412 +#pragma D attributes Evolving/Evolving/Common provider python provider
413 +#pragma D attributes Private/Private/Common provider python module
414 +#pragma D attributes Private/Private/Common provider python function
415 +#pragma D attributes Evolving/Evolving/Common provider python name
416 +#pragma D attributes Evolving/Evolving/Common provider python args
417 --- Python-2.7.6/Lib/test/test_sys.py.~1~ 2013-11-09 23:36:40.000000000 -0800
418 +++ Python-2.7.6/Lib/test/test_sys.py 2014-05-14 13:07:05.332748121 -0700
420 nfrees = len(x.f_code.co_freevars)
421 extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
423 - check(x, vsize('12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
424 + check(x, vsize('12P4i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
427 check(func, size('9P'))