1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008-2022 NetApp, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef _SYS_BOOTTRACE_H_
28#define	_SYS_BOOTTRACE_H_
29
30#define	_BOOTTRACE_BOOTTRACE	"kern.boottrace.boottrace"
31#define	_BOOTTRACE_RUNTRACE	"kern.boottrace.runtrace"
32#define	_BOOTTRACE_SHUTTRACE	"kern.boottrace.shuttrace"
33
34/* Messages are formatted as 'tdname:name' */
35#define	BT_EVENT_TDNAMELEN	24
36#define	BT_EVENT_NAMELEN	40
37#define	BT_MSGLEN		(BT_EVENT_NAMELEN + 1 + BT_EVENT_TDNAMELEN)
38
39#ifndef _KERNEL
40#include <stdarg.h>
41#include <stdio.h>
42#include <string.h>
43#include <sys/sysctl.h>
44
45/*
46 * Convenience macros. Userland API.
47 */
48#define	BOOTTRACE(...)		_boottrace(_BOOTTRACE_BOOTTRACE, __VA_ARGS__)
49#define	RUNTRACE(...)		_boottrace(_BOOTTRACE_RUNTRACE, __VA_ARGS__)
50#define	SHUTTRACE(...)		_boottrace(_BOOTTRACE_SHUTTRACE, __VA_ARGS__)
51
52/*
53 * Call the requested boottrace sysctl with provided va-formatted message.
54 */
55static __inline void
56_boottrace(const char *sysctlname, const char *fmt, ...)
57{
58	va_list ap;
59	char msg[BT_MSGLEN];
60	int len;
61
62	va_start(ap, fmt);
63	len = vsnprintf(msg, sizeof(msg), fmt, ap);
64	va_end(ap);
65
66	/* Log the event, even if truncated. */
67	if (len >= 0)
68		(void)sysctlbyname(sysctlname, NULL, NULL, msg, strlen(msg));
69}
70
71#else /* _KERNEL */
72
73/*
74 * Convenience macros. Kernel API.
75 */
76#define	_BOOTTRACE(tdname, ...) do {					\
77		if (boottrace_enabled)					\
78			(void)boottrace(tdname, __VA_ARGS__);		\
79	} while (0)
80#define	BOOTTRACE(...)		_BOOTTRACE(NULL, __VA_ARGS__)
81#define	BOOTTRACE_INIT(...)	_BOOTTRACE("kernel", __VA_ARGS__)
82
83extern bool boottrace_enabled;
84extern bool shutdown_trace;
85
86int  boottrace(const char *_tdname, const char *_eventfmt, ...)
87    __printflike(2,3);
88void boottrace_reset(const char *_actor);
89int  boottrace_resize(u_int _newsize);
90void boottrace_dump_console(void);
91
92#endif /* _KERNEL */
93#endif /* _SYS_BOOTTRACE_H_ */
94