blob: 8044f504a43f5ded933c97690e6e59fca82ff697 [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
John Criswell7c0e0222003-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 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 Lattner4a106452002-12-23 23:50:16 +000032 cl::opt<OutputType>
33 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000034 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukmanb687d822004-04-19 03:12:35 +000035 clEnumValN(RunLLI, "run-int",
36 "Execute with the interpreter"),
Misha Brukman50733362003-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 Lattner4d143ee2004-07-16 00:08:28 +000040 clEnumValEnd),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000041 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000042
Brian Gaekec5cad212004-02-11 18:37:32 +000043 cl::opt<bool>
44 CheckProgramExitCode("check-exit-code",
Misha Brukmaneed80e22004-07-23 01:30:49 +000045 cl::desc("Assume nonzero exit code is failure (default on)"),
Brian Gaekec5cad212004-02-11 18:37:32 +000046 cl::init(true));
47
Chris Lattner3c053a02003-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 Lattner7dac6582003-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 Lattner7d91e492004-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 Lattner4a106452002-12-23 23:50:16 +000061}
62
Brian Gaeked0fde302003-11-11 22:41:34 +000063namespace llvm {
Chris Lattnerfa761832004-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 Lattner60083e22004-05-06 22:05:35 +000068 cl::ZeroOrMore, cl::PositionalEatsArgs);
Brian Gaeke636df3d2004-05-04 21:09:16 +000069
70 cl::list<std::string>
71 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000072 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +000073}
Misha Brukman9d679cb2003-07-30 20:15:44 +000074
Chris Lattner4a106452002-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 Brukman41485562003-09-29 22:40:52 +000085 // Create an instance of the AbstractInterpreter interface as specified on
86 // the command line
Chris Lattner7bb11542004-02-18 20:52:02 +000087 cbe = 0;
Chris Lattner4a106452002-12-23 23:50:16 +000088 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +000089
Chris Lattnercc876a72003-05-03 03:19:41 +000090 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +000091 case AutoPick:
92 InterpreterSel = RunCBE;
Brian Gaeke636df3d2004-05-04 21:09:16 +000093 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
94 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +000095 if (!Interpreter) {
96 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +000097 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
98 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +000099 }
100 if (!Interpreter) {
101 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000102 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
103 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000104 }
105 if (!Interpreter) {
106 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000107 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
108 &ToolArgv);
Brian Gaekeb5ee5092003-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 Lattner769f1fe2003-10-14 21:59:36 +0000115 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000116 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
117 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000118 break;
119 case RunLLC:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000120 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
121 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000122 break;
123 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000124 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
125 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000126 break;
127 case RunCBE:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000128 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
129 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000130 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000131 default:
Misha Brukman41485562003-09-29 22:40:52 +0000132 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000133 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000134 }
Misha Brukman41485562003-09-29 22:40:52 +0000135 std::cerr << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000136
Misha Brukmana259c9b2003-07-24 21:59:10 +0000137 // Initialize auxiliary tools for debugging
Chris Lattner7bb11542004-02-18 20:52:02 +0000138 if (!cbe) {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000139 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Chris Lattner7bb11542004-02-18 20:52:02 +0000140 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
141 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000142 gcc = GCC::create(getToolName(), Message);
Misha Brukmana259c9b2003-07-24 21:59:10 +0000143 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
144
Chris Lattner4a106452002-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 Lattnerea9212c2004-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...
Reid Spencer97182982004-12-15 01:53:08 +0000155 sys::Path BytecodeFile ("bugpoint-test-program.bc");
156 BytecodeFile.makeUnique();
157 if (writeProgramToFile(BytecodeFile.toString(), M)) {
Chris Lattnerea9212c2004-02-18 23:25:22 +0000158 std::cerr << ToolName << ": Error emitting bytecode to file '"
159 << BytecodeFile << "'!\n";
160 exit(1);
161 }
162
163 // Remove the temporary bytecode file when we are done.
Reid Spencer5f767602004-12-16 23:04:20 +0000164 FileRemover BytecodeFileRemover(BytecodeFile);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000165
166 // Actually compile the program!
Reid Spencer97182982004-12-15 01:53:08 +0000167 Interpreter->compileProgram(BytecodeFile.toString());
Chris Lattnerea9212c2004-02-18 23:25:22 +0000168}
169
Chris Lattner4a106452002-12-23 23:50:16 +0000170
171/// executeProgram - This method runs "Program", capturing the output of the
172/// program to a file, returning the filename of the file. A recommended
173/// filename may be optionally specified.
174///
175std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukman50733362003-07-24 18:17:43 +0000176 std::string BytecodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000177 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000178 AbstractInterpreter *AI,
179 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000180 if (AI == 0) AI = Interpreter;
181 assert(AI && "Interpreter should have been created already!");
Chris Lattner4a106452002-12-23 23:50:16 +0000182 bool CreatedBytecode = false;
183 if (BytecodeFile.empty()) {
184 // Emit the program to a bytecode file...
Reid Spencer97182982004-12-15 01:53:08 +0000185 sys::Path uniqueFilename("bugpoint-test-program.bc");
186 uniqueFilename.makeUnique();
187 BytecodeFile = uniqueFilename.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000188
189 if (writeProgramToFile(BytecodeFile, Program)) {
190 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukman50733362003-07-24 18:17:43 +0000191 << BytecodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000192 exit(1);
193 }
194 CreatedBytecode = true;
195 }
196
Chris Lattner97092722004-02-18 22:01:21 +0000197 // Remove the temporary bytecode file when we are done.
Reid Spencer5f767602004-12-16 23:04:20 +0000198 FileRemover BytecodeFileRemover(sys::Path(BytecodeFile), CreatedBytecode);
Chris Lattner97092722004-02-18 22:01:21 +0000199
Chris Lattner4a106452002-12-23 23:50:16 +0000200 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000201
Chris Lattner4a106452002-12-23 23:50:16 +0000202 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000203 sys::Path uniqueFile(OutputFile);
204 uniqueFile.makeUnique();
205 OutputFile = uniqueFile.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000206
Chris Lattner769f1fe2003-10-14 21:59:36 +0000207 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000208 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000209 if (!SharedObj.empty())
210 SharedObjs.push_back(SharedObj);
211
Chris Lattner4a106452002-12-23 23:50:16 +0000212 // Actually execute the program!
Chris Lattner769f1fe2003-10-14 21:59:36 +0000213 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
Chris Lattner7d91e492004-07-24 07:53:26 +0000214 OutputFile, SharedObjs, TimeoutValue);
215
216 if (RetVal == -1) {
217 std::cerr << "<timeout>";
218 static bool FirstTimeout = true;
219 if (FirstTimeout) {
220 std::cout << "\n"
221 "*** Program execution timed out! This mechanism is designed to handle\n"
222 " programs stuck in infinite loops gracefully. The -timeout option\n"
223 " can be used to change the timeout threshold or disable it completely\n"
224 " (with -timeout=0). This message is only displayed once.\n";
225 FirstTimeout = false;
226 }
227 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000228
Brian Gaekec5cad212004-02-11 18:37:32 +0000229 if (ProgramExitedNonzero != 0)
230 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000231
Chris Lattner4a106452002-12-23 23:50:16 +0000232 // Return the filename we captured the output to.
233 return OutputFile;
234}
235
Brian Gaekec5cad212004-02-11 18:37:32 +0000236/// executeProgramWithCBE - Used to create reference output with the C
237/// backend, if reference output is not provided.
238///
239std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
240 bool ProgramExitedNonzero;
241 std::string outFN = executeProgram(OutputFile, "", "",
242 (AbstractInterpreter*)cbe,
243 &ProgramExitedNonzero);
244 if (ProgramExitedNonzero) {
245 std::cerr
246 << "Warning: While generating reference output, program exited with\n"
247 << "non-zero exit code. This will NOT be treated as a failure.\n";
248 CheckProgramExitCode = false;
249 }
250 return outFN;
251}
Misha Brukman50733362003-07-24 18:17:43 +0000252
Chris Lattnera0f5b152003-10-14 21:09:11 +0000253std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000254 assert(Interpreter && "Interpreter should have been created already!");
Reid Spencer5f767602004-12-16 23:04:20 +0000255 sys::Path OutputCFile;
Misha Brukman50733362003-07-24 18:17:43 +0000256
257 // Using CBE
Misha Brukman50733362003-07-24 18:17:43 +0000258 cbe->OutputC(BytecodeFile, OutputCFile);
259
260#if 0 /* This is an alternative, as yet unimplemented */
261 // Using LLC
Chris Lattnera0f5b152003-10-14 21:09:11 +0000262 std::string Message;
Misha Brukman41485562003-09-29 22:40:52 +0000263 LLC *llc = createLLCtool(Message);
Misha Brukman50733362003-07-24 18:17:43 +0000264 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
265 std::cerr << "Could not generate asm code with `llc', exiting.\n";
266 exit(1);
267 }
268#endif
269
Chris Lattnera0f5b152003-10-14 21:09:11 +0000270 std::string SharedObjectFile;
Reid Spencer5f767602004-12-16 23:04:20 +0000271 if (gcc->MakeSharedObject(OutputCFile.toString(), GCC::CFile,
272 SharedObjectFile))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000273 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000274
275 // Remove the intermediate C file
Reid Spencer5f767602004-12-16 23:04:20 +0000276 OutputCFile.destroyFile();
Misha Brukman50733362003-07-24 18:17:43 +0000277
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000278 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000279}
280
281
Chris Lattner4a106452002-12-23 23:50:16 +0000282/// diffProgram - This method executes the specified module and diffs the output
283/// against the file specified by ReferenceOutputFile. If the output is
284/// different, true is returned.
285///
Misha Brukman50733362003-07-24 18:17:43 +0000286bool BugDriver::diffProgram(const std::string &BytecodeFile,
287 const std::string &SharedObject,
Chris Lattner640f22e2003-04-24 17:02:17 +0000288 bool RemoveBytecode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000289 bool ProgramExitedNonzero;
290
Chris Lattner4a106452002-12-23 23:50:16 +0000291 // Execute the program, generating an output file...
Reid Spencer5f767602004-12-16 23:04:20 +0000292 sys::Path Output (executeProgram("", BytecodeFile, SharedObject, 0,
293 &ProgramExitedNonzero));
Brian Gaekec5cad212004-02-11 18:37:32 +0000294
295 // If we're checking the program exit code, assume anything nonzero is bad.
Chris Lattner58d84ce2004-04-02 05:33:06 +0000296 if (CheckProgramExitCode && ProgramExitedNonzero) {
Reid Spencer5f767602004-12-16 23:04:20 +0000297 Output.destroyFile();
298 if (RemoveBytecode)
299 sys::Path(BytecodeFile).destroyFile();
Brian Gaekec5cad212004-02-11 18:37:32 +0000300 return true;
Chris Lattner58d84ce2004-04-02 05:33:06 +0000301 }
Chris Lattner4a106452002-12-23 23:50:16 +0000302
Chris Lattner65f62792003-08-01 20:29:45 +0000303 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000304 bool FilesDifferent = false;
Reid Spencer5f767602004-12-16 23:04:20 +0000305 if (DiffFiles(ReferenceOutputFile, Output.toString(), &Error)) {
Chris Lattner65f62792003-08-01 20:29:45 +0000306 if (!Error.empty()) {
Misha Brukmaneed80e22004-07-23 01:30:49 +0000307 std::cerr << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000308 exit(1);
309 }
310 FilesDifferent = true;
311 }
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000312
313 // Remove the generated output.
Reid Spencer5f767602004-12-16 23:04:20 +0000314 Output.destroyFile();
Chris Lattner4a106452002-12-23 23:50:16 +0000315
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000316 // Remove the bytecode file if we are supposed to.
Reid Spencer5f767602004-12-16 23:04:20 +0000317 if (RemoveBytecode) sys::Path(BytecodeFile).destroyFile();
Chris Lattner4a106452002-12-23 23:50:16 +0000318 return FilesDifferent;
319}
Misha Brukman91eabc12003-07-28 19:16:14 +0000320
321bool BugDriver::isExecutingJIT() {
322 return InterpreterSel == RunJIT;
323}
Brian Gaeked0fde302003-11-11 22:41:34 +0000324