blob: 89ff7a66ff229d5278950cf6ee8fce1b8a672916 [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"
Owen Anderson25209b42009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Support/PassNameParser.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/ManagedStatic.h"
23#include "llvm/Support/PluginLoader.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000024#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/System/Process.h"
26#include "llvm/System/Signals.h"
27#include "llvm/LinkAllVMCore.h"
28#include <iostream>
29using namespace llvm;
30
31// AsChild - Specifies that this invocation of bugpoint is being generated
32// from a parent process. It is not intended to be used by users so the
33// option is hidden.
34static cl::opt<bool>
35AsChild("as-child", cl::desc("Run bugpoint as child process"),
36 cl::ReallyHidden);
37
38static cl::opt<bool>
Dan Gohmand56bb2f2008-02-18 17:15:45 +000039FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 "on program to find bugs"), cl::init(false));
41
42static cl::list<std::string>
43InputFilenames(cl::Positional, cl::OneOrMore,
44 cl::desc("<input llvm ll/bc files>"));
45
46static cl::opt<unsigned>
47TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
48 cl::desc("Number of seconds program is allowed to run before it "
49 "is killed (default is 300s), 0 disables timeout"));
50
51static cl::opt<unsigned>
52MemoryLimit("mlimit", cl::init(100), cl::value_desc("MBytes"),
53 cl::desc("Maximum amount of memory to use. 0 disables check."));
54
55// The AnalysesList is automatically populated with registered Passes by the
56// PassNameParser.
57//
58static cl::list<const PassInfo*, bool, PassNameParser>
59PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
60
61/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
62bool llvm::BugpointIsInterrupted = false;
63
64static void BugpointInterruptFunction() {
65 BugpointIsInterrupted = true;
66}
67
68int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000069 llvm::sys::PrintStackTraceOnErrorSignal();
70 llvm::PrettyStackTraceProgram X(argc, argv);
71 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +000073 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattnerc95317e2009-02-07 18:56:30 +000074 "llvm.org/cmds/bugpoint.html"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 " for more information.\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 sys::SetInterruptFunction(BugpointInterruptFunction);
Owen Anderson25209b42009-07-01 16:58:40 +000077
Owen Andersone84b8b32009-07-15 22:16:10 +000078 LLVMContext& Context = getGlobalContext();
Owen Andersona148fdd2009-07-01 21:22:36 +000079 BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit, Context);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 if (D.addSources(InputFilenames)) return 1;
81 D.addPasses(PassList.begin(), PassList.end());
82
83 // Bugpoint has the ability of generating a plethora of core files, so to
84 // avoid filling up the disk, we prevent it
85 sys::Process::PreventCoreFiles();
86
87 try {
88 return D.run();
89 } catch (ToolExecutionError &TEE) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000090 errs() << "Tool execution error: " << TEE.what() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 } catch (const std::string& msg) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000092 errs() << argv[0] << ": " << msg << "\n";
Dan Gohman27679392009-04-27 01:30:37 +000093 } catch (const std::bad_alloc &e) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000094 errs() << "Oh no, a bugpoint process ran out of memory!\n"
95 "To increase the allocation limits for bugpoint child\n"
96 "processes, use the -mlimit option.\n";
Dan Gohman27679392009-04-27 01:30:37 +000097 } catch (const std::exception &e) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000098 errs() << "Whoops, a std::exception leaked out of bugpoint: "
99 << e.what() << "\n"
100 << "This is a bug in bugpoint!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 } catch (...) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000102 errs() << "Whoops, an exception leaked out of bugpoint. "
103 << "This is a bug in bugpoint!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 }
105 return 1;
106}