void test_c64_vice_basic_breakpoint(void**) { Session_action(s_session, PDAction_step); uint64_t breakAddress = 0x0813; stepToPC(0x080e); // Add a breakpoint at 0x0814 PDWriter* writer = s_session->currentWriter; PDWrite_eventBegin(writer, PDEventType_setBreakpoint); PDWrite_u64(writer, "address", breakAddress); PDWrite_eventEnd(writer); PDBinaryWriter_finalize(writer); Session_update(s_session); Session_action(s_session, PDAction_run); waitForBreak(breakAddress, 0, 0); stepToPC(0x080e); // Update the breakpoint to different address breakAddress = 0x0816; writer = s_session->currentWriter; PDWrite_eventBegin(writer, PDEventType_setBreakpoint); PDWrite_u64(writer, "address", breakAddress); PDWrite_u64(writer, "id", 1); PDWrite_eventEnd(writer); PDBinaryWriter_finalize(writer); Session_update(s_session); Session_action(s_session, PDAction_run); waitForBreak(breakAddress, 0, 0); // Delete the breakpoint stepToPC(0x080e); writer = s_session->currentWriter; PDWrite_eventBegin(writer, PDEventType_deleteBreakpoint); PDWrite_u32(writer, "id", 2); PDWrite_eventEnd(writer); PDBinaryWriter_finalize(writer); Session_update(s_session); Session_action(s_session, PDAction_run); }
static void setCallstack(DbgEngPlugin* plugin, PDWriter* writer) { const ULONG maxFrames = 1024; DEBUG_STACK_FRAME frames[maxFrames]; ULONG frameSize = sizeof(frames[0]); ULONG framesFilled = 0; plugin->debugControl->GetStackTrace(0, 0, 0, frames, frameSize, &framesFilled); printf("DbgEngPlugin: setCallstack\n"); if (framesFilled == 0) return; PDWrite_event_begin(writer, PDEventType_setCallstack); PDWrite_array_begin(writer, "callstack"); for (ULONG i = 0; i < framesFilled; ++i) { const DEBUG_STACK_FRAME& frame = frames[i]; PDWrite_array_entry_begin(writer); PDWrite_u64(writer, "address", frame.InstructionOffset); PDWrite_entry_end(writer); } PDWrite_array_end(writer); PDWrite_event_end(writer); }
void test_c64_vice_breakpoint_cond(void**) { CPUState state = { 0 }; Session_action(s_session, PDAction_step); const uint64_t breakAddress = 0x0816; stepToPC(0x080e); // Add a breakpoint at 0x0814 PDWriter* writer = s_session->currentWriter; PDWrite_eventBegin(writer, PDEventType_setBreakpoint); PDWrite_u64(writer, "address", breakAddress); PDWrite_string(writer, "condition", ".y == 0"); PDWrite_eventEnd(writer); PDBinaryWriter_finalize(writer); Session_update(s_session); Session_action(s_session, PDAction_run); waitForBreak(breakAddress, &state, CPUState_maskY); }
void test_c64_vice_get_disassembly(void**) { static Assembly assembly[] = { { 0x080e, "A9 22 LDA #$22" }, { 0x0810, "A2 32 LDX #$32" }, { 0x0812, "C8 INY" }, { 0x0813, "EE 20 D0 INC $D020" }, { 0x0816, "EE 21 D0 INC $D021" }, { 0x0819, "4C 0E 08 JMP $080E" }, { 0, 0 }, }; PDWriter* writer = s_session->currentWriter; PDWrite_eventBegin(writer, PDEventType_getDisassembly); PDWrite_u64(writer, "address_start", 0x80e); PDWrite_u32(writer, "instruction_count", (uint32_t)4); PDWrite_eventEnd(writer); PDBinaryWriter_finalize(writer); Session_update(s_session); PDReader* reader = s_session->reader; PDBinaryReader_initStream(reader, PDBinaryWriter_getData(s_session->currentWriter), PDBinaryWriter_getSize(s_session->currentWriter)); uint32_t event; while ((event = PDRead_getEvent(reader)) != 0) { if (event != PDEventType_setDisassembly) continue; PDReaderIterator it; assert_false(PDRead_findArray(reader, &it, "disassembly", 0) == PDReadStatus_notFound); int i = 0; while (PDRead_getNextEntry(reader, &it)) { uint64_t address; const char* text; PDRead_findU64(reader, &address, "address", it); PDRead_findString(reader, &text, "line", it); assert_non_null(assembly[i].text); assert_int_equal((int)assembly[i].address, (int)address); assert_string_equal(assembly[i].text, text); i++; } return; } }
static int update(void* userData, PDUI* uiFuncs, PDReader* inEvents, PDWriter* writer) { uint32_t event; DissassemblyData* data = (DissassemblyData*)userData; data->requestDisassembly = false; while ((event = PDRead_getEvent(inEvents)) != 0) { switch (event) { case PDEventType_setDisassembly: { setDisassemblyCode(data, inEvents); break; } case PDEventType_setExceptionLocation: { uint64_t location = 0; PDRead_findU64(inEvents, &location, "address", 0); if (location != data->location) { data->location = location; data->requestDisassembly = true; } PDRead_findU8(inEvents, &data->locationSize, "address_size", 0); break; } case PDEventType_setRegisters: { updateRegisters(data, inEvents); break; } } } renderUI(data, uiFuncs); if (data->requestDisassembly) { int pc = (int)(data->pc) & ~(BlockSize - 1); PDWrite_eventBegin(writer, PDEventType_getDisassembly); PDWrite_u64(writer, "address_start", (uint64_t)pc); PDWrite_u32(writer, "instruction_count", (uint32_t)BlockSize / 3); PDWrite_eventEnd(writer); } return 0; }
static void setCallstack(LLDBPlugin* plugin, PDWriter* writer) { lldb::SBThread thread(plugin->process.GetThreadByID(plugin->selectedThreadId)); printf("set callstack\n"); int frameCount = (int)thread.GetNumFrames(); if (frameCount == 0) return; // TODO: Write type of callstack PDWrite_event_begin(writer, PDEventType_SetCallstack); PDWrite_array_begin(writer, "callstack"); for (int i = 0; i < frameCount; ++i) { char fileLine[2048]; char moduleName[2048]; lldb::SBFrame frame = thread.GetFrameAtIndex((uint32_t)i); lldb::SBModule module = frame.GetModule(); lldb::SBCompileUnit compileUnit = frame.GetCompileUnit(); lldb::SBSymbolContext context(frame.GetSymbolContext(0x0000006e)); lldb::SBLineEntry entry(context.GetLineEntry()); uint64_t address = (uint64_t)frame.GetPC(); module.GetFileSpec().GetPath(moduleName, sizeof(moduleName)); PDWrite_array_entry_begin(writer); if (compileUnit.GetNumSupportFiles() > 0) { char filename[2048]; lldb::SBFileSpec fileSpec = compileUnit.GetSupportFileAtIndex(0); fileSpec.GetPath(filename, sizeof(filename)); sprintf(fileLine, "%s:%d", filename, entry.GetLine()); printf("callstack %s:%d\n", fileLine, entry.GetLine()); PDWrite_string(writer, "filename", filename); PDWrite_u32(writer, "line", entry.GetLine()); } PDWrite_string(writer, "module_name", moduleName); PDWrite_u64(writer, "address", address); PDWrite_entry_end(writer); } PDWrite_array_end(writer); PDWrite_event_end(writer); }
bool getMemory(void* dest, int* len, uint16_t inAddress, int readLength) { PDWriter* writer = s_session->currentWriter; PDWrite_eventBegin(writer, PDEventType_getMemory); PDWrite_u64(writer, "address_start", inAddress); PDWrite_u64(writer, "size", (uint32_t)readLength); PDWrite_eventEnd(writer); PDBinaryWriter_finalize(writer); Session_update(s_session); PDReader* reader = s_session->reader; PDBinaryReader_initStream(reader, PDBinaryWriter_getData(s_session->currentWriter), PDBinaryWriter_getSize(s_session->currentWriter)); uint32_t event; while ((event = PDRead_getEvent(reader)) != 0) { uint8_t* data; uint64_t dataSize; uint64_t address; if (event != PDEventType_setMemory) continue; assert_true(PDRead_findU64(reader, &address, "address", 0) & PDReadStatus_ok); assert_true((PDRead_findData(reader, (void**)&data, &dataSize, "data", 0) & PDReadStatus_typeMask) == PDReadType_data); memcpy(dest, data, dataSize); *len = (int)dataSize; return true; } return false; }
static void parseBreakpoint(PluginData* data, const char* res, PDWriter* writer) { Breakpoint* bp = 0; // TODO: loop, look for more breakpoints const char* breakStrOffset = strstr(res, "BREAK:"); if (!breakStrOffset) return; int id = atoi(breakStrOffset + 7); const char* address = strstr(breakStrOffset, "C:$"); if (!findBreakpointById(data, &bp, id)) { bp = createBreakpoint(); addBreakpoint(data, bp); } bp->id = id; if (address) bp->address = (uint16_t)strtol(address + 3, 0, 16); // add data or update existing PDWrite_eventBegin(writer, PDEventType_replyBreakpoint); PDWrite_u64(writer, "address", bp->address); PDWrite_u32(writer, "id", (uint32_t)id); PDWrite_eventEnd(writer); printf("sending reply back: breakpoint %x - %d\n", bp->address, id); // TODO: Condition //if (bp->condition) // free(bp->condition); //if (condition) // bp->condition = strdup(condition); // else // bp->condition = 0; }
static void setThreads(LLDBPlugin* plugin, PDWriter* writer) { uint32_t threadCount = plugin->process.GetNumThreads(); if (threadCount == 0) return; PDWrite_event_begin(writer, PDEventType_SetThreads); PDWrite_array_begin(writer, "threads"); for (uint32_t i = 0; i < threadCount; ++i) { lldb::SBThread thread = plugin->process.GetThreadAtIndex(i); lldb::SBFrame frame = thread.GetFrameAtIndex(0); uint64_t threadId = thread.GetThreadID(); const char* threadName = thread.GetName(); const char* queueName = thread.GetQueueName(); const char* functionName = frame.GetFunctionName(); PDWrite_array_entry_begin(writer); PDWrite_u64(writer, "id", threadId); if (threadName) PDWrite_string(writer, "name", threadName); else if (queueName) PDWrite_string(writer, "name", queueName); else PDWrite_string(writer, "name", "unknown_thread"); if (functionName) PDWrite_string(writer, "function", functionName); else PDWrite_string(writer, "function", "unknown_function"); PDWrite_entry_end(writer); } PDWrite_array_end(writer); PDWrite_event_end(writer); }
static int update(void* userData, PDUI* uiFuncs, PDReader* inEvents, PDWriter* writer) { uint32_t event; DissassemblyData* data = (DissassemblyData*)userData; while ((event = PDRead_getEvent(inEvents)) != 0) { switch (event) { case PDEventType_setDisassembly: { setDisassemblyCode(data, inEvents); break; } case PDEventType_setExceptionLocation: { PDRead_findU64(inEvents, &data->location, "address", 0); PDRead_findU8(inEvents, &data->locationSize, "address_size", 0); break; } case PDEventType_setRegisters: updateRegisters(data, inEvents); break; } } renderUI(data, uiFuncs); // Temporary req PDWrite_eventBegin(writer, PDEventType_getDisassembly); PDWrite_u64(writer, "address_start", 0); PDWrite_u32(writer, "instruction_count", (uint32_t)10); PDWrite_eventEnd(writer); return 0; }
static PDDebugState update(void* userData, PDAction action, PDReader* reader, PDWriter* writer) { PluginData* plugin = (PluginData*)userData; plugin->hasUpdatedRegistes = false; plugin->hasUpdatedExceptionLocation = false; onAction(plugin, action); processEvents(plugin, reader, writer); updateEvents(plugin, writer); if (plugin->hasUpdatedRegistes) { PDWrite_eventBegin(writer, PDEventType_setRegisters); PDWrite_arrayBegin(writer, "registers"); writeStatusRegister(writer, "flags", plugin->regs.flags); writeRegister(writer, "pc", 2, plugin->regs.pc, 1); writeRegister(writer, "sp", 1, plugin->regs.sp, 0); writeRegister(writer, "a", 1, plugin->regs.a, 0); writeRegister(writer, "x", 1, plugin->regs.x, 0); writeRegister(writer, "y", 1, plugin->regs.y, 0); PDWrite_arrayEnd(writer); PDWrite_eventEnd(writer); } if (plugin->hasUpdatedExceptionLocation) { PDWrite_eventBegin(writer, PDEventType_setExceptionLocation); PDWrite_u64(writer, "address", plugin->regs.pc); PDWrite_u8(writer, "address_size", 2); PDWrite_eventEnd(writer); } return plugin->state; }
static void get_memory(PluginData* data, PDReader* reader, PDWriter* writer) { uint64_t address; uint64_t size; size_t read_size = 0; PDRead_find_u64(reader, &address, "address_start", 0); PDRead_find_u64(reader, &size, "size", 0); // so this is a bit of a hack. If we request memory d000 we switch to io and then back // this isn't really correct but will do for now if (address == 0xdd00) { send_command(data, "bank io\n"); } uint8_t* memory = get_memory_internal(data, data->temp_file_full, &read_size, (uint16_t)(address), (uint16_t)(address + size)); if (address == 0xdd00) { send_command(data, "bank ram\n"); } if (memory) { // Lets do this! // + 2 is because VICE writes address at the start of the block and at the end // log_debug("c64_vice: sending memory\n", ""); PDWrite_event_begin(writer, PDEventType_SetMemory); PDWrite_u64(writer, "address", address); PDWrite_data(writer, "data", memory + 2, (uint32_t)(read_size - 3)); PDWrite_event_end(writer); // writer takes a copy free(memory); } }
static PDDebugState update(void* user_data, PDAction action, PDReader* reader, PDWriter* writer) { PluginData* plugin = (PluginData*)user_data; plugin->has_updated_registers = false; plugin->has_updated_exception_location = false; on_action(plugin, action); process_events(plugin, reader, writer); update_events(plugin); if (plugin->has_updated_registers) { log_debug("sending registens\n", ""); PDWrite_event_begin(writer, PDEventType_SetRegisters); PDWrite_array_begin(writer, "registers"); write_status_registers(writer, "flags", plugin->regs.flags); write_register(writer, "pc", 2, plugin->regs.pc, 1); write_register(writer, "sp", 1, plugin->regs.sp, 0); write_register(writer, "a", 1, plugin->regs.a, 0); write_register(writer, "x", 1, plugin->regs.x, 0); write_register(writer, "y", 1, plugin->regs.y, 0); PDWrite_array_end(writer); PDWrite_event_end(writer); } if (plugin->has_updated_exception_location) { PDWrite_event_begin(writer, PDEventType_SetExceptionLocation); PDWrite_u64(writer, "address", plugin->regs.pc); PDWrite_u8(writer, "address_size", 2); PDWrite_event_end(writer); } return plugin->state; }
static bool parse_breakpoint_call(PluginData* data, const char* res, int len, PDReader* reader, PDWriter* writer) { Breakpoint* bp = 0; (void)len; (void)reader; const char* breakStrOffset = strstr(res, "BREAK:"); if (!breakStrOffset) return false; int id = atoi(breakStrOffset + 7); const char* address = strstr(breakStrOffset, "C:$"); if (!find_breakpoint_by_id(data, &bp, id)) { bp = create_breakpoint(); add_breakpoint(data, bp); } bp->id = id; if (address) bp->address = (uint16_t)strtol(address + 3, 0, 16); // add data or update existing PDWrite_event_begin(writer, PDEventType_ReplyBreakpoint); PDWrite_u64(writer, "address", bp->address); PDWrite_u32(writer, "id", (uint32_t)id); PDWrite_event_end(writer); log_debug("sending reply back: breakpoint %x - %d\n", bp->address, id); // make sure we got all dat; return strstr(breakStrOffset, "(C:$"); }
static void setLocals(LLDBPlugin* plugin, PDWriter* writer) { lldb::SBThread thread(plugin->process.GetThreadByID(plugin->selectedThreadId)); lldb::SBFrame frame = thread.GetSelectedFrame(); lldb::SBValueList variables = frame.GetVariables(true, true, true, false); uint32_t count = variables.GetSize(); if (count <= 0) return; PDWrite_event_begin(writer, PDEventType_SetLocals); PDWrite_array_begin(writer, "locals"); for (uint32_t i = 0; i < count; ++i) { lldb::SBValue value = variables.GetValueAtIndex(i); PDWrite_array_entry_begin(writer); PDWrite_u64(writer, "address", value.GetAddress().GetFileAddress()); if (value.GetValue()) PDWrite_string(writer, "value", value.GetValue()); if (value.GetTypeName()) PDWrite_string(writer, "type", value.GetTypeName()); if (value.GetName()) PDWrite_string(writer, "name", value.GetName()); PDWrite_entry_end(writer); } PDWrite_array_end(writer); PDWrite_event_end(writer); }
static int update(void* userData, PDUI* uiFuncs, PDReader* inEvents, PDWriter* writer) { uint32_t event; (void)uiFuncs; (void)writer; BreakpointsData* data = (BreakpointsData*)userData; while ((event = PDRead_getEvent(inEvents)) != 0) { switch (event) { case PDEventType_setBreakpoint: { toogleBreakpointFileLine(data, inEvents); break; } case PDEventType_replyBreakpoint: { uint64_t address = 0; uint32_t id = (uint32_t) ~0; PDRead_findU64(inEvents, &address, "address", 0); PDRead_findU32(inEvents, &id, "id", 0); for (Breakpoint* bp : data->breakpoints) { if ((uint64_t)strtol(bp->location.address, 0, 16) == address) { bp->pendingCount = 0; // breakpoint accepted printf("bp view: updated breakpoint with id %d (was %d)\n", id, bp->id); bp->id = (int)id; break; } } break; } } } uiFuncs->text(""); if (uiFuncs->button("Add Breakpoint", { 0.0f, 0.0f } )) { Breakpoint* bp = createBreakpoint(data); data->breakpoints.push_back(bp); } uiFuncs->columns(4, "", true); //uiFuncs->text(""); uiFuncs->nextColumn(); uiFuncs->text("Name/Address"); uiFuncs->nextColumn(); uiFuncs->text("Label"); uiFuncs->nextColumn(); uiFuncs->text("Condition"); uiFuncs->nextColumn(); uiFuncs->text(""); uiFuncs->nextColumn(); for (auto& i : data->breakpoints) { Breakpoint* bp = i; bool needUpdate = false; uiFuncs->pushIdPtr(bp); //if (uiFuncs->checkbox("Enabled", &bp->enabled)) // needUpdate = true; if (bp->location.filename) { uiFuncs->inputText("##filename", bp->location.filename, (int)data->maxPath, 0, 0, 0); } else { if (uiFuncs->inputText("##address", bp->location.address, (int)data->maxPath, PDUIInputTextFlags_CharsHexadecimal | PDUIInputTextFlags_EnterReturnsTrue, 0, 0)) needUpdate = true; } uiFuncs->nextColumn(); uiFuncs->text(""); uiFuncs->nextColumn(); uiFuncs->text(""); // no condition for now //if (uiFuncs->inputText("##condition", bp->condition, (int)data->maxPath, PDInputTextFlags_EnterReturnsTrue, 0, 0)) // needUpdate = true; uiFuncs->nextColumn(); if (needUpdate) { // TODO: Add support for file/line PDWrite_eventBegin(writer, PDEventType_setBreakpoint); PDWrite_u64(writer, "address", (uint64_t)strtol(bp->location.address, 0, 16)); //if (bp->condition[0] != 0) // PDWrite_string(writer, "condition", bp->condition); if (bp->id != -1) PDWrite_u32(writer, "id", (uint32_t)bp->id); PDWrite_eventEnd(writer); printf("Sending breakpint\n"); } if (uiFuncs->button("Delete", {0.0f, 0.0f})) { PDWrite_eventBegin(writer, PDEventType_deleteBreakpoint); PDWrite_u32(writer, "id", (uint32_t)bp->id); PDWrite_eventEnd(writer); bp->markDelete = true; } uiFuncs->nextColumn(); uiFuncs->popId(); } // Delete breakpoints that have been marked delete for (auto i = data->breakpoints.begin(); i != data->breakpoints.end(); ++i) { Breakpoint* bp = *i; if (bp->pendingCount > 1) bp->pendingCount++; if (bp->markDelete || bp->pendingCount >= 10) i = data->breakpoints.erase(i); } return 0; }