1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2018, Matthew Macy
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29 #include <sys/types.h>
30 #include <sys/errno.h>
31 #include <sys/sysctl.h>
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <err.h>
35 #include <limits.h>
36 #include <string.h>
37 #include <pmc.h>
38 #include <pmclog.h>
39 #include <libelf.h>
40 #include <libpmcstat.h>
41 #include <sysexits.h>
42 #include <unistd.h>
43
44 #include "cmd_pmc.h"
45
46 int pmc_displayheight = DEFAULT_DISPLAY_HEIGHT;
47 int pmc_displaywidth = DEFAULT_DISPLAY_WIDTH;
48 int pmc_kq;
49 struct pmcstat_args pmc_args;
50
51 struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs);
52
53 struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH];
54
55 struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH];
56
57 struct cmd_handler {
58 const char *ch_name;
59 cmd_disp_t ch_fn;
60 const char *ch_desc;
61 };
62
63 static struct cmd_handler disp_table[] = {
64 { "filter", cmd_pmc_filter, NULL },
65 { "frontend", cmd_pmc_frontend, "Analyze front-end stalls" },
66 { "info", cmd_pmc_info, "Display high level information about a log" },
67 { "list-events", cmd_pmc_list_events, "List available PMC events" },
68 { "record", cmd_pmc_record, "Record a performance sample" },
69 { "stat", cmd_pmc_stat, NULL },
70 { "stat-system", cmd_pmc_stat_system, NULL },
71 { "summary", cmd_pmc_summary, NULL },
72 { NULL, NULL, NULL }
73 };
74
75 static void
usage(void)76 usage(void)
77 {
78 int i;
79
80 fprintf(stderr, "Usage: pmc <COMMAND> [OPTIONS] ...\n\n");
81 fprintf(stderr, "PMC tool\n\n");
82
83 fprintf(stderr, "Commands:\n");
84 for (i = 0; disp_table[i].ch_name != NULL; i++) {
85 // Hide stuff we plan to deprecate
86 if (disp_table[i].ch_desc)
87 fprintf(stderr, " %-12s %s\n",
88 disp_table[i].ch_name, disp_table[i].ch_desc);
89 }
90
91 fprintf(stderr, "\nEnvironment Variables:\n");
92 fprintf(stderr, " CLICOLOR Enables color graphs\n");
93 fprintf(stderr, " SYSROOT Path to system root\n");
94 fprintf(stderr, " SYMROOT Path to debug symbols\n");
95 fprintf(stderr, " TERM Controls symbols and color palettes\n");
96
97 exit(EX_USAGE);
98 }
99
100 static cmd_disp_t
disp_lookup(char * name)101 disp_lookup(char *name)
102 {
103 struct cmd_handler *hnd;
104
105 for (hnd = disp_table; hnd->ch_name != NULL; hnd++)
106 if (strcmp(hnd->ch_name, name) == 0)
107 return (hnd->ch_fn);
108 return (NULL);
109 }
110
111 int
main(int argc,char ** argv)112 main(int argc, char **argv)
113 {
114 cmd_disp_t disp;
115
116 pmc_args.pa_printfile = stderr;
117 STAILQ_INIT(&pmc_args.pa_events);
118 SLIST_INIT(&pmc_args.pa_targets);
119 if (argc == 1)
120 usage();
121 if ((disp = disp_lookup(argv[1])) == NULL)
122 usage();
123 argc--;
124 argv++;
125
126 /* Allocate a kqueue */
127 if ((pmc_kq = kqueue()) < 0)
128 err(EX_OSERR, "ERROR: Cannot allocate kqueue");
129
130 if (pmc_init() < 0)
131 err(EX_UNAVAILABLE,
132 "ERROR: Initialization of the pmc(3) library failed"
133 );
134
135 if (elf_version(EV_CURRENT) == EV_NONE)
136 err(EX_UNAVAILABLE, "ERROR: ELF library version too old");
137
138 return (disp(argc, argv));
139 }
140