blob: 7207233de1578cf949d681912422dfd0d34de647 [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
39/* For convenience, TRACE() alone uses the file's default TRACE_LEVEL, most
40 * likely TRACE_LEVEL_DEVELOPER. The 4 arguments are the 4 source-specific
41 * arguments that are passed to the cb() callback dedicated to decoding, and
42 * which may be used for special tracking. These 4 arguments as well as the
43 * cb() function pointer may all be NULL, or simply omitted (in which case
44 * they will be replaced by a NULL). This ordering allows many TRACE() calls
45 * to be placed using copy-paste and just change the message at the end.
46 */
47
48#define TRACE(mask, a1, a2, a3, a4, cb, msg) \
49 trace(TRACE_LEVEL, (mask), TRACE_SOURCE, ist(TRC_LOC), DEFNULL(a1), DEFNULL(a2), DEFNULL(a3), DEFNULL(a4), DEFNULL(cb), ist(msg))
50
51/* and explicit trace levels (recommended) */
52#define TRACE_USER(mask, a1, a2, a3, a4, cb, msg) \
53 trace(TRACE_LEVEL_USER, (mask), TRACE_SOURCE, ist(TRC_LOC), DEFNULL(a1), DEFNULL(a2), DEFNULL(a3), DEFNULL(a4), DEFNULL(cb), ist(msg))
54#define TRACE_PAYLOAD(mask, a1, a2, a3, a4, cb, msg) \
55 trace(TRACE_LEVEL_PAYLOAD, (mask), TRACE_SOURCE, ist(TRC_LOC), DEFNULL(a1), DEFNULL(a2), DEFNULL(a3), DEFNULL(a4), DEFNULL(cb), ist(msg))
56#define TRACE_PROTO(mask, a1, a2, a3, a4, cb, msg) \
57 trace(TRACE_LEVEL_PROTO, (mask), TRACE_SOURCE, ist(TRC_LOC), DEFNULL(a1), DEFNULL(a2), DEFNULL(a3), DEFNULL(a4), DEFNULL(cb), ist(msg))
58#define TRACE_STATE(mask, a1, a2, a3, a4, cb, msg) \
59 trace(TRACE_LEVEL_STATE, (mask), TRACE_SOURCE, ist(TRC_LOC), DEFNULL(a1), DEFNULL(a2), DEFNULL(a3), DEFNULL(a4), DEFNULL(cb), ist(msg))
60#define TRACE_DEVEL(mask, a1, a2, a3, a4, cb, msg) \
61 trace(TRACE_LEVEL_DEVELOPER, (mask), TRACE_SOURCE, ist(TRC_LOC), DEFNULL(a1), DEFNULL(a2), DEFNULL(a3), DEFNULL(a4), DEFNULL(cb), ist(msg))
62
Willy Tarreau4151c752019-08-08 18:21:26 +020063extern struct list trace_sources;
Willy Tarreau88ebd402019-08-19 15:55:34 +020064extern THREAD_LOCAL struct buffer trace_buf;
Willy Tarreau4151c752019-08-08 18:21:26 +020065
Willy Tarreaubfd14fc2019-08-19 16:28:07 +020066void __trace(enum trace_level level, uint64_t mask, struct trace_source *src, const struct ist where,
67 const void *a1, const void *a2, const void *a3, const void *a4,
68 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src, const struct ist where,
69 const void *a1, const void *a2, const void *a3, const void *a4),
70 const struct ist msg);
Willy Tarreau4c2ae482019-08-12 15:51:58 +020071
Willy Tarreau864e8802019-08-08 18:48:12 +020072/* return a single char to describe a trace state */
73static inline char trace_state_char(enum trace_state st)
74{
75 return (st == TRACE_STATE_RUNNING) ? 'R' :
76 (st == TRACE_STATE_WAITING) ? 'w' :
77 '.';
78}
79
80/* return a single char to describe an event state */
81static inline char trace_event_char(uint64_t conf, uint64_t ev)
82{
83 return (conf & ev) ? '+' : '-';
84}
85
Willy Tarreau4151c752019-08-08 18:21:26 +020086/* registers trace source <source>. Modifies the list element!
87 * The {start,pause,stop,report} events are not changed so the source may
88 * preset them.
89 */
90static inline void trace_register_source(struct trace_source *source)
91{
92 source->lockon = TRACE_LOCKON_NOTHING;
93 source->level = TRACE_LEVEL_USER;
94 source->detail_level = LOG_NOTICE;
95 source->sink = NULL;
96 source->state = TRACE_STATE_STOPPED;
97 source->lockon_ptr = NULL;
98 LIST_ADDQ(&trace_sources, &source->source_link);
99}
100
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200101/* sends a trace for the given source */
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200102static inline void trace(enum trace_level level, uint64_t mask, struct trace_source *src, const struct ist where,
103 const void *a1, const void *a2, const void *a3, const void *a4,
104 void (*cb)(enum trace_level level, uint64_t mask, const struct trace_source *src, const struct ist where,
105 const void *a1, const void *a2, const void *a3, const void *a4),
106 const struct ist msg)
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200107{
108 if (unlikely(src->state != TRACE_STATE_STOPPED))
Willy Tarreaubfd14fc2019-08-19 16:28:07 +0200109 __trace(level, mask, src, where, a1, a2, a3, a4, cb, msg);
Willy Tarreau4c2ae482019-08-12 15:51:58 +0200110}
111
Willy Tarreau4151c752019-08-08 18:21:26 +0200112#endif /* _PROTO_TRACE_H */
113
114/*
115 * Local variables:
116 * c-indent-level: 8
117 * c-basic-offset: 8
118 * End:
119 */