Switch ProcessKDPLog to the new channel registration mechanism

llvm-svn: 295450
This commit is contained in:
Pavel Labath 2017-02-17 15:08:08 +00:00
parent f891e90b35
commit 7b35b78160
4 changed files with 33 additions and 201 deletions

View File

@ -726,11 +726,7 @@ void ProcessKDP::Initialize() {
GetPluginDescriptionStatic(), CreateInstance,
DebuggerInitialize);
Log::Callbacks log_callbacks = {ProcessKDPLog::DisableLog,
ProcessKDPLog::EnableLog,
ProcessKDPLog::ListLogCategories};
Log::RegisterLogChannel(ProcessKDP::GetPluginNameStatic(), log_callbacks);
ProcessKDPLog::Initialize();
});
}

View File

@ -9,181 +9,28 @@
#include "ProcessKDPLog.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Interpreter/Args.h"
#include "ProcessKDP.h"
using namespace lldb;
using namespace lldb_private;
// We want to avoid global constructors where code needs to be run so here we
// control access to our static g_log_sp by hiding it in a singleton function
// that will construct the static g_log_sp the first time this function is
// called.
static bool g_log_enabled = false;
static Log *g_log = NULL;
static Log *GetLog() {
if (!g_log_enabled)
return NULL;
return g_log;
}
static constexpr Log::Category g_categories[] = {
{{"async"}, {"log asynchronous activity"}, KDP_LOG_ASYNC},
{{"break"}, {"log breakpoints"}, KDP_LOG_BREAKPOINTS},
{{"comm"}, {"log communication activity"}, KDP_LOG_COMM},
{{"data-long"},
{"log memory bytes for memory reads and writes for all transactions"},
KDP_LOG_MEMORY_DATA_LONG},
{{"data-short"},
{"log memory bytes for memory reads and writes for short transactions "
"only"},
KDP_LOG_MEMORY_DATA_SHORT},
{{"memory"}, {"log memory reads and writes"}, KDP_LOG_MEMORY},
{{"packets"}, {"log gdb remote packets"}, KDP_LOG_PACKETS},
{{"process"}, {"log process events and activities"}, KDP_LOG_PROCESS},
{{"step"}, {"log step related activities"}, KDP_LOG_STEP},
{{"thread"}, {"log thread events and activities"}, KDP_LOG_THREAD},
{{"watch"}, {"log watchpoint related activities"}, KDP_LOG_WATCHPOINTS},
};
Log *ProcessKDPLog::GetLogIfAllCategoriesSet(uint32_t mask) {
Log *log(GetLog());
if (log && mask) {
uint32_t log_mask = log->GetMask().Get();
if ((log_mask & mask) != mask)
return NULL;
}
return log;
}
Log::Channel ProcessKDPLog::g_channel(g_categories, KDP_LOG_DEFAULT);
void ProcessKDPLog::DisableLog(const char **categories, Stream *feedback_strm) {
Log *log(GetLog());
if (log) {
uint32_t flag_bits = 0;
if (categories[0] != NULL) {
flag_bits = log->GetMask().Get();
for (size_t i = 0; categories[i] != NULL; ++i) {
const char *arg = categories[i];
if (::strcasecmp(arg, "all") == 0)
flag_bits &= ~KDP_LOG_ALL;
else if (::strcasecmp(arg, "async") == 0)
flag_bits &= ~KDP_LOG_ASYNC;
else if (::strncasecmp(arg, "break", 5) == 0)
flag_bits &= ~KDP_LOG_BREAKPOINTS;
else if (::strncasecmp(arg, "comm", 4) == 0)
flag_bits &= ~KDP_LOG_COMM;
else if (::strcasecmp(arg, "default") == 0)
flag_bits &= ~KDP_LOG_DEFAULT;
else if (::strcasecmp(arg, "packets") == 0)
flag_bits &= ~KDP_LOG_PACKETS;
else if (::strcasecmp(arg, "memory") == 0)
flag_bits &= ~KDP_LOG_MEMORY;
else if (::strcasecmp(arg, "data-short") == 0)
flag_bits &= ~KDP_LOG_MEMORY_DATA_SHORT;
else if (::strcasecmp(arg, "data-long") == 0)
flag_bits &= ~KDP_LOG_MEMORY_DATA_LONG;
else if (::strcasecmp(arg, "process") == 0)
flag_bits &= ~KDP_LOG_PROCESS;
else if (::strcasecmp(arg, "step") == 0)
flag_bits &= ~KDP_LOG_STEP;
else if (::strcasecmp(arg, "thread") == 0)
flag_bits &= ~KDP_LOG_THREAD;
else if (::strncasecmp(arg, "watch", 5) == 0)
flag_bits &= ~KDP_LOG_WATCHPOINTS;
else {
feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
ListLogCategories(feedback_strm);
}
}
}
log->GetMask().Reset(flag_bits);
if (flag_bits == 0)
g_log_enabled = false;
}
return;
}
Log *ProcessKDPLog::EnableLog(
const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
uint32_t log_options, const char **categories, Stream *feedback_strm) {
// Try see if there already is a log - that way we can reuse its settings.
// We could reuse the log in toto, but we don't know that the stream is the
// same.
uint32_t flag_bits = 0;
if (g_log)
flag_bits = g_log->GetMask().Get();
// Now make a new log with this stream if one was provided
if (log_stream_sp) {
if (g_log)
g_log->SetStream(log_stream_sp);
else
g_log = new Log(log_stream_sp);
}
if (g_log) {
bool got_unknown_category = false;
for (size_t i = 0; categories[i] != NULL; ++i) {
const char *arg = categories[i];
if (::strcasecmp(arg, "all") == 0)
flag_bits |= KDP_LOG_ALL;
else if (::strcasecmp(arg, "async") == 0)
flag_bits |= KDP_LOG_ASYNC;
else if (::strncasecmp(arg, "break", 5) == 0)
flag_bits |= KDP_LOG_BREAKPOINTS;
else if (::strncasecmp(arg, "comm", 4) == 0)
flag_bits |= KDP_LOG_COMM;
else if (::strcasecmp(arg, "default") == 0)
flag_bits |= KDP_LOG_DEFAULT;
else if (::strcasecmp(arg, "packets") == 0)
flag_bits |= KDP_LOG_PACKETS;
else if (::strcasecmp(arg, "memory") == 0)
flag_bits |= KDP_LOG_MEMORY;
else if (::strcasecmp(arg, "data-short") == 0)
flag_bits |= KDP_LOG_MEMORY_DATA_SHORT;
else if (::strcasecmp(arg, "data-long") == 0)
flag_bits |= KDP_LOG_MEMORY_DATA_LONG;
else if (::strcasecmp(arg, "process") == 0)
flag_bits |= KDP_LOG_PROCESS;
else if (::strcasecmp(arg, "step") == 0)
flag_bits |= KDP_LOG_STEP;
else if (::strcasecmp(arg, "thread") == 0)
flag_bits |= KDP_LOG_THREAD;
else if (::strncasecmp(arg, "watch", 5) == 0)
flag_bits |= KDP_LOG_WATCHPOINTS;
else {
feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
if (got_unknown_category == false) {
got_unknown_category = true;
ListLogCategories(feedback_strm);
}
}
}
if (flag_bits == 0)
flag_bits = KDP_LOG_DEFAULT;
g_log->GetMask().Reset(flag_bits);
g_log->GetOptions().Reset(log_options);
}
g_log_enabled = true;
return g_log;
}
void ProcessKDPLog::ListLogCategories(Stream *strm) {
strm->Printf(
"Logging categories for '%s':\n"
" all - turn on all available logging categories\n"
" async - log asynchronous activity\n"
" break - log breakpoints\n"
" communication - log communication activity\n"
" default - enable the default set of logging categories for liblldb\n"
" packets - log gdb remote packets\n"
" memory - log memory reads and writes\n"
" data-short - log memory bytes for memory reads and writes for short "
"transactions only\n"
" data-long - log memory bytes for memory reads and writes for all "
"transactions\n"
" process - log process events and activities\n"
" thread - log thread events and activities\n"
" step - log step related activities\n"
" verbose - enable verbose logging\n"
" watch - log watchpoint related activities\n",
ProcessKDP::GetPluginNameStatic().GetCString());
}
void ProcessKDPLog::LogIf(uint32_t mask, const char *format, ...) {
Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(mask));
if (log) {
va_list args;
va_start(args, format);
log->VAPrintf(format, args);
va_end(args);
}
}
void ProcessKDPLog::Initialize() { Log::Register("kdp-remote", g_channel); }

