• A Basil Plant@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      6 months ago

      https://en.wikipedia.org/wiki/INT_(x86_instruction) (scroll down to INT3)

      https://stackoverflow.com/a/61946177

      The TL;DR is that it’s used by debuggers to set a breakpoint in code.

      For example, if you’re familiar with gdb, one of the simplest ways to make code stop executing at a particular point in the code is to add a breakpoint there.

      Gdb replaces the instruction at the breakpoint with 0xCC, which happens to be the opcode for INT 3 — generate interrupt 3. The CPU then generates interrupt 3, the kernel’s interrupt handler sends a signal (SIGTRAP) to the debugger, and thus the debugger will know it’s meant to start a debugging loop there.

      • Valmond@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        6 months ago

        Hey thank you!

        Not what I thought it was for sure 😃

        How does it work if an instruction gets replaced by the INT3 though?

        • A Basil Plant@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          6 months ago

          Excellent question!

          Before replacing the instruction with INT 3, the debugger keeps a note of what instruction was at that point in the code. When the CPU encounters INT 3, it hands control to the debugger.

          When the debugging operations are done, the debugger replaces the INT 3 with the original instruction and makes the instruction pointer go back one step, thereby ensuring that the original instruction is executed.