Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 12 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Core/StreamFile.h" |
| 14 | |
| 15 | #include "ProcessGDBRemote.h" |
| 16 | |
| 17 | using namespace lldb; |
| 18 | using namespace lldb_private; |
| 19 | |
| 20 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 21 | // We want to avoid global constructors where code needs to be run so here we |
| 22 | // control access to our static g_log_sp by hiding it in a singleton function |
| 23 | // that will construct the static g_lob_sp the first time this function is |
| 24 | // called. |
| 25 | static LogSP & |
| 26 | GetLog () |
| 27 | { |
| 28 | static LogSP g_log_sp; |
| 29 | return g_log_sp; |
| 30 | } |
| 31 | |
| 32 | LogSP |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (uint32_t mask) |
| 34 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 35 | LogSP log(GetLog ()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | if (log && mask) |
| 37 | { |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 38 | uint32_t log_mask = log->GetMask().Get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | if ((log_mask & mask) != mask) |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 40 | return LogSP(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | } |
| 42 | return log; |
| 43 | } |
| 44 | |
| 45 | void |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 46 | ProcessGDBRemoteLog::DisableLog (Args &args, Stream *feedback_strm) |
| 47 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 48 | LogSP log (GetLog ()); |
| 49 | if (log) |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 50 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 51 | uint32_t flag_bits = log->GetMask().Get(); |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 52 | const size_t argc = args.GetArgumentCount (); |
| 53 | for (size_t i = 0; i < argc; ++i) |
| 54 | { |
| 55 | const char *arg = args.GetArgumentAtIndex (i); |
| 56 | |
| 57 | |
| 58 | if (::strcasecmp (arg, "all") == 0 ) flag_bits &= ~GDBR_LOG_ALL; |
| 59 | else if (::strcasecmp (arg, "async") == 0 ) flag_bits &= ~GDBR_LOG_ASYNC; |
| 60 | else if (::strcasestr (arg, "break") == arg ) flag_bits &= ~GDBR_LOG_BREAKPOINTS; |
| 61 | else if (::strcasestr (arg, "comm") == arg ) flag_bits &= ~GDBR_LOG_COMM; |
| 62 | else if (::strcasecmp (arg, "default") == 0 ) flag_bits &= ~GDBR_LOG_DEFAULT; |
| 63 | else if (::strcasecmp (arg, "packets") == 0 ) flag_bits &= ~GDBR_LOG_PACKETS; |
| 64 | else if (::strcasecmp (arg, "memory") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY; |
| 65 | else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_SHORT; |
| 66 | else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_LONG; |
| 67 | else if (::strcasecmp (arg, "process") == 0 ) flag_bits &= ~GDBR_LOG_PROCESS; |
| 68 | else if (::strcasecmp (arg, "step") == 0 ) flag_bits &= ~GDBR_LOG_STEP; |
| 69 | else if (::strcasecmp (arg, "thread") == 0 ) flag_bits &= ~GDBR_LOG_THREAD; |
| 70 | else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits &= ~GDBR_LOG_VERBOSE; |
| 71 | else if (::strcasestr (arg, "watch") == arg ) flag_bits &= ~GDBR_LOG_WATCHPOINTS; |
| 72 | else |
| 73 | { |
| 74 | feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); |
| 75 | ListLogCategories (feedback_strm); |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | |
| 80 | if (flag_bits == 0) |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 81 | GetLog ().reset(); |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 82 | else |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 83 | log->GetMask().Reset (flag_bits); |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return; |
| 87 | } |
| 88 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 89 | LogSP |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | ProcessGDBRemoteLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm) |
| 91 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 92 | // Try see if there already is a log - that way we can reuse its settings. |
| 93 | // We could reuse the log in toto, but we don't know that the stream is the same. |
| 94 | uint32_t flag_bits; |
| 95 | LogSP log(GetLog ()); |
| 96 | if (log) |
| 97 | flag_bits = log->GetMask().Get(); |
| 98 | else |
| 99 | flag_bits = 0; |
| 100 | |
| 101 | // Now make a new log with this stream if one was provided |
| 102 | if (log_stream_sp) |
| 103 | { |
| 104 | log = make_shared<Log>(log_stream_sp); |
| 105 | GetLog () = log; |
| 106 | } |
| 107 | |
| 108 | if (log) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | { |
| 110 | uint32_t flag_bits = 0; |
| 111 | bool got_unknown_category = false; |
| 112 | const size_t argc = args.GetArgumentCount(); |
| 113 | for (size_t i=0; i<argc; ++i) |
| 114 | { |
| 115 | const char *arg = args.GetArgumentAtIndex(i); |
| 116 | |
| 117 | if (::strcasecmp (arg, "all") == 0 ) flag_bits |= GDBR_LOG_ALL; |
Greg Clayton | b4d1d33 | 2010-09-03 21:14:27 +0000 | [diff] [blame] | 118 | else if (::strcasecmp (arg, "async") == 0 ) flag_bits |= GDBR_LOG_ASYNC; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | else if (::strcasestr (arg, "break") == arg ) flag_bits |= GDBR_LOG_BREAKPOINTS; |
Greg Clayton | b4d1d33 | 2010-09-03 21:14:27 +0000 | [diff] [blame] | 120 | else if (::strcasestr (arg, "comm") == arg ) flag_bits |= GDBR_LOG_COMM; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 121 | else if (::strcasecmp (arg, "default") == 0 ) flag_bits |= GDBR_LOG_DEFAULT; |
| 122 | else if (::strcasecmp (arg, "packets") == 0 ) flag_bits |= GDBR_LOG_PACKETS; |
| 123 | else if (::strcasecmp (arg, "memory") == 0 ) flag_bits |= GDBR_LOG_MEMORY; |
| 124 | else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_SHORT; |
| 125 | else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_LONG; |
| 126 | else if (::strcasecmp (arg, "process") == 0 ) flag_bits |= GDBR_LOG_PROCESS; |
| 127 | else if (::strcasecmp (arg, "step") == 0 ) flag_bits |= GDBR_LOG_STEP; |
| 128 | else if (::strcasecmp (arg, "thread") == 0 ) flag_bits |= GDBR_LOG_THREAD; |
| 129 | else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits |= GDBR_LOG_VERBOSE; |
| 130 | else if (::strcasestr (arg, "watch") == arg ) flag_bits |= GDBR_LOG_WATCHPOINTS; |
| 131 | else |
| 132 | { |
| 133 | feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); |
| 134 | if (got_unknown_category == false) |
| 135 | { |
| 136 | got_unknown_category = true; |
| 137 | ListLogCategories (feedback_strm); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | if (flag_bits == 0) |
| 142 | flag_bits = GDBR_LOG_DEFAULT; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 143 | log->GetMask().Reset(flag_bits); |
| 144 | log->GetOptions().Reset(log_options); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 145 | } |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 146 | return log; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
| 150 | ProcessGDBRemoteLog::ListLogCategories (Stream *strm) |
| 151 | { |
| 152 | strm->Printf("Logging categories for '%s':\n" |
| 153 | "\tall - turn on all available logging categories\n" |
Greg Clayton | b4d1d33 | 2010-09-03 21:14:27 +0000 | [diff] [blame] | 154 | "\tasync - log asynchronous activity\n" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | "\tbreak - log breakpoints\n" |
Greg Clayton | b4d1d33 | 2010-09-03 21:14:27 +0000 | [diff] [blame] | 156 | "\tcommunication - log communication activity\n" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | "\tdefault - enable the default set of logging categories for liblldb\n" |
| 158 | "\tpackets - log gdb remote packets\n" |
| 159 | "\tmemory - log memory reads and writes\n" |
| 160 | "\tdata-short - log memory bytes for memory reads and writes for short transactions only\n" |
| 161 | "\tdata-long - log memory bytes for memory reads and writes for all transactions\n" |
| 162 | "\tprocess - log process events and activities\n" |
| 163 | "\tthread - log thread events and activities\n" |
| 164 | "\tstep - log step related activities\n" |
| 165 | "\tverbose - enable verbose loggging\n" |
| 166 | "\twatch - log watchpoint related activities\n", ProcessGDBRemote::GetPluginNameStatic()); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | void |
| 171 | ProcessGDBRemoteLog::LogIf (uint32_t mask, const char *format, ...) |
| 172 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 173 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (mask)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | if (log) |
| 175 | { |
| 176 | va_list args; |
| 177 | va_start (args, format); |
| 178 | log->VAPrintf (format, args); |
| 179 | va_end (args); |
| 180 | } |
| 181 | } |