Add in missing files.
[m6w6/libmemcached] / libmemcached / backtrace.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libmcachedd client library.
4 *
5 * Copyright (C) 2011 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 <libmemcached/common.h>
39
40 #include <cstring>
41 #include <cstdlib>
42
43 #ifdef HAVE_EXECINFO_H
44 #include <execinfo.h>
45 #endif
46
47 #ifdef HAVE_CXXABI_H
48 #include <cxxabi.h>
49 #endif
50
51 #ifdef HAVE_GCC_ABI_DEMANGLE
52 #define USE_DEMANGLE 1
53 #else
54 #define USE_DEMANGLE 0
55 #endif
56
57 void custom_backtrace(void)
58 {
59 #ifdef HAVE_EXECINFO_H
60 void *array[50];
61
62 size_t size= backtrace(array, 50);
63 char **strings= backtrace_symbols(array, size);
64
65 if (strings == NULL)
66 {
67 return;
68 }
69
70 fprintf(stderr, "Number of stack frames obtained: %lu\n", (unsigned long)size);
71
72 char *named_function= (char *)std::realloc(NULL, 1024);
73
74 if (named_function == NULL)
75 {
76 std::free(strings);
77 return;
78 }
79
80 for (size_t x= 1; x < size; x++)
81 {
82 if (USE_DEMANGLE)
83 {
84 size_t sz= 200;
85 char *named_function_ptr= (char *)std::realloc(named_function, sz);
86 if (named_function_ptr == NULL)
87 {
88 continue;
89 }
90 named_function= named_function_ptr;
91
92 char *begin_name= 0;
93 char *begin_offset= 0;
94 char *end_offset= 0;
95
96 for (char *j= strings[x]; *j; ++j)
97 {
98 if (*j == '(')
99 {
100 begin_name= j;
101 }
102 else if (*j == '+')
103 {
104 begin_offset= j;
105 }
106 else if (*j == ')' and begin_offset)
107 {
108 end_offset= j;
109 break;
110 }
111 }
112
113 if (begin_name and begin_offset and end_offset and begin_name < begin_offset)
114 {
115 *begin_name++= '\0';
116 *begin_offset++= '\0';
117 *end_offset= '\0';
118
119 int status;
120 char *ret= abi::__cxa_demangle(begin_name, named_function, &sz, &status);
121 if (ret) // realloc()'ed string
122 {
123 named_function= ret;
124 fprintf(stderr, " %s : %s()+%s\n", strings[x], begin_name, begin_offset);
125 }
126 else
127 {
128 fprintf(stderr, " %s : %s()+%s\n", strings[x], begin_name, begin_offset);
129 }
130 }
131 else
132 {
133 fprintf(stderr, " %s\n", strings[x]);
134 }
135 }
136 else
137 {
138 fprintf(stderr, " unmangled: %s\n", strings[x]);
139 }
140 }
141
142 std::free(named_function);
143 std::free(strings);
144 #endif // HAVE_EXECINFO_H
145 }