frame-id for frame #2: @{stack=0x7fffffffac90,code=0x000000000040111c,!special@}
@end smallexample
+@item maint info inline-frames
+@itemx maint info inline-frames @var{address}
+@cindex frames of inlined functions
+@kindex maint info inline-frames
+Print information about inlined frames which start at the current
+address, or @var{address} if specified.
+
+In order to allow the user to correctly step into inlined functions,
+@value{GDBN} needs to identify which inlined functions start at a
+particular address, and @value{GDBN} also needs to track which of
+these functions was last displayed to the user as the current frame.
+
+Imagine a situation where function @code{main} calls @code{foo}, which
+then calls @code{bar}, something like this:
+
+@smallexample
+@group
+int
+main ()
+@{
+ /* Some interesting code here... */
+
+ foo ();
+
+ /* More interesting code here... */
+@}
+
+void
+foo ()
+@{
+ bar ();
+@}
+
+void
+bar ()
+@{
+ /* Some interesting code here... */
+@}
+@end group
+@end smallexample
+
+As both @code{foo} and @code{bar} are inlined within @code{main} then
+there could be one address within @code{main} which is also the start
+of @code{foo} and also the start of @code{bar}. When the user stops
+at this address they will initially be told the inferior is in
+@code{main}, if the user does a @kbd{step} then @value{GDBN} doesn't
+actually step the inferior, instead the user is told the inferior
+entered @code{foo}. After the next @kbd{step} the user is told the
+inferior entered @code{bar}. The @kbd{maint info inline-frames}
+command can be used to view this internal @value{GDBN} state, like
+this:
+
+@smallexample
+@group
+(@value{GDBP}) step
+24 foo ();
+(@value{GDBP}) maintenance info inline-frames
+Cached inline state information for thread 1.
+program counter = 0x401137
+skipped frames = 2
+ bar
+ foo
+> main
+@end group
+@end smallexample
+
+Here the user is stopped in @code{main} at the call to @code{foo}. The
+inline-frames information shows that at this address @value{GDBN} has
+found the start of inlined functions @code{bar} and @code{foo}, but
+currently @value{GDBN} has skipped 2 frames and considers @code{main}
+to be the current frame, this is indicated with the @samp{>}.
+
+If the user performs a @kbd{step} to enter @code{foo} then the
+situation is updated:
+
+@smallexample
+@group
+(@value{GDBP}) step
+foo () at inline.c:14
+14 bar ();
+(@value{GDBP}) maintenance info inline-frames
+Cached inline state information for thread 1.
+program counter = 0x401137
+skipped frames = 1
+ bar
+> foo
+ main
+@end group
+@end smallexample
+
+Notice that the program counter value @code{0x401137} hasn't change,
+but now @value{GDBN} considers @code{foo} to be the current frame, and
+it is marked as such with the @samp{>}.
+
+Finally, the user performs another @kbd{step} to enter @code{bar}:
+
+@smallexample
+@group
+(@value{GDBP}) step
+bar () at inline.c:6
+6 ++global_counter;
+(@value{GDBP}) maintenance info inline-frames
+Cached inline state information for thread 1.
+program counter = 0x401137
+skipped frames = 0
+> bar
+ foo
+ main
+@end group
+@end smallexample
+
@kindex maint print registers
@kindex maint print raw-registers
@kindex maint print cooked-registers
#include "regcache.h"
#include "symtab.h"
#include "frame.h"
+#include "cli/cli-cmds.h"
+#include "cli/cli-style.h"
#include <algorithm>
/* We need to save a few variables for every thread stopped at the
struct inline_state
{
inline_state (thread_info *thread_, int skipped_frames_, CORE_ADDR saved_pc_,
- std::vector<const symbol *> &&skipped_symbols_)
+ std::vector<const symbol *> &&function_symbols_)
: thread (thread_), skipped_frames (skipped_frames_), saved_pc (saved_pc_),
- skipped_symbols (std::move (skipped_symbols_))
+ function_symbols (std::move (function_symbols_))
{}
/* The thread this data relates to. It should be a currently
functions can be stepped in to. */
int skipped_frames;
- /* Only valid if SKIPPED_FRAMES is non-zero. This is the PC used
- when calculating SKIPPED_FRAMES; used to check whether we have
- moved to a new location by user request. If so, we invalidate
- any skipped frames. */
+ /* This is the PC used when calculating FUNCTION_SYMBOLS; used to check
+ whether we have moved to a new location by user request. If so, we
+ invalidate any skipped frames. */
CORE_ADDR saved_pc;
- /* Only valid if SKIPPED_FRAMES is non-zero. This is the list of all
- function symbols that have been skipped, from inner most to outer
- most. It is used to find the call site of the current frame. */
- std::vector<const symbol *> skipped_symbols;
+ /* The list of all inline functions that start at SAVED_PC, except for
+ the last entry which will either be a non-inline function, or an
+ inline function that doesn't start at SAVED_PC. This last entry is
+ the function that "contains" all of the earlier functions.
+
+ This list can be empty if SAVED_PC is for a code region which is not
+ covered by any function (inline or non-inline). */
+ std::vector<const symbol *> function_symbols;
};
static std::vector<inline_state> inline_states;
/* Loop over the stop chain and determine if execution stopped in an
inlined frame because of a breakpoint with a user-specified location
- set at FRAME_BLOCK. */
+ set at FRAME_SYMBOL. */
static bool
-stopped_by_user_bp_inline_frame (const block *frame_block, bpstat *stop_chain)
+stopped_by_user_bp_inline_frame (const symbol *frame_symbol,
+ bpstat *stop_chain)
{
for (bpstat *s = stop_chain; s != nullptr; s = s->next)
{
to presenting the stop at the innermost inline
function. */
if (loc->symbol == nullptr
- || frame_block == loc->symbol->value_block ())
+ || frame_symbol == loc->symbol)
return true;
}
}
return false;
}
+/* Return a list of all the inline function symbols that start at THIS_PC
+ and the symbol for the function which contains all of the inline
+ functions.
+
+ The function symbols are ordered such that the most inner function is
+ first.
+
+ The returned list can be empty if there are no function at THIS_PC. Or
+ the returned list may have only a single entry if there are no inline
+ functions starting at THIS_PC. */
+
+static std::vector<const symbol *>
+gather_inline_frames (CORE_ADDR this_pc)
+{
+ /* Build the list of inline frames starting at THIS_PC. After the loop,
+ CUR_BLOCK is expected to point at the first function symbol (inlined or
+ not) "containing" the inline frames starting at THIS_PC. */
+ const block *cur_block = block_for_pc (this_pc);
+ if (cur_block == nullptr)
+ return {};
+
+ std::vector<const symbol *> function_symbols;
+ while (cur_block != nullptr)
+ {
+ if (cur_block->inlined_p ())
+ {
+ gdb_assert (cur_block->function () != nullptr);
+
+ /* See comments in inline_frame_this_id about this use
+ of BLOCK_ENTRY_PC. */
+ if (cur_block->entry_pc () == this_pc
+ || block_starting_point_at (this_pc, cur_block))
+ function_symbols.push_back (cur_block->function ());
+ else
+ break;
+ }
+ else if (cur_block->function () != nullptr)
+ break;
+
+ cur_block = cur_block->superblock ();
+ }
+
+ /* If we have a code region for which we have no function blocks,
+ possibly due to bad debug, or possibly just when some debug
+ information has been stripped, then we can end up in a situation where
+ there are global and static blocks for an address, but no function
+ blocks. In this case the early return above will not trigger as we
+ will find the static block for THIS_PC, but in the loop above we will
+ fail to find any function blocks (inline or non-inline) and so
+ CUR_BLOCK will eventually become NULL. If this happens then
+ FUNCTION_SYMBOLS must be empty (as we found no function blocks).
+
+ Otherwise, if we did find a function block, then we should only leave
+ the above loop when CUR_BLOCK is pointing to a non-inline function
+ that possibly contains some inline functions, or CUR_BLOCK should
+ point to an inline function that doesn't start at THIS_PC. */
+ if (cur_block != nullptr)
+ {
+ gdb_assert (cur_block->function () != nullptr);
+ function_symbols.push_back (cur_block->function ());
+ }
+ else
+ gdb_assert (function_symbols.empty ());
+
+ return function_symbols;
+}
+
/* See inline-frame.h. */
void
skip_inline_frames (thread_info *thread, bpstat *stop_chain)
{
- const struct block *frame_block, *cur_block;
- std::vector<const symbol *> skipped_syms;
- int skip_count = 0;
+ gdb_assert (find_inline_frame_state (thread) == nullptr);
- /* This function is called right after reinitializing the frame
- cache. We try not to do more unwinding than absolutely
- necessary, for performance. */
CORE_ADDR this_pc = get_frame_pc (get_current_frame ());
- frame_block = block_for_pc (this_pc);
- if (frame_block != NULL)
+ std::vector<const symbol *> function_symbols
+ = gather_inline_frames (this_pc);
+
+ /* Figure out how many of the inlined frames to skip. Do not skip an
+ inlined frame (and its callers) if execution stopped because of a user
+ breakpoint for this specific function.
+
+ By default, skip all the found inlined frames.
+
+ The last entry in FUNCTION_SYMBOLS is special, this is the function
+ which contains all of the inlined functions, we never skip this. */
+ int skipped_frames = 0;
+
+ for (const auto sym : function_symbols)
{
- cur_block = frame_block;
- while (cur_block->superblock ())
- {
- if (cur_block->inlined_p ())
- {
- /* See comments in inline_frame_this_id about this use
- of BLOCK_ENTRY_PC. */
- if (cur_block->entry_pc () == this_pc
- || block_starting_point_at (this_pc, cur_block))
- {
- /* Do not skip the inlined frame if execution
- stopped in an inlined frame because of a user
- breakpoint for this inline function. */
- if (stopped_by_user_bp_inline_frame (cur_block, stop_chain))
- break;
-
- skip_count++;
- skipped_syms.push_back (cur_block->function ());
- }
- else
- break;
- }
- else if (cur_block->function () != NULL)
- break;
+ if (stopped_by_user_bp_inline_frame (sym, stop_chain)
+ || sym == function_symbols.back ())
+ break;
- cur_block = cur_block->superblock ();
- }
+ ++skipped_frames;
}
- gdb_assert (find_inline_frame_state (thread) == NULL);
- inline_states.emplace_back (thread, skip_count, this_pc,
- std::move (skipped_syms));
-
- if (skip_count != 0)
+ if (skipped_frames > 0)
reinit_frame_cache ();
+
+ inline_states.emplace_back (thread, skipped_frames, this_pc,
+ std::move (function_symbols));
}
/* Step into an inlined function by unhiding it. */
gdb_assert (state != NULL);
/* This should only be called when we are skipping at least one frame,
- hence SKIPPED_FRAMES will be greater than zero when we get here.
- We initialise SKIPPED_FRAMES at the same time as we build
- SKIPPED_SYMBOLS, hence it should be true that SKIPPED_FRAMES never
- indexes outside of the SKIPPED_SYMBOLS vector. */
+ hence FUNCTION_SYMBOLS will contain more than one entry (the last
+ entry is the "outer" containing function).
+
+ As we initialise SKIPPED_FRAMES at the same time as we build
+ FUNCTION_SYMBOLS it should be true that SKIPPED_FRAMES never indexes
+ outside of the FUNCTION_SYMBOLS vector. */
+ gdb_assert (state->function_symbols.size () > 1);
gdb_assert (state->skipped_frames > 0);
- gdb_assert (state->skipped_frames <= state->skipped_symbols.size ());
- return state->skipped_symbols[state->skipped_frames - 1];
+ gdb_assert (state->skipped_frames < state->function_symbols.size ());
+ return state->function_symbols[state->skipped_frames - 1];
}
/* Return the number of functions inlined into THIS_FRAME. Some of
return inline_count;
}
+
+/* The 'maint info inline-frames' command. Takes an optional address
+ expression and displays inline frames that start at the given address,
+ or at the address of the current thread if no address is given. */
+
+static void
+maintenance_info_inline_frames (const char *arg, int from_tty)
+{
+ std::optional<std::vector<const symbol *>> local_function_symbols;
+ std::vector<const symbol *> *function_symbols;
+ int skipped_frames;
+ CORE_ADDR addr;
+
+ if (arg == nullptr)
+ {
+ /* With no argument then the user wants to know about the current
+ inline frame information. This information is cached per-thread
+ and can be updated as the user steps between inline functions at
+ the current address. */
+
+ if (inferior_ptid == null_ptid)
+ error (_("no inferior thread"));
+
+ thread_info *thread = inferior_thread ();
+ auto it = std::find_if (inline_states.begin (), inline_states.end (),
+ [thread] (const inline_state &istate)
+ {
+ return thread == istate.thread;
+ });
+
+ /* Stopped threads always have cached inline_state information. */
+ gdb_assert (it != inline_states.end ());
+
+ gdb_printf (_("Cached inline state information for thread %s.\n"),
+ print_thread_id (thread));
+
+ function_symbols = &it->function_symbols;
+ skipped_frames = it->skipped_frames;
+ addr = it->saved_pc;
+ }
+ else
+ {
+ /* If there is an argument then parse it as an address, the user is
+ asking about inline functions that start at the given address. */
+
+ addr = parse_and_eval_address (arg);
+ local_function_symbols.emplace (gather_inline_frames (addr));
+
+ function_symbols = &(local_function_symbols.value ());
+ skipped_frames = function_symbols->size () - 1;
+ }
+
+ /* The address we're analysing. */
+ gdb_printf (_("program counter = %ps\n"),
+ styled_string (address_style.style (),
+ core_addr_to_string_nz (addr)));
+
+ gdb_printf (_("skipped frames = %d\n"), skipped_frames);
+
+ /* Print the full list of function symbols in STATE. Highlight the
+ current function as indicated by the skipped frames counter. */
+ for (size_t i = 0; i < function_symbols->size (); ++i)
+ gdb_printf (_("%c %ps\n"),
+ (i == skipped_frames ? '>' : ' '),
+ styled_string (function_name_style.style (),
+ (*function_symbols)[i]->print_name ()));
+}
+
+\f
+
+void _initialize_inline_frame ();
+void
+_initialize_inline_frame ()
+{
+ add_cmd ("inline-frames", class_maintenance, maintenance_info_inline_frames,
+ _("\
+Display inline frame information for current thread.\n\
+\n\
+Usage:\n\
+\n\
+ maintenance info inline-frames [ADDRESS]\n\
+\n\
+With no ADDRESS show all inline frames starting at the current program\n\
+counter address. When ADDRESS is given, list all inline frames starting\n\
+at ADDRESS.\n\
+\n\
+The last frame listed might not start at ADDRESS, this is the frame that\n\
+contains the other inline frames."),
+ &maintenanceinfolist);
+}
--- /dev/null
+# Copyright (C) 2024 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Check the 'maint info inline-frames' command.
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
+ {debug nopie}]} {
+ return -1
+}
+
+if {![runto normal_func]} {
+ return 0
+}
+
+# Next forward until we find the call to inline_func_a(). The hope is
+# that when we see the 'inline_func_a()' line this will be the start of
+# the inlined function. This might not be the case on all
+# architectures if the compiler needs to perform some preamble.
+gdb_test_multiple "next" "next forward to inline_func_a" {
+ -re "^$decimal\\s+inline_func_a \\(\\);\r\n" {
+ # Consume the next prompt.
+ gdb_expect {
+ -re "^$gdb_prompt $" {}
+ }
+ pass $gdb_test_name
+ }
+
+ -re "^$decimal\\s+\[^\r\n\]+After inline function\[^\r\n\]+\r\n" {
+ # We've gone too far!
+ fail $gdb_test_name
+ }
+
+ -re "^$decimal\\s+\[^\r\n\]+\r\n" {
+ send_gdb "next\n"
+ exp_continue
+ }
+
+ -re "^\[^\r\n\]+\r\n" {
+ exp_continue
+ }
+}
+
+# View the inline frame information. This should display that we are
+# at the start of inline_func_a() within normal_func().
+gdb_test "maint info inline-frames" \
+ [multi_line \
+ "^Cached inline state information for thread $decimal\\." \
+ "program counter = $hex" \
+ "skipped frames = 2" \
+ " inline_func_b" \
+ " inline_func_a" \
+ "> normal_func"] \
+ "check inline-frames state when in normal_func"
+
+# Step, we should now enter the inlined function.
+gdb_test "step" ".*" \
+ "step to enter inline_func"
+
+# And the inline-frames information should update.
+gdb_test "maint info inline-frames" \
+ [multi_line \
+ "^Cached inline state information for thread $decimal\\." \
+ "program counter = $hex" \
+ "skipped frames = 1" \
+ " inline_func_b" \
+ "> inline_func_a" \
+ " normal_func"] \
+ "check inline-frames state when just entered inline_func_a"
+
+# Record the current program counter.
+set pc [get_hexadecimal_valueof "\$pc" "UNKNOWN"]
+
+# Use the recorded $pc value to check inline frames.
+gdb_test "maint info inline-frames $pc" \
+ [multi_line \
+ "^program counter = $hex" \
+ "skipped frames = 2" \
+ " inline_func_b" \
+ " inline_func_a" \
+ "> normal_func"] \
+ "check inline-frames state at recorded \$pc while at the \$pc"
+
+# Step again, we should now enter inlined_func_b().
+gdb_test "step" ".*" \
+ "step into inline_func_b"
+
+gdb_test "maint info inline-frames" \
+ [multi_line \
+ "^Cached inline state information for thread $decimal\\." \
+ "program counter = $hex" \
+ "skipped frames = 0" \
+ "> inline_func_b" \
+ " inline_func_a" \
+ " normal_func"] \
+ "check inline-frames state when just entered inline_func_b"
+
+gdb_test "step" ".*" \
+ "step into the body of inline_func_b"
+
+# Now we are no longer at the start of the inlined function we should
+# no longer see normal_func() in the inline-frames information.
+gdb_test "maint info inline-frames" \
+ [multi_line \
+ "^Cached inline state information for thread $decimal\\." \
+ "program counter = $hex" \
+ "skipped frames = 0" \
+ "> inline_func_b"] \
+ "check inline-frames state when within inline_func_b"
+
+# Use the recorded $pc value to check inline frames.
+gdb_test "maint info inline-frames $pc" \
+ [multi_line \
+ "^program counter = $hex" \
+ "skipped frames = 2" \
+ " inline_func_b" \
+ " inline_func_a" \
+ "> normal_func"] \
+ "check inline-frames state at recorded \$pc"
+
+clean_restart $binfile
+
+# Use the recorded $pc value to check inline frames when the inferior
+# is not executing.
+gdb_test "maint info inline-frames $pc" \
+ [multi_line \
+ "^program counter = $hex" \
+ "skipped frames = 2" \
+ " inline_func_b" \
+ " inline_func_a" \
+ "> normal_func"] \
+ "check inline-frames state at recorded \$pc before execution starts"
+
+# Trying to read the $pc from the current thread should fail if the
+# inferior is not yet running.
+gdb_test "maint info inline-frames" \
+ "^no inferior thread" \
+ "check inline-frames state of current thread before execution starts"