OpenASIP  2.0
TTASim.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2012 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 TTASim.cc
26  *
27  * Implementation of ttasim.
28  *
29  * The command line version of the TTA Simulator / Debugger
30  *
31  * @author Pekka Jääskeläinen 2005-2012 (pjaaskel-no.spam-cs.tut.fi)
32  * @note rating: red
33  */
34 
35 #include <string>
36 #include <iostream>
37 #include <boost/shared_ptr.hpp>
38 
40 #include "SimulatorFrontend.hh"
41 #include "FileSystem.hh"
42 #include "SimulatorInterpreter.hh"
43 #include "SimulatorCLI.hh"
44 #include "SimulatorToolbox.hh"
45 
46 /**
47  * A handler class for Ctrl-c signal.
48  *
49  * Stops the simulation (if it's running).
50  */
52 public:
53  /**
54  * Constructor.
55  *
56  * @param target The target SimulatorFrontend instance.
57  */
59  }
60 
61  /**
62  * Stops the simulation.
63  */
64  virtual void execute(int /*data*/, siginfo_t* /*info*/) {
66  }
67 private:
68  /// Simulator frontend to use when stopping the simulation.
70 };
71 
72 /**
73  * A handler class for SIGFPE signal
74  *
75  * Stops the simulation (if it's running). Used for catching
76  * errors from the simulated program in the compiled simulation
77  * engine.
78  */
80 public:
81  /**
82  * Constructor.
83  *
84  * @param target The target SimulatorFrontend instance.
85  */
87  }
88 
89  /**
90  * Terminates the simulation.
91  *
92  * @exception SimulationExecutionError thrown always
93  */
94  virtual void execute(int, siginfo_t *info) {
95  std::string msg("Unknown floating point exception");
96 
97  if (info->si_code == FPE_INTDIV) {
98  msg = "integer division by zero";
99  } else if (info->si_code == FPE_FLTDIV) {
100  msg = "floating-point division by zero";
101  } else if (info->si_code == FPE_INTOVF) {
102  msg = "integer overflow";
103  } else if (info->si_code == FPE_FLTOVF) {
104  msg = "floating-point overflow";
105  } else if (info->si_code == FPE_FLTUND) {
106  msg = "floating-point underflow";
107  } else if (info->si_code == FPE_FLTRES) {
108  msg = "floating-point inexact result";
109  } else if (info->si_code == FPE_FLTINV) {
110  msg = "invalid floating-point operation";
111  } else if (info->si_code == FPE_FLTSUB) {
112  msg = " Subscript out of range";
113  }
114 
118 
119  throw SimulationExecutionError(__FILE__, __LINE__, __FUNCTION__, msg);
120  }
121 private:
122  /// Simulator frontend to use when stopping the simulation.
124 };
125 
126 /**
127  * A handler class for SIGSEGV signal
128  *
129  * Stops the simulation (if it's running). Used for catching
130  * errors from the simulated program in the compiled simulation
131  * engine.
132  */
134 public:
135  /**
136  * Constructor.
137  *
138  * @param target The target SimulatorFrontend instance.
139  */
141  }
142 
143  /**
144  * Terminates the simulation.
145  *
146  * @exception SimulationExecutionError thrown always
147  */
148  virtual void execute(int, siginfo_t*) {
149  std::string msg("Invalid memory reference");
150 
154 
155  throw SimulationExecutionError(__FILE__, __LINE__, __FUNCTION__, msg);
156  }
157 private:
158  /// Simulator frontend to use when stopping the simulation.
160 };
161 
162 /**
163  * Main function.
164  *
165  * Parses the command line and figures out whether to start the interactive
166  * debugging mode or not.
167  *
168  * @param argc The command line argument count.
169  * @param argv The command line arguments (passed to the interpreter).
170  * @return The return status.
171  */
172 int main(int argc, char* argv[]) {
173 
174  Application::initialize(argc, argv);
175 
176  boost::shared_ptr<SimulatorFrontend> simFront;
177  /// @todo: read command line options and initialize the
178  /// simulator (frontend) using the given values.
180  try {
181  options->parse(argv, argc);
183  } catch (ParserStopRequest) {
184  return EXIT_SUCCESS;
185  } catch (const IllegalCommandLine& i) {
186  std::cerr << i.errorMessage() << std::endl;
187  return EXIT_FAILURE;
188  }
189 
190  simFront.reset(new SimulatorFrontend(options->backendType()));
191 
192  SimulatorCLI* cli = new SimulatorCLI(*simFront);
193 
194  if (options->debugMode()) {
195  // handler for catching ctrl-c from the user (stops simulation)
196  SigINTHandler* ctrlcHandler = new SigINTHandler(*simFront);
197  Application::setSignalHandler(SIGINT, *ctrlcHandler);
198 
199  if (simFront->isCompiledSimulation()) {
200 
201  /* Catch errors caused by the simulated program
202  in compiled simulation these show up as normal
203  signals as the simulation code is native code we are
204  running in the simulation process. */
205  SigFPEHandler fpeHandler(*simFront);
206  SigSegvHandler segvHandler(*simFront);
207  Application::setSignalHandler(SIGFPE, fpeHandler);
208  Application::setSignalHandler(SIGSEGV, segvHandler);
209  }
210  }
211 
212  // check if there is an initialization file in user's home dir and
213  // execute it
214  const std::string personalInitScript =
215  FileSystem::homeDirectory() + DIR_SEPARATOR + SIM_INIT_FILE_NAME;
216  if (FileSystem::fileExists(personalInitScript)) {
217  cli->interpreter().processScriptFile(personalInitScript);
218  }
219 
220  std::string machineToLoad = options->machineFile();
221  std::string programToLoad = options->programFile();
222  const std::string scriptString = options->scriptString();
223 
224  if (options->numberOfArguments() != 0) {
225  std::cerr << SimulatorToolbox::textGenerator().text(
226  Texts::TXT_ILLEGAL_ARGUMENTS).str() << std::endl;
227  return EXIT_FAILURE;
228  }
229 
230  // check if there is an initialization file in the current dir and
231  // execute it
232  const std::string currentDirInitScript =
234  if (FileSystem::fileExists(currentDirInitScript)) {
235  cli->interpreter().processScriptFile(currentDirInitScript);
236  }
237 
238  if (machineToLoad != "") {
239  cli->interpreteAndPrintResults(std::string("mach " ) + machineToLoad);
240  }
241 
242  if (programToLoad != "") {
243  cli->interpreteAndPrintResults(std::string("prog " ) + programToLoad);
244  if (scriptString == "") {
245  // by default, if program is given, start simulation immediately
246  cli->interpreteAndPrintResults("run");
247  }
248  }
249 
250  if (scriptString != "") {
251  cli->interpreteAndPrintResults(scriptString);
252  }
253 
254 
255  if (options->debugMode()) {
256  cli->run();
258  if (simFront->isCompiledSimulation()) {
261  }
262  }
263  delete cli;
264  return EXIT_SUCCESS;
265 }
FileSystem.hh
SigINTHandler::SigINTHandler
SigINTHandler(SimulatorFrontend &target)
Definition: TTASim.cc:58
SigINTHandler
Definition: TTASim.cc:51
SigSegvHandler
Definition: TTASim.cc:133
ParserStopRequest
Definition: Exception.hh:491
SRE_USER_REQUESTED
@ SRE_USER_REQUESTED
User requested the simulation to stop explicitly, e.g., by pressing ctrl-c in the CLI.
Definition: SimulatorConstants.hh:66
CmdLineParser::numberOfArguments
virtual int numberOfArguments() const
Application::restoreSignalHandler
static void restoreSignalHandler(int signalNum)
Definition: Application.cc:471
SimulatorCmdLineOptions
Definition: SimulatorCmdLineOptions.hh:45
SigSegvHandler::execute
virtual void execute(int, siginfo_t *)
Definition: TTASim.cc:148
SIM_INIT_FILE_NAME
#define SIM_INIT_FILE_NAME
The initialization script file name.
Definition: SimulatorConstants.hh:48
SigINTHandler::execute
virtual void execute(int, siginfo_t *)
Definition: TTASim.cc:64
Application::setCmdLineOptions
static void setCmdLineOptions(CmdLineOptions *options_)
Definition: Application.cc:381
IllegalCommandLine
Definition: Exception.hh:438
SimulationExecutionError
Definition: Exception.hh:951
Application::setSignalHandler
static void setSignalHandler(int signalNum, UnixSignalHandler &handler)
Definition: Application.cc:417
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
SigSegvHandler::SigSegvHandler
SigSegvHandler(SimulatorFrontend &target)
Definition: TTASim.cc:140
FileSystem::homeDirectory
static std::string homeDirectory()
SimulatorToolbox.hh
SigFPEHandler::execute
virtual void execute(int, siginfo_t *info)
Definition: TTASim.cc:94
Application::UnixSignalHandler
Definition: Application.hh:203
SimulatorCmdLineOptions.hh
SimulatorCLI::run
virtual void run()
Definition: SimulatorCLI.cc:189
SigSegvHandler::target_
SimulatorFrontend & target_
Simulator frontend to use when stopping the simulation.
Definition: TTASim.cc:159
SigFPEHandler
Definition: TTASim.cc:79
Texts::TXT_ILLEGAL_ARGUMENTS
@ TXT_ILLEGAL_ARGUMENTS
Definition: TextGenerator.hh:67
SimulatorToolbox::textGenerator
static SimulatorTextGenerator & textGenerator()
Definition: SimulatorToolbox.cc:75
SRE_RUNTIME_ERROR
@ SRE_RUNTIME_ERROR
A fatal runtime error occured in the simulated program.
Definition: SimulatorConstants.hh:68
SimulatorFrontend::RES_FATAL
@ RES_FATAL
Fatal runtime error, there is a serious error in the simulated program, thus it makes no sense to go ...
Definition: SimulatorFrontend.hh:95
SigINTHandler::target_
SimulatorFrontend & target_
Simulator frontend to use when stopping the simulation.
Definition: TTASim.cc:69
SimulatorFrontend.hh
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
CmdLineOptions::parse
void parse(char *argv[], int argc)
Definition: CmdLineOptions.cc:107
SimulatorCLI::interpreter
SimulatorInterpreter & interpreter()
Definition: SimulatorCLI.hh:58
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
SimulatorCLI.hh
FileSystem::fileExists
static bool fileExists(const std::string fileName)
Application::initialize
static void initialize()
Definition: Application.cc:99
SimulatorInterpreter.hh
SigFPEHandler::SigFPEHandler
SigFPEHandler(SimulatorFrontend &target)
Definition: TTASim.cc:86
SigFPEHandler::target_
SimulatorFrontend & target_
Simulator frontend to use when stopping the simulation.
Definition: TTASim.cc:123
FileSystem::currentWorkingDir
static std::string currentWorkingDir()
Definition: FileSystem.cc:142
SimulatorFrontend::reportSimulatedProgramError
void reportSimulatedProgramError(RuntimeErrorSeverity severity, const std::string &description)
Definition: SimulatorFrontend.cc:2304
SimulatorCLI
Definition: SimulatorCLI.hh:48
main
int main(int argc, char *argv[])
Definition: TTASim.cc:172
SimulatorFrontend
Definition: SimulatorFrontend.hh:89
SimulatorCLI::interpreteAndPrintResults
virtual void interpreteAndPrintResults(const TCEString &scriptString)
Definition: SimulatorCLI.cc:176
TclInterpreter::processScriptFile
virtual bool processScriptFile(const std::string &scriptFileName)
Definition: TclInterpreter.cc:250
SimulatorFrontend::prepareToStop
void prepareToStop(StopReason reason)
Definition: SimulatorFrontend.cc:1331