5 #include <MSP430_EEM.h>
8 #define REPORT_AND_RETURN(msg, result) { ReportLastMSP430Error(msg); return result; }
10 using namespace GDBServerFoundation;
11 using namespace MSP430Proxy;
24 #define MAKE_BP_COOKIE(type, data) (((type) & kBpCookieTypeMask) | ((data) & kBpCookieDataMask))
38 if (!__super::Initialize(settings))
41 if (m_pBreakpointManager)
46 MESSAGE_ID msgs = {0,};
54 C_ASSERT(
sizeof(LONG) ==
sizeof(
this));
55 if (MSP430_EEM_Init(sEEMHandler, (LONG)
this, &msgs) != STATUS_OK)
58 m_bEEMInitialized =
true;
63 memset(&bkpt, 0,
sizeof(bkpt));
65 bkpt.bpMode = BP_COMPLEX;
68 bkpt.bpAccess = BP_FETCH;
69 bkpt.bpAction = BP_BRK;
70 bkpt.bpOperat = BP_EQUAL;
71 bkpt.bpCondition = BP_NO_COND;
73 if (MSP430_EEM_SetBreakpoint(&m_SoftwareBreakpointWrapperHandle, &bkpt) != STATUS_OK)
74 REPORT_AND_RETURN(
"Cannot create a MDB breakpoint for handling software breakpoints",
false);
75 m_HardwareBreakpointsUsed++;
78 m_SoftwareBreakpointWrapperHandle = -1;
87 delete m_pBreakpointManager;
89 if (m_SoftwareBreakpointWrapperHandle != -1)
92 memset(&bkpt, 0,
sizeof(bkpt));
93 bkpt.bpMode = BP_CLEAR;
95 MSP430_EEM_SetBreakpoint(&m_SoftwareBreakpointWrapperHandle, &bkpt);
98 if (m_bEEMInitialized)
102 void MSP430Proxy::MSP430EEMTarget::EEMNotificationHandler(
MSP430_MSG wMsg, WPARAM wParam, LPARAM lParam )
109 m_LastStopEvent = wMsg;
110 m_TargetStopped.Set();
115 void MSP430Proxy::MSP430EEMTarget::sEEMHandler( UINT MsgId, UINT wParam, LONG lParam, LONG clientHandle )
125 m_TargetStopped.Wait();
127 bool breakIn = m_BreakInPending;
128 m_BreakInPending =
false;
132 if (MSP430_Read_Register(®PC, PC) != STATUS_OK)
136 if (m_RAMBreakpoints.IsBreakpointPresent((USHORT)regPC - 2))
137 bpState = SoftwareBreakpointManager::BreakpointActive;
141 case SoftwareBreakpointManager::BreakpointActive:
142 case SoftwareBreakpointManager::BreakpointInactive:
145 if (regPC == m_BreakpointAddrOfLastResumeOp)
149 if (m_LastResumeMode != SINGLE_STEP)
151 if (!DoResumeTarget(m_LastResumeMode))
158 if (MSP430_Write_Register(®PC, PC) != STATUS_OK)
161 if (bpState == SoftwareBreakpointManager::BreakpointInactive && m_LastResumeMode != SINGLE_STEP && !breakIn)
164 if (!DoResumeTarget(m_LastResumeMode))
170 case SoftwareBreakpointManager::NoBreakpoint:
181 case bptSoftwareBreakpoint:
182 switch(m_BreakpointPolicy)
185 return DoCreateCodeBreakpoint(
true, Address, pCookie);
187 return DoCreateCodeBreakpoint((m_HardwareBreakpointsUsed < m_DeviceInfo.nBreakpoints), Address, pCookie);
190 return DoCreateCodeBreakpoint(
false, Address, pCookie);
192 case bptHardwareBreakpoint:
194 return DoCreateCodeBreakpoint((m_HardwareBreakpointsUsed < m_DeviceInfo.nBreakpoints), Address, pCookie);
196 return DoCreateCodeBreakpoint(
true, Address, pCookie);
197 case bptAccessWatchpoint:
198 case bptWriteWatchpoint:
199 case bptReadWatchpoint:
202 return kGDBNotSupported;
206 memset(&bkpt, 0,
sizeof(bkpt));
207 bkpt.bpMode = BP_COMPLEX;
208 bkpt.lAddrVal = (ULONG)Address;
209 bkpt.bpType = BP_MAB;
212 case bptAccessWatchpoint:
213 bkpt.bpAccess = BP_NO_FETCH;
215 case bptReadWatchpoint:
216 bkpt.bpAccess = BP_READ;
218 case bptWriteWatchpoint:
219 bkpt.bpAccess = BP_WRITE;
222 bkpt.bpAction = BP_BRK;
223 bkpt.bpOperat = BP_EQUAL;
224 bkpt.bpCondition = BP_NO_COND;
228 if (MSP430_EEM_SetBreakpoint(&bpHandle, &bkpt) != STATUS_OK)
240 case bptSoftwareBreakpoint:
241 return DoRemoveCodeBreakpoint(
false, Address, Cookie);
242 case bptHardwareBreakpoint:
243 case bptReadWatchpoint:
244 case bptWriteWatchpoint:
245 case bptAccessWatchpoint:
246 return DoRemoveCodeBreakpoint(
true, Address, Cookie);
248 return kGDBNotSupported;
256 unsigned short originalInsn;
258 if (MSP430_Read_Register(®PC, PC) != STATUS_OK)
260 if (m_pBreakpointManager->GetOriginalInstruction(regPC, &originalInsn))
262 if (MSP430_Configure(SET_MDB_BEFORE_RUN, originalInsn) != STATUS_OK)
264 m_BreakpointAddrOfLastResumeOp = regPC;
267 m_BreakpointAddrOfLastResumeOp = -1;
269 m_LastResumeMode = mode;
270 m_TargetStopped.Reset();
271 if (!m_pBreakpointManager->CommitBreakpoints())
273 printf(
"ERROR: Cannot commit software breakpoints\n");
276 return __super::DoResumeTarget(mode);
282 m_BreakInPending =
true;
283 if (MSP430_State(&state, TRUE, NULL) != STATUS_OK)
290 GDBStatus status = __super::ReadTargetMemory(Address, pBuffer, pSizeInBytes);
291 if (status != kGDBSuccess)
294 m_pBreakpointManager->HideOrRestoreBreakpointsInMemorySnapshot((
unsigned)Address, pBuffer, *pSizeInBytes,
true);
301 GDBStatus status = __super::WriteTargetMemory(Address, pBuffer, sizeInBytes);
302 if (status != kGDBSuccess)
310 GDBServerFoundation::GDBStatus MSP430Proxy::MSP430EEMTarget::DoCreateCodeBreakpoint(
bool hardware, ULONGLONG Address, INT_PTR *pCookie )
315 memset(&bkpt, 0,
sizeof(bkpt));
316 bkpt.bpMode = BP_CODE;
317 bkpt.lAddrVal = (LONG)Address;
318 bkpt.bpCondition = BP_NO_COND;
319 bkpt.bpAction = BP_BRK;
322 if (MSP430_EEM_SetBreakpoint(&bpHandle, &bkpt) != STATUS_OK)
325 m_HardwareBreakpointsUsed++;
327 C_ASSERT(
sizeof(bpHandle) == 2);
334 if (!IsFLASHAddress(Address))
336 ULONG addr = (ULONG)(Address & ~1);
338 if (m_RAMBreakpoints.IsBreakpointPresent((USHORT)addr))
340 printf(
"Cannot set a breakpoint at 0x%04x. Breakpoint already exists.\n", (
unsigned)Address);
341 return kGDBUnknownError;
345 if (MSP430_Read_Memory(addr, (
char *)&insn, 2) != STATUS_OK)
350 insn = m_BreakpointInstruction;
351 if (MSP430_Write_Memory(addr, (
char *)&insn, 2) != STATUS_OK)
354 m_RAMBreakpoints.InsertBreakpoint((USHORT)addr);
358 if (!m_pBreakpointManager->SetBreakpoint((
unsigned)Address))
359 return kGDBUnknownError;
366 GDBServerFoundation::GDBStatus MSP430Proxy::MSP430EEMTarget::DoRemoveCodeBreakpoint(
bool hardware, ULONGLONG Address, INT_PTR Cookie )
371 memset(&bkpt, 0,
sizeof(bkpt));
372 bkpt.bpMode = BP_CLEAR;
376 if (MSP430_EEM_SetBreakpoint(&bpHandle, &bkpt) != STATUS_OK)
378 m_HardwareBreakpointsUsed--;
383 if (!IsFLASHAddress(Address))
386 unsigned short originalINSN = (
unsigned short)(Cookie & kBpCookieDataMask);
388 if (MSP430_Write_Memory(Address & ~1, (
char *)&originalINSN, 2) != STATUS_OK)
389 REPORT_AND_RETURN(
"Cannot remove a software breakpoint from RAM", kGDBUnknownError);
391 m_RAMBreakpoints.RemoveBreakpoint((USHORT)(Address & ~1));
396 if (!m_pBreakpointManager->RemoveBreakpoint((
unsigned)Address))
397 return kGDBUnknownError;