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