Skip to content

Commit

Permalink
Accept only true values for CLING_PROFILE and CLING_DEBUG
Browse files Browse the repository at this point in the history
This implementation of ConvertEnvValueToBool uses const char* as
input intentionally in order to be able to accept nullptr for when
environment variables are not set, but also because using something
like std::string would require bringing in several extra headers.
  • Loading branch information
amadio committed Nov 10, 2022
1 parent acc8412 commit a8e50f8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
38 changes: 38 additions & 0 deletions interpreter/cling/include/cling/Utils/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//--------------------------------------------------------------------*- C++ -*-
// CLING - the C++ LLVM-based InterpreterG :)
// author: Guilherme Amadio <amadio@cern.ch>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------

#include <cstdlib>
#include <cstring>

#ifndef CLING_UTILS_UTILS_H
#define CLING_UTILS_UTILS_H

#if defined(_MSC_VER) && !defined(strcasecmp)
#define strcasecmp _stricmp
#endif

namespace cling {
namespace utils {
/** Convert @p value to boolean */
static inline bool ConvertEnvValueToBool(const char* value) {
const char* true_strs[] = {"1", "true", "on", "yes"};

if (!value)
return false;

for (auto str : true_strs)
if (strcasecmp(value, str) == 0)
return true;

return false;
}
} // namespace utils
} // namespace cling

#endif // CLING_UTILS_UTILS_H
12 changes: 9 additions & 3 deletions interpreter/cling/lib/Interpreter/CIFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "cling/Utils/Output.h"
#include "cling/Utils/Paths.h"
#include "cling/Utils/Platform.h"
#include "cling/Utils/Utils.h"

#include "clang/AST/ASTContext.h"
#include "clang/Basic/TargetInfo.h"
Expand Down Expand Up @@ -1243,6 +1244,11 @@ namespace {
std::vector<const char*> argvCompile(argv, argv+1);
argvCompile.reserve(argc+32);

bool debuggingEnabled =
cling::utils::ConvertEnvValueToBool(std::getenv("CLING_DEBUG"));
bool profilingEnabled =
cling::utils::ConvertEnvValueToBool(std::getenv("CLING_PROFILE"));

#if __APPLE__ && __arm64__
argvCompile.push_back("--target=arm64-apple-darwin20.3.0");
#endif
Expand Down Expand Up @@ -1325,12 +1331,12 @@ namespace {

#ifdef __linux__
// Keep frame pointer to make JIT stack unwinding reliable for profiling
if (std::getenv("CLING_PROFILE"))
if (profilingEnabled)
argvCompile.push_back("-fno-omit-frame-pointer");
#endif

// Disable optimizations and keep frame pointer when debugging
if (std::getenv("CLING_DEBUG"))
if (debuggingEnabled)
argvCompile.push_back("-O0 -fno-omit-frame-pointer");

// argv[0] already inserted, get the rest
Expand Down Expand Up @@ -1672,7 +1678,7 @@ namespace {
CGOpts.setInlining(CodeGenOptions::NormalInlining);

// Add debugging info when debugging or profiling
if (std::getenv("CLING_DEBUG") || std::getenv("CLING_PROFILE"))
if (debuggingEnabled || profilingEnabled)
CGOpts.setDebugInfo(clang::codegenoptions::FullDebugInfo);

// CGOpts.EmitDeclMetadata = 1; // For unloading, for later
Expand Down
5 changes: 3 additions & 2 deletions interpreter/cling/lib/Interpreter/IncrementalJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "IncrementalExecutor.h"
#include "cling/Utils/Platform.h"
#include "cling/Utils/Utils.h"

#include "llvm/ExecutionEngine/Orc/Legacy.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Expand Down Expand Up @@ -359,12 +360,12 @@ IncrementalJIT::IncrementalJIT(IncrementalExecutor& exe,
llvm::sys::DynamicLibrary::LoadLibraryPermanently(0, 0);

// Enable GDB JIT listener for debugging if CLING_DEBUG is set
if (std::getenv("CLING_DEBUG"))
if (cling::utils::ConvertEnvValueToBool(std::getenv("CLING_DEBUG")))
m_Listeners.push_back(JITEventListener::createGDBRegistrationListener());

#ifdef __linux__
// Enable perf profiling of JITted code on Linux if CLING_PROFILE is set
if (std::getenv("CLING_PROFILE"))
if (cling::utils::ConvertEnvValueToBool(std::getenv("CLING_PROFILE")))
m_Listeners.push_back(cling::createPerfJITEventListener());
#endif

Expand Down

0 comments on commit a8e50f8

Please sign in to comment.