blob: 20f0e99a848881b40180c27afef40bb912d05ce5 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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"
17#include "ToolRunner.h"
18#include "llvm/LinkAllPasses.h"
19#include "llvm/Support/PassNameParser.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/PluginLoader.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000023#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/System/Process.h"
25#include "llvm/System/Signals.h"
26#include "llvm/LinkAllVMCore.h"
27#include <iostream>
28using namespace llvm;
29
30// 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>
34AsChild("as-child", cl::desc("Run bugpoint as child process"),
35 cl::ReallyHidden);
36
37static cl::opt<bool>
Dan Gohmand56bb2f2008-02-18 17:15:45 +000038FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 "on program to find bugs"), cl::init(false));
40
41static cl::list<std::string>
42InputFilenames(cl::Positional, cl::OneOrMore,
43 cl::desc("<input llvm ll/bc files>"));
44
45static 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
50static 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
54// The AnalysesList is automatically populated with registered Passes by the
55// PassNameParser.
56//
57static cl::list<const PassInfo*, bool, PassNameParser>
58PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
59
60/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
61bool llvm::BugpointIsInterrupted = false;
62
63static void BugpointInterruptFunction() {
64 BugpointIsInterrupted = true;
65}
66
67int main(int argc, char **argv) {
Chris Lattnere6012df2009-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +000072 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattnerc95317e2009-02-07 18:56:30 +000073 "llvm.org/cmds/bugpoint.html"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 " for more information.\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 sys::SetInterruptFunction(BugpointInterruptFunction);
76
77 BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit);
78 if (D.addSources(InputFilenames)) return 1;
79 D.addPasses(PassList.begin(), PassList.end());
80
81 // Bugpoint has the ability of generating a plethora of core files, so to
82 // avoid filling up the disk, we prevent it
83 sys::Process::PreventCoreFiles();
84
85 try {
86 return D.run();
87 } catch (ToolExecutionError &TEE) {
88 std::cerr << "Tool execution error: " << TEE.what() << '\n';
89 } catch (const std::string& msg) {
90 std::cerr << argv[0] << ": " << msg << "\n";
Dan Gohman27679392009-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";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 } catch (...) {
100 std::cerr << "Whoops, an exception leaked out of bugpoint. "
101 << "This is a bug in bugpoint!\n";
102 }
103 return 1;
104}