/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5387.2.7 by John Arbash Meinel
Merge bzr.dev 5444 to resolve some small text conflicts.
1
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
17
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
18
"""Support for running strace against the current process."""
19
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
20
import os
21
import signal
22
import subprocess
23
import tempfile
24
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
25
from . import errors
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
26
27
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
28
def strace(function, *args, **kwargs):
29
    """Invoke strace on function.
30
2367.1.9 by Robert Collins
Review feedback.
31
    :return: a tuple: function-result, a StraceResult.
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
32
    """
2566.3.4 by Vincent Ladeuil
Take Martin and Robert comments into account.
33
    return strace_detailed(function, args, kwargs)
34
35
36
def strace_detailed(function, args, kwargs, follow_children=True):
2566.3.1 by Vincent Ladeuil
Fix #102019 by not asking strace to follow children forks during tests.
37
    # FIXME: strace is buggy
38
    # (https://bugs.launchpad.net/ubuntu/+source/strace/+bug/103133) and the
39
    # test suite hangs if the '-f' is given to strace *and* more than one
2566.3.4 by Vincent Ladeuil
Take Martin and Robert comments into account.
40
    # thread is running. Using follow_children=False allows the test suite to
41
    # disable fork following to work around the bug.
42
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
43
    # capture strace output to a file
2394.1.1 by Andrew Bennetts
Wait for strace to attach before running the function we want to trace.
44
    log_file = tempfile.NamedTemporaryFile()
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
45
    err_file = tempfile.NamedTemporaryFile()
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
46
    pid = os.getpid()
47
    # start strace
2566.3.1 by Vincent Ladeuil
Fix #102019 by not asking strace to follow children forks during tests.
48
    strace_cmd = ['strace', '-r', '-tt', '-p', str(pid), '-o', log_file.name]
2566.3.2 by Vincent Ladeuil
Fix typo, add comment, following Martin's review.
49
    if follow_children:
6379.6.3 by Jelmer Vernooij
Use absolute_import.
50
        strace_cmd.append('-f')
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
51
    # need to catch both stdout and stderr to work around
52
    # bug 627208
2566.3.1 by Vincent Ladeuil
Fix #102019 by not asking strace to follow children forks during tests.
53
    proc = subprocess.Popen(strace_cmd,
54
                            stdout=subprocess.PIPE,
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
55
                            stderr=err_file.fileno())
2394.1.1 by Andrew Bennetts
Wait for strace to attach before running the function we want to trace.
56
    # Wait for strace to attach
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
57
    proc.stdout.readline()
2394.1.1 by Andrew Bennetts
Wait for strace to attach before running the function we want to trace.
58
    # Run the function to strace
2367.1.9 by Robert Collins
Review feedback.
59
    result = function(*args, **kwargs)
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
60
    # stop strace
61
    os.kill(proc.pid, signal.SIGQUIT)
62
    proc.communicate()
63
    # grab the log
64
    log_file.seek(0)
65
    log = log_file.read()
66
    log_file.close()
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
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):
7143.15.2 by Jelmer Vernooij
Run autopep8.
78
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
79
    _fmt = "strace failed: %(err_messages)s"
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
80
81
82
class StraceResult(object):
83
    """The result of stracing a function."""
84
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
85
    def __init__(self, raw_log, err_messages):
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
86
        """Create a StraceResult.
87
88
        :param raw_log: The output that strace created.
89
        """
90
        self.raw_log = raw_log
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
91
        self.err_messages = err_messages