From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x235.google.com (mail-lj1-x235.google.com [IPv6:2a00:1450:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id A569D3858C54 for ; Tue, 16 Jan 2024 19:13:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A569D3858C54 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A569D3858C54 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::235 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705432419; cv=none; b=ZWfM9bTl2pU0R+yfJiEwkIHXdM2pdDKajMPl60tJbfFhYdXTFgeUY7+0rgvJcykitRct9/le0qFYmHc9vlyRlMO5e9S+f0jYf6U1dXrekydbcMtiOdpXKiyvHuOQzhVuWFuZLi8ZZyraIEWW74dGBD3kPxq3dxYe4zLWiiAn6QQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705432419; c=relaxed/simple; bh=00P1iux/rL+v5jGbOX/i8b45g8xu0wNuvZ7TcqAVKKY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=pqtU/0gyFR08+Agnn1DhZhSXN/ID4FFMWhRHXDDqk3E6AMk5AtOkSXSj2A0dJ/Kt7JzTb2oqs5X+3A954Fx6xvI0a087XjTkT3HWteKHleUfVFilUfAHGyMwLtaw2XIX7wwz5jkUXSi/fxAifnzM2L2Pe5sk6kEUSVtQ905ohbs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lj1-x235.google.com with SMTP id 38308e7fff4ca-2cd46e7ae8fso113328031fa.1 for ; Tue, 16 Jan 2024 11:13:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705432414; x=1706037214; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Cy4c7tPLdXCQDeJCZzQBur4gAqCVSVcvAolR58YhTIE=; b=KVG+/0gmR8fOWbTNAL6ZyVu4jqRxzU3lMbhDihpfPwmhL2WJm9NL0ssdkFaXnTnfha V6jeszICRmwNA+kVgNGRqGQPNrpxS6SIAdMXm36tq1qskql76VSkHrvuHG8LlrraQbnI A931YdwjX86nrF5uHB4ERpWCOUv8oxNXcHyOuNX5FNqEEUn+Tsi+hWDta1aCazEsdHMF H7WDFEicg3H7ECGJ2tF9Rqiexx2elBYav5sUP95rQqg0nqF5XOeN+o7NzhkCuL+Q1Smg /4BG+/zjc4PQtqs4jw6YdA+0tP9AWTGeApg4oS98ZB7UDGEsu8GqFeKi4ChGQfy0D+nL oUtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705432414; x=1706037214; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Cy4c7tPLdXCQDeJCZzQBur4gAqCVSVcvAolR58YhTIE=; b=hcBUZIe3FGjBBuz2B65Z6BJDd5ha5uEh/R++beaL6rGtcMPB3vp2Hq8hzzYXpIJI7T NTHjO1SoF0khA9XGRdunAVxljrolC/ZvM5uHcCbPU5PICClKsJpiWLNEVKHhxdSZMRgQ Mwgm/GRQbH2F+UnOEdC1jo5+s03yhpM+qMR6atF7zqHQ9HDYGvP+9G/+MWi1IlVkb0yX mZoKW83GQBb+wE2rrzWyl5L2bKUQmxF/DelCrQhNiLQ59KI5x7pEtf4JoITw+MjSyt6v YpNndA3X8ez2OCA/+ODKqavRjdgtmVwRfismtoPrEf1u/UG3VYThTyBj/Fo5iVxvnul7 Z5jw== X-Gm-Message-State: AOJu0YweNVeeVbzHeyQM7OicmP6B+eg45LXoeF7q+8UCrE4I7GyWBUVT eJSjpcScc9KGWPaDoTRue4YiW1a1VpY= X-Google-Smtp-Source: AGHT+IFWfe+enDKy0A6g6MYU+OP440plLNXER6Q2QT9hlAuPsV0kFQyjIQe57wXKW7tS0UISVWb0RA== X-Received: by 2002:a2e:9587:0:b0:2cc:7b1e:3643 with SMTP id w7-20020a2e9587000000b002cc7b1e3643mr1709321ljh.192.1705432414455; Tue, 16 Jan 2024 11:13:34 -0800 (PST) Received: from localhost.localdomain ([2001:2044:7c:4b00:37f8:fc67:7f51:113d]) by smtp.gmail.com with ESMTPSA id u1-20020a2eb801000000b002cdc84fdef9sm630413ljo.89.2024.01.16.11.13.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jan 2024 11:13:34 -0800 (PST) From: Simon Farre To: gdb-patches@sourceware.org Cc: Simon Farre Subject: [PATCH 3/5] [dap & linetable]: Change gdb.LineTableEntry & Add gdb.lookup_linetable Date: Tue, 16 Jan 2024 20:13:03 +0100 Message-ID: <20240116191305.126345-3-simon.farre.cx@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116191305.126345-1-simon.farre.cx@gmail.com> References: <20240116191305.126345-1-simon.farre.cx@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.5 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This patch changes the Python type `LineTableEntry` by adding the `column` accessor which returns the newly added `column` value from the internal linetable_entry. It also exposes a new "module function": gdb.lookup_linetable which searches all symbtabs using a string; comparing it with the symtabs 'fullname' and returning the linetable of the (first) symtab that matches. To search for a linetable from python: lt = gdb.lookup_linetable("main.cpp") And like before (as per current documentation), one can use that like so: for lte in lt: print(f"line:col={lte.line}:{lte.column}; pc = {hex(lte.pc)}") --- gdb/python/py-linetable.c | 35 ++++++++++++++++++++++++----------- gdb/python/py-symtab.c | 33 +++++++++++++++++++++++++++++++++ gdb/python/python-internal.h | 2 ++ gdb/python/python.c | 2 ++ gdb/symtab.c | 19 ++++++++++++++++++- gdb/symtab.h | 3 +++ 6 files changed, 82 insertions(+), 12 deletions(-) diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c index 788a6e1e24b..ba96c663027 100644 --- a/gdb/python/py-linetable.c +++ b/gdb/python/py-linetable.c @@ -24,6 +24,8 @@ struct linetable_entry_object { PyObject_HEAD /* The line table source line. */ int line; + /* The line table source column. */ + int col; /* The pc associated with the source line. */ CORE_ADDR pc; }; @@ -99,7 +101,7 @@ symtab_to_linetable_object (PyObject *symtab) and an address. */ static PyObject * -build_linetable_entry (int line, CORE_ADDR address) +build_linetable_entry (int line, int col, CORE_ADDR address) { linetable_entry_object *obj; @@ -109,6 +111,7 @@ build_linetable_entry (int line, CORE_ADDR address) { obj->line = line; obj->pc = address; + obj->col = col; } return (PyObject *) obj; @@ -121,7 +124,7 @@ build_linetable_entry (int line, CORE_ADDR address) address. */ static PyObject * -build_line_table_tuple_from_pcs (int line, const std::vector &pcs) +build_line_table_tuple_from_pcs (int line, const symtab& sym, const std::vector &pcs) { int i; @@ -133,10 +136,11 @@ build_line_table_tuple_from_pcs (int line, const std::vector &pcs) if (tuple == NULL) return NULL; + const auto of = sym.compunit ()->objfile (); for (i = 0; i < pcs.size (); ++i) { - CORE_ADDR pc = pcs[i]; - gdbpy_ref<> obj (build_linetable_entry (line, pc)); + const auto& lte = pcs[i]; + gdbpy_ref<> obj (build_linetable_entry (line, lte.col, lte.pc (of))); if (obj == NULL) return NULL; @@ -152,12 +156,10 @@ build_line_table_tuple_from_pcs (int line, const std::vector &pcs) in the line table. */ static PyObject * -ltpy_get_pcs_for_line (PyObject *self, PyObject *args) +ltpy_get_ltes_for_line (PyObject *self, PyObject *args) { struct symtab *symtab; gdb_py_longest py_line; - const linetable_entry *best_entry = nullptr; - std::vector pcs; LTPY_REQUIRE_VALID (self, symtab); @@ -166,14 +168,15 @@ ltpy_get_pcs_for_line (PyObject *self, PyObject *args) try { - pcs = find_pcs_for_symtab_line (symtab, py_line, &best_entry); + const auto ltes = find_ltes_for_symtab_line (*symtab, py_line); + return build_line_table_tuple_from_pcs (py_line, *symtab, ltes); } catch (const gdb_exception &except) { GDB_PY_HANDLE_EXCEPTION (except); } - return build_line_table_tuple_from_pcs (py_line, pcs); + Py_RETURN_NONE; } /* Implementation of gdb.LineTable.has_line (self, line) -> Boolean. @@ -327,6 +330,14 @@ ltpy_entry_get_line (PyObject *self, void *closure) return gdb_py_object_from_longest (obj->line).release (); } +static PyObject * +ltpy_entry_get_column (PyObject *self, void *closure) +{ + linetable_entry_object *obj = (linetable_entry_object *) self; + + return gdb_py_object_from_longest (obj->col).release (); +} + /* Implementation of gdb.LineTableEntry.pc (self) -> Long. Returns a a long integer associated with the PC of the line table entry. */ @@ -422,7 +433,7 @@ ltpy_iternext (PyObject *self) } struct objfile *objfile = symtab->compunit ()->objfile (); - obj = build_linetable_entry (item->line, item->pc (objfile)); + obj = build_linetable_entry (item->line, item->col, item->pc (objfile)); iter_obj->current_index++; return obj; @@ -451,7 +462,7 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_linetable); static PyMethodDef linetable_object_methods[] = { - { "line", ltpy_get_pcs_for_line, METH_VARARGS, + { "line", ltpy_get_ltes_for_line, METH_VARARGS, "line (lineno) -> Tuple\n\ Return executable locations for a given source line." }, { "has_line", ltpy_has_line, METH_VARARGS, @@ -548,6 +559,8 @@ PyTypeObject ltpy_iterator_object_type = { static gdb_PyGetSetDef linetable_entry_object_getset[] = { { "line", ltpy_entry_get_line, NULL, "The line number in the source file.", NULL }, + { "column", ltpy_entry_get_column, NULL, + "The column number in the source file", NULL }, { "pc", ltpy_entry_get_pc, NULL, "The memory address for this line number.", NULL }, { NULL } /* Sentinel */ diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c index b28f6ca8b48..e23c02aa0e4 100644 --- a/gdb/python/py-symtab.c +++ b/gdb/python/py-symtab.c @@ -475,6 +475,39 @@ symtab_to_symtab_object (struct symtab *symtab) return (PyObject *) symtab_obj; } +/* Search for a symtab whose fullname contains FILENAME and return it's + linetable. */ +PyObject * +gdbpy_lookup_linetable_by_filename (PyObject *, PyObject *args, PyObject *kw) +{ + const char *arg = nullptr; + static const char *keywords[] = { "filename", nullptr }; + + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords, &arg)) + { + return nullptr; + } + + for (objfile *objfile : current_program_space->objfiles ()) + for (compunit_symtab *cu : objfile->compunits ()) + for (symtab *s : cu->filetabs ()) + { + const auto name = symtab_to_fullname (s); + + if (strstr (name, arg)) + { + auto symtab_pyobj = symtab_to_symtab_object (s); + if (!symtab_pyobj) + Py_RETURN_NONE; + auto lt_obj = symtab_to_linetable_object (symtab_pyobj); + if (!lt_obj) + Py_RETURN_NONE; + return lt_obj; + } + } + Py_RETURN_NONE; +} + /* Create a new symtab and line (gdb.Symtab_and_line) object that encapsulates the symtab_and_line structure from GDB. */ PyObject * diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 7c05007cbab..dcfc03e3beb 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -408,6 +408,8 @@ PyObject *gdbpy_convenience_variable (PyObject *self, PyObject *args); PyObject *gdbpy_set_convenience_variable (PyObject *self, PyObject *args); PyObject *gdbpy_breakpoints (PyObject *, PyObject *); PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *); +PyObject *gdbpy_lookup_linetable_by_filename (PyObject *, PyObject* args, + PyObject *kw); PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw); PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw); diff --git a/gdb/python/python.c b/gdb/python/python.c index 1d406392bd3..3d7c70fed09 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2573,6 +2573,8 @@ gdbpy_initialized (const struct extension_language_defn *extlang) PyMethodDef python_GdbMethods[] = { + { "lookup_linetable", (PyCFunction) gdbpy_lookup_linetable_by_filename, + METH_VARARGS | METH_KEYWORDS }, { "history", gdbpy_history, METH_VARARGS, "Get a value from history" }, { "add_history", gdbpy_add_history, METH_VARARGS, diff --git a/gdb/symtab.c b/gdb/symtab.c index 7c0a69108d4..e206ba9cada 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -3519,7 +3519,24 @@ find_pcs_for_symtab_line (struct symtab *symtab, int line, return result; } - +/** Return line table entries for SYMTAB at LINE. */ + +std::vector +find_ltes_for_symtab_line (const symtab &symtab, int line) +{ + auto lte = symtab.linetable (); + if (lte == nullptr) + return {}; + + std::vector result; + for (auto i = 0; i < lte->nitems; ++i) + { + if (lte->item[i].line == line) + result.push_back (lte->item[i]); + } + return result; +} + /* Set the PC value for a given source file and line number and return true. Returns false for invalid line number (and sets the PC to 0). The source file is specified with a struct symtab. */ diff --git a/gdb/symtab.h b/gdb/symtab.h index 38d08fe8599..9098822c90b 100644 --- a/gdb/symtab.h +++ b/gdb/symtab.h @@ -2772,6 +2772,9 @@ void iterate_over_symtabs (const char *name, std::vector find_pcs_for_symtab_line (struct symtab *symtab, int line, const linetable_entry **best_entry); +std::vector find_ltes_for_symtab_line + (const symtab &symtab, int line); + /* Prototype for callbacks for LA_ITERATE_OVER_SYMBOLS. The callback is called once per matching symbol SYM. The callback should return true to indicate that LA_ITERATE_OVER_SYMBOLS should continue -- 2.43.0