Skip to content

Commit 8740cf4

Browse files
nedbassbehlendorf
authored andcommitted
Add line info and SET_ERROR() to ZFS debug log
Redefine the SET_ERROR macro in terms of __dprintf() so the error return codes get logged as both tracepoint events (if tracepoints are enabled) and as ZFS debug log entries. This also allows us to use the same definition of SET_ERROR() in kernel and user space. Define a new debug flag ZFS_DEBUG_SET_ERROR=512 that may be bitwise or'd into zfs_flags. Setting this flag enables both dprintf() and SET_ERROR() messages in the debug log. That is, setting ZFS_DEBUG_SET_ERROR and ZFS_DEBUG_DPRINTF|ZFS_DEBUG_SET_ERROR are equivalent (this was done for sake of simplicity). Leaving ZFS_DEBUG_SET_ERROR unset suppresses the SET_ERROR() messages which helps avoid cluttering up the logs. To enable SET_ERROR() logging, run: echo 1 > /sys/module/zfs/parameters/zfs_dbgmsg_enable echo 512 > /sys/module/zfs/parameters/zfs_flags Remove the zfs_set_error_class tracepoints event class since SET_ERROR() now uses __dprintf(). This sacrifices a bit of granularity when selecting individual tracepoint events to enable but it makes the code simpler. Include file, function, and line number information in debug log entries. The information is now added to the message buffer in __dprintf() and as a result the zfs_dprintf_class tracepoints event class was changed from a 4 parameter interface to a single parameter. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Ned Bass <[email protected]> Closes openzfs#6400
1 parent 9ff13db commit 8740cf4

File tree

6 files changed

+37
-102
lines changed

6 files changed

+37
-102
lines changed

include/sys/sdt.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@
3434
#define ZFS_PROBE2(a, c, e) ((void) 0)
3535
#define ZFS_PROBE3(a, c, e, g) ((void) 0)
3636
#define ZFS_PROBE4(a, c, e, g, i) ((void) 0)
37-
#define ZFS_SET_ERROR(err) ((void) 0)
3837

39-
#else
40-
41-
#if defined(HAVE_DECLARE_EVENT_CLASS)
42-
43-
#include <sys/trace.h>
38+
#endif /* _KERNEL */
4439

4540
/*
4641
* The set-error SDT probe is extra static, in that we declare its fake
@@ -55,16 +50,9 @@
5550
* twice, so it should not have side effects (e.g. something like:
5651
* "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice).
5752
*/
58-
#define SET_ERROR(err) \
59-
(trace_zfs_set__error(__FILE__, __func__, __LINE__, err), err)
60-
61-
#else
62-
53+
extern void __set_error(const char *file, const char *func, int line, int err);
6354
#undef SET_ERROR
64-
#define SET_ERROR(err) (err)
65-
66-
#endif /* HAVE_DECLARE_EVENT_CLASS */
67-
68-
#endif /* _KERNEL */
55+
#define SET_ERROR(err) \
56+
(__set_error(__FILE__, __func__, __LINE__, err), err)
6957

7058
#endif /* _SYS_SDT_H */

include/sys/trace_dbgmsg.h

Lines changed: 7 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -37,92 +37,29 @@
3737
*/
3838

3939
/*
40-
* Generic support for four argument tracepoints of the form:
40+
* Generic support for one argument tracepoints of the form:
4141
*
42-
* DTRACE_PROBE4(...,
43-
* const char *, ...,
44-
* const char *, ...,
45-
* int, ...,
42+
* DTRACE_PROBE1(...,
4643
* const char *, ...);
4744
*/
4845
/* BEGIN CSTYLED */
4946
DECLARE_EVENT_CLASS(zfs_dprintf_class,
50-
TP_PROTO(const char *file, const char *function, int line,
51-
const char *msg),
52-
TP_ARGS(file, function, line, msg),
47+
TP_PROTO(const char *msg),
48+
TP_ARGS(msg),
5349
TP_STRUCT__entry(
54-
__string(file, file)
55-
__string(function, function)
56-
__field(int, line)
5750
__string(msg, msg)
5851
),
5952
TP_fast_assign(
60-
__assign_str(file, strchr(file, '/') ?
61-
strrchr(file, '/') + 1 : file)
62-
__assign_str(function, function);
63-
__entry->line = line;
6453
__assign_str(msg, msg);
6554
),
66-
TP_printk("%s:%d:%s(): %s", __get_str(file), __entry->line,
67-
__get_str(function), __get_str(msg))
55+
TP_printk("%s", __get_str(msg))
6856
);
6957
/* END CSTYLED */
7058

