OpenASIP  2.0
ProximDebuggerWindow.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2016 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ProximDebuggerWindow.cc
26  *
27  * Definition of ProximDebuggerWindow class.
28  *
29  * @author Alex Hirvonen 2016 (alex.hirvonen-no.spam-gmail.com)
30  * @note rating: red
31  */
32 
33 
34 #include <wx/sizer.h>
35 #include <wx/textctrl.h>
36 
37 #include "ProximDebuggerWindow.hh"
38 #include "ProximToolbox.hh"
39 #include "Proxim.hh"
42 #include "Machine.hh"
43 #include "Program.hh"
44 #include "Instruction.hh"
45 #include "Move.hh"
46 #include "WxConversion.hh"
47 #include "ErrorDialog.hh"
48 
49 using namespace TTAProgram;
50 
54  EVT_CHOICE(ID_SOURCEFILE_CHOICE, ProximDebuggerWindow::onSourceFileChoice)
56 
57 
59  ProximMainFrame* parent, int id):
60  ProximSimulatorWindow(parent, id, wxDefaultPosition, wxSize(800,600)),
61  currentFile_() {
62 
63  createContents(this, true, true);
64  loadProgram(ProximToolbox::program());
65 }
66 
67 
69 }
70 
71 
72 /**
73  * Resets the window when a new program or machine is being loaded in the
74  * simulator.
75  */
76 void
78  sourceFileList_->Clear();
79  sourceCodeText_->Clear();
80  currentFile_.clear();
81  currentLineNums_.clear();
82 }
83 
84 
85 /**
86  * Loads a program model to the window.
87  *
88  * @param program Program to load.
89  */
90 void
92  // get list of used source code files for drop-down list
93  for (int i = 0; i < program.moveCount(); i++) {
94 
95  const TTAProgram::Move& m = program.moveAt(i);
96 
97  if (m.hasSourceFileName()) {
98  wxString sourceFile = WxConversion::toWxString(m.sourceFileName());
99 
100  if (sourceFileList_->FindString(sourceFile) == -1) {
101  sourceFileList_->Append(sourceFile);
102  }
103  }
104  }
105 
106  // load first file from drop-down list and set hilights
107  if (sourceFileList_->GetCount() > 0) {
108  loadSourceCode(sourceFileList_->GetString(0));
109  updateAnnotations();
110  } else {
111  ErrorDialog dialog(
112  this, _T("No debugging information available."));
113  dialog.ShowModal();
114  }
115 }
116 
117 
118 /**
119  * Parses possible annotations in the current instruction and hilights
120  * source code lines.
121  */
122 void
124 
125  Word pc = wxGetApp().simulation()->frontend()->programCounter();
126 
127  const TTAProgram::Instruction& instruction =
129 
130  // check annotations and hilight possible lines
131  for (int i = 0; i < instruction.moveCount(); i++) {
132  const TTAProgram::Move& m = instruction.move(i);
133 
134  if (m.hasSourceLineNumber()) {
135  wxString sourceFile = WxConversion::toWxString(m.sourceFileName());
136  if (sourceFile == currentFile_) {
137  highlightLine(m.sourceLineNumber());
138  }
139  }
140  }
141  // make last higlighted line visible
142  if (currentLineNums_.size() > 0) {
143  showLine(currentLineNums_.back());
144  }
145 }
146 
147 
148 
149 /**
150  * Loads source code into the sourceCodeText_ widget
151  *
152  * @param sourceFile Absolute file location as wxString.
153  */
154 void
156 
157  sourceCodeText_->LoadFile(sourceFile);
158  currentFile_ = sourceFile;
159  sourceFileList_->SetSelection(sourceFileList_->FindString(sourceFile));
160 }
161 
162 
163 /**
164  * Sets certain style attributes to the line in sourceCodeText_ widget
165  *
166  * @param lineNum Line number
167  * @param style Style attributes (text color, background color, font, alignment)
168  */
169 void
170 ProximDebuggerWindow::setLineAttributes(int lineNum, wxTextAttr style) {
171 
172  long lineStart = sourceCodeText_->XYToPosition(0, lineNum - 1);
173  long lineEnd = lineStart + sourceCodeText_->GetLineLength(lineNum - 1);
174 
175  sourceCodeText_->SetStyle(lineStart, lineEnd, style);
176 }
177 
178 
179 /**
180  * Highlights line of code in the sourceCodeText_ widget
181  *
182  * @param lineNum Source code line number.
183  */
184 void
186 
187  if (std::find(currentLineNums_.begin(), currentLineNums_.end(), lineNum) ==
188  currentLineNums_.end()) {
189 
190  currentLineNums_.push_back(lineNum);
191  setLineAttributes(lineNum, wxTextAttr(*wxWHITE, *wxBLUE));
192  }
193 }
194 
195 
196 /**
197  * Makes line visible in the sourceCodeText_ widget
198  *
199  * @param lineNum Source code line number.
200  */
201 void
203  sourceCodeText_->ShowPosition(sourceCodeText_->XYToPosition(0,lineNum - 1));
204 }
205 
206 
207 /**
208  * Event handler which is called when a new program is loaded in the simulator.
209  */
210 void
212  loadProgram(ProximToolbox::program());
213 }
214 
215 
216 /**
217  * Event handler which is called when simulation step ends.
218  */
219 void
221  // unhilight old lines
222  for (int lineNum : currentLineNums_) {
223  setLineAttributes(lineNum, wxTextAttr(*wxBLACK, *wxWHITE));
224  }
225  currentLineNums_.clear();
226 
227  updateAnnotations();
228 }
229 
230 
231 /**
232  * Event handler for drop-down list selection.
233  *
234  * Loads selected source code from the drop-down list.
235  */
236 void
238  loadSourceCode(sourceFileList_->GetString(sourceFileList_->GetSelection()));
239  currentLineNums_.clear();
240  updateAnnotations();
241 }
242 
243 
244 /**
245  * Creates the window contents.
246  *
247  * Code generated by wxDesigner.
248  */
249 wxSizer*
251  wxWindow *parent, bool call_fit, bool set_sizer) {
252 
253  wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
254 
255  wxBoxSizer *upperSizer = new wxBoxSizer(wxHORIZONTAL);
256  sourceFileList_ = new wxChoice(
257  parent, ID_SOURCEFILE_CHOICE, wxDefaultPosition, wxDefaultSize);
258 
259  upperSizer->Add(sourceFileList_, 1, wxGROW|wxALL, 5);
260  mainSizer->Add(upperSizer, 0, wxGROW|wxALL, 5);
261 
262  sourceCodeText_ = new wxTextCtrl(
263  parent, ID_SOURCECODE, wxEmptyString, wxDefaultPosition,
264  wxSize(640, 600), wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP);
265 
266  mainSizer->Add(sourceCodeText_, 1, wxGROW|wxALL, 5);
267 
268  if (set_sizer)
269  {
270  parent->SetSizer( mainSizer );
271  if (call_fit)
272  mainSizer->SetSizeHints( parent );
273  }
274 
275  return mainSizer;
276 }
ProximMainFrame
Definition: ProximMainFrame.hh:58
TTAProgram::Move::sourceFileName
std::string sourceFileName() const
Definition: Move.cc:488
TTAProgram
Definition: Estimator.hh:65
TTAProgram::Program
Definition: Program.hh:63
WxConversion::toWxString
static wxString toWxString(const std::string &source)
TTAProgram::Instruction::move
Move & move(int i) const
Definition: Instruction.cc:193
TTAProgram::Instruction
Definition: Instruction.hh:57
TTAProgram::Move::sourceLineNumber
int sourceLineNumber() const
Definition: Move.cc:459
ProximToolbox::program
static const TTAProgram::Program & program()
Definition: ProximToolbox.cc:88
ProximDebuggerWindow::updateAnnotations
void updateAnnotations()
Definition: ProximDebuggerWindow.cc:123
ProximSimulationThread.hh
Proxim.hh
ProximDebuggerWindow::showLine
void showLine(int lineNum)
Definition: ProximDebuggerWindow.cc:202
ProximDebuggerWindow::highlightLine
void highlightLine(int lineNum)
Definition: ProximDebuggerWindow.cc:185
ProximSimulatorWindow
Definition: ProximSimulatorWindow.hh:47
TTAProgram::Program::instructionAt
Instruction & instructionAt(InstructionAddress address) const
Definition: Program.cc:374
ProximToolbox.hh
ProximDebuggerWindow::~ProximDebuggerWindow
virtual ~ProximDebuggerWindow()
Definition: ProximDebuggerWindow.cc:68
ErrorDialog
Definition: ErrorDialog.hh:42
Instruction.hh
ErrorDialog.hh
ProximDebuggerWindow::onSimulationStop
void onSimulationStop(const SimulatorEvent &event)
Definition: ProximDebuggerWindow.cc:220
ProximDebuggerWindow::setLineAttributes
void setLineAttributes(int lineNum, wxTextAttr style)
Definition: ProximDebuggerWindow.cc:170
TTAProgram::Move
Definition: Move.hh:55
Machine.hh
ProximDebuggerWindow::createContents
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
Definition: ProximDebuggerWindow.cc:250
ProximDebuggerWindow::loadProgram
void loadProgram(const TTAProgram::Program &program)
Definition: ProximDebuggerWindow.cc:91
TracedSimulatorFrontend.hh
TTAProgram::Move::hasSourceLineNumber
bool hasSourceLineNumber() const
Definition: Move.cc:445
Program.hh
ProximDebuggerWindow::onSourceFileChoice
void onSourceFileChoice(wxCommandEvent &event)
Definition: ProximDebuggerWindow.cc:237
ProximDebuggerWindow.hh
WxConversion.hh
program
find Finds info of the inner loops in the program
Definition: InnerLoopFinder.cc:80
ProximDebuggerWindow::loadSourceCode
void loadSourceCode(wxString sourceFile)
Definition: ProximDebuggerWindow.cc:155
ProximDebuggerWindow::reset
virtual void reset()
Definition: ProximDebuggerWindow.cc:77
EVT_SIMULATOR_PROGRAM_LOADED
#define EVT_SIMULATOR_PROGRAM_LOADED(id, fn)
Definition: SimulatorEvent.hh:151
SimulatorEvent
Definition: SimulatorEvent.hh:42
Move.hh
ProximDebuggerWindow::onProgramLoaded
void onProgramLoaded(const SimulatorEvent &event)
Definition: ProximDebuggerWindow.cc:211
EVT_SIMULATOR_STOP
#define EVT_SIMULATOR_STOP(id, fn)
Definition: SimulatorEvent.hh:106
TTAProgram::Instruction::moveCount
int moveCount() const
Definition: Instruction.cc:176
END_EVENT_TABLE
END_EVENT_TABLE() using namespace IDF
ProximDebuggerWindow
Definition: ProximDebuggerWindow.hh:54
TTAProgram::Move::hasSourceFileName
bool hasSourceFileName() const
Definition: Move.cc:478