xref: /titanic_50/usr/src/cmd/mdb/common/modules/genunix/tsd.c (revision 28e4da25922bdfc5cba7ab29f47de911bbd78009)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
26*28e4da25SMatthew Ahrens /*
27*28e4da25SMatthew Ahrens  * Copyright (c) 2012 by Delphix. All rights reserved.
28*28e4da25SMatthew Ahrens  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/thread.h>
317c478bd9Sstevel@tonic-gate #include "tsd.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Initialize the tsd walker by either using the given starting address,
357c478bd9Sstevel@tonic-gate  * or reading the value of the kernel's tsd_list pointer.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate int
tsd_walk_init(mdb_walk_state_t * wsp)387c478bd9Sstevel@tonic-gate tsd_walk_init(mdb_walk_state_t *wsp)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL &&
417c478bd9Sstevel@tonic-gate 	    mdb_readvar(&wsp->walk_addr, "tsd_list") == -1) {
427c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'tsd_list'");
437c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
447c478bd9Sstevel@tonic-gate 	}
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	wsp->walk_data = mdb_alloc(sizeof (struct tsd_thread), UM_SLEEP);
477c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate int
tsd_walk_step(mdb_walk_state_t * wsp)517c478bd9Sstevel@tonic-gate tsd_walk_step(mdb_walk_state_t *wsp)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	int status;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
567c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if (mdb_vread(wsp->walk_data,
597c478bd9Sstevel@tonic-gate 	    sizeof (struct tsd_thread), wsp->walk_addr) == -1) {
607c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read tsd at %p", wsp->walk_addr);
617c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
657c478bd9Sstevel@tonic-gate 	    wsp->walk_cbdata);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	wsp->walk_addr =
687c478bd9Sstevel@tonic-gate 	    (uintptr_t)(((struct tsd_thread *)wsp->walk_data)->ts_next);
697c478bd9Sstevel@tonic-gate 	return (status);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate void
tsd_walk_fini(mdb_walk_state_t * wsp)737c478bd9Sstevel@tonic-gate tsd_walk_fini(mdb_walk_state_t *wsp)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (struct tsd_thread));
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Map from thread pointer to tsd pointer for given key
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate int
ttotsd(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)827c478bd9Sstevel@tonic-gate ttotsd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	kthread_t thread, *t = &thread;
857c478bd9Sstevel@tonic-gate 	struct tsd_thread tsdata, *ts = &tsdata;
867c478bd9Sstevel@tonic-gate 	uintptr_t key = NULL;
877c478bd9Sstevel@tonic-gate 	uintptr_t eladdr;
88*28e4da25SMatthew Ahrens 	void *element = NULL;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv, 'k', MDB_OPT_UINTPTR, &key, NULL) != argc)
917c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || key == NULL)
947c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (mdb_vread(t, sizeof (*t), addr) == -1) {
977c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read thread at %p", addr);
987c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
101*28e4da25SMatthew Ahrens 	if (t->t_tsd == NULL)
102*28e4da25SMatthew Ahrens 		goto out;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (mdb_vread(ts, sizeof (*ts), (uintptr_t)t->t_tsd) == -1) {
1057c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read tsd at %p", t->t_tsd);
1067c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
109*28e4da25SMatthew Ahrens 	if (key > ts->ts_nkeys)
110*28e4da25SMatthew Ahrens 		goto out;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	eladdr = (uintptr_t)(ts->ts_value + key - 1);
1137c478bd9Sstevel@tonic-gate 	if (mdb_vread(&element, sizeof (element), eladdr) == -1) {
1147c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read t->t_tsd[%d] at %p", key - 1, eladdr);
1157c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
118*28e4da25SMatthew Ahrens out:
119*28e4da25SMatthew Ahrens 	if (element == NULL && (flags & DCMD_PIPE))
120*28e4da25SMatthew Ahrens 		return (DCMD_OK);
121*28e4da25SMatthew Ahrens 
1227c478bd9Sstevel@tonic-gate 	mdb_printf("%p\n", element);
1237c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static int
tsdthr_match(uintptr_t addr,const kthread_t * t,uintptr_t tsdaddr)1277c478bd9Sstevel@tonic-gate tsdthr_match(uintptr_t addr, const kthread_t *t, uintptr_t tsdaddr)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Allow for multiple matches, even though that "can't happen."
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	if (tsdaddr == (uintptr_t)t->t_tsd)
1337c478bd9Sstevel@tonic-gate 		mdb_printf("%p\n", addr);
1347c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Given a tsd pointer, find the owning thread
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1417c478bd9Sstevel@tonic-gate int
tsdtot(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1427c478bd9Sstevel@tonic-gate tsdtot(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	if (addr == 0 || argc != 0)
1457c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
1467c478bd9Sstevel@tonic-gate 	if (mdb_walk("thread", (mdb_walk_cb_t)tsdthr_match, (void *)addr) == -1)
1477c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1487c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1497c478bd9Sstevel@tonic-gate }
150