2 * Copyright (c) 2007 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
43 * These commands manage DDB(4) scripts from user space. For better or worse,
44 * the setting and unsetting of scripts is only poorly represented using
45 * sysctl(8), and this interface provides a more user-friendly way to
46 * accomplish this management, wrapped around lower-level sysctls. For
47 * completeness, listing of scripts is also included.
50 #define SYSCTL_SCRIPT "debug.ddb.scripting.script"
51 #define SYSCTL_SCRIPTS "debug.ddb.scripting.scripts"
52 #define SYSCTL_UNSCRIPT "debug.ddb.scripting.unscript"
55 * Print all scripts (scriptname==NULL) or a specific script.
58 ddb_list_scripts(const char *scriptname)
60 char *buffer, *line, *nextline;
61 char *line_script, *line_scriptname;
66 if (sysctlbyname(SYSCTL_SCRIPTS, NULL, &buflen, NULL, 0) < 0)
67 err(EX_OSERR, "sysctl: %s", SYSCTL_SCRIPTS);
70 buffer = malloc(buflen);
72 err(EX_OSERR, "malloc");
73 bzero(buffer, buflen);
75 ret = sysctlbyname(SYSCTL_SCRIPTS, buffer, &len, NULL, 0);
76 if (ret < 0 && errno != ENOMEM)
77 err(EX_OSERR, "sysctl: %s", SYSCTL_SCRIPTS);
84 * We nul'd the buffer before calling sysctl(), so at worst empty.
86 * If a specific script hasn't been requested, print it all.
88 if (scriptname == NULL) {
95 * If a specific script has been requested, we have to parse the
99 while ((line = strsep(&nextline, "\n")) != NULL) {
101 line_scriptname = strsep(&line_script, "=");
102 if (line_script == NULL)
104 if (strcmp(scriptname, line_scriptname) != 0)
106 printf("%s\n", line_script);
111 err(EX_DATAERR, "%s", scriptname);
117 * "ddb script" can be used to either print or set a script.
120 ddb_script(int argc, char *argv[])
127 if (strchr(argv[0], '=') != 0) {
128 if (sysctlbyname(SYSCTL_SCRIPT, NULL, NULL, argv[0],
129 strlen(argv[0]) + 1) < 0)
130 err(EX_OSERR, "sysctl: %s", SYSCTL_SCRIPTS);
132 ddb_list_scripts(argv[0]);
136 ddb_scripts(int argc, char *argv[])
141 ddb_list_scripts(NULL);
145 ddb_unscript(int argc, char *argv[])
153 ret = sysctlbyname(SYSCTL_UNSCRIPT, NULL, NULL, argv[0],
154 strlen(argv[0]) + 1);
155 if (ret < 0 && errno == EINVAL) {
157 err(EX_DATAERR, "sysctl: %s", argv[0]);
159 err(EX_OSERR, "sysctl: %s", SYSCTL_UNSCRIPT);