1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang *
4a9643ea8Slogwang * Copyright (c) 2007 Robert N. M. Watson
5a9643ea8Slogwang * All rights reserved.
6a9643ea8Slogwang *
7a9643ea8Slogwang * Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang * modification, are permitted provided that the following conditions
9a9643ea8Slogwang * are met:
10a9643ea8Slogwang * 1. Redistributions of source code must retain the above copyright
11a9643ea8Slogwang * notice, this list of conditions and the following disclaimer.
12a9643ea8Slogwang * 2. Redistributions in binary form must reproduce the above copyright
13a9643ea8Slogwang * notice, this list of conditions and the following disclaimer in the
14a9643ea8Slogwang * documentation and/or other materials provided with the distribution.
15a9643ea8Slogwang *
16a9643ea8Slogwang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a9643ea8Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a9643ea8Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a9643ea8Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a9643ea8Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a9643ea8Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a9643ea8Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a9643ea8Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a9643ea8Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a9643ea8Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a9643ea8Slogwang * SUCH DAMAGE.
27a9643ea8Slogwang */
28a9643ea8Slogwang
29a9643ea8Slogwang /*-
30a9643ea8Slogwang * Simple DDB scripting mechanism. Each script consists of a named list of
31a9643ea8Slogwang * DDB commands to execute sequentially. A more sophisticated scripting
32a9643ea8Slogwang * language might be desirable, but would be significantly more complex to
33a9643ea8Slogwang * implement. A more interesting syntax might allow general use of variables
34a9643ea8Slogwang * and extracting of useful values, such as a thread's process identifier,
35a9643ea8Slogwang * for passing into further DDB commands. Certain scripts are run
36a9643ea8Slogwang * automatically at kdb_enter(), if defined, based on how the debugger is
37a9643ea8Slogwang * entered, allowing scripted responses to panics, break signals, etc.
38a9643ea8Slogwang *
39a9643ea8Slogwang * Scripts may be managed from within DDB using the script, scripts, and
40a9643ea8Slogwang * unscript commands. They may also be managed from userspace using ddb(8),
41a9643ea8Slogwang * which operates using a set of sysctls.
42a9643ea8Slogwang *
43a9643ea8Slogwang * TODO:
44a9643ea8Slogwang * - Allow scripts to be defined using tunables so that they can be defined
45a9643ea8Slogwang * before boot and be present in single-user mode without boot scripts
46a9643ea8Slogwang * running.
47a9643ea8Slogwang * - Memory allocation is not possible from within DDB, so we use a set of
48a9643ea8Slogwang * statically allocated buffers to hold defined scripts. However, when
49a9643ea8Slogwang * scripts are being defined from userspace via sysctl, we could in fact be
50a9643ea8Slogwang * using malloc(9) and therefore not impose a static limit, giving greater
51a9643ea8Slogwang * flexibility and avoiding hard-defined buffer limits.
52a9643ea8Slogwang * - When scripts run automatically on entrance to DDB, placing "continue" at
53a9643ea8Slogwang * the end still results in being in the debugger, as we unconditionally
54a9643ea8Slogwang * run db_command_loop() after the script. There should be a way to avoid
55a9643ea8Slogwang * this.
56a9643ea8Slogwang */
57a9643ea8Slogwang
58a9643ea8Slogwang #include <sys/cdefs.h>
59a9643ea8Slogwang __FBSDID("$FreeBSD$");
60a9643ea8Slogwang
61a9643ea8Slogwang #include <sys/param.h>
62a9643ea8Slogwang #include <sys/kdb.h>
63a9643ea8Slogwang #include <sys/kernel.h>
64a9643ea8Slogwang #include <sys/libkern.h>
65a9643ea8Slogwang #include <sys/lock.h>
66a9643ea8Slogwang #include <sys/malloc.h>
67a9643ea8Slogwang #include <sys/mutex.h>
68a9643ea8Slogwang #include <sys/sbuf.h>
69a9643ea8Slogwang #include <sys/sysctl.h>
70a9643ea8Slogwang #include <sys/systm.h>
71a9643ea8Slogwang
72a9643ea8Slogwang #include <ddb/ddb.h>
73a9643ea8Slogwang #include <ddb/db_command.h>
74a9643ea8Slogwang #include <ddb/db_lex.h>
75a9643ea8Slogwang
76a9643ea8Slogwang #include <machine/setjmp.h>
77a9643ea8Slogwang
78a9643ea8Slogwang /*
79a9643ea8Slogwang * struct ddb_script describes an individual script.
80a9643ea8Slogwang */
81a9643ea8Slogwang struct ddb_script {
82a9643ea8Slogwang char ds_scriptname[DB_MAXSCRIPTNAME];
83a9643ea8Slogwang char ds_script[DB_MAXSCRIPTLEN];
84a9643ea8Slogwang };
85a9643ea8Slogwang
86a9643ea8Slogwang /*
87a9643ea8Slogwang * Global list of scripts -- defined scripts have non-empty name fields.
88a9643ea8Slogwang */
89a9643ea8Slogwang static struct ddb_script db_script_table[DB_MAXSCRIPTS];
90a9643ea8Slogwang
91a9643ea8Slogwang /*
92a9643ea8Slogwang * While executing a script, we parse it using strsep(), so require a
93a9643ea8Slogwang * temporary buffer that may be used destructively. Since we support weak
94a9643ea8Slogwang * recursion of scripts (one may reference another), we need one buffer for
95a9643ea8Slogwang * each concurrently executing script.
96a9643ea8Slogwang */
97a9643ea8Slogwang static struct db_recursion_data {
98a9643ea8Slogwang char drd_buffer[DB_MAXSCRIPTLEN];
99a9643ea8Slogwang } db_recursion_data[DB_MAXSCRIPTRECURSION];
100a9643ea8Slogwang static int db_recursion = -1;
101a9643ea8Slogwang
102a9643ea8Slogwang /*
103a9643ea8Slogwang * We use a separate static buffer for script validation so that it is safe
104a9643ea8Slogwang * to validate scripts from within a script. This is used only in
105a9643ea8Slogwang * db_script_valid(), which should never be called reentrantly.
106a9643ea8Slogwang */
107a9643ea8Slogwang static char db_static_buffer[DB_MAXSCRIPTLEN];
108a9643ea8Slogwang
109a9643ea8Slogwang /*
110a9643ea8Slogwang * Synchronization is not required from within the debugger, as it is
111a9643ea8Slogwang * singe-threaded (although reentrance must be carefully considered).
112a9643ea8Slogwang * However, it is required when interacting with scripts from user space
113a9643ea8Slogwang * processes. Sysctl procedures acquire db_script_mtx before accessing the
114a9643ea8Slogwang * global script data structures.
115a9643ea8Slogwang */
116a9643ea8Slogwang static struct mtx db_script_mtx;
117a9643ea8Slogwang MTX_SYSINIT(db_script_mtx, &db_script_mtx, "db_script_mtx", MTX_DEF);
118a9643ea8Slogwang
119a9643ea8Slogwang /*
120a9643ea8Slogwang * Some script names have special meaning, such as those executed
121a9643ea8Slogwang * automatically when KDB is entered.
122a9643ea8Slogwang */
123a9643ea8Slogwang #define DB_SCRIPT_KDBENTER_PREFIX "kdb.enter" /* KDB has entered. */
124a9643ea8Slogwang #define DB_SCRIPT_KDBENTER_DEFAULT "kdb.enter.default"
125a9643ea8Slogwang
126a9643ea8Slogwang /*
127a9643ea8Slogwang * Find the existing script slot for a named script, if any.
128a9643ea8Slogwang */
129a9643ea8Slogwang static struct ddb_script *
db_script_lookup(const char * scriptname)130a9643ea8Slogwang db_script_lookup(const char *scriptname)
131a9643ea8Slogwang {
132a9643ea8Slogwang int i;
133a9643ea8Slogwang
134a9643ea8Slogwang for (i = 0; i < DB_MAXSCRIPTS; i++) {
135a9643ea8Slogwang if (strcmp(db_script_table[i].ds_scriptname, scriptname) ==
136a9643ea8Slogwang 0)
137a9643ea8Slogwang return (&db_script_table[i]);
138a9643ea8Slogwang }
139a9643ea8Slogwang return (NULL);
140a9643ea8Slogwang }
141a9643ea8Slogwang
142a9643ea8Slogwang /*
143a9643ea8Slogwang * Find a new slot for a script, if available. Does not mark as allocated in
144a9643ea8Slogwang * any way--this must be done by the caller.
145a9643ea8Slogwang */
146a9643ea8Slogwang static struct ddb_script *
db_script_new(void)147a9643ea8Slogwang db_script_new(void)
148a9643ea8Slogwang {
149a9643ea8Slogwang int i;
150a9643ea8Slogwang
151a9643ea8Slogwang for (i = 0; i < DB_MAXSCRIPTS; i++) {
152a9643ea8Slogwang if (strlen(db_script_table[i].ds_scriptname) == 0)
153a9643ea8Slogwang return (&db_script_table[i]);
154a9643ea8Slogwang }
155a9643ea8Slogwang return (NULL);
156a9643ea8Slogwang }
157a9643ea8Slogwang
158a9643ea8Slogwang /*
159a9643ea8Slogwang * Perform very rudimentary validation of a proposed script. It would be
160a9643ea8Slogwang * easy to imagine something more comprehensive. The script string is
161a9643ea8Slogwang * validated in a static buffer.
162a9643ea8Slogwang */
163a9643ea8Slogwang static int
db_script_valid(const char * scriptname,const char * script)164a9643ea8Slogwang db_script_valid(const char *scriptname, const char *script)
165a9643ea8Slogwang {
166a9643ea8Slogwang char *buffer, *command;
167a9643ea8Slogwang
168a9643ea8Slogwang if (strlen(scriptname) == 0)
169a9643ea8Slogwang return (EINVAL);
170a9643ea8Slogwang if (strlen(scriptname) >= DB_MAXSCRIPTNAME)
171a9643ea8Slogwang return (EINVAL);
172a9643ea8Slogwang if (strlen(script) >= DB_MAXSCRIPTLEN)
173a9643ea8Slogwang return (EINVAL);
174a9643ea8Slogwang buffer = db_static_buffer;
175a9643ea8Slogwang strcpy(buffer, script);
176a9643ea8Slogwang while ((command = strsep(&buffer, ";")) != NULL) {
177a9643ea8Slogwang if (strlen(command) >= DB_MAXLINE)
178a9643ea8Slogwang return (EINVAL);
179a9643ea8Slogwang }
180a9643ea8Slogwang return (0);
181a9643ea8Slogwang }
182a9643ea8Slogwang
183a9643ea8Slogwang /*
184a9643ea8Slogwang * Modify an existing script or add a new script with the specified script
185a9643ea8Slogwang * name and contents. If there are no script slots available, an error will
186a9643ea8Slogwang * be returned.
187a9643ea8Slogwang */
188a9643ea8Slogwang static int
db_script_set(const char * scriptname,const char * script)189a9643ea8Slogwang db_script_set(const char *scriptname, const char *script)
190a9643ea8Slogwang {
191a9643ea8Slogwang struct ddb_script *dsp;
192a9643ea8Slogwang int error;
193a9643ea8Slogwang
194a9643ea8Slogwang error = db_script_valid(scriptname, script);
195a9643ea8Slogwang if (error)
196a9643ea8Slogwang return (error);
197a9643ea8Slogwang dsp = db_script_lookup(scriptname);
198a9643ea8Slogwang if (dsp == NULL) {
199a9643ea8Slogwang dsp = db_script_new();
200a9643ea8Slogwang if (dsp == NULL)
201a9643ea8Slogwang return (ENOSPC);
202a9643ea8Slogwang strlcpy(dsp->ds_scriptname, scriptname,
203a9643ea8Slogwang sizeof(dsp->ds_scriptname));
204a9643ea8Slogwang }
205a9643ea8Slogwang strlcpy(dsp->ds_script, script, sizeof(dsp->ds_script));
206a9643ea8Slogwang return (0);
207a9643ea8Slogwang }
208a9643ea8Slogwang
209a9643ea8Slogwang /*
210a9643ea8Slogwang * Delete an existing script by name, if found.
211a9643ea8Slogwang */
212a9643ea8Slogwang static int
db_script_unset(const char * scriptname)213a9643ea8Slogwang db_script_unset(const char *scriptname)
214a9643ea8Slogwang {
215a9643ea8Slogwang struct ddb_script *dsp;
216a9643ea8Slogwang
217a9643ea8Slogwang dsp = db_script_lookup(scriptname);
218a9643ea8Slogwang if (dsp == NULL)
219a9643ea8Slogwang return (ENOENT);
220a9643ea8Slogwang strcpy(dsp->ds_scriptname, "");
221a9643ea8Slogwang strcpy(dsp->ds_script, "");
222a9643ea8Slogwang return (0);
223a9643ea8Slogwang }
224a9643ea8Slogwang
225a9643ea8Slogwang /*
226a9643ea8Slogwang * Trim leading/trailing white space in a command so that we don't pass
227a9643ea8Slogwang * carriage returns, etc, into DDB command parser.
228a9643ea8Slogwang */
229a9643ea8Slogwang static int
db_command_trimmable(char ch)230a9643ea8Slogwang db_command_trimmable(char ch)
231a9643ea8Slogwang {
232a9643ea8Slogwang
233a9643ea8Slogwang switch (ch) {
234a9643ea8Slogwang case ' ':
235a9643ea8Slogwang case '\t':
236a9643ea8Slogwang case '\n':
237a9643ea8Slogwang case '\r':
238a9643ea8Slogwang return (1);
239a9643ea8Slogwang
240a9643ea8Slogwang default:
241a9643ea8Slogwang return (0);
242a9643ea8Slogwang }
243a9643ea8Slogwang }
244a9643ea8Slogwang
245a9643ea8Slogwang static void
db_command_trim(char ** commandp)246a9643ea8Slogwang db_command_trim(char **commandp)
247a9643ea8Slogwang {
248a9643ea8Slogwang char *command;
249a9643ea8Slogwang
250a9643ea8Slogwang command = *commandp;
251a9643ea8Slogwang while (db_command_trimmable(*command))
252a9643ea8Slogwang command++;
253a9643ea8Slogwang while ((strlen(command) > 0) &&
254a9643ea8Slogwang db_command_trimmable(command[strlen(command) - 1]))
255a9643ea8Slogwang command[strlen(command) - 1] = 0;
256a9643ea8Slogwang *commandp = command;
257a9643ea8Slogwang }
258a9643ea8Slogwang
259a9643ea8Slogwang /*
260a9643ea8Slogwang * Execute a script, breaking it up into individual commands and passing them
261a9643ea8Slogwang * sequentially into DDB's input processing. Use the KDB jump buffer to
262a9643ea8Slogwang * restore control to the main script loop if things get too wonky when
263a9643ea8Slogwang * processing a command -- i.e., traps, etc. Also, make sure we don't exceed
264a9643ea8Slogwang * practical limits on recursion.
265a9643ea8Slogwang *
266a9643ea8Slogwang * XXXRW: If any individual command is too long, it will be truncated when
267a9643ea8Slogwang * injected into the input at a lower layer. We should validate the script
268a9643ea8Slogwang * before configuring it to avoid this scenario.
269a9643ea8Slogwang */
270a9643ea8Slogwang static int
db_script_exec(const char * scriptname,int warnifnotfound)271a9643ea8Slogwang db_script_exec(const char *scriptname, int warnifnotfound)
272a9643ea8Slogwang {
273a9643ea8Slogwang struct db_recursion_data *drd;
274a9643ea8Slogwang struct ddb_script *dsp;
275a9643ea8Slogwang char *buffer, *command;
276a9643ea8Slogwang void *prev_jb;
277a9643ea8Slogwang jmp_buf jb;
278a9643ea8Slogwang
279a9643ea8Slogwang dsp = db_script_lookup(scriptname);
280a9643ea8Slogwang if (dsp == NULL) {
281a9643ea8Slogwang if (warnifnotfound)
282a9643ea8Slogwang db_printf("script '%s' not found\n", scriptname);
283a9643ea8Slogwang return (ENOENT);
284a9643ea8Slogwang }
285a9643ea8Slogwang
286a9643ea8Slogwang if (db_recursion >= DB_MAXSCRIPTRECURSION) {
287a9643ea8Slogwang db_printf("Script stack too deep\n");
288a9643ea8Slogwang return (E2BIG);
289a9643ea8Slogwang }
290a9643ea8Slogwang db_recursion++;
291a9643ea8Slogwang drd = &db_recursion_data[db_recursion];
292a9643ea8Slogwang
293a9643ea8Slogwang /*
294a9643ea8Slogwang * Parse script in temporary buffer, since strsep() is destructive.
295a9643ea8Slogwang */
296a9643ea8Slogwang buffer = drd->drd_buffer;
297a9643ea8Slogwang strcpy(buffer, dsp->ds_script);
298a9643ea8Slogwang while ((command = strsep(&buffer, ";")) != NULL) {
299*22ce4affSfengbojiang db_printf("db:%d:%s> %s\n", db_recursion, dsp->ds_scriptname,
300a9643ea8Slogwang command);
301a9643ea8Slogwang db_command_trim(&command);
302a9643ea8Slogwang prev_jb = kdb_jmpbuf(jb);
303a9643ea8Slogwang if (setjmp(jb) == 0)
304a9643ea8Slogwang db_command_script(command);
305a9643ea8Slogwang else
306a9643ea8Slogwang db_printf("Script command '%s' returned error\n",
307a9643ea8Slogwang command);
308a9643ea8Slogwang kdb_jmpbuf(prev_jb);
309a9643ea8Slogwang }
310a9643ea8Slogwang db_recursion--;
311a9643ea8Slogwang return (0);
312a9643ea8Slogwang }
313a9643ea8Slogwang
314a9643ea8Slogwang /*
315a9643ea8Slogwang * Wrapper for exec path that is called on KDB enter. Map reason for KDB
316a9643ea8Slogwang * enter to a script name, and don't whine if the script doesn't exist. If
317a9643ea8Slogwang * there is no matching script, try the catch-all script.
318a9643ea8Slogwang */
319a9643ea8Slogwang void
db_script_kdbenter(const char * eventname)320a9643ea8Slogwang db_script_kdbenter(const char *eventname)
321a9643ea8Slogwang {
322a9643ea8Slogwang char scriptname[DB_MAXSCRIPTNAME];
323a9643ea8Slogwang
324a9643ea8Slogwang snprintf(scriptname, sizeof(scriptname), "%s.%s",
325a9643ea8Slogwang DB_SCRIPT_KDBENTER_PREFIX, eventname);
326a9643ea8Slogwang if (db_script_exec(scriptname, 0) == ENOENT)
327a9643ea8Slogwang (void)db_script_exec(DB_SCRIPT_KDBENTER_DEFAULT, 0);
328a9643ea8Slogwang }
329a9643ea8Slogwang
330a9643ea8Slogwang /*-
331a9643ea8Slogwang * DDB commands for scripting, as reached via the DDB user interface:
332a9643ea8Slogwang *
333a9643ea8Slogwang * scripts - lists scripts
334a9643ea8Slogwang * run <scriptname> - run a script
335a9643ea8Slogwang * script <scriptname> - prints script
336a9643ea8Slogwang * script <scriptname> <script> - set a script
337a9643ea8Slogwang * unscript <scriptname> - remove a script
338a9643ea8Slogwang */
339a9643ea8Slogwang
340a9643ea8Slogwang /*
341a9643ea8Slogwang * List scripts and their contents.
342a9643ea8Slogwang */
343a9643ea8Slogwang void
db_scripts_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)344a9643ea8Slogwang db_scripts_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
345a9643ea8Slogwang char *modif)
346a9643ea8Slogwang {
347a9643ea8Slogwang int i;
348a9643ea8Slogwang
349a9643ea8Slogwang for (i = 0; i < DB_MAXSCRIPTS; i++) {
350a9643ea8Slogwang if (strlen(db_script_table[i].ds_scriptname) != 0) {
351a9643ea8Slogwang db_printf("%s=%s\n",
352a9643ea8Slogwang db_script_table[i].ds_scriptname,
353a9643ea8Slogwang db_script_table[i].ds_script);
354a9643ea8Slogwang }
355a9643ea8Slogwang }
356a9643ea8Slogwang }
357a9643ea8Slogwang
358a9643ea8Slogwang /*
359a9643ea8Slogwang * Execute a script.
360a9643ea8Slogwang */
361a9643ea8Slogwang void
db_run_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)362a9643ea8Slogwang db_run_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
363a9643ea8Slogwang {
364a9643ea8Slogwang int t;
365a9643ea8Slogwang
366a9643ea8Slogwang /*
367a9643ea8Slogwang * Right now, we accept exactly one argument. In the future, we
368a9643ea8Slogwang * might want to accept flags and arguments to the script itself.
369a9643ea8Slogwang */
370a9643ea8Slogwang t = db_read_token();
371a9643ea8Slogwang if (t != tIDENT)
372a9643ea8Slogwang db_error("?\n");
373a9643ea8Slogwang
374a9643ea8Slogwang if (db_read_token() != tEOL)
375a9643ea8Slogwang db_error("?\n");
376a9643ea8Slogwang
377a9643ea8Slogwang db_script_exec(db_tok_string, 1);
378a9643ea8Slogwang }
379a9643ea8Slogwang
380a9643ea8Slogwang /*
381a9643ea8Slogwang * Print or set a named script, with the set portion broken out into its own
382a9643ea8Slogwang * function. We must directly access the remainder of the DDB line input as
383a9643ea8Slogwang * we do not wish to use db_lex's token processing.
384a9643ea8Slogwang */
385a9643ea8Slogwang void
db_script_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)386a9643ea8Slogwang db_script_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
387a9643ea8Slogwang char *modif)
388a9643ea8Slogwang {
389a9643ea8Slogwang char *buf, scriptname[DB_MAXSCRIPTNAME];
390a9643ea8Slogwang struct ddb_script *dsp;
391a9643ea8Slogwang int error, t;
392a9643ea8Slogwang
393a9643ea8Slogwang t = db_read_token();
394a9643ea8Slogwang if (t != tIDENT) {
395a9643ea8Slogwang db_printf("usage: script scriptname=script\n");
396a9643ea8Slogwang db_skip_to_eol();
397a9643ea8Slogwang return;
398a9643ea8Slogwang }
399a9643ea8Slogwang
400a9643ea8Slogwang if (strlcpy(scriptname, db_tok_string, sizeof(scriptname)) >=
401a9643ea8Slogwang sizeof(scriptname)) {
402a9643ea8Slogwang db_printf("scriptname too long\n");
403a9643ea8Slogwang db_skip_to_eol();
404a9643ea8Slogwang return;
405a9643ea8Slogwang }
406a9643ea8Slogwang
407a9643ea8Slogwang t = db_read_token();
408a9643ea8Slogwang if (t == tEOL) {
409a9643ea8Slogwang dsp = db_script_lookup(scriptname);
410a9643ea8Slogwang if (dsp == NULL) {
411a9643ea8Slogwang db_printf("script '%s' not found\n", scriptname);
412a9643ea8Slogwang db_skip_to_eol();
413a9643ea8Slogwang return;
414a9643ea8Slogwang }
415a9643ea8Slogwang db_printf("%s=%s\n", scriptname, dsp->ds_script);
416a9643ea8Slogwang } else if (t == tEQ) {
417a9643ea8Slogwang buf = db_get_line();
418a9643ea8Slogwang if (buf[strlen(buf)-1] == '\n')
419a9643ea8Slogwang buf[strlen(buf)-1] = '\0';
420a9643ea8Slogwang error = db_script_set(scriptname, buf);
421a9643ea8Slogwang if (error != 0)
422a9643ea8Slogwang db_printf("Error: %d\n", error);
423a9643ea8Slogwang } else
424a9643ea8Slogwang db_printf("?\n");
425a9643ea8Slogwang db_skip_to_eol();
426a9643ea8Slogwang }
427a9643ea8Slogwang
428a9643ea8Slogwang /*
429a9643ea8Slogwang * Remove a named script.
430a9643ea8Slogwang */
431a9643ea8Slogwang void
db_unscript_cmd(db_expr_t addr,bool have_addr,db_expr_t count,char * modif)432a9643ea8Slogwang db_unscript_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
433a9643ea8Slogwang char *modif)
434a9643ea8Slogwang {
435a9643ea8Slogwang int error, t;
436a9643ea8Slogwang
437a9643ea8Slogwang t = db_read_token();
438a9643ea8Slogwang if (t != tIDENT) {
439a9643ea8Slogwang db_printf("?\n");
440a9643ea8Slogwang db_skip_to_eol();
441a9643ea8Slogwang return;
442a9643ea8Slogwang }
443a9643ea8Slogwang
444a9643ea8Slogwang error = db_script_unset(db_tok_string);
445a9643ea8Slogwang if (error == ENOENT) {
446a9643ea8Slogwang db_printf("script '%s' not found\n", db_tok_string);
447a9643ea8Slogwang db_skip_to_eol();
448a9643ea8Slogwang return;
449a9643ea8Slogwang }
450a9643ea8Slogwang db_skip_to_eol();
451a9643ea8Slogwang }
452a9643ea8Slogwang
453a9643ea8Slogwang /*
454a9643ea8Slogwang * Sysctls for managing DDB scripting:
455a9643ea8Slogwang *
456a9643ea8Slogwang * debug.ddb.scripting.script - Define a new script
457a9643ea8Slogwang * debug.ddb.scripting.scripts - List of names *and* scripts
458a9643ea8Slogwang * debug.ddb.scripting.unscript - Remove an existing script
459a9643ea8Slogwang *
460a9643ea8Slogwang * Since we don't want to try to manage arbitrary extensions to the sysctl
461a9643ea8Slogwang * name space from the debugger, the script/unscript sysctls are a bit more
462a9643ea8Slogwang * like RPCs and a bit less like normal get/set requests. The ddb(8) command
463a9643ea8Slogwang * line tool wraps them to make things a bit more user-friendly.
464a9643ea8Slogwang */
465*22ce4affSfengbojiang static SYSCTL_NODE(_debug_ddb, OID_AUTO, scripting,
466*22ce4affSfengbojiang CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
467a9643ea8Slogwang "DDB script settings");
468a9643ea8Slogwang
469a9643ea8Slogwang static int
sysctl_debug_ddb_scripting_scripts(SYSCTL_HANDLER_ARGS)470a9643ea8Slogwang sysctl_debug_ddb_scripting_scripts(SYSCTL_HANDLER_ARGS)
471a9643ea8Slogwang {
472a9643ea8Slogwang struct sbuf sb;
473a9643ea8Slogwang int error, i, len;
474a9643ea8Slogwang char *buffer;
475a9643ea8Slogwang
476a9643ea8Slogwang /*
477a9643ea8Slogwang * Make space to include a maximum-length name, = symbol,
478a9643ea8Slogwang * maximum-length script, and carriage return for every script that
479a9643ea8Slogwang * may be defined.
480a9643ea8Slogwang */
481a9643ea8Slogwang len = DB_MAXSCRIPTS * (DB_MAXSCRIPTNAME + 1 + DB_MAXSCRIPTLEN + 1);
482a9643ea8Slogwang buffer = malloc(len, M_TEMP, M_WAITOK);
483a9643ea8Slogwang (void)sbuf_new(&sb, buffer, len, SBUF_FIXEDLEN);
484a9643ea8Slogwang mtx_lock(&db_script_mtx);
485a9643ea8Slogwang for (i = 0; i < DB_MAXSCRIPTS; i++) {
486a9643ea8Slogwang if (strlen(db_script_table[i].ds_scriptname) == 0)
487a9643ea8Slogwang continue;
488a9643ea8Slogwang (void)sbuf_printf(&sb, "%s=%s\n",
489a9643ea8Slogwang db_script_table[i].ds_scriptname,
490a9643ea8Slogwang db_script_table[i].ds_script);
491a9643ea8Slogwang }
492a9643ea8Slogwang mtx_unlock(&db_script_mtx);
493a9643ea8Slogwang sbuf_finish(&sb);
494a9643ea8Slogwang error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb) + 1);
495a9643ea8Slogwang sbuf_delete(&sb);
496a9643ea8Slogwang free(buffer, M_TEMP);
497a9643ea8Slogwang return (error);
498a9643ea8Slogwang }
499*22ce4affSfengbojiang SYSCTL_PROC(_debug_ddb_scripting, OID_AUTO, scripts,
500*22ce4affSfengbojiang CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
501*22ce4affSfengbojiang sysctl_debug_ddb_scripting_scripts, "A",
502a9643ea8Slogwang "List of defined scripts");
503a9643ea8Slogwang
504a9643ea8Slogwang static int
sysctl_debug_ddb_scripting_script(SYSCTL_HANDLER_ARGS)505a9643ea8Slogwang sysctl_debug_ddb_scripting_script(SYSCTL_HANDLER_ARGS)
506a9643ea8Slogwang {
507a9643ea8Slogwang char *buffer, *script, *scriptname;
508a9643ea8Slogwang int error, len;
509a9643ea8Slogwang
510a9643ea8Slogwang /*
511a9643ea8Slogwang * Maximum length for an input string is DB_MAXSCRIPTNAME + '='
512a9643ea8Slogwang * symbol + DB_MAXSCRIPT.
513a9643ea8Slogwang */
514a9643ea8Slogwang len = DB_MAXSCRIPTNAME + DB_MAXSCRIPTLEN + 1;
515a9643ea8Slogwang buffer = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
516a9643ea8Slogwang error = sysctl_handle_string(oidp, buffer, len, req);
517a9643ea8Slogwang if (error)
518a9643ea8Slogwang goto out;
519a9643ea8Slogwang
520a9643ea8Slogwang /*
521a9643ea8Slogwang * Argument will be in form scriptname=script, so split into the
522a9643ea8Slogwang * scriptname and script.
523a9643ea8Slogwang */
524a9643ea8Slogwang script = buffer;
525a9643ea8Slogwang scriptname = strsep(&script, "=");
526a9643ea8Slogwang if (script == NULL) {
527a9643ea8Slogwang error = EINVAL;
528a9643ea8Slogwang goto out;
529a9643ea8Slogwang }
530a9643ea8Slogwang mtx_lock(&db_script_mtx);
531a9643ea8Slogwang error = db_script_set(scriptname, script);
532a9643ea8Slogwang mtx_unlock(&db_script_mtx);
533a9643ea8Slogwang out:
534a9643ea8Slogwang free(buffer, M_TEMP);
535a9643ea8Slogwang return (error);
536a9643ea8Slogwang }
537*22ce4affSfengbojiang SYSCTL_PROC(_debug_ddb_scripting, OID_AUTO, script,
538*22ce4affSfengbojiang CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
539*22ce4affSfengbojiang sysctl_debug_ddb_scripting_script, "A",
540a9643ea8Slogwang "Set a script");
541a9643ea8Slogwang
542a9643ea8Slogwang /*
543a9643ea8Slogwang * debug.ddb.scripting.unscript has somewhat unusual sysctl semantics -- set
544a9643ea8Slogwang * the name of the script that you want to delete.
545a9643ea8Slogwang */
546a9643ea8Slogwang static int
sysctl_debug_ddb_scripting_unscript(SYSCTL_HANDLER_ARGS)547a9643ea8Slogwang sysctl_debug_ddb_scripting_unscript(SYSCTL_HANDLER_ARGS)
548a9643ea8Slogwang {
549a9643ea8Slogwang char name[DB_MAXSCRIPTNAME];
550a9643ea8Slogwang int error;
551a9643ea8Slogwang
552a9643ea8Slogwang bzero(name, sizeof(name));
553a9643ea8Slogwang error = sysctl_handle_string(oidp, name, sizeof(name), req);
554a9643ea8Slogwang if (error)
555a9643ea8Slogwang return (error);
556a9643ea8Slogwang if (req->newptr == NULL)
557a9643ea8Slogwang return (0);
558a9643ea8Slogwang mtx_lock(&db_script_mtx);
559a9643ea8Slogwang error = db_script_unset(name);
560a9643ea8Slogwang mtx_unlock(&db_script_mtx);
561a9643ea8Slogwang if (error == ENOENT)
562a9643ea8Slogwang return (EINVAL); /* Don't confuse sysctl consumers. */
563a9643ea8Slogwang return (0);
564a9643ea8Slogwang }
565*22ce4affSfengbojiang SYSCTL_PROC(_debug_ddb_scripting, OID_AUTO, unscript,
566*22ce4affSfengbojiang CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
567*22ce4affSfengbojiang sysctl_debug_ddb_scripting_unscript, "A",
568a9643ea8Slogwang "Unset a script");
569