Repository: ruby/debug (Ruby)

Issue:
`rdbg` fails to start postmortem session from ENV variable
Current SHA: cb41b9934741177cb95949ddce2959b6fb4fb459

```
$ RUBY_DEBUG_POSTMORTEM=true bundle exec rdbg -- -e true
bundler: failed to load command: rdbg (/usr/local/bundle/bin/rdbg)
NameError: uninitialized constant DEBUGGER__::Config::SESSION
Did you mean?  DEBUGGER__::Session
  /workspace/lib/debug/config.rb:116:in `block in update'
  /workspace/lib/debug/config.rb:126:in `if_updated'
  /workspace/lib/debug/config.rb:115:in `update'
  /workspace/lib/debug/config.rb:59:in `initialize'
  /workspace/lib/debug/config.rb:350:in `new'
  /workspace/lib/debug/config.rb:350:in `<module:DEBUGGER__>'
  /workspace/lib/debug/config.rb:5:in `<top (required)>'
  /workspace/exe/rdbg:3:in `require_relative'
  /workspace/exe/rdbg:3:in `<top (required)>'
  /usr/local/bundle/bin/rdbg:23:in `load'
  /usr/local/bundle/bin/rdbg:23:in `<top (required)>'
```



Discussion:

Patch:
diff --git a/lib/debug/config.rb b/lib/debug/config.rb
index 00dd4f336..f00c7b5ea 100644
--- a/lib/debug/config.rb
+++ b/lib/debug/config.rb
@@ -111,7 +111,9 @@ def update conf
       end
 
       if_updated old_conf, conf, :postmortem do |_, new_p|
-        SESSION.postmortem = new_p
+        if defined?(SESSION)
+          SESSION.postmortem = new_p
+        end
       end
 
       if_updated old_conf, conf, :sigdump_sig do |old_sig, new_sig|
diff --git a/lib/debug/session.rb b/lib/debug/session.rb
index 02b322d4f..906a2b892 100644
--- a/lib/debug/session.rb
+++ b/lib/debug/session.rb
@@ -103,12 +103,15 @@ def initialize ui
       @var_map   = {1 => [:globals], } # {id => ...} for DAP
       @src_map   = {} # {id => src}
 
+      @tp_thread_begin = nil
       @tp_load_script = TracePoint.new(:script_compiled){|tp|
         ThreadClient.current.on_load tp.instruction_sequence, tp.eval_script
       }
       @tp_load_script.enable
 
       activate
+
+      self.postmortem = CONFIG[:postmortem]
     end
 
     def active?
@@ -1525,7 +1528,6 @@ def event type, *args
         i, bp = *args
         puts "\nStop by \##{i} #{bp}" if bp
       when :suspend_trap
-        sig = args.first
         puts "\nStop by #{args.first}"
       end
     end
@@ -1614,7 +1616,6 @@ def self.setup_initial_suspend
       case
       when CONFIG[:stop_at_load]
         add_line_breakpoint __FILE__, __LINE__ + 1, oneshot: true, hook_call: false
-        a = 1
       when path = ENV['RUBY_DEBUG_INITIAL_SUSPEND_PATH']
         add_line_breakpoint path, 0, oneshot: true, hook_call: false
       when loc = ::DEBUGGER__.require_location


Test patch:
diff --git a/test/debug/config_postmortem_test.rb b/test/debug/config_postmortem_test.rb
index 722a9217b..ccbc04158 100644
--- a/test/debug/config_postmortem_test.rb
+++ b/test/debug/config_postmortem_test.rb
@@ -29,8 +29,27 @@ def test_config_postmortem
         type 'step'
         assert_line_text(/Can not use this command on postmortem mode/)
         type 'c'
-        # assert_line_text(/unhandled exception/)
+        assert_finish
+      end
+    end
+
+    def test_env_var_postmortem
+      ENV["RUBY_DEBUG_POSTMORTEM"] = "true"
+      debug_code(program) do
+        type 'c'
+        assert_line_text(/Enter postmortem mode with RuntimeError/)
+        type 'p x'
+        assert_line_text(/=> 4/)
+        type 'up'
+        type 'p y'
+        assert_line_text(/=> 1/)
+        type 'step'
+        assert_line_text(/Can not use this command on postmortem mode/)
+        type 'c'
+        assert_finish
       end
+    ensure
+      ENV["RUBY_DEBUG_POSTMORTEM"] = nil
     end
   end