summaryrefslogtreecommitdiffstats
path: root/src/common/assert.cc
blob: 6fb50014f4399b93ce1c541379b7c5993116e7fe (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2008-2011 New Dream Network
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "include/compat.h"
#include "common/debug.h"

namespace ceph {
  static CephContext *g_assert_context = NULL;

  /* If you register an assert context, ceph_assert() will try to lock the dout
   * stream of that context before starting an assert. This is nice because the
   * output looks better. Your assert will not be interleaved with other dout
   * statements.
   *
   * However, this is strictly optional and library code currently does not
   * register an assert context. The extra complexity of supporting this
   * wouldn't really be worth it.
   */
  void register_assert_context(CephContext *cct)
  {
    ceph_assert(!g_assert_context);
    g_assert_context = cct;
  }

  [[gnu::cold]] void __ceph_assert_fail(const char *assertion,
					const char *file, int line,
					const char *func)
  {
    g_assert_condition = assertion;
    g_assert_file = file;
    g_assert_line = line;
    g_assert_func = func;
    g_assert_thread = (unsigned long long)pthread_self();
    ceph_pthread_getname(pthread_self(), g_assert_thread_name,
		       sizeof(g_assert_thread_name));

    ostringstream tss;
    tss << ceph_clock_now();

    snprintf(g_assert_msg, sizeof(g_assert_msg),
	     "%s: In function '%s' thread %llx time %s\n"
	     "%s: %d: FAILED ceph_assert(%s)\n",
	     file, func, (unsigned long long)pthread_self(), tss.str().c_str(),
	     file, line, assertion);
    dout_emergency(g_assert_msg);

    // TODO: get rid of this memory allocation.
    ostringstream oss;
    oss << BackTrace(1);
    dout_emergency(oss.str());

    if (g_assert_context) {
      lderr(g_assert_context) << g_assert_msg << std::endl;
      *_dout << oss.str() << dendl;

      // dump recent only if the abort signal handler won't do it for us
      if (!g_assert_context->_conf->fatal_signal_handlers) {
	g_assert_context->_log->dump_recent();
      }
    }

    abort();
  }

  [[gnu::cold]] void __ceph_assert_fail(const assert_data &ctx)
  {
    __ceph_assert_fail(ctx.assertion, ctx.file, ctx.line, ctx.function);
  }

  class BufAppender {
  public:
    BufAppender(char* buf, int size) : bufptr(buf), remaining(size) {}

    void printf(const char * format, ...) {
      va_list args;
      va_start(args, format);
      this->vprintf(format, args);
      va_end(args);
    }

    void vprintf(const char * format, va_list args) {
      int n = vsnprintf(bufptr, remaining, format, args);
      if (n >= 0) {
	if (n < remaining) {
	  remaining -= n;
	  bufptr += n;
	} else {
	  remaining = 0;
	}
      }
    }

  private:
    char* bufptr;
    int remaining;
  };


  [[gnu::cold]] void __ceph_assertf_fail(const char *assertion,
					 const char *file, int line,
					 const char *func, const char* msg,
					 ...)
  {
    ostringstream tss;
    tss << ceph_clock_now();

    g_assert_condition = assertion;
    g_assert_file = file;
    g_assert_line = line;
    g_assert_func = func;
    g_assert_thread = (unsigned long long)pthread_self();
    ceph_pthread_getname(pthread_self(), g_assert_thread_name,
		       sizeof(g_assert_thread_name));

    BufAppender ba(g_assert_msg, sizeof(g_assert_msg));
    BackTrace *bt = new BackTrace(1);
    ba.printf("%s: In function '%s' thread %llx time %s\n"
	     "%s: %d: FAILED ceph_assert(%s)\n",
	     file, func, (unsigned long long)pthread_self(), tss.str().c_str(),
	     file, line, assertion);
    ba.printf("Assertion details: ");
    va_list args;
    va_start(args, msg);
    ba.vprintf(msg, args);
    va_end(args);
    ba.printf("\n");
    dout_emergency(g_assert_msg);

    // TODO: get rid of this memory allocation.
    ostringstream oss;
    oss << *bt;
    dout_emergency(oss.str());

    if (g_assert_context) {
      lderr(g_assert_context) << g_assert_msg << std::endl;
      *_dout << oss.str() << dendl;

      // dump recent only if the abort signal handler won't do it for us
      if (!g_assert_context->_conf->fatal_signal_handlers) {
	g_assert_context->_log->dump_recent();
      }
    }

    abort();
  }

  [[gnu::cold]] void __ceph_abort(const char *file, int line,
				  const char *func, const string& msg)
  {
    ostringstream tss;
    tss << ceph_clock_now();

    g_assert_condition = "abort";
    g_assert_file = file;
    g_assert_line = line;
    g_assert_func = func;
    g_assert_thread = (unsigned long long)pthread_self();
    ceph_pthread_getname(pthread_self(), g_assert_thread_name,
		       sizeof(g_assert_thread_name));

    BackTrace *bt = new BackTrace(1);
    snprintf(g_assert_msg, sizeof(g_assert_msg),
             "%s: In function '%s' thread %llx time %s\n"
	     "%s: %d: ceph_abort_msg(\"%s\")\n", file, func,
	     (unsigned long long)pthread_self(),
	     tss.str().c_str(), file, line,
	     msg.c_str());
    dout_emergency(g_assert_msg);

    // TODO: get rid of this memory allocation.
    ostringstream oss;
    oss << *bt;
    dout_emergency(oss.str());

    if (g_assert_context) {
      lderr(g_assert_context) << g_assert_msg << std::endl;
      *_dout << oss.str() << dendl;

      // dump recent only if the abort signal handler won't do it for us
      if (!g_assert_context->_conf->fatal_signal_handlers) {
	g_assert_context->_log->dump_recent();
      }
    }

    abort();
  }

  [[gnu::cold]] void __ceph_abortf(const char *file, int line,
				   const char *func, const char* msg,
				   ...)
  {
    ostringstream tss;
    tss << ceph_clock_now();

    g_assert_condition = "abort";
    g_assert_file = file;
    g_assert_line = line;
    g_assert_func = func;
    g_assert_thread = (unsigned long long)pthread_self();
    ceph_pthread_getname(pthread_self(), g_assert_thread_name,
		       sizeof(g_assert_thread_name));

    BufAppender ba(g_assert_msg, sizeof(g_assert_msg));
    BackTrace *bt = new BackTrace(1);
    ba.printf("%s: In function '%s' thread %llx time %s\n"
	      "%s: %d: abort()\n",
	      file, func, (unsigned long long)pthread_self(), tss.str().c_str(),
	      file, line);
    ba.printf("Abort details: ");
    va_list args;
    va_start(args, msg);
    ba.vprintf(msg, args);
    va_end(args);
    ba.printf("\n");
    dout_emergency(g_assert_msg);

    // TODO: get rid of this memory allocation.
    ostringstream oss;
    oss << *bt;
    dout_emergency(oss.str());

    if (g_assert_context) {
      lderr(g_assert_context) << g_assert_msg << std::endl;
      *_dout << oss.str() << dendl;

      // dump recent only if the abort signal handler won't do it for us
      if (!g_assert_context->_conf->fatal_signal_handlers) {
	g_assert_context->_log->dump_recent();
      }
    }

    abort();
  }

  [[gnu::cold]] void __ceph_assert_warn(const char *assertion,
					const char *file,
					int line, const char *func)
  {
    char buf[8096];
    snprintf(buf, sizeof(buf),
	     "WARNING: ceph_assert(%s) at: %s: %d: %s()\n",
	     assertion, file, line, func);
    dout_emergency(buf);
  }
}