• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

ink_stack_trace.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #include "libts.h"
00025 #include "ink_stack_trace.h"
00026 
00027 #include <strings.h>
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <unistd.h>
00031 
00032 #ifndef STDERR_FILENO
00033 #define STDERR_FILENO 2
00034 #endif
00035 
00036 #if TS_HAS_BACKTRACE
00037 
00038 #include <execinfo.h>           /* for backtrace_symbols, etc. */
00039 #include <signal.h>
00040 
00041 void
00042 ink_stack_trace_dump()
00043 {
00044   int btl;
00045 
00046   // Recopy and re-terminate the app name in case it has been trashed.
00047   const char *msg = " - STACK TRACE: \n";
00048   if (write(STDERR_FILENO, program_name, strlen(program_name)) == -1)
00049     return;
00050   if (write(STDERR_FILENO, msg, strlen(msg)) == -1)
00051     return;
00052 
00053   // In certain situations you can get stuck in malloc waiting for a lock
00054   // that your program held when it segfaulted. We set an alarm so that
00055   // if this situation happens it will allow traffic_server to exit.
00056   alarm(10);
00057 
00058   void *stack[INK_STACK_TRACE_MAX_LEVELS + 1];
00059   memset(stack, 0, sizeof(stack));
00060   if ((btl = backtrace(stack, INK_STACK_TRACE_MAX_LEVELS)) > 2) {
00061     // dump the backtrace to stderr
00062     backtrace_symbols_fd(stack + 2, btl - 2, STDERR_FILENO);
00063   }
00064 }
00065 
00066 #else  /* !TS_HAS_BACKTRACE */
00067 
00068 void
00069 ink_stack_trace_dump()
00070 {
00071   const char msg[] = "ink_stack_trace_dump not implemented on this operating system\n";
00072   if (write(STDERR_FILENO, msg, sizeof(msg) - 1) == -1)
00073       return;
00074 }
00075 
00076 #endif  /* TS_HAS_BACKTRACE */

Generated by  doxygen 1.7.1