tor  0.4.2.0-alpha-dev
util_bug.c
Go to the documentation of this file.
1 /* Copyright (c) 2003, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
10 #include "orconfig.h"
11 #include "lib/log/util_bug.h"
12 #include "lib/log/log.h"
13 #include "lib/err/backtrace.h"
14 #include "lib/err/torerr.h"
15 #ifdef TOR_UNIT_TESTS
18 #endif
19 #include "lib/malloc/malloc.h"
20 #include "lib/string/printf.h"
21 
22 #include <string.h>
23 #include <stdlib.h>
24 
25 #ifdef TOR_UNIT_TESTS
26 static void (*failed_assertion_cb)(void) = NULL;
27 static int n_bugs_to_capture = 0;
28 static smartlist_t *bug_messages = NULL;
29 #define capturing_bugs() (bug_messages != NULL && n_bugs_to_capture)
30 void
31 tor_capture_bugs_(int n)
32 {
33  tor_end_capture_bugs_();
34  bug_messages = smartlist_new();
35  n_bugs_to_capture = n;
36 }
37 void
38 tor_end_capture_bugs_(void)
39 {
40  n_bugs_to_capture = 0;
41  if (!bug_messages)
42  return;
43  SMARTLIST_FOREACH(bug_messages, char *, cp, tor_free(cp));
44  smartlist_free(bug_messages);
45  bug_messages = NULL;
46 }
47 const smartlist_t *
48 tor_get_captured_bug_log_(void)
49 {
50  return bug_messages;
51 }
52 static void
53 add_captured_bug(const char *s)
54 {
55  --n_bugs_to_capture;
56  smartlist_add_strdup(bug_messages, s);
57 }
62 void
63 tor_set_failed_assertion_callback(void (*fn)(void))
64 {
65  failed_assertion_cb = fn;
66 }
67 #else /* !(defined(TOR_UNIT_TESTS)) */
68 #define capturing_bugs() (0)
69 #define add_captured_bug(s) do { } while (0)
70 #endif /* defined(TOR_UNIT_TESTS) */
71 
73 void
74 CHECK_PRINTF(5, 6)
75 tor_assertion_failed_(const char *fname, unsigned int line,
76  const char *func, const char *expr,
77  const char *fmt, ...)
78 {
79  char *buf = NULL;
80  char *extra = NULL;
81  va_list ap;
82 
83 #ifdef __clang__
84 #pragma clang diagnostic push
85 #pragma clang diagnostic ignored "-Wformat-nonliteral"
86 #endif
87  if (fmt) {
88  va_start(ap,fmt);
89  tor_vasprintf(&extra, fmt, ap);
90  va_end(ap);
91  }
92 #ifdef __clang__
93 #pragma clang diagnostic pop
94 #endif
95 
96  log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
97  fname, line, func, expr);
98  tor_asprintf(&buf, "Assertion %s failed in %s at %s:%u: %s",
99  expr, func, fname, line, extra ? extra : "");
100  tor_free(extra);
101  log_backtrace(LOG_ERR, LD_BUG, buf);
102  tor_free(buf);
103 }
104 
106 void
107 CHECK_PRINTF(6, 7)
108 tor_bug_occurred_(const char *fname, unsigned int line,
109  const char *func, const char *expr,
110  int once, const char *fmt, ...)
111 {
112  char *buf = NULL;
113  const char *once_str = once ?
114  " (Future instances of this warning will be silenced.)": "";
115  if (! expr) {
116  if (capturing_bugs()) {
117  add_captured_bug("This line should not have been reached.");
118  return;
119  }
120  log_warn(LD_BUG, "%s:%u: %s: This line should not have been reached.%s",
121  fname, line, func, once_str);
122  tor_asprintf(&buf,
123  "Line unexpectedly reached at %s at %s:%u",
124  func, fname, line);
125  } else {
126  if (capturing_bugs()) {
127  add_captured_bug(expr);
128  return;
129  }
130 
131  va_list ap;
132  char *extra = NULL;
133 
134 #ifdef __clang__
135 #pragma clang diagnostic push
136 #pragma clang diagnostic ignored "-Wformat-nonliteral"
137 #endif
138  if (fmt) {
139  va_start(ap,fmt);
140  tor_vasprintf(&extra, fmt, ap);
141  va_end(ap);
142  }
143 #ifdef __clang__
144 #pragma clang diagnostic pop
145 #endif
146 
147  log_warn(LD_BUG, "%s:%u: %s: Non-fatal assertion %s failed.%s",
148  fname, line, func, expr, once_str);
149  tor_asprintf(&buf, "Non-fatal assertion %s failed in %s at %s:%u%s%s",
150  expr, func, fname, line, fmt ? " : " : "",
151  extra ? extra : "");
152  tor_free(extra);
153  }
154  log_backtrace(LOG_WARN, LD_BUG, buf);
155  tor_free(buf);
156 
157 #ifdef TOR_UNIT_TESTS
158  if (failed_assertion_cb) {
159  failed_assertion_cb();
160  }
161 #endif
162 }
163 
172 void
174 {
176  tor_raw_abort_();
177 }
178 
179 #ifdef _WIN32
180 
186 const char *
187 tor_fix_source_file(const char *fname)
188 {
189  const char *cp1, *cp2, *r;
190  cp1 = strrchr(fname, '/');
191  cp2 = strrchr(fname, '\\');
192  if (cp1 && cp2) {
193  r = (cp1<cp2)?(cp2+1):(cp1+1);
194  } else if (cp1) {
195  r = cp1+1;
196  } else if (cp2) {
197  r = cp2+1;
198  } else {
199  r = fname;
200  }
201  return r;
202 }
203 #endif /* defined(_WIN32) */
Header for printf.c.
Header for backtrace.c.
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
int tor_vasprintf(char **strp, const char *fmt, va_list args)
Definition: printf.c:96
#define tor_free(p)
Definition: malloc.h:52
Headers for util_malloc.c.
Top-level declarations for the smartlist_t dynamic array type.
void tor_abort_(void)
Definition: util_bug.c:173
Macros for iterating over the elements of a smartlist_t.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
void tor_assertion_failed_(const char *fname, unsigned int line, const char *func, const char *expr, const char *fmt,...)
Definition: util_bug.c:75
#define LOG_WARN
Definition: log.h:51
void logs_close_sigsafe(void)
Definition: log.c:846
#define LOG_ERR
Definition: log.h:54
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Headers for torerr.c.
void tor_bug_occurred_(const char *fname, unsigned int line, const char *func, const char *expr, int once, const char *fmt,...)
Definition: util_bug.c:108
Headers for log.c.
Macros to manage assertions, fatal and non-fatal.
#define LD_BUG
Definition: log.h:84
void tor_raw_abort_(void)
Definition: torerr.c:224