Source

gb_emulator / gb_debugger_msvs / breakpoints / pending_breakpoint.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*  Copyright Š 2011 Chris Spencer <spencercw@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "stdafx.h"

#include "pending_breakpoint.h"

#include <iomanip>
#include <sstream>

#include "bound_breakpoint.h"
#include "breakpoint_resolution.h"
#include "code_context.h"
#include "com_enum.h"
#include "disassembly_stream.h"
#include "document_context.h"
#include "error_breakpoint.h"
#include "error_breakpoint_resolution.h"
#include "events.h"

using boost::shared_ptr;
using std::hex;
using std::wistringstream;
using std::wstring;

// IDebugPendingBreakpoint2 methods

HRESULT CPendingBreakpoint::CanBind(IEnumDebugErrorBreakpoints2 **ppErrorEnum)
{
	(void) ppErrorEnum;

	return E_NOTIMPL;
}

HRESULT CPendingBreakpoint::Bind()
{
	if (state_ == PBPS_DELETED)
	{
		return E_BP_DELETED;
	}

	// Get the location of the breakpoint
	BP_REQUEST_INFO requestInfo;
	HRESULT hr = request_->GetRequestInfo(BPREQI_BPLOCATION, &requestInfo);
	if (hr != S_OK)
	{
		return hr;
	}
	if (!(requestInfo.dwFields & BPREQI_BPLOCATION))
	{
		return E_FAIL;
	}

	switch (requestInfo.bpLocation.bpLocationType)
	{
	case BPLT_CODE_FILE_LINE:
		{
			// Source file line breakpoint
			BSTR filename;
			hr = requestInfo.bpLocation.bpLocation.bplocCodeFileLine.pDocPos->
				GetFileName(&filename);
			if (hr != S_OK)
			{
				return hr;
			}

			TEXT_POSITION startPos, endPos;
			hr = requestInfo.bpLocation.bpLocation.bplocCodeFileLine.pDocPos->
				GetRange(&startPos, &endPos);
			if (hr != S_OK)
			{
				return hr;
			}
			if (endPos.dwLine < startPos.dwLine)
			{
				return E_FAIL;
			}

			// Bind the breakpoint in the debugger (this is an asynchronous method; see
			// OnBreakpointSet() for the callback)
			gbd->setBreakpoint(wstring(filename), startPos.dwLine, endPos.dwLine - startPos.dwLine,
				this);

			// Clean up
			SysFreeString(filename);
		}
		break;

	case BPLT_CODE_CONTEXT:
		{
			// Code address breakpoint
			CCodeContext *context = NULL;
			HRESULT hr = requestInfo.bpLocation.bpLocation.bplocCodeContext.pCodeContext->
				QueryInterface(IID_GbDebuggerMsvsCodeContext, reinterpret_cast<void **>(&context));
			if (!SUCCEEDED(hr))
			{
				return hr;
			}

			uint32_t address = context->GetAddress();
			context->Release();

			// Bind the breakpoint
			gbd->setBreakpoint(address, this);
		}
		break;

	case BPLT_DATA_STRING:
		{
			// Parse the memory address. This is what is returned from CMemoryContext::GetInfo().
			UINT length =
				SysStringLen(requestInfo.bpLocation.bpLocation.bplocDataString.bstrDataExpr);
			if (length < 3)
			{
				return E_INVALIDARG;
			}
			wstring addressStr(requestInfo.bpLocation.bpLocation.bplocDataString.bstrDataExpr,
				length);

			if (addressStr.substr(0, 2) != L"0x")
			{
				return E_INVALIDARG;
			}
			addressStr = addressStr.substr(2);
			
			uint32_t address;
			wistringstream iss(addressStr);
			iss >> hex >> address;
			if (!iss)
			{
				return E_INVALIDARG;
			}

			// Bind the breakpoint
			gbd->setDataBreakpoint(static_cast<uint16_t>(address),
				static_cast<uint16_t>(requestInfo.bpLocation.bpLocation.bplocDataString.
				dwNumElements), this);
		}
		break;

	default:
		return E_FAIL;
	}

	return S_OK;
}

