[RFC,3/6] Add "tips" file to gdb

Message ID 20200404145422.19917-4-tom@tromey.com
State Superseded
Headers show
Series
  • Some user-friendliness changes
Related show

Commit Message

Tom Tromey April 4, 2020, 2:54 p.m.
This adds a "tips" file to gdb.  This file holds handy tips -- right
now there are just a few, but it's easy to add more.  A random tip is
displayed during interactive startup.

gdb/ChangeLog
2020-03-28  Tom Tromey  <tom@tromey.com>

	* NEWS: Add entry.
	* top.c (startup_style): New global.
	(print_tip): New function.
	(print_gdb_version): Update.  Don't print "show configuration"
	or "apropos" info.  Call print_tip.
	* tips: New file.
	* data-directory/Makefile.in (GDB_FILES): Add "tips".
---
 gdb/ChangeLog                  | 10 +++++
 gdb/NEWS                       |  2 +
 gdb/data-directory/Makefile.in |  2 +-
 gdb/tips                       |  9 ++++
 gdb/top.c                      | 78 +++++++++++++++++++++++++++++-----
 5 files changed, 89 insertions(+), 12 deletions(-)
 create mode 100644 gdb/tips

-- 
2.17.2

Comments

Eli Zaretskii April 4, 2020, 4:16 p.m. | #1
> From: Tom Tromey <tom@tromey.com>

> Date: Sat,  4 Apr 2020 08:54:19 -0600

> Cc: Tom Tromey <tom@tromey.com>

> 

> diff --git a/gdb/NEWS b/gdb/NEWS

> index 9d85e630e8c..b376737e409 100644

> --- a/gdb/NEWS

> +++ b/gdb/NEWS

> @@ -3,6 +3,8 @@

>  

>  *** Changes since GDB 9

>  

> +* GDB will now display a helpful tip when starting up.

> +


This part is OK.

Thanks.
Eli Zaretskii April 4, 2020, 4:42 p.m. | #2
> Date: Sat, 04 Apr 2020 19:16:51 +0300

> From: Eli Zaretskii <eliz@gnu.org>

> Cc: gdb-patches@sourceware.org

> 

> > From: Tom Tromey <tom@tromey.com>

> > Date: Sat,  4 Apr 2020 08:54:19 -0600

> > Cc: Tom Tromey <tom@tromey.com>

> > 

> > diff --git a/gdb/NEWS b/gdb/NEWS

> > index 9d85e630e8c..b376737e409 100644

> > --- a/gdb/NEWS

> > +++ b/gdb/NEWS

> > @@ -3,6 +3,8 @@

> >  

> >  *** Changes since GDB 9

> >  

> > +* GDB will now display a helpful tip when starting up.

> > +


Should this be in the manual?

Also, I think we want the structure and format of the 'tips' file
documented in the manual, as I presume we'd add tips to it with time
Tom Tromey June 21, 2020, 8:23 p.m. | #3
>> > +* GDB will now display a helpful tip when starting up.

>> > +


Eli> Should this be in the manual?

Yeah, where would you suggest.

Eli> Also, I think we want the structure and format of the 'tips' file
Eli> documented in the manual, as I presume we'd add tips to it with time

It is documented in the file itself.

Tom
Eli Zaretskii June 22, 2020, 2:24 a.m. | #4
> From: Tom Tromey <tom@tromey.com>

> Cc: tom@tromey.com,  gdb-patches@sourceware.org

> Date: Sun, 21 Jun 2020 14:23:48 -0600

> 

> >> > +* GDB will now display a helpful tip when starting up.

> >> > +

> 

> Eli> Should this be in the manual?

> 

> Yeah, where would you suggest.


The node "Help" sounds like a good place.

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 9d85e630e8c..b376737e409 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,8 @@ 
 
 *** Changes since GDB 9
 
+* GDB will now display a helpful tip when starting up.
+
 * GDB now supports debuginfod, an HTTP server for distributing ELF/DWARF
   debugging information as well as source code.
 
diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
index 95258d184e7..5cc7ec80c40 100644
--- a/gdb/data-directory/Makefile.in
+++ b/gdb/data-directory/Makefile.in
@@ -139,7 +139,7 @@  SYSTEM_GDBINIT_FILES = \
 	elinos.py \
 	wrs-linux.py
 
-GDB_FILES = NEWS
+GDB_FILES = NEWS tips
 
 FLAGS_TO_PASS = \
 	"prefix=$(prefix)" \
