blob: 72e564ec3c09d17f5ce9766600f6ac7b9c4a1aee [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"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000016#include "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>
Chris Lattnere31a9cc2006-01-22 22:53:40 +000022#include <iostream>
Reid Spencer51ab5c82006-06-06 00:00:42 +000023
Brian Gaeked0fde302003-11-11 22:41:34 +000024using namespace llvm;
25
Chris Lattner4a106452002-12-23 23:50:16 +000026namespace {
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
29 //
30 enum OutputType {
Brian Gaekeb5ee5092003-10-21 17:41:35 +000031 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
Chris Lattner4a106452002-12-23 23:50:16 +000032 };
Misha Brukman41485562003-09-29 22:40:52 +000033
Chris Lattnera328c512005-01-23 03:45:26 +000034 cl::opt<double>
35 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
36 cl::init(0.0));
37 cl::opt<double>
38 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
39 cl::init(0.0));
40
Chris Lattner4a106452002-12-23 23:50:16 +000041 cl::opt<OutputType>
42 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000043 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukmanb687d822004-04-19 03:12:35 +000044 clEnumValN(RunLLI, "run-int",
45 "Execute with the interpreter"),
Misha Brukman50733362003-07-24 18:17:43 +000046 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
47 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
48 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000049 clEnumValEnd),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000050 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000051
Brian Gaekec5cad212004-02-11 18:37:32 +000052 cl::opt<bool>
53 CheckProgramExitCode("check-exit-code",
Misha Brukmaneed80e22004-07-23 01:30:49 +000054 cl::desc("Assume nonzero exit code is failure (default on)"),
Brian Gaekec5cad212004-02-11 18:37:32 +000055 cl::init(true));
56
Chris Lattner3c053a02003-04-23 20:31:37 +000057 cl::opt<std::string>
58 InputFile("input", cl::init("/dev/null"),
59 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000060
61 cl::list<std::string>
62 AdditionalSOs("additional-so",
63 cl::desc("Additional shared objects to load "
64 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000065
66 cl::opt<unsigned>
67 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
68 cl::desc("Number of seconds program is allowed to run before it "
69 "is killed (default is 300s), 0 disables timeout"));
Reid Spencer51ab5c82006-06-06 00:00:42 +000070
71 cl::list<std::string>
72 AdditionalLinkerArgs("Xlinker",
73 cl::desc("Additional arguments to pass to the linker"));
Chris Lattner4a106452002-12-23 23:50:16 +000074}
75
Brian Gaeked0fde302003-11-11 22:41:34 +000076namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +000077 // Anything specified after the --args option are taken as arguments to the
78 // program being debugged.
79 cl::list<std::string>
80 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000081 cl::ZeroOrMore, cl::PositionalEatsArgs);
Brian Gaeke636df3d2004-05-04 21:09:16 +000082
83 cl::list<std::string>
84 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000085 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +000086}
Misha Brukman9d679cb2003-07-30 20:15:44 +000087
Chris Lattner4a106452002-12-23 23:50:16 +000088//===----------------------------------------------------------------------===//
89// BugDriver method implementation
90//
91
92/// initializeExecutionEnvironment - This method is used to set up the
93/// environment for executing LLVM programs.
94///
95bool BugDriver::initializeExecutionEnvironment() {
96 std::cout << "Initializing execution environment: ";
97
Misha Brukman41485562003-09-29 22:40:52 +000098 // Create an instance of the AbstractInterpreter interface as specified on
99 // the command line
Chris Lattner7bb11542004-02-18 20:52:02 +0000100 cbe = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000101 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000102
Chris Lattnercc876a72003-05-03 03:19:41 +0000103 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000104 case AutoPick:
105 InterpreterSel = RunCBE;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000106 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
107 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000108 if (!Interpreter) {
109 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000110 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
111 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000112 }
113 if (!Interpreter) {
114 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000115 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
116 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000117 }
118 if (!Interpreter) {
119 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000120 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
121 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000122 }
123 if (!Interpreter) {
124 InterpreterSel = AutoPick;
125 Message = "Sorry, I can't automatically select an interpreter!\n";
126 }
127 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000128 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000129 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
130 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000131 break;
132 case RunLLC:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000133 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
134 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000135 break;
136 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000137 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
138 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000139 break;
140 case RunCBE:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000141 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
142 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000143 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000144 default:
Misha Brukman41485562003-09-29 22:40:52 +0000145 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000146 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000147 }
Misha Brukman41485562003-09-29 22:40:52 +0000148 std::cerr << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000149
Misha Brukmana259c9b2003-07-24 21:59:10 +0000150 // Initialize auxiliary tools for debugging
Chris Lattner7bb11542004-02-18 20:52:02 +0000151 if (!cbe) {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000152 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Chris Lattner7bb11542004-02-18 20:52:02 +0000153 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
154 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000155 gcc = GCC::create(getToolName(), Message);
Misha Brukmana259c9b2003-07-24 21:59:10 +0000156 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
157
Chris Lattner4a106452002-12-23 23:50:16 +0000158 // If there was an error creating the selected interpreter, quit with error.
159 return Interpreter == 0;
160}
161
Chris Lattnerea9212c2004-02-18 23:25:22 +0000162/// compileProgram - Try to compile the specified module, throwing an exception
163/// if an error occurs, or returning normally if not. This is used for code
164/// generation crash testing.
165///
166void BugDriver::compileProgram(Module *M) {
167 // Emit the program to a bytecode file...
Reid Spencer97182982004-12-15 01:53:08 +0000168 sys::Path BytecodeFile ("bugpoint-test-program.bc");
169 BytecodeFile.makeUnique();
170 if (writeProgramToFile(BytecodeFile.toString(), M)) {
Chris Lattnerea9212c2004-02-18 23:25:22 +0000171 std::cerr << ToolName << ": Error emitting bytecode to file '"
172 << BytecodeFile << "'!\n";
173 exit(1);
174 }
175
176 // Remove the temporary bytecode file when we are done.
Reid Spencer5f767602004-12-16 23:04:20 +0000177 FileRemover BytecodeFileRemover(BytecodeFile);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000178
179 // Actually compile the program!
Reid Spencer97182982004-12-15 01:53:08 +0000180 Interpreter->compileProgram(BytecodeFile.toString());
Chris Lattnerea9212c2004-02-18 23:25:22 +0000181}
182
Chris Lattner4a106452002-12-23 23:50:16 +0000183
184/// executeProgram - This method runs "Program", capturing the output of the
185/// program to a file, returning the filename of the file. A recommended
186/// filename may be optionally specified.
187///
188std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukman50733362003-07-24 18:17:43 +0000189 std::string BytecodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000190 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000191 AbstractInterpreter *AI,
192 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000193 if (AI == 0) AI = Interpreter;
194 assert(AI && "Interpreter should have been created already!");
Chris Lattner4a106452002-12-23 23:50:16 +0000195 bool CreatedBytecode = false;
196 if (BytecodeFile.empty()) {
197 // Emit the program to a bytecode file...
Reid Spencer97182982004-12-15 01:53:08 +0000198 sys::Path uniqueFilename("bugpoint-test-program.bc");
199 uniqueFilename.makeUnique();
200 BytecodeFile = uniqueFilename.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000201
202 if (writeProgramToFile(BytecodeFile, Program)) {
203 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukman50733362003-07-24 18:17:43 +0000204 << BytecodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000205 exit(1);
206 }
207 CreatedBytecode = true;
208 }
209
Chris Lattner97092722004-02-18 22:01:21 +0000210 // Remove the temporary bytecode file when we are done.
Brian Gaeke06c375b2004-12-22 22:33:33 +0000211 sys::Path BytecodePath (BytecodeFile);
212 FileRemover BytecodeFileRemover(BytecodePath, CreatedBytecode);
Chris Lattner97092722004-02-18 22:01:21 +0000213
Chris Lattner4a106452002-12-23 23:50:16 +0000214 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000215
Chris Lattner4a106452002-12-23 23:50:16 +0000216 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000217 sys::Path uniqueFile(OutputFile);
218 uniqueFile.makeUnique();
219 OutputFile = uniqueFile.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000220
Chris Lattner769f1fe2003-10-14 21:59:36 +0000221 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000222 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000223 if (!SharedObj.empty())
224 SharedObjs.push_back(SharedObj);
225
Reid Spencer51ab5c82006-06-06 00:00:42 +0000226
227 // If this is an LLC or CBE run, then the GCC compiler might get run to
228 // compile the program. If so, we should pass the user's -Xlinker options
229 // as the GCCArgs.
230 int RetVal = 0;
231 if (InterpreterSel == RunLLC || InterpreterSel == RunCBE)
232 RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
233 OutputFile, AdditionalLinkerArgs, SharedObjs,
234 TimeoutValue);
235 else
236 RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
237 OutputFile, std::vector<std::string>(),
238 SharedObjs, TimeoutValue);
Chris Lattner7d91e492004-07-24 07:53:26 +0000239
240 if (RetVal == -1) {
241 std::cerr << "<timeout>";
242 static bool FirstTimeout = true;
243 if (FirstTimeout) {
244 std::cout << "\n"
245 "*** Program execution timed out! This mechanism is designed to handle\n"
246 " programs stuck in infinite loops gracefully. The -timeout option\n"
247 " can be used to change the timeout threshold or disable it completely\n"
248 " (with -timeout=0). This message is only displayed once.\n";
249 FirstTimeout = false;
250 }
251 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000252
Brian Gaekec5cad212004-02-11 18:37:32 +0000253 if (ProgramExitedNonzero != 0)
254 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000255
Chris Lattner4a106452002-12-23 23:50:16 +0000256 // Return the filename we captured the output to.
257 return OutputFile;
258}
259
Brian Gaekec5cad212004-02-11 18:37:32 +0000260/// executeProgramWithCBE - Used to create reference output with the C
261/// backend, if reference output is not provided.
262///
263std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
264 bool ProgramExitedNonzero;
265 std::string outFN = executeProgram(OutputFile, "", "",
266 (AbstractInterpreter*)cbe,
267 &ProgramExitedNonzero);
268 if (ProgramExitedNonzero) {
269 std::cerr
270 << "Warning: While generating reference output, program exited with\n"
271 << "non-zero exit code. This will NOT be treated as a failure.\n";
272 CheckProgramExitCode = false;
273 }
274 return outFN;
275}
Misha Brukman50733362003-07-24 18:17:43 +0000276
Chris Lattnera0f5b152003-10-14 21:09:11 +0000277std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000278 assert(Interpreter && "Interpreter should have been created already!");
Reid Spencer5f767602004-12-16 23:04:20 +0000279 sys::Path OutputCFile;
Misha Brukman50733362003-07-24 18:17:43 +0000280
281 // Using CBE
Misha Brukman50733362003-07-24 18:17:43 +0000282 cbe->OutputC(BytecodeFile, OutputCFile);
283
284#if 0 /* This is an alternative, as yet unimplemented */
285 // Using LLC
Chris Lattnera0f5b152003-10-14 21:09:11 +0000286 std::string Message;
Misha Brukman41485562003-09-29 22:40:52 +0000287 LLC *llc = createLLCtool(Message);
Misha Brukman50733362003-07-24 18:17:43 +0000288 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
289 std::cerr << "Could not generate asm code with `llc', exiting.\n";
290 exit(1);
291 }
292#endif
293
Chris Lattnera0f5b152003-10-14 21:09:11 +0000294 std::string SharedObjectFile;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000295 if (gcc->MakeSharedObject(OutputCFile.toString(), GCC::CFile,
Reid Spencer5f767602004-12-16 23:04:20 +0000296 SharedObjectFile))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000297 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000298
299 // Remove the intermediate C file
Reid Spencera229c5c2005-07-08 03:08:58 +0000300 OutputCFile.eraseFromDisk();
Misha Brukman50733362003-07-24 18:17:43 +0000301
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000302 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000303}
304
305
Chris Lattner4a106452002-12-23 23:50:16 +0000306/// diffProgram - This method executes the specified module and diffs the output
307/// against the file specified by ReferenceOutputFile. If the output is
308/// different, true is returned.
309///
Misha Brukman50733362003-07-24 18:17:43 +0000310bool BugDriver::diffProgram(const std::string &BytecodeFile,
311 const std::string &SharedObject,
Chris Lattner640f22e2003-04-24 17:02:17 +0000312 bool RemoveBytecode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000313 bool ProgramExitedNonzero;
314
Chris Lattner4a106452002-12-23 23:50:16 +0000315 // Execute the program, generating an output file...
Reid Spencer5f767602004-12-16 23:04:20 +0000316 sys::Path Output (executeProgram("", BytecodeFile, SharedObject, 0,
317 &ProgramExitedNonzero));
Brian Gaekec5cad212004-02-11 18:37:32 +0000318
319 // If we're checking the program exit code, assume anything nonzero is bad.
Chris Lattner58d84ce2004-04-02 05:33:06 +0000320 if (CheckProgramExitCode && ProgramExitedNonzero) {
Reid Spencera229c5c2005-07-08 03:08:58 +0000321 Output.eraseFromDisk();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000322 if (RemoveBytecode)
Reid Spencera229c5c2005-07-08 03:08:58 +0000323 sys::Path(BytecodeFile).eraseFromDisk();
Brian Gaekec5cad212004-02-11 18:37:32 +0000324 return true;
Chris Lattner58d84ce2004-04-02 05:33:06 +0000325 }
Chris Lattner4a106452002-12-23 23:50:16 +0000326
Chris Lattner65f62792003-08-01 20:29:45 +0000327 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000328 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000329 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
330 sys::Path(Output.toString()),
331 AbsTolerance, RelTolerance, &Error)) {
332 if (Diff == 2) {
Misha Brukmaneed80e22004-07-23 01:30:49 +0000333 std::cerr << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000334 exit(1);
335 }
336 FilesDifferent = true;
337 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000338
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000339 // Remove the generated output.
Reid Spencera229c5c2005-07-08 03:08:58 +0000340 Output.eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000341
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000342 // Remove the bytecode file if we are supposed to.
Jeff Cohen00b168892005-07-27 06:12:32 +0000343 if (RemoveBytecode)
Reid Spencera229c5c2005-07-08 03:08:58 +0000344 sys::Path(BytecodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000345 return FilesDifferent;
346}
Misha Brukman91eabc12003-07-28 19:16:14 +0000347
348bool BugDriver::isExecutingJIT() {
349 return InterpreterSel == RunJIT;
350}
Brian Gaeked0fde302003-11-11 22:41:34 +0000351