summaryrefslogtreecommitdiff
path: root/Source/Core/DebuggerUICommon/Src/CodeView.cpp
blob: 9ec4bf5d0cd3132ef25837db5c66e044b89f69cc (plain)
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright (C) 2003 Dolphin Project.

// 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, version 2.0.

// 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 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

// #include "Debugger.h"
// #include "Debugger/PPCDebugInterface.h"

// #include "PowerPC/SymbolDB.h"
#include "Common.h"
#include "StringUtil.h"
#include "DebuggerUIUtil.h"
#include "DebugInterface.h"

#include "Host.h"
#include "CodeView.h"
#include "SymbolDB.h"
// #include "JitWindow.h"

#include <wx/event.h>
#include <wx/clipbrd.h>
#include <wx/textdlg.h>

DEFINE_EVENT_TYPE(wxEVT_CODEVIEW_CHANGE);

enum
{
	IDM_GOTOINMEMVIEW = 12000,
	IDM_COPYADDRESS,
	IDM_COPYHEX,
	IDM_COPYCODE,
	IDM_INSERTBLR, IDM_INSERTNOP,
	IDM_RUNTOHERE,
	IDM_JITRESULTS,
	IDM_FOLLOWBRANCH,
	IDM_RENAMESYMBOL,
	IDM_PATCHALERT,
	IDM_COPYFUNCTION,
	IDM_ADDFUNCTION,
};


BEGIN_EVENT_TABLE(CCodeView, wxControl)
	EVT_ERASE_BACKGROUND(CCodeView::OnErase)
	EVT_PAINT(CCodeView::OnPaint)
	EVT_LEFT_DOWN(CCodeView::OnMouseDown)
	EVT_LEFT_UP(CCodeView::OnMouseUpL)
	EVT_MOTION(CCodeView::OnMouseMove)
	EVT_RIGHT_DOWN(CCodeView::OnMouseDown)
	EVT_RIGHT_UP(CCodeView::OnMouseUpR)
	EVT_MENU(-1, CCodeView::OnPopupMenu)
END_EVENT_TABLE()

CCodeView::CCodeView(DebugInterface* debuginterface, SymbolDB *symboldb, wxWindow* parent, wxWindowID Id, const wxSize& Size)
	: wxControl(parent, Id, wxDefaultPosition, Size),
      debugger(debuginterface),
	  symbol_db(symboldb),
	  plain(false),
      rowHeight(13),
      selection(0),
      oldSelection(0),
      selectionChanged(false),
      selecting(false),
      hasFocus(false),
      showHex(false),
      lx(-1),
      ly(-1)
{
	rowHeight = 13;
	align = debuginterface->getInstructionSize(0);
	curAddress = debuginterface->getPC();
	selection = 0;
}


wxSize CCodeView::DoGetBestSize() const
{
	wxSize bestSize;
	bestSize.x = 400;
	bestSize.y = 800;
	return(bestSize);
}


int CCodeView::YToAddress(int y)
{
	wxRect rc = GetClientRect();
	int ydiff = y - rc.height / 2 - rowHeight / 2;
	ydiff = (int)(floorf((float)ydiff / (float)rowHeight)) + 1;
	return(curAddress + ydiff * align);
}


void CCodeView::OnMouseDown(wxMouseEvent& event)
{
	int x = event.m_x;
	int y = event.m_y;

	if (x > 16)
	{
		oldSelection = selection;
		selection = YToAddress(y);
		// SetCapture(wnd);
		bool oldselecting = selecting;
		selecting = true;

		if (!oldselecting || (selection != oldSelection))
		{
			redraw();
		}
	}
	else
	{
		debugger->toggleBreakpoint(YToAddress(y));
		redraw();
		Host_UpdateBreakPointView();
	}

	event.Skip(true);
}


void CCodeView::OnMouseMove(wxMouseEvent& event)
{
	wxRect rc = GetClientRect();

	if (event.m_leftDown)
	{
		if (event.m_x > 16)
		{
			if (event.m_y < 0)
			{
				curAddress -= align;
				redraw();
			}
			else if (event.m_y > rc.height)
			{
				curAddress += align;
				redraw();
			}
			else
			{
				OnMouseDown(event);
			}
		}
	}

	event.Skip(true);
}

void CCodeView::RaiseEvent()
{
	wxCommandEvent ev(wxEVT_CODEVIEW_CHANGE, GetId());
	ev.SetEventObject(this);
	ev.SetInt(selection);
	GetEventHandler()->ProcessEvent(ev);
}

