bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
| 
2367.1.7
by Robert Collins
 Added ``bzrlib.strace.strace`` which will strace a single callable and  | 
1  | 
# Copyright (C) 2007 Canonical Ltd
 | 
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
 | 
|
16  | 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 | 
|
17  | 
||
18  | 
"""Support for running strace against the current process."""
 | 
|
19  | 
||
20  | 
import os  | 
|
21  | 
import signal  | 
|
22  | 
import subprocess  | 
|
23  | 
import tempfile  | 
|
24  | 
||
25  | 
# this is currently test-focused, so importing bzrlib.tests is ok. We might
 | 
|
26  | 
# want to move feature to its own module though.
 | 
|
27  | 
from bzrlib.tests import Feature  | 
|
28  | 
||
| 
2367.1.9
by Robert Collins
 Review feedback.  | 
29  | 
|
| 
2367.1.7
by Robert Collins
 Added ``bzrlib.strace.strace`` which will strace a single callable and  | 
30  | 
def strace(function, *args, **kwargs):  | 
31  | 
"""Invoke strace on function.  | 
|
32  | 
||
| 
2367.1.9
by Robert Collins
 Review feedback.  | 
33  | 
    :return: a tuple: function-result, a StraceResult.
 | 
| 
2367.1.7
by Robert Collins
 Added ``bzrlib.strace.strace`` which will strace a single callable and  | 
34  | 
    """
 | 
35  | 
    # capture strace output to a file
 | 
|
36  | 
log_file = tempfile.TemporaryFile()  | 
|
37  | 
log_file_fd = log_file.fileno()  | 
|
38  | 
pid = os.getpid()  | 
|
39  | 
    # start strace
 | 
|
40  | 
proc = subprocess.Popen(['strace',  | 
|
41  | 
'-f', '-r', '-tt', '-p', str(pid),  | 
|
42  | 
        ],
 | 
|
43  | 
stderr=log_file_fd,  | 
|
44  | 
stdout=log_file_fd)  | 
|
45  | 
    # TODO? confirm its started (test suite should be sufficient)
 | 
|
46  | 
    # (can loop on proc.pid, but that may not indicate started and attached.)
 | 
|
| 
2367.1.9
by Robert Collins
 Review feedback.  | 
47  | 
result = function(*args, **kwargs)  | 
| 
2367.1.7
by Robert Collins
 Added ``bzrlib.strace.strace`` which will strace a single callable and  | 
48  | 
    # stop strace
 | 
49  | 
os.kill(proc.pid, signal.SIGQUIT)  | 
|
50  | 
proc.communicate()  | 
|
51  | 
    # grab the log
 | 
|
52  | 
log_file.seek(0)  | 
|
53  | 
log = log_file.read()  | 
|
54  | 
log_file.close()  | 
|
| 
2367.1.9
by Robert Collins
 Review feedback.  | 
55  | 
return result, StraceResult(log)  | 
| 
2367.1.7
by Robert Collins
 Added ``bzrlib.strace.strace`` which will strace a single callable and  | 
56  | 
|
57  | 
||
58  | 
class StraceResult(object):  | 
|
59  | 
"""The result of stracing a function."""  | 
|
60  | 
||
61  | 
def __init__(self, raw_log):  | 
|
62  | 
"""Create a StraceResult.  | 
|
63  | 
||
64  | 
        :param raw_log: The output that strace created.
 | 
|
65  | 
        """
 | 
|
66  | 
self.raw_log = raw_log  | 
|
67  | 
||
68  | 
||
69  | 
class _StraceFeature(Feature):  | 
|
70  | 
||
71  | 
def _probe(self):  | 
|
72  | 
try:  | 
|
73  | 
proc = subprocess.Popen(['strace'],  | 
|
74  | 
stderr=subprocess.PIPE,  | 
|
75  | 
stdout=subprocess.PIPE)  | 
|
76  | 
proc.communicate()  | 
|
77  | 
return True  | 
|
78  | 
except OSError, e:  | 
|
79  | 
if e.errno == errno.ENOENT:  | 
|
80  | 
                # strace is not installed
 | 
|
81  | 
return False  | 
|
82  | 
else:  | 
|
83  | 
                raise
 | 
|
84  | 
||
85  | 
def feature_name(self):  | 
|
86  | 
return 'strace'  | 
|
87  | 
||
88  | 
StraceFeature = _StraceFeature()  |