xref: /illumos-gate/usr/src/cmd/truss/fcall.c (revision 186f7fbf)
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
56fced65dSraf  * Common Development and Distribution License (the "License").
66fced65dSraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
216fced65dSraf 
227c478bd9Sstevel@tonic-gate /*
230df991f9SRoger A. Faulkner  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #define	_SYSCALL32
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <memory.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stack.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
417c478bd9Sstevel@tonic-gate #include <proc_service.h>
427c478bd9Sstevel@tonic-gate #include <dlfcn.h>
437c478bd9Sstevel@tonic-gate #include <fnmatch.h>
447c478bd9Sstevel@tonic-gate #include <libproc.h>
457c478bd9Sstevel@tonic-gate #include "ramdata.h"
467c478bd9Sstevel@tonic-gate #include "systable.h"
477c478bd9Sstevel@tonic-gate #include "print.h"
487c478bd9Sstevel@tonic-gate #include "proto.h"
497c478bd9Sstevel@tonic-gate #include "htbl.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Functions supporting library function call tracing.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate typedef struct {
567c478bd9Sstevel@tonic-gate 	prmap_t	*pmap;
577c478bd9Sstevel@tonic-gate 	int	nmap;
587c478bd9Sstevel@tonic-gate } ph_map_t;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * static functions in this file.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate void function_entry(private_t *, struct bkpt *, struct callstack *);
647c478bd9Sstevel@tonic-gate void function_return(private_t *, struct callstack *);
657c478bd9Sstevel@tonic-gate int object_iter(void *, const prmap_t *, const char *);
666fced65dSraf int object_present(void *, const prmap_t *, const char *);
677c478bd9Sstevel@tonic-gate int symbol_iter(void *, const GElf_Sym *, const char *);
687c478bd9Sstevel@tonic-gate uintptr_t get_return_address(uintptr_t *);
697c478bd9Sstevel@tonic-gate int get_arguments(long *argp);
707c478bd9Sstevel@tonic-gate uintptr_t previous_fp(uintptr_t, uintptr_t *);
717c478bd9Sstevel@tonic-gate int lwp_stack_traps(void *cd, const lwpstatus_t *Lsp);
727c478bd9Sstevel@tonic-gate int thr_stack_traps(const td_thrhandle_t *Thp, void *cd);
737c478bd9Sstevel@tonic-gate struct bkpt *create_bkpt(uintptr_t, int, int);
747c478bd9Sstevel@tonic-gate void set_deferred_breakpoints(void);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	DEF_MAXCALL	16	/* initial value of Stk->maxcall */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	FAULT_ADDR	((uintptr_t)(0-8))
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	HASHSZ	2048
817c478bd9Sstevel@tonic-gate #define	bpt_hash(addr)	((((addr) >> 13) ^ ((addr) >> 2)) & 0x7ff)
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static void
setup_thread_agent(void)847c478bd9Sstevel@tonic-gate setup_thread_agent(void)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
877c478bd9Sstevel@tonic-gate 	td_notify_t notify;
887c478bd9Sstevel@tonic-gate 	td_thr_events_t events;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (Thr_agent != NULL)	/* only once */
917c478bd9Sstevel@tonic-gate 		return;
927c478bd9Sstevel@tonic-gate 	if (td_init() != TD_OK || td_ta_new(Proc, &Thr_agent) != TD_OK)
937c478bd9Sstevel@tonic-gate 		Thr_agent = NULL;
947c478bd9Sstevel@tonic-gate 	else {
957c478bd9Sstevel@tonic-gate 		td_event_emptyset(&events);
967c478bd9Sstevel@tonic-gate 		td_event_addset(&events, TD_CREATE);
977c478bd9Sstevel@tonic-gate 		if (td_ta_event_addr(Thr_agent, TD_CREATE, &notify) == TD_OK &&
987c478bd9Sstevel@tonic-gate 		    notify.type == NOTIFY_BPT &&
997c478bd9Sstevel@tonic-gate 		    td_ta_set_event(Thr_agent, &events) == TD_OK &&
1007c478bd9Sstevel@tonic-gate 		    (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
1017c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_TD_CREATE;
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1056fced65dSraf /*
1066fced65dSraf  * Delete all breakpoints in the range [base .. base+size)
1076fced65dSraf  * from the breakpoint hash table.
1086fced65dSraf  */
1096fced65dSraf static void
delete_breakpoints(uintptr_t base,size_t size)1106fced65dSraf delete_breakpoints(uintptr_t base, size_t size)
1116fced65dSraf {
1126fced65dSraf 	struct bkpt **Bpp;
1136fced65dSraf 	struct bkpt *Bp;
1146fced65dSraf 	int i;
1156fced65dSraf 
1166fced65dSraf 	if (bpt_hashtable == NULL)
1176fced65dSraf 		return;
1186fced65dSraf 	for (i = 0; i < HASHSZ; i++) {
1196fced65dSraf 		Bpp = &bpt_hashtable[i];
1206fced65dSraf 		while ((Bp = *Bpp) != NULL) {
1216fced65dSraf 			if (Bp->addr < base || Bp->addr >= base + size) {
1226fced65dSraf 				Bpp = &Bp->next;
1236fced65dSraf 				continue;
1246fced65dSraf 			}
1256fced65dSraf 			*Bpp = Bp->next;
1266fced65dSraf 			if (Bp->sym_name)
1276fced65dSraf 				free(Bp->sym_name);
1286fced65dSraf 			free(Bp);
1296fced65dSraf 		}
1306fced65dSraf 	}
1316fced65dSraf }
1326fced65dSraf 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * Establishment of breakpoints on traced library functions.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate void
establish_breakpoints(void)1377c478bd9Sstevel@tonic-gate establish_breakpoints(void)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL)
1407c478bd9Sstevel@tonic-gate 		return;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/* allocate the breakpoint hash table */
1437c478bd9Sstevel@tonic-gate 	if (bpt_hashtable == NULL) {
1447c478bd9Sstevel@tonic-gate 		bpt_hashtable = my_malloc(HASHSZ * sizeof (struct bkpt *),
1450df991f9SRoger A. Faulkner 		    NULL);
1467c478bd9Sstevel@tonic-gate 		(void) memset(bpt_hashtable, 0,
1470df991f9SRoger A. Faulkner 		    HASHSZ * sizeof (struct bkpt *));
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/*
1517c478bd9Sstevel@tonic-gate 	 * Set special rtld_db event breakpoints, first time only.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	if (Rdb_agent == NULL &&
1547c478bd9Sstevel@tonic-gate 	    (Rdb_agent = Prd_agent(Proc)) != NULL) {
1557c478bd9Sstevel@tonic-gate 		rd_notify_t notify;
1567c478bd9Sstevel@tonic-gate 		struct bkpt *Bp;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		(void) rd_event_enable(Rdb_agent, 1);
1597c478bd9Sstevel@tonic-gate 		if (rd_event_addr(Rdb_agent, RD_PREINIT, &notify) == RD_OK &&
1607c478bd9Sstevel@tonic-gate 		    (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
1617c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_PREINIT;
1627c478bd9Sstevel@tonic-gate 		if (rd_event_addr(Rdb_agent, RD_POSTINIT, &notify) == RD_OK &&
1637c478bd9Sstevel@tonic-gate 		    (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
1647c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_POSTINIT;
1657c478bd9Sstevel@tonic-gate 		if (rd_event_addr(Rdb_agent, RD_DLACTIVITY, &notify) == RD_OK &&
1667c478bd9Sstevel@tonic-gate 		    (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
1677c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_DLACTIVITY;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Set special thread event breakpoint, first time libc is seen.
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL)
1747c478bd9Sstevel@tonic-gate 		setup_thread_agent();
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Tell libproc to update its mappings.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	Pupdate_maps(Proc);
1807c478bd9Sstevel@tonic-gate 
1816fced65dSraf 	/*
1826fced65dSraf 	 * If rtld_db told us a library was being deleted,
1836fced65dSraf 	 * first mark all of the dynlibs as not present, then
1846fced65dSraf 	 * iterate over the shared objects, marking only those
1856fced65dSraf 	 * present that really are present, and finally delete
1866fced65dSraf 	 * all of the not-present dynlibs.
1876fced65dSraf 	 */
1886fced65dSraf 	if (delete_library) {
1896fced65dSraf 		struct dynlib **Dpp;
1906fced65dSraf 		struct dynlib *Dp;
1916fced65dSraf 
192*186f7fbfSEdward Pilatowicz 		for (Dp = Dynlib; Dp != NULL; Dp = Dp->next)
1936fced65dSraf 			Dp->present = FALSE;
1946fced65dSraf 		(void) Pobject_iter(Proc, object_present, NULL);
195*186f7fbfSEdward Pilatowicz 		Dpp = &Dynlib;
1966fced65dSraf 		while ((Dp = *Dpp) != NULL) {
1976fced65dSraf 			if (Dp->present) {
1986fced65dSraf 				Dpp = &Dp->next;
1996fced65dSraf 				continue;
2006fced65dSraf 			}
2016fced65dSraf 			delete_breakpoints(Dp->base, Dp->size);
2026fced65dSraf 			*Dpp = Dp->next;
2036fced65dSraf 			free(Dp->lib_name);
2046fced65dSraf 			free(Dp->match_name);
2056fced65dSraf 			free(Dp->prt_name);
2066fced65dSraf 			free(Dp);
2076fced65dSraf 		}
2086fced65dSraf 		delete_library = FALSE;
2096fced65dSraf 	}
2106fced65dSraf 
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 	 * Iterate over the shared objects, creating breakpoints.
2137c478bd9Sstevel@tonic-gate 	 */
2147c478bd9Sstevel@tonic-gate 	(void) Pobject_iter(Proc, object_iter, NULL);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/*
2177c478bd9Sstevel@tonic-gate 	 * Now actually set all the breakpoints we just created.
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	set_deferred_breakpoints();
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Initial establishment of stacks in a newly-grabbed process.
2247c478bd9Sstevel@tonic-gate  * establish_breakpoints() has already been called.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate void
establish_stacks(void)2277c478bd9Sstevel@tonic-gate establish_stacks(void)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
2307c478bd9Sstevel@tonic-gate 	char mapfile[64];
2317c478bd9Sstevel@tonic-gate 	int mapfd;
2327c478bd9Sstevel@tonic-gate 	struct stat statb;
2337c478bd9Sstevel@tonic-gate 	prmap_t *Pmap = NULL;
2347c478bd9Sstevel@tonic-gate 	int nmap = 0;
2357c478bd9Sstevel@tonic-gate 	ph_map_t ph_map;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	(void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid);
2387c478bd9Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
2397c478bd9Sstevel@tonic-gate 	    fstat(mapfd, &statb) != 0 ||
2407c478bd9Sstevel@tonic-gate 	    statb.st_size < sizeof (prmap_t) ||
2417c478bd9Sstevel@tonic-gate 	    (Pmap = my_malloc(statb.st_size, NULL)) == NULL ||
2427c478bd9Sstevel@tonic-gate 	    (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
2437c478bd9Sstevel@tonic-gate 	    (nmap /= sizeof (prmap_t)) == 0) {
2447c478bd9Sstevel@tonic-gate 		if (Pmap != NULL)
2457c478bd9Sstevel@tonic-gate 			free(Pmap);
2467c478bd9Sstevel@tonic-gate 		Pmap = NULL;
2477c478bd9Sstevel@tonic-gate 		nmap = 0;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	if (mapfd >= 0)
2507c478bd9Sstevel@tonic-gate 		(void) close(mapfd);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Iterate over lwps, establishing stacks.
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	ph_map.pmap = Pmap;
2567c478bd9Sstevel@tonic-gate 	ph_map.nmap = nmap;
2577c478bd9Sstevel@tonic-gate 	(void) Plwp_iter(Proc, lwp_stack_traps, &ph_map);
2587c478bd9Sstevel@tonic-gate 	if (Pmap != NULL)
2597c478bd9Sstevel@tonic-gate 		free(Pmap);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL)
2627c478bd9Sstevel@tonic-gate 		return;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * Iterate over unbound threads, establishing stacks.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	(void) td_ta_thr_iter(Thr_agent, thr_stack_traps, NULL,
2680df991f9SRoger A. Faulkner 	    TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
2690df991f9SRoger A. Faulkner 	    TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate void
do_symbol_iter(const char * object_name,struct dynpat * Dyp)2737c478bd9Sstevel@tonic-gate do_symbol_iter(const char *object_name, struct dynpat *Dyp)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	if (*Dyp->Dp->prt_name == '\0')
2767c478bd9Sstevel@tonic-gate 		object_name = PR_OBJ_EXEC;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * Always search the dynamic symbol table.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	(void) Psymbol_iter(Proc, object_name,
2820df991f9SRoger A. Faulkner 	    PR_DYNSYM, BIND_WEAK|BIND_GLOBAL|TYPE_FUNC,
2830df991f9SRoger A. Faulkner 	    symbol_iter, Dyp);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * Search the static symbol table if this is the
2877c478bd9Sstevel@tonic-gate 	 * executable file or if we are being asked to
2887c478bd9Sstevel@tonic-gate 	 * report internal calls within the library.
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	if (object_name == PR_OBJ_EXEC || Dyp->internal)
2917c478bd9Sstevel@tonic-gate 		(void) Psymbol_iter(Proc, object_name,
2920df991f9SRoger A. Faulkner 		    PR_SYMTAB, BIND_ANY|TYPE_FUNC,
2930df991f9SRoger A. Faulkner 		    symbol_iter, Dyp);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /* ARGSUSED */
2977c478bd9Sstevel@tonic-gate int
object_iter(void * cd,const prmap_t * pmp,const char * object_name)2987c478bd9Sstevel@tonic-gate object_iter(void *cd, const prmap_t *pmp, const char *object_name)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	char name[100];
3017c478bd9Sstevel@tonic-gate 	struct dynpat *Dyp;
3027c478bd9Sstevel@tonic-gate 	struct dynlib *Dp;
3037c478bd9Sstevel@tonic-gate 	const char *str;
3047c478bd9Sstevel@tonic-gate 	char *s;
3057c478bd9Sstevel@tonic-gate 	int i;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if ((pmp->pr_mflags & MA_WRITE) || !(pmp->pr_mflags & MA_EXEC))
3087c478bd9Sstevel@tonic-gate 		return (0);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * Set special thread event breakpoint, first time libc is seen.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL && strstr(object_name, "/libc.so.") != NULL)
3147c478bd9Sstevel@tonic-gate 		setup_thread_agent();
3157c478bd9Sstevel@tonic-gate 
316*186f7fbfSEdward Pilatowicz 	for (Dp = Dynlib; Dp != NULL; Dp = Dp->next)
3177c478bd9Sstevel@tonic-gate 		if (strcmp(object_name, Dp->lib_name) == 0 ||
3187c478bd9Sstevel@tonic-gate 		    (strcmp(Dp->lib_name, "a.out") == 0 &&
3197c478bd9Sstevel@tonic-gate 		    strcmp(pmp->pr_mapname, "a.out") == 0))
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (Dp == NULL) {
3237c478bd9Sstevel@tonic-gate 		Dp = my_malloc(sizeof (struct dynlib), NULL);
3247c478bd9Sstevel@tonic-gate 		(void) memset(Dp, 0, sizeof (struct dynlib));
3257c478bd9Sstevel@tonic-gate 		if (strcmp(pmp->pr_mapname, "a.out") == 0) {
3267c478bd9Sstevel@tonic-gate 			Dp->lib_name = strdup(pmp->pr_mapname);
3277c478bd9Sstevel@tonic-gate 			Dp->match_name = strdup(pmp->pr_mapname);
3287c478bd9Sstevel@tonic-gate 			Dp->prt_name = strdup("");
3297c478bd9Sstevel@tonic-gate 		} else {
3307c478bd9Sstevel@tonic-gate 			Dp->lib_name = strdup(object_name);
3317c478bd9Sstevel@tonic-gate 			if ((str = strrchr(object_name, '/')) != NULL)
3327c478bd9Sstevel@tonic-gate 				str++;
3337c478bd9Sstevel@tonic-gate 			else
3347c478bd9Sstevel@tonic-gate 				str = object_name;
3357c478bd9Sstevel@tonic-gate 			(void) strncpy(name, str, sizeof (name) - 2);
3367c478bd9Sstevel@tonic-gate 			name[sizeof (name) - 2] = '\0';
3377c478bd9Sstevel@tonic-gate 			if ((s = strstr(name, ".so")) != NULL)
3387c478bd9Sstevel@tonic-gate 				*s = '\0';
3397c478bd9Sstevel@tonic-gate 			Dp->match_name = strdup(name);
3407c478bd9Sstevel@tonic-gate 			(void) strcat(name, ":");
3417c478bd9Sstevel@tonic-gate 			Dp->prt_name = strdup(name);
3427c478bd9Sstevel@tonic-gate 		}
343*186f7fbfSEdward Pilatowicz 		Dp->next = Dynlib;
344*186f7fbfSEdward Pilatowicz 		Dynlib = Dp;
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (Dp->built ||
3487c478bd9Sstevel@tonic-gate 	    (not_consist && strcmp(Dp->prt_name, "ld:") != 0))	/* kludge */
3497c478bd9Sstevel@tonic-gate 		return (0);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (hflag && not_consist)
3527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "not_consist is TRUE, building %s\n",
3530df991f9SRoger A. Faulkner 		    Dp->lib_name);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	Dp->base = pmp->pr_vaddr;
3567c478bd9Sstevel@tonic-gate 	Dp->size = pmp->pr_size;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * For every dynlib pattern that matches this library's name,
3607c478bd9Sstevel@tonic-gate 	 * iterate through all of the library's symbols looking for
3617c478bd9Sstevel@tonic-gate 	 * matching symbol name patterns.
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	for (Dyp = Dynpat; Dyp != NULL; Dyp = Dyp->next) {
3647c478bd9Sstevel@tonic-gate 		if (interrupt|sigusr1)
3657c478bd9Sstevel@tonic-gate 			break;
3667c478bd9Sstevel@tonic-gate 		for (i = 0; i < Dyp->nlibpat; i++) {
3677c478bd9Sstevel@tonic-gate 			if (interrupt|sigusr1)
3687c478bd9Sstevel@tonic-gate 				break;
3697c478bd9Sstevel@tonic-gate 			if (fnmatch(Dyp->libpat[i], Dp->match_name, 0) != 0)
3707c478bd9Sstevel@tonic-gate 				continue;	/* no match */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 			/*
3737c478bd9Sstevel@tonic-gate 			 * Require an exact match for the executable (a.out)
3747c478bd9Sstevel@tonic-gate 			 * and for the dynamic linker (ld.so.1).
3757c478bd9Sstevel@tonic-gate 			 */
3767c478bd9Sstevel@tonic-gate 			if ((strcmp(Dp->match_name, "a.out") == 0 ||
3777c478bd9Sstevel@tonic-gate 			    strcmp(Dp->match_name, "ld") == 0) &&
3787c478bd9Sstevel@tonic-gate 			    strcmp(Dyp->libpat[i], Dp->match_name) != 0)
3797c478bd9Sstevel@tonic-gate 				continue;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 			/*
3827c478bd9Sstevel@tonic-gate 			 * Set Dyp->Dp to Dp so symbol_iter() can use it.
3837c478bd9Sstevel@tonic-gate 			 */
3847c478bd9Sstevel@tonic-gate 			Dyp->Dp = Dp;
3857c478bd9Sstevel@tonic-gate 			do_symbol_iter(object_name, Dyp);
3867c478bd9Sstevel@tonic-gate 			Dyp->Dp = NULL;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	Dp->built = TRUE;
3917c478bd9Sstevel@tonic-gate 	return (interrupt | sigusr1);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3946fced65dSraf /* ARGSUSED */
3956fced65dSraf int
object_present(void * cd,const prmap_t * pmp,const char * object_name)3966fced65dSraf object_present(void *cd, const prmap_t *pmp, const char *object_name)
3976fced65dSraf {
3986fced65dSraf 	struct dynlib *Dp;
3996fced65dSraf 
400*186f7fbfSEdward Pilatowicz 	for (Dp = Dynlib; Dp != NULL; Dp = Dp->next) {
4016fced65dSraf 		if (Dp->base == pmp->pr_vaddr)
4026fced65dSraf 			Dp->present = TRUE;
4036fced65dSraf 	}
4046fced65dSraf 
4056fced65dSraf 	return (0);
4066fced65dSraf }
4076fced65dSraf 
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate  * Search for an existing breakpoint at the 'pc' location.
4107c478bd9Sstevel@tonic-gate  */
4117c478bd9Sstevel@tonic-gate struct bkpt *
get_bkpt(uintptr_t pc)4127c478bd9Sstevel@tonic-gate get_bkpt(uintptr_t pc)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	for (Bp = bpt_hashtable[bpt_hash(pc)]; Bp != NULL; Bp = Bp->next)
4177c478bd9Sstevel@tonic-gate 		if (pc == Bp->addr)
4187c478bd9Sstevel@tonic-gate 			break;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	return (Bp);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * Create a breakpoint at 'pc', if one is not there already.
4257c478bd9Sstevel@tonic-gate  * 'ret' is true when creating a function return breakpoint, in which case
4267c478bd9Sstevel@tonic-gate  * fail and return NULL if the breakpoint would be created in writeable data.
4277c478bd9Sstevel@tonic-gate  * If 'set' it true, set the breakpoint in the process now.
4287c478bd9Sstevel@tonic-gate  */
4297c478bd9Sstevel@tonic-gate struct bkpt *
create_bkpt(uintptr_t pc,int ret,int set)4307c478bd9Sstevel@tonic-gate create_bkpt(uintptr_t pc, int ret, int set)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	uint_t hix = bpt_hash(pc);
4337c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
4347c478bd9Sstevel@tonic-gate 	const prmap_t *pmp;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	for (Bp = bpt_hashtable[hix]; Bp != NULL; Bp = Bp->next)
4377c478bd9Sstevel@tonic-gate 		if (pc == Bp->addr)
4387c478bd9Sstevel@tonic-gate 			return (Bp);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/*
4417c478bd9Sstevel@tonic-gate 	 * Don't set return breakpoints on writeable data
4427c478bd9Sstevel@tonic-gate 	 * or on any space other than executable text.
4437c478bd9Sstevel@tonic-gate 	 * Don't set breakpoints in the child of a vfork()
4447c478bd9Sstevel@tonic-gate 	 * because that would modify the parent's address space.
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 	if (is_vfork_child ||
4477c478bd9Sstevel@tonic-gate 	    (ret &&
4487c478bd9Sstevel@tonic-gate 	    ((pmp = Paddr_to_text_map(Proc, pc)) == NULL ||
4497c478bd9Sstevel@tonic-gate 	    !(pmp->pr_mflags & MA_EXEC) ||
4507c478bd9Sstevel@tonic-gate 	    (pmp->pr_mflags & MA_WRITE))))
4517c478bd9Sstevel@tonic-gate 		return (NULL);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/* create a new unnamed breakpoint */
4547c478bd9Sstevel@tonic-gate 	Bp = my_malloc(sizeof (struct bkpt), NULL);
4557c478bd9Sstevel@tonic-gate 	Bp->sym_name = NULL;
4567c478bd9Sstevel@tonic-gate 	Bp->dyn = NULL;
4577c478bd9Sstevel@tonic-gate 	Bp->addr = pc;
4587c478bd9Sstevel@tonic-gate 	Bp->instr = 0;
4597c478bd9Sstevel@tonic-gate 	Bp->flags = 0;
4607c478bd9Sstevel@tonic-gate 	if (set && Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0)
4617c478bd9Sstevel@tonic-gate 		Bp->flags |= BPT_ACTIVE;
4627c478bd9Sstevel@tonic-gate 	Bp->next = bpt_hashtable[hix];
4637c478bd9Sstevel@tonic-gate 	bpt_hashtable[hix] = Bp;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	return (Bp);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * Set all breakpoints that haven't been set yet.
4707c478bd9Sstevel@tonic-gate  * Deactivate all breakpoints from modules that are not present any more.
4717c478bd9Sstevel@tonic-gate  */
4727c478bd9Sstevel@tonic-gate void
set_deferred_breakpoints(void)4737c478bd9Sstevel@tonic-gate set_deferred_breakpoints(void)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
4767c478bd9Sstevel@tonic-gate 	int i;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (is_vfork_child)
4797c478bd9Sstevel@tonic-gate 		return;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++) {
4827c478bd9Sstevel@tonic-gate 		for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
4837c478bd9Sstevel@tonic-gate 			if (!(Bp->flags & BPT_ACTIVE)) {
4847c478bd9Sstevel@tonic-gate 				if (!(Bp->flags & BPT_EXCLUDE) &&
4857c478bd9Sstevel@tonic-gate 				    Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0)
4867c478bd9Sstevel@tonic-gate 					Bp->flags |= BPT_ACTIVE;
4877c478bd9Sstevel@tonic-gate 			} else if (Paddr_to_text_map(Proc, Bp->addr) == NULL) {
4887c478bd9Sstevel@tonic-gate 				Bp->flags &= ~BPT_ACTIVE;
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate int
symbol_iter(void * cd,const GElf_Sym * sym,const char * sym_name)4957c478bd9Sstevel@tonic-gate symbol_iter(void *cd, const GElf_Sym *sym, const char *sym_name)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	struct dynpat *Dyp = cd;
4987c478bd9Sstevel@tonic-gate 	struct dynlib *Dp = Dyp->Dp;
4997c478bd9Sstevel@tonic-gate 	uintptr_t pc = sym->st_value;
5007c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
5017c478bd9Sstevel@tonic-gate 	int i;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/* ignore any undefined symbols */
5047c478bd9Sstevel@tonic-gate 	if (sym->st_shndx == SHN_UNDEF)
5057c478bd9Sstevel@tonic-gate 		return (0);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/*
5087c478bd9Sstevel@tonic-gate 	 * Arbitrarily omit "_start" from the executable.
5097c478bd9Sstevel@tonic-gate 	 * (Avoid indentation before main().)
5107c478bd9Sstevel@tonic-gate 	 */
5117c478bd9Sstevel@tonic-gate 	if (*Dp->prt_name == '\0' && strcmp(sym_name, "_start") == 0)
5127c478bd9Sstevel@tonic-gate 		return (0);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/*
5157c478bd9Sstevel@tonic-gate 	 * Arbitrarily omit "_rt_boot" from the dynamic linker.
5167c478bd9Sstevel@tonic-gate 	 * (Avoid indentation before main().)
5177c478bd9Sstevel@tonic-gate 	 */
5187c478bd9Sstevel@tonic-gate 	if (strcmp(Dp->match_name, "ld") == 0 &&
5197c478bd9Sstevel@tonic-gate 	    strcmp(sym_name, "_rt_boot") == 0)
5207c478bd9Sstevel@tonic-gate 		return (0);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * Arbitrarily omit any symbols whose name starts with '.'.
5247c478bd9Sstevel@tonic-gate 	 * Apparantly putting a breakpoint on .umul causes a
5257c478bd9Sstevel@tonic-gate 	 * fatal error in libthread (%y is not restored correctly
5267c478bd9Sstevel@tonic-gate 	 * when a single step is taken).  Looks like a /proc bug.
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	if (*sym_name == '.')
5297c478bd9Sstevel@tonic-gate 		return (0);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * For each pattern in the array of symbol patterns,
5337c478bd9Sstevel@tonic-gate 	 * if the pattern matches the symbol name, then
5347c478bd9Sstevel@tonic-gate 	 * create a breakpoint at the function in question.
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	for (i = 0; i < Dyp->nsympat; i++) {
5377c478bd9Sstevel@tonic-gate 		if (interrupt|sigusr1)
5387c478bd9Sstevel@tonic-gate 			break;
5397c478bd9Sstevel@tonic-gate 		if (fnmatch(Dyp->sympat[i], sym_name, 0) != 0)
5407c478bd9Sstevel@tonic-gate 			continue;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		if ((Bp = create_bkpt(pc, 0, 0)) == NULL)	/* can't fail */
5437c478bd9Sstevel@tonic-gate 			return (0);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 		/*
5467c478bd9Sstevel@tonic-gate 		 * New breakpoints receive a name now.
5477c478bd9Sstevel@tonic-gate 		 * For existing breakpoints, prefer the subset name if possible,
5487c478bd9Sstevel@tonic-gate 		 * else prefer the shorter name.
5497c478bd9Sstevel@tonic-gate 		 */
5507c478bd9Sstevel@tonic-gate 		if (Bp->sym_name == NULL) {
5517c478bd9Sstevel@tonic-gate 			Bp->sym_name = strdup(sym_name);
5527c478bd9Sstevel@tonic-gate 		} else if (strstr(Bp->sym_name, sym_name) != NULL ||
5537c478bd9Sstevel@tonic-gate 		    strlen(Bp->sym_name) > strlen(sym_name)) {
5547c478bd9Sstevel@tonic-gate 			free(Bp->sym_name);
5557c478bd9Sstevel@tonic-gate 			Bp->sym_name = strdup(sym_name);
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 		Bp->dyn = Dp;
5587c478bd9Sstevel@tonic-gate 		Bp->flags |= Dyp->flag;
5597c478bd9Sstevel@tonic-gate 		if (Dyp->exclude)
5607c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_EXCLUDE;
5617c478bd9Sstevel@tonic-gate 		else if (Dyp->internal || *Dp->prt_name == '\0')
5627c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_INTERNAL;
5637c478bd9Sstevel@tonic-gate 		return (0);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	return (interrupt | sigusr1);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate /* For debugging only ---- */
5707c478bd9Sstevel@tonic-gate void
report_htable_stats(void)5717c478bd9Sstevel@tonic-gate report_htable_stats(void)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
5747c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
5757c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
5767c478bd9Sstevel@tonic-gate 	uint_t Min = 1000000;
5777c478bd9Sstevel@tonic-gate 	uint_t Max = 0;
5787c478bd9Sstevel@tonic-gate 	uint_t Avg = 0;
5797c478bd9Sstevel@tonic-gate 	uint_t Total = 0;
5807c478bd9Sstevel@tonic-gate 	uint_t i, j;
5817c478bd9Sstevel@tonic-gate 	uint_t bucket[HASHSZ];
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL || !hflag)
5847c478bd9Sstevel@tonic-gate 		return;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	hflag = FALSE;
5877c478bd9Sstevel@tonic-gate 	(void) memset(bucket, 0, sizeof (bucket));
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++) {
5907c478bd9Sstevel@tonic-gate 		j = 0;
5917c478bd9Sstevel@tonic-gate 		for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next)
5927c478bd9Sstevel@tonic-gate 			j++;
5937c478bd9Sstevel@tonic-gate 		if (j < Min)
5947c478bd9Sstevel@tonic-gate 			Min = j;
5957c478bd9Sstevel@tonic-gate 		if (j > Max)
5967c478bd9Sstevel@tonic-gate 			Max = j;
5977c478bd9Sstevel@tonic-gate 		if (j < HASHSZ)
5987c478bd9Sstevel@tonic-gate 			bucket[j]++;
5997c478bd9Sstevel@tonic-gate 		Total += j;
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	Avg = (Total + HASHSZ / 2) / HASHSZ;
6027c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "truss hash table statistics --------\n");
6037c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "    Total = %u\n", Total);
6047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "      Min = %u\n", Min);
6057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "      Max = %u\n", Max);
6067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "      Avg = %u\n", Avg);
6077c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++)
6087c478bd9Sstevel@tonic-gate 		if (bucket[i])
6097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "    %3u buckets of size %d\n",
6100df991f9SRoger A. Faulkner 			    bucket[i], i);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "truss-detected stacks --------\n");
6137c478bd9Sstevel@tonic-gate 	for (Stk = callstack; Stk != NULL; Stk = Stk->next) {
6147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6150df991f9SRoger A. Faulkner 		    "    base = 0x%.8lx  end = 0x%.8lx  size = %ld\n",
6160df991f9SRoger A. Faulkner 		    (ulong_t)Stk->stkbase,
6170df991f9SRoger A. Faulkner 		    (ulong_t)Stk->stkend,
6180df991f9SRoger A. Faulkner 		    (ulong_t)(Stk->stkend - Stk->stkbase));
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "primary unix stack --------\n");
6217c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6220df991f9SRoger A. Faulkner 	    "    base = 0x%.8lx  end = 0x%.8lx  size = %ld\n",
6230df991f9SRoger A. Faulkner 	    (ulong_t)Psp->pr_stkbase,
6240df991f9SRoger A. Faulkner 	    (ulong_t)(Psp->pr_stkbase + Psp->pr_stksize),
6250df991f9SRoger A. Faulkner 	    (ulong_t)Psp->pr_stksize);
6267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "nthr_create = %u\n", nthr_create);
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate void
make_lwp_stack(const lwpstatus_t * Lsp,prmap_t * Pmap,int nmap)6307c478bd9Sstevel@tonic-gate make_lwp_stack(const lwpstatus_t *Lsp, prmap_t *Pmap, int nmap)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
6337c478bd9Sstevel@tonic-gate 	uintptr_t sp = Lsp->pr_reg[R_SP];
6347c478bd9Sstevel@tonic-gate 	id_t lwpid = Lsp->pr_lwpid;
6357c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
6367c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
6377c478bd9Sstevel@tonic-gate 	td_thrinfo_t thrinfo;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64)
6407c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	/* check to see if we already have this stack */
6437c478bd9Sstevel@tonic-gate 	if (sp == 0)
6447c478bd9Sstevel@tonic-gate 		return;
6457c478bd9Sstevel@tonic-gate 	for (Stk = callstack; Stk != NULL; Stk = Stk->next)
6467c478bd9Sstevel@tonic-gate 		if (sp >= Stk->stkbase && sp < Stk->stkend)
6477c478bd9Sstevel@tonic-gate 			return;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	Stk = my_malloc(sizeof (struct callstack), NULL);
6507c478bd9Sstevel@tonic-gate 	Stk->next = callstack;
6517c478bd9Sstevel@tonic-gate 	callstack = Stk;
6527c478bd9Sstevel@tonic-gate 	nstack++;
6537c478bd9Sstevel@tonic-gate 	Stk->tref = 0;
6547c478bd9Sstevel@tonic-gate 	Stk->tid = 0;
6557c478bd9Sstevel@tonic-gate 	Stk->nthr_create = 0;
6567c478bd9Sstevel@tonic-gate 	Stk->ncall = 0;
6577c478bd9Sstevel@tonic-gate 	Stk->maxcall = DEF_MAXCALL;
6587c478bd9Sstevel@tonic-gate 	Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/* primary stack */
6617c478bd9Sstevel@tonic-gate 	if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
6627c478bd9Sstevel@tonic-gate 		Stk->stkbase = Psp->pr_stkbase;
6637c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Psp->pr_stksize;
6647c478bd9Sstevel@tonic-gate 		return;
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	/* alternate stack */
6687c478bd9Sstevel@tonic-gate 	if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) &&
6697c478bd9Sstevel@tonic-gate 	    sp >= (uintptr_t)Lsp->pr_altstack.ss_sp &&
6707c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)Lsp->pr_altstack.ss_sp
6717c478bd9Sstevel@tonic-gate 	    + Lsp->pr_altstack.ss_size) {
6727c478bd9Sstevel@tonic-gate 		Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp;
6737c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size;
6747c478bd9Sstevel@tonic-gate 		return;
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/* thread stacks? */
6787c478bd9Sstevel@tonic-gate 	if (Thr_agent != NULL &&
6797c478bd9Sstevel@tonic-gate 	    td_ta_map_lwp2thr(Thr_agent, lwpid, &th) == TD_OK &&
6807c478bd9Sstevel@tonic-gate 	    td_thr_get_info(&th, &thrinfo) == TD_OK &&
6817c478bd9Sstevel@tonic-gate 	    sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
6827c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)thrinfo.ti_stkbase) {
6837c478bd9Sstevel@tonic-gate 		/* The bloody fools got this backwards! */
6847c478bd9Sstevel@tonic-gate 		Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
6857c478bd9Sstevel@tonic-gate 		Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
6867c478bd9Sstevel@tonic-gate 		return;
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/* last chance -- try the raw memory map */
6907c478bd9Sstevel@tonic-gate 	for (; nmap; nmap--, Pmap++) {
6917c478bd9Sstevel@tonic-gate 		if (sp >= Pmap->pr_vaddr &&
6927c478bd9Sstevel@tonic-gate 		    sp < Pmap->pr_vaddr + Pmap->pr_size) {
6937c478bd9Sstevel@tonic-gate 			Stk->stkbase = Pmap->pr_vaddr;
6947c478bd9Sstevel@tonic-gate 			Stk->stkend = Pmap->pr_vaddr + Pmap->pr_size;
6957c478bd9Sstevel@tonic-gate 			return;
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	callstack = Stk->next;
7007c478bd9Sstevel@tonic-gate 	nstack--;
7017c478bd9Sstevel@tonic-gate 	free(Stk->stack);
7027c478bd9Sstevel@tonic-gate 	free(Stk);
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate void
make_thr_stack(const td_thrhandle_t * Thp,prgregset_t reg)7067c478bd9Sstevel@tonic-gate make_thr_stack(const td_thrhandle_t *Thp, prgregset_t reg)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
7097c478bd9Sstevel@tonic-gate 	td_thrinfo_t thrinfo;
7107c478bd9Sstevel@tonic-gate 	uintptr_t sp = reg[R_SP];
7117c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64)
7147c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/* check to see if we already have this stack */
7177c478bd9Sstevel@tonic-gate 	if (sp == 0)
7187c478bd9Sstevel@tonic-gate 		return;
7197c478bd9Sstevel@tonic-gate 	for (Stk = callstack; Stk != NULL; Stk = Stk->next)
7207c478bd9Sstevel@tonic-gate 		if (sp >= Stk->stkbase && sp < Stk->stkend)
7217c478bd9Sstevel@tonic-gate 			return;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	Stk = my_malloc(sizeof (struct callstack), NULL);
7247c478bd9Sstevel@tonic-gate 	Stk->next = callstack;
7257c478bd9Sstevel@tonic-gate 	callstack = Stk;
7267c478bd9Sstevel@tonic-gate 	nstack++;
7277c478bd9Sstevel@tonic-gate 	Stk->tref = 0;
7287c478bd9Sstevel@tonic-gate 	Stk->tid = 0;
7297c478bd9Sstevel@tonic-gate 	Stk->nthr_create = 0;
7307c478bd9Sstevel@tonic-gate 	Stk->ncall = 0;
7317c478bd9Sstevel@tonic-gate 	Stk->maxcall = DEF_MAXCALL;
7327c478bd9Sstevel@tonic-gate 	Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/* primary stack */
7357c478bd9Sstevel@tonic-gate 	if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
7367c478bd9Sstevel@tonic-gate 		Stk->stkbase = Psp->pr_stkbase;
7377c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Psp->pr_stksize;
7387c478bd9Sstevel@tonic-gate 		return;
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (td_thr_get_info(Thp, &thrinfo) == TD_OK &&
7427c478bd9Sstevel@tonic-gate 	    sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
7437c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)thrinfo.ti_stkbase) {
7447c478bd9Sstevel@tonic-gate 		/* The bloody fools got this backwards! */
7457c478bd9Sstevel@tonic-gate 		Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
7467c478bd9Sstevel@tonic-gate 		Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
7477c478bd9Sstevel@tonic-gate 		return;
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	callstack = Stk->next;
7517c478bd9Sstevel@tonic-gate 	nstack--;
7527c478bd9Sstevel@tonic-gate 	free(Stk->stack);
7537c478bd9Sstevel@tonic-gate 	free(Stk);
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate struct callstack *
find_lwp_stack(uintptr_t sp)7577c478bd9Sstevel@tonic-gate find_lwp_stack(uintptr_t sp)
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
7607c478bd9Sstevel@tonic-gate 	char mapfile[64];
7617c478bd9Sstevel@tonic-gate 	int mapfd;
7627c478bd9Sstevel@tonic-gate 	struct stat statb;
7637c478bd9Sstevel@tonic-gate 	prmap_t *Pmap = NULL;
7647c478bd9Sstevel@tonic-gate 	prmap_t *pmap = NULL;
7657c478bd9Sstevel@tonic-gate 	int nmap = 0;
7667c478bd9Sstevel@tonic-gate 	struct callstack *Stk = NULL;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	/*
7697c478bd9Sstevel@tonic-gate 	 * Get the address space map.
7707c478bd9Sstevel@tonic-gate 	 */
7717c478bd9Sstevel@tonic-gate 	(void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid);
7727c478bd9Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
7737c478bd9Sstevel@tonic-gate 	    fstat(mapfd, &statb) != 0 ||
7747c478bd9Sstevel@tonic-gate 	    statb.st_size < sizeof (prmap_t) ||
7757c478bd9Sstevel@tonic-gate 	    (Pmap = my_malloc(statb.st_size, NULL)) == NULL ||
7767c478bd9Sstevel@tonic-gate 	    (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
7777c478bd9Sstevel@tonic-gate 	    (nmap /= sizeof (prmap_t)) == 0) {
7787c478bd9Sstevel@tonic-gate 		if (Pmap != NULL)
7797c478bd9Sstevel@tonic-gate 			free(Pmap);
7807c478bd9Sstevel@tonic-gate 		if (mapfd >= 0)
7817c478bd9Sstevel@tonic-gate 			(void) close(mapfd);
7827c478bd9Sstevel@tonic-gate 		return (NULL);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	(void) close(mapfd);
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	for (pmap = Pmap; nmap--; pmap++) {
7877c478bd9Sstevel@tonic-gate 		if (sp >= pmap->pr_vaddr &&
7887c478bd9Sstevel@tonic-gate 		    sp < pmap->pr_vaddr + pmap->pr_size) {
7897c478bd9Sstevel@tonic-gate 			Stk = my_malloc(sizeof (struct callstack), NULL);
7907c478bd9Sstevel@tonic-gate 			Stk->next = callstack;
7917c478bd9Sstevel@tonic-gate 			callstack = Stk;
7927c478bd9Sstevel@tonic-gate 			nstack++;
7937c478bd9Sstevel@tonic-gate 			Stk->stkbase = pmap->pr_vaddr;
7947c478bd9Sstevel@tonic-gate 			Stk->stkend = pmap->pr_vaddr + pmap->pr_size;
7957c478bd9Sstevel@tonic-gate 			Stk->tref = 0;
7967c478bd9Sstevel@tonic-gate 			Stk->tid = 0;
7977c478bd9Sstevel@tonic-gate 			Stk->nthr_create = 0;
7987c478bd9Sstevel@tonic-gate 			Stk->ncall = 0;
7997c478bd9Sstevel@tonic-gate 			Stk->maxcall = DEF_MAXCALL;
8007c478bd9Sstevel@tonic-gate 			Stk->stack = my_malloc(
8010df991f9SRoger A. Faulkner 			    DEF_MAXCALL * sizeof (*Stk->stack), NULL);
8027c478bd9Sstevel@tonic-gate 			break;
8037c478bd9Sstevel@tonic-gate 		}
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	free(Pmap);
8077c478bd9Sstevel@tonic-gate 	return (Stk);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate struct callstack *
find_stack(uintptr_t sp)8117c478bd9Sstevel@tonic-gate find_stack(uintptr_t sp)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
8147c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
8157c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
8167c478bd9Sstevel@tonic-gate 	id_t lwpid = Lsp->pr_lwpid;
8177c478bd9Sstevel@tonic-gate #if defined(__sparc)
8187c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[R_G7];
8197c478bd9Sstevel@tonic-gate #elif defined(__amd64)
8207c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[REG_FS];
8217c478bd9Sstevel@tonic-gate #elif defined(__i386)
8227c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[GS];
8237c478bd9Sstevel@tonic-gate #endif
8247c478bd9Sstevel@tonic-gate 	struct callstack *Stk = NULL;
8257c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
8267c478bd9Sstevel@tonic-gate 	td_thrinfo_t thrinfo;
8277c478bd9Sstevel@tonic-gate 	td_err_e error;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	/* primary stack */
8307c478bd9Sstevel@tonic-gate 	if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
8317c478bd9Sstevel@tonic-gate 		Stk = my_malloc(sizeof (struct callstack), NULL);
8327c478bd9Sstevel@tonic-gate 		Stk->next = callstack;
8337c478bd9Sstevel@tonic-gate 		callstack = Stk;
8347c478bd9Sstevel@tonic-gate 		nstack++;
8357c478bd9Sstevel@tonic-gate 		Stk->stkbase = Psp->pr_stkbase;
8367c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Psp->pr_stksize;
8377c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
8387c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
8397c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
8407c478bd9Sstevel@tonic-gate 		Stk->ncall = 0;
8417c478bd9Sstevel@tonic-gate 		Stk->maxcall = DEF_MAXCALL;
8427c478bd9Sstevel@tonic-gate 		Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
8430df991f9SRoger A. Faulkner 		    NULL);
8447c478bd9Sstevel@tonic-gate 		return (Stk);
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/* alternate stack */
8487c478bd9Sstevel@tonic-gate 	if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) &&
8497c478bd9Sstevel@tonic-gate 	    sp >= (uintptr_t)Lsp->pr_altstack.ss_sp &&
8507c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)Lsp->pr_altstack.ss_sp
8517c478bd9Sstevel@tonic-gate 	    + Lsp->pr_altstack.ss_size) {
8527c478bd9Sstevel@tonic-gate 		Stk = my_malloc(sizeof (struct callstack), NULL);
8537c478bd9Sstevel@tonic-gate 		Stk->next = callstack;
8547c478bd9Sstevel@tonic-gate 		callstack = Stk;
8557c478bd9Sstevel@tonic-gate 		nstack++;
8567c478bd9Sstevel@tonic-gate 		Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp;
8577c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size;
8587c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
8597c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
8607c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
8617c478bd9Sstevel@tonic-gate 		Stk->ncall = 0;
8627c478bd9Sstevel@tonic-gate 		Stk->maxcall = DEF_MAXCALL;
8637c478bd9Sstevel@tonic-gate 		Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
8640df991f9SRoger A. Faulkner 		    NULL);
8657c478bd9Sstevel@tonic-gate 		return (Stk);
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL)
8697c478bd9Sstevel@tonic-gate 		return (find_lwp_stack(sp));
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	/* thread stacks? */
8727c478bd9Sstevel@tonic-gate 	if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) {
8737c478bd9Sstevel@tonic-gate 		if (hflag)
8747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8750df991f9SRoger A. Faulkner 			    "cannot get thread handle for "
8760df991f9SRoger A. Faulkner 			    "lwp#%d, error=%d, tref=0x%.8lx\n",
8770df991f9SRoger A. Faulkner 			    (int)lwpid, error, (long)tref);
8787c478bd9Sstevel@tonic-gate 		return (NULL);
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) {
8827c478bd9Sstevel@tonic-gate 		if (hflag)
8837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8840df991f9SRoger A. Faulkner 			    "cannot get thread info for "
8850df991f9SRoger A. Faulkner 			    "lwp#%d, error=%d, tref=0x%.8lx\n",
8860df991f9SRoger A. Faulkner 			    (int)lwpid, error, (long)tref);
8877c478bd9Sstevel@tonic-gate 		return (NULL);
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
8917c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)thrinfo.ti_stkbase) {
8927c478bd9Sstevel@tonic-gate 		Stk = my_malloc(sizeof (struct callstack), NULL);
8937c478bd9Sstevel@tonic-gate 		Stk->next = callstack;
8947c478bd9Sstevel@tonic-gate 		callstack = Stk;
8957c478bd9Sstevel@tonic-gate 		nstack++;
8967c478bd9Sstevel@tonic-gate 		/* The bloody fools got this backwards! */
8977c478bd9Sstevel@tonic-gate 		Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
8987c478bd9Sstevel@tonic-gate 		Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
8997c478bd9Sstevel@tonic-gate 		Stk->tref = tref;
9007c478bd9Sstevel@tonic-gate 		Stk->tid = thrinfo.ti_tid;
9017c478bd9Sstevel@tonic-gate 		Stk->nthr_create = nthr_create;
9027c478bd9Sstevel@tonic-gate 		Stk->ncall = 0;
9037c478bd9Sstevel@tonic-gate 		Stk->maxcall = DEF_MAXCALL;
9047c478bd9Sstevel@tonic-gate 		Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
9050df991f9SRoger A. Faulkner 		    NULL);
9067c478bd9Sstevel@tonic-gate 		return (Stk);
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	/* stack bounds failure -- complain bitterly */
9107c478bd9Sstevel@tonic-gate 	if (hflag) {
9117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9120df991f9SRoger A. Faulkner 		    "sp not within thread stack: "
9130df991f9SRoger A. Faulkner 		    "sp=0x%.8lx stkbase=0x%.8lx stkend=0x%.8lx\n",
9140df991f9SRoger A. Faulkner 		    (ulong_t)sp,
9150df991f9SRoger A. Faulkner 		    /* The bloody fools got this backwards! */
9160df991f9SRoger A. Faulkner 		    (ulong_t)thrinfo.ti_stkbase - thrinfo.ti_stksize,
9170df991f9SRoger A. Faulkner 		    (ulong_t)thrinfo.ti_stkbase);
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	return (NULL);
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate void
get_tid(struct callstack * Stk)9247c478bd9Sstevel@tonic-gate get_tid(struct callstack *Stk)
9257c478bd9Sstevel@tonic-gate {
9267c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
9277c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
9287c478bd9Sstevel@tonic-gate 	id_t lwpid = Lsp->pr_lwpid;
9297c478bd9Sstevel@tonic-gate #if defined(__sparc)
9307c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[R_G7];
9317c478bd9Sstevel@tonic-gate #elif defined(__amd64)
9327c478bd9Sstevel@tonic-gate 	prgreg_t tref = (data_model == PR_MODEL_LP64) ?
9337c478bd9Sstevel@tonic-gate 	    Lsp->pr_reg[REG_FS] : Lsp->pr_reg[REG_GS];
9347c478bd9Sstevel@tonic-gate #elif defined(__i386)
9357c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[GS];
9367c478bd9Sstevel@tonic-gate #endif
9377c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
9387c478bd9Sstevel@tonic-gate 	td_thrinfo_t thrinfo;
9397c478bd9Sstevel@tonic-gate 	td_err_e error;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL) {
9427c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
9437c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
9447c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
9457c478bd9Sstevel@tonic-gate 		return;
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	/*
9497c478bd9Sstevel@tonic-gate 	 * Shortcut here --
9507c478bd9Sstevel@tonic-gate 	 * If we have a matching tref and no new threads have
9517c478bd9Sstevel@tonic-gate 	 * been created since the last time we encountered this
9527c478bd9Sstevel@tonic-gate 	 * stack, then we don't have to go through the overhead
9537c478bd9Sstevel@tonic-gate 	 * of calling td_ta_map_lwp2thr() to get the thread-id.
9547c478bd9Sstevel@tonic-gate 	 */
9557c478bd9Sstevel@tonic-gate 	if (tref == Stk->tref && Stk->nthr_create == nthr_create)
9567c478bd9Sstevel@tonic-gate 		return;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) {
9597c478bd9Sstevel@tonic-gate 		if (hflag)
9607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9610df991f9SRoger A. Faulkner 			    "cannot get thread handle for "
9620df991f9SRoger A. Faulkner 			    "lwp#%d, error=%d, tref=0x%.8lx\n",
9630df991f9SRoger A. Faulkner 			    (int)lwpid, error, (long)tref);
9647c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
9657c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
9667c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
9677c478bd9Sstevel@tonic-gate 	} else if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) {
9687c478bd9Sstevel@tonic-gate 		if (hflag)
9697c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9700df991f9SRoger A. Faulkner 			    "cannot get thread info for "
9710df991f9SRoger A. Faulkner 			    "lwp#%d, error=%d, tref=0x%.8lx\n",
9720df991f9SRoger A. Faulkner 			    (int)lwpid, error, (long)tref);
9737c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
9747c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
9757c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
9767c478bd9Sstevel@tonic-gate 	} else {
9777c478bd9Sstevel@tonic-gate 		Stk->tref = tref;
9787c478bd9Sstevel@tonic-gate 		Stk->tid = thrinfo.ti_tid;
9797c478bd9Sstevel@tonic-gate 		Stk->nthr_create = nthr_create;
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate struct callstack *
callstack_info(uintptr_t sp,uintptr_t fp,int makeid)9847c478bd9Sstevel@tonic-gate callstack_info(uintptr_t sp, uintptr_t fp, int makeid)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
9877c478bd9Sstevel@tonic-gate 	uintptr_t trash;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	if (sp == 0 ||
9907c478bd9Sstevel@tonic-gate 	    Pread(Proc, &trash, sizeof (trash), sp) != sizeof (trash))
9917c478bd9Sstevel@tonic-gate 		return (NULL);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	for (Stk = callstack; Stk != NULL; Stk = Stk->next)
9947c478bd9Sstevel@tonic-gate 		if (sp >= Stk->stkbase && sp < Stk->stkend)
9957c478bd9Sstevel@tonic-gate 			break;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/*
9987c478bd9Sstevel@tonic-gate 	 * If we didn't find the stack, do it the hard way.
9997c478bd9Sstevel@tonic-gate 	 */
10007c478bd9Sstevel@tonic-gate 	if (Stk == NULL) {
10017c478bd9Sstevel@tonic-gate 		uintptr_t stkbase = sp;
10027c478bd9Sstevel@tonic-gate 		uintptr_t stkend;
10037c478bd9Sstevel@tonic-gate 		uint_t minsize;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
10067c478bd9Sstevel@tonic-gate #ifdef _LP64
10077c478bd9Sstevel@tonic-gate 		if (data_model == PR_MODEL_LP64)
10087c478bd9Sstevel@tonic-gate 			minsize = 2 * sizeof (uintptr_t);	/* fp + pc */
10097c478bd9Sstevel@tonic-gate 		else
10107c478bd9Sstevel@tonic-gate #endif
10117c478bd9Sstevel@tonic-gate 			minsize = 2 * sizeof (uint32_t);
10127c478bd9Sstevel@tonic-gate #else
10137c478bd9Sstevel@tonic-gate #ifdef _LP64
10147c478bd9Sstevel@tonic-gate 		if (data_model != PR_MODEL_LP64)
10157c478bd9Sstevel@tonic-gate 			minsize = SA32(MINFRAME32);
10167c478bd9Sstevel@tonic-gate 		else
10177c478bd9Sstevel@tonic-gate 			minsize = SA64(MINFRAME64);
10187c478bd9Sstevel@tonic-gate #else
10197c478bd9Sstevel@tonic-gate 		minsize = SA(MINFRAME);
10207c478bd9Sstevel@tonic-gate #endif
10217c478bd9Sstevel@tonic-gate #endif	/* i386 */
10227c478bd9Sstevel@tonic-gate 		stkend = sp + minsize;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 		while (Stk == NULL && fp != 0 && fp >= sp) {
10257c478bd9Sstevel@tonic-gate 			stkend = fp + minsize;
10267c478bd9Sstevel@tonic-gate 			for (Stk = callstack; Stk != NULL; Stk = Stk->next)
10277c478bd9Sstevel@tonic-gate 				if ((fp >= Stk->stkbase && fp < Stk->stkend) ||
10287c478bd9Sstevel@tonic-gate 				    (stkend > Stk->stkbase &&
10297c478bd9Sstevel@tonic-gate 				    stkend <= Stk->stkend))
10307c478bd9Sstevel@tonic-gate 					break;
10317c478bd9Sstevel@tonic-gate 			if (Stk == NULL)
10327c478bd9Sstevel@tonic-gate 				fp = previous_fp(fp, NULL);
10337c478bd9Sstevel@tonic-gate 		}
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 		if (Stk != NULL)	/* the stack grew */
10367c478bd9Sstevel@tonic-gate 			Stk->stkbase = stkbase;
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	if (Stk == NULL && makeid)	/* new stack */
10407c478bd9Sstevel@tonic-gate 		Stk = find_stack(sp);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	if (Stk == NULL)
10437c478bd9Sstevel@tonic-gate 		return (NULL);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	/*
10467c478bd9Sstevel@tonic-gate 	 * Ensure that there is room for at least one more entry.
10477c478bd9Sstevel@tonic-gate 	 */
10487c478bd9Sstevel@tonic-gate 	if (Stk->ncall == Stk->maxcall) {
10497c478bd9Sstevel@tonic-gate 		Stk->maxcall *= 2;
10507c478bd9Sstevel@tonic-gate 		Stk->stack = my_realloc(Stk->stack,
10517c478bd9Sstevel@tonic-gate 		    Stk->maxcall * sizeof (*Stk->stack), NULL);
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if (makeid)
10557c478bd9Sstevel@tonic-gate 		get_tid(Stk);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	return (Stk);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate /*
10617c478bd9Sstevel@tonic-gate  * Reset the breakpoint information (called on successful exec()).
10627c478bd9Sstevel@tonic-gate  */
10637c478bd9Sstevel@tonic-gate void
reset_breakpoints(void)10647c478bd9Sstevel@tonic-gate reset_breakpoints(void)
10657c478bd9Sstevel@tonic-gate {
10667c478bd9Sstevel@tonic-gate 	struct dynlib *Dp;
10677c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
10687c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
10697c478bd9Sstevel@tonic-gate 	int i;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL)
10727c478bd9Sstevel@tonic-gate 		return;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	/* destroy all previous dynamic library information */
1075*186f7fbfSEdward Pilatowicz 	while ((Dp = Dynlib) != NULL) {
1076*186f7fbfSEdward Pilatowicz 		Dynlib = Dp->next;
10777c478bd9Sstevel@tonic-gate 		free(Dp->lib_name);
10787c478bd9Sstevel@tonic-gate 		free(Dp->match_name);
10797c478bd9Sstevel@tonic-gate 		free(Dp->prt_name);
10807c478bd9Sstevel@tonic-gate 		free(Dp);
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/* destroy all previous breakpoint trap information */
10847c478bd9Sstevel@tonic-gate 	if (bpt_hashtable != NULL) {
10857c478bd9Sstevel@tonic-gate 		for (i = 0; i < HASHSZ; i++) {
10867c478bd9Sstevel@tonic-gate 			while ((Bp = bpt_hashtable[i]) != NULL) {
10877c478bd9Sstevel@tonic-gate 				bpt_hashtable[i] = Bp->next;
10887c478bd9Sstevel@tonic-gate 				if (Bp->sym_name)
10897c478bd9Sstevel@tonic-gate 					free(Bp->sym_name);
10907c478bd9Sstevel@tonic-gate 				free(Bp);
10917c478bd9Sstevel@tonic-gate 			}
10927c478bd9Sstevel@tonic-gate 		}
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	/* destroy all the callstack information */
10967c478bd9Sstevel@tonic-gate 	while ((Stk = callstack) != NULL) {
10977c478bd9Sstevel@tonic-gate 		callstack = Stk->next;
10987c478bd9Sstevel@tonic-gate 		free(Stk->stack);
10997c478bd9Sstevel@tonic-gate 		free(Stk);
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	/* we are not a multi-threaded process anymore */
11037c478bd9Sstevel@tonic-gate 	if (Thr_agent != NULL)
11047c478bd9Sstevel@tonic-gate 		(void) td_ta_delete(Thr_agent);
11057c478bd9Sstevel@tonic-gate 	Thr_agent = NULL;
1106