blob: a83075481edf5ee34462fec514a6a58510ad1762 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Log.cpp -------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include <stdio.h>
12#include <stdarg.h>
13#include <stdlib.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15// C++ Includes
16#include <map>
17#include <string>
18
19// Other libraries and framework includes
20// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Log.h"
22#include "lldb/Core/PluginManager.h"
23#include "lldb/Core/StreamFile.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Host/Mutex.h"
Zachary Turner39de3112014-09-09 20:54:56 +000027#include "lldb/Host/ThisThread.h"
28#include "lldb/Host/TimeValue.h"
Caroline Tice20ad3c42010-10-29 21:48:37 +000029#include "lldb/Interpreter/Args.h"
Zachary Turner50232572015-03-18 21:31:45 +000030#include "lldb/Utility/NameMatches.h"
Zachary Turner39de3112014-09-09 20:54:56 +000031
32#include "llvm/ADT/SmallString.h"
Zachary Turnera893d302015-03-06 20:45:43 +000033#include "llvm/Support/raw_ostream.h"
34#include "llvm/Support/Signals.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035using namespace lldb;
36using namespace lldb_private;
37
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038Log::Log () :
39 m_stream_sp(),
40 m_options(0),
41 m_mask_bits(0)
42{
43}
44
Greg Clayton5160ce52013-03-27 23:08:40 +000045Log::Log (const StreamSP &stream_sp) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046 m_stream_sp(stream_sp),
47 m_options(0),
48 m_mask_bits(0)
49{
50}
51
52Log::~Log ()
53{
54}
55
56Flags &
57Log::GetOptions()
58{
59 return m_options;
60}
61
62const Flags &
63Log::GetOptions() const
64{
65 return m_options;
66}
67
68Flags &
69Log::GetMask()
70{
71 return m_mask_bits;
72}
73
74const Flags &
75Log::GetMask() const
76{
77 return m_mask_bits;
78}
79
Zachary Turnerc1592652015-04-29 22:55:28 +000080void
81Log::PutCString(const char *cstr)
82{
83 Printf("%s", cstr);
84}
85
86//----------------------------------------------------------------------
87// Simple variable argument logging with flags.
88//----------------------------------------------------------------------
89void
90Log::Printf(const char *format, ...)
91{
92 va_list args;
93 va_start(args, format);
94 VAPrintf(format, args);
95 va_end(args);
96}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
98//----------------------------------------------------------------------
99// All logging eventually boils down to this function call. If we have
100// a callback registered, then we call the logging callback. If we have
101// a valid file handle, we also log to the file.
102//----------------------------------------------------------------------
103void
Zachary Turnerc1592652015-04-29 22:55:28 +0000104Log::VAPrintf(const char *format, va_list args)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105{
Greg Claytone98008c2014-02-13 23:34:38 +0000106 // Make a copy of our stream shared pointer in case someone disables our
107 // log while we are logging and releases the stream
108 StreamSP stream_sp(m_stream_sp);
109 if (stream_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 {
111 static uint32_t g_sequence_id = 0;
112 StreamString header;
Greg Clayton64195a22011-02-23 00:35:02 +0000113 // Enabling the thread safe logging actually deadlocks right now.
114 // Need to fix this at some point.
115// static Mutex g_LogThreadedMutex(Mutex::eMutexTypeRecursive);
116// Mutex::Locker locker (g_LogThreadedMutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
118 // Add a sequence ID if requested
Greg Clayton73b472d2010-10-27 03:32:59 +0000119 if (m_options.Test (LLDB_LOG_OPTION_PREPEND_SEQUENCE))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 header.Printf ("%u ", ++g_sequence_id);
121
122 // Timestamp if requested
Greg Clayton73b472d2010-10-27 03:32:59 +0000123 if (m_options.Test (LLDB_LOG_OPTION_PREPEND_TIMESTAMP))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 {
Virgile Bello0a3b1512013-09-04 13:56:11 +0000125 TimeValue now = TimeValue::Now();
Jason Molenda3c2daca2013-09-11 21:00:37 +0000126 header.Printf ("%9d.%6.6d ", now.seconds(), now.nanoseconds());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 }
128
129 // Add the process and thread if requested
Greg Clayton73b472d2010-10-27 03:32:59 +0000130 if (m_options.Test (LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
Daniel Malead01b2952012-11-29 21:49:15 +0000131 header.Printf ("[%4.4x/%4.4" PRIx64 "]: ", getpid(), Host::GetCurrentThreadID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132
Ed Mastecc913d142014-05-21 13:46:46 +0000133 // Add the thread name if requested
Greg Clayton73b472d2010-10-27 03:32:59 +0000134 if (m_options.Test (LLDB_LOG_OPTION_PREPEND_THREAD_NAME))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 {
Zachary Turner39de3112014-09-09 20:54:56 +0000136 llvm::SmallString<32> thread_name;
137 ThisThread::GetName(thread_name);
Greg Clayton85719632013-02-27 22:51:58 +0000138 if (!thread_name.empty())
139 header.Printf ("%s ", thread_name.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 }
141
142 header.PrintfVarArg (format, args);
Greg Claytone98008c2014-02-13 23:34:38 +0000143 stream_sp->Printf("%s\n", header.GetData());
Greg Clayton3a18e312012-10-08 22:41:53 +0000144
Zachary Turnera893d302015-03-06 20:45:43 +0000145 if (m_options.Test(LLDB_LOG_OPTION_BACKTRACE))
146 {
147 std::string back_trace;
148 llvm::raw_string_ostream stream(back_trace);
149 llvm::sys::PrintStackTrace(stream);
150 stream_sp->PutCString(back_trace.c_str());
151 }
Greg Claytone98008c2014-02-13 23:34:38 +0000152 stream_sp->Flush();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 }
154}
155
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156//----------------------------------------------------------------------
157// Print debug strings if and only if the global debug option is set to
158// a non-zero value.
159//----------------------------------------------------------------------
160void
Zachary Turnerc1592652015-04-29 22:55:28 +0000161Log::Debug(const char *format, ...)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162{
Zachary Turnerc1592652015-04-29 22:55:28 +0000163 if (!GetOptions().Test(LLDB_LOG_OPTION_DEBUG))
164 return;
165
166 va_list args;
167 va_start(args, format);
168 VAPrintf(format, args);
169 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170}
171
172
173//----------------------------------------------------------------------
174// Print debug strings if and only if the global debug option is set to
175// a non-zero value.
176//----------------------------------------------------------------------
177void
Zachary Turnerc1592652015-04-29 22:55:28 +0000178Log::DebugVerbose(const char *format, ...)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179{
Zachary Turnerc1592652015-04-29 22:55:28 +0000180 if (!GetOptions().AllSet(LLDB_LOG_OPTION_DEBUG | LLDB_LOG_OPTION_VERBOSE))
181 return;
182
183 va_list args;
184 va_start(args, format);
185 VAPrintf(format, args);
186 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187}
188
189
190//----------------------------------------------------------------------
191// Log only if all of the bits are set
192//----------------------------------------------------------------------
193void
Zachary Turnerc1592652015-04-29 22:55:28 +0000194Log::LogIf(uint32_t bits, const char *format, ...)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195{
Zachary Turnerc1592652015-04-29 22:55:28 +0000196 if (!m_options.AllSet(bits))
197 return;
198
199 va_list args;
200 va_start(args, format);
201 VAPrintf(format, args);
202 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203}
204
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205//----------------------------------------------------------------------
206// Printing of errors that are not fatal.
207//----------------------------------------------------------------------
208void
Zachary Turnerc1592652015-04-29 22:55:28 +0000209Log::Error(const char *format, ...)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210{
Zachary Turnerc1592652015-04-29 22:55:28 +0000211 char *arg_msg = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212 va_list args;
Zachary Turnerc1592652015-04-29 22:55:28 +0000213 va_start(args, format);
214 ::vasprintf(&arg_msg, format, args);
215 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216
Zachary Turnerc1592652015-04-29 22:55:28 +0000217 if (arg_msg == nullptr)
218 return;
219
Pavel Labathd351bcc2015-04-30 10:47:56 +0000220 Printf("error: %s", arg_msg);
Zachary Turnerc1592652015-04-29 22:55:28 +0000221 free(arg_msg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222}
223
224//----------------------------------------------------------------------
225// Printing of errors that ARE fatal. Exit with ERR exit code
226// immediately.
227//----------------------------------------------------------------------
228void
Zachary Turnerc1592652015-04-29 22:55:28 +0000229Log::FatalError(int err, const char *format, ...)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230{
Zachary Turnerc1592652015-04-29 22:55:28 +0000231 char *arg_msg = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232 va_list args;
Zachary Turnerc1592652015-04-29 22:55:28 +0000233 va_start(args, format);
234 ::vasprintf(&arg_msg, format, args);
235 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236
Zachary Turnerc1592652015-04-29 22:55:28 +0000237 if (arg_msg != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 {
Zachary Turnerb74e2792015-04-29 23:24:12 +0000239 Printf("error: %s", arg_msg);
Zachary Turnerc1592652015-04-29 22:55:28 +0000240 ::free(arg_msg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 }
Zachary Turnerc1592652015-04-29 22:55:28 +0000242 ::exit(err);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243}
244
245
246//----------------------------------------------------------------------
247// Printing of warnings that are not fatal only if verbose mode is
248// enabled.
249//----------------------------------------------------------------------
250void
Zachary Turnerc1592652015-04-29 22:55:28 +0000251Log::Verbose(const char *format, ...)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252{
Zachary Turnerc1592652015-04-29 22:55:28 +0000253 if (!m_options.Test(LLDB_LOG_OPTION_VERBOSE))
254 return;
255
256 va_list args;
257 va_start(args, format);
258 VAPrintf(format, args);
259 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260}
261
262//----------------------------------------------------------------------
263// Printing of warnings that are not fatal only if verbose mode is
264// enabled.
265//----------------------------------------------------------------------
266void
Zachary Turnerc1592652015-04-29 22:55:28 +0000267Log::WarningVerbose(const char *format, ...)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268{
Zachary Turnerc1592652015-04-29 22:55:28 +0000269 if (!m_options.Test(LLDB_LOG_OPTION_VERBOSE))
270 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271
Zachary Turnerc1592652015-04-29 22:55:28 +0000272 char *arg_msg = nullptr;
273 va_list args;
274 va_start(args, format);
275 ::vasprintf(&arg_msg, format, args);
276 va_end(args);
277
278 if (arg_msg == nullptr)
279 return;
280
Zachary Turnerb74e2792015-04-29 23:24:12 +0000281 Printf("warning: %s", arg_msg);
Zachary Turnerc1592652015-04-29 22:55:28 +0000282 free(arg_msg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283}
284//----------------------------------------------------------------------
285// Printing of warnings that are not fatal.
286//----------------------------------------------------------------------
287void
Zachary Turnerc1592652015-04-29 22:55:28 +0000288Log::Warning(const char *format, ...)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289{
Zachary Turnerc1592652015-04-29 22:55:28 +0000290 char *arg_msg = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 va_list args;
Zachary Turnerc1592652015-04-29 22:55:28 +0000292 va_start(args, format);
293 ::vasprintf(&arg_msg, format, args);
294 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
Zachary Turnerc1592652015-04-29 22:55:28 +0000296 if (arg_msg == nullptr)
297 return;
298
Zachary Turnerb74e2792015-04-29 23:24:12 +0000299 Printf("warning: %s", arg_msg);
Zachary Turnerc1592652015-04-29 22:55:28 +0000300 free(arg_msg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301}
302
Greg Clayton57abc5d2013-05-10 21:47:16 +0000303typedef std::map <ConstString, Log::Callbacks> CallbackMap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304typedef CallbackMap::iterator CallbackMapIter;
305
306typedef std::map <ConstString, LogChannelSP> LogChannelMap;
307typedef LogChannelMap::iterator LogChannelMapIter;
308
309
310// Surround our callback map with a singleton function so we don't have any
311// global initializers.
312static CallbackMap &
313GetCallbackMap ()
314{
315 static CallbackMap g_callback_map;
316 return g_callback_map;
317}
318
319static LogChannelMap &
320GetChannelMap ()
321{
322 static LogChannelMap g_channel_map;
323 return g_channel_map;
324}
325
326void
Greg Clayton57abc5d2013-05-10 21:47:16 +0000327Log::RegisterLogChannel (const ConstString &channel, const Log::Callbacks &log_callbacks)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328{
329 GetCallbackMap().insert(std::make_pair(channel, log_callbacks));
330}
331
332bool
Greg Clayton57abc5d2013-05-10 21:47:16 +0000333Log::UnregisterLogChannel (const ConstString &channel)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334{
335 return GetCallbackMap().erase(channel) != 0;
336}
337
338bool
Greg Clayton57abc5d2013-05-10 21:47:16 +0000339Log::GetLogChannelCallbacks (const ConstString &channel, Log::Callbacks &log_callbacks)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340{
341 CallbackMap &callback_map = GetCallbackMap ();
342 CallbackMapIter pos = callback_map.find(channel);
343 if (pos != callback_map.end())
344 {
345 log_callbacks = pos->second;
346 return true;
347 }
Greg Clayton72b77eb2011-02-04 21:13:05 +0000348 ::memset (&log_callbacks, 0, sizeof(log_callbacks));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 return false;
350}
351
352void
353Log::EnableAllLogChannels
354(
355 StreamSP &log_stream_sp,
356 uint32_t log_options,
Jim Ingham228063c2012-02-21 02:23:08 +0000357 const char **categories,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 Stream *feedback_strm
359)
360{
361 CallbackMap &callback_map = GetCallbackMap ();
362 CallbackMapIter pos, end = callback_map.end();
363
364 for (pos = callback_map.begin(); pos != end; ++pos)
Jim Ingham228063c2012-02-21 02:23:08 +0000365 pos->second.enable (log_stream_sp, log_options, categories, feedback_strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366
367 LogChannelMap &channel_map = GetChannelMap ();
368 LogChannelMapIter channel_pos, channel_end = channel_map.end();
369 for (channel_pos = channel_map.begin(); channel_pos != channel_end; ++channel_pos)
370 {
Jim Ingham228063c2012-02-21 02:23:08 +0000371 channel_pos->second->Enable (log_stream_sp, log_options, feedback_strm, categories);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 }
373
374}
375
376void
Greg Claytonab65b342011-04-13 22:47:15 +0000377Log::AutoCompleteChannelName (const char *channel_name, StringList &matches)
378{
379 LogChannelMap &map = GetChannelMap ();
380 LogChannelMapIter pos, end = map.end();
381 for (pos = map.begin(); pos != end; ++pos)
382 {
383 const char *pos_channel_name = pos->first.GetCString();
384 if (channel_name && channel_name[0])
385 {
386 if (NameMatches (channel_name, eNameMatchStartsWith, pos_channel_name))
387 {
388 matches.AppendString(pos_channel_name);
389 }
390 }
391 else
392 matches.AppendString(pos_channel_name);
393
394 }
395}
396
397void
Caroline Tice20ad3c42010-10-29 21:48:37 +0000398Log::DisableAllLogChannels (Stream *feedback_strm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399{
400 CallbackMap &callback_map = GetCallbackMap ();
401 CallbackMapIter pos, end = callback_map.end();
Jim Ingham228063c2012-02-21 02:23:08 +0000402 const char *categories[1] = {NULL};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403
404 for (pos = callback_map.begin(); pos != end; ++pos)
Jim Ingham228063c2012-02-21 02:23:08 +0000405 pos->second.disable (categories, feedback_strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406
407 LogChannelMap &channel_map = GetChannelMap ();
408 LogChannelMapIter channel_pos, channel_end = channel_map.end();
409 for (channel_pos = channel_map.begin(); channel_pos != channel_end; ++channel_pos)
Jim Ingham228063c2012-02-21 02:23:08 +0000410 channel_pos->second->Disable (categories, feedback_strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411}
412
413void
Greg Clayton99d0faf2010-11-18 23:32:35 +0000414Log::Initialize()
415{
416 Log::Callbacks log_callbacks = { DisableLog, EnableLog, ListLogCategories };
Greg Clayton57abc5d2013-05-10 21:47:16 +0000417 Log::RegisterLogChannel (ConstString("lldb"), log_callbacks);
Greg Clayton99d0faf2010-11-18 23:32:35 +0000418}
419
420void
421Log::Terminate ()
422{
423 DisableAllLogChannels (NULL);
424}
425
426void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427Log::ListAllLogChannels (Stream *strm)
428{
429 CallbackMap &callback_map = GetCallbackMap ();
430 LogChannelMap &channel_map = GetChannelMap ();
431
432 if (callback_map.empty() && channel_map.empty())
433 {
434 strm->PutCString ("No logging channels are currently registered.\n");
435 return;
436 }
437
438 CallbackMapIter pos, end = callback_map.end();
439 for (pos = callback_map.begin(); pos != end; ++pos)
440 pos->second.list_categories (strm);
441
442 uint32_t idx = 0;
443 const char *name;
444 for (idx = 0; (name = PluginManager::GetLogChannelCreateNameAtIndex (idx)) != NULL; ++idx)
445 {
446 LogChannelSP log_channel_sp(LogChannel::FindPlugin (name));
447 if (log_channel_sp)
448 log_channel_sp->ListCategories (strm);
449 }
450}
451
452bool
453Log::GetVerbose() const
454{
Jim Inghamd8d148e2011-01-22 01:24:30 +0000455 // FIXME: This has to be centralized between the stream and the log...
456 if (m_options.Test(LLDB_LOG_OPTION_VERBOSE))
457 return true;
458
Greg Claytone98008c2014-02-13 23:34:38 +0000459 // Make a copy of our stream shared pointer in case someone disables our
460 // log while we are logging and releases the stream
461 StreamSP stream_sp(m_stream_sp);
462 if (stream_sp)
463 return stream_sp->GetVerbose();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 return false;
465}
466
467//------------------------------------------------------------------
468// Returns true if the debug flag bit is set in this stream.
469//------------------------------------------------------------------
470bool
471Log::GetDebug() const
472{
Greg Claytone98008c2014-02-13 23:34:38 +0000473 // Make a copy of our stream shared pointer in case someone disables our
474 // log while we are logging and releases the stream
475 StreamSP stream_sp(m_stream_sp);
476 if (stream_sp)
477 return stream_sp->GetDebug();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 return false;
479}
480
481
482LogChannelSP
483LogChannel::FindPlugin (const char *plugin_name)
484{
485 LogChannelSP log_channel_sp;
486 LogChannelMap &channel_map = GetChannelMap ();
487 ConstString log_channel_name (plugin_name);
488 LogChannelMapIter pos = channel_map.find (log_channel_name);
489 if (pos == channel_map.end())
490 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000491 ConstString const_plugin_name (plugin_name);
492 LogChannelCreateInstance create_callback = PluginManager::GetLogChannelCreateCallbackForPluginName (const_plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 if (create_callback)
494 {
495 log_channel_sp.reset(create_callback());
496 if (log_channel_sp)
497 {
498 // Cache the one and only loaded instance of each log channel
499 // plug-in after it has been loaded once.
500 channel_map[log_channel_name] = log_channel_sp;
501 }
502 }
503 }
504 else
505 {
506 // We have already loaded an instance of this log channel class,
507 // so just return the cached instance.
508 log_channel_sp = pos->second;
509 }
510 return log_channel_sp;
511}
512
513LogChannel::LogChannel () :
Greg Clayton5160ce52013-03-27 23:08:40 +0000514 m_log_ap ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515{
516}
517
518LogChannel::~LogChannel ()
519{
520}
521
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522