0bb088066466cb1ea331e849305ed783cdd990fc
[lttng-tools.git] / src / common / error.cpp
1 /*
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "error.hpp"
10
11 #include <common/common.hpp>
12 #include <common/compat/errno.hpp>
13 #include <common/compat/getenv.hpp>
14 #include <common/thread.hpp>
15
16 #include <lttng/lttng-error.h>
17
18 #include <inttypes.h>
19 #include <pthread.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 namespace {
24 /*
25 * lttng_opt_abort_on_error: unset: -1, disabled: 0, enabled: 1.
26 * Controlled by the LTTNG_ABORT_ON_ERROR environment variable.
27 */
28 int lttng_opt_abort_on_error = -1;
29
30 /* TLS variable that contains the time of one single log entry. */
31 DEFINE_URCU_TLS(struct log_time, error_log_time);
32 } /* namespace */
33
34 DEFINE_URCU_TLS(const char *, logger_thread_name);
35
36 const char *log_add_time()
37 {
38 int ret;
39 struct tm tm, *res;
40 struct timespec tp;
41 time_t now;
42 const int errsv = errno;
43
44 ret = lttng_clock_gettime(CLOCK_REALTIME, &tp);
45 if (ret < 0) {
46 goto error;
47 }
48 now = (time_t) tp.tv_sec;
49
50 res = localtime_r(&now, &tm);
51 if (!res) {
52 goto error;
53 }
54
55 /* Format time in the TLS variable. */
56 ret = snprintf(URCU_TLS(error_log_time).str,
57 sizeof(URCU_TLS(error_log_time).str),
58 "%02d:%02d:%02d.%09ld",
59 tm.tm_hour,
60 tm.tm_min,
61 tm.tm_sec,
62 tp.tv_nsec);
63 if (ret < 0) {
64 goto error;
65 }
66
67 errno = errsv;
68 return URCU_TLS(error_log_time).str;
69
70 error:
71 /* Return an empty string on error so logging is not affected. */
72 errno = errsv;
73 return "";
74 }
75
76 void logger_set_thread_name(const char *name, bool set_pthread_name)
77 {
78 int ret;
79
80 LTTNG_ASSERT(name);
81 URCU_TLS(logger_thread_name) = name;
82
83 if (set_pthread_name) {
84 ret = lttng_thread_setname(name);
85 if (ret && ret != -ENOSYS) {
86 /* Don't fail as this is not essential. */
87 DBG("Failed to set pthread name attribute");
88 }
89 }
90 }
91
92 /*
93 * Human readable error message.
94 */
95 static const char *lttng_error_code_str(lttng_error_code code)
96 {
97 switch (code) {
98 case LTTNG_OK:
99 return "Success";
100 case LTTNG_ERR_UNK:
101 return "Unknown error";
102 case LTTNG_ERR_UND:
103 return "Undefined command";
104 case LTTNG_ERR_UNKNOWN_DOMAIN:
105 return "Unknown tracing domain";
106 case LTTNG_ERR_NO_SESSION:
107 return "No session found";
108 case LTTNG_ERR_CREATE_DIR_FAIL:
109 return "Create directory failed";
110 case LTTNG_ERR_SESSION_FAIL:
111 return "Create session failed";
112 case LTTNG_ERR_SESS_NOT_FOUND:
113 return "Session name not found";
114 case LTTNG_ERR_FATAL:
115 return "Fatal error of the session daemon";
116 case LTTNG_ERR_SELECT_SESS:
117 return "A session MUST be selected";
118 case LTTNG_ERR_EXIST_SESS:
119 return "Session name already exists";
120 case LTTNG_ERR_NO_EVENT:
121 return "Event not found";
122 case LTTNG_ERR_CONNECT_FAIL:
123 return "Unable to connect to Unix socket";
124 case LTTNG_ERR_EPERM:
125 return "Permission denied";
126 case LTTNG_ERR_KERN_NA:
127 return "Kernel tracer not available";
128 case LTTNG_ERR_KERN_VERSION:
129 return "Kernel tracer version is not compatible";
130 case LTTNG_ERR_KERN_EVENT_EXIST:
131 return "Kernel event already exists";
132 case LTTNG_ERR_KERN_SESS_FAIL:
133 return "Kernel create session failed";
134 case LTTNG_ERR_KERN_CHAN_EXIST:
135 return "Kernel channel already exists";
136 case LTTNG_ERR_KERN_CHAN_FAIL:
137 return "Kernel create channel failed";
138 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
139 return "Kernel channel not found";
140 case LTTNG_ERR_KERN_CHAN_DISABLE_FAIL:
141 return "Disable kernel channel failed";
142 case LTTNG_ERR_KERN_CHAN_ENABLE_FAIL:
143 return "Enable kernel channel failed";
144 case LTTNG_ERR_KERN_CONTEXT_FAIL:
145 return "Add kernel context failed";
146 case LTTNG_ERR_KERN_ENABLE_FAIL:
147 return "Enable kernel event failed";
148 case LTTNG_ERR_KERN_DISABLE_FAIL:
149 return "Disable kernel event failed";
150 case LTTNG_ERR_KERN_META_FAIL:
151 return "Opening metadata failed";
152 case LTTNG_ERR_KERN_START_FAIL:
153 return "Starting kernel trace failed";
154 case LTTNG_ERR_KERN_STOP_FAIL:
155 return "Stopping kernel trace failed";
156 case LTTNG_ERR_KERN_CONSUMER_FAIL:
157 return "Kernel consumer start failed";
158 case LTTNG_ERR_KERN_STREAM_FAIL:
159 return "Kernel create stream failed";
160 case LTTNG_ERR_KERN_LIST_FAIL:
161 return "Listing kernel events failed";
162 case LTTNG_ERR_UST_CALIBRATE_FAIL:
163 return "UST calibration failed";
164 case LTTNG_ERR_UST_SESS_FAIL:
165 return "UST create session failed";
166 case LTTNG_ERR_UST_CHAN_FAIL:
167 return "UST create channel failed";
168 case LTTNG_ERR_UST_CHAN_EXIST:
169 return "UST channel already exist";
170 case LTTNG_ERR_UST_CHAN_NOT_FOUND:
171 return "UST channel not found";
172 case LTTNG_ERR_UST_CHAN_DISABLE_FAIL:
173 return "Disable UST channel failed";
174 case LTTNG_ERR_UST_CHAN_ENABLE_FAIL:
175 return "Enable UST channel failed";
176 case LTTNG_ERR_UST_ENABLE_FAIL:
177 return "Enable UST event failed";
178 case LTTNG_ERR_UST_DISABLE_FAIL:
179 return "Disable UST event failed";
180 case LTTNG_ERR_UST_META_FAIL:
181 return "Opening metadata failed";
182 case LTTNG_ERR_UST_START_FAIL:
183 return "Starting UST trace failed";
184 case LTTNG_ERR_UST_STOP_FAIL:
185 return "Stopping UST trace failed";
186 case LTTNG_ERR_UST_CONSUMER64_FAIL:
187 return "64-bit UST consumer start failed";
188 case LTTNG_ERR_UST_CONSUMER32_FAIL:
189 return "32-bit UST consumer start failed";
190 case LTTNG_ERR_UST_STREAM_FAIL:
191 return "UST create stream failed";
192 case LTTNG_ERR_UST_LIST_FAIL:
193 return "Listing UST events failed";
194 case LTTNG_ERR_UST_EVENT_EXIST:
195 return "UST event already exist";
196 case LTTNG_ERR_UST_EVENT_NOT_FOUND:
197 return "UST event not found";
198 case LTTNG_ERR_UST_CONTEXT_EXIST:
199 return "UST context already exist";
200 case LTTNG_ERR_UST_CONTEXT_INVAL:
201 return "UST invalid context";
202 case LTTNG_ERR_NEED_ROOT_SESSIOND:
203 return "Tracing the kernel requires a root lttng-sessiond daemon, as well as \"tracing\" group membership or root user ID for the lttng client";
204 case LTTNG_ERR_NO_UST:
205 return "LTTng-UST tracer is not supported. Please rebuild lttng-tools with lttng-ust support enabled";
206 case LTTNG_ERR_TRACE_ALREADY_STARTED:
207 return "Tracing has already been started once";
208 case LTTNG_ERR_TRACE_ALREADY_STOPPED:
209 return "Tracing has already been stopped";
210 case LTTNG_ERR_KERN_EVENT_ENOSYS:
211 return "Kernel event type not supported";
212 case LTTNG_ERR_NEED_CHANNEL_NAME:
213 return "Non-default channel exists within session: channel name needs to be specified with '-c name'";
214 case LTTNG_ERR_INVALID:
215 return "Invalid parameter";
216 case LTTNG_ERR_NO_USTCONSUMERD:
217 return "No UST consumer detected";
218 case LTTNG_ERR_NO_KERNCONSUMERD:
219 return "No kernel consumer detected";
220 case LTTNG_ERR_EVENT_EXIST_LOGLEVEL:
221 return "Event already enabled with different loglevel";
222 case LTTNG_ERR_URL_DATA_MISS:
223 return "Missing data path URL";
224 case LTTNG_ERR_URL_CTRL_MISS:
225 return "Missing control path URL";
226 case LTTNG_ERR_ENABLE_CONSUMER_FAIL:
227 return "Enabling consumer failed";
228 case LTTNG_ERR_RELAYD_CONNECT_FAIL:
229 return "Unable to connect to lttng-relayd";
230 case LTTNG_ERR_RELAYD_VERSION_FAIL:
231 return "Relay daemon not compatible";
232 case LTTNG_ERR_FILTER_INVAL:
233 return "Invalid filter bytecode";
234 case LTTNG_ERR_FILTER_NOMEM:
235 return "Not enough memory for filter bytecode";
236 case LTTNG_ERR_FILTER_EXIST:
237 return "Filter already exist";
238 case LTTNG_ERR_NO_CONSUMER:
239 return "Consumer not found for recording session";
240 case LTTNG_ERR_EXCLUSION_INVAL:
241 return "Invalid event exclusion data";
242 case LTTNG_ERR_EXCLUSION_NOMEM:
243 return "Lack of memory while processing event exclusions";
244 case LTTNG_ERR_NO_SESSIOND:
245 return "No session daemon is available";
246 case LTTNG_ERR_SESSION_STARTED:
247 return "Session is running";
248 case LTTNG_ERR_NOT_SUPPORTED:
249 return "Operation not supported";
250 case LTTNG_ERR_UST_EVENT_ENABLED:
251 return "UST event already enabled";
252 case LTTNG_ERR_SET_URL:
253 return "Error setting URL";
254 case LTTNG_ERR_URL_EXIST:
255 return "URL already exists";
256 case LTTNG_ERR_BUFFER_NOT_SUPPORTED:
257 return "Buffer type not supported";
258 case LTTNG_ERR_BUFFER_TYPE_MISMATCH:
259 return "Buffer type mismatch for session";
260 case LTTNG_ERR_NOMEM:
261 return "Not enough memory";
262 case LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST:
263 return "Snapshot output already exists";
264 case LTTNG_ERR_START_SESSION_ONCE:
265 return "Session needs to be started once";
266 case LTTNG_ERR_SNAPSHOT_FAIL:
267 return "Snapshot record failed";
268 case LTTNG_ERR_NO_STREAM:
269 return "Index without stream on relay";
270 case LTTNG_ERR_CHAN_EXIST:
271 return "Channel already exists";
272 case LTTNG_ERR_SNAPSHOT_NODATA:
273 return "No data available in snapshot";
274 case LTTNG_ERR_NO_CHANNEL:
275 return "No channel found in the session";
276 case LTTNG_ERR_SESSION_INVALID_CHAR:
277 return "Invalid character found in session name";
278 case LTTNG_ERR_SAVE_FILE_EXIST:
279 return "Session file already exists";
280 case LTTNG_ERR_SAVE_IO_FAIL:
281 return "IO error while writing session configuration";
282 case LTTNG_ERR_LOAD_INVALID_CONFIG:
283 return "Invalid session configuration";
284 case LTTNG_ERR_LOAD_IO_FAIL:
285 return "IO error while reading a session configuration";
286 case LTTNG_ERR_LOAD_SESSION_NOENT:
287 return "Session file not found";
288 case LTTNG_ERR_MAX_SIZE_INVALID:
289 return "Snapshot max size is invalid";
290 case LTTNG_ERR_MI_OUTPUT_TYPE:
291 return "Invalid MI output format";
292 case LTTNG_ERR_MI_IO_FAIL:
293 return "IO error while writing MI output";
294 case LTTNG_ERR_MI_NOT_IMPLEMENTED:
295 return "Mi feature not implemented";
296 case LTTNG_ERR_INVALID_EVENT_NAME:
297 return "Invalid event name";
298 case LTTNG_ERR_INVALID_CHANNEL_NAME:
299 return "Invalid channel name";
300 case LTTNG_ERR_PROCESS_ATTR_EXISTS:
301 return "Process attribute is already tracked";
302 case LTTNG_ERR_PROCESS_ATTR_MISSING:
303 return "Process attribute was not tracked";
304 case LTTNG_ERR_INVALID_CHANNEL_DOMAIN:
305 return "Invalid channel domain";
306 case LTTNG_ERR_OVERFLOW:
307 return "Overflow occurred";
308 case LTTNG_ERR_SESSION_NOT_STARTED:
309 return "Session not started";
310 case LTTNG_ERR_LIVE_SESSION:
311 return "Live sessions are not supported";
312 case LTTNG_ERR_PER_PID_SESSION:
313 return "Per-PID recording sessions are not supported";
314 case LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE:
315 return "Context unavailable on this kernel";
316 case LTTNG_ERR_REGEN_STATEDUMP_FAIL:
317 return "Failed to regenerate the state dump";
318 case LTTNG_ERR_REGEN_STATEDUMP_NOMEM:
319 return "Failed to regenerate the state dump, not enough memory";
320 case LTTNG_ERR_NOT_SNAPSHOT_SESSION:
321 return "Snapshot command can't be applied to a non-snapshot session";
322 case LTTNG_ERR_INVALID_TRIGGER:
323 return "Invalid trigger";
324 case LTTNG_ERR_TRIGGER_EXISTS:
325 return "Trigger already registered";
326 case LTTNG_ERR_TRIGGER_NOT_FOUND:
327 return "Trigger not found";
328 case LTTNG_ERR_COMMAND_CANCELLED:
329 return "Command cancelled";
330 case LTTNG_ERR_ROTATION_PENDING:
331 return "Rotation already pending for this session";
332 case LTTNG_ERR_ROTATION_NOT_AVAILABLE:
333 return "Rotation feature not available for this session's creation mode";
334 case LTTNG_ERR_ROTATION_SCHEDULE_SET:
335 return "A session rotation schedule of this type is already set on the session";
336 case LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET:
337 return "No session rotation schedule of this type is set on the session";
338 case LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP:
339 return "Session was already rotated once since it became inactive";
340 case LTTNG_ERR_ROTATION_WRONG_VERSION:
341 return "Session rotation is not supported by this kernel tracer version";
342 case LTTNG_ERR_NO_SESSION_OUTPUT:
343 return "Session has no output";
344 case LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY:
345 return "Rotation feature not available on the relay";
346 case LTTNG_ERR_AGENT_TRACING_DISABLED:
347 return "Session daemon agent tracing is disabled";
348 case LTTNG_ERR_PROBE_LOCATION_INVAL:
349 return "Invalid userspace probe location";
350 case LTTNG_ERR_ELF_PARSING:
351 return "ELF parsing error";
352 case LTTNG_ERR_SDT_PROBE_SEMAPHORE:
353 return "SDT probe guarded by a semaphore";
354 case LTTNG_ERR_ROTATION_FAIL_CONSUMER:
355 return "Rotation failure on consumer";
356 case LTTNG_ERR_ROTATE_RENAME_FAIL_CONSUMER:
357 return "Rotation rename failure on consumer";
358 case LTTNG_ERR_ROTATION_PENDING_LOCAL_FAIL_CONSUMER:
359 return "Rotation pending check (local) failure on consumer";
360 case LTTNG_ERR_ROTATION_PENDING_RELAY_FAIL_CONSUMER:
361 return "Rotation pending check (relay) failure on consumer";
362 case LTTNG_ERR_MKDIR_FAIL_CONSUMER:
363 return "Directory creation failure on consumer";
364 case LTTNG_ERR_CHAN_NOT_FOUND:
365 return "Channel not found";
366 case LTTNG_ERR_SNAPSHOT_UNSUPPORTED:
367 return "Session configuration does not allow the use of snapshots";
368 case LTTNG_ERR_SESSION_NOT_EXIST:
369 return "Recording session does not exist";
370 case LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER:
371 return "Trace chunk creation failed on consumer";
372 case LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER:
373 return "Trace chunk close failed on consumer";
374 case LTTNG_ERR_TRACE_CHUNK_EXISTS_FAIL_CONSUMER:
375 return "Failed to query consumer for trace chunk existence";
376 case LTTNG_ERR_INVALID_PROTOCOL:
377 return "Protocol error occurred";
378 case LTTNG_ERR_FILE_CREATION_ERROR:
379 return "Failed to create file";
380 case LTTNG_ERR_TIMER_STOP_ERROR:
381 return "Failed to stop a timer";
382 case LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL:
383 return "Rotation feature not supported by the kernel tracer";
384 case LTTNG_ERR_CLEAR_RELAY_DISALLOWED:
385 return "Relayd daemon peer does not allow sessions to be cleared";
386 case LTTNG_ERR_CLEAR_NOT_AVAILABLE_RELAY:
387 return "Clearing a session is not supported by the relay daemon";
388 case LTTNG_ERR_CLEAR_FAIL_CONSUMER:
389 return "Consumer failed to clear the session";
390 case LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR:
391 return "Session was already cleared since it became inactive";
392 case LTTNG_ERR_USER_NOT_FOUND:
393 return "User not found";
394 case LTTNG_ERR_GROUP_NOT_FOUND:
395 return "Group not found";
396 case LTTNG_ERR_UNSUPPORTED_DOMAIN:
397 return "Unsupported domain used";
398 case LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY:
399 return "Operation does not apply to the process attribute tracker's tracking policy";
400 case LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD:
401 return "Failed to create an event notifier group notification file descriptor";
402 case LTTNG_ERR_INVALID_CAPTURE_EXPRESSION:
403 return "Invalid capture expression";
404 case LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION:
405 return "Failed to create event notifier";
406 case LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING:
407 return "Failed to initialize event notifier error accounting";
408 case LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL:
409 return "No index available in event notifier error accounting";
410 case LTTNG_ERR_INVALID_ERROR_QUERY_TARGET:
411 return "Invalid error query target.";
412 case LTTNG_ERR_BUFFER_FLUSH_FAILED:
413 return "Failed to flush stream buffer";
414 case LTTNG_ERR_NR:
415 abort();
416 }
417
418 abort();
419 };
420
421 /*
422 * Return ptr to string representing a human readable error code from the
423 * lttng_error_code enum.
424 *
425 * These code MUST be negative in other to treat that as an error value.
426 */
427 const char *error_get_str(int32_t code)
428 {
429 code = -code;
430
431 if (code < LTTNG_OK || code >= LTTNG_ERR_NR) {
432 code = LTTNG_ERR_UNK;
433 }
434
435 return lttng_error_code_str((lttng_error_code) code);
436 }
437
438 void lttng_abort_on_error(void)
439 {
440 if (lttng_opt_abort_on_error < 0) {
441 /* Use lttng_secure_getenv() to query its state. */
442 const char *value;
443
444 value = lttng_secure_getenv("LTTNG_ABORT_ON_ERROR");
445 if (value && !strcmp(value, "1")) {
446 lttng_opt_abort_on_error = 1;
447 } else {
448 lttng_opt_abort_on_error = 0;
449 }
450 }
451 if (lttng_opt_abort_on_error > 0) {
452 abort();
453 }
454 }
This page took 0.039465 seconds and 4 git commands to generate.