1
# Copyright (C) 2007 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Support for running strace against the current process."""
26
# this is currently test-focused, so importing bzrlib.tests is ok. We might
27
# want to move feature to its own module though.
28
from bzrlib.tests import Feature
31
def strace(function, *args, **kwargs):
32
"""Invoke strace on function.
34
:return: a tuple: function-result, a StraceResult.
36
return strace_detailed(function, args, kwargs)
39
def strace_detailed(function, args, kwargs, follow_children=True):
40
# FIXME: strace is buggy
41
# (https://bugs.launchpad.net/ubuntu/+source/strace/+bug/103133) and the
42
# test suite hangs if the '-f' is given to strace *and* more than one
43
# thread is running. Using follow_children=False allows the test suite to
44
# disable fork following to work around the bug.
46
# capture strace output to a file
47
log_file = tempfile.NamedTemporaryFile()
48
log_file_fd = log_file.fileno()
51
strace_cmd = ['strace', '-r', '-tt', '-p', str(pid), '-o', log_file.name]
53
strace_args.append('-f')
54
proc = subprocess.Popen(strace_cmd,
55
stdout=subprocess.PIPE,
56
stderr=subprocess.STDOUT)
57
# Wait for strace to attach
58
attached_notice = proc.stdout.readline()
59
# Run the function to strace
60
result = function(*args, **kwargs)
62
os.kill(proc.pid, signal.SIGQUIT)
68
return result, StraceResult(log)
71
class StraceResult(object):
72
"""The result of stracing a function."""
74
def __init__(self, raw_log):
75
"""Create a StraceResult.
77
:param raw_log: The output that strace created.
79
self.raw_log = raw_log
82
class _StraceFeature(Feature):
86
proc = subprocess.Popen(['strace'],
87
stderr=subprocess.PIPE,
88
stdout=subprocess.PIPE)
92
if e.errno == errno.ENOENT:
93
# strace is not installed
98
def feature_name(self):
101
StraceFeature = _StraceFeature()