1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2008-2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "common/debug.h"
18 static CephContext *g_assert_context = NULL;
20 /* If you register an assert context, assert() will try to lock the dout
21 * stream of that context before starting an assert. This is nice because the
22 * output looks better. Your assert will not be interleaved with other dout
25 * However, this is strictly optional and library code currently does not
26 * register an assert context. The extra complexity of supporting this
27 * wouldn't really be worth it.
29 void register_assert_context(CephContext *cct)
31 assert(!g_assert_context);
32 g_assert_context = cct;
35 void __ceph_assert_fail(const char *assertion, const char *file, int line,
39 tss << ceph_clock_now();
42 snprintf(buf, sizeof(buf),
43 "%s: In function '%s' thread %llx time %s\n"
44 "%s: %d: FAILED assert(%s)\n",
45 file, func, (unsigned long long)pthread_self(), tss.str().c_str(),
46 file, line, assertion);
49 // TODO: get rid of this memory allocation.
52 dout_emergency(oss.str());
54 dout_emergency(" NOTE: a copy of the executable, or `objdump -rdS <executable>` "
55 "is needed to interpret this.\n");
57 if (g_assert_context) {
58 lderr(g_assert_context) << buf << std::endl;
60 *_dout << " NOTE: a copy of the executable, or `objdump -rdS <executable>` "
61 << "is needed to interpret this.\n" << dendl;
63 g_assert_context->_log->dump_recent();
69 void __ceph_assertf_fail(const char *assertion, const char *file, int line,
70 const char *func, const char* msg, ...)
73 tss << ceph_clock_now();
77 BufAppender(char* buf, int size) : bufptr(buf), remaining(size) {
80 void printf(const char * format, ...) {
82 va_start(args, format);
83 this->vprintf(format, args);
87 void vprintf(const char * format, va_list args) {
88 int n = vsnprintf(bufptr, remaining, format, args);
105 BufAppender ba(buf, sizeof(buf));
106 BackTrace *bt = new BackTrace(1);
107 ba.printf("%s: In function '%s' thread %llx time %s\n"
108 "%s: %d: FAILED assert(%s)\n",
109 file, func, (unsigned long long)pthread_self(), tss.str().c_str(),
110 file, line, assertion);
111 ba.printf("Assertion details: ");
114 ba.vprintf(msg, args);
119 // TODO: get rid of this memory allocation.
122 dout_emergency(oss.str());
124 dout_emergency(" NOTE: a copy of the executable, or `objdump -rdS <executable>` "
125 "is needed to interpret this.\n");
127 if (g_assert_context) {
128 lderr(g_assert_context) << buf << std::endl;
130 *_dout << " NOTE: a copy of the executable, or `objdump -rdS <executable>` "
131 << "is needed to interpret this.\n" << dendl;
133 g_assert_context->_log->dump_recent();
139 void __ceph_assert_warn(const char *assertion, const char *file,
140 int line, const char *func)
143 snprintf(buf, sizeof(buf),
144 "WARNING: assert(%s) at: %s: %d: %s()\n",
145 assertion, file, line, func);