7159
/* BEGIN CSTYLED */
7260
#define DEFINE_DPRINTF_EVENT(name) \
7361
DEFINE_EVENT(zfs_dprintf_class, name, \
74-
TP_PROTO(const char *file, const char *function, int line, \
75-
const char *msg), \
76-
TP_ARGS(file, function, line, msg))
62+
TP_PROTO(const char *msg), \
63+
TP_ARGS(msg))
7764
/* END CSTYLED */
7865
DEFINE_DPRINTF_EVENT(zfs_zfs__dprintf);
79-
80-
/*
81-
* Generic support for four argument tracepoints of the form:
82-
*
83-
* DTRACE_PROBE4(...,
84-
* const char *, ...,
85-
* const char *, ...,
86-
* int, ...,
87-
* uintptr_t, ...);
88-
*/
89-
/* BEGIN CSTYLED */
90-
DECLARE_EVENT_CLASS(zfs_set_error_class,
91-
TP_PROTO(const char *file, const char *function, int line,
92-
uintptr_t error),
93-
TP_ARGS(file, function, line, error),
94-
TP_STRUCT__entry(
95-
__string(file, file)
96-
__string(function, function)
97-
__field(int, line)
98-
__field(uintptr_t, error)
99-
),
100-
TP_fast_assign(
101-
__assign_str(file, strchr(file, '/') ?
102-
strrchr(file, '/') + 1 : file)
103-
__assign_str(function, function);
104-
__entry->line = line;
105-
__entry->error = error;
106-
),
107-
TP_printk("%s:%d:%s(): error 0x%lx", __get_str(file), __entry->line,
108-
__get_str(function), __entry->error)
109-
);
110-
/* END CSTYLED */
111-
112-
/* BEGIN CSTYLED */
113-
#ifdef TP_CONDITION
114-
#define DEFINE_SET_ERROR_EVENT(name) \
115-
DEFINE_EVENT_CONDITION(zfs_set_error_class, name, \
116-
TP_PROTO(const char *file, const char *function, int line, \
117-
uintptr_t error), \
118-
TP_ARGS(file, function, line, error), \
119-
TP_CONDITION(error))
120-
#else
121-
#define DEFINE_SET_ERROR_EVENT(name) \
122-
DEFINE_EVENT(zfs_set_error_class, name, \
123-
TP_PROTO(const char *file, const char *function, int line, \
124-
uintptr_t error), \
125-
TP_ARGS(file, function, line, error))
126-
#endif
127-
/* END CSTYLED */
128-
DEFINE_SET_ERROR_EVENT(zfs_set__error);

include/sys/zfs_context.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,6 @@ extern int aok;
206206
ZFS_PROBE4(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g, \
207207
(unsigned long)i)
208208

209-
/*
210-
* We use the comma operator so that this macro can be used without much
211-
* additional code. For example, "return (EINVAL);" becomes
212-
* "return (SET_ERROR(EINVAL));". Note that the argument will be evaluated
213-
* twice, so it should not have side effects (e.g. something like:
214-
* "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice).
215-
*/
216-
#define SET_ERROR(err) (ZFS_SET_ERROR(err), err)
217-
218209
/*
219210
* Threads. TS_STACK_MIN is dictated by the minimum allowed pthread stack
220211
* size. While TS_STACK_MAX is somewhat arbitrary, it was selected to be

include/sys/zfs_debug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extern int zfs_free_leak_on_eio;
5151
#define ZFS_DEBUG_ZIO_FREE (1 << 6)
5252
#define ZFS_DEBUG_HISTOGRAM_VERIFY (1 << 7)
5353
#define ZFS_DEBUG_METASLAB_VERIFY (1 << 8)
54+
#define ZFS_DEBUG_SET_ERROR (1 << 9)
5455

5556
extern void __dprintf(const char *file, const char *func,
5657
int line, const char *fmt, ...);

man/man5/zfs-module-parameters.5

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,12 @@ _
12581258
_
12591259
128 ZFS_DEBUG_HISTOGRAM_VERIFY
12601260
Enable extra spacemap histogram verifications.
1261+
_
1262+
256 ZFS_DEBUG_METASLAB_VERIFY
1263+
Verify space accounting on disk matches in-core range_trees.
1264+
_
1265+
512 ZFS_DEBUG_SET_ERROR
1266+
Enable SET_ERROR and dprintf entries in the debug log.
12611267
.TE
12621268
.sp
12631269
* Requires debug build.

module/zfs/zfs_debug.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ __zfs_dbgmsg(char *buf)
161161
mutex_exit(&zfs_dbgmsgs_lock);
162162
}
163163

164+
void
165+
__set_error(const char *file, const char *func, int line, int err)
166+
{
167+
if (zfs_flags & ZFS_DEBUG_SET_ERROR)
168+
__dprintf(file, func, line, "error %lu", err);
169+
}
170+
164171
#ifdef _KERNEL
165172
void
166173
__dprintf(const char *file, const char *func, int line, const char *fmt, ...)
@@ -170,8 +177,10 @@ __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
170177
size_t size;
171178
char *buf;
172179
char *nl;
180+
int i;
173181

174-
if (!zfs_dbgmsg_enable && !(zfs_flags & ZFS_DEBUG_DPRINTF))
182+
if (!zfs_dbgmsg_enable &&
183+
!(zfs_flags & (ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SET_ERROR)))
175184
return;
176185

177186
size = 1024;
@@ -187,9 +196,13 @@ __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
187196
newfile = file;
188197
}
189198

190-
va_start(adx, fmt);
191-
(void) vsnprintf(buf, size, fmt, adx);
192-
va_end(adx);
199+
i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
200+
201+
if (i < size) {
202+
va_start(adx, fmt);
203+
(void) vsnprintf(buf + i, size - i, fmt, adx);
204+
va_end(adx);
205+
}
193206

194207
/*
195208
* Get rid of trailing newline.
@@ -209,9 +222,8 @@ __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
209222
* # Dump the ring buffer.
210223
* $ cat /sys/kernel/debug/tracing/trace
211224
*/
212-
if (zfs_flags & ZFS_DEBUG_DPRINTF)
213-
DTRACE_PROBE4(zfs__dprintf,
214-
char *, newfile, char *, func, int, line, char *, buf);
225+
if (zfs_flags & (ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SET_ERROR))
226+
DTRACE_PROBE1(zfs__dprintf, char *, buf);
215227

216228
/*
217229
* To get this data enable the zfs debug log as shown:

0 commit comments

Comments
 (0)