/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/strace.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

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
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 . 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):
44
45
 
45
46
    # capture strace output to a file
46
47
    log_file = tempfile.NamedTemporaryFile()
47
 
    err_file = tempfile.NamedTemporaryFile()
 
48
    log_file_fd = log_file.fileno()
48
49
    pid = os.getpid()
49
50
    # start strace
50
51
    strace_cmd = ['strace', '-r', '-tt', '-p', str(pid), '-o', log_file.name]
51
52
    if follow_children:
52
 
        strace_cmd.append('-f')
53
 
    # need to catch both stdout and stderr to work around
54
 
    # bug 627208
 
53
        strace_args.append('-f')
55
54
    proc = subprocess.Popen(strace_cmd,
56
55
                            stdout=subprocess.PIPE,
57
 
                            stderr=err_file.fileno())
 
56
                            stderr=subprocess.STDOUT)
58
57
    # Wait for strace to attach
59
 
    proc.stdout.readline()
 
58
    attached_notice = proc.stdout.readline()
60
59
    # Run the function to strace
61
60
    result = function(*args, **kwargs)
62
61
    # stop strace
66
65
    log_file.seek(0)
67
66
    log = log_file.read()
68
67
    log_file.close()
69
 
    # and stderr
70
 
    err_file.seek(0)
71
 
    err_messages = err_file.read()
72
 
    err_file.close()
73
 
    # and read any errors
74
 
    if err_messages.startswith("attach: ptrace(PTRACE_ATTACH,"):
75
 
        raise StraceError(err_messages=err_messages)
76
 
    return result, StraceResult(log, err_messages)
77
 
 
78
 
 
79
 
class StraceError(errors.BzrError):
80
 
 
81
 
    _fmt = "strace failed: %(err_messages)s"
 
68
    return result, StraceResult(log)
82
69
 
83
70
 
84
71
class StraceResult(object):
85
72
    """The result of stracing a function."""
86
73
 
87
 
    def __init__(self, raw_log, err_messages):
 
74
    def __init__(self, raw_log):
88
75
        """Create a StraceResult.
89
76
 
90
77
        :param raw_log: The output that strace created.
91
78
        """
92
79
        self.raw_log = raw_log
93
 
        self.err_messages = err_messages
 
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()