summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/debug/points.cpp
blob: a19f0ab5619e6d32ea350c1588d4736ff2009e0e (plain) (blame)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    points.cpp

    Debugger breakpoints, watchpoints, etc.

***************************************************************************/

#include "emu.h"
#include "points.h"
#include "debugger.h"
#include "debugcon.h"


//**************************************************************************
//  DEBUG BREAKPOINT
//**************************************************************************

//-------------------------------------------------
//  debug_breakpoint - constructor
//-------------------------------------------------

debug_breakpoint::debug_breakpoint(
		device_debug *debugInterface,
		symbol_table &symbols,
		int index,
		offs_t address,
		const char *condition,
		const char *action) :
	m_debugInterface(debugInterface),
	m_index(index),
	m_enabled(true),
	m_address(address),
	m_condition(symbols, condition ? condition : "1"),
	m_action(action ? action : "")
{
}


//-------------------------------------------------
//  hit - detect a hit
//-------------------------------------------------

bool debug_breakpoint::hit(offs_t pc)
{
	// don't hit if disabled
	if (!m_enabled)
		return false;

	// must match our address
	if (m_address != pc)
		return false;

	// must satisfy the condition
	if (!m_condition.is_empty())
	{
		try
		{
			return (m_condition.execute() != 0);
		}
		catch (expression_error const &)
		{
			return false;
		}
	}

	return true;
}



//**************************************************************************
//  DEBUG WATCHPOINT
//**************************************************************************

//-------------------------------------------------
//  debug_watchpoint - constructor
//-------------------------------------------------

debug_watchpoint::debug_watchpoint(
		device_debug* debugInterface,
		symbol_table &symbols,
		int index,
		address_space &space,
		read_or_write type,
		offs_t address,
		offs_t length,
		const char *condition,
		const char *action) :
	m_debugInterface(debugInterface),
	m_phr(nullptr),
	m_phw(nullptr),
	m_space(space),
	m_index(index),
	m_enabled(true),
	m_type(type),
	m_address(address & space.addrmask()),
	m_length(length),
	m_condition(symbols, condition ? condition : "1"),
	m_action(action ? action : ""),
	m_installing(false)
{
	std::fill(std::begin(m_start_address), std::end(m_start_address), 0);
	std::fill(std::begin(m_end_address), std::end(m_end_address), 0);
	std::fill(std::begin(m_masks), std::end(m_masks), 0);

	int ashift = m_space.addr_shift();
	endianness_t endian = m_space.endianness();
	offs_t subamask = m_space.alignment() - 1;
	offs_t unit_size = ashift <= 0 ? 8 << -ashift : 8 >> ashift;
	offs_t start = m_address;
	offs_t end = (m_address + m_length - 1) & space.addrmask();
	if (end < start)
		end = space.addrmask();
	offs_t rstart = start & ~subamask;
	offs_t rend = end | subamask;
	u64 smask, mmask, emask;
	smask = mmask = emask = make_bitmask<u64>(m_space.data_width());
	if (start != rstart)
	{
		if (endian == ENDIANNESS_LITTLE)
			smask &= ~make_bitmask<u64>((start - rstart) * unit_size);
		else
			smask &= make_bitmask<u64>((rstart + subamask + 1 - start) * unit_size);
	}
	if (end != rend)
	{
		if (endian == ENDIANNESS_LITTLE)
			emask &= make_bitmask<u64>((subamask + 1 + end - rend) * unit_size);
		else
			emask &= ~make_bitmask<u64>((rend - end) * unit_size);
	}

	if (rend == (rstart | subamask) || smask == emask)
	{
		m_start_address[0] = rstart;
		m_end_address[0] = rend;
		m_masks[0] = smask & emask;
	}
	else
	{
		int idx = 0;
		if (smask != mmask)
		{
			m_start_address[idx] = rstart;
			m_end_address[idx] = rstart | subamask;
			m_masks[idx] = smask;
			idx++;
			rstart += subamask + 1;
		}
		if (mmask == emask)
		{
			m_start_address[idx] = rstart;
			m_end_address[idx] = rend;
			m_masks[idx] = emask;
		}
		else
		{
			if (rstart < rend - subamask)
			{
				m_start_address[idx] = rstart;
				m_end_address[idx] = rend - subamask - 1;
				m_masks[idx] = mmask;
				idx++;
			}
			m_start_address[idx] = rend - subamask;
			m_end_address[idx] = rend;
			m_masks[idx] = emask;
		}
	}

	install(read_or_write::READWRITE);
	m_notifier = m_space.add_change_notifier(
			[this] (read_or_write mode)
			{
				if (m_enabled)
				{
					install(mode);
				}
			});
}

