Merge commit 'a058d1cc571af5fbcfe7f1d719df1abbfdb722f3' into merges
[unleashed.git] / usr / src / cmd / truss / fcall.c
blob867ee672e7e3bd9b43e4cef32b9744c72552d18d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #define _SYSCALL32
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <memory.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stack.h>
38 #include <signal.h>
39 #include <limits.h>
40 #include <sys/isa_defs.h>
41 #include <proc_service.h>
42 #include <dlfcn.h>
43 #include <fnmatch.h>
44 #include <libproc.h>
45 #include "ramdata.h"
46 #include "systable.h"
47 #include "print.h"
48 #include "proto.h"
49 #include "htbl.h"
52 * Functions supporting library function call tracing.
55 typedef struct {
56 prmap_t *pmap;
57 int nmap;
58 } ph_map_t;
61 * static functions in this file.
63 void function_entry(private_t *, struct bkpt *, struct callstack *);
64 void function_return(private_t *, struct callstack *);
65 int object_iter(void *, const prmap_t *, const char *);
66 int object_present(void *, const prmap_t *, const char *);
67 int symbol_iter(void *, const GElf_Sym *, const char *);
68 uintptr_t get_return_address(uintptr_t *);
69 int get_arguments(long *argp);
70 uintptr_t previous_fp(uintptr_t, uintptr_t *);
71 int lwp_stack_traps(void *cd, const lwpstatus_t *Lsp);
72 int thr_stack_traps(const td_thrhandle_t *Thp, void *cd);
73 struct bkpt *create_bkpt(uintptr_t, int, int);
74 void set_deferred_breakpoints(void);
76 #define DEF_MAXCALL 16 /* initial value of Stk->maxcall */
78 #define FAULT_ADDR ((uintptr_t)(0-8))
80 #define HASHSZ 2048
81 #define bpt_hash(addr) ((((addr) >> 13) ^ ((addr) >> 2)) & 0x7ff)
83 static void
84 setup_thread_agent(void)
86 struct bkpt *Bp;
87 td_notify_t notify;
88 td_thr_events_t events;
90 if (Thr_agent != NULL) /* only once */
91 return;
92 if (td_init() != TD_OK || td_ta_new(Proc, &Thr_agent) != TD_OK)
93 Thr_agent = NULL;
94 else {
95 td_event_emptyset(&events);
96 td_event_addset(&events, TD_CREATE);
97 if (td_ta_event_addr(Thr_agent, TD_CREATE, &notify) == TD_OK &&
98 notify.type == NOTIFY_BPT &&
99 td_ta_set_event(Thr_agent, &events) == TD_OK &&
100 (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
101 Bp->flags |= BPT_TD_CREATE;
106 * Delete all breakpoints in the range [base .. base+size)
107 * from the breakpoint hash table.
109 static void
110 delete_breakpoints(uintptr_t base, size_t size)
112 struct bkpt **Bpp;
113 struct bkpt *Bp;
114 int i;
116 if (bpt_hashtable == NULL)
117 return;
118 for (i = 0; i < HASHSZ; i++) {
119 Bpp = &bpt_hashtable[i];
120 while ((Bp = *Bpp) != NULL) {
121 if (Bp->addr < base || Bp->addr >= base + size) {
122 Bpp = &Bp->next;
123 continue;
125 *Bpp = Bp->next;
126 free(Bp->sym_name);
127 free(Bp);
133 * Establishment of breakpoints on traced library functions.
135 void
136 establish_breakpoints(void)
138 if (Dynpat == NULL)
139 return;
141 /* allocate the breakpoint hash table */
142 if (bpt_hashtable == NULL) {
143 bpt_hashtable = my_malloc(HASHSZ * sizeof (struct bkpt *),
144 NULL);
145 (void) memset(bpt_hashtable, 0,
146 HASHSZ * sizeof (struct bkpt *));
150 * Set special rtld_db event breakpoints, first time only.
152 if (Rdb_agent == NULL &&
153 (Rdb_agent = Prd_agent(Proc)) != NULL) {
154 rd_notify_t notify;
155 struct bkpt *Bp;
157 (void) rd_event_enable(Rdb_agent, 1);
158 if (rd_event_addr(Rdb_agent, RD_PREINIT, &notify) == RD_OK &&
159 (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
160 Bp->flags |= BPT_PREINIT;
161 if (rd_event_addr(Rdb_agent, RD_POSTINIT, &notify) == RD_OK &&
162 (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
163 Bp->flags |= BPT_POSTINIT;
164 if (rd_event_addr(Rdb_agent, RD_DLACTIVITY, &notify) == RD_OK &&
165 (Bp = create_bkpt(notify.u.bptaddr, 0, 1)) != NULL)
166 Bp->flags |= BPT_DLACTIVITY;
170 * Set special thread event breakpoint, first time libc is seen.
172 if (Thr_agent == NULL)
173 setup_thread_agent();
176 * Tell libproc to update its mappings.
178 Pupdate_maps(Proc);
181 * If rtld_db told us a library was being deleted,
182 * first mark all of the dynlibs as not present, then
183 * iterate over the shared objects, marking only those
184 * present that really are present, and finally delete
185 * all of the not-present dynlibs.
187 if (delete_library) {
188 struct dynlib **Dpp;
189 struct dynlib *Dp;
191 for (Dp = Dynlib; Dp != NULL; Dp = Dp->next)
192 Dp->present = FALSE;
193 (void) Pobject_iter(Proc, object_present, NULL);
194 Dpp = &Dynlib;
195 while ((Dp = *Dpp) != NULL) {
196 if (Dp->present) {
197 Dpp = &Dp->next;
198 continue;
200 delete_breakpoints(Dp->base, Dp->size);
201 *Dpp = Dp->next;
202 free(Dp->lib_name);
203 free(Dp->match_name);
204 free(Dp->prt_name);
205 free(Dp);
207 delete_library = FALSE;
211 * Iterate over the shared objects, creating breakpoints.
213 (void) Pobject_iter(Proc, object_iter, NULL);
216 * Now actually set all the breakpoints we just created.
218 set_deferred_breakpoints();
222 * Initial establishment of stacks in a newly-grabbed process.
223 * establish_breakpoints() has already been called.
225 void
226 establish_stacks(void)
228 const pstatus_t *Psp = Pstatus(Proc);
229 char mapfile[64];
230 int mapfd;
231 struct stat statb;
232 prmap_t *Pmap = NULL;
233 int nmap = 0;
234 ph_map_t ph_map;
236 (void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid);
237 if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
238 fstat(mapfd, &statb) != 0 ||
239 statb.st_size < sizeof (prmap_t) ||
240 (Pmap = my_malloc(statb.st_size, NULL)) == NULL ||
241 (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
242 (nmap /= sizeof (prmap_t)) == 0) {
243 free(Pmap);
244 Pmap = NULL;
245 nmap = 0;
247 if (mapfd >= 0)
248 (void) close(mapfd);
251 * Iterate over lwps, establishing stacks.
253 ph_map.pmap = Pmap;
254 ph_map.nmap = nmap;
255 (void) Plwp_iter(Proc, lwp_stack_traps, &ph_map);
256 free(Pmap);
258 if (Thr_agent == NULL)
259 return;
262 * Iterate over unbound threads, establishing stacks.
264 (void) td_ta_thr_iter(Thr_agent, thr_stack_traps, NULL,
265 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
266 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
269 void
270 do_symbol_iter(const char *object_name, struct dynpat *Dyp)
272 if (*Dyp->Dp->prt_name == '\0')
273 object_name = PR_OBJ_EXEC;
276 * Always search the dynamic symbol table.
278 (void) Psymbol_iter(Proc, object_name,
279 PR_DYNSYM, BIND_WEAK|BIND_GLOBAL|TYPE_FUNC,
280 symbol_iter, Dyp);
283 * Search the static symbol table if this is the
284 * executable file or if we are being asked to
285 * report internal calls within the library.
287 if (object_name == PR_OBJ_EXEC || Dyp->internal)
288 (void) Psymbol_iter(Proc, object_name,
289 PR_SYMTAB, BIND_ANY|TYPE_FUNC,
290 symbol_iter, Dyp);
293 /* ARGSUSED */
295 object_iter(void *cd, const prmap_t *pmp, const char *object_name)
297 char name[100];
298 struct dynpat *Dyp;
299 struct dynlib *Dp;
300 const char *str;
301 char *s;
302 int i;
304 if ((pmp->pr_mflags & MA_WRITE) || !(pmp->pr_mflags & MA_EXEC))
305 return (0);
308 * Set special thread event breakpoint, first time libc is seen.
310 if (Thr_agent == NULL && strstr(object_name, "/libc.so.") != NULL)
311 setup_thread_agent();
313 for (Dp = Dynlib; Dp != NULL; Dp = Dp->next)
314 if (strcmp(object_name, Dp->lib_name) == 0 ||
315 (strcmp(Dp->lib_name, "a.out") == 0 &&
316 strcmp(pmp->pr_mapname, "a.out") == 0))
317 break;
319 if (Dp == NULL) {
320 Dp = my_malloc(sizeof (struct dynlib), NULL);
321 (void) memset(Dp, 0, sizeof (struct dynlib));
322 if (strcmp(pmp->pr_mapname, "a.out") == 0) {
323 Dp->lib_name = strdup(pmp->pr_mapname);
324 Dp->match_name = strdup(pmp->pr_mapname);
325 Dp->prt_name = strdup("");
326 } else {
327 Dp->lib_name = strdup(object_name);
328 if ((str = strrchr(object_name, '/')) != NULL)
329 str++;
330 else
331 str = object_name;
332 (void) strncpy(name, str, sizeof (name) - 2);
333 name[sizeof (name) - 2] = '\0';
334 if ((s = strstr(name, ".so")) != NULL)
335 *s = '\0';
336 Dp->match_name = strdup(name);
337 (void) strcat(name, ":");
338 Dp->prt_name = strdup(name);
340 Dp->next = Dynlib;
341 Dynlib = Dp;
344 if (Dp->built ||
345 (not_consist && strcmp(Dp->prt_name, "ld:") != 0)) /* kludge */
346 return (0);
348 if (hflag && not_consist)
349 (void) fprintf(stderr, "not_consist is TRUE, building %s\n",
350 Dp->lib_name);
352 Dp->base = pmp->pr_vaddr;
353 Dp->size = pmp->pr_size;
356 * For every dynlib pattern that matches this library's name,
357 * iterate through all of the library's symbols looking for
358 * matching symbol name patterns.
360 for (Dyp = Dynpat; Dyp != NULL; Dyp = Dyp->next) {
361 if (interrupt|sigusr1)
362 break;
363 for (i = 0; i < Dyp->nlibpat; i++) {
364 if (interrupt|sigusr1)
365 break;
366 if (fnmatch(Dyp->libpat[i], Dp->match_name, 0) != 0)
367 continue; /* no match */
370 * Require an exact match for the executable (a.out)
371 * and for the dynamic linker (ld.so.1).
373 if ((strcmp(Dp->match_name, "a.out") == 0 ||
374 strcmp(Dp->match_name, "ld") == 0) &&
375 strcmp(Dyp->libpat[i], Dp->match_name) != 0)
376 continue;
379 * Set Dyp->Dp to Dp so symbol_iter() can use it.
381 Dyp->Dp = Dp;
382 do_symbol_iter(object_name, Dyp);
383 Dyp->Dp = NULL;
387 Dp->built = TRUE;
388 return (interrupt | sigusr1);
391 /* ARGSUSED */
393 object_present(void *cd, const prmap_t *pmp, const char *object_name)
395 struct dynlib *Dp;
397 for (Dp = Dynlib; Dp != NULL; Dp = Dp->next) {
398 if (Dp->base == pmp->pr_vaddr)
399 Dp->present = TRUE;
402 return (0);
406 * Search for an existing breakpoint at the 'pc' location.
408 struct bkpt *
409 get_bkpt(uintptr_t pc)
411 struct bkpt *Bp;
413 for (Bp = bpt_hashtable[bpt_hash(pc)]; Bp != NULL; Bp = Bp->next)
414 if (pc == Bp->addr)
415 break;
417 return (Bp);
421 * Create a breakpoint at 'pc', if one is not there already.
422 * 'ret' is true when creating a function return breakpoint, in which case
423 * fail and return NULL if the breakpoint would be created in writeable data.
424 * If 'set' it true, set the breakpoint in the process now.
426 struct bkpt *
427 create_bkpt(uintptr_t pc, int ret, int set)
429 uint_t hix = bpt_hash(pc);
430 struct bkpt *Bp;
431 const prmap_t *pmp;
433 for (Bp = bpt_hashtable[hix]; Bp != NULL; Bp = Bp->next)
434 if (pc == Bp->addr)
435 return (Bp);
438 * Don't set return breakpoints on writeable data
439 * or on any space other than executable text.
440 * Don't set breakpoints in the child of a vfork()
441 * because that would modify the parent's address space.
443 if (is_vfork_child ||
444 (ret &&
445 ((pmp = Paddr_to_text_map(Proc, pc)) == NULL ||
446 !(pmp->pr_mflags & MA_EXEC) ||
447 (pmp->pr_mflags & MA_WRITE))))
448 return (NULL);
450 /* create a new unnamed breakpoint */
451 Bp = my_malloc(sizeof (struct bkpt), NULL);
452 Bp->sym_name = NULL;
453 Bp->dyn = NULL;
454 Bp->addr = pc;
455 Bp->instr = 0;
456 Bp->flags = 0;
457 if (set && Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0)
458 Bp->flags |= BPT_ACTIVE;
459 Bp->next = bpt_hashtable[hix];
460 bpt_hashtable[hix] = Bp;
462 return (Bp);
466 * Set all breakpoints that haven't been set yet.
467 * Deactivate all breakpoints from modules that are not present any more.
469 void
470 set_deferred_breakpoints(void)
472 struct bkpt *Bp;
473 int i;
475 if (is_vfork_child)
476 return;
478 for (i = 0; i < HASHSZ; i++) {
479 for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
480 if (!(Bp->flags & BPT_ACTIVE)) {
481 if (!(Bp->flags & BPT_EXCLUDE) &&
482 Psetbkpt(Proc, Bp->addr, &Bp->instr) == 0)
483 Bp->flags |= BPT_ACTIVE;
484 } else if (Paddr_to_text_map(Proc, Bp->addr) == NULL) {
485 Bp->flags &= ~BPT_ACTIVE;
492 symbol_iter(void *cd, const GElf_Sym *sym, const char *sym_name)
494 struct dynpat *Dyp = cd;
495 struct dynlib *Dp = Dyp->Dp;
496 uintptr_t pc = sym->st_value;
497 struct bkpt *Bp;
498 int i;
500 /* ignore any undefined symbols */
501 if (sym->st_shndx == SHN_UNDEF)
502 return (0);
505 * Arbitrarily omit "_start" from the executable.
506 * (Avoid indentation before main().)
508 if (*Dp->prt_name == '\0' && strcmp(sym_name, "_start") == 0)
509 return (0);
512 * Arbitrarily omit "_rt_boot" from the dynamic linker.
513 * (Avoid indentation before main().)
515 if (strcmp(Dp->match_name, "ld") == 0 &&
516 strcmp(sym_name, "_rt_boot") == 0)
517 return (0);
520 * Arbitrarily omit any symbols whose name starts with '.'.
521 * Apparantly putting a breakpoint on .umul causes a
522 * fatal error in libthread (%y is not restored correctly
523 * when a single step is taken). Looks like a /proc bug.
525 if (*sym_name == '.')
526 return (0);
529 * For each pattern in the array of symbol patterns,
530 * if the pattern matches the symbol name, then
531 * create a breakpoint at the function in question.
533 for (i = 0; i < Dyp->nsympat; i++) {
534 if (interrupt|sigusr1)
535 break;
536 if (fnmatch(Dyp->sympat[i], sym_name, 0) != 0)
537 continue;
539 if ((Bp = create_bkpt(pc, 0, 0)) == NULL) /* can't fail */
540 return (0);
543 * New breakpoints receive a name now.
544 * For existing breakpoints, prefer the subset name if possible,
545 * else prefer the shorter name.
547 if (Bp->sym_name == NULL) {
548 Bp->sym_name = strdup(sym_name);
549 } else if (strstr(Bp->sym_name, sym_name) != NULL ||
550 strlen(Bp->sym_name) > strlen(sym_name)) {
551 free(Bp->sym_name);
552 Bp->sym_name = strdup(sym_name);
554 Bp->dyn = Dp;
555 Bp->flags |= Dyp->flag;
556 if (Dyp->exclude)
557 Bp->flags |= BPT_EXCLUDE;
558 else if (Dyp->internal || *Dp->prt_name == '\0')
559 Bp->flags |= BPT_INTERNAL;
560 return (0);
563 return (interrupt | sigusr1);
566 /* For debugging only ---- */
567 void
568 report_htable_stats(void)
570 const pstatus_t *Psp = Pstatus(Proc);
571 struct callstack *Stk;
572 struct bkpt *Bp;
573 uint_t Min = 1000000;
574 uint_t Max = 0;
575 uint_t Avg = 0;
576 uint_t Total = 0;
577 uint_t i, j;
578 uint_t bucket[HASHSZ];
580 if (Dynpat == NULL || !hflag)
581 return;
583 hflag = FALSE;
584 (void) memset(bucket, 0, sizeof (bucket));
586 for (i = 0; i < HASHSZ; i++) {
587 j = 0;
588 for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next)
589 j++;
590 if (j < Min)
591 Min = j;
592 if (j > Max)
593 Max = j;
594 if (j < HASHSZ)
595 bucket[j]++;
596 Total += j;
598 Avg = (Total + HASHSZ / 2) / HASHSZ;
599 (void) fprintf(stderr, "truss hash table statistics --------\n");
600 (void) fprintf(stderr, " Total = %u\n", Total);
601 (void) fprintf(stderr, " Min = %u\n", Min);
602 (void) fprintf(stderr, " Max = %u\n", Max);
603 (void) fprintf(stderr, " Avg = %u\n", Avg);
604 for (i = 0; i < HASHSZ; i++)
605 if (bucket[i])
606 (void) fprintf(stderr, " %3u buckets of size %d\n",
607 bucket[i], i);
609 (void) fprintf(stderr, "truss-detected stacks --------\n");
610 for (Stk = callstack; Stk != NULL; Stk = Stk->next) {
611 (void) fprintf(stderr,
612 " base = 0x%.8lx end = 0x%.8lx size = %ld\n",
613 (ulong_t)Stk->stkbase,
614 (ulong_t)Stk->stkend,
615 (ulong_t)(Stk->stkend - Stk->stkbase));
617 (void) fprintf(stderr, "primary unix stack --------\n");
618 (void) fprintf(stderr,
619 " base = 0x%.8lx end = 0x%.8lx size = %ld\n",
620 (ulong_t)Psp->pr_stkbase,
621 (ulong_t)(Psp->pr_stkbase + Psp->pr_stksize),
622 (ulong_t)Psp->pr_stksize);
623 (void) fprintf(stderr, "nthr_create = %u\n", nthr_create);
626 void
627 make_lwp_stack(const lwpstatus_t *Lsp, prmap_t *Pmap, int nmap)
629 const pstatus_t *Psp = Pstatus(Proc);
630 uintptr_t sp = Lsp->pr_reg[R_SP];
631 id_t lwpid = Lsp->pr_lwpid;
632 struct callstack *Stk;
633 td_thrhandle_t th;
634 td_thrinfo_t thrinfo;
636 if (data_model != PR_MODEL_LP64)
637 sp = (uint32_t)sp;
639 /* check to see if we already have this stack */
640 if (sp == 0)
641 return;
642 for (Stk = callstack; Stk != NULL; Stk = Stk->next)
643 if (sp >= Stk->stkbase && sp < Stk->stkend)
644 return;
646 Stk = my_malloc(sizeof (struct callstack), NULL);
647 Stk->next = callstack;
648 callstack = Stk;
649 nstack++;
650 Stk->tref = 0;
651 Stk->tid = 0;
652 Stk->nthr_create = 0;
653 Stk->ncall = 0;
654 Stk->maxcall = DEF_MAXCALL;
655 Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL);
657 /* primary stack */
658 if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
659 Stk->stkbase = Psp->pr_stkbase;
660 Stk->stkend = Stk->stkbase + Psp->pr_stksize;
661 return;
664 /* alternate stack */
665 if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) &&
666 sp >= (uintptr_t)Lsp->pr_altstack.ss_sp &&
667 sp < (uintptr_t)Lsp->pr_altstack.ss_sp
668 + Lsp->pr_altstack.ss_size) {
669 Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp;
670 Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size;
671 return;
674 /* thread stacks? */
675 if (Thr_agent != NULL &&
676 td_ta_map_lwp2thr(Thr_agent, lwpid, &th) == TD_OK &&
677 td_thr_get_info(&th, &thrinfo) == TD_OK &&
678 sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
679 sp < (uintptr_t)thrinfo.ti_stkbase) {
680 /* The bloody fools got this backwards! */
681 Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
682 Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
683 return;
686 /* last chance -- try the raw memory map */
687 for (; nmap; nmap--, Pmap++) {
688 if (sp >= Pmap->pr_vaddr &&
689 sp < Pmap->pr_vaddr + Pmap->pr_size) {
690 Stk->stkbase = Pmap->pr_vaddr;
691 Stk->stkend = Pmap->pr_vaddr + Pmap->pr_size;
692 return;
696 callstack = Stk->next;
697 nstack--;
698 free(Stk->stack);
699 free(Stk);
702 void
703 make_thr_stack(const td_thrhandle_t *Thp, prgregset_t reg)
705 const pstatus_t *Psp = Pstatus(Proc);
706 td_thrinfo_t thrinfo;
707 uintptr_t sp = reg[R_SP];
708 struct callstack *Stk;
710 if (data_model != PR_MODEL_LP64)
711 sp = (uint32_t)sp;
713 /* check to see if we already have this stack */
714 if (sp == 0)
715 return;
716 for (Stk = callstack; Stk != NULL; Stk = Stk->next)
717 if (sp >= Stk->stkbase && sp < Stk->stkend)
718 return;
720 Stk = my_malloc(sizeof (struct callstack), NULL);
721 Stk->next = callstack;
722 callstack = Stk;
723 nstack++;
724 Stk->tref = 0;
725 Stk->tid = 0;
726 Stk->nthr_create = 0;
727 Stk->ncall = 0;
728 Stk->maxcall = DEF_MAXCALL;
729 Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack), NULL);
731 /* primary stack */
732 if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
733 Stk->stkbase = Psp->pr_stkbase;
734 Stk->stkend = Stk->stkbase + Psp->pr_stksize;
735 return;
738 if (td_thr_get_info(Thp, &thrinfo) == TD_OK &&
739 sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
740 sp < (uintptr_t)thrinfo.ti_stkbase) {
741 /* The bloody fools got this backwards! */
742 Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
743 Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
744 return;
747 callstack = Stk->next;
748 nstack--;
749 free(Stk->stack);
750 free(Stk);
753 struct callstack *
754 find_lwp_stack(uintptr_t sp)
756 const pstatus_t *Psp = Pstatus(Proc);
757 char mapfile[64];
758 int mapfd;
759 struct stat statb;
760 prmap_t *Pmap = NULL;
761 prmap_t *pmap = NULL;
762 int nmap = 0;
763 struct callstack *Stk = NULL;
766 * Get the address space map.
768 (void) sprintf(mapfile, "/proc/%d/rmap", (int)Psp->pr_pid);
769 if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
770 fstat(mapfd, &statb) != 0 ||
771 statb.st_size < sizeof (prmap_t) ||
772 (Pmap = my_malloc(statb.st_size, NULL)) == NULL ||
773 (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
774 (nmap /= sizeof (prmap_t)) == 0) {
775 free(Pmap);
776 if (mapfd >= 0)
777 (void) close(mapfd);
778 return (NULL);
780 (void) close(mapfd);
782 for (pmap = Pmap; nmap--; pmap++) {
783 if (sp >= pmap->pr_vaddr &&
784 sp < pmap->pr_vaddr + pmap->pr_size) {
785 Stk = my_malloc(sizeof (struct callstack), NULL);
786 Stk->next = callstack;
787 callstack = Stk;
788 nstack++;
789 Stk->stkbase = pmap->pr_vaddr;
790 Stk->stkend = pmap->pr_vaddr + pmap->pr_size;
791 Stk->tref = 0;
792 Stk->tid = 0;
793 Stk->nthr_create = 0;
794 Stk->ncall = 0;
795 Stk->maxcall = DEF_MAXCALL;
796 Stk->stack = my_malloc(
797 DEF_MAXCALL * sizeof (*Stk->stack), NULL);
798 break;
802 free(Pmap);
803 return (Stk);
806 struct callstack *
807 find_stack(uintptr_t sp)
809 const pstatus_t *Psp = Pstatus(Proc);
810 private_t *pri = get_private();
811 const lwpstatus_t *Lsp = pri->lwpstat;
812 id_t lwpid = Lsp->pr_lwpid;
813 #if defined(__sparc)
814 prgreg_t tref = Lsp->pr_reg[R_G7];
815 #elif defined(__amd64)
816 prgreg_t tref = Lsp->pr_reg[REG_FS];
817 #elif defined(__i386)
818 prgreg_t tref = Lsp->pr_reg[GS];
819 #endif
820 struct callstack *Stk = NULL;
821 td_thrhandle_t th;
822 td_thrinfo_t thrinfo;
823 td_err_e error;
825 /* primary stack */
826 if (sp >= Psp->pr_stkbase && sp < Psp->pr_stkbase + Psp->pr_stksize) {
827 Stk = my_malloc(sizeof (struct callstack), NULL);
828 Stk->next = callstack;
829 callstack = Stk;
830 nstack++;
831 Stk->stkbase = Psp->pr_stkbase;
832 Stk->stkend = Stk->stkbase + Psp->pr_stksize;
833 Stk->tref = 0;
834 Stk->tid = 0;
835 Stk->nthr_create = 0;
836 Stk->ncall = 0;
837 Stk->maxcall = DEF_MAXCALL;
838 Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
839 NULL);
840 return (Stk);
843 /* alternate stack */
844 if ((Lsp->pr_altstack.ss_flags & SS_ONSTACK) &&
845 sp >= (uintptr_t)Lsp->pr_altstack.ss_sp &&
846 sp < (uintptr_t)Lsp->pr_altstack.ss_sp
847 + Lsp->pr_altstack.ss_size) {
848 Stk = my_malloc(sizeof (struct callstack), NULL);
849 Stk->next = callstack;
850 callstack = Stk;
851 nstack++;
852 Stk->stkbase = (uintptr_t)Lsp->pr_altstack.ss_sp;
853 Stk->stkend = Stk->stkbase + Lsp->pr_altstack.ss_size;
854 Stk->tref = 0;
855 Stk->tid = 0;
856 Stk->nthr_create = 0;
857 Stk->ncall = 0;
858 Stk->maxcall = DEF_MAXCALL;
859 Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
860 NULL);
861 return (Stk);
864 if (Thr_agent == NULL)
865 return (find_lwp_stack(sp));
867 /* thread stacks? */
868 if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) {
869 if (hflag)
870 (void) fprintf(stderr,
871 "cannot get thread handle for "
872 "lwp#%d, error=%d, tref=0x%.8lx\n",
873 (int)lwpid, error, (long)tref);
874 return (NULL);
877 if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) {
878 if (hflag)
879 (void) fprintf(stderr,
880 "cannot get thread info for "
881 "lwp#%d, error=%d, tref=0x%.8lx\n",
882 (int)lwpid, error, (long)tref);
883 return (NULL);
886 if (sp >= (uintptr_t)thrinfo.ti_stkbase - thrinfo.ti_stksize &&
887 sp < (uintptr_t)thrinfo.ti_stkbase) {
888 Stk = my_malloc(sizeof (struct callstack), NULL);
889 Stk->next = callstack;
890 callstack = Stk;
891 nstack++;
892 /* The bloody fools got this backwards! */
893 Stk->stkend = (uintptr_t)thrinfo.ti_stkbase;
894 Stk->stkbase = Stk->stkend - thrinfo.ti_stksize;
895 Stk->tref = tref;
896 Stk->tid = thrinfo.ti_tid;
897 Stk->nthr_create = nthr_create;
898 Stk->ncall = 0;
899 Stk->maxcall = DEF_MAXCALL;
900 Stk->stack = my_malloc(DEF_MAXCALL * sizeof (*Stk->stack),
901 NULL);
902 return (Stk);
905 /* stack bounds failure -- complain bitterly */
906 if (hflag) {
907 (void) fprintf(stderr,
908 "sp not within thread stack: "
909 "sp=0x%.8lx stkbase=0x%.8lx stkend=0x%.8lx\n",
910 (ulong_t)sp,
911 /* The bloody fools got this backwards! */
912 (ulong_t)thrinfo.ti_stkbase - thrinfo.ti_stksize,
913 (ulong_t)thrinfo.ti_stkbase);
916 return (NULL);
919 void
920 get_tid(struct callstack *Stk)
922 private_t *pri = get_private();
923 const lwpstatus_t *Lsp = pri->lwpstat;
924 id_t lwpid = Lsp->pr_lwpid;
925 #if defined(__sparc)
926 prgreg_t tref = Lsp->pr_reg[R_G7];
927 #elif defined(__amd64)
928 prgreg_t tref = (data_model == PR_MODEL_LP64) ?
929 Lsp->pr_reg[REG_FS] : Lsp->pr_reg[REG_GS];
930 #elif defined(__i386)
931 prgreg_t tref = Lsp->pr_reg[GS];
932 #endif
933 td_thrhandle_t th;
934 td_thrinfo_t thrinfo;
935 td_err_e error;
937 if (Thr_agent == NULL) {
938 Stk->tref = 0;
939 Stk->tid = 0;
940 Stk->nthr_create = 0;
941 return;
945 * Shortcut here --
946 * If we have a matching tref and no new threads have
947 * been created since the last time we encountered this
948 * stack, then we don't have to go through the overhead
949 * of calling td_ta_map_lwp2thr() to get the thread-id.
951 if (tref == Stk->tref && Stk->nthr_create == nthr_create)
952 return;
954 if ((error = td_ta_map_lwp2thr(Thr_agent, lwpid, &th)) != TD_OK) {
955 if (hflag)
956 (void) fprintf(stderr,
957 "cannot get thread handle for "
958 "lwp#%d, error=%d, tref=0x%.8lx\n",
959 (int)lwpid, error, (long)tref);
960 Stk->tref = 0;
961 Stk->tid = 0;
962 Stk->nthr_create = 0;
963 } else if ((error = td_thr_get_info(&th, &thrinfo)) != TD_OK) {
964 if (hflag)
965 (void) fprintf(stderr,
966 "cannot get thread info for "
967 "lwp#%d, error=%d, tref=0x%.8lx\n",
968 (int)lwpid, error, (long)tref);
969 Stk->tref = 0;
970 Stk->tid = 0;
971 Stk->nthr_create = 0;
972 } else {
973 Stk->tref = tref;
974 Stk->tid = thrinfo.ti_tid;
975 Stk->nthr_create = nthr_create;
979 struct callstack *
980 callstack_info(uintptr_t sp, uintptr_t fp, int makeid)
982 struct callstack *Stk;
983 uintptr_t trash;
985 if (sp == 0 ||
986 Pread(Proc, &trash, sizeof (trash), sp) != sizeof (trash))
987 return (NULL);
989 for (Stk = callstack; Stk != NULL; Stk = Stk->next)
990 if (sp >= Stk->stkbase && sp < Stk->stkend)
991 break;
994 * If we didn't find the stack, do it the hard way.
996 if (Stk == NULL) {
997 uintptr_t stkbase = sp;
998 uintptr_t stkend;
999 uint_t minsize;
1001 #if defined(i386) || defined(__amd64)
1002 #ifdef _LP64
1003 if (data_model == PR_MODEL_LP64)
1004 minsize = 2 * sizeof (uintptr_t); /* fp + pc */
1005 else
1006 #endif
1007 minsize = 2 * sizeof (uint32_t);
1008 #else
1009 #ifdef _LP64
1010 if (data_model != PR_MODEL_LP64)
1011 minsize = SA32(MINFRAME32);
1012 else
1013 minsize = SA64(MINFRAME64);
1014 #else
1015 minsize = SA(MINFRAME);
1016 #endif
1017 #endif /* i386 */
1018 stkend = sp + minsize;
1020 while (Stk == NULL && fp != 0 && fp >= sp) {
1021 stkend = fp + minsize;
1022 for (Stk = callstack; Stk != NULL; Stk = Stk->next)
1023 if ((fp >= Stk->stkbase && fp < Stk->stkend) ||
1024 (stkend > Stk->stkbase &&
1025 stkend <= Stk->stkend))
1026 break;
1027 if (Stk == NULL)
1028 fp = previous_fp(fp, NULL);
1031 if (Stk != NULL) /* the stack grew */
1032 Stk->stkbase = stkbase;
1035 if (Stk == NULL && makeid) /* new stack */
1036 Stk = find_stack(sp);
1038 if (Stk == NULL)
1039 return (NULL);
1042 * Ensure that there is room for at least one more entry.
1044 if (Stk->ncall == Stk->maxcall) {
1045 Stk->maxcall *= 2;
1046 Stk->stack = my_realloc(Stk->stack,
1047 Stk->maxcall * sizeof (*Stk->stack), NULL);
1050 if (makeid)
1051 get_tid(Stk);
1053 return (Stk);
1057 * Reset the breakpoint information (called on successful exec()).
1059 void
1060 reset_breakpoints(void)
1062 struct dynlib *Dp;
1063 struct bkpt *Bp;
1064 struct callstack *Stk;
1065 int i;
1067 if (Dynpat == NULL)
1068 return;
1070 /* destroy all previous dynamic library information */
1071 while ((Dp = Dynlib) != NULL) {
1072 Dynlib = Dp->next;
1073 free(Dp->lib_name);
1074 free(Dp->match_name);
1075 free(Dp->prt_name);
1076 free(Dp);
1079 /* destroy all previous breakpoint trap information */
1080 if (bpt_hashtable != NULL) {
1081 for (i = 0; i < HASHSZ; i++) {
1082 while ((Bp = bpt_hashtable[i]) != NULL) {
1083 bpt_hashtable[i] = Bp->next;
1084 free(Bp->sym_name);
1085 free(Bp);
1090 /* destroy all the callstack information */
1091 while ((Stk = callstack) != NULL) {
1092 callstack = Stk->next;
1093 free(Stk->stack);
1094 free(Stk);
1097 /* we are not a multi-threaded process anymore */
1098 if (Thr_agent != NULL)
1099 (void) td_ta_delete(Thr_agent);
1100 Thr_agent = NULL;
1102 /* tell libproc to clear out its mapping information */
1103 Preset_maps(Proc);
1104 Rdb_agent = NULL;
1106 /* Reestablish the symbols from the executable */
1107 (void) establish_breakpoints();
1111 * Clear breakpoints from the process (called before Prelease()).
1112 * Don't actually destroy the breakpoint table;
1113 * threads currently fielding breakpoints will need it.
1115 void
1116 clear_breakpoints(void)
1118 struct bkpt *Bp;
1119 int i;
1121 if (Dynpat == NULL)
1122 return;
1125 * Change all breakpoint traps back to normal instructions.
1126 * We attempt to remove a breakpoint from every address which
1127 * may have ever contained a breakpoint to protect our victims.
1129 report_htable_stats(); /* report stats first */
1130 for (i = 0; i < HASHSZ; i++) {
1131 for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
1132 if (Bp->flags & BPT_ACTIVE)
1133 (void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
1134 Bp->flags &= ~BPT_ACTIVE;
1138 if (Thr_agent != NULL) {
1139 td_thr_events_t events;
1141 td_event_fillset(&events);
1142 (void) td_ta_clear_event(Thr_agent, &events);
1143 (void) td_ta_delete(Thr_agent);
1145 Thr_agent = NULL;
1149 * Reestablish the breakpoint traps in the process.
1150 * Called after resuming from a vfork() in the parent.
1152 void
1153 reestablish_traps(void)
1155 struct bkpt *Bp;
1156 ulong_t instr;
1157 int i;
1159 if (Dynpat == NULL || is_vfork_child)
1160 return;
1162 for (i = 0; i < HASHSZ; i++) {
1163 for (Bp = bpt_hashtable[i]; Bp != NULL; Bp = Bp->next) {
1164 if ((Bp->flags & BPT_ACTIVE) &&
1165 Psetbkpt(Proc, Bp->addr, &instr) != 0)
1166 Bp->flags &= ~BPT_ACTIVE;
1171 void
1172 show_function_call(private_t *pri,
1173 struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp)
1175 long arg[8];
1176 int narg;
1177 int i;
1179 narg = get_arguments(arg);
1180 make_pname(pri, (Stk != NULL)? Stk->tid : 0);
1181 putpname(pri);
1182 timestamp(pri);
1183 if (Stk != NULL) {
1184 for (i = 1; i < Stk->ncall; i++) {
1185 (void) fputc(' ', stdout);
1186 (void) fputc(' ', stdout);
1189 (void) printf("-> %s%s(", Dp->prt_name, Bp->sym_name);
1190 for (i = 0; i < narg; i++) {
1191 (void) printf("0x%lx", arg[i]);
1192 if (i < narg-1) {
1193 (void) fputc(',', stdout);
1194 (void) fputc(' ', stdout);
1197 (void) printf(")\n");
1198 Flush();
1201 /* ARGSUSED */
1202 void
1203 show_function_return(private_t *pri, long rval, int stret,
1204 struct callstack *Stk, struct dynlib *Dp, struct bkpt *Bp)
1206 int i;
1208 make_pname(pri, Stk->tid);
1209 putpname(pri);
1210 timestamp(pri);
1211 for (i = 0; i < Stk->ncall; i++) {
1212 (void) fputc(' ', stdout);
1213 (void) fputc(' ', stdout);
1215 (void) printf("<- %s%s() = ", Dp->prt_name, Bp->sym_name);
1216 if (stret) {
1217 (void) printf("struct return\n");
1218 } else if (data_model == PR_MODEL_LP64) {
1219 if (rval >= (64 * 1024) || -rval >= (64 * 1024))
1220 (void) printf("0x%lx\n", rval);
1221 else
1222 (void) printf("%ld\n", rval);
1223 } else {
1224 int rval32 = (int)rval;
1225 if (rval32 >= (64 * 1024) || -rval32 >= (64 * 1024))
1226 (void) printf("0x%x\n", rval32);
1227 else
1228 (void) printf("%d\n", rval32);
1230 Flush();
1234 * Called to deal with function-call tracing.
1235 * Return 0 on normal success, 1 to indicate a BPT_HANG success,
1236 * and -1 on failure (not tracing functions or unknown breakpoint).
1239 function_trace(private_t *pri, int first, int clear, int dotrace)
1241 struct ps_lwphandle *Lwp = pri->Lwp;
1242 const lwpstatus_t *Lsp = pri->lwpstat;
1243 uintptr_t pc = Lsp->pr_reg[R_PC];
1244 uintptr_t sp = Lsp->pr_reg[R_SP];
1245 uintptr_t fp = Lsp->pr_reg[R_FP];
1246 struct bkpt *Bp;
1247 struct dynlib *Dp;
1248 struct callstack *Stk;
1249 ulong_t instr;
1250 int active;
1251 int rval = 0;
1253 if (Dynpat == NULL)
1254 return (-1);
1256 if (data_model != PR_MODEL_LP64) {
1257 pc = (uint32_t)pc;
1258 sp = (uint32_t)sp;
1259 fp = (uint32_t)fp;
1262 if ((Bp = get_bkpt(pc)) == NULL) {
1263 if (hflag)
1264 (void) fprintf(stderr,
1265 "function_trace(): "
1266 "cannot find breakpoint for pc: 0x%.8lx\n",
1267 (ulong_t)pc);
1268 return (-1);
1271 if ((Bp->flags & (BPT_PREINIT|BPT_POSTINIT|BPT_DLACTIVITY)) && !clear) {
1272 rd_event_msg_t event_msg;
1274 if (hflag) {
1275 if (Bp->flags & BPT_PREINIT)
1276 (void) fprintf(stderr, "function_trace(): "
1277 "RD_PREINIT breakpoint\n");
1278 if (Bp->flags & BPT_POSTINIT)
1279 (void) fprintf(stderr, "function_trace(): "
1280 "RD_POSTINIT breakpoint\n");
1281 if (Bp->flags & BPT_DLACTIVITY)
1282 (void) fprintf(stderr, "function_trace(): "
1283 "RD_DLACTIVITY breakpoint\n");
1285 if (rd_event_getmsg(Rdb_agent, &event_msg) == RD_OK) {
1286 if (event_msg.type == RD_DLACTIVITY) {
1287 switch (event_msg.u.state) {
1288 case RD_CONSISTENT:
1289 establish_breakpoints();
1290 break;
1291 case RD_ADD:
1292 not_consist = TRUE; /* kludge */
1293 establish_breakpoints();
1294 not_consist = FALSE;
1295 break;
1296 case RD_DELETE:
1297 delete_library = TRUE;
1298 break;
1299 default:
1300 break;
1303 if (hflag) {
1304 const char *et;
1305 char buf[32];
1307 switch (event_msg.type) {
1308 case RD_NONE:
1309 et = "RD_NONE";
1310 break;
1311 case RD_PREINIT:
1312 et = "RD_PREINIT";
1313 break;
1314 case RD_POSTINIT:
1315 et = "RD_POSTINIT";
1316 break;
1317 case RD_DLACTIVITY:
1318 et = "RD_DLACTIVITY";
1319 break;
1320 default:
1321 (void) sprintf(buf, "0x%x",
1322 event_msg.type);
1323 et = buf;
1324 break;
1326 (void) fprintf(stderr,
1327 "event_msg.type = %s ", et);
1328 switch (event_msg.u.state) {
1329 case RD_NOSTATE:
1330 et = "RD_NOSTATE";
1331 break;
1332 case RD_CONSISTENT:
1333 et = "RD_CONSISTENT";
1334 break;
1335 case RD_ADD:
1336 et = "RD_ADD";
1337 break;
1338 case RD_DELETE:
1339 et = "RD_DELETE";
1340 break;
1341 default:
1342 (void) sprintf(buf, "0x%x",
1343 event_msg.u.state);
1344 et = buf;
1345 break;
1347 (void) fprintf(stderr,
1348 "event_msg.u.state = %s\n", et);
1353 if ((Bp->flags & BPT_TD_CREATE) && !clear) {
1354 nthr_create++;
1355 if (hflag)
1356 (void) fprintf(stderr, "function_trace(): "
1357 "BPT_TD_CREATE breakpoint\n");
1358 /* we don't care about the event message */
1361 Dp = Bp->dyn;
1363 if (dotrace) {
1364 if ((Stk = callstack_info(sp, fp, 1)) == NULL) {
1365 if (Dp != NULL && !clear) {
1366 if (cflag) {
1367 add_fcall(fcall_tbl, Dp->prt_name,
1368 Bp->sym_name, (unsigned long)1);
1370 else
1371 show_function_call(pri, NULL, Dp, Bp);
1372 if ((Bp->flags & BPT_HANG) && !first)
1373 rval = 1;
1375 } else if (!clear) {
1376 if (Dp != NULL) {
1377 function_entry(pri, Bp, Stk);
1378 if ((Bp->flags & BPT_HANG) && !first)
1379 rval = 1;
1380 } else {
1381 function_return(pri, Stk);
1387 * Single-step the traced instruction. Since it's possible that
1388 * another thread has deactivated this breakpoint, we indicate
1389 * that we have reactivated it by virtue of executing it.
1391 * To avoid a deadlock with some other thread in the process
1392 * performing a fork() or a thr_suspend() operation, we must
1393 * drop and later reacquire truss_lock. Some fancy dancing here.
1395 active = (Bp->flags & BPT_ACTIVE);
1396 Bp->flags |= BPT_ACTIVE;
1397 instr = Bp->instr;
1398 (void) mutex_unlock(&truss_lock);
1399 (void) Lxecbkpt(Lwp, instr);
1400 (void) mutex_lock(&truss_lock);
1402 if (rval || clear) { /* leave process stopped and abandoned */
1403 #if defined(__i386)
1405 * Leave it stopped in a state that a stack trace is reasonable.
1407 /* XX64 needs to be updated for amd64 & gcc */
1408 if (rval && instr == 0x55) { /* pushl %ebp */
1409 /* step it over the movl %esp,%ebp */
1410 (void) mutex_unlock(&truss_lock);
1411 (void) Lsetrun(Lwp, 0, PRCFAULT|PRSTEP);
1412 /* we're wrapping up; wait one second at most */
1413 (void) Lwait(Lwp, MILLISEC);
1414 (void) mutex_lock(&truss_lock);
1416 #endif
1417 if (get_bkpt(pc) != Bp)
1418 abend("function_trace: lost breakpoint", NULL);
1419 (void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
1420 Bp->flags &= ~BPT_ACTIVE;
1421 (void) mutex_unlock(&truss_lock);
1422 (void) Lsetrun(Lwp, 0, PRCFAULT|PRSTOP);
1423 /* we're wrapping up; wait one second at most */
1424 (void) Lwait(Lwp, MILLISEC);
1425 (void) mutex_lock(&truss_lock);
1426 } else {
1427 if (get_bkpt(pc) != Bp)
1428 abend("function_trace: lost breakpoint", NULL);
1429 if (!active || !(Bp->flags & BPT_ACTIVE)) {
1430 (void) Pdelbkpt(Proc, Bp->addr, Bp->instr);
1431 Bp->flags &= ~BPT_ACTIVE;
1434 return (rval);
1437 void
1438 function_entry(private_t *pri, struct bkpt *Bp, struct callstack *Stk)
1440 const lwpstatus_t *Lsp = pri->lwpstat;
1441 uintptr_t sp = Lsp->pr_reg[R_SP];
1442 uintptr_t rpc = get_return_address(&sp);
1443 struct dynlib *Dp = Bp->dyn;
1444 int oldframe = FALSE;
1445 int i;
1447 #ifdef _LP64
1448 if (data_model != PR_MODEL_LP64) {
1449 sp = (uint32_t)sp;
1450 rpc = (uint32_t)rpc;
1452 #endif
1455 * If the sp is not within the stack bounds, forget it.
1456 * If the symbol's 'internal' flag is false,
1457 * don't report internal calls within the library.
1459 if (!(sp >= Stk->stkbase && sp < Stk->stkend) ||
1460 (!(Bp->flags & BPT_INTERNAL) &&
1461 rpc >= Dp->base && rpc < Dp->base + Dp->size))
1462 return;
1464 for (i = 0; i < Stk->ncall; i++) {
1465 if (sp >= Stk->stack[i].sp) {
1466 Stk->ncall = i;
1467 if (sp == Stk->stack[i].sp)
1468 oldframe = TRUE;
1469 break;
1474 * Breakpoints for function returns are set here
1475 * If we're counting function calls, there is no need to set
1476 * a breakpoint upon return
1479 if (!oldframe && !cflag) {
1480 (void) create_bkpt(rpc, 1, 1); /* may or may not be set */
1481 Stk->stack[Stk->ncall].sp = sp; /* record it anyeay */
1482 Stk->stack[Stk->ncall].pc = rpc;
1483 Stk->stack[Stk->ncall].fcn = Bp;
1485 Stk->ncall++;
1486 if (cflag) {
1487 add_fcall(fcall_tbl, Dp->prt_name, Bp->sym_name,
1488 (unsigned long)1);
1489 } else {
1490 show_function_call(pri, Stk, Dp, Bp);
1495 * We are here because we hit an unnamed breakpoint.
1496 * Attempt to match this up with a return pc on the stack
1497 * and report the function return.
1499 void
1500 function_return(private_t *pri, struct callstack *Stk)
1502 const lwpstatus_t *Lsp = pri->lwpstat;
1503 uintptr_t sp = Lsp->pr_reg[R_SP];
1504 uintptr_t fp = Lsp->pr_reg[R_FP];
1505 int i;
1507 #ifdef _LP64
1508 if (data_model != PR_MODEL_LP64) {
1509 sp = (uint32_t)sp;
1510 fp = (uint32_t)fp;
1512 #endif
1514 if (fp < sp + 8)
1515 fp = sp + 8;
1517 for (i = Stk->ncall - 1; i >= 0; i--) {
1518 if (sp <= Stk->stack[i].sp && fp > Stk->stack[i].sp) {
1519 Stk->ncall = i;
1520 break;
1524 #if defined(i386) || defined(__amd64)
1525 if (i < 0) {
1526 /* probably __mul64() or friends -- try harder */
1527 int j;
1528 for (j = 0; i < 0 && j < 8; j++) { /* up to 8 args */
1529 sp -= 4;
1530 for (i = Stk->ncall - 1; i >= 0; i--) {
1531 if (sp <= Stk->stack[i].sp &&
1532 fp > Stk->stack[i].sp) {
1533 Stk->ncall = i;
1534 break;
1539 #endif
1541 if ((i >= 0) && (!cflag)) {
1542 show_function_return(pri, Lsp->pr_reg[R_R0], 0,
1543 Stk, Stk->stack[i].fcn->dyn, Stk->stack[i].fcn);
1547 #if defined(__sparc)
1548 #define FPADJUST 0
1549 #elif defined(__amd64)
1550 #define FPADJUST 8
1551 #elif defined(__i386)
1552 #define FPADJUST 4
1553 #endif
1555 void
1556 trap_one_stack(prgregset_t reg)
1558 struct dynlib *Dp;
1559 struct bkpt *Bp;
1560 struct callstack *Stk;
1561 GElf_Sym sym;
1562 char sym_name[32];
1563 uintptr_t sp = reg[R_SP];
1564 uintptr_t pc = reg[R_PC];
1565 uintptr_t fp;
1566 uintptr_t rpc;
1567 uint_t nframe = 0;
1568 uint_t maxframe = 8;
1569 struct {
1570 uintptr_t sp; /* %sp within called function */
1571 uintptr_t pc; /* %pc within called function */
1572 uintptr_t rsp; /* the return sp */
1573 uintptr_t rpc; /* the return pc */
1574 } *frame = my_malloc(maxframe * sizeof (*frame), NULL);
1577 * Gather stack frames bottom to top.
1579 while (sp != 0) {
1580 fp = sp; /* remember higest non-null sp */
1581 frame[nframe].sp = sp;
1582 frame[nframe].pc = pc;
1583 sp = previous_fp(sp, &pc);
1584 frame[nframe].rsp = sp;
1585 frame[nframe].rpc = pc;
1586 if (++nframe == maxframe) {
1587 maxframe *= 2;
1588 frame = my_realloc(frame, maxframe * sizeof (*frame),
1589 NULL);
1594 * Scan for function return breakpoints top to bottom.
1596 while (nframe--) {
1597 /* lookup the called function in the symbol tables */
1598 if (Plookup_by_addr(Proc, frame[nframe].pc, sym_name,
1599 sizeof (sym_name), &sym) != 0)
1600 continue;
1602 pc = sym.st_value; /* entry point of the function */
1603 rpc = frame[nframe].rpc; /* caller's return pc */
1605 /* lookup the function in the breakpoint table */
1606 if ((Bp = get_bkpt(pc)) == NULL || (Dp = Bp->dyn) == NULL)
1607 continue;
1609 if (!(Bp->flags & BPT_INTERNAL) &&
1610 rpc >= Dp->base && rpc < Dp->base + Dp->size)
1611 continue;
1613 sp = frame[nframe].rsp + FPADJUST; /* %sp at time of call */
1614 if ((Stk = callstack_info(sp, fp, 0)) == NULL)
1615 continue; /* can't happen? */
1617 if (create_bkpt(rpc, 1, 1) != NULL) {
1618 Stk->stack[Stk->ncall].sp = sp;
1619 Stk->stack[Stk->ncall].pc = rpc;
1620 Stk->stack[Stk->ncall].fcn = Bp;
1621 Stk->ncall++;
1625 free(frame);
1629 lwp_stack_traps(void *cd, const lwpstatus_t *Lsp)
1631 ph_map_t *ph_map = (ph_map_t *)cd;
1632 prgregset_t reg;
1634 (void) memcpy(reg, Lsp->pr_reg, sizeof (prgregset_t));
1635 make_lwp_stack(Lsp, ph_map->pmap, ph_map->nmap);
1636 trap_one_stack(reg);
1638 return (interrupt | sigusr1);
1641 /* ARGSUSED */
1643 thr_stack_traps(const td_thrhandle_t *Thp, void *cd)
1645 prgregset_t reg;
1648 * We have already dealt with all the lwps.
1649 * We only care about unbound threads here (TD_PARTIALREG).
1651 if (td_thr_getgregs(Thp, reg) != TD_PARTIALREG)
1652 return (0);
1654 make_thr_stack(Thp, reg);
1655 trap_one_stack(reg);
1657 return (interrupt | sigusr1);
1661 #if defined(__i386) || defined(__amd64)
1663 uintptr_t
1664 previous_fp(uintptr_t fp, uintptr_t *rpc)
1666 uintptr_t frame[2];
1667 uintptr_t trash[2];
1669 if (Pread(Proc, frame, sizeof (frame), fp) != sizeof (frame) ||
1670 (frame[0] != 0 &&
1671 Pread(Proc, trash, sizeof (trash), frame[0]) != sizeof (trash)))
1672 frame[0] = frame[1] = 0;
1674 if (rpc)
1675 *rpc = frame[1];
1676 return (frame[0]);
1679 #endif
1681 #if defined(__amd64) || defined(__i386)
1684 * Examine the instruction at the return location of a function call
1685 * and return the byte count by which the stack is adjusted on return.
1686 * It the instruction at the return location is an addl, as expected,
1687 * then adjust the return pc by the size of that instruction so that
1688 * we will place the return breakpoint on the following instruction.
1689 * This allows programs that interrogate their own stacks and record
1690 * function calls and arguments to work correctly even while we interfere.
1691 * Return the count on success, -1 on failure.
1694 return_count32(uint32_t *ppc)
1696 uintptr_t pc = *ppc;
1697 struct bkpt *Bp;
1698 int count;
1699 uchar_t instr[6]; /* instruction at pc */
1701 if ((count = Pread(Proc, instr, sizeof (instr), pc)) < 0)
1702 return (-1);
1704 /* find the replaced instruction at pc (if any) */
1705 if ((Bp = get_bkpt(pc)) != NULL && (Bp->flags & BPT_ACTIVE))
1706 instr[0] = (uchar_t)Bp->instr;
1708 if (count != sizeof (instr) &&
1709 (count < 3 || instr[0] != 0x83))
1710 return (-1);
1713 * A bit of disassembly of the instruction is required here.
1715 if (instr[1] != 0xc4) { /* not an addl mumble,%esp inctruction */
1716 count = 0;
1717 } else if (instr[0] == 0x81) { /* count is a longword */
1718 count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24);
1719 *ppc += 6;
1720 } else if (instr[0] == 0x83) { /* count is a byte */
1721 count = instr[2];
1722 *ppc += 3;
1723 } else { /* not an addl inctruction */
1724 count = 0;
1727 return (count);
1730 uintptr_t
1731 get_return_address32(uintptr_t *psp)
1733 uint32_t sp = *psp;
1734 uint32_t rpc;
1735 int count;
1737 *psp += 4; /* account for popping the stack on return */
1738 if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc))
1739 return (0);
1740 if ((count = return_count32(&rpc)) < 0)
1741 count = 0;
1742 *psp += count; /* expected sp on return */
1743 return (rpc);
1746 uintptr_t
1747 get_return_address(uintptr_t *psp)
1749 #ifdef _LP64
1750 uintptr_t rpc;
1751 uintptr_t sp = *psp;
1753 if (data_model == PR_MODEL_LP64) {
1754 if (Pread(Proc, &rpc, sizeof (rpc), sp) != sizeof (rpc))
1755 return (0);
1757 * Ignore arguments pushed on the stack. See comments in
1758 * get_arguments().
1760 return (rpc);
1761 } else
1762 #endif
1763 return (get_return_address32(psp));
1768 get_arguments32(long *argp)
1770 private_t *pri = get_private();
1771 const lwpstatus_t *Lsp = pri->lwpstat;
1772 uint32_t frame[5]; /* return pc + 4 args */
1773 int narg;
1774 int count;
1775 int i;
1777 narg = Pread(Proc, frame, sizeof (frame),
1778 (uintptr_t)Lsp->pr_reg[R_SP]);
1779 narg -= sizeof (greg32_t);
1780 if (narg <= 0)
1781 return (0);
1782 narg /= sizeof (greg32_t); /* no more than 4 */
1785 * Given the return PC, determine the number of arguments.
1787 if ((count = return_count32(&frame[0])) < 0)
1788 narg = 0;
1789 else {
1790 count /= sizeof (greg32_t);
1791 if (narg > count)
1792 narg = count;
1795 for (i = 0; i < narg; i++)
1796 argp[i] = (long)frame[i+1];
1798 return (narg);
1802 get_arguments(long *argp)
1804 #ifdef _LP64
1805 private_t *pri = get_private();
1806 const lwpstatus_t *Lsp = pri->lwpstat;
1808 if (data_model == PR_MODEL_LP64) {
1810 * On amd64, we do not know how many arguments are passed to
1811 * each function. While it may be possible to detect if we
1812 * have more than 6 arguments, it is of marginal value.
1813 * Instead, assume that we always have 6 arguments, which are
1814 * passed via registers.
1816 argp[0] = Lsp->pr_reg[REG_RDI];
1817 argp[1] = Lsp->pr_reg[REG_RSI];
1818 argp[2] = Lsp->pr_reg[REG_RDX];
1819 argp[3] = Lsp->pr_reg[REG_RCX];
1820 argp[4] = Lsp->pr_reg[REG_R8];
1821 argp[5] = Lsp->pr_reg[REG_R9];
1822 return (6);
1823 } else
1824 #endif
1825 return (get_arguments32(argp));
1828 #endif /* __amd64 || __i386 */