From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id 31004382CBAB for ; Fri, 27 May 2022 17:36:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 31004382CBAB Received: by mail-il1-x12d.google.com with SMTP id g15so773414ilr.3 for ; Fri, 27 May 2022 10:36:46 -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=rPXR+ikofAJ+wgA3I6Ou+xJCI4MVy7DLiyYqioYKEgY=; b=IPYmUyPwE0xdL67bPJjG7LdLz54CDFR2TUitskp9vYbm085aobgHCaGL5j+77JfarM krvvtHX0G3+BUV3H9yA6S0s0i0yzDF9/MboGl/tFuUgWwry4oegX6podVWZnmKc8pGJg hIu8xVmdBZ0wjl+L8ZbayUdX63SJq7W/3paM0zNzAl8T5tzbKLtLOrlPxJIVrO2EtZ0D vEyJHx3DP7ryzY3EHyRiL8oYfxwHWPHmweet5fx3VzrTlK4CBA+6apyw3liLYRbtrJV4 kIMBYfXZPbOwLKO54e12UFgaYywCmm95y9fl8awO2ADi/F8ZjrZiZUBPnT8sX5mcttWh yZTw== X-Gm-Message-State: AOAM5306PeCaqFDN+rv3A023RuEne3parFPAJ4/J7lMQshD1ic6tQaUn kttNfqFzGQE/fxVcDgihA0f+QLE3LMa1rw== X-Google-Smtp-Source: ABdhPJw+pgBshUYp92yPEU2VuwDSS2NGbNx/q87nBWQqq30GdYuz9MOU7jGawwQ8hSVuo6MIfHB/WA== X-Received: by 2002:a92:3f0a:0:b0:2cf:7745:d38 with SMTP id m10-20020a923f0a000000b002cf77450d38mr22571866ila.289.1653673005295; Fri, 27 May 2022 10:36:45 -0700 (PDT) Received: from murgatroyd.Home (71-211-158-194.hlrn.qwest.net. [71.211.158.194]) by smtp.gmail.com with ESMTPSA id q2-20020a927502000000b002d1bc2eb604sm1460760ilc.58.2022.05.27.10.36.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 May 2022 10:36:44 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Add gdb.current_language function Date: Fri, 27 May 2022 11:36:42 -0600 Message-Id: <20220527173642.1180218-1-tromey@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.6 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: Fri, 27 May 2022 17:36:48 -0000 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". --- gdb/NEWS | 4 ++++ gdb/doc/python.texi | 6 ++++++ gdb/python/python.c | 12 ++++++++++++ gdb/testsuite/gdb.python/py-parameter.exp | 13 +++++++++++++ 4 files changed, 35 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index 52ffdc4c83a..2e87f8d56f3 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -51,6 +51,10 @@ 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'. + *** 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 cb5283e03c0..0767e157e4a 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -667,6 +667,12 @@ 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. Note that, +unlike @code{gdb.parameter(language')}, this function will never +return @samp{auto}. +@end defun + @node Exception Handling @subsubsection Exception Handling @cindex python exceptions 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-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp index 199d3bc16ec..fcafa7ee717 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 -- 2.34.1