Squash
Posts: 2
Joined: Sun May 15, 2016 5:24 pm

How set MAME breakpoint from GDB?

When debugging problems with a machine (say SNES), I set various breakpoints in gdb (debugging mame.exe).

Often, I'd like to also set a breakpoint for the emulated SNES depending on watchpoints I see in the gdb-debugged mame.exe, not by pressing "continue" in gdb and waiting for the internal MAME debugging console to come back up, but immediately.

I've tried various variants of this line:
(g_machine->firstcpu->debug())->breakpoint_set(0x1234, "", "")

But the best I can get it a sigsegv :-(

I generally inspect the emulated program counter with g_machine->firstcpu->pc, which seems to work on every line I've cared to break.

Just in case, I added __attribute__((optimize("O0"), noinline, used) to the definition of breakpoint_set.
User avatar
Tafoid
Posts: 351
Joined: Thu Nov 06, 2014 12:50 pm
Location: USA
Contact: Website

Re: How set MAME breakpoint from GDB?

Squash wrote:When debugging problems with a machine (say SNES), I set various breakpoints in gdb (debugging mame.exe).

Often, I'd like to also set a breakpoint for the emulated SNES depending on watchpoints I see in the gdb-debugged mame.exe, not by pressing "continue" in gdb and waiting for the internal MAME debugging console to come back up, but immediately.

I've tried various variants of this line:
(g_machine->firstcpu->debug())->breakpoint_set(0x1234, "", "")

But the best I can get it a sigsegv :-(

I generally inspect the emulated program counter with g_machine->firstcpu->pc, which seems to work on every line I've cared to break.

Just in case, I added __attribute__((optimize("O0"), noinline, used) to the definition of breakpoint_set.
Unfortunately, I don't know of any Developers that use GDB for that purpose. The built-in debugger should be able to accomplish all you need, I presume. For myself as a tester, GDB is useful for tracking crash/exceptions to a file/line number in the source, even checking variables which may be causing the issue. Perhaps someone else with more knowledge of the debugger/gdb may be able to provide more insight.
Squash
Posts: 2
Joined: Sun May 15, 2016 5:24 pm

Re: How set MAME breakpoint from GDB?

Tafoid wrote:Unfortunately, I don't know of any Developers that use GDB for that purpose. The built-in debugger should be able to accomplish all you need, I presume.
No, you misunderstand; I'm using gdb to debug the emulator itself (the x86 binary) to track down bugs in the emulation.

But I'm also using the build-in debugger at the same time, for instance to trigger certain behavior in the emulated system (let's say a write to emulated video memory) that will trigger bugs in the emulator (let's say a buffer pointer for the SDL renderrer that was accidentally overwritten).

So I run, from the command line:

Code: Select all

gdb mame.exe --args mame.exe -debug
Then I set a breakpoint in mame.exe like
break write_memory

When gdb stops at the write_memory function, I say let's say that the emulated system's program counter is 0x1234, and I realize I'd like the MAME debugger to break at 0x124a.

If I simply use continue in gdb, I won't have time to use the osd debugger interface to type in bp 1234, because the emulator is running at full speed and will skip past 0x124a immediately. Simply setting a breakpoint before whatever emulated code is causing write_memory to be called is not an option, if the system is large and you don't already have the problem narrowed down. In any case, it is not what I'm asking for; I want to set a MAME debugger breakpoint while mame.exe is stopped in gdb.

How can I set that breakpoint for the emulated system, from within gdb by calling the debugger interface code?
yz70s
Posts: 1
Joined: Fri May 01, 2015 6:12 pm

Re: How set MAME breakpoint from GDB?

Instead of setting a breakpoint inside the emulated machine from gdb, is much simpler to use gdb to have mame break in its debugger at the next machine language instruction.
Tell gdb to execute mame until it reaches the routine device_debug::instruction_hook in file debugcpu.cpp, then tell gdb to continue execution until it reaches the line

Code: Select all

if (debugcpu.execution_state() != EXECUTION_STATE_STOPPED && (m_flags & (DEBUG_FLAG_STOP_TIME | DEBUG_FLAG_STOP_PC | DEBUG_FLAG_LIVE_BP)) != 0)
then set the next statement to be executed at the line (5 lines below)

Code: Select all

machine.debugger().console().printf("Stopped at time interval %.1g\n", machine.time().as_double());
finally continue execution from that point and you will see that mame immediatiely pops up its debugger, usually at the instruction after the one that caused the memory access.

Return to “MAME Discussion”