10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52192Sraf * Common Development and Distribution License (the "License"). 62192Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 212192Sraf 220Sstevel@tonic-gate /* 237330SRoger.Faulkner@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #define _SYSCALL32 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <unistd.h> 320Sstevel@tonic-gate #include <ctype.h> 330Sstevel@tonic-gate #include <string.h> 340Sstevel@tonic-gate #include <memory.h> 350Sstevel@tonic-gate #include <errno.h> 360Sstevel@tonic-gate #include <sys/types.h> 370Sstevel@tonic-gate #include <sys/stack.h> 380Sstevel@tonic-gate #include <signal.h> 390Sstevel@tonic-gate #include <limits.h> 400Sstevel@tonic-gate #include <sys/isa_defs.h> 410Sstevel@tonic-gate #include <proc_service.h> 420Sstevel@tonic-gate #include <dlfcn.h> 430Sstevel@tonic-gate #include <fnmatch.h> 440Sstevel@tonic-gate #include <libproc.h> 450Sstevel@tonic-gate #include "ramdata.h" 460Sstevel@tonic-gate #include "systable.h" 470Sstevel@tonic-gate #include "print.h" 480Sstevel@tonic-gate #include "proto.h" 490Sstevel@tonic-gate #include "htbl.h" 500Sstevel@tonic-gate 510Sstevel@tonic-gate /* 520Sstevel@tonic-gate * Functions supporting library function call tracing. 530Sstevel@tonic-gate */ 540Sstevel@tonic-gate 550Sstevel@tonic-gate typedef struct { 560Sstevel@tonic-gate prmap_t *pmap; 570Sstevel@tonic-gate int nmap; 580Sstevel@tonic-gate } ph_map_t; 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * static functions in this file. 620Sstevel@tonic-gate */ 630Sstevel@tonic-gate void function_entry(private_t *, struct bkpt *, struct callstack *); 640Sstevel@tonic-gate void function_return(private_t *, struct callstack *); 650Sstevel@tonic-gate int object_iter(void *, const prmap_t *, const char *); 662192Sraf int object_present(void *, const prmap_t *, const char *); 670Sstevel@tonic-gate int symbol_iter(void *, const GElf_Sym *, const char *); 680Sstevel@tonic-gate uintptr_t get_return_address(uintptr_t *); 690Sstevel@tonic-gate int get_arguments(long *argp); 700Sstevel@tonic-gate uintptr_t previous_fp(uintptr_t, uintptr_t *); 710Sstevel@tonic-gate int lwp_stack_traps(void *cd, const lwpstatus_t *Lsp); 720Sstevel@tonic-gate int thr_stack_traps(const td_thrhandle_t *Thp, void *cd); 730Sstevel@tonic-gate struct bkpt *create_bkpt(uintptr_t, int, int); 740Sstevel@tonic-gate void set_deferred_breakpoints(void); 750Sstevel@tonic-gate 760Sstevel@tonic-gate #define DEF_MAXCALL 16 /* initial value of Stk->maxcall */ 770Sstevel@tonic-gate 780Sstevel@tonic-gate #define FAULT_ADDR ((uintptr_t)(0-8)) 790Sstevel@tonic-gate 800Sstevel@tonic-gate #define HASHSZ 2048 810Sstevel@tonic-gate #define bpt_hash(addr) ((((addr) >> 13) ^ ((addr) >> 2)) & 0x7ff) 820Sstevel@tonic-gate 830Sstevel@tonic-gate static void 840Sstevel@tonic-gate setup_thread_agent(void) 850Sstevel@tonic-gate { 860Sstevel@tonic-gate struct bkpt *Bp; 870Sstevel@tonic-gate td_notify_t notify; 880Sstevel@tonic-gate td_thr_events_t events; 890Sstevel@tonic-gate 900Sstevel@tonic-gate if (Thr_agent != NULL) /* only once */ 910Sstevel@tonic-gate return; 920Sstevel@tonic-gate if (td_init() != TD_OK || td_ta_new(Proc, &Thr_agent) != TD_OK) 930Sstevel@tonic-gate Thr_agent = NULL; 940Sstevel@tonic-gate else { 950Sstevel@tonic-gate td_event_emptyset(&events); 960Sstevel@tonic-gate td_event_addset(&events, TD_CREATE); 970Sstevel@tonic-gate if (td_ta_event_addr(Thr_agent, TD_CREATE, ¬ify) == TD_OK && 980Sstevel@tonic-gate notify.type == NOTIFY_BPT && 990Sstevel@tonic-gate td_ta_set_event(Thr_agent, &events) == TD_OK && 1000Sstevel@tonic-gate (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL) 1010Sstevel@tonic-gate Bp->flags |= BPT_TD_CREATE; 1020Sstevel@tonic-gate } 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate /* 1062192Sraf * Delete all breakpoints in the range [base .. base+size) 1072192Sraf * from the breakpoint hash table. 1082192Sraf */ 1092192Sraf static void 1102192Sraf delete_breakpoints(uintptr_t base, size_t size) 1112192Sraf { 1122192Sraf struct bkpt **Bpp; 1132192Sraf struct bkpt *Bp; 1142192Sraf int i; 1152192Sraf 1162192Sraf if (bpt_hashtable == NULL) 1172192Sraf return; 1182192Sraf for (i = 0; i < HASHSZ; i++) { 1192192Sraf Bpp = &bpt_hashtable[i]; 1202192Sraf while ((Bp = *Bpp) != NULL) { 1212192Sraf if (Bp->addr < base || Bp->addr >= base + size) { 1222192Sraf Bpp = &Bp->next; 1232192Sraf continue; 1242192Sraf } 1252192Sraf *Bpp = Bp->next; 1262192Sraf if (Bp->sym_name) 1272192Sraf free(Bp->sym_name); 1282192Sraf free(Bp); 1292192Sraf } 1302192Sraf } 1312192Sraf } 1322192Sraf 1332192Sraf /* 1340Sstevel@tonic-gate * Establishment of breakpoints on traced library functions. 1350Sstevel@tonic-gate */ 1360Sstevel@tonic-gate void 1370Sstevel@tonic-gate establish_breakpoints(void) 1380Sstevel@tonic-gate { 1390Sstevel@tonic-gate if (Dynpat == NULL) 1400Sstevel@tonic-gate return; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* allocate the breakpoint hash table */ 1430Sstevel@tonic-gate if (bpt_hashtable == NULL) { 1440Sstevel@tonic-gate bpt_hashtable = my_malloc(HASHSZ * sizeof (struct bkpt *), 1457330SRoger.Faulkner@Sun.COM NULL); 1460Sstevel@tonic-gate (void) memset(bpt_hashtable, 0, 1477330SRoger.Faulkner@Sun.COM HASHSZ * sizeof (struct bkpt *)); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * Set special rtld_db event breakpoints, first time only. 1520Sstevel@tonic-gate */ 1530Sstevel@tonic-gate if (Rdb_agent == NULL && 1540Sstevel@tonic-gate (Rdb_agent = Prd_agent(Proc)) != NULL) { 1550Sstevel@tonic-gate rd_notify_t notify; 1560Sstevel@tonic-gate struct bkpt *Bp; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate (void) rd_event_enable(Rdb_agent, 1); 1590Sstevel@tonic-gate if (rd_event_addr(Rdb_agent, RD_PREINIT, ¬ify) == RD_OK && 1600Sstevel@tonic-gate (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL) 1610Sstevel@tonic-gate Bp->flags |= BPT_PREINIT; 1620Sstevel@tonic-gate if (rd_event_addr(Rdb_agent, RD_POSTINIT, ¬ify) == RD_OK && 1630Sstevel@tonic-gate (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL) 1640Sstevel@tonic-gate Bp->flags |= BPT_POSTINIT; 1650Sstevel@tonic-gate if (rd_event_addr(Rdb_agent, RD_DLACTIVITY, ¬ify) == RD_OK && 1660Sstevel@tonic-gate (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL) 1670Sstevel@tonic-gate Bp->flags |= BPT_DLACTIVITY; 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* 1710Sstevel@tonic-gate * Set special thread event breakpoint, first time libc is seen. 1720Sstevel@tonic-gate */ 1730Sstevel@tonic-gate if (Thr_agent == NULL) 1740Sstevel@tonic-gate setup_thread_agent(); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate /* 1770Sstevel@tonic-gate * Tell libproc to update its mappings. 1780Sstevel@tonic-gate */ 1790Sstevel@tonic-gate Pupdate_maps(Proc); 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* 1822192Sraf * If rtld_db told us a library was being deleted, 1832192Sraf * first mark all of the dynlibs as not present, then 1842192Sraf * iterate over the shared objects, marking only those 1852192Sraf * present that really are present, and finally delete 1862192Sraf * all of the not-present dynlibs. 1872192Sraf */ 1882192Sraf if (delete_library) { 1892192Sraf struct dynlib **Dpp; 1902192Sraf struct dynlib *Dp; 1912192Sraf 192*7675SEdward.Pilatowicz@Sun.COM for (Dp = Dynlib; Dp != NULL; Dp = Dp->next) 1932192Sraf Dp->present = FALSE; 1942192Sraf (void) Pobject_iter(Proc, object_present, NULL); 195*7675SEdward.Pilatowicz@Sun.COM Dpp = &Dynlib; 1962192Sraf while ((Dp = *Dpp) != NULL) { 1972192Sraf if (Dp->present) { 1982192Sraf Dpp = &Dp->next; 1992192Sraf continue; 2002192Sraf } 2012192Sraf delete_breakpoints(Dp->base, Dp->size); 2022192Sraf *Dpp = Dp->next; 2032192Sraf free(Dp->lib_name); 2042192Sraf free(Dp->match_name); 2052192Sraf free(Dp->prt_name); 2062192Sraf free(Dp); 2072192Sraf } 2082192Sraf delete_library = FALSE; 2092192Sraf } 2102192Sraf 2112192Sraf /* 2120Sstevel@tonic-gate * Iterate over the shared objects, creating breakpoints. 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate (void) Pobject_iter(Proc, object_iter, NULL); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * Now actually set all the breakpoints we just created. 2180Sstevel@tonic-gate */ 2190Sstevel@tonic-gate set_deferred_breakpoints(); 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate /* 2230Sstevel@tonic-gate * Initial establishment of stacks in a newly-grabbed process. 2240Sstevel@tonic-gate * establish_breakpoints() has already been called. 2250Sstevel@tonic-gate */ 2260Sstevel@tonic-gate void 2270Sstevel@tonic-gate establish_stacks(void) 2280Sstevel@tonic-gate { 2290Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Proc); 2300Sstevel@tonic-gate char mapfile[64]; 2310Sstevel@tonic-gate int mapfd; 2320Sstevel@tonic-gate struct stat statb; 2330Sstevel@tonic-gate prmap_t *Pmap = NULL; 2340Sstevel@tonic-gate int nmap = 0; 2350Sstevel@tonic-gate ph_map_t ph_map; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate (void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid); 2380Sstevel@tonic-gate if ((mapfd = open(mapfile, O_RDONLY)) < 0 || 2390Sstevel@tonic-gate fstat(mapfd, &statb) != 0 || 2400Sstevel@tonic-gate statb.st_size < sizeof (prmap_t) || 2410Sstevel@tonic-gate (Pmap = my_malloc(statb.st_size, NULL)) == NULL || 2420Sstevel@tonic-gate (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 || 2430Sstevel@tonic-gate (nmap /= sizeof (prmap_t)) == 0) { 2440Sstevel@tonic-gate if (Pmap != NULL) 2450Sstevel@tonic-gate free(Pmap); 2460Sstevel@tonic-gate Pmap = NULL; 2470Sstevel@tonic-gate nmap = 0; 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate if (mapfd >= 0) 2500Sstevel@tonic-gate (void) close(mapfd); 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * Iterate over lwps, establishing stacks. 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate ph_map.pmap = Pmap; 2560Sstevel@tonic-gate ph_map.nmap = nmap; 2570Sstevel@tonic-gate (void) Plwp_iter(Proc, lwp_stack_traps, &ph_map); 2580Sstevel@tonic-gate if (Pmap != NULL) 2590Sstevel@tonic-gate free(Pmap); 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate if (Thr_agent == NULL) 2620Sstevel@tonic-gate return; 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate /* 2650Sstevel@tonic-gate * Iterate over unbound threads, establishing stacks. 2660Sstevel@tonic-gate */ 2670Sstevel@tonic-gate (void) td_ta_thr_iter(Thr_agent, thr_stack_traps, NULL, 2687330SRoger.Faulkner@Sun.COM TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, 2697330SRoger.Faulkner@Sun.COM TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate void 2730Sstevel@tonic-gate do_symbol_iter(const char *object_name, struct dynpat *Dyp) 2740Sstevel@tonic-gate { 2750Sstevel@tonic-gate if (*Dyp->Dp->prt_name == '\0') 2760Sstevel@tonic-gate object_name = PR_OBJ_EXEC; 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate /* 2790Sstevel@tonic-gate * Always search the dynamic symbol table. 2800Sstevel@tonic-gate */ 2810Sstevel@tonic-gate (void) Psymbol_iter(Proc, object_name, 2827330SRoger.Faulkner@Sun.COM PR_DYNSYM, BIND_WEAK|BIND_GLOBAL|TYPE_FUNC, 2837330SRoger.Faulkner@Sun.COM symbol_iter, Dyp); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* 2860Sstevel@tonic-gate * Search the static symbol table if this is the 2870Sstevel@tonic-gate * executable file or if we are being asked to 2880Sstevel@tonic-gate * report internal calls within the library. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate if (object_name == PR_OBJ_EXEC || Dyp->internal) 2910Sstevel@tonic-gate (void) Psymbol_iter(Proc, object_name, 2927330SRoger.Faulkner@Sun.COM PR_SYMTAB, BIND_ANY|TYPE_FUNC, 2937330SRoger.Faulkner@Sun.COM symbol_iter, Dyp); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate /* ARGSUSED */ 2970Sstevel@tonic-gate int 2980Sstevel@tonic-gate object_iter(void *cd, const prmap_t *pmp, const char *object_name) 2990Sstevel@tonic-gate { 3000Sstevel@tonic-gate char name[100]; 3010Sstevel@tonic-gate struct dynpat *Dyp; 3020Sstevel@tonic-gate struct dynlib *Dp; 3030Sstevel@tonic-gate const char *str; 3040Sstevel@tonic-gate char *s; 3050Sstevel@tonic-gate int i; 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate if ((pmp->pr_mflags & MA_WRITE) || !(pmp->pr_mflags & MA_EXEC)) 3080Sstevel@tonic-gate return (0); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate /* 3110Sstevel@tonic-gate * Set special thread event breakpoint, first time libc is seen. 3120Sstevel@tonic-gate */ 3130Sstevel@tonic-gate if (Thr_agent == NULL && strstr(object_name, "/libc.so.") != NULL) 3140Sstevel@tonic-gate setup_thread_agent(); 3150Sstevel@tonic-gate 316*7675SEdward.Pilatowicz@Sun.COM for (Dp = Dynlib; Dp != NULL; Dp = Dp->next) 3170Sstevel@tonic-gate if (strcmp(object_name, Dp->lib_name) == 0 || 3180Sstevel@tonic-gate (strcmp(Dp->lib_name, "a.out") == 0 && 3190Sstevel@tonic-gate strcmp(pmp->pr_mapname, "a.out") == 0)) 3200Sstevel@tonic-gate break; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate if (Dp == NULL) { 3230Sstevel@tonic-gate Dp = my_malloc(sizeof (struct dynlib), NULL); 3240Sstevel@tonic-gate (void) memset(Dp, 0, sizeof (struct dynlib)); 3250Sstevel@tonic-gate if (strcmp(pmp->pr_mapname, "a.out") == 0) { 3260Sstevel@tonic-gate Dp->lib_name = strdup(pmp->pr_mapname); 3270Sstevel@tonic-gate Dp->match_name = strdup(pmp->pr_mapname); 3280Sstevel@tonic-gate Dp->prt_name = strdup(""); 3290Sstevel@tonic-gate } else { 3300Sstevel@tonic-gate Dp->lib_name = strdup(object_name); 3310Sstevel@tonic-gate if ((str = strrchr(object_name, '/')) != NULL) 3320Sstevel@tonic-gate str++; 3330Sstevel@tonic-gate else 3340Sstevel@tonic-gate str = object_name; 3350Sstevel@tonic-gate (void) strncpy(name, str, sizeof (name) - 2); 3360Sstevel@tonic-gate name[sizeof (name) - 2] = '\0'; 3370Sstevel@tonic-gate if ((s = strstr(name, ".so")) != NULL) 3380Sstevel@tonic-gate *s = '\0'; 3390Sstevel@tonic-gate Dp->match_name = strdup(name); 3400Sstevel@tonic-gate (void) strcat(name, ":"); 3410Sstevel@tonic-gate Dp->prt_name = strdup(name); 3420Sstevel@tonic-gate } 343*7675SEdward.Pilatowicz@Sun.COM Dp->next = Dynlib; 344*7675SEdward.Pilatowicz@Sun.COM Dynlib = Dp; 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate if (Dp->built || 3480Sstevel@tonic-gate (not_consist && strcmp(Dp->prt_name, "ld:") != 0)) /* kludge */ 3490Sstevel@tonic-gate return (0); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate if (hflag && not_consist) 3520Sstevel@tonic-gate (void) fprintf(stderr, "not_consist is TRUE, building %s\n", 3537330SRoger.Faulkner@Sun.COM Dp->lib_name); 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate Dp->base = pmp->pr_vaddr; 3560Sstevel@tonic-gate Dp->size = pmp->pr_size; 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * For every dynlib pattern that matches this library's name, 3600Sstevel@tonic-gate * iterate through all of the library's symbols looking for 3610Sstevel@tonic-gate * matching symbol name patterns. 3620Sstevel@tonic-gate */ 3630Sstevel@tonic-gate for (Dyp = Dynpat; Dyp != NULL; Dyp = Dyp->next) { 3640Sstevel@tonic-gate if (interrupt|sigusr1) 3650Sstevel@tonic-gate break; 3660Sstevel@tonic-gate for (i = 0; i < Dyp->nlibpat; i++) { 3670Sstevel@tonic-gate if (interrupt|sigusr1) 3680Sstevel@tonic-gate break; 3690Sstevel@tonic-gate if (fnmatch(Dyp->libpat[i], Dp->match_name, 0) != 0) 3700Sstevel@tonic-gate continue; /* no match */ 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * Require an exact match for the executable (a.out) 3740Sstevel@tonic-gate * and for the dynamic linker (ld.so.1). 3750Sstevel@tonic-gate */ 3760Sstevel@tonic-gate if ((strcmp(Dp->match_name, "a.out") == 0 || 3770Sstevel@tonic-gate strcmp(Dp->match_name, "ld") == 0) && 3780Sstevel@tonic-gate strcmp(Dyp->libpat[i], Dp->match_name) != 0) 3790Sstevel@tonic-gate continue; 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* 3820Sstevel@tonic-gate * Set Dyp->Dp to Dp so symbol_iter() can use it. 3830Sstevel@tonic-gate */ 3840Sstevel@tonic-gate Dyp->Dp = Dp; 3850Sstevel@tonic-gate do_symbol_iter(object_name, Dyp); 3860Sstevel@tonic-gate Dyp->Dp = NULL; 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate Dp->built = TRUE; 3910Sstevel@tonic-gate return (interrupt | sigusr1); 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate 3942192Sraf /* ARGSUSED */ 3952192Sraf int 3962192Sraf object_present(void *cd, const prmap_t *pmp, const char *object_name) 3972192Sraf { 3982192Sraf struct dynlib *Dp; 3992192Sraf 400*7675SEdward.Pilatowicz@Sun.COM for (Dp = Dynlib; Dp != NULL; Dp = Dp->next) { 4012192Sraf if (Dp->base == pmp->pr_vaddr) 4022192Sraf Dp->present = TRUE; 4032192Sraf } 4042192Sraf 4052192Sraf return (0); 4062192Sraf } 4072192Sraf 4080Sstevel@tonic-gate /* 4090Sstevel@tonic-gate * Search for an existing breakpoint at the 'pc' location. 4100Sstevel@tonic-gate */ 4110Sstevel@tonic-gate struct bkpt * 4120Sstevel@tonic-gate get_bkpt(uintptr_t pc) 4130Sstevel@tonic-gate { 4140Sstevel@tonic-gate struct bkpt *Bp; 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate for (Bp = bpt_hashtable[bpt_hash(pc)]; Bp != NULL; Bp = Bp->next) 4170Sstevel@tonic-gate if (pc == Bp->addr) 4180Sstevel@tonic-gate break; 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate return (Bp); 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * Create a breakpoint at 'pc', if one is not there already. 4250Sstevel@tonic-gate * 'ret' is true when creating a function return breakpoint, in which case 4260Sstevel@tonic-gate * fail and return NULL if the breakpoint would be created in writeable data. 4270Sstevel@tonic-gate * If 'set' it true, set the breakpoint in the process now. 4280Sstevel@tonic-gate */ 4290Sstevel@tonic-gate struct bkpt * 4300Sstevel@tonic-gate create_bkpt(uintptr_t pc, int ret, int set) 4310Sstevel@tonic-gate { 4320Sstevel@tonic-gate uint_t hix = bpt_hash(pc); 4330Sstevel@tonic-gate struct bkpt *Bp; 4340Sstevel@tonic-gate const prmap_t *pmp; 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate for (Bp = bpt_hashtable[hix]; Bp != NULL; Bp = Bp->next) 4370Sstevel@tonic-gate if (pc == Bp->addr) 4380Sstevel@tonic-gate return (Bp); 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * Don't set return breakpoints on writeable data 4420Sstevel@tonic-gate * or on any space other than executable text. 4430Sstevel@tonic-gate * Don't set breakpoints in the child of a vfork() 4440Sstevel@tonic-gate * because that would modify the parent's address space. 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate if (is_vfork_child || 4470Sstevel@tonic-gate (ret && 4480Sstevel@tonic-gate ((pmp = Paddr_to_text_map(Proc, pc)) == NULL || 4490Sstevel@tonic-gate !(pmp->pr_mflags & MA_EXEC) || 4500Sstevel@tonic-gate (pmp->pr_mflags & MA_WRITE)))) 4510Sstevel@tonic-gate return (NULL); 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate /* create a new unnamed breakpoint */ 4540Sstevel@tonic-gate Bp = my_malloc(sizeof (struct bkpt), NULL); 4550Sstevel@tonic-gate Bp->sym_name = NULL; 4560Sstevel@tonic-gate Bp->dyn = NULL; 4570Sstevel@tonic-gate Bp->addr = pc; 4580Sstevel@tonic-gate Bp->instr = 0; 4590Sstevel@tonic-gate Bp->flags = 0; 4600Sstevel@tonic-gate if (set && Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0) 4610Sstevel@tonic-gate Bp->flags |= BPT_ACTIVE; 4620Sstevel@tonic-gate Bp->next = bpt_hashtable[hix]; 4630Sstevel@tonic-gate bpt_hashtable[hix] = Bp; 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate return (Bp); 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate /* 4690Sstevel@tonic-gate * Set all breakpoints that haven't been set yet. 4700Sstevel@tonic-gate * Deactivate all breakpoints from modules that are not present any more. 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate void 4730Sstevel@tonic-gate set_deferred_breakpoints(void) 4740Sstevel@tonic-gate { 4750Sstevel@tonic-gate struct bkpt *Bp; 4760Sstevel@tonic-gate int i; 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate if (is_vfork_child) 4790Sstevel@tonic-gate return; 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate for (i = 0; i < HASHSZ; i++) { 4820Sstevel@tonic-gate for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) { 4830Sstevel@tonic-gate if (!(Bp->flags & BPT_ACTIVE)) { 4840Sstevel@tonic-gate if (!(Bp->flags & BPT_EXCLUDE) && 4850Sstevel@tonic-gate Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0) 4860Sstevel@tonic-gate Bp->flags |= BPT_ACTIVE; 4870Sstevel@tonic-gate } else if (Paddr_to_text_map(Proc, Bp->addr) == NULL) { 4880Sstevel@tonic-gate Bp->flags &= ~BPT_ACTIVE; 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate } 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate int 4950Sstevel@tonic-gate symbol_iter(void *cd, const GElf_Sym *sym, const char *sym_name) 4960Sstevel@tonic-gate { 4970Sstevel@tonic-gate struct dynpat *Dyp = cd; 4980Sstevel@tonic-gate struct dynlib *Dp = Dyp->Dp; 4990Sstevel@tonic-gate uintptr_t pc = sym->st_value; 5000Sstevel@tonic-gate struct bkpt *Bp; 5010Sstevel@tonic-gate int i; 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate /* ignore any undefined symbols */ 5040Sstevel@tonic-gate if (sym->st_shndx == SHN_UNDEF) 5050Sstevel@tonic-gate return (0); 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* 5080Sstevel@tonic-gate * Arbitrarily omit "_start" from the executable. 5090Sstevel@tonic-gate * (Avoid indentation before main().) 5100Sstevel@tonic-gate */ 5110Sstevel@tonic-gate if (*Dp->prt_name == '\0' && strcmp(sym_name, "_start") == 0) 5120Sstevel@tonic-gate return (0); 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate /* 5150Sstevel@tonic-gate * Arbitrarily omit "_rt_boot" from the dynamic linker. 5160Sstevel@tonic-gate * (Avoid indentation before main().) 5170Sstevel@tonic-gate */ 5180Sstevel@tonic-gate if (strcmp(Dp->match_name, "ld") == 0 && 5190Sstevel@tonic-gate strcmp(sym_name, "_rt_boot") == 0) 5200Sstevel@tonic-gate return (0); 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate /* 5230Sstevel@tonic-gate * Arbitrarily omit any symbols whose name starts with '.'. 5240Sstevel@tonic-gate * Apparantly putting a breakpoint on .umul causes a 5250Sstevel@tonic-gate * fatal error in libthread (%y is not restored correctly 5260Sstevel@tonic-gate * when a single step is taken). Looks like a /proc bug. 5270Sstevel@tonic-gate */ 5280Sstevel@tonic-gate if (*sym_name == '.') 5290Sstevel@tonic-gate return (0); 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate /* 5320Sstevel@tonic-gate * For each pattern in the array of symbol patterns, 5330Sstevel@tonic-gate * if the pattern matches the symbol name, then 5340Sstevel@tonic-gate * create a breakpoint at the function in question. 5350Sstevel@tonic-gate */ 5360Sstevel@tonic-gate for (i = 0; i < Dyp->nsympat; i++) { 5370Sstevel@tonic-gate if (interrupt|sigusr1) 5380Sstevel@tonic-gate break; 5390Sstevel@tonic-gate if (fnmatch(Dyp->sympat[i], sym_name, 0) != 0) 5400Sstevel@tonic-gate continue; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate if ((Bp = create_bkpt(pc, 0, 0)) == NULL) /* can't fail */ 5430Sstevel@tonic-gate return (0); 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate /* 5460Sstevel@tonic-gate * New breakpoints receive a name now. 5470Sstevel@tonic-gate * For existing breakpoints, prefer the subset name if possible, 5480Sstevel@tonic-gate * else prefer the shorter name. 5490Sstevel@tonic-gate */ 5500Sstevel@tonic-gate if (Bp->sym_name == NULL) { 5510Sstevel@tonic-gate Bp->sym_name = strdup(sym_name); 5520Sstevel@tonic-gate } else if (strstr(Bp->sym_name, sym_name) != NULL || 5530Sstevel@tonic-gate strlen(Bp->sym_name) > strlen(sym_name)) { 5540Sstevel@tonic-gate free(Bp->sym_name); 5550Sstevel@tonic-gate Bp->sym_name = strdup(sym_name); 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate Bp->dyn = Dp; 5580Sstevel@tonic-gate Bp->flags |= Dyp->flag; 5590Sstevel@tonic-gate if (Dyp->exclude) 5600Sstevel@tonic-gate Bp->flags |= BPT_EXCLUDE; 5610Sstevel@tonic-gate else if (Dyp->internal || *Dp->prt_name == '\0') 5620Sstevel@tonic-gate Bp->flags |= BPT_INTERNAL; 5630Sstevel@tonic-gate return (0); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate return (interrupt | sigusr1); 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate /* For debugging only ---- */ 5700Sstevel@tonic-gate void 5710Sstevel@tonic-gate report_htable_stats(void) 5720Sstevel@tonic-gate { 5730Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Proc); 5740Sstevel@tonic-gate struct callstack *Stk; 5750Sstevel@tonic-gate struct bkpt *Bp; 5760Sstevel@tonic-gate uint_t Min = 1000000; 5770Sstevel@tonic-gate uint_t Max = 0; 5780Sstevel@tonic-gate uint_t Avg = 0; 5790Sstevel@tonic-gate uint_t Total = 0; 5800Sstevel@tonic-gate uint_t i, j; 5810Sstevel@tonic-gate uint_t bucket[HASHSZ]; 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate if (Dynpat == NULL || !hflag) 5840Sstevel@tonic-gate return; 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate hflag = FALSE; 5870Sstevel@tonic-gate (void) memset(bucket, 0, sizeof (bucket)); 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate for (i = 0; i < HASHSZ; i++) { 5900Sstevel@tonic-gate j = 0; 5910Sstevel@tonic-gate for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) 5920Sstevel@tonic-gate j++; 5930Sstevel@tonic-gate if (j < Min) 5940Sstevel@tonic-gate Min = j; 5950Sstevel@tonic-gate if (j > Max) 5960Sstevel@tonic-gate Max = j; 5970Sstevel@tonic-gate if (j < HASHSZ) 5980Sstevel@tonic-gate bucket[j]++; 5990Sstevel@tonic-gate Total += j; 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate Avg = (Total + HASHSZ / 2) / HASHSZ; 6020Sstevel@tonic-gate (void) fprintf(stderr, "truss hash table statistics --------\n"); 6030Sstevel@tonic-gate (void) fprintf(stderr, " Total = %u\n", Total); 6040Sstevel@tonic-gate (void) fprintf(stderr, " Min = %u\n", Min); 6050Sstevel@tonic-gate (void) fprintf(stderr, " Max = %u\n", Max); 6060Sstevel@tonic-gate (void) fprintf(stderr, " Avg = %u\n", Avg); 6070Sstevel@tonic-gate for (i = 0; i < HASHSZ; i++) 6080Sstevel@tonic-gate if (bucket[i]) 6090Sstevel@tonic-gate (void) fprintf(stderr, " %3u buckets of size %d\n", 6107330SRoger.Faulkner@Sun.COM bucket[i], i); 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate (void) fprintf(stderr, "truss-detected stacks --------\n"); 6130Sstevel@tonic-gate for (Stk = callstack; Stk != NULL; Stk = Stk->next) { 6140Sstevel@tonic-gate (void) fprintf(stderr, 6157330SRoger.Faulkner@Sun.COM " base = 0x%.8lx end = 0x%.8lx size = %ld\n", 6167330SRoger.Faulkner@Sun.COM (ulong_t)Stk->stkbase, 6177330SRoger.Faulkner@Sun.COM (ulong_t)Stk->stkend, 6187330SRoger.Faulkner@Sun.COM (ulong_t)(Stk->stkend - Stk->stkbase)); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate (void) fprintf(stderr, "primary unix stack --------\n"); 6210Sstevel@tonic-gate (void) fprintf(stderr, 6227330SRoger.Faulkner@Sun.COM " base = 0x%.8lx end = 0x%.8lx size = %ld\n", 6237330SRoger.Faulkner@Sun.COM (ulong_t)Psp->pr_stkbase, 6247330SRoger.Faulkner@Sun.COM (ulong_t)(Psp->pr_stkbase + Psp->pr_stksize), 6257330SRoger.Faulkner@Sun.COM (ulong_t)Psp->pr_stksize); 6260Sstevel@tonic-gate (void) fprintf(stderr, "nthr_create = %u\n", nthr_create); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate void 6300Sstevel@tonic-gate make_lwp_stack(const lwpstatus_t *Lsp, prmap_t *Pmap, int nmap) 6310Sstevel@tonic-gate { 6320Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Proc); 6330Sstevel@tonic-gate uintptr_t sp = Lsp->pr_reg[R_SP]; 6340Sstevel@tonic-gate id_t lwpid = Lsp->pr_lwpid; 6350Sstevel@tonic-gate struct callstack *Stk; 6360Sstevel@tonic-gate td_thrhandle_t th; 6370Sstevel@tonic-gate td_thrinfo_t thrinfo; 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate if (data_model != PR_MODEL_LP64) 6400Sstevel@tonic-gate sp = (uint32_t)sp; 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate /* check to see if we already have this stack */ 6430Sstevel@tonic-gate if (sp == 0) 6440Sstevel@tonic-gate return; 6450Sstevel@tonic-gate for (Stk = callstack; Stk != NULL; Stk = Stk->next) 6460Sstevel@tonic-gate if (sp >= Stk->stkbase && sp < Stk->stkend) 6470Sstevel@tonic-gate return; 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate Stk = my_malloc(sizeof (struct callstack), NULL); 6500Sstevel@tonic-gate Stk->next = callstack; 6510Sstevel@tonic-gate callstack = Stk; 6520Sstevel@tonic-gate nstack++; 6530Sstevel@tonic-gate Stk->tref = 0; 6540Sstevel@tonic-gate Stk->tid = 0; 6550Sstevel@tonic-gate Stk->nthr_create = 0; 6560Sstevel@tonic-gate Stk->ncall = 0; 6570Sstevel@tonic-gate Stk->maxcall = DEF_MAXCALL; 6580Sstevel@tonic-gate Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL); 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /* primary stack */ 6610Sstevel@tonic-gate if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) { 6620Sstevel@tonic-gate Stk->stkbase = Psp->pr_stkbase; 6630Sstevel@tonic-gate Stk->stkend = Stk->stkbase + Psp->pr_stksize; 6640Sstevel@tonic-gate return; 6650Sstevel@tonic-gate } 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate /* alternate stack */ 6680Sstevel@tonic-gate if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) && 6690Sstevel@tonic-gate sp >= (uintptr_t)Lsp->pr_altstack.ss_sp && 6700Sstevel@tonic-gate sp < (uintptr_t)Lsp->pr_altstack.ss_sp 6710Sstevel@tonic-gate + Lsp->pr_altstack.ss_size) { 6720Sstevel@tonic-gate Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp; 6730Sstevel@tonic-gate Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size; 6740Sstevel@tonic-gate return; 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate /* thread stacks? */ 6780Sstevel@tonic-gate if (Thr_agent != NULL && 6790Sstevel@tonic-gate td_ta_map_lwp2thr(Thr_agent, lwpid, &th) == TD_OK && 6800Sstevel@tonic-gate td_thr_get_info(&th, &thrinfo) == TD_OK && 6810Sstevel@tonic-gate sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize && 6820Sstevel@tonic-gate sp < (uintptr_t)thrinfo.ti_stkbase) { 6830Sstevel@tonic-gate /* The bloody fools got this backwards! */ 6840Sstevel@tonic-gate Stk->stkend = (uintptr_t)thrinfo.ti_stkbase; 6850Sstevel@tonic-gate Stk->stkbase = Stk->stkend - thrinfo.ti_stksize; 6860Sstevel@tonic-gate return; 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate /* last chance -- try the raw memory map */ 6900Sstevel@tonic-gate for (; nmap; nmap--, Pmap++) { 6910Sstevel@tonic-gate if (sp >= Pmap->pr_vaddr && 6920Sstevel@tonic-gate sp < Pmap->pr_vaddr + Pmap->pr_size) { 6930Sstevel@tonic-gate Stk->stkbase = Pmap->pr_vaddr; 6940Sstevel@tonic-gate Stk->stkend = Pmap->pr_vaddr + Pmap->pr_size; 6950Sstevel@tonic-gate return; 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate callstack = Stk->next; 7000Sstevel@tonic-gate nstack--; 7010Sstevel@tonic-gate free(Stk->stack); 7020Sstevel@tonic-gate free(Stk); 7030Sstevel@tonic-gate } 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate void 7060Sstevel@tonic-gate make_thr_stack(const td_thrhandle_t *Thp, prgregset_t reg) 7070Sstevel@tonic-gate { 7080Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Proc); 7090Sstevel@tonic-gate td_thrinfo_t thrinfo; 7100Sstevel@tonic-gate uintptr_t sp = reg[R_SP]; 7110Sstevel@tonic-gate struct callstack *Stk; 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate if (data_model != PR_MODEL_LP64) 7140Sstevel@tonic-gate sp = (uint32_t)sp; 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate /* check to see if we already have this stack */ 7170Sstevel@tonic-gate if (sp == 0) 7180Sstevel@tonic-gate return; 7190Sstevel@tonic-gate for (Stk = callstack; Stk != NULL; Stk = Stk->next) 7200Sstevel@tonic-gate if (sp >= Stk->stkbase && sp < Stk->stkend) 7210Sstevel@tonic-gate return; 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate Stk = my_malloc(sizeof (struct callstack), NULL); 7240Sstevel@tonic-gate Stk->next = callstack; 7250Sstevel@tonic-gate callstack = Stk; 7260Sstevel@tonic-gate nstack++; 7270Sstevel@tonic-gate Stk->tref = 0; 7280Sstevel@tonic-gate Stk->tid = 0; 7290Sstevel@tonic-gate Stk->nthr_create = 0; 7300Sstevel@tonic-gate Stk->ncall = 0; 7310Sstevel@tonic-gate Stk->maxcall = DEF_MAXCALL; 7320Sstevel@tonic-gate Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL); 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate /* primary stack */ 7350Sstevel@tonic-gate if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) { 7360Sstevel@tonic-gate Stk->stkbase = Psp->pr_stkbase; 7370Sstevel@tonic-gate Stk->stkend = Stk->stkbase + Psp->pr_stksize; 7380Sstevel@tonic-gate return; 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate if (td_thr_get_info(Thp, &thrinfo) == TD_OK && 7420Sstevel@tonic-gate sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize && 7430Sstevel@tonic-gate sp < (uintptr_t)thrinfo.ti_stkbase) { 7440Sstevel@tonic-gate /* The bloody fools got this backwards! */ 7450Sstevel@tonic-gate Stk->stkend = (uintptr_t)thrinfo.ti_stkbase; 7460Sstevel@tonic-gate Stk->stkbase = Stk->stkend - thrinfo.ti_stksize; 7470Sstevel@tonic-gate return; 7480Sstevel@tonic-gate } 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate callstack = Stk->next; 7510Sstevel@tonic-gate nstack--; 7520Sstevel@tonic-gate free(Stk->stack); 7530Sstevel@tonic-gate free(Stk); 7540Sstevel@tonic-gate } 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate struct callstack * 7570Sstevel@tonic-gate find_lwp_stack(uintptr_t sp) 7580Sstevel@tonic-gate { 7590Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Proc); 7600Sstevel@tonic-gate char mapfile[64]; 7610Sstevel@tonic-gate int mapfd; 7620Sstevel@tonic-gate struct stat statb; 7630Sstevel@tonic-gate prmap_t *Pmap = NULL; 7640Sstevel@tonic-gate prmap_t *pmap = NULL; 7650Sstevel@tonic-gate int nmap = 0; 7660Sstevel@tonic-gate struct callstack *Stk = NULL; 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate /* 7690Sstevel@tonic-gate * Get the address space map. 7700Sstevel@tonic-gate */ 7710Sstevel@tonic-gate (void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid); 7720Sstevel@tonic-gate if ((mapfd = open(mapfile, O_RDONLY)) < 0 || 7730Sstevel@tonic-gate fstat(mapfd, &statb) != 0 || 7740Sstevel@tonic-gate statb.st_size < sizeof (prmap_t) || 7750Sstevel@tonic-gate (Pmap = my_malloc(statb.st_size, NULL)) == NULL || 7760Sstevel@tonic-gate (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 || 7770Sstevel@tonic-gate (nmap /= sizeof (prmap_t)) == 0) { 7780Sstevel@tonic-gate if (Pmap != NULL) 7790Sstevel@tonic-gate free(Pmap); 7800Sstevel@tonic-gate if (mapfd >= 0) 7810Sstevel@tonic-gate (void) close(mapfd); 7820Sstevel@tonic-gate return (NULL); 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate (void) close(mapfd); 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate for (pmap = Pmap; nmap--; pmap++) { 7870Sstevel@tonic-gate if (sp >= pmap->pr_vaddr && 7880Sstevel@tonic-gate sp < pmap->pr_vaddr + pmap->pr_size) { 7890Sstevel@tonic-gate Stk = my_malloc(sizeof (struct callstack), NULL); 7900Sstevel@tonic-gate Stk->next = callstack; 7910Sstevel@tonic-gate callstack = Stk; 7920Sstevel@tonic-gate nstack++; 7930Sstevel@tonic-gate Stk->stkbase = pmap->pr_vaddr; 7940Sstevel@tonic-gate Stk->stkend = pmap->pr_vaddr + pmap->pr_size; 7950Sstevel@tonic-gate Stk->tref = 0; 7960Sstevel@tonic-gate Stk->tid = 0; 7970Sstevel@tonic-gate Stk->nthr_create = 0; 7980Sstevel@tonic-gate Stk->ncall = 0; 7990Sstevel@tonic-gate Stk->maxcall = DEF_MAXCALL; 8000Sstevel@tonic-gate Stk->stack = my_malloc( 8017330SRoger.Faulkner@Sun.COM DEF_MAXCALL * sizeof (*Stk->stack), NULL); 8020Sstevel@tonic-gate break; 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate free(Pmap); 8070Sstevel@tonic-gate return (Stk); 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate struct callstack * 8110Sstevel@tonic-gate find_stack(uintptr_t sp) 8120Sstevel@tonic-gate { 8130Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Proc); 8140Sstevel@tonic-gate private_t *pri = get_private(); 8150Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat; 8160Sstevel@tonic-gate id_t lwpid = Lsp->pr_lwpid; 8170Sstevel@tonic-gate #if defined(__sparc) 8180Sstevel@tonic-gate prgreg_t tref = Lsp->pr_reg[R_G7]; 8190Sstevel@tonic-gate #elif defined(__amd64) 8200Sstevel@tonic-gate prgreg_t tref = Lsp->pr_reg[REG_FS]; 8210Sstevel@tonic-gate #elif defined(__i386) 8220Sstevel@tonic-gate prgreg_t tref = Lsp->pr_reg[GS]; 8230Sstevel@tonic-gate #endif 8240Sstevel@tonic-gate struct callstack *Stk = NULL; 8250Sstevel@tonic-gate td_thrhandle_t th; 8260Sstevel@tonic-gate td_thrinfo_t thrinfo; 8270Sstevel@tonic-gate td_err_e error; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate /* primary stack */ 8300Sstevel@tonic-gate if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) { 8310Sstevel@tonic-gate Stk = my_malloc(sizeof (struct callstack), NULL); 8320Sstevel@tonic-gate Stk->next = callstack; 8330Sstevel@tonic-gate callstack = Stk; 8340Sstevel@tonic-gate nstack++; 8350Sstevel@tonic-gate Stk->stkbase = Psp->pr_stkbase; 8360Sstevel@tonic-gate Stk->stkend = Stk->stkbase + Psp->pr_stksize; 8370Sstevel@tonic-gate Stk->tref = 0; 8380Sstevel@tonic-gate Stk->tid = 0; 8390Sstevel@tonic-gate Stk->nthr_create = 0; 8400Sstevel@tonic-gate Stk->ncall = 0; 8410Sstevel@tonic-gate Stk->maxcall = DEF_MAXCALL; 8420Sstevel@tonic-gate Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), 8437330SRoger.Faulkner@Sun.COM NULL); 8440Sstevel@tonic-gate return (Stk); 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate /* alternate stack */ 8480Sstevel@tonic-gate if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) && 8490Sstevel@tonic-gate sp >= (uintptr_t)Lsp->pr_altstack.ss_sp && 8500Sstevel@tonic-gate sp < (uintptr_t)Lsp->pr_altstack.ss_sp 8510Sstevel@tonic-gate + Lsp->pr_altstack.ss_size) { 8520Sstevel@tonic-gate Stk = my_malloc(sizeof (struct callstack), NULL); 8530Sstevel@tonic-gate Stk->next = callstack; 8540Sstevel@tonic-gate callstack = Stk; 8550Sstevel@tonic-gate nstack++; 8560Sstevel@tonic-gate Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp; 8570Sstevel@tonic-gate Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size; 8580Sstevel@tonic-gate Stk->tref = 0; 8590Sstevel@tonic-gate Stk->tid = 0; 8600Sstevel@tonic-gate Stk->nthr_create = 0; 8610Sstevel@tonic-gate Stk->ncall = 0; 8620Sstevel@tonic-gate Stk->maxcall = DEF_MAXCALL; 8630Sstevel@tonic-gate Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), 8647330SRoger.Faulkner@Sun.COM NULL); 8650Sstevel@tonic-gate return (Stk); 8660Sstevel@tonic-gate } 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate if (Thr_agent == NULL) 8690Sstevel@tonic-gate return (find_lwp_stack(sp)); 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate /* thread stacks? */ 8720Sstevel@tonic-gate if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) { 8730Sstevel@tonic-gate if (hflag) 8740Sstevel@tonic-gate (void) fprintf(stderr, 8757330SRoger.Faulkner@Sun.COM "cannot get thread handle for " 8767330SRoger.Faulkner@Sun.COM "lwp#%d, error=%d, tref=0x%.8lx\n", 8777330SRoger.Faulkner@Sun.COM (int)lwpid, error, (long)tref); 8780Sstevel@tonic-gate return (NULL); 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) { 8820Sstevel@tonic-gate if (hflag) 8830Sstevel@tonic-gate (void) fprintf(stderr, 8847330SRoger.Faulkner@Sun.COM "cannot get thread info for " 8857330SRoger.Faulkner@Sun.COM "lwp#%d, error=%d, tref=0x%.8lx\n", 8867330SRoger.Faulkner@Sun.COM (int)lwpid, error, (long)tref); 8870Sstevel@tonic-gate return (NULL); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate if (sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize && 8910Sstevel@tonic-gate sp < (uintptr_t)thrinfo.ti_stkbase) { 8920Sstevel@tonic-gate Stk = my_malloc(sizeof (struct callstack), NULL); 8930Sstevel@tonic-gate Stk->next = callstack; 8940Sstevel@tonic-gate callstack = Stk; 8950Sstevel@tonic-gate nstack++; 8960Sstevel@tonic-gate /* The bloody fools got this backwards! */ 8970Sstevel@tonic-gate Stk->stkend = (uintptr_t)thrinfo.ti_stkbase; 8980Sstevel@tonic-gate Stk->stkbase = Stk->stkend - thrinfo.ti_stksize; 8990Sstevel@tonic-gate Stk->tref = tref; 9000Sstevel@tonic-gate Stk->tid = thrinfo.ti_tid; 9010Sstevel@tonic-gate Stk->nthr_create = nthr_create; 9020Sstevel@tonic-gate Stk->ncall = 0; 9030Sstevel@tonic-gate Stk->maxcall = DEF_MAXCALL; 9040Sstevel@tonic-gate Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), 9057330SRoger.Faulkner@Sun.COM NULL); 9060Sstevel@tonic-gate return (Stk); 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate /* stack bounds failure -- complain bitterly */ 9100Sstevel@tonic-gate if (hflag) { 9110Sstevel@tonic-gate (void) fprintf(stderr, 9127330SRoger.Faulkner@Sun.COM "sp not within thread stack: " 9137330SRoger.Faulkner@Sun.COM "sp=0x%.8lx stkbase=0x%.8lx stkend=0x%.8lx\n", 9147330SRoger.Faulkner@Sun.COM (ulong_t)sp, 9157330SRoger.Faulkner@Sun.COM /* The bloody fools got this backwards! */ 9167330SRoger.Faulkner@Sun.COM (ulong_t)thrinfo.ti_stkbase - thrinfo.ti_stksize, 9177330SRoger.Faulkner@Sun.COM (ulong_t)thrinfo.ti_stkbase); 9180Sstevel@tonic-gate } 9190Sstevel@tonic-gate 9200Sstevel@tonic-gate return (NULL); 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate void 9240Sstevel@tonic-gate get_tid(struct callstack *Stk) 9250Sstevel@tonic-gate { 9260Sstevel@tonic-gate private_t *pri = get_private(); 9270Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat; 9280Sstevel@tonic-gate id_t lwpid = Lsp->pr_lwpid; 9290Sstevel@tonic-gate #if defined(__sparc) 9300Sstevel@tonic-gate prgreg_t tref = Lsp->pr_reg[R_G7]; 9310Sstevel@tonic-gate #elif defined(__amd64) 9320Sstevel@tonic-gate prgreg_t tref = (data_model == PR_MODEL_LP64) ? 9330Sstevel@tonic-gate Lsp->pr_reg[REG_FS] : Lsp->pr_reg[REG_GS]; 9340Sstevel@tonic-gate #elif defined(__i386) 9350Sstevel@tonic-gate prgreg_t tref = Lsp->pr_reg[GS]; 9360Sstevel@tonic-gate #endif 9370Sstevel@tonic-gate td_thrhandle_t th; 9380Sstevel@tonic-gate td_thrinfo_t thrinfo; 9390Sstevel@tonic-gate td_err_e error; 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate if (Thr_agent == NULL) { 9420Sstevel@tonic-gate Stk->tref = 0; 9430Sstevel@tonic-gate Stk->tid = 0; 9440Sstevel@tonic-gate Stk->nthr_create = 0; 9450Sstevel@tonic-gate return; 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate /* 9490Sstevel@tonic-gate * Shortcut here -- 9500Sstevel@tonic-gate * If we have a matching tref and no new threads have 9510Sstevel@tonic-gate * been created since the last time we encountered this 9520Sstevel@tonic-gate * stack, then we don't have to go through the overhead 9530Sstevel@tonic-gate * of calling td_ta_map_lwp2thr() to get the thread-id. 9540Sstevel@tonic-gate */ 9550Sstevel@tonic-gate if (tref == Stk->tref && Stk->nthr_create == nthr_create) 9560Sstevel@tonic-gate return; 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) { 9590Sstevel@tonic-gate if (hflag) 9600Sstevel@tonic-gate (void) fprintf(stderr, 9617330SRoger.Faulkner@Sun.COM "cannot get thread handle for " 9627330SRoger.Faulkner@Sun.COM "lwp#%d, error=%d, tref=0x%.8lx\n", 9637330SRoger.Faulkner@Sun.COM (int)lwpid, error, (long)tref); 9640Sstevel@tonic-gate Stk->tref = 0; 9650Sstevel@tonic-gate Stk->tid = 0; 9660Sstevel@tonic-gate Stk->nthr_create = 0; 9670Sstevel@tonic-gate } else if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) { 9680Sstevel@tonic-gate if (hflag) 9690Sstevel@tonic-gate (void) fprintf(stderr, 9707330SRoger.Faulkner@Sun.COM "cannot get thread info for " 9717330SRoger.Faulkner@Sun.COM "lwp#%d, error=%d, tref=0x%.8lx\n", 9727330SRoger.Faulkner@Sun.COM (int)lwpid, error, (long)tref); 9730Sstevel@tonic-gate Stk->tref = 0; 9740Sstevel@tonic-gate Stk->tid = 0; 9750Sstevel@tonic-gate Stk->nthr_create = 0; 9760Sstevel@tonic-gate } else { 9770Sstevel@tonic-gate Stk->tref = tref; 9780Sstevel@tonic-gate Stk->tid = thrinfo.ti_tid; 9790Sstevel@tonic-gate Stk->nthr_create = nthr_create; 9800Sstevel@tonic-gate } 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate struct callstack * 9840Sstevel@tonic-gate callstack_info(uintptr_t sp, uintptr_t fp, int makeid) 9850Sstevel@tonic-gate { 9860Sstevel@tonic-gate struct callstack *Stk; 9870Sstevel@tonic-gate uintptr_t trash; 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate if (sp == 0 || 9900Sstevel@tonic-gate Pread(Proc, &trash, sizeof (trash), sp) != sizeof (trash)) 9910Sstevel@tonic-gate return (NULL); 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate for (Stk = callstack; Stk != NULL; Stk = Stk->next) 9940Sstevel@tonic-gate if (sp >= Stk->stkbase && sp < Stk->stkend) 9950Sstevel@tonic-gate break; 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate /* 9980Sstevel@tonic-gate * If we didn't find the stack, do it the hard way. 9990Sstevel@tonic-gate */ 10000Sstevel@tonic-gate if (Stk == NULL) { 10010Sstevel@tonic-gate uintptr_t stkbase = sp; 10020Sstevel@tonic-gate uintptr_t stkend; 10030Sstevel@tonic-gate uint_t minsize; 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate #if defined(i386) || defined(__amd64) 10060Sstevel@tonic-gate #ifdef _LP64 10070Sstevel@tonic-gate if (data_model == PR_MODEL_LP64) 10080Sstevel@tonic-gate minsize = 2 * sizeof (uintptr_t); /* fp + pc */ 10090Sstevel@tonic-gate else 10100Sstevel@tonic-gate #endif 10110Sstevel@tonic-gate minsize = 2 * sizeof (uint32_t); 10120Sstevel@tonic-gate #else 10130Sstevel@tonic-gate #ifdef _LP64 10140Sstevel@tonic-gate if (data_model != PR_MODEL_LP64) 10150Sstevel@tonic-gate minsize = SA32(MINFRAME32); 10160Sstevel@tonic-gate else 10170Sstevel@tonic-gate minsize = SA64(MINFRAME64); 10180Sstevel@tonic-gate #else 10190Sstevel@tonic-gate minsize = SA(MINFRAME); 10200Sstevel@tonic-gate #endif 10210Sstevel@tonic-gate #endif /* i386 */ 10220Sstevel@tonic-gate stkend = sp + minsize; 10230Sstevel@tonic-gate 10240Sstevel@tonic-gate while (Stk == NULL && fp != 0 && fp >= sp) { 10250Sstevel@tonic-gate stkend = fp + minsize; 10260Sstevel@tonic-gate for (Stk = callstack; Stk != NULL; Stk = Stk->next) 10270Sstevel@tonic-gate if ((fp >= Stk->stkbase && fp < Stk->stkend) || 10280Sstevel@tonic-gate (stkend > Stk->stkbase && 10290Sstevel@tonic-gate stkend <= Stk->stkend)) 10300Sstevel@tonic-gate break; 10310Sstevel@tonic-gate if (Stk == NULL) 10320Sstevel@tonic-gate fp = previous_fp(fp, NULL); 10330Sstevel@tonic-gate } 10340Sstevel@tonic-gate 10350Sstevel@tonic-gate if (Stk != NULL) /* the stack grew */ 10360Sstevel@tonic-gate Stk->stkbase = stkbase; 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate if (Stk == NULL && makeid) /* new stack */ 10400Sstevel@tonic-gate Stk = find_stack(sp); 10410Sstevel@tonic-gate 10420Sstevel@tonic-gate if (Stk == NULL) 10430Sstevel@tonic-gate return (NULL); 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate /* 10460Sstevel@tonic-gate * Ensure that there is room for at least one more entry. 10470Sstevel@tonic-gate */ 10480Sstevel@tonic-gate if (Stk->ncall == Stk->maxcall) { 10490Sstevel@tonic-gate Stk->maxcall *= 2; 10500Sstevel@tonic-gate Stk->stack = my_realloc(Stk->stack, 10510Sstevel@tonic-gate Stk->maxcall * sizeof (*Stk->stack), NULL); 10520Sstevel@tonic-gate } 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate if (makeid) 10550Sstevel@tonic-gate get_tid(Stk); 10560Sstevel@tonic-gate 10570Sstevel@tonic-gate return (Stk); 10580Sstevel@tonic-gate } 10590Sstevel@tonic-gate 10600Sstevel@tonic-gate /* 10610Sstevel@tonic-gate * Reset the breakpoint information (called on successful exec()). 10620Sstevel@tonic-gate */ 10630Sstevel@tonic-gate void 10640Sstevel@tonic-gate reset_breakpoints(void) 10650Sstevel@tonic-gate { 10660Sstevel@tonic-gate struct dynlib *Dp; 10670Sstevel@tonic-gate struct bkpt *Bp; 10680Sstevel@tonic-gate struct callstack *Stk; 10690Sstevel@tonic-gate int i; 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate if (Dynpat == NULL) 10720Sstevel@tonic-gate return; 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate /* destroy all previous dynamic library information */ 1075*7675SEdward.Pilatowicz@Sun.COM while ((Dp = Dynlib) != NULL) { 1076*7675SEdward.Pilatowicz@Sun.COM Dynlib = Dp->next; 10770Sstevel@tonic-gate free(Dp->lib_name); 10780Sstevel@tonic-gate free(Dp->match_name); 10790Sstevel@tonic-gate free(Dp->prt_name); 10800Sstevel@tonic-gate free(Dp); 10810Sstevel@tonic-gate } 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate /* destroy all previous breakpoint trap information */ 10840Sstevel@tonic-gate if (bpt_hashtable != NULL) { 10850Sstevel@tonic-gate for (i = 0; i < HASHSZ; i++) { 10860Sstevel@tonic-gate while ((Bp = bpt_hashtable[i]) != NULL) { 10870Sstevel@tonic-gate bpt_hashtable[i] = Bp->next; 10880Sstevel@tonic-gate if (Bp->sym_name) 10890Sstevel@tonic-gate free(Bp->sym_name); 10900Sstevel@tonic-gate free(Bp); 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate } 10930Sstevel@tonic-gate } 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate /* destroy all the callstack information */ 10960Sstevel@tonic-gate while ((Stk = callstack) != NULL) { 10970Sstevel@tonic-gate callstack = Stk->next; 10980Sstevel@tonic-gate free(Stk->stack); 10990Sstevel@tonic-gate free(Stk); 11000Sstevel@tonic-gate } 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate /* we are not a multi-threaded process anymore */ 11030Sstevel@tonic-gate if (Thr_agent != NULL) 11040Sstevel@tonic-gate (void) td_ta_delete(Thr_agent); 11050Sstevel@tonic-gate Thr_agent = NULL; 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate /* tell libproc to clear out its mapping information */ 11080Sstevel@tonic-gate Preset_maps(Proc); 11090Sstevel@tonic-gate Rdb_agent = NULL; 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate /* Reestablish the symbols from the executable */ 11120Sstevel@tonic-gate (void) establish_breakpoints(); 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate /* 11160Sstevel@tonic-gate * Clear breakpoints from the process (called before Prelease()). 11170Sstevel@tonic-gate * Don't actually destroy the breakpoint table; 11180Sstevel@tonic-gate * threads currently fielding breakpoints will need it. 11190Sstevel@tonic-gate */ 11200Sstevel@tonic-gate void 11210Sstevel@tonic-gate clear_breakpoints(void) 11220Sstevel@tonic-gate { 11230Sstevel@tonic-gate struct bkpt *Bp; 11240Sstevel@tonic-gate int i; 11250Sstevel@tonic-gate 11260Sstevel@tonic-gate if (Dynpat == NULL) 11270Sstevel@tonic-gate return; 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate /* 11300Sstevel@tonic-gate * Change all breakpoint traps back to normal instructions. 11310Sstevel@tonic-gate * We attempt to remove a breakpoint from every address which 11320Sstevel@tonic-gate * may have ever contained a breakpoint to protect our victims. 11330Sstevel@tonic-gate */ 11340Sstevel@tonic-gate report_htable_stats(); /* report stats first */ 11350Sstevel@tonic-gate for (i = 0; i < HASHSZ; i++) { 11360Sstevel@tonic-gate for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) { 11370Sstevel@tonic-gate if (Bp->flags & BPT_ACTIVE) 11380Sstevel@tonic-gate (void) Pdelbkpt(Proc, Bp->addr, Bp->instr); 11390Sstevel@tonic-gate Bp->flags &= ~BPT_ACTIVE; 11400Sstevel@tonic-gate } 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate if (Thr_agent != NULL) { 11440Sstevel@tonic-gate td_thr_events_t events; 11450Sstevel@tonic-gate 11467330SRoger.Faulkner@Sun.COM td_event_fillset(&events); 11477330SRoger.Faulkner@Sun.COM (void) td_ta_clear_event(Thr_agent, &events); 11480Sstevel@tonic-gate (void) td_ta_delete(Thr_agent); 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate Thr_agent = NULL; 11510Sstevel@tonic-gate } 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate /* 11540Sstevel@tonic-gate * Reestablish the breakpoint traps in the process. 11550Sstevel@tonic-gate * Called after resuming from a vfork() in the parent. 11560Sstevel@tonic-gate */ 11570Sstevel@tonic-gate void 11580Sstevel@tonic-gate reestablish_traps(void) 11590Sstevel@tonic-gate { 11600Sstevel@tonic-gate struct bkpt *Bp; 11610Sstevel@tonic-gate ulong_t instr; 11620Sstevel@tonic-gate int i; 11630Sstevel@tonic-gate 11640Sstevel@tonic-gate if (Dynpat == NULL || is_vfork_child) 11650Sstevel@tonic-gate return; 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate for (i = 0; i < HASHSZ; i++) { 11680Sstevel@tonic-gate for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) { 11690Sstevel@tonic-gate if ((Bp->flags & BPT_ACTIVE) && 11700Sstevel@tonic-gate Psetbkpt(Proc, Bp->addr, &instr) != 0) 11710Sstevel@tonic-gate Bp->flags &= ~BPT_ACTIVE; 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate } 11740Sstevel@tonic-gate } 11750Sstevel@tonic-gate 11760Sstevel@tonic-gate void 11770Sstevel@tonic-gate show_function_call(private_t *pri, 11780Sstevel@tonic-gate struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp) 11790Sstevel@tonic-gate { 11800Sstevel@tonic-gate long arg[8]; 11810Sstevel@tonic-gate int narg; 11820Sstevel@tonic-gate int i; 11830Sstevel@tonic-gate 11840Sstevel@tonic-gate narg = get_arguments(arg); 11850Sstevel@tonic-gate make_pname(pri, (Stk != NULL)? Stk->tid : 0); 11860Sstevel@tonic-gate putpname(pri); 11870Sstevel@tonic-gate timestamp(pri); 11880Sstevel@tonic-gate if (Stk != NULL) { 11890Sstevel@tonic-gate for (i = 1; i < Stk->ncall; i++) { 11900Sstevel@tonic-gate (void) fputc(' ', stdout); 11910Sstevel@tonic-gate (void) fputc(' ', stdout); 11920Sstevel@tonic-gate } 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate (void) printf("-> %s%s(", Dp->prt_name, Bp->sym_name); 11950Sstevel@tonic-gate for (i = 0; i < narg; i++) { 11960Sstevel@tonic-gate (void) printf("0x%lx", arg[i]); 11970Sstevel@tonic-gate if (i < narg-1) { 11980Sstevel@tonic-gate (void) fputc(',', stdout); 11990Sstevel@tonic-gate (void) fputc(' ', stdout); 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate } 12020Sstevel@tonic-gate (void) printf(")\n"); 12030Sstevel@tonic-gate Flush(); 12040Sstevel@tonic-gate } 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate /* ARGSUSED */ 12070Sstevel@tonic-gate void 12080Sstevel@tonic-gate show_function_return(private_t *pri, long rval, int stret, 12090Sstevel@tonic-gate struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp) 12100Sstevel@tonic-gate { 12110Sstevel@tonic-gate int i; 12120Sstevel@tonic-gate 12130Sstevel@tonic-gate make_pname(pri, Stk->tid); 12140Sstevel@tonic-gate putpname(pri); 12150Sstevel@tonic-gate timestamp(pri); 12160Sstevel@tonic-gate for (i = 0; i < Stk->ncall; i++) { 12170Sstevel@tonic-gate (void) fputc(' ', stdout); 12180Sstevel@tonic-gate (void) fputc(' ', stdout); 12190Sstevel@tonic-gate } 12200Sstevel@tonic-gate (void) printf("<- %s%s() = ", Dp->prt_name, Bp->sym_name); 12210Sstevel@tonic-gate if (stret) { 12220Sstevel@tonic-gate (void) printf("struct return\n"); 12230Sstevel@tonic-gate } else if (data_model == PR_MODEL_LP64) { 12240Sstevel@tonic-gate if (rval >= (64 * 1024) || -rval >= (64 * 1024)) 12250Sstevel@tonic-gate (void) printf("0x%lx\n", rval); 12260Sstevel@tonic-gate else 12270Sstevel@tonic-gate (void) printf("%ld\n", rval); 12280Sstevel@tonic-gate } else { 12290Sstevel@tonic-gate int rval32 = (int)rval; 12300Sstevel@tonic-gate if (rval32 >= (64 * 1024) || -rval32 >= (64 * 1024)) 12310Sstevel@tonic-gate (void) printf("0x%x\n", rval32); 12320Sstevel@tonic-gate else 12330Sstevel@tonic-gate (void) printf("%d\n", rval32); 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate Flush(); 12360Sstevel@tonic-gate } 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate /* 12390Sstevel@tonic-gate * Called to deal with function-call tracing. 12400Sstevel@tonic-gate * Return 0 on normal success, 1 to indicate a BPT_HANG success, 12410Sstevel@tonic-gate * and -1 on failure (not tracing functions or unknown breakpoint). 12420Sstevel@tonic-gate */ 12430Sstevel@tonic-gate int 12440Sstevel@tonic-gate function_trace(private_t *pri, int first, int clear, int dotrace) 12450Sstevel@tonic-gate { 12460Sstevel@tonic-gate struct ps_lwphandle *Lwp = pri->Lwp; 12470Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat; 12480Sstevel@tonic-gate uintptr_t pc = Lsp->pr_reg[R_PC]; 12490Sstevel@tonic-gate uintptr_t sp = Lsp->pr_reg[R_SP]; 12500Sstevel@tonic-gate uintptr_t fp = Lsp->pr_reg[R_FP]; 12510Sstevel@tonic-gate struct bkpt *Bp; 12520Sstevel@tonic-gate struct dynlib *Dp; 12530Sstevel@tonic-gate struct callstack *Stk; 12540Sstevel@tonic-gate ulong_t instr; 12550Sstevel@tonic-gate int active; 12560Sstevel@tonic-gate int rval = 0; 12570Sstevel@tonic-gate 12580Sstevel@tonic-gate if (Dynpat == NULL) 12590Sstevel@tonic-gate return (-1); 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate if (data_model != PR_MODEL_LP64) { 12620Sstevel@tonic-gate pc = (uint32_t)pc; 12630Sstevel@tonic-gate sp = (uint32_t)sp; 12640Sstevel@tonic-gate fp = (uint32_t)fp; 12650Sstevel@tonic-gate } 12660Sstevel@tonic-gate 12670Sstevel@tonic-gate if ((Bp = get_bkpt(pc)) == NULL) { 12680Sstevel@tonic-gate if (hflag) 12690Sstevel@tonic-gate (void) fprintf(stderr, 12707330SRoger.Faulkner@Sun.COM "function_trace(): " 12717330SRoger.Faulkner@Sun.COM "cannot find breakpoint for pc: 0x%.8lx\n", 12727330SRoger.Faulkner@Sun.COM (ulong_t)pc); 12730Sstevel@tonic-gate return (-1); 12740Sstevel@tonic-gate } 12750Sstevel@tonic-gate 12760Sstevel@tonic-gate if ((Bp->flags & (BPT_PREINIT|BPT_POSTINIT|BPT_DLACTIVITY)) && !clear) { 12770Sstevel@tonic-gate rd_event_msg_t event_msg; 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate if (hflag) { 12800Sstevel@tonic-gate if (Bp->flags & BPT_PREINIT) 12810Sstevel@tonic-gate (void) fprintf(stderr, "function_trace(): " 12827330SRoger.Faulkner@Sun.COM "RD_PREINIT breakpoint\n"); 12830Sstevel@tonic-gate if (Bp->flags & BPT_POSTINIT) 12840Sstevel@tonic-gate (void) fprintf(stderr, "function_trace(): " 12857330SRoger.Faulkner@Sun.COM "RD_POSTINIT breakpoint\n"); 12860Sstevel@tonic-gate if (Bp->flags & BPT_DLACTIVITY) 12870Sstevel@tonic-gate (void) fprintf(stderr, "function_trace(): " 12887330SRoger.Faulkner@Sun.COM "RD_DLACTIVITY breakpoint\n"); 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate if (rd_event_getmsg(Rdb_agent, &event_msg) == RD_OK) { 12910Sstevel@tonic-gate if (event_msg.type == RD_DLACTIVITY) { 12922192Sraf switch (event_msg.u.state) { 12932192Sraf case RD_CONSISTENT: 12940Sstevel@tonic-gate establish_breakpoints(); 12952192Sraf break; 12962192Sraf case RD_ADD: 12970Sstevel@tonic-gate not_consist = TRUE; /* kludge */ 12980Sstevel@tonic-gate establish_breakpoints(); 12990Sstevel@tonic-gate not_consist = FALSE; 13002192Sraf break; 13012192Sraf case RD_DELETE: 13022192Sraf delete_library = TRUE; 13032192Sraf break; 13042192Sraf default: 13052192Sraf break; 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate } 13080Sstevel@tonic-gate if (hflag) { 13090Sstevel@tonic-gate const char *et; 13100Sstevel@tonic-gate char buf[32]; 13110Sstevel@tonic-gate 13120Sstevel@tonic-gate switch (event_msg.type) { 13130Sstevel@tonic-gate case RD_NONE: 13140Sstevel@tonic-gate et = "RD_NONE"; 13150Sstevel@tonic-gate break; 13160Sstevel@tonic-gate case RD_PREINIT: 13170Sstevel@tonic-gate et = "RD_PREINIT"; 13180Sstevel@tonic-gate break; 13190Sstevel@tonic-gate case RD_POSTINIT: 13200Sstevel@tonic-gate et = "RD_POSTINIT"; 13210Sstevel@tonic-gate break; 13220Sstevel@tonic-gate case RD_DLACTIVITY: 13230Sstevel@tonic-gate et = "RD_DLACTIVITY"; 13240Sstevel@tonic-gate break; 13250Sstevel@tonic-gate default: 13260Sstevel@tonic-gate (void) sprintf(buf, "0x%x", 13277330SRoger.Faulkner@Sun.COM event_msg.type); 13280Sstevel@tonic-gate et = buf; 13290Sstevel@tonic-gate break; 13300Sstevel@tonic-gate } 13310Sstevel@tonic-gate (void) fprintf(stderr, 13327330SRoger.Faulkner@Sun.COM "event_msg.type = %s ", et); 13330Sstevel@tonic-gate switch (event_msg.u.state) { 13340Sstevel@tonic-gate case RD_NOSTATE: 13350Sstevel@tonic-gate et = "RD_NOSTATE"; 13360Sstevel@tonic-gate break; 13370Sstevel@tonic-gate case RD_CONSISTENT: 13380Sstevel@tonic-gate et = "RD_CONSISTENT"; 13390Sstevel@tonic-gate break; 13400Sstevel@tonic-gate case RD_ADD: 13410Sstevel@tonic-gate et = "RD_ADD"; 13420Sstevel@tonic-gate break; 13430Sstevel@tonic-gate case RD_DELETE: 13440Sstevel@tonic-gate et = "RD_DELETE"; 13450Sstevel@tonic-gate break; 13460Sstevel@tonic-gate default: 13470Sstevel@tonic-gate (void) sprintf(buf, "0x%x", 13487330SRoger.Faulkner@Sun.COM event_msg.u.state); 13490Sstevel@tonic-gate et = buf; 13500Sstevel@tonic-gate break; 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate (void) fprintf(stderr, 13537330SRoger.Faulkner@Sun.COM "event_msg.u.state = %s\n", et); 13540Sstevel@tonic-gate } 13550Sstevel@tonic-gate } 13560Sstevel@tonic-gate } 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate if ((Bp->flags & BPT_TD_CREATE) && !clear) { 13590Sstevel@tonic-gate nthr_create++; 13600Sstevel@tonic-gate if (hflag) 13610Sstevel@tonic-gate (void) fprintf(stderr, "function_trace(): " 13627330SRoger.Faulkner@Sun.COM "BPT_TD_CREATE breakpoint\n"); 13630Sstevel@tonic-gate /* we don't care about the event message */ 13640Sstevel@tonic-gate } 13650Sstevel@tonic-gate 13660Sstevel@tonic-gate Dp = Bp->dyn; 13670Sstevel@tonic-gate 13680Sstevel@tonic-gate if (dotrace) { 13690Sstevel@tonic-gate if ((Stk = callstack_info(sp, fp, 1)) == NULL) { 13700Sstevel@tonic-gate if (Dp != NULL && !clear) { 13710Sstevel@tonic-gate if (cflag) { 13720Sstevel@tonic-gate add_fcall(fcall_tbl, Dp->prt_name, 13730Sstevel@tonic-gate Bp->sym_name, (unsigned long)1); 13740Sstevel@tonic-gate } 13750Sstevel@tonic-gate else 13760Sstevel@tonic-gate show_function_call(pri, NULL, Dp, Bp); 13770Sstevel@tonic-gate if ((Bp->flags & BPT_HANG) && !first) 13780Sstevel@tonic-gate rval = 1; 13790Sstevel@tonic-gate } 13800Sstevel@tonic-gate } else if (!clear) { 13810Sstevel@tonic-gate if (Dp != NULL) { 13820Sstevel@tonic-gate function_entry(pri, Bp, Stk); 13830Sstevel@tonic-gate if ((Bp->flags & BPT_HANG) && !first) 13840Sstevel@tonic-gate rval = 1; 13850Sstevel@tonic-gate } else { 13860Sstevel@tonic-gate function_return(pri, Stk); 13870Sstevel@tonic-gate } 13880Sstevel@tonic-gate } 13890Sstevel@tonic-gate } 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate /* 13920Sstevel@tonic-gate * Single-step the traced instruction. Since it's possible that 13930Sstevel@tonic-gate * another thread has deactivated this breakpoint, we indicate 13940Sstevel@tonic-gate * that we have reactivated it by virtue of executing it. 13950Sstevel@tonic-gate * 13960Sstevel@tonic-gate * To avoid a deadlock with some other thread in the process 13970Sstevel@tonic-gate * performing a fork() or a thr_suspend() operation, we must 13980Sstevel@tonic-gate * drop and later reacquire truss_lock. Some fancy dancing here. 13990Sstevel@tonic-gate */ 14000Sstevel@tonic-gate active = (Bp->flags & BPT_ACTIVE); 14010Sstevel@tonic-gate Bp->flags |= BPT_ACTIVE; 14020Sstevel@tonic-gate instr = Bp->instr; 14030Sstevel@tonic-gate (void) mutex_unlock(&truss_lock); 14040Sstevel@tonic-gate (void) Lxecbkpt(Lwp, instr); 14050Sstevel@tonic-gate (void) mutex_lock(&truss_lock); 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate if (rval || clear) { /* leave process stopped and abandoned */ 14080Sstevel@tonic-gate #if defined(__i386) 14090Sstevel@tonic-gate /* 14100Sstevel@tonic-gate * Leave it stopped in a state that a stack trace is reasonable. 14110Sstevel@tonic-gate */ 14120Sstevel@tonic-gate /* XX64 needs to be updated for amd64 & gcc */ 14130Sstevel@tonic-gate if (rval && instr == 0x55) { /* pushl %ebp */ 14140Sstevel@tonic-gate /* step it over the movl %esp,%ebp */ 14150Sstevel@tonic-gate (void) mutex_unlock(&truss_lock); 14160Sstevel@tonic-gate (void) Lsetrun(Lwp, 0, PRCFAULT|PRSTEP); 14170Sstevel@tonic-gate /* we're wrapping up; wait one second at most */ 14180Sstevel@tonic-gate (void) Lwait(Lwp, MILLISEC); 14190Sstevel@tonic-gate (void) mutex_lock(&truss_lock); 14200Sstevel@tonic-gate } 14210Sstevel@tonic-gate #endif 14220Sstevel@tonic-gate if (get_bkpt(pc) != Bp) 14230Sstevel@tonic-gate abend("function_trace: lost breakpoint", NULL); 14240Sstevel@tonic-gate (void) Pdelbkpt(Proc, Bp->addr, Bp->instr); 14250Sstevel@tonic-gate Bp->flags &= ~BPT_ACTIVE; 14260Sstevel@tonic-gate (void) mutex_unlock(&truss_lock); 14270Sstevel@tonic-gate (void) Lsetrun(Lwp, 0, PRCFAULT|PRSTOP); 14280Sstevel@tonic-gate /* we're wrapping up; wait one second at most */ 14290Sstevel@tonic-gate (void) Lwait(Lwp, MILLISEC); 14300Sstevel@tonic-gate (void) mutex_lock(&truss_lock); 14310Sstevel@tonic-gate } else { 14320Sstevel@tonic-gate if (get_bkpt(pc) != Bp) 14330Sstevel@tonic-gate abend("function_trace: lost breakpoint", NULL); 14340Sstevel@tonic-gate if (!active || !(Bp->flags & BPT_ACTIVE)) { 14350Sstevel@tonic-gate (void) Pdelbkpt(Proc, Bp->addr, Bp->instr); 14360Sstevel@tonic-gate Bp->flags &= ~BPT_ACTIVE; 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate } 14390Sstevel@tonic-gate return (rval); 14400Sstevel@tonic-gate } 14410Sstevel@tonic-gate 14420Sstevel@tonic-gate void 14430Sstevel@tonic-gate function_entry(private_t *pri, struct bkpt *Bp, struct callstack *Stk) 14440Sstevel@tonic-gate { 14450Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat; 14460Sstevel@tonic-gate uintptr_t sp = Lsp->pr_reg[R_SP]; 14470Sstevel@tonic-gate uintptr_t rpc = get_return_address(&sp); 14480Sstevel@tonic-gate struct dynlib *Dp = Bp->dyn; 14490Sstevel@tonic-gate int oldframe = FALSE; 14500Sstevel@tonic-gate int i; 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate #ifdef _LP64 14530Sstevel@tonic-gate if (data_model != PR_MODEL_LP64) { 14540Sstevel@tonic-gate sp = (uint32_t)sp; 14550Sstevel@tonic-gate rpc = (uint32_t)rpc; 14560Sstevel@tonic-gate } 14570Sstevel@tonic-gate #endif 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate /* 14600Sstevel@tonic-gate * If the sp is not within the stack bounds, forget it. 14610Sstevel@tonic-gate * If the symbol's 'internal' flag is false, 14620Sstevel@tonic-gate * don't report internal calls within the library. 14630Sstevel@tonic-gate */ 14640Sstevel@tonic-gate if (!(sp >= Stk->stkbase && sp < Stk->stkend) || 14650Sstevel@tonic-gate (!(Bp->flags & BPT_INTERNAL) && 14660Sstevel@tonic-gate rpc >= Dp->base && rpc < Dp->base + Dp->size)) 14670Sstevel@tonic-gate return; 14680Sstevel@tonic-gate 14690Sstevel@tonic-gate for (i = 0; i < Stk->ncall; i++) { 14700Sstevel@tonic-gate if (sp >= Stk->stack[i].sp) { 14710Sstevel@tonic-gate Stk->ncall = i; 14720Sstevel@tonic-gate if (sp == Stk->stack[i].sp) 14730Sstevel@tonic-gate oldframe = TRUE; 14740Sstevel@tonic-gate break; 14750Sstevel@tonic-gate } 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate /* 14790Sstevel@tonic-gate * Breakpoints for function returns are set here 14800Sstevel@tonic-gate * If we're counting function calls, there is no need to set 14810Sstevel@tonic-gate * a breakpoint upon return 14820Sstevel@tonic-gate */ 14830Sstevel@tonic-gate 14840Sstevel@tonic-gate if (!oldframe && !cflag) { 14850Sstevel@tonic-gate (void) create_bkpt(rpc, 1, 1); /* may or may not be set */ 14860Sstevel@tonic-gate Stk->stack[Stk->ncall].sp = sp; /* record it anyeay */ 14870Sstevel@tonic-gate Stk->stack[Stk->ncall].pc = rpc; 14880Sstevel@tonic-gate Stk->stack[Stk->ncall].fcn = Bp; 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate Stk->ncall++; 14910Sstevel@tonic-gate if (cflag) { 14920Sstevel@tonic-gate add_fcall(fcall_tbl, Dp->prt_name, Bp->sym_name, 14930Sstevel@tonic-gate (unsigned long)1); 14940Sstevel@tonic-gate } else { 14950Sstevel@tonic-gate show_function_call(pri, Stk, Dp, Bp); 14960Sstevel@tonic-gate } 14970Sstevel@tonic-gate } 14980Sstevel@tonic-gate 14990Sstevel@tonic-gate /* 15000Sstevel@tonic-gate * We are here because we hit an unnamed breakpoint. 15010Sstevel@tonic-gate * Attempt to match this up with a return pc on the stack 15020Sstevel@tonic-gate * and report the function return. 15030Sstevel@tonic-gate */ 15040Sstevel@tonic-gate void 15050Sstevel@tonic-gate function_return(private_t *pri, struct callstack *Stk) 15060Sstevel@tonic-gate { 15070Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat; 15080Sstevel@tonic-gate uintptr_t sp = Lsp->pr_reg[R_SP]; 15090Sstevel@tonic-gate uintptr_t fp = Lsp->pr_reg[R_FP]; 15100Sstevel@tonic-gate int i; 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate #ifdef _LP64 15130Sstevel@tonic-gate if (data_model != PR_MODEL_LP64) { 15140Sstevel@tonic-gate sp = (uint32_t)sp; 15150Sstevel@tonic-gate fp = (uint32_t)fp; 15160Sstevel@tonic-gate } 15170Sstevel@tonic-gate #endif 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate if (fp < sp + 8) 15200Sstevel@tonic-gate fp = sp + 8; 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate for (i = Stk->ncall - 1; i >= 0; i--) { 15230Sstevel@tonic-gate if (sp <= Stk->stack[i].sp && fp > Stk->stack[i].sp) { 15240Sstevel@tonic-gate Stk->ncall = i; 15250Sstevel@tonic-gate break; 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate 15290Sstevel@tonic-gate #if defined(i386) || defined(__amd64) 15300Sstevel@tonic-gate if (i < 0) { 15310Sstevel@tonic-gate /* probably __mul64() or friends -- try harder */ 15320Sstevel@tonic-gate int j; 15330Sstevel@tonic-gate for (j = 0; i < 0 && j < 8; j++) { /* up to 8 args */ 15340Sstevel@tonic-gate sp -= 4; 15350Sstevel@tonic-gate for (i = Stk->ncall - 1; i >= 0; i--) { 15360Sstevel@tonic-gate if (sp <= Stk->stack[i].sp && 15370Sstevel@tonic-gate fp > Stk->stack[i].sp) { 15380Sstevel@tonic-gate Stk->ncall = i; 15390Sstevel@tonic-gate break; 15400Sstevel@tonic-gate } 15410Sstevel@tonic-gate } 15420Sstevel@tonic-gate } 15430Sstevel@tonic-gate } 15440Sstevel@tonic-gate #endif 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate if ((i >= 0) && (!cflag)) { 15470Sstevel@tonic-gate show_function_return(pri, Lsp->pr_reg[R_R0], 0, 15487330SRoger.Faulkner@Sun.COM Stk, Stk->stack[i].fcn->dyn, Stk->stack[i].fcn); 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate 15520Sstevel@tonic-gate #if defined(__sparc) 15530Sstevel@tonic-gate #define FPADJUST 0 15540Sstevel@tonic-gate #elif defined(__amd64) 15550Sstevel@tonic-gate #define FPADJUST 8 15560Sstevel@tonic-gate #elif defined(__i386) 15570Sstevel@tonic-gate #define FPADJUST 4 15580Sstevel@tonic-gate #endif 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate void 15610Sstevel@tonic-gate trap_one_stack(prgregset_t reg) 15620Sstevel@tonic-gate { 15630Sstevel@tonic-gate struct dynlib *Dp; 15640Sstevel@tonic-gate struct bkpt *Bp; 15650Sstevel@tonic-gate struct callstack *Stk; 15660Sstevel@tonic-gate GElf_Sym sym; 15670Sstevel@tonic-gate char sym_name[32]; 15680Sstevel@tonic-gate uintptr_t sp = reg[R_SP]; 15690Sstevel@tonic-gate uintptr_t pc = reg[R_PC]; 15700Sstevel@tonic-gate uintptr_t fp; 15710Sstevel@tonic-gate uintptr_t rpc; 15720Sstevel@tonic-gate uint_t nframe = 0; 15730Sstevel@tonic-gate uint_t maxframe = 8; 15740Sstevel@tonic-gate struct { 15750Sstevel@tonic-gate uintptr_t sp; /* %sp within called function */ 15760Sstevel@tonic-gate uintptr_t pc; /* %pc within called function */ 15770Sstevel@tonic-gate uintptr_t rsp; /* the return sp */ 15780Sstevel@tonic-gate uintptr_t rpc; /* the return pc */ 15790Sstevel@tonic-gate } *frame = my_malloc(maxframe * sizeof (*frame), NULL); 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate /* 15820Sstevel@tonic-gate * Gather stack frames bottom to top. 15830Sstevel@tonic-gate */ 15840Sstevel@tonic-gate while (sp != 0) { 15850Sstevel@tonic-gate fp = sp; /* remember higest non-null sp */ 15860Sstevel@tonic-gate frame[nframe].sp = sp; 15870Sstevel@tonic-gate frame[nframe].pc = pc; 15880Sstevel@tonic-gate sp = previous_fp(sp, &pc); 15890Sstevel@tonic-gate frame[nframe].rsp = sp; 15900Sstevel@tonic-gate frame[nframe].rpc = pc; 15910Sstevel@tonic-gate if (++nframe == maxframe) { 15920Sstevel@tonic-gate maxframe *= 2; 15930Sstevel@tonic-gate frame = my_realloc(frame, maxframe * sizeof (*frame), 15947330SRoger.Faulkner@Sun.COM NULL); 15950Sstevel@tonic-gate } 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate /* 15990Sstevel@tonic-gate * Scan for function return breakpoints top to bottom. 16000Sstevel@tonic-gate */ 16010Sstevel@tonic-gate while (nframe--) { 16020Sstevel@tonic-gate /* lookup the called function in the symbol tables */ 16030Sstevel@tonic-gate if (Plookup_by_addr(Proc, frame[nframe].pc, sym_name, 16040Sstevel@tonic-gate sizeof (sym_name), &sym) != 0) 16050Sstevel@tonic-gate continue; 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate pc = sym.st_value; /* entry point of the function */ 16080Sstevel@tonic-gate rpc = frame[nframe].rpc; /* caller's return pc */ 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate /* lookup the function in the breakpoint table */ 16110Sstevel@tonic-gate if ((Bp = get_bkpt(pc)) == NULL || (Dp = Bp->dyn) == NULL) 16120Sstevel@tonic-gate continue; 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate if (!(Bp->flags & BPT_INTERNAL) && 16150Sstevel@tonic-gate rpc >= Dp->base && rpc < Dp->base + Dp->size) 16160Sstevel@tonic-gate continue; 16170Sstevel@tonic-gate 16180Sstevel@tonic-gate sp = frame[nframe].rsp + FPADJUST; /* %sp at time of call */ 16190Sstevel@tonic-gate if ((Stk = callstack_info(sp, fp, 0)) == NULL) 16200Sstevel@tonic-gate continue; /* can't happen? */ 16210Sstevel@tonic-gate 16220Sstevel@tonic-gate if (create_bkpt(rpc, 1, 1) != NULL) { 16230Sstevel@tonic-gate Stk->stack[Stk->ncall].sp = sp; 16240Sstevel@tonic-gate Stk->stack[Stk->ncall].pc = rpc; 16250Sstevel@tonic-gate Stk->stack[Stk->ncall].fcn = Bp; 16260Sstevel@tonic-gate Stk->ncall++; 16270Sstevel@tonic-gate } 16280Sstevel@tonic-gate } 16290Sstevel@tonic-gate 16300Sstevel@tonic-gate free(frame); 16310Sstevel@tonic-gate } 16320Sstevel@tonic-gate 16330Sstevel@tonic-gate int 16340Sstevel@tonic-gate lwp_stack_traps(void *cd, const lwpstatus_t *Lsp) 16350Sstevel@tonic-gate { 16360Sstevel@tonic-gate ph_map_t *ph_map = (ph_map_t *)cd; 16370Sstevel@tonic-gate prgregset_t reg; 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate (void) memcpy(reg, Lsp->pr_reg, sizeof (prgregset_t)); 16400Sstevel@tonic-gate make_lwp_stack(Lsp, ph_map->pmap, ph_map->nmap); 16410Sstevel@tonic-gate trap_one_stack(reg); 16420Sstevel@tonic-gate 16430Sstevel@tonic-gate return (interrupt | sigusr1); 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate 16460Sstevel@tonic-gate /* ARGSUSED */ 16470Sstevel@tonic-gate int 16480Sstevel@tonic-gate thr_stack_traps(const td_thrhandle_t *Thp, void *cd) 16490Sstevel@tonic-gate { 16500Sstevel@tonic-gate prgregset_t reg; 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate /* 16530Sstevel@tonic-gate * We have already dealt with all the lwps. 16540Sstevel@tonic-gate * We only care about unbound threads here (TD_PARTIALREG). 16550Sstevel@tonic-gate */ 16560Sstevel@tonic-gate if (td_thr_getgregs(Thp, reg) != TD_PARTIALREG) 16570Sstevel@tonic-gate return (0); 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate make_thr_stack(Thp, reg); 16600Sstevel@tonic-gate trap_one_stack(reg); 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate return (interrupt | sigusr1); 16630Sstevel@tonic-gate } 16640Sstevel@tonic-gate 16650Sstevel@tonic-gate #if defined(__sparc) 16660Sstevel@tonic-gate 16670Sstevel@tonic-gate uintptr_t 16680Sstevel@tonic-gate previous_fp(uintptr_t sp, uintptr_t *rpc) 16690Sstevel@tonic-gate { 16700Sstevel@tonic-gate uintptr_t fp = 0; 16710Sstevel@tonic-gate uintptr_t pc = 0; 16720Sstevel@tonic-gate 16730Sstevel@tonic-gate #ifdef _LP64 16740Sstevel@tonic-gate if (data_model == PR_MODEL_LP64) { 16750Sstevel@tonic-gate struct rwindow64 rwin; 16760Sstevel@tonic-gate if (Pread(Proc, &rwin, sizeof (rwin), sp + STACK_BIAS) 16770Sstevel@tonic-gate == sizeof (rwin)) { 16780Sstevel@tonic-gate fp = (uintptr_t)rwin.rw_fp; 16790Sstevel@tonic-gate pc = (uintptr_t)rwin.rw_rtn; 16800Sstevel@tonic-gate } 16810Sstevel@tonic-gate if (fp != 0 && 16820Sstevel@tonic-gate Pread(Proc, &rwin, sizeof (rwin), fp + STACK_BIAS) 16830Sstevel@tonic-gate != sizeof (rwin)) 16840Sstevel@tonic-gate fp = pc = 0; 16850Sstevel@tonic-gate } else { 16860Sstevel@tonic-gate struct rwindow32 rwin; 16870Sstevel@tonic-gate #else /* _LP64 */ 16880Sstevel@tonic-gate struct rwindow rwin; 16890Sstevel@tonic-gate #endif /* _LP64 */ 16900Sstevel@tonic-gate if (Pread(Proc, &rwin, sizeof (rwin), sp) == sizeof (rwin)) { 16910Sstevel@tonic-gate fp = (uint32_t)rwin.rw_fp; 16920Sstevel@tonic-gate pc = (uint32_t)rwin.rw_rtn; 16930Sstevel@tonic-gate } 16940Sstevel@tonic-gate if (fp != 0 && 16950Sstevel@tonic-gate Pread(Proc, &rwin, sizeof (rwin), fp) != sizeof (rwin)) 16960Sstevel@tonic-gate fp = pc = 0; 16970Sstevel@tonic-gate #ifdef _LP64 16980Sstevel@tonic-gate } 16990Sstevel@tonic-gate #endif 17000Sstevel@tonic-gate if (rpc) 17010Sstevel@tonic-gate *rpc = pc; 17020Sstevel@tonic-gate return (fp); 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate /* ARGSUSED */ 17060Sstevel@tonic-gate uintptr_t 17070Sstevel@tonic-gate get_return_address(uintptr_t *psp) 17080Sstevel@tonic-gate { 17090Sstevel@tonic-gate instr_t inst; 17100Sstevel@tonic-gate private_t *pri = get_private(); 17110Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat; 17120Sstevel@tonic-gate uintptr_t rpc; 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate rpc = (uintptr_t)Lsp->pr_reg[R_O7] + 8; 17150Sstevel@tonic-gate if (data_model != PR_MODEL_LP64) 17160Sstevel@tonic-gate rpc = (uint32_t)rpc; 17170Sstevel@tonic-gate 17180Sstevel@tonic-gate /* check for structure return (bletch!) */ 17190Sstevel@tonic-gate if (Pread(Proc, &inst, sizeof (inst), rpc) == sizeof (inst) && 17200Sstevel@tonic-gate inst < 0x1000) 17210Sstevel@tonic-gate rpc += sizeof (instr_t); 17220Sstevel@tonic-gate 17230Sstevel@tonic-gate return (rpc); 17240Sstevel@tonic-gate } 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate int 17270Sstevel@tonic-gate get_arguments(long *argp) 17280Sstevel@tonic-gate { 17290Sstevel@tonic-gate private_t *pri = get_private(); 17300Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat; 17310Sstevel@tonic-gate int i; 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate if (data_model != PR_MODEL_LP64) 17340Sstevel@tonic-gate for (i = 0; i < 4; i++) 17350Sstevel@tonic-gate argp[i] = (uint_t)Lsp->pr_reg[R_O0+i]; 17360Sstevel@tonic-gate else 17370Sstevel@tonic-gate for (i = 0; i < 4; i++) 17380Sstevel@tonic-gate argp[i] = (long)Lsp->pr_reg[R_O0+i]; 17390Sstevel@tonic-gate return (4); 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate 17420Sstevel@tonic-gate #endif /* __sparc */ 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 17450Sstevel@tonic-gate 17460Sstevel@tonic-gate uintptr_t 17470Sstevel@tonic-gate previous_fp(uintptr_t fp, uintptr_t *rpc) 17480Sstevel@tonic-gate { 17490Sstevel@tonic-gate uintptr_t frame[2]; 17500Sstevel@tonic-gate uintptr_t trash[2]; 17510Sstevel@tonic-gate 17520Sstevel@tonic-gate if (Pread(Proc, frame, sizeof (frame), fp) != sizeof (frame) || 17530Sstevel@tonic-gate (frame[0] != 0 && 17540Sstevel@tonic-gate Pread(Proc, trash, sizeof (trash), frame[0]) != sizeof (trash))) 17550Sstevel@tonic-gate frame[0] = frame[1] = 0; 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate if (rpc) 17580Sstevel@tonic-gate *rpc = frame[1]; 17590Sstevel@tonic-gate return (frame[0]); 17600Sstevel@tonic-gate } 17610Sstevel@tonic-gate 17620Sstevel@tonic-gate #endif 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate #if defined(__amd64) || defined(__i386) 17650Sstevel@tonic-gate 17660Sstevel@tonic-gate /* 17670Sstevel@tonic-gate * Examine the instruction at the return location of a function call 17680Sstevel@tonic-gate * and return the byte count by which the stack is adjusted on return. 17690Sstevel@tonic-gate * It the instruction at the return location is an addl, as expected, 17700Sstevel@tonic-gate * then adjust the return pc by the size of that instruction so that 17710Sstevel@tonic-gate * we will place the return breakpoint on the following instruction. 17720Sstevel@tonic-gate * This allows programs that interrogate their own stacks and record 17730Sstevel@tonic-gate * function calls and arguments to work correctly even while we interfere. 17740Sstevel@tonic-gate * Return the count on success, -1 on failure. 17750Sstevel@tonic-gate */ 17760Sstevel@tonic-gate int 17770Sstevel@tonic-gate return_count32(uint32_t *ppc) 17780Sstevel@tonic-gate { 17790Sstevel@tonic-gate uintptr_t pc = *ppc; 17800Sstevel@tonic-gate struct bkpt *Bp; 17810Sstevel@tonic-gate int count; 17820Sstevel@tonic-gate uchar_t instr[6]; /* instruction at pc */ 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate if ((count = Pread(Proc, instr, sizeof (instr), pc)) < 0) 17850Sstevel@tonic-gate return (-1); 17860Sstevel@tonic-gate 17870Sstevel@tonic-gate /* find the replaced instruction at pc (if any) */ 17880Sstevel@tonic-gate if ((Bp = get_bkpt(pc)) != NULL && (Bp->flags & BPT_ACTIVE)) 17890Sstevel@tonic-gate instr[0] = (uchar_t)Bp->instr; 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate if (count != sizeof (instr) && 17920Sstevel@tonic-gate (count < 3 || instr[0] != 0x83)) 17930Sstevel@tonic-gate return (-1); 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate /* 17960Sstevel@tonic-gate * A bit of disassembly of the instruction is required here. 17970Sstevel@tonic-gate */ 17980Sstevel@tonic-gate if (instr[1] != 0xc4) { /* not an addl mumble,%esp inctruction */ 17990Sstevel@tonic-gate count = 0; 18000Sstevel@tonic-gate } else if (instr[0] == 0x81) { /* count is a longword */ 18010Sstevel@tonic-gate count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24); 18020Sstevel@tonic-gate *ppc += 6; 18030Sstevel@tonic-gate } else if (instr[0] == 0x83) { /* count is a byte */ 18040Sstevel@tonic-gate count = instr[2]; 18050Sstevel@tonic-gate *ppc += 3; 18060Sstevel@tonic-gate } else { /* not an addl inctruction */ 18070Sstevel@tonic-gate count = 0; 18080Sstevel@tonic-gate } 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate return (count); 18110Sstevel@tonic-gate } 18120Sstevel@tonic-gate 18130Sstevel@tonic-gate uintptr_t 18140Sstevel@tonic-gate get_return_address32(uintptr_t *psp) 18150Sstevel@tonic-gate { 18160Sstevel@tonic-gate uint32_t sp = *psp; 18170Sstevel@tonic-gate uint32_t rpc; 18180Sstevel@tonic-gate int count; 18190Sstevel@tonic-gate 18200Sstevel@tonic-gate *psp += 4; /* account for popping the stack on return */ 18210Sstevel@tonic-gate if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc)) 18220Sstevel@tonic-gate return (0); 18230Sstevel@tonic-gate if ((count = return_count32(&rpc)) < 0) 18240Sstevel@tonic-gate count = 0; 18250Sstevel@tonic-gate *psp += count; /* expected sp on return */ 18260Sstevel@tonic-gate return (rpc); 18270Sstevel@tonic-gate } 18280Sstevel@tonic-gate 18290Sstevel@tonic-gate uintptr_t 18300Sstevel@tonic-gate get_return_address(uintptr_t *psp) 18310Sstevel@tonic-gate { 18320Sstevel@tonic-gate #ifdef _LP64 18330Sstevel@tonic-gate uintptr_t rpc; 18340Sstevel@tonic-gate uintptr_t sp = *psp; 18350Sstevel@tonic-gate 18360Sstevel@tonic-gate if (data_model == PR_MODEL_LP64) { 18370Sstevel@tonic-gate if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc)) 18380Sstevel@tonic-gate return (0); 18390Sstevel@tonic-gate /* 18400Sstevel@tonic-gate * Ignore arguments pushed on the stack. See comments in 18410Sstevel@tonic-gate * get_arguments(). 18420Sstevel@tonic-gate */ 18430Sstevel@tonic-gate return (rpc); 18440Sstevel@tonic-gate } else 18450Sstevel@tonic-gate #endif 18460Sstevel@tonic-gate return (get_return_address32(psp)); 18470Sstevel@tonic-gate } 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate int 18510Sstevel@tonic-gate get_arguments32(long *argp) 18520Sstevel@tonic-gate { 18530Sstevel@tonic-gate private_t *pri = get_private(); 18540Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat; 18550Sstevel@tonic-gate uint32_t frame[5]; /* return pc + 4 args */ 18560Sstevel@tonic-gate int narg; 18570Sstevel@tonic-gate int count; 18580Sstevel@tonic-gate int i; 18590Sstevel@tonic-gate 18600Sstevel@tonic-gate narg = Pread(Proc, frame, sizeof (frame), 18617330SRoger.Faulkner@Sun.COM (uintptr_t)Lsp->pr_reg[R_SP]); 18620Sstevel@tonic-gate narg -= sizeof (greg32_t); 18630Sstevel@tonic-gate if (narg <= 0) 18640Sstevel@tonic-gate return (0); 18650Sstevel@tonic-gate narg /= sizeof (greg32_t); /* no more than 4 */ 18660Sstevel@tonic-gate 18670Sstevel@tonic-gate /* 18680Sstevel@tonic-gate * Given the return PC, determine the number of arguments. 18690Sstevel@tonic-gate */ 18700Sstevel@tonic-gate if ((count = return_count32(&frame[0])) < 0) 18710Sstevel@tonic-gate narg = 0; 18720Sstevel@tonic-gate else { 18730Sstevel@tonic-gate count /= sizeof (greg32_t); 18740Sstevel@tonic-gate if (narg > count) 18750Sstevel@tonic-gate narg = count; 18760Sstevel@tonic-gate } 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate for (i = 0; i < narg; i++) 18790Sstevel@tonic-gate argp[i] = (long)frame[i+1]; 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate return (narg); 18820Sstevel@tonic-gate } 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate int 18850Sstevel@tonic-gate get_arguments(long *argp) 18860Sstevel@tonic-gate { 18870Sstevel@tonic-gate #ifdef _LP64 18880Sstevel@tonic-gate private_t *pri = get_private(); 18890Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat; 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate if (data_model == PR_MODEL_LP64) { 18920Sstevel@tonic-gate /* 18930Sstevel@tonic-gate * On amd64, we do not know how many arguments are passed to 18940Sstevel@tonic-gate * each function. While it may be possible to detect if we 18950Sstevel@tonic-gate * have more than 6 arguments, it is of marginal value. 18960Sstevel@tonic-gate * Instead, assume that we always have 6 arguments, which are 18970Sstevel@tonic-gate * passed via registers. 18980Sstevel@tonic-gate */ 18990Sstevel@tonic-gate argp[0] = Lsp->pr_reg[REG_RDI]; 19000Sstevel@tonic-gate argp[1] = Lsp->pr_reg[REG_RSI]; 19010Sstevel@tonic-gate argp[2] = Lsp->pr_reg[REG_RDX]; 19020Sstevel@tonic-gate argp[3] = Lsp->pr_reg[REG_RCX]; 19030Sstevel@tonic-gate argp[4] = Lsp->pr_reg[REG_R8]; 19040Sstevel@tonic-gate argp[5] = Lsp->pr_reg[REG_R9]; 19050Sstevel@tonic-gate return (6); 19060Sstevel@tonic-gate } else 19070Sstevel@tonic-gate #endif 19080Sstevel@tonic-gate return (get_arguments32(argp)); 19090Sstevel@tonic-gate } 19100Sstevel@tonic-gate 19110Sstevel@tonic-gate #endif /* __amd64 || __i386 */ 1912