OpenASIP  2.0
MemDumpCommand.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 MemDumpCommand.cc
26  *
27  * Implementation of MemDumpCommand class
28  *
29  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
30  * @author Alex Hirvonen 2016
31  * @note rating: red
32  */
33 
34 #include "MemDumpCommand.hh"
35 #include "SimulatorFrontend.hh"
36 #include "MemorySystem.hh"
37 #include "SimulatorToolbox.hh"
38 #include "Memory.hh"
39 #include "StringTools.hh"
40 #include "Address.hh"
41 #include "NullAddressSpace.hh"
42 #include "SimValue.hh"
43 #include "Conversion.hh"
44 #include <iostream>
45 #include <fstream>
46 
47 /**
48  * Constructor.
49  *
50  * Sets the name of the command to the base class.
51  */
54 }
55 
56 /**
57  * Destructor.
58  *
59  * Does nothing.
60  */
62 }
63 
64 /**
65  * Executes the "x" command.
66  *
67  * This low-level command prints the data in memory starting at specified
68  * addresses addr.
69  *
70  * @param arguments The address to print.
71  * @return True in case simulation is initialized and arguments are ok.
72  * @exception NumberFormatException Is never thrown by this command.
73  */
74 bool
75 MemDumpCommand::execute(const std::vector<DataObject>& arguments) {
76  static size_t displayedCount = 1;
77  static size_t lastDisplayedAddress = 0;
78  static size_t MAUsToDisplay = 1;
79 
80  const int argumentCount = arguments.size() - 1;
81  if (!checkArgumentCount(argumentCount, 0, 9)) {
82  return false;
83  }
84 
85  if (!checkProgramLoaded()) {
86  return false;
87  }
88 
89  bool illegalArguments = false;
90  size_t newDisplayedCount = displayedCount;
91  size_t newMAUCount = MAUsToDisplay;
92  size_t newDisplayedAddress = lastDisplayedAddress + MAUsToDisplay;
93  std::string addressSpaceName = "";
94  std::string fileName = "";
95  for (size_t i = 1; i < arguments.size(); ++i) {
96  if (StringTools::ciEqual(arguments.at(i).stringValue(), "/n")) {
97  if (i == arguments.size() - 1 ||
98  !checkUnsignedIntegerArgument(arguments.at(i + 1))) {
99  illegalArguments = true;
100  break;
101  }
102  newDisplayedCount =
103  static_cast<size_t>(arguments.at(i + 1).integerValue());
104  ++i;
105  } else if (StringTools::ciEqual(arguments.at(i).stringValue(), "/u")) {
106  if (i == arguments.size() - 1) {
107  illegalArguments = true;
108  break;
109  }
110  const std::string size = arguments.at(i + 1).stringValue();
111  if (StringTools::ciEqual(size, "b")) {
112  newMAUCount = 1;
113  } else if (StringTools::ciEqual(size, "h")) {
114  newMAUCount = 2;
115  } else if (StringTools::ciEqual(size, "w")) {
116  newMAUCount = 4;
117  } else {
118  illegalArguments = true;
119  break;
120  }
121  ++i;
122  } else if (StringTools::ciEqual(arguments.at(i).stringValue(), "/a")) {
123  if (i == arguments.size() - 1) {
124  illegalArguments = true;
125  break;
126  }
127  addressSpaceName = arguments.at(i + 1).stringValue();
128  ++i;
129  } else if (StringTools::ciEqual(arguments.at(i).stringValue(), "/f")) {
130  if (i == arguments.size() - 1) {
131  illegalArguments = true;
132  break;
133  }
134  fileName = arguments.at(i + 1).stringValue();
135  ++i;
136  } else if (i == arguments.size() - 1) {
137  const std::string addressString = arguments.at(i).stringValue();
138  if (!setMemoryAddress(
139  addressString, addressSpaceName, newDisplayedAddress)) {
140  return false;
141  }
142  } else {
143  illegalArguments = true;
144  break;
145  }
146  }
147 
148  if (illegalArguments) {
152  interpreter()->setError(true);
153  return false;
154  }
155 
156  displayedCount = newDisplayedCount;
157  lastDisplayedAddress = newDisplayedAddress;
158 
160  if (!setMemoryPointer(memory, addressSpaceName)) {
161  return false;
162  }
163 
164  size_t MAUSize;
165  if (simulatorFrontend().memorySystem().memoryCount() == 1) {
167  } else {
168  MAUSize =
170  addressSpace(addressSpaceName).width();
171  }
172 
173  if (MAUSize*newMAUCount > SIMULATOR_MAX_INTWORD_BITWIDTH) {
175  (boost::format("Maximum printable integer size %d.") %
177  interpreter()->setError(true);
178  return false;
179  }
180 
181  std::ofstream* out = NULL;
182  const bool dumpToFile = fileName != "";
183  if (dumpToFile) {
184  if (MAUSize != sizeof(char)*8) {
186  (boost::format(
187  "Can only dump 8 bit memories to files. The given "
188  "address space is %d.") % MAUSize).str());
189  interpreter()->setError(true);
190  return false;
191  }
192  newMAUCount = 1;
193  out = new std::ofstream(fileName.c_str(), std::ios::binary);
194  }
195 
196  MAUsToDisplay = newMAUCount;
197  DataObject* result = new DataObject("");
198  // read the wanted number (given with /n) of chunks of data to the result
199  while (newDisplayedCount > 0) {
200 
201  ULongWord data = 0;
202  try {
203  memory->read(newDisplayedAddress, MAUsToDisplay, data);
204  } catch (const OutOfRange&) {
208  interpreter()->setError(true);
209  return false;
210  }
211 
212 
213  newDisplayedCount--;
214  newDisplayedAddress += MAUsToDisplay;
215 
216  if (!dumpToFile) {
217  const int HEX_DIGITS = MAUSize*newMAUCount/4;
218  result->setString(
219  result->stringValue() +
220  Conversion::toHexString(data, HEX_DIGITS));
221 
222  if (newDisplayedCount > 0) {
223  result->setString(result->stringValue() + " ");
224  }
225  } else {
226  *out << (char)data;
227  }
228  }
229 
230  if (dumpToFile) {
231  out->close();
232  delete out;
233  out = NULL;
234  }
235  interpreter()->setResult(result);
236  return true;
237 }
238 
239 /**
240  * Returns the help text for this command.
241  *
242  * Help text is searched from SimulatorTextGenerator.
243  *
244  * @return The help text.
245  */
246 std::string
250 }
CustomCommand::checkArgumentCount
bool checkArgumentCount(int argumentCount, int minimum, int maximum)
Definition: CustomCommand.cc:82
MemDumpCommand.hh
DataObject
Definition: DataObject.hh:50
DataObject::stringValue
virtual std::string stringValue() const
Definition: DataObject.cc:344
SimControlLanguageCommand::setMemoryPointer
bool setMemoryPointer(MemorySystem::MemoryPtr &memory, const std::string &addressSpaceName)
Definition: SimControlLanguageCommand.cc:780
Memory.hh
SIMULATOR_MAX_INTWORD_BITWIDTH
#define SIMULATOR_MAX_INTWORD_BITWIDTH
Definition: SimValue.hh:248
OutOfRange
Definition: Exception.hh:320
Texts::TXT_ADDRESS_OUT_OF_RANGE
@ TXT_ADDRESS_OUT_OF_RANGE
Definition: SimulatorTextGenerator.hh:192
SimControlLanguageCommand::checkProgramLoaded
bool checkProgramLoaded()
Definition: SimControlLanguageCommand.cc:180
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
MemorySystem.hh
SimControlLanguageCommand::setMemoryAddress
bool setMemoryAddress(const std::string &addressString, std::string &addressSpaceName, std::size_t &memoryAddress)
Definition: SimControlLanguageCommand.cc:753
MemorySystem::MemoryPtr
boost::shared_ptr< Memory > MemoryPtr
Definition: MemorySystem.hh:57
Texts::TXT_INTERP_HELP_X
@ TXT_INTERP_HELP_X
Help text for command "x" of the CLI.
Definition: SimulatorTextGenerator.hh:105
StringTools.hh
MemDumpCommand::~MemDumpCommand
virtual ~MemDumpCommand()
Definition: MemDumpCommand.cc:61
SimulatorToolbox.hh
Conversion.hh
Texts::TXT_ILLEGAL_ARGUMENTS
@ TXT_ILLEGAL_ARGUMENTS
Definition: TextGenerator.hh:67
NullAddressSpace.hh
CustomCommand::checkUnsignedIntegerArgument
bool checkUnsignedIntegerArgument(const DataObject &argument)
Definition: CustomCommand.cc:164
SimulatorToolbox::textGenerator
static SimulatorTextGenerator & textGenerator()
Definition: SimulatorToolbox.cc:75
MemDumpCommand::helpText
virtual std::string helpText() const
Definition: MemDumpCommand.cc:247
StringTools::ciEqual
static bool ciEqual(const std::string &a, const std::string &b)
Definition: StringTools.cc:240
SimulatorFrontend.hh
Conversion::toHexString
static std::string toHexString(T source, std::size_t digits=0, bool include0x=true)
CustomCommand::interpreter
ScriptInterpreter * interpreter() const
ScriptInterpreter::setResult
virtual void setResult(DataObject *result)
Definition: ScriptInterpreter.cc:128
TTAMachine::AddressSpace::width
virtual int width() const
Definition: AddressSpace.cc:155
MemDumpCommand::execute
virtual bool execute(const std::vector< DataObject > &arguments)
Definition: MemDumpCommand.cc:75
SimulatorFrontend::memorySystem
MemorySystem & memorySystem(int coreId=-1)
Definition: SimulatorFrontend.cc:2121
Address.hh
SimValue.hh
ULongWord
unsigned long ULongWord
Definition: BaseType.hh:51
ScriptInterpreter::setError
virtual void setError(bool state)
Definition: ScriptInterpreter.cc:205
SimControlLanguageCommand
Definition: SimControlLanguageCommand.hh:63
DataObject::setString
virtual void setString(std::string value)
Definition: DataObject.cc:130
SimControlLanguageCommand::simulatorFrontend
SimulatorFrontend & simulatorFrontend()
Definition: SimControlLanguageCommand.cc:214
MemorySystem::addressSpace
const TTAMachine::AddressSpace & addressSpace(unsigned int i)
Definition: MemorySystem.cc:261
MemDumpCommand::MemDumpCommand
MemDumpCommand()
Definition: MemDumpCommand.cc:52