HRESULT CPendingBreakpoint::GetState(PENDING_BP_STATE_INFO *pState)
{
	(void) pState;

	return E_NOTIMPL;
}

HRESULT CPendingBreakpoint::GetBreakpointRequest(IDebugBreakpointRequest2 **ppBPRequest)
{
	if (!ppBPRequest)
	{
		return E_POINTER;
	}
	if (state_ == PBPS_DELETED)
	{
		return E_BP_DELETED;
	}

	*ppBPRequest = request_;
	(*ppBPRequest)->AddRef();
	return S_OK;
}

HRESULT CPendingBreakpoint::Virtualize(BOOL fVirtualize)
{
	(void) fVirtualize;

	return E_NOTIMPL;
}

HRESULT CPendingBreakpoint::Enable(BOOL fEnable)
{
	if (state_ == PBPS_DELETED)
	{
		return E_BP_DELETED;
	}

	// TODO: Implement
	(void) fEnable;

	return S_OK;
}

HRESULT CPendingBreakpoint::SetCondition(BP_CONDITION bpCondition)
{
	(void) bpCondition;

	return E_NOTIMPL;
}

HRESULT CPendingBreakpoint::SetPassCount(BP_PASSCOUNT bpPassCount)
{
	(void) bpPassCount;

	return E_NOTIMPL;
}

HRESULT CPendingBreakpoint::EnumBoundBreakpoints(IEnumDebugBoundBreakpoints2 **ppEnum)
{
	if (!ppEnum)
	{
		return E_POINTER;
	}
	if (state_ == PBPS_DELETED)
	{
		return E_BP_DELETED;
	}
	if (!boundBreakpoint_)
	{
		return S_FALSE;
	}

	typedef CComEnumWithCount<
		IEnumDebugBoundBreakpoints2,
		&IID_IEnumDebugBoundBreakpoints2,
		IDebugBoundBreakpoint2 *,
		_CopyInterface<IDebugBoundBreakpoint2> > CEnumDebugBoundBreakpoints;
	CComObject<CEnumDebugBoundBreakpoints> *enumerator;
	CComObject<CEnumDebugBoundBreakpoints>::CreateInstance(&enumerator);
	enumerator->AddRef();

	IDebugBoundBreakpoint2 *breakpoints[] = { boundBreakpoint_ };
	enumerator->Init(breakpoints, &breakpoints[1], NULL, AtlFlagCopy);

	*ppEnum = enumerator;
	return S_OK;
}

HRESULT CPendingBreakpoint::EnumErrorBreakpoints(
	BP_ERROR_TYPE bpErrorType,
	IEnumDebugErrorBreakpoints2 **ppEnum)
{
	if (!ppEnum)
	{
		return E_POINTER;
	}
	if (state_ == PBPS_DELETED)
	{
		return E_BP_DELETED;
	}
	if (!errorBreakpoint_)
	{
		return S_FALSE;
	}

	// Check the error type matches
	IDebugErrorBreakpointResolution2 *resolution;
	HRESULT hr = errorBreakpoint_->GetBreakpointResolution(&resolution);
	if (hr != S_OK)
	{
		return hr;
	}

	BP_ERROR_RESOLUTION_INFO resolutionInfo;
	hr = resolution->GetResolutionInfo(BPERESI_TYPE, &resolutionInfo);
	resolution->Release();
	if (hr != S_OK)
	{
		return hr;
	}
	if (!(resolutionInfo.dwFields & BPERESI_TYPE))
	{
		return E_FAIL;
	}

	if ((resolutionInfo.dwType & bpErrorType) != resolutionInfo.dwType)
	{
		return S_FALSE;
	}

	// Types match; create the enumerator
	typedef CComEnumWithCount<
		IEnumDebugErrorBreakpoints2,
		&IID_IEnumDebugErrorBreakpoints2,
		IDebugErrorBreakpoint2 *,
		_CopyInterface<IDebugErrorBreakpoint2> > CEnumDebugErrorBreakpoints;
	CComObject<CEnumDebugErrorBreakpoints> *enumerator;
	CComObject<CEnumDebugErrorBreakpoints>::CreateInstance(&enumerator);
	enumerator->AddRef();

	IDebugErrorBreakpoint2 *breakpoints[] = { errorBreakpoint_ };
	enumerator->Init(breakpoints, &breakpoints[1], NULL, AtlFlagCopy);

	*ppEnum = enumerator;
	return S_OK;
}

