Skip to content

Adjust PC for abort trap (s_trap 2) to improve __builtin_verbose_trap support#199

Open
amd-bfilipov wants to merge 2 commits into
amd-stagingfrom
users/bfilipov/fix-s_trap-2-pc-adjustment
Open

Adjust PC for abort trap (s_trap 2) to improve __builtin_verbose_trap support#199
amd-bfilipov wants to merge 2 commits into
amd-stagingfrom
users/bfilipov/fix-s_trap-2-pc-adjustment

Conversation

@amd-bfilipov

Copy link
Copy Markdown
Contributor

Ticket: AIROCGDB-558

Problem

When __builtin_verbose_trap() executes on AMD GPU, it generates an s_trap 2 instruction. After the trap, the
hardware increments the PC past the trap instruction, causing it to exit the inlined DWARF frame containing the
verbose trap message. This makes the error message invisible to the debugger.

Solution

Extend PC adjustment to include AMD_DBGAPI_WAVE_STOP_REASON_ASSERT_TRAP (s_trap 2), similar to existing breakpoint
handling. When a wave stops due to an abort trap, rewind the PC by 4 bytes to point at the trap instruction.

Rationale

We only apply PC adjustment to abort traps (s_trap 2) because:

  • Abort traps do not expect resumption (they terminate execution)
  • Rewinding PC is safe since the wave will not continue past the trap
  • This keeps the PC in the correct DWARF frame for source location and verbose trap messages

Debug traps (s_trap 3) are intentionally excluded as they may have different resume semantics.

Testing

Added test case gdb.rocm/builtin_trap.exp to verify PC points to the trap instruction after stopping at
__builtin_trap().

@amd-bfilipov amd-bfilipov requested a review from a team as a code owner July 10, 2026 13:23
Comment thread gdb/amd-dbgapi-target.c Outdated
containing DWARF debug info for verbose trap messages.

Do not adjust PC for abort traps when single-stepping, as stepping
has its own PC management logic.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real question is: should we allow to single step past an abort? Is __builtin_verbose_trap marked noreturn?

Comment thread gdb/testsuite/gdb.rocm/builtin_trap.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/builtin_trap.exp Outdated
Comment thread gdb/amd-dbgapi-target.c
Comment thread gdb/testsuite/gdb.rocm/builtin_trap.cpp Outdated
@@ -0,0 +1,12 @@
#include <hip/hip_runtime.h>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing copyright header.

Comment thread gdb/testsuite/gdb.rocm/builtin_trap.cpp Outdated
{
test_trap_kernel<<<1, 1>>>();
return hipDeviceSynchronize() != hipSuccess ? 1 : 0;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding style.

Should be

__global__ void
test_trap_kernel ()
{
  __builtin_trap ();
}

int
main ()
{
 test_trap_kernel<<<1, 1>>> ();
  return hipDeviceSynchronize () != hipSuccess;
}

Comment thread gdb/testsuite/gdb.rocm/builtin_trap.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/builtin_trap.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/builtin_trap.exp Outdated
When a wave stops with s_trap 2,
rewind the PC by 4 bytes to point at the trap instruction, similar
to breakpoint handling.

This ensures the PC remains in the inlined DWARF frame containing
debug info for __builtin_verbose_trap messages.

Fixes: ROCM-22957
@amd-bfilipov amd-bfilipov force-pushed the users/bfilipov/fix-s_trap-2-pc-adjustment branch from 37f9101 to 998fd18 Compare July 10, 2026 14:21
Test that __builtin_verbose_trap() correctly stops with PC pointing
at the trap instruction.

Related: ROCM-22957
@amd-bfilipov amd-bfilipov force-pushed the users/bfilipov/fix-s_trap-2-pc-adjustment branch from 998fd18 to f65ff67 Compare July 13, 2026 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants