churchyard / rpms / python3

Forked from rpms/python3 3 years ago
Clone

Blame 00055-systemtap.patch

78727de
diff -up Python-3.3.0rc2/configure.ac.systemtap Python-3.3.0rc2/configure.ac
78727de
--- Python-3.3.0rc2/configure.ac.systemtap	2012-09-09 05:11:14.000000000 -0400
78727de
+++ Python-3.3.0rc2/configure.ac	2012-09-10 09:17:21.114511781 -0400
62d7207
@@ -2678,6 +2678,23 @@ if test "$with_valgrind" != no; then
7eb10c8
     OPT="-DDYNAMIC_ANNOTATIONS_ENABLED=1 $OPT"
c969609
 fi
c969609
 
1c94c1a
+# Check for systemtap support
1c94c1a
+# On Linux, /usr/bin/dtrace is in fact a shim to SystemTap
1c94c1a
+AC_MSG_CHECKING([for --with-systemtap])
1c94c1a
+AC_ARG_WITH([systemtap],
1c94c1a
+            AC_HELP_STRING([--with(out)-systemtap], [disable/enable SystemTap support]),,
1c94c1a
+            with_systemtap=no)
1c94c1a
+AC_MSG_RESULT([$with_systemtap])
1c94c1a
+if test "$with_systemtap" != no; then
1c94c1a
+    AC_DEFINE(WITH_SYSTEMTAP, 1,
1c94c1a
+        [Define if you want to compile in SystemTap support])
1c94c1a
+    SYSTEMTAPOBJS="Python/pysystemtap.o"
1c94c1a
+    SYSTEMTAPDEPS="\$(srcdir)/Python/pysystemtap.h"
c969609
+fi
c969609
+
1c94c1a
+AC_SUBST(SYSTEMTAPOBJS)
1c94c1a
+AC_SUBST(SYSTEMTAPDEPS)
c969609
+
5080ffc
 # -I${DLINCLDIR} is added to the compile rule for importdl.o
5080ffc
 AC_SUBST(DLINCLDIR)
5080ffc
 DLINCLDIR=.
78727de
diff -up Python-3.3.0rc2/configure.systemtap Python-3.3.0rc2/configure
78727de
--- Python-3.3.0rc2/configure.systemtap	2012-09-09 05:11:14.000000000 -0400
78727de
+++ Python-3.3.0rc2/configure	2012-09-10 09:17:21.116511780 -0400
62d7207
@@ -618,6 +618,8 @@ TRUE
1c94c1a
 MACHDEP_OBJS
1c94c1a
 DYNLOADFILE
1c94c1a
 DLINCLDIR
1c94c1a
+SYSTEMTAPDEPS
1c94c1a
+SYSTEMTAPOBJS
1c94c1a
 THREADOBJ
1c94c1a
 LDLAST
1c94c1a
 USE_THREAD_MODULE
62d7207
@@ -779,6 +781,7 @@ with_doc_strings
1c94c1a
 with_tsc
1c94c1a
 with_pymalloc
1c94c1a
 with_valgrind
1c94c1a
+with_systemtap
1c94c1a
 with_fpectl
1c94c1a
 with_libm
1c94c1a
 with_libc
62d7207
@@ -1456,6 +1459,7 @@ Optional Packages:
1c94c1a
   --with(out)-tsc         enable/disable timestamp counter profile
1c94c1a
   --with(out)-pymalloc    disable/enable specialized mallocs
1c94c1a
   --with-valgrind         Enable Valgrind support
1c94c1a
+  --with(out)-systemtap   disable/enable SystemTap support
1c94c1a
   --with-fpectl           enable SIGFPE catching
1c94c1a
   --with-libm=STRING      math library
1c94c1a
   --with-libc=STRING      C library
62d7207
@@ -10065,6 +10069,31 @@ fi
1c94c1a
     OPT="-DDYNAMIC_ANNOTATIONS_ENABLED=1 $OPT"
1c94c1a
 fi
1c94c1a
 
1c94c1a
+# Check for systemtap support
1c94c1a
+# On Linux, /usr/bin/dtrace is in fact a shim to SystemTap
1c94c1a
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for --with-systemtap" >&5
1c94c1a
+$as_echo_n "checking for --with-systemtap... " >&6; }
1c94c1a
+
1c94c1a
+# Check whether --with-systemtap was given.
1c94c1a
+if test "${with_systemtap+set}" = set; then :
1c94c1a
+  withval=$with_systemtap;
1c94c1a
+else
1c94c1a
+  with_systemtap=no
1c94c1a
+fi
1c94c1a
+
1c94c1a
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_systemtap" >&5
1c94c1a
+$as_echo "$with_systemtap" >&6; }
1c94c1a
+if test "$with_systemtap" != no; then
1c94c1a
+
1c94c1a
+$as_echo "#define WITH_SYSTEMTAP 1" >>confdefs.h
1c94c1a
+
1c94c1a
+    SYSTEMTAPOBJS="Python/pysystemtap.o"
1c94c1a
+    SYSTEMTAPDEPS="\$(srcdir)/Python/pysystemtap.h"
1c94c1a
+fi
1c94c1a
+
1c94c1a
+
1c94c1a
+
1c94c1a
+
1c94c1a
 # -I${DLINCLDIR} is added to the compile rule for importdl.o
1c94c1a
 
1c94c1a
 DLINCLDIR=.
78727de
diff -up Python-3.3.0rc2/Doc/howto/index.rst.systemtap Python-3.3.0rc2/Doc/howto/index.rst
78727de
--- Python-3.3.0rc2/Doc/howto/index.rst.systemtap	2012-09-09 05:10:51.000000000 -0400
78727de
+++ Python-3.3.0rc2/Doc/howto/index.rst	2012-09-10 09:17:21.117511779 -0400
1c94c1a
@@ -29,4 +29,5 @@ Currently, the HOWTOs are:
1c94c1a
    argparse.rst
1c94c1a
    ipaddress.rst
