45 #include "params/Process.hh"
49 std::string
const& errout)
50 : _fdArray(), _input(input), _output(
output), _errout(errout),
52 {
"cin", STDIN_FILENO},
53 {
"stdin", STDIN_FILENO}},
55 {
"cout", STDOUT_FILENO},
56 {
"stdout", STDOUT_FILENO},
57 {
"cerr", STDERR_FILENO},
58 {
"stderr", STDERR_FILENO}}
61 std::map<std::string, int>::iterator it;
67 if ((it = _imap.find(input)) != _imap.end())
70 sim_fd = openInputFile(input);
72 auto ffd = std::make_shared<FileFDEntry>(sim_fd, O_RDONLY, input,
false);
73 _fdArray[STDIN_FILENO] = ffd;
79 if ((it = _oemap.find(
output)) != _oemap.end())
82 sim_fd = openOutputFile(
output);
84 ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
86 _fdArray[STDOUT_FILENO] = ffd;
90 else if ((it = _oemap.find(errout)) != _oemap.end())
93 sim_fd = openOutputFile(errout);
95 ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
97 _fdArray[STDERR_FILENO] = ffd;
109 auto ffd = std::dynamic_pointer_cast<FileFDEntry>(fdp);
118 int sim_fd = ffd->getSimFD();
119 ffd->setFileOffset(lseek(sim_fd, 0, SEEK_CUR));
133 auto seek = [] (std::shared_ptr<FileFDEntry> ffd)
135 if (lseek(ffd->getSimFD(), ffd->getFileOffset(), SEEK_SET) < 0)
136 fatal(
"Unable to seek to location in %s", ffd->getFileName());
139 std::map<std::string, int>::iterator it;
148 std::shared_ptr<FDEntry> stdin_fde =
_fdArray[STDIN_FILENO];
149 auto stdin_ffd = std::dynamic_pointer_cast<FileFDEntry>(stdin_fde);
151 if (
_input != stdin_ffd->getFileName()) {
152 warn(
"Using new input file (%s) rather than checkpointed (%s)\n",
153 _input, stdin_ffd->getFileName());
154 stdin_ffd->setFileName(
_input);
155 stdin_ffd->setFileOffset(0);
158 if ((it =
_imap.find(stdin_ffd->getFileName())) !=
_imap.end()) {
159 stdin_ffd->setSimFD(it->second);
161 stdin_ffd->setSimFD(
openInputFile(stdin_ffd->getFileName()));
172 std::shared_ptr<FDEntry> stdout_fde =
_fdArray[STDOUT_FILENO];
173 auto stdout_ffd = std::dynamic_pointer_cast<FileFDEntry>(stdout_fde);
175 if (
_output != stdout_ffd->getFileName()) {
176 warn(
"Using new output file (%s) rather than checkpointed (%s)\n",
177 _output, stdout_ffd->getFileName());
178 stdout_ffd->setFileName(
_output);
179 stdout_ffd->setFileOffset(0);
182 if ((it =
_oemap.find(stdout_ffd->getFileName())) !=
_oemap.end()) {
183 stdout_ffd->setSimFD(it->second);
196 std::shared_ptr<FDEntry> stderr_fde =
_fdArray[STDERR_FILENO];
197 auto stderr_ffd = std::dynamic_pointer_cast<FileFDEntry>(stderr_fde);
199 if (
_errout != stderr_ffd->getFileName()) {
200 warn(
"Using new error file (%s) rather than checkpointed (%s)\n",
201 _errout, stderr_ffd->getFileName());
202 stderr_ffd->setFileName(
_errout);
203 stderr_ffd->setFileOffset(0);
206 if (stdout_ffd->getFileName() == stderr_ffd->getFileName()) {
208 stderr_ffd->setSimFD(stdout_ffd->getSimFD());
209 }
else if ((it =
_oemap.find(stderr_ffd->getFileName())) !=
_oemap.end()) {
210 stderr_ffd->setSimFD(it->second);
216 for (
int tgt_fd = 3; tgt_fd <
_fdArray.size(); tgt_fd++) {
217 std::shared_ptr<FDEntry> fdp =
_fdArray[tgt_fd];
222 if (
auto pfd = std::dynamic_pointer_cast<PipeFDEntry>(fdp)) {
228 if (pfd->getEndType() == PipeFDEntry::EndType::write)
233 if (pipe(fd_pair) < 0)
234 fatal(
"Unable to create new pipe");
240 pfd->setSimFD(fd_pair[0]);
246 int prs = pfd->getPipeReadSource();
247 std::shared_ptr<FDEntry> write_fdp =
_fdArray[prs];
250 auto write_pfd = std::dynamic_pointer_cast<PipeFDEntry>(write_fdp);
253 write_pfd->setSimFD(fd_pair[1]);
257 if (
auto dfd = std::dynamic_pointer_cast<DeviceFDEntry>(fdp)) {
263 fatal(
"Unable to restore checkpoints with emulated drivers");
267 if (
auto ffd = std::dynamic_pointer_cast<FileFDEntry>(fdp)) {
275 int sim_fd =
openFile(ffd->getFileName(), ffd->getFlags(), 0664);
276 ffd->setSimFD(sim_fd);
286 std::shared_ptr<FDEntry> fdp =
_fdArray[
i];
292 fatal(
"Out of target file descriptors");
298 int sim_fd = open(filename.c_str(), flags,
mode);
301 fatal(
"Unable to open %s with mode %O", filename,
mode);
307 return openFile(filename, O_RDONLY, 00);
314 O_WRONLY | O_CREAT | O_TRUNC, 0664);
317 std::shared_ptr<FDEntry>
320 assert(0 <= tgt_fd && tgt_fd <
_fdArray.size());
327 assert(0 <= tgt_fd && tgt_fd <
_fdArray.size());
334 if (tgt_fd >=
_fdArray.size() || tgt_fd < 0)
338 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>(
_fdArray[tgt_fd]);
340 sim_fd = hbfdp->getSimFD();