Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add strace_me, a simple strace(1) wrapper

  • Loading branch information...
commit d87ecda7db5b6153044b958405ee2fb414931928 0 parents
@drbrain authored
8 .autotest
@@ -0,0 +1,8 @@
+# -*- ruby -*-
+
+require 'autotest/restart'
+
+Autotest.add_hook :initialize do |at|
+ at.testlib = 'minitest/unit'
+end
+
4 .gitignore
@@ -0,0 +1,4 @@
+/TAGS
+/pkg
+/doc
+*.swp
5 History.txt
@@ -0,0 +1,5 @@
+=== 1.0.0 / 2010-06-07
+
+* Major Enhancement
+ * Birthday!
+
5 Manifest.txt
@@ -0,0 +1,5 @@
+History.txt
+Manifest.txt
+README.txt
+Rakefile
+lib/strace.rb
50 README.txt
@@ -0,0 +1,50 @@
+= strace_me
+
+* http://seattlerb.rubyforge.org/strace_me
+
+== DESCRIPTION:
+
+A wrapper around strace(1) that allows you to perform targetted tracing of a
+block. strace_me allows you to track down problems without wading through
+gobs of ruby startup or other irrelevant noise.
+
+== FEATURES/PROBLEMS:
+
+* strace only
+
+== SYNOPSIS:
+
+See Strace::me
+
+== REQUIREMENTS:
+
+* strace
+
+== INSTALL:
+
+* sudo gem install strace_me
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2010 Eric Hodel
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 Rakefile
@@ -0,0 +1,16 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+
+Hoe.plugin :git
+Hoe.plugin :minitest
+
+Hoe.spec 'strace_me' do
+ self.rubyforge_name = 'seattlerb'
+ developer 'Eric Hodel', 'drbrain@segment7.net'
+
+ extra_deps << ['open4', '~> 0.9']
+end
+
+# vim: syntax=Ruby
62 lib/strace.rb
@@ -0,0 +1,62 @@
+require 'open4'
+
+##
+# Simple wrapper of strace(1) for targetted tracing.
+
+module Strace
+
+ ##
+ # The version of strace_me you're using
+
+ VERSION = '1.0'
+
+ ##
+ # Runs strace(1) during the code in the block and returns an IO containing
+ # the strace output and the exception raised by the block (if any).
+ #
+ # Simple example (it doesn't matter if you have a /COPYRIGHT):
+ #
+ # trace, err = Strace.me do
+ # open '/COPYRIGHT' do |io|
+ # io.read 1024
+ # end
+ # end
+ #
+ # puts "error: #{err.message} (#{err.class})\n\n" if err
+ # puts "trace:\n\n#{trace.read}"
+ #
+ # Tracing an existing library call:
+ #
+ # alias old_connect connect
+ #
+ # def connect host, user, password
+ # connection = nil
+ #
+ # trace, err = Strace.me do
+ # connection = old_connect host, user, password
+ # end
+ #
+ # $stderr.puts "connect trace:\n\n#{trace.read}"
+ #
+ # raise err if err
+ # connection
+ # end
+
+ def self.me
+ pid, i, o, e = Open4.open4 'strace', '-p', $$.to_s
+
+ i.close
+ e.gets # consumes "attached" message, but we know strace is ready
+
+ begin
+ yield
+ ensure
+ Process.kill 'INT', pid
+ Process.waitpid pid
+
+ return [e, $!]
+ end
+ end
+
+end
+
20 test/test_strace.rb
@@ -0,0 +1,20 @@
+require 'rubygems'
+require 'minitest/autorun'
+require 'strace'
+
+class TestStrace < MiniTest::Unit::TestCase
+
+ def test_class_me_no_strace
+ path = ENV['PATH']
+ ENV['PATH'] = nil
+
+ assert_raises Errno::ENOENT do
+ Strace.me do end
+ end
+
+ ensure
+ ENV['PATH'] = path
+ end
+
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.