[MERGE] Wait for strace to attach before running the function we want to trace

Andrew Bennetts andrew at canonical.com
Wed Apr 4 02:49:01 BST 2007


Martin Pool wrote:
> This doesn't actually wait, it's just equivalent to a brief sleep.

You're quite right, I was thinking it was a pipe rather than a file.

Here's a corrected version.  I pass "-o" to strace so that it writes the trace
to a file, which means that the only traffic on stderr should be the "Process
XXXX attached" message, which is small enough we can safely call use readline to
read off the pipe in a blocking fashion.

This also has the side-effect that the temporary file being used will be visible
in "ps" output for the strace process, which is perhaps useful.

I've never actually been able to reproduce the reported race on my system, but
this change *looks* correct.  But then I thought that last time, too :)

-Andrew.

-------------- next part --------------
# Bazaar revision bundle v0.9
#
# message:
#   Wait for strace to attach before running the function we want to trace.
# committer: Andrew Bennetts <andrew.bennetts at canonical.com>
# date: Wed 2007-04-04 11:42:41.947000027 +1000

=== modified file bzrlib/strace.py
--- bzrlib/strace.py
+++ bzrlib/strace.py
@@ -33,17 +33,18 @@
     :return: a tuple: function-result, a StraceResult.
     """
     # capture strace output to a file
-    log_file = tempfile.TemporaryFile()
+    log_file = tempfile.NamedTemporaryFile()
     log_file_fd = log_file.fileno()
     pid = os.getpid()
     # start strace
     proc = subprocess.Popen(['strace',
-        '-f', '-r', '-tt', '-p', str(pid),
+        '-f', '-r', '-tt', '-p', str(pid), '-o', log_file.name
         ],
-        stderr=log_file_fd,
-        stdout=log_file_fd)
-    # TODO? confirm its started (test suite should be sufficient)
-    # (can loop on proc.pid, but that may not indicate started and attached.)
+        stdout=subprocess.PIPE,
+        stderr=subprocess.STDOUT)
+    # Wait for strace to attach
+    attached_notice = proc.stdout.readline()
+    # Run the function to strace
     result = function(*args, **kwargs)
     # stop strace
     os.kill(proc.pid, signal.SIGQUIT)

=== modified directory  // last-changed:andrew.bennetts at canonical.com-200704040
... 14241-swk0f9ec602dnezm
# revision id: andrew.bennetts at canonical.com-20070404014241-swk0f9ec602dnezm
# sha1: 2b66f47c27da96290077c194322d58be43a82e3c
# inventory sha1: 09bd382914ae9ee3c6ef6b85eef1a1a463f79440
# parent ids:
#   pqm at pqm.ubuntu.com-20070404005411-46c388fbf2acf940
# base id: pqm at pqm.ubuntu.com-20070404005411-46c388fbf2acf940
# properties:
#   branch-nick: strace-attach



More information about the bazaar mailing list