void CCodeView::OnMouseUpL(wxMouseEvent& event)
{
	if (event.m_x > 16)
	{
		curAddress = YToAddress(event.m_y);
		selecting = false;
		//ReleaseCapture();
		redraw();
	}
	RaiseEvent();
	event.Skip(true);
}

u32 CCodeView::AddrToBranch(u32 addr)
{
	char disasm[256];
	debugger->disasm(addr, disasm, 256);
	const char *mojs = strstr(disasm, "->0x");
	if (mojs)
	{
		u32 dest;
		sscanf(mojs+4,"%08x", &dest);
		return dest;
	}
	return 0;
}

void CCodeView::InsertBlrNop(int Blr)
{
	// Check if this address has been modified
	int find = -1;				
	for(u32 i = 0; i < BlrList.size(); i++)
	{
		if(BlrList.at(i).Address == selection)
		{ find = i; break;  }					
	}

	// Save the old value				
	if (find >= 0)
	{
		debugger->writeExtraMemory(0, BlrList.at(find).OldValue, selection);
		BlrList.erase(BlrList.begin() + find);
	}
	else
	{
		BlrStruct Temp;
		Temp.Address = selection;
		Temp.OldValue = debugger->readMemory(selection);
		BlrList.push_back(Temp);
		if (Blr == 0)
			debugger->insertBLR(selection, 0x4e800020);			
		else
			debugger->insertBLR(selection, 0x60000000);	
	}
	redraw();	
}

void CCodeView::OnPopupMenu(wxCommandEvent& event)
{
#if wxUSE_CLIPBOARD
	wxTheClipboard->Open();
#endif

	switch (event.GetId())
	{
	    case IDM_GOTOINMEMVIEW:
		    // CMemoryDlg::Goto(selection);
		    break;

#if wxUSE_CLIPBOARD
	    case IDM_COPYADDRESS:
		    wxTheClipboard->SetData(new wxTextDataObject(wxString::Format(_T("%08x"), selection)));
		    break;

		case IDM_COPYCODE:
		{
			char disasm[256];
			debugger->disasm(selection, disasm, 256);
		    wxTheClipboard->SetData(new wxTextDataObject(wxString::FromAscii(disasm))); //Have to manually convert from char* to wxString, don't have to in Windows?
		}
		    break;

	    case IDM_COPYHEX:
		    {
		    char temp[24];
		    sprintf(temp, "%08x", debugger->readInstruction(selection));
		    wxTheClipboard->SetData(new wxTextDataObject(wxString::FromAscii(temp)));
		    }
		    break;


		case IDM_COPYFUNCTION:
			{
			Symbol *symbol = symbol_db->GetSymbolFromAddr(selection);
			if (symbol) {
				std::string text;
				text = text + symbol->name + "\r\n";
				// we got a function
				u32 start = symbol->address;
				u32 end = start + symbol->size;
				for (u32 addr = start; addr != end; addr += 4) {
					char disasm[256];
					debugger->disasm(addr, disasm, 256);
					text = text + StringFromFormat("%08x: ", addr) + disasm + "\r\n";
				}
				wxTheClipboard->SetData(new wxTextDataObject(wxString::FromAscii(text.c_str())));
			}
			}
			break;
#endif

	    case IDM_RUNTOHERE:
		    debugger->setBreakpoint(selection);
		    debugger->runToBreakpoint();
		    redraw();
		    break;

		// Insert blr or restore old value
		case IDM_INSERTBLR:
			InsertBlrNop(0);
			redraw();
			break;
		case IDM_INSERTNOP:
			InsertBlrNop(1);
			redraw();
			break;

	    case IDM_JITRESULTS:
			debugger->showJitResults(selection);
		    break;
			
		case IDM_FOLLOWBRANCH:
			{
			u32 dest = AddrToBranch(selection);
			if (dest)
				Center(dest);
				RaiseEvent();
			}
			break;
	
		case IDM_ADDFUNCTION:
			{
			symbol_db->AddFunction(selection);
			Host_NotifyMapLoaded();
			}
			break;

		case IDM_RENAMESYMBOL:
			{
			Symbol *symbol = symbol_db->GetSymbolFromAddr(selection);
			if (symbol) {
				wxTextEntryDialog input_symbol(this, wxString::FromAscii("Rename symbol:"), wxGetTextFromUserPromptStr,
					wxString::FromAscii(symbol->name.c_str()));
				if (input_symbol.ShowModal() == wxID_OK) {
					symbol->name = input_symbol.GetValue().mb_str();
					redraw(); // Redraw to show the renamed symbol
				}
				Host_NotifyMapLoaded();
			}
			}
			break;

		case IDM_PATCHALERT:
			{

			}
			break;
	}

#if wxUSE_CLIPBOARD
	wxTheClipboard->Close();
#endif
	event.Skip(true);
}


