~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/strace.py

  • Committer: Ian Clatworthy
  • Date: 2009-09-09 15:30:59 UTC
  • mto: (4634.37.2 prepare-2.0)
  • mto: This revision was merged to the branch mainline in revision 4689.
  • Revision ID: ian.clatworthy@canonical.com-20090909153059-sb038agvd38ci2q8
more link fixes in the User Guide

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 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):
45
46
    # capture strace output to a file
46
47
    log_file = tempfile.NamedTemporaryFile()
47
48
    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_cmd.append('-f')
54
 
    # need to catch both stdout and stderr to work around
55
 
    # bug 627208
 
53
        strace_args.append('-f')
56
54
    proc = subprocess.Popen(strace_cmd,
57
55
                            stdout=subprocess.PIPE,
58
 
                            stderr=err_file.fileno())
 
56
                            stderr=subprocess.STDOUT)
59
57
    # Wait for strace to attach
60
58
    attached_notice = proc.stdout.readline()
61
59
    # Run the function to strace
67
65
    log_file.seek(0)
68
66
    log = log_file.read()
69
67
    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"
 
68
    return result, StraceResult(log)
83
69
 
84
70
 
85
71
class StraceResult(object):
86
72
    """The result of stracing a function."""
87
73
 
88
 
    def __init__(self, raw_log, err_messages):
 
74
    def __init__(self, raw_log):
89
75
        """Create a StraceResult.
90
76
 
91
77
        :param raw_log: The output that strace created.
92
78
        """
93
79
        self.raw_log = raw_log
94
 
        self.err_messages = err_messages
95
 
 
96
 
 
 
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()