Skip to content

Commit

Permalink
Merge branch 'dtrace'
Browse files Browse the repository at this point in the history
  • Loading branch information
bkidney committed Mar 1, 2019
2 parents a3427f4 + eea8d98 commit 587ce43
Show file tree
Hide file tree
Showing 20 changed files with 505 additions and 60 deletions.
2 changes: 1 addition & 1 deletion scripts/loom-fbsdmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/sh -x

. `dirname $0`/xtools.sh

Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(FILES
DebugInfo
DTraceLogger
Instrumentation
Instrumenter
InstrStrategy
Expand All @@ -11,6 +12,7 @@ set(FILES
PolicyFile
Serializer
Strings
Transform
)

foreach(base IN LISTS FILES)
Expand Down
120 changes: 120 additions & 0 deletions src/DTraceLogger.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//! @file DTraceLogger.cc Definition of @ref DTraceLogger.
/*
* Copyright (c) 2017 Brian Kidney
* All rights reserved.
*
* This software was developed by BAE Systems, the University of Cambridge
* Computer Laboratory, and Memorial University under DARPA/AFRL contract
* FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
* (TC) research program.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include "DTraceLogger.hh"

#include <algorithm>

#include <llvm/IR/Instructions.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/TypeBuilder.h>
#include <llvm/Support/raw_ostream.h>

using namespace loom;
using namespace llvm;
using std::vector;

DTraceLogger::DTraceLogger(llvm::Module& Mod)
: Logger(Mod){ };

Value* DTraceLogger::ConvertValueToPtr(IRBuilder<>& B, LLVMContext& Ctx, Value* V, Type* param_t)
{
Type *T = V->getType();
if (T->isPointerTy()) {
return B.CreatePtrToInt(V, param_t);
} else if (T->isIntegerTy()) {
return B.CreateSExt(V, param_t);
} else if (T->isDoubleTy()) {
return B.CreateBitCast(V, TypeBuilder<int64_t, false>::get(Ctx));
} else if (T->isFloatTy()) {
auto *BC = B.CreateBitCast(V, TypeBuilder<int32_t, false>::get(Ctx));
return B.CreateSExt(BC, param_t);
}
return ConstantInt::get(param_t, 0);
}



Value* DTraceLogger::Log(Instruction *I, ArrayRef<Value*> Values,
StringRef Name, StringRef Descrip,
Metadata Metadata, std::vector<Transform> Transforms,
bool /* SuppressUniqueness */) {

if (not Metadata.isValid())
errs() << "Warning: Dtrace Logger requires the use of Metadata to function properly.";

IRBuilder<> B(I);

LLVMContext &Ctx = Mod.getContext();
Type* id_t = TypeBuilder<unsigned int, false>::get(Ctx);
Type* param_t = TypeBuilder<uintptr_t, false>::get(Ctx);

size_t n_args = std::min(Values.size(), 5ul);

std::vector<Type*> params;
for (int i = 0; i < n_args + 1; i++) {
params.push_back(param_t);
}

auto *FT = FunctionType::get( TypeBuilder<int, false>::get(Ctx), params, false);

Constant *F = Mod.getOrInsertFunction("dt_probe", FT);


Value* args[6];
args[0] = B.CreateSExt(ConstantInt::get(id_t, Metadata.Id), param_t);;
for (int i = 0; i < n_args; i++)
{
Value *ptr;

Transform Tf;
for (auto t: Transforms) {
if (t.Arg == i)
Tf = t;
break;
}

if (Tf.isValid()) {
ptr = ConvertValueToPtr(B, Ctx, Tf.CreateTransform(I, Mod, Values[i]), param_t);
} else {
ptr = ConvertValueToPtr(B, Ctx, Values[i], param_t);
}

args[i + 1] = ptr;
}

ArrayRef<Value*> argsRef(args, n_args + 1);

return B.CreateCall(F, argsRef);

}

63 changes: 63 additions & 0 deletions src/DTraceLogger.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! @file DTraceLogger.hh Declaration of @ref DTraceLogger.
/*
* Copyright (c) 2017 Brian Kidney
* All rights reserved.
*
* This software was developed by BAE Systems, the University of Cambridge
* Computer Laboratory, and Memorial University under DARPA/AFRL contract
* FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
* (TC) research program.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#ifndef DTRACE_LOGGER_H_
#define DTRACE_LOGGER_H_

#include "Logger.hh"

namespace loom {

class Serializer;

/**
* A logging technique that writes values to the DTrace framework using
* Userland Statically Defined Traces (USDT). libusdt is used to create probes
* at runtime.
*/
class DTraceLogger : public loom::Logger {
public:
DTraceLogger(llvm::Module& Mod);


virtual llvm::Value* Log(llvm::Instruction *I, llvm::ArrayRef<llvm::Value*> Values,
llvm::StringRef Name, llvm::StringRef Descrip,
Metadata Metadata, std::vector<Transform> Transforms,
bool /* SuppressUniqueness */) override;
private:
llvm::Value* ConvertValueToPtr(llvm::IRBuilder<>&, llvm::LLVMContext&, llvm::Value*, llvm::Type*);

};

} // namespace loom

