DTrace patch for Python 2.7.x and 3.x

DTrace patch for Python 2.7.x and 3.x

Última Actualización: 21 de septiembre de 2015

https://www.jcea.es/artic/python_dtrace.htm

You can follow this work in the Python bugtracker on issue 13405 (original work on issue 4111).

How to get the patch

You can clone my mercurial repository. Interesting branches are dtrace-issue13405_2.7dtrace-issue13405_3.4dtrace-issue13405_3.5 and dtrace-issue13405 (future 3.6). This configuration is recommended if you plan to contribute.

If you just want to compile your Python interpreter with dtrace patch applied, you only need to apply the matching patch over a pristine copy of the canonical Python source tree:

How to compile and test

Do "autoconf".

Add "--with-dtrace" to your "configure" command line and compile as usual. If you have previously compiled Python in the same directory, remember to do "make distclean" first.

When done, you should run the complete testsuite. If you just want to run dtrace tests, do

# LD_LIBRARY_PATH=`pwd` ./python Lib/test/regrtest.py -v test_dtrace.py

You should get something like

[1/1] test_dtrace
test_function_entry_return (test.test_dtrace.DTraceTestsNormal) ... ok
test_garbage_collection (test.test_dtrace.DTraceTestsNormal) ... ok
test_instance_creation_destruction (test.test_dtrace.DTraceTestsNormal) ... ok
test_line (test.test_dtrace.DTraceTestsNormal) ... ok
test_stack (test.test_dtrace.DTraceTestsNormal) ... ok
test_unicode_function_entry_return (test.test_dtrace.DTraceTestsNormal) ... ok
test_unicode_stack (test.test_dtrace.DTraceTestsNormal) ... ok
test_verify_opcodes (test.test_dtrace.DTraceTestsNormal) ... ok

----------------------------------------------------------------------
Ran 8 tests in 5.335s

OK
test_function_entry_return (test.test_dtrace.DTraceTestsOptimize) ... ok
test_garbage_collection (test.test_dtrace.DTraceTestsOptimize) ... ok
test_instance_creation_destruction (test.test_dtrace.DTraceTestsOptimize) ... ok
test_line (test.test_dtrace.DTraceTestsOptimize) ... ok
test_stack (test.test_dtrace.DTraceTestsOptimize) ... ok
test_unicode_function_entry_return (test.test_dtrace.DTraceTestsOptimize) ... ok
test_unicode_stack (test.test_dtrace.DTraceTestsOptimize) ... ok
test_verify_opcodes (test.test_dtrace.DTraceTestsOptimize) ... ok

----------------------------------------------------------------------
Ran 8 tests in 5.455s

OK
1 test OK.

The first set of tests run under non-optimizing Python, while the second set run under optimizing Python (-O).

Documentation

: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
accessible to external scripts.

The probes are exported through 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 as 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 changes the execution line. Parameters are the
   same as 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 creations.

.. opcode:: instance-new-done (arg0, arg1)

   Fires when an object instanciation finishes. Parameters are the same
   as in ``instance-new-done``.

   The probe is not fired for most C code object creations.

.. opcode:: instance-delete-start (arg0, arg1)

   Fires when an object instance is going to be destroyed. Parameters
   are the same as in ``instance-new-done``.

   The probe is not fired for most C code object destructions.

.. opcode:: instance-delete-done (arg0, arg1)

   Fires when an object instance has been destroyed. parameters are the
   same as 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 destructions.

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
augmented 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 spent 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();
  }
时间: 2024-11-05 13:46:02

DTrace patch for Python 2.7.x and 3.x的相关文章

应用安全 - 编程语言 | 框架 - PHP - Djiango - 漏洞 -汇总

CVE-2007-0404 Date August 16, 2006 类型Filename validation issue in translation framework. Full description 影响范围 CVE-2007-0405 Date January 21, 2007 类型 Apparent “caching” of authenticated user. Full description Issues under Django’s security process¶Al

Python Monkey patch猴子补丁

monkey patch (猴子补丁)   用来在运行时动态修改已有的代码,而不需要修改原始代码. 简单的monkey patch 实现:[python] #coding=utf-8 def originalFunc():     print 'this is original function!'      def modifiedFunc():     modifiedFunc=1     print 'this is modified function!'      def main():

python中的猴子补丁Monkey Patch

python中的猴子补丁Monkey Patch 什么是猴子补丁 the term monkey patch only refers to dynamic modifications of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as desired

python的猴子补丁monkey patch

monkey patch指的是在运行时动态替换,一般是在startup的时候. 用过gevent就会知道,会在最开头的地方gevent.monkey.patch_all();把标准库中的thread/socket等给替换掉.这样我们在后面使用socket的时候可以跟平常一样使用,无需修改任何代码,但是它变成非阻塞的了. 一个比较实用的例子,很多代码用到 import json,后来发现ujson性能更高,如果觉得把每个文件的import json 改成 import ujson as json成

强劲的Linux Trace工具:bpftrace (DTrace 2.0) for Linux 2

译者:?姜弋 译者注:原作者是大名鼎鼎的性能分析专家:Brendan Gregg,现在工作在Netflix,之前工作在Sun,在Sun公司的时候,他就做了大量的性能分析和tracing相关的工作,在Sun的Solaris上存在一种传说中的性能分析和Debug神器: Dtrace,然而,可惜的是,在我们现在的Linux操作系统上并没有Dtrace神器(这可能是因为Dtrace是从Soloris操作系统的衍生品无法迁移到别的操作系统上),Brendan Gregg 在Netflix后,继续利用他的业

python中requests的用法

一个最简单的demo: html = requests.get('http://www.cnblogs.com/liaocheng/p/5215225.html') return html.text 这个函数也可以设置提交参数和表头,当然,也有post版本. 以下为详细: 发送请求 使用Requests发送网络请求非常简单. 一开始要导入Requests模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取Github的公共时间线 >>&

读《我为什么从python转向go》的一些感受

一开始我以为是一篇2013年的老帖子,没想到竟然是2015年.不懂Python不要乱喷啊.你直接说"我不懂Python,我也不愿意维护前任写的糟糕代码,我Go牛B,所以我要重构一遍!"我到觉得你智商正常点.. 我觉得go不错,但是如果不是特定领域开发,没有足够多成熟稳定的库仍然很麻烦的事情. http://www.jianshu.com/p/afa14e631930 http://www.lupaworld.com/article-254456-1.html python虽然很强大,但

linux下多版本python环境配置

1. 依赖pyenv安装使用git # yum install git -y # yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel 2. 创建用户python # useradd python # passwd python 3. 使用python用户登录 su – python 4. 开始部署pyenv pyenv安装方式: pyen

Python模拟数据工具哪些比较好用

今天给大家推荐两款基本的Python模拟数据工具:mock和pytest monkeypatch. 为什么要模拟数据? 我们的应用中有一些部分需要依赖外部的库或对象.为了隔离开这部分,我们需要代替这些外部依赖,因而就用到了模拟数据.我们模拟外部的API来产生特定的行为,比如说返回符合之前定义的恰当的返回值. 模拟函数 我们有一个function.py的模块: 然后我们来看下如何将其与Mock库结合使用的: 这里发生了什么?1-4行是为了兼容python 2和3来引入的代码,在python 3中m