forked from CosmosOS/Cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormMain.cs
474 lines (414 loc) · 15.6 KB
/
FormMain.cs
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
468
469
470
471
472
473
474
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Cosmos.Debug.GDB {
public partial class FormMain : Form {
protected class GdbAsmLine {
public readonly UInt32 mAddr;
public readonly string mLabel;
public readonly string mOp;
public readonly string mData = string.Empty;
public readonly bool mEIPHere;
public GdbAsmLine(string aInput) {
//"0x0056d2b9 <_end_data+0>:\tmov DWORD PTR ds:0x550020,ebx\n"
var s = GDB.Unescape(aInput);
var xSplit1 = s.Split(Global.TabSeparator, StringSplitOptions.RemoveEmptyEntries);
var xSplit2 = xSplit1[0].Split(Global.SpaceSeparator, StringSplitOptions.RemoveEmptyEntries);
int xIndex = 0;
//newer gdb above 6.6 or higher versions
if (xSplit2[0] == "=>")
{
mEIPHere = true;
xIndex = 1;
}
mAddr = Global.FromHexWithLeadingZeroX(xSplit2[xIndex]);
string xLabel;
if (xSplit2.Length > xIndex + 1) {
xLabel = xSplit2[xIndex + 1];
}
xSplit2 = xSplit1[1].Split(Global.SpaceSeparator, StringSplitOptions.RemoveEmptyEntries);
mOp = xSplit2[0];
if (xSplit2.Length > 1) {
for (int j = 1; j < xSplit2.Length; j++) {
mData += xSplit2[j] + " ";
}
mData = mData.TrimEnd();
}
}
public override string ToString() {
// First char reserved for breakpoint (*)
return " " + mAddr.ToString("X8") + ": " + mOp + " " + mData.TrimEnd();
}
}
const int MAX_RETRY = 3;
protected string mFuncName;
protected bool mCreated;
protected int mConnectRetry;
protected void OnGDBResponse(GDB.Response aResponse) {
try {
Windows.mLogForm.Log(aResponse);
var xCmdLine = aResponse.Command.ToLower();
if (xCmdLine == "info registers") {
Windows.mRegistersForm.UpdateRegisters(aResponse);
Windows.UpdateAfterRegisterUpdate();
}else if(xCmdLine.Length == 0) {
if (aResponse.Text.Count == 2 && aResponse.Text[0] == "Breakpoint")
{
// program breaks on aResponse.Text[1]
}
else
{
// contains address where we are
}
} else {
var xCmdParts = xCmdLine.Split(Global.SpaceSeparator);
var xCmd = xCmdParts[0];
if (xCmd.EndsWith("&")) {
xCmd = xCmd.Substring(0, xCmd.Length - 1);
}
if (xCmd == "disassemble") {
OnDisassemble(aResponse);
} else if (xCmd == "symbol-file") { // nothing
} else if (xCmd == "add-symbol-file") { //nothing
} else if (xCmd == "set") { // nothing
} else if (xCmd == "target") {
if (Global.GDB.Connected)
{
mitmRefresh.Enabled = true;
lablConnected.Visible = true;
lablRunning.Visible = true;
mitmConnect.Enabled = true;
butnConnect.Enabled = true;
mitmConnect.Text = "&Disconnect";
butnConnect.Text = "&Disconnect";
Settings.InitWindows();
lboxDisassemble.SetItems(Global.AsmSource.Lines);
}
else
{
if (mConnectRetry < MAX_RETRY + 1)
{
Connect();
}
else
{
mitmConnect.Enabled = true;
butnConnect.Enabled = true;
mitmConnect.Text = "&Connect";
butnConnect.Text = "&Connect";
lboxDisassemble.Items.Clear();
}
}
} else if (xCmd == "detach") {
if (false == Global.GDB.Connected)
{
mitmConnect.Text = "&Connect";
butnConnect.Text = "&Connect";
mitmRefresh.Enabled = false;
mitmContinue.Enabled = false;
butnContinue.Enabled = false;
mitmStepInto.Enabled = false;
mitmStepOver.Enabled = false;
lboxDisassemble.Items.Clear();
lablConnected.Visible = false;
lablRunning.Visible = false;
textCurrentFunction.Visible = false;
}
} else if (xCmd == "delete") {
Windows.mBreakpointsForm.OnDelete(aResponse);
} else if ((xCmd == "stepi") || (xCmd == "nexti")) {
} else if (xCmd == "continue" || xCmd== "fg") {
//lboxDisassemble.Items.Clear();
} else if (xCmd == "where") {
Windows.mCallStackForm.OnWhere(aResponse);
} else if (xCmd == "break") {
Windows.mBreakpointsForm.OnBreak(aResponse);
} else if (xCmd.StartsWith("x/")) {
Windows.mWatchesForm.OnWatchUpdate(aResponse);
} else {
throw new Exception("Unrecognized command response: " + aResponse.Command);
}
}
} catch (Exception e) {
MessageBox.Show("Exception: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
public void Disassemble(string aLabel) {
textCurrentFunction.Text = string.Empty;
textCurrentFunction.Visible = true;
// force space free at end
var xDisAsmCmd = "disassemble";
var xLabelTrimed = aLabel.TrimEnd();
if (xLabelTrimed.Length > 0)
xDisAsmCmd += " " + xLabelTrimed;
Global.GDB.SendCmd(xDisAsmCmd);
}
protected void OnDisassemble(GDB.Response xResponse)
{
var xResult = xResponse.Text;
// In some cases GDB might return no results. This is common when no symbols are loaded.
if (xResult.Count == 0)
return;
// Get function name
var xSplit = GDB.Unescape(xResult[0]).Split(Global.SpaceSeparator, StringSplitOptions.RemoveEmptyEntries);
mFuncName = xSplit[xSplit.Length - 1];
textCurrentFunction.Text = mFuncName;
// remove ':'
mFuncName = mFuncName.Substring(0, mFuncName.Length - 1);
int labelLine = Global.AsmSource.GetLineOfLabel(mFuncName);
labelLine++;
// 1 and -2 to eliminate header and footer line
for (int i = 1; i <= xResult.Count - 2; i++, labelLine++)
{
var asmLine = Global.AsmSource.Lines[labelLine];
while (asmLine.IsLabel ||
(asmLine.FirstToken != null && (asmLine.FirstToken == "global" || asmLine.FirstToken.StartsWith(";"))))
{
labelLine++;
asmLine = Global.AsmSource.Lines[labelLine];
}
var gdbLine = new GdbAsmLine(xResult[i]);
asmLine.Address = gdbLine.mAddr;
// check if line different, if so, we set a line for tooltip
string strGdbLine = gdbLine.ToString();
string gdbLineWithoutAddress = strGdbLine.Substring(strGdbLine.IndexOf(":") + 3);
string gdbLineWithoutAddressLower = gdbLineWithoutAddress.Replace(" ", string.Empty).ToLower().Replace("dwordptr",string.Empty);
string asmlineFromFile = asmLine.OrignalLine.TrimStart('\t', ' ').ToLower().Replace("dword", string.Empty);
string asmlineFromFileWithoutspace = asmlineFromFile.Replace(" ", string.Empty);
if (gdbLineWithoutAddressLower != asmlineFromFileWithoutspace)
{
asmLine.GDBLine = gdbLineWithoutAddress;
}
}
}
public void SetEIP(UInt32 aAddr) {
lboxDisassemble.SelectedAddress = aAddr;
}
public FormMain() {
InitializeComponent();
}
// TODO
// watches
// View stack
// If close without connect, it wipes out the settings file
private void mitmExit_Click(object sender, EventArgs e) {
Close();
}
private void mitmStepInto_Click(object sender, EventArgs e) {
Global.GDB.SendCmd("stepi&");
}
private void mitmStepOver_Click(object sender, EventArgs e) {
Global.GDB.SendCmd("nexti&");
}
protected void Connect() {
if (Settings.OutputPath == null)
{
// path of asm, obj and cgdb
using(var xDialog = new OpenFileDialog())
{
xDialog.Filter = "Symbols (*.asm;*.obj;*.mdf)|*.asm;*.obj;*.mdf";
xDialog.ShowHelp = true;
xDialog.HelpRequest += new EventHandler(xDialog_HelpRequest);
if (xDialog.ShowDialog(this) != System.Windows.Forms.DialogResult.OK)
return;
if (false == Settings.LoadOnFly(xDialog.FileName))
{
MessageBox.Show("Error on loading selection!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
return;
}
mitmSave.Enabled = true;
}
}
mitmConnect.Enabled = false;
butnConnect.Enabled = false;
mitmConnect.Text = "Try " + mConnectRetry;
butnConnect.Text = "Try " + mConnectRetry++;
if (false == mCreated)
{
Windows.CreateForms();
Global.AsmSource = new AsmFile(Path.Combine(Settings.OutputPath, Settings.AsmFile));
Global.GDB = new GDB(OnGDBResponse, OnRunStateChanged);
mCreated = true;
}
Global.GDB.Connect();
}
void xDialog_HelpRequest(object sender, EventArgs e)
{
MessageBox.Show("Select a folder which contain files of type asm, obj and mdf with same name.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
private void OnRunStateChanged(bool stopped)
{
if (InvokeRequired)
{
Invoke((MethodInvoker) delegate{OnRunStateChanged(stopped);});
return;
}
if (stopped)
{
lablRunning.Text = "Stopped";
mitmContinue.Enabled = true;
butnContinue.Enabled = true;
mitmBreak.Enabled = false;
butnBreak.Enabled = false;
mitmConnect.Enabled = true;
butnConnect.Enabled = true;
mitmStepInto.Enabled = true;
mitmStepOver.Enabled = true;
Windows.UpdateAllWindows();
}
else
{
lablRunning.Text = "Running";
mitmContinue.Enabled = false;
butnContinue.Enabled = false;
mitmBreak.Enabled = true;
butnBreak.Enabled = true;
mitmConnect.Enabled = false;
butnConnect.Enabled = false;
mitmStepInto.Enabled = false;
mitmStepOver.Enabled = false;
}
}
private void mitmConnect_Click(object sender, EventArgs e) {
if (!mitmConnect.Enabled)
return;
if (mCreated && Global.GDB.Connected)
{
Global.GDB.Disconnect();
}
else
{
mConnectRetry = 1;
Connect();
}
}
private void mitmRefresh_Click(object sender, EventArgs e) {
Windows.UpdateAllWindows();
}
private void mitmContinue_Click(object sender, EventArgs e) {
Global.GDB.SendCmd("continue&");
}
private void mitmMainViewCallStack_Click(object sender, EventArgs e) {
Windows.Show(Windows.mCallStackForm);
}
private void mitmMainViewWatches_Click(object sender, EventArgs e) {
Windows.Show(Windows.mWatchesForm);
}
protected FormWindowState mLastWindowState = FormWindowState.Normal;
private void FormMain_Resize(object sender, EventArgs e) {
if (WindowState == FormWindowState.Minimized) {
// Window is being minimized
Windows.Hide();
} else if ((mLastWindowState == FormWindowState.Minimized) && (WindowState != FormWindowState.Minimized)) {
// Window is being restored
Windows.Reshow();
}
mLastWindowState = WindowState;
}
private void mitmViewLog_Click(object sender, EventArgs e) {
Windows.Show(Windows.mLogForm);
}
private void FormMain_Load(object sender, EventArgs e) {
Windows.mMainForm = this;
}
private void mitmViewBreakpoints_Click(object sender, EventArgs e) {
Windows.Show(Windows.mBreakpointsForm);
}
private void mitmRegisters_Click(object sender, EventArgs e) {
Windows.Show(Windows.mRegistersForm);
}
private void FormMain_Shown(object sender, EventArgs e) {
mitmContinue.Enabled = false;
butnContinue.Enabled = false;
mitmBreak.Enabled = false;
butnBreak.Enabled = false;
mitmStepInto.Enabled = false;
mitmStepOver.Enabled = false;
mitmRefresh.Enabled = false;
if (Settings.OutputPath == null)
{
mitmSave.Enabled = false;
}
// Dont put this in load. Load happens in main call from Main.cs and on exceptions just
// goes out, no message.
// Also we want to show other forms after main form, not before.
// We also only want to run this once, not on each possible show.
if (mitmConnect.Enabled) {
if (Settings.AutoConnect) {
mConnectRetry = 1;
Connect();
}
}
}
private void BringWindowsToTop() {
mIgnoreNextActivate = true;
foreach (var xWindow in Windows.mForms) {
if (xWindow == this) {
continue;
}
if (xWindow.Visible) {
xWindow.Activate();
}
}
this.Activate();
}
private void mitmWindowsToForeground_Click(object sender, EventArgs e) {
BringWindowsToTop();
}
private bool mIgnoreNextActivate = false;
private void FormMain_Activated(object sender, EventArgs e) {
// Necessary else we get looping becuase BringWindowsToTop reactivates this.
if (mIgnoreNextActivate) {
mIgnoreNextActivate = false;
} else {
BringWindowsToTop();
}
}
private void mitmSave_Click(object sender, EventArgs e) {
Settings.Save();
}
private void mitemDisassemblyAddBreakpoint_Click(object sender, EventArgs e) {
if (lboxDisassemble.SelectedIndices.Count == 0)
return;
var x = Global.AsmSource.Lines[lboxDisassemble.SelectedIndices[0]];
if (x.Address != 0) {
Windows.mBreakpointsForm.AddBreakpoint("*0x" + x.Address.ToString("X8"));
}
}
private void mitmCopyToClipboard_Click(object sender, EventArgs e) {
if (lboxDisassemble.SelectedIndices.Count == 0)
return;
var x = Global.AsmSource.Lines[lboxDisassemble.SelectedIndices[0]];
Clipboard.SetText(x.ToString());
}
private void butnBreakpoints_Click(object sender, EventArgs e) {
mitmViewBreakpoints.PerformClick();
}
private void textCurrentFunction_TextChanged(object sender, EventArgs e) {
base.OnTextChanged(e);
using (Graphics g = textCurrentFunction.CreateGraphics()) {
SizeF size = g.MeasureString(textCurrentFunction.Text, textCurrentFunction.Font);
textCurrentFunction.Width = (int)size.Width + textCurrentFunction.Padding.Horizontal;
}
}
private void mitmBreak_Click(object sender, EventArgs e) {
Global.GDB.SendCmd("-exec-interrupt");
}
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
if (mCreated && Global.GDB.Connected)
{
Global.GDB.Disconnect();
Global.GDB.SendCmd("quit");
}
}
}
}