blob: 972338acad85a0aaf144ba572b5e8becefb6660e [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-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 Lattner4a106452002-12-23 23:50:16 +000015#include "BugDriver.h"
Misha Brukmanb687d822004-04-19 03:12:35 +000016#include "llvm/Support/ToolRunner.h"
Reid Spencer551ccae2004-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 Lattner4a106452002-12-23 23:50:16 +000021#include <fstream>
Brian Gaeked0fde302003-11-11 22:41:34 +000022using namespace llvm;
23
Chris Lattner4a106452002-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 Gaekeb5ee5092003-10-21 17:41:35 +000029 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
Chris Lattner4a106452002-12-23 23:50:16 +000030 };
Misha Brukman41485562003-09-29 22:40:52 +000031
Chris Lattnera328c512005-01-23 03:45:26 +000032 cl::opt<double>
33 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
34 cl::init(0.0));
35 cl::opt<double>
36 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
37 cl::init(0.0));
38
Chris Lattner4a106452002-12-23 23:50:16 +000039 cl::opt<OutputType>
40 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000041 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukmanb687d822004-04-19 03:12:35 +000042 clEnumValN(RunLLI, "run-int",
43 "Execute with the interpreter"),
Misha Brukman50733362003-07-24 18:17:43 +000044 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
45 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
46 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000047 clEnumValEnd),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000048 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000049
Brian Gaekec5cad212004-02-11 18:37:32 +000050 cl::opt<bool>
51 CheckProgramExitCode("check-exit-code",
Misha Brukmaneed80e22004-07-23 01:30:49 +000052 cl::desc("Assume nonzero exit code is failure (default on)"),
Brian Gaekec5cad212004-02-11 18:37:32 +000053 cl::init(true));
54
Chris Lattner3c053a02003-04-23 20:31:37 +000055 cl::opt<std::string>
56 InputFile("input", cl::init("/dev/null"),
57 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000058
59 cl::list<std::string>
60 AdditionalSOs("additional-so",
61 cl::desc("Additional shared objects to load "
62 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000063
64 cl::opt<unsigned>
65 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
66 cl::desc("Number of seconds program is allowed to run before it "
67 "is killed (default is 300s), 0 disables timeout"));
Chris Lattner4a106452002-12-23 23:50:16 +000068}
69
Brian Gaeked0fde302003-11-11 22:41:34 +000070namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +000071 // Anything specified after the --args option are taken as arguments to the
72 // program being debugged.
73 cl::list<std::string>
74 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000075 cl::ZeroOrMore, cl::PositionalEatsArgs);
Brian Gaeke636df3d2004-05-04 21:09:16 +000076
77 cl::list<std::string>
78 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000079 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +000080}
Misha Brukman9d679cb2003-07-30 20:15:44 +000081
Chris Lattner4a106452002-12-23 23:50:16 +000082//===----------------------------------------------------------------------===//
83// BugDriver method implementation
84//
85
86/// initializeExecutionEnvironment - This method is used to set up the
87/// environment for executing LLVM programs.
88///
89bool BugDriver::initializeExecutionEnvironment() {
90 std::cout << "Initializing execution environment: ";
91
Misha Brukman41485562003-09-29 22:40:52 +000092 // Create an instance of the AbstractInterpreter interface as specified on
93 // the command line
Chris Lattner7bb11542004-02-18 20:52:02 +000094 cbe = 0;
Chris Lattner4a106452002-12-23 23:50:16 +000095 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +000096
Chris Lattnercc876a72003-05-03 03:19:41 +000097 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +000098 case AutoPick:
99 InterpreterSel = RunCBE;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000100 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
101 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000102 if (!Interpreter) {
103 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000104 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
105 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000106 }
107 if (!Interpreter) {
108 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000109 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
110 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000111 }
112 if (!Interpreter) {
113 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000114 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
115 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000116 }
117 if (!Interpreter) {
118 InterpreterSel = AutoPick;
119 Message = "Sorry, I can't automatically select an interpreter!\n";
120 }
121 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000122 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000123 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
124 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000125 break;
126 case RunLLC:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000127 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
128 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000129 break;
130 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000131 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
132 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000133 break;
134 case RunCBE:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000135 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
136 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000137 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000138 default:
Misha Brukman41485562003-09-29 22:40:52 +0000139 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000140 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000141 }
Misha Brukman41485562003-09-29 22:40:52 +0000142 std::cerr << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000143
Misha Brukmana259c9b2003-07-24 21:59:10 +0000144 // Initialize auxiliary tools for debugging
Chris Lattner7bb11542004-02-18 20:52:02 +0000145 if (!cbe) {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000146 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Chris Lattner7bb11542004-02-18 20:52:02 +0000147 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
148 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000149 gcc = GCC::create(getToolName(), Message);
Misha Brukmana259c9b2003-07-24 21:59:10 +0000150 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
151
Chris Lattner4a106452002-12-23 23:50:16 +0000152 // If there was an error creating the selected interpreter, quit with error.
153 return Interpreter == 0;
154}
155
Chris Lattnerea9212c2004-02-18 23:25:22 +0000156/// compileProgram - Try to compile the specified module, throwing an exception
157/// if an error occurs, or returning normally if not. This is used for code
158/// generation crash testing.
159///
160void BugDriver::compileProgram(Module *M) {
161 // Emit the program to a bytecode file...
Reid Spencer97182982004-12-15 01:53:08 +0000162 sys::Path BytecodeFile ("bugpoint-test-program.bc");
163 BytecodeFile.makeUnique();
164 if (writeProgramToFile(BytecodeFile.toString(), M)) {
Chris Lattnerea9212c2004-02-18 23:25:22 +0000165 std::cerr << ToolName << ": Error emitting bytecode to file '"
166 << BytecodeFile << "'!\n";
167 exit(1);
168 }
169
170 // Remove the temporary bytecode file when we are done.
Reid Spencer5f767602004-12-16 23:04:20 +0000171 FileRemover BytecodeFileRemover(BytecodeFile);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000172
173 // Actually compile the program!
Reid Spencer97182982004-12-15 01:53:08 +0000174 Interpreter->compileProgram(BytecodeFile.toString());
Chris Lattnerea9212c2004-02-18 23:25:22 +0000175}
176
Chris Lattner4a106452002-12-23 23:50:16 +0000177
178/// executeProgram - This method runs "Program", capturing the output of the
179/// program to a file, returning the filename of the file. A recommended
180/// filename may be optionally specified.
181///
182std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukman50733362003-07-24 18:17:43 +0000183 std::string BytecodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000184 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000185 AbstractInterpreter *AI,
186 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000187 if (AI == 0) AI = Interpreter;
188 assert(AI && "Interpreter should have been created already!");
Chris Lattner4a106452002-12-23 23:50:16 +0000189 bool CreatedBytecode = false;
190 if (BytecodeFile.empty()) {
191 // Emit the program to a bytecode file...
Reid Spencer97182982004-12-15 01:53:08 +0000192 sys::Path uniqueFilename("bugpoint-test-program.bc");
193 uniqueFilename.makeUnique();
194 BytecodeFile = uniqueFilename.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000195
196 if (writeProgramToFile(BytecodeFile, Program)) {
197 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukman50733362003-07-24 18:17:43 +0000198 << BytecodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000199 exit(1);
200 }
201 CreatedBytecode = true;
202 }
203
Chris Lattner97092722004-02-18 22:01:21 +0000204 // Remove the temporary bytecode file when we are done.
Brian Gaeke06c375b2004-12-22 22:33:33 +0000205 sys::Path BytecodePath (BytecodeFile);
206 FileRemover BytecodeFileRemover(BytecodePath, CreatedBytecode);
Chris Lattner97092722004-02-18 22:01:21 +0000207
Chris Lattner4a106452002-12-23 23:50:16 +0000208 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000209
Chris Lattner4a106452002-12-23 23:50:16 +0000210 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000211 sys::Path uniqueFile(OutputFile);
212 uniqueFile.makeUnique();
213 OutputFile = uniqueFile.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000214
Chris Lattner769f1fe2003-10-14 21:59:36 +0000215 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000216 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000217 if (!SharedObj.empty())
218 SharedObjs.push_back(SharedObj);
219
Chris Lattner4a106452002-12-23 23:50:16 +0000220 // Actually execute the program!
Chris Lattner769f1fe2003-10-14 21:59:36 +0000221 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
Chris Lattner7d91e492004-07-24 07:53:26 +0000222 OutputFile, SharedObjs, TimeoutValue);
223
224 if (RetVal == -1) {
225 std::cerr << "<timeout>";
226 static bool FirstTimeout = true;
227 if (FirstTimeout) {
228 std::cout << "\n"
229 "*** Program execution timed out! This mechanism is designed to handle\n"
230 " programs stuck in infinite loops gracefully. The -timeout option\n"
231 " can be used to change the timeout threshold or disable it completely\n"
232 " (with -timeout=0). This message is only displayed once.\n";
233 FirstTimeout = false;
234 }
235 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000236
Brian Gaekec5cad212004-02-11 18:37:32 +0000237 if (ProgramExitedNonzero != 0)
238 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000239
Chris Lattner4a106452002-12-23 23:50:16 +0000240 // Return the filename we captured the output to.
241 return OutputFile;
242}
243
Brian Gaekec5cad212004-02-11 18:37:32 +0000244/// executeProgramWithCBE - Used to create reference output with the C
245/// backend, if reference output is not provided.
246///
247std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
248 bool ProgramExitedNonzero;
249 std::string outFN = executeProgram(OutputFile, "", "",
250 (AbstractInterpreter*)cbe,
251 &ProgramExitedNonzero);
252 if (ProgramExitedNonzero) {
253 std::cerr
254 << "Warning: While generating reference output, program exited with\n"
255 << "non-zero exit code. This will NOT be treated as a failure.\n";
256 CheckProgramExitCode = false;
257 }
258 return outFN;
259}
Misha Brukman50733362003-07-24 18:17:43 +0000260
Chris Lattnera0f5b152003-10-14 21:09:11 +0000261std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000262 assert(Interpreter && "Interpreter should have been created already!");
Reid Spencer5f767602004-12-16 23:04:20 +0000263 sys::Path OutputCFile;
Misha Brukman50733362003-07-24 18:17:43 +0000264
265 // Using CBE
Misha Brukman50733362003-07-24 18:17:43 +0000266 cbe->OutputC(BytecodeFile, OutputCFile);
267
268#if 0 /* This is an alternative, as yet unimplemented */
269 // Using LLC
Chris Lattnera0f5b152003-10-14 21:09:11 +0000270 std::string Message;
Misha Brukman41485562003-09-29 22:40:52 +0000271 LLC *llc = createLLCtool(Message);
Misha Brukman50733362003-07-24 18:17:43 +0000272 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
273 std::cerr << "Could not generate asm code with `llc', exiting.\n";
274 exit(1);
275 }
276#endif
277
Chris Lattnera0f5b152003-10-14 21:09:11 +0000278 std::string SharedObjectFile;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000279 if (gcc->MakeSharedObject(OutputCFile.toString(), GCC::CFile,
Reid Spencer5f767602004-12-16 23:04:20 +0000280 SharedObjectFile))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000281 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000282
283 // Remove the intermediate C file
Reid Spencer5f767602004-12-16 23:04:20 +0000284 OutputCFile.destroyFile();
Misha Brukman50733362003-07-24 18:17:43 +0000285
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000286 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000287}
288
289
Chris Lattner4a106452002-12-23 23:50:16 +0000290/// diffProgram - This method executes the specified module and diffs the output
291/// against the file specified by ReferenceOutputFile. If the output is
292/// different, true is returned.
293///
Misha Brukman50733362003-07-24 18:17:43 +0000294bool BugDriver::diffProgram(const std::string &BytecodeFile,
295 const std::string &SharedObject,
Chris Lattner640f22e2003-04-24 17:02:17 +0000296 bool RemoveBytecode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000297 bool ProgramExitedNonzero;
298
Chris Lattner4a106452002-12-23 23:50:16 +0000299 // Execute the program, generating an output file...
Reid Spencer5f767602004-12-16 23:04:20 +0000300 sys::Path Output (executeProgram("", BytecodeFile, SharedObject, 0,
301 &ProgramExitedNonzero));
Brian Gaekec5cad212004-02-11 18:37:32 +0000302
303 // If we're checking the program exit code, assume anything nonzero is bad.
Chris Lattner58d84ce2004-04-02 05:33:06 +0000304 if (CheckProgramExitCode && ProgramExitedNonzero) {
Reid Spencer5f767602004-12-16 23:04:20 +0000305 Output.destroyFile();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000306 if (RemoveBytecode)
Reid Spencer5f767602004-12-16 23:04:20 +0000307 sys::Path(BytecodeFile).destroyFile();
Brian Gaekec5cad212004-02-11 18:37:32 +0000308 return true;
Chris Lattner58d84ce2004-04-02 05:33:06 +0000309 }
Chris Lattner4a106452002-12-23 23:50:16 +0000310
Chris Lattner65f62792003-08-01 20:29:45 +0000311 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000312 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000313 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
314 sys::Path(Output.toString()),
315 AbsTolerance, RelTolerance, &Error)) {
316 if (Diff == 2) {
Misha Brukmaneed80e22004-07-23 01:30:49 +0000317 std::cerr << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000318 exit(1);
319 }
320 FilesDifferent = true;
321 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000322
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000323 // Remove the generated output.
Reid Spencer5f767602004-12-16 23:04:20 +0000324 Output.destroyFile();
Chris Lattner4a106452002-12-23 23:50:16 +0000325
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000326 // Remove the bytecode file if we are supposed to.
Reid Spencer5f767602004-12-16 23:04:20 +0000327 if (RemoveBytecode) sys::Path(BytecodeFile).destroyFile();
Chris Lattner4a106452002-12-23 23:50:16 +0000328 return FilesDifferent;
329}
Misha Brukman91eabc12003-07-28 19:16:14 +0000330
331bool BugDriver::isExecutingJIT() {
332 return InterpreterSel == RunJIT;
333}
Brian Gaeked0fde302003-11-11 22:41:34 +0000334