2 * Copyright (C) 2016-2019 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #if WITH_AFB_HOOK && WITH_AFB_TRACE
31 #include <json-c/json.h>
32 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
33 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
36 #define AFB_BINDING_VERSION 3
37 #include <afb/afb-binding.h>
40 #include "afb-hook-flags.h"
42 #include "afb-session.h"
44 #include "afb-export.h"
46 #include "afb-session.h"
47 #include "afb-trace.h"
49 #include "wrap-json.h"
52 /*******************************************************************************/
53 /***** default names *****/
54 /*******************************************************************************/
56 #if !defined(DEFAULT_EVENT_NAME)
57 # define DEFAULT_EVENT_NAME "trace"
59 #if !defined(DEFAULT_TAG_NAME)
60 # define DEFAULT_TAG_NAME "trace"
63 /*******************************************************************************/
65 /*******************************************************************************/
69 struct tag *next; /* link to the next */
70 char tag[]; /* name of the tag */
73 /* struct for events */
75 struct event *next; /* link to the next event */
76 struct afb_evtid *evtid; /* the event */
79 /* struct for sessions */
81 struct afb_session *session; /* the session */
82 struct afb_trace *trace; /* the tracer */
85 /* struct for recording hooks */
87 struct hook *next; /* link to next hook */
88 void *handler; /* the handler of the hook */
89 struct event *event; /* the associated event */
90 struct tag *tag; /* the associated tag */
91 struct afb_session *session; /* the associated session */
97 Trace_Type_Xreq, /* xreq hooks */
98 Trace_Type_Api, /* api hooks */
99 Trace_Type_Evt, /* evt hooks */
100 Trace_Type_Session, /* session hooks */
101 Trace_Type_Global, /* global hooks */
102 #if !defined(REMOVE_LEGACY_TRACE)
103 Trace_Legacy_Type_Ditf, /* export hooks */
104 Trace_Legacy_Type_Svc, /* export hooks */
106 Trace_Type_Count, /* count of types of hooks */
112 int refcount; /* reference count */
113 pthread_mutex_t mutex; /* concurrency management */
114 const char *apiname; /* api name for events */
115 struct afb_session *bound; /* bound to session */
116 struct event *events; /* list of events */
117 struct tag *tags; /* list of tags */
118 struct hook *hooks[Trace_Type_Count]; /* hooks */
121 /*******************************************************************************/
122 /***** utility functions *****/
123 /*******************************************************************************/
125 static void ctxt_error(char **errors, const char *format, ...)
133 va_start(ap, format);
134 len = vsnprintf(buffer, sizeof buffer, format, ap);
136 if (len > (int)(sizeof buffer - 2))
137 len = (int)(sizeof buffer - 2);
138 buffer[len++] = '\n';
142 sz = errs ? strlen(errs) : 0;
143 errs = realloc(errs, sz + (size_t)len);
145 memcpy(errs + sz, buffer, len);
151 static struct json_object *timestamp(const struct afb_hookid *hookid)
153 return json_object_new_double((double)hookid->time.tv_sec +
154 (double)hookid->time.tv_nsec * .000000001);
157 /* verbosity level name or NULL */
158 static const char *verbosity_level_name(int level)
160 static const char *names[] = {
168 return level >= Log_Level_Error && level <= Log_Level_Debug ? names[level - Log_Level_Error] : NULL;
172 static void emit(void *closure, const struct afb_hookid *hookid, const char *type, const char *fmt1, const char *fmt2, va_list ap2, ...)
174 struct hook *hook = closure;
175 struct json_object *data, *data1, *data2;
178 data1 = data2 = data = NULL;
180 wrap_json_vpack(&data1, fmt1, ap1);
183 wrap_json_vpack(&data2, fmt2, ap2);
185 wrap_json_pack(&data, "{so ss ss si so so*}",
186 "time", timestamp(hookid),
187 "tag", hook->tag->tag,
189 "id", (int)(hookid->id & INT_MAX),
193 afb_evt_evtid_push(hook->event->evtid, data);
196 /*******************************************************************************/
197 /***** trace the requests *****/
198 /*******************************************************************************/
200 static void hook_xreq(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *action, const char *format, ...)
202 struct json_object *cred = NULL;
203 const char *session = NULL;
206 if (xreq->context.session)
207 session = afb_session_uuid(xreq->context.session);
210 wrap_json_pack(&cred, "{si ss si si ss* ss*}",
211 "uid", (int)xreq->cred->uid,
212 "user", xreq->cred->user,
213 "gid", (int)xreq->cred->gid,
214 "pid", (int)xreq->cred->pid,
215 "label", xreq->cred->label,
218 va_start(ap, format);
219 emit(closure, hookid, "request", "{si ss ss ss so* ss*}", format, ap,
220 "index", xreq->hookindex,
221 "api", xreq->request.called_api,
222 "verb", xreq->request.called_verb,
229 static void hook_xreq_begin(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
231 hook_xreq(closure, hookid, xreq, "begin", "{sO?}",
232 "json", afb_xreq_unhooked_json((struct afb_xreq*)xreq));
235 static void hook_xreq_end(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
237 hook_xreq(closure, hookid, xreq, "end", NULL);
240 static void hook_xreq_json(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
242 hook_xreq(closure, hookid, xreq, "json", "{sO?}",
246 static void hook_xreq_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
248 hook_xreq(closure, hookid, xreq, "get", "{ss? ss? ss? ss?}",
255 static void hook_xreq_reply(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
257 hook_xreq(closure, hookid, xreq, "reply", "{sO? ss? ss?}",
263 static void hook_xreq_context_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
265 hook_xreq(closure, hookid, xreq, "context_get", NULL);
268 static void hook_xreq_context_set(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
270 hook_xreq(closure, hookid, xreq, "context_set", NULL);
273 static void hook_xreq_addref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
275 hook_xreq(closure, hookid, xreq, "addref", NULL);
278 static void hook_xreq_unref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
280 hook_xreq(closure, hookid, xreq, "unref", NULL);
283 static void hook_xreq_session_close(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
285 hook_xreq(closure, hookid, xreq, "session_close", NULL);
288 static void hook_xreq_session_set_LOA(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
290 hook_xreq(closure, hookid, xreq, "session_set_LOA", "{si si}",
295 static void hook_xreq_subscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event, int result)
297 hook_xreq(closure, hookid, xreq, "subscribe", "{s{ss si} si}",
299 "name", afb_evt_event_x2_fullname(event),
300 "id", afb_evt_event_x2_id(event),
304 static void hook_xreq_unsubscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event, int result)
306 hook_xreq(closure, hookid, xreq, "unsubscribe", "{s{ss? si} si}",
308 "name", afb_evt_event_x2_fullname(event),
309 "id", afb_evt_event_x2_id(event),
313 static void hook_xreq_subcall(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
315 hook_xreq(closure, hookid, xreq, "subcall", "{ss? ss? sO?}",
321 static void hook_xreq_subcall_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
323 hook_xreq(closure, hookid, xreq, "subcall_result", "{sO? ss? ss?}",
329 static void hook_xreq_subcallsync(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
331 hook_xreq(closure, hookid, xreq, "subcallsync", "{ss? ss? sO?}",
337 static void hook_xreq_subcallsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
339 hook_xreq(closure, hookid, xreq, "subcallsync_result", "{si sO? ss? ss?}",
346 static void hook_xreq_vverbose(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
348 struct json_object *pos;
357 len = vasprintf(&msg, fmt, ap);
361 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", func);
363 hook_xreq(closure, hookid, xreq, "vverbose", "{si ss* ss? so*}",
365 "type", verbosity_level_name(level),
366 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
372 static void hook_xreq_store(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
374 hook_xreq(closure, hookid, xreq, "store", NULL);
377 static void hook_xreq_unstore(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
379 hook_xreq(closure, hookid, xreq, "unstore", NULL);
382 static void hook_xreq_has_permission(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
384 hook_xreq(closure, hookid, xreq, "has_permission", "{ss sb}",
385 "permission", permission,
389 static void hook_xreq_get_application_id(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
391 hook_xreq(closure, hookid, xreq, "get_application_id", "{ss?}",
395 static void hook_xreq_context_make(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure, void *result)
397 char pc[50], pf[50], pv[50], pr[50];
398 snprintf(pc, sizeof pc, "%p", create_value);
399 snprintf(pf, sizeof pf, "%p", free_value);
400 snprintf(pv, sizeof pv, "%p", create_closure);
401 snprintf(pr, sizeof pr, "%p", result);
402 hook_xreq(closure, hookid, xreq, "context_make", "{sb ss ss ss ss}",
410 static void hook_xreq_get_uid(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
412 hook_xreq(closure, hookid, xreq, "get_uid", "{si}",
416 static void hook_xreq_get_client_info(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *result)
418 hook_xreq(closure, hookid, xreq, "get_client_info", "{sO}",
422 static struct afb_hook_xreq_itf hook_xreq_itf = {
423 .hook_xreq_begin = hook_xreq_begin,
424 .hook_xreq_end = hook_xreq_end,
425 .hook_xreq_json = hook_xreq_json,
426 .hook_xreq_get = hook_xreq_get,
427 .hook_xreq_reply = hook_xreq_reply,
428 .hook_xreq_legacy_context_get = hook_xreq_context_get,
429 .hook_xreq_legacy_context_set = hook_xreq_context_set,
430 .hook_xreq_addref = hook_xreq_addref,
431 .hook_xreq_unref = hook_xreq_unref,
432 .hook_xreq_session_close = hook_xreq_session_close,
433 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA,
434 .hook_xreq_subscribe = hook_xreq_subscribe,
435 .hook_xreq_unsubscribe = hook_xreq_unsubscribe,
436 .hook_xreq_subcall = hook_xreq_subcall,
437 .hook_xreq_subcall_result = hook_xreq_subcall_result,
438 .hook_xreq_subcallsync = hook_xreq_subcallsync,
439 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result,
440 .hook_xreq_vverbose = hook_xreq_vverbose,
441 .hook_xreq_legacy_store = hook_xreq_store,
442 .hook_xreq_legacy_unstore = hook_xreq_unstore,
443 .hook_xreq_has_permission = hook_xreq_has_permission,
444 .hook_xreq_get_application_id = hook_xreq_get_application_id,
445 .hook_xreq_context_make = hook_xreq_context_make,
446 .hook_xreq_get_uid = hook_xreq_get_uid,
447 .hook_xreq_get_client_info = hook_xreq_get_client_info,
450 /*******************************************************************************/
451 /***** trace the api interface *****/
452 /*******************************************************************************/
454 static void hook_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
458 va_start(ap, format);
459 emit(closure, hookid, "api", "{ss ss}", format, ap,
460 "api", afb_export_apiname(export),
465 static void hook_api_event_broadcast_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
467 hook_api(closure, hookid, export, "event_broadcast_before", "{ss sO?}",
468 "name", name, "data", object);
471 static void hook_api_event_broadcast_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object, int result)
473 hook_api(closure, hookid, export, "event_broadcast_after", "{ss sO? si}",
474 "name", name, "data", object, "result", result);
477 static void hook_api_get_event_loop(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
479 hook_api(closure, hookid, export, "get_event_loop", NULL);
482 static void hook_api_get_user_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
484 hook_api(closure, hookid, export, "get_user_bus", NULL);
487 static void hook_api_get_system_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
489 hook_api(closure, hookid, export, "get_system_bus", NULL);
492 static void hook_api_vverbose(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
494 struct json_object *pos;
503 len = vasprintf(&msg, fmt, ap);
507 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
509 hook_api(closure, hookid, export, "vverbose", "{si ss* ss? so*}",
511 "type", verbosity_level_name(level),
512 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
518 static void hook_api_event_make(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_event_x2 *result)
520 hook_api(closure, hookid, export, "event_make", "{ss ss si}",
521 "name", name, "event", afb_evt_event_x2_fullname(result), "id", afb_evt_event_x2_id(result));
524 static void hook_api_rootdir_get_fd(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
526 char path[PATH_MAX], proc[100];
527 const char *key, *val;
531 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
532 s = readlink(proc, path, sizeof path);
533 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
538 val = strerror(errno);
541 hook_api(closure, hookid, export, "rootdir_get_fd", "{ss}", key, val);
544 static void hook_api_rootdir_open_locale(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
546 char path[PATH_MAX], proc[100];
547 const char *key, *val;
551 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
552 s = readlink(proc, path, sizeof path);
553 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
558 val = strerror(errno);
561 hook_api(closure, hookid, export, "rootdir_open_locale", "{ss si ss* ss}",
568 static void hook_api_queue_job(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
570 hook_api(closure, hookid, export, "queue_job", "{ss}", "result", result);
573 static void hook_api_unstore_req(void * closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
575 hook_api(closure, hookid, export, "unstore_req", NULL);
578 static void hook_api_require_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
580 hook_api(closure, hookid, export, "require_api", "{ss sb}", "name", name, "initialized", initialized);
583 static void hook_api_require_api_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
585 hook_api(closure, hookid, export, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
588 static void hook_api_add_alias_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *alias, int result)
590 hook_api(closure, hookid, export, "add_alias", "{si ss? ss}", "status", result, "api", api, "alias", alias);
593 static void hook_api_start_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
595 hook_api(closure, hookid, export, "start_before", NULL);
598 static void hook_api_start_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
600 hook_api(closure, hookid, export, "start_after", "{si}", "result", status);
603 static void hook_api_on_event_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int evtid, struct json_object *object)
605 hook_api(closure, hookid, export, "on_event_before", "{ss si sO*}",
606 "event", event, "id", evtid, "data", object);
609 static void hook_api_on_event_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int evtid, struct json_object *object)
611 hook_api(closure, hookid, export, "on_event_after", "{ss si sO?}",
612 "event", event, "id", evtid, "data", object);
615 static void hook_api_call(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
617 hook_api(closure, hookid, export, "call", "{ss ss sO?}",
618 "api", api, "verb", verb, "args", args);
621 static void hook_api_call_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object, const char *error, const char *info)
623 hook_api(closure, hookid, export, "call_result", "{sO? ss? ss?}",
624 "object", object, "error", error, "info", info);
627 static void hook_api_callsync(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
629 hook_api(closure, hookid, export, "callsync", "{ss ss sO?}",
630 "api", api, "verb", verb, "args", args);
633 static void hook_api_callsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
635 hook_api(closure, hookid, export, "callsync_result", "{si sO? ss? ss?}",
636 "status", status, "object", object, "error", error, "info", info);
639 static void hook_api_new_api_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *info, int noconcurrency)
641 hook_api(closure, hookid, export, "new_api.before", "{ss ss? sb}",
642 "api", api, "info", info, "noconcurrency", noconcurrency);
645 static void hook_api_new_api_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *api)
647 hook_api(closure, hookid, export, "new_api.after", "{si ss}",
648 "status", result, "api", api);
651 static void hook_api_api_set_verbs_v2(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
653 hook_api(closure, hookid, export, "set_verbs_v2", "{si}", "status", result);
656 static void hook_api_api_set_verbs_v3(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
658 hook_api(closure, hookid, export, "set_verbs_v3", "{si}", "status", result);
662 static void hook_api_api_add_verb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb, const char *info, int glob)
664 hook_api(closure, hookid, export, "add_verb", "{si ss ss? sb}", "status", result, "verb", verb, "info", info, "glob", glob);
667 static void hook_api_api_del_verb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb)
669 hook_api(closure, hookid, export, "del_verb", "{si ss}", "status", result, "verb", verb);
672 static void hook_api_api_set_on_event(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
674 hook_api(closure, hookid, export, "set_on_event", "{si}", "status", result);
677 static void hook_api_api_set_on_init(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
679 hook_api(closure, hookid, export, "set_on_init", "{si}", "status", result);
682 static void hook_api_api_seal(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
684 hook_api(closure, hookid, export, "seal", NULL);
687 static void hook_api_event_handler_add(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
689 hook_api(closure, hookid, export, "event_handler_add", "{si ss?}", "status", result, "pattern", pattern);
692 static void hook_api_event_handler_del(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
694 hook_api(closure, hookid, export, "event_handler_del", "{si ss?}", "status", result, "pattern", pattern);
697 static void hook_api_class_provide(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
699 hook_api(closure, hookid, export, "class_provide", "{si ss?}", "status", result, "name", name);
702 static void hook_api_class_require(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
704 hook_api(closure, hookid, export, "class_require", "{si ss?}", "status", result, "name", name);
707 static void hook_api_delete_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
709 hook_api(closure, hookid, export, "delete_api", "{si}", "status", result);
712 static void hook_api_on_event_handler_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
714 hook_api(closure, hookid, export, "on_event_handler.before",
715 "{ss ss sO?}", "pattern", pattern, "event", event, "data", object);
718 static void hook_api_on_event_handler_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
720 hook_api(closure, hookid, export, "on_event_handler.after",
721 "{ss ss sO?}", "pattern", pattern, "event", event, "data", object);
724 static void hook_api_settings(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object)
726 hook_api(closure, hookid, export, "settings", "{sO}", "settings", object);
729 static struct afb_hook_api_itf hook_api_itf = {
730 .hook_api_event_broadcast_before = hook_api_event_broadcast_before,
731 .hook_api_event_broadcast_after = hook_api_event_broadcast_after,
732 .hook_api_get_event_loop = hook_api_get_event_loop,
733 .hook_api_get_user_bus = hook_api_get_user_bus,
734 .hook_api_get_system_bus = hook_api_get_system_bus,
735 .hook_api_vverbose = hook_api_vverbose,
736 .hook_api_event_make = hook_api_event_make,
737 .hook_api_rootdir_get_fd = hook_api_rootdir_get_fd,
738 .hook_api_rootdir_open_locale = hook_api_rootdir_open_locale,
739 .hook_api_queue_job = hook_api_queue_job,
740 .hook_api_legacy_unstore_req = hook_api_unstore_req,
741 .hook_api_require_api = hook_api_require_api,
742 .hook_api_require_api_result = hook_api_require_api_result,
743 .hook_api_add_alias = hook_api_add_alias_cb,
744 .hook_api_start_before = hook_api_start_before,
745 .hook_api_start_after = hook_api_start_after,
746 .hook_api_on_event_before = hook_api_on_event_before,
747 .hook_api_on_event_after = hook_api_on_event_after,
748 .hook_api_call = hook_api_call,
749 .hook_api_call_result = hook_api_call_result,
750 .hook_api_callsync = hook_api_callsync,
751 .hook_api_callsync_result = hook_api_callsync_result,
752 .hook_api_new_api_before = hook_api_new_api_before,
753 .hook_api_new_api_after = hook_api_new_api_after,
754 .hook_api_api_set_verbs_v2 = hook_api_api_set_verbs_v2,
755 .hook_api_api_set_verbs_v3 = hook_api_api_set_verbs_v3,
756 .hook_api_api_add_verb = hook_api_api_add_verb,
757 .hook_api_api_del_verb = hook_api_api_del_verb,
758 .hook_api_api_set_on_event = hook_api_api_set_on_event,
759 .hook_api_api_set_on_init = hook_api_api_set_on_init,
760 .hook_api_api_seal = hook_api_api_seal,
761 .hook_api_event_handler_add = hook_api_event_handler_add,
762 .hook_api_event_handler_del = hook_api_event_handler_del,
763 .hook_api_class_provide = hook_api_class_provide,
764 .hook_api_class_require = hook_api_class_require,
765 .hook_api_delete_api = hook_api_delete_api,
766 .hook_api_on_event_handler_before = hook_api_on_event_handler_before,
767 .hook_api_on_event_handler_after = hook_api_on_event_handler_after,
768 .hook_api_settings = hook_api_settings,
771 /*******************************************************************************/
772 /***** trace the events *****/
773 /*******************************************************************************/
775 static void hook_evt(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *action, const char *format, ...)
779 va_start(ap, format);
780 emit(closure, hookid, "event", "{si ss ss}", format, ap,
787 static void hook_evt_create(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
789 hook_evt(closure, hookid, evt, id, "create", NULL);
792 static void hook_evt_push_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
794 hook_evt(closure, hookid, evt, id, "push_before", "{sO*}", "data", obj);
798 static void hook_evt_push_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
800 hook_evt(closure, hookid, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
803 static void hook_evt_broadcast_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
805 hook_evt(closure, hookid, evt, id, "broadcast_before", "{sO*}", "data", obj);
808 static void hook_evt_broadcast_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
810 hook_evt(closure, hookid, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
813 static void hook_evt_name(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
815 hook_evt(closure, hookid, evt, id, "name", "{ss}", "result", result);
818 static void hook_evt_addref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
820 hook_evt(closure, hookid, evt, id, "addref", NULL);
823 static void hook_evt_unref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
825 hook_evt(closure, hookid, evt, id, "unref", NULL);
828 static struct afb_hook_evt_itf hook_evt_itf = {
829 .hook_evt_create = hook_evt_create,
830 .hook_evt_push_before = hook_evt_push_before,
831 .hook_evt_push_after = hook_evt_push_after,
832 .hook_evt_broadcast_before = hook_evt_broadcast_before,
833 .hook_evt_broadcast_after = hook_evt_broadcast_after,
834 .hook_evt_name = hook_evt_name,
835 .hook_evt_addref = hook_evt_addref,
836 .hook_evt_unref = hook_evt_unref
839 /*******************************************************************************/
840 /***** trace the sessions *****/
841 /*******************************************************************************/
843 static void hook_session(void *closure, const struct afb_hookid *hookid, struct afb_session *session, const char *action, const char *format, ...)
847 va_start(ap, format);
848 emit(closure, hookid, "session", "{ss ss}", format, ap,
849 "uuid", afb_session_uuid(session),
854 static void hook_session_create(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
856 hook_session(closure, hookid, session, "create", "{ss}", "token", afb_session_token(session));
859 static void hook_session_close(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
861 hook_session(closure, hookid, session, "close", NULL);
864 static void hook_session_destroy(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
866 hook_session(closure, hookid, session, "destroy", NULL);
869 static void hook_session_renew(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
871 hook_session(closure, hookid, session, "renew", "{ss}", "token", afb_session_token(session));
874 static void hook_session_addref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
876 hook_session(closure, hookid, session, "addref", NULL);
879 static void hook_session_unref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
881 hook_session(closure, hookid, session, "unref", NULL);
884 static struct afb_hook_session_itf hook_session_itf = {
885 .hook_session_create = hook_session_create,
886 .hook_session_close = hook_session_close,
887 .hook_session_destroy = hook_session_destroy,
888 .hook_session_renew = hook_session_renew,
889 .hook_session_addref = hook_session_addref,
890 .hook_session_unref = hook_session_unref
893 /*******************************************************************************/
894 /***** trace the globals *****/
895 /*******************************************************************************/
897 static void hook_global(void *closure, const struct afb_hookid *hookid, const char *action, const char *format, ...)
901 va_start(ap, format);
902 emit(closure, hookid, "global", "{ss}", format, ap, "action", action);
906 static void hook_global_vverbose(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
908 struct json_object *pos;
917 len = vasprintf(&msg, fmt, ap);
921 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
923 hook_global(closure, hookid, "vverbose", "{si ss* ss? so*}",
925 "type", verbosity_level_name(level),
926 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
932 static struct afb_hook_global_itf hook_global_itf = {
933 .hook_global_vverbose = hook_global_vverbose,
936 /*******************************************************************************/
937 /***** abstract types *****/
938 /*******************************************************************************/
944 void (*unref)(void*);
945 int (*get_flag)(const char*);
947 abstracting[Trace_Type_Count] =
952 .unref = (void(*)(void*))afb_hook_unref_xreq,
953 .get_flag = afb_hook_flags_xreq_from_text
958 .unref = (void(*)(void*))afb_hook_unref_api,
959 .get_flag = afb_hook_flags_api_from_text
964 .unref = (void(*)(void*))afb_hook_unref_evt,
965 .get_flag = afb_hook_flags_evt_from_text
967 [Trace_Type_Session] =
970 .unref = (void(*)(void*))afb_hook_unref_session,
971 .get_flag = afb_hook_flags_session_from_text
973 [Trace_Type_Global] =
976 .unref = (void(*)(void*))afb_hook_unref_global,
977 .get_flag = afb_hook_flags_global_from_text
979 #if !defined(REMOVE_LEGACY_TRACE)
980 [Trace_Legacy_Type_Ditf] =
983 .unref = (void(*)(void*))afb_hook_unref_api,
984 .get_flag = afb_hook_flags_legacy_ditf_from_text
986 [Trace_Legacy_Type_Svc] =
989 .unref = (void(*)(void*))afb_hook_unref_api,
990 .get_flag = afb_hook_flags_legacy_svc_from_text
995 /*******************************************************************************/
996 /***** handle trace data *****/
997 /*******************************************************************************/
999 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
1000 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct afb_session *session)
1003 struct hook *hook, **prev;
1005 /* remove any event */
1006 for (i = 0 ; i < Trace_Type_Count ; i++) {
1007 prev = &trace->hooks[i];
1008 while ((hook = *prev)) {
1009 if ((tag && tag != hook->tag)
1010 || (event && event != hook->event)
1011 || (session && session != hook->session))
1015 abstracting[i].unref(hook->handler);
1022 /* cleanup: removes unused tags, events and sessions of the 'trace' */
1023 static void trace_cleanup(struct afb_trace *trace)
1027 struct tag *tag, **ptag;
1028 struct event *event, **pevent;
1031 ptag = &trace->tags;
1032 while ((tag = *ptag)) {
1033 /* search for tag */
1034 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1035 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
1036 /* keep or free whether used or not */
1045 pevent = &trace->events;
1046 while ((event = *pevent)) {
1047 /* search for event */
1048 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1049 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
1050 /* keep or free whether used or not */
1052 pevent = &event->next;
1054 *pevent = event->next;
1055 afb_evt_evtid_unref(event->evtid);
1062 * Get the tag of 'name' within 'trace'.
1063 * If 'alloc' isn't zero, create the tag and add it.
1065 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
1069 /* search the tag of 'name' */
1071 while (tag && strcmp(name, tag->tag))
1074 if (!tag && alloc) {
1075 /* creation if needed */
1076 tag = malloc(sizeof * tag + 1 + strlen(name));
1078 strcpy(tag->tag, name);
1079 tag->next = trace->tags;
1087 * Get the event of 'name' within 'trace'.
1088 * If 'alloc' isn't zero, create the event and add it.
1090 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
1092 struct event *event;
1094 /* search the event */
1095 event = trace->events;
1096 while (event && strcmp(afb_evt_evtid_name(event->evtid), name))
1097 event = event->next;
1099 if (!event && alloc) {
1100 event = malloc(sizeof * event);
1102 event->evtid = afb_evt_evtid_create2(trace->apiname, name);
1104 event->next = trace->events;
1105 trace->events = event;
1116 * called on session closing
1118 static void session_closed(void *item)
1120 struct cookie *cookie = item;
1122 pthread_mutex_lock(&cookie->trace->mutex);
1123 trace_unhook(cookie->trace, NULL, NULL, cookie->session);
1124 pthread_mutex_unlock(&cookie->trace->mutex);
1129 * records the cookie of session for tracking close
1131 static void *session_open(void *closure)
1133 struct cookie *param = closure, *cookie;
1134 cookie = malloc(sizeof *cookie);
1141 * Get the session of 'uuid' within 'trace'.
1142 * If 'alloc' isn't zero, create the session and add it.
1144 static struct afb_session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1146 struct cookie cookie;
1149 cookie.session = afb_session_search(uuid);
1151 cookie.session = afb_session_get(uuid, AFB_SESSION_TIMEOUT_DEFAULT, NULL);
1152 if (cookie.session) {
1153 cookie.trace = trace;
1154 afb_session_cookie(cookie.session, cookie.trace, session_open, session_closed, &cookie, 0);
1157 return cookie.session;
1160 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1164 tag = tag ?: DEFAULT_TAG_NAME;
1165 event = event ?: DEFAULT_EVENT_NAME;
1166 hook = malloc(sizeof *hook);
1168 hook->tag = trace_get_tag(trace, tag, 1);
1169 hook->event = trace_get_event(trace, event, 1);
1170 hook->session = NULL;
1171 hook->handler = NULL;
1176 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1178 hook->next = trace->hooks[type];
1179 trace->hooks[type] = hook;
1182 /*******************************************************************************/
1183 /***** handle client requests *****/
1184 /*******************************************************************************/
1188 struct afb_trace *trace;
1195 struct context *context;
1199 const char *apiname;
1200 const char *verbname;
1201 const char *pattern;
1202 int flags[Trace_Type_Count];
1205 static void addhook(struct desc *desc, enum trace_type type)
1208 struct afb_session *session;
1209 struct afb_session *bind;
1210 struct afb_trace *trace = desc->context->trace;
1212 /* check permission for bound traces */
1213 bind = trace->bound;
1215 if (type != Trace_Type_Xreq) {
1216 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1220 ctxt_error(&desc->context->errors, "setting session is forbidden");
1225 /* allocate the hook */
1226 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1228 ctxt_error(&desc->context->errors, "allocation of hook failed");
1232 /* create the hook handler */
1234 case Trace_Type_Xreq:
1236 session = afb_session_addref(bind);
1238 session = trace_get_session_by_uuid(trace, desc->uuid, 1);
1240 ctxt_error(&desc->context->errors, "allocation of session failed");
1245 hook->handler = afb_hook_create_xreq(desc->apiname, desc->verbname, session,
1246 desc->flags[type], &hook_xreq_itf, hook);
1247 afb_session_unref(session);
1249 case Trace_Type_Api:
1250 hook->handler = afb_hook_create_api(desc->apiname, desc->flags[type], &hook_api_itf, hook);
1252 case Trace_Type_Evt:
1253 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1255 case Trace_Type_Session:
1256 hook->handler = afb_hook_create_session(desc->uuid, desc->flags[type], &hook_session_itf, hook);
1258 case Trace_Type_Global:
1259 hook->handler = afb_hook_create_global(desc->flags[type], &hook_global_itf, hook);
1264 if (!hook->handler) {
1265 ctxt_error(&desc->context->errors, "creation of hook failed");
1270 /* attach and activate the hook */
1271 afb_req_subscribe(desc->context->req, afb_evt_event_x2_from_evtid(hook->event->evtid));
1272 trace_attach_hook(trace, hook, type);
1275 static void addhooks(struct desc *desc)
1279 #if !defined(REMOVE_LEGACY_TRACE)
1280 desc->flags[Trace_Type_Api] |= desc->flags[Trace_Legacy_Type_Ditf] | desc->flags[Trace_Legacy_Type_Svc];
1281 desc->flags[Trace_Legacy_Type_Ditf] = desc->flags[Trace_Legacy_Type_Svc] = 0;
1284 for (i = 0 ; i < Trace_Type_Count ; i++) {
1290 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1293 const char *name, *queried;
1294 struct desc *desc = closure;
1296 if (wrap_json_unpack(object, "s", &name))
1297 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1298 abstracting[type].name,
1299 json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1301 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1302 value = abstracting[type].get_flag(queried);
1304 desc->flags[type] |= value;
1306 ctxt_error(&desc->context->errors, "unknown %s name %s",
1307 abstracting[type].name, name);
1311 static void add_xreq_flags(void *closure, struct json_object *object)
1313 add_flags(closure, object, Trace_Type_Xreq);
1316 #if !defined(REMOVE_LEGACY_TRACE)
1317 static void legacy_add_ditf_flags(void *closure, struct json_object *object)
1319 add_flags(closure, object, Trace_Legacy_Type_Ditf);
1322 static void legacy_add_svc_flags(void *closure, struct json_object *object)
1324 add_flags(closure, object, Trace_Legacy_Type_Svc);
1328 static void add_api_flags(void *closure, struct json_object *object)
1330 add_flags(closure, object, Trace_Type_Api);
1333 static void add_evt_flags(void *closure, struct json_object *object)
1335 add_flags(closure, object, Trace_Type_Evt);
1338 static void add_session_flags(void *closure, struct json_object *object)
1340 add_flags(closure, object, Trace_Type_Session);
1343 static void add_global_flags(void *closure, struct json_object *object)
1345 add_flags(closure, object, Trace_Type_Global);
1349 static void add(void *closure, struct json_object *object)
1353 struct json_object *request, *event, *sub, *global, *session, *api;
1354 #if !defined(REMOVE_LEGACY_TRACE)
1355 struct json_object *daemon, *service;
1358 memcpy (&desc, closure, sizeof desc);
1359 request = event = sub = global = session = api = NULL;
1360 #if !defined(REMOVE_LEGACY_TRACE)
1361 daemon = service = NULL;
1364 rc = wrap_json_unpack(object, "{s?s s?s s?s s?s s?s s?s s?o s?o s?o s?o s?o s?o s?o}",
1367 "apiname", &desc.apiname,
1368 "verbname", &desc.verbname,
1370 "pattern", &desc.pattern,
1372 "request", &request,
1373 #if !defined(REMOVE_LEGACY_TRACE)
1375 "service", &service,
1378 "session", &session,
1384 if (desc.apiname && desc.apiname[0] == '*' && !desc.apiname[1])
1385 desc.apiname = NULL;
1387 if (desc.verbname && desc.verbname[0] == '*' && !desc.verbname[1])
1388 desc.verbname = NULL;
1390 if (desc.uuid && desc.uuid[0] == '*' && !desc.uuid[1])
1393 /* get what is expected */
1395 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1398 wrap_json_optarray_for_all(api, add_api_flags, &desc);
1400 #if !defined(REMOVE_LEGACY_TRACE)
1402 wrap_json_optarray_for_all(daemon, legacy_add_ditf_flags, &desc);
1405 wrap_json_optarray_for_all(service, legacy_add_svc_flags, &desc);
1409 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1412 wrap_json_optarray_for_all(session, add_session_flags, &desc);
1415 wrap_json_optarray_for_all(global, add_global_flags, &desc);
1419 wrap_json_optarray_for_all(sub, add, &desc);
1424 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1429 /* drop hooks of given tag */
1430 static void drop_tag(void *closure, struct json_object *object)
1433 struct context *context = closure;
1437 rc = wrap_json_unpack(object, "s", &name);
1439 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1441 tag = trace_get_tag(context->trace, name, 0);
1443 ctxt_error(&context->errors, "tag %s not found", name);
1445 trace_unhook(context->trace, tag, NULL, NULL);
1449 /* drop hooks of given event */
1450 static void drop_event(void *closure, struct json_object *object)
1453 struct context *context = closure;
1454 struct event *event;
1457 rc = wrap_json_unpack(object, "s", &name);
1459 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1461 event = trace_get_event(context->trace, name, 0);
1463 ctxt_error(&context->errors, "event %s not found", name);
1465 trace_unhook(context->trace, NULL, event, NULL);
1469 /* drop hooks of given session */
1470 static void drop_session(void *closure, struct json_object *object)
1473 struct context *context = closure;
1474 struct afb_session *session;
1477 rc = wrap_json_unpack(object, "s", &uuid);
1479 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1481 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1483 ctxt_error(&context->errors, "session %s not found", uuid);
1485 trace_unhook(context->trace, NULL, NULL, session);
1486 afb_session_unref(session);
1491 /*******************************************************************************/
1492 /***** public interface *****/
1493 /*******************************************************************************/
1495 /* allocates an afb_trace instance */
1496 struct afb_trace *afb_trace_create(const char *apiname, struct afb_session *bound)
1498 struct afb_trace *trace;
1502 trace = calloc(1, sizeof *trace);
1504 trace->refcount = 1;
1505 trace->bound = bound;
1506 trace->apiname = apiname;
1507 pthread_mutex_init(&trace->mutex, NULL);
1512 /* add a reference to the trace */
1513 void afb_trace_addref(struct afb_trace *trace)
1515 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1518 /* drop one reference to the trace */
1519 void afb_trace_unref(struct afb_trace *trace)
1521 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1523 trace_unhook(trace, NULL, NULL, NULL);
1524 trace_cleanup(trace);
1525 pthread_mutex_destroy(&trace->mutex);
1531 int afb_trace_add(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1533 struct context context;
1536 memset(&context, 0, sizeof context);
1537 context.trace = trace;
1540 memset(&desc, 0, sizeof desc);
1541 desc.context = &context;
1543 pthread_mutex_lock(&trace->mutex);
1544 wrap_json_optarray_for_all(args, add, &desc);
1545 pthread_mutex_unlock(&trace->mutex);
1547 if (!context.errors)
1550 afb_req_fail(req, "error-detected", context.errors);
1551 free(context.errors);
1556 int afb_trace_drop(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1559 struct context context;
1560 struct json_object *tags, *events, *uuids;
1562 memset(&context, 0, sizeof context);
1563 context.trace = trace;
1566 /* special: boolean value */
1567 if (!wrap_json_unpack(args, "b", &rc)) {
1569 pthread_mutex_lock(&trace->mutex);
1570 trace_unhook(trace, NULL, NULL, NULL);
1571 trace_cleanup(trace);
1572 pthread_mutex_unlock(&trace->mutex);
1577 tags = events = uuids = NULL;
1578 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1583 if (rc < 0 || !(events || tags || uuids)) {
1584 afb_req_fail(req, "error-detected", "bad drop arguments");
1588 pthread_mutex_lock(&trace->mutex);
1591 wrap_json_optarray_for_all(tags, drop_tag, &context);
1594 wrap_json_optarray_for_all(events, drop_event, &context);
1597 wrap_json_optarray_for_all(uuids, drop_session, &context);
1599 trace_cleanup(trace);
1601 pthread_mutex_unlock(&trace->mutex);
1603 if (!context.errors)
1606 afb_req_fail(req, "error-detected", context.errors);
1607 free(context.errors);