2 * Copyright (C) 2016, 2017 "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.
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
30 #include <json-c/json.h>
31 #include <afb/afb-binding-v2.h>
35 #include "afb-session.h"
40 #include "afb-trace.h"
42 #include "wrap-json.h"
45 /*******************************************************************************/
46 /***** default names *****/
47 /*******************************************************************************/
49 #if !defined(DEFAULT_EVENT_NAME)
50 # define DEFAULT_EVENT_NAME "trace"
52 #if !defined(DEFAULT_TAG_NAME)
53 # define DEFAULT_TAG_NAME "trace"
56 /*******************************************************************************/
58 /*******************************************************************************/
60 /* structure for searching flags by names */
63 const char *name; /** the name */
64 int value; /** the value */
69 struct tag *next; /* link to the next */
70 char tag[1]; /* name of the tag */
73 /* struct for events */
75 struct event *next; /* link to the next event */
76 struct afb_event event; /* the event */
79 /* struct for sessions */
81 struct session *next; /* link to the next session */
82 struct afb_session *session; /* the session */
83 struct afb_trace *trace; /* the tracer */
86 /* struct for recording hooks */
88 struct hook *next; /* link to next hook */
89 void *handler; /* the handler of the hook */
90 struct event *event; /* the associated event */
91 struct tag *tag; /* the associated tag */
92 struct session *session; /* the associated session */
98 Trace_Type_Xreq, /* xreq hooks */
99 Trace_Type_Ditf, /* ditf hooks */
100 Trace_Type_Svc, /* svc hooks */
101 Trace_Type_Evt, /* evt hooks */
102 Trace_Type_Global, /* global hooks */
103 Trace_Type_Count /* count of types of hooks */
109 int refcount; /* reference count */
110 pthread_mutex_t mutex; /* concurrency management */
111 struct afb_daemon *daemon; /* daemon */
112 struct afb_session *bound; /* bound to session */
113 struct event *events; /* list of events */
114 struct tag *tags; /* list of tags */
115 struct session *sessions; /* list of tags */
116 struct hook *hooks[Trace_Type_Count]; /* hooks */
119 /*******************************************************************************/
120 /***** utility functions *****/
121 /*******************************************************************************/
123 static void ctxt_error(char **errors, const char *format, ...)
131 va_start(ap, format);
132 len = vsnprintf(buffer, sizeof buffer, format, ap);
134 if (len > (int)(sizeof buffer - 2))
135 len = (int)(sizeof buffer - 2);
136 buffer[len++] = '\n';
140 sz = errs ? strlen(errs) : 0;
141 errs = realloc(errs, sz + (size_t)len);
143 memcpy(errs + sz, buffer, len);
148 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
149 static int get_flag(const char *name, struct flag flags[], int count)
151 /* dichotomic search */
152 int lower = 0, upper = count;
153 while (lower < upper) {
154 int mid = (lower + upper) >> 1;
155 int cmp = strcmp(name, flags[mid].name);
157 return flags[mid].value;
167 static struct json_object *timestamp(const struct afb_hookid *hookid)
171 snprintf(ts, sizeof ts, "%llu.%06lu", (long long unsigned)hookid->time.tv_sec, (long unsigned)(hookid->time.tv_nsec / 1000));
172 return json_object_new_string(ts);
175 /* verbosity level name or NULL */
176 static const char *verbosity_level_name(int level)
178 static const char *names[] = {
186 return level >= Log_Level_Error && level <= Log_Level_Debug ? names[level - Log_Level_Error] : NULL;
190 static void emit(void *closure, const struct afb_hookid *hookid, const char *type, const char *fmt1, const char *fmt2, va_list ap2, ...)
192 struct hook *hook = closure;
193 struct json_object *event, *data1, *data2;
196 data1 = data2 = event = NULL;
198 wrap_json_vpack(&data1, fmt1, ap1);
201 wrap_json_vpack(&data2, fmt2, ap2);
203 wrap_json_pack(&event, "{so ss ss si so so*}",
204 "time", timestamp(hookid),
205 "tag", hook->tag->tag,
207 "id", (int)(hookid->id & INT_MAX),
211 afb_evt_unhooked_push(hook->event->event, event);
214 /*******************************************************************************/
215 /***** trace the requests *****/
216 /*******************************************************************************/
218 static struct flag xreq_flags[] = { /* must be sorted by names */
219 { "addref", afb_hook_flag_req_addref },
220 { "all", afb_hook_flags_req_all },
221 { "args", afb_hook_flags_req_args },
222 { "begin", afb_hook_flag_req_begin },
223 { "common", afb_hook_flags_req_common },
224 { "context", afb_hook_flags_req_context },
225 { "context_get", afb_hook_flag_req_context_get },
226 { "context_set", afb_hook_flag_req_context_set },
227 { "end", afb_hook_flag_req_end },
228 { "event", afb_hook_flags_req_event },
229 { "extra", afb_hook_flags_req_extra },
230 { "fail", afb_hook_flag_req_fail },
231 { "get", afb_hook_flag_req_get },
232 { "json", afb_hook_flag_req_json },
233 { "life", afb_hook_flags_req_life },
234 { "ref", afb_hook_flags_req_ref },
235 { "result", afb_hook_flags_req_result },
236 { "session", afb_hook_flags_req_session },
237 { "session_close", afb_hook_flag_req_session_close },
238 { "session_set_LOA", afb_hook_flag_req_session_set_LOA },
239 { "store", afb_hook_flag_req_store },
240 { "stores", afb_hook_flags_req_stores },
241 { "subcall", afb_hook_flag_req_subcall },
242 { "subcall_req", afb_hook_flag_req_subcall_req },
243 { "subcall_req_result", afb_hook_flag_req_subcall_req_result },
244 { "subcall_result", afb_hook_flag_req_subcall_result },
245 { "subcalls", afb_hook_flags_req_subcalls },
246 { "subcallsync", afb_hook_flag_req_subcallsync },
247 { "subcallsync_result", afb_hook_flag_req_subcallsync_result },
248 { "subscribe", afb_hook_flag_req_subscribe },
249 { "success", afb_hook_flag_req_success },
250 { "unref", afb_hook_flag_req_unref },
251 { "unstore", afb_hook_flag_req_unstore },
252 { "unsubscribe", afb_hook_flag_req_unsubscribe },
253 { "vverbose", afb_hook_flag_req_vverbose },
256 /* get the xreq value for flag of 'name' */
257 static int get_xreq_flag(const char *name)
259 return get_flag(name, xreq_flags, (int)(sizeof xreq_flags / sizeof *xreq_flags));
262 static void hook_xreq(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *action, const char *format, ...)
264 struct json_object *cred = NULL;
265 const char *session = NULL;
268 if (xreq->context.session)
269 session = afb_session_uuid(xreq->context.session);
272 wrap_json_pack(&cred, "{si ss si si ss* ss*}",
273 "uid", (int)xreq->cred->uid,
274 "user", xreq->cred->user,
275 "gid", (int)xreq->cred->gid,
276 "pid", (int)xreq->cred->pid,
277 "label", xreq->cred->label,
280 va_start(ap, format);
281 emit(closure, hookid, "request", "{si ss ss ss so* ss*}", format, ap,
282 "index", xreq->hookindex,
291 static void hook_xreq_begin(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
293 hook_xreq(closure, hookid, xreq, "begin", NULL);
296 static void hook_xreq_end(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
298 hook_xreq(closure, hookid, xreq, "end", NULL);
301 static void hook_xreq_json(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
303 hook_xreq(closure, hookid, xreq, "json", "{sO?}",
307 static void hook_xreq_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
309 hook_xreq(closure, hookid, xreq, "get", "{ss? ss? ss? ss?}",
316 static void hook_xreq_success(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
318 hook_xreq(closure, hookid, xreq, "success", "{sO? ss?}",
323 static void hook_xreq_fail(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *status, const char *info)
325 hook_xreq(closure, hookid, xreq, "fail", "{ss? ss?}",
330 static void hook_xreq_context_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
332 hook_xreq(closure, hookid, xreq, "context_get", NULL);
335 static void hook_xreq_context_set(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
337 hook_xreq(closure, hookid, xreq, "context_set", NULL);
340 static void hook_xreq_addref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
342 hook_xreq(closure, hookid, xreq, "addref", NULL);
345 static void hook_xreq_unref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
347 hook_xreq(closure, hookid, xreq, "unref", NULL);
350 static void hook_xreq_session_close(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
352 hook_xreq(closure, hookid, xreq, "session_close", NULL);
355 static void hook_xreq_session_set_LOA(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
357 hook_xreq(closure, hookid, xreq, "session_set_LOA", "{si si}",
362 static void hook_xreq_subscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
364 hook_xreq(closure, hookid, xreq, "subscribe", "{s{ss si} si}",
366 "name", afb_evt_event_name(event),
367 "id", afb_evt_event_id(event),
371 static void hook_xreq_unsubscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
373 hook_xreq(closure, hookid, xreq, "unsubscribe", "{s{ss? si} si}",
375 "name", afb_evt_event_name(event),
376 "id", afb_evt_event_id(event),
380 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)
382 hook_xreq(closure, hookid, xreq, "subcall", "{ss? ss? sO?}",
388 static void hook_xreq_subcall_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
390 hook_xreq(closure, hookid, xreq, "subcall_result", "{si sO?}",
395 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)
397 hook_xreq(closure, hookid, xreq, "subcallsync", "{ss? ss? sO?}",
403 static void hook_xreq_subcallsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
405 hook_xreq(closure, hookid, xreq, "subcallsync_result", "{si sO?}",
410 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)
412 struct json_object *pos;
421 len = vasprintf(&msg, fmt, ap);
425 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", func);
427 hook_xreq(closure, hookid, xreq, "vverbose", "{si ss* ss? so*}",
429 "type", verbosity_level_name(level),
430 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
436 static void hook_xreq_store(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
438 hook_xreq(closure, hookid, xreq, "store", NULL);
441 static void hook_xreq_unstore(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
443 hook_xreq(closure, hookid, xreq, "unstore", NULL);
446 static void hook_xreq_subcall_req(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
448 hook_xreq(closure, hookid, xreq, "subcall_req", "{ss? ss? sO?}",
454 static void hook_xreq_subcall_req_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
456 hook_xreq(closure, hookid, xreq, "subcall_req_result", "{si sO?}",
461 static struct afb_hook_xreq_itf hook_xreq_itf = {
462 .hook_xreq_begin = hook_xreq_begin,
463 .hook_xreq_end = hook_xreq_end,
464 .hook_xreq_json = hook_xreq_json,
465 .hook_xreq_get = hook_xreq_get,
466 .hook_xreq_success = hook_xreq_success,
467 .hook_xreq_fail = hook_xreq_fail,
468 .hook_xreq_context_get = hook_xreq_context_get,
469 .hook_xreq_context_set = hook_xreq_context_set,
470 .hook_xreq_addref = hook_xreq_addref,
471 .hook_xreq_unref = hook_xreq_unref,
472 .hook_xreq_session_close = hook_xreq_session_close,
473 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA,
474 .hook_xreq_subscribe = hook_xreq_subscribe,
475 .hook_xreq_unsubscribe = hook_xreq_unsubscribe,
476 .hook_xreq_subcall = hook_xreq_subcall,
477 .hook_xreq_subcall_result = hook_xreq_subcall_result,
478 .hook_xreq_subcallsync = hook_xreq_subcallsync,
479 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result,
480 .hook_xreq_vverbose = hook_xreq_vverbose,
481 .hook_xreq_store = hook_xreq_store,
482 .hook_xreq_unstore = hook_xreq_unstore,
483 .hook_xreq_subcall_req = hook_xreq_subcall_req,
484 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result
487 /*******************************************************************************/
488 /***** trace the daemon interface *****/
489 /*******************************************************************************/
491 static struct flag ditf_flags[] = { /* must be sorted by names */
492 { "all", afb_hook_flags_ditf_all },
493 { "common", afb_hook_flags_ditf_common },
494 { "event_broadcast_after", afb_hook_flag_ditf_event_broadcast_after },
495 { "event_broadcast_before", afb_hook_flag_ditf_event_broadcast_before },
496 { "event_make", afb_hook_flag_ditf_event_make },
497 { "extra", afb_hook_flags_ditf_extra },
498 { "get_event_loop", afb_hook_flag_ditf_get_event_loop },
499 { "get_system_bus", afb_hook_flag_ditf_get_system_bus },
500 { "get_user_bus", afb_hook_flag_ditf_get_user_bus },
501 { "queue_job", afb_hook_flag_ditf_queue_job },
502 { "require_api", afb_hook_flag_ditf_require_api },
503 { "require_api_result", afb_hook_flag_ditf_require_api_result },
504 { "rootdir_get_fd", afb_hook_flag_ditf_rootdir_get_fd },
505 { "rootdir_open_locale", afb_hook_flag_ditf_rootdir_open_locale },
506 { "unstore_req", afb_hook_flag_ditf_unstore_req },
507 { "vverbose", afb_hook_flag_ditf_vverbose },
510 /* get the ditf value for flag of 'name' */
511 static int get_ditf_flag(const char *name)
513 return get_flag(name, ditf_flags, (int)(sizeof ditf_flags / sizeof *ditf_flags));
517 static void hook_ditf(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *action, const char *format, ...)
521 va_start(ap, format);
522 emit(closure, hookid, "daemon", "{ss ss}", format, ap,
528 static void hook_ditf_event_broadcast_before(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, struct json_object *object)
530 hook_ditf(closure, hookid, ditf, "event_broadcast_before", "{ss sO*}",
531 "name", name, "data", object);
534 static void hook_ditf_event_broadcast_after(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
536 hook_ditf(closure, hookid, ditf, "event_broadcast_after", "{ss sO* si}",
537 "name", name, "data", object, "result", result);
540 static void hook_ditf_get_event_loop(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, struct sd_event *result)
542 hook_ditf(closure, hookid, ditf, "get_event_loop", NULL);
545 static void hook_ditf_get_user_bus(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, struct sd_bus *result)
547 hook_ditf(closure, hookid, ditf, "get_user_bus", NULL);
550 static void hook_ditf_get_system_bus(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, struct sd_bus *result)
552 hook_ditf(closure, hookid, ditf, "get_system_bus", NULL);
555 static void hook_ditf_vverbose(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
557 struct json_object *pos;
566 len = vasprintf(&msg, fmt, ap);
570 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
572 hook_ditf(closure, hookid, ditf, "vverbose", "{si ss* ss? so*}",
574 "type", verbosity_level_name(level),
575 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
581 static void hook_ditf_event_make(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, struct afb_event result)
583 hook_ditf(closure, hookid, ditf, "event_make", "{ss ss si}",
584 "name", name, "event", afb_evt_event_name(result), "id", afb_evt_event_id(result));
587 static void hook_ditf_rootdir_get_fd(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, int result)
592 sprintf(path, "/proc/self/fd/%d", result);
593 readlink(path, path, sizeof path);
596 hook_ditf(closure, hookid, ditf, "rootdir_get_fd", "{ss}",
597 result < 0 ? "path" : "error",
598 result < 0 ? strerror(errno) : path);
601 static void hook_ditf_rootdir_open_locale(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
606 sprintf(path, "/proc/self/fd/%d", result);
607 readlink(path, path, sizeof path);
610 hook_ditf(closure, hookid, ditf, "rootdir_open_locale", "{ss si ss* ss}",
614 result < 0 ? "path" : "error",
615 result < 0 ? strerror(errno) : path);
618 static void hook_ditf_queue_job(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
620 hook_ditf(closure, hookid, ditf, "queue_job", "{ss}", "result", result);
623 static void hook_ditf_unstore_req(void * closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, struct afb_stored_req *sreq)
625 hook_ditf(closure, hookid, ditf, "unstore_req", NULL);
628 static void hook_ditf_require_api(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, int initialized)
630 hook_ditf(closure, hookid, ditf, "require_api", "{ss sb}", "name", name, "initialized", initialized);
633 static void hook_ditf_require_api_result(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, int initialized, int result)
635 hook_ditf(closure, hookid, ditf, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
638 static struct afb_hook_ditf_itf hook_ditf_itf = {
639 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before,
640 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after,
641 .hook_ditf_get_event_loop = hook_ditf_get_event_loop,
642 .hook_ditf_get_user_bus = hook_ditf_get_user_bus,
643 .hook_ditf_get_system_bus = hook_ditf_get_system_bus,
644 .hook_ditf_vverbose = hook_ditf_vverbose,
645 .hook_ditf_event_make = hook_ditf_event_make,
646 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd,
647 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale,
648 .hook_ditf_queue_job = hook_ditf_queue_job,
649 .hook_ditf_unstore_req = hook_ditf_unstore_req,
650 .hook_ditf_require_api = hook_ditf_require_api,
651 .hook_ditf_require_api_result = hook_ditf_require_api_result
654 /*******************************************************************************/
655 /***** trace the services *****/
656 /*******************************************************************************/
658 static struct flag svc_flags[] = { /* must be sorted by names */
659 { "all", afb_hook_flags_svc_all },
660 { "call", afb_hook_flag_svc_call },
661 { "call_result", afb_hook_flag_svc_call_result },
662 { "callsync", afb_hook_flag_svc_callsync },
663 { "callsync_result", afb_hook_flag_svc_callsync_result },
664 { "on_event_after", afb_hook_flag_svc_on_event_after },
665 { "on_event_before", afb_hook_flag_svc_on_event_before },
666 { "start_after", afb_hook_flag_svc_start_after },
667 { "start_before", afb_hook_flag_svc_start_before },
670 /* get the svc value for flag of 'name' */
671 static int get_svc_flag(const char *name)
673 return get_flag(name, svc_flags, (int)(sizeof svc_flags / sizeof *svc_flags));
676 static void hook_svc(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, const char *action, const char *format, ...)
680 va_start(ap, format);
681 emit(closure, hookid, "service", "{ss ss}", format, ap,
687 static void hook_svc_start_before(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc)
689 hook_svc(closure, hookid, svc, "start_before", NULL);
692 static void hook_svc_start_after(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, int status)
694 hook_svc(closure, hookid, svc, "start_after", "{si}", "result", status);
697 static void hook_svc_on_event_before(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
699 hook_svc(closure, hookid, svc, "on_event_before", "{ss si sO*}",
700 "event", event, "id", eventid, "data", object);
703 static void hook_svc_on_event_after(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
705 hook_svc(closure, hookid, svc, "on_event_after", "{ss si sO*}",
706 "event", event, "id", eventid, "data", object);
709 static void hook_svc_call(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
711 hook_svc(closure, hookid, svc, "call", "{ss ss sO*}",
712 "api", api, "verb", verb, "args", args);
715 static void hook_svc_call_result(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, int status, struct json_object *result)
717 hook_svc(closure, hookid, svc, "call_result", "{si sO*}",
718 "status", status, "result", result);
721 static void hook_svc_callsync(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
723 hook_svc(closure, hookid, svc, "callsync", "{ss ss sO*}",
724 "api", api, "verb", verb, "args", args);
727 static void hook_svc_callsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, int status, struct json_object *result)
729 hook_svc(closure, hookid, svc, "callsync_result", "{si sO*}",
730 "status", status, "result", result);
733 static struct afb_hook_svc_itf hook_svc_itf = {
734 .hook_svc_start_before = hook_svc_start_before,
735 .hook_svc_start_after = hook_svc_start_after,
736 .hook_svc_on_event_before = hook_svc_on_event_before,
737 .hook_svc_on_event_after = hook_svc_on_event_after,
738 .hook_svc_call = hook_svc_call,
739 .hook_svc_call_result = hook_svc_call_result,
740 .hook_svc_callsync = hook_svc_callsync,
741 .hook_svc_callsync_result = hook_svc_callsync_result
744 /*******************************************************************************/
745 /***** trace the events *****/
746 /*******************************************************************************/
748 static struct flag evt_flags[] = { /* must be sorted by names */
749 { "all", afb_hook_flags_evt_all },
750 { "broadcast_after", afb_hook_flag_evt_broadcast_after },
751 { "broadcast_before", afb_hook_flag_evt_broadcast_before },
752 { "common", afb_hook_flags_evt_common },
753 { "create", afb_hook_flag_evt_create },
754 { "drop", afb_hook_flag_evt_drop },
755 { "extra", afb_hook_flags_evt_extra },
756 { "name", afb_hook_flag_evt_name },
757 { "push_after", afb_hook_flag_evt_push_after },
758 { "push_before", afb_hook_flag_evt_push_before },
761 /* get the evt value for flag of 'name' */
762 static int get_evt_flag(const char *name)
764 return get_flag(name, evt_flags, (int)(sizeof evt_flags / sizeof *evt_flags));
767 static void hook_evt(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *action, const char *format, ...)
771 va_start(ap, format);
772 emit(closure, hookid, "event", "{si ss ss}", format, ap,
779 static void hook_evt_create(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
781 hook_evt(closure, hookid, evt, id, "create", NULL);
784 static void hook_evt_push_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
786 hook_evt(closure, hookid, evt, id, "push_before", "{sO*}", "data", obj);
790 static void hook_evt_push_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
792 hook_evt(closure, hookid, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
795 static void hook_evt_broadcast_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
797 hook_evt(closure, hookid, evt, id, "broadcast_before", "{sO*}", "data", obj);
800 static void hook_evt_broadcast_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
802 hook_evt(closure, hookid, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
805 static void hook_evt_name(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
807 hook_evt(closure, hookid, evt, id, "name", NULL);
810 static void hook_evt_drop(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
812 hook_evt(closure, hookid, evt, id, "drop", NULL);
815 static struct afb_hook_evt_itf hook_evt_itf = {
816 .hook_evt_create = hook_evt_create,
817 .hook_evt_push_before = hook_evt_push_before,
818 .hook_evt_push_after = hook_evt_push_after,
819 .hook_evt_broadcast_before = hook_evt_broadcast_before,
820 .hook_evt_broadcast_after = hook_evt_broadcast_after,
821 .hook_evt_name = hook_evt_name,
822 .hook_evt_drop = hook_evt_drop
825 /*******************************************************************************/
826 /***** trace the globals *****/
827 /*******************************************************************************/
829 static struct flag global_flags[] = { /* must be sorted by names */
830 { "all", afb_hook_flags_global_all },
831 { "vverbose", afb_hook_flag_global_vverbose },
834 /* get the global value for flag of 'name' */
835 static int get_global_flag(const char *name)
837 return get_flag(name, global_flags, (int)(sizeof global_flags / sizeof *global_flags));
840 static void hook_global(void *closure, const struct afb_hookid *hookid, const char *action, const char *format, ...)
844 va_start(ap, format);
845 emit(closure, hookid, "global", "{ss}", format, ap, "action", action);
849 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)
851 struct json_object *pos;
860 len = vasprintf(&msg, fmt, ap);
864 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
866 hook_global(closure, hookid, "vverbose", "{si ss* ss? so*}",
868 "type", verbosity_level_name(level),
869 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
875 static struct afb_hook_global_itf hook_global_itf = {
876 .hook_global_vverbose = hook_global_vverbose,
879 /*******************************************************************************/
880 /***** abstract types *****/
881 /*******************************************************************************/
887 void (*unref)(void*);
888 int (*get_flag)(const char*);
890 abstracting[Trace_Type_Count] =
895 .unref = (void(*)(void*))afb_hook_unref_xreq,
896 .get_flag = get_xreq_flag
901 .unref = (void(*)(void*))afb_hook_unref_ditf,
902 .get_flag = get_ditf_flag
907 .unref = (void(*)(void*))afb_hook_unref_svc,
908 .get_flag = get_svc_flag
913 .unref = (void(*)(void*))afb_hook_unref_evt,
914 .get_flag = get_evt_flag
916 [Trace_Type_Global] =
919 .unref = (void(*)(void*))afb_hook_unref_global,
920 .get_flag = get_global_flag
924 /*******************************************************************************/
925 /***** handle trace data *****/
926 /*******************************************************************************/
928 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
929 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct session *session)
932 struct hook *hook, **prev;
934 /* remove any event */
935 for (i = 0 ; i < Trace_Type_Count ; i++) {
936 prev = &trace->hooks[i];
937 while ((hook = *prev)) {
938 if ((tag && tag != hook->tag)
939 || (event && event != hook->event)
940 || (session && session != hook->session))
944 abstracting[i].unref(hook->handler);
951 /* cleanup: removes unused tags, events and sessions of the 'trace' */
952 static void trace_cleanup(struct afb_trace *trace)
956 struct tag *tag, **ptag;
957 struct event *event, **pevent;
958 struct session *session, **psession;
961 psession = &trace->sessions;
962 while ((session = *psession)) {
963 /* search for session */
964 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
965 for (hook = trace->hooks[i] ; hook && hook->session != session ; hook = hook->next);
966 /* keep or free whether used or not */
968 psession = &session->next;
970 *psession = session->next;
971 if (__atomic_exchange_n(&session->trace, NULL, __ATOMIC_RELAXED))
972 afb_session_set_cookie(session->session, session, NULL, NULL);
978 while ((tag = *ptag)) {
980 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
981 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
982 /* keep or free whether used or not */
991 pevent = &trace->events;
992 while ((event = *pevent)) {
993 /* search for event */
994 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
995 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
996 /* keep or free whether used or not */
998 pevent = &event->next;
1000 *pevent = event->next;
1001 afb_event_drop(event->event);
1007 /* callback at end of traced session */
1008 static void free_session_cookie(void *cookie)
1010 struct session *session = cookie;
1011 struct afb_trace *trace = __atomic_exchange_n(&session->trace, NULL, __ATOMIC_RELAXED);
1013 pthread_mutex_lock(&trace->mutex);
1014 trace_unhook(trace, NULL, NULL, session);
1015 trace_cleanup(trace);
1016 pthread_mutex_unlock(&trace->mutex);
1021 * Get the tag of 'name' within 'trace'.
1022 * If 'alloc' isn't zero, create the tag and add it.
1024 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
1028 /* search the tag of 'name' */
1030 while (tag && strcmp(name, tag->tag))
1033 if (!tag && alloc) {
1034 /* creation if needed */
1035 tag = malloc(sizeof * tag + strlen(name));
1037 strcpy(tag->tag, name);
1038 tag->next = trace->tags;
1046 * Get the event of 'name' within 'trace'.
1047 * If 'alloc' isn't zero, create the event and add it.
1049 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
1051 struct event *event;
1053 /* search the event */
1054 event = trace->events;
1055 while (event && strcmp(afb_event_name(event->event), name))
1056 event = event->next;
1058 if (!event && alloc) {
1059 event = malloc(sizeof * event);
1061 event->event = trace->daemon->itf->event_make(trace->daemon->closure, name);
1062 if (afb_event_is_valid(event->event)) {
1063 event->next = trace->events;
1064 trace->events = event;
1075 * Get the session of 'value' within 'trace'.
1076 * If 'alloc' isn't zero, create the session and add it.
1078 static struct session *trace_get_session(struct afb_trace *trace, struct afb_session *value, int alloc)
1080 struct session *session;
1082 /* search the session */
1083 session = trace->sessions;
1084 while (session && session->session != value)
1085 session = session->next;
1087 if (!session && alloc) {
1088 session = malloc(sizeof * session);
1090 session->session = value;
1091 session->trace = NULL;
1092 session->next = trace->sessions;
1093 trace->sessions = session;
1100 * Get the session of 'uuid' within 'trace'.
1101 * If 'alloc' isn't zero, create the session and add it.
1103 static struct session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1105 struct afb_session *session;
1108 session = afb_session_get(uuid, alloc ? &created : NULL);
1109 return session ? trace_get_session(trace, session, alloc) : NULL;
1112 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1116 tag = tag ?: DEFAULT_TAG_NAME;
1117 event = event ?: DEFAULT_EVENT_NAME;
1118 hook = malloc(sizeof *hook);
1120 hook->tag = trace_get_tag(trace, tag, 1);
1121 hook->event = trace_get_event(trace, event, 1);
1122 hook->session = NULL;
1123 hook->handler = NULL;
1128 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1130 struct session *session = hook->session;
1131 hook->next = trace->hooks[type];
1132 trace->hooks[type] = hook;
1133 if (session && !session->trace) {
1134 session->trace = trace;
1135 afb_session_set_cookie(session->session, session, session, free_session_cookie);
1139 /*******************************************************************************/
1140 /***** handle client requests *****/
1141 /*******************************************************************************/
1145 struct afb_trace *trace;
1152 struct context *context;
1155 const char *session;
1158 const char *pattern;
1159 int flags[Trace_Type_Count];
1163 static void addhook(struct desc *desc, enum trace_type type)
1166 struct session *session;
1167 struct afb_session *bind;
1168 struct afb_trace *trace = desc->context->trace;
1170 /* check permission for bound traces */
1171 bind = trace->bound;
1173 if (type != Trace_Type_Xreq) {
1174 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1177 if (desc->session) {
1178 ctxt_error(&desc->context->errors, "setting session is forbidden");
1183 /* allocate the hook */
1184 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1186 ctxt_error(&desc->context->errors, "allocation of hook failed");
1190 /* create the hook handler */
1192 case Trace_Type_Xreq:
1193 if (desc->session) {
1194 session = trace_get_session_by_uuid(trace, desc->session, 1);
1196 ctxt_error(&desc->context->errors, "allocation of session failed");
1200 bind = session->session;
1202 hook->handler = afb_hook_create_xreq(desc->api, desc->verb, bind,
1203 desc->flags[type], &hook_xreq_itf, hook);
1205 case Trace_Type_Ditf:
1206 hook->handler = afb_hook_create_ditf(desc->api, desc->flags[type], &hook_ditf_itf, hook);
1208 case Trace_Type_Svc:
1209 hook->handler = afb_hook_create_svc(desc->api, desc->flags[type], &hook_svc_itf, hook);
1211 case Trace_Type_Evt:
1212 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1214 case Trace_Type_Global:
1215 hook->handler = afb_hook_create_global(desc->flags[type], &hook_global_itf, hook);
1220 if (!hook->handler) {
1221 ctxt_error(&desc->context->errors, "creation of hook failed");
1226 /* attach and activate the hook */
1227 afb_req_subscribe(desc->context->req, hook->event->event);
1228 trace_attach_hook(trace, hook, type);
1231 static void addhooks(struct desc *desc)
1235 for (i = 0 ; i < Trace_Type_Count ; i++) {
1241 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1244 const char *name, *queried;
1245 struct desc *desc = closure;
1247 if (wrap_json_unpack(object, "s", &name))
1248 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1249 abstracting[type].name,
1250 json_object_to_json_string(object));
1252 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1253 value = abstracting[type].get_flag(queried);
1255 desc->flags[type] |= value;
1257 ctxt_error(&desc->context->errors, "unknown %s name %s",
1258 abstracting[type].name, name);
1262 static void add_xreq_flags(void *closure, struct json_object *object)
1264 add_flags(closure, object, Trace_Type_Xreq);
1267 static void add_ditf_flags(void *closure, struct json_object *object)
1269 add_flags(closure, object, Trace_Type_Ditf);
1272 static void add_svc_flags(void *closure, struct json_object *object)
1274 add_flags(closure, object, Trace_Type_Svc);
1277 static void add_evt_flags(void *closure, struct json_object *object)
1279 add_flags(closure, object, Trace_Type_Evt);
1282 static void add_global_flags(void *closure, struct json_object *object)
1284 add_flags(closure, object, Trace_Type_Global);
1288 static void add(void *closure, struct json_object *object)
1292 struct json_object *request, *event, *daemon, *service, *sub, *global;
1294 memcpy (&desc, closure, sizeof desc);
1295 request = event = daemon = service = sub = global = NULL;
1297 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}",
1302 "session", &desc.session,
1303 "pattern", &desc.pattern,
1304 "request", &request,
1306 "service", &service,
1313 if (desc.api && desc.api[0] == '*' && !desc.api[1])
1316 if (desc.verb && desc.verb[0] == '*' && !desc.verb[1])
1319 if (desc.session && desc.session[0] == '*' && !desc.session[1])
1320 desc.session = NULL;
1322 /* get what is expected */
1324 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1327 wrap_json_optarray_for_all(daemon, add_ditf_flags, &desc);
1330 wrap_json_optarray_for_all(service, add_svc_flags, &desc);
1333 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1336 wrap_json_optarray_for_all(global, add_global_flags, &desc);
1340 wrap_json_optarray_for_all(sub, add, &desc);
1345 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1350 /* drop hooks of given tag */
1351 static void drop_tag(void *closure, struct json_object *object)
1354 struct context *context = closure;
1358 rc = wrap_json_unpack(object, "s", &name);
1360 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string(object));
1362 tag = trace_get_tag(context->trace, name, 0);
1364 ctxt_error(&context->errors, "tag %s not found", name);
1366 trace_unhook(context->trace, tag, NULL, NULL);
1370 /* drop hooks of given event */
1371 static void drop_event(void *closure, struct json_object *object)
1374 struct context *context = closure;
1375 struct event *event;
1378 rc = wrap_json_unpack(object, "s", &name);
1380 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string(object));
1382 event = trace_get_event(context->trace, name, 0);
1384 ctxt_error(&context->errors, "event %s not found", name);
1386 trace_unhook(context->trace, NULL, event, NULL);
1390 /* drop hooks of given session */
1391 static void drop_session(void *closure, struct json_object *object)
1394 struct context *context = closure;
1395 struct session *session;
1398 rc = wrap_json_unpack(object, "s", &uuid);
1400 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string(object));
1402 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1404 ctxt_error(&context->errors, "session %s not found", uuid);
1406 trace_unhook(context->trace, NULL, NULL, session);
1410 /*******************************************************************************/
1411 /***** public interface *****/
1412 /*******************************************************************************/
1414 /* allocates an afb_trace instance */
1415 struct afb_trace *afb_trace_create(struct afb_daemon *daemon, struct afb_session *bound)
1417 struct afb_trace *trace;
1421 trace = calloc(1, sizeof *trace);
1423 trace->refcount = 1;
1424 trace->bound = bound;
1425 trace->daemon = daemon;
1426 pthread_mutex_init(&trace->mutex, NULL);
1431 /* add a reference to the trace */
1432 void afb_trace_addref(struct afb_trace *trace)
1434 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1437 /* drop one reference to the trace */
1438 void afb_trace_unref(struct afb_trace *trace)
1440 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1442 trace_unhook(trace, NULL, NULL, NULL);
1443 trace_cleanup(trace);
1444 pthread_mutex_destroy(&trace->mutex);
1450 int afb_trace_add(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1452 struct context context;
1455 memset(&context, 0, sizeof context);
1456 context.trace = trace;
1459 memset(&desc, 0, sizeof desc);
1460 desc.context = &context;
1462 pthread_mutex_lock(&trace->mutex);
1463 wrap_json_optarray_for_all(args, add, &desc);
1464 pthread_mutex_unlock(&trace->mutex);
1466 if (!context.errors)
1469 afb_req_fail(req, "error-detected", context.errors);
1470 free(context.errors);
1475 extern int afb_trace_drop(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1478 struct context context;
1479 struct json_object *tags, *events, *sessions;
1481 memset(&context, 0, sizeof context);
1482 context.trace = trace;
1485 /* special: boolean value */
1486 if (!wrap_json_unpack(args, "b", &rc)) {
1488 pthread_mutex_lock(&trace->mutex);
1489 trace_unhook(trace, NULL, NULL, NULL);
1490 trace_cleanup(trace);
1491 pthread_mutex_unlock(&trace->mutex);
1496 tags = events = sessions = NULL;
1497 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1500 "session", &sessions);
1502 if (rc < 0 || !(events || tags || sessions)) {
1503 afb_req_fail(req, "error-detected", "bad drop arguments");
1507 pthread_mutex_lock(&trace->mutex);
1510 wrap_json_optarray_for_all(tags, drop_tag, &context);
1513 wrap_json_optarray_for_all(events, drop_event, &context);
1516 wrap_json_optarray_for_all(sessions, drop_session, &context);
1518 trace_cleanup(trace);
1520 pthread_mutex_unlock(&trace->mutex);
1522 if (!context.errors)
1525 afb_req_fail(req, "error-detected", context.errors);
1526 free(context.errors);