From 180cb0d25f701cb4d2524b35311ca93e09321e39 Mon Sep 17 00:00:00 2001 From: Maxim Poliakovski Date: Thu, 3 Dec 2020 02:18:13 +0100 Subject: [PATCH] Make 68k debugger optional and disabled by default. --- CMakeLists.txt | 78 ++++++++++++++++++++-------------- debugger/CMakeLists.txt | 1 - debugger/debugger.cpp | 92 +++++++++++++++++++++++++---------------- 3 files changed, 104 insertions(+), 67 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc5b218..e5241d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,34 +16,40 @@ if (UNIX AND NOT APPLE) endif() endif() -# Build capstone as static library. -set(CAPSTONE_BUILD_STATIC ON CACHE BOOL "") +option(ENABLE_68K_DEBUGGER "Enable 68k debugging" OFF) -# Turn off anything unnecessary. -set(CAPSTONE_BUILD_SHARED OFF CACHE BOOL "") -set(CAPSTONE_BUILD_TESTS OFF CACHE BOOL "") -set(CAPSTONE_BUILD_CSTOOL OFF CACHE BOOL "") -set(CAPSTONE_BUILD_DIET OFF CACHE BOOL "") -#set(CAPSTONE_OSXKERNEL_SUPPORT OFF CACHE BOOL "") +if (ENABLE_68K_DEBUGGER) + # Build capstone as static library. + set(CAPSTONE_BUILD_STATIC ON CACHE BOOL "") -# Disable unused Capstone architectures. -set(CAPSTONE_ARM_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_ARM64_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_MIPS_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_PPC_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_SPARC_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_SYSZ_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_XCORE_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_X86_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_TMS320C64X_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_M680X_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_EVM_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_MOS65XX_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_WASM_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_BPF_SUPPORT OFF CACHE BOOL "") -set(CAPSTONE_RISCV_SUPPORT OFF CACHE BOOL "") + # Turn off anything unnecessary. + set(CAPSTONE_BUILD_SHARED OFF CACHE BOOL "") + set(CAPSTONE_BUILD_TESTS OFF CACHE BOOL "") + set(CAPSTONE_BUILD_CSTOOL OFF CACHE BOOL "") + set(CAPSTONE_BUILD_DIET OFF CACHE BOOL "") + set(CAPSTONE_OSXKERNEL_SUPPORT OFF CACHE BOOL "") -add_subdirectory(thirdparty/capstone EXCLUDE_FROM_ALL) + # Disable unused Capstone architectures. + set(CAPSTONE_ARM_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_ARM64_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_MIPS_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_PPC_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_SPARC_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_SYSZ_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_XCORE_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_X86_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_TMS320C64X_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_M680X_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_EVM_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_MOS65XX_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_WASM_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_BPF_SUPPORT OFF CACHE BOOL "") + set(CAPSTONE_RISCV_SUPPORT OFF CACHE BOOL "") + + ADD_DEFINITIONS(-DENABLE_68K_DEBUGGER) + + add_subdirectory(thirdparty/capstone EXCLUDE_FROM_ALL) +endif() add_subdirectory("${PROJECT_SOURCE_DIR}/cpu/ppc/") add_subdirectory("${PROJECT_SOURCE_DIR}/debugger/") @@ -94,13 +100,17 @@ add_executable(dingusppc ${SOURCES} $ if (WIN32) target_link_libraries(dingusppc "${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2.lib" "${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2main.lib" - cubeb capstone-static) + cubeb) else() #target_link_libraries(dingusppc libsoundio_static ${LIBSOUNDIO_LIBS} ${SDL2_LIBRARIES}) -target_link_libraries(dingusppc cubeb ${SDL2_LIBRARIES} capstone-static ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(dingusppc cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) target_include_directories(dingusppc PRIVATE ${CLI11_ROOT}) endif() +if (ENABLE_68K_DEBUGGER) + target_link_libraries(dingusppc capstone-static) +endif() + add_executable(testppc ${TEST_SOURCES} $ $ @@ -112,10 +122,14 @@ add_executable(testppc ${TEST_SOURCES} $ if (WIN32) target_link_libraries(testppc "${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2.lib" "${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2main.lib" - cubeb capstone-static) + cubeb) else() #target_link_libraries(testppc libsoundio_static ${LIBSOUNDIO_LIBS} ${SDL2_LIBRARIES}) -target_link_libraries(testppc cubeb ${SDL2_LIBRARIES} capstone-static ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(testppc cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) +endif() + +if (ENABLE_68K_DEBUGGER) + target_link_libraries(testppc capstone-static) endif() file(GLOB BENCH_SOURCES "${PROJECT_SOURCE_DIR}/benchmark/*.cpp") @@ -126,7 +140,11 @@ add_executable(bench1 ${BENCH_SOURCES} $ $ $) -target_link_libraries(bench1 cubeb ${SDL2_LIBRARIES} capstone-static ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(bench1 cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) + +if (ENABLE_68K_DEBUGGER) + target_link_libraries(bench1 capstone-static) +endif() add_custom_command( TARGET testppc POST_BUILD diff --git a/debugger/CMakeLists.txt b/debugger/CMakeLists.txt index acdf045..50d4d11 100644 --- a/debugger/CMakeLists.txt +++ b/debugger/CMakeLists.txt @@ -4,4 +4,3 @@ include_directories("${PROJECT_SOURCE_DIR}" file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") add_library(debugger OBJECT ${SOURCES}) -target_link_libraries(debugger PRIVATE capstone) diff --git a/debugger/debugger.cpp b/debugger/debugger.cpp index bf1b4c9..b7abb20 100644 --- a/debugger/debugger.cpp +++ b/debugger/debugger.cpp @@ -27,11 +27,13 @@ along with this program. If not, see . #include #include #include -#include #include "../cpu/ppc/ppcdisasm.h" #include "../cpu/ppc/ppcemu.h" #include "../cpu/ppc/ppcmmu.h" +#ifdef ENABLE_68K_DEBUGGER // optionally defined in CMakeLists.txt + #include +#endif using namespace std; @@ -72,25 +74,16 @@ static void show_help() { cout << " disas N,X -- disassemble N instructions starting at address X" << endl; cout << " X can be any number or a known register name" << endl; cout << " disas with no arguments defaults to disas 1,pc" << endl; +#ifdef ENABLE_68K_DEBUGGER cout << " context X -- switch to the debugging context X." << endl; cout << " X can be either 'ppc' (default) or '68k'" << endl; cout << " Use 68k for debugging emulated 68k code only." << endl; +#endif cout << " quit -- quit the debugger" << endl << endl; cout << "Pressing ENTER will repeat last command." << endl; } -static void disasm(uint32_t count, uint32_t address) { - PPCDisasmContext ctx; - - ctx.instr_addr = address; - ctx.simplified = true; - - for (int i = 0; i < count; i++) { - ctx.instr_code = mem_read_dbg(ctx.instr_addr, 4); - cout << uppercase << hex << ctx.instr_addr; - cout << " " << disassemble_single(&ctx) << endl; - } -} +#ifdef ENABLE_68K_DEBUGGER static void disasm_68k(uint32_t count, uint32_t address) { csh cs_handle; @@ -199,6 +192,35 @@ void exec_until_68k(uint32_t target_addr) } } +void print_68k_regs() +{ + int i; + string reg; + + for (i = 0; i < 8; i++) { + reg = "R" + to_string(i + 8); + cout << "D" << dec << i << " : " << uppercase << hex << get_reg(reg) << endl; + } + + for (i = 0; i < 7; i++) { + reg = "R" + to_string(i + 16); + cout << "A" << dec << i << " : " << uppercase << hex << get_reg(reg) << endl; + } + reg = "R1"; + cout << "A7 : " << uppercase << hex << get_reg(reg) << endl; + + reg = "R24"; + cout << "PC: " << uppercase << hex << get_reg(reg) - 2 << endl; + + reg = "R25"; + cout << "SR: " << uppercase << hex << ((get_reg(reg) & 0xFF) << 8) << endl; + + reg = "R26"; + cout << "CCR: " << uppercase << hex << get_reg(reg) << endl; +} + +#endif // ENABLE_68K_DEBUGGER + static void dump_mem(string& params) { int cell_size, chars_per_line; bool is_char; @@ -293,31 +315,17 @@ static void dump_mem(string& params) { cout << endl << endl; } -void print_68k_regs() -{ - int i; - string reg; +static void disasm(uint32_t count, uint32_t address) { + PPCDisasmContext ctx; - for (i = 0; i < 8; i++) { - reg = "R" + to_string(i + 8); - cout << "D" << dec << i << " : " << uppercase << hex << get_reg(reg) << endl; + ctx.instr_addr = address; + ctx.simplified = true; + + for (int i = 0; i < count; i++) { + ctx.instr_code = mem_read_dbg(ctx.instr_addr, 4); + cout << uppercase << hex << ctx.instr_addr; + cout << " " << disassemble_single(&ctx) << endl; } - - for (i = 0; i < 7; i++) { - reg = "R" + to_string(i + 16); - cout << "A" << dec << i << " : " << uppercase << hex << get_reg(reg) << endl; - } - reg = "R1"; - cout << "A7 : " << uppercase << hex << get_reg(reg) << endl; - - reg = "R24"; - cout << "PC: " << uppercase << hex << get_reg(reg) - 2 << endl; - - reg = "R25"; - cout << "SR: " << uppercase << hex << ((get_reg(reg) & 0xFF) << 8) << endl; - - reg = "R26"; - cout << "CCR: " << uppercase << hex << get_reg(reg) << endl; } void enter_debugger() { @@ -362,7 +370,9 @@ void enter_debugger() { #endif else if (cmd == "regs") { if (context == 2) { +#ifdef ENABLE_68K_DEBUGGER print_68k_regs(); +#endif } else { print_gprs(); } @@ -394,7 +404,9 @@ void enter_debugger() { } } else if (cmd == "step" || cmd == "si") { if (context == 2) { +#ifdef ENABLE_68K_DEBUGGER exec_single_68k(); +#endif } else { ppc_exec_single(); } @@ -407,7 +419,9 @@ void enter_debugger() { try { addr = str2addr(addr_str); if (context == 2) { +#ifdef ENABLE_68K_DEBUGGER exec_until_68k(addr); +#endif } else { ppc_exec_until(addr); } @@ -444,7 +458,9 @@ void enter_debugger() { } try { if (context == 2) { +#ifdef ENABLE_68K_DEBUGGER disasm_68k(inst_grab, addr); +#endif } else { disasm(inst_grab, addr); } @@ -454,9 +470,11 @@ void enter_debugger() { } else { /* disas without arguments defaults to disas 1,pc */ if (context == 2) { +#ifdef ENABLE_68K_DEBUGGER addr_str = "R24"; addr = get_reg(addr_str); disasm_68k(1, addr - 2); +#endif } else { addr_str = "PC"; addr = get_reg(addr_str); @@ -467,6 +485,7 @@ void enter_debugger() { expr_str = ""; ss >> expr_str; dump_mem(expr_str); +#ifdef ENABLE_68K_DEBUGGER } else if (cmd == "context") { expr_str = ""; ss >> expr_str; @@ -477,6 +496,7 @@ void enter_debugger() { } else { cout << "Unknown debugging context: " << expr_str << endl; } +#endif } else { cout << "Unknown command: " << cmd << endl; continue;