~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/strace.py

(vila) Forbid more operations on ReadonlyTransportDecorator (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009, 2010 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
17
17
 
18
18
"""Support for running strace against the current process."""
19
19
 
20
 
import errno
 
20
from __future__ import absolute_import
 
21
 
21
22
import os
22
23
import signal
23
24
import subprocess
24
25
import tempfile
25
26
 
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
 
27
from bzrlib import errors
29
28
 
30
29
 
31
30
def strace(function, *args, **kwargs):
46
45
    # capture strace output to a file
47
46
    log_file = tempfile.NamedTemporaryFile()
48
47
    log_file_fd = log_file.fileno()
 
48
    err_file = tempfile.NamedTemporaryFile()
49
49
    pid = os.getpid()
50
50
    # start strace
51
51
    strace_cmd = ['strace', '-r', '-tt', '-p', str(pid), '-o', log_file.name]
52
52
    if follow_children:
53
 
        strace_args.append('-f')
 
53
        strace_cmd.append('-f')
 
54
    # need to catch both stdout and stderr to work around
 
55
    # bug 627208
54
56
    proc = subprocess.Popen(strace_cmd,
55
57
                            stdout=subprocess.PIPE,
56
 
                            stderr=subprocess.STDOUT)
 
58
                            stderr=err_file.fileno())
57
59
    # Wait for strace to attach
58
60
    attached_notice = proc.stdout.readline()
59
61
    # Run the function to strace
65
67
    log_file.seek(0)
66
68
    log = log_file.read()
67
69
    log_file.close()
68
 
    return result, StraceResult(log)
 
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"
69
83
 
70
84
 
71
85
class StraceResult(object):
72
86
    """The result of stracing a function."""
73
87
 
74
 
    def __init__(self, raw_log):
 
88
    def __init__(self, raw_log, err_messages):
75
89
        """Create a StraceResult.
76
90
 
77
91
        :param raw_log: The output that strace created.
78
92
        """
79
93
        self.raw_log = raw_log
80
 
 
81
 
 
82
 
class _StraceFeature(Feature):
83
 
 
84
 
    def _probe(self):
85
 
        try:
86
 
            proc = subprocess.Popen(['strace'],
87
 
                stderr=subprocess.PIPE,
88
 
                stdout=subprocess.PIPE)
89
 
            proc.communicate()
90
 
            return True
91
 
        except OSError, e:
92
 
            if e.errno == errno.ENOENT:
93
 
                # strace is not installed
94
 
                return False
95
 
            else:
96
 
                raise
97
 
 
98
 
    def feature_name(self):
99
 
        return 'strace'
100
 
 
101
 
StraceFeature = _StraceFeature()
 
94
        self.err_messages = err_messages
 
95
 
 
96