xref: /onnv-gate/usr/src/cmd/mdb/common/modules/hook/hook.c (revision 3448:aaf16568054b)
12958Sdr146992 /*
22958Sdr146992  * CDDL HEADER START
32958Sdr146992  *
42958Sdr146992  * The contents of this file are subject to the terms of the
52958Sdr146992  * Common Development and Distribution License (the "License").
62958Sdr146992  * You may not use this file except in compliance with the License.
72958Sdr146992  *
82958Sdr146992  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92958Sdr146992  * or http://www.opensolaris.org/os/licensing.
102958Sdr146992  * See the License for the specific language governing permissions
112958Sdr146992  * and limitations under the License.
122958Sdr146992  *
132958Sdr146992  * When distributing Covered Code, include this CDDL HEADER in each
142958Sdr146992  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152958Sdr146992  * If applicable, add the following below this CDDL HEADER, with the
162958Sdr146992  * fields enclosed by brackets "[]" replaced with your own identifying
172958Sdr146992  * information: Portions Copyright [yyyy] [name of copyright owner]
182958Sdr146992  *
192958Sdr146992  * CDDL HEADER END
202958Sdr146992  */
212958Sdr146992 /*
22*3448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
232958Sdr146992  * Use is subject to license terms.
242958Sdr146992  */
252958Sdr146992 
262958Sdr146992 #pragma ident	"%Z%%M%	%I%	%E% SMI"
272958Sdr146992 
282958Sdr146992 #include <sys/types.h>
292958Sdr146992 #include <sys/rwlock.h>
302958Sdr146992 #include <mdb/mdb_modapi.h>
312958Sdr146992 #include <sys/queue.h>
32*3448Sdh155122 #include <inet/ip.h>
332958Sdr146992 #include <sys/hook.h>
342958Sdr146992 #include <sys/hook_impl.h>
352958Sdr146992 
362958Sdr146992 #define	MAX_LENGTH 64
372958Sdr146992 
382958Sdr146992 /*
392958Sdr146992  * List pfhooks hook list information.
402958Sdr146992  */
412958Sdr146992 /*ARGSUSED*/
422958Sdr146992 int
432958Sdr146992 hooklist(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
442958Sdr146992 {
452958Sdr146992 	hook_event_int_t hr;
462958Sdr146992 	hook_int_t hl, *hlp;
472958Sdr146992 	char hrstr[MAX_LENGTH];
482958Sdr146992 	GElf_Sym sym;
492958Sdr146992 	char buf[MDB_SYM_NAMLEN + 1];
502958Sdr146992 
512958Sdr146992 	if (argc)
522958Sdr146992 		return (DCMD_USAGE);
532958Sdr146992 
542958Sdr146992 	if (mdb_vread((void *)&hr, sizeof (hr), (uintptr_t)addr) == -1) {
552958Sdr146992 		mdb_warn("couldn't read hook register at %p", addr);
562958Sdr146992 		return (DCMD_ERR);
572958Sdr146992 	}
582958Sdr146992 
592958Sdr146992 	mdb_printf("%<u>%?s %10s %20s %?s%</u>\n",
602958Sdr146992 	    "ADDR", "FLAG", "FUNC", "NAME");
612958Sdr146992 	hlp = TAILQ_FIRST(&hr.hei_head);
622958Sdr146992 	while (hlp) {
632958Sdr146992 		if (mdb_vread((void *)&hl, sizeof (hl),
642958Sdr146992 		    (uintptr_t)hlp) == -1) {
652958Sdr146992 			mdb_warn("couldn't read hook list at %p",
662958Sdr146992 			    hlp);
672958Sdr146992 			return (DCMD_ERR);
682958Sdr146992 		}
692958Sdr146992 		if (!hl.hi_hook.h_name) {
702958Sdr146992 			mdb_warn("hook list at %p has null role",
712958Sdr146992 			    hl.hi_hook);
722958Sdr146992 			return (DCMD_ERR);
732958Sdr146992 		}
742958Sdr146992 		if (mdb_readstr((char *)hrstr, sizeof (hrstr),
752958Sdr146992 		    (uintptr_t)hl.hi_hook.h_name) == -1) {
762958Sdr146992 			mdb_warn("couldn't read list role at %p",
772958Sdr146992 			    hl.hi_hook.h_name);
782958Sdr146992 			return (DCMD_ERR);
792958Sdr146992 		}
802958Sdr146992 		if (mdb_lookup_by_addr((uintptr_t)hl.hi_hook.h_func,
812958Sdr146992 		    MDB_SYM_EXACT, buf, sizeof (buf), &sym) == -1)
822958Sdr146992 			mdb_printf("%0?p %10x %0?p %10s\n",
832958Sdr146992 			    hlp, hl.hi_hook.h_flags, hl.hi_hook.h_func, hrstr);
842958Sdr146992 		else
852958Sdr146992 			mdb_printf("%0?p %10x %20s %10s\n",
862958Sdr146992 			    hlp, hl.hi_hook.h_flags, buf, hrstr);
872958Sdr146992 		hlp = TAILQ_NEXT(&hl, hi_entry);
882958Sdr146992 	}
892958Sdr146992 	return (DCMD_OK);
902958Sdr146992 }
912958Sdr146992 
922958Sdr146992 
932958Sdr146992 /*
942958Sdr146992  * List pfhooks event information.
952958Sdr146992  * List the hooks information in verbose mode as well.
962958Sdr146992  */
972958Sdr146992 /*ARGSUSED*/
982958Sdr146992 int
992958Sdr146992 hookeventlist(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1002958Sdr146992 {
1012958Sdr146992 	hook_family_int_t hf;
1022958Sdr146992 	hook_event_int_t hr, *hrp;
1032958Sdr146992 	hook_event_t hp;
1042958Sdr146992 	char hprstr[MAX_LENGTH];
1052958Sdr146992 
1062958Sdr146992 	if (argc)
1072958Sdr146992 		return (DCMD_USAGE);
1082958Sdr146992 
1092958Sdr146992 	if (mdb_vread((void *)&hf, sizeof (hf), (uintptr_t)addr) == -1) {
1102958Sdr146992 		mdb_warn("couldn't read hook family at %p", addr);
1112958Sdr146992 		return (DCMD_ERR);
1122958Sdr146992 	}
1132958Sdr146992 
1142958Sdr146992 	mdb_printf("%<u>%?s %10s %20s%</u>\n", "ADDR", "FLAG", "NAME");
1152958Sdr146992 	hrp = SLIST_FIRST(&hf.hfi_head);
1162958Sdr146992 	while (hrp) {
1172958Sdr146992 		if (mdb_vread((void *)&hr, sizeof (hr), (uintptr_t)hrp) == -1) {
1182958Sdr146992 			mdb_warn("couldn't read hook register at %p", hrp);
1192958Sdr146992 			return (DCMD_ERR);
1202958Sdr146992 		}
1212958Sdr146992 		if (!hr.hei_event) {
1222958Sdr146992 			mdb_warn("hook register at %p has no hook provider",
1232958Sdr146992 			    hrp);
1242958Sdr146992 			return (DCMD_ERR);
1252958Sdr146992 		}
1262958Sdr146992 		if (mdb_vread((void *)&hp, sizeof (hp),
1272958Sdr146992 		    (uintptr_t)hr.hei_event) == -1) {
1282958Sdr146992 			mdb_warn("hook provider at %p has null role",
1292958Sdr146992 			    hr.hei_event);
1302958Sdr146992 			return (DCMD_ERR);
1312958Sdr146992 		}
1322958Sdr146992 		if (!hp.he_name) {
1332958Sdr146992 			mdb_warn("hook provider at %p has null role",
1342958Sdr146992 			    hr.hei_event);
1352958Sdr146992 			return (DCMD_ERR);
1362958Sdr146992 		}
1372958Sdr146992 		if (mdb_readstr((char *)hprstr, sizeof (hprstr),
1382958Sdr146992 		    (uintptr_t)hp.he_name) == -1) {
1392958Sdr146992 			mdb_warn("couldn't read provider role at %p",
1402958Sdr146992 			    hp.he_name);
1412958Sdr146992 			return (DCMD_ERR);
1422958Sdr146992 		}
1432958Sdr146992 		mdb_printf("%0?p %10x %20s\n", hrp, hp.he_flags, hprstr);
1442958Sdr146992 		hrp = SLIST_NEXT(&hr, hei_entry);
1452958Sdr146992 	}
1462958Sdr146992 
1472958Sdr146992 	return (DCMD_OK);
1482958Sdr146992 }
1492958Sdr146992 
1502958Sdr146992 /*
1512958Sdr146992  * List pfhooks family information.
1522958Sdr146992  */
1532958Sdr146992 /*ARGSUSED*/
1542958Sdr146992 int
1552958Sdr146992 hookrootlist(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1562958Sdr146992 {
157*3448Sdh155122 	struct hook_stack *hks;
1582958Sdr146992 	hook_family_int_head_t hfh;
1592958Sdr146992 	hook_family_int_t hf, *hfp;
1602958Sdr146992 	char hrrstr[MAX_LENGTH];
1612958Sdr146992 
1622958Sdr146992 	if (argc)
1632958Sdr146992 		return (DCMD_USAGE);
1642958Sdr146992 
165*3448Sdh155122 	if (mdb_vread((void *)&hks, sizeof (hks),
166*3448Sdh155122 	    (uintptr_t)(addr + OFFSETOF(netstack_t, netstack_hook))) == -1) {
167*3448Sdh155122 		mdb_warn("couldn't read netstack_hook");
168*3448Sdh155122 		return (DCMD_ERR);
169*3448Sdh155122 	}
170*3448Sdh155122 
171*3448Sdh155122 	if (mdb_vread((void *)&hfh, sizeof (hfh), (uintptr_t)((uintptr_t)hks +
172*3448Sdh155122 	    OFFSETOF(hook_stack_t, hks_familylist))) == -1) {
173*3448Sdh155122 		mdb_warn("couldn't read hook family head");
1742958Sdr146992 		return (DCMD_ERR);
1752958Sdr146992 	}
1762958Sdr146992 
1772958Sdr146992 	mdb_printf("%<u>%?s %10s%</u>\n", "ADDR", "FAMILY");
1782958Sdr146992 	hfp = SLIST_FIRST(&hfh);
1792958Sdr146992 	while (hfp) {
1802958Sdr146992 		if (mdb_vread((void *)&hf, sizeof (hf), (uintptr_t)hfp) == -1) {
1812958Sdr146992 			mdb_warn("couldn't read hook family at %p", hfp);
1822958Sdr146992 			return (DCMD_ERR);
1832958Sdr146992 		}
1842958Sdr146992 		if (!hf.hfi_family.hf_name) {
1852958Sdr146992 			mdb_warn("hook root at %p has null role",
1862958Sdr146992 			    hf.hfi_family);
1872958Sdr146992 			return (DCMD_ERR);
1882958Sdr146992 		}
1892958Sdr146992 		if (mdb_readstr((char *)hrrstr, sizeof (hrrstr),
1902958Sdr146992 		    (uintptr_t)hf.hfi_family.hf_name) == -1) {
1912958Sdr146992 			mdb_warn("couldn't read root role at %p",
1922958Sdr146992 			    hf.hfi_family.hf_name);
1932958Sdr146992 			return (DCMD_ERR);
1942958Sdr146992 		}
1952958Sdr146992 		mdb_printf("%0?p %10s\n", hfp, hrrstr);
1962958Sdr146992 		hfp = SLIST_NEXT(&hf, hfi_entry);
1972958Sdr146992 	}
1982958Sdr146992 
1992958Sdr146992 	return (DCMD_OK);
2002958Sdr146992 }
2012958Sdr146992 
2022958Sdr146992 
2032958Sdr146992 static int
204*3448Sdh155122 hookevent_stack_walk_init(mdb_walk_state_t *wsp)
2052958Sdr146992 {
2062958Sdr146992 	hook_family_int_t hf;
2072958Sdr146992 
2082958Sdr146992 	if (wsp->walk_addr == NULL) {
2092958Sdr146992 		mdb_warn("global walk not supported\n");
2102958Sdr146992 		return (WALK_ERR);
2112958Sdr146992 	}
2122958Sdr146992 
2132958Sdr146992 	if (mdb_vread((void *)&hf, sizeof (hf),
2142958Sdr146992 	    (uintptr_t)wsp->walk_addr) == -1) {
2152958Sdr146992 		mdb_warn("couldn't read hook family at %p", wsp->walk_addr);
2162958Sdr146992 		return (DCMD_ERR);
2172958Sdr146992 	}
2182958Sdr146992 	wsp->walk_addr = (uintptr_t)SLIST_FIRST(&hf.hfi_head);
2192958Sdr146992 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
2202958Sdr146992 		    wsp->walk_cbdata));
2212958Sdr146992 }
2222958Sdr146992 
2232958Sdr146992 static int
224*3448Sdh155122 hookevent_stack_walk_step(mdb_walk_state_t *wsp)
2252958Sdr146992 {
2262958Sdr146992 	hook_event_int_t hr;
2272958Sdr146992 
2282958Sdr146992 	if (mdb_vread((void *)&hr, sizeof (hr),
2292958Sdr146992 	    (uintptr_t)wsp->walk_addr) == -1) {
2302958Sdr146992 		mdb_warn("couldn't read hook event at %p", wsp->walk_addr);
2312958Sdr146992 		return (DCMD_ERR);
2322958Sdr146992 	}
2332958Sdr146992 	wsp->walk_addr = (uintptr_t)SLIST_NEXT(&hr, hei_entry);
2342958Sdr146992 	if (wsp->walk_addr == NULL)
2352958Sdr146992 		return (WALK_DONE);
2362958Sdr146992 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
2372958Sdr146992 		    wsp->walk_cbdata));
2382958Sdr146992 }
2392958Sdr146992 
2402958Sdr146992 static const mdb_dcmd_t dcmds[] = {
2412958Sdr146992 	{ "hookrootlist", "", "display hook family information", hookrootlist },
2422958Sdr146992 	{ "hookeventlist", "", "display hook event information",
2432958Sdr146992 		hookeventlist, NULL },
2442958Sdr146992 	{ "hooklist", "", "display hooks", hooklist },
2452958Sdr146992 	{ NULL }
2462958Sdr146992 };
2472958Sdr146992 
2482958Sdr146992 static const mdb_walker_t walkers[] = {
249*3448Sdh155122 	{ "hookevent_stack", "walk list of hooks",
250*3448Sdh155122 		hookevent_stack_walk_init, hookevent_stack_walk_step, NULL },
2512958Sdr146992 	{ NULL }
2522958Sdr146992 };
2532958Sdr146992 
2542958Sdr146992 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
2552958Sdr146992 
2562958Sdr146992 const mdb_modinfo_t *
2572958Sdr146992 _mdb_init(void)
2582958Sdr146992 {
2592958Sdr146992 	return (&modinfo);
2602958Sdr146992 }
261