1 //===-- ProcessGDBRemoteLog.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 #include "ProcessGDBRemoteLog.h"
11 
12 #include "lldb/Interpreter/Args.h"
13 #include "lldb/Core/StreamFile.h"
14 
15 #include "ProcessGDBRemote.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 
21 static Log* g_log = NULL; // Leak for now as auto_ptr was being cleaned up
22                           // by global constructors before other threads
23                           // were done with it.
24 Log *
25 ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (uint32_t mask)
26 {
27     Log *log = g_log;
28     if (log && mask)
29     {
30         uint32_t log_mask = log->GetMask().Get();
31         if ((log_mask & mask) != mask)
32             return NULL;
33     }
34     return log;
35 }
36 
37 void
38 ProcessGDBRemoteLog::DeleteLog ()
39 {
40     if (g_log)
41     {
42         delete g_log;
43         g_log = NULL;
44     }
45 }
46 
47 void
48 ProcessGDBRemoteLog::DisableLog (Args &args, Stream *feedback_strm)
49 {
50     if (g_log)
51     {
52         uint32_t flag_bits = g_log->GetMask().Get();
53         const size_t argc = args.GetArgumentCount ();
54         for (size_t i = 0; i < argc; ++i)
55         {
56             const char *arg = args.GetArgumentAtIndex (i);
57 
58 
59             if      (::strcasecmp (arg, "all")        == 0   ) flag_bits &= ~GDBR_LOG_ALL;
60             else if (::strcasecmp (arg, "async")      == 0   ) flag_bits &= ~GDBR_LOG_ASYNC;
61             else if (::strcasestr (arg, "break")      == arg ) flag_bits &= ~GDBR_LOG_BREAKPOINTS;
62             else if (::strcasestr (arg, "comm")       == arg ) flag_bits &= ~GDBR_LOG_COMM;
63             else if (::strcasecmp (arg, "default")    == 0   ) flag_bits &= ~GDBR_LOG_DEFAULT;
64             else if (::strcasecmp (arg, "packets")    == 0   ) flag_bits &= ~GDBR_LOG_PACKETS;
65             else if (::strcasecmp (arg, "memory")     == 0   ) flag_bits &= ~GDBR_LOG_MEMORY;
66             else if (::strcasecmp (arg, "data-short") == 0   ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_SHORT;
67             else if (::strcasecmp (arg, "data-long")  == 0   ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_LONG;
68             else if (::strcasecmp (arg, "process")    == 0   ) flag_bits &= ~GDBR_LOG_PROCESS;
69             else if (::strcasecmp (arg, "step")       == 0   ) flag_bits &= ~GDBR_LOG_STEP;
70             else if (::strcasecmp (arg, "thread")     == 0   ) flag_bits &= ~GDBR_LOG_THREAD;
71             else if (::strcasecmp (arg, "verbose")    == 0   ) flag_bits &= ~GDBR_LOG_VERBOSE;
72             else if (::strcasestr (arg, "watch")      == arg ) flag_bits &= ~GDBR_LOG_WATCHPOINTS;
73             else
74             {
75                 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
76                 ListLogCategories (feedback_strm);
77             }
78 
79         }
80 
81         if (flag_bits == 0)
82             DeleteLog();
83         else
84             g_log->GetMask().Reset (flag_bits);
85     }
86 
87     return;
88 }
89 
90 Log *
91 ProcessGDBRemoteLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
92 {
93     DeleteLog ();
94     g_log = new Log (log_stream_sp);
95     if (g_log)
96     {
97         uint32_t flag_bits = 0;
98         bool got_unknown_category = false;
99         const size_t argc = args.GetArgumentCount();
100         for (size_t i=0; i<argc; ++i)
101         {
102             const char *arg = args.GetArgumentAtIndex(i);
103 
104             if      (::strcasecmp (arg, "all")        == 0   ) flag_bits |= GDBR_LOG_ALL;
105             else if (::strcasecmp (arg, "async")      == 0   ) flag_bits |= GDBR_LOG_ASYNC;
106             else if (::strcasestr (arg, "break")      == arg ) flag_bits |= GDBR_LOG_BREAKPOINTS;
107             else if (::strcasestr (arg, "comm")       == arg ) flag_bits |= GDBR_LOG_COMM;
108             else if (::strcasecmp (arg, "default")    == 0   ) flag_bits |= GDBR_LOG_DEFAULT;
109             else if (::strcasecmp (arg, "packets")    == 0   ) flag_bits |= GDBR_LOG_PACKETS;
110             else if (::strcasecmp (arg, "memory")     == 0   ) flag_bits |= GDBR_LOG_MEMORY;
111             else if (::strcasecmp (arg, "data-short") == 0   ) flag_bits |= GDBR_LOG_MEMORY_DATA_SHORT;
112             else if (::strcasecmp (arg, "data-long")  == 0   ) flag_bits |= GDBR_LOG_MEMORY_DATA_LONG;
113             else if (::strcasecmp (arg, "process")    == 0   ) flag_bits |= GDBR_LOG_PROCESS;
114             else if (::strcasecmp (arg, "step")       == 0   ) flag_bits |= GDBR_LOG_STEP;
115             else if (::strcasecmp (arg, "thread")     == 0   ) flag_bits |= GDBR_LOG_THREAD;
116             else if (::strcasecmp (arg, "verbose")    == 0   ) flag_bits |= GDBR_LOG_VERBOSE;
117             else if (::strcasestr (arg, "watch")      == arg ) flag_bits |= GDBR_LOG_WATCHPOINTS;
118             else
119             {
120                 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
121                 if (got_unknown_category == false)
122                 {
123                     got_unknown_category = true;
124                     ListLogCategories (feedback_strm);
125                 }
126             }
127         }
128         if (flag_bits == 0)
129             flag_bits = GDBR_LOG_DEFAULT;
130         g_log->GetMask().Reset(flag_bits);
131         g_log->GetOptions().Reset(log_options);
132     }
133     return g_log;
134 }
135 
136 void
137 ProcessGDBRemoteLog::ListLogCategories (Stream *strm)
138 {
139     strm->Printf("Logging categories for '%s':\n"
140         "\tall - turn on all available logging categories\n"
141         "\tasync - log asynchronous activity\n"
142         "\tbreak - log breakpoints\n"
143         "\tcommunication - log communication activity\n"
144         "\tdefault - enable the default set of logging categories for liblldb\n"
145         "\tpackets - log gdb remote packets\n"
146         "\tmemory - log memory reads and writes\n"
147         "\tdata-short - log memory bytes for memory reads and writes for short transactions only\n"
148         "\tdata-long - log memory bytes for memory reads and writes for all transactions\n"
149         "\tprocess - log process events and activities\n"
150         "\tthread - log thread events and activities\n"
151         "\tstep - log step related activities\n"
152         "\tverbose - enable verbose loggging\n"
153         "\twatch - log watchpoint related activities\n", ProcessGDBRemote::GetPluginNameStatic());
154 }
155 
156 
157 void
158 ProcessGDBRemoteLog::LogIf (uint32_t mask, const char *format, ...)
159 {
160     Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (mask);
161     if (log)
162     {
163         va_list args;
164         va_start (args, format);
165         log->VAPrintf (format, args);
166         va_end (args);
167     }
168 }
169