Skip to content

Commit

Permalink
Update utils.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
NaC-L committed Oct 23, 2024
1 parent 54ca7e8 commit 2395ac0
Showing 1 changed file with 67 additions and 41 deletions.
108 changes: 67 additions & 41 deletions lifter/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <llvm/Analysis/ValueLattice.h>
#include <llvm/Support/KnownBits.h>
#include <map>
#include <z3++.h>

namespace FileHelper {

Expand Down Expand Up @@ -81,30 +80,57 @@ namespace debugging {
int ic = 1;
int increaseInstCounter() { return ++ic; }
bool shouldDebug = false;
void enableDebug() {
shouldDebug = 1;
cout << "Debugging enabled\n";
llvm::raw_ostream* debugStream = nullptr;
std::unique_ptr<llvm::raw_fd_ostream> fileStream;

void enableDebug(const std::string& filename = "") {
shouldDebug = true;
if (!filename.empty()) {
std::error_code EC;
fileStream = std::make_unique<llvm::raw_fd_ostream>(filename, EC);
if (EC) {
llvm::errs() << "Error opening debug file: " << EC.message() << "\n";
fileStream.reset();
debugStream = &llvm::errs();
shouldDebug = false;
return;
}
debugStream = fileStream.get();
} else {
debugStream = &llvm::outs();
}
llvm::outs() << "Debugging enabled\n";
}

void printLLVMValue(llvm::Value* v, const char* name) {
if (!shouldDebug)
if (!shouldDebug || !debugStream)
return;
outs() << " " << name << " : ";
v->print(outs());
outs() << "\n";
outs().flush();
*debugStream << " " << name << " : ";
v->print(*debugStream);
*debugStream << "\n";
debugStream->flush();
}
void doIfDebug(const std::function<void(void)>& dothis) {
if (!shouldDebug)

// Other functions remain the same, but use debugStream instead of
// llvm::outs() For example:
template <typename T> void printValue(const T& v, const char* name) {
if (!shouldDebug || !debugStream)
return;
(dothis)();
if constexpr (std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t>) {
*debugStream << " " << name << " : " << static_cast<int>(v) << "\n";
debugStream->flush();
return;
}
else
*debugStream << " " << name << " : " << v << "\n";
debugStream->flush();
}
template <typename T> void printValue(const T& v, const char* name) {

void doIfDebug(const std::function<void(void)>& dothis) {
if (!shouldDebug)
return;
outs() << " " << name << " : " << v << "\n";
outs().flush();
(dothis)();
}

template void printValue<uint64_t>(const uint64_t& v, const char* name);
template void printValue<uint32_t>(const uint32_t& v, const char* name);
template void printValue<uint16_t>(const uint16_t& v, const char* name);
Expand All @@ -114,14 +140,20 @@ namespace debugging {
template void printValue<int16_t>(const int16_t& v, const char* name);
template void printValue<int8_t>(const int8_t& v, const char* name);
template void printValue<bool>(const bool& v, const char* name);
template void printValue<ValueLatticeElement>(const ValueLatticeElement& v,
template void printValue<std::string>(const std::string& v, const char* name);
template void printValue<char*>(char* const& v, const char* name);
template void printValue<char[256]>(char const (&)[256], const char* name);
template void
printValue<llvm::FormattedNumber>(llvm::FormattedNumber const(&),
const char* name);
template void
printValue<llvm::ValueLatticeElement>(const llvm::ValueLatticeElement& v,
const char* name);
template void printValue<llvm::KnownBits>(const llvm::KnownBits& v,
const char* name);
template void printValue<llvm::APInt>(const llvm::APInt& v, const char* name);
template void printValue<llvm::ConstantRange>(const llvm::ConstantRange& v,
const char* name);
template void printValue<KnownBits>(const KnownBits& v, const char* name);
template void printValue<APInt>(const APInt& v, const char* name);
template void printValue<ROP_info>(const ROP_info& v, const char* name);
template void printValue<ConstantRange>(const ConstantRange& v,
const char* name);

} // namespace debugging

namespace argparser {
Expand All @@ -132,8 +164,8 @@ namespace argparser {
}

std::map<std::string, std::function<void()>> options = {
{"-d", debugging::enableDebug},
{"--enable-debug", debugging::enableDebug},
{"-d", []() { debugging::enableDebug("debug.txt"); }},
//
{"-h", printHelp}};

void parseArguments(std::vector<std::string>& args) {
Expand All @@ -160,7 +192,6 @@ namespace timer {
using duration = std::chrono::duration<double, std::milli>;

time_point startTime;
duration elapsedTime{0};
bool running = false;

void startTimer() {
Expand All @@ -169,29 +200,24 @@ namespace timer {
}

double getTimer() {
elapsedTime += clock::now() - startTime;
return elapsedTime.count();
}

double stopTimer() {
if (running) {
elapsedTime += clock::now() - startTime;
running = false;
return std::chrono::duration_cast<duration>(clock::now() - startTime)
.count();
}
return elapsedTime.count();
return 0.0;
}

void suspendTimer() {
double stopTimer() {
if (running) {
elapsedTime += clock::now() - startTime;
running = false;
return std::chrono::duration_cast<duration>(clock::now() - startTime)
.count();
}
return 0.0;
}

void resumeTimer() {
if (!running) {
startTime = clock::now();
running = true;
}
void resetTimer() {
startTime = clock::now();
running = true;
}
} // namespace timer

0 comments on commit 2395ac0

Please sign in to comment.