#define _SYSCALL32
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <memory.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stack.h>
#include <signal.h>
#include <limits.h>
#include <sys/isa_defs.h>
#include <proc_service.h>
#include <dlfcn.h>
#include <fnmatch.h>
#include <libproc.h>
#include "ramdata.h"
#include "systable.h"
#include "print.h"
#include "proto.h"
#include "htbl.h"
typedef struct {
prmap_t *pmap;
int nmap;
} ph_map_t;
void function_entry(private_t *, struct bkpt *, struct callstack *);
void function_return(private_t *, struct callstack *);
int object_iter(void *, const prmap_t *, const char *);
int object_present(void *, const prmap_t *, const char *);
int symbol_iter(void *, const GElf_Sym *, const char *);
uintptr_t get_return_address(uintptr_t *);
int get_arguments(long *argp);
uintptr_t previous_fp(uintptr_t, uintptr_t *);
int lwp_stack_traps(void *cd, const lwpstatus_t *Lsp);
int thr_stack_traps(const td_thrhandle_t *Thp, void *cd);
struct bkpt *create_bkpt(uintptr_t, int, int);
void set_deferred_breakpoints(void);
#define DEF_MAXCALL 16
#define FAULT_ADDR ((uintptr_t)(0-8))
#define HASHSZ 2048
#define bpt_hash(addr) ((((addr) >> 13) ^ ((addr) >> 2)) & 0x7ff)
static void
setup_thread_agent(void)
{
struct bkpt *Bp;
td_notify_t notify;
td_thr_events_t events;
if (Thr_agent != NULL)
return;
if (td_init() != TD_OK || td_ta_new(Proc, &Thr_agent) != TD_OK)
Thr_agent = NULL;
else {
td_event_emptyset(&events);
td_event_addset(&events, TD_CREATE);
if (td_ta_event_addr(Thr_agent, TD_CREATE, ¬ify) == TD_OK &&
notify.type == NOTIFY_BPT &&
td_ta_set_event(Thr_agent, &events) == TD_OK &&
(Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
Bp->flags |= BPT_TD_CREATE;
}
}
static void
delete_breakpoints(uintptr_t base, size_t size)
{
struct bkpt **Bpp;
struct bkpt *Bp;
int i;
if (bpt_hashtable == NULL)
return;
for (i = 0; i < HASHSZ; i++) {
Bpp = &bpt_hashtable[i];
while ((Bp = *Bpp) != NULL) {
if (Bp->addr < base || Bp->addr >= base + size) {
Bpp = &Bp->next;
continue;
}
*Bpp = Bp->next;
if (Bp->sym_name)
free(Bp->sym_name);
free(Bp);
}
}
}
void
establish_breakpoints(void)
{
if (Dynpat == NULL)
return;
if (bpt_hashtable == NULL) {
bpt_hashtable = my_malloc(HASHSZ * sizeof (struct bkpt *),
NULL);
(void) memset(bpt_hashtable, 0,
HASHSZ * sizeof (struct bkpt *));
}
if (Rdb_agent == NULL &&
(Rdb_agent = Prd_agent(Proc)) != NULL) {
rd_notify_t notify;
struct bkpt *Bp;
(void) rd_event_enable(Rdb_agent, 1);
if (rd_event_addr(Rdb_agent, RD_PREINIT, ¬ify) == RD_OK &&
(Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
Bp->flags |= BPT_PREINIT;
if (rd_event_addr(Rdb_agent, RD_POSTINIT, ¬ify) == RD_OK &&
(Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
Bp->flags |= BPT_POSTINIT;
if (rd_event_addr(Rdb_agent, RD_DLACTIVITY, ¬ify) == RD_OK &&
(Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
Bp->flags |= BPT_DLACTIVITY;
}
if (Thr_agent == NULL)
setup_thread_agent();
Pupdate_maps(Proc);
if (delete_library) {
struct dynlib **Dpp;
struct dynlib *Dp;
for (Dp = Dynlib; Dp != NULL; Dp = Dp->next)
Dp->present = FALSE;
(void) Pobject_iter(Proc, object_present, NULL);
Dpp = &Dynlib;
while ((Dp = *Dpp) != NULL) {
if (Dp->present) {
Dpp = &Dp->next;
continue;
}
delete_breakpoints(Dp->base, Dp->size);
*Dpp = Dp->next;
free(Dp->lib_name);
free(Dp->match_name);
free(Dp->prt_name);
free(Dp);
}
delete_library = FALSE;
}
(void) Pobject_iter(Proc, object_iter, NULL);
set_deferred_breakpoints();
}
void
establish_stacks(void)
{
const pstatus_t *Psp = Pstatus(Proc);
char mapfile[64];
int mapfd;
struct stat statb;
prmap_t *Pmap = NULL;
int nmap = 0;
ph_map_t ph_map;
(void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid);
if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
fstat(mapfd, &statb) != 0 ||
statb.st_size < sizeof (prmap_t) ||
(Pmap = my_malloc(statb.st_size, NULL)) == NULL ||
(nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
(nmap /= sizeof (prmap_t)) == 0) {
if (Pmap != NULL)
free(Pmap);
Pmap = NULL;
nmap = 0;
}
if (mapfd >= 0)
(void) close(mapfd);
ph_map.pmap = Pmap;
ph_map.nmap = nmap;
(void) Plwp_iter(Proc, lwp_stack_traps, &ph_map);
if (Pmap != NULL)
free(Pmap);
if (Thr_agent == NULL)
return;
(void) td_ta_thr_iter(Thr_agent, thr_stack_traps, NULL,
TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
}
void
do_symbol_iter(const char *object_name, struct dynpat *Dyp)
{
if (*Dyp->Dp->prt_name == '\0')
object_name = PR_OBJ_EXEC;
(void) Psymbol_iter(Proc, object_name,
PR_DYNSYM, BIND_WEAK|BIND_GLOBAL|TYPE_FUNC,
symbol_iter, Dyp);
if (object_name == PR_OBJ_EXEC || Dyp->internal)
(void) Psymbol_iter(Proc, object_name,
PR_SYMTAB, BIND_ANY|TYPE_FUNC,
symbol_iter, Dyp);
}
int
object_iter(void *cd, const prmap_t *pmp, const char *object_name)
{
char name[100];
struct dynpat *Dyp;
struct dynlib *Dp;
const char *str;
char *s;
int i;
if ((pmp->pr_mflags & MA_WRITE) || !(pmp->pr_mflags & MA_EXEC))
return (0);
if (Thr_agent == NULL && strstr(object_name, "/libc.so.") != NULL)
setup_thread_agent();
for (Dp = Dynlib; Dp != NULL; Dp = Dp->next)
if (strcmp(object_name, Dp->lib_name) == 0 ||
(strcmp(Dp->lib_name, "a.out") == 0 &&
strcmp(pmp->pr_mapname, "a.out") == 0))
break;
if (Dp == NULL) {
Dp = my_malloc(sizeof (struct dynlib), NULL);
(void) memset(Dp, 0, sizeof (struct dynlib));
if (strcmp(pmp->pr_mapname, "a.out") == 0) {
Dp->lib_name = strdup(pmp->pr_mapname);
Dp->match_name = strdup(pmp->pr_mapname);
Dp->prt_name = strdup("");
} else {
Dp->lib_name = strdup(object_name);
if ((str = strrchr(object_name, '/')) != NULL)
str++;
else
str = object_name;
(void) strncpy(name, str, sizeof (name) - 2);
name[sizeof (name) - 2] = '\0';
if ((s = strstr(name, ".so")) != NULL)
*s = '\0';
Dp->match_name = strdup(name);
(void) strcat(name, ":");
Dp->prt_name = strdup(name);
}
Dp->next = Dynlib;
Dynlib = Dp;
}
if (Dp->built ||
(not_consist && strcmp(Dp->prt_name, "ld:") != 0))
return (0);
if (hflag && not_consist)
(void) fprintf(stderr, "not_consist is TRUE, building %s\n",
Dp->lib_name);
Dp->base = pmp->pr_vaddr;
Dp->size = pmp->pr_size;
for (Dyp = Dynpat; Dyp != NULL; Dyp = Dyp->next) {
if (interrupt|sigusr1)
break;
for (i = 0; i < Dyp->nlibpat; i++) {
if (interrupt|sigusr1)
break;
if (fnmatch(Dyp->libpat[i], Dp->match_name, 0) != 0)
continue;
if ((strcmp(Dp->match_name, "a.out") == 0 ||
strcmp(Dp->match_name, "ld") == 0) &&
strcmp(Dyp->libpat[i], Dp->match_name) != 0)
continue;
Dyp->Dp = Dp;
do_symbol_iter(object_name, Dyp);
Dyp->Dp = NULL;
}
}
Dp->built = TRUE;
return (interrupt | sigusr1);
}
int
object_present(void *cd, const prmap_t *pmp, const char *object_name)
{
struct dynlib *Dp;
for (Dp = Dynlib; Dp != NULL; Dp = Dp->next) {
if (Dp->base == pmp->pr_vaddr)
Dp->present = TRUE;
}
return (0);
}
struct bkpt *
get_bkpt(uintptr_t pc)
{
struct bkpt *Bp;
for (Bp = bpt_hashtable[bpt_hash(pc)]; Bp != NULL; Bp = Bp->next)
if (pc == Bp->addr)
break;
return (Bp);
}
struct bkpt *
create_bkpt(uintptr_t pc, int ret, int set)
{
uint_t hix = bpt_hash(pc);
struct bkpt *Bp;
const prmap_t *pmp;
for (Bp = bpt_hashtable[hix]; Bp != NULL; Bp = Bp->next)
if (pc == Bp->addr)
return (Bp);
if (is_vfork_child ||
(ret &&
((pmp = Paddr_to_text_map(Proc, pc)) == NULL ||
!(pmp->pr_mflags & MA_EXEC) ||
(pmp->pr_mflags & MA_WRITE))))
return (NULL);
Bp = my_malloc(sizeof (struct bkpt), NULL);
Bp->sym_name = NULL;
Bp->dyn = NULL;
Bp->addr = pc;
Bp->instr = 0;
Bp->flags = 0;
if (set && Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0)
Bp->flags |= BPT_ACTIVE;
Bp->next = bpt_hashtable[hix];
bpt_hashtable[hix] = Bp;
return (Bp);
}
void
set_deferred_breakpoints(void)
{
struct bkpt *Bp;
int i;
if (is_vfork_child)
return;
for (i = 0; i < HASHSZ; i++) {
for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
if (!(Bp->flags & BPT_ACTIVE)) {
if (!(Bp->flags & BPT_EXCLUDE) &&
Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0)
Bp->flags |= BPT_ACTIVE;
} else if (Paddr_to_text_map(Proc, Bp->addr) == NULL) {
Bp->flags &= ~BPT_ACTIVE;
}
}
}
}
int
symbol_iter(void *cd, const GElf_Sym *sym, const char *sym_name)
{
struct dynpat *Dyp = cd;
struct dynlib *Dp = Dyp->Dp;
uintptr_t pc = sym->st_value;
struct bkpt *Bp;
int i;
if (sym->st_shndx == SHN_UNDEF)
return (0);
if (*Dp->prt_name == '\0' && strcmp(sym_name, "_start") == 0)
return (0);
if (strcmp(Dp->match_name, "ld") == 0 &&
strcmp(sym_name, "_rt_boot") == 0)
return (0);
if (*sym_name == '.')
return (0);
for (i = 0; i < Dyp->nsympat; i++) {
if (interrupt|sigusr1)
break;
if (fnmatch(Dyp->sympat[i], sym_name, 0) != 0)
continue;
if ((Bp = create_bkpt(pc, 0, 0)) == NULL)
return (0);
if (Bp->sym_name == NULL) {
Bp->sym_name = strdup(sym_name);
} else if (strstr(Bp->sym_name, sym_name) != NULL ||
strlen(Bp->sym_name) > strlen(sym_name)) {
free(Bp->sym_name);
Bp->sym_name = strdup(sym_name);
}
Bp->dyn = Dp;
Bp->flags |= Dyp->flag;
if (Dyp->exclude)
Bp->flags |= BPT_EXCLUDE;
else if (Dyp->internal || *Dp->prt_name == '\0')
Bp->flags |= BPT_INTERNAL;
return (0);
}
return (interrupt | sigusr1);
}
void
report_htable_stats(void)
{
const pstatus_t *Psp = Pstatus(Proc);
struct callstack *Stk;
struct bkpt *Bp;
uint_t Min = 1000000;
uint_t Max = 0;
uint_t Avg = 0;
uint_t Total = 0;
uint_t i, j;
uint_t bucket[HASHSZ];
if (Dynpat == NULL || !hflag)
return;
hflag = FALSE;
(void) memset(bucket, 0, sizeof (bucket));
for (i = 0; i < HASHSZ; i++) {
j = 0;
for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next)
j++;
if (j < Min)
Min = j;
if (j > Max)
Max = j;
if (j < HASHSZ)
bucket[j]++;
Total += j;
}
Avg = (Total + HASHSZ / 2) / HASHSZ;
(void) fprintf(stderr, "truss hash table statistics --------\n");
(void) fprintf(stderr, " Total = %u\n", Total);
(void) fprintf(stderr, " Min = %u\n", Min);
(void) fprintf(stderr, " Max = %u\n", Max);
(void) fprintf(stderr, " Avg = %u\n", Avg);
for (i = 0; i < HASHSZ; i++)
if (bucket[i])
(void) fprintf(stderr, " %3u buckets of size %d\n",
bucket[i], i);
(void) fprintf(stderr, "truss-detected stacks --------\n");
for (Stk = callstack; Stk != NULL; Stk = Stk->next) {
(void) fprintf(stderr,
" base = 0x%.8lx end = 0x%.8lx size = %ld\n",
(ulong_t)Stk->stkbase,
(ulong_t)Stk->stkend,
(ulong_t)(Stk->stkend - Stk->stkbase));
}
(void) fprintf(stderr, "primary unix stack --------\n");
(void) fprintf(stderr,
" base = 0x%.8lx end = 0x%.8lx size = %ld\n",
(ulong_t)Psp->pr_stkbase,
(ulong_t)(Psp->pr_stkbase + Psp->pr_stksize),
(ulong_t)Psp->pr_stksize);
(void) fprintf(stderr, "nthr_create = %u\n", nthr_create);
}
void
make_lwp_stack(const lwpstatus_t *Lsp, prmap_t *Pmap, int nmap)
{
const pstatus_t *Psp = Pstatus(Proc);
uintptr_t sp = Lsp->pr_reg[R_SP];
id_t lwpid = Lsp->pr_lwpid;
struct callstack *Stk;
td_thrhandle_t th;
td_thrinfo_t thrinfo;
if (data_model != PR_MODEL_LP64)
sp = (uint32_t)sp;
if (sp == 0)
return;
for (Stk = callstack; Stk != NULL; Stk = Stk->next)
if (sp >= Stk->stkbase && sp < Stk->stkend)
return;
Stk = my_malloc(sizeof (struct callstack), NULL);
Stk->next = callstack;
callstack = Stk;
nstack++;
Stk->tref = 0;
Stk->tid = 0;
Stk->nthr_create = 0;
Stk->ncall = 0;
Stk->maxcall = DEF_MAXCALL;
Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL);
if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
Stk->stkbase = Psp->pr_stkbase;
Stk->stkend = Stk->stkbase + Psp->pr_stksize;
return;
}
if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) &&
sp >= (uintptr_t)Lsp->pr_altstack.ss_sp &&
sp < (uintptr_t)Lsp->pr_altstack.ss_sp
+ Lsp->pr_altstack.ss_size) {
Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp;
Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size;
return;
}
if (Thr_agent != NULL &&
td_ta_map_lwp2thr(Thr_agent, lwpid, &th) == TD_OK &&
td_thr_get_info(&th, &thrinfo) == TD_OK &&
sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
sp < (uintptr_t)thrinfo.ti_stkbase) {
Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
return;
}
for (; nmap; nmap--, Pmap++) {
if (sp >= Pmap->pr_vaddr &&
sp < Pmap->pr_vaddr + Pmap->pr_size) {
Stk->stkbase = Pmap->pr_vaddr;
Stk->stkend = Pmap->pr_vaddr + Pmap->pr_size;
return;
}
}
callstack = Stk->next;
nstack--;
free(Stk->stack);
free(Stk);
}
void
make_thr_stack(const td_thrhandle_t *Thp, prgregset_t reg)
{
const pstatus_t *Psp = Pstatus(Proc);
td_thrinfo_t thrinfo;
uintptr_t sp = reg[R_SP];
struct callstack *Stk;
if (data_model != PR_MODEL_LP64)
sp = (uint32_t)sp;
if (sp == 0)
return;
for (Stk = callstack; Stk != NULL; Stk = Stk->next)
if (sp >= Stk->stkbase && sp < Stk->stkend)
return;
Stk = my_malloc(sizeof (struct callstack), NULL);
Stk->next = callstack;
callstack = Stk;
nstack++;
Stk->tref = 0;
Stk->tid = 0;
Stk->nthr_create = 0;
Stk->ncall = 0;
Stk->maxcall = DEF_MAXCALL;
Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL);
if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
Stk->stkbase = Psp->pr_stkbase;
Stk->stkend = Stk->stkbase + Psp->pr_stksize;
return;
}
if (td_thr_get_info(Thp, &thrinfo) == TD_OK &&
sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
sp < (uintptr_t)thrinfo.ti_stkbase) {
Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
return;
}
callstack = Stk->next;
nstack--;
free(Stk->stack);
free(Stk);
}
struct callstack *
find_lwp_stack(uintptr_t sp)
{
const pstatus_t *Psp = Pstatus(Proc);
char mapfile[64];
int mapfd;
struct stat statb;
prmap_t *Pmap = NULL;
prmap_t *pmap = NULL;
int nmap = 0;
struct callstack *Stk = NULL;
(void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid);
if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
fstat(mapfd, &statb) != 0 ||
statb.st_size < sizeof (prmap_t) ||
(Pmap = my_malloc(statb.st_size, NULL)) == NULL ||
(nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
(nmap /= sizeof (prmap_t)) == 0) {
if (Pmap != NULL)
free(Pmap);
if (mapfd >= 0)
(void) close(mapfd);
return (NULL);
}
(void) close(mapfd);
for (pmap = Pmap; nmap--; pmap++) {
if (sp >= pmap->pr_vaddr &&
sp < pmap->pr_vaddr + pmap->pr_size) {
Stk = my_malloc(sizeof (struct callstack), NULL);
Stk->next = callstack;
callstack = Stk;
nstack++;
Stk->stkbase = pmap->pr_vaddr;
Stk->stkend = pmap->pr_vaddr + pmap->pr_size;
Stk->tref = 0;
Stk->tid = 0;
Stk->nthr_create = 0;
Stk->ncall = 0;
Stk->maxcall = DEF_MAXCALL;
Stk->stack = my_malloc(
DEF_MAXCALL * sizeof (*Stk->stack), NULL);
break;
}
}
free(Pmap);
return (Stk);
}
struct callstack *
find_stack(uintptr_t sp)
{
const pstatus_t *Psp = Pstatus(Proc);
private_t *pri = get_private();
const lwpstatus_t *Lsp = pri->lwpstat;
id_t lwpid = Lsp->pr_lwpid;
#if defined(__sparc)
prgreg_t tref = Lsp->pr_reg[R_G7];
#elif defined(__amd64)
prgreg_t tref = Lsp->pr_reg[REG_FS];
#elif defined(__i386)
prgreg_t tref = Lsp->pr_reg[GS];
#endif
struct callstack *Stk = NULL;
td_thrhandle_t th;
td_thrinfo_t thrinfo;
td_err_e error;
if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
Stk = my_malloc(sizeof (struct callstack), NULL);
Stk->next = callstack;
callstack = Stk;
nstack++;
Stk->stkbase = Psp->pr_stkbase;
Stk->stkend = Stk->stkbase + Psp->pr_stksize;
Stk->tref = 0;
Stk->tid = 0;
Stk->nthr_create = 0;
Stk->ncall = 0;
Stk->maxcall = DEF_MAXCALL;
Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
NULL);
return (Stk);
}
if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) &&
sp >= (uintptr_t)Lsp->pr_altstack.ss_sp &&
sp < (uintptr_t)Lsp->pr_altstack.ss_sp
+ Lsp->pr_altstack.ss_size) {
Stk = my_malloc(sizeof (struct callstack), NULL);
Stk->next = callstack;
callstack = Stk;
nstack++;
Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp;
Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size;
Stk->tref = 0;
Stk->tid = 0;
Stk->nthr_create = 0;
Stk->ncall = 0;
Stk->maxcall = DEF_MAXCALL;
Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
NULL);
return (Stk);
}
if (Thr_agent == NULL)
return (find_lwp_stack(sp));
if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) {
if (hflag)
(void) fprintf(stderr,
"cannot get thread handle for "
"lwp#%d, error=%d, tref=0x%.8lx\n",
(int)lwpid, error, (long)tref);
return (NULL);
}
if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) {
if (hflag)
(void) fprintf(stderr,
"cannot get thread info for "
"lwp#%d, error=%d, tref=0x%.8lx\n",
(int)lwpid, error, (long)tref);
return (NULL);
}
if (sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
sp < (uintptr_t)thrinfo.ti_stkbase) {
Stk = my_malloc(sizeof (struct callstack), NULL);
Stk->next = callstack;
callstack = Stk;
nstack++;
Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
Stk->tref = tref;
Stk->tid = thrinfo.ti_tid;
Stk->nthr_create = nthr_create;
Stk->ncall = 0;
Stk->maxcall = DEF_MAXCALL;
Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
NULL);
return (Stk);
}
if (hflag) {
(void) fprintf(stderr,
"sp not within thread stack: "
"sp=0x%.8lx stkbase=0x%.8lx stkend=0x%.8lx\n",
(ulong_t)sp,
(ulong_t)thrinfo.ti_stkbase - thrinfo.ti_stksize,
(ulong_t)thrinfo.ti_stkbase);
}
return (NULL);
}
void
get_tid(struct callstack *Stk)
{
private_t *pri = get_private();
const lwpstatus_t *Lsp = pri->lwpstat;
id_t lwpid = Lsp->pr_lwpid;
#if defined(__sparc)
prgreg_t tref = Lsp->pr_reg[R_G7];
#elif defined(__amd64)
prgreg_t tref = (data_model == PR_MODEL_LP64) ?
Lsp->pr_reg[REG_FS] : Lsp->pr_reg[REG_GS];
#elif defined(__i386)
prgreg_t tref = Lsp->pr_reg[GS];
#endif
td_thrhandle_t th;
td_thrinfo_t thrinfo;
td_err_e error;
if (Thr_agent == NULL) {
Stk->tref = 0;
Stk->tid = 0;
Stk->nthr_create = 0;
return;
}
if (tref == Stk->tref && Stk->nthr_create == nthr_create)
return;
if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) {
if (hflag)
(void) fprintf(stderr,
"cannot get thread handle for "
"lwp#%d, error=%d, tref=0x%.8lx\n",
(int)lwpid, error, (long)tref);
Stk->tref = 0;
Stk->tid = 0;
Stk->nthr_create = 0;
} else if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) {
if (hflag)
(void) fprintf(stderr,
"cannot get thread info for "
"lwp#%d, error=%d, tref=0x%.8lx\n",
(int)lwpid, error, (long)tref);
Stk->tref = 0;
Stk->tid = 0;
Stk->nthr_create = 0;
} else {
Stk->tref = tref;
Stk->tid = thrinfo.ti_tid;
Stk->nthr_create = nthr_create;
}
}
struct callstack *
callstack_info(uintptr_t sp, uintptr_t fp, int makeid)
{
struct callstack *Stk;
uintptr_t trash;
if (sp == 0 ||
Pread(Proc, &trash, sizeof (trash), sp) != sizeof (trash))
return (NULL);
for (Stk = callstack; Stk != NULL; Stk = Stk->next)
if (sp >= Stk->stkbase && sp < Stk->stkend)
break;
if (Stk == NULL) {
uintptr_t stkbase = sp;
uintptr_t stkend;
uint_t minsize;
#if defined(i386) || defined(__amd64)
if (data_model == PR_MODEL_LP64)
minsize = 2 * sizeof (uintptr_t);
else
minsize = 2 * sizeof (uint32_t);
#else
if (data_model != PR_MODEL_LP64)
minsize = SA32(MINFRAME32);
else
minsize = SA64(MINFRAME64);
#endif
stkend = sp + minsize;
while (Stk == NULL && fp != 0 && fp >= sp) {
stkend = fp + minsize;
for (Stk = callstack; Stk != NULL; Stk = Stk->next)
if ((fp >= Stk->stkbase && fp < Stk->stkend) ||
(stkend > Stk->stkbase &&
stkend <= Stk->stkend))
break;
if (Stk == NULL)
fp = previous_fp(fp, NULL);
}
if (Stk != NULL)
Stk->stkbase = stkbase;
}
if (Stk == NULL && makeid)
Stk = find_stack(sp);
if (Stk == NULL)
return (NULL);
if (Stk->ncall == Stk->maxcall) {
Stk->maxcall *= 2;
Stk->stack = my_realloc(Stk->stack,
Stk->maxcall * sizeof (*Stk->stack), NULL);
}
if (makeid)
get_tid(Stk);
return (Stk);
}
void
reset_breakpoints(void)
{
struct dynlib *Dp;
struct bkpt *Bp;
struct callstack *Stk;
int i;
if (Dynpat == NULL)
return;
while ((Dp = Dynlib) != NULL) {
Dynlib = Dp->next;
free(Dp->lib_name);
free(Dp->match_name);
free(Dp->prt_name);
free(Dp);
}
if (bpt_hashtable != NULL) {
for (i = 0; i < HASHSZ; i++) {
while ((Bp = bpt_hashtable[i]) != NULL) {
bpt_hashtable[i] = Bp->next;
if (Bp->sym_name)
free(Bp->sym_name);
free(Bp);
}
}
}
while ((Stk = callstack) != NULL) {
callstack = Stk->next;
free(Stk->stack);
free(Stk);
}
if (Thr_agent != NULL)
(void) td_ta_delete(Thr_agent);
Thr_agent = NULL;
Preset_maps(Proc);
Rdb_agent = NULL;
(void) establish_breakpoints();
}
void
clear_breakpoints(void)
{
struct bkpt *Bp;
int i;
if (Dynpat == NULL)
return;
report_htable_stats();
for (i = 0; i < HASHSZ; i++) {
for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
if (Bp->flags & BPT_ACTIVE)
(void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
Bp->flags &= ~BPT_ACTIVE;
}
}
if (Thr_agent != NULL) {
td_thr_events_t events;
td_event_fillset(&events);
(void) td_ta_clear_event(Thr_agent, &events);
(void) td_ta_delete(Thr_agent);
}
Thr_agent = NULL;
}
void
reestablish_traps(void)
{
struct bkpt *Bp;
ulong_t instr;
int i;
if (Dynpat == NULL || is_vfork_child)
return;
for (i = 0; i < HASHSZ; i++) {
for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
if ((Bp->flags & BPT_ACTIVE) &&
Psetbkpt(Proc, Bp->addr, &instr) != 0)
Bp->flags &= ~BPT_ACTIVE;
}
}
}
void
show_function_call(private_t *pri,
struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp)
{
long arg[8];
int narg;
int i;
narg = get_arguments(arg);
make_pname(pri, (Stk != NULL)? Stk->tid : 0);
putpname(pri);
timestamp(pri);
if (Stk != NULL) {
for (i = 1; i < Stk->ncall; i++) {
(void) fputc(' ', stdout);
(void) fputc(' ', stdout);
}
}
(void) printf("-> %s%s(", Dp->prt_name, Bp->sym_name);
for (i = 0; i < narg; i++) {
(void) printf("0x%lx", arg[i]);
if (i < narg-1) {
(void) fputc(',', stdout);
(void) fputc(' ', stdout);
}
}
(void) printf(")\n");
Flush();
}
void
show_function_return(private_t *pri, long rval, int stret,
struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp)
{
int i;
make_pname(pri, Stk->tid);
putpname(pri);
timestamp(pri);
for (i = 0; i < Stk->ncall; i++) {
(void) fputc(' ', stdout);
(void) fputc(' ', stdout);
}
(void) printf("<- %s%s() = ", Dp->prt_name, Bp->sym_name);
if (stret) {
(void) printf("struct return\n");
} else if (data_model == PR_MODEL_LP64) {
if (rval >= (64 * 1024) || -rval >= (64 * 1024))
(void) printf("0x%lx\n", rval);
else
(void) printf("%ld\n", rval);
} else {
int rval32 = (int)rval;
if (rval32 >= (64 * 1024) || -rval32 >= (64 * 1024))
(void) printf("0x%x\n", rval32);
else
(void) printf("%d\n", rval32);
}
Flush();
}
int
function_trace(private_t *pri, int first, int clear, int dotrace)
{
struct ps_lwphandle *Lwp = pri->Lwp;
const lwpstatus_t *Lsp = pri->lwpstat;
uintptr_t pc = Lsp->pr_reg[R_PC];
uintptr_t sp = Lsp->pr_reg[R_SP];
uintptr_t fp = Lsp->pr_reg[R_FP];
struct bkpt *Bp;
struct dynlib *Dp;
struct callstack *Stk;
ulong_t instr;
int active;
int rval = 0;
if (Dynpat == NULL)
return (-1);
if (data_model != PR_MODEL_LP64) {
pc = (uint32_t)pc;
sp = (uint32_t)sp;
fp = (uint32_t)fp;
}
if ((Bp = get_bkpt(pc)) == NULL) {
if (hflag)
(void) fprintf(stderr,
"function_trace(): "
"cannot find breakpoint for pc: 0x%.8lx\n",
(ulong_t)pc);
return (-1);
}
if ((Bp->flags & (BPT_PREINIT|BPT_POSTINIT|BPT_DLACTIVITY)) && !clear) {
rd_event_msg_t event_msg;
if (hflag) {
if (Bp->flags & BPT_PREINIT)
(void) fprintf(stderr, "function_trace(): "
"RD_PREINIT breakpoint\n");
if (Bp->flags & BPT_POSTINIT)
(void) fprintf(stderr, "function_trace(): "
"RD_POSTINIT breakpoint\n");
if (Bp->flags & BPT_DLACTIVITY)
(void) fprintf(stderr, "function_trace(): "
"RD_DLACTIVITY breakpoint\n");
}
if (rd_event_getmsg(Rdb_agent, &event_msg) == RD_OK) {
if (event_msg.type == RD_DLACTIVITY) {
switch (event_msg.u.state) {
case RD_CONSISTENT:
establish_breakpoints();
break;
case RD_ADD:
not_consist = TRUE;
establish_breakpoints();
not_consist = FALSE;
break;
case RD_DELETE:
delete_library = TRUE;
break;
default:
break;
}
}
if (hflag) {
const char *et;
char buf[32];
switch (event_msg.type) {
case RD_NONE:
et = "RD_NONE";
break;
case RD_PREINIT:
et = "RD_PREINIT";
break;
case RD_POSTINIT:
et = "RD_POSTINIT";
break;
case RD_DLACTIVITY:
et = "RD_DLACTIVITY";
break;
default:
(void) sprintf(buf, "0x%x",
event_msg.type);
et = buf;
break;
}
(void) fprintf(stderr,
"event_msg.type = %s ", et);
switch (event_msg.u.state) {
case RD_NOSTATE:
et = "RD_NOSTATE";
break;
case RD_CONSISTENT:
et = "RD_CONSISTENT";
break;
case RD_ADD:
et = "RD_ADD";
break;
case RD_DELETE:
et = "RD_DELETE";
break;
default:
(void) sprintf(buf, "0x%x",
event_msg.u.state);
et = buf;
break;
}
(void) fprintf(stderr,
"event_msg.u.state = %s\n", et);
}
}
}
if ((Bp->flags & BPT_TD_CREATE) && !clear) {
nthr_create++;
if (hflag)
(void) fprintf(stderr, "function_trace(): "
"BPT_TD_CREATE breakpoint\n");
}
Dp = Bp->dyn;
if (dotrace) {
if ((Stk = callstack_info(sp, fp, 1)) == NULL) {
if (Dp != NULL && !clear) {
if (cflag) {
add_fcall(fcall_tbl, Dp->prt_name,
Bp->sym_name, (unsigned long)1);
}
else
show_function_call(pri, NULL, Dp, Bp);
if ((Bp->flags & BPT_HANG) && !first)
rval = 1;
}
} else if (!clear) {
if (Dp != NULL) {
function_entry(pri, Bp, Stk);
if ((Bp->flags & BPT_HANG) && !first)
rval = 1;
} else {
function_return(pri, Stk);
}
}
}
active = (Bp->flags & BPT_ACTIVE);
Bp->flags |= BPT_ACTIVE;
instr = Bp->instr;
(void) mutex_unlock(&truss_lock);
(void) Lxecbkpt(Lwp, instr);
(void) mutex_lock(&truss_lock);
if (rval || clear) {
#if defined(__i386)
if (rval && instr == 0x55) {
(void) mutex_unlock(&truss_lock);
(void) Lsetrun(Lwp, 0, PRCFAULT|PRSTEP);
(void) Lwait(Lwp, MILLISEC);
(void) mutex_lock(&truss_lock);
}
#endif
if (get_bkpt(pc) != Bp)
abend("function_trace: lost breakpoint", NULL);
(void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
Bp->flags &= ~BPT_ACTIVE;
(void) mutex_unlock(&truss_lock);
(void) Lsetrun(Lwp, 0, PRCFAULT|PRSTOP);
(void) Lwait(Lwp, MILLISEC);
(void) mutex_lock(&truss_lock);
} else {
if (get_bkpt(pc) != Bp)
abend("function_trace: lost breakpoint", NULL);
if (!active || !(Bp->flags & BPT_ACTIVE)) {
(void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
Bp->flags &= ~BPT_ACTIVE;
}
}
return (rval);
}
void
function_entry(private_t *pri, struct bkpt *Bp, struct callstack *Stk)
{
const lwpstatus_t *Lsp = pri->lwpstat;
uintptr_t sp = Lsp->pr_reg[R_SP];
uintptr_t rpc = get_return_address(&sp);
struct dynlib *Dp = Bp->dyn;
int oldframe = FALSE;
int i;
if (data_model != PR_MODEL_LP64) {
sp = (uint32_t)sp;
rpc = (uint32_t)rpc;
}
if (!(sp >= Stk->stkbase && sp < Stk->stkend) ||
(!(Bp->flags & BPT_INTERNAL) &&
rpc >= Dp->base && rpc < Dp->base + Dp->size))
return;
for (i = 0; i < Stk->ncall; i++) {
if (sp >= Stk->stack[i].sp) {
Stk->ncall = i;
if (sp == Stk->stack[i].sp)
oldframe = TRUE;
break;
}
}
if (!oldframe && !cflag) {
(void) create_bkpt(rpc, 1, 1);
Stk->stack[Stk->ncall].sp = sp;
Stk->stack[Stk->ncall].pc = rpc;
Stk->stack[Stk->ncall].fcn = Bp;
}
Stk->ncall++;
if (cflag) {
add_fcall(fcall_tbl, Dp->prt_name, Bp->sym_name,
(unsigned long)1);
} else {
show_function_call(pri, Stk, Dp, Bp);
}
}
void
function_return(private_t *pri, struct callstack *Stk)
{
const lwpstatus_t *Lsp = pri->lwpstat;
uintptr_t sp = Lsp->pr_reg[R_SP];
uintptr_t fp = Lsp->pr_reg[R_FP];
int i;
if (data_model != PR_MODEL_LP64) {
sp = (uint32_t)sp;
fp = (uint32_t)fp;
}
if (fp < sp + 8)
fp = sp + 8;
for (i = Stk->ncall - 1; i >= 0; i--) {
if (sp <= Stk->stack[i].sp && fp > Stk->stack[i].sp) {
Stk->ncall = i;
break;
}
}
#if defined(i386) || defined(__amd64)
if (i < 0) {
int j;
for (j = 0; i < 0 && j < 8; j++) {
sp -= 4;
for (i = Stk->ncall - 1; i >= 0; i--) {
if (sp <= Stk->stack[i].sp &&
fp > Stk->stack[i].sp) {
Stk->ncall = i;
break;
}
}
}
}
#endif
if ((i >= 0) && (!cflag)) {
show_function_return(pri, Lsp->pr_reg[R_R0], 0,
Stk, Stk->stack[i].fcn->dyn, Stk->stack[i].fcn);
}
}
#if defined(__sparc)
#define FPADJUST 0
#elif defined(__amd64)
#define FPADJUST 8
#elif defined(__i386)
#define FPADJUST 4
#endif
void
trap_one_stack(prgregset_t reg)
{
struct dynlib *Dp;
struct bkpt *Bp;
struct callstack *Stk;
GElf_Sym sym;
char sym_name[32];
uintptr_t sp = reg[R_SP];
uintptr_t pc = reg[R_PC];
uintptr_t fp;
uintptr_t rpc;
uint_t nframe = 0;
uint_t maxframe = 8;
struct {
uintptr_t sp;
uintptr_t pc;
uintptr_t rsp;
uintptr_t rpc;
} *frame = my_malloc(maxframe * sizeof (*frame), NULL);
while (sp != 0) {
fp = sp;
frame[nframe].sp = sp;
frame[nframe].pc = pc;
sp = previous_fp(sp, &pc);
frame[nframe].rsp = sp;
frame[nframe].rpc = pc;
if (++nframe == maxframe) {
maxframe *= 2;
frame = my_realloc(frame, maxframe * sizeof (*frame),
NULL);
}
}
while (nframe--) {
if (Plookup_by_addr(Proc, frame[nframe].pc, sym_name,
sizeof (sym_name), &sym) != 0)
continue;
pc = sym.st_value;
rpc = frame[nframe].rpc;
if ((Bp = get_bkpt(pc)) == NULL || (Dp = Bp->dyn) == NULL)
continue;
if (!(Bp->flags & BPT_INTERNAL) &&
rpc >= Dp->base && rpc < Dp->base + Dp->size)
continue;
sp = frame[nframe].rsp + FPADJUST;
if ((Stk = callstack_info(sp, fp, 0)) == NULL)
continue;
if (create_bkpt(rpc, 1, 1) != NULL) {
Stk->stack[Stk->ncall].sp = sp;
Stk->stack[Stk->ncall].pc = rpc;
Stk->stack[Stk->ncall].fcn = Bp;
Stk->ncall++;
}
}
free(frame);
}
int
lwp_stack_traps(void *cd, const lwpstatus_t *Lsp)
{
ph_map_t *ph_map = (ph_map_t *)cd;
prgregset_t reg;
(void) memcpy(reg, Lsp->pr_reg, sizeof (prgregset_t));
make_lwp_stack(Lsp, ph_map->pmap, ph_map->nmap);
trap_one_stack(reg);
return (interrupt | sigusr1);
}
int
thr_stack_traps(const td_thrhandle_t *Thp, void *cd)
{
prgregset_t reg;
if (td_thr_getgregs(Thp, reg) != TD_PARTIALREG)
return (0);
make_thr_stack(Thp, reg);
trap_one_stack(reg);
return (interrupt | sigusr1);
}
#if defined(__sparc)
uintptr_t
previous_fp(uintptr_t sp, uintptr_t *rpc)
{
uintptr_t fp = 0;
uintptr_t pc = 0;
if (data_model == PR_MODEL_LP64) {
struct rwindow64 rwin;
if (Pread(Proc, &rwin, sizeof (rwin), sp + STACK_BIAS)
== sizeof (rwin)) {
fp = (uintptr_t)rwin.rw_fp;
pc = (uintptr_t)rwin.rw_rtn;
}
if (fp != 0 &&
Pread(Proc, &rwin, sizeof (rwin), fp + STACK_BIAS)
!= sizeof (rwin))
fp = pc = 0;
} else {
struct rwindow32 rwin;
if (Pread(Proc, &rwin, sizeof (rwin), sp) == sizeof (rwin)) {
fp = (uint32_t)rwin.rw_fp;
pc = (uint32_t)rwin.rw_rtn;
}
if (fp != 0 &&
Pread(Proc, &rwin, sizeof (rwin), fp) != sizeof (rwin))
fp = pc = 0;
}
if (rpc)
*rpc = pc;
return (fp);
}
uintptr_t
get_return_address(uintptr_t *psp)
{
instr_t inst;
private_t *pri = get_private();
const lwpstatus_t *Lsp = pri->lwpstat;
uintptr_t rpc;
rpc = (uintptr_t)Lsp->pr_reg[R_O7] + 8;
if (data_model != PR_MODEL_LP64)
rpc = (uint32_t)rpc;
if (Pread(Proc, &inst, sizeof (inst), rpc) == sizeof (inst) &&
inst < 0x1000)
rpc += sizeof (instr_t);
return (rpc);
}
int
get_arguments(long *argp)
{
private_t *pri = get_private();
const lwpstatus_t *Lsp = pri->lwpstat;
int i;
if (data_model != PR_MODEL_LP64)
for (i = 0; i < 4; i++)
argp[i] = (uint_t)Lsp->pr_reg[R_O0+i];
else
for (i = 0; i < 4; i++)
argp[i] = (long)Lsp->pr_reg[R_O0+i];
return (4);
}
#endif
#if defined(__i386) || defined(__amd64)
uintptr_t
previous_fp(uintptr_t fp, uintptr_t *rpc)
{
uintptr_t frame[2];
uintptr_t trash[2];
if (Pread(Proc, frame, sizeof (frame), fp) != sizeof (frame) ||
(frame[0] != 0 &&
Pread(Proc, trash, sizeof (trash), frame[0]) != sizeof (trash)))
frame[0] = frame[1] = 0;
if (rpc)
*rpc = frame[1];
return (frame[0]);
}
#endif
#if defined(__amd64) || defined(__i386)
int
return_count32(uint32_t *ppc)
{
uintptr_t pc = *ppc;
struct bkpt *Bp;
int count;
uchar_t instr[6];
if ((count = Pread(Proc, instr, sizeof (instr), pc)) < 0)
return (-1);
if ((Bp = get_bkpt(pc)) != NULL && (Bp->flags & BPT_ACTIVE))
instr[0] = (uchar_t)Bp->instr;
if (count != sizeof (instr) &&
(count < 3 || instr[0] != 0x83))
return (-1);
if (instr[1] != 0xc4) {
count = 0;
} else if (instr[0] == 0x81) {
count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24);
*ppc += 6;
} else if (instr[0] == 0x83) {
count = instr[2];
*ppc += 3;
} else {
count = 0;
}
return (count);
}
uintptr_t
get_return_address32(uintptr_t *psp)
{
uint32_t sp = *psp;
uint32_t rpc;
int count;
*psp += 4;
if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc))
return (0);
if ((count = return_count32(&rpc)) < 0)
count = 0;
*psp += count;
return (rpc);
}
uintptr_t
get_return_address(uintptr_t *psp)
{
uintptr_t rpc;
uintptr_t sp = *psp;
if (data_model == PR_MODEL_LP64) {
if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc))
return (0);
return (rpc);
} else
return (get_return_address32(psp));
}
int
get_arguments32(long *argp)
{
private_t *pri = get_private();
const lwpstatus_t *Lsp = pri->lwpstat;
uint32_t frame[5];
int narg;
int count;
int i;
narg = Pread(Proc, frame, sizeof (frame),
(uintptr_t)Lsp->pr_reg[R_SP]);
narg -= sizeof (greg32_t);
if (narg <= 0)
return (0);
narg /= sizeof (greg32_t);
if ((count = return_count32(&frame[0])) < 0)
narg = 0;
else {
count /= sizeof (greg32_t);
if (narg > count)
narg = count;
}
for (i = 0; i < narg; i++)
argp[i] = (long)frame[i+1];
return (narg);
}
int
get_arguments(long *argp)
{
private_t *pri = get_private();
const lwpstatus_t *Lsp = pri->lwpstat;
if (data_model == PR_MODEL_LP64) {
argp[0] = Lsp->pr_reg[REG_RDI];
argp[1] = Lsp->pr_reg[REG_RSI];
argp[2] = Lsp->pr_reg[REG_RDX];
argp[3] = Lsp->pr_reg[REG_RCX];
argp[4] = Lsp->pr_reg[REG_R8];
argp[5] = Lsp->pr_reg[REG_R9];
return (6);
} else
return (get_arguments32(argp));
}
#endif