Skip to content

Commit

Permalink
Add strace_me, a simple strace(1) wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Jun 7, 2010
0 parents commit d87ecda
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .autotest
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- ruby -*-

require 'autotest/restart'

Autotest.add_hook :initialize do |at|
at.testlib = 'minitest/unit'
end

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/TAGS
/pkg
/doc
*.swp
5 changes: 5 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=== 1.0.0 / 2010-06-07

* Major Enhancement
* Birthday!

5 changes: 5 additions & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
History.txt
Manifest.txt
README.txt
Rakefile
lib/strace.rb
50 changes: 50 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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 changes: 62 additions & 0 deletions lib/strace.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions test/test_strace.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d87ecda

Please sign in to comment.