diff options
Diffstat (limited to 'cddl/contrib/opensolaris/cmd/dtrace/test/cmd')
10 files changed, 3084 insertions, 0 deletions
diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/baddof/baddof.c b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/baddof/baddof.c new file mode 100644 index 000000000000..1c14c6592c88 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/baddof/baddof.c @@ -0,0 +1,207 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#include <sys/stat.h> +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <sys/varargs.h> +#include <errno.h> +#include <math.h> +#include <dtrace.h> + +void +fatal(char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + + fprintf(stderr, "%s: ", "baddof"); + vfprintf(stderr, fmt, ap); + + if (fmt[strlen(fmt) - 1] != '\n') + fprintf(stderr, ": %s\n", strerror(errno)); + + exit(1); +} + +#define LEAP_DISTANCE 20 + +void +corrupt(int fd, unsigned char *buf, int len) +{ + static int ttl, valid; + int bit, i; + unsigned char saved; + int val[LEAP_DISTANCE], pos[LEAP_DISTANCE]; + int new, rv; + +again: + printf("valid DOF #%d\n", valid++); + + /* + * We are going iterate through, flipping one bit and attempting + * to enable. + */ + for (bit = 0; bit < len * 8; bit++) { + saved = buf[bit / 8]; + buf[bit / 8] ^= (1 << (bit % 8)); + + if ((bit % 100) == 0) + printf("%d\n", bit); + + if ((rv = ioctl(fd, DTRACEIOC_ENABLE, buf)) == -1) { + /* + * That failed -- restore the bit and drive on. + */ + buf[bit / 8] = saved; + continue; + } + + /* + * That worked -- and it may have enabled probes. To keep + * enabled probes down to a reasonable level, we'll close + * and reopen pseudodevice if we have more than 10,000 + * probes enabled. + */ + ttl += rv; + + if (ttl < 10000) { + buf[bit / 8] = saved; + continue; + } + + printf("enabled %d probes; resetting device.\n", ttl); + close(fd); + + new = open("/devices/pseudo/dtrace@0:dtrace", O_RDWR); + + if (new == -1) + fatal("couldn't open DTrace pseudo device"); + + if (new != fd) { + dup2(new, fd); + close(new); + } + + ttl = 0; + buf[bit / 8] = saved; + } + + for (;;) { + /* + * Now we want to get as many bits away as possible. We flip + * bits randomly -- getting as far away as we can until we don't + * seem to be making any progress. + */ + for (i = 0; i < LEAP_DISTANCE; i++) { + /* + * Pick a random bit and corrupt it. + */ + bit = lrand48() % (len * 8); + + val[i] = buf[bit / 8]; + pos[i] = bit / 8; + buf[bit / 8] ^= (1 << (bit % 8)); + } + + /* + * Let's see if that managed to get us valid DOF... + */ + if ((rv = ioctl(fd, DTRACEIOC_ENABLE, buf)) > 0) { + /* + * Success! This will be our new base for valid DOF. + */ + ttl += rv; + goto again; + } + + /* + * No luck -- we'll restore those bits and try flipping a + * different set. Note that this must be done in reverse + * order... + */ + for (i = LEAP_DISTANCE - 1; i >= 0; i--) + buf[pos[i]] = val[i]; + } +} + +int +main(int argc, char **argv) +{ + char *filename = argv[1]; + dtrace_hdl_t *dtp; + dtrace_prog_t *pgp; + int err, fd, len; + FILE *fp; + unsigned char *dof, *copy; + + if (argc < 2) + fatal("expected D script as argument\n"); + + if ((fp = fopen(filename, "r")) == NULL) + fatal("couldn't open %s", filename); + + /* + * First, we need to compile our provided D into DOF. + */ + if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) { + fatal("cannot open dtrace library: %s\n", + dtrace_errmsg(NULL, err)); + } + + pgp = dtrace_program_fcompile(dtp, fp, 0, 0, NULL); + fclose(fp); + + if (pgp == NULL) { + fatal("failed to compile script %s: %s\n", filename, + dtrace_errmsg(dtp, dtrace_errno(dtp))); + } + + dof = dtrace_dof_create(dtp, pgp, 0); + len = ((dof_hdr_t *)dof)->dofh_loadsz; + + if ((copy = malloc(len)) == NULL) + fatal("could not allocate copy of %d bytes", len); + + for (;;) { + bcopy(dof, copy, len); + /* + * Open another instance of the dtrace device. + */ + fd = open("/devices/pseudo/dtrace@0:dtrace", O_RDWR); + + if (fd == -1) + fatal("couldn't open DTrace pseudo device"); + + corrupt(fd, copy, len); + close(fd); + } + + /* NOTREACHED */ + return (0); +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/badioctl/badioctl.c b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/badioctl/badioctl.c new file mode 100644 index 000000000000..8d6833d52173 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/badioctl/badioctl.c @@ -0,0 +1,145 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#pragma ident "%Z%%M% %I% %E% SMI" + +#include <sys/stat.h> +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <sys/varargs.h> +#include <errno.h> +#include <sys/mman.h> +#include <sys/wait.h> +#include <unistd.h> + +#define DTRACEIOC (('d' << 24) | ('t' << 16) | ('r' << 8)) +#define DTRACEIOC_MAX 17 + +void +fatal(char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + + fprintf(stderr, "%s: ", "badioctl"); + vfprintf(stderr, fmt, ap); + + if (fmt[strlen(fmt) - 1] != '\n') + fprintf(stderr, ": %s\n", strerror(errno)); + + exit(1); +} + +void +badioctl(pid_t parent) +{ + int fd = -1, random, ps = sysconf(_SC_PAGESIZE); + int i = 0, seconds; + caddr_t addr; + hrtime_t now, last = 0, end; + + if ((random = open("/dev/random", O_RDONLY)) == -1) + fatal("couldn't open /dev/random"); + + if ((addr = mmap(0, ps, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0)) == (caddr_t)-1) + fatal("mmap"); + + for (;;) { + unsigned int ioc; + + if ((now = gethrtime()) - last > NANOSEC) { + if (kill(parent, 0) == -1 && errno == ESRCH) { + /* + * Our parent died. We will kill ourselves in + * sympathy. + */ + exit(0); + } + + /* + * Once a second, we'll reopen the device. + */ + if (fd != -1) + close(fd); + + fd = open("/devices/pseudo/dtrace@0:dtrace", O_RDONLY); + + if (fd == -1) + fatal("couldn't open DTrace pseudo device"); + + last = now; + } + + + if ((i++ % 1000) == 0) { + /* + * Every thousand iterations, change our random gunk. + */ + read(random, addr, ps); + } + + read(random, &ioc, sizeof (ioc)); + ioc %= DTRACEIOC_MAX; + ioc++; + ioctl(fd, DTRACEIOC | ioc, addr); + } +} + +int +main() +{ + pid_t child, parent = getpid(); + int status; + + for (;;) { + if ((child = fork()) == 0) + badioctl(parent); + + while (waitpid(child, &status, WEXITED) != child) + continue; + + if (WIFEXITED(status)) { + /* + * Our child exited by design -- we'll exit with + * the same status code. + */ + exit(WEXITSTATUS(status)); + } + + /* + * Our child died on a signal. Respawn it. + */ + printf("badioctl: child died on signal %d; respawning.\n", + WTERMSIG(status)); + fflush(stdout); + } + + /* NOTREACHED */ + return (0); +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/chkargs/chkargs.c b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/chkargs/chkargs.c new file mode 100644 index 000000000000..a7e0222fd0ea --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/chkargs/chkargs.c @@ -0,0 +1,149 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#include <strings.h> +#include <unistd.h> +#include <dtrace.h> + +static int g_count; +static int g_errs; +static int g_fd; +static int g_verbose; +static int g_errexit; +static char *g_progname; + +static int +probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *data) +{ + dtrace_probeinfo_t p; + dtrace_argdesc_t arg; + char buf[BUFSIZ]; + int i; + + (void) printf("\r%6d", ++g_count); + (void) fflush(stdout); + + if (dtrace_probe_info(dtp, pdp, &p) != 0) { + (void) printf(" failed to get probe info for " + "%s:%s:%s:%s [%d]\n", pdp->dtpd_provider, pdp->dtpd_mod, + pdp->dtpd_func, pdp->dtpd_name, pdp->dtpd_id); + g_errs++; + return (0); + } + + for (i = 0; i < p.dtp_argc; i++) { + if (p.dtp_argv[i].dtt_type == CTF_ERR) { + bzero(&arg, sizeof (dtrace_argdesc_t)); + arg.dtargd_id = pdp->dtpd_id; + arg.dtargd_ndx = i; + (void) ioctl(g_fd, DTRACEIOC_PROBEARG, &arg); + + (void) printf(" failed to get types for args[%d] " + "of %s:%s:%s:%s [%d]: <%s> -> <%s>\n", i, + pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, + pdp->dtpd_name, pdp->dtpd_id, + arg.dtargd_native, arg.dtargd_xlate); + + g_errs++; + + if (g_errexit) + return (-1); + + } else if (g_verbose) { + (void) printf("%d args[%d] : %s\n", pdp->dtpd_id, i, + ctf_type_name(p.dtp_argv[i].dtt_ctfp, + p.dtp_argv[i].dtt_type, buf, sizeof (buf))); + } + } + + return (0); +} + +int +main(int argc, char *argv[]) +{ + dtrace_probedesc_t pd, *pdp = NULL; + dtrace_hdl_t *dtp; + int err, c; + char *p; + + g_progname = argv[0]; + + if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) { + (void) fprintf(stderr, "%s: failed to open dtrace: %s\n", + g_progname, dtrace_errmsg(dtp, err)); + return (1); + } + + while ((c = getopt(argc, argv, "evx:")) != -1) { + switch (c) { + case 'e': + g_errexit++; + break; + case 'v': + g_verbose++; + break; + case 'x': + if ((p = strchr(optarg, '=')) != NULL) + *p++ = '\0'; + + if (dtrace_setopt(dtp, optarg, p) != 0) { + (void) fprintf(stderr, "%s: failed to set " + "option -x %s: %s\n", g_progname, optarg, + dtrace_errmsg(dtp, dtrace_errno(dtp))); + return (2); + } + break; + + default: + (void) fprintf(stderr, "Usage: %s [-ev] " + "[-x opt[=arg]] [probedesc]\n", g_progname); + return (2); + } + } + + argv += optind; + argc -= optind; + + if (argc > 0) { + if (dtrace_str2desc(dtp, DTRACE_PROBESPEC_NAME, argv[0], &pd)) { + (void) fprintf(stderr, "%s: invalid probe description " + "%s: %s\n", g_progname, argv[0], + dtrace_errmsg(dtp, dtrace_errno(dtp))); + return (2); + } + pdp = &pd; + } + + g_fd = dtrace_ctlfd(dtp); + (void) dtrace_probe_iter(dtp, pdp, probe, NULL); + dtrace_close(dtp); + + (void) printf("\nTotal probes: %d\n", g_count); + (void) printf("Total errors: %d\n\n", g_errs); + + return (g_errs != 0); +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/Getopt.java b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/Getopt.java new file mode 100644 index 000000000000..e06a170d6a88 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/Getopt.java @@ -0,0 +1,453 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + * + * ident "%Z%%M% %I% %E% SMI" + */ + +/* Copyright (c) 1988 AT&T */ +/* All Rights Reserved */ + +import java.io.StringWriter; +import java.io.PrintWriter; + +/** + * A Java port of Solaris {@code lib/libc/port/gen/getopt.c}, which is a + * port of System V UNIX getopt. See <b>getopt(3C)</b> and SUS/XPG + * getopt() for function definition and requirements. Unlike that + * definition, this implementation moves non-options to the end of the + * argv array rather than quitting at the first non-option. + */ +public class Getopt { + static final int EOF = -1; + + private String progname; + private String[] args; + private int argc; + private String optstring; + private int optind = 0; // args index + private int optopt = 0; + private String optarg = null; + private boolean opterr = true; + + /* + * _sp is required to keep state between successive calls to + * getopt() while extracting aggregated short-options (ie: -abcd). + */ + private int _sp = 1; + + /** + * Creates a {Code Getopt} instance to parse the given command-line + * arguments. Modifies the given args array by swapping the + * positions of non-options and options so that non-options appear + * at the end of the array. + */ + public Getopt(String programName, String[] args, + String optionString) + { + progname = programName; + // No defensive copy; Getopt is expected to modify the given + // args array + this.args = args; + argc = this.args.length; + optstring = optionString; + validate(); + } + + private void + validate() + { + if (progname == null) { + throw new NullPointerException("program name is null"); + } + int i = 0; + for (String s : args) { + if (s == null) { + throw new NullPointerException("null arg at index " + i); + } + ++i; + } + if (optstring == null) { + throw new NullPointerException("option string is null"); + } + } + + private static class StringRef { + private String s; + + public String + get() + { + return s; + } + + public StringRef + set(String value) + { + s = value; + return this; + } + } + + /* + * Generalized error processing method. If the optstr parameter is + * null, the character c is converted to a string and displayed + * instead. + */ + void + err(String format, char c, String optstr) + { + if (opterr && optstring.charAt(0) != ':') { + StringWriter w = new StringWriter(); + PrintWriter p = new PrintWriter(w); + p.printf(format, progname, (optstr == null ? + Character.toString(c) : optstr.substring(2))); + System.err.println(w.toString()); + } + } + + /* + * Determine if the specified character (c) is present in the string + * (optstring) as a regular, single character option. If the option + * is found, return an index into optstring where the short-option + * character is found, otherwise return -1. The characters ':' and + * '(' are not allowed. + */ + static int + parseshort(String optstring, char c) + { + if (c == ':' || c == '(') { + return -1; + } + + int ch; + int len = optstring.length(); + for (int i = 0; i < len; ++i) { + ch = optstring.charAt(i); + if (ch == c) { + return i; + } + + while (i < len && ch == '(') { + for (++i; i < len && (ch = optstring.charAt(i)) != ')'; ++i); + } + } + + return -1; + } + + /** + * Determine if the specified string (opt) is present in the string + * (optstring) as a long-option contained within parenthesis. If the + * long-option specifies option-argument, return a reference to it + * in longoptarg. Otherwise set the longoptarg reference to null. + * If the option is found, return an index into optstring at the + * position of the short-option character associated with the + * long-option; otherwise return -1. + * + * @param optstring the entire optstring passed to the {@code + * Getopt} constructor + * @param opt the long option read from the command line + * @param longoptarg the value of the option is returned in this + * parameter, if an option exists. Possible return values in + * longoptarg are: + * <ul> + * <li><b>NULL:</b> No argument was found</li> + * <li><b>empty string (""):</b> Argument was explicitly left empty + * by the user (e.g., --option= )</li> + * <li><b>valid string:</b> Argument found on the command line</li> + * </ul> + * @return index to equivalent short-option in optstring, or -1 if + * option not found in optstring. + */ + static int + parselong(String optstring, String opt, StringRef longoptarg) + { + int cp; // index into optstring, beginning of one option spec + int ip; // index into optstring, traverses every char + char ic; // optstring char + int il; // optstring length + int op; // index into opt + char oc; // opt char + int ol; // opt length + boolean match; // true if opt is matching part of optstring + + longoptarg.set(null); + cp = ip = 0; + il = optstring.length(); + ol = opt.length(); + do { + ic = optstring.charAt(ip); + if (ic != '(' && ++ip == il) + break; + ic = optstring.charAt(ip); + if (ic == ':' && ++ip == il) + break; + ic = optstring.charAt(ip); + while (ic == '(') { + if (++ip == il) + break; + op = 0; + match = true; + while (ip < il && (ic = optstring.charAt(ip)) != ')' && + op < ol) { + oc = opt.charAt(op++); + match = (ic == oc && match); + ++ip; + } + + if (match && ip < il && ic == ')' && (op >= ol || + opt.charAt(op) == '=')) { + if (op < ol && opt.charAt(op) == '=') { + /* may be an empty string - OK */ + longoptarg.set(opt.substring(op + 1)); + } else { + longoptarg.set(null); + } + return cp; + } + if (ip < il && ic == ')' && ++ip == il) + break; + ic = optstring.charAt(ip); + } + cp = ip; + /* + * Handle double-colon in optstring ("a::(longa)") The old + * getopt() accepts it and treats it as a required argument. + */ + while ((cp > 0) && (cp < il) && (optstring.charAt(cp) == ':')) { + --cp; + } + } while (cp < il); + return -1; + } + + /** + * Get the current option value. + */ + public String + getOptarg() + { + return optarg; + } + + /** + * Get the index of the next option to be parsed. + */ + public int + getOptind() + { + return optind; + } + + /** + * Gets the command-line arguments. + */ + public String[] + getArgv() + { + // No defensive copy: Getopt is expected to modify the given + // args array. + return args; + } + + /** + * Gets the aggregated short option that just failed. Since long + * options can't be aggregated, a failed long option can be obtained + * by {@code getArgv()[getOptind() - 1]}. + */ + public int + getOptopt() + { + return optopt; + } + + /** + * Set to {@code false} to suppress diagnostic messages to stderr. + */ + public void + setOpterr(boolean err) + { + opterr = err; + } + + /** + * Gets the next option character, or -1 if there are no more + * options. If getopt() encounters a short-option character or a + * long-option string not described in the {@code optionString} + * argument to the constructor, it returns the question-mark (?) + * character. If it detects a missing option-argument, it also + * returns the question-mark (?) character, unless the first + * character of the {@code optionString} argument was a colon (:), + * in which case getopt() returns the colon (:) character. + * <p> + * This implementation swaps the positions of options and + * non-options in the given argv array. + */ + public int + getopt() + { + char c; + int cp; + boolean longopt; + StringRef longoptarg = new StringRef(); + + /* + * Has the end of the options been encountered? The following + * implements the SUS requirements: + * + * If, when getopt() is called: + * - the first character of argv[optind] is not '-' + * - argv[optind] is the string "-" + * getopt() returns -1 without changing optind if + * - argv[optind] is the string "--" + * getopt() returns -1 after incrementing optind + */ + if (_sp == 1) { + boolean nonOption; + do { + nonOption = false; + if (optind >= argc || args[optind].equals("-")) { + return EOF; + } else if (args[optind].equals("--")) { + ++optind; + return EOF; + } else if (args[optind].charAt(0) != '-') { + // non-option: here we deviate from the SUS requirements + // by not quitting, and instead move non-options to the + // end of the args array + nonOption = true; + String tmp = args[optind]; + if (optind + 1 < args.length) { + System.arraycopy(args, optind + 1, args, optind, + args.length - (optind + 1)); + args[args.length - 1] = tmp; + } + --argc; + } + } while (nonOption); + } + + /* + * Getting this far indicates that an option has been encountered. + * Note that the syntax of optstring applies special meanings to + * the characters ':' and '(', so they are not permissible as + * option letters. A special meaning is also applied to the ')' + * character, but its meaning can be determined from context. + * Note that the specification only requires that the alnum + * characters be accepted. + * + * If the second character of the argument is a '-' this must be + * a long-option, otherwise it must be a short option. Scan for + * the option in optstring by the appropriate algorithm. Either + * scan will return an index to the short-option character in + * optstring if the option is found and -1 otherwise. + * + * For an unrecognized long-option, optopt will equal 0, but + * since long-options can't aggregate the failing option can be + * identified by argv[optind-1]. + */ + optopt = c = args[optind].charAt(_sp); + optarg = null; + longopt = (_sp == 1 && c == '-'); + if (!(longopt + ? ((cp = parselong(optstring, args[optind].substring(2), + longoptarg)) != -1) + : ((cp = parseshort(optstring, c)) != -1))) { + err("%s: illegal option -- %s", c, + (longopt ? args[optind] : null)); + /* + * Note: When the long option is unrecognized, optopt will + * be '-' here, which matches the specification. + */ + if (args[optind].length() == ++_sp || longopt) { + ++optind; + _sp = 1; + } + return '?'; + } + optopt = c = optstring.charAt(cp); + + /* + * A valid option has been identified. If it should have an + * option-argument, process that now. SUS defines the setting + * of optarg as follows: + * + * 1. If the option was the last character in an element of + * argv, then optarg contains the next element of argv, and + * optind is incremented by 2. If the resulting value of + * optind is not less than argc, this indicates a missing + * option-argument, and getopt() returns an error indication. + * + * 2. Otherwise, optarg points to the string following the + * option character in that element of argv, and optind is + * incremented by 1. + * + * The second clause allows -abcd (where b requires an + * option-argument) to be interpreted as "-a -b cd". + * + * Note that the option-argument can legally be an empty string, + * such as: + * command --option= operand + * which explicitly sets the value of --option to nil + */ + if (cp + 1 < optstring.length() && optstring.charAt(cp + 1) == ':') { + // The option takes an argument + if (!longopt && ((_sp + 1) < args[optind].length())) { + optarg = args[optind++].substring(_sp + 1); + } else if (longopt && (longoptarg.get() != null)) { + /* + * The option argument was explicitly set to the empty + * string on the command line (--option=) + */ + optind++; + optarg = longoptarg.get(); + } else if (++optind >= argc) { + err("%s: option requires an argument -- %s", c, + (longopt ? args[optind - 1] : null)); + _sp = 1; + optarg = null; + return (optstring.charAt(0) == ':' ? ':' : '?'); + } else + optarg = args[optind++]; + _sp = 1; + } else { + // The option does NOT take an argument + if (longopt && (longoptarg.get() != null)) { + // User supplied an arg to an option that takes none + err("%s: option doesn't take an argument -- %s", (char)0, + (longopt ? args[optind] : null)); + optarg = longoptarg.set(null).get(); + c = '?'; + } + + if (longopt || args[optind].length() == ++_sp) { + _sp = 1; + ++optind; + } + optarg = null; + } + return (c); + } +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/JDTrace.java b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/JDTrace.java new file mode 100644 index 000000000000..3c5654d88df5 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/JDTrace.java @@ -0,0 +1,1042 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright 2008 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + * + * ident "%Z%%M% %I% %E% SMI" + */ +import org.opensolaris.os.dtrace.*; +import java.io.*; +import java.util.*; +import java.util.logging.*; + +/** + * Emulates {@code dtrace(1M)} using the Java DTrace API. + */ +public class JDTrace { + static Logger logger = Logger.getLogger(JDTrace.class.getName()); + + static Consumer dtrace; + + static { + Handler handler = new ConsoleHandler(); + handler.setLevel(Level.ALL); + logger.addHandler(handler); + } + + static final String CLASSNAME = "JDTrace"; + static final String OPTSTR = + "3:6:b:c:CD:ef:Fi:I:lL:m:n:o:p:P:qs:U:vVwx:X:Z"; + static boolean heading = false; + static boolean quiet = false; + static boolean flow = false; + static int stackindent = 14; + static int exitStatus = 0; + static boolean started; + static boolean stopped; + static PrintStream out = System.out; + static final String ATS = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"; + static final String SPACES = " "; + static final int QUANTIZE_ZERO_BUCKET = 63; + + enum Mode { + EXEC, + INFO, + LIST, + VERSION + } + + enum ProgramType { + STRING, + FILE + } + + static class CompileRequest { + String s; + ProgramType type; + ProbeDescription.Spec probespec; + } + + // Modify program string by expanding an incomplete probe + // description according to the requested probespec. + static void + applyProbespec(CompileRequest req) + { + ProbeDescription.Spec spec = ((req.probespec == null) + ? ProbeDescription.Spec.NAME + : req.probespec); + + int colons = 0; + switch (req.probespec) { + case PROVIDER: + colons = 3; + break; + case MODULE: + colons = 2; + break; + case FUNCTION: + colons = 1; + break; + } + + StringBuffer buf = new StringBuffer(); + if (colons > 0) { + char ch; + int len = req.s.length(); + + int i = 0; + // Find first whitespace character not including leading + // whitespace (end of first token). Ignore whitespace + // inside a block if the block is concatenated with the + // probe description. + for (; (i < len) && Character.isWhitespace(req.s.charAt(i)); ++i); + int npos = i; + boolean inBlock = false; + for (; (npos < len) && + (!Character.isWhitespace(ch = req.s.charAt(npos)) || + inBlock); ++npos) { + if (ch == '{') { + inBlock = true; + } else if (ch == '}') { + inBlock = false; + } + } + + // libdtrace lets you concatenate multiple probe + // descriptions separated by code blocks in curly braces, + // for example genunix::'{printf("FOUND");}'::entry, as long + // as the concatenated probe descriptions begin with ':' and + // not a specific field such as 'syscall'. So to expand the + // possibly multiple probe descriptions, we need to insert + // colons before each open curly brace, and again at the end + // only if there is at least one non-whitespace (probe + // specifying) character after the last closing curly brace. + + int prev_i = 0; + while (i < npos) { + for (; (i < npos) && (req.s.charAt(i) != '{'); ++i); + buf.append(req.s.substring(prev_i, i)); + if ((i < npos) || ((i > 0) && (req.s.charAt(i - 1) != '}'))) { + for (int c = 0; c < colons; ++c) { + buf.append(':'); + } + } + if (i < npos) { + buf.append(req.s.charAt(i++)); + } + prev_i = i; + } + + // append remainder of program text + buf.append(req.s.substring(i)); + + req.s = buf.toString(); + } + } + + static void + printValue(Object value, int bytes, String stringFormat) + { + if (value instanceof Integer) { + if (bytes == 1) { + out.printf(" %3d", (Integer)value); + } else if (bytes == 2) { + out.printf(" %5d", (Integer)value); + } else { + out.printf(" %8d", (Integer)value); + } + } else if (value instanceof Long) { + out.printf(" %16d", (Long)value); + } else { + out.printf(stringFormat, value.toString()); + } + } + + static void + consumeProbeData(ProbeData data) + { + if (logger.isLoggable(Level.FINER)) { + logger.finer(data.toString()); + } + + if (!heading) { + if (flow) { + out.printf("%3s %-41s\n", "CPU", "FUNCTION"); + } else { + if (!quiet) { + out.printf("%3s %6s %32s\n", + "CPU", "ID", "FUNCTION:NAME"); + } + } + heading = true; + } + ProbeDescription probe = data.getEnabledProbeDescription(); + if (flow) { + Flow flow = data.getFlow(); + int indent = (flow.getDepth() * 2); + StringBuffer buf = new StringBuffer(); + // indent + buf.append(' '); + for (int i = 0; i < indent; ++i) { + buf.append(' '); + } + // prefix + switch (flow.getKind()) { + case ENTRY: + if (indent == 0) { + buf.append("=> "); + } else { + buf.append("-> "); + } + break; + case RETURN: + if (indent == 0) { + buf.append("<= "); + } else { + buf.append("<- "); + } + break; + } + + switch (flow.getKind()) { + case NONE: + buf.append(probe.getFunction()); + buf.append(':'); + buf.append(probe.getName()); + break; + default: + buf.append(probe.getFunction()); + } + + out.printf("%3s %-41s ", data.getCPU(), + buf.toString()); + } else { + if (!quiet) { + StringBuffer buf = new StringBuffer(); + buf.append(probe.getFunction()); + buf.append(':'); + buf.append(probe.getName()); + out.printf("%3s %6s %32s ", + data.getCPU(), probe.getID(), + buf.toString()); + } + } + Record record = null; + Object value; + List <Record> records = data.getRecords(); + Iterator <Record> itr = records.iterator(); + while (itr.hasNext()) { + record = itr.next(); + + if (record instanceof ExitRecord) { + exitStatus = ((ExitRecord)record).getStatus(); + } else if (record instanceof ScalarRecord) { + ScalarRecord scalar = (ScalarRecord)record; + value = scalar.getValue(); + if (value instanceof byte[]) { + out.print(record.toString()); + } else { + if (quiet) { + out.print(value); + } else { + printValue(value, scalar.getNumberOfBytes(), + " %-33s"); + } + } + } else if (record instanceof PrintfRecord) { + out.print(record); + } else if (record instanceof PrintaRecord) { + PrintaRecord printa = (PrintaRecord)record; + List <Tuple> tuples = printa.getTuples(); + if (tuples.isEmpty()) { + out.print(printa.getOutput()); + } else { + for (Tuple t : tuples) { + out.print(printa.getFormattedString(t)); + } + } + + if (logger.isLoggable(Level.FINE)) { + logger.fine(printa.toString()); + } + } else if (record instanceof StackValueRecord) { + printStack((StackValueRecord)record); + } + } + if (!quiet) { + out.println(); + } + } + + static void + printDistribution(Distribution d) + { + out.printf("\n%16s %41s %-9s\n", "value", + "------------- Distribution -------------", + "count"); + long v; // bucket frequency (value) + long b; // lower bound of bucket range + double total = 0; + boolean positives = false; + boolean negatives = false; + + Distribution.Bucket bucket; + int b1 = 0; // first displayed bucket + int b2 = d.size() - 1; // last displayed bucket + for (; (b1 <= b2) && (d.get(b1).getFrequency() == 0); ++b1); + // If possible, get one bucket before the first non-zero + // bucket and one bucket after the last. + if (b1 > b2) { + // There isn't any data. This is possible if (and only if) + // negative increment values have been used. In this case, + // print the buckets around the base. + if (d instanceof LinearDistribution) { + b1 = 0; + b2 = 2; + } else { + b1 = QUANTIZE_ZERO_BUCKET - 1; + b2 = QUANTIZE_ZERO_BUCKET + 1; + } + } else { + if (b1 > 0) --b1; + for (; (b2 > 0) && (d.get(b2).getFrequency() == 0); --b2); + if (b2 < (d.size() - 1)) ++b2; + } + for (int i = b1; i <= b2; ++i) { + v = d.get(i).getFrequency(); + if (v > 0) { + positives = true; + } + if (v < 0) { + negatives = true; + } + total += Math.abs((double)v); + } + for (int i = b1; i <= b2; ++i) { + bucket = d.get(i); + v = bucket.getFrequency(); + b = bucket.getMin(); + + if (d instanceof LinearDistribution) { + if (b == Long.MIN_VALUE) { + String lt = "< " + ((LinearDistribution)d).getBase(); + out.printf("%16s ", lt); + } else if (bucket.getMax() == Long.MAX_VALUE) { + String ge = ">= " + b; + out.printf("%16s ", ge); + } else { + out.printf("%16d ", b); + } + } else { + out.printf("%16d ", b); + } + + printDistributionLine(v, total, positives, negatives); + } + } + + static void + printDistributionLine(long val, double total, boolean positives, + boolean negatives) + { + double f; + int depth, len = 40; + + assert (ATS.length() == len && SPACES.length() == len); + assert (!(total == 0 && (positives || negatives))); + assert (!(val < 0 && !negatives)); + assert (!(val > 0 && !positives)); + assert (!(val != 0 && total == 0)); + + if (!negatives) { + if (positives) { + f = (Math.abs((double)val) * (double)len) / total; + depth = (int)(f + 0.5); + } else { + depth = 0; + } + + out.printf("|%s%s %-9d\n", ATS.substring(len - depth), + SPACES.substring(depth), val); + return; + } + + if (!positives) { + f = (Math.abs((double)val) * (double)len) / total; + depth = (int)(f + 0.5); + + out.printf("%s%s| %-9d\n", SPACES.substring(depth), + ATS.substring(len - depth), val); + return; + } + + /* + * If we're here, we have both positive and negative bucket values. + * To express this graphically, we're going to generate both positive + * and negative bars separated by a centerline. These bars are half + * the size of normal quantize()/lquantize() bars, so we divide the + * length in half before calculating the bar length. + */ + len /= 2; + String ats = ATS.substring(len); + String spaces = SPACES.substring(len); + + f = (Math.abs((double)val) * (double)len) / total; + depth = (int)(f + 0.5); + + if (val <= 0) { + out.printf("%s%s|%s %-9d\n", spaces.substring(depth), + ats.substring(len - depth), repeat(" ", len), val); + return; + } else { + out.printf("%20s|%s%s %-9d\n", "", ats.substring(len - depth), + spaces.substring(depth), val); + } + } + + public static String + repeat(String s, int n) + { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < n; ++i) { + buf.append(s); + } + return buf.toString(); + } + + static void + printStack(StackValueRecord rec) + { + StackFrame[] frames = rec.getStackFrames(); + int i; + out.println(); + String s; + for (StackFrame f : frames) { + for (i = 0; i < stackindent; ++i) { + out.print(' '); + } + s = f.getFrame(); + if (s.indexOf('[') == 0) { + out.print(" "); + } + out.println(s); + } + } + + static void + printAggregate(Aggregate aggregate) + { + printAggregationRecords(aggregate.getOrderedRecords()); + } + + static void + printAggregationRecords(List <AggregationRecord> list) + { + Tuple tuple; + AggregationValue value; + ValueRecord tupleRecord; + int i; + int len; + for (AggregationRecord r : list) { + tuple = r.getTuple(); + value = r.getValue(); + len = tuple.size(); + for (i = 0; i < len; ++i) { + tupleRecord = tuple.get(i); + if (tupleRecord instanceof StackValueRecord) { + printStack((StackValueRecord)tupleRecord); + } else if (tupleRecord instanceof SymbolValueRecord) { + printValue(tupleRecord.toString(), -1, " %-50s"); + } else { + printValue(tupleRecord.getValue(), + ((ScalarRecord)tupleRecord).getNumberOfBytes(), + " %-50s"); + } + } + if (value instanceof Distribution) { + Distribution d = (Distribution)value; + printDistribution(d); + } else { + Number v = value.getValue(); + printValue(v, -1, " %-50s"); + } + out.println(); + } + } + + static void + exit(int status) + { + out.flush(); + System.err.flush(); + if (status == 0) { + status = exitStatus; + } + System.exit(status); + } + + static void + usage() + { + String predact = "[[ predicate ] action ]"; + System.err.printf("Usage: java %s [-32|-64] [-CeFlqvVwZ] " + + "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] " + + "[-o output] [-p pid] [-s script] [-U name]\n\t" + + "[-x opt[=val]] [-X a|c|s|t]\n\n" + + "\t[-P provider %s]\n" + + "\t[-m [ provider: ] module %s]\n" + + "\t[-f [[ provider: ] module: ] func %s]\n" + + "\t[-n [[[ provider: ] module: ] func: ] name %s]\n" + + "\t[-i probe-id %s] [ args ... ]\n\n", CLASSNAME, + predact, predact, predact, predact, predact); + System.err.printf("\tpredicate -> '/' D-expression '/'\n"); + System.err.printf("\t action -> '{' D-statements '}'\n"); + System.err.printf("\n" + + "\t-32 generate 32-bit D programs\n" + + "\t-64 generate 64-bit D programs\n\n" + + "\t-b set trace buffer size\n" + + "\t-c run specified command and exit upon its completion\n" + + "\t-C run cpp(1) preprocessor on script files\n" + + "\t-D define symbol when invoking preprocessor\n" + + "\t-e exit after compiling request but prior to enabling " + + "probes\n" + + "\t-f enable or list probes matching the specified " + + "function name\n" + + "\t-F coalesce trace output by function\n" + + "\t-i enable or list probes matching the specified probe id\n" + + "\t-I add include directory to preprocessor search path\n" + + "\t-l list probes matching specified criteria\n" + + "\t-L add library directory to library search path\n" + + "\t-m enable or list probes matching the specified " + + "module name\n" + + "\t-n enable or list probes matching the specified probe name\n" + + "\t-o set output file\n" + + "\t-p grab specified process-ID and cache its symbol tables\n" + + "\t-P enable or list probes matching the specified " + + "provider name\n" + + "\t-q set quiet mode (only output explicitly traced data)\n" + + "\t-s enable or list probes according to the specified " + + "D script\n" + + "\t-U undefine symbol when invoking preprocessor\n" + + "\t-v set verbose mode (report stability attributes, " + + "arguments)\n" + + "\t-V report DTrace API version\n" + + "\t-w permit destructive actions\n" + + "\t-x enable or modify compiler and tracing options\n" + + "\t-X specify ISO C conformance settings for preprocessor\n" + + "\t-Z permit probe descriptions that match zero probes\n" + + "\n" + + "\tTo log PrintaRecord, set this environment variable:\n" + + "\t\tJDTRACE_LOGGING_LEVEL=FINE\n" + + "\tTo log ProbeData, set JDTRACE_LOGGING_LEVEL=FINER\n"); + exit(2); + } + + static void + printProgramStability(String programType, String programDescription, + ProgramInfo info) + { + out.println(); + out.printf("Stability data for %s %s:\n\n", + programType, programDescription); + InterfaceAttributes a; + out.println("\tMinimum probe description " + + "attributes"); + a = info.getMinimumProbeAttributes(); + out.printf("\t\tIdentifier Names: %s\n", + a.getNameStability()); + out.printf("\t\tData Semantics: %s\n", + a.getDataStability()); + out.printf("\t\tDependency Class: %s\n", + a.getDependencyClass()); + out.println("\tMinimum probe statement attributes"); + a = info.getMinimumStatementAttributes(); + out.printf("\t\tIdentifier Names: %s\n", + a.getNameStability()); + out.printf("\t\tData Semantics: %s\n", + a.getDataStability()); + out.printf("\t\tDependency Class: %s\n", + a.getDependencyClass()); + } + + static void + printProbeDescription(ProbeDescription p) + { + out.printf("%5d %10s %17s %33s %s\n", p.getID(), + p.getProvider(), p.getModule(), + p.getFunction(), p.getName()); + } + + static void + printProbeInfo(ProbeInfo p) + { + InterfaceAttributes a; + out.println("\n\tProbe Description Attributes"); + + a = p.getProbeAttributes(); + out.printf("\t\tIdentifier Names: %s\n", + a.getNameStability()); + out.printf("\t\tData Semantics: %s\n", + a.getDataStability()); + out.printf("\t\tDependency Class: %s\n", + a.getDependencyClass()); + + out.println("\n\tArgument Attributes"); + + a = p.getArgumentAttributes(); + out.printf("\t\tIdentifier Names: %s\n", + a.getNameStability()); + out.printf("\t\tData Semantics: %s\n", + a.getDataStability()); + out.printf("\t\tDependency Class: %s\n", + a.getDependencyClass()); + + // Argument types unsupported for now. + + out.println(); + } + + public static void + main(String[] args) + { + String loggingLevel = System.getenv().get("JDTRACE_LOGGING_LEVEL"); + try { + logger.setLevel(Level.parse(loggingLevel)); + } catch (Exception e) { + logger.setLevel(Level.OFF); + } + + if (args.length == 0) { + usage(); + } + + List <CompileRequest> compileRequests = new LinkedList + <CompileRequest> (); + List <Program> programList = new LinkedList <Program> (); + boolean verbose = false; + Mode mode = Mode.EXEC; + + final ExceptionHandler exceptionHandler = new ExceptionHandler() { + public void handleException(Throwable e) { + if (e instanceof DTraceException) { + DTraceException de = (DTraceException)e; + System.err.printf("dtrace: %s\n", de.getMessage()); + } else if (e instanceof ConsumerException) { + ConsumerException ce = (ConsumerException)e; + Object msg = ce.getNotificationObject(); + if ((msg instanceof org.opensolaris.os.dtrace.Error) || + (msg instanceof Drop)) { + System.err.printf("dtrace: %s\n", ce.getMessage()); + } else { + ce.printStackTrace(); + } + } else { + e.printStackTrace(); + } + exit(1); + } + }; + + Getopt g = new Getopt(CLASSNAME, args, OPTSTR); + int c = 0; + + List <Consumer.OpenFlag> openFlags = + new ArrayList <Consumer.OpenFlag> (); + + while ((c = g.getopt()) != -1) { + switch (c) { + case '3': { + String s = g.getOptarg(); + if (!s.equals("2")) { + System.err.println("dtrace: illegal option -- 3" + s); + usage(); + } + openFlags.add(Consumer.OpenFlag.ILP32); + break; + } + case '6': { + String s = g.getOptarg(); + if (!s.equals("4")) { + System.err.println("dtrace: illegal option -- 6" + s); + usage(); + } + openFlags.add(Consumer.OpenFlag.LP64); + break; + } + } + } + + Consumer.OpenFlag[] oflags = new Consumer.OpenFlag[openFlags.size()]; + oflags = openFlags.toArray(oflags); + + dtrace = new LocalConsumer() { + protected Thread createThread() { + Thread t = super.createThread(); + t.setDaemon(false); + t.setPriority(Thread.MIN_PRIORITY); + return t; + } + }; + + g = new Getopt(CLASSNAME, args, OPTSTR); + c = 0; + + try { + dtrace.open(oflags); + + // Set default options that may be overriden by options or #pragma + dtrace.setOption(Option.bufsize, Option.mb(4)); + dtrace.setOption(Option.aggsize, Option.mb(4)); + + CompileRequest r; + while ((c = g.getopt()) != -1) { + switch (c) { + case 'b': + dtrace.setOption(Option.bufsize, g.getOptarg()); + break; + case 'c': + dtrace.createProcess(g.getOptarg()); + break; + case 'C': + dtrace.setOption(Option.cpp); + break; + case 'D': + dtrace.setOption(Option.define, g.getOptarg()); + break; + case 'e': + mode = Mode.INFO; + break; + case 'f': + r = new CompileRequest(); + r.s = g.getOptarg(); + r.type = ProgramType.STRING; + r.probespec = ProbeDescription.Spec.FUNCTION; + compileRequests.add(r); + break; + case 'F': + dtrace.setOption(Option.flowindent); + break; + case 'i': + r = new CompileRequest(); + r.s = g.getOptarg(); + r.type = ProgramType.STRING; + r.probespec = ProbeDescription.Spec.NAME; + compileRequests.add(r); + break; + case 'I': + dtrace.setOption(Option.incdir, g.getOptarg()); + break; + case 'l': + mode = Mode.LIST; + dtrace.setOption(Option.zdefs); // -l implies -Z + break; + case 'L': + dtrace.setOption(Option.libdir, g.getOptarg()); + break; + case 'm': + r = new CompileRequest(); + r.s = g.getOptarg(); + r.type = ProgramType.STRING; + r.probespec = ProbeDescription.Spec.MODULE; + compileRequests.add(r); + break; + case 'n': + r = new CompileRequest(); + r.s = g.getOptarg(); + r.type = ProgramType.STRING; + r.probespec = ProbeDescription.Spec.NAME; + compileRequests.add(r); + break; + case 'o': + String outFileName = g.getOptarg(); + File outFile = new File(outFileName); + try { + FileOutputStream fos = new FileOutputStream( + outFile, true); + out = new PrintStream(fos); + } catch (FileNotFoundException e) { + System.err.println("failed to open " + + outFileName + " in write mode"); + exit(1); + } catch (SecurityException e) { + System.err.println("failed to open " + + outFileName); + exit(1); + } + break; + case 'p': + String pidstr = g.getOptarg(); + int pid = -1; + try { + pid = Integer.parseInt(pidstr); + } catch (NumberFormatException e) { + System.err.println("invalid pid: " + pidstr); + exit(1); + } + dtrace.grabProcess(pid); + break; + case 'P': + r = new CompileRequest(); + r.s = g.getOptarg(); + r.type = ProgramType.STRING; + r.probespec = ProbeDescription.Spec.PROVIDER; + compileRequests.add(r); + break; + case 'q': + dtrace.setOption(Option.quiet); + break; + case 's': + r = new CompileRequest(); + r.s = g.getOptarg(); + r.type = ProgramType.FILE; + compileRequests.add(r); + break; + case 'U': + dtrace.setOption(Option.undef, g.getOptarg()); + break; + case 'v': + verbose = true; + break; + case 'V': + mode = Mode.VERSION; + break; + case 'w': + dtrace.setOption(Option.destructive); + break; + case 'x': + String[] xarg = g.getOptarg().split("=", 2); + if (xarg.length > 1) { + dtrace.setOption(xarg[0], xarg[1]); + } else if (xarg.length == 1) { + dtrace.setOption(xarg[0]); + } + break; + case 'X': + dtrace.setOption(Option.stdc, g.getOptarg()); + break; + case 'Z': + dtrace.setOption(Option.zdefs); + break; + case '?': + usage(); // getopt() already printed an error + break; + default: + System.err.print("getopt() returned " + c + "\n"); + c = 0; + } + } + c = 0; + List <String> argList = new LinkedList <String> (); + for (int i = g.getOptind(); i < args.length; ++i) { + argList.add(args[i]); + } + + if (mode == Mode.VERSION) { + out.printf("dtrace: %s\n", dtrace.getVersion()); + dtrace.close(); + exit(0); + } + + String[] compileArgs = new String[argList.size()]; + compileArgs = argList.toArray(compileArgs); + + Program program; + for (CompileRequest req : compileRequests) { + switch (req.type) { + case STRING: + applyProbespec(req); + program = dtrace.compile(req.s, compileArgs); + break; + case FILE: + File file = new File(req.s); + program = dtrace.compile(file, compileArgs); + break; + default: + throw new IllegalArgumentException( + "Unexpected program type: " + req.type); + } + + programList.add(program); + } + + // Get options set by #pragmas in compiled program + long optval; + quiet = (dtrace.getOption(Option.quiet) != Option.UNSET); + flow = (dtrace.getOption(Option.flowindent) != Option.UNSET); + optval = dtrace.getOption("stackindent"); + if (optval != Option.UNSET) { + stackindent = (int)optval; + } + + if (mode == Mode.LIST) { + out.printf("%5s %10s %17s %33s %s\n", + "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME"); + + if (verbose) { + List <List <Probe>> lists = + new LinkedList <List <Probe>> (); + for (Program p : programList) { + lists.add(dtrace.listProgramProbeDetail(p)); + } + ProbeDescription p; + ProbeInfo pinfo; + for (List <Probe> list : lists) { + for (Probe probe : list) { + p = probe.getDescription(); + pinfo = probe.getInfo(); + printProbeDescription(p); + printProbeInfo(pinfo); + } + } + } else { + List <List <ProbeDescription>> lists = + new LinkedList <List <ProbeDescription>> (); + for (Program p : programList) { + lists.add(dtrace.listProgramProbes(p)); + } + for (List <ProbeDescription> list : lists) { + for (ProbeDescription p : list) { + printProbeDescription(p); + } + } + } + exit(0); + } + + String programType; + String programDescription; + ProgramInfo info; + for (Program p : programList) { + if (p instanceof Program.File) { + Program.File pf = (Program.File)p; + programType = "script"; + programDescription = pf.getFile().getPath(); + } else { + programType = "description"; + programDescription = + p.getContents().split("[/{;]", 2)[0]; + } + + if (mode == Mode.EXEC) { + dtrace.enable(p); + } else { + dtrace.getProgramInfo(p); + } + info = p.getInfo(); + if ((mode == Mode.EXEC) && !quiet) { + System.err.printf("dtrace: %s '%s' matched %d probe%s\n", + programType, programDescription, + info.getMatchingProbeCount(), + info.getMatchingProbeCount() == 1 ? "" : "s"); + } + if (verbose) { + printProgramStability(programType, + programDescription, info); + } + } + if (mode != Mode.EXEC) { + exit(0); + } + dtrace.addConsumerListener(new ConsumerAdapter() { + public void consumerStarted(ConsumerEvent e) { + started = true; + } + public void consumerStopped(ConsumerEvent e) { + stopped = true; + out.println(); + try { + Aggregate aggregate = dtrace.getAggregate(); + if (aggregate != null) { + printAggregate(aggregate); + } + dtrace.close(); + } catch (Throwable x) { + exceptionHandler.handleException(x); + } + exit(0); + } + public void dataDropped(DropEvent e) { + System.err.printf("dtrace: %s", + e.getDrop().getDefaultMessage()); + } + public void errorEncountered(ErrorEvent e) + throws ConsumerException { + org.opensolaris.os.dtrace.Error error = e.getError(); + if (logger.isLoggable(Level.FINE)) { + logger.fine(error.toString()); + } + System.err.printf("dtrace: %s", + error.getDefaultMessage()); + } + public void dataReceived(DataEvent e) + throws ConsumerException { + consumeProbeData(e.getProbeData()); + } + public void processStateChanged(ProcessEvent e) + throws ConsumerException { + if (logger.isLoggable(Level.FINE)) { + logger.fine(e.getProcessState().toString()); + } + } + }); + // Print unprinted aggregations after Ctrl-C + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + if (stopped || !started) { + return; + } + + try { + Aggregate aggregate = dtrace.getAggregate(); + if (aggregate != null) { + out.println(); + out.println(); + printAggregate(aggregate); + } + } catch (Throwable x) { + exceptionHandler.handleException(x); + } + } + }); + dtrace.go(exceptionHandler); + } catch (DTraceException e) { + if (c > 0) { + // set option error + if (g.getOptarg() == null) { + System.err.printf("dtrace: failed to set -%c: %s\n", + c, e.getMessage()); + } else { + System.err.printf("dtrace: failed to set -%c %s: %s\n", + c, g.getOptarg(), e.getMessage()); + } + } else { + // any other error + System.err.printf("dtrace: %s\n", e.getMessage()); + } + exit(1); + } catch (Exception e) { + e.printStackTrace(); + exit(1); + } + } +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/exception.lst b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/exception.lst new file mode 100644 index 000000000000..19fc3aca51c2 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/exception.lst @@ -0,0 +1,79 @@ +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE +# or http://www.opensolaris.org/os/licensing. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at usr/src/OPENSOLARIS.LICENSE. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# + +# +# Copyright 2008 Sun Microsystems, Inc. All rights reserved. +# Use is subject to license terms. +# + +# Exception list: names tests that are bypassed when running in Java +# mode (relative to /opt/SUNWdtrt/tst) + +# double precision (64-bit floating point) not same in java +common/aggs/tst.neglquant.d +common/aggs/tst.negquant.d + +# freopen() (to suppress output) not supported by Java DTrace API +common/printa/tst.walltimestamp.ksh + +# -G option not supported by jdtrace +common/dtraceUtil/tst.ELFGenerationOut.d.ksh +common/dtraceUtil/tst.ELFGenerationWithO.d.ksh + +# -H option not supported by jdtrace +common/dtraceUtil/tst.PreprocessorStatement.d.ksh + +# -G and -h options not supported by jdtrace +common/usdt/tst.badguess.ksh +common/usdt/tst.dlclose1.ksh +common/usdt/tst.dlclose2.ksh +common/usdt/tst.dlclose3.ksh +common/usdt/tst.eliminate.ksh +common/usdt/tst.enabled.ksh +common/usdt/tst.enabled2.ksh +common/usdt/tst.entryreturn.ksh +common/usdt/tst.fork.ksh +common/usdt/tst.guess32.ksh +common/usdt/tst.guess64.ksh +common/usdt/tst.header.ksh +common/usdt/tst.linkpriv.ksh +common/usdt/tst.linkunpriv.ksh +common/usdt/tst.multiple.ksh +common/usdt/tst.nodtrace.ksh +common/usdt/tst.noreap.ksh +common/usdt/tst.noreapring.ksh +common/usdt/tst.onlyenabled.ksh +common/usdt/tst.reap.ksh +common/usdt/tst.reeval.ksh +common/usdt/tst.static.ksh +common/usdt/tst.static2.ksh +common/usdt/tst.user.ksh +sparc/usdt/tst.tailcall.ksh +common/pid/tst.provregex3.ksh +common/pid/tst.provregex4.ksh + +# freopen() and ftruncate() not supported by Java DTrace API +common/funcs/tst.badfreopen.ksh +common/funcs/tst.freopen.ksh +common/funcs/tst.ftruncate.ksh + +# jdtrace doesn't pull in library files? +common/pragma/tst.libdepfullyconnected.ksh diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/jdtrace.c b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/jdtrace.c new file mode 100644 index 000000000000..095126569cc1 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/jdtrace.c @@ -0,0 +1,60 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#pragma ident "%Z%%M% %I% %E% SMI" + +#include <alloca.h> +#include <stdio.h> +#include <unistd.h> +#include <sys/systeminfo.h> + +int +main(int argc, char **argv) +{ + int i, ac, has64; + char **av, **p; + + ac = argc + 3; + av = p = alloca(sizeof (char *) * ac); + + *p++ = "java"; + *p++ = "-jar"; + *p++ = "/opt/SUNWdtrt/lib/java/jdtrace.jar"; + + argc--; + argv++; + + for (i = 0; i < argc; i++) { + p[i] = argv[i]; + } + p[i] = NULL; + + (void) execvp(av[0], av); + + perror("exec failed"); + + return (0); +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/manifest/jdtrace.jar-manifest b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/manifest/jdtrace.jar-manifest new file mode 100644 index 000000000000..add47ebf4c8d --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/jdtrace/manifest/jdtrace.jar-manifest @@ -0,0 +1,3 @@ +Manifest-Version: 1.0
+Main-Class: JDTrace
+Class-Path: /usr/share/lib/java/dtrace.jar
diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/scripts/dstyle.pl b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/scripts/dstyle.pl new file mode 100755 index 000000000000..30b54d850733 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/scripts/dstyle.pl @@ -0,0 +1,240 @@ +#!/usr/local/bin/perl +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE +# or http://www.opensolaris.org/os/licensing. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at usr/src/OPENSOLARIS.LICENSE. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# + +# +# Copyright 2008 Sun Microsystems, Inc. All rights reserved. +# Use is subject to license terms. +# + +# +# Copyright (c) 2014, 2016 by Delphix. All rights reserved. +# + +require 5.8.4; + +$PNAME = $0; +$PNAME =~ s:.*/::; +$USAGE = "Usage: $PNAME [file ...]\n"; +$errs = 0; + +sub err +{ + my($msg) = @_; + + print "$file: $lineno: $msg\n"; + $errs++; +} + +sub dstyle +{ + open(FILE, "$file"); + $lineno = 0; + $inclause = 0; + $skipnext = 0; + + while (<FILE>) { + $lineno++; + + chop; + + if ($skipnext) { + $skipnext = 0; + next; + } + + # + # Amazingly, some ident strings are longer than 80 characters! + # + if (/^#pragma ident/) { + next; + } + + # + # The algorithm to calculate line length from cstyle. + # + $line = $_; + if ($line =~ tr/\t/\t/ * 7 + length($line) > 80) { + # yes, there is a chance. + # replace tabs with spaces and check again. + $eline = $line; + 1 while $eline =~ + s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e; + + if (length($eline) > 80) { + err "line > 80 characters"; + } + } + + if (/\/\*DSTYLED\*\//) { + $skipnext = 1; + next; + } + + if (/^#pragma/) { + next; + } + + if (/^#include/) { + next; + } + + # + # Before we do any more analysis, we want to prune out any + # quoted strings. This is a bit tricky because we need + # to be careful of backslashed quotes within quoted strings. + # I'm sure there is a very crafty way to do this with a + # single regular expression, but that will have to wait for + # somone with better regex juju that I; we do this by first + # eliminating the backslashed quotes, and then eliminating + # whatever quoted strings are left. Note that we eliminate + # the string by replacing it with "quotedstr"; this is to + # allow lines to end with a quoted string. (If we simply + # eliminated the quoted string, dstyle might complain about + # the line ending in a space or tab.) + # + s/\\\"//g; + s/\"[^\"]*\"/quotedstr/g; + + if (/[ \t]$/) { + err "space or tab at end of line"; + } + + if (/^[\t]+[ ]+[\t]+/) { + err "spaces between tabs"; + } + + if (/^[\t]* \*/) { + next; + } + + if (/^ /) { + err "indented by spaces not tabs"; + } + + if (/^{}$/) { + next; + } + + if (!/^enum/ && !/^\t*struct/ && !/^\t*union/ && !/^typedef/ && + !/^translator/ && !/^provider/ && !/\tif / && + !/ else /) { + if (/[\w\s]+{/) { + err "left brace not on its own line"; + } + + if (/{[\w\s]+/) { + err "left brace not on its own line"; + } + } + + if (!/;$/ && !/\t*}$/ && !/ else /) { + if (/[\w\s]+}/) { + err "right brace not on its own line"; + } + + if (/}[\w\s]+/) { + err "right brace not on its own line"; + } + } + + if (/^}/) { + $inclause = 0; + } + + if (!$inclause && /^[\w ]+\//) { + err "predicate not at beginning of line"; + } + + if (!$inclause && /^\/[ \t]+\w/) { + err "space between '/' and expression in predicate"; + } + + if (!$inclause && /\w[ \t]+\/$/) { + err "space between expression and '/' in predicate"; + } + + if (!$inclause && /\s,/) { + err "space before comma in probe description"; + } + + if (!$inclause && /\w,[\w\s]/ && !/;$/) { + if (!/extern/ && !/\(/ && !/inline/) { + err "multiple probe descriptions on same line"; + } + } + + if ($inclause && /sizeof\(/) { + err "missing space after sizeof"; + } + + if ($inclause && /^[\w ]/) { + err "line doesn't begin with a tab"; + } + + if ($inclause && /,[\w]/) { + err "comma without trailing space"; + } + + if (/\w&&/ || /&&\w/ || /\w\|\|/ || /\|\|\w/) { + err "logical operator not set off with spaces"; + } + + # + # We want to catch "i<0" variants, but we don't want to + # erroneously flag translators. + # + if (!/\w<\w+>\(/) { + if (/\w>/ || / >\w/ || /\w</ || /<\w/) { + err "comparison operator not set " . + "off with spaces"; + } + } + + if (/\w==/ || /==\w/ || /\w<=/ || />=\w/ || /\w!=/ || /!=\w/) { + err "comparison operator not set off with spaces"; + } + + if (/\w=/ || /=\w/) { + err "assignment operator not set off with spaces"; + } + + if (/^{/) { + $inclause = 1; + } + } +} + +foreach $arg (@ARGV) { + if (-f $arg) { + push(@files, $arg); + } else { + die "$PNAME: $arg is not a valid file\n"; + } +} + +die $USAGE if (scalar(@files) == 0); + +foreach $file (@files) { + dstyle($file); +} + +exit($errs != 0); diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/scripts/dtest.pl b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/scripts/dtest.pl new file mode 100755 index 000000000000..d5a7244f8ebb --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/cmd/scripts/dtest.pl @@ -0,0 +1,706 @@ +#!/usr/local/bin/perl +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE +# or http://www.opensolaris.org/os/licensing. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at usr/src/OPENSOLARIS.LICENSE. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# + +# +# Copyright 2008 Sun Microsystems, Inc. All rights reserved. +# Use is subject to license terms. +# + +require 5.8.4; + +use File::Find; +use File::Basename; +use Getopt::Std; +use Cwd; +use Cwd 'abs_path'; + +$PNAME = $0; +$PNAME =~ s:.*/::; +$OPTSTR = 'abd:fghi:jlnqsx:'; +$USAGE = "Usage: $PNAME [-abfghjlnqs] [-d dir] [-i isa] " + . "[-x opt[=arg]] [file | dir ...]\n"; +($MACH = `uname -p`) =~ s/\W*\n//; +($PLATFORM = `uname -i`) =~ s/\W*\n//; + +@dtrace_argv = (); + +$ksh_path = '/usr/local/bin/ksh'; + +@files = (); +%exceptions = (); +%results = (); +$errs = 0; + +# +# If no test files are specified on the command-line, execute a find on "." +# and append any tst.*.d, tst.*.ksh, err.*.d or drp.*.d files found within +# the directory tree. +# +sub wanted +{ + push(@files, $File::Find::name) + if ($_ =~ /^(tst|err|drp)\..+\.(d|ksh)$/ && -f "$_"); +} + +sub dirname { + my($s) = @_; + my($i); + + $s = substr($s, 0, $i) if (($i = rindex($s, '/')) != -1); + return $i == -1 ? '.' : $i == 0 ? '/' : $s; +} + +sub usage +{ + print $USAGE; + print "\t -a execute test suite using anonymous enablings\n"; + print "\t -b execute bad ioctl test program\n"; + print "\t -d specify directory for test results files and cores\n"; + print "\t -g enable libumem debugging when running tests\n"; + print "\t -f force bypassed tests to run\n"; + print "\t -h display verbose usage message\n"; + print "\t -i specify ISA to test instead of isaexec(3C) default\n"; + print "\t -j execute test suite using jdtrace (Java API) only\n"; + print "\t -l save log file of results and PIDs used by tests\n"; + print "\t -n execute test suite using dtrace(1m) only\n"; + print "\t -q set quiet mode (only report errors and summary)\n"; + print "\t -s save results files even for tests that pass\n"; + print "\t -x pass corresponding -x argument to dtrace(1M)\n"; + exit(2); +} + +sub errmsg +{ + my($msg) = @_; + + print STDERR $msg; + print LOG $msg if ($opt_l); + $errs++; +} + +sub fail +{ + my(@parms) = @_; + my($msg) = $parms[0]; + my($errfile) = $parms[1]; + my($n) = 0; + my($dest) = basename($file); + + while (-d "$opt_d/failure.$n") { + $n++; + } + + unless (mkdir "$opt_d/failure.$n") { + warn "ERROR: failed to make directory $opt_d/failure.$n: $!\n"; + exit(125); + } + + open(README, ">$opt_d/failure.$n/README"); + print README "ERROR: " . $file . " " . $msg; + + if (scalar @parms > 1) { + print README "; see $errfile\n"; + } else { + if (-f "$opt_d/$pid.core") { + print README "; see $pid.core\n"; + } else { + print README "\n"; + } + } + + close(README); + + if (-f "$opt_d/$pid.out") { + rename("$opt_d/$pid.out", "$opt_d/failure.$n/$pid.out"); + link("$file.out", "$opt_d/failure.$n/$dest.out"); + } + + if (-f "$opt_d/$pid.err") { + rename("$opt_d/$pid.err", "$opt_d/failure.$n/$pid.err"); + link("$file.err", "$opt_d/failure.$n/$dest.err"); + } + + if (-f "$opt_d/$pid.core") { + rename("$opt_d/$pid.core", "$opt_d/failure.$n/$pid.core"); + } + + link("$file", "$opt_d/failure.$n/$dest"); + + $msg = "ERROR: " . $dest . " " . $msg; + + if (scalar @parms > 1) { + $msg = $msg . "; see $errfile in failure.$n\n"; + } else { + $msg = $msg . "; details in failure.$n\n"; + } + + errmsg($msg); +} + +sub logmsg +{ + my($msg) = @_; + + print STDOUT $msg unless ($opt_q); + print LOG $msg if ($opt_l); +} + +# Trim leading and trailing whitespace +sub trim { + my($s) = @_; + + $s =~ s/^\s*//; + $s =~ s/\s*$//; + return $s; +} + +# Load exception set of skipped tests from the file at the given +# pathname. The test names are assumed to be paths relative to $dt_tst, +# for example: common/aggs/tst.neglquant.d, and specify tests to be +# skipped. +sub load_exceptions { + my($listfile) = @_; + my($line) = ""; + + %exceptions = (); + if (length($listfile) > 0) { + exit(123) unless open(STDIN, "<$listfile"); + while (<STDIN>) { + chomp; + $line = $_; + # line is non-empty and not a comment + if ((length($line) > 0) && ($line =~ /^\s*[^\s#]/ )) { + $exceptions{trim($line)} = 1; + } + } + } +} + +# Return 1 if the test is found in the exception set, 0 otherwise. +sub is_exception { + my($file) = @_; + my($i) = -1; + + if (scalar(keys(%exceptions)) == 0) { + return 0; + } + + # hash absolute pathname after $dt_tst/ + $file = abs_path($file); + $i = index($file, $dt_tst); + if ($i == 0) { + $file = substr($file, length($dt_tst) + 1); + return $exceptions{$file}; + } + return 0; +} + +# +# Iterate over the set of test files specified on the command-line or by a find +# on "$defdir/common", "$defdir/$MACH" and "$defdir/$PLATFORM" and execute each +# one. If the test file is executable, we fork and exec it. If the test is a +# .ksh file, we run it with $ksh_path. Otherwise we run dtrace -s on it. If +# the file is named tst.* we assume it should return exit status 0. If the +# file is named err.* we assume it should return exit status 1. If the file is +# named err.D_[A-Z0-9]+[.*].d we use dtrace -xerrtags and examine stderr to +# ensure that a matching error tag was produced. If the file is named +# drp.[A-Z0-9]+[.*].d we use dtrace -xdroptags and examine stderr to ensure +# that a matching drop tag was produced. If any *.out or *.err files are found +# we perform output comparisons. +# +# run_tests takes two arguments: The first is the pathname of the dtrace +# command to invoke when running the tests. The second is the pathname +# of a file (may be the empty string) listing tests that ought to be +# skipped (skipped tests are listed as paths relative to $dt_tst, for +# example: common/aggs/tst.neglquant.d). +# +sub run_tests { + my($dtrace, $exceptions_path) = @_; + my($passed) = 0; + my($bypassed) = 0; + my($failed) = $errs; + my($total) = 0; + + die "$PNAME: $dtrace not found\n" unless (-x "$dtrace"); + logmsg($dtrace . "\n"); + + load_exceptions($exceptions_path); + + foreach $file (sort @files) { + $file =~ m:.*/((.*)\.(\w+)):; + $name = $1; + $base = $2; + $ext = $3; + + $dir = dirname($file); + $isksh = 0; + $tag = 0; + $droptag = 0; + + if ($name =~ /^tst\./) { + $isksh = ($ext eq 'ksh'); + $status = 0; + } elsif ($name =~ /^err\.(D_[A-Z0-9_]+)\./) { + $status = 1; + $tag = $1; + } elsif ($name =~ /^err\./) { + $status = 1; + } elsif ($name =~ /^drp\.([A-Z0-9_]+)\./) { + $status = 0; + $droptag = $1; + } else { + errmsg("ERROR: $file is not a valid test file name\n"); + next; + } + + $fullname = "$dir/$name"; + $exe = "$dir/$base.exe"; + $exe_pid = -1; + + if ($opt_a && ($status != 0 || $tag != 0 || $droptag != 0 || + -x $exe || $isksh || -x $fullname)) { + $bypassed++; + next; + } + + if (!$opt_f && is_exception("$dir/$name")) { + $bypassed++; + next; + } + + if (!$isksh && -x $exe) { + if (($exe_pid = fork()) == -1) { + errmsg( + "ERROR: failed to fork to run $exe: $!\n"); + next; + } + + if ($exe_pid == 0) { + open(STDIN, '</dev/null'); + + exec($exe); + + warn "ERROR: failed to exec $exe: $!\n"; + } + } + + logmsg("testing $file ... "); + + if (($pid = fork()) == -1) { + errmsg("ERROR: failed to fork to run test $file: $!\n"); + next; + } + + if ($pid == 0) { + open(STDIN, '</dev/null'); + exit(125) unless open(STDOUT, ">$opt_d/$$.out"); + exit(125) unless open(STDERR, ">$opt_d/$$.err"); + + unless (chdir($dir)) { + warn "ERROR: failed to chdir for $file: $!\n"; + exit(126); + } + + push(@dtrace_argv, '-xerrtags') if ($tag); + push(@dtrace_argv, '-xdroptags') if ($droptag); + push(@dtrace_argv, $exe_pid) if ($exe_pid != -1); + + if ($isksh) { + exit(123) unless open(STDIN, "<$name"); + exec("$ksh_path /dev/stdin $dtrace"); + } elsif (-x $name) { + warn "ERROR: $name is executable\n"; + exit(1); + } else { + if ($tag == 0 && $status == $0 && $opt_a) { + push(@dtrace_argv, '-A'); + } + + push(@dtrace_argv, '-C'); + push(@dtrace_argv, '-s'); + push(@dtrace_argv, $name); + exec($dtrace, @dtrace_argv); + } + + warn "ERROR: failed to exec for $file: $!\n"; + exit(127); + } + + if (waitpid($pid, 0) == -1) { + errmsg("ERROR: timed out waiting for $file\n"); + kill(9, $exe_pid) if ($exe_pid != -1); + kill(9, $pid); + next; + } + + kill(9, $exe_pid) if ($exe_pid != -1); + + if ($tag == 0 && $status == $0 && $opt_a) { + # + # We can chuck the earler output. + # + unlink($pid . '.out'); + unlink($pid . '.err'); + + # + # This is an anonymous enabling. We need to get + # the module unloaded. + # + system("dtrace -ae 1> /dev/null 2> /dev/null"); + system("svcadm disable -s " . + "svc:/network/nfs/mapid:default"); + system("modunload -i 0 ; modunload -i 0 ; " . + "modunload -i 0"); + if (!system("modinfo | grep dtrace")) { + warn "ERROR: couldn't unload dtrace\n"; + system("svcadm enable " . + "-s svc:/network/nfs/mapid:default"); + exit(124); + } + + # + # DTrace is gone. Now update_drv(1M), and rip + # everything out again. + # + system("update_drv dtrace"); + system("dtrace -ae 1> /dev/null 2> /dev/null"); + system("modunload -i 0 ; modunload -i 0 ; " . + "modunload -i 0"); + if (!system("modinfo | grep dtrace")) { + warn "ERROR: couldn't unload dtrace\n"; + system("svcadm enable " . + "-s svc:/network/nfs/mapid:default"); + exit(124); + } + + # + # Now bring DTrace back in. + # + system("sync ; sync"); + system("dtrace -l -n bogusprobe 1> /dev/null " . + "2> /dev/null"); + system("svcadm enable -s " . + "svc:/network/nfs/mapid:default"); + + # + # That should have caused DTrace to reload with + # the new configuration file. Now we can try to + # snag our anonymous state. + # + if (($pid = fork()) == -1) { + errmsg("ERROR: failed to fork to run " . + "test $file: $!\n"); + next; + } + + if ($pid == 0) { + open(STDIN, '</dev/null'); + exit(125) unless open(STDOUT, ">$opt_d/$$.out"); + exit(125) unless open(STDERR, ">$opt_d/$$.err"); + + push(@dtrace_argv, '-a'); + + unless (chdir($dir)) { + warn "ERROR: failed to chdir " . + "for $file: $!\n"; + exit(126); + } + + exec($dtrace, @dtrace_argv); + warn "ERROR: failed to exec for $file: $!\n"; + exit(127); + } + + if (waitpid($pid, 0) == -1) { + errmsg("ERROR: timed out waiting for $file\n"); + kill(9, $pid); + next; + } + } + + logmsg("[$pid]\n"); + $wstat = $?; + $wifexited = ($wstat & 0xFF) == 0; + $wexitstat = ($wstat >> 8) & 0xFF; + $wtermsig = ($wstat & 0x7F); + + if (!$wifexited) { + fail("died from signal $wtermsig"); + next; + } + + if ($wexitstat == 125) { + die "$PNAME: failed to create output file in $opt_d " . + "(cd elsewhere or use -d)\n"; + } + + if ($wexitstat != $status) { + fail("returned $wexitstat instead of $status"); + next; + } + + if (-f "$file.out" && + system("cmp -s $file.out $opt_d/$pid.out") != 0) { + fail("stdout mismatch", "$pid.out"); + next; + } + + if (-f "$file.err" && + system("cmp -s $file.err $opt_d/$pid.err") != 0) { + fail("stderr mismatch: see $pid.err"); + next; + } + + if ($tag) { + open(TSTERR, "<$opt_d/$pid.err"); + $tsterr = <TSTERR>; + close(TSTERR); + + unless ($tsterr =~ /: \[$tag\] line \d+:/) { + fail("errtag mismatch: see $pid.err"); + next; + } + } + + if ($droptag) { + $found = 0; + open(TSTERR, "<$opt_d/$pid.err"); + + while (<TSTERR>) { + if (/\[$droptag\] /) { + $found = 1; + last; + } + } + + close (TSTERR); + + unless ($found) { + fail("droptag mismatch: see $pid.err"); + next; + } + } + + unless ($opt_s) { + unlink($pid . '.out'); + unlink($pid . '.err'); + } + } + + if ($opt_a) { + # + # If we're running with anonymous enablings, we need to + # restore the .conf file. + # + system("dtrace -A 1> /dev/null 2> /dev/null"); + system("dtrace -ae 1> /dev/null 2> /dev/null"); + system("modunload -i 0 ; modunload -i 0 ; modunload -i 0"); + system("update_drv dtrace"); + } + + $total = scalar(@files); + $failed = $errs - $failed; + $passed = ($total - $failed - $bypassed); + $results{$dtrace} = { + "passed" => $passed, + "bypassed" => $bypassed, + "failed" => $failed, + "total" => $total + }; +} + +die $USAGE unless (getopts($OPTSTR)); +usage() if ($opt_h); + +foreach $arg (@ARGV) { + if (-f $arg) { + push(@files, $arg); + } elsif (-d $arg) { + find(\&wanted, $arg); + } else { + die "$PNAME: $arg is not a valid file or directory\n"; + } +} + +$dt_tst = '/opt/SUNWdtrt/tst'; +$dt_bin = '/opt/SUNWdtrt/bin'; +$defdir = -d $dt_tst ? $dt_tst : '.'; +$bindir = -d $dt_bin ? $dt_bin : '.'; + +find(\&wanted, "$defdir/common") if (scalar(@ARGV) == 0); +find(\&wanted, "$defdir/$MACH") if (scalar(@ARGV) == 0); +find(\&wanted, "$defdir/$PLATFORM") if (scalar(@ARGV) == 0); +die $USAGE if (scalar(@files) == 0); + +$dtrace_path = '/usr/sbin/dtrace'; +$jdtrace_path = "$bindir/jdtrace"; + +%exception_lists = ("$jdtrace_path" => "$bindir/exception.lst"); + +if ($opt_j || $opt_n || $opt_i) { + @dtrace_cmds = (); + push(@dtrace_cmds, $dtrace_path) if ($opt_n); + push(@dtrace_cmds, $jdtrace_path) if ($opt_j); + push(@dtrace_cmds, "/usr/sbin/$opt_i/dtrace") if ($opt_i); +} else { + @dtrace_cmds = ($dtrace_path, $jdtrace_path); +} + +if ($opt_d) { + die "$PNAME: -d arg must be absolute path\n" unless ($opt_d =~ /^\//); + die "$PNAME: -d arg $opt_d is not a directory\n" unless (-d "$opt_d"); + system("coreadm -p $opt_d/%p.core"); +} else { + my $dir = getcwd; + system("coreadm -p $dir/%p.core"); + $opt_d = '.'; +} + +if ($opt_x) { + push(@dtrace_argv, '-x'); + push(@dtrace_argv, $opt_x); +} + +die "$PNAME: failed to open $PNAME.$$.log: $!\n" + unless (!$opt_l || open(LOG, ">$PNAME.$$.log")); + +$ENV{'DTRACE_DEBUG_REGSET'} = 'true'; + +if ($opt_g) { + $ENV{'UMEM_DEBUG'} = 'default,verbose'; + $ENV{'UMEM_LOGGING'} = 'fail,contents'; + $ENV{'LD_PRELOAD'} = 'libumem.so'; +} + +# +# Ensure that $PATH contains a cc(1) so that we can execute the +# test programs that require compilation of C code. +# +#$ENV{'PATH'} = $ENV{'PATH'} . ':/ws/onnv-tools/SUNWspro/SS11/bin'; + +if ($opt_b) { + logmsg("badioctl'ing ... "); + + if (($badioctl = fork()) == -1) { + errmsg("ERROR: failed to fork to run badioctl: $!\n"); + next; + } + + if ($badioctl == 0) { + open(STDIN, '</dev/null'); + exit(125) unless open(STDOUT, ">$opt_d/$$.out"); + exit(125) unless open(STDERR, ">$opt_d/$$.err"); + + exec($bindir . "/badioctl"); + warn "ERROR: failed to exec badioctl: $!\n"; + exit(127); + } + + + logmsg("[$badioctl]\n"); + + # + # If we're going to be bad, we're just going to iterate over each + # test file. + # + foreach $file (sort @files) { + ($name = $file) =~ s:.*/::; + $dir = dirname($file); + + if (!($name =~ /^tst\./ && $name =~ /\.d$/)) { + next; + } + + logmsg("baddof'ing $file ... "); + + if (($pid = fork()) == -1) { + errmsg("ERROR: failed to fork to run baddof: $!\n"); + next; + } + + if ($pid == 0) { + open(STDIN, '</dev/null'); + exit(125) unless open(STDOUT, ">$opt_d/$$.out"); + exit(125) unless open(STDERR, ">$opt_d/$$.err"); + + unless (chdir($dir)) { + warn "ERROR: failed to chdir for $file: $!\n"; + exit(126); + } + + exec($bindir . "/baddof", $name); + + warn "ERROR: failed to exec for $file: $!\n"; + exit(127); + } + + sleep 60; + kill(9, $pid); + waitpid($pid, 0); + + logmsg("[$pid]\n"); + + unless ($opt_s) { + unlink($pid . '.out'); + unlink($pid . '.err'); + } + } + + kill(9, $badioctl); + waitpid($badioctl, 0); + + unless ($opt_s) { + unlink($badioctl . '.out'); + unlink($badioctl . '.err'); + } + + exit(0); +} + +# +# Run all the tests specified on the command-line (the entire test suite +# by default) once for each dtrace command tested, skipping any tests +# not valid for that command. +# +foreach $dtrace_cmd (@dtrace_cmds) { + run_tests($dtrace_cmd, $exception_lists{$dtrace_cmd}); +} + +$opt_q = 0; # force final summary to appear regardless of -q option + +logmsg("\n==== TEST RESULTS ====\n"); +foreach $key (keys %results) { + my $passed = $results{$key}{"passed"}; + my $bypassed = $results{$key}{"bypassed"}; + my $failed = $results{$key}{"failed"}; + my $total = $results{$key}{"total"}; + + logmsg("\n mode: " . $key . "\n"); + logmsg(" passed: " . $passed . "\n"); + if ($bypassed) { + logmsg(" bypassed: " . $bypassed . "\n"); + } + logmsg(" failed: " . $failed . "\n"); + logmsg(" total: " . $total . "\n"); +} + +exit($errs != 0); |