Skip to content

Commit 0d7f242

Browse files
committed
Merge branch 'jk/trace-sifter'
* jk/trace-sifter: trace: give repo_setup trace its own key add packet tracing debug code trace: add trace_strbuf trace: factor out "do we want to trace" logic trace: refactor to support multiple env variables trace: add trace_vprintf
2 parents dc7f96f + 7e2342d commit 0d7f242

File tree

10 files changed

+121
-24
lines changed

10 files changed

+121
-24
lines changed

builtin/clone.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
383383

384384
junk_pid = getpid();
385385

386+
packet_trace_identity("clone");
386387
argc = parse_options(argc, argv, prefix, builtin_clone_options,
387388
builtin_clone_usage, 0);
388389

builtin/fetch-pack.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,8 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
804804
char **pack_lockfile_ptr = NULL;
805805
struct child_process *conn;
806806

807+
packet_trace_identity("fetch-pack");
808+
807809
nr_heads = 0;
808810
heads = NULL;
809811
for (i = 1; i < argc; i++) {

builtin/fetch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
906906
struct remote *remote;
907907
int result = 0;
908908

909+
packet_trace_identity("fetch");
910+
909911
/* Record the command line for the reflog */
910912
strbuf_addstr(&default_rla, "fetch");
911913
for (i = 1; i < argc; i++)

builtin/push.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
245245
OPT_END()
246246
};
247247

248+
packet_trace_identity("push");
248249
git_config(git_default_config, NULL);
249250
argc = parse_options(argc, argv, prefix, options, push_usage, 0);
250251

builtin/receive-pack.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,8 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
778778
char *dir = NULL;
779779
struct command *commands;
780780

781+
packet_trace_identity("receive-pack");
782+
781783
argv++;
782784
for (i = 1; i < argc; i++) {
783785
const char *arg = *argv++;

cache.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,9 +1083,14 @@ extern void alloc_report(void);
10831083
/* trace.c */
10841084
__attribute__((format (printf, 1, 2)))
10851085
extern void trace_printf(const char *format, ...);
1086+
extern void trace_vprintf(const char *key, const char *format, va_list ap);
10861087
__attribute__((format (printf, 2, 3)))
10871088
extern void trace_argv_printf(const char **argv, const char *format, ...);
10881089
extern void trace_repo_setup(const char *prefix);
1090+
extern int trace_want(const char *key);
1091+
extern void trace_strbuf(const char *key, const struct strbuf *buf);
1092+
1093+
void packet_trace_identity(const char *prog);
10891094

10901095
/* convert.c */
10911096
/* returns 1 if *dst was used */

pkt-line.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,51 @@
11
#include "cache.h"
22
#include "pkt-line.h"
33

4+
const char *packet_trace_prefix = "git";
5+
static const char trace_key[] = "GIT_TRACE_PACKET";
6+
7+
void packet_trace_identity(const char *prog)
8+
{
9+
packet_trace_prefix = xstrdup(prog);
10+
}
11+
12+
static void packet_trace(const char *buf, unsigned int len, int write)
13+
{
14+
int i;
15+
struct strbuf out;
16+
17+
if (!trace_want(trace_key))
18+
return;
19+
20+
/* +32 is just a guess for header + quoting */
21+
strbuf_init(&out, len+32);
22+
23+
strbuf_addf(&out, "packet: %12s%c ",
24+
packet_trace_prefix, write ? '>' : '<');
25+
26+
if ((len >= 4 && !prefixcmp(buf, "PACK")) ||
27+
(len >= 5 && !prefixcmp(buf+1, "PACK"))) {
28+
strbuf_addstr(&out, "PACK ...");
29+
unsetenv(trace_key);
30+
}
31+
else {
32+
/* XXX we should really handle printable utf8 */
33+
for (i = 0; i < len; i++) {
34+
/* suppress newlines */
35+
if (buf[i] == '\n')
36+
continue;
37+
if (buf[i] >= 0x20 && buf[i] <= 0x7e)
38+
strbuf_addch(&out, buf[i]);
39+
else
40+
strbuf_addf(&out, "\\%o", buf[i]);
41+
}
42+
}
43+
44+
strbuf_addch(&out, '\n');
45+
trace_strbuf(trace_key, &out);
46+
strbuf_release(&out);
47+
}
48+
449
/*
550
* Write a packetized stream, where each line is preceded by
651
* its length (including the header) as a 4-byte hex number.
@@ -39,11 +84,13 @@ ssize_t safe_write(int fd, const void *buf, ssize_t n)
3984
*/
4085
void packet_flush(int fd)
4186
{
87+
packet_trace("0000", 4, 1);
4288
safe_write(fd, "0000", 4);
4389
}
4490

4591
void packet_buf_flush(struct strbuf *buf)
4692
{
93+
packet_trace("0000", 4, 1);
4794
strbuf_add(buf, "0000", 4);
4895
}
4996

@@ -62,6 +109,7 @@ static unsigned format_packet(const char *fmt, va_list args)
62109
buffer[1] = hex(n >> 8);
63110
buffer[2] = hex(n >> 4);
64111
buffer[3] = hex(n);
112+
packet_trace(buffer+4, n-4, 1);
65113
return n;
66114
}
67115