void CCodeView::OnMouseUpR(wxMouseEvent& event)
{
	bool isSymbol = symbol_db->GetSymbolFromAddr(selection) != 0;
	// popup menu
	wxMenu* menu = new wxMenu;
	//menu->Append(IDM_GOTOINMEMVIEW, "&Goto in mem view");
	menu->Append(IDM_FOLLOWBRANCH, wxString::FromAscii("&Follow branch"))->Enable(AddrToBranch(selection) ? true : false);
	menu->AppendSeparator();
#if wxUSE_CLIPBOARD
	menu->Append(IDM_COPYADDRESS, wxString::FromAscii("Copy &address"));
	menu->Append(IDM_COPYFUNCTION, wxString::FromAscii("Copy &function"))->Enable(isSymbol);
	menu->Append(IDM_COPYCODE, wxString::FromAscii("Copy &code line"));
	menu->Append(IDM_COPYHEX, wxString::FromAscii("Copy &hex"));
	menu->AppendSeparator();
#endif
	menu->Append(IDM_RENAMESYMBOL, wxString::FromAscii("Rename &symbol"))->Enable(isSymbol);
	menu->AppendSeparator();
	menu->Append(IDM_RUNTOHERE, _T("&Run To Here"));
	menu->Append(IDM_ADDFUNCTION, _T("&Add function"));
	menu->Append(IDM_JITRESULTS, wxString::FromAscii("PPC vs X86"));
	menu->Append(IDM_INSERTBLR, wxString::FromAscii("Insert &blr"));
	menu->Append(IDM_INSERTNOP, wxString::FromAscii("Insert &nop"));
	menu->Append(IDM_PATCHALERT, wxString::FromAscii("Patch alert"));
	PopupMenu(menu);
	event.Skip(true);
}


void CCodeView::OnErase(wxEraseEvent& event)
{}


