Skip to content

Commit 42339bc

Browse files
palvessimark
authored andcommitted
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 <[email protected]> Approved-By: Tom Tromey <[email protected]>
1 parent c29af2b commit 42339bc

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

gdb/maint.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,28 @@ set_per_command_cmd (const char *args, int from_tty)
11511151
}
11521152
}
11531153

1154+
/* Handle "mt set per-command time". Warn if per-thread run time
1155+
information is not possible. */
1156+
1157+
static void
1158+
maintenance_set_command_time_cmd (const char *args, int from_tty,
1159+
cmd_list_element *c)
1160+
{
1161+
/* No point warning if this platform can't use multiple threads at
1162+
all. */
1163+
#if CXX_STD_THREAD
1164+
static bool already_warned = false;
1165+
if (per_command_time
1166+
&& !get_run_time_thread_scope_available ()
1167+
&& !already_warned)
1168+
{
1169+
warning (_("\
1170+
per-thread run time information not available on this platform"));
1171+
already_warned = true;;
1172+
}
1173+
#endif
1174+
}
1175+
11541176
/* See maint.h. */
11551177

11561178
scoped_time_it::scoped_time_it (const char *what)
@@ -1423,7 +1445,7 @@ Show whether to display per-command execution time."),
14231445
_("\
14241446
If enabled, the execution time for each command will be\n\
14251447
displayed following the command's output."),
1426-
NULL, NULL,
1448+
maintenance_set_command_time_cmd, NULL,
14271449
&per_command_setlist, &per_command_showlist);
14281450

14291451
add_setshow_boolean_cmd ("space", class_maintenance,

gdb/testsuite/gdb.base/maint.exp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,40 @@ if {[prepare_for_testing "failed to prepare" $testfile \
5252
return -1
5353
}
5454

55+
# Check "maint set per-command" warnings. We do this early so that
56+
# the following tests don't need to expect them, as GDB only warns
57+
# once.
58+
59+
with_test_prefix "warnings" {
60+
# Potential warning given by "maint set per-command time".
61+
set maybe_per_command_warning \
62+
"(?:warning: per-thread run time information not available on this platform)?"
63+
64+
# This one should not issue the "per-command time" warning.
65+
with_test_prefix "per-command space" {
66+
gdb_test_no_output "mt set per-command space on"
67+
gdb_test_no_output "mt set per-command space off"
68+
}
69+
70+
# These might warn. "per-command on" enables all sub commands, so
71+
# might trigger the "per-command time" warning.
72+
foreach cmd {"per-command" "per-command time"} {
73+
with_test_prefix $cmd {
74+
# GDB only warns once, so restart between commands.
75+
clean_restart $binfile
76+
gdb_test "mt set $cmd on" "$maybe_per_command_warning"
77+
gdb_test "mt set $cmd off" "command started"
78+
gdb_test_no_output "mt set $cmd on" \
79+
"mt set $cmd on, again"
80+
gdb_test "mt set $cmd off" "command started" \
81+
"mt set $cmd off, again"
82+
}
83+
}
84+
85+
# We've already warned once above, so the following tests don't
86+
# need to expect the warning.
87+
}
88+
5589
set readnow_p [readnow]
5690

5791
# The commands we test here produce many lines of output; disable "press

gdbsupport/run-time-clock.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,33 @@ timeval_to_microseconds (struct timeval *tv)
3737
}
3838
#endif
3939

40+
/* See run-time-clock.h. */
41+
42+
bool
43+
get_run_time_thread_scope_available ()
44+
{
45+
#if defined HAVE_GETRUSAGE && defined RUSAGE_THREAD
46+
return true;
47+
#else
48+
return false;
49+
#endif
50+
}
51+
4052
void
4153
get_run_time (user_cpu_time_clock::time_point &user,
4254
system_cpu_time_clock::time_point &system,
4355
run_time_scope scope) noexcept
4456
{
4557
#ifdef HAVE_GETRUSAGE
58+
59+
/* If we can't provide thread scope run time usage, fallback to
60+
process scope. This will at least be right if GDB is built
61+
without threading support in the first place (or is set to use
62+
zero worker threads). */
63+
# ifndef RUSAGE_THREAD
64+
# define RUSAGE_THREAD RUSAGE_SELF
65+
# endif
66+
4667
struct rusage rusage;
4768
int who;
4869

gdbsupport/run-time-clock.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,20 @@ enum class run_time_scope
6464
supported. If not supported, then the combined user+kernel time
6565
is returned in USER and SYSTEM is set to zero.
6666
67-
SCOPE indicates whether to return the run time for the whole process or
68-
just for the calling thread. */
67+
SCOPE indicates whether to return the run time for the whole
68+
process or just for the calling thread. If the latter isn't
69+
supported, then returns the run time for the whole process even if
70+
run_time_scope::thread is requested. */
6971

7072
void get_run_time (user_cpu_time_clock::time_point &user,
7173
system_cpu_time_clock::time_point &system,
7274
run_time_scope scope) noexcept;
7375

76+
/* Returns true if is it possible for get_run_time above to return the
77+
run time for just the calling thread. */
78+
79+
bool get_run_time_thread_scope_available ();
80+
7481
/* Count the total amount of time spent executing in userspace+kernel
7582
mode. */
7683

0 commit comments

Comments
 (0)