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>
}
}
+/* 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)
_("\
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,
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
}
#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;
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. */