xref: /titanic_53/usr/src/cmd/truss/fcall.c (revision 6fced65dc155494a63adf3c0c3d6d7ead47949d3)
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
5*6fced65dSraf  * Common Development and Distribution License (the "License").
6*6fced65dSraf  * 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  */
21*6fced65dSraf 
227c478bd9Sstevel@tonic-gate /*
23*6fced65dSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #define	_SYSCALL32
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <memory.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stack.h>
407c478bd9Sstevel@tonic-gate #include <signal.h>
417c478bd9Sstevel@tonic-gate #include <limits.h>
427c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
437c478bd9Sstevel@tonic-gate #include <proc_service.h>
447c478bd9Sstevel@tonic-gate #include <dlfcn.h>
457c478bd9Sstevel@tonic-gate #include <fnmatch.h>
467c478bd9Sstevel@tonic-gate #include <libproc.h>
477c478bd9Sstevel@tonic-gate #include "ramdata.h"
487c478bd9Sstevel@tonic-gate #include "systable.h"
497c478bd9Sstevel@tonic-gate #include "print.h"
507c478bd9Sstevel@tonic-gate #include "proto.h"
517c478bd9Sstevel@tonic-gate #include "htbl.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Functions supporting library function call tracing.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate typedef struct {
587c478bd9Sstevel@tonic-gate 	prmap_t	*pmap;
597c478bd9Sstevel@tonic-gate 	int	nmap;
607c478bd9Sstevel@tonic-gate } ph_map_t;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * static functions in this file.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate void function_entry(private_t *, struct bkpt *, struct callstack *);
667c478bd9Sstevel@tonic-gate void function_return(private_t *, struct callstack *);
677c478bd9Sstevel@tonic-gate int object_iter(void *, const prmap_t *, const char *);
68*6fced65dSraf int object_present(void *, const prmap_t *, const char *);
697c478bd9Sstevel@tonic-gate int symbol_iter(void *, const GElf_Sym *, const char *);
707c478bd9Sstevel@tonic-gate uintptr_t get_return_address(uintptr_t *);
717c478bd9Sstevel@tonic-gate int get_arguments(long *argp);
727c478bd9Sstevel@tonic-gate uintptr_t previous_fp(uintptr_t, uintptr_t *);
737c478bd9Sstevel@tonic-gate int lwp_stack_traps(void *cd, const lwpstatus_t *Lsp);
747c478bd9Sstevel@tonic-gate int thr_stack_traps(const td_thrhandle_t *Thp, void *cd);
757c478bd9Sstevel@tonic-gate struct bkpt *create_bkpt(uintptr_t, int, int);
767c478bd9Sstevel@tonic-gate void set_deferred_breakpoints(void);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	DEF_MAXCALL	16	/* initial value of Stk->maxcall */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	FAULT_ADDR	((uintptr_t)(0-8))
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	HASHSZ	2048
837c478bd9Sstevel@tonic-gate #define	bpt_hash(addr)	((((addr) >> 13) ^ ((addr) >> 2)) & 0x7ff)
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static void
867c478bd9Sstevel@tonic-gate setup_thread_agent(void)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
897c478bd9Sstevel@tonic-gate 	td_notify_t notify;
907c478bd9Sstevel@tonic-gate 	td_thr_events_t events;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (Thr_agent != NULL)	/* only once */
937c478bd9Sstevel@tonic-gate 		return;
947c478bd9Sstevel@tonic-gate 	if (td_init() != TD_OK || td_ta_new(Proc, &Thr_agent) != TD_OK)
957c478bd9Sstevel@tonic-gate 		Thr_agent = NULL;
967c478bd9Sstevel@tonic-gate 	else {
977c478bd9Sstevel@tonic-gate 		td_event_emptyset(&events);
987c478bd9Sstevel@tonic-gate 		td_event_addset(&events, TD_CREATE);
997c478bd9Sstevel@tonic-gate 		if (td_ta_event_addr(Thr_agent, TD_CREATE, &notify) == TD_OK &&
1007c478bd9Sstevel@tonic-gate 		    notify.type == NOTIFY_BPT &&
1017c478bd9Sstevel@tonic-gate 		    td_ta_set_event(Thr_agent, &events) == TD_OK &&
1027c478bd9Sstevel@tonic-gate 		    (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
1037c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_TD_CREATE;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
108*6fced65dSraf  * Delete all breakpoints in the range [base .. base+size)
109*6fced65dSraf  * from the breakpoint hash table.
110*6fced65dSraf  */
111*6fced65dSraf static void
112*6fced65dSraf delete_breakpoints(uintptr_t base, size_t size)
113*6fced65dSraf {
114*6fced65dSraf 	struct bkpt **Bpp;
115*6fced65dSraf 	struct bkpt *Bp;
116*6fced65dSraf 	int i;
117*6fced65dSraf 
118*6fced65dSraf 	if (bpt_hashtable == NULL)
119*6fced65dSraf 		return;
120*6fced65dSraf 	for (i = 0; i < HASHSZ; i++) {
121*6fced65dSraf 		Bpp = &bpt_hashtable[i];
122*6fced65dSraf 		while ((Bp = *Bpp) != NULL) {
123*6fced65dSraf 			if (Bp->addr < base || Bp->addr >= base + size) {
124*6fced65dSraf 				Bpp = &Bp->next;
125*6fced65dSraf 				continue;
126*6fced65dSraf 			}
127*6fced65dSraf 			*Bpp = Bp->next;
128*6fced65dSraf 			if (Bp->sym_name)
129*6fced65dSraf 				free(Bp->sym_name);
130*6fced65dSraf 			free(Bp);
131*6fced65dSraf 		}
132*6fced65dSraf 	}
133*6fced65dSraf }
134*6fced65dSraf 
135*6fced65dSraf /*
1367c478bd9Sstevel@tonic-gate  * Establishment of breakpoints on traced library functions.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate void
1397c478bd9Sstevel@tonic-gate establish_breakpoints(void)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL)
1427c478bd9Sstevel@tonic-gate 		return;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/* allocate the breakpoint hash table */
1457c478bd9Sstevel@tonic-gate 	if (bpt_hashtable == NULL) {
1467c478bd9Sstevel@tonic-gate 		bpt_hashtable = my_malloc(HASHSZ * sizeof (struct bkpt *),
1477c478bd9Sstevel@tonic-gate 			NULL);
1487c478bd9Sstevel@tonic-gate 		(void) memset(bpt_hashtable, 0,
1497c478bd9Sstevel@tonic-gate 			HASHSZ * sizeof (struct bkpt *));
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * Set special rtld_db event breakpoints, first time only.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	if (Rdb_agent == NULL &&
1567c478bd9Sstevel@tonic-gate 	    (Rdb_agent = Prd_agent(Proc)) != NULL) {
1577c478bd9Sstevel@tonic-gate 		rd_notify_t notify;
1587c478bd9Sstevel@tonic-gate 		struct bkpt *Bp;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		(void) rd_event_enable(Rdb_agent, 1);
1617c478bd9Sstevel@tonic-gate 		if (rd_event_addr(Rdb_agent, RD_PREINIT, &notify) == RD_OK &&
1627c478bd9Sstevel@tonic-gate 		    (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
1637c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_PREINIT;
1647c478bd9Sstevel@tonic-gate 		if (rd_event_addr(Rdb_agent, RD_POSTINIT, &notify) == RD_OK &&
1657c478bd9Sstevel@tonic-gate 		    (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
1667c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_POSTINIT;
1677c478bd9Sstevel@tonic-gate 		if (rd_event_addr(Rdb_agent, RD_DLACTIVITY, &notify) == RD_OK &&
1687c478bd9Sstevel@tonic-gate 		    (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
1697c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_DLACTIVITY;
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Set special thread event breakpoint, first time libc is seen.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL)
1767c478bd9Sstevel@tonic-gate 		setup_thread_agent();
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * Tell libproc to update its mappings.
1807c478bd9Sstevel@tonic-gate 	 */
1817c478bd9Sstevel@tonic-gate 	Pupdate_maps(Proc);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
184*6fced65dSraf 	 * If rtld_db told us a library was being deleted,
185*6fced65dSraf 	 * first mark all of the dynlibs as not present, then
186*6fced65dSraf 	 * iterate over the shared objects, marking only those
187*6fced65dSraf 	 * present that really are present, and finally delete
188*6fced65dSraf 	 * all of the not-present dynlibs.
189*6fced65dSraf 	 */
190*6fced65dSraf 	if (delete_library) {
191*6fced65dSraf 		struct dynlib **Dpp;
192*6fced65dSraf 		struct dynlib *Dp;
193*6fced65dSraf 
194*6fced65dSraf 		for (Dp = Dyn; Dp != NULL; Dp = Dp->next)
195*6fced65dSraf 			Dp->present = FALSE;
196*6fced65dSraf 		(void) Pobject_iter(Proc, object_present, NULL);
197*6fced65dSraf 		Dpp = &Dyn;
198*6fced65dSraf 		while ((Dp = *Dpp) != NULL) {
199*6fced65dSraf 			if (Dp->present) {
200*6fced65dSraf 				Dpp = &Dp->next;
201*6fced65dSraf 				continue;
202*6fced65dSraf 			}
203*6fced65dSraf 			delete_breakpoints(Dp->base, Dp->size);
204*6fced65dSraf 			*Dpp = Dp->next;
205*6fced65dSraf 			free(Dp->lib_name);
206*6fced65dSraf 			free(Dp->match_name);
207*6fced65dSraf 			free(Dp->prt_name);
208*6fced65dSraf 			free(Dp);
209*6fced65dSraf 		}
210*6fced65dSraf 		delete_library = FALSE;
211*6fced65dSraf 	}
212*6fced65dSraf 
213*6fced65dSraf 	/*
2147c478bd9Sstevel@tonic-gate 	 * Iterate over the shared objects, creating breakpoints.
2157c478bd9Sstevel@tonic-gate 	 */
2167c478bd9Sstevel@tonic-gate 	(void) Pobject_iter(Proc, object_iter, NULL);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Now actually set all the breakpoints we just created.
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	set_deferred_breakpoints();
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * Initial establishment of stacks in a newly-grabbed process.
2267c478bd9Sstevel@tonic-gate  * establish_breakpoints() has already been called.
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate void
2297c478bd9Sstevel@tonic-gate establish_stacks(void)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
2327c478bd9Sstevel@tonic-gate 	char mapfile[64];
2337c478bd9Sstevel@tonic-gate 	int mapfd;
2347c478bd9Sstevel@tonic-gate 	struct stat statb;
2357c478bd9Sstevel@tonic-gate 	prmap_t *Pmap = NULL;
2367c478bd9Sstevel@tonic-gate 	int nmap = 0;
2377c478bd9Sstevel@tonic-gate 	ph_map_t ph_map;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	(void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid);
2407c478bd9Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
2417c478bd9Sstevel@tonic-gate 	    fstat(mapfd, &statb) != 0 ||
2427c478bd9Sstevel@tonic-gate 	    statb.st_size < sizeof (prmap_t) ||
2437c478bd9Sstevel@tonic-gate 	    (Pmap = my_malloc(statb.st_size, NULL)) == NULL ||
2447c478bd9Sstevel@tonic-gate 	    (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
2457c478bd9Sstevel@tonic-gate 	    (nmap /= sizeof (prmap_t)) == 0) {
2467c478bd9Sstevel@tonic-gate 		if (Pmap != NULL)
2477c478bd9Sstevel@tonic-gate 			free(Pmap);
2487c478bd9Sstevel@tonic-gate 		Pmap = NULL;
2497c478bd9Sstevel@tonic-gate 		nmap = 0;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	if (mapfd >= 0)
2527c478bd9Sstevel@tonic-gate 		(void) close(mapfd);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * Iterate over lwps, establishing stacks.
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	ph_map.pmap = Pmap;
2587c478bd9Sstevel@tonic-gate 	ph_map.nmap = nmap;
2597c478bd9Sstevel@tonic-gate 	(void) Plwp_iter(Proc, lwp_stack_traps, &ph_map);
2607c478bd9Sstevel@tonic-gate 	if (Pmap != NULL)
2617c478bd9Sstevel@tonic-gate 		free(Pmap);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL)
2647c478bd9Sstevel@tonic-gate 		return;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Iterate over unbound threads, establishing stacks.
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	(void) td_ta_thr_iter(Thr_agent, thr_stack_traps, NULL,
2707c478bd9Sstevel@tonic-gate 		TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
2717c478bd9Sstevel@tonic-gate 		TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate void
2757c478bd9Sstevel@tonic-gate do_symbol_iter(const char *object_name, struct dynpat *Dyp)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	if (*Dyp->Dp->prt_name == '\0')
2787c478bd9Sstevel@tonic-gate 		object_name = PR_OBJ_EXEC;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/*
2817c478bd9Sstevel@tonic-gate 	 * Always search the dynamic symbol table.
2827c478bd9Sstevel@tonic-gate 	 */
2837c478bd9Sstevel@tonic-gate 	(void) Psymbol_iter(Proc, object_name,
2847c478bd9Sstevel@tonic-gate 		PR_DYNSYM, BIND_WEAK|BIND_GLOBAL|TYPE_FUNC,
2857c478bd9Sstevel@tonic-gate 		symbol_iter, Dyp);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * Search the static symbol table if this is the
2897c478bd9Sstevel@tonic-gate 	 * executable file or if we are being asked to
2907c478bd9Sstevel@tonic-gate 	 * report internal calls within the library.
2917c478bd9Sstevel@tonic-gate 	 */
2927c478bd9Sstevel@tonic-gate 	if (object_name == PR_OBJ_EXEC || Dyp->internal)
2937c478bd9Sstevel@tonic-gate 		(void) Psymbol_iter(Proc, object_name,
2947c478bd9Sstevel@tonic-gate 			PR_SYMTAB, BIND_ANY|TYPE_FUNC,
2957c478bd9Sstevel@tonic-gate 			symbol_iter, Dyp);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate /* ARGSUSED */
2997c478bd9Sstevel@tonic-gate int
3007c478bd9Sstevel@tonic-gate object_iter(void *cd, const prmap_t *pmp, const char *object_name)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	char name[100];
3037c478bd9Sstevel@tonic-gate 	struct dynpat *Dyp;
3047c478bd9Sstevel@tonic-gate 	struct dynlib *Dp;
3057c478bd9Sstevel@tonic-gate 	const char *str;
3067c478bd9Sstevel@tonic-gate 	char *s;
3077c478bd9Sstevel@tonic-gate 	int i;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if ((pmp->pr_mflags & MA_WRITE) || !(pmp->pr_mflags & MA_EXEC))
3107c478bd9Sstevel@tonic-gate 		return (0);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * Set special thread event breakpoint, first time libc is seen.
3147c478bd9Sstevel@tonic-gate 	 */
3157c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL && strstr(object_name, "/libc.so.") != NULL)
3167c478bd9Sstevel@tonic-gate 		setup_thread_agent();
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	for (Dp = Dyn; Dp != NULL; Dp = Dp->next)
3197c478bd9Sstevel@tonic-gate 		if (strcmp(object_name, Dp->lib_name) == 0 ||
3207c478bd9Sstevel@tonic-gate 		    (strcmp(Dp->lib_name, "a.out") == 0 &&
3217c478bd9Sstevel@tonic-gate 		    strcmp(pmp->pr_mapname, "a.out") == 0))
3227c478bd9Sstevel@tonic-gate 			break;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if (Dp == NULL) {
3257c478bd9Sstevel@tonic-gate 		Dp = my_malloc(sizeof (struct dynlib), NULL);
3267c478bd9Sstevel@tonic-gate 		(void) memset(Dp, 0, sizeof (struct dynlib));
3277c478bd9Sstevel@tonic-gate 		if (strcmp(pmp->pr_mapname, "a.out") == 0) {
3287c478bd9Sstevel@tonic-gate 			Dp->lib_name = strdup(pmp->pr_mapname);
3297c478bd9Sstevel@tonic-gate 			Dp->match_name = strdup(pmp->pr_mapname);
3307c478bd9Sstevel@tonic-gate 			Dp->prt_name = strdup("");
3317c478bd9Sstevel@tonic-gate 		} else {
3327c478bd9Sstevel@tonic-gate 			Dp->lib_name = strdup(object_name);
3337c478bd9Sstevel@tonic-gate 			if ((str = strrchr(object_name, '/')) != NULL)
3347c478bd9Sstevel@tonic-gate 				str++;
3357c478bd9Sstevel@tonic-gate 			else
3367c478bd9Sstevel@tonic-gate 				str = object_name;
3377c478bd9Sstevel@tonic-gate 			(void) strncpy(name, str, sizeof (name) - 2);
3387c478bd9Sstevel@tonic-gate 			name[sizeof (name) - 2] = '\0';
3397c478bd9Sstevel@tonic-gate 			if ((s = strstr(name, ".so")) != NULL)
3407c478bd9Sstevel@tonic-gate 				*s = '\0';
3417c478bd9Sstevel@tonic-gate 			Dp->match_name = strdup(name);
3427c478bd9Sstevel@tonic-gate 			(void) strcat(name, ":");
3437c478bd9Sstevel@tonic-gate 			Dp->prt_name = strdup(name);
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 		Dp->next = Dyn;
3467c478bd9Sstevel@tonic-gate 		Dyn = Dp;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (Dp->built ||
3507c478bd9Sstevel@tonic-gate 	    (not_consist && strcmp(Dp->prt_name, "ld:") != 0))	/* kludge */
3517c478bd9Sstevel@tonic-gate 		return (0);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (hflag && not_consist)
3547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "not_consist is TRUE, building %s\n",
3557c478bd9Sstevel@tonic-gate 			Dp->lib_name);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	Dp->base = pmp->pr_vaddr;
3587c478bd9Sstevel@tonic-gate 	Dp->size = pmp->pr_size;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/*
3617c478bd9Sstevel@tonic-gate 	 * For every dynlib pattern that matches this library's name,
3627c478bd9Sstevel@tonic-gate 	 * iterate through all of the library's symbols looking for
3637c478bd9Sstevel@tonic-gate 	 * matching symbol name patterns.
3647c478bd9Sstevel@tonic-gate 	 */
3657c478bd9Sstevel@tonic-gate 	for (Dyp = Dynpat; Dyp != NULL; Dyp = Dyp->next) {
3667c478bd9Sstevel@tonic-gate 		if (interrupt|sigusr1)
3677c478bd9Sstevel@tonic-gate 			break;
3687c478bd9Sstevel@tonic-gate 		for (i = 0; i < Dyp->nlibpat; i++) {
3697c478bd9Sstevel@tonic-gate 			if (interrupt|sigusr1)
3707c478bd9Sstevel@tonic-gate 				break;
3717c478bd9Sstevel@tonic-gate 			if (fnmatch(Dyp->libpat[i], Dp->match_name, 0) != 0)
3727c478bd9Sstevel@tonic-gate 				continue;	/* no match */
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 			/*
3757c478bd9Sstevel@tonic-gate 			 * Require an exact match for the executable (a.out)
3767c478bd9Sstevel@tonic-gate 			 * and for the dynamic linker (ld.so.1).
3777c478bd9Sstevel@tonic-gate 			 */
3787c478bd9Sstevel@tonic-gate 			if ((strcmp(Dp->match_name, "a.out") == 0 ||
3797c478bd9Sstevel@tonic-gate 			    strcmp(Dp->match_name, "ld") == 0) &&
3807c478bd9Sstevel@tonic-gate 			    strcmp(Dyp->libpat[i], Dp->match_name) != 0)
3817c478bd9Sstevel@tonic-gate 				continue;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 			/*
3847c478bd9Sstevel@tonic-gate 			 * Set Dyp->Dp to Dp so symbol_iter() can use it.
3857c478bd9Sstevel@tonic-gate 			 */
3867c478bd9Sstevel@tonic-gate 			Dyp->Dp = Dp;
3877c478bd9Sstevel@tonic-gate 			do_symbol_iter(object_name, Dyp);
3887c478bd9Sstevel@tonic-gate 			Dyp->Dp = NULL;
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	Dp->built = TRUE;
3937c478bd9Sstevel@tonic-gate 	return (interrupt | sigusr1);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
396*6fced65dSraf /* ARGSUSED */
397*6fced65dSraf int
398*6fced65dSraf object_present(void *cd, const prmap_t *pmp, const char *object_name)
399*6fced65dSraf {
400*6fced65dSraf 	struct dynlib *Dp;
401*6fced65dSraf 
402*6fced65dSraf 	for (Dp = Dyn; Dp != NULL; Dp = Dp->next) {
403*6fced65dSraf 		if (Dp->base == pmp->pr_vaddr)
404*6fced65dSraf 			Dp->present = TRUE;
405*6fced65dSraf 	}
406*6fced65dSraf 
407*6fced65dSraf 	return (0);
408*6fced65dSraf }
409*6fced65dSraf 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * Search for an existing breakpoint at the 'pc' location.
4127c478bd9Sstevel@tonic-gate  */
4137c478bd9Sstevel@tonic-gate struct bkpt *
4147c478bd9Sstevel@tonic-gate get_bkpt(uintptr_t pc)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	for (Bp = bpt_hashtable[bpt_hash(pc)]; Bp != NULL; Bp = Bp->next)
4197c478bd9Sstevel@tonic-gate 		if (pc == Bp->addr)
4207c478bd9Sstevel@tonic-gate 			break;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	return (Bp);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  * Create a breakpoint at 'pc', if one is not there already.
4277c478bd9Sstevel@tonic-gate  * 'ret' is true when creating a function return breakpoint, in which case
4287c478bd9Sstevel@tonic-gate  * fail and return NULL if the breakpoint would be created in writeable data.
4297c478bd9Sstevel@tonic-gate  * If 'set' it true, set the breakpoint in the process now.
4307c478bd9Sstevel@tonic-gate  */
4317c478bd9Sstevel@tonic-gate struct bkpt *
4327c478bd9Sstevel@tonic-gate create_bkpt(uintptr_t pc, int ret, int set)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	uint_t hix = bpt_hash(pc);
4357c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
4367c478bd9Sstevel@tonic-gate 	const prmap_t *pmp;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	for (Bp = bpt_hashtable[hix]; Bp != NULL; Bp = Bp->next)
4397c478bd9Sstevel@tonic-gate 		if (pc == Bp->addr)
4407c478bd9Sstevel@tonic-gate 			return (Bp);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	/*
4437c478bd9Sstevel@tonic-gate 	 * Don't set return breakpoints on writeable data
4447c478bd9Sstevel@tonic-gate 	 * or on any space other than executable text.
4457c478bd9Sstevel@tonic-gate 	 * Don't set breakpoints in the child of a vfork()
4467c478bd9Sstevel@tonic-gate 	 * because that would modify the parent's address space.
4477c478bd9Sstevel@tonic-gate 	 */
4487c478bd9Sstevel@tonic-gate 	if (is_vfork_child ||
4497c478bd9Sstevel@tonic-gate 	    (ret &&
4507c478bd9Sstevel@tonic-gate 	    ((pmp = Paddr_to_text_map(Proc, pc)) == NULL ||
4517c478bd9Sstevel@tonic-gate 	    !(pmp->pr_mflags & MA_EXEC) ||
4527c478bd9Sstevel@tonic-gate 	    (pmp->pr_mflags & MA_WRITE))))
4537c478bd9Sstevel@tonic-gate 		return (NULL);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/* create a new unnamed breakpoint */
4567c478bd9Sstevel@tonic-gate 	Bp = my_malloc(sizeof (struct bkpt), NULL);
4577c478bd9Sstevel@tonic-gate 	Bp->sym_name = NULL;
4587c478bd9Sstevel@tonic-gate 	Bp->dyn = NULL;
4597c478bd9Sstevel@tonic-gate 	Bp->addr = pc;
4607c478bd9Sstevel@tonic-gate 	Bp->instr = 0;
4617c478bd9Sstevel@tonic-gate 	Bp->flags = 0;
4627c478bd9Sstevel@tonic-gate 	if (set && Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0)
4637c478bd9Sstevel@tonic-gate 		Bp->flags |= BPT_ACTIVE;
4647c478bd9Sstevel@tonic-gate 	Bp->next = bpt_hashtable[hix];
4657c478bd9Sstevel@tonic-gate 	bpt_hashtable[hix] = Bp;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	return (Bp);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate /*
4717c478bd9Sstevel@tonic-gate  * Set all breakpoints that haven't been set yet.
4727c478bd9Sstevel@tonic-gate  * Deactivate all breakpoints from modules that are not present any more.
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate void
4757c478bd9Sstevel@tonic-gate set_deferred_breakpoints(void)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
4787c478bd9Sstevel@tonic-gate 	int i;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (is_vfork_child)
4817c478bd9Sstevel@tonic-gate 		return;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++) {
4847c478bd9Sstevel@tonic-gate 		for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
4857c478bd9Sstevel@tonic-gate 			if (!(Bp->flags & BPT_ACTIVE)) {
4867c478bd9Sstevel@tonic-gate 				if (!(Bp->flags & BPT_EXCLUDE) &&
4877c478bd9Sstevel@tonic-gate 				    Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0)
4887c478bd9Sstevel@tonic-gate 					Bp->flags |= BPT_ACTIVE;
4897c478bd9Sstevel@tonic-gate 			} else if (Paddr_to_text_map(Proc, Bp->addr) == NULL) {
4907c478bd9Sstevel@tonic-gate 				Bp->flags &= ~BPT_ACTIVE;
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate int
4977c478bd9Sstevel@tonic-gate symbol_iter(void *cd, const GElf_Sym *sym, const char *sym_name)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	struct dynpat *Dyp = cd;
5007c478bd9Sstevel@tonic-gate 	struct dynlib *Dp = Dyp->Dp;
5017c478bd9Sstevel@tonic-gate 	uintptr_t pc = sym->st_value;
5027c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
5037c478bd9Sstevel@tonic-gate 	int i;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/* ignore any undefined symbols */
5067c478bd9Sstevel@tonic-gate 	if (sym->st_shndx == SHN_UNDEF)
5077c478bd9Sstevel@tonic-gate 		return (0);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Arbitrarily omit "_start" from the executable.
5117c478bd9Sstevel@tonic-gate 	 * (Avoid indentation before main().)
5127c478bd9Sstevel@tonic-gate 	 */
5137c478bd9Sstevel@tonic-gate 	if (*Dp->prt_name == '\0' && strcmp(sym_name, "_start") == 0)
5147c478bd9Sstevel@tonic-gate 		return (0);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/*
5177c478bd9Sstevel@tonic-gate 	 * Arbitrarily omit "_rt_boot" from the dynamic linker.
5187c478bd9Sstevel@tonic-gate 	 * (Avoid indentation before main().)
5197c478bd9Sstevel@tonic-gate 	 */
5207c478bd9Sstevel@tonic-gate 	if (strcmp(Dp->match_name, "ld") == 0 &&
5217c478bd9Sstevel@tonic-gate 	    strcmp(sym_name, "_rt_boot") == 0)
5227c478bd9Sstevel@tonic-gate 		return (0);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * Arbitrarily omit any symbols whose name starts with '.'.
5267c478bd9Sstevel@tonic-gate 	 * Apparantly putting a breakpoint on .umul causes a
5277c478bd9Sstevel@tonic-gate 	 * fatal error in libthread (%y is not restored correctly
5287c478bd9Sstevel@tonic-gate 	 * when a single step is taken).  Looks like a /proc bug.
5297c478bd9Sstevel@tonic-gate 	 */
5307c478bd9Sstevel@tonic-gate 	if (*sym_name == '.')
5317c478bd9Sstevel@tonic-gate 		return (0);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/*
5347c478bd9Sstevel@tonic-gate 	 * For each pattern in the array of symbol patterns,
5357c478bd9Sstevel@tonic-gate 	 * if the pattern matches the symbol name, then
5367c478bd9Sstevel@tonic-gate 	 * create a breakpoint at the function in question.
5377c478bd9Sstevel@tonic-gate 	 */
5387c478bd9Sstevel@tonic-gate 	for (i = 0; i < Dyp->nsympat; i++) {
5397c478bd9Sstevel@tonic-gate 		if (interrupt|sigusr1)
5407c478bd9Sstevel@tonic-gate 			break;
5417c478bd9Sstevel@tonic-gate 		if (fnmatch(Dyp->sympat[i], sym_name, 0) != 0)
5427c478bd9Sstevel@tonic-gate 			continue;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		if ((Bp = create_bkpt(pc, 0, 0)) == NULL)	/* can't fail */
5457c478bd9Sstevel@tonic-gate 			return (0);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		/*
5487c478bd9Sstevel@tonic-gate 		 * New breakpoints receive a name now.
5497c478bd9Sstevel@tonic-gate 		 * For existing breakpoints, prefer the subset name if possible,
5507c478bd9Sstevel@tonic-gate 		 * else prefer the shorter name.
5517c478bd9Sstevel@tonic-gate 		 */
5527c478bd9Sstevel@tonic-gate 		if (Bp->sym_name == NULL) {
5537c478bd9Sstevel@tonic-gate 			Bp->sym_name = strdup(sym_name);
5547c478bd9Sstevel@tonic-gate 		} else if (strstr(Bp->sym_name, sym_name) != NULL ||
5557c478bd9Sstevel@tonic-gate 		    strlen(Bp->sym_name) > strlen(sym_name)) {
5567c478bd9Sstevel@tonic-gate 			free(Bp->sym_name);
5577c478bd9Sstevel@tonic-gate 			Bp->sym_name = strdup(sym_name);
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 		Bp->dyn = Dp;
5607c478bd9Sstevel@tonic-gate 		Bp->flags |= Dyp->flag;
5617c478bd9Sstevel@tonic-gate 		if (Dyp->exclude)
5627c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_EXCLUDE;
5637c478bd9Sstevel@tonic-gate 		else if (Dyp->internal || *Dp->prt_name == '\0')
5647c478bd9Sstevel@tonic-gate 			Bp->flags |= BPT_INTERNAL;
5657c478bd9Sstevel@tonic-gate 		return (0);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	return (interrupt | sigusr1);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /* For debugging only ---- */
5727c478bd9Sstevel@tonic-gate void
5737c478bd9Sstevel@tonic-gate report_htable_stats(void)
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
5767c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
5777c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
5787c478bd9Sstevel@tonic-gate 	uint_t Min = 1000000;
5797c478bd9Sstevel@tonic-gate 	uint_t Max = 0;
5807c478bd9Sstevel@tonic-gate 	uint_t Avg = 0;
5817c478bd9Sstevel@tonic-gate 	uint_t Total = 0;
5827c478bd9Sstevel@tonic-gate 	uint_t i, j;
5837c478bd9Sstevel@tonic-gate 	uint_t bucket[HASHSZ];
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL || !hflag)
5867c478bd9Sstevel@tonic-gate 		return;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	hflag = FALSE;
5897c478bd9Sstevel@tonic-gate 	(void) memset(bucket, 0, sizeof (bucket));
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++) {
5927c478bd9Sstevel@tonic-gate 		j = 0;
5937c478bd9Sstevel@tonic-gate 		for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next)
5947c478bd9Sstevel@tonic-gate 			j++;
5957c478bd9Sstevel@tonic-gate 		if (j < Min)
5967c478bd9Sstevel@tonic-gate 			Min = j;
5977c478bd9Sstevel@tonic-gate 		if (j > Max)
5987c478bd9Sstevel@tonic-gate 			Max = j;
5997c478bd9Sstevel@tonic-gate 		if (j < HASHSZ)
6007c478bd9Sstevel@tonic-gate 			bucket[j]++;
6017c478bd9Sstevel@tonic-gate 		Total += j;
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	Avg = (Total + HASHSZ / 2) / HASHSZ;
6047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "truss hash table statistics --------\n");
6057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "    Total = %u\n", Total);
6067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "      Min = %u\n", Min);
6077c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "      Max = %u\n", Max);
6087c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "      Avg = %u\n", Avg);
6097c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++)
6107c478bd9Sstevel@tonic-gate 		if (bucket[i])
6117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "    %3u buckets of size %d\n",
6127c478bd9Sstevel@tonic-gate 				bucket[i], i);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "truss-detected stacks --------\n");
6157c478bd9Sstevel@tonic-gate 	for (Stk = callstack; Stk != NULL; Stk = Stk->next) {
6167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6177c478bd9Sstevel@tonic-gate 			"    base = 0x%.8lx  end = 0x%.8lx  size = %ld\n",
6187c478bd9Sstevel@tonic-gate 			(ulong_t)Stk->stkbase,
6197c478bd9Sstevel@tonic-gate 			(ulong_t)Stk->stkend,
6207c478bd9Sstevel@tonic-gate 			(ulong_t)(Stk->stkend - Stk->stkbase));
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "primary unix stack --------\n");
6237c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6247c478bd9Sstevel@tonic-gate 		"    base = 0x%.8lx  end = 0x%.8lx  size = %ld\n",
6257c478bd9Sstevel@tonic-gate 		(ulong_t)Psp->pr_stkbase,
6267c478bd9Sstevel@tonic-gate 		(ulong_t)(Psp->pr_stkbase + Psp->pr_stksize),
6277c478bd9Sstevel@tonic-gate 		(ulong_t)Psp->pr_stksize);
6287c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "nthr_create = %u\n", nthr_create);
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate void
6327c478bd9Sstevel@tonic-gate make_lwp_stack(const lwpstatus_t *Lsp, prmap_t *Pmap, int nmap)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
6357c478bd9Sstevel@tonic-gate 	uintptr_t sp = Lsp->pr_reg[R_SP];
6367c478bd9Sstevel@tonic-gate 	id_t lwpid = Lsp->pr_lwpid;
6377c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
6387c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
6397c478bd9Sstevel@tonic-gate 	td_thrinfo_t thrinfo;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64)
6427c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/* check to see if we already have this stack */
6457c478bd9Sstevel@tonic-gate 	if (sp == 0)
6467c478bd9Sstevel@tonic-gate 		return;
6477c478bd9Sstevel@tonic-gate 	for (Stk = callstack; Stk != NULL; Stk = Stk->next)
6487c478bd9Sstevel@tonic-gate 		if (sp >= Stk->stkbase && sp < Stk->stkend)
6497c478bd9Sstevel@tonic-gate 			return;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	Stk = my_malloc(sizeof (struct callstack), NULL);
6527c478bd9Sstevel@tonic-gate 	Stk->next = callstack;
6537c478bd9Sstevel@tonic-gate 	callstack = Stk;
6547c478bd9Sstevel@tonic-gate 	nstack++;
6557c478bd9Sstevel@tonic-gate 	Stk->tref = 0;
6567c478bd9Sstevel@tonic-gate 	Stk->tid = 0;
6577c478bd9Sstevel@tonic-gate 	Stk->nthr_create = 0;
6587c478bd9Sstevel@tonic-gate 	Stk->ncall = 0;
6597c478bd9Sstevel@tonic-gate 	Stk->maxcall = DEF_MAXCALL;
6607c478bd9Sstevel@tonic-gate 	Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/* primary stack */
6637c478bd9Sstevel@tonic-gate 	if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
6647c478bd9Sstevel@tonic-gate 		Stk->stkbase = Psp->pr_stkbase;
6657c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Psp->pr_stksize;
6667c478bd9Sstevel@tonic-gate 		return;
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/* alternate stack */
6707c478bd9Sstevel@tonic-gate 	if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) &&
6717c478bd9Sstevel@tonic-gate 	    sp >= (uintptr_t)Lsp->pr_altstack.ss_sp &&
6727c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)Lsp->pr_altstack.ss_sp
6737c478bd9Sstevel@tonic-gate 	    + Lsp->pr_altstack.ss_size) {
6747c478bd9Sstevel@tonic-gate 		Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp;
6757c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size;
6767c478bd9Sstevel@tonic-gate 		return;
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/* thread stacks? */
6807c478bd9Sstevel@tonic-gate 	if (Thr_agent != NULL &&
6817c478bd9Sstevel@tonic-gate 	    td_ta_map_lwp2thr(Thr_agent, lwpid, &th) == TD_OK &&
6827c478bd9Sstevel@tonic-gate 	    td_thr_get_info(&th, &thrinfo) == TD_OK &&
6837c478bd9Sstevel@tonic-gate 	    sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
6847c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)thrinfo.ti_stkbase) {
6857c478bd9Sstevel@tonic-gate 		/* The bloody fools got this backwards! */
6867c478bd9Sstevel@tonic-gate 		Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
6877c478bd9Sstevel@tonic-gate 		Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
6887c478bd9Sstevel@tonic-gate 		return;
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/* last chance -- try the raw memory map */
6927c478bd9Sstevel@tonic-gate 	for (; nmap; nmap--, Pmap++) {
6937c478bd9Sstevel@tonic-gate 		if (sp >= Pmap->pr_vaddr &&
6947c478bd9Sstevel@tonic-gate 		    sp < Pmap->pr_vaddr + Pmap->pr_size) {
6957c478bd9Sstevel@tonic-gate 			Stk->stkbase = Pmap->pr_vaddr;
6967c478bd9Sstevel@tonic-gate 			Stk->stkend = Pmap->pr_vaddr + Pmap->pr_size;
6977c478bd9Sstevel@tonic-gate 			return;
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	callstack = Stk->next;
7027c478bd9Sstevel@tonic-gate 	nstack--;
7037c478bd9Sstevel@tonic-gate 	free(Stk->stack);
7047c478bd9Sstevel@tonic-gate 	free(Stk);
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate void
7087c478bd9Sstevel@tonic-gate make_thr_stack(const td_thrhandle_t *Thp, prgregset_t reg)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
7117c478bd9Sstevel@tonic-gate 	td_thrinfo_t thrinfo;
7127c478bd9Sstevel@tonic-gate 	uintptr_t sp = reg[R_SP];
7137c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64)
7167c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/* check to see if we already have this stack */
7197c478bd9Sstevel@tonic-gate 	if (sp == 0)
7207c478bd9Sstevel@tonic-gate 		return;
7217c478bd9Sstevel@tonic-gate 	for (Stk = callstack; Stk != NULL; Stk = Stk->next)
7227c478bd9Sstevel@tonic-gate 		if (sp >= Stk->stkbase && sp < Stk->stkend)
7237c478bd9Sstevel@tonic-gate 			return;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	Stk = my_malloc(sizeof (struct callstack), NULL);
7267c478bd9Sstevel@tonic-gate 	Stk->next = callstack;
7277c478bd9Sstevel@tonic-gate 	callstack = Stk;
7287c478bd9Sstevel@tonic-gate 	nstack++;
7297c478bd9Sstevel@tonic-gate 	Stk->tref = 0;
7307c478bd9Sstevel@tonic-gate 	Stk->tid = 0;
7317c478bd9Sstevel@tonic-gate 	Stk->nthr_create = 0;
7327c478bd9Sstevel@tonic-gate 	Stk->ncall = 0;
7337c478bd9Sstevel@tonic-gate 	Stk->maxcall = DEF_MAXCALL;
7347c478bd9Sstevel@tonic-gate 	Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	/* primary stack */
7377c478bd9Sstevel@tonic-gate 	if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
7387c478bd9Sstevel@tonic-gate 		Stk->stkbase = Psp->pr_stkbase;
7397c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Psp->pr_stksize;
7407c478bd9Sstevel@tonic-gate 		return;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	if (td_thr_get_info(Thp, &thrinfo) == TD_OK &&
7447c478bd9Sstevel@tonic-gate 	    sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
7457c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)thrinfo.ti_stkbase) {
7467c478bd9Sstevel@tonic-gate 		/* The bloody fools got this backwards! */
7477c478bd9Sstevel@tonic-gate 		Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
7487c478bd9Sstevel@tonic-gate 		Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
7497c478bd9Sstevel@tonic-gate 		return;
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	callstack = Stk->next;
7537c478bd9Sstevel@tonic-gate 	nstack--;
7547c478bd9Sstevel@tonic-gate 	free(Stk->stack);
7557c478bd9Sstevel@tonic-gate 	free(Stk);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate struct callstack *
7597c478bd9Sstevel@tonic-gate find_lwp_stack(uintptr_t sp)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
7627c478bd9Sstevel@tonic-gate 	char mapfile[64];
7637c478bd9Sstevel@tonic-gate 	int mapfd;
7647c478bd9Sstevel@tonic-gate 	struct stat statb;
7657c478bd9Sstevel@tonic-gate 	prmap_t *Pmap = NULL;
7667c478bd9Sstevel@tonic-gate 	prmap_t *pmap = NULL;
7677c478bd9Sstevel@tonic-gate 	int nmap = 0;
7687c478bd9Sstevel@tonic-gate 	struct callstack *Stk = NULL;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	/*
7717c478bd9Sstevel@tonic-gate 	 * Get the address space map.
7727c478bd9Sstevel@tonic-gate 	 */
7737c478bd9Sstevel@tonic-gate 	(void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid);
7747c478bd9Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
7757c478bd9Sstevel@tonic-gate 	    fstat(mapfd, &statb) != 0 ||
7767c478bd9Sstevel@tonic-gate 	    statb.st_size < sizeof (prmap_t) ||
7777c478bd9Sstevel@tonic-gate 	    (Pmap = my_malloc(statb.st_size, NULL)) == NULL ||
7787c478bd9Sstevel@tonic-gate 	    (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
7797c478bd9Sstevel@tonic-gate 	    (nmap /= sizeof (prmap_t)) == 0) {
7807c478bd9Sstevel@tonic-gate 		if (Pmap != NULL)
7817c478bd9Sstevel@tonic-gate 			free(Pmap);
7827c478bd9Sstevel@tonic-gate 		if (mapfd >= 0)
7837c478bd9Sstevel@tonic-gate 			(void) close(mapfd);
7847c478bd9Sstevel@tonic-gate 		return (NULL);
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 	(void) close(mapfd);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	for (pmap = Pmap; nmap--; pmap++) {
7897c478bd9Sstevel@tonic-gate 		if (sp >= pmap->pr_vaddr &&
7907c478bd9Sstevel@tonic-gate 		    sp < pmap->pr_vaddr + pmap->pr_size) {
7917c478bd9Sstevel@tonic-gate 			Stk = my_malloc(sizeof (struct callstack), NULL);
7927c478bd9Sstevel@tonic-gate 			Stk->next = callstack;
7937c478bd9Sstevel@tonic-gate 			callstack = Stk;
7947c478bd9Sstevel@tonic-gate 			nstack++;
7957c478bd9Sstevel@tonic-gate 			Stk->stkbase = pmap->pr_vaddr;
7967c478bd9Sstevel@tonic-gate 			Stk->stkend = pmap->pr_vaddr + pmap->pr_size;
7977c478bd9Sstevel@tonic-gate 			Stk->tref = 0;
7987c478bd9Sstevel@tonic-gate 			Stk->tid = 0;
7997c478bd9Sstevel@tonic-gate 			Stk->nthr_create = 0;
8007c478bd9Sstevel@tonic-gate 			Stk->ncall = 0;
8017c478bd9Sstevel@tonic-gate 			Stk->maxcall = DEF_MAXCALL;
8027c478bd9Sstevel@tonic-gate 			Stk->stack = my_malloc(
8037c478bd9Sstevel@tonic-gate 				DEF_MAXCALL * sizeof (*Stk->stack), NULL);
8047c478bd9Sstevel@tonic-gate 			break;
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	free(Pmap);
8097c478bd9Sstevel@tonic-gate 	return (Stk);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate struct callstack *
8137c478bd9Sstevel@tonic-gate find_stack(uintptr_t sp)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	const pstatus_t *Psp = Pstatus(Proc);
8167c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
8177c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
8187c478bd9Sstevel@tonic-gate 	id_t lwpid = Lsp->pr_lwpid;
8197c478bd9Sstevel@tonic-gate #if defined(__sparc)
8207c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[R_G7];
8217c478bd9Sstevel@tonic-gate #elif defined(__amd64)
8227c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[REG_FS];
8237c478bd9Sstevel@tonic-gate #elif defined(__i386)
8247c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[GS];
8257c478bd9Sstevel@tonic-gate #endif
8267c478bd9Sstevel@tonic-gate 	struct callstack *Stk = NULL;
8277c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
8287c478bd9Sstevel@tonic-gate 	td_thrinfo_t thrinfo;
8297c478bd9Sstevel@tonic-gate 	td_err_e error;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/* primary stack */
8327c478bd9Sstevel@tonic-gate 	if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
8337c478bd9Sstevel@tonic-gate 		Stk = my_malloc(sizeof (struct callstack), NULL);
8347c478bd9Sstevel@tonic-gate 		Stk->next = callstack;
8357c478bd9Sstevel@tonic-gate 		callstack = Stk;
8367c478bd9Sstevel@tonic-gate 		nstack++;
8377c478bd9Sstevel@tonic-gate 		Stk->stkbase = Psp->pr_stkbase;
8387c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Psp->pr_stksize;
8397c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
8407c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
8417c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
8427c478bd9Sstevel@tonic-gate 		Stk->ncall = 0;
8437c478bd9Sstevel@tonic-gate 		Stk->maxcall = DEF_MAXCALL;
8447c478bd9Sstevel@tonic-gate 		Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
8457c478bd9Sstevel@tonic-gate 			NULL);
8467c478bd9Sstevel@tonic-gate 		return (Stk);
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	/* alternate stack */
8507c478bd9Sstevel@tonic-gate 	if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) &&
8517c478bd9Sstevel@tonic-gate 	    sp >= (uintptr_t)Lsp->pr_altstack.ss_sp &&
8527c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)Lsp->pr_altstack.ss_sp
8537c478bd9Sstevel@tonic-gate 	    + Lsp->pr_altstack.ss_size) {
8547c478bd9Sstevel@tonic-gate 		Stk = my_malloc(sizeof (struct callstack), NULL);
8557c478bd9Sstevel@tonic-gate 		Stk->next = callstack;
8567c478bd9Sstevel@tonic-gate 		callstack = Stk;
8577c478bd9Sstevel@tonic-gate 		nstack++;
8587c478bd9Sstevel@tonic-gate 		Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp;
8597c478bd9Sstevel@tonic-gate 		Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size;
8607c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
8617c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
8627c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
8637c478bd9Sstevel@tonic-gate 		Stk->ncall = 0;
8647c478bd9Sstevel@tonic-gate 		Stk->maxcall = DEF_MAXCALL;
8657c478bd9Sstevel@tonic-gate 		Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
8667c478bd9Sstevel@tonic-gate 			NULL);
8677c478bd9Sstevel@tonic-gate 		return (Stk);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL)
8717c478bd9Sstevel@tonic-gate 		return (find_lwp_stack(sp));
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/* thread stacks? */
8747c478bd9Sstevel@tonic-gate 	if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) {
8757c478bd9Sstevel@tonic-gate 		if (hflag)
8767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8777c478bd9Sstevel@tonic-gate 				"cannot get thread handle for "
8787c478bd9Sstevel@tonic-gate 				"lwp#%d, error=%d, tref=0x%.8lx\n",
8797c478bd9Sstevel@tonic-gate 				(int)lwpid, error, (long)tref);
8807c478bd9Sstevel@tonic-gate 		return (NULL);
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) {
8847c478bd9Sstevel@tonic-gate 		if (hflag)
8857c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8867c478bd9Sstevel@tonic-gate 				"cannot get thread info for "
8877c478bd9Sstevel@tonic-gate 				"lwp#%d, error=%d, tref=0x%.8lx\n",
8887c478bd9Sstevel@tonic-gate 				(int)lwpid, error, (long)tref);
8897c478bd9Sstevel@tonic-gate 		return (NULL);
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	if (sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
8937c478bd9Sstevel@tonic-gate 	    sp < (uintptr_t)thrinfo.ti_stkbase) {
8947c478bd9Sstevel@tonic-gate 		Stk = my_malloc(sizeof (struct callstack), NULL);
8957c478bd9Sstevel@tonic-gate 		Stk->next = callstack;
8967c478bd9Sstevel@tonic-gate 		callstack = Stk;
8977c478bd9Sstevel@tonic-gate 		nstack++;
8987c478bd9Sstevel@tonic-gate 		/* The bloody fools got this backwards! */
8997c478bd9Sstevel@tonic-gate 		Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
9007c478bd9Sstevel@tonic-gate 		Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
9017c478bd9Sstevel@tonic-gate 		Stk->tref = tref;
9027c478bd9Sstevel@tonic-gate 		Stk->tid = thrinfo.ti_tid;
9037c478bd9Sstevel@tonic-gate 		Stk->nthr_create = nthr_create;
9047c478bd9Sstevel@tonic-gate 		Stk->ncall = 0;
9057c478bd9Sstevel@tonic-gate 		Stk->maxcall = DEF_MAXCALL;
9067c478bd9Sstevel@tonic-gate 		Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
9077c478bd9Sstevel@tonic-gate 			NULL);
9087c478bd9Sstevel@tonic-gate 		return (Stk);
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	/* stack bounds failure -- complain bitterly */
9127c478bd9Sstevel@tonic-gate 	if (hflag) {
9137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9147c478bd9Sstevel@tonic-gate 			"sp not within thread stack: "
9157c478bd9Sstevel@tonic-gate 			"sp=0x%.8lx stkbase=0x%.8lx stkend=0x%.8lx\n",
9167c478bd9Sstevel@tonic-gate 			(ulong_t)sp,
9177c478bd9Sstevel@tonic-gate 			/* The bloody fools got this backwards! */
9187c478bd9Sstevel@tonic-gate 			(ulong_t)thrinfo.ti_stkbase - thrinfo.ti_stksize,
9197c478bd9Sstevel@tonic-gate 			(ulong_t)thrinfo.ti_stkbase);
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	return (NULL);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate void
9267c478bd9Sstevel@tonic-gate get_tid(struct callstack *Stk)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
9297c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
9307c478bd9Sstevel@tonic-gate 	id_t lwpid = Lsp->pr_lwpid;
9317c478bd9Sstevel@tonic-gate #if defined(__sparc)
9327c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[R_G7];
9337c478bd9Sstevel@tonic-gate #elif defined(__amd64)
9347c478bd9Sstevel@tonic-gate 	prgreg_t tref = (data_model == PR_MODEL_LP64) ?
9357c478bd9Sstevel@tonic-gate 	    Lsp->pr_reg[REG_FS] : Lsp->pr_reg[REG_GS];
9367c478bd9Sstevel@tonic-gate #elif defined(__i386)
9377c478bd9Sstevel@tonic-gate 	prgreg_t tref = Lsp->pr_reg[GS];
9387c478bd9Sstevel@tonic-gate #endif
9397c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
9407c478bd9Sstevel@tonic-gate 	td_thrinfo_t thrinfo;
9417c478bd9Sstevel@tonic-gate 	td_err_e error;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	if (Thr_agent == NULL) {
9447c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
9457c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
9467c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
9477c478bd9Sstevel@tonic-gate 		return;
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	/*
9517c478bd9Sstevel@tonic-gate 	 * Shortcut here --
9527c478bd9Sstevel@tonic-gate 	 * If we have a matching tref and no new threads have
9537c478bd9Sstevel@tonic-gate 	 * been created since the last time we encountered this
9547c478bd9Sstevel@tonic-gate 	 * stack, then we don't have to go through the overhead
9557c478bd9Sstevel@tonic-gate 	 * of calling td_ta_map_lwp2thr() to get the thread-id.
9567c478bd9Sstevel@tonic-gate 	 */
9577c478bd9Sstevel@tonic-gate 	if (tref == Stk->tref && Stk->nthr_create == nthr_create)
9587c478bd9Sstevel@tonic-gate 		return;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) {
9617c478bd9Sstevel@tonic-gate 		if (hflag)
9627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9637c478bd9Sstevel@tonic-gate 				"cannot get thread handle for "
9647c478bd9Sstevel@tonic-gate 				"lwp#%d, error=%d, tref=0x%.8lx\n",
9657c478bd9Sstevel@tonic-gate 				(int)lwpid, error, (long)tref);
9667c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
9677c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
9687c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
9697c478bd9Sstevel@tonic-gate 	} else if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) {
9707c478bd9Sstevel@tonic-gate 		if (hflag)
9717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9727c478bd9Sstevel@tonic-gate 				"cannot get thread info for "
9737c478bd9Sstevel@tonic-gate 				"lwp#%d, error=%d, tref=0x%.8lx\n",
9747c478bd9Sstevel@tonic-gate 				(int)lwpid, error, (long)tref);
9757c478bd9Sstevel@tonic-gate 		Stk->tref = 0;
9767c478bd9Sstevel@tonic-gate 		Stk->tid = 0;
9777c478bd9Sstevel@tonic-gate 		Stk->nthr_create = 0;
9787c478bd9Sstevel@tonic-gate 	} else {
9797c478bd9Sstevel@tonic-gate 		Stk->tref = tref;
9807c478bd9Sstevel@tonic-gate 		Stk->tid = thrinfo.ti_tid;
9817c478bd9Sstevel@tonic-gate 		Stk->nthr_create = nthr_create;
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate struct callstack *
9867c478bd9Sstevel@tonic-gate callstack_info(uintptr_t sp, uintptr_t fp, int makeid)
9877c478bd9Sstevel@tonic-gate {
9887c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
9897c478bd9Sstevel@tonic-gate 	uintptr_t trash;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	if (sp == 0 ||
9927c478bd9Sstevel@tonic-gate 	    Pread(Proc, &trash, sizeof (trash), sp) != sizeof (trash))
9937c478bd9Sstevel@tonic-gate 		return (NULL);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	for (Stk = callstack; Stk != NULL; Stk = Stk->next)
9967c478bd9Sstevel@tonic-gate 		if (sp >= Stk->stkbase && sp < Stk->stkend)
9977c478bd9Sstevel@tonic-gate 			break;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	/*
10007c478bd9Sstevel@tonic-gate 	 * If we didn't find the stack, do it the hard way.
10017c478bd9Sstevel@tonic-gate 	 */
10027c478bd9Sstevel@tonic-gate 	if (Stk == NULL) {
10037c478bd9Sstevel@tonic-gate 		uintptr_t stkbase = sp;
10047c478bd9Sstevel@tonic-gate 		uintptr_t stkend;
10057c478bd9Sstevel@tonic-gate 		uint_t minsize;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
10087c478bd9Sstevel@tonic-gate #ifdef _LP64
10097c478bd9Sstevel@tonic-gate 		if (data_model == PR_MODEL_LP64)
10107c478bd9Sstevel@tonic-gate 			minsize = 2 * sizeof (uintptr_t);	/* fp + pc */
10117c478bd9Sstevel@tonic-gate 		else
10127c478bd9Sstevel@tonic-gate #endif
10137c478bd9Sstevel@tonic-gate 			minsize = 2 * sizeof (uint32_t);
10147c478bd9Sstevel@tonic-gate #else
10157c478bd9Sstevel@tonic-gate #ifdef _LP64
10167c478bd9Sstevel@tonic-gate 		if (data_model != PR_MODEL_LP64)
10177c478bd9Sstevel@tonic-gate 			minsize = SA32(MINFRAME32);
10187c478bd9Sstevel@tonic-gate 		else
10197c478bd9Sstevel@tonic-gate 			minsize = SA64(MINFRAME64);
10207c478bd9Sstevel@tonic-gate #else
10217c478bd9Sstevel@tonic-gate 		minsize = SA(MINFRAME);
10227c478bd9Sstevel@tonic-gate #endif
10237c478bd9Sstevel@tonic-gate #endif	/* i386 */
10247c478bd9Sstevel@tonic-gate 		stkend = sp + minsize;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 		while (Stk == NULL && fp != 0 && fp >= sp) {
10277c478bd9Sstevel@tonic-gate 			stkend = fp + minsize;
10287c478bd9Sstevel@tonic-gate 			for (Stk = callstack; Stk != NULL; Stk = Stk->next)
10297c478bd9Sstevel@tonic-gate 				if ((fp >= Stk->stkbase && fp < Stk->stkend) ||
10307c478bd9Sstevel@tonic-gate 				    (stkend > Stk->stkbase &&
10317c478bd9Sstevel@tonic-gate 				    stkend <= Stk->stkend))
10327c478bd9Sstevel@tonic-gate 					break;
10337c478bd9Sstevel@tonic-gate 			if (Stk == NULL)
10347c478bd9Sstevel@tonic-gate 				fp = previous_fp(fp, NULL);
10357c478bd9Sstevel@tonic-gate 		}
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		if (Stk != NULL)	/* the stack grew */
10387c478bd9Sstevel@tonic-gate 			Stk->stkbase = stkbase;
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	if (Stk == NULL && makeid)	/* new stack */
10427c478bd9Sstevel@tonic-gate 		Stk = find_stack(sp);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	if (Stk == NULL)
10457c478bd9Sstevel@tonic-gate 		return (NULL);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	/*
10487c478bd9Sstevel@tonic-gate 	 * Ensure that there is room for at least one more entry.
10497c478bd9Sstevel@tonic-gate 	 */
10507c478bd9Sstevel@tonic-gate 	if (Stk->ncall == Stk->maxcall) {
10517c478bd9Sstevel@tonic-gate 		Stk->maxcall *= 2;
10527c478bd9Sstevel@tonic-gate 		Stk->stack = my_realloc(Stk->stack,
10537c478bd9Sstevel@tonic-gate 		    Stk->maxcall * sizeof (*Stk->stack), NULL);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	if (makeid)
10577c478bd9Sstevel@tonic-gate 		get_tid(Stk);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	return (Stk);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate  * Reset the breakpoint information (called on successful exec()).
10647c478bd9Sstevel@tonic-gate  */
10657c478bd9Sstevel@tonic-gate void
10667c478bd9Sstevel@tonic-gate reset_breakpoints(void)
10677c478bd9Sstevel@tonic-gate {
10687c478bd9Sstevel@tonic-gate 	struct dynlib *Dp;
10697c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
10707c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
10717c478bd9Sstevel@tonic-gate 	int i;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL)
10747c478bd9Sstevel@tonic-gate 		return;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	/* destroy all previous dynamic library information */
10777c478bd9Sstevel@tonic-gate 	while ((Dp = Dyn) != NULL) {
10787c478bd9Sstevel@tonic-gate 		Dyn = Dp->next;
10797c478bd9Sstevel@tonic-gate 		free(Dp->lib_name);
10807c478bd9Sstevel@tonic-gate 		free(Dp->match_name);
10817c478bd9Sstevel@tonic-gate 		free(Dp->prt_name);
10827c478bd9Sstevel@tonic-gate 		free(Dp);
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	/* destroy all previous breakpoint trap information */
10867c478bd9Sstevel@tonic-gate 	if (bpt_hashtable != NULL) {
10877c478bd9Sstevel@tonic-gate 		for (i = 0; i < HASHSZ; i++) {
10887c478bd9Sstevel@tonic-gate 			while ((Bp = bpt_hashtable[i]) != NULL) {
10897c478bd9Sstevel@tonic-gate 				bpt_hashtable[i] = Bp->next;
10907c478bd9Sstevel@tonic-gate 				if (Bp->sym_name)
10917c478bd9Sstevel@tonic-gate 					free(Bp->sym_name);
10927c478bd9Sstevel@tonic-gate 				free(Bp);
10937c478bd9Sstevel@tonic-gate 			}
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	/* destroy all the callstack information */
10987c478bd9Sstevel@tonic-gate 	while ((Stk = callstack) != NULL) {
10997c478bd9Sstevel@tonic-gate 		callstack = Stk->next;
11007c478bd9Sstevel@tonic-gate 		free(Stk->stack);
11017c478bd9Sstevel@tonic-gate 		free(Stk);
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	/* we are not a multi-threaded process anymore */
11057c478bd9Sstevel@tonic-gate 	if (Thr_agent != NULL)
11067c478bd9Sstevel@tonic-gate 		(void) td_ta_delete(Thr_agent);
11077c478bd9Sstevel@tonic-gate 	Thr_agent = NULL;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	/* tell libproc to clear out its mapping information */
11107c478bd9Sstevel@tonic-gate 	Preset_maps(Proc);
11117c478bd9Sstevel@tonic-gate 	Rdb_agent = NULL;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/* Reestablish the symbols from the executable */
11147c478bd9Sstevel@tonic-gate 	(void) establish_breakpoints();
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate /*
11187c478bd9Sstevel@tonic-gate  * Clear breakpoints from the process (called before Prelease()).
11197c478bd9Sstevel@tonic-gate  * Don't actually destroy the breakpoint table;
11207c478bd9Sstevel@tonic-gate  * threads currently fielding breakpoints will need it.
11217c478bd9Sstevel@tonic-gate  */
11227c478bd9Sstevel@tonic-gate void
11237c478bd9Sstevel@tonic-gate clear_breakpoints(void)
11247c478bd9Sstevel@tonic-gate {
11257c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
11267c478bd9Sstevel@tonic-gate 	int i;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL)
11297c478bd9Sstevel@tonic-gate 		return;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	/*
11327c478bd9Sstevel@tonic-gate 	 * Change all breakpoint traps back to normal instructions.
11337c478bd9Sstevel@tonic-gate 	 * We attempt to remove a breakpoint from every address which
11347c478bd9Sstevel@tonic-gate 	 * may have ever contained a breakpoint to protect our victims.
11357c478bd9Sstevel@tonic-gate 	 */
11367c478bd9Sstevel@tonic-gate 	report_htable_stats();	/* report stats first */
11377c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++) {
11387c478bd9Sstevel@tonic-gate 		for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
11397c478bd9Sstevel@tonic-gate 			if (Bp->flags & BPT_ACTIVE)
11407c478bd9Sstevel@tonic-gate 				(void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
11417c478bd9Sstevel@tonic-gate 			Bp->flags &= ~BPT_ACTIVE;
11427c478bd9Sstevel@tonic-gate 		}
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	if (Thr_agent != NULL) {
11467c478bd9Sstevel@tonic-gate 		td_thr_events_t events;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		td_event_emptyset(&events);
11497c478bd9Sstevel@tonic-gate 		(void) td_ta_set_event(Thr_agent, &events);
11507c478bd9Sstevel@tonic-gate 		(void) td_ta_delete(Thr_agent);
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 	Thr_agent = NULL;
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate /*
11567c478bd9Sstevel@tonic-gate  * Reestablish the breakpoint traps in the process.
11577c478bd9Sstevel@tonic-gate  * Called after resuming from a vfork() in the parent.
11587c478bd9Sstevel@tonic-gate  */
11597c478bd9Sstevel@tonic-gate void
11607c478bd9Sstevel@tonic-gate reestablish_traps(void)
11617c478bd9Sstevel@tonic-gate {
11627c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
11637c478bd9Sstevel@tonic-gate 	ulong_t instr;
11647c478bd9Sstevel@tonic-gate 	int i;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL || is_vfork_child)
11677c478bd9Sstevel@tonic-gate 		return;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHSZ; i++) {
11707c478bd9Sstevel@tonic-gate 		for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
11717c478bd9Sstevel@tonic-gate 			if ((Bp->flags & BPT_ACTIVE) &&
11727c478bd9Sstevel@tonic-gate 			    Psetbkpt(Proc, Bp->addr, &instr) != 0)
11737c478bd9Sstevel@tonic-gate 				Bp->flags &= ~BPT_ACTIVE;
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate void
11797c478bd9Sstevel@tonic-gate show_function_call(private_t *pri,
11807c478bd9Sstevel@tonic-gate 	struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	long arg[8];
11837c478bd9Sstevel@tonic-gate 	int narg;
11847c478bd9Sstevel@tonic-gate 	int i;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	narg = get_arguments(arg);
11877c478bd9Sstevel@tonic-gate 	make_pname(pri, (Stk != NULL)? Stk->tid : 0);
11887c478bd9Sstevel@tonic-gate 	putpname(pri);
11897c478bd9Sstevel@tonic-gate 	timestamp(pri);
11907c478bd9Sstevel@tonic-gate 	if (Stk != NULL) {
11917c478bd9Sstevel@tonic-gate 		for (i = 1; i < Stk->ncall; i++) {
11927c478bd9Sstevel@tonic-gate 			(void) fputc(' ', stdout);
11937c478bd9Sstevel@tonic-gate 			(void) fputc(' ', stdout);
11947c478bd9Sstevel@tonic-gate 		}
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate 	(void) printf("-> %s%s(", Dp->prt_name, Bp->sym_name);
11977c478bd9Sstevel@tonic-gate 	for (i = 0; i < narg; i++) {
11987c478bd9Sstevel@tonic-gate 		(void) printf("0x%lx", arg[i]);
11997c478bd9Sstevel@tonic-gate 		if (i < narg-1) {
12007c478bd9Sstevel@tonic-gate 			(void) fputc(',', stdout);
12017c478bd9Sstevel@tonic-gate 			(void) fputc(' ', stdout);
12027c478bd9Sstevel@tonic-gate 		}
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 	(void) printf(")\n");
12057c478bd9Sstevel@tonic-gate 	Flush();
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate /* ARGSUSED */
12097c478bd9Sstevel@tonic-gate void
12107c478bd9Sstevel@tonic-gate show_function_return(private_t *pri, long rval, int stret,
12117c478bd9Sstevel@tonic-gate 	struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp)
12127c478bd9Sstevel@tonic-gate {
12137c478bd9Sstevel@tonic-gate 	int i;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	make_pname(pri, Stk->tid);
12167c478bd9Sstevel@tonic-gate 	putpname(pri);
12177c478bd9Sstevel@tonic-gate 	timestamp(pri);
12187c478bd9Sstevel@tonic-gate 	for (i = 0; i < Stk->ncall; i++) {
12197c478bd9Sstevel@tonic-gate 		(void) fputc(' ', stdout);
12207c478bd9Sstevel@tonic-gate 		(void) fputc(' ', stdout);
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 	(void) printf("<- %s%s() = ", Dp->prt_name, Bp->sym_name);
12237c478bd9Sstevel@tonic-gate 	if (stret) {
12247c478bd9Sstevel@tonic-gate 		(void) printf("struct return\n");
12257c478bd9Sstevel@tonic-gate 	} else if (data_model == PR_MODEL_LP64) {
12267c478bd9Sstevel@tonic-gate 		if (rval >= (64 * 1024) || -rval >= (64 * 1024))
12277c478bd9Sstevel@tonic-gate 			(void) printf("0x%lx\n", rval);
12287c478bd9Sstevel@tonic-gate 		else
12297c478bd9Sstevel@tonic-gate 			(void) printf("%ld\n", rval);
12307c478bd9Sstevel@tonic-gate 	} else {
12317c478bd9Sstevel@tonic-gate 		int rval32 = (int)rval;
12327c478bd9Sstevel@tonic-gate 		if (rval32 >= (64 * 1024) || -rval32 >= (64 * 1024))
12337c478bd9Sstevel@tonic-gate 			(void) printf("0x%x\n", rval32);
12347c478bd9Sstevel@tonic-gate 		else
12357c478bd9Sstevel@tonic-gate 			(void) printf("%d\n", rval32);
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 	Flush();
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate /*
12417c478bd9Sstevel@tonic-gate  * Called to deal with function-call tracing.
12427c478bd9Sstevel@tonic-gate  * Return 0 on normal success, 1 to indicate a BPT_HANG success,
12437c478bd9Sstevel@tonic-gate  * and -1 on failure (not tracing functions or unknown breakpoint).
12447c478bd9Sstevel@tonic-gate  */
12457c478bd9Sstevel@tonic-gate int
12467c478bd9Sstevel@tonic-gate function_trace(private_t *pri, int first, int clear, int dotrace)
12477c478bd9Sstevel@tonic-gate {
12487c478bd9Sstevel@tonic-gate 	struct ps_lwphandle *Lwp = pri->Lwp;
12497c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
12507c478bd9Sstevel@tonic-gate 	uintptr_t pc = Lsp->pr_reg[R_PC];
12517c478bd9Sstevel@tonic-gate 	uintptr_t sp = Lsp->pr_reg[R_SP];
12527c478bd9Sstevel@tonic-gate 	uintptr_t fp = Lsp->pr_reg[R_FP];
12537c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
12547c478bd9Sstevel@tonic-gate 	struct dynlib *Dp;
12557c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
12567c478bd9Sstevel@tonic-gate 	ulong_t instr;
12577c478bd9Sstevel@tonic-gate 	int active;
12587c478bd9Sstevel@tonic-gate 	int rval = 0;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	if (Dynpat == NULL)
12617c478bd9Sstevel@tonic-gate 		return (-1);
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64) {
12647c478bd9Sstevel@tonic-gate 		pc = (uint32_t)pc;
12657c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
12667c478bd9Sstevel@tonic-gate 		fp = (uint32_t)fp;
12677c478bd9Sstevel@tonic-gate 	}
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	if ((Bp = get_bkpt(pc)) == NULL) {
12707c478bd9Sstevel@tonic-gate 		if (hflag)
12717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12727c478bd9Sstevel@tonic-gate 				"function_trace(): "
12737c478bd9Sstevel@tonic-gate 				"cannot find breakpoint for pc: 0x%.8lx\n",
12747c478bd9Sstevel@tonic-gate 				(ulong_t)pc);
12757c478bd9Sstevel@tonic-gate 		return (-1);
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	if ((Bp->flags & (BPT_PREINIT|BPT_POSTINIT|BPT_DLACTIVITY)) && !clear) {
12797c478bd9Sstevel@tonic-gate 		rd_event_msg_t event_msg;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		if (hflag) {
12827c478bd9Sstevel@tonic-gate 			if (Bp->flags & BPT_PREINIT)
12837c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "function_trace(): "
12847c478bd9Sstevel@tonic-gate 					"RD_PREINIT breakpoint\n");
12857c478bd9Sstevel@tonic-gate 			if (Bp->flags & BPT_POSTINIT)
12867c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "function_trace(): "
12877c478bd9Sstevel@tonic-gate 					"RD_POSTINIT breakpoint\n");
12887c478bd9Sstevel@tonic-gate 			if (Bp->flags & BPT_DLACTIVITY)
12897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "function_trace(): "
12907c478bd9Sstevel@tonic-gate 					"RD_DLACTIVITY breakpoint\n");
12917c478bd9Sstevel@tonic-gate 		}
12927c478bd9Sstevel@tonic-gate 		if (rd_event_getmsg(Rdb_agent, &event_msg) == RD_OK) {
12937c478bd9Sstevel@tonic-gate 			if (event_msg.type == RD_DLACTIVITY) {
1294*6fced65dSraf 				switch (event_msg.u.state) {
1295*6fced65dSraf 				case RD_CONSISTENT:
12967c478bd9Sstevel@tonic-gate 					establish_breakpoints();
1297*6fced65dSraf 					break;
1298*6fced65dSraf 				case RD_ADD:
12997c478bd9Sstevel@tonic-gate 					not_consist = TRUE;	/* kludge */
13007c478bd9Sstevel@tonic-gate 					establish_breakpoints();
13017c478bd9Sstevel@tonic-gate 					not_consist = FALSE;
1302*6fced65dSraf 					break;
1303*6fced65dSraf 				case RD_DELETE:
1304*6fced65dSraf 					delete_library = TRUE;
1305*6fced65dSraf 					break;
1306*6fced65dSraf 				default:
1307*6fced65dSraf 					break;
13087c478bd9Sstevel@tonic-gate 				}
13097c478bd9Sstevel@tonic-gate 			}
13107c478bd9Sstevel@tonic-gate 			if (hflag) {
13117c478bd9Sstevel@tonic-gate 				const char *et;
13127c478bd9Sstevel@tonic-gate 				char buf[32];
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 				switch (event_msg.type) {
13157c478bd9Sstevel@tonic-gate 				case RD_NONE:
13167c478bd9Sstevel@tonic-gate 					et = "RD_NONE";
13177c478bd9Sstevel@tonic-gate 					break;
13187c478bd9Sstevel@tonic-gate 				case RD_PREINIT:
13197c478bd9Sstevel@tonic-gate 					et = "RD_PREINIT";
13207c478bd9Sstevel@tonic-gate 					break;
13217c478bd9Sstevel@tonic-gate 				case RD_POSTINIT:
13227c478bd9Sstevel@tonic-gate 					et = "RD_POSTINIT";
13237c478bd9Sstevel@tonic-gate 					break;
13247c478bd9Sstevel@tonic-gate 				case RD_DLACTIVITY:
13257c478bd9Sstevel@tonic-gate 					et = "RD_DLACTIVITY";
13267c478bd9Sstevel@tonic-gate 					break;
13277c478bd9Sstevel@tonic-gate 				default:
13287c478bd9Sstevel@tonic-gate 					(void) sprintf(buf, "0x%x",
13297c478bd9Sstevel@tonic-gate 						event_msg.type);
13307c478bd9Sstevel@tonic-gate 					et = buf;
13317c478bd9Sstevel@tonic-gate 					break;
13327c478bd9Sstevel@tonic-gate 				}
13337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13347c478bd9Sstevel@tonic-gate 					"event_msg.type = %s ", et);
13357c478bd9Sstevel@tonic-gate 				switch (event_msg.u.state) {
13367c478bd9Sstevel@tonic-gate 				case RD_NOSTATE:
13377c478bd9Sstevel@tonic-gate 					et = "RD_NOSTATE";
13387c478bd9Sstevel@tonic-gate 					break;
13397c478bd9Sstevel@tonic-gate 				case RD_CONSISTENT:
13407c478bd9Sstevel@tonic-gate 					et = "RD_CONSISTENT";
13417c478bd9Sstevel@tonic-gate 					break;
13427c478bd9Sstevel@tonic-gate 				case RD_ADD:
13437c478bd9Sstevel@tonic-gate 					et = "RD_ADD";
13447c478bd9Sstevel@tonic-gate 					break;
13457c478bd9Sstevel@tonic-gate 				case RD_DELETE:
13467c478bd9Sstevel@tonic-gate 					et = "RD_DELETE";
13477c478bd9Sstevel@tonic-gate 					break;
13487c478bd9Sstevel@tonic-gate 				default:
13497c478bd9Sstevel@tonic-gate 					(void) sprintf(buf, "0x%x",
13507c478bd9Sstevel@tonic-gate 						event_msg.u.state);
13517c478bd9Sstevel@tonic-gate 					et = buf;
13527c478bd9Sstevel@tonic-gate 					break;
13537c478bd9Sstevel@tonic-gate 				}
13547c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13557c478bd9Sstevel@tonic-gate 					"event_msg.u.state = %s\n", et);
13567c478bd9Sstevel@tonic-gate 			}
13577c478bd9Sstevel@tonic-gate 		}
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	if ((Bp->flags & BPT_TD_CREATE) && !clear) {
13617c478bd9Sstevel@tonic-gate 		nthr_create++;
13627c478bd9Sstevel@tonic-gate 		if (hflag)
13637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "function_trace(): "
13647c478bd9Sstevel@tonic-gate 				"BPT_TD_CREATE breakpoint\n");
13657c478bd9Sstevel@tonic-gate 		/* we don't care about the event message */
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	Dp = Bp->dyn;
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	if (dotrace) {
13717c478bd9Sstevel@tonic-gate 		if ((Stk = callstack_info(sp, fp, 1)) == NULL) {
13727c478bd9Sstevel@tonic-gate 			if (Dp != NULL && !clear) {
13737c478bd9Sstevel@tonic-gate 				if (cflag) {
13747c478bd9Sstevel@tonic-gate 					add_fcall(fcall_tbl, Dp->prt_name,
13757c478bd9Sstevel@tonic-gate 					    Bp->sym_name, (unsigned long)1);
13767c478bd9Sstevel@tonic-gate 				}
13777c478bd9Sstevel@tonic-gate 				else
13787c478bd9Sstevel@tonic-gate 					show_function_call(pri, NULL, Dp, Bp);
13797c478bd9Sstevel@tonic-gate 				if ((Bp->flags & BPT_HANG) && !first)
13807c478bd9Sstevel@tonic-gate 					rval = 1;
13817c478bd9Sstevel@tonic-gate 			}
13827c478bd9Sstevel@tonic-gate 		} else if (!clear) {
13837c478bd9Sstevel@tonic-gate 			if (Dp != NULL) {
13847c478bd9Sstevel@tonic-gate 				function_entry(pri, Bp, Stk);
13857c478bd9Sstevel@tonic-gate 				if ((Bp->flags & BPT_HANG) && !first)
13867c478bd9Sstevel@tonic-gate 					rval = 1;
13877c478bd9Sstevel@tonic-gate 			} else {
13887c478bd9Sstevel@tonic-gate 				function_return(pri, Stk);
13897c478bd9Sstevel@tonic-gate 			}
13907c478bd9Sstevel@tonic-gate 		}
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	/*
13947c478bd9Sstevel@tonic-gate 	 * Single-step the traced instruction. Since it's possible that
13957c478bd9Sstevel@tonic-gate 	 * another thread has deactivated this breakpoint, we indicate
13967c478bd9Sstevel@tonic-gate 	 * that we have reactivated it by virtue of executing it.
13977c478bd9Sstevel@tonic-gate 	 *
13987c478bd9Sstevel@tonic-gate 	 * To avoid a deadlock with some other thread in the process
13997c478bd9Sstevel@tonic-gate 	 * performing a fork() or a thr_suspend() operation, we must
14007c478bd9Sstevel@tonic-gate 	 * drop and later reacquire truss_lock.  Some fancy dancing here.
14017c478bd9Sstevel@tonic-gate 	 */
14027c478bd9Sstevel@tonic-gate 	active = (Bp->flags & BPT_ACTIVE);
14037c478bd9Sstevel@tonic-gate 	Bp->flags |= BPT_ACTIVE;
14047c478bd9Sstevel@tonic-gate 	instr = Bp->instr;
14057c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&truss_lock);
14067c478bd9Sstevel@tonic-gate 	(void) Lxecbkpt(Lwp, instr);
14077c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&truss_lock);
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if (rval || clear) {	/* leave process stopped and abandoned */
14107c478bd9Sstevel@tonic-gate #if defined(__i386)
14117c478bd9Sstevel@tonic-gate 		/*
14127c478bd9Sstevel@tonic-gate 		 * Leave it stopped in a state that a stack trace is reasonable.
14137c478bd9Sstevel@tonic-gate 		 */
14147c478bd9Sstevel@tonic-gate 		/* XX64 needs to be updated for amd64 & gcc */
14157c478bd9Sstevel@tonic-gate 		if (rval && instr == 0x55) {	/* pushl %ebp */
14167c478bd9Sstevel@tonic-gate 			/* step it over the movl %esp,%ebp */
14177c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&truss_lock);
14187c478bd9Sstevel@tonic-gate 			(void) Lsetrun(Lwp, 0, PRCFAULT|PRSTEP);
14197c478bd9Sstevel@tonic-gate 			/* we're wrapping up; wait one second at most */
14207c478bd9Sstevel@tonic-gate 			(void) Lwait(Lwp, MILLISEC);
14217c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&truss_lock);
14227c478bd9Sstevel@tonic-gate 		}
14237c478bd9Sstevel@tonic-gate #endif
14247c478bd9Sstevel@tonic-gate 		if (get_bkpt(pc) != Bp)
14257c478bd9Sstevel@tonic-gate 			abend("function_trace: lost breakpoint", NULL);
14267c478bd9Sstevel@tonic-gate 		(void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
14277c478bd9Sstevel@tonic-gate 		Bp->flags &= ~BPT_ACTIVE;
14287c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&truss_lock);
14297c478bd9Sstevel@tonic-gate 		(void) Lsetrun(Lwp, 0, PRCFAULT|PRSTOP);
14307c478bd9Sstevel@tonic-gate 		/* we're wrapping up; wait one second at most */
14317c478bd9Sstevel@tonic-gate 		(void) Lwait(Lwp, MILLISEC);
14327c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&truss_lock);
14337c478bd9Sstevel@tonic-gate 	} else {
14347c478bd9Sstevel@tonic-gate 		if (get_bkpt(pc) != Bp)
14357c478bd9Sstevel@tonic-gate 			abend("function_trace: lost breakpoint", NULL);
14367c478bd9Sstevel@tonic-gate 		if (!active || !(Bp->flags & BPT_ACTIVE)) {
14377c478bd9Sstevel@tonic-gate 			(void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
14387c478bd9Sstevel@tonic-gate 			Bp->flags &= ~BPT_ACTIVE;
14397c478bd9Sstevel@tonic-gate 		}
14407c478bd9Sstevel@tonic-gate 	}
14417c478bd9Sstevel@tonic-gate 	return (rval);
14427c478bd9Sstevel@tonic-gate }
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate void
14457c478bd9Sstevel@tonic-gate function_entry(private_t *pri, struct bkpt *Bp, struct callstack *Stk)
14467c478bd9Sstevel@tonic-gate {
14477c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
14487c478bd9Sstevel@tonic-gate 	uintptr_t sp = Lsp->pr_reg[R_SP];
14497c478bd9Sstevel@tonic-gate 	uintptr_t rpc = get_return_address(&sp);
14507c478bd9Sstevel@tonic-gate 	struct dynlib *Dp = Bp->dyn;
14517c478bd9Sstevel@tonic-gate 	int oldframe = FALSE;
14527c478bd9Sstevel@tonic-gate 	int i;
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate #ifdef _LP64
14557c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64) {
14567c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
14577c478bd9Sstevel@tonic-gate 		rpc = (uint32_t)rpc;
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate #endif
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	/*
14627c478bd9Sstevel@tonic-gate 	 * If the sp is not within the stack bounds, forget it.
14637c478bd9Sstevel@tonic-gate 	 * If the symbol's 'internal' flag is false,
14647c478bd9Sstevel@tonic-gate 	 * don't report internal calls within the library.
14657c478bd9Sstevel@tonic-gate 	 */
14667c478bd9Sstevel@tonic-gate 	if (!(sp >= Stk->stkbase && sp < Stk->stkend) ||
14677c478bd9Sstevel@tonic-gate 	    (!(Bp->flags & BPT_INTERNAL) &&
14687c478bd9Sstevel@tonic-gate 	    rpc >= Dp->base && rpc < Dp->base + Dp->size))
14697c478bd9Sstevel@tonic-gate 		return;
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	for (i = 0; i < Stk->ncall; i++) {
14727c478bd9Sstevel@tonic-gate 		if (sp >= Stk->stack[i].sp) {
14737c478bd9Sstevel@tonic-gate 			Stk->ncall = i;
14747c478bd9Sstevel@tonic-gate 			if (sp == Stk->stack[i].sp)
14757c478bd9Sstevel@tonic-gate 				oldframe = TRUE;
14767c478bd9Sstevel@tonic-gate 			break;
14777c478bd9Sstevel@tonic-gate 		}
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	/*
14817c478bd9Sstevel@tonic-gate 	 * Breakpoints for function returns are set here
14827c478bd9Sstevel@tonic-gate 	 * If we're counting function calls, there is no need to set
14837c478bd9Sstevel@tonic-gate 	 * a breakpoint upon return
14847c478bd9Sstevel@tonic-gate 	 */
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	if (!oldframe && !cflag) {
14877c478bd9Sstevel@tonic-gate 		(void) create_bkpt(rpc, 1, 1); /* may or may not be set */
14887c478bd9Sstevel@tonic-gate 		Stk->stack[Stk->ncall].sp = sp;	/* record it anyeay */
14897c478bd9Sstevel@tonic-gate 		Stk->stack[Stk->ncall].pc = rpc;
14907c478bd9Sstevel@tonic-gate 		Stk->stack[Stk->ncall].fcn = Bp;
14917c478bd9Sstevel@tonic-gate 	}
14927c478bd9Sstevel@tonic-gate 	Stk->ncall++;
14937c478bd9Sstevel@tonic-gate 	if (cflag) {
14947c478bd9Sstevel@tonic-gate 		add_fcall(fcall_tbl, Dp->prt_name, Bp->sym_name,
14957c478bd9Sstevel@tonic-gate 		    (unsigned long)1);
14967c478bd9Sstevel@tonic-gate 	} else {
14977c478bd9Sstevel@tonic-gate 		show_function_call(pri, Stk, Dp, Bp);
14987c478bd9Sstevel@tonic-gate 	}
14997c478bd9Sstevel@tonic-gate }
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate /*
15027c478bd9Sstevel@tonic-gate  * We are here because we hit an unnamed breakpoint.
15037c478bd9Sstevel@tonic-gate  * Attempt to match this up with a return pc on the stack
15047c478bd9Sstevel@tonic-gate  * and report the function return.
15057c478bd9Sstevel@tonic-gate  */
15067c478bd9Sstevel@tonic-gate void
15077c478bd9Sstevel@tonic-gate function_return(private_t *pri, struct callstack *Stk)
15087c478bd9Sstevel@tonic-gate {
15097c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
15107c478bd9Sstevel@tonic-gate 	uintptr_t sp = Lsp->pr_reg[R_SP];
15117c478bd9Sstevel@tonic-gate 	uintptr_t fp = Lsp->pr_reg[R_FP];
15127c478bd9Sstevel@tonic-gate 	int i;
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate #ifdef _LP64
15157c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64) {
15167c478bd9Sstevel@tonic-gate 		sp = (uint32_t)sp;
15177c478bd9Sstevel@tonic-gate 		fp = (uint32_t)fp;
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate #endif
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	if (fp < sp + 8)
15227c478bd9Sstevel@tonic-gate 		fp = sp + 8;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	for (i = Stk->ncall - 1; i >= 0; i--) {
15257c478bd9Sstevel@tonic-gate 		if (sp <= Stk->stack[i].sp && fp > Stk->stack[i].sp) {
15267c478bd9Sstevel@tonic-gate 			Stk->ncall = i;
15277c478bd9Sstevel@tonic-gate 			break;
15287c478bd9Sstevel@tonic-gate 		}
15297c478bd9Sstevel@tonic-gate 	}
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
15327c478bd9Sstevel@tonic-gate 	if (i < 0) {
15337c478bd9Sstevel@tonic-gate 		/* probably __mul64() or friends -- try harder */
15347c478bd9Sstevel@tonic-gate 		int j;
15357c478bd9Sstevel@tonic-gate 		for (j = 0; i < 0 && j < 8; j++) {	/* up to 8 args */
15367c478bd9Sstevel@tonic-gate 			sp -= 4;
15377c478bd9Sstevel@tonic-gate 			for (i = Stk->ncall - 1; i >= 0; i--) {
15387c478bd9Sstevel@tonic-gate 				if (sp <= Stk->stack[i].sp &&
15397c478bd9Sstevel@tonic-gate 				    fp > Stk->stack[i].sp) {
15407c478bd9Sstevel@tonic-gate 					Stk->ncall = i;
15417c478bd9Sstevel@tonic-gate 					break;
15427c478bd9Sstevel@tonic-gate 				}
15437c478bd9Sstevel@tonic-gate 			}
15447c478bd9Sstevel@tonic-gate 		}
15457c478bd9Sstevel@tonic-gate 	}
15467c478bd9Sstevel@tonic-gate #endif
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	if ((i >= 0) && (!cflag)) {
15497c478bd9Sstevel@tonic-gate 		show_function_return(pri, Lsp->pr_reg[R_R0], 0,
15507c478bd9Sstevel@tonic-gate 			Stk, Stk->stack[i].fcn->dyn, Stk->stack[i].fcn);
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate }
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate #if defined(__sparc)
15557c478bd9Sstevel@tonic-gate #define	FPADJUST	0
15567c478bd9Sstevel@tonic-gate #elif defined(__amd64)
15577c478bd9Sstevel@tonic-gate #define	FPADJUST	8
15587c478bd9Sstevel@tonic-gate #elif defined(__i386)
15597c478bd9Sstevel@tonic-gate #define	FPADJUST	4
15607c478bd9Sstevel@tonic-gate #endif
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate void
15637c478bd9Sstevel@tonic-gate trap_one_stack(prgregset_t reg)
15647c478bd9Sstevel@tonic-gate {
15657c478bd9Sstevel@tonic-gate 	struct dynlib *Dp;
15667c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
15677c478bd9Sstevel@tonic-gate 	struct callstack *Stk;
15687c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
15697c478bd9Sstevel@tonic-gate 	char sym_name[32];
15707c478bd9Sstevel@tonic-gate 	uintptr_t sp = reg[R_SP];
15717c478bd9Sstevel@tonic-gate 	uintptr_t pc = reg[R_PC];
15727c478bd9Sstevel@tonic-gate 	uintptr_t fp;
15737c478bd9Sstevel@tonic-gate 	uintptr_t rpc;
15747c478bd9Sstevel@tonic-gate 	uint_t nframe = 0;
15757c478bd9Sstevel@tonic-gate 	uint_t maxframe = 8;
15767c478bd9Sstevel@tonic-gate 	struct {
15777c478bd9Sstevel@tonic-gate 		uintptr_t sp;		/* %sp within called function */
15787c478bd9Sstevel@tonic-gate 		uintptr_t pc;		/* %pc within called function */
15797c478bd9Sstevel@tonic-gate 		uintptr_t rsp;		/* the return sp */
15807c478bd9Sstevel@tonic-gate 		uintptr_t rpc;		/* the return pc */
15817c478bd9Sstevel@tonic-gate 	} *frame = my_malloc(maxframe * sizeof (*frame), NULL);
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	/*
15847c478bd9Sstevel@tonic-gate 	 * Gather stack frames bottom to top.
15857c478bd9Sstevel@tonic-gate 	 */
15867c478bd9Sstevel@tonic-gate 	while (sp != 0) {
15877c478bd9Sstevel@tonic-gate 		fp = sp;	/* remember higest non-null sp */
15887c478bd9Sstevel@tonic-gate 		frame[nframe].sp = sp;
15897c478bd9Sstevel@tonic-gate 		frame[nframe].pc = pc;
15907c478bd9Sstevel@tonic-gate 		sp = previous_fp(sp, &pc);
15917c478bd9Sstevel@tonic-gate 		frame[nframe].rsp = sp;
15927c478bd9Sstevel@tonic-gate 		frame[nframe].rpc = pc;
15937c478bd9Sstevel@tonic-gate 		if (++nframe == maxframe) {
15947c478bd9Sstevel@tonic-gate 			maxframe *= 2;
15957c478bd9Sstevel@tonic-gate 			frame = my_realloc(frame, maxframe * sizeof (*frame),
15967c478bd9Sstevel@tonic-gate 				NULL);
15977c478bd9Sstevel@tonic-gate 		}
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	/*
16017c478bd9Sstevel@tonic-gate 	 * Scan for function return breakpoints top to bottom.
16027c478bd9Sstevel@tonic-gate 	 */
16037c478bd9Sstevel@tonic-gate 	while (nframe--) {
16047c478bd9Sstevel@tonic-gate 		/* lookup the called function in the symbol tables */
16057c478bd9Sstevel@tonic-gate 		if (Plookup_by_addr(Proc, frame[nframe].pc, sym_name,
16067c478bd9Sstevel@tonic-gate 		    sizeof (sym_name), &sym) != 0)
16077c478bd9Sstevel@tonic-gate 			continue;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 		pc = sym.st_value;	/* entry point of the function */
16107c478bd9Sstevel@tonic-gate 		rpc = frame[nframe].rpc;	/* caller's return pc */
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 		/* lookup the function in the breakpoint table */
16137c478bd9Sstevel@tonic-gate 		if ((Bp = get_bkpt(pc)) == NULL || (Dp = Bp->dyn) == NULL)
16147c478bd9Sstevel@tonic-gate 			continue;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 		if (!(Bp->flags & BPT_INTERNAL) &&
16177c478bd9Sstevel@tonic-gate 		    rpc >= Dp->base && rpc < Dp->base + Dp->size)
16187c478bd9Sstevel@tonic-gate 			continue;
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 		sp = frame[nframe].rsp + FPADJUST;  /* %sp at time of call */
16217c478bd9Sstevel@tonic-gate 		if ((Stk = callstack_info(sp, fp, 0)) == NULL)
16227c478bd9Sstevel@tonic-gate 			continue;	/* can't happen? */
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 		if (create_bkpt(rpc, 1, 1) != NULL) {
16257c478bd9Sstevel@tonic-gate 			Stk->stack[Stk->ncall].sp = sp;
16267c478bd9Sstevel@tonic-gate 			Stk->stack[Stk->ncall].pc = rpc;
16277c478bd9Sstevel@tonic-gate 			Stk->stack[Stk->ncall].fcn = Bp;
16287c478bd9Sstevel@tonic-gate 			Stk->ncall++;
16297c478bd9Sstevel@tonic-gate 		}
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	free(frame);
16337c478bd9Sstevel@tonic-gate }
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate int
16367c478bd9Sstevel@tonic-gate lwp_stack_traps(void *cd, const lwpstatus_t *Lsp)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	ph_map_t *ph_map = (ph_map_t *)cd;
16397c478bd9Sstevel@tonic-gate 	prgregset_t reg;
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	(void) memcpy(reg, Lsp->pr_reg, sizeof (prgregset_t));
16427c478bd9Sstevel@tonic-gate 	make_lwp_stack(Lsp, ph_map->pmap, ph_map->nmap);
16437c478bd9Sstevel@tonic-gate 	trap_one_stack(reg);
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	return (interrupt | sigusr1);
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate /* ARGSUSED */
16497c478bd9Sstevel@tonic-gate int
16507c478bd9Sstevel@tonic-gate thr_stack_traps(const td_thrhandle_t *Thp, void *cd)
16517c478bd9Sstevel@tonic-gate {
16527c478bd9Sstevel@tonic-gate 	prgregset_t reg;
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	/*
16557c478bd9Sstevel@tonic-gate 	 * We have already dealt with all the lwps.
16567c478bd9Sstevel@tonic-gate 	 * We only care about unbound threads here (TD_PARTIALREG).
16577c478bd9Sstevel@tonic-gate 	 */
16587c478bd9Sstevel@tonic-gate 	if (td_thr_getgregs(Thp, reg) != TD_PARTIALREG)
16597c478bd9Sstevel@tonic-gate 		return (0);
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	make_thr_stack(Thp, reg);
16627c478bd9Sstevel@tonic-gate 	trap_one_stack(reg);
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	return (interrupt | sigusr1);
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate #if defined(__sparc)
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate uintptr_t
16707c478bd9Sstevel@tonic-gate previous_fp(uintptr_t sp, uintptr_t *rpc)
16717c478bd9Sstevel@tonic-gate {
16727c478bd9Sstevel@tonic-gate 	uintptr_t fp = 0;
16737c478bd9Sstevel@tonic-gate 	uintptr_t pc = 0;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate #ifdef _LP64
16767c478bd9Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
16777c478bd9Sstevel@tonic-gate 		struct rwindow64 rwin;
16787c478bd9Sstevel@tonic-gate 		if (Pread(Proc, &rwin, sizeof (rwin), sp + STACK_BIAS)
16797c478bd9Sstevel@tonic-gate 		    == sizeof (rwin)) {
16807c478bd9Sstevel@tonic-gate 			fp = (uintptr_t)rwin.rw_fp;
16817c478bd9Sstevel@tonic-gate 			pc = (uintptr_t)rwin.rw_rtn;
16827c478bd9Sstevel@tonic-gate 		}
16837c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
16847c478bd9Sstevel@tonic-gate 		    Pread(Proc, &rwin, sizeof (rwin), fp + STACK_BIAS)
16857c478bd9Sstevel@tonic-gate 		    != sizeof (rwin))
16867c478bd9Sstevel@tonic-gate 			fp = pc = 0;
16877c478bd9Sstevel@tonic-gate 	} else {
16887c478bd9Sstevel@tonic-gate 		struct rwindow32 rwin;
16897c478bd9Sstevel@tonic-gate #else	/* _LP64 */
16907c478bd9Sstevel@tonic-gate 		struct rwindow rwin;
16917c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
16927c478bd9Sstevel@tonic-gate 		if (Pread(Proc, &rwin, sizeof (rwin), sp) == sizeof (rwin)) {
16937c478bd9Sstevel@tonic-gate 			fp = (uint32_t)rwin.rw_fp;
16947c478bd9Sstevel@tonic-gate 			pc = (uint32_t)rwin.rw_rtn;
16957c478bd9Sstevel@tonic-gate 		}
16967c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
16977c478bd9Sstevel@tonic-gate 		    Pread(Proc, &rwin, sizeof (rwin), fp) != sizeof (rwin))
16987c478bd9Sstevel@tonic-gate 			fp = pc = 0;
16997c478bd9Sstevel@tonic-gate #ifdef _LP64
17007c478bd9Sstevel@tonic-gate 	}
17017c478bd9Sstevel@tonic-gate #endif
17027c478bd9Sstevel@tonic-gate 	if (rpc)
17037c478bd9Sstevel@tonic-gate 		*rpc = pc;
17047c478bd9Sstevel@tonic-gate 	return (fp);
17057c478bd9Sstevel@tonic-gate }
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate /* ARGSUSED */
17087c478bd9Sstevel@tonic-gate uintptr_t
17097c478bd9Sstevel@tonic-gate get_return_address(uintptr_t *psp)
17107c478bd9Sstevel@tonic-gate {
17117c478bd9Sstevel@tonic-gate 	instr_t inst;
17127c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
17137c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
17147c478bd9Sstevel@tonic-gate 	uintptr_t rpc;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	rpc = (uintptr_t)Lsp->pr_reg[R_O7] + 8;
17177c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64)
17187c478bd9Sstevel@tonic-gate 		rpc = (uint32_t)rpc;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	/* check for structure return (bletch!) */
17217c478bd9Sstevel@tonic-gate 	if (Pread(Proc, &inst, sizeof (inst), rpc) == sizeof (inst) &&
17227c478bd9Sstevel@tonic-gate 	    inst < 0x1000)
17237c478bd9Sstevel@tonic-gate 		rpc += sizeof (instr_t);
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	return (rpc);
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate int
17297c478bd9Sstevel@tonic-gate get_arguments(long *argp)
17307c478bd9Sstevel@tonic-gate {
17317c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
17327c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
17337c478bd9Sstevel@tonic-gate 	int i;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	if (data_model != PR_MODEL_LP64)
17367c478bd9Sstevel@tonic-gate 		for (i = 0; i < 4; i++)
17377c478bd9Sstevel@tonic-gate 			argp[i] = (uint_t)Lsp->pr_reg[R_O0+i];
17387c478bd9Sstevel@tonic-gate 	else
17397c478bd9Sstevel@tonic-gate 		for (i = 0; i < 4; i++)
17407c478bd9Sstevel@tonic-gate 			argp[i] = (long)Lsp->pr_reg[R_O0+i];
17417c478bd9Sstevel@tonic-gate 	return (4);
17427c478bd9Sstevel@tonic-gate }
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate #endif	/* __sparc */
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate uintptr_t
17497c478bd9Sstevel@tonic-gate previous_fp(uintptr_t fp, uintptr_t *rpc)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	uintptr_t frame[2];
17527c478bd9Sstevel@tonic-gate 	uintptr_t trash[2];
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	if (Pread(Proc, frame, sizeof (frame), fp) != sizeof (frame) ||
17557c478bd9Sstevel@tonic-gate 	    (frame[0] != 0 &&
17567c478bd9Sstevel@tonic-gate 	    Pread(Proc, trash, sizeof (trash), frame[0]) != sizeof (trash)))
17577c478bd9Sstevel@tonic-gate 		frame[0] = frame[1] = 0;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	if (rpc)
17607c478bd9Sstevel@tonic-gate 		*rpc = frame[1];
17617c478bd9Sstevel@tonic-gate 	return (frame[0]);
17627c478bd9Sstevel@tonic-gate }
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate #endif
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate #if defined(__amd64) || defined(__i386)
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate /*
17697c478bd9Sstevel@tonic-gate  * Examine the instruction at the return location of a function call
17707c478bd9Sstevel@tonic-gate  * and return the byte count by which the stack is adjusted on return.
17717c478bd9Sstevel@tonic-gate  * It the instruction at the return location is an addl, as expected,
17727c478bd9Sstevel@tonic-gate  * then adjust the return pc by the size of that instruction so that
17737c478bd9Sstevel@tonic-gate  * we will place the return breakpoint on the following instruction.
17747c478bd9Sstevel@tonic-gate  * This allows programs that interrogate their own stacks and record
17757c478bd9Sstevel@tonic-gate  * function calls and arguments to work correctly even while we interfere.
17767c478bd9Sstevel@tonic-gate  * Return the count on success, -1 on failure.
17777c478bd9Sstevel@tonic-gate  */
17787c478bd9Sstevel@tonic-gate int
17797c478bd9Sstevel@tonic-gate return_count32(uint32_t *ppc)
17807c478bd9Sstevel@tonic-gate {
17817c478bd9Sstevel@tonic-gate 	uintptr_t pc = *ppc;
17827c478bd9Sstevel@tonic-gate 	struct bkpt *Bp;
17837c478bd9Sstevel@tonic-gate 	int count;
17847c478bd9Sstevel@tonic-gate 	uchar_t instr[6];	/* instruction at pc */
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	if ((count = Pread(Proc, instr, sizeof (instr), pc)) < 0)
17877c478bd9Sstevel@tonic-gate 		return (-1);
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	/* find the replaced instruction at pc (if any) */
17907c478bd9Sstevel@tonic-gate 	if ((Bp = get_bkpt(pc)) != NULL && (Bp->flags & BPT_ACTIVE))
17917c478bd9Sstevel@tonic-gate 		instr[0] = (uchar_t)Bp->instr;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	if (count != sizeof (instr) &&
17947c478bd9Sstevel@tonic-gate 	    (count < 3 || instr[0] != 0x83))
17957c478bd9Sstevel@tonic-gate 		return (-1);
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	/*
17987c478bd9Sstevel@tonic-gate 	 * A bit of disassembly of the instruction is required here.
17997c478bd9Sstevel@tonic-gate 	 */
18007c478bd9Sstevel@tonic-gate 	if (instr[1] != 0xc4) {	/* not an addl mumble,%esp inctruction */
18017c478bd9Sstevel@tonic-gate 		count = 0;
18027c478bd9Sstevel@tonic-gate 	} else if (instr[0] == 0x81) {	/* count is a longword */
18037c478bd9Sstevel@tonic-gate 		count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24);
18047c478bd9Sstevel@tonic-gate 		*ppc += 6;
18057c478bd9Sstevel@tonic-gate 	} else if (instr[0] == 0x83) {	/* count is a byte */
18067c478bd9Sstevel@tonic-gate 		count = instr[2];
18077c478bd9Sstevel@tonic-gate 		*ppc += 3;
18087c478bd9Sstevel@tonic-gate 	} else {		/* not an addl inctruction */
18097c478bd9Sstevel@tonic-gate 		count = 0;
18107c478bd9Sstevel@tonic-gate 	}
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	return (count);
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate uintptr_t
18167c478bd9Sstevel@tonic-gate get_return_address32(uintptr_t *psp)
18177c478bd9Sstevel@tonic-gate {
18187c478bd9Sstevel@tonic-gate 	uint32_t sp = *psp;
18197c478bd9Sstevel@tonic-gate 	uint32_t rpc;
18207c478bd9Sstevel@tonic-gate 	int count;
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	*psp += 4;	/* account for popping the stack on return */
18237c478bd9Sstevel@tonic-gate 	if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc))
18247c478bd9Sstevel@tonic-gate 		return (0);
18257c478bd9Sstevel@tonic-gate 	if ((count = return_count32(&rpc)) < 0)
18267c478bd9Sstevel@tonic-gate 		count = 0;
18277c478bd9Sstevel@tonic-gate 	*psp += count;		/* expected sp on return */
18287c478bd9Sstevel@tonic-gate 	return (rpc);
18297c478bd9Sstevel@tonic-gate }
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate uintptr_t
18327c478bd9Sstevel@tonic-gate get_return_address(uintptr_t *psp)
18337c478bd9Sstevel@tonic-gate {
18347c478bd9Sstevel@tonic-gate #ifdef _LP64
18357c478bd9Sstevel@tonic-gate 	uintptr_t rpc;
18367c478bd9Sstevel@tonic-gate 	uintptr_t sp = *psp;
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
18397c478bd9Sstevel@tonic-gate 		if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc))
18407c478bd9Sstevel@tonic-gate 			return (0);
18417c478bd9Sstevel@tonic-gate 		/*
18427c478bd9Sstevel@tonic-gate 		 * Ignore arguments pushed on the stack.  See comments in
18437c478bd9Sstevel@tonic-gate 		 * get_arguments().
18447c478bd9Sstevel@tonic-gate 		 */
18457c478bd9Sstevel@tonic-gate 		return (rpc);
18467c478bd9Sstevel@tonic-gate 	} else
18477c478bd9Sstevel@tonic-gate #endif
18487c478bd9Sstevel@tonic-gate 		return (get_return_address32(psp));
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate int
18537c478bd9Sstevel@tonic-gate get_arguments32(long *argp)
18547c478bd9Sstevel@tonic-gate {
18557c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
18567c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
18577c478bd9Sstevel@tonic-gate 	uint32_t frame[5];	/* return pc + 4 args */
18587c478bd9Sstevel@tonic-gate 	int narg;
18597c478bd9Sstevel@tonic-gate 	int count;
18607c478bd9Sstevel@tonic-gate 	int i;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	narg = Pread(Proc, frame, sizeof (frame),
18637c478bd9Sstevel@tonic-gate 		(uintptr_t)Lsp->pr_reg[R_SP]);
18647c478bd9Sstevel@tonic-gate 	narg -= sizeof (greg32_t);
18657c478bd9Sstevel@tonic-gate 	if (narg <= 0)
18667c478bd9Sstevel@tonic-gate 		return (0);
18677c478bd9Sstevel@tonic-gate 	narg /= sizeof (greg32_t); /* no more than 4 */
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	/*
18707c478bd9Sstevel@tonic-gate 	 * Given the return PC, determine the number of arguments.
18717c478bd9Sstevel@tonic-gate 	 */
18727c478bd9Sstevel@tonic-gate 	if ((count = return_count32(&frame[0])) < 0)
18737c478bd9Sstevel@tonic-gate 		narg = 0;
18747c478bd9Sstevel@tonic-gate 	else {
18757c478bd9Sstevel@tonic-gate 		count /= sizeof (greg32_t);
18767c478bd9Sstevel@tonic-gate 		if (narg > count)
18777c478bd9Sstevel@tonic-gate 			narg = count;
18787c478bd9Sstevel@tonic-gate 	}
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	for (i = 0; i < narg; i++)
18817c478bd9Sstevel@tonic-gate 		argp[i] = (long)frame[i+1];
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	return (narg);
18847c478bd9Sstevel@tonic-gate }
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate int
18877c478bd9Sstevel@tonic-gate get_arguments(long *argp)
18887c478bd9Sstevel@tonic-gate {
18897c478bd9Sstevel@tonic-gate #ifdef _LP64
18907c478bd9Sstevel@tonic-gate 	private_t *pri = get_private();
18917c478bd9Sstevel@tonic-gate 	const lwpstatus_t *Lsp = pri->lwpstat;
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	if (data_model == PR_MODEL_LP64) {
18947c478bd9Sstevel@tonic-gate 		/*
18957c478bd9Sstevel@tonic-gate 		 * On amd64, we do not know how many arguments are passed to
18967c478bd9Sstevel@tonic-gate 		 * each function.  While it may be possible to detect if we
18977c478bd9Sstevel@tonic-gate 		 * have more than 6 arguments, it is of marginal value.
18987c478bd9Sstevel@tonic-gate 		 * Instead, assume that we always have 6 arguments, which are
18997c478bd9Sstevel@tonic-gate 		 * passed via registers.
19007c478bd9Sstevel@tonic-gate 		 */
19017c478bd9Sstevel@tonic-gate 		argp[0] = Lsp->pr_reg[REG_RDI];
19027c478bd9Sstevel@tonic-gate 		argp[1] = Lsp->pr_reg[REG_RSI];
19037c478bd9Sstevel@tonic-gate 		argp[2] = Lsp->pr_reg[REG_RDX];
19047c478bd9Sstevel@tonic-gate 		argp[3] = Lsp->pr_reg[REG_RCX];
19057c478bd9Sstevel@tonic-gate 		argp[4] = Lsp->pr_reg[REG_R8];
19067c478bd9Sstevel@tonic-gate 		argp[5] = Lsp->pr_reg[REG_R9];
19077c478bd9Sstevel@tonic-gate 		return (6);
19087c478bd9Sstevel@tonic-gate 	} else
19097c478bd9Sstevel@tonic-gate #endif
19107c478bd9Sstevel@tonic-gate 		return (get_arguments32(argp));
19117c478bd9Sstevel@tonic-gate }
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate #endif	/* __amd64 || __i386 */
1914