void CCodeView::OnPaint(wxPaintEvent& event)
{
	// --------------------------------------------------------------------
	// General settings
	// -------------------------
	wxPaintDC dc(this);
	wxRect rc = GetClientRect();

	dc.SetFont(DebuggerFont);

	struct branch
	{
		int src, dst, srcAddr;
	};

	branch branches[256];
	int numBranches = 0;
	// TODO: Add any drawing code here...
	int width   = rc.width;
	int numRows = (rc.height / rowHeight) / 2 + 2;
	//numRows=(numRows&(~1)) + 1;
	// ------------


	// --------------------------------------------------------------------
	// Colors and brushes
	// -------------------------	
	dc.SetBackgroundMode(wxTRANSPARENT); // the text background
	const wxChar* bgColor = _T("#ffffff");
	wxPen nullPen(bgColor);
	wxPen currentPen(_T("#000000"));
	wxPen selPen(_T("#808080")); // gray
	nullPen.SetStyle(wxTRANSPARENT);
	currentPen.SetStyle(wxSOLID);
	wxBrush currentBrush(_T("#FFEfE8")); // the ... ? ... is light gray
	wxBrush pcBrush(_T("#70FF70")); // the selected code line is green	
	wxBrush bpBrush(_T("#FF3311")); // red

	wxBrush bgBrush(bgColor);
	wxBrush nullBrush(bgColor);
	nullBrush.SetStyle(wxTRANSPARENT);

	dc.SetPen(nullPen);
	dc.SetBrush(bgBrush);
	dc.DrawRectangle(0, 0, 16, rc.height);
	dc.DrawRectangle(0, 0, rc.width, 5);
	// ------------


	// --------------------------------------------------------------------
	// Walk through all visible rows
	// -------------------------
	for (int i = -numRows; i <= numRows; i++)
	{
		unsigned int address = curAddress + i * align;

		int rowY1 = rc.height / 2 + rowHeight * i - rowHeight / 2;
		int rowY2 = rc.height / 2 + rowHeight * i + rowHeight / 2;

		wxString temp = wxString::Format(_T("%08x"), address);
		u32 col = debugger->getColor(address);
		wxBrush rowBrush(wxColor(col >> 16, col >> 8, col));
		dc.SetBrush(nullBrush);
		dc.SetPen(nullPen);
		dc.DrawRectangle(0, rowY1, 16, rowY2 - rowY1 + 2);

		if (selecting && (address == selection))
			dc.SetPen(selPen);
		else
			dc.SetPen(i == 0 ? currentPen : nullPen);

		if (address == debugger->getPC())
			dc.SetBrush(pcBrush);
		else
			dc.SetBrush(rowBrush);

		dc.DrawRectangle(16, rowY1, width, rowY2 - rowY1 + 1);
		dc.SetBrush(currentBrush);
		if (!plain) {
			dc.SetTextForeground(_T("#600000")); // the address text is dark red
			dc.DrawText(temp, 17, rowY1);
			dc.SetTextForeground(_T("#000000"));
		}

		// If running
		if (debugger->isAlive())
		{
			char dis[256];
			debugger->disasm(address, dis, 256);
			char* dis2 = strchr(dis, '\t');
			char desc[256] = "";

			// If we have a code
			if (dis2)
			{
				*dis2 = 0;
				dis2++;
				// look for hex strings to decode branches
				const char* mojs = strstr(dis2, "0x8");
				if (mojs)
				{
					for (int k = 0; k < 8; k++)
					{
						bool found = false;
						for (int j = 0; j < 22; j++)
						{
							if (mojs[k + 2] == "0123456789ABCDEFabcdef"[j])
								found = true;
						}
						if (!found)
						{
							mojs = 0;
							break;
						}
					}
				}
				if (mojs)
				{
					int offs;
					sscanf(mojs + 2, "%08x", &offs);
					branches[numBranches].src = rowY1 + rowHeight / 2;
					branches[numBranches].srcAddr = address / align;
					branches[numBranches++].dst = (int)(rowY1 + ((s64)(u32)offs - (s64)(u32)address) * rowHeight / align + rowHeight / 2);
					sprintf(desc, "-->%s", debugger->getDescription(offs).c_str());
					dc.SetTextForeground(_T("#600060")); // the -> arrow illustrations are purple
				}
				else
				{
					dc.SetTextForeground(_T("#000000"));
				}

				dc.DrawText(wxString::FromAscii(dis2), 140, rowY1);
				// ------------
			}

			// Show blr as its' own color
			if (strcmp(dis, "blr"))
				dc.SetTextForeground(_T("#007000")); // dark green
			else
				dc.SetTextForeground(_T("#8000FF")); // purple

			dc.DrawText(wxString::FromAscii(dis), plain ? 25 : 80, rowY1);

			if (desc[0] == 0)
			{
				strcpy(desc, debugger->getDescription(address).c_str());
			}

			if (!plain) {
				dc.SetTextForeground(_T("#0000FF")); // blue

				//char temp[256];
				//UnDecorateSymbolName(desc,temp,255,UNDNAME_COMPLETE);
				if (strlen(desc))
				{
					dc.DrawText(wxString::FromAscii(desc), 270, rowY1);
				}
			}

			// Show red breakpoint dot
			if (debugger->isBreakpoint(address))
			{
				dc.SetBrush(bpBrush);
				dc.DrawRectangle(2, rowY1 + 1, 11, 11);
			}
		}
	} // end of for
	// ------------


	// --------------------------------------------------------------------
	// Colors and brushes
	// -------------------------
	dc.SetPen(currentPen);
	
	for (int i = 0; i < numBranches; i++)
	{
	    int x = 370 + (branches[i].srcAddr % 9) * 8;
	    _MoveTo(x-2, branches[i].src);

		if (branches[i].dst < rc.height + 400 && branches[i].dst > -400)
	    {
			_LineTo(dc, x+2, branches[i].src);
			_LineTo(dc, x+2, branches[i].dst);
			_LineTo(dc, x-4, branches[i].dst);

			_MoveTo(x, branches[i].dst - 4);
			_LineTo(dc, x-4, branches[i].dst);
			_LineTo(dc, x+1, branches[i].dst+5);
	    }
		//else
		//{
			// This can be re-enabled when there is a scrollbar or
			// something on the codeview (the lines are too long)

			//_LineTo(dc, x+4, branches[i].src);
			//_MoveTo(x+2, branches[i].dst-4);
			//_LineTo(dc, x+6, branches[i].dst);
			//_LineTo(dc, x+1, branches[i].dst+5);
		//}

		//_LineTo(dc, x, branches[i].dst+4);
		//_LineTo(dc, x-2, branches[i].dst);
	}
	// ------------
}


void CCodeView::_LineTo(wxPaintDC &dc, int x, int y)
{
	dc.DrawLine(lx, ly, x, y);
	lx = x;
	ly = y;
}