Skip to content

Commit

Permalink
Get rid of Error::PutToLog().
Browse files Browse the repository at this point in the history
Instead just rely on LLDB_LOG().

This is part of an effort to sort out dependency hell in LLDB.
Error is in Utility, but Log is in Core.  Core can depend on
Utility, but not vice versa.  So this patch moves the knowledge
about how to log Errors from the Error class to the Log file.

Differential Revision: https://reviews.llvm.org/D29514

llvm-svn: 294210
  • Loading branch information
Zachary Turner committed Feb 6, 2017
1 parent 3c74c68 commit 33aba3c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 168 deletions.
50 changes: 2 additions & 48 deletions lldb/include/lldb/Utility/Error.h
Expand Up @@ -12,6 +12,7 @@
#if defined(__cplusplus)

#include "llvm/Support/DataTypes.h"
#include "llvm/Support/FormatAdapters.h"
#include "llvm/Support/FormatVariadic.h"

#include <cstdarg>
Expand All @@ -24,8 +25,6 @@

namespace lldb_private {

class Log;

//----------------------------------------------------------------------
/// @class Error Error.h "lldb/Utility/Error.h"
/// @brief An error handling class.
Expand Down Expand Up @@ -147,48 +146,6 @@ class Error {
//------------------------------------------------------------------
lldb::ErrorType GetType() const;

//------------------------------------------------------------------
/// Log an error to Log().
///
/// Log the error given a formatted string \a format. If the this
/// object contains an error code, update the error string to
/// contain the prefix "error: ", followed by the formatted string,
/// followed by the error value and any string that describes the
/// error value. This allows more context to be given to an error
/// string that remains cached in this object. Logging always occurs
/// even when the error code contains a non-error value.
///
/// @param[in] format
/// A printf style format string.
///
/// @param[in] ...
/// Variable arguments that are needed for the printf style
/// format string \a format.
//------------------------------------------------------------------
void PutToLog(Log *log, const char *format, ...)
__attribute__((format(printf, 3, 4)));

//------------------------------------------------------------------
/// Log an error to Log() if the error value is an error.
///
/// Log the error given a formatted string \a format only if the
/// error value in this object describes an error condition. If the
/// this object contains an error, update the error string to
/// contain the prefix "error: " followed by the formatted string,
/// followed by the error value and any string that describes the
/// error value. This allows more context to be given to an error
/// string that remains cached in this object.
///
/// @param[in] format
/// A printf style format string.
///
/// @param[in] ...
/// Variable arguments that are needed for the printf style
/// format string \a format.
//------------------------------------------------------------------
void LogIfError(Log *log, const char *format, ...)
__attribute__((format(printf, 3, 4)));

//------------------------------------------------------------------
/// Set accessor from a kern_return_t.
///
Expand Down Expand Up @@ -304,10 +261,7 @@ class Error {
namespace llvm {
template <> struct format_provider<lldb_private::Error> {
static void format(const lldb_private::Error &error, llvm::raw_ostream &OS,
llvm::StringRef Options) {
llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
Options);
}
llvm::StringRef Options);
};
}

Expand Down
14 changes: 6 additions & 8 deletions lldb/source/Core/Communication.cpp
Expand Up @@ -322,10 +322,9 @@ lldb::thread_result_t Communication::ReadThread(lldb::thread_arg_t p) {
comm->Disconnect();
done = true;
}
if (log)
error.LogIfError(
log, "%p Communication::ReadFromConnection () => status = %s", p,
Communication::ConnectionStatusAsCString(status));
if (error.Fail())
LLDB_LOG(log, "error: {0}, status = {1}", error,
Communication::ConnectionStatusAsCString(status));
break;
case eConnectionStatusInterrupted: // Synchronization signal from
// SynchronizeWithReadThread()
Expand All @@ -340,10 +339,9 @@ lldb::thread_result_t Communication::ReadThread(lldb::thread_arg_t p) {
done = true;
LLVM_FALLTHROUGH;
case eConnectionStatusTimedOut: // Request timed out
if (log)
error.LogIfError(
log, "%p Communication::ReadFromConnection () => status = %s", p,
Communication::ConnectionStatusAsCString(status));
if (error.Fail())
LLDB_LOG(log, "error: {0}, status = {1}", error,
Communication::ConnectionStatusAsCString(status));
break;
}
}
Expand Down
86 changes: 41 additions & 45 deletions lldb/source/Host/common/Host.cpp
Expand Up @@ -685,10 +685,10 @@ Error Host::LaunchProcessPosixSpawn(const char *exe_path,
posix_spawnattr_t attr;
error.SetError(::posix_spawnattr_init(&attr), eErrorTypePOSIX);

if (error.Fail() || log)
error.PutToLog(log, "::posix_spawnattr_init ( &attr )");
if (error.Fail())
if (error.Fail()) {
LLDB_LOG(log, "error: {0}, ::posix_spawnattr_init ( &attr )", error);
return error;
}

// Make a quick class that will cleanup the posix spawn attributes in case
// we return in the middle of this function.
Expand All @@ -709,11 +709,12 @@ Error Host::LaunchProcessPosixSpawn(const char *exe_path,
short flags = GetPosixspawnFlags(launch_info);

error.SetError(::posix_spawnattr_setflags(&attr, flags), eErrorTypePOSIX);
if (error.Fail() || log)
error.PutToLog(log, "::posix_spawnattr_setflags ( &attr, flags=0x%8.8x )",
flags);
if (error.Fail())
if (error.Fail()) {
LLDB_LOG(log,
"error: {0}, ::posix_spawnattr_setflags ( &attr, flags={1:x} )",
error, flags);
return error;
}

// posix_spawnattr_setbinpref_np appears to be an Apple extension per:
// http://www.unix.com/man-page/OSX/3/posix_spawnattr_setbinpref_np/
Expand All @@ -740,10 +741,10 @@ Error Host::LaunchProcessPosixSpawn(const char *exe_path,
size_t ocount = 0;
error.SetError(::posix_spawnattr_setbinpref_np(&attr, 1, &cpu, &ocount),
eErrorTypePOSIX);
if (error.Fail() || log)
error.PutToLog(log, "::posix_spawnattr_setbinpref_np ( &attr, 1, "
"cpu_type = 0x%8.8x, count => %llu )",
cpu, (uint64_t)ocount);
if (error.Fail())
LLDB_LOG(log, "error: {0}, ::posix_spawnattr_setbinpref_np ( &attr, 1, "
"cpu_type = {1:x}, count => {2} )",
error, cpu, ocount);

if (error.Fail() || ocount != 1)
return error;
Expand Down Expand Up @@ -813,10 +814,12 @@ Error Host::LaunchProcessPosixSpawn(const char *exe_path,
posix_spawn_file_actions_t file_actions;
error.SetError(::posix_spawn_file_actions_init(&file_actions),
eErrorTypePOSIX);
if (error.Fail() || log)
error.PutToLog(log, "::posix_spawn_file_actions_init ( &file_actions )");
if (error.Fail())
if (error.Fail()) {
LLDB_LOG(log,
"error: {0}, ::posix_spawn_file_actions_init ( &file_actions )",
error);
return error;
}

// Make a quick class that will cleanup the posix spawn attributes in case
// we return in the middle of this function.
Expand All @@ -838,16 +841,14 @@ Error Host::LaunchProcessPosixSpawn(const char *exe_path,
::posix_spawnp(&result_pid, exe_path, &file_actions, &attr, argv, envp),
eErrorTypePOSIX);

if (error.Fail() || log) {
error.PutToLog(
log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, "
"attr = %p, argv = %p, envp = %p )",
result_pid, exe_path, static_cast<void *>(&file_actions),
static_cast<void *>(&attr), reinterpret_cast<const void *>(argv),
reinterpret_cast<const void *>(envp));
if (error.Fail()) {
LLDB_LOG(log, "error: {0}, ::posix_spawnp(pid => {1}, path = '{2}', "
"file_actions = {3}, "
"attr = {4}, argv = {5}, envp = {6} )",
error, result_pid, exe_path, &file_actions, &attr, argv, envp);
if (log) {
for (int ii = 0; argv[ii]; ++ii)
log->Printf("argv[%i] = '%s'", ii, argv[ii]);
LLDB_LOG(log, "argv[{0}] = '{1}'", ii, argv[ii]);
}
}

Expand All @@ -856,16 +857,13 @@ Error Host::LaunchProcessPosixSpawn(const char *exe_path,
::posix_spawnp(&result_pid, exe_path, NULL, &attr, argv, envp),
eErrorTypePOSIX);

if (error.Fail() || log) {
error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', "
"file_actions = NULL, attr = %p, argv = %p, envp = "
"%p )",
result_pid, exe_path, static_cast<void *>(&attr),
reinterpret_cast<const void *>(argv),
reinterpret_cast<const void *>(envp));
if (error.Fail()) {
LLDB_LOG(log, "error: {0}, ::posix_spawnp ( pid => {1}, path = '{2}', "
"file_actions = NULL, attr = {3}, argv = {4}, envp = {5} )",
error, result_pid, exe_path, &attr, argv, envp);
if (log) {
for (int ii = 0; argv[ii]; ++ii)
log->Printf("argv[%i] = '%s'", ii, argv[ii]);
LLDB_LOG("argv[{0}] = '{1}'", ii, argv[ii]);
}
}
}
Expand Down Expand Up @@ -908,10 +906,10 @@ bool Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info,
error.SetError(
::posix_spawn_file_actions_addclose(file_actions, info->GetFD()),
eErrorTypePOSIX);
if (log && (error.Fail() || log))
error.PutToLog(log,
"posix_spawn_file_actions_addclose (action=%p, fd=%i)",
static_cast<void *>(file_actions), info->GetFD());
if (error.Fail())
LLDB_LOG(log, "error: {0}, posix_spawn_file_actions_addclose "
"(action={1}, fd={2})",
error, file_actions, info->GetFD());
}
break;

Expand All @@ -927,12 +925,10 @@ bool Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info,
::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(),
info->GetActionArgument()),
eErrorTypePOSIX);
if (log && (error.Fail() || log))
error.PutToLog(
log,
"posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)",
static_cast<void *>(file_actions), info->GetFD(),
info->GetActionArgument());
if (error.Fail())
LLDB_LOG(log, "error: {0}, posix_spawn_file_actions_adddup2 "
"(action={1}, fd={2}, dup_fd={3})",
error, file_actions, info->GetFD(), info->GetActionArgument());
}
break;

