From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by sourceware.org (Postfix) with ESMTPS id 939A53858C74 for ; Mon, 12 Jun 2023 19:36:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 939A53858C74 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-io1-xd2c.google.com with SMTP id ca18e2360f4ac-777b922466dso297675939f.3 for ; Mon, 12 Jun 2023 12:36:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1686598593; x=1689190593; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=og38m1GmQa0fR6A4d8GpMudCauHO6diTwrleqS9cddo=; b=jEREAExr4RcIWVU+Lfx78iIMPTQIKpX/u/qTBktBvZ1s30rrrQWvmhd+Bni+HxN4Po 5iwKynmNw3pvgsU2aYqk/JVoj3XVth+QKKKZMLGO0F2GSBDbYXfO4nToCFw0g/kkmboQ SZ8tDJs0kvnjd/sJDWTAS3+Pt7iTLE5HHPmRExSQ5qL2rfwnznrMGo/ULrVN2yVNvRWN kqtq/B6Bu2FGGzIOQDS6XtEKyQbfa+eC55XRMCWbBBWKOYCRrqWPkMwKd8QqDIrKBqXj XOmQ2SUP2L7Ch+YE9OHc7Ym2vhixUrBXydKv5z4Wh88c7bKK4EwWeV60muV/ac0lGRse WWmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686598593; x=1689190593; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=og38m1GmQa0fR6A4d8GpMudCauHO6diTwrleqS9cddo=; b=l/yDAPNrYM/ow8ikTITcIV+3umfGpQiyzasf06mk6+r5IVZ0I3X3+52tozLruWNdm0 bFdUvHXLjIjK4fleRsK45iF158+XPn5Hr5BjGdYEna3iZ6RJHlu71900NIXrn5G0uxwA aEuW/S2bYr+G5UXE7ZL4gVrGC+q3r/kBhHEPsr9GZcXU1hAa0rEFR8U/zQXfv5jgFqzG io2BPnKIYslMjiFERMhcSUBR0abZJ359MGvZpHCsPpKeMTvm+gruniCaIxlKbn+GmV5A 9PkPHnMWTxeYwFRh73/2VlAYk/friGeiiTnJt9pMOqXS7CvLwBNbJEUCR3vS0n759vTD oOGA== X-Gm-Message-State: AC+VfDzSoBqu5xR0thBiyzm4ie5K55BN8iVwzmd9C0AKyjoAyd+ffUIB btxOo4qHqFRugvc72HaigiRpaTaSh8TVJsYUm5UHpA== X-Google-Smtp-Source: ACHHUZ6tp1Qe0OpzwwIFNV24/f9ETz4HRBjFMcqZHR/1BEqm1DDmp8LxCLQNMoxqyUevrtEBZlObdg== X-Received: by 2002:a6b:5b13:0:b0:77a:e86a:c24b with SMTP id v19-20020a6b5b13000000b0077ae86ac24bmr7228497ioh.6.1686598592765; Mon, 12 Jun 2023 12:36:32 -0700 (PDT) Received: from localhost.localdomain (75-166-136-83.hlrn.qwest.net. [75.166.136.83]) by smtp.gmail.com with ESMTPSA id m12-20020a056638224c00b00418a5e0e93esm2938091jas.162.2023.06.12.12.36.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 12:36:32 -0700 (PDT) From: Tom Tromey Date: Mon, 12 Jun 2023 13:36:31 -0600 Subject: [PATCH 4/7] Implement type checking for DAP breakpoint requests MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230612-more-dap-v1-4-ad868f1a4cc0@adacore.com> References: <20230612-more-dap-v1-0-ad868f1a4cc0@adacore.com> In-Reply-To: <20230612-more-dap-v1-0-ad868f1a4cc0@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: I realized that with a small refactoring, it is possible to type-check the parameters to the various DAP breakpoint requests. This would have caught the earlier bug involving hitCondition. --- gdb/python/lib/gdb/dap/breakpoint.py | 128 ++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 46 deletions(-) diff --git a/gdb/python/lib/gdb/dap/breakpoint.py b/gdb/python/lib/gdb/dap/breakpoint.py index 3f40cfb7f63..227085ad73e 100644 --- a/gdb/python/lib/gdb/dap/breakpoint.py +++ b/gdb/python/lib/gdb/dap/breakpoint.py @@ -143,19 +143,28 @@ def _set_breakpoints(kind, specs): return _set_breakpoints_callback(kind, specs, gdb.Breakpoint) -# Turn a DAP SourceBreakpoint, FunctionBreakpoint, or -# InstructionBreakpoint into a "spec" that is used by -# _set_breakpoints. SPEC is a dictionary of parameters that is used -# as the base of the result; it is modified in place. -def _basic_spec(bp_info, spec): - for name in ("condition", "hitCondition"): - if name in bp_info: - spec[name] = bp_info[name] - return spec +# A helper function that rewrites a SourceBreakpoint into the internal +# form passed to the creator. This function also allows for +# type-checking of each SourceBreakpoint. +@type_check +def _rewrite_src_breakpoint( + *, + # This is a Source but we don't type-check it. + source, + line: int, + condition: Optional[str] = None, + hitCondition: Optional[str] = None, + **args, +): + return { + "source": source["path"], + "line": line, + "condition": condition, + "hitCondition": hitCondition, + } # FIXME we do not specify a type for 'source'. -# FIXME 'breakpoints' is really a list[SourceBreakpoint]. @request("setBreakpoints") @capability("supportsHitConditionalBreakpoints") @capability("supportsConditionalBreakpoints") @@ -163,17 +172,7 @@ def set_breakpoint(*, source, breakpoints: Sequence = (), **args): if "path" not in source: result = [] else: - specs = [] - for obj in breakpoints: - specs.append( - _basic_spec( - obj, - { - "source": source["path"], - "line": obj["line"], - }, - ) - ) + specs = [_rewrite_src_breakpoint(source=source, **bp) for bp in breakpoints] # Be sure to include the path in the key, so that we only # clear out breakpoints coming from this same source. key = "source:" + source["path"] @@ -183,45 +182,64 @@ def set_breakpoint(*, source, breakpoints: Sequence = (), **args): } +# A helper function that rewrites a FunctionBreakpoint into the +# internal form passed to the creator. This function also allows for +# type-checking of each FunctionBreakpoint. +@type_check +def _rewrite_fn_breakpoint( + *, + name: str, + condition: Optional[str] = None, + hitCondition: Optional[str] = None, + **args, +): + return { + "function": name, + "condition": condition, + "hitCondition": hitCondition, + } + + @request("setFunctionBreakpoints") @capability("supportsFunctionBreakpoints") def set_fn_breakpoint(*, breakpoints: Sequence, **args): - specs = [] - for bp in breakpoints: - specs.append( - _basic_spec( - bp, - { - "function": bp["name"], - }, - ) - ) + specs = [_rewrite_fn_breakpoint(**bp) for bp in breakpoints] result = send_gdb_with_response(lambda: _set_breakpoints("function", specs)) return { "breakpoints": result, } +# A helper function that rewrites an InstructionBreakpoint into the +# internal form passed to the creator. This function also allows for +# type-checking of each InstructionBreakpoint. +@type_check +def _rewrite_insn_breakpoint( + *, + instructionReference: str, + offset: Optional[int] = None, + condition: Optional[str] = None, + hitCondition: Optional[str] = None, + **args, +): + # There's no way to set an explicit address breakpoint from + # Python, so we rely on "spec" instead. + val = "*" + instructionReference + if offset is not None: + val = val + " + " + str(offset) + return { + "spec": val, + "condition": condition, + "hitCondition": hitCondition, + } + + @request("setInstructionBreakpoints") @capability("supportsInstructionBreakpoints") def set_insn_breakpoints( *, breakpoints: Sequence, offset: Optional[int] = None, **args ): - specs = [] - for bp in breakpoints: - # There's no way to set an explicit address breakpoint - # from Python, so we rely on "spec" instead. - val = "*" + bp["instructionReference"] - if offset is not None: - val = val + " + " + str(offset) - specs.append( - _basic_spec( - bp, - { - "spec": val, - }, - ) - ) + specs = [_rewrite_insn_breakpoint(**bp) for bp in breakpoints] result = send_gdb_with_response(lambda: _set_breakpoints("instruction", specs)) return { "breakpoints": result, @@ -249,6 +267,23 @@ def _set_exception_catchpoints(filter_options): return _set_breakpoints_callback("exception", filter_options, _catch_exception) +# A helper function that rewrites an ExceptionFilterOptions into the +# internal form passed to the creator. This function also allows for +# type-checking of each ExceptionFilterOptions. +@type_check +def _rewrite_exception_breakpoint( + *, + filterId: str, + condition: Optional[str] = None, + # Note that exception breakpoints do not support a hit count. + **args, +): + return { + "filterId": filterId, + "condition": condition, + } + + @request("setExceptionBreakpoints") @capability("supportsExceptionFilterOptions") @capability( @@ -272,6 +307,7 @@ def set_exception_breakpoints( # Convert the 'filters' to the filter-options style. options = [{"filterId": filter} for filter in filters] options.extend(filterOptions) + options = [_rewrite_exception_breakpoint(**bp) for bp in options] result = send_gdb_with_response(lambda: _set_exception_catchpoints(options)) return { "breakpoints": result, -- 2.40.1