HRESULT CPendingBreakpoint::Delete()
{
	if (state_ == PBPS_DELETED)
	{
		return E_BP_DELETED;
	}
	
	// Delete the bound or error breakpoint if necessary
	if (boundBreakpoint_)
	{
		boundBreakpoint_->Delete();
		boundBreakpoint_ = NULL;
	}
	if (errorBreakpoint_)
	{
		errorBreakpoint_ = NULL;
	}
	state_ = PBPS_DELETED;

	return S_OK;
}

// CPendingBreakpoint methods

void CPendingBreakpoint::Init(IDebugBreakpointRequest2 *request, IDebugEventCallback2 *callback,
	IDebugEngine2 *engine, IDebugProgram2 *program, IDebugThread2 *thread)
{
	state_ = PBPS_ENABLED;
	request_ = request;
	callback_ = callback;
	engine_ = engine;
	program_ = program;
	thread_ = thread;
}

void CPendingBreakpoint::OnBreakpointSet(shared_ptr<GbBreakpoint> breakpoint)
{
	if (!breakpoint)
	{
		// The breakpoint could not be set; we need to send an error event

		// Create the error resolution
		CComObject<CErrorBreakpointResolution> *errorResolution;
		CComObject<CErrorBreakpointResolution>::CreateInstance(&errorResolution);
		errorResolution->AddRef();
		errorResolution->Init(program_, thread_);

		// Create the error breakpoint
		CComObject<CErrorBreakpoint> *errorBreakpoint;
		CComObject<CErrorBreakpoint>::CreateInstance(&errorBreakpoint);
		errorBreakpoint_ = errorBreakpoint;
		errorBreakpoint->Init(this, errorResolution);

		// Send the event
		CBreakpointErrorEvent *errorEvent = new CBreakpointErrorEvent(errorBreakpoint);
		errorEvent->SendEvent(callback_, engine_, program_, thread_);

		// Clean up
		errorResolution->Release();
	}
	else
	{
		// Create the document context if this is a source code breakpoint
		CComObject<CDocumentContext> *documentContext = NULL;
		if (breakpoint->sourceFile)
		{
			CComObject<CDocumentContext>::CreateInstance(&documentContext);
			documentContext->AddRef();
			documentContext->Init(breakpoint->sourceFile, breakpoint->line);
		}

		// Create the code context
		CComObject<CCodeContext> *codeContext;
		CComObject<CCodeContext>::CreateInstance(&codeContext);
		codeContext->AddRef();
		codeContext->Init(documentContext, static_cast<uint32_t>(breakpoint->address),
			breakpoint->module);

		// Create a breakpoint resolution
		CComObject<CBreakpointResolution> *resolution;
		CComObject<CBreakpointResolution>::CreateInstance(&resolution);
		resolution->AddRef();
		resolution->Init(codeContext, program_, thread_);

		// Create a bound breakpoint
		CComObject<CBoundBreakpoint> *boundBreakpoint;
		CComObject<CBoundBreakpoint>::CreateInstance(&boundBreakpoint);
		boundBreakpoint_ = boundBreakpoint;
		boundBreakpoint->Init(breakpoint, this, resolution);

		// Create an enumerator containing the bound breakpoint
		IEnumDebugBoundBreakpoints2 *enumerator;
		EnumBoundBreakpoints(&enumerator);

		// Send the event
		CBreakpointBoundEvent *boundEvent = new CBreakpointBoundEvent(enumerator, this);
		boundEvent->SendEvent(callback_, engine_, program_, thread_);

		// Clean up
		resolution->Release();
		codeContext->Release();
		if (documentContext)
		{
			documentContext->Release();
		}
	}
}

void CPendingBreakpoint::OnBreakpointHit()
{
	// Send the event to the SDM
	CCodeBreakpointEvent *breakpointEvnt = new CCodeBreakpointEvent(boundBreakpoint_);
	breakpointEvnt->SendEvent(callback_, engine_, program_, thread_);
}