diff -r 736b0aec412b -r d7c58422eada Doc/library/debug.rst
--- a/Doc/library/debug.rst Thu Nov 24 22:00:46 2011 +0200
+++ b/Doc/library/debug.rst Sun Nov 27 05:30:58 2011 +0100
@@ -15,3 +15,4 @@
profile.rst
timeit.rst
trace.rst
+ dtrace.rst
diff -r 736b0aec412b -r d7c58422eada Doc/library/dtrace.rst
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Doc/library/dtrace.rst Sun Nov 27 05:30:58 2011 +0100
@@ -0,0 +1,183 @@
+:mod:`dtrace` --- DTrace probes for Python
+===============================================
+
+.. module:: dtrace
+ :synopsis: DTrace probes for Python.
+
+**Source code:** :source:`Lib/dtrace.py`
+
+--------------
+
+The :mod:`dtrace` module indicates if the CPython executable currently
+running has been compiled with DTrace probes support.
+
+.. impl-detail::
+
+ DTrace probes are implementation details of the CPython interpreter!
+ No garantees are made about probe compatibility between versions of
+ CPython. DTrace scripts can stop working or work incorrectly without
+ warning when changing CPython versions.
+
+The :mod:`dtrace` module defines the following variable:
+
+
+.. data:: available
+
+ The variable will be ``True`` if the current CPython interpreter was
+ compiled with DTrace probe support. ``False`` if not.
+
+
+DTrace probes
+-------------
+
+DTrace scripts are run externally to CPython. DTrace probes export
+selected events inside CPython interpreter in order to make them
+accesible to external scripts.
+
+The probes are exported thru the "python" provider. The available probes
+are defined in the file :file:`Include/pydtrace.d`.
+
+To learn how to use DTrace, read `DTrace User Guide
+`_.
+
+.. opcode:: function-entry (arg0, arg1, arg2)
+
+ Fires when python code enters a new function. *arg0* is sourcecode
+ file path, *arg1* is the name of the funcion called, and *arg2* is
+ line number.
+
+ The probe is not fired if Python code calls C functions.
+
+.. opcode:: function-return (arg0, arg1, arg2)
+
+ Fires when Python code finishes execution of a function. Parameters
+ are the same that in ``function-entry``.
+
+ The probe is not fired if the finishing function is written in C.
+
+.. opcode:: line (arg0, arg1, arg2)
+
+ Fires when Python code change execution line. Parameters are the same
+ that in ``function-entry``.
+
+ The probe is not fired in C functions.
+
+.. opcode:: gc-start (arg0)
+
+ Fires when the Python interpreter starts a garbage collection cycle.
+ *arg0* is the generation to scan, like :func:`gc.collect()`.
+
+.. opcode:: gc-done (arg0)
+
+ Fires when the Python interpreter finishes a garbage collection
+ cycle. *arg0* is the number of collected objects.
+
+.. opcode:: instance-new-start (arg0, arg1)
+
+ Fires when an object instanciation starts. *arg0* is the class name,
+ *arg1* is the filename where the class is defined.
+
+ The probe is not fired for most C code object creation.
+
+.. opcode:: instance-new-done (arg0, arg1)
+
+ Fires when an object instanciation finishes. Parameters are the same
+ that in ``instance-new-done``.
+
+ The probe is not fired for most C code object creation.
+
+.. opcode:: instance-delete-start (arg0, arg1)
+
+ Fires when an object instance is going to be destroyed. Parameters
+ are the same that in ``instance-new-done``.
+
+ The probe is not fired for most C code object destruction.
+
+.. opcode:: instance-delete-done (arg0, arg1)
+
+ Fires when an object instance has been destroyed. parameters are the
+ same that in ``instance-new-done``.
+
+ Between an ``instance-delete-start`` and corresponding
+ ``instance-delete-done`` others probes can fire if, for instance,
+ deletion of an instance creates a deletion cascade.
+
+ The probe is not fired for most C code object destruction.
+
+
+Python stack
+------------
+
+When a DTrace probe is fired, the DTrace script can examine the stack.
+Since CPython is a Python interpreter coded in C, the stack will show C
+functions, with no direct relation to the Python code currently being
+executed.
+
+Using the special "jstack()" DTrace function, the user will be given
+hints about the python program stack, if possible. In particular, the
+argumented stack will show python function calls, filename, name
+of the function or method, and the line number.
+
+DTrace scripts examples
+-----------------------
+
+DTrace python provider is suffixed by the pid of the process to monitor.
+In the examples, the pid will be 9876.
+
+Show the time spend doing garbage collection (in nanoseconds)::
+
+ python9876:::gc-start
+ {
+ self->t = timestamp;
+ }
+
+ python9876:::gc-done
+ /self->t/
+ {
+ printf("%d", timestamp-self->t);
+ self->t = 0;
+ }
+
+Count how many instances are created of each class::
+
+ python9876:::instance-new-start
+ {
+ @v[copyinstr(arg1), copyinstr(arg0)] = count();
+ }
+
+Observe time spent in object destruction, useful if datastructures are
+complicated and deletion of an object can create a cascade effect::
+
+ python9876:::instance-delete-start
+ /self->t==0/
+ {
+ self->t = timestamp;
+ self->level = 0;
+ }
+
+ python9876:::instance-delete-start
+ /self->t/
+ {
+ self->level += 1;
+ }
+
+ python9876:::instance-delete-done
+ /(self->level) && (self->t)/
+ {
+ self->level -= 1;
+ }
+
+ python9876:::instance-delete-done
+ /(self->level==0) && (self->t)/
+ {
+ @time = quantize(timestamp-self->t);
+ self->t = 0;
+ }
+
+To know which python source code lines create new TCP/IP connections::
+
+ pid9876::sock_connect:entry
+ {
+ @conn[jstack()] = count();
+ }
+
diff -r 736b0aec412b -r d7c58422eada Include/code.h
--- a/Include/code.h Thu Nov 24 22:00:46 2011 +0200
+++ b/Include/code.h Sun Nov 27 05:30:58 2011 +0100
@@ -7,6 +7,8 @@
extern "C" {
#endif
+#include "pyconfig.h"
+
/* Bytecode object */
typedef struct {
PyObject_HEAD
@@ -25,6 +27,10 @@
unsigned char *co_cell2arg; /* Maps cell vars which are arguments. */
PyObject *co_filename; /* unicode (where it was loaded from) */
PyObject *co_name; /* unicode (name, for reference) */
+#ifdef WITH_DTRACE
+ PyObject *co_filename_encoded; /* UTF8 version */
+ PyObject *co_name_encoded; /* UTF8 version */
+#endif
int co_firstlineno; /* first source line number */
PyObject *co_lnotab; /* string (encoding addr<->lineno mapping) See
Objects/lnotab_notes.txt for details. */
diff -r 736b0aec412b -r d7c58422eada Include/dynamic_annotations.h
--- a/Include/dynamic_annotations.h Thu Nov 24 22:00:46 2011 +0200
+++ b/Include/dynamic_annotations.h Sun Nov 27 05:30:58 2011 +0100
@@ -325,46 +325,46 @@
#else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
- #define _Py_ANNOTATE_RWLOCK_CREATE(lock) /* empty */
- #define _Py_ANNOTATE_RWLOCK_DESTROY(lock) /* empty */
- #define _Py_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) /* empty */
- #define _Py_ANNOTATE_RWLOCK_RELEASED(lock, is_w) /* empty */
- #define _Py_ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) /* */
- #define _Py_ANNOTATE_BARRIER_WAIT_BEFORE(barrier) /* empty */
- #define _Py_ANNOTATE_BARRIER_WAIT_AFTER(barrier) /* empty */
- #define _Py_ANNOTATE_BARRIER_DESTROY(barrier) /* empty */
- #define _Py_ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) /* empty */
- #define _Py_ANNOTATE_CONDVAR_WAIT(cv) /* empty */
- #define _Py_ANNOTATE_CONDVAR_SIGNAL(cv) /* empty */
- #define _Py_ANNOTATE_CONDVAR_SIGNAL_ALL(cv) /* empty */
- #define _Py_ANNOTATE_HAPPENS_BEFORE(obj) /* empty */
- #define _Py_ANNOTATE_HAPPENS_AFTER(obj) /* empty */
- #define _Py_ANNOTATE_PUBLISH_MEMORY_RANGE(address, size) /* empty */
- #define _Py_ANNOTATE_UNPUBLISH_MEMORY_RANGE(address, size) /* empty */
- #define _Py_ANNOTATE_SWAP_MEMORY_RANGE(address, size) /* empty */
- #define _Py_ANNOTATE_PCQ_CREATE(pcq) /* empty */
- #define _Py_ANNOTATE_PCQ_DESTROY(pcq) /* empty */
- #define _Py_ANNOTATE_PCQ_PUT(pcq) /* empty */
- #define _Py_ANNOTATE_PCQ_GET(pcq) /* empty */
- #define _Py_ANNOTATE_NEW_MEMORY(address, size) /* empty */
- #define _Py_ANNOTATE_EXPECT_RACE(address, description) /* empty */
- #define _Py_ANNOTATE_BENIGN_RACE(address, description) /* empty */
- #define _Py_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) /* empty */
- #define _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) /* empty */
- #define _Py_ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) /* empty */
- #define _Py_ANNOTATE_TRACE_MEMORY(arg) /* empty */
- #define _Py_ANNOTATE_THREAD_NAME(name) /* empty */
- #define _Py_ANNOTATE_IGNORE_READS_BEGIN() /* empty */
- #define _Py_ANNOTATE_IGNORE_READS_END() /* empty */
- #define _Py_ANNOTATE_IGNORE_WRITES_BEGIN() /* empty */
- #define _Py_ANNOTATE_IGNORE_WRITES_END() /* empty */
- #define _Py_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() /* empty */
- #define _Py_ANNOTATE_IGNORE_READS_AND_WRITES_END() /* empty */
- #define _Py_ANNOTATE_IGNORE_SYNC_BEGIN() /* empty */
- #define _Py_ANNOTATE_IGNORE_SYNC_END() /* empty */
- #define _Py_ANNOTATE_ENABLE_RACE_DETECTION(enable) /* empty */
- #define _Py_ANNOTATE_NO_OP(arg) /* empty */
- #define _Py_ANNOTATE_FLUSH_STATE() /* empty */
+#define _Py_ANNOTATE_RWLOCK_CREATE(lock) /* empty */
+#define _Py_ANNOTATE_RWLOCK_DESTROY(lock) /* empty */
+#define _Py_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) /* empty */
+#define _Py_ANNOTATE_RWLOCK_RELEASED(lock, is_w) /* empty */
+#define _Py_ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) /* */
+#define _Py_ANNOTATE_BARRIER_WAIT_BEFORE(barrier) /* empty */
+#define _Py_ANNOTATE_BARRIER_WAIT_AFTER(barrier) /* empty */
+#define _Py_ANNOTATE_BARRIER_DESTROY(barrier) /* empty */
+#define _Py_ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) /* empty */
+#define _Py_ANNOTATE_CONDVAR_WAIT(cv) /* empty */
+#define _Py_ANNOTATE_CONDVAR_SIGNAL(cv) /* empty */
+#define _Py_ANNOTATE_CONDVAR_SIGNAL_ALL(cv) /* empty */
+#define _Py_ANNOTATE_HAPPENS_BEFORE(obj) /* empty */
+#define _Py_ANNOTATE_HAPPENS_AFTER(obj) /* empty */
+#define _Py_ANNOTATE_PUBLISH_MEMORY_RANGE(address, size) /* empty */
+#define _Py_ANNOTATE_UNPUBLISH_MEMORY_RANGE(address, size) /* empty */
+#define _Py_ANNOTATE_SWAP_MEMORY_RANGE(address, size) /* empty */
+#define _Py_ANNOTATE_PCQ_CREATE(pcq) /* empty */
+#define _Py_ANNOTATE_PCQ_DESTROY(pcq) /* empty */
+#define _Py_ANNOTATE_PCQ_PUT(pcq) /* empty */
+#define _Py_ANNOTATE_PCQ_GET(pcq) /* empty */
+#define _Py_ANNOTATE_NEW_MEMORY(address, size) /* empty */
+#define _Py_ANNOTATE_EXPECT_RACE(address, description) /* empty */
+#define _Py_ANNOTATE_BENIGN_RACE(address, description) /* empty */
+#define _Py_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) /* empty */
+#define _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) /* empty */
+#define _Py_ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) /* empty */
+#define _Py_ANNOTATE_TRACE_MEMORY(arg) /* empty */
+#define _Py_ANNOTATE_THREAD_NAME(name) /* empty */
+#define _Py_ANNOTATE_IGNORE_READS_BEGIN() /* empty */
+#define _Py_ANNOTATE_IGNORE_READS_END() /* empty */
+#define _Py_ANNOTATE_IGNORE_WRITES_BEGIN() /* empty */
+#define _Py_ANNOTATE_IGNORE_WRITES_END() /* empty */
+#define _Py_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() /* empty */
+#define _Py_ANNOTATE_IGNORE_READS_AND_WRITES_END() /* empty */
+#define _Py_ANNOTATE_IGNORE_SYNC_BEGIN() /* empty */
+#define _Py_ANNOTATE_IGNORE_SYNC_END() /* empty */
+#define _Py_ANNOTATE_ENABLE_RACE_DETECTION(enable) /* empty */
+#define _Py_ANNOTATE_NO_OP(arg) /* empty */
+#define _Py_ANNOTATE_FLUSH_STATE() /* empty */
#endif /* DYNAMIC_ANNOTATIONS_ENABLED */
@@ -491,8 +491,8 @@
}
#else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
- #define _Py_ANNOTATE_UNPROTECTED_READ(x) (x)
- #define _Py_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) /* empty */
+#define _Py_ANNOTATE_UNPROTECTED_READ(x) (x)
+#define _Py_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) /* empty */
#endif /* DYNAMIC_ANNOTATIONS_ENABLED */
diff -r 736b0aec412b -r d7c58422eada Include/frameobject.h
--- a/Include/frameobject.h Thu Nov 24 22:00:46 2011 +0200
+++ b/Include/frameobject.h Sun Nov 27 05:30:58 2011 +0100
@@ -45,6 +45,9 @@
PyCode_Addr2Line to calculate the line from the current
bytecode index. */
int f_lineno; /* Current line number */
+#ifdef WITH_DTRACE
+ int f_calllineno; /* line number of call site */
+#endif
int f_iblock; /* index in f_blockstack */
PyTryBlock f_blockstack[CO_MAXBLOCKS]; /* for try and loop blocks */
PyObject *f_localsplus[1]; /* locals+stack, dynamically sized */
diff -r 736b0aec412b -r d7c58422eada Include/phelper.d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Include/phelper.d Sun Nov 27 05:30:58 2011 +0100
@@ -0,0 +1,155 @@
+
+/*
+ * Python ustack helper. This relies on the first argument (PyFrame *) being
+ * on the stack; see Python/ceval.c for the contortions we go through to ensure
+ * this is the case.
+ *
+ * On x86, the PyFrame * is two slots up from the frame pointer; on SPARC, it's
+ * eight.
+ *
+ * Some details about this in "Python and DTrace in build 65":
+ * http://blogs.oracle.com/levon/entry/python_and_dtrace_in_build
+ */
+
+/*
+ * Yes, this is as gross as it looks. DTrace cannot handle static functions,
+ * and our stat_impl.h has them in ILP32.
+ */
+#define _SYS_STAT_H
+
+/*
+** When compiling in 32 bits:
+** - Early inclusion to avoid problems with
+** _FILE_OFFSET_BITS redefined.
+** - Also, we must "undef" _POSIX_PTHREAD_SEMANTICS
+** to avoid error compiling this source.
+*/
+#include "pyconfig.h"
+#undef _POSIX_PTHREAD_SEMANTICS
+
+#include
+#include
+
+#include "pyport.h"
+#include "pyatomic.h"
+#include "object.h"
+#include "pystate.h"
+#include "pyarena.h"
+#include "pythonrun.h"
+#include "compile.h"
+#include "frameobject.h"
+#include "bytesobject.h"
+
+#if defined(__i386)
+#define startframe PyEval_EvalFrameEx
+#define endframe PyEval_EvalCodeEx
+#elif defined(__amd64)
+#define PyEval_EvalFrameEx PyEval_EvalFrameExReal
+#define startframe PyEval_EvalFrameExReal
+#define endframe PyEval_EvalCodeEx
+#elif defined(__sparc)
+#define PyEval_EvalFrameEx PyEval_EvalFrameExReal
+#define startframe PyEval_EvalFrameEx
+#define endframe PyEval_EvalFrameExReal
+#endif
+
+#ifdef __sparcv9
+#define STACK_BIAS (2048-1)
+#else
+#define STACK_BIAS 0
+#endif
+
+/*
+ * Not defining PHELPER lets us test this code as a normal D script.
+ */
+#ifdef PHELPER
+
+#define at_evalframe(addr) \
+ ((uintptr_t)addr >= ((uintptr_t)&``startframe) && \
+ (uintptr_t)addr < ((uintptr_t)&``endframe))
+#define probe dtrace:helper:ustack:
+#define print_result(r) (r)
+
+#if defined(__i386) || defined(__amd64)
+#define frame_ptr_addr ((uintptr_t)arg1 + sizeof(uintptr_t) * 2)
+#elif defined(__sparc)
+#define frame_ptr_addr ((uintptr_t)arg1 + STACK_BIAS + sizeof(uintptr_t) * 8)
+#else
+#error unknown architecture
+#endif
+
+#else /* PHELPER */
+
+#define at_evalframe(addr) (1)
+#define probe pid$target::PyEval_EvalFrame:entry
+#define print_result(r) (trace(r))
+
+#if defined(__i386) || defined(__amd64)
+#define frame_ptr_addr ((uintptr_t)uregs[R_SP] + sizeof(uintptr_t))
+#elif defined(__sparc)
+/*
+ * Not implemented: we could just use R_I0, but what's the point?
+ */
+#else
+#error unknown architecture
+#endif
+
+#endif /* PHELPER */
+
+extern uintptr_t PyEval_EvalFrameEx;
+extern uintptr_t PyEval_EvalCodeEx;
+
+#define copyin_obj(addr, obj) ((obj *)copyin((uintptr_t)addr, sizeof(obj)))
+#define pystr_addr(addr) ((char *)addr + offsetof(PyBytesObject, ob_sval))
+#define copyin_str(dest, addr, obj) \
+ (copyinto((uintptr_t)pystr_addr(addr), obj->ob_size, (dest)))
+#define add_str(addr, obj) \
+ copyin_str(this->result + this->pos, addr, obj); \
+ this->pos += obj->ob_size; \
+ this->result[this->pos] = '\0';
+#define add_digit(nr, div) ((nr / div) ? \
+ (this->result[this->pos++] = '0' + ((nr / div) % 10)) : \
+ (this->result[this->pos] = '\0'))
+#define add_char(c) (this->result[this->pos++] = c)
+
+/*
+probe /at_evalframe(arg0)/
+{
+ this->framep = *(uintptr_t *)copyin(frame_ptr_addr, sizeof(uintptr_t));
+ this->frameo = copyin_obj(this->framep, PyFrameObject);
+ this->codep = this->frameo->f_code;
+ this->lineno = this->frameo->f_calllineno;
+ this->codeo = copyin_obj(this->codep, PyCodeObject);
+ this->filenamep = this->codeo->co_filename_encoded;
+ this->fnamep = this->codeo->co_name_encoded;
+ this->filenameo = copyin_obj(this->filenamep, PyBytesObject);
+ this->fnameo = copyin_obj(this->fnamep, PyBytesObject);
+
+ this->len = 1 + this->filenameo->ob_size + 1 + 5 + 2 +
+ this->fnameo->ob_size + 1 + 1;
+
+ this->result = (char *)alloca(this->len);
+ this->pos = 0;
+
+ add_char('@');
+ add_str(this->filenamep, this->filenameo);
+ add_char(':');
+ add_digit(this->lineno, 10000);
+ add_digit(this->lineno, 1000);
+ add_digit(this->lineno, 100);
+ add_digit(this->lineno, 10);
+ add_digit(this->lineno, 1);
+ add_char(' ');
+ add_char('(');
+ add_str(this->fnamep, this->fnameo);
+ add_char(')');
+ this->result[this->pos] = '\0';
+
+ print_result(stringof(this->result));
+}
+*/
+
+probe /!at_evalframe(arg0)/
+{
+ NULL;
+}
diff -r 736b0aec412b -r d7c58422eada Include/pydtrace.d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Include/pydtrace.d Sun Nov 27 05:30:58 2011 +0100
@@ -0,0 +1,17 @@
+provider python {
+ probe function__entry(const char *, const char *, int);
+ probe function__return(const char *, const char *, int);
+ probe instance__new__start(const char *, const char *);
+ probe instance__new__done(const char *, const char *);
+ probe instance__delete__start(const char *, const char *);
+ probe instance__delete__done(const char *, const char *);
+ probe line(const char *, const char *, int);
+ probe gc__start(int);
+ probe gc__done(long);
+};
+
+#pragma D attributes Evolving/Evolving/Common provider python provider
+#pragma D attributes Private/Private/Common provider python module
+#pragma D attributes Private/Private/Common provider python function
+#pragma D attributes Evolving/Evolving/Common provider python name
+#pragma D attributes Evolving/Evolving/Common provider python args
diff -r 736b0aec412b -r d7c58422eada Include/pydtrace.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Include/pydtrace.h Sun Nov 27 05:30:58 2011 +0100
@@ -0,0 +1,101 @@
+/*
+ * Generated by dtrace(1M).
+ */
+
+#ifndef _PYDTRACE_H
+#define _PYDTRACE_H
+
+#include
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if _DTRACE_VERSION
+
+#define PYTHON_FUNCTION_ENTRY(arg0, arg1, arg2) \
+ __dtrace_python___function__entry(arg0, arg1, arg2)
+#define PYTHON_FUNCTION_ENTRY_ENABLED() \
+ __dtraceenabled_python___function__entry()
+#define PYTHON_FUNCTION_RETURN(arg0, arg1, arg2) \
+ __dtrace_python___function__return(arg0, arg1, arg2)
+#define PYTHON_FUNCTION_RETURN_ENABLED() \
+ __dtraceenabled_python___function__return()
+#define PYTHON_GC_DONE(arg0) \
+ __dtrace_python___gc__done(arg0)
+#define PYTHON_GC_DONE_ENABLED() \
+ __dtraceenabled_python___gc__done()
+#define PYTHON_GC_START(arg0) \
+ __dtrace_python___gc__start(arg0)
+#define PYTHON_GC_START_ENABLED() \
+ __dtraceenabled_python___gc__start()
+#define PYTHON_INSTANCE_DELETE_DONE(arg0, arg1) \
+ __dtrace_python___instance__delete__done(arg0, arg1)
+#define PYTHON_INSTANCE_DELETE_DONE_ENABLED() \
+ __dtraceenabled_python___instance__delete__done()
+#define PYTHON_INSTANCE_DELETE_START(arg0, arg1) \
+ __dtrace_python___instance__delete__start(arg0, arg1)
+#define PYTHON_INSTANCE_DELETE_START_ENABLED() \
+ __dtraceenabled_python___instance__delete__start()
+#define PYTHON_INSTANCE_NEW_DONE(arg0, arg1) \
+ __dtrace_python___instance__new__done(arg0, arg1)
+#define PYTHON_INSTANCE_NEW_DONE_ENABLED() \
+ __dtraceenabled_python___instance__new__done()
+#define PYTHON_INSTANCE_NEW_START(arg0, arg1) \
+ __dtrace_python___instance__new__start(arg0, arg1)
+#define PYTHON_INSTANCE_NEW_START_ENABLED() \
+ __dtraceenabled_python___instance__new__start()
+#define PYTHON_LINE(arg0, arg1, arg2) \
+ __dtrace_python___line(arg0, arg1, arg2)
+#define PYTHON_LINE_ENABLED() \
+ __dtraceenabled_python___line()
+
+
+extern void __dtrace_python___function__entry(char *, char *, int);
+extern int __dtraceenabled_python___function__entry(void);
+extern void __dtrace_python___function__return(char *, char *, int);
+extern int __dtraceenabled_python___function__return(void);
+extern void __dtrace_python___gc__done(long);
+extern int __dtraceenabled_python___gc__done(void);
+extern void __dtrace_python___gc__start(int);
+extern int __dtraceenabled_python___gc__start(void);
+extern void __dtrace_python___instance__delete__done(char *, char *);
+extern int __dtraceenabled_python___instance__delete__done(void);
+extern void __dtrace_python___instance__delete__start(char *, char *);
+extern int __dtraceenabled_python___instance__delete__start(void);
+extern void __dtrace_python___instance__new__done(char *, char *);
+extern int __dtraceenabled_python___instance__new__done(void);
+extern void __dtrace_python___instance__new__start(char *, char *);
+extern int __dtraceenabled_python___instance__new__start(void);
+extern void __dtrace_python___line(char *, char *, int);
+extern int __dtraceenabled_python___line(void);
+
+#else
+
+#define PYTHON_FUNCTION_ENTRY(arg0, arg1, arg2)
+#define PYTHON_FUNCTION_ENTRY_ENABLED() (0)
+#define PYTHON_FUNCTION_RETURN(arg0, arg1, arg2)
+#define PYTHON_FUNCTION_RETURN_ENABLED() (0)
+#define PYTHON_GC_DONE(arg0)
+#define PYTHON_GC_DONE_ENABLED() (0)
+#define PYTHON_GC_START(arg0)
+#define PYTHON_GC_START_ENABLED() (0)
+#define PYTHON_INSTANCE_DELETE_DONE(arg0, arg1)
+#define PYTHON_INSTANCE_DELETE_DONE_ENABLED() (0)
+#define PYTHON_INSTANCE_DELETE_START(arg0, arg1)
+#define PYTHON_INSTANCE_DELETE_START_ENABLED() (0)
+#define PYTHON_INSTANCE_NEW_DONE(arg0, arg1)
+#define PYTHON_INSTANCE_NEW_DONE_ENABLED() (0)
+#define PYTHON_INSTANCE_NEW_START(arg0, arg1)
+#define PYTHON_INSTANCE_NEW_START_ENABLED() (0)
+#define PYTHON_LINE(arg0, arg1, arg2)
+#define PYTHON_LINE_ENABLED() (0)
+
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _PYDTRACE_H */
diff -r 736b0aec412b -r d7c58422eada Lib/test/dtrace_sample.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Lib/test/dtrace_sample.py Sun Nov 27 05:30:58 2011 +0100
@@ -0,0 +1,73 @@
+# Sample script for use by test_dtrace.py
+# DO NOT MODIFY THIS FILE IN ANY WAY WITHOUT UPDATING test_dtrace.py!!!!!
+
+import gc
+
+def function_1() :
+ pass
+
+# Check stacktrace
+def function_2() :
+ function_1()
+
+# CALL_FUNCTION_VAR
+def function_3(dummy, dummy2) :
+ pass
+
+# CALL_FUNCTION_KW
+def function_4(**dummy) :
+ pass
+
+# CALL_FUNCTION_VAR_KW
+def function_5(dummy, dummy2, **dummy3) :
+ pass
+
+def test_entry_return_and_stack() :
+ function_1()
+ function_2()
+ function_3(*(1,2))
+ function_4(**{"test":42})
+ function_5(*(1,2), **{"test":42})
+
+def test_line() :
+ a = 1 # Preamble
+ for i in range(2) :
+ a = i
+ b = i+2
+ c = i+3
+ d = a + b +c
+ a = 1 # Epilogue
+
+def test_instance_creation_destruction() :
+ class old_style_class() :
+ pass
+ class new_style_class(object) :
+ pass
+
+ a = old_style_class()
+ del a
+ gc.collect()
+ b = new_style_class()
+ del b
+ gc.collect()
+
+ a = old_style_class()
+ del old_style_class
+ gc.collect()
+ b = new_style_class()
+ del new_style_class
+ gc.collect()
+ del a
+ gc.collect()
+ del b
+ gc.collect()
+
+def test_garbage_collection() :
+ gc.collect()
+
+if __name__ == "__main__":
+ test_entry_return_and_stack()
+ test_line()
+ test_instance_creation_destruction()
+ test_garbage_collection()
+
diff -r 736b0aec412b -r d7c58422eada Lib/test/test_dtrace.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Lib/test/test_dtrace.py Sun Nov 27 05:30:58 2011 +0100
@@ -0,0 +1,320 @@
+import sys, unittest, subprocess, os.path, dis, types
+import dtrace
+from test.support import TESTFN, run_unittest, findfile
+
+sample = os.path.abspath(findfile("dtrace_sample.py"))
+if not dtrace.available :
+ raise unittest.SkipTest("dtrace support not compiled in")
+
+dscript = """
+pid$target::PyEval_EvalCode:entry
+"""
+dscript = dscript.replace("\r", "").replace("\n", "")
+result, _ = subprocess.Popen(["dtrace", "-q", "-l", "-n", dscript,
+ "-c", "%s %s" %(sys.executable, sample)], stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT).communicate()
+if result.decode("ascii").split("\n")[1].split()[-2:] != \
+ ["PyEval_EvalCode", "entry"] :
+ result2 = repr(result)
+ raise unittest.SkipTest("dtrace seems not to be working. " + \
+ "Please, check your privileges. " +
+ "Result: " +result2)
+
+class DTraceTestsNormal(unittest.TestCase) :
+ def setUp(self) :
+ self.optimize = False
+
+ def test_function_entry_return(self) :
+ dscript = """
+python$target:::function-entry
+/(copyinstr(arg0)=="%(path)s") &&
+(copyinstr(arg1)=="test_entry_return_and_stack")/
+{
+ self->trace = 1;
+}
+python$target:::function-entry,python$target:::function-return
+/(copyinstr(arg0)=="%(path)s") && (self->trace)/
+{
+ printf("**%%s*%%s*%%s*%%d\\n", probename, copyinstr(arg0),
+ copyinstr(arg1), arg2);
+}
+python$target:::function-return
+/(copyinstr(arg0)=="%(path)s") &&
+(copyinstr(arg1)=="test_entry_return_and_stack")/
+{
+ self->trace = 0;
+}
+""" %{"path":sample}
+
+ dscript = dscript.replace("\r", "").replace("\n", "")
+ expected_result = """
+ **function-entry*%(path)s*test_entry_return_and_stack*25
+ **function-entry*%(path)s*function_1*6
+ **function-return*%(path)s*function_1*7
+ **function-entry*%(path)s*function_2*10
+ **function-entry*%(path)s*function_1*6
+ **function-return*%(path)s*function_1*7
+ **function-return*%(path)s*function_2*11
+ **function-entry*%(path)s*function_3*14
+ **function-return*%(path)s*function_3*15
+ **function-entry*%(path)s*function_4*18
+ **function-return*%(path)s*function_4*19
+ **function-entry*%(path)s*function_5*22
+ **function-return*%(path)s*function_5*23
+ **function-return*%(path)s*test_entry_return_and_stack*30
+ """ %{"path":sample}
+
+ command = "%s %s" %(sys.executable, sample)
+ if self.optimize :
+ command = "%s -OO %s" %(sys.executable, sample)
+ actual_result, _ = subprocess.Popen(["dtrace", "-q", "-n",
+ dscript,
+ "-c", command],
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()
+
+ actual_result = actual_result.decode("ascii").replace("\r",
+ "").replace("\n", "").replace(" ", "")
+ expected_result = expected_result.replace("\r", "").replace("\n",
+ "").replace(" ", "")
+ self.assertEqual(actual_result, expected_result)
+
+ def test_stack(self) :
+ dscript = """
+python$target:::function-entry
+/(copyinstr(arg0)=="%(path)s") &&
+(copyinstr(arg1)=="test_entry_return_and_stack")/
+{
+ self->trace = 1;
+}
+python$target:::function-entry
+/(copyinstr(arg0)=="%(path)s") && (self->trace)/
+{
+ printf("[x]");
+ jstack();
+}
+python$target:::function-return
+/(copyinstr(arg0)=="%(path)s") &&
+(copyinstr(arg1)=="test_entry_return_and_stack")/
+{
+ self->trace = 0;
+}
+""" %{"path":sample}
+
+ dscript = dscript.replace("\r", "").replace("\n", "")
+ expected_result = """
+ [x]
+ [%(path)s:25(test_entry_return_and_stack)]
+ [x]
+ [%(path)s:6(function_1)]
+ [%(path)s:26(test_entry_return_and_stack)]
+ [x]
+ [%(path)s:10(function_2)]
+ [%(path)s:27(test_entry_return_and_stack)]
+ [x]
+ [%(path)s:6(function_1)]
+ [%(path)s:11(function_2)]
+ [%(path)s:27(test_entry_return_and_stack)]
+ [x]
+ [%(path)s:14(function_3)]
+ [%(path)s:28(test_entry_return_and_stack)]
+ [x]
+ [%(path)s:18(function_4)]
+ [%(path)s:29(test_entry_return_and_stack)]
+ [x]
+ [%(path)s:22(function_5)]
+ [%(path)s:30(test_entry_return_and_stack)]
+ """ %{"path":sample}
+
+ command = "%s %s" %(sys.executable, sample)
+ if self.optimize :
+ command = "%s -OO %s" %(sys.executable, sample)
+ actual_result, _ = subprocess.Popen(["dtrace", "-q", "-n",
+ dscript,
+ "-c", command],
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()
+
+ actual_result = [i for i in actual_result.decode("ascii").split("\n") \
+ if (("[" in i) and not i.endswith(" () ]"))]
+ actual_result = "".join(actual_result)
+ actual_result = actual_result.replace("\r", "").replace("\n",
+ "").replace(" ", "")
+ expected_result = expected_result.replace("\r", "").replace("\n",
+ "").replace(" ", "")
+ self.assertEqual(actual_result, expected_result)
+
+ def test_garbage_collection(self) :
+ dscript = """
+python$target:::gc-start,python$target:::gc-done
+{
+ printf("**%s(%ld)\\n", probename, arg0);
+}
+"""
+
+ dscript = dscript.replace("\r", "").replace("\n", "")
+ command = "%s %s" %(sys.executable, sample)
+ if self.optimize :
+ command = "%s -OO %s" %(sys.executable, sample)
+ actual_result, _ = subprocess.Popen(["dtrace", "-q", "-n",
+ dscript,
+ "-c", command],
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()
+
+ actual_result = "".join(actual_result.decode("ascii"))
+ actual_result = actual_result.replace("\r", "").replace("\n",
+ "").replace(" ", "")
+ for i in range(10) :
+ actual_result = actual_result.replace(str(i), "")
+ expected_result = "**gc-start()**gc-done()" * \
+ actual_result.count("**gc-start()**")
+
+ self.assertEqual(actual_result, expected_result)
+
+ def test_verify_opcodes(self) :
+ # Verify that we are checking:
+ opcodes = set(["CALL_FUNCTION", "CALL_FUNCTION_VAR",
+ "CALL_FUNCTION_KW", "CALL_FUNCTION_VAR_KW"])
+ obj = compile(open(sample).read(), "sample", "exec")
+ class dump() :
+ def __init__(self) :
+ self.buf = []
+ def write(self, v) :
+ self.buf.append(v)
+
+ dump = dump()
+ stdout = sys.stdout
+ sys.stdout = dump
+ for i in obj.co_consts :
+ if isinstance(i, types.CodeType) and \
+ (i.co_name == 'test_entry_return_and_stack') :
+ dis.dis(i)
+ sys.stdout = stdout
+ dump = "\n".join(dump.buf)
+ dump = dump.replace("\r", "").replace("\n", "").split()
+ for i in dump :
+ opcodes.discard(i)
+ # Are we verifying all the relevant opcodes?
+ self.assertEqual(set(), opcodes) # Are we verifying all opcodes?
+
+ def test_line(self) :
+ dscript = """
+python$target:::line
+/(copyinstr(arg0)=="%(path)s") &&
+(copyinstr(arg1)=="test_line")/
+{
+ printf("**%%s*%%s*%%s*%%d\\n", probename, copyinstr(arg0),
+ copyinstr(arg1), arg2);
+}
+""" %{"path":sample}
+
+ dscript = dscript.replace("\r", "").replace("\n", "")
+ expected_result = """
+ **line*%(path)s*test_line*33
+ **line*%(path)s*test_line*34
+ **line*%(path)s*test_line*35
+ **line*%(path)s*test_line*36
+ **line*%(path)s*test_line*37
+ **line*%(path)s*test_line*38
+ **line*%(path)s*test_line*34
+ **line*%(path)s*test_line*35
+ **line*%(path)s*test_line*36
+ **line*%(path)s*test_line*37
+ **line*%(path)s*test_line*38
+ **line*%(path)s*test_line*34
+ **line*%(path)s*test_line*39
+ """ %{"path":sample}
+
+ command = "%s %s" %(sys.executable, sample)
+ if self.optimize :
+ command = "%s -OO %s" %(sys.executable, sample)
+ actual_result, _ = subprocess.Popen(["dtrace", "-q", "-n",
+ dscript,
+ "-c", command],
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()
+
+ actual_result = actual_result.decode("ascii").replace("\r",
+ "").replace("\n", "").replace(" ", "")
+ expected_result = expected_result.replace("\r", "").replace("\n",
+ "").replace(" ", "")
+ self.assertEqual(actual_result, expected_result)
+
+ def test_instance_creation_destruction(self) :
+ dscript = """
+python$target:::function-entry
+/(copyinstr(arg0)=="%(path)s") &&
+(copyinstr(arg1)=="test_instance_creation_destruction")/
+{
+ self->trace = 1;
+}
+
+python$target:::instance-new-start,
+python$target:::instance-new-done,
+python$target:::instance-delete-start,
+python$target:::instance-delete-done
+/self->trace/
+{
+ printf("**%%s* (%%s.%%s)\\n", probename, copyinstr(arg1), copyinstr(arg0));
+}
+
+python$target:::function-return
+/(copyinstr(arg0)=="%(path)s") &&
+(copyinstr(arg1)=="test_instance_creation_destruction")/
+{
+ self->trace = 0;
+}
+""" %{"path":sample}
+
+ dscript = dscript.replace("\r", "").replace("\n", "")
+ expected_result = """
+ **instance-new-start*(__main__.old_style_class)
+ **instance-new-done*(__main__.old_style_class)
+ **instance-delete-start*(__main__.old_style_class)
+ **instance-delete-done*(__main__.old_style_class)
+ **instance-new-start*(__main__.new_style_class)
+ **instance-new-done*(__main__.new_style_class)
+ **instance-delete-start*(__main__.new_style_class)
+ **instance-delete-done*(__main__.new_style_class)
+ **instance-new-start*(__main__.old_style_class)
+ **instance-new-done*(__main__.old_style_class)
+ **instance-new-start*(__main__.new_style_class)
+ **instance-new-done*(__main__.new_style_class)
+ **instance-delete-start*(__main__.old_style_class)
+ **instance-delete-done*(__main__.old_style_class)
+ **instance-delete-start*(__main__.new_style_class)
+ **instance-delete-done*(__main__.new_style_class)
+ """
+
+ command = "%s %s" %(sys.executable, sample)
+ if self.optimize :
+ command = "%s -OO %s" %(sys.executable, sample)
+ actual_result, _ = subprocess.Popen(["dtrace", "-q", "-n",
+ dscript,
+ "-c", command],
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()
+
+ actual_result = actual_result.decode("ascii").replace("\r",
+ "").replace("\n", "").replace(" ", "")
+ expected_result = expected_result.replace("\r", "").replace("\n",
+ "").replace(" ", "")
+ self.assertEqual(actual_result, expected_result)
+
+
+
+# This class try to verify that dtrace probes
+# are still working with optimizations enabled in the bytecode.
+#
+# Some tests will not actually verify it. For instance,
+# source code compilation follows optimization status of
+# current working Python. So, you should run the test
+# both with an optimizing and a non optimizing Python.
+class DTraceTestsOptimize(DTraceTestsNormal) :
+ def setUp(self) :
+ self.optimize = True
+
+
+def test_main():
+ run_unittest(DTraceTestsNormal)
+ run_unittest(DTraceTestsOptimize)
+
+if __name__ == '__main__':
+ test_main()
+
diff -r 736b0aec412b -r d7c58422eada Makefile.pre.in
--- a/Makefile.pre.in Thu Nov 24 22:00:46 2011 +0200
+++ b/Makefile.pre.in Sun Nov 27 05:30:58 2011 +0100
@@ -48,6 +48,10 @@
# Use this to make a link between python$(VERSION) and python in $(BINDIR)
LN= @LN@
+DTRACE= @DTRACE@
+DFLAGS= @DFLAGS@
+
+
# Portable install script (configure doesn't always guess right)
INSTALL= @INSTALL@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
@@ -333,6 +337,7 @@
Python/formatter_unicode.o \
Python/fileutils.o \
Python/$(DYNLOADFILE) \
+ @DTRACEOBJS@ \
$(LIBOBJS) \
$(MACHDEP_OBJS) \
$(THREADOBJ)
@@ -673,6 +678,42 @@
$(srcdir)/Objects/typeslots.inc: $(srcdir)/Include/typeslots.h $(srcdir)/Objects/typeslots.py
$(PYTHON) $(srcdir)/Objects/typeslots.py < $(srcdir)/Include/typeslots.h > $(srcdir)/Objects/typeslots.inc
+# Only generated on Solaris
+Python/phelper.o: $(srcdir)/Include/phelper.d
+ if test "$(DTRACE)" != "" ; then \
+ $(DTRACE) -o $@ -DPHELPER $(DFLAGS) \
+ $(PY_CPPFLAGS) -C -G -s $(srcdir)/Include/phelper.d ; \
+ else touch $@ ; \
+ fi;
+
+Include/pydtrace.h: $(srcdir)/Include/pydtrace.d
+ if test "$(DTRACE)" != "" ; then \
+ $(DTRACE) -o $@ $(DFLAGS) \
+ -C -h -s $(srcdir)/Include/pydtrace.d ; \
+ else touch $@ ; \
+ fi;
+
+Include/phelper.h: $(srcdir)/Include/phelper.d
+ if test "$(DTRACE)" != "" ; then \
+ $(DTRACE) -o $@ $(DFLAGS) \
+ -C -h -s $(srcdir)/Python/python.d ; \
+ else touch $@ ; \
+ fi;
+
+Python/ceval.o: Include/pydtrace.h
+Modules/gcmodule.o: Include/pydtrace.h
+Objects/typeobject.o: Include/pydtrace.h
+
+Python/dtrace.o: $(srcdir)/Include/pydtrace.d Python/ceval.o Modules/gcmodule.o \
+ Objects/typeobject.o
+ if test "$(DTRACE)" != "" ; then \
+ $(DTRACE) -o $@ $(DFLAGS) \
+ -C -G -s $(srcdir)/Include/pydtrace.d \
+ Python/ceval.o Modules/gcmodule.o \
+ Objects/typeobject.o; \
+ else touch $@ ; \
+ fi;
+
############################################################################
# Header files
@@ -1314,6 +1355,7 @@
find . -name '*.so.[0-9]*.[0-9]*' -exec rm -f {} ';'
find build -name 'fficonfig.h' -exec rm -f {} ';' || true
find build -name 'fficonfig.py' -exec rm -f {} ';' || true
+ rm -f Include/pydtrace.h Include/phelper.h
-rm -f Lib/lib2to3/*Grammar*.pickle
profile-removal:
@@ -1343,6 +1385,7 @@
-o -name '*.orig' -o -name '*.rej' \
-o -name '*.bak' ')' \
-exec rm -f {} ';'
+ rm -f Include/pydtrace.h Include/phelper.h
# Check for smelly exported symbols (not starting with Py/_Py)
smelly: all
diff -r 736b0aec412b -r d7c58422eada Modules/dtracemodule.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Modules/dtracemodule.c Sun Nov 27 05:30:58 2011 +0100
@@ -0,0 +1,39 @@
+#include "Python.h"
+
+static PyMethodDef dtrace_methods[] = {
+ {NULL, NULL} /* sentinel */
+};
+
+
+static struct PyModuleDef dtracemodule = {
+ PyModuleDef_HEAD_INIT,
+ "dtrace",
+ NULL,
+ -1,
+ dtrace_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+};
+
+PyMODINIT_FUNC
+PyInit_dtrace(void)
+{
+ PyObject *m, *v;
+
+ m = PyModule_Create(&dtracemodule);
+ if (m) {
+#ifdef WITH_DTRACE
+ v = Py_True;
+#else
+ v = Py_False;
+#endif
+ Py_INCREF(v);
+ if (PyModule_AddObject(m, "available", v) < 0) {
+ Py_DECREF(m);
+ return NULL;
+ }
+ }
+ return m;
+}
diff -r 736b0aec412b -r d7c58422eada Modules/gcmodule.c
--- a/Modules/gcmodule.c Thu Nov 24 22:00:46 2011 +0200
+++ b/Modules/gcmodule.c Sun Nov 27 05:30:58 2011 +0100
@@ -26,6 +26,10 @@
#include "Python.h"
#include "frameobject.h" /* for PyFrame_ClearFreeList */
+#ifdef WITH_DTRACE
+#include "pydtrace.h"
+#endif
+
/* Get an object's GC head */
#define AS_GC(o) ((PyGC_Head *)(o)-1)
@@ -789,7 +793,12 @@
/* This is the main function. Read this to understand how the
* collection process works. */
static Py_ssize_t
-collect(int generation)
+#ifdef WITH_DTRACE
+collect2
+#else
+collect
+#endif
+(int generation)
{
int i;
Py_ssize_t m = 0; /* # objects collected */
@@ -946,6 +955,49 @@
return n+m;
}
+#ifdef WITH_DTRACE
+static void
+dtrace_gc_start(int collection)
+{
+ PYTHON_GC_START(collection);
+
+ /*
+ * Currently a USDT tail-call will not receive the correct arguments.
+ * Disable the tail call here.
+ */
+#if defined(__sparc)
+ asm("nop");
+#endif
+}
+
+static void
+dtrace_gc_done(Py_ssize_t value)
+{
+ PYTHON_GC_DONE((long) value);
+
+ /*
+ * Currently a USDT tail-call will not receive the correct arguments.
+ * Disable the tail call here.
+ */
+#if defined(__sparc)
+ asm("nop");
+#endif
+}
+
+static Py_ssize_t
+collect(int collection)
+{
+Py_ssize_t value;
+
+ if (PYTHON_GC_START_ENABLED())
+ dtrace_gc_start(collection);
+ value = collect2(collection);
+ if (PYTHON_GC_DONE_ENABLED())
+ dtrace_gc_done(value);
+ return value;
+}
+#endif /* WITH_DTRACE */
+
static Py_ssize_t
collect_generations(void)
{
diff -r 736b0aec412b -r d7c58422eada Objects/codeobject.c
--- a/Objects/codeobject.c Thu Nov 24 22:00:46 2011 +0200
+++ b/Objects/codeobject.c Sun Nov 27 05:30:58 2011 +0100
@@ -142,6 +142,10 @@
co->co_filename = filename;
Py_INCREF(name);
co->co_name = name;
+#ifdef WITH_DTRACE
+ co->co_filename_encoded = PyUnicode_AsUTF8String(filename);
+ co->co_name_encoded = PyUnicode_AsUTF8String(name);
+#endif
co->co_firstlineno = firstlineno;
Py_INCREF(lnotab);
co->co_lnotab = lnotab;
@@ -364,6 +368,10 @@
Py_XDECREF(co->co_cellvars);
Py_XDECREF(co->co_filename);
Py_XDECREF(co->co_name);
+#ifdef WITH_DTRACE
+ Py_XDECREF(co->co_filename_encoded);
+ Py_XDECREF(co->co_name_encoded);
+#endif
Py_XDECREF(co->co_lnotab);
if (co->co_cell2arg != NULL)
PyMem_FREE(co->co_cell2arg);
diff -r 736b0aec412b -r d7c58422eada Objects/frameobject.c
--- a/Objects/frameobject.c Thu Nov 24 22:00:46 2011 +0200
+++ b/Objects/frameobject.c Sun Nov 27 05:30:58 2011 +0100
@@ -709,6 +709,9 @@
f->f_tstate = tstate;
f->f_lasti = -1;
+#ifdef WITH_DTRACE
+ f->f_calllineno = code->co_firstlineno;
+#endif
f->f_lineno = code->co_firstlineno;
f->f_iblock = 0;
diff -r 736b0aec412b -r d7c58422eada Objects/typeobject.c
--- a/Objects/typeobject.c Thu Nov 24 22:00:46 2011 +0200
+++ b/Objects/typeobject.c Sun Nov 27 05:30:58 2011 +0100
@@ -4,6 +4,10 @@
#include "frameobject.h"
#include "structmember.h"
+#ifdef WITH_DTRACE
+#include "pydtrace.h"
+#endif
+
#include
@@ -705,8 +709,27 @@
PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
{
PyObject *obj;
- const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
+ size_t size;
+
+#ifdef WITH_DTRACE
+ PyObject *mod;
+ char *mod_name;
+
+ if (PYTHON_INSTANCE_NEW_START_ENABLED()) {
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
+ mod = PyDict_GetItemString(type->tp_dict, "__module__");
+ if (mod == NULL || !PyUnicode_Check(mod)) {
+ mod_name = "?";
+ } else {
+ mod_name = _PyUnicode_AsString(mod);
+ }
+ PYTHON_INSTANCE_NEW_START((char *)(type->tp_name), mod_name);
+ }
+ }
+#endif
+
/* note that we need to add one, for the sentinel */
+ size = _PyObject_VAR_SIZE(type, nitems+1);
if (PyType_IS_GC(type))
obj = _PyObject_GC_Malloc(size);
@@ -728,6 +751,21 @@
if (PyType_IS_GC(type))
_PyObject_GC_TRACK(obj);
+
+#ifdef WITH_DTRACE
+ if (PYTHON_INSTANCE_NEW_DONE_ENABLED()) {
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
+ mod = PyDict_GetItemString(type->tp_dict, "__module__");
+ if (mod == NULL || !PyUnicode_Check(mod)) {
+ mod_name = "?";
+ } else {
+ mod_name = _PyUnicode_AsString(mod);
+ }
+ PYTHON_INSTANCE_NEW_DONE((char *)(type->tp_name), mod_name);
+ }
+ }
+#endif
+
return obj;
}
@@ -843,9 +881,52 @@
return 0;
}
+#ifdef WITH_DTRACE
+static void subtype_dealloc2(PyObject *); /* Forward declaration */
+
static void
subtype_dealloc(PyObject *self)
{
+ PyObject *mod;
+ char *mod_name;
+ PyTypeObject *type;
+
+ type = Py_TYPE(self);
+ Py_INCREF(type);
+
+ if (PYTHON_INSTANCE_DELETE_START_ENABLED()) {
+ mod = PyDict_GetItemString(type->tp_dict, "__module__");
+ if (mod == NULL || !PyUnicode_Check(mod)) {
+ mod_name = "?";
+ } else {
+ mod_name = _PyUnicode_AsString(mod);
+ }
+ PYTHON_INSTANCE_DELETE_START((char *)(type->tp_name), mod_name);
+ }
+
+ subtype_dealloc2(self);
+
+ if (PYTHON_INSTANCE_DELETE_DONE_ENABLED()) {
+ mod = PyDict_GetItemString(type->tp_dict, "__module__");
+ if (mod == NULL || !PyUnicode_Check(mod)) {
+ mod_name = "?";
+ } else {
+ mod_name = _PyUnicode_AsString(mod);
+ }
+ PYTHON_INSTANCE_DELETE_DONE((char *)(type->tp_name), mod_name);
+ }
+ Py_DECREF(type);
+}
+#endif
+
+static void
+#ifdef WITH_DTRACE
+subtype_dealloc2
+#else
+subtype_dealloc
+#endif
+(PyObject *self)
+{
PyTypeObject *type, *base;
destructor basedealloc;
diff -r 736b0aec412b -r d7c58422eada Python/ceval.c
--- a/Python/ceval.c Thu Nov 24 22:00:46 2011 +0200
+++ b/Python/ceval.c Sun Nov 27 05:30:58 2011 +0100
@@ -18,6 +18,10 @@
#include
+#ifdef WITH_DTRACE
+#include "pydtrace.h"
+#endif
+
#ifndef WITH_TSC
#define READ_TIMESTAMP(var)
@@ -119,6 +123,12 @@
#define CALL_FLAG_VAR 1
#define CALL_FLAG_KW 2
+#ifdef WITH_DTRACE
+static void maybe_dtrace_line(PyFrameObject *frame,
+ int *instr_lb, int *instr_ub,
+ int *instr_prev);
+#endif
+
#ifdef LLTRACE
static int lltrace;
static int prtrace(PyObject *, char *);
@@ -567,6 +577,7 @@
j = pendingfirst;
if (j == pendinglast) {
func = NULL; /* Queue empty */
+ arg = NULL;
} else {
func = pendingcalls[j].func;
arg = pendingcalls[j].arg;
@@ -776,6 +787,71 @@
NULL, NULL);
}
+#ifdef WITH_DTRACE
+static void
+dtrace_entry(PyFrameObject *f)
+{
+ const char *filename;
+ const char *fname;
+ int lineno;
+
+ if (f->f_code->co_filename_encoded) {
+ filename = PyBytes_AS_STRING(f->f_code->co_filename_encoded);
+ } else {
+ filename = "???";
+ }
+ if (f->f_code->co_name_encoded) {
+ fname = PyBytes_AS_STRING(f->f_code->co_name_encoded);
+ } else {
+ fname = "???";
+ }
+ lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
+
+ PYTHON_FUNCTION_ENTRY((char *)filename, (char *)fname, lineno);
+
+ /*
+ * Currently a USDT tail-call will not receive the correct arguments.
+ * Disable the tail call here.
+ */
+#if defined(__sparc)
+ asm("nop");
+#endif
+}
+
+static void
+dtrace_return(PyFrameObject *f)
+{
+ const char *filename;
+ const char *fname;
+ int lineno;
+
+ if (f->f_code->co_filename_encoded) {
+ filename = PyBytes_AS_STRING(f->f_code->co_filename_encoded);
+ } else {
+ filename = "???";
+ }
+ if (f->f_code->co_name_encoded) {
+ fname = PyBytes_AS_STRING(f->f_code->co_name_encoded);
+ } else {
+ fname = "???";
+ }
+ lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
+ PYTHON_FUNCTION_RETURN((char *)filename, (char *)fname, lineno);
+
+ /*
+ * Currently a USDT tail-call will not receive the correct arguments.
+ * Disable the tail call here.
+ */
+#if defined(__sparc)
+ asm("nop");
+#endif
+}
+#else
+#define PYTHON_FUNCTION_ENTRY_ENABLED() 0
+#define PYTHON_FUNCTION_RETURN_ENABLED() 0
+#define dtrace_entry(f)
+#define dtrace_return(f)
+#endif
/* Interpreter main loop */
@@ -787,9 +863,84 @@
return PyEval_EvalFrameEx(f, 0);
}
+/*
+ * These shenanigans look like utter madness, but what we're actually doing is
+ * making sure that the ustack helper will see the PyFrameObject pointer on the
+ * stack. We have two tricky cases:
+ *
+ * amd64
+ *
+ * We use up the six registers for passing arguments, meaning the call can't
+ * use a register for passing 'f', and has to push it onto the stack in a known
+ * location.
+ *
+ * And how does "throwflag" figure in to this? -PN
+ *
+ * SPARC
+ *
+ * Here the problem is that (on 32-bit) the compiler is re-using %i0 before
+ * some calls inside PyEval_EvalFrameReal(), which means that when it's saved,
+ * it's just some junk value rather than the real first argument. So, instead,
+ * we trace our proxy PyEval_EvalFrame(), where we 'know' the compiler won't
+ * decide to re-use %i0. We also need to defeat optimization of our proxy.
+ */
+
+#if defined(WITH_DTRACE)
+
+#if defined(__amd64)
+PyObject *PyEval_EvalFrameExReal(long, long, long, long, long, long,
+ PyFrameObject *, int throwflag);
+
+
+
PyObject *
PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
{
+ volatile PyObject *f2;
+ f2 = PyEval_EvalFrameExReal(0, 0, 0, 0, 0, 0, f, throwflag);
+ return (PyObject *)f2;
+}
+
+PyObject *
+PyEval_EvalFrameExReal(long a1, long a2, long a3, long a4, long a5, long a6,
+ PyFrameObject *f, int throwflag)
+{
+
+#elif defined(__sparc)
+
+PyObject *PyEval_EvalFrameExReal(PyFrameObject *f, int throwflag);
+
+volatile int dummy;
+
+PyObject *
+PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
+{
+ volatile PyObject *f2;
+ f2 = PyEval_EvalFrameExReal(f, throwflag);
+ dummy = f->ob_refcnt;
+ return (PyObject *)f2;
+}
+
+PyObject *
+PyEval_EvalFrameExReal(PyFrameObject *f, int throwflag)
+{
+
+#else /* __amd64 || __sparc */
+
+PyObject *
+PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
+{
+
+#endif /* __amd64 || __sparc */
+
+#else /* WITH_DTRACE not defined */
+
+PyObject *
+PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
+{
+
+#endif /* WITH_DTRACE */
+
#ifdef DXPAIRS
int lastopcode = 0;
#endif
@@ -1156,6 +1307,9 @@
}
}
+ if (PYTHON_FUNCTION_ENTRY_ENABLED())
+ dtrace_entry(f);
+
co = f->f_code;
names = co->co_names;
consts = co->co_consts;
@@ -1341,6 +1495,10 @@
/* Main switch on opcode */
READ_TIMESTAMP(inst0);
+ if (PYTHON_LINE_ENABLED()) {
+ maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
+ }
+
switch (opcode) {
/* BEWARE!
@@ -2620,6 +2778,9 @@
PyObject **sp;
PCALL(PCALL_ALL);
sp = stack_pointer;
+#ifdef WITH_DTRACE
+ f->f_calllineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
+#endif
#ifdef WITH_TSC
x = call_function(&sp, oparg, &intr0, &intr1);
#else
@@ -2663,6 +2824,10 @@
} else
Py_INCREF(func);
sp = stack_pointer;
+#ifdef WITH_DTRACE
+ f->f_calllineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
+#endif
+
READ_TIMESTAMP(intr0);
x = ext_do_call(func, &sp, flags, na, nk);
READ_TIMESTAMP(intr1);
@@ -3022,6 +3187,8 @@
/* pop frame */
exit_eval_frame:
+ if (PYTHON_FUNCTION_RETURN_ENABLED())
+ dtrace_return(f);
Py_LeaveRecursiveCall();
tstate->frame = f->f_back;
@@ -3723,6 +3890,45 @@
return result;
}
+#ifdef WITH_DTRACE
+/* See Objects/lnotab_notes.txt for a description of how tracing works. */
+/* Practically a ripoff of "maybe_call_line_trace" function. */
+static void
+maybe_dtrace_line(PyFrameObject *frame, int *instr_lb, int *instr_ub,
+ int *instr_prev)
+{
+ int line = frame->f_lineno;
+
+ /* If the last instruction executed isn't in the current
+ instruction window, reset the window.
+ */
+ if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
+ PyAddrPair bounds;
+ line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
+ &bounds);
+ *instr_lb = bounds.ap_lower;
+ *instr_ub = bounds.ap_upper;
+ }
+ /* If the last instruction falls at the start of a line or if
+ it represents a jump backwards, update the frame's line
+ number and call the trace function. */
+ if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
+ frame->f_lineno = line;
+ PYTHON_LINE((char *)_PyUnicode_AsString(frame->f_code->co_filename),
+ (char *)_PyUnicode_AsString(frame->f_code->co_name), line);
+ }
+ *instr_prev = frame->f_lasti;
+
+ /*
+ * Currently a USDT tail-call will not receive the correct arguments.
+ * Disable the tail call here.
+ */
+#if defined(__sparc)
+ asm("nop");
+#endif
+}
+#endif
+
/* See Objects/lnotab_notes.txt for a description of how tracing works. */
static int
maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
diff -r 736b0aec412b -r d7c58422eada Python/import.c
--- a/Python/import.c Thu Nov 24 22:00:46 2011 +0200
+++ b/Python/import.c Sun Nov 27 05:30:58 2011 +0100
@@ -1391,6 +1391,10 @@
co->co_filename = newname;
Py_INCREF(co->co_filename);
Py_DECREF(tmp);
+#ifdef WITH_DTRACE
+ Py_XDECREF(co->co_filename_encoded);
+ co->co_filename_encoded = PyUnicode_AsUTF8String(co->co_filename);
+#endif
constants = co->co_consts;
n = PyTuple_GET_SIZE(constants);
diff -r 736b0aec412b -r d7c58422eada Python/sysmodule.c
--- a/Python/sysmodule.c Thu Nov 24 22:00:46 2011 +0200
+++ b/Python/sysmodule.c Sun Nov 27 05:30:58 2011 +0100
@@ -1504,6 +1504,7 @@
PyDict_GetItemString(sysdict, "displayhook"));
PyDict_SetItemString(sysdict, "__excepthook__",
PyDict_GetItemString(sysdict, "excepthook"));
+
SET_SYS_FROM_STRING("version",
PyUnicode_FromString(Py_GetVersion()));
SET_SYS_FROM_STRING("hexversion",
diff -r 736b0aec412b -r d7c58422eada configure
--- a/configure Thu Nov 24 22:00:46 2011 +0200
+++ b/configure Sun Nov 27 05:30:58 2011 +0100
@@ -618,6 +618,10 @@
MACHDEP_OBJS
DYNLOADFILE
DLINCLDIR
+DTRACEHDRS
+DTRACEOBJS
+DFLAGS
+DTRACE
THREADOBJ
LDLAST
USE_THREAD_MODULE
@@ -763,6 +767,7 @@
with_tsc
with_pymalloc
with_valgrind
+with_dtrace
with_fpectl
with_libm
with_libc
@@ -1434,6 +1439,7 @@
--with(out)-tsc enable/disable timestamp counter profile
--with(out)-pymalloc disable/enable specialized mallocs
--with-valgrind Enable Valgrind support
+ --with(out)-dtrace disable/enable dtrace support
--with-fpectl enable SIGFPE catching
--with-libm=STRING math library
--with-libc=STRING C library
@@ -9325,6 +9331,93 @@
OPT="-DDYNAMIC_ANNOTATIONS_ENABLED=1 $OPT"
fi
+# Check for dtrace support
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for --with-dtrace" >&5
+$as_echo_n "checking for --with-dtrace... " >&6; }
+
+# Check whether --with-dtrace was given.
+if test "${with_dtrace+set}" = set; then :
+ withval=$with_dtrace;
+fi
+
+
+
+
+DTRACE=
+DFLAGS=
+if test ! -z "$with_dtrace"
+then
+ # The cast to long int works around a bug in the HP C Compiler
+# version HP92453-01 B.11.11.23709.GP, which incorrectly rejects
+# declarations like `int a3[[(sizeof (unsigned char)) >= 0]];'.
+# This bug is HP SR number 8606223364.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking size of long" >&5
+$as_echo_n "checking size of long... " >&6; }
+if ${ac_cv_sizeof_long+:} false; then :
+ $as_echo_n "(cached) " >&6
+else
+ if ac_fn_c_compute_int "$LINENO" "(long int) (sizeof (long))" "ac_cv_sizeof_long" "$ac_includes_default"; then :
+
+else
+ if test "$ac_cv_type_long" = yes; then
+ { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
+$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
+as_fn_error 77 "cannot compute sizeof (long)
+See \`config.log' for more details" "$LINENO" 5; }
+ else
+ ac_cv_sizeof_long=0
+ fi
+fi
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_sizeof_long" >&5
+$as_echo "$ac_cv_sizeof_long" >&6; }
+
+
+
+cat >>confdefs.h <<_ACEOF
+#define SIZEOF_LONG $ac_cv_sizeof_long
+_ACEOF
+
+
+ if test "$ac_cv_sizeof_long" -eq 8
+ then
+ DFLAGS="-64"
+ else
+ DFLAGS="-32"
+ fi
+
+ #if dtrace -G -o /dev/null Include/pydtrace.d 2>/dev/null
+ if true
+ then
+
+$as_echo "#define WITH_DTRACE 1" >>confdefs.h
+
+ with_dtrace="Sun"
+ DTRACEOBJS="Python/phelper.o Python/dtrace.o"
+ DTRADEHDRS=""
+ DTRACE=dtrace
+ elif dtrace -h -o /dev/null -s Include/pydtrace.d
+ then
+
+$as_echo "#define WITH_DTRACE 1" >>confdefs.h
+
+ with_dtrace="Apple"
+ DTRACEOBJS=""
+ DTRADEHDRS="phelper.h pydtrace.h"
+ DTRACE=dtrace
+ else
+ with_dtrace="no"
+ fi
+else
+ with_dtrace="no"
+fi
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_dtrace" >&5
+$as_echo "$with_dtrace" >&6; }
+
+
+
# -I${DLINCLDIR} is added to the compile rule for importdl.o
DLINCLDIR=.
diff -r 736b0aec412b -r d7c58422eada configure.in
--- a/configure.in Thu Nov 24 22:00:46 2011 +0200
+++ b/configure.in Sun Nov 27 05:30:58 2011 +0100
@@ -2506,6 +2506,53 @@
OPT="-DDYNAMIC_ANNOTATIONS_ENABLED=1 $OPT"
fi
+# Check for dtrace support
+AC_MSG_CHECKING(for --with-dtrace)
+AC_ARG_WITH(dtrace,
+ AC_HELP_STRING(--with(out)-dtrace, disable/enable dtrace support))
+
+AC_SUBST(DTRACE)
+AC_SUBST(DFLAGS)
+DTRACE=
+DFLAGS=
+if test ! -z "$with_dtrace"
+then
+ AC_CHECK_SIZEOF([long])
+ if [test "$ac_cv_sizeof_long" -eq 8]
+ then
+ DFLAGS="-64"
+ else
+ DFLAGS="-32"
+ fi
+
+ #if dtrace -G -o /dev/null Include/pydtrace.d 2>/dev/null
+ if true
+ then
+ AC_DEFINE(WITH_DTRACE, 1,
+ [Define if you want to compile in Dtrace support])
+ with_dtrace="Sun"
+ DTRACEOBJS="Python/phelper.o Python/dtrace.o"
+ DTRADEHDRS=""
+ DTRACE=dtrace
+ elif dtrace -h -o /dev/null -s Include/pydtrace.d
+ then
+ AC_DEFINE(WITH_DTRACE, 1,
+ [Define if you want to compile in Dtrace support])
+ with_dtrace="Apple"
+ DTRACEOBJS=""
+ DTRADEHDRS="phelper.h pydtrace.h"
+ DTRACE=dtrace
+ else
+ with_dtrace="no"
+ fi
+else
+ with_dtrace="no"
+fi
+
+AC_MSG_RESULT($with_dtrace)
+AC_SUBST(DTRACEOBJS)
+AC_SUBST(DTRACEHDRS)
+
# -I${DLINCLDIR} is added to the compile rule for importdl.o
AC_SUBST(DLINCLDIR)
DLINCLDIR=.
diff -r 736b0aec412b -r d7c58422eada pyconfig.h.in
--- a/pyconfig.h.in Thu Nov 24 22:00:46 2011 +0200
+++ b/pyconfig.h.in Sun Nov 27 05:30:58 2011 +0100
@@ -1260,6 +1260,9 @@
/* Define if you want documentation strings in extension modules */
#undef WITH_DOC_STRINGS
+/* Define to compile in Dtrace support */
+#undef WITH_DTRACE
+
/* Define if you want to use the new-style (Openstep, Rhapsody, MacOS) dynamic
linker (dyld) instead of the old-style (NextStep) dynamic linker (rld).
Dyld is necessary to support frameworks. */
diff -r 736b0aec412b -r d7c58422eada setup.py
--- a/setup.py Thu Nov 24 22:00:46 2011 +0200
+++ b/setup.py Sun Nov 27 05:30:58 2011 +0100
@@ -569,6 +569,9 @@
# syslog daemon interface
exts.append( Extension('syslog', ['syslogmodule.c']) )
+ # jcea DTRACE probes
+ exts.append( Extension('dtrace', ['dtracemodule.c']) )
+
#
# Here ends the simple stuff. From here on, modules need certain
# libraries, are platform-specific, or present other surprises.