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