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