blob: 236467550407a7b8adbda997da56b7b21f40be6f [file] [log] [blame]
Chris Lattner94321112003-10-20 17:57:13 +00001//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
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 Lattnerafade922002-11-20 22:28:10 +00009//
10// This program is an automated compiler debugger tool. It is used to narrow
11// down miscompilations and crash problems to a specific pass in the compiler,
12// and the specific Module or Function input that is causing the problem.
13//
14//===----------------------------------------------------------------------===//
15
16#include "BugDriver.h"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000017#include "ToolRunner.h"
Reid Spencer62c51052006-08-21 05:34:03 +000018#include "llvm/LinkAllPasses.h"
Chris Lattnerafade922002-11-20 22:28:10 +000019#include "llvm/Support/PassNameParser.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000021#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/PluginLoader.h"
Reid Spencer51163302004-12-27 06:18:02 +000023#include "llvm/System/Process.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Reid Spencere3f05612006-06-07 23:06:50 +000025#include "llvm/LinkAllVMCore.h"
Bill Wendlinga089d442006-11-17 10:09:22 +000026#include <iostream>
Brian Gaeked0fde302003-11-11 22:41:34 +000027using namespace llvm;
28
Reid Spencerc4bb0522005-12-22 20:02:55 +000029// AsChild - Specifies that this invocation of bugpoint is being generated
30// from a parent process. It is not intended to be used by users so the
31// option is hidden.
32static cl::opt<bool>
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000033AsChild("as-child", cl::desc("Run bugpoint as child process"),
34 cl::ReallyHidden);
35
36static cl::opt<bool>
Dan Gohman39564492008-02-18 17:15:45 +000037FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000038 "on program to find bugs"), cl::init(false));
Reid Spencerc4bb0522005-12-22 20:02:55 +000039
Chris Lattnerafade922002-11-20 22:28:10 +000040static cl::list<std::string>
41InputFilenames(cl::Positional, cl::OneOrMore,
42 cl::desc("<input llvm ll/bc files>"));
43
Chris Lattner9686ae72006-06-13 03:10:48 +000044static cl::opt<unsigned>
45TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
46 cl::desc("Number of seconds program is allowed to run before it "
47 "is killed (default is 300s), 0 disables timeout"));
48
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000049static cl::opt<unsigned>
50MemoryLimit("mlimit", cl::init(100), cl::value_desc("MBytes"),
51 cl::desc("Maximum amount of memory to use. 0 disables check."));
52
Chris Lattnerafade922002-11-20 22:28:10 +000053// The AnalysesList is automatically populated with registered Passes by the
54// PassNameParser.
55//
56static cl::list<const PassInfo*, bool, PassNameParser>
Misha Brukman50733362003-07-24 18:17:43 +000057PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
Chris Lattnerafade922002-11-20 22:28:10 +000058
Chris Lattnerf9aaae02005-08-02 02:16:17 +000059/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
60bool llvm::BugpointIsInterrupted = false;
61
62static void BugpointInterruptFunction() {
63 BugpointIsInterrupted = true;
64}
65
Chris Lattnerafade922002-11-20 22:28:10 +000066int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000067 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattner670406d2003-10-18 21:55:35 +000068 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +000069 "LLVM automatic testcase reducer. See\nhttp://"
Reid Spencer9dce2b32006-03-14 05:54:52 +000070 "llvm.org/docs/CommandGuide/bugpoint.html"
Chris Lattner670406d2003-10-18 21:55:35 +000071 " for more information.\n");
Reid Spencer9de7b332004-08-29 19:28:55 +000072 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf9aaae02005-08-02 02:16:17 +000073 sys::SetInterruptFunction(BugpointInterruptFunction);
74
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000075 BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit);
Chris Lattnerafade922002-11-20 22:28:10 +000076 if (D.addSources(InputFilenames)) return 1;
77 D.addPasses(PassList.begin(), PassList.end());
78
Misha Brukman67b36e42003-09-12 20:42:57 +000079 // Bugpoint has the ability of generating a plethora of core files, so to
Reid Spencer551ccae2004-09-01 22:55:40 +000080 // avoid filling up the disk, we prevent it
Reid Spencer51163302004-12-27 06:18:02 +000081 sys::Process::PreventCoreFiles();
Misha Brukman67b36e42003-09-12 20:42:57 +000082
Chris Lattner74d45272004-02-18 17:32:54 +000083 try {
84 return D.run();
Chris Lattner230fef82004-02-18 20:22:11 +000085 } catch (ToolExecutionError &TEE) {
Misha Brukmaneed80e22004-07-23 01:30:49 +000086 std::cerr << "Tool execution error: " << TEE.what() << '\n';
Reid Spencer1ef8bda2004-12-30 05:36:08 +000087 } catch (const std::string& msg) {
88 std::cerr << argv[0] << ": " << msg << "\n";
Chris Lattner74d45272004-02-18 17:32:54 +000089 } catch (...) {
90 std::cerr << "Whoops, an exception leaked out of bugpoint. "
91 << "This is a bug in bugpoint!\n";
Chris Lattner74d45272004-02-18 17:32:54 +000092 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +000093 return 1;
Chris Lattnerafade922002-11-20 22:28:10 +000094}