Slightly more informative logging
[LibreOffice.git] / sal / osl / unx / backtrace_bsd.c
blobf20b739cc25c5c6aef7d551358a9432d1def10e4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 // This file is #include from backtrace.c
22 #include <dlfcn.h>
23 #include <pthread.h>
24 #include <setjmp.h>
25 #include <stddef.h>
26 #include <stdio.h>
28 /* no frame.h on FreeBSD */
29 struct frame {
30 struct frame *fr_savfp;
31 long fr_savpc;
34 #if defined(POWERPC) || defined(POWERPC64)
36 #define FRAME_PTR_OFFSET 1
37 #define FRAME_OFFSET 0
39 #else
41 #define FRAME_PTR_OFFSET 3
42 #define FRAME_OFFSET 0
44 #endif
46 int backtrace( void **buffer, int max_frames )
48 struct frame *fp;
49 jmp_buf ctx;
50 int i;
51 /* get stack- and framepointer */
52 setjmp(ctx);
53 fp = (struct frame*)(((size_t*)(ctx))[FRAME_PTR_OFFSET]);
54 for ( i=0; (i<FRAME_OFFSET) && (fp!=0); i++)
55 fp = fp->fr_savfp;
56 /* iterate through backtrace */
57 for (i=0; fp && fp->fr_savpc && i<max_frames; i++)
59 /* store frame */
60 *(buffer++) = (void *)fp->fr_savpc;
61 /* next frame */
62 fp=fp->fr_savfp;
64 return i;
67 char ** backtrace_symbols(void * const * buffer, int size)
69 (void)buffer; (void)size;
70 return NULL; /*TODO*/
73 void backtrace_symbols_fd( void **buffer, int size, int fd )
75 FILE *fp = fdopen( fd, "w" );
77 if ( fp )
79 void **pFramePtr;
80 for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- )
82 Dl_info dli;
83 ptrdiff_t offset;
85 if ( 0 != dladdr( *pFramePtr, &dli ) )
87 if ( dli.dli_fname && dli.dli_fbase )
89 offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase;
90 fprintf( fp, "%s+0x%" SAL_PRI_PTRDIFFT "x", dli.dli_fname, offset );
92 if ( dli.dli_sname && dli.dli_saddr )
94 offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr;
95 fprintf( fp, "(%s+0x%" SAL_PRI_PTRDIFFT "x)", dli.dli_sname, offset );
98 fprintf( fp, "[%p]\n", *pFramePtr );
100 fclose( fp );
105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */