1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino * Copyright (c) 2008 Yahoo!, Inc.
3*86d7f5d3SJohn Marino * All rights reserved.
4*86d7f5d3SJohn Marino * Written by: John Baldwin <jhb@FreeBSD.org>
5*86d7f5d3SJohn Marino *
6*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
7*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
8*86d7f5d3SJohn Marino * are met:
9*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
10*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
11*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
12*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
13*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
14*86d7f5d3SJohn Marino * 3. Neither the name of the author nor the names of any co-contributors
15*86d7f5d3SJohn Marino * may be used to endorse or promote products derived from this software
16*86d7f5d3SJohn Marino * without specific prior written permission.
17*86d7f5d3SJohn Marino *
18*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*86d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*86d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*86d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*86d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*86d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*86d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*86d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*86d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*86d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*86d7f5d3SJohn Marino * SUCH DAMAGE.
29*86d7f5d3SJohn Marino *
30*86d7f5d3SJohn Marino * $FreeBSD: src/usr.sbin/mptutil/mpt_evt.c,v 1.2 2010/11/09 19:28:06 jhb Exp $
31*86d7f5d3SJohn Marino */
32*86d7f5d3SJohn Marino
33*86d7f5d3SJohn Marino #include <sys/param.h>
34*86d7f5d3SJohn Marino #include <sys/errno.h>
35*86d7f5d3SJohn Marino #include <ctype.h>
36*86d7f5d3SJohn Marino #include <err.h>
37*86d7f5d3SJohn Marino #include <stdio.h>
38*86d7f5d3SJohn Marino #include <stdlib.h>
39*86d7f5d3SJohn Marino #include <unistd.h>
40*86d7f5d3SJohn Marino #include "mptutil.h"
41*86d7f5d3SJohn Marino
42*86d7f5d3SJohn Marino static CONFIG_PAGE_LOG_0 *
mpt_get_events(int fd,U16 * IOCStatus)43*86d7f5d3SJohn Marino mpt_get_events(int fd, U16 *IOCStatus)
44*86d7f5d3SJohn Marino {
45*86d7f5d3SJohn Marino
46*86d7f5d3SJohn Marino return (mpt_read_extended_config_page(fd, MPI_CONFIG_EXTPAGETYPE_LOG,
47*86d7f5d3SJohn Marino 0, 0, 0, IOCStatus));
48*86d7f5d3SJohn Marino }
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino /*
51*86d7f5d3SJohn Marino * 1 2 3 4 5 6 7
52*86d7f5d3SJohn Marino * 1234567890123456789012345678901234567890123456789012345678901234567890
53*86d7f5d3SJohn Marino * < ID> < time > <ty> <X XX XX XX XX XX XX XX XX XX XX XX XX XX |..............|
54*86d7f5d3SJohn Marino * ID Time Type Log Data
55*86d7f5d3SJohn Marino */
56*86d7f5d3SJohn Marino static void
mpt_print_event(MPI_LOG_0_ENTRY * entry,int verbose __unused)57*86d7f5d3SJohn Marino mpt_print_event(MPI_LOG_0_ENTRY *entry, int verbose __unused)
58*86d7f5d3SJohn Marino {
59*86d7f5d3SJohn Marino int i;
60*86d7f5d3SJohn Marino
61*86d7f5d3SJohn Marino printf("%5d %7ds %4x ", entry->LogSequence, entry->TimeStamp,
62*86d7f5d3SJohn Marino entry->LogEntryQualifier);
63*86d7f5d3SJohn Marino for (i = 0; i < 14; i++)
64*86d7f5d3SJohn Marino printf("%02x ", entry->LogData[i]);
65*86d7f5d3SJohn Marino printf("|");
66*86d7f5d3SJohn Marino for (i = 0; i < 14; i++)
67*86d7f5d3SJohn Marino printf("%c", isprint(entry->LogData[i]) ? entry->LogData[i] :
68*86d7f5d3SJohn Marino '.');
69*86d7f5d3SJohn Marino printf("|\n");
70*86d7f5d3SJohn Marino printf(" ");
71*86d7f5d3SJohn Marino for (i = 0; i < 14; i++)
72*86d7f5d3SJohn Marino printf("%02x ", entry->LogData[i + 14]);
73*86d7f5d3SJohn Marino printf("|");
74*86d7f5d3SJohn Marino for (i = 0; i < 14; i++)
75*86d7f5d3SJohn Marino printf("%c", isprint(entry->LogData[i + 14]) ?
76*86d7f5d3SJohn Marino entry->LogData[i + 14] : '.');
77*86d7f5d3SJohn Marino printf("|\n");
78*86d7f5d3SJohn Marino }
79*86d7f5d3SJohn Marino
80*86d7f5d3SJohn Marino static int
event_compare(const void * first,const void * second)81*86d7f5d3SJohn Marino event_compare(const void *first, const void *second)
82*86d7f5d3SJohn Marino {
83*86d7f5d3SJohn Marino MPI_LOG_0_ENTRY * const *one;
84*86d7f5d3SJohn Marino MPI_LOG_0_ENTRY * const *two;
85*86d7f5d3SJohn Marino
86*86d7f5d3SJohn Marino one = first;
87*86d7f5d3SJohn Marino two = second;
88*86d7f5d3SJohn Marino return ((*one)->LogSequence - ((*two)->LogSequence));
89*86d7f5d3SJohn Marino }
90*86d7f5d3SJohn Marino
91*86d7f5d3SJohn Marino static int
show_events(int ac,char ** av)92*86d7f5d3SJohn Marino show_events(int ac, char **av)
93*86d7f5d3SJohn Marino {
94*86d7f5d3SJohn Marino CONFIG_PAGE_LOG_0 *log;
95*86d7f5d3SJohn Marino MPI_LOG_0_ENTRY **entries;
96*86d7f5d3SJohn Marino int ch, error, fd, i, num_events, verbose;
97*86d7f5d3SJohn Marino
98*86d7f5d3SJohn Marino fd = mpt_open(mpt_unit);
99*86d7f5d3SJohn Marino if (fd < 0) {
100*86d7f5d3SJohn Marino error = errno;
101*86d7f5d3SJohn Marino warn("mpt_open");
102*86d7f5d3SJohn Marino return (error);
103*86d7f5d3SJohn Marino }
104*86d7f5d3SJohn Marino
105*86d7f5d3SJohn Marino log = mpt_get_events(fd, NULL);
106*86d7f5d3SJohn Marino if (log == NULL) {
107*86d7f5d3SJohn Marino error = errno;
108*86d7f5d3SJohn Marino warn("Failed to get event log info");
109*86d7f5d3SJohn Marino return (error);
110*86d7f5d3SJohn Marino }
111*86d7f5d3SJohn Marino
112*86d7f5d3SJohn Marino /* Default settings. */
113*86d7f5d3SJohn Marino verbose = 0;
114*86d7f5d3SJohn Marino
115*86d7f5d3SJohn Marino /* Parse any options. */
116*86d7f5d3SJohn Marino optind = 1;
117*86d7f5d3SJohn Marino while ((ch = getopt(ac, av, "v")) != -1) {
118*86d7f5d3SJohn Marino switch (ch) {
119*86d7f5d3SJohn Marino case 'v':
120*86d7f5d3SJohn Marino verbose = 1;
121*86d7f5d3SJohn Marino break;
122*86d7f5d3SJohn Marino case '?':
123*86d7f5d3SJohn Marino default:
124*86d7f5d3SJohn Marino return (EINVAL);
125*86d7f5d3SJohn Marino }
126*86d7f5d3SJohn Marino }
127*86d7f5d3SJohn Marino ac -= optind;
128*86d7f5d3SJohn Marino av += optind;
129*86d7f5d3SJohn Marino
130*86d7f5d3SJohn Marino /* Build a list of valid entries and sort them by sequence. */
131*86d7f5d3SJohn Marino entries = malloc(sizeof(MPI_LOG_0_ENTRY *) * log->NumLogEntries);
132*86d7f5d3SJohn Marino if (entries == NULL)
133*86d7f5d3SJohn Marino return (ENOMEM);
134*86d7f5d3SJohn Marino num_events = 0;
135*86d7f5d3SJohn Marino for (i = 0; i < log->NumLogEntries; i++) {
136*86d7f5d3SJohn Marino if (log->LogEntry[i].LogEntryQualifier ==
137*86d7f5d3SJohn Marino MPI_LOG_0_ENTRY_QUAL_ENTRY_UNUSED)
138*86d7f5d3SJohn Marino continue;
139*86d7f5d3SJohn Marino entries[num_events] = &log->LogEntry[i];
140*86d7f5d3SJohn Marino num_events++;
141*86d7f5d3SJohn Marino }
142*86d7f5d3SJohn Marino
143*86d7f5d3SJohn Marino qsort(entries, num_events, sizeof(MPI_LOG_0_ENTRY *), event_compare);
144*86d7f5d3SJohn Marino
145*86d7f5d3SJohn Marino if (num_events == 0)
146*86d7f5d3SJohn Marino printf("Event log is empty\n");
147*86d7f5d3SJohn Marino else {
148*86d7f5d3SJohn Marino printf(" ID Time Type Log Data\n");
149*86d7f5d3SJohn Marino for (i = 0; i < num_events; i++)
150*86d7f5d3SJohn Marino mpt_print_event(entries[i], verbose);
151*86d7f5d3SJohn Marino }
152*86d7f5d3SJohn Marino
153*86d7f5d3SJohn Marino free(entries);
154*86d7f5d3SJohn Marino close(fd);
155*86d7f5d3SJohn Marino
156*86d7f5d3SJohn Marino return (0);
157*86d7f5d3SJohn Marino }
158*86d7f5d3SJohn Marino MPT_COMMAND(show, events, show_events);
159