Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help: How to get a stack trace on solaris

299 views
Skip to first unread message

Dean Enix

unread,
Nov 16, 1995, 3:00:00 AM11/16/95
to
How could one obtain a stack trace under program control on solaris?
Any pointers to source code or documentation would be appreciated!

BTW, I am aware that a debugger can display the stack. However, I want
to be able to control display of the stack in the program...

Thx in advance,

Dean
--

Dean Enix
dea...@auto-trol.com
Auto-Trol Technology Corporation

Davin Milun

unread,
Nov 17, 1995, 3:00:00 AM11/17/95
to
Dean Enix <dea...@auto-trol.com> wrote:
»How could one obtain a stack trace under program control on solaris?

»Any pointers to source code or documentation would be appreciated!
»
»BTW, I am aware that a debugger can display the stack. However, I want
»to be able to control display of the stack in the program...

Here is an article that was posted recently:

|From: ba...@Eng.Sun.COM (Bart Smaalders)
|Newsgroups: comp.unix.solaris
|Subject: Re: In C, how to print stack trace like db
|Date: 24 Oct 1995 20:53:08 GMT
|
|Kit Cragin <k...@protosoft.com> wrote:
|>I would like to be able to print a function call stack trace
|>in C code similar to the way dbx does when you do a 'where'
|>on a core file.
|
|This works on Solaris 2.4 and higher (sparc, x86 and PPC):
|
|/*
| walks up call stack, printing library:routine+offset for each routine
| */
|
|#include <dlfcn.h>
|#include <setjmp.h>
|#include<sys/types.h>
|#include <sys/reg.h>
|#include <sys/frame.h>
|
|#if defined(sparc) || defined(__sparc)
|#define FLUSHWIN() asm("ta 3");
|#define FRAME_PTR_INDEX 1
|#define SKIP_FRAMES 0
|#endif
|
|#if defined(i386) || defined(__i386)
|#define FLUSHWIN()
|#define FRAME_PTR_INDEX 3
|#define SKIP_FRAMES 1
|#endif
|
|#if defined(ppc) || defined(__ppc)
|#define FLUSHWIN()
|#define FRAME_PTR_INDEX 0
|#define SKIP_FRAMES 2
|#endif
|
|/*
| this function walks up call stack, calling user-supplied
| function once for each stack frame, passing the pc and the user-supplied
| usrarg as the argument.
| */
|
|int cs_operate(int (*func)(void *, void *), void * usrarg)
|{
| struct frame *sp;
| jmp_buf env;
| int i;
| int * iptr;
|
| FLUSHWIN();
|
| setjmp(env);
| iptr = (int*) env;
|
| sp = (struct frame *) iptr[FRAME_PTR_INDEX];
|
| for(i=0;i<SKIP_FRAMES && sp;i++)
| sp = sp->fr_savfp;
|
| i = 0;
|
| while(sp && sp->fr_savpc && ++i && (*func)((void*)sp->fr_savpc, usrarg)) {
| sp = sp->fr_savfp;
| }
|
| return(i);
|}
|
|print_my_stack()
|{
| int print_address(void *, void *);
| cs_operate(print_address, NULL);
|}
|
|int print_address(void *pc, void * usrarg)
|{
| Dl_info info;
| char * func;
| char * lib;
|
| if(dladdr(pc, & info) == 0) {
| func = "??";
| lib = "??";
| }
| else {
| lib = (char *) info.dli_fname;
| func = (char *) info.dli_sname;
| }
|
| printf("%s:%s+0x%x\n",
| lib,
| func,
| (unsigned int)pc - (unsigned int)info.dli_saddr);
|
| return(1);
|}
|
|---
|Bart Smaalders OS Performance SunSoft
|ba...@cyber.eng.sun.com (415) 336-6322 MS MTV05-44
|http://playground.sun.com/~barts 2550 Garcia Ave

0 new messages