import json
import threading
from contextlib import contextmanager
+from typing import Optional
import gdb
exec_and_log("kill")
+@in_gdb_thread
+def _disconnect_or_kill(terminate: Optional[bool]):
+ inf = gdb.selected_inferior()
+ if inf.connection is None:
+ # Nothing to do here.
+ return
+ if terminate is None:
+ # The default depends on whether the inferior was attached or
+ # launched.
+ terminate = not inf.was_attached
+ if terminate:
+ exec_and_log("kill")
+ elif inf.was_attached:
+ exec_and_log("detach")
+
+
@request("disconnect", on_dap_thread=True, expect_stopped=False)
@capability("supportTerminateDebuggee")
-def disconnect(*, terminateDebuggee: bool = False, **args):
- if terminateDebuggee:
- send_gdb_with_response(lambda: exec_and_log("kill"))
+def disconnect(*, terminateDebuggee: Optional[bool] = None, **args):
+ send_gdb_with_response(lambda: _disconnect_or_kill(terminateDebuggee))
_server.shutdown()
--- /dev/null
+# Copyright 2025 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+load_lib dap-support.exp
+
+require allow_dap_tests
+
+if {[dap_initialize] == ""} {
+ return
+}
+
+dap_shutdown true