Git fork
at reftables-rust 409 lines 12 kB view raw
1#include "git-compat-util.h" 2#include "config.h" 3#include "repository.h" 4#include "run-command.h" 5#include "strbuf.h" 6#include "quote.h" 7#include "version.h" 8#include "trace2/tr2_dst.h" 9#include "trace2/tr2_sysenv.h" 10#include "trace2/tr2_tbuf.h" 11#include "trace2/tr2_tgt.h" 12#include "trace2/tr2_tls.h" 13#include "trace2/tr2_tmr.h" 14 15static struct tr2_dst tr2dst_normal = { 16 .sysenv_var = TR2_SYSENV_NORMAL, 17}; 18 19/* 20 * Use the TR2_SYSENV_NORMAL_BRIEF setting to omit the "<time> <file>:<line>" 21 * fields from each line written to the builtin normal target. 22 * 23 * Unit tests may want to use this to help with testing. 24 */ 25static int tr2env_normal_be_brief; 26 27#define TR2FMT_NORMAL_FL_WIDTH (50) 28 29static int fn_init(void) 30{ 31 int want = tr2_dst_trace_want(&tr2dst_normal); 32 int want_brief; 33 const char *brief; 34 35 if (!want) 36 return want; 37 38 brief = tr2_sysenv_get(TR2_SYSENV_NORMAL_BRIEF); 39 if (brief && *brief && 40 ((want_brief = git_parse_maybe_bool(brief)) != -1)) 41 tr2env_normal_be_brief = want_brief; 42 43 return want; 44} 45 46static void fn_term(void) 47{ 48 tr2_dst_trace_disable(&tr2dst_normal); 49} 50 51static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf) 52{ 53 strbuf_setlen(buf, 0); 54 55 if (!tr2env_normal_be_brief) { 56 struct tr2_tbuf tb_now; 57 58 tr2_tbuf_local_time(&tb_now); 59 strbuf_addstr(buf, tb_now.buf); 60 strbuf_addch(buf, ' '); 61 62 if (file && *file) 63 strbuf_addf(buf, "%s:%d ", file, line); 64 while (buf->len < TR2FMT_NORMAL_FL_WIDTH) 65 strbuf_addch(buf, ' '); 66 } 67} 68 69static void normal_io_write_fl(const char *file, int line, 70 const struct strbuf *buf_payload) 71{ 72 struct strbuf buf_line = STRBUF_INIT; 73 74 normal_fmt_prepare(file, line, &buf_line); 75 strbuf_addbuf(&buf_line, buf_payload); 76 tr2_dst_write_line(&tr2dst_normal, &buf_line); 77 strbuf_release(&buf_line); 78} 79 80static void fn_version_fl(const char *file, int line) 81{ 82 struct strbuf buf_payload = STRBUF_INIT; 83 84 strbuf_addf(&buf_payload, "version %s", git_version_string); 85 normal_io_write_fl(file, line, &buf_payload); 86 strbuf_release(&buf_payload); 87} 88 89static void fn_start_fl(const char *file, int line, 90 uint64_t us_elapsed_absolute UNUSED, const char **argv) 91{ 92 struct strbuf buf_payload = STRBUF_INIT; 93 94 strbuf_addstr(&buf_payload, "start "); 95 sq_append_quote_argv_pretty(&buf_payload, argv); 96 normal_io_write_fl(file, line, &buf_payload); 97 strbuf_release(&buf_payload); 98} 99 100static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute, 101 int code) 102{ 103 struct strbuf buf_payload = STRBUF_INIT; 104 double elapsed = (double)us_elapsed_absolute / 1000000.0; 105 106 strbuf_addf(&buf_payload, "exit elapsed:%.6f code:%d", elapsed, code); 107 normal_io_write_fl(file, line, &buf_payload); 108 strbuf_release(&buf_payload); 109} 110 111static void fn_signal(uint64_t us_elapsed_absolute, int signo) 112{ 113 struct strbuf buf_payload = STRBUF_INIT; 114 double elapsed = (double)us_elapsed_absolute / 1000000.0; 115 116 strbuf_addf(&buf_payload, "signal elapsed:%.6f code:%d", elapsed, 117 signo); 118 normal_io_write_fl(__FILE__, __LINE__, &buf_payload); 119 strbuf_release(&buf_payload); 120} 121 122static void fn_atexit(uint64_t us_elapsed_absolute, int code) 123{ 124 struct strbuf buf_payload = STRBUF_INIT; 125 double elapsed = (double)us_elapsed_absolute / 1000000.0; 126 127 strbuf_addf(&buf_payload, "atexit elapsed:%.6f code:%d", elapsed, code); 128 normal_io_write_fl(__FILE__, __LINE__, &buf_payload); 129 strbuf_release(&buf_payload); 130} 131 132static void maybe_append_string_va(struct strbuf *buf, const char *fmt, 133 va_list ap) 134{ 135 if (fmt && *fmt) { 136 va_list copy_ap; 137 138 va_copy(copy_ap, ap); 139 strbuf_vaddf(buf, fmt, copy_ap); 140 va_end(copy_ap); 141 return; 142 } 143} 144 145static void fn_error_va_fl(const char *file, int line, const char *fmt, 146 va_list ap) 147{ 148 struct strbuf buf_payload = STRBUF_INIT; 149 150 strbuf_addstr(&buf_payload, "error"); 151 if (fmt && *fmt) { 152 strbuf_addch(&buf_payload, ' '); 153 maybe_append_string_va(&buf_payload, fmt, ap); 154 } 155 normal_io_write_fl(file, line, &buf_payload); 156 strbuf_release(&buf_payload); 157} 158 159static void fn_command_path_fl(const char *file, int line, const char *pathname) 160{ 161 struct strbuf buf_payload = STRBUF_INIT; 162 163 strbuf_addf(&buf_payload, "cmd_path %s", pathname); 164 normal_io_write_fl(file, line, &buf_payload); 165 strbuf_release(&buf_payload); 166} 167 168static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names) 169{ 170 const char *parent_name = NULL; 171 struct strbuf buf_payload = STRBUF_INIT; 172 173 /* cmd_ancestry parent <- grandparent <- great-grandparent */ 174 strbuf_addstr(&buf_payload, "cmd_ancestry "); 175 while ((parent_name = *parent_names++)) { 176 strbuf_addstr(&buf_payload, parent_name); 177 /* if we'll write another one after this, add a delimiter */ 178 if (parent_names && *parent_names) 179 strbuf_addstr(&buf_payload, " <- "); 180 } 181 182 normal_io_write_fl(file, line, &buf_payload); 183 strbuf_release(&buf_payload); 184} 185 186static void fn_command_name_fl(const char *file, int line, const char *name, 187 const char *hierarchy) 188{ 189 struct strbuf buf_payload = STRBUF_INIT; 190 191 strbuf_addf(&buf_payload, "cmd_name %s", name); 192 if (hierarchy && *hierarchy) 193 strbuf_addf(&buf_payload, " (%s)", hierarchy); 194 normal_io_write_fl(file, line, &buf_payload); 195 strbuf_release(&buf_payload); 196} 197 198static void fn_command_mode_fl(const char *file, int line, const char *mode) 199{ 200 struct strbuf buf_payload = STRBUF_INIT; 201 202 strbuf_addf(&buf_payload, "cmd_mode %s", mode); 203 normal_io_write_fl(file, line, &buf_payload); 204 strbuf_release(&buf_payload); 205} 206 207static void fn_alias_fl(const char *file, int line, const char *alias, 208 const char **argv) 209{ 210 struct strbuf buf_payload = STRBUF_INIT; 211 212 strbuf_addf(&buf_payload, "alias %s -> ", alias); 213 sq_append_quote_argv_pretty(&buf_payload, argv); 214 normal_io_write_fl(file, line, &buf_payload); 215 strbuf_release(&buf_payload); 216} 217 218static void fn_child_start_fl(const char *file, int line, 219 uint64_t us_elapsed_absolute UNUSED, 220 const struct child_process *cmd) 221{ 222 struct strbuf buf_payload = STRBUF_INIT; 223 224 strbuf_addf(&buf_payload, "child_start[%d]", cmd->trace2_child_id); 225 226 if (cmd->dir) { 227 strbuf_addstr(&buf_payload, " cd "); 228 sq_quote_buf_pretty(&buf_payload, cmd->dir); 229 strbuf_addstr(&buf_payload, ";"); 230 } 231 232 /* 233 * TODO if (cmd->env) { Consider dumping changes to environment. } 234 * See trace_add_env() in run-command.c as used by original trace.c 235 */ 236 237 strbuf_addch(&buf_payload, ' '); 238 if (cmd->git_cmd) 239 strbuf_addstr(&buf_payload, "git "); 240 sq_append_quote_argv_pretty(&buf_payload, cmd->args.v); 241 242 normal_io_write_fl(file, line, &buf_payload); 243 strbuf_release(&buf_payload); 244} 245 246static void fn_child_exit_fl(const char *file, int line, 247 uint64_t us_elapsed_absolute UNUSED, 248 int cid, int pid, 249 int code, uint64_t us_elapsed_child) 250{ 251 struct strbuf buf_payload = STRBUF_INIT; 252 double elapsed = (double)us_elapsed_child / 1000000.0; 253 254 strbuf_addf(&buf_payload, "child_exit[%d] pid:%d code:%d elapsed:%.6f", 255 cid, pid, code, elapsed); 256 normal_io_write_fl(file, line, &buf_payload); 257 strbuf_release(&buf_payload); 258} 259 260static void fn_child_ready_fl(const char *file, int line, 261 uint64_t us_elapsed_absolute UNUSED, 262 int cid, int pid, 263 const char *ready, uint64_t us_elapsed_child) 264{ 265 struct strbuf buf_payload = STRBUF_INIT; 266 double elapsed = (double)us_elapsed_child / 1000000.0; 267 268 strbuf_addf(&buf_payload, "child_ready[%d] pid:%d ready:%s elapsed:%.6f", 269 cid, pid, ready, elapsed); 270 normal_io_write_fl(file, line, &buf_payload); 271 strbuf_release(&buf_payload); 272} 273 274static void fn_exec_fl(const char *file, int line, 275 uint64_t us_elapsed_absolute UNUSED, 276 int exec_id, const char *exe, const char **argv) 277{ 278 struct strbuf buf_payload = STRBUF_INIT; 279 280 strbuf_addf(&buf_payload, "exec[%d] ", exec_id); 281 if (exe) { 282 strbuf_addstr(&buf_payload, exe); 283 strbuf_addch(&buf_payload, ' '); 284 } 285 sq_append_quote_argv_pretty(&buf_payload, argv); 286 normal_io_write_fl(file, line, &buf_payload); 287 strbuf_release(&buf_payload); 288} 289 290static void fn_exec_result_fl(const char *file, int line, 291 uint64_t us_elapsed_absolute UNUSED, 292 int exec_id, int code) 293{ 294 struct strbuf buf_payload = STRBUF_INIT; 295 296 strbuf_addf(&buf_payload, "exec_result[%d] code:%d", exec_id, code); 297 if (code > 0) 298 strbuf_addf(&buf_payload, " err:%s", strerror(code)); 299 normal_io_write_fl(file, line, &buf_payload); 300 strbuf_release(&buf_payload); 301} 302 303static void fn_param_fl(const char *file, int line, const char *param, 304 const char *value, const struct key_value_info *kvi) 305{ 306 struct strbuf buf_payload = STRBUF_INIT; 307 enum config_scope scope = kvi->scope; 308 const char *scope_name = config_scope_name(scope); 309 310 strbuf_addf(&buf_payload, "def_param scope:%s %s", scope_name, param); 311 if (value) 312 strbuf_addf(&buf_payload, "=%s", value); 313 normal_io_write_fl(file, line, &buf_payload); 314 strbuf_release(&buf_payload); 315} 316 317static void fn_repo_fl(const char *file, int line, 318 const struct repository *repo) 319{ 320 struct strbuf buf_payload = STRBUF_INIT; 321 322 strbuf_addstr(&buf_payload, "worktree "); 323 sq_quote_buf_pretty(&buf_payload, repo->worktree); 324 normal_io_write_fl(file, line, &buf_payload); 325 strbuf_release(&buf_payload); 326} 327 328static void fn_printf_va_fl(const char *file, int line, 329 uint64_t us_elapsed_absolute UNUSED, 330 const char *fmt, 331 va_list ap) 332{ 333 struct strbuf buf_payload = STRBUF_INIT; 334 335 maybe_append_string_va(&buf_payload, fmt, ap); 336 normal_io_write_fl(file, line, &buf_payload); 337 strbuf_release(&buf_payload); 338} 339 340static void fn_timer(const struct tr2_timer_metadata *meta, 341 const struct tr2_timer *timer, 342 int is_final_data) 343{ 344 const char *event_name = is_final_data ? "timer" : "th_timer"; 345 struct strbuf buf_payload = STRBUF_INIT; 346 double t_total = NS_TO_SEC(timer->total_ns); 347 double t_min = NS_TO_SEC(timer->min_ns); 348 double t_max = NS_TO_SEC(timer->max_ns); 349 350 strbuf_addf(&buf_payload, ("%s %s/%s" 351 " intervals:%"PRIu64 352 " total:%8.6f min:%8.6f max:%8.6f"), 353 event_name, meta->category, meta->name, 354 timer->interval_count, 355 t_total, t_min, t_max); 356 357 normal_io_write_fl(__FILE__, __LINE__, &buf_payload); 358 strbuf_release(&buf_payload); 359} 360 361static void fn_counter(const struct tr2_counter_metadata *meta, 362 const struct tr2_counter *counter, 363 int is_final_data) 364{ 365 const char *event_name = is_final_data ? "counter" : "th_counter"; 366 struct strbuf buf_payload = STRBUF_INIT; 367 368 strbuf_addf(&buf_payload, "%s %s/%s value:%"PRIu64, 369 event_name, meta->category, meta->name, 370 counter->value); 371 372 normal_io_write_fl(__FILE__, __LINE__, &buf_payload); 373 strbuf_release(&buf_payload); 374} 375 376struct tr2_tgt tr2_tgt_normal = { 377 .pdst = &tr2dst_normal, 378 379 .pfn_init = fn_init, 380 .pfn_term = fn_term, 381 382 .pfn_version_fl = fn_version_fl, 383 .pfn_start_fl = fn_start_fl, 384 .pfn_exit_fl = fn_exit_fl, 385 .pfn_signal = fn_signal, 386 .pfn_atexit = fn_atexit, 387 .pfn_error_va_fl = fn_error_va_fl, 388 .pfn_command_path_fl = fn_command_path_fl, 389 .pfn_command_ancestry_fl = fn_command_ancestry_fl, 390 .pfn_command_name_fl = fn_command_name_fl, 391 .pfn_command_mode_fl = fn_command_mode_fl, 392 .pfn_alias_fl = fn_alias_fl, 393 .pfn_child_start_fl = fn_child_start_fl, 394 .pfn_child_exit_fl = fn_child_exit_fl, 395 .pfn_child_ready_fl = fn_child_ready_fl, 396 .pfn_thread_start_fl = NULL, 397 .pfn_thread_exit_fl = NULL, 398 .pfn_exec_fl = fn_exec_fl, 399 .pfn_exec_result_fl = fn_exec_result_fl, 400 .pfn_param_fl = fn_param_fl, 401 .pfn_repo_fl = fn_repo_fl, 402 .pfn_region_enter_printf_va_fl = NULL, 403 .pfn_region_leave_printf_va_fl = NULL, 404 .pfn_data_fl = NULL, 405 .pfn_data_json_fl = NULL, 406 .pfn_printf_va_fl = fn_printf_va_fl, 407 .pfn_timer = fn_timer, 408 .pfn_counter = fn_counter, 409};