1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright 2023 RackTop Systems, Inc.
26  */
27 
28 #include <mdb/mdb_modapi.h>
29 #include <sys/types.h>
30 #include <sys/stream.h>
31 #include <sys/strlog.h>
32 
33 int
msgbuf(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)34 msgbuf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
35 {
36 	queue_t q;
37 	uintptr_t qp;
38 	mblk_t next;
39 	mblk_t cont;
40 	log_ctl_t lctl;
41 	char line[1024];
42 	uint_t verbose = FALSE;
43 	uint_t delta = FALSE;
44 	uint_t abstime = FALSE;
45 
46 	if (!(flags & DCMD_ADDRSPEC)) {
47 		if (mdb_readsym(&qp, sizeof (qp), "log_recentq") == -1) {
48 			mdb_warn("failed to read log_recent");
49 			return (DCMD_ERR);
50 		}
51 
52 		if (mdb_vread(&q, sizeof (q), qp) == -1) {
53 			mdb_warn("failed to read queue_t at %p", qp);
54 			return (DCMD_ERR);
55 		}
56 
57 		if (mdb_pwalk_dcmd("b_next", "msgbuf", argc, argv,
58 		    (uintptr_t)q.q_first) == -1) {
59 			mdb_warn("can't walk 'b_next'");
60 			return (DCMD_ERR);
61 		}
62 		return (DCMD_OK);
63 	}
64 
65 	if (mdb_getopts(argc, argv,
66 	    't', MDB_OPT_SETBITS, TRUE, &delta,
67 	    'T', MDB_OPT_SETBITS, TRUE, &abstime,
68 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
69 		return (DCMD_USAGE);
70 
71 	/* For backwards compatability, -v implies -T */
72 	if (verbose)
73 		abstime = TRUE;
74 
75 	if (DCMD_HDRSPEC(flags)) {
76 		int amt = 80;
77 
78 		mdb_printf("%<u>");
79 		if (abstime) {
80 			mdb_printf("%-20s ", "TIMESTAMP");
81 			amt -= 21;
82 		}
83 		if (delta) {
84 			mdb_printf("%-20s ", "DELTA");
85 			amt -= 21;
86 		}
87 		if (verbose) {
88 			mdb_printf("%?s ", "LOGCTL");
89 			amt -= 17;
90 		}
91 		mdb_printf("%-*s%</u>\n", amt, "MESSAGE");
92 	}
93 
94 	if (mdb_vread(&next, sizeof (next), addr) == -1) {
95 		mdb_warn("failed to read msgb structure at %p", addr);
96 		return (DCMD_ERR);
97 	}
98 
99 	if (mdb_vread(&lctl, sizeof (lctl), (uintptr_t)next.b_rptr) == -1) {
100 		mdb_warn("failed to read log_ctl_t at %p", next.b_rptr);
101 		return (DCMD_ERR);
102 	}
103 
104 	if (mdb_vread(&cont, sizeof (cont), (uintptr_t)next.b_cont) == -1) {
105 		mdb_warn("failed to read msgb structure at %p", next.b_cont);
106 		return (DCMD_ERR);
107 	}
108 
109 	if (mdb_readstr(line, sizeof (line), (uintptr_t)cont.b_rptr) == -1) {
110 		mdb_warn("failed to read string at %p", cont.b_rptr);
111 		return (DCMD_ERR);
112 	}
113 
114 	if (abstime)
115 		mdb_printf("%Y ", lctl.ttime);
116 
117 	if (delta) {
118 		timestruc_t	hr_time;
119 		int64_t		diff;
120 		char		buf[32] = { 0 };
121 
122 		if (mdb_readvar(&hr_time, "panic_hrestime") == -1) {
123 			mdb_warn("failed to read panic_hrestime");
124 			return (DCMD_ERR);
125 		}
126 
127 		if (hr_time.tv_sec == 0 &&
128 		    mdb_readvar(&hr_time, "hrestime") == -1) {
129 			mdb_warn("failed to read hrestime");
130 			return (DCMD_ERR);
131 		}
132 
133 		diff = (int64_t)lctl.ttime - hr_time.tv_sec;
134 		mdb_nicetime(SEC2NSEC(diff), buf, sizeof (buf));
135 		mdb_printf("%-20s ", buf);
136 	}
137 
138 	if (verbose)
139 		mdb_printf("%?p ", next.b_rptr);
140 
141 	/* skip leading CR to avoid extra lines */
142 	if (line[0] == 0x0d)
143 		mdb_printf("%s", &line[1]);
144 	else
145 		mdb_printf("%s", &line[0]);
146 
147 	return (DCMD_OK);
148 }
149 
150 void
msgbuf_help(void)151 msgbuf_help(void)
152 {
153 	mdb_printf("Print the most recent console messages.\n\n"
154 	    "%<b>OPTIONS%</b>\n"
155 	    "\t-t\tInclude the age of the message from now.\n"
156 	    "\t-T\tInclude the date/time of the message.\n"
157 	    "\t-v\tInclude the date/time of the message as well as the address "
158 	    "of its log_ctl_t.\n");
159 }
160