diff --git a/gdb/tips b/gdb/tips
new file mode 100644
index 00000000000..75957fb853d
--- /dev/null
+++ b/gdb/tips
@@ -0,0 +1,9 @@ 
+This file is in the old "fortune" format.  Each entry is separated by
+a percent character on a line of its own.  Anything before the first
+percent will never be shown.
+%
+You can use "help news" to see what has changed in GDB.
+%
+Type "apropos word" to search for commands related to "word".
+%
+Type "show configuration" for configuration details.
diff --git a/gdb/top.c b/gdb/top.c
index e2432489dc8..7af820b0bb9 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -56,6 +56,8 @@ 
 #include "gdbarch.h"
 #include "gdbsupport/pathstuff.h"
 #include "cli/cli-style.h"
+#include "gdbsupport/scoped_fd.h"
+#include <random>
 
 /* readline include files.  */
 #include "readline/readline.h"
@@ -128,6 +130,14 @@  current_ui_current_uiout_ptr ()
 
 int inhibit_gdbinit = 0;
 
+/* The style used for informational messages at startup.  */
+static ui_file_style startup_style =
+{
+  ui_file_style::MAGENTA,
+  ui_file_style::NONE,
+  ui_file_style::BOLD
+};
+
 /* Flag for whether we want to confirm potentially dangerous
    operations.  Default is yes.  */
 
@@ -1376,6 +1386,59 @@  command_line_input (const char *prompt_arg, const char *annotation_suffix)
   return cmd;
 }
 
+
+/* Print a useful tip to STREAM.  */
+
+static void
+print_tip (struct ui_file *stream)
+{
+  std::string tip_name = std::string (gdb_datadir) + SLASH_STRING + "tips";
+  scoped_fd fd (gdb_open_cloexec (tip_name.c_str (), O_RDONLY | O_BINARY, 0));
+  if (fd.get () < 0)
+    {
+      perror_warning_with_name ((std::string (_("Could not open tip file "))
+				 + tip_name).c_str ());
+      return;
+    }
+
+  std::string lines;
+  try
+    {
+      lines = read_entire_file (tip_name.c_str (), fd.get ());
+    }
+  catch (const gdb_exception &exc)
+    {
+      exception_print (stream, exc);
+      return;
+    }
+
+  std::vector<gdb::string_view> strings;
+  size_t offset = lines.find ("%\n");
+  if (offset == std::string::npos)
+    return;
+  while (offset != std::string::npos)
+    {
+      offset += 2;
+      size_t next_offset = lines.find ("%\n", offset);
+
+      if (next_offset == std::string::npos)
+	strings.emplace_back (lines.c_str () + offset);
+      else
+	strings.emplace_back (lines.c_str () + offset, next_offset - offset);
+
+      offset = next_offset;
+    }
+
+  std::random_device rd;
+  std::mt19937 mt (rd ());
+  std::uniform_int_distribution<> distr (0, strings.size () - 1);
+  int index = distr (mt);
+  /* Note that the string will include a newline.  */
+  fprintf_styled (stream, startup_style, "\n%.*s",
+		  (int) strings[index].size (),
+		  strings[index].data ());
+}
+
 /* See top.h.  */
 void
 print_gdb_version (struct ui_file *stream, bool interactive)
@@ -1386,11 +1449,7 @@  print_gdb_version (struct ui_file *stream, bool interactive)
 
   ui_file_style style;
   if (interactive)
-    {
-      ui_file_style nstyle = { ui_file_style::MAGENTA, ui_file_style::NONE,
-			       ui_file_style::BOLD };
-      style = nstyle;
-    }
+    style = startup_style;
   fprintf_styled (stream, style, "GNU gdb %s%s\n", PKGVERSION, version);
 
   /* Second line is a copyright notice.  */
@@ -1428,9 +1487,6 @@  There is NO WARRANTY, to the extent permitted by law.");
     }
   fprintf_filtered (stream, "\".\n");
 
-  fprintf_filtered (stream, _("Type \"show configuration\" "
-			      "for configuration details.\n"));
-
   if (REPORT_BUGS_TO[0])
     {
       fprintf_filtered (stream,
@@ -1442,9 +1498,9 @@  There is NO WARRANTY, to the extent permitted by law.");
 resources online at:\n    <http://www.gnu.org/software/gdb/documentation/>."));
   fprintf_filtered (stream, "\n\n");
   fprintf_filtered (stream, _("For help, type \"help\".\n"));
-  fprintf_filtered (stream,
-		    _("Type \"apropos word\" to search for commands \
-related to \"word\"."));
+
+  if (interactive)
+    print_tip (stream);
 }
 
 /* Print the details of GDB build-time configuration.  */