blob: ceaf8f498bc64c4979f24e67ecd337ed162eb416 [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//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Gabor Greif8ff70c22007-07-04 21:55:50 +000011// various ways of running LLVM bitcode.
Chris Lattner4a106452002-12-23 23:50:16 +000012//
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>
Reid Spencer51ab5c82006-06-06 00:00:42 +000022
Brian Gaeked0fde302003-11-11 22:41:34 +000023using namespace llvm;
24
Chris Lattner4a106452002-12-23 23:50:16 +000025namespace {
26 // OutputType - Allow the user to specify the way code should be run, to test
27 // for miscompilation.
28 //
29 enum OutputType {
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000030 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE, CBE_bug, LLC_Safe, Custom
Chris Lattner4a106452002-12-23 23:50:16 +000031 };
Misha Brukman41485562003-09-29 22:40:52 +000032
Chris Lattnera328c512005-01-23 03:45:26 +000033 cl::opt<double>
34 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
35 cl::init(0.0));
36 cl::opt<double>
37 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
38 cl::init(0.0));
39
Chris Lattner4a106452002-12-23 23:50:16 +000040 cl::opt<OutputType>
Dan Gohman70ef4492008-12-08 04:02:47 +000041 InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000042 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukmanb687d822004-04-19 03:12:35 +000043 clEnumValN(RunLLI, "run-int",
44 "Execute with the interpreter"),
Misha Brukman50733362003-07-24 18:17:43 +000045 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
46 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
47 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattnerc600f3c2006-09-15 21:29:15 +000048 clEnumValN(CBE_bug,"cbe-bug", "Find CBE bugs"),
Chris Lattnercd6f46e2006-11-09 05:57:53 +000049 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000050 clEnumValN(Custom, "run-custom",
51 "Use -exec-command to define a command to execute "
52 "the bitcode. Useful for cross-compilation."),
Chris Lattner4d143ee2004-07-16 00:08:28 +000053 clEnumValEnd),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000054 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000055
Dan Gohman70ef4492008-12-08 04:02:47 +000056 cl::opt<OutputType>
57 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
Evan Cheng49419e22009-07-21 19:25:09 +000058 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
59 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
60 clEnumValN(RunCBE, "safe-run-cbe", "Compile with CBE"),
61 clEnumValN(Custom, "safe-run-custom",
62 "Use -exec-command to define a command to execute "
63 "the bitcode. Useful for cross-compilation."),
64 clEnumValEnd),
Dan Gohman70ef4492008-12-08 04:02:47 +000065 cl::init(AutoPick));
66
67 cl::opt<std::string>
68 SafeInterpreterPath("safe-path",
Evan Cheng49419e22009-07-21 19:25:09 +000069 cl::desc("Specify the path to the \"safe\" backend program"),
70 cl::init(""));
Dan Gohman70ef4492008-12-08 04:02:47 +000071
Brian Gaekec5cad212004-02-11 18:37:32 +000072 cl::opt<bool>
Reid Spencer5e1452c2006-11-28 07:04:10 +000073 AppendProgramExitCode("append-exit-code",
74 cl::desc("Append the exit code to the output so it gets diff'd too"),
75 cl::init(false));
76
Chris Lattner3c053a02003-04-23 20:31:37 +000077 cl::opt<std::string>
78 InputFile("input", cl::init("/dev/null"),
79 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000080
81 cl::list<std::string>
82 AdditionalSOs("additional-so",
83 cl::desc("Additional shared objects to load "
84 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000085
Reid Spencer51ab5c82006-06-06 00:00:42 +000086 cl::list<std::string>
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000087 AdditionalLinkerArgs("Xlinker",
Reid Spencer51ab5c82006-06-06 00:00:42 +000088 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000089
90 cl::opt<std::string>
91 CustomExecCommand("exec-command", cl::init("simulate"),
92 cl::desc("Command to execute the bitcode (use with -run-custom) "
93 "(default: simulate)"));
Chris Lattner4a106452002-12-23 23:50:16 +000094}
95
Brian Gaeked0fde302003-11-11 22:41:34 +000096namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +000097 // Anything specified after the --args option are taken as arguments to the
98 // program being debugged.
99 cl::list<std::string>
100 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000101 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman70ef4492008-12-08 04:02:47 +0000102}
Brian Gaeke636df3d2004-05-04 21:09:16 +0000103
Dan Gohman70ef4492008-12-08 04:02:47 +0000104namespace {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000105 cl::list<std::string>
106 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000107 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman70ef4492008-12-08 04:02:47 +0000108
109 cl::list<std::string>
110 SafeToolArgv("safe-tool-args", cl::Positional,
111 cl::desc("<safe-tool arguments>..."),
112 cl::ZeroOrMore, cl::PositionalEatsArgs);
Bill Wendling38efa382009-03-02 23:13:18 +0000113
114 cl::list<std::string>
115 GCCToolArgv("gcc-tool-args", cl::Positional,
116 cl::desc("<gcc-tool arguments>..."),
117 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +0000118}
Misha Brukman9d679cb2003-07-30 20:15:44 +0000119
Chris Lattner4a106452002-12-23 23:50:16 +0000120//===----------------------------------------------------------------------===//
121// BugDriver method implementation
122//
123
124/// initializeExecutionEnvironment - This method is used to set up the
125/// environment for executing LLVM programs.
126///
127bool BugDriver::initializeExecutionEnvironment() {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000128 outs() << "Initializing execution environment: ";
Chris Lattner4a106452002-12-23 23:50:16 +0000129
Misha Brukman41485562003-09-29 22:40:52 +0000130 // Create an instance of the AbstractInterpreter interface as specified on
131 // the command line
Dan Gohman70ef4492008-12-08 04:02:47 +0000132 SafeInterpreter = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000133 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000134
Chris Lattnercc876a72003-05-03 03:19:41 +0000135 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000136 case AutoPick:
137 InterpreterSel = RunCBE;
Dan Gohman70ef4492008-12-08 04:02:47 +0000138 Interpreter =
Bill Wendling38efa382009-03-02 23:13:18 +0000139 AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv,
140 &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000141 if (!Interpreter) {
142 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000143 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
144 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000145 }
146 if (!Interpreter) {
147 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000148 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000149 &ToolArgv, &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000150 }
151 if (!Interpreter) {
152 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000153 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
154 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000155 }
156 if (!Interpreter) {
157 InterpreterSel = AutoPick;
158 Message = "Sorry, I can't automatically select an interpreter!\n";
159 }
160 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000161 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000162 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
163 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000164 break;
165 case RunLLC:
Dan Gohman70ef4492008-12-08 04:02:47 +0000166 case LLC_Safe:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000167 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000168 &ToolArgv, &GCCToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000169 break;
170 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000171 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
172 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000173 break;
174 case RunCBE:
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000175 case CBE_bug:
176 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000177 &ToolArgv, &GCCToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000178 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000179 case Custom:
180 Interpreter = AbstractInterpreter::createCustom(getToolName(), Message,
181 CustomExecCommand);
182 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000183 default:
Misha Brukman41485562003-09-29 22:40:52 +0000184 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000185 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000186 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000187 if (!Interpreter)
Dan Gohman65f57c22009-07-15 16:35:29 +0000188 errs() << Message;
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000189 else // Display informational messages on stdout instead of stderr
Dan Gohmanac95cc72009-07-16 15:30:09 +0000190 outs() << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000191
Dan Gohman70ef4492008-12-08 04:02:47 +0000192 std::string Path = SafeInterpreterPath;
193 if (Path.empty())
194 Path = getToolName();
195 std::vector<std::string> SafeToolArgs = SafeToolArgv;
196 switch (SafeInterpreterSel) {
197 case AutoPick:
198 // In "cbe-bug" mode, default to using LLC as the "safe" backend.
199 if (!SafeInterpreter &&
200 InterpreterSel == CBE_bug) {
201 SafeInterpreterSel = RunLLC;
202 SafeToolArgs.push_back("--relocation-model=pic");
203 SafeInterpreter = AbstractInterpreter::createLLC(Path, Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000204 &SafeToolArgs,
205 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000206 }
207
208 // In "llc-safe" mode, default to using LLC as the "safe" backend.
209 if (!SafeInterpreter &&
210 InterpreterSel == LLC_Safe) {
211 SafeInterpreterSel = RunLLC;
212 SafeToolArgs.push_back("--relocation-model=pic");
213 SafeInterpreter = AbstractInterpreter::createLLC(Path, Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000214 &SafeToolArgs,
215 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000216 }
217
218 // Pick a backend that's different from the test backend. The JIT and
219 // LLC backends share a lot of code, so prefer to use the CBE as the
220 // safe back-end when testing them.
221 if (!SafeInterpreter &&
222 InterpreterSel != RunCBE) {
223 SafeInterpreterSel = RunCBE;
224 SafeInterpreter = AbstractInterpreter::createCBE(Path, Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000225 &SafeToolArgs,
226 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000227 }
228 if (!SafeInterpreter &&
229 InterpreterSel != RunLLC &&
230 InterpreterSel != RunJIT) {
231 SafeInterpreterSel = RunLLC;
232 SafeToolArgs.push_back("--relocation-model=pic");
233 SafeInterpreter = AbstractInterpreter::createLLC(Path, Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000234 &SafeToolArgs,
235 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000236 }
237 if (!SafeInterpreter) {
238 SafeInterpreterSel = AutoPick;
239 Message = "Sorry, I can't automatically select an interpreter!\n";
240 }
241 break;
242 case RunLLC:
243 SafeToolArgs.push_back("--relocation-model=pic");
244 SafeInterpreter = AbstractInterpreter::createLLC(Path, Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000245 &SafeToolArgs,
246 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000247 break;
248 case RunCBE:
249 SafeInterpreter = AbstractInterpreter::createCBE(Path, Message,
Bill Wendling38efa382009-03-02 23:13:18 +0000250 &SafeToolArgs,
251 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000252 break;
253 case Custom:
254 SafeInterpreter = AbstractInterpreter::createCustom(Path, Message,
255 CustomExecCommand);
256 break;
257 default:
258 Message = "Sorry, this back-end is not supported by bugpoint as the "
259 "\"safe\" backend right now!\n";
260 break;
Chris Lattner7bb11542004-02-18 20:52:02 +0000261 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000262 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000263
Bill Wendling38efa382009-03-02 23:13:18 +0000264 gcc = GCC::create(getToolName(), Message, &GCCToolArgv);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000265 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000266
Chris Lattner4a106452002-12-23 23:50:16 +0000267 // If there was an error creating the selected interpreter, quit with error.
268 return Interpreter == 0;
269}
270
Chris Lattnerea9212c2004-02-18 23:25:22 +0000271/// compileProgram - Try to compile the specified module, throwing an exception
272/// if an error occurs, or returning normally if not. This is used for code
273/// generation crash testing.
274///
275void BugDriver::compileProgram(Module *M) {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000276 // Emit the program to a bitcode file...
277 sys::Path BitcodeFile ("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000278 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000279 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000280 errs() << ToolName << ": Error making unique filename: " << ErrMsg
281 << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000282 exit(1);
283 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000284 if (writeProgramToFile(BitcodeFile.toString(), M)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000285 errs() << ToolName << ": Error emitting bitcode to file '"
286 << BitcodeFile << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000287 exit(1);
288 }
289
Gabor Greif8ff70c22007-07-04 21:55:50 +0000290 // Remove the temporary bitcode file when we are done.
Anton Korobeynikov86c006a2009-08-05 09:32:10 +0000291 FileRemover BitcodeFileRemover(BitcodeFile, !SaveTemps);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000292
293 // Actually compile the program!
Gabor Greif8ff70c22007-07-04 21:55:50 +0000294 Interpreter->compileProgram(BitcodeFile.toString());
Chris Lattnerea9212c2004-02-18 23:25:22 +0000295}
296
Chris Lattner4a106452002-12-23 23:50:16 +0000297
298/// executeProgram - This method runs "Program", capturing the output of the
299/// program to a file, returning the filename of the file. A recommended
300/// filename may be optionally specified.
301///
302std::string BugDriver::executeProgram(std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000303 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000304 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000305 AbstractInterpreter *AI,
306 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000307 if (AI == 0) AI = Interpreter;
308 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000309 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000310 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000311 if (BitcodeFile.empty()) {
312 // Emit the program to a bitcode file...
Reid Spencer97182982004-12-15 01:53:08 +0000313 sys::Path uniqueFilename("bugpoint-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000314 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000315 errs() << ToolName << ": Error making unique filename: "
316 << ErrMsg << "!\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000317 exit(1);
318 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000319 BitcodeFile = uniqueFilename.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000320
Gabor Greif8ff70c22007-07-04 21:55:50 +0000321 if (writeProgramToFile(BitcodeFile, Program)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000322 errs() << ToolName << ": Error emitting bitcode to file '"
323 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000324 exit(1);
325 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000326 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000327 }
328
Gabor Greif8ff70c22007-07-04 21:55:50 +0000329 // Remove the temporary bitcode file when we are done.
330 sys::Path BitcodePath (BitcodeFile);
Anton Korobeynikov86c006a2009-08-05 09:32:10 +0000331 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
Chris Lattner97092722004-02-18 22:01:21 +0000332
Chris Lattner4a106452002-12-23 23:50:16 +0000333 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000334
Chris Lattner4a106452002-12-23 23:50:16 +0000335 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000336 sys::Path uniqueFile(OutputFile);
Reid Spencer51c5a282006-08-23 20:34:57 +0000337 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000338 errs() << ToolName << ": Error making unique filename: "
339 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000340 exit(1);
341 }
Reid Spencer97182982004-12-15 01:53:08 +0000342 OutputFile = uniqueFile.toString();
Chris Lattner4a106452002-12-23 23:50:16 +0000343
Chris Lattner769f1fe2003-10-14 21:59:36 +0000344 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000345 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000346 if (!SharedObj.empty())
347 SharedObjs.push_back(SharedObj);
348
Dan Gohman70ef4492008-12-08 04:02:47 +0000349 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
350 OutputFile, AdditionalLinkerArgs, SharedObjs,
351 Timeout, MemoryLimit);
Chris Lattner7d91e492004-07-24 07:53:26 +0000352
353 if (RetVal == -1) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000354 errs() << "<timeout>";
Chris Lattner7d91e492004-07-24 07:53:26 +0000355 static bool FirstTimeout = true;
356 if (FirstTimeout) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000357 outs() << "\n"
Chris Lattner7d91e492004-07-24 07:53:26 +0000358 "*** Program execution timed out! This mechanism is designed to handle\n"
359 " programs stuck in infinite loops gracefully. The -timeout option\n"
360 " can be used to change the timeout threshold or disable it completely\n"
361 " (with -timeout=0). This message is only displayed once.\n";
362 FirstTimeout = false;
363 }
364 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000365
Reid Spencer5e1452c2006-11-28 07:04:10 +0000366 if (AppendProgramExitCode) {
367 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
368 outFile << "exit " << RetVal << '\n';
369 outFile.close();
370 }
371
Brian Gaekec5cad212004-02-11 18:37:32 +0000372 if (ProgramExitedNonzero != 0)
373 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000374
Chris Lattner4a106452002-12-23 23:50:16 +0000375 // Return the filename we captured the output to.
376 return OutputFile;
377}
378
Dan Gohman70ef4492008-12-08 04:02:47 +0000379/// executeProgramSafely - Used to create reference output with the "safe"
Brian Gaekec5cad212004-02-11 18:37:32 +0000380/// backend, if reference output is not provided.
381///
Dan Gohman70ef4492008-12-08 04:02:47 +0000382std::string BugDriver::executeProgramSafely(std::string OutputFile) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000383 bool ProgramExitedNonzero;
Dan Gohman70ef4492008-12-08 04:02:47 +0000384 std::string outFN = executeProgram(OutputFile, "", "", SafeInterpreter,
Brian Gaekec5cad212004-02-11 18:37:32 +0000385 &ProgramExitedNonzero);
Brian Gaekec5cad212004-02-11 18:37:32 +0000386 return outFN;
387}
Misha Brukman50733362003-07-24 18:17:43 +0000388
Gabor Greif8ff70c22007-07-04 21:55:50 +0000389std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000390 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000391 sys::Path OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000392
Dan Gohman70ef4492008-12-08 04:02:47 +0000393 // Using the known-good backend.
394 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
Misha Brukman50733362003-07-24 18:17:43 +0000395
Chris Lattnera0f5b152003-10-14 21:09:11 +0000396 std::string SharedObjectFile;
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000397 if (gcc->MakeSharedObject(OutputFile.toString(), FT,
Chris Lattner130e2a32006-06-27 20:35:36 +0000398 SharedObjectFile, AdditionalLinkerArgs))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000399 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000400
401 // Remove the intermediate C file
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000402 OutputFile.eraseFromDisk();
Misha Brukman50733362003-07-24 18:17:43 +0000403
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000404 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000405}
406
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000407/// createReferenceFile - calls compileProgram and then records the output
408/// into ReferenceOutputFile. Returns true if reference file created, false
409/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
410/// this function.
411///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000412bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000413 try {
414 compileProgram(Program);
Jeff Cohend41b30d2006-11-05 19:31:28 +0000415 } catch (ToolExecutionError &) {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000416 return false;
417 }
418 try {
Dan Gohman70ef4492008-12-08 04:02:47 +0000419 ReferenceOutputFile = executeProgramSafely(Filename);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000420 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000421 } catch (ToolExecutionError &TEE) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000422 errs() << TEE.what();
Dan Gohman70ef4492008-12-08 04:02:47 +0000423 if (Interpreter != SafeInterpreter) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000424 errs() << "*** There is a bug running the \"safe\" backend. Either"
425 << " debug it (for example with the -run-cbe bugpoint option,"
426 << " if CBE is being used as the \"safe\" backend), or fix the"
427 << " error some other way.\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000428 }
429 return false;
430 }
431 return true;
432}
Misha Brukman50733362003-07-24 18:17:43 +0000433
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000434/// diffProgram - This method executes the specified module and diffs the
435/// output against the file specified by ReferenceOutputFile. If the output
436/// is different, true is returned. If there is a problem with the code
437/// generator (e.g., llc crashes), this will throw an exception.
Chris Lattner4a106452002-12-23 23:50:16 +0000438///
Gabor Greif8ff70c22007-07-04 21:55:50 +0000439bool BugDriver::diffProgram(const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000440 const std::string &SharedObject,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000441 bool RemoveBitcode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000442 bool ProgramExitedNonzero;
443
Chris Lattner4a106452002-12-23 23:50:16 +0000444 // Execute the program, generating an output file...
Gabor Greif8ff70c22007-07-04 21:55:50 +0000445 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0,
Reid Spencer5f767602004-12-16 23:04:20 +0000446 &ProgramExitedNonzero));
Brian Gaekec5cad212004-02-11 18:37:32 +0000447
Chris Lattner65f62792003-08-01 20:29:45 +0000448 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000449 bool FilesDifferent = false;
Chris Lattnera328c512005-01-23 03:45:26 +0000450 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
451 sys::Path(Output.toString()),
452 AbsTolerance, RelTolerance, &Error)) {
453 if (Diff == 2) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000454 errs() << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000455 exit(1);
456 }
457 FilesDifferent = true;
458 }
David Goodwin80becf12009-07-10 21:39:28 +0000459 else {
460 // Remove the generated output if there are no differences.
461 Output.eraseFromDisk();
462 }
Chris Lattner4a106452002-12-23 23:50:16 +0000463
Gabor Greif8ff70c22007-07-04 21:55:50 +0000464 // Remove the bitcode file if we are supposed to.
465 if (RemoveBitcode)
466 sys::Path(BitcodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000467 return FilesDifferent;
468}
Misha Brukman91eabc12003-07-28 19:16:14 +0000469
470bool BugDriver::isExecutingJIT() {
471 return InterpreterSel == RunJIT;
472}
Brian Gaeked0fde302003-11-11 22:41:34 +0000473