1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus-core - api/debugger.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2009 Richard Goedeken *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 /* This file contains the Core debugger functions which will be exported
23 * outside of the core library.
28 #define M64P_CORE_PROTOTYPES 1
29 #include "m64p_types.h"
30 #include "m64p_debugger.h"
31 #include "callbacks.h"
34 #include "debugger/dbg_types.h"
35 #include "debugger/dbg_breakpoints.h"
36 #include "debugger/dbg_decoder.h"
37 #include "debugger/dbg_memory.h"
38 #include "debugger/debugger.h"
39 #include "memory/memory.h"
40 #include "r4300/r4300.h"
42 extern unsigned int op; /* this is in r4300/pure_interp.c */
45 static void (*callback_ui_init)(void) = NULL;
46 static void (*callback_ui_update)(unsigned int) = NULL;
47 static void (*callback_ui_vi)(void) = NULL;
49 static void (*callback_core_compare)(unsigned int) = NULL;
50 static void (*callback_core_data_sync)(int, void *) = NULL;
52 /* global Functions for use by the Core */
54 void DebuggerCallback(eDbgCallbackType type, unsigned int param)
56 if (type == DEBUG_UI_INIT)
58 if (callback_ui_init != NULL)
59 (*callback_ui_init)();
61 else if (type == DEBUG_UI_UPDATE)
63 if (callback_ui_update != NULL)
64 (*callback_ui_update)(param);
66 else if (type == DEBUG_UI_VI)
68 if (callback_ui_vi != NULL)
73 void CoreCompareCallback(void)
75 if (callback_core_compare != NULL)
76 (*callback_core_compare)(op);
79 void CoreCompareDataSync(int length, void *ptr)
81 if (callback_core_data_sync != NULL)
82 (*callback_core_data_sync)(length, ptr);
85 /* exported functions for use by the front-end User Interface */
87 EXPORT m64p_error CALL DebugSetCoreCompare(void (*dbg_core_compare)(unsigned int), void (*dbg_core_data_sync)(int, void *))
89 callback_core_compare = dbg_core_compare;
90 callback_core_data_sync = dbg_core_data_sync;
91 return M64ERR_SUCCESS;
94 EXPORT m64p_error CALL DebugSetCallbacks(void (*dbg_frontend_init)(void), void (*dbg_frontend_update)(unsigned int pc), void (*dbg_frontend_vi)(void))
97 callback_ui_init = dbg_frontend_init;
98 callback_ui_update = dbg_frontend_update;
99 callback_ui_vi = dbg_frontend_vi;
100 return M64ERR_SUCCESS;
102 return M64ERR_UNSUPPORTED;
106 EXPORT m64p_error CALL DebugSetRunState(int runstate)
109 run = runstate; /* in debugger/debugger.c */
110 return M64ERR_SUCCESS;
112 return M64ERR_UNSUPPORTED;
116 EXPORT int CALL DebugGetState(m64p_dbg_state statenum)
121 case M64P_DBG_RUN_STATE:
123 case M64P_DBG_PREVIOUS_PC:
125 case M64P_DBG_NUM_BREAKPOINTS:
126 return g_NumBreakpoints;
127 case M64P_DBG_CPU_DYNACORE:
129 case M64P_DBG_CPU_NEXT_INTERRUPT:
130 return next_interupt;
132 DebugMessage(M64MSG_WARNING, "Bug: invalid m64p_dbg_state input in DebugGetState()");
137 DebugMessage(M64MSG_ERROR, "Bug: DebugGetState() called, but Debugger not supported in Core library");
142 EXPORT m64p_error CALL DebugStep(void)
145 if (!g_DebuggerActive)
146 return M64ERR_INVALID_STATE;
147 debugger_step(); /* in debugger/debugger.c */
148 return M64ERR_SUCCESS;
150 return M64ERR_UNSUPPORTED;
154 EXPORT void CALL DebugDecodeOp(unsigned int instruction, char *op, char *args, int pc)
157 r4300_decode_op(instruction, op, args, pc);
159 DebugMessage(M64MSG_ERROR, "Bug: DebugDecodeOp() called, but Debugger not supported in Core library");
163 EXPORT void * CALL DebugMemGetRecompInfo(m64p_dbg_mem_info recomp_type, unsigned int address, int index)
168 case M64P_DBG_RECOMP_OPCODE:
169 return get_recompiled_opcode(address, index);
170 case M64P_DBG_RECOMP_ARGS:
171 return get_recompiled_args(address, index);
172 case M64P_DBG_RECOMP_ADDR:
173 return get_recompiled_addr(address, index);
175 DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetRecompInfo() called with invalid m64p_dbg_mem_info");
179 DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetRecompInfo() called, but Debugger not supported in Core library");
184 EXPORT int CALL DebugMemGetMemInfo(m64p_dbg_mem_info mem_info_type, unsigned int address)
187 switch (mem_info_type)
189 case M64P_DBG_MEM_TYPE:
190 return get_memory_type(address);
191 case M64P_DBG_MEM_FLAGS:
192 return get_memory_flags(address);
193 case M64P_DBG_MEM_HAS_RECOMPILED:
194 return get_has_recompiled(address);
195 case M64P_DBG_MEM_NUM_RECOMPILED:
196 return get_num_recompiled(address);
198 DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetMemInfo() called with invalid m64p_dbg_mem_info");
202 DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetMemInfo() called, but Debugger not supported in Core library");
207 EXPORT void * CALL DebugMemGetPointer(m64p_dbg_memptr_type mem_ptr_type)
209 switch (mem_ptr_type)
211 case M64P_DBG_PTR_RDRAM:
213 case M64P_DBG_PTR_PI_REG:
215 case M64P_DBG_PTR_SI_REG:
217 case M64P_DBG_PTR_VI_REG:
219 case M64P_DBG_PTR_RI_REG:
221 case M64P_DBG_PTR_AI_REG:
224 DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetPointer() called with invalid m64p_dbg_memptr_type");
229 EXPORT unsigned long long CALL DebugMemRead64(unsigned int address)
232 if ((address & 3) == 0)
233 return read_memory_64(address);
235 return read_memory_64_unaligned(address);
237 DebugMessage(M64MSG_ERROR, "Bug: DebugMemRead64() called, but Debugger not supported in Core library");
242 EXPORT unsigned int CALL DebugMemRead32(unsigned int address)
245 if ((address & 3) == 0)
246 return read_memory_32(address);
248 return read_memory_32_unaligned(address);
250 DebugMessage(M64MSG_ERROR, "Bug: DebugMemRead32() called, but Debugger not supported in Core library");
255 EXPORT unsigned short CALL DebugMemRead16(unsigned int address)
258 return read_memory_16(address);
260 DebugMessage(M64MSG_ERROR, "Bug: DebugMemRead16() called, but Debugger not supported in Core library");
265 EXPORT unsigned char CALL DebugMemRead8(unsigned int address)
268 return read_memory_8(address);
270 DebugMessage(M64MSG_ERROR, "Bug: DebugMemRead8() called, but Debugger not supported in Core library");
275 EXPORT void CALL DebugMemWrite64(unsigned int address, unsigned long long value)
278 if ((address & 3) == 0)
279 write_memory_64(address, value);
281 write_memory_64_unaligned(address, value);
283 DebugMessage(M64MSG_ERROR, "Bug: DebugMemWrite64() called, but Debugger not supported in Core library");
287 EXPORT void CALL DebugMemWrite32(unsigned int address, unsigned int value)
290 if ((address & 3) == 0)
291 write_memory_32(address, value);
293 write_memory_32_unaligned(address, value);
295 DebugMessage(M64MSG_ERROR, "Bug: DebugMemWrite32() called, but Debugger not supported in Core library");
299 EXPORT void CALL DebugMemWrite16(unsigned int address, unsigned short value)
302 write_memory_16(address, value);
304 DebugMessage(M64MSG_ERROR, "Bug: DebugMemWrite16() called, but Debugger not supported in Core library");
308 EXPORT void CALL DebugMemWrite8(unsigned int address, unsigned char value)
311 write_memory_8(address, value);
313 DebugMessage(M64MSG_ERROR, "Bug: DebugMemWrite8() called, but Debugger not supported in Core library");
317 EXPORT void * CALL DebugGetCPUDataPtr(m64p_dbg_cpu_data cpu_data_type)
319 switch (cpu_data_type)
323 case M64P_CPU_REG_REG:
325 case M64P_CPU_REG_HI:
327 case M64P_CPU_REG_LO:
329 case M64P_CPU_REG_COP0:
331 case M64P_CPU_REG_COP1_DOUBLE_PTR:
332 return reg_cop1_double;
333 case M64P_CPU_REG_COP1_SIMPLE_PTR:
334 return reg_cop1_simple;
335 case M64P_CPU_REG_COP1_FGR_64:
336 return reg_cop1_fgr_64;
340 DebugMessage(M64MSG_ERROR, "Bug: DebugGetCPUDataPtr() called with invalid input m64p_dbg_cpu_data");
345 EXPORT int CALL DebugBreakpointLookup(unsigned int address, unsigned int size, unsigned int flags)
348 return lookup_breakpoint(address, size, flags);
350 DebugMessage(M64MSG_ERROR, "Bug: DebugBreakpointLookup() called, but Debugger not supported in Core library");
355 EXPORT int CALL DebugBreakpointCommand(m64p_dbg_bkp_command command, unsigned int index, void *ptr)
360 case M64P_BKP_CMD_ADD_ADDR:
361 return add_breakpoint(index);
362 case M64P_BKP_CMD_ADD_STRUCT:
363 return add_breakpoint_struct((breakpoint *) ptr);
364 case M64P_BKP_CMD_REPLACE:
365 replace_breakpoint_num(index, (breakpoint *) ptr);
367 case M64P_BKP_CMD_REMOVE_ADDR:
368 remove_breakpoint_by_address(index);
370 case M64P_BKP_CMD_REMOVE_IDX:
371 remove_breakpoint_by_num(index);
373 case M64P_BKP_CMD_ENABLE:
374 enable_breakpoint(index);
376 case M64P_BKP_CMD_DISABLE:
377 disable_breakpoint(index);
379 case M64P_BKP_CMD_CHECK:
380 return check_breakpoints(index);
382 DebugMessage(M64MSG_ERROR, "Bug: DebugBreakpointCommand() called with invalid input m64p_dbg_bkp_command");
386 DebugMessage(M64MSG_ERROR, "Bug: DebugBreakpointCommand() called, but Debugger not supported in Core library");