Freeciv-3.3
Loading...
Searching...
No Matches
fcbacktrace.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif /* HAVE_CONFIG_H */
17
18#include "fc_prehdrs.h"
19
20#ifdef HAVE_EXECINFO_H
21#include <execinfo.h>
22#endif
23
24/* utility */
25#include "log.h"
26#include "shared.h"
27
28#include "fcbacktrace.h"
29
30/* We don't want backtrace-spam to testmatic logs */
31#if defined(FREECIV_DEBUG) && defined(HAVE_BACKTRACE) && !defined(FREECIV_TESTMATIC)
32#define BACKTRACE_ACTIVE 1
33#endif
34
35#ifdef BACKTRACE_ACTIVE
36
37/* We write always in level LOG_NORMAL and not in higher one since those
38 * interact badly with server callback to send error messages to local
39 * client. */
40#define LOG_BACKTRACE LOG_NORMAL
41
42#define MAX_NUM_FRAMES 256
43
44static log_pre_callback_fn previous = nullptr;
45
47 const char *where, const char *msg);
48static void backtrace_log(enum log_level level, bool print_from_where,
49 const char *where, const char *msg);
50#endif /* BACKTRACE_ACTIVE */
51
52/********************************************************************/
56{
57#ifdef BACKTRACE_ACTIVE
59#endif
60}
61
62/********************************************************************/
66{
67#ifdef BACKTRACE_ACTIVE
69
71
72 if (active != backtrace_log) {
73 /* We were not the active callback!
74 * Restore the active callback and log error */
76 log_error("Backtrace log (pre)callback cannot be removed");
77 }
78#endif /* BACKTRACE_ACTIVE */
79}
80
81#ifdef BACKTRACE_ACTIVE
82/********************************************************************/
86 const char *where, const char *msg)
87{
88 /* Current behavior of this function is to write to chained callback,
89 * nothing more, nothing less. */
90 if (previous != nullptr) {
92 }
93}
94
95/********************************************************************/
98static void backtrace_log(enum log_level level, bool print_from_where,
99 const char *where, const char *msg)
100{
101 if (previous != nullptr) {
102 /* Call chained callback first */
104 }
105
106 if (level <= LOG_ERROR) {
108 }
109}
110
111#endif /* BACKTRACE_ACTIVE */
112
113/********************************************************************/
117{
118#ifdef BACKTRACE_ACTIVE
119 void *buffer[MAX_NUM_FRAMES];
120 int frames;
121 char **names;
122
123 frames = backtrace(buffer, ARRAY_SIZE(buffer));
124 names = backtrace_symbols(buffer, frames);
125
126 if (names == nullptr) {
127 write_backtrace_line(level, FALSE, nullptr, "No backtrace");
128 } else {
129 int i;
130
131 write_backtrace_line(level, FALSE, nullptr, "Backtrace:");
132
133 for (i = 0; i < MIN(frames, MAX_NUM_FRAMES); i++) {
134 char linestr[256];
135
136 fc_snprintf(linestr, sizeof(linestr), "%5d: %s", i, names[i]);
137
139 }
140
141 free(names);
142 }
143#endif /* BACKTRACE_ACTIVE */
144}
char * incite_cost
Definition comments.c:76
void backtrace_deinit(void)
Definition fcbacktrace.c:65
void backtrace_init(void)
Definition fcbacktrace.c:55
void backtrace_print(enum log_level level)
log_pre_callback_fn log_set_pre_callback(log_pre_callback_fn precallback)
Definition log.c:278
void(* log_pre_callback_fn)(enum log_level, bool print_from_where, const char *where, const char *msg)
Definition log.h:48
log_level
Definition log.h:29
@ LOG_ERROR
Definition log.h:31
#define log_error(message,...)
Definition log.h:104
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MIN(x, y)
Definition shared.h:55
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
#define FALSE
Definition support.h:47