1 /*
2 * include/haproxy/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 _HAPROXY_TRACE_H
23 #define _HAPROXY_TRACE_H
24
25 #include <import/ist.h>
26 #include <haproxy/api.h>
27 #include <haproxy/list.h>
28 #include <haproxy/sink-t.h>
29 #include <haproxy/tools.h>
30 #include <haproxy/trace-t.h>
31
32 /* Make a string from the location of the trace producer as "file:line" */
33 #define TRC_LOC _TRC_LOC(__FILE__, __LINE__)
34 #define _TRC_LOC(f,l) __TRC_LOC(f, ":", l)
35 #define __TRC_LOC(f,c,l) f c #l
36
37 /* truncate a macro arg list to exactly 5 args and replace missing ones with NULL */
38 #define TRC_5ARGS(a1,a2,a3,a4,a5,...) DEFNULL(a1),DEFNULL(a2),DEFNULL(a3),DEFNULL(a4),DEFNULL(a5)
39
40 /* For convenience, TRACE() alone uses the file's default TRACE_LEVEL, most
41 * likely TRACE_LEVEL_DEVELOPER, though the other explicit variants specify
42 * the desired level and will work when TRACE_LEVEL is not set. The 5 optional
43 * arguments are the 4 source-specific arguments that are passed to the cb()
44 * callback dedicated to decoding, and which may be used for special tracking.
45 * These 4 arguments as well as the cb() function pointer may all be NULL, or
46 * simply omitted (in which case they will be replaced by a NULL). This
47 * ordering allows many TRACE() calls to be placed using copy-paste and just
48 * change the message at the beginning. Only TRACE_DEVEL(), TRACE_ENTER() and
49 * TRACE_LEAVE() will report the calling function's name.
50 */
51 #define TRACE(msg, mask, ...) \
52 trace(TRACE_LEVEL, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
53
54 #define TRACE_USER(msg, mask, ...) \
55 trace(TRACE_LEVEL_USER, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
56
57 #define TRACE_DATA(msg, mask, ...) \
58 trace(TRACE_LEVEL_DATA, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
59
60 #define TRACE_PROTO(msg, mask, ...) \
61 trace(TRACE_LEVEL_PROTO, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
62
63 #define TRACE_STATE(msg, mask, ...) \
64 trace(TRACE_LEVEL_STATE, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
65
66 #define TRACE_DEVEL(msg, mask, ...) \
67 trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
68
69 #define TRACE_ENTER(mask, ...) \
70 trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist("entering"))
71
72 #define TRACE_LEAVE(mask, ...) \
73 trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist("leaving"))
74
75 #define TRACE_POINT(mask, ...) \
76 trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist("in"))
77
78 #if defined(DEBUG_DEV) || defined(DEBUG_FULL)
79 # define DBG_TRACE(msg, mask, ...) TRACE(msg, mask, __VA_ARGS__)
80 # define DBG_TRACE_USER(msg, mask, ...) TRACE_USER(msg, mask, __VA_ARGS__)
81 # define DBG_TRACE_DATA(msg, mask, ...) TRACE_DATA(msg, mask, __VA_ARGS__)
82 # define DBG_TRACE_PROTO(msg, mask, ...) TRACE_PROTO(msg, mask, __VA_ARGS__)
83 # define DBG_TRACE_STATE(msg, mask, ...) TRACE_STATE(msg, mask, __VA_ARGS__)
84 # define DBG_TRACE_DEVEL(msg, mask, ...) TRACE_DEVEL(msg, mask, __VA_ARGS__)
85 # define DBG_TRACE_ENTER(mask, ...) TRACE_ENTER(mask, __VA_ARGS__)
86 # define DBG_TRACE_LEAVE(mask, ...) TRACE_LEAVE(mask, __VA_ARGS__)
87 # define DBG_TRACE_POINT(mask, ...) TRACE_POINT(mask, __VA_ARGS__)
88 #else
89 # define DBG_TRACE(msg, mask, ...) do { /* do nothing */ } while(0)
90 # define DBG_TRACE_USER(msg, mask, ...) do { /* do nothing */ } while(0)
91 # define DBG_TRACE_DATA(msg, mask, ...) do { /* do nothing */ } while(0)
92 # define DBG_TRACE_PROTO(msg, mask, ...) do { /* do nothing */ } while(0)
93 # define DBG_TRACE_STATE(msg, mask, ...) do { /* do nothing */ } while(0)
94 # define DBG_TRACE_DEVEL(msg, mask, ...) do { /* do nothing */ } while(0)
95 # define DBG_TRACE_ENTER(mask, ...) do { /* do nothing */ } while(0)
96 # define DBG_TRACE_LEAVE(mask, ...) do { /* do nothing */ } while(0)
97 # define DBG_TRACE_POINT(mask, ...) do { /* do nothing */ } while(0)
98 #endif
99
100 extern struct list trace_sources;
101 extern THREAD_LOCAL struct buffer trace_buf;
102
103 void __trace(enum trace_level level, uint64_t mask, struct trace_source *src,
104 const struct ist where, const char *func,
105 const void *a1, const void *a2, const void *a3, const void *a4,
106 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src,
107 const struct ist where, const struct ist func,
108 const void *a1, const void *a2, const void *a3, const void *a4),
109 const struct ist msg);
110
111 /* return a single char to describe a trace state */
trace_state_char(enum trace_state st)112 static inline char trace_state_char(enum trace_state st)
113 {
114 return (st == TRACE_STATE_RUNNING) ? 'R' :
115 (st == TRACE_STATE_WAITING) ? 'w' :
116 '.';
117 }
118
119 /* return a single char to describe an event state */
trace_event_char(uint64_t conf,uint64_t ev)120 static inline char trace_event_char(uint64_t conf, uint64_t ev)
121 {
122 return (conf & ev) ? '+' : '-';
123 }
124
125 /* registers trace source <source>. Modifies the list element!
126 * The {start,pause,stop,report} events are not changed so the source may
127 * preset them.
128 */
trace_register_source(struct trace_source * source)129 static inline void trace_register_source(struct trace_source *source)
130 {
131 source->lockon = TRACE_LOCKON_NOTHING;
132 source->level = TRACE_LEVEL_USER;
133 source->verbosity = 1;
134 source->sink = NULL;
135 source->state = TRACE_STATE_STOPPED;
136 source->lockon_ptr = NULL;
137 LIST_ADDQ(&trace_sources, &source->source_link);
138 }
139
140 /* sends a trace for the given source */
trace(enum trace_level level,uint64_t mask,struct trace_source * src,const struct ist where,const char * func,const void * a1,const void * a2,const void * a3,const void * a4,void (* cb)(enum trace_level level,uint64_t mask,const struct trace_source * src,const struct ist where,const struct ist func,const void * a1,const void * a2,const void * a3,const void * a4),const struct ist msg)141 static inline void trace(enum trace_level level, uint64_t mask, struct trace_source *src,
142 const struct ist where, const char *func,
143 const void *a1, const void *a2, const void *a3, const void *a4,
144 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src,
145 const struct ist where, const struct ist func,
146 const void *a1, const void *a2, const void *a3, const void *a4),
147 const struct ist msg)
148 {
149 if (unlikely(src->state != TRACE_STATE_STOPPED))
150 __trace(level, mask, src, where, func, a1, a2, a3, a4, cb, msg);
151 }
152
153 #endif /* _HAPROXY_TRACE_H */
154
155 /*
156 * Local variables:
157 * c-indent-level: 8
158 * c-basic-offset: 8
159 * End:
160 */
161