File: DialogBreakpoints.cpp

package info (click to toggle)
edb-debugger 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,124 kB
  • sloc: cpp: 46,241; xml: 4,998; ansic: 3,088; sh: 52; asm: 33; makefile: 5
file content (317 lines) | stat: -rw-r--r-- 9,546 bytes parent folder | download | duplicates (4)
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
/*
Copyright (C) 2006 - 2015 Evan Teran
                          evan.teran@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 2 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 "DialogBreakpoints.h"
#include "Expression.h"
#include "IBreakpoint.h"
#include "IDebugger.h"
#include "MemoryRegions.h"
#include "edb.h"

#include <QHeaderView>
#include <QInputDialog>
#include <QMessageBox>

#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QStringList>
#include <QTextStream>

namespace BreakpointManagerPlugin {

/**
 * @brief DialogBreakpoints::DialogBreakpoints
 * @param parent
 * @param f
 */
DialogBreakpoints::DialogBreakpoints(QWidget *parent, Qt::WindowFlags f)
	: QDialog(parent, f) {

	ui.setupUi(this);
	ui.tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
}

/**
 * @brief DialogBreakpoints::showEvent
 */
void DialogBreakpoints::showEvent(QShowEvent *) {
	connect(edb::v1::disassembly_widget(), SIGNAL(signalUpdated()), this, SLOT(updateList()));
	updateList();
}

/**
 * @brief DialogBreakpoints::hideEvent
 */
void DialogBreakpoints::hideEvent(QHideEvent *) {
	disconnect(edb::v1::disassembly_widget(), SIGNAL(signalUpdated()), this, SLOT(updateList()));
}

/**
 * @brief DialogBreakpoints::updateList
 */
void DialogBreakpoints::updateList() {

	ui.tableWidget->setSortingEnabled(false);
	ui.tableWidget->setRowCount(0);

	const IDebugger::BreakpointList breakpoint_state = edb::v1::debugger_core->backupBreakpoints();

	for (const std::shared_ptr<IBreakpoint> &bp : breakpoint_state) {

		//Skip if it's an internal bp; we don't want to insert a row for it.
		if (bp->internal()) {
			continue;
		}

		const int row = ui.tableWidget->rowCount();
		ui.tableWidget->insertRow(row);

		const edb::address_t address = bp->address();
		const QString condition      = bp->condition;
		const bool onetime           = bp->oneTime();
		const QString symname        = edb::v1::find_function_symbol(address, QString(), nullptr);
		const QString bytes          = edb::v1::format_bytes(bp->originalBytes(), bp->size());

		auto item = new QTableWidgetItem(edb::v1::format_pointer(address));
		item->setData(Qt::UserRole, address.toQVariant());

		ui.tableWidget->setItem(row, 0, item);
		ui.tableWidget->setItem(row, 1, new QTableWidgetItem(condition));
		ui.tableWidget->setItem(row, 2, new QTableWidgetItem(bytes));
		ui.tableWidget->setItem(row, 3, new QTableWidgetItem(onetime ? tr("One Time") : tr("Standard")));
		ui.tableWidget->setItem(row, 4, new QTableWidgetItem(symname));
	}

	ui.tableWidget->setSortingEnabled(true);
}

/**
 * @brief DialogBreakpoints::on_btnAdd_clicked
 */
void DialogBreakpoints::on_btnAdd_clicked() {

	bool ok;
	QString text = QInputDialog::getText(this, tr("Add Breakpoint"), tr("Address:"), QLineEdit::Normal, QString(), &ok);

	if (ok && !text.isEmpty()) {
		Expression<edb::address_t> expr(text, edb::v1::get_variable, edb::v1::get_value);

		const Result<edb::address_t, ExpressionError> address = expr.evaluate();
		if (address) {
			edb::v1::create_breakpoint(*address);
			updateList();

		} else {
			QMessageBox::critical(this, tr("Error In Address Expression!"), address.error().what());
		}
	}
}

/**
 * @brief DialogBreakpoints::on_btnCondition_clicked
 */
void DialogBreakpoints::on_btnCondition_clicked() {
	QList<QTableWidgetItem *> sel = ui.tableWidget->selectedItems();
	if (!sel.empty()) {
		QTableWidgetItem *const item = sel[0];
		bool ok;
		const edb::address_t address = item->data(Qt::UserRole).toULongLong();
		const QString condition      = edb::v1::get_breakpoint_condition(address);
		const QString text           = QInputDialog::getText(this, tr("Set Breakpoint Condition"), tr("Expression:"), QLineEdit::Normal, condition, &ok);
		if (ok) {
			edb::v1::set_breakpoint_condition(address, text);
			updateList();
		}
	}
}

/**
 * @brief DialogBreakpoints::on_btnRemove_clicked
 */
void DialogBreakpoints::on_btnRemove_clicked() {
	QList<QTableWidgetItem *> sel = ui.tableWidget->selectedItems();
	if (!sel.empty()) {
		QTableWidgetItem *const item = sel[0];
		const edb::address_t address = item->data(Qt::UserRole).toULongLong();
		edb::v1::remove_breakpoint(address);
	}
	updateList();
}

/**
 * @brief DialogBreakpoints::on_tableWidget_cellDoubleClicked
 * @param row
 * @param col
 */
void DialogBreakpoints::on_tableWidget_cellDoubleClicked(int row, int col) {
	switch (col) {
	case 0: // address
		if (QTableWidgetItem *const address_item = ui.tableWidget->item(row, 0)) {
			const edb::address_t address = address_item->data(Qt::UserRole).toULongLong();
			edb::v1::jump_to_address(address);
		}
		break;
	case 1: // condition
		if (QTableWidgetItem *const address_item = ui.tableWidget->item(row, 0)) {
			bool ok;
			const edb::address_t address = address_item->data(Qt::UserRole).toULongLong();
			const QString condition      = edb::v1::get_breakpoint_condition(address);
			const QString text           = QInputDialog::getText(this, tr("Set Breakpoint Condition"), tr("Expression:"), QLineEdit::Normal, condition, &ok);
			if (ok) {
				edb::v1::set_breakpoint_condition(address, text);
				updateList();
			}
		}
		break;
	}
}

/**
 * @brief DialogBreakpoints::on_btnImport_clicked
 *
 * Opens a file selection window to choose a file with newline-separated,
 * hex address breakpoints.
 */
void DialogBreakpoints::on_btnImport_clicked() {

	// Let the user choose the file; get the file name.
	QString home_directory = QDir::homePath();
	QString file_name      = QFileDialog::getOpenFileName(this, tr("Breakpoint Import File"), home_directory, nullptr);

	if (file_name.isEmpty()) {
		return;
	}

	// Open the file; fail if error or it doesn't exist.
	QFile file(file_name);
	if (!file.open(QIODevice::ReadOnly)) {
		QMessageBox::critical(this, tr("Error Opening File"), tr("Unable to open breakpoint file: %1").arg(file_name));
		return;
	}

	// Keep a list of any lines in the file that don't make valid breakpoints.
	QStringList errors;

	// Iterate through each line; attempt to make a breakpoint for each line.
	// Addreses should be prefixed with 0x, i.e. a hex number.
	// Count each breakpoint successfully made.
	int count = 0;
	Q_FOREVER {

		// Get the address
		QString line = file.readLine().trimmed();

		if (line.isEmpty()) {
			break;
		}

		bool ok;
		int base               = 16;
		edb::address_t address = line.toULong(&ok, base);

		// Skip if there's an issue.
		if (!ok) {
			errors.append(line);
			continue;
		}

		// If there's an issue with the line or address isn't in any region,
		// add to error list and skip.
		edb::v1::memory_regions().sync();
		std::shared_ptr<IRegion> p = edb::v1::memory_regions().findRegion(address);
		if (!p) {
			errors.append(line);
			continue;
		}

		// If the bp already exists, skip.  No error.
		if (edb::v1::debugger_core->findBreakpoint(address)) {
			continue;
		}

		// If the line was converted to an address, try to create the breakpoint.
		// Access debugger_core directly to avoid many possible error windows by edb::v1::create_breakpoint()
		if (const std::shared_ptr<IBreakpoint> bp = edb::v1::debugger_core->addBreakpoint(address)) {
			count++;
		} else {
			errors.append(line);
		}
	}

	// Report any errors to the user
	if (errors.size() > 0) {
		QMessageBox::warning(this, tr("Invalid Breakpoints"), tr("The following breakpoints were not made:\n%1").arg(errors.join("")));
	}

	// Report breakpoints successfully made
	QMessageBox::information(this, tr("Breakpoint Import"), tr("Imported %1 breakpoints.").arg(count));

	updateList();
}

/**
 * @brief DialogBreakpoints::on_btnExport_clicked
 *
 * Opens a file selection window to choose a file to save newline-separated,
 * hex address breakpoints.
 */
void DialogBreakpoints::on_btnExport_clicked() {

	//Get the current list of breakpoints
	const IDebugger::BreakpointList breakpoint_state = edb::v1::debugger_core->backupBreakpoints();

	//Create a list for addresses to be exported at the end
	QList<edb::address_t> export_list;

	//Go through our breakpoints and add for export if not one-time and not internal.
	for (const std::shared_ptr<IBreakpoint> &bp : breakpoint_state) {
		if (!bp->oneTime() && !bp->internal()) {
			export_list.append(bp->address());
		}
	}

	//If there are no breakpoints, fail
	if (export_list.isEmpty()) {
		QMessageBox::critical(this, tr("No Breakpoints"), tr("There are no breakpoints to export."));
		return;
	}

	//Now ask the user for a file, open it, and write each address to it.
	QString filename = QFileDialog::getSaveFileName(this, tr("Breakpoint Export File"), QDir::homePath());

	if (filename.isEmpty()) {
		return;
	}

	QFile file(filename);

	if (!file.open(QIODevice::WriteOnly)) {
		return;
	}

	for (edb::address_t address : export_list) {
		QString string_address = "0x" + QString::number(address, 16) + "\n";
		file.write(string_address.toLatin1());
	}

	QMessageBox::information(this, tr("Breakpoint Export"), tr("Exported %1 breakpoints").arg(export_list.size()));
}

}