debug_watchpoint::~debug_watchpoint()
{
	m_notifier.reset();
	m_phr.remove();
	m_phw.remove();
}

void debug_watchpoint::setEnabled(bool value)
{
	if (m_enabled != value)
	{
		m_enabled = value;
		if (m_enabled)
			install(read_or_write::READWRITE);
		else
		{
			m_installing = true;
			m_phr.remove();
			m_phw.remove();
			m_installing = false;
		}
	}
}

void debug_watchpoint::install(read_or_write mode)
{
	if (m_installing)
		return;
	m_installing = true;
	if (u32(mode) & u32(read_or_write::READ))
		m_phr.remove();
	if (u32(mode) & u32(read_or_write::WRITE))
		m_phw.remove();
	std::string name = util::string_format("wp@%x", m_address);
	switch (m_space.data_width())
	{
	case  8:
		if (u32(m_type) & u32(mode) & u32(read_or_write::READ))
			m_phr = m_space.install_read_tap(
					m_start_address[0], m_end_address[0], name,
					[this](offs_t offset, u8 &data, u8 mem_mask) {
						triggered(read_or_write::READ, offset, data, mem_mask);
					},
					&m_phr);
		if (u32(m_type) & u32(mode) & u32(read_or_write::WRITE))
			m_phw = m_space.install_write_tap(
					m_start_address[0], m_end_address[0], name,
					[this](offs_t offset, u8 &data, u8 mem_mask) {
						triggered(read_or_write::WRITE, offset, data, mem_mask);
					},
					&m_phw);
		break;

	case 16:
		for (int i=0; i != 3; i++)
			if (m_masks[i])
			{
				u16 mask = m_masks[i];
				if (u32(m_type) & u32(mode) & u32(read_or_write::READ))
					m_phr = m_space.install_read_tap(
							m_start_address[i], m_end_address[i], name,
							[this, mask](offs_t offset, u16 &data, u16 mem_mask) {
								if (mem_mask & mask)
									triggered(read_or_write::READ, offset, data, mem_mask);
							},
							&m_phr);
				if (u32(m_type) & u32(mode) & u32(read_or_write::WRITE))
					m_phw = m_space.install_write_tap(
							m_start_address[i], m_end_address[i], name,
							[this, mask](offs_t offset, u16 &data, u16 mem_mask) {
								if (mem_mask & mask)
									triggered(read_or_write::WRITE, offset, data, mem_mask);
							},
							&m_phw);
			}
		break;

	case 32:
		for (int i=0; i != 3; i++)
			if (m_masks[i])
			{
				u32 mask = m_masks[i];
				if (u32(m_type) & u32(mode) & u32(read_or_write::READ))
					m_phr = m_space.install_read_tap(
							m_start_address[i], m_end_address[i], name,
							[this, mask](offs_t offset, u32 &data, u32 mem_mask) {
								if (mem_mask & mask)
									triggered(read_or_write::READ, offset, data, mem_mask);
							},
							&m_phr);
				if (u32(m_type) & u32(mode) & u32(read_or_write::WRITE))
					m_phw = m_space.install_write_tap(
							m_start_address[i], m_end_address[i], name,
							[this, mask](offs_t offset, u32 &data, u32 mem_mask) {
								if (mem_mask & mask)
									triggered(read_or_write::WRITE, offset, data, mem_mask);
								},
								&m_phw);
			}
		break;

	case 64:
		for (int i=0; i != 3; i++)
			if (m_masks[i])
			{
				u64 mask = m_masks[i];
				if (u32(m_type) & u32(mode) & u32(read_or_write::READ))
					m_phr = m_space.install_read_tap(
							m_start_address[i], m_end_address[i], name,
							[this, mask](offs_t offset, u64 &data, u64 mem_mask) {
								if (mem_mask & mask)
									triggered(read_or_write::READ, offset, data, mem_mask);
							},
							&m_phr);
				if (u32(m_type) & u32(mode) & u32(read_or_write::WRITE))
					m_phw = m_space.install_write_tap(m_start_address[i], m_end_address[i], name,
							[this, mask](offs_t offset, u64 &data, u64 mem_mask) {
								if (mem_mask & mask)
									triggered(read_or_write::WRITE, offset, data, mem_mask);
							},
							&m_phw);
			}
		break;
	}
	m_installing = false;
}

