Attention une mise à jour du serveur va être effectuée le lundi 17 mai entre 13h et 13h30. Cette mise à jour va générer une interruption du service de quelques minutes.

Commit 12aa8167 authored by Davide Pala's avatar Davide Pala

Refactored file opening and added file error checking

- Changed the interface of BasicSimulator to take file names
  as std::strings
- Added helper function to open file and check for fopen errors
- Added helper function to open file or assign default if file name
  is an empty string
parent f132a6ce
Pipeline #204069 passed with stages
in 5 minutes and 43 seconds
......@@ -2,7 +2,6 @@
#define __BASIC_SIMULATOR_H__
#include "simulator.h"
#include <map>
#include <vector>
class BasicSimulator : public Simulator {
......@@ -19,8 +18,9 @@ class BasicSimulator : public Simulator {
FILE* signatureFile;
public:
BasicSimulator(const char* binaryFile, std::vector<std::string>, const char* inFile, const char* outFile,
const char* tFile, const char* sFile);
BasicSimulator(std::string binaryFile, std::vector<std::string>,
std::string inFile, std::string outFile,
std::string tFile, std::string sFile);
~BasicSimulator();
protected:
......@@ -59,6 +59,7 @@ private:
void setByte(unsigned, ac_int<8, true>);
void readElf(const char*);
void pushArgsOnStack(const std::vector<std::string>);
void openFiles(std::string inFile, std::string outFile, std::string tFile, std::string sFile);
};
#endif // __BASIC_SIMULATOR_H__
......@@ -14,8 +14,9 @@
#define DEBUG 0
BasicSimulator::BasicSimulator(const char* binaryFile, std::vector<std::string> args, const char* inFile,
const char* outFile, const char* tFile, const char* sFile)
BasicSimulator::BasicSimulator(std::string binaryFile, std::vector<std::string> args,
std::string inFile, std::string outFile,
std::string tFile, std::string sFile)
{
memset((char*)&core, 0, sizeof(Core));
......@@ -28,40 +29,40 @@ BasicSimulator::BasicSimulator(const char* binaryFile, std::vector<std::string>
// core.im = new CacheMemory<4, 16, 64>(new SimpleMemory<4>(mem.data()), false);
// core.dm = new CacheMemory<4, 16, 64>(new SimpleMemory<4>(mem.data()), false);
heapAddress = 0;
inputFile = stdin;
outputFile = stdout;
traceFile = stderr;
signatureFile = NULL;
if (inFile)
inputFile = fopen(inFile, "rb");
if (outFile)
outputFile = fopen(outFile, "wb");
if (tFile)
traceFile = fopen(tFile, "wb");
if(sFile)
signatureFile = fopen(sFile, "wb");
//****************************************************************************
// Populate memory using ELF file
readElf(binaryFile);
openFiles(inFile, outFile, tFile, sFile);
if(DEBUG){
printf("Start Symbol Reading done.\n");
}
readElf(binaryFile.c_str());
pushArgsOnStack(args);
if(DEBUG){
printf("Populate Data Memory done.\n");
}
core.regFile[2] = STACK_INIT;
}
FILE* fopenCheck(const char* fname, const char* mode){
FILE* fp = fopen(fname, mode);
if(fp == NULL){
fprintf(stderr, "Error: cannot open file %s\n", fname);
exit(-1);
}
return fp;
}
void openOrDefault(std::string fname, const char* mode, FILE* def, FILE* &dest){
if (fname.empty())
dest = def;
else
dest = fopenCheck(fname.c_str(), mode);
}
void BasicSimulator::openFiles(std::string inFile, std::string outFile, std::string tFile, std::string sFile){
openOrDefault(inFile, "rb", stdin, inputFile);
openOrDefault(outFile, "wb", stdout, outputFile);
openOrDefault(tFile, "wb", stderr, traceFile);
openOrDefault(sFile, "wb", NULL, signatureFile);
}
void BasicSimulator::readElf(const char *binaryFile){
heapAddress = 0;
ElfFile elfFile(binaryFile);
for(auto const &section : elfFile.sectionTable){
if(section.address != 0){
......@@ -81,6 +82,10 @@ void BasicSimulator::readElf(const char *binaryFile){
begin_signature = find_by_name(elfFile.symbols, "begin_signature").offset;
end_signature = find_by_name(elfFile.symbols, "end_signature").offset;
}
if(DEBUG){
printf("Elf Reading done.\n");
}
}
void BasicSimulator::pushArgsOnStack(const std::vector<std::string> args){
......@@ -100,6 +105,9 @@ void BasicSimulator::pushArgsOnStack(const std::vector<std::string> args){
setByte(currentPlaceStrings + oneCharIndex, 0);
currentPlaceStrings += oneCharIndex + 1;
}
if(DEBUG){
printf("Populate Data Memory done.\n");
}
}
BasicSimulator::~BasicSimulator()
......
......@@ -30,7 +30,7 @@ int main(int argc, char** argv)
benchArgs.push_back(binaryFile);
for (auto a : pargs)
benchArgs.push_back(a);
BasicSimulator sim(binaryFile.c_str(), benchArgs, inputFile.c_str(), outputFile.c_str(), traceFile.c_str(), signatureFile.c_str());
BasicSimulator sim(binaryFile, benchArgs, inputFile, outputFile, traceFile, signatureFile);
sim.run();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment