~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/strace.py

  • Committer: John Arbash Meinel
  • Date: 2007-05-03 16:42:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2481.
  • Revision ID: john@arbash-meinel.com-20070503164230-y0411liq6w3bphj0
Vastly improve bundle install performance by only validating the bundle one time

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
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
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
"""Support for running strace against the current process."""
19
19
 
20
 
from __future__ import absolute_import
21
 
 
 
20
import errno
22
21
import os
23
22
import signal
24
23
import subprocess
25
24
import tempfile
26
25
 
27
 
from bzrlib import errors
 
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
28
29
 
29
30
 
30
31
def strace(function, *args, **kwargs):
32
33
 
33
34
    :return: a tuple: function-result, a StraceResult.
34
35
    """
35
 
    return strace_detailed(function, args, kwargs)
36
 
 
37
 
 
38
 
def strace_detailed(function, args, kwargs, follow_children=True):
39
 
    # FIXME: strace is buggy
40
 
    # (https://bugs.launchpad.net/ubuntu/+source/strace/+bug/103133) and the
41
 
    # test suite hangs if the '-f' is given to strace *and* more than one
42
 
    # thread is running. Using follow_children=False allows the test suite to
43
 
    # disable fork following to work around the bug.
44
 
 
45
36
    # capture strace output to a file
46
37
    log_file = tempfile.NamedTemporaryFile()
47
38
    log_file_fd = log_file.fileno()
48
 
    err_file = tempfile.NamedTemporaryFile()
49
39
    pid = os.getpid()
50
40
    # start strace
51
 
    strace_cmd = ['strace', '-r', '-tt', '-p', str(pid), '-o', log_file.name]
52
 
    if follow_children:
53
 
        strace_cmd.append('-f')
54
 
    # need to catch both stdout and stderr to work around
55
 
    # bug 627208
56
 
    proc = subprocess.Popen(strace_cmd,
57
 
                            stdout=subprocess.PIPE,
58
 
                            stderr=err_file.fileno())
 
41
    proc = subprocess.Popen(['strace',
 
42
        '-f', '-r', '-tt', '-p', str(pid), '-o', log_file.name
 
43
        ],
 
44
        stdout=subprocess.PIPE,
 
45
        stderr=subprocess.STDOUT)
59
46
    # Wait for strace to attach
60
47
    attached_notice = proc.stdout.readline()
61
48
    # Run the function to strace
67
54
    log_file.seek(0)
68
55
    log = log_file.read()
69
56
    log_file.close()
70
 
    # and stderr
71
 
    err_file.seek(0)
72
 
    err_messages = err_file.read()
73
 
    err_file.close()
74
 
    # and read any errors
75
 
    if err_messages.startswith("attach: ptrace(PTRACE_ATTACH,"):
76
 
        raise StraceError(err_messages=err_messages)
77
 
    return result, StraceResult(log, err_messages)
78
 
 
79
 
 
80
 
class StraceError(errors.BzrError):
81
 
    
82
 
    _fmt = "strace failed: %(err_messages)s"
 
57
    return result, StraceResult(log)
83
58
 
84
59
 
85
60
class StraceResult(object):
86
61
    """The result of stracing a function."""
87
62
 
88
 
    def __init__(self, raw_log, err_messages):
 
63
    def __init__(self, raw_log):
89
64
        """Create a StraceResult.
90
65
 
91
66
        :param raw_log: The output that strace created.
92
67
        """
93
68
        self.raw_log = raw_log
94
 
        self.err_messages = err_messages
95
 
 
96
 
 
 
69
 
 
70
 
 
71
class _StraceFeature(Feature):
 
72
 
 
73
    def _probe(self):
 
74
        try:
 
75
            proc = subprocess.Popen(['strace'],
 
76
                stderr=subprocess.PIPE,
 
77
                stdout=subprocess.PIPE)
 
78
            proc.communicate()
 
79
            return True
 
80
        except OSError, e:
 
81
            if e.errno == errno.ENOENT:
 
82
                # strace is not installed
 
83
                return False
 
84
            else:
 
85
                raise
 
86
 
 
87
    def feature_name(self):
 
88
        return 'strace'
 
89
 
 
90
StraceFeature = _StraceFeature()