~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/strace.py

  • Committer: Martin Pool
  • Date: 2007-04-04 06:17:31 UTC
  • mto: This revision was merged to the branch mainline in revision 2397.
  • Revision ID: mbp@sourcefrog.net-20070404061731-tt2xrzllqhbodn83
Contents of TODO file moved into bug tracker

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Support for running strace against the current process."""
19
19
 
20
 
import errno
21
20
import os
22
21
import signal
23
22
import subprocess
33
32
 
34
33
    :return: a tuple: function-result, a StraceResult.
35
34
    """
36
 
    return strace_detailed(function, args, kwargs)
37
 
 
38
 
 
39
 
def strace_detailed(function, args, kwargs, follow_children=True):
40
 
    # FIXME: strace is buggy
41
 
    # (https://bugs.launchpad.net/ubuntu/+source/strace/+bug/103133) and the
42
 
    # test suite hangs if the '-f' is given to strace *and* more than one
43
 
    # thread is running. Using follow_children=False allows the test suite to
44
 
    # disable fork following to work around the bug.
45
 
 
46
35
    # capture strace output to a file
47
 
    log_file = tempfile.NamedTemporaryFile()
 
36
    log_file = tempfile.TemporaryFile()
48
37
    log_file_fd = log_file.fileno()
49
38
    pid = os.getpid()
50
39
    # start strace
51
 
    strace_cmd = ['strace', '-r', '-tt', '-p', str(pid), '-o', log_file.name]
52
 
    if follow_children:
53
 
        strace_args.append('-f')
54
 
    proc = subprocess.Popen(strace_cmd,
55
 
                            stdout=subprocess.PIPE,
56
 
                            stderr=subprocess.STDOUT)
57
 
    # Wait for strace to attach
58
 
    attached_notice = proc.stdout.readline()
59
 
    # Run the function to 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.)
60
47
    result = function(*args, **kwargs)
61
48
    # stop strace
62
49
    os.kill(proc.pid, signal.SIGQUIT)