]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix build when RUSAGE_THREAD is not available & add warning
authorPedro Alves <pedro@palves.net>
Sat, 17 May 2025 21:30:56 +0000 (22:30 +0100)
committerSimon Marchi <simon.marchi@polymtl.ca>
Thu, 29 May 2025 15:08:43 +0000 (11:08 -0400)
Building current GDB on Cygwin, fails like so:

 /home/pedro/gdb/src/gdbsupport/run-time-clock.cc: In function ‘void get_run_time(user_cpu_time_clock::time_point&, system_cpu_time_clock::time_point&, run_time_scope ’:
 /home/pedro/gdb/src/gdbsupport/run-time-clock.cc:52:13: error: ‘RUSAGE_THREAD’ was not declared in this scope; did you mean ‘SIGEV_THREAD’?
    52 |       who = RUSAGE_THREAD;
       |             ^~~~~~~~~~~~~
       |             SIGEV_THREAD

Cygwin does not implement RUSAGE_THREAD.  Googling around, I see
Cygwin is not alone, other platforms don't support it either.  For
example, here is someone suggesting an alternative for darwin/macos:
https://stackoverflow.com/questions/5652463/equivalent-to-rusage-thread-darwin

Fix this by falling back to process scope if thread scope can't be
supported.  I chose this instead of returning zero usage or some other
constant, because if gdb is built without threading support, then
process-scope run time usage is the right info to return.

But instead of falling back silently, print a warning (just once),
like so:

 (gdb) maint set per-command time on
 ⚠️ warning: per-thread run time information not available on this platform

... so that developers on other platforms at least have a hint
upfront.

This new warning also shows on platforms that don't have getrusage in
the first place, but does not show if the build doesn't support
threading at all.

New tests are added to gdb.base/maint.exp, to expect the warning, and
also to ensure other "mt per-command" sub commands don't trigger the
new warning.

Change-Id: Ie01b916b62f87006f855e31594a5ac7cf09e4c02
Approved-By: Simon Marchi <simon.marchi@efficios.com>
Approved-By: Tom Tromey <tom@tromey.com>
gdb/maint.c
gdb/testsuite/gdb.base/maint.exp
gdbsupport/run-time-clock.cc
gdbsupport/run-time-clock.h

index c6f9b322e504ffa0f460b10b664328923d55640f..0526fffe24a19a074f2d25c96bc94e72a9ec8f98 100644 (file)
@@ -1151,6 +1151,28 @@ set_per_command_cmd (const char *args, int from_tty)
       }
 }
 
+/* Handle "mt set per-command time".  Warn if per-thread run time
+   information is not possible.  */
+
+static void
+maintenance_set_command_time_cmd (const char *args, int from_tty,
+                                 cmd_list_element *c)
+{
+  /* No point warning if this platform can't use multiple threads at
+     all.  */
+#if CXX_STD_THREAD
+  static bool already_warned = false;
+  if (per_command_time
+      && !get_run_time_thread_scope_available ()
+      && !already_warned)
+    {
+      warning (_("\
+per-thread run time information not available on this platform"));
+      already_warned = true;;
+    }
+#endif
+}
+
 /* See maint.h.  */
 
 scoped_time_it::scoped_time_it (const char *what)
@@ -1423,7 +1445,7 @@ Show whether to display per-command execution time."),
                           _("\
 If enabled, the execution time for each command will be\n\
 displayed following the command's output."),
-                          NULL, NULL,
+                          maintenance_set_command_time_cmd, NULL,
                           &per_command_setlist, &per_command_showlist);
 
   add_setshow_boolean_cmd ("space", class_maintenance,
index 52282bc32b3788a291d21f642f635dd44823fdcc..43fc2c0019826eb0bdac5dd4c2af44d4a7859748 100644 (file)
@@ -52,6 +52,40 @@ if {[prepare_for_testing "failed to prepare" $testfile \
     return -1
 }
 
+# Check "maint set per-command" warnings.  We do this early so that
+# the following tests don't need to expect them, as GDB only warns
+# once.
+
+with_test_prefix "warnings" {
+    # Potential warning given by "maint set per-command time".
+    set maybe_per_command_warning \
+       "(?:warning: per-thread run time information not available on this platform)?"
+
+    # This one should not issue the "per-command time" warning.
+    with_test_prefix "per-command space" {
+       gdb_test_no_output "mt set per-command space on"
+       gdb_test_no_output "mt set per-command space off"
+    }
+
+    # These might warn.  "per-command on" enables all sub commands, so
+    # might trigger the "per-command time" warning.
+    foreach cmd {"per-command" "per-command time"} {
+       with_test_prefix $cmd {
+           # GDB only warns once, so restart between commands.
+           clean_restart $binfile
+           gdb_test "mt set $cmd on" "$maybe_per_command_warning"
+           gdb_test "mt set $cmd off" "command started"
+           gdb_test_no_output "mt set $cmd on"  \
+               "mt set $cmd on, again"
+           gdb_test "mt set $cmd off" "command started" \
+               "mt set $cmd off, again"
+       }
+    }
+
+    # We've already warned once above, so the following tests don't
+    # need to expect the warning.
+}
+
 set readnow_p [readnow]
 
 # The commands we test here produce many lines of output; disable "press
index 621ba77ed9a27604df07a72c96c49d7480facafb..cda60b023a39dcbeafd58a69b3531068cb35f1c0 100644 (file)
@@ -37,12 +37,33 @@ timeval_to_microseconds (struct timeval *tv)
 }
 #endif
 
+/* See run-time-clock.h.  */
+
+bool
+get_run_time_thread_scope_available ()
+{
+#if defined HAVE_GETRUSAGE && defined RUSAGE_THREAD
+  return true;
+#else
+  return false;
+#endif
+}
+
 void
 get_run_time (user_cpu_time_clock::time_point &user,
              system_cpu_time_clock::time_point &system,
              run_time_scope scope) noexcept
 {
 #ifdef HAVE_GETRUSAGE
+
+  /* If we can't provide thread scope run time usage, fallback to
+     process scope.  This will at least be right if GDB is built
+     without threading support in the first place (or is set to use
+     zero worker threads).  */
+# ifndef RUSAGE_THREAD
+#  define RUSAGE_THREAD RUSAGE_SELF
+# endif
+
   struct rusage rusage;
   int who;
 
index a985dbb2667fb569aaef86f3dfebfcce608e505f..274351425e70145bd83e819e5803002bce4058e0 100644 (file)
@@ -64,13 +64,20 @@ enum class run_time_scope
    supported.  If not supported, then the combined user+kernel time
    is returned in USER and SYSTEM is set to zero.
 
-   SCOPE indicates whether to return the run time for the whole process or
-   just for the calling thread.  */
+   SCOPE indicates whether to return the run time for the whole
+   process or just for the calling thread.  If the latter isn't
+   supported, then returns the run time for the whole process even if
+   run_time_scope::thread is requested.  */
 
 void get_run_time (user_cpu_time_clock::time_point &user,
                   system_cpu_time_clock::time_point &system,
                   run_time_scope scope) noexcept;
 
+/* Returns true if is it possible for get_run_time above to return the
+   run time for just the calling thread.  */
+
+bool get_run_time_thread_scope_available ();
+
 /* Count the total amount of time spent executing in userspace+kernel
    mode.  */