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