Avoid potential negative array index access to cached text.
[LibreOffice.git] / sal / osl / unx / backtrace_solaris.c
blob76f4475bfb2ff224b68747f4fc13f0b19adc8471
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 <stdio.h>
26 #include <sys/frame.h>
28 #if defined(SPARC)
30 #if defined IS_LP64
32 #define FRAME_PTR_OFFSET 1
33 #define FRAME_OFFSET 0
34 #define STACK_BIAS 0x7ff
36 #else
38 #define FRAME_PTR_OFFSET 1
39 #define FRAME_OFFSET 0
40 #define STACK_BIAS 0
42 #endif
44 #elif defined( INTEL )
46 #define FRAME_PTR_OFFSET 3
47 #define FRAME_OFFSET 0
48 #define STACK_BIAS 0
50 #else
52 #error Unknown Solaris target platform.
54 #endif /* defined SPARC or INTEL */
56 int backtrace( void **buffer, int max_frames )
58 jmp_buf ctx;
59 long fpval;
60 struct frame *fp;
61 int i;
63 /* flush register windows */
64 #ifdef SPARC
65 asm("ta 3");
66 #endif
68 /* get stack- and framepointer */
69 setjmp(ctx);
71 fpval = ((long*)(ctx))[FRAME_PTR_OFFSET];
72 fp = (struct frame*)((char*)(fpval) + STACK_BIAS);
74 for (i = 0; (i < FRAME_OFFSET) && (fp != 0); i++)
75 fp = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS);
77 /* iterate through backtrace */
78 for (i = 0; (fp != 0) && (fp->fr_savpc != 0) && (i < max_frames); i++)
80 /* saved (prev) frame */
81 struct frame * prev = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS);
83 /* store frame */
84 *(buffer++) = (void*)(fp->fr_savpc);
86 /* prev frame (w/ stack growing top down) */
87 fp = (prev > fp) ? prev : 0;
90 /* return number of frames stored */
91 return i;
94 char ** backtrace_symbols(void * const * buffer, int size)
96 (void)buffer; (void)size;
97 return NULL; /*TODO*/
100 void backtrace_symbols_fd( void **buffer, int size, int fd )
102 FILE *fp = fdopen( fd, "w" );
104 if ( fp )
106 void **pFramePtr;
108 for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- )
110 Dl_info dli;
111 ptrdiff_t offset;
113 if ( 0 != dladdr( *pFramePtr, &dli ) )
115 if ( dli.dli_fname && dli.dli_fbase )
117 offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase;
118 fprintf( fp, "%s+0x%" SAL_PRI_PTRDIFFT "x", dli.dli_fname, offset );
120 if ( dli.dli_sname && dli.dli_saddr )
122 offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr;
123 fprintf( fp, "(%s+0x%" SAL_PRI_PTRDIFFT "x)", dli.dli_sname, offset );
126 fprintf( fp, "[%p]\n", *pFramePtr );
129 fclose( fp );
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */