Determine if a program is running in debug mode
Asked Answered
H

3

10

I use RubyMine to write and debug my Ruby 2.0 code. It uses ruby-debug-ide for that purpose. I want to know if a program is running in debug mode.

I know there is the Ruby $DEBUG global variable, but as far as I understand ruby-debug-ide didn't change it, because it didn't use the -d ruby flag.

If I debug my file using Rubymine the command executed looks like this:

/home/user/.rvm/rubies/ruby-2.0.0-p353/bin/ruby -e at_exit{sleep(1)};$stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/user/.rvm/gems/ruby-2.0.0-p353/gems/ruby-debug-ide-0.4.22/bin/rdebug-ide --disable-int-handler --port 37737 --dispatcher-port 47992 -- /home/user/file.rb

I tried to use ARGV or $0, to determine if the command line contains the string 'rdebug-ide' but ARGV is an empty array and $0 is just '/home/user/file.rb', how can I get the full command line executed by RubyMine?

Hanshaw answered 26/2, 2014 at 11:21 Comment(2)
have you tried ENV['debugger_host'].nil?Disvalue
ENV['debugger_host'].nil? true in both casesHanshaw
D
7

This is what I did:

I put the following code in an (rails) action and did a diff on the outputs both in debug and non-debug modes:

puts ENV.to_hash.to_yaml

I noticed that one of the differences is in ENV['RUBYLIB'] (there's also IDE_PROCESS_DISPATCHER, DEBUGGER_STORED_RUBYLIB, RUBYOPT, and DEBUGGER_HOST)

So here's how you'd check:

if ENV['RUBYLIB'] =~ /ruby-debug-ide/
  puts 'in debug mode'
else
  puts 'not in debug mode'
end
Disvalue answered 26/2, 2014 at 13:8 Comment(4)
@PerLundberg check my method and do your own diff to figure out what works for you environmentDisvalue
@Disvalue Checked now. None of RUBYLIB or the other env variables mentioned have any unusual values when being run through the VSCode debugger.Lauber
@PerLundberg I don't use VSCode but I suggest changing the debugging command (there should be somewhere where you can add variables to it). Now that I'm thinking about this question/answer, I'm not sure that was useful. Have you tried gem pry-rails ?Disvalue
As @PerLundberg mentioned, on VSCode running puts ENV.to_hash.to_yaml with (F5) or without (Ctrl+F5) debugger returns the same output.Aeschines
U
1

You need the global variable $LOAD_PATH.

a = $LOAD_PATH
a.each do |current_path|
    puts 'Debug mode' if current_path.include?('rb/gems')
end

$LOAD_PATH has this line "/home/username/RubyMine-6.0.2/rb/gems" if I use debug mode.

Untried answered 26/2, 2014 at 11:50 Comment(2)
Welcome to Stack Overflow! Can you please edit your answer to include an example showing how to use the variable $LOAD_PATH to achieve what OP wants?Symptomatology
Sorry for mislead. For me this variant work only if clean .rb file. But if I use it in my project - both debug and run $LOAD_PATH contains 'ruby-debug', don't know whyHanshaw
U
1

If you know which debug library is being used, you can check if a constant defined by that extension is currently defined.

For example, the most common gem is ruby/debug and it defines the DEBUGGER__ module when it loads, so you can do:

if defined?(DEBUGGER__)
  ...
end
Unfathomable answered 15/5 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.