Skip to content

Commit

Permalink
Add a call to 'write' right before the call to abort() in the unwind …
Browse files Browse the repository at this point in the history
…path.

This causes the JIT, or LLC'd program to print out a nice message, explaining
WHY the program aborted.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11184 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
lattner committed Feb 8, 2004
1 parent 973e3ec commit e1c0930
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/Transforms/Utils/LowerInvoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Scalar.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/iTerminators.h"
#include "llvm/iOther.h"
#include "llvm/Module.h"
#include "llvm/Type.h"
#include "llvm/Constant.h"
#include "Support/Statistic.h"
using namespace llvm;

namespace {
Statistic<> NumLowered("lowerinvoke", "Number of invoke & unwinds replaced");

class LowerInvoke : public FunctionPass {
Value *AbortMessage;
unsigned AbortMessageLength;
Function *WriteFn;
Function *AbortFn;
public:
bool doInitialization(Module &M);
Expand All @@ -44,7 +47,21 @@ FunctionPass *llvm::createLowerInvokePass() { return new LowerInvoke(); }
// doInitialization - Make sure that there is a prototype for abort in the
// current module.
bool LowerInvoke::doInitialization(Module &M) {
Constant *Msg =
ConstantArray::get("Exception handler needed, but not available.\n");
AbortMessageLength = Msg->getNumOperands()-1; // don't include \0

GlobalVariable *MsgGV =
new GlobalVariable(Msg->getType(), true, GlobalValue::InternalLinkage, Msg,
"", &M);
std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::LongTy));
AbortMessage =
ConstantExpr::getGetElementPtr(ConstantPointerRef::get(MsgGV), GEPIdx);

AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, 0);
WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::IntTy,
PointerType::get(Type::SByteTy), Type::IntTy,
0);
return true;
}

Expand All @@ -70,6 +87,13 @@ bool LowerInvoke::runOnFunction(Function &F) {

++NumLowered; Changed = true;
} else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
// Insert a new call to write(2, AbortMessage, AbortMessageLength);
std::vector<Value*> Args;
Args.push_back(ConstantInt::get(Type::IntTy, 2));
Args.push_back(AbortMessage);
Args.push_back(ConstantInt::get(Type::IntTy, AbortMessageLength));
new CallInst(WriteFn, Args, "", UI);

// Insert a call to abort()
new CallInst(AbortFn, std::vector<Value*>(), "", UI);

Expand Down

0 comments on commit e1c0930

Please sign in to comment.