From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id 2AE1F3858284; Thu, 29 Feb 2024 20:29:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2AE1F3858284 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1709238561; bh=EJIayxYPVInhnIvqEh4UU/7xxhi9re6UazQfaBbU94k=; h=From:To:Subject:Date:From; b=BN+uAkfU+nac2vdZceHYI3E8/jfIbEne35XMAW6YhL29QbXj1p6Dt8n2+9gwqROxZ ToqZwvhvRaltFaSB+VDEEx1ZP4+GPlschUs5HN/nQUyvYleMUfAKPC2ZTpKAf6zYh7 To1g/WNTpp3TmVhm0hXBCvTA+lmHq8qWHTLO6SzM= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb/dap] Move send_gdb and send_gdb_with_response to server module X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: bbb12eb9c84aa2b32480b7c022c494c2469ef717 X-Git-Newrev: fd09caf44f193fb4359376b27904bad0a16ca594 Message-Id: <20240229202921.2AE1F3858284@sourceware.org> Date: Thu, 29 Feb 2024 20:29:21 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dfd09caf44f19= 3fb4359376b27904bad0a16ca594 commit fd09caf44f193fb4359376b27904bad0a16ca594 Author: Tom de Vries Date: Thu Feb 29 21:29:34 2024 +0100 [gdb/dap] Move send_gdb and send_gdb_with_response to server module =20 Move functions send_gdb and send_gdb_with_response, as well as class In= voker to server module. =20 Separated out to make the following patch easier to read. =20 Tested on aarch64-linux. =20 Approved-By: Tom Tromey Diff: --- gdb/python/lib/gdb/dap/next.py | 4 ++-- gdb/python/lib/gdb/dap/server.py | 48 +++++++++++++++++++++++++++++++++++= ++-- gdb/python/lib/gdb/dap/startup.py | 46 -----------------------------------= -- 3 files changed, 48 insertions(+), 50 deletions(-) diff --git a/gdb/python/lib/gdb/dap/next.py b/gdb/python/lib/gdb/dap/next.py index 17bf57d1788..1dc1d6dd74d 100644 --- a/gdb/python/lib/gdb/dap/next.py +++ b/gdb/python/lib/gdb/dap/next.py @@ -16,8 +16,8 @@ import gdb =20 from .events import exec_and_expect_stop -from .server import capability, request -from .startup import in_gdb_thread, send_gdb, send_gdb_with_response +from .server import capability, request, send_gdb, send_gdb_with_response +from .startup import in_gdb_thread from .state import set_thread =20 =20 diff --git a/gdb/python/lib/gdb/dap/server.py b/gdb/python/lib/gdb/dap/serv= er.py index 19840f4028d..fe1f8bbd428 100644 --- a/gdb/python/lib/gdb/dap/server.py +++ b/gdb/python/lib/gdb/dap/server.py @@ -27,8 +27,6 @@ from .startup import ( DAPQueue, in_dap_thread, in_gdb_thread, - send_gdb, - send_gdb_with_response, start_thread, log, log_stack, @@ -422,3 +420,49 @@ def cancel(**args): # ... which gdb takes to mean that it is fine for all cancel # requests to report success. return None + + +class Invoker(object): + """A simple class that can invoke a gdb command.""" + + def __init__(self, cmd): + self.cmd =3D cmd + + # This is invoked in the gdb thread to run the command. + @in_gdb_thread + def __call__(self): + exec_and_log(self.cmd) + + +def send_gdb(cmd): + """Send CMD to the gdb thread. + CMD can be either a function or a string. + If it is a string, it is passed to gdb.execute.""" + if isinstance(cmd, str): + cmd =3D Invoker(cmd) + gdb.post_event(cmd) + + +def send_gdb_with_response(fn): + """Send FN to the gdb thread and return its result. + If FN is a string, it is passed to gdb.execute and None is + returned as the result. + If FN throws an exception, this function will throw the + same exception in the calling thread. + """ + if isinstance(fn, str): + fn =3D Invoker(fn) + result_q =3D DAPQueue() + + def message(): + try: + val =3D fn() + result_q.put(val) + except (Exception, KeyboardInterrupt) as e: + result_q.put(e) + + send_gdb(message) + val =3D result_q.get() + if isinstance(val, (Exception, KeyboardInterrupt)): + raise val + return val diff --git a/gdb/python/lib/gdb/dap/startup.py b/gdb/python/lib/gdb/dap/sta= rtup.py index a2b68996dba..aaf1e8cd657 100644 --- a/gdb/python/lib/gdb/dap/startup.py +++ b/gdb/python/lib/gdb/dap/startup.py @@ -214,49 +214,3 @@ def exec_and_log(cmd): log(">>> " + output) except gdb.error: log_stack() - - -class Invoker(object): - """A simple class that can invoke a gdb command.""" - - def __init__(self, cmd): - self.cmd =3D cmd - - # This is invoked in the gdb thread to run the command. - @in_gdb_thread - def __call__(self): - exec_and_log(self.cmd) - - -def send_gdb(cmd): - """Send CMD to the gdb thread. - CMD can be either a function or a string. - If it is a string, it is passed to gdb.execute.""" - if isinstance(cmd, str): - cmd =3D Invoker(cmd) - gdb.post_event(cmd) - - -def send_gdb_with_response(fn): - """Send FN to the gdb thread and return its result. - If FN is a string, it is passed to gdb.execute and None is - returned as the result. - If FN throws an exception, this function will throw the - same exception in the calling thread. - """ - if isinstance(fn, str): - fn =3D Invoker(fn) - result_q =3D DAPQueue() - - def message(): - try: - val =3D fn() - result_q.put(val) - except (Exception, KeyboardInterrupt) as e: - result_q.put(e) - - send_gdb(message) - val =3D result_q.get() - if isinstance(val, (Exception, KeyboardInterrupt)): - raise val - return val