From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 4D0323858C50 for ; Tue, 29 Mar 2022 21:28:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4D0323858C50 Received: by mail-lf1-x131.google.com with SMTP id w7so32529912lfd.6 for ; Tue, 29 Mar 2022 14:28:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=aoGNKAsumGrlQIHjDh2XyaS8bWfukW28ou7i4KZdcd8=; b=s8OxVTtANcmo7gVrZjEUqqtZ70l0bu5Vj8MLb3u3hzZsKNp8pGUdvVpkS8retuXWth I8MSJKncrzDjvjr0woladpf9LjJXNC17O4BuRxf/kAR1TZjHvuuzC440nWXe+o5wfcXS 8bmJ1VdbmNk7u/sawu/H1/utKMT3SFLp6bdHipv6a7y0j4UwAiHuvsoFQWOvOrxkB/aP 0h1r5ypoloJ/LcI1iC3UNnWxMk1sHanAlv4LYcgXvk10cmwKPYsNoA7qjAmzpwNb+Z42 FZ8lyRvjk5b2G97YsgZfNB356X63pm53w8JZhy0oDUrMmXDT6dVVNhTlG4Psn58lbXip 9AhQ== X-Gm-Message-State: AOAM5311lEPRxEKo//MO4yXs3NOekjM6mZnp9x5UMsGj2ZEhewxUUjoV ERuGbkCMXI7mlrPa0VFXhxpyRE0UxBg= X-Google-Smtp-Source: ABdhPJz4Yw6y+l1NkSwE/ru8REq1r+12Gc4A4ehJQTLZAmFPeq8TPA79qirbTsa+mPklTCJB8Y8AlA== X-Received: by 2002:a05:6512:2250:b0:44a:56d2:c24e with SMTP id i16-20020a056512225000b0044a56d2c24emr4326806lfu.262.1648589302707; Tue, 29 Mar 2022 14:28:22 -0700 (PDT) Received: from cx-desktop.. (78-73-77-63-no2450.tbcn.telia.com. [78.73.77.63]) by smtp.gmail.com with ESMTPSA id bj15-20020a2eaa8f000000b00246233c02a5sm2182971ljb.44.2022.03.29.14.28.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Mar 2022 14:28:22 -0700 (PDT) From: Simon Farre To: gdb-patches@sourceware.org Cc: Simon Farre Subject: [PATCH 1/2] gdb/python: add gdb.Breakpoint.source_locations property Date: Tue, 29 Mar 2022 23:27:16 +0200 Message-Id: <20220329212717.1184271-1-simon.farre.cx@gmail.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 Mar 2022 21:28:26 -0000 This attribute returns the source locations where this breakpoint was installed. It's returned as a list of tuples of type (file path: string, line: long). Currently it's not possible from the Python API to know where a breakpoint has been installed. The rationale for this patch is for IDE or debug adapters as they are called, for instance for VSCode, to be able to fetch Breakpoint related data directly from Python, but also makes it possible to get this data for plugins without having the parse the string output of the command `info breakpoints`. --- gdb/NEWS | 5 +++++ gdb/doc/python.texi | 8 ++++++++ gdb/python/py-breakpoint.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index e10062752d0..9e7e41d3bd6 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -16,6 +16,11 @@ This is the same format that GDB uses when printing address, symbol, and offset information from the disassembler. + ** Added attribute Breakpoint.source_locations + This returns a list of (file path, line number) tuples where the + Breakpoint was installed. This is the information displayed + by the 'info breakpoints' command. + *** Changes in GDB 12 * DBX mode is deprecated, and will be removed in GDB 13 diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 7c414b01d70..0d842af6229 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -6066,6 +6066,14 @@ commands, separated by newlines. If there are no commands, this attribute is @code{None}. This attribute is writable. @end defvar +@defvar Breakpoint.source_locations +This attribute holds the source locations where the breakpoint installed. +It returns a list of tuples containing the file path and the line number. +If the breakpoint has no source location(s), either because it's a pending +breakpoint or is of a type that doesn't have a source location related to it +it returns @code{None}. This attribute is not writable. +@end defvar + @node Finish Breakpoints in Python @subsubsection Finish Breakpoints diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index 5891c3a9735..1b9ac6957a5 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -692,6 +692,35 @@ bppy_get_ignore_count (PyObject *self, void *closure) return gdb_py_object_from_longest (self_bp->bp->ignore_count).release (); } +/* Python function to get all source locations where this breakpoint is set. + returns a list of tuples containing the file path and the line number. */ + +static PyObject* bppy_get_source_locations(PyObject *self, void* closure) +{ + gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; + struct breakpoint* bp = self_bp->bp; + + BPPY_REQUIRE_VALID (self_bp); + + if (!is_breakpoint (bp) || pending_breakpoint_p(bp)) + Py_RETURN_NONE; + + gdbpy_ref<> list (PyList_New (0)); + + for (const bp_location* it = bp->loc; it != nullptr; it = it->next) + { + PyObject* tuple = PyTuple_New(2); + if(PyTuple_SetItem(tuple, 0, host_string_to_python_string (it->symtab->filename).release ()) != 0) + return nullptr; + if(PyTuple_SetItem(tuple, 1, PyLong_FromLong(it->line_number)) != 0) + return nullptr; + if(PyList_Append(list.get(), tuple) != 0) + return nullptr; + } + + return list.release(); +} + /* Internal function to validate the Python parameters/keywords provided to bppy_init. */ @@ -1264,6 +1293,8 @@ or None if no condition set."}, "Whether this breakpoint is a temporary breakpoint."}, { "pending", bppy_get_pending, NULL, "Whether this breakpoint is a pending breakpoint."}, + { "source_locations", bppy_get_source_locations, NULL, + "Source file locations where this breakpoint is installed"}, { NULL } /* Sentinel. */ }; base-commit: b8e92c571baed4e794bd62b7bf417fa8bbaf5c95 -- 2.32.0