1*59ec512cSGreg Clayton //===-- ProcessKDPLog.cpp ---------------------------------------*- C++ -*-===//
2f9765acdSGreg Clayton //
3f9765acdSGreg Clayton //                     The LLVM Compiler Infrastructure
4f9765acdSGreg Clayton //
5f9765acdSGreg Clayton // This file is distributed under the University of Illinois Open Source
6f9765acdSGreg Clayton // License. See LICENSE.TXT for details.
7f9765acdSGreg Clayton //
8f9765acdSGreg Clayton //===----------------------------------------------------------------------===//
9f9765acdSGreg Clayton 
10f9765acdSGreg Clayton #include "ProcessKDPLog.h"
11f9765acdSGreg Clayton 
12f9765acdSGreg Clayton #include "lldb/Interpreter/Args.h"
13f9765acdSGreg Clayton #include "lldb/Core/StreamFile.h"
14f9765acdSGreg Clayton 
15f9765acdSGreg Clayton #include "ProcessKDP.h"
16f9765acdSGreg Clayton 
17f9765acdSGreg Clayton using namespace lldb;
18f9765acdSGreg Clayton using namespace lldb_private;
19f9765acdSGreg Clayton 
20f9765acdSGreg Clayton 
21f9765acdSGreg Clayton // We want to avoid global constructors where code needs to be run so here we
22f9765acdSGreg Clayton // control access to our static g_log_sp by hiding it in a singleton function
23f9765acdSGreg Clayton // that will construct the static g_lob_sp the first time this function is
24f9765acdSGreg Clayton // called.
25f9765acdSGreg Clayton static LogSP &
26f9765acdSGreg Clayton GetLog ()
27f9765acdSGreg Clayton {
28f9765acdSGreg Clayton     static LogSP g_log_sp;
29f9765acdSGreg Clayton     return g_log_sp;
30f9765acdSGreg Clayton }
31f9765acdSGreg Clayton 
32f9765acdSGreg Clayton LogSP
33f9765acdSGreg Clayton ProcessKDPLog::GetLogIfAllCategoriesSet (uint32_t mask)
34f9765acdSGreg Clayton {
35f9765acdSGreg Clayton     LogSP log(GetLog ());
36f9765acdSGreg Clayton     if (log && mask)
37f9765acdSGreg Clayton     {
38f9765acdSGreg Clayton         uint32_t log_mask = log->GetMask().Get();
39f9765acdSGreg Clayton         if ((log_mask & mask) != mask)
40f9765acdSGreg Clayton             return LogSP();
41f9765acdSGreg Clayton     }
42f9765acdSGreg Clayton     return log;
43f9765acdSGreg Clayton }
44f9765acdSGreg Clayton 
45f9765acdSGreg Clayton void
46f9765acdSGreg Clayton ProcessKDPLog::DisableLog (Args &args, Stream *feedback_strm)
47f9765acdSGreg Clayton {
48f9765acdSGreg Clayton     LogSP log (GetLog ());
49f9765acdSGreg Clayton     if (log)
50f9765acdSGreg Clayton     {
51f9765acdSGreg Clayton         uint32_t flag_bits = 0;
52f9765acdSGreg Clayton 
53f9765acdSGreg Clayton         const size_t argc = args.GetArgumentCount ();
54f9765acdSGreg Clayton         if (argc > 0)
55f9765acdSGreg Clayton         {
56f9765acdSGreg Clayton             flag_bits = log->GetMask().Get();
57f9765acdSGreg Clayton             for (size_t i = 0; i < argc; ++i)
58f9765acdSGreg Clayton             {
59f9765acdSGreg Clayton                 const char *arg = args.GetArgumentAtIndex (i);
60f9765acdSGreg Clayton 
61f9765acdSGreg Clayton 
62f9765acdSGreg Clayton                 if      (::strcasecmp (arg, "all")        == 0 ) flag_bits &= ~KDP_LOG_ALL;
63f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "async")      == 0 ) flag_bits &= ~KDP_LOG_ASYNC;
64f9765acdSGreg Clayton                 else if (::strncasecmp (arg, "break", 5)  == 0 ) flag_bits &= ~KDP_LOG_BREAKPOINTS;
65f9765acdSGreg Clayton                 else if (::strncasecmp (arg, "comm", 4)   == 0 ) flag_bits &= ~KDP_LOG_COMM;
66f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "default")    == 0 ) flag_bits &= ~KDP_LOG_DEFAULT;
67f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "packets")    == 0 ) flag_bits &= ~KDP_LOG_PACKETS;
68f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "memory")     == 0 ) flag_bits &= ~KDP_LOG_MEMORY;
69f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~KDP_LOG_MEMORY_DATA_SHORT;
70f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "data-long")  == 0 ) flag_bits &= ~KDP_LOG_MEMORY_DATA_LONG;
71f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "process")    == 0 ) flag_bits &= ~KDP_LOG_PROCESS;
72f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "step")       == 0 ) flag_bits &= ~KDP_LOG_STEP;
73f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "thread")     == 0 ) flag_bits &= ~KDP_LOG_THREAD;
74f9765acdSGreg Clayton                 else if (::strcasecmp (arg, "verbose")    == 0 ) flag_bits &= ~KDP_LOG_VERBOSE;
75f9765acdSGreg Clayton                 else if (::strncasecmp (arg, "watch", 5)  == 0 ) flag_bits &= ~KDP_LOG_WATCHPOINTS;
76f9765acdSGreg Clayton                 else
77f9765acdSGreg Clayton                 {
78f9765acdSGreg Clayton                     feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
79f9765acdSGreg Clayton                     ListLogCategories (feedback_strm);
80f9765acdSGreg Clayton                 }
81f9765acdSGreg Clayton 
82f9765acdSGreg Clayton             }
83f9765acdSGreg Clayton         }
84f9765acdSGreg Clayton 
85f9765acdSGreg Clayton         if (flag_bits == 0)
86f9765acdSGreg Clayton             GetLog ().reset();
87f9765acdSGreg Clayton         else
88f9765acdSGreg Clayton             log->GetMask().Reset (flag_bits);
89f9765acdSGreg Clayton     }
90f9765acdSGreg Clayton 
91f9765acdSGreg Clayton     return;
92f9765acdSGreg Clayton }
93f9765acdSGreg Clayton 
94f9765acdSGreg Clayton LogSP
95f9765acdSGreg Clayton ProcessKDPLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
96f9765acdSGreg Clayton {
97f9765acdSGreg Clayton     // Try see if there already is a log - that way we can reuse its settings.
98f9765acdSGreg Clayton     // We could reuse the log in toto, but we don't know that the stream is the same.
99f9765acdSGreg Clayton     uint32_t flag_bits = 0;
100f9765acdSGreg Clayton     LogSP log(GetLog ());
101f9765acdSGreg Clayton     if (log)
102f9765acdSGreg Clayton         flag_bits = log->GetMask().Get();
103f9765acdSGreg Clayton 
104f9765acdSGreg Clayton     // Now make a new log with this stream if one was provided
105f9765acdSGreg Clayton     if (log_stream_sp)
106f9765acdSGreg Clayton     {
107f9765acdSGreg Clayton         log = make_shared<Log>(log_stream_sp);
108f9765acdSGreg Clayton         GetLog () = log;
109f9765acdSGreg Clayton     }
110f9765acdSGreg Clayton 
111f9765acdSGreg Clayton     if (log)
112f9765acdSGreg Clayton     {
113f9765acdSGreg Clayton         bool got_unknown_category = false;
114f9765acdSGreg Clayton         const size_t argc = args.GetArgumentCount();
115f9765acdSGreg Clayton         for (size_t i=0; i<argc; ++i)
116f9765acdSGreg Clayton         {
117f9765acdSGreg Clayton             const char *arg = args.GetArgumentAtIndex(i);
118f9765acdSGreg Clayton 
119f9765acdSGreg Clayton             if      (::strcasecmp (arg, "all")        == 0 ) flag_bits |= KDP_LOG_ALL;
120f9765acdSGreg Clayton             else if (::strcasecmp (arg, "async")      == 0 ) flag_bits |= KDP_LOG_ASYNC;
121f9765acdSGreg Clayton             else if (::strncasecmp (arg, "break", 5)  == 0 ) flag_bits |= KDP_LOG_BREAKPOINTS;
122f9765acdSGreg Clayton             else if (::strncasecmp (arg, "comm", 4)   == 0 ) flag_bits |= KDP_LOG_COMM;
123f9765acdSGreg Clayton             else if (::strcasecmp (arg, "default")    == 0 ) flag_bits |= KDP_LOG_DEFAULT;
124f9765acdSGreg Clayton             else if (::strcasecmp (arg, "packets")    == 0 ) flag_bits |= KDP_LOG_PACKETS;
125f9765acdSGreg Clayton             else if (::strcasecmp (arg, "memory")     == 0 ) flag_bits |= KDP_LOG_MEMORY;
126f9765acdSGreg Clayton             else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= KDP_LOG_MEMORY_DATA_SHORT;
127f9765acdSGreg Clayton             else if (::strcasecmp (arg, "data-long")  == 0 ) flag_bits |= KDP_LOG_MEMORY_DATA_LONG;
128f9765acdSGreg Clayton             else if (::strcasecmp (arg, "process")    == 0 ) flag_bits |= KDP_LOG_PROCESS;
129f9765acdSGreg Clayton             else if (::strcasecmp (arg, "step")       == 0 ) flag_bits |= KDP_LOG_STEP;
130f9765acdSGreg Clayton             else if (::strcasecmp (arg, "thread")     == 0 ) flag_bits |= KDP_LOG_THREAD;
131f9765acdSGreg Clayton             else if (::strcasecmp (arg, "verbose")    == 0 ) flag_bits |= KDP_LOG_VERBOSE;
132f9765acdSGreg Clayton             else if (::strncasecmp (arg, "watch", 5)  == 0 ) flag_bits |= KDP_LOG_WATCHPOINTS;
133f9765acdSGreg Clayton             else
134f9765acdSGreg Clayton             {
135f9765acdSGreg Clayton                 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
136f9765acdSGreg Clayton                 if (got_unknown_category == false)
137f9765acdSGreg Clayton                 {
138f9765acdSGreg Clayton                     got_unknown_category = true;
139f9765acdSGreg Clayton                     ListLogCategories (feedback_strm);
140f9765acdSGreg Clayton                 }
141f9765acdSGreg Clayton             }
142f9765acdSGreg Clayton         }
143f9765acdSGreg Clayton         if (flag_bits == 0)
144f9765acdSGreg Clayton             flag_bits = KDP_LOG_DEFAULT;
145f9765acdSGreg Clayton         log->GetMask().Reset(flag_bits);
146f9765acdSGreg Clayton         log->GetOptions().Reset(log_options);
147f9765acdSGreg Clayton     }
148f9765acdSGreg Clayton     return log;
149f9765acdSGreg Clayton }
150f9765acdSGreg Clayton 
151f9765acdSGreg Clayton void
152f9765acdSGreg Clayton ProcessKDPLog::ListLogCategories (Stream *strm)
153f9765acdSGreg Clayton {
154f9765acdSGreg Clayton     strm->Printf("Logging categories for '%s':\n"
155f9765acdSGreg Clayton         "\tall - turn on all available logging categories\n"
156f9765acdSGreg Clayton         "\tasync - log asynchronous activity\n"
157f9765acdSGreg Clayton         "\tbreak - log breakpoints\n"
158f9765acdSGreg Clayton         "\tcommunication - log communication activity\n"
159f9765acdSGreg Clayton         "\tdefault - enable the default set of logging categories for liblldb\n"
160f9765acdSGreg Clayton         "\tpackets - log gdb remote packets\n"
161f9765acdSGreg Clayton         "\tmemory - log memory reads and writes\n"
162f9765acdSGreg Clayton         "\tdata-short - log memory bytes for memory reads and writes for short transactions only\n"
163f9765acdSGreg Clayton         "\tdata-long - log memory bytes for memory reads and writes for all transactions\n"
164f9765acdSGreg Clayton         "\tprocess - log process events and activities\n"
165f9765acdSGreg Clayton         "\tthread - log thread events and activities\n"
166f9765acdSGreg Clayton         "\tstep - log step related activities\n"
167f9765acdSGreg Clayton         "\tverbose - enable verbose logging\n"
168f9765acdSGreg Clayton         "\twatch - log watchpoint related activities\n", ProcessKDP::GetPluginNameStatic());
169f9765acdSGreg Clayton }
170f9765acdSGreg Clayton 
171f9765acdSGreg Clayton 
172f9765acdSGreg Clayton void
173f9765acdSGreg Clayton ProcessKDPLog::LogIf (uint32_t mask, const char *format, ...)
174f9765acdSGreg Clayton {
175f9765acdSGreg Clayton     LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (mask));
176f9765acdSGreg Clayton     if (log)
177f9765acdSGreg Clayton     {
178f9765acdSGreg Clayton         va_list args;
179f9765acdSGreg Clayton         va_start (args, format);
180f9765acdSGreg Clayton         log->VAPrintf (format, args);
181f9765acdSGreg Clayton         va_end (args);
182f9765acdSGreg Clayton     }
183f9765acdSGreg Clayton }
184