public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/4] Add DAP log level parameter
Date: Tue, 12 Dec 2023 10:44:43 -0700	[thread overview]
Message-ID: <20231212-dap-no-test-exceptions-v1-2-af0e33f10093@adacore.com> (raw)
In-Reply-To: <20231212-dap-no-test-exceptions-v1-0-af0e33f10093@adacore.com>

This adds a new parameter to control the DAP logging level.  By
default, "expected" exceptions are not logged, but the parameter lets
the user change this when more logging is desired.

This also changes a couple of spots to avoid logging the stack trace
for a DAPException.

This patch also documents the existing DAP logging parameter.  I
forgot to document this before.
---
 gdb/NEWS                             |  5 +++++
 gdb/doc/gdb.texinfo                  | 18 ++++++++++++++++++
 gdb/python/lib/gdb/dap/breakpoint.py |  6 ++++--
 gdb/python/lib/gdb/dap/server.py     | 10 ++++++++++
 gdb/python/lib/gdb/dap/startup.py    | 29 +++++++++++++++++++++++++----
 5 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 534e2e7f364..6d35722cc23 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -88,6 +88,11 @@ show remote thread-options-packet
 
   ** GDB now supports the "cancel" request.
 
+  ** New command "set debug dap-log-level" controls DAP logging.
+
+  ** The "set debug dap-log-file" command is now documented.  This
+     command was available in GDB 14 but not documented.
+
 * New remote packets
 
 New stop reason: clone
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 6e4adf512ee..fd60e41091a 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -39611,6 +39611,24 @@ Evaluations like this can be interrupted using the DAP @code{cancel}
 request.  (In fact, @code{cancel} should work for any request, but it
 is unlikely to be useful for most of them.)
 
+@value{GDBN} provides a couple of logging settings that can be used in
+DAP mode.  These can be set on the command line using the @code{-iex}
+option (@pxref{File Options}).
+
+@table @code
+@item set debug dap-log-file @r{[}@var{filename}@r{]}
+Enable DAP logging.  Logs are written to the specified file.  If no
+file is given, logging is stopped.
+
+@item set debug dap-log-level @var{level}
+Set the DAP logging level.  The default is @samp{1}, which logs the
+DAP protocol, whatever debug messages the developers thought were
+useful, and unexpected exceptions.  Level @samp{2} can be used to log
+all exceptions, including ones that are considered to be expected.
+For example, a failure to parse an expression would be considered a
+normal exception and not normally be logged.
+@end table
+
 @node JIT Interface
 @chapter JIT Compilation Interface
 @cindex just-in-time compilation
diff --git a/gdb/python/lib/gdb/dap/breakpoint.py b/gdb/python/lib/gdb/dap/breakpoint.py
index c67bb471daf..cef3df910f0 100644
--- a/gdb/python/lib/gdb/dap/breakpoint.py
+++ b/gdb/python/lib/gdb/dap/breakpoint.py
@@ -24,7 +24,7 @@ from typing import Optional, Sequence
 
 from .server import request, capability, send_event
 from .sources import make_source
-from .startup import in_gdb_thread, log_stack, parse_and_eval, DAPException
+from .startup import in_gdb_thread, log_stack, parse_and_eval, LOG_FULL, DAPException
 from .typecheck import type_check
 
 
@@ -176,7 +176,9 @@ def _set_breakpoints_callback(kind, specs, creator):
             result.append(_breakpoint_descriptor(bp))
         # Exceptions other than gdb.error are possible here.
         except Exception as e:
-            log_stack()
+            # Don't normally want to see this, as it interferes with
+            # the test suite.
+            log_stack(LOG_FULL)
             # Maybe the breakpoint was made but setting an attribute
             # failed.  We still want this to fail.
             if bp is not None:
