blob: 0687c4c08f3b2f41d977a7f9a4f2ee58bea34fce [file] [log] [blame]
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
John Criswell09344dc2003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00009//
10// This file contains code used to execute the program utilizing one of the
11// various ways of running LLVM bytecode.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000015#include "BugDriver.h"
Misha Brukman8a32c6d2004-04-19 03:12:35 +000016#include "llvm/Support/ToolRunner.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
20#include "llvm/Support/SystemUtils.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000021#include <fstream>
Brian Gaeke960707c2003-11-11 22:41:34 +000022using namespace llvm;
23
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000024namespace {
25 // OutputType - Allow the user to specify the way code should be run, to test
26 // for miscompilation.
27 //
28 enum OutputType {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000029 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000030 };
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000031
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000032 cl::opt<OutputType>
33 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000034 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukman8a32c6d2004-04-19 03:12:35 +000035 clEnumValN(RunLLI, "run-int",
36 "Execute with the interpreter"),
Misha Brukmand792c9b2003-07-24 18:17:43 +000037 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
38 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
39 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattner97161002004-07-16 00:08:28 +000040 clEnumValEnd),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000041 cl::init(AutoPick));
Chris Lattner68efaa72003-04-23 20:31:37 +000042
Brian Gaeke35145be2004-02-11 18:37:32 +000043 cl::opt<bool>
44 CheckProgramExitCode("check-exit-code",
Misha Brukman6aa3c832004-07-23 01:30:49 +000045 cl::desc("Assume nonzero exit code is failure (default on)"),
Brian Gaeke35145be2004-02-11 18:37:32 +000046 cl::init(true));
47
Chris Lattner68efaa72003-04-23 20:31:37 +000048 cl::opt<std::string>
49 InputFile("input", cl::init("/dev/null"),
50 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattnerdc92fa62003-10-14 22:24:31 +000051
52 cl::list<std::string>
53 AdditionalSOs("additional-so",
54 cl::desc("Additional shared objects to load "
55 "into executing programs"));
Chris Lattner4e0969b2004-07-24 07:53:26 +000056
57 cl::opt<unsigned>
58 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
59 cl::desc("Number of seconds program is allowed to run before it "
60 "is killed (default is 300s), 0 disables timeout"));
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000061}
62
Brian Gaeke960707c2003-11-11 22:41:34 +000063namespace llvm {
Chris Lattner2f1aa112004-01-14 03:38:37 +000064 // Anything specified after the --args option are taken as arguments to the
65 // program being debugged.
66 cl::list<std::string>
67 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner6aee736d2004-05-06 22:05:35 +000068 cl::ZeroOrMore, cl::PositionalEatsArgs);
Brian Gaeke4a278f02004-05-04 21:09:16 +000069
70 cl::list<std::string>
71 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner6aee736d2004-05-06 22:05:35 +000072 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattner2f1aa112004-01-14 03:38:37 +000073}
Misha Brukman40feb362003-07-30 20:15:44 +000074
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000075//===----------------------------------------------------------------------===//
76// BugDriver method implementation
77//
78
79/// initializeExecutionEnvironment - This method is used to set up the
80/// environment for executing LLVM programs.
81///
82bool BugDriver::initializeExecutionEnvironment() {
83 std::cout << "Initializing execution environment: ";
84
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000085 // Create an instance of the AbstractInterpreter interface as specified on
86 // the command line
Chris Lattner898de4a2004-02-18 20:52:02 +000087 cbe = 0;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000088 std::string Message;
Brian Gaeke4a278f02004-05-04 21:09:16 +000089
Chris Lattner7709ec52003-05-03 03:19:41 +000090 switch (InterpreterSel) {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000091 case AutoPick:
92 InterpreterSel = RunCBE;
Brian Gaeke4a278f02004-05-04 21:09:16 +000093 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
94 &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000095 if (!Interpreter) {
96 InterpreterSel = RunJIT;
Brian Gaeke4a278f02004-05-04 21:09:16 +000097 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
98 &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000099 }
100 if (!Interpreter) {
101 InterpreterSel = RunLLC;
Brian Gaeke4a278f02004-05-04 21:09:16 +0000102 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
103 &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000104 }
105 if (!Interpreter) {
106 InterpreterSel = RunLLI;
Brian Gaeke4a278f02004-05-04 21:09:16 +0000107 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
108 &ToolArgv);
Brian Gaeke9a0bdb12003-10-21 17:41:35 +0000109 }
110 if (!Interpreter) {
111 InterpreterSel = AutoPick;
112 Message = "Sorry, I can't automatically select an interpreter!\n";
113 }
114 break;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000115 case RunLLI:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000116 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
117 &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000118 break;
119 case RunLLC:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000120 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
121 &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000122 break;
123 case RunJIT:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000124 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
125 &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000126 break;
127 case RunCBE:
Brian Gaeke4a278f02004-05-04 21:09:16 +0000128 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
129 &ToolArgv);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000130 break;
Chris Lattner7709ec52003-05-03 03:19:41 +0000131 default:
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000132 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattner7709ec52003-05-03 03:19:41 +0000133 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000134 }
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000135 std::cerr << Message;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000136
Misha Brukman0fd31722003-07-24 21:59:10 +0000137 // Initialize auxiliary tools for debugging
Chris Lattner898de4a2004-02-18 20:52:02 +0000138 if (!cbe) {
Brian Gaeke4a278f02004-05-04 21:09:16 +0000139 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Chris Lattner898de4a2004-02-18 20:52:02 +0000140 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
141 }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000142 gcc = GCC::create(getToolName(), Message);
Misha Brukman0fd31722003-07-24 21:59:10 +0000143 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
144
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000145 // If there was an error creating the selected interpreter, quit with error.
146 return Interpreter == 0;
147}
148
Chris Lattner96d41dd2004-02-18 23:25:22 +0000149/// compileProgram - Try to compile the specified module, throwing an exception
150/// if an error occurs, or returning normally if not. This is used for code
151/// generation crash testing.
152///
153void BugDriver::compileProgram(Module *M) {
154 // Emit the program to a bytecode file...
155 std::string BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
156 if (writeProgramToFile(BytecodeFile, M)) {
157 std::cerr << ToolName << ": Error emitting bytecode to file '"
158 << BytecodeFile << "'!\n";
159 exit(1);
160 }
161
162 // Remove the temporary bytecode file when we are done.
163 FileRemover BytecodeFileRemover(BytecodeFile);
164
165 // Actually compile the program!
166 Interpreter->compileProgram(BytecodeFile);
167}
168
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000169
170/// executeProgram - This method runs "Program", capturing the output of the
171/// program to a file, returning the filename of the file. A recommended
172/// filename may be optionally specified.
173///
174std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000175 std::string BytecodeFile,
Chris Lattner3f6e5222003-10-14 21:59:36 +0000176 const std::string &SharedObj,
Brian Gaeke35145be2004-02-11 18:37:32 +0000177 AbstractInterpreter *AI,
178 bool *ProgramExitedNonzero) {
Chris Lattner3f6e5222003-10-14 21:59:36 +0000179 if (AI == 0) AI = Interpreter;
180 assert(AI && "Interpreter should have been created already!");
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000181 bool CreatedBytecode = false;
182 if (BytecodeFile.empty()) {
183 // Emit the program to a bytecode file...
184 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
185
186 if (writeProgramToFile(BytecodeFile, Program)) {
187 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukmand792c9b2003-07-24 18:17:43 +0000188 << BytecodeFile << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000189 exit(1);
190 }
191 CreatedBytecode = true;
192 }
193
Chris Lattner1f80a922004-02-18 22:01:21 +0000194 // Remove the temporary bytecode file when we are done.
195 FileRemover BytecodeFileRemover(BytecodeFile, CreatedBytecode);
196
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000197 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000198
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000199 // Check to see if this is a valid output filename...
200 OutputFile = getUniqueFilename(OutputFile);
201
Chris Lattner3f6e5222003-10-14 21:59:36 +0000202 // Figure out which shared objects to run, if any.
Chris Lattnerdc92fa62003-10-14 22:24:31 +0000203 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000204 if (!SharedObj.empty())
205 SharedObjs.push_back(SharedObj);
206
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000207 // Actually execute the program!
Chris Lattner3f6e5222003-10-14 21:59:36 +0000208 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
Chris Lattner4e0969b2004-07-24 07:53:26 +0000209 OutputFile, SharedObjs, TimeoutValue);
210
211 if (RetVal == -1) {
212 std::cerr << "<timeout>";
213 static bool FirstTimeout = true;
214 if (FirstTimeout) {
215 std::cout << "\n"
216 "*** Program execution timed out! This mechanism is designed to handle\n"
217 " programs stuck in infinite loops gracefully. The -timeout option\n"
218 " can be used to change the timeout threshold or disable it completely\n"
219 " (with -timeout=0). This message is only displayed once.\n";
220 FirstTimeout = false;
221 }
222 }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000223
Brian Gaeke35145be2004-02-11 18:37:32 +0000224 if (ProgramExitedNonzero != 0)
225 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000226
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000227 // Return the filename we captured the output to.
228 return OutputFile;
229}
230
Brian Gaeke35145be2004-02-11 18:37:32 +0000231/// executeProgramWithCBE - Used to create reference output with the C
232/// backend, if reference output is not provided.
233///
234std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
235 bool ProgramExitedNonzero;
236 std::string outFN = executeProgram(OutputFile, "", "",
237 (AbstractInterpreter*)cbe,
238 &ProgramExitedNonzero);
239 if (ProgramExitedNonzero) {
240 std::cerr
241 << "Warning: While generating reference output, program exited with\n"
242 << "non-zero exit code. This will NOT be treated as a failure.\n";
243 CheckProgramExitCode = false;
244 }
245 return outFN;
246}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000247
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000248std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000249 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000250 std::string OutputCFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000251
252 // Using CBE
Misha Brukmand792c9b2003-07-24 18:17:43 +0000253 cbe->OutputC(BytecodeFile, OutputCFile);
254
255#if 0 /* This is an alternative, as yet unimplemented */
256 // Using LLC
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000257 std::string Message;
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000258 LLC *llc = createLLCtool(Message);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000259 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
260 std::cerr << "Could not generate asm code with `llc', exiting.\n";
261 exit(1);
262 }
263#endif
264
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000265 std::string SharedObjectFile;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000266 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000267 exit(1);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000268
269 // Remove the intermediate C file
270 removeFile(OutputCFile);
271
Chris Lattner2b97d6e2003-10-19 21:54:13 +0000272 return "./" + SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000273}
274
275
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000276/// diffProgram - This method executes the specified module and diffs the output
277/// against the file specified by ReferenceOutputFile. If the output is
278/// different, true is returned.
279///
Misha Brukmand792c9b2003-07-24 18:17:43 +0000280bool BugDriver::diffProgram(const std::string &BytecodeFile,
281 const std::string &SharedObject,
Chris Lattner16a41312003-04-24 17:02:17 +0000282 bool RemoveBytecode) {
Brian Gaeke35145be2004-02-11 18:37:32 +0000283 bool ProgramExitedNonzero;
284
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000285 // Execute the program, generating an output file...
Brian Gaeke35145be2004-02-11 18:37:32 +0000286 std::string Output = executeProgram("", BytecodeFile, SharedObject, 0,
287 &ProgramExitedNonzero);
288
289 // If we're checking the program exit code, assume anything nonzero is bad.
Chris Lattner73c141a2004-04-02 05:33:06 +0000290 if (CheckProgramExitCode && ProgramExitedNonzero) {
291 removeFile(Output);
292 if (RemoveBytecode) removeFile(BytecodeFile);
Brian Gaeke35145be2004-02-11 18:37:32 +0000293 return true;
Chris Lattner73c141a2004-04-02 05:33:06 +0000294 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000295
Chris Lattneraa997fb2003-08-01 20:29:45 +0000296 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000297 bool FilesDifferent = false;
Chris Lattneraa997fb2003-08-01 20:29:45 +0000298 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
299 if (!Error.empty()) {
Misha Brukman6aa3c832004-07-23 01:30:49 +0000300 std::cerr << "While diffing output: " << Error << '\n';
Chris Lattneraa997fb2003-08-01 20:29:45 +0000301 exit(1);
302 }
303 FilesDifferent = true;
304 }
Chris Lattner759b9932003-10-18 21:02:51 +0000305
306 // Remove the generated output.
307 removeFile(Output);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000308
Chris Lattner759b9932003-10-18 21:02:51 +0000309 // Remove the bytecode file if we are supposed to.
Chris Lattner16a41312003-04-24 17:02:17 +0000310 if (RemoveBytecode) removeFile(BytecodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000311 return FilesDifferent;
312}
Misha Brukman539f9592003-07-28 19:16:14 +0000313
314bool BugDriver::isExecutingJIT() {
315 return InterpreterSel == RunJIT;
316}
Brian Gaeke960707c2003-11-11 22:41:34 +0000317