]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/assert.cc
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 "BackTrace.h"
16 #include "common/ceph_context.h"
17 #include "common/config.h"
18 #include "common/debug.h"
19 #include "common/Clock.h"
20 #include "include/assert.h"
29 static CephContext
*g_assert_context
= NULL
;
31 /* If you register an assert context, assert() will try to lock the dout
32 * stream of that context before starting an assert. This is nice because the
33 * output looks better. Your assert will not be interleaved with other dout
36 * However, this is strictly optional and library code currently does not
37 * register an assert context. The extra complexity of supporting this
38 * wouldn't really be worth it.
40 void register_assert_context(CephContext
*cct
)
42 assert(!g_assert_context
);
43 g_assert_context
= cct
;
46 void __ceph_assert_fail(const char *assertion
, const char *file
, int line
,
50 tss
<< ceph_clock_now();
53 snprintf(buf
, sizeof(buf
),
54 "%s: In function '%s' thread %llx time %s\n"
55 "%s: %d: FAILED assert(%s)\n",
56 file
, func
, (unsigned long long)pthread_self(), tss
.str().c_str(),
57 file
, line
, assertion
);
60 // TODO: get rid of this memory allocation.
63 dout_emergency(oss
.str());
65 dout_emergency(" NOTE: a copy of the executable, or `objdump -rdS <executable>` "
66 "is needed to interpret this.\n");
68 if (g_assert_context
) {
69 lderr(g_assert_context
) << buf
<< std::endl
;
71 *_dout
<< " NOTE: a copy of the executable, or `objdump -rdS <executable>` "
72 << "is needed to interpret this.\n" << dendl
;
74 g_assert_context
->_log
->dump_recent();
80 void __ceph_assertf_fail(const char *assertion
, const char *file
, int line
,
81 const char *func
, const char* msg
, ...)
84 tss
<< ceph_clock_now();
88 BufAppender(char* buf
, int size
) : bufptr(buf
), remaining(size
) {
91 void printf(const char * format
, ...) {
93 va_start(args
, format
);
94 this->vprintf(format
, args
);
98 void vprintf(const char * format
, va_list args
) {
99 int n
= vsnprintf(bufptr
, remaining
, format
, args
);
116 BufAppender
ba(buf
, sizeof(buf
));
117 BackTrace
*bt
= new BackTrace(1);
118 ba
.printf("%s: In function '%s' thread %llx time %s\n"
119 "%s: %d: FAILED assert(%s)\n",
120 file
, func
, (unsigned long long)pthread_self(), tss
.str().c_str(),
121 file
, line
, assertion
);
122 ba
.printf("Assertion details: ");
125 ba
.vprintf(msg
, args
);
130 // TODO: get rid of this memory allocation.
133 dout_emergency(oss
.str());
135 dout_emergency(" NOTE: a copy of the executable, or `objdump -rdS <executable>` "
136 "is needed to interpret this.\n");
138 if (g_assert_context
) {
139 lderr(g_assert_context
) << buf
<< std::endl
;
141 *_dout
<< " NOTE: a copy of the executable, or `objdump -rdS <executable>` "
142 << "is needed to interpret this.\n" << dendl
;
144 g_assert_context
->_log
->dump_recent();
150 void __ceph_assert_warn(const char *assertion
, const char *file
,
151 int line
, const char *func
)
154 snprintf(buf
, sizeof(buf
),
155 "WARNING: assert(%s) at: %s: %d: %s()\n",
156 assertion
, file
, line
, func
);