Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Runtime tracing API |
| 3 | * |
| 4 | * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation, version 2.1 |
| 9 | * exclusively. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <common/buffer.h> |
| 22 | #include <common/compat.h> |
| 23 | #include <common/config.h> |
| 24 | #include <common/ist.h> |
| 25 | #include <common/mini-clist.h> |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 26 | #include <proto/cli.h> |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 27 | #include <proto/log.h> |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 28 | #include <proto/sink.h> |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 29 | #include <proto/trace.h> |
| 30 | |
| 31 | struct list trace_sources = LIST_HEAD_INIT(trace_sources); |
Willy Tarreau | 88ebd40 | 2019-08-19 15:55:34 +0200 | [diff] [blame] | 32 | THREAD_LOCAL struct buffer trace_buf = { }; |
| 33 | |
| 34 | /* allocates the trace buffers. Returns 0 in case of failure. It is safe to |
| 35 | * call to call this function multiple times if the size changes. |
| 36 | */ |
| 37 | static int alloc_trace_buffers_per_thread() |
| 38 | { |
| 39 | chunk_init(&trace_buf, my_realloc2(trace_buf.area, global.tune.bufsize), global.tune.bufsize); |
| 40 | return !!trash.area; |
| 41 | } |
| 42 | |
| 43 | static void free_trace_buffers_per_thread() |
| 44 | { |
| 45 | chunk_destroy(&trace_buf); |
| 46 | } |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | 88ebd40 | 2019-08-19 15:55:34 +0200 | [diff] [blame] | 48 | REGISTER_PER_THREAD_ALLOC(alloc_trace_buffers_per_thread); |
| 49 | REGISTER_PER_THREAD_FREE(free_trace_buffers_per_thread); |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 50 | |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 51 | /* pick the lowest non-null argument with a non-null arg_def mask */ |
| 52 | static inline const void *trace_pick_arg(uint32_t arg_def, const void *a1, const void *a2, const void *a3, const void *a4) |
| 53 | { |
| 54 | if (arg_def & 0x0000FFFF) { |
| 55 | if ((arg_def & 0x000000FF) && a1) |
| 56 | return a1; |
| 57 | if ((arg_def & 0x0000FF00) && a2) |
| 58 | return a2; |
| 59 | } |
| 60 | |
| 61 | if (arg_def & 0xFFFF0000) { |
| 62 | if ((arg_def & 0x00FF0000) && a3) |
| 63 | return a3; |
| 64 | if ((arg_def & 0xFF000000) && a4) |
| 65 | return a4; |
| 66 | } |
| 67 | |
| 68 | return NULL; |
| 69 | } |
| 70 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 71 | /* write a message for the given trace source */ |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 72 | void __trace(enum trace_level level, uint64_t mask, struct trace_source *src, const struct ist where, |
| 73 | const void *a1, const void *a2, const void *a3, const void *a4, |
| 74 | void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src, const struct ist where, |
| 75 | const void *a1, const void *a2, const void *a3, const void *a4), |
| 76 | const struct ist msg) |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 77 | { |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 78 | const struct listener *li = NULL; |
| 79 | const struct proxy *fe = NULL; |
| 80 | const struct proxy *be = NULL; |
| 81 | const struct server *srv = NULL; |
| 82 | const struct session *sess = NULL; |
| 83 | const struct stream *strm = NULL; |
| 84 | const struct connection *conn = NULL; |
| 85 | const void *lockon_ptr = NULL; |
Willy Tarreau | a9f5b96 | 2019-08-28 10:08:58 +0200 | [diff] [blame] | 86 | char tnum[4]; |
Willy Tarreau | 419bd49 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 87 | struct ist line[8]; |
| 88 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 89 | if (likely(src->state == TRACE_STATE_STOPPED)) |
| 90 | return; |
| 91 | |
| 92 | /* check that at least one action is interested by this event */ |
| 93 | if (((src->report_events | src->start_events | src->pause_events | src->stop_events) & mask) == 0) |
| 94 | return; |
| 95 | |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 96 | /* retrieve available information from the caller's arguments */ |
| 97 | if (src->arg_def & TRC_ARGS_CONN) |
| 98 | conn = trace_pick_arg(src->arg_def & TRC_ARGS_CONN, a1, a2, a3, a4); |
| 99 | |
| 100 | if (src->arg_def & TRC_ARGS_SESS) |
| 101 | sess = trace_pick_arg(src->arg_def & TRC_ARGS_SESS, a1, a2, a3, a4); |
| 102 | |
| 103 | if (src->arg_def & TRC_ARGS_STRM) |
| 104 | strm = trace_pick_arg(src->arg_def & TRC_ARGS_STRM, a1, a2, a3, a4); |
| 105 | |
| 106 | if (!sess && strm) |
| 107 | sess = strm->sess; |
| 108 | else if (!sess && conn) |
| 109 | sess = conn->owner; |
| 110 | |
| 111 | if (sess) { |
| 112 | fe = sess->fe; |
| 113 | li = sess->listener; |
| 114 | } |
| 115 | |
| 116 | if (!li && conn) |
| 117 | li = objt_listener(conn->target); |
| 118 | |
| 119 | if (li && !fe) |
| 120 | fe = li->bind_conf->frontend; |
| 121 | |
| 122 | if (strm) { |
| 123 | be = strm->be; |
| 124 | srv = strm->srv_conn; |
| 125 | } |
| 126 | |
| 127 | if (!srv && conn) |
| 128 | srv = objt_server(conn->target); |
| 129 | |
| 130 | if (srv && !be) |
| 131 | be = srv->proxy; |
| 132 | |
| 133 | if (!be && conn) |
| 134 | be = objt_proxy(conn->target); |
| 135 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 136 | /* TODO: add handling of filters here, return if no match (not even update states) */ |
| 137 | |
| 138 | /* check if we need to start the trace now */ |
| 139 | if (src->state == TRACE_STATE_WAITING) { |
| 140 | if ((src->start_events & mask) == 0) |
| 141 | return; |
| 142 | |
| 143 | /* TODO: add update of lockon+lockon_ptr here */ |
| 144 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING); |
| 145 | } |
| 146 | |
Willy Tarreau | d8b99ed | 2019-08-21 17:05:46 +0200 | [diff] [blame] | 147 | /* we may want to lock on a particular object */ |
| 148 | if (src->lockon != TRACE_LOCKON_NOTHING) { |
| 149 | switch (src->lockon) { |
| 150 | case TRACE_LOCKON_BACKEND: lockon_ptr = be; break; |
| 151 | case TRACE_LOCKON_CONNECTION: lockon_ptr = conn; break; |
| 152 | case TRACE_LOCKON_FRONTEND: lockon_ptr = fe; break; |
| 153 | case TRACE_LOCKON_LISTENER: lockon_ptr = li; break; |
| 154 | case TRACE_LOCKON_SERVER: lockon_ptr = srv; break; |
| 155 | case TRACE_LOCKON_SESSION: lockon_ptr = sess; break; |
| 156 | case TRACE_LOCKON_STREAM: lockon_ptr = strm; break; |
| 157 | case TRACE_LOCKON_THREAD: lockon_ptr = ti; break; |
| 158 | case TRACE_LOCKON_ARG1: lockon_ptr = a1; break; |
| 159 | case TRACE_LOCKON_ARG2: lockon_ptr = a2; break; |
| 160 | case TRACE_LOCKON_ARG3: lockon_ptr = a3; break; |
| 161 | case TRACE_LOCKON_ARG4: lockon_ptr = a4; break; |
| 162 | default: break; // silence stupid gcc -Wswitch |
| 163 | } |
| 164 | |
| 165 | if (src->lockon_ptr && src->lockon_ptr != lockon_ptr) |
| 166 | return; |
| 167 | |
| 168 | if (!src->lockon_ptr && lockon_ptr && src->state == TRACE_STATE_RUNNING) |
| 169 | HA_ATOMIC_STORE(&src->lockon_ptr, lockon_ptr); |
| 170 | } |
| 171 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 172 | /* here the trace is running and is tracking a desired item */ |
| 173 | |
Willy Tarreau | 5da4088 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 174 | if ((src->report_events & mask) == 0 || level > src->level) |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 175 | goto end; |
| 176 | |
Willy Tarreau | 419bd49 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 177 | /* log the logging location truncated to 10 chars from the right so that |
| 178 | * the line number and the end of the file name are there. |
| 179 | */ |
| 180 | line[0] = ist("["); |
Willy Tarreau | a9f5b96 | 2019-08-28 10:08:58 +0200 | [diff] [blame] | 181 | tnum[0] = '0' + tid / 10; |
| 182 | tnum[1] = '0' + tid % 10; |
| 183 | tnum[2] = '|'; |
| 184 | tnum[3] = 0; |
| 185 | line[1] = ist(tnum); |
| 186 | line[2] = src->name; |
| 187 | line[3] = ist("|"); |
| 188 | line[4] = where; |
| 189 | if (line[4].len > 13) { |
| 190 | line[4].ptr += (line[4].len - 13); |
| 191 | line[4].len = 13; |
Willy Tarreau | 419bd49 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 192 | } |
Willy Tarreau | a9f5b96 | 2019-08-28 10:08:58 +0200 | [diff] [blame] | 193 | line[5] = ist("] "); |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 194 | |
Willy Tarreau | 3da0026 | 2019-08-28 07:03:58 +0200 | [diff] [blame] | 195 | if (!cb) |
| 196 | cb = src->default_cb; |
| 197 | |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 198 | if (cb) { |
| 199 | /* decode function passed, we want to pre-fill the |
| 200 | * buffer with the message and let the decode function |
| 201 | * do its job, possibly even overwriting it. |
| 202 | */ |
| 203 | b_reset(&trace_buf); |
| 204 | b_istput(&trace_buf, msg); |
| 205 | cb(level, mask, src, where, a1, a2, a3, a4); |
Willy Tarreau | a9f5b96 | 2019-08-28 10:08:58 +0200 | [diff] [blame] | 206 | line[6].ptr = trace_buf.area; |
| 207 | line[6].len = trace_buf.data; |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 208 | } |
| 209 | else |
Willy Tarreau | a9f5b96 | 2019-08-28 10:08:58 +0200 | [diff] [blame] | 210 | line[6] = msg; |
Willy Tarreau | 419bd49 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 211 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 212 | if (src->sink) |
Willy Tarreau | a9f5b96 | 2019-08-28 10:08:58 +0200 | [diff] [blame] | 213 | sink_write(src->sink, line, 7); |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 214 | |
| 215 | end: |
| 216 | /* check if we need to stop the trace now */ |
| 217 | if ((src->stop_events & mask) != 0) { |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 218 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 219 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED); |
| 220 | } |
| 221 | else if ((src->pause_events & mask) != 0) { |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 222 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 223 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING); |
| 224 | } |
| 225 | } |
| 226 | |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 227 | struct trace_source *trace_find_source(const char *name) |
| 228 | { |
| 229 | struct trace_source *src; |
| 230 | const struct ist iname = ist(name); |
| 231 | |
| 232 | list_for_each_entry(src, &trace_sources, source_link) |
| 233 | if (isteq(src->name, iname)) |
| 234 | return src; |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | const struct trace_event *trace_find_event(const struct trace_event *ev, const char *name) |
| 239 | { |
| 240 | for (; ev && ev->mask; ev++) |
| 241 | if (strcmp(ev->name, name) == 0) |
| 242 | return ev; |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | /* parse the command, returns 1 if a message is returned, otherwise zero */ |
| 247 | static int cli_parse_trace(char **args, char *payload, struct appctx *appctx, void *private) |
| 248 | { |
| 249 | struct trace_source *src; |
| 250 | uint64_t *ev_ptr = NULL; |
| 251 | |
| 252 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 253 | return 1; |
| 254 | |
| 255 | if (!*args[1]) { |
| 256 | /* no arg => report the list of supported sources as a warning */ |
| 257 | chunk_printf(&trash, |
| 258 | "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n" |
| 259 | " [.] 0 : not a source, will immediately stop all traces\n" |
| 260 | ); |
| 261 | |
| 262 | list_for_each_entry(src, &trace_sources, source_link) |
| 263 | chunk_appendf(&trash, " [%c] %-10s : %s\n", trace_state_char(src->state), src->name.ptr, src->desc); |
| 264 | |
| 265 | trash.area[trash.data] = 0; |
| 266 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 267 | } |
| 268 | |
| 269 | if (strcmp(args[1], "0") == 0) { |
| 270 | /* emergency stop of all traces */ |
| 271 | list_for_each_entry(src, &trace_sources, source_link) |
| 272 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED); |
| 273 | return cli_msg(appctx, LOG_NOTICE, "All traces now stopped"); |
| 274 | } |
| 275 | |
| 276 | src = trace_find_source(args[1]); |
| 277 | if (!src) |
| 278 | return cli_err(appctx, "No such trace source"); |
| 279 | |
| 280 | if (!*args[2]) { |
| 281 | return cli_msg(appctx, LOG_WARNING, |
| 282 | "Supported commands:\n" |
| 283 | " event : list/enable/disable source-specific event reporting\n" |
| 284 | //" filter : list/enable/disable generic filters\n" |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame^] | 285 | " level : list/set trace reporting level\n" |
Willy Tarreau | 60e4c9f | 2019-08-20 19:24:10 +0200 | [diff] [blame] | 286 | " lock : automatic lock on thread/connection/stream/...\n" |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 287 | " pause : pause and automatically restart after a specific event\n" |
| 288 | " sink : list/set event sinks\n" |
| 289 | " start : start immediately or after a specific event\n" |
| 290 | " stop : stop immediately or after a specific event\n" |
| 291 | ); |
| 292 | } |
| 293 | else if ((strcmp(args[2], "event") == 0 && (ev_ptr = &src->report_events)) || |
| 294 | (strcmp(args[2], "pause") == 0 && (ev_ptr = &src->pause_events)) || |
| 295 | (strcmp(args[2], "start") == 0 && (ev_ptr = &src->start_events)) || |
| 296 | (strcmp(args[2], "stop") == 0 && (ev_ptr = &src->stop_events))) { |
| 297 | const struct trace_event *ev; |
| 298 | const char *name = args[3]; |
| 299 | int neg = 0; |
| 300 | int i; |
| 301 | |
| 302 | /* skip prefix '!', '-', '+' and remind negation */ |
| 303 | while (*name) { |
| 304 | if (*name == '!' || *name == '-') |
| 305 | neg = 1; |
| 306 | else if (*name == '+') |
| 307 | neg = 0; |
| 308 | else |
| 309 | break; |
| 310 | name++; |
| 311 | } |
| 312 | |
| 313 | if (!*name) { |
| 314 | chunk_printf(&trash, "Supported events for source %s (+=enabled, -=disabled):\n", src->name.ptr); |
| 315 | if (ev_ptr != &src->report_events) |
| 316 | chunk_appendf(&trash, " - now : don't wait for events, immediately change the state\n"); |
| 317 | chunk_appendf(&trash, " - none : disable all event types\n"); |
| 318 | chunk_appendf(&trash, " - any : enable all event types\n"); |
| 319 | for (i = 0; src->known_events && src->known_events[i].mask; i++) { |
| 320 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 321 | trace_event_char(*ev_ptr, src->known_events[i].mask), |
| 322 | src->known_events[i].name, src->known_events[i].desc); |
| 323 | } |
| 324 | trash.area[trash.data] = 0; |
| 325 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 326 | } |
| 327 | |
| 328 | if (strcmp(name, "now") == 0 && ev_ptr != &src->report_events) { |
| 329 | HA_ATOMIC_STORE(ev_ptr, 0); |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 330 | if (ev_ptr == &src->pause_events) { |
| 331 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 332 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_WAITING); |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 333 | } |
| 334 | else if (ev_ptr == &src->start_events) { |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 335 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_RUNNING); |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 336 | } |
| 337 | else if (ev_ptr == &src->stop_events) { |
| 338 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 339 | HA_ATOMIC_STORE(&src->state, TRACE_STATE_STOPPED); |
Willy Tarreau | beadb5c | 2019-08-20 18:57:48 +0200 | [diff] [blame] | 340 | } |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | if (strcmp(name, "none") == 0) |
| 345 | HA_ATOMIC_STORE(ev_ptr, 0); |
| 346 | else if (strcmp(name, "any") == 0) |
| 347 | HA_ATOMIC_STORE(ev_ptr, ~0); |
| 348 | else { |
| 349 | ev = trace_find_event(src->known_events, name); |
| 350 | if (!ev) |
| 351 | return cli_err(appctx, "No such trace event"); |
| 352 | |
| 353 | if (!neg) |
| 354 | HA_ATOMIC_OR(ev_ptr, ev->mask); |
| 355 | else |
| 356 | HA_ATOMIC_AND(ev_ptr, ~ev->mask); |
| 357 | } |
| 358 | } |
| 359 | else if (strcmp(args[2], "sink") == 0) { |
| 360 | const char *name = args[3]; |
| 361 | struct sink *sink; |
| 362 | |
| 363 | if (!*name) { |
| 364 | chunk_printf(&trash, "Supported sinks for source %s (*=current):\n", src->name.ptr); |
| 365 | chunk_appendf(&trash, " %c none : no sink\n", src->sink ? ' ' : '*'); |
| 366 | list_for_each_entry(sink, &sink_list, sink_list) { |
| 367 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 368 | src->sink == sink ? '*' : ' ', |
| 369 | sink->name, sink->desc); |
| 370 | } |
| 371 | trash.area[trash.data] = 0; |
| 372 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 373 | } |
| 374 | |
| 375 | if (strcmp(name, "none") == 0) |
| 376 | sink = NULL; |
| 377 | else { |
| 378 | sink = sink_find(name); |
| 379 | if (!sink) |
| 380 | return cli_err(appctx, "No such sink"); |
| 381 | } |
| 382 | |
| 383 | HA_ATOMIC_STORE(&src->sink, sink); |
| 384 | } |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 385 | else if (strcmp(args[2], "level") == 0) { |
| 386 | const char *name = args[3]; |
| 387 | |
| 388 | if (!*name) { |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame^] | 389 | chunk_printf(&trash, "Supported trace levels for source %s:\n", src->name.ptr); |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 390 | chunk_appendf(&trash, " %c user : information useful to the end user\n", |
| 391 | src->level == TRACE_LEVEL_USER ? '*' : ' '); |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame^] | 392 | chunk_appendf(&trash, " %c proto : also protocol-level updates\n", |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 393 | src->level == TRACE_LEVEL_PROTO ? '*' : ' '); |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame^] | 394 | chunk_appendf(&trash, " %c state : also report internal state changes\n", |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 395 | src->level == TRACE_LEVEL_STATE ? '*' : ' '); |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame^] | 396 | chunk_appendf(&trash, " %c data : also report data transfers\n", |
| 397 | src->level == TRACE_LEVEL_DATA ? '*' : ' '); |
| 398 | chunk_appendf(&trash, " %c developer : also report information useful only to the developer\n", |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 399 | src->level == TRACE_LEVEL_DEVELOPER ? '*' : ' '); |
| 400 | trash.area[trash.data] = 0; |
| 401 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 402 | } |
| 403 | |
| 404 | if (strcmp(name, "user") == 0) |
| 405 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_USER); |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 406 | else if (strcmp(name, "proto") == 0) |
| 407 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_PROTO); |
| 408 | else if (strcmp(name, "state") == 0) |
| 409 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_STATE); |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame^] | 410 | else if (strcmp(name, "data") == 0) |
| 411 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DATA); |
Willy Tarreau | aaaf411 | 2019-08-12 17:57:57 +0200 | [diff] [blame] | 412 | else if (strcmp(name, "developer") == 0) |
| 413 | HA_ATOMIC_STORE(&src->level, TRACE_LEVEL_DEVELOPER); |
| 414 | else |
| 415 | return cli_err(appctx, "No such trace level"); |
| 416 | } |
Willy Tarreau | 60e4c9f | 2019-08-20 19:24:10 +0200 | [diff] [blame] | 417 | else if (strcmp(args[2], "lock") == 0) { |
| 418 | const char *name = args[3]; |
| 419 | |
| 420 | if (!*name) { |
| 421 | chunk_printf(&trash, "Supported lock-on criteria for source %s:\n", src->name.ptr); |
| 422 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) |
| 423 | chunk_appendf(&trash, " %c backend : lock on the backend that started the trace\n", |
| 424 | src->lockon == TRACE_LOCKON_BACKEND ? '*' : ' '); |
| 425 | |
| 426 | if (src->arg_def & TRC_ARGS_CONN) |
| 427 | chunk_appendf(&trash, " %c connection : lock on the connection that started the trace\n", |
| 428 | src->lockon == TRACE_LOCKON_CONNECTION ? '*' : ' '); |
| 429 | |
| 430 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) |
| 431 | chunk_appendf(&trash, " %c frontend : lock on the frontend that started the trace\n", |
| 432 | src->lockon == TRACE_LOCKON_FRONTEND ? '*' : ' '); |
| 433 | |
| 434 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) |
| 435 | chunk_appendf(&trash, " %c listener : lock on the listener that started the trace\n", |
| 436 | src->lockon == TRACE_LOCKON_LISTENER ? '*' : ' '); |
| 437 | |
| 438 | chunk_appendf(&trash, " %c nothing : do not lock on anything\n", |
| 439 | src->lockon == TRACE_LOCKON_NOTHING ? '*' : ' '); |
| 440 | |
| 441 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) |
| 442 | chunk_appendf(&trash, " %c server : lock on the server that started the trace\n", |
| 443 | src->lockon == TRACE_LOCKON_SERVER ? '*' : ' '); |
| 444 | |
| 445 | if (src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) |
| 446 | chunk_appendf(&trash, " %c session : lock on the session that started the trace\n", |
| 447 | src->lockon == TRACE_LOCKON_SESSION ? '*' : ' '); |
| 448 | |
| 449 | if (src->arg_def & TRC_ARGS_STRM) |
| 450 | chunk_appendf(&trash, " %c stream : lock on the stream that started the trace\n", |
| 451 | src->lockon == TRACE_LOCKON_STREAM ? '*' : ' '); |
| 452 | |
| 453 | chunk_appendf(&trash, " %c thread : lock on the thread that started the trace\n", |
| 454 | src->lockon == TRACE_LOCKON_THREAD ? '*' : ' '); |
| 455 | |
| 456 | if (src->lockon_args && src->lockon_args[0].name) |
| 457 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 458 | src->lockon == TRACE_LOCKON_ARG1 ? '*' : ' ', |
| 459 | src->lockon_args[0].name, src->lockon_args[0].desc); |
| 460 | |
| 461 | if (src->lockon_args && src->lockon_args[1].name) |
| 462 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 463 | src->lockon == TRACE_LOCKON_ARG2 ? '*' : ' ', |
| 464 | src->lockon_args[1].name, src->lockon_args[1].desc); |
| 465 | |
| 466 | if (src->lockon_args && src->lockon_args[2].name) |
| 467 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 468 | src->lockon == TRACE_LOCKON_ARG3 ? '*' : ' ', |
| 469 | src->lockon_args[2].name, src->lockon_args[2].desc); |
| 470 | |
| 471 | if (src->lockon_args && src->lockon_args[3].name) |
| 472 | chunk_appendf(&trash, " %c %-10s : %s\n", |
| 473 | src->lockon == TRACE_LOCKON_ARG4 ? '*' : ' ', |
| 474 | src->lockon_args[3].name, src->lockon_args[3].desc); |
| 475 | |
| 476 | trash.area[trash.data] = 0; |
| 477 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 478 | } |
| 479 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "backend") == 0) { |
| 480 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_BACKEND); |
| 481 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 482 | } |
| 483 | else if ((src->arg_def & TRC_ARGS_CONN) && strcmp(name, "connection") == 0) { |
| 484 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_CONNECTION); |
| 485 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 486 | } |
| 487 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "frontend") == 0) { |
| 488 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_FRONTEND); |
| 489 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 490 | } |
| 491 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "listener") == 0) { |
| 492 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_LISTENER); |
| 493 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 494 | } |
| 495 | else if (strcmp(name, "nothing") == 0) { |
| 496 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_NOTHING); |
| 497 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 498 | } |
| 499 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_STRM)) && strcmp(name, "server") == 0) { |
| 500 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SERVER); |
| 501 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 502 | } |
| 503 | else if ((src->arg_def & (TRC_ARGS_CONN|TRC_ARGS_SESS|TRC_ARGS_STRM)) && strcmp(name, "session") == 0) { |
| 504 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_SESSION); |
| 505 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 506 | } |
| 507 | else if ((src->arg_def & TRC_ARGS_STRM) && strcmp(name, "stream") == 0) { |
| 508 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_STREAM); |
| 509 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 510 | } |
| 511 | else if (strcmp(name, "thread") == 0) { |
| 512 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_THREAD); |
| 513 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 514 | } |
| 515 | else if (src->lockon_args && src->lockon_args[0].name && strcmp(name, src->lockon_args[0].name) == 0) { |
| 516 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG1); |
| 517 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 518 | } |
| 519 | else if (src->lockon_args && src->lockon_args[1].name && strcmp(name, src->lockon_args[1].name) == 0) { |
| 520 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG2); |
| 521 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 522 | } |
| 523 | else if (src->lockon_args && src->lockon_args[2].name && strcmp(name, src->lockon_args[2].name) == 0) { |
| 524 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG3); |
| 525 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 526 | } |
| 527 | else if (src->lockon_args && src->lockon_args[3].name && strcmp(name, src->lockon_args[3].name) == 0) { |
| 528 | HA_ATOMIC_STORE(&src->lockon, TRACE_LOCKON_ARG4); |
| 529 | HA_ATOMIC_STORE(&src->lockon_ptr, NULL); |
| 530 | } |
| 531 | else |
| 532 | return cli_err(appctx, "Unsupported lock-on criterion"); |
| 533 | } |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 534 | else |
| 535 | return cli_err(appctx, "Unknown trace keyword"); |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
Willy Tarreau | 85b1575 | 2019-08-12 16:44:33 +0200 | [diff] [blame] | 540 | /* parse the command, returns 1 if a message is returned, otherwise zero */ |
| 541 | static int cli_parse_show_trace(char **args, char *payload, struct appctx *appctx, void *private) |
| 542 | { |
| 543 | struct trace_source *src; |
| 544 | const struct sink *sink; |
| 545 | int i; |
| 546 | |
| 547 | args++; // make args[1] the 1st arg |
| 548 | |
| 549 | if (!*args[1]) { |
| 550 | /* no arg => report the list of supported sources */ |
| 551 | chunk_printf(&trash, |
| 552 | "Supported trace sources and states (.=stopped, w=waiting, R=running) :\n" |
| 553 | ); |
| 554 | |
| 555 | list_for_each_entry(src, &trace_sources, source_link) { |
| 556 | sink = src->sink; |
| 557 | chunk_appendf(&trash, " [%c] %-10s -> %s [drp %u] [%s]\n", |
| 558 | trace_state_char(src->state), src->name.ptr, |
| 559 | sink ? sink->name : "none", |
| 560 | sink ? sink->ctx.dropped : 0, |
| 561 | src->desc); |
| 562 | } |
| 563 | |
| 564 | trash.area[trash.data] = 0; |
| 565 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 566 | } |
| 567 | |
| 568 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 569 | return 1; |
| 570 | |
| 571 | src = trace_find_source(args[1]); |
| 572 | if (!src) |
| 573 | return cli_err(appctx, "No such trace source"); |
| 574 | |
| 575 | sink = src->sink; |
| 576 | chunk_printf(&trash, "Trace status for %s:\n", src->name.ptr); |
| 577 | chunk_appendf(&trash, " - sink: %s [%u dropped]\n", |
| 578 | sink ? sink->name : "none", sink ? sink->ctx.dropped : 0); |
| 579 | |
| 580 | chunk_appendf(&trash, " - event name : report start stop pause\n"); |
| 581 | for (i = 0; src->known_events && src->known_events[i].mask; i++) { |
| 582 | chunk_appendf(&trash, " %-10s : %c %c %c %c\n", |
| 583 | src->known_events[i].name, |
| 584 | trace_event_char(src->report_events, src->known_events[i].mask), |
| 585 | trace_event_char(src->start_events, src->known_events[i].mask), |
| 586 | trace_event_char(src->stop_events, src->known_events[i].mask), |
| 587 | trace_event_char(src->pause_events, src->known_events[i].mask)); |
| 588 | } |
| 589 | |
| 590 | trash.area[trash.data] = 0; |
| 591 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 592 | } |
| 593 | |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 594 | static struct cli_kw_list cli_kws = {{ },{ |
| 595 | { { "trace", NULL }, "trace <module> [cmd [args...]] : manage live tracing", cli_parse_trace, NULL, NULL }, |
Willy Tarreau | 85b1575 | 2019-08-12 16:44:33 +0200 | [diff] [blame] | 596 | { { "show", "trace", NULL }, "show trace [<module>] : show live tracing state", cli_parse_show_trace, NULL, NULL }, |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 597 | {{},} |
| 598 | }}; |
| 599 | |
| 600 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 601 | |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 602 | /* |
| 603 | * Local variables: |
| 604 | * c-indent-level: 8 |
| 605 | * c-basic-offset: 8 |
| 606 | * End: |
| 607 | */ |