trace2 / tr2_tgt_event.con commit Merge branch 'dl/no-extern-in-func-decl' (4aeeef3)
   1#include "cache.h"
   2#include "config.h"
   3#include "json-writer.h"
   4#include "run-command.h"
   5#include "version.h"
   6#include "trace2/tr2_dst.h"
   7#include "trace2/tr2_tbuf.h"
   8#include "trace2/tr2_sid.h"
   9#include "trace2/tr2_sysenv.h"
  10#include "trace2/tr2_tgt.h"
  11#include "trace2/tr2_tls.h"
  12
  13static struct tr2_dst tr2dst_event = { TR2_SYSENV_EVENT, 0, 0, 0 };
  14
  15/*
  16 * The version number of the JSON data generated by the EVENT target
  17 * in this source file.  Update this if you make a significant change
  18 * to the JSON fields or message structure.  You probably do not need
  19 * to update this if you just add another call to one of the existing
  20 * TRACE2 API methods.
  21 */
  22#define TR2_EVENT_VERSION "1"
  23
  24/*
  25 * Region nesting limit for messages written to the event target.
  26 *
  27 * The "region_enter" and "region_leave" messages (especially recursive
  28 * messages such as those produced while diving the worktree or index)
  29 * are primarily intended for the performance target during debugging.
  30 *
  31 * Some of the outer-most messages, however, may be of interest to the
  32 * event target.  Use the TR2_SYSENV_EVENT_NESTING setting to increase
  33 * region details in the event target.
  34 */
  35static int tr2env_event_max_nesting_levels = 2;
  36
  37/*
  38 * Use the TR2_SYSENV_EVENT_BRIEF to omit the <time>, <file>, and
  39 * <line> fields from most events.
  40 */
  41static int tr2env_event_be_brief;
  42
  43static int fn_init(void)
  44{
  45        int want = tr2_dst_trace_want(&tr2dst_event);
  46        int max_nesting;
  47        int want_brief;
  48        const char *nesting;
  49        const char *brief;
  50
  51        if (!want)
  52                return want;
  53
  54        nesting = tr2_sysenv_get(TR2_SYSENV_EVENT_NESTING);
  55        if (nesting && *nesting && ((max_nesting = atoi(nesting)) > 0))
  56                tr2env_event_max_nesting_levels = max_nesting;
  57
  58        brief = tr2_sysenv_get(TR2_SYSENV_EVENT_BRIEF);
  59        if (brief && *brief &&
  60            ((want_brief = git_parse_maybe_bool(brief)) != -1))
  61                tr2env_event_be_brief = want_brief;
  62
  63        return want;
  64}
  65
  66static void fn_term(void)
  67{
  68        tr2_dst_trace_disable(&tr2dst_event);
  69}
  70
  71/*
  72 * Append common key-value pairs to the currently open JSON object.
  73 *     "event:"<event_name>"
  74 *      "sid":"<sid>"
  75 *   "thread":"<thread_name>"
  76 *     "time":"<time>"
  77 *     "file":"<filename>"
  78 *     "line":<line_number>
  79 *     "repo":<repo_id>
  80 */
  81static void event_fmt_prepare(const char *event_name, const char *file,
  82                              int line, const struct repository *repo,
  83                              struct json_writer *jw)
  84{
  85        struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
  86        struct tr2_tbuf tb_now;
  87
  88        jw_object_string(jw, "event", event_name);
  89        jw_object_string(jw, "sid", tr2_sid_get());
  90        jw_object_string(jw, "thread", ctx->thread_name.buf);
  91
  92        /*
  93         * In brief mode, only emit <time> on these 2 event types.
  94         */
  95        if (!tr2env_event_be_brief || !strcmp(event_name, "version") ||
  96            !strcmp(event_name, "atexit")) {
  97                tr2_tbuf_utc_datetime_extended(&tb_now);
  98                jw_object_string(jw, "time", tb_now.buf);
  99        }
 100
 101        if (!tr2env_event_be_brief && file && *file) {
 102                jw_object_string(jw, "file", file);
 103                jw_object_intmax(jw, "line", line);
 104        }
 105
 106        if (repo)
 107                jw_object_intmax(jw, "repo", repo->trace2_repo_id);
 108}
 109
 110static void fn_version_fl(const char *file, int line)
 111{
 112        const char *event_name = "version";
 113        struct json_writer jw = JSON_WRITER_INIT;
 114
 115        jw_object_begin(&jw, 0);
 116        event_fmt_prepare(event_name, file, line, NULL, &jw);
 117        jw_object_string(&jw, "evt", TR2_EVENT_VERSION);
 118        jw_object_string(&jw, "exe", git_version_string);
 119        jw_end(&jw);
 120
 121        tr2_dst_write_line(&tr2dst_event, &jw.json);
 122        jw_release(&jw);
 123}
 124
 125static void fn_start_fl(const char *file, int line,
 126                        uint64_t us_elapsed_absolute, const char **argv)
 127{
 128        const char *event_name = "start";
 129        struct json_writer jw = JSON_WRITER_INIT;
 130        double t_abs = (double)us_elapsed_absolute / 1000000.0;
 131
 132        jw_object_begin(&jw, 0);
 133        event_fmt_prepare(event_name, file, line, NULL, &jw);
 134        jw_object_double(&jw, "t_abs", 6, t_abs);
 135        jw_object_inline_begin_array(&jw, "argv");
 136        jw_array_argv(&jw, argv);
 137        jw_end(&jw);
 138        jw_end(&jw);
 139
 140        tr2_dst_write_line(&tr2dst_event, &jw.json);
 141        jw_release(&jw);
 142}
 143
 144static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
 145                       int code)
 146{
 147        const char *event_name = "exit";
 148        struct json_writer jw = JSON_WRITER_INIT;
 149        double t_abs = (double)us_elapsed_absolute / 1000000.0;
 150
 151        jw_object_begin(&jw, 0);
 152        event_fmt_prepare(event_name, file, line, NULL, &jw);
 153        jw_object_double(&jw, "t_abs", 6, t_abs);
 154        jw_object_intmax(&jw, "code", code);
 155        jw_end(&jw);
 156
 157        tr2_dst_write_line(&tr2dst_event, &jw.json);
 158        jw_release(&jw);
 159}
 160
 161static void fn_signal(uint64_t us_elapsed_absolute, int signo)
 162{
 163        const char *event_name = "signal";
 164        struct json_writer jw = JSON_WRITER_INIT;
 165        double t_abs = (double)us_elapsed_absolute / 1000000.0;
 166
 167        jw_object_begin(&jw, 0);
 168        event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
 169        jw_object_double(&jw, "t_abs", 6, t_abs);
 170        jw_object_intmax(&jw, "signo", signo);
 171        jw_end(&jw);
 172
 173        tr2_dst_write_line(&tr2dst_event, &jw.json);
 174        jw_release(&jw);
 175}
 176
 177static void fn_atexit(uint64_t us_elapsed_absolute, int code)
 178{
 179        const char *event_name = "atexit";
 180        struct json_writer jw = JSON_WRITER_INIT;
 181        double t_abs = (double)us_elapsed_absolute / 1000000.0;
 182
 183        jw_object_begin(&jw, 0);
 184        event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
 185        jw_object_double(&jw, "t_abs", 6, t_abs);
 186        jw_object_intmax(&jw, "code", code);
 187        jw_end(&jw);
 188
 189        tr2_dst_write_line(&tr2dst_event, &jw.json);
 190        jw_release(&jw);
 191}
 192
 193static void maybe_add_string_va(struct json_writer *jw, const char *field_name,
 194                                const char *fmt, va_list ap)
 195{
 196        if (fmt && *fmt) {
 197                va_list copy_ap;
 198                struct strbuf buf = STRBUF_INIT;
 199
 200                va_copy(copy_ap, ap);
 201                strbuf_vaddf(&buf, fmt, copy_ap);
 202                va_end(copy_ap);
 203
 204                jw_object_string(jw, field_name, buf.buf);
 205                strbuf_release(&buf);
 206                return;
 207        }
 208
 209        if (fmt && *fmt) {
 210                jw_object_string(jw, field_name, fmt);
 211                return;
 212        }
 213}
 214
 215static void fn_error_va_fl(const char *file, int line, const char *fmt,
 216                           va_list ap)
 217{
 218        const char *event_name = "error";
 219        struct json_writer jw = JSON_WRITER_INIT;
 220
 221        jw_object_begin(&jw, 0);
 222        event_fmt_prepare(event_name, file, line, NULL, &jw);
 223        maybe_add_string_va(&jw, "msg", fmt, ap);
 224        /*
 225         * Also emit the format string as a field in case
 226         * post-processors want to aggregate common error
 227         * messages by type without argument fields (such
 228         * as pathnames or branch names) cluttering it up.
 229         */
 230        if (fmt && *fmt)
 231                jw_object_string(&jw, "fmt", fmt);
 232        jw_end(&jw);
 233
 234        tr2_dst_write_line(&tr2dst_event, &jw.json);
 235        jw_release(&jw);
 236}
 237
 238static void fn_command_path_fl(const char *file, int line, const char *pathname)
 239{
 240        const char *event_name = "cmd_path";
 241        struct json_writer jw = JSON_WRITER_INIT;
 242
 243        jw_object_begin(&jw, 0);
 244        event_fmt_prepare(event_name, file, line, NULL, &jw);
 245        jw_object_string(&jw, "path", pathname);
 246        jw_end(&jw);
 247
 248        tr2_dst_write_line(&tr2dst_event, &jw.json);
 249        jw_release(&jw);
 250}
 251
 252static void fn_command_name_fl(const char *file, int line, const char *name,
 253                               const char *hierarchy)
 254{
 255        const char *event_name = "cmd_name";
 256        struct json_writer jw = JSON_WRITER_INIT;
 257
 258        jw_object_begin(&jw, 0);
 259        event_fmt_prepare(event_name, file, line, NULL, &jw);
 260        jw_object_string(&jw, "name", name);
 261        if (hierarchy && *hierarchy)
 262                jw_object_string(&jw, "hierarchy", hierarchy);
 263        jw_end(&jw);
 264
 265        tr2_dst_write_line(&tr2dst_event, &jw.json);
 266        jw_release(&jw);
 267}
 268
 269static void fn_command_mode_fl(const char *file, int line, const char *mode)
 270{
 271        const char *event_name = "cmd_mode";
 272        struct json_writer jw = JSON_WRITER_INIT;
 273
 274        jw_object_begin(&jw, 0);
 275        event_fmt_prepare(event_name, file, line, NULL, &jw);
 276        jw_object_string(&jw, "name", mode);
 277        jw_end(&jw);
 278
 279        tr2_dst_write_line(&tr2dst_event, &jw.json);
 280        jw_release(&jw);
 281}
 282
 283static void fn_alias_fl(const char *file, int line, const char *alias,
 284                        const char **argv)
 285{
 286        const char *event_name = "alias";
 287        struct json_writer jw = JSON_WRITER_INIT;
 288
 289        jw_object_begin(&jw, 0);
 290        event_fmt_prepare(event_name, file, line, NULL, &jw);
 291        jw_object_string(&jw, "alias", alias);
 292        jw_object_inline_begin_array(&jw, "argv");
 293        jw_array_argv(&jw, argv);
 294        jw_end(&jw);
 295        jw_end(&jw);
 296
 297        tr2_dst_write_line(&tr2dst_event, &jw.json);
 298        jw_release(&jw);
 299}
 300
 301static void fn_child_start_fl(const char *file, int line,
 302                              uint64_t us_elapsed_absolute,
 303                              const struct child_process *cmd)
 304{
 305        const char *event_name = "child_start";
 306        struct json_writer jw = JSON_WRITER_INIT;
 307
 308        jw_object_begin(&jw, 0);
 309        event_fmt_prepare(event_name, file, line, NULL, &jw);
 310        jw_object_intmax(&jw, "child_id", cmd->trace2_child_id);
 311        if (cmd->trace2_hook_name) {
 312                jw_object_string(&jw, "child_class", "hook");
 313                jw_object_string(&jw, "hook_name", cmd->trace2_hook_name);
 314        } else {
 315                const char *child_class =
 316                        cmd->trace2_child_class ? cmd->trace2_child_class : "?";
 317                jw_object_string(&jw, "child_class", child_class);
 318        }
 319        if (cmd->dir)
 320                jw_object_string(&jw, "cd", cmd->dir);
 321        jw_object_bool(&jw, "use_shell", cmd->use_shell);
 322        jw_object_inline_begin_array(&jw, "argv");
 323        if (cmd->git_cmd)
 324                jw_array_string(&jw, "git");
 325        jw_array_argv(&jw, cmd->argv);
 326        jw_end(&jw);
 327        jw_end(&jw);
 328
 329        tr2_dst_write_line(&tr2dst_event, &jw.json);
 330        jw_release(&jw);
 331}
 332
 333static void fn_child_exit_fl(const char *file, int line,
 334                             uint64_t us_elapsed_absolute, int cid, int pid,
 335                             int code, uint64_t us_elapsed_child)
 336{
 337        const char *event_name = "child_exit";
 338        struct json_writer jw = JSON_WRITER_INIT;
 339        double t_rel = (double)us_elapsed_child / 1000000.0;
 340
 341        jw_object_begin(&jw, 0);
 342        event_fmt_prepare(event_name, file, line, NULL, &jw);
 343        jw_object_intmax(&jw, "child_id", cid);
 344        jw_object_intmax(&jw, "pid", pid);
 345        jw_object_intmax(&jw, "code", code);
 346        jw_object_double(&jw, "t_rel", 6, t_rel);
 347        jw_end(&jw);
 348
 349        tr2_dst_write_line(&tr2dst_event, &jw.json);
 350
 351        jw_release(&jw);
 352}
 353
 354static void fn_thread_start_fl(const char *file, int line,
 355                               uint64_t us_elapsed_absolute)
 356{
 357        const char *event_name = "thread_start";
 358        struct json_writer jw = JSON_WRITER_INIT;
 359
 360        jw_object_begin(&jw, 0);
 361        event_fmt_prepare(event_name, file, line, NULL, &jw);
 362        jw_end(&jw);
 363
 364        tr2_dst_write_line(&tr2dst_event, &jw.json);
 365        jw_release(&jw);
 366}
 367
 368static void fn_thread_exit_fl(const char *file, int line,
 369                              uint64_t us_elapsed_absolute,
 370                              uint64_t us_elapsed_thread)
 371{
 372        const char *event_name = "thread_exit";
 373        struct json_writer jw = JSON_WRITER_INIT;
 374        double t_rel = (double)us_elapsed_thread / 1000000.0;
 375
 376        jw_object_begin(&jw, 0);
 377        event_fmt_prepare(event_name, file, line, NULL, &jw);
 378        jw_object_double(&jw, "t_rel", 6, t_rel);
 379        jw_end(&jw);
 380
 381        tr2_dst_write_line(&tr2dst_event, &jw.json);
 382        jw_release(&jw);
 383}
 384
 385static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
 386                       int exec_id, const char *exe, const char **argv)
 387{
 388        const char *event_name = "exec";
 389        struct json_writer jw = JSON_WRITER_INIT;
 390
 391        jw_object_begin(&jw, 0);
 392        event_fmt_prepare(event_name, file, line, NULL, &jw);
 393        jw_object_intmax(&jw, "exec_id", exec_id);
 394        if (exe)
 395                jw_object_string(&jw, "exe", exe);
 396        jw_object_inline_begin_array(&jw, "argv");
 397        jw_array_argv(&jw, argv);
 398        jw_end(&jw);
 399        jw_end(&jw);
 400
 401        tr2_dst_write_line(&tr2dst_event, &jw.json);
 402        jw_release(&jw);
 403}
 404
 405static void fn_exec_result_fl(const char *file, int line,
 406                              uint64_t us_elapsed_absolute, int exec_id,
 407                              int code)
 408{
 409        const char *event_name = "exec_result";
 410        struct json_writer jw = JSON_WRITER_INIT;
 411
 412        jw_object_begin(&jw, 0);
 413        event_fmt_prepare(event_name, file, line, NULL, &jw);
 414        jw_object_intmax(&jw, "exec_id", exec_id);
 415        jw_object_intmax(&jw, "code", code);
 416        jw_end(&jw);
 417
 418        tr2_dst_write_line(&tr2dst_event, &jw.json);
 419        jw_release(&jw);
 420}
 421
 422static void fn_param_fl(const char *file, int line, const char *param,
 423                        const char *value)
 424{
 425        const char *event_name = "def_param";
 426        struct json_writer jw = JSON_WRITER_INIT;
 427
 428        jw_object_begin(&jw, 0);
 429        event_fmt_prepare(event_name, file, line, NULL, &jw);
 430        jw_object_string(&jw, "param", param);
 431        jw_object_string(&jw, "value", value);
 432        jw_end(&jw);
 433
 434        tr2_dst_write_line(&tr2dst_event, &jw.json);
 435        jw_release(&jw);
 436}
 437
 438static void fn_repo_fl(const char *file, int line,
 439                       const struct repository *repo)
 440{
 441        const char *event_name = "def_repo";
 442        struct json_writer jw = JSON_WRITER_INIT;
 443
 444        jw_object_begin(&jw, 0);
 445        event_fmt_prepare(event_name, file, line, repo, &jw);
 446        jw_object_string(&jw, "worktree", repo->worktree);
 447        jw_end(&jw);
 448
 449        tr2_dst_write_line(&tr2dst_event, &jw.json);
 450        jw_release(&jw);
 451}
 452
 453static void fn_region_enter_printf_va_fl(const char *file, int line,
 454                                         uint64_t us_elapsed_absolute,
 455                                         const char *category,
 456                                         const char *label,
 457                                         const struct repository *repo,
 458                                         const char *fmt, va_list ap)
 459{
 460        const char *event_name = "region_enter";
 461        struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
 462        if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
 463                struct json_writer jw = JSON_WRITER_INIT;
 464
 465                jw_object_begin(&jw, 0);
 466                event_fmt_prepare(event_name, file, line, repo, &jw);
 467                jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
 468                if (category)
 469                        jw_object_string(&jw, "category", category);
 470                if (label)
 471                        jw_object_string(&jw, "label", label);
 472                maybe_add_string_va(&jw, "msg", fmt, ap);
 473                jw_end(&jw);
 474
 475                tr2_dst_write_line(&tr2dst_event, &jw.json);
 476                jw_release(&jw);
 477        }
 478}
 479
 480static void fn_region_leave_printf_va_fl(
 481        const char *file, int line, uint64_t us_elapsed_absolute,
 482        uint64_t us_elapsed_region, const char *category, const char *label,
 483        const struct repository *repo, const char *fmt, va_list ap)
 484{
 485        const char *event_name = "region_leave";
 486        struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
 487        if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
 488                struct json_writer jw = JSON_WRITER_INIT;
 489                double t_rel = (double)us_elapsed_region / 1000000.0;
 490
 491                jw_object_begin(&jw, 0);
 492                event_fmt_prepare(event_name, file, line, repo, &jw);
 493                jw_object_double(&jw, "t_rel", 6, t_rel);
 494                jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
 495                if (category)
 496                        jw_object_string(&jw, "category", category);
 497                if (label)
 498                        jw_object_string(&jw, "label", label);
 499                maybe_add_string_va(&jw, "msg", fmt, ap);
 500                jw_end(&jw);
 501
 502                tr2_dst_write_line(&tr2dst_event, &jw.json);
 503                jw_release(&jw);
 504        }
 505}
 506
 507static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
 508                       uint64_t us_elapsed_region, const char *category,
 509                       const struct repository *repo, const char *key,
 510                       const char *value)
 511{
 512        const char *event_name = "data";
 513        struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
 514        if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
 515                struct json_writer jw = JSON_WRITER_INIT;
 516                double t_abs = (double)us_elapsed_absolute / 1000000.0;
 517                double t_rel = (double)us_elapsed_region / 1000000.0;
 518
 519                jw_object_begin(&jw, 0);
 520                event_fmt_prepare(event_name, file, line, repo, &jw);
 521                jw_object_double(&jw, "t_abs", 6, t_abs);
 522                jw_object_double(&jw, "t_rel", 6, t_rel);
 523                jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
 524                jw_object_string(&jw, "category", category);
 525                jw_object_string(&jw, "key", key);
 526                jw_object_string(&jw, "value", value);
 527                jw_end(&jw);
 528
 529                tr2_dst_write_line(&tr2dst_event, &jw.json);
 530                jw_release(&jw);
 531        }
 532}
 533
 534static void fn_data_json_fl(const char *file, int line,
 535                            uint64_t us_elapsed_absolute,
 536                            uint64_t us_elapsed_region, const char *category,
 537                            const struct repository *repo, const char *key,
 538                            const struct json_writer *value)
 539{
 540        const char *event_name = "data_json";
 541        struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
 542        if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
 543                struct json_writer jw = JSON_WRITER_INIT;
 544                double t_abs = (double)us_elapsed_absolute / 1000000.0;
 545                double t_rel = (double)us_elapsed_region / 1000000.0;
 546
 547                jw_object_begin(&jw, 0);
 548                event_fmt_prepare(event_name, file, line, repo, &jw);
 549                jw_object_double(&jw, "t_abs", 6, t_abs);
 550                jw_object_double(&jw, "t_rel", 6, t_rel);
 551                jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
 552                jw_object_string(&jw, "category", category);
 553                jw_object_string(&jw, "key", key);
 554                jw_object_sub_jw(&jw, "value", value);
 555                jw_end(&jw);
 556
 557                tr2_dst_write_line(&tr2dst_event, &jw.json);
 558                jw_release(&jw);
 559        }
 560}
 561
 562struct tr2_tgt tr2_tgt_event = {
 563        &tr2dst_event,
 564
 565        fn_init,
 566        fn_term,
 567
 568        fn_version_fl,
 569        fn_start_fl,
 570        fn_exit_fl,
 571        fn_signal,
 572        fn_atexit,
 573        fn_error_va_fl,
 574        fn_command_path_fl,
 575        fn_command_name_fl,
 576        fn_command_mode_fl,
 577        fn_alias_fl,
 578        fn_child_start_fl,
 579        fn_child_exit_fl,
 580        fn_thread_start_fl,
 581        fn_thread_exit_fl,
 582        fn_exec_fl,
 583        fn_exec_result_fl,
 584        fn_param_fl,
 585        fn_repo_fl,
 586        fn_region_enter_printf_va_fl,
 587        fn_region_leave_printf_va_fl,
 588        fn_data_fl,
 589        fn_data_json_fl,
 590        NULL, /* printf */
 591};