summaryrefslogtreecommitdiff
path: root/Source/Core/DebuggerWX/src/CodeWindow.cpp
blob: 319f6096911c2233607b177377c075595c4b9953 (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
// Copyright (C) 2003-2008 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 "RegisterWindow.h"
#include "LogWindow.h"
#include "BreakpointWindow.h"
#include "IniFile.h"

#include "wx/button.h"
#include "wx/textctrl.h"
#include "wx/listctrl.h"
#include "wx/thread.h"
#include "wx/listctrl.h"
#include "CodeWindow.h"
#include "HW/CPU.h"
#include "PowerPC/PowerPC.h"
#include "Host.h"

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

#include "Core.h"
#include "LogManager.h"

// ugly that this lib included code from the main
#include "../../DolphinWX/src/globals.h"

class SymbolList
	: public wxListCtrl
{
	wxString OnGetItemText(long item, long column)
	{
		return(_T("hello"));
	}
};

enum
{
	IDM_DEBUG_GO = 350,
	IDM_STEP,
	IDM_STEPOVER,
	IDM_SKIP,
	IDM_SETPC,
	IDM_GOTOPC,
	IDM_ADDRBOX,
	IDM_CALLSTACKLIST,
	IDM_SYMBOLLIST,
	IDM_INTERPRETER,
	IDM_DUALCORE,
	IDM_LOGWINDOW,
	IDM_REGISTERWINDOW,
	IDM_BREAKPOINTWINDOW
};

BEGIN_EVENT_TABLE(CCodeWindow, wxFrame)
EVT_BUTTON(IDM_DEBUG_GO,        CCodeWindow::OnCodeStep)
EVT_BUTTON(IDM_STEP,            CCodeWindow::OnCodeStep)
EVT_BUTTON(IDM_STEPOVER,        CCodeWindow::OnCodeStep)
EVT_BUTTON(IDM_SKIP,            CCodeWindow::OnCodeStep)
EVT_BUTTON(IDM_SETPC,           CCodeWindow::OnCodeStep)
EVT_BUTTON(IDM_GOTOPC,          CCodeWindow::OnCodeStep)
EVT_TEXT(IDM_ADDRBOX,           CCodeWindow::OnAddrBoxChange)
EVT_LISTBOX(IDM_SYMBOLLIST,     CCodeWindow::OnSymolListChange)
EVT_LISTBOX(IDM_CALLSTACKLIST,  CCodeWindow::OnCallstackListChange)
EVT_HOST_COMMAND(wxID_ANY,      CCodeWindow::OnHostMessage)
EVT_MENU(IDM_LOGWINDOW,         CCodeWindow::OnToggleLogWindow)
EVT_MENU(IDM_REGISTERWINDOW,    CCodeWindow::OnToggleRegisterWindow)
EVT_MENU(IDM_BREAKPOINTWINDOW,  CCodeWindow::OnToggleBreakPointWindow)
END_EVENT_TABLE()


CCodeWindow::CCodeWindow(const SCoreStartupParameter& _LocalCoreStartupParameter, wxWindow* parent, wxWindowID id,
		const wxString& title, const wxPoint& pos, const wxSize& size, long style)
	: wxFrame(parent, id, title, pos, size, style)
	, m_RegisterWindow(NULL)
	, m_logwindow(NULL)
{    
	CreateMenu(_LocalCoreStartupParameter);

	wxBoxSizer* sizerBig   = new wxBoxSizer(wxHORIZONTAL);
	wxBoxSizer* sizerRight = new wxBoxSizer(wxVERTICAL);
	wxBoxSizer* sizerLeft  = new wxBoxSizer(wxVERTICAL);

	DebugInterface* di = new PPCDebugInterface();

	sizerLeft->Add(callstack = new wxListBox(this, IDM_CALLSTACKLIST, wxDefaultPosition, wxSize(90, 100)), 0, wxEXPAND);
	sizerLeft->Add(symbols = new wxListBox(this, IDM_SYMBOLLIST, wxDefaultPosition, wxSize(90, 100), 0, NULL, wxLB_SORT), 1, wxEXPAND);
	codeview = new CCodeView(di, this, wxID_ANY);
	sizerBig->Add(sizerLeft, 2, wxEXPAND);
	sizerBig->Add(codeview, 5, wxEXPAND);
	sizerBig->Add(sizerRight, 0, wxEXPAND | wxALL, 3);
	sizerRight->Add(buttonGo = new wxButton(this, IDM_DEBUG_GO, _T("&Go")));
	sizerRight->Add(buttonStep = new wxButton(this, IDM_STEP, _T("&Step")));
	sizerRight->Add(buttonStepOver = new wxButton(this, IDM_STEPOVER, _T("Step &Over")));
	sizerRight->Add(buttonSkip = new wxButton(this, IDM_SKIP, _T("Ski&p")));
	sizerRight->Add(buttonGotoPC = new wxButton(this, IDM_GOTOPC, _T("G&oto PC")));
	sizerRight->Add(addrbox = new wxTextCtrl(this, IDM_ADDRBOX, _T("")));
	sizerRight->Add(new wxButton(this, IDM_SETPC, _T("S&et PC")));

	SetSizer(sizerBig);

	sizerLeft->SetSizeHints(this);
	sizerLeft->Fit(this);
	sizerRight->SetSizeHints(this);
	sizerRight->Fit(this);
	sizerBig->SetSizeHints(this);
	sizerBig->Fit(this);

	sync_event.Init();

	// additional dialogs
	if (IsLoggingActivated())
	{
		m_logwindow = new CLogWindow(this);
		m_logwindow->Show(true);
	}

	m_RegisterWindow = new CRegisterWindow(this);
	m_RegisterWindow->Show(true);

	m_BreakpointWindow = new CBreakPointWindow(this, this);
	m_BreakpointWindow->Show(true);


	UpdateButtonStates();


    int x,y,w,h;

    IniFile file;
    file.Load("Debugger.ini");

    file.Get("Code", "x", &x, GetPosition().x);
    file.Get("Code", "y", &y, GetPosition().y);
    file.Get("Code", "w", &w, GetSize().GetWidth());
    file.Get("Code", "h", &h, GetSize().GetHeight());
    this->SetSize(x, y, w, h);

    file.Get("BreakPoint", "x", &x, m_BreakpointWindow->GetPosition().x);
    file.Get("BreakPoint", "y", &y, m_BreakpointWindow->GetPosition().y);
    file.Get("BreakPoint", "w", &w, m_BreakpointWindow->GetSize().GetWidth());
    file.Get("BreakPoint", "h", &h, m_BreakpointWindow->GetSize().GetHeight());
    m_BreakpointWindow->SetSize(x, y, w, h);

    file.Get("LogWindow", "x", &x, m_logwindow->GetPosition().x);
    file.Get("LogWindow", "y", &y, m_logwindow->GetPosition().y);
    file.Get("LogWindow", "w", &w, m_logwindow->GetSize().GetWidth());
    file.Get("LogWindow", "h", &h, m_logwindow->GetSize().GetHeight());
    m_logwindow->SetSize(x, y, w, h);

    file.Get("RegisterWindow", "x", &x, m_RegisterWindow->GetPosition().x);
    file.Get("RegisterWindow", "y", &y, m_RegisterWindow->GetPosition().y);
    file.Get("RegisterWindow", "w", &w, m_RegisterWindow->GetSize().GetWidth());
    file.Get("RegisterWindow", "h", &h, m_RegisterWindow->GetSize().GetHeight());
    m_RegisterWindow->SetSize(x, y, w, h);
}


CCodeWindow::~CCodeWindow()
{
    IniFile file;
    file.Load("Debugger.ini");

    file.Set("Code", "x", GetPosition().x);
    file.Set("Code", "y", GetPosition().y);
    file.Set("Code", "w", GetSize().GetWidth());
    file.Set("Code", "h", GetSize().GetHeight());

    file.Set("BreakPoint", "x", m_BreakpointWindow->GetPosition().x);
    file.Set("BreakPoint", "y", m_BreakpointWindow->GetPosition().y);
    file.Set("BreakPoint", "w", m_BreakpointWindow->GetSize().GetWidth());
    file.Set("BreakPoint", "h", m_BreakpointWindow->GetSize().GetHeight());

    file.Set("LogWindow", "x", m_logwindow->GetPosition().x);
    file.Set("LogWindow", "y", m_logwindow->GetPosition().y);
    file.Set("LogWindow", "w", m_logwindow->GetSize().GetWidth());
    file.Set("LogWindow", "h", m_logwindow->GetSize().GetHeight());

    file.Set("RegisterWindow", "x", m_RegisterWindow->GetPosition().x);
    file.Set("RegisterWindow", "y", m_RegisterWindow->GetPosition().y);
    file.Set("RegisterWindow", "w", m_RegisterWindow->GetSize().GetWidth());
    file.Set("RegisterWindow", "h", m_RegisterWindow->GetSize().GetHeight());

    file.Save("Debugger.ini");
}


void CCodeWindow::CreateMenu(const SCoreStartupParameter& _LocalCoreStartupParameter)
{
	wxMenuBar* pMenuBar = new wxMenuBar(wxMB_DOCKABLE);

	{
		wxMenu* pDebugMenu = new wxMenu;
		wxMenuItem* interpreter = pDebugMenu->Append(IDM_INTERPRETER, _T("&Interpreter"), wxEmptyString, wxITEM_CHECK);
		interpreter->Check(!_LocalCoreStartupParameter.bUseDynarec);

//		wxMenuItem* dualcore = pDebugMenu->Append(IDM_DUALCORE, _T("&DualCore"), wxEmptyString, wxITEM_CHECK);
//		dualcore->Check(_LocalCoreStartupParameter.bUseDualCore);

		pMenuBar->Append(pDebugMenu, _T("&Core Startup"));
	}

	{
		wxMenu* pDebugDialogs = new wxMenu;

		if (IsLoggingActivated())
		{
			wxMenuItem* pLogWindow = pDebugDialogs->Append(IDM_LOGWINDOW, _T("&LogManager"), wxEmptyString, wxITEM_CHECK);
			pLogWindow->Check(true);
		}

		wxMenuItem* pRegister = pDebugDialogs->Append(IDM_REGISTERWINDOW, _T("&Registers"), wxEmptyString, wxITEM_CHECK);
		pRegister->Check(true);

		wxMenuItem* pBreakPoints = pDebugDialogs->Append(IDM_BREAKPOINTWINDOW, _T("&BreakPoints"), wxEmptyString, wxITEM_CHECK);
		pBreakPoints->Check(true);

		pMenuBar->Append(pDebugDialogs, _T("&Dialogs"));
	}

	SetMenuBar(pMenuBar);
}


bool CCodeWindow::UseInterpreter()
{
	return(GetMenuBar()->IsChecked(IDM_INTERPRETER));
}


bool CCodeWindow::UseDualCore()
{
	return(GetMenuBar()->IsChecked(IDM_DUALCORE));
}


void CCodeWindow::JumpToAddress(u32 _Address)
{
    codeview->Center(_Address);
}


void CCodeWindow::OnCodeStep(wxCommandEvent& event)
{
	switch (event.GetId())
	{
	    case IDM_DEBUG_GO:
	    {
		    // [F|RES] prolly we should disable the other buttons in go mode too ...
		    codeview->Center(PC);

		    if (CCPU::IsStepping())
		    {
			    CCPU::EnableStepping(false);
		    }
		    else
		    {
			    CCPU::EnableStepping(true);
			    Host_UpdateLogDisplay();
		    }

		    Update();
	    }
		    break;

	    case IDM_STEP:
	    {
		    CCPU::StepOpcode(&sync_event);
//            if (CCPU::IsStepping())
//	            sync_event.Wait();
		    wxThread::Sleep(20);
		    // need a short wait here
		    codeview->Center(PC);
		    Update();
		    Host_UpdateLogDisplay();
	    }
		    break;

	    case IDM_STEPOVER:
		    CCPU::EnableStepping(true);
		    break;

	    case IDM_SKIP:
		    PC += 4;
		    Update();
		    break;

	    case IDM_SETPC:
		    PC = codeview->GetSelection();
		    Update();
		    break;

	    case IDM_GOTOPC:
		    codeview->Center(PC);
		    break;
	}

	UpdateButtonStates();
}


void CCodeWindow::OnAddrBoxChange(wxCommandEvent& event)
{
	wxString txt = addrbox->GetValue();

	if (txt.size() == 8)
	{
		u32 addr;
		sscanf(txt.mb_str(), "%08x", &addr);
		codeview->Center(addr);
	}

	event.Skip(1);
}


void CCodeWindow::Update()
{
	codeview->Refresh();

	callstack->Clear();

	std::vector<Debugger::SCallstackEntry>stack;

	if (Debugger::GetCallstack(stack))
	{
		for (size_t i = 0; i < stack.size(); i++)
		{
			int idx = callstack->Append(wxString::FromAscii(stack[i].Name.c_str()));
			callstack->SetClientData(idx, (void*)(u64)stack[i].vAddress);
		}
	}
	else
	{
		callstack->Append("invalid callstack");
	}

	UpdateButtonStates();

	codeview->Center(PC);

	Host_UpdateLogDisplay();
}


void CCodeWindow::NotifyMapLoaded()
{
	symbols->Show(false); // hide it for faster filling
	symbols->Clear();
#ifdef _WIN32
	const Debugger::XVectorSymbol& syms = Debugger::AccessSymbols();

	for (int i = 0; i < (int)syms.size(); i++)
	{
		int idx = symbols->Append(syms[i].GetName().c_str());
		symbols->SetClientData(idx, (void*)&syms[i]);
	}

	//
#endif

	symbols->Show(true);
	Update();
}


void CCodeWindow::UpdateButtonStates()
{
	if (Core::GetState() == Core::CORE_UNINITIALIZED)
	{
		buttonGo->Enable(false);
		buttonStep->Enable(false);
		buttonStepOver->Enable(false);
		buttonSkip->Enable(false);
	}
	else
	{
		if (!CCPU::IsStepping())
		{
			buttonGo->SetLabel(_T("&Pause"));
			buttonGo->Enable(true);
			buttonStep->Enable(false);
			buttonStepOver->Enable(false);
			buttonSkip->Enable(false);
		}
		else
		{
			buttonGo->SetLabel(_T("&Go"));
			buttonGo->Enable(true);
			buttonStep->Enable(true);
			buttonStepOver->Enable(true);
			buttonSkip->Enable(true);
		}
	}
}


void CCodeWindow::OnSymolListChange(wxCommandEvent& event)
{
	int index = symbols->GetSelection();
	Debugger::CSymbol* pSymbol = static_cast<Debugger::CSymbol*>(symbols->GetClientData(index));

	if (pSymbol != NULL)
	{
		codeview->Center(pSymbol->vaddress);
	}
}


void CCodeWindow::OnCallstackListChange(wxCommandEvent& event)
{
	int index   = callstack->GetSelection();
	u32 address = (u32)(u64)(callstack->GetClientData(index));

	if (address != 0x00)
	{
		codeview->Center(address);
	}
}


void CCodeWindow::OnToggleLogWindow(wxCommandEvent& event)
{
	if (IsLoggingActivated())
	{
		bool show = GetMenuBar()->IsChecked(event.GetId());

		if (show)
		{
			if (!m_logwindow)
			{
				m_logwindow = new CLogWindow(this);
			}

			m_logwindow->Show(true);
		}
		else // hide
		{
			// If m_dialog is NULL, then possibly the system
			// didn't report the checked menu item status correctly.
			// It should be true just after the menu item was selected,
			// if there was no modeless dialog yet.
			wxASSERT(m_logwindow != NULL);

			if (m_logwindow)
			{
				m_logwindow->Hide();
			}
		}
	}
}


void CCodeWindow::OnToggleRegisterWindow(wxCommandEvent& event)
{
	bool show = GetMenuBar()->IsChecked(event.GetId());

	if (show)
	{
		if (!m_RegisterWindow)
		{
			m_RegisterWindow = new CRegisterWindow(this);
		}

		m_RegisterWindow->Show(true);
	}
	else // hide
	{
		// If m_dialog is NULL, then possibly the system
		// didn't report the checked menu item status correctly.
		// It should be true just after the menu item was selected,
		// if there was no modeless dialog yet.
		wxASSERT(m_RegisterWindow != NULL);

		if (m_RegisterWindow)
		{
			m_RegisterWindow->Hide();
		}
	}
}

void CCodeWindow::OnToggleBreakPointWindow(wxCommandEvent& event)
{
	bool show = GetMenuBar()->IsChecked(event.GetId());

	if (show)
	{
		if (!m_BreakpointWindow)
		{
			m_BreakpointWindow = new CBreakPointWindow(this, this);
		}

		m_BreakpointWindow->Show(true);
	}
	else // hide
	{
		// If m_dialog is NULL, then possibly the system
		// didn't report the checked menu item status correctly.
		// It should be true just after the menu item was selected,
		// if there was no modeless dialog yet.
		wxASSERT(m_BreakpointWindow != NULL);

		if (m_BreakpointWindow)
		{
			m_BreakpointWindow->Hide();
		}
	}
}

void CCodeWindow::OnHostMessage(wxCommandEvent& event)
{
	switch (event.GetId())
	{
	    case IDM_NOTIFYMAPLOADED:
		    NotifyMapLoaded();
		    break;

	    case IDM_UPDATELOGDISPLAY:

		    if (m_logwindow)
		    {
			    m_logwindow->NotifyUpdate();
		    }

		    break;

	    case IDM_UPDATEDISASMDIALOG:
		    Update();

		    if (m_RegisterWindow)
		    {
			    m_RegisterWindow->NotifyUpdate();
		    }

		    break;

		case IDM_UPDATEBREAKPOINTS:
            Update();

			if (m_BreakpointWindow)
			{
				m_BreakpointWindow->NotifyUpdate();
			}
			break;

	}
}