Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python function tracing for both python2 and python3 #680

Closed

Conversation

honggyukim
Copy link
Collaborator

@honggyukim honggyukim commented Feb 23, 2019

This PR is based on review/python-tracing-v3.

Related issue: #436

@namhyung
Copy link
Owner

I'd like to see python3 support for script first. And I prefer keeping single file both for python2 and python3 if possible.

@honggyukim
Copy link
Collaborator Author

Sure. I will do that.

namhyung and others added 12 commits October 7, 2019 14:30
The trace_python module implement 'trace' function to be set by sys.settrace().
It will call uftrace (i.e. libmcount) to save trace info along with the
interpreter.  But C extension module cannot be used by it so also added
misc/uftrace.py module just as a loader.

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
It needs to map python function name to an address so that uftrace can
deal with it like C functions.  As lowest address won't be used by
native programs, just use simple counter (from 1) to save symbol
address.  The symbol table will be written as "python-symtab.sym" in the
data directory.

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
If it finds the python symbol table, it'll add a map so that uftrace can
find its address.  For now, it assumes having up to 4k symbols.

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
  $ cat hello.py
  #!/usr/bin/env python2
  def b(arg):
      return arg+1
  def a():
      return b(0)
  print a()

  $ u --python hello.py
  1
  # DURATION     TID     FUNCTION
              [   560] | <module>() {
              [   560] |   a() {
     0.958 us [   560] |     b();
     3.751 us [   560] |   } /* a */
    10.769 us [   560] | } /* <module> */
     6.969 us [   560] | _remove();
     2.099 us [   560] | _remove();

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
Convert module initializer from "<module>" to "<module:name>".

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
misc/uftrace.py indirectly executes the given python script, but it
shouldn't be passed as the first argument in sys.argv.

This patch also changes execfile() to exec() to make it work in both
python2 and python3.

In "What’s New In Python 3.0":
"Removed execfile(). Instead of execfile(fn) use exec(open(fn).read())."

Link: http://docs.python.org/3.3/whatsnew/3.0.html?highlight=execfile#builtins
Signed-off-by: Honggyu Kim <honggyu.kp@gmail.com>
Since python-config provides a way to get the correct cflags and
ldflags, it's better to use it to avoid using version specific header path.

Signed-off-by: Honggyu Kim <honggyu.kp@gmail.com>
In order to support both python2 and python3, this patch adds a new
"trace-python2.c" file, which contains only python2 specific code.

"trace-python.c" only contains common code for both python2 and python3.

Signed-off-by: Honggyu Kim <honggyu.kp@gmail.com>
This patch changes the name of python2 shared object from
"python_trace.so" to "python_trace2.so".

This is a preparation phase for additional python3 support.

Signed-off-by: Honggyu Kim <honggyu.kp@gmail.com>
This adds a new file "misc/trace-python3.c" to support python3 script
tracing.  It only contains the code so build script change will be
followed in the next commit.

Signed-off-by: Honggyu Kim <honggyu.kp@gmail.com>
This patch adds a build rule to generate "trace-python3.so" for python3
tracing support.

Signed-off-by: Honggyu Kim <honggyu.kp@gmail.com>
misc/uftrace.py is a middle layer before calling the target python
script, so it can check the python version before tracing.

Based on the python version, it can decide whether to import
"trace_python2.so" or "trace_python3.so".

Signed-off-by: Honggyu Kim <honggyu.kp@gmail.com>
@honggyukim honggyukim force-pushed the review/python3-tracing-v1 branch from eb87857 to c9f4e80 Compare October 7, 2019 07:52
Signed-off-by: Honggyu Kim <honggyu.kp@gmail.com>
@honggyukim honggyukim closed this Oct 7, 2019
@honggyukim
Copy link
Collaborator Author

This PR isn't properly updated so opened a new PR in #934.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants