blob: ece23293bdc3e75210afbf8e8ba96bf4339f412b [file] [log] [blame]
Willy Tarreau4151c752019-08-08 18:21:26 +02001/*
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
25#include <common/buffer.h>
26#include <common/compat.h>
27#include <common/config.h>
28#include <common/ist.h>
29#include <common/mini-clist.h>
30#include <types/log.h>
31#include <types/sink.h>
32#include <types/trace.h>
33
Willy Tarreau4ab24212019-08-12 17:27:09 +020034/* Make a string from the location of the trace producer as "file:line" */
35#define TRC_LOC _TRC_LOC(__FILE__, __LINE__)
36#define _TRC_LOC(f,l) __TRC_LOC(f, ":", l)
37#define __TRC_LOC(f,c,l) f c #l
38
Willy Tarreauc326ecc2019-08-28 10:35:16 +020039/* truncate a macro arg list to exactly 5 args and replace missing ones with NULL */
40#define TRC_5ARGS(a1,a2,a3,a4,a5,...) DEFNULL(a1),DEFNULL(a2),DEFNULL(a3),DEFNULL(a4),DEFNULL(a5)
41
Willy Tarreau4ab24212019-08-12 17:27:09 +020042/* For convenience, TRACE() alone uses the file's default TRACE_LEVEL, most
Willy Tarreauc326ecc2019-08-28 10:35:16 +020043 * likely TRACE_LEVEL_DEVELOPER, though the other explicit variants specify
44 * the desired level and will work when TRACE_LEVEL is not set. The 5 optional
45 * arguments are the 4 source-specific arguments that are passed to the cb()
46 * callback dedicated to decoding, and which may be used for special tracking.
47 * These 4 arguments as well as the cb() function pointer may all be NULL, or
48 * simply omitted (in which case they will be replaced by a NULL). This
49 * ordering allows many TRACE() calls to be placed using copy-paste and just
Willy Tarreau09fb0df2019-08-29 08:40:59 +020050 * change the message at the beginning. Only TRACE_DEVEL(), TRACE_ENTER() and
51 * TRACE_LEAVE() will report the calling function's name.
Willy Tarreau4ab24212019-08-12 17:27:09 +020052 */
Willy Tarreauc326ecc2019-08-28 10:35:16 +020053#define TRACE(msg, mask, ...) \
Willy Tarreau09fb0df2019-08-29 08:40:59 +020054 trace(TRACE_LEVEL, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
Willy Tarreauc326ecc2019-08-28 10:35:16 +020055
56#define TRACE_USER(msg, mask, ...) \
Willy Tarreau09fb0df2019-08-29 08:40:59 +020057 trace(TRACE_LEVEL_USER, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
Willy Tarreau4ab24212019-08-12 17:27:09 +020058
Willy Tarreau2ea549b2019-08-29 08:01:48 +020059#define TRACE_DATA(msg, mask, ...) \
Willy Tarreau09fb0df2019-08-29 08:40:59 +020060 trace(TRACE_LEVEL_DATA, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
Willy Tarreauc326ecc2019-08-28 10:35:16 +020061
62#define TRACE_PROTO(msg, mask, ...) \
Willy Tarreau09fb0df2019-08-29 08:40:59 +020063 trace(TRACE_LEVEL_PROTO, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
Willy Tarreauc326ecc2019-08-28 10:35:16 +020064
65#define TRACE_STATE(msg, mask, ...) \
Willy Tarreau09fb0df2019-08-29 08:40:59 +020066 trace(TRACE_LEVEL_STATE, (mask), TRACE_SOURCE, ist(TRC_LOC), NULL, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
Willy Tarreau4ab24212019-08-12 17:27:09 +020067
Willy Tarreauc326ecc2019-08-28 10:35:16 +020068#define TRACE_DEVEL(msg, mask, ...) \
Willy Tarreau09fb0df2019-08-29 08:40:59 +020069 trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist(msg))
70
71#define TRACE_ENTER(mask, ...) \
72 trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist("entering"))
73
74#define TRACE_LEAVE(mask, ...) \
75 trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist("leaving"))
76
77#define TRACE_POINT(mask, ...) \
78 trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), __FUNCTION__, TRC_5ARGS(__VA_ARGS__,,,,,), ist("in"))
Willy Tarreau4ab24212019-08-12 17:27:09 +020079
Christopher Fauletdb703b12019-11-04 11:40:10 +010080#if defined(DEBUG_DEV) || defined(DEBUG_FULL)
81# define DBG_TRACE(msg, mask, ...) TRACE(msg, mask, __VA_ARGS__)
82# define DBG_TRACE_USER(msg, mask, ...) TRACE_USER(msg, mask, __VA_ARGS__)
83# define DBG_TRACE_DATA(msg, mask, ...) TRACE_DATA(msg, mask, __VA_ARGS__)
84# define DBG_TRACE_PROTO(msg, mask, ...) TRACE_PROTO(msg, mask, __VA_ARGS__)
85# define DBG_TRACE_STATE(msg, mask, ...) TRACE_STATE(msg, mask, __VA_ARGS__)
86# define DBG_TRACE_DEVEL(msg, mask, ...) TRACE_DEVEL(msg, mask, __VA_ARGS__)
87# define DBG_TRACE_ENTER(mask, ...) TRACE_ENTER(mask, __VA_ARGS__)
88# define DBG_TRACE_LEAVE(mask, ...) TRACE_LEAVE(mask, __VA_ARGS__)
89# define DBG_TRACE_POINT(mask, ...) TRACE_POINT(mask, __VA_ARGS__)
90#else
91# define DBG_TRACE(msg, mask, ...) do { /* do nothing */ } while(0)
92# define DBG_TRACE_USER(msg, mask, ...) do { /* do nothing */ } while(0)
93# define DBG_TRACE_DATA(msg, mask, ...) do { /* do nothing */ } while(0)
94# define DBG_TRACE_PROTO(msg, mask, ...) do { /* do nothing */ } while(0)
95# define DBG_TRACE_STATE(msg, mask, ...) do { /* do nothing */ } while(0)
96# define DBG_TRACE_DEVEL(msg, mask, ...) do { /* do nothing */ } while(0)
97# define DBG_TRACE_ENTER(mask, ...) do { /* do nothing */ } while(0)
98# define DBG_TRACE_LEAVE(mask, ...) do { /* do nothing */ } while(0)
99# define DBG_TRACE_POINT(mask, ...) do { /* do nothing */ } while(0)
100#endif
101
Willy Tarreau4151c752019-08-08 18:21:26 +0200102extern struct list trace_sources;
Willy Tarreau88ebd402019-08-19 15:55:34 +0200103extern THREAD_LOCAL struct buffer trace_buf;
Willy Tarreau4151c752019-08-08 18:21:26 +0200104
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200105void __trace(enum trace_level level, uint64_t mask, struct trace_source *src,
106 const struct ist where, const char *func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200107 const void *a1, const void *a2, const void *a3, const void *a4,
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200108 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src,
109 const struct ist where, const struct ist func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200110 const void *a1, const void *a2, const void *a3, const void *a4),
111 const struct ist msg);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200112
Willy Tarreau864e8802019-08-08 18:48:12 +0200113/* return a single char to describe a trace state */
114static inline char trace_state_char(enum trace_state st)
115{
116 return (st == TRACE_STATE_RUNNING) ? 'R' :
117 (st == TRACE_STATE_WAITING) ? 'w' :
118 '.';
119}
120
121/* return a single char to describe an event state */
122static inline char trace_event_char(uint64_t conf, uint64_t ev)
123{
124 return (conf & ev) ? '+' : '-';
125}
126
Willy Tarreau4151c752019-08-08 18:21:26 +0200127/* registers trace source <source>. Modifies the list element!
128 * The {start,pause,stop,report} events are not changed so the source may
129 * preset them.
130 */
131static inline void trace_register_source(struct trace_source *source)
132{
133 source->lockon = TRACE_LOCKON_NOTHING;
134 source->level = TRACE_LEVEL_USER;
Willy Tarreau370a6942019-08-29 08:24:16 +0200135 source->verbosity = 1;
Willy Tarreau4151c752019-08-08 18:21:26 +0200136 source->sink = NULL;
137 source->state = TRACE_STATE_STOPPED;
138 source->lockon_ptr = NULL;
139 LIST_ADDQ(&trace_sources, &source->source_link);
140}
141
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200142/* sends a trace for the given source */
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200143static inline void trace(enum trace_level level, uint64_t mask, struct trace_source *src,
144 const struct ist where, const char *func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200145 const void *a1, const void *a2, const void *a3, const void *a4,
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200146 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src,
147 const struct ist where, const struct ist func,
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200148 const void *a1, const void *a2, const void *a3, const void *a4),
149 const struct ist msg)
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200150{
151 if (unlikely(src->state != TRACE_STATE_STOPPED))
Willy Tarreau09fb0df2019-08-29 08:40:59 +0200152 __trace(level, mask, src, where, func, a1, a2, a3, a4, cb, msg);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200153}
154
Willy Tarreau4151c752019-08-08 18:21:26 +0200155#endif /* _PROTO_TRACE_H */
156
157/*
158 * Local variables:
159 * c-indent-level: 8
160 * c-basic-offset: 8
161 * End:
162 */