Skip to content
This repository was archived by the owner on Aug 13, 2025. It is now read-only.

Commit 295e7c8

Browse files
committed
Ad-hoc backtrace logging with isc_backtrace_log()
It's sometimes helpful to get a quick idea of the call stack when debugging. This change factors out the backtrace logging from named's fatal error handler so that it's easy to use in other places too.
1 parent cc8b9c1 commit 295e7c8

File tree

4 files changed

+50
-28
lines changed

4 files changed

+50
-28
lines changed

bin/named/main.c

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@
101101
*/
102102
/* #include "xxdb.h" */
103103

104-
/*
105-
* The maximum number of stack frames to dump on assertion failure.
106-
*/
107-
#ifndef BACKTRACE_MAXFRAME
108-
#define BACKTRACE_MAXFRAME 128
109-
#endif /* ifndef BACKTRACE_MAXFRAME */
110-
111104
extern unsigned int dns_zone_mkey_hour;
112105
extern unsigned int dns_zone_mkey_day;
113106
extern unsigned int dns_zone_mkey_month;
@@ -189,9 +182,6 @@ assertion_failed(const char *file, int line, isc_assertiontype_t type,
189182
static void
190183
assertion_failed(const char *file, int line, isc_assertiontype_t type,
191184
const char *cond) {
192-
void *tracebuf[BACKTRACE_MAXFRAME];
193-
int nframes;
194-
195185
/*
196186
* Handle assertion failures.
197187
*/
@@ -203,24 +193,12 @@ assertion_failed(const char *file, int line, isc_assertiontype_t type,
203193
*/
204194
isc_assertion_setcallback(NULL);
205195

206-
nframes = isc_backtrace(tracebuf, BACKTRACE_MAXFRAME);
207196
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
208197
NAMED_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
209-
"%s:%d: %s(%s) failed%s", file, line,
210-
isc_assertion_typetotext(type), cond,
211-
(nframes > 0) ? ", back trace" : "");
212-
if (nframes > 0) {
213-
char **strs = isc_backtrace_symbols(tracebuf, nframes);
214-
if (strs != NULL) {
215-
for (int i = 0; i < nframes; i++) {
216-
isc_log_write(named_g_lctx,
217-
NAMED_LOGCATEGORY_GENERAL,
218-
NAMED_LOGMODULE_MAIN,
219-
ISC_LOG_CRITICAL, "%s",
220-
strs[i]);
221-
}
222-
}
223-
}
198+
"%s:%d: %s(%s) failed", file, line,
199+
isc_assertion_typetotext(type), cond);
200+
isc_backtrace_log(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
201+
NAMED_LOGMODULE_MAIN, ISC_LOG_CRITICAL);
224202
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
225203
NAMED_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
226204
"exiting (due to assertion failure)");

lib/isc/assertions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ isc_assertion_typetotext(isc_assertiontype_t type) {
9595
static void
9696
default_callback(const char *file, int line, isc_assertiontype_t type,
9797
const char *cond) {
98-
void *tracebuf[BACKTRACE_MAXFRAME];
99-
int nframes = isc_backtrace(tracebuf, BACKTRACE_MAXFRAME);
98+
void *tracebuf[ISC_BACKTRACE_MAXFRAME];
99+
int nframes = isc_backtrace(tracebuf, ISC_BACKTRACE_MAXFRAME);
100100

101101
fprintf(stderr, "%s:%d: %s(%s) failed%s\n", file, line,
102102
isc_assertion_typetotext(type), cond,

lib/isc/backtrace.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#endif /* HAVE_BACKTRACE_SYMBOLS */
2121

2222
#include <isc/backtrace.h>
23+
#include <isc/log.h>
2324
#include <isc/result.h>
2425
#include <isc/util.h>
2526

@@ -60,6 +61,26 @@ isc_backtrace_symbols_fd(void *const *buffer, int size, int fd) {
6061
backtrace_symbols_fd(buffer, size, fd);
6162
}
6263

64+
void
65+
isc_backtrace_log(isc_log_t *lctx, isc_logcategory_t *category,
66+
isc_logmodule_t *module, int level) {
67+
void *tracebuf[ISC_BACKTRACE_MAXFRAME];
68+
int nframes;
69+
char **strs;
70+
71+
nframes = isc_backtrace(tracebuf, ISC_BACKTRACE_MAXFRAME);
72+
if (nframes <= 0) {
73+
return;
74+
}
75+
strs = isc_backtrace_symbols(tracebuf, nframes);
76+
if (strs == NULL) {
77+
return;
78+
}
79+
for (int i = 0; i < nframes; i++) {
80+
isc_log_write(lctx, category, module, level, "%s", strs[i]);
81+
}
82+
}
83+
6384
#else /* HAVE_BACKTRACE_SYMBOLS */
6485

6586
int
@@ -85,4 +106,13 @@ isc_backtrace_symbols_fd(void *const *buffer, int size, int fd) {
85106
UNUSED(fd);
86107
}
87108

109+
void
110+
isc_backtrace_log(isc_log_t *lctx, isc_logcategory_t *category,
111+
isc_logmodule_t *module, int level) {
112+
UNUSED(lctx);
113+
UNUSED(category);
114+
UNUSED(module);
115+
UNUSED(level);
116+
}
117+
88118
#endif /* HAVE_BACKTRACE_SYMBOLS */

lib/isc/include/isc/backtrace.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
***/
3535
#include <isc/types.h>
3636

37+
/*
38+
* The maximum number of stack frames to dump on assertion failure.
39+
*/
40+
#ifndef ISC_BACKTRACE_MAXFRAME
41+
#define ISC_BACKTRACE_MAXFRAME 128
42+
#endif /* ifndef ISC_BACKTRACE_MAXFRAME */
43+
3744
/***
3845
*** Functions
3946
***/
@@ -98,4 +105,11 @@ isc_backtrace_symbols_fd(void *const *buffer, int size, int fd);
98105
*\li See platform NOTES for backtrace_symbols_fd for caveats
99106
*/
100107

108+
void
109+
isc_backtrace_log(isc_log_t *lctx, isc_logcategory_t *category,
110+
isc_logmodule_t *module, int level);
111+
/*
112+
* Write a backtrace to the log.
113+
*/
114+
101115
ISC_LANG_ENDDECLS

0 commit comments

Comments
 (0)