From: Pedro Alves Date: Sat, 17 May 2025 21:30:56 +0000 (+0100) Subject: Fix build when RUSAGE_THREAD is not available & add warning X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=42339bc4e0a146531e91632cd998e63de6efa81f;p=thirdparty%2Fbinutils-gdb.git Fix build when RUSAGE_THREAD is not available & add warning 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 Approved-By: Tom Tromey --- diff --git a/gdb/maint.c b/gdb/maint.c index c6f9b322e50..0526fffe24a 100644 --- a/gdb/maint.c +++ b/gdb/maint.c @@ -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, diff --git a/gdb/testsuite/gdb.base/maint.exp b/gdb/testsuite/gdb.base/maint.exp index 52282bc32b3..43fc2c00198 100644 --- a/gdb/testsuite/gdb.base/maint.exp +++ b/gdb/testsuite/gdb.base/maint.exp @@ -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 diff --git a/gdbsupport/run-time-clock.cc b/gdbsupport/run-time-clock.cc index 621ba77ed9a..cda60b023a3 100644 --- a/gdbsupport/run-time-clock.cc +++ b/gdbsupport/run-time-clock.cc @@ -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; diff --git a/gdbsupport/run-time-clock.h b/gdbsupport/run-time-clock.h index a985dbb2667..274351425e7 100644 --- a/gdbsupport/run-time-clock.h +++ b/gdbsupport/run-time-clock.h @@ -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. */