#endif // !DTRACE_LOGGER_H_
23 changes: 13 additions & 10 deletions src/InstrStrategy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

#include "InstrStrategy.hh"
#include "Instrumentation.hh"
#include "Metadata.hh"

using namespace llvm;
using namespace loom;
Expand All @@ -58,7 +57,8 @@ class CalloutStrategy : public InstrStrategy {
Instrumentation Instrument(Instruction *I, StringRef Name, StringRef Descrip,
ArrayRef<Parameter> Params,
ArrayRef<Value *> Values, loom::Metadata Md,
bool VarArgs, bool AfterInst, bool) override;
std::vector<loom::Transform> Transforms, bool VarArgs,
bool AfterInst, bool) override;
};

class InlineStrategy : public InstrStrategy {
Expand All @@ -68,8 +68,8 @@ class InlineStrategy : public InstrStrategy {
Instrumentation Instrument(Instruction *I, StringRef Name, StringRef Descrip,
ArrayRef<Parameter> Params,
ArrayRef<Value *> Values, loom::Metadata Md,
bool VarArgs, bool AfterInst,
bool SuppressInstrumentation) override;
std::vector<loom::Transform> Transforms, bool VarArgs,
bool AfterInst, bool SuppressInstrumentation) override;
};

} // anonymous namespace
Expand All @@ -93,12 +93,13 @@ void InstrStrategy::AddLogger(unique_ptr<Logger> L) {

Value *InstrStrategy::AddLogging(Instruction *I, ArrayRef<Value *> Values,
StringRef Name, StringRef Description,
loom::Metadata Md, bool SuppressUniqueness) {
loom::Metadata Md, std::vector<loom::Transform> Transforms,
bool SuppressUniqueness) {
Value *End = nullptr;

for (auto &L : Loggers) {
assert(L);
End = L->Log(I, Values, Name, Description, Md, SuppressUniqueness);
End = L->Log(I, Values, Name, Description, Md, Transforms, SuppressUniqueness);
}

return End;
Expand All @@ -108,7 +109,8 @@ Instrumentation CalloutStrategy::Instrument(Instruction *I, StringRef Name,
StringRef Descrip,
ArrayRef<Parameter> Params,
ArrayRef<Value *> Values,
loom::Metadata Md, bool VarArgs,
loom::Metadata Md,
std::vector<loom::Transform> Transforms, bool VarArgs,
bool AfterInst, bool SuppressUniq) {

Module *M = I->getModule();
Expand Down Expand Up @@ -158,7 +160,7 @@ Instrumentation CalloutStrategy::Instrument(Instruction *I, StringRef Name,
End = PreambleEnd;
}

AddLogging(PreambleEnd, InstrValues, Name, Descrip, Md, SuppressUniq);
AddLogging(PreambleEnd, InstrValues, Name, Descrip, Md, Transforms, SuppressUniq);

// Also set instrumentation function's parameter names:
size_t i = 0;
Expand Down Expand Up @@ -192,7 +194,8 @@ Instrumentation InlineStrategy::Instrument(Instruction *I, StringRef Name,
StringRef Descrip,
ArrayRef<Parameter> Params,
ArrayRef<Value *> Values,
loom::Metadata Md, bool VarArgs,
loom::Metadata Md,
std::vector<loom::Transform> Transforms, bool VarArgs,
bool AfterInst, bool SuppressUniq) {

BasicBlock *BB = I->getParent();
Expand Down Expand Up @@ -238,7 +241,7 @@ Instrumentation InlineStrategy::Instrument(Instruction *I, StringRef Name,
End = I;
}

AddLogging(PreambleEnd, Values, Name, Descrip, Md, SuppressUniq);
AddLogging(PreambleEnd, Values, Name, Descrip, Md, Transforms, SuppressUniq);

SmallVector<Value *, 4> V(Values.begin(), Values.end());

Expand Down
5 changes: 2 additions & 3 deletions src/InstrStrategy.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

#include "IRUtils.hh"
#include "Logger.hh"
#include "Metadata.hh"

namespace llvm {
class Instruction;
Expand Down Expand Up @@ -127,7 +126,7 @@ public:
Instrument(llvm::Instruction *I, llvm::StringRef Name,
llvm::StringRef Description, llvm::ArrayRef<Parameter> Params,
llvm::ArrayRef<llvm::Value *> Values,
Metadata Md, bool VarArgs = false,
Metadata Md, std::vector<Transform> Tf, bool VarArgs = false,
bool AfterInst = false, bool SuppressUniqueness = false) = 0;

bool Initialize(llvm::Function &main);
Expand All @@ -143,7 +142,7 @@ protected:
*/
llvm::Value *AddLogging(llvm::Instruction *I, llvm::ArrayRef<llvm::Value *>,
llvm::StringRef Name, llvm::StringRef Description,
Metadata Md, bool SuppressUniqueness);
Metadata Md, std::vector<Transform> Tf, bool SuppressUniqueness);

/**
* Use an explicit structure of premable/instrumentation/end BasicBlocks
Expand Down
Loading

0 comments on commit 587ce43

Please sign in to comment.