Module: Hoe::Debugging

Defined in:
lib/hoe/debugging.rb

Overview

Whee, stuff to help when your codes are b0rked. Tasks provided:

  • test:gdb

  • test:valgrind

  • test:valgrind:mem

  • test:valgrind:mem0

Defined Under Namespace

Classes: ValgrindHelper

Constant Summary collapse

VERSION =

:nodoc:

"2.0.0"
ERROR_EXITCODE =

The exit code of valgrind when it detects an error.

42

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gdb_optionsObject

Optional: Used to add flags to GDB. [default: []]



23
24
25
# File 'lib/hoe/debugging.rb', line 23

def gdb_options
  @gdb_options
end

#valgrind_optionsObject

Optional: Used to add flags to valgrind. [default: %w(--num-callers=50 --error-limit=no --partial-loads-ok=yes --undef-value-errors=no)]



30
31
32
# File 'lib/hoe/debugging.rb', line 30

def valgrind_options
  @valgrind_options
end

Instance Method Details

#define_debugging_tasksObject

:nodoc:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hoe/debugging.rb', line 80

def define_debugging_tasks #:nodoc:
  desc "Run the test suite under GDB."
  task "test:gdb" do
    sh "gdb #{gdb_options.join ' '} --args #{hoe_debugging_command}"
  end

  desc "Run the test suite under Valgrind."
  task "test:valgrind" do
    vopts = valgrind_options
    hoe_debugging_check_for_suppression_file vopts
    hoe_debugging_run_valgrind hoe_debugging_command, vopts
  end

  desc "Run the test suite under Valgrind with memory-fill."
  task "test:valgrind:mem" do
    vopts = valgrind_options + ["--freelist-vol=100000000", "--malloc-fill=6D", "--free-fill=66"]
    hoe_debugging_check_for_suppression_file vopts
    hoe_debugging_run_valgrind hoe_debugging_command, vopts
  end

  desc "Run the test suite under Valgrind with memory-zero."
  task "test:valgrind:mem0" do
    vopts = valgrind_options + ["--freelist-vol=100000000", "--malloc-fill=00", "--free-fill=00"]
    hoe_debugging_check_for_suppression_file vopts
    hoe_debugging_run_valgrind hoe_debugging_command, vopts
  end

  desc "Generate a valgrind suppression file for your test suite."
  task "test:valgrind:suppression" do
    vopts = valgrind_options
    generated_suppression_file = false
    ::Tempfile.open "hoe_debugging_valgrind_suppression_log" do |logfile|
      begin
        hoe_debugging_run_valgrind "#{hoe_debugging_ruby} #{hoe_debugging_make_test_cmd} 2> #{logfile.path}", vopts
      rescue RuntimeError
        suppfile = hoe_debugging_valgrind_helper.save_suppressions_from logfile.path
        puts "NOTICE: saved suppressions to #{suppfile}"
        generated_suppression_file = true
      end
    end
    unless generated_suppression_file
      puts "WARNING: no valgrind warnings detected, no suppressions file generated"
    end
  end

  desc "Generate a valgrind suppression file from a previous run's log file"
  task "valgrind:suppression", [:file] do |task, args|
    file_path = args[:file]
    raise "Please specify the path to your log file" unless file
    suppfile = hoe_debugging_valgrind_helper.save_suppressions_from file_path
    puts "NOTICE: saved suppressions to #{suppfile}"
  end
end

#hoe_debugging_check_for_suppression_file(options) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/hoe/debugging.rb', line 68

def hoe_debugging_check_for_suppression_file options
  suppression_files = hoe_debugging_valgrind_helper.matching_suppression_files
  suppression_files.each do |suppression_file|
    puts "NOTICE: using valgrind suppressions in #{suppression_file.inspect}"
    options << "--suppressions=#{suppression_file}"
  end
end

#hoe_debugging_commandObject



60
61
62
# File 'lib/hoe/debugging.rb', line 60

def hoe_debugging_command
  "#{hoe_debugging_ruby} #{hoe_debugging_make_test_cmd}"
end

#hoe_debugging_make_test_cmdObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/hoe/debugging.rb', line 49

def hoe_debugging_make_test_cmd
  cmd = []
  if File.directory? "spec"
    cmd << "-S rspec"
    cmd << (ENV['FILTER'] || ENV['TESTOPTS'])
  else
    cmd << make_test_cmd
  end
  cmd.join(' ')
end

#hoe_debugging_rubyObject



44
45
46
47
# File 'lib/hoe/debugging.rb', line 44

def hoe_debugging_ruby
  # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151376
  @ruby ||= File.join(RbConfig::CONFIG["bindir"], (RbConfig::CONFIG["RUBY_INSTALL_NAME"] + RbConfig::CONFIG["EXEEXT"]))
end

#hoe_debugging_run_valgrind(command, cmdline_options = []) ⇒ Object



64
65
66
# File 'lib/hoe/debugging.rb', line 64

def hoe_debugging_run_valgrind command, cmdline_options=[]
  sh "#{hoe_debugging_valgrind_helper.valgrind} #{cmdline_options.join(' ')} #{command}"
end

#hoe_debugging_valgrind_helperObject



76
77
78
# File 'lib/hoe/debugging.rb', line 76

def hoe_debugging_valgrind_helper
  @valgrind_helper ||= ValgrindHelper.new name
end

#initialize_debuggingObject

:nodoc:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hoe/debugging.rb', line 32

def initialize_debugging #:nodoc:
  self.gdb_options = []

  self.valgrind_options = ["--num-callers=50",
                           "--error-limit=no",
                           "--partial-loads-ok=yes",
                           "--undef-value-errors=no",
                           "--error-exitcode=#{ERROR_EXITCODE}",
                           "--gen-suppressions=all",
                          ]
end