xref: /illumos-gate/usr/src/cmd/truss/fcall.c (revision 6f9914e7)
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 
192186f7fbfSEdward Pilatowicz 		for (Dp = Dynlib; Dp != NULL; Dp = Dp->next)
1936fced65dSraf 			Dp->present = FALSE;
1946fced65dSraf 		(void) Pobject_iter(Proc, object_present, NULL);
195186f7fbfSEdward 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 
316186f7fbfSEdward 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 		}
343186f7fbfSEdward Pilatowicz 		Dp->next = Dynlib;
344186f7fbfSEdward 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 
400186f7fbfSEdward 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 		if (data_model == PR_MODEL_LP64)
10077c478bd9Sstevel@tonic-gate 			minsize = 2 * sizeof (uintptr_t);	/* fp + pc */
10087c478bd9Sstevel@tonic-gate 		else
10097c478bd9Sstevel@tonic-gate 			minsize = 2 * sizeof (uint32_t);
10107c478bd9Sstevel@tonic-gate #else
10117c478bd9Sstevel@tonic-gate 		if (data_model != PR_MODEL_LP64)
10127c478bd9Sstevel@tonic-gate 			minsize = SA32(MINFRAME32);
10137c478bd9Sstevel@tonic-gate 		else
10147c478bd9Sstevel@tonic-gate 			minsize = SA64(MINFRAME64);
10157c478bd9Sstevel@tonic-gate #endif	/* i386 */
10167c478bd9Sstevel@tonic-gate 		stkend = sp + minsize;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 		while (Stk == NULL && fp != 0 && fp >= sp) {
10197c478bd9Sstevel@tonic-gate 			stkend = fp + minsize;
10207c478bd9Sstevel@tonic-gate 			for (Stk = callstack; Stk != NULL; Stk = Stk->next)
10217c478bd9Sstevel@tonic-gate 				if ((fp >= Stk->stkbase && fp < Stk->stkend) ||
10227c478bd9Sstevel@tonic-gate 				    (stkend > Stk->stkbase &&
10237c478bd9Sstevel@tonic-gate 				    stkend <= Stk->stkend))
10247c478bd9Sstevel@tonic-gate 					break;
10257c478bd9Sstevel@tonic-gate 			if (Stk == NULL)
10267c478bd9Sstevel@tonic-gate 				fp = previous_fp(fp, NULL);
10277c478bd9Sstevel@tonic-gate 		}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		if (Stk != NULL)	/* the stack grew */
10307c478bd9Sstevel@tonic-gate 			Stk->stkbase = stkbase;
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	if (Stk == NULL && makeid)	/* new stack */
10347c478bd9Sstevel@tonic-gate 		Stk = find_stack(sp);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if (Stk == NULL)
10377c478bd9Sstevel@tonic-gate 		return (NULL);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	/*
10407c478bd9Sstevel@tonic-gate 	 * Ensure that there is room for at least one more entry.
10417c478bd9Sstevel@tonic-gate 	 */
10427c478bd9Sstevel@tonic-gate 	if (Stk->ncall == Stk->maxcall) {
10437c478bd9Sstevel@tonic-gate 		Stk->maxcall *= 2;
10447c478bd9Sstevel@tonic-gate 		Stk->stack = my_realloc(Stk->stack,
10457c478bd9Sstevel@tonic-gate 		    Stk->maxcall * sizeof (*Stk->stack), NULL);
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	if (makeid)
10497c478bd9Sstevel@tonic-gate 		get_tid(Stk);
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	return (Stk);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate /*
10557c478bd9Sstevel@tonic-gate  * Reset the breakpoint information (called on successful exec()).
10567c478bd9Sstevel@tonic-gate  */
10577c478bd9Sstevel@tonic-gate void
reset_breakpoints(void)10587c478bd9Sstevel@tonic-gate reset_breakpoints(void)
10597c478bd9Sstevel@tonic-gate {
10607c478bd9Sstevel@tonic-gate 	struct dynlib *Dp;
10617c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
10627c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
10637c478bd9Sstevel@tonic-gate 	int i;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL)
10667c478bd9Sstevel@tonic-gate 		return;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	/* destroy all previous dynamic library information */
1069186f7fbfSEdward Pilatowicz 	while ((Dp = Dynlib) != NULL) {
1070186f7fbfSEdward Pilatowicz 		Dynlib = Dp->next;
10717c478bd9Sstevel@tonic-gate 		free(Dp->lib_name);
10727c478bd9Sstevel@tonic-gate 		free(Dp->match_name);
10737c478bd9Sstevel@tonic-gate 		free(Dp->prt_name);
10747c478bd9Sstevel@tonic-gate 		free(Dp);
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	/* destroy all previous breakpoint trap information */
10787c478bd9Sstevel@tonic-gate 	if (bpt_hashtable != NULL) {
10797c478bd9Sstevel@tonic-gate 		for (i = 0; i < HASHSZ; i++) {
10807c478bd9Sstevel@tonic-gate 			while ((Bp = bpt_hashtable[i]) != NULL) {
10817c478bd9Sstevel@tonic-gate 				bpt_hashtable[i] = Bp->next;
10827c478bd9Sstevel@tonic-gate 				if (Bp->sym_name)
10837c478bd9Sstevel@tonic-gate 					free(Bp->sym_name);
10847c478bd9Sstevel@tonic-gate 				free(Bp);
10857c478bd9Sstevel@tonic-gate 			}
10867c478bd9Sstevel@tonic-gate 		}
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	/* destroy all the callstack information */
10907c478bd9Sstevel@tonic-gate 	while ((Stk = callstack) != NULL) {
10917c478bd9Sstevel@tonic-gate 		callstack = Stk->next;
10927c478bd9Sstevel@tonic-gate 		free(Stk->stack);
10937c478bd9Sstevel@tonic-gate 		free(Stk);
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	/* we are not a multi-threaded process anymore */
10977c478bd9Sstevel@tonic-gate 	if (Thr_agent != NULL)
10987c478bd9Sstevel@tonic-gate 		(void) td_ta_delete(Thr_agent);
10997c478bd9Sstevel@tonic-gate 	Thr_agent = NULL;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/* tell libproc to clear out its mapping information */
11027c478bd9Sstevel@tonic-gate 	Preset_maps(Proc);
11037c478bd9Sstevel@tonic-gate 	Rdb_agent = NULL;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	/* Reestablish the symbols from the executable */
11067c478bd9Sstevel@tonic-gate 	(void) establish_breakpoints();
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate /*
11107c478bd9Sstevel@tonic-gate  * Clear breakpoints from the process (called before Prelease()).
11117c478bd9Sstevel@tonic-gate  * Don't actually destroy the breakpoint table;
11127c478bd9Sstevel@tonic-gate  * threads currently fielding breakpoints will need it.
11137c478bd9Sstevel@tonic-gate  */
11147c478bd9Sstevel@tonic-gate void
clear_breakpoints(void)11157c478bd9Sstevel@tonic-gate clear_breakpoints(void)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
11187c478bd9Sstevel@tonic-gate 	int i;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL)
11217c478bd9Sstevel@tonic-gate 		return;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	/*
11247c478bd9Sstevel@tonic-gate 	 * Change all breakpoint traps back to normal instructions.
11257c478bd9Sstevel@tonic-gate 	 * We attempt to remove a breakpoint from every address which
11267c478bd9Sstevel@tonic-gate 	 * may have ever contained a breakpoint to protect our victims.
11277c478bd9Sstevel@tonic-gate 	 */
11287c478bd9Sstevel@tonic-gate 	report_htable_stats();	/* report stats first */
11297c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++) {
11307c478bd9Sstevel@tonic-gate 		for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
11317c478bd9Sstevel@tonic-gate 			if (Bp->flags & BPT_ACTIVE)
11327c478bd9Sstevel@tonic-gate 				(void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
11337c478bd9Sstevel@tonic-gate 			Bp->flags &= ~BPT_ACTIVE;
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (Thr_agent != NULL) {
11387c478bd9Sstevel@tonic-gate 		td_thr_events_t events;
11397c478bd9Sstevel@tonic-gate 
11400df991f9SRoger A. Faulkner 		td_event_fillset(&events);
11410df991f9SRoger A. Faulkner 		(void) td_ta_clear_event(Thr_agent, &events);
11427c478bd9Sstevel@tonic-gate 		(void) td_ta_delete(Thr_agent);
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 	Thr_agent = NULL;
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate /*
11487c478bd9Sstevel@tonic-gate  * Reestablish the breakpoint traps in the process.
11497c478bd9Sstevel@tonic-gate  * Called after resuming from a vfork() in the parent.
11507c478bd9Sstevel@tonic-gate  */
11517c478bd9Sstevel@tonic-gate void
reestablish_traps(void)11527c478bd9Sstevel@tonic-gate reestablish_traps(void)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
11557c478bd9Sstevel@tonic-gate 	ulong_t instr;
11567c478bd9Sstevel@tonic-gate 	int i;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL || is_vfork_child)
11597c478bd9Sstevel@tonic-gate 		return;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++) {
11627c478bd9Sstevel@tonic-gate 		for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
11637c478bd9Sstevel@tonic-gate 			if ((Bp->flags & BPT_ACTIVE) &&
11647c478bd9Sstevel@tonic-gate 			    Psetbkpt(Proc, Bp->addr, &instr) != 0)
11657c478bd9Sstevel@tonic-gate 				Bp->flags &= ~BPT_ACTIVE;
11667c478bd9Sstevel@tonic-gate 		}
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate }
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate void
show_function_call(private_t * pri,struct callstack * Stk,struct dynlib * Dp,struct bkpt * Bp)11717c478bd9Sstevel@tonic-gate show_function_call(private_t *pri,
1172*6f9914e7SRichard Lowe     struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp)
11737c478bd9Sstevel@tonic-gate {
11747c478bd9Sstevel@tonic-gate 	long arg[8];
11757c478bd9Sstevel@tonic-gate 	int narg;
11767c478bd9Sstevel@tonic-gate 	int i;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	narg = get_arguments(arg);
11797c478bd9Sstevel@tonic-gate 	make_pname(pri, (Stk != NULL)? Stk->tid : 0);
11807c478bd9Sstevel@tonic-gate 	putpname(pri);
11817c478bd9Sstevel@tonic-gate 	timestamp(pri);
11827c478bd9Sstevel@tonic-gate 	if (Stk != NULL) {
11837c478bd9Sstevel@tonic-gate 		for (i = 1; i < Stk->ncall; i++) {
11847c478bd9Sstevel@tonic-gate 			(void) fputc(' ', stdout);
11857c478bd9Sstevel@tonic-gate 			(void) fputc(' ', stdout);
11867c478bd9Sstevel@tonic-gate 		}
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 	(void) printf("-> %s%s(", Dp->prt_name, Bp->sym_name);
11897c478bd9Sstevel@tonic-gate 	for (i = 0; i < narg; i++) {
11907c478bd9Sstevel@tonic-gate 		(void) printf("0x%lx", arg[i]);
11917c478bd9Sstevel@tonic-gate 		if (i < narg-1) {
11927c478bd9Sstevel@tonic-gate 			(void) fputc(',', stdout);
11937c478bd9Sstevel@tonic-gate 			(void) fputc(' ', stdout);
11947c478bd9Sstevel@tonic-gate 		}
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate 	(void) printf(")\n");
11977c478bd9Sstevel@tonic-gate 	Flush();
11987c478bd9Sstevel@tonic-gate }
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate /* ARGSUSED */
12017c478bd9Sstevel@tonic-gate void
show_function_return(private_t * pri,long rval,int stret,struct callstack * Stk,struct dynlib * Dp,struct bkpt * Bp)12027c478bd9Sstevel@tonic-gate show_function_return(private_t *pri, long rval, int stret,
1203*6f9914e7SRichard Lowe     struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp)
12047c478bd9Sstevel@tonic-gate {
12057c478bd9Sstevel@tonic-gate 	int i;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	make_pname(pri, Stk->tid);
12087c478bd9Sstevel@tonic-gate 	putpname(pri);
12097c478bd9Sstevel@tonic-gate 	timestamp(pri);
12107c478bd9Sstevel@tonic-gate 	for (i = 0; i < Stk->ncall; i++) {
12117c478bd9Sstevel@tonic-gate 		(void) fputc(' ', stdout);
12127c478bd9Sstevel@tonic-gate 		(void) fputc(' ', stdout);
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 	(void) printf("<- %s%s() = ", Dp->prt_name, Bp->sym_name);
12157c478bd9Sstevel@tonic-gate 	if (stret) {
12167c478bd9Sstevel@tonic-gate 		(void) printf("struct return\n");
12177c478bd9Sstevel@tonic-gate 	} else if (data_model == PR_MODEL_LP64) {
12187c478bd9Sstevel@tonic-gate 		if (rval >= (64 * 1024) || -rval >= (64 * 1024))
12197c478bd9Sstevel@tonic-gate 			(void) printf("0x%lx\n", rval);
12207c478bd9Sstevel@tonic-gate 		else
12217c478bd9Sstevel@tonic-gate 			(void) printf("%ld\n", rval);
12227c478bd9Sstevel@tonic-gate 	} else {
12237c478bd9Sstevel@tonic-gate 		int rval32 = (int)rval;
12247c478bd9Sstevel@tonic-gate 		if (rval32 >= (64 * 1024) || -rval32 >= (64 * 1024))
12257c478bd9Sstevel@tonic-gate 			(void) printf("0x%x\n", rval32);
12267c478bd9Sstevel@tonic-gate 		else
12277c478bd9Sstevel@tonic-gate 			(void) printf("%d\n", rval32);
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate 	Flush();
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate /*
12337c478bd9Sstevel@tonic-gate  * Called to deal with function-call tracing.
12347c478bd9Sstevel@tonic-gate  * Return 0 on normal success, 1 to indicate a BPT_HANG success,
12357c478bd9Sstevel@tonic-gate  * and -1 on failure (not tracing functions or unknown breakpoint).
12367c478bd9Sstevel@tonic-gate  */
12377c478bd9Sstevel@tonic-gate int
function_trace(private_t * pri,int first,int clear,int dotrace)12387c478bd9Sstevel@tonic-gate function_trace(private_t *pri, int first, int clear, int dotrace)
12397c478bd9Sstevel@tonic-gate {
12407c478bd9Sstevel@tonic-gate 	struct ps_lwphandle *Lwp = pri->Lwp;
12417c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
12427c478bd9Sstevel@tonic-gate 	uintptr_t pc = Lsp->pr_reg[R_PC];
12437c478bd9Sstevel@tonic-gate 	uintptr_t sp = Lsp->pr_reg[R_SP];
12447c478bd9Sstevel@tonic-gate 	uintptr_t fp = Lsp->pr_reg[R_FP];
12457c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
12467c478bd9Sstevel@tonic-gate 	struct dynlib *Dp;
12477c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
12487c478bd9Sstevel@tonic-gate 	ulong_t instr;
12497c478bd9Sstevel@tonic-gate 	int active;
12507c478bd9Sstevel@tonic-gate 	int rval = 0;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL)
12537c478bd9Sstevel@tonic-gate 		return (-1);
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64) {
12567c478bd9Sstevel@tonic-gate 		pc = (uint32_t)pc;
12577c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
12587c478bd9Sstevel@tonic-gate 		fp = (uint32_t)fp;
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	if ((Bp = get_bkpt(pc)) == NULL) {
12627c478bd9Sstevel@tonic-gate 		if (hflag)
12637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12640df991f9SRoger A. Faulkner 			    "function_trace(): "
12650df991f9SRoger A. Faulkner 			    "cannot find breakpoint for pc: 0x%.8lx\n",
12660df991f9SRoger A. Faulkner 			    (ulong_t)pc);
12677c478bd9Sstevel@tonic-gate 		return (-1);
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	if ((Bp->flags & (BPT_PREINIT|BPT_POSTINIT|BPT_DLACTIVITY)) && !clear) {
12717c478bd9Sstevel@tonic-gate 		rd_event_msg_t event_msg;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 		if (hflag) {
12747c478bd9Sstevel@tonic-gate 			if (Bp->flags & BPT_PREINIT)
12757c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "function_trace(): "
12760df991f9SRoger A. Faulkner 				    "RD_PREINIT breakpoint\n");
12777c478bd9Sstevel@tonic-gate 			if (Bp->flags & BPT_POSTINIT)
12787c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "function_trace(): "
12790df991f9SRoger A. Faulkner 				    "RD_POSTINIT breakpoint\n");
12807c478bd9Sstevel@tonic-gate 			if (Bp->flags & BPT_DLACTIVITY)
12817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "function_trace(): "
12820df991f9SRoger A. Faulkner 				    "RD_DLACTIVITY breakpoint\n");
12837c478bd9Sstevel@tonic-gate 		}
12847c478bd9Sstevel@tonic-gate 		if (rd_event_getmsg(Rdb_agent, &event_msg) == RD_OK) {
12857c478bd9Sstevel@tonic-gate 			if (event_msg.type == RD_DLACTIVITY) {
12866fced65dSraf 				switch (event_msg.u.state) {
12876fced65dSraf 				case RD_CONSISTENT:
12887c478bd9Sstevel@tonic-gate 					establish_breakpoints();
12896fced65dSraf 					break;
12906fced65dSraf 				case RD_ADD:
12917c478bd9Sstevel@tonic-gate 					not_consist = TRUE;	/* kludge */
12927c478bd9Sstevel@tonic-gate 					establish_breakpoints();
12937c478bd9Sstevel@tonic-gate 					not_consist = FALSE;
12946fced65dSraf 					break;
12956fced65dSraf 				case RD_DELETE:
12966fced65dSraf 					delete_library = TRUE;
12976fced65dSraf 					break;
12986fced65dSraf 				default:
12996fced65dSraf 					break;
13007c478bd9Sstevel@tonic-gate 				}
13017c478bd9Sstevel@tonic-gate 			}
13027c478bd9Sstevel@tonic-gate 			if (hflag) {
13037c478bd9Sstevel@tonic-gate 				const char *et;
13047c478bd9Sstevel@tonic-gate 				char buf[32];
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 				switch (event_msg.type) {
13077c478bd9Sstevel@tonic-gate 				case RD_NONE:
13087c478bd9Sstevel@tonic-gate 					et = "RD_NONE";
13097c478bd9Sstevel@tonic-gate 					break;
13107c478bd9Sstevel@tonic-gate 				case RD_PREINIT:
13117c478bd9Sstevel@tonic-gate 					et = "RD_PREINIT";
13127c478bd9Sstevel@tonic-gate 					break;
13137c478bd9Sstevel@tonic-gate 				case RD_POSTINIT:
13147c478bd9Sstevel@tonic-gate 					et = "RD_POSTINIT";
13157c478bd9Sstevel@tonic-gate 					break;
13167c478bd9Sstevel@tonic-gate 				case RD_DLACTIVITY:
13177c478bd9Sstevel@tonic-gate 					et = "RD_DLACTIVITY";
13187c478bd9Sstevel@tonic-gate 					break;
13197c478bd9Sstevel@tonic-gate 				default:
13207c478bd9Sstevel@tonic-gate 					(void) sprintf(buf, "0x%x",
13210df991f9SRoger A. Faulkner 					    event_msg.type);
13227c478bd9Sstevel@tonic-gate 					et = buf;
13237c478bd9Sstevel@tonic-gate 					break;
13247c478bd9Sstevel@tonic-gate 				}
13257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13260df991f9SRoger A. Faulkner 				    "event_msg.type = %s ", et);
13277c478bd9Sstevel@tonic-gate 				switch (event_msg.u.state) {
13287c478bd9Sstevel@tonic-gate 				case RD_NOSTATE:
13297c478bd9Sstevel@tonic-gate 					et = "RD_NOSTATE";
13307c478bd9Sstevel@tonic-gate 					break;
13317c478bd9Sstevel@tonic-gate 				case RD_CONSISTENT:
13327c478bd9Sstevel@tonic-gate 					et = "RD_CONSISTENT";
13337c478bd9Sstevel@tonic-gate 					break;
13347c478bd9Sstevel@tonic-gate 				case RD_ADD:
13357c478bd9Sstevel@tonic-gate 					et = "RD_ADD";
13367c478bd9Sstevel@tonic-gate 					break;
13377c478bd9Sstevel@tonic-gate 				case RD_DELETE:
13387c478bd9Sstevel@tonic-gate 					et = "RD_DELETE";
13397c478bd9Sstevel@tonic-gate 					break;
13407c478bd9Sstevel@tonic-gate 				default:
13417c478bd9Sstevel@tonic-gate 					(void) sprintf(buf, "0x%x",
13420df991f9SRoger A. Faulkner 					    event_msg.u.state);
13437c478bd9Sstevel@tonic-gate 					et = buf;
13447c478bd9Sstevel@tonic-gate 					break;
13457c478bd9Sstevel@tonic-gate 				}
13467c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13470df991f9SRoger A. Faulkner 				    "event_msg.u.state = %s\n", et);
13487c478bd9Sstevel@tonic-gate 			}
13497c478bd9Sstevel@tonic-gate 		}
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	if ((Bp->flags & BPT_TD_CREATE) && !clear) {
13537c478bd9Sstevel@tonic-gate 		nthr_create++;
13547c478bd9Sstevel@tonic-gate 		if (hflag)
13557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "function_trace(): "
13560df991f9SRoger A. Faulkner 			    "BPT_TD_CREATE breakpoint\n");
13577c478bd9Sstevel@tonic-gate 		/* we don't care about the event message */
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	Dp = Bp->dyn;
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	if (dotrace) {
13637c478bd9Sstevel@tonic-gate 		if ((Stk = callstack_info(sp, fp, 1)) == NULL) {
13647c478bd9Sstevel@tonic-gate 			if (Dp != NULL && !clear) {
13657c478bd9Sstevel@tonic-gate 				if (cflag) {
13667c478bd9Sstevel@tonic-gate 					add_fcall(fcall_tbl, Dp->prt_name,
13677c478bd9Sstevel@tonic-gate 					    Bp->sym_name, (unsigned long)1);
13687c478bd9Sstevel@tonic-gate 				}
13697c478bd9Sstevel@tonic-gate 				else
13707c478bd9Sstevel@tonic-gate 					show_function_call(pri, NULL, Dp, Bp);
13717c478bd9Sstevel@tonic-gate 				if ((Bp->flags & BPT_HANG) && !first)
13727c478bd9Sstevel@tonic-gate 					rval = 1;
13737c478bd9Sstevel@tonic-gate 			}
13747c478bd9Sstevel@tonic-gate 		} else if (!clear) {
13757c478bd9Sstevel@tonic-gate 			if (Dp != NULL) {
13767c478bd9Sstevel@tonic-gate 				function_entry(pri, Bp, Stk);
13777c478bd9Sstevel@tonic-gate 				if ((Bp->flags & BPT_HANG) && !first)
13787c478bd9Sstevel@tonic-gate 					rval = 1;
13797c478bd9Sstevel@tonic-gate 			} else {
13807c478bd9Sstevel@tonic-gate 				function_return(pri, Stk);
13817c478bd9Sstevel@tonic-gate 			}
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	/*
13867c478bd9Sstevel@tonic-gate 	 * Single-step the traced instruction. Since it's possible that
13877c478bd9Sstevel@tonic-gate 	 * another thread has deactivated this breakpoint, we indicate
13887c478bd9Sstevel@tonic-gate 	 * that we have reactivated it by virtue of executing it.
13897c478bd9Sstevel@tonic-gate 	 *
13907c478bd9Sstevel@tonic-gate 	 * To avoid a deadlock with some other thread in the process
13917c478bd9Sstevel@tonic-gate 	 * performing a fork() or a thr_suspend() operation, we must
13927c478bd9Sstevel@tonic-gate 	 * drop and later reacquire truss_lock.  Some fancy dancing here.
13937c478bd9Sstevel@tonic-gate 	 */
13947c478bd9Sstevel@tonic-gate 	active = (Bp->flags & BPT_ACTIVE);
13957c478bd9Sstevel@tonic-gate 	Bp->flags |= BPT_ACTIVE;
13967c478bd9Sstevel@tonic-gate 	instr = Bp->instr;
13977c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&truss_lock);
13987c478bd9Sstevel@tonic-gate 	(void) Lxecbkpt(Lwp, instr);
13997c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&truss_lock);
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	if (rval || clear) {	/* leave process stopped and abandoned */
14027c478bd9Sstevel@tonic-gate #if defined(__i386)
14037c478bd9Sstevel@tonic-gate 		/*
14047c478bd9Sstevel@tonic-gate 		 * Leave it stopped in a state that a stack trace is reasonable.
14057c478bd9Sstevel@tonic-gate 		 */
14067c478bd9Sstevel@tonic-gate 		/* XX64 needs to be updated for amd64 & gcc */
14077c478bd9Sstevel@tonic-gate 		if (rval && instr == 0x55) {	/* pushl %ebp */
14087c478bd9Sstevel@tonic-gate 			/* step it over the movl %esp,%ebp */
14097c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&truss_lock);
14107c478bd9Sstevel@tonic-gate 			(void) Lsetrun(Lwp, 0, PRCFAULT|PRSTEP);
14117c478bd9Sstevel@tonic-gate 			/* we're wrapping up; wait one second at most */
14127c478bd9Sstevel@tonic-gate 			(void) Lwait(Lwp, MILLISEC);
14137c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&truss_lock);
14147c478bd9Sstevel@tonic-gate 		}
14157c478bd9Sstevel@tonic-gate #endif
14167c478bd9Sstevel@tonic-gate 		if (get_bkpt(pc) != Bp)
14177c478bd9Sstevel@tonic-gate 			abend("function_trace: lost breakpoint", NULL);
14187c478bd9Sstevel@tonic-gate 		(void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
14197c478bd9Sstevel@tonic-gate 		Bp->flags &= ~BPT_ACTIVE;
14207c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&truss_lock);
14217c478bd9Sstevel@tonic-gate 		(void) Lsetrun(Lwp, 0, PRCFAULT|PRSTOP);
14227c478bd9Sstevel@tonic-gate 		/* we're wrapping up; wait one second at most */
14237c478bd9Sstevel@tonic-gate 		(void) Lwait(Lwp, MILLISEC);
14247c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&truss_lock);
14257c478bd9Sstevel@tonic-gate 	} else {
14267c478bd9Sstevel@tonic-gate 		if (get_bkpt(pc) != Bp)
14277c478bd9Sstevel@tonic-gate 			abend("function_trace: lost breakpoint", NULL);
14287c478bd9Sstevel@tonic-gate 		if (!active || !(Bp->flags & BPT_ACTIVE)) {
14297c478bd9Sstevel@tonic-gate 			(void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
14307c478bd9Sstevel@tonic-gate 			Bp->flags &= ~BPT_ACTIVE;
14317c478bd9Sstevel@tonic-gate 		}
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 	return (rval);
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate void
function_entry(private_t * pri,struct bkpt * Bp,struct callstack * Stk)14377c478bd9Sstevel@tonic-gate function_entry(private_t *pri, struct bkpt *Bp, struct callstack *Stk)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
14407c478bd9Sstevel@tonic-gate 	uintptr_t sp = Lsp->pr_reg[R_SP];
14417c478bd9Sstevel@tonic-gate 	uintptr_t rpc = get_return_address(&sp);
14427c478bd9Sstevel@tonic-gate 	struct dynlib *Dp = Bp->dyn;
14437c478bd9Sstevel@tonic-gate 	int oldframe = FALSE;
14447c478bd9Sstevel@tonic-gate 	int i;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64) {
14477c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
14487c478bd9Sstevel@tonic-gate 		rpc = (uint32_t)rpc;
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	/*
14527c478bd9Sstevel@tonic-gate 	 * If the sp is not within the stack bounds, forget it.
14537c478bd9Sstevel@tonic-gate 	 * If the symbol's 'internal' flag is false,
14547c478bd9Sstevel@tonic-gate 	 * don't report internal calls within the library.
14557c478bd9Sstevel@tonic-gate 	 */
14567c478bd9Sstevel@tonic-gate 	if (!(sp >= Stk->stkbase && sp < Stk->stkend) ||
14577c478bd9Sstevel@tonic-gate 	    (!(Bp->flags & BPT_INTERNAL) &&
14587c478bd9Sstevel@tonic-gate 	    rpc >= Dp->base && rpc < Dp->base + Dp->size))
14597c478bd9Sstevel@tonic-gate 		return;
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	for (i = 0; i < Stk->ncall; i++) {
14627c478bd9Sstevel@tonic-gate 		if (sp >= Stk->stack[i].sp) {
14637c478bd9Sstevel@tonic-gate 			Stk->ncall = i;
14647c478bd9Sstevel@tonic-gate 			if (sp == Stk->stack[i].sp)
14657c478bd9Sstevel@tonic-gate 				oldframe = TRUE;
14667c478bd9Sstevel@tonic-gate 			break;
14677c478bd9Sstevel@tonic-gate 		}
14687c478bd9Sstevel@tonic-gate 	}
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	/*
14717c478bd9Sstevel@tonic-gate 	 * Breakpoints for function returns are set here
14727c478bd9Sstevel@tonic-gate 	 * If we're counting function calls, there is no need to set
14737c478bd9Sstevel@tonic-gate 	 * a breakpoint upon return
14747c478bd9Sstevel@tonic-gate 	 */
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	if (!oldframe && !cflag) {
14777c478bd9Sstevel@tonic-gate 		(void) create_bkpt(rpc, 1, 1); /* may or may not be set */
14787c478bd9Sstevel@tonic-gate 		Stk->stack[Stk->ncall].sp = sp;	/* record it anyeay */
14797c478bd9Sstevel@tonic-gate 		Stk->stack[Stk->ncall].pc = rpc;
14807c478bd9Sstevel@tonic-gate 		Stk->stack[Stk->ncall].fcn = Bp;
14817c478bd9Sstevel@tonic-gate 	}
14827c478bd9Sstevel@tonic-gate 	Stk->ncall++;
14837c478bd9Sstevel@tonic-gate 	if (cflag) {
14847c478bd9Sstevel@tonic-gate 		add_fcall(fcall_tbl, Dp->prt_name, Bp->sym_name,
14857c478bd9Sstevel@tonic-gate 		    (unsigned long)1);
14867c478bd9Sstevel@tonic-gate 	} else {
14877c478bd9Sstevel@tonic-gate 		show_function_call(pri, Stk, Dp, Bp);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate /*
14927c478bd9Sstevel@tonic-gate  * We are here because we hit an unnamed breakpoint.
14937c478bd9Sstevel@tonic-gate  * Attempt to match this up with a return pc on the stack
14947c478bd9Sstevel@tonic-gate  * and report the function return.
14957c478bd9Sstevel@tonic-gate  */
14967c478bd9Sstevel@tonic-gate void
function_return(private_t * pri,struct callstack * Stk)14977c478bd9Sstevel@tonic-gate function_return(private_t *pri, struct callstack *Stk)
14987c478bd9Sstevel@tonic-gate {
14997c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
15007c478bd9Sstevel@tonic-gate 	uintptr_t sp = Lsp->pr_reg[R_SP];
15017c478bd9Sstevel@tonic-gate 	uintptr_t fp = Lsp->pr_reg[R_FP];
15027c478bd9Sstevel@tonic-gate 	int i;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64) {
15057c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
15067c478bd9Sstevel@tonic-gate 		fp = (uint32_t)fp;
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	if (fp < sp + 8)
15107c478bd9Sstevel@tonic-gate 		fp = sp + 8;
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	for (i = Stk->ncall - 1; i >= 0; i--) {
15137c478bd9Sstevel@tonic-gate 		if (sp <= Stk->stack[i].sp && fp > Stk->stack[i].sp) {
15147c478bd9Sstevel@tonic-gate 			Stk->ncall = i;
15157c478bd9Sstevel@tonic-gate 			break;
15167c478bd9Sstevel@tonic-gate 		}
15177c478bd9Sstevel@tonic-gate 	}
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
15207c478bd9Sstevel@tonic-gate 	if (i < 0) {
15217c478bd9Sstevel@tonic-gate 		/* probably __mul64() or friends -- try harder */
15227c478bd9Sstevel@tonic-gate 		int j;
15237c478bd9Sstevel@tonic-gate 		for (j = 0; i < 0 && j < 8; j++) {	/* up to 8 args */
15247c478bd9Sstevel@tonic-gate 			sp -= 4;
15257c478bd9Sstevel@tonic-gate 			for (i = Stk->ncall - 1; i >= 0; i--) {
15267c478bd9Sstevel@tonic-gate 				if (sp <= Stk->stack[i].sp &&
15277c478bd9Sstevel@tonic-gate 				    fp > Stk->stack[i].sp) {
15287c478bd9Sstevel@tonic-gate 					Stk->ncall = i;
15297c478bd9Sstevel@tonic-gate 					break;
15307c478bd9Sstevel@tonic-gate 				}
15317c478bd9Sstevel@tonic-gate 			}
15327c478bd9Sstevel@tonic-gate 		}
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate #endif
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	if ((i >= 0) && (!cflag)) {
15377c478bd9Sstevel@tonic-gate 		show_function_return(pri, Lsp->pr_reg[R_R0], 0,
15380df991f9SRoger A. Faulkner 		    Stk, Stk->stack[i].fcn->dyn, Stk->stack[i].fcn);
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate }
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate #if defined(__sparc)
15437c478bd9Sstevel@tonic-gate #define	FPADJUST	0
15447c478bd9Sstevel@tonic-gate #elif defined(__amd64)
15457c478bd9Sstevel@tonic-gate #define	FPADJUST	8
15467c478bd9Sstevel@tonic-gate #elif defined(__i386)
15477c478bd9Sstevel@tonic-gate #define	FPADJUST	4
15487c478bd9Sstevel@tonic-gate #endif
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate void
trap_one_stack(prgregset_t reg)15517c478bd9Sstevel@tonic-gate trap_one_stack(prgregset_t reg)
15527c478bd9Sstevel@tonic-gate {
15537c478bd9Sstevel@tonic-gate 	struct dynlib *Dp;
15547c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
15557c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
15567c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
15577c478bd9Sstevel@tonic-gate 	char sym_name[32];
15587c478bd9Sstevel@tonic-gate 	uintptr_t sp = reg[R_SP];
15597c478bd9Sstevel@tonic-gate 	uintptr_t pc = reg[R_PC];
15607c478bd9Sstevel@tonic-gate 	uintptr_t fp;
15617c478bd9Sstevel@tonic-gate 	uintptr_t rpc;
15627c478bd9Sstevel@tonic-gate 	uint_t nframe = 0;
15637c478bd9Sstevel@tonic-gate 	uint_t maxframe = 8;
15647c478bd9Sstevel@tonic-gate 	struct {
15657c478bd9Sstevel@tonic-gate 		uintptr_t sp;		/* %sp within called function */
15667c478bd9Sstevel@tonic-gate 		uintptr_t pc;		/* %pc within called function */
15677c478bd9Sstevel@tonic-gate 		uintptr_t rsp;		/* the return sp */
15687c478bd9Sstevel@tonic-gate 		uintptr_t rpc;		/* the return pc */
15697c478bd9Sstevel@tonic-gate 	} *frame = my_malloc(maxframe * sizeof (*frame), NULL);
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	/*
15727c478bd9Sstevel@tonic-gate 	 * Gather stack frames bottom to top.
15737c478bd9Sstevel@tonic-gate 	 */
15747c478bd9Sstevel@tonic-gate 	while (sp != 0) {
15757c478bd9Sstevel@tonic-gate 		fp = sp;	/* remember higest non-null sp */
15767c478bd9Sstevel@tonic-gate 		frame[nframe].sp = sp;
15777c478bd9Sstevel@tonic-gate 		frame[nframe].pc = pc;
15787c478bd9Sstevel@tonic-gate 		sp = previous_fp(sp, &pc);
15797c478bd9Sstevel@tonic-gate 		frame[nframe].rsp = sp;
15807c478bd9Sstevel@tonic-gate 		frame[nframe].rpc = pc;
15817c478bd9Sstevel@tonic-gate 		if (++nframe == maxframe) {
15827c478bd9Sstevel@tonic-gate 			maxframe *= 2;
15837c478bd9Sstevel@tonic-gate 			frame = my_realloc(frame, maxframe * sizeof (*frame),
15840df991f9SRoger A. Faulkner 			    NULL);
15857c478bd9Sstevel@tonic-gate 		}
15867c478bd9Sstevel@tonic-gate 	}
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	/*
15897c478bd9Sstevel@tonic-gate 	 * Scan for function return breakpoints top to bottom.
15907c478bd9Sstevel@tonic-gate 	 */
15917c478bd9Sstevel@tonic-gate 	while (nframe--) {
15927c478bd9Sstevel@tonic-gate 		/* lookup the called function in the symbol tables */
15937c478bd9Sstevel@tonic-gate 		if (Plookup_by_addr(Proc, frame[nframe].pc, sym_name,
15947c478bd9Sstevel@tonic-gate 		    sizeof (sym_name), &sym) != 0)
15957c478bd9Sstevel@tonic-gate 			continue;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 		pc = sym.st_value;	/* entry point of the function */
15987c478bd9Sstevel@tonic-gate 		rpc = frame[nframe].rpc;	/* caller's return pc */
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 		/* lookup the function in the breakpoint table */
16017c478bd9Sstevel@tonic-gate 		if ((Bp = get_bkpt(pc)) == NULL || (Dp = Bp->dyn) == NULL)
16027c478bd9Sstevel@tonic-gate 			continue;
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 		if (!(Bp->flags & BPT_INTERNAL) &&
16057c478bd9Sstevel@tonic-gate 		    rpc >= Dp->base && rpc < Dp->base + Dp->size)
16067c478bd9Sstevel@tonic-gate 			continue;
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 		sp = frame[nframe].rsp + FPADJUST;  /* %sp at time of call */
16097c478bd9Sstevel@tonic-gate 		if ((Stk = callstack_info(sp, fp, 0)) == NULL)
16107c478bd9Sstevel@tonic-gate 			continue;	/* can't happen? */
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 		if (create_bkpt(rpc, 1, 1) != NULL) {
16137c478bd9Sstevel@tonic-gate 			Stk->stack[Stk->ncall].sp = sp;
16147c478bd9Sstevel@tonic-gate 			Stk->stack[Stk->ncall].pc = rpc;
16157c478bd9Sstevel@tonic-gate 			Stk->stack[Stk->ncall].fcn = Bp;
16167c478bd9Sstevel@tonic-gate 			Stk->ncall++;
16177c478bd9Sstevel@tonic-gate 		}
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	free(frame);
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate int
lwp_stack_traps(void * cd,const lwpstatus_t * Lsp)16247c478bd9Sstevel@tonic-gate lwp_stack_traps(void *cd, const lwpstatus_t *Lsp)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	ph_map_t *ph_map = (ph_map_t *)cd;
16277c478bd9Sstevel@tonic-gate 	prgregset_t reg;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	(void) memcpy(reg, Lsp->pr_reg, sizeof (prgregset_t));
16307c478bd9Sstevel@tonic-gate 	make_lwp_stack(Lsp, ph_map->pmap, ph_map->nmap);
16317c478bd9Sstevel@tonic-gate 	trap_one_stack(reg);
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	return (interrupt | sigusr1);
16347c478bd9Sstevel@tonic-gate }
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate /* ARGSUSED */
16377c478bd9Sstevel@tonic-gate int
thr_stack_traps(const td_thrhandle_t * Thp,void * cd)16387c478bd9Sstevel@tonic-gate thr_stack_traps(const td_thrhandle_t *Thp, void *cd)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate 	prgregset_t reg;
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	/*
16437c478bd9Sstevel@tonic-gate 	 * We have already dealt with all the lwps.
16447c478bd9Sstevel@tonic-gate 	 * We only care about unbound threads here (TD_PARTIALREG).
16457c478bd9Sstevel@tonic-gate 	 */
16467c478bd9Sstevel@tonic-gate 	if (td_thr_getgregs(Thp, reg) != TD_PARTIALREG)
16477c478bd9Sstevel@tonic-gate 		return (0);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	make_thr_stack(Thp, reg);
16507c478bd9Sstevel@tonic-gate 	trap_one_stack(reg);
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	return (interrupt | sigusr1);
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate #if defined(__sparc)
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate uintptr_t
previous_fp(uintptr_t sp,uintptr_t * rpc)16587c478bd9Sstevel@tonic-gate previous_fp(uintptr_t sp, uintptr_t *rpc)
16597c478bd9Sstevel@tonic-gate {
16607c478bd9Sstevel@tonic-gate 	uintptr_t fp = 0;
16617c478bd9Sstevel@tonic-gate 	uintptr_t pc = 0;
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
16647c478bd9Sstevel@tonic-gate 		struct rwindow64 rwin;
16657c478bd9Sstevel@tonic-gate 		if (Pread(Proc, &rwin, sizeof (rwin), sp + STACK_BIAS)
16667c478bd9Sstevel@tonic-gate 		    == sizeof (rwin)) {
16677c478bd9Sstevel@tonic-gate 			fp = (uintptr_t)rwin.rw_fp;
16687c478bd9Sstevel@tonic-gate 			pc = (uintptr_t)rwin.rw_rtn;
16697c478bd9Sstevel@tonic-gate 		}
16707c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
16717c478bd9Sstevel@tonic-gate 		    Pread(Proc, &rwin, sizeof (rwin), fp + STACK_BIAS)
16727c478bd9Sstevel@tonic-gate 		    != sizeof (rwin))
16737c478bd9Sstevel@tonic-gate 			fp = pc = 0;
16747c478bd9Sstevel@tonic-gate 	} else {
16757c478bd9Sstevel@tonic-gate 		struct rwindow32 rwin;
16767c478bd9Sstevel@tonic-gate 		if (Pread(Proc, &rwin, sizeof (rwin), sp) == sizeof (rwin)) {
16777c478bd9Sstevel@tonic-gate 			fp = (uint32_t)rwin.rw_fp;
16787c478bd9Sstevel@tonic-gate 			pc = (uint32_t)rwin.rw_rtn;
16797c478bd9Sstevel@tonic-gate 		}
16807c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
16817c478bd9Sstevel@tonic-gate 		    Pread(Proc, &rwin, sizeof (rwin), fp) != sizeof (rwin))
16827c478bd9Sstevel@tonic-gate 			fp = pc = 0;
16837c478bd9Sstevel@tonic-gate 	}
16847c478bd9Sstevel@tonic-gate 	if (rpc)
16857c478bd9Sstevel@tonic-gate 		*rpc = pc;
16867c478bd9Sstevel@tonic-gate 	return (fp);
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate /* ARGSUSED */
16907c478bd9Sstevel@tonic-gate uintptr_t
get_return_address(uintptr_t * psp)16917c478bd9Sstevel@tonic-gate get_return_address(uintptr_t *psp)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	instr_t inst;
16947c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
16957c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
16967c478bd9Sstevel@tonic-gate 	uintptr_t rpc;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	rpc = (uintptr_t)Lsp->pr_reg[R_O7] + 8;
16997c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64)
17007c478bd9Sstevel@tonic-gate 		rpc = (uint32_t)rpc;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	/* check for structure return (bletch!) */
17037c478bd9Sstevel@tonic-gate 	if (Pread(Proc, &inst, sizeof (inst), rpc) == sizeof (inst) &&
17047c478bd9Sstevel@tonic-gate 	    inst < 0x1000)
17057c478bd9Sstevel@tonic-gate 		rpc += sizeof (instr_t);
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	return (rpc);
17087c478bd9Sstevel@tonic-gate }
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate int
get_arguments(long * argp)17117c478bd9Sstevel@tonic-gate get_arguments(long *argp)
17127c478bd9Sstevel@tonic-gate {
17137c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
17147c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
17157c478bd9Sstevel@tonic-gate 	int i;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64)
17187c478bd9Sstevel@tonic-gate 		for (i = 0; i < 4; i++)
17197c478bd9Sstevel@tonic-gate 			argp[i] = (uint_t)Lsp->pr_reg[R_O0+i];
17207c478bd9Sstevel@tonic-gate 	else
17217c478bd9Sstevel@tonic-gate 		for (i = 0; i < 4; i++)
17227c478bd9Sstevel@tonic-gate 			argp[i] = (long)Lsp->pr_reg[R_O0+i];
17237c478bd9Sstevel@tonic-gate 	return (4);
17247c478bd9Sstevel@tonic-gate }
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate #endif	/* __sparc */
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate uintptr_t
previous_fp(uintptr_t fp,uintptr_t * rpc)17317c478bd9Sstevel@tonic-gate previous_fp(uintptr_t fp, uintptr_t *rpc)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	uintptr_t frame[2];
17347c478bd9Sstevel@tonic-gate 	uintptr_t trash[2];
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	if (Pread(Proc, frame, sizeof (frame), fp) != sizeof (frame) ||
17377c478bd9Sstevel@tonic-gate 	    (frame[0] != 0 &&
17387c478bd9Sstevel@tonic-gate 	    Pread(Proc, trash, sizeof (trash), frame[0]) != sizeof (trash)))
17397c478bd9Sstevel@tonic-gate 		frame[0] = frame[1] = 0;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	if (rpc)
17427c478bd9Sstevel@tonic-gate 		*rpc = frame[1];
17437c478bd9Sstevel@tonic-gate 	return (frame[0]);
17447c478bd9Sstevel@tonic-gate }
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate #endif
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate #if defined(__amd64) || defined(__i386)
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate /*
17517c478bd9Sstevel@tonic-gate  * Examine the instruction at the return location of a function call
17527c478bd9Sstevel@tonic-gate  * and return the byte count by which the stack is adjusted on return.
17537c478bd9Sstevel@tonic-gate  * It the instruction at the return location is an addl, as expected,
17547c478bd9Sstevel@tonic-gate  * then adjust the return pc by the size of that instruction so that
17557c478bd9Sstevel@tonic-gate  * we will place the return breakpoint on the following instruction.
17567c478bd9Sstevel@tonic-gate  * This allows programs that interrogate their own stacks and record
17577c478bd9Sstevel@tonic-gate  * function calls and arguments to work correctly even while we interfere.
17587c478bd9Sstevel@tonic-gate  * Return the count on success, -1 on failure.
17597c478bd9Sstevel@tonic-gate  */
17607c478bd9Sstevel@tonic-gate int
return_count32(uint32_t * ppc)17617c478bd9Sstevel@tonic-gate return_count32(uint32_t *ppc)
17627c478bd9Sstevel@tonic-gate {
17637c478bd9Sstevel@tonic-gate 	uintptr_t pc = *ppc;
17647c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
17657c478bd9Sstevel@tonic-gate 	int count;
17667c478bd9Sstevel@tonic-gate 	uchar_t instr[6];	/* instruction at pc */
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	if ((count = Pread(Proc, instr, sizeof (instr), pc)) < 0)
17697c478bd9Sstevel@tonic-gate 		return (-1);
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	/* find the replaced instruction at pc (if any) */
17727c478bd9Sstevel@tonic-gate 	if ((Bp = get_bkpt(pc)) != NULL && (Bp->flags & BPT_ACTIVE))
17737c478bd9Sstevel@tonic-gate 		instr[0] = (uchar_t)Bp->instr;
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	if (count != sizeof (instr) &&
17767c478bd9Sstevel@tonic-gate 	    (count < 3 || instr[0] != 0x83))
17777c478bd9Sstevel@tonic-gate 		return (-1);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	/*
17807c478bd9Sstevel@tonic-gate 	 * A bit of disassembly of the instruction is required here.
17817c478bd9Sstevel@tonic-gate 	 */
17827c478bd9Sstevel@tonic-gate 	if (instr[1] != 0xc4) {	/* not an addl mumble,%esp inctruction */
17837c478bd9Sstevel@tonic-gate 		count = 0;
17847c478bd9Sstevel@tonic-gate 	} else if (instr[0] == 0x81) {	/* count is a longword */
17857c478bd9Sstevel@tonic-gate 		count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24);
17867c478bd9Sstevel@tonic-gate 		*ppc += 6;
17877c478bd9Sstevel@tonic-gate 	} else if (instr[0] == 0x83) {	/* count is a byte */
17887c478bd9Sstevel@tonic-gate 		count = instr[2];
17897c478bd9Sstevel@tonic-gate 		*ppc += 3;
17907c478bd9Sstevel@tonic-gate 	} else {		/* not an addl inctruction */
17917c478bd9Sstevel@tonic-gate 		count = 0;
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	return (count);
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate uintptr_t
get_return_address32(uintptr_t * psp)17987c478bd9Sstevel@tonic-gate get_return_address32(uintptr_t *psp)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate 	uint32_t sp = *psp;
18017c478bd9Sstevel@tonic-gate 	uint32_t rpc;
18027c478bd9Sstevel@tonic-gate 	int count;
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	*psp += 4;	/* account for popping the stack on return */
18057c478bd9Sstevel@tonic-gate 	if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc))
18067c478bd9Sstevel@tonic-gate 		return (0);
18077c478bd9Sstevel@tonic-gate 	if ((count = return_count32(&rpc)) < 0)
18087c478bd9Sstevel@tonic-gate 		count = 0;
18097c478bd9Sstevel@tonic-gate 	*psp += count;		/* expected sp on return */
18107c478bd9Sstevel@tonic-gate 	return (rpc);
18117c478bd9Sstevel@tonic-gate }
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate uintptr_t
get_return_address(uintptr_t * psp)18147c478bd9Sstevel@tonic-gate get_return_address(uintptr_t *psp)
18157c478bd9Sstevel@tonic-gate {
18167c478bd9Sstevel@tonic-gate 	uintptr_t rpc;
18177c478bd9Sstevel@tonic-gate 	uintptr_t sp = *psp;
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
18207c478bd9Sstevel@tonic-gate 		if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc))
18217c478bd9Sstevel@tonic-gate 			return (0);
18227c478bd9Sstevel@tonic-gate 		/*
18237c478bd9Sstevel@tonic-gate 		 * Ignore arguments pushed on the stack.  See comments in
18247c478bd9Sstevel@tonic-gate 		 * get_arguments().
18257c478bd9Sstevel@tonic-gate 		 */
18267c478bd9Sstevel@tonic-gate 		return (rpc);
18277c478bd9Sstevel@tonic-gate 	} else
18287c478bd9Sstevel@tonic-gate 		return (get_return_address32(psp));
18297c478bd9Sstevel@tonic-gate }
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate int
get_arguments32(long * argp)18337c478bd9Sstevel@tonic-gate get_arguments32(long *argp)
18347c478bd9Sstevel@tonic-gate {
18357c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
18367c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
18377c478bd9Sstevel@tonic-gate 	uint32_t frame[5];	/* return pc + 4 args */
18387c478bd9Sstevel@tonic-gate 	int narg;
18397c478bd9Sstevel@tonic-gate 	int count;
18407c478bd9Sstevel@tonic-gate 	int i;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	narg = Pread(Proc, frame, sizeof (frame),
18430df991f9SRoger A. Faulkner 	    (uintptr_t)Lsp->pr_reg[R_SP]);
18447c478bd9Sstevel@tonic-gate 	narg -= sizeof (greg32_t);
18457c478bd9Sstevel@tonic-gate 	if (narg <= 0)
18467c478bd9Sstevel@tonic-gate 		return (0);
18477c478bd9Sstevel@tonic-gate 	narg /= sizeof (greg32_t); /* no more than 4 */
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	/*
18507c478bd9Sstevel@tonic-gate 	 * Given the return PC, determine the number of arguments.
18517c478bd9Sstevel@tonic-gate 	 */
18527c478bd9Sstevel@tonic-gate 	if ((count = return_count32(&frame[0])) < 0)
18537c478bd9Sstevel@tonic-gate 		narg = 0;
18547c478bd9Sstevel@tonic-gate 	else {
18557c478bd9Sstevel@tonic-gate 		count /= sizeof (greg32_t);
18567c478bd9Sstevel@tonic-gate 		if (narg > count)
18577c478bd9Sstevel@tonic-gate 			narg = count;
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	for (i = 0; i < narg; i++)
18617c478bd9Sstevel@tonic-gate 		argp[i] = (long)frame[i+1];
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	return (narg);
18647c478bd9Sstevel@tonic-gate }
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate int
get_arguments(long * argp)18677c478bd9Sstevel@tonic-gate get_arguments(long *argp)
18687c478bd9Sstevel@tonic-gate {
18697c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
18707c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
18737c478bd9Sstevel@tonic-gate 		/*
18747c478bd9Sstevel@tonic-gate 		 * On amd64, we do not know how many arguments are passed to
18757c478bd9Sstevel@tonic-gate 		 * each function.  While it may be possible to detect if we
18767c478bd9Sstevel@tonic-gate 		 * have more than 6 arguments, it is of marginal value.
18777c478bd9Sstevel@tonic-gate 		 * Instead, assume that we always have 6 arguments, which are
18787c478bd9Sstevel@tonic-gate 		 * passed via registers.
18797c478bd9Sstevel@tonic-gate 		 */
18807c478bd9Sstevel@tonic-gate 		argp[0] = Lsp->pr_reg[REG_RDI];
18817c478bd9Sstevel@tonic-gate 		argp[1] = Lsp->pr_reg[REG_RSI];
18827c478bd9Sstevel@tonic-gate 		argp[2] = Lsp->pr_reg[REG_RDX];
18837c478bd9Sstevel@tonic-gate 		argp[3] = Lsp->pr_reg[REG_RCX];
18847c478bd9Sstevel@tonic-gate 		argp[4] = Lsp->pr_reg[REG_R8];
18857c478bd9Sstevel@tonic-gate 		argp[5] = Lsp->pr_reg[REG_R9];
18867c478bd9Sstevel@tonic-gate 		return (6);
18877c478bd9Sstevel@tonic-gate 	} else
18887c478bd9Sstevel@tonic-gate 		return (get_arguments32(argp));
18897c478bd9Sstevel@tonic-gate }
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate #endif	/* __amd64 || __i386 */
1892