drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
backtrace.cc
1/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Brian Aker
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <config.h>
22#include <drizzled/util/backtrace.h>
23
24#include <cstring>
25#include <cstdlib>
26#include <iostream>
27#include <cstdio>
28
29#include <execinfo.h>
30#include <cxxabi.h>
31
32namespace drizzled
33{
34namespace util
35{
36
37void custom_backtrace(const char *file, int line, const char *func, size_t depth)
38{
39 (void)file; (void)line; (void)func; (void)depth;
40 void *array[50];
41
42 size_t size= backtrace(array, 50);
43 char **strings= backtrace_symbols(array, size);
44
45 if (strings == NULL)
46 {
47 return;
48 }
49
50 std::cerr << std::endl << "call_backtrace(" << size << ") began at " << file << ":" << line << " for " << func << "()" << std::endl;
51
52 if (depth == 0)
53 {
54 depth= size;
55 }
56 else
57 {
58 depth= std::min(depth, size);
59 }
60
61 char *named_function= (char *)::realloc(NULL, 1024);
62
63 if (named_function == NULL)
64 {
65 ::free(strings);
66 return;
67 }
68
69 for (size_t x= 1; x < depth; x++)
70 {
71 if (true) // DEMANGLE
72 {
73 size_t sz= 200;
74 char *named_function_ptr= (char *)::realloc(named_function, sz);
75 if (named_function_ptr == NULL)
76 {
77 continue;
78 }
79 named_function= named_function_ptr;
80
81 char *begin_name= 0;
82 char *begin_offset= 0;
83 char *end_offset= 0;
84
85 for (char *j= strings[x]; *j; ++j)
86 {
87 if (*j == '(')
88 {
89 begin_name= j;
90 }
91 else if (*j == '+')
92 {
93 begin_offset= j;
94 }
95 else if (*j == ')' and begin_offset)
96 {
97 end_offset= j;
98 break;
99 }
100 }
101
102 if (begin_name and begin_offset and end_offset and begin_name < begin_offset)
103 {
104 *begin_name++= '\0';
105 *begin_offset++= '\0';
106 *end_offset= '\0';
107
108 int status;
109 char *ret= abi::__cxa_demangle(begin_name, named_function, &sz, &status);
110 if (ret) // realloc()'ed string
111 {
112 named_function= ret;
113 std::cerr << " " << strings[x] << " : " << begin_name << "() + " << begin_offset << std::endl;
114 }
115 else
116 {
117 std::cerr << " " << strings[x] << " : " << begin_name << "() + " << begin_offset << std::endl;
118 }
119 }
120 else
121 {
122 std::cerr << " " << strings[x] << std::endl;
123 }
124 }
125 else
126 {
127 std::cerr << " unmangled:" << strings[x] << std::endl;
128 }
129 }
130
131 ::free(named_function);
132 ::free(strings);
133}
134
135} /* namespace util */
136} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.