Commit | Line | Data |
---|---|---|
553c4bab PP |
1 | /* |
2 | * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #define BT_LOG_TAG "LIB/ERROR" | |
24 | #include "lib/logging.h" | |
25 | ||
26 | #include <stdlib.h> | |
27 | #include <stdint.h> | |
28 | #include <babeltrace2/error-const.h> | |
29 | #include <babeltrace2/error-cause-const.h> | |
30 | ||
31 | #include "error.h" | |
32 | #include "graph/message/iterator.h" | |
33 | #include "graph/component.h" | |
34 | #include "graph/component-class.h" | |
35 | #include "common/assert.h" | |
36 | #include "lib/assert-pre.h" | |
37 | #include "lib/func-status.h" | |
38 | ||
39 | #define BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(_cause, _exp_type) \ | |
40 | BT_ASSERT_PRE(((const struct bt_error_cause *) (_cause))->actor_type == _exp_type, \ | |
41 | "Unexpected error cause's actor type: type=%s, exp-type=%s", \ | |
42 | bt_error_cause_actor_type_string(((const struct bt_error_cause *) (_cause))->actor_type), \ | |
43 | bt_error_cause_actor_type_string(_exp_type)) | |
44 | ||
45 | static | |
46 | void fini_component_class_id( | |
47 | struct bt_error_cause_component_class_id *comp_class_id) | |
48 | { | |
49 | BT_ASSERT(comp_class_id); | |
50 | ||
51 | if (comp_class_id->name) { | |
52 | g_string_free(comp_class_id->name, TRUE); | |
53 | comp_class_id->name = NULL; | |
54 | } | |
55 | ||
56 | if (comp_class_id->plugin_name) { | |
57 | g_string_free(comp_class_id->plugin_name, TRUE); | |
58 | comp_class_id->plugin_name = NULL; | |
59 | } | |
60 | } | |
61 | ||
62 | static | |
63 | void fini_error_cause(struct bt_error_cause *cause) | |
64 | { | |
65 | BT_ASSERT(cause); | |
66 | BT_LIB_LOGD("Finalizing error cause: %!+r", cause); | |
67 | ||
68 | if (cause->module_name) { | |
69 | g_string_free(cause->module_name, TRUE); | |
70 | cause->module_name = NULL; | |
71 | } | |
72 | ||
73 | if (cause->file_name) { | |
74 | g_string_free(cause->file_name, TRUE); | |
75 | cause->file_name = NULL; | |
76 | } | |
77 | ||
78 | if (cause->message) { | |
79 | g_string_free(cause->message, TRUE); | |
80 | cause->message = NULL; | |
81 | } | |
82 | } | |
83 | ||
84 | static | |
85 | void destroy_error_cause(struct bt_error_cause *cause) | |
86 | { | |
87 | if (!cause) { | |
88 | goto end; | |
89 | } | |
90 | ||
91 | BT_LIB_LOGD("Destroying error cause: %!+r", cause); | |
92 | ||
93 | switch (cause->actor_type) { | |
94 | case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT: | |
95 | { | |
96 | struct bt_error_cause_component_actor *spec_cause = | |
97 | (void *) cause; | |
98 | ||
99 | if (spec_cause->comp_name) { | |
100 | g_string_free(spec_cause->comp_name, TRUE); | |
101 | spec_cause->comp_name = NULL; | |
102 | } | |
103 | ||
104 | fini_component_class_id(&spec_cause->comp_class_id); | |
105 | break; | |
106 | } | |
107 | case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS: | |
108 | { | |
109 | struct bt_error_cause_component_class_actor *spec_cause = | |
110 | (void *) cause; | |
111 | ||
112 | fini_component_class_id(&spec_cause->comp_class_id); | |
113 | break; | |
114 | } | |
115 | case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR: | |
116 | { | |
117 | struct bt_error_cause_message_iterator_actor *spec_cause = | |
118 | (void *) cause; | |
119 | ||
120 | if (spec_cause->comp_name) { | |
121 | g_string_free(spec_cause->comp_name, TRUE); | |
122 | spec_cause->comp_name = NULL; | |
123 | } | |
124 | ||
125 | if (spec_cause->output_port_name) { | |
126 | g_string_free(spec_cause->output_port_name, TRUE); | |
127 | spec_cause->output_port_name = NULL; | |
128 | } | |
129 | ||
130 | fini_component_class_id(&spec_cause->comp_class_id); | |
131 | break; | |
132 | } | |
133 | default: | |
134 | break; | |
135 | } | |
136 | ||
137 | fini_error_cause(cause); | |
138 | g_free(cause); | |
139 | ||
140 | end: | |
141 | return; | |
142 | } | |
143 | ||
144 | static | |
145 | int init_error_cause(struct bt_error_cause *cause, | |
146 | enum bt_error_cause_actor_type actor_type) | |
147 | { | |
148 | int ret = 0; | |
149 | ||
150 | BT_ASSERT(cause); | |
151 | BT_LIB_LOGD("Initializing error cause: %!+r", cause); | |
152 | cause->actor_type = actor_type; | |
153 | cause->module_name = g_string_new(NULL); | |
154 | if (!cause->module_name) { | |
155 | BT_LOGE_STR("Failed to allocate one GString."); | |
156 | ret = -1; | |
157 | goto end; | |
158 | } | |
159 | ||
160 | cause->message = g_string_new(NULL); | |
161 | if (!cause->message) { | |
162 | BT_LOGE_STR("Failed to allocate one GString."); | |
163 | ret = -1; | |
164 | goto end; | |
165 | } | |
166 | ||
167 | cause->file_name = g_string_new(NULL); | |
168 | if (!cause->file_name) { | |
169 | BT_LOGE_STR("Failed to allocate one GString."); | |
170 | ret = -1; | |
171 | goto end; | |
172 | } | |
173 | ||
174 | BT_LIB_LOGD("Initialized error cause: %!+r", cause); | |
175 | ||
176 | end: | |
177 | return ret; | |
178 | } | |
179 | ||
180 | static | |
181 | int init_component_class_id( | |
182 | struct bt_error_cause_component_class_id *comp_class_id, | |
183 | struct bt_component_class *comp_cls) | |
184 | { | |
185 | int ret = 0; | |
186 | ||
187 | BT_ASSERT(comp_class_id); | |
188 | comp_class_id->type = comp_cls->type; | |
189 | comp_class_id->name = g_string_new(comp_cls->name->str); | |
190 | if (!comp_class_id->name) { | |
191 | BT_LOGE_STR("Failed to allocate one GString."); | |
192 | ret = -1; | |
193 | goto end; | |
194 | } | |
195 | ||
196 | comp_class_id->plugin_name = g_string_new(comp_cls->plugin_name->str); | |
197 | if (!comp_class_id->plugin_name) { | |
198 | BT_LOGE_STR("Failed to allocate one GString."); | |
199 | ret = -1; | |
200 | goto end; | |
201 | } | |
202 | ||
203 | end: | |
204 | return ret; | |
205 | } | |
206 | ||
207 | static | |
208 | void set_error_cause_props(struct bt_error_cause *cause, | |
209 | const char *file_name, uint64_t line_no) | |
210 | { | |
211 | BT_ASSERT(cause); | |
212 | g_string_assign(cause->file_name, file_name); | |
213 | cause->line_no = line_no; | |
214 | } | |
215 | ||
216 | static | |
217 | struct bt_error_cause *create_error_cause(const char *module_name, | |
218 | const char *file_name, uint64_t line_no) | |
219 | { | |
220 | struct bt_error_cause *cause = g_new0(struct bt_error_cause, 1); | |
221 | int ret; | |
222 | ||
223 | BT_LOGD_STR("Creating error cause (unknown actor)."); | |
224 | ||
225 | if (!cause) { | |
226 | BT_LOGE_STR("Failed to allocate one error cause."); | |
227 | goto error; | |
228 | } | |
229 | ||
230 | ret = init_error_cause(cause, BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN); | |
231 | if (ret) { | |
232 | goto error; | |
233 | } | |
234 | ||
235 | g_string_assign(cause->module_name, module_name); | |
236 | set_error_cause_props(cause, file_name, line_no); | |
237 | BT_LIB_LOGD("Created error cause: %!+r", cause); | |
238 | goto end; | |
239 | ||
240 | error: | |
241 | destroy_error_cause(cause); | |
242 | cause = NULL; | |
243 | ||
244 | end: | |
245 | return cause; | |
246 | } | |
247 | ||
248 | static | |
249 | void append_component_class_id_str(GString *str, | |
250 | struct bt_error_cause_component_class_id *comp_class_id) | |
251 | { | |
252 | const char *type_str = NULL; | |
253 | ||
254 | switch (comp_class_id->type) { | |
255 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
256 | type_str = "src"; | |
257 | break; | |
258 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
259 | type_str = "flt"; | |
260 | break; | |
261 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
262 | type_str = "sink"; | |
263 | break; | |
264 | default: | |
265 | abort(); | |
266 | } | |
267 | ||
268 | if (comp_class_id->plugin_name->len > 0) { | |
269 | g_string_append_printf(str, "%s.%s.%s", | |
270 | type_str, comp_class_id->plugin_name->str, | |
271 | comp_class_id->name->str); | |
272 | } else { | |
273 | g_string_append_printf(str, "%s.%s", | |
274 | type_str, comp_class_id->name->str); | |
275 | } | |
276 | } | |
277 | ||
278 | static | |
279 | struct bt_error_cause_component_actor *create_error_cause_component_actor( | |
280 | struct bt_component *comp, const char *file_name, | |
281 | uint64_t line_no) | |
282 | { | |
283 | struct bt_error_cause_component_actor *cause = | |
284 | g_new0(struct bt_error_cause_component_actor, 1); | |
285 | int ret; | |
286 | ||
287 | BT_LOGD_STR("Creating error cause object (component actor)."); | |
288 | ||
289 | if (!cause) { | |
290 | goto error; | |
291 | } | |
292 | ||
293 | ret = init_error_cause(&cause->base, | |
294 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); | |
295 | if (ret) { | |
296 | goto error; | |
297 | } | |
298 | ||
299 | set_error_cause_props(&cause->base, file_name, line_no); | |
300 | cause->comp_name = g_string_new(comp->name->str); | |
301 | if (!cause->comp_name) { | |
302 | BT_LOGE_STR("Failed to allocate one GString."); | |
303 | goto error; | |
304 | } | |
305 | ||
306 | ret = init_component_class_id(&cause->comp_class_id, comp->class); | |
307 | if (ret) { | |
308 | goto error; | |
309 | } | |
310 | ||
311 | g_string_append_printf(cause->base.module_name, "%s: ", | |
312 | comp->name->str); | |
313 | append_component_class_id_str(cause->base.module_name, | |
314 | &cause->comp_class_id); | |
315 | BT_LIB_LOGD("Created error cause object: %!+r", cause); | |
316 | goto end; | |
317 | ||
318 | error: | |
319 | destroy_error_cause(&cause->base); | |
320 | cause = NULL; | |
321 | ||
322 | end: | |
323 | return cause; | |
324 | } | |
325 | ||
326 | static | |
327 | struct bt_error_cause_component_class_actor * | |
328 | create_error_cause_component_class_actor(struct bt_component_class *comp_cls, | |
329 | const char *file_name, uint64_t line_no) | |
330 | { | |
331 | struct bt_error_cause_component_class_actor *cause = | |
332 | g_new0(struct bt_error_cause_component_class_actor, 1); | |
333 | int ret; | |
334 | ||
335 | BT_LOGD_STR("Creating error cause object (component class actor)."); | |
336 | ||
337 | if (!cause) { | |
338 | BT_LOGE_STR("Failed to allocate one error cause object."); | |
339 | goto error; | |
340 | } | |
341 | ||
342 | ret = init_error_cause(&cause->base, | |
343 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS); | |
344 | if (ret) { | |
345 | goto error; | |
346 | } | |
347 | ||
348 | set_error_cause_props(&cause->base, file_name, line_no); | |
349 | ret = init_component_class_id(&cause->comp_class_id, comp_cls); | |
350 | if (ret) { | |
351 | goto error; | |
352 | } | |
353 | ||
354 | append_component_class_id_str(cause->base.module_name, | |
355 | &cause->comp_class_id); | |
356 | BT_LIB_LOGD("Created error cause object: %!+r", cause); | |
357 | goto end; | |
358 | ||
359 | error: | |
360 | destroy_error_cause(&cause->base); | |
361 | cause = NULL; | |
362 | ||
363 | end: | |
364 | return cause; | |
365 | } | |
366 | ||
367 | struct bt_error_cause_message_iterator_actor * | |
368 | create_error_cause_message_iterator_actor(struct bt_message_iterator *iter, | |
369 | const char *file_name, uint64_t line_no) | |
370 | { | |
371 | struct bt_error_cause_message_iterator_actor *cause; | |
372 | struct bt_self_component_port_input_message_iterator *input_port_iter; | |
373 | int ret; | |
374 | ||
375 | BT_LOGD_STR("Creating error cause object (message iterator actor)."); | |
376 | ||
377 | /* | |
378 | * This can only be created from within a graph, from a user | |
379 | * message iterator, which is a self component port input | |
380 | * message iterator. | |
381 | */ | |
382 | BT_ASSERT(iter->type == | |
383 | BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT); | |
384 | input_port_iter = (void *) iter; | |
385 | cause = g_new0(struct bt_error_cause_message_iterator_actor, 1); | |
386 | if (!cause) { | |
387 | BT_LOGE_STR("Failed to allocate one error cause object."); | |
388 | goto error; | |
389 | } | |
390 | ||
391 | ret = init_error_cause(&cause->base, | |
392 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); | |
393 | if (ret) { | |
394 | goto error; | |
395 | } | |
396 | ||
397 | set_error_cause_props(&cause->base, file_name, line_no); | |
398 | cause->comp_name = g_string_new( | |
399 | input_port_iter->upstream_component->name->str); | |
400 | if (!cause->comp_name) { | |
401 | BT_LOGE_STR("Failed to allocate one GString."); | |
402 | goto error; | |
403 | } | |
404 | ||
405 | cause->output_port_name = g_string_new( | |
406 | input_port_iter->upstream_port->name->str); | |
407 | if (!cause->output_port_name) { | |
408 | BT_LOGE_STR("Failed to allocate one GString."); | |
409 | goto error; | |
410 | } | |
411 | ||
412 | ret = init_component_class_id(&cause->comp_class_id, | |
413 | input_port_iter->upstream_component->class); | |
414 | if (ret) { | |
415 | goto error; | |
416 | } | |
417 | ||
418 | g_string_append_printf(cause->base.module_name, "%s (%s): ", | |
419 | input_port_iter->upstream_component->name->str, | |
420 | input_port_iter->upstream_port->name->str); | |
421 | append_component_class_id_str(cause->base.module_name, | |
422 | &cause->comp_class_id); | |
423 | BT_LIB_LOGD("Created error cause object: %!+r", cause); | |
424 | goto end; | |
425 | ||
426 | error: | |
427 | destroy_error_cause(&cause->base); | |
428 | cause = NULL; | |
429 | ||
430 | end: | |
431 | return cause; | |
432 | } | |
433 | ||
434 | BT_HIDDEN | |
435 | struct bt_error *bt_error_create(void) | |
436 | { | |
437 | struct bt_error *error; | |
438 | ||
439 | BT_LOGD_STR("Creating error object."); | |
440 | error = g_new0(struct bt_error, 1); | |
441 | if (!error) { | |
442 | BT_LOGE_STR("Failed to allocate one error object."); | |
443 | goto error; | |
444 | } | |
445 | ||
446 | error->causes = g_ptr_array_new_with_free_func( | |
447 | (GDestroyNotify) destroy_error_cause); | |
448 | if (!error->causes) { | |
449 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
450 | goto error; | |
451 | } | |
452 | ||
453 | BT_LOGD("Created error object: addr=%p", error); | |
454 | goto end; | |
455 | ||
456 | error: | |
457 | bt_error_destroy(error); | |
458 | error = NULL; | |
459 | ||
460 | end: | |
461 | return error; | |
462 | } | |
463 | ||
464 | BT_HIDDEN | |
465 | void bt_error_destroy(struct bt_error *error) | |
466 | { | |
467 | if (!error) { | |
468 | goto end; | |
469 | } | |
470 | ||
471 | if (error->causes) { | |
472 | g_ptr_array_free(error->causes, TRUE); | |
473 | error->causes = NULL; | |
474 | } | |
475 | ||
476 | g_free(error); | |
477 | ||
478 | end: | |
479 | return; | |
480 | } | |
481 | ||
482 | BT_HIDDEN | |
483 | int bt_error_append_cause_from_unknown(struct bt_error *error, | |
484 | const char *module_name, const char *file_name, | |
485 | uint64_t line_no, const char *msg_fmt, va_list args) | |
486 | { | |
487 | struct bt_error_cause *cause = NULL; | |
488 | int status = BT_FUNC_STATUS_OK; | |
489 | ||
490 | BT_ASSERT_PRE_NON_NULL(error, "Error"); | |
491 | BT_ASSERT_PRE_NON_NULL(module_name, "Module name"); | |
492 | BT_ASSERT_PRE_NON_NULL(file_name, "Function name"); | |
493 | BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string"); | |
494 | BT_LOGD("Appending error cause from unknown actor: " | |
495 | "module-name=\"%s\", func-name=\"%s\", line-no=%" PRIu64, | |
496 | module_name, file_name, line_no); | |
497 | cause = create_error_cause(module_name, file_name, line_no); | |
498 | if (!cause) { | |
499 | /* create_error_cause() logs errors */ | |
500 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
501 | goto end; | |
502 | } | |
503 | ||
504 | g_string_append_vprintf(cause->message, msg_fmt, args); | |
505 | g_ptr_array_add(error->causes, cause); | |
506 | BT_LIB_LOGD("Appended error cause: %!+r", cause); | |
507 | cause = NULL; | |
508 | ||
509 | end: | |
510 | destroy_error_cause(cause); | |
511 | return status; | |
512 | } | |
513 | ||
514 | BT_HIDDEN | |
515 | int bt_error_append_cause_from_component( | |
516 | struct bt_error *error, bt_self_component *self_comp, | |
517 | const char *file_name, uint64_t line_no, | |
518 | const char *msg_fmt, va_list args) | |
519 | { | |
520 | struct bt_error_cause_component_actor *cause = NULL; | |
521 | int status = BT_FUNC_STATUS_OK; | |
522 | ||
523 | BT_ASSERT_PRE_NON_NULL(error, "Error"); | |
524 | BT_ASSERT_PRE_NON_NULL(self_comp, "Component"); | |
525 | BT_ASSERT_PRE_NON_NULL(file_name, "Function name"); | |
526 | BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string"); | |
527 | BT_LIB_LOGD("Appending error cause from component actor: %![comp-]+c", | |
528 | self_comp); | |
529 | cause = create_error_cause_component_actor((void *) self_comp, | |
530 | file_name, line_no); | |
531 | if (!cause) { | |
532 | /* create_error_cause_component_actor() logs errors */ | |
533 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
534 | goto end; | |
535 | } | |
536 | ||
537 | g_string_append_vprintf(cause->base.message, msg_fmt, args); | |
538 | g_ptr_array_add(error->causes, cause); | |
539 | BT_LIB_LOGD("Appended error cause: %!+r", cause); | |
540 | cause = NULL; | |
541 | ||
542 | end: | |
543 | destroy_error_cause(&cause->base); | |
544 | return status; | |
545 | } | |
546 | ||
547 | BT_HIDDEN | |
548 | int bt_error_append_cause_from_component_class( | |
549 | struct bt_error *error, | |
550 | bt_self_component_class *self_comp_class, | |
551 | const char *file_name, uint64_t line_no, | |
552 | const char *msg_fmt, va_list args) | |
553 | { | |
554 | struct bt_error_cause_component_class_actor *cause = NULL; | |
555 | int status = BT_FUNC_STATUS_OK; | |
556 | ||
557 | BT_ASSERT_PRE_NON_NULL(error, "Error"); | |
558 | BT_ASSERT_PRE_NON_NULL(self_comp_class, "Component class"); | |
559 | BT_ASSERT_PRE_NON_NULL(file_name, "Function name"); | |
560 | BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string"); | |
561 | BT_LIB_LOGD("Appending error cause from component class actor: " | |
562 | "%![comp-cls-]+C", self_comp_class); | |
563 | cause = create_error_cause_component_class_actor( | |
564 | (void *) self_comp_class, file_name, line_no); | |
565 | if (!cause) { | |
566 | /* create_error_cause_component_class_actor() logs errors */ | |
567 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
568 | goto end; | |
569 | } | |
570 | ||
571 | g_string_append_vprintf(cause->base.message, msg_fmt, args); | |
572 | g_ptr_array_add(error->causes, cause); | |
573 | BT_LIB_LOGD("Appended error cause: %!+r", cause); | |
574 | cause = NULL; | |
575 | ||
576 | end: | |
577 | destroy_error_cause(&cause->base); | |
578 | return status; | |
579 | } | |
580 | ||
581 | BT_HIDDEN | |
582 | int bt_error_append_cause_from_message_iterator( | |
583 | struct bt_error *error, bt_self_message_iterator *self_iter, | |
584 | const char *file_name, uint64_t line_no, | |
585 | const char *msg_fmt, va_list args) | |
586 | { | |
587 | struct bt_error_cause_message_iterator_actor *cause = NULL; | |
588 | int status = BT_FUNC_STATUS_OK; | |
589 | ||
590 | BT_ASSERT_PRE_NON_NULL(error, "Error"); | |
591 | BT_ASSERT_PRE_NON_NULL(self_iter, "Message iterator"); | |
592 | BT_ASSERT_PRE_NON_NULL(file_name, "Function name"); | |
593 | BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string"); | |
594 | BT_LIB_LOGD("Appending error cause from message iterator actor: " | |
595 | "%![comp-]+i", self_iter); | |
596 | cause = create_error_cause_message_iterator_actor( | |
597 | (void *) self_iter, file_name, line_no); | |
598 | if (!cause) { | |
599 | /* create_error_cause_message_iterator_actor() logs errors */ | |
600 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
601 | goto end; | |
602 | } | |
603 | ||
604 | g_string_append_vprintf(cause->base.message, msg_fmt, args); | |
605 | g_ptr_array_add(error->causes, cause); | |
606 | BT_LIB_LOGD("Appended error cause: %!+r", cause); | |
607 | cause = NULL; | |
608 | ||
609 | end: | |
610 | destroy_error_cause(&cause->base); | |
611 | return status; | |
612 | } | |
613 | ||
614 | static | |
615 | uint64_t error_cause_count(const bt_error *error) | |
616 | { | |
617 | return error->causes ? error->causes->len : 0; | |
618 | } | |
619 | ||
620 | uint64_t bt_error_get_cause_count(const bt_error *error) | |
621 | { | |
622 | BT_ASSERT_PRE_NON_NULL(error, "Error"); | |
623 | return error_cause_count(error); | |
624 | } | |
625 | ||
626 | void bt_error_release(const struct bt_error *error) | |
627 | { | |
628 | BT_ASSERT_PRE_NON_NULL(error, "Error"); | |
629 | bt_error_destroy((void *) error); | |
630 | } | |
631 | ||
632 | const struct bt_error_cause *bt_error_borrow_cause_by_index( | |
633 | const bt_error *error, uint64_t index) | |
634 | { | |
635 | BT_ASSERT_PRE_NON_NULL(error, "Error"); | |
636 | BT_ASSERT_PRE_VALID_INDEX(index, error_cause_count(error)); | |
637 | return error->causes->pdata[index]; | |
638 | } | |
639 | ||
640 | enum bt_error_cause_actor_type bt_error_cause_get_actor_type( | |
641 | const struct bt_error_cause *cause) | |
642 | { | |
643 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
644 | return cause->actor_type; | |
645 | } | |
646 | ||
647 | const char *bt_error_cause_get_message(const struct bt_error_cause *cause) | |
648 | { | |
649 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
650 | return cause->message->str; | |
651 | } | |
652 | ||
653 | const char *bt_error_cause_get_module_name(const struct bt_error_cause *cause) | |
654 | { | |
655 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
656 | return cause->module_name->str; | |
657 | } | |
658 | ||
659 | const char *bt_error_cause_get_file_name(const struct bt_error_cause *cause) | |
660 | { | |
661 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
662 | return cause->file_name->str; | |
663 | } | |
664 | ||
665 | uint64_t bt_error_cause_get_line_number(const bt_error_cause *cause) | |
666 | { | |
667 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
668 | return cause->line_no; | |
669 | } | |
670 | ||
671 | const char *bt_error_cause_component_actor_get_component_name( | |
672 | const struct bt_error_cause *cause) | |
673 | { | |
674 | const struct bt_error_cause_component_actor *spec_cause = | |
675 | (const void *) cause; | |
676 | ||
677 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
678 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
679 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); | |
680 | return spec_cause->comp_name->str; | |
681 | } | |
682 | ||
683 | bt_component_class_type bt_error_cause_component_actor_get_component_class_type( | |
684 | const struct bt_error_cause *cause) | |
685 | { | |
686 | const struct bt_error_cause_component_actor *spec_cause = | |
687 | (const void *) cause; | |
688 | ||
689 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
690 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
691 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); | |
692 | return spec_cause->comp_class_id.type; | |
693 | } | |
694 | ||
695 | const char *bt_error_cause_component_actor_get_component_class_name( | |
696 | const struct bt_error_cause *cause) | |
697 | { | |
698 | const struct bt_error_cause_component_actor *spec_cause = | |
699 | (const void *) cause; | |
700 | ||
701 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
702 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
703 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); | |
704 | return spec_cause->comp_class_id.name->str; | |
705 | } | |
706 | ||
707 | const char *bt_error_cause_component_actor_get_plugin_name( | |
708 | const struct bt_error_cause *cause) | |
709 | { | |
710 | const struct bt_error_cause_component_actor *spec_cause = | |
711 | (const void *) cause; | |
712 | ||
713 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
714 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
715 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); | |
716 | return spec_cause->comp_class_id.plugin_name->len > 0 ? | |
717 | spec_cause->comp_class_id.plugin_name->str : NULL; | |
718 | } | |
719 | ||
720 | bt_component_class_type | |
721 | bt_error_cause_component_class_actor_get_component_class_type( | |
722 | const struct bt_error_cause *cause) | |
723 | { | |
724 | const struct bt_error_cause_component_class_actor *spec_cause = | |
725 | (const void *) cause; | |
726 | ||
727 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
728 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
729 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS); | |
730 | return spec_cause->comp_class_id.type; | |
731 | } | |
732 | ||
733 | const char *bt_error_cause_component_class_actor_get_component_class_name( | |
734 | const struct bt_error_cause *cause) | |
735 | { | |
736 | const struct bt_error_cause_component_class_actor *spec_cause = | |
737 | (const void *) cause; | |
738 | ||
739 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
740 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
741 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS); | |
742 | return spec_cause->comp_class_id.name->str; | |
743 | } | |
744 | ||
745 | const char *bt_error_cause_component_class_actor_get_plugin_name( | |
746 | const struct bt_error_cause *cause) | |
747 | { | |
748 | const struct bt_error_cause_component_class_actor *spec_cause = | |
749 | (const void *) cause; | |
750 | ||
751 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
752 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
753 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS); | |
754 | return spec_cause->comp_class_id.plugin_name->len > 0 ? | |
755 | spec_cause->comp_class_id.plugin_name->str : NULL; | |
756 | } | |
757 | ||
758 | const char *bt_error_cause_message_iterator_actor_get_component_name( | |
759 | const struct bt_error_cause *cause) | |
760 | { | |
761 | const struct bt_error_cause_message_iterator_actor *spec_cause = | |
762 | (const void *) cause; | |
763 | ||
764 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
765 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
766 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); | |
767 | return spec_cause->comp_name->str; | |
768 | } | |
769 | ||
770 | const char * | |
771 | bt_error_cause_message_iterator_actor_get_component_output_port_name( | |
772 | const struct bt_error_cause *cause) | |
773 | { | |
774 | const struct bt_error_cause_message_iterator_actor *spec_cause = | |
775 | (const void *) cause; | |
776 | ||
777 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
778 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
779 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); | |
780 | return spec_cause->output_port_name->str; | |
781 | } | |
782 | ||
783 | bt_component_class_type | |
784 | bt_error_cause_message_iterator_actor_get_component_class_type( | |
785 | const struct bt_error_cause *cause) | |
786 | { | |
787 | const struct bt_error_cause_message_iterator_actor *spec_cause = | |
788 | (const void *) cause; | |
789 | ||
790 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
791 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
792 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); | |
793 | return spec_cause->comp_class_id.type; | |
794 | } | |
795 | ||
796 | const char *bt_error_cause_message_iterator_actor_get_component_class_name( | |
797 | const struct bt_error_cause *cause) | |
798 | { | |
799 | const struct bt_error_cause_message_iterator_actor *spec_cause = | |
800 | (const void *) cause; | |
801 | ||
802 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
803 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
804 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); | |
805 | return spec_cause->comp_class_id.name->str; | |
806 | } | |
807 | ||
808 | const char *bt_error_cause_message_iterator_actor_get_plugin_name( | |
809 | const struct bt_error_cause *cause) | |
810 | { | |
811 | const struct bt_error_cause_message_iterator_actor *spec_cause = | |
812 | (const void *) cause; | |
813 | ||
814 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); | |
815 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, | |
816 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); | |
817 | return spec_cause->comp_class_id.plugin_name->len > 0 ? | |
818 | spec_cause->comp_class_id.plugin_name->str : NULL; | |
819 | } |