/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: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

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