61fd48d
    clinic.rst
1c94c1a
+   instrumentation.rst
1c94c1a
 
78727de
diff -up Python-3.3.0rc2/Doc/howto/instrumentation.rst.systemtap Python-3.3.0rc2/Doc/howto/instrumentation.rst
78727de
--- Python-3.3.0rc2/Doc/howto/instrumentation.rst.systemtap	2012-09-10 09:17:21.117511779 -0400
78727de
+++ Python-3.3.0rc2/Doc/howto/instrumentation.rst	2012-09-10 09:17:21.117511779 -0400
1c94c1a
@@ -0,0 +1,295 @@
1c94c1a
+.. _instrumentation:
1c94c1a
+
1c94c1a
+====================================
1c94c1a
+Instrumenting CPython with SystemTap
1c94c1a
+====================================
1c94c1a
+
1c94c1a
+:author: David Malcolm <dmalcolm@redhat.com>
1c94c1a
+
1c94c1a
+DTrace and SystemTap are monitoring tools, each providing a way to inspect
1c94c1a
+what the processes on a computer system are doing.  They both use
1c94c1a
+domain-specific languages allowing a user to write scripts which:
1c94c1a
+
1c94c1a
+  - filter which processes are to be observed
1c94c1a
+  - gather data from the processes of interest
1c94c1a
+  - generate reports on the data
1c94c1a
+
1c94c1a
+As of Python 3.3, CPython can be built with embedded "markers" that can be
1c94c1a
+observed by a SystemTap script, making it easier to monitor what the CPython
1c94c1a
+processes on a system are doing.
1c94c1a
+
1c94c1a
+.. Potentially this document could be expanded to also cover DTrace markers.
1c94c1a
+   However, I'm not a DTrace expert.
1c94c1a
+
1c94c1a
+.. I'm using ".. code-block:: c" for SystemTap scripts, as "c" is syntactically
1c94c1a
+   the closest match that Sphinx supports
1c94c1a
+
1c94c1a
+
1c94c1a
+Enabling the static markers
1c94c1a
+---------------------------
1c94c1a
+
1c94c1a
+In order to build CPython with the embedded markers for SystemTap, the
1c94c1a
+SystemTap development tools must be installed.
1c94c1a
+
1c94c1a
+On a Fedora or Red Hat Enterprise Linux machine, this can be done via::
1c94c1a
+
1c94c1a
+   yum install systemtap-sdt-devel
1c94c1a
+
1c94c1a
+CPython must then be configured `--with-systemtap`::
1c94c1a
+
1c94c1a
+   checking for --with-systemtap... yes
1c94c1a
+
1c94c1a
+You can verify if the SystemTap static markers are present in the built
1c94c1a
+binary by seeing if it contains a ".note.stapsdt" section.
1c94c1a
+
1c94c1a
+.. code-block:: bash
1c94c1a
+
1c94c1a
+   $ eu-readelf -S ./python | grep .note.stapsdt
1c94c1a
+   [29] .note.stapsdt        NOTE         0000000000000000 00308d78 000000b8  0        0   0  4
1c94c1a
+
1c94c1a
+If you've built python as a shared library (with --enable-shared), you need
1c94c1a
+to look instead within the shared library.  For example:
1c94c1a
+
1c94c1a
+.. code-block:: bash
1c94c1a
+
1c94c1a
+   $ eu-readelf -S libpython3.3dm.so.1.0 | grep .note.stapsdt
1c94c1a
+   [28] .note.stapsdt        NOTE         0000000000000000 00365b68 000000b8  0        0   0  4
1c94c1a
+
1c94c1a
+Earlier versions of SystemTap stored the markers in a ".probes" section.
1c94c1a
+
1c94c1a
+For the curious, you can see the metadata for the static markers using this
1c94c1a
+invocation.
1c94c1a
+
1c94c1a
+.. code-block:: bash
1c94c1a
+
1c94c1a
+  $ eu-readelf -x .note.stapsdt ./python
1c94c1a
+
1c94c1a
+  Hex dump of section [29] '.note.stapsdt', 184 bytes at offset 0x308d78:
1c94c1a
+    0x00000000 08000000 45000000 03000000 73746170 ....E.......stap
1c94c1a
+    0x00000010 73647400 d4664b00 00000000 4fc36600 sdt..fK.....O.f.
1c94c1a
+    0x00000020 00000000 488d9000 00000000 70797468 ....H.......pyth
1c94c1a
+    0x00000030 6f6e0066 756e6374 696f6e5f 5f656e74 on.function__ent
1c94c1a
+    0x00000040 72790038 40257261 78203840 25726478 ry.8@%rax 8@%rdx
1c94c1a
+    0x00000050 202d3440 25656378 00000000 08000000  -4@%ecx........
1c94c1a
+    0x00000060 46000000 03000000 73746170 73647400 F.......stapsdt.
1c94c1a
+    0x00000070 0d674b00 00000000 4fc36600 00000000 .gK.....O.f.....
1c94c1a
+    0x00000080 4a8d9000 00000000 70797468 6f6e0066 J.......python.f
1c94c1a
+    0x00000090 756e6374 696f6e5f 5f726574 75726e00 unction__return.
1c94c1a
+    0x000000a0 38402572 61782038 40257264 78202d34 8@%rax 8@%rdx -4
1c94c1a
+    0x000000b0 40256563 78000000                   @%ecx...
1c94c1a
+
1c94c1a
+and a sufficiently modern eu-readelf can print the metadata:
1c94c1a
+
1c94c1a
+.. code-block:: bash
1c94c1a
+
1c94c1a
+  $ eu-readelf -n ./python
1c94c1a
+
1c94c1a
+  Note section [ 1] '.note.gnu.build-id' of 36 bytes at offset 0x190:
1c94c1a
+    Owner          Data size  Type
1c94c1a
+    GNU                   20  GNU_BUILD_ID
1c94c1a
+      Build ID: a28f8db1b224530b0d38ad7b82a249cf7c3f18d6
1c94c1a
+
1c94c1a
+  Note section [27] '.note.stapsdt' of 184 bytes at offset 0x1ae884:
1c94c1a
+    Owner          Data size  Type
1c94c1a
+    stapsdt               70  Version: 3
1c94c1a
+      PC: 0xe0d3a, Base: 0x14b150, Semaphore: 0x3ae882
1c94c1a
+      Provider: python, Name: function__return, Args: '8@%rbx 8@%r13 -4@%eax'
1c94c1a
+    stapsdt               69  Version: 3
1c94c1a
+      PC: 0xe0f37, Base: 0x14b150, Semaphore: 0x3ae880
1c94c1a
+      Provider: python, Name: function__entry, Args: '8@%rbx 8@%r13 -4@%eax'
1c94c1a
+
1c94c1a
+The above metadata contains information for SystemTap describing how it can
1c94c1a
+patch strategically-placed machine code instructions to enable the tracing
1c94c1a
+hooks used by a SystemTap script.
1c94c1a
+
1c94c1a
+
1c94c1a
+Static markers
1c94c1a
+--------------
1c94c1a
+
1c94c1a
+The low-level way to use the SystemTap integration is to use the static
1c94c1a
+markers directly.  This requires you to explicitly state the binary file
1c94c1a
+containing them.
1c94c1a
+
1c94c1a
+For example, this script can be used to show the call/return hierarchy of a
1c94c1a
+Python script:
1c94c1a
+
1c94c1a
+.. code-block:: c
1c94c1a
+
1c94c1a
+   probe process('python').mark("function__entry") {
1c94c1a
+        filename = user_string($arg1);
1c94c1a
+        funcname = user_string($arg2);
1c94c1a
+        lineno = $arg3;
1c94c1a
+
1c94c1a
+        printf("%s => %s in %s:%d\\n",
1c94c1a
+               thread_indent(1), funcname, filename, lineno);
1c94c1a
+   }
1c94c1a
+
1c94c1a
+   probe process('python').mark("function__return") {
1c94c1a
+       filename = user_string($arg1);
1c94c1a
+       funcname = user_string($arg2);
1c94c1a
+       lineno = $arg3;
1c94c1a
+
1c94c1a
+       printf("%s <= %s in %s:%d\\n",
1c94c1a
+              thread_indent(-1), funcname, filename, lineno);
1c94c1a
+   }
1c94c1a
+
1c94c1a
+It can be invoked like this:
1c94c1a
+
1c94c1a
+.. code-block:: bash
1c94c1a
+
1c94c1a
+   $ stap \
1c94c1a
+     show-call-hierarchy.stp \
1c94c1a
+     -c ./python test.py
1c94c1a
+
1c94c1a
+The output looks like this::
1c94c1a
+
1c94c1a
+   11408 python(8274):        => __contains__ in Lib/_abcoll.py:362
1c94c1a
+   11414 python(8274):         => __getitem__ in Lib/os.py:425
1c94c1a
+   11418 python(8274):          => encode in Lib/os.py:490
1c94c1a
+   11424 python(8274):          <= encode in Lib/os.py:493
1c94c1a
+   11428 python(8274):         <= __getitem__ in Lib/os.py:426
1c94c1a
+   11433 python(8274):        <= __contains__ in Lib/_abcoll.py:366
1c94c1a
+
1c94c1a
+where the columns are:
1c94c1a
+
1c94c1a
+  - time in microseconds since start of script
1c94c1a
+
1c94c1a
+  - name of executable
1c94c1a
+
1c94c1a
+  - PID of process
1c94c1a
+
1c94c1a
+and the remainder indicates the call/return hierarchy as the script executes.
1c94c1a
+
1c94c1a
+For a `--enable-shared` build of CPython, the markers are contained within the
1c94c1a
+libpython shared library, and the probe's dotted path needs to reflect this. For
1c94c1a
+example, this line from the above example::
1c94c1a
+
1c94c1a
+   probe process('python').mark("function__entry") {
1c94c1a
+
1c94c1a
+should instead read::
1c94c1a
+
1c94c1a
+   probe process('python').library("libpython3.3dm.so.1.0").mark("function__entry") {
1c94c1a
+
1c94c1a
+(assuming a debug build of CPython 3.3)
1c94c1a
+
1c94c1a
+.. I'm reusing the "c:function" type for markers
1c94c1a
+
1c94c1a
+.. c:function:: function__entry(str filename, str funcname, int lineno)
1c94c1a
+
1c94c1a
+   This marker indicates that execution of a Python function has begun.  It is
1c94c1a
+   only triggered for pure-python (bytecode) functions.
1c94c1a
+
1c94c1a
+   The filename, function name, and line number are provided back to the
1c94c1a
+   tracing script as positional arguments, which must be accessed using
1c94c1a
+   `$arg1`, `$arg2`:
1c94c1a
+
1c94c1a
+       * `$arg1` : `(const char *)` filename, accessible using `user_string($arg1)`
1c94c1a
+
1c94c1a
+       * `$arg2` : `(const char *)` function name, accessible using
1c94c1a
+         `user_string($arg2)`
1c94c1a
+
1c94c1a
+       * `$arg3` : `int` line number
1c94c1a
+
1c94c1a
+       * `$arg4` : `(PyFrameObject *)`, the frame being executed
1c94c1a
+
1c94c1a
+.. c:function:: function__return(str filename, str funcname, int lineno)
1c94c1a
+
1c94c1a
+   This marker is the converse of `function__entry`, and indicates that
1c94c1a
+   execution of a Python function has ended (either via ``return``, or via an
1c94c1a
+   exception).  It is only triggered for pure-python (bytecode) functions.
1c94c1a
+
1c94c1a
+   The arguments are the same as for `function__entry`
1c94c1a
+
1c94c1a
+
1c94c1a
+Tapsets
1c94c1a
+-------
1c94c1a
+
1c94c1a
+The higher-level way to use the SystemTap integration is to use a "tapset":
1c94c1a
+SystemTap's equivalent of a library, which hides some of the lower-level
1c94c1a
+details of the static markers.
1c94c1a
+
1c94c1a
+Here is a tapset file, based on a non-shared build of CPython:
1c94c1a
+
1c94c1a
+.. code-block:: c
1c94c1a
+
1c94c1a
+    /*
1c94c1a
+       Provide a higher-level wrapping around the function__entry and
1c94c1a
+       function__return markers:
1c94c1a
+     */
1c94c1a
+    probe python.function.entry = process("python").mark("function__entry")
1c94c1a
+    {
1c94c1a
+        filename = user_string($arg1);
1c94c1a
+        funcname = user_string($arg2);
1c94c1a
+        lineno = $arg3;
1c94c1a
+        frameptr = $arg4
1c94c1a
+    }
1c94c1a
+    probe python.function.return = process("python").mark("function__return")
1c94c1a
+    {
1c94c1a
+        filename = user_string($arg1);
1c94c1a
+        funcname = user_string($arg2);
1c94c1a
+        lineno = $arg3;
1c94c1a
+        frameptr = $arg4
1c94c1a
+    }
1c94c1a
+
1c94c1a
+If this file is installed in SystemTap's tapset directory (e.g.
1c94c1a
+`/usr/share/systemtap/tapset`), then these additional probepoints become
1c94c1a
+available:
1c94c1a
+
1c94c1a
+.. c:function:: python.function.entry(str filename, str funcname, int lineno, frameptr)
1c94c1a
+
1c94c1a
+   This probe point indicates that execution of a Python function has begun.
1c94c1a
+   It is only triggered for pure-python (bytecode) functions.
1c94c1a
+
1c94c1a
+.. c:function:: python.function.return(str filename, str funcname, int lineno, frameptr)
1c94c1a
+
1c94c1a
+   This probe point is the converse of `python.function.return`, and indicates
1c94c1a
+   that execution of a Python function has ended (either via ``return``, or
1c94c1a
+   via an exception).  It is only triggered for pure-python (bytecode) functions.
1c94c1a
+
1c94c1a
+
1c94c1a
+Examples
1c94c1a
+--------
1c94c1a
+This SystemTap script uses the tapset above to more cleanly implement the
1c94c1a
+example given above of tracing the Python function-call hierarchy, without
1c94c1a
+needing to directly name the static markers:
1c94c1a
+
1c94c1a
+.. code-block:: c
1c94c1a
+
1c94c1a
+    probe python.function.entry
1c94c1a
+    {
1c94c1a
+      printf("%s => %s in %s:%d\n",
1c94c1a
+             thread_indent(1), funcname, filename, lineno);
1c94c1a
+    }
1c94c1a
+
1c94c1a
+    probe python.function.return
1c94c1a
+    {
1c94c1a
+      printf("%s <= %s in %s:%d\n",
1c94c1a
+             thread_indent(-1), funcname, filename, lineno);
1c94c1a
+    }
1c94c1a
+
c969609
+
1c94c1a
+The following script uses the tapset above to provide a top-like view of all
1c94c1a
+running CPython code, showing the top 20 most frequently-entered bytecode
1c94c1a
+frames, each second, across the whole system:
1c94c1a
+
1c94c1a
+.. code-block:: c
1c94c1a
+
1c94c1a
+    global fn_calls;
1c94c1a
+
1c94c1a
+    probe python.function.entry
1c94c1a
+    {
1c94c1a
+      fn_calls[pid(), filename, funcname, lineno] += 1;
1c94c1a
+    }
1c94c1a
+
1c94c1a
+    probe timer.ms(1000) {
1c94c1a
+        printf("\033[2J\033[1;1H") /* clear screen */
1c94c1a
+        printf("%6s %80s %6s %30s %6s\n",
1c94c1a
+               "PID", "FILENAME", "LINE", "FUNCTION", "CALLS")
1c94c1a
+        foreach ([pid, filename, funcname, lineno] in fn_calls- limit 20) {
1c94c1a
+            printf("%6d %80s %6d %30s %6d\n",
1c94c1a
+                pid, filename, lineno, funcname,
1c94c1a
+                fn_calls[pid, filename, funcname, lineno]);
1c94c1a
+        }
1c94c1a
+        delete fn_calls;
1c94c1a
+    }
1c94c1a
+
78727de
diff -up Python-3.3.0rc2/Lib/test/test_systemtap.py.systemtap Python-3.3.0rc2/Lib/test/test_systemtap.py
78727de
--- Python-3.3.0rc2/Lib/test/test_systemtap.py.systemtap	2012-09-10 09:17:21.117511779 -0400
78727de
+++ Python-3.3.0rc2/Lib/test/test_systemtap.py	2012-09-10 09:17:21.117511779 -0400
62d7207
@@ -0,0 +1,234 @@
1c94c1a
+# Verify that systemtap static probes work
1c94c1a
+#
1c94c1a
+import subprocess
1c94c1a
+import sys
1c94c1a
+import sysconfig
1c94c1a
+import os
1c94c1a
+import unittest
1c94c1a
+
1c94c1a
+from test.support import run_unittest, TESTFN, unlink
1c94c1a
+
1c94c1a
+if '--with-systemtap' not in sysconfig.get_config_var('CONFIG_ARGS'):
1c94c1a
+    raise unittest.SkipTest("Python was not configured --with-systemtap")
1c94c1a
+
1c94c1a
+try:
1c94c1a
+    _, stap_version = subprocess.Popen(["stap", "-V"],
1c94c1a
+                                       stdout=subprocess.PIPE,
1c94c1a
+                                       stderr=subprocess.PIPE,
1c94c1a
+                                       ).communicate()
1c94c1a
+except OSError:
1c94c1a
+    # This is what "no stap" looks like.  There may, however, be other
1c94c1a
+    # errors that manifest this way too.
1c94c1a
+    raise unittest.SkipTest("Couldn't find stap on the path")
1c94c1a
+
1c94c1a
+def invoke_systemtap_script(script, cmd):
1c94c1a
+    # Start a child process, probing with the given systemtap script
1c94c1a
+    # (passed as stdin to the "stap" tool)
1c94c1a
+    # The script should be a bytes instance
1c94c1a
+    # Return (stdout, stderr) pair
1c94c1a
+
1c94c1a
+    p = subprocess.Popen(["stap", "-", '-vv', '-c', cmd],
1c94c1a
+                         stdin=subprocess.PIPE,
1c94c1a
+                         stdout=subprocess.PIPE,
1c94c1a
+                         stderr=subprocess.PIPE)
1c94c1a
+    out, err = p.communicate(input=script)
1c94c1a
+    return out, err
1c94c1a
+
1c94c1a
+# Verify that stap can run a simple "hello world"-style script
1c94c1a
+# This can fail for various reasons:
1c94c1a
+# - missing kernel headers
1c94c1a
+# - permissions (a non-root user needs to be in the "stapdev" group)
1c94c1a
+TRIVIAL_STAP_SCRIPT = b'probe begin { println("hello world") exit () }'
1c94c1a
+
1c94c1a
+out, err = invoke_systemtap_script(TRIVIAL_STAP_SCRIPT, 'true')
1c94c1a
+if out != b'hello world\n':
1c94c1a
+    raise unittest.SkipTest("Test systemtap script did not run; stderr was: %s" % err)
1c94c1a
+
1c94c1a
+# We don't expect stderr to be empty, since we're invoking stap with "-vv": stap
1c94c1a
+# will (we hope) generate debugging output on stderr.
1c94c1a
+
1c94c1a
+def invoke_python_under_systemtap(script, pythoncode=None, pythonfile=None):
1c94c1a
+    # Start a child python process, probing with the given systemtap script
1c94c1a
+    # (passed as stdin to the "stap" tool)
1c94c1a
+    # The script should be a bytes instance
1c94c1a
+    # Return (stdout, stderr) pair
1c94c1a
+
1c94c1a
+    if pythonfile:
1c94c1a
+        pythoncmd = '%s %s' % (sys.executable, pythonfile)
1c94c1a
+    else:
1c94c1a
+        pythoncmd = '%s -c %r' % (sys.executable, pythoncode)
1c94c1a
+
1c94c1a
+    # The process tree of a stap invocation of a command goes through
1c94c1a
+    # something like this:
1c94c1a
+    #    stap ->fork/exec(staprun; exec stapio ->f/e(-c cmd); exec staprun -r)
1c94c1a
+    # and this trip through setuid leads to LD_LIBRARY_PATH being dropped,
1c94c1a
+    # which would lead to an --enable-shared build of python failing to be
1c94c1a
+    # find its libpython, with an error like:
1c94c1a
+    #    error while loading shared libraries: libpython3.3dm.so.1.0: cannot
1c94c1a
+    #    open shared object file: No such file or directory
1c94c1a
+    # Hence we need to jump through some hoops to expose LD_LIBRARY_PATH to
1c94c1a
+    # the invoked python process:
1c94c1a
+    LD_LIBRARY_PATH = os.environ.get('LD_LIBRARY_PATH', '')
1c94c1a
+    if LD_LIBRARY_PATH:
1c94c1a
+        pythoncmd = 'env LD_LIBRARY_PATH=%s ' % LD_LIBRARY_PATH + pythoncmd
1c94c1a
+
1c94c1a
+    return invoke_systemtap_script(script, pythoncmd)
1c94c1a
+
1c94c1a
+# When using the static markers, we need to supply the prefix of a systemtap
1c94c1a
+# dotted probe point that containing the marker.
1c94c1a
+# See http://sourceware.org/systemtap/langref/Probe_points.html
1c94c1a
+#
1c94c1a
+# We need to determine if this is a shared-library build
1c94c1a
+#
1c94c1a
+# Note that sysconfig can get this wrong; see:
1c94c1a
+#   http://bugs.python.org/issue14774
1c94c1a
+#
1c94c1a
+if '--enable-shared' in sysconfig.get_config_var('CONFIG_ARGS'):
1c94c1a
+    # For a shared-library build, the markers are in library(INSTSONAME):
1c94c1a
+    INSTSONAME = sysconfig.get_config_var('INSTSONAME')
1c94c1a
+    probe_prefix = 'process("%s").library("%s")' % (sys.executable, INSTSONAME)
1c94c1a
+else:
1c94c1a
+    # For a non-shared-library build, we can simply use sys.executable:
1c94c1a
+    probe_prefix = 'process("%s")' % sys.executable
1c94c1a
+
1c94c1a
+# The following script ought to generate lots of lines showing recursive
1c94c1a
+# function entry and return, of the form:
1c94c1a
+#     11408 python(8274):        => __contains__ in Lib/_abcoll.py:362
1c94c1a
+#     11414 python(8274):         => __getitem__ in Lib/os.py:425
1c94c1a
+#     11418 python(8274):          => encode in Lib/os.py:490
1c94c1a
+#     11424 python(8274):          <= encode in Lib/os.py:493
1c94c1a
+#     11428 python(8274):         <= __getitem__ in Lib/os.py:426
1c94c1a
+#     11433 python(8274):        <= __contains__ in Lib/_abcoll.py:366
1c94c1a
+# where the column are:
1c94c1a
+#  - time in microseconds since start of script
1c94c1a
+#  - name of executable
1c94c1a
+#  - PID of process
1c94c1a
+#  and the remainder indicates the call/return hierarchy
1c94c1a
+
1c94c1a
+hierarchy_script = ('''
1c94c1a
+probe %s.mark("function__entry") {
1c94c1a
+    filename = user_string($arg1);
1c94c1a
+    funcname = user_string($arg2);
1c94c1a
+    lineno = $arg3;
1c94c1a
+
1c94c1a
+    printf("%%s => %%s in %%s:%%d\\n", thread_indent(1), funcname, filename, lineno);
1c94c1a
+}
1c94c1a
+
1c94c1a
+probe %s.mark("function__return") {
1c94c1a
+    filename = user_string($arg1);
1c94c1a
+    funcname = user_string($arg2);
1c94c1a
+    lineno = $arg3;
1c94c1a
+
1c94c1a
+    printf("%%s <= %%s in %%s:%%d\\n", thread_indent(-1), funcname, filename, lineno);
1c94c1a
+}
1c94c1a
+''' % (probe_prefix, probe_prefix)).encode('utf-8')
1c94c1a
+
1c94c1a
+
1c94c1a
+class ErrorDumper:
1c94c1a
+    # A context manager that dumps extra information if an exception is raised,
1c94c1a
+    # to help track down why the problem occurred
1c94c1a
+    def __init__(self, out, err):
1c94c1a
+        self.out = out
1c94c1a
+        self.err = err
1c94c1a
+
1c94c1a
+    def __enter__(self):
1c94c1a
+        pass
1c94c1a
+
1c94c1a
+    def __exit__(self, type_, value, traceback):
1c94c1a
+        if type_:
1c94c1a
+            # an exception is being raised:
1c94c1a
+            print('stdout: %s' % out.decode())
1c94c1a
+            print('stderr: %s' % err.decode())
1c94c1a
+
1c94c1a
+class SystemtapTests(unittest.TestCase):
1c94c1a
+
1c94c1a
+    def test_invoking_python(self):
1c94c1a
+        # Ensure that we can invoke python under stap, with a trivial stap
1c94c1a
+        # script:
1c94c1a
+        out, err = invoke_python_under_systemtap(
1c94c1a
+            b'probe begin { println("hello from stap") exit () }',
1c94c1a
+            pythoncode="print('hello from python')")
1c94c1a
+        with ErrorDumper(out, err):
1c94c1a
+            self.assertIn(b'hello from stap', out)
1c94c1a
+            self.assertIn(b'hello from python', out)
1c94c1a
+
1c94c1a
+    def test_function_entry(self):
1c94c1a
+        # Ensure that the function_entry static marker works
1c94c1a
+        out, err = invoke_python_under_systemtap(hierarchy_script)
1c94c1a
+        # stdout ought to contain various lines showing recursive function
1c94c1a
+        # entry and return (see above)
1c94c1a
+
1c94c1a
+        # Uncomment this for debugging purposes:
1c94c1a
+        # print(out.decode('utf-8'))
1c94c1a
+
1c94c1a
+        #   Executing the cmdline-supplied "pass":
1c94c1a
+        #      0 python(8274): => <module> in <string>:1
1c94c1a
+        #      5 python(8274): <= <module> in <string>:1
1c94c1a
+        with ErrorDumper(out, err):
1c94c1a
+            self.assertIn(b'=> <module> in <string>:1', out,
1c94c1a
+                          msg="stdout: %s\nstderr: %s\n" % (out, err))
1c94c1a
+
62d7207
+    def test_function_encoding(self):
62d7207
+        # Ensure that function names containing non-Latin 1 code
1c94c1a
+        # points are handled:
62d7207
+        pythonfile = TESTFN
1c94c1a
+        try:
1c94c1a
+            unlink(pythonfile)
1c94c1a
+            f = open(pythonfile, "wb")
1c94c1a
+            f.write("""
1c94c1a
+# Sample script with non-ASCII filename, for use by test_systemtap.py
1c94c1a
+# Implicitly UTF-8
1c94c1a
+
1c94c1a
+def 文字化け():
1c94c1a
+    '''Function with non-ASCII identifier; I believe this reads "mojibake"'''
1c94c1a
+    print("hello world!")
1c94c1a
+
1c94c1a
+文字化け()
1c94c1a
+""".encode('utf-8'))
1c94c1a
+            f.close()
1c94c1a
+
1c94c1a
+            out, err = invoke_python_under_systemtap(hierarchy_script,
1c94c1a
+                                                     pythonfile=pythonfile)
1c94c1a
+            out_utf8 = out.decode('utf-8')
1c94c1a
+            with ErrorDumper(out, err):
1c94c1a
+                self.assertIn('=> <module> in %s:5' % pythonfile, out_utf8)
1c94c1a
+                self.assertIn(' => 文字化け in %s:5' % pythonfile, out_utf8)
1c94c1a
+                self.assertIn(' <= 文字化け in %s:7' % pythonfile, out_utf8)
1c94c1a
+                self.assertIn('<= <module> in %s:9' % pythonfile, out_utf8)
1c94c1a
+        finally:
1c94c1a
+            unlink(pythonfile)
1c94c1a
+
62d7207
+    @unittest.skipIf(sys.getfilesystemencoding() == 'ascii',
62d7207
+                     'the test filename is not encodable with ASCII')
62d7207
+    def test_filename_encoding(self):
62d7207
+        # Ensure that scripts names containing non-Latin 1 code
62d7207
+        # points are handled:
62d7207
+        pythonfile = TESTFN + '_☠.py'
62d7207
+        try:
62d7207
+            unlink(pythonfile)
62d7207
+            f = open(pythonfile, "wb")
62d7207
+            f.write("""
62d7207
+def foo():
62d7207
+    '''Function with non-ASCII identifier; I believe this reads "mojibake"'''
62d7207
+    print("hello world!")
62d7207
+
62d7207
+foo()
62d7207
+""".encode('utf-8'))
62d7207
+            f.close()
62d7207
+
62d7207
+            out, err = invoke_python_under_systemtap(hierarchy_script,
62d7207
+                                                     pythonfile=pythonfile)
62d7207
+            out_utf8 = out.decode('utf-8')
62d7207
+            with ErrorDumper(out, err):
62d7207
+                self.assertIn('=> <module> in %s:2' % pythonfile, out_utf8)
62d7207
+                self.assertIn(' => foo in %s:2' % pythonfile, out_utf8)
62d7207
+                self.assertIn(' <= foo in %s:4' % pythonfile, out_utf8)
62d7207
+                self.assertIn('<= <module> in %s:6' % pythonfile, out_utf8)
62d7207
+        finally:
62d7207
+            unlink(pythonfile)
62d7207
+
1c94c1a
+def test_main():
1c94c1a
+    run_unittest(SystemtapTests)
1c94c1a
+
1c94c1a
+if __name__ == "__main__":
1c94c1a
+    test_main()
78727de
diff -up Python-3.3.0rc2/Makefile.pre.in.systemtap Python-3.3.0rc2/Makefile.pre.in
78727de
--- Python-3.3.0rc2/Makefile.pre.in.systemtap	2012-09-09 05:11:05.000000000 -0400
78727de
+++ Python-3.3.0rc2/Makefile.pre.in	2012-09-10 09:19:51.195501518 -0400
62d7207
@@ -363,6 +363,7 @@ PYTHON_OBJS=	\
c969609
 		Python/formatter_unicode.o \
5080ffc
 		Python/fileutils.o \
c969609
 		Python/$(DYNLOADFILE) \
1c94c1a
+		@SYSTEMTAPOBJS@ \
c969609
 		$(LIBOBJS) \
c969609
 		$(MACHDEP_OBJS) \
c969609
 		$(THREADOBJ)
62d7207
@@ -713,7 +714,8 @@ Objects/setobject.o: $(srcdir)/Objects/s
1c94c1a
 $(OPCODETARGETS_H): $(OPCODETARGETGEN_FILES)
1c94c1a
 	$(OPCODETARGETGEN) $(OPCODETARGETS_H)
1c94c1a
 
1c94c1a
-Python/ceval.o: $(OPCODETARGETS_H) $(srcdir)/Python/ceval_gil.h
1c94c1a
+Python/ceval.o: $(OPCODETARGETS_H) $(srcdir)/Python/ceval_gil.h \
1c94c1a
+			$(srcdir)/Python/ceval_systemtap.h @SYSTEMTAPDEPS@
1c94c1a
 
Matej Stuchlik abb2ff8
 Python/frozen.o: Python/importlib.h Python/importlib_external.h
f5250ec
 
62d7207
@@ -724,6 +726,13 @@ Objects/typeobject.o: $(srcdir)/Objects/
58f477b
 Objects/typeslots.inc: $(srcdir)/Include/typeslots.h $(srcdir)/Objects/typeslots.py
58f477b
 	$(PYTHON) $(srcdir)/Objects/typeslots.py < $(srcdir)/Include/typeslots.h > Objects/typeslots.inc
c969609
 
1c94c1a
+# Only needed with --with-systemtap; not a public header:
1c94c1a
+$(srcdir)/Python/pysystemtap.h: $(srcdir)/Python/pysystemtap.d
1c94c1a
+	dtrace -o $@ $(DFLAGS) -C -h -s $(srcdir)/Python/pysystemtap.d
c969609
+
1c94c1a
+Python/pysystemtap.o: $(srcdir)/Python/pysystemtap.d Python/ceval.o
1c94c1a
+	dtrace -o $@ $(DFLAGS) -C -G -s $(srcdir)/Python/pysystemtap.d Python/ceval.o
c969609
+
c969609
 ############################################################################
c969609
 # Header files
c969609
 
78727de
@@ -1345,6 +1354,7 @@ clean: pycremoval
78727de
 	-rm -f Lib/lib2to3/*Grammar*.pickle
Matej Stuchlik abb2ff8
 	-rm -f Programs/_testembed Programs/_freeze_importlib
Robert Kuska e248727
	-rm -rf build
1c94c1a
+	-rm -f $(srcdir)/Python/pysystemtap.h
1c94c1a
 
1c94c1a
 profile-removal:
1c94c1a
 	find . -name '*.gc??' -exec rm -f {} ';'
78727de
diff -up Python-3.3.0rc2/pyconfig.h.in.systemtap Python-3.3.0rc2/pyconfig.h.in
78727de
--- Python-3.3.0rc2/pyconfig.h.in.systemtap	2012-09-09 05:11:14.000000000 -0400
78727de
+++ Python-3.3.0rc2/pyconfig.h.in	2012-09-10 09:17:21.120511781 -0400
1c94c1a
@@ -1306,6 +1306,9 @@
1c94c1a
 /* Define if you want to compile in Python-specific mallocs */
1c94c1a
 #undef WITH_PYMALLOC
1c94c1a
 
1c94c1a
+/* Define if you want to compile in SystemTap support */
1c94c1a
+#undef WITH_SYSTEMTAP
1c94c1a
+
1c94c1a
 /* Define if you want to compile in rudimentary thread support */
1c94c1a
 #undef WITH_THREAD
1c94c1a
 
78727de
diff -up Python-3.3.0rc2/Python/ceval.c.systemtap Python-3.3.0rc2/Python/ceval.c
78727de
--- Python-3.3.0rc2/Python/ceval.c.systemtap	2012-09-09 05:11:12.000000000 -0400
78727de
+++ Python-3.3.0rc2/Python/ceval.c	2012-09-10 09:17:21.122511781 -0400
1c94c1a
@@ -18,6 +18,8 @@
c969609
 
c969609
 #include <ctype.h>
c969609
 
1c94c1a
+#include "ceval_systemtap.h"
c969609
+
c969609
 #ifndef WITH_TSC
c969609
 
c969609
 #define READ_TIMESTAMP(var)
1c94c1a
@@ -1160,6 +1162,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int
1c94c1a
         }
