summaryrefslogtreecommitdiff
path: root/libshouldbeinlibc/assert-backtrace.c
blob: 7c6fac515e9b102d7a909d26869f79e9b553cb68 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Augment failing assertions with backtraces.

   Copyright (C) 2015,2016 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd 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, or (at
   your option) any later version.

   The GNU Hurd 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 the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.  */

#ifndef NDEBUG

#include <error.h>
#include <errno.h>
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mach/mach_traps.h>

#include "assert-backtrace.h"

static const size_t size = 128;
static const size_t skip = 2;

static void __attribute__ ((noreturn))
__assert_fail_base_backtrace (const char *fmt,
			      const char *assertion,
			      const char *file,
			      unsigned int line,
			      const char *function)
{
  int nptrs;
  void *buffer[size];

  nptrs = backtrace(buffer, size);
  if (nptrs == 0)
    error (1, *__errno_location (), "backtrace");

  fprintf (stderr,
	   fmt, program_invocation_name, file, line, function, assertion);
  if (nptrs > skip)
    {
      backtrace_symbols_fd (&buffer[skip], nptrs - skip, STDERR_FILENO);
      fflush (stderr);
    }

  /* Die.  */
  abort ();
}

void
__assert_fail_backtrace (const char *assertion, const char *file,
			 unsigned int line, const char *function)
{
  __assert_fail_base_backtrace ("%s: %s:%u: %s: Assertion '%s' failed.\n",
				assertion, file, line, function);
}

void
__assert_perror_fail_backtrace (int errnum,
				const char *file,
				unsigned int line,
				const char *function)
{
  char errbuf[1024];

  char *e = strerror_r (errnum, errbuf, sizeof errbuf);
  __assert_fail_base_backtrace ("%s: %s:%u: %s: Unexpected error: %s.\n",
				e, file, line, function);

}

void
backtrace_stderr (void)
{
  int nptrs;
  void *buffer[size];

  nptrs = backtrace (buffer, size);
  backtrace_symbols_fd (buffer, nptrs, STDERR_FILENO);
  fflush (stderr);
}

void
backtrace_mach (void)
{
  int nptrs;
  void *buffer[size];
  char **symbols;
  int i;

  nptrs = backtrace (buffer, size);
  symbols = backtrace_symbols (buffer, nptrs);

  for (i = 0; i < nptrs; i++)
    {
      mach_print(symbols[i]);
      mach_print("\n");
    }

  free(symbols);
}

#endif /* ! defined NDEBUG */