stardot / b-em

An opensource BBC Micro emulator for Win32 and Linux
http://stardot.org.uk/forums/viewtopic.php?f=4&t=10823
GNU General Public License v2.0
112 stars 57 forks source link

Is there a way to trigger a debugger breakpoint from running code? #175

Open davidgiven opened 1 year ago

davidgiven commented 1 year ago

...similar to int3 on x86 architecture?

Rationale: I'm working on some 6502 code that gets relocated at runtime and so I don't know the address of any routines. It'd be nice to be able to drop something like int3 into my code somewhere where I want to do debugging, run the program, and have the debugger stop there automatically.

I'm aware that the debugger stops on brk but it will also insist on executing the brk, which makes it less useful.

Thanks!

SteveFosdick commented 1 year ago

At the moment, there is no specific support for doing that, but I can see the use for it.

When assembling code with BASIC into DIMed space where it can change each time the program is run I had resorted to printing a few key addresses to allow breakpoints to be set.

On using BRK, I had hoped it would be possible to use the debugger rset command to change PC to be past the BRK but it seems that only changes the address pushed as the return address from handling the BRK, i.e. the BRK still happens.

It would be possible to implement something here fairly easily, I think. Looking at the opcode table, some previous 6502 emulators have used opcode &02 as a "trap to host" feature on the basis that this opcode would cause a real 6502 to halt until reset which isn't very useful so, presumably, no-one would write it in real code. That would work fine for debug code as long the breakpoint is not left in code that runs on a real 6502. On the 65C02 this opcode is a single-byte NOP.

SteveFosdick commented 1 year ago

I have pushed a branch to GitHub that includes an implementation as described above: https://github.com/stardot/b-em/tree/sf/selfbrk. I used the following test program:

   10 REM > SELFBRK
   20 DIM C% 100
   30 FOR N%=0 TO 3 STEP 3
   40   P%=C%
   50   [OPT N%
   60   .start
   70   EQUB &02
   80   LDA #&41
   90   JMP &FFEE
  100   ]
  110 NEXT
  120 CALL start