Re: GDB version as convenience variable

Eli Zaretskii
2019-03-25T17:31:53Z

Seems like there's a consensus to have 2 separate variables, so here's
a version of the patch that does that. OK to commit?

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 2de1adc..f841e0a 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2019-03-06 Eli Zaretskii <eliz@gnu.org>
+
+ * NEWS: Announce $_gdb_major and $_gdb_minor.
+
+ * top.c (init_gdb_version_vars): New function.
+ (gdb_init): Call init_gdb_version_vars.
+
2019-03-25 Alan Hayward <alan.hayward@arm.com>

* s390-linux-nat.c: Add include.
diff --git a/gdb/NEWS b/gdb/NEWS
index c45b313..676f0a0 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,12 @@

*** Changes since GDB 8.3

+* New built-in convenience variables $_gdb_major and $_gdb_minor
+ provide the GDB version. They are handy for conditionally using
+ features available only in or since specific GDB versions, in
+ scripts that should work error-free with many different versions,
+ such as in system-wide init files.
+
*** Changes in GDB 8.3

* GDB and GDBserver now support access to additional registers on
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 1f41498..c384267 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,7 @@
+2019-03-06 Eli Zaretskii <eliz@gnu.org>
+
+ * gdb.texinfo (Convenience Vars): Document $_gdb_version.
+
2019-03-22 Alan Hayward <alan.hayward@arm.com>
Jiong Wang <jiong.wang@arm.com>

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 4c1d5e8..115bcf7 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -11211,6 +11211,17 @@
@item $_gthread
The global number of the current thread. @xref{global thread numbers}.

+@item $_gdb_major
+@itemx $_gdb_minor
+@vindex $_gdb_major@{, convenience variable}
+@vindex $_gdb_minor@{, convenience variable}
+The major and minor version numbers of the running @value{GDBN}.
+Development snapshots and pretest versions have their minor version
+incremented by one; thus, @value{GDBN} pretest 9.11.90 will produce
+the value 12 for @code{$_gdb_minor}. These variables allow you to
+write scripts that work with different versions of @value{GDBN}
+without errors caused by features unavailable in some of those
+versions.
@end table

@node Convenience Funs
diff --git a/gdb/top.c b/gdb/top.c
index b10b064..afb77c0 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -2003,6 +2004,17 @@ set_history_filename (const char *args,
}

static void
+init_gdb_version_vars (void)
+{
+ struct internalvar *major_version_var = create_internalvar ("_gdb_major");
+ struct internalvar *minor_version_var = create_internalvar ("_gdb_minor");
+ int vmajor = 0, vminor = 0, vrevision = 0;
+ sscanf (version, "%d.%d.%d", &vmajor, &vminor, &vrevision);
+ set_internalvar_integer (major_version_var, vmajor);
+ set_internalvar_integer (minor_version_var, vminor + (vrevision > 0));
+}
+
+static void
init_main (void)
{
struct cmd_list_element *c;
@@ -2206,4 +2218,7 @@ gdb_init (char *argv0)
prefix to be installed. Keep things simple and just do final
script initialization here. */
finish_ext_lang_initialization ();
+
+ /* Create $_gdb_major and $_gdb_minor convenience variables. */
+ init_gdb_version_vars ();
}