8017f7a2f0437404ca37b0d39d94aca8fa2f9caa
[awesomized/libmemcached] / src / libmemcached / backtrace.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached client library.
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include "mem_config.h"
39
40 #include "libmemcached/backtrace.hpp"
41
42 #include <cstdio>
43 #include <cstdlib>
44 #include <cstring>
45
46 #if HAVE_BACKTRACE
47
48 #include BACKTRACE_HEADER
49
50 #if HAVE_ABI____CXA_DEMANGLE
51 # include <cxxabi.h>
52 # define USE_DEMANGLE 1
53 #else
54 # define USE_DEMANGLE 0
55 #endif
56
57 #ifdef HAVE_DLFCN_H
58 # include <dlfcn.h>
59 #endif
60
61 const int MAX_DEPTH= 50;
62
63 void custom_backtrace(void)
64 {
65 void *backtrace_buffer[MAX_DEPTH +1];
66
67 int stack_frames= backtrace(backtrace_buffer, MAX_DEPTH);
68 if (stack_frames)
69 {
70 char **symbollist= backtrace_symbols(backtrace_buffer, stack_frames);
71 if (symbollist)
72 {
73 for (int x= 0; x < stack_frames; x++)
74 {
75 bool was_demangled= false;
76
77 if (USE_DEMANGLE)
78 {
79 #ifdef HAVE_DLFCN_H
80 Dl_info dlinfo;
81 if (dladdr(backtrace_buffer[x], &dlinfo))
82 {
83 char demangled_buffer[1024];
84 const char *called_in= "<unresolved>";
85 if (dlinfo.dli_sname)
86 {
87 size_t demangled_size= sizeof(demangled_buffer);
88 int status;
89 char* demangled;
90 if ((demangled= abi::__cxa_demangle(dlinfo.dli_sname, demangled_buffer, &demangled_size, &status)))
91 {
92 called_in= demangled;
93 fprintf(stderr, "---> demangled: %s -> %s\n", demangled_buffer, demangled);
94 }
95 else
96 {
97 called_in= dlinfo.dli_sname;
98 }
99
100 was_demangled= true;
101 fprintf(stderr, "#%d %p in %s at %s\n",
102 x, backtrace_buffer[x],
103 called_in,
104 dlinfo.dli_fname);
105 }
106 }
107 #endif
108 }
109
110 if (was_demangled == false)
111 {
112 fprintf(stderr, "?%d %p in %s\n", x, backtrace_buffer[x], symbollist[x]);
113 }
114 }
115
116 ::free(symbollist);
117 }
118 }
119 }
120
121 #else // HAVE_BACKTRACE
122
123 void custom_backtrace(void)
124 {
125 fprintf(stderr, "Backtrace null function called\n");
126 }
127 #endif // HAVE_BACKTRACE