xref: /freebsd/sbin/ddb/ddb.c (revision 98fc0505155575df4170c863107153df2a751a13)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <err.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sysexits.h>
37 #include <unistd.h>
38 
39 #include "ddb.h"
40 
41 void ddb_readfile(char *file);
42 void ddb_main(int argc, char *argv[]);
43 
44 void
45 usage(void)
46 {
47 
48 	fprintf(stderr, "usage: ddb capture [-M core] [-N system] print\n");
49 	fprintf(stderr, "       ddb capture [-M core] [-N system] status\n");
50 	fprintf(stderr, "       ddb script scriptname\n");
51 	fprintf(stderr, "       ddb script scriptname=script\n");
52 	fprintf(stderr, "       ddb scripts\n");
53 	fprintf(stderr, "       ddb unscript scriptname\n");
54 	fprintf(stderr, "       ddb pathname\n");
55 	exit(EX_USAGE);
56 }
57 
58 void
59 ddb_readfile(char *filename)
60 {
61 	char    buf[BUFSIZ];
62 	FILE*	f;
63 
64 	if ((f = fopen(filename, "r")) == NULL)
65 		err(EX_UNAVAILABLE, "fopen: %s", filename);
66 
67 #define WHITESP		" \t"
68 #define MAXARG	 	2
69 	while (fgets(buf, BUFSIZ, f)) {
70 		int argc = 0;
71 		char *argv[MAXARG];
72 		size_t spn;
73 
74 		spn = strlen(buf);
75 		if (buf[spn-1] == '\n')
76 			buf[spn-1] = '\0';
77 
78 		spn = strspn(buf, WHITESP);
79 		argv[0] = buf + spn;
80 		if (*argv[0] == '#' || *argv[0] == '\0')
81 			continue;
82 		argc++;
83 
84 		spn = strcspn(argv[0], WHITESP);
85 		argv[1] = argv[0] + spn + strspn(argv[0] + spn, WHITESP);
86 		argv[0][spn] = '\0';
87 		if (*argv[1] != '\0')
88 			argc++;
89 
90 #ifdef DEBUG
91 		{
92 			int i;
93 			printf("argc = %d\n", argc);
94 			for (i = 0; i < argc; i++) {
95 				printf("arg[%d] = %s\n", i, argv[i]);
96 			}
97 		}
98 #endif
99 		ddb_main(argc, argv);
100 	}
101 	fclose(f);
102 }
103 
104 void
105 ddb_main(int argc, char *argv[])
106 {
107 
108 	if (argc < 1)
109 		usage();
110 
111 	if (strcmp(argv[0], "capture") == 0)
112 		ddb_capture(argc, argv);
113 	else if (strcmp(argv[0], "script") == 0)
114 		ddb_script(argc, argv);
115 	else if (strcmp(argv[0], "scripts") == 0)
116 		ddb_scripts(argc, argv);
117 	else if (strcmp(argv[0], "unscript") == 0)
118 		ddb_unscript(argc, argv);
119 	else
120 		usage();
121 }
122 
123 int
124 main(int argc, char *argv[])
125 {
126 
127 	/*
128 	 * If we've only got one argument and it's an absolute path to a file,
129 	 * interpret as a file to be read in.
130 	 */
131 	if (argc == 2 && argv[1][0] == '/' && access(argv[1], R_OK) == 0)
132 		ddb_readfile(argv[1]);
133 	else
134 		ddb_main(argc-1, argv+1);
135 	exit(EX_OK);
136 }
137