File: kvu_debug.cpp

package info (click to toggle)
ecasound 2.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,292 kB
  • sloc: cpp: 39,475; sh: 4,335; lisp: 1,918; ansic: 1,883; makefile: 888; python: 617; ruby: 202
file content (87 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// ------------------------------------------------------------------------
// kvu_debug.cpp: Debug helper functions
// Copyright (C) 2009 Kai Vehmanen
//
// Attributes:
//     eca-style-version: 3
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
// ------------------------------------------------------------------------

// References
//  - glibc manual (section "33.1 Backtraces"
//    http://www.gnu.org/software/libc/manual/html_node/index.html#toc_Debugging-Support


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>

#ifdef HAVE_FEATURES_H
#include <features.h>
#endif

using namespace std;

/* backtrace is an GNU glibc extension and it was 
 * first added to glibc 2.1 */
#if defined(__GLIBC_PREREQ) 
#if __GLIBC_PREREQ(2,1) && defined(HAVE_EXECINFO_H)

#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

void kvu_print_backtrace_stderr(void)
{
  const int NUM_STACK_FRAMES = 10;
  void *array[NUM_STACK_FRAMES];
  size_t size;
  char **strings;
  size_t i;

  size = backtrace (array, NUM_STACK_FRAMES);
  strings = backtrace_symbols (array, size);

  cerr << 
"-----------------------------------------------------------------------"
       << endl
       << "Function call backtrace ("
       << size 
       << " frames):" << endl;

  for (i = 0; i < size; i++)
    cerr << " " << i << ": " << strings[i] << endl;

  free (strings);
  cerr << 
"-----------------------------------------------------------------------"
       << endl;
}

#endif

#else

/* non-glibc case */

void kvu_print_backtrace_stderr(void)
{
  cout << "ERROR (kvu_print_backtrace_stderr): backtrace printing only supported with glibc" << endl;
}

#endif