|
|
|||
File indexing completed on 2026-04-09 07:49:24
0001 /* 0002 * Copyright (c) 2019 Opticks Team. All Rights Reserved. 0003 * 0004 * This file is part of Opticks 0005 * (see https://bitbucket.org/simoncblyth/opticks). 0006 * 0007 * Licensed under the Apache License, Version 2.0 (the "License"); 0008 * you may not use this file except in compliance with the License. 0009 * You may obtain a copy of the License at 0010 * 0011 * http://www.apache.org/licenses/LICENSE-2.0 0012 * 0013 * Unless required by applicable law or agreed to in writing, software 0014 * distributed under the License is distributed on an "AS IS" BASIS, 0015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 * See the License for the specific language governing permissions and 0017 * limitations under the License. 0018 */ 0019 0020 // https://eli.thegreenplace.net/2015/programmatic-access-to-the-call-stack-in-c/ 0021 0022 #include "OPTICKS_LOG.hh" 0023 0024 #define UNW_LOCAL_ONLY 0025 #include <libunwind.h> 0026 #include <stdio.h> 0027 0028 // Call this function to get a backtrace. 0029 void backtrace() { 0030 unw_cursor_t cursor; 0031 unw_context_t context; 0032 0033 // Initialize cursor to current frame for local unwinding. 0034 unw_getcontext(&context); 0035 unw_init_local(&cursor, &context); 0036 0037 // Unwind frames one by one, going up the frame stack. 0038 while (unw_step(&cursor) > 0) { 0039 unw_word_t offset, pc; 0040 unw_get_reg(&cursor, UNW_REG_IP, &pc); 0041 if (pc == 0) { 0042 break; 0043 } 0044 printf("0x%llx:", pc); 0045 0046 char sym[256]; 0047 if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) { 0048 printf(" (%s+0x%llx)\n", sym, offset); 0049 } else { 0050 printf(" -- error: unable to obtain symbol name for this frame\n"); 0051 } 0052 } 0053 } 0054 0055 void foo() { 0056 backtrace(); // <-------- backtrace here! 0057 } 0058 0059 void bar() { 0060 foo(); 0061 } 0062 0063 int main(int argc, char **argv) 0064 { 0065 OPTICKS_LOG(argc, argv ); 0066 0067 bar(); 0068 0069 return 0; 0070 }
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|