-
Notifications
You must be signed in to change notification settings - Fork 0
/
GdbWidget.cpp
446 lines (390 loc) · 13.8 KB
/
GdbWidget.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
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
#include "GdbWidget.h"
#include "ui_GdbWidget.h"
#include <QDebug>
#include<QTextLayout>
#include <QTextEdit>
#include <QScrollBar>
#include <QVector>
#include <QTimer>
#include <QSettings>
#include <string.h>
#define qout qDebug()
GdbWidget::GdbWidget(QString path, QWidget *parent) :
QDialog (parent),
ui(new Ui::GdbWidget)
{
minTextCurse = 0;
ui->setupUi(this);
ui->checkBox_caseSensitive->setCheckState(Qt::CheckState::Checked);
caseSensitive = Qt::CaseSensitive;
ui->tEdit_shell->installEventFilter(this);
ui->tEdit_shell->setHtml("<body bgcolor=\"#000000\"></body>");
ui->tEdit_shell->setTextColor("green");
ui->tEdit_shell->insertPlainText("Bottod Shell V0.1\n");
ui->tEdit_shell->setFocus();
quickComplets.append("@CMD.clean");
loadseting();
gdb_path = path;
autoDebugPs.start(gdb_path);
autoDebugPs.waitForStarted();
connect(&autoDebugPs,&QProcess::readyReadStandardOutput,this,&GdbWidget::ReadProcessStandardOutput);
connect(&autoDebugPs,&QProcess::readyReadStandardError,this,&GdbWidget::ReadProcessStandardOutput);
minTextCurse = ui->tEdit_shell->textCursor().position();
dumpWidget = new DumpWidget(this);
connect(dumpWidget,&DumpWidget::dumpRamInfo,this,&GdbWidget::excuDumpInfo);
restoreWidget = new RestoreWidget(this);
connect(restoreWidget,&RestoreWidget::WriteRamInfo,this, &GdbWidget::excuWriteInfo);
}
void GdbWidget::setGdbPath(QString & path)
{
gdb_path = path;
autoDebugPs.close();
autoDebugPs.start(gdb_path);
autoDebugPs.waitForStarted();
}
GdbWidget::~GdbWidget()
{
saveseting();
autoDebugPs.close();
delete ui;
}
bool GdbWidget:: eventFilter(QObject* target,QEvent * event)
{
if(target == ui->tEdit_shell)
{
QTextCursor txtcur = ui->tEdit_shell->textCursor();
if(event->type()==QEvent::KeyPress)
{
if(ui->tEdit_shell->textCursor().position()< minTextCurse)
return true ;
QKeyEvent *k = static_cast<QKeyEvent *>(event);
if(k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter)
{
txtcur.movePosition(QTextCursor::End,QTextCursor::MoveAnchor);
txtcur.setPosition(minTextCurse,QTextCursor::KeepAnchor);
QString msg = txtcur.selectedText();
if(history.isEmpty() || QString::compare(msg,history.back()) != 0)
history.append(msg);
if(msg.compare("@CMD.clean") == 0)
{
ui->tEdit_shell->setText("");
ui->tEdit_shell->setTextColor(ui->cBox_ColorList->currentText());
ui->tEdit_shell->setHtml("<body bgcolor=\"#000000\"></body>");
ui->tEdit_shell->append("clean");
minTextCurse = ui->tEdit_shell->textCursor().position();
// ui->tEdit_shell->setFocus();
}
else
{
sendText(msg);
}
historyNum = history.size();
ui->tEdit_shell->append("");
txtcur.movePosition(QTextCursor::End,QTextCursor::MoveAnchor);
ui->tEdit_shell->setTextCursor(txtcur);
minTextCurse = ui->tEdit_shell->textCursor().position();
return true;
}
else if (static_cast<QKeyEvent *>(event)->key() == Qt::Key_Up)
{
QTextCursor tc = ui->tEdit_shell->textCursor();
if(history.size() == 0) return true;//如果没有历史记录,则不操作
tc.movePosition(QTextCursor::End,QTextCursor::MoveAnchor);
tc.setPosition(minTextCurse,QTextCursor::KeepAnchor);
tc.removeSelectedText();
if(historyNum == 0)
historyNum = history.size();
if(historyNum > history.size())
historyNum = 1;
ui->tEdit_shell->insertPlainText(history[historyNum-1]); //插入上一个输入的历史记录
lastTextCurse = ui->tEdit_shell->textCursor().position();
qout<<history[historyNum-1]<<"history size "<< history.length();
historyNum--;
qout<<"使用上一个记录"<<endl;
return true;
}
else if (static_cast<QKeyEvent *>(event)->key() == Qt::Key_Down)
{
QTextCursor tc = ui->tEdit_shell->textCursor();
if(history.size() == 0) return true;//如果没有历史记录,则不操作
tc.movePosition(QTextCursor::End,QTextCursor::MoveAnchor);
tc.setPosition(minTextCurse,QTextCursor::KeepAnchor);
tc.removeSelectedText();
if(historyNum > history.size())
historyNum = 1;
if(historyNum == 0)
historyNum = history.size();
ui->tEdit_shell->insertPlainText(history[historyNum-1]); //插入上一个输入的历史记录
lastTextCurse = ui->tEdit_shell->textCursor().position();
qout<<history[historyNum-1]<<"history size "<< history.length();
historyNum++;
qout<<"使用下一个记录"<<endl;
return true;
}
else if (static_cast<QKeyEvent *>(event)->key() == Qt::Key_Tab)
{
qout<<"tab"<<endl;
//得到最后一行的字符
QTextCursor txtcur = ui->tEdit_shell->textCursor();
txtcur.movePosition(QTextCursor::StartOfLine,QTextCursor::KeepAnchor);
QString msg = txtcur.selectedText();
int index = msg.lastIndexOf(' ');
if(index == -1)
index = 0;
else
index++;
QString ins = msg.mid(index);
const QString insertStr = findQuickCompletString( ins);
if(insertStr != "")
{
txtcur = ui->tEdit_shell->textCursor();
txtcur.movePosition(QTextCursor::Left,QTextCursor::KeepAnchor,ins.length());
txtcur.removeSelectedText();
ui->tEdit_shell->insertPlainText(findQuickCompletString( msg.mid( msg.lastIndexOf(' ')+1)));
}
qout<<"ins :" <<ins <<endl;
return true;
}
else if (static_cast<QKeyEvent *>(event)->key() == Qt::Key_Backspace)
{
QTextCursor tc = ui->tEdit_shell->textCursor();
qout<<"back";
if(tc.position()<=minTextCurse)
{
qout << "delete over";
return true;
}
}
else if(static_cast<QKeyEvent *>(event)->key() == Qt::Key_Left)
{
if(minTextCurse == ui->tEdit_shell->textCursor().position())
{
return true;
}
}
}
}
return QWidget::eventFilter(target,event);
}
void GdbWidget::sendText(QString & text)
{
for(int i = 0;i<text.length();i++)
{
if(text.at(i) == '\\' && (i+1)<text.length())
{
if(text.at(i+1) == '\\')
{
text.remove(i+1,1);
}
else if(text.at(i+1) == 't')
{
text.replace(i,1,'\t');
text.remove(i+1,1);
}
else if (text.at(i+1) == 'n')
{
text.replace(i,1,'\n');
text.remove(i+1,1);
}
}
}
text.append("\n");qout<<text<<endl;
qout<<"send has be called,will send string:"<<text<<endl;
autoDebugPs.write(text.toStdString().c_str());
}
void GdbWidget::ReadProcessStandardOutput()
{
QString ret = autoDebugPs.readAllStandardOutput();
QString ret_err = autoDebugPs.readAllStandardError();
if(ret.isEmpty())
ret = ret_err;
ui->tEdit_shell->append(ret); //get gdb msg
//reset cursor make readonly
QTextCursor txtcur = ui->tEdit_shell->textCursor();
txtcur.movePosition(QTextCursor::End,QTextCursor::MoveAnchor);
ui->tEdit_shell->setTextCursor(txtcur);
minTextCurse = ui->tEdit_shell->textCursor().position();
}
void GdbWidget::on_tEdit_shell_cursorPositionChanged()
{
//do nothing;
if(ui->tEdit_shell->textCursor().position()<minTextCurse)
ui->tEdit_shell->setReadOnly(true);
else
{
ui->tEdit_shell->setReadOnly(false);
}
}
void GdbWidget::on_tEdit_shell_textChanged()
{
QScrollBar *qbar = ui->tEdit_shell->verticalScrollBar();
qbar->setValue(qbar->maximum());
}
const QString & GdbWidget::findQuickCompletString(const QString & str)
{
int retindex = -1;
for(int i = 0;i<quickComplets.size();i++)
{
qout<<"find quitck index :" << i<<"quickComplets[i].indexOf("<<str<<") == "<<quickComplets[i].indexOf(str,0,caseSensitive) <<endl;
if(quickComplets[i].indexOf(str,0,caseSensitive) == 0)
{
if(retindex == -1)
retindex = i;
else
{
retindex = -1;
break;
}
}
}
if(retindex == -1)
return emptyStr;
else
return quickComplets[retindex];
}
void GdbWidget::on_cBox_ColorList_currentTextChanged(const QString &arg1)
{
qout<<"change str"<<arg1<<endl;
ui->tEdit_shell->setTextColor(arg1);
QTextCursor tc = ui->tEdit_shell->textCursor();
tc.movePosition(QTextCursor::End);
// ui->tEdit_shell->setTextCursor(tc);
ui->tEdit_shell->insertPlainText("text color change :"+arg1+"\n");
minTextCurse = ui->tEdit_shell->textCursor().position();
}
void GdbWidget::on_pButton_AddquickCompelat_clicked()
{
QString plaintext = ui->plaintEdit_quickCompleat->toPlainText();
plaintext.remove('\n');
plaintext.remove(' ');
qout<<"plaintext"<<plaintext;
QStringList sliplist = plaintext.split(',');
//插入到快捷补全的数组中去.
for(int i = 0;i<sliplist.size();i++)
{
bool is_exist = false;
if(sliplist[i].size() == 0) continue;
for(int j = 0;j<quickComplets.size();j++)
{
if(QString::compare(sliplist[i],quickComplets[j],caseSensitive) == 0)
{
qout<<"检测到"<<sliplist[i]<<"重复"<<endl;
is_exist = true;
break;
}
}
if(is_exist == true)
continue;
else
{
quickComplets.append(sliplist[i]);
}
}
}
void GdbWidget::on_GdbWidget_destroyed()
{
qout<<"关闭"<<endl;
}
void GdbWidget::saveseting(void)
{
QSettings setting("BottodGdbShell","BottodGdbShell");
setting.setValue("isAutosive",1);
setting.setValue("iscaseSensitive",caseSensitive);
setting.setValue("textcolor",ui->cBox_ColorList->currentText());
setting.sync();
}
void GdbWidget::loadseting(void)
{
QSettings setting("BottodGdbShell","BottodGdbShell");
if(setting.value("isAutosive").toInt() == 0)
return ;
if(setting.value("iscaseSensitive").toInt() == 1)
{
caseSensitive = Qt::CaseSensitive;
ui->checkBox_caseSensitive->setCheckState(Qt::CheckState::Checked);
}
else
{
caseSensitive = Qt::CaseInsensitive;
ui->checkBox_caseSensitive->setCheckState(Qt::CheckState::Unchecked);
}
ui->tEdit_shell->setTextColor(setting.value("textcolor").toString());
ui->cBox_ColorList->setCurrentText(setting.value("textcolor").toString());
ui->tEdit_shell->setTextColor(ui->cBox_ColorList->currentText());
setting.setValue("isAutosive",1);
setting.sync();
}
void GdbWidget::on_tabWidget_currentChanged(int index)
{
qout<<"index"<<index;
if(index == 1)
{
//reflash quick complet list
QString listStr = "";
for(int i = 0;i<quickComplets.size();i++)
{
listStr.append(quickComplets[i]+",\n");
}
listStr.remove( listStr.lastIndexOf(','),1);
ui->plaintEdit_quickCompleat->clear();
ui->plaintEdit_quickCompleat->setPlainText(listStr);
//reflash over
}
}
void GdbWidget::on_pButton_delquickCompelat_clicked()
{
QString selecttext = ui->plaintEdit_quickCompleat->textCursor().selectedText() ;
selecttext.remove(' ');
selecttext.remove(',');
selecttext.remove('\n');
quickComplets.remove( quickComplets.indexOf(selecttext),1);
ui->plaintEdit_quickCompleat->textCursor().removeSelectedText();
}
void GdbWidget::on_checkBox_caseSensitive_stateChanged(int arg1)
{
if(arg1== 0)
caseSensitive = Qt::CaseInsensitive;
else
{
caseSensitive = Qt::CaseSensitive;
}
}
void GdbWidget::on_pushButton_clicked()
{
QString cmd;
cmd.sprintf("target remote %s:%s\n",host.toStdString().c_str(),port.toStdString().c_str());
autoDebugPs.write(cmd.toStdString().c_str());
}
void GdbWidget::setInfo(QString & m_host, QString & m_port)
{
host = m_host;
port = m_port;
}
void GdbWidget::on_pushButton_4_clicked()
{
autoDebugPs.close();
autoDebugPs.start(gdb_path);
autoDebugPs.waitForStarted();
}
void GdbWidget::on_pushButton_2_clicked()
{
dumpWidget->show();
}
void GdbWidget::excuDumpInfo(QString str1,QString str2, QString str3)
{
first_address = str1;
last_address = str2;
filename = str3;
QString cmd = "";
cmd.sprintf("dump binary memory %s %s %s\n",filename.toStdString().c_str(),first_address.toStdString().c_str(),last_address.toStdString().c_str());
autoDebugPs.write(cmd.toStdString().c_str());
emit dumpingNow(filename);
}
void GdbWidget::excuWriteInfo(QString cmd)
{
autoDebugPs.write(cmd.toStdString().c_str());
}
void GdbWidget::on_pushButton_3_clicked()
{
restoreWidget->show();
}