xref: /llvm-project/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp (revision ef3cf2b95460f12d1f69a4dd3babc74b9406d45a)
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().GetAllFlagBits();
31         if ((log_mask & mask) != mask)
32             return NULL;
33     }
34     return log;
35 }
36 
37 void
38 ProcessGDBRemoteLog::DisableLog ()
39 {
40     if (g_log)
41     {
42         delete g_log;
43         g_log = NULL;
44     }
45 }
46 
47 Log *
48 ProcessGDBRemoteLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
49 {
50     DisableLog ();
51     g_log = new Log (log_stream_sp);
52     if (g_log)
53     {
54         uint32_t flag_bits = 0;
55         bool got_unknown_category = false;
56         const size_t argc = args.GetArgumentCount();
57         for (size_t i=0; i<argc; ++i)
58         {
59             const char *arg = args.GetArgumentAtIndex(i);
60 
61             if      (::strcasecmp (arg, "all")        == 0   ) flag_bits |= GDBR_LOG_ALL;
62             else if (::strcasecmp (arg, "async")      == 0   ) flag_bits |= GDBR_LOG_ASYNC;
63             else if (::strcasestr (arg, "break")      == arg ) flag_bits |= GDBR_LOG_BREAKPOINTS;
64             else if (::strcasestr (arg, "comm")       == arg ) flag_bits |= GDBR_LOG_COMM;
65             else if (::strcasecmp (arg, "default")    == 0   ) flag_bits |= GDBR_LOG_DEFAULT;
66             else if (::strcasecmp (arg, "packets")    == 0   ) flag_bits |= GDBR_LOG_PACKETS;
67             else if (::strcasecmp (arg, "memory")     == 0   ) flag_bits |= GDBR_LOG_MEMORY;
68             else if (::strcasecmp (arg, "data-short") == 0   ) flag_bits |= GDBR_LOG_MEMORY_DATA_SHORT;
69             else if (::strcasecmp (arg, "data-long")  == 0   ) flag_bits |= GDBR_LOG_MEMORY_DATA_LONG;
70             else if (::strcasecmp (arg, "process")    == 0   ) flag_bits |= GDBR_LOG_PROCESS;
71             else if (::strcasecmp (arg, "step")       == 0   ) flag_bits |= GDBR_LOG_STEP;
72             else if (::strcasecmp (arg, "thread")     == 0   ) flag_bits |= GDBR_LOG_THREAD;
73             else if (::strcasecmp (arg, "verbose")    == 0   ) flag_bits |= GDBR_LOG_VERBOSE;
74             else if (::strcasestr (arg, "watch")      == arg ) flag_bits |= GDBR_LOG_WATCHPOINTS;
75             else
76             {
77                 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
78                 if (got_unknown_category == false)
79                 {
80                     got_unknown_category = true;
81                     ListLogCategories (feedback_strm);
82                 }
83             }
84         }
85         if (flag_bits == 0)
86             flag_bits = GDBR_LOG_DEFAULT;
87         g_log->GetMask().SetAllFlagBits(flag_bits);
88         g_log->GetOptions().SetAllFlagBits(log_options);
89     }
90     return g_log;
91 }
92 
93 void
94 ProcessGDBRemoteLog::ListLogCategories (Stream *strm)
95 {
96     strm->Printf("Logging categories for '%s':\n"
97         "\tall - turn on all available logging categories\n"
98         "\tasync - log asynchronous activity\n"
99         "\tbreak - log breakpoints\n"
100         "\tcommunication - log communication activity\n"
101         "\tdefault - enable the default set of logging categories for liblldb\n"
102         "\tpackets - log gdb remote packets\n"
103         "\tmemory - log memory reads and writes\n"
104         "\tdata-short - log memory bytes for memory reads and writes for short transactions only\n"
105         "\tdata-long - log memory bytes for memory reads and writes for all transactions\n"
106         "\tprocess - log process events and activities\n"
107         "\tthread - log thread events and activities\n"
108         "\tstep - log step related activities\n"
109         "\tverbose - enable verbose loggging\n"
110         "\twatch - log watchpoint related activities\n", ProcessGDBRemote::GetPluginNameStatic());
111 }
112 
113 
114 void
115 ProcessGDBRemoteLog::LogIf (uint32_t mask, const char *format, ...)
116 {
117     Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (mask);
118     if (log)
119     {
120         va_list args;
121         va_start (args, format);
122         log->VAPrintf (format, args);
123         va_end (args);
124     }
125 }
126