190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//===-- ProcessPOSIXLog.cpp ---------------------------------------*- C++ -*-===// 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// The LLVM Compiler Infrastructure 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// License. See LICENSE.TXT for details. 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)//===----------------------------------------------------------------------===// 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ProcessPOSIXLog.h" 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "lldb/Interpreter/Args.h" 1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "lldb/Core/StreamFile.h" 1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ProcessPOSIX.h" 1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ProcessPOSIXLog.h" 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using namespace lldb; 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using namespace lldb_private; 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// We want to avoid global constructors where code needs to be run so here we 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// control access to our static g_log_sp by hiding it in a singleton function 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// that will construct the static g_log_sp the first time this function is 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// called. 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)static bool g_log_enabled = false; 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)static Log * g_log = NULL; 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)static Log * 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)GetLog () 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles){ 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (!g_log_enabled) 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return NULL; 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return g_log; 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)Log * 387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)ProcessPOSIXLog::GetLogIfAllCategoriesSet (uint32_t mask) 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles){ 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Log *log(GetLog ()); 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (log && mask) 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) { 4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint32_t log_mask = log->GetMask().Get(); 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if ((log_mask & mask) != mask) 4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return NULL; 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return log; 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)static uint32_t 5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)GetFlagBits (const char *arg) 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles){ 5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (::strcasecmp (arg, "all") == 0 ) return POSIX_LOG_ALL; 5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "async") == 0 ) return POSIX_LOG_ASYNC; 5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strncasecmp (arg, "break", 5) == 0 ) return POSIX_LOG_BREAKPOINTS; 5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strncasecmp (arg, "comm", 4) == 0 ) return POSIX_LOG_COMM; 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "default") == 0 ) return POSIX_LOG_DEFAULT; 5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "packets") == 0 ) return POSIX_LOG_PACKETS; 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "memory") == 0 ) return POSIX_LOG_MEMORY; 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "data-short") == 0 ) return POSIX_LOG_MEMORY_DATA_SHORT; 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "data-long") == 0 ) return POSIX_LOG_MEMORY_DATA_LONG; 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "process") == 0 ) return POSIX_LOG_PROCESS; 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "ptrace") == 0 ) return POSIX_LOG_PTRACE; 6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "registers") == 0 ) return POSIX_LOG_REGISTERS; 6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "step") == 0 ) return POSIX_LOG_STEP; 6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else if (::strcasecmp (arg, "thread") == 0 ) return POSIX_LOG_THREAD; 67 else if (::strcasecmp (arg, "verbose") == 0 ) return POSIX_LOG_VERBOSE; 68 else if (::strncasecmp (arg, "watch", 5) == 0 ) return POSIX_LOG_WATCHPOINTS; 69 return 0; 70} 71 72void 73ProcessPOSIXLog::DisableLog (const char **args, Stream *feedback_strm) 74{ 75 Log *log (GetLog ()); 76 if (log) 77 { 78 uint32_t flag_bits = 0; 79 80 flag_bits = log->GetMask().Get(); 81 for (; args[0]; args++) 82 { 83 const char *arg = args[0]; 84 uint32_t bits = GetFlagBits(arg); 85 86 if (bits) 87 { 88 flag_bits &= ~bits; 89 } 90 else 91 { 92 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); 93 ListLogCategories (feedback_strm); 94 } 95 } 96 97 log->GetMask().Reset (flag_bits); 98 if (flag_bits == 0) 99 g_log_enabled = false; 100 } 101 102 return; 103} 104 105Log * 106ProcessPOSIXLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **args, Stream *feedback_strm) 107{ 108 // Try see if there already is a log - that way we can reuse its settings. 109 // We could reuse the log in toto, but we don't know that the stream is the same. 110 uint32_t flag_bits = 0; 111 if (g_log) 112 flag_bits = g_log->GetMask().Get(); 113 114 // Now make a new log with this stream if one was provided 115 if (log_stream_sp) 116 { 117 if (g_log) 118 g_log->SetStream(log_stream_sp); 119 else 120 g_log = new Log(log_stream_sp); 121 } 122 123 if (g_log) 124 { 125 bool got_unknown_category = false; 126 for (; args[0]; args++) 127 { 128 const char *arg = args[0]; 129 uint32_t bits = GetFlagBits(arg); 130 131 if (bits) 132 { 133 flag_bits |= bits; 134 } 135 else 136 { 137 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); 138 if (got_unknown_category == false) 139 { 140 got_unknown_category = true; 141 ListLogCategories (feedback_strm); 142 } 143 } 144 } 145 if (flag_bits == 0) 146 flag_bits = POSIX_LOG_DEFAULT; 147 g_log->GetMask().Reset(flag_bits); 148 g_log->GetOptions().Reset(log_options); 149 g_log_enabled = true; 150 } 151 return g_log; 152} 153 154void 155ProcessPOSIXLog::ListLogCategories (Stream *strm) 156{ 157 strm->Printf ("Logging categories for '%s':\n" 158 " all - turn on all available logging categories\n" 159 " async - log asynchronous activity\n" 160 " break - log breakpoints\n" 161 " communication - log communication activity\n" 162 " default - enable the default set of logging categories for liblldb\n" 163 " packets - log gdb remote packets\n" 164 " memory - log memory reads and writes\n" 165 " data-short - log memory bytes for memory reads and writes for short transactions only\n" 166 " data-long - log memory bytes for memory reads and writes for all transactions\n" 167 " process - log process events and activities\n" 168#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION 169 " ptrace - log all calls to ptrace\n" 170#endif 171 " registers - log register read/writes\n" 172 " thread - log thread events and activities\n" 173 " step - log step related activities\n" 174 " verbose - enable verbose logging\n" 175 " watch - log watchpoint related activities\n", ProcessPOSIXLog::m_pluginname); 176} 177 178 179void 180ProcessPOSIXLog::LogIf (uint32_t mask, const char *format, ...) 181{ 182 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (mask)); 183 if (log) 184 { 185 va_list args; 186 va_start (args, format); 187 log->VAPrintf (format, args); 188 va_end (args); 189 } 190} 191 192int ProcessPOSIXLog::m_nestinglevel; 193const char *ProcessPOSIXLog::m_pluginname = ""; 194