void debug_watchpoint::triggered(read_or_write type, offs_t address, u64 data, u64 mem_mask)
{
	running_machine &machine = m_debugInterface->device().machine();
	debugger_manager &debug = machine.debugger();

	// if we're within debugger code, don't trigger
	if (debug.cpu().within_instruction_hook() || machine.side_effects_disabled())
		return;

	// adjust address, size & value_to_write based on mem_mask.
	offs_t size = 0;
	int ashift = m_space.addr_shift();
	offs_t unit_size = ashift <= 0 ? 8 << -ashift : 8 >> ashift;
	u64 unit_mask = make_bitmask<u64>(unit_size);

	offs_t address_offset = 0;

	if(!mem_mask)
		mem_mask = 0xff;

	while (!(mem_mask & unit_mask))
	{
		address_offset++;
		data >>= unit_size;
		mem_mask >>= unit_size;
	}

	while (mem_mask)
	{
		size++;
		mem_mask >>= unit_size;
	}

	data &= make_bitmask<u64>(size * unit_size);

	if (m_space.endianness() == ENDIANNESS_LITTLE)
		address += address_offset;
	else
		address += m_space.alignment() - size - address_offset;

	// stash the value that will be written or has just been read
	debug.cpu().set_wpinfo(address, data, size * unit_size);

	// protect against recursion
	debug.cpu().set_within_instruction(true);

	// must satisfy the condition
	if (!m_condition.is_empty())
	{
		try
		{
			if (!m_condition.execute())
			{
				debug.cpu().set_within_instruction(false);
				return;
			}
		}
		catch (expression_error &)
		{
			debug.cpu().set_within_instruction(false);
			return;
		}
	}

	// halt in the debugger by default
	bool was_stopped = debug.cpu().is_stopped();
	debug.cpu().set_execution_stopped();

	// evaluate the action
	if (!m_action.empty())
		debug.console().execute_command(m_action, false);

	// print a notification, unless the action made us go again
	if (debug.cpu().is_stopped())
	{
		std::string buffer;

		if (m_space.is_octal())
			buffer = string_format(type == read_or_write::READ ?
								   "Stopped at watchpoint %X reading %0*o from %0*o" :
								   "Stopped at watchpoint %X writing %0*o to %0*o",
								   m_index,
								   (size * unit_size + 2) / 3,
								   data,
								   (m_space.addr_width() + 2) / 3,
								   address);
		else
			buffer = string_format(type == read_or_write::READ ?
								   "Stopped at watchpoint %X reading %0*X from %0*X" :
								   "Stopped at watchpoint %X writing %0*X to %0*X",
								   m_index,
								   size * unit_size / 4,
								   data,
								   (m_space.addr_width() + 3) / 4,
								   address);

		const device_state_interface *state;
		if (debug.cpu().live_cpu() == &m_debugInterface->device() && m_debugInterface->device().interface(state))
		{
			debug.console().printf("%s (PC=%s)\n", buffer, state->state_string(STATE_GENPCBASE));
			m_debugInterface->compute_debug_flags();
		}
		else if (!was_stopped)
		{
			debug.console().printf("%s\n", buffer);
			debug.cpu().set_execution_running();
			debug.cpu().set_break_cpu(&m_debugInterface->device());
		}
		m_debugInterface->set_triggered_watchpoint(this);
	}

	debug.cpu().set_within_instruction(false);
}

//**************************************************************************
//  DEBUG REGISTERPOINT
//**************************************************************************

//-------------------------------------------------
//  debug_registerpoint - constructor
//-------------------------------------------------

debug_registerpoint::debug_registerpoint(symbol_table &symbols, int index, const char *condition, const char *action)
	: m_index(index),
		m_enabled(true),
		m_condition(symbols, (condition != nullptr) ? condition : "1"),
		m_action((action != nullptr) ? action : "")
{
}


//-------------------------------------------------
//  hit - detect a hit
//-------------------------------------------------

bool debug_registerpoint::hit()
{
	// don't hit if disabled
	if (!m_enabled)
		return false;

	// must satisfy the condition
	if (!m_condition.is_empty())
	{
		try
		{
			return (m_condition.execute() != 0);
		}
		catch (expression_error &)
		{
			return false;
		}
	}

	return true;
}