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
@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
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
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. */
"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 */
};
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 ();
+}
+
\f
/* The "current" objfile. This is set when gdb detects that a new
ARCH, a gdb.Architecture to determine the address size. The format of\n\
the returned string is 'ADDRESS <SYMBOL+OFFSET>' 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}
};
" = 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"
"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
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" \
gdb_test "print outer" " = pp::Outer \\(x\\(5\\)\\)"
gdb_test "print outer.0" " = x\\(5\\)"
+
+gdb_test "python print(gdb.selected_frame().language())" "rust"