1*647f063dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*647f063dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*647f063dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*647f063dSAndrew Rist * distributed with this work for additional information 6*647f063dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*647f063dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*647f063dSAndrew Rist * "License"); you may not use this file except in compliance 9*647f063dSAndrew Rist * with the License. You may obtain a copy of the License at 10*647f063dSAndrew Rist * 11*647f063dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*647f063dSAndrew Rist * 13*647f063dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*647f063dSAndrew Rist * software distributed under the License is distributed on an 15*647f063dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*647f063dSAndrew Rist * KIND, either express or implied. See the License for the 17*647f063dSAndrew Rist * specific language governing permissions and limitations 18*647f063dSAndrew Rist * under the License. 19*647f063dSAndrew Rist * 20*647f063dSAndrew Rist *************************************************************/ 21*647f063dSAndrew Rist 22*647f063dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir 25cdf0e10cSrcweir #ifdef SOLARIS 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <dlfcn.h> 28cdf0e10cSrcweir #include <pthread.h> 29cdf0e10cSrcweir #include <setjmp.h> 30cdf0e10cSrcweir #include <stdio.h> 31cdf0e10cSrcweir #include <sys/frame.h> 32cdf0e10cSrcweir #include "backtrace.h" 33cdf0e10cSrcweir 34cdf0e10cSrcweir #if defined(SPARC) 35cdf0e10cSrcweir 36cdf0e10cSrcweir #if defined IS_LP64 37cdf0e10cSrcweir 38cdf0e10cSrcweir #define FRAME_PTR_OFFSET 1 39cdf0e10cSrcweir #define FRAME_OFFSET 0 40cdf0e10cSrcweir #define STACK_BIAS 0x7ff 41cdf0e10cSrcweir 42cdf0e10cSrcweir #else 43cdf0e10cSrcweir 44cdf0e10cSrcweir #define FRAME_PTR_OFFSET 1 45cdf0e10cSrcweir #define FRAME_OFFSET 0 46cdf0e10cSrcweir #define STACK_BIAS 0 47cdf0e10cSrcweir 48cdf0e10cSrcweir #endif 49cdf0e10cSrcweir 50cdf0e10cSrcweir #elif defined( INTEL ) 51cdf0e10cSrcweir 52cdf0e10cSrcweir #define FRAME_PTR_OFFSET 3 53cdf0e10cSrcweir #define FRAME_OFFSET 0 54cdf0e10cSrcweir #define STACK_BIAS 0 55cdf0e10cSrcweir 56cdf0e10cSrcweir #else 57cdf0e10cSrcweir 58cdf0e10cSrcweir #error Unknown Solaris target platform. 59cdf0e10cSrcweir 60cdf0e10cSrcweir #endif /* defined SPARC or INTEL */ 61cdf0e10cSrcweir 62cdf0e10cSrcweir 63cdf0e10cSrcweir int backtrace( void **buffer, int max_frames ) 64cdf0e10cSrcweir { 65cdf0e10cSrcweir jmp_buf ctx; 66cdf0e10cSrcweir long fpval; 67cdf0e10cSrcweir struct frame *fp; 68cdf0e10cSrcweir int i; 69cdf0e10cSrcweir 70cdf0e10cSrcweir /* flush register windows */ 71cdf0e10cSrcweir #ifdef SPARC 72cdf0e10cSrcweir asm("ta 3"); 73cdf0e10cSrcweir #endif 74cdf0e10cSrcweir 75cdf0e10cSrcweir /* get stack- and framepointer */ 76cdf0e10cSrcweir setjmp(ctx); 77cdf0e10cSrcweir 78cdf0e10cSrcweir fpval = ((long*)(ctx))[FRAME_PTR_OFFSET]; 79cdf0e10cSrcweir fp = (struct frame*)((char*)(fpval) + STACK_BIAS); 80cdf0e10cSrcweir 81cdf0e10cSrcweir for (i = 0; (i < FRAME_OFFSET) && (fp != 0); i++) 82cdf0e10cSrcweir fp = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS); 83cdf0e10cSrcweir 84cdf0e10cSrcweir /* iterate through backtrace */ 85cdf0e10cSrcweir for (i = 0; (fp != 0) && (fp->fr_savpc != 0) && (i < max_frames); i++) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir /* saved (prev) frame */ 88cdf0e10cSrcweir struct frame * prev = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS); 89cdf0e10cSrcweir 90cdf0e10cSrcweir /* store frame */ 91cdf0e10cSrcweir *(buffer++) = (void*)(fp->fr_savpc); 92cdf0e10cSrcweir 93cdf0e10cSrcweir /* prev frame (w/ stack growing top down) */ 94cdf0e10cSrcweir fp = (prev > fp) ? prev : 0; 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir /* return number of frames stored */ 98cdf0e10cSrcweir return i; 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir void backtrace_symbols_fd( void **buffer, int size, int fd ) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir FILE *fp = fdopen( fd, "w" ); 104cdf0e10cSrcweir 105cdf0e10cSrcweir if ( fp ) 106cdf0e10cSrcweir { 107cdf0e10cSrcweir void **pFramePtr; 108cdf0e10cSrcweir 109cdf0e10cSrcweir for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- ) 110cdf0e10cSrcweir { 111cdf0e10cSrcweir Dl_info dli; 112cdf0e10cSrcweir ptrdiff_t offset; 113cdf0e10cSrcweir 114cdf0e10cSrcweir if ( 0 != dladdr( *pFramePtr, &dli ) ) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir if ( dli.dli_fname && dli.dli_fbase ) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase; 119cdf0e10cSrcweir fprintf( fp, "%s+0x%x", dli.dli_fname, offset ); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir if ( dli.dli_sname && dli.dli_saddr ) 122cdf0e10cSrcweir { 123cdf0e10cSrcweir offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr; 124cdf0e10cSrcweir fprintf( fp, "(%s+0x%x)", dli.dli_sname, offset ); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir } 127cdf0e10cSrcweir fprintf( fp, "[0x%x]\n", *pFramePtr ); 128cdf0e10cSrcweir } 129cdf0e10cSrcweir 130cdf0e10cSrcweir fflush( fp ); 131cdf0e10cSrcweir fclose( fp ); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir #endif /* defined SOLARIS */ 136cdf0e10cSrcweir 137cdf0e10cSrcweir 138cdf0e10cSrcweir #if defined FREEBSD || defined NETBSD 139cdf0e10cSrcweir #include <dlfcn.h> 140cdf0e10cSrcweir #include <pthread.h> 141cdf0e10cSrcweir #include <setjmp.h> 142cdf0e10cSrcweir #include <stddef.h> 143cdf0e10cSrcweir #include <stdio.h> 144cdf0e10cSrcweir #include "backtrace.h" 145cdf0e10cSrcweir 146cdf0e10cSrcweir #define FRAME_PTR_OFFSET 1 147cdf0e10cSrcweir #define FRAME_OFFSET 0 148cdf0e10cSrcweir 149cdf0e10cSrcweir int backtrace( void **buffer, int max_frames ) 150cdf0e10cSrcweir { 151cdf0e10cSrcweir struct frame *fp; 152cdf0e10cSrcweir jmp_buf ctx; 153cdf0e10cSrcweir int i; 154cdf0e10cSrcweir /* get stack- and framepointer */ 155cdf0e10cSrcweir setjmp(ctx); 156cdf0e10cSrcweir fp = (struct frame*)(((size_t*)(ctx))[FRAME_PTR_OFFSET]); 157cdf0e10cSrcweir for ( i=0; (i<FRAME_OFFSET) && (fp!=0); i++) 158cdf0e10cSrcweir fp = fp->fr_savfp; 159cdf0e10cSrcweir /* iterate through backtrace */ 160cdf0e10cSrcweir for (i=0; fp && fp->fr_savpc && i<max_frames; i++) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir /* store frame */ 163cdf0e10cSrcweir *(buffer++) = (void *)fp->fr_savpc; 164cdf0e10cSrcweir /* next frame */ 165cdf0e10cSrcweir fp=fp->fr_savfp; 166cdf0e10cSrcweir } 167cdf0e10cSrcweir return i; 168cdf0e10cSrcweir } 169cdf0e10cSrcweir 170cdf0e10cSrcweir void backtrace_symbols_fd( void **buffer, int size, int fd ) 171cdf0e10cSrcweir { 172cdf0e10cSrcweir FILE *fp = fdopen( fd, "w" ); 173cdf0e10cSrcweir 174cdf0e10cSrcweir if ( fp ) 175cdf0e10cSrcweir { 176cdf0e10cSrcweir void **pFramePtr; 177cdf0e10cSrcweir for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- ) 178cdf0e10cSrcweir { 179cdf0e10cSrcweir Dl_info dli; 180cdf0e10cSrcweir ptrdiff_t offset; 181cdf0e10cSrcweir 182cdf0e10cSrcweir if ( 0 != dladdr( *pFramePtr, &dli ) ) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir if ( dli.dli_fname && dli.dli_fbase ) 185cdf0e10cSrcweir { 186cdf0e10cSrcweir offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase; 187cdf0e10cSrcweir fprintf( fp, "%s+0x%x", dli.dli_fname, offset ); 188cdf0e10cSrcweir } 189cdf0e10cSrcweir if ( dli.dli_sname && dli.dli_saddr ) 190cdf0e10cSrcweir { 191cdf0e10cSrcweir offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr; 192cdf0e10cSrcweir fprintf( fp, "(%s+0x%x)", dli.dli_sname, offset ); 193cdf0e10cSrcweir } 194cdf0e10cSrcweir } 195cdf0e10cSrcweir fprintf( fp, "[0x%x]\n", *pFramePtr ); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir fflush( fp ); 198cdf0e10cSrcweir fclose( fp ); 199cdf0e10cSrcweir } 200cdf0e10cSrcweir } 201cdf0e10cSrcweir #endif /* defined FREEBSD */ 202cdf0e10cSrcweir 203cdf0e10cSrcweir #ifdef LINUX 204cdf0e10cSrcweir 205cdf0e10cSrcweir #ifndef _GNU_SOURCE 206cdf0e10cSrcweir #define _GNU_SOURCE 207cdf0e10cSrcweir #endif 208cdf0e10cSrcweir 209cdf0e10cSrcweir #include <dlfcn.h> 210cdf0e10cSrcweir #include <pthread.h> 211cdf0e10cSrcweir #include <setjmp.h> 212cdf0e10cSrcweir #include <stdio.h> 213cdf0e10cSrcweir #include "backtrace.h" 214cdf0e10cSrcweir 215cdf0e10cSrcweir #if defined(SPARC) 216cdf0e10cSrcweir 217cdf0e10cSrcweir #define FRAME_PTR_OFFSET 1 218cdf0e10cSrcweir #define FRAME_OFFSET 0 219cdf0e10cSrcweir 220cdf0e10cSrcweir #else 221cdf0e10cSrcweir 222cdf0e10cSrcweir #error Unknown Linux target platform. 223cdf0e10cSrcweir 224cdf0e10cSrcweir #endif /* defined SPARC or INTEL */ 225cdf0e10cSrcweir 226cdf0e10cSrcweir typedef int ptrdiff_t; 227cdf0e10cSrcweir 228cdf0e10cSrcweir int backtrace( void **buffer, int max_frames ) 229cdf0e10cSrcweir { 230cdf0e10cSrcweir struct frame *fp; 231cdf0e10cSrcweir jmp_buf ctx; 232cdf0e10cSrcweir int i; 233cdf0e10cSrcweir 234cdf0e10cSrcweir /* flush register windows */ 235cdf0e10cSrcweir #ifdef SPARC 236cdf0e10cSrcweir asm("ta 3"); 237cdf0e10cSrcweir #endif 238cdf0e10cSrcweir /* get stack- and framepointer */ 239cdf0e10cSrcweir setjmp(ctx); 240cdf0e10cSrcweir fp = (struct frame*)(((size_t*)(ctx))[FRAME_PTR_OFFSET]); 241cdf0e10cSrcweir for ( i=0; (i<FRAME_OFFSET) && (fp!=0); i++) 242cdf0e10cSrcweir fp = fp->fr_savfp; 243cdf0e10cSrcweir 244cdf0e10cSrcweir /* iterate through backtrace */ 245cdf0e10cSrcweir for (i=0; fp && fp->fr_savpc && i<max_frames; i++) 246cdf0e10cSrcweir { 247cdf0e10cSrcweir /* store frame */ 248cdf0e10cSrcweir *(buffer++) = (void *)fp->fr_savpc; 249cdf0e10cSrcweir /* next frame */ 250cdf0e10cSrcweir fp=fp->fr_savfp; 251cdf0e10cSrcweir } 252cdf0e10cSrcweir return i; 253cdf0e10cSrcweir } 254cdf0e10cSrcweir 255cdf0e10cSrcweir void backtrace_symbols_fd( void **buffer, int size, int fd ) 256cdf0e10cSrcweir { 257cdf0e10cSrcweir FILE *fp = fdopen( fd, "w" ); 258cdf0e10cSrcweir 259cdf0e10cSrcweir if ( fp ) 260cdf0e10cSrcweir { 261cdf0e10cSrcweir void **pFramePtr; 262cdf0e10cSrcweir 263cdf0e10cSrcweir for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- ) 264cdf0e10cSrcweir { 265cdf0e10cSrcweir Dl_info dli; 266cdf0e10cSrcweir ptrdiff_t offset; 267cdf0e10cSrcweir 268cdf0e10cSrcweir if ( 0 != dladdr( *pFramePtr, &dli ) ) 269cdf0e10cSrcweir { 270cdf0e10cSrcweir if ( dli.dli_fname && dli.dli_fbase ) 271cdf0e10cSrcweir { 272cdf0e10cSrcweir offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase; 273cdf0e10cSrcweir fprintf( fp, "%s+0x%x", dli.dli_fname, offset ); 274cdf0e10cSrcweir } 275cdf0e10cSrcweir if ( dli.dli_sname && dli.dli_saddr ) 276cdf0e10cSrcweir { 277cdf0e10cSrcweir offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr; 278cdf0e10cSrcweir fprintf( fp, "(%s+0x%x)", dli.dli_sname, offset ); 279cdf0e10cSrcweir } 280cdf0e10cSrcweir } 281cdf0e10cSrcweir fprintf( fp, "[0x%x]\n", *pFramePtr ); 282cdf0e10cSrcweir } 283cdf0e10cSrcweir 284cdf0e10cSrcweir fflush( fp ); 285cdf0e10cSrcweir fclose( fp ); 286cdf0e10cSrcweir } 287cdf0e10cSrcweir } 288cdf0e10cSrcweir 289cdf0e10cSrcweir #endif /* defined LINUX */ 290cdf0e10cSrcweir 291cdf0e10cSrcweir #if defined( MACOSX ) 292cdf0e10cSrcweir 293cdf0e10cSrcweir #include <dlfcn.h> 294cdf0e10cSrcweir #include <stdio.h> 295cdf0e10cSrcweir #include "backtrace.h" 296cdf0e10cSrcweir 297cdf0e10cSrcweir typedef unsigned ptrdiff_t; 298cdf0e10cSrcweir 299cdf0e10cSrcweir /* glib backtrace is only available on MacOsX 10.5 or higher 300cdf0e10cSrcweir so we do it on our own */ 301cdf0e10cSrcweir 302cdf0e10cSrcweir int backtrace( void **buffer, int max_frames ) 303cdf0e10cSrcweir { 304cdf0e10cSrcweir void **frame = (void **)__builtin_frame_address(0); 305cdf0e10cSrcweir void **bp = ( void **)(*frame); 306cdf0e10cSrcweir void *ip = frame[1]; 307cdf0e10cSrcweir int i; 308cdf0e10cSrcweir 309cdf0e10cSrcweir for ( i = 0; bp && ip && i < max_frames; i++ ) 310cdf0e10cSrcweir { 311cdf0e10cSrcweir *(buffer++) = ip; 312cdf0e10cSrcweir 313cdf0e10cSrcweir ip = bp[1]; 314cdf0e10cSrcweir bp = (void**)(bp[0]); 315cdf0e10cSrcweir } 316cdf0e10cSrcweir 317cdf0e10cSrcweir return i; 318cdf0e10cSrcweir } 319cdf0e10cSrcweir 320cdf0e10cSrcweir 321cdf0e10cSrcweir void backtrace_symbols_fd( void **buffer, int size, int fd ) 322cdf0e10cSrcweir { 323cdf0e10cSrcweir FILE *fp = fdopen( fd, "w" ); 324cdf0e10cSrcweir 325cdf0e10cSrcweir if ( fp ) 326cdf0e10cSrcweir { 327cdf0e10cSrcweir void **pFramePtr; 328cdf0e10cSrcweir 329cdf0e10cSrcweir for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- ) 330cdf0e10cSrcweir { 331cdf0e10cSrcweir Dl_info dli; 332cdf0e10cSrcweir ptrdiff_t offset; 333cdf0e10cSrcweir 334cdf0e10cSrcweir if ( 0 != dladdr( *pFramePtr, &dli ) ) 335cdf0e10cSrcweir { 336cdf0e10cSrcweir if ( dli.dli_fname && dli.dli_fbase ) 337cdf0e10cSrcweir { 338cdf0e10cSrcweir offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase; 339cdf0e10cSrcweir fprintf( fp, "%s+0x%x", dli.dli_fname, offset ); 340cdf0e10cSrcweir } 341cdf0e10cSrcweir if ( dli.dli_sname && dli.dli_saddr ) 342cdf0e10cSrcweir { 343cdf0e10cSrcweir offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr; 344cdf0e10cSrcweir fprintf( fp, "(%s+0x%x)", dli.dli_sname, offset ); 345cdf0e10cSrcweir } 346cdf0e10cSrcweir } 347cdf0e10cSrcweir fprintf( fp, "[0x%x]\n", (unsigned int)*pFramePtr ); 348cdf0e10cSrcweir } 349cdf0e10cSrcweir 350cdf0e10cSrcweir fflush( fp ); 351cdf0e10cSrcweir fclose( fp ); 352cdf0e10cSrcweir } 353cdf0e10cSrcweir } 354cdf0e10cSrcweir 355cdf0e10cSrcweir #endif /* defined MACOSX */ 356