Subject: Re: bug#64186: Patch: Suppress deprecated '-gdb-set target-async' warning message in gdb-mi.el

From: Wang Diancheng <[email-masked]>
Date: Wed, 28 Jun 2023 11:51:39 +0800

Hi,
Eli Zaretskii <[email-masked]> 于2023年6月21日周三 21:12写道：
>
> merge 64186 63084
> thanks
>
> > From: Wang Diancheng <[email-masked]>
> > Date: Wed, 21 Jun 2023 14:49:01 +0800
> > Cc: [email-masked], [email-masked]
> >
> > Eli Zaretskii <[email-masked]> 于2023年6月20日周二 19:55写道：
> > >
> > > Given all that mess, I wonder if "M-x gdb" in its current shape indeed
> > > supports the asynchronous execution.  If it doesn't, we may be better
> > > off not sending that command at all for now, until bug#63084 is fixed.
> >
> > Yes, I agree. Currently gdb-mi doesn't actually support asynchronous
> > execution because of bug#63084. Before it is fixed, to suppress the
> > annoying warning, we can set gdb-non-stop-setting to nil as a
> > workaround.
>
> Thanks, I've now done so on the emacs-29 branch.  I will leave this
> bug open (and merge it to bug#63084), so that they could be fixed in
> the future, hopefully not too distant future.

I did some hacks to make gdb-non-stop work, patch is attached. The
patch try to set `gdb-non-stop` when target is running or at first
stop. Sorry for rough patch. I'm new for elisp.
diff --git a/lisp/progmodes/gdb-mi.el b/lisp/progmodes/gdb-mi.el
index c9afe502a50..62645a54244 100644
--- a/lisp/progmodes/gdb-mi.el
+++ b/lisp/progmodes/gdb-mi.el
@@ -237,6 +237,7 @@ gdb-handler-list
 (defvar gdb-source-file-list nil
   "List of source files for the current executable.")
 (defvar gdb-first-done-or-error t)
+(defvar gdb-target-async-checked nil)
 (defvar gdb-source-window-list nil
   "List of windows used for displaying source files.
 Sorted in most-recently-visited-first order.")
@@ -453,9 +454,7 @@ gdb-debug-log-max
           (const   :tag "Unlimited" nil))
   :version "22.1")
 
-;; This is disabled by default because we don't really support
-;; asynchronous execution of the debuggee; see bug#63084.  FIXME.
-(defcustom gdb-non-stop-setting nil
+(defcustom gdb-non-stop-setting (not (eq system-type 'windows-nt))
   "If non-nil, GDB sessions are expected to support the non-stop mode.
 When in the non-stop mode, stopped threads can be examined while
 other threads continue to execute.
@@ -470,7 +469,7 @@ gdb-non-stop-setting
 GDB session needs to be restarted for this setting to take effect."
   :type 'boolean
   :group 'gdb-non-stop
-  :version "29.1")
+  :version "30.1")
 
 (defcustom gdb-debuginfod-enable-setting
   ;; debuginfod servers are only for ELF executables, and elfutils, of
@@ -1069,6 +1068,7 @@ gdb-init-1
 	gdb-handler-list '()
 	gdb-prompt-name nil
 	gdb-first-done-or-error t
+        gdb-target-async-checked nil
 	gdb-buffer-fringe-width (car (window-fringes))
 	gdb-debug-log nil
 	gdb-source-window-list nil
@@ -1078,7 +1078,8 @@ gdb-init-1
         gdb-threads-list '()
         gdb-breakpoints-list '()
         gdb-register-names '()
-        gdb-non-stop gdb-non-stop-setting
+        gdb-supports-non-stop nil
+        gdb-non-stop nil
         gdb-debuginfod-enable gdb-debuginfod-enable-setting)
   ;;
   (gdbmi-bnf-init)
@@ -1110,7 +1111,7 @@ gdb-init-1
     (gdb-input "-gdb-set interactive-mode on" 'ignore))
   (gdb-input "-gdb-set height 0" 'ignore)
 
-  (when gdb-non-stop
+  (when gdb-non-stop-setting
     (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
 
   (gdb-input "-enable-pretty-printing" 'ignore)
@@ -1145,16 +1146,30 @@ gdb-non-stop-handler
 	(setq gdb-non-stop nil)
 	(setq gdb-supports-non-stop nil))
     (setq gdb-supports-non-stop t)
-    (gdb-input "-gdb-set target-async 1" 'ignore)
+    ;; Try to use "mi-async" first, needs GDB 7.7 onwards.  Note if
+    ;; "mi-async" is not available, GDB is still running in "sync"
+    ;; mode, "No symbol" for "mi-async" must appear before other
+    ;; commands.
+    (gdb-input "-gdb-set mi-async 1" 'gdb-set-mi-async-handler)))
+
+(defun gdb-set-mi-async-handler()
+  (goto-char (point-min))
+  (if (re-search-forward "No symbol" nil t)
+      (gdb-input "-gdb-set target-async 1" 'ignore)))
+
+(defun gdb-try-check-target-async-support()
+  (when (and gdb-non-stop-setting gdb-supports-non-stop
+             (not gdb-target-async-checked))
     (gdb-input "-list-target-features" 'gdb-check-target-async)))
 
 (defun gdb-check-target-async ()
   (goto-char (point-min))
-  (unless (re-search-forward "async" nil t)
+  (if (re-search-forward "async" nil t)
+      (setq gdb-non-stop t)
     (message
      "Target doesn't support non-stop mode.  Turning it off.")
-    (setq gdb-non-stop nil)
-    (gdb-input "-gdb-set non-stop 0" 'ignore)))
+    (gdb-input "-gdb-set non-stop 0" 'ignore))
+  (setq gdb-target-async-checked t))
 
 (defun gdb-delchar-or-quit (arg)
   "Delete ARG characters or send a quit command to GDB.
@@ -2652,6 +2667,13 @@ gdb-running
 (defun gdb-starting (_output-field _result)
   ;; CLI commands don't emit ^running at the moment so use gdb-running too.
   (setq gdb-inferior-status "running")
+
+  ;; Set `gdb-non-stop` when `gdb-last-command` is a CLI background
+  ;; `run` command e.g. r& or MI command `-exec-run`
+  (when (or (string-match "&\s*$" gdb-last-command)
+            (string-match "-exec-run" gdb-last-command))
+    (gdb-try-check-target-async-support))
+
   (gdb-force-mode-line-update
    (propertize gdb-inferior-status 'face font-lock-type-face))
   (setq gdb-active-process t)
@@ -2722,6 +2744,9 @@ gdb-stopped
 
     ;; Print "(gdb)" to GUD console
     (when gdb-first-done-or-error
+      ;; If run target with CLI foreground command `run`, `target
+      ;; async` can only be checked when target is stopped
+      (gdb-try-check-target-async-support)
       (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
 
     ;; In non-stop, we update information as soon as another thread gets