diff --git a/gdb/python/lib/gdb/dap/server.py b/gdb/python/lib/gdb/dap/server.py
index 44dffb1b809..e682c0faf84 100644
--- a/gdb/python/lib/gdb/dap/server.py
+++ b/gdb/python/lib/gdb/dap/server.py
@@ -23,6 +23,7 @@ import threading
 from .io import start_json_writer, read_json
 from .startup import (
     exec_and_log,
+    DAPException,
     DAPQueue,
     in_dap_thread,
     in_gdb_thread,
@@ -31,6 +32,7 @@ from .startup import (
     start_thread,
     log,
     log_stack,
+    LOG_FULL,
 )
 from .typecheck import type_check
 
@@ -139,12 +141,20 @@ class Server:
                 result["body"] = body
             result["success"] = True
         except NotStoppedException:
+            # This is an expected exception, and the result is clearly
+            # visible in the log, so do not log it.
             result["success"] = False
             result["message"] = "notStopped"
         except KeyboardInterrupt:
             # This can only happen when a request has been canceled.
             result["success"] = False
             result["message"] = "cancelled"
+        except DAPException as e:
+            # Don't normally want to see this, as it interferes with
+            # the test suite.
+            log_stack(LOG_FULL)
+            result["success"] = False
+            result["message"] = str(e)
         except BaseException as e:
             log_stack()
             result["success"] = False
diff --git a/gdb/python/lib/gdb/dap/startup.py b/gdb/python/lib/gdb/dap/startup.py
index 64a46597bf4..548fedf4ac9 100644
--- a/gdb/python/lib/gdb/dap/startup.py
+++ b/gdb/python/lib/gdb/dap/startup.py
@@ -101,6 +101,27 @@ def in_dap_thread(func):
     return ensure_dap_thread
 
 
+# Logging levels.
+LOG_DEFAULT = 1
+LOG_FULL = 2
+
+
+class LogLevel(gdb.Parameter):
+    """DAP logging level."""
+
+    set_doc = "Set the DAP logging level."
+    show_doc = "Show the DAP logging level."
+
+    def __init__(self):
+        super().__init__(
+            "debug dap-log-level", gdb.COMMAND_MAINTENANCE, gdb.PARAM_ZUINTEGER
+        )
+        self.value = LOG_DEFAULT
+
+
+_log_level = LogLevel()
+
+
 class LoggingParam(gdb.Parameter):
     """Whether DAP logging is enabled."""
 
@@ -128,16 +149,16 @@ class LoggingParam(gdb.Parameter):
 dap_log = LoggingParam()
 
 
-def log(something):
+def log(something, level=LOG_DEFAULT):
     """Log SOMETHING to the log file, if logging is enabled."""
-    if dap_log.log_file is not None:
+    if dap_log.log_file is not None and level <= _log_level.value:
         print(something, file=dap_log.log_file)
         dap_log.log_file.flush()
 
 
-def log_stack():
+def log_stack(level=LOG_DEFAULT):
     """Log a stack trace to the log file, if logging is enabled."""
-    if dap_log.log_file is not None:
+    if dap_log.log_file is not None and level <= _log_level.value:
         traceback.print_exc(file=dap_log.log_file)
 
 

-- 
2.43.0


  parent reply	other threads:[~2023-12-12 17:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-12 17:44 [PATCH 0/4] Check for rogue DAP exceptions Tom Tromey
2023-12-12 17:44 ` [PATCH 1/4] Introduce and use DAPException Tom Tromey
2023-12-12 17:44 ` Tom Tromey [this message]
2023-12-12 18:39   ` [PATCH 2/4] Add DAP log level parameter Eli Zaretskii
2023-12-14 19:02     ` Tom Tromey
2023-12-13 16:35   ` Kévin Le Gouguec
2023-12-14 19:06     ` Tom Tromey
2023-12-12 17:44 ` [PATCH 3/4] Avoid exception from attach in DAP Tom Tromey
2023-12-13 16:36   ` Kévin Le Gouguec
2023-12-14 18:52     ` Tom Tromey
2023-12-12 17:44 ` [PATCH 4/4] Check for rogue DAP exceptions in test suite Tom Tromey
2023-12-13 16:36 ` [PATCH 0/4] Check for rogue DAP exceptions Kévin Le Gouguec
2023-12-22 16:57 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231212-dap-no-test-exceptions-v1-2-af0e33f10093@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).