Expand All @@ -952,11 +948,11 @@ bool Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info,
file_actions, info->GetFD(),
info->GetPath().str().c_str(), oflag, mode),
eErrorTypePOSIX);
if (error.Fail() || log)
error.PutToLog(log, "posix_spawn_file_actions_addopen (action=%p, "
"fd=%i, path='%s', oflag=%i, mode=%i)",
static_cast<void *>(file_actions), info->GetFD(),
info->GetPath().str().c_str(), oflag, mode);
if (error.Fail())
LLDB_LOG(
log, "error: {0}, posix_spawn_file_actions_addopen (action={1}, "
"fd={2}, path='{3}', oflag={4}, mode={5})",
error, file_actions, info->GetFD(), info->GetPath(), oflag, mode);
}
break;
}
Expand Down
Expand Up @@ -319,7 +319,7 @@ bool OperatingSystemGo::UpdateThreadList(ThreadList &old_thread_list,
for (uint64_t i = 0; i < allglen; ++i) {
goroutines.push_back(CreateGoroutineAtIndex(i, err));
if (err.Fail()) {
err.PutToLog(log, "OperatingSystemGo::UpdateThreadList");
LLDB_LOG(log, "error: {0}", err);
return new_thread_list.GetSize(false) > 0;
}
}
Expand Down
73 changes: 7 additions & 66 deletions lldb/source/Utility/Error.cpp
Expand Up @@ -129,72 +129,6 @@ ErrorType Error::GetType() const { return m_type; }
//----------------------------------------------------------------------
bool Error::Fail() const { return m_code != 0; }

//----------------------------------------------------------------------
// Log the error given a string with format. If the this object
// contains an error code, update the error string to contain the
// "error: " followed by the formatted string, followed by the error
// value and any string that describes the current error. This
// allows more context to be given to an error string that remains
// cached in this object. Logging always occurs even when the error
// code contains a non-error value.
//----------------------------------------------------------------------
void Error::PutToLog(Log *log, const char *format, ...) {
char *arg_msg = nullptr;
va_list args;
va_start(args, format);
::vasprintf(&arg_msg, format, args);
va_end(args);

if (arg_msg != nullptr) {
if (Fail()) {
const char *err_str = AsCString();
if (err_str == nullptr)
err_str = "???";

SetErrorStringWithFormat("error: %s err = %s (0x%8.8x)", arg_msg, err_str,
m_code);
if (log != nullptr)
log->Error("%s", m_string.c_str());
} else {
if (log != nullptr)
log->Printf("%s err = 0x%8.8x", arg_msg, m_code);
}
::free(arg_msg);
}
}

//----------------------------------------------------------------------
// Log the error given a string with format. If the this object
// contains an error code, update the error string to contain the
// "error: " followed by the formatted string, followed by the error
// value and any string that describes the current error. This
// allows more context to be given to an error string that remains
// cached in this object. Logging only occurs even when the error
// code contains a error value.
//----------------------------------------------------------------------
void Error::LogIfError(Log *log, const char *format, ...) {
if (Fail()) {
char *arg_msg = nullptr;
va_list args;
va_start(args, format);
::vasprintf(&arg_msg, format, args);
va_end(args);

if (arg_msg != nullptr) {
const char *err_str = AsCString();
if (err_str == nullptr)
err_str = "???";

SetErrorStringWithFormat("%s err = %s (0x%8.8x)", arg_msg, err_str,
m_code);
if (log != nullptr)
log->Error("%s", m_string.c_str());

::free(arg_msg);
}
}
}

//----------------------------------------------------------------------
// Set accesssor for the error value to "err" and the type to
// "eErrorTypeMachKernel"
Expand Down Expand Up @@ -334,3 +268,10 @@ bool Error::Success() const { return m_code == 0; }
bool Error::WasInterrupted() const {
return (m_type == eErrorTypePOSIX && m_code == EINTR);
}

void llvm::format_provider<lldb_private::Error>::format(
const lldb_private::Error &error, llvm::raw_ostream &OS,
llvm::StringRef Options) {
llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
Options);
}

0 comments on commit 33aba3c

Please sign in to comment.