1c94c1a
     }
c969609
 
1c94c1a
+    if (PYTHON_FUNCTION_ENTRY_ENABLED()) {
1c94c1a
+        systemtap_function_entry(f);
1c94c1a
+    }
1c94c1a
+
1c94c1a
     co = f->f_code;
1c94c1a
     names = co->co_names;
1c94c1a
     consts = co->co_consts;
1c94c1a
@@ -3077,6 +3083,11 @@ fast_yield:
7eb10c8
 
1c94c1a
     /* pop frame */
1c94c1a
 exit_eval_frame:
1c94c1a
+
1c94c1a
+    if (PYTHON_FUNCTION_RETURN_ENABLED()) {
1c94c1a
+        systemtap_function_return(f);
1c94c1a
+    }
1c94c1a
+
1c94c1a
     Py_LeaveRecursiveCall();
f5250ec
     f->f_executing = 0;
1c94c1a
     tstate->frame = f->f_back;
78727de
diff -up Python-3.3.0rc2/Python/ceval_systemtap.h.systemtap Python-3.3.0rc2/Python/ceval_systemtap.h
78727de
--- Python-3.3.0rc2/Python/ceval_systemtap.h.systemtap	2012-09-10 09:17:21.122511781 -0400
78727de
+++ Python-3.3.0rc2/Python/ceval_systemtap.h	2012-09-10 09:17:21.122511781 -0400
1c94c1a
@@ -0,0 +1,86 @@
1c94c1a
+/*
1c94c1a
+  Support for SystemTap static markers  
1c94c1a
+*/
1c94c1a
+
1c94c1a
+#ifdef WITH_SYSTEMTAP
1c94c1a
+
1c94c1a
+#include "pysystemtap.h"
1c94c1a
+
1c94c1a
+/*
1c94c1a
+  A struct to hold all of the information gathered when one of the traceable
1c94c1a
+  markers is triggered
1c94c1a
+*/
c969609
+struct frame_marker_info
c969609
+{
1c94c1a
+    PyObject *filename_obj;
1c94c1a
+    PyObject *funcname_obj;
1c94c1a
+    const char *filename;
1c94c1a
+    const char *funcname;
1c94c1a
+    int lineno;
c969609
+};
c969609
+
c969609
+static void
c969609
+get_frame_marker_info(PyFrameObject *f, struct frame_marker_info *fmi)
c969609
+{
1c94c1a
+    PyObject *ptype;
1c94c1a
+    PyObject *pvalue;
1c94c1a
+    PyObject *ptraceback;
1c94c1a
+
1c94c1a
+    PyErr_Fetch(&ptype, &pvalue, &ptraceback);
1c94c1a
+
1c94c1a
+    fmi->filename_obj = PyUnicode_EncodeFSDefault(f->f_code->co_filename);
1c94c1a
+    if (fmi->filename_obj) {
1c94c1a
+        fmi->filename = PyBytes_AsString(fmi->filename_obj);
1c94c1a
+    } else {
1c94c1a
+        fmi->filename = NULL;
1c94c1a
+    }
1c94c1a
+
1c94c1a
+    fmi->funcname_obj = PyUnicode_AsUTF8String(f->f_code->co_name);
1c94c1a
+    if (fmi->funcname_obj) {
1c94c1a
+        fmi->funcname = PyBytes_AsString(fmi->funcname_obj);
1c94c1a
+    } else {
1c94c1a
+        fmi->funcname = NULL;
1c94c1a
+    }
1c94c1a
+
1c94c1a
+    fmi->lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
1c94c1a
+
1c94c1a
+    PyErr_Restore(ptype, pvalue, ptraceback);
1c94c1a
+
c969609
+}
c969609
+
c969609
+static void
c969609
+release_frame_marker_info(struct frame_marker_info *fmi)
c969609
+{
1c94c1a
+    Py_XDECREF(fmi->filename_obj);
1c94c1a
+    Py_XDECREF(fmi->funcname_obj);
c969609
+}
c969609
+
c969609
+static void
1c94c1a
+systemtap_function_entry(PyFrameObject *f)
c969609
+{
1c94c1a
+    struct frame_marker_info fmi;
1c94c1a
+    get_frame_marker_info(f, &fmi);
1c94c1a
+    PYTHON_FUNCTION_ENTRY(fmi.filename, fmi.funcname, fmi.lineno, f);
1c94c1a
+    release_frame_marker_info(&fmi);
c969609
+}
c969609
+
c969609
+static void
1c94c1a
+systemtap_function_return(PyFrameObject *f)
c969609
+{
1c94c1a
+    struct frame_marker_info fmi;
1c94c1a
+    get_frame_marker_info(f, &fmi);
1c94c1a
+    PYTHON_FUNCTION_RETURN(fmi.filename, fmi.funcname, fmi.lineno, f);
1c94c1a
+    release_frame_marker_info(&fmi);
c969609
+}
c969609
+
1c94c1a
+#else /* #ifdef WITH_SYSTEMTAP */
1c94c1a
+
1c94c1a
+/*
1c94c1a
+  When configured --without-systemtap, everything compiles away to nothing:
1c94c1a
+*/
1c94c1a
+#define PYTHON_FUNCTION_ENTRY_ENABLED() 0
1c94c1a
+#define PYTHON_FUNCTION_RETURN_ENABLED() 0
1c94c1a
+#define systemtap_function_entry(f)
1c94c1a
+#define systemtap_function_return(f)
1c94c1a
+
1c94c1a
+#endif
78727de
diff -up Python-3.3.0rc2/Python/pysystemtap.d.systemtap Python-3.3.0rc2/Python/pysystemtap.d
78727de
--- Python-3.3.0rc2/Python/pysystemtap.d.systemtap	2012-09-10 09:17:21.122511781 -0400
78727de
+++ Python-3.3.0rc2/Python/pysystemtap.d	2012-09-10 09:17:21.122511781 -0400
1c94c1a
@@ -0,0 +1,4 @@
1c94c1a
+provider python {
1c94c1a
+    probe function__entry(const char *, const char *, int, PyFrameObject *);
1c94c1a
+    probe function__return(const char *, const char *, int, PyFrameObject *);
1c94c1a
+};