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?

@@ -0,0 +1,30 @@
# Test that __builtin_trap PC is correctly adjusted

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 file should have a copyright notice.

gdb_test "x/i \$pc" \
".*s_trap.*" \
"PC points to trap instruction"
}

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.

If the core of GDB has some logic for single stepping & s_trap, we should probably test for it.

Comment thread gdb/amd-dbgapi-target.c
program counter needs to be adjusted so that it points to the
breakpoint instruction.
/* If the wave is stopped because of a software breakpoint or an abort
trap (s_trap 2, used by __builtin_verbose_trap), the program counter

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 test is not exercising __builtin_verbose_trap, it probably should.

@@ -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.

{
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;
}

int main()
{
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.

not a big fan of the (foo ? 1 : 0). The 1 if true 0 otherwise is tautologic, this is how foo would convert to int anyway.

return
}

# Continue to the trap

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 period at the end of the sentence.

"received trap signal"

# Check PC points to trap instruction
# If PC adjustment is working, PC should be at the s_trap instruction

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.

dito.

Bratislav Filipovic added 2 commits July 10, 2026 14:21
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
Test that __builtin_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 37f9101 to 998fd18 Compare July 10, 2026 14:21
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