View File

@ -10,11 +10,6 @@
#ifndef liblldb_ProcessKDPLog_h_
#define liblldb_ProcessKDPLog_h_
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Log.h"
#define KDP_LOG_PROCESS (1u << 1)
@ -32,21 +27,17 @@
#define KDP_LOG_ALL (UINT32_MAX)
#define KDP_LOG_DEFAULT KDP_LOG_PACKETS
namespace lldb_private {
class ProcessKDPLog {
static Log::Channel g_channel;
public:
static lldb_private::Log *GetLogIfAllCategoriesSet(uint32_t mask = 0);
static void Initialize();
static void DisableLog(const char **categories,
lldb_private::Stream *feedback_strm);
static lldb_private::Log *
EnableLog(const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
uint32_t log_options, const char **categories,
lldb_private::Stream *feedback_strm);
static void ListLogCategories(lldb_private::Stream *strm);
static void LogIf(uint32_t mask, const char *format, ...);
static Log *GetLogIfAllCategoriesSet(uint32_t mask) {
return g_channel.GetLogIfAll(mask);
}
};
}
#endif // liblldb_ProcessKDPLog_h_

View File

@ -40,15 +40,13 @@ using namespace lldb_private;
ThreadKDP::ThreadKDP(Process &process, lldb::tid_t tid)
: Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS) {
ProcessKDPLog::LogIf(KDP_LOG_THREAD,
"%p: ThreadKDP::ThreadKDP (tid = 0x%4.4x)", this,
GetID());
Log *log = ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_THREAD);
LLDB_LOG(log, "this = {0}, tid = {1:x}", this, GetID());
}
ThreadKDP::~ThreadKDP() {
ProcessKDPLog::LogIf(KDP_LOG_THREAD,
"%p: ThreadKDP::~ThreadKDP (tid = 0x%4.4x)", this,
GetID());
Log *log = ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_THREAD);
LLDB_LOG(log, "this = {0}, tid = {1:x}", this, GetID());
DestroyThread();
}