@@ -130,13 +178,16 @@ int packet_read_line(int fd, char *buffer, unsigned size)
130178
len = packet_length(linelen);
131179
if (len < 0)
132180
die("protocol error: bad line length character: %.4s", linelen);
133-
if (!len)
181+
if (!len) {
182+
packet_trace("0000", 4, 0);
134183
return 0;
184+
}
135185
len -= 4;
136186
if (len >= size)
137187
die("protocol error: bad line length %d", len);
138188
safe_read(fd, buffer, len);
139189
buffer[len] = 0;
190+
packet_trace(buffer, len, 0);
140191
return len;
141192
}
142193

@@ -153,6 +204,7 @@ int packet_get_line(struct strbuf *out,
153204
if (!len) {
154205
*src_buf += 4;
155206
*src_len -= 4;
207+
packet_trace("0000", 4, 0);
156208
return 0;
157209
}
158210
if (*src_len < len)
@@ -165,5 +217,6 @@ int packet_get_line(struct strbuf *out,
165217
strbuf_add(out, *src_buf, len);
166218
*src_buf += len;
167219
*src_len -= len;
220+
packet_trace(out->buf, out->len, 0);
168221
return len;
169222
}

t/t1510-repo-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test_repo () {
5757
export GIT_WORK_TREE
5858
fi &&
5959
rm -f trace &&
60-
GIT_TRACE="$(pwd)/trace" git symbolic-ref HEAD >/dev/null &&
60+
GIT_TRACE_SETUP="$(pwd)/trace" git symbolic-ref HEAD >/dev/null &&
6161
grep '^setup: ' trace >result &&
6262
test_cmp expected result
6363
)

trace.c

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
#include "cache.h"
2626
#include "quote.h"
2727

28-
/* Get a trace file descriptor from GIT_TRACE env variable. */
29-
static int get_trace_fd(int *need_close)
28+
/* Get a trace file descriptor from "key" env variable. */
29+
static int get_trace_fd(const char *key, int *need_close)
3030
{
31-
char *trace = getenv("GIT_TRACE");
31+
char *trace = getenv(key);
3232

3333
if (!trace || !strcmp(trace, "") ||
3434
!strcmp(trace, "0") || !strcasecmp(trace, "false"))
@@ -50,10 +50,10 @@ static int get_trace_fd(int *need_close)
5050
return fd;
5151
}
5252

53-
fprintf(stderr, "What does '%s' for GIT_TRACE mean?\n", trace);
53+
fprintf(stderr, "What does '%s' for %s mean?\n", trace, key);
5454
fprintf(stderr, "If you want to trace into a file, "
55-
"then please set GIT_TRACE to an absolute pathname "
56-
"(starting with /).\n");
55+
"then please set %s to an absolute pathname "
56+
"(starting with /).\n", key);
5757
fprintf(stderr, "Defaulting to tracing on stderr...\n");
5858

5959
return STDERR_FILENO;
@@ -62,23 +62,44 @@ static int get_trace_fd(int *need_close)
6262
static const char err_msg[] = "Could not trace into fd given by "
6363
"GIT_TRACE environment variable";
6464

65-
void trace_printf(const char *fmt, ...)
65+
void trace_vprintf(const char *key, const char *fmt, va_list ap)
6666
{
6767
struct strbuf buf = STRBUF_INIT;
68-
va_list ap;
69-
int fd, need_close = 0;
7068

71-
fd = get_trace_fd(&need_close);
72-
if (!fd)
69+
if (!trace_want(key))
7370
return;
7471

7572
set_try_to_free_routine(NULL); /* is never reset */
76-
va_start(ap, fmt);
7773
strbuf_vaddf(&buf, fmt, ap);
74+
trace_strbuf(key, &buf);
75+
strbuf_release(&buf);
76+
}
77+
78+
void trace_printf_key(const char *key, const char *fmt, ...)
79+
{
80+
va_list ap;
81+
va_start(ap, fmt);
82+
trace_vprintf(key, fmt, ap);
7883
va_end(ap);
84+
}
7985

80-
write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
81-
strbuf_release(&buf);
86+
void trace_printf(const char *fmt, ...)
87+
{
88+
va_list ap;
89+
va_start(ap, fmt);
90+
trace_vprintf("GIT_TRACE", fmt, ap);
91+
va_end(ap);
92+
}
93+
94+
void trace_strbuf(const char *key, const struct strbuf *buf)
95+
{
96+
int fd, need_close = 0;
97+
98+
fd = get_trace_fd(key, &need_close);
99+
if (!fd)
100+
return;
101+
102+
write_or_whine_pipe(fd, buf->buf, buf->len, err_msg);
82103

83104
if (need_close)
84105
close(fd);
@@ -90,7 +111,7 @@ void trace_argv_printf(const char **argv, const char *fmt, ...)
90111
va_list ap;
91112
int fd, need_close = 0;
92113

93-
fd = get_trace_fd(&need_close);
114+
fd = get_trace_fd("GIT_TRACE", &need_close);
94115
if (!fd)
95116
return;
96117

@@ -134,12 +155,11 @@ static const char *quote_crnl(const char *path)
134155
/* FIXME: move prefix to startup_info struct and get rid of this arg */
135156
void trace_repo_setup(const char *prefix)
136157
{
158+
static const char *key = "GIT_TRACE_SETUP";
137159
const char *git_work_tree;
138160
char cwd[PATH_MAX];
139-
char *trace = getenv("GIT_TRACE");
140161

141-
if (!trace || !strcmp(trace, "") ||
142-
!strcmp(trace, "0") || !strcasecmp(trace, "false"))
162+
if (!trace_want(key))
143163
return;
144164

145165
if (!getcwd(cwd, PATH_MAX))
@@ -151,8 +171,18 @@ void trace_repo_setup(const char *prefix)
151171
if (!prefix)
152172
prefix = "(null)";
153173

154-
trace_printf("setup: git_dir: %s\n", quote_crnl(get_git_dir()));
155-
trace_printf("setup: worktree: %s\n", quote_crnl(git_work_tree));
156-
trace_printf("setup: cwd: %s\n", quote_crnl(cwd));
157-
trace_printf("setup: prefix: %s\n", quote_crnl(prefix));
174+
trace_printf_key(key, "setup: git_dir: %s\n", quote_crnl(get_git_dir()));
175+
trace_printf_key(key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
176+
trace_printf_key(key, "setup: cwd: %s\n", quote_crnl(cwd));
177+
trace_printf_key(key, "setup: prefix: %s\n", quote_crnl(prefix));
178+
}
179+
180+
int trace_want(const char *key)
181+
{
182+
const char *trace = getenv(key);
183+
184+
if (!trace || !strcmp(trace, "") ||
185+
!strcmp(trace, "0") || !strcasecmp(trace, "false"))
186+
return 0;
187+
return 1;
158188
}

upload-pack.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ int main(int argc, char **argv)
682682
int i;
683683
int strict = 0;
684684

685+
packet_trace_identity("upload-pack");
685686
git_extract_argv0_path(argv[0]);
686687
read_replace_refs = 0;
687688

0 commit comments

Comments
 (0)