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
31 #include <json-c/json.h>
32 #include <afb/afb-binding-v2.h>
36 #include "afb-session.h"
41 #include "afb-trace.h"
43 #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_Count /* count of types of hooks */
108 int refcount; /* reference count */
109 pthread_mutex_t mutex; /* concurrency management */
110 struct afb_daemon *daemon; /* daemon */
111 struct afb_session *bound; /* bound to session */
112 struct event *events; /* list of events */
113 struct tag *tags; /* list of tags */
114 struct session *sessions; /* list of tags */
115 struct hook *hooks[Trace_Type_Count]; /* hooks */
118 /*******************************************************************************/
119 /***** utility functions *****/
120 /*******************************************************************************/
122 static void ctxt_error(char **errors, const char *format, ...)
130 va_start(ap, format);
131 len = vsnprintf(buffer, sizeof buffer, format, ap);
133 if (len > (int)(sizeof buffer - 2))
134 len = (int)(sizeof buffer - 2);
135 buffer[len++] = '\n';
139 sz = errs ? strlen(errs) : 0;
140 errs = realloc(errs, sz + (size_t)len);
142 memcpy(errs + sz, buffer, len);
147 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
148 static int get_flag(const char *name, struct flag flags[], int count)
150 /* dichotomic search */
151 int lower = 0, upper = count;
152 while (lower < upper) {
153 int mid = (lower + upper) >> 1;
154 int cmp = strcmp(name, flags[mid].name);
156 return flags[mid].value;
166 static struct json_object *timestamp()
171 clock_gettime(CLOCK_MONOTONIC, &tv);
172 snprintf(ts, sizeof ts, "%llu.%06lu", (long long unsigned)tv.tv_sec, (long unsigned)(tv.tv_nsec / 1000));
173 return json_object_new_string(ts);
176 /* verbosity level name or NULL */
177 static const char *verbosity_level_name(int level)
179 static const char *names[] = {
187 return level >= 3 && level <= 7 ? names[level - 3] : NULL;
191 static void emit(void *closure, const char *type, const char *fmt1, const char *fmt2, va_list ap2, ...)
193 struct hook *hook = closure;
194 struct json_object *event, *data1, *data2;
197 data1 = data2 = event = NULL;
199 wrap_json_vpack(&data1, fmt1, ap1);
202 wrap_json_vpack(&data2, fmt2, ap2);
204 wrap_json_pack(&event, "{so ss so so*}",
206 "tag", hook->tag->tag,
210 afb_evt_unhooked_push(hook->event->event, event);
213 /*******************************************************************************/
214 /***** trace the requests *****/
215 /*******************************************************************************/
217 static struct flag xreq_flags[] = { /* must be sorted by names */
218 { "addref", afb_hook_flag_req_addref },
219 { "all", afb_hook_flags_req_all },
220 { "args", afb_hook_flags_req_args },
221 { "begin", afb_hook_flag_req_begin },
222 { "common", afb_hook_flags_req_common },
223 { "context", afb_hook_flags_req_context },
224 { "context_get", afb_hook_flag_req_context_get },
225 { "context_set", afb_hook_flag_req_context_set },
226 { "end", afb_hook_flag_req_end },
227 { "event", afb_hook_flags_req_event },
228 { "extra", afb_hook_flags_req_extra },
229 { "fail", afb_hook_flag_req_fail },
230 { "get", afb_hook_flag_req_get },
231 { "json", afb_hook_flag_req_json },
232 { "life", afb_hook_flags_req_life },
233 { "ref", afb_hook_flags_req_ref },
234 { "result", afb_hook_flags_req_result },
235 { "session", afb_hook_flags_req_session },
236 { "session_close", afb_hook_flag_req_session_close },
237 { "session_set_LOA", afb_hook_flag_req_session_set_LOA },
238 { "store", afb_hook_flag_req_store },
239 { "stores", afb_hook_flags_req_stores },
240 { "subcall", afb_hook_flag_req_subcall },
241 { "subcall_req", afb_hook_flag_req_subcall_req },
242 { "subcall_req_result", afb_hook_flag_req_subcall_req_result },
243 { "subcall_result", afb_hook_flag_req_subcall_result },
244 { "subcalls", afb_hook_flags_req_subcalls },
245 { "subcallsync", afb_hook_flag_req_subcallsync },
246 { "subcallsync_result", afb_hook_flag_req_subcallsync_result },
247 { "subscribe", afb_hook_flag_req_subscribe },
248 { "success", afb_hook_flag_req_success },
249 { "unref", afb_hook_flag_req_unref },
250 { "unstore", afb_hook_flag_req_unstore },
251 { "unsubscribe", afb_hook_flag_req_unsubscribe },
252 { "vverbose", afb_hook_flag_req_vverbose },
255 /* get the xreq value for flag of 'name' */
256 static int get_xreq_flag(const char *name)
258 return get_flag(name, xreq_flags, (int)(sizeof xreq_flags / sizeof *xreq_flags));
261 static void hook_xreq(void *closure, const struct afb_xreq *xreq, const char *action, const char *format, ...)
263 struct json_object *cred = NULL;
264 const char *session = NULL;
267 if (xreq->context.session)
268 session = afb_session_uuid(xreq->context.session);
271 wrap_json_pack(&cred, "{si ss si si ss* ss*}",
272 "uid", (int)xreq->cred->uid,
273 "user", xreq->cred->user,
274 "gid", (int)xreq->cred->gid,
275 "pid", (int)xreq->cred->pid,
276 "label", xreq->cred->label,
279 va_start(ap, format);
280 emit(closure, "request", "{si ss ss ss so* ss*}", format, ap,
281 "index", xreq->hookindex,
290 static void hook_xreq_begin(void *closure, const struct afb_xreq *xreq)
292 hook_xreq(closure, xreq, "begin", NULL);
295 static void hook_xreq_end(void *closure, const struct afb_xreq *xreq)
297 hook_xreq(closure, xreq, "end", NULL);
300 static void hook_xreq_json(void *closure, const struct afb_xreq *xreq, struct json_object *obj)
302 hook_xreq(closure, xreq, "json", "{sO?}",
306 static void hook_xreq_get(void *closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
308 hook_xreq(closure, xreq, "get", "{ss? ss? ss? ss?}",
315 static void hook_xreq_success(void *closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
317 hook_xreq(closure, xreq, "success", "{sO? ss?}",
322 static void hook_xreq_fail(void *closure, const struct afb_xreq *xreq, const char *status, const char *info)
324 hook_xreq(closure, xreq, "fail", "{ss? ss?}",
329 static void hook_xreq_context_get(void *closure, const struct afb_xreq *xreq, void *value)
331 hook_xreq(closure, xreq, "context_get", NULL);
334 static void hook_xreq_context_set(void *closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
336 hook_xreq(closure, xreq, "context_set", NULL);
339 static void hook_xreq_addref(void *closure, const struct afb_xreq *xreq)
341 hook_xreq(closure, xreq, "addref", NULL);
344 static void hook_xreq_unref(void *closure, const struct afb_xreq *xreq)
346 hook_xreq(closure, xreq, "unref", NULL);
349 static void hook_xreq_session_close(void *closure, const struct afb_xreq *xreq)
351 hook_xreq(closure, xreq, "session_close", NULL);
354 static void hook_xreq_session_set_LOA(void *closure, const struct afb_xreq *xreq, unsigned level, int result)
356 hook_xreq(closure, xreq, "session_set_LOA", "{si si}",
361 static void hook_xreq_subscribe(void *closure, const struct afb_xreq *xreq, struct afb_event event, int result)
363 hook_xreq(closure, xreq, "subscribe", "{s{ss si} si}",
365 "name", afb_evt_event_name(event),
366 "id", afb_evt_event_id(event),
370 static void hook_xreq_unsubscribe(void *closure, const struct afb_xreq *xreq, struct afb_event event, int result)
372 hook_xreq(closure, xreq, "unsubscribe", "{s{ss? si} si}",
374 "name", afb_evt_event_name(event),
375 "id", afb_evt_event_id(event),
379 static void hook_xreq_subcall(void *closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
381 hook_xreq(closure, xreq, "subcall", "{ss? ss? sO?}",
387 static void hook_xreq_subcall_result(void *closure, const struct afb_xreq *xreq, int status, struct json_object *result)
389 hook_xreq(closure, xreq, "subcall_result", "{si sO?}",
394 static void hook_xreq_subcallsync(void *closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
396 hook_xreq(closure, xreq, "subcallsync", "{ss? ss? sO?}",
402 static void hook_xreq_subcallsync_result(void *closure, const struct afb_xreq *xreq, int status, struct json_object *result)
404 hook_xreq(closure, xreq, "subcallsync_result", "{si sO?}",
409 static void hook_xreq_vverbose(void *closure, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
411 struct json_object *pos;
420 len = vasprintf(&msg, fmt, ap);
424 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", func);
426 hook_xreq(closure, xreq, "vverbose", "{si ss* ss? so*}",
428 "type", verbosity_level_name(level),
429 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
435 static void hook_xreq_store(void *closure, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
437 hook_xreq(closure, xreq, "store", NULL);
440 static void hook_xreq_unstore(void *closure, const struct afb_xreq *xreq)
442 hook_xreq(closure, xreq, "unstore", NULL);
445 static void hook_xreq_subcall_req(void *closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
447 hook_xreq(closure, xreq, "subcall_req", "{ss? ss? sO?}",
453 static void hook_xreq_subcall_req_result(void *closure, const struct afb_xreq *xreq, int status, struct json_object *result)
455 hook_xreq(closure, xreq, "subcall_req_result", "{si sO?}",
460 static struct afb_hook_xreq_itf hook_xreq_itf = {
461 .hook_xreq_begin = hook_xreq_begin,
462 .hook_xreq_end = hook_xreq_end,
463 .hook_xreq_json = hook_xreq_json,
464 .hook_xreq_get = hook_xreq_get,
465 .hook_xreq_success = hook_xreq_success,
466 .hook_xreq_fail = hook_xreq_fail,
467 .hook_xreq_context_get = hook_xreq_context_get,
468 .hook_xreq_context_set = hook_xreq_context_set,
469 .hook_xreq_addref = hook_xreq_addref,
470 .hook_xreq_unref = hook_xreq_unref,
471 .hook_xreq_session_close = hook_xreq_session_close,
472 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA,
473 .hook_xreq_subscribe = hook_xreq_subscribe,
474 .hook_xreq_unsubscribe = hook_xreq_unsubscribe,
475 .hook_xreq_subcall = hook_xreq_subcall,
476 .hook_xreq_subcall_result = hook_xreq_subcall_result,
477 .hook_xreq_subcallsync = hook_xreq_subcallsync,
478 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result,
479 .hook_xreq_vverbose = hook_xreq_vverbose,
480 .hook_xreq_store = hook_xreq_store,
481 .hook_xreq_unstore = hook_xreq_unstore,
482 .hook_xreq_subcall_req = hook_xreq_subcall_req,
483 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result
486 /*******************************************************************************/
487 /***** trace the daemon interface *****/
488 /*******************************************************************************/
490 static struct flag ditf_flags[] = { /* must be sorted by names */
491 { "all", afb_hook_flags_ditf_all },
492 { "common", afb_hook_flags_ditf_common },
493 { "event_broadcast_after", afb_hook_flag_ditf_event_broadcast_after },
494 { "event_broadcast_before", afb_hook_flag_ditf_event_broadcast_before },
495 { "event_make", afb_hook_flag_ditf_event_make },
496 { "extra", afb_hook_flags_ditf_extra },
497 { "get_event_loop", afb_hook_flag_ditf_get_event_loop },
498 { "get_system_bus", afb_hook_flag_ditf_get_system_bus },
499 { "get_user_bus", afb_hook_flag_ditf_get_user_bus },
500 { "queue_job", afb_hook_flag_ditf_queue_job },
501 { "require_api", afb_hook_flag_ditf_require_api },
502 { "require_api_result", afb_hook_flag_ditf_require_api_result },
503 { "rootdir_get_fd", afb_hook_flag_ditf_rootdir_get_fd },
504 { "rootdir_open_locale", afb_hook_flag_ditf_rootdir_open_locale },
505 { "unstore_req", afb_hook_flag_ditf_unstore_req },
506 { "vverbose", afb_hook_flag_ditf_vverbose },
509 /* get the ditf value for flag of 'name' */
510 static int get_ditf_flag(const char *name)
512 return get_flag(name, ditf_flags, (int)(sizeof ditf_flags / sizeof *ditf_flags));
516 static void hook_ditf(void *closure, const struct afb_ditf *ditf, const char *action, const char *format, ...)
520 va_start(ap, format);
521 emit(closure, "daemon", "{ss ss}", format, ap,
527 static void hook_ditf_event_broadcast_before(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object)
529 hook_ditf(closure, ditf, "event_broadcast_before", "{ss sO*}",
530 "name", name, "data", object);
533 static void hook_ditf_event_broadcast_after(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
535 hook_ditf(closure, ditf, "event_broadcast_after", "{ss sO* si}",
536 "name", name, "data", object, "result", result);
539 static void hook_ditf_get_event_loop(void *closure, const struct afb_ditf *ditf, struct sd_event *result)
541 hook_ditf(closure, ditf, "get_event_loop", NULL);
544 static void hook_ditf_get_user_bus(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
546 hook_ditf(closure, ditf, "get_user_bus", NULL);
549 static void hook_ditf_get_system_bus(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
551 hook_ditf(closure, ditf, "get_system_bus", NULL);
554 static void hook_ditf_vverbose(void*closure, const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
556 struct json_object *pos;
565 len = vasprintf(&msg, fmt, ap);
569 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
571 hook_ditf(closure, ditf, "vverbose", "{si ss* ss? so*}",
573 "type", verbosity_level_name(level),
574 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
580 static void hook_ditf_event_make(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result)
582 hook_ditf(closure, ditf, "event_make", "{ss ss si}",
583 "name", name, "event", afb_evt_event_name(result), "id", afb_evt_event_id(result));
586 static void hook_ditf_rootdir_get_fd(void *closure, const struct afb_ditf *ditf, int result)
591 sprintf(path, "/proc/self/fd/%d", result);
592 readlink(path, path, sizeof path);
595 hook_ditf(closure, ditf, "rootdir_get_fd", "{ss}",
596 result < 0 ? "path" : "error",
597 result < 0 ? strerror(errno) : path);
600 static void hook_ditf_rootdir_open_locale(void *closure, const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
605 sprintf(path, "/proc/self/fd/%d", result);
606 readlink(path, path, sizeof path);
609 hook_ditf(closure, ditf, "rootdir_open_locale", "{ss si ss* ss}",
613 result < 0 ? "path" : "error",
614 result < 0 ? strerror(errno) : path);
617 static void hook_ditf_queue_job(void *closure, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
619 hook_ditf(closure, ditf, "queue_job", "{ss}", "result", result);
622 static void hook_ditf_unstore_req(void * closure, const struct afb_ditf *ditf, struct afb_stored_req *sreq)
624 hook_ditf(closure, ditf, "unstore_req", NULL);
627 static void hook_ditf_require_api(void *closure, const struct afb_ditf *ditf, const char *name, int initialized)
629 hook_ditf(closure, ditf, "require_api", "{ss sb}", "name", name, "initialized", initialized);
632 static void hook_ditf_require_api_result(void *closure, const struct afb_ditf *ditf, const char *name, int initialized, int result)
634 hook_ditf(closure, ditf, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
637 static struct afb_hook_ditf_itf hook_ditf_itf = {
638 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before,
639 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after,
640 .hook_ditf_get_event_loop = hook_ditf_get_event_loop,
641 .hook_ditf_get_user_bus = hook_ditf_get_user_bus,
642 .hook_ditf_get_system_bus = hook_ditf_get_system_bus,
643 .hook_ditf_vverbose = hook_ditf_vverbose,
644 .hook_ditf_event_make = hook_ditf_event_make,
645 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd,
646 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale,
647 .hook_ditf_queue_job = hook_ditf_queue_job,
648 .hook_ditf_unstore_req = hook_ditf_unstore_req,
649 .hook_ditf_require_api = hook_ditf_require_api,
650 .hook_ditf_require_api_result = hook_ditf_require_api_result
653 /*******************************************************************************/
654 /***** trace the services *****/
655 /*******************************************************************************/
657 static struct flag svc_flags[] = { /* must be sorted by names */
658 { "all", afb_hook_flags_svc_all },
659 { "call", afb_hook_flag_svc_call },
660 { "call_result", afb_hook_flag_svc_call_result },
661 { "callsync", afb_hook_flag_svc_callsync },
662 { "callsync_result", afb_hook_flag_svc_callsync_result },
663 { "on_event_after", afb_hook_flag_svc_on_event_after },
664 { "on_event_before", afb_hook_flag_svc_on_event_before },
665 { "start_after", afb_hook_flag_svc_start_after },
666 { "start_before", afb_hook_flag_svc_start_before },
669 /* get the svc value for flag of 'name' */
670 static int get_svc_flag(const char *name)
672 return get_flag(name, svc_flags, (int)(sizeof svc_flags / sizeof *svc_flags));
675 static void hook_svc(void *closure, const struct afb_svc *svc, const char *action, const char *format, ...)
679 va_start(ap, format);
680 emit(closure, "service", "{ss ss}", format, ap,
686 static void hook_svc_start_before(void *closure, const struct afb_svc *svc)
688 hook_svc(closure, svc, "start_before", NULL);
691 static void hook_svc_start_after(void *closure, const struct afb_svc *svc, int status)
693 hook_svc(closure, svc, "start_after", "{si}", "result", status);
696 static void hook_svc_on_event_before(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
698 hook_svc(closure, svc, "on_event_before", "{ss si sO*}",
699 "event", event, "id", eventid, "data", object);
702 static void hook_svc_on_event_after(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
704 hook_svc(closure, svc, "on_event_after", "{ss si sO*}",
705 "event", event, "id", eventid, "data", object);
708 static void hook_svc_call(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
710 hook_svc(closure, svc, "call", "{ss ss sO*}",
711 "api", api, "verb", verb, "args", args);
714 static void hook_svc_call_result(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
716 hook_svc(closure, svc, "call_result", "{si sO*}",
717 "status", status, "result", result);
720 static void hook_svc_callsync(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
722 hook_svc(closure, svc, "callsync", "{ss ss sO*}",
723 "api", api, "verb", verb, "args", args);
726 static void hook_svc_callsync_result(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
728 hook_svc(closure, svc, "callsync_result", "{si sO*}",
729 "status", status, "result", result);
732 static struct afb_hook_svc_itf hook_svc_itf = {
733 .hook_svc_start_before = hook_svc_start_before,
734 .hook_svc_start_after = hook_svc_start_after,
735 .hook_svc_on_event_before = hook_svc_on_event_before,
736 .hook_svc_on_event_after = hook_svc_on_event_after,
737 .hook_svc_call = hook_svc_call,
738 .hook_svc_call_result = hook_svc_call_result,
739 .hook_svc_callsync = hook_svc_callsync,
740 .hook_svc_callsync_result = hook_svc_callsync_result
743 /*******************************************************************************/
744 /***** trace the events *****/
745 /*******************************************************************************/
747 static struct flag evt_flags[] = { /* must be sorted by names */
748 { "all", afb_hook_flags_evt_all },
749 { "broadcast_after", afb_hook_flag_evt_broadcast_after },
750 { "broadcast_before", afb_hook_flag_evt_broadcast_before },
751 { "common", afb_hook_flags_evt_common },
752 { "create", afb_hook_flag_evt_create },
753 { "drop", afb_hook_flag_evt_drop },
754 { "extra", afb_hook_flags_evt_extra },
755 { "name", afb_hook_flag_evt_name },
756 { "push_after", afb_hook_flag_evt_push_after },
757 { "push_before", afb_hook_flag_evt_push_before },
760 /* get the evt value for flag of 'name' */
761 static int get_evt_flag(const char *name)
763 return get_flag(name, evt_flags, (int)(sizeof evt_flags / sizeof *evt_flags));
766 static void hook_evt(void *closure, const char *evt, int id, const char *action, const char *format, ...)
770 va_start(ap, format);
771 emit(closure, "event", "{si ss ss}", format, ap,
778 static void hook_evt_create(void *closure, const char *evt, int id)
780 hook_evt(closure, evt, id, "create", NULL);
783 static void hook_evt_push_before(void *closure, const char *evt, int id, struct json_object *obj)
785 hook_evt(closure, evt, id, "push_before", "{sO*}", "data", obj);
789 static void hook_evt_push_after(void *closure, const char *evt, int id, struct json_object *obj, int result)
791 hook_evt(closure, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
794 static void hook_evt_broadcast_before(void *closure, const char *evt, int id, struct json_object *obj)
796 hook_evt(closure, evt, id, "broadcast_before", "{sO*}", "data", obj);
799 static void hook_evt_broadcast_after(void *closure, const char *evt, int id, struct json_object *obj, int result)
801 hook_evt(closure, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
804 static void hook_evt_name(void *closure, const char *evt, int id)
806 hook_evt(closure, evt, id, "name", NULL);
809 static void hook_evt_drop(void *closure, const char *evt, int id)
811 hook_evt(closure, evt, id, "drop", NULL);
814 static struct afb_hook_evt_itf hook_evt_itf = {
815 .hook_evt_create = hook_evt_create,
816 .hook_evt_push_before = hook_evt_push_before,
817 .hook_evt_push_after = hook_evt_push_after,
818 .hook_evt_broadcast_before = hook_evt_broadcast_before,
819 .hook_evt_broadcast_after = hook_evt_broadcast_after,
820 .hook_evt_name = hook_evt_name,
821 .hook_evt_drop = hook_evt_drop
824 /*******************************************************************************/
825 /***** abstract types *****/
826 /*******************************************************************************/
832 void (*unref)(void*);
833 int (*get_flag)(const char*);
835 abstracting[Trace_Type_Count] =
840 .unref = (void(*)(void*))afb_hook_unref_xreq,
841 .get_flag = get_xreq_flag
846 .unref = (void(*)(void*))afb_hook_unref_ditf,
847 .get_flag = get_ditf_flag
852 .unref = (void(*)(void*))afb_hook_unref_svc,
853 .get_flag = get_svc_flag
858 .unref = (void(*)(void*))afb_hook_unref_evt,
859 .get_flag = get_evt_flag
863 /*******************************************************************************/
864 /***** handle trace data *****/
865 /*******************************************************************************/
867 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
868 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct session *session)
871 struct hook *hook, **prev;
873 /* remove any event */
874 for (i = 0 ; i < Trace_Type_Count ; i++) {
875 prev = &trace->hooks[i];
876 while ((hook = *prev)) {
877 if ((tag && tag != hook->tag)
878 || (event && event != hook->event)
879 || (session && session != hook->session))
883 abstracting[i].unref(hook->handler);
890 /* cleanup: removes unused tags, events and sessions of the 'trace' */
891 static void trace_cleanup(struct afb_trace *trace)
895 struct tag *tag, **ptag;
896 struct event *event, **pevent;
897 struct session *session, **psession;
900 psession = &trace->sessions;
901 while ((session = *psession)) {
902 /* search for session */
903 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
904 for (hook = trace->hooks[i] ; hook && hook->session != session ; hook = hook->next);
905 /* keep or free whether used or not */
907 psession = &session->next;
909 *psession = session->next;
910 if (__atomic_exchange_n(&session->trace, NULL, __ATOMIC_RELAXED))
911 afb_session_set_cookie(session->session, session, NULL, NULL);
917 while ((tag = *ptag)) {
919 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
920 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
921 /* keep or free whether used or not */
930 pevent = &trace->events;
931 while ((event = *pevent)) {
932 /* search for event */
933 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
934 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
935 /* keep or free whether used or not */
937 pevent = &event->next;
939 *pevent = event->next;
940 afb_event_drop(event->event);
946 /* callback at end of traced session */
947 static void free_session_cookie(void *cookie)
949 struct session *session = cookie;
950 struct afb_trace *trace = __atomic_exchange_n(&session->trace, NULL, __ATOMIC_RELAXED);
952 pthread_mutex_lock(&trace->mutex);
953 trace_unhook(trace, NULL, NULL, session);
954 trace_cleanup(trace);
955 pthread_mutex_unlock(&trace->mutex);
960 * Get the tag of 'name' within 'trace'.
961 * If 'alloc' isn't zero, create the tag and add it.
963 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
967 /* search the tag of 'name' */
969 while (tag && strcmp(name, tag->tag))
973 /* creation if needed */
974 tag = malloc(sizeof * tag + strlen(name));
976 strcpy(tag->tag, name);
977 tag->next = trace->tags;
985 * Get the event of 'name' within 'trace'.
986 * If 'alloc' isn't zero, create the event and add it.
988 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
992 /* search the event */
993 event = trace->events;
994 while (event && strcmp(afb_event_name(event->event), name))
997 if (!event && alloc) {
998 event = malloc(sizeof * event);
1000 event->event = trace->daemon->itf->event_make(trace->daemon->closure, name);
1001 if (afb_event_is_valid(event->event)) {
1002 event->next = trace->events;
1003 trace->events = event;
1014 * Get the session of 'value' within 'trace'.
1015 * If 'alloc' isn't zero, create the session and add it.
1017 static struct session *trace_get_session(struct afb_trace *trace, struct afb_session *value, int alloc)
1019 struct session *session;
1021 /* search the session */
1022 session = trace->sessions;
1023 while (session && session->session != value)
1024 session = session->next;
1026 if (!session && alloc) {
1027 session = malloc(sizeof * session);
1029 session->session = value;
1030 session->trace = NULL;
1031 session->next = trace->sessions;
1032 trace->sessions = session;
1039 * Get the session of 'uuid' within 'trace'.
1040 * If 'alloc' isn't zero, create the session and add it.
1042 static struct session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1044 struct afb_session *session;
1047 session = afb_session_get(uuid, alloc ? &created : NULL);
1048 return session ? trace_get_session(trace, session, alloc) : NULL;
1051 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1055 tag = tag ?: DEFAULT_TAG_NAME;
1056 event = event ?: DEFAULT_EVENT_NAME;
1057 hook = malloc(sizeof *hook);
1059 hook->tag = trace_get_tag(trace, tag, 1);
1060 hook->event = trace_get_event(trace, event, 1);
1061 hook->session = NULL;
1062 hook->handler = NULL;
1067 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1069 struct session *session = hook->session;
1070 hook->next = trace->hooks[type];
1071 trace->hooks[type] = hook;
1072 if (session && !session->trace) {
1073 session->trace = trace;
1074 afb_session_set_cookie(session->session, session, session, free_session_cookie);
1078 /*******************************************************************************/
1079 /***** handle client requests *****/
1080 /*******************************************************************************/
1084 struct afb_trace *trace;
1091 struct context *context;
1094 const char *session;
1097 const char *pattern;
1098 int flags[Trace_Type_Count];
1102 static void addhook(struct desc *desc, enum trace_type type)
1105 struct session *session;
1106 struct afb_session *bind;
1107 struct afb_trace *trace = desc->context->trace;
1109 /* check permission for bound traces */
1110 bind = trace->bound;
1112 if (type != Trace_Type_Xreq) {
1113 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1116 if (desc->session) {
1117 ctxt_error(&desc->context->errors, "setting session is forbidden");
1122 /* allocate the hook */
1123 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1125 ctxt_error(&desc->context->errors, "allocation of hook failed");
1129 /* create the hook handler */
1131 case Trace_Type_Xreq:
1132 if (desc->session) {
1133 session = trace_get_session_by_uuid(trace, desc->session, 1);
1135 ctxt_error(&desc->context->errors, "allocation of session failed");
1139 bind = session->session;
1141 hook->handler = afb_hook_create_xreq(desc->api, desc->verb, bind,
1142 desc->flags[type], &hook_xreq_itf, hook);
1144 case Trace_Type_Ditf:
1145 hook->handler = afb_hook_create_ditf(desc->api, desc->flags[type], &hook_ditf_itf, hook);
1147 case Trace_Type_Svc:
1148 hook->handler = afb_hook_create_svc(desc->api, desc->flags[type], &hook_svc_itf, hook);
1150 case Trace_Type_Evt:
1151 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1156 if (!hook->handler) {
1157 ctxt_error(&desc->context->errors, "creation of hook failed");
1162 /* attach and activate the hook */
1163 afb_req_subscribe(desc->context->req, hook->event->event);
1164 trace_attach_hook(trace, hook, type);
1167 static void addhooks(struct desc *desc)
1171 for (i = 0 ; i < Trace_Type_Count ; i++) {
1177 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1180 const char *name, *queried;
1181 struct desc *desc = closure;
1183 if (wrap_json_unpack(object, "s", &name))
1184 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1185 abstracting[type].name,
1186 json_object_to_json_string(object));
1188 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1189 value = abstracting[type].get_flag(queried);
1191 desc->flags[type] |= value;
1193 ctxt_error(&desc->context->errors, "unknown %s name %s",
1194 abstracting[type].name, name);
1198 static void add_xreq_flags(void *closure, struct json_object *object)
1200 add_flags(closure, object, Trace_Type_Xreq);
1203 static void add_ditf_flags(void *closure, struct json_object *object)
1205 add_flags(closure, object, Trace_Type_Ditf);
1208 static void add_svc_flags(void *closure, struct json_object *object)
1210 add_flags(closure, object, Trace_Type_Svc);
1213 static void add_evt_flags(void *closure, struct json_object *object)
1215 add_flags(closure, object, Trace_Type_Evt);
1219 static void add(void *closure, struct json_object *object)
1223 struct json_object *request, *event, *daemon, *service, *sub;
1225 memcpy (&desc, closure, sizeof desc);
1226 request = event = daemon = service = sub = NULL;
1228 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}",
1233 "session", &desc.session,
1234 "pattern", &desc.pattern,
1235 "request", &request,
1237 "service", &service,
1243 if (desc.api && desc.api[0] == '*' && !desc.api[1])
1246 if (desc.verb && desc.verb[0] == '*' && !desc.verb[1])
1249 if (desc.session && desc.session[0] == '*' && !desc.session[1])
1250 desc.session = NULL;
1252 /* get what is expected */
1254 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1257 wrap_json_optarray_for_all(daemon, add_ditf_flags, &desc);
1260 wrap_json_optarray_for_all(service, add_svc_flags, &desc);
1263 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1267 wrap_json_optarray_for_all(sub, add, &desc);
1272 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1277 /* drop hooks of given tag */
1278 static void drop_tag(void *closure, struct json_object *object)
1281 struct context *context = closure;
1285 rc = wrap_json_unpack(object, "s", &name);
1287 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string(object));
1289 tag = trace_get_tag(context->trace, name, 0);
1291 ctxt_error(&context->errors, "tag %s not found", name);
1293 trace_unhook(context->trace, tag, NULL, NULL);
1297 /* drop hooks of given event */
1298 static void drop_event(void *closure, struct json_object *object)
1301 struct context *context = closure;
1302 struct event *event;
1305 rc = wrap_json_unpack(object, "s", &name);
1307 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string(object));
1309 event = trace_get_event(context->trace, name, 0);
1311 ctxt_error(&context->errors, "event %s not found", name);
1313 trace_unhook(context->trace, NULL, event, NULL);
1317 /* drop hooks of given session */
1318 static void drop_session(void *closure, struct json_object *object)
1321 struct context *context = closure;
1322 struct session *session;
1325 rc = wrap_json_unpack(object, "s", &uuid);
1327 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string(object));
1329 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1331 ctxt_error(&context->errors, "session %s not found", uuid);
1333 trace_unhook(context->trace, NULL, NULL, session);
1337 /*******************************************************************************/
1338 /***** public interface *****/
1339 /*******************************************************************************/
1341 /* allocates an afb_trace instance */
1342 struct afb_trace *afb_trace_create(struct afb_daemon *daemon, struct afb_session *bound)
1344 struct afb_trace *trace;
1348 trace = calloc(1, sizeof *trace);
1350 trace->refcount = 1;
1351 trace->bound = bound;
1352 trace->daemon = daemon;
1353 pthread_mutex_init(&trace->mutex, NULL);
1358 /* add a reference to the trace */
1359 void afb_trace_addref(struct afb_trace *trace)
1361 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1364 /* drop one reference to the trace */
1365 void afb_trace_unref(struct afb_trace *trace)
1367 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1369 trace_unhook(trace, NULL, NULL, NULL);
1370 trace_cleanup(trace);
1371 pthread_mutex_destroy(&trace->mutex);
1377 int afb_trace_add(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1379 struct context context;
1382 memset(&context, 0, sizeof context);
1383 context.trace = trace;
1386 memset(&desc, 0, sizeof desc);
1387 desc.context = &context;
1389 pthread_mutex_lock(&trace->mutex);
1390 wrap_json_optarray_for_all(args, add, &desc);
1391 pthread_mutex_unlock(&trace->mutex);
1393 if (!context.errors)
1396 afb_req_fail(req, "error-detected", context.errors);
1397 free(context.errors);
1402 extern int afb_trace_drop(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1405 struct context context;
1406 struct json_object *tags, *events, *sessions;
1408 memset(&context, 0, sizeof context);
1409 context.trace = trace;
1412 /* special: boolean value */
1413 if (!wrap_json_unpack(args, "b", &rc)) {
1415 pthread_mutex_lock(&trace->mutex);
1416 trace_unhook(trace, NULL, NULL, NULL);
1417 trace_cleanup(trace);
1418 pthread_mutex_unlock(&trace->mutex);
1423 tags = events = sessions = NULL;
1424 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1427 "session", &sessions);
1429 if (rc < 0 || !(events || tags || sessions)) {
1430 afb_req_fail(req, "error-detected", "bad drop arguments");
1434 pthread_mutex_lock(&trace->mutex);
1437 wrap_json_optarray_for_all(tags, drop_tag, &context);
1440 wrap_json_optarray_for_all(events, drop_event, &context);
1443 wrap_json_optarray_for_all(sessions, drop_session, &context);
1445 trace_cleanup(trace);
1447 pthread_mutex_unlock(&trace->mutex);
1449 if (!context.errors)
1452 afb_req_fail(req, "error-detected", context.errors);
1453 free(context.errors);