2 * Copyright (c) 2015 Netflix, Inc.
3 * Written by: Scott Long <scottl@freebsd.org>
5 * Copyright (c) 2008 Yahoo!, Inc.
7 * Written by: John Baldwin <jhb@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/errno.h>
48 SET_DECLARE(MPS_DATASET(top), struct mpsutil_command);
49 SET_DECLARE(MPS_DATASET(usage), struct mpsutil_usage);
57 struct mpsutil_usage **cmd;
58 const char *args, *desc;
60 fprintf(stderr, "usage: %s [-u unit] <command> ...\n\n", getprogname());
61 fprintf(stderr, "Commands include:\n");
62 SET_FOREACH(cmd, MPS_DATASET(usage)) {
64 fprintf(stderr, "\n");
66 (*cmd)->handler(&args, &desc);
67 if (strncmp((*cmd)->set, "top", 3) == 0)
68 fprintf(stderr, "%s %-30s\t%s\n",
69 (*cmd)->name, args, desc);
71 fprintf(stderr, "%s %s %-30s\t%s\n",
72 (*cmd)->set, (*cmd)->name, args, desc);
79 version(int ac, char **av)
82 printf("%s: version %s", MPSUTIL_VERSION, getprogname());
90 MPS_COMMAND(top, version, version, "", "version")
93 main(int ac, char **av)
95 struct mpsutil_command **cmd;
100 is_mps = !strcmp(getprogname(), "mpsutil");
102 while ((ch = getopt(ac, av, "u:h?")) != -1) {
105 if (strncmp(optarg, _PATH_DEV, strlen(_PATH_DEV)) == 0) {
106 optarg += strlen(_PATH_DEV);
107 if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) != 0)
108 errx(1, "Invalid device: %s", optarg);
110 if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) == 0)
112 unit = strtoumax(optarg, &end, 10);
113 if (*end != '\0' || unit == UINTMAX_MAX || unit > INT_MAX)
114 errx(1, "Invalid unit: %s", optarg);
127 /* getopt() eats av[0], so we can't use mpt_table_handler() directly. */
133 SET_FOREACH(cmd, MPS_DATASET(top)) {
134 if (strcmp((*cmd)->name, av[0]) == 0) {
135 if ((*cmd)->handler(ac, av))
141 warnx("Unknown command %s.", av[0]);
146 mps_table_handler(struct mpsutil_command **start, struct mpsutil_command **end,
149 struct mpsutil_command **cmd;
152 warnx("The %s command requires a sub-command.", av[0]);
155 for (cmd = start; cmd < end; cmd++) {
156 if (strcmp((*cmd)->name, av[1]) == 0)
157 return ((*cmd)->handler(ac - 1, av + 1));
160 warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
165 hexdump(const void *ptr, int length, const char *hdr, int flags)
169 const unsigned char *cp;
172 if ((flags & HD_DELIM_MASK) != 0)
173 delim = (flags & HD_DELIM_MASK) >> 8;
177 if ((flags & HD_COLUMN_MASK) != 0)
178 cols = flags & HD_COLUMN_MASK;
183 for (i = 0; i < length; i+= cols) {
187 if ((flags & HD_OMIT_COUNT) == 0)
190 if ((flags & HD_OMIT_HEX) == 0) {
191 for (j = 0; j < cols; j++) {
192 if (flags & HD_REVERSED)
193 k = i + (cols - 1 - j);
197 printf("%c%02x", delim, cp[k]);
203 if ((flags & HD_OMIT_CHARS) == 0) {
205 for (j = 0; j < cols; j++) {
206 if (flags & HD_REVERSED)
207 k = i + (cols - 1 - j);
212 else if (cp[k] >= ' ' && cp[k] <= '~')
223 #define PCHAR(c) { if (retval < tmpsz) { *outbuf++ = (c); retval++; } }
226 mps_parse_flags(uintmax_t num, const char *q, char *outbuf, int tmpsz)
228 int n, tmp, retval = 0;
233 /* %b conversion flag format. */
237 if (num & (1 << (n - 1))) {
238 PCHAR(retval != tmp ? ',' : '<');
239 for (; (n = *q) > ' '; ++q)
242 for (; *q > ' '; ++q)