From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id CD0D03857BB1 for ; Tue, 31 May 2022 14:01:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CD0D03857BB1 Received: by mail-il1-x12e.google.com with SMTP id h18so3529720ilj.7 for ; Tue, 31 May 2022 07:01:03 -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:references:date:in-reply-to :message-id:user-agent:mime-version; bh=cXcMztQ0G+q6lRWaIkjNG9vkFl1L+Sl0B5u+9yqbkFw=; b=c6HTJ0aBtLWAnPzqaaQa26vMtR4FSBhy6qOEksleMWMx1bmYwf47XgR9GGMeELSax8 ZoDsaPlkmzK5xJXppy+uNMNolm36k8OfDZI/5JdzbkHL9QjTFBDkpmqFS18+1tEcW1A6 Wb2OTqd/0EGhUopKfnzIJF0Z470tUZeZQiwDihp0lPA414Pd4T1VcAu1IP7GEnK+kBZI rF/XM81GFCheVRaYxPncdz0yDGMtdtGpqIgPors6dvn359DNUeYgr0PRjUCp4JYt1K8F B3GS6sALyPXShGjHNF6wJL/92Sv0gCmiVnUWNXAsZ2HYGbxfxCCxzQ+xZGBPSxzQZXhM /9yg== X-Gm-Message-State: AOAM5337s/T9GbhjSOYt1vcPAe5AUhVox/8L8C7pdh2f9d2fhL32uVyU ApKz54QZyLuxrO3r64i5iIRTGw== X-Google-Smtp-Source: ABdhPJxJ3NoCjuS3po+Vjdp1P/u6zWcNobpPaPoIwI+INUsqfDcPLfLODAAar7KvEiVhp9BBMViHtg== X-Received: by 2002:a05:6e02:180f:b0:2d1:3d44:8fcd with SMTP id a15-20020a056e02180f00b002d13d448fcdmr32804040ilv.0.1654005663003; Tue, 31 May 2022 07:01:03 -0700 (PDT) Received: from murgatroyd (71-211-167-178.hlrn.qwest.net. [71.211.167.178]) by smtp.gmail.com with ESMTPSA id m3-20020a056638224300b0032b3a7817b3sm3528957jas.119.2022.05.31.07.01.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 07:01:02 -0700 (PDT) From: Tom Tromey To: Andrew Burgess Cc: Tom Tromey via Gdb-patches , Tom Tromey Subject: Re: [PATCH] Add gdb.current_language function References: <20220527173642.1180218-1-tromey@adacore.com> <871qwbfjwu.fsf@redhat.com> X-Attribution: Tom Date: Tue, 31 May 2022 08:01:00 -0600 In-Reply-To: <871qwbfjwu.fsf@redhat.com> (Andrew Burgess's message of "Mon, 30 May 2022 10:29:21 +0100") Message-ID: <87ilpl6btv.fsf@tromey.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-11.7 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 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, 31 May 2022 14:01:10 -0000 Andrew> I think you should also add Frame.current_lanaguage, and the docs for Andrew> gdb.current_lanaguage should make it clear that the Frame method should Andrew> be used in preference where a Frame is available. Here's v2 with this addition. Tom commit dba44c7ff83db6ac80377b5a42ce75cf5557347f Author: Tom Tromey Date: Tue May 24 10:15:17 2022 -0600 Add gdb.current_language and gdb.Frame.language This adds the gdb.current_language function, which can be used to find the current language without (1) ever having the value "auto" or (2) having to parse the output of "show language". It also adds the gdb.Frame.language, which can be used to find the language of a given frame. This is normally preferable if one has a Frame object handy. diff --git a/gdb/NEWS b/gdb/NEWS index dac6dabfa42..960f90b4387 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -58,6 +58,13 @@ maintenance info line-table This is the same format that GDB uses when printing address, symbol, and offset information from the disassembler. + ** New function gdb.current_language that returns the name of the + current language. Unlike gdb.parameter('language'), this will + never return 'auto'. + + ** New method gdb.Frame.language that returns the name of the + frame's language. + *** 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 f933c7d30c9..ba5a9b315e1 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -668,6 +668,14 @@ Here are some examples of the possible string formats: @end smallexample @end defun +@defun gdb.current_language () +Return the name of the current language as a string. Unlike +@code{gdb.parameter('language')}, this function will never return +@samp{auto}. If a @code{gdb.Frame} object is available (@pxref{Frames +In Python}), the @code{language} method might be preferable in some +cases, as that is not affected by the user's language setting. +@end defun + @node Exception Handling @subsubsection Exception Handling @cindex python exceptions @@ -5202,6 +5210,10 @@ Stack}. Return an integer, the stack frame level for this frame. @xref{Frames, ,Stack Frames}. @end defun +@defun Frame.language () +Return a string, the source language for this frame. +@end defun + @node Blocks In Python @subsubsection Accessing blocks from Python diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index 769e28c1a2c..9a28c36c1cc 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -598,6 +598,29 @@ frapy_level (PyObject *self, PyObject *args) Py_RETURN_NONE; } +/* The language for this frame. */ + +static PyObject * +frapy_language (PyObject *self, PyObject *args) +{ + try + { + struct frame_info *fi; + FRAPY_REQUIRE_VALID (self, fi); + + enum language lang = get_frame_language (fi); + const language_defn *lang_def = language_def (lang); + + return host_string_to_python_string (lang_def->name ()).release (); + } + catch (const gdb_exception &except) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + + Py_RETURN_NONE; +} + /* Implementation of gdb.newest_frame () -> gdb.Frame. Returns the newest frame object. */ @@ -771,6 +794,8 @@ Return the value of the variable in this frame." }, "Select this frame as the user's current frame." }, { "level", frapy_level, METH_NOARGS, "The stack level of this frame." }, + { "language", frapy_language, METH_NOARGS, + "The language of this frame." }, {NULL} /* Sentinel */ }; diff --git a/gdb/python/python.c b/gdb/python/python.c index 11aaa7ae778..9bef2252e88 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1571,6 +1571,14 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2) return list.release (); } +/* Return the name of the current language. */ + +static PyObject * +gdbpy_current_language (PyObject *unused1, PyObject *unused2) +{ + return host_string_to_python_string (current_language->name ()).release (); +} + /* The "current" objfile. This is set when gdb detects that a new @@ -2534,6 +2542,10 @@ Format ADDRESS, an address within PROG_SPACE, a gdb.Progspace, using\n\ ARCH, a gdb.Architecture to determine the address size. The format of\n\ the returned string is 'ADDRESS ' without the quotes." }, + { "current_language", gdbpy_current_language, METH_NOARGS, + "current_language () -> string\n\ +Return the name of the currently selected language." }, + {NULL, NULL, 0, NULL} }; diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp index b91ffe62a83..4991e8a0c5d 100644 --- a/gdb/testsuite/gdb.python/py-frame.exp +++ b/gdb/testsuite/gdb.python/py-frame.exp @@ -128,3 +128,9 @@ if { $pc != "" } { " = True" \ "test Frame.read_register($pc)" } + +# Test language. +gdb_test "python print(gdb.selected_frame().language())" "c" +gdb_test "set language ada" +gdb_test "python print(gdb.selected_frame().language())" "c" \ + "frame language is not affected by global language" diff --git a/gdb/testsuite/gdb.python/py-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp index 199d3bc16ec..db158ddec26 100644 --- a/gdb/testsuite/gdb.python/py-parameter.exp +++ b/gdb/testsuite/gdb.python/py-parameter.exp @@ -370,6 +370,19 @@ proc_with_prefix test_throwing_parameter { } { "gdb.GdbError does not show Python stack" } +proc_with_prefix test_language {} { + gdb_test "python print(gdb.parameter('language'))" "auto" \ + "print language parameter" + gdb_test "python print(gdb.current_language())" "c" \ + "print current language" + gdb_test_no_output "set lang rust" + gdb_test "python print(gdb.parameter('language'))" "rust" \ + "print language parameter for rust" + gdb_test "python print(gdb.current_language())" "rust" \ + "print current language for rust" + gdb_test_no_output "set lang auto" +} + test_directories test_data_directory test_boolean_parameter @@ -380,6 +393,7 @@ test_really_undocumented_parameter test_deprecated_api_parameter test_integer_parameter test_throwing_parameter +test_language # This caused a gdb crash. gdb_test "python print(gdb.parameter('endian'))" "auto" \ diff --git a/gdb/testsuite/gdb.rust/pp.exp b/gdb/testsuite/gdb.rust/pp.exp index 7c7c78b5847..e3e226c5db5 100644 --- a/gdb/testsuite/gdb.rust/pp.exp +++ b/gdb/testsuite/gdb.rust/pp.exp @@ -40,3 +40,5 @@ if {![runto ${srcfile}:$line]} { gdb_test "print outer" " = pp::Outer \\(x\\(5\\)\\)" gdb_test "print outer.0" " = x\\(5\\)" + +gdb_test "python print(gdb.selected_frame().language())" "rust"