Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/trace.h |
| 3 | * This file provides functions for runtime tracing |
| 4 | * |
| 5 | * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_TRACE_H |
| 23 | #define _PROTO_TRACE_H |
| 24 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 25 | #include <haproxy/api.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 26 | #include <haproxy/tools.h> |
Willy Tarreau | eb6f701 | 2020-05-27 16:21:26 +0200 | [diff] [blame] | 27 | #include <import/ist.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 28 | #include <haproxy/list.h> |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 29 | #include <types/log.h> |
Willy Tarreau | ba2f73d | 2020-06-03 20:02:28 +0200 | [diff] [blame] | 30 | #include <haproxy/sink-t.h> |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 31 | #include <types/trace.h> |
| 32 | |
Willy Tarreau | 4ab2421 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 33 | /* Make a string from the location of the trace producer as "file:line" */ |
| 34 | #define TRC_LOC _TRC_LOC(__FILE__, __LINE__) |
| 35 | #define _TRC_LOC(f,l) __TRC_LOC(f, ":", l) |
| 36 | #define __TRC_LOC(f,c,l) f c #l |
| 37 | |
Willy Tarreau | c326ecc | 2019-08-28 10:35:16 +0200 | [diff] [blame] | 38 | /* truncate a macro arg list to exactly 5 args and replace missing ones with NULL */ |
| 39 | #define TRC_5ARGS(a1,a2,a3,a4,a5,...) DEFNULL(a1),DEFNULL(a2),DEFNULL(a3),DEFNULL(a4),DEFNULL(a5) |
| 40 | |
Willy Tarreau | 4ab2421 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 41 | /* For convenience, TRACE() alone uses the file's default TRACE_LEVEL, most |
Willy Tarreau | c326ecc | 2019-08-28 10:35:16 +0200 | [diff] [blame] | 42 | * likely TRACE_LEVEL_DEVELOPER, though the other explicit variants specify |
| 43 | * the desired level and will work when TRACE_LEVEL is not set. The 5 optional |
| 44 | * arguments are the 4 source-specific arguments that are passed to the cb() |
| 45 | * callback dedicated to decoding, and which may be used for special tracking. |
| 46 | * These 4 arguments as well as the cb() function pointer may all be NULL, or |
| 47 | * simply omitted (in which case they will be replaced by a NULL). This |
| 48 | * ordering allows many TRACE() calls to be placed using copy-paste and just |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 49 | * change the message at the beginning. Only TRACE_DEVEL(), TRACE_ENTER() and |
| 50 | * TRACE_LEAVE() will report the calling function's name. |
Willy Tarreau | 4ab2421 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 51 | */ |
Willy Tarreau | c326ecc | 2019-08-28 10:35:16 +0200 | [diff] [blame] | 52 | #define TRACE(msg, mask, ...) \ |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 53 | trace(TRACE_LEVEL, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg)) |
Willy Tarreau | c326ecc | 2019-08-28 10:35:16 +0200 | [diff] [blame] | 54 | |
| 55 | #define TRACE_USER(msg, mask, ...) \ |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 56 | trace(TRACE_LEVEL_USER, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg)) |
Willy Tarreau | 4ab2421 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 57 | |
Willy Tarreau | 2ea549b | 2019-08-29 08:01:48 +0200 | [diff] [blame] | 58 | #define TRACE_DATA(msg, mask, ...) \ |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 59 | trace(TRACE_LEVEL_DATA, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg)) |
Willy Tarreau | c326ecc | 2019-08-28 10:35:16 +0200 | [diff] [blame] | 60 | |
| 61 | #define TRACE_PROTO(msg, mask, ...) \ |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 62 | trace(TRACE_LEVEL_PROTO, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg)) |
Willy Tarreau | c326ecc | 2019-08-28 10:35:16 +0200 | [diff] [blame] | 63 | |
| 64 | #define TRACE_STATE(msg, mask, ...) \ |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 65 | trace(TRACE_LEVEL_STATE, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg)) |
Willy Tarreau | 4ab2421 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 66 | |
Willy Tarreau | c326ecc | 2019-08-28 10:35:16 +0200 | [diff] [blame] | 67 | #define TRACE_DEVEL(msg, mask, ...) \ |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 68 | trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg)) |
| 69 | |
| 70 | #define TRACE_ENTER(mask, ...) \ |
| 71 | trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist("entering")) |
| 72 | |
| 73 | #define TRACE_LEAVE(mask, ...) \ |
| 74 | trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist("leaving")) |
| 75 | |
| 76 | #define TRACE_POINT(mask, ...) \ |
| 77 | trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist("in")) |
Willy Tarreau | 4ab2421 | 2019-08-12 17:27:09 +0200 | [diff] [blame] | 78 | |
Christopher Faulet | db703b1 | 2019-11-04 11:40:10 +0100 | [diff] [blame] | 79 | #if defined(DEBUG_DEV) || defined(DEBUG_FULL) |
| 80 | # define DBG_TRACE(msg, mask, ...) TRACE(msg, mask, __VA_ARGS__) |
| 81 | # define DBG_TRACE_USER(msg, mask, ...) TRACE_USER(msg, mask, __VA_ARGS__) |
| 82 | # define DBG_TRACE_DATA(msg, mask, ...) TRACE_DATA(msg, mask, __VA_ARGS__) |
| 83 | # define DBG_TRACE_PROTO(msg, mask, ...) TRACE_PROTO(msg, mask, __VA_ARGS__) |
| 84 | # define DBG_TRACE_STATE(msg, mask, ...) TRACE_STATE(msg, mask, __VA_ARGS__) |
| 85 | # define DBG_TRACE_DEVEL(msg, mask, ...) TRACE_DEVEL(msg, mask, __VA_ARGS__) |
| 86 | # define DBG_TRACE_ENTER(mask, ...) TRACE_ENTER(mask, __VA_ARGS__) |
| 87 | # define DBG_TRACE_LEAVE(mask, ...) TRACE_LEAVE(mask, __VA_ARGS__) |
| 88 | # define DBG_TRACE_POINT(mask, ...) TRACE_POINT(mask, __VA_ARGS__) |
| 89 | #else |
| 90 | # define DBG_TRACE(msg, mask, ...) do { /* do nothing */ } while(0) |
| 91 | # define DBG_TRACE_USER(msg, mask, ...) do { /* do nothing */ } while(0) |
| 92 | # define DBG_TRACE_DATA(msg, mask, ...) do { /* do nothing */ } while(0) |
| 93 | # define DBG_TRACE_PROTO(msg, mask, ...) do { /* do nothing */ } while(0) |
| 94 | # define DBG_TRACE_STATE(msg, mask, ...) do { /* do nothing */ } while(0) |
| 95 | # define DBG_TRACE_DEVEL(msg, mask, ...) do { /* do nothing */ } while(0) |
| 96 | # define DBG_TRACE_ENTER(mask, ...) do { /* do nothing */ } while(0) |
| 97 | # define DBG_TRACE_LEAVE(mask, ...) do { /* do nothing */ } while(0) |
| 98 | # define DBG_TRACE_POINT(mask, ...) do { /* do nothing */ } while(0) |
| 99 | #endif |
| 100 | |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 101 | extern struct list trace_sources; |
Willy Tarreau | 88ebd40 | 2019-08-19 15:55:34 +0200 | [diff] [blame] | 102 | extern THREAD_LOCAL struct buffer trace_buf; |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 103 | |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 104 | void __trace(enum trace_level level, uint64_t mask, struct trace_source *src, |
| 105 | const struct ist where, const char *func, |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 106 | const void *a1, const void *a2, const void *a3, const void *a4, |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 107 | void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 108 | const struct ist where, const struct ist func, |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 109 | const void *a1, const void *a2, const void *a3, const void *a4), |
| 110 | const struct ist msg); |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 111 | |
Willy Tarreau | 864e880 | 2019-08-08 18:48:12 +0200 | [diff] [blame] | 112 | /* return a single char to describe a trace state */ |
| 113 | static inline char trace_state_char(enum trace_state st) |
| 114 | { |
| 115 | return (st == TRACE_STATE_RUNNING) ? 'R' : |
| 116 | (st == TRACE_STATE_WAITING) ? 'w' : |
| 117 | '.'; |
| 118 | } |
| 119 | |
| 120 | /* return a single char to describe an event state */ |
| 121 | static inline char trace_event_char(uint64_t conf, uint64_t ev) |
| 122 | { |
| 123 | return (conf & ev) ? '+' : '-'; |
| 124 | } |
| 125 | |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 126 | /* registers trace source <source>. Modifies the list element! |
| 127 | * The {start,pause,stop,report} events are not changed so the source may |
| 128 | * preset them. |
| 129 | */ |
| 130 | static inline void trace_register_source(struct trace_source *source) |
| 131 | { |
| 132 | source->lockon = TRACE_LOCKON_NOTHING; |
| 133 | source->level = TRACE_LEVEL_USER; |
Willy Tarreau | 370a694 | 2019-08-29 08:24:16 +0200 | [diff] [blame] | 134 | source->verbosity = 1; |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 135 | source->sink = NULL; |
| 136 | source->state = TRACE_STATE_STOPPED; |
| 137 | source->lockon_ptr = NULL; |
| 138 | LIST_ADDQ(&trace_sources, &source->source_link); |
| 139 | } |
| 140 | |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 141 | /* sends a trace for the given source */ |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 142 | static inline void trace(enum trace_level level, uint64_t mask, struct trace_source *src, |
| 143 | const struct ist where, const char *func, |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 144 | const void *a1, const void *a2, const void *a3, const void *a4, |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 145 | void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 146 | const struct ist where, const struct ist func, |
Willy Tarreau | bfd14fc | 2019-08-19 16:28:07 +0200 | [diff] [blame] | 147 | const void *a1, const void *a2, const void *a3, const void *a4), |
| 148 | const struct ist msg) |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 149 | { |
| 150 | if (unlikely(src->state != TRACE_STATE_STOPPED)) |
Willy Tarreau | 09fb0df | 2019-08-29 08:40:59 +0200 | [diff] [blame] | 151 | __trace(level, mask, src, where, func, a1, a2, a3, a4, cb, msg); |
Willy Tarreau | 4c2ae48 | 2019-08-12 15:51:58 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Willy Tarreau | 4151c75 | 2019-08-08 18:21:26 +0200 | [diff] [blame] | 154 | #endif /* _PROTO_TRACE_H */ |
| 155 | |
| 156 | /* |
| 157 | * Local variables: |
| 158 | * c-indent-level: 8 |
| 159 | * c-basic-offset: 8 |
| 160 | * End: |
| 161 | */ |