xref: /titanic_41/usr/src/cmd/mdb/common/modules/genunix/tsd.c (revision 6185db853e024a486ff8837e6784dd290d866112)
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 (c) 2000-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/thread.h>
30 #include "tsd.h"
31 
32 /*
33  * Initialize the tsd walker by either using the given starting address,
34  * or reading the value of the kernel's tsd_list pointer.
35  */
36 int
37 tsd_walk_init(mdb_walk_state_t *wsp)
38 {
39 	if (wsp->walk_addr == NULL &&
40 	    mdb_readvar(&wsp->walk_addr, "tsd_list") == -1) {
41 		mdb_warn("failed to read 'tsd_list'");
42 		return (WALK_ERR);
43 	}
44 
45 	wsp->walk_data = mdb_alloc(sizeof (struct tsd_thread), UM_SLEEP);
46 	return (WALK_NEXT);
47 }
48 
49 int
50 tsd_walk_step(mdb_walk_state_t *wsp)
51 {
52 	int status;
53 
54 	if (wsp->walk_addr == NULL)
55 		return (WALK_DONE);
56 
57 	if (mdb_vread(wsp->walk_data,
58 	    sizeof (struct tsd_thread), wsp->walk_addr) == -1) {
59 		mdb_warn("failed to read tsd at %p", wsp->walk_addr);
60 		return (WALK_ERR);
61 	}
62 
63 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
64 	    wsp->walk_cbdata);
65 
66 	wsp->walk_addr =
67 	    (uintptr_t)(((struct tsd_thread *)wsp->walk_data)->ts_next);
68 	return (status);
69 }
70 
71 void
72 tsd_walk_fini(mdb_walk_state_t *wsp)
73 {
74 	mdb_free(wsp->walk_data, sizeof (struct tsd_thread));
75 }
76 
77 /*
78  * Map from thread pointer to tsd pointer for given key
79  */
80 int
81 ttotsd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
82 {
83 	kthread_t thread, *t = &thread;
84 	struct tsd_thread tsdata, *ts = &tsdata;
85 	uintptr_t key = NULL;
86 	uintptr_t eladdr;
87 	void *element;
88 
89 	if (mdb_getopts(argc, argv, 'k', MDB_OPT_UINTPTR, &key, NULL) != argc)
90 		return (DCMD_USAGE);
91 
92 	if (!(flags & DCMD_ADDRSPEC) || key == NULL)
93 		return (DCMD_USAGE);
94 
95 	if (mdb_vread(t, sizeof (*t), addr) == -1) {
96 		mdb_warn("failed to read thread at %p", addr);
97 		return (DCMD_ERR);
98 	}
99 
100 	if (t->t_tsd == NULL) {
101 		if (flags & DCMD_PIPE)
102 			return (DCMD_OK);
103 		mdb_warn("no tsd on thread\n");
104 		return (DCMD_ERR);
105 	}
106 
107 	if (mdb_vread(ts, sizeof (*ts), (uintptr_t)t->t_tsd) == -1) {
108 		mdb_warn("failed to read tsd at %p", t->t_tsd);
109 		return (DCMD_ERR);
110 	}
111 
112 	if (key > ts->ts_nkeys) {
113 		mdb_warn("key out of range\n");
114 		return (DCMD_ERR);
115 	}
116 
117 	eladdr = (uintptr_t)(ts->ts_value + key - 1);
118 	if (mdb_vread(&element, sizeof (element), eladdr) == -1) {
119 		mdb_warn("failed to read t->t_tsd[%d] at %p", key - 1, eladdr);
120 		return (DCMD_ERR);
121 	}
122 
123 	mdb_printf("%p\n", element);
124 	return (DCMD_OK);
125 }
126 
127 static int
128 tsdthr_match(uintptr_t addr, const kthread_t *t, uintptr_t tsdaddr)
129 {
130 	/*
131 	 * Allow for multiple matches, even though that "can't happen."
132 	 */
133 	if (tsdaddr == (uintptr_t)t->t_tsd)
134 		mdb_printf("%p\n", addr);
135 	return (WALK_NEXT);
136 }
137 
138 /*
139  * Given a tsd pointer, find the owning thread
140  */
141 /*ARGSUSED*/
142 int
143 tsdtot(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
144 {
145 	if (addr == 0 || argc != 0)
146 		return (DCMD_USAGE);
147 	if (mdb_walk("thread", (mdb_walk_cb_t)tsdthr_match, (void *)addr) == -1)
148 		return (DCMD_ERR);
149 	return (DCMD_OK);
150 }
151