blob: 20f0e99a848881b40180c27afef40bb912d05ce5 [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"
Chris Lattnercc14d252009-03-06 05:34:10 +000023#include "llvm/Support/PrettyStackTrace.h"
Reid Spencer51163302004-12-27 06:18:02 +000024#include "llvm/System/Process.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000025#include "llvm/System/Signals.h"
Reid Spencere3f05612006-06-07 23:06:50 +000026#include "llvm/LinkAllVMCore.h"
Bill Wendlinga089d442006-11-17 10:09:22 +000027#include <iostream>
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Reid Spencerc4bb0522005-12-22 20:02:55 +000030// AsChild - Specifies that this invocation of bugpoint is being generated
31// from a parent process. It is not intended to be used by users so the
32// option is hidden.
33static cl::opt<bool>
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000034AsChild("as-child", cl::desc("Run bugpoint as child process"),
35 cl::ReallyHidden);
36
37static cl::opt<bool>
Dan Gohman39564492008-02-18 17:15:45 +000038FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000039 "on program to find bugs"), cl::init(false));
Reid Spencerc4bb0522005-12-22 20:02:55 +000040
Chris Lattnerafade922002-11-20 22:28:10 +000041static cl::list<std::string>
42InputFilenames(cl::Positional, cl::OneOrMore,
43 cl::desc("<input llvm ll/bc files>"));
44
Chris Lattner9686ae72006-06-13 03:10:48 +000045static cl::opt<unsigned>
46TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
47 cl::desc("Number of seconds program is allowed to run before it "
48 "is killed (default is 300s), 0 disables timeout"));
49
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000050static cl::opt<unsigned>
51MemoryLimit("mlimit", cl::init(100), cl::value_desc("MBytes"),
52 cl::desc("Maximum amount of memory to use. 0 disables check."));
53
Chris Lattnerafade922002-11-20 22:28:10 +000054// The AnalysesList is automatically populated with registered Passes by the
55// PassNameParser.
56//
57static cl::list<const PassInfo*, bool, PassNameParser>
Misha Brukman50733362003-07-24 18:17:43 +000058PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
Chris Lattnerafade922002-11-20 22:28:10 +000059
Chris Lattnerf9aaae02005-08-02 02:16:17 +000060/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
61bool llvm::BugpointIsInterrupted = false;
62
63static void BugpointInterruptFunction() {
64 BugpointIsInterrupted = true;
65}
66
Chris Lattnerafade922002-11-20 22:28:10 +000067int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000068 llvm::sys::PrintStackTraceOnErrorSignal();
69 llvm::PrettyStackTraceProgram X(argc, argv);
70 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner670406d2003-10-18 21:55:35 +000071 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +000072 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattner3a4baf12009-02-07 18:56:30 +000073 "llvm.org/cmds/bugpoint.html"
Chris Lattner670406d2003-10-18 21:55:35 +000074 " for more information.\n");
Chris Lattnerf9aaae02005-08-02 02:16:17 +000075 sys::SetInterruptFunction(BugpointInterruptFunction);
76
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000077 BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit);
Chris Lattnerafade922002-11-20 22:28:10 +000078 if (D.addSources(InputFilenames)) return 1;
79 D.addPasses(PassList.begin(), PassList.end());
80
Misha Brukman67b36e42003-09-12 20:42:57 +000081 // Bugpoint has the ability of generating a plethora of core files, so to
Reid Spencer551ccae2004-09-01 22:55:40 +000082 // avoid filling up the disk, we prevent it
Reid Spencer51163302004-12-27 06:18:02 +000083 sys::Process::PreventCoreFiles();
Misha Brukman67b36e42003-09-12 20:42:57 +000084
Chris Lattner74d45272004-02-18 17:32:54 +000085 try {
86 return D.run();
Chris Lattner230fef82004-02-18 20:22:11 +000087 } catch (ToolExecutionError &TEE) {
Misha Brukmaneed80e22004-07-23 01:30:49 +000088 std::cerr << "Tool execution error: " << TEE.what() << '\n';
Reid Spencer1ef8bda2004-12-30 05:36:08 +000089 } catch (const std::string& msg) {
90 std::cerr << argv[0] << ": " << msg << "\n";
Dan Gohman03242052009-04-27 01:30:37 +000091 } catch (const std::bad_alloc &e) {
92 std::cerr << "Oh no, a bugpoint process ran out of memory!\n"
93 "To increase the allocation limits for bugpoint child\n"
94 "processes, use the -mlimit option.\n";
95 } catch (const std::exception &e) {
96 std::cerr << "Whoops, a std::exception leaked out of bugpoint: "
97 << e.what() << "\n"
98 << "This is a bug in bugpoint!\n";
Chris Lattner74d45272004-02-18 17:32:54 +000099 } catch (...) {
100 std::cerr << "Whoops, an exception leaked out of bugpoint. "
101 << "This is a bug in bugpoint!\n";
Chris Lattner74d45272004-02-18 17:32:54 +0000102 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000103 return 1;
Chris Lattnerafade922002-11-20 22:28:10 +0000104}