   (when gud-last-frame (setq gud-last-last-frame gud-last-frame))
   (when rdebug-many-windows
     (rdebug-setup-windows)))
+
+(defun rdebug-process-sentinel (process event)
+  "Restore the original window configuration when the debugger process exits."
+  (rdebug-debug-enter "rdebug-process-sentinel"
+    (rdebug-debug-message "status=%S event=%S state=%S\n"
+                          (process-status process)
+                          event
+                          rdebug-frame-configuration-state)
+    ;; When the debugger process exited, when the comint buffer has no
+    ;; buffer process (nil). When the debugger processes is replaced
+    ;; with another process we should not restore the window
+    ;; configuration.
+    (when (and rdebug-restore-original-frame-configuration
+               (or (null (get-buffer-process gud-comint-buffer))
+                   (eq process (get-buffer-process gud-comint-buffer)))
+               (eq rdebug-frame-configuration-state 'debugger)
+               (not (eq (process-status process) 'run)))
+      (setq rdebug-frame-configuration-state 'original)
+      (rdebug-display-original-frame-configuration))))
 
 ;; Fontification and keymaps for secondary buffers (breakpoints, stack)
 

	 (gud-find-file (car gud-last-last-frame))
       ;; Put buffer list in window if we
       ;; can't find a source file.
       (list-buffers-noselect)))
    (other-window 1)
    (set-window-buffer 
     (selected-window) 

(defconst rdebug-annotation-end-regexp
  "\n")

(defvar rdebug-original-frame-configuration nil
  "The window layout rdebug should restore when the debugger exits.")

;; This is used to ensure that the original frame configuration is
;; restored even when the user re-starts the debugger several times.
(defvar rdebug-frame-configuration-state 'original
  "Represent which frame configuration  currently is in use.
Can be `original' or `debugger'.")

;; rdebugtrack constants
(defconst rdebugtrack-stack-entry-regexp
      "^(\\([-a-zA-Z0-9_/.]*\\):\\([0-9]+\\)):[ \